Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Performance counter support - powerpc architecture code |
| 3 | * |
| 4 | * Copyright 2008-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/sched.h> |
| 13 | #include <linux/perf_counter.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/hardirq.h> |
| 16 | #include <asm/reg.h> |
| 17 | #include <asm/pmc.h> |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 18 | #include <asm/machdep.h> |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 19 | #include <asm/firmware.h> |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 20 | #include <asm/ptrace.h> |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 21 | |
| 22 | struct cpu_hw_counters { |
| 23 | int n_counters; |
| 24 | int n_percpu; |
| 25 | int disabled; |
| 26 | int n_added; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 27 | int n_limited; |
| 28 | u8 pmcs_enabled; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 29 | struct perf_counter *counter[MAX_HWCOUNTERS]; |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 30 | u64 events[MAX_HWCOUNTERS]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 31 | unsigned int flags[MAX_HWCOUNTERS]; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 32 | u64 mmcr[3]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 33 | struct perf_counter *limited_counter[MAX_LIMITED_HWCOUNTERS]; |
| 34 | u8 limited_hwidx[MAX_LIMITED_HWCOUNTERS]; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 35 | }; |
| 36 | DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters); |
| 37 | |
| 38 | struct power_pmu *ppmu; |
| 39 | |
Paul Mackerras | d095cd4 | 2009-02-23 23:01:28 +1100 | [diff] [blame] | 40 | /* |
| 41 | * Normally, to ignore kernel events we set the FCS (freeze counters |
| 42 | * in supervisor mode) bit in MMCR0, but if the kernel runs with the |
| 43 | * hypervisor bit set in the MSR, or if we are running on a processor |
| 44 | * where the hypervisor bit is forced to 1 (as on Apple G5 processors), |
| 45 | * then we need to use the FCHV bit to ignore kernel events. |
| 46 | */ |
| 47 | static unsigned int freeze_counters_kernel = MMCR0_FCS; |
| 48 | |
Paul Mackerras | 7595d63 | 2009-03-30 19:07:07 +0200 | [diff] [blame] | 49 | static void perf_counter_interrupt(struct pt_regs *regs); |
| 50 | |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 51 | void perf_counter_print_debug(void) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 56 | * Read one performance monitor counter (PMC). |
| 57 | */ |
| 58 | static unsigned long read_pmc(int idx) |
| 59 | { |
| 60 | unsigned long val; |
| 61 | |
| 62 | switch (idx) { |
| 63 | case 1: |
| 64 | val = mfspr(SPRN_PMC1); |
| 65 | break; |
| 66 | case 2: |
| 67 | val = mfspr(SPRN_PMC2); |
| 68 | break; |
| 69 | case 3: |
| 70 | val = mfspr(SPRN_PMC3); |
| 71 | break; |
| 72 | case 4: |
| 73 | val = mfspr(SPRN_PMC4); |
| 74 | break; |
| 75 | case 5: |
| 76 | val = mfspr(SPRN_PMC5); |
| 77 | break; |
| 78 | case 6: |
| 79 | val = mfspr(SPRN_PMC6); |
| 80 | break; |
| 81 | case 7: |
| 82 | val = mfspr(SPRN_PMC7); |
| 83 | break; |
| 84 | case 8: |
| 85 | val = mfspr(SPRN_PMC8); |
| 86 | break; |
| 87 | default: |
| 88 | printk(KERN_ERR "oops trying to read PMC%d\n", idx); |
| 89 | val = 0; |
| 90 | } |
| 91 | return val; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Write one PMC. |
| 96 | */ |
| 97 | static void write_pmc(int idx, unsigned long val) |
| 98 | { |
| 99 | switch (idx) { |
| 100 | case 1: |
| 101 | mtspr(SPRN_PMC1, val); |
| 102 | break; |
| 103 | case 2: |
| 104 | mtspr(SPRN_PMC2, val); |
| 105 | break; |
| 106 | case 3: |
| 107 | mtspr(SPRN_PMC3, val); |
| 108 | break; |
| 109 | case 4: |
| 110 | mtspr(SPRN_PMC4, val); |
| 111 | break; |
| 112 | case 5: |
| 113 | mtspr(SPRN_PMC5, val); |
| 114 | break; |
| 115 | case 6: |
| 116 | mtspr(SPRN_PMC6, val); |
| 117 | break; |
| 118 | case 7: |
| 119 | mtspr(SPRN_PMC7, val); |
| 120 | break; |
| 121 | case 8: |
| 122 | mtspr(SPRN_PMC8, val); |
| 123 | break; |
| 124 | default: |
| 125 | printk(KERN_ERR "oops trying to write PMC%d\n", idx); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Check if a set of events can all go on the PMU at once. |
| 131 | * If they can't, this will look at alternative codes for the events |
| 132 | * and see if any combination of alternative codes is feasible. |
| 133 | * The feasible set is returned in event[]. |
| 134 | */ |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 135 | static int power_check_constraints(u64 event[], unsigned int cflags[], |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 136 | int n_ev) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 137 | { |
| 138 | u64 mask, value, nv; |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 139 | u64 alternatives[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES]; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 140 | u64 amasks[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES]; |
| 141 | u64 avalues[MAX_HWCOUNTERS][MAX_EVENT_ALTERNATIVES]; |
| 142 | u64 smasks[MAX_HWCOUNTERS], svalues[MAX_HWCOUNTERS]; |
| 143 | int n_alt[MAX_HWCOUNTERS], choice[MAX_HWCOUNTERS]; |
| 144 | int i, j; |
| 145 | u64 addf = ppmu->add_fields; |
| 146 | u64 tadd = ppmu->test_adder; |
| 147 | |
| 148 | if (n_ev > ppmu->n_counter) |
| 149 | return -1; |
| 150 | |
| 151 | /* First see if the events will go on as-is */ |
| 152 | for (i = 0; i < n_ev; ++i) { |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 153 | if ((cflags[i] & PPMU_LIMITED_PMC_REQD) |
| 154 | && !ppmu->limited_pmc_event(event[i])) { |
| 155 | ppmu->get_alternatives(event[i], cflags[i], |
| 156 | alternatives[i]); |
| 157 | event[i] = alternatives[i][0]; |
| 158 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 159 | if (ppmu->get_constraint(event[i], &amasks[i][0], |
| 160 | &avalues[i][0])) |
| 161 | return -1; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 162 | } |
| 163 | value = mask = 0; |
| 164 | for (i = 0; i < n_ev; ++i) { |
| 165 | nv = (value | avalues[i][0]) + (value & avalues[i][0] & addf); |
| 166 | if ((((nv + tadd) ^ value) & mask) != 0 || |
| 167 | (((nv + tadd) ^ avalues[i][0]) & amasks[i][0]) != 0) |
| 168 | break; |
| 169 | value = nv; |
| 170 | mask |= amasks[i][0]; |
| 171 | } |
| 172 | if (i == n_ev) |
| 173 | return 0; /* all OK */ |
| 174 | |
| 175 | /* doesn't work, gather alternatives... */ |
| 176 | if (!ppmu->get_alternatives) |
| 177 | return -1; |
| 178 | for (i = 0; i < n_ev; ++i) { |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 179 | choice[i] = 0; |
| 180 | n_alt[i] = ppmu->get_alternatives(event[i], cflags[i], |
| 181 | alternatives[i]); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 182 | for (j = 1; j < n_alt[i]; ++j) |
| 183 | ppmu->get_constraint(alternatives[i][j], |
| 184 | &amasks[i][j], &avalues[i][j]); |
| 185 | } |
| 186 | |
| 187 | /* enumerate all possibilities and see if any will work */ |
| 188 | i = 0; |
| 189 | j = -1; |
| 190 | value = mask = nv = 0; |
| 191 | while (i < n_ev) { |
| 192 | if (j >= 0) { |
| 193 | /* we're backtracking, restore context */ |
| 194 | value = svalues[i]; |
| 195 | mask = smasks[i]; |
| 196 | j = choice[i]; |
| 197 | } |
| 198 | /* |
| 199 | * See if any alternative k for event i, |
| 200 | * where k > j, will satisfy the constraints. |
| 201 | */ |
| 202 | while (++j < n_alt[i]) { |
| 203 | nv = (value | avalues[i][j]) + |
| 204 | (value & avalues[i][j] & addf); |
| 205 | if ((((nv + tadd) ^ value) & mask) == 0 && |
| 206 | (((nv + tadd) ^ avalues[i][j]) |
| 207 | & amasks[i][j]) == 0) |
| 208 | break; |
| 209 | } |
| 210 | if (j >= n_alt[i]) { |
| 211 | /* |
| 212 | * No feasible alternative, backtrack |
| 213 | * to event i-1 and continue enumerating its |
| 214 | * alternatives from where we got up to. |
| 215 | */ |
| 216 | if (--i < 0) |
| 217 | return -1; |
| 218 | } else { |
| 219 | /* |
| 220 | * Found a feasible alternative for event i, |
| 221 | * remember where we got up to with this event, |
| 222 | * go on to the next event, and start with |
| 223 | * the first alternative for it. |
| 224 | */ |
| 225 | choice[i] = j; |
| 226 | svalues[i] = value; |
| 227 | smasks[i] = mask; |
| 228 | value = nv; |
| 229 | mask |= amasks[i][j]; |
| 230 | ++i; |
| 231 | j = -1; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* OK, we have a feasible combination, tell the caller the solution */ |
| 236 | for (i = 0; i < n_ev; ++i) |
| 237 | event[i] = alternatives[i][choice[i]]; |
| 238 | return 0; |
| 239 | } |
| 240 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 241 | /* |
| 242 | * Check if newly-added counters have consistent settings for |
| 243 | * exclude_{user,kernel,hv} with each other and any previously |
| 244 | * added counters. |
| 245 | */ |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 246 | static int check_excludes(struct perf_counter **ctrs, unsigned int cflags[], |
| 247 | int n_prev, int n_new) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 248 | { |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 249 | int eu = 0, ek = 0, eh = 0; |
| 250 | int i, n, first; |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 251 | struct perf_counter *counter; |
| 252 | |
| 253 | n = n_prev + n_new; |
| 254 | if (n <= 1) |
| 255 | return 0; |
| 256 | |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 257 | first = 1; |
| 258 | for (i = 0; i < n; ++i) { |
| 259 | if (cflags[i] & PPMU_LIMITED_PMC_OK) { |
| 260 | cflags[i] &= ~PPMU_LIMITED_PMC_REQD; |
| 261 | continue; |
| 262 | } |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 263 | counter = ctrs[i]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 264 | if (first) { |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 265 | eu = counter->attr.exclude_user; |
| 266 | ek = counter->attr.exclude_kernel; |
| 267 | eh = counter->attr.exclude_hv; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 268 | first = 0; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 269 | } else if (counter->attr.exclude_user != eu || |
| 270 | counter->attr.exclude_kernel != ek || |
| 271 | counter->attr.exclude_hv != eh) { |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 272 | return -EAGAIN; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 273 | } |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 274 | } |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 275 | |
| 276 | if (eu || ek || eh) |
| 277 | for (i = 0; i < n; ++i) |
| 278 | if (cflags[i] & PPMU_LIMITED_PMC_OK) |
| 279 | cflags[i] |= PPMU_LIMITED_PMC_REQD; |
| 280 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 284 | static void power_pmu_read(struct perf_counter *counter) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 285 | { |
| 286 | long val, delta, prev; |
| 287 | |
| 288 | if (!counter->hw.idx) |
| 289 | return; |
| 290 | /* |
| 291 | * Performance monitor interrupts come even when interrupts |
| 292 | * are soft-disabled, as long as interrupts are hard-enabled. |
| 293 | * Therefore we treat them like NMIs. |
| 294 | */ |
| 295 | do { |
| 296 | prev = atomic64_read(&counter->hw.prev_count); |
| 297 | barrier(); |
| 298 | val = read_pmc(counter->hw.idx); |
| 299 | } while (atomic64_cmpxchg(&counter->hw.prev_count, prev, val) != prev); |
| 300 | |
| 301 | /* The counters are only 32 bits wide */ |
| 302 | delta = (val - prev) & 0xfffffffful; |
| 303 | atomic64_add(delta, &counter->count); |
| 304 | atomic64_sub(delta, &counter->hw.period_left); |
| 305 | } |
| 306 | |
| 307 | /* |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 308 | * On some machines, PMC5 and PMC6 can't be written, don't respect |
| 309 | * the freeze conditions, and don't generate interrupts. This tells |
| 310 | * us if `counter' is using such a PMC. |
| 311 | */ |
| 312 | static int is_limited_pmc(int pmcnum) |
| 313 | { |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 314 | return (ppmu->flags & PPMU_LIMITED_PMC5_6) |
| 315 | && (pmcnum == 5 || pmcnum == 6); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void freeze_limited_counters(struct cpu_hw_counters *cpuhw, |
| 319 | unsigned long pmc5, unsigned long pmc6) |
| 320 | { |
| 321 | struct perf_counter *counter; |
| 322 | u64 val, prev, delta; |
| 323 | int i; |
| 324 | |
| 325 | for (i = 0; i < cpuhw->n_limited; ++i) { |
| 326 | counter = cpuhw->limited_counter[i]; |
| 327 | if (!counter->hw.idx) |
| 328 | continue; |
| 329 | val = (counter->hw.idx == 5) ? pmc5 : pmc6; |
| 330 | prev = atomic64_read(&counter->hw.prev_count); |
| 331 | counter->hw.idx = 0; |
| 332 | delta = (val - prev) & 0xfffffffful; |
| 333 | atomic64_add(delta, &counter->count); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static void thaw_limited_counters(struct cpu_hw_counters *cpuhw, |
| 338 | unsigned long pmc5, unsigned long pmc6) |
| 339 | { |
| 340 | struct perf_counter *counter; |
| 341 | u64 val; |
| 342 | int i; |
| 343 | |
| 344 | for (i = 0; i < cpuhw->n_limited; ++i) { |
| 345 | counter = cpuhw->limited_counter[i]; |
| 346 | counter->hw.idx = cpuhw->limited_hwidx[i]; |
| 347 | val = (counter->hw.idx == 5) ? pmc5 : pmc6; |
| 348 | atomic64_set(&counter->hw.prev_count, val); |
| 349 | perf_counter_update_userpage(counter); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Since limited counters don't respect the freeze conditions, we |
| 355 | * have to read them immediately after freezing or unfreezing the |
| 356 | * other counters. We try to keep the values from the limited |
| 357 | * counters as consistent as possible by keeping the delay (in |
| 358 | * cycles and instructions) between freezing/unfreezing and reading |
| 359 | * the limited counters as small and consistent as possible. |
| 360 | * Therefore, if any limited counters are in use, we read them |
| 361 | * both, and always in the same order, to minimize variability, |
| 362 | * and do it inside the same asm that writes MMCR0. |
| 363 | */ |
| 364 | static void write_mmcr0(struct cpu_hw_counters *cpuhw, unsigned long mmcr0) |
| 365 | { |
| 366 | unsigned long pmc5, pmc6; |
| 367 | |
| 368 | if (!cpuhw->n_limited) { |
| 369 | mtspr(SPRN_MMCR0, mmcr0); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Write MMCR0, then read PMC5 and PMC6 immediately. |
| 375 | */ |
| 376 | asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5" |
| 377 | : "=&r" (pmc5), "=&r" (pmc6) |
| 378 | : "r" (mmcr0), "i" (SPRN_MMCR0), |
| 379 | "i" (SPRN_PMC5), "i" (SPRN_PMC6)); |
| 380 | |
| 381 | if (mmcr0 & MMCR0_FC) |
| 382 | freeze_limited_counters(cpuhw, pmc5, pmc6); |
| 383 | else |
| 384 | thaw_limited_counters(cpuhw, pmc5, pmc6); |
| 385 | } |
| 386 | |
| 387 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 388 | * Disable all counters to prevent PMU interrupts and to allow |
| 389 | * counters to be added or removed. |
| 390 | */ |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 391 | void hw_perf_disable(void) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 392 | { |
| 393 | struct cpu_hw_counters *cpuhw; |
| 394 | unsigned long ret; |
| 395 | unsigned long flags; |
| 396 | |
| 397 | local_irq_save(flags); |
| 398 | cpuhw = &__get_cpu_var(cpu_hw_counters); |
| 399 | |
| 400 | ret = cpuhw->disabled; |
| 401 | if (!ret) { |
| 402 | cpuhw->disabled = 1; |
| 403 | cpuhw->n_added = 0; |
| 404 | |
| 405 | /* |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 406 | * Check if we ever enabled the PMU on this cpu. |
| 407 | */ |
| 408 | if (!cpuhw->pmcs_enabled) { |
| 409 | if (ppc_md.enable_pmcs) |
| 410 | ppc_md.enable_pmcs(); |
| 411 | cpuhw->pmcs_enabled = 1; |
| 412 | } |
| 413 | |
| 414 | /* |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 415 | * Disable instruction sampling if it was enabled |
| 416 | */ |
| 417 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 418 | mtspr(SPRN_MMCRA, |
| 419 | cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
| 420 | mb(); |
| 421 | } |
| 422 | |
| 423 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 424 | * Set the 'freeze counters' bit. |
| 425 | * The barrier is to make sure the mtspr has been |
| 426 | * executed and the PMU has frozen the counters |
| 427 | * before we return. |
| 428 | */ |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 429 | write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 430 | mb(); |
| 431 | } |
| 432 | local_irq_restore(flags); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Re-enable all counters if disable == 0. |
| 437 | * If we were previously disabled and counters were added, then |
| 438 | * put the new config on the PMU. |
| 439 | */ |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 440 | void hw_perf_enable(void) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 441 | { |
| 442 | struct perf_counter *counter; |
| 443 | struct cpu_hw_counters *cpuhw; |
| 444 | unsigned long flags; |
| 445 | long i; |
| 446 | unsigned long val; |
| 447 | s64 left; |
| 448 | unsigned int hwc_index[MAX_HWCOUNTERS]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 449 | int n_lim; |
| 450 | int idx; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 451 | |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 452 | local_irq_save(flags); |
Paul Mackerras | c0daaf3 | 2009-05-18 14:02:12 +1000 | [diff] [blame] | 453 | cpuhw = &__get_cpu_var(cpu_hw_counters); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 454 | if (!cpuhw->disabled) { |
| 455 | local_irq_restore(flags); |
| 456 | return; |
| 457 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 458 | cpuhw->disabled = 0; |
| 459 | |
| 460 | /* |
| 461 | * If we didn't change anything, or only removed counters, |
| 462 | * no need to recalculate MMCR* settings and reset the PMCs. |
| 463 | * Just reenable the PMU with the current MMCR* settings |
| 464 | * (possibly updated for removal of counters). |
| 465 | */ |
| 466 | if (!cpuhw->n_added) { |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 467 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 468 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 469 | if (cpuhw->n_counters == 0) |
| 470 | get_lppaca()->pmcregs_in_use = 0; |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 471 | goto out_enable; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Compute MMCR* values for the new set of counters |
| 476 | */ |
| 477 | if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_counters, hwc_index, |
| 478 | cpuhw->mmcr)) { |
| 479 | /* shouldn't ever get here */ |
| 480 | printk(KERN_ERR "oops compute_mmcr failed\n"); |
| 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 485 | * Add in MMCR0 freeze bits corresponding to the |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 486 | * attr.exclude_* bits for the first counter. |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 487 | * We have already checked that all counters have the |
| 488 | * same values for these bits as the first counter. |
| 489 | */ |
| 490 | counter = cpuhw->counter[0]; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 491 | if (counter->attr.exclude_user) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 492 | cpuhw->mmcr[0] |= MMCR0_FCP; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 493 | if (counter->attr.exclude_kernel) |
Paul Mackerras | d095cd4 | 2009-02-23 23:01:28 +1100 | [diff] [blame] | 494 | cpuhw->mmcr[0] |= freeze_counters_kernel; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 495 | if (counter->attr.exclude_hv) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 496 | cpuhw->mmcr[0] |= MMCR0_FCHV; |
| 497 | |
| 498 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 499 | * Write the new configuration to MMCR* with the freeze |
| 500 | * bit set and set the hardware counters to their initial values. |
| 501 | * Then unfreeze the counters. |
| 502 | */ |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 503 | get_lppaca()->pmcregs_in_use = 1; |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 504 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 505 | mtspr(SPRN_MMCR1, cpuhw->mmcr[1]); |
| 506 | mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)) |
| 507 | | MMCR0_FC); |
| 508 | |
| 509 | /* |
| 510 | * Read off any pre-existing counters that need to move |
| 511 | * to another PMC. |
| 512 | */ |
| 513 | for (i = 0; i < cpuhw->n_counters; ++i) { |
| 514 | counter = cpuhw->counter[i]; |
| 515 | if (counter->hw.idx && counter->hw.idx != hwc_index[i] + 1) { |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 516 | power_pmu_read(counter); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 517 | write_pmc(counter->hw.idx, 0); |
| 518 | counter->hw.idx = 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Initialize the PMCs for all the new and moved counters. |
| 524 | */ |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 525 | cpuhw->n_limited = n_lim = 0; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 526 | for (i = 0; i < cpuhw->n_counters; ++i) { |
| 527 | counter = cpuhw->counter[i]; |
| 528 | if (counter->hw.idx) |
| 529 | continue; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 530 | idx = hwc_index[i] + 1; |
| 531 | if (is_limited_pmc(idx)) { |
| 532 | cpuhw->limited_counter[n_lim] = counter; |
| 533 | cpuhw->limited_hwidx[n_lim] = idx; |
| 534 | ++n_lim; |
| 535 | continue; |
| 536 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 537 | val = 0; |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 538 | if (counter->hw.sample_period) { |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 539 | left = atomic64_read(&counter->hw.period_left); |
| 540 | if (left < 0x80000000L) |
| 541 | val = 0x80000000L - left; |
| 542 | } |
| 543 | atomic64_set(&counter->hw.prev_count, val); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 544 | counter->hw.idx = idx; |
| 545 | write_pmc(idx, val); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 546 | perf_counter_update_userpage(counter); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 547 | } |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 548 | cpuhw->n_limited = n_lim; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 549 | cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE; |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 550 | |
| 551 | out_enable: |
| 552 | mb(); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 553 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 554 | |
Paul Mackerras | f708223 | 2009-04-08 20:30:18 +1000 | [diff] [blame] | 555 | /* |
| 556 | * Enable instruction sampling if necessary |
| 557 | */ |
| 558 | if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) { |
| 559 | mb(); |
| 560 | mtspr(SPRN_MMCRA, cpuhw->mmcr[2]); |
| 561 | } |
| 562 | |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 563 | out: |
| 564 | local_irq_restore(flags); |
| 565 | } |
| 566 | |
| 567 | static int collect_events(struct perf_counter *group, int max_count, |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 568 | struct perf_counter *ctrs[], u64 *events, |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 569 | unsigned int *flags) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 570 | { |
| 571 | int n = 0; |
| 572 | struct perf_counter *counter; |
| 573 | |
| 574 | if (!is_software_counter(group)) { |
| 575 | if (n >= max_count) |
| 576 | return -1; |
| 577 | ctrs[n] = group; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 578 | flags[n] = group->hw.counter_base; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 579 | events[n++] = group->hw.config; |
| 580 | } |
| 581 | list_for_each_entry(counter, &group->sibling_list, list_entry) { |
| 582 | if (!is_software_counter(counter) && |
| 583 | counter->state != PERF_COUNTER_STATE_OFF) { |
| 584 | if (n >= max_count) |
| 585 | return -1; |
| 586 | ctrs[n] = counter; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 587 | flags[n] = counter->hw.counter_base; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 588 | events[n++] = counter->hw.config; |
| 589 | } |
| 590 | } |
| 591 | return n; |
| 592 | } |
| 593 | |
| 594 | static void counter_sched_in(struct perf_counter *counter, int cpu) |
| 595 | { |
| 596 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
| 597 | counter->oncpu = cpu; |
Paul Mackerras | dc66270 | 2009-04-08 20:30:10 +1000 | [diff] [blame] | 598 | counter->tstamp_running += counter->ctx->time - counter->tstamp_stopped; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 599 | if (is_software_counter(counter)) |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 600 | counter->pmu->enable(counter); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Called to enable a whole group of counters. |
| 605 | * Returns 1 if the group was enabled, or -EAGAIN if it could not be. |
| 606 | * Assumes the caller has disabled interrupts and has |
| 607 | * frozen the PMU with hw_perf_save_disable. |
| 608 | */ |
| 609 | int hw_perf_group_sched_in(struct perf_counter *group_leader, |
| 610 | struct perf_cpu_context *cpuctx, |
| 611 | struct perf_counter_context *ctx, int cpu) |
| 612 | { |
| 613 | struct cpu_hw_counters *cpuhw; |
| 614 | long i, n, n0; |
| 615 | struct perf_counter *sub; |
| 616 | |
| 617 | cpuhw = &__get_cpu_var(cpu_hw_counters); |
| 618 | n0 = cpuhw->n_counters; |
| 619 | n = collect_events(group_leader, ppmu->n_counter - n0, |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 620 | &cpuhw->counter[n0], &cpuhw->events[n0], |
| 621 | &cpuhw->flags[n0]); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 622 | if (n < 0) |
| 623 | return -EAGAIN; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 624 | if (check_excludes(cpuhw->counter, cpuhw->flags, n0, n)) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 625 | return -EAGAIN; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 626 | i = power_check_constraints(cpuhw->events, cpuhw->flags, n + n0); |
| 627 | if (i < 0) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 628 | return -EAGAIN; |
| 629 | cpuhw->n_counters = n0 + n; |
| 630 | cpuhw->n_added += n; |
| 631 | |
| 632 | /* |
| 633 | * OK, this group can go on; update counter states etc., |
| 634 | * and enable any software counters |
| 635 | */ |
| 636 | for (i = n0; i < n0 + n; ++i) |
| 637 | cpuhw->counter[i]->hw.config = cpuhw->events[i]; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 638 | cpuctx->active_oncpu += n; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 639 | n = 1; |
| 640 | counter_sched_in(group_leader, cpu); |
| 641 | list_for_each_entry(sub, &group_leader->sibling_list, list_entry) { |
| 642 | if (sub->state != PERF_COUNTER_STATE_OFF) { |
| 643 | counter_sched_in(sub, cpu); |
| 644 | ++n; |
| 645 | } |
| 646 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 647 | ctx->nr_active += n; |
| 648 | |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Add a counter to the PMU. |
| 654 | * If all counters are not already frozen, then we disable and |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 655 | * re-enable the PMU in order to get hw_perf_enable to do the |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 656 | * actual work of reconfiguring the PMU. |
| 657 | */ |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 658 | static int power_pmu_enable(struct perf_counter *counter) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 659 | { |
| 660 | struct cpu_hw_counters *cpuhw; |
| 661 | unsigned long flags; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 662 | int n0; |
| 663 | int ret = -EAGAIN; |
| 664 | |
| 665 | local_irq_save(flags); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 666 | perf_disable(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * Add the counter to the list (if there is room) |
| 670 | * and check whether the total set is still feasible. |
| 671 | */ |
| 672 | cpuhw = &__get_cpu_var(cpu_hw_counters); |
| 673 | n0 = cpuhw->n_counters; |
| 674 | if (n0 >= ppmu->n_counter) |
| 675 | goto out; |
| 676 | cpuhw->counter[n0] = counter; |
| 677 | cpuhw->events[n0] = counter->hw.config; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 678 | cpuhw->flags[n0] = counter->hw.counter_base; |
| 679 | if (check_excludes(cpuhw->counter, cpuhw->flags, n0, 1)) |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 680 | goto out; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 681 | if (power_check_constraints(cpuhw->events, cpuhw->flags, n0 + 1)) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 682 | goto out; |
| 683 | |
| 684 | counter->hw.config = cpuhw->events[n0]; |
| 685 | ++cpuhw->n_counters; |
| 686 | ++cpuhw->n_added; |
| 687 | |
| 688 | ret = 0; |
| 689 | out: |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 690 | perf_enable(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 691 | local_irq_restore(flags); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Remove a counter from the PMU. |
| 697 | */ |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 698 | static void power_pmu_disable(struct perf_counter *counter) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 699 | { |
| 700 | struct cpu_hw_counters *cpuhw; |
| 701 | long i; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 702 | unsigned long flags; |
| 703 | |
| 704 | local_irq_save(flags); |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 705 | perf_disable(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 706 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 707 | power_pmu_read(counter); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 708 | |
| 709 | cpuhw = &__get_cpu_var(cpu_hw_counters); |
| 710 | for (i = 0; i < cpuhw->n_counters; ++i) { |
| 711 | if (counter == cpuhw->counter[i]) { |
| 712 | while (++i < cpuhw->n_counters) |
| 713 | cpuhw->counter[i-1] = cpuhw->counter[i]; |
| 714 | --cpuhw->n_counters; |
| 715 | ppmu->disable_pmc(counter->hw.idx - 1, cpuhw->mmcr); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 716 | if (counter->hw.idx) { |
| 717 | write_pmc(counter->hw.idx, 0); |
| 718 | counter->hw.idx = 0; |
| 719 | } |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 720 | perf_counter_update_userpage(counter); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 724 | for (i = 0; i < cpuhw->n_limited; ++i) |
| 725 | if (counter == cpuhw->limited_counter[i]) |
| 726 | break; |
| 727 | if (i < cpuhw->n_limited) { |
| 728 | while (++i < cpuhw->n_limited) { |
| 729 | cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i]; |
| 730 | cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i]; |
| 731 | } |
| 732 | --cpuhw->n_limited; |
| 733 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 734 | if (cpuhw->n_counters == 0) { |
| 735 | /* disable exceptions if no counters are running */ |
| 736 | cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE); |
| 737 | } |
| 738 | |
Peter Zijlstra | 9e35ad3 | 2009-05-13 16:21:38 +0200 | [diff] [blame] | 739 | perf_enable(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 740 | local_irq_restore(flags); |
| 741 | } |
| 742 | |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 743 | /* |
| 744 | * Re-enable interrupts on a counter after they were throttled |
| 745 | * because they were coming too fast. |
| 746 | */ |
| 747 | static void power_pmu_unthrottle(struct perf_counter *counter) |
| 748 | { |
| 749 | s64 val, left; |
| 750 | unsigned long flags; |
| 751 | |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 752 | if (!counter->hw.idx || !counter->hw.sample_period) |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 753 | return; |
| 754 | local_irq_save(flags); |
| 755 | perf_disable(); |
| 756 | power_pmu_read(counter); |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 757 | left = counter->hw.sample_period; |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 758 | val = 0; |
| 759 | if (left < 0x80000000L) |
| 760 | val = 0x80000000L - left; |
| 761 | write_pmc(counter->hw.idx, val); |
| 762 | atomic64_set(&counter->hw.prev_count, val); |
| 763 | atomic64_set(&counter->hw.period_left, left); |
| 764 | perf_counter_update_userpage(counter); |
| 765 | perf_enable(); |
| 766 | local_irq_restore(flags); |
| 767 | } |
| 768 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 769 | struct pmu power_pmu = { |
| 770 | .enable = power_pmu_enable, |
| 771 | .disable = power_pmu_disable, |
| 772 | .read = power_pmu_read, |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 773 | .unthrottle = power_pmu_unthrottle, |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 774 | }; |
| 775 | |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 776 | /* |
| 777 | * Return 1 if we might be able to put counter on a limited PMC, |
| 778 | * or 0 if not. |
| 779 | * A counter can only go on a limited PMC if it counts something |
| 780 | * that a limited PMC can count, doesn't require interrupts, and |
| 781 | * doesn't exclude any processor mode. |
| 782 | */ |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 783 | static int can_go_on_limited_pmc(struct perf_counter *counter, u64 ev, |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 784 | unsigned int flags) |
| 785 | { |
| 786 | int n; |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 787 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 788 | |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 789 | if (counter->attr.exclude_user |
| 790 | || counter->attr.exclude_kernel |
| 791 | || counter->attr.exclude_hv |
| 792 | || counter->attr.sample_period) |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 793 | return 0; |
| 794 | |
| 795 | if (ppmu->limited_pmc_event(ev)) |
| 796 | return 1; |
| 797 | |
| 798 | /* |
| 799 | * The requested event isn't on a limited PMC already; |
| 800 | * see if any alternative code goes on a limited PMC. |
| 801 | */ |
| 802 | if (!ppmu->get_alternatives) |
| 803 | return 0; |
| 804 | |
| 805 | flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD; |
| 806 | n = ppmu->get_alternatives(ev, flags, alt); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 807 | |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 808 | return n > 0; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Find an alternative event that goes on a normal PMC, if possible, |
| 813 | * and return the event code, or 0 if there is no such alternative. |
| 814 | * (Note: event code 0 is "don't count" on all machines.) |
| 815 | */ |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 816 | static u64 normal_pmc_alternative(u64 ev, unsigned long flags) |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 817 | { |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 818 | u64 alt[MAX_EVENT_ALTERNATIVES]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 819 | int n; |
| 820 | |
| 821 | flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD); |
| 822 | n = ppmu->get_alternatives(ev, flags, alt); |
| 823 | if (!n) |
| 824 | return 0; |
| 825 | return alt[0]; |
| 826 | } |
| 827 | |
Paul Mackerras | 7595d63 | 2009-03-30 19:07:07 +0200 | [diff] [blame] | 828 | /* Number of perf_counters counting hardware events */ |
| 829 | static atomic_t num_counters; |
| 830 | /* Used to avoid races in calling reserve/release_pmc_hardware */ |
| 831 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 832 | |
| 833 | /* |
| 834 | * Release the PMU if this is the last perf_counter. |
| 835 | */ |
| 836 | static void hw_perf_counter_destroy(struct perf_counter *counter) |
| 837 | { |
| 838 | if (!atomic_add_unless(&num_counters, -1, 1)) { |
| 839 | mutex_lock(&pmc_reserve_mutex); |
| 840 | if (atomic_dec_return(&num_counters) == 0) |
| 841 | release_pmc_hardware(); |
| 842 | mutex_unlock(&pmc_reserve_mutex); |
| 843 | } |
| 844 | } |
| 845 | |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 846 | const struct pmu *hw_perf_counter_init(struct perf_counter *counter) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 847 | { |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 848 | u64 ev; |
| 849 | unsigned long flags; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 850 | struct perf_counter *ctrs[MAX_HWCOUNTERS]; |
Paul Mackerras | ef92321 | 2009-05-14 13:29:14 +1000 | [diff] [blame] | 851 | u64 events[MAX_HWCOUNTERS]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 852 | unsigned int cflags[MAX_HWCOUNTERS]; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 853 | int n; |
Paul Mackerras | 7595d63 | 2009-03-30 19:07:07 +0200 | [diff] [blame] | 854 | int err; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 855 | |
| 856 | if (!ppmu) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 857 | return ERR_PTR(-ENXIO); |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 858 | if (!perf_event_raw(&counter->attr)) { |
| 859 | ev = perf_event_id(&counter->attr); |
Paul Mackerras | 9aaa131 | 2009-03-21 15:31:47 +1100 | [diff] [blame] | 860 | if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 861 | return ERR_PTR(-EOPNOTSUPP); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 862 | ev = ppmu->generic_events[ev]; |
Paul Mackerras | 9aaa131 | 2009-03-21 15:31:47 +1100 | [diff] [blame] | 863 | } else { |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 864 | ev = perf_event_config(&counter->attr); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 865 | } |
| 866 | counter->hw.config_base = ev; |
| 867 | counter->hw.idx = 0; |
| 868 | |
| 869 | /* |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 870 | * If we are not running on a hypervisor, force the |
| 871 | * exclude_hv bit to 0 so that we don't care what |
Paul Mackerras | d095cd4 | 2009-02-23 23:01:28 +1100 | [diff] [blame] | 872 | * the user set it to. |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 873 | */ |
| 874 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 875 | counter->attr.exclude_hv = 0; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * If this is a per-task counter, then we can use |
| 879 | * PM_RUN_* events interchangeably with their non RUN_* |
| 880 | * equivalents, e.g. PM_RUN_CYC instead of PM_CYC. |
| 881 | * XXX we should check if the task is an idle task. |
| 882 | */ |
| 883 | flags = 0; |
| 884 | if (counter->ctx->task) |
| 885 | flags |= PPMU_ONLY_COUNT_RUN; |
| 886 | |
| 887 | /* |
| 888 | * If this machine has limited counters, check whether this |
| 889 | * event could go on a limited counter. |
| 890 | */ |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 891 | if (ppmu->flags & PPMU_LIMITED_PMC5_6) { |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 892 | if (can_go_on_limited_pmc(counter, ev, flags)) { |
| 893 | flags |= PPMU_LIMITED_PMC_OK; |
| 894 | } else if (ppmu->limited_pmc_event(ev)) { |
| 895 | /* |
| 896 | * The requested event is on a limited PMC, |
| 897 | * but we can't use a limited PMC; see if any |
| 898 | * alternative goes on a normal PMC. |
| 899 | */ |
| 900 | ev = normal_pmc_alternative(ev, flags); |
| 901 | if (!ev) |
| 902 | return ERR_PTR(-EINVAL); |
| 903 | } |
| 904 | } |
| 905 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 906 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 907 | * If this is in a group, check if it can go on with all the |
| 908 | * other hardware counters in the group. We assume the counter |
| 909 | * hasn't been linked into its leader's sibling list at this point. |
| 910 | */ |
| 911 | n = 0; |
| 912 | if (counter->group_leader != counter) { |
| 913 | n = collect_events(counter->group_leader, ppmu->n_counter - 1, |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 914 | ctrs, events, cflags); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 915 | if (n < 0) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 916 | return ERR_PTR(-EINVAL); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 917 | } |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 918 | events[n] = ev; |
Paul Mackerras | 8602859 | 2009-03-05 14:05:57 +1100 | [diff] [blame] | 919 | ctrs[n] = counter; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 920 | cflags[n] = flags; |
| 921 | if (check_excludes(ctrs, cflags, n, 1)) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 922 | return ERR_PTR(-EINVAL); |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 923 | if (power_check_constraints(events, cflags, n + 1)) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 924 | return ERR_PTR(-EINVAL); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 925 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 926 | counter->hw.config = events[n]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 927 | counter->hw.counter_base = cflags[n]; |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 928 | atomic64_set(&counter->hw.period_left, counter->hw.sample_period); |
Paul Mackerras | 7595d63 | 2009-03-30 19:07:07 +0200 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * See if we need to reserve the PMU. |
| 932 | * If no counters are currently in use, then we have to take a |
| 933 | * mutex to ensure that we don't race with another task doing |
| 934 | * reserve_pmc_hardware or release_pmc_hardware. |
| 935 | */ |
| 936 | err = 0; |
| 937 | if (!atomic_inc_not_zero(&num_counters)) { |
| 938 | mutex_lock(&pmc_reserve_mutex); |
| 939 | if (atomic_read(&num_counters) == 0 && |
| 940 | reserve_pmc_hardware(perf_counter_interrupt)) |
| 941 | err = -EBUSY; |
| 942 | else |
| 943 | atomic_inc(&num_counters); |
| 944 | mutex_unlock(&pmc_reserve_mutex); |
| 945 | } |
| 946 | counter->destroy = hw_perf_counter_destroy; |
| 947 | |
| 948 | if (err) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 949 | return ERR_PTR(err); |
Robert Richter | 4aeb0b4 | 2009-04-29 12:47:03 +0200 | [diff] [blame] | 950 | return &power_pmu; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 954 | * A counter has overflowed; update its count and record |
| 955 | * things if requested. Note that interrupts are hard-disabled |
| 956 | * here so there is no possibility of being interrupted. |
| 957 | */ |
| 958 | static void record_and_restart(struct perf_counter *counter, long val, |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 959 | struct pt_regs *regs, int nmi) |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 960 | { |
Peter Zijlstra | b23f332 | 2009-06-02 15:13:03 +0200 | [diff] [blame] | 961 | u64 period = counter->hw.sample_period; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 962 | s64 prev, delta, left; |
| 963 | int record = 0; |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 964 | u64 addr, mmcra, sdsync; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 965 | |
| 966 | /* we don't have to worry about interrupts here */ |
| 967 | prev = atomic64_read(&counter->hw.prev_count); |
| 968 | delta = (val - prev) & 0xfffffffful; |
| 969 | atomic64_add(delta, &counter->count); |
| 970 | |
| 971 | /* |
| 972 | * See if the total period for this counter has expired, |
| 973 | * and update for the next period. |
| 974 | */ |
| 975 | val = 0; |
| 976 | left = atomic64_read(&counter->hw.period_left) - delta; |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 977 | if (period) { |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 978 | if (left <= 0) { |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 979 | left += period; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 980 | if (left <= 0) |
Peter Zijlstra | 60db5e0 | 2009-05-15 15:19:28 +0200 | [diff] [blame] | 981 | left = period; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 982 | record = 1; |
| 983 | } |
| 984 | if (left < 0x80000000L) |
| 985 | val = 0x80000000L - left; |
| 986 | } |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 987 | |
| 988 | /* |
| 989 | * Finally record data if requested. |
| 990 | */ |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 991 | if (record) { |
| 992 | addr = 0; |
Peter Zijlstra | 0d48696 | 2009-06-02 19:22:16 +0200 | [diff] [blame^] | 993 | if (counter->attr.record_type & PERF_RECORD_ADDR) { |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 994 | /* |
| 995 | * The user wants a data address recorded. |
| 996 | * If we're not doing instruction sampling, |
| 997 | * give them the SDAR (sampled data address). |
| 998 | * If we are doing instruction sampling, then only |
| 999 | * give them the SDAR if it corresponds to the |
| 1000 | * instruction pointed to by SIAR; this is indicated |
| 1001 | * by the [POWER6_]MMCRA_SDSYNC bit in MMCRA. |
| 1002 | */ |
| 1003 | mmcra = regs->dsisr; |
| 1004 | sdsync = (ppmu->flags & PPMU_ALT_SIPR) ? |
| 1005 | POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC; |
| 1006 | if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync)) |
| 1007 | addr = mfspr(SPRN_SDAR); |
| 1008 | } |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 1009 | if (perf_counter_overflow(counter, nmi, regs, addr)) { |
| 1010 | /* |
| 1011 | * Interrupts are coming too fast - throttle them |
| 1012 | * by setting the counter to 0, so it will be |
| 1013 | * at least 2^30 cycles until the next interrupt |
| 1014 | * (assuming each counter counts at most 2 counts |
| 1015 | * per cycle). |
| 1016 | */ |
| 1017 | val = 0; |
| 1018 | left = ~0ULL >> 1; |
| 1019 | } |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 1020 | } |
Paul Mackerras | 8a7b8cb | 2009-05-26 16:27:59 +1000 | [diff] [blame] | 1021 | |
| 1022 | write_pmc(counter->hw.idx, val); |
| 1023 | atomic64_set(&counter->hw.prev_count, val); |
| 1024 | atomic64_set(&counter->hw.period_left, left); |
| 1025 | perf_counter_update_userpage(counter); |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Called from generic code to get the misc flags (i.e. processor mode) |
| 1030 | * for an event. |
| 1031 | */ |
| 1032 | unsigned long perf_misc_flags(struct pt_regs *regs) |
| 1033 | { |
| 1034 | unsigned long mmcra; |
| 1035 | |
| 1036 | if (TRAP(regs) != 0xf00) { |
| 1037 | /* not a PMU interrupt */ |
| 1038 | return user_mode(regs) ? PERF_EVENT_MISC_USER : |
| 1039 | PERF_EVENT_MISC_KERNEL; |
| 1040 | } |
| 1041 | |
| 1042 | mmcra = regs->dsisr; |
| 1043 | if (ppmu->flags & PPMU_ALT_SIPR) { |
| 1044 | if (mmcra & POWER6_MMCRA_SIHV) |
| 1045 | return PERF_EVENT_MISC_HYPERVISOR; |
| 1046 | return (mmcra & POWER6_MMCRA_SIPR) ? PERF_EVENT_MISC_USER : |
| 1047 | PERF_EVENT_MISC_KERNEL; |
| 1048 | } |
| 1049 | if (mmcra & MMCRA_SIHV) |
| 1050 | return PERF_EVENT_MISC_HYPERVISOR; |
| 1051 | return (mmcra & MMCRA_SIPR) ? PERF_EVENT_MISC_USER : |
| 1052 | PERF_EVENT_MISC_KERNEL; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Called from generic code to get the instruction pointer |
| 1057 | * for an event. |
| 1058 | */ |
| 1059 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
| 1060 | { |
| 1061 | unsigned long mmcra; |
| 1062 | unsigned long ip; |
| 1063 | unsigned long slot; |
| 1064 | |
| 1065 | if (TRAP(regs) != 0xf00) |
| 1066 | return regs->nip; /* not a PMU interrupt */ |
| 1067 | |
| 1068 | ip = mfspr(SPRN_SIAR); |
| 1069 | mmcra = regs->dsisr; |
| 1070 | if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) { |
| 1071 | slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT; |
| 1072 | if (slot > 1) |
| 1073 | ip += 4 * (slot - 1); |
| 1074 | } |
| 1075 | return ip; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Performance monitor interrupt stuff |
| 1080 | */ |
| 1081 | static void perf_counter_interrupt(struct pt_regs *regs) |
| 1082 | { |
| 1083 | int i; |
| 1084 | struct cpu_hw_counters *cpuhw = &__get_cpu_var(cpu_hw_counters); |
| 1085 | struct perf_counter *counter; |
| 1086 | long val; |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1087 | int found = 0; |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 1088 | int nmi; |
| 1089 | |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 1090 | if (cpuhw->n_limited) |
| 1091 | freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5), |
| 1092 | mfspr(SPRN_PMC6)); |
| 1093 | |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 1094 | /* |
Paul Mackerras | 0bbd0d4 | 2009-05-14 13:31:48 +1000 | [diff] [blame] | 1095 | * Overload regs->dsisr to store MMCRA so we only need to read it once. |
| 1096 | */ |
| 1097 | regs->dsisr = mfspr(SPRN_MMCRA); |
| 1098 | |
| 1099 | /* |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 1100 | * If interrupts were soft-disabled when this PMU interrupt |
| 1101 | * occurred, treat it as an NMI. |
| 1102 | */ |
| 1103 | nmi = !regs->softe; |
| 1104 | if (nmi) |
| 1105 | nmi_enter(); |
| 1106 | else |
| 1107 | irq_enter(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1108 | |
| 1109 | for (i = 0; i < cpuhw->n_counters; ++i) { |
| 1110 | counter = cpuhw->counter[i]; |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 1111 | if (is_limited_pmc(counter->hw.idx)) |
| 1112 | continue; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1113 | val = read_pmc(counter->hw.idx); |
| 1114 | if ((int)val < 0) { |
| 1115 | /* counter has overflowed */ |
| 1116 | found = 1; |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 1117 | record_and_restart(counter, val, regs, nmi); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * In case we didn't find and reset the counter that caused |
| 1123 | * the interrupt, scan all counters and reset any that are |
| 1124 | * negative, to avoid getting continual interrupts. |
| 1125 | * Any that we processed in the previous loop will not be negative. |
| 1126 | */ |
| 1127 | if (!found) { |
| 1128 | for (i = 0; i < ppmu->n_counter; ++i) { |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 1129 | if (is_limited_pmc(i + 1)) |
| 1130 | continue; |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1131 | val = read_pmc(i + 1); |
| 1132 | if ((int)val < 0) |
| 1133 | write_pmc(i + 1, 0); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Reset MMCR0 to its normal value. This will set PMXE and |
| 1139 | * clear FC (freeze counters) and PMAO (perf mon alert occurred) |
| 1140 | * and thus allow interrupts to occur again. |
| 1141 | * XXX might want to use MSR.PM to keep the counters frozen until |
| 1142 | * we get back out of this interrupt. |
| 1143 | */ |
Paul Mackerras | ab7ef2e | 2009-04-29 22:38:51 +1000 | [diff] [blame] | 1144 | write_mmcr0(cpuhw, cpuhw->mmcr[0]); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1145 | |
Paul Mackerras | ca8f2d7 | 2009-04-09 14:42:56 +1000 | [diff] [blame] | 1146 | if (nmi) |
| 1147 | nmi_exit(); |
| 1148 | else |
Paul Mackerras | db4fb5a | 2009-03-19 20:26:20 +0100 | [diff] [blame] | 1149 | irq_exit(); |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1150 | } |
| 1151 | |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 1152 | void hw_perf_counter_setup(int cpu) |
| 1153 | { |
| 1154 | struct cpu_hw_counters *cpuhw = &per_cpu(cpu_hw_counters, cpu); |
| 1155 | |
| 1156 | memset(cpuhw, 0, sizeof(*cpuhw)); |
| 1157 | cpuhw->mmcr[0] = MMCR0_FC; |
| 1158 | } |
| 1159 | |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 1160 | extern struct power_pmu power4_pmu; |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1161 | extern struct power_pmu ppc970_pmu; |
Paul Mackerras | 742bd95 | 2009-02-24 11:33:56 +1100 | [diff] [blame] | 1162 | extern struct power_pmu power5_pmu; |
Paul Mackerras | aabbaa6 | 2009-03-06 16:27:10 +1100 | [diff] [blame] | 1163 | extern struct power_pmu power5p_pmu; |
Paul Mackerras | f786283 | 2009-01-09 21:05:35 +1100 | [diff] [blame] | 1164 | extern struct power_pmu power6_pmu; |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1165 | |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1166 | static int init_perf_counters(void) |
| 1167 | { |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1168 | unsigned long pvr; |
| 1169 | |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1170 | /* XXX should get this from cputable */ |
| 1171 | pvr = mfspr(SPRN_PVR); |
| 1172 | switch (PVR_VER(pvr)) { |
Paul Mackerras | 880860e | 2009-03-06 16:30:52 +1100 | [diff] [blame] | 1173 | case PV_POWER4: |
| 1174 | case PV_POWER4p: |
| 1175 | ppmu = &power4_pmu; |
| 1176 | break; |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1177 | case PV_970: |
| 1178 | case PV_970FX: |
| 1179 | case PV_970MP: |
| 1180 | ppmu = &ppc970_pmu; |
| 1181 | break; |
Paul Mackerras | 742bd95 | 2009-02-24 11:33:56 +1100 | [diff] [blame] | 1182 | case PV_POWER5: |
| 1183 | ppmu = &power5_pmu; |
| 1184 | break; |
Paul Mackerras | aabbaa6 | 2009-03-06 16:27:10 +1100 | [diff] [blame] | 1185 | case PV_POWER5p: |
| 1186 | ppmu = &power5p_pmu; |
| 1187 | break; |
Paul Mackerras | f786283 | 2009-01-09 21:05:35 +1100 | [diff] [blame] | 1188 | case 0x3e: |
| 1189 | ppmu = &power6_pmu; |
| 1190 | break; |
Paul Mackerras | 16b0679 | 2009-01-10 16:34:07 +1100 | [diff] [blame] | 1191 | } |
Paul Mackerras | d095cd4 | 2009-02-23 23:01:28 +1100 | [diff] [blame] | 1192 | |
| 1193 | /* |
| 1194 | * Use FCHV to ignore kernel events if MSR.HV is set. |
| 1195 | */ |
| 1196 | if (mfmsr() & MSR_HV) |
| 1197 | freeze_counters_kernel = MMCR0_FCHV; |
| 1198 | |
Paul Mackerras | 4574910 | 2009-01-09 20:21:55 +1100 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | arch_initcall(init_perf_counters); |