uncore.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2013 Advanced Micro Devices, Inc.
 *
 * Author: Jacob Shin <jacob.shin@amd.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/perf_event.h>
#include <linux/percpu.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>

#include <asm/cpufeature.h>
#include <asm/perf_event.h>
#include <asm/msr.h>

#define NUM_COUNTERS_NB		4
#define NUM_COUNTERS_L2		4
25 26
#define NUM_COUNTERS_L3		6
#define MAX_COUNTERS		6
27 28

#define RDPMC_BASE_NB		6
29
#define RDPMC_BASE_LLC		10
30 31 32

#define COUNTER_SHIFT		16

33 34 35
static int num_counters_llc;
static int num_counters_nb;

36 37
static HLIST_HEAD(uncore_unused_list);

38 39 40 41 42 43 44 45 46 47
struct amd_uncore {
	int id;
	int refcnt;
	int cpu;
	int num_counters;
	int rdpmc_base;
	u32 msr_base;
	cpumask_t *active_mask;
	struct pmu *pmu;
	struct perf_event *events[MAX_COUNTERS];
48
	struct hlist_node node;
49 50 51
};

static struct amd_uncore * __percpu *amd_uncore_nb;
52
static struct amd_uncore * __percpu *amd_uncore_llc;
53 54

static struct pmu amd_nb_pmu;
55
static struct pmu amd_llc_pmu;
56 57

static cpumask_t amd_nb_active_mask;
58
static cpumask_t amd_llc_active_mask;
59 60 61 62 63 64

static bool is_nb_event(struct perf_event *event)
{
	return event->pmu->type == amd_nb_pmu.type;
}

65
static bool is_llc_event(struct perf_event *event)
66
{
67
	return event->pmu->type == amd_llc_pmu.type;
68 69 70 71 72 73
}

static struct amd_uncore *event_to_amd_uncore(struct perf_event *event)
{
	if (is_nb_event(event) && amd_uncore_nb)
		return *per_cpu_ptr(amd_uncore_nb, event->cpu);
74 75
	else if (is_llc_event(event) && amd_uncore_llc)
		return *per_cpu_ptr(amd_uncore_llc, event->cpu);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

	return NULL;
}

static void amd_uncore_read(struct perf_event *event)
{
	struct hw_perf_event *hwc = &event->hw;
	u64 prev, new;
	s64 delta;

	/*
	 * since we do not enable counter overflow interrupts,
	 * we do not have to worry about prev_count changing on us
	 */

	prev = local64_read(&hwc->prev_count);
	rdpmcl(hwc->event_base_rdpmc, new);
	local64_set(&hwc->prev_count, new);
	delta = (new << COUNTER_SHIFT) - (prev << COUNTER_SHIFT);
	delta >>= COUNTER_SHIFT;
	local64_add(delta, &event->count);
}

static void amd_uncore_start(struct perf_event *event, int flags)
{
	struct hw_perf_event *hwc = &event->hw;

	if (flags & PERF_EF_RELOAD)
		wrmsrl(hwc->event_base, (u64)local64_read(&hwc->prev_count));

	hwc->state = 0;
	wrmsrl(hwc->config_base, (hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE));
	perf_event_update_userpage(event);
}

static void amd_uncore_stop(struct perf_event *event, int flags)
{
	struct hw_perf_event *hwc = &event->hw;

	wrmsrl(hwc->config_base, hwc->config);
	hwc->state |= PERF_HES_STOPPED;

	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
		amd_uncore_read(event);
		hwc->state |= PERF_HES_UPTODATE;
	}
}

static int amd_uncore_add(struct perf_event *event, int flags)
{
	int i;
	struct amd_uncore *uncore = event_to_amd_uncore(event);
	struct hw_perf_event *hwc = &event->hw;

	/* are we already assigned? */
	if (hwc->idx != -1 && uncore->events[hwc->idx] == event)
		goto out;

	for (i = 0; i < uncore->num_counters; i++) {
		if (uncore->events[i] == event) {
			hwc->idx = i;
			goto out;
		}
	}

	/* if not, take the first available counter */
	hwc->idx = -1;
	for (i = 0; i < uncore->num_counters; i++) {
		if (cmpxchg(&uncore->events[i], NULL, event) == NULL) {
			hwc->idx = i;
			break;
		}
	}

out:
	if (hwc->idx == -1)
		return -EBUSY;

	hwc->config_base = uncore->msr_base + (2 * hwc->idx);
	hwc->event_base = uncore->msr_base + 1 + (2 * hwc->idx);
	hwc->event_base_rdpmc = uncore->rdpmc_base + hwc->idx;
	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;

	if (flags & PERF_EF_START)
		amd_uncore_start(event, PERF_EF_RELOAD);

	return 0;
}

static void amd_uncore_del(struct perf_event *event, int flags)
{
	int i;
	struct amd_uncore *uncore = event_to_amd_uncore(event);
	struct hw_perf_event *hwc = &event->hw;

	amd_uncore_stop(event, PERF_EF_UPDATE);

	for (i = 0; i < uncore->num_counters; i++) {
		if (cmpxchg(&uncore->events[i], event, NULL) == event)
			break;
	}

	hwc->idx = -1;
}

static int amd_uncore_event_init(struct perf_event *event)
{
	struct amd_uncore *uncore;
	struct hw_perf_event *hwc = &event->hw;

	if (event->attr.type != event->pmu->type)
		return -ENOENT;

	/*
190 191 192 193 194
	 * NB and Last level cache counters (MSRs) are shared across all cores
	 * that share the same NB / Last level cache. Interrupts can be directed
	 * to a single target core, however, event counts generated by processes
	 * running on other cores cannot be masked out. So we do not support
	 * sampling and per-thread events.
195 196 197 198
	 */
	if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
		return -EINVAL;

199
	/* NB and Last level cache counters do not have usr/os/guest/host bits */
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	if (event->attr.exclude_user || event->attr.exclude_kernel ||
	    event->attr.exclude_host || event->attr.exclude_guest)
		return -EINVAL;

	/* and we do not enable counter overflow interrupts */
	hwc->config = event->attr.config & AMD64_RAW_EVENT_MASK_NB;
	hwc->idx = -1;

	if (event->cpu < 0)
		return -EINVAL;

	uncore = event_to_amd_uncore(event);
	if (!uncore)
		return -ENODEV;

	/*
	 * since request can come in to any of the shared cores, we will remap
	 * to a single common cpu.
	 */
	event->cpu = uncore->cpu;

	return 0;
}

static ssize_t amd_uncore_attr_show_cpumask(struct device *dev,
					    struct device_attribute *attr,
					    char *buf)
{
	cpumask_t *active_mask;
	struct pmu *pmu = dev_get_drvdata(dev);

	if (pmu->type == amd_nb_pmu.type)
		active_mask = &amd_nb_active_mask;
233 234
	else if (pmu->type == amd_llc_pmu.type)
		active_mask = &amd_llc_active_mask;
235 236 237
	else
		return 0;

238
	return cpumap_print_to_pagebuf(true, buf, active_mask);
239 240 241 242 243 244 245 246 247 248 249 250
}
static DEVICE_ATTR(cpumask, S_IRUGO, amd_uncore_attr_show_cpumask, NULL);

static struct attribute *amd_uncore_attrs[] = {
	&dev_attr_cpumask.attr,
	NULL,
};

static struct attribute_group amd_uncore_attr_group = {
	.attrs = amd_uncore_attrs,
};

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/*
 * Similar to PMU_FORMAT_ATTR but allowing for format_attr to be assigned based
 * on family
 */
#define AMD_FORMAT_ATTR(_dev, _name, _format)				     \
static ssize_t								     \
_dev##_show##_name(struct device *dev,					     \
		struct device_attribute *attr,				     \
		char *page)						     \
{									     \
	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			     \
	return sprintf(page, _format "\n");				     \
}									     \
static struct device_attribute format_attr_##_dev##_name = __ATTR_RO(_dev);

/* Used for each uncore counter type */
#define AMD_ATTRIBUTE(_name)						     \
static struct attribute *amd_uncore_format_attr_##_name[] = {		     \
	&format_attr_event_##_name.attr,				     \
	&format_attr_umask.attr,					     \
	NULL,								     \
};									     \
static struct attribute_group amd_uncore_format_group_##_name = {	     \
	.name = "format",						     \
	.attrs = amd_uncore_format_attr_##_name,			     \
};									     \
static const struct attribute_group *amd_uncore_attr_groups_##_name[] = {    \
	&amd_uncore_attr_group,						     \
	&amd_uncore_format_group_##_name,				     \
	NULL,								     \
281 282
};

283 284 285 286 287 288
AMD_FORMAT_ATTR(event, , "config:0-7,32-35");
AMD_FORMAT_ATTR(umask, , "config:8-15");
AMD_FORMAT_ATTR(event, _df, "config:0-7,32-35,59-60");
AMD_FORMAT_ATTR(event, _l3, "config:0-7");
AMD_ATTRIBUTE(df);
AMD_ATTRIBUTE(l3);
289 290

static struct pmu amd_nb_pmu = {
291
	.task_ctx_nr	= perf_invalid_context,
292 293 294 295 296 297 298 299
	.event_init	= amd_uncore_event_init,
	.add		= amd_uncore_add,
	.del		= amd_uncore_del,
	.start		= amd_uncore_start,
	.stop		= amd_uncore_stop,
	.read		= amd_uncore_read,
};

300
static struct pmu amd_llc_pmu = {
301
	.task_ctx_nr	= perf_invalid_context,
302 303 304 305 306 307 308 309
	.event_init	= amd_uncore_event_init,
	.add		= amd_uncore_add,
	.del		= amd_uncore_del,
	.start		= amd_uncore_start,
	.stop		= amd_uncore_stop,
	.read		= amd_uncore_read,
};

310
static struct amd_uncore *amd_uncore_alloc(unsigned int cpu)
311 312 313 314 315
{
	return kzalloc_node(sizeof(struct amd_uncore), GFP_KERNEL,
			cpu_to_node(cpu));
}

316
static int amd_uncore_cpu_up_prepare(unsigned int cpu)
317
{
318
	struct amd_uncore *uncore_nb = NULL, *uncore_llc;
319 320

	if (amd_uncore_nb) {
321 322 323 324
		uncore_nb = amd_uncore_alloc(cpu);
		if (!uncore_nb)
			goto fail;
		uncore_nb->cpu = cpu;
325
		uncore_nb->num_counters = num_counters_nb;
326 327 328 329
		uncore_nb->rdpmc_base = RDPMC_BASE_NB;
		uncore_nb->msr_base = MSR_F15H_NB_PERF_CTL;
		uncore_nb->active_mask = &amd_nb_active_mask;
		uncore_nb->pmu = &amd_nb_pmu;
330
		uncore_nb->id = -1;
331
		*per_cpu_ptr(amd_uncore_nb, cpu) = uncore_nb;
332 333
	}

334 335 336
	if (amd_uncore_llc) {
		uncore_llc = amd_uncore_alloc(cpu);
		if (!uncore_llc)
337
			goto fail;
338
		uncore_llc->cpu = cpu;
339
		uncore_llc->num_counters = num_counters_llc;
340 341 342 343 344 345
		uncore_llc->rdpmc_base = RDPMC_BASE_LLC;
		uncore_llc->msr_base = MSR_F16H_L2I_PERF_CTL;
		uncore_llc->active_mask = &amd_llc_active_mask;
		uncore_llc->pmu = &amd_llc_pmu;
		uncore_llc->id = -1;
		*per_cpu_ptr(amd_uncore_llc, cpu) = uncore_llc;
346
	}
347 348 349 350

	return 0;

fail:
351 352
	if (amd_uncore_nb)
		*per_cpu_ptr(amd_uncore_nb, cpu) = NULL;
353 354
	kfree(uncore_nb);
	return -ENOMEM;
355 356 357
}

static struct amd_uncore *
358 359
amd_uncore_find_online_sibling(struct amd_uncore *this,
			       struct amd_uncore * __percpu *uncores)
360 361 362 363 364 365 366 367 368 369 370 371 372 373
{
	unsigned int cpu;
	struct amd_uncore *that;

	for_each_online_cpu(cpu) {
		that = *per_cpu_ptr(uncores, cpu);

		if (!that)
			continue;

		if (this == that)
			continue;

		if (this->id == that->id) {
374
			hlist_add_head(&this->node, &uncore_unused_list);
375 376 377 378 379 380 381 382 383
			this = that;
			break;
		}
	}

	this->refcnt++;
	return this;
}

384
static int amd_uncore_cpu_starting(unsigned int cpu)
385 386 387 388 389 390 391 392 393 394 395 396 397
{
	unsigned int eax, ebx, ecx, edx;
	struct amd_uncore *uncore;

	if (amd_uncore_nb) {
		uncore = *per_cpu_ptr(amd_uncore_nb, cpu);
		cpuid(0x8000001e, &eax, &ebx, &ecx, &edx);
		uncore->id = ecx & 0xff;

		uncore = amd_uncore_find_online_sibling(uncore, amd_uncore_nb);
		*per_cpu_ptr(amd_uncore_nb, cpu) = uncore;
	}

398
	if (amd_uncore_llc) {
399 400 401
		unsigned int apicid = cpu_data(cpu).apicid;
		unsigned int nshared;

402
		uncore = *per_cpu_ptr(amd_uncore_llc, cpu);
403 404 405 406
		cpuid_count(0x8000001d, 2, &eax, &ebx, &ecx, &edx);
		nshared = ((eax >> 14) & 0xfff) + 1;
		uncore->id = apicid - (apicid % nshared);

407 408
		uncore = amd_uncore_find_online_sibling(uncore, amd_uncore_llc);
		*per_cpu_ptr(amd_uncore_llc, cpu) = uncore;
409
	}
410 411

	return 0;
412 413
}

414 415 416 417 418 419 420 421 422 423 424
static void uncore_clean_online(void)
{
	struct amd_uncore *uncore;
	struct hlist_node *n;

	hlist_for_each_entry_safe(uncore, n, &uncore_unused_list, node) {
		hlist_del(&uncore->node);
		kfree(uncore);
	}
}

425 426
static void uncore_online(unsigned int cpu,
			  struct amd_uncore * __percpu *uncores)
427 428 429
{
	struct amd_uncore *uncore = *per_cpu_ptr(uncores, cpu);

430
	uncore_clean_online();
431 432 433 434 435

	if (cpu == uncore->cpu)
		cpumask_set_cpu(cpu, uncore->active_mask);
}

436
static int amd_uncore_cpu_online(unsigned int cpu)
437 438 439 440
{
	if (amd_uncore_nb)
		uncore_online(cpu, amd_uncore_nb);

441 442
	if (amd_uncore_llc)
		uncore_online(cpu, amd_uncore_llc);
443 444

	return 0;
445 446
}

447 448
static void uncore_down_prepare(unsigned int cpu,
				struct amd_uncore * __percpu *uncores)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
	unsigned int i;
	struct amd_uncore *this = *per_cpu_ptr(uncores, cpu);

	if (this->cpu != cpu)
		return;

	/* this cpu is going down, migrate to a shared sibling if possible */
	for_each_online_cpu(i) {
		struct amd_uncore *that = *per_cpu_ptr(uncores, i);

		if (cpu == i)
			continue;

		if (this == that) {
			perf_pmu_migrate_context(this->pmu, cpu, i);
			cpumask_clear_cpu(cpu, that->active_mask);
			cpumask_set_cpu(i, that->active_mask);
			that->cpu = i;
			break;
		}
	}
}

473
static int amd_uncore_cpu_down_prepare(unsigned int cpu)
474 475 476 477
{
	if (amd_uncore_nb)
		uncore_down_prepare(cpu, amd_uncore_nb);

478 479
	if (amd_uncore_llc)
		uncore_down_prepare(cpu, amd_uncore_llc);
480 481

	return 0;
482 483
}

484
static void uncore_dead(unsigned int cpu, struct amd_uncore * __percpu *uncores)
485 486 487 488 489 490 491 492
{
	struct amd_uncore *uncore = *per_cpu_ptr(uncores, cpu);

	if (cpu == uncore->cpu)
		cpumask_clear_cpu(cpu, uncore->active_mask);

	if (!--uncore->refcnt)
		kfree(uncore);
493
	*per_cpu_ptr(uncores, cpu) = NULL;
494 495
}

496
static int amd_uncore_cpu_dead(unsigned int cpu)
497 498 499 500
{
	if (amd_uncore_nb)
		uncore_dead(cpu, amd_uncore_nb);

501 502
	if (amd_uncore_llc)
		uncore_dead(cpu, amd_uncore_llc);
503

504
	return 0;
505 506
}

507 508 509 510 511
static int __init amd_uncore_init(void)
{
	int ret = -ENODEV;

	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
512 513 514 515
		return -ENODEV;

	if (!boot_cpu_has(X86_FEATURE_TOPOEXT))
		return -ENODEV;
516

517 518 519 520 521
	switch(boot_cpu_data.x86) {
		case 23:
			/* Family 17h: */
			num_counters_nb = NUM_COUNTERS_NB;
			num_counters_llc = NUM_COUNTERS_L3;
522 523 524 525 526 527 528 529 530 531
			/*
			 * For Family17h, the NorthBridge counters are
			 * re-purposed as Data Fabric counters. Also, support is
			 * added for L3 counters. The pmus are exported based on
			 * family as either L2 or L3 and NB or DF.
			 */
			amd_nb_pmu.name = "amd_df";
			amd_llc_pmu.name = "amd_l3";
			format_attr_event_df.show = &event_show_df;
			format_attr_event_l3.show = &event_show_l3;
532 533 534 535 536
			break;
		case 22:
			/* Family 16h - may change: */
			num_counters_nb = NUM_COUNTERS_NB;
			num_counters_llc = NUM_COUNTERS_L2;
537 538 539 540
			amd_nb_pmu.name = "amd_nb";
			amd_llc_pmu.name = "amd_l2";
			format_attr_event_df = format_attr_event;
			format_attr_event_l3 = format_attr_event;
541 542 543 544 545 546 547 548
			break;
		default:
			/*
			 * All prior families have the same number of
			 * NorthBridge and Last Level Cache counters
			 */
			num_counters_nb = NUM_COUNTERS_NB;
			num_counters_llc = NUM_COUNTERS_L2;
549 550 551 552
			amd_nb_pmu.name = "amd_nb";
			amd_llc_pmu.name = "amd_l2";
			format_attr_event_df = format_attr_event;
			format_attr_event_l3 = format_attr_event;
553 554
			break;
	}
555 556
	amd_nb_pmu.attr_groups = amd_uncore_attr_groups_df;
	amd_llc_pmu.attr_groups = amd_uncore_attr_groups_l3;
557

558
	if (boot_cpu_has(X86_FEATURE_PERFCTR_NB)) {
559
		amd_uncore_nb = alloc_percpu(struct amd_uncore *);
560 561 562 563 564 565 566
		if (!amd_uncore_nb) {
			ret = -ENOMEM;
			goto fail_nb;
		}
		ret = perf_pmu_register(&amd_nb_pmu, amd_nb_pmu.name, -1);
		if (ret)
			goto fail_nb;
567

568
		pr_info("perf: AMD NB counters detected\n");
569 570 571
		ret = 0;
	}

572
	if (boot_cpu_has(X86_FEATURE_PERFCTR_L2)) {
573 574
		amd_uncore_llc = alloc_percpu(struct amd_uncore *);
		if (!amd_uncore_llc) {
575
			ret = -ENOMEM;
576
			goto fail_llc;
577
		}
578
		ret = perf_pmu_register(&amd_llc_pmu, amd_llc_pmu.name, -1);
579
		if (ret)
580
			goto fail_llc;
581

582
		pr_info("perf: AMD LLC counters detected\n");
583 584 585
		ret = 0;
	}

586 587 588 589
	/*
	 * Install callbacks. Core will call them for each online cpu.
	 */
	if (cpuhp_setup_state(CPUHP_PERF_X86_AMD_UNCORE_PREP,
T
Thomas Gleixner 已提交
590
			      "perf/x86/amd/uncore:prepare",
591
			      amd_uncore_cpu_up_prepare, amd_uncore_cpu_dead))
592
		goto fail_llc;
593 594

	if (cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
T
Thomas Gleixner 已提交
595
			      "perf/x86/amd/uncore:starting",
596 597 598
			      amd_uncore_cpu_starting, NULL))
		goto fail_prep;
	if (cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_ONLINE,
T
Thomas Gleixner 已提交
599
			      "perf/x86/amd/uncore:online",
600 601 602
			      amd_uncore_cpu_online,
			      amd_uncore_cpu_down_prepare))
		goto fail_start;
603
	return 0;
604

605 606 607 608
fail_start:
	cpuhp_remove_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING);
fail_prep:
	cpuhp_remove_state(CPUHP_PERF_X86_AMD_UNCORE_PREP);
609
fail_llc:
610
	if (boot_cpu_has(X86_FEATURE_PERFCTR_NB))
611
		perf_pmu_unregister(&amd_nb_pmu);
612 613
	if (amd_uncore_llc)
		free_percpu(amd_uncore_llc);
614 615 616 617 618
fail_nb:
	if (amd_uncore_nb)
		free_percpu(amd_uncore_nb);

	return ret;
619 620
}
device_initcall(amd_uncore_init);