2011-09-21 17:30:18 +08:00
|
|
|
/*
|
|
|
|
* Performance events - AMD IBS
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter
|
|
|
|
*
|
|
|
|
* For licencing details see kernel-base/COPYING
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/perf_event.h>
|
2016-07-14 08:19:01 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/export.h>
|
2011-09-21 17:30:18 +08:00
|
|
|
#include <linux/pci.h>
|
2012-04-03 02:19:11 +08:00
|
|
|
#include <linux/ptrace.h>
|
2014-01-15 22:57:29 +08:00
|
|
|
#include <linux/syscore_ops.h>
|
2017-02-01 23:36:40 +08:00
|
|
|
#include <linux/sched/clock.h>
|
2011-09-21 17:30:18 +08:00
|
|
|
|
|
|
|
#include <asm/apic.h>
|
|
|
|
|
2016-02-10 17:55:23 +08:00
|
|
|
#include "../perf_event.h"
|
2012-07-10 15:42:15 +08:00
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
static u32 ibs_caps;
|
|
|
|
|
|
|
|
#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
|
|
|
|
|
2011-12-16 00:56:37 +08:00
|
|
|
#include <linux/kprobes.h>
|
|
|
|
#include <linux/hardirq.h>
|
|
|
|
|
|
|
|
#include <asm/nmi.h>
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
|
|
|
|
#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
|
|
|
|
|
2016-03-21 18:47:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IBS states:
|
|
|
|
*
|
|
|
|
* ENABLED; tracks the pmu::add(), pmu::del() state, when set the counter is taken
|
|
|
|
* and any further add()s must fail.
|
|
|
|
*
|
|
|
|
* STARTED/STOPPING/STOPPED; deal with pmu::start(), pmu::stop() state but are
|
|
|
|
* complicated by the fact that the IBS hardware can send late NMIs (ie. after
|
|
|
|
* we've cleared the EN bit).
|
|
|
|
*
|
|
|
|
* In order to consume these late NMIs we have the STOPPED state, any NMI that
|
|
|
|
* happens after we've cleared the EN state will clear this bit and report the
|
|
|
|
* NMI handled (this is fundamentally racy in the face or multiple NMI sources,
|
|
|
|
* someone else can consume our BIT and our NMI will go unhandled).
|
|
|
|
*
|
|
|
|
* And since we cannot set/clear this separate bit together with the EN bit,
|
|
|
|
* there are races; if we cleared STARTED early, an NMI could land in
|
|
|
|
* between clearing STARTED and clearing the EN bit (in fact multiple NMIs
|
|
|
|
* could happen if the period is small enough), and consume our STOPPED bit
|
|
|
|
* and trigger streams of unhandled NMIs.
|
|
|
|
*
|
|
|
|
* If, however, we clear STARTED late, an NMI can hit between clearing the
|
|
|
|
* EN bit and clearing STARTED, still see STARTED set and process the event.
|
|
|
|
* If this event will have the VALID bit clear, we bail properly, but this
|
|
|
|
* is not a given. With VALID set we can end up calling pmu::stop() again
|
|
|
|
* (the throttle logic) and trigger the WARNs in there.
|
|
|
|
*
|
|
|
|
* So what we do is set STOPPING before clearing EN to avoid the pmu::stop()
|
|
|
|
* nesting, and clear STARTED late, so that we have a well defined state over
|
|
|
|
* the clearing of the EN bit.
|
|
|
|
*
|
|
|
|
* XXX: we could probably be using !atomic bitops for all this.
|
|
|
|
*/
|
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
enum ibs_states {
|
|
|
|
IBS_ENABLED = 0,
|
|
|
|
IBS_STARTED = 1,
|
|
|
|
IBS_STOPPING = 2,
|
2016-03-21 18:47:52 +08:00
|
|
|
IBS_STOPPED = 3,
|
2011-12-16 00:56:38 +08:00
|
|
|
|
|
|
|
IBS_MAX_STATES,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cpu_perf_ibs {
|
|
|
|
struct perf_event *event;
|
|
|
|
unsigned long state[BITS_TO_LONGS(IBS_MAX_STATES)];
|
|
|
|
};
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
struct perf_ibs {
|
2012-09-12 18:59:44 +08:00
|
|
|
struct pmu pmu;
|
|
|
|
unsigned int msr;
|
|
|
|
u64 config_mask;
|
|
|
|
u64 cnt_mask;
|
|
|
|
u64 enable_mask;
|
|
|
|
u64 valid_mask;
|
|
|
|
u64 max_period;
|
|
|
|
unsigned long offset_mask[1];
|
|
|
|
int offset_max;
|
|
|
|
struct cpu_perf_ibs __percpu *pcpu;
|
|
|
|
|
|
|
|
struct attribute **format_attrs;
|
|
|
|
struct attribute_group format_group;
|
|
|
|
const struct attribute_group *attr_groups[2];
|
|
|
|
|
|
|
|
u64 (*get_count)(u64 config);
|
2011-12-16 00:56:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct perf_ibs_data {
|
|
|
|
u32 size;
|
|
|
|
union {
|
|
|
|
u32 data[0]; /* data buffer starts here */
|
|
|
|
u32 caps;
|
|
|
|
};
|
|
|
|
u64 regs[MSR_AMD64_IBS_REG_COUNT_MAX];
|
2011-12-16 00:56:36 +08:00
|
|
|
};
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
static int
|
2012-04-03 02:19:13 +08:00
|
|
|
perf_event_set_period(struct hw_perf_event *hwc, u64 min, u64 max, u64 *hw_period)
|
2011-12-16 00:56:39 +08:00
|
|
|
{
|
|
|
|
s64 left = local64_read(&hwc->period_left);
|
|
|
|
s64 period = hwc->sample_period;
|
|
|
|
int overflow = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are way outside a reasonable range then just skip forward:
|
|
|
|
*/
|
|
|
|
if (unlikely(left <= -period)) {
|
|
|
|
left = period;
|
|
|
|
local64_set(&hwc->period_left, left);
|
|
|
|
hwc->last_period = period;
|
|
|
|
overflow = 1;
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:19:14 +08:00
|
|
|
if (unlikely(left < (s64)min)) {
|
2011-12-16 00:56:39 +08:00
|
|
|
left += period;
|
|
|
|
local64_set(&hwc->period_left, left);
|
|
|
|
hwc->last_period = period;
|
|
|
|
overflow = 1;
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:19:15 +08:00
|
|
|
/*
|
|
|
|
* If the hw period that triggers the sw overflow is too short
|
|
|
|
* we might hit the irq handler. This biases the results.
|
|
|
|
* Thus we shorten the next-to-last period and set the last
|
|
|
|
* period to the max period.
|
|
|
|
*/
|
|
|
|
if (left > max) {
|
|
|
|
left -= max;
|
|
|
|
if (left > max)
|
|
|
|
left = max;
|
|
|
|
else if (left < min)
|
|
|
|
left = min;
|
|
|
|
}
|
2011-12-16 00:56:39 +08:00
|
|
|
|
2012-04-03 02:19:13 +08:00
|
|
|
*hw_period = (u64)left;
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
return overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
perf_event_try_update(struct perf_event *event, u64 new_raw_count, int width)
|
|
|
|
{
|
|
|
|
struct hw_perf_event *hwc = &event->hw;
|
|
|
|
int shift = 64 - width;
|
|
|
|
u64 prev_raw_count;
|
|
|
|
u64 delta;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Careful: an NMI might modify the previous event value.
|
|
|
|
*
|
|
|
|
* Our tactic to handle this is to first atomically read and
|
|
|
|
* exchange a new raw count - then add that new-prev delta
|
|
|
|
* count to the generic event atomically:
|
|
|
|
*/
|
|
|
|
prev_raw_count = local64_read(&hwc->prev_count);
|
|
|
|
if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
|
|
|
|
new_raw_count) != prev_raw_count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we have the new raw value and have updated the prev
|
|
|
|
* timestamp already. We can now calculate the elapsed delta
|
|
|
|
* (event-)time and add that to the generic event.
|
|
|
|
*
|
|
|
|
* Careful, not all hw sign-extends above the physical width
|
|
|
|
* of the count.
|
|
|
|
*/
|
|
|
|
delta = (new_raw_count << shift) - (prev_raw_count << shift);
|
|
|
|
delta >>= shift;
|
|
|
|
|
|
|
|
local64_add(delta, &event->count);
|
|
|
|
local64_sub(delta, &hwc->period_left);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
static struct perf_ibs perf_ibs_fetch;
|
|
|
|
static struct perf_ibs perf_ibs_op;
|
|
|
|
|
|
|
|
static struct perf_ibs *get_ibs_pmu(int type)
|
|
|
|
{
|
|
|
|
if (perf_ibs_fetch.pmu.type == type)
|
|
|
|
return &perf_ibs_fetch;
|
|
|
|
if (perf_ibs_op.pmu.type == type)
|
|
|
|
return &perf_ibs_op;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-21 17:30:18 +08:00
|
|
|
|
2012-03-12 19:54:32 +08:00
|
|
|
/*
|
|
|
|
* Use IBS for precise event sampling:
|
|
|
|
*
|
|
|
|
* perf record -a -e cpu-cycles:p ... # use ibs op counting cycle count
|
|
|
|
* perf record -a -e r076:p ... # same as -e cpu-cycles:p
|
|
|
|
* perf record -a -e r0C1:p ... # use ibs op counting micro-ops
|
|
|
|
*
|
|
|
|
* IbsOpCntCtl (bit 19) of IBS Execution Control Register (IbsOpCtl,
|
|
|
|
* MSRC001_1033) is used to select either cycle or micro-ops counting
|
|
|
|
* mode.
|
|
|
|
*
|
|
|
|
* The rip of IBS samples has skid 0. Thus, IBS supports precise
|
|
|
|
* levels 1 and 2 and the PERF_EFLAGS_EXACT is set. In rare cases the
|
|
|
|
* rip is invalid when IBS was not able to record the rip correctly.
|
|
|
|
* We clear PERF_EFLAGS_EXACT and take the rip from pt_regs then.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int perf_ibs_precise_event(struct perf_event *event, u64 *config)
|
|
|
|
{
|
|
|
|
switch (event->attr.precise_ip) {
|
|
|
|
case 0:
|
|
|
|
return -ENOENT;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event->attr.type) {
|
|
|
|
case PERF_TYPE_HARDWARE:
|
|
|
|
switch (event->attr.config) {
|
|
|
|
case PERF_COUNT_HW_CPU_CYCLES:
|
|
|
|
*config = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERF_TYPE_RAW:
|
|
|
|
switch (event->attr.config) {
|
|
|
|
case 0x0076:
|
|
|
|
*config = 0;
|
|
|
|
return 0;
|
|
|
|
case 0x00C1:
|
|
|
|
*config = IBS_OP_CNT_CTL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2012-07-26 01:12:45 +08:00
|
|
|
static const struct perf_event_attr ibs_notsupp = {
|
|
|
|
.exclude_user = 1,
|
|
|
|
.exclude_kernel = 1,
|
|
|
|
.exclude_hv = 1,
|
|
|
|
.exclude_idle = 1,
|
|
|
|
.exclude_host = 1,
|
|
|
|
.exclude_guest = 1,
|
|
|
|
};
|
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
static int perf_ibs_init(struct perf_event *event)
|
|
|
|
{
|
2011-12-16 00:56:36 +08:00
|
|
|
struct hw_perf_event *hwc = &event->hw;
|
|
|
|
struct perf_ibs *perf_ibs;
|
|
|
|
u64 max_cnt, config;
|
2012-03-12 19:54:32 +08:00
|
|
|
int ret;
|
2011-12-16 00:56:36 +08:00
|
|
|
|
|
|
|
perf_ibs = get_ibs_pmu(event->attr.type);
|
2012-03-12 19:54:32 +08:00
|
|
|
if (perf_ibs) {
|
|
|
|
config = event->attr.config;
|
|
|
|
} else {
|
|
|
|
perf_ibs = &perf_ibs_op;
|
|
|
|
ret = perf_ibs_precise_event(event, &config);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->pmu != &perf_ibs->pmu)
|
2011-09-21 17:30:18 +08:00
|
|
|
return -ENOENT;
|
2011-12-16 00:56:36 +08:00
|
|
|
|
2012-07-26 01:12:45 +08:00
|
|
|
if (perf_flags(&event->attr) & perf_flags(&ibs_notsupp))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
if (config & ~perf_ibs->config_mask)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (hwc->sample_period) {
|
|
|
|
if (config & perf_ibs->cnt_mask)
|
|
|
|
/* raw max_cnt may not be set */
|
|
|
|
return -EINVAL;
|
2012-04-03 02:19:10 +08:00
|
|
|
if (!event->attr.sample_freq && hwc->sample_period & 0x0f)
|
|
|
|
/*
|
|
|
|
* lower 4 bits can not be set in ibs max cnt,
|
|
|
|
* but allowing it in case we adjust the
|
|
|
|
* sample period to set a frequency.
|
|
|
|
*/
|
2011-12-16 00:56:36 +08:00
|
|
|
return -EINVAL;
|
2012-04-03 02:19:10 +08:00
|
|
|
hwc->sample_period &= ~0x0FULL;
|
|
|
|
if (!hwc->sample_period)
|
|
|
|
hwc->sample_period = 0x10;
|
2011-12-16 00:56:36 +08:00
|
|
|
} else {
|
|
|
|
max_cnt = config & perf_ibs->cnt_mask;
|
2011-12-16 00:56:39 +08:00
|
|
|
config &= ~perf_ibs->cnt_mask;
|
2011-12-16 00:56:36 +08:00
|
|
|
event->attr.sample_period = max_cnt << 4;
|
|
|
|
hwc->sample_period = event->attr.sample_period;
|
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
if (!hwc->sample_period)
|
2011-12-16 00:56:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-04-03 02:19:10 +08:00
|
|
|
/*
|
|
|
|
* If we modify hwc->sample_period, we also need to update
|
|
|
|
* hwc->last_period and hwc->period_left.
|
|
|
|
*/
|
|
|
|
hwc->last_period = hwc->sample_period;
|
|
|
|
local64_set(&hwc->period_left, hwc->sample_period);
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
hwc->config_base = perf_ibs->msr;
|
|
|
|
hwc->config = config;
|
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
static int perf_ibs_set_period(struct perf_ibs *perf_ibs,
|
|
|
|
struct hw_perf_event *hwc, u64 *period)
|
|
|
|
{
|
2012-04-03 02:19:13 +08:00
|
|
|
int overflow;
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
/* ignore lower 4 bits in min count: */
|
2012-04-03 02:19:13 +08:00
|
|
|
overflow = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period);
|
2011-12-16 00:56:39 +08:00
|
|
|
local64_set(&hwc->prev_count, 0);
|
|
|
|
|
2012-04-03 02:19:13 +08:00
|
|
|
return overflow;
|
2011-12-16 00:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static u64 get_ibs_fetch_count(u64 config)
|
|
|
|
{
|
|
|
|
return (config & IBS_FETCH_CNT) >> 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 get_ibs_op_count(u64 config)
|
|
|
|
{
|
2012-04-03 02:19:18 +08:00
|
|
|
u64 count = 0;
|
|
|
|
|
|
|
|
if (config & IBS_OP_VAL)
|
|
|
|
count += (config & IBS_OP_MAX_CNT) << 4; /* cnt rolled over */
|
|
|
|
|
|
|
|
if (ibs_caps & IBS_CAPS_RDWROPCNT)
|
|
|
|
count += (config & IBS_OP_CUR_CNT) >> 32;
|
|
|
|
|
|
|
|
return count;
|
2011-12-16 00:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event,
|
2012-04-03 02:19:16 +08:00
|
|
|
u64 *config)
|
2011-12-16 00:56:39 +08:00
|
|
|
{
|
2012-04-03 02:19:16 +08:00
|
|
|
u64 count = perf_ibs->get_count(*config);
|
2011-12-16 00:56:39 +08:00
|
|
|
|
2012-04-03 02:19:18 +08:00
|
|
|
/*
|
|
|
|
* Set width to 64 since we do not overflow on max width but
|
|
|
|
* instead on max count. In perf_ibs_set_period() we clear
|
|
|
|
* prev count manually on overflow.
|
|
|
|
*/
|
|
|
|
while (!perf_event_try_update(event, count, 64)) {
|
2012-04-03 02:19:16 +08:00
|
|
|
rdmsrl(event->hw.config_base, *config);
|
|
|
|
count = perf_ibs->get_count(*config);
|
2011-12-16 00:56:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:19:16 +08:00
|
|
|
static inline void perf_ibs_enable_event(struct perf_ibs *perf_ibs,
|
|
|
|
struct hw_perf_event *hwc, u64 config)
|
2011-12-16 00:56:39 +08:00
|
|
|
{
|
2012-04-03 02:19:16 +08:00
|
|
|
wrmsrl(hwc->config_base, hwc->config | config | perf_ibs->enable_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Erratum #420 Instruction-Based Sampling Engine May Generate
|
|
|
|
* Interrupt that Cannot Be Cleared:
|
|
|
|
*
|
|
|
|
* Must clear counter mask first, then clear the enable bit. See
|
|
|
|
* Revision Guide for AMD Family 10h Processors, Publication #41322.
|
|
|
|
*/
|
|
|
|
static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs,
|
|
|
|
struct hw_perf_event *hwc, u64 config)
|
|
|
|
{
|
|
|
|
config &= ~perf_ibs->cnt_mask;
|
|
|
|
wrmsrl(hwc->config_base, config);
|
|
|
|
config &= ~perf_ibs->enable_mask;
|
|
|
|
wrmsrl(hwc->config_base, config);
|
2011-12-16 00:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot restore the ibs pmu state, so we always needs to update
|
|
|
|
* the event while stopping it and then reset the state when starting
|
|
|
|
* again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in
|
|
|
|
* perf_ibs_start()/perf_ibs_stop() and instead always do it.
|
|
|
|
*/
|
2011-12-16 00:56:38 +08:00
|
|
|
static void perf_ibs_start(struct perf_event *event, int flags)
|
|
|
|
{
|
|
|
|
struct hw_perf_event *hwc = &event->hw;
|
|
|
|
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
|
|
|
|
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
|
2012-04-03 02:19:16 +08:00
|
|
|
u64 period;
|
2011-12-16 00:56:38 +08:00
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
|
2011-12-16 00:56:38 +08:00
|
|
|
return;
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
|
|
|
|
hwc->state = 0;
|
|
|
|
|
2012-04-03 02:19:16 +08:00
|
|
|
perf_ibs_set_period(perf_ibs, hwc, &period);
|
2016-03-17 06:55:21 +08:00
|
|
|
/*
|
2016-03-21 18:47:52 +08:00
|
|
|
* Set STARTED before enabling the hardware, such that a subsequent NMI
|
|
|
|
* must observe it.
|
2016-03-17 06:55:21 +08:00
|
|
|
*/
|
2016-03-21 18:47:52 +08:00
|
|
|
set_bit(IBS_STARTED, pcpu->state);
|
2016-03-17 06:55:21 +08:00
|
|
|
clear_bit(IBS_STOPPING, pcpu->state);
|
2012-04-03 02:19:16 +08:00
|
|
|
perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
perf_event_update_userpage(event);
|
2011-12-16 00:56:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void perf_ibs_stop(struct perf_event *event, int flags)
|
|
|
|
{
|
|
|
|
struct hw_perf_event *hwc = &event->hw;
|
|
|
|
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
|
|
|
|
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
|
2012-04-03 02:19:16 +08:00
|
|
|
u64 config;
|
2011-12-16 00:56:39 +08:00
|
|
|
int stopping;
|
2011-12-16 00:56:38 +08:00
|
|
|
|
2016-03-21 18:47:52 +08:00
|
|
|
if (test_and_set_bit(IBS_STOPPING, pcpu->state))
|
|
|
|
return;
|
|
|
|
|
2016-03-17 06:55:21 +08:00
|
|
|
stopping = test_bit(IBS_STARTED, pcpu->state);
|
2011-12-16 00:56:38 +08:00
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
if (!stopping && (hwc->state & PERF_HES_UPTODATE))
|
|
|
|
return;
|
2011-12-16 00:56:38 +08:00
|
|
|
|
2012-04-03 02:19:16 +08:00
|
|
|
rdmsrl(hwc->config_base, config);
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
if (stopping) {
|
2016-03-17 06:55:21 +08:00
|
|
|
/*
|
2016-03-21 18:47:52 +08:00
|
|
|
* Set STOPPED before disabling the hardware, such that it
|
2016-03-17 06:55:21 +08:00
|
|
|
* must be visible to NMIs the moment we clear the EN bit,
|
|
|
|
* at which point we can generate an !VALID sample which
|
|
|
|
* we need to consume.
|
|
|
|
*/
|
2016-03-21 18:47:52 +08:00
|
|
|
set_bit(IBS_STOPPED, pcpu->state);
|
2012-04-03 02:19:16 +08:00
|
|
|
perf_ibs_disable_event(perf_ibs, hwc, config);
|
2016-03-17 06:55:21 +08:00
|
|
|
/*
|
|
|
|
* Clear STARTED after disabling the hardware; if it were
|
|
|
|
* cleared before an NMI hitting after the clear but before
|
|
|
|
* clearing the EN bit might think it a spurious NMI and not
|
|
|
|
* handle it.
|
|
|
|
*
|
|
|
|
* Clearing it after, however, creates the problem of the NMI
|
|
|
|
* handler seeing STARTED but not having a valid sample.
|
|
|
|
*/
|
|
|
|
clear_bit(IBS_STARTED, pcpu->state);
|
2011-12-16 00:56:39 +08:00
|
|
|
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
|
|
|
|
hwc->state |= PERF_HES_STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hwc->state & PERF_HES_UPTODATE)
|
|
|
|
return;
|
|
|
|
|
2012-04-03 02:19:18 +08:00
|
|
|
/*
|
|
|
|
* Clear valid bit to not count rollovers on update, rollovers
|
|
|
|
* are only updated in the irq handler.
|
|
|
|
*/
|
|
|
|
config &= ~perf_ibs->valid_mask;
|
|
|
|
|
2012-04-03 02:19:16 +08:00
|
|
|
perf_ibs_event_update(perf_ibs, event, &config);
|
2011-12-16 00:56:39 +08:00
|
|
|
hwc->state |= PERF_HES_UPTODATE;
|
2011-12-16 00:56:38 +08:00
|
|
|
}
|
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
static int perf_ibs_add(struct perf_event *event, int flags)
|
|
|
|
{
|
2011-12-16 00:56:38 +08:00
|
|
|
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
|
|
|
|
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
|
|
|
|
|
|
|
|
if (test_and_set_bit(IBS_ENABLED, pcpu->state))
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
|
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
pcpu->event = event;
|
|
|
|
|
|
|
|
if (flags & PERF_EF_START)
|
|
|
|
perf_ibs_start(event, PERF_EF_RELOAD);
|
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void perf_ibs_del(struct perf_event *event, int flags)
|
|
|
|
{
|
2011-12-16 00:56:38 +08:00
|
|
|
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
|
|
|
|
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
|
|
|
|
|
|
|
|
if (!test_and_clear_bit(IBS_ENABLED, pcpu->state))
|
|
|
|
return;
|
|
|
|
|
2011-12-16 00:56:39 +08:00
|
|
|
perf_ibs_stop(event, PERF_EF_UPDATE);
|
2011-12-16 00:56:38 +08:00
|
|
|
|
|
|
|
pcpu->event = NULL;
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
perf_event_update_userpage(event);
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
static void perf_ibs_read(struct perf_event *event) { }
|
|
|
|
|
2012-09-12 18:59:44 +08:00
|
|
|
PMU_FORMAT_ATTR(rand_en, "config:57");
|
|
|
|
PMU_FORMAT_ATTR(cnt_ctl, "config:19");
|
|
|
|
|
|
|
|
static struct attribute *ibs_fetch_format_attrs[] = {
|
|
|
|
&format_attr_rand_en.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct attribute *ibs_op_format_attrs[] = {
|
|
|
|
NULL, /* &format_attr_cnt_ctl.attr if IBS_CAPS_OPCNT */
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2011-12-16 00:56:36 +08:00
|
|
|
static struct perf_ibs perf_ibs_fetch = {
|
|
|
|
.pmu = {
|
|
|
|
.task_ctx_nr = perf_invalid_context,
|
|
|
|
|
|
|
|
.event_init = perf_ibs_init,
|
|
|
|
.add = perf_ibs_add,
|
|
|
|
.del = perf_ibs_del,
|
2011-12-16 00:56:38 +08:00
|
|
|
.start = perf_ibs_start,
|
|
|
|
.stop = perf_ibs_stop,
|
|
|
|
.read = perf_ibs_read,
|
2011-12-16 00:56:36 +08:00
|
|
|
},
|
|
|
|
.msr = MSR_AMD64_IBSFETCHCTL,
|
|
|
|
.config_mask = IBS_FETCH_CONFIG_MASK,
|
|
|
|
.cnt_mask = IBS_FETCH_MAX_CNT,
|
|
|
|
.enable_mask = IBS_FETCH_ENABLE,
|
2011-12-16 00:56:37 +08:00
|
|
|
.valid_mask = IBS_FETCH_VAL,
|
2011-12-16 00:56:39 +08:00
|
|
|
.max_period = IBS_FETCH_MAX_CNT << 4,
|
2011-12-16 00:56:37 +08:00
|
|
|
.offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK },
|
|
|
|
.offset_max = MSR_AMD64_IBSFETCH_REG_COUNT,
|
2012-09-12 18:59:44 +08:00
|
|
|
.format_attrs = ibs_fetch_format_attrs,
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
.get_count = get_ibs_fetch_count,
|
2011-12-16 00:56:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct perf_ibs perf_ibs_op = {
|
|
|
|
.pmu = {
|
|
|
|
.task_ctx_nr = perf_invalid_context,
|
|
|
|
|
|
|
|
.event_init = perf_ibs_init,
|
|
|
|
.add = perf_ibs_add,
|
|
|
|
.del = perf_ibs_del,
|
2011-12-16 00:56:38 +08:00
|
|
|
.start = perf_ibs_start,
|
|
|
|
.stop = perf_ibs_stop,
|
|
|
|
.read = perf_ibs_read,
|
2011-12-16 00:56:36 +08:00
|
|
|
},
|
|
|
|
.msr = MSR_AMD64_IBSOPCTL,
|
|
|
|
.config_mask = IBS_OP_CONFIG_MASK,
|
|
|
|
.cnt_mask = IBS_OP_MAX_CNT,
|
|
|
|
.enable_mask = IBS_OP_ENABLE,
|
2011-12-16 00:56:37 +08:00
|
|
|
.valid_mask = IBS_OP_VAL,
|
2011-12-16 00:56:39 +08:00
|
|
|
.max_period = IBS_OP_MAX_CNT << 4,
|
2011-12-16 00:56:37 +08:00
|
|
|
.offset_mask = { MSR_AMD64_IBSOP_REG_MASK },
|
|
|
|
.offset_max = MSR_AMD64_IBSOP_REG_COUNT,
|
2012-09-12 18:59:44 +08:00
|
|
|
.format_attrs = ibs_op_format_attrs,
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
.get_count = get_ibs_op_count,
|
2011-09-21 17:30:18 +08:00
|
|
|
};
|
|
|
|
|
2011-12-16 00:56:37 +08:00
|
|
|
static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
|
|
|
|
{
|
2011-12-16 00:56:38 +08:00
|
|
|
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
|
|
|
|
struct perf_event *event = pcpu->event;
|
2011-12-16 00:56:37 +08:00
|
|
|
struct hw_perf_event *hwc = &event->hw;
|
|
|
|
struct perf_sample_data data;
|
|
|
|
struct perf_raw_record raw;
|
|
|
|
struct pt_regs regs;
|
|
|
|
struct perf_ibs_data ibs_data;
|
2012-04-03 02:19:11 +08:00
|
|
|
int offset, size, check_rip, offset_max, throttle = 0;
|
2011-12-16 00:56:37 +08:00
|
|
|
unsigned int msr;
|
2012-04-03 02:19:16 +08:00
|
|
|
u64 *buf, *config, period;
|
2011-12-16 00:56:37 +08:00
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
if (!test_bit(IBS_STARTED, pcpu->state)) {
|
2016-03-17 06:55:21 +08:00
|
|
|
fail:
|
2012-04-03 02:19:17 +08:00
|
|
|
/*
|
|
|
|
* Catch spurious interrupts after stopping IBS: After
|
2012-12-28 00:33:02 +08:00
|
|
|
* disabling IBS there could be still incoming NMIs
|
2012-04-03 02:19:17 +08:00
|
|
|
* with samples that even have the valid bit cleared.
|
|
|
|
* Mark all this NMIs as handled.
|
|
|
|
*/
|
2016-03-21 18:47:52 +08:00
|
|
|
if (test_and_clear_bit(IBS_STOPPED, pcpu->state))
|
2016-03-17 06:55:21 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2011-12-16 00:56:38 +08:00
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:37 +08:00
|
|
|
msr = hwc->config_base;
|
|
|
|
buf = ibs_data.regs;
|
|
|
|
rdmsrl(msr, *buf);
|
|
|
|
if (!(*buf++ & perf_ibs->valid_mask))
|
2016-03-17 06:55:21 +08:00
|
|
|
goto fail;
|
2011-12-16 00:56:37 +08:00
|
|
|
|
2012-04-03 02:19:16 +08:00
|
|
|
config = &ibs_data.regs[0];
|
2012-04-03 02:19:07 +08:00
|
|
|
perf_ibs_event_update(perf_ibs, event, config);
|
2012-04-03 02:19:08 +08:00
|
|
|
perf_sample_data_init(&data, 0, hwc->last_period);
|
2012-04-03 02:19:16 +08:00
|
|
|
if (!perf_ibs_set_period(perf_ibs, hwc, &period))
|
2012-04-03 02:19:11 +08:00
|
|
|
goto out; /* no sw counter overflow */
|
|
|
|
|
|
|
|
ibs_data.caps = ibs_caps;
|
|
|
|
size = 1;
|
|
|
|
offset = 1;
|
|
|
|
check_rip = (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_RIPINVALIDCHK));
|
|
|
|
if (event->attr.sample_type & PERF_SAMPLE_RAW)
|
|
|
|
offset_max = perf_ibs->offset_max;
|
|
|
|
else if (check_rip)
|
|
|
|
offset_max = 2;
|
|
|
|
else
|
|
|
|
offset_max = 1;
|
|
|
|
do {
|
|
|
|
rdmsrl(msr + offset, *buf++);
|
|
|
|
size++;
|
|
|
|
offset = find_next_bit(perf_ibs->offset_mask,
|
|
|
|
perf_ibs->offset_max,
|
|
|
|
offset + 1);
|
|
|
|
} while (offset < offset_max);
|
2014-11-11 04:24:26 +08:00
|
|
|
if (event->attr.sample_type & PERF_SAMPLE_RAW) {
|
|
|
|
/*
|
|
|
|
* Read IbsBrTarget and IbsOpData4 separately
|
|
|
|
* depending on their availability.
|
|
|
|
* Can't add to offset_max as they are staggered
|
|
|
|
*/
|
|
|
|
if (ibs_caps & IBS_CAPS_BRNTRGT) {
|
|
|
|
rdmsrl(MSR_AMD64_IBSBRTARGET, *buf++);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
if (ibs_caps & IBS_CAPS_OPDATA4) {
|
|
|
|
rdmsrl(MSR_AMD64_IBSOPDATA4, *buf++);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
}
|
2012-04-03 02:19:11 +08:00
|
|
|
ibs_data.size = sizeof(u64) * size;
|
|
|
|
|
|
|
|
regs = *iregs;
|
2012-03-12 19:54:32 +08:00
|
|
|
if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
|
|
|
|
regs.flags &= ~PERF_EFLAGS_EXACT;
|
|
|
|
} else {
|
2012-07-10 15:42:15 +08:00
|
|
|
set_linear_ip(®s, ibs_data.regs[1]);
|
2012-03-12 19:54:32 +08:00
|
|
|
regs.flags |= PERF_EFLAGS_EXACT;
|
|
|
|
}
|
2012-04-03 02:19:07 +08:00
|
|
|
|
2011-12-16 00:56:37 +08:00
|
|
|
if (event->attr.sample_type & PERF_SAMPLE_RAW) {
|
perf, events: add non-linear data support for raw records
This patch adds support for non-linear data on raw records. It
extends raw records to have one or multiple fragments that will
be written linearly into the ring slot, where each fragment can
optionally have a custom callback handler to walk and extract
complex, possibly non-linear data.
If a callback handler is provided for a fragment, then the new
__output_custom() will be used instead of __output_copy() for
the perf_output_sample() part. perf_prepare_sample() does all
the size calculation only once, so perf_output_sample() doesn't
need to redo the same work anymore, meaning real_size and padding
will be cached in the raw record. The raw record becomes 32 bytes
in size without holes; to not increase it further and to avoid
doing unnecessary recalculations in fast-path, we can reuse
next pointer of the last fragment, idea here is borrowed from
ZERO_OR_NULL_PTR(), which should keep the perf_output_sample()
path for PERF_SAMPLE_RAW minimal.
This facility is needed for BPF's event output helper as a first
user that will, in a follow-up, add an additional perf_raw_frag
to its perf_raw_record in order to be able to more efficiently
dump skb context after a linear head meta data related to it.
skbs can be non-linear and thus need a custom output function to
dump buffers. Currently, the skb data needs to be copied twice;
with the help of __output_custom() this work only needs to be
done once. Future users could be things like XDP/BPF programs
that work on different context though and would thus also have
a different callback function.
The few users of raw records are adapted to initialize their frag
data from the raw record itself, no change in behavior for them.
The code is based upon a PoC diff provided by Peter Zijlstra [1].
[1] http://thread.gmane.org/gmane.linux.network/421294
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-07-15 00:08:03 +08:00
|
|
|
raw = (struct perf_raw_record){
|
|
|
|
.frag = {
|
|
|
|
.size = sizeof(u32) + ibs_data.size,
|
|
|
|
.data = ibs_data.data,
|
|
|
|
},
|
|
|
|
};
|
2011-12-16 00:56:37 +08:00
|
|
|
data.raw = &raw;
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:19:11 +08:00
|
|
|
throttle = perf_event_overflow(event, &data, ®s);
|
|
|
|
out:
|
2012-04-03 02:19:16 +08:00
|
|
|
if (throttle)
|
2016-03-11 22:23:46 +08:00
|
|
|
perf_ibs_stop(event, 0);
|
2012-04-03 02:19:16 +08:00
|
|
|
else
|
|
|
|
perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
|
2011-12-16 00:56:39 +08:00
|
|
|
|
|
|
|
perf_event_update_userpage(event);
|
2011-12-16 00:56:37 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-17 16:18:14 +08:00
|
|
|
static int
|
2011-12-16 00:56:37 +08:00
|
|
|
perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
|
|
|
|
{
|
2016-03-17 22:06:58 +08:00
|
|
|
u64 stamp = sched_clock();
|
2011-12-16 00:56:37 +08:00
|
|
|
int handled = 0;
|
|
|
|
|
|
|
|
handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
|
|
|
|
handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
|
|
|
|
|
|
|
|
if (handled)
|
|
|
|
inc_irq_stat(apic_perf_irqs);
|
|
|
|
|
2016-03-17 22:06:58 +08:00
|
|
|
perf_sample_event_took(sched_clock() - stamp);
|
|
|
|
|
2011-12-16 00:56:37 +08:00
|
|
|
return handled;
|
|
|
|
}
|
2014-04-17 16:18:14 +08:00
|
|
|
NOKPROBE_SYMBOL(perf_ibs_nmi_handler);
|
2011-12-16 00:56:37 +08:00
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
|
|
|
|
{
|
|
|
|
struct cpu_perf_ibs __percpu *pcpu;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pcpu = alloc_percpu(struct cpu_perf_ibs);
|
|
|
|
if (!pcpu)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
perf_ibs->pcpu = pcpu;
|
|
|
|
|
2012-09-12 18:59:44 +08:00
|
|
|
/* register attributes */
|
|
|
|
if (perf_ibs->format_attrs[0]) {
|
|
|
|
memset(&perf_ibs->format_group, 0, sizeof(perf_ibs->format_group));
|
|
|
|
perf_ibs->format_group.name = "format";
|
|
|
|
perf_ibs->format_group.attrs = perf_ibs->format_attrs;
|
|
|
|
|
|
|
|
memset(&perf_ibs->attr_groups, 0, sizeof(perf_ibs->attr_groups));
|
|
|
|
perf_ibs->attr_groups[0] = &perf_ibs->format_group;
|
|
|
|
perf_ibs->pmu.attr_groups = perf_ibs->attr_groups;
|
|
|
|
}
|
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
|
|
|
|
if (ret) {
|
|
|
|
perf_ibs->pcpu = NULL;
|
|
|
|
free_percpu(pcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static __init void perf_event_ibs_init(void)
|
2011-09-21 17:30:18 +08:00
|
|
|
{
|
2012-09-12 18:59:44 +08:00
|
|
|
struct attribute **attr = ibs_op_format_attrs;
|
|
|
|
|
2011-12-16 00:56:38 +08:00
|
|
|
perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
|
2012-09-12 18:59:44 +08:00
|
|
|
|
|
|
|
if (ibs_caps & IBS_CAPS_OPCNT) {
|
2012-04-03 02:19:09 +08:00
|
|
|
perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
|
2012-09-12 18:59:44 +08:00
|
|
|
*attr++ = &format_attr_cnt_ctl.attr;
|
|
|
|
}
|
2011-12-16 00:56:38 +08:00
|
|
|
perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
|
2012-09-12 18:59:44 +08:00
|
|
|
|
2012-04-25 18:55:22 +08:00
|
|
|
register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
|
2016-02-02 11:45:02 +08:00
|
|
|
pr_info("perf: AMD IBS detected (0x%08x)\n", ibs_caps);
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static __init void perf_event_ibs_init(void) { }
|
2011-09-21 17:30:18 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* IBS - apic initialization, for perf and oprofile */
|
|
|
|
|
|
|
|
static __init u32 __get_ibs_caps(void)
|
|
|
|
{
|
|
|
|
u32 caps;
|
|
|
|
unsigned int max_level;
|
|
|
|
|
|
|
|
if (!boot_cpu_has(X86_FEATURE_IBS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check IBS cpuid feature flags */
|
|
|
|
max_level = cpuid_eax(0x80000000);
|
|
|
|
if (max_level < IBS_CPUID_FEATURES)
|
|
|
|
return IBS_CAPS_DEFAULT;
|
|
|
|
|
|
|
|
caps = cpuid_eax(IBS_CPUID_FEATURES);
|
|
|
|
if (!(caps & IBS_CAPS_AVAIL))
|
|
|
|
/* cpuid flags not valid */
|
|
|
|
return IBS_CAPS_DEFAULT;
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 get_ibs_caps(void)
|
|
|
|
{
|
|
|
|
return ibs_caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(get_ibs_caps);
|
|
|
|
|
|
|
|
static inline int get_eilvt(int offset)
|
|
|
|
{
|
|
|
|
return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int put_eilvt(int offset)
|
|
|
|
{
|
|
|
|
return !setup_APIC_eilvt(offset, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check and reserve APIC extended interrupt LVT offset for IBS if available.
|
|
|
|
*/
|
|
|
|
static inline int ibs_eilvt_valid(void)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
u64 val;
|
|
|
|
int valid = 0;
|
|
|
|
|
|
|
|
preempt_disable();
|
|
|
|
|
|
|
|
rdmsrl(MSR_AMD64_IBSCTL, val);
|
|
|
|
offset = val & IBSCTL_LVT_OFFSET_MASK;
|
|
|
|
|
|
|
|
if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
|
|
|
|
pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
|
|
|
|
smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!get_eilvt(offset)) {
|
|
|
|
pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
|
|
|
|
smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = 1;
|
|
|
|
out:
|
|
|
|
preempt_enable();
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setup_ibs_ctl(int ibs_eilvt_off)
|
|
|
|
{
|
|
|
|
struct pci_dev *cpu_cfg;
|
|
|
|
int nodes;
|
|
|
|
u32 value = 0;
|
|
|
|
|
|
|
|
nodes = 0;
|
|
|
|
cpu_cfg = NULL;
|
|
|
|
do {
|
|
|
|
cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
|
|
|
|
PCI_DEVICE_ID_AMD_10H_NB_MISC,
|
|
|
|
cpu_cfg);
|
|
|
|
if (!cpu_cfg)
|
|
|
|
break;
|
|
|
|
++nodes;
|
|
|
|
pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
|
|
|
|
| IBSCTL_LVT_OFFSET_VALID);
|
|
|
|
pci_read_config_dword(cpu_cfg, IBSCTL, &value);
|
|
|
|
if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
|
|
|
|
pci_dev_put(cpu_cfg);
|
2016-02-02 11:45:02 +08:00
|
|
|
pr_debug("Failed to setup IBS LVT offset, IBSCTL = 0x%08x\n",
|
|
|
|
value);
|
2011-09-21 17:30:18 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (!nodes) {
|
2016-02-02 11:45:02 +08:00
|
|
|
pr_debug("No CPU node configured for IBS\n");
|
2011-09-21 17:30:18 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This runs only on the current cpu. We try to find an LVT offset and
|
|
|
|
* setup the local APIC. For this we must disable preemption. On
|
|
|
|
* success we initialize all nodes with this offset. This updates then
|
|
|
|
* the offset in the IBS_CTL per-node msr. The per-core APIC setup of
|
|
|
|
* the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
|
|
|
|
* is using the new offset.
|
|
|
|
*/
|
2015-01-24 02:19:35 +08:00
|
|
|
static void force_ibs_eilvt_setup(void)
|
2011-09-21 17:30:18 +08:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
preempt_disable();
|
|
|
|
/* find the next free available EILVT entry, skip offset 0 */
|
|
|
|
for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
|
|
|
|
if (get_eilvt(offset))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
preempt_enable();
|
|
|
|
|
|
|
|
if (offset == APIC_EILVT_NR_MAX) {
|
2016-02-02 11:45:02 +08:00
|
|
|
pr_debug("No EILVT entry available\n");
|
2015-01-24 02:19:35 +08:00
|
|
|
return;
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = setup_ibs_ctl(offset);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2015-01-24 02:19:35 +08:00
|
|
|
if (!ibs_eilvt_valid())
|
2011-09-21 17:30:18 +08:00
|
|
|
goto out;
|
|
|
|
|
2011-11-09 02:20:44 +08:00
|
|
|
pr_info("IBS: LVT offset %d assigned\n", offset);
|
2011-09-21 17:30:18 +08:00
|
|
|
|
2015-01-24 02:19:35 +08:00
|
|
|
return;
|
2011-09-21 17:30:18 +08:00
|
|
|
out:
|
|
|
|
preempt_disable();
|
|
|
|
put_eilvt(offset);
|
|
|
|
preempt_enable();
|
2015-01-24 02:19:35 +08:00
|
|
|
return;
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 22:57:29 +08:00
|
|
|
static void ibs_eilvt_setup(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Force LVT offset assignment for family 10h: The offsets are
|
|
|
|
* not assigned by the BIOS for this family, so the OS is
|
|
|
|
* responsible for doing it. If the OS assignment fails, fall
|
|
|
|
* back to BIOS settings and try to setup this.
|
|
|
|
*/
|
|
|
|
if (boot_cpu_data.x86 == 0x10)
|
|
|
|
force_ibs_eilvt_setup();
|
|
|
|
}
|
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
static inline int get_ibs_lvt_offset(void)
|
|
|
|
{
|
|
|
|
u64 val;
|
|
|
|
|
|
|
|
rdmsrl(MSR_AMD64_IBSCTL, val);
|
|
|
|
if (!(val & IBSCTL_LVT_OFFSET_VALID))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return val & IBSCTL_LVT_OFFSET_MASK;
|
|
|
|
}
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static void setup_APIC_ibs(void)
|
2011-09-21 17:30:18 +08:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
offset = get_ibs_lvt_offset();
|
|
|
|
if (offset < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
|
|
|
|
return;
|
|
|
|
failed:
|
|
|
|
pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
|
|
|
|
smp_processor_id());
|
|
|
|
}
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static void clear_APIC_ibs(void)
|
2011-09-21 17:30:18 +08:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
offset = get_ibs_lvt_offset();
|
|
|
|
if (offset >= 0)
|
|
|
|
setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
|
|
|
|
}
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static int x86_pmu_amd_ibs_starting_cpu(unsigned int cpu)
|
|
|
|
{
|
|
|
|
setup_APIC_ibs();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:57:29 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
|
|
|
|
static int perf_ibs_suspend(void)
|
|
|
|
{
|
2016-07-14 01:16:14 +08:00
|
|
|
clear_APIC_ibs();
|
2014-01-15 22:57:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void perf_ibs_resume(void)
|
|
|
|
{
|
|
|
|
ibs_eilvt_setup();
|
2016-07-14 01:16:14 +08:00
|
|
|
setup_APIC_ibs();
|
2014-01-15 22:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct syscore_ops perf_ibs_syscore_ops = {
|
|
|
|
.resume = perf_ibs_resume,
|
|
|
|
.suspend = perf_ibs_suspend,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void perf_ibs_pm_init(void)
|
|
|
|
{
|
|
|
|
register_syscore_ops(&perf_ibs_syscore_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static inline void perf_ibs_pm_init(void) { }
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
static int x86_pmu_amd_ibs_dying_cpu(unsigned int cpu)
|
2011-09-21 17:30:18 +08:00
|
|
|
{
|
2016-07-14 01:16:14 +08:00
|
|
|
clear_APIC_ibs();
|
|
|
|
return 0;
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static __init int amd_ibs_init(void)
|
|
|
|
{
|
|
|
|
u32 caps;
|
|
|
|
|
|
|
|
caps = __get_ibs_caps();
|
|
|
|
if (!caps)
|
|
|
|
return -ENODEV; /* ibs not supported by the cpu */
|
|
|
|
|
2014-01-15 22:57:29 +08:00
|
|
|
ibs_eilvt_setup();
|
2011-11-09 02:20:44 +08:00
|
|
|
|
|
|
|
if (!ibs_eilvt_valid())
|
2016-07-14 01:16:14 +08:00
|
|
|
return -EINVAL;
|
2011-09-21 17:30:18 +08:00
|
|
|
|
2014-01-15 22:57:29 +08:00
|
|
|
perf_ibs_pm_init();
|
2016-07-14 01:16:14 +08:00
|
|
|
|
2011-09-21 17:30:18 +08:00
|
|
|
ibs_caps = caps;
|
|
|
|
/* make ibs_caps visible to other cpus: */
|
|
|
|
smp_mb();
|
2016-07-14 01:16:14 +08:00
|
|
|
/*
|
|
|
|
* x86_pmu_amd_ibs_starting_cpu will be called from core on
|
|
|
|
* all online cpus.
|
|
|
|
*/
|
|
|
|
cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_IBS_STARTING,
|
2016-12-26 18:05:11 +08:00
|
|
|
"perf/x86/amd/ibs:starting",
|
2016-07-14 01:16:14 +08:00
|
|
|
x86_pmu_amd_ibs_starting_cpu,
|
|
|
|
x86_pmu_amd_ibs_dying_cpu);
|
2011-09-21 17:30:18 +08:00
|
|
|
|
2016-07-14 01:16:14 +08:00
|
|
|
perf_event_ibs_init();
|
|
|
|
|
|
|
|
return 0;
|
2011-09-21 17:30:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Since we need the pci subsystem to init ibs we can't do this earlier: */
|
|
|
|
device_initcall(amd_ibs_init);
|