Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * APM X-Gene SoC PMU (Performance Monitor Unit)
   4 *
   5 * Copyright (c) 2016, Applied Micro Circuits Corporation
   6 * Author: Hoan Tran <hotran@apm.com>
   7 *         Tai Nguyen <ttnguyen@apm.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
   8 */
   9
  10#include <linux/acpi.h>
  11#include <linux/clk.h>
  12#include <linux/cpuhotplug.h>
  13#include <linux/cpumask.h>
  14#include <linux/interrupt.h>
  15#include <linux/io.h>
  16#include <linux/mfd/syscon.h>
  17#include <linux/module.h>
  18#include <linux/of_address.h>
 
 
 
  19#include <linux/perf_event.h>
  20#include <linux/platform_device.h>
  21#include <linux/property.h>
  22#include <linux/regmap.h>
  23#include <linux/slab.h>
  24
  25#define CSW_CSWCR                       0x0000
  26#define  CSW_CSWCR_DUALMCB_MASK         BIT(0)
  27#define  CSW_CSWCR_MCB0_ROUTING(x)	(((x) & 0x0C) >> 2)
  28#define  CSW_CSWCR_MCB1_ROUTING(x)	(((x) & 0x30) >> 4)
  29#define MCBADDRMR                       0x0000
  30#define  MCBADDRMR_DUALMCU_MODE_MASK    BIT(2)
  31
  32#define PCPPMU_INTSTATUS_REG	0x000
  33#define PCPPMU_INTMASK_REG	0x004
  34#define  PCPPMU_INTMASK		0x0000000F
  35#define  PCPPMU_INTENMASK	0xFFFFFFFF
  36#define  PCPPMU_INTCLRMASK	0xFFFFFFF0
  37#define  PCPPMU_INT_MCU		BIT(0)
  38#define  PCPPMU_INT_MCB		BIT(1)
  39#define  PCPPMU_INT_L3C		BIT(2)
  40#define  PCPPMU_INT_IOB		BIT(3)
  41
  42#define  PCPPMU_V3_INTMASK	0x00FF33FF
  43#define  PCPPMU_V3_INTENMASK	0xFFFFFFFF
  44#define  PCPPMU_V3_INTCLRMASK	0xFF00CC00
  45#define  PCPPMU_V3_INT_MCU	0x000000FF
  46#define  PCPPMU_V3_INT_MCB	0x00000300
  47#define  PCPPMU_V3_INT_L3C	0x00FF0000
  48#define  PCPPMU_V3_INT_IOB	0x00003000
  49
  50#define PMU_MAX_COUNTERS	4
  51#define PMU_CNT_MAX_PERIOD	0xFFFFFFFFULL
  52#define PMU_V3_CNT_MAX_PERIOD	0xFFFFFFFFFFFFFFFFULL
  53#define PMU_OVERFLOW_MASK	0xF
  54#define PMU_PMCR_E		BIT(0)
  55#define PMU_PMCR_P		BIT(1)
  56
  57#define PMU_PMEVCNTR0		0x000
  58#define PMU_PMEVCNTR1		0x004
  59#define PMU_PMEVCNTR2		0x008
  60#define PMU_PMEVCNTR3		0x00C
  61#define PMU_PMEVTYPER0		0x400
  62#define PMU_PMEVTYPER1		0x404
  63#define PMU_PMEVTYPER2		0x408
  64#define PMU_PMEVTYPER3		0x40C
  65#define PMU_PMAMR0		0xA00
  66#define PMU_PMAMR1		0xA04
  67#define PMU_PMCNTENSET		0xC00
  68#define PMU_PMCNTENCLR		0xC20
  69#define PMU_PMINTENSET		0xC40
  70#define PMU_PMINTENCLR		0xC60
  71#define PMU_PMOVSR		0xC80
  72#define PMU_PMCR		0xE04
  73
  74/* PMU registers for V3 */
  75#define PMU_PMOVSCLR		0xC80
  76#define PMU_PMOVSSET		0xCC0
  77
  78#define to_pmu_dev(p)     container_of(p, struct xgene_pmu_dev, pmu)
  79#define GET_CNTR(ev)      (ev->hw.idx)
  80#define GET_EVENTID(ev)   (ev->hw.config & 0xFFULL)
  81#define GET_AGENTID(ev)   (ev->hw.config_base & 0xFFFFFFFFUL)
  82#define GET_AGENT1ID(ev)  ((ev->hw.config_base >> 32) & 0xFFFFFFFFUL)
  83
  84struct hw_pmu_info {
  85	u32 type;
  86	u32 enable_mask;
  87	void __iomem *csr;
  88};
  89
  90struct xgene_pmu_dev {
  91	struct hw_pmu_info *inf;
  92	struct xgene_pmu *parent;
  93	struct pmu pmu;
  94	u8 max_counters;
  95	DECLARE_BITMAP(cntr_assign_mask, PMU_MAX_COUNTERS);
  96	u64 max_period;
  97	const struct attribute_group **attr_groups;
  98	struct perf_event *pmu_counter_event[PMU_MAX_COUNTERS];
  99};
 100
 101struct xgene_pmu_ops {
 102	void (*mask_int)(struct xgene_pmu *pmu);
 103	void (*unmask_int)(struct xgene_pmu *pmu);
 104	u64 (*read_counter)(struct xgene_pmu_dev *pmu, int idx);
 105	void (*write_counter)(struct xgene_pmu_dev *pmu, int idx, u64 val);
 106	void (*write_evttype)(struct xgene_pmu_dev *pmu_dev, int idx, u32 val);
 107	void (*write_agentmsk)(struct xgene_pmu_dev *pmu_dev, u32 val);
 108	void (*write_agent1msk)(struct xgene_pmu_dev *pmu_dev, u32 val);
 109	void (*enable_counter)(struct xgene_pmu_dev *pmu_dev, int idx);
 110	void (*disable_counter)(struct xgene_pmu_dev *pmu_dev, int idx);
 111	void (*enable_counter_int)(struct xgene_pmu_dev *pmu_dev, int idx);
 112	void (*disable_counter_int)(struct xgene_pmu_dev *pmu_dev, int idx);
 113	void (*reset_counters)(struct xgene_pmu_dev *pmu_dev);
 114	void (*start_counters)(struct xgene_pmu_dev *pmu_dev);
 115	void (*stop_counters)(struct xgene_pmu_dev *pmu_dev);
 116};
 117
 118struct xgene_pmu {
 119	struct device *dev;
 120	struct hlist_node node;
 121	int version;
 122	void __iomem *pcppmu_csr;
 123	u32 mcb_active_mask;
 124	u32 mc_active_mask;
 125	u32 l3c_active_mask;
 126	cpumask_t cpu;
 127	int irq;
 128	raw_spinlock_t lock;
 129	const struct xgene_pmu_ops *ops;
 130	struct list_head l3cpmus;
 131	struct list_head iobpmus;
 132	struct list_head mcbpmus;
 133	struct list_head mcpmus;
 134};
 135
 136struct xgene_pmu_dev_ctx {
 137	char *name;
 138	struct list_head next;
 139	struct xgene_pmu_dev *pmu_dev;
 140	struct hw_pmu_info inf;
 141};
 142
 143struct xgene_pmu_data {
 144	int id;
 145	u32 data;
 146};
 147
 148enum xgene_pmu_version {
 149	PCP_PMU_V1 = 1,
 150	PCP_PMU_V2,
 151	PCP_PMU_V3,
 152};
 153
 154enum xgene_pmu_dev_type {
 155	PMU_TYPE_L3C = 0,
 156	PMU_TYPE_IOB,
 157	PMU_TYPE_IOB_SLOW,
 158	PMU_TYPE_MCB,
 159	PMU_TYPE_MC,
 160};
 161
 162/*
 163 * sysfs format attributes
 164 */
 165static ssize_t xgene_pmu_format_show(struct device *dev,
 166				     struct device_attribute *attr, char *buf)
 167{
 168	struct dev_ext_attribute *eattr;
 169
 170	eattr = container_of(attr, struct dev_ext_attribute, attr);
 171	return sysfs_emit(buf, "%s\n", (char *) eattr->var);
 172}
 173
 174#define XGENE_PMU_FORMAT_ATTR(_name, _config)		\
 175	(&((struct dev_ext_attribute[]) {		\
 176		{ .attr = __ATTR(_name, S_IRUGO, xgene_pmu_format_show, NULL), \
 177		  .var = (void *) _config, }		\
 178	})[0].attr.attr)
 179
 180static struct attribute *l3c_pmu_format_attrs[] = {
 181	XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-7"),
 182	XGENE_PMU_FORMAT_ATTR(l3c_agentid, "config1:0-9"),
 183	NULL,
 184};
 185
 186static struct attribute *iob_pmu_format_attrs[] = {
 187	XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-7"),
 188	XGENE_PMU_FORMAT_ATTR(iob_agentid, "config1:0-63"),
 189	NULL,
 190};
 191
 192static struct attribute *mcb_pmu_format_attrs[] = {
 193	XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-5"),
 194	XGENE_PMU_FORMAT_ATTR(mcb_agentid, "config1:0-9"),
 195	NULL,
 196};
 197
 198static struct attribute *mc_pmu_format_attrs[] = {
 199	XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-28"),
 200	NULL,
 201};
 202
 203static const struct attribute_group l3c_pmu_format_attr_group = {
 204	.name = "format",
 205	.attrs = l3c_pmu_format_attrs,
 206};
 207
 208static const struct attribute_group iob_pmu_format_attr_group = {
 209	.name = "format",
 210	.attrs = iob_pmu_format_attrs,
 211};
 212
 213static const struct attribute_group mcb_pmu_format_attr_group = {
 214	.name = "format",
 215	.attrs = mcb_pmu_format_attrs,
 216};
 217
 218static const struct attribute_group mc_pmu_format_attr_group = {
 219	.name = "format",
 220	.attrs = mc_pmu_format_attrs,
 221};
 222
 223static struct attribute *l3c_pmu_v3_format_attrs[] = {
 224	XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-39"),
 225	NULL,
 226};
 227
 228static struct attribute *iob_pmu_v3_format_attrs[] = {
 229	XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-47"),
 230	NULL,
 231};
 232
 233static struct attribute *iob_slow_pmu_v3_format_attrs[] = {
 234	XGENE_PMU_FORMAT_ATTR(iob_slow_eventid, "config:0-16"),
 235	NULL,
 236};
 237
 238static struct attribute *mcb_pmu_v3_format_attrs[] = {
 239	XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-35"),
 240	NULL,
 241};
 242
 243static struct attribute *mc_pmu_v3_format_attrs[] = {
 244	XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-44"),
 245	NULL,
 246};
 247
 248static const struct attribute_group l3c_pmu_v3_format_attr_group = {
 249	.name = "format",
 250	.attrs = l3c_pmu_v3_format_attrs,
 251};
 252
 253static const struct attribute_group iob_pmu_v3_format_attr_group = {
 254	.name = "format",
 255	.attrs = iob_pmu_v3_format_attrs,
 256};
 257
 258static const struct attribute_group iob_slow_pmu_v3_format_attr_group = {
 259	.name = "format",
 260	.attrs = iob_slow_pmu_v3_format_attrs,
 261};
 262
 263static const struct attribute_group mcb_pmu_v3_format_attr_group = {
 264	.name = "format",
 265	.attrs = mcb_pmu_v3_format_attrs,
 266};
 267
 268static const struct attribute_group mc_pmu_v3_format_attr_group = {
 269	.name = "format",
 270	.attrs = mc_pmu_v3_format_attrs,
 271};
 272
 273/*
 274 * sysfs event attributes
 275 */
 276static ssize_t xgene_pmu_event_show(struct device *dev,
 277				    struct device_attribute *attr, char *buf)
 278{
 279	struct perf_pmu_events_attr *pmu_attr =
 280		container_of(attr, struct perf_pmu_events_attr, attr);
 281
 282	return sysfs_emit(buf, "config=0x%llx\n", pmu_attr->id);
 
 283}
 284
 285#define XGENE_PMU_EVENT_ATTR(_name, _config)		\
 286	PMU_EVENT_ATTR_ID(_name, xgene_pmu_event_show, _config)
 
 
 
 287
 288static struct attribute *l3c_pmu_events_attrs[] = {
 289	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 290	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 291	XGENE_PMU_EVENT_ATTR(read-hit,				0x02),
 292	XGENE_PMU_EVENT_ATTR(read-miss,				0x03),
 293	XGENE_PMU_EVENT_ATTR(write-need-replacement,		0x06),
 294	XGENE_PMU_EVENT_ATTR(write-not-need-replacement,	0x07),
 295	XGENE_PMU_EVENT_ATTR(tq-full,				0x08),
 296	XGENE_PMU_EVENT_ATTR(ackq-full,				0x09),
 297	XGENE_PMU_EVENT_ATTR(wdb-full,				0x0a),
 298	XGENE_PMU_EVENT_ATTR(bank-fifo-full,			0x0b),
 299	XGENE_PMU_EVENT_ATTR(odb-full,				0x0c),
 300	XGENE_PMU_EVENT_ATTR(wbq-full,				0x0d),
 301	XGENE_PMU_EVENT_ATTR(bank-conflict-fifo-issue,		0x0e),
 302	XGENE_PMU_EVENT_ATTR(bank-fifo-issue,			0x0f),
 303	NULL,
 304};
 305
 306static struct attribute *iob_pmu_events_attrs[] = {
 307	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 308	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 309	XGENE_PMU_EVENT_ATTR(axi0-read,				0x02),
 310	XGENE_PMU_EVENT_ATTR(axi0-read-partial,			0x03),
 311	XGENE_PMU_EVENT_ATTR(axi1-read,				0x04),
 312	XGENE_PMU_EVENT_ATTR(axi1-read-partial,			0x05),
 313	XGENE_PMU_EVENT_ATTR(csw-read-block,			0x06),
 314	XGENE_PMU_EVENT_ATTR(csw-read-partial,			0x07),
 315	XGENE_PMU_EVENT_ATTR(axi0-write,			0x10),
 316	XGENE_PMU_EVENT_ATTR(axi0-write-partial,		0x11),
 317	XGENE_PMU_EVENT_ATTR(axi1-write,			0x13),
 318	XGENE_PMU_EVENT_ATTR(axi1-write-partial,		0x14),
 319	XGENE_PMU_EVENT_ATTR(csw-inbound-dirty,			0x16),
 320	NULL,
 321};
 322
 323static struct attribute *mcb_pmu_events_attrs[] = {
 324	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 325	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 326	XGENE_PMU_EVENT_ATTR(csw-read,				0x02),
 327	XGENE_PMU_EVENT_ATTR(csw-write-request,			0x03),
 328	XGENE_PMU_EVENT_ATTR(mcb-csw-stall,			0x04),
 329	XGENE_PMU_EVENT_ATTR(cancel-read-gack,			0x05),
 330	NULL,
 331};
 332
 333static struct attribute *mc_pmu_events_attrs[] = {
 334	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 335	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 336	XGENE_PMU_EVENT_ATTR(act-cmd-sent,			0x02),
 337	XGENE_PMU_EVENT_ATTR(pre-cmd-sent,			0x03),
 338	XGENE_PMU_EVENT_ATTR(rd-cmd-sent,			0x04),
 339	XGENE_PMU_EVENT_ATTR(rda-cmd-sent,			0x05),
 340	XGENE_PMU_EVENT_ATTR(wr-cmd-sent,			0x06),
 341	XGENE_PMU_EVENT_ATTR(wra-cmd-sent,			0x07),
 342	XGENE_PMU_EVENT_ATTR(pde-cmd-sent,			0x08),
 343	XGENE_PMU_EVENT_ATTR(sre-cmd-sent,			0x09),
 344	XGENE_PMU_EVENT_ATTR(prea-cmd-sent,			0x0a),
 345	XGENE_PMU_EVENT_ATTR(ref-cmd-sent,			0x0b),
 346	XGENE_PMU_EVENT_ATTR(rd-rda-cmd-sent,			0x0c),
 347	XGENE_PMU_EVENT_ATTR(wr-wra-cmd-sent,			0x0d),
 348	XGENE_PMU_EVENT_ATTR(in-rd-collision,			0x0e),
 349	XGENE_PMU_EVENT_ATTR(in-wr-collision,			0x0f),
 350	XGENE_PMU_EVENT_ATTR(collision-queue-not-empty,		0x10),
 351	XGENE_PMU_EVENT_ATTR(collision-queue-full,		0x11),
 352	XGENE_PMU_EVENT_ATTR(mcu-request,			0x12),
 353	XGENE_PMU_EVENT_ATTR(mcu-rd-request,			0x13),
 354	XGENE_PMU_EVENT_ATTR(mcu-hp-rd-request,			0x14),
 355	XGENE_PMU_EVENT_ATTR(mcu-wr-request,			0x15),
 356	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-all,		0x16),
 357	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-cancel,		0x17),
 358	XGENE_PMU_EVENT_ATTR(mcu-rd-response,			0x18),
 359	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-all,	0x19),
 360	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-cancel,	0x1a),
 361	XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-all,		0x1b),
 362	XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-cancel,		0x1c),
 363	NULL,
 364};
 365
 366static const struct attribute_group l3c_pmu_events_attr_group = {
 367	.name = "events",
 368	.attrs = l3c_pmu_events_attrs,
 369};
 370
 371static const struct attribute_group iob_pmu_events_attr_group = {
 372	.name = "events",
 373	.attrs = iob_pmu_events_attrs,
 374};
 375
 376static const struct attribute_group mcb_pmu_events_attr_group = {
 377	.name = "events",
 378	.attrs = mcb_pmu_events_attrs,
 379};
 380
 381static const struct attribute_group mc_pmu_events_attr_group = {
 382	.name = "events",
 383	.attrs = mc_pmu_events_attrs,
 384};
 385
 386static struct attribute *l3c_pmu_v3_events_attrs[] = {
 387	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 388	XGENE_PMU_EVENT_ATTR(read-hit,				0x01),
 389	XGENE_PMU_EVENT_ATTR(read-miss,				0x02),
 390	XGENE_PMU_EVENT_ATTR(index-flush-eviction,		0x03),
 391	XGENE_PMU_EVENT_ATTR(write-caused-replacement,		0x04),
 392	XGENE_PMU_EVENT_ATTR(write-not-caused-replacement,	0x05),
 393	XGENE_PMU_EVENT_ATTR(clean-eviction,			0x06),
 394	XGENE_PMU_EVENT_ATTR(dirty-eviction,			0x07),
 395	XGENE_PMU_EVENT_ATTR(read,				0x08),
 396	XGENE_PMU_EVENT_ATTR(write,				0x09),
 397	XGENE_PMU_EVENT_ATTR(request,				0x0a),
 398	XGENE_PMU_EVENT_ATTR(tq-bank-conflict-issue-stall,	0x0b),
 399	XGENE_PMU_EVENT_ATTR(tq-full,				0x0c),
 400	XGENE_PMU_EVENT_ATTR(ackq-full,				0x0d),
 401	XGENE_PMU_EVENT_ATTR(wdb-full,				0x0e),
 402	XGENE_PMU_EVENT_ATTR(odb-full,				0x10),
 403	XGENE_PMU_EVENT_ATTR(wbq-full,				0x11),
 404	XGENE_PMU_EVENT_ATTR(input-req-async-fifo-stall,	0x12),
 405	XGENE_PMU_EVENT_ATTR(output-req-async-fifo-stall,	0x13),
 406	XGENE_PMU_EVENT_ATTR(output-data-async-fifo-stall,	0x14),
 407	XGENE_PMU_EVENT_ATTR(total-insertion,			0x15),
 408	XGENE_PMU_EVENT_ATTR(sip-insertions-r-set,		0x16),
 409	XGENE_PMU_EVENT_ATTR(sip-insertions-r-clear,		0x17),
 410	XGENE_PMU_EVENT_ATTR(dip-insertions-r-set,		0x18),
 411	XGENE_PMU_EVENT_ATTR(dip-insertions-r-clear,		0x19),
 412	XGENE_PMU_EVENT_ATTR(dip-insertions-force-r-set,	0x1a),
 413	XGENE_PMU_EVENT_ATTR(egression,				0x1b),
 414	XGENE_PMU_EVENT_ATTR(replacement,			0x1c),
 415	XGENE_PMU_EVENT_ATTR(old-replacement,			0x1d),
 416	XGENE_PMU_EVENT_ATTR(young-replacement,			0x1e),
 417	XGENE_PMU_EVENT_ATTR(r-set-replacement,			0x1f),
 418	XGENE_PMU_EVENT_ATTR(r-clear-replacement,		0x20),
 419	XGENE_PMU_EVENT_ATTR(old-r-replacement,			0x21),
 420	XGENE_PMU_EVENT_ATTR(old-nr-replacement,		0x22),
 421	XGENE_PMU_EVENT_ATTR(young-r-replacement,		0x23),
 422	XGENE_PMU_EVENT_ATTR(young-nr-replacement,		0x24),
 423	XGENE_PMU_EVENT_ATTR(bloomfilter-clearing,		0x25),
 424	XGENE_PMU_EVENT_ATTR(generation-flip,			0x26),
 425	XGENE_PMU_EVENT_ATTR(vcc-droop-detected,		0x27),
 426	NULL,
 427};
 428
 429static struct attribute *iob_fast_pmu_v3_events_attrs[] = {
 430	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 431	XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-all,		0x01),
 432	XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-rd,		0x02),
 433	XGENE_PMU_EVENT_ATTR(pa-req-buf-alloc-wr,		0x03),
 434	XGENE_PMU_EVENT_ATTR(pa-all-cp-req,			0x04),
 435	XGENE_PMU_EVENT_ATTR(pa-cp-blk-req,			0x05),
 436	XGENE_PMU_EVENT_ATTR(pa-cp-ptl-req,			0x06),
 437	XGENE_PMU_EVENT_ATTR(pa-cp-rd-req,			0x07),
 438	XGENE_PMU_EVENT_ATTR(pa-cp-wr-req,			0x08),
 439	XGENE_PMU_EVENT_ATTR(ba-all-req,			0x09),
 440	XGENE_PMU_EVENT_ATTR(ba-rd-req,				0x0a),
 441	XGENE_PMU_EVENT_ATTR(ba-wr-req,				0x0b),
 442	XGENE_PMU_EVENT_ATTR(pa-rd-shared-req-issued,		0x10),
 443	XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-req-issued,	0x11),
 444	XGENE_PMU_EVENT_ATTR(pa-wr-invalidate-req-issued-stashable, 0x12),
 445	XGENE_PMU_EVENT_ATTR(pa-wr-invalidate-req-issued-nonstashable, 0x13),
 446	XGENE_PMU_EVENT_ATTR(pa-wr-back-req-issued-stashable,	0x14),
 447	XGENE_PMU_EVENT_ATTR(pa-wr-back-req-issued-nonstashable, 0x15),
 448	XGENE_PMU_EVENT_ATTR(pa-ptl-wr-req,			0x16),
 449	XGENE_PMU_EVENT_ATTR(pa-ptl-rd-req,			0x17),
 450	XGENE_PMU_EVENT_ATTR(pa-wr-back-clean-data,		0x18),
 451	XGENE_PMU_EVENT_ATTR(pa-wr-back-cancelled-on-SS,	0x1b),
 452	XGENE_PMU_EVENT_ATTR(pa-barrier-occurrence,		0x1c),
 453	XGENE_PMU_EVENT_ATTR(pa-barrier-cycles,			0x1d),
 454	XGENE_PMU_EVENT_ATTR(pa-total-cp-snoops,		0x20),
 455	XGENE_PMU_EVENT_ATTR(pa-rd-shared-snoop,		0x21),
 456	XGENE_PMU_EVENT_ATTR(pa-rd-shared-snoop-hit,		0x22),
 457	XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-snoop,		0x23),
 458	XGENE_PMU_EVENT_ATTR(pa-rd-exclusive-snoop-hit,		0x24),
 459	XGENE_PMU_EVENT_ATTR(pa-rd-wr-invalid-snoop,		0x25),
 460	XGENE_PMU_EVENT_ATTR(pa-rd-wr-invalid-snoop-hit,	0x26),
 461	XGENE_PMU_EVENT_ATTR(pa-req-buffer-full,		0x28),
 462	XGENE_PMU_EVENT_ATTR(cswlf-outbound-req-fifo-full,	0x29),
 463	XGENE_PMU_EVENT_ATTR(cswlf-inbound-snoop-fifo-backpressure, 0x2a),
 464	XGENE_PMU_EVENT_ATTR(cswlf-outbound-lack-fifo-full,	0x2b),
 465	XGENE_PMU_EVENT_ATTR(cswlf-inbound-gack-fifo-backpressure, 0x2c),
 466	XGENE_PMU_EVENT_ATTR(cswlf-outbound-data-fifo-full,	0x2d),
 467	XGENE_PMU_EVENT_ATTR(cswlf-inbound-data-fifo-backpressure, 0x2e),
 468	XGENE_PMU_EVENT_ATTR(cswlf-inbound-req-backpressure,	0x2f),
 469	NULL,
 470};
 471
 472static struct attribute *iob_slow_pmu_v3_events_attrs[] = {
 473	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 474	XGENE_PMU_EVENT_ATTR(pa-axi0-rd-req,			0x01),
 475	XGENE_PMU_EVENT_ATTR(pa-axi0-wr-req,			0x02),
 476	XGENE_PMU_EVENT_ATTR(pa-axi1-rd-req,			0x03),
 477	XGENE_PMU_EVENT_ATTR(pa-axi1-wr-req,			0x04),
 478	XGENE_PMU_EVENT_ATTR(ba-all-axi-req,			0x07),
 479	XGENE_PMU_EVENT_ATTR(ba-axi-rd-req,			0x08),
 480	XGENE_PMU_EVENT_ATTR(ba-axi-wr-req,			0x09),
 481	XGENE_PMU_EVENT_ATTR(ba-free-list-empty,		0x10),
 482	NULL,
 483};
 484
 485static struct attribute *mcb_pmu_v3_events_attrs[] = {
 486	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 487	XGENE_PMU_EVENT_ATTR(req-receive,			0x01),
 488	XGENE_PMU_EVENT_ATTR(rd-req-recv,			0x02),
 489	XGENE_PMU_EVENT_ATTR(rd-req-recv-2,			0x03),
 490	XGENE_PMU_EVENT_ATTR(wr-req-recv,			0x04),
 491	XGENE_PMU_EVENT_ATTR(wr-req-recv-2,			0x05),
 492	XGENE_PMU_EVENT_ATTR(rd-req-sent-to-mcu,		0x06),
 493	XGENE_PMU_EVENT_ATTR(rd-req-sent-to-mcu-2,		0x07),
 494	XGENE_PMU_EVENT_ATTR(rd-req-sent-to-spec-mcu,		0x08),
 495	XGENE_PMU_EVENT_ATTR(rd-req-sent-to-spec-mcu-2,		0x09),
 496	XGENE_PMU_EVENT_ATTR(glbl-ack-recv-for-rd-sent-to-spec-mcu, 0x0a),
 497	XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-for-rd-sent-to-spec-mcu, 0x0b),
 498	XGENE_PMU_EVENT_ATTR(glbl-ack-nogo-recv-for-rd-sent-to-spec-mcu, 0x0c),
 499	XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-any-rd-req,	0x0d),
 500	XGENE_PMU_EVENT_ATTR(glbl-ack-go-recv-any-rd-req-2,	0x0e),
 501	XGENE_PMU_EVENT_ATTR(wr-req-sent-to-mcu,		0x0f),
 502	XGENE_PMU_EVENT_ATTR(gack-recv,				0x10),
 503	XGENE_PMU_EVENT_ATTR(rd-gack-recv,			0x11),
 504	XGENE_PMU_EVENT_ATTR(wr-gack-recv,			0x12),
 505	XGENE_PMU_EVENT_ATTR(cancel-rd-gack,			0x13),
 506	XGENE_PMU_EVENT_ATTR(cancel-wr-gack,			0x14),
 507	XGENE_PMU_EVENT_ATTR(mcb-csw-req-stall,			0x15),
 508	XGENE_PMU_EVENT_ATTR(mcu-req-intf-blocked,		0x16),
 509	XGENE_PMU_EVENT_ATTR(mcb-mcu-rd-intf-stall,		0x17),
 510	XGENE_PMU_EVENT_ATTR(csw-rd-intf-blocked,		0x18),
 511	XGENE_PMU_EVENT_ATTR(csw-local-ack-intf-blocked,	0x19),
 512	XGENE_PMU_EVENT_ATTR(mcu-req-table-full,		0x1a),
 513	XGENE_PMU_EVENT_ATTR(mcu-stat-table-full,		0x1b),
 514	XGENE_PMU_EVENT_ATTR(mcu-wr-table-full,			0x1c),
 515	XGENE_PMU_EVENT_ATTR(mcu-rdreceipt-resp,		0x1d),
 516	XGENE_PMU_EVENT_ATTR(mcu-wrcomplete-resp,		0x1e),
 517	XGENE_PMU_EVENT_ATTR(mcu-retryack-resp,			0x1f),
 518	XGENE_PMU_EVENT_ATTR(mcu-pcrdgrant-resp,		0x20),
 519	XGENE_PMU_EVENT_ATTR(mcu-req-from-lastload,		0x21),
 520	XGENE_PMU_EVENT_ATTR(mcu-req-from-bypass,		0x22),
 521	XGENE_PMU_EVENT_ATTR(volt-droop-detect,			0x23),
 522	NULL,
 523};
 524
 525static struct attribute *mc_pmu_v3_events_attrs[] = {
 526	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 527	XGENE_PMU_EVENT_ATTR(act-sent,				0x01),
 528	XGENE_PMU_EVENT_ATTR(pre-sent,				0x02),
 529	XGENE_PMU_EVENT_ATTR(rd-sent,				0x03),
 530	XGENE_PMU_EVENT_ATTR(rda-sent,				0x04),
 531	XGENE_PMU_EVENT_ATTR(wr-sent,				0x05),
 532	XGENE_PMU_EVENT_ATTR(wra-sent,				0x06),
 533	XGENE_PMU_EVENT_ATTR(pd-entry-vld,			0x07),
 534	XGENE_PMU_EVENT_ATTR(sref-entry-vld,			0x08),
 535	XGENE_PMU_EVENT_ATTR(prea-sent,				0x09),
 536	XGENE_PMU_EVENT_ATTR(ref-sent,				0x0a),
 537	XGENE_PMU_EVENT_ATTR(rd-rda-sent,			0x0b),
 538	XGENE_PMU_EVENT_ATTR(wr-wra-sent,			0x0c),
 539	XGENE_PMU_EVENT_ATTR(raw-hazard,			0x0d),
 540	XGENE_PMU_EVENT_ATTR(war-hazard,			0x0e),
 541	XGENE_PMU_EVENT_ATTR(waw-hazard,			0x0f),
 542	XGENE_PMU_EVENT_ATTR(rar-hazard,			0x10),
 543	XGENE_PMU_EVENT_ATTR(raw-war-waw-hazard,		0x11),
 544	XGENE_PMU_EVENT_ATTR(hprd-lprd-wr-req-vld,		0x12),
 545	XGENE_PMU_EVENT_ATTR(lprd-req-vld,			0x13),
 546	XGENE_PMU_EVENT_ATTR(hprd-req-vld,			0x14),
 547	XGENE_PMU_EVENT_ATTR(hprd-lprd-req-vld,			0x15),
 548	XGENE_PMU_EVENT_ATTR(wr-req-vld,			0x16),
 549	XGENE_PMU_EVENT_ATTR(partial-wr-req-vld,		0x17),
 550	XGENE_PMU_EVENT_ATTR(rd-retry,				0x18),
 551	XGENE_PMU_EVENT_ATTR(wr-retry,				0x19),
 552	XGENE_PMU_EVENT_ATTR(retry-gnt,				0x1a),
 553	XGENE_PMU_EVENT_ATTR(rank-change,			0x1b),
 554	XGENE_PMU_EVENT_ATTR(dir-change,			0x1c),
 555	XGENE_PMU_EVENT_ATTR(rank-dir-change,			0x1d),
 556	XGENE_PMU_EVENT_ATTR(rank-active,			0x1e),
 557	XGENE_PMU_EVENT_ATTR(rank-idle,				0x1f),
 558	XGENE_PMU_EVENT_ATTR(rank-pd,				0x20),
 559	XGENE_PMU_EVENT_ATTR(rank-sref,				0x21),
 560	XGENE_PMU_EVENT_ATTR(queue-fill-gt-thresh,		0x22),
 561	XGENE_PMU_EVENT_ATTR(queue-rds-gt-thresh,		0x23),
 562	XGENE_PMU_EVENT_ATTR(queue-wrs-gt-thresh,		0x24),
 563	XGENE_PMU_EVENT_ATTR(phy-updt-complt,			0x25),
 564	XGENE_PMU_EVENT_ATTR(tz-fail,				0x26),
 565	XGENE_PMU_EVENT_ATTR(dram-errc,				0x27),
 566	XGENE_PMU_EVENT_ATTR(dram-errd,				0x28),
 567	XGENE_PMU_EVENT_ATTR(rd-enq,				0x29),
 568	XGENE_PMU_EVENT_ATTR(wr-enq,				0x2a),
 569	XGENE_PMU_EVENT_ATTR(tmac-limit-reached,		0x2b),
 570	XGENE_PMU_EVENT_ATTR(tmaw-tracker-full,			0x2c),
 571	NULL,
 572};
 573
 574static const struct attribute_group l3c_pmu_v3_events_attr_group = {
 575	.name = "events",
 576	.attrs = l3c_pmu_v3_events_attrs,
 577};
 578
 579static const struct attribute_group iob_fast_pmu_v3_events_attr_group = {
 580	.name = "events",
 581	.attrs = iob_fast_pmu_v3_events_attrs,
 582};
 583
 584static const struct attribute_group iob_slow_pmu_v3_events_attr_group = {
 585	.name = "events",
 586	.attrs = iob_slow_pmu_v3_events_attrs,
 587};
 588
 589static const struct attribute_group mcb_pmu_v3_events_attr_group = {
 590	.name = "events",
 591	.attrs = mcb_pmu_v3_events_attrs,
 592};
 593
 594static const struct attribute_group mc_pmu_v3_events_attr_group = {
 595	.name = "events",
 596	.attrs = mc_pmu_v3_events_attrs,
 597};
 598
 599/*
 600 * sysfs cpumask attributes
 601 */
 602static ssize_t cpumask_show(struct device *dev,
 603			    struct device_attribute *attr, char *buf)
 604{
 605	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(dev_get_drvdata(dev));
 606
 607	return cpumap_print_to_pagebuf(true, buf, &pmu_dev->parent->cpu);
 608}
 609
 610static DEVICE_ATTR_RO(cpumask);
 611
 612static struct attribute *xgene_pmu_cpumask_attrs[] = {
 613	&dev_attr_cpumask.attr,
 614	NULL,
 615};
 616
 617static const struct attribute_group pmu_cpumask_attr_group = {
 618	.attrs = xgene_pmu_cpumask_attrs,
 619};
 620
 621/*
 622 * Per PMU device attribute groups of PMU v1 and v2
 623 */
 624static const struct attribute_group *l3c_pmu_attr_groups[] = {
 625	&l3c_pmu_format_attr_group,
 626	&pmu_cpumask_attr_group,
 627	&l3c_pmu_events_attr_group,
 628	NULL
 629};
 630
 631static const struct attribute_group *iob_pmu_attr_groups[] = {
 632	&iob_pmu_format_attr_group,
 633	&pmu_cpumask_attr_group,
 634	&iob_pmu_events_attr_group,
 635	NULL
 636};
 637
 638static const struct attribute_group *mcb_pmu_attr_groups[] = {
 639	&mcb_pmu_format_attr_group,
 640	&pmu_cpumask_attr_group,
 641	&mcb_pmu_events_attr_group,
 642	NULL
 643};
 644
 645static const struct attribute_group *mc_pmu_attr_groups[] = {
 646	&mc_pmu_format_attr_group,
 647	&pmu_cpumask_attr_group,
 648	&mc_pmu_events_attr_group,
 649	NULL
 650};
 651
 652/*
 653 * Per PMU device attribute groups of PMU v3
 654 */
 655static const struct attribute_group *l3c_pmu_v3_attr_groups[] = {
 656	&l3c_pmu_v3_format_attr_group,
 657	&pmu_cpumask_attr_group,
 658	&l3c_pmu_v3_events_attr_group,
 659	NULL
 660};
 661
 662static const struct attribute_group *iob_fast_pmu_v3_attr_groups[] = {
 663	&iob_pmu_v3_format_attr_group,
 664	&pmu_cpumask_attr_group,
 665	&iob_fast_pmu_v3_events_attr_group,
 666	NULL
 667};
 668
 669static const struct attribute_group *iob_slow_pmu_v3_attr_groups[] = {
 670	&iob_slow_pmu_v3_format_attr_group,
 671	&pmu_cpumask_attr_group,
 672	&iob_slow_pmu_v3_events_attr_group,
 673	NULL
 674};
 675
 676static const struct attribute_group *mcb_pmu_v3_attr_groups[] = {
 677	&mcb_pmu_v3_format_attr_group,
 678	&pmu_cpumask_attr_group,
 679	&mcb_pmu_v3_events_attr_group,
 680	NULL
 681};
 682
 683static const struct attribute_group *mc_pmu_v3_attr_groups[] = {
 684	&mc_pmu_v3_format_attr_group,
 685	&pmu_cpumask_attr_group,
 686	&mc_pmu_v3_events_attr_group,
 687	NULL
 688};
 689
 690static int get_next_avail_cntr(struct xgene_pmu_dev *pmu_dev)
 691{
 692	int cntr;
 693
 694	cntr = find_first_zero_bit(pmu_dev->cntr_assign_mask,
 695				pmu_dev->max_counters);
 696	if (cntr == pmu_dev->max_counters)
 697		return -ENOSPC;
 698	set_bit(cntr, pmu_dev->cntr_assign_mask);
 699
 700	return cntr;
 701}
 702
 703static void clear_avail_cntr(struct xgene_pmu_dev *pmu_dev, int cntr)
 704{
 705	clear_bit(cntr, pmu_dev->cntr_assign_mask);
 706}
 707
 708static inline void xgene_pmu_mask_int(struct xgene_pmu *xgene_pmu)
 709{
 710	writel(PCPPMU_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 711}
 712
 713static inline void xgene_pmu_v3_mask_int(struct xgene_pmu *xgene_pmu)
 714{
 715	writel(PCPPMU_V3_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 716}
 717
 718static inline void xgene_pmu_unmask_int(struct xgene_pmu *xgene_pmu)
 719{
 720	writel(PCPPMU_INTCLRMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 721}
 722
 723static inline void xgene_pmu_v3_unmask_int(struct xgene_pmu *xgene_pmu)
 724{
 725	writel(PCPPMU_V3_INTCLRMASK,
 726	       xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 727}
 728
 729static inline u64 xgene_pmu_read_counter32(struct xgene_pmu_dev *pmu_dev,
 730					   int idx)
 731{
 732	return readl(pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
 733}
 734
 735static inline u64 xgene_pmu_read_counter64(struct xgene_pmu_dev *pmu_dev,
 736					   int idx)
 737{
 738	u32 lo, hi;
 739
 740	/*
 741	 * v3 has 64-bit counter registers composed by 2 32-bit registers
 742	 * This can be a problem if the counter increases and carries
 743	 * out of bit [31] between 2 reads. The extra reads would help
 744	 * to prevent this issue.
 745	 */
 746	do {
 747		hi = xgene_pmu_read_counter32(pmu_dev, 2 * idx + 1);
 748		lo = xgene_pmu_read_counter32(pmu_dev, 2 * idx);
 749	} while (hi != xgene_pmu_read_counter32(pmu_dev, 2 * idx + 1));
 750
 751	return (((u64)hi << 32) | lo);
 752}
 753
 754static inline void
 755xgene_pmu_write_counter32(struct xgene_pmu_dev *pmu_dev, int idx, u64 val)
 756{
 757	writel(val, pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
 758}
 759
 760static inline void
 761xgene_pmu_write_counter64(struct xgene_pmu_dev *pmu_dev, int idx, u64 val)
 762{
 763	u32 cnt_lo, cnt_hi;
 764
 765	cnt_hi = upper_32_bits(val);
 766	cnt_lo = lower_32_bits(val);
 767
 768	/* v3 has 64-bit counter registers composed by 2 32-bit registers */
 769	xgene_pmu_write_counter32(pmu_dev, 2 * idx, cnt_lo);
 770	xgene_pmu_write_counter32(pmu_dev, 2 * idx + 1, cnt_hi);
 771}
 772
 773static inline void
 774xgene_pmu_write_evttype(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
 775{
 776	writel(val, pmu_dev->inf->csr + PMU_PMEVTYPER0 + (4 * idx));
 777}
 778
 779static inline void
 780xgene_pmu_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val)
 781{
 782	writel(val, pmu_dev->inf->csr + PMU_PMAMR0);
 783}
 784
 785static inline void
 786xgene_pmu_v3_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val) { }
 787
 788static inline void
 789xgene_pmu_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val)
 790{
 791	writel(val, pmu_dev->inf->csr + PMU_PMAMR1);
 792}
 793
 794static inline void
 795xgene_pmu_v3_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val) { }
 796
 797static inline void
 798xgene_pmu_enable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
 799{
 800	u32 val;
 801
 802	val = readl(pmu_dev->inf->csr + PMU_PMCNTENSET);
 803	val |= 1 << idx;
 804	writel(val, pmu_dev->inf->csr + PMU_PMCNTENSET);
 805}
 806
 807static inline void
 808xgene_pmu_disable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
 809{
 810	u32 val;
 811
 812	val = readl(pmu_dev->inf->csr + PMU_PMCNTENCLR);
 813	val |= 1 << idx;
 814	writel(val, pmu_dev->inf->csr + PMU_PMCNTENCLR);
 815}
 816
 817static inline void
 818xgene_pmu_enable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
 819{
 820	u32 val;
 821
 822	val = readl(pmu_dev->inf->csr + PMU_PMINTENSET);
 823	val |= 1 << idx;
 824	writel(val, pmu_dev->inf->csr + PMU_PMINTENSET);
 825}
 826
 827static inline void
 828xgene_pmu_disable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
 829{
 830	u32 val;
 831
 832	val = readl(pmu_dev->inf->csr + PMU_PMINTENCLR);
 833	val |= 1 << idx;
 834	writel(val, pmu_dev->inf->csr + PMU_PMINTENCLR);
 835}
 836
 837static inline void xgene_pmu_reset_counters(struct xgene_pmu_dev *pmu_dev)
 838{
 839	u32 val;
 840
 841	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 842	val |= PMU_PMCR_P;
 843	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 844}
 845
 846static inline void xgene_pmu_start_counters(struct xgene_pmu_dev *pmu_dev)
 847{
 848	u32 val;
 849
 850	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 851	val |= PMU_PMCR_E;
 852	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 853}
 854
 855static inline void xgene_pmu_stop_counters(struct xgene_pmu_dev *pmu_dev)
 856{
 857	u32 val;
 858
 859	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 860	val &= ~PMU_PMCR_E;
 861	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 862}
 863
 864static void xgene_perf_pmu_enable(struct pmu *pmu)
 865{
 866	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
 867	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 868	bool enabled = !bitmap_empty(pmu_dev->cntr_assign_mask,
 869			pmu_dev->max_counters);
 870
 871	if (!enabled)
 872		return;
 873
 874	xgene_pmu->ops->start_counters(pmu_dev);
 875}
 876
 877static void xgene_perf_pmu_disable(struct pmu *pmu)
 878{
 879	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
 880	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 881
 882	xgene_pmu->ops->stop_counters(pmu_dev);
 883}
 884
 885static int xgene_perf_event_init(struct perf_event *event)
 886{
 887	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 888	struct hw_perf_event *hw = &event->hw;
 889	struct perf_event *sibling;
 890
 891	/* Test the event attr type check for PMU enumeration */
 892	if (event->attr.type != event->pmu->type)
 893		return -ENOENT;
 894
 895	/*
 896	 * SOC PMU counters are shared across all cores.
 897	 * Therefore, it does not support per-process mode.
 898	 * Also, it does not support event sampling mode.
 899	 */
 900	if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
 901		return -EINVAL;
 902
 
 
 
 
 
 903	if (event->cpu < 0)
 904		return -EINVAL;
 905	/*
 906	 * Many perf core operations (eg. events rotation) operate on a
 907	 * single CPU context. This is obvious for CPU PMUs, where one
 908	 * expects the same sets of events being observed on all CPUs,
 909	 * but can lead to issues for off-core PMUs, where each
 910	 * event could be theoretically assigned to a different CPU. To
 911	 * mitigate this, we enforce CPU assignment to one, selected
 912	 * processor (the one described in the "cpumask" attribute).
 913	 */
 914	event->cpu = cpumask_first(&pmu_dev->parent->cpu);
 915
 916	hw->config = event->attr.config;
 917	/*
 918	 * Each bit of the config1 field represents an agent from which the
 919	 * request of the event come. The event is counted only if it's caused
 920	 * by a request of an agent has the bit cleared.
 921	 * By default, the event is counted for all agents.
 922	 */
 923	hw->config_base = event->attr.config1;
 924
 925	/*
 926	 * We must NOT create groups containing mixed PMUs, although software
 927	 * events are acceptable
 928	 */
 929	if (event->group_leader->pmu != event->pmu &&
 930			!is_software_event(event->group_leader))
 931		return -EINVAL;
 932
 933	for_each_sibling_event(sibling, event->group_leader) {
 
 934		if (sibling->pmu != event->pmu &&
 935				!is_software_event(sibling))
 936			return -EINVAL;
 937	}
 938
 939	return 0;
 940}
 941
 942static void xgene_perf_enable_event(struct perf_event *event)
 943{
 944	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 945	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 946
 947	xgene_pmu->ops->write_evttype(pmu_dev, GET_CNTR(event),
 948				      GET_EVENTID(event));
 949	xgene_pmu->ops->write_agentmsk(pmu_dev, ~((u32)GET_AGENTID(event)));
 950	if (pmu_dev->inf->type == PMU_TYPE_IOB)
 951		xgene_pmu->ops->write_agent1msk(pmu_dev,
 952						~((u32)GET_AGENT1ID(event)));
 953
 954	xgene_pmu->ops->enable_counter(pmu_dev, GET_CNTR(event));
 955	xgene_pmu->ops->enable_counter_int(pmu_dev, GET_CNTR(event));
 956}
 957
 958static void xgene_perf_disable_event(struct perf_event *event)
 959{
 960	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 961	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 962
 963	xgene_pmu->ops->disable_counter(pmu_dev, GET_CNTR(event));
 964	xgene_pmu->ops->disable_counter_int(pmu_dev, GET_CNTR(event));
 965}
 966
 967static void xgene_perf_event_set_period(struct perf_event *event)
 968{
 969	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 970	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 971	struct hw_perf_event *hw = &event->hw;
 972	/*
 973	 * For 32 bit counter, it has a period of 2^32. To account for the
 974	 * possibility of extreme interrupt latency we program for a period of
 975	 * half that. Hopefully, we can handle the interrupt before another 2^31
 976	 * events occur and the counter overtakes its previous value.
 977	 * For 64 bit counter, we don't expect it overflow.
 978	 */
 979	u64 val = 1ULL << 31;
 980
 981	local64_set(&hw->prev_count, val);
 982	xgene_pmu->ops->write_counter(pmu_dev, hw->idx, val);
 983}
 984
 985static void xgene_perf_event_update(struct perf_event *event)
 986{
 987	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 988	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 989	struct hw_perf_event *hw = &event->hw;
 990	u64 delta, prev_raw_count, new_raw_count;
 991
 992again:
 993	prev_raw_count = local64_read(&hw->prev_count);
 994	new_raw_count = xgene_pmu->ops->read_counter(pmu_dev, GET_CNTR(event));
 995
 996	if (local64_cmpxchg(&hw->prev_count, prev_raw_count,
 997			    new_raw_count) != prev_raw_count)
 998		goto again;
 999
1000	delta = (new_raw_count - prev_raw_count) & pmu_dev->max_period;
1001
1002	local64_add(delta, &event->count);
1003}
1004
1005static void xgene_perf_read(struct perf_event *event)
1006{
1007	xgene_perf_event_update(event);
1008}
1009
1010static void xgene_perf_start(struct perf_event *event, int flags)
1011{
1012	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
1013	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
1014	struct hw_perf_event *hw = &event->hw;
1015
1016	if (WARN_ON_ONCE(!(hw->state & PERF_HES_STOPPED)))
1017		return;
1018
1019	WARN_ON_ONCE(!(hw->state & PERF_HES_UPTODATE));
1020	hw->state = 0;
1021
1022	xgene_perf_event_set_period(event);
1023
1024	if (flags & PERF_EF_RELOAD) {
1025		u64 prev_raw_count =  local64_read(&hw->prev_count);
1026
1027		xgene_pmu->ops->write_counter(pmu_dev, GET_CNTR(event),
1028					      prev_raw_count);
1029	}
1030
1031	xgene_perf_enable_event(event);
1032	perf_event_update_userpage(event);
1033}
1034
1035static void xgene_perf_stop(struct perf_event *event, int flags)
1036{
1037	struct hw_perf_event *hw = &event->hw;
 
1038
1039	if (hw->state & PERF_HES_UPTODATE)
1040		return;
1041
1042	xgene_perf_disable_event(event);
1043	WARN_ON_ONCE(hw->state & PERF_HES_STOPPED);
1044	hw->state |= PERF_HES_STOPPED;
1045
1046	if (hw->state & PERF_HES_UPTODATE)
1047		return;
1048
 
1049	xgene_perf_read(event);
1050	hw->state |= PERF_HES_UPTODATE;
1051}
1052
1053static int xgene_perf_add(struct perf_event *event, int flags)
1054{
1055	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
1056	struct hw_perf_event *hw = &event->hw;
1057
1058	hw->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1059
1060	/* Allocate an event counter */
1061	hw->idx = get_next_avail_cntr(pmu_dev);
1062	if (hw->idx < 0)
1063		return -EAGAIN;
1064
1065	/* Update counter event pointer for Interrupt handler */
1066	pmu_dev->pmu_counter_event[hw->idx] = event;
1067
1068	if (flags & PERF_EF_START)
1069		xgene_perf_start(event, PERF_EF_RELOAD);
1070
1071	return 0;
1072}
1073
1074static void xgene_perf_del(struct perf_event *event, int flags)
1075{
1076	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
1077	struct hw_perf_event *hw = &event->hw;
1078
1079	xgene_perf_stop(event, PERF_EF_UPDATE);
1080
1081	/* clear the assigned counter */
1082	clear_avail_cntr(pmu_dev, GET_CNTR(event));
1083
1084	perf_event_update_userpage(event);
1085	pmu_dev->pmu_counter_event[hw->idx] = NULL;
1086}
1087
1088static int xgene_init_perf(struct xgene_pmu_dev *pmu_dev, char *name)
1089{
1090	struct xgene_pmu *xgene_pmu;
1091
1092	if (pmu_dev->parent->version == PCP_PMU_V3)
1093		pmu_dev->max_period = PMU_V3_CNT_MAX_PERIOD;
1094	else
1095		pmu_dev->max_period = PMU_CNT_MAX_PERIOD;
1096	/* First version PMU supports only single event counter */
1097	xgene_pmu = pmu_dev->parent;
1098	if (xgene_pmu->version == PCP_PMU_V1)
1099		pmu_dev->max_counters = 1;
1100	else
1101		pmu_dev->max_counters = PMU_MAX_COUNTERS;
1102
1103	/* Perf driver registration */
1104	pmu_dev->pmu = (struct pmu) {
1105		.attr_groups	= pmu_dev->attr_groups,
1106		.task_ctx_nr	= perf_invalid_context,
1107		.pmu_enable	= xgene_perf_pmu_enable,
1108		.pmu_disable	= xgene_perf_pmu_disable,
1109		.event_init	= xgene_perf_event_init,
1110		.add		= xgene_perf_add,
1111		.del		= xgene_perf_del,
1112		.start		= xgene_perf_start,
1113		.stop		= xgene_perf_stop,
1114		.read		= xgene_perf_read,
1115		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
1116	};
1117
1118	/* Hardware counter init */
1119	xgene_pmu->ops->stop_counters(pmu_dev);
1120	xgene_pmu->ops->reset_counters(pmu_dev);
1121
1122	return perf_pmu_register(&pmu_dev->pmu, name, -1);
1123}
1124
1125static int
1126xgene_pmu_dev_add(struct xgene_pmu *xgene_pmu, struct xgene_pmu_dev_ctx *ctx)
1127{
1128	struct device *dev = xgene_pmu->dev;
1129	struct xgene_pmu_dev *pmu;
 
1130
1131	pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);
1132	if (!pmu)
1133		return -ENOMEM;
1134	pmu->parent = xgene_pmu;
1135	pmu->inf = &ctx->inf;
1136	ctx->pmu_dev = pmu;
1137
1138	switch (pmu->inf->type) {
1139	case PMU_TYPE_L3C:
1140		if (!(xgene_pmu->l3c_active_mask & pmu->inf->enable_mask))
1141			return -ENODEV;
1142		if (xgene_pmu->version == PCP_PMU_V3)
1143			pmu->attr_groups = l3c_pmu_v3_attr_groups;
1144		else
1145			pmu->attr_groups = l3c_pmu_attr_groups;
1146		break;
1147	case PMU_TYPE_IOB:
1148		if (xgene_pmu->version == PCP_PMU_V3)
1149			pmu->attr_groups = iob_fast_pmu_v3_attr_groups;
1150		else
1151			pmu->attr_groups = iob_pmu_attr_groups;
1152		break;
1153	case PMU_TYPE_IOB_SLOW:
1154		if (xgene_pmu->version == PCP_PMU_V3)
1155			pmu->attr_groups = iob_slow_pmu_v3_attr_groups;
1156		break;
1157	case PMU_TYPE_MCB:
1158		if (!(xgene_pmu->mcb_active_mask & pmu->inf->enable_mask))
1159			return -ENODEV;
1160		if (xgene_pmu->version == PCP_PMU_V3)
1161			pmu->attr_groups = mcb_pmu_v3_attr_groups;
1162		else
1163			pmu->attr_groups = mcb_pmu_attr_groups;
1164		break;
1165	case PMU_TYPE_MC:
1166		if (!(xgene_pmu->mc_active_mask & pmu->inf->enable_mask))
1167			return -ENODEV;
1168		if (xgene_pmu->version == PCP_PMU_V3)
1169			pmu->attr_groups = mc_pmu_v3_attr_groups;
1170		else
1171			pmu->attr_groups = mc_pmu_attr_groups;
1172		break;
1173	default:
1174		return -EINVAL;
1175	}
1176
1177	if (xgene_init_perf(pmu, ctx->name)) {
 
1178		dev_err(dev, "%s PMU: Failed to init perf driver\n", ctx->name);
1179		return -ENODEV;
1180	}
1181
1182	dev_info(dev, "%s PMU registered\n", ctx->name);
1183
1184	return 0;
 
 
 
 
1185}
1186
1187static void _xgene_pmu_isr(int irq, struct xgene_pmu_dev *pmu_dev)
1188{
1189	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
1190	void __iomem *csr = pmu_dev->inf->csr;
1191	u32 pmovsr;
1192	int idx;
1193
1194	xgene_pmu->ops->stop_counters(pmu_dev);
1195
1196	if (xgene_pmu->version == PCP_PMU_V3)
1197		pmovsr = readl(csr + PMU_PMOVSSET) & PMU_OVERFLOW_MASK;
1198	else
1199		pmovsr = readl(csr + PMU_PMOVSR) & PMU_OVERFLOW_MASK;
1200
1201	if (!pmovsr)
1202		goto out;
1203
1204	/* Clear interrupt flag */
1205	if (xgene_pmu->version == PCP_PMU_V1)
1206		writel(0x0, csr + PMU_PMOVSR);
1207	else if (xgene_pmu->version == PCP_PMU_V2)
1208		writel(pmovsr, csr + PMU_PMOVSR);
1209	else
1210		writel(pmovsr, csr + PMU_PMOVSCLR);
1211
1212	for (idx = 0; idx < PMU_MAX_COUNTERS; idx++) {
1213		struct perf_event *event = pmu_dev->pmu_counter_event[idx];
1214		int overflowed = pmovsr & BIT(idx);
1215
1216		/* Ignore if we don't have an event. */
1217		if (!event || !overflowed)
1218			continue;
1219		xgene_perf_event_update(event);
1220		xgene_perf_event_set_period(event);
1221	}
1222
1223out:
1224	xgene_pmu->ops->start_counters(pmu_dev);
1225}
1226
1227static irqreturn_t xgene_pmu_isr(int irq, void *dev_id)
1228{
1229	u32 intr_mcu, intr_mcb, intr_l3c, intr_iob;
1230	struct xgene_pmu_dev_ctx *ctx;
1231	struct xgene_pmu *xgene_pmu = dev_id;
 
1232	u32 val;
1233
1234	raw_spin_lock(&xgene_pmu->lock);
1235
1236	/* Get Interrupt PMU source */
1237	val = readl(xgene_pmu->pcppmu_csr + PCPPMU_INTSTATUS_REG);
1238	if (xgene_pmu->version == PCP_PMU_V3) {
1239		intr_mcu = PCPPMU_V3_INT_MCU;
1240		intr_mcb = PCPPMU_V3_INT_MCB;
1241		intr_l3c = PCPPMU_V3_INT_L3C;
1242		intr_iob = PCPPMU_V3_INT_IOB;
1243	} else {
1244		intr_mcu = PCPPMU_INT_MCU;
1245		intr_mcb = PCPPMU_INT_MCB;
1246		intr_l3c = PCPPMU_INT_L3C;
1247		intr_iob = PCPPMU_INT_IOB;
1248	}
1249	if (val & intr_mcu) {
1250		list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
1251			_xgene_pmu_isr(irq, ctx->pmu_dev);
1252		}
1253	}
1254	if (val & intr_mcb) {
1255		list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
1256			_xgene_pmu_isr(irq, ctx->pmu_dev);
1257		}
1258	}
1259	if (val & intr_l3c) {
1260		list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
1261			_xgene_pmu_isr(irq, ctx->pmu_dev);
1262		}
1263	}
1264	if (val & intr_iob) {
1265		list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
1266			_xgene_pmu_isr(irq, ctx->pmu_dev);
1267		}
1268	}
1269
1270	raw_spin_unlock(&xgene_pmu->lock);
1271
1272	return IRQ_HANDLED;
1273}
1274
1275static int acpi_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1276					     struct platform_device *pdev)
1277{
1278	void __iomem *csw_csr, *mcba_csr, *mcbb_csr;
 
1279	unsigned int reg;
1280
1281	csw_csr = devm_platform_ioremap_resource(pdev, 1);
 
1282	if (IS_ERR(csw_csr)) {
1283		dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
1284		return PTR_ERR(csw_csr);
1285	}
1286
1287	mcba_csr = devm_platform_ioremap_resource(pdev, 2);
 
1288	if (IS_ERR(mcba_csr)) {
1289		dev_err(&pdev->dev, "ioremap failed for MCBA CSR resource\n");
1290		return PTR_ERR(mcba_csr);
1291	}
1292
1293	mcbb_csr = devm_platform_ioremap_resource(pdev, 3);
 
1294	if (IS_ERR(mcbb_csr)) {
1295		dev_err(&pdev->dev, "ioremap failed for MCBB CSR resource\n");
1296		return PTR_ERR(mcbb_csr);
1297	}
1298
1299	xgene_pmu->l3c_active_mask = 0x1;
1300
1301	reg = readl(csw_csr + CSW_CSWCR);
1302	if (reg & CSW_CSWCR_DUALMCB_MASK) {
1303		/* Dual MCB active */
1304		xgene_pmu->mcb_active_mask = 0x3;
1305		/* Probe all active MC(s) */
1306		reg = readl(mcbb_csr + CSW_CSWCR);
1307		xgene_pmu->mc_active_mask =
1308			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
1309	} else {
1310		/* Single MCB active */
1311		xgene_pmu->mcb_active_mask = 0x1;
1312		/* Probe all active MC(s) */
1313		reg = readl(mcba_csr + CSW_CSWCR);
1314		xgene_pmu->mc_active_mask =
1315			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
1316	}
1317
1318	return 0;
1319}
1320
1321static int acpi_pmu_v3_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1322						struct platform_device *pdev)
1323{
1324	void __iomem *csw_csr;
1325	unsigned int reg;
1326	u32 mcb0routing;
1327	u32 mcb1routing;
1328
1329	csw_csr = devm_platform_ioremap_resource(pdev, 1);
1330	if (IS_ERR(csw_csr)) {
1331		dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
1332		return PTR_ERR(csw_csr);
1333	}
1334
1335	reg = readl(csw_csr + CSW_CSWCR);
1336	mcb0routing = CSW_CSWCR_MCB0_ROUTING(reg);
1337	mcb1routing = CSW_CSWCR_MCB1_ROUTING(reg);
1338	if (reg & CSW_CSWCR_DUALMCB_MASK) {
1339		/* Dual MCB active */
1340		xgene_pmu->mcb_active_mask = 0x3;
1341		/* Probe all active L3C(s), maximum is 8 */
1342		xgene_pmu->l3c_active_mask = 0xFF;
1343		/* Probe all active MC(s), maximum is 8 */
1344		if ((mcb0routing == 0x2) && (mcb1routing == 0x2))
1345			xgene_pmu->mc_active_mask = 0xFF;
1346		else if ((mcb0routing == 0x1) && (mcb1routing == 0x1))
1347			xgene_pmu->mc_active_mask =  0x33;
1348		else
1349			xgene_pmu->mc_active_mask =  0x11;
1350	} else {
1351		/* Single MCB active */
1352		xgene_pmu->mcb_active_mask = 0x1;
1353		/* Probe all active L3C(s), maximum is 4 */
1354		xgene_pmu->l3c_active_mask = 0x0F;
1355		/* Probe all active MC(s), maximum is 4 */
1356		if (mcb0routing == 0x2)
1357			xgene_pmu->mc_active_mask = 0x0F;
1358		else if (mcb0routing == 0x1)
1359			xgene_pmu->mc_active_mask =  0x03;
1360		else
1361			xgene_pmu->mc_active_mask =  0x01;
1362	}
1363
1364	return 0;
1365}
1366
1367static int fdt_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1368					    struct platform_device *pdev)
1369{
1370	struct regmap *csw_map, *mcba_map, *mcbb_map;
1371	struct device_node *np = pdev->dev.of_node;
1372	unsigned int reg;
1373
1374	csw_map = syscon_regmap_lookup_by_phandle(np, "regmap-csw");
1375	if (IS_ERR(csw_map)) {
1376		dev_err(&pdev->dev, "unable to get syscon regmap csw\n");
1377		return PTR_ERR(csw_map);
1378	}
1379
1380	mcba_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcba");
1381	if (IS_ERR(mcba_map)) {
1382		dev_err(&pdev->dev, "unable to get syscon regmap mcba\n");
1383		return PTR_ERR(mcba_map);
1384	}
1385
1386	mcbb_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcbb");
1387	if (IS_ERR(mcbb_map)) {
1388		dev_err(&pdev->dev, "unable to get syscon regmap mcbb\n");
1389		return PTR_ERR(mcbb_map);
1390	}
1391
1392	xgene_pmu->l3c_active_mask = 0x1;
1393	if (regmap_read(csw_map, CSW_CSWCR, &reg))
1394		return -EINVAL;
1395
1396	if (reg & CSW_CSWCR_DUALMCB_MASK) {
1397		/* Dual MCB active */
1398		xgene_pmu->mcb_active_mask = 0x3;
1399		/* Probe all active MC(s) */
1400		if (regmap_read(mcbb_map, MCBADDRMR, &reg))
1401			return 0;
1402		xgene_pmu->mc_active_mask =
1403			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
1404	} else {
1405		/* Single MCB active */
1406		xgene_pmu->mcb_active_mask = 0x1;
1407		/* Probe all active MC(s) */
1408		if (regmap_read(mcba_map, MCBADDRMR, &reg))
1409			return 0;
1410		xgene_pmu->mc_active_mask =
1411			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
1412	}
1413
1414	return 0;
1415}
1416
1417static int xgene_pmu_probe_active_mcb_mcu_l3c(struct xgene_pmu *xgene_pmu,
1418					      struct platform_device *pdev)
1419{
1420	if (has_acpi_companion(&pdev->dev)) {
1421		if (xgene_pmu->version == PCP_PMU_V3)
1422			return acpi_pmu_v3_probe_active_mcb_mcu_l3c(xgene_pmu,
1423								    pdev);
1424		else
1425			return acpi_pmu_probe_active_mcb_mcu_l3c(xgene_pmu,
1426								 pdev);
1427	}
1428	return fdt_pmu_probe_active_mcb_mcu_l3c(xgene_pmu, pdev);
1429}
1430
1431static char *xgene_pmu_dev_name(struct device *dev, u32 type, int id)
1432{
1433	switch (type) {
1434	case PMU_TYPE_L3C:
1435		return devm_kasprintf(dev, GFP_KERNEL, "l3c%d", id);
1436	case PMU_TYPE_IOB:
1437		return devm_kasprintf(dev, GFP_KERNEL, "iob%d", id);
1438	case PMU_TYPE_IOB_SLOW:
1439		return devm_kasprintf(dev, GFP_KERNEL, "iob_slow%d", id);
1440	case PMU_TYPE_MCB:
1441		return devm_kasprintf(dev, GFP_KERNEL, "mcb%d", id);
1442	case PMU_TYPE_MC:
1443		return devm_kasprintf(dev, GFP_KERNEL, "mc%d", id);
1444	default:
1445		return devm_kasprintf(dev, GFP_KERNEL, "unknown");
1446	}
1447}
1448
1449#if defined(CONFIG_ACPI)
 
 
 
 
 
 
 
 
 
 
 
1450static struct
1451xgene_pmu_dev_ctx *acpi_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
1452				       struct acpi_device *adev, u32 type)
1453{
1454	struct device *dev = xgene_pmu->dev;
1455	struct list_head resource_list;
1456	struct xgene_pmu_dev_ctx *ctx;
1457	const union acpi_object *obj;
1458	struct hw_pmu_info *inf;
1459	void __iomem *dev_csr;
1460	struct resource res;
1461	struct resource_entry *rentry;
1462	int enable_bit;
1463	int rc;
1464
1465	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1466	if (!ctx)
1467		return NULL;
1468
1469	INIT_LIST_HEAD(&resource_list);
1470	rc = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
1471	if (rc <= 0) {
1472		dev_err(dev, "PMU type %d: No resources found\n", type);
1473		return NULL;
1474	}
1475
1476	list_for_each_entry(rentry, &resource_list, node) {
1477		if (resource_type(rentry->res) == IORESOURCE_MEM) {
1478			res = *rentry->res;
1479			rentry = NULL;
1480			break;
1481		}
1482	}
1483	acpi_dev_free_resource_list(&resource_list);
1484
1485	if (rentry) {
1486		dev_err(dev, "PMU type %d: No memory resource found\n", type);
1487		return NULL;
1488	}
1489
1490	dev_csr = devm_ioremap_resource(dev, &res);
1491	if (IS_ERR(dev_csr)) {
1492		dev_err(dev, "PMU type %d: Fail to map resource\n", type);
1493		return NULL;
1494	}
1495
1496	/* A PMU device node without enable-bit-index is always enabled */
1497	rc = acpi_dev_get_property(adev, "enable-bit-index",
1498				   ACPI_TYPE_INTEGER, &obj);
1499	if (rc < 0)
1500		enable_bit = 0;
1501	else
1502		enable_bit = (int) obj->integer.value;
1503
1504	ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1505	if (!ctx->name) {
1506		dev_err(dev, "PMU type %d: Fail to get device name\n", type);
1507		return NULL;
1508	}
1509	inf = &ctx->inf;
1510	inf->type = type;
1511	inf->csr = dev_csr;
1512	inf->enable_mask = 1 << enable_bit;
1513
1514	return ctx;
1515}
1516
1517static const struct acpi_device_id xgene_pmu_acpi_type_match[] = {
1518	{"APMC0D5D", PMU_TYPE_L3C},
1519	{"APMC0D5E", PMU_TYPE_IOB},
1520	{"APMC0D5F", PMU_TYPE_MCB},
1521	{"APMC0D60", PMU_TYPE_MC},
1522	{"APMC0D84", PMU_TYPE_L3C},
1523	{"APMC0D85", PMU_TYPE_IOB},
1524	{"APMC0D86", PMU_TYPE_IOB_SLOW},
1525	{"APMC0D87", PMU_TYPE_MCB},
1526	{"APMC0D88", PMU_TYPE_MC},
1527	{},
1528};
1529
1530static const struct acpi_device_id *xgene_pmu_acpi_match_type(
1531					const struct acpi_device_id *ids,
1532					struct acpi_device *adev)
1533{
1534	const struct acpi_device_id *match_id = NULL;
1535	const struct acpi_device_id *id;
1536
1537	for (id = ids; id->id[0] || id->cls; id++) {
1538		if (!acpi_match_device_ids(adev, id))
1539			match_id = id;
1540		else if (match_id)
1541			break;
1542	}
1543
1544	return match_id;
1545}
1546
1547static acpi_status acpi_pmu_dev_add(acpi_handle handle, u32 level,
1548				    void *data, void **return_value)
1549{
1550	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
1551	const struct acpi_device_id *acpi_id;
1552	struct xgene_pmu *xgene_pmu = data;
1553	struct xgene_pmu_dev_ctx *ctx;
 
1554
1555	if (!adev || acpi_bus_get_status(adev) || !adev->status.present)
1556		return AE_OK;
1557
1558	acpi_id = xgene_pmu_acpi_match_type(xgene_pmu_acpi_type_match, adev);
1559	if (!acpi_id)
1560		return AE_OK;
1561
1562	ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, (u32)acpi_id->driver_data);
 
 
 
 
 
 
 
 
 
 
1563	if (!ctx)
1564		return AE_OK;
1565
1566	if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1567		/* Can't add the PMU device, skip it */
1568		devm_kfree(xgene_pmu->dev, ctx);
1569		return AE_OK;
1570	}
1571
1572	switch (ctx->inf.type) {
1573	case PMU_TYPE_L3C:
1574		list_add(&ctx->next, &xgene_pmu->l3cpmus);
1575		break;
1576	case PMU_TYPE_IOB:
1577		list_add(&ctx->next, &xgene_pmu->iobpmus);
1578		break;
1579	case PMU_TYPE_IOB_SLOW:
1580		list_add(&ctx->next, &xgene_pmu->iobpmus);
1581		break;
1582	case PMU_TYPE_MCB:
1583		list_add(&ctx->next, &xgene_pmu->mcbpmus);
1584		break;
1585	case PMU_TYPE_MC:
1586		list_add(&ctx->next, &xgene_pmu->mcpmus);
1587		break;
1588	}
1589	return AE_OK;
1590}
1591
1592static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1593				  struct platform_device *pdev)
1594{
1595	struct device *dev = xgene_pmu->dev;
1596	acpi_handle handle;
1597	acpi_status status;
1598
1599	handle = ACPI_HANDLE(dev);
1600	if (!handle)
1601		return -EINVAL;
1602
1603	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1604				     acpi_pmu_dev_add, NULL, xgene_pmu, NULL);
1605	if (ACPI_FAILURE(status)) {
1606		dev_err(dev, "failed to probe PMU devices\n");
1607		return -ENODEV;
1608	}
1609
1610	return 0;
1611}
1612#else
1613static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1614				  struct platform_device *pdev)
1615{
1616	return 0;
1617}
1618#endif
1619
1620static struct
1621xgene_pmu_dev_ctx *fdt_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
1622				      struct device_node *np, u32 type)
1623{
1624	struct device *dev = xgene_pmu->dev;
1625	struct xgene_pmu_dev_ctx *ctx;
1626	struct hw_pmu_info *inf;
1627	void __iomem *dev_csr;
1628	struct resource res;
1629	int enable_bit;
 
1630
1631	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1632	if (!ctx)
1633		return NULL;
1634
1635	if (of_address_to_resource(np, 0, &res) < 0) {
1636		dev_err(dev, "PMU type %d: No resource address found\n", type);
1637		return NULL;
1638	}
1639
1640	dev_csr = devm_ioremap_resource(dev, &res);
1641	if (IS_ERR(dev_csr)) {
1642		dev_err(dev, "PMU type %d: Fail to map resource\n", type);
1643		return NULL;
1644	}
1645
1646	/* A PMU device node without enable-bit-index is always enabled */
1647	if (of_property_read_u32(np, "enable-bit-index", &enable_bit))
1648		enable_bit = 0;
1649
1650	ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1651	if (!ctx->name) {
1652		dev_err(dev, "PMU type %d: Fail to get device name\n", type);
1653		return NULL;
1654	}
1655
1656	inf = &ctx->inf;
1657	inf->type = type;
1658	inf->csr = dev_csr;
1659	inf->enable_mask = 1 << enable_bit;
1660
1661	return ctx;
 
 
 
1662}
1663
1664static int fdt_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1665				 struct platform_device *pdev)
1666{
1667	struct xgene_pmu_dev_ctx *ctx;
1668	struct device_node *np;
1669
1670	for_each_child_of_node(pdev->dev.of_node, np) {
1671		if (!of_device_is_available(np))
1672			continue;
1673
1674		if (of_device_is_compatible(np, "apm,xgene-pmu-l3c"))
1675			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_L3C);
1676		else if (of_device_is_compatible(np, "apm,xgene-pmu-iob"))
1677			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_IOB);
1678		else if (of_device_is_compatible(np, "apm,xgene-pmu-mcb"))
1679			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MCB);
1680		else if (of_device_is_compatible(np, "apm,xgene-pmu-mc"))
1681			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MC);
1682		else
1683			ctx = NULL;
1684
1685		if (!ctx)
1686			continue;
1687
1688		if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1689			/* Can't add the PMU device, skip it */
1690			devm_kfree(xgene_pmu->dev, ctx);
1691			continue;
1692		}
1693
1694		switch (ctx->inf.type) {
1695		case PMU_TYPE_L3C:
1696			list_add(&ctx->next, &xgene_pmu->l3cpmus);
1697			break;
1698		case PMU_TYPE_IOB:
1699			list_add(&ctx->next, &xgene_pmu->iobpmus);
1700			break;
1701		case PMU_TYPE_IOB_SLOW:
1702			list_add(&ctx->next, &xgene_pmu->iobpmus);
1703			break;
1704		case PMU_TYPE_MCB:
1705			list_add(&ctx->next, &xgene_pmu->mcbpmus);
1706			break;
1707		case PMU_TYPE_MC:
1708			list_add(&ctx->next, &xgene_pmu->mcpmus);
1709			break;
1710		}
1711	}
1712
1713	return 0;
1714}
1715
1716static int xgene_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1717				   struct platform_device *pdev)
1718{
1719	if (has_acpi_companion(&pdev->dev))
1720		return acpi_pmu_probe_pmu_dev(xgene_pmu, pdev);
1721	return fdt_pmu_probe_pmu_dev(xgene_pmu, pdev);
1722}
1723
1724static const struct xgene_pmu_data xgene_pmu_data = {
1725	.id   = PCP_PMU_V1,
1726};
1727
1728static const struct xgene_pmu_data xgene_pmu_v2_data = {
1729	.id   = PCP_PMU_V2,
1730};
1731
1732#ifdef CONFIG_ACPI
1733static const struct xgene_pmu_data xgene_pmu_v3_data = {
1734	.id   = PCP_PMU_V3,
1735};
1736#endif
1737
1738static const struct xgene_pmu_ops xgene_pmu_ops = {
1739	.mask_int = xgene_pmu_mask_int,
1740	.unmask_int = xgene_pmu_unmask_int,
1741	.read_counter = xgene_pmu_read_counter32,
1742	.write_counter = xgene_pmu_write_counter32,
1743	.write_evttype = xgene_pmu_write_evttype,
1744	.write_agentmsk = xgene_pmu_write_agentmsk,
1745	.write_agent1msk = xgene_pmu_write_agent1msk,
1746	.enable_counter = xgene_pmu_enable_counter,
1747	.disable_counter = xgene_pmu_disable_counter,
1748	.enable_counter_int = xgene_pmu_enable_counter_int,
1749	.disable_counter_int = xgene_pmu_disable_counter_int,
1750	.reset_counters = xgene_pmu_reset_counters,
1751	.start_counters = xgene_pmu_start_counters,
1752	.stop_counters = xgene_pmu_stop_counters,
1753};
1754
1755static const struct xgene_pmu_ops xgene_pmu_v3_ops = {
1756	.mask_int = xgene_pmu_v3_mask_int,
1757	.unmask_int = xgene_pmu_v3_unmask_int,
1758	.read_counter = xgene_pmu_read_counter64,
1759	.write_counter = xgene_pmu_write_counter64,
1760	.write_evttype = xgene_pmu_write_evttype,
1761	.write_agentmsk = xgene_pmu_v3_write_agentmsk,
1762	.write_agent1msk = xgene_pmu_v3_write_agent1msk,
1763	.enable_counter = xgene_pmu_enable_counter,
1764	.disable_counter = xgene_pmu_disable_counter,
1765	.enable_counter_int = xgene_pmu_enable_counter_int,
1766	.disable_counter_int = xgene_pmu_disable_counter_int,
1767	.reset_counters = xgene_pmu_reset_counters,
1768	.start_counters = xgene_pmu_start_counters,
1769	.stop_counters = xgene_pmu_stop_counters,
1770};
1771
1772static const struct of_device_id xgene_pmu_of_match[] = {
1773	{ .compatible	= "apm,xgene-pmu",	.data = &xgene_pmu_data },
1774	{ .compatible	= "apm,xgene-pmu-v2",	.data = &xgene_pmu_v2_data },
1775	{},
1776};
1777MODULE_DEVICE_TABLE(of, xgene_pmu_of_match);
1778#ifdef CONFIG_ACPI
1779static const struct acpi_device_id xgene_pmu_acpi_match[] = {
1780	{"APMC0D5B", (kernel_ulong_t)&xgene_pmu_data},
1781	{"APMC0D5C", (kernel_ulong_t)&xgene_pmu_v2_data},
1782	{"APMC0D83", (kernel_ulong_t)&xgene_pmu_v3_data},
1783	{},
1784};
1785MODULE_DEVICE_TABLE(acpi, xgene_pmu_acpi_match);
1786#endif
1787
1788static int xgene_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
1789{
1790	struct xgene_pmu *xgene_pmu = hlist_entry_safe(node, struct xgene_pmu,
1791						       node);
1792
1793	if (cpumask_empty(&xgene_pmu->cpu))
1794		cpumask_set_cpu(cpu, &xgene_pmu->cpu);
1795
1796	/* Overflow interrupt also should use the same CPU */
1797	WARN_ON(irq_set_affinity(xgene_pmu->irq, &xgene_pmu->cpu));
1798
1799	return 0;
1800}
1801
1802static int xgene_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
1803{
1804	struct xgene_pmu *xgene_pmu = hlist_entry_safe(node, struct xgene_pmu,
1805						       node);
1806	struct xgene_pmu_dev_ctx *ctx;
1807	unsigned int target;
1808
1809	if (!cpumask_test_and_clear_cpu(cpu, &xgene_pmu->cpu))
1810		return 0;
1811	target = cpumask_any_but(cpu_online_mask, cpu);
1812	if (target >= nr_cpu_ids)
1813		return 0;
1814
1815	list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
1816		perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1817	}
1818	list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
1819		perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1820	}
1821	list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
1822		perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1823	}
1824	list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
1825		perf_pmu_migrate_context(&ctx->pmu_dev->pmu, cpu, target);
1826	}
1827
1828	cpumask_set_cpu(target, &xgene_pmu->cpu);
1829	/* Overflow interrupt also should use the same CPU */
1830	WARN_ON(irq_set_affinity(xgene_pmu->irq, &xgene_pmu->cpu));
1831
1832	return 0;
1833}
1834
1835static int xgene_pmu_probe(struct platform_device *pdev)
1836{
1837	const struct xgene_pmu_data *dev_data;
 
1838	struct xgene_pmu *xgene_pmu;
 
1839	int irq, rc;
1840	int version;
1841
1842	/* Install a hook to update the reader CPU in case it goes offline */
1843	rc = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1844				      "CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE",
1845				      xgene_pmu_online_cpu,
1846				      xgene_pmu_offline_cpu);
1847	if (rc)
1848		return rc;
1849
1850	xgene_pmu = devm_kzalloc(&pdev->dev, sizeof(*xgene_pmu), GFP_KERNEL);
1851	if (!xgene_pmu)
1852		return -ENOMEM;
1853	xgene_pmu->dev = &pdev->dev;
1854	platform_set_drvdata(pdev, xgene_pmu);
1855
1856	dev_data = device_get_match_data(&pdev->dev);
1857	if (!dev_data)
1858		return -ENODEV;
1859	version = dev_data->id;
 
 
1860
1861	if (version == PCP_PMU_V3)
1862		xgene_pmu->ops = &xgene_pmu_v3_ops;
1863	else
1864		xgene_pmu->ops = &xgene_pmu_ops;
 
 
 
 
 
 
 
1865
1866	INIT_LIST_HEAD(&xgene_pmu->l3cpmus);
1867	INIT_LIST_HEAD(&xgene_pmu->iobpmus);
1868	INIT_LIST_HEAD(&xgene_pmu->mcbpmus);
1869	INIT_LIST_HEAD(&xgene_pmu->mcpmus);
1870
1871	xgene_pmu->version = version;
1872	dev_info(&pdev->dev, "X-Gene PMU version %d\n", xgene_pmu->version);
1873
1874	xgene_pmu->pcppmu_csr = devm_platform_ioremap_resource(pdev, 0);
 
1875	if (IS_ERR(xgene_pmu->pcppmu_csr)) {
1876		dev_err(&pdev->dev, "ioremap failed for PCP PMU resource\n");
1877		return PTR_ERR(xgene_pmu->pcppmu_csr);
 
1878	}
1879
1880	irq = platform_get_irq(pdev, 0);
1881	if (irq < 0)
1882		return -EINVAL;
1883
 
 
1884	rc = devm_request_irq(&pdev->dev, irq, xgene_pmu_isr,
1885				IRQF_NOBALANCING | IRQF_NO_THREAD,
1886				dev_name(&pdev->dev), xgene_pmu);
1887	if (rc) {
1888		dev_err(&pdev->dev, "Could not request IRQ %d\n", irq);
1889		return rc;
1890	}
1891
1892	xgene_pmu->irq = irq;
1893
1894	raw_spin_lock_init(&xgene_pmu->lock);
1895
1896	/* Check for active MCBs and MCUs */
1897	rc = xgene_pmu_probe_active_mcb_mcu_l3c(xgene_pmu, pdev);
1898	if (rc) {
1899		dev_warn(&pdev->dev, "Unknown MCB/MCU active status\n");
1900		xgene_pmu->mcb_active_mask = 0x1;
1901		xgene_pmu->mc_active_mask = 0x1;
1902	}
1903
1904	/* Add this instance to the list used by the hotplug callback */
1905	rc = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1906				      &xgene_pmu->node);
 
 
1907	if (rc) {
1908		dev_err(&pdev->dev, "Error %d registering hotplug", rc);
1909		return rc;
1910	}
1911
1912	/* Walk through the tree for all PMU perf devices */
1913	rc = xgene_pmu_probe_pmu_dev(xgene_pmu, pdev);
1914	if (rc) {
1915		dev_err(&pdev->dev, "No PMU perf devices found!\n");
1916		goto out_unregister;
1917	}
1918
1919	/* Enable interrupt */
1920	xgene_pmu->ops->unmask_int(xgene_pmu);
1921
1922	return 0;
1923
1924out_unregister:
1925	cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1926				    &xgene_pmu->node);
 
 
1927	return rc;
1928}
1929
1930static void
1931xgene_pmu_dev_cleanup(struct xgene_pmu *xgene_pmu, struct list_head *pmus)
1932{
1933	struct xgene_pmu_dev_ctx *ctx;
 
 
1934
1935	list_for_each_entry(ctx, pmus, next) {
1936		perf_pmu_unregister(&ctx->pmu_dev->pmu);
 
 
 
 
1937	}
1938}
1939
1940static int xgene_pmu_remove(struct platform_device *pdev)
1941{
1942	struct xgene_pmu *xgene_pmu = dev_get_drvdata(&pdev->dev);
1943
1944	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->l3cpmus);
1945	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->iobpmus);
1946	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcbpmus);
1947	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcpmus);
1948	cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_APM_XGENE_ONLINE,
1949				    &xgene_pmu->node);
 
 
1950
1951	return 0;
1952}
1953
1954static struct platform_driver xgene_pmu_driver = {
1955	.probe = xgene_pmu_probe,
1956	.remove = xgene_pmu_remove,
1957	.driver = {
1958		.name		= "xgene-pmu",
1959		.of_match_table = xgene_pmu_of_match,
1960		.acpi_match_table = ACPI_PTR(xgene_pmu_acpi_match),
1961		.suppress_bind_attrs = true,
1962	},
1963};
1964
1965builtin_platform_driver(xgene_pmu_driver);
v4.10.11
 
   1/*
   2 * APM X-Gene SoC PMU (Performance Monitor Unit)
   3 *
   4 * Copyright (c) 2016, Applied Micro Circuits Corporation
   5 * Author: Hoan Tran <hotran@apm.com>
   6 *         Tai Nguyen <ttnguyen@apm.com>
   7 *
   8 * This program is free software; you can redistribute  it and/or modify it
   9 * under  the terms of  the GNU General  Public License as published by the
  10 * Free Software Foundation;  either version 2 of the  License, or (at your
  11 * option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20 */
  21
  22#include <linux/acpi.h>
  23#include <linux/clk.h>
 
  24#include <linux/cpumask.h>
  25#include <linux/interrupt.h>
  26#include <linux/io.h>
  27#include <linux/mfd/syscon.h>
 
  28#include <linux/of_address.h>
  29#include <linux/of_fdt.h>
  30#include <linux/of_irq.h>
  31#include <linux/of_platform.h>
  32#include <linux/perf_event.h>
  33#include <linux/platform_device.h>
 
  34#include <linux/regmap.h>
  35#include <linux/slab.h>
  36
  37#define CSW_CSWCR                       0x0000
  38#define  CSW_CSWCR_DUALMCB_MASK         BIT(0)
 
 
  39#define MCBADDRMR                       0x0000
  40#define  MCBADDRMR_DUALMCU_MODE_MASK    BIT(2)
  41
  42#define PCPPMU_INTSTATUS_REG	0x000
  43#define PCPPMU_INTMASK_REG	0x004
  44#define  PCPPMU_INTMASK		0x0000000F
  45#define  PCPPMU_INTENMASK	0xFFFFFFFF
  46#define  PCPPMU_INTCLRMASK	0xFFFFFFF0
  47#define  PCPPMU_INT_MCU		BIT(0)
  48#define  PCPPMU_INT_MCB		BIT(1)
  49#define  PCPPMU_INT_L3C		BIT(2)
  50#define  PCPPMU_INT_IOB		BIT(3)
  51
 
 
 
 
 
 
 
 
  52#define PMU_MAX_COUNTERS	4
  53#define PMU_CNT_MAX_PERIOD	0x100000000ULL
 
  54#define PMU_OVERFLOW_MASK	0xF
  55#define PMU_PMCR_E		BIT(0)
  56#define PMU_PMCR_P		BIT(1)
  57
  58#define PMU_PMEVCNTR0		0x000
  59#define PMU_PMEVCNTR1		0x004
  60#define PMU_PMEVCNTR2		0x008
  61#define PMU_PMEVCNTR3		0x00C
  62#define PMU_PMEVTYPER0		0x400
  63#define PMU_PMEVTYPER1		0x404
  64#define PMU_PMEVTYPER2		0x408
  65#define PMU_PMEVTYPER3		0x40C
  66#define PMU_PMAMR0		0xA00
  67#define PMU_PMAMR1		0xA04
  68#define PMU_PMCNTENSET		0xC00
  69#define PMU_PMCNTENCLR		0xC20
  70#define PMU_PMINTENSET		0xC40
  71#define PMU_PMINTENCLR		0xC60
  72#define PMU_PMOVSR		0xC80
  73#define PMU_PMCR		0xE04
  74
 
 
 
 
  75#define to_pmu_dev(p)     container_of(p, struct xgene_pmu_dev, pmu)
  76#define GET_CNTR(ev)      (ev->hw.idx)
  77#define GET_EVENTID(ev)   (ev->hw.config & 0xFFULL)
  78#define GET_AGENTID(ev)   (ev->hw.config_base & 0xFFFFFFFFUL)
  79#define GET_AGENT1ID(ev)  ((ev->hw.config_base >> 32) & 0xFFFFFFFFUL)
  80
  81struct hw_pmu_info {
  82	u32 type;
  83	u32 enable_mask;
  84	void __iomem *csr;
  85};
  86
  87struct xgene_pmu_dev {
  88	struct hw_pmu_info *inf;
  89	struct xgene_pmu *parent;
  90	struct pmu pmu;
  91	u8 max_counters;
  92	DECLARE_BITMAP(cntr_assign_mask, PMU_MAX_COUNTERS);
  93	u64 max_period;
  94	const struct attribute_group **attr_groups;
  95	struct perf_event *pmu_counter_event[PMU_MAX_COUNTERS];
  96};
  97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  98struct xgene_pmu {
  99	struct device *dev;
 
 100	int version;
 101	void __iomem *pcppmu_csr;
 102	u32 mcb_active_mask;
 103	u32 mc_active_mask;
 
 104	cpumask_t cpu;
 
 105	raw_spinlock_t lock;
 
 106	struct list_head l3cpmus;
 107	struct list_head iobpmus;
 108	struct list_head mcbpmus;
 109	struct list_head mcpmus;
 110};
 111
 112struct xgene_pmu_dev_ctx {
 113	char *name;
 114	struct list_head next;
 115	struct xgene_pmu_dev *pmu_dev;
 116	struct hw_pmu_info inf;
 117};
 118
 119struct xgene_pmu_data {
 120	int id;
 121	u32 data;
 122};
 123
 124enum xgene_pmu_version {
 125	PCP_PMU_V1 = 1,
 126	PCP_PMU_V2,
 
 127};
 128
 129enum xgene_pmu_dev_type {
 130	PMU_TYPE_L3C = 0,
 131	PMU_TYPE_IOB,
 
 132	PMU_TYPE_MCB,
 133	PMU_TYPE_MC,
 134};
 135
 136/*
 137 * sysfs format attributes
 138 */
 139static ssize_t xgene_pmu_format_show(struct device *dev,
 140				     struct device_attribute *attr, char *buf)
 141{
 142	struct dev_ext_attribute *eattr;
 143
 144	eattr = container_of(attr, struct dev_ext_attribute, attr);
 145	return sprintf(buf, "%s\n", (char *) eattr->var);
 146}
 147
 148#define XGENE_PMU_FORMAT_ATTR(_name, _config)		\
 149	(&((struct dev_ext_attribute[]) {		\
 150		{ .attr = __ATTR(_name, S_IRUGO, xgene_pmu_format_show, NULL), \
 151		  .var = (void *) _config, }		\
 152	})[0].attr.attr)
 153
 154static struct attribute *l3c_pmu_format_attrs[] = {
 155	XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-7"),
 156	XGENE_PMU_FORMAT_ATTR(l3c_agentid, "config1:0-9"),
 157	NULL,
 158};
 159
 160static struct attribute *iob_pmu_format_attrs[] = {
 161	XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-7"),
 162	XGENE_PMU_FORMAT_ATTR(iob_agentid, "config1:0-63"),
 163	NULL,
 164};
 165
 166static struct attribute *mcb_pmu_format_attrs[] = {
 167	XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-5"),
 168	XGENE_PMU_FORMAT_ATTR(mcb_agentid, "config1:0-9"),
 169	NULL,
 170};
 171
 172static struct attribute *mc_pmu_format_attrs[] = {
 173	XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-28"),
 174	NULL,
 175};
 176
 177static const struct attribute_group l3c_pmu_format_attr_group = {
 178	.name = "format",
 179	.attrs = l3c_pmu_format_attrs,
 180};
 181
 182static const struct attribute_group iob_pmu_format_attr_group = {
 183	.name = "format",
 184	.attrs = iob_pmu_format_attrs,
 185};
 186
 187static const struct attribute_group mcb_pmu_format_attr_group = {
 188	.name = "format",
 189	.attrs = mcb_pmu_format_attrs,
 190};
 191
 192static const struct attribute_group mc_pmu_format_attr_group = {
 193	.name = "format",
 194	.attrs = mc_pmu_format_attrs,
 195};
 196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 197/*
 198 * sysfs event attributes
 199 */
 200static ssize_t xgene_pmu_event_show(struct device *dev,
 201				    struct device_attribute *attr, char *buf)
 202{
 203	struct dev_ext_attribute *eattr;
 
 204
 205	eattr = container_of(attr, struct dev_ext_attribute, attr);
 206	return sprintf(buf, "config=0x%lx\n", (unsigned long) eattr->var);
 207}
 208
 209#define XGENE_PMU_EVENT_ATTR(_name, _config)		\
 210	(&((struct dev_ext_attribute[]) {		\
 211		{ .attr = __ATTR(_name, S_IRUGO, xgene_pmu_event_show, NULL), \
 212		  .var = (void *) _config, }		\
 213	 })[0].attr.attr)
 214
 215static struct attribute *l3c_pmu_events_attrs[] = {
 216	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 217	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 218	XGENE_PMU_EVENT_ATTR(read-hit,				0x02),
 219	XGENE_PMU_EVENT_ATTR(read-miss,				0x03),
 220	XGENE_PMU_EVENT_ATTR(write-need-replacement,		0x06),
 221	XGENE_PMU_EVENT_ATTR(write-not-need-replacement,	0x07),
 222	XGENE_PMU_EVENT_ATTR(tq-full,				0x08),
 223	XGENE_PMU_EVENT_ATTR(ackq-full,				0x09),
 224	XGENE_PMU_EVENT_ATTR(wdb-full,				0x0a),
 225	XGENE_PMU_EVENT_ATTR(bank-fifo-full,			0x0b),
 226	XGENE_PMU_EVENT_ATTR(odb-full,				0x0c),
 227	XGENE_PMU_EVENT_ATTR(wbq-full,				0x0d),
 228	XGENE_PMU_EVENT_ATTR(bank-conflict-fifo-issue,		0x0e),
 229	XGENE_PMU_EVENT_ATTR(bank-fifo-issue,			0x0f),
 230	NULL,
 231};
 232
 233static struct attribute *iob_pmu_events_attrs[] = {
 234	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 235	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 236	XGENE_PMU_EVENT_ATTR(axi0-read,				0x02),
 237	XGENE_PMU_EVENT_ATTR(axi0-read-partial,			0x03),
 238	XGENE_PMU_EVENT_ATTR(axi1-read,				0x04),
 239	XGENE_PMU_EVENT_ATTR(axi1-read-partial,			0x05),
 240	XGENE_PMU_EVENT_ATTR(csw-read-block,			0x06),
 241	XGENE_PMU_EVENT_ATTR(csw-read-partial,			0x07),
 242	XGENE_PMU_EVENT_ATTR(axi0-write,			0x10),
 243	XGENE_PMU_EVENT_ATTR(axi0-write-partial,		0x11),
 244	XGENE_PMU_EVENT_ATTR(axi1-write,			0x13),
 245	XGENE_PMU_EVENT_ATTR(axi1-write-partial,		0x14),
 246	XGENE_PMU_EVENT_ATTR(csw-inbound-dirty,			0x16),
 247	NULL,
 248};
 249
 250static struct attribute *mcb_pmu_events_attrs[] = {
 251	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 252	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 253	XGENE_PMU_EVENT_ATTR(csw-read,				0x02),
 254	XGENE_PMU_EVENT_ATTR(csw-write-request,			0x03),
 255	XGENE_PMU_EVENT_ATTR(mcb-csw-stall,			0x04),
 256	XGENE_PMU_EVENT_ATTR(cancel-read-gack,			0x05),
 257	NULL,
 258};
 259
 260static struct attribute *mc_pmu_events_attrs[] = {
 261	XGENE_PMU_EVENT_ATTR(cycle-count,			0x00),
 262	XGENE_PMU_EVENT_ATTR(cycle-count-div-64,		0x01),
 263	XGENE_PMU_EVENT_ATTR(act-cmd-sent,			0x02),
 264	XGENE_PMU_EVENT_ATTR(pre-cmd-sent,			0x03),
 265	XGENE_PMU_EVENT_ATTR(rd-cmd-sent,			0x04),
 266	XGENE_PMU_EVENT_ATTR(rda-cmd-sent,			0x05),
 267	XGENE_PMU_EVENT_ATTR(wr-cmd-sent,			0x06),
 268	XGENE_PMU_EVENT_ATTR(wra-cmd-sent,			0x07),
 269	XGENE_PMU_EVENT_ATTR(pde-cmd-sent,			0x08),
 270	XGENE_PMU_EVENT_ATTR(sre-cmd-sent,			0x09),
 271	XGENE_PMU_EVENT_ATTR(prea-cmd-sent,			0x0a),
 272	XGENE_PMU_EVENT_ATTR(ref-cmd-sent,			0x0b),
 273	XGENE_PMU_EVENT_ATTR(rd-rda-cmd-sent,			0x0c),
 274	XGENE_PMU_EVENT_ATTR(wr-wra-cmd-sent,			0x0d),
 275	XGENE_PMU_EVENT_ATTR(in-rd-collision,			0x0e),
 276	XGENE_PMU_EVENT_ATTR(in-wr-collision,			0x0f),
 277	XGENE_PMU_EVENT_ATTR(collision-queue-not-empty,		0x10),
 278	XGENE_PMU_EVENT_ATTR(collision-queue-full,		0x11),
 279	XGENE_PMU_EVENT_ATTR(mcu-request,			0x12),
 280	XGENE_PMU_EVENT_ATTR(mcu-rd-request,			0x13),
 281	XGENE_PMU_EVENT_ATTR(mcu-hp-rd-request,			0x14),
 282	XGENE_PMU_EVENT_ATTR(mcu-wr-request,			0x15),
 283	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-all,		0x16),
 284	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-cancel,		0x17),
 285	XGENE_PMU_EVENT_ATTR(mcu-rd-response,			0x18),
 286	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-all,	0x19),
 287	XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-cancel,	0x1a),
 288	XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-all,		0x1b),
 289	XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-cancel,		0x1c),
 290	NULL,
 291};
 292
 293static const struct attribute_group l3c_pmu_events_attr_group = {
 294	.name = "events",
 295	.attrs = l3c_pmu_events_attrs,
 296};
 297
 298static const struct attribute_group iob_pmu_events_attr_group = {
 299	.name = "events",
 300	.attrs = iob_pmu_events_attrs,
 301};
 302
 303static const struct attribute_group mcb_pmu_events_attr_group = {
 304	.name = "events",
 305	.attrs = mcb_pmu_events_attrs,
 306};
 307
 308static const struct attribute_group mc_pmu_events_attr_group = {
 309	.name = "events",
 310	.attrs = mc_pmu_events_attrs,
 311};
 312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 313/*
 314 * sysfs cpumask attributes
 315 */
 316static ssize_t xgene_pmu_cpumask_show(struct device *dev,
 317				      struct device_attribute *attr, char *buf)
 318{
 319	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(dev_get_drvdata(dev));
 320
 321	return cpumap_print_to_pagebuf(true, buf, &pmu_dev->parent->cpu);
 322}
 323
 324static DEVICE_ATTR(cpumask, S_IRUGO, xgene_pmu_cpumask_show, NULL);
 325
 326static struct attribute *xgene_pmu_cpumask_attrs[] = {
 327	&dev_attr_cpumask.attr,
 328	NULL,
 329};
 330
 331static const struct attribute_group pmu_cpumask_attr_group = {
 332	.attrs = xgene_pmu_cpumask_attrs,
 333};
 334
 335/*
 336 * Per PMU device attribute groups
 337 */
 338static const struct attribute_group *l3c_pmu_attr_groups[] = {
 339	&l3c_pmu_format_attr_group,
 340	&pmu_cpumask_attr_group,
 341	&l3c_pmu_events_attr_group,
 342	NULL
 343};
 344
 345static const struct attribute_group *iob_pmu_attr_groups[] = {
 346	&iob_pmu_format_attr_group,
 347	&pmu_cpumask_attr_group,
 348	&iob_pmu_events_attr_group,
 349	NULL
 350};
 351
 352static const struct attribute_group *mcb_pmu_attr_groups[] = {
 353	&mcb_pmu_format_attr_group,
 354	&pmu_cpumask_attr_group,
 355	&mcb_pmu_events_attr_group,
 356	NULL
 357};
 358
 359static const struct attribute_group *mc_pmu_attr_groups[] = {
 360	&mc_pmu_format_attr_group,
 361	&pmu_cpumask_attr_group,
 362	&mc_pmu_events_attr_group,
 363	NULL
 364};
 365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 366static int get_next_avail_cntr(struct xgene_pmu_dev *pmu_dev)
 367{
 368	int cntr;
 369
 370	cntr = find_first_zero_bit(pmu_dev->cntr_assign_mask,
 371				pmu_dev->max_counters);
 372	if (cntr == pmu_dev->max_counters)
 373		return -ENOSPC;
 374	set_bit(cntr, pmu_dev->cntr_assign_mask);
 375
 376	return cntr;
 377}
 378
 379static void clear_avail_cntr(struct xgene_pmu_dev *pmu_dev, int cntr)
 380{
 381	clear_bit(cntr, pmu_dev->cntr_assign_mask);
 382}
 383
 384static inline void xgene_pmu_mask_int(struct xgene_pmu *xgene_pmu)
 385{
 386	writel(PCPPMU_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 387}
 388
 
 
 
 
 
 389static inline void xgene_pmu_unmask_int(struct xgene_pmu *xgene_pmu)
 390{
 391	writel(PCPPMU_INTCLRMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
 392}
 393
 394static inline u32 xgene_pmu_read_counter(struct xgene_pmu_dev *pmu_dev, int idx)
 
 
 
 
 
 
 
 395{
 396	return readl(pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
 397}
 398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 399static inline void
 400xgene_pmu_write_counter(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
 401{
 402	writel(val, pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
 403}
 404
 405static inline void
 
 
 
 
 
 
 
 
 
 
 
 
 
 406xgene_pmu_write_evttype(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
 407{
 408	writel(val, pmu_dev->inf->csr + PMU_PMEVTYPER0 + (4 * idx));
 409}
 410
 411static inline void
 412xgene_pmu_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val)
 413{
 414	writel(val, pmu_dev->inf->csr + PMU_PMAMR0);
 415}
 416
 417static inline void
 
 
 
 418xgene_pmu_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val)
 419{
 420	writel(val, pmu_dev->inf->csr + PMU_PMAMR1);
 421}
 422
 423static inline void
 
 
 
 424xgene_pmu_enable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
 425{
 426	u32 val;
 427
 428	val = readl(pmu_dev->inf->csr + PMU_PMCNTENSET);
 429	val |= 1 << idx;
 430	writel(val, pmu_dev->inf->csr + PMU_PMCNTENSET);
 431}
 432
 433static inline void
 434xgene_pmu_disable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
 435{
 436	u32 val;
 437
 438	val = readl(pmu_dev->inf->csr + PMU_PMCNTENCLR);
 439	val |= 1 << idx;
 440	writel(val, pmu_dev->inf->csr + PMU_PMCNTENCLR);
 441}
 442
 443static inline void
 444xgene_pmu_enable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
 445{
 446	u32 val;
 447
 448	val = readl(pmu_dev->inf->csr + PMU_PMINTENSET);
 449	val |= 1 << idx;
 450	writel(val, pmu_dev->inf->csr + PMU_PMINTENSET);
 451}
 452
 453static inline void
 454xgene_pmu_disable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
 455{
 456	u32 val;
 457
 458	val = readl(pmu_dev->inf->csr + PMU_PMINTENCLR);
 459	val |= 1 << idx;
 460	writel(val, pmu_dev->inf->csr + PMU_PMINTENCLR);
 461}
 462
 463static inline void xgene_pmu_reset_counters(struct xgene_pmu_dev *pmu_dev)
 464{
 465	u32 val;
 466
 467	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 468	val |= PMU_PMCR_P;
 469	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 470}
 471
 472static inline void xgene_pmu_start_counters(struct xgene_pmu_dev *pmu_dev)
 473{
 474	u32 val;
 475
 476	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 477	val |= PMU_PMCR_E;
 478	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 479}
 480
 481static inline void xgene_pmu_stop_counters(struct xgene_pmu_dev *pmu_dev)
 482{
 483	u32 val;
 484
 485	val = readl(pmu_dev->inf->csr + PMU_PMCR);
 486	val &= ~PMU_PMCR_E;
 487	writel(val, pmu_dev->inf->csr + PMU_PMCR);
 488}
 489
 490static void xgene_perf_pmu_enable(struct pmu *pmu)
 491{
 492	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
 493	int enabled = bitmap_weight(pmu_dev->cntr_assign_mask,
 
 494			pmu_dev->max_counters);
 495
 496	if (!enabled)
 497		return;
 498
 499	xgene_pmu_start_counters(pmu_dev);
 500}
 501
 502static void xgene_perf_pmu_disable(struct pmu *pmu)
 503{
 504	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
 
 505
 506	xgene_pmu_stop_counters(pmu_dev);
 507}
 508
 509static int xgene_perf_event_init(struct perf_event *event)
 510{
 511	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 512	struct hw_perf_event *hw = &event->hw;
 513	struct perf_event *sibling;
 514
 515	/* Test the event attr type check for PMU enumeration */
 516	if (event->attr.type != event->pmu->type)
 517		return -ENOENT;
 518
 519	/*
 520	 * SOC PMU counters are shared across all cores.
 521	 * Therefore, it does not support per-process mode.
 522	 * Also, it does not support event sampling mode.
 523	 */
 524	if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
 525		return -EINVAL;
 526
 527	/* SOC counters do not have usr/os/guest/host bits */
 528	if (event->attr.exclude_user || event->attr.exclude_kernel ||
 529	    event->attr.exclude_host || event->attr.exclude_guest)
 530		return -EINVAL;
 531
 532	if (event->cpu < 0)
 533		return -EINVAL;
 534	/*
 535	 * Many perf core operations (eg. events rotation) operate on a
 536	 * single CPU context. This is obvious for CPU PMUs, where one
 537	 * expects the same sets of events being observed on all CPUs,
 538	 * but can lead to issues for off-core PMUs, where each
 539	 * event could be theoretically assigned to a different CPU. To
 540	 * mitigate this, we enforce CPU assignment to one, selected
 541	 * processor (the one described in the "cpumask" attribute).
 542	 */
 543	event->cpu = cpumask_first(&pmu_dev->parent->cpu);
 544
 545	hw->config = event->attr.config;
 546	/*
 547	 * Each bit of the config1 field represents an agent from which the
 548	 * request of the event come. The event is counted only if it's caused
 549	 * by a request of an agent has the bit cleared.
 550	 * By default, the event is counted for all agents.
 551	 */
 552	hw->config_base = event->attr.config1;
 553
 554	/*
 555	 * We must NOT create groups containing mixed PMUs, although software
 556	 * events are acceptable
 557	 */
 558	if (event->group_leader->pmu != event->pmu &&
 559			!is_software_event(event->group_leader))
 560		return -EINVAL;
 561
 562	list_for_each_entry(sibling, &event->group_leader->sibling_list,
 563			group_entry)
 564		if (sibling->pmu != event->pmu &&
 565				!is_software_event(sibling))
 566			return -EINVAL;
 
 567
 568	return 0;
 569}
 570
 571static void xgene_perf_enable_event(struct perf_event *event)
 572{
 573	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 
 574
 575	xgene_pmu_write_evttype(pmu_dev, GET_CNTR(event), GET_EVENTID(event));
 576	xgene_pmu_write_agentmsk(pmu_dev, ~((u32)GET_AGENTID(event)));
 
 577	if (pmu_dev->inf->type == PMU_TYPE_IOB)
 578		xgene_pmu_write_agent1msk(pmu_dev, ~((u32)GET_AGENT1ID(event)));
 
 579
 580	xgene_pmu_enable_counter(pmu_dev, GET_CNTR(event));
 581	xgene_pmu_enable_counter_int(pmu_dev, GET_CNTR(event));
 582}
 583
 584static void xgene_perf_disable_event(struct perf_event *event)
 585{
 586	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 
 587
 588	xgene_pmu_disable_counter(pmu_dev, GET_CNTR(event));
 589	xgene_pmu_disable_counter_int(pmu_dev, GET_CNTR(event));
 590}
 591
 592static void xgene_perf_event_set_period(struct perf_event *event)
 593{
 594	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 
 595	struct hw_perf_event *hw = &event->hw;
 596	/*
 597	 * The X-Gene PMU counters have a period of 2^32. To account for the
 598	 * possiblity of extreme interrupt latency we program for a period of
 599	 * half that. Hopefully we can handle the interrupt before another 2^31
 600	 * events occur and the counter overtakes its previous value.
 
 601	 */
 602	u64 val = 1ULL << 31;
 603
 604	local64_set(&hw->prev_count, val);
 605	xgene_pmu_write_counter(pmu_dev, hw->idx, (u32) val);
 606}
 607
 608static void xgene_perf_event_update(struct perf_event *event)
 609{
 610	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 
 611	struct hw_perf_event *hw = &event->hw;
 612	u64 delta, prev_raw_count, new_raw_count;
 613
 614again:
 615	prev_raw_count = local64_read(&hw->prev_count);
 616	new_raw_count = xgene_pmu_read_counter(pmu_dev, GET_CNTR(event));
 617
 618	if (local64_cmpxchg(&hw->prev_count, prev_raw_count,
 619			    new_raw_count) != prev_raw_count)
 620		goto again;
 621
 622	delta = (new_raw_count - prev_raw_count) & pmu_dev->max_period;
 623
 624	local64_add(delta, &event->count);
 625}
 626
 627static void xgene_perf_read(struct perf_event *event)
 628{
 629	xgene_perf_event_update(event);
 630}
 631
 632static void xgene_perf_start(struct perf_event *event, int flags)
 633{
 634	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 
 635	struct hw_perf_event *hw = &event->hw;
 636
 637	if (WARN_ON_ONCE(!(hw->state & PERF_HES_STOPPED)))
 638		return;
 639
 640	WARN_ON_ONCE(!(hw->state & PERF_HES_UPTODATE));
 641	hw->state = 0;
 642
 643	xgene_perf_event_set_period(event);
 644
 645	if (flags & PERF_EF_RELOAD) {
 646		u64 prev_raw_count =  local64_read(&hw->prev_count);
 647
 648		xgene_pmu_write_counter(pmu_dev, GET_CNTR(event),
 649					(u32) prev_raw_count);
 650	}
 651
 652	xgene_perf_enable_event(event);
 653	perf_event_update_userpage(event);
 654}
 655
 656static void xgene_perf_stop(struct perf_event *event, int flags)
 657{
 658	struct hw_perf_event *hw = &event->hw;
 659	u64 config;
 660
 661	if (hw->state & PERF_HES_UPTODATE)
 662		return;
 663
 664	xgene_perf_disable_event(event);
 665	WARN_ON_ONCE(hw->state & PERF_HES_STOPPED);
 666	hw->state |= PERF_HES_STOPPED;
 667
 668	if (hw->state & PERF_HES_UPTODATE)
 669		return;
 670
 671	config = hw->config;
 672	xgene_perf_read(event);
 673	hw->state |= PERF_HES_UPTODATE;
 674}
 675
 676static int xgene_perf_add(struct perf_event *event, int flags)
 677{
 678	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 679	struct hw_perf_event *hw = &event->hw;
 680
 681	hw->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
 682
 683	/* Allocate an event counter */
 684	hw->idx = get_next_avail_cntr(pmu_dev);
 685	if (hw->idx < 0)
 686		return -EAGAIN;
 687
 688	/* Update counter event pointer for Interrupt handler */
 689	pmu_dev->pmu_counter_event[hw->idx] = event;
 690
 691	if (flags & PERF_EF_START)
 692		xgene_perf_start(event, PERF_EF_RELOAD);
 693
 694	return 0;
 695}
 696
 697static void xgene_perf_del(struct perf_event *event, int flags)
 698{
 699	struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
 700	struct hw_perf_event *hw = &event->hw;
 701
 702	xgene_perf_stop(event, PERF_EF_UPDATE);
 703
 704	/* clear the assigned counter */
 705	clear_avail_cntr(pmu_dev, GET_CNTR(event));
 706
 707	perf_event_update_userpage(event);
 708	pmu_dev->pmu_counter_event[hw->idx] = NULL;
 709}
 710
 711static int xgene_init_perf(struct xgene_pmu_dev *pmu_dev, char *name)
 712{
 713	struct xgene_pmu *xgene_pmu;
 714
 715	pmu_dev->max_period = PMU_CNT_MAX_PERIOD - 1;
 
 
 
 716	/* First version PMU supports only single event counter */
 717	xgene_pmu = pmu_dev->parent;
 718	if (xgene_pmu->version == PCP_PMU_V1)
 719		pmu_dev->max_counters = 1;
 720	else
 721		pmu_dev->max_counters = PMU_MAX_COUNTERS;
 722
 723	/* Perf driver registration */
 724	pmu_dev->pmu = (struct pmu) {
 725		.attr_groups	= pmu_dev->attr_groups,
 726		.task_ctx_nr	= perf_invalid_context,
 727		.pmu_enable	= xgene_perf_pmu_enable,
 728		.pmu_disable	= xgene_perf_pmu_disable,
 729		.event_init	= xgene_perf_event_init,
 730		.add		= xgene_perf_add,
 731		.del		= xgene_perf_del,
 732		.start		= xgene_perf_start,
 733		.stop		= xgene_perf_stop,
 734		.read		= xgene_perf_read,
 
 735	};
 736
 737	/* Hardware counter init */
 738	xgene_pmu_stop_counters(pmu_dev);
 739	xgene_pmu_reset_counters(pmu_dev);
 740
 741	return perf_pmu_register(&pmu_dev->pmu, name, -1);
 742}
 743
 744static int
 745xgene_pmu_dev_add(struct xgene_pmu *xgene_pmu, struct xgene_pmu_dev_ctx *ctx)
 746{
 747	struct device *dev = xgene_pmu->dev;
 748	struct xgene_pmu_dev *pmu;
 749	int rc;
 750
 751	pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);
 752	if (!pmu)
 753		return -ENOMEM;
 754	pmu->parent = xgene_pmu;
 755	pmu->inf = &ctx->inf;
 756	ctx->pmu_dev = pmu;
 757
 758	switch (pmu->inf->type) {
 759	case PMU_TYPE_L3C:
 760		pmu->attr_groups = l3c_pmu_attr_groups;
 
 
 
 
 
 761		break;
 762	case PMU_TYPE_IOB:
 763		pmu->attr_groups = iob_pmu_attr_groups;
 
 
 
 
 
 
 
 764		break;
 765	case PMU_TYPE_MCB:
 766		if (!(xgene_pmu->mcb_active_mask & pmu->inf->enable_mask))
 767			goto dev_err;
 768		pmu->attr_groups = mcb_pmu_attr_groups;
 
 
 
 769		break;
 770	case PMU_TYPE_MC:
 771		if (!(xgene_pmu->mc_active_mask & pmu->inf->enable_mask))
 772			goto dev_err;
 773		pmu->attr_groups = mc_pmu_attr_groups;
 
 
 
 774		break;
 775	default:
 776		return -EINVAL;
 777	}
 778
 779	rc = xgene_init_perf(pmu, ctx->name);
 780	if (rc) {
 781		dev_err(dev, "%s PMU: Failed to init perf driver\n", ctx->name);
 782		goto dev_err;
 783	}
 784
 785	dev_info(dev, "%s PMU registered\n", ctx->name);
 786
 787	return rc;
 788
 789dev_err:
 790	devm_kfree(dev, pmu);
 791	return -ENODEV;
 792}
 793
 794static void _xgene_pmu_isr(int irq, struct xgene_pmu_dev *pmu_dev)
 795{
 796	struct xgene_pmu *xgene_pmu = pmu_dev->parent;
 
 797	u32 pmovsr;
 798	int idx;
 799
 800	pmovsr = readl(pmu_dev->inf->csr + PMU_PMOVSR) & PMU_OVERFLOW_MASK;
 
 
 
 
 
 
 801	if (!pmovsr)
 802		return;
 803
 804	/* Clear interrupt flag */
 805	if (xgene_pmu->version == PCP_PMU_V1)
 806		writel(0x0, pmu_dev->inf->csr + PMU_PMOVSR);
 
 
 807	else
 808		writel(pmovsr, pmu_dev->inf->csr + PMU_PMOVSR);
 809
 810	for (idx = 0; idx < PMU_MAX_COUNTERS; idx++) {
 811		struct perf_event *event = pmu_dev->pmu_counter_event[idx];
 812		int overflowed = pmovsr & BIT(idx);
 813
 814		/* Ignore if we don't have an event. */
 815		if (!event || !overflowed)
 816			continue;
 817		xgene_perf_event_update(event);
 818		xgene_perf_event_set_period(event);
 819	}
 
 
 
 820}
 821
 822static irqreturn_t xgene_pmu_isr(int irq, void *dev_id)
 823{
 
 824	struct xgene_pmu_dev_ctx *ctx;
 825	struct xgene_pmu *xgene_pmu = dev_id;
 826	unsigned long flags;
 827	u32 val;
 828
 829	raw_spin_lock_irqsave(&xgene_pmu->lock, flags);
 830
 831	/* Get Interrupt PMU source */
 832	val = readl(xgene_pmu->pcppmu_csr + PCPPMU_INTSTATUS_REG);
 833	if (val & PCPPMU_INT_MCU) {
 
 
 
 
 
 
 
 
 
 
 
 834		list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
 835			_xgene_pmu_isr(irq, ctx->pmu_dev);
 836		}
 837	}
 838	if (val & PCPPMU_INT_MCB) {
 839		list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
 840			_xgene_pmu_isr(irq, ctx->pmu_dev);
 841		}
 842	}
 843	if (val & PCPPMU_INT_L3C) {
 844		list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
 845			_xgene_pmu_isr(irq, ctx->pmu_dev);
 846		}
 847	}
 848	if (val & PCPPMU_INT_IOB) {
 849		list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
 850			_xgene_pmu_isr(irq, ctx->pmu_dev);
 851		}
 852	}
 853
 854	raw_spin_unlock_irqrestore(&xgene_pmu->lock, flags);
 855
 856	return IRQ_HANDLED;
 857}
 858
 859static int acpi_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
 860					 struct platform_device *pdev)
 861{
 862	void __iomem *csw_csr, *mcba_csr, *mcbb_csr;
 863	struct resource *res;
 864	unsigned int reg;
 865
 866	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 867	csw_csr = devm_ioremap_resource(&pdev->dev, res);
 868	if (IS_ERR(csw_csr)) {
 869		dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
 870		return PTR_ERR(csw_csr);
 871	}
 872
 873	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
 874	mcba_csr = devm_ioremap_resource(&pdev->dev, res);
 875	if (IS_ERR(mcba_csr)) {
 876		dev_err(&pdev->dev, "ioremap failed for MCBA CSR resource\n");
 877		return PTR_ERR(mcba_csr);
 878	}
 879
 880	res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
 881	mcbb_csr = devm_ioremap_resource(&pdev->dev, res);
 882	if (IS_ERR(mcbb_csr)) {
 883		dev_err(&pdev->dev, "ioremap failed for MCBB CSR resource\n");
 884		return PTR_ERR(mcbb_csr);
 885	}
 886
 
 
 887	reg = readl(csw_csr + CSW_CSWCR);
 888	if (reg & CSW_CSWCR_DUALMCB_MASK) {
 889		/* Dual MCB active */
 890		xgene_pmu->mcb_active_mask = 0x3;
 891		/* Probe all active MC(s) */
 892		reg = readl(mcbb_csr + CSW_CSWCR);
 893		xgene_pmu->mc_active_mask =
 894			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
 895	} else {
 896		/* Single MCB active */
 897		xgene_pmu->mcb_active_mask = 0x1;
 898		/* Probe all active MC(s) */
 899		reg = readl(mcba_csr + CSW_CSWCR);
 900		xgene_pmu->mc_active_mask =
 901			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
 902	}
 903
 904	return 0;
 905}
 906
 907static int fdt_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
 908					struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 909{
 910	struct regmap *csw_map, *mcba_map, *mcbb_map;
 911	struct device_node *np = pdev->dev.of_node;
 912	unsigned int reg;
 913
 914	csw_map = syscon_regmap_lookup_by_phandle(np, "regmap-csw");
 915	if (IS_ERR(csw_map)) {
 916		dev_err(&pdev->dev, "unable to get syscon regmap csw\n");
 917		return PTR_ERR(csw_map);
 918	}
 919
 920	mcba_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcba");
 921	if (IS_ERR(mcba_map)) {
 922		dev_err(&pdev->dev, "unable to get syscon regmap mcba\n");
 923		return PTR_ERR(mcba_map);
 924	}
 925
 926	mcbb_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcbb");
 927	if (IS_ERR(mcbb_map)) {
 928		dev_err(&pdev->dev, "unable to get syscon regmap mcbb\n");
 929		return PTR_ERR(mcbb_map);
 930	}
 931
 
 932	if (regmap_read(csw_map, CSW_CSWCR, &reg))
 933		return -EINVAL;
 934
 935	if (reg & CSW_CSWCR_DUALMCB_MASK) {
 936		/* Dual MCB active */
 937		xgene_pmu->mcb_active_mask = 0x3;
 938		/* Probe all active MC(s) */
 939		if (regmap_read(mcbb_map, MCBADDRMR, &reg))
 940			return 0;
 941		xgene_pmu->mc_active_mask =
 942			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
 943	} else {
 944		/* Single MCB active */
 945		xgene_pmu->mcb_active_mask = 0x1;
 946		/* Probe all active MC(s) */
 947		if (regmap_read(mcba_map, MCBADDRMR, &reg))
 948			return 0;
 949		xgene_pmu->mc_active_mask =
 950			(reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
 951	}
 952
 953	return 0;
 954}
 955
 956static int xgene_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
 957					  struct platform_device *pdev)
 958{
 959	if (has_acpi_companion(&pdev->dev))
 960		return acpi_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
 961	return fdt_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
 
 
 
 
 
 
 962}
 963
 964static char *xgene_pmu_dev_name(struct device *dev, u32 type, int id)
 965{
 966	switch (type) {
 967	case PMU_TYPE_L3C:
 968		return devm_kasprintf(dev, GFP_KERNEL, "l3c%d", id);
 969	case PMU_TYPE_IOB:
 970		return devm_kasprintf(dev, GFP_KERNEL, "iob%d", id);
 
 
 971	case PMU_TYPE_MCB:
 972		return devm_kasprintf(dev, GFP_KERNEL, "mcb%d", id);
 973	case PMU_TYPE_MC:
 974		return devm_kasprintf(dev, GFP_KERNEL, "mc%d", id);
 975	default:
 976		return devm_kasprintf(dev, GFP_KERNEL, "unknown");
 977	}
 978}
 979
 980#if defined(CONFIG_ACPI)
 981static int acpi_pmu_dev_add_resource(struct acpi_resource *ares, void *data)
 982{
 983	struct resource *res = data;
 984
 985	if (ares->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32)
 986		acpi_dev_resource_memory(ares, res);
 987
 988	/* Always tell the ACPI core to skip this resource */
 989	return 1;
 990}
 991
 992static struct
 993xgene_pmu_dev_ctx *acpi_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
 994				       struct acpi_device *adev, u32 type)
 995{
 996	struct device *dev = xgene_pmu->dev;
 997	struct list_head resource_list;
 998	struct xgene_pmu_dev_ctx *ctx;
 999	const union acpi_object *obj;
1000	struct hw_pmu_info *inf;
1001	void __iomem *dev_csr;
1002	struct resource res;
 
1003	int enable_bit;
1004	int rc;
1005
1006	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1007	if (!ctx)
1008		return NULL;
1009
1010	INIT_LIST_HEAD(&resource_list);
1011	rc = acpi_dev_get_resources(adev, &resource_list,
1012				    acpi_pmu_dev_add_resource, &res);
 
 
 
 
 
 
 
 
 
 
 
1013	acpi_dev_free_resource_list(&resource_list);
1014	if (rc < 0) {
1015		dev_err(dev, "PMU type %d: No resource address found\n", type);
1016		goto err;
 
1017	}
1018
1019	dev_csr = devm_ioremap_resource(dev, &res);
1020	if (IS_ERR(dev_csr)) {
1021		dev_err(dev, "PMU type %d: Fail to map resource\n", type);
1022		goto err;
1023	}
1024
1025	/* A PMU device node without enable-bit-index is always enabled */
1026	rc = acpi_dev_get_property(adev, "enable-bit-index",
1027				   ACPI_TYPE_INTEGER, &obj);
1028	if (rc < 0)
1029		enable_bit = 0;
1030	else
1031		enable_bit = (int) obj->integer.value;
1032
1033	ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1034	if (!ctx->name) {
1035		dev_err(dev, "PMU type %d: Fail to get device name\n", type);
1036		goto err;
1037	}
1038	inf = &ctx->inf;
1039	inf->type = type;
1040	inf->csr = dev_csr;
1041	inf->enable_mask = 1 << enable_bit;
1042
1043	return ctx;
1044err:
1045	devm_kfree(dev, ctx);
1046	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1047}
1048
1049static acpi_status acpi_pmu_dev_add(acpi_handle handle, u32 level,
1050				    void *data, void **return_value)
1051{
 
 
1052	struct xgene_pmu *xgene_pmu = data;
1053	struct xgene_pmu_dev_ctx *ctx;
1054	struct acpi_device *adev;
1055
1056	if (acpi_bus_get_device(handle, &adev))
1057		return AE_OK;
1058	if (acpi_bus_get_status(adev) || !adev->status.present)
 
 
1059		return AE_OK;
1060
1061	if (!strcmp(acpi_device_hid(adev), "APMC0D5D"))
1062		ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_L3C);
1063	else if (!strcmp(acpi_device_hid(adev), "APMC0D5E"))
1064		ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_IOB);
1065	else if (!strcmp(acpi_device_hid(adev), "APMC0D5F"))
1066		ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_MCB);
1067	else if (!strcmp(acpi_device_hid(adev), "APMC0D60"))
1068		ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_MC);
1069	else
1070		ctx = NULL;
1071
1072	if (!ctx)
1073		return AE_OK;
1074
1075	if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1076		/* Can't add the PMU device, skip it */
1077		devm_kfree(xgene_pmu->dev, ctx);
1078		return AE_OK;
1079	}
1080
1081	switch (ctx->inf.type) {
1082	case PMU_TYPE_L3C:
1083		list_add(&ctx->next, &xgene_pmu->l3cpmus);
1084		break;
1085	case PMU_TYPE_IOB:
1086		list_add(&ctx->next, &xgene_pmu->iobpmus);
1087		break;
 
 
 
1088	case PMU_TYPE_MCB:
1089		list_add(&ctx->next, &xgene_pmu->mcbpmus);
1090		break;
1091	case PMU_TYPE_MC:
1092		list_add(&ctx->next, &xgene_pmu->mcpmus);
1093		break;
1094	}
1095	return AE_OK;
1096}
1097
1098static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1099				  struct platform_device *pdev)
1100{
1101	struct device *dev = xgene_pmu->dev;
1102	acpi_handle handle;
1103	acpi_status status;
1104
1105	handle = ACPI_HANDLE(dev);
1106	if (!handle)
1107		return -EINVAL;
1108
1109	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1110				     acpi_pmu_dev_add, NULL, xgene_pmu, NULL);
1111	if (ACPI_FAILURE(status)) {
1112		dev_err(dev, "failed to probe PMU devices\n");
1113		return -ENODEV;
1114	}
1115
1116	return 0;
1117}
1118#else
1119static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1120				  struct platform_device *pdev)
1121{
1122	return 0;
1123}
1124#endif
1125
1126static struct
1127xgene_pmu_dev_ctx *fdt_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
1128				      struct device_node *np, u32 type)
1129{
1130	struct device *dev = xgene_pmu->dev;
1131	struct xgene_pmu_dev_ctx *ctx;
1132	struct hw_pmu_info *inf;
1133	void __iomem *dev_csr;
1134	struct resource res;
1135	int enable_bit;
1136	int rc;
1137
1138	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1139	if (!ctx)
1140		return NULL;
1141	rc = of_address_to_resource(np, 0, &res);
1142	if (rc < 0) {
1143		dev_err(dev, "PMU type %d: No resource address found\n", type);
1144		goto err;
1145	}
 
1146	dev_csr = devm_ioremap_resource(dev, &res);
1147	if (IS_ERR(dev_csr)) {
1148		dev_err(dev, "PMU type %d: Fail to map resource\n", type);
1149		goto err;
1150	}
1151
1152	/* A PMU device node without enable-bit-index is always enabled */
1153	if (of_property_read_u32(np, "enable-bit-index", &enable_bit))
1154		enable_bit = 0;
1155
1156	ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
1157	if (!ctx->name) {
1158		dev_err(dev, "PMU type %d: Fail to get device name\n", type);
1159		goto err;
1160	}
 
1161	inf = &ctx->inf;
1162	inf->type = type;
1163	inf->csr = dev_csr;
1164	inf->enable_mask = 1 << enable_bit;
1165
1166	return ctx;
1167err:
1168	devm_kfree(dev, ctx);
1169	return NULL;
1170}
1171
1172static int fdt_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1173				 struct platform_device *pdev)
1174{
1175	struct xgene_pmu_dev_ctx *ctx;
1176	struct device_node *np;
1177
1178	for_each_child_of_node(pdev->dev.of_node, np) {
1179		if (!of_device_is_available(np))
1180			continue;
1181
1182		if (of_device_is_compatible(np, "apm,xgene-pmu-l3c"))
1183			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_L3C);
1184		else if (of_device_is_compatible(np, "apm,xgene-pmu-iob"))
1185			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_IOB);
1186		else if (of_device_is_compatible(np, "apm,xgene-pmu-mcb"))
1187			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MCB);
1188		else if (of_device_is_compatible(np, "apm,xgene-pmu-mc"))
1189			ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MC);
1190		else
1191			ctx = NULL;
1192
1193		if (!ctx)
1194			continue;
1195
1196		if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
1197			/* Can't add the PMU device, skip it */
1198			devm_kfree(xgene_pmu->dev, ctx);
1199			continue;
1200		}
1201
1202		switch (ctx->inf.type) {
1203		case PMU_TYPE_L3C:
1204			list_add(&ctx->next, &xgene_pmu->l3cpmus);
1205			break;
1206		case PMU_TYPE_IOB:
1207			list_add(&ctx->next, &xgene_pmu->iobpmus);
1208			break;
 
 
 
1209		case PMU_TYPE_MCB:
1210			list_add(&ctx->next, &xgene_pmu->mcbpmus);
1211			break;
1212		case PMU_TYPE_MC:
1213			list_add(&ctx->next, &xgene_pmu->mcpmus);
1214			break;
1215		}
1216	}
1217
1218	return 0;
1219}
1220
1221static int xgene_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
1222				   struct platform_device *pdev)
1223{
1224	if (has_acpi_companion(&pdev->dev))
1225		return acpi_pmu_probe_pmu_dev(xgene_pmu, pdev);
1226	return fdt_pmu_probe_pmu_dev(xgene_pmu, pdev);
1227}
1228
1229static const struct xgene_pmu_data xgene_pmu_data = {
1230	.id   = PCP_PMU_V1,
1231};
1232
1233static const struct xgene_pmu_data xgene_pmu_v2_data = {
1234	.id   = PCP_PMU_V2,
1235};
1236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1237static const struct of_device_id xgene_pmu_of_match[] = {
1238	{ .compatible	= "apm,xgene-pmu",	.data = &xgene_pmu_data },
1239	{ .compatible	= "apm,xgene-pmu-v2",	.data = &xgene_pmu_v2_data },
1240	{},
1241};
1242MODULE_DEVICE_TABLE(of, xgene_pmu_of_match);
1243#ifdef CONFIG_ACPI
1244static const struct acpi_device_id xgene_pmu_acpi_match[] = {
1245	{"APMC0D5B", PCP_PMU_V1},
1246	{"APMC0D5C", PCP_PMU_V2},
 
1247	{},
1248};
1249MODULE_DEVICE_TABLE(acpi, xgene_pmu_acpi_match);
1250#endif
1251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1252static int xgene_pmu_probe(struct platform_device *pdev)
1253{
1254	const struct xgene_pmu_data *dev_data;
1255	const struct of_device_id *of_id;
1256	struct xgene_pmu *xgene_pmu;
1257	struct resource *res;
1258	int irq, rc;
1259	int version;
1260
 
 
 
 
 
 
 
 
1261	xgene_pmu = devm_kzalloc(&pdev->dev, sizeof(*xgene_pmu), GFP_KERNEL);
1262	if (!xgene_pmu)
1263		return -ENOMEM;
1264	xgene_pmu->dev = &pdev->dev;
1265	platform_set_drvdata(pdev, xgene_pmu);
1266
1267	version = -EINVAL;
1268	of_id = of_match_device(xgene_pmu_of_match, &pdev->dev);
1269	if (of_id) {
1270		dev_data = (const struct xgene_pmu_data *) of_id->data;
1271		version = dev_data->id;
1272	}
1273
1274#ifdef CONFIG_ACPI
1275	if (ACPI_COMPANION(&pdev->dev)) {
1276		const struct acpi_device_id *acpi_id;
1277
1278		acpi_id = acpi_match_device(xgene_pmu_acpi_match, &pdev->dev);
1279		if (acpi_id)
1280			version = (int) acpi_id->driver_data;
1281	}
1282#endif
1283	if (version < 0)
1284		return -ENODEV;
1285
1286	INIT_LIST_HEAD(&xgene_pmu->l3cpmus);
1287	INIT_LIST_HEAD(&xgene_pmu->iobpmus);
1288	INIT_LIST_HEAD(&xgene_pmu->mcbpmus);
1289	INIT_LIST_HEAD(&xgene_pmu->mcpmus);
1290
1291	xgene_pmu->version = version;
1292	dev_info(&pdev->dev, "X-Gene PMU version %d\n", xgene_pmu->version);
1293
1294	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1295	xgene_pmu->pcppmu_csr = devm_ioremap_resource(&pdev->dev, res);
1296	if (IS_ERR(xgene_pmu->pcppmu_csr)) {
1297		dev_err(&pdev->dev, "ioremap failed for PCP PMU resource\n");
1298		rc = PTR_ERR(xgene_pmu->pcppmu_csr);
1299		goto err;
1300	}
1301
1302	irq = platform_get_irq(pdev, 0);
1303	if (irq < 0) {
1304		dev_err(&pdev->dev, "No IRQ resource\n");
1305		rc = -EINVAL;
1306		goto err;
1307	}
1308	rc = devm_request_irq(&pdev->dev, irq, xgene_pmu_isr,
1309				IRQF_NOBALANCING | IRQF_NO_THREAD,
1310				dev_name(&pdev->dev), xgene_pmu);
1311	if (rc) {
1312		dev_err(&pdev->dev, "Could not request IRQ %d\n", irq);
1313		goto err;
1314	}
1315
 
 
1316	raw_spin_lock_init(&xgene_pmu->lock);
1317
1318	/* Check for active MCBs and MCUs */
1319	rc = xgene_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
1320	if (rc) {
1321		dev_warn(&pdev->dev, "Unknown MCB/MCU active status\n");
1322		xgene_pmu->mcb_active_mask = 0x1;
1323		xgene_pmu->mc_active_mask = 0x1;
1324	}
1325
1326	/* Pick one core to use for cpumask attributes */
1327	cpumask_set_cpu(smp_processor_id(), &xgene_pmu->cpu);
1328
1329	/* Make sure that the overflow interrupt is handled by this CPU */
1330	rc = irq_set_affinity(irq, &xgene_pmu->cpu);
1331	if (rc) {
1332		dev_err(&pdev->dev, "Failed to set interrupt affinity!\n");
1333		goto err;
1334	}
1335
1336	/* Walk through the tree for all PMU perf devices */
1337	rc = xgene_pmu_probe_pmu_dev(xgene_pmu, pdev);
1338	if (rc) {
1339		dev_err(&pdev->dev, "No PMU perf devices found!\n");
1340		goto err;
1341	}
1342
1343	/* Enable interrupt */
1344	xgene_pmu_unmask_int(xgene_pmu);
1345
1346	return 0;
1347
1348err:
1349	if (xgene_pmu->pcppmu_csr)
1350		devm_iounmap(&pdev->dev, xgene_pmu->pcppmu_csr);
1351	devm_kfree(&pdev->dev, xgene_pmu);
1352
1353	return rc;
1354}
1355
1356static void
1357xgene_pmu_dev_cleanup(struct xgene_pmu *xgene_pmu, struct list_head *pmus)
1358{
1359	struct xgene_pmu_dev_ctx *ctx;
1360	struct device *dev = xgene_pmu->dev;
1361	struct xgene_pmu_dev *pmu_dev;
1362
1363	list_for_each_entry(ctx, pmus, next) {
1364		pmu_dev = ctx->pmu_dev;
1365		if (pmu_dev->inf->csr)
1366			devm_iounmap(dev, pmu_dev->inf->csr);
1367		devm_kfree(dev, ctx);
1368		devm_kfree(dev, pmu_dev);
1369	}
1370}
1371
1372static int xgene_pmu_remove(struct platform_device *pdev)
1373{
1374	struct xgene_pmu *xgene_pmu = dev_get_drvdata(&pdev->dev);
1375
1376	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->l3cpmus);
1377	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->iobpmus);
1378	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcbpmus);
1379	xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcpmus);
1380
1381	if (xgene_pmu->pcppmu_csr)
1382		devm_iounmap(&pdev->dev, xgene_pmu->pcppmu_csr);
1383	devm_kfree(&pdev->dev, xgene_pmu);
1384
1385	return 0;
1386}
1387
1388static struct platform_driver xgene_pmu_driver = {
1389	.probe = xgene_pmu_probe,
1390	.remove = xgene_pmu_remove,
1391	.driver = {
1392		.name		= "xgene-pmu",
1393		.of_match_table = xgene_pmu_of_match,
1394		.acpi_match_table = ACPI_PTR(xgene_pmu_acpi_match),
 
1395	},
1396};
1397
1398builtin_platform_driver(xgene_pmu_driver);