xref: /linux/arch/x86/events/amd/uncore.c (revision 4cbdd58dfa7aaad679bf62f81cdbc96a6cb7c6ae)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Advanced Micro Devices, Inc.
4  *
5  * Author: Jacob Shin <jacob.shin@amd.com>
6  */
7 
8 #include <linux/perf_event.h>
9 #include <linux/percpu.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/cpu.h>
14 #include <linux/cpumask.h>
15 #include <linux/cpufeature.h>
16 #include <linux/smp.h>
17 
18 #include <asm/perf_event.h>
19 #include <asm/cpuid/api.h>
20 #include <asm/msr.h>
21 
22 #define NUM_COUNTERS_NB		4
23 #define NUM_COUNTERS_L2		4
24 #define NUM_COUNTERS_L3		6
25 #define NUM_COUNTERS_MAX	64
26 
27 #define RDPMC_BASE_NB		6
28 #define RDPMC_BASE_LLC		10
29 
30 #define COUNTER_SHIFT		16
31 #define UNCORE_NAME_LEN		16
32 #define UNCORE_GROUP_MAX	256
33 
34 #undef pr_fmt
35 #define pr_fmt(fmt)	"amd_uncore: " fmt
36 
37 static int pmu_version;
38 
39 struct amd_uncore_ctx {
40 	int refcnt;
41 	int cpu;
42 	unsigned long active_mask[BITS_TO_LONGS(NUM_COUNTERS_MAX)];
43 	int nr_active;
44 	struct hrtimer hrtimer;
45 	u64 hrtimer_duration;
46 	struct perf_event *events[];
47 };
48 
49 struct amd_uncore_pmu {
50 	char name[UNCORE_NAME_LEN];
51 	int num_counters;
52 	int rdpmc_base;
53 	u32 msr_base;
54 	int group;
55 	cpumask_t active_mask;
56 	struct pmu pmu;
57 	struct amd_uncore_ctx * __percpu *ctx;
58 };
59 
60 enum {
61 	UNCORE_TYPE_DF,
62 	UNCORE_TYPE_L3,
63 	UNCORE_TYPE_UMC,
64 
65 	UNCORE_TYPE_MAX
66 };
67 
68 union amd_uncore_info {
69 	struct {
70 		u64	aux_data:32;	/* auxiliary data */
71 		u64	num_pmcs:8;	/* number of counters */
72 		u64	gid:8;		/* group id */
73 		u64	cid:8;		/* context id */
74 	} split;
75 	u64		full;
76 };
77 
78 struct amd_uncore {
79 	union amd_uncore_info  __percpu *info;
80 	struct amd_uncore_pmu *pmus;
81 	unsigned int num_pmus;
82 	bool init_done;
83 	void (*scan)(struct amd_uncore *uncore, unsigned int cpu);
84 	int  (*init)(struct amd_uncore *uncore, unsigned int cpu);
85 	void (*move)(struct amd_uncore *uncore, unsigned int cpu);
86 	void (*free)(struct amd_uncore *uncore, unsigned int cpu);
87 };
88 
89 static struct amd_uncore uncores[UNCORE_TYPE_MAX];
90 
91 /* Interval for hrtimer, defaults to 60000 milliseconds */
92 static unsigned int update_interval = 60 * MSEC_PER_SEC;
93 module_param(update_interval, uint, 0444);
94 
event_to_amd_uncore_pmu(struct perf_event * event)95 static struct amd_uncore_pmu *event_to_amd_uncore_pmu(struct perf_event *event)
96 {
97 	return container_of(event->pmu, struct amd_uncore_pmu, pmu);
98 }
99 
amd_uncore_hrtimer(struct hrtimer * hrtimer)100 static enum hrtimer_restart amd_uncore_hrtimer(struct hrtimer *hrtimer)
101 {
102 	struct amd_uncore_ctx *ctx;
103 	struct perf_event *event;
104 	int bit;
105 
106 	ctx = container_of(hrtimer, struct amd_uncore_ctx, hrtimer);
107 
108 	if (!ctx->nr_active || ctx->cpu != smp_processor_id())
109 		return HRTIMER_NORESTART;
110 
111 	for_each_set_bit(bit, ctx->active_mask, NUM_COUNTERS_MAX) {
112 		event = ctx->events[bit];
113 		event->pmu->read(event);
114 	}
115 
116 	hrtimer_forward_now(hrtimer, ns_to_ktime(ctx->hrtimer_duration));
117 	return HRTIMER_RESTART;
118 }
119 
amd_uncore_start_hrtimer(struct amd_uncore_ctx * ctx)120 static void amd_uncore_start_hrtimer(struct amd_uncore_ctx *ctx)
121 {
122 	hrtimer_start(&ctx->hrtimer, ns_to_ktime(ctx->hrtimer_duration),
123 		      HRTIMER_MODE_REL_PINNED_HARD);
124 }
125 
amd_uncore_cancel_hrtimer(struct amd_uncore_ctx * ctx)126 static void amd_uncore_cancel_hrtimer(struct amd_uncore_ctx *ctx)
127 {
128 	hrtimer_cancel(&ctx->hrtimer);
129 }
130 
amd_uncore_init_hrtimer(struct amd_uncore_ctx * ctx)131 static void amd_uncore_init_hrtimer(struct amd_uncore_ctx *ctx)
132 {
133 	hrtimer_setup(&ctx->hrtimer, amd_uncore_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
134 }
135 
amd_uncore_read(struct perf_event * event)136 static void amd_uncore_read(struct perf_event *event)
137 {
138 	struct hw_perf_event *hwc = &event->hw;
139 	u64 prev, new;
140 	s64 delta;
141 
142 	/*
143 	 * since we do not enable counter overflow interrupts,
144 	 * we do not have to worry about prev_count changing on us
145 	 */
146 
147 	prev = local64_read(&hwc->prev_count);
148 
149 	/*
150 	 * Some uncore PMUs do not have RDPMC assignments. In such cases,
151 	 * read counts directly from the corresponding PERF_CTR.
152 	 */
153 	if (hwc->event_base_rdpmc < 0)
154 		rdmsrq(hwc->event_base, new);
155 	else
156 		new = rdpmc(hwc->event_base_rdpmc);
157 
158 	local64_set(&hwc->prev_count, new);
159 	delta = (new << COUNTER_SHIFT) - (prev << COUNTER_SHIFT);
160 	delta >>= COUNTER_SHIFT;
161 	local64_add(delta, &event->count);
162 }
163 
amd_uncore_start(struct perf_event * event,int flags)164 static void amd_uncore_start(struct perf_event *event, int flags)
165 {
166 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
167 	struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
168 	struct hw_perf_event *hwc = &event->hw;
169 
170 	if (!ctx->nr_active++)
171 		amd_uncore_start_hrtimer(ctx);
172 
173 	if (flags & PERF_EF_RELOAD)
174 		wrmsrq(hwc->event_base, (u64)local64_read(&hwc->prev_count));
175 
176 	hwc->state = 0;
177 	__set_bit(hwc->idx, ctx->active_mask);
178 	wrmsrq(hwc->config_base, (hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE));
179 	perf_event_update_userpage(event);
180 }
181 
amd_uncore_stop(struct perf_event * event,int flags)182 static void amd_uncore_stop(struct perf_event *event, int flags)
183 {
184 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
185 	struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
186 	struct hw_perf_event *hwc = &event->hw;
187 
188 	wrmsrq(hwc->config_base, hwc->config);
189 	hwc->state |= PERF_HES_STOPPED;
190 
191 	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
192 		event->pmu->read(event);
193 		hwc->state |= PERF_HES_UPTODATE;
194 	}
195 
196 	if (!--ctx->nr_active)
197 		amd_uncore_cancel_hrtimer(ctx);
198 
199 	__clear_bit(hwc->idx, ctx->active_mask);
200 }
201 
amd_uncore_add(struct perf_event * event,int flags)202 static int amd_uncore_add(struct perf_event *event, int flags)
203 {
204 	int i;
205 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
206 	struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
207 	struct hw_perf_event *hwc = &event->hw;
208 
209 	/*
210 	 * Perf serializes ->add() and ->del() for an event. A successful
211 	 * ->add() records the claimed slot in hwc->idx before returning, and
212 	 * ->del() clears that slot before resetting hwc->idx. Therefore, an
213 	 * existing assignment must be at hwc->idx.
214 	 */
215 	if (hwc->idx != -1 && ctx->events[hwc->idx] == event)
216 		goto out;
217 
218 	/* if not, take the first available counter */
219 	hwc->idx = -1;
220 	for (i = 0; i < pmu->num_counters; i++) {
221 		struct perf_event *tmp = NULL;
222 
223 		if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
224 			hwc->idx = i;
225 			break;
226 		}
227 	}
228 
229 out:
230 	if (hwc->idx == -1)
231 		return -EBUSY;
232 
233 	hwc->config_base = pmu->msr_base + (2 * hwc->idx);
234 	hwc->event_base = pmu->msr_base + 1 + (2 * hwc->idx);
235 	hwc->event_base_rdpmc = pmu->rdpmc_base + hwc->idx;
236 	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
237 
238 	if (pmu->rdpmc_base < 0)
239 		hwc->event_base_rdpmc = -1;
240 
241 	if (flags & PERF_EF_START)
242 		event->pmu->start(event, PERF_EF_RELOAD);
243 
244 	return 0;
245 }
246 
amd_uncore_del(struct perf_event * event,int flags)247 static void amd_uncore_del(struct perf_event *event, int flags)
248 {
249 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
250 	struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
251 	struct hw_perf_event *hwc = &event->hw;
252 	struct perf_event *old = event;
253 
254 	event->pmu->stop(event, PERF_EF_UPDATE);
255 
256 	/* ->del() follows a successful ->add(), so hwc->idx owns this slot. */
257 	WARN_ON_ONCE(!try_cmpxchg(&ctx->events[hwc->idx], &old, NULL));
258 
259 	hwc->idx = -1;
260 }
261 
amd_uncore_group_valid(struct perf_event * event)262 static bool amd_uncore_group_valid(struct perf_event *event)
263 {
264 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
265 	struct perf_event *leader = event->group_leader;
266 	struct perf_event *sibling;
267 	int counters = 0;
268 
269 	if (leader->pmu == event->pmu)
270 		counters++;
271 
272 	for_each_sibling_event(sibling, leader) {
273 		if (sibling->pmu == event->pmu &&
274 		    sibling->state > PERF_EVENT_STATE_OFF)
275 			counters++;
276 	}
277 
278 	/*
279 	 * When pmu->event_init() is called, the event is yet to be linked to
280 	 * its leader's sibling list, so it is counted separately
281 	 */
282 	return (counters + 1) <= pmu->num_counters;
283 }
284 
amd_uncore_event_init(struct perf_event * event)285 static int amd_uncore_event_init(struct perf_event *event)
286 {
287 	struct amd_uncore_pmu *pmu;
288 	struct amd_uncore_ctx *ctx;
289 	struct hw_perf_event *hwc = &event->hw;
290 
291 	if (event->attr.type != event->pmu->type)
292 		return -ENOENT;
293 
294 	if (event->cpu < 0)
295 		return -EINVAL;
296 
297 	pmu = event_to_amd_uncore_pmu(event);
298 	ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
299 	if (!ctx)
300 		return -ENODEV;
301 
302 	/*
303 	 * Ensure that all events in a group can be scheduled together so that
304 	 * a failure can be reported at perf_event_open() time rather than
305 	 * silently at pmu->add() time when no free counter is found
306 	 */
307 	if (event->group_leader != event && !amd_uncore_group_valid(event))
308 		return -EINVAL;
309 
310 	/*
311 	 * NB and Last level cache counters (MSRs) are shared across all cores
312 	 * that share the same NB / Last level cache.  On family 16h and below,
313 	 * Interrupts can be directed to a single target core, however, event
314 	 * counts generated by processes running on other cores cannot be masked
315 	 * out. So we do not support sampling and per-thread events via
316 	 * CAP_NO_INTERRUPT, and we do not enable counter overflow interrupts:
317 	 */
318 	hwc->config = event->attr.config;
319 	hwc->idx = -1;
320 
321 	/*
322 	 * since request can come in to any of the shared cores, we will remap
323 	 * to a single common cpu.
324 	 */
325 	event->cpu = ctx->cpu;
326 
327 	return 0;
328 }
329 
330 static umode_t
amd_f17h_uncore_is_visible(struct kobject * kobj,struct attribute * attr,int i)331 amd_f17h_uncore_is_visible(struct kobject *kobj, struct attribute *attr, int i)
332 {
333 	return boot_cpu_data.x86 >= 0x17 && boot_cpu_data.x86 < 0x19 ?
334 	       attr->mode : 0;
335 }
336 
337 static umode_t
amd_f19h_uncore_is_visible(struct kobject * kobj,struct attribute * attr,int i)338 amd_f19h_uncore_is_visible(struct kobject *kobj, struct attribute *attr, int i)
339 {
340 	return boot_cpu_data.x86 >= 0x19 ? attr->mode : 0;
341 }
342 
amd_uncore_attr_show_cpumask(struct device * dev,struct device_attribute * attr,char * buf)343 static ssize_t amd_uncore_attr_show_cpumask(struct device *dev,
344 					    struct device_attribute *attr,
345 					    char *buf)
346 {
347 	struct pmu *ptr = dev_get_drvdata(dev);
348 	struct amd_uncore_pmu *pmu = container_of(ptr, struct amd_uncore_pmu, pmu);
349 
350 	return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(&pmu->active_mask));
351 }
352 static DEVICE_ATTR(cpumask, S_IRUGO, amd_uncore_attr_show_cpumask, NULL);
353 
354 static struct attribute *amd_uncore_attrs[] = {
355 	&dev_attr_cpumask.attr,
356 	NULL,
357 };
358 
359 static struct attribute_group amd_uncore_attr_group = {
360 	.attrs = amd_uncore_attrs,
361 };
362 
363 #define DEFINE_UNCORE_FORMAT_ATTR(_var, _name, _format)			\
364 static ssize_t __uncore_##_var##_show(struct device *dev,		\
365 				struct device_attribute *attr,		\
366 				char *page)				\
367 {									\
368 	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			\
369 	return sprintf(page, _format "\n");				\
370 }									\
371 static struct device_attribute format_attr_##_var =			\
372 	__ATTR(_name, 0444, __uncore_##_var##_show, NULL)
373 
374 DEFINE_UNCORE_FORMAT_ATTR(event12,	event,		"config:0-7,32-35");
375 DEFINE_UNCORE_FORMAT_ATTR(event14,	event,		"config:0-7,32-35,59-60"); /* F17h+ DF */
376 DEFINE_UNCORE_FORMAT_ATTR(event14v2,	event,		"config:0-7,32-37");	   /* PerfMonV2 DF */
377 DEFINE_UNCORE_FORMAT_ATTR(event8,	event,		"config:0-7");		   /* F17h+ L3, PerfMonV2 UMC */
378 DEFINE_UNCORE_FORMAT_ATTR(umask8,	umask,		"config:8-15");
379 DEFINE_UNCORE_FORMAT_ATTR(umask12,	umask,		"config:8-15,24-27");	   /* PerfMonV2 DF */
380 DEFINE_UNCORE_FORMAT_ATTR(coreid,	coreid,		"config:42-44");	   /* F19h L3 */
381 DEFINE_UNCORE_FORMAT_ATTR(slicemask,	slicemask,	"config:48-51");	   /* F17h L3 */
382 DEFINE_UNCORE_FORMAT_ATTR(threadmask8,	threadmask,	"config:56-63");	   /* F17h L3 */
383 DEFINE_UNCORE_FORMAT_ATTR(threadmask2,	threadmask,	"config:56-57");	   /* F19h L3 */
384 DEFINE_UNCORE_FORMAT_ATTR(enallslices,	enallslices,	"config:46");		   /* F19h L3 */
385 DEFINE_UNCORE_FORMAT_ATTR(enallcores,	enallcores,	"config:47");		   /* F19h L3 */
386 DEFINE_UNCORE_FORMAT_ATTR(sliceid,	sliceid,	"config:48-50");	   /* F19h L3 */
387 DEFINE_UNCORE_FORMAT_ATTR(rdwrmask,	rdwrmask,	"config:8-9");		   /* PerfMonV2 UMC */
388 
389 /* Common DF and NB attributes */
390 static struct attribute *amd_uncore_df_format_attr[] = {
391 	&format_attr_event12.attr,	/* event */
392 	&format_attr_umask8.attr,	/* umask */
393 	NULL,
394 };
395 
396 /* Common L2 and L3 attributes */
397 static struct attribute *amd_uncore_l3_format_attr[] = {
398 	&format_attr_event12.attr,	/* event */
399 	&format_attr_umask8.attr,	/* umask */
400 	NULL,				/* threadmask */
401 	NULL,
402 };
403 
404 /* Common UMC attributes */
405 static struct attribute *amd_uncore_umc_format_attr[] = {
406 	&format_attr_event8.attr,       /* event */
407 	&format_attr_rdwrmask.attr,     /* rdwrmask */
408 	NULL,
409 };
410 
411 /* F17h unique L3 attributes */
412 static struct attribute *amd_f17h_uncore_l3_format_attr[] = {
413 	&format_attr_slicemask.attr,	/* slicemask */
414 	NULL,
415 };
416 
417 /* F19h unique L3 attributes */
418 static struct attribute *amd_f19h_uncore_l3_format_attr[] = {
419 	&format_attr_coreid.attr,	/* coreid */
420 	&format_attr_enallslices.attr,	/* enallslices */
421 	&format_attr_enallcores.attr,	/* enallcores */
422 	&format_attr_sliceid.attr,	/* sliceid */
423 	NULL,
424 };
425 
426 static struct attribute_group amd_uncore_df_format_group = {
427 	.name = "format",
428 	.attrs = amd_uncore_df_format_attr,
429 };
430 
431 static struct attribute_group amd_uncore_l3_format_group = {
432 	.name = "format",
433 	.attrs = amd_uncore_l3_format_attr,
434 };
435 
436 static struct attribute_group amd_f17h_uncore_l3_format_group = {
437 	.name = "format",
438 	.attrs = amd_f17h_uncore_l3_format_attr,
439 	.is_visible = amd_f17h_uncore_is_visible,
440 };
441 
442 static struct attribute_group amd_f19h_uncore_l3_format_group = {
443 	.name = "format",
444 	.attrs = amd_f19h_uncore_l3_format_attr,
445 	.is_visible = amd_f19h_uncore_is_visible,
446 };
447 
448 static struct attribute_group amd_uncore_umc_format_group = {
449 	.name = "format",
450 	.attrs = amd_uncore_umc_format_attr,
451 };
452 
453 static const struct attribute_group *amd_uncore_df_attr_groups[] = {
454 	&amd_uncore_attr_group,
455 	&amd_uncore_df_format_group,
456 	NULL,
457 };
458 
459 static const struct attribute_group *amd_uncore_l3_attr_groups[] = {
460 	&amd_uncore_attr_group,
461 	&amd_uncore_l3_format_group,
462 	NULL,
463 };
464 
465 static const struct attribute_group *amd_uncore_l3_attr_update[] = {
466 	&amd_f17h_uncore_l3_format_group,
467 	&amd_f19h_uncore_l3_format_group,
468 	NULL,
469 };
470 
471 static const struct attribute_group *amd_uncore_umc_attr_groups[] = {
472 	&amd_uncore_attr_group,
473 	&amd_uncore_umc_format_group,
474 	NULL,
475 };
476 
477 static __always_inline
amd_uncore_ctx_cid(struct amd_uncore * uncore,unsigned int cpu)478 int amd_uncore_ctx_cid(struct amd_uncore *uncore, unsigned int cpu)
479 {
480 	union amd_uncore_info *info = per_cpu_ptr(uncore->info, cpu);
481 	return info->split.cid;
482 }
483 
484 static __always_inline
amd_uncore_ctx_gid(struct amd_uncore * uncore,unsigned int cpu)485 int amd_uncore_ctx_gid(struct amd_uncore *uncore, unsigned int cpu)
486 {
487 	union amd_uncore_info *info = per_cpu_ptr(uncore->info, cpu);
488 	return info->split.gid;
489 }
490 
491 static __always_inline
amd_uncore_ctx_num_pmcs(struct amd_uncore * uncore,unsigned int cpu)492 int amd_uncore_ctx_num_pmcs(struct amd_uncore *uncore, unsigned int cpu)
493 {
494 	union amd_uncore_info *info = per_cpu_ptr(uncore->info, cpu);
495 	return info->split.num_pmcs;
496 }
497 
amd_uncore_ctx_free(struct amd_uncore * uncore,unsigned int cpu)498 static void amd_uncore_ctx_free(struct amd_uncore *uncore, unsigned int cpu)
499 {
500 	struct amd_uncore_pmu *pmu;
501 	struct amd_uncore_ctx *ctx;
502 	int i;
503 
504 	if (!uncore->init_done)
505 		return;
506 
507 	for (i = 0; i < uncore->num_pmus; i++) {
508 		pmu = &uncore->pmus[i];
509 		ctx = *per_cpu_ptr(pmu->ctx, cpu);
510 		if (!ctx)
511 			continue;
512 
513 		if (cpu == ctx->cpu)
514 			cpumask_clear_cpu(cpu, &pmu->active_mask);
515 
516 		if (!--ctx->refcnt)
517 			kfree(ctx);
518 
519 		*per_cpu_ptr(pmu->ctx, cpu) = NULL;
520 	}
521 }
522 
amd_uncore_ctx_init(struct amd_uncore * uncore,unsigned int cpu)523 static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
524 {
525 	struct amd_uncore_ctx *curr, *prev;
526 	struct amd_uncore_pmu *pmu;
527 	int node, cid, gid, i, j;
528 
529 	if (!uncore->init_done || !uncore->num_pmus)
530 		return 0;
531 
532 	cid = amd_uncore_ctx_cid(uncore, cpu);
533 	gid = amd_uncore_ctx_gid(uncore, cpu);
534 
535 	for (i = 0; i < uncore->num_pmus; i++) {
536 		pmu = &uncore->pmus[i];
537 		*per_cpu_ptr(pmu->ctx, cpu) = NULL;
538 		curr = NULL;
539 
540 		/* Check for group exclusivity */
541 		if (gid != pmu->group)
542 			continue;
543 
544 		/* Find a sibling context */
545 		for_each_online_cpu(j) {
546 			if (cpu == j)
547 				continue;
548 
549 			prev = *per_cpu_ptr(pmu->ctx, j);
550 			if (!prev)
551 				continue;
552 
553 			if (cid == amd_uncore_ctx_cid(uncore, j)) {
554 				curr = prev;
555 				break;
556 			}
557 		}
558 
559 		/* Allocate context if sibling does not exist */
560 		if (!curr) {
561 			node = cpu_to_node(cpu);
562 			curr = kzalloc_node(struct_size(curr, events, pmu->num_counters), GFP_KERNEL, node);
563 			if (!curr)
564 				goto fail;
565 
566 			curr->cpu = cpu;
567 
568 			amd_uncore_init_hrtimer(curr);
569 			curr->hrtimer_duration = (u64)update_interval * NSEC_PER_MSEC;
570 
571 			cpumask_set_cpu(cpu, &pmu->active_mask);
572 		}
573 
574 		curr->refcnt++;
575 		*per_cpu_ptr(pmu->ctx, cpu) = curr;
576 	}
577 
578 	return 0;
579 
580 fail:
581 	amd_uncore_ctx_free(uncore, cpu);
582 
583 	return -ENOMEM;
584 }
585 
amd_uncore_ctx_move(struct amd_uncore * uncore,unsigned int cpu)586 static void amd_uncore_ctx_move(struct amd_uncore *uncore, unsigned int cpu)
587 {
588 	struct amd_uncore_ctx *curr, *next;
589 	struct amd_uncore_pmu *pmu;
590 	int i, j;
591 
592 	if (!uncore->init_done)
593 		return;
594 
595 	for (i = 0; i < uncore->num_pmus; i++) {
596 		pmu = &uncore->pmus[i];
597 		curr = *per_cpu_ptr(pmu->ctx, cpu);
598 		if (!curr)
599 			continue;
600 
601 		/* Migrate to a shared sibling if possible */
602 		for_each_online_cpu(j) {
603 			next = *per_cpu_ptr(pmu->ctx, j);
604 			if (!next || cpu == j)
605 				continue;
606 
607 			if (curr == next) {
608 				perf_pmu_migrate_context(&pmu->pmu, cpu, j);
609 				cpumask_clear_cpu(cpu, &pmu->active_mask);
610 				cpumask_set_cpu(j, &pmu->active_mask);
611 				next->cpu = j;
612 				break;
613 			}
614 		}
615 	}
616 }
617 
amd_uncore_cpu_starting(unsigned int cpu)618 static int amd_uncore_cpu_starting(unsigned int cpu)
619 {
620 	struct amd_uncore *uncore;
621 	int i;
622 
623 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
624 		uncore = &uncores[i];
625 		uncore->scan(uncore, cpu);
626 	}
627 
628 	return 0;
629 }
630 
amd_uncore_cpu_online(unsigned int cpu)631 static int amd_uncore_cpu_online(unsigned int cpu)
632 {
633 	struct amd_uncore *uncore;
634 	int i;
635 
636 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
637 		uncore = &uncores[i];
638 		if (uncore->init(uncore, cpu))
639 			break;
640 	}
641 
642 	return 0;
643 }
644 
amd_uncore_cpu_down_prepare(unsigned int cpu)645 static int amd_uncore_cpu_down_prepare(unsigned int cpu)
646 {
647 	struct amd_uncore *uncore;
648 	int i;
649 
650 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
651 		uncore = &uncores[i];
652 		uncore->move(uncore, cpu);
653 	}
654 
655 	return 0;
656 }
657 
amd_uncore_cpu_dead(unsigned int cpu)658 static int amd_uncore_cpu_dead(unsigned int cpu)
659 {
660 	struct amd_uncore *uncore;
661 	int i;
662 
663 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
664 		uncore = &uncores[i];
665 		uncore->free(uncore, cpu);
666 	}
667 
668 	return 0;
669 }
670 
amd_uncore_df_event_init(struct perf_event * event)671 static int amd_uncore_df_event_init(struct perf_event *event)
672 {
673 	struct hw_perf_event *hwc = &event->hw;
674 	int ret = amd_uncore_event_init(event);
675 
676 	hwc->config = event->attr.config &
677 		      (pmu_version >= 2 ? AMD64_PERFMON_V2_RAW_EVENT_MASK_NB :
678 					  AMD64_RAW_EVENT_MASK_NB);
679 
680 	return ret;
681 }
682 
amd_uncore_df_add(struct perf_event * event,int flags)683 static int amd_uncore_df_add(struct perf_event *event, int flags)
684 {
685 	int ret = amd_uncore_add(event, flags & ~PERF_EF_START);
686 	struct hw_perf_event *hwc = &event->hw;
687 
688 	if (ret)
689 		return ret;
690 
691 	/*
692 	 * The first four DF counters are accessible via RDPMC index 6 to 9
693 	 * followed by the L3 counters from index 10 to 15. For processors
694 	 * with more than four DF counters, the DF RDPMC assignments become
695 	 * discontiguous as the additional counters are accessible starting
696 	 * from index 16.
697 	 */
698 	if (hwc->idx >= NUM_COUNTERS_NB)
699 		hwc->event_base_rdpmc += NUM_COUNTERS_L3;
700 
701 	/* Delayed start after rdpmc base update */
702 	if (flags & PERF_EF_START)
703 		amd_uncore_start(event, PERF_EF_RELOAD);
704 
705 	return 0;
706 }
707 
708 static
amd_uncore_df_ctx_scan(struct amd_uncore * uncore,unsigned int cpu)709 void amd_uncore_df_ctx_scan(struct amd_uncore *uncore, unsigned int cpu)
710 {
711 	union cpuid_0x80000022_ebx ebx;
712 	union amd_uncore_info info;
713 
714 	if (!boot_cpu_has(X86_FEATURE_PERFCTR_NB))
715 		return;
716 
717 	info.split.aux_data = 0;
718 	info.split.num_pmcs = NUM_COUNTERS_NB;
719 	info.split.gid = 0;
720 	info.split.cid = topology_amd_node_id(cpu);
721 
722 	if (pmu_version >= 2) {
723 		ebx.full = cpuid_ebx(EXT_PERFMON_DEBUG_FEATURES);
724 		info.split.num_pmcs = ebx.split.num_df_pmc;
725 	}
726 
727 	*per_cpu_ptr(uncore->info, cpu) = info;
728 }
729 
730 static
amd_uncore_df_ctx_init(struct amd_uncore * uncore,unsigned int cpu)731 int amd_uncore_df_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
732 {
733 	struct attribute **df_attr = amd_uncore_df_format_attr;
734 	struct amd_uncore_pmu *pmu;
735 	int num_counters;
736 
737 	/* Run just once */
738 	if (uncore->init_done)
739 		return amd_uncore_ctx_init(uncore, cpu);
740 
741 	num_counters = amd_uncore_ctx_num_pmcs(uncore, cpu);
742 	if (!num_counters)
743 		goto done;
744 
745 	/* No grouping, single instance for a system */
746 	uncore->pmus = kzalloc_obj(*uncore->pmus);
747 	if (!uncore->pmus)
748 		goto done;
749 
750 	/*
751 	 * For Family 17h and above, the Northbridge counters are repurposed
752 	 * as Data Fabric counters. The PMUs are exported based on family as
753 	 * either NB or DF.
754 	 */
755 	pmu = &uncore->pmus[0];
756 	strscpy(pmu->name, boot_cpu_data.x86 >= 0x17 ? "amd_df" : "amd_nb",
757 		sizeof(pmu->name));
758 	pmu->num_counters = num_counters;
759 	pmu->msr_base = MSR_F15H_NB_PERF_CTL;
760 	pmu->rdpmc_base = RDPMC_BASE_NB;
761 	pmu->group = amd_uncore_ctx_gid(uncore, cpu);
762 
763 	if (pmu_version >= 2) {
764 		*df_attr++ = &format_attr_event14v2.attr;
765 		*df_attr++ = &format_attr_umask12.attr;
766 	} else if (boot_cpu_data.x86 >= 0x17) {
767 		*df_attr = &format_attr_event14.attr;
768 	}
769 
770 	pmu->ctx = alloc_percpu(struct amd_uncore_ctx *);
771 	if (!pmu->ctx)
772 		goto done;
773 
774 	pmu->pmu = (struct pmu) {
775 		.task_ctx_nr	= perf_invalid_context,
776 		.attr_groups	= amd_uncore_df_attr_groups,
777 		.name		= pmu->name,
778 		.event_init	= amd_uncore_df_event_init,
779 		.add		= amd_uncore_df_add,
780 		.del		= amd_uncore_del,
781 		.start		= amd_uncore_start,
782 		.stop		= amd_uncore_stop,
783 		.read		= amd_uncore_read,
784 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
785 		.module		= THIS_MODULE,
786 	};
787 
788 	if (perf_pmu_register(&pmu->pmu, pmu->pmu.name, -1)) {
789 		free_percpu(pmu->ctx);
790 		pmu->ctx = NULL;
791 		goto done;
792 	}
793 
794 	pr_info("%d %s%s counters detected\n", pmu->num_counters,
795 		boot_cpu_data.x86_vendor == X86_VENDOR_HYGON ?  "HYGON " : "",
796 		pmu->pmu.name);
797 
798 	uncore->num_pmus = 1;
799 
800 done:
801 	uncore->init_done = true;
802 
803 	return amd_uncore_ctx_init(uncore, cpu);
804 }
805 
amd_uncore_l3_event_init(struct perf_event * event)806 static int amd_uncore_l3_event_init(struct perf_event *event)
807 {
808 	int ret = amd_uncore_event_init(event);
809 	struct hw_perf_event *hwc = &event->hw;
810 	u64 config = event->attr.config;
811 	u64 mask;
812 
813 	hwc->config = config & AMD64_RAW_EVENT_MASK_NB;
814 
815 	/*
816 	 * SliceMask and ThreadMask need to be set for certain L3 events.
817 	 * For other events, the two fields do not affect the count.
818 	 */
819 	if (ret || boot_cpu_data.x86 < 0x17)
820 		return ret;
821 
822 	mask = config & (AMD64_L3_F19H_THREAD_MASK | AMD64_L3_SLICEID_MASK |
823 			 AMD64_L3_EN_ALL_CORES | AMD64_L3_EN_ALL_SLICES |
824 			 AMD64_L3_COREID_MASK);
825 
826 	if (boot_cpu_data.x86 <= 0x18)
827 		mask = ((config & AMD64_L3_SLICE_MASK) ? : AMD64_L3_SLICE_MASK) |
828 		       ((config & AMD64_L3_THREAD_MASK) ? : AMD64_L3_THREAD_MASK);
829 
830 	/*
831 	 * If the user doesn't specify a ThreadMask, they're not trying to
832 	 * count core 0, so we enable all cores & threads.
833 	 * We'll also assume that they want to count slice 0 if they specify
834 	 * a ThreadMask and leave SliceId and EnAllSlices unpopulated.
835 	 */
836 	else if (!(config & AMD64_L3_F19H_THREAD_MASK))
837 		mask = AMD64_L3_F19H_THREAD_MASK | AMD64_L3_EN_ALL_SLICES |
838 		       AMD64_L3_EN_ALL_CORES;
839 
840 	hwc->config |= mask;
841 
842 	return 0;
843 }
844 
845 static
amd_uncore_l3_ctx_scan(struct amd_uncore * uncore,unsigned int cpu)846 void amd_uncore_l3_ctx_scan(struct amd_uncore *uncore, unsigned int cpu)
847 {
848 	union amd_uncore_info info;
849 
850 	if (!boot_cpu_has(X86_FEATURE_PERFCTR_LLC))
851 		return;
852 
853 	info.split.aux_data = 0;
854 	info.split.num_pmcs = NUM_COUNTERS_L2;
855 	info.split.gid = 0;
856 	info.split.cid = per_cpu_llc_id(cpu);
857 
858 	if (boot_cpu_data.x86 >= 0x17)
859 		info.split.num_pmcs = NUM_COUNTERS_L3;
860 
861 	*per_cpu_ptr(uncore->info, cpu) = info;
862 }
863 
864 static
amd_uncore_l3_ctx_init(struct amd_uncore * uncore,unsigned int cpu)865 int amd_uncore_l3_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
866 {
867 	struct attribute **l3_attr = amd_uncore_l3_format_attr;
868 	struct amd_uncore_pmu *pmu;
869 	int num_counters;
870 
871 	/* Run just once */
872 	if (uncore->init_done)
873 		return amd_uncore_ctx_init(uncore, cpu);
874 
875 	num_counters = amd_uncore_ctx_num_pmcs(uncore, cpu);
876 	if (!num_counters)
877 		goto done;
878 
879 	/* No grouping, single instance for a system */
880 	uncore->pmus = kzalloc_obj(*uncore->pmus);
881 	if (!uncore->pmus)
882 		goto done;
883 
884 	/*
885 	 * For Family 17h and above, L3 cache counters are available instead
886 	 * of L2 cache counters. The PMUs are exported based on family as
887 	 * either L2 or L3.
888 	 */
889 	pmu = &uncore->pmus[0];
890 	strscpy(pmu->name, boot_cpu_data.x86 >= 0x17 ? "amd_l3" : "amd_l2",
891 		sizeof(pmu->name));
892 	pmu->num_counters = num_counters;
893 	pmu->msr_base = MSR_F16H_L2I_PERF_CTL;
894 	pmu->rdpmc_base = RDPMC_BASE_LLC;
895 	pmu->group = amd_uncore_ctx_gid(uncore, cpu);
896 
897 	if (boot_cpu_data.x86 >= 0x17) {
898 		*l3_attr++ = &format_attr_event8.attr;
899 		*l3_attr++ = &format_attr_umask8.attr;
900 		*l3_attr++ = boot_cpu_data.x86 >= 0x19 ?
901 			     &format_attr_threadmask2.attr :
902 			     &format_attr_threadmask8.attr;
903 	}
904 
905 	pmu->ctx = alloc_percpu(struct amd_uncore_ctx *);
906 	if (!pmu->ctx)
907 		goto done;
908 
909 	pmu->pmu = (struct pmu) {
910 		.task_ctx_nr	= perf_invalid_context,
911 		.attr_groups	= amd_uncore_l3_attr_groups,
912 		.attr_update	= amd_uncore_l3_attr_update,
913 		.name		= pmu->name,
914 		.event_init	= amd_uncore_l3_event_init,
915 		.add		= amd_uncore_add,
916 		.del		= amd_uncore_del,
917 		.start		= amd_uncore_start,
918 		.stop		= amd_uncore_stop,
919 		.read		= amd_uncore_read,
920 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
921 		.module		= THIS_MODULE,
922 	};
923 
924 	if (perf_pmu_register(&pmu->pmu, pmu->pmu.name, -1)) {
925 		free_percpu(pmu->ctx);
926 		pmu->ctx = NULL;
927 		goto done;
928 	}
929 
930 	pr_info("%d %s%s counters detected\n", pmu->num_counters,
931 		boot_cpu_data.x86_vendor == X86_VENDOR_HYGON ?  "HYGON " : "",
932 		pmu->pmu.name);
933 
934 	uncore->num_pmus = 1;
935 
936 done:
937 	uncore->init_done = true;
938 
939 	return amd_uncore_ctx_init(uncore, cpu);
940 }
941 
amd_uncore_umc_event_init(struct perf_event * event)942 static int amd_uncore_umc_event_init(struct perf_event *event)
943 {
944 	struct hw_perf_event *hwc = &event->hw;
945 	int ret = amd_uncore_event_init(event);
946 
947 	if (ret)
948 		return ret;
949 
950 	hwc->config = event->attr.config & AMD64_PERFMON_V2_RAW_EVENT_MASK_UMC;
951 
952 	return 0;
953 }
954 
amd_uncore_umc_start(struct perf_event * event,int flags)955 static void amd_uncore_umc_start(struct perf_event *event, int flags)
956 {
957 	struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
958 	struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
959 	struct hw_perf_event *hwc = &event->hw;
960 
961 	if (!ctx->nr_active++)
962 		amd_uncore_start_hrtimer(ctx);
963 
964 	if (flags & PERF_EF_RELOAD)
965 		wrmsrq(hwc->event_base, (u64)local64_read(&hwc->prev_count));
966 
967 	hwc->state = 0;
968 	__set_bit(hwc->idx, ctx->active_mask);
969 	wrmsrq(hwc->config_base, (hwc->config | AMD64_PERFMON_V2_ENABLE_UMC));
970 	perf_event_update_userpage(event);
971 }
972 
amd_uncore_umc_read(struct perf_event * event)973 static void amd_uncore_umc_read(struct perf_event *event)
974 {
975 	struct hw_perf_event *hwc = &event->hw;
976 	u64 prev, new, shift;
977 	s64 delta;
978 
979 	shift = COUNTER_SHIFT + 1;
980 	prev = local64_read(&hwc->prev_count);
981 
982 	/*
983 	 * UMC counters do not have RDPMC assignments. Read counts directly
984 	 * from the corresponding PERF_CTR.
985 	 */
986 	rdmsrq(hwc->event_base, new);
987 
988 	/*
989 	 * Unlike the other uncore counters, UMC counters saturate and set the
990 	 * Overflow bit (bit 48) on overflow. Since they do not roll over,
991 	 * proactively reset the corresponding PERF_CTR when bit 47 is set so
992 	 * that the counter never gets a chance to saturate.
993 	 */
994 	if (new & BIT_ULL(63 - COUNTER_SHIFT)) {
995 		wrmsrq(hwc->event_base, 0);
996 		local64_set(&hwc->prev_count, 0);
997 	} else {
998 		local64_set(&hwc->prev_count, new);
999 	}
1000 
1001 	delta = (new << shift) - (prev << shift);
1002 	delta >>= shift;
1003 	local64_add(delta, &event->count);
1004 }
1005 
1006 static
amd_uncore_umc_ctx_scan(struct amd_uncore * uncore,unsigned int cpu)1007 void amd_uncore_umc_ctx_scan(struct amd_uncore *uncore, unsigned int cpu)
1008 {
1009 	union cpuid_0x80000022_ebx ebx;
1010 	union amd_uncore_info info;
1011 	unsigned int eax, ecx, edx;
1012 
1013 	if (pmu_version < 2)
1014 		return;
1015 
1016 	cpuid(EXT_PERFMON_DEBUG_FEATURES, &eax, &ebx.full, &ecx, &edx);
1017 	info.split.aux_data = ecx;	/* stash active mask */
1018 	info.split.num_pmcs = ebx.split.num_umc_pmc;
1019 	info.split.gid = topology_amd_node_id(cpu);
1020 	info.split.cid = topology_amd_node_id(cpu);
1021 	*per_cpu_ptr(uncore->info, cpu) = info;
1022 }
1023 
1024 static
amd_uncore_umc_ctx_init(struct amd_uncore * uncore,unsigned int cpu)1025 int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
1026 {
1027 	DECLARE_BITMAP(gmask, UNCORE_GROUP_MAX) = { 0 };
1028 	u8 group_num_pmus[UNCORE_GROUP_MAX] = { 0 };
1029 	u8 group_num_pmcs[UNCORE_GROUP_MAX] = { 0 };
1030 	union amd_uncore_info info;
1031 	struct amd_uncore_pmu *pmu;
1032 	int gid, i;
1033 	u16 index = 0;
1034 
1035 	if (pmu_version < 2)
1036 		return 0;
1037 
1038 	/* Run just once */
1039 	if (uncore->init_done)
1040 		return amd_uncore_ctx_init(uncore, cpu);
1041 
1042 	/* Find unique groups */
1043 	for_each_online_cpu(i) {
1044 		info = *per_cpu_ptr(uncore->info, i);
1045 		gid = info.split.gid;
1046 		if (test_bit(gid, gmask))
1047 			continue;
1048 
1049 		__set_bit(gid, gmask);
1050 		group_num_pmus[gid] = hweight32(info.split.aux_data);
1051 		group_num_pmcs[gid] = info.split.num_pmcs;
1052 		uncore->num_pmus += group_num_pmus[gid];
1053 	}
1054 
1055 	uncore->pmus = kzalloc(sizeof(*uncore->pmus) * uncore->num_pmus,
1056 			       GFP_KERNEL);
1057 	if (!uncore->pmus) {
1058 		uncore->num_pmus = 0;
1059 		goto done;
1060 	}
1061 
1062 	for_each_set_bit(gid, gmask, UNCORE_GROUP_MAX) {
1063 		for (i = 0; i < group_num_pmus[gid]; i++) {
1064 			pmu = &uncore->pmus[index];
1065 			snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%hu", index);
1066 			pmu->num_counters = group_num_pmcs[gid] / group_num_pmus[gid];
1067 			pmu->msr_base = MSR_F19H_UMC_PERF_CTL + i * pmu->num_counters * 2;
1068 			pmu->rdpmc_base = -1;
1069 			pmu->group = gid;
1070 
1071 			pmu->ctx = alloc_percpu(struct amd_uncore_ctx *);
1072 			if (!pmu->ctx)
1073 				goto done;
1074 
1075 			pmu->pmu = (struct pmu) {
1076 				.task_ctx_nr	= perf_invalid_context,
1077 				.attr_groups	= amd_uncore_umc_attr_groups,
1078 				.name		= pmu->name,
1079 				.event_init	= amd_uncore_umc_event_init,
1080 				.add		= amd_uncore_add,
1081 				.del		= amd_uncore_del,
1082 				.start		= amd_uncore_umc_start,
1083 				.stop		= amd_uncore_stop,
1084 				.read		= amd_uncore_umc_read,
1085 				.capabilities	= PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
1086 				.module		= THIS_MODULE,
1087 			};
1088 
1089 			if (perf_pmu_register(&pmu->pmu, pmu->pmu.name, -1)) {
1090 				free_percpu(pmu->ctx);
1091 				pmu->ctx = NULL;
1092 				goto done;
1093 			}
1094 
1095 			pr_info("%d %s counters detected\n", pmu->num_counters,
1096 				pmu->pmu.name);
1097 
1098 			index++;
1099 		}
1100 	}
1101 
1102 done:
1103 	uncore->num_pmus = index;
1104 	uncore->init_done = true;
1105 
1106 	return amd_uncore_ctx_init(uncore, cpu);
1107 }
1108 
1109 static struct amd_uncore uncores[UNCORE_TYPE_MAX] = {
1110 	/* UNCORE_TYPE_DF */
1111 	{
1112 		.scan = amd_uncore_df_ctx_scan,
1113 		.init = amd_uncore_df_ctx_init,
1114 		.move = amd_uncore_ctx_move,
1115 		.free = amd_uncore_ctx_free,
1116 	},
1117 	/* UNCORE_TYPE_L3 */
1118 	{
1119 		.scan = amd_uncore_l3_ctx_scan,
1120 		.init = amd_uncore_l3_ctx_init,
1121 		.move = amd_uncore_ctx_move,
1122 		.free = amd_uncore_ctx_free,
1123 	},
1124 	/* UNCORE_TYPE_UMC */
1125 	{
1126 		.scan = amd_uncore_umc_ctx_scan,
1127 		.init = amd_uncore_umc_ctx_init,
1128 		.move = amd_uncore_ctx_move,
1129 		.free = amd_uncore_ctx_free,
1130 	},
1131 };
1132 
amd_uncore_init(void)1133 static int __init amd_uncore_init(void)
1134 {
1135 	struct amd_uncore *uncore;
1136 	int ret = -ENODEV;
1137 	int i;
1138 
1139 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
1140 	    boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
1141 		return -ENODEV;
1142 
1143 	if (!boot_cpu_has(X86_FEATURE_TOPOEXT))
1144 		return -ENODEV;
1145 
1146 	if (boot_cpu_has(X86_FEATURE_PERFMON_V2))
1147 		pmu_version = 2;
1148 
1149 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
1150 		uncore = &uncores[i];
1151 
1152 		BUG_ON(!uncore->scan);
1153 		BUG_ON(!uncore->init);
1154 		BUG_ON(!uncore->move);
1155 		BUG_ON(!uncore->free);
1156 
1157 		uncore->info = alloc_percpu(union amd_uncore_info);
1158 		if (!uncore->info) {
1159 			ret = -ENOMEM;
1160 			goto fail;
1161 		}
1162 	};
1163 
1164 	/*
1165 	 * Install callbacks. Core will call them for each online cpu.
1166 	 */
1167 	ret = cpuhp_setup_state(CPUHP_PERF_X86_AMD_UNCORE_PREP,
1168 				"perf/x86/amd/uncore:prepare",
1169 				NULL, amd_uncore_cpu_dead);
1170 	if (ret)
1171 		goto fail;
1172 
1173 	ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
1174 				"perf/x86/amd/uncore:starting",
1175 				amd_uncore_cpu_starting, NULL);
1176 	if (ret)
1177 		goto fail_prep;
1178 
1179 	ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_ONLINE,
1180 				"perf/x86/amd/uncore:online",
1181 				amd_uncore_cpu_online,
1182 				amd_uncore_cpu_down_prepare);
1183 	if (ret)
1184 		goto fail_start;
1185 
1186 	return 0;
1187 
1188 fail_start:
1189 	cpuhp_remove_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING);
1190 fail_prep:
1191 	cpuhp_remove_state(CPUHP_PERF_X86_AMD_UNCORE_PREP);
1192 fail:
1193 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
1194 		uncore = &uncores[i];
1195 		if (uncore->info) {
1196 			free_percpu(uncore->info);
1197 			uncore->info = NULL;
1198 		}
1199 	}
1200 
1201 	return ret;
1202 }
1203 
amd_uncore_exit(void)1204 static void __exit amd_uncore_exit(void)
1205 {
1206 	struct amd_uncore *uncore;
1207 	struct amd_uncore_pmu *pmu;
1208 	int i, j;
1209 
1210 	cpuhp_remove_state(CPUHP_AP_PERF_X86_AMD_UNCORE_ONLINE);
1211 	cpuhp_remove_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING);
1212 	cpuhp_remove_state(CPUHP_PERF_X86_AMD_UNCORE_PREP);
1213 
1214 	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
1215 		uncore = &uncores[i];
1216 		if (!uncore->info)
1217 			continue;
1218 
1219 		free_percpu(uncore->info);
1220 		uncore->info = NULL;
1221 
1222 		for (j = 0; j < uncore->num_pmus; j++) {
1223 			pmu = &uncore->pmus[j];
1224 			if (!pmu->ctx)
1225 				continue;
1226 
1227 			perf_pmu_unregister(&pmu->pmu);
1228 			free_percpu(pmu->ctx);
1229 			pmu->ctx = NULL;
1230 		}
1231 
1232 		kfree(uncore->pmus);
1233 		uncore->pmus = NULL;
1234 	}
1235 }
1236 
1237 module_init(amd_uncore_init);
1238 module_exit(amd_uncore_exit);
1239 
1240 MODULE_DESCRIPTION("AMD Uncore Driver");
1241 MODULE_LICENSE("GPL v2");
1242