Neil Leeder | 7d839b4 | 2019-03-26 15:17:51 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * This driver adds support for perf events to use the Performance |
| 5 | * Monitor Counter Groups (PMCG) associated with an SMMUv3 node |
| 6 | * to monitor that node. |
| 7 | * |
| 8 | * SMMUv3 PMCG devices are named as smmuv3_pmcg_<phys_addr_page> where |
| 9 | * <phys_addr_page> is the physical page address of the SMMU PMCG wrapped |
| 10 | * to 4K boundary. For example, the PMCG at 0xff88840000 is named |
| 11 | * smmuv3_pmcg_ff88840 |
| 12 | * |
| 13 | * Filtering by stream id is done by specifying filtering parameters |
| 14 | * with the event. options are: |
| 15 | * filter_enable - 0 = no filtering, 1 = filtering enabled |
| 16 | * filter_span - 0 = exact match, 1 = pattern match |
| 17 | * filter_stream_id - pattern to filter against |
| 18 | * |
| 19 | * To match a partial StreamID where the X most-significant bits must match |
| 20 | * but the Y least-significant bits might differ, STREAMID is programmed |
| 21 | * with a value that contains: |
| 22 | * STREAMID[Y - 1] == 0. |
| 23 | * STREAMID[Y - 2:0] == 1 (where Y > 1). |
| 24 | * The remainder of implemented bits of STREAMID (X bits, from bit Y upwards) |
| 25 | * contain a value to match from the corresponding bits of event StreamID. |
| 26 | * |
| 27 | * Example: perf stat -e smmuv3_pmcg_ff88840/transaction,filter_enable=1, |
| 28 | * filter_span=1,filter_stream_id=0x42/ -a netperf |
| 29 | * Applies filter pattern 0x42 to transaction events, which means events |
| 30 | * matching stream ids 0x42 and 0x43 are counted. Further filtering |
| 31 | * information is available in the SMMU documentation. |
| 32 | * |
| 33 | * SMMU events are not attributable to a CPU, so task mode and sampling |
| 34 | * are not supported. |
| 35 | */ |
| 36 | |
| 37 | #include <linux/acpi.h> |
| 38 | #include <linux/bitfield.h> |
| 39 | #include <linux/bitops.h> |
| 40 | #include <linux/cpuhotplug.h> |
| 41 | #include <linux/cpumask.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/errno.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/irq.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/list.h> |
| 48 | #include <linux/msi.h> |
| 49 | #include <linux/perf_event.h> |
| 50 | #include <linux/platform_device.h> |
| 51 | #include <linux/smp.h> |
| 52 | #include <linux/sysfs.h> |
| 53 | #include <linux/types.h> |
| 54 | |
| 55 | #define SMMU_PMCG_EVCNTR0 0x0 |
| 56 | #define SMMU_PMCG_EVCNTR(n, stride) (SMMU_PMCG_EVCNTR0 + (n) * (stride)) |
| 57 | #define SMMU_PMCG_EVTYPER0 0x400 |
| 58 | #define SMMU_PMCG_EVTYPER(n) (SMMU_PMCG_EVTYPER0 + (n) * 4) |
| 59 | #define SMMU_PMCG_SID_SPAN_SHIFT 29 |
| 60 | #define SMMU_PMCG_SMR0 0xA00 |
| 61 | #define SMMU_PMCG_SMR(n) (SMMU_PMCG_SMR0 + (n) * 4) |
| 62 | #define SMMU_PMCG_CNTENSET0 0xC00 |
| 63 | #define SMMU_PMCG_CNTENCLR0 0xC20 |
| 64 | #define SMMU_PMCG_INTENSET0 0xC40 |
| 65 | #define SMMU_PMCG_INTENCLR0 0xC60 |
| 66 | #define SMMU_PMCG_OVSCLR0 0xC80 |
| 67 | #define SMMU_PMCG_OVSSET0 0xCC0 |
| 68 | #define SMMU_PMCG_CFGR 0xE00 |
| 69 | #define SMMU_PMCG_CFGR_SID_FILTER_TYPE BIT(23) |
Shameer Kolothum | f202cda | 2019-03-26 15:17:52 +0000 | [diff] [blame^] | 70 | #define SMMU_PMCG_CFGR_MSI BIT(21) |
Neil Leeder | 7d839b4 | 2019-03-26 15:17:51 +0000 | [diff] [blame] | 71 | #define SMMU_PMCG_CFGR_RELOC_CTRS BIT(20) |
| 72 | #define SMMU_PMCG_CFGR_SIZE GENMASK(13, 8) |
| 73 | #define SMMU_PMCG_CFGR_NCTR GENMASK(5, 0) |
| 74 | #define SMMU_PMCG_CR 0xE04 |
| 75 | #define SMMU_PMCG_CR_ENABLE BIT(0) |
| 76 | #define SMMU_PMCG_CEID0 0xE20 |
| 77 | #define SMMU_PMCG_CEID1 0xE28 |
| 78 | #define SMMU_PMCG_IRQ_CTRL 0xE50 |
| 79 | #define SMMU_PMCG_IRQ_CTRL_IRQEN BIT(0) |
| 80 | #define SMMU_PMCG_IRQ_CFG0 0xE58 |
Shameer Kolothum | f202cda | 2019-03-26 15:17:52 +0000 | [diff] [blame^] | 81 | #define SMMU_PMCG_IRQ_CFG1 0xE60 |
| 82 | #define SMMU_PMCG_IRQ_CFG2 0xE64 |
| 83 | |
| 84 | /* MSI config fields */ |
| 85 | #define MSI_CFG0_ADDR_MASK GENMASK_ULL(51, 2) |
| 86 | #define MSI_CFG2_MEMATTR_DEVICE_nGnRE 0x1 |
Neil Leeder | 7d839b4 | 2019-03-26 15:17:51 +0000 | [diff] [blame] | 87 | |
| 88 | #define SMMU_PMCG_DEFAULT_FILTER_SPAN 1 |
| 89 | #define SMMU_PMCG_DEFAULT_FILTER_SID GENMASK(31, 0) |
| 90 | |
| 91 | #define SMMU_PMCG_MAX_COUNTERS 64 |
| 92 | #define SMMU_PMCG_ARCH_MAX_EVENTS 128 |
| 93 | |
| 94 | #define SMMU_PMCG_PA_SHIFT 12 |
| 95 | |
| 96 | static int cpuhp_state_num; |
| 97 | |
| 98 | struct smmu_pmu { |
| 99 | struct hlist_node node; |
| 100 | struct perf_event *events[SMMU_PMCG_MAX_COUNTERS]; |
| 101 | DECLARE_BITMAP(used_counters, SMMU_PMCG_MAX_COUNTERS); |
| 102 | DECLARE_BITMAP(supported_events, SMMU_PMCG_ARCH_MAX_EVENTS); |
| 103 | unsigned int irq; |
| 104 | unsigned int on_cpu; |
| 105 | struct pmu pmu; |
| 106 | unsigned int num_counters; |
| 107 | struct device *dev; |
| 108 | void __iomem *reg_base; |
| 109 | void __iomem *reloc_base; |
| 110 | u64 counter_mask; |
| 111 | bool global_filter; |
| 112 | u32 global_filter_span; |
| 113 | u32 global_filter_sid; |
| 114 | }; |
| 115 | |
| 116 | #define to_smmu_pmu(p) (container_of(p, struct smmu_pmu, pmu)) |
| 117 | |
| 118 | #define SMMU_PMU_EVENT_ATTR_EXTRACTOR(_name, _config, _start, _end) \ |
| 119 | static inline u32 get_##_name(struct perf_event *event) \ |
| 120 | { \ |
| 121 | return FIELD_GET(GENMASK_ULL(_end, _start), \ |
| 122 | event->attr._config); \ |
| 123 | } \ |
| 124 | |
| 125 | SMMU_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 15); |
| 126 | SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_stream_id, config1, 0, 31); |
| 127 | SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_span, config1, 32, 32); |
| 128 | SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_enable, config1, 33, 33); |
| 129 | |
| 130 | static inline void smmu_pmu_enable(struct pmu *pmu) |
| 131 | { |
| 132 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu); |
| 133 | |
| 134 | writel(SMMU_PMCG_IRQ_CTRL_IRQEN, |
| 135 | smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL); |
| 136 | writel(SMMU_PMCG_CR_ENABLE, smmu_pmu->reg_base + SMMU_PMCG_CR); |
| 137 | } |
| 138 | |
| 139 | static inline void smmu_pmu_disable(struct pmu *pmu) |
| 140 | { |
| 141 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu); |
| 142 | |
| 143 | writel(0, smmu_pmu->reg_base + SMMU_PMCG_CR); |
| 144 | writel(0, smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL); |
| 145 | } |
| 146 | |
| 147 | static inline void smmu_pmu_counter_set_value(struct smmu_pmu *smmu_pmu, |
| 148 | u32 idx, u64 value) |
| 149 | { |
| 150 | if (smmu_pmu->counter_mask & BIT(32)) |
| 151 | writeq(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8)); |
| 152 | else |
| 153 | writel(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4)); |
| 154 | } |
| 155 | |
| 156 | static inline u64 smmu_pmu_counter_get_value(struct smmu_pmu *smmu_pmu, u32 idx) |
| 157 | { |
| 158 | u64 value; |
| 159 | |
| 160 | if (smmu_pmu->counter_mask & BIT(32)) |
| 161 | value = readq(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8)); |
| 162 | else |
| 163 | value = readl(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4)); |
| 164 | |
| 165 | return value; |
| 166 | } |
| 167 | |
| 168 | static inline void smmu_pmu_counter_enable(struct smmu_pmu *smmu_pmu, u32 idx) |
| 169 | { |
| 170 | writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENSET0); |
| 171 | } |
| 172 | |
| 173 | static inline void smmu_pmu_counter_disable(struct smmu_pmu *smmu_pmu, u32 idx) |
| 174 | { |
| 175 | writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0); |
| 176 | } |
| 177 | |
| 178 | static inline void smmu_pmu_interrupt_enable(struct smmu_pmu *smmu_pmu, u32 idx) |
| 179 | { |
| 180 | writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENSET0); |
| 181 | } |
| 182 | |
| 183 | static inline void smmu_pmu_interrupt_disable(struct smmu_pmu *smmu_pmu, |
| 184 | u32 idx) |
| 185 | { |
| 186 | writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0); |
| 187 | } |
| 188 | |
| 189 | static inline void smmu_pmu_set_evtyper(struct smmu_pmu *smmu_pmu, u32 idx, |
| 190 | u32 val) |
| 191 | { |
| 192 | writel(val, smmu_pmu->reg_base + SMMU_PMCG_EVTYPER(idx)); |
| 193 | } |
| 194 | |
| 195 | static inline void smmu_pmu_set_smr(struct smmu_pmu *smmu_pmu, u32 idx, u32 val) |
| 196 | { |
| 197 | writel(val, smmu_pmu->reg_base + SMMU_PMCG_SMR(idx)); |
| 198 | } |
| 199 | |
| 200 | static void smmu_pmu_event_update(struct perf_event *event) |
| 201 | { |
| 202 | struct hw_perf_event *hwc = &event->hw; |
| 203 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 204 | u64 delta, prev, now; |
| 205 | u32 idx = hwc->idx; |
| 206 | |
| 207 | do { |
| 208 | prev = local64_read(&hwc->prev_count); |
| 209 | now = smmu_pmu_counter_get_value(smmu_pmu, idx); |
| 210 | } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev); |
| 211 | |
| 212 | /* handle overflow. */ |
| 213 | delta = now - prev; |
| 214 | delta &= smmu_pmu->counter_mask; |
| 215 | |
| 216 | local64_add(delta, &event->count); |
| 217 | } |
| 218 | |
| 219 | static void smmu_pmu_set_period(struct smmu_pmu *smmu_pmu, |
| 220 | struct hw_perf_event *hwc) |
| 221 | { |
| 222 | u32 idx = hwc->idx; |
| 223 | u64 new; |
| 224 | |
| 225 | /* |
| 226 | * We limit the max period to half the max counter value of the counter |
| 227 | * size, so that even in the case of extreme interrupt latency the |
| 228 | * counter will (hopefully) not wrap past its initial value. |
| 229 | */ |
| 230 | new = smmu_pmu->counter_mask >> 1; |
| 231 | |
| 232 | local64_set(&hwc->prev_count, new); |
| 233 | smmu_pmu_counter_set_value(smmu_pmu, idx, new); |
| 234 | } |
| 235 | |
| 236 | static void smmu_pmu_set_event_filter(struct perf_event *event, |
| 237 | int idx, u32 span, u32 sid) |
| 238 | { |
| 239 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 240 | u32 evtyper; |
| 241 | |
| 242 | evtyper = get_event(event) | span << SMMU_PMCG_SID_SPAN_SHIFT; |
| 243 | smmu_pmu_set_evtyper(smmu_pmu, idx, evtyper); |
| 244 | smmu_pmu_set_smr(smmu_pmu, idx, sid); |
| 245 | } |
| 246 | |
| 247 | static int smmu_pmu_apply_event_filter(struct smmu_pmu *smmu_pmu, |
| 248 | struct perf_event *event, int idx) |
| 249 | { |
| 250 | u32 span, sid; |
| 251 | unsigned int num_ctrs = smmu_pmu->num_counters; |
| 252 | bool filter_en = !!get_filter_enable(event); |
| 253 | |
| 254 | span = filter_en ? get_filter_span(event) : |
| 255 | SMMU_PMCG_DEFAULT_FILTER_SPAN; |
| 256 | sid = filter_en ? get_filter_stream_id(event) : |
| 257 | SMMU_PMCG_DEFAULT_FILTER_SID; |
| 258 | |
| 259 | /* Support individual filter settings */ |
| 260 | if (!smmu_pmu->global_filter) { |
| 261 | smmu_pmu_set_event_filter(event, idx, span, sid); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* Requested settings same as current global settings*/ |
| 266 | if (span == smmu_pmu->global_filter_span && |
| 267 | sid == smmu_pmu->global_filter_sid) |
| 268 | return 0; |
| 269 | |
| 270 | if (!bitmap_empty(smmu_pmu->used_counters, num_ctrs)) |
| 271 | return -EAGAIN; |
| 272 | |
| 273 | smmu_pmu_set_event_filter(event, 0, span, sid); |
| 274 | smmu_pmu->global_filter_span = span; |
| 275 | smmu_pmu->global_filter_sid = sid; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int smmu_pmu_get_event_idx(struct smmu_pmu *smmu_pmu, |
| 280 | struct perf_event *event) |
| 281 | { |
| 282 | int idx, err; |
| 283 | unsigned int num_ctrs = smmu_pmu->num_counters; |
| 284 | |
| 285 | idx = find_first_zero_bit(smmu_pmu->used_counters, num_ctrs); |
| 286 | if (idx == num_ctrs) |
| 287 | /* The counters are all in use. */ |
| 288 | return -EAGAIN; |
| 289 | |
| 290 | err = smmu_pmu_apply_event_filter(smmu_pmu, event, idx); |
| 291 | if (err) |
| 292 | return err; |
| 293 | |
| 294 | set_bit(idx, smmu_pmu->used_counters); |
| 295 | |
| 296 | return idx; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Implementation of abstract pmu functionality required by |
| 301 | * the core perf events code. |
| 302 | */ |
| 303 | |
| 304 | static int smmu_pmu_event_init(struct perf_event *event) |
| 305 | { |
| 306 | struct hw_perf_event *hwc = &event->hw; |
| 307 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 308 | struct device *dev = smmu_pmu->dev; |
| 309 | struct perf_event *sibling; |
| 310 | u16 event_id; |
| 311 | |
| 312 | if (event->attr.type != event->pmu->type) |
| 313 | return -ENOENT; |
| 314 | |
| 315 | if (hwc->sample_period) { |
| 316 | dev_dbg(dev, "Sampling not supported\n"); |
| 317 | return -EOPNOTSUPP; |
| 318 | } |
| 319 | |
| 320 | if (event->cpu < 0) { |
| 321 | dev_dbg(dev, "Per-task mode not supported\n"); |
| 322 | return -EOPNOTSUPP; |
| 323 | } |
| 324 | |
| 325 | /* Verify specified event is supported on this PMU */ |
| 326 | event_id = get_event(event); |
| 327 | if (event_id < SMMU_PMCG_ARCH_MAX_EVENTS && |
| 328 | (!test_bit(event_id, smmu_pmu->supported_events))) { |
| 329 | dev_dbg(dev, "Invalid event %d for this PMU\n", event_id); |
| 330 | return -EINVAL; |
| 331 | } |
| 332 | |
| 333 | /* Don't allow groups with mixed PMUs, except for s/w events */ |
| 334 | if (event->group_leader->pmu != event->pmu && |
| 335 | !is_software_event(event->group_leader)) { |
| 336 | dev_dbg(dev, "Can't create mixed PMU group\n"); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | for_each_sibling_event(sibling, event->group_leader) { |
| 341 | if (sibling->pmu != event->pmu && |
| 342 | !is_software_event(sibling)) { |
| 343 | dev_dbg(dev, "Can't create mixed PMU group\n"); |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | hwc->idx = -1; |
| 349 | |
| 350 | /* |
| 351 | * Ensure all events are on the same cpu so all events are in the |
| 352 | * same cpu context, to avoid races on pmu_enable etc. |
| 353 | */ |
| 354 | event->cpu = smmu_pmu->on_cpu; |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static void smmu_pmu_event_start(struct perf_event *event, int flags) |
| 360 | { |
| 361 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 362 | struct hw_perf_event *hwc = &event->hw; |
| 363 | int idx = hwc->idx; |
| 364 | |
| 365 | hwc->state = 0; |
| 366 | |
| 367 | smmu_pmu_set_period(smmu_pmu, hwc); |
| 368 | |
| 369 | smmu_pmu_counter_enable(smmu_pmu, idx); |
| 370 | } |
| 371 | |
| 372 | static void smmu_pmu_event_stop(struct perf_event *event, int flags) |
| 373 | { |
| 374 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 375 | struct hw_perf_event *hwc = &event->hw; |
| 376 | int idx = hwc->idx; |
| 377 | |
| 378 | if (hwc->state & PERF_HES_STOPPED) |
| 379 | return; |
| 380 | |
| 381 | smmu_pmu_counter_disable(smmu_pmu, idx); |
| 382 | /* As the counter gets updated on _start, ignore PERF_EF_UPDATE */ |
| 383 | smmu_pmu_event_update(event); |
| 384 | hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 385 | } |
| 386 | |
| 387 | static int smmu_pmu_event_add(struct perf_event *event, int flags) |
| 388 | { |
| 389 | struct hw_perf_event *hwc = &event->hw; |
| 390 | int idx; |
| 391 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 392 | |
| 393 | idx = smmu_pmu_get_event_idx(smmu_pmu, event); |
| 394 | if (idx < 0) |
| 395 | return idx; |
| 396 | |
| 397 | hwc->idx = idx; |
| 398 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 399 | smmu_pmu->events[idx] = event; |
| 400 | local64_set(&hwc->prev_count, 0); |
| 401 | |
| 402 | smmu_pmu_interrupt_enable(smmu_pmu, idx); |
| 403 | |
| 404 | if (flags & PERF_EF_START) |
| 405 | smmu_pmu_event_start(event, flags); |
| 406 | |
| 407 | /* Propagate changes to the userspace mapping. */ |
| 408 | perf_event_update_userpage(event); |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void smmu_pmu_event_del(struct perf_event *event, int flags) |
| 414 | { |
| 415 | struct hw_perf_event *hwc = &event->hw; |
| 416 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu); |
| 417 | int idx = hwc->idx; |
| 418 | |
| 419 | smmu_pmu_event_stop(event, flags | PERF_EF_UPDATE); |
| 420 | smmu_pmu_interrupt_disable(smmu_pmu, idx); |
| 421 | smmu_pmu->events[idx] = NULL; |
| 422 | clear_bit(idx, smmu_pmu->used_counters); |
| 423 | |
| 424 | perf_event_update_userpage(event); |
| 425 | } |
| 426 | |
| 427 | static void smmu_pmu_event_read(struct perf_event *event) |
| 428 | { |
| 429 | smmu_pmu_event_update(event); |
| 430 | } |
| 431 | |
| 432 | /* cpumask */ |
| 433 | |
| 434 | static ssize_t smmu_pmu_cpumask_show(struct device *dev, |
| 435 | struct device_attribute *attr, |
| 436 | char *buf) |
| 437 | { |
| 438 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev)); |
| 439 | |
| 440 | return cpumap_print_to_pagebuf(true, buf, cpumask_of(smmu_pmu->on_cpu)); |
| 441 | } |
| 442 | |
| 443 | static struct device_attribute smmu_pmu_cpumask_attr = |
| 444 | __ATTR(cpumask, 0444, smmu_pmu_cpumask_show, NULL); |
| 445 | |
| 446 | static struct attribute *smmu_pmu_cpumask_attrs[] = { |
| 447 | &smmu_pmu_cpumask_attr.attr, |
| 448 | NULL |
| 449 | }; |
| 450 | |
| 451 | static struct attribute_group smmu_pmu_cpumask_group = { |
| 452 | .attrs = smmu_pmu_cpumask_attrs, |
| 453 | }; |
| 454 | |
| 455 | /* Events */ |
| 456 | |
| 457 | static ssize_t smmu_pmu_event_show(struct device *dev, |
| 458 | struct device_attribute *attr, char *page) |
| 459 | { |
| 460 | struct perf_pmu_events_attr *pmu_attr; |
| 461 | |
| 462 | pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr); |
| 463 | |
| 464 | return sprintf(page, "event=0x%02llx\n", pmu_attr->id); |
| 465 | } |
| 466 | |
| 467 | #define SMMU_EVENT_ATTR(name, config) \ |
| 468 | PMU_EVENT_ATTR(name, smmu_event_attr_##name, \ |
| 469 | config, smmu_pmu_event_show) |
| 470 | SMMU_EVENT_ATTR(cycles, 0); |
| 471 | SMMU_EVENT_ATTR(transaction, 1); |
| 472 | SMMU_EVENT_ATTR(tlb_miss, 2); |
| 473 | SMMU_EVENT_ATTR(config_cache_miss, 3); |
| 474 | SMMU_EVENT_ATTR(trans_table_walk_access, 4); |
| 475 | SMMU_EVENT_ATTR(config_struct_access, 5); |
| 476 | SMMU_EVENT_ATTR(pcie_ats_trans_rq, 6); |
| 477 | SMMU_EVENT_ATTR(pcie_ats_trans_passed, 7); |
| 478 | |
| 479 | static struct attribute *smmu_pmu_events[] = { |
| 480 | &smmu_event_attr_cycles.attr.attr, |
| 481 | &smmu_event_attr_transaction.attr.attr, |
| 482 | &smmu_event_attr_tlb_miss.attr.attr, |
| 483 | &smmu_event_attr_config_cache_miss.attr.attr, |
| 484 | &smmu_event_attr_trans_table_walk_access.attr.attr, |
| 485 | &smmu_event_attr_config_struct_access.attr.attr, |
| 486 | &smmu_event_attr_pcie_ats_trans_rq.attr.attr, |
| 487 | &smmu_event_attr_pcie_ats_trans_passed.attr.attr, |
| 488 | NULL |
| 489 | }; |
| 490 | |
| 491 | static umode_t smmu_pmu_event_is_visible(struct kobject *kobj, |
| 492 | struct attribute *attr, int unused) |
| 493 | { |
| 494 | struct device *dev = kobj_to_dev(kobj); |
| 495 | struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev)); |
| 496 | struct perf_pmu_events_attr *pmu_attr; |
| 497 | |
| 498 | pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr); |
| 499 | |
| 500 | if (test_bit(pmu_attr->id, smmu_pmu->supported_events)) |
| 501 | return attr->mode; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static struct attribute_group smmu_pmu_events_group = { |
| 507 | .name = "events", |
| 508 | .attrs = smmu_pmu_events, |
| 509 | .is_visible = smmu_pmu_event_is_visible, |
| 510 | }; |
| 511 | |
| 512 | /* Formats */ |
| 513 | PMU_FORMAT_ATTR(event, "config:0-15"); |
| 514 | PMU_FORMAT_ATTR(filter_stream_id, "config1:0-31"); |
| 515 | PMU_FORMAT_ATTR(filter_span, "config1:32"); |
| 516 | PMU_FORMAT_ATTR(filter_enable, "config1:33"); |
| 517 | |
| 518 | static struct attribute *smmu_pmu_formats[] = { |
| 519 | &format_attr_event.attr, |
| 520 | &format_attr_filter_stream_id.attr, |
| 521 | &format_attr_filter_span.attr, |
| 522 | &format_attr_filter_enable.attr, |
| 523 | NULL |
| 524 | }; |
| 525 | |
| 526 | static struct attribute_group smmu_pmu_format_group = { |
| 527 | .name = "format", |
| 528 | .attrs = smmu_pmu_formats, |
| 529 | }; |
| 530 | |
| 531 | static const struct attribute_group *smmu_pmu_attr_grps[] = { |
| 532 | &smmu_pmu_cpumask_group, |
| 533 | &smmu_pmu_events_group, |
| 534 | &smmu_pmu_format_group, |
| 535 | NULL |
| 536 | }; |
| 537 | |
| 538 | /* |
| 539 | * Generic device handlers |
| 540 | */ |
| 541 | |
| 542 | static int smmu_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) |
| 543 | { |
| 544 | struct smmu_pmu *smmu_pmu; |
| 545 | unsigned int target; |
| 546 | |
| 547 | smmu_pmu = hlist_entry_safe(node, struct smmu_pmu, node); |
| 548 | if (cpu != smmu_pmu->on_cpu) |
| 549 | return 0; |
| 550 | |
| 551 | target = cpumask_any_but(cpu_online_mask, cpu); |
| 552 | if (target >= nr_cpu_ids) |
| 553 | return 0; |
| 554 | |
| 555 | perf_pmu_migrate_context(&smmu_pmu->pmu, cpu, target); |
| 556 | smmu_pmu->on_cpu = target; |
| 557 | WARN_ON(irq_set_affinity_hint(smmu_pmu->irq, cpumask_of(target))); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static irqreturn_t smmu_pmu_handle_irq(int irq_num, void *data) |
| 563 | { |
| 564 | struct smmu_pmu *smmu_pmu = data; |
| 565 | u64 ovsr; |
| 566 | unsigned int idx; |
| 567 | |
| 568 | ovsr = readq(smmu_pmu->reloc_base + SMMU_PMCG_OVSSET0); |
| 569 | if (!ovsr) |
| 570 | return IRQ_NONE; |
| 571 | |
| 572 | writeq(ovsr, smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0); |
| 573 | |
| 574 | for_each_set_bit(idx, (unsigned long *)&ovsr, smmu_pmu->num_counters) { |
| 575 | struct perf_event *event = smmu_pmu->events[idx]; |
| 576 | struct hw_perf_event *hwc; |
| 577 | |
| 578 | if (WARN_ON_ONCE(!event)) |
| 579 | continue; |
| 580 | |
| 581 | smmu_pmu_event_update(event); |
| 582 | hwc = &event->hw; |
| 583 | |
| 584 | smmu_pmu_set_period(smmu_pmu, hwc); |
| 585 | } |
| 586 | |
| 587 | return IRQ_HANDLED; |
| 588 | } |
| 589 | |
Shameer Kolothum | f202cda | 2019-03-26 15:17:52 +0000 | [diff] [blame^] | 590 | static void smmu_pmu_free_msis(void *data) |
| 591 | { |
| 592 | struct device *dev = data; |
| 593 | |
| 594 | platform_msi_domain_free_irqs(dev); |
| 595 | } |
| 596 | |
| 597 | static void smmu_pmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) |
| 598 | { |
| 599 | phys_addr_t doorbell; |
| 600 | struct device *dev = msi_desc_to_dev(desc); |
| 601 | struct smmu_pmu *pmu = dev_get_drvdata(dev); |
| 602 | |
| 603 | doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo; |
| 604 | doorbell &= MSI_CFG0_ADDR_MASK; |
| 605 | |
| 606 | writeq_relaxed(doorbell, pmu->reg_base + SMMU_PMCG_IRQ_CFG0); |
| 607 | writel_relaxed(msg->data, pmu->reg_base + SMMU_PMCG_IRQ_CFG1); |
| 608 | writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE, |
| 609 | pmu->reg_base + SMMU_PMCG_IRQ_CFG2); |
| 610 | } |
| 611 | |
| 612 | static void smmu_pmu_setup_msi(struct smmu_pmu *pmu) |
| 613 | { |
| 614 | struct msi_desc *desc; |
| 615 | struct device *dev = pmu->dev; |
| 616 | int ret; |
| 617 | |
| 618 | /* Clear MSI address reg */ |
| 619 | writeq_relaxed(0, pmu->reg_base + SMMU_PMCG_IRQ_CFG0); |
| 620 | |
| 621 | /* MSI supported or not */ |
| 622 | if (!(readl(pmu->reg_base + SMMU_PMCG_CFGR) & SMMU_PMCG_CFGR_MSI)) |
| 623 | return; |
| 624 | |
| 625 | ret = platform_msi_domain_alloc_irqs(dev, 1, smmu_pmu_write_msi_msg); |
| 626 | if (ret) { |
| 627 | dev_warn(dev, "failed to allocate MSIs\n"); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | desc = first_msi_entry(dev); |
| 632 | if (desc) |
| 633 | pmu->irq = desc->irq; |
| 634 | |
| 635 | /* Add callback to free MSIs on teardown */ |
| 636 | devm_add_action(dev, smmu_pmu_free_msis, dev); |
| 637 | } |
| 638 | |
Neil Leeder | 7d839b4 | 2019-03-26 15:17:51 +0000 | [diff] [blame] | 639 | static int smmu_pmu_setup_irq(struct smmu_pmu *pmu) |
| 640 | { |
| 641 | unsigned long flags = IRQF_NOBALANCING | IRQF_SHARED | IRQF_NO_THREAD; |
| 642 | int irq, ret = -ENXIO; |
| 643 | |
Shameer Kolothum | f202cda | 2019-03-26 15:17:52 +0000 | [diff] [blame^] | 644 | smmu_pmu_setup_msi(pmu); |
| 645 | |
Neil Leeder | 7d839b4 | 2019-03-26 15:17:51 +0000 | [diff] [blame] | 646 | irq = pmu->irq; |
| 647 | if (irq) |
| 648 | ret = devm_request_irq(pmu->dev, irq, smmu_pmu_handle_irq, |
| 649 | flags, "smmuv3-pmu", pmu); |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | static void smmu_pmu_reset(struct smmu_pmu *smmu_pmu) |
| 654 | { |
| 655 | u64 counter_present_mask = GENMASK_ULL(smmu_pmu->num_counters - 1, 0); |
| 656 | |
| 657 | smmu_pmu_disable(&smmu_pmu->pmu); |
| 658 | |
| 659 | /* Disable counter and interrupt */ |
| 660 | writeq_relaxed(counter_present_mask, |
| 661 | smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0); |
| 662 | writeq_relaxed(counter_present_mask, |
| 663 | smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0); |
| 664 | writeq_relaxed(counter_present_mask, |
| 665 | smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0); |
| 666 | } |
| 667 | |
| 668 | static int smmu_pmu_probe(struct platform_device *pdev) |
| 669 | { |
| 670 | struct smmu_pmu *smmu_pmu; |
| 671 | struct resource *res_0, *res_1; |
| 672 | u32 cfgr, reg_size; |
| 673 | u64 ceid_64[2]; |
| 674 | int irq, err; |
| 675 | char *name; |
| 676 | struct device *dev = &pdev->dev; |
| 677 | |
| 678 | smmu_pmu = devm_kzalloc(dev, sizeof(*smmu_pmu), GFP_KERNEL); |
| 679 | if (!smmu_pmu) |
| 680 | return -ENOMEM; |
| 681 | |
| 682 | smmu_pmu->dev = dev; |
| 683 | platform_set_drvdata(pdev, smmu_pmu); |
| 684 | |
| 685 | smmu_pmu->pmu = (struct pmu) { |
| 686 | .task_ctx_nr = perf_invalid_context, |
| 687 | .pmu_enable = smmu_pmu_enable, |
| 688 | .pmu_disable = smmu_pmu_disable, |
| 689 | .event_init = smmu_pmu_event_init, |
| 690 | .add = smmu_pmu_event_add, |
| 691 | .del = smmu_pmu_event_del, |
| 692 | .start = smmu_pmu_event_start, |
| 693 | .stop = smmu_pmu_event_stop, |
| 694 | .read = smmu_pmu_event_read, |
| 695 | .attr_groups = smmu_pmu_attr_grps, |
| 696 | .capabilities = PERF_PMU_CAP_NO_EXCLUDE, |
| 697 | }; |
| 698 | |
| 699 | res_0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 700 | smmu_pmu->reg_base = devm_ioremap_resource(dev, res_0); |
| 701 | if (IS_ERR(smmu_pmu->reg_base)) |
| 702 | return PTR_ERR(smmu_pmu->reg_base); |
| 703 | |
| 704 | cfgr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CFGR); |
| 705 | |
| 706 | /* Determine if page 1 is present */ |
| 707 | if (cfgr & SMMU_PMCG_CFGR_RELOC_CTRS) { |
| 708 | res_1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 709 | smmu_pmu->reloc_base = devm_ioremap_resource(dev, res_1); |
| 710 | if (IS_ERR(smmu_pmu->reloc_base)) |
| 711 | return PTR_ERR(smmu_pmu->reloc_base); |
| 712 | } else { |
| 713 | smmu_pmu->reloc_base = smmu_pmu->reg_base; |
| 714 | } |
| 715 | |
| 716 | irq = platform_get_irq(pdev, 0); |
| 717 | if (irq > 0) |
| 718 | smmu_pmu->irq = irq; |
| 719 | |
| 720 | ceid_64[0] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID0); |
| 721 | ceid_64[1] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID1); |
| 722 | bitmap_from_arr32(smmu_pmu->supported_events, (u32 *)ceid_64, |
| 723 | SMMU_PMCG_ARCH_MAX_EVENTS); |
| 724 | |
| 725 | smmu_pmu->num_counters = FIELD_GET(SMMU_PMCG_CFGR_NCTR, cfgr) + 1; |
| 726 | |
| 727 | smmu_pmu->global_filter = !!(cfgr & SMMU_PMCG_CFGR_SID_FILTER_TYPE); |
| 728 | |
| 729 | reg_size = FIELD_GET(SMMU_PMCG_CFGR_SIZE, cfgr); |
| 730 | smmu_pmu->counter_mask = GENMASK_ULL(reg_size, 0); |
| 731 | |
| 732 | smmu_pmu_reset(smmu_pmu); |
| 733 | |
| 734 | err = smmu_pmu_setup_irq(smmu_pmu); |
| 735 | if (err) { |
| 736 | dev_err(dev, "Setup irq failed, PMU @%pa\n", &res_0->start); |
| 737 | return err; |
| 738 | } |
| 739 | |
| 740 | name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "smmuv3_pmcg_%llx", |
| 741 | (res_0->start) >> SMMU_PMCG_PA_SHIFT); |
| 742 | if (!name) { |
| 743 | dev_err(dev, "Create name failed, PMU @%pa\n", &res_0->start); |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | |
| 747 | /* Pick one CPU to be the preferred one to use */ |
| 748 | smmu_pmu->on_cpu = raw_smp_processor_id(); |
| 749 | WARN_ON(irq_set_affinity_hint(smmu_pmu->irq, |
| 750 | cpumask_of(smmu_pmu->on_cpu))); |
| 751 | |
| 752 | err = cpuhp_state_add_instance_nocalls(cpuhp_state_num, |
| 753 | &smmu_pmu->node); |
| 754 | if (err) { |
| 755 | dev_err(dev, "Error %d registering hotplug, PMU @%pa\n", |
| 756 | err, &res_0->start); |
| 757 | goto out_cpuhp_err; |
| 758 | } |
| 759 | |
| 760 | err = perf_pmu_register(&smmu_pmu->pmu, name, -1); |
| 761 | if (err) { |
| 762 | dev_err(dev, "Error %d registering PMU @%pa\n", |
| 763 | err, &res_0->start); |
| 764 | goto out_unregister; |
| 765 | } |
| 766 | |
| 767 | dev_info(dev, "Registered PMU @ %pa using %d counters with %s filter settings\n", |
| 768 | &res_0->start, smmu_pmu->num_counters, |
| 769 | smmu_pmu->global_filter ? "Global(Counter0)" : |
| 770 | "Individual"); |
| 771 | |
| 772 | return 0; |
| 773 | |
| 774 | out_unregister: |
| 775 | cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node); |
| 776 | out_cpuhp_err: |
| 777 | put_cpu(); |
| 778 | return err; |
| 779 | } |
| 780 | |
| 781 | static int smmu_pmu_remove(struct platform_device *pdev) |
| 782 | { |
| 783 | struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev); |
| 784 | |
| 785 | perf_pmu_unregister(&smmu_pmu->pmu); |
| 786 | cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node); |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void smmu_pmu_shutdown(struct platform_device *pdev) |
| 792 | { |
| 793 | struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev); |
| 794 | |
| 795 | smmu_pmu_disable(&smmu_pmu->pmu); |
| 796 | } |
| 797 | |
| 798 | static struct platform_driver smmu_pmu_driver = { |
| 799 | .driver = { |
| 800 | .name = "arm-smmu-v3-pmcg", |
| 801 | }, |
| 802 | .probe = smmu_pmu_probe, |
| 803 | .remove = smmu_pmu_remove, |
| 804 | .shutdown = smmu_pmu_shutdown, |
| 805 | }; |
| 806 | |
| 807 | static int __init arm_smmu_pmu_init(void) |
| 808 | { |
| 809 | cpuhp_state_num = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, |
| 810 | "perf/arm/pmcg:online", |
| 811 | NULL, |
| 812 | smmu_pmu_offline_cpu); |
| 813 | if (cpuhp_state_num < 0) |
| 814 | return cpuhp_state_num; |
| 815 | |
| 816 | return platform_driver_register(&smmu_pmu_driver); |
| 817 | } |
| 818 | module_init(arm_smmu_pmu_init); |
| 819 | |
| 820 | static void __exit arm_smmu_pmu_exit(void) |
| 821 | { |
| 822 | platform_driver_unregister(&smmu_pmu_driver); |
| 823 | cpuhp_remove_multi_state(cpuhp_state_num); |
| 824 | } |
| 825 | |
| 826 | module_exit(arm_smmu_pmu_exit); |
| 827 | |
| 828 | MODULE_DESCRIPTION("PMU driver for ARM SMMUv3 Performance Monitors Extension"); |
| 829 | MODULE_AUTHOR("Neil Leeder <nleeder@codeaurora.org>"); |
| 830 | MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>"); |
| 831 | MODULE_LICENSE("GPL v2"); |