Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Performance counter support for POWER4 (GP) and POWER4+ (GQ) processors. |
| 3 | * |
| 4 | * Copyright 2009 Paul Mackerras, IBM Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/perf_counter.h> |
| 13 | #include <asm/reg.h> |
| 14 | |
| 15 | /* |
| 16 | * Bits in event code for POWER4 |
| 17 | */ |
| 18 | #define PM_PMC_SH 12 /* PMC number (1-based) for direct events */ |
| 19 | #define PM_PMC_MSK 0xf |
| 20 | #define PM_UNIT_SH 8 /* TTMMUX number and setting - unit select */ |
| 21 | #define PM_UNIT_MSK 0xf |
| 22 | #define PM_LOWER_SH 6 |
| 23 | #define PM_LOWER_MSK 1 |
| 24 | #define PM_LOWER_MSKS 0x40 |
| 25 | #define PM_BYTE_SH 4 /* Byte number of event bus to use */ |
| 26 | #define PM_BYTE_MSK 3 |
| 27 | #define PM_PMCSEL_MSK 7 |
| 28 | |
| 29 | /* |
| 30 | * Unit code values |
| 31 | */ |
| 32 | #define PM_FPU 1 |
| 33 | #define PM_ISU1 2 |
| 34 | #define PM_IFU 3 |
| 35 | #define PM_IDU0 4 |
| 36 | #define PM_ISU1_ALT 6 |
| 37 | #define PM_ISU2 7 |
| 38 | #define PM_IFU_ALT 8 |
| 39 | #define PM_LSU0 9 |
| 40 | #define PM_LSU1 0xc |
| 41 | #define PM_GPS 0xf |
| 42 | |
| 43 | /* |
| 44 | * Bits in MMCR0 for POWER4 |
| 45 | */ |
| 46 | #define MMCR0_PMC1SEL_SH 8 |
| 47 | #define MMCR0_PMC2SEL_SH 1 |
| 48 | #define MMCR_PMCSEL_MSK 0x1f |
| 49 | |
| 50 | /* |
| 51 | * Bits in MMCR1 for POWER4 |
| 52 | */ |
| 53 | #define MMCR1_TTM0SEL_SH 62 |
| 54 | #define MMCR1_TTC0SEL_SH 61 |
| 55 | #define MMCR1_TTM1SEL_SH 59 |
| 56 | #define MMCR1_TTC1SEL_SH 58 |
| 57 | #define MMCR1_TTM2SEL_SH 56 |
| 58 | #define MMCR1_TTC2SEL_SH 55 |
| 59 | #define MMCR1_TTM3SEL_SH 53 |
| 60 | #define MMCR1_TTC3SEL_SH 52 |
| 61 | #define MMCR1_TTMSEL_MSK 3 |
| 62 | #define MMCR1_TD_CP_DBG0SEL_SH 50 |
| 63 | #define MMCR1_TD_CP_DBG1SEL_SH 48 |
| 64 | #define MMCR1_TD_CP_DBG2SEL_SH 46 |
| 65 | #define MMCR1_TD_CP_DBG3SEL_SH 44 |
| 66 | #define MMCR1_DEBUG0SEL_SH 43 |
| 67 | #define MMCR1_DEBUG1SEL_SH 42 |
| 68 | #define MMCR1_DEBUG2SEL_SH 41 |
| 69 | #define MMCR1_DEBUG3SEL_SH 40 |
| 70 | #define MMCR1_PMC1_ADDER_SEL_SH 39 |
| 71 | #define MMCR1_PMC2_ADDER_SEL_SH 38 |
| 72 | #define MMCR1_PMC6_ADDER_SEL_SH 37 |
| 73 | #define MMCR1_PMC5_ADDER_SEL_SH 36 |
| 74 | #define MMCR1_PMC8_ADDER_SEL_SH 35 |
| 75 | #define MMCR1_PMC7_ADDER_SEL_SH 34 |
| 76 | #define MMCR1_PMC3_ADDER_SEL_SH 33 |
| 77 | #define MMCR1_PMC4_ADDER_SEL_SH 32 |
| 78 | #define MMCR1_PMC3SEL_SH 27 |
| 79 | #define MMCR1_PMC4SEL_SH 22 |
| 80 | #define MMCR1_PMC5SEL_SH 17 |
| 81 | #define MMCR1_PMC6SEL_SH 12 |
| 82 | #define MMCR1_PMC7SEL_SH 7 |
| 83 | #define MMCR1_PMC8SEL_SH 2 /* note bit 0 is in MMCRA for GP */ |
| 84 | |
| 85 | static short mmcr1_adder_bits[8] = { |
| 86 | MMCR1_PMC1_ADDER_SEL_SH, |
| 87 | MMCR1_PMC2_ADDER_SEL_SH, |
| 88 | MMCR1_PMC3_ADDER_SEL_SH, |
| 89 | MMCR1_PMC4_ADDER_SEL_SH, |
| 90 | MMCR1_PMC5_ADDER_SEL_SH, |
| 91 | MMCR1_PMC6_ADDER_SEL_SH, |
| 92 | MMCR1_PMC7_ADDER_SEL_SH, |
| 93 | MMCR1_PMC8_ADDER_SEL_SH |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * Bits in MMCRA |
| 98 | */ |
| 99 | #define MMCRA_PMC8SEL0_SH 17 /* PMC8SEL bit 0 for GP */ |
| 100 | |
| 101 | /* |
| 102 | * Layout of constraint bits: |
| 103 | * 6666555555555544444444443333333333222222222211111111110000000000 |
| 104 | * 3210987654321098765432109876543210987654321098765432109876543210 |
| 105 | * |[ >[ >[ >|||[ >[ >< >< >< >< ><><><><><><><><> |
| 106 | * | UC1 UC2 UC3 ||| PS1 PS2 B0 B1 B2 B3 P1P2P3P4P5P6P7P8 |
| 107 | * \SMPL ||\TTC3SEL |
| 108 | * |\TTC_IFU_SEL |
| 109 | * \TTM2SEL0 |
| 110 | * |
| 111 | * SMPL - SAMPLE_ENABLE constraint |
| 112 | * 56: SAMPLE_ENABLE value 0x0100_0000_0000_0000 |
| 113 | * |
| 114 | * UC1 - unit constraint 1: can't have all three of FPU/ISU1/IDU0|ISU2 |
| 115 | * 55: UC1 error 0x0080_0000_0000_0000 |
| 116 | * 54: FPU events needed 0x0040_0000_0000_0000 |
| 117 | * 53: ISU1 events needed 0x0020_0000_0000_0000 |
| 118 | * 52: IDU0|ISU2 events needed 0x0010_0000_0000_0000 |
| 119 | * |
| 120 | * UC2 - unit constraint 2: can't have all three of FPU/IFU/LSU0 |
| 121 | * 51: UC2 error 0x0008_0000_0000_0000 |
| 122 | * 50: FPU events needed 0x0004_0000_0000_0000 |
| 123 | * 49: IFU events needed 0x0002_0000_0000_0000 |
| 124 | * 48: LSU0 events needed 0x0001_0000_0000_0000 |
| 125 | * |
| 126 | * UC3 - unit constraint 3: can't have all four of LSU0/IFU/IDU0|ISU2/ISU1 |
| 127 | * 47: UC3 error 0x8000_0000_0000 |
| 128 | * 46: LSU0 events needed 0x4000_0000_0000 |
| 129 | * 45: IFU events needed 0x2000_0000_0000 |
| 130 | * 44: IDU0|ISU2 events needed 0x1000_0000_0000 |
| 131 | * 43: ISU1 events needed 0x0800_0000_0000 |
| 132 | * |
| 133 | * TTM2SEL0 |
| 134 | * 42: 0 = IDU0 events needed |
| 135 | * 1 = ISU2 events needed 0x0400_0000_0000 |
| 136 | * |
| 137 | * TTC_IFU_SEL |
| 138 | * 41: 0 = IFU.U events needed |
| 139 | * 1 = IFU.L events needed 0x0200_0000_0000 |
| 140 | * |
| 141 | * TTC3SEL |
| 142 | * 40: 0 = LSU1.U events needed |
| 143 | * 1 = LSU1.L events needed 0x0100_0000_0000 |
| 144 | * |
| 145 | * PS1 |
| 146 | * 39: PS1 error 0x0080_0000_0000 |
| 147 | * 36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000 |
| 148 | * |
| 149 | * PS2 |
| 150 | * 35: PS2 error 0x0008_0000_0000 |
| 151 | * 32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000 |
| 152 | * |
| 153 | * B0 |
| 154 | * 28-31: Byte 0 event source 0xf000_0000 |
| 155 | * 1 = FPU |
| 156 | * 2 = ISU1 |
| 157 | * 3 = IFU |
| 158 | * 4 = IDU0 |
| 159 | * 7 = ISU2 |
| 160 | * 9 = LSU0 |
| 161 | * c = LSU1 |
| 162 | * f = GPS |
| 163 | * |
| 164 | * B1, B2, B3 |
| 165 | * 24-27, 20-23, 16-19: Byte 1, 2, 3 event sources |
| 166 | * |
| 167 | * P8 |
| 168 | * 15: P8 error 0x8000 |
| 169 | * 14-15: Count of events needing PMC8 |
| 170 | * |
| 171 | * P1..P7 |
| 172 | * 0-13: Count of events needing PMC1..PMC7 |
| 173 | * |
| 174 | * Note: this doesn't allow events using IFU.U to be combined with events |
| 175 | * using IFU.L, though that is feasible (using TTM0 and TTM2). However |
| 176 | * there are no listed events for IFU.L (they are debug events not |
| 177 | * verified for performance monitoring) so this shouldn't cause a |
| 178 | * problem. |
| 179 | */ |
| 180 | |
| 181 | static struct unitinfo { |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 182 | unsigned long value, mask; |
| 183 | int unit; |
| 184 | int lowerbit; |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 185 | } p4_unitinfo[16] = { |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 186 | [PM_FPU] = { 0x44000000000000ul, 0x88000000000000ul, PM_FPU, 0 }, |
| 187 | [PM_ISU1] = { 0x20080000000000ul, 0x88000000000000ul, PM_ISU1, 0 }, |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 188 | [PM_ISU1_ALT] = |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 189 | { 0x20080000000000ul, 0x88000000000000ul, PM_ISU1, 0 }, |
| 190 | [PM_IFU] = { 0x02200000000000ul, 0x08820000000000ul, PM_IFU, 41 }, |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 191 | [PM_IFU_ALT] = |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 192 | { 0x02200000000000ul, 0x08820000000000ul, PM_IFU, 41 }, |
| 193 | [PM_IDU0] = { 0x10100000000000ul, 0x80840000000000ul, PM_IDU0, 1 }, |
| 194 | [PM_ISU2] = { 0x10140000000000ul, 0x80840000000000ul, PM_ISU2, 0 }, |
| 195 | [PM_LSU0] = { 0x01400000000000ul, 0x08800000000000ul, PM_LSU0, 0 }, |
| 196 | [PM_LSU1] = { 0x00000000000000ul, 0x00010000000000ul, PM_LSU1, 40 }, |
| 197 | [PM_GPS] = { 0x00000000000000ul, 0x00000000000000ul, PM_GPS, 0 } |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static unsigned char direct_marked_event[8] = { |
| 201 | (1<<2) | (1<<3), /* PMC1: PM_MRK_GRP_DISP, PM_MRK_ST_CMPL */ |
| 202 | (1<<3) | (1<<5), /* PMC2: PM_THRESH_TIMEO, PM_MRK_BRU_FIN */ |
| 203 | (1<<3), /* PMC3: PM_MRK_ST_CMPL_INT */ |
| 204 | (1<<4) | (1<<5), /* PMC4: PM_MRK_GRP_CMPL, PM_MRK_CRU_FIN */ |
| 205 | (1<<4) | (1<<5), /* PMC5: PM_MRK_GRP_TIMEO */ |
| 206 | (1<<3) | (1<<4) | (1<<5), |
| 207 | /* PMC6: PM_MRK_ST_GPS, PM_MRK_FXU_FIN, PM_MRK_GRP_ISSUED */ |
| 208 | (1<<4) | (1<<5), /* PMC7: PM_MRK_FPU_FIN, PM_MRK_INST_FIN */ |
| 209 | (1<<4), /* PMC8: PM_MRK_LSU_FIN */ |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * Returns 1 if event counts things relating to marked instructions |
| 214 | * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not. |
| 215 | */ |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 216 | static int p4_marked_instr_event(u64 event) |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 217 | { |
| 218 | int pmc, psel, unit, byte, bit; |
| 219 | unsigned int mask; |
| 220 | |
| 221 | pmc = (event >> PM_PMC_SH) & PM_PMC_MSK; |
| 222 | psel = event & PM_PMCSEL_MSK; |
| 223 | if (pmc) { |
| 224 | if (direct_marked_event[pmc - 1] & (1 << psel)) |
| 225 | return 1; |
| 226 | if (psel == 0) /* add events */ |
| 227 | bit = (pmc <= 4)? pmc - 1: 8 - pmc; |
| 228 | else if (psel == 6) /* decode events */ |
| 229 | bit = 4; |
| 230 | else |
| 231 | return 0; |
| 232 | } else |
| 233 | bit = psel; |
| 234 | |
| 235 | byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK; |
| 236 | unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK; |
| 237 | mask = 0; |
| 238 | switch (unit) { |
| 239 | case PM_LSU1: |
| 240 | if (event & PM_LOWER_MSKS) |
| 241 | mask = 1 << 28; /* byte 7 bit 4 */ |
| 242 | else |
| 243 | mask = 6 << 24; /* byte 3 bits 1 and 2 */ |
| 244 | break; |
| 245 | case PM_LSU0: |
| 246 | /* byte 3, bit 3; byte 2 bits 0,2,3,4,5; byte 1 */ |
| 247 | mask = 0x083dff00; |
| 248 | } |
| 249 | return (mask >> (byte * 8 + bit)) & 1; |
| 250 | } |
| 251 | |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 252 | static int p4_get_constraint(u64 event, unsigned long *maskp, |
| 253 | unsigned long *valp) |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 254 | { |
| 255 | int pmc, byte, unit, lower, sh; |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 256 | unsigned long mask = 0, value = 0; |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 257 | int grp = -1; |
| 258 | |
| 259 | pmc = (event >> PM_PMC_SH) & PM_PMC_MSK; |
| 260 | if (pmc) { |
| 261 | if (pmc > 8) |
| 262 | return -1; |
| 263 | sh = (pmc - 1) * 2; |
| 264 | mask |= 2 << sh; |
| 265 | value |= 1 << sh; |
| 266 | grp = ((pmc - 1) >> 1) & 1; |
| 267 | } |
| 268 | unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK; |
| 269 | byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK; |
| 270 | if (unit) { |
| 271 | lower = (event >> PM_LOWER_SH) & PM_LOWER_MSK; |
| 272 | |
| 273 | /* |
| 274 | * Bus events on bytes 0 and 2 can be counted |
| 275 | * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8. |
| 276 | */ |
| 277 | if (!pmc) |
| 278 | grp = byte & 1; |
| 279 | |
| 280 | if (!p4_unitinfo[unit].unit) |
| 281 | return -1; |
| 282 | mask |= p4_unitinfo[unit].mask; |
| 283 | value |= p4_unitinfo[unit].value; |
| 284 | sh = p4_unitinfo[unit].lowerbit; |
| 285 | if (sh > 1) |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 286 | value |= (unsigned long)lower << sh; |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 287 | else if (lower != sh) |
| 288 | return -1; |
| 289 | unit = p4_unitinfo[unit].unit; |
| 290 | |
| 291 | /* Set byte lane select field */ |
| 292 | mask |= 0xfULL << (28 - 4 * byte); |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 293 | value |= (unsigned long)unit << (28 - 4 * byte); |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 294 | } |
| 295 | if (grp == 0) { |
| 296 | /* increment PMC1/2/5/6 field */ |
| 297 | mask |= 0x8000000000ull; |
| 298 | value |= 0x1000000000ull; |
| 299 | } else { |
| 300 | /* increment PMC3/4/7/8 field */ |
| 301 | mask |= 0x800000000ull; |
| 302 | value |= 0x100000000ull; |
| 303 | } |
| 304 | |
| 305 | /* Marked instruction events need sample_enable set */ |
| 306 | if (p4_marked_instr_event(event)) { |
| 307 | mask |= 1ull << 56; |
| 308 | value |= 1ull << 56; |
| 309 | } |
| 310 | |
| 311 | /* PMCSEL=6 decode events on byte 2 need sample_enable clear */ |
| 312 | if (pmc && (event & PM_PMCSEL_MSK) == 6 && byte == 2) |
| 313 | mask |= 1ull << 56; |
| 314 | |
| 315 | *maskp = mask; |
| 316 | *valp = value; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static unsigned int ppc_inst_cmpl[] = { |
| 321 | 0x1001, 0x4001, 0x6001, 0x7001, 0x8001 |
| 322 | }; |
| 323 | |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 324 | static int p4_get_alternatives(u64 event, unsigned int flags, u64 alt[]) |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 325 | { |
| 326 | int i, j, na; |
| 327 | |
| 328 | alt[0] = event; |
| 329 | na = 1; |
| 330 | |
| 331 | /* 2 possibilities for PM_GRP_DISP_REJECT */ |
| 332 | if (event == 0x8003 || event == 0x0224) { |
| 333 | alt[1] = event ^ (0x8003 ^ 0x0224); |
| 334 | return 2; |
| 335 | } |
| 336 | |
| 337 | /* 2 possibilities for PM_ST_MISS_L1 */ |
| 338 | if (event == 0x0c13 || event == 0x0c23) { |
| 339 | alt[1] = event ^ (0x0c13 ^ 0x0c23); |
| 340 | return 2; |
| 341 | } |
| 342 | |
| 343 | /* several possibilities for PM_INST_CMPL */ |
| 344 | for (i = 0; i < ARRAY_SIZE(ppc_inst_cmpl); ++i) { |
| 345 | if (event == ppc_inst_cmpl[i]) { |
| 346 | for (j = 0; j < ARRAY_SIZE(ppc_inst_cmpl); ++j) |
| 347 | if (j != i) |
| 348 | alt[na++] = ppc_inst_cmpl[j]; |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return na; |
| 354 | } |
| 355 | |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 356 | static int p4_compute_mmcr(u64 event[], int n_ev, |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 357 | unsigned int hwc[], unsigned long mmcr[]) |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 358 | { |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 359 | unsigned long mmcr0 = 0, mmcr1 = 0, mmcra = 0; |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 360 | unsigned int pmc, unit, byte, psel, lower; |
| 361 | unsigned int ttm, grp; |
| 362 | unsigned int pmc_inuse = 0; |
| 363 | unsigned int pmc_grp_use[2]; |
| 364 | unsigned char busbyte[4]; |
| 365 | unsigned char unituse[16]; |
| 366 | unsigned int unitlower = 0; |
| 367 | int i; |
| 368 | |
| 369 | if (n_ev > 8) |
| 370 | return -1; |
| 371 | |
| 372 | /* First pass to count resource use */ |
| 373 | pmc_grp_use[0] = pmc_grp_use[1] = 0; |
| 374 | memset(busbyte, 0, sizeof(busbyte)); |
| 375 | memset(unituse, 0, sizeof(unituse)); |
| 376 | for (i = 0; i < n_ev; ++i) { |
| 377 | pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK; |
| 378 | if (pmc) { |
| 379 | if (pmc_inuse & (1 << (pmc - 1))) |
| 380 | return -1; |
| 381 | pmc_inuse |= 1 << (pmc - 1); |
| 382 | /* count 1/2/5/6 vs 3/4/7/8 use */ |
| 383 | ++pmc_grp_use[((pmc - 1) >> 1) & 1]; |
| 384 | } |
| 385 | unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK; |
| 386 | byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK; |
| 387 | lower = (event[i] >> PM_LOWER_SH) & PM_LOWER_MSK; |
| 388 | if (unit) { |
| 389 | if (!pmc) |
| 390 | ++pmc_grp_use[byte & 1]; |
| 391 | if (unit == 6 || unit == 8) |
| 392 | /* map alt ISU1/IFU codes: 6->2, 8->3 */ |
| 393 | unit = (unit >> 1) - 1; |
| 394 | if (busbyte[byte] && busbyte[byte] != unit) |
| 395 | return -1; |
| 396 | busbyte[byte] = unit; |
| 397 | lower <<= unit; |
| 398 | if (unituse[unit] && lower != (unitlower & lower)) |
| 399 | return -1; |
| 400 | unituse[unit] = 1; |
| 401 | unitlower |= lower; |
| 402 | } |
| 403 | } |
| 404 | if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4) |
| 405 | return -1; |
| 406 | |
| 407 | /* |
| 408 | * Assign resources and set multiplexer selects. |
| 409 | * |
| 410 | * Units 1,2,3 are on TTM0, 4,6,7 on TTM1, 8,10 on TTM2. |
| 411 | * Each TTMx can only select one unit, but since |
| 412 | * units 2 and 6 are both ISU1, and 3 and 8 are both IFU, |
| 413 | * we have some choices. |
| 414 | */ |
| 415 | if (unituse[2] & (unituse[1] | (unituse[3] & unituse[9]))) { |
| 416 | unituse[6] = 1; /* Move 2 to 6 */ |
| 417 | unituse[2] = 0; |
| 418 | } |
| 419 | if (unituse[3] & (unituse[1] | unituse[2])) { |
| 420 | unituse[8] = 1; /* Move 3 to 8 */ |
| 421 | unituse[3] = 0; |
| 422 | unitlower = (unitlower & ~8) | ((unitlower & 8) << 5); |
| 423 | } |
| 424 | /* Check only one unit per TTMx */ |
| 425 | if (unituse[1] + unituse[2] + unituse[3] > 1 || |
| 426 | unituse[4] + unituse[6] + unituse[7] > 1 || |
| 427 | unituse[8] + unituse[9] > 1 || |
| 428 | (unituse[5] | unituse[10] | unituse[11] | |
| 429 | unituse[13] | unituse[14])) |
| 430 | return -1; |
| 431 | |
| 432 | /* Set TTMxSEL fields. Note, units 1-3 => TTM0SEL codes 0-2 */ |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 433 | mmcr1 |= (unsigned long)(unituse[3] * 2 + unituse[2]) |
| 434 | << MMCR1_TTM0SEL_SH; |
| 435 | mmcr1 |= (unsigned long)(unituse[7] * 3 + unituse[6] * 2) |
| 436 | << MMCR1_TTM1SEL_SH; |
| 437 | mmcr1 |= (unsigned long)unituse[9] << MMCR1_TTM2SEL_SH; |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 438 | |
| 439 | /* Set TTCxSEL fields. */ |
| 440 | if (unitlower & 0xe) |
| 441 | mmcr1 |= 1ull << MMCR1_TTC0SEL_SH; |
| 442 | if (unitlower & 0xf0) |
| 443 | mmcr1 |= 1ull << MMCR1_TTC1SEL_SH; |
| 444 | if (unitlower & 0xf00) |
| 445 | mmcr1 |= 1ull << MMCR1_TTC2SEL_SH; |
| 446 | if (unitlower & 0x7000) |
| 447 | mmcr1 |= 1ull << MMCR1_TTC3SEL_SH; |
| 448 | |
| 449 | /* Set byte lane select fields. */ |
| 450 | for (byte = 0; byte < 4; ++byte) { |
| 451 | unit = busbyte[byte]; |
| 452 | if (!unit) |
| 453 | continue; |
| 454 | if (unit == 0xf) { |
| 455 | /* special case for GPS */ |
| 456 | mmcr1 |= 1ull << (MMCR1_DEBUG0SEL_SH - byte); |
| 457 | } else { |
| 458 | if (!unituse[unit]) |
| 459 | ttm = unit - 1; /* 2->1, 3->2 */ |
| 460 | else |
| 461 | ttm = unit >> 2; |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 462 | mmcr1 |= (unsigned long)ttm |
| 463 | << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte); |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */ |
| 468 | for (i = 0; i < n_ev; ++i) { |
| 469 | pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK; |
| 470 | unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK; |
| 471 | byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK; |
| 472 | psel = event[i] & PM_PMCSEL_MSK; |
| 473 | if (!pmc) { |
| 474 | /* Bus event or 00xxx direct event (off or cycles) */ |
| 475 | if (unit) |
| 476 | psel |= 0x10 | ((byte & 2) << 2); |
| 477 | for (pmc = 0; pmc < 8; ++pmc) { |
| 478 | if (pmc_inuse & (1 << pmc)) |
| 479 | continue; |
| 480 | grp = (pmc >> 1) & 1; |
| 481 | if (unit) { |
| 482 | if (grp == (byte & 1)) |
| 483 | break; |
| 484 | } else if (pmc_grp_use[grp] < 4) { |
| 485 | ++pmc_grp_use[grp]; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | pmc_inuse |= 1 << pmc; |
| 490 | } else { |
| 491 | /* Direct event */ |
| 492 | --pmc; |
| 493 | if (psel == 0 && (byte & 2)) |
| 494 | /* add events on higher-numbered bus */ |
| 495 | mmcr1 |= 1ull << mmcr1_adder_bits[pmc]; |
| 496 | else if (psel == 6 && byte == 3) |
| 497 | /* seem to need to set sample_enable here */ |
| 498 | mmcra |= MMCRA_SAMPLE_ENABLE; |
| 499 | psel |= 8; |
| 500 | } |
| 501 | if (pmc <= 1) |
| 502 | mmcr0 |= psel << (MMCR0_PMC1SEL_SH - 7 * pmc); |
| 503 | else |
| 504 | mmcr1 |= psel << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2)); |
| 505 | if (pmc == 7) /* PMC8 */ |
| 506 | mmcra |= (psel & 1) << MMCRA_PMC8SEL0_SH; |
| 507 | hwc[i] = pmc; |
| 508 | if (p4_marked_instr_event(event[i])) |
| 509 | mmcra |= MMCRA_SAMPLE_ENABLE; |
| 510 | } |
| 511 | |
| 512 | if (pmc_inuse & 1) |
| 513 | mmcr0 |= MMCR0_PMC1CE; |
| 514 | if (pmc_inuse & 0xfe) |
| 515 | mmcr0 |= MMCR0_PMCjCE; |
| 516 | |
| 517 | mmcra |= 0x2000; /* mark only one IOP per PPC instruction */ |
| 518 | |
| 519 | /* Return MMCRx values */ |
| 520 | mmcr[0] = mmcr0; |
| 521 | mmcr[1] = mmcr1; |
| 522 | mmcr[2] = mmcra; |
| 523 | return 0; |
| 524 | } |
| 525 | |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 526 | static void p4_disable_pmc(unsigned int pmc, unsigned long mmcr[]) |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 527 | { |
| 528 | /* |
| 529 | * Setting the PMCxSEL field to 0 disables PMC x. |
| 530 | * (Note that pmc is 0-based here, not 1-based.) |
| 531 | */ |
| 532 | if (pmc <= 1) { |
| 533 | mmcr[0] &= ~(0x1fUL << (MMCR0_PMC1SEL_SH - 7 * pmc)); |
| 534 | } else { |
| 535 | mmcr[1] &= ~(0x1fUL << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2))); |
| 536 | if (pmc == 7) |
| 537 | mmcr[2] &= ~(1UL << MMCRA_PMC8SEL0_SH); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static int p4_generic_events[] = { |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 542 | [PERF_COUNT_HW_CPU_CYCLES] = 7, |
| 543 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x1001, |
| 544 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x8c10, /* PM_LD_REF_L1 */ |
| 545 | [PERF_COUNT_HW_CACHE_MISSES] = 0x3c10, /* PM_LD_MISS_L1 */ |
| 546 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x330, /* PM_BR_ISSUED */ |
| 547 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x331, /* PM_BR_MPRED_CR */ |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 548 | }; |
| 549 | |
Paul Mackerras | 106b506 | 2009-06-11 14:55:42 +1000 | [diff] [blame] | 550 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 551 | |
| 552 | /* |
| 553 | * Table of generalized cache-related events. |
| 554 | * 0 means not supported, -1 means nonsensical, other values |
| 555 | * are event codes. |
| 556 | */ |
| 557 | static int power4_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = { |
| 558 | [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */ |
| 559 | [C(OP_READ)] = { 0x8c10, 0x3c10 }, |
| 560 | [C(OP_WRITE)] = { 0x7c10, 0xc13 }, |
| 561 | [C(OP_PREFETCH)] = { 0xc35, 0 }, |
| 562 | }, |
| 563 | [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */ |
| 564 | [C(OP_READ)] = { 0, 0 }, |
| 565 | [C(OP_WRITE)] = { -1, -1 }, |
| 566 | [C(OP_PREFETCH)] = { 0, 0 }, |
| 567 | }, |
Peter Zijlstra | 8be6e8f | 2009-06-11 14:19:11 +0200 | [diff] [blame] | 568 | [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */ |
Paul Mackerras | 106b506 | 2009-06-11 14:55:42 +1000 | [diff] [blame] | 569 | [C(OP_READ)] = { 0, 0 }, |
| 570 | [C(OP_WRITE)] = { 0, 0 }, |
| 571 | [C(OP_PREFETCH)] = { 0xc34, 0 }, |
| 572 | }, |
| 573 | [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */ |
| 574 | [C(OP_READ)] = { 0, 0x904 }, |
| 575 | [C(OP_WRITE)] = { -1, -1 }, |
| 576 | [C(OP_PREFETCH)] = { -1, -1 }, |
| 577 | }, |
| 578 | [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */ |
| 579 | [C(OP_READ)] = { 0, 0x900 }, |
| 580 | [C(OP_WRITE)] = { -1, -1 }, |
| 581 | [C(OP_PREFETCH)] = { -1, -1 }, |
| 582 | }, |
| 583 | [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */ |
| 584 | [C(OP_READ)] = { 0x330, 0x331 }, |
| 585 | [C(OP_WRITE)] = { -1, -1 }, |
| 586 | [C(OP_PREFETCH)] = { -1, -1 }, |
| 587 | }, |
| 588 | }; |
| 589 | |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 590 | struct power_pmu power4_pmu = { |
Paul Mackerras | 448d64f | 2009-06-17 21:51:13 +1000 | [diff] [blame^] | 591 | .n_counter = 8, |
| 592 | .max_alternatives = 5, |
| 593 | .add_fields = 0x0000001100005555ul, |
| 594 | .test_adder = 0x0011083300000000ul, |
| 595 | .compute_mmcr = p4_compute_mmcr, |
| 596 | .get_constraint = p4_get_constraint, |
| 597 | .get_alternatives = p4_get_alternatives, |
| 598 | .disable_pmc = p4_disable_pmc, |
| 599 | .n_generic = ARRAY_SIZE(p4_generic_events), |
| 600 | .generic_events = p4_generic_events, |
| 601 | .cache_events = &power4_cache_events, |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 602 | }; |