1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * In-Memory Collection (IMC) Performance Monitor counter support.
5 * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
6 * (C) 2017 Anju T Sudhakar, IBM Corporation.
7 * (C) 2017 Hemant K Shaw, IBM Corporation.
9 #include <linux/perf_event.h>
10 #include <linux/slab.h>
12 #include <asm/imc-pmu.h>
13 #include <asm/cputhreads.h>
15 #include <linux/string.h>
17 /* Nest IMC data structures and variables */
20 * Used to avoid races in counting the nest-pmu units during hotplug
21 * register and unregister
23 static DEFINE_MUTEX(nest_init_lock
);
24 static DEFINE_PER_CPU(struct imc_pmu_ref
*, local_nest_imc_refc
);
25 static struct imc_pmu
**per_nest_pmu_arr
;
26 static cpumask_t nest_imc_cpumask
;
27 static struct imc_pmu_ref
*nest_imc_refc
;
30 /* Core IMC data structures and variables */
32 static cpumask_t core_imc_cpumask
;
33 static struct imc_pmu_ref
*core_imc_refc
;
34 static struct imc_pmu
*core_imc_pmu
;
36 /* Thread IMC data structures and variables */
38 static DEFINE_PER_CPU(u64
*, thread_imc_mem
);
39 static struct imc_pmu
*thread_imc_pmu
;
40 static int thread_imc_mem_size
;
42 /* Trace IMC data structures */
43 static DEFINE_PER_CPU(u64
*, trace_imc_mem
);
44 static struct imc_pmu_ref
*trace_imc_refc
;
45 static int trace_imc_mem_size
;
47 static struct imc_pmu
*imc_event_to_pmu(struct perf_event
*event
)
49 return container_of(event
->pmu
, struct imc_pmu
, pmu
);
52 PMU_FORMAT_ATTR(event
, "config:0-61");
53 PMU_FORMAT_ATTR(offset
, "config:0-31");
54 PMU_FORMAT_ATTR(rvalue
, "config:32");
55 PMU_FORMAT_ATTR(mode
, "config:33-40");
56 static struct attribute
*imc_format_attrs
[] = {
57 &format_attr_event
.attr
,
58 &format_attr_offset
.attr
,
59 &format_attr_rvalue
.attr
,
60 &format_attr_mode
.attr
,
64 static struct attribute_group imc_format_group
= {
66 .attrs
= imc_format_attrs
,
69 /* Format attribute for imc trace-mode */
70 PMU_FORMAT_ATTR(cpmc_reserved
, "config:0-19");
71 PMU_FORMAT_ATTR(cpmc_event
, "config:20-27");
72 PMU_FORMAT_ATTR(cpmc_samplesel
, "config:28-29");
73 PMU_FORMAT_ATTR(cpmc_load
, "config:30-61");
74 static struct attribute
*trace_imc_format_attrs
[] = {
75 &format_attr_event
.attr
,
76 &format_attr_cpmc_reserved
.attr
,
77 &format_attr_cpmc_event
.attr
,
78 &format_attr_cpmc_samplesel
.attr
,
79 &format_attr_cpmc_load
.attr
,
83 static struct attribute_group trace_imc_format_group
= {
85 .attrs
= trace_imc_format_attrs
,
88 /* Get the cpumask printed to a buffer "buf" */
89 static ssize_t
imc_pmu_cpumask_get_attr(struct device
*dev
,
90 struct device_attribute
*attr
,
93 struct pmu
*pmu
= dev_get_drvdata(dev
);
94 struct imc_pmu
*imc_pmu
= container_of(pmu
, struct imc_pmu
, pmu
);
95 cpumask_t
*active_mask
;
97 switch(imc_pmu
->domain
){
99 active_mask
= &nest_imc_cpumask
;
101 case IMC_DOMAIN_CORE
:
102 active_mask
= &core_imc_cpumask
;
108 return cpumap_print_to_pagebuf(true, buf
, active_mask
);
111 static DEVICE_ATTR(cpumask
, S_IRUGO
, imc_pmu_cpumask_get_attr
, NULL
);
113 static struct attribute
*imc_pmu_cpumask_attrs
[] = {
114 &dev_attr_cpumask
.attr
,
118 static struct attribute_group imc_pmu_cpumask_attr_group
= {
119 .attrs
= imc_pmu_cpumask_attrs
,
122 /* device_str_attr_create : Populate event "name" and string "str" in attribute */
123 static struct attribute
*device_str_attr_create(const char *name
, const char *str
)
125 struct perf_pmu_events_attr
*attr
;
127 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
130 sysfs_attr_init(&attr
->attr
.attr
);
132 attr
->event_str
= str
;
133 attr
->attr
.attr
.name
= name
;
134 attr
->attr
.attr
.mode
= 0444;
135 attr
->attr
.show
= perf_event_sysfs_show
;
137 return &attr
->attr
.attr
;
140 static int imc_parse_event(struct device_node
*np
, const char *scale
,
141 const char *unit
, const char *prefix
,
142 u32 base
, struct imc_events
*event
)
147 if (of_property_read_u32(np
, "reg", ®
))
149 /* Add the base_reg value to the "reg" */
150 event
->value
= base
+ reg
;
152 if (of_property_read_string(np
, "event-name", &s
))
155 event
->name
= kasprintf(GFP_KERNEL
, "%s%s", prefix
, s
);
159 if (of_property_read_string(np
, "scale", &s
))
163 event
->scale
= kstrdup(s
, GFP_KERNEL
);
168 if (of_property_read_string(np
, "unit", &s
))
172 event
->unit
= kstrdup(s
, GFP_KERNEL
);
186 * imc_free_events: Function to cleanup the events list, having
189 static void imc_free_events(struct imc_events
*events
, int nr_entries
)
193 /* Nothing to clean, return */
196 for (i
= 0; i
< nr_entries
; i
++) {
197 kfree(events
[i
].unit
);
198 kfree(events
[i
].scale
);
199 kfree(events
[i
].name
);
206 * update_events_in_group: Update the "events" information in an attr_group
207 * and assign the attr_group to the pmu "pmu".
209 static int update_events_in_group(struct device_node
*node
, struct imc_pmu
*pmu
)
211 struct attribute_group
*attr_group
;
212 struct attribute
**attrs
, *dev_str
;
213 struct device_node
*np
, *pmu_events
;
214 u32 handle
, base_reg
;
215 int i
= 0, j
= 0, ct
, ret
;
216 const char *prefix
, *g_scale
, *g_unit
;
217 const char *ev_val_str
, *ev_scale_str
, *ev_unit_str
;
219 if (!of_property_read_u32(node
, "events", &handle
))
220 pmu_events
= of_find_node_by_phandle(handle
);
224 /* Did not find any node with a given phandle */
228 /* Get a count of number of child nodes */
229 ct
= of_get_child_count(pmu_events
);
231 /* Get the event prefix */
232 if (of_property_read_string(node
, "events-prefix", &prefix
))
235 /* Get a global unit and scale data if available */
236 if (of_property_read_string(node
, "scale", &g_scale
))
239 if (of_property_read_string(node
, "unit", &g_unit
))
242 /* "reg" property gives out the base offset of the counters data */
243 of_property_read_u32(node
, "reg", &base_reg
);
245 /* Allocate memory for the events */
246 pmu
->events
= kcalloc(ct
, sizeof(struct imc_events
), GFP_KERNEL
);
251 /* Parse the events and update the struct */
252 for_each_child_of_node(pmu_events
, np
) {
253 ret
= imc_parse_event(np
, g_scale
, g_unit
, prefix
, base_reg
, &pmu
->events
[ct
]);
258 /* Allocate memory for attribute group */
259 attr_group
= kzalloc(sizeof(*attr_group
), GFP_KERNEL
);
261 imc_free_events(pmu
->events
, ct
);
266 * Allocate memory for attributes.
267 * Since we have count of events for this pmu, we also allocate
268 * memory for the scale and unit attribute for now.
269 * "ct" has the total event structs added from the events-parent node.
270 * So allocate three times the "ct" (this includes event, event_scale and
273 attrs
= kcalloc(((ct
* 3) + 1), sizeof(struct attribute
*), GFP_KERNEL
);
276 imc_free_events(pmu
->events
, ct
);
280 attr_group
->name
= "events";
281 attr_group
->attrs
= attrs
;
283 ev_val_str
= kasprintf(GFP_KERNEL
, "event=0x%x", pmu
->events
[i
].value
);
284 dev_str
= device_str_attr_create(pmu
->events
[i
].name
, ev_val_str
);
288 attrs
[j
++] = dev_str
;
289 if (pmu
->events
[i
].scale
) {
290 ev_scale_str
= kasprintf(GFP_KERNEL
, "%s.scale", pmu
->events
[i
].name
);
291 dev_str
= device_str_attr_create(ev_scale_str
, pmu
->events
[i
].scale
);
295 attrs
[j
++] = dev_str
;
298 if (pmu
->events
[i
].unit
) {
299 ev_unit_str
= kasprintf(GFP_KERNEL
, "%s.unit", pmu
->events
[i
].name
);
300 dev_str
= device_str_attr_create(ev_unit_str
, pmu
->events
[i
].unit
);
304 attrs
[j
++] = dev_str
;
308 /* Save the event attribute */
309 pmu
->attr_groups
[IMC_EVENT_ATTR
] = attr_group
;
314 /* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */
315 static struct imc_pmu_ref
*get_nest_pmu_ref(int cpu
)
317 return per_cpu(local_nest_imc_refc
, cpu
);
320 static void nest_change_cpu_context(int old_cpu
, int new_cpu
)
322 struct imc_pmu
**pn
= per_nest_pmu_arr
;
324 if (old_cpu
< 0 || new_cpu
< 0)
328 perf_pmu_migrate_context(&(*pn
)->pmu
, old_cpu
, new_cpu
);
333 static int ppc_nest_imc_cpu_offline(unsigned int cpu
)
335 int nid
, target
= -1;
336 const struct cpumask
*l_cpumask
;
337 struct imc_pmu_ref
*ref
;
340 * Check in the designated list for this cpu. Dont bother
341 * if not one of them.
343 if (!cpumask_test_and_clear_cpu(cpu
, &nest_imc_cpumask
))
347 * Check whether nest_imc is registered. We could end up here if the
348 * cpuhotplug callback registration fails. i.e, callback invokes the
349 * offline path for all successfully registered nodes. At this stage,
350 * nest_imc pmu will not be registered and we should return here.
352 * We return with a zero since this is not an offline failure. And
353 * cpuhp_setup_state() returns the actual failure reason to the caller,
354 * which in turn will call the cleanup routine.
360 * Now that this cpu is one of the designated,
361 * find a next cpu a) which is online and b) in same chip.
363 nid
= cpu_to_node(cpu
);
364 l_cpumask
= cpumask_of_node(nid
);
365 target
= cpumask_last(l_cpumask
);
368 * If this(target) is the last cpu in the cpumask for this chip,
369 * check for any possible online cpu in the chip.
371 if (unlikely(target
== cpu
))
372 target
= cpumask_any_but(l_cpumask
, cpu
);
375 * Update the cpumask with the target cpu and
376 * migrate the context if needed
378 if (target
>= 0 && target
< nr_cpu_ids
) {
379 cpumask_set_cpu(target
, &nest_imc_cpumask
);
380 nest_change_cpu_context(cpu
, target
);
382 opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST
,
383 get_hard_smp_processor_id(cpu
));
385 * If this is the last cpu in this chip then, skip the reference
386 * count mutex lock and make the reference count on this chip zero.
388 ref
= get_nest_pmu_ref(cpu
);
397 static int ppc_nest_imc_cpu_online(unsigned int cpu
)
399 const struct cpumask
*l_cpumask
;
400 static struct cpumask tmp_mask
;
403 /* Get the cpumask of this node */
404 l_cpumask
= cpumask_of_node(cpu_to_node(cpu
));
407 * If this is not the first online CPU on this node, then
410 if (cpumask_and(&tmp_mask
, l_cpumask
, &nest_imc_cpumask
))
414 * If this is the first online cpu on this node
415 * disable the nest counters by making an OPAL call.
417 res
= opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST
,
418 get_hard_smp_processor_id(cpu
));
422 /* Make this CPU the designated target for counter collection */
423 cpumask_set_cpu(cpu
, &nest_imc_cpumask
);
427 static int nest_pmu_cpumask_init(void)
429 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE
,
430 "perf/powerpc/imc:online",
431 ppc_nest_imc_cpu_online
,
432 ppc_nest_imc_cpu_offline
);
435 static void nest_imc_counters_release(struct perf_event
*event
)
438 struct imc_pmu_ref
*ref
;
443 node_id
= cpu_to_node(event
->cpu
);
446 * See if we need to disable the nest PMU.
447 * If no events are currently in use, then we have to take a
448 * mutex to ensure that we don't race with another task doing
449 * enable or disable the nest counters.
451 ref
= get_nest_pmu_ref(event
->cpu
);
455 /* Take the mutex lock for this node and then decrement the reference count */
456 mutex_lock(&ref
->lock
);
457 if (ref
->refc
== 0) {
459 * The scenario where this is true is, when perf session is
460 * started, followed by offlining of all cpus in a given node.
462 * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline()
463 * function set the ref->count to zero, if the cpu which is
464 * about to offline is the last cpu in a given node and make
465 * an OPAL call to disable the engine in that node.
468 mutex_unlock(&ref
->lock
);
472 if (ref
->refc
== 0) {
473 rc
= opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST
,
474 get_hard_smp_processor_id(event
->cpu
));
476 mutex_unlock(&ref
->lock
);
477 pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id
);
480 } else if (ref
->refc
< 0) {
481 WARN(1, "nest-imc: Invalid event reference count\n");
484 mutex_unlock(&ref
->lock
);
487 static int nest_imc_event_init(struct perf_event
*event
)
489 int chip_id
, rc
, node_id
;
490 u32 l_config
, config
= event
->attr
.config
;
491 struct imc_mem_info
*pcni
;
493 struct imc_pmu_ref
*ref
;
496 if (event
->attr
.type
!= event
->pmu
->type
)
499 /* Sampling not supported */
500 if (event
->hw
.sample_period
)
506 pmu
= imc_event_to_pmu(event
);
508 /* Sanity check for config (event offset) */
509 if ((config
& IMC_EVENT_OFFSET_MASK
) > pmu
->counter_mem_size
)
513 * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
514 * Get the base memory addresss for this cpu.
516 chip_id
= cpu_to_chip_id(event
->cpu
);
518 /* Return, if chip_id is not valid */
522 pcni
= pmu
->mem_info
;
524 if (pcni
->id
== chip_id
) {
529 } while (pcni
->vbase
!= 0);
535 * Add the event offset to the base address.
537 l_config
= config
& IMC_EVENT_OFFSET_MASK
;
538 event
->hw
.event_base
= (u64
)pcni
->vbase
+ l_config
;
539 node_id
= cpu_to_node(event
->cpu
);
542 * Get the imc_pmu_ref struct for this node.
543 * Take the mutex lock and then increment the count of nest pmu events
546 ref
= get_nest_pmu_ref(event
->cpu
);
550 mutex_lock(&ref
->lock
);
551 if (ref
->refc
== 0) {
552 rc
= opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST
,
553 get_hard_smp_processor_id(event
->cpu
));
555 mutex_unlock(&ref
->lock
);
556 pr_err("nest-imc: Unable to start the counters for node %d\n",
562 mutex_unlock(&ref
->lock
);
564 event
->destroy
= nest_imc_counters_release
;
569 * core_imc_mem_init : Initializes memory for the current core.
571 * Uses alloc_pages_node() and uses the returned address as an argument to
572 * an opal call to configure the pdbar. The address sent as an argument is
573 * converted to physical address before the opal call is made. This is the
574 * base address at which the core imc counters are populated.
576 static int core_imc_mem_init(int cpu
, int size
)
578 int nid
, rc
= 0, core_id
= (cpu
/ threads_per_core
);
579 struct imc_mem_info
*mem_info
;
583 * alloc_pages_node() will allocate memory for core in the
586 nid
= cpu_to_node(cpu
);
587 mem_info
= &core_imc_pmu
->mem_info
[core_id
];
588 mem_info
->id
= core_id
;
590 /* We need only vbase for core counters */
591 page
= alloc_pages_node(nid
,
592 GFP_KERNEL
| __GFP_ZERO
| __GFP_THISNODE
|
593 __GFP_NOWARN
, get_order(size
));
596 mem_info
->vbase
= page_address(page
);
599 core_imc_refc
[core_id
].id
= core_id
;
600 mutex_init(&core_imc_refc
[core_id
].lock
);
602 rc
= opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE
,
603 __pa((void *)mem_info
->vbase
),
604 get_hard_smp_processor_id(cpu
));
606 free_pages((u64
)mem_info
->vbase
, get_order(size
));
607 mem_info
->vbase
= NULL
;
613 static bool is_core_imc_mem_inited(int cpu
)
615 struct imc_mem_info
*mem_info
;
616 int core_id
= (cpu
/ threads_per_core
);
618 mem_info
= &core_imc_pmu
->mem_info
[core_id
];
619 if (!mem_info
->vbase
)
625 static int ppc_core_imc_cpu_online(unsigned int cpu
)
627 const struct cpumask
*l_cpumask
;
628 static struct cpumask tmp_mask
;
631 /* Get the cpumask for this core */
632 l_cpumask
= cpu_sibling_mask(cpu
);
634 /* If a cpu for this core is already set, then, don't do anything */
635 if (cpumask_and(&tmp_mask
, l_cpumask
, &core_imc_cpumask
))
638 if (!is_core_imc_mem_inited(cpu
)) {
639 ret
= core_imc_mem_init(cpu
, core_imc_pmu
->counter_mem_size
);
641 pr_info("core_imc memory allocation for cpu %d failed\n", cpu
);
646 /* set the cpu in the mask */
647 cpumask_set_cpu(cpu
, &core_imc_cpumask
);
651 static int ppc_core_imc_cpu_offline(unsigned int cpu
)
653 unsigned int core_id
;
655 struct imc_pmu_ref
*ref
;
658 * clear this cpu out of the mask, if not present in the mask,
659 * don't bother doing anything.
661 if (!cpumask_test_and_clear_cpu(cpu
, &core_imc_cpumask
))
665 * Check whether core_imc is registered. We could end up here
666 * if the cpuhotplug callback registration fails. i.e, callback
667 * invokes the offline path for all sucessfully registered cpus.
668 * At this stage, core_imc pmu will not be registered and we
669 * should return here.
671 * We return with a zero since this is not an offline failure.
672 * And cpuhp_setup_state() returns the actual failure reason
673 * to the caller, which inturn will call the cleanup routine.
675 if (!core_imc_pmu
->pmu
.event_init
)
678 /* Find any online cpu in that core except the current "cpu" */
679 ncpu
= cpumask_last(cpu_sibling_mask(cpu
));
681 if (unlikely(ncpu
== cpu
))
682 ncpu
= cpumask_any_but(cpu_sibling_mask(cpu
), cpu
);
684 if (ncpu
>= 0 && ncpu
< nr_cpu_ids
) {
685 cpumask_set_cpu(ncpu
, &core_imc_cpumask
);
686 perf_pmu_migrate_context(&core_imc_pmu
->pmu
, cpu
, ncpu
);
689 * If this is the last cpu in this core then, skip taking refernce
690 * count mutex lock for this core and directly zero "refc" for
693 opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE
,
694 get_hard_smp_processor_id(cpu
));
695 core_id
= cpu
/ threads_per_core
;
696 ref
= &core_imc_refc
[core_id
];
705 static int core_imc_pmu_cpumask_init(void)
707 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE
,
708 "perf/powerpc/imc_core:online",
709 ppc_core_imc_cpu_online
,
710 ppc_core_imc_cpu_offline
);
713 static void core_imc_counters_release(struct perf_event
*event
)
716 struct imc_pmu_ref
*ref
;
721 * See if we need to disable the IMC PMU.
722 * If no events are currently in use, then we have to take a
723 * mutex to ensure that we don't race with another task doing
724 * enable or disable the core counters.
726 core_id
= event
->cpu
/ threads_per_core
;
728 /* Take the mutex lock and decrement the refernce count for this core */
729 ref
= &core_imc_refc
[core_id
];
733 mutex_lock(&ref
->lock
);
734 if (ref
->refc
== 0) {
736 * The scenario where this is true is, when perf session is
737 * started, followed by offlining of all cpus in a given core.
739 * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
740 * function set the ref->count to zero, if the cpu which is
741 * about to offline is the last cpu in a given core and make
742 * an OPAL call to disable the engine in that core.
745 mutex_unlock(&ref
->lock
);
749 if (ref
->refc
== 0) {
750 rc
= opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE
,
751 get_hard_smp_processor_id(event
->cpu
));
753 mutex_unlock(&ref
->lock
);
754 pr_err("IMC: Unable to stop the counters for core %d\n", core_id
);
757 } else if (ref
->refc
< 0) {
758 WARN(1, "core-imc: Invalid event reference count\n");
761 mutex_unlock(&ref
->lock
);
764 static int core_imc_event_init(struct perf_event
*event
)
767 u64 config
= event
->attr
.config
;
768 struct imc_mem_info
*pcmi
;
770 struct imc_pmu_ref
*ref
;
772 if (event
->attr
.type
!= event
->pmu
->type
)
775 /* Sampling not supported */
776 if (event
->hw
.sample_period
)
783 pmu
= imc_event_to_pmu(event
);
785 /* Sanity check for config (event offset) */
786 if (((config
& IMC_EVENT_OFFSET_MASK
) > pmu
->counter_mem_size
))
789 if (!is_core_imc_mem_inited(event
->cpu
))
792 core_id
= event
->cpu
/ threads_per_core
;
793 pcmi
= &core_imc_pmu
->mem_info
[core_id
];
797 /* Get the core_imc mutex for this core */
798 ref
= &core_imc_refc
[core_id
];
803 * Core pmu units are enabled only when it is used.
804 * See if this is triggered for the first time.
805 * If yes, take the mutex lock and enable the core counters.
806 * If not, just increment the count in core_imc_refc struct.
808 mutex_lock(&ref
->lock
);
809 if (ref
->refc
== 0) {
810 rc
= opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE
,
811 get_hard_smp_processor_id(event
->cpu
));
813 mutex_unlock(&ref
->lock
);
814 pr_err("core-imc: Unable to start the counters for core %d\n",
820 mutex_unlock(&ref
->lock
);
822 event
->hw
.event_base
= (u64
)pcmi
->vbase
+ (config
& IMC_EVENT_OFFSET_MASK
);
823 event
->destroy
= core_imc_counters_release
;
828 * Allocates a page of memory for each of the online cpus, and load
830 * The physical base address of the page allocated for a cpu will be
831 * written to the LDBAR for that cpu, when the thread-imc event
834 * LDBAR Register Layout:
836 * 0 4 8 12 16 20 24 28
837 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
838 * | | [ ] [ Counter Address [8:50]
843 * 32 36 40 44 48 52 56 60
844 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
845 * Counter Address [8:50] ]
848 static int thread_imc_mem_alloc(int cpu_id
, int size
)
850 u64
*local_mem
= per_cpu(thread_imc_mem
, cpu_id
);
851 int nid
= cpu_to_node(cpu_id
);
856 * This case could happen only once at start, since we dont
857 * free the memory in cpu offline path.
859 page
= alloc_pages_node(nid
,
860 GFP_KERNEL
| __GFP_ZERO
| __GFP_THISNODE
|
861 __GFP_NOWARN
, get_order(size
));
864 local_mem
= page_address(page
);
866 per_cpu(thread_imc_mem
, cpu_id
) = local_mem
;
869 mtspr(SPRN_LDBAR
, 0);
873 static int ppc_thread_imc_cpu_online(unsigned int cpu
)
875 return thread_imc_mem_alloc(cpu
, thread_imc_mem_size
);
878 static int ppc_thread_imc_cpu_offline(unsigned int cpu
)
880 mtspr(SPRN_LDBAR
, 0);
884 static int thread_imc_cpu_init(void)
886 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE
,
887 "perf/powerpc/imc_thread:online",
888 ppc_thread_imc_cpu_online
,
889 ppc_thread_imc_cpu_offline
);
892 static int thread_imc_event_init(struct perf_event
*event
)
894 u32 config
= event
->attr
.config
;
895 struct task_struct
*target
;
898 if (event
->attr
.type
!= event
->pmu
->type
)
901 if (!capable(CAP_SYS_ADMIN
))
904 /* Sampling not supported */
905 if (event
->hw
.sample_period
)
909 pmu
= imc_event_to_pmu(event
);
911 /* Sanity check for config offset */
912 if (((config
& IMC_EVENT_OFFSET_MASK
) > pmu
->counter_mem_size
))
915 target
= event
->hw
.target
;
919 event
->pmu
->task_ctx_nr
= perf_sw_context
;
923 static bool is_thread_imc_pmu(struct perf_event
*event
)
925 if (!strncmp(event
->pmu
->name
, "thread_imc", strlen("thread_imc")))
931 static u64
* get_event_base_addr(struct perf_event
*event
)
935 if (is_thread_imc_pmu(event
)) {
936 addr
= (u64
)per_cpu(thread_imc_mem
, smp_processor_id());
937 return (u64
*)(addr
+ (event
->attr
.config
& IMC_EVENT_OFFSET_MASK
));
940 return (u64
*)event
->hw
.event_base
;
943 static void thread_imc_pmu_start_txn(struct pmu
*pmu
,
944 unsigned int txn_flags
)
946 if (txn_flags
& ~PERF_PMU_TXN_ADD
)
948 perf_pmu_disable(pmu
);
951 static void thread_imc_pmu_cancel_txn(struct pmu
*pmu
)
953 perf_pmu_enable(pmu
);
956 static int thread_imc_pmu_commit_txn(struct pmu
*pmu
)
958 perf_pmu_enable(pmu
);
962 static u64
imc_read_counter(struct perf_event
*event
)
967 * In-Memory Collection (IMC) counters are free flowing counters.
968 * So we take a snapshot of the counter value on enable and save it
969 * to calculate the delta at later stage to present the event counter
972 addr
= get_event_base_addr(event
);
973 data
= be64_to_cpu(READ_ONCE(*addr
));
974 local64_set(&event
->hw
.prev_count
, data
);
979 static void imc_event_update(struct perf_event
*event
)
981 u64 counter_prev
, counter_new
, final_count
;
983 counter_prev
= local64_read(&event
->hw
.prev_count
);
984 counter_new
= imc_read_counter(event
);
985 final_count
= counter_new
- counter_prev
;
987 /* Update the delta to the event count */
988 local64_add(final_count
, &event
->count
);
991 static void imc_event_start(struct perf_event
*event
, int flags
)
994 * In Memory Counters are free flowing counters. HW or the microcode
995 * keeps adding to the counter offset in memory. To get event
996 * counter value, we snapshot the value here and we calculate
997 * delta at later point.
999 imc_read_counter(event
);
1002 static void imc_event_stop(struct perf_event
*event
, int flags
)
1005 * Take a snapshot and calculate the delta and update
1006 * the event counter values.
1008 imc_event_update(event
);
1011 static int imc_event_add(struct perf_event
*event
, int flags
)
1013 if (flags
& PERF_EF_START
)
1014 imc_event_start(event
, flags
);
1019 static int thread_imc_event_add(struct perf_event
*event
, int flags
)
1022 struct imc_pmu_ref
*ref
;
1023 u64 ldbar_value
, *local_mem
= per_cpu(thread_imc_mem
, smp_processor_id());
1025 if (flags
& PERF_EF_START
)
1026 imc_event_start(event
, flags
);
1028 if (!is_core_imc_mem_inited(smp_processor_id()))
1031 core_id
= smp_processor_id() / threads_per_core
;
1032 ldbar_value
= ((u64
)local_mem
& THREAD_IMC_LDBAR_MASK
) | THREAD_IMC_ENABLE
;
1033 mtspr(SPRN_LDBAR
, ldbar_value
);
1036 * imc pmus are enabled only when it is used.
1037 * See if this is triggered for the first time.
1038 * If yes, take the mutex lock and enable the counters.
1039 * If not, just increment the count in ref count struct.
1041 ref
= &core_imc_refc
[core_id
];
1045 mutex_lock(&ref
->lock
);
1046 if (ref
->refc
== 0) {
1047 if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE
,
1048 get_hard_smp_processor_id(smp_processor_id()))) {
1049 mutex_unlock(&ref
->lock
);
1050 pr_err("thread-imc: Unable to start the counter\
1051 for core %d\n", core_id
);
1056 mutex_unlock(&ref
->lock
);
1060 static void thread_imc_event_del(struct perf_event
*event
, int flags
)
1064 struct imc_pmu_ref
*ref
;
1066 mtspr(SPRN_LDBAR
, 0);
1068 core_id
= smp_processor_id() / threads_per_core
;
1069 ref
= &core_imc_refc
[core_id
];
1071 mutex_lock(&ref
->lock
);
1073 if (ref
->refc
== 0) {
1074 if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE
,
1075 get_hard_smp_processor_id(smp_processor_id()))) {
1076 mutex_unlock(&ref
->lock
);
1077 pr_err("thread-imc: Unable to stop the counters\
1078 for core %d\n", core_id
);
1081 } else if (ref
->refc
< 0) {
1084 mutex_unlock(&ref
->lock
);
1086 * Take a snapshot and calculate the delta and update
1087 * the event counter values.
1089 imc_event_update(event
);
1093 * Allocate a page of memory for each cpu, and load LDBAR with 0.
1095 static int trace_imc_mem_alloc(int cpu_id
, int size
)
1097 u64
*local_mem
= per_cpu(trace_imc_mem
, cpu_id
);
1098 int phys_id
= cpu_to_node(cpu_id
), rc
= 0;
1099 int core_id
= (cpu_id
/ threads_per_core
);
1104 page
= alloc_pages_node(phys_id
,
1105 GFP_KERNEL
| __GFP_ZERO
| __GFP_THISNODE
|
1106 __GFP_NOWARN
, get_order(size
));
1109 local_mem
= page_address(page
);
1110 per_cpu(trace_imc_mem
, cpu_id
) = local_mem
;
1112 /* Initialise the counters for trace mode */
1113 rc
= opal_imc_counters_init(OPAL_IMC_COUNTERS_TRACE
, __pa((void *)local_mem
),
1114 get_hard_smp_processor_id(cpu_id
));
1116 pr_info("IMC:opal init failed for trace imc\n");
1121 /* Init the mutex, if not already */
1122 trace_imc_refc
[core_id
].id
= core_id
;
1123 mutex_init(&trace_imc_refc
[core_id
].lock
);
1125 mtspr(SPRN_LDBAR
, 0);
1129 static int ppc_trace_imc_cpu_online(unsigned int cpu
)
1131 return trace_imc_mem_alloc(cpu
, trace_imc_mem_size
);
1134 static int ppc_trace_imc_cpu_offline(unsigned int cpu
)
1136 mtspr(SPRN_LDBAR
, 0);
1140 static int trace_imc_cpu_init(void)
1142 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE
,
1143 "perf/powerpc/imc_trace:online",
1144 ppc_trace_imc_cpu_online
,
1145 ppc_trace_imc_cpu_offline
);
1148 static u64
get_trace_imc_event_base_addr(void)
1150 return (u64
)per_cpu(trace_imc_mem
, smp_processor_id());
1154 * Function to parse trace-imc data obtained
1155 * and to prepare the perf sample.
1157 static int trace_imc_prepare_sample(struct trace_imc_data
*mem
,
1158 struct perf_sample_data
*data
,
1160 struct perf_event_header
*header
,
1161 struct perf_event
*event
)
1163 /* Sanity checks for a valid record */
1164 if (be64_to_cpu(READ_ONCE(mem
->tb1
)) > *prev_tb
)
1165 *prev_tb
= be64_to_cpu(READ_ONCE(mem
->tb1
));
1169 if ((be64_to_cpu(READ_ONCE(mem
->tb1
)) & IMC_TRACE_RECORD_TB1_MASK
) !=
1170 be64_to_cpu(READ_ONCE(mem
->tb2
)))
1173 /* Prepare perf sample */
1174 data
->ip
= be64_to_cpu(READ_ONCE(mem
->ip
));
1175 data
->period
= event
->hw
.last_period
;
1177 header
->type
= PERF_RECORD_SAMPLE
;
1178 header
->size
= sizeof(*header
) + event
->header_size
;
1181 if (is_kernel_addr(data
->ip
))
1182 header
->misc
|= PERF_RECORD_MISC_KERNEL
;
1184 header
->misc
|= PERF_RECORD_MISC_USER
;
1186 perf_event_header__init_id(header
, data
, event
);
1191 static void dump_trace_imc_data(struct perf_event
*event
)
1193 struct trace_imc_data
*mem
;
1197 mem
= (struct trace_imc_data
*)get_trace_imc_event_base_addr();
1198 for (i
= 0; i
< (trace_imc_mem_size
/ sizeof(struct trace_imc_data
));
1200 struct perf_sample_data data
;
1201 struct perf_event_header header
;
1203 ret
= trace_imc_prepare_sample(mem
, &data
, &prev_tb
, &header
, event
);
1204 if (ret
) /* Exit, if not a valid record */
1207 /* If this is a valid record, create the sample */
1208 struct perf_output_handle handle
;
1210 if (perf_output_begin(&handle
, event
, header
.size
))
1213 perf_output_sample(&handle
, &header
, &data
, event
);
1214 perf_output_end(&handle
);
1219 static int trace_imc_event_add(struct perf_event
*event
, int flags
)
1221 int core_id
= smp_processor_id() / threads_per_core
;
1222 struct imc_pmu_ref
*ref
= NULL
;
1223 u64 local_mem
, ldbar_value
;
1225 /* Set trace-imc bit in ldbar and load ldbar with per-thread memory address */
1226 local_mem
= get_trace_imc_event_base_addr();
1227 ldbar_value
= ((u64
)local_mem
& THREAD_IMC_LDBAR_MASK
) | TRACE_IMC_ENABLE
;
1230 ref
= &core_imc_refc
[core_id
];
1232 /* If core-imc is not enabled, use trace-imc reference count */
1234 ref
= &trace_imc_refc
[core_id
];
1238 mtspr(SPRN_LDBAR
, ldbar_value
);
1239 mutex_lock(&ref
->lock
);
1240 if (ref
->refc
== 0) {
1241 if (opal_imc_counters_start(OPAL_IMC_COUNTERS_TRACE
,
1242 get_hard_smp_processor_id(smp_processor_id()))) {
1243 mutex_unlock(&ref
->lock
);
1244 pr_err("trace-imc: Unable to start the counters for core %d\n", core_id
);
1245 mtspr(SPRN_LDBAR
, 0);
1250 mutex_unlock(&ref
->lock
);
1255 static void trace_imc_event_read(struct perf_event
*event
)
1260 static void trace_imc_event_stop(struct perf_event
*event
, int flags
)
1262 u64 local_mem
= get_trace_imc_event_base_addr();
1263 dump_trace_imc_data(event
);
1264 memset((void *)local_mem
, 0, sizeof(u64
));
1267 static void trace_imc_event_start(struct perf_event
*event
, int flags
)
1272 static void trace_imc_event_del(struct perf_event
*event
, int flags
)
1274 int core_id
= smp_processor_id() / threads_per_core
;
1275 struct imc_pmu_ref
*ref
= NULL
;
1278 ref
= &core_imc_refc
[core_id
];
1280 /* If core-imc is not enabled, use trace-imc reference count */
1282 ref
= &trace_imc_refc
[core_id
];
1286 mtspr(SPRN_LDBAR
, 0);
1287 mutex_lock(&ref
->lock
);
1289 if (ref
->refc
== 0) {
1290 if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_TRACE
,
1291 get_hard_smp_processor_id(smp_processor_id()))) {
1292 mutex_unlock(&ref
->lock
);
1293 pr_err("trace-imc: Unable to stop the counters for core %d\n", core_id
);
1296 } else if (ref
->refc
< 0) {
1299 mutex_unlock(&ref
->lock
);
1300 trace_imc_event_stop(event
, flags
);
1303 static int trace_imc_event_init(struct perf_event
*event
)
1305 struct task_struct
*target
;
1307 if (event
->attr
.type
!= event
->pmu
->type
)
1310 if (!capable(CAP_SYS_ADMIN
))
1313 /* Return if this is a couting event */
1314 if (event
->attr
.sample_period
== 0)
1318 target
= event
->hw
.target
;
1320 event
->pmu
->task_ctx_nr
= perf_hw_context
;
1324 /* update_pmu_ops : Populate the appropriate operations for "pmu" */
1325 static int update_pmu_ops(struct imc_pmu
*pmu
)
1327 pmu
->pmu
.task_ctx_nr
= perf_invalid_context
;
1328 pmu
->pmu
.add
= imc_event_add
;
1329 pmu
->pmu
.del
= imc_event_stop
;
1330 pmu
->pmu
.start
= imc_event_start
;
1331 pmu
->pmu
.stop
= imc_event_stop
;
1332 pmu
->pmu
.read
= imc_event_update
;
1333 pmu
->pmu
.attr_groups
= pmu
->attr_groups
;
1334 pmu
->pmu
.capabilities
= PERF_PMU_CAP_NO_EXCLUDE
;
1335 pmu
->attr_groups
[IMC_FORMAT_ATTR
] = &imc_format_group
;
1337 switch (pmu
->domain
) {
1338 case IMC_DOMAIN_NEST
:
1339 pmu
->pmu
.event_init
= nest_imc_event_init
;
1340 pmu
->attr_groups
[IMC_CPUMASK_ATTR
] = &imc_pmu_cpumask_attr_group
;
1342 case IMC_DOMAIN_CORE
:
1343 pmu
->pmu
.event_init
= core_imc_event_init
;
1344 pmu
->attr_groups
[IMC_CPUMASK_ATTR
] = &imc_pmu_cpumask_attr_group
;
1346 case IMC_DOMAIN_THREAD
:
1347 pmu
->pmu
.event_init
= thread_imc_event_init
;
1348 pmu
->pmu
.add
= thread_imc_event_add
;
1349 pmu
->pmu
.del
= thread_imc_event_del
;
1350 pmu
->pmu
.start_txn
= thread_imc_pmu_start_txn
;
1351 pmu
->pmu
.cancel_txn
= thread_imc_pmu_cancel_txn
;
1352 pmu
->pmu
.commit_txn
= thread_imc_pmu_commit_txn
;
1354 case IMC_DOMAIN_TRACE
:
1355 pmu
->pmu
.event_init
= trace_imc_event_init
;
1356 pmu
->pmu
.add
= trace_imc_event_add
;
1357 pmu
->pmu
.del
= trace_imc_event_del
;
1358 pmu
->pmu
.start
= trace_imc_event_start
;
1359 pmu
->pmu
.stop
= trace_imc_event_stop
;
1360 pmu
->pmu
.read
= trace_imc_event_read
;
1361 pmu
->attr_groups
[IMC_FORMAT_ATTR
] = &trace_imc_format_group
;
1369 /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
1370 static int init_nest_pmu_ref(void)
1374 nest_imc_refc
= kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc
),
1381 for_each_node(nid
) {
1383 * Mutex lock to avoid races while tracking the number of
1384 * sessions using the chip's nest pmu units.
1386 mutex_init(&nest_imc_refc
[i
].lock
);
1389 * Loop to init the "id" with the node_id. Variable "i" initialized to
1390 * 0 and will be used as index to the array. "i" will not go off the
1391 * end of the array since the "for_each_node" loops for "N_POSSIBLE"
1394 nest_imc_refc
[i
++].id
= nid
;
1398 * Loop to init the per_cpu "local_nest_imc_refc" with the proper
1399 * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
1401 for_each_possible_cpu(cpu
) {
1402 nid
= cpu_to_node(cpu
);
1403 for (i
= 0; i
< num_possible_nodes(); i
++) {
1404 if (nest_imc_refc
[i
].id
== nid
) {
1405 per_cpu(local_nest_imc_refc
, cpu
) = &nest_imc_refc
[i
];
1413 static void cleanup_all_core_imc_memory(void)
1415 int i
, nr_cores
= DIV_ROUND_UP(num_possible_cpus(), threads_per_core
);
1416 struct imc_mem_info
*ptr
= core_imc_pmu
->mem_info
;
1417 int size
= core_imc_pmu
->counter_mem_size
;
1419 /* mem_info will never be NULL */
1420 for (i
= 0; i
< nr_cores
; i
++) {
1422 free_pages((u64
)ptr
[i
].vbase
, get_order(size
));
1426 kfree(core_imc_refc
);
1429 static void thread_imc_ldbar_disable(void *dummy
)
1432 * By Zeroing LDBAR, we disable thread-imc
1435 mtspr(SPRN_LDBAR
, 0);
1438 void thread_imc_disable(void)
1440 on_each_cpu(thread_imc_ldbar_disable
, NULL
, 1);
1443 static void cleanup_all_thread_imc_memory(void)
1445 int i
, order
= get_order(thread_imc_mem_size
);
1447 for_each_online_cpu(i
) {
1448 if (per_cpu(thread_imc_mem
, i
))
1449 free_pages((u64
)per_cpu(thread_imc_mem
, i
), order
);
1454 static void cleanup_all_trace_imc_memory(void)
1456 int i
, order
= get_order(trace_imc_mem_size
);
1458 for_each_online_cpu(i
) {
1459 if (per_cpu(trace_imc_mem
, i
))
1460 free_pages((u64
)per_cpu(trace_imc_mem
, i
), order
);
1463 kfree(trace_imc_refc
);
1466 /* Function to free the attr_groups which are dynamically allocated */
1467 static void imc_common_mem_free(struct imc_pmu
*pmu_ptr
)
1469 if (pmu_ptr
->attr_groups
[IMC_EVENT_ATTR
])
1470 kfree(pmu_ptr
->attr_groups
[IMC_EVENT_ATTR
]->attrs
);
1471 kfree(pmu_ptr
->attr_groups
[IMC_EVENT_ATTR
]);
1475 * Common function to unregister cpu hotplug callback and
1477 * TODO: Need to handle pmu unregistering, which will be
1478 * done in followup series.
1480 static void imc_common_cpuhp_mem_free(struct imc_pmu
*pmu_ptr
)
1482 if (pmu_ptr
->domain
== IMC_DOMAIN_NEST
) {
1483 mutex_lock(&nest_init_lock
);
1484 if (nest_pmus
== 1) {
1485 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE
);
1486 kfree(nest_imc_refc
);
1487 kfree(per_nest_pmu_arr
);
1488 per_nest_pmu_arr
= NULL
;
1493 mutex_unlock(&nest_init_lock
);
1496 /* Free core_imc memory */
1497 if (pmu_ptr
->domain
== IMC_DOMAIN_CORE
) {
1498 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE
);
1499 cleanup_all_core_imc_memory();
1502 /* Free thread_imc memory */
1503 if (pmu_ptr
->domain
== IMC_DOMAIN_THREAD
) {
1504 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE
);
1505 cleanup_all_thread_imc_memory();
1508 if (pmu_ptr
->domain
== IMC_DOMAIN_TRACE
) {
1509 cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_TRACE_IMC_ONLINE
);
1510 cleanup_all_trace_imc_memory();
1515 * Function to unregister thread-imc if core-imc
1516 * is not registered.
1518 void unregister_thread_imc(void)
1520 imc_common_cpuhp_mem_free(thread_imc_pmu
);
1521 imc_common_mem_free(thread_imc_pmu
);
1522 perf_pmu_unregister(&thread_imc_pmu
->pmu
);
1526 * imc_mem_init : Function to support memory allocation for core imc.
1528 static int imc_mem_init(struct imc_pmu
*pmu_ptr
, struct device_node
*parent
,
1532 int nr_cores
, cpu
, res
= -ENOMEM
;
1534 if (of_property_read_string(parent
, "name", &s
))
1537 switch (pmu_ptr
->domain
) {
1538 case IMC_DOMAIN_NEST
:
1539 /* Update the pmu name */
1540 pmu_ptr
->pmu
.name
= kasprintf(GFP_KERNEL
, "%s%s_imc", "nest_", s
);
1541 if (!pmu_ptr
->pmu
.name
)
1544 /* Needed for hotplug/migration */
1545 if (!per_nest_pmu_arr
) {
1546 per_nest_pmu_arr
= kcalloc(get_max_nest_dev() + 1,
1547 sizeof(struct imc_pmu
*),
1549 if (!per_nest_pmu_arr
)
1552 per_nest_pmu_arr
[pmu_index
] = pmu_ptr
;
1554 case IMC_DOMAIN_CORE
:
1555 /* Update the pmu name */
1556 pmu_ptr
->pmu
.name
= kasprintf(GFP_KERNEL
, "%s%s", s
, "_imc");
1557 if (!pmu_ptr
->pmu
.name
)
1560 nr_cores
= DIV_ROUND_UP(num_possible_cpus(), threads_per_core
);
1561 pmu_ptr
->mem_info
= kcalloc(nr_cores
, sizeof(struct imc_mem_info
),
1564 if (!pmu_ptr
->mem_info
)
1567 core_imc_refc
= kcalloc(nr_cores
, sizeof(struct imc_pmu_ref
),
1570 if (!core_imc_refc
) {
1571 kfree(pmu_ptr
->mem_info
);
1575 core_imc_pmu
= pmu_ptr
;
1577 case IMC_DOMAIN_THREAD
:
1578 /* Update the pmu name */
1579 pmu_ptr
->pmu
.name
= kasprintf(GFP_KERNEL
, "%s%s", s
, "_imc");
1580 if (!pmu_ptr
->pmu
.name
)
1583 thread_imc_mem_size
= pmu_ptr
->counter_mem_size
;
1584 for_each_online_cpu(cpu
) {
1585 res
= thread_imc_mem_alloc(cpu
, pmu_ptr
->counter_mem_size
);
1587 cleanup_all_thread_imc_memory();
1592 thread_imc_pmu
= pmu_ptr
;
1594 case IMC_DOMAIN_TRACE
:
1595 /* Update the pmu name */
1596 pmu_ptr
->pmu
.name
= kasprintf(GFP_KERNEL
, "%s%s", s
, "_imc");
1597 if (!pmu_ptr
->pmu
.name
)
1600 nr_cores
= DIV_ROUND_UP(num_possible_cpus(), threads_per_core
);
1601 trace_imc_refc
= kcalloc(nr_cores
, sizeof(struct imc_pmu_ref
),
1603 if (!trace_imc_refc
)
1606 trace_imc_mem_size
= pmu_ptr
->counter_mem_size
;
1607 for_each_online_cpu(cpu
) {
1608 res
= trace_imc_mem_alloc(cpu
, trace_imc_mem_size
);
1610 cleanup_all_trace_imc_memory();
1625 * init_imc_pmu : Setup and register the IMC pmu device.
1627 * @parent: Device tree unit node
1628 * @pmu_ptr: memory allocated for this pmu
1629 * @pmu_idx: Count of nest pmc registered
1631 * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
1632 * Handles failure cases and accordingly frees memory.
1634 int init_imc_pmu(struct device_node
*parent
, struct imc_pmu
*pmu_ptr
, int pmu_idx
)
1638 ret
= imc_mem_init(pmu_ptr
, parent
, pmu_idx
);
1642 switch (pmu_ptr
->domain
) {
1643 case IMC_DOMAIN_NEST
:
1645 * Nest imc pmu need only one cpu per chip, we initialize the
1646 * cpumask for the first nest imc pmu and use the same for the
1647 * rest. To handle the cpuhotplug callback unregister, we track
1648 * the number of nest pmus in "nest_pmus".
1650 mutex_lock(&nest_init_lock
);
1651 if (nest_pmus
== 0) {
1652 ret
= init_nest_pmu_ref();
1654 mutex_unlock(&nest_init_lock
);
1655 kfree(per_nest_pmu_arr
);
1656 per_nest_pmu_arr
= NULL
;
1659 /* Register for cpu hotplug notification. */
1660 ret
= nest_pmu_cpumask_init();
1662 mutex_unlock(&nest_init_lock
);
1663 kfree(nest_imc_refc
);
1664 kfree(per_nest_pmu_arr
);
1665 per_nest_pmu_arr
= NULL
;
1670 mutex_unlock(&nest_init_lock
);
1672 case IMC_DOMAIN_CORE
:
1673 ret
= core_imc_pmu_cpumask_init();
1675 cleanup_all_core_imc_memory();
1680 case IMC_DOMAIN_THREAD
:
1681 ret
= thread_imc_cpu_init();
1683 cleanup_all_thread_imc_memory();
1688 case IMC_DOMAIN_TRACE
:
1689 ret
= trace_imc_cpu_init();
1691 cleanup_all_trace_imc_memory();
1697 return -EINVAL
; /* Unknown domain */
1700 ret
= update_events_in_group(parent
, pmu_ptr
);
1702 goto err_free_cpuhp_mem
;
1704 ret
= update_pmu_ops(pmu_ptr
);
1706 goto err_free_cpuhp_mem
;
1708 ret
= perf_pmu_register(&pmu_ptr
->pmu
, pmu_ptr
->pmu
.name
, -1);
1710 goto err_free_cpuhp_mem
;
1712 pr_debug("%s performance monitor hardware support registered\n",
1718 imc_common_cpuhp_mem_free(pmu_ptr
);
1720 imc_common_mem_free(pmu_ptr
);