perf_event.c 15.2 KB
Newer Older
1 2 3 4 5 6
#undef DEBUG

/*
 * ARM performance counter support.
 *
 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
7
 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
8
 *
9 10 11 12 13 14 15
 * This code is based on the sparc64 perf event code, which is in turn based
 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
 * code.
 */
#define pr_fmt(fmt) "hw perfevents: " fmt

#include <linux/kernel.h>
16
#include <linux/platform_device.h>
J
Jon Hunter 已提交
17
#include <linux/pm_runtime.h>
18
#include <linux/uaccess.h>
19 20
#include <linux/irq.h>
#include <linux/irqdesc.h>
21 22 23 24 25 26

#include <asm/irq_regs.h>
#include <asm/pmu.h>
#include <asm/stacktrace.h>

static int
M
Mark Rutland 已提交
27 28 29 30 31
armpmu_map_cache_event(const unsigned (*cache_map)
				      [PERF_COUNT_HW_CACHE_MAX]
				      [PERF_COUNT_HW_CACHE_OP_MAX]
				      [PERF_COUNT_HW_CACHE_RESULT_MAX],
		       u64 config)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	unsigned int cache_type, cache_op, cache_result, ret;

	cache_type = (config >>  0) & 0xff;
	if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
		return -EINVAL;

	cache_op = (config >>  8) & 0xff;
	if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
		return -EINVAL;

	cache_result = (config >> 16) & 0xff;
	if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
		return -EINVAL;

M
Mark Rutland 已提交
47
	ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
48 49 50 51 52 53 54

	if (ret == CACHE_OP_UNSUPPORTED)
		return -ENOENT;

	return ret;
}

55
static int
56
armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
57
{
58 59 60 61 62 63
	int mapping;

	if (config >= PERF_COUNT_HW_MAX)
		return -EINVAL;

	mapping = (*event_map)[config];
M
Mark Rutland 已提交
64
	return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
65 66 67
}

static int
M
Mark Rutland 已提交
68
armpmu_map_raw_event(u32 raw_event_mask, u64 config)
69
{
M
Mark Rutland 已提交
70 71 72
	return (int)(config & raw_event_mask);
}

73 74 75 76 77 78 79 80
int
armpmu_map_event(struct perf_event *event,
		 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
		 const unsigned (*cache_map)
				[PERF_COUNT_HW_CACHE_MAX]
				[PERF_COUNT_HW_CACHE_OP_MAX]
				[PERF_COUNT_HW_CACHE_RESULT_MAX],
		 u32 raw_event_mask)
M
Mark Rutland 已提交
81 82 83 84 85
{
	u64 config = event->attr.config;

	switch (event->attr.type) {
	case PERF_TYPE_HARDWARE:
86
		return armpmu_map_hw_event(event_map, config);
M
Mark Rutland 已提交
87 88 89 90 91 92 93
	case PERF_TYPE_HW_CACHE:
		return armpmu_map_cache_event(cache_map, config);
	case PERF_TYPE_RAW:
		return armpmu_map_raw_event(raw_event_mask, config);
	}

	return -ENOENT;
94 95
}

96
int armpmu_event_set_period(struct perf_event *event)
97
{
98
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
99
	struct hw_perf_event *hwc = &event->hw;
100
	s64 left = local64_read(&hwc->period_left);
101 102 103 104 105
	s64 period = hwc->sample_period;
	int ret = 0;

	if (unlikely(left <= -period)) {
		left = period;
106
		local64_set(&hwc->period_left, left);
107 108 109 110 111 112
		hwc->last_period = period;
		ret = 1;
	}

	if (unlikely(left <= 0)) {
		left += period;
113
		local64_set(&hwc->period_left, left);
114 115 116 117 118 119 120
		hwc->last_period = period;
		ret = 1;
	}

	if (left > (s64)armpmu->max_period)
		left = armpmu->max_period;

121
	local64_set(&hwc->prev_count, (u64)-left);
122

123
	armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
124 125 126 127 128 129

	perf_event_update_userpage(event);

	return ret;
}

130
u64 armpmu_event_update(struct perf_event *event)
131
{
132
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
133
	struct hw_perf_event *hwc = &event->hw;
134
	u64 delta, prev_raw_count, new_raw_count;
135 136

again:
137
	prev_raw_count = local64_read(&hwc->prev_count);
138
	new_raw_count = armpmu->read_counter(event);
139

140
	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
141 142 143
			     new_raw_count) != prev_raw_count)
		goto again;

144
	delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
145

146 147
	local64_add(delta, &event->count);
	local64_sub(delta, &hwc->period_left);
148 149 150 151 152

	return new_raw_count;
}

static void
P
Peter Zijlstra 已提交
153
armpmu_read(struct perf_event *event)
154
{
155
	armpmu_event_update(event);
156 157 158
}

static void
P
Peter Zijlstra 已提交
159
armpmu_stop(struct perf_event *event, int flags)
160
{
161
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
162 163
	struct hw_perf_event *hwc = &event->hw;

P
Peter Zijlstra 已提交
164 165 166 167 168
	/*
	 * ARM pmu always has to update the counter, so ignore
	 * PERF_EF_UPDATE, see comments in armpmu_start().
	 */
	if (!(hwc->state & PERF_HES_STOPPED)) {
169 170
		armpmu->disable(event);
		armpmu_event_update(event);
P
Peter Zijlstra 已提交
171 172
		hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
	}
173 174
}

175
static void armpmu_start(struct perf_event *event, int flags)
176
{
177
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
178 179
	struct hw_perf_event *hwc = &event->hw;

P
Peter Zijlstra 已提交
180 181 182 183 184 185 186 187
	/*
	 * ARM pmu always has to reprogram the period, so ignore
	 * PERF_EF_RELOAD, see the comment below.
	 */
	if (flags & PERF_EF_RELOAD)
		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

	hwc->state = 0;
188 189
	/*
	 * Set the period again. Some counters can't be stopped, so when we
P
Peter Zijlstra 已提交
190
	 * were stopped we simply disabled the IRQ source and the counter
191 192 193 194
	 * may have been left counting. If we don't do this step then we may
	 * get an interrupt too soon or *way* too late if the overflow has
	 * happened since disabling.
	 */
195 196
	armpmu_event_set_period(event);
	armpmu->enable(event);
197 198
}

P
Peter Zijlstra 已提交
199 200 201
static void
armpmu_del(struct perf_event *event, int flags)
{
202
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
203
	struct pmu_hw_events *hw_events = armpmu->get_hw_events();
P
Peter Zijlstra 已提交
204 205 206 207
	struct hw_perf_event *hwc = &event->hw;
	int idx = hwc->idx;

	armpmu_stop(event, PERF_EF_UPDATE);
208 209
	hw_events->events[idx] = NULL;
	clear_bit(idx, hw_events->used_mask);
P
Peter Zijlstra 已提交
210 211 212 213

	perf_event_update_userpage(event);
}

214
static int
P
Peter Zijlstra 已提交
215
armpmu_add(struct perf_event *event, int flags)
216
{
217
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
218
	struct pmu_hw_events *hw_events = armpmu->get_hw_events();
219 220 221 222
	struct hw_perf_event *hwc = &event->hw;
	int idx;
	int err = 0;

P
Peter Zijlstra 已提交
223
	perf_pmu_disable(event->pmu);
224

225
	/* If we don't have a space for the counter then finish early. */
226
	idx = armpmu->get_event_idx(hw_events, event);
227 228 229 230 231 232 233 234 235 236
	if (idx < 0) {
		err = idx;
		goto out;
	}

	/*
	 * If there is an event in the counter we are going to use then make
	 * sure it is disabled.
	 */
	event->hw.idx = idx;
237
	armpmu->disable(event);
238
	hw_events->events[idx] = event;
239

P
Peter Zijlstra 已提交
240 241 242
	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
	if (flags & PERF_EF_START)
		armpmu_start(event, PERF_EF_RELOAD);
243 244 245 246 247

	/* Propagate our changes to the userspace mapping. */
	perf_event_update_userpage(event);

out:
P
Peter Zijlstra 已提交
248
	perf_pmu_enable(event->pmu);
249 250 251 252
	return err;
}

static int
253
validate_event(struct pmu_hw_events *hw_events,
254 255
	       struct perf_event *event)
{
256
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
257

258 259 260
	if (is_software_event(event))
		return 1;

261
	if (event->state < PERF_EVENT_STATE_OFF)
262 263 264
		return 1;

	if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
265
		return 1;
266

267
	return armpmu->get_event_idx(hw_events, event) >= 0;
268 269 270 271 272 273
}

static int
validate_group(struct perf_event *event)
{
	struct perf_event *sibling, *leader = event->group_leader;
274
	struct pmu_hw_events fake_pmu;
275
	DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
276

277 278 279 280 281 282
	/*
	 * Initialise the fake PMU. We only need to populate the
	 * used_mask for the purposes of validation.
	 */
	memset(fake_used_mask, 0, sizeof(fake_used_mask));
	fake_pmu.used_mask = fake_used_mask;
283 284

	if (!validate_event(&fake_pmu, leader))
285
		return -EINVAL;
286 287 288

	list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
		if (!validate_event(&fake_pmu, sibling))
289
			return -EINVAL;
290 291 292
	}

	if (!validate_event(&fake_pmu, event))
293
		return -EINVAL;
294 295 296 297

	return 0;
}

298
static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
299
{
300 301 302 303 304 305 306 307 308
	struct arm_pmu *armpmu;
	struct platform_device *plat_device;
	struct arm_pmu_platdata *plat;

	if (irq_is_percpu(irq))
		dev = *(void **)dev;
	armpmu = dev;
	plat_device = armpmu->plat_device;
	plat = dev_get_platdata(&plat_device->dev);
309

310 311 312 313
	if (plat && plat->handle_irq)
		return plat->handle_irq(irq, dev, armpmu->handle_irq);
	else
		return armpmu->handle_irq(irq, dev);
314 315
}

316
static void
317
armpmu_release_hardware(struct arm_pmu *armpmu)
318
{
319
	armpmu->free_irq(armpmu);
320
	pm_runtime_put_sync(&armpmu->plat_device->dev);
321 322
}

323
static int
324
armpmu_reserve_hardware(struct arm_pmu *armpmu)
325
{
326
	int err;
327
	struct platform_device *pmu_device = armpmu->plat_device;
328

329 330 331
	if (!pmu_device)
		return -ENODEV;

J
Jon Hunter 已提交
332
	pm_runtime_get_sync(&pmu_device->dev);
333
	err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
334 335 336
	if (err) {
		armpmu_release_hardware(armpmu);
		return err;
337
	}
338

339
	return 0;
340 341 342 343 344
}

static void
hw_perf_event_destroy(struct perf_event *event)
{
345
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
346 347 348 349
	atomic_t *active_events	 = &armpmu->active_events;
	struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;

	if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
350
		armpmu_release_hardware(armpmu);
351
		mutex_unlock(pmu_reserve_mutex);
352 353 354
	}
}

355 356 357 358 359 360 361
static int
event_requires_mode_exclusion(struct perf_event_attr *attr)
{
	return attr->exclude_idle || attr->exclude_user ||
	       attr->exclude_kernel || attr->exclude_hv;
}

362 363 364
static int
__hw_perf_event_init(struct perf_event *event)
{
365
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
366
	struct hw_perf_event *hwc = &event->hw;
367
	int mapping;
368

M
Mark Rutland 已提交
369
	mapping = armpmu->map_event(event);
370 371 372 373 374 375 376

	if (mapping < 0) {
		pr_debug("event %x:%llx not supported\n", event->attr.type,
			 event->attr.config);
		return mapping;
	}

377 378 379 380 381 382 383 384 385 386 387
	/*
	 * We don't assign an index until we actually place the event onto
	 * hardware. Use -1 to signify that we haven't decided where to put it
	 * yet. For SMP systems, each core has it's own PMU so we can't do any
	 * clever allocation or constraints checking at this point.
	 */
	hwc->idx		= -1;
	hwc->config_base	= 0;
	hwc->config		= 0;
	hwc->event_base		= 0;

388 389 390
	/*
	 * Check whether we need to exclude the counter from certain modes.
	 */
391 392 393
	if ((!armpmu->set_event_filter ||
	     armpmu->set_event_filter(hwc, &event->attr)) &&
	     event_requires_mode_exclusion(&event->attr)) {
394 395
		pr_debug("ARM performance counters do not support "
			 "mode exclusion\n");
396
		return -EOPNOTSUPP;
397 398 399
	}

	/*
400
	 * Store the event encoding into the config_base field.
401
	 */
402
	hwc->config_base	    |= (unsigned long)mapping;
403 404

	if (!hwc->sample_period) {
405 406 407 408 409 410 411
		/*
		 * For non-sampling runs, limit the sample_period to half
		 * of the counter width. That way, the new counter value
		 * is far less likely to overtake the previous one unless
		 * you have some serious IRQ latency issues.
		 */
		hwc->sample_period  = armpmu->max_period >> 1;
412
		hwc->last_period    = hwc->sample_period;
413
		local64_set(&hwc->period_left, hwc->sample_period);
414 415 416
	}

	if (event->group_leader != event) {
417
		if (validate_group(event) != 0)
418 419 420
			return -EINVAL;
	}

421
	return 0;
422 423
}

424
static int armpmu_event_init(struct perf_event *event)
425
{
426
	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
427
	int err = 0;
428
	atomic_t *active_events = &armpmu->active_events;
429

430 431 432 433
	/* does not support taken branch sampling */
	if (has_branch_stack(event))
		return -EOPNOTSUPP;

M
Mark Rutland 已提交
434
	if (armpmu->map_event(event) == -ENOENT)
435 436
		return -ENOENT;

437 438
	event->destroy = hw_perf_event_destroy;

439 440 441
	if (!atomic_inc_not_zero(active_events)) {
		mutex_lock(&armpmu->reserve_mutex);
		if (atomic_read(active_events) == 0)
442
			err = armpmu_reserve_hardware(armpmu);
443 444

		if (!err)
445 446
			atomic_inc(active_events);
		mutex_unlock(&armpmu->reserve_mutex);
447 448 449
	}

	if (err)
450
		return err;
451 452 453 454 455

	err = __hw_perf_event_init(event);
	if (err)
		hw_perf_event_destroy(event);

456
	return err;
457 458
}

P
Peter Zijlstra 已提交
459
static void armpmu_enable(struct pmu *pmu)
460
{
461 462
	struct arm_pmu *armpmu = to_arm_pmu(pmu);
	struct pmu_hw_events *hw_events = armpmu->get_hw_events();
463
	int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
464

465
	if (enabled)
466
		armpmu->start(armpmu);
467 468
}

P
Peter Zijlstra 已提交
469
static void armpmu_disable(struct pmu *pmu)
470
{
471
	struct arm_pmu *armpmu = to_arm_pmu(pmu);
472
	armpmu->stop(armpmu);
473 474
}

J
Jon Hunter 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#ifdef CONFIG_PM_RUNTIME
static int armpmu_runtime_resume(struct device *dev)
{
	struct arm_pmu_platdata *plat = dev_get_platdata(dev);

	if (plat && plat->runtime_resume)
		return plat->runtime_resume(dev);

	return 0;
}

static int armpmu_runtime_suspend(struct device *dev)
{
	struct arm_pmu_platdata *plat = dev_get_platdata(dev);

	if (plat && plat->runtime_suspend)
		return plat->runtime_suspend(dev);

	return 0;
}
#endif

497 498 499 500
const struct dev_pm_ops armpmu_dev_pm_ops = {
	SET_RUNTIME_PM_OPS(armpmu_runtime_suspend, armpmu_runtime_resume, NULL)
};

501
static void armpmu_init(struct arm_pmu *armpmu)
502 503 504
{
	atomic_set(&armpmu->active_events, 0);
	mutex_init(&armpmu->reserve_mutex);
505 506 507 508 509 510 511 512 513 514 515 516 517

	armpmu->pmu = (struct pmu) {
		.pmu_enable	= armpmu_enable,
		.pmu_disable	= armpmu_disable,
		.event_init	= armpmu_event_init,
		.add		= armpmu_add,
		.del		= armpmu_del,
		.start		= armpmu_start,
		.stop		= armpmu_stop,
		.read		= armpmu_read,
	};
}

518
int armpmu_register(struct arm_pmu *armpmu, int type)
519 520
{
	armpmu_init(armpmu);
J
Jon Hunter 已提交
521
	pm_runtime_enable(&armpmu->plat_device->dev);
522 523
	pr_info("enabled with %s PMU driver, %d counters available\n",
			armpmu->name, armpmu->num_events);
524
	return perf_pmu_register(&armpmu->pmu, armpmu->name, type);
525 526
}

527 528 529 530 531 532 533 534 535 536 537 538 539
/*
 * Callchain handling code.
 */

/*
 * The registers we're interested in are at the end of the variable
 * length saved register structure. The fp points at the end of this
 * structure so the address of this struct is:
 * (struct frame_tail *)(xxx->fp)-1
 *
 * This code has been adapted from the ARM OProfile support.
 */
struct frame_tail {
540 541 542
	struct frame_tail __user *fp;
	unsigned long sp;
	unsigned long lr;
543 544 545 546 547 548
} __attribute__((packed));

/*
 * Get the return address for a single stackframe and return a pointer to the
 * next frame tail.
 */
549 550
static struct frame_tail __user *
user_backtrace(struct frame_tail __user *tail,
551 552 553 554 555 556 557 558 559 560
	       struct perf_callchain_entry *entry)
{
	struct frame_tail buftail;

	/* Also check accessibility of one struct frame_tail beyond */
	if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
		return NULL;
	if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
		return NULL;

561
	perf_callchain_store(entry, buftail.lr);
562 563 564 565 566

	/*
	 * Frame pointers should strictly progress back up the stack
	 * (towards higher addresses).
	 */
567
	if (tail + 1 >= buftail.fp)
568 569 570 571 572
		return NULL;

	return buftail.fp - 1;
}

573 574
void
perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
575
{
576
	struct frame_tail __user *tail;
577

578 579 580 581
	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
		/* We don't support guest os callchain now */
		return;
	}
582

583
	perf_callchain_store(entry, regs->ARM_pc);
584
	tail = (struct frame_tail __user *)regs->ARM_fp - 1;
585

586 587
	while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
	       tail && !((unsigned long)tail & 0x3))
588 589 590 591 592 593 594 595 596 597 598 599 600
		tail = user_backtrace(tail, entry);
}

/*
 * Gets called by walk_stackframe() for every stackframe. This will be called
 * whist unwinding the stackframe and is like a subroutine return so we use
 * the PC.
 */
static int
callchain_trace(struct stackframe *fr,
		void *data)
{
	struct perf_callchain_entry *entry = data;
601
	perf_callchain_store(entry, fr->pc);
602 603 604
	return 0;
}

605 606
void
perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
607 608 609
{
	struct stackframe fr;

610 611 612 613 614
	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
		/* We don't support guest os callchain now */
		return;
	}

615 616 617 618 619 620
	fr.fp = regs->ARM_fp;
	fr.sp = regs->ARM_sp;
	fr.lr = regs->ARM_lr;
	fr.pc = regs->ARM_pc;
	walk_stackframe(&fr, callchain_trace, entry);
}
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

unsigned long perf_instruction_pointer(struct pt_regs *regs)
{
	if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
		return perf_guest_cbs->get_guest_ip();

	return instruction_pointer(regs);
}

unsigned long perf_misc_flags(struct pt_regs *regs)
{
	int misc = 0;

	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
		if (perf_guest_cbs->is_user_mode())
			misc |= PERF_RECORD_MISC_GUEST_USER;
		else
			misc |= PERF_RECORD_MISC_GUEST_KERNEL;
	} else {
		if (user_mode(regs))
			misc |= PERF_RECORD_MISC_USER;
		else
			misc |= PERF_RECORD_MISC_KERNEL;
	}

	return misc;
}