1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Support Intel uncore PerfMon discovery mechanism.
4 * Copyright(c) 2021 Intel Corporation.
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <asm/msr.h>
9 #include "uncore.h"
10 #include "uncore_discovery.h"
11
12 static struct rb_root discovery_tables = RB_ROOT;
13 static int num_discovered_types[UNCORE_ACCESS_MAX];
14
15 static int logical_die_id;
16
get_device_die_id(struct pci_dev * dev)17 static int get_device_die_id(struct pci_dev *dev)
18 {
19 int node = pcibus_to_node(dev->bus);
20
21 /*
22 * If the NUMA info is not available, assume that the logical die id is
23 * continuous in the order in which the discovery table devices are
24 * detected.
25 */
26 if (node < 0)
27 return logical_die_id++;
28
29 return uncore_device_to_die(dev);
30 }
31
32 #define __node_2_type(cur) \
33 rb_entry((cur), struct intel_uncore_discovery_type, node)
34
__type_cmp(const void * key,const struct rb_node * b)35 static inline int __type_cmp(const void *key, const struct rb_node *b)
36 {
37 const struct intel_uncore_discovery_type *type_b = __node_2_type(b);
38 const u16 *type_id = key;
39
40 if (type_b->type > *type_id)
41 return -1;
42 else if (type_b->type < *type_id)
43 return 1;
44
45 return 0;
46 }
47
48 static inline struct intel_uncore_discovery_type *
search_uncore_discovery_type(u16 type_id)49 search_uncore_discovery_type(u16 type_id)
50 {
51 struct rb_node *node = rb_find(&type_id, &discovery_tables, __type_cmp);
52
53 return (node) ? __node_2_type(node) : NULL;
54 }
55
__type_less(struct rb_node * a,const struct rb_node * b)56 static inline bool __type_less(struct rb_node *a, const struct rb_node *b)
57 {
58 return (__node_2_type(a)->type < __node_2_type(b)->type);
59 }
60
61 static struct intel_uncore_discovery_type *
add_uncore_discovery_type(struct uncore_unit_discovery * unit)62 add_uncore_discovery_type(struct uncore_unit_discovery *unit)
63 {
64 struct intel_uncore_discovery_type *type;
65
66 if (unit->access_type >= UNCORE_ACCESS_MAX) {
67 pr_warn("Unsupported access type %d\n", unit->access_type);
68 return NULL;
69 }
70
71 type = kzalloc_obj(struct intel_uncore_discovery_type);
72 if (!type)
73 return NULL;
74
75 type->units = RB_ROOT;
76
77 type->access_type = unit->access_type;
78 num_discovered_types[type->access_type]++;
79 type->type = unit->box_type;
80
81 rb_add(&type->node, &discovery_tables, __type_less);
82
83 return type;
84 }
85
86 static struct intel_uncore_discovery_type *
get_uncore_discovery_type(struct uncore_unit_discovery * unit)87 get_uncore_discovery_type(struct uncore_unit_discovery *unit)
88 {
89 struct intel_uncore_discovery_type *type;
90
91 type = search_uncore_discovery_type(unit->box_type);
92 if (type)
93 return type;
94
95 return add_uncore_discovery_type(unit);
96 }
97
pmu_idx_cmp(const void * key,const struct rb_node * b)98 static inline int pmu_idx_cmp(const void *key, const struct rb_node *b)
99 {
100 const struct intel_uncore_discovery_unit *unit;
101 const unsigned int *id = key;
102
103 unit = rb_entry(b, struct intel_uncore_discovery_unit, node);
104
105 if (unit->pmu_idx > *id)
106 return -1;
107 else if (unit->pmu_idx < *id)
108 return 1;
109
110 return 0;
111 }
112
113 static struct intel_uncore_discovery_unit *
intel_uncore_find_discovery_unit(struct rb_root * units,int die,unsigned int pmu_idx)114 intel_uncore_find_discovery_unit(struct rb_root *units, int die,
115 unsigned int pmu_idx)
116 {
117 struct intel_uncore_discovery_unit *unit;
118 struct rb_node *pos;
119
120 if (!units)
121 return NULL;
122
123 pos = rb_find_first(&pmu_idx, units, pmu_idx_cmp);
124 if (!pos)
125 return NULL;
126 unit = rb_entry(pos, struct intel_uncore_discovery_unit, node);
127
128 if (die < 0)
129 return unit;
130
131 for (; pos; pos = rb_next(pos)) {
132 unit = rb_entry(pos, struct intel_uncore_discovery_unit, node);
133
134 if (unit->pmu_idx != pmu_idx)
135 break;
136
137 if (unit->die == die)
138 return unit;
139 }
140
141 return NULL;
142 }
143
intel_uncore_find_discovery_unit_id(struct rb_root * units,int die,unsigned int pmu_idx)144 int intel_uncore_find_discovery_unit_id(struct rb_root *units, int die,
145 unsigned int pmu_idx)
146 {
147 struct intel_uncore_discovery_unit *unit;
148
149 unit = intel_uncore_find_discovery_unit(units, die, pmu_idx);
150 if (unit)
151 return unit->id;
152
153 return -1;
154 }
155
unit_less(struct rb_node * a,const struct rb_node * b)156 static inline bool unit_less(struct rb_node *a, const struct rb_node *b)
157 {
158 const struct intel_uncore_discovery_unit *a_node, *b_node;
159
160 a_node = rb_entry(a, struct intel_uncore_discovery_unit, node);
161 b_node = rb_entry(b, struct intel_uncore_discovery_unit, node);
162
163 if (a_node->pmu_idx < b_node->pmu_idx)
164 return true;
165 if (a_node->pmu_idx > b_node->pmu_idx)
166 return false;
167
168 if (a_node->die < b_node->die)
169 return true;
170 if (a_node->die > b_node->die)
171 return false;
172
173 return 0;
174 }
175
176 static inline struct intel_uncore_discovery_unit *
uncore_find_unit(struct rb_root * root,unsigned int id)177 uncore_find_unit(struct rb_root *root, unsigned int id)
178 {
179 struct intel_uncore_discovery_unit *unit;
180 struct rb_node *node;
181
182 for (node = rb_first(root); node; node = rb_next(node)) {
183 unit = rb_entry(node, struct intel_uncore_discovery_unit, node);
184 if (unit->id == id)
185 return unit;
186 }
187
188 return NULL;
189 }
190
uncore_find_add_unit(struct intel_uncore_discovery_unit * node,struct rb_root * root,u16 * num_units)191 void uncore_find_add_unit(struct intel_uncore_discovery_unit *node,
192 struct rb_root *root, u16 *num_units)
193 {
194 struct intel_uncore_discovery_unit *unit = uncore_find_unit(root, node->id);
195
196 if (unit)
197 node->pmu_idx = unit->pmu_idx;
198 else if (num_units)
199 node->pmu_idx = (*num_units)++;
200
201 rb_add(&node->node, root, unit_less);
202 }
203
204 static void
uncore_insert_box_info(struct uncore_unit_discovery * unit,int die)205 uncore_insert_box_info(struct uncore_unit_discovery *unit,
206 int die)
207 {
208 struct intel_uncore_discovery_unit *node;
209 struct intel_uncore_discovery_type *type;
210
211 if (!unit->ctl || !unit->ctl_offset || !unit->ctr_offset) {
212 pr_info("Invalid address is detected for uncore type %d box %d, "
213 "Disable the uncore unit.\n",
214 unit->box_type, unit->box_id);
215 return;
216 }
217
218 node = kzalloc_obj(*node);
219 if (!node)
220 return;
221
222 node->die = die;
223 node->id = unit->box_id;
224 node->addr = unit->ctl;
225
226 type = get_uncore_discovery_type(unit);
227 if (!type) {
228 kfree(node);
229 return;
230 }
231
232 uncore_find_add_unit(node, &type->units, &type->num_units);
233
234 /* Store generic information for the first box */
235 if (type->num_units == 1) {
236 type->num_counters = unit->num_regs;
237 type->counter_width = unit->bit_width;
238 type->ctl_offset = unit->ctl_offset;
239 type->ctr_offset = unit->ctr_offset;
240 }
241 }
242
243 static bool
uncore_ignore_unit(struct uncore_unit_discovery * unit,struct uncore_discovery_domain * domain)244 uncore_ignore_unit(struct uncore_unit_discovery *unit,
245 struct uncore_discovery_domain *domain)
246 {
247 int i;
248
249 if (!domain || !domain->units_ignore)
250 return false;
251
252 for (i = 0; domain->units_ignore[i] != UNCORE_IGNORE_END ; i++) {
253 if (unit->box_type == domain->units_ignore[i])
254 return true;
255 }
256
257 return false;
258 }
259
__parse_discovery_table(struct uncore_discovery_domain * domain,resource_size_t addr,int die,bool * parsed)260 static int __parse_discovery_table(struct uncore_discovery_domain *domain,
261 resource_size_t addr, int die, bool *parsed)
262 {
263 struct uncore_global_discovery global;
264 struct uncore_unit_discovery unit;
265 void __iomem *io_addr;
266 unsigned long size;
267 int ret = 0;
268 int i;
269
270 size = UNCORE_DISCOVERY_GLOBAL_MAP_SIZE;
271 io_addr = ioremap(addr, size);
272 if (!io_addr)
273 return -ENOMEM;
274
275 /* Read Global Discovery State */
276 memcpy_fromio(&global, io_addr, sizeof(struct uncore_global_discovery));
277 iounmap(io_addr);
278
279 if (uncore_discovery_invalid_unit(global)) {
280 pr_info("Invalid Global Discovery State: 0x%llx 0x%llx 0x%llx\n",
281 global.table1, global.ctl, global.table3);
282 return -EINVAL;
283 }
284
285 size = (1 + global.max_units) * global.stride * 8;
286 io_addr = ioremap(addr, size);
287 if (!io_addr)
288 return -ENOMEM;
289
290 if (domain->global_init && domain->global_init(global.ctl)) {
291 ret = -ENODEV;
292 goto out;
293 }
294
295 /* Parsing Unit Discovery State */
296 for (i = 0; i < global.max_units; i++) {
297 memcpy_fromio(&unit, io_addr + (i + 1) * (global.stride * 8),
298 sizeof(struct uncore_unit_discovery));
299
300 if (uncore_discovery_invalid_unit(unit))
301 continue;
302
303 if (unit.access_type >= UNCORE_ACCESS_MAX)
304 continue;
305
306 if (uncore_ignore_unit(&unit, domain))
307 continue;
308
309 uncore_insert_box_info(&unit, die);
310 }
311
312 *parsed = true;
313
314 out:
315 iounmap(io_addr);
316 return ret;
317 }
318
parse_discovery_table(struct uncore_discovery_domain * domain,struct pci_dev * dev,int die,u32 bar_offset,bool * parsed)319 static int parse_discovery_table(struct uncore_discovery_domain *domain,
320 struct pci_dev *dev, int die,
321 u32 bar_offset, bool *parsed)
322 {
323 resource_size_t addr;
324 u32 val;
325
326 pci_read_config_dword(dev, bar_offset, &val);
327
328 if (val & ~PCI_BASE_ADDRESS_MEM_MASK & ~PCI_BASE_ADDRESS_MEM_TYPE_64)
329 return -EINVAL;
330
331 addr = (resource_size_t)(val & PCI_BASE_ADDRESS_MEM_MASK);
332 #ifdef CONFIG_PHYS_ADDR_T_64BIT
333 if ((val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
334 u32 val2;
335
336 pci_read_config_dword(dev, bar_offset + 4, &val2);
337 addr |= ((resource_size_t)val2) << 32;
338 }
339 #endif
340
341 return __parse_discovery_table(domain, addr, die, parsed);
342 }
343
uncore_discovery_pci(struct uncore_discovery_domain * domain)344 static bool uncore_discovery_pci(struct uncore_discovery_domain *domain)
345 {
346 u32 device, val, entry_id, bar_offset;
347 int die, dvsec = 0, ret = true;
348 struct pci_dev *dev = NULL;
349 bool parsed = false;
350
351 device = domain->discovery_base;
352
353 /*
354 * Start a new search and iterates through the list of
355 * the discovery table devices.
356 */
357 while ((dev = pci_get_device(PCI_VENDOR_ID_INTEL, device, dev)) != NULL) {
358 while ((dvsec = pci_find_next_ext_capability(dev, dvsec, UNCORE_EXT_CAP_ID_DISCOVERY))) {
359 pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC_OFFSET, &val);
360 entry_id = val & UNCORE_DISCOVERY_DVSEC_ID_MASK;
361 if (entry_id != UNCORE_DISCOVERY_DVSEC_ID_PMON)
362 continue;
363
364 pci_read_config_dword(dev, dvsec + UNCORE_DISCOVERY_DVSEC2_OFFSET, &val);
365
366 if (val & ~UNCORE_DISCOVERY_DVSEC2_BIR_MASK) {
367 ret = false;
368 goto err;
369 }
370 bar_offset = UNCORE_DISCOVERY_BIR_BASE +
371 (val & UNCORE_DISCOVERY_DVSEC2_BIR_MASK) * UNCORE_DISCOVERY_BIR_STEP;
372
373 die = get_device_die_id(dev);
374 if ((die < 0) || (die >= uncore_max_dies()))
375 continue;
376
377 parse_discovery_table(domain, dev, die, bar_offset, &parsed);
378 }
379 }
380
381 /* None of the discovery tables are available */
382 if (!parsed)
383 ret = false;
384 err:
385 pci_dev_put(dev);
386
387 return ret;
388 }
389
uncore_discovery_msr(struct uncore_discovery_domain * domain)390 static bool uncore_discovery_msr(struct uncore_discovery_domain *domain)
391 {
392 unsigned long *die_mask;
393 bool parsed = false;
394 int cpu, die;
395 u64 base;
396
397 die_mask = kcalloc(BITS_TO_LONGS(uncore_max_dies()),
398 sizeof(unsigned long), GFP_KERNEL);
399 if (!die_mask)
400 return false;
401
402 cpus_read_lock();
403 for_each_online_cpu(cpu) {
404 die = topology_logical_die_id(cpu);
405 if (__test_and_set_bit(die, die_mask))
406 continue;
407
408 if (rdmsrq_safe_on_cpu(cpu, domain->discovery_base, &base))
409 continue;
410
411 if (!base)
412 continue;
413
414 __parse_discovery_table(domain, base, die, &parsed);
415 }
416
417 cpus_read_unlock();
418
419 kfree(die_mask);
420 return parsed;
421 }
422
uncore_discovery(struct uncore_plat_init * init)423 bool uncore_discovery(struct uncore_plat_init *init)
424 {
425 struct uncore_discovery_domain *domain;
426 bool ret = false;
427 int i;
428
429 for (i = 0; i < UNCORE_DISCOVERY_DOMAINS; i++) {
430 domain = &init->domain[i];
431 if (domain->discovery_base) {
432 if (!domain->base_is_pci)
433 ret |= uncore_discovery_msr(domain);
434 else
435 ret |= uncore_discovery_pci(domain);
436 }
437 }
438
439 return ret;
440 }
441
intel_uncore_clear_discovery_tables(void)442 void intel_uncore_clear_discovery_tables(void)
443 {
444 struct intel_uncore_discovery_type *type, *next;
445 struct intel_uncore_discovery_unit *pos;
446 struct rb_node *node;
447
448 rbtree_postorder_for_each_entry_safe(type, next, &discovery_tables, node) {
449 while (!RB_EMPTY_ROOT(&type->units)) {
450 node = rb_first(&type->units);
451 pos = rb_entry(node, struct intel_uncore_discovery_unit, node);
452 rb_erase(node, &type->units);
453 kfree(pos);
454 }
455 kfree(type);
456 }
457 }
458
459 DEFINE_UNCORE_FORMAT_ATTR(event, event, "config:0-7");
460 DEFINE_UNCORE_FORMAT_ATTR(umask, umask, "config:8-15");
461 DEFINE_UNCORE_FORMAT_ATTR(edge, edge, "config:18");
462 DEFINE_UNCORE_FORMAT_ATTR(inv, inv, "config:23");
463 DEFINE_UNCORE_FORMAT_ATTR(thresh, thresh, "config:24-31");
464
465 static struct attribute *generic_uncore_formats_attr[] = {
466 &format_attr_event.attr,
467 &format_attr_umask.attr,
468 &format_attr_edge.attr,
469 &format_attr_inv.attr,
470 &format_attr_thresh.attr,
471 NULL,
472 };
473
474 static const struct attribute_group generic_uncore_format_group = {
475 .name = "format",
476 .attrs = generic_uncore_formats_attr,
477 };
478
intel_generic_uncore_box_ctl(struct intel_uncore_box * box)479 static u64 intel_generic_uncore_box_ctl(struct intel_uncore_box *box)
480 {
481 struct intel_uncore_discovery_unit *unit;
482
483 unit = intel_uncore_find_discovery_unit(box->pmu->type->boxes,
484 -1, box->pmu->pmu_idx);
485 if (WARN_ON_ONCE(!unit))
486 return 0;
487
488 return unit->addr;
489 }
490
intel_generic_uncore_msr_init_box(struct intel_uncore_box * box)491 void intel_generic_uncore_msr_init_box(struct intel_uncore_box *box)
492 {
493 wrmsrq(intel_generic_uncore_box_ctl(box), GENERIC_PMON_BOX_CTL_INT);
494 }
495
intel_generic_uncore_msr_disable_box(struct intel_uncore_box * box)496 void intel_generic_uncore_msr_disable_box(struct intel_uncore_box *box)
497 {
498 wrmsrq(intel_generic_uncore_box_ctl(box), GENERIC_PMON_BOX_CTL_FRZ);
499 }
500
intel_generic_uncore_msr_enable_box(struct intel_uncore_box * box)501 void intel_generic_uncore_msr_enable_box(struct intel_uncore_box *box)
502 {
503 wrmsrq(intel_generic_uncore_box_ctl(box), 0);
504 }
505
intel_generic_uncore_msr_enable_event(struct intel_uncore_box * box,struct perf_event * event)506 static void intel_generic_uncore_msr_enable_event(struct intel_uncore_box *box,
507 struct perf_event *event)
508 {
509 struct hw_perf_event *hwc = &event->hw;
510
511 wrmsrq(hwc->config_base, hwc->config);
512 }
513
intel_generic_uncore_msr_disable_event(struct intel_uncore_box * box,struct perf_event * event)514 static void intel_generic_uncore_msr_disable_event(struct intel_uncore_box *box,
515 struct perf_event *event)
516 {
517 struct hw_perf_event *hwc = &event->hw;
518
519 wrmsrq(hwc->config_base, 0);
520 }
521
522 static struct intel_uncore_ops generic_uncore_msr_ops = {
523 .init_box = intel_generic_uncore_msr_init_box,
524 .disable_box = intel_generic_uncore_msr_disable_box,
525 .enable_box = intel_generic_uncore_msr_enable_box,
526 .disable_event = intel_generic_uncore_msr_disable_event,
527 .enable_event = intel_generic_uncore_msr_enable_event,
528 .read_counter = uncore_msr_read_counter,
529 };
530
intel_generic_uncore_assign_hw_event(struct perf_event * event,struct intel_uncore_box * box)531 bool intel_generic_uncore_assign_hw_event(struct perf_event *event,
532 struct intel_uncore_box *box)
533 {
534 struct hw_perf_event *hwc = &event->hw;
535 u64 box_ctl;
536
537 if (!box->pmu->type->boxes)
538 return false;
539
540 if (box->io_addr) {
541 hwc->config_base = uncore_pci_event_ctl(box, hwc->idx);
542 hwc->event_base = uncore_pci_perf_ctr(box, hwc->idx);
543 return true;
544 }
545
546 box_ctl = intel_generic_uncore_box_ctl(box);
547 if (!box_ctl)
548 return false;
549
550 if (box->pci_dev) {
551 box_ctl = UNCORE_DISCOVERY_PCI_BOX_CTRL(box_ctl);
552 hwc->config_base = box_ctl + uncore_pci_event_ctl(box, hwc->idx);
553 hwc->event_base = box_ctl + uncore_pci_perf_ctr(box, hwc->idx);
554 return true;
555 }
556
557 hwc->config_base = box_ctl + box->pmu->type->event_ctl + hwc->idx;
558 hwc->event_base = box_ctl + box->pmu->type->perf_ctr + hwc->idx;
559
560 return true;
561 }
562
intel_pci_uncore_box_ctl(struct intel_uncore_box * box)563 static inline int intel_pci_uncore_box_ctl(struct intel_uncore_box *box)
564 {
565 return UNCORE_DISCOVERY_PCI_BOX_CTRL(intel_generic_uncore_box_ctl(box));
566 }
567
intel_generic_uncore_pci_init_box(struct intel_uncore_box * box)568 void intel_generic_uncore_pci_init_box(struct intel_uncore_box *box)
569 {
570 struct pci_dev *pdev = box->pci_dev;
571 int box_ctl = intel_pci_uncore_box_ctl(box);
572
573 __set_bit(UNCORE_BOX_FLAG_CTL_OFFS8, &box->flags);
574 pci_write_config_dword(pdev, box_ctl, GENERIC_PMON_BOX_CTL_INT);
575 }
576
intel_generic_uncore_pci_disable_box(struct intel_uncore_box * box)577 void intel_generic_uncore_pci_disable_box(struct intel_uncore_box *box)
578 {
579 struct pci_dev *pdev = box->pci_dev;
580 int box_ctl = intel_pci_uncore_box_ctl(box);
581
582 pci_write_config_dword(pdev, box_ctl, GENERIC_PMON_BOX_CTL_FRZ);
583 }
584
intel_generic_uncore_pci_enable_box(struct intel_uncore_box * box)585 void intel_generic_uncore_pci_enable_box(struct intel_uncore_box *box)
586 {
587 struct pci_dev *pdev = box->pci_dev;
588 int box_ctl = intel_pci_uncore_box_ctl(box);
589
590 pci_write_config_dword(pdev, box_ctl, 0);
591 }
592
intel_generic_uncore_pci_enable_event(struct intel_uncore_box * box,struct perf_event * event)593 static void intel_generic_uncore_pci_enable_event(struct intel_uncore_box *box,
594 struct perf_event *event)
595 {
596 struct pci_dev *pdev = box->pci_dev;
597 struct hw_perf_event *hwc = &event->hw;
598
599 pci_write_config_dword(pdev, hwc->config_base, hwc->config);
600 }
601
intel_generic_uncore_pci_disable_event(struct intel_uncore_box * box,struct perf_event * event)602 void intel_generic_uncore_pci_disable_event(struct intel_uncore_box *box,
603 struct perf_event *event)
604 {
605 struct pci_dev *pdev = box->pci_dev;
606 struct hw_perf_event *hwc = &event->hw;
607
608 pci_write_config_dword(pdev, hwc->config_base, 0);
609 }
610
intel_generic_uncore_pci_read_counter(struct intel_uncore_box * box,struct perf_event * event)611 u64 intel_generic_uncore_pci_read_counter(struct intel_uncore_box *box,
612 struct perf_event *event)
613 {
614 struct pci_dev *pdev = box->pci_dev;
615 struct hw_perf_event *hwc = &event->hw;
616 u64 count = 0;
617
618 pci_read_config_dword(pdev, hwc->event_base, (u32 *)&count);
619 pci_read_config_dword(pdev, hwc->event_base + 4, (u32 *)&count + 1);
620
621 return count;
622 }
623
624 static struct intel_uncore_ops generic_uncore_pci_ops = {
625 .init_box = intel_generic_uncore_pci_init_box,
626 .disable_box = intel_generic_uncore_pci_disable_box,
627 .enable_box = intel_generic_uncore_pci_enable_box,
628 .disable_event = intel_generic_uncore_pci_disable_event,
629 .enable_event = intel_generic_uncore_pci_enable_event,
630 .read_counter = intel_generic_uncore_pci_read_counter,
631 };
632
633 #define UNCORE_GENERIC_MMIO_SIZE 0x4000
634
intel_generic_uncore_mmio_init_box(struct intel_uncore_box * box)635 void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)
636 {
637 static struct intel_uncore_discovery_unit *unit;
638 struct intel_uncore_type *type = box->pmu->type;
639 resource_size_t addr;
640
641 unit = intel_uncore_find_discovery_unit(type->boxes, box->dieid, box->pmu->pmu_idx);
642 if (!unit) {
643 pr_warn("Uncore type %d id %d: Cannot find box control address.\n",
644 type->type_id, box->pmu->pmu_idx);
645 return;
646 }
647
648 if (!unit->addr) {
649 pr_warn("Uncore type %d box %d: Invalid box control address.\n",
650 type->type_id, unit->id);
651 return;
652 }
653
654 addr = unit->addr;
655 box->io_addr = ioremap(addr, type->mmio_map_size);
656 if (!box->io_addr) {
657 pr_warn("Uncore type %d box %d: ioremap error for 0x%llx.\n",
658 type->type_id, unit->id, (unsigned long long)addr);
659 return;
660 }
661
662 writel(GENERIC_PMON_BOX_CTL_INT, box->io_addr);
663 }
664
intel_generic_uncore_mmio_disable_box(struct intel_uncore_box * box)665 void intel_generic_uncore_mmio_disable_box(struct intel_uncore_box *box)
666 {
667 if (!box->io_addr)
668 return;
669
670 writel(GENERIC_PMON_BOX_CTL_FRZ, box->io_addr);
671 }
672
intel_generic_uncore_mmio_enable_box(struct intel_uncore_box * box)673 void intel_generic_uncore_mmio_enable_box(struct intel_uncore_box *box)
674 {
675 if (!box->io_addr)
676 return;
677
678 writel(0, box->io_addr);
679 }
680
intel_generic_uncore_mmio_enable_event(struct intel_uncore_box * box,struct perf_event * event)681 void intel_generic_uncore_mmio_enable_event(struct intel_uncore_box *box,
682 struct perf_event *event)
683 {
684 struct hw_perf_event *hwc = &event->hw;
685
686 if (!box->io_addr)
687 return;
688
689 writel(hwc->config, box->io_addr + hwc->config_base);
690 }
691
intel_generic_uncore_mmio_disable_event(struct intel_uncore_box * box,struct perf_event * event)692 void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,
693 struct perf_event *event)
694 {
695 struct hw_perf_event *hwc = &event->hw;
696
697 if (!box->io_addr)
698 return;
699
700 writel(0, box->io_addr + hwc->config_base);
701 }
702
703 static struct intel_uncore_ops generic_uncore_mmio_ops = {
704 .init_box = intel_generic_uncore_mmio_init_box,
705 .exit_box = uncore_mmio_exit_box,
706 .disable_box = intel_generic_uncore_mmio_disable_box,
707 .enable_box = intel_generic_uncore_mmio_enable_box,
708 .disable_event = intel_generic_uncore_mmio_disable_event,
709 .enable_event = intel_generic_uncore_mmio_enable_event,
710 .read_counter = uncore_mmio_read_counter,
711 };
712
uncore_update_uncore_type(enum uncore_access_type type_id,struct intel_uncore_type * uncore,struct intel_uncore_discovery_type * type)713 static bool uncore_update_uncore_type(enum uncore_access_type type_id,
714 struct intel_uncore_type *uncore,
715 struct intel_uncore_discovery_type *type)
716 {
717 uncore->type_id = type->type;
718 uncore->num_counters = type->num_counters;
719 uncore->perf_ctr_bits = type->counter_width;
720 uncore->perf_ctr = (unsigned int)type->ctr_offset;
721 uncore->event_ctl = (unsigned int)type->ctl_offset;
722 uncore->boxes = &type->units;
723 uncore->num_boxes = type->num_units;
724
725 switch (type_id) {
726 case UNCORE_ACCESS_MSR:
727 uncore->ops = &generic_uncore_msr_ops;
728 break;
729 case UNCORE_ACCESS_PCI:
730 uncore->ops = &generic_uncore_pci_ops;
731 break;
732 case UNCORE_ACCESS_MMIO:
733 uncore->ops = &generic_uncore_mmio_ops;
734 uncore->mmio_map_size = UNCORE_GENERIC_MMIO_SIZE;
735 break;
736 default:
737 return false;
738 }
739
740 return true;
741 }
742
743 struct intel_uncore_type **
intel_uncore_generic_init_uncores(enum uncore_access_type type_id,int num_extra)744 intel_uncore_generic_init_uncores(enum uncore_access_type type_id, int num_extra)
745 {
746 struct intel_uncore_discovery_type *type;
747 struct intel_uncore_type **uncores;
748 struct intel_uncore_type *uncore;
749 struct rb_node *node;
750 int i = 0;
751
752 uncores = kzalloc_objs(struct intel_uncore_type *,
753 num_discovered_types[type_id] + num_extra + 1);
754 if (!uncores)
755 return empty_uncore;
756
757 for (node = rb_first(&discovery_tables); node; node = rb_next(node)) {
758 type = rb_entry(node, struct intel_uncore_discovery_type, node);
759 if (type->access_type != type_id)
760 continue;
761
762 uncore = kzalloc_obj(struct intel_uncore_type);
763 if (!uncore)
764 break;
765
766 uncore->event_mask = GENERIC_PMON_RAW_EVENT_MASK;
767 uncore->format_group = &generic_uncore_format_group;
768
769 if (!uncore_update_uncore_type(type_id, uncore, type)) {
770 kfree(uncore);
771 continue;
772 }
773 uncores[i++] = uncore;
774 }
775
776 return uncores;
777 }
778
intel_uncore_generic_uncore_cpu_init(void)779 void intel_uncore_generic_uncore_cpu_init(void)
780 {
781 uncore_msr_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_MSR, 0);
782 }
783
intel_uncore_generic_uncore_pci_init(void)784 int intel_uncore_generic_uncore_pci_init(void)
785 {
786 uncore_pci_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_PCI, 0);
787
788 return 0;
789 }
790
intel_uncore_generic_uncore_mmio_init(void)791 void intel_uncore_generic_uncore_mmio_init(void)
792 {
793 uncore_mmio_uncores = intel_uncore_generic_init_uncores(UNCORE_ACCESS_MMIO, 0);
794 }
795