15 #include <linux/perf_event.h>
18 #include <linux/kernel.h>
22 #include <asm/stacktrace.h>
24 #include <asm/uaccess.h>
28 #include <asm/cacheflush.h>
68 #define MAX_HWEVENTS 4
70 #define MAX_PERIOD ((1UL << 32) - 1)
72 #define PIC_UPPER_INDEX 0
73 #define PIC_LOWER_INDEX 1
74 #define PIC_NO_INDEX -1
124 #define PIC_NONE 0x00
125 #define PIC_UPPER 0x01
126 #define PIC_LOWER 0x02
130 static unsigned long perf_event_encode(
const struct perf_event_map *pmap)
135 static u8 perf_event_get_msk(
unsigned long val)
140 static u64 perf_event_get_enc(
unsigned long val)
145 #define C(x) PERF_COUNT_HW_CACHE_##x
147 #define CACHE_OP_UNSUPPORTED 0xfffe
148 #define CACHE_OP_NONSENSE 0xffff
171 #define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001
172 #define SPARC_PMU_HAS_CONFLICTS 0x00000002
178 static u32 sparc_default_read_pmc(
int idx)
186 return val & 0xffffffff;
189 static void sparc_default_write_pmc(
int idx,
u64 val)
197 mask = ((
u64) 0xffffffff) << shift;
215 return &ultra3_perfmon_event_map[
event_id];
242 [
C(OP_PREFETCH) ] = {
270 [
C(OP_PREFETCH) ] = {
284 [
C(OP_PREFETCH) ] = {
298 [
C(OP_PREFETCH) ] = {
312 [
C(OP_PREFETCH) ] = {
319 static const struct sparc_pmu ultra3_pmu = {
320 .event_map = ultra3_event_map,
321 .cache_map = &ultra3_cache_map,
322 .max_events =
ARRAY_SIZE(ultra3_perfmon_event_map),
323 .read_pmc = sparc_default_read_pmc,
324 .write_pmc = sparc_default_write_pmc,
344 static const struct perf_event_map niagara1_perfmon_event_map[] = {
353 return &niagara1_perfmon_event_map[
event_id];
380 [
C(OP_PREFETCH) ] = {
408 [
C(OP_PREFETCH) ] = {
422 [
C(OP_PREFETCH) ] = {
436 [
C(OP_PREFETCH) ] = {
450 [
C(OP_PREFETCH) ] = {
457 static const struct sparc_pmu niagara1_pmu = {
458 .event_map = niagara1_event_map,
459 .cache_map = &niagara1_cache_map,
460 .max_events =
ARRAY_SIZE(niagara1_perfmon_event_map),
461 .read_pmc = sparc_default_read_pmc,
462 .write_pmc = sparc_default_write_pmc,
477 static const struct perf_event_map niagara2_perfmon_event_map[] = {
488 return &niagara2_perfmon_event_map[
event_id];
515 [
C(OP_PREFETCH) ] = {
543 [
C(OP_PREFETCH) ] = {
557 [
C(OP_PREFETCH) ] = {
571 [
C(OP_PREFETCH) ] = {
585 [
C(OP_PREFETCH) ] = {
592 static const struct sparc_pmu niagara2_pmu = {
593 .event_map = niagara2_event_map,
594 .cache_map = &niagara2_cache_map,
595 .max_events =
ARRAY_SIZE(niagara2_perfmon_event_map),
596 .read_pmc = sparc_default_read_pmc,
597 .write_pmc = sparc_default_write_pmc,
614 static const struct perf_event_map niagara4_perfmon_event_map[] = {
625 return &niagara4_perfmon_event_map[
event_id];
631 [
C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
632 [
C(RESULT_MISS)] = { (16 << 6) | 0x07 },
635 [
C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
636 [
C(RESULT_MISS)] = { (16 << 6) | 0x07 },
645 [
C(RESULT_ACCESS)] = { (3 << 6) | 0x3f },
646 [
C(RESULT_MISS)] = { (11 << 6) | 0x03 },
652 [
C(OP_PREFETCH) ] = {
659 [
C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
663 [
C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
674 [
C(RESULT_MISS)] = { (17 << 6) | 0x3f },
680 [
C(OP_PREFETCH) ] = {
688 [
C(RESULT_MISS)] = { (6 << 6) | 0x3f },
694 [
C(OP_PREFETCH) ] = {
708 [
C(OP_PREFETCH) ] = {
722 [
C(OP_PREFETCH) ] = {
729 static u32 sparc_vt_read_pmc(
int idx)
733 return val & 0xffffffff;
736 static void sparc_vt_write_pmc(
int idx,
u64 val)
765 static const struct sparc_pmu niagara4_pmu = {
766 .event_map = niagara4_event_map,
767 .cache_map = &niagara4_cache_map,
768 .max_events =
ARRAY_SIZE(niagara4_perfmon_event_map),
769 .read_pmc = sparc_vt_read_pmc,
770 .write_pmc = sparc_vt_write_pmc,
806 static u64 mask_for_index(
int idx)
811 static u64 nop_for_index(
int idx)
820 u64 enc,
val, mask = mask_for_index(idx);
826 enc = perf_event_get_enc(cpuc->
events[idx]);
828 val = cpuc->
pcr[pcr_index];
830 val |= event_encoding(enc, idx);
831 cpuc->
pcr[pcr_index] =
val;
838 u64 mask = mask_for_index(idx);
839 u64 nop = nop_for_index(idx);
846 val = cpuc->
pcr[pcr_index];
849 cpuc->
pcr[pcr_index] =
val;
858 u64 prev_raw_count, new_raw_count;
866 new_raw_count) != prev_raw_count)
869 delta = (new_raw_count << shift) - (prev_raw_count << shift);
875 return new_raw_count;
878 static int sparc_perf_event_set_period(
struct perf_event *event,
888 hwc->last_period =
period;
895 hwc->last_period =
period;
914 for (i = 0; i < cpuc->
n_events; i++) {
919 sparc_perf_event_update(cp, &cp->hw,
940 for (i = 0; i < cpuc->
n_events; i++) {
949 sparc_perf_event_set_period(cp, hwc, idx);
952 enc = perf_event_get_enc(cpuc->
events[i]);
953 cpuc->
pcr[0] &= ~mask_for_index(idx);
955 cpuc->
pcr[0] |= nop_for_index(idx);
957 cpuc->
pcr[0] |= event_encoding(enc, idx);
960 cpuc->
pcr[0] |= cpuc->
event[0]->hw.config_base;
964 static void calculate_multiple_pcrs(
struct cpu_hw_events *cpuc)
971 for (i = 0; i < cpuc->
n_events; i++) {
980 sparc_perf_event_set_period(cp, hwc, idx);
983 enc = perf_event_get_enc(cpuc->
events[i]);
984 cpuc->
pcr[
idx] &= ~mask_for_index(idx);
986 cpuc->
pcr[
idx] |= nop_for_index(idx);
988 cpuc->
pcr[
idx] |= event_encoding(enc, idx);
991 for (i = 0; i < cpuc->
n_events; i++) {
993 int idx = cp->hw.idx;
995 cpuc->
pcr[
idx] |= cp->hw.config_base;
1002 static void update_pcrs_for_enable(
struct cpu_hw_events *cpuc)
1005 read_in_all_counters(cpuc);
1008 calculate_single_pcr(cpuc);
1010 calculate_multiple_pcrs(cpuc);
1014 static void sparc_pmu_enable(
struct pmu *
pmu)
1026 update_pcrs_for_enable(cpuc);
1032 static void sparc_pmu_disable(
struct pmu *pmu)
1058 for (i = 0; i < cpuc->
n_events; i++) {
1059 if (cpuc->
event[i] == event)
1069 int idx = active_event_index(cpuc, event);
1073 sparc_perf_event_set_period(event, &event->hw, idx);
1076 event->hw.state = 0;
1078 sparc_pmu_enable_event(cpuc, &event->hw, idx);
1081 static void sparc_pmu_stop(
struct perf_event *event,
int flags)
1084 int idx = active_event_index(cpuc, event);
1087 sparc_pmu_disable_event(cpuc, &event->hw, idx);
1092 sparc_perf_event_update(event, &event->hw, idx);
1097 static void sparc_pmu_del(
struct perf_event *event,
int _flags)
1100 unsigned long flags;
1106 for (i = 0; i < cpuc->
n_events; i++) {
1107 if (event == cpuc->
event[i]) {
1134 static void sparc_pmu_read(
struct perf_event *event)
1137 int idx = active_event_index(cpuc, event);
1140 sparc_perf_event_update(event, hwc, idx);
1146 static void perf_stop_nmi_watchdog(
void *
unused)
1189 cache_type = (config >> 0) & 0xff;
1193 cache_op = (config >> 8) & 0xff;
1197 cache_result = (config >> 16) & 0xff;
1212 static void hw_perf_event_destroy(
struct perf_event *event)
1228 unsigned long *
events,
int n_ev)
1230 u8 msk0 = 0, msk1 = 0;
1245 for (i = 0; i < n_ev; i++)
1246 evts[i]->
hw.idx = i;
1250 msk0 = perf_event_get_msk(events[0]);
1257 msk1 = perf_event_get_msk(events[1]);
1293 evts[0]->hw.idx = idx0;
1295 evts[1]->hw.idx = idx0 ^ 1;
1299 static int check_excludes(
struct perf_event **evts,
int n_prev,
int n_new)
1301 int eu = 0, ek = 0, eh = 0;
1313 for (i = 0; i <
n; i++) {
1316 eu =
event->
attr.exclude_user;
1317 ek =
event->attr.exclude_kernel;
1318 eh =
event->attr.exclude_hv;
1320 }
else if (event->
attr.exclude_user != eu ||
1321 event->
attr.exclude_kernel != ek ||
1322 event->
attr.exclude_hv != eh) {
1331 struct perf_event *evts[],
unsigned long *events,
1337 if (!is_software_event(group)) {
1341 events[
n] = group->hw.event_base;
1345 if (!is_software_event(event) &&
1350 events[
n] =
event->hw.event_base;
1357 static int sparc_pmu_add(
struct perf_event *event,
int ef_flags)
1361 unsigned long flags;
1371 cpuc->
events[n0] =
event->hw.event_base;
1386 if (check_excludes(cpuc->
event, n0, 1))
1388 if (sparc_check_constraints(cpuc->
event, cpuc->
events, n0 + 1))
1402 static int sparc_pmu_event_init(
struct perf_event *event)
1416 if (has_branch_stack(event))
1419 switch (attr->
type) {
1427 pmap = sparc_map_cache_event(attr->
config);
1429 return PTR_ERR(pmap);
1442 hwc->event_base = perf_event_encode(pmap);
1448 hwc->event_base = attr->
config;
1461 if (event->group_leader != event) {
1462 n = collect_events(event->group_leader,
1464 evts, events, current_idx_dmy);
1468 events[
n] = hwc->event_base;
1471 if (check_excludes(evts, n, 1))
1474 if (sparc_check_constraints(evts, events, n + 1))
1483 event->destroy = hw_perf_event_destroy;
1485 if (!hwc->sample_period) {
1487 hwc->last_period = hwc->sample_period;
1488 local64_set(&hwc->period_left, hwc->sample_period);
1499 static void sparc_pmu_start_txn(
struct pmu *pmu)
1512 static void sparc_pmu_cancel_txn(
struct pmu *pmu)
1525 static int sparc_pmu_commit_txn(
struct pmu *pmu)
1535 if (check_excludes(cpuc->
event, 0, n))
1537 if (sparc_check_constraints(cpuc->
event, cpuc->
events, n))
1545 static struct pmu pmu = {
1547 .pmu_disable = sparc_pmu_disable,
1548 .event_init = sparc_pmu_event_init,
1549 .add = sparc_pmu_add,
1550 .del = sparc_pmu_del,
1551 .start = sparc_pmu_start,
1552 .stop = sparc_pmu_stop,
1553 .read = sparc_pmu_read,
1554 .start_txn = sparc_pmu_start_txn,
1555 .cancel_txn = sparc_pmu_cancel_txn,
1556 .commit_txn = sparc_pmu_commit_txn,
1561 unsigned long flags;
1573 pr_info(
"CPU#%d: PCR%d[%016llx]\n",
1576 pr_info(
"CPU#%d: PIC%d[%016llx]\n",
1583 unsigned long cmd,
void *__args)
1586 struct perf_sample_data
data;
1617 for (i = 0; i < cpuc->
n_events; i++) {
1628 val = sparc_perf_event_update(event, hwc, idx);
1629 if (val & (1ULL << 31))
1632 perf_sample_data_init(&
data, 0, hwc->last_period);
1633 if (!sparc_perf_event_set_period(event, hwc, idx))
1637 sparc_pmu_stop(event, 0);
1643 static __read_mostly
struct notifier_block perf_event_nmi_notifier = {
1644 .notifier_call = perf_event_nmi_handler,
1647 static bool __init supported_pmu(
void)
1674 pr_info(
"Performance events: ");
1676 if (!supported_pmu()) {
1693 unsigned long ksp,
fp;
1694 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1700 perf_callchain_store(entry, regs->tpc);
1713 regs = (
struct pt_regs *) (sf + 1);
1722 fp = (
unsigned long)sf->
fp + STACK_BIAS;
1724 perf_callchain_store(entry, pc);
1725 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1728 if (
current->ret_stack && index >= graph) {
1729 pc =
current->ret_stack[index - graph].ret;
1730 perf_callchain_store(entry, pc);
1754 perf_callchain_store(entry, pc);
1767 if (thread32_stack_is_64bit(ufp)) {
1775 ufp = ((
unsigned long) sf.
fp) & 0xffffffff;
1777 struct sparc_stackf32 *usf,
sf;
1778 usf = (
struct sparc_stackf32 *) ufp;
1782 ufp = (
unsigned long)sf.fp;
1784 perf_callchain_store(entry, pc);
1791 perf_callchain_store(entry, regs->tpc);
1797 if (test_thread_flag(TIF_32BIT))
1798 perf_callchain_user_32(entry, regs);
1800 perf_callchain_user_64(entry, regs);