11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/perf_event.h>
18 #include <asm/machdep.h>
20 #include <asm/ptrace.h>
53 static unsigned int freeze_events_kernel = MMCR0_FCS;
62 #define MMCR0_PMCjCE MMCR0_PMCnCE
64 #define SPRN_MMCRA SPRN_MMCR2
65 #define MMCRA_SAMPLE_ENABLE 0
67 static inline unsigned long perf_ip_adjust(
struct pt_regs *
regs)
71 static inline void perf_get_data_addr(
struct pt_regs *
regs,
u64 *addrp) { }
76 static inline void perf_read_regs(
struct pt_regs *
regs)
80 static inline int perf_intr_is_nmi(
struct pt_regs *
regs)
97 static inline unsigned long perf_ip_adjust(
struct pt_regs *
regs)
99 unsigned long mmcra = regs->
dsisr;
102 unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
104 return 4 * (slot - 1);
117 static inline void perf_get_data_addr(
struct pt_regs *regs,
u64 *addrp)
119 unsigned long mmcra = regs->
dsisr;
120 unsigned long sdsync;
123 sdsync = POWER7P_MMCRA_SDAR_VALID;
125 sdsync = POWER6_MMCRA_SDSYNC;
127 sdsync = MMCRA_SDSYNC;
129 if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
130 *addrp =
mfspr(SPRN_SDAR);
133 static bool mmcra_sihv(
unsigned long mmcra)
135 unsigned long sihv = MMCRA_SIHV;
138 sihv = POWER6_MMCRA_SIHV;
140 return !!(mmcra & sihv);
143 static bool mmcra_sipr(
unsigned long mmcra)
145 unsigned long sipr = MMCRA_SIPR;
148 sipr = POWER6_MMCRA_SIPR;
150 return !!(mmcra & sipr);
153 static inline u32 perf_flags_from_msr(
struct pt_regs *regs)
155 if (regs->
msr & MSR_PR)
157 if ((regs->
msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
162 static inline u32 perf_get_misc_flags(
struct pt_regs *regs)
164 unsigned long mmcra = regs->
dsisr;
165 unsigned long use_siar = regs->
result;
168 return perf_flags_from_msr(regs);
177 unsigned long siar =
mfspr(SPRN_SIAR);
184 if (mmcra_sipr(mmcra))
186 if (mmcra_sihv(mmcra) && (freeze_events_kernel != MMCR0_FCHV))
197 static inline void perf_read_regs(
struct pt_regs *regs)
199 unsigned long mmcra =
mfspr(SPRN_MMCRA);
200 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
220 if (
TRAP(regs) != 0xf00)
239 static inline int perf_intr_is_nmi(
struct pt_regs *regs)
251 static inline int siar_valid(
struct pt_regs *regs)
253 unsigned long mmcra = regs->
dsisr;
254 int marked = mmcra & MMCRA_SAMPLE_ENABLE;
257 return mmcra & POWER7P_MMCRA_SIAR_VALID;
264 static void perf_event_interrupt(
struct pt_regs *regs);
273 static unsigned long read_pmc(
int idx)
279 val =
mfspr(SPRN_PMC1);
282 val =
mfspr(SPRN_PMC2);
285 val =
mfspr(SPRN_PMC3);
288 val =
mfspr(SPRN_PMC4);
291 val =
mfspr(SPRN_PMC5);
294 val =
mfspr(SPRN_PMC6);
298 val =
mfspr(SPRN_PMC7);
301 val =
mfspr(SPRN_PMC8);
314 static void write_pmc(
int idx,
unsigned long val)
318 mtspr(SPRN_PMC1, val);
321 mtspr(SPRN_PMC2, val);
324 mtspr(SPRN_PMC3, val);
327 mtspr(SPRN_PMC4, val);
330 mtspr(SPRN_PMC5, val);
333 mtspr(SPRN_PMC6, val);
337 mtspr(SPRN_PMC7, val);
340 mtspr(SPRN_PMC8, val);
354 static int power_check_constraints(
struct cpu_hw_events *cpuhw,
369 for (i = 0; i < n_ev; ++
i) {
381 for (i = 0; i < n_ev; ++
i) {
382 nv = (value | cpuhw->
avalues[
i][0]) +
383 (value & cpuhw->
avalues[i][0] & addf);
384 if ((((nv + tadd) ^ value) &
mask) != 0 ||
385 (((nv + tadd) ^ cpuhw->
avalues[i][0]) &
397 for (i = 0; i < n_ev; ++
i) {
401 for (j = 1; j < n_alt[
i]; ++
j)
410 value = mask = nv = 0;
422 while (++j < n_alt[i]) {
424 (value & cpuhw->
avalues[i][j] & addf);
425 if ((((nv + tadd) ^ value) & mask) == 0 &&
426 (((nv + tadd) ^ cpuhw->
avalues[i][j])
456 for (i = 0; i < n_ev; ++
i)
466 static int check_excludes(
struct perf_event **ctrs,
unsigned int cflags[],
467 int n_prev,
int n_new)
469 int eu = 0, ek = 0, eh = 0;
478 for (i = 0; i <
n; ++
i) {
480 cflags[
i] &= ~PPMU_LIMITED_PMC_REQD;
485 eu =
event->
attr.exclude_user;
486 ek =
event->attr.exclude_kernel;
487 eh =
event->attr.exclude_hv;
489 }
else if (event->
attr.exclude_user != eu ||
490 event->
attr.exclude_kernel != ek ||
491 event->
attr.exclude_hv != eh) {
497 for (i = 0; i <
n; ++
i)
498 if (cflags[i] & PPMU_LIMITED_PMC_OK)
517 if (prev > val && (prev - val) < 256)
523 static void power_pmu_read(
struct perf_event *event)
540 val = read_pmc(event->hw.idx);
541 delta = check_and_compute_delta(prev, val);
555 static int is_limited_pmc(
int pmcnum)
558 && (pmcnum == 5 || pmcnum == 6);
561 static void freeze_limited_counters(
struct cpu_hw_events *cpuhw,
562 unsigned long pmc5,
unsigned long pmc6)
572 val = (
event->hw.idx == 5) ? pmc5 : pmc6;
575 delta = check_and_compute_delta(prev, val);
581 static void thaw_limited_counters(
struct cpu_hw_events *cpuhw,
582 unsigned long pmc5,
unsigned long pmc6)
591 val = (
event->hw.idx == 5) ? pmc5 : pmc6;
593 if (check_and_compute_delta(prev, val))
610 static void write_mmcr0(
struct cpu_hw_events *cpuhw,
unsigned long mmcr0)
612 unsigned long pmc5, pmc6;
615 mtspr(SPRN_MMCR0, mmcr0);
626 asm volatile(
"mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
627 :
"=&r" (pmc5),
"=&r" (pmc6)
628 :
"r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
630 "i" (SPRN_PMC5),
"i" (SPRN_PMC6));
632 if (mmcr0 & MMCR0_FC)
633 freeze_limited_counters(cpuhw, pmc5, pmc6);
635 thaw_limited_counters(cpuhw, pmc5, pmc6);
641 if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
642 mtspr(SPRN_MMCR0, mmcr0);
649 static void power_pmu_disable(
struct pmu *
pmu)
674 if (cpuhw->
mmcr[2] & MMCRA_SAMPLE_ENABLE) {
676 cpuhw->
mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
686 write_mmcr0(cpuhw,
mfspr(SPRN_MMCR0) | MMCR0_FC);
697 static void power_pmu_enable(
struct pmu *pmu)
726 mtspr(SPRN_MMCRA, cpuhw->
mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
729 ppc_set_pmu_inuse(0);
749 event = cpuhw->
event[0];
750 if (event->
attr.exclude_user)
751 cpuhw->
mmcr[0] |= MMCR0_FCP;
752 if (event->
attr.exclude_kernel)
753 cpuhw->
mmcr[0] |= freeze_events_kernel;
754 if (event->
attr.exclude_hv)
755 cpuhw->
mmcr[0] |= MMCR0_FCHV;
762 ppc_set_pmu_inuse(1);
763 mtspr(SPRN_MMCRA, cpuhw->
mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
765 mtspr(SPRN_MMCR0, (cpuhw->
mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
774 if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
775 power_pmu_read(event);
776 write_pmc(event->hw.idx, 0);
789 idx = hwc_index[
i] + 1;
790 if (is_limited_pmc(idx)) {
797 if (event->hw.sample_period) {
799 if (left < 0x80000000L)
800 val = 0x80000000
L -
left;
810 cpuhw->
mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
814 write_mmcr0(cpuhw, cpuhw->
mmcr[0]);
819 if (cpuhw->
mmcr[2] & MMCRA_SAMPLE_ENABLE) {
835 if (!is_software_event(group)) {
839 flags[
n] = group->hw.event_base;
840 events[n++] = group->hw.config;
843 if (!is_software_event(event) &&
848 flags[
n] =
event->hw.event_base;
849 events[n++] =
event->hw.config;
861 static int power_pmu_add(
struct perf_event *event,
int ef_flags)
880 cpuhw->
events[n0] =
event->hw.config;
881 cpuhw->
flags[n0] =
event->hw.event_base;
894 if (check_excludes(cpuhw->
event, cpuhw->
flags, n0, 1))
896 if (power_check_constraints(cpuhw, cpuhw->
events, cpuhw->
flags, n0 + 1))
898 event->hw.config = cpuhw->
events[n0];
914 static void power_pmu_del(
struct perf_event *event,
int ef_flags)
923 power_pmu_read(event);
927 if (event == cpuhw->
event[i]) {
936 write_pmc(event->hw.idx, 0);
947 while (++i < cpuhw->n_limited) {
955 cpuhw->
mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
967 static void power_pmu_start(
struct perf_event *event,
int ef_flags)
973 if (!event->hw.idx || !event->hw.sample_period)
989 if (left < 0x80000000L)
990 val = 0x80000000
L -
left;
992 write_pmc(event->hw.idx, val);
999 static void power_pmu_stop(
struct perf_event *event,
int ef_flags)
1001 unsigned long flags;
1003 if (!event->hw.idx || !event->hw.sample_period)
1012 power_pmu_read(event);
1014 write_pmc(event->hw.idx, 0);
1062 if (check_excludes(cpuhw->
event, cpuhw->
flags, 0, n))
1064 i = power_check_constraints(cpuhw, cpuhw->
events, cpuhw->
flags, n);
1083 static int can_go_on_limited_pmc(
struct perf_event *event,
u64 ev,
1089 if (event->
attr.exclude_user
1090 || event->
attr.exclude_kernel
1091 || event->
attr.exclude_hv
1092 || event->
attr.sample_period)
1116 static u64 normal_pmc_alternative(
u64 ev,
unsigned long flags)
1136 static void hw_perf_event_destroy(
struct perf_event *event)
1138 if (!atomic_add_unless(&num_events, -1, 1)) {
1149 static int hw_perf_cache_event(
u64 config,
u64 *eventp)
1158 type = config & 0xff;
1159 op = (config >> 8) & 0xff;
1160 result = (config >> 16) & 0xff;
1176 static int power_pmu_event_init(
struct perf_event *event)
1179 unsigned long flags;
1191 if (has_branch_stack(event))
1194 switch (event->
attr.type) {
1196 ev =
event->attr.config;
1202 err = hw_perf_cache_event(event->
attr.config, &ev);
1207 ev =
event->attr.config;
1213 event->hw.config_base = ev;
1221 if (!firmware_has_feature(FW_FEATURE_LPAR))
1222 event->attr.exclude_hv = 0;
1239 if (can_go_on_limited_pmc(event, ev, flags)) {
1247 ev = normal_pmc_alternative(ev, flags);
1259 if (event->group_leader != event) {
1260 n = collect_events(event->group_leader, ppmu->
n_counter - 1,
1261 ctrs, events, cflags);
1268 if (check_excludes(ctrs, cflags, n, 1))
1272 err = power_check_constraints(cpuhw, events, cflags, n + 1);
1277 event->hw.config = events[
n];
1278 event->hw.event_base = cflags[
n];
1279 event->hw.last_period =
event->hw.sample_period;
1280 local64_set(&event->hw.period_left, event->hw.last_period);
1298 event->destroy = hw_perf_event_destroy;
1303 static int power_pmu_event_idx(
struct perf_event *event)
1305 return event->hw.idx;
1309 .pmu_enable = power_pmu_enable,
1310 .pmu_disable = power_pmu_disable,
1311 .event_init = power_pmu_event_init,
1312 .add = power_pmu_add,
1313 .del = power_pmu_del,
1314 .start = power_pmu_start,
1315 .stop = power_pmu_stop,
1316 .read = power_pmu_read,
1320 .event_idx = power_pmu_event_idx,
1329 static void record_and_restart(
struct perf_event *event,
unsigned long val,
1337 write_pmc(event->hw.idx, 0);
1343 delta = check_and_compute_delta(prev, val);
1357 record = siar_valid(regs);
1358 event->hw.last_period =
event->hw.sample_period;
1360 if (left < 0x80000000LL)
1361 val = 0x80000000
LL -
left;
1364 write_pmc(event->hw.idx, val);
1373 struct perf_sample_data
data;
1375 perf_sample_data_init(&
data, ~0ULL, event->hw.last_period);
1378 perf_get_data_addr(regs, &
data.addr);
1381 power_pmu_stop(event, 0);
1391 u32 flags = perf_get_misc_flags(regs);
1405 unsigned long use_siar = regs->
result;
1407 if (use_siar && siar_valid(regs))
1408 return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
1415 static bool pmc_overflow(
unsigned long val)
1431 if (pvr_version_is(PVR_POWER7) && ((0x80000000 - val) <= 256))
1440 static void perf_event_interrupt(
struct pt_regs *regs)
1450 freeze_limited_counters(cpuhw,
mfspr(SPRN_PMC5),
1453 perf_read_regs(regs);
1455 nmi = perf_intr_is_nmi(regs);
1463 if (!event->hw.idx || is_limited_pmc(event->hw.idx))
1465 val = read_pmc(event->hw.idx);
1469 record_and_restart(event, val, regs);
1481 if (is_limited_pmc(i + 1))
1483 val = read_pmc(i + 1);
1484 if (pmc_overflow(val))
1485 write_pmc(i + 1, 0);
1496 write_mmcr0(cpuhw, cpuhw->
mmcr[0]);
1504 static void power_pmu_setup(
int cpu)
1510 memset(cpuhw, 0,
sizeof(*cpuhw));
1511 cpuhw->
mmcr[0] = MMCR0_FC;
1517 unsigned int cpu = (
long)hcpu;
1521 power_pmu_setup(cpu);
1537 pr_info(
"%s performance monitor hardware support registered\n",
1544 if (mfmsr() & MSR_HV)
1545 freeze_events_kernel = MMCR0_FCHV;