0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #include <linux/acpi.h>
0038 #include <linux/acpi_iort.h>
0039 #include <linux/bitfield.h>
0040 #include <linux/bitops.h>
0041 #include <linux/cpuhotplug.h>
0042 #include <linux/cpumask.h>
0043 #include <linux/device.h>
0044 #include <linux/errno.h>
0045 #include <linux/interrupt.h>
0046 #include <linux/irq.h>
0047 #include <linux/kernel.h>
0048 #include <linux/list.h>
0049 #include <linux/msi.h>
0050 #include <linux/of.h>
0051 #include <linux/perf_event.h>
0052 #include <linux/platform_device.h>
0053 #include <linux/smp.h>
0054 #include <linux/sysfs.h>
0055 #include <linux/types.h>
0056
0057 #define SMMU_PMCG_EVCNTR0 0x0
0058 #define SMMU_PMCG_EVCNTR(n, stride) (SMMU_PMCG_EVCNTR0 + (n) * (stride))
0059 #define SMMU_PMCG_EVTYPER0 0x400
0060 #define SMMU_PMCG_EVTYPER(n) (SMMU_PMCG_EVTYPER0 + (n) * 4)
0061 #define SMMU_PMCG_SID_SPAN_SHIFT 29
0062 #define SMMU_PMCG_SMR0 0xA00
0063 #define SMMU_PMCG_SMR(n) (SMMU_PMCG_SMR0 + (n) * 4)
0064 #define SMMU_PMCG_CNTENSET0 0xC00
0065 #define SMMU_PMCG_CNTENCLR0 0xC20
0066 #define SMMU_PMCG_INTENSET0 0xC40
0067 #define SMMU_PMCG_INTENCLR0 0xC60
0068 #define SMMU_PMCG_OVSCLR0 0xC80
0069 #define SMMU_PMCG_OVSSET0 0xCC0
0070 #define SMMU_PMCG_CFGR 0xE00
0071 #define SMMU_PMCG_CFGR_SID_FILTER_TYPE BIT(23)
0072 #define SMMU_PMCG_CFGR_MSI BIT(21)
0073 #define SMMU_PMCG_CFGR_RELOC_CTRS BIT(20)
0074 #define SMMU_PMCG_CFGR_SIZE GENMASK(13, 8)
0075 #define SMMU_PMCG_CFGR_NCTR GENMASK(5, 0)
0076 #define SMMU_PMCG_CR 0xE04
0077 #define SMMU_PMCG_CR_ENABLE BIT(0)
0078 #define SMMU_PMCG_IIDR 0xE08
0079 #define SMMU_PMCG_IIDR_PRODUCTID GENMASK(31, 20)
0080 #define SMMU_PMCG_IIDR_VARIANT GENMASK(19, 16)
0081 #define SMMU_PMCG_IIDR_REVISION GENMASK(15, 12)
0082 #define SMMU_PMCG_IIDR_IMPLEMENTER GENMASK(11, 0)
0083 #define SMMU_PMCG_CEID0 0xE20
0084 #define SMMU_PMCG_CEID1 0xE28
0085 #define SMMU_PMCG_IRQ_CTRL 0xE50
0086 #define SMMU_PMCG_IRQ_CTRL_IRQEN BIT(0)
0087 #define SMMU_PMCG_IRQ_CFG0 0xE58
0088 #define SMMU_PMCG_IRQ_CFG1 0xE60
0089 #define SMMU_PMCG_IRQ_CFG2 0xE64
0090
0091
0092 #define SMMU_PMCG_PIDR0 0xFE0
0093 #define SMMU_PMCG_PIDR0_PART_0 GENMASK(7, 0)
0094 #define SMMU_PMCG_PIDR1 0xFE4
0095 #define SMMU_PMCG_PIDR1_DES_0 GENMASK(7, 4)
0096 #define SMMU_PMCG_PIDR1_PART_1 GENMASK(3, 0)
0097 #define SMMU_PMCG_PIDR2 0xFE8
0098 #define SMMU_PMCG_PIDR2_REVISION GENMASK(7, 4)
0099 #define SMMU_PMCG_PIDR2_DES_1 GENMASK(2, 0)
0100 #define SMMU_PMCG_PIDR3 0xFEC
0101 #define SMMU_PMCG_PIDR3_REVAND GENMASK(7, 4)
0102 #define SMMU_PMCG_PIDR4 0xFD0
0103 #define SMMU_PMCG_PIDR4_DES_2 GENMASK(3, 0)
0104
0105
0106 #define MSI_CFG0_ADDR_MASK GENMASK_ULL(51, 2)
0107 #define MSI_CFG2_MEMATTR_DEVICE_nGnRE 0x1
0108
0109 #define SMMU_PMCG_DEFAULT_FILTER_SPAN 1
0110 #define SMMU_PMCG_DEFAULT_FILTER_SID GENMASK(31, 0)
0111
0112 #define SMMU_PMCG_MAX_COUNTERS 64
0113 #define SMMU_PMCG_ARCH_MAX_EVENTS 128
0114
0115 #define SMMU_PMCG_PA_SHIFT 12
0116
0117 #define SMMU_PMCG_EVCNTR_RDONLY BIT(0)
0118
0119 static int cpuhp_state_num;
0120
0121 struct smmu_pmu {
0122 struct hlist_node node;
0123 struct perf_event *events[SMMU_PMCG_MAX_COUNTERS];
0124 DECLARE_BITMAP(used_counters, SMMU_PMCG_MAX_COUNTERS);
0125 DECLARE_BITMAP(supported_events, SMMU_PMCG_ARCH_MAX_EVENTS);
0126 unsigned int irq;
0127 unsigned int on_cpu;
0128 struct pmu pmu;
0129 unsigned int num_counters;
0130 struct device *dev;
0131 void __iomem *reg_base;
0132 void __iomem *reloc_base;
0133 u64 counter_mask;
0134 u32 options;
0135 u32 iidr;
0136 bool global_filter;
0137 };
0138
0139 #define to_smmu_pmu(p) (container_of(p, struct smmu_pmu, pmu))
0140
0141 #define SMMU_PMU_EVENT_ATTR_EXTRACTOR(_name, _config, _start, _end) \
0142 static inline u32 get_##_name(struct perf_event *event) \
0143 { \
0144 return FIELD_GET(GENMASK_ULL(_end, _start), \
0145 event->attr._config); \
0146 } \
0147
0148 SMMU_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 15);
0149 SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_stream_id, config1, 0, 31);
0150 SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_span, config1, 32, 32);
0151 SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_enable, config1, 33, 33);
0152
0153 static inline void smmu_pmu_enable(struct pmu *pmu)
0154 {
0155 struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
0156
0157 writel(SMMU_PMCG_IRQ_CTRL_IRQEN,
0158 smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
0159 writel(SMMU_PMCG_CR_ENABLE, smmu_pmu->reg_base + SMMU_PMCG_CR);
0160 }
0161
0162 static inline void smmu_pmu_disable(struct pmu *pmu)
0163 {
0164 struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
0165
0166 writel(0, smmu_pmu->reg_base + SMMU_PMCG_CR);
0167 writel(0, smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
0168 }
0169
0170 static inline void smmu_pmu_counter_set_value(struct smmu_pmu *smmu_pmu,
0171 u32 idx, u64 value)
0172 {
0173 if (smmu_pmu->counter_mask & BIT(32))
0174 writeq(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
0175 else
0176 writel(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
0177 }
0178
0179 static inline u64 smmu_pmu_counter_get_value(struct smmu_pmu *smmu_pmu, u32 idx)
0180 {
0181 u64 value;
0182
0183 if (smmu_pmu->counter_mask & BIT(32))
0184 value = readq(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
0185 else
0186 value = readl(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
0187
0188 return value;
0189 }
0190
0191 static inline void smmu_pmu_counter_enable(struct smmu_pmu *smmu_pmu, u32 idx)
0192 {
0193 writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENSET0);
0194 }
0195
0196 static inline void smmu_pmu_counter_disable(struct smmu_pmu *smmu_pmu, u32 idx)
0197 {
0198 writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
0199 }
0200
0201 static inline void smmu_pmu_interrupt_enable(struct smmu_pmu *smmu_pmu, u32 idx)
0202 {
0203 writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENSET0);
0204 }
0205
0206 static inline void smmu_pmu_interrupt_disable(struct smmu_pmu *smmu_pmu,
0207 u32 idx)
0208 {
0209 writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
0210 }
0211
0212 static inline void smmu_pmu_set_evtyper(struct smmu_pmu *smmu_pmu, u32 idx,
0213 u32 val)
0214 {
0215 writel(val, smmu_pmu->reg_base + SMMU_PMCG_EVTYPER(idx));
0216 }
0217
0218 static inline void smmu_pmu_set_smr(struct smmu_pmu *smmu_pmu, u32 idx, u32 val)
0219 {
0220 writel(val, smmu_pmu->reg_base + SMMU_PMCG_SMR(idx));
0221 }
0222
0223 static void smmu_pmu_event_update(struct perf_event *event)
0224 {
0225 struct hw_perf_event *hwc = &event->hw;
0226 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0227 u64 delta, prev, now;
0228 u32 idx = hwc->idx;
0229
0230 do {
0231 prev = local64_read(&hwc->prev_count);
0232 now = smmu_pmu_counter_get_value(smmu_pmu, idx);
0233 } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
0234
0235
0236 delta = now - prev;
0237 delta &= smmu_pmu->counter_mask;
0238
0239 local64_add(delta, &event->count);
0240 }
0241
0242 static void smmu_pmu_set_period(struct smmu_pmu *smmu_pmu,
0243 struct hw_perf_event *hwc)
0244 {
0245 u32 idx = hwc->idx;
0246 u64 new;
0247
0248 if (smmu_pmu->options & SMMU_PMCG_EVCNTR_RDONLY) {
0249
0250
0251
0252
0253
0254
0255
0256 new = smmu_pmu_counter_get_value(smmu_pmu, idx);
0257 } else {
0258
0259
0260
0261
0262
0263
0264 new = smmu_pmu->counter_mask >> 1;
0265 smmu_pmu_counter_set_value(smmu_pmu, idx, new);
0266 }
0267
0268 local64_set(&hwc->prev_count, new);
0269 }
0270
0271 static void smmu_pmu_set_event_filter(struct perf_event *event,
0272 int idx, u32 span, u32 sid)
0273 {
0274 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0275 u32 evtyper;
0276
0277 evtyper = get_event(event) | span << SMMU_PMCG_SID_SPAN_SHIFT;
0278 smmu_pmu_set_evtyper(smmu_pmu, idx, evtyper);
0279 smmu_pmu_set_smr(smmu_pmu, idx, sid);
0280 }
0281
0282 static bool smmu_pmu_check_global_filter(struct perf_event *curr,
0283 struct perf_event *new)
0284 {
0285 if (get_filter_enable(new) != get_filter_enable(curr))
0286 return false;
0287
0288 if (!get_filter_enable(new))
0289 return true;
0290
0291 return get_filter_span(new) == get_filter_span(curr) &&
0292 get_filter_stream_id(new) == get_filter_stream_id(curr);
0293 }
0294
0295 static int smmu_pmu_apply_event_filter(struct smmu_pmu *smmu_pmu,
0296 struct perf_event *event, int idx)
0297 {
0298 u32 span, sid;
0299 unsigned int cur_idx, num_ctrs = smmu_pmu->num_counters;
0300 bool filter_en = !!get_filter_enable(event);
0301
0302 span = filter_en ? get_filter_span(event) :
0303 SMMU_PMCG_DEFAULT_FILTER_SPAN;
0304 sid = filter_en ? get_filter_stream_id(event) :
0305 SMMU_PMCG_DEFAULT_FILTER_SID;
0306
0307 cur_idx = find_first_bit(smmu_pmu->used_counters, num_ctrs);
0308
0309
0310
0311
0312 if (!smmu_pmu->global_filter || cur_idx == num_ctrs) {
0313 smmu_pmu_set_event_filter(event, idx, span, sid);
0314 return 0;
0315 }
0316
0317
0318 if (smmu_pmu_check_global_filter(smmu_pmu->events[cur_idx], event)) {
0319 smmu_pmu_set_evtyper(smmu_pmu, idx, get_event(event));
0320 return 0;
0321 }
0322
0323 return -EAGAIN;
0324 }
0325
0326 static int smmu_pmu_get_event_idx(struct smmu_pmu *smmu_pmu,
0327 struct perf_event *event)
0328 {
0329 int idx, err;
0330 unsigned int num_ctrs = smmu_pmu->num_counters;
0331
0332 idx = find_first_zero_bit(smmu_pmu->used_counters, num_ctrs);
0333 if (idx == num_ctrs)
0334
0335 return -EAGAIN;
0336
0337 err = smmu_pmu_apply_event_filter(smmu_pmu, event, idx);
0338 if (err)
0339 return err;
0340
0341 set_bit(idx, smmu_pmu->used_counters);
0342
0343 return idx;
0344 }
0345
0346 static bool smmu_pmu_events_compatible(struct perf_event *curr,
0347 struct perf_event *new)
0348 {
0349 if (new->pmu != curr->pmu)
0350 return false;
0351
0352 if (to_smmu_pmu(new->pmu)->global_filter &&
0353 !smmu_pmu_check_global_filter(curr, new))
0354 return false;
0355
0356 return true;
0357 }
0358
0359
0360
0361
0362
0363
0364 static int smmu_pmu_event_init(struct perf_event *event)
0365 {
0366 struct hw_perf_event *hwc = &event->hw;
0367 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0368 struct device *dev = smmu_pmu->dev;
0369 struct perf_event *sibling;
0370 int group_num_events = 1;
0371 u16 event_id;
0372
0373 if (event->attr.type != event->pmu->type)
0374 return -ENOENT;
0375
0376 if (hwc->sample_period) {
0377 dev_dbg(dev, "Sampling not supported\n");
0378 return -EOPNOTSUPP;
0379 }
0380
0381 if (event->cpu < 0) {
0382 dev_dbg(dev, "Per-task mode not supported\n");
0383 return -EOPNOTSUPP;
0384 }
0385
0386
0387 event_id = get_event(event);
0388 if (event_id < SMMU_PMCG_ARCH_MAX_EVENTS &&
0389 (!test_bit(event_id, smmu_pmu->supported_events))) {
0390 dev_dbg(dev, "Invalid event %d for this PMU\n", event_id);
0391 return -EINVAL;
0392 }
0393
0394
0395 if (!is_software_event(event->group_leader)) {
0396 if (!smmu_pmu_events_compatible(event->group_leader, event))
0397 return -EINVAL;
0398
0399 if (++group_num_events > smmu_pmu->num_counters)
0400 return -EINVAL;
0401 }
0402
0403 for_each_sibling_event(sibling, event->group_leader) {
0404 if (is_software_event(sibling))
0405 continue;
0406
0407 if (!smmu_pmu_events_compatible(sibling, event))
0408 return -EINVAL;
0409
0410 if (++group_num_events > smmu_pmu->num_counters)
0411 return -EINVAL;
0412 }
0413
0414 hwc->idx = -1;
0415
0416
0417
0418
0419
0420 event->cpu = smmu_pmu->on_cpu;
0421
0422 return 0;
0423 }
0424
0425 static void smmu_pmu_event_start(struct perf_event *event, int flags)
0426 {
0427 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0428 struct hw_perf_event *hwc = &event->hw;
0429 int idx = hwc->idx;
0430
0431 hwc->state = 0;
0432
0433 smmu_pmu_set_period(smmu_pmu, hwc);
0434
0435 smmu_pmu_counter_enable(smmu_pmu, idx);
0436 }
0437
0438 static void smmu_pmu_event_stop(struct perf_event *event, int flags)
0439 {
0440 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0441 struct hw_perf_event *hwc = &event->hw;
0442 int idx = hwc->idx;
0443
0444 if (hwc->state & PERF_HES_STOPPED)
0445 return;
0446
0447 smmu_pmu_counter_disable(smmu_pmu, idx);
0448
0449 smmu_pmu_event_update(event);
0450 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
0451 }
0452
0453 static int smmu_pmu_event_add(struct perf_event *event, int flags)
0454 {
0455 struct hw_perf_event *hwc = &event->hw;
0456 int idx;
0457 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0458
0459 idx = smmu_pmu_get_event_idx(smmu_pmu, event);
0460 if (idx < 0)
0461 return idx;
0462
0463 hwc->idx = idx;
0464 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
0465 smmu_pmu->events[idx] = event;
0466 local64_set(&hwc->prev_count, 0);
0467
0468 smmu_pmu_interrupt_enable(smmu_pmu, idx);
0469
0470 if (flags & PERF_EF_START)
0471 smmu_pmu_event_start(event, flags);
0472
0473
0474 perf_event_update_userpage(event);
0475
0476 return 0;
0477 }
0478
0479 static void smmu_pmu_event_del(struct perf_event *event, int flags)
0480 {
0481 struct hw_perf_event *hwc = &event->hw;
0482 struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
0483 int idx = hwc->idx;
0484
0485 smmu_pmu_event_stop(event, flags | PERF_EF_UPDATE);
0486 smmu_pmu_interrupt_disable(smmu_pmu, idx);
0487 smmu_pmu->events[idx] = NULL;
0488 clear_bit(idx, smmu_pmu->used_counters);
0489
0490 perf_event_update_userpage(event);
0491 }
0492
0493 static void smmu_pmu_event_read(struct perf_event *event)
0494 {
0495 smmu_pmu_event_update(event);
0496 }
0497
0498
0499
0500 static ssize_t smmu_pmu_cpumask_show(struct device *dev,
0501 struct device_attribute *attr,
0502 char *buf)
0503 {
0504 struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
0505
0506 return cpumap_print_to_pagebuf(true, buf, cpumask_of(smmu_pmu->on_cpu));
0507 }
0508
0509 static struct device_attribute smmu_pmu_cpumask_attr =
0510 __ATTR(cpumask, 0444, smmu_pmu_cpumask_show, NULL);
0511
0512 static struct attribute *smmu_pmu_cpumask_attrs[] = {
0513 &smmu_pmu_cpumask_attr.attr,
0514 NULL
0515 };
0516
0517 static const struct attribute_group smmu_pmu_cpumask_group = {
0518 .attrs = smmu_pmu_cpumask_attrs,
0519 };
0520
0521
0522
0523 static ssize_t smmu_pmu_event_show(struct device *dev,
0524 struct device_attribute *attr, char *page)
0525 {
0526 struct perf_pmu_events_attr *pmu_attr;
0527
0528 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
0529
0530 return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
0531 }
0532
0533 #define SMMU_EVENT_ATTR(name, config) \
0534 PMU_EVENT_ATTR_ID(name, smmu_pmu_event_show, config)
0535
0536 static struct attribute *smmu_pmu_events[] = {
0537 SMMU_EVENT_ATTR(cycles, 0),
0538 SMMU_EVENT_ATTR(transaction, 1),
0539 SMMU_EVENT_ATTR(tlb_miss, 2),
0540 SMMU_EVENT_ATTR(config_cache_miss, 3),
0541 SMMU_EVENT_ATTR(trans_table_walk_access, 4),
0542 SMMU_EVENT_ATTR(config_struct_access, 5),
0543 SMMU_EVENT_ATTR(pcie_ats_trans_rq, 6),
0544 SMMU_EVENT_ATTR(pcie_ats_trans_passed, 7),
0545 NULL
0546 };
0547
0548 static umode_t smmu_pmu_event_is_visible(struct kobject *kobj,
0549 struct attribute *attr, int unused)
0550 {
0551 struct device *dev = kobj_to_dev(kobj);
0552 struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
0553 struct perf_pmu_events_attr *pmu_attr;
0554
0555 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
0556
0557 if (test_bit(pmu_attr->id, smmu_pmu->supported_events))
0558 return attr->mode;
0559
0560 return 0;
0561 }
0562
0563 static const struct attribute_group smmu_pmu_events_group = {
0564 .name = "events",
0565 .attrs = smmu_pmu_events,
0566 .is_visible = smmu_pmu_event_is_visible,
0567 };
0568
0569 static ssize_t smmu_pmu_identifier_attr_show(struct device *dev,
0570 struct device_attribute *attr,
0571 char *page)
0572 {
0573 struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
0574
0575 return sysfs_emit(page, "0x%08x\n", smmu_pmu->iidr);
0576 }
0577
0578 static umode_t smmu_pmu_identifier_attr_visible(struct kobject *kobj,
0579 struct attribute *attr,
0580 int n)
0581 {
0582 struct device *dev = kobj_to_dev(kobj);
0583 struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
0584
0585 if (!smmu_pmu->iidr)
0586 return 0;
0587 return attr->mode;
0588 }
0589
0590 static struct device_attribute smmu_pmu_identifier_attr =
0591 __ATTR(identifier, 0444, smmu_pmu_identifier_attr_show, NULL);
0592
0593 static struct attribute *smmu_pmu_identifier_attrs[] = {
0594 &smmu_pmu_identifier_attr.attr,
0595 NULL
0596 };
0597
0598 static const struct attribute_group smmu_pmu_identifier_group = {
0599 .attrs = smmu_pmu_identifier_attrs,
0600 .is_visible = smmu_pmu_identifier_attr_visible,
0601 };
0602
0603
0604 PMU_FORMAT_ATTR(event, "config:0-15");
0605 PMU_FORMAT_ATTR(filter_stream_id, "config1:0-31");
0606 PMU_FORMAT_ATTR(filter_span, "config1:32");
0607 PMU_FORMAT_ATTR(filter_enable, "config1:33");
0608
0609 static struct attribute *smmu_pmu_formats[] = {
0610 &format_attr_event.attr,
0611 &format_attr_filter_stream_id.attr,
0612 &format_attr_filter_span.attr,
0613 &format_attr_filter_enable.attr,
0614 NULL
0615 };
0616
0617 static const struct attribute_group smmu_pmu_format_group = {
0618 .name = "format",
0619 .attrs = smmu_pmu_formats,
0620 };
0621
0622 static const struct attribute_group *smmu_pmu_attr_grps[] = {
0623 &smmu_pmu_cpumask_group,
0624 &smmu_pmu_events_group,
0625 &smmu_pmu_format_group,
0626 &smmu_pmu_identifier_group,
0627 NULL
0628 };
0629
0630
0631
0632
0633
0634 static int smmu_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
0635 {
0636 struct smmu_pmu *smmu_pmu;
0637 unsigned int target;
0638
0639 smmu_pmu = hlist_entry_safe(node, struct smmu_pmu, node);
0640 if (cpu != smmu_pmu->on_cpu)
0641 return 0;
0642
0643 target = cpumask_any_but(cpu_online_mask, cpu);
0644 if (target >= nr_cpu_ids)
0645 return 0;
0646
0647 perf_pmu_migrate_context(&smmu_pmu->pmu, cpu, target);
0648 smmu_pmu->on_cpu = target;
0649 WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(target)));
0650
0651 return 0;
0652 }
0653
0654 static irqreturn_t smmu_pmu_handle_irq(int irq_num, void *data)
0655 {
0656 struct smmu_pmu *smmu_pmu = data;
0657 DECLARE_BITMAP(ovs, BITS_PER_TYPE(u64));
0658 u64 ovsr;
0659 unsigned int idx;
0660
0661 ovsr = readq(smmu_pmu->reloc_base + SMMU_PMCG_OVSSET0);
0662 if (!ovsr)
0663 return IRQ_NONE;
0664
0665 writeq(ovsr, smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
0666
0667 bitmap_from_u64(ovs, ovsr);
0668 for_each_set_bit(idx, ovs, smmu_pmu->num_counters) {
0669 struct perf_event *event = smmu_pmu->events[idx];
0670 struct hw_perf_event *hwc;
0671
0672 if (WARN_ON_ONCE(!event))
0673 continue;
0674
0675 smmu_pmu_event_update(event);
0676 hwc = &event->hw;
0677
0678 smmu_pmu_set_period(smmu_pmu, hwc);
0679 }
0680
0681 return IRQ_HANDLED;
0682 }
0683
0684 static void smmu_pmu_free_msis(void *data)
0685 {
0686 struct device *dev = data;
0687
0688 platform_msi_domain_free_irqs(dev);
0689 }
0690
0691 static void smmu_pmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
0692 {
0693 phys_addr_t doorbell;
0694 struct device *dev = msi_desc_to_dev(desc);
0695 struct smmu_pmu *pmu = dev_get_drvdata(dev);
0696
0697 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
0698 doorbell &= MSI_CFG0_ADDR_MASK;
0699
0700 writeq_relaxed(doorbell, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
0701 writel_relaxed(msg->data, pmu->reg_base + SMMU_PMCG_IRQ_CFG1);
0702 writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE,
0703 pmu->reg_base + SMMU_PMCG_IRQ_CFG2);
0704 }
0705
0706 static void smmu_pmu_setup_msi(struct smmu_pmu *pmu)
0707 {
0708 struct device *dev = pmu->dev;
0709 int ret;
0710
0711
0712 writeq_relaxed(0, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
0713
0714
0715 if (!(readl(pmu->reg_base + SMMU_PMCG_CFGR) & SMMU_PMCG_CFGR_MSI))
0716 return;
0717
0718 ret = platform_msi_domain_alloc_irqs(dev, 1, smmu_pmu_write_msi_msg);
0719 if (ret) {
0720 dev_warn(dev, "failed to allocate MSIs\n");
0721 return;
0722 }
0723
0724 pmu->irq = msi_get_virq(dev, 0);
0725
0726
0727 devm_add_action(dev, smmu_pmu_free_msis, dev);
0728 }
0729
0730 static int smmu_pmu_setup_irq(struct smmu_pmu *pmu)
0731 {
0732 unsigned long flags = IRQF_NOBALANCING | IRQF_SHARED | IRQF_NO_THREAD;
0733 int irq, ret = -ENXIO;
0734
0735 smmu_pmu_setup_msi(pmu);
0736
0737 irq = pmu->irq;
0738 if (irq)
0739 ret = devm_request_irq(pmu->dev, irq, smmu_pmu_handle_irq,
0740 flags, "smmuv3-pmu", pmu);
0741 return ret;
0742 }
0743
0744 static void smmu_pmu_reset(struct smmu_pmu *smmu_pmu)
0745 {
0746 u64 counter_present_mask = GENMASK_ULL(smmu_pmu->num_counters - 1, 0);
0747
0748 smmu_pmu_disable(&smmu_pmu->pmu);
0749
0750
0751 writeq_relaxed(counter_present_mask,
0752 smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
0753 writeq_relaxed(counter_present_mask,
0754 smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
0755 writeq_relaxed(counter_present_mask,
0756 smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
0757 }
0758
0759 static void smmu_pmu_get_acpi_options(struct smmu_pmu *smmu_pmu)
0760 {
0761 u32 model;
0762
0763 model = *(u32 *)dev_get_platdata(smmu_pmu->dev);
0764
0765 switch (model) {
0766 case IORT_SMMU_V3_PMCG_HISI_HIP08:
0767
0768 smmu_pmu->options |= SMMU_PMCG_EVCNTR_RDONLY;
0769 break;
0770 }
0771
0772 dev_notice(smmu_pmu->dev, "option mask 0x%x\n", smmu_pmu->options);
0773 }
0774
0775 static bool smmu_pmu_coresight_id_regs(struct smmu_pmu *smmu_pmu)
0776 {
0777 return of_device_is_compatible(smmu_pmu->dev->of_node,
0778 "arm,mmu-600-pmcg");
0779 }
0780
0781 static void smmu_pmu_get_iidr(struct smmu_pmu *smmu_pmu)
0782 {
0783 u32 iidr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_IIDR);
0784
0785 if (!iidr && smmu_pmu_coresight_id_regs(smmu_pmu)) {
0786 u32 pidr0 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR0);
0787 u32 pidr1 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR1);
0788 u32 pidr2 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR2);
0789 u32 pidr3 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR3);
0790 u32 pidr4 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR4);
0791
0792 u32 productid = FIELD_GET(SMMU_PMCG_PIDR0_PART_0, pidr0) |
0793 (FIELD_GET(SMMU_PMCG_PIDR1_PART_1, pidr1) << 8);
0794 u32 variant = FIELD_GET(SMMU_PMCG_PIDR2_REVISION, pidr2);
0795 u32 revision = FIELD_GET(SMMU_PMCG_PIDR3_REVAND, pidr3);
0796 u32 implementer =
0797 FIELD_GET(SMMU_PMCG_PIDR1_DES_0, pidr1) |
0798 (FIELD_GET(SMMU_PMCG_PIDR2_DES_1, pidr2) << 4) |
0799 (FIELD_GET(SMMU_PMCG_PIDR4_DES_2, pidr4) << 8);
0800
0801 iidr = FIELD_PREP(SMMU_PMCG_IIDR_PRODUCTID, productid) |
0802 FIELD_PREP(SMMU_PMCG_IIDR_VARIANT, variant) |
0803 FIELD_PREP(SMMU_PMCG_IIDR_REVISION, revision) |
0804 FIELD_PREP(SMMU_PMCG_IIDR_IMPLEMENTER, implementer);
0805 }
0806
0807 smmu_pmu->iidr = iidr;
0808 }
0809
0810 static int smmu_pmu_probe(struct platform_device *pdev)
0811 {
0812 struct smmu_pmu *smmu_pmu;
0813 struct resource *res_0;
0814 u32 cfgr, reg_size;
0815 u64 ceid_64[2];
0816 int irq, err;
0817 char *name;
0818 struct device *dev = &pdev->dev;
0819
0820 smmu_pmu = devm_kzalloc(dev, sizeof(*smmu_pmu), GFP_KERNEL);
0821 if (!smmu_pmu)
0822 return -ENOMEM;
0823
0824 smmu_pmu->dev = dev;
0825 platform_set_drvdata(pdev, smmu_pmu);
0826
0827 smmu_pmu->pmu = (struct pmu) {
0828 .module = THIS_MODULE,
0829 .task_ctx_nr = perf_invalid_context,
0830 .pmu_enable = smmu_pmu_enable,
0831 .pmu_disable = smmu_pmu_disable,
0832 .event_init = smmu_pmu_event_init,
0833 .add = smmu_pmu_event_add,
0834 .del = smmu_pmu_event_del,
0835 .start = smmu_pmu_event_start,
0836 .stop = smmu_pmu_event_stop,
0837 .read = smmu_pmu_event_read,
0838 .attr_groups = smmu_pmu_attr_grps,
0839 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
0840 };
0841
0842 smmu_pmu->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res_0);
0843 if (IS_ERR(smmu_pmu->reg_base))
0844 return PTR_ERR(smmu_pmu->reg_base);
0845
0846 cfgr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CFGR);
0847
0848
0849 if (cfgr & SMMU_PMCG_CFGR_RELOC_CTRS) {
0850 smmu_pmu->reloc_base = devm_platform_ioremap_resource(pdev, 1);
0851 if (IS_ERR(smmu_pmu->reloc_base))
0852 return PTR_ERR(smmu_pmu->reloc_base);
0853 } else {
0854 smmu_pmu->reloc_base = smmu_pmu->reg_base;
0855 }
0856
0857 irq = platform_get_irq_optional(pdev, 0);
0858 if (irq > 0)
0859 smmu_pmu->irq = irq;
0860
0861 ceid_64[0] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID0);
0862 ceid_64[1] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID1);
0863 bitmap_from_arr32(smmu_pmu->supported_events, (u32 *)ceid_64,
0864 SMMU_PMCG_ARCH_MAX_EVENTS);
0865
0866 smmu_pmu->num_counters = FIELD_GET(SMMU_PMCG_CFGR_NCTR, cfgr) + 1;
0867
0868 smmu_pmu->global_filter = !!(cfgr & SMMU_PMCG_CFGR_SID_FILTER_TYPE);
0869
0870 reg_size = FIELD_GET(SMMU_PMCG_CFGR_SIZE, cfgr);
0871 smmu_pmu->counter_mask = GENMASK_ULL(reg_size, 0);
0872
0873 smmu_pmu_reset(smmu_pmu);
0874
0875 err = smmu_pmu_setup_irq(smmu_pmu);
0876 if (err) {
0877 dev_err(dev, "Setup irq failed, PMU @%pa\n", &res_0->start);
0878 return err;
0879 }
0880
0881 smmu_pmu_get_iidr(smmu_pmu);
0882
0883 name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "smmuv3_pmcg_%llx",
0884 (res_0->start) >> SMMU_PMCG_PA_SHIFT);
0885 if (!name) {
0886 dev_err(dev, "Create name failed, PMU @%pa\n", &res_0->start);
0887 return -EINVAL;
0888 }
0889
0890 if (!dev->of_node)
0891 smmu_pmu_get_acpi_options(smmu_pmu);
0892
0893
0894 smmu_pmu->on_cpu = raw_smp_processor_id();
0895 WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(smmu_pmu->on_cpu)));
0896
0897 err = cpuhp_state_add_instance_nocalls(cpuhp_state_num,
0898 &smmu_pmu->node);
0899 if (err) {
0900 dev_err(dev, "Error %d registering hotplug, PMU @%pa\n",
0901 err, &res_0->start);
0902 return err;
0903 }
0904
0905 err = perf_pmu_register(&smmu_pmu->pmu, name, -1);
0906 if (err) {
0907 dev_err(dev, "Error %d registering PMU @%pa\n",
0908 err, &res_0->start);
0909 goto out_unregister;
0910 }
0911
0912 dev_info(dev, "Registered PMU @ %pa using %d counters with %s filter settings\n",
0913 &res_0->start, smmu_pmu->num_counters,
0914 smmu_pmu->global_filter ? "Global(Counter0)" :
0915 "Individual");
0916
0917 return 0;
0918
0919 out_unregister:
0920 cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
0921 return err;
0922 }
0923
0924 static int smmu_pmu_remove(struct platform_device *pdev)
0925 {
0926 struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
0927
0928 perf_pmu_unregister(&smmu_pmu->pmu);
0929 cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
0930
0931 return 0;
0932 }
0933
0934 static void smmu_pmu_shutdown(struct platform_device *pdev)
0935 {
0936 struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
0937
0938 smmu_pmu_disable(&smmu_pmu->pmu);
0939 }
0940
0941 #ifdef CONFIG_OF
0942 static const struct of_device_id smmu_pmu_of_match[] = {
0943 { .compatible = "arm,smmu-v3-pmcg" },
0944 {}
0945 };
0946 MODULE_DEVICE_TABLE(of, smmu_pmu_of_match);
0947 #endif
0948
0949 static struct platform_driver smmu_pmu_driver = {
0950 .driver = {
0951 .name = "arm-smmu-v3-pmcg",
0952 .of_match_table = of_match_ptr(smmu_pmu_of_match),
0953 .suppress_bind_attrs = true,
0954 },
0955 .probe = smmu_pmu_probe,
0956 .remove = smmu_pmu_remove,
0957 .shutdown = smmu_pmu_shutdown,
0958 };
0959
0960 static int __init arm_smmu_pmu_init(void)
0961 {
0962 cpuhp_state_num = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
0963 "perf/arm/pmcg:online",
0964 NULL,
0965 smmu_pmu_offline_cpu);
0966 if (cpuhp_state_num < 0)
0967 return cpuhp_state_num;
0968
0969 return platform_driver_register(&smmu_pmu_driver);
0970 }
0971 module_init(arm_smmu_pmu_init);
0972
0973 static void __exit arm_smmu_pmu_exit(void)
0974 {
0975 platform_driver_unregister(&smmu_pmu_driver);
0976 cpuhp_remove_multi_state(cpuhp_state_num);
0977 }
0978
0979 module_exit(arm_smmu_pmu_exit);
0980
0981 MODULE_DESCRIPTION("PMU driver for ARM SMMUv3 Performance Monitors Extension");
0982 MODULE_AUTHOR("Neil Leeder <nleeder@codeaurora.org>");
0983 MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>");
0984 MODULE_LICENSE("GPL v2");