1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2020 Intel Corporation. All rights rsvd. */
4 #include <linux/sched/task.h>
5 #include <linux/io-64-nonatomic-lo-hi.h>
9 static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr,
12 static cpumask_t perfmon_dsa_cpu_mask;
13 static bool cpuhp_set_up;
14 static enum cpuhp_state cpuhp_slot;
17 * perf userspace reads this attribute to determine which cpus to open
18 * counters on. It's connected to perfmon_dsa_cpu_mask, which is
19 * maintained by the cpu hotplug handlers.
21 static DEVICE_ATTR_RO(cpumask);
23 static struct attribute *perfmon_cpumask_attrs[] = {
24 &dev_attr_cpumask.attr,
28 static struct attribute_group cpumask_attr_group = {
29 .attrs = perfmon_cpumask_attrs,
33 * These attributes specify the bits in the config word that the perf
34 * syscall uses to pass the event ids and categories to perfmon.
36 DEFINE_PERFMON_FORMAT_ATTR(event_category, "config:0-3");
37 DEFINE_PERFMON_FORMAT_ATTR(event, "config:4-31");
40 * These attributes specify the bits in the config1 word that the perf
41 * syscall uses to pass filter data to perfmon.
43 DEFINE_PERFMON_FORMAT_ATTR(filter_wq, "config1:0-31");
44 DEFINE_PERFMON_FORMAT_ATTR(filter_tc, "config1:32-39");
45 DEFINE_PERFMON_FORMAT_ATTR(filter_pgsz, "config1:40-43");
46 DEFINE_PERFMON_FORMAT_ATTR(filter_sz, "config1:44-51");
47 DEFINE_PERFMON_FORMAT_ATTR(filter_eng, "config1:52-59");
49 #define PERFMON_FILTERS_START 2
50 #define PERFMON_FILTERS_MAX 5
52 static struct attribute *perfmon_format_attrs[] = {
53 &format_attr_idxd_event_category.attr,
54 &format_attr_idxd_event.attr,
55 &format_attr_idxd_filter_wq.attr,
56 &format_attr_idxd_filter_tc.attr,
57 &format_attr_idxd_filter_pgsz.attr,
58 &format_attr_idxd_filter_sz.attr,
59 &format_attr_idxd_filter_eng.attr,
63 static struct attribute_group perfmon_format_attr_group = {
65 .attrs = perfmon_format_attrs,
68 static const struct attribute_group *perfmon_attr_groups[] = {
69 &perfmon_format_attr_group,
74 static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr,
77 return cpumap_print_to_pagebuf(true, buf, &perfmon_dsa_cpu_mask);
80 static bool is_idxd_event(struct idxd_pmu *idxd_pmu, struct perf_event *event)
82 return &idxd_pmu->pmu == event->pmu;
85 static int perfmon_collect_events(struct idxd_pmu *idxd_pmu,
86 struct perf_event *leader,
89 struct perf_event *event;
92 max_count = idxd_pmu->n_counters;
93 n = idxd_pmu->n_events;
98 if (is_idxd_event(idxd_pmu, leader)) {
99 idxd_pmu->event_list[n] = leader;
100 idxd_pmu->event_list[n]->hw.idx = n;
107 for_each_sibling_event(event, leader) {
108 if (!is_idxd_event(idxd_pmu, event) ||
109 event->state <= PERF_EVENT_STATE_OFF)
115 idxd_pmu->event_list[n] = event;
116 idxd_pmu->event_list[n]->hw.idx = n;
123 static void perfmon_assign_hw_event(struct idxd_pmu *idxd_pmu,
124 struct perf_event *event, int idx)
126 struct idxd_device *idxd = idxd_pmu->idxd;
127 struct hw_perf_event *hwc = &event->hw;
130 hwc->config_base = ioread64(CNTRCFG_REG(idxd, idx));
131 hwc->event_base = ioread64(CNTRCFG_REG(idxd, idx));
134 static int perfmon_assign_event(struct idxd_pmu *idxd_pmu,
135 struct perf_event *event)
139 for (i = 0; i < IDXD_PMU_EVENT_MAX; i++)
140 if (!test_and_set_bit(i, idxd_pmu->used_mask))
147 * Check whether there are enough counters to satisfy that all the
148 * events in the group can actually be scheduled at the same time.
150 * To do this, create a fake idxd_pmu object so the event collection
151 * and assignment functions can be used without affecting the internal
152 * state of the real idxd_pmu object.
154 static int perfmon_validate_group(struct idxd_pmu *pmu,
155 struct perf_event *event)
157 struct perf_event *leader = event->group_leader;
158 struct idxd_pmu *fake_pmu;
159 int i, ret = 0, n, idx;
161 fake_pmu = kzalloc(sizeof(*fake_pmu), GFP_KERNEL);
165 fake_pmu->pmu.name = pmu->pmu.name;
166 fake_pmu->n_counters = pmu->n_counters;
168 n = perfmon_collect_events(fake_pmu, leader, true);
174 fake_pmu->n_events = n;
175 n = perfmon_collect_events(fake_pmu, event, false);
181 fake_pmu->n_events = n;
183 for (i = 0; i < n; i++) {
184 event = fake_pmu->event_list[i];
186 idx = perfmon_assign_event(fake_pmu, event);
198 static int perfmon_pmu_event_init(struct perf_event *event)
200 struct idxd_device *idxd;
203 idxd = event_to_idxd(event);
206 if (event->attr.type != event->pmu->type)
209 /* sampling not supported */
210 if (event->attr.sample_period)
216 if (event->pmu != &idxd->idxd_pmu->pmu)
219 event->hw.event_base = ioread64(PERFMON_TABLE_OFFSET(idxd));
220 event->cpu = idxd->idxd_pmu->cpu;
221 event->hw.config = event->attr.config;
223 if (event->group_leader != event)
224 /* non-group events have themselves as leader */
225 ret = perfmon_validate_group(idxd->idxd_pmu, event);
230 static inline u64 perfmon_pmu_read_counter(struct perf_event *event)
232 struct hw_perf_event *hwc = &event->hw;
233 struct idxd_device *idxd;
236 idxd = event_to_idxd(event);
238 return ioread64(CNTRDATA_REG(idxd, cntr));
241 static void perfmon_pmu_event_update(struct perf_event *event)
243 struct idxd_device *idxd = event_to_idxd(event);
244 u64 prev_raw_count, new_raw_count, delta, p, n;
245 int shift = 64 - idxd->idxd_pmu->counter_width;
246 struct hw_perf_event *hwc = &event->hw;
249 prev_raw_count = local64_read(&hwc->prev_count);
250 new_raw_count = perfmon_pmu_read_counter(event);
251 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
252 new_raw_count) != prev_raw_count);
254 n = (new_raw_count << shift);
255 p = (prev_raw_count << shift);
257 delta = ((n - p) >> shift);
259 local64_add(delta, &event->count);
262 void perfmon_counter_overflow(struct idxd_device *idxd)
264 int i, n_counters, max_loop = OVERFLOW_SIZE;
265 struct perf_event *event;
266 unsigned long ovfstatus;
268 n_counters = min(idxd->idxd_pmu->n_counters, OVERFLOW_SIZE);
270 ovfstatus = ioread32(OVFSTATUS_REG(idxd));
273 * While updating overflowed counters, other counters behind
274 * them could overflow and be missed in a given pass.
275 * Normally this could happen at most n_counters times, but in
276 * theory a tiny counter width could result in continual
277 * overflows and endless looping. max_loop provides a
278 * failsafe in that highly unlikely case.
280 while (ovfstatus && max_loop--) {
281 /* Figure out which counter(s) overflowed */
282 for_each_set_bit(i, &ovfstatus, n_counters) {
283 unsigned long ovfstatus_clear = 0;
285 /* Update event->count for overflowed counter */
286 event = idxd->idxd_pmu->event_list[i];
287 perfmon_pmu_event_update(event);
288 /* Writing 1 to OVFSTATUS bit clears it */
289 set_bit(i, &ovfstatus_clear);
290 iowrite32(ovfstatus_clear, OVFSTATUS_REG(idxd));
293 ovfstatus = ioread32(OVFSTATUS_REG(idxd));
297 * Should never happen. If so, it means a counter(s) looped
298 * around twice while this handler was running.
300 WARN_ON_ONCE(ovfstatus);
303 static inline void perfmon_reset_config(struct idxd_device *idxd)
305 iowrite32(CONFIG_RESET, PERFRST_REG(idxd));
306 iowrite32(0, OVFSTATUS_REG(idxd));
307 iowrite32(0, PERFFRZ_REG(idxd));
310 static inline void perfmon_reset_counters(struct idxd_device *idxd)
312 iowrite32(CNTR_RESET, PERFRST_REG(idxd));
315 static inline void perfmon_reset(struct idxd_device *idxd)
317 perfmon_reset_config(idxd);
318 perfmon_reset_counters(idxd);
321 static void perfmon_pmu_event_start(struct perf_event *event, int mode)
323 u32 flt_wq, flt_tc, flt_pg_sz, flt_xfer_sz, flt_eng = 0;
324 u64 cntr_cfg, cntrdata, event_enc, event_cat = 0;
325 struct hw_perf_event *hwc = &event->hw;
326 union filter_cfg flt_cfg;
327 union event_cfg event_cfg;
328 struct idxd_device *idxd;
331 idxd = event_to_idxd(event);
333 event->hw.idx = hwc->idx;
336 /* Obtain event category and event value from user space */
337 event_cfg.val = event->attr.config;
338 flt_cfg.val = event->attr.config1;
339 event_cat = event_cfg.event_cat;
340 event_enc = event_cfg.event_enc;
342 /* Obtain filter configuration from user space */
345 flt_pg_sz = flt_cfg.pg_sz;
346 flt_xfer_sz = flt_cfg.xfer_sz;
347 flt_eng = flt_cfg.eng;
349 if (flt_wq && test_bit(FLT_WQ, &idxd->idxd_pmu->supported_filters))
350 iowrite32(flt_wq, FLTCFG_REG(idxd, cntr, FLT_WQ));
351 if (flt_tc && test_bit(FLT_TC, &idxd->idxd_pmu->supported_filters))
352 iowrite32(flt_tc, FLTCFG_REG(idxd, cntr, FLT_TC));
353 if (flt_pg_sz && test_bit(FLT_PG_SZ, &idxd->idxd_pmu->supported_filters))
354 iowrite32(flt_pg_sz, FLTCFG_REG(idxd, cntr, FLT_PG_SZ));
355 if (flt_xfer_sz && test_bit(FLT_XFER_SZ, &idxd->idxd_pmu->supported_filters))
356 iowrite32(flt_xfer_sz, FLTCFG_REG(idxd, cntr, FLT_XFER_SZ));
357 if (flt_eng && test_bit(FLT_ENG, &idxd->idxd_pmu->supported_filters))
358 iowrite32(flt_eng, FLTCFG_REG(idxd, cntr, FLT_ENG));
360 /* Read the start value */
361 cntrdata = ioread64(CNTRDATA_REG(idxd, cntr));
362 local64_set(&event->hw.prev_count, cntrdata);
364 /* Set counter to event/category */
365 cntr_cfg = event_cat << CNTRCFG_CATEGORY_SHIFT;
366 cntr_cfg |= event_enc << CNTRCFG_EVENT_SHIFT;
367 /* Set interrupt on overflow and counter enable bits */
368 cntr_cfg |= (CNTRCFG_IRQ_OVERFLOW | CNTRCFG_ENABLE);
370 iowrite64(cntr_cfg, CNTRCFG_REG(idxd, cntr));
373 static void perfmon_pmu_event_stop(struct perf_event *event, int mode)
375 struct hw_perf_event *hwc = &event->hw;
376 struct idxd_device *idxd;
377 int i, cntr = hwc->idx;
380 idxd = event_to_idxd(event);
382 /* remove this event from event list */
383 for (i = 0; i < idxd->idxd_pmu->n_events; i++) {
384 if (event != idxd->idxd_pmu->event_list[i])
387 for (++i; i < idxd->idxd_pmu->n_events; i++)
388 idxd->idxd_pmu->event_list[i - 1] = idxd->idxd_pmu->event_list[i];
389 --idxd->idxd_pmu->n_events;
393 cntr_cfg = ioread64(CNTRCFG_REG(idxd, cntr));
394 cntr_cfg &= ~CNTRCFG_ENABLE;
395 iowrite64(cntr_cfg, CNTRCFG_REG(idxd, cntr));
397 if (mode == PERF_EF_UPDATE)
398 perfmon_pmu_event_update(event);
401 clear_bit(cntr, idxd->idxd_pmu->used_mask);
404 static void perfmon_pmu_event_del(struct perf_event *event, int mode)
406 perfmon_pmu_event_stop(event, PERF_EF_UPDATE);
409 static int perfmon_pmu_event_add(struct perf_event *event, int flags)
411 struct idxd_device *idxd = event_to_idxd(event);
412 struct idxd_pmu *idxd_pmu = idxd->idxd_pmu;
413 struct hw_perf_event *hwc = &event->hw;
416 n = perfmon_collect_events(idxd_pmu, event, false);
420 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
421 if (!(flags & PERF_EF_START))
422 hwc->state |= PERF_HES_ARCH;
424 idx = perfmon_assign_event(idxd_pmu, event);
428 perfmon_assign_hw_event(idxd_pmu, event, idx);
430 if (flags & PERF_EF_START)
431 perfmon_pmu_event_start(event, 0);
433 idxd_pmu->n_events = n;
438 static void enable_perfmon_pmu(struct idxd_device *idxd)
440 iowrite32(COUNTER_UNFREEZE, PERFFRZ_REG(idxd));
443 static void disable_perfmon_pmu(struct idxd_device *idxd)
445 iowrite32(COUNTER_FREEZE, PERFFRZ_REG(idxd));
448 static void perfmon_pmu_enable(struct pmu *pmu)
450 struct idxd_device *idxd = pmu_to_idxd(pmu);
452 enable_perfmon_pmu(idxd);
455 static void perfmon_pmu_disable(struct pmu *pmu)
457 struct idxd_device *idxd = pmu_to_idxd(pmu);
459 disable_perfmon_pmu(idxd);
462 static void skip_filter(int i)
466 for (j = i; j < PERFMON_FILTERS_MAX; j++)
467 perfmon_format_attrs[PERFMON_FILTERS_START + j] =
468 perfmon_format_attrs[PERFMON_FILTERS_START + j + 1];
471 static void idxd_pmu_init(struct idxd_pmu *idxd_pmu)
475 for (i = 0 ; i < PERFMON_FILTERS_MAX; i++) {
476 if (!test_bit(i, &idxd_pmu->supported_filters))
480 idxd_pmu->pmu.name = idxd_pmu->name;
481 idxd_pmu->pmu.attr_groups = perfmon_attr_groups;
482 idxd_pmu->pmu.task_ctx_nr = perf_invalid_context;
483 idxd_pmu->pmu.event_init = perfmon_pmu_event_init;
484 idxd_pmu->pmu.pmu_enable = perfmon_pmu_enable,
485 idxd_pmu->pmu.pmu_disable = perfmon_pmu_disable,
486 idxd_pmu->pmu.add = perfmon_pmu_event_add;
487 idxd_pmu->pmu.del = perfmon_pmu_event_del;
488 idxd_pmu->pmu.start = perfmon_pmu_event_start;
489 idxd_pmu->pmu.stop = perfmon_pmu_event_stop;
490 idxd_pmu->pmu.read = perfmon_pmu_event_update;
491 idxd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
492 idxd_pmu->pmu.module = THIS_MODULE;
495 void perfmon_pmu_remove(struct idxd_device *idxd)
500 cpuhp_state_remove_instance(cpuhp_slot, &idxd->idxd_pmu->cpuhp_node);
501 perf_pmu_unregister(&idxd->idxd_pmu->pmu);
502 kfree(idxd->idxd_pmu);
503 idxd->idxd_pmu = NULL;
506 static int perf_event_cpu_online(unsigned int cpu, struct hlist_node *node)
508 struct idxd_pmu *idxd_pmu;
510 idxd_pmu = hlist_entry_safe(node, typeof(*idxd_pmu), cpuhp_node);
512 /* select the first online CPU as the designated reader */
513 if (cpumask_empty(&perfmon_dsa_cpu_mask)) {
514 cpumask_set_cpu(cpu, &perfmon_dsa_cpu_mask);
521 static int perf_event_cpu_offline(unsigned int cpu, struct hlist_node *node)
523 struct idxd_pmu *idxd_pmu;
526 idxd_pmu = hlist_entry_safe(node, typeof(*idxd_pmu), cpuhp_node);
528 if (!cpumask_test_and_clear_cpu(cpu, &perfmon_dsa_cpu_mask))
531 target = cpumask_any_but(cpu_online_mask, cpu);
533 /* migrate events if there is a valid target */
534 if (target < nr_cpu_ids)
535 cpumask_set_cpu(target, &perfmon_dsa_cpu_mask);
539 perf_pmu_migrate_context(&idxd_pmu->pmu, cpu, target);
544 int perfmon_pmu_init(struct idxd_device *idxd)
546 union idxd_perfcap perfcap;
547 struct idxd_pmu *idxd_pmu;
551 * perfmon module initialization failed, nothing to do
557 * If perfmon_offset or num_counters is 0, it means perfmon is
558 * not supported on this hardware.
560 if (idxd->perfmon_offset == 0)
563 idxd_pmu = kzalloc(sizeof(*idxd_pmu), GFP_KERNEL);
567 idxd_pmu->idxd = idxd;
568 idxd->idxd_pmu = idxd_pmu;
570 if (idxd->data->type == IDXD_TYPE_DSA) {
571 rc = sprintf(idxd_pmu->name, "dsa%d", idxd->id);
574 } else if (idxd->data->type == IDXD_TYPE_IAX) {
575 rc = sprintf(idxd_pmu->name, "iax%d", idxd->id);
584 perfcap.bits = ioread64(PERFCAP_REG(idxd));
587 * If total perf counter is 0, stop further registration.
588 * This is necessary in order to support driver running on
589 * guest which does not have pmon support.
591 if (perfcap.num_perf_counter == 0)
594 /* A counter width of 0 means it can't count */
595 if (perfcap.counter_width == 0)
598 /* Overflow interrupt and counter freeze support must be available */
599 if (!perfcap.overflow_interrupt || !perfcap.counter_freeze)
602 /* Number of event categories cannot be 0 */
603 if (perfcap.num_event_category == 0)
607 * We don't support per-counter capabilities for now.
609 if (perfcap.cap_per_counter)
612 idxd_pmu->n_event_categories = perfcap.num_event_category;
613 idxd_pmu->supported_event_categories = perfcap.global_event_category;
614 idxd_pmu->per_counter_caps_supported = perfcap.cap_per_counter;
616 /* check filter capability. If 0, then filters are not supported */
617 idxd_pmu->supported_filters = perfcap.filter;
619 idxd_pmu->n_filters = hweight8(perfcap.filter);
621 /* Store the total number of counters categories, and counter width */
622 idxd_pmu->n_counters = perfcap.num_perf_counter;
623 idxd_pmu->counter_width = perfcap.counter_width;
625 idxd_pmu_init(idxd_pmu);
627 rc = perf_pmu_register(&idxd_pmu->pmu, idxd_pmu->name, -1);
631 rc = cpuhp_state_add_instance(cpuhp_slot, &idxd_pmu->cpuhp_node);
633 perf_pmu_unregister(&idxd->idxd_pmu->pmu);
640 idxd->idxd_pmu = NULL;
645 void __init perfmon_init(void)
647 int rc = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
648 "driver/dma/idxd/perf:online",
649 perf_event_cpu_online,
650 perf_event_cpu_offline);
658 void __exit perfmon_exit(void)
661 cpuhp_remove_multi_state(cpuhp_slot);