xref: /openbmc/linux/arch/powerpc/perf/imc-pmu.c (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
1 /*
2  * In-Memory Collection (IMC) Performance Monitor counter support.
3  *
4  * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
5  *           (C) 2017 Anju T Sudhakar, IBM Corporation.
6  *           (C) 2017 Hemant K Shaw, IBM Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or later version.
12  */
13 #include <linux/perf_event.h>
14 #include <linux/slab.h>
15 #include <asm/opal.h>
16 #include <asm/imc-pmu.h>
17 #include <asm/cputhreads.h>
18 #include <asm/smp.h>
19 #include <linux/string.h>
20 
21 /* Nest IMC data structures and variables */
22 
23 /*
24  * Used to avoid races in counting the nest-pmu units during hotplug
25  * register and unregister
26  */
27 static DEFINE_MUTEX(nest_init_lock);
28 static DEFINE_PER_CPU(struct imc_pmu_ref *, local_nest_imc_refc);
29 static struct imc_pmu **per_nest_pmu_arr;
30 static cpumask_t nest_imc_cpumask;
31 struct imc_pmu_ref *nest_imc_refc;
32 static int nest_pmus;
33 
34 /* Core IMC data structures and variables */
35 
36 static cpumask_t core_imc_cpumask;
37 struct imc_pmu_ref *core_imc_refc;
38 static struct imc_pmu *core_imc_pmu;
39 
40 /* Thread IMC data structures and variables */
41 
42 static DEFINE_PER_CPU(u64 *, thread_imc_mem);
43 static struct imc_pmu *thread_imc_pmu;
44 static int thread_imc_mem_size;
45 
46 struct imc_pmu *imc_event_to_pmu(struct perf_event *event)
47 {
48 	return container_of(event->pmu, struct imc_pmu, pmu);
49 }
50 
51 PMU_FORMAT_ATTR(event, "config:0-40");
52 PMU_FORMAT_ATTR(offset, "config:0-31");
53 PMU_FORMAT_ATTR(rvalue, "config:32");
54 PMU_FORMAT_ATTR(mode, "config:33-40");
55 static struct attribute *imc_format_attrs[] = {
56 	&format_attr_event.attr,
57 	&format_attr_offset.attr,
58 	&format_attr_rvalue.attr,
59 	&format_attr_mode.attr,
60 	NULL,
61 };
62 
63 static struct attribute_group imc_format_group = {
64 	.name = "format",
65 	.attrs = imc_format_attrs,
66 };
67 
68 /* Get the cpumask printed to a buffer "buf" */
69 static ssize_t imc_pmu_cpumask_get_attr(struct device *dev,
70 					struct device_attribute *attr,
71 					char *buf)
72 {
73 	struct pmu *pmu = dev_get_drvdata(dev);
74 	struct imc_pmu *imc_pmu = container_of(pmu, struct imc_pmu, pmu);
75 	cpumask_t *active_mask;
76 
77 	switch(imc_pmu->domain){
78 	case IMC_DOMAIN_NEST:
79 		active_mask = &nest_imc_cpumask;
80 		break;
81 	case IMC_DOMAIN_CORE:
82 		active_mask = &core_imc_cpumask;
83 		break;
84 	default:
85 		return 0;
86 	}
87 
88 	return cpumap_print_to_pagebuf(true, buf, active_mask);
89 }
90 
91 static DEVICE_ATTR(cpumask, S_IRUGO, imc_pmu_cpumask_get_attr, NULL);
92 
93 static struct attribute *imc_pmu_cpumask_attrs[] = {
94 	&dev_attr_cpumask.attr,
95 	NULL,
96 };
97 
98 static struct attribute_group imc_pmu_cpumask_attr_group = {
99 	.attrs = imc_pmu_cpumask_attrs,
100 };
101 
102 /* device_str_attr_create : Populate event "name" and string "str" in attribute */
103 static struct attribute *device_str_attr_create(const char *name, const char *str)
104 {
105 	struct perf_pmu_events_attr *attr;
106 
107 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
108 	if (!attr)
109 		return NULL;
110 	sysfs_attr_init(&attr->attr.attr);
111 
112 	attr->event_str = str;
113 	attr->attr.attr.name = name;
114 	attr->attr.attr.mode = 0444;
115 	attr->attr.show = perf_event_sysfs_show;
116 
117 	return &attr->attr.attr;
118 }
119 
120 struct imc_events *imc_parse_event(struct device_node *np, const char *scale,
121 				  const char *unit, const char *prefix, u32 base)
122 {
123 	struct imc_events *event;
124 	const char *s;
125 	u32 reg;
126 
127 	event = kzalloc(sizeof(struct imc_events), GFP_KERNEL);
128 	if (!event)
129 		return NULL;
130 
131 	if (of_property_read_u32(np, "reg", &reg))
132 		goto error;
133 	/* Add the base_reg value to the "reg" */
134 	event->value = base + reg;
135 
136 	if (of_property_read_string(np, "event-name", &s))
137 		goto error;
138 
139 	event->name = kasprintf(GFP_KERNEL, "%s%s", prefix, s);
140 	if (!event->name)
141 		goto error;
142 
143 	if (of_property_read_string(np, "scale", &s))
144 		s = scale;
145 
146 	if (s) {
147 		event->scale = kstrdup(s, GFP_KERNEL);
148 		if (!event->scale)
149 			goto error;
150 	}
151 
152 	if (of_property_read_string(np, "unit", &s))
153 		s = unit;
154 
155 	if (s) {
156 		event->unit = kstrdup(s, GFP_KERNEL);
157 		if (!event->unit)
158 			goto error;
159 	}
160 
161 	return event;
162 error:
163 	kfree(event->unit);
164 	kfree(event->scale);
165 	kfree(event->name);
166 	kfree(event);
167 
168 	return NULL;
169 }
170 
171 /*
172  * update_events_in_group: Update the "events" information in an attr_group
173  *                         and assign the attr_group to the pmu "pmu".
174  */
175 static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
176 {
177 	struct attribute_group *attr_group;
178 	struct attribute **attrs, *dev_str;
179 	struct device_node *np, *pmu_events;
180 	struct imc_events *ev;
181 	u32 handle, base_reg;
182 	int i=0, j=0, ct;
183 	const char *prefix, *g_scale, *g_unit;
184 	const char *ev_val_str, *ev_scale_str, *ev_unit_str;
185 
186 	if (!of_property_read_u32(node, "events", &handle))
187 		pmu_events = of_find_node_by_phandle(handle);
188 	else
189 		return 0;
190 
191 	/* Did not find any node with a given phandle */
192 	if (!pmu_events)
193 		return 0;
194 
195 	/* Get a count of number of child nodes */
196 	ct = of_get_child_count(pmu_events);
197 
198 	/* Get the event prefix */
199 	if (of_property_read_string(node, "events-prefix", &prefix))
200 		return 0;
201 
202 	/* Get a global unit and scale data if available */
203 	if (of_property_read_string(node, "scale", &g_scale))
204 		g_scale = NULL;
205 
206 	if (of_property_read_string(node, "unit", &g_unit))
207 		g_unit = NULL;
208 
209 	/* "reg" property gives out the base offset of the counters data */
210 	of_property_read_u32(node, "reg", &base_reg);
211 
212 	/* Allocate memory for the events */
213 	pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
214 	if (!pmu->events)
215 		return -ENOMEM;
216 
217 	ct = 0;
218 	/* Parse the events and update the struct */
219 	for_each_child_of_node(pmu_events, np) {
220 		ev = imc_parse_event(np, g_scale, g_unit, prefix, base_reg);
221 		if (ev)
222 			pmu->events[ct++] = ev;
223 	}
224 
225 	/* Allocate memory for attribute group */
226 	attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
227 	if (!attr_group)
228 		return -ENOMEM;
229 
230 	/*
231 	 * Allocate memory for attributes.
232 	 * Since we have count of events for this pmu, we also allocate
233 	 * memory for the scale and unit attribute for now.
234 	 * "ct" has the total event structs added from the events-parent node.
235 	 * So allocate three times the "ct" (this includes event, event_scale and
236 	 * event_unit).
237 	 */
238 	attrs = kcalloc(((ct * 3) + 1), sizeof(struct attribute *), GFP_KERNEL);
239 	if (!attrs) {
240 		kfree(attr_group);
241 		kfree(pmu->events);
242 		return -ENOMEM;
243 	}
244 
245 	attr_group->name = "events";
246 	attr_group->attrs = attrs;
247 	do {
248 		ev_val_str = kasprintf(GFP_KERNEL, "event=0x%x", pmu->events[i]->value);
249 		dev_str = device_str_attr_create(pmu->events[i]->name, ev_val_str);
250 		if (!dev_str)
251 			continue;
252 
253 		attrs[j++] = dev_str;
254 		if (pmu->events[i]->scale) {
255 			ev_scale_str = kasprintf(GFP_KERNEL, "%s.scale",pmu->events[i]->name);
256 			dev_str = device_str_attr_create(ev_scale_str, pmu->events[i]->scale);
257 			if (!dev_str)
258 				continue;
259 
260 			attrs[j++] = dev_str;
261 		}
262 
263 		if (pmu->events[i]->unit) {
264 			ev_unit_str = kasprintf(GFP_KERNEL, "%s.unit",pmu->events[i]->name);
265 			dev_str = device_str_attr_create(ev_unit_str, pmu->events[i]->unit);
266 			if (!dev_str)
267 				continue;
268 
269 			attrs[j++] = dev_str;
270 		}
271 	} while (++i < ct);
272 
273 	/* Save the event attribute */
274 	pmu->attr_groups[IMC_EVENT_ATTR] = attr_group;
275 
276 	kfree(pmu->events);
277 	return 0;
278 }
279 
280 /* get_nest_pmu_ref: Return the imc_pmu_ref struct for the given node */
281 static struct imc_pmu_ref *get_nest_pmu_ref(int cpu)
282 {
283 	return per_cpu(local_nest_imc_refc, cpu);
284 }
285 
286 static void nest_change_cpu_context(int old_cpu, int new_cpu)
287 {
288 	struct imc_pmu **pn = per_nest_pmu_arr;
289 
290 	if (old_cpu < 0 || new_cpu < 0)
291 		return;
292 
293 	while (*pn) {
294 		perf_pmu_migrate_context(&(*pn)->pmu, old_cpu, new_cpu);
295 		pn++;
296 	}
297 }
298 
299 static int ppc_nest_imc_cpu_offline(unsigned int cpu)
300 {
301 	int nid, target = -1;
302 	const struct cpumask *l_cpumask;
303 	struct imc_pmu_ref *ref;
304 
305 	/*
306 	 * Check in the designated list for this cpu. Dont bother
307 	 * if not one of them.
308 	 */
309 	if (!cpumask_test_and_clear_cpu(cpu, &nest_imc_cpumask))
310 		return 0;
311 
312 	/*
313 	 * Check whether nest_imc is registered. We could end up here if the
314 	 * cpuhotplug callback registration fails. i.e, callback invokes the
315 	 * offline path for all successfully registered nodes. At this stage,
316 	 * nest_imc pmu will not be registered and we should return here.
317 	 *
318 	 * We return with a zero since this is not an offline failure. And
319 	 * cpuhp_setup_state() returns the actual failure reason to the caller,
320 	 * which in turn will call the cleanup routine.
321 	 */
322 	if (!nest_pmus)
323 		return 0;
324 
325 	/*
326 	 * Now that this cpu is one of the designated,
327 	 * find a next cpu a) which is online and b) in same chip.
328 	 */
329 	nid = cpu_to_node(cpu);
330 	l_cpumask = cpumask_of_node(nid);
331 	target = cpumask_any_but(l_cpumask, cpu);
332 
333 	/*
334 	 * Update the cpumask with the target cpu and
335 	 * migrate the context if needed
336 	 */
337 	if (target >= 0 && target < nr_cpu_ids) {
338 		cpumask_set_cpu(target, &nest_imc_cpumask);
339 		nest_change_cpu_context(cpu, target);
340 	} else {
341 		opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
342 				       get_hard_smp_processor_id(cpu));
343 		/*
344 		 * If this is the last cpu in this chip then, skip the reference
345 		 * count mutex lock and make the reference count on this chip zero.
346 		 */
347 		ref = get_nest_pmu_ref(cpu);
348 		if (!ref)
349 			return -EINVAL;
350 
351 		ref->refc = 0;
352 	}
353 	return 0;
354 }
355 
356 static int ppc_nest_imc_cpu_online(unsigned int cpu)
357 {
358 	const struct cpumask *l_cpumask;
359 	static struct cpumask tmp_mask;
360 	int res;
361 
362 	/* Get the cpumask of this node */
363 	l_cpumask = cpumask_of_node(cpu_to_node(cpu));
364 
365 	/*
366 	 * If this is not the first online CPU on this node, then
367 	 * just return.
368 	 */
369 	if (cpumask_and(&tmp_mask, l_cpumask, &nest_imc_cpumask))
370 		return 0;
371 
372 	/*
373 	 * If this is the first online cpu on this node
374 	 * disable the nest counters by making an OPAL call.
375 	 */
376 	res = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
377 				     get_hard_smp_processor_id(cpu));
378 	if (res)
379 		return res;
380 
381 	/* Make this CPU the designated target for counter collection */
382 	cpumask_set_cpu(cpu, &nest_imc_cpumask);
383 	return 0;
384 }
385 
386 static int nest_pmu_cpumask_init(void)
387 {
388 	return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE,
389 				 "perf/powerpc/imc:online",
390 				 ppc_nest_imc_cpu_online,
391 				 ppc_nest_imc_cpu_offline);
392 }
393 
394 static void nest_imc_counters_release(struct perf_event *event)
395 {
396 	int rc, node_id;
397 	struct imc_pmu_ref *ref;
398 
399 	if (event->cpu < 0)
400 		return;
401 
402 	node_id = cpu_to_node(event->cpu);
403 
404 	/*
405 	 * See if we need to disable the nest PMU.
406 	 * If no events are currently in use, then we have to take a
407 	 * mutex to ensure that we don't race with another task doing
408 	 * enable or disable the nest counters.
409 	 */
410 	ref = get_nest_pmu_ref(event->cpu);
411 	if (!ref)
412 		return;
413 
414 	/* Take the mutex lock for this node and then decrement the reference count */
415 	mutex_lock(&ref->lock);
416 	if (ref->refc == 0) {
417 		/*
418 		 * The scenario where this is true is, when perf session is
419 		 * started, followed by offlining of all cpus in a given node.
420 		 *
421 		 * In the cpuhotplug offline path, ppc_nest_imc_cpu_offline()
422 		 * function set the ref->count to zero, if the cpu which is
423 		 * about to offline is the last cpu in a given node and make
424 		 * an OPAL call to disable the engine in that node.
425 		 *
426 		 */
427 		mutex_unlock(&ref->lock);
428 		return;
429 	}
430 	ref->refc--;
431 	if (ref->refc == 0) {
432 		rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_NEST,
433 					    get_hard_smp_processor_id(event->cpu));
434 		if (rc) {
435 			mutex_unlock(&ref->lock);
436 			pr_err("nest-imc: Unable to stop the counters for core %d\n", node_id);
437 			return;
438 		}
439 	} else if (ref->refc < 0) {
440 		WARN(1, "nest-imc: Invalid event reference count\n");
441 		ref->refc = 0;
442 	}
443 	mutex_unlock(&ref->lock);
444 }
445 
446 static int nest_imc_event_init(struct perf_event *event)
447 {
448 	int chip_id, rc, node_id;
449 	u32 l_config, config = event->attr.config;
450 	struct imc_mem_info *pcni;
451 	struct imc_pmu *pmu;
452 	struct imc_pmu_ref *ref;
453 	bool flag = false;
454 
455 	if (event->attr.type != event->pmu->type)
456 		return -ENOENT;
457 
458 	/* Sampling not supported */
459 	if (event->hw.sample_period)
460 		return -EINVAL;
461 
462 	/* unsupported modes and filters */
463 	if (event->attr.exclude_user   ||
464 	    event->attr.exclude_kernel ||
465 	    event->attr.exclude_hv     ||
466 	    event->attr.exclude_idle   ||
467 	    event->attr.exclude_host   ||
468 	    event->attr.exclude_guest)
469 		return -EINVAL;
470 
471 	if (event->cpu < 0)
472 		return -EINVAL;
473 
474 	pmu = imc_event_to_pmu(event);
475 
476 	/* Sanity check for config (event offset) */
477 	if ((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size)
478 		return -EINVAL;
479 
480 	/*
481 	 * Nest HW counter memory resides in a per-chip reserve-memory (HOMER).
482 	 * Get the base memory addresss for this cpu.
483 	 */
484 	chip_id = cpu_to_chip_id(event->cpu);
485 	pcni = pmu->mem_info;
486 	do {
487 		if (pcni->id == chip_id) {
488 			flag = true;
489 			break;
490 		}
491 		pcni++;
492 	} while (pcni);
493 
494 	if (!flag)
495 		return -ENODEV;
496 
497 	/*
498 	 * Add the event offset to the base address.
499 	 */
500 	l_config = config & IMC_EVENT_OFFSET_MASK;
501 	event->hw.event_base = (u64)pcni->vbase + l_config;
502 	node_id = cpu_to_node(event->cpu);
503 
504 	/*
505 	 * Get the imc_pmu_ref struct for this node.
506 	 * Take the mutex lock and then increment the count of nest pmu events
507 	 * inited.
508 	 */
509 	ref = get_nest_pmu_ref(event->cpu);
510 	if (!ref)
511 		return -EINVAL;
512 
513 	mutex_lock(&ref->lock);
514 	if (ref->refc == 0) {
515 		rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_NEST,
516 					     get_hard_smp_processor_id(event->cpu));
517 		if (rc) {
518 			mutex_unlock(&ref->lock);
519 			pr_err("nest-imc: Unable to start the counters for node %d\n",
520 									node_id);
521 			return rc;
522 		}
523 	}
524 	++ref->refc;
525 	mutex_unlock(&ref->lock);
526 
527 	event->destroy = nest_imc_counters_release;
528 	return 0;
529 }
530 
531 /*
532  * core_imc_mem_init : Initializes memory for the current core.
533  *
534  * Uses alloc_pages_node() and uses the returned address as an argument to
535  * an opal call to configure the pdbar. The address sent as an argument is
536  * converted to physical address before the opal call is made. This is the
537  * base address at which the core imc counters are populated.
538  */
539 static int core_imc_mem_init(int cpu, int size)
540 {
541 	int nid, rc = 0, core_id = (cpu / threads_per_core);
542 	struct imc_mem_info *mem_info;
543 
544 	/*
545 	 * alloc_pages_node() will allocate memory for core in the
546 	 * local node only.
547 	 */
548 	nid = cpu_to_node(cpu);
549 	mem_info = &core_imc_pmu->mem_info[core_id];
550 	mem_info->id = core_id;
551 
552 	/* We need only vbase for core counters */
553 	mem_info->vbase = page_address(alloc_pages_node(nid,
554 					  GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
555 					  __GFP_NOWARN, get_order(size)));
556 	if (!mem_info->vbase)
557 		return -ENOMEM;
558 
559 	/* Init the mutex */
560 	core_imc_refc[core_id].id = core_id;
561 	mutex_init(&core_imc_refc[core_id].lock);
562 
563 	rc = opal_imc_counters_init(OPAL_IMC_COUNTERS_CORE,
564 				__pa((void *)mem_info->vbase),
565 				get_hard_smp_processor_id(cpu));
566 	if (rc) {
567 		free_pages((u64)mem_info->vbase, get_order(size));
568 		mem_info->vbase = NULL;
569 	}
570 
571 	return rc;
572 }
573 
574 static bool is_core_imc_mem_inited(int cpu)
575 {
576 	struct imc_mem_info *mem_info;
577 	int core_id = (cpu / threads_per_core);
578 
579 	mem_info = &core_imc_pmu->mem_info[core_id];
580 	if (!mem_info->vbase)
581 		return false;
582 
583 	return true;
584 }
585 
586 static int ppc_core_imc_cpu_online(unsigned int cpu)
587 {
588 	const struct cpumask *l_cpumask;
589 	static struct cpumask tmp_mask;
590 	int ret = 0;
591 
592 	/* Get the cpumask for this core */
593 	l_cpumask = cpu_sibling_mask(cpu);
594 
595 	/* If a cpu for this core is already set, then, don't do anything */
596 	if (cpumask_and(&tmp_mask, l_cpumask, &core_imc_cpumask))
597 		return 0;
598 
599 	if (!is_core_imc_mem_inited(cpu)) {
600 		ret = core_imc_mem_init(cpu, core_imc_pmu->counter_mem_size);
601 		if (ret) {
602 			pr_info("core_imc memory allocation for cpu %d failed\n", cpu);
603 			return ret;
604 		}
605 	}
606 
607 	/* set the cpu in the mask */
608 	cpumask_set_cpu(cpu, &core_imc_cpumask);
609 	return 0;
610 }
611 
612 static int ppc_core_imc_cpu_offline(unsigned int cpu)
613 {
614 	unsigned int ncpu, core_id;
615 	struct imc_pmu_ref *ref;
616 
617 	/*
618 	 * clear this cpu out of the mask, if not present in the mask,
619 	 * don't bother doing anything.
620 	 */
621 	if (!cpumask_test_and_clear_cpu(cpu, &core_imc_cpumask))
622 		return 0;
623 
624 	/*
625 	 * Check whether core_imc is registered. We could end up here
626 	 * if the cpuhotplug callback registration fails. i.e, callback
627 	 * invokes the offline path for all sucessfully registered cpus.
628 	 * At this stage, core_imc pmu will not be registered and we
629 	 * should return here.
630 	 *
631 	 * We return with a zero since this is not an offline failure.
632 	 * And cpuhp_setup_state() returns the actual failure reason
633 	 * to the caller, which inturn will call the cleanup routine.
634 	 */
635 	if (!core_imc_pmu->pmu.event_init)
636 		return 0;
637 
638 	/* Find any online cpu in that core except the current "cpu" */
639 	ncpu = cpumask_any_but(cpu_sibling_mask(cpu), cpu);
640 
641 	if (ncpu >= 0 && ncpu < nr_cpu_ids) {
642 		cpumask_set_cpu(ncpu, &core_imc_cpumask);
643 		perf_pmu_migrate_context(&core_imc_pmu->pmu, cpu, ncpu);
644 	} else {
645 		/*
646 		 * If this is the last cpu in this core then, skip taking refernce
647 		 * count mutex lock for this core and directly zero "refc" for
648 		 * this core.
649 		 */
650 		opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
651 				       get_hard_smp_processor_id(cpu));
652 		core_id = cpu / threads_per_core;
653 		ref = &core_imc_refc[core_id];
654 		if (!ref)
655 			return -EINVAL;
656 
657 		ref->refc = 0;
658 	}
659 	return 0;
660 }
661 
662 static int core_imc_pmu_cpumask_init(void)
663 {
664 	return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE,
665 				 "perf/powerpc/imc_core:online",
666 				 ppc_core_imc_cpu_online,
667 				 ppc_core_imc_cpu_offline);
668 }
669 
670 static void core_imc_counters_release(struct perf_event *event)
671 {
672 	int rc, core_id;
673 	struct imc_pmu_ref *ref;
674 
675 	if (event->cpu < 0)
676 		return;
677 	/*
678 	 * See if we need to disable the IMC PMU.
679 	 * If no events are currently in use, then we have to take a
680 	 * mutex to ensure that we don't race with another task doing
681 	 * enable or disable the core counters.
682 	 */
683 	core_id = event->cpu / threads_per_core;
684 
685 	/* Take the mutex lock and decrement the refernce count for this core */
686 	ref = &core_imc_refc[core_id];
687 	if (!ref)
688 		return;
689 
690 	mutex_lock(&ref->lock);
691 	if (ref->refc == 0) {
692 		/*
693 		 * The scenario where this is true is, when perf session is
694 		 * started, followed by offlining of all cpus in a given core.
695 		 *
696 		 * In the cpuhotplug offline path, ppc_core_imc_cpu_offline()
697 		 * function set the ref->count to zero, if the cpu which is
698 		 * about to offline is the last cpu in a given core and make
699 		 * an OPAL call to disable the engine in that core.
700 		 *
701 		 */
702 		mutex_unlock(&ref->lock);
703 		return;
704 	}
705 	ref->refc--;
706 	if (ref->refc == 0) {
707 		rc = opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
708 					    get_hard_smp_processor_id(event->cpu));
709 		if (rc) {
710 			mutex_unlock(&ref->lock);
711 			pr_err("IMC: Unable to stop the counters for core %d\n", core_id);
712 			return;
713 		}
714 	} else if (ref->refc < 0) {
715 		WARN(1, "core-imc: Invalid event reference count\n");
716 		ref->refc = 0;
717 	}
718 	mutex_unlock(&ref->lock);
719 }
720 
721 static int core_imc_event_init(struct perf_event *event)
722 {
723 	int core_id, rc;
724 	u64 config = event->attr.config;
725 	struct imc_mem_info *pcmi;
726 	struct imc_pmu *pmu;
727 	struct imc_pmu_ref *ref;
728 
729 	if (event->attr.type != event->pmu->type)
730 		return -ENOENT;
731 
732 	/* Sampling not supported */
733 	if (event->hw.sample_period)
734 		return -EINVAL;
735 
736 	/* unsupported modes and filters */
737 	if (event->attr.exclude_user   ||
738 	    event->attr.exclude_kernel ||
739 	    event->attr.exclude_hv     ||
740 	    event->attr.exclude_idle   ||
741 	    event->attr.exclude_host   ||
742 	    event->attr.exclude_guest)
743 		return -EINVAL;
744 
745 	if (event->cpu < 0)
746 		return -EINVAL;
747 
748 	event->hw.idx = -1;
749 	pmu = imc_event_to_pmu(event);
750 
751 	/* Sanity check for config (event offset) */
752 	if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
753 		return -EINVAL;
754 
755 	if (!is_core_imc_mem_inited(event->cpu))
756 		return -ENODEV;
757 
758 	core_id = event->cpu / threads_per_core;
759 	pcmi = &core_imc_pmu->mem_info[core_id];
760 	if ((!pcmi->vbase))
761 		return -ENODEV;
762 
763 	/* Get the core_imc mutex for this core */
764 	ref = &core_imc_refc[core_id];
765 	if (!ref)
766 		return -EINVAL;
767 
768 	/*
769 	 * Core pmu units are enabled only when it is used.
770 	 * See if this is triggered for the first time.
771 	 * If yes, take the mutex lock and enable the core counters.
772 	 * If not, just increment the count in core_imc_refc struct.
773 	 */
774 	mutex_lock(&ref->lock);
775 	if (ref->refc == 0) {
776 		rc = opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
777 					     get_hard_smp_processor_id(event->cpu));
778 		if (rc) {
779 			mutex_unlock(&ref->lock);
780 			pr_err("core-imc: Unable to start the counters for core %d\n",
781 									core_id);
782 			return rc;
783 		}
784 	}
785 	++ref->refc;
786 	mutex_unlock(&ref->lock);
787 
788 	event->hw.event_base = (u64)pcmi->vbase + (config & IMC_EVENT_OFFSET_MASK);
789 	event->destroy = core_imc_counters_release;
790 	return 0;
791 }
792 
793 /*
794  * Allocates a page of memory for each of the online cpus, and write the
795  * physical base address of that page to the LDBAR for that cpu.
796  *
797  * LDBAR Register Layout:
798  *
799  *  0          4         8         12        16        20        24        28
800  * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
801  *   | |       [   ]    [                   Counter Address [8:50]
802  *   | * Mode    |
803  *   |           * PB Scope
804  *   * Enable/Disable
805  *
806  *  32        36        40        44        48        52        56        60
807  * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
808  *           Counter Address [8:50]              ]
809  *
810  */
811 static int thread_imc_mem_alloc(int cpu_id, int size)
812 {
813 	u64 ldbar_value, *local_mem = per_cpu(thread_imc_mem, cpu_id);
814 	int nid = cpu_to_node(cpu_id);
815 
816 	if (!local_mem) {
817 		/*
818 		 * This case could happen only once at start, since we dont
819 		 * free the memory in cpu offline path.
820 		 */
821 		local_mem = page_address(alloc_pages_node(nid,
822 				  GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE |
823 				  __GFP_NOWARN, get_order(size)));
824 		if (!local_mem)
825 			return -ENOMEM;
826 
827 		per_cpu(thread_imc_mem, cpu_id) = local_mem;
828 	}
829 
830 	ldbar_value = ((u64)local_mem & THREAD_IMC_LDBAR_MASK) | THREAD_IMC_ENABLE;
831 
832 	mtspr(SPRN_LDBAR, ldbar_value);
833 	return 0;
834 }
835 
836 static int ppc_thread_imc_cpu_online(unsigned int cpu)
837 {
838 	return thread_imc_mem_alloc(cpu, thread_imc_mem_size);
839 }
840 
841 static int ppc_thread_imc_cpu_offline(unsigned int cpu)
842 {
843 	mtspr(SPRN_LDBAR, 0);
844 	return 0;
845 }
846 
847 static int thread_imc_cpu_init(void)
848 {
849 	return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE,
850 			  "perf/powerpc/imc_thread:online",
851 			  ppc_thread_imc_cpu_online,
852 			  ppc_thread_imc_cpu_offline);
853 }
854 
855 void thread_imc_pmu_sched_task(struct perf_event_context *ctx,
856 				      bool sched_in)
857 {
858 	int core_id;
859 	struct imc_pmu_ref *ref;
860 
861 	if (!is_core_imc_mem_inited(smp_processor_id()))
862 		return;
863 
864 	core_id = smp_processor_id() / threads_per_core;
865 	/*
866 	 * imc pmus are enabled only when it is used.
867 	 * See if this is triggered for the first time.
868 	 * If yes, take the mutex lock and enable the counters.
869 	 * If not, just increment the count in ref count struct.
870 	 */
871 	ref = &core_imc_refc[core_id];
872 	if (!ref)
873 		return;
874 
875 	if (sched_in) {
876 		mutex_lock(&ref->lock);
877 		if (ref->refc == 0) {
878 			if (opal_imc_counters_start(OPAL_IMC_COUNTERS_CORE,
879 			     get_hard_smp_processor_id(smp_processor_id()))) {
880 				mutex_unlock(&ref->lock);
881 				pr_err("thread-imc: Unable to start the counter\
882 							for core %d\n", core_id);
883 				return;
884 			}
885 		}
886 		++ref->refc;
887 		mutex_unlock(&ref->lock);
888 	} else {
889 		mutex_lock(&ref->lock);
890 		ref->refc--;
891 		if (ref->refc == 0) {
892 			if (opal_imc_counters_stop(OPAL_IMC_COUNTERS_CORE,
893 			    get_hard_smp_processor_id(smp_processor_id()))) {
894 				mutex_unlock(&ref->lock);
895 				pr_err("thread-imc: Unable to stop the counters\
896 							for core %d\n", core_id);
897 				return;
898 			}
899 		} else if (ref->refc < 0) {
900 			ref->refc = 0;
901 		}
902 		mutex_unlock(&ref->lock);
903 	}
904 
905 	return;
906 }
907 
908 static int thread_imc_event_init(struct perf_event *event)
909 {
910 	u32 config = event->attr.config;
911 	struct task_struct *target;
912 	struct imc_pmu *pmu;
913 
914 	if (event->attr.type != event->pmu->type)
915 		return -ENOENT;
916 
917 	/* Sampling not supported */
918 	if (event->hw.sample_period)
919 		return -EINVAL;
920 
921 	event->hw.idx = -1;
922 	pmu = imc_event_to_pmu(event);
923 
924 	/* Sanity check for config offset */
925 	if (((config & IMC_EVENT_OFFSET_MASK) > pmu->counter_mem_size))
926 		return -EINVAL;
927 
928 	target = event->hw.target;
929 	if (!target)
930 		return -EINVAL;
931 
932 	event->pmu->task_ctx_nr = perf_sw_context;
933 	return 0;
934 }
935 
936 static bool is_thread_imc_pmu(struct perf_event *event)
937 {
938 	if (!strncmp(event->pmu->name, "thread_imc", strlen("thread_imc")))
939 		return true;
940 
941 	return false;
942 }
943 
944 static u64 * get_event_base_addr(struct perf_event *event)
945 {
946 	u64 addr;
947 
948 	if (is_thread_imc_pmu(event)) {
949 		addr = (u64)per_cpu(thread_imc_mem, smp_processor_id());
950 		return (u64 *)(addr + (event->attr.config & IMC_EVENT_OFFSET_MASK));
951 	}
952 
953 	return (u64 *)event->hw.event_base;
954 }
955 
956 static void thread_imc_pmu_start_txn(struct pmu *pmu,
957 				     unsigned int txn_flags)
958 {
959 	if (txn_flags & ~PERF_PMU_TXN_ADD)
960 		return;
961 	perf_pmu_disable(pmu);
962 }
963 
964 static void thread_imc_pmu_cancel_txn(struct pmu *pmu)
965 {
966 	perf_pmu_enable(pmu);
967 }
968 
969 static int thread_imc_pmu_commit_txn(struct pmu *pmu)
970 {
971 	perf_pmu_enable(pmu);
972 	return 0;
973 }
974 
975 static u64 imc_read_counter(struct perf_event *event)
976 {
977 	u64 *addr, data;
978 
979 	/*
980 	 * In-Memory Collection (IMC) counters are free flowing counters.
981 	 * So we take a snapshot of the counter value on enable and save it
982 	 * to calculate the delta at later stage to present the event counter
983 	 * value.
984 	 */
985 	addr = get_event_base_addr(event);
986 	data = be64_to_cpu(READ_ONCE(*addr));
987 	local64_set(&event->hw.prev_count, data);
988 
989 	return data;
990 }
991 
992 static void imc_event_update(struct perf_event *event)
993 {
994 	u64 counter_prev, counter_new, final_count;
995 
996 	counter_prev = local64_read(&event->hw.prev_count);
997 	counter_new = imc_read_counter(event);
998 	final_count = counter_new - counter_prev;
999 
1000 	/* Update the delta to the event count */
1001 	local64_add(final_count, &event->count);
1002 }
1003 
1004 static void imc_event_start(struct perf_event *event, int flags)
1005 {
1006 	/*
1007 	 * In Memory Counters are free flowing counters. HW or the microcode
1008 	 * keeps adding to the counter offset in memory. To get event
1009 	 * counter value, we snapshot the value here and we calculate
1010 	 * delta at later point.
1011 	 */
1012 	imc_read_counter(event);
1013 }
1014 
1015 static void imc_event_stop(struct perf_event *event, int flags)
1016 {
1017 	/*
1018 	 * Take a snapshot and calculate the delta and update
1019 	 * the event counter values.
1020 	 */
1021 	imc_event_update(event);
1022 }
1023 
1024 static int imc_event_add(struct perf_event *event, int flags)
1025 {
1026 	if (flags & PERF_EF_START)
1027 		imc_event_start(event, flags);
1028 
1029 	return 0;
1030 }
1031 
1032 static int thread_imc_event_add(struct perf_event *event, int flags)
1033 {
1034 	if (flags & PERF_EF_START)
1035 		imc_event_start(event, flags);
1036 
1037 	/* Enable the sched_task to start the engine */
1038 	perf_sched_cb_inc(event->ctx->pmu);
1039 	return 0;
1040 }
1041 
1042 static void thread_imc_event_del(struct perf_event *event, int flags)
1043 {
1044 	/*
1045 	 * Take a snapshot and calculate the delta and update
1046 	 * the event counter values.
1047 	 */
1048 	imc_event_update(event);
1049 	perf_sched_cb_dec(event->ctx->pmu);
1050 }
1051 
1052 /* update_pmu_ops : Populate the appropriate operations for "pmu" */
1053 static int update_pmu_ops(struct imc_pmu *pmu)
1054 {
1055 	pmu->pmu.task_ctx_nr = perf_invalid_context;
1056 	pmu->pmu.add = imc_event_add;
1057 	pmu->pmu.del = imc_event_stop;
1058 	pmu->pmu.start = imc_event_start;
1059 	pmu->pmu.stop = imc_event_stop;
1060 	pmu->pmu.read = imc_event_update;
1061 	pmu->pmu.attr_groups = pmu->attr_groups;
1062 	pmu->attr_groups[IMC_FORMAT_ATTR] = &imc_format_group;
1063 
1064 	switch (pmu->domain) {
1065 	case IMC_DOMAIN_NEST:
1066 		pmu->pmu.event_init = nest_imc_event_init;
1067 		pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
1068 		break;
1069 	case IMC_DOMAIN_CORE:
1070 		pmu->pmu.event_init = core_imc_event_init;
1071 		pmu->attr_groups[IMC_CPUMASK_ATTR] = &imc_pmu_cpumask_attr_group;
1072 		break;
1073 	case IMC_DOMAIN_THREAD:
1074 		pmu->pmu.event_init = thread_imc_event_init;
1075 		pmu->pmu.sched_task = thread_imc_pmu_sched_task;
1076 		pmu->pmu.add = thread_imc_event_add;
1077 		pmu->pmu.del = thread_imc_event_del;
1078 		pmu->pmu.start_txn = thread_imc_pmu_start_txn;
1079 		pmu->pmu.cancel_txn = thread_imc_pmu_cancel_txn;
1080 		pmu->pmu.commit_txn = thread_imc_pmu_commit_txn;
1081 		break;
1082 	default:
1083 		break;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
1089 /* init_nest_pmu_ref: Initialize the imc_pmu_ref struct for all the nodes */
1090 static int init_nest_pmu_ref(void)
1091 {
1092 	int nid, i, cpu;
1093 
1094 	nest_imc_refc = kcalloc(num_possible_nodes(), sizeof(*nest_imc_refc),
1095 								GFP_KERNEL);
1096 
1097 	if (!nest_imc_refc)
1098 		return -ENOMEM;
1099 
1100 	i = 0;
1101 	for_each_node(nid) {
1102 		/*
1103 		 * Mutex lock to avoid races while tracking the number of
1104 		 * sessions using the chip's nest pmu units.
1105 		 */
1106 		mutex_init(&nest_imc_refc[i].lock);
1107 
1108 		/*
1109 		 * Loop to init the "id" with the node_id. Variable "i" initialized to
1110 		 * 0 and will be used as index to the array. "i" will not go off the
1111 		 * end of the array since the "for_each_node" loops for "N_POSSIBLE"
1112 		 * nodes only.
1113 		 */
1114 		nest_imc_refc[i++].id = nid;
1115 	}
1116 
1117 	/*
1118 	 * Loop to init the per_cpu "local_nest_imc_refc" with the proper
1119 	 * "nest_imc_refc" index. This makes get_nest_pmu_ref() alot simple.
1120 	 */
1121 	for_each_possible_cpu(cpu) {
1122 		nid = cpu_to_node(cpu);
1123 		for (i = 0; i < num_possible_nodes(); i++) {
1124 			if (nest_imc_refc[i].id == nid) {
1125 				per_cpu(local_nest_imc_refc, cpu) = &nest_imc_refc[i];
1126 				break;
1127 			}
1128 		}
1129 	}
1130 	return 0;
1131 }
1132 
1133 static void cleanup_all_core_imc_memory(void)
1134 {
1135 	int i, nr_cores = DIV_ROUND_UP(num_present_cpus(), threads_per_core);
1136 	struct imc_mem_info *ptr = core_imc_pmu->mem_info;
1137 	int size = core_imc_pmu->counter_mem_size;
1138 
1139 	/* mem_info will never be NULL */
1140 	for (i = 0; i < nr_cores; i++) {
1141 		if (ptr[i].vbase)
1142 			free_pages((u64)ptr->vbase, get_order(size));
1143 	}
1144 
1145 	kfree(ptr);
1146 	kfree(core_imc_refc);
1147 }
1148 
1149 static void thread_imc_ldbar_disable(void *dummy)
1150 {
1151 	/*
1152 	 * By Zeroing LDBAR, we disable thread-imc
1153 	 * updates.
1154 	 */
1155 	mtspr(SPRN_LDBAR, 0);
1156 }
1157 
1158 void thread_imc_disable(void)
1159 {
1160 	on_each_cpu(thread_imc_ldbar_disable, NULL, 1);
1161 }
1162 
1163 static void cleanup_all_thread_imc_memory(void)
1164 {
1165 	int i, order = get_order(thread_imc_mem_size);
1166 
1167 	for_each_online_cpu(i) {
1168 		if (per_cpu(thread_imc_mem, i))
1169 			free_pages((u64)per_cpu(thread_imc_mem, i), order);
1170 
1171 	}
1172 }
1173 
1174 /*
1175  * Common function to unregister cpu hotplug callback and
1176  * free the memory.
1177  * TODO: Need to handle pmu unregistering, which will be
1178  * done in followup series.
1179  */
1180 static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
1181 {
1182 	if (pmu_ptr->domain == IMC_DOMAIN_NEST) {
1183 		mutex_lock(&nest_init_lock);
1184 		if (nest_pmus == 1) {
1185 			cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_NEST_IMC_ONLINE);
1186 			kfree(nest_imc_refc);
1187 			kfree(per_nest_pmu_arr);
1188 		}
1189 
1190 		if (nest_pmus > 0)
1191 			nest_pmus--;
1192 		mutex_unlock(&nest_init_lock);
1193 	}
1194 
1195 	/* Free core_imc memory */
1196 	if (pmu_ptr->domain == IMC_DOMAIN_CORE) {
1197 		cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_CORE_IMC_ONLINE);
1198 		cleanup_all_core_imc_memory();
1199 	}
1200 
1201 	/* Free thread_imc memory */
1202 	if (pmu_ptr->domain == IMC_DOMAIN_THREAD) {
1203 		cpuhp_remove_state(CPUHP_AP_PERF_POWERPC_THREAD_IMC_ONLINE);
1204 		cleanup_all_thread_imc_memory();
1205 	}
1206 
1207 	/* Only free the attr_groups which are dynamically allocated  */
1208 	if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
1209 		kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
1210 	kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
1211 	kfree(pmu_ptr);
1212 	return;
1213 }
1214 
1215 
1216 /*
1217  * imc_mem_init : Function to support memory allocation for core imc.
1218  */
1219 static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,
1220 								int pmu_index)
1221 {
1222 	const char *s;
1223 	int nr_cores, cpu, res;
1224 
1225 	if (of_property_read_string(parent, "name", &s))
1226 		return -ENODEV;
1227 
1228 	switch (pmu_ptr->domain) {
1229 	case IMC_DOMAIN_NEST:
1230 		/* Update the pmu name */
1231 		pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s_imc", "nest_", s);
1232 		if (!pmu_ptr->pmu.name)
1233 			return -ENOMEM;
1234 
1235 		/* Needed for hotplug/migration */
1236 		if (!per_nest_pmu_arr) {
1237 			per_nest_pmu_arr = kcalloc(get_max_nest_dev() + 1,
1238 						sizeof(struct imc_pmu *),
1239 						GFP_KERNEL);
1240 			if (!per_nest_pmu_arr)
1241 				return -ENOMEM;
1242 		}
1243 		per_nest_pmu_arr[pmu_index] = pmu_ptr;
1244 		break;
1245 	case IMC_DOMAIN_CORE:
1246 		/* Update the pmu name */
1247 		pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
1248 		if (!pmu_ptr->pmu.name)
1249 			return -ENOMEM;
1250 
1251 		nr_cores = DIV_ROUND_UP(num_present_cpus(), threads_per_core);
1252 		pmu_ptr->mem_info = kcalloc(nr_cores, sizeof(struct imc_mem_info),
1253 								GFP_KERNEL);
1254 
1255 		if (!pmu_ptr->mem_info)
1256 			return -ENOMEM;
1257 
1258 		core_imc_refc = kcalloc(nr_cores, sizeof(struct imc_pmu_ref),
1259 								GFP_KERNEL);
1260 
1261 		if (!core_imc_refc)
1262 			return -ENOMEM;
1263 
1264 		core_imc_pmu = pmu_ptr;
1265 		break;
1266 	case IMC_DOMAIN_THREAD:
1267 		/* Update the pmu name */
1268 		pmu_ptr->pmu.name = kasprintf(GFP_KERNEL, "%s%s", s, "_imc");
1269 		if (!pmu_ptr->pmu.name)
1270 			return -ENOMEM;
1271 
1272 		thread_imc_mem_size = pmu_ptr->counter_mem_size;
1273 		for_each_online_cpu(cpu) {
1274 			res = thread_imc_mem_alloc(cpu, pmu_ptr->counter_mem_size);
1275 			if (res)
1276 				return res;
1277 		}
1278 
1279 		thread_imc_pmu = pmu_ptr;
1280 		break;
1281 	default:
1282 		return -EINVAL;
1283 	}
1284 
1285 	return 0;
1286 }
1287 
1288 /*
1289  * init_imc_pmu : Setup and register the IMC pmu device.
1290  *
1291  * @parent:	Device tree unit node
1292  * @pmu_ptr:	memory allocated for this pmu
1293  * @pmu_idx:	Count of nest pmc registered
1294  *
1295  * init_imc_pmu() setup pmu cpumask and registers for a cpu hotplug callback.
1296  * Handles failure cases and accordingly frees memory.
1297  */
1298 int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_idx)
1299 {
1300 	int ret;
1301 
1302 	ret = imc_mem_init(pmu_ptr, parent, pmu_idx);
1303 	if (ret)
1304 		goto err_free;
1305 
1306 	switch (pmu_ptr->domain) {
1307 	case IMC_DOMAIN_NEST:
1308 		/*
1309 		* Nest imc pmu need only one cpu per chip, we initialize the
1310 		* cpumask for the first nest imc pmu and use the same for the
1311 		* rest. To handle the cpuhotplug callback unregister, we track
1312 		* the number of nest pmus in "nest_pmus".
1313 		*/
1314 		mutex_lock(&nest_init_lock);
1315 		if (nest_pmus == 0) {
1316 			ret = init_nest_pmu_ref();
1317 			if (ret) {
1318 				mutex_unlock(&nest_init_lock);
1319 				goto err_free;
1320 			}
1321 			/* Register for cpu hotplug notification. */
1322 			ret = nest_pmu_cpumask_init();
1323 			if (ret) {
1324 				mutex_unlock(&nest_init_lock);
1325 				kfree(nest_imc_refc);
1326 				kfree(per_nest_pmu_arr);
1327 				goto err_free;
1328 			}
1329 		}
1330 		nest_pmus++;
1331 		mutex_unlock(&nest_init_lock);
1332 		break;
1333 	case IMC_DOMAIN_CORE:
1334 		ret = core_imc_pmu_cpumask_init();
1335 		if (ret) {
1336 			cleanup_all_core_imc_memory();
1337 			return ret;
1338 		}
1339 
1340 		break;
1341 	case IMC_DOMAIN_THREAD:
1342 		ret = thread_imc_cpu_init();
1343 		if (ret) {
1344 			cleanup_all_thread_imc_memory();
1345 			return ret;
1346 		}
1347 
1348 		break;
1349 	default:
1350 		return  -1;	/* Unknown domain */
1351 	}
1352 
1353 	ret = update_events_in_group(parent, pmu_ptr);
1354 	if (ret)
1355 		goto err_free;
1356 
1357 	ret = update_pmu_ops(pmu_ptr);
1358 	if (ret)
1359 		goto err_free;
1360 
1361 	ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
1362 	if (ret)
1363 		goto err_free;
1364 
1365 	pr_info("%s performance monitor hardware support registered\n",
1366 							pmu_ptr->pmu.name);
1367 
1368 	return 0;
1369 
1370 err_free:
1371 	imc_common_cpuhp_mem_free(pmu_ptr);
1372 	return ret;
1373 }
1374