1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * In-Memory Collection (IMC) Performance Monitor counter support.
4 *
5 * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
6 * (C) 2017 Anju T Sudhakar, IBM Corporation.
7 * (C) 2017 Hemant K Shaw, IBM Corporation.
8 */
9 #include <linux/perf_event.h>
10 #include <linux/slab.h>
11 #include <asm/opal.h>
12 #include <asm/imc-pmu.h>
13 #include <asm/cputhreads.h>
14 #include <asm/smp.h>
15 #include <linux/string.h>
16 #include <linux/spinlock.h>
17
18 /* Nest IMC data structures and variables */
19
20 /*
21 * Used to avoid races in counting the nest-pmu units during hotplug
22 * register and unregister
23 */
24 static DEFINE_SPINLOCK(nest_init_lock);
25 static DEFINE_PER_CPU(struct imc_pmu_ref *, local_nest_imc_refc);
26 static struct imc_pmu **per_nest_pmu_arr;
27 static cpumask_t nest_imc_cpumask;
28 static struct imc_pmu_ref *nest_imc_refc;
29 static int nest_pmus;
30
31 /* Core IMC data structures and variables */
32
33 static cpumask_t core_imc_cpumask;
34 static struct imc_pmu_ref *core_imc_refc;
35 static struct imc_pmu *core_imc_pmu;
36
37 /* Thread IMC data structures and variables */
38
39 static DEFINE_PER_CPU(u64 *, thread_imc_mem);
40 static struct imc_pmu *thread_imc_pmu;
41 static int thread_imc_mem_size;
42
43 /* Trace IMC data structures */
44 static DEFINE_PER_CPU(u64 *, trace_imc_mem);
45 static struct imc_pmu_ref *trace_imc_refc;
46 static int trace_imc_mem_size;
47
48 /*
49 * Global data structure used to avoid races between thread,
50 * core and trace-imc
51 */
52 static struct imc_pmu_ref imc_global_refc = {
53 .lock = __SPIN_LOCK_INITIALIZER(imc_global_refc.lock),
54 .id = 0,
55 .refc = 0,
56 };
57
imc_event_to_pmu(struct perf_event * event)58 static struct imc_pmu *imc_event_to_pmu(struct perf_event *event)
59 {
60 return container_of(event->pmu, struct imc_pmu, pmu);
61 }
62
63 PMU_FORMAT_ATTR(event, "config:0-61");
64 PMU_FORMAT_ATTR(offset, "config:0-31");
65 PMU_FORMAT_ATTR(rvalue, "config:32");
66 PMU_FORMAT_ATTR(mode, "config:33-40");
67 static struct attribute *imc_format_attrs[] = {
68 &format_attr_event.attr,
69 &format_attr_offset.attr,
70 &format_attr_rvalue.attr,
71 &format_attr_mode.attr,
72 NULL,
73 };
74
75 static struct attribute_group imc_format_group = {
76 .name = "format",
77 .attrs = imc_format_attrs,
78 };
79
80 /* Format attribute for imc trace-mode */
81 PMU_FORMAT_ATTR(cpmc_reserved, "config:0-19");
82 PMU_FORMAT_ATTR(cpmc_event, "config:20-27");
83 PMU_FORMAT_ATTR(cpmc_samplesel, "config:28-29");
84 PMU_FORMAT_ATTR(cpmc_load, "config:30-61");
85 static struct attribute *trace_imc_format_attrs[] = {
86 &format_attr_event.attr,
87 &format_attr_cpmc_reserved.attr,
88 &format_attr_cpmc_event.attr,
89 &format_attr_cpmc_samplesel.attr,
90 &format_attr_cpmc_load.attr,
91 NULL,
92 };
93
94 static struct attribute_group trace_imc_format_group = {
95 .name = "format",
96 .attrs = trace_imc_format_attrs,
97 };
98
99 /* Get the cpumask printed to a buffer "buf" */
imc_pmu_cpumask_get_attr(struct device * dev,struct device_attribute * attr,char * buf)100 static ssize_t imc_pmu_cpumask_get_attr(struct device *dev,
101 struct device_attribute *attr,
102 char *buf)
103 {
104 struct pmu *pmu = dev_get_drvdata(dev);
105 struct imc_pmu *imc_pmu = container_of(pmu, struct imc_pmu, pmu);
106 cpumask_t *active_mask;
107
108 switch(imc_pmu->domain){
109 case IMC_DOMAIN_NEST:
110 active_mask = &nest_imc_cpumask;
111 break;
112 case IMC_DOMAIN_CORE:
113 active_mask = &core_imc_cpumask;
114 break;
115 default:
116 return 0;
117 }
118
119 return cpumap_print_to_pagebuf(true, buf, active_mask);
120 }
121
122 static DEVICE_ATTR(cpumask, S_IRUGO, imc_pmu_cpumask_get_attr, NULL);
123
124 static struct attribute *imc_pmu_cpumask_attrs[] = {
125 &dev_attr_cpumask.attr,
126 NULL,
127 };
128
129 static struct attribute_group imc_pmu_cpumask_attr_group = {
130 .attrs = imc_pmu_cpumask_attrs,
131 };
132
133 /* device_str_attr_create : Populate event "name" and string "str" in attribute */
device_str_attr_create(const char * name,const char * str)134 static struct attribute *device_str_attr_create(const char *name, const char *str)
135 {
136 struct perf_pmu_events_attr *attr;
137
138 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
139 if (!attr)
140 return NULL;
141 sysfs_attr_init(&attr->attr.attr);
142
143 attr->event_str = str;
144 attr->attr.attr.name = name;
145 attr->attr.attr.mode = 0444;
146 attr->attr.show = perf_event_sysfs_show;
147
148 return &attr->attr.attr;
149 }
150
imc_parse_event(struct device_node * np,const char * scale,const char * unit,const char * prefix,u32 base,struct imc_events * event)151 static int imc_parse_event(struct device_node *np, const char *scale,
152 const char *unit, const char *prefix,
153 u32 base, struct imc_events *event)
154 {
155 const char *s;
156 u32 reg;
157
158 if (of_property_read_u32(np, "reg", ®))
159 goto error;
160 /* Add the base_reg value to the "reg" */
161 event->value = base + reg;
162
163 if (of_property_read_string(np, "event-name", &s))
164 goto error;
165
166 event->name = kasprintf(GFP_KERNEL, "%s%s", prefix, s);
167 if (!event->name)
168 goto error;
169
170 if (of_property_read_string(np, "scale", &s))
171 s = scale;
172
173 if (s) {
174 event->scale = kstrdup(s, GFP_KERNEL);
175 if (!event->scale)
176 goto error;
177 }
178
179 if (of_property_read_string(np, "unit", &s))
180 s = unit;
181
182 if (s) {
183 event->unit = kstrdup(s, GFP_KERNEL);
184 if (!event->unit)
185 goto error;
186 }
187
188 return 0;
189 error:
190 kfree(event->unit);
191 kfree(event->scale);
192 kfree(event->name);
193 return -EINVAL;
194 }
195
196 /*
197 * imc_free_events: Function to cleanup the events list, having
198 * "nr_entries".
199 */
imc_free_events(struct imc_events * events,int nr_entries)200 static void imc_free_events(struct imc_events *events, int nr_entries)
201 {
202 int i;
203
204 /* Nothing to clean, return */
205 if (!events)
206 return;
207 for (i = 0; i < nr_entries; i++) {
208 kfree(events[i].unit);
209 kfree(events[i].scale);
210 kfree(events[i].name);
211 }
212
213 kfree(events);
214 }
215
216 /*
217 * update_events_in_group: Update the "events" information in an attr_group
218 * and assign the attr_group to the pmu "pmu".
219 */
update_events_in_group(struct device_node * node,struct imc_pmu * pmu)220 static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
221 {
222 struct attribute_group *attr_group;
223 struct attribute **attrs, *dev_str;
224 struct device_node *np, *pmu_events;
225 u32 handle, base_reg;
226 int i = 0, j = 0, ct, ret;
227 const char *prefix, *g_scale, *g_unit;
228 const char *ev_val_str, *ev_scale_str, *ev_unit_str;
229
230 if (!of_property_read_u32(node, "events", &handle))
231 pmu_events = of_find_node_by_phandle(handle);
232 else
233 return 0;
234
235 /* Did not find any node with a given phandle */
236 if (!pmu_events)
237 return 0;
238
239 /* Get a count of number of child nodes */
240 ct = of_get_child_count(pmu_events);
241
242 /* Get the event prefix */
243 if (of_property_read_string(node, "events-prefix", &prefix))
244 return 0;
245
246 /* Get a global unit and scale data if available */
247 if (of_property_read_string(node, "scale", &g_scale))
248 g_scale = NULL;
249
250 if (of_property_read_string(node, "unit", &g_unit))
251 g_unit = NULL;
252
253 /* "reg" property gives out the base offset of the counters data */
254 of_property_read_u32(node, "reg", &base_reg);
255
256 /* Allocate memory for the events */
257 pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
258 if (!pmu->events)
259 return -ENOMEM;
260
261 ct = 0;
262 /* Parse the events and update the struct */
263 for_each_child_of_node(pmu_events, np) {
264 ret = imc_parse_event(np, g_scale, g_unit, prefix, base_reg, &pmu->events[ct]);
265 if (!ret)
266 ct++;
267 }
268
269 /* Allocate memory for attribute group */
270 attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
271 if (!attr_group) {
272 imc_free_events(pmu->events, ct);
273 return -ENOMEM;
274 }
275
276 /*
277 * Allocate memory for attributes.
278 * Since we have count of events for this pmu, we also allocate
279 * memory for the scale and unit attribute for now.
280 * "ct" has the total event structs added from the events-parent node.
281 * So allocate three times the "ct" (this includes event, event_scale and
282 * event_unit).
283 */
284 attrs = kcalloc(((ct * 3) + 1), sizeof(struct attribute *), GFP_KERNEL);
285 if (!attrs) {
286 kfree(attr_group);
287 imc_free_events(pmu->events, ct);
288 return -ENOMEM;
289 }
290
291 attr_group->name = "events";
292 attr_group->attrs = attrs;
293 do {
294 ev_val_str = kasprintf(GFP_KERNEL, "event=0x%x", pmu->events[i].value);
295 dev_str = device_str_attr_create(pmu->events[i].name, ev_val_str);
296 if (!dev_str)
297 continue;
298
299 attrs[j++] = dev_str;
300 if (pmu->events[i].scale) {
301 ev_scale_str = kasprintf(GFP_KERNEL, "%s.scale", pmu->events[i].name);
302 dev_str = device_str_attr_create(ev_scale_str, pmu->events[i].scale);
303 if (!dev_str)
304 continue;
305
306 attrs[j++] = dev_str;
307 }
308
309 if (pmu->events[i].unit) {
310 ev_unit_str = kasprintf(GFP_KERNEL, "%s.unit", pmu->events[i].name);
311 dev_str = device_str_attr_create(ev_unit_str, pmu->events[i].unit);
312 if (!dev_str)
313 continue;
314
315 attrs[j++] = dev_str;
316 }
317 } while (++i < ct);
318
319 /* Save the event attribute */
320 pmu->attr_groups[IMC_EVENT_ATTR] = attr_group;
321
322 return 0;
323 }
324
325 /* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */
get_nest_pmu_ref(int cpu)326 static struct imc_pmu_ref *get_nest_pmu_ref(int cpu)
327 {
328 return per_cpu(local_nest_imc_refc, cpu);
329 }
330
nest_change_cpu_context(int old_cpu,int new_cpu)331 static void nest_change_cpu_context(int old_cpu, int new_cpu)
332 {
333 struct imc_pmu **pn = per_nest_pmu_arr;
334
335 if (old_cpu < 0 || new_cpu < 0)
336 return;
337
338 while (*pn) {
339 perf_pmu_migrate_context(&(*pn)->pmu, old_cpu, new_cpu);
340 pn++;
341 }
342 }
343
ppc_nest_imc_cpu_offline(unsigned int cpu)344 static int ppc_nest_imc_cpu_offline(unsigned int cpu)
345 {
346 int nid, target = -1;
347 const struct cpumask *l_cpumask;
348 struct imc_pmu_ref *ref;
349
350 /*
351 * Check in the designated list for this cpu. Dont bother
352 * if not one of them.
353 */
354 if (!cpumask_test_and_clear_cpu(cpu, &nest_imc_cpumask))
355 return 0;
356
357 /*
358 * Check whether nest_imc is registered. We could end up here if the
359 * cpuhotplug callback registration fails. i.e, callback invokes the
360 * offline path for all successfully registered nodes. At this stage,
361 * nest_imc pmu will not be registered and we should return here.
362 *
363 * We return with a zero since this is not an offline failure. And
364 * cpuhp_setup_state() returns the actual failure reason to the caller,
365 * which in turn will call the cleanup routine.
366 */
367 if (!nest_pmus)
368 return 0;
369
370 /*
371 * Now that this cpu is one of the designated,
372 * find a next cpu a) which is online and b) in same chip.
373 */
374 nid = cpu_to_node(cpu);
375 l_cpumask = cpumask_of_node(nid);
376 target = cpumask_last(l_cpumask);
377
378 /*
379 * If this(target) is the last cpu in the cpumask for this chip,
380 * check for any possible online cpu in the chip.
381 */
382 if (unlikely(target == cpu))
383 target = cpumask_any_but(l_cpumask, cpu);
384
385 /*
386 * Update the cpumask with the target cpu and
387 * migrate the context if needed
388 */
389 if (target >= 0 && target < nr_cpu_ids) {
390 cpumask_set_cpu(target, &nest_imc_cpumask);
391 nest_change_cpu_context(cpu, target);
392 } else {
393 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
394 get_hard_smp_processor_id(cpu));
395 /*
396 * If this is the last cpu in this chip then, skip the reference
397 * count lock and make the reference count on this chip zero.
398 */
399 ref = get_nest_pmu_ref(cpu);
400 if (!ref)
401 return -EINVAL;
402
403 ref->refc = 0;
404 }
405 return 0;
406 }
407
ppc_nest_imc_cpu_online(unsigned int cpu)408 static int ppc_nest_imc_cpu_online(unsigned int cpu)
409 {
410 const struct cpumask *l_cpumask;
411 static struct cpumask tmp_mask;
412 int res;
413
414 /* Get the cpumask of this node */
415 l_cpumask = cpumask_of_node(cpu_to_node(cpu));
416
417 /*
418 * If this is not the first online CPU on this node, then
419 * just return.
420 */
421 if (cpumask_and(&tmp_mask, l_cpumask, &nest_imc_cpumask))
422 return 0;
423
424 /*
425 * If this is the first online cpu on this node
426 * disable the nest counters by making an OPAL call.
427 */
428 res = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
429 get_hard_smp_processor_id(cpu));
430 if (res)
431 return res;
432
433 /* Make this CPU the designated target for counter collection */
434 cpumask_set_cpu(cpu, &nest_imc_cpumask);
435 return 0;
436 }
437
nest_pmu_cpumask_init(void)438 static int nest_pmu_cpumask_init(void)
439 {
440 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE,
441 "perf/powerpc/imc:online",
442 ppc_nest_imc_cpu_online,
443 ppc_nest_imc_cpu_offline);
444 }
445
nest_imc_counters_release(struct perf_event * event)446 static void nest_imc_counters_release(struct perf_event *event)
447 {
448 int rc, node_id;
449 struct imc_pmu_ref *ref;
450
451 if (event->cpu < 0)
452 return;
453
454 node_id = cpu_to_node(event->cpu);
455
456 /*
457 * See if we need to disable the nest PMU.
458 * If no events are currently in use, then we have to take a
459 * lock to ensure that we don't race with another task doing
460 * enable or disable the nest counters.
461 */
462 ref = get_nest_pmu_ref(event->cpu);
463 if (!ref)
464 return;
465
466 /* Take the lock for this node and then decrement the reference count */
467 spin_lock(&ref->lock);
468 if (ref->refc == 0) {
469 /*
470 * The scenario where this is true is, when perf session is
471 * started, followed by offlining of all cpus in a given node.
472 *
473 * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline()
474 * function set the ref->count to zero, if the cpu which is
475 * about to offline is the last cpu in a given node and make
476 * an OPAL call to disable the engine in that node.
477 *
478 */
479 spin_unlock(&ref->lock);
480 return;
481 }
482 ref->refc--;
483 if (ref->refc == 0) {
484 rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
485 get_hard_smp_processor_id(event->cpu));
486 if (rc) {
487 spin_unlock(&ref->lock);
488 pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id);
489 return;
490 }
491 } else if (ref->refc < 0) {
492 WARN(1, "nest-imc: Invalid event reference count\n");
493 ref->refc = 0;
494 }
495 spin_unlock(&ref->lock);
496 }
497
nest_imc_event_init(struct perf_event * event)498 static int nest_imc_event_init(struct perf_event *event)
499 {
500 int chip_id, rc, node_id;
501 u32 l_config, config = event->attr.config;
502 struct imc_mem_info *pcni;
503 struct imc_pmu *pmu;
504 struct imc_pmu_ref *ref;
505 bool flag = false;
506
507 if (event->attr.type != event->pmu->type)
508 return -ENOENT;
509
510 /* Sampling not supported */
511 if (event->hw.sample_period)
512 return -EINVAL;
513
514 if (event->cpu < 0)
515 return -EINVAL;
516
517 pmu = imc_event_to_pmu(event);
518
519 /* Sanity check for config (event offset) */
520 if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)
521 return -EINVAL;
522
523 /*
524 * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
525 * Get the base memory addresss for this cpu.
526 */
527 chip_id = cpu_to_chip_id(event->cpu);
528
529 /* Return, if chip_id is not valid */
530 if (chip_id < 0)
531 return -ENODEV;
532
533 pcni = pmu->mem_info;
534 do {
535 if (pcni->id == chip_id) {
536 flag = true;
537 break;
538 }
539 pcni++;
540 } while (pcni->vbase != 0);
541
542 if (!flag)
543 return -ENODEV;
544
545 /*
546 * Add the event offset to the base address.
547 */
548 l_config = config & IMC_EVENT_OFFSET_MASK;
549 event->hw.event_base = (u64)pcni->vbase + l_config;
550 node_id = cpu_to_node(event->cpu);
551
552 /*
553 * Get the imc_pmu_ref struct for this node.
554 * Take the lock and then increment the count of nest pmu events inited.
555 */
556 ref = get_nest_pmu_ref(event->cpu);
557 if (!ref)
558 return -EINVAL;
559
560 spin_lock(&ref->lock);
561 if (ref->refc == 0) {
562 rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
563 get_hard_smp_processor_id(event->cpu));
564 if (rc) {
565 spin_unlock(&ref->lock);
566 pr_err("nest-imc: Unable to start the counters for node %d\n",
567 node_id);
568 return rc;
569 }
570 }
571 ++ref->refc;
572 spin_unlock(&ref->lock);
573
574 event->destroy = nest_imc_counters_release;
575 return 0;
576 }
577
578 /*
579 * core_imc_mem_init : Initializes memory for the current core.
580 *
581 * Uses alloc_pages_node() and uses the returned address as an argument to
582 * an opal call to configure the pdbar. The address sent as an argument is
583 * converted to physical address before the opal call is made. This is the
584 * base address at which the core imc counters are populated.
585 */
core_imc_mem_init(int cpu,int size)586 static int core_imc_mem_init(int cpu, int size)
587 {
588 int nid, rc = 0, core_id = (cpu / threads_per_core);
589 struct imc_mem_info *mem_info;
590 struct page *page;
591
592 /*
593 * alloc_pages_node() will allocate memory for core in the
594 * local node only.
595 */
596 nid = cpu_to_node(cpu);
597 mem_info = &core_imc_pmu->mem_info[core_id];
598 mem_info->id = core_id;
599
600 /* We need only vbase for core counters */
601 page = alloc_pages_node(nid,
602 GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
603 __GFP_NOWARN, get_order(size));
604 if (!page)
605 return -ENOMEM;
606 mem_info->vbase = page_address(page);
607
608 core_imc_refc[core_id].id = core_id;
609 spin_lock_init(&core_imc_refc[core_id].lock);
610
611 rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE,
612 __pa((void *)mem_info->vbase),
613 get_hard_smp_processor_id(cpu));
614 if (rc) {
615 free_pages((u64)mem_info->vbase, get_order(size));
616 mem_info->vbase = NULL;
617 }
618
619 return rc;
620 }
621
is_core_imc_mem_inited(int cpu)622 static bool is_core_imc_mem_inited(int cpu)
623 {
624 struct imc_mem_info *mem_info;
625 int core_id = (cpu / threads_per_core);
626
627 mem_info = &core_imc_pmu->mem_info[core_id];
628 if (!mem_info->vbase)
629 return false;
630
631 return true;
632 }
633
ppc_core_imc_cpu_online(unsigned int cpu)634 static int ppc_core_imc_cpu_online(unsigned int cpu)
635 {
636 const struct cpumask *l_cpumask;
637 static struct cpumask tmp_mask;
638 int ret = 0;
639
640 /* Get the cpumask for this core */
641 l_cpumask = cpu_sibling_mask(cpu);
642
643 /* If a cpu for this core is already set, then, don't do anything */
644 if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask))
645 return 0;
646
647 if (!is_core_imc_mem_inited(cpu)) {
648 ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size);
649 if (ret) {
650 pr_info("core_imc memory allocation for cpu %d failed\n", cpu);
651 return ret;
652 }
653 }
654
655 /* set the cpu in the mask */
656 cpumask_set_cpu(cpu, &core_imc_cpumask);
657 return 0;
658 }
659
ppc_core_imc_cpu_offline(unsigned int cpu)660 static int ppc_core_imc_cpu_offline(unsigned int cpu)
661 {
662 unsigned int core_id;
663 int ncpu;
664 struct imc_pmu_ref *ref;
665
666 /*
667 * clear this cpu out of the mask, if not present in the mask,
668 * don't bother doing anything.
669 */
670 if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask))
671 return 0;
672
673 /*
674 * Check whether core_imc is registered. We could end up here
675 * if the cpuhotplug callback registration fails. i.e, callback
676 * invokes the offline path for all sucessfully registered cpus.
677 * At this stage, core_imc pmu will not be registered and we
678 * should return here.
679 *
680 * We return with a zero since this is not an offline failure.
681 * And cpuhp_setup_state() returns the actual failure reason
682 * to the caller, which inturn will call the cleanup routine.
683 */
684 if (!core_imc_pmu->pmu.event_init)
685 return 0;
686
687 /* Find any online cpu in that core except the current "cpu" */
688 ncpu = cpumask_last(cpu_sibling_mask(cpu));
689
690 if (unlikely(ncpu == cpu))
691 ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
692
693 if (ncpu >= 0 && ncpu < nr_cpu_ids) {
694 cpumask_set_cpu(ncpu, &core_imc_cpumask);
695 perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
696 } else {
697 /*
698 * If this is the last cpu in this core then skip taking reference
699 * count lock for this core and directly zero "refc" for this core.
700 */
701 opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
702 get_hard_smp_processor_id(cpu));
703 core_id = cpu / threads_per_core;
704 ref = &core_imc_refc[core_id];
705 if (!ref)
706 return -EINVAL;
707
708 ref->refc = 0;
709 /*
710 * Reduce the global reference count, if this is the
711 * last cpu in this core and core-imc event running
712 * in this cpu.
713 */
714 spin_lock(&imc_global_refc.lock);
715 if (imc_global_refc.id == IMC_DOMAIN_CORE)
716 imc_global_refc.refc--;
717
718 spin_unlock(&imc_global_refc.lock);
719 }
720 return 0;
721 }
722
core_imc_pmu_cpumask_init(void)723 static int core_imc_pmu_cpumask_init(void)
724 {
725 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE,
726 "perf/powerpc/imc_core:online",
727 ppc_core_imc_cpu_online,
728 ppc_core_imc_cpu_offline);
729 }
730
reset_global_refc(struct perf_event * event)731 static void reset_global_refc(struct perf_event *event)
732 {
733 spin_lock(&imc_global_refc.lock);
734 imc_global_refc.refc--;
735
736 /*
737 * If no other thread is running any
738 * event for this domain(thread/core/trace),
739 * set the global id to zero.
740 */
741 if (imc_global_refc.refc <= 0) {
742 imc_global_refc.refc = 0;
743 imc_global_refc.id = 0;
744 }
745 spin_unlock(&imc_global_refc.lock);
746 }
747
core_imc_counters_release(struct perf_event * event)748 static void core_imc_counters_release(struct perf_event *event)
749 {
750 int rc, core_id;
751 struct imc_pmu_ref *ref;
752
753 if (event->cpu < 0)
754 return;
755 /*
756 * See if we need to disable the IMC PMU.
757 * If no events are currently in use, then we have to take a
758 * lock to ensure that we don't race with another task doing
759 * enable or disable the core counters.
760 */
761 core_id = event->cpu / threads_per_core;
762
763 /* Take the lock and decrement the refernce count for this core */
764 ref = &core_imc_refc[core_id];
765 if (!ref)
766 return;
767
768 spin_lock(&ref->lock);
769 if (ref->refc == 0) {
770 /*
771 * The scenario where this is true is, when perf session is
772 * started, followed by offlining of all cpus in a given core.
773 *
774 * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
775 * function set the ref->count to zero, if the cpu which is
776 * about to offline is the last cpu in a given core and make
777 * an OPAL call to disable the engine in that core.
778 *
779 */
780 spin_unlock(&ref->lock);
781 return;
782 }
783 ref->refc--;
784 if (ref->refc == 0) {
785 rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
786 get_hard_smp_processor_id(event->cpu));
787 if (rc) {
788 spin_unlock(&ref->lock);
789 pr_err("IMC: Unable to stop the counters for core %d\n", core_id);
790 return;
791 }
792 } else if (ref->refc < 0) {
793 WARN(1, "core-imc: Invalid event reference count\n");
794 ref->refc = 0;
795 }
796 spin_unlock(&ref->lock);
797
798 reset_global_refc(event);
799 }
800
core_imc_event_init(struct perf_event * event)801 static int core_imc_event_init(struct perf_event *event)
802 {
803 int core_id, rc;
804 u64 config = event->attr.config;
805 struct imc_mem_info *pcmi;
806 struct imc_pmu *pmu;
807 struct imc_pmu_ref *ref;
808
809 if (event->attr.type != event->pmu->type)
810 return -ENOENT;
811
812 /* Sampling not supported */
813 if (event->hw.sample_period)
814 return -EINVAL;
815
816 if (event->cpu < 0)
817 return -EINVAL;
818
819 event->hw.idx = -1;
820 pmu = imc_event_to_pmu(event);
821
822 /* Sanity check for config (event offset) */
823 if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
824 return -EINVAL;
825
826 if (!is_core_imc_mem_inited(event->cpu))
827 return -ENODEV;
828
829 core_id = event->cpu / threads_per_core;
830 pcmi = &core_imc_pmu->mem_info[core_id];
831 if ((!pcmi->vbase))
832 return -ENODEV;
833
834 ref = &core_imc_refc[core_id];
835 if (!ref)
836 return -EINVAL;
837
838 /*
839 * Core pmu units are enabled only when it is used.
840 * See if this is triggered for the first time.
841 * If yes, take the lock and enable the core counters.
842 * If not, just increment the count in core_imc_refc struct.
843 */
844 spin_lock(&ref->lock);
845 if (ref->refc == 0) {
846 rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
847 get_hard_smp_processor_id(event->cpu));
848 if (rc) {
849 spin_unlock(&ref->lock);
850 pr_err("core-imc: Unable to start the counters for core %d\n",
851 core_id);
852 return rc;
853 }
854 }
855 ++ref->refc;
856 spin_unlock(&ref->lock);
857
858 /*
859 * Since the system can run either in accumulation or trace-mode
860 * of IMC at a time, core-imc events are allowed only if no other
861 * trace/thread imc events are enabled/monitored.
862 *
863 * Take the global lock, and check the refc.id
864 * to know whether any other trace/thread imc
865 * events are running.
866 */
867 spin_lock(&imc_global_refc.lock);
868 if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_CORE) {
869 /*
870 * No other trace/thread imc events are running in
871 * the system, so set the refc.id to core-imc.
872 */
873 imc_global_refc.id = IMC_DOMAIN_CORE;
874 imc_global_refc.refc++;
875 } else {
876 spin_unlock(&imc_global_refc.lock);
877 return -EBUSY;
878 }
879 spin_unlock(&imc_global_refc.lock);
880
881 event->hw.event_base = (u64)pcmi->vbase + (config & IMC_EVENT_OFFSET_MASK);
882 event->destroy = core_imc_counters_release;
883 return 0;
884 }
885
886 /*
887 * Allocates a page of memory for each of the online cpus, and load
888 * LDBAR with 0.
889 * The physical base address of the page allocated for a cpu will be
890 * written to the LDBAR for that cpu, when the thread-imc event
891 * is added.
892 *
893 * LDBAR Register Layout:
894 *
895 * 0 4 8 12 16 20 24 28
896 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
897 * | | [ ] [ Counter Address [8:50]
898 * | * Mode |
899 * | * PB Scope
900 * * Enable/Disable
901 *
902 * 32 36 40 44 48 52 56 60
903 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
904 * Counter Address [8:50] ]
905 *
906 */
thread_imc_mem_alloc(int cpu_id,int size)907 static int thread_imc_mem_alloc(int cpu_id, int size)
908 {
909 u64 *local_mem = per_cpu(thread_imc_mem, cpu_id);
910 int nid = cpu_to_node(cpu_id);
911
912 if (!local_mem) {
913 struct page *page;
914 /*
915 * This case could happen only once at start, since we dont
916 * free the memory in cpu offline path.
917 */
918 page = alloc_pages_node(nid,
919 GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
920 __GFP_NOWARN, get_order(size));
921 if (!page)
922 return -ENOMEM;
923 local_mem = page_address(page);
924
925 per_cpu(thread_imc_mem, cpu_id) = local_mem;
926 }
927
928 mtspr(SPRN_LDBAR, 0);
929 return 0;
930 }
931
ppc_thread_imc_cpu_online(unsigned int cpu)932 static int ppc_thread_imc_cpu_online(unsigned int cpu)
933 {
934 return thread_imc_mem_alloc(cpu, thread_imc_mem_size);
935 }
936
ppc_thread_imc_cpu_offline(unsigned int cpu)937 static int ppc_thread_imc_cpu_offline(unsigned int cpu)
938 {
939 /*
940 * Set the bit 0 of LDBAR to zero.
941 *
942 * If bit 0 of LDBAR is unset, it will stop posting
943 * the counter data to memory.
944 * For thread-imc, bit 0 of LDBAR will be set to 1 in the
945 * event_add function. So reset this bit here, to stop the updates
946 * to memory in the cpu_offline path.
947 */
948 mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
949
950 /* Reduce the refc if thread-imc event running on this cpu */
951 spin_lock(&imc_global_refc.lock);
952 if (imc_global_refc.id == IMC_DOMAIN_THREAD)
953 imc_global_refc.refc--;
954 spin_unlock(&imc_global_refc.lock);
955
956 return 0;
957 }
958
thread_imc_cpu_init(void)959 static int thread_imc_cpu_init(void)
960 {
961 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE,
962 "perf/powerpc/imc_thread:online",
963 ppc_thread_imc_cpu_online,
964 ppc_thread_imc_cpu_offline);
965 }
966
thread_imc_event_init(struct perf_event * event)967 static int thread_imc_event_init(struct perf_event *event)
968 {
969 u32 config = event->attr.config;
970 struct task_struct *target;
971 struct imc_pmu *pmu;
972
973 if (event->attr.type != event->pmu->type)
974 return -ENOENT;
975
976 if (!perfmon_capable())
977 return -EACCES;
978
979 /* Sampling not supported */
980 if (event->hw.sample_period)
981 return -EINVAL;
982
983 event->hw.idx = -1;
984 pmu = imc_event_to_pmu(event);
985
986 /* Sanity check for config offset */
987 if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
988 return -EINVAL;
989
990 target = event->hw.target;
991 if (!target)
992 return -EINVAL;
993
994 spin_lock(&imc_global_refc.lock);
995 /*
996 * Check if any other trace/core imc events are running in the
997 * system, if not set the global id to thread-imc.
998 */
999 if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_THREAD) {
1000 imc_global_refc.id = IMC_DOMAIN_THREAD;
1001 imc_global_refc.refc++;
1002 } else {
1003 spin_unlock(&imc_global_refc.lock);
1004 return -EBUSY;
1005 }
1006 spin_unlock(&imc_global_refc.lock);
1007
1008 event->pmu->task_ctx_nr = perf_sw_context;
1009 event->destroy = reset_global_refc;
1010 return 0;
1011 }
1012
is_thread_imc_pmu(struct perf_event * event)1013 static bool is_thread_imc_pmu(struct perf_event *event)
1014 {
1015 if (!strncmp(event->pmu->name, "thread_imc", strlen("thread_imc")))
1016 return true;
1017
1018 return false;
1019 }
1020
get_event_base_addr(struct perf_event * event)1021 static u64 * get_event_base_addr(struct perf_event *event)
1022 {
1023 u64 addr;
1024
1025 if (is_thread_imc_pmu(event)) {
1026 addr = (u64)per_cpu(thread_imc_mem, smp_processor_id());
1027 return (u64 *)(addr + (event->attr.config & IMC_EVENT_OFFSET_MASK));
1028 }
1029
1030 return (u64 *)event->hw.event_base;
1031 }
1032
thread_imc_pmu_start_txn(struct pmu * pmu,unsigned int txn_flags)1033 static void thread_imc_pmu_start_txn(struct pmu *pmu,
1034 unsigned int txn_flags)
1035 {
1036 if (txn_flags & ~PERF_PMU_TXN_ADD)
1037 return;
1038 perf_pmu_disable(pmu);
1039 }
1040
thread_imc_pmu_cancel_txn(struct pmu * pmu)1041 static void thread_imc_pmu_cancel_txn(struct pmu *pmu)
1042 {
1043 perf_pmu_enable(pmu);
1044 }
1045
thread_imc_pmu_commit_txn(struct pmu * pmu)1046 static int thread_imc_pmu_commit_txn(struct pmu *pmu)
1047 {
1048 perf_pmu_enable(pmu);
1049 return 0;
1050 }
1051
imc_read_counter(struct perf_event * event)1052 static u64 imc_read_counter(struct perf_event *event)
1053 {
1054 u64 *addr, data;
1055
1056 /*
1057 * In-Memory Collection (IMC) counters are free flowing counters.
1058 * So we take a snapshot of the counter value on enable and save it
1059 * to calculate the delta at later stage to present the event counter
1060 * value.
1061 */
1062 addr = get_event_base_addr(event);
1063 data = be64_to_cpu(READ_ONCE(*addr));
1064 local64_set(&event->hw.prev_count, data);
1065
1066 return data;
1067 }
1068
imc_event_update(struct perf_event * event)1069 static void imc_event_update(struct perf_event *event)
1070 {
1071 u64 counter_prev, counter_new, final_count;
1072
1073 counter_prev = local64_read(&event->hw.prev_count);
1074 counter_new = imc_read_counter(event);
1075 final_count = counter_new - counter_prev;
1076
1077 /* Update the delta to the event count */
1078 local64_add(final_count, &event->count);
1079 }
1080
imc_event_start(struct perf_event * event,int flags)1081 static void imc_event_start(struct perf_event *event, int flags)
1082 {
1083 /*
1084 * In Memory Counters are free flowing counters. HW or the microcode
1085 * keeps adding to the counter offset in memory. To get event
1086 * counter value, we snapshot the value here and we calculate
1087 * delta at later point.
1088 */
1089 imc_read_counter(event);
1090 }
1091
imc_event_stop(struct perf_event * event,int flags)1092 static void imc_event_stop(struct perf_event *event, int flags)
1093 {
1094 /*
1095 * Take a snapshot and calculate the delta and update
1096 * the event counter values.
1097 */
1098 imc_event_update(event);
1099 }
1100
imc_event_add(struct perf_event * event,int flags)1101 static int imc_event_add(struct perf_event *event, int flags)
1102 {
1103 if (flags & PERF_EF_START)
1104 imc_event_start(event, flags);
1105
1106 return 0;
1107 }
1108
thread_imc_event_add(struct perf_event * event,int flags)1109 static int thread_imc_event_add(struct perf_event *event, int flags)
1110 {
1111 int core_id;
1112 struct imc_pmu_ref *ref;
1113 u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, smp_processor_id());
1114
1115 if (flags & PERF_EF_START)
1116 imc_event_start(event, flags);
1117
1118 if (!is_core_imc_mem_inited(smp_processor_id()))
1119 return -EINVAL;
1120
1121 core_id = smp_processor_id() / threads_per_core;
1122 ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | THREAD_IMC_ENABLE;
1123 mtspr(SPRN_LDBAR, ldbar_value);
1124
1125 /*
1126 * imc pmus are enabled only when it is used.
1127 * See if this is triggered for the first time.
1128 * If yes, take the lock and enable the counters.
1129 * If not, just increment the count in ref count struct.
1130 */
1131 ref = &core_imc_refc[core_id];
1132 if (!ref)
1133 return -EINVAL;
1134
1135 spin_lock(&ref->lock);
1136 if (ref->refc == 0) {
1137 if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
1138 get_hard_smp_processor_id(smp_processor_id()))) {
1139 spin_unlock(&ref->lock);
1140 pr_err("thread-imc: Unable to start the counter\
1141 for core %d\n", core_id);
1142 return -EINVAL;
1143 }
1144 }
1145 ++ref->refc;
1146 spin_unlock(&ref->lock);
1147 return 0;
1148 }
1149
thread_imc_event_del(struct perf_event * event,int flags)1150 static void thread_imc_event_del(struct perf_event *event, int flags)
1151 {
1152
1153 int core_id;
1154 struct imc_pmu_ref *ref;
1155
1156 core_id = smp_processor_id() / threads_per_core;
1157 ref = &core_imc_refc[core_id];
1158 if (!ref) {
1159 pr_debug("imc: Failed to get event reference count\n");
1160 return;
1161 }
1162
1163 spin_lock(&ref->lock);
1164 ref->refc--;
1165 if (ref->refc == 0) {
1166 if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
1167 get_hard_smp_processor_id(smp_processor_id()))) {
1168 spin_unlock(&ref->lock);
1169 pr_err("thread-imc: Unable to stop the counters\
1170 for core %d\n", core_id);
1171 return;
1172 }
1173 } else if (ref->refc < 0) {
1174 ref->refc = 0;
1175 }
1176 spin_unlock(&ref->lock);
1177
1178 /* Set bit 0 of LDBAR to zero, to stop posting updates to memory */
1179 mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
1180
1181 /*
1182 * Take a snapshot and calculate the delta and update
1183 * the event counter values.
1184 */
1185 imc_event_update(event);
1186 }
1187
1188 /*
1189 * Allocate a page of memory for each cpu, and load LDBAR with 0.
1190 */
trace_imc_mem_alloc(int cpu_id,int size)1191 static int trace_imc_mem_alloc(int cpu_id, int size)
1192 {
1193 u64 *local_mem = per_cpu(trace_imc_mem, cpu_id);
1194 int phys_id = cpu_to_node(cpu_id), rc = 0;
1195 int core_id = (cpu_id / threads_per_core);
1196
1197 if (!local_mem) {
1198 struct page *page;
1199
1200 page = alloc_pages_node(phys_id,
1201 GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
1202 __GFP_NOWARN, get_order(size));
1203 if (!page)
1204 return -ENOMEM;
1205 local_mem = page_address(page);
1206 per_cpu(trace_imc_mem, cpu_id) = local_mem;
1207
1208 /* Initialise the counters for trace mode */
1209 rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_TRACE, __pa((void *)local_mem),
1210 get_hard_smp_processor_id(cpu_id));
1211 if (rc) {
1212 pr_info("IMC:opal init failed for trace imc\n");
1213 return rc;
1214 }
1215 }
1216
1217 trace_imc_refc[core_id].id = core_id;
1218 spin_lock_init(&trace_imc_refc[core_id].lock);
1219
1220 mtspr(SPRN_LDBAR, 0);
1221 return 0;
1222 }
1223
ppc_trace_imc_cpu_online(unsigned int cpu)1224 static int ppc_trace_imc_cpu_online(unsigned int cpu)
1225 {
1226 return trace_imc_mem_alloc(cpu, trace_imc_mem_size);
1227 }
1228
ppc_trace_imc_cpu_offline(unsigned int cpu)1229 static int ppc_trace_imc_cpu_offline(unsigned int cpu)
1230 {
1231 /*
1232 * No need to set bit 0 of LDBAR to zero, as
1233 * it is set to zero for imc trace-mode
1234 *
1235 * Reduce the refc if any trace-imc event running
1236 * on this cpu.
1237 */
1238 spin_lock(&imc_global_refc.lock);
1239 if (imc_global_refc.id == IMC_DOMAIN_TRACE)
1240 imc_global_refc.refc--;
1241 spin_unlock(&imc_global_refc.lock);
1242
1243 return 0;
1244 }
1245
trace_imc_cpu_init(void)1246 static int trace_imc_cpu_init(void)
1247 {
1248 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE,
1249 "perf/powerpc/imc_trace:online",
1250 ppc_trace_imc_cpu_online,
1251 ppc_trace_imc_cpu_offline);
1252 }
1253
get_trace_imc_event_base_addr(void)1254 static u64 get_trace_imc_event_base_addr(void)
1255 {
1256 return (u64)per_cpu(trace_imc_mem, smp_processor_id());
1257 }
1258
1259 /*
1260 * Function to parse trace-imc data obtained
1261 * and to prepare the perf sample.
1262 */
trace_imc_prepare_sample(struct trace_imc_data * mem,struct perf_sample_data * data,u64 * prev_tb,struct perf_event_header * header,struct perf_event * event)1263 static int trace_imc_prepare_sample(struct trace_imc_data *mem,
1264 struct perf_sample_data *data,
1265 u64 *prev_tb,
1266 struct perf_event_header *header,
1267 struct perf_event *event)
1268 {
1269 /* Sanity checks for a valid record */
1270 if (be64_to_cpu(READ_ONCE(mem->tb1)) > *prev_tb)
1271 *prev_tb = be64_to_cpu(READ_ONCE(mem->tb1));
1272 else
1273 return -EINVAL;
1274
1275 if ((be64_to_cpu(READ_ONCE(mem->tb1)) & IMC_TRACE_RECORD_TB1_MASK) !=
1276 be64_to_cpu(READ_ONCE(mem->tb2)))
1277 return -EINVAL;
1278
1279 /* Prepare perf sample */
1280 data->ip = be64_to_cpu(READ_ONCE(mem->ip));
1281 data->period = event->hw.last_period;
1282
1283 header->type = PERF_RECORD_SAMPLE;
1284 header->size = sizeof(*header) + event->header_size;
1285 header->misc = 0;
1286
1287 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
1288 switch (IMC_TRACE_RECORD_VAL_HVPR(be64_to_cpu(READ_ONCE(mem->val)))) {
1289 case 0:/* when MSR HV and PR not set in the trace-record */
1290 header->misc |= PERF_RECORD_MISC_GUEST_KERNEL;
1291 break;
1292 case 1: /* MSR HV is 0 and PR is 1 */
1293 header->misc |= PERF_RECORD_MISC_GUEST_USER;
1294 break;
1295 case 2: /* MSR HV is 1 and PR is 0 */
1296 header->misc |= PERF_RECORD_MISC_KERNEL;
1297 break;
1298 case 3: /* MSR HV is 1 and PR is 1 */
1299 header->misc |= PERF_RECORD_MISC_USER;
1300 break;
1301 default:
1302 pr_info("IMC: Unable to set the flag based on MSR bits\n");
1303 break;
1304 }
1305 } else {
1306 if (is_kernel_addr(data->ip))
1307 header->misc |= PERF_RECORD_MISC_KERNEL;
1308 else
1309 header->misc |= PERF_RECORD_MISC_USER;
1310 }
1311 perf_event_header__init_id(header, data, event);
1312
1313 return 0;
1314 }
1315
dump_trace_imc_data(struct perf_event * event)1316 static void dump_trace_imc_data(struct perf_event *event)
1317 {
1318 struct trace_imc_data *mem;
1319 int i, ret;
1320 u64 prev_tb = 0;
1321
1322 mem = (struct trace_imc_data *)get_trace_imc_event_base_addr();
1323 for (i = 0; i < (trace_imc_mem_size / sizeof(struct trace_imc_data));
1324 i++, mem++) {
1325 struct perf_sample_data data;
1326 struct perf_event_header header;
1327
1328 ret = trace_imc_prepare_sample(mem, &data, &prev_tb, &header, event);
1329 if (ret) /* Exit, if not a valid record */
1330 break;
1331 else {
1332 /* If this is a valid record, create the sample */
1333 struct perf_output_handle handle;
1334
1335 if (perf_output_begin(&handle, &data, event, header.size))
1336 return;
1337
1338 perf_output_sample(&handle, &header, &data, event);
1339 perf_output_end(&handle);
1340 }
1341 }
1342 }
1343
trace_imc_event_add(struct perf_event * event,int flags)1344 static int trace_imc_event_add(struct perf_event *event, int flags)
1345 {
1346 int core_id = smp_processor_id() / threads_per_core;
1347 struct imc_pmu_ref *ref = NULL;
1348 u64 local_mem, ldbar_value;
1349
1350 /* Set trace-imc bit in ldbar and load ldbar with per-thread memory address */
1351 local_mem = get_trace_imc_event_base_addr();
1352 ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | TRACE_IMC_ENABLE;
1353
1354 /* trace-imc reference count */
1355 if (trace_imc_refc)
1356 ref = &trace_imc_refc[core_id];
1357 if (!ref) {
1358 pr_debug("imc: Failed to get the event reference count\n");
1359 return -EINVAL;
1360 }
1361
1362 mtspr(SPRN_LDBAR, ldbar_value);
1363 spin_lock(&ref->lock);
1364 if (ref->refc == 0) {
1365 if (opal_imc_counters_start(OPAL_IMC_COUNTERS_TRACE,
1366 get_hard_smp_processor_id(smp_processor_id()))) {
1367 spin_unlock(&ref->lock);
1368 pr_err("trace-imc: Unable to start the counters for core %d\n", core_id);
1369 return -EINVAL;
1370 }
1371 }
1372 ++ref->refc;
1373 spin_unlock(&ref->lock);
1374 return 0;
1375 }
1376
trace_imc_event_read(struct perf_event * event)1377 static void trace_imc_event_read(struct perf_event *event)
1378 {
1379 return;
1380 }
1381
trace_imc_event_stop(struct perf_event * event,int flags)1382 static void trace_imc_event_stop(struct perf_event *event, int flags)
1383 {
1384 u64 local_mem = get_trace_imc_event_base_addr();
1385 dump_trace_imc_data(event);
1386 memset((void *)local_mem, 0, sizeof(u64));
1387 }
1388
trace_imc_event_start(struct perf_event * event,int flags)1389 static void trace_imc_event_start(struct perf_event *event, int flags)
1390 {
1391 return;
1392 }
1393
trace_imc_event_del(struct perf_event * event,int flags)1394 static void trace_imc_event_del(struct perf_event *event, int flags)
1395 {
1396 int core_id = smp_processor_id() / threads_per_core;
1397 struct imc_pmu_ref *ref = NULL;
1398
1399 if (trace_imc_refc)
1400 ref = &trace_imc_refc[core_id];
1401 if (!ref) {
1402 pr_debug("imc: Failed to get event reference count\n");
1403 return;
1404 }
1405
1406 spin_lock(&ref->lock);
1407 ref->refc--;
1408 if (ref->refc == 0) {
1409 if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_TRACE,
1410 get_hard_smp_processor_id(smp_processor_id()))) {
1411 spin_unlock(&ref->lock);
1412 pr_err("trace-imc: Unable to stop the counters for core %d\n", core_id);
1413 return;
1414 }
1415 } else if (ref->refc < 0) {
1416 ref->refc = 0;
1417 }
1418 spin_unlock(&ref->lock);
1419
1420 trace_imc_event_stop(event, flags);
1421 }
1422
trace_imc_event_init(struct perf_event * event)1423 static int trace_imc_event_init(struct perf_event *event)
1424 {
1425 if (event->attr.type != event->pmu->type)
1426 return -ENOENT;
1427
1428 if (!perfmon_capable())
1429 return -EACCES;
1430
1431 /* Return if this is a couting event */
1432 if (event->attr.sample_period == 0)
1433 return -ENOENT;
1434
1435 /*
1436 * Take the global lock, and make sure
1437 * no other thread is running any core/thread imc
1438 * events
1439 */
1440 spin_lock(&imc_global_refc.lock);
1441 if (imc_global_refc.id == 0 || imc_global_refc.id == IMC_DOMAIN_TRACE) {
1442 /*
1443 * No core/thread imc events are running in the
1444 * system, so set the refc.id to trace-imc.
1445 */
1446 imc_global_refc.id = IMC_DOMAIN_TRACE;
1447 imc_global_refc.refc++;
1448 } else {
1449 spin_unlock(&imc_global_refc.lock);
1450 return -EBUSY;
1451 }
1452 spin_unlock(&imc_global_refc.lock);
1453
1454 event->hw.idx = -1;
1455
1456 /*
1457 * There can only be a single PMU for perf_hw_context events which is assigned to
1458 * core PMU. Hence use "perf_sw_context" for trace_imc.
1459 */
1460 event->pmu->task_ctx_nr = perf_sw_context;
1461 event->destroy = reset_global_refc;
1462 return 0;
1463 }
1464
1465 /* update_pmu_ops : Populate the appropriate operations for "pmu" */
update_pmu_ops(struct imc_pmu * pmu)1466 static int update_pmu_ops(struct imc_pmu *pmu)
1467 {
1468 pmu->pmu.task_ctx_nr = perf_invalid_context;
1469 pmu->pmu.add = imc_event_add;
1470 pmu->pmu.del = imc_event_stop;
1471 pmu->pmu.start = imc_event_start;
1472 pmu->pmu.stop = imc_event_stop;
1473 pmu->pmu.read = imc_event_update;
1474 pmu->pmu.attr_groups = pmu->attr_groups;
1475 pmu->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
1476 pmu->attr_groups[IMC_FORMAT_ATTR] = &imc_format_group;
1477
1478 switch (pmu->domain) {
1479 case IMC_DOMAIN_NEST:
1480 pmu->pmu.event_init = nest_imc_event_init;
1481 pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
1482 break;
1483 case IMC_DOMAIN_CORE:
1484 pmu->pmu.event_init = core_imc_event_init;
1485 pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
1486 break;
1487 case IMC_DOMAIN_THREAD:
1488 pmu->pmu.event_init = thread_imc_event_init;
1489 pmu->pmu.add = thread_imc_event_add;
1490 pmu->pmu.del = thread_imc_event_del;
1491 pmu->pmu.start_txn = thread_imc_pmu_start_txn;
1492 pmu->pmu.cancel_txn = thread_imc_pmu_cancel_txn;
1493 pmu->pmu.commit_txn = thread_imc_pmu_commit_txn;
1494 break;
1495 case IMC_DOMAIN_TRACE:
1496 pmu->pmu.event_init = trace_imc_event_init;
1497 pmu->pmu.add = trace_imc_event_add;
1498 pmu->pmu.del = trace_imc_event_del;
1499 pmu->pmu.start = trace_imc_event_start;
1500 pmu->pmu.stop = trace_imc_event_stop;
1501 pmu->pmu.read = trace_imc_event_read;
1502 pmu->attr_groups[IMC_FORMAT_ATTR] = &trace_imc_format_group;
1503 default:
1504 break;
1505 }
1506
1507 return 0;
1508 }
1509
1510 /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
init_nest_pmu_ref(void)1511 static int init_nest_pmu_ref(void)
1512 {
1513 int nid, i, cpu;
1514
1515 nest_imc_refc = kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc),
1516 GFP_KERNEL);
1517
1518 if (!nest_imc_refc)
1519 return -ENOMEM;
1520
1521 i = 0;
1522 for_each_node(nid) {
1523 /*
1524 * Take the lock to avoid races while tracking the number of
1525 * sessions using the chip's nest pmu units.
1526 */
1527 spin_lock_init(&nest_imc_refc[i].lock);
1528
1529 /*
1530 * Loop to init the "id" with the node_id. Variable "i" initialized to
1531 * 0 and will be used as index to the array. "i" will not go off the
1532 * end of the array since the "for_each_node" loops for "N_POSSIBLE"
1533 * nodes only.
1534 */
1535 nest_imc_refc[i++].id = nid;
1536 }
1537
1538 /*
1539 * Loop to init the per_cpu "local_nest_imc_refc" with the proper
1540 * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
1541 */
1542 for_each_possible_cpu(cpu) {
1543 nid = cpu_to_node(cpu);
1544 for (i = 0; i < num_possible_nodes(); i++) {
1545 if (nest_imc_refc[i].id == nid) {
1546 per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i];
1547 break;
1548 }
1549 }
1550 }
1551 return 0;
1552 }
1553
cleanup_all_core_imc_memory(void)1554 static void cleanup_all_core_imc_memory(void)
1555 {
1556 int i, nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
1557 struct imc_mem_info *ptr = core_imc_pmu->mem_info;
1558 int size = core_imc_pmu->counter_mem_size;
1559
1560 /* mem_info will never be NULL */
1561 for (i = 0; i < nr_cores; i++) {
1562 if (ptr[i].vbase)
1563 free_pages((u64)ptr[i].vbase, get_order(size));
1564 }
1565
1566 kfree(ptr);
1567 kfree(core_imc_refc);
1568 }
1569
thread_imc_ldbar_disable(void * dummy)1570 static void thread_imc_ldbar_disable(void *dummy)
1571 {
1572 /*
1573 * By setting 0th bit of LDBAR to zero, we disable thread-imc
1574 * updates to memory.
1575 */
1576 mtspr(SPRN_LDBAR, (mfspr(SPRN_LDBAR) & (~(1UL << 63))));
1577 }
1578
thread_imc_disable(void)1579 void thread_imc_disable(void)
1580 {
1581 on_each_cpu(thread_imc_ldbar_disable, NULL, 1);
1582 }
1583
cleanup_all_thread_imc_memory(void)1584 static void cleanup_all_thread_imc_memory(void)
1585 {
1586 int i, order = get_order(thread_imc_mem_size);
1587
1588 for_each_online_cpu(i) {
1589 if (per_cpu(thread_imc_mem, i))
1590 free_pages((u64)per_cpu(thread_imc_mem, i), order);
1591
1592 }
1593 }
1594
cleanup_all_trace_imc_memory(void)1595 static void cleanup_all_trace_imc_memory(void)
1596 {
1597 int i, order = get_order(trace_imc_mem_size);
1598
1599 for_each_online_cpu(i) {
1600 if (per_cpu(trace_imc_mem, i))
1601 free_pages((u64)per_cpu(trace_imc_mem, i), order);
1602
1603 }
1604 kfree(trace_imc_refc);
1605 }
1606
1607 /* Function to free the attr_groups which are dynamically allocated */
imc_common_mem_free(struct imc_pmu * pmu_ptr)1608 static void imc_common_mem_free(struct imc_pmu *pmu_ptr)
1609 {
1610 if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
1611 kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
1612 kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
1613 }
1614
1615 /*
1616 * Common function to unregister cpu hotplug callback and
1617 * free the memory.
1618 * TODO: Need to handle pmu unregistering, which will be
1619 * done in followup series.
1620 */
imc_common_cpuhp_mem_free(struct imc_pmu * pmu_ptr)1621 static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
1622 {
1623 if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
1624 spin_lock(&nest_init_lock);
1625 if (nest_pmus == 1) {
1626 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
1627 kfree(nest_imc_refc);
1628 kfree(per_nest_pmu_arr);
1629 per_nest_pmu_arr = NULL;
1630 }
1631
1632 if (nest_pmus > 0)
1633 nest_pmus--;
1634 spin_unlock(&nest_init_lock);
1635 }
1636
1637 /* Free core_imc memory */
1638 if (pmu_ptr->domain == IMC_DOMAIN_CORE) {
1639 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE);
1640 cleanup_all_core_imc_memory();
1641 }
1642
1643 /* Free thread_imc memory */
1644 if (pmu_ptr->domain == IMC_DOMAIN_THREAD) {
1645 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE);
1646 cleanup_all_thread_imc_memory();
1647 }
1648
1649 if (pmu_ptr->domain == IMC_DOMAIN_TRACE) {
1650 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE);
1651 cleanup_all_trace_imc_memory();
1652 }
1653 }
1654
1655 /*
1656 * Function to unregister thread-imc if core-imc
1657 * is not registered.
1658 */
unregister_thread_imc(void)1659 void unregister_thread_imc(void)
1660 {
1661 imc_common_cpuhp_mem_free(thread_imc_pmu);
1662 imc_common_mem_free(thread_imc_pmu);
1663 perf_pmu_unregister(&thread_imc_pmu->pmu);
1664 }
1665
1666 /*
1667 * imc_mem_init : Function to support memory allocation for core imc.
1668 */
imc_mem_init(struct imc_pmu * pmu_ptr,struct device_node * parent,int pmu_index)1669 static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,
1670 int pmu_index)
1671 {
1672 const char *s;
1673 int nr_cores, cpu, res = -ENOMEM;
1674
1675 if (of_property_read_string(parent, "name", &s))
1676 return -ENODEV;
1677
1678 switch (pmu_ptr->domain) {
1679 case IMC_DOMAIN_NEST:
1680 /* Update the pmu name */
1681 pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s);
1682 if (!pmu_ptr->pmu.name)
1683 goto err;
1684
1685 /* Needed for hotplug/migration */
1686 if (!per_nest_pmu_arr) {
1687 per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1,
1688 sizeof(struct imc_pmu *),
1689 GFP_KERNEL);
1690 if (!per_nest_pmu_arr)
1691 goto err;
1692 }
1693 per_nest_pmu_arr[pmu_index] = pmu_ptr;
1694 break;
1695 case IMC_DOMAIN_CORE:
1696 /* Update the pmu name */
1697 pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
1698 if (!pmu_ptr->pmu.name)
1699 goto err;
1700
1701 nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
1702 pmu_ptr->mem_info = kcalloc(nr_cores, sizeof(struct imc_mem_info),
1703 GFP_KERNEL);
1704
1705 if (!pmu_ptr->mem_info)
1706 goto err;
1707
1708 core_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
1709 GFP_KERNEL);
1710
1711 if (!core_imc_refc) {
1712 kfree(pmu_ptr->mem_info);
1713 goto err;
1714 }
1715
1716 core_imc_pmu = pmu_ptr;
1717 break;
1718 case IMC_DOMAIN_THREAD:
1719 /* Update the pmu name */
1720 pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
1721 if (!pmu_ptr->pmu.name)
1722 goto err;
1723
1724 thread_imc_mem_size = pmu_ptr->counter_mem_size;
1725 for_each_online_cpu(cpu) {
1726 res = thread_imc_mem_alloc(cpu, pmu_ptr->counter_mem_size);
1727 if (res) {
1728 cleanup_all_thread_imc_memory();
1729 goto err;
1730 }
1731 }
1732
1733 thread_imc_pmu = pmu_ptr;
1734 break;
1735 case IMC_DOMAIN_TRACE:
1736 /* Update the pmu name */
1737 pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
1738 if (!pmu_ptr->pmu.name)
1739 return -ENOMEM;
1740
1741 nr_cores = DIV_ROUND_UP(num_possible_cpus(), threads_per_core);
1742 trace_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
1743 GFP_KERNEL);
1744 if (!trace_imc_refc)
1745 return -ENOMEM;
1746
1747 trace_imc_mem_size = pmu_ptr->counter_mem_size;
1748 for_each_online_cpu(cpu) {
1749 res = trace_imc_mem_alloc(cpu, trace_imc_mem_size);
1750 if (res) {
1751 cleanup_all_trace_imc_memory();
1752 goto err;
1753 }
1754 }
1755 break;
1756 default:
1757 return -EINVAL;
1758 }
1759
1760 return 0;
1761 err:
1762 return res;
1763 }
1764
1765 /*
1766 * init_imc_pmu : Setup and register the IMC pmu device.
1767 *
1768 * @parent: Device tree unit node
1769 * @pmu_ptr: memory allocated for this pmu
1770 * @pmu_idx: Count of nest pmc registered
1771 *
1772 * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
1773 * Handles failure cases and accordingly frees memory.
1774 */
init_imc_pmu(struct device_node * parent,struct imc_pmu * pmu_ptr,int pmu_idx)1775 int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
1776 {
1777 int ret;
1778
1779 ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
1780 if (ret)
1781 goto err_free_mem;
1782
1783 switch (pmu_ptr->domain) {
1784 case IMC_DOMAIN_NEST:
1785 /*
1786 * Nest imc pmu need only one cpu per chip, we initialize the
1787 * cpumask for the first nest imc pmu and use the same for the
1788 * rest. To handle the cpuhotplug callback unregister, we track
1789 * the number of nest pmus in "nest_pmus".
1790 */
1791 spin_lock(&nest_init_lock);
1792 if (nest_pmus == 0) {
1793 ret = init_nest_pmu_ref();
1794 if (ret) {
1795 spin_unlock(&nest_init_lock);
1796 kfree(per_nest_pmu_arr);
1797 per_nest_pmu_arr = NULL;
1798 goto err_free_mem;
1799 }
1800 /* Register for cpu hotplug notification. */
1801 ret = nest_pmu_cpumask_init();
1802 if (ret) {
1803 spin_unlock(&nest_init_lock);
1804 kfree(nest_imc_refc);
1805 kfree(per_nest_pmu_arr);
1806 per_nest_pmu_arr = NULL;
1807 goto err_free_mem;
1808 }
1809 }
1810 nest_pmus++;
1811 spin_unlock(&nest_init_lock);
1812 break;
1813 case IMC_DOMAIN_CORE:
1814 ret = core_imc_pmu_cpumask_init();
1815 if (ret) {
1816 cleanup_all_core_imc_memory();
1817 goto err_free_mem;
1818 }
1819
1820 break;
1821 case IMC_DOMAIN_THREAD:
1822 ret = thread_imc_cpu_init();
1823 if (ret) {
1824 cleanup_all_thread_imc_memory();
1825 goto err_free_mem;
1826 }
1827
1828 break;
1829 case IMC_DOMAIN_TRACE:
1830 ret = trace_imc_cpu_init();
1831 if (ret) {
1832 cleanup_all_trace_imc_memory();
1833 goto err_free_mem;
1834 }
1835
1836 break;
1837 default:
1838 return -EINVAL; /* Unknown domain */
1839 }
1840
1841 ret = update_events_in_group(parent, pmu_ptr);
1842 if (ret)
1843 goto err_free_cpuhp_mem;
1844
1845 ret = update_pmu_ops(pmu_ptr);
1846 if (ret)
1847 goto err_free_cpuhp_mem;
1848
1849 ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
1850 if (ret)
1851 goto err_free_cpuhp_mem;
1852
1853 pr_debug("%s performance monitor hardware support registered\n",
1854 pmu_ptr->pmu.name);
1855
1856 return 0;
1857
1858 err_free_cpuhp_mem:
1859 imc_common_cpuhp_mem_free(pmu_ptr);
1860 err_free_mem:
1861 imc_common_mem_free(pmu_ptr);
1862 return ret;
1863 }
1864