11 #include <linux/kernel.h>
12 #include <linux/perf_event.h>
13 #include <linux/string.h>
15 #include <asm/cputable.h>
21 #define PM_PMC_MSK 0xf
22 #define PM_PMC_MSKS (PM_PMC_MSK << PM_PMC_SH)
24 #define PM_UNIT_MSK 0xf
29 #define PM_BUSEVENT_MSK 0x80
30 #define PM_PMCSEL_MSK 0x7f
42 #define PM_LASTUNIT 0xc
47 #define MMCR1_TTM0SEL_SH 62
48 #define MMCR1_TTM1SEL_SH 60
49 #define MMCR1_TTM2SEL_SH 58
50 #define MMCR1_TTM3SEL_SH 56
51 #define MMCR1_TTMSEL_MSK 3
52 #define MMCR1_TD_CP_DBG0SEL_SH 54
53 #define MMCR1_TD_CP_DBG1SEL_SH 52
54 #define MMCR1_TD_CP_DBG2SEL_SH 50
55 #define MMCR1_TD_CP_DBG3SEL_SH 48
56 #define MMCR1_GRS_L2SEL_SH 46
57 #define MMCR1_GRS_L2SEL_MSK 3
58 #define MMCR1_GRS_L3SEL_SH 44
59 #define MMCR1_GRS_L3SEL_MSK 3
60 #define MMCR1_GRS_MCSEL_SH 41
61 #define MMCR1_GRS_MCSEL_MSK 7
62 #define MMCR1_GRS_FABSEL_SH 39
63 #define MMCR1_GRS_FABSEL_MSK 3
64 #define MMCR1_PMC1_ADDER_SEL_SH 35
65 #define MMCR1_PMC2_ADDER_SEL_SH 34
66 #define MMCR1_PMC3_ADDER_SEL_SH 33
67 #define MMCR1_PMC4_ADDER_SEL_SH 32
68 #define MMCR1_PMC1SEL_SH 25
69 #define MMCR1_PMC2SEL_SH 17
70 #define MMCR1_PMC3SEL_SH 9
71 #define MMCR1_PMC4SEL_SH 1
72 #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
73 #define MMCR1_PMCSEL_MSK 0x7f
120 static const int grsel_shift[8] = {
127 static unsigned long unit_cons[
PM_LASTUNIT+1][2] = {
128 [
PM_FPU] = { 0x3200000000ul, 0x0100000000ul },
129 [
PM_ISU0] = { 0x0200000000ul, 0x0080000000ul },
130 [
PM_ISU1] = { 0x3200000000ul, 0x3100000000ul },
131 [
PM_IFU] = { 0x3200000000ul, 0x2100000000ul },
132 [
PM_IDU] = { 0x0e00000000ul, 0x0040000000ul },
133 [
PM_GRS] = { 0x0e00000000ul, 0x0c40000000ul },
136 static int power5p_get_constraint(
u64 event,
unsigned long *maskp,
150 if (pmc >= 5 && !(event == 0x500009 || event == 0x600005))
159 mask |= unit_cons[
unit][0];
171 fmask = (bit == 6)? 7: 3;
172 sh = grsel_shift[
bit];
173 mask |= (
unsigned long)fmask << sh;
178 mask |= 0xf
UL << (24 - 4 *
byte);
179 value |= (
unsigned long)unit << (24 - 4 * byte);
183 mask |= 0x8000000000000ul;
184 value |= 0x1000000000000ul;
191 static int power5p_limited_pmc_event(
u64 event)
195 return pmc == 5 || pmc == 6;
200 static const unsigned int event_alternatives[][
MAX_ALT] = {
201 { 0x100c0, 0x40001f },
202 { 0x120e4, 0x400002 },
203 { 0x230e2, 0x323087 },
204 { 0x230e3, 0x223087, 0x3230a0 },
205 { 0x410c7, 0x441084 },
206 { 0x800c4, 0xc20e0 },
207 { 0xc50c6, 0xc60e0 },
208 { 0x100005, 0x600005 },
209 { 0x100009, 0x200009 },
210 { 0x200015, 0x300015 },
211 { 0x300009, 0x400009 },
218 static int find_alternative(
unsigned int event)
222 for (i = 0; i <
ARRAY_SIZE(event_alternatives); ++
i) {
223 if (event < event_alternatives[i][0])
225 for (j = 0; j <
MAX_ALT && event_alternatives[
i][
j]; ++
j)
226 if (event == event_alternatives[i][j])
232 static const unsigned char bytedecode_alternatives[4][4] = {
233 { 0x21, 0x23, 0x25, 0x27 },
234 { 0x07, 0x17, 0x0e, 0x1e },
235 { 0x20, 0x22, 0x24, 0x26 },
236 { 0x07, 0x17, 0x0e, 0x1e }
245 static s64 find_alternative_bdecode(
u64 event)
247 int pmc, altpmc,
pp,
j;
250 if (pmc == 0 || pmc > 4)
254 for (j = 0; j < 4; ++
j) {
255 if (bytedecode_alternatives[pmc - 1][j] == pp) {
258 bytedecode_alternatives[altpmc - 1][
j];
263 if (pmc == 1 && (pp == 0x0d || pp == 0x0e))
264 return event + (2 <<
PM_PMC_SH) + (0x2e - 0x0d);
265 if (pmc == 3 && (pp == 0x2e || pp == 0x2f))
266 return event - (2 <<
PM_PMC_SH) - (0x2e - 0x0d);
269 if (pp == 0x10 || pp == 0x28)
276 static int power5p_get_alternatives(
u64 event,
unsigned int flags,
u64 alt[])
284 nlim = power5p_limited_pmc_event(event);
285 i = find_alternative(event);
288 ae = event_alternatives[
i][
j];
289 if (ae && ae != event)
291 nlim += power5p_limited_pmc_event(ae);
294 ae = find_alternative_bdecode(event);
311 for (i = 0; i < nalt; ++
i) {
336 for (i = 0; i < nalt; ++
i) {
337 if (!power5p_limited_pmc_event(alt[i])) {
346 for (i = 0; i < nalt; ++
i) {
347 if (power5p_limited_pmc_event(alt[i])) {
364 static unsigned char direct_event_is_marked[0x28] = {
405 static int power5p_marked_instr_event(
u64 event)
417 if (psel <
sizeof(direct_event_is_marked)) {
418 if (direct_event_is_marked[psel] & (1 << pmc))
420 if (direct_event_is_marked[psel] & 0x80)
422 else if (psel == 0x08)
424 else if (psel == 0x10)
426 else if (psel == 0x1b && (pmc == 1 || pmc == 3))
428 }
else if ((psel & 0x48) == 0x40) {
430 }
else if (psel == 0x28) {
432 }
else if (pmc == 3 && (psel == 0x2e || psel == 0x2f)) {
436 if (!(event & PM_BUSEVENT_MSK) || bit == -1)
444 }
else if (unit ==
PM_LSU1 && byte >= 4) {
451 return (mask >> (byte * 8 + bit)) & 1;
454 static int power5p_compute_mmcr(
u64 event[],
int n_ev,
455 unsigned int hwc[],
unsigned long mmcr[])
457 unsigned long mmcr1 = 0;
458 unsigned long mmcra = 0;
461 int i, isbus,
bit, grsel;
462 unsigned int pmc_inuse = 0;
463 unsigned char busbyte[4];
464 unsigned char unituse[16];
471 memset(busbyte, 0,
sizeof(busbyte));
472 memset(unituse, 0,
sizeof(unituse));
473 for (i = 0; i < n_ev; ++
i) {
478 if (pmc_inuse & (1 << (pmc - 1)))
480 pmc_inuse |= 1 << (pmc - 1);
482 if (event[i] & PM_BUSEVENT_MSK) {
495 if (busbyte[byte] && busbyte[byte] != unit)
534 for (byte = 0; byte < 4; ++
byte) {
535 unit = busbyte[
byte];
541 }
else if (unit ==
PM_LSU1 + 1) {
546 mmcr1 |= (
unsigned long)ttm
551 for (i = 0; i < n_ev; ++
i) {
559 for (pmc = 0; pmc < 4; ++pmc) {
560 if (!(pmc_inuse & (1 << pmc)))
565 pmc_inuse |= 1 << pmc;
566 }
else if (pmc <= 4) {
569 if (isbus && (byte & 2) &&
570 (psel == 8 || psel == 0x10 || psel == 0x28))
577 if (isbus && unit ==
PM_GRS) {
580 mmcr1 |= (
unsigned long)grsel << grsel_shift[bit];
582 if (power5p_marked_instr_event(event[i]))
583 mmcra |= MMCRA_SAMPLE_ENABLE;
584 if ((psel & 0x58) == 0x40 && (byte & 1) != ((pmc >> 1) & 1))
595 mmcr[0] = MMCR0_PMC1CE;
596 if (pmc_inuse & 0x3e)
597 mmcr[0] |= MMCR0_PMCjCE;
603 static void power5p_disable_pmc(
unsigned int pmc,
unsigned long mmcr[])
609 static int power5p_generic_events[] = {
618 #define C(x) PERF_COUNT_HW_CACHE_##x
625 static int power5p_cache_events[
C(
MAX)][
C(
OP_MAX)][
C(RESULT_MAX)] = {
627 [
C(
OP_READ)] = { 0x1c10a8, 0x3c1088 },
629 [
C(OP_PREFETCH)] = { 0xc70e7, -1 },
634 [
C(OP_PREFETCH)] = { 0, 0 },
639 [
C(OP_PREFETCH)] = { 0xc50c3, 0 },
642 [
C(
OP_READ)] = { 0xc20e4, 0x800c4 },
644 [
C(OP_PREFETCH)] = { -1, -1 },
649 [
C(OP_PREFETCH)] = { -1, -1 },
652 [
C(
OP_READ)] = { 0x230e4, 0x230e5 },
654 [
C(OP_PREFETCH)] = { -1, -1 },
659 [
C(OP_PREFETCH)] = { -1, -1 },
664 .name =
"POWER5+/++",
667 .add_fields = 0x7000000000055ul,
668 .test_adder = 0x3000040000000ul,
669 .compute_mmcr = power5p_compute_mmcr,
670 .get_constraint = power5p_get_constraint,
671 .get_alternatives = power5p_get_alternatives,
672 .disable_pmc = power5p_disable_pmc,
673 .limited_pmc_event = power5p_limited_pmc_event,
675 .n_generic =
ARRAY_SIZE(power5p_generic_events),
676 .generic_events = power5p_generic_events,
677 .cache_events = &power5p_cache_events,
680 static int __init init_power5p_pmu(
void)