blob: 5e5e76a52f58cd60678ceb3401cbaa6a32378ede [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
Peter Zijlstra90eec102015-11-16 11:08:45 +01008 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
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>
Andy Lutomirski7911d3f2014-10-24 15:58:12 -070034#include <asm/mmu_context.h>
Andy Lutomirski375074c2014-10-24 15:58:07 -070035#include <asm/tlbflush.h>
Peter Zijlstrae3f35412011-11-21 11:43:53 +010036#include <asm/timer.h>
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +020037#include <asm/desc.h>
38#include <asm/ldt.h>
Ingo Molnar241771e2008-12-03 10:39:53 +010039
Borislav Petkov27f6d222016-02-10 10:55:23 +010040#include "perf_event.h"
Kevin Winchesterde0428a2011-08-30 20:41:05 -030041
Kevin Winchesterde0428a2011-08-30 20:41:05 -030042struct x86_pmu x86_pmu __read_mostly;
Stephane Eranianefc9f052011-06-06 16:57:03 +020043
Kevin Winchesterde0428a2011-08-30 20:41:05 -030044DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +010045 .enabled = 1,
46};
Ingo Molnar241771e2008-12-03 10:39:53 +010047
Andy Lutomirskia6673422014-10-24 15:58:13 -070048struct static_key rdpmc_always_available = STATIC_KEY_INIT_FALSE;
49
Kevin Winchesterde0428a2011-08-30 20:41:05 -030050u64 __read_mostly hw_cache_event_ids
Ingo Molnar8326f442009-06-05 20:22:46 +020051 [PERF_COUNT_HW_CACHE_MAX]
52 [PERF_COUNT_HW_CACHE_OP_MAX]
53 [PERF_COUNT_HW_CACHE_RESULT_MAX];
Kevin Winchesterde0428a2011-08-30 20:41:05 -030054u64 __read_mostly hw_cache_extra_regs
Andi Kleene994d7d2011-03-03 10:34:48 +080055 [PERF_COUNT_HW_CACHE_MAX]
56 [PERF_COUNT_HW_CACHE_OP_MAX]
57 [PERF_COUNT_HW_CACHE_RESULT_MAX];
Ingo Molnar8326f442009-06-05 20:22:46 +020058
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +053059/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020060 * Propagate event elapsed time into the generic event.
61 * Can only be executed on the CPU where the event is active.
Ingo Molnaree060942008-12-13 09:00:03 +010062 * Returns the delta events processed.
63 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -030064u64 x86_perf_event_update(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +010065{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +010066 struct hw_perf_event *hwc = &event->hw;
Robert Richter948b1bb2010-03-29 18:36:50 +020067 int shift = 64 - x86_pmu.cntval_bits;
Peter Zijlstraec3232b2009-05-13 09:45:19 +020068 u64 prev_raw_count, new_raw_count;
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +010069 int idx = hwc->idx;
Peter Zijlstraec3232b2009-05-13 09:45:19 +020070 s64 delta;
Ingo Molnaree060942008-12-13 09:00:03 +010071
Robert Richter15c7ad52012-06-20 20:46:33 +020072 if (idx == INTEL_PMC_IDX_FIXED_BTS)
Markus Metzger30dd5682009-07-21 15:56:48 +020073 return 0;
74
Ingo Molnaree060942008-12-13 09:00:03 +010075 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020076 * Careful: an NMI might modify the previous event value.
Ingo Molnaree060942008-12-13 09:00:03 +010077 *
78 * Our tactic to handle this is to first atomically read and
79 * exchange a new raw count - then add that new-prev delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +020080 * count to the generic event atomically:
Ingo Molnaree060942008-12-13 09:00:03 +010081 */
82again:
Peter Zijlstrae7850592010-05-21 14:43:08 +020083 prev_raw_count = local64_read(&hwc->prev_count);
Vince Weaverc48b6052012-03-01 17:28:14 -050084 rdpmcl(hwc->event_base_rdpmc, new_raw_count);
Ingo Molnaree060942008-12-13 09:00:03 +010085
Peter Zijlstrae7850592010-05-21 14:43:08 +020086 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Ingo Molnaree060942008-12-13 09:00:03 +010087 new_raw_count) != prev_raw_count)
88 goto again;
89
90 /*
91 * Now we have the new raw value and have updated the prev
92 * timestamp already. We can now calculate the elapsed delta
Ingo Molnarcdd6c482009-09-21 12:02:48 +020093 * (event-)time and add that to the generic event.
Ingo Molnaree060942008-12-13 09:00:03 +010094 *
95 * Careful, not all hw sign-extends above the physical width
Peter Zijlstraec3232b2009-05-13 09:45:19 +020096 * of the count.
Ingo Molnaree060942008-12-13 09:00:03 +010097 */
Peter Zijlstraec3232b2009-05-13 09:45:19 +020098 delta = (new_raw_count << shift) - (prev_raw_count << shift);
99 delta >>= shift;
Ingo Molnaree060942008-12-13 09:00:03 +0100100
Peter Zijlstrae7850592010-05-21 14:43:08 +0200101 local64_add(delta, &event->count);
102 local64_sub(delta, &hwc->period_left);
Robert Richter4b7bfd02009-04-29 12:47:22 +0200103
104 return new_raw_count;
Ingo Molnaree060942008-12-13 09:00:03 +0100105}
106
Andi Kleena7e3ed12011-03-03 10:34:47 +0800107/*
108 * Find and validate any extra registers to set up.
109 */
110static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
111{
Stephane Eranianefc9f052011-06-06 16:57:03 +0200112 struct hw_perf_event_extra *reg;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800113 struct extra_reg *er;
114
Stephane Eranianefc9f052011-06-06 16:57:03 +0200115 reg = &event->hw.extra_reg;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800116
117 if (!x86_pmu.extra_regs)
118 return 0;
119
120 for (er = x86_pmu.extra_regs; er->msr; er++) {
121 if (er->event != (config & er->config_mask))
122 continue;
123 if (event->attr.config1 & ~er->valid_mask)
124 return -EINVAL;
Kan Liang338b5222014-07-14 12:25:56 -0700125 /* Check if the extra msrs can be safely accessed*/
126 if (!er->extra_msr_access)
127 return -ENXIO;
Stephane Eranianefc9f052011-06-06 16:57:03 +0200128
129 reg->idx = er->idx;
130 reg->config = event->attr.config1;
131 reg->reg = er->msr;
Andi Kleena7e3ed12011-03-03 10:34:47 +0800132 break;
133 }
134 return 0;
135}
136
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137static atomic_t active_events;
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300138static atomic_t pmc_refcount;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200139static DEFINE_MUTEX(pmc_reserve_mutex);
140
Robert Richterb27ea292010-03-17 12:49:10 +0100141#ifdef CONFIG_X86_LOCAL_APIC
142
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200143static bool reserve_pmc_hardware(void)
144{
145 int i;
146
Robert Richter948b1bb2010-03-29 18:36:50 +0200147 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100148 if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200149 goto perfctr_fail;
150 }
151
Robert Richter948b1bb2010-03-29 18:36:50 +0200152 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100153 if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200154 goto eventsel_fail;
155 }
156
157 return true;
158
159eventsel_fail:
160 for (i--; i >= 0; i--)
Robert Richter41bf4982011-02-02 17:40:57 +0100161 release_evntsel_nmi(x86_pmu_config_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200162
Robert Richter948b1bb2010-03-29 18:36:50 +0200163 i = x86_pmu.num_counters;
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200164
165perfctr_fail:
166 for (i--; i >= 0; i--)
Robert Richter41bf4982011-02-02 17:40:57 +0100167 release_perfctr_nmi(x86_pmu_event_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200168
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200169 return false;
170}
171
172static void release_pmc_hardware(void)
173{
174 int i;
175
Robert Richter948b1bb2010-03-29 18:36:50 +0200176 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100177 release_perfctr_nmi(x86_pmu_event_addr(i));
178 release_evntsel_nmi(x86_pmu_config_addr(i));
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200179 }
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200180}
181
Robert Richterb27ea292010-03-17 12:49:10 +0100182#else
183
184static bool reserve_pmc_hardware(void) { return true; }
185static void release_pmc_hardware(void) {}
186
187#endif
188
Don Zickus33c6d6a2010-11-22 16:55:23 -0500189static bool check_hw_exists(void)
190{
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100191 u64 val, val_fail, val_new= ~0;
192 int i, reg, reg_fail, ret = 0;
193 int bios_fail = 0;
Don Zickus68ab7472015-05-18 15:16:48 -0400194 int reg_safe = -1;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500195
Peter Zijlstra44072042010-12-08 15:56:23 +0100196 /*
197 * Check to see if the BIOS enabled any of the counters, if so
198 * complain and bail.
199 */
200 for (i = 0; i < x86_pmu.num_counters; i++) {
Robert Richter41bf4982011-02-02 17:40:57 +0100201 reg = x86_pmu_config_addr(i);
Peter Zijlstra44072042010-12-08 15:56:23 +0100202 ret = rdmsrl_safe(reg, &val);
203 if (ret)
204 goto msr_fail;
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100205 if (val & ARCH_PERFMON_EVENTSEL_ENABLE) {
206 bios_fail = 1;
207 val_fail = val;
208 reg_fail = reg;
Don Zickus68ab7472015-05-18 15:16:48 -0400209 } else {
210 reg_safe = i;
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100211 }
Peter Zijlstra44072042010-12-08 15:56:23 +0100212 }
213
214 if (x86_pmu.num_counters_fixed) {
215 reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
216 ret = rdmsrl_safe(reg, &val);
217 if (ret)
218 goto msr_fail;
219 for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100220 if (val & (0x03 << i*4)) {
221 bios_fail = 1;
222 val_fail = val;
223 reg_fail = reg;
224 }
Peter Zijlstra44072042010-12-08 15:56:23 +0100225 }
226 }
227
228 /*
Don Zickus68ab7472015-05-18 15:16:48 -0400229 * If all the counters are enabled, the below test will always
230 * fail. The tools will also become useless in this scenario.
231 * Just fail and disable the hardware counters.
232 */
233
234 if (reg_safe == -1) {
235 reg = reg_safe;
236 goto msr_fail;
237 }
238
239 /*
Andre Przywarabffd5fc2012-10-09 17:38:35 +0200240 * Read the current value, change it and read it back to see if it
241 * matches, this is needed to detect certain hardware emulators
242 * (qemu/kvm) that don't trap on the MSR access and always return 0s.
Peter Zijlstra44072042010-12-08 15:56:23 +0100243 */
Don Zickus68ab7472015-05-18 15:16:48 -0400244 reg = x86_pmu_event_addr(reg_safe);
Andre Przywarabffd5fc2012-10-09 17:38:35 +0200245 if (rdmsrl_safe(reg, &val))
246 goto msr_fail;
247 val ^= 0xffffUL;
Robert Richterf285f922012-06-20 20:46:36 +0200248 ret = wrmsrl_safe(reg, val);
249 ret |= rdmsrl_safe(reg, &val_new);
Don Zickus33c6d6a2010-11-22 16:55:23 -0500250 if (ret || val != val_new)
Peter Zijlstra44072042010-12-08 15:56:23 +0100251 goto msr_fail;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500252
Ingo Molnar45daae52011-03-25 10:24:23 +0100253 /*
254 * We still allow the PMU driver to operate:
255 */
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100256 if (bios_fail) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800257 pr_cont("Broken BIOS detected, complain to your hardware vendor.\n");
258 pr_err(FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n",
259 reg_fail, val_fail);
George Dunlapa5ebe0b2013-04-03 15:46:28 +0100260 }
Ingo Molnar45daae52011-03-25 10:24:23 +0100261
262 return true;
Peter Zijlstra44072042010-12-08 15:56:23 +0100263
264msr_fail:
Chen Yucong1b74dde2016-02-02 11:45:02 +0800265 pr_cont("Broken PMU hardware detected, using software events only.\n");
266 pr_info("%sFailed to access perfctr msr (MSR %x is %Lx)\n",
Peter Zijlstra (Intel)65d71fe2014-10-07 19:07:33 +0200267 boot_cpu_has(X86_FEATURE_HYPERVISOR) ? KERN_INFO : KERN_ERR,
268 reg, val_new);
Ingo Molnar45daae52011-03-25 10:24:23 +0100269
Peter Zijlstra44072042010-12-08 15:56:23 +0100270 return false;
Don Zickus33c6d6a2010-11-22 16:55:23 -0500271}
272
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200273static void hw_perf_event_destroy(struct perf_event *event)
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200274{
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300275 x86_release_hardware();
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300276 atomic_dec(&active_events);
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200277}
278
Alexander Shishkin48070342015-01-14 14:18:20 +0200279void hw_perf_lbr_event_destroy(struct perf_event *event)
280{
281 hw_perf_event_destroy(event);
282
283 /* undo the lbr/bts event accounting */
284 x86_del_exclusive(x86_lbr_exclusive_lbr);
285}
286
Robert Richter85cf9db2009-04-29 12:47:20 +0200287static inline int x86_pmu_initialized(void)
288{
289 return x86_pmu.handle_irq != NULL;
290}
291
Ingo Molnar8326f442009-06-05 20:22:46 +0200292static inline int
Andi Kleene994d7d2011-03-03 10:34:48 +0800293set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
Ingo Molnar8326f442009-06-05 20:22:46 +0200294{
Andi Kleene994d7d2011-03-03 10:34:48 +0800295 struct perf_event_attr *attr = &event->attr;
Ingo Molnar8326f442009-06-05 20:22:46 +0200296 unsigned int cache_type, cache_op, cache_result;
297 u64 config, val;
298
299 config = attr->config;
300
301 cache_type = (config >> 0) & 0xff;
302 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
303 return -EINVAL;
304
305 cache_op = (config >> 8) & 0xff;
306 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
307 return -EINVAL;
308
309 cache_result = (config >> 16) & 0xff;
310 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
311 return -EINVAL;
312
313 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
314
315 if (val == 0)
316 return -ENOENT;
317
318 if (val == -1)
319 return -EINVAL;
320
321 hwc->config |= val;
Andi Kleene994d7d2011-03-03 10:34:48 +0800322 attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
323 return x86_pmu_extra_regs(val, event);
Ingo Molnar8326f442009-06-05 20:22:46 +0200324}
325
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300326int x86_reserve_hardware(void)
327{
328 int err = 0;
329
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300330 if (!atomic_inc_not_zero(&pmc_refcount)) {
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300331 mutex_lock(&pmc_reserve_mutex);
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300332 if (atomic_read(&pmc_refcount) == 0) {
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300333 if (!reserve_pmc_hardware())
334 err = -EBUSY;
335 else
336 reserve_ds_buffers();
337 }
338 if (!err)
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300339 atomic_inc(&pmc_refcount);
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300340 mutex_unlock(&pmc_reserve_mutex);
341 }
342
343 return err;
344}
345
346void x86_release_hardware(void)
347{
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300348 if (atomic_dec_and_mutex_lock(&pmc_refcount, &pmc_reserve_mutex)) {
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300349 release_pmc_hardware();
350 release_ds_buffers();
351 mutex_unlock(&pmc_reserve_mutex);
352 }
353}
354
Alexander Shishkin48070342015-01-14 14:18:20 +0200355/*
356 * Check if we can create event of a certain type (that no conflicting events
357 * are present).
358 */
359int x86_add_exclusive(unsigned int what)
360{
Peter Zijlstra93472af2015-06-24 16:47:50 +0200361 int i;
Alexander Shishkin48070342015-01-14 14:18:20 +0200362
Alexander Shishkinccbebba2016-04-28 18:35:46 +0300363 if (x86_pmu.lbr_pt_coexist)
364 return 0;
365
Peter Zijlstra93472af2015-06-24 16:47:50 +0200366 if (!atomic_inc_not_zero(&x86_pmu.lbr_exclusive[what])) {
367 mutex_lock(&pmc_reserve_mutex);
368 for (i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++) {
369 if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i]))
370 goto fail_unlock;
371 }
372 atomic_inc(&x86_pmu.lbr_exclusive[what]);
373 mutex_unlock(&pmc_reserve_mutex);
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300374 }
Alexander Shishkin48070342015-01-14 14:18:20 +0200375
Peter Zijlstra93472af2015-06-24 16:47:50 +0200376 atomic_inc(&active_events);
377 return 0;
Alexander Shishkin48070342015-01-14 14:18:20 +0200378
Peter Zijlstra93472af2015-06-24 16:47:50 +0200379fail_unlock:
Alexander Shishkin48070342015-01-14 14:18:20 +0200380 mutex_unlock(&pmc_reserve_mutex);
Peter Zijlstra93472af2015-06-24 16:47:50 +0200381 return -EBUSY;
Alexander Shishkin48070342015-01-14 14:18:20 +0200382}
383
384void x86_del_exclusive(unsigned int what)
385{
Alexander Shishkinccbebba2016-04-28 18:35:46 +0300386 if (x86_pmu.lbr_pt_coexist)
387 return;
388
Alexander Shishkin48070342015-01-14 14:18:20 +0200389 atomic_dec(&x86_pmu.lbr_exclusive[what]);
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300390 atomic_dec(&active_events);
Alexander Shishkin48070342015-01-14 14:18:20 +0200391}
392
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300393int x86_setup_perfctr(struct perf_event *event)
Robert Richterc1726f32010-04-13 22:23:11 +0200394{
395 struct perf_event_attr *attr = &event->attr;
396 struct hw_perf_event *hwc = &event->hw;
397 u64 config;
398
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +0100399 if (!is_sampling_event(event)) {
Robert Richterc1726f32010-04-13 22:23:11 +0200400 hwc->sample_period = x86_pmu.max_period;
401 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200402 local64_set(&hwc->period_left, hwc->sample_period);
Robert Richterc1726f32010-04-13 22:23:11 +0200403 }
404
405 if (attr->type == PERF_TYPE_RAW)
Peter Zijlstraed13ec52011-11-14 10:03:25 +0100406 return x86_pmu_extra_regs(event->attr.config, event);
Robert Richterc1726f32010-04-13 22:23:11 +0200407
408 if (attr->type == PERF_TYPE_HW_CACHE)
Andi Kleene994d7d2011-03-03 10:34:48 +0800409 return set_ext_hw_attr(hwc, event);
Robert Richterc1726f32010-04-13 22:23:11 +0200410
411 if (attr->config >= x86_pmu.max_events)
412 return -EINVAL;
413
414 /*
415 * The generic map:
416 */
417 config = x86_pmu.event_map(attr->config);
418
419 if (config == 0)
420 return -ENOENT;
421
422 if (config == -1LL)
423 return -EINVAL;
424
425 /*
426 * Branch tracing:
427 */
Peter Zijlstra18a073a2011-04-26 13:24:33 +0200428 if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
429 !attr->freq && hwc->sample_period == 1) {
Robert Richterc1726f32010-04-13 22:23:11 +0200430 /* BTS is not supported by this architecture. */
Peter Zijlstra6809b6e2010-10-19 14:22:50 +0200431 if (!x86_pmu.bts_active)
Robert Richterc1726f32010-04-13 22:23:11 +0200432 return -EOPNOTSUPP;
433
434 /* BTS is currently only allowed for user-mode. */
435 if (!attr->exclude_kernel)
436 return -EOPNOTSUPP;
Alexander Shishkin48070342015-01-14 14:18:20 +0200437
438 /* disallow bts if conflicting events are present */
439 if (x86_add_exclusive(x86_lbr_exclusive_lbr))
440 return -EBUSY;
441
442 event->destroy = hw_perf_lbr_event_destroy;
Robert Richterc1726f32010-04-13 22:23:11 +0200443 }
444
445 hwc->config |= config;
446
447 return 0;
448}
Robert Richter4261e0e2010-04-13 22:23:10 +0200449
Stephane Eranianff3fb512012-02-09 23:20:54 +0100450/*
451 * check that branch_sample_type is compatible with
452 * settings needed for precise_ip > 1 which implies
453 * using the LBR to capture ALL taken branches at the
454 * priv levels of the measurement
455 */
456static inline int precise_br_compat(struct perf_event *event)
457{
458 u64 m = event->attr.branch_sample_type;
459 u64 b = 0;
460
461 /* must capture all branches */
462 if (!(m & PERF_SAMPLE_BRANCH_ANY))
463 return 0;
464
465 m &= PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_USER;
466
467 if (!event->attr.exclude_user)
468 b |= PERF_SAMPLE_BRANCH_USER;
469
470 if (!event->attr.exclude_kernel)
471 b |= PERF_SAMPLE_BRANCH_KERNEL;
472
473 /*
474 * ignore PERF_SAMPLE_BRANCH_HV, not supported on x86
475 */
476
477 return m == b;
478}
479
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300480int x86_pmu_hw_config(struct perf_event *event)
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300481{
Peter Zijlstraab608342010-04-08 23:03:20 +0200482 if (event->attr.precise_ip) {
483 int precise = 0;
484
485 /* Support for constant skid */
Peter Zijlstrac93dc842012-06-08 14:50:50 +0200486 if (x86_pmu.pebs_active && !x86_pmu.pebs_broken) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200487 precise++;
488
Peter Zijlstra5553be22010-10-19 14:38:11 +0200489 /* Support for IP fixup */
Andi Kleen03de8742014-08-07 17:08:54 -0700490 if (x86_pmu.lbr_nr || x86_pmu.intel_cap.pebs_format >= 2)
Peter Zijlstra5553be22010-10-19 14:38:11 +0200491 precise++;
Andi Kleen72469762015-12-04 03:50:52 -0800492
493 if (x86_pmu.pebs_prec_dist)
494 precise++;
Peter Zijlstra5553be22010-10-19 14:38:11 +0200495 }
Peter Zijlstraab608342010-04-08 23:03:20 +0200496
497 if (event->attr.precise_ip > precise)
498 return -EOPNOTSUPP;
Yan, Zheng4b854902014-11-04 21:56:08 -0500499 }
500 /*
501 * check that PEBS LBR correction does not conflict with
502 * whatever the user is asking with attr->branch_sample_type
503 */
504 if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) {
505 u64 *br_type = &event->attr.branch_sample_type;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100506
Yan, Zheng4b854902014-11-04 21:56:08 -0500507 if (has_branch_stack(event)) {
508 if (!precise_br_compat(event))
509 return -EOPNOTSUPP;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100510
Yan, Zheng4b854902014-11-04 21:56:08 -0500511 /* branch_sample_type is compatible */
Stephane Eranianff3fb512012-02-09 23:20:54 +0100512
Yan, Zheng4b854902014-11-04 21:56:08 -0500513 } else {
514 /*
515 * user did not specify branch_sample_type
516 *
517 * For PEBS fixups, we capture all
518 * the branches at the priv level of the
519 * event.
520 */
521 *br_type = PERF_SAMPLE_BRANCH_ANY;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100522
Yan, Zheng4b854902014-11-04 21:56:08 -0500523 if (!event->attr.exclude_user)
524 *br_type |= PERF_SAMPLE_BRANCH_USER;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100525
Yan, Zheng4b854902014-11-04 21:56:08 -0500526 if (!event->attr.exclude_kernel)
527 *br_type |= PERF_SAMPLE_BRANCH_KERNEL;
Stephane Eranianff3fb512012-02-09 23:20:54 +0100528 }
Peter Zijlstraab608342010-04-08 23:03:20 +0200529 }
530
Yan, Zhenge18bf522014-11-04 21:56:03 -0500531 if (event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK)
532 event->attach_state |= PERF_ATTACH_TASK_DATA;
533
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300534 /*
535 * Generate PMC IRQs:
536 * (keep 'enabled' bit clear for now)
537 */
Peter Zijlstrab4cdc5c2010-03-30 17:00:06 +0200538 event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300539
540 /*
541 * Count user and OS events unless requested not to
542 */
Peter Zijlstrab4cdc5c2010-03-30 17:00:06 +0200543 if (!event->attr.exclude_user)
544 event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
545 if (!event->attr.exclude_kernel)
546 event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
547
548 if (event->attr.type == PERF_TYPE_RAW)
549 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300550
Andi Kleen294fe0f2015-02-17 18:18:06 -0800551 if (event->attr.sample_period && x86_pmu.limit_period) {
552 if (x86_pmu.limit_period(event, event->attr.sample_period) >
553 event->attr.sample_period)
554 return -EINVAL;
555 }
556
Robert Richter9d0fcba62010-04-13 22:23:12 +0200557 return x86_setup_perfctr(event);
Cyrill Gorcunova0727382010-03-11 19:54:39 +0300558}
559
Ingo Molnaree060942008-12-13 09:00:03 +0100560/*
Peter Zijlstra0d486962009-06-02 19:22:16 +0200561 * Setup the hardware configuration for a given attr_type
Ingo Molnar241771e2008-12-03 10:39:53 +0100562 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200563static int __x86_pmu_event_init(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +0100564{
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200565 int err;
Ingo Molnar241771e2008-12-03 10:39:53 +0100566
Robert Richter85cf9db2009-04-29 12:47:20 +0200567 if (!x86_pmu_initialized())
568 return -ENODEV;
Ingo Molnar241771e2008-12-03 10:39:53 +0100569
Alexander Shishkin6b099d92015-06-11 15:13:56 +0300570 err = x86_reserve_hardware();
Peter Zijlstra4e935e42009-03-30 19:07:16 +0200571 if (err)
572 return err;
573
Alexander Shishkin1b7b9382015-06-09 13:03:26 +0300574 atomic_inc(&active_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575 event->destroy = hw_perf_event_destroy;
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +0200576
Robert Richter4261e0e2010-04-13 22:23:10 +0200577 event->hw.idx = -1;
578 event->hw.last_cpu = -1;
579 event->hw.last_tag = ~0ULL;
Stephane Eranianb6900812009-10-06 16:42:09 +0200580
Stephane Eranianefc9f052011-06-06 16:57:03 +0200581 /* mark unused */
582 event->hw.extra_reg.idx = EXTRA_REG_NONE;
Stephane Eranianb36817e2012-02-09 23:20:53 +0100583 event->hw.branch_reg.idx = EXTRA_REG_NONE;
584
Robert Richter9d0fcba62010-04-13 22:23:12 +0200585 return x86_pmu.hw_config(event);
Robert Richter4261e0e2010-04-13 22:23:10 +0200586}
587
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300588void x86_pmu_disable_all(void)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530589{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500590 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200591 int idx;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100592
Robert Richter948b1bb2010-03-29 18:36:50 +0200593 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100594 u64 val;
595
Robert Richter43f62012009-04-29 16:55:56 +0200596 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200597 continue;
Robert Richter41bf4982011-02-02 17:40:57 +0100598 rdmsrl(x86_pmu_config_addr(idx), val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100599 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
Robert Richter4295ee62009-04-29 12:47:01 +0200600 continue;
Robert Richterbb1165d2010-03-01 14:21:23 +0100601 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richter41bf4982011-02-02 17:40:57 +0100602 wrmsrl(x86_pmu_config_addr(idx), val);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530603 }
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530604}
605
Kan Liangc3d266c2016-03-03 18:07:28 -0500606/*
607 * There may be PMI landing after enabled=0. The PMI hitting could be before or
608 * after disable_all.
609 *
610 * If PMI hits before disable_all, the PMU will be disabled in the NMI handler.
611 * It will not be re-enabled in the NMI handler again, because enabled=0. After
612 * handling the NMI, disable_all will be called, which will not change the
613 * state either. If PMI hits after disable_all, the PMU is already disabled
614 * before entering NMI handler. The NMI handler will not change the state
615 * either.
616 *
617 * So either situation is harmless.
618 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200619static void x86_pmu_disable(struct pmu *pmu)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530620{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500621 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200622
Robert Richter85cf9db2009-04-29 12:47:20 +0200623 if (!x86_pmu_initialized())
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200624 return;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200625
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +0100626 if (!cpuc->enabled)
627 return;
628
629 cpuc->n_added = 0;
630 cpuc->enabled = 0;
631 barrier();
Stephane Eranian1da53e02010-01-18 10:58:01 +0200632
633 x86_pmu.disable_all();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +0530634}
Ingo Molnar241771e2008-12-03 10:39:53 +0100635
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300636void x86_pmu_enable_all(int added)
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530637{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500638 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530639 int idx;
640
Robert Richter948b1bb2010-03-29 18:36:50 +0200641 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richterd45dd922011-02-02 17:40:56 +0100642 struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
Peter Zijlstrab0f3f282009-03-05 18:08:27 +0100643
Robert Richter43f62012009-04-29 16:55:56 +0200644 if (!test_bit(idx, cpuc->active_mask))
Robert Richter4295ee62009-04-29 12:47:01 +0200645 continue;
Peter Zijlstra984b8382009-07-10 09:59:56 +0200646
Robert Richterd45dd922011-02-02 17:40:56 +0100647 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +0530648 }
649}
650
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200651static struct pmu pmu;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200652
653static inline int is_x86_event(struct perf_event *event)
654{
655 return event->pmu == &pmu;
656}
657
Robert Richter1e2ad282011-11-18 12:35:21 +0100658/*
659 * Event scheduler state:
660 *
661 * Assign events iterating over all events and counters, beginning
662 * with events with least weights first. Keep the current iterator
663 * state in struct sched_state.
664 */
665struct sched_state {
666 int weight;
667 int event; /* event index */
668 int counter; /* counter index */
669 int unassigned; /* number of events to be assigned left */
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200670 int nr_gp; /* number of GP counters used */
Robert Richter1e2ad282011-11-18 12:35:21 +0100671 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
672};
673
Robert Richterbc1738f2011-11-18 12:35:22 +0100674/* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */
675#define SCHED_STATES_MAX 2
676
Robert Richter1e2ad282011-11-18 12:35:21 +0100677struct perf_sched {
678 int max_weight;
679 int max_events;
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200680 int max_gp;
681 int saved_states;
Peter Zijlstrab371b592015-05-21 10:57:13 +0200682 struct event_constraint **constraints;
Robert Richter1e2ad282011-11-18 12:35:21 +0100683 struct sched_state state;
Robert Richterbc1738f2011-11-18 12:35:22 +0100684 struct sched_state saved[SCHED_STATES_MAX];
Robert Richter1e2ad282011-11-18 12:35:21 +0100685};
686
687/*
688 * Initialize interator that runs through all events and counters.
689 */
Peter Zijlstrab371b592015-05-21 10:57:13 +0200690static void perf_sched_init(struct perf_sched *sched, struct event_constraint **constraints,
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200691 int num, int wmin, int wmax, int gpmax)
Robert Richter1e2ad282011-11-18 12:35:21 +0100692{
693 int idx;
694
695 memset(sched, 0, sizeof(*sched));
696 sched->max_events = num;
697 sched->max_weight = wmax;
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200698 sched->max_gp = gpmax;
Peter Zijlstrab371b592015-05-21 10:57:13 +0200699 sched->constraints = constraints;
Robert Richter1e2ad282011-11-18 12:35:21 +0100700
701 for (idx = 0; idx < num; idx++) {
Peter Zijlstrab371b592015-05-21 10:57:13 +0200702 if (constraints[idx]->weight == wmin)
Robert Richter1e2ad282011-11-18 12:35:21 +0100703 break;
704 }
705
706 sched->state.event = idx; /* start with min weight */
707 sched->state.weight = wmin;
708 sched->state.unassigned = num;
709}
710
Robert Richterbc1738f2011-11-18 12:35:22 +0100711static void perf_sched_save_state(struct perf_sched *sched)
712{
713 if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX))
714 return;
715
716 sched->saved[sched->saved_states] = sched->state;
717 sched->saved_states++;
718}
719
720static bool perf_sched_restore_state(struct perf_sched *sched)
721{
722 if (!sched->saved_states)
723 return false;
724
725 sched->saved_states--;
726 sched->state = sched->saved[sched->saved_states];
727
728 /* continue with next counter: */
729 clear_bit(sched->state.counter++, sched->state.used);
730
731 return true;
732}
733
Robert Richter1e2ad282011-11-18 12:35:21 +0100734/*
735 * Select a counter for the current event to schedule. Return true on
736 * success.
737 */
Robert Richterbc1738f2011-11-18 12:35:22 +0100738static bool __perf_sched_find_counter(struct perf_sched *sched)
Robert Richter1e2ad282011-11-18 12:35:21 +0100739{
740 struct event_constraint *c;
741 int idx;
742
743 if (!sched->state.unassigned)
744 return false;
745
746 if (sched->state.event >= sched->max_events)
747 return false;
748
Peter Zijlstrab371b592015-05-21 10:57:13 +0200749 c = sched->constraints[sched->state.event];
Peter Zijlstra4defea82011-11-10 15:15:42 +0100750 /* Prefer fixed purpose counters */
Robert Richter15c7ad52012-06-20 20:46:33 +0200751 if (c->idxmsk64 & (~0ULL << INTEL_PMC_IDX_FIXED)) {
752 idx = INTEL_PMC_IDX_FIXED;
Akinobu Mita307b1cd2012-03-23 15:02:03 -0700753 for_each_set_bit_from(idx, c->idxmsk, X86_PMC_IDX_MAX) {
Peter Zijlstra4defea82011-11-10 15:15:42 +0100754 if (!__test_and_set_bit(idx, sched->state.used))
755 goto done;
756 }
757 }
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200758
Robert Richter1e2ad282011-11-18 12:35:21 +0100759 /* Grab the first unused counter starting with idx */
760 idx = sched->state.counter;
Robert Richter15c7ad52012-06-20 20:46:33 +0200761 for_each_set_bit_from(idx, c->idxmsk, INTEL_PMC_IDX_FIXED) {
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200762 if (!__test_and_set_bit(idx, sched->state.used)) {
763 if (sched->state.nr_gp++ >= sched->max_gp)
764 return false;
765
Peter Zijlstra4defea82011-11-10 15:15:42 +0100766 goto done;
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200767 }
Robert Richter1e2ad282011-11-18 12:35:21 +0100768 }
Robert Richter1e2ad282011-11-18 12:35:21 +0100769
Peter Zijlstra4defea82011-11-10 15:15:42 +0100770 return false;
771
772done:
773 sched->state.counter = idx;
Robert Richter1e2ad282011-11-18 12:35:21 +0100774
Robert Richterbc1738f2011-11-18 12:35:22 +0100775 if (c->overlap)
776 perf_sched_save_state(sched);
777
778 return true;
779}
780
781static bool perf_sched_find_counter(struct perf_sched *sched)
782{
783 while (!__perf_sched_find_counter(sched)) {
784 if (!perf_sched_restore_state(sched))
785 return false;
786 }
787
Robert Richter1e2ad282011-11-18 12:35:21 +0100788 return true;
789}
790
791/*
792 * Go through all unassigned events and find the next one to schedule.
793 * Take events with the least weight first. Return true on success.
794 */
795static bool perf_sched_next_event(struct perf_sched *sched)
796{
797 struct event_constraint *c;
798
799 if (!sched->state.unassigned || !--sched->state.unassigned)
800 return false;
801
802 do {
803 /* next event */
804 sched->state.event++;
805 if (sched->state.event >= sched->max_events) {
806 /* next weight */
807 sched->state.event = 0;
808 sched->state.weight++;
809 if (sched->state.weight > sched->max_weight)
810 return false;
811 }
Peter Zijlstrab371b592015-05-21 10:57:13 +0200812 c = sched->constraints[sched->state.event];
Robert Richter1e2ad282011-11-18 12:35:21 +0100813 } while (c->weight != sched->state.weight);
814
815 sched->state.counter = 0; /* start with first counter */
816
817 return true;
818}
819
820/*
821 * Assign a counter for each event.
822 */
Peter Zijlstrab371b592015-05-21 10:57:13 +0200823int perf_assign_events(struct event_constraint **constraints, int n,
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200824 int wmin, int wmax, int gpmax, int *assign)
Robert Richter1e2ad282011-11-18 12:35:21 +0100825{
826 struct perf_sched sched;
827
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200828 perf_sched_init(&sched, constraints, n, wmin, wmax, gpmax);
Robert Richter1e2ad282011-11-18 12:35:21 +0100829
830 do {
831 if (!perf_sched_find_counter(&sched))
832 break; /* failed */
833 if (assign)
834 assign[sched.state.event] = sched.state.counter;
835 } while (perf_sched_next_event(&sched));
836
837 return sched.state.unassigned;
838}
Yan, Zheng4a3dc122014-03-18 16:56:43 +0800839EXPORT_SYMBOL_GPL(perf_assign_events);
Robert Richter1e2ad282011-11-18 12:35:21 +0100840
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300841int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200842{
Andrew Hunter43b457802013-05-23 11:07:03 -0700843 struct event_constraint *c;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200844 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200845 struct perf_event *e;
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100846 int i, wmin, wmax, unsched = 0;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200847 struct hw_perf_event *hwc;
848
849 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
850
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100851 if (x86_pmu.start_scheduling)
852 x86_pmu.start_scheduling(cpuc);
853
Robert Richter1e2ad282011-11-18 12:35:21 +0100854 for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
Peter Zijlstrab371b592015-05-21 10:57:13 +0200855 cpuc->event_constraint[i] = NULL;
Stephane Eranian79cba822014-11-17 20:06:56 +0100856 c = x86_pmu.get_event_constraints(cpuc, i, cpuc->event_list[i]);
Peter Zijlstrab371b592015-05-21 10:57:13 +0200857 cpuc->event_constraint[i] = c;
Andrew Hunter43b457802013-05-23 11:07:03 -0700858
Robert Richter1e2ad282011-11-18 12:35:21 +0100859 wmin = min(wmin, c->weight);
860 wmax = max(wmax, c->weight);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200861 }
862
863 /*
Stephane Eranian81130702010-01-21 17:39:01 +0200864 * fastpath, try to reuse previous register
865 */
Peter Zijlstrac933c1a2010-01-22 16:40:12 +0100866 for (i = 0; i < n; i++) {
Stephane Eranian81130702010-01-21 17:39:01 +0200867 hwc = &cpuc->event_list[i]->hw;
Peter Zijlstrab371b592015-05-21 10:57:13 +0200868 c = cpuc->event_constraint[i];
Stephane Eranian81130702010-01-21 17:39:01 +0200869
870 /* never assigned */
871 if (hwc->idx == -1)
872 break;
873
874 /* constraint still honored */
Peter Zijlstra63b14642010-01-22 16:32:17 +0100875 if (!test_bit(hwc->idx, c->idxmsk))
Stephane Eranian81130702010-01-21 17:39:01 +0200876 break;
877
878 /* not already used */
879 if (test_bit(hwc->idx, used_mask))
880 break;
881
Peter Zijlstra34538ee2010-03-02 21:16:55 +0100882 __set_bit(hwc->idx, used_mask);
Stephane Eranian81130702010-01-21 17:39:01 +0200883 if (assign)
884 assign[i] = hwc->idx;
885 }
Stephane Eranian81130702010-01-21 17:39:01 +0200886
Robert Richter1e2ad282011-11-18 12:35:21 +0100887 /* slow path */
Peter Zijlstrab371b592015-05-21 10:57:13 +0200888 if (i != n) {
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200889 int gpmax = x86_pmu.num_counters;
890
891 /*
892 * Do not allow scheduling of more than half the available
893 * generic counters.
894 *
895 * This helps avoid counter starvation of sibling thread by
896 * ensuring at most half the counters cannot be in exclusive
897 * mode. There is no designated counters for the limits. Any
898 * N/2 counters can be used. This helps with events with
899 * specific counter constraints.
900 */
901 if (is_ht_workaround_enabled() && !cpuc->is_fake &&
902 READ_ONCE(cpuc->excl_cntrs->exclusive_present))
903 gpmax /= 2;
904
Peter Zijlstrab371b592015-05-21 10:57:13 +0200905 unsched = perf_assign_events(cpuc->event_constraint, n, wmin,
Peter Zijlstracc1790c2015-05-21 10:57:17 +0200906 wmax, gpmax, assign);
Peter Zijlstrab371b592015-05-21 10:57:13 +0200907 }
Stephane Eranian81130702010-01-21 17:39:01 +0200908
Stephane Eranian1da53e02010-01-18 10:58:01 +0200909 /*
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100910 * In case of success (unsched = 0), mark events as committed,
911 * so we do not put_constraint() in case new events are added
912 * and fail to be scheduled
913 *
914 * We invoke the lower level commit callback to lock the resource
915 *
916 * We do not need to do all of this in case we are called to
917 * validate an event group (assign == NULL)
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200918 */
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100919 if (!unsched && assign) {
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200920 for (i = 0; i < n; i++) {
921 e = cpuc->event_list[i];
922 e->hw.flags |= PERF_X86_EVENT_COMMITTED;
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100923 if (x86_pmu.commit_scheduling)
Peter Zijlstrab371b592015-05-21 10:57:13 +0200924 x86_pmu.commit_scheduling(cpuc, i, assign[i]);
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200925 }
Peter Zijlstra8736e542015-05-21 10:57:43 +0200926 } else {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200927 for (i = 0; i < n; i++) {
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200928 e = cpuc->event_list[i];
929 /*
930 * do not put_constraint() on comitted events,
931 * because they are good to go
932 */
933 if ((e->hw.flags & PERF_X86_EVENT_COMMITTED))
934 continue;
935
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100936 /*
937 * release events that failed scheduling
938 */
Stephane Eranian1da53e02010-01-18 10:58:01 +0200939 if (x86_pmu.put_event_constraints)
Stephane Eranian2f7f73a2013-06-20 18:42:54 +0200940 x86_pmu.put_event_constraints(cpuc, e);
Stephane Eranian1da53e02010-01-18 10:58:01 +0200941 }
942 }
Maria Dimakopoulouc5362c02014-11-17 20:06:55 +0100943
944 if (x86_pmu.stop_scheduling)
945 x86_pmu.stop_scheduling(cpuc);
946
Maria Dimakopouloue9791212014-11-17 20:06:58 +0100947 return unsched ? -EINVAL : 0;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200948}
949
950/*
951 * dogrp: true if must collect siblings events (group)
952 * returns total number of events and error code
953 */
954static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
955{
956 struct perf_event *event;
957 int n, max_count;
958
Robert Richter948b1bb2010-03-29 18:36:50 +0200959 max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200960
961 /* current number of events already accepted */
962 n = cpuc->n_events;
963
964 if (is_x86_event(leader)) {
965 if (n >= max_count)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100966 return -EINVAL;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200967 cpuc->event_list[n] = leader;
968 n++;
969 }
970 if (!dogrp)
971 return n;
972
973 list_for_each_entry(event, &leader->sibling_list, group_entry) {
974 if (!is_x86_event(event) ||
Stephane Eranian81130702010-01-21 17:39:01 +0200975 event->state <= PERF_EVENT_STATE_OFF)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200976 continue;
977
978 if (n >= max_count)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100979 return -EINVAL;
Stephane Eranian1da53e02010-01-18 10:58:01 +0200980
981 cpuc->event_list[n] = event;
982 n++;
983 }
984 return n;
985}
986
Stephane Eranian1da53e02010-01-18 10:58:01 +0200987static inline void x86_assign_hw_event(struct perf_event *event,
Stephane Eranian447a1942010-02-01 14:50:01 +0200988 struct cpu_hw_events *cpuc, int i)
Stephane Eranian1da53e02010-01-18 10:58:01 +0200989{
Stephane Eranian447a1942010-02-01 14:50:01 +0200990 struct hw_perf_event *hwc = &event->hw;
991
992 hwc->idx = cpuc->assign[i];
993 hwc->last_cpu = smp_processor_id();
994 hwc->last_tag = ++cpuc->tags[i];
Stephane Eranian1da53e02010-01-18 10:58:01 +0200995
Robert Richter15c7ad52012-06-20 20:46:33 +0200996 if (hwc->idx == INTEL_PMC_IDX_FIXED_BTS) {
Stephane Eranian1da53e02010-01-18 10:58:01 +0200997 hwc->config_base = 0;
998 hwc->event_base = 0;
Robert Richter15c7ad52012-06-20 20:46:33 +0200999 } else if (hwc->idx >= INTEL_PMC_IDX_FIXED) {
Stephane Eranian1da53e02010-01-18 10:58:01 +02001000 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
Robert Richter15c7ad52012-06-20 20:46:33 +02001001 hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - INTEL_PMC_IDX_FIXED);
1002 hwc->event_base_rdpmc = (hwc->idx - INTEL_PMC_IDX_FIXED) | 1<<30;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001003 } else {
Robert Richter73d6e522011-02-02 17:40:59 +01001004 hwc->config_base = x86_pmu_config_addr(hwc->idx);
1005 hwc->event_base = x86_pmu_event_addr(hwc->idx);
Jacob Shin0fbdad02013-02-06 11:26:28 -06001006 hwc->event_base_rdpmc = x86_pmu_rdpmc_index(hwc->idx);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001007 }
1008}
1009
Stephane Eranian447a1942010-02-01 14:50:01 +02001010static inline int match_prev_assignment(struct hw_perf_event *hwc,
1011 struct cpu_hw_events *cpuc,
1012 int i)
1013{
1014 return hwc->idx == cpuc->assign[i] &&
1015 hwc->last_cpu == smp_processor_id() &&
1016 hwc->last_tag == cpuc->tags[i];
1017}
1018
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001019static void x86_pmu_start(struct perf_event *event, int flags);
Peter Zijlstra2e841872010-01-25 15:58:43 +01001020
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001021static void x86_pmu_enable(struct pmu *pmu)
Ingo Molnaree060942008-12-13 09:00:03 +01001022{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001023 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001024 struct perf_event *event;
1025 struct hw_perf_event *hwc;
Peter Zijlstra11164cd2010-03-26 14:08:44 +01001026 int i, added = cpuc->n_added;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001027
Robert Richter85cf9db2009-04-29 12:47:20 +02001028 if (!x86_pmu_initialized())
Ingo Molnar2b9ff0d2008-12-14 18:36:30 +01001029 return;
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +01001030
1031 if (cpuc->enabled)
1032 return;
1033
Stephane Eranian1da53e02010-01-18 10:58:01 +02001034 if (cpuc->n_added) {
Peter Zijlstra19925ce2010-03-06 13:20:40 +01001035 int n_running = cpuc->n_events - cpuc->n_added;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001036 /*
1037 * apply assignment obtained either from
1038 * hw_perf_group_sched_in() or x86_pmu_enable()
1039 *
1040 * step1: save events moving to new counters
Stephane Eranian1da53e02010-01-18 10:58:01 +02001041 */
Peter Zijlstra19925ce2010-03-06 13:20:40 +01001042 for (i = 0; i < n_running; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +02001043 event = cpuc->event_list[i];
1044 hwc = &event->hw;
1045
Stephane Eranian447a1942010-02-01 14:50:01 +02001046 /*
1047 * we can avoid reprogramming counter if:
1048 * - assigned same counter as last time
1049 * - running on same CPU as last time
1050 * - no other event has used the counter since
1051 */
1052 if (hwc->idx == -1 ||
1053 match_prev_assignment(hwc, cpuc, i))
Stephane Eranian1da53e02010-01-18 10:58:01 +02001054 continue;
1055
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001056 /*
1057 * Ensure we don't accidentally enable a stopped
1058 * counter simply because we rescheduled.
1059 */
1060 if (hwc->state & PERF_HES_STOPPED)
1061 hwc->state |= PERF_HES_ARCH;
1062
1063 x86_pmu_stop(event, PERF_EF_UPDATE);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001064 }
1065
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001066 /*
1067 * step2: reprogram moved events into new counters
1068 */
Stephane Eranian1da53e02010-01-18 10:58:01 +02001069 for (i = 0; i < cpuc->n_events; i++) {
Stephane Eranian1da53e02010-01-18 10:58:01 +02001070 event = cpuc->event_list[i];
1071 hwc = &event->hw;
1072
Peter Zijlstra45e16a62010-03-11 13:40:30 +01001073 if (!match_prev_assignment(hwc, cpuc, i))
Stephane Eranian447a1942010-02-01 14:50:01 +02001074 x86_assign_hw_event(event, cpuc, i);
Peter Zijlstra45e16a62010-03-11 13:40:30 +01001075 else if (i < n_running)
1076 continue;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001077
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001078 if (hwc->state & PERF_HES_ARCH)
1079 continue;
1080
1081 x86_pmu_start(event, PERF_EF_RELOAD);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001082 }
1083 cpuc->n_added = 0;
1084 perf_events_lapic_init();
1085 }
Peter Zijlstra1a6e21f2010-01-27 23:07:47 +01001086
1087 cpuc->enabled = 1;
1088 barrier();
1089
Peter Zijlstra11164cd2010-03-26 14:08:44 +01001090 x86_pmu.enable_all(added);
Ingo Molnaree060942008-12-13 09:00:03 +01001091}
Ingo Molnaree060942008-12-13 09:00:03 +01001092
Tejun Heo245b2e72009-06-24 15:13:48 +09001093static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +01001094
Ingo Molnaree060942008-12-13 09:00:03 +01001095/*
1096 * Set the next IRQ period, based on the hwc->period_left value.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001097 * To be called with the event disabled in hw:
Ingo Molnaree060942008-12-13 09:00:03 +01001098 */
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001099int x86_perf_event_set_period(struct perf_event *event)
Ingo Molnar241771e2008-12-03 10:39:53 +01001100{
Peter Zijlstra07088ed2010-03-02 20:16:01 +01001101 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001102 s64 left = local64_read(&hwc->period_left);
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001103 s64 period = hwc->sample_period;
Peter Zijlstra7645a242010-03-08 13:51:31 +01001104 int ret = 0, idx = hwc->idx;
Ingo Molnar241771e2008-12-03 10:39:53 +01001105
Robert Richter15c7ad52012-06-20 20:46:33 +02001106 if (idx == INTEL_PMC_IDX_FIXED_BTS)
Markus Metzger30dd5682009-07-21 15:56:48 +02001107 return 0;
1108
Ingo Molnaree060942008-12-13 09:00:03 +01001109 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001110 * If we are way outside a reasonable range then just skip forward:
Ingo Molnaree060942008-12-13 09:00:03 +01001111 */
1112 if (unlikely(left <= -period)) {
1113 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001114 local64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001115 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001116 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +01001117 }
1118
1119 if (unlikely(left <= 0)) {
1120 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +02001121 local64_set(&hwc->period_left, left);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001122 hwc->last_period = period;
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001123 ret = 1;
Ingo Molnaree060942008-12-13 09:00:03 +01001124 }
Ingo Molnar1c80f4b2009-05-15 08:25:22 +02001125 /*
Ingo Molnardfc65092009-09-21 11:31:35 +02001126 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
Ingo Molnar1c80f4b2009-05-15 08:25:22 +02001127 */
1128 if (unlikely(left < 2))
1129 left = 2;
Ingo Molnaree060942008-12-13 09:00:03 +01001130
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001131 if (left > x86_pmu.max_period)
1132 left = x86_pmu.max_period;
1133
Andi Kleen294fe0f2015-02-17 18:18:06 -08001134 if (x86_pmu.limit_period)
1135 left = x86_pmu.limit_period(event, left);
1136
Tejun Heo245b2e72009-06-24 15:13:48 +09001137 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
Ingo Molnaree060942008-12-13 09:00:03 +01001138
Yan, Zheng851559e2015-05-06 15:33:47 -04001139 if (!(hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) ||
1140 local64_read(&hwc->prev_count) != (u64)-left) {
1141 /*
1142 * The hw event starts counting from this event offset,
1143 * mark it to be able to extra future deltas:
1144 */
1145 local64_set(&hwc->prev_count, (u64)-left);
Ingo Molnaree060942008-12-13 09:00:03 +01001146
Yan, Zheng851559e2015-05-06 15:33:47 -04001147 wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
1148 }
Cyrill Gorcunov68aa00a2010-06-03 01:23:04 +04001149
1150 /*
1151 * Due to erratum on certan cpu we need
1152 * a second write to be sure the register
1153 * is updated properly
1154 */
1155 if (x86_pmu.perfctr_second_write) {
Robert Richter73d6e522011-02-02 17:40:59 +01001156 wrmsrl(hwc->event_base,
Robert Richter948b1bb2010-03-29 18:36:50 +02001157 (u64)(-left) & x86_pmu.cntval_mask);
Cyrill Gorcunov68aa00a2010-06-03 01:23:04 +04001158 }
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001159
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160 perf_event_update_userpage(event);
Peter Zijlstra194002b2009-06-22 16:35:24 +02001161
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001162 return ret;
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001163}
1164
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001165void x86_pmu_enable_event(struct perf_event *event)
Robert Richter7c90cc42009-04-29 12:47:18 +02001166{
Tejun Heo0a3aee02010-12-18 16:28:55 +01001167 if (__this_cpu_read(cpu_hw_events.enabled))
Robert Richter31fa58a2010-04-13 22:23:14 +02001168 __x86_pmu_enable_event(&event->hw,
1169 ARCH_PERFMON_EVENTSEL_ENABLE);
Ingo Molnar241771e2008-12-03 10:39:53 +01001170}
1171
Ingo Molnaree060942008-12-13 09:00:03 +01001172/*
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001173 * Add a single event to the PMU.
Stephane Eranian1da53e02010-01-18 10:58:01 +02001174 *
1175 * The event is added to the group of enabled events
1176 * but only if it can be scehduled with existing events.
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001177 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001178static int x86_pmu_add(struct perf_event *event, int flags)
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001179{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001180 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001181 struct hw_perf_event *hwc;
1182 int assign[X86_PMC_IDX_MAX];
1183 int n, n0, ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001184
Stephane Eranian1da53e02010-01-18 10:58:01 +02001185 hwc = &event->hw;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001186
Stephane Eranian1da53e02010-01-18 10:58:01 +02001187 n0 = cpuc->n_events;
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001188 ret = n = collect_events(cpuc, event, false);
1189 if (ret < 0)
1190 goto out;
Ingo Molnar53b441a2009-05-25 21:41:28 +02001191
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001192 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1193 if (!(flags & PERF_EF_START))
1194 hwc->state |= PERF_HES_ARCH;
1195
Lin Ming4d1c52b2010-04-23 13:56:12 +08001196 /*
1197 * If group events scheduling transaction was started,
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001198 * skip the schedulability test here, it will be performed
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001199 * at commit time (->commit_txn) as a whole.
Lin Ming4d1c52b2010-04-23 13:56:12 +08001200 */
Sukadev Bhattiprolu8f3e5682015-09-03 20:07:53 -07001201 if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001202 goto done_collect;
Lin Ming4d1c52b2010-04-23 13:56:12 +08001203
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001204 ret = x86_pmu.schedule_events(cpuc, n, assign);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001205 if (ret)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001206 goto out;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001207 /*
1208 * copy new assignment, now we know it is possible
1209 * will be used by hw_perf_enable()
1210 */
1211 memcpy(cpuc->assign, assign, n*sizeof(int));
Ingo Molnar241771e2008-12-03 10:39:53 +01001212
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001213done_collect:
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001214 /*
1215 * Commit the collect_events() state. See x86_pmu_del() and
1216 * x86_pmu_*_txn().
1217 */
Stephane Eranian1da53e02010-01-18 10:58:01 +02001218 cpuc->n_events = n;
Peter Zijlstra356e1f22010-03-06 13:49:56 +01001219 cpuc->n_added += n - n0;
Stephane Eranian90151c352010-05-25 16:23:10 +02001220 cpuc->n_txn += n - n0;
Ingo Molnar7e2ae342008-12-09 11:40:46 +01001221
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001222 ret = 0;
1223out:
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001224 return ret;
Ingo Molnar241771e2008-12-03 10:39:53 +01001225}
1226
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001227static void x86_pmu_start(struct perf_event *event, int flags)
Stephane Eraniand76a0812010-02-08 17:06:01 +02001228{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001229 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001230 int idx = event->hw.idx;
1231
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001232 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1233 return;
Stephane Eraniand76a0812010-02-08 17:06:01 +02001234
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001235 if (WARN_ON_ONCE(idx == -1))
1236 return;
1237
1238 if (flags & PERF_EF_RELOAD) {
1239 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1240 x86_perf_event_set_period(event);
1241 }
1242
1243 event->hw.state = 0;
1244
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001245 cpuc->events[idx] = event;
1246 __set_bit(idx, cpuc->active_mask);
Robert Richter63e6be62010-09-15 18:20:34 +02001247 __set_bit(idx, cpuc->running);
Peter Zijlstraaff3d912010-03-02 20:32:08 +01001248 x86_pmu.enable(event);
Peter Zijlstrac08053e2010-03-06 13:19:24 +01001249 perf_event_update_userpage(event);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001250}
1251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252void perf_event_print_debug(void)
Ingo Molnar241771e2008-12-03 10:39:53 +01001253{
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001254 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
Andi Kleenda3e6062015-02-27 09:48:31 -08001255 u64 pebs, debugctl;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256 struct cpu_hw_events *cpuc;
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001257 unsigned long flags;
Ingo Molnar1e125672008-12-09 12:18:18 +01001258 int cpu, idx;
1259
Robert Richter948b1bb2010-03-29 18:36:50 +02001260 if (!x86_pmu.num_counters)
Ingo Molnar1e125672008-12-09 12:18:18 +01001261 return;
Ingo Molnar241771e2008-12-03 10:39:53 +01001262
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001263 local_irq_save(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +01001264
1265 cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001266 cpuc = &per_cpu(cpu_hw_events, cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +01001267
Robert Richterfaa28ae2009-04-29 12:47:13 +02001268 if (x86_pmu.version >= 2) {
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301269 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1270 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1271 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1272 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
Ingo Molnar241771e2008-12-03 10:39:53 +01001273
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301274 pr_info("\n");
1275 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1276 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1277 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1278 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
Andi Kleen15fde112015-02-27 09:48:32 -08001279 if (x86_pmu.pebs_constraints) {
1280 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
1281 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
1282 }
Andi Kleenda3e6062015-02-27 09:48:31 -08001283 if (x86_pmu.lbr_nr) {
1284 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
1285 pr_info("CPU#%d: debugctl: %016llx\n", cpu, debugctl);
1286 }
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301287 }
Peter Zijlstra7645a242010-03-08 13:51:31 +01001288 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
Ingo Molnar241771e2008-12-03 10:39:53 +01001289
Robert Richter948b1bb2010-03-29 18:36:50 +02001290 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richter41bf4982011-02-02 17:40:57 +01001291 rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
1292 rdmsrl(x86_pmu_event_addr(idx), pmc_count);
Ingo Molnar241771e2008-12-03 10:39:53 +01001293
Tejun Heo245b2e72009-06-24 15:13:48 +09001294 prev_left = per_cpu(pmc_prev_left[idx], cpu);
Ingo Molnar241771e2008-12-03 10:39:53 +01001295
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301296 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001297 cpu, idx, pmc_ctrl);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301298 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
Ingo Molnar241771e2008-12-03 10:39:53 +01001299 cpu, idx, pmc_count);
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301300 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
Ingo Molnaree060942008-12-13 09:00:03 +01001301 cpu, idx, prev_left);
Ingo Molnar241771e2008-12-03 10:39:53 +01001302 }
Robert Richter948b1bb2010-03-29 18:36:50 +02001303 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001304 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1305
Jaswinder Singh Rajputa1ef58f2009-02-28 18:45:39 +05301306 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
Ingo Molnar2f18d1e2008-12-22 11:10:42 +01001307 cpu, idx, pmc_count);
1308 }
Peter Zijlstra5bb9efe2009-05-13 08:12:51 +02001309 local_irq_restore(flags);
Ingo Molnar241771e2008-12-03 10:39:53 +01001310}
1311
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001312void x86_pmu_stop(struct perf_event *event, int flags)
Ingo Molnar241771e2008-12-03 10:39:53 +01001313{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001314 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315 struct hw_perf_event *hwc = &event->hw;
Ingo Molnar241771e2008-12-03 10:39:53 +01001316
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001317 if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
1318 x86_pmu.disable(event);
1319 cpuc->events[hwc->idx] = NULL;
1320 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
1321 hwc->state |= PERF_HES_STOPPED;
1322 }
Peter Zijlstra71e2d282010-03-08 17:51:33 +01001323
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001324 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
1325 /*
1326 * Drain the remaining delta count out of a event
1327 * that we are disabling:
1328 */
1329 x86_perf_event_update(event);
1330 hwc->state |= PERF_HES_UPTODATE;
1331 }
Peter Zijlstra2e841872010-01-25 15:58:43 +01001332}
1333
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001334static void x86_pmu_del(struct perf_event *event, int flags)
Peter Zijlstra2e841872010-01-25 15:58:43 +01001335{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001336 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstra2e841872010-01-25 15:58:43 +01001337 int i;
1338
Stephane Eranian90151c352010-05-25 16:23:10 +02001339 /*
Stephane Eranian2f7f73a2013-06-20 18:42:54 +02001340 * event is descheduled
1341 */
1342 event->hw.flags &= ~PERF_X86_EVENT_COMMITTED;
1343
1344 /*
Stephane Eranian90151c352010-05-25 16:23:10 +02001345 * If we're called during a txn, we don't need to do anything.
1346 * The events never got scheduled and ->cancel_txn will truncate
1347 * the event_list.
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001348 *
1349 * XXX assumes any ->del() called during a TXN will only be on
1350 * an event added during that same TXN.
Stephane Eranian90151c352010-05-25 16:23:10 +02001351 */
Sukadev Bhattiprolu8f3e5682015-09-03 20:07:53 -07001352 if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
Stephane Eranian90151c352010-05-25 16:23:10 +02001353 return;
1354
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001355 /*
1356 * Not a TXN, therefore cleanup properly.
1357 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001358 x86_pmu_stop(event, PERF_EF_UPDATE);
Peter Zijlstra194002b2009-06-22 16:35:24 +02001359
Stephane Eranian1da53e02010-01-18 10:58:01 +02001360 for (i = 0; i < cpuc->n_events; i++) {
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001361 if (event == cpuc->event_list[i])
Peter Zijlstra6c9687a2010-01-25 11:57:25 +01001362 break;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001363 }
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001364
1365 if (WARN_ON_ONCE(i == cpuc->n_events)) /* called ->del() without ->add() ? */
1366 return;
1367
1368 /* If we have a newly added event; make sure to decrease n_added. */
1369 if (i >= cpuc->n_events - cpuc->n_added)
1370 --cpuc->n_added;
1371
1372 if (x86_pmu.put_event_constraints)
1373 x86_pmu.put_event_constraints(cpuc, event);
1374
1375 /* Delete the array entry. */
Peter Zijlstrab371b592015-05-21 10:57:13 +02001376 while (++i < cpuc->n_events) {
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001377 cpuc->event_list[i-1] = cpuc->event_list[i];
Peter Zijlstrab371b592015-05-21 10:57:13 +02001378 cpuc->event_constraint[i-1] = cpuc->event_constraint[i];
1379 }
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001380 --cpuc->n_events;
1381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382 perf_event_update_userpage(event);
Ingo Molnar241771e2008-12-03 10:39:53 +01001383}
1384
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001385int x86_pmu_handle_irq(struct pt_regs *regs)
Robert Richtera29aa8a2009-04-29 12:47:21 +02001386{
Peter Zijlstradf1a1322009-06-10 21:02:22 +02001387 struct perf_sample_data data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388 struct cpu_hw_events *cpuc;
1389 struct perf_event *event;
Vince Weaver11d15782009-07-08 17:46:14 -04001390 int idx, handled = 0;
Ingo Molnar9029a5e2009-05-15 08:26:20 +02001391 u64 val;
1392
Christoph Lameter89cbc762014-08-17 12:30:40 -05001393 cpuc = this_cpu_ptr(&cpu_hw_events);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001394
Don Zickus2bce5da2011-04-27 06:32:33 -04001395 /*
1396 * Some chipsets need to unmask the LVTPC in a particular spot
1397 * inside the nmi handler. As a result, the unmasking was pushed
1398 * into all the nmi handlers.
1399 *
1400 * This generic handler doesn't seem to have any issues where the
1401 * unmasking occurs so it was left at the top.
1402 */
1403 apic_write(APIC_LVTPC, APIC_DM_NMI);
1404
Robert Richter948b1bb2010-03-29 18:36:50 +02001405 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
Robert Richter63e6be62010-09-15 18:20:34 +02001406 if (!test_bit(idx, cpuc->active_mask)) {
1407 /*
1408 * Though we deactivated the counter some cpus
1409 * might still deliver spurious interrupts still
1410 * in flight. Catch them:
1411 */
1412 if (__test_and_clear_bit(idx, cpuc->running))
1413 handled++;
Robert Richtera29aa8a2009-04-29 12:47:21 +02001414 continue;
Robert Richter63e6be62010-09-15 18:20:34 +02001415 }
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001416
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 event = cpuc->events[idx];
Peter Zijlstraa4016a72009-05-14 14:52:17 +02001418
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001419 val = x86_perf_event_update(event);
Robert Richter948b1bb2010-03-29 18:36:50 +02001420 if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
Peter Zijlstra48e22d52009-05-25 17:39:04 +02001421 continue;
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001422
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001423 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424 * event overflow
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001425 */
Robert Richter4177c422010-09-02 15:07:48 -04001426 handled++;
Robert Richterfd0d0002012-04-02 20:19:08 +02001427 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001428
Peter Zijlstra07088ed2010-03-02 20:16:01 +01001429 if (!x86_perf_event_set_period(event))
Peter Zijlstrae4abb5d2009-06-02 16:08:20 +02001430 continue;
1431
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001432 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001433 x86_pmu_stop(event, 0);
Robert Richtera29aa8a2009-04-29 12:47:21 +02001434 }
Peter Zijlstra962bf7a2009-05-13 13:21:36 +02001435
Peter Zijlstra9e350de2009-06-10 21:34:59 +02001436 if (handled)
1437 inc_irq_stat(apic_perf_irqs);
1438
Robert Richtera29aa8a2009-04-29 12:47:21 +02001439 return handled;
1440}
Robert Richter39d81ea2009-04-29 12:47:05 +02001441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442void perf_events_lapic_init(void)
Ingo Molnar241771e2008-12-03 10:39:53 +01001443{
Ingo Molnar04da8a42009-08-11 10:40:08 +02001444 if (!x86_pmu.apic || !x86_pmu_initialized())
Ingo Molnar241771e2008-12-03 10:39:53 +01001445 return;
Robert Richter85cf9db2009-04-29 12:47:20 +02001446
Ingo Molnar241771e2008-12-03 10:39:53 +01001447 /*
Yong Wangc323d952009-05-29 13:28:35 +08001448 * Always use NMI for PMU
Ingo Molnar241771e2008-12-03 10:39:53 +01001449 */
Yong Wangc323d952009-05-29 13:28:35 +08001450 apic_write(APIC_LVTPC, APIC_DM_NMI);
Ingo Molnar241771e2008-12-03 10:39:53 +01001451}
1452
Masami Hiramatsu93266382014-04-17 17:18:14 +09001453static int
Don Zickus9c48f1c2011-09-30 15:06:21 -04001454perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar241771e2008-12-03 10:39:53 +01001455{
Dave Hansen14c63f12013-06-21 08:51:36 -07001456 u64 start_clock;
1457 u64 finish_clock;
Peter Zijlstrae8a923c2013-10-17 15:32:10 +02001458 int ret;
Dave Hansen14c63f12013-06-21 08:51:36 -07001459
Alexander Shishkin1b7b9382015-06-09 13:03:26 +03001460 /*
1461 * All PMUs/events that share this PMI handler should make sure to
1462 * increment active_events for their events.
1463 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464 if (!atomic_read(&active_events))
Don Zickus9c48f1c2011-09-30 15:06:21 -04001465 return NMI_DONE;
Peter Zijlstra63a809a2009-05-01 12:23:17 +02001466
Peter Zijlstrae8a923c2013-10-17 15:32:10 +02001467 start_clock = sched_clock();
Dave Hansen14c63f12013-06-21 08:51:36 -07001468 ret = x86_pmu.handle_irq(regs);
Peter Zijlstrae8a923c2013-10-17 15:32:10 +02001469 finish_clock = sched_clock();
Dave Hansen14c63f12013-06-21 08:51:36 -07001470
1471 perf_sample_event_took(finish_clock - start_clock);
1472
1473 return ret;
Ingo Molnar241771e2008-12-03 10:39:53 +01001474}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001475NOKPROBE_SYMBOL(perf_event_nmi_handler);
Ingo Molnar241771e2008-12-03 10:39:53 +01001476
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001477struct event_constraint emptyconstraint;
1478struct event_constraint unconstrained;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301479
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001480static int
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001481x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1482{
1483 unsigned int cpu = (long)hcpu;
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001484 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
Stephane Eranian90413462014-11-17 20:06:54 +01001485 int i, ret = NOTIFY_OK;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001486
1487 switch (action & ~CPU_TASKS_FROZEN) {
1488 case CPU_UP_PREPARE:
Stephane Eranian90413462014-11-17 20:06:54 +01001489 for (i = 0 ; i < X86_PERF_KFREE_MAX; i++)
1490 cpuc->kfree_on_online[i] = NULL;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001491 if (x86_pmu.cpu_prepare)
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001492 ret = x86_pmu.cpu_prepare(cpu);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001493 break;
1494
1495 case CPU_STARTING:
1496 if (x86_pmu.cpu_starting)
1497 x86_pmu.cpu_starting(cpu);
1498 break;
1499
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001500 case CPU_ONLINE:
Stephane Eranian90413462014-11-17 20:06:54 +01001501 for (i = 0 ; i < X86_PERF_KFREE_MAX; i++) {
1502 kfree(cpuc->kfree_on_online[i]);
1503 cpuc->kfree_on_online[i] = NULL;
1504 }
Peter Zijlstra7fdba1c2011-07-22 13:41:54 +02001505 break;
1506
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001507 case CPU_DYING:
1508 if (x86_pmu.cpu_dying)
1509 x86_pmu.cpu_dying(cpu);
1510 break;
1511
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001512 case CPU_UP_CANCELED:
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001513 case CPU_DEAD:
1514 if (x86_pmu.cpu_dead)
1515 x86_pmu.cpu_dead(cpu);
1516 break;
1517
1518 default:
1519 break;
1520 }
1521
Peter Zijlstrab38b24e2010-03-23 19:31:15 +01001522 return ret;
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001523}
1524
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001525static void __init pmu_check_apic(void)
1526{
1527 if (cpu_has_apic)
1528 return;
1529
1530 x86_pmu.apic = 0;
1531 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1532 pr_info("no hardware sampling interrupt available.\n");
Vince Weaverc184c982014-05-16 17:18:07 -04001533
1534 /*
1535 * If we have a PMU initialized but no APIC
1536 * interrupts, we cannot sample hardware
1537 * events (user-space has to fall back and
1538 * sample via a hrtimer based software event):
1539 */
1540 pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
1541
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001542}
1543
Jiri Olsa641cc932012-03-15 20:09:14 +01001544static struct attribute_group x86_pmu_format_group = {
1545 .name = "format",
1546 .attrs = NULL,
1547};
1548
Jiri Olsa8300daa2012-10-10 14:53:12 +02001549/*
1550 * Remove all undefined events (x86_pmu.event_map(id) == 0)
1551 * out of events_attr attributes.
1552 */
1553static void __init filter_events(struct attribute **attrs)
1554{
Stephane Eranian3a54aaa2013-01-24 16:10:26 +01001555 struct device_attribute *d;
1556 struct perf_pmu_events_attr *pmu_attr;
Stephane Eranian61b87ca2015-12-07 20:33:25 +01001557 int offset = 0;
Jiri Olsa8300daa2012-10-10 14:53:12 +02001558 int i, j;
1559
1560 for (i = 0; attrs[i]; i++) {
Stephane Eranian3a54aaa2013-01-24 16:10:26 +01001561 d = (struct device_attribute *)attrs[i];
1562 pmu_attr = container_of(d, struct perf_pmu_events_attr, attr);
1563 /* str trumps id */
1564 if (pmu_attr->event_str)
1565 continue;
Stephane Eranian61b87ca2015-12-07 20:33:25 +01001566 if (x86_pmu.event_map(i + offset))
Jiri Olsa8300daa2012-10-10 14:53:12 +02001567 continue;
1568
1569 for (j = i; attrs[j]; j++)
1570 attrs[j] = attrs[j + 1];
1571
1572 /* Check the shifted attr. */
1573 i--;
Stephane Eranian61b87ca2015-12-07 20:33:25 +01001574
1575 /*
1576 * event_map() is index based, the attrs array is organized
1577 * by increasing event index. If we shift the events, then
1578 * we need to compensate for the event_map(), otherwise
1579 * we are looking up the wrong event in the map
1580 */
1581 offset++;
Jiri Olsa8300daa2012-10-10 14:53:12 +02001582 }
1583}
1584
Andi Kleen1a6461b2013-01-24 16:10:25 +01001585/* Merge two pointer arrays */
Andi Kleen47732d82015-06-29 14:22:13 -07001586__init struct attribute **merge_attr(struct attribute **a, struct attribute **b)
Andi Kleen1a6461b2013-01-24 16:10:25 +01001587{
1588 struct attribute **new;
1589 int j, i;
1590
1591 for (j = 0; a[j]; j++)
1592 ;
1593 for (i = 0; b[i]; i++)
1594 j++;
1595 j++;
1596
1597 new = kmalloc(sizeof(struct attribute *) * j, GFP_KERNEL);
1598 if (!new)
1599 return NULL;
1600
1601 j = 0;
1602 for (i = 0; a[i]; i++)
1603 new[j++] = a[i];
1604 for (i = 0; b[i]; i++)
1605 new[j++] = b[i];
1606 new[j] = NULL;
1607
1608 return new;
1609}
1610
Huang Ruic7ab62b2016-03-09 13:45:06 +08001611ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, char *page)
Jiri Olsaa4747392012-10-10 14:53:11 +02001612{
1613 struct perf_pmu_events_attr *pmu_attr = \
1614 container_of(attr, struct perf_pmu_events_attr, attr);
Jiri Olsaa4747392012-10-10 14:53:11 +02001615 u64 config = x86_pmu.event_map(pmu_attr->id);
Stephane Eranian3a54aaa2013-01-24 16:10:26 +01001616
1617 /* string trumps id */
1618 if (pmu_attr->event_str)
1619 return sprintf(page, "%s", pmu_attr->event_str);
1620
Jiri Olsaa4747392012-10-10 14:53:11 +02001621 return x86_pmu.events_sysfs_show(page, config);
1622}
Huang Ruic7ab62b2016-03-09 13:45:06 +08001623EXPORT_SYMBOL_GPL(events_sysfs_show);
Jiri Olsaa4747392012-10-10 14:53:11 +02001624
Jiri Olsaa4747392012-10-10 14:53:11 +02001625EVENT_ATTR(cpu-cycles, CPU_CYCLES );
1626EVENT_ATTR(instructions, INSTRUCTIONS );
1627EVENT_ATTR(cache-references, CACHE_REFERENCES );
1628EVENT_ATTR(cache-misses, CACHE_MISSES );
1629EVENT_ATTR(branch-instructions, BRANCH_INSTRUCTIONS );
1630EVENT_ATTR(branch-misses, BRANCH_MISSES );
1631EVENT_ATTR(bus-cycles, BUS_CYCLES );
1632EVENT_ATTR(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND );
1633EVENT_ATTR(stalled-cycles-backend, STALLED_CYCLES_BACKEND );
1634EVENT_ATTR(ref-cycles, REF_CPU_CYCLES );
1635
1636static struct attribute *empty_attrs;
1637
Peter Huewe95d18aa2012-10-29 21:48:17 +01001638static struct attribute *events_attr[] = {
Jiri Olsaa4747392012-10-10 14:53:11 +02001639 EVENT_PTR(CPU_CYCLES),
1640 EVENT_PTR(INSTRUCTIONS),
1641 EVENT_PTR(CACHE_REFERENCES),
1642 EVENT_PTR(CACHE_MISSES),
1643 EVENT_PTR(BRANCH_INSTRUCTIONS),
1644 EVENT_PTR(BRANCH_MISSES),
1645 EVENT_PTR(BUS_CYCLES),
1646 EVENT_PTR(STALLED_CYCLES_FRONTEND),
1647 EVENT_PTR(STALLED_CYCLES_BACKEND),
1648 EVENT_PTR(REF_CPU_CYCLES),
1649 NULL,
1650};
1651
1652static struct attribute_group x86_pmu_events_group = {
1653 .name = "events",
1654 .attrs = events_attr,
1655};
1656
Jiri Olsa0bf79d42012-10-10 14:53:14 +02001657ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event)
Jiri Olsa43c032f2012-10-10 14:53:13 +02001658{
Jiri Olsa43c032f2012-10-10 14:53:13 +02001659 u64 umask = (config & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
1660 u64 cmask = (config & ARCH_PERFMON_EVENTSEL_CMASK) >> 24;
1661 bool edge = (config & ARCH_PERFMON_EVENTSEL_EDGE);
1662 bool pc = (config & ARCH_PERFMON_EVENTSEL_PIN_CONTROL);
1663 bool any = (config & ARCH_PERFMON_EVENTSEL_ANY);
1664 bool inv = (config & ARCH_PERFMON_EVENTSEL_INV);
1665 ssize_t ret;
1666
1667 /*
1668 * We have whole page size to spend and just little data
1669 * to write, so we can safely use sprintf.
1670 */
1671 ret = sprintf(page, "event=0x%02llx", event);
1672
1673 if (umask)
1674 ret += sprintf(page + ret, ",umask=0x%02llx", umask);
1675
1676 if (edge)
1677 ret += sprintf(page + ret, ",edge");
1678
1679 if (pc)
1680 ret += sprintf(page + ret, ",pc");
1681
1682 if (any)
1683 ret += sprintf(page + ret, ",any");
1684
1685 if (inv)
1686 ret += sprintf(page + ret, ",inv");
1687
1688 if (cmask)
1689 ret += sprintf(page + ret, ",cmask=0x%02llx", cmask);
1690
1691 ret += sprintf(page + ret, "\n");
1692
1693 return ret;
1694}
1695
Yinghai Ludda99112011-01-21 15:30:01 -08001696static int __init init_hw_perf_events(void)
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301697{
Peter Zijlstrac1d6f422011-12-06 14:07:15 +01001698 struct x86_pmu_quirk *quirk;
Robert Richter72eae042009-04-29 12:47:10 +02001699 int err;
1700
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701 pr_info("Performance Events: ");
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001702
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301703 switch (boot_cpu_data.x86_vendor) {
1704 case X86_VENDOR_INTEL:
Robert Richter72eae042009-04-29 12:47:10 +02001705 err = intel_pmu_init();
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301706 break;
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301707 case X86_VENDOR_AMD:
Robert Richter72eae042009-04-29 12:47:10 +02001708 err = amd_pmu_init();
Jaswinder Singh Rajputf87ad352009-02-27 20:15:14 +05301709 break;
Robert Richter41389602009-04-29 12:47:00 +02001710 default:
Ingo Molnar8a3da6c72013-09-28 15:48:48 +02001711 err = -ENOTSUPP;
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301712 }
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001713 if (err != 0) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001714 pr_cont("no PMU driver, software events only.\n");
Peter Zijlstra004417a2010-11-25 18:38:29 +01001715 return 0;
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001716 }
Jaswinder Singh Rajputb56a3802009-02-27 18:09:09 +05301717
Cyrill Gorcunov12558032009-12-10 19:56:34 +03001718 pmu_check_apic();
1719
Don Zickus33c6d6a2010-11-22 16:55:23 -05001720 /* sanity check that the hardware exists or is emulated */
Peter Zijlstra44072042010-12-08 15:56:23 +01001721 if (!check_hw_exists())
Peter Zijlstra004417a2010-11-25 18:38:29 +01001722 return 0;
Don Zickus33c6d6a2010-11-22 16:55:23 -05001723
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001724 pr_cont("%s PMU driver.\n", x86_pmu.name);
Robert Richterfaa28ae2009-04-29 12:47:13 +02001725
Peter Zijlstrae97df762014-02-05 20:48:51 +01001726 x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */
1727
Peter Zijlstrac1d6f422011-12-06 14:07:15 +01001728 for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next)
1729 quirk->func();
Peter Zijlstra3c447802010-03-04 21:49:01 +01001730
Robert Richtera1eac7a2012-06-20 20:46:34 +02001731 if (!x86_pmu.intel_ctrl)
1732 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
Ingo Molnar862a1a52008-12-17 13:09:20 +01001733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 perf_events_lapic_init();
Don Zickus9c48f1c2011-09-30 15:06:21 -04001735 register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI");
Ingo Molnar1123e3a2009-05-29 11:25:09 +02001736
Peter Zijlstra63b14642010-01-22 16:32:17 +01001737 unconstrained = (struct event_constraint)
Robert Richter948b1bb2010-03-29 18:36:50 +02001738 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
Stephane Eranian9fac2cf2013-01-24 16:10:27 +01001739 0, x86_pmu.num_counters, 0, 0);
Peter Zijlstra63b14642010-01-22 16:32:17 +01001740
Jiri Olsa641cc932012-03-15 20:09:14 +01001741 x86_pmu_format_group.attrs = x86_pmu.format_attrs;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01001742
Stephane Eranianf20093e2013-01-24 16:10:32 +01001743 if (x86_pmu.event_attrs)
1744 x86_pmu_events_group.attrs = x86_pmu.event_attrs;
1745
Jiri Olsaa4747392012-10-10 14:53:11 +02001746 if (!x86_pmu.events_sysfs_show)
1747 x86_pmu_events_group.attrs = &empty_attrs;
Jiri Olsa8300daa2012-10-10 14:53:12 +02001748 else
1749 filter_events(x86_pmu_events_group.attrs);
Jiri Olsaa4747392012-10-10 14:53:11 +02001750
Andi Kleen1a6461b2013-01-24 16:10:25 +01001751 if (x86_pmu.cpu_events) {
1752 struct attribute **tmp;
1753
1754 tmp = merge_attr(x86_pmu_events_group.attrs, x86_pmu.cpu_events);
1755 if (!WARN_ON(!tmp))
1756 x86_pmu_events_group.attrs = tmp;
1757 }
1758
Ingo Molnar57c0c152009-09-21 12:20:38 +02001759 pr_info("... version: %d\n", x86_pmu.version);
Robert Richter948b1bb2010-03-29 18:36:50 +02001760 pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
1761 pr_info("... generic registers: %d\n", x86_pmu.num_counters);
1762 pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
Ingo Molnar57c0c152009-09-21 12:20:38 +02001763 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
Robert Richter948b1bb2010-03-29 18:36:50 +02001764 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
Robert Richterd6dc0b42010-03-17 12:49:13 +01001765 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001766
Peter Zijlstra2e80a822010-11-17 23:17:36 +01001767 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstra3f6da392010-03-05 13:01:18 +01001768 perf_cpu_notifier(x86_pmu_notifier);
Peter Zijlstra004417a2010-11-25 18:38:29 +01001769
1770 return 0;
Ingo Molnar241771e2008-12-03 10:39:53 +01001771}
Peter Zijlstra004417a2010-11-25 18:38:29 +01001772early_initcall(init_hw_perf_events);
Ingo Molnar621a01e2008-12-11 12:46:46 +01001773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774static inline void x86_pmu_read(struct perf_event *event)
Ingo Molnaree060942008-12-13 09:00:03 +01001775{
Peter Zijlstracc2ad4b2010-03-02 20:18:39 +01001776 x86_perf_event_update(event);
Ingo Molnaree060942008-12-13 09:00:03 +01001777}
1778
Lin Ming4d1c52b2010-04-23 13:56:12 +08001779/*
1780 * Start group events scheduling transaction
1781 * Set the flag to make pmu::enable() not perform the
1782 * schedulability test, it will be performed at commit time
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001783 *
1784 * We only support PERF_PMU_TXN_ADD transactions. Save the
1785 * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
1786 * transactions.
Lin Ming4d1c52b2010-04-23 13:56:12 +08001787 */
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001788static void x86_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001789{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001790 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1791
1792 WARN_ON_ONCE(cpuc->txn_flags); /* txn already in flight */
1793
1794 cpuc->txn_flags = txn_flags;
1795 if (txn_flags & ~PERF_PMU_TXN_ADD)
1796 return;
1797
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001798 perf_pmu_disable(pmu);
Tejun Heo0a3aee02010-12-18 16:28:55 +01001799 __this_cpu_write(cpu_hw_events.n_txn, 0);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001800}
1801
1802/*
1803 * Stop group events scheduling transaction
1804 * Clear the flag and pmu::enable() will perform the
1805 * schedulability test.
1806 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001807static void x86_pmu_cancel_txn(struct pmu *pmu)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001808{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001809 unsigned int txn_flags;
1810 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1811
1812 WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
1813
1814 txn_flags = cpuc->txn_flags;
1815 cpuc->txn_flags = 0;
1816 if (txn_flags & ~PERF_PMU_TXN_ADD)
1817 return;
1818
Stephane Eranian90151c352010-05-25 16:23:10 +02001819 /*
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001820 * Truncate collected array by the number of events added in this
1821 * transaction. See x86_pmu_add() and x86_pmu_*_txn().
Stephane Eranian90151c352010-05-25 16:23:10 +02001822 */
Tejun Heo0a3aee02010-12-18 16:28:55 +01001823 __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
1824 __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001825 perf_pmu_enable(pmu);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001826}
1827
1828/*
1829 * Commit group events scheduling transaction
1830 * Perform the group schedulability test as a whole
1831 * Return 0 if success
Peter Zijlstrac347a2f2014-02-24 12:26:21 +01001832 *
1833 * Does not cancel the transaction on failure; expects the caller to do this.
Lin Ming4d1c52b2010-04-23 13:56:12 +08001834 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001835static int x86_pmu_commit_txn(struct pmu *pmu)
Lin Ming4d1c52b2010-04-23 13:56:12 +08001836{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001837 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001838 int assign[X86_PMC_IDX_MAX];
1839 int n, ret;
1840
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001841 WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
1842
1843 if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
1844 cpuc->txn_flags = 0;
1845 return 0;
1846 }
1847
Lin Ming4d1c52b2010-04-23 13:56:12 +08001848 n = cpuc->n_events;
1849
1850 if (!x86_pmu_initialized())
1851 return -EAGAIN;
1852
1853 ret = x86_pmu.schedule_events(cpuc, n, assign);
1854 if (ret)
1855 return ret;
1856
1857 /*
1858 * copy new assignment, now we know it is possible
1859 * will be used by hw_perf_enable()
1860 */
1861 memcpy(cpuc->assign, assign, n*sizeof(int));
1862
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07001863 cpuc->txn_flags = 0;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001864 perf_pmu_enable(pmu);
Lin Ming4d1c52b2010-04-23 13:56:12 +08001865 return 0;
1866}
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001867/*
1868 * a fake_cpuc is used to validate event groups. Due to
1869 * the extra reg logic, we need to also allocate a fake
1870 * per_core and per_cpu structure. Otherwise, group events
1871 * using extra reg may conflict without the kernel being
1872 * able to catch this when the last event gets added to
1873 * the group.
1874 */
1875static void free_fake_cpuc(struct cpu_hw_events *cpuc)
1876{
1877 kfree(cpuc->shared_regs);
1878 kfree(cpuc);
1879}
1880
1881static struct cpu_hw_events *allocate_fake_cpuc(void)
1882{
1883 struct cpu_hw_events *cpuc;
1884 int cpu = raw_smp_processor_id();
1885
1886 cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL);
1887 if (!cpuc)
1888 return ERR_PTR(-ENOMEM);
1889
1890 /* only needed, if we have extra_regs */
1891 if (x86_pmu.extra_regs) {
1892 cpuc->shared_regs = allocate_shared_regs(cpu);
1893 if (!cpuc->shared_regs)
1894 goto error;
1895 }
Peter Zijlstrab430f7c2012-06-05 15:30:31 +02001896 cpuc->is_fake = 1;
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001897 return cpuc;
1898error:
1899 free_fake_cpuc(cpuc);
1900 return ERR_PTR(-ENOMEM);
1901}
Lin Ming4d1c52b2010-04-23 13:56:12 +08001902
Stephane Eranian1da53e02010-01-18 10:58:01 +02001903/*
Peter Zijlstraca037702010-03-02 19:52:12 +01001904 * validate that we can schedule this event
1905 */
1906static int validate_event(struct perf_event *event)
1907{
1908 struct cpu_hw_events *fake_cpuc;
1909 struct event_constraint *c;
1910 int ret = 0;
1911
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001912 fake_cpuc = allocate_fake_cpuc();
1913 if (IS_ERR(fake_cpuc))
1914 return PTR_ERR(fake_cpuc);
Peter Zijlstraca037702010-03-02 19:52:12 +01001915
Stephane Eranian79cba822014-11-17 20:06:56 +01001916 c = x86_pmu.get_event_constraints(fake_cpuc, -1, event);
Peter Zijlstraca037702010-03-02 19:52:12 +01001917
1918 if (!c || !c->weight)
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +01001919 ret = -EINVAL;
Peter Zijlstraca037702010-03-02 19:52:12 +01001920
1921 if (x86_pmu.put_event_constraints)
1922 x86_pmu.put_event_constraints(fake_cpuc, event);
1923
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001924 free_fake_cpuc(fake_cpuc);
Peter Zijlstraca037702010-03-02 19:52:12 +01001925
1926 return ret;
1927}
1928
1929/*
Stephane Eranian1da53e02010-01-18 10:58:01 +02001930 * validate a single event group
1931 *
1932 * validation include:
Ingo Molnar184f4122010-01-27 08:39:39 +01001933 * - check events are compatible which each other
1934 * - events do not compete for the same counter
1935 * - number of events <= number of counters
Stephane Eranian1da53e02010-01-18 10:58:01 +02001936 *
1937 * validation ensures the group can be loaded onto the
1938 * PMU if it was the only group available.
1939 */
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001940static int validate_group(struct perf_event *event)
1941{
Stephane Eranian1da53e02010-01-18 10:58:01 +02001942 struct perf_event *leader = event->group_leader;
Peter Zijlstra502568d2010-01-22 14:35:46 +01001943 struct cpu_hw_events *fake_cpuc;
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +01001944 int ret = -EINVAL, n;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001945
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001946 fake_cpuc = allocate_fake_cpuc();
1947 if (IS_ERR(fake_cpuc))
1948 return PTR_ERR(fake_cpuc);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001949 /*
1950 * the event is not yet connected with its
1951 * siblings therefore we must first collect
1952 * existing siblings, then add the new event
1953 * before we can simulate the scheduling
1954 */
Peter Zijlstra502568d2010-01-22 14:35:46 +01001955 n = collect_events(fake_cpuc, leader, true);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001956 if (n < 0)
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001957 goto out;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001958
Peter Zijlstra502568d2010-01-22 14:35:46 +01001959 fake_cpuc->n_events = n;
1960 n = collect_events(fake_cpuc, event, false);
Stephane Eranian1da53e02010-01-18 10:58:01 +02001961 if (n < 0)
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001962 goto out;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001963
Peter Zijlstra502568d2010-01-22 14:35:46 +01001964 fake_cpuc->n_events = n;
Stephane Eranian1da53e02010-01-18 10:58:01 +02001965
Cyrill Gorcunova0727382010-03-11 19:54:39 +03001966 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
Peter Zijlstra502568d2010-01-22 14:35:46 +01001967
Peter Zijlstra502568d2010-01-22 14:35:46 +01001968out:
Stephane Eraniancd8a38d2011-06-06 16:57:08 +02001969 free_fake_cpuc(fake_cpuc);
Peter Zijlstra502568d2010-01-22 14:35:46 +01001970 return ret;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001971}
1972
Yinghai Ludda99112011-01-21 15:30:01 -08001973static int x86_pmu_event_init(struct perf_event *event)
Ingo Molnar621a01e2008-12-11 12:46:46 +01001974{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001975 struct pmu *tmp;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001976 int err;
1977
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02001978 switch (event->attr.type) {
1979 case PERF_TYPE_RAW:
1980 case PERF_TYPE_HARDWARE:
1981 case PERF_TYPE_HW_CACHE:
1982 break;
1983
1984 default:
1985 return -ENOENT;
1986 }
1987
1988 err = __x86_pmu_event_init(event);
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001989 if (!err) {
Stephane Eranian81130702010-01-21 17:39:01 +02001990 /*
1991 * we temporarily connect event to its pmu
1992 * such that validate_group() can classify
1993 * it as an x86 event using is_x86_event()
1994 */
1995 tmp = event->pmu;
1996 event->pmu = &pmu;
1997
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02001998 if (event->group_leader != event)
1999 err = validate_group(event);
Peter Zijlstraca037702010-03-02 19:52:12 +01002000 else
2001 err = validate_event(event);
Stephane Eranian81130702010-01-21 17:39:01 +02002002
2003 event->pmu = tmp;
Peter Zijlstrafe9081c2009-10-08 11:56:07 +02002004 }
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02002005 if (err) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 if (event->destroy)
2007 event->destroy(event);
Peter Zijlstraa1792cdac2009-09-09 10:04:47 +02002008 }
Ingo Molnar621a01e2008-12-11 12:46:46 +01002009
Andy Lutomirski7911d3f2014-10-24 15:58:12 -07002010 if (ACCESS_ONCE(x86_pmu.attr_rdpmc))
2011 event->hw.flags |= PERF_X86_EVENT_RDPMC_ALLOWED;
2012
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02002013 return err;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002014}
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002015
Andy Lutomirski7911d3f2014-10-24 15:58:12 -07002016static void refresh_pce(void *ignored)
2017{
2018 if (current->mm)
2019 load_mm_cr4(current->mm);
2020}
2021
2022static void x86_pmu_event_mapped(struct perf_event *event)
2023{
2024 if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
2025 return;
2026
2027 if (atomic_inc_return(&current->mm->context.perf_rdpmc_allowed) == 1)
2028 on_each_cpu_mask(mm_cpumask(current->mm), refresh_pce, NULL, 1);
2029}
2030
2031static void x86_pmu_event_unmapped(struct perf_event *event)
2032{
2033 if (!current->mm)
2034 return;
2035
2036 if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
2037 return;
2038
2039 if (atomic_dec_and_test(&current->mm->context.perf_rdpmc_allowed))
2040 on_each_cpu_mask(mm_cpumask(current->mm), refresh_pce, NULL, 1);
2041}
2042
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01002043static int x86_pmu_event_idx(struct perf_event *event)
2044{
2045 int idx = event->hw.idx;
2046
Andy Lutomirski7911d3f2014-10-24 15:58:12 -07002047 if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED))
Peter Zijlstrac7206202012-03-22 17:26:36 +01002048 return 0;
2049
Robert Richter15c7ad52012-06-20 20:46:33 +02002050 if (x86_pmu.num_counters_fixed && idx >= INTEL_PMC_IDX_FIXED) {
2051 idx -= INTEL_PMC_IDX_FIXED;
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01002052 idx |= 1 << 30;
2053 }
2054
2055 return idx + 1;
2056}
2057
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002058static ssize_t get_attr_rdpmc(struct device *cdev,
2059 struct device_attribute *attr,
2060 char *buf)
2061{
2062 return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
2063}
2064
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002065static ssize_t set_attr_rdpmc(struct device *cdev,
2066 struct device_attribute *attr,
2067 const char *buf, size_t count)
2068{
Shuah Khane2b297f2012-06-10 21:13:41 -06002069 unsigned long val;
2070 ssize_t ret;
2071
2072 ret = kstrtoul(buf, 0, &val);
2073 if (ret)
2074 return ret;
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002075
Andy Lutomirskia6673422014-10-24 15:58:13 -07002076 if (val > 2)
2077 return -EINVAL;
2078
Peter Zijlstrae97df762014-02-05 20:48:51 +01002079 if (x86_pmu.attr_rdpmc_broken)
2080 return -ENOTSUPP;
2081
Andy Lutomirskia6673422014-10-24 15:58:13 -07002082 if ((val == 2) != (x86_pmu.attr_rdpmc == 2)) {
2083 /*
2084 * Changing into or out of always available, aka
2085 * perf-event-bypassing mode. This path is extremely slow,
2086 * but only root can trigger it, so it's okay.
2087 */
2088 if (val == 2)
2089 static_key_slow_inc(&rdpmc_always_available);
2090 else
2091 static_key_slow_dec(&rdpmc_always_available);
2092 on_each_cpu(refresh_pce, NULL, 1);
2093 }
2094
2095 x86_pmu.attr_rdpmc = val;
2096
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002097 return count;
2098}
2099
2100static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc);
2101
2102static struct attribute *x86_pmu_attrs[] = {
2103 &dev_attr_rdpmc.attr,
2104 NULL,
2105};
2106
2107static struct attribute_group x86_pmu_attr_group = {
2108 .attrs = x86_pmu_attrs,
2109};
2110
2111static const struct attribute_group *x86_pmu_attr_groups[] = {
2112 &x86_pmu_attr_group,
Jiri Olsa641cc932012-03-15 20:09:14 +01002113 &x86_pmu_format_group,
Jiri Olsaa4747392012-10-10 14:53:11 +02002114 &x86_pmu_events_group,
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002115 NULL,
2116};
2117
Yan, Zhengba532502014-11-04 21:55:58 -05002118static void x86_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
Stephane Eraniand010b332012-02-09 23:21:00 +01002119{
Yan, Zhengba532502014-11-04 21:55:58 -05002120 if (x86_pmu.sched_task)
2121 x86_pmu.sched_task(ctx, sched_in);
Stephane Eraniand010b332012-02-09 23:21:00 +01002122}
2123
Peter Zijlstrac93dc842012-06-08 14:50:50 +02002124void perf_check_microcode(void)
2125{
2126 if (x86_pmu.check_microcode)
2127 x86_pmu.check_microcode();
2128}
2129EXPORT_SYMBOL_GPL(perf_check_microcode);
2130
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02002131static struct pmu pmu = {
Stephane Eraniand010b332012-02-09 23:21:00 +01002132 .pmu_enable = x86_pmu_enable,
2133 .pmu_disable = x86_pmu_disable,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002134
Peter Zijlstrac93dc842012-06-08 14:50:50 +02002135 .attr_groups = x86_pmu_attr_groups,
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01002136
Peter Zijlstrac93dc842012-06-08 14:50:50 +02002137 .event_init = x86_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002138
Andy Lutomirski7911d3f2014-10-24 15:58:12 -07002139 .event_mapped = x86_pmu_event_mapped,
2140 .event_unmapped = x86_pmu_event_unmapped,
2141
Stephane Eraniand010b332012-02-09 23:21:00 +01002142 .add = x86_pmu_add,
2143 .del = x86_pmu_del,
2144 .start = x86_pmu_start,
2145 .stop = x86_pmu_stop,
2146 .read = x86_pmu_read,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002147
Peter Zijlstrac93dc842012-06-08 14:50:50 +02002148 .start_txn = x86_pmu_start_txn,
2149 .cancel_txn = x86_pmu_cancel_txn,
2150 .commit_txn = x86_pmu_commit_txn,
Peter Zijlstrafe4a3302011-11-20 20:44:06 +01002151
Peter Zijlstrac93dc842012-06-08 14:50:50 +02002152 .event_idx = x86_pmu_event_idx,
Yan, Zhengba532502014-11-04 21:55:58 -05002153 .sched_task = x86_pmu_sched_task,
Yan, Zhenge18bf522014-11-04 21:56:03 -05002154 .task_ctx_size = sizeof(struct x86_perf_task_context),
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02002155};
2156
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07002157void arch_perf_update_userpage(struct perf_event *event,
2158 struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01002159{
Peter Zijlstra20d1c862013-11-29 15:40:29 +01002160 struct cyc2ns_data *data;
2161
Peter Zijlstrafa731582013-09-19 10:16:42 +02002162 userpg->cap_user_time = 0;
2163 userpg->cap_user_time_zero = 0;
Andy Lutomirski7911d3f2014-10-24 15:58:12 -07002164 userpg->cap_user_rdpmc =
2165 !!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED);
Peter Zijlstrac7206202012-03-22 17:26:36 +01002166 userpg->pmc_width = x86_pmu.cntval_bits;
2167
Peter Zijlstra35af99e2013-11-28 19:38:42 +01002168 if (!sched_clock_stable())
Peter Zijlstrae3f35412011-11-21 11:43:53 +01002169 return;
2170
Peter Zijlstra20d1c862013-11-29 15:40:29 +01002171 data = cyc2ns_read_begin();
2172
Peter Zijlstra34f43922015-02-20 14:05:38 +01002173 /*
2174 * Internal timekeeping for enabled/running/stopped times
2175 * is always in the local_clock domain.
2176 */
Peter Zijlstrafa731582013-09-19 10:16:42 +02002177 userpg->cap_user_time = 1;
Peter Zijlstra20d1c862013-11-29 15:40:29 +01002178 userpg->time_mult = data->cyc2ns_mul;
2179 userpg->time_shift = data->cyc2ns_shift;
2180 userpg->time_offset = data->cyc2ns_offset - now;
Adrian Hunterc73deb62013-06-28 16:22:18 +03002181
Peter Zijlstra34f43922015-02-20 14:05:38 +01002182 /*
2183 * cap_user_time_zero doesn't make sense when we're using a different
2184 * time base for the records.
2185 */
2186 if (event->clock == &local_clock) {
2187 userpg->cap_user_time_zero = 1;
2188 userpg->time_zero = data->cyc2ns_offset;
2189 }
Peter Zijlstra20d1c862013-11-29 15:40:29 +01002190
2191 cyc2ns_read_end(data);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01002192}
2193
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002194/*
2195 * callchain support
2196 */
2197
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002198static int backtrace_stack(void *data, char *name)
2199{
Ingo Molnar038e8362009-06-15 09:57:59 +02002200 return 0;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002201}
2202
Alexei Starovoitov568b3292016-02-17 19:58:57 -08002203static int backtrace_address(void *data, unsigned long addr, int reliable)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002204{
2205 struct perf_callchain_entry *entry = data;
2206
Alexei Starovoitov568b3292016-02-17 19:58:57 -08002207 return perf_callchain_store(entry, addr);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002208}
2209
2210static const struct stacktrace_ops backtrace_ops = {
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002211 .stack = backtrace_stack,
2212 .address = backtrace_address,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +01002213 .walk_stack = print_context_stack_bp,
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002214};
2215
Frederic Weisbecker56962b42010-06-30 23:03:51 +02002216void
2217perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002218{
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002219 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
2220 /* TODO: We don't support guest os callchain now */
Peter Zijlstraed805262010-08-20 14:30:41 +02002221 return;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002222 }
2223
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02002224 perf_callchain_store(entry, regs->ip);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002225
Namhyung Kime8e999cf2011-03-18 11:40:06 +09002226 dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002227}
2228
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07002229static inline int
2230valid_user_frame(const void __user *fp, unsigned long size)
2231{
2232 return (__range_not_ok(fp, size, TASK_SIZE) == 0);
2233}
2234
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002235static unsigned long get_segment_base(unsigned int segment)
2236{
2237 struct desc_struct *desc;
2238 int idx = segment >> 3;
2239
2240 if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Andy Lutomirskia5b9e5a2015-07-30 14:31:34 -07002241#ifdef CONFIG_MODIFY_LDT_SYSCALL
Andy Lutomirski37868fe2015-07-30 14:31:32 -07002242 struct ldt_struct *ldt;
2243
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002244 if (idx > LDT_ENTRIES)
2245 return 0;
2246
Andy Lutomirski37868fe2015-07-30 14:31:32 -07002247 /* IRQs are off, so this synchronizes with smp_store_release */
2248 ldt = lockless_dereference(current->active_mm->context.ldt);
2249 if (!ldt || idx > ldt->size)
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002250 return 0;
2251
Andy Lutomirski37868fe2015-07-30 14:31:32 -07002252 desc = &ldt->entries[idx];
Andy Lutomirskia5b9e5a2015-07-30 14:31:34 -07002253#else
2254 return 0;
2255#endif
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002256 } else {
2257 if (idx > GDT_ENTRIES)
2258 return 0;
2259
Andy Lutomirski37868fe2015-07-30 14:31:32 -07002260 desc = raw_cpu_ptr(gdt_page.gdt) + idx;
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002261 }
2262
Andy Lutomirski37868fe2015-07-30 14:31:32 -07002263 return get_desc_base(desc);
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002264}
2265
Brian Gerst10ed3492015-06-22 07:55:17 -04002266#ifdef CONFIG_IA32_EMULATION
H. Peter Anvind1a797f2012-02-19 10:06:34 -08002267
2268#include <asm/compat.h>
2269
Torok Edwin257ef9d2010-03-17 12:07:16 +02002270static inline int
2271perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
Peter Zijlstra74193ef2009-06-15 13:07:24 +02002272{
Torok Edwin257ef9d2010-03-17 12:07:16 +02002273 /* 32-bit process in 64-bit kernel. */
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002274 unsigned long ss_base, cs_base;
Torok Edwin257ef9d2010-03-17 12:07:16 +02002275 struct stack_frame_ia32 frame;
2276 const void __user *fp;
Peter Zijlstra74193ef2009-06-15 13:07:24 +02002277
Torok Edwin257ef9d2010-03-17 12:07:16 +02002278 if (!test_thread_flag(TIF_IA32))
2279 return 0;
Peter Zijlstra74193ef2009-06-15 13:07:24 +02002280
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002281 cs_base = get_segment_base(regs->cs);
2282 ss_base = get_segment_base(regs->ss);
2283
2284 fp = compat_ptr(ss_base + regs->bp);
Andi Kleen75925e12015-10-22 15:07:21 -07002285 pagefault_disable();
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -03002286 while (entry->nr < sysctl_perf_event_max_stack) {
Torok Edwin257ef9d2010-03-17 12:07:16 +02002287 unsigned long bytes;
2288 frame.next_frame = 0;
2289 frame.return_address = 0;
2290
Andi Kleen75925e12015-10-22 15:07:21 -07002291 if (!access_ok(VERIFY_READ, fp, 8))
2292 break;
2293
2294 bytes = __copy_from_user_nmi(&frame.next_frame, fp, 4);
2295 if (bytes != 0)
2296 break;
2297 bytes = __copy_from_user_nmi(&frame.return_address, fp+4, 4);
Peter Zijlstra0a196842013-10-30 21:16:22 +01002298 if (bytes != 0)
Torok Edwin257ef9d2010-03-17 12:07:16 +02002299 break;
2300
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07002301 if (!valid_user_frame(fp, sizeof(frame)))
2302 break;
2303
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002304 perf_callchain_store(entry, cs_base + frame.return_address);
2305 fp = compat_ptr(ss_base + frame.next_frame);
Torok Edwin257ef9d2010-03-17 12:07:16 +02002306 }
Andi Kleen75925e12015-10-22 15:07:21 -07002307 pagefault_enable();
Torok Edwin257ef9d2010-03-17 12:07:16 +02002308 return 1;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002309}
Torok Edwin257ef9d2010-03-17 12:07:16 +02002310#else
2311static inline int
2312perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
2313{
2314 return 0;
2315}
2316#endif
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002317
Frederic Weisbecker56962b42010-06-30 23:03:51 +02002318void
2319perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002320{
2321 struct stack_frame frame;
2322 const void __user *fp;
2323
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002324 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
2325 /* TODO: We don't support guest os callchain now */
Peter Zijlstraed805262010-08-20 14:30:41 +02002326 return;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002327 }
Ingo Molnar5a6cec32009-05-29 11:25:09 +02002328
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002329 /*
2330 * We don't know what to do with VM86 stacks.. ignore them for now.
2331 */
2332 if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
2333 return;
2334
Peter Zijlstra74193ef2009-06-15 13:07:24 +02002335 fp = (void __user *)regs->bp;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002336
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02002337 perf_callchain_store(entry, regs->ip);
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002338
Andrey Vagin20afc602011-08-30 12:32:36 +04002339 if (!current->mm)
2340 return;
2341
Torok Edwin257ef9d2010-03-17 12:07:16 +02002342 if (perf_callchain_user32(regs, entry))
2343 return;
2344
Andi Kleen75925e12015-10-22 15:07:21 -07002345 pagefault_disable();
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -03002346 while (entry->nr < sysctl_perf_event_max_stack) {
Torok Edwin257ef9d2010-03-17 12:07:16 +02002347 unsigned long bytes;
Ingo Molnar038e8362009-06-15 09:57:59 +02002348 frame.next_frame = NULL;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002349 frame.return_address = 0;
2350
Andi Kleen75925e12015-10-22 15:07:21 -07002351 if (!access_ok(VERIFY_READ, fp, 16))
2352 break;
2353
2354 bytes = __copy_from_user_nmi(&frame.next_frame, fp, 8);
2355 if (bytes != 0)
2356 break;
2357 bytes = __copy_from_user_nmi(&frame.return_address, fp+8, 8);
Peter Zijlstra0a196842013-10-30 21:16:22 +01002358 if (bytes != 0)
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002359 break;
2360
Arun Sharmabc6ca7b2012-04-20 15:41:35 -07002361 if (!valid_user_frame(fp, sizeof(frame)))
2362 break;
2363
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02002364 perf_callchain_store(entry, frame.return_address);
Andi Kleen75925e12015-10-22 15:07:21 -07002365 fp = (void __user *)frame.next_frame;
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002366 }
Andi Kleen75925e12015-10-22 15:07:21 -07002367 pagefault_enable();
Peter Zijlstrad7d59fb2009-03-30 19:07:15 +02002368}
2369
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002370/*
2371 * Deal with code segment offsets for the various execution modes:
2372 *
2373 * VM86 - the good olde 16 bit days, where the linear address is
2374 * 20 bits and we use regs->ip + 0x10 * regs->cs.
2375 *
2376 * IA32 - Where we need to look at GDT/LDT segment descriptor tables
2377 * to figure out what the 32bit base address is.
2378 *
2379 * X32 - has TIF_X32 set, but is running in x86_64
2380 *
2381 * X86_64 - CS,DS,SS,ES are all zero based.
2382 */
2383static unsigned long code_segment_base(struct pt_regs *regs)
2384{
2385 /*
Andy Lutomirski383f3af2015-03-18 18:33:30 -07002386 * For IA32 we look at the GDT/LDT segment base to convert the
2387 * effective IP to a linear address.
2388 */
2389
2390#ifdef CONFIG_X86_32
2391 /*
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002392 * If we are in VM86 mode, add the segment offset to convert to a
2393 * linear address.
2394 */
2395 if (regs->flags & X86_VM_MASK)
2396 return 0x10 * regs->cs;
2397
Ingo Molnar55474c42015-03-29 11:02:34 +02002398 if (user_mode(regs) && regs->cs != __USER_CS)
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002399 return get_segment_base(regs->cs);
2400#else
Andy Lutomirskic56716a2015-03-18 18:33:28 -07002401 if (user_mode(regs) && !user_64bit_mode(regs) &&
2402 regs->cs != __USER32_CS)
2403 return get_segment_base(regs->cs);
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002404#endif
2405 return 0;
2406}
2407
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002408unsigned long perf_instruction_pointer(struct pt_regs *regs)
2409{
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002410 if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002411 return perf_guest_cbs->get_guest_ip();
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08002412
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002413 return regs->ip + code_segment_base(regs);
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002414}
2415
2416unsigned long perf_misc_flags(struct pt_regs *regs)
2417{
2418 int misc = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08002419
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002420 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08002421 if (perf_guest_cbs->is_user_mode())
2422 misc |= PERF_RECORD_MISC_GUEST_USER;
2423 else
2424 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
2425 } else {
Peter Zijlstrad07bdfd2012-07-10 09:42:15 +02002426 if (user_mode(regs))
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08002427 misc |= PERF_RECORD_MISC_USER;
2428 else
2429 misc |= PERF_RECORD_MISC_KERNEL;
2430 }
2431
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002432 if (regs->flags & PERF_EFLAGS_EXACT)
Peter Zijlstraab608342010-04-08 23:03:20 +02002433 misc |= PERF_RECORD_MISC_EXACT_IP;
Zhang, Yanmin39447b32010-04-19 13:32:41 +08002434
2435 return misc;
2436}
Gleb Natapovb3d94682011-11-10 14:57:27 +02002437
2438void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
2439{
2440 cap->version = x86_pmu.version;
2441 cap->num_counters_gp = x86_pmu.num_counters;
2442 cap->num_counters_fixed = x86_pmu.num_counters_fixed;
2443 cap->bit_width_gp = x86_pmu.cntval_bits;
2444 cap->bit_width_fixed = x86_pmu.cntval_bits;
2445 cap->events_mask = (unsigned int)x86_pmu.events_maskl;
2446 cap->events_mask_len = x86_pmu.events_mask_len;
2447}
2448EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);