blob: 915b876edd1e2b8a509786446642302648989a73 [file] [log] [blame]
Ingo Molnar241771e2008-12-03 10:39:53 +01001/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002 * Performance events x86 architecture code
Ingo Molnar241771e2008-12-03 10:39:53 +01003 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Markus Metzger30dd5682009-07-21 15:56:48 +02009 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
Stephane Eranian1da53e02010-01-18 10:58:01 +020010 * Copyright (C) 2009 Google, Inc., Stephane Eranian
Ingo Molnar241771e2008-12-03 10:39:53 +010011 *
12 * For licencing details see kernel-base/COPYING
13 */
14
Ingo Molnarcdd6c482009-09-21 12:02:48 +020015#include <linux/perf_event.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010016#include <linux/capability.h>
17#include <linux/notifier.h>
18#include <linux/hardirq.h>
19#include <linux/kprobes.h>
Thomas Gleixner4ac13292008-12-09 21:43:39 +010020#include <linux/module.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010021#include <linux/kdebug.h>
22#include <linux/sched.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020023#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Markus Metzger30dd5682009-07-21 15:56:48 +020025#include <linux/cpu.h>
Peter Zijlstra272d30b2010-01-22 16:32:17 +010026#include <linux/bitops.h>
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +010027#include <linux/device.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010028
Ingo Molnar241771e2008-12-03 10:39:53 +010029#include <asm/apic.h>
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +020030#include <asm/stacktrace.h>
Peter Zijlstra4e935e42009-03-30 19:07:16 +020031#include <asm/nmi.h>
Lin Ming69092622011-03-03 10:34:50 +080032#include <asm/smp.h>
Robert Richterc8e59102011-04-16 02:27:55 +020033#include <asm/alternative.h>
Peter Zijlstrae3f35412011-11-21 11:43:53 +010034#include <asm/timer.h>
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +020035#include <asm/desc.h>
36#include <asm/ldt.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010037
Kevin Winchesterde0428a2011-08-30 20:41:05 -030038#include "perf_event.h"
39
Kevin Winchesterde0428a2011-08-30 20:41:05 -030040struct x86_pmu x86_pmu __read_mostly;
Stephane Eranianefc9f052011-06-06 16:57:03 +020041
Kevin Winchesterde0428a2011-08-30 20:41:05 -030042DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010043 .enabled = 1,
44};
Ingo Molnar241771e2008-12-03 10:39:53 +010045
Kevin Winchesterde0428a2011-08-30 20:41:05 -030046u64 __read_mostly hw_cache_event_ids
Ingo Molnar8326f442009-06-05 20:22:46 +020047 [PERF_COUNT_HW_CACHE_MAX]
48 [PERF_COUNT_HW_CACHE_OP_MAX]
49 [PERF_COUNT_HW_CACHE_RESULT_MAX];
Kevin Winchesterde0428a2011-08-30 20:41:05 -030050u64 __read_mostly hw_cache_extra_regs
Andi Kleene994d7d2011-03-03 10:34:48 +080051 [PERF_COUNT_HW_CACHE_MAX]
52 [PERF_COUNT_HW_CACHE_OP_MAX]
53 [PERF_COUNT_HW_CACHE_RESULT_MAX];
Ingo Molnar8326f442009-06-05 20:22:46 +020054
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +053055/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056 * Propagate event elapsed time into the generic event.
57 * Can only be executed on the CPU where the event is active.
Ingo Molnaree060942008-12-13 09:00:03 +010058 * Returns the delta events processed.
59 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030060u64 x86_perf_event_update(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +010061{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +010062 struct hw_perf_event *hwc = &event->hw;
Robert Richter948b1bb2010-03-29 18:36:50 +020063 int shift = 64 - x86_pmu.cntval_bits;
Peter Zijlstraec3232b2009-05-13 09:45:19 +020064 u64 prev_raw_count, new_raw_count;
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +010065 int idx = hwc->idx;
Peter Zijlstraec3232b2009-05-13 09:45:19 +020066 s64 delta;
Ingo Molnaree060942008-12-13 09:00:03 +010067
Robert Richter15c7ad52012-06-20 20:46:33 +020068 if (idx == INTEL_PMC_IDX_FIXED_BTS)
Markus Metzger30dd5682009-07-21 15:56:48 +020069 return 0;
70
Ingo Molnaree060942008-12-13 09:00:03 +010071 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020072 * Careful: an NMI might modify the previous event value.
Ingo Molnaree060942008-12-13 09:00:03 +010073 *
74 * Our tactic to handle this is to first atomically read and
75 * exchange a new raw count - then add that new-prev delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +020076 * count to the generic event atomically:
Ingo Molnaree060942008-12-13 09:00:03 +010077 */
78again:
Peter Zijlstrae7850592010-05-21 14:43:08 +020079 prev_raw_count = local64_read(&hwc->prev_count);
Vince Weaverc48b6052012-03-01 17:28:14 -050080 rdpmcl(hwc->event_base_rdpmc, new_raw_count);
Ingo Molnaree060942008-12-13 09:00:03 +010081
Peter Zijlstrae7850592010-05-21 14:43:08 +020082 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Ingo Molnaree060942008-12-13 09:00:03 +010083 new_raw_count) != prev_raw_count)
84 goto again;
85
86 /*
87 * Now we have the new raw value and have updated the prev
88 * timestamp already. We can now calculate the elapsed delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +020089 * (event-)time and add that to the generic event.
Ingo Molnaree060942008-12-13 09:00:03 +010090 *
91 * Careful, not all hw sign-extends above the physical width
Peter Zijlstraec3232b2009-05-13 09:45:19 +020092 * of the count.
Ingo Molnaree060942008-12-13 09:00:03 +010093 */
Peter Zijlstraec3232b2009-05-13 09:45:19 +020094 delta = (new_raw_count << shift) - (prev_raw_count << shift);
95 delta >>= shift;
Ingo Molnaree060942008-12-13 09:00:03 +010096
Peter Zijlstrae7850592010-05-21 14:43:08 +020097 local64_add(delta, &event->count);
98 local64_sub(delta, &hwc->period_left);
Robert Richter4b7bfd02009-04-29 12:47:22 +020099
100 return new_raw_count;
Ingo Molnaree060942008-12-13 09:00:03 +0100101}
102
Andi Kleena7e3ed12011-03-03 10:34:47 +0800103/*
104 * Find and validate any extra registers to set up.
105 */
106static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
107{
Stephane Eranianefc9f052011-06-06 16:57:03 +0200108 struct hw_perf_event_extra *reg;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800109 struct extra_reg *er;
110
Stephane Eranianefc9f052011-06-06 16:57:03 +0200111 reg = &event->hw.extra_reg;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800112
113 if (!x86_pmu.extra_regs)
114 return 0;
115
116 for (er = x86_pmu.extra_regs; er->msr; er++) {
117 if (er->event != (config & er->config_mask))
118 continue;
119 if (event->attr.config1 & ~er->valid_mask)
120 return -EINVAL;
Stephane Eranianefc9f052011-06-06 16:57:03 +0200121
122 reg->idx = er->idx;
123 reg->config = event->attr.config1;
124 reg->reg = er->msr;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800125 break;
126 }
127 return 0;
128}
129
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200130static atomic_t active_events;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200131static DEFINE_MUTEX(pmc_reserve_mutex);
132
Robert Richterb27ea292010-03-17 12:49:10 +0100133#ifdef CONFIG_X86_LOCAL_APIC
134
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200135static bool reserve_pmc_hardware(void)
136{
137 int i;
138
Robert Richter948b1bb2010-03-29 18:36:50 +0200139 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100140 if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200141 goto perfctr_fail;
142 }
143
Robert Richter948b1bb2010-03-29 18:36:50 +0200144 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100145 if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200146 goto eventsel_fail;
147 }
148
149 return true;
150
151eventsel_fail:
152 for (i--; i >= 0; i--)
Robert Richter41bf4982011-02-02 17:40:57 +0100153 release_evntsel_nmi(x86_pmu_config_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200154
Robert Richter948b1bb2010-03-29 18:36:50 +0200155 i = x86_pmu.num_counters;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200156
157perfctr_fail:
158 for (i--; i >= 0; i--)
Robert Richter41bf4982011-02-02 17:40:57 +0100159 release_perfctr_nmi(x86_pmu_event_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200160
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200161 return false;
162}
163
164static void release_pmc_hardware(void)
165{
166 int i;
167
Robert Richter948b1bb2010-03-29 18:36:50 +0200168 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100169 release_perfctr_nmi(x86_pmu_event_addr(i));
170 release_evntsel_nmi(x86_pmu_config_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200171 }
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200172}
173
Robert Richterb27ea292010-03-17 12:49:10 +0100174#else
175
176static bool reserve_pmc_hardware(void) { return true; }
177static void release_pmc_hardware(void) {}
178
179#endif
180
Don Zickus33c6d6a2010-11-22 16:55:23 -0500181static bool check_hw_exists(void)
182{
Robert Richterf285f922012-06-20 20:46:36 +0200183 u64 val, val_new = ~0;
Peter Zijlstra44072042010-12-08 15:56:23 +0100184 int i, reg, ret = 0;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500185
Peter Zijlstra44072042010-12-08 15:56:23 +0100186 /*
187 * Check to see if the BIOS enabled any of the counters, if so
188 * complain and bail.
189 */
190 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100191 reg = x86_pmu_config_addr(i);
Peter Zijlstra44072042010-12-08 15:56:23 +0100192 ret = rdmsrl_safe(reg, &val);
193 if (ret)
194 goto msr_fail;
195 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
196 goto bios_fail;
197 }
198
199 if (x86_pmu.num_counters_fixed) {
200 reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
201 ret = rdmsrl_safe(reg, &val);
202 if (ret)
203 goto msr_fail;
204 for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
205 if (val & (0x03 << i*4))
206 goto bios_fail;
207 }
208 }
209
210 /*
211 * Now write a value and read it back to see if it matches,
212 * this is needed to detect certain hardware emulators (qemu/kvm)
213 * that don't trap on the MSR access and always return 0s.
214 */
Don Zickus33c6d6a2010-11-22 16:55:23 -0500215 val = 0xabcdUL;
Robert Richterf285f922012-06-20 20:46:36 +0200216 reg = x86_pmu_event_addr(0);
217 ret = wrmsrl_safe(reg, val);
218 ret |= rdmsrl_safe(reg, &val_new);
Don Zickus33c6d6a2010-11-22 16:55:23 -0500219 if (ret || val != val_new)
Peter Zijlstra44072042010-12-08 15:56:23 +0100220 goto msr_fail;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500221
222 return true;
Peter Zijlstra44072042010-12-08 15:56:23 +0100223
224bios_fail:
Ingo Molnar45daae52011-03-25 10:24:23 +0100225 /*
226 * We still allow the PMU driver to operate:
227 */
228 printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n");
Peter Zijlstra44072042010-12-08 15:56:23 +0100229 printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
Ingo Molnar45daae52011-03-25 10:24:23 +0100230
231 return true;
Peter Zijlstra44072042010-12-08 15:56:23 +0100232
233msr_fail:
234 printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
Robert Richterf285f922012-06-20 20:46:36 +0200235 printk(KERN_ERR "Failed to access perfctr msr (MSR %x is %Lx)\n", reg, val_new);
Ingo Molnar45daae52011-03-25 10:24:23 +0100236
Peter Zijlstra44072042010-12-08 15:56:23 +0100237 return false;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500238}
239
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200240static void hw_perf_event_destroy(struct perf_event *event)
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200241{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200242 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200243 release_pmc_hardware();
Peter Zijlstraca037702010-03-02 19:52:12 +0100244 release_ds_buffers();
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200245 mutex_unlock(&pmc_reserve_mutex);
246 }
247}
248
Robert Richter85cf9db2009-04-29 12:47:20 +0200249static inline int x86_pmu_initialized(void)
250{
251 return x86_pmu.handle_irq != NULL;
252}
253
Ingo Molnar8326f442009-06-05 20:22:46 +0200254static inline int
Andi Kleene994d7d2011-03-03 10:34:48 +0800255set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
Ingo Molnar8326f442009-06-05 20:22:46 +0200256{
Andi Kleene994d7d2011-03-03 10:34:48 +0800257 struct perf_event_attr *attr = &event->attr;
Ingo Molnar8326f442009-06-05 20:22:46 +0200258 unsigned int cache_type, cache_op, cache_result;
259 u64 config, val;
260
261 config = attr->config;
262
263 cache_type = (config >> 0) & 0xff;
264 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
265 return -EINVAL;
266
267 cache_op = (config >> 8) & 0xff;
268 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
269 return -EINVAL;
270
271 cache_result = (config >> 16) & 0xff;
272 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
273 return -EINVAL;
274
275 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
276
277 if (val == 0)
278 return -ENOENT;
279
280 if (val == -1)
281 return -EINVAL;
282
283 hwc->config |= val;
Andi Kleene994d7d2011-03-03 10:34:48 +0800284 attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
285 return x86_pmu_extra_regs(val, event);
Ingo Molnar8326f442009-06-05 20:22:46 +0200286}
287
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300288int x86_setup_perfctr(struct perf_event *event)
Robert Richterc1726f32010-04-13 22:23:11 +0200289{
290 struct perf_event_attr *attr = &event->attr;
291 struct hw_perf_event *hwc = &event->hw;
292 u64 config;
293
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +0100294 if (!is_sampling_event(event)) {
Robert Richterc1726f32010-04-13 22:23:11 +0200295 hwc->sample_period = x86_pmu.max_period;
296 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200297 local64_set(&hwc->period_left, hwc->sample_period);
Robert Richterc1726f32010-04-13 22:23:11 +0200298 } else {
299 /*
300 * If we have a PMU initialized but no APIC
301 * interrupts, we cannot sample hardware
302 * events (user-space has to fall back and
303 * sample via a hrtimer based software event):
304 */
305 if (!x86_pmu.apic)
306 return -EOPNOTSUPP;
307 }
308
309 if (attr->type == PERF_TYPE_RAW)
Peter Zijlstraed13ec52011-11-14 10:03:25 +0100310 return x86_pmu_extra_regs(event->attr.config, event);
Robert Richterc1726f32010-04-13 22:23:11 +0200311
312 if (attr->type == PERF_TYPE_HW_CACHE)
Andi Kleene994d7d2011-03-03 10:34:48 +0800313 return set_ext_hw_attr(hwc, event);
Robert Richterc1726f32010-04-13 22:23:11 +0200314
315 if (attr->config >= x86_pmu.max_events)
316 return -EINVAL;
317
318 /*
319 * The generic map:
320 */
321 config = x86_pmu.event_map(attr->config);
322
323 if (config == 0)
324 return -ENOENT;
325
326 if (config == -1LL)
327 return -EINVAL;
328
329 /*
330 * Branch tracing:
331 */
Peter Zijlstra18a073a2011-04-26 13:24:33 +0200332 if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
333 !attr->freq && hwc->sample_period == 1) {
Robert Richterc1726f32010-04-13 22:23:11 +0200334 /* BTS is not supported by this architecture. */
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200335 if (!x86_pmu.bts_active)
Robert Richterc1726f32010-04-13 22:23:11 +0200336 return -EOPNOTSUPP;
337
338 /* BTS is currently only allowed for user-mode. */
339 if (!attr->exclude_kernel)
340 return -EOPNOTSUPP;
341 }
342
343 hwc->config |= config;
344
345 return 0;
346}
Robert Richter4261e0e2010-04-13 22:23:10 +0200347
Stephane Eranianff3fb512012-02-09 23:20:54 +0100348/*
349 * check that branch_sample_type is compatible with
350 * settings needed for precise_ip > 1 which implies
351 * using the LBR to capture ALL taken branches at the
352 * priv levels of the measurement
353 */
354static inline int precise_br_compat(struct perf_event *event)
355{
356 u64 m = event->attr.branch_sample_type;
357 u64 b = 0;
358
359 /* must capture all branches */
360 if (!(m & PERF_SAMPLE_BRANCH_ANY))
361 return 0;
362
363 m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER;
364
365 if (!event->attr.exclude_user)
366 b |= PERF_SAMPLE_BRANCH_USER;
367
368 if (!event->attr.exclude_kernel)
369 b |= PERF_SAMPLE_BRANCH_KERNEL;
370
371 /*
372 * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86
373 */
374
375 return m == b;
376}
377
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300378int x86_pmu_hw_config(struct perf_event *event)
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300379{
Peter Zijlstraab608342010-04-08 23:03:20 +0200380 if (event->attr.precise_ip) {
381 int precise = 0;
382
383 /* Support for constant skid */
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200384 if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200385 precise++;
386
Peter Zijlstra5553be22010-10-19 14:38:11 +0200387 /* Support for IP fixup */
388 if (x86_pmu.lbr_nr)
389 precise++;
390 }
Peter Zijlstraab608342010-04-08 23:03:20 +0200391
392 if (event->attr.precise_ip > precise)
393 return -EOPNOTSUPP;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100394 /*
395 * check that PEBS LBR correction does not conflict with
396 * whatever the user is asking with attr->branch_sample_type
397 */
398 if (event->attr.precise_ip > 1) {
399 u64 *br_type = &event->attr.branch_sample_type;
400
401 if (has_branch_stack(event)) {
402 if (!precise_br_compat(event))
403 return -EOPNOTSUPP;
404
405 /* branch_sample_type is compatible */
406
407 } else {
408 /*
409 * user did not specify branch_sample_type
410 *
411 * For PEBS fixups, we capture all
412 * the branches at the priv level of the
413 * event.
414 */
415 *br_type = PERF_SAMPLE_BRANCH_ANY;
416
417 if (!event->attr.exclude_user)
418 *br_type |= PERF_SAMPLE_BRANCH_USER;
419
420 if (!event->attr.exclude_kernel)
421 *br_type |= PERF_SAMPLE_BRANCH_KERNEL;
422 }
423 }
Peter Zijlstraab608342010-04-08 23:03:20 +0200424 }
425
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300426 /*
427 * Generate PMC IRQs:
428 * (keep 'enabled' bit clear for now)
429 */
Peter Zijlstrab4cdc5c2010-03-30 17:00:06 +0200430 event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300431
432 /*
433 * Count user and OS events unless requested not to
434 */
Peter Zijlstrab4cdc5c2010-03-30 17:00:06 +0200435 if (!event->attr.exclude_user)
436 event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
437 if (!event->attr.exclude_kernel)
438 event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
439
440 if (event->attr.type == PERF_TYPE_RAW)
441 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300442
Robert Richter9d0fcba62010-04-13 22:23:12 +0200443 return x86_setup_perfctr(event);
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300444}
445
Ingo Molnaree060942008-12-13 09:00:03 +0100446/*
Peter Zijlstra0d486962009-06-02 19:22:16 +0200447 * Setup the hardware configuration for a given attr_type
Ingo Molnar241771e2008-12-03 10:39:53 +0100448 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200449static int __x86_pmu_event_init(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +0100450{
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200451 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100452
Robert Richter85cf9db2009-04-29 12:47:20 +0200453 if (!x86_pmu_initialized())
454 return -ENODEV;
Ingo Molnar241771e2008-12-03 10:39:53 +0100455
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200456 err = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200457 if (!atomic_inc_not_zero(&active_events)) {
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200458 mutex_lock(&pmc_reserve_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200459 if (atomic_read(&active_events) == 0) {
Markus Metzger30dd5682009-07-21 15:56:48 +0200460 if (!reserve_pmc_hardware())
461 err = -EBUSY;
Peter Zijlstraf80c9e32010-10-19 14:50:02 +0200462 else
463 reserve_ds_buffers();
Markus Metzger30dd5682009-07-21 15:56:48 +0200464 }
465 if (!err)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466 atomic_inc(&active_events);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200467 mutex_unlock(&pmc_reserve_mutex);
468 }
469 if (err)
470 return err;
471
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200472 event->destroy = hw_perf_event_destroy;
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +0200473
Robert Richter4261e0e2010-04-13 22:23:10 +0200474 event->hw.idx = -1;
475 event->hw.last_cpu = -1;
476 event->hw.last_tag = ~0ULL;
Stephane Eranianb6900812009-10-06 16:42:09 +0200477
Stephane Eranianefc9f052011-06-06 16:57:03 +0200478 /* mark unused */
479 event->hw.extra_reg.idx = EXTRA_REG_NONE;
Stephane Eranianb36817e2012-02-09 23:20:53 +0100480 event->hw.branch_reg.idx = EXTRA_REG_NONE;
481
Robert Richter9d0fcba62010-04-13 22:23:12 +0200482 return x86_pmu.hw_config(event);
Robert Richter4261e0e2010-04-13 22:23:10 +0200483}
484
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300485void x86_pmu_disable_all(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530486{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200487 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200488 int idx;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100489
Robert Richter948b1bb2010-03-29 18:36:50 +0200490 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100491 u64 val;
492
Robert Richter43f62012009-04-29 16:55:56 +0200493 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200494 continue;
Robert Richter41bf4982011-02-02 17:40:57 +0100495 rdmsrl(x86_pmu_config_addr(idx), val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100496 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
Robert Richter4295ee62009-04-29 12:47:01 +0200497 continue;
Robert Richterbb1165d2010-03-01 14:21:23 +0100498 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richter41bf4982011-02-02 17:40:57 +0100499 wrmsrl(x86_pmu_config_addr(idx), val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530500 }
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530501}
502
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200503static void x86_pmu_disable(struct pmu *pmu)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530504{
Stephane Eranian1da53e02010-01-18 10:58:01 +0200505 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
506
Robert Richter85cf9db2009-04-29 12:47:20 +0200507 if (!x86_pmu_initialized())
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200508 return;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200509
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100510 if (!cpuc->enabled)
511 return;
512
513 cpuc->n_added = 0;
514 cpuc->enabled = 0;
515 barrier();
Stephane Eranian1da53e02010-01-18 10:58:01 +0200516
517 x86_pmu.disable_all();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530518}
Ingo Molnar241771e2008-12-03 10:39:53 +0100519
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300520void x86_pmu_enable_all(int added)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530521{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200522 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530523 int idx;
524
Robert Richter948b1bb2010-03-29 18:36:50 +0200525 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richterd45dd922011-02-02 17:40:56 +0100526 struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100527
Robert Richter43f62012009-04-29 16:55:56 +0200528 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200529 continue;
Peter Zijlstra984b8382009-07-10 09:59:56 +0200530
Robert Richterd45dd922011-02-02 17:40:56 +0100531 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530532 }
533}
534
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200535static struct pmu pmu;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200536
537static inline int is_x86_event(struct perf_event *event)
538{
539 return event->pmu == &pmu;
540}
541
Robert Richter1e2ad282011-11-18 12:35:21 +0100542/*
543 * Event scheduler state:
544 *
545 * Assign events iterating over all events and counters, beginning
546 * with events with least weights first. Keep the current iterator
547 * state in struct sched_state.
548 */
549struct sched_state {
550 int weight;
551 int event; /* event index */
552 int counter; /* counter index */
553 int unassigned; /* number of events to be assigned left */
554 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
555};
556
Robert Richterbc1738f2011-11-18 12:35:22 +0100557/* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */
558#define SCHED_STATES_MAX 2
559
Robert Richter1e2ad282011-11-18 12:35:21 +0100560struct perf_sched {
561 int max_weight;
562 int max_events;
563 struct event_constraint **constraints;
564 struct sched_state state;
Robert Richterbc1738f2011-11-18 12:35:22 +0100565 int saved_states;
566 struct sched_state saved[SCHED_STATES_MAX];
Robert Richter1e2ad282011-11-18 12:35:21 +0100567};
568
569/*
570 * Initialize interator that runs through all events and counters.
571 */
572static void perf_sched_init(struct perf_sched *sched, struct event_constraint **c,
573 int num, int wmin, int wmax)
574{
575 int idx;
576
577 memset(sched, 0, sizeof(*sched));
578 sched->max_events = num;
579 sched->max_weight = wmax;
580 sched->constraints = c;
581
582 for (idx = 0; idx < num; idx++) {
583 if (c[idx]->weight == wmin)
584 break;
585 }
586
587 sched->state.event = idx; /* start with min weight */
588 sched->state.weight = wmin;
589 sched->state.unassigned = num;
590}
591
Robert Richterbc1738f2011-11-18 12:35:22 +0100592static void perf_sched_save_state(struct perf_sched *sched)
593{
594 if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX))
595 return;
596
597 sched->saved[sched->saved_states] = sched->state;
598 sched->saved_states++;
599}
600
601static bool perf_sched_restore_state(struct perf_sched *sched)
602{
603 if (!sched->saved_states)
604 return false;
605
606 sched->saved_states--;
607 sched->state = sched->saved[sched->saved_states];
608
609 /* continue with next counter: */
610 clear_bit(sched->state.counter++, sched->state.used);
611
612 return true;
613}
614
Robert Richter1e2ad282011-11-18 12:35:21 +0100615/*
616 * Select a counter for the current event to schedule. Return true on
617 * success.
618 */
Robert Richterbc1738f2011-11-18 12:35:22 +0100619static bool __perf_sched_find_counter(struct perf_sched *sched)
Robert Richter1e2ad282011-11-18 12:35:21 +0100620{
621 struct event_constraint *c;
622 int idx;
623
624 if (!sched->state.unassigned)
625 return false;
626
627 if (sched->state.event >= sched->max_events)
628 return false;
629
630 c = sched->constraints[sched->state.event];
631
Peter Zijlstra4defea82011-11-10 15:15:42 +0100632 /* Prefer fixed purpose counters */
Robert Richter15c7ad52012-06-20 20:46:33 +0200633 if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) {
634 idx = INTEL_PMC_IDX_FIXED;
Akinobu Mita307b1cd2012-03-23 15:02:03 -0700635 for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) {
Peter Zijlstra4defea82011-11-10 15:15:42 +0100636 if (!__test_and_set_bit(idx, sched->state.used))
637 goto done;
638 }
639 }
Robert Richter1e2ad282011-11-18 12:35:21 +0100640 /* Grab the first unused counter starting with idx */
641 idx = sched->state.counter;
Robert Richter15c7ad52012-06-20 20:46:33 +0200642 for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) {
Robert Richter1e2ad282011-11-18 12:35:21 +0100643 if (!__test_and_set_bit(idx, sched->state.used))
Peter Zijlstra4defea82011-11-10 15:15:42 +0100644 goto done;
Robert Richter1e2ad282011-11-18 12:35:21 +0100645 }
Robert Richter1e2ad282011-11-18 12:35:21 +0100646
Peter Zijlstra4defea82011-11-10 15:15:42 +0100647 return false;
648
649done:
650 sched->state.counter = idx;
Robert Richter1e2ad282011-11-18 12:35:21 +0100651
Robert Richterbc1738f2011-11-18 12:35:22 +0100652 if (c->overlap)
653 perf_sched_save_state(sched);
654
655 return true;
656}
657
658static bool perf_sched_find_counter(struct perf_sched *sched)
659{
660 while (!__perf_sched_find_counter(sched)) {
661 if (!perf_sched_restore_state(sched))
662 return false;
663 }
664
Robert Richter1e2ad282011-11-18 12:35:21 +0100665 return true;
666}
667
668/*
669 * Go through all unassigned events and find the next one to schedule.
670 * Take events with the least weight first. Return true on success.
671 */
672static bool perf_sched_next_event(struct perf_sched *sched)
673{
674 struct event_constraint *c;
675
676 if (!sched->state.unassigned || !--sched->state.unassigned)
677 return false;
678
679 do {
680 /* next event */
681 sched->state.event++;
682 if (sched->state.event >= sched->max_events) {
683 /* next weight */
684 sched->state.event = 0;
685 sched->state.weight++;
686 if (sched->state.weight > sched->max_weight)
687 return false;
688 }
689 c = sched->constraints[sched->state.event];
690 } while (c->weight != sched->state.weight);
691
692 sched->state.counter = 0; /* start with first counter */
693
694 return true;
695}
696
697/*
698 * Assign a counter for each event.
699 */
Yan, Zheng4b4969b2012-06-15 14:31:30 +0800700int perf_assign_events(struct event_constraint **constraints, int n,
701 int wmin, int wmax, int *assign)
Robert Richter1e2ad282011-11-18 12:35:21 +0100702{
703 struct perf_sched sched;
704
705 perf_sched_init(&sched, constraints, n, wmin, wmax);
706
707 do {
708 if (!perf_sched_find_counter(&sched))
709 break; /* failed */
710 if (assign)
711 assign[sched.state.event] = sched.state.counter;
712 } while (perf_sched_next_event(&sched));
713
714 return sched.state.unassigned;
715}
716
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300717int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200718{
Peter Zijlstra63b14642010-01-22 16:32:17 +0100719 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200720 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Robert Richter1e2ad282011-11-18 12:35:21 +0100721 int i, wmin, wmax, num = 0;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200722 struct hw_perf_event *hwc;
723
724 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
725
Robert Richter1e2ad282011-11-18 12:35:21 +0100726 for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
Peter Zijlstrab622d642010-02-01 15:36:30 +0100727 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
728 constraints[i] = c;
Robert Richter1e2ad282011-11-18 12:35:21 +0100729 wmin = min(wmin, c->weight);
730 wmax = max(wmax, c->weight);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200731 }
732
733 /*
Stephane Eranian81130702010-01-21 17:39:01 +0200734 * fastpath, try to reuse previous register
735 */
Peter Zijlstrac933c1a2010-01-22 16:40:12 +0100736 for (i = 0; i < n; i++) {
Stephane Eranian81130702010-01-21 17:39:01 +0200737 hwc = &cpuc->event_list[i]->hw;
Peter Zijlstra81269a02010-01-22 14:55:22 +0100738 c = constraints[i];
Stephane Eranian81130702010-01-21 17:39:01 +0200739
740 /* never assigned */
741 if (hwc->idx == -1)
742 break;
743
744 /* constraint still honored */
Peter Zijlstra63b14642010-01-22 16:32:17 +0100745 if (!test_bit(hwc->idx, c->idxmsk))
Stephane Eranian81130702010-01-21 17:39:01 +0200746 break;
747
748 /* not already used */
749 if (test_bit(hwc->idx, used_mask))
750 break;
751
Peter Zijlstra34538ee2010-03-02 21:16:55 +0100752 __set_bit(hwc->idx, used_mask);
Stephane Eranian81130702010-01-21 17:39:01 +0200753 if (assign)
754 assign[i] = hwc->idx;
755 }
Stephane Eranian81130702010-01-21 17:39:01 +0200756
Robert Richter1e2ad282011-11-18 12:35:21 +0100757 /* slow path */
758 if (i != n)
759 num = perf_assign_events(constraints, n, wmin, wmax, assign);
Stephane Eranian81130702010-01-21 17:39:01 +0200760
Stephane Eranian1da53e02010-01-18 10:58:01 +0200761 /*
762 * scheduling failed or is just a simulation,
763 * free resources if necessary
764 */
765 if (!assign || num) {
766 for (i = 0; i < n; i++) {
767 if (x86_pmu.put_event_constraints)
768 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
769 }
770 }
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100771 return num ? -EINVAL : 0;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200772}
773
774/*
775 * dogrp: true if must collect siblings events (group)
776 * returns total number of events and error code
777 */
778static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
779{
780 struct perf_event *event;
781 int n, max_count;
782
Robert Richter948b1bb2010-03-29 18:36:50 +0200783 max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200784
785 /* current number of events already accepted */
786 n = cpuc->n_events;
787
788 if (is_x86_event(leader)) {
789 if (n >= max_count)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100790 return -EINVAL;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200791 cpuc->event_list[n] = leader;
792 n++;
793 }
794 if (!dogrp)
795 return n;
796
797 list_for_each_entry(event, &leader->sibling_list, group_entry) {
798 if (!is_x86_event(event) ||
Stephane Eranian81130702010-01-21 17:39:01 +0200799 event->state <= PERF_EVENT_STATE_OFF)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200800 continue;
801
802 if (n >= max_count)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100803 return -EINVAL;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200804
805 cpuc->event_list[n] = event;
806 n++;
807 }
808 return n;
809}
810
Stephane Eranian1da53e02010-01-18 10:58:01 +0200811static inline void x86_assign_hw_event(struct perf_event *event,
Stephane Eranian447a1942010-02-01 14:50:01 +0200812 struct cpu_hw_events *cpuc, int i)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200813{
Stephane Eranian447a1942010-02-01 14:50:01 +0200814 struct hw_perf_event *hwc = &event->hw;
815
816 hwc->idx = cpuc->assign[i];
817 hwc->last_cpu = smp_processor_id();
818 hwc->last_tag = ++cpuc->tags[i];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200819
Robert Richter15c7ad52012-06-20 20:46:33 +0200820 if (hwc->idx == INTEL_PMC_IDX_FIXED_BTS) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200821 hwc->config_base = 0;
822 hwc->event_base = 0;
Robert Richter15c7ad52012-06-20 20:46:33 +0200823 } else if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200824 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
Robert Richter15c7ad52012-06-20 20:46:33 +0200825 hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - INTEL_PMC_IDX_FIXED);
826 hwc->event_base_rdpmc = (hwc->idx - INTEL_PMC_IDX_FIXED) | 1<<30;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200827 } else {
Robert Richter73d6e522011-02-02 17:40:59 +0100828 hwc->config_base = x86_pmu_config_addr(hwc->idx);
829 hwc->event_base = x86_pmu_event_addr(hwc->idx);
Robert Richter76958a62012-06-15 19:06:44 +0200830 hwc->event_base_rdpmc = hwc->idx;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200831 }
832}
833
Stephane Eranian447a1942010-02-01 14:50:01 +0200834static inline int match_prev_assignment(struct hw_perf_event *hwc,
835 struct cpu_hw_events *cpuc,
836 int i)
837{
838 return hwc->idx == cpuc->assign[i] &&
839 hwc->last_cpu == smp_processor_id() &&
840 hwc->last_tag == cpuc->tags[i];
841}
842
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200843static void x86_pmu_start(struct perf_event *event, int flags);
Peter Zijlstra2e841872010-01-25 15:58:43 +0100844
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200845static void x86_pmu_enable(struct pmu *pmu)
Ingo Molnaree060942008-12-13 09:00:03 +0100846{
Stephane Eranian1da53e02010-01-18 10:58:01 +0200847 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
848 struct perf_event *event;
849 struct hw_perf_event *hwc;
Peter Zijlstra11164cd2010-03-26 14:08:44 +0100850 int i, added = cpuc->n_added;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200851
Robert Richter85cf9db2009-04-29 12:47:20 +0200852 if (!x86_pmu_initialized())
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +0100853 return;
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100854
855 if (cpuc->enabled)
856 return;
857
Stephane Eranian1da53e02010-01-18 10:58:01 +0200858 if (cpuc->n_added) {
Peter Zijlstra19925ce2010-03-06 13:20:40 +0100859 int n_running = cpuc->n_events - cpuc->n_added;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200860 /*
861 * apply assignment obtained either from
862 * hw_perf_group_sched_in() or x86_pmu_enable()
863 *
864 * step1: save events moving to new counters
865 * step2: reprogram moved events into new counters
866 */
Peter Zijlstra19925ce2010-03-06 13:20:40 +0100867 for (i = 0; i < n_running; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200868 event = cpuc->event_list[i];
869 hwc = &event->hw;
870
Stephane Eranian447a1942010-02-01 14:50:01 +0200871 /*
872 * we can avoid reprogramming counter if:
873 * - assigned same counter as last time
874 * - running on same CPU as last time
875 * - no other event has used the counter since
876 */
877 if (hwc->idx == -1 ||
878 match_prev_assignment(hwc, cpuc, i))
Stephane Eranian1da53e02010-01-18 10:58:01 +0200879 continue;
880
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200881 /*
882 * Ensure we don't accidentally enable a stopped
883 * counter simply because we rescheduled.
884 */
885 if (hwc->state & PERF_HES_STOPPED)
886 hwc->state |= PERF_HES_ARCH;
887
888 x86_pmu_stop(event, PERF_EF_UPDATE);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200889 }
890
891 for (i = 0; i < cpuc->n_events; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200892 event = cpuc->event_list[i];
893 hwc = &event->hw;
894
Peter Zijlstra45e16a62010-03-11 13:40:30 +0100895 if (!match_prev_assignment(hwc, cpuc, i))
Stephane Eranian447a1942010-02-01 14:50:01 +0200896 x86_assign_hw_event(event, cpuc, i);
Peter Zijlstra45e16a62010-03-11 13:40:30 +0100897 else if (i < n_running)
898 continue;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200899
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200900 if (hwc->state & PERF_HES_ARCH)
901 continue;
902
903 x86_pmu_start(event, PERF_EF_RELOAD);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200904 }
905 cpuc->n_added = 0;
906 perf_events_lapic_init();
907 }
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100908
909 cpuc->enabled = 1;
910 barrier();
911
Peter Zijlstra11164cd2010-03-26 14:08:44 +0100912 x86_pmu.enable_all(added);
Ingo Molnaree060942008-12-13 09:00:03 +0100913}
Ingo Molnaree060942008-12-13 09:00:03 +0100914
Tejun Heo245b2e72009-06-24 15:13:48 +0900915static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +0100916
Ingo Molnaree060942008-12-13 09:00:03 +0100917/*
918 * Set the next IRQ period, based on the hwc->period_left value.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200919 * To be called with the event disabled in hw:
Ingo Molnaree060942008-12-13 09:00:03 +0100920 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300921int x86_perf_event_set_period(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +0100922{
Peter Zijlstra07088ed2010-03-02 20:16:01 +0100923 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200924 s64 left = local64_read(&hwc->period_left);
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200925 s64 period = hwc->sample_period;
Peter Zijlstra7645a242010-03-08 13:51:31 +0100926 int ret = 0, idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +0100927
Robert Richter15c7ad52012-06-20 20:46:33 +0200928 if (idx == INTEL_PMC_IDX_FIXED_BTS)
Markus Metzger30dd5682009-07-21 15:56:48 +0200929 return 0;
930
Ingo Molnaree060942008-12-13 09:00:03 +0100931 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200932 * If we are way outside a reasonable range then just skip forward:
Ingo Molnaree060942008-12-13 09:00:03 +0100933 */
934 if (unlikely(left <= -period)) {
935 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200936 local64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200937 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200938 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +0100939 }
940
941 if (unlikely(left <= 0)) {
942 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200943 local64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200944 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200945 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +0100946 }
Ingo Molnar1c80f4b2009-05-15 08:25:22 +0200947 /*
Ingo Molnardfc65092009-09-21 11:31:35 +0200948 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
Ingo Molnar1c80f4b2009-05-15 08:25:22 +0200949 */
950 if (unlikely(left < 2))
951 left = 2;
Ingo Molnaree060942008-12-13 09:00:03 +0100952
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200953 if (left > x86_pmu.max_period)
954 left = x86_pmu.max_period;
955
Tejun Heo245b2e72009-06-24 15:13:48 +0900956 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
Ingo Molnaree060942008-12-13 09:00:03 +0100957
958 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200959 * The hw event starts counting from this event offset,
Ingo Molnaree060942008-12-13 09:00:03 +0100960 * mark it to be able to extra future deltas:
961 */
Peter Zijlstrae7850592010-05-21 14:43:08 +0200962 local64_set(&hwc->prev_count, (u64)-left);
Ingo Molnaree060942008-12-13 09:00:03 +0100963
Robert Richter73d6e522011-02-02 17:40:59 +0100964 wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
Cyrill Gorcunov68aa00a2010-06-03 01:23:04 +0400965
966 /*
967 * Due to erratum on certan cpu we need
968 * a second write to be sure the register
969 * is updated properly
970 */
971 if (x86_pmu.perfctr_second_write) {
Robert Richter73d6e522011-02-02 17:40:59 +0100972 wrmsrl(hwc->event_base,
Robert Richter948b1bb2010-03-29 18:36:50 +0200973 (u64)(-left) & x86_pmu.cntval_mask);
Cyrill Gorcunov68aa00a2010-06-03 01:23:04 +0400974 }
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200975
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200976 perf_event_update_userpage(event);
Peter Zijlstra194002b2009-06-22 16:35:24 +0200977
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +0200978 return ret;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +0100979}
980
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300981void x86_pmu_enable_event(struct perf_event *event)
Robert Richter7c90cc42009-04-29 12:47:18 +0200982{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100983 if (__this_cpu_read(cpu_hw_events.enabled))
Robert Richter31fa58a2010-04-13 22:23:14 +0200984 __x86_pmu_enable_event(&event->hw,
985 ARCH_PERFMON_EVENTSEL_ENABLE);
Ingo Molnar241771e2008-12-03 10:39:53 +0100986}
987
Ingo Molnaree060942008-12-13 09:00:03 +0100988/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200989 * Add a single event to the PMU.
Stephane Eranian1da53e02010-01-18 10:58:01 +0200990 *
991 * The event is added to the group of enabled events
992 * but only if it can be scehduled with existing events.
Peter Zijlstrafe9081c2009-10-08 11:56:07 +0200993 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200994static int x86_pmu_add(struct perf_event *event, int flags)
Peter Zijlstrafe9081c2009-10-08 11:56:07 +0200995{
996 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200997 struct hw_perf_event *hwc;
998 int assign[X86_PMC_IDX_MAX];
999 int n, n0, ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001000
Stephane Eranian1da53e02010-01-18 10:58:01 +02001001 hwc = &event->hw;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001002
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001003 perf_pmu_disable(event->pmu);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001004 n0 = cpuc->n_events;
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001005 ret = n = collect_events(cpuc, event, false);
1006 if (ret < 0)
1007 goto out;
Ingo Molnar53b441a2009-05-25 21:41:28 +02001008
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001009 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1010 if (!(flags & PERF_EF_START))
1011 hwc->state |= PERF_HES_ARCH;
1012
Lin Ming4d1c52b2010-04-23 13:56:12 +08001013 /*
1014 * If group events scheduling transaction was started,
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001015 * skip the schedulability test here, it will be performed
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001016 * at commit time (->commit_txn) as a whole
Lin Ming4d1c52b2010-04-23 13:56:12 +08001017 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001018 if (cpuc->group_flag & PERF_EVENT_TXN)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001019 goto done_collect;
Lin Ming4d1c52b2010-04-23 13:56:12 +08001020
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001021 ret = x86_pmu.schedule_events(cpuc, n, assign);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001022 if (ret)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001023 goto out;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001024 /*
1025 * copy new assignment, now we know it is possible
1026 * will be used by hw_perf_enable()
1027 */
1028 memcpy(cpuc->assign, assign, n*sizeof(int));
Ingo Molnar241771e2008-12-03 10:39:53 +01001029
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001030done_collect:
Stephane Eranian1da53e02010-01-18 10:58:01 +02001031 cpuc->n_events = n;
Peter Zijlstra356e1f22010-03-06 13:49:56 +01001032 cpuc->n_added += n - n0;
Stephane Eranian90151c352010-05-25 16:23:10 +02001033 cpuc->n_txn += n - n0;
Ingo Molnar7e2ae342008-12-09 11:40:46 +01001034
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001035 ret = 0;
1036out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001037 perf_pmu_enable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001038 return ret;
Ingo Molnar241771e2008-12-03 10:39:53 +01001039}
1040
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001041static void x86_pmu_start(struct perf_event *event, int flags)
Stephane Eraniand76a0812010-02-08 17:06:01 +02001042{
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001043 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1044 int idx = event->hw.idx;
1045
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001046 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1047 return;
Stephane Eraniand76a0812010-02-08 17:06:01 +02001048
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001049 if (WARN_ON_ONCE(idx == -1))
1050 return;
1051
1052 if (flags & PERF_EF_RELOAD) {
1053 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1054 x86_perf_event_set_period(event);
1055 }
1056
1057 event->hw.state = 0;
1058
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001059 cpuc->events[idx] = event;
1060 __set_bit(idx, cpuc->active_mask);
Robert Richter63e6be62010-09-15 18:20:34 +02001061 __set_bit(idx, cpuc->running);
Peter Zijlstraaff3d912010-03-02 20:32:08 +01001062 x86_pmu.enable(event);
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001063 perf_event_update_userpage(event);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001064}
1065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001066void perf_event_print_debug(void)
Ingo Molnar241771e2008-12-03 10:39:53 +01001067{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001068 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
Peter Zijlstraca037702010-03-02 19:52:12 +01001069 u64 pebs;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001070 struct cpu_hw_events *cpuc;
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001071 unsigned long flags;
Ingo Molnar1e125672008-12-09 12:18:18 +01001072 int cpu, idx;
1073
Robert Richter948b1bb2010-03-29 18:36:50 +02001074 if (!x86_pmu.num_counters)
Ingo Molnar1e125672008-12-09 12:18:18 +01001075 return;
Ingo Molnar241771e2008-12-03 10:39:53 +01001076
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001077 local_irq_save(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +01001078
1079 cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001080 cpuc = &per_cpu(cpu_hw_events, cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +01001081
Robert Richterfaa28ae2009-04-29 12:47:13 +02001082 if (x86_pmu.version >= 2) {
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301083 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1084 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1085 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1086 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
Peter Zijlstraca037702010-03-02 19:52:12 +01001087 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
Ingo Molnar241771e2008-12-03 10:39:53 +01001088
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301089 pr_info("\n");
1090 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1091 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1092 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1093 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
Peter Zijlstraca037702010-03-02 19:52:12 +01001094 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301095 }
Peter Zijlstra7645a242010-03-08 13:51:31 +01001096 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
Ingo Molnar241771e2008-12-03 10:39:53 +01001097
Robert Richter948b1bb2010-03-29 18:36:50 +02001098 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richter41bf4982011-02-02 17:40:57 +01001099 rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
1100 rdmsrl(x86_pmu_event_addr(idx), pmc_count);
Ingo Molnar241771e2008-12-03 10:39:53 +01001101
Tejun Heo245b2e72009-06-24 15:13:48 +09001102 prev_left = per_cpu(pmc_prev_left[idx], cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +01001103
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301104 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001105 cpu, idx, pmc_ctrl);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301106 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001107 cpu, idx, pmc_count);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301108 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
Ingo Molnaree060942008-12-13 09:00:03 +01001109 cpu, idx, prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +01001110 }
Robert Richter948b1bb2010-03-29 18:36:50 +02001111 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001112 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1113
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301114 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001115 cpu, idx, pmc_count);
1116 }
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001117 local_irq_restore(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +01001118}
1119
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001120void x86_pmu_stop(struct perf_event *event, int flags)
Ingo Molnar241771e2008-12-03 10:39:53 +01001121{
Stephane Eraniand76a0812010-02-08 17:06:01 +02001122 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001123 struct hw_perf_event *hwc = &event->hw;
Ingo Molnar241771e2008-12-03 10:39:53 +01001124
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001125 if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
1126 x86_pmu.disable(event);
1127 cpuc->events[hwc->idx] = NULL;
1128 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
1129 hwc->state |= PERF_HES_STOPPED;
1130 }
Peter Zijlstra71e2d282010-03-08 17:51:33 +01001131
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001132 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
1133 /*
1134 * Drain the remaining delta count out of a event
1135 * that we are disabling:
1136 */
1137 x86_perf_event_update(event);
1138 hwc->state |= PERF_HES_UPTODATE;
1139 }
Peter Zijlstra2e841872010-01-25 15:58:43 +01001140}
1141
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001142static void x86_pmu_del(struct perf_event *event, int flags)
Peter Zijlstra2e841872010-01-25 15:58:43 +01001143{
1144 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1145 int i;
1146
Stephane Eranian90151c352010-05-25 16:23:10 +02001147 /*
1148 * If we're called during a txn, we don't need to do anything.
1149 * The events never got scheduled and ->cancel_txn will truncate
1150 * the event_list.
1151 */
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001152 if (cpuc->group_flag & PERF_EVENT_TXN)
Stephane Eranian90151c352010-05-25 16:23:10 +02001153 return;
1154
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001155 x86_pmu_stop(event, PERF_EF_UPDATE);
Peter Zijlstra194002b2009-06-22 16:35:24 +02001156
Stephane Eranian1da53e02010-01-18 10:58:01 +02001157 for (i = 0; i < cpuc->n_events; i++) {
1158 if (event == cpuc->event_list[i]) {
1159
1160 if (x86_pmu.put_event_constraints)
1161 x86_pmu.put_event_constraints(cpuc, event);
1162
1163 while (++i < cpuc->n_events)
1164 cpuc->event_list[i-1] = cpuc->event_list[i];
1165
1166 --cpuc->n_events;
Peter Zijlstra6c9687a2010-01-25 11:57:25 +01001167 break;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001168 }
1169 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001170 perf_event_update_userpage(event);
Ingo Molnar241771e2008-12-03 10:39:53 +01001171}
1172
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001173int x86_pmu_handle_irq(struct pt_regs *regs)
Robert Richtera29aa8a2009-04-29 12:47:21 +02001174{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001175 struct perf_sample_data data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176 struct cpu_hw_events *cpuc;
1177 struct perf_event *event;
Vince Weaver11d15782009-07-08 17:46:14 -04001178 int idx, handled = 0;
Ingo Molnar9029a5e2009-05-15 08:26:20 +02001179 u64 val;
1180
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001181 cpuc = &__get_cpu_var(cpu_hw_events);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001182
Don Zickus2bce5da2011-04-27 06:32:33 -04001183 /*
1184 * Some chipsets need to unmask the LVTPC in a particular spot
1185 * inside the nmi handler. As a result, the unmasking was pushed
1186 * into all the nmi handlers.
1187 *
1188 * This generic handler doesn't seem to have any issues where the
1189 * unmasking occurs so it was left at the top.
1190 */
1191 apic_write(APIC_LVTPC, APIC_DM_NMI);
1192
Robert Richter948b1bb2010-03-29 18:36:50 +02001193 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richter63e6be62010-09-15 18:20:34 +02001194 if (!test_bit(idx, cpuc->active_mask)) {
1195 /*
1196 * Though we deactivated the counter some cpus
1197 * might still deliver spurious interrupts still
1198 * in flight. Catch them:
1199 */
1200 if (__test_and_clear_bit(idx, cpuc->running))
1201 handled++;
Robert Richtera29aa8a2009-04-29 12:47:21 +02001202 continue;
Robert Richter63e6be62010-09-15 18:20:34 +02001203 }
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 event = cpuc->events[idx];
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001206
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001207 val = x86_perf_event_update(event);
Robert Richter948b1bb2010-03-29 18:36:50 +02001208 if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
Peter Zijlstra48e22d52009-05-25 17:39:04 +02001209 continue;
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001210
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001211 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001212 * event overflow
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001213 */
Robert Richter4177c422010-09-02 15:07:48 -04001214 handled++;
Robert Richterfd0d0002012-04-02 20:19:08 +02001215 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001216
Peter Zijlstra07088ed2010-03-02 20:16:01 +01001217 if (!x86_perf_event_set_period(event))
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001218 continue;
1219
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001220 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001221 x86_pmu_stop(event, 0);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001222 }
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001223
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001224 if (handled)
1225 inc_irq_stat(apic_perf_irqs);
1226
Robert Richtera29aa8a2009-04-29 12:47:21 +02001227 return handled;
1228}
Robert Richter39d81ea2009-04-29 12:47:05 +02001229
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230void perf_events_lapic_init(void)
Ingo Molnar241771e2008-12-03 10:39:53 +01001231{
Ingo Molnar04da8a42009-08-11 10:40:08 +02001232 if (!x86_pmu.apic || !x86_pmu_initialized())
Ingo Molnar241771e2008-12-03 10:39:53 +01001233 return;
Robert Richter85cf9db2009-04-29 12:47:20 +02001234
Ingo Molnar241771e2008-12-03 10:39:53 +01001235 /*
Yong Wangc323d952009-05-29 13:28:35 +08001236 * Always use NMI for PMU
Ingo Molnar241771e2008-12-03 10:39:53 +01001237 */
Yong Wangc323d952009-05-29 13:28:35 +08001238 apic_write(APIC_LVTPC, APIC_DM_NMI);
Ingo Molnar241771e2008-12-03 10:39:53 +01001239}
1240
1241static int __kprobes
Don Zickus9c48f1c2011-09-30 15:06:21 -04001242perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar241771e2008-12-03 10:39:53 +01001243{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 if (!atomic_read(&active_events))
Don Zickus9c48f1c2011-09-30 15:06:21 -04001245 return NMI_DONE;
Peter Zijlstra63a809a2009-05-01 12:23:17 +02001246
Don Zickus9c48f1c2011-09-30 15:06:21 -04001247 return x86_pmu.handle_irq(regs);
Ingo Molnar241771e2008-12-03 10:39:53 +01001248}
1249
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001250struct event_constraint emptyconstraint;
1251struct event_constraint unconstrained;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301252
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001253static int __cpuinit
1254x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1255{
1256 unsigned int cpu = (long)hcpu;
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001257 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001258 int ret = NOTIFY_OK;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001259
1260 switch (action & ~CPU_TASKS_FROZEN) {
1261 case CPU_UP_PREPARE:
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001262 cpuc->kfree_on_online = NULL;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001263 if (x86_pmu.cpu_prepare)
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001264 ret = x86_pmu.cpu_prepare(cpu);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001265 break;
1266
1267 case CPU_STARTING:
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001268 if (x86_pmu.attr_rdpmc)
1269 set_in_cr4(X86_CR4_PCE);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001270 if (x86_pmu.cpu_starting)
1271 x86_pmu.cpu_starting(cpu);
1272 break;
1273
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001274 case CPU_ONLINE:
1275 kfree(cpuc->kfree_on_online);
1276 break;
1277
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001278 case CPU_DYING:
1279 if (x86_pmu.cpu_dying)
1280 x86_pmu.cpu_dying(cpu);
1281 break;
1282
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001283 case CPU_UP_CANCELED:
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001284 case CPU_DEAD:
1285 if (x86_pmu.cpu_dead)
1286 x86_pmu.cpu_dead(cpu);
1287 break;
1288
1289 default:
1290 break;
1291 }
1292
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001293 return ret;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001294}
1295
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001296static void __init pmu_check_apic(void)
1297{
1298 if (cpu_has_apic)
1299 return;
1300
1301 x86_pmu.apic = 0;
1302 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1303 pr_info("no hardware sampling interrupt available.\n");
1304}
1305
Jiri Olsa641cc932012-03-15 20:09:14 +01001306static struct attribute_group x86_pmu_format_group = {
1307 .name = "format",
1308 .attrs = NULL,
1309};
1310
Yinghai Ludda99112011-01-21 15:30:01 -08001311static int __init init_hw_perf_events(void)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301312{
Peter Zijlstrac1d6f422011-12-06 14:07:15 +01001313 struct x86_pmu_quirk *quirk;
Robert Richter72eae042009-04-29 12:47:10 +02001314 int err;
1315
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316 pr_info("Performance Events: ");
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001317
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301318 switch (boot_cpu_data.x86_vendor) {
1319 case X86_VENDOR_INTEL:
Robert Richter72eae042009-04-29 12:47:10 +02001320 err = intel_pmu_init();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301321 break;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301322 case X86_VENDOR_AMD:
Robert Richter72eae042009-04-29 12:47:10 +02001323 err = amd_pmu_init();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301324 break;
Robert Richter41389602009-04-29 12:47:00 +02001325 default:
Peter Zijlstra004417a2010-11-25 18:38:29 +01001326 return 0;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301327 }
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001328 if (err != 0) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329 pr_cont("no PMU driver, software events only.\n");
Peter Zijlstra004417a2010-11-25 18:38:29 +01001330 return 0;
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001331 }
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301332
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001333 pmu_check_apic();
1334
Don Zickus33c6d6a2010-11-22 16:55:23 -05001335 /* sanity check that the hardware exists or is emulated */
Peter Zijlstra44072042010-12-08 15:56:23 +01001336 if (!check_hw_exists())
Peter Zijlstra004417a2010-11-25 18:38:29 +01001337 return 0;
Don Zickus33c6d6a2010-11-22 16:55:23 -05001338
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001339 pr_cont("%s PMU driver.\n", x86_pmu.name);
Robert Richterfaa28ae2009-04-29 12:47:13 +02001340
Peter Zijlstrac1d6f422011-12-06 14:07:15 +01001341 for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
1342 quirk->func();
Peter Zijlstra3c447802010-03-04 21:49:01 +01001343
Robert Richtera1eac7a2012-06-20 20:46:34 +02001344 if (!x86_pmu.intel_ctrl)
1345 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
Ingo Molnar862a1a52008-12-17 13:09:20 +01001346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 perf_events_lapic_init();
Don Zickus9c48f1c2011-09-30 15:06:21 -04001348 register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001349
Peter Zijlstra63b14642010-01-22 16:32:17 +01001350 unconstrained = (struct event_constraint)
Robert Richter948b1bb2010-03-29 18:36:50 +02001351 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
Robert Richterbc1738f2011-11-18 12:35:22 +01001352 0, x86_pmu.num_counters, 0);
Peter Zijlstra63b14642010-01-22 16:32:17 +01001353
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001354 x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */
Jiri Olsa641cc932012-03-15 20:09:14 +01001355 x86_pmu_format_group.attrs = x86_pmu.format_attrs;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001356
Ingo Molnar57c0c152009-09-21 12:20:38 +02001357 pr_info("... version: %d\n", x86_pmu.version);
Robert Richter948b1bb2010-03-29 18:36:50 +02001358 pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
1359 pr_info("... generic registers: %d\n", x86_pmu.num_counters);
1360 pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
Ingo Molnar57c0c152009-09-21 12:20:38 +02001361 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
Robert Richter948b1bb2010-03-29 18:36:50 +02001362 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
Robert Richterd6dc0b42010-03-17 12:49:13 +01001363 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001364
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001365 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001366 perf_cpu_notifier(x86_pmu_notifier);
Peter Zijlstra004417a2010-11-25 18:38:29 +01001367
1368 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +01001369}
Peter Zijlstra004417a2010-11-25 18:38:29 +01001370early_initcall(init_hw_perf_events);
Ingo Molnar621a01e2008-12-11 12:46:46 +01001371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001372static inline void x86_pmu_read(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +01001373{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001374 x86_perf_event_update(event);
Ingo Molnaree060942008-12-13 09:00:03 +01001375}
1376
Lin Ming4d1c52b2010-04-23 13:56:12 +08001377/*
1378 * Start group events scheduling transaction
1379 * Set the flag to make pmu::enable() not perform the
1380 * schedulability test, it will be performed at commit time
1381 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001382static void x86_pmu_start_txn(struct pmu *pmu)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001383{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001384 perf_pmu_disable(pmu);
Tejun Heo0a3aee02010-12-18 16:28:55 +01001385 __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
1386 __this_cpu_write(cpu_hw_events.n_txn, 0);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001387}
1388
1389/*
1390 * Stop group events scheduling transaction
1391 * Clear the flag and pmu::enable() will perform the
1392 * schedulability test.
1393 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001394static void x86_pmu_cancel_txn(struct pmu *pmu)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001395{
Tejun Heo0a3aee02010-12-18 16:28:55 +01001396 __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
Stephane Eranian90151c352010-05-25 16:23:10 +02001397 /*
1398 * Truncate the collected events.
1399 */
Tejun Heo0a3aee02010-12-18 16:28:55 +01001400 __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
1401 __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001402 perf_pmu_enable(pmu);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001403}
1404
1405/*
1406 * Commit group events scheduling transaction
1407 * Perform the group schedulability test as a whole
1408 * Return 0 if success
1409 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001410static int x86_pmu_commit_txn(struct pmu *pmu)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001411{
1412 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1413 int assign[X86_PMC_IDX_MAX];
1414 int n, ret;
1415
1416 n = cpuc->n_events;
1417
1418 if (!x86_pmu_initialized())
1419 return -EAGAIN;
1420
1421 ret = x86_pmu.schedule_events(cpuc, n, assign);
1422 if (ret)
1423 return ret;
1424
1425 /*
1426 * copy new assignment, now we know it is possible
1427 * will be used by hw_perf_enable()
1428 */
1429 memcpy(cpuc->assign, assign, n*sizeof(int));
1430
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +02001431 cpuc->group_flag &= ~PERF_EVENT_TXN;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001432 perf_pmu_enable(pmu);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001433 return 0;
1434}
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001435/*
1436 * a fake_cpuc is used to validate event groups. Due to
1437 * the extra reg logic, we need to also allocate a fake
1438 * per_core and per_cpu structure. Otherwise, group events
1439 * using extra reg may conflict without the kernel being
1440 * able to catch this when the last event gets added to
1441 * the group.
1442 */
1443static void free_fake_cpuc(struct cpu_hw_events *cpuc)
1444{
1445 kfree(cpuc->shared_regs);
1446 kfree(cpuc);
1447}
1448
1449static struct cpu_hw_events *allocate_fake_cpuc(void)
1450{
1451 struct cpu_hw_events *cpuc;
1452 int cpu = raw_smp_processor_id();
1453
1454 cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
1455 if (!cpuc)
1456 return ERR_PTR(-ENOMEM);
1457
1458 /* only needed, if we have extra_regs */
1459 if (x86_pmu.extra_regs) {
1460 cpuc->shared_regs = allocate_shared_regs(cpu);
1461 if (!cpuc->shared_regs)
1462 goto error;
1463 }
Peter Zijlstrab430f7c2012-06-05 15:30:31 +02001464 cpuc->is_fake = 1;
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001465 return cpuc;
1466error:
1467 free_fake_cpuc(cpuc);
1468 return ERR_PTR(-ENOMEM);
1469}
Lin Ming4d1c52b2010-04-23 13:56:12 +08001470
Stephane Eranian1da53e02010-01-18 10:58:01 +02001471/*
Peter Zijlstraca037702010-03-02 19:52:12 +01001472 * validate that we can schedule this event
1473 */
1474static int validate_event(struct perf_event *event)
1475{
1476 struct cpu_hw_events *fake_cpuc;
1477 struct event_constraint *c;
1478 int ret = 0;
1479
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001480 fake_cpuc = allocate_fake_cpuc();
1481 if (IS_ERR(fake_cpuc))
1482 return PTR_ERR(fake_cpuc);
Peter Zijlstraca037702010-03-02 19:52:12 +01001483
1484 c = x86_pmu.get_event_constraints(fake_cpuc, event);
1485
1486 if (!c || !c->weight)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +01001487 ret = -EINVAL;
Peter Zijlstraca037702010-03-02 19:52:12 +01001488
1489 if (x86_pmu.put_event_constraints)
1490 x86_pmu.put_event_constraints(fake_cpuc, event);
1491
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001492 free_fake_cpuc(fake_cpuc);
Peter Zijlstraca037702010-03-02 19:52:12 +01001493
1494 return ret;
1495}
1496
1497/*
Stephane Eranian1da53e02010-01-18 10:58:01 +02001498 * validate a single event group
1499 *
1500 * validation include:
Ingo Molnar184f4122010-01-27 08:39:39 +01001501 * - check events are compatible which each other
1502 * - events do not compete for the same counter
1503 * - number of events <= number of counters
Stephane Eranian1da53e02010-01-18 10:58:01 +02001504 *
1505 * validation ensures the group can be loaded onto the
1506 * PMU if it was the only group available.
1507 */
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001508static int validate_group(struct perf_event *event)
1509{
Stephane Eranian1da53e02010-01-18 10:58:01 +02001510 struct perf_event *leader = event->group_leader;
Peter Zijlstra502568d2010-01-22 14:35:46 +01001511 struct cpu_hw_events *fake_cpuc;
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +01001512 int ret = -EINVAL, n;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001513
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001514 fake_cpuc = allocate_fake_cpuc();
1515 if (IS_ERR(fake_cpuc))
1516 return PTR_ERR(fake_cpuc);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001517 /*
1518 * the event is not yet connected with its
1519 * siblings therefore we must first collect
1520 * existing siblings, then add the new event
1521 * before we can simulate the scheduling
1522 */
Peter Zijlstra502568d2010-01-22 14:35:46 +01001523 n = collect_events(fake_cpuc, leader, true);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001524 if (n < 0)
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001525 goto out;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001526
Peter Zijlstra502568d2010-01-22 14:35:46 +01001527 fake_cpuc->n_events = n;
1528 n = collect_events(fake_cpuc, event, false);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001529 if (n < 0)
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001530 goto out;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001531
Peter Zijlstra502568d2010-01-22 14:35:46 +01001532 fake_cpuc->n_events = n;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001533
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001534 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
Peter Zijlstra502568d2010-01-22 14:35:46 +01001535
Peter Zijlstra502568d2010-01-22 14:35:46 +01001536out:
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001537 free_fake_cpuc(fake_cpuc);
Peter Zijlstra502568d2010-01-22 14:35:46 +01001538 return ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001539}
1540
Yinghai Ludda99112011-01-21 15:30:01 -08001541static int x86_pmu_event_init(struct perf_event *event)
Ingo Molnar621a01e2008-12-11 12:46:46 +01001542{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001543 struct pmu *tmp;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001544 int err;
1545
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001546 switch (event->attr.type) {
1547 case PERF_TYPE_RAW:
1548 case PERF_TYPE_HARDWARE:
1549 case PERF_TYPE_HW_CACHE:
1550 break;
1551
1552 default:
1553 return -ENOENT;
1554 }
1555
1556 err = __x86_pmu_event_init(event);
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001557 if (!err) {
Stephane Eranian81130702010-01-21 17:39:01 +02001558 /*
1559 * we temporarily connect event to its pmu
1560 * such that validate_group() can classify
1561 * it as an x86 event using is_x86_event()
1562 */
1563 tmp = event->pmu;
1564 event->pmu = &pmu;
1565
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001566 if (event->group_leader != event)
1567 err = validate_group(event);
Peter Zijlstraca037702010-03-02 19:52:12 +01001568 else
1569 err = validate_event(event);
Stephane Eranian81130702010-01-21 17:39:01 +02001570
1571 event->pmu = tmp;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001572 }
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02001573 if (err) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 if (event->destroy)
1575 event->destroy(event);
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02001576 }
Ingo Molnar621a01e2008-12-11 12:46:46 +01001577
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001578 return err;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001579}
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001580
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01001581static int x86_pmu_event_idx(struct perf_event *event)
1582{
1583 int idx = event->hw.idx;
1584
Peter Zijlstrac7206202012-03-22 17:26:36 +01001585 if (!x86_pmu.attr_rdpmc)
1586 return 0;
1587
Robert Richter15c7ad52012-06-20 20:46:33 +02001588 if (x86_pmu.num_counters_fixed && idx >= INTEL_PMC_IDX_FIXED) {
1589 idx -= INTEL_PMC_IDX_FIXED;
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01001590 idx |= 1 << 30;
1591 }
1592
1593 return idx + 1;
1594}
1595
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001596static ssize_t get_attr_rdpmc(struct device *cdev,
1597 struct device_attribute *attr,
1598 char *buf)
1599{
1600 return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
1601}
1602
1603static void change_rdpmc(void *info)
1604{
1605 bool enable = !!(unsigned long)info;
1606
1607 if (enable)
1608 set_in_cr4(X86_CR4_PCE);
1609 else
1610 clear_in_cr4(X86_CR4_PCE);
1611}
1612
1613static ssize_t set_attr_rdpmc(struct device *cdev,
1614 struct device_attribute *attr,
1615 const char *buf, size_t count)
1616{
Shuah Khane2b297f2012-06-10 21:13:41 -06001617 unsigned long val;
1618 ssize_t ret;
1619
1620 ret = kstrtoul(buf, 0, &val);
1621 if (ret)
1622 return ret;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001623
1624 if (!!val != !!x86_pmu.attr_rdpmc) {
1625 x86_pmu.attr_rdpmc = !!val;
1626 smp_call_function(change_rdpmc, (void *)val, 1);
1627 }
1628
1629 return count;
1630}
1631
1632static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc);
1633
1634static struct attribute *x86_pmu_attrs[] = {
1635 &dev_attr_rdpmc.attr,
1636 NULL,
1637};
1638
1639static struct attribute_group x86_pmu_attr_group = {
1640 .attrs = x86_pmu_attrs,
1641};
1642
1643static const struct attribute_group *x86_pmu_attr_groups[] = {
1644 &x86_pmu_attr_group,
Jiri Olsa641cc932012-03-15 20:09:14 +01001645 &x86_pmu_format_group,
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001646 NULL,
1647};
1648
Stephane Eraniand010b332012-02-09 23:21:00 +01001649static void x86_pmu_flush_branch_stack(void)
1650{
1651 if (x86_pmu.flush_branch_stack)
1652 x86_pmu.flush_branch_stack();
1653}
1654
Peter Zijlstrac93dc842012-06-08 14:50:50 +02001655void perf_check_microcode(void)
1656{
1657 if (x86_pmu.check_microcode)
1658 x86_pmu.check_microcode();
1659}
1660EXPORT_SYMBOL_GPL(perf_check_microcode);
1661
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001662static struct pmu pmu = {
Stephane Eraniand010b332012-02-09 23:21:00 +01001663 .pmu_enable = x86_pmu_enable,
1664 .pmu_disable = x86_pmu_disable,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001665
Peter Zijlstrac93dc842012-06-08 14:50:50 +02001666 .attr_groups = x86_pmu_attr_groups,
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001667
Peter Zijlstrac93dc842012-06-08 14:50:50 +02001668 .event_init = x86_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001669
Stephane Eraniand010b332012-02-09 23:21:00 +01001670 .add = x86_pmu_add,
1671 .del = x86_pmu_del,
1672 .start = x86_pmu_start,
1673 .stop = x86_pmu_stop,
1674 .read = x86_pmu_read,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001675
Peter Zijlstrac93dc842012-06-08 14:50:50 +02001676 .start_txn = x86_pmu_start_txn,
1677 .cancel_txn = x86_pmu_cancel_txn,
1678 .commit_txn = x86_pmu_commit_txn,
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01001679
Peter Zijlstrac93dc842012-06-08 14:50:50 +02001680 .event_idx = x86_pmu_event_idx,
Stephane Eraniand010b332012-02-09 23:21:00 +01001681 .flush_branch_stack = x86_pmu_flush_branch_stack,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001682};
1683
Peter Zijlstrac7206202012-03-22 17:26:36 +01001684void arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01001685{
Peter Zijlstrac7206202012-03-22 17:26:36 +01001686 userpg->cap_usr_time = 0;
1687 userpg->cap_usr_rdpmc = x86_pmu.attr_rdpmc;
1688 userpg->pmc_width = x86_pmu.cntval_bits;
1689
Peter Zijlstrae3f35412011-11-21 11:43:53 +01001690 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
1691 return;
1692
1693 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
1694 return;
1695
Peter Zijlstrac7206202012-03-22 17:26:36 +01001696 userpg->cap_usr_time = 1;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01001697 userpg->time_mult = this_cpu_read(cyc2ns);
1698 userpg->time_shift = CYC2NS_SCALE_FACTOR;
1699 userpg->time_offset = this_cpu_read(cyc2ns_offset) - now;
1700}
1701
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001702/*
1703 * callchain support
1704 */
1705
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001706static int backtrace_stack(void *data, char *name)
1707{
Ingo Molnar038e8362009-06-15 09:57:59 +02001708 return 0;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001709}
1710
1711static void backtrace_address(void *data, unsigned long addr, int reliable)
1712{
1713 struct perf_callchain_entry *entry = data;
1714
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001715 perf_callchain_store(entry, addr);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001716}
1717
1718static const struct stacktrace_ops backtrace_ops = {
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001719 .stack = backtrace_stack,
1720 .address = backtrace_address,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +01001721 .walk_stack = print_context_stack_bp,
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001722};
1723
Frederic Weisbecker56962b4442010-06-30 23:03:51 +02001724void
1725perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001726{
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001727 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1728 /* TODO: We don't support guest os callchain now */
Peter Zijlstraed805262010-08-20 14:30:41 +02001729 return;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001730 }
1731
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001732 perf_callchain_store(entry, regs->ip);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001733
Namhyung Kime8e999cf2011-03-18 11:40:06 +09001734 dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001735}
1736
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07001737static inline int
1738valid_user_frame(const void __user *fp, unsigned long size)
1739{
1740 return (__range_not_ok(fp, size, TASK_SIZE) == 0);
1741}
1742
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001743static unsigned long get_segment_base(unsigned int segment)
1744{
1745 struct desc_struct *desc;
1746 int idx = segment >> 3;
1747
1748 if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
1749 if (idx > LDT_ENTRIES)
1750 return 0;
1751
1752 if (idx > current->active_mm->context.size)
1753 return 0;
1754
1755 desc = current->active_mm->context.ldt;
1756 } else {
1757 if (idx > GDT_ENTRIES)
1758 return 0;
1759
1760 desc = __this_cpu_ptr(&gdt_page.gdt[0]);
1761 }
1762
1763 return get_desc_base(desc + idx);
1764}
1765
Torok Edwin257ef9d2010-03-17 12:07:16 +02001766#ifdef CONFIG_COMPAT
H. Peter Anvind1a797f2012-02-19 10:06:34 -08001767
1768#include <asm/compat.h>
1769
Torok Edwin257ef9d2010-03-17 12:07:16 +02001770static inline int
1771perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001772{
Torok Edwin257ef9d2010-03-17 12:07:16 +02001773 /* 32-bit process in 64-bit kernel. */
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001774 unsigned long ss_base, cs_base;
Torok Edwin257ef9d2010-03-17 12:07:16 +02001775 struct stack_frame_ia32 frame;
1776 const void __user *fp;
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001777
Torok Edwin257ef9d2010-03-17 12:07:16 +02001778 if (!test_thread_flag(TIF_IA32))
1779 return 0;
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001780
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001781 cs_base = get_segment_base(regs->cs);
1782 ss_base = get_segment_base(regs->ss);
1783
1784 fp = compat_ptr(ss_base + regs->bp);
Torok Edwin257ef9d2010-03-17 12:07:16 +02001785 while (entry->nr < PERF_MAX_STACK_DEPTH) {
1786 unsigned long bytes;
1787 frame.next_frame = 0;
1788 frame.return_address = 0;
1789
1790 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1791 if (bytes != sizeof(frame))
1792 break;
1793
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07001794 if (!valid_user_frame(fp, sizeof(frame)))
1795 break;
1796
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001797 perf_callchain_store(entry, cs_base + frame.return_address);
1798 fp = compat_ptr(ss_base + frame.next_frame);
Torok Edwin257ef9d2010-03-17 12:07:16 +02001799 }
1800 return 1;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001801}
Torok Edwin257ef9d2010-03-17 12:07:16 +02001802#else
1803static inline int
1804perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
1805{
1806 return 0;
1807}
1808#endif
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001809
Frederic Weisbecker56962b4442010-06-30 23:03:51 +02001810void
1811perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001812{
1813 struct stack_frame frame;
1814 const void __user *fp;
1815
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001816 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1817 /* TODO: We don't support guest os callchain now */
Peter Zijlstraed805262010-08-20 14:30:41 +02001818 return;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001819 }
Ingo Molnar5a6cec32009-05-29 11:25:09 +02001820
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001821 /*
1822 * We don't know what to do with VM86 stacks.. ignore them for now.
1823 */
1824 if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
1825 return;
1826
Peter Zijlstra74193ef2009-06-15 13:07:24 +02001827 fp = (void __user *)regs->bp;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001828
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001829 perf_callchain_store(entry, regs->ip);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001830
Andrey Vagin20afc602011-08-30 12:32:36 +04001831 if (!current->mm)
1832 return;
1833
Torok Edwin257ef9d2010-03-17 12:07:16 +02001834 if (perf_callchain_user32(regs, entry))
1835 return;
1836
Peter Zijlstraf9188e02009-06-18 22:20:52 +02001837 while (entry->nr < PERF_MAX_STACK_DEPTH) {
Torok Edwin257ef9d2010-03-17 12:07:16 +02001838 unsigned long bytes;
Ingo Molnar038e8362009-06-15 09:57:59 +02001839 frame.next_frame = NULL;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001840 frame.return_address = 0;
1841
Torok Edwin257ef9d2010-03-17 12:07:16 +02001842 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1843 if (bytes != sizeof(frame))
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001844 break;
1845
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07001846 if (!valid_user_frame(fp, sizeof(frame)))
1847 break;
1848
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001849 perf_callchain_store(entry, frame.return_address);
Ingo Molnar038e8362009-06-15 09:57:59 +02001850 fp = frame.next_frame;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02001851 }
1852}
1853
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001854/*
1855 * Deal with code segment offsets for the various execution modes:
1856 *
1857 * VM86 - the good olde 16 bit days, where the linear address is
1858 * 20 bits and we use regs->ip + 0x10 * regs->cs.
1859 *
1860 * IA32 - Where we need to look at GDT/LDT segment descriptor tables
1861 * to figure out what the 32bit base address is.
1862 *
1863 * X32 - has TIF_X32 set, but is running in x86_64
1864 *
1865 * X86_64 - CS,DS,SS,ES are all zero based.
1866 */
1867static unsigned long code_segment_base(struct pt_regs *regs)
1868{
1869 /*
1870 * If we are in VM86 mode, add the segment offset to convert to a
1871 * linear address.
1872 */
1873 if (regs->flags & X86_VM_MASK)
1874 return 0x10 * regs->cs;
1875
1876 /*
1877 * For IA32 we look at the GDT/LDT segment base to convert the
1878 * effective IP to a linear address.
1879 */
1880#ifdef CONFIG_X86_32
1881 if (user_mode(regs) && regs->cs != __USER_CS)
1882 return get_segment_base(regs->cs);
1883#else
1884 if (test_thread_flag(TIF_IA32)) {
1885 if (user_mode(regs) && regs->cs != __USER32_CS)
1886 return get_segment_base(regs->cs);
1887 }
1888#endif
1889 return 0;
1890}
1891
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001892unsigned long perf_instruction_pointer(struct pt_regs *regs)
1893{
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001894 if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001895 return perf_guest_cbs->get_guest_ip();
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08001896
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001897 return regs->ip + code_segment_base(regs);
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001898}
1899
1900unsigned long perf_misc_flags(struct pt_regs *regs)
1901{
1902 int misc = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08001903
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001904 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08001905 if (perf_guest_cbs->is_user_mode())
1906 misc |= PERF_RECORD_MISC_GUEST_USER;
1907 else
1908 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
1909 } else {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02001910 if (user_mode(regs))
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08001911 misc |= PERF_RECORD_MISC_USER;
1912 else
1913 misc |= PERF_RECORD_MISC_KERNEL;
1914 }
1915
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001916 if (regs->flags & PERF_EFLAGS_EXACT)
Peter Zijlstraab608342010-04-08 23:03:20 +02001917 misc |= PERF_RECORD_MISC_EXACT_IP;
Zhang, Yanmin39447b32010-04-19 13:32:41 +08001918
1919 return misc;
1920}
Gleb Natapovb3d94682011-11-10 14:57:27 +02001921
1922void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
1923{
1924 cap->version = x86_pmu.version;
1925 cap->num_counters_gp = x86_pmu.num_counters;
1926 cap->num_counters_fixed = x86_pmu.num_counters_fixed;
1927 cap->bit_width_gp = x86_pmu.cntval_bits;
1928 cap->bit_width_fixed = x86_pmu.cntval_bits;
1929 cap->events_mask = (unsigned int)x86_pmu.events_maskl;
1930 cap->events_mask_len = x86_pmu.events_mask_len;
1931}
1932EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);