1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Support Intel uncore PerfMon discovery mechanism.
4 * Copyright(c) 2021 Intel Corporation.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include "uncore_discovery.h"
11 static struct rb_root discovery_tables = RB_ROOT;
12 static int num_discovered_types[UNCORE_ACCESS_MAX];
14 static bool has_generic_discovery_table(void)
19 dev = pci_get_device(PCI_VENDOR_ID_INTEL, UNCORE_DISCOVERY_TABLE_DEVICE, NULL);
23 /* A discovery table device has the unique capability ID. */
24 dvsec = pci_find_next_ext_capability(dev, 0, UNCORE_EXT_CAP_ID_DISCOVERY);
32 static int logical_die_id;
34 static int get_device_die_id(struct pci_dev *dev)
36 int node = pcibus_to_node(dev->bus);
39 * If the NUMA info is not available, assume that the logical die id is
40 * continuous in the order in which the discovery table devices are
44 return logical_die_id++;
46 return uncore_device_to_die(dev);
49 #define __node_2_type(cur) \
50 rb_entry((cur), struct intel_uncore_discovery_type, node)
52 static inline int __type_cmp(const void *key, const struct rb_node *b)
54 struct intel_uncore_discovery_type *type_b = __node_2_type(b);
55 const u16 *type_id = key;
57 if (type_b->type > *type_id)
59 else if (type_b->type < *type_id)
65 static inline struct intel_uncore_discovery_type *
66 search_uncore_discovery_type(u16 type_id)
68 struct rb_node *node = rb_find(&type_id, &discovery_tables, __type_cmp);
70 return (node) ? __node_2_type(node) : NULL;
73 static inline bool __type_less(struct rb_node *a, const struct rb_node *b)
75 return (__node_2_type(a)->type < __node_2_type(b)->type);
78 static struct intel_uncore_discovery_type *
79 add_uncore_discovery_type(struct uncore_unit_discovery *unit)
81 struct intel_uncore_discovery_type *type;
83 if (unit->access_type >= UNCORE_ACCESS_MAX) {
84 pr_warn("Unsupported access type %d\n", unit->access_type);
88 type = kzalloc(sizeof(struct intel_uncore_discovery_type), GFP_KERNEL);
92 type->box_ctrl_die = kcalloc(__uncore_max_dies, sizeof(u64), GFP_KERNEL);
93 if (!type->box_ctrl_die)
96 type->access_type = unit->access_type;
97 num_discovered_types[type->access_type]++;
98 type->type = unit->box_type;
100 rb_add(&type->node, &discovery_tables, __type_less);
111 static struct intel_uncore_discovery_type *
112 get_uncore_discovery_type(struct uncore_unit_discovery *unit)
114 struct intel_uncore_discovery_type *type;
116 type = search_uncore_discovery_type(unit->box_type);
120 return add_uncore_discovery_type(unit);
124 uncore_insert_box_info(struct uncore_unit_discovery *unit,
125 int die, bool parsed)
127 struct intel_uncore_discovery_type *type;
132 if (!unit->ctl || !unit->ctl_offset || !unit->ctr_offset) {
133 pr_info("Invalid address is detected for uncore type %d box %d, "
134 "Disable the uncore unit.\n",
135 unit->box_type, unit->box_id);
140 type = search_uncore_discovery_type(unit->box_type);
142 pr_info("A spurious uncore type %d is detected, "
143 "Disable the uncore type.\n",
147 /* Store the first box of each die */
148 if (!type->box_ctrl_die[die])
149 type->box_ctrl_die[die] = unit->ctl;
153 type = get_uncore_discovery_type(unit);
157 box_offset = kcalloc(type->num_boxes + 1, sizeof(u64), GFP_KERNEL);
161 ids = kcalloc(type->num_boxes + 1, sizeof(unsigned int), GFP_KERNEL);
163 goto free_box_offset;
165 /* Store generic information for the first box */
166 if (!type->num_boxes) {
167 type->box_ctrl = unit->ctl;
168 type->box_ctrl_die[die] = unit->ctl;
169 type->num_counters = unit->num_regs;
170 type->counter_width = unit->bit_width;
171 type->ctl_offset = unit->ctl_offset;
172 type->ctr_offset = unit->ctr_offset;
177 for (i = 0; i < type->num_boxes; i++) {
178 ids[i] = type->ids[i];
179 box_offset[i] = type->box_offset[i];
181 if (unit->box_id == ids[i]) {
182 pr_info("Duplicate uncore type %d box ID %d is detected, "
183 "Drop the duplicate uncore unit.\n",
184 unit->box_type, unit->box_id);
188 ids[i] = unit->box_id;
189 box_offset[i] = unit->ctl - type->box_ctrl;
191 kfree(type->box_offset);
194 type->box_offset = box_offset;
207 uncore_ignore_unit(struct uncore_unit_discovery *unit, int *ignore)
214 for (i = 0; ignore[i] != UNCORE_IGNORE_END ; i++) {
215 if (unit->box_type == ignore[i])
222 static int parse_discovery_table(struct pci_dev *dev, int die,
223 u32 bar_offset, bool *parsed,
226 struct uncore_global_discovery global;
227 struct uncore_unit_discovery unit;
228 void __iomem *io_addr;
229 resource_size_t addr;
234 pci_read_config_dword(dev, bar_offset, &val);
236 if (val & ~PCI_BASE_ADDRESS_MEM_MASK & ~PCI_BASE_ADDRESS_MEM_TYPE_64)
239 addr = (resource_size_t)(val & PCI_BASE_ADDRESS_MEM_MASK);
240 #ifdef CONFIG_PHYS_ADDR_T_64BIT
241 if ((val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
244 pci_read_config_dword(dev, bar_offset + 4, &val2);
245 addr |= ((resource_size_t)val2) << 32;
248 size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;
249 io_addr = ioremap(addr, size);
253 /* Read Global Discovery State */
254 memcpy_fromio(&global, io_addr, sizeof(struct uncore_global_discovery));
255 if (uncore_discovery_invalid_unit(global)) {
256 pr_info("Invalid Global Discovery State: 0x%llx 0x%llx 0x%llx\n",
257 global.table1, global.ctl, global.table3);
263 size = (1 + global.max_units) * global.stride * 8;
264 io_addr = ioremap(addr, size);
268 /* Parsing Unit Discovery State */
269 for (i = 0; i < global.max_units; i++) {
270 memcpy_fromio(&unit, io_addr + (i + 1) * (global.stride * 8),
271 sizeof(struct uncore_unit_discovery));
273 if (uncore_discovery_invalid_unit(unit))
276 if (unit.access_type >= UNCORE_ACCESS_MAX)
279 if (uncore_ignore_unit(&unit, ignore))
282 uncore_insert_box_info(&unit, die, *parsed);
290 bool intel_uncore_has_discovery_tables(int *ignore)
292 u32 device, val, entry_id, bar_offset;
293 int die, dvsec = 0, ret = true;
294 struct pci_dev *dev = NULL;
297 if (has_generic_discovery_table())
298 device = UNCORE_DISCOVERY_TABLE_DEVICE;
303 * Start a new search and iterates through the list of
304 * the discovery table devices.
306 while ((dev = pci_get_device(PCI_VENDOR_ID_INTEL, device, dev)) != NULL) {
307 while ((dvsec = pci_find_next_ext_capability(dev, dvsec, UNCORE_EXT_CAP_ID_DISCOVERY))) {
308 pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC_OFFSET, &val);
309 entry_id = val & UNCORE_DISCOVERY_DVSEC_ID_MASK;
310 if (entry_id != UNCORE_DISCOVERY_DVSEC_ID_PMON)
313 pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC2_OFFSET, &val);
315 if (val & ~UNCORE_DISCOVERY_DVSEC2_BIR_MASK) {
319 bar_offset = UNCORE_DISCOVERY_BIR_BASE +
320 (val & UNCORE_DISCOVERY_DVSEC2_BIR_MASK) * UNCORE_DISCOVERY_BIR_STEP;
322 die = get_device_die_id(dev);
326 parse_discovery_table(dev, die, bar_offset, &parsed, ignore);
330 /* None of the discovery tables are available */
339 void intel_uncore_clear_discovery_tables(void)
341 struct intel_uncore_discovery_type *type, *next;
343 rbtree_postorder_for_each_entry_safe(type, next, &discovery_tables, node) {
344 kfree(type->box_ctrl_die);
349 DEFINE_UNCORE_FORMAT_ATTR(event, event, "config:0-7");
350 DEFINE_UNCORE_FORMAT_ATTR(umask, umask, "config:8-15");
351 DEFINE_UNCORE_FORMAT_ATTR(edge, edge, "config:18");
352 DEFINE_UNCORE_FORMAT_ATTR(inv, inv, "config:23");
353 DEFINE_UNCORE_FORMAT_ATTR(thresh, thresh, "config:24-31");
355 static struct attribute *generic_uncore_formats_attr[] = {
356 &format_attr_event.attr,
357 &format_attr_umask.attr,
358 &format_attr_edge.attr,
359 &format_attr_inv.attr,
360 &format_attr_thresh.attr,
364 static const struct attribute_group generic_uncore_format_group = {
366 .attrs = generic_uncore_formats_attr,
369 void intel_generic_uncore_msr_init_box(struct intel_uncore_box *box)
371 wrmsrl(uncore_msr_box_ctl(box), GENERIC_PMON_BOX_CTL_INT);
374 void intel_generic_uncore_msr_disable_box(struct intel_uncore_box *box)
376 wrmsrl(uncore_msr_box_ctl(box), GENERIC_PMON_BOX_CTL_FRZ);
379 void intel_generic_uncore_msr_enable_box(struct intel_uncore_box *box)
381 wrmsrl(uncore_msr_box_ctl(box), 0);
384 static void intel_generic_uncore_msr_enable_event(struct intel_uncore_box *box,
385 struct perf_event *event)
387 struct hw_perf_event *hwc = &event->hw;
389 wrmsrl(hwc->config_base, hwc->config);
392 static void intel_generic_uncore_msr_disable_event(struct intel_uncore_box *box,
393 struct perf_event *event)
395 struct hw_perf_event *hwc = &event->hw;
397 wrmsrl(hwc->config_base, 0);
400 static struct intel_uncore_ops generic_uncore_msr_ops = {
401 .init_box = intel_generic_uncore_msr_init_box,
402 .disable_box = intel_generic_uncore_msr_disable_box,
403 .enable_box = intel_generic_uncore_msr_enable_box,
404 .disable_event = intel_generic_uncore_msr_disable_event,
405 .enable_event = intel_generic_uncore_msr_enable_event,
406 .read_counter = uncore_msr_read_counter,
409 void intel_generic_uncore_pci_init_box(struct intel_uncore_box *box)
411 struct pci_dev *pdev = box->pci_dev;
412 int box_ctl = uncore_pci_box_ctl(box);
414 __set_bit(UNCORE_BOX_FLAG_CTL_OFFS8, &box->flags);
415 pci_write_config_dword(pdev, box_ctl, GENERIC_PMON_BOX_CTL_INT);
418 void intel_generic_uncore_pci_disable_box(struct intel_uncore_box *box)
420 struct pci_dev *pdev = box->pci_dev;
421 int box_ctl = uncore_pci_box_ctl(box);
423 pci_write_config_dword(pdev, box_ctl, GENERIC_PMON_BOX_CTL_FRZ);
426 void intel_generic_uncore_pci_enable_box(struct intel_uncore_box *box)
428 struct pci_dev *pdev = box->pci_dev;
429 int box_ctl = uncore_pci_box_ctl(box);
431 pci_write_config_dword(pdev, box_ctl, 0);
434 static void intel_generic_uncore_pci_enable_event(struct intel_uncore_box *box,
435 struct perf_event *event)
437 struct pci_dev *pdev = box->pci_dev;
438 struct hw_perf_event *hwc = &event->hw;
440 pci_write_config_dword(pdev, hwc->config_base, hwc->config);
443 void intel_generic_uncore_pci_disable_event(struct intel_uncore_box *box,
444 struct perf_event *event)
446 struct pci_dev *pdev = box->pci_dev;
447 struct hw_perf_event *hwc = &event->hw;
449 pci_write_config_dword(pdev, hwc->config_base, 0);
452 u64 intel_generic_uncore_pci_read_counter(struct intel_uncore_box *box,
453 struct perf_event *event)
455 struct pci_dev *pdev = box->pci_dev;
456 struct hw_perf_event *hwc = &event->hw;
459 pci_read_config_dword(pdev, hwc->event_base, (u32 *)&count);
460 pci_read_config_dword(pdev, hwc->event_base + 4, (u32 *)&count + 1);
465 static struct intel_uncore_ops generic_uncore_pci_ops = {
466 .init_box = intel_generic_uncore_pci_init_box,
467 .disable_box = intel_generic_uncore_pci_disable_box,
468 .enable_box = intel_generic_uncore_pci_enable_box,
469 .disable_event = intel_generic_uncore_pci_disable_event,
470 .enable_event = intel_generic_uncore_pci_enable_event,
471 .read_counter = intel_generic_uncore_pci_read_counter,
474 #define UNCORE_GENERIC_MMIO_SIZE 0x4000
476 static u64 generic_uncore_mmio_box_ctl(struct intel_uncore_box *box)
478 struct intel_uncore_type *type = box->pmu->type;
480 if (!type->box_ctls || !type->box_ctls[box->dieid] || !type->mmio_offsets)
483 return type->box_ctls[box->dieid] + type->mmio_offsets[box->pmu->pmu_idx];
486 void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)
488 u64 box_ctl = generic_uncore_mmio_box_ctl(box);
489 struct intel_uncore_type *type = box->pmu->type;
490 resource_size_t addr;
493 pr_warn("Uncore type %d box %d: Invalid box control address.\n",
494 type->type_id, type->box_ids[box->pmu->pmu_idx]);
499 box->io_addr = ioremap(addr, UNCORE_GENERIC_MMIO_SIZE);
501 pr_warn("Uncore type %d box %d: ioremap error for 0x%llx.\n",
502 type->type_id, type->box_ids[box->pmu->pmu_idx],
503 (unsigned long long)addr);
507 writel(GENERIC_PMON_BOX_CTL_INT, box->io_addr);
510 void intel_generic_uncore_mmio_disable_box(struct intel_uncore_box *box)
515 writel(GENERIC_PMON_BOX_CTL_FRZ, box->io_addr);
518 void intel_generic_uncore_mmio_enable_box(struct intel_uncore_box *box)
523 writel(0, box->io_addr);
526 void intel_generic_uncore_mmio_enable_event(struct intel_uncore_box *box,
527 struct perf_event *event)
529 struct hw_perf_event *hwc = &event->hw;
534 writel(hwc->config, box->io_addr + hwc->config_base);
537 void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,
538 struct perf_event *event)
540 struct hw_perf_event *hwc = &event->hw;
545 writel(0, box->io_addr + hwc->config_base);
548 static struct intel_uncore_ops generic_uncore_mmio_ops = {
549 .init_box = intel_generic_uncore_mmio_init_box,
550 .exit_box = uncore_mmio_exit_box,
551 .disable_box = intel_generic_uncore_mmio_disable_box,
552 .enable_box = intel_generic_uncore_mmio_enable_box,
553 .disable_event = intel_generic_uncore_mmio_disable_event,
554 .enable_event = intel_generic_uncore_mmio_enable_event,
555 .read_counter = uncore_mmio_read_counter,
558 static bool uncore_update_uncore_type(enum uncore_access_type type_id,
559 struct intel_uncore_type *uncore,
560 struct intel_uncore_discovery_type *type)
562 uncore->type_id = type->type;
563 uncore->num_boxes = type->num_boxes;
564 uncore->num_counters = type->num_counters;
565 uncore->perf_ctr_bits = type->counter_width;
566 uncore->box_ids = type->ids;
569 case UNCORE_ACCESS_MSR:
570 uncore->ops = &generic_uncore_msr_ops;
571 uncore->perf_ctr = (unsigned int)type->box_ctrl + type->ctr_offset;
572 uncore->event_ctl = (unsigned int)type->box_ctrl + type->ctl_offset;
573 uncore->box_ctl = (unsigned int)type->box_ctrl;
574 uncore->msr_offsets = type->box_offset;
576 case UNCORE_ACCESS_PCI:
577 uncore->ops = &generic_uncore_pci_ops;
578 uncore->perf_ctr = (unsigned int)UNCORE_DISCOVERY_PCI_BOX_CTRL(type->box_ctrl) + type->ctr_offset;
579 uncore->event_ctl = (unsigned int)UNCORE_DISCOVERY_PCI_BOX_CTRL(type->box_ctrl) + type->ctl_offset;
580 uncore->box_ctl = (unsigned int)UNCORE_DISCOVERY_PCI_BOX_CTRL(type->box_ctrl);
581 uncore->box_ctls = type->box_ctrl_die;
582 uncore->pci_offsets = type->box_offset;
584 case UNCORE_ACCESS_MMIO:
585 uncore->ops = &generic_uncore_mmio_ops;
586 uncore->perf_ctr = (unsigned int)type->ctr_offset;
587 uncore->event_ctl = (unsigned int)type->ctl_offset;
588 uncore->box_ctl = (unsigned int)type->box_ctrl;
589 uncore->box_ctls = type->box_ctrl_die;
590 uncore->mmio_offsets = type->box_offset;
591 uncore->mmio_map_size = UNCORE_GENERIC_MMIO_SIZE;
600 struct intel_uncore_type **
601 intel_uncore_generic_init_uncores(enum uncore_access_type type_id, int num_extra)
603 struct intel_uncore_discovery_type *type;
604 struct intel_uncore_type **uncores;
605 struct intel_uncore_type *uncore;
606 struct rb_node *node;
609 uncores = kcalloc(num_discovered_types[type_id] + num_extra + 1,
610 sizeof(struct intel_uncore_type *), GFP_KERNEL);
614 for (node = rb_first(&discovery_tables); node; node = rb_next(node)) {
615 type = rb_entry(node, struct intel_uncore_discovery_type, node);
616 if (type->access_type != type_id)
619 uncore = kzalloc(sizeof(struct intel_uncore_type), GFP_KERNEL);
623 uncore->event_mask = GENERIC_PMON_RAW_EVENT_MASK;
624 uncore->format_group = &generic_uncore_format_group;
626 if (!uncore_update_uncore_type(type_id, uncore, type)) {
630 uncores[i++] = uncore;
636 void intel_uncore_generic_uncore_cpu_init(void)
638 uncore_msr_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_MSR, 0);
641 int intel_uncore_generic_uncore_pci_init(void)
643 uncore_pci_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_PCI, 0);
648 void intel_uncore_generic_uncore_mmio_init(void)
650 uncore_mmio_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_MMIO, 0);