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
124 static const int grsel_shift[8] = {
131 static unsigned long unit_cons[
PM_LASTUNIT+1][2] = {
132 [
PM_FPU] = { 0xc0002000000000ul, 0x00001000000000ul },
133 [
PM_ISU0] = { 0x00002000000000ul, 0x00000800000000ul },
134 [
PM_ISU1] = { 0xc0002000000000ul, 0xc0001000000000ul },
135 [
PM_IFU] = { 0xc0002000000000ul, 0x80001000000000ul },
136 [
PM_IDU] = { 0x30002000000000ul, 0x00000400000000ul },
137 [
PM_GRS] = { 0x30002000000000ul, 0x30000400000000ul },
140 static int power5_get_constraint(
u64 event,
unsigned long *maskp,
156 grp = (pmc - 1) >> 1;
157 else if (event != 0x500009 && event != 0x600005)
166 mask |= unit_cons[
unit][0];
178 fmask = (bit == 6)? 7: 3;
179 sh = grsel_shift[
bit];
180 mask |= (
unsigned long)fmask << sh;
191 mask |= 0xf
UL << (24 - 4 *
byte);
192 value |= (
unsigned long)unit << (24 - 4 * byte);
196 mask |= 0x200000000ul;
197 value |= 0x080000000ul;
198 }
else if (grp == 1) {
200 mask |= 0x40000000ul;
201 value |= 0x10000000ul;
205 mask |= 0x8000000000000ul;
206 value |= 0x1000000000000ul;
215 static const unsigned int event_alternatives[][
MAX_ALT] = {
216 { 0x120e4, 0x400002 },
217 { 0x410c7, 0x441084 },
218 { 0x100005, 0x600005 },
219 { 0x100009, 0x200009, 0x500009 },
220 { 0x300009, 0x400009 },
227 static int find_alternative(
u64 event)
231 for (i = 0; i <
ARRAY_SIZE(event_alternatives); ++
i) {
232 if (event < event_alternatives[i][0])
234 for (j = 0; j <
MAX_ALT && event_alternatives[
i][
j]; ++
j)
235 if (event == event_alternatives[i][j])
241 static const unsigned char bytedecode_alternatives[4][4] = {
242 { 0x21, 0x23, 0x25, 0x27 },
243 { 0x07, 0x17, 0x0e, 0x1e },
244 { 0x20, 0x22, 0x24, 0x26 },
245 { 0x07, 0x17, 0x0e, 0x1e }
253 static s64 find_alternative_bdecode(
u64 event)
255 int pmc, altpmc,
pp,
j;
258 if (pmc == 0 || pmc > 4)
262 for (j = 0; j < 4; ++
j) {
263 if (bytedecode_alternatives[pmc - 1][j] == pp) {
266 bytedecode_alternatives[altpmc - 1][
j];
272 static int power5_get_alternatives(
u64 event,
unsigned int flags,
u64 alt[])
279 i = find_alternative(event);
282 ae = event_alternatives[
i][
j];
283 if (ae && ae != event)
287 ae = find_alternative_bdecode(event);
300 static unsigned char direct_event_is_marked[0x28] = {
341 static int power5_marked_instr_event(
u64 event)
353 if (psel <
sizeof(direct_event_is_marked)) {
354 if (direct_event_is_marked[psel] & (1 << pmc))
356 if (direct_event_is_marked[psel] & 0x80)
358 else if (psel == 0x08)
360 else if (psel == 0x10)
362 else if (psel == 0x1b && (pmc == 1 || pmc == 3))
364 }
else if ((psel & 0x58) == 0x40)
367 if (!(event & PM_BUSEVENT_MSK))
375 }
else if (unit ==
PM_LSU1 && byte >= 4) {
382 return (mask >> (byte * 8 + bit)) & 1;
385 static int power5_compute_mmcr(
u64 event[],
int n_ev,
386 unsigned int hwc[],
unsigned long mmcr[])
388 unsigned long mmcr1 = 0;
389 unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
391 unsigned int ttm, grp;
392 int i, isbus,
bit, grsel;
393 unsigned int pmc_inuse = 0;
394 unsigned int pmc_grp_use[2];
395 unsigned char busbyte[4];
396 unsigned char unituse[16];
403 pmc_grp_use[0] = pmc_grp_use[1] = 0;
404 memset(busbyte, 0,
sizeof(busbyte));
405 memset(unituse, 0,
sizeof(unituse));
406 for (i = 0; i < n_ev; ++
i) {
411 if (pmc_inuse & (1 << (pmc - 1)))
413 pmc_inuse |= 1 << (pmc - 1);
416 ++pmc_grp_use[(pmc - 1) >> 1];
418 if (event[i] & PM_BUSEVENT_MSK) {
432 ++pmc_grp_use[byte & 1];
433 if (busbyte[byte] && busbyte[byte] != unit)
439 if (pmc_grp_use[0] > 2 || pmc_grp_use[1] > 2)
474 for (byte = 0; byte < 4; ++
byte) {
475 unit = busbyte[
byte];
481 }
else if (unit ==
PM_LSU1 + 1) {
486 mmcr1 |= (
unsigned long)ttm
491 for (i = 0; i < n_ev; ++
i) {
499 for (pmc = 0; pmc < 4; ++pmc) {
500 if (pmc_inuse & (1 << pmc))
502 grp = (pmc >> 1) & 1;
504 if (grp == (byte & 1))
506 }
else if (pmc_grp_use[grp] < 2) {
511 pmc_inuse |= 1 << pmc;
512 }
else if (pmc <= 4) {
515 if ((psel == 8 || psel == 0x10) && isbus && (byte & 2))
522 if (isbus && unit ==
PM_GRS) {
525 mmcr1 |= (
unsigned long)grsel << grsel_shift[bit];
527 if (power5_marked_instr_event(event[i]))
528 mmcra |= MMCRA_SAMPLE_ENABLE;
537 mmcr[0] = MMCR0_PMC1CE;
538 if (pmc_inuse & 0x3e)
539 mmcr[0] |= MMCR0_PMCjCE;
545 static void power5_disable_pmc(
unsigned int pmc,
unsigned long mmcr[])
551 static int power5_generic_events[] = {
560 #define C(x) PERF_COUNT_HW_CACHE_##x
567 static int power5_cache_events[
C(
MAX)][
C(
OP_MAX)][
C(RESULT_MAX)] = {
569 [
C(
OP_READ)] = { 0x4c1090, 0x3c1088 },
571 [
C(OP_PREFETCH)] = { 0xc70e7, 0 },
576 [
C(OP_PREFETCH)] = { 0, 0 },
581 [
C(OP_PREFETCH)] = { 0xc50c3, 0 },
584 [
C(
OP_READ)] = { 0x2c4090, 0x800c4 },
586 [
C(OP_PREFETCH)] = { -1, -1 },
591 [
C(OP_PREFETCH)] = { -1, -1 },
594 [
C(
OP_READ)] = { 0x230e4, 0x230e5 },
596 [
C(OP_PREFETCH)] = { -1, -1 },
601 [
C(OP_PREFETCH)] = { -1, -1 },
609 .add_fields = 0x7000090000555ul,
610 .test_adder = 0x3000490000000ul,
611 .compute_mmcr = power5_compute_mmcr,
612 .get_constraint = power5_get_constraint,
613 .get_alternatives = power5_get_alternatives,
614 .disable_pmc = power5_disable_pmc,
615 .n_generic =
ARRAY_SIZE(power5_generic_events),
616 .generic_events = power5_generic_events,
617 .cache_events = &power5_cache_events,
620 static int __init init_power5_pmu(
void)