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