Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branches 'hwbreak', 'perf/updates' and 'perf/system-pmus' into for-rmk

+650 -696
+74 -19
arch/arm/include/asm/pmu.h
··· 13 13 #define __ARM_PMU_H__ 14 14 15 15 #include <linux/interrupt.h> 16 + #include <linux/perf_event.h> 16 17 18 + /* 19 + * Types of PMUs that can be accessed directly and require mutual 20 + * exclusion between profiling tools. 21 + */ 17 22 enum arm_pmu_type { 18 23 ARM_PMU_DEVICE_CPU = 0, 19 24 ARM_NUM_PMU_DEVICES, ··· 42 37 * reserve_pmu() - reserve the hardware performance counters 43 38 * 44 39 * Reserve the hardware performance counters in the system for exclusive use. 45 - * The platform_device for the system is returned on success, ERR_PTR() 46 - * encoded error on failure. 40 + * Returns 0 on success or -EBUSY if the lock is already held. 47 41 */ 48 - extern struct platform_device * 42 + extern int 49 43 reserve_pmu(enum arm_pmu_type type); 50 44 51 45 /** 52 46 * release_pmu() - Relinquish control of the performance counters 53 47 * 54 48 * Release the performance counters and allow someone else to use them. 55 - * Callers must have disabled the counters and released IRQs before calling 56 - * this. The platform_device returned from reserve_pmu() must be passed as 57 - * a cookie. 58 49 */ 59 - extern int 50 + extern void 60 51 release_pmu(enum arm_pmu_type type); 61 52 62 53 /** ··· 69 68 70 69 #include <linux/err.h> 71 70 72 - static inline struct platform_device * 71 + static inline int 73 72 reserve_pmu(enum arm_pmu_type type) 74 73 { 75 - return ERR_PTR(-ENODEV); 76 - } 77 - 78 - static inline int 79 - release_pmu(enum arm_pmu_type type) 80 - { 81 74 return -ENODEV; 82 75 } 83 76 84 - static inline int 85 - init_pmu(enum arm_pmu_type type) 86 - { 87 - return -ENODEV; 88 - } 77 + static inline void 78 + release_pmu(enum arm_pmu_type type) { } 89 79 90 80 #endif /* CONFIG_CPU_HAS_PMU */ 81 + 82 + #ifdef CONFIG_HW_PERF_EVENTS 83 + 84 + /* The events for a given PMU register set. */ 85 + struct pmu_hw_events { 86 + /* 87 + * The events that are active on the PMU for the given index. 88 + */ 89 + struct perf_event **events; 90 + 91 + /* 92 + * A 1 bit for an index indicates that the counter is being used for 93 + * an event. A 0 means that the counter can be used. 94 + */ 95 + unsigned long *used_mask; 96 + 97 + /* 98 + * Hardware lock to serialize accesses to PMU registers. Needed for the 99 + * read/modify/write sequences. 100 + */ 101 + raw_spinlock_t pmu_lock; 102 + }; 103 + 104 + struct arm_pmu { 105 + struct pmu pmu; 106 + enum arm_perf_pmu_ids id; 107 + enum arm_pmu_type type; 108 + cpumask_t active_irqs; 109 + const char *name; 110 + irqreturn_t (*handle_irq)(int irq_num, void *dev); 111 + void (*enable)(struct hw_perf_event *evt, int idx); 112 + void (*disable)(struct hw_perf_event *evt, int idx); 113 + int (*get_event_idx)(struct pmu_hw_events *hw_events, 114 + struct hw_perf_event *hwc); 115 + int (*set_event_filter)(struct hw_perf_event *evt, 116 + struct perf_event_attr *attr); 117 + u32 (*read_counter)(int idx); 118 + void (*write_counter)(int idx, u32 val); 119 + void (*start)(void); 120 + void (*stop)(void); 121 + void (*reset)(void *); 122 + int (*map_event)(struct perf_event *event); 123 + int num_events; 124 + atomic_t active_events; 125 + struct mutex reserve_mutex; 126 + u64 max_period; 127 + struct platform_device *plat_device; 128 + struct pmu_hw_events *(*get_hw_events)(void); 129 + }; 130 + 131 + #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) 132 + 133 + int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type); 134 + 135 + u64 armpmu_event_update(struct perf_event *event, 136 + struct hw_perf_event *hwc, 137 + int idx, int overflow); 138 + 139 + int armpmu_event_set_period(struct perf_event *event, 140 + struct hw_perf_event *hwc, 141 + int idx); 142 + 143 + #endif /* CONFIG_HW_PERF_EVENTS */ 91 144 92 145 #endif /* __ARM_PMU_H__ */
+259 -226
arch/arm/kernel/perf_event.c
··· 12 12 */ 13 13 #define pr_fmt(fmt) "hw perfevents: " fmt 14 14 15 + #include <linux/bitmap.h> 15 16 #include <linux/interrupt.h> 16 17 #include <linux/kernel.h> 17 18 #include <linux/module.h> ··· 27 26 #include <asm/pmu.h> 28 27 #include <asm/stacktrace.h> 29 28 30 - static struct platform_device *pmu_device; 31 - 32 29 /* 33 - * Hardware lock to serialize accesses to PMU registers. Needed for the 34 - * read/modify/write sequences. 35 - */ 36 - static DEFINE_RAW_SPINLOCK(pmu_lock); 37 - 38 - /* 39 - * ARMv6 supports a maximum of 3 events, starting from index 1. If we add 30 + * ARMv6 supports a maximum of 3 events, starting from index 0. If we add 40 31 * another platform that supports more, we need to increase this to be the 41 32 * largest of all platforms. 42 33 * ··· 36 43 * cycle counter CCNT + 31 events counters CNT0..30. 37 44 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters. 38 45 */ 39 - #define ARMPMU_MAX_HWEVENTS 33 46 + #define ARMPMU_MAX_HWEVENTS 32 40 47 41 - /* The events for a given CPU. */ 42 - struct cpu_hw_events { 43 - /* 44 - * The events that are active on the CPU for the given index. Index 0 45 - * is reserved. 46 - */ 47 - struct perf_event *events[ARMPMU_MAX_HWEVENTS]; 48 + static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events); 49 + static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask); 50 + static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events); 48 51 49 - /* 50 - * A 1 bit for an index indicates that the counter is being used for 51 - * an event. A 0 means that the counter can be used. 52 - */ 53 - unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; 54 - 55 - /* 56 - * A 1 bit for an index indicates that the counter is actively being 57 - * used. 58 - */ 59 - unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)]; 60 - }; 61 - static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); 62 - 63 - struct arm_pmu { 64 - enum arm_perf_pmu_ids id; 65 - const char *name; 66 - irqreturn_t (*handle_irq)(int irq_num, void *dev); 67 - void (*enable)(struct hw_perf_event *evt, int idx); 68 - void (*disable)(struct hw_perf_event *evt, int idx); 69 - int (*get_event_idx)(struct cpu_hw_events *cpuc, 70 - struct hw_perf_event *hwc); 71 - u32 (*read_counter)(int idx); 72 - void (*write_counter)(int idx, u32 val); 73 - void (*start)(void); 74 - void (*stop)(void); 75 - void (*reset)(void *); 76 - const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX] 77 - [PERF_COUNT_HW_CACHE_OP_MAX] 78 - [PERF_COUNT_HW_CACHE_RESULT_MAX]; 79 - const unsigned (*event_map)[PERF_COUNT_HW_MAX]; 80 - u32 raw_event_mask; 81 - int num_events; 82 - u64 max_period; 83 - }; 52 + #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) 84 53 85 54 /* Set at runtime when we know what CPU type we are. */ 86 - static const struct arm_pmu *armpmu; 55 + static struct arm_pmu *cpu_pmu; 87 56 88 57 enum arm_perf_pmu_ids 89 58 armpmu_get_pmu_id(void) 90 59 { 91 60 int id = -ENODEV; 92 61 93 - if (armpmu != NULL) 94 - id = armpmu->id; 62 + if (cpu_pmu != NULL) 63 + id = cpu_pmu->id; 95 64 96 65 return id; 97 66 } ··· 64 109 { 65 110 int max_events = 0; 66 111 67 - if (armpmu != NULL) 68 - max_events = armpmu->num_events; 112 + if (cpu_pmu != NULL) 113 + max_events = cpu_pmu->num_events; 69 114 70 115 return max_events; 71 116 } ··· 85 130 #define CACHE_OP_UNSUPPORTED 0xFFFF 86 131 87 132 static int 88 - armpmu_map_cache_event(u64 config) 133 + armpmu_map_cache_event(const unsigned (*cache_map) 134 + [PERF_COUNT_HW_CACHE_MAX] 135 + [PERF_COUNT_HW_CACHE_OP_MAX] 136 + [PERF_COUNT_HW_CACHE_RESULT_MAX], 137 + u64 config) 89 138 { 90 139 unsigned int cache_type, cache_op, cache_result, ret; 91 140 ··· 105 146 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) 106 147 return -EINVAL; 107 148 108 - ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result]; 149 + ret = (int)(*cache_map)[cache_type][cache_op][cache_result]; 109 150 110 151 if (ret == CACHE_OP_UNSUPPORTED) 111 152 return -ENOENT; ··· 114 155 } 115 156 116 157 static int 117 - armpmu_map_event(u64 config) 158 + armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config) 118 159 { 119 - int mapping = (*armpmu->event_map)[config]; 120 - return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping; 160 + int mapping = (*event_map)[config]; 161 + return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping; 121 162 } 122 163 123 164 static int 124 - armpmu_map_raw_event(u64 config) 165 + armpmu_map_raw_event(u32 raw_event_mask, u64 config) 125 166 { 126 - return (int)(config & armpmu->raw_event_mask); 167 + return (int)(config & raw_event_mask); 127 168 } 128 169 129 - static int 170 + static int map_cpu_event(struct perf_event *event, 171 + const unsigned (*event_map)[PERF_COUNT_HW_MAX], 172 + const unsigned (*cache_map) 173 + [PERF_COUNT_HW_CACHE_MAX] 174 + [PERF_COUNT_HW_CACHE_OP_MAX] 175 + [PERF_COUNT_HW_CACHE_RESULT_MAX], 176 + u32 raw_event_mask) 177 + { 178 + u64 config = event->attr.config; 179 + 180 + switch (event->attr.type) { 181 + case PERF_TYPE_HARDWARE: 182 + return armpmu_map_event(event_map, config); 183 + case PERF_TYPE_HW_CACHE: 184 + return armpmu_map_cache_event(cache_map, config); 185 + case PERF_TYPE_RAW: 186 + return armpmu_map_raw_event(raw_event_mask, config); 187 + } 188 + 189 + return -ENOENT; 190 + } 191 + 192 + int 130 193 armpmu_event_set_period(struct perf_event *event, 131 194 struct hw_perf_event *hwc, 132 195 int idx) 133 196 { 197 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 134 198 s64 left = local64_read(&hwc->period_left); 135 199 s64 period = hwc->sample_period; 136 200 int ret = 0; ··· 184 202 return ret; 185 203 } 186 204 187 - static u64 205 + u64 188 206 armpmu_event_update(struct perf_event *event, 189 207 struct hw_perf_event *hwc, 190 208 int idx, int overflow) 191 209 { 210 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 192 211 u64 delta, prev_raw_count, new_raw_count; 193 212 194 213 again: ··· 229 246 static void 230 247 armpmu_stop(struct perf_event *event, int flags) 231 248 { 249 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 232 250 struct hw_perf_event *hwc = &event->hw; 233 - 234 - if (!armpmu) 235 - return; 236 251 237 252 /* 238 253 * ARM pmu always has to update the counter, so ignore ··· 247 266 static void 248 267 armpmu_start(struct perf_event *event, int flags) 249 268 { 269 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 250 270 struct hw_perf_event *hwc = &event->hw; 251 - 252 - if (!armpmu) 253 - return; 254 271 255 272 /* 256 273 * ARM pmu always has to reprogram the period, so ignore ··· 272 293 static void 273 294 armpmu_del(struct perf_event *event, int flags) 274 295 { 275 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 296 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 297 + struct pmu_hw_events *hw_events = armpmu->get_hw_events(); 276 298 struct hw_perf_event *hwc = &event->hw; 277 299 int idx = hwc->idx; 278 300 279 301 WARN_ON(idx < 0); 280 302 281 - clear_bit(idx, cpuc->active_mask); 282 303 armpmu_stop(event, PERF_EF_UPDATE); 283 - cpuc->events[idx] = NULL; 284 - clear_bit(idx, cpuc->used_mask); 304 + hw_events->events[idx] = NULL; 305 + clear_bit(idx, hw_events->used_mask); 285 306 286 307 perf_event_update_userpage(event); 287 308 } ··· 289 310 static int 290 311 armpmu_add(struct perf_event *event, int flags) 291 312 { 292 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 313 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 314 + struct pmu_hw_events *hw_events = armpmu->get_hw_events(); 293 315 struct hw_perf_event *hwc = &event->hw; 294 316 int idx; 295 317 int err = 0; ··· 298 318 perf_pmu_disable(event->pmu); 299 319 300 320 /* If we don't have a space for the counter then finish early. */ 301 - idx = armpmu->get_event_idx(cpuc, hwc); 321 + idx = armpmu->get_event_idx(hw_events, hwc); 302 322 if (idx < 0) { 303 323 err = idx; 304 324 goto out; ··· 310 330 */ 311 331 event->hw.idx = idx; 312 332 armpmu->disable(hwc, idx); 313 - cpuc->events[idx] = event; 314 - set_bit(idx, cpuc->active_mask); 333 + hw_events->events[idx] = event; 315 334 316 335 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; 317 336 if (flags & PERF_EF_START) ··· 324 345 return err; 325 346 } 326 347 327 - static struct pmu pmu; 328 - 329 348 static int 330 - validate_event(struct cpu_hw_events *cpuc, 349 + validate_event(struct pmu_hw_events *hw_events, 331 350 struct perf_event *event) 332 351 { 352 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 333 353 struct hw_perf_event fake_event = event->hw; 354 + struct pmu *leader_pmu = event->group_leader->pmu; 334 355 335 - if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF) 356 + if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF) 336 357 return 1; 337 358 338 - return armpmu->get_event_idx(cpuc, &fake_event) >= 0; 359 + return armpmu->get_event_idx(hw_events, &fake_event) >= 0; 339 360 } 340 361 341 362 static int 342 363 validate_group(struct perf_event *event) 343 364 { 344 365 struct perf_event *sibling, *leader = event->group_leader; 345 - struct cpu_hw_events fake_pmu; 366 + struct pmu_hw_events fake_pmu; 346 367 347 368 memset(&fake_pmu, 0, sizeof(fake_pmu)); 348 369 ··· 362 383 363 384 static irqreturn_t armpmu_platform_irq(int irq, void *dev) 364 385 { 365 - struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev); 386 + struct arm_pmu *armpmu = (struct arm_pmu *) dev; 387 + struct platform_device *plat_device = armpmu->plat_device; 388 + struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev); 366 389 367 390 return plat->handle_irq(irq, dev, armpmu->handle_irq); 368 391 } 369 392 393 + static void 394 + armpmu_release_hardware(struct arm_pmu *armpmu) 395 + { 396 + int i, irq, irqs; 397 + struct platform_device *pmu_device = armpmu->plat_device; 398 + 399 + irqs = min(pmu_device->num_resources, num_possible_cpus()); 400 + 401 + for (i = 0; i < irqs; ++i) { 402 + if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs)) 403 + continue; 404 + irq = platform_get_irq(pmu_device, i); 405 + if (irq >= 0) 406 + free_irq(irq, armpmu); 407 + } 408 + 409 + release_pmu(armpmu->type); 410 + } 411 + 370 412 static int 371 - armpmu_reserve_hardware(void) 413 + armpmu_reserve_hardware(struct arm_pmu *armpmu) 372 414 { 373 415 struct arm_pmu_platdata *plat; 374 416 irq_handler_t handle_irq; 375 - int i, err = -ENODEV, irq; 417 + int i, err, irq, irqs; 418 + struct platform_device *pmu_device = armpmu->plat_device; 376 419 377 - pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU); 378 - if (IS_ERR(pmu_device)) { 420 + err = reserve_pmu(armpmu->type); 421 + if (err) { 379 422 pr_warning("unable to reserve pmu\n"); 380 - return PTR_ERR(pmu_device); 423 + return err; 381 424 } 382 - 383 - init_pmu(ARM_PMU_DEVICE_CPU); 384 425 385 426 plat = dev_get_platdata(&pmu_device->dev); 386 427 if (plat && plat->handle_irq) ··· 408 409 else 409 410 handle_irq = armpmu->handle_irq; 410 411 411 - if (pmu_device->num_resources < 1) { 412 + irqs = min(pmu_device->num_resources, num_possible_cpus()); 413 + if (irqs < 1) { 412 414 pr_err("no irqs for PMUs defined\n"); 413 415 return -ENODEV; 414 416 } 415 417 416 - for (i = 0; i < pmu_device->num_resources; ++i) { 418 + for (i = 0; i < irqs; ++i) { 419 + err = 0; 417 420 irq = platform_get_irq(pmu_device, i); 418 421 if (irq < 0) 419 422 continue; 420 423 424 + /* 425 + * If we have a single PMU interrupt that we can't shift, 426 + * assume that we're running on a uniprocessor machine and 427 + * continue. Otherwise, continue without this interrupt. 428 + */ 429 + if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) { 430 + pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", 431 + irq, i); 432 + continue; 433 + } 434 + 421 435 err = request_irq(irq, handle_irq, 422 436 IRQF_DISABLED | IRQF_NOBALANCING, 423 - "armpmu", NULL); 437 + "arm-pmu", armpmu); 424 438 if (err) { 425 - pr_warning("unable to request IRQ%d for ARM perf " 426 - "counters\n", irq); 427 - break; 439 + pr_err("unable to request IRQ%d for ARM PMU counters\n", 440 + irq); 441 + armpmu_release_hardware(armpmu); 442 + return err; 428 443 } 444 + 445 + cpumask_set_cpu(i, &armpmu->active_irqs); 429 446 } 430 447 431 - if (err) { 432 - for (i = i - 1; i >= 0; --i) { 433 - irq = platform_get_irq(pmu_device, i); 434 - if (irq >= 0) 435 - free_irq(irq, NULL); 436 - } 437 - release_pmu(ARM_PMU_DEVICE_CPU); 438 - pmu_device = NULL; 439 - } 440 - 441 - return err; 448 + return 0; 442 449 } 443 - 444 - static void 445 - armpmu_release_hardware(void) 446 - { 447 - int i, irq; 448 - 449 - for (i = pmu_device->num_resources - 1; i >= 0; --i) { 450 - irq = platform_get_irq(pmu_device, i); 451 - if (irq >= 0) 452 - free_irq(irq, NULL); 453 - } 454 - armpmu->stop(); 455 - 456 - release_pmu(ARM_PMU_DEVICE_CPU); 457 - pmu_device = NULL; 458 - } 459 - 460 - static atomic_t active_events = ATOMIC_INIT(0); 461 - static DEFINE_MUTEX(pmu_reserve_mutex); 462 450 463 451 static void 464 452 hw_perf_event_destroy(struct perf_event *event) 465 453 { 466 - if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) { 467 - armpmu_release_hardware(); 468 - mutex_unlock(&pmu_reserve_mutex); 454 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 455 + atomic_t *active_events = &armpmu->active_events; 456 + struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex; 457 + 458 + if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) { 459 + armpmu_release_hardware(armpmu); 460 + mutex_unlock(pmu_reserve_mutex); 469 461 } 462 + } 463 + 464 + static int 465 + event_requires_mode_exclusion(struct perf_event_attr *attr) 466 + { 467 + return attr->exclude_idle || attr->exclude_user || 468 + attr->exclude_kernel || attr->exclude_hv; 470 469 } 471 470 472 471 static int 473 472 __hw_perf_event_init(struct perf_event *event) 474 473 { 474 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 475 475 struct hw_perf_event *hwc = &event->hw; 476 476 int mapping, err; 477 477 478 - /* Decode the generic type into an ARM event identifier. */ 479 - if (PERF_TYPE_HARDWARE == event->attr.type) { 480 - mapping = armpmu_map_event(event->attr.config); 481 - } else if (PERF_TYPE_HW_CACHE == event->attr.type) { 482 - mapping = armpmu_map_cache_event(event->attr.config); 483 - } else if (PERF_TYPE_RAW == event->attr.type) { 484 - mapping = armpmu_map_raw_event(event->attr.config); 485 - } else { 486 - pr_debug("event type %x not supported\n", event->attr.type); 487 - return -EOPNOTSUPP; 488 - } 478 + mapping = armpmu->map_event(event); 489 479 490 480 if (mapping < 0) { 491 481 pr_debug("event %x:%llx not supported\n", event->attr.type, ··· 483 495 } 484 496 485 497 /* 486 - * Check whether we need to exclude the counter from certain modes. 487 - * The ARM performance counters are on all of the time so if someone 488 - * has asked us for some excludes then we have to fail. 498 + * We don't assign an index until we actually place the event onto 499 + * hardware. Use -1 to signify that we haven't decided where to put it 500 + * yet. For SMP systems, each core has it's own PMU so we can't do any 501 + * clever allocation or constraints checking at this point. 489 502 */ 490 - if (event->attr.exclude_kernel || event->attr.exclude_user || 491 - event->attr.exclude_hv || event->attr.exclude_idle) { 503 + hwc->idx = -1; 504 + hwc->config_base = 0; 505 + hwc->config = 0; 506 + hwc->event_base = 0; 507 + 508 + /* 509 + * Check whether we need to exclude the counter from certain modes. 510 + */ 511 + if ((!armpmu->set_event_filter || 512 + armpmu->set_event_filter(hwc, &event->attr)) && 513 + event_requires_mode_exclusion(&event->attr)) { 492 514 pr_debug("ARM performance counters do not support " 493 515 "mode exclusion\n"); 494 516 return -EPERM; 495 517 } 496 518 497 519 /* 498 - * We don't assign an index until we actually place the event onto 499 - * hardware. Use -1 to signify that we haven't decided where to put it 500 - * yet. For SMP systems, each core has it's own PMU so we can't do any 501 - * clever allocation or constraints checking at this point. 520 + * Store the event encoding into the config_base field. 502 521 */ 503 - hwc->idx = -1; 504 - 505 - /* 506 - * Store the event encoding into the config_base field. config and 507 - * event_base are unused as the only 2 things we need to know are 508 - * the event mapping and the counter to use. The counter to use is 509 - * also the indx and the config_base is the event type. 510 - */ 511 - hwc->config_base = (unsigned long)mapping; 512 - hwc->config = 0; 513 - hwc->event_base = 0; 522 + hwc->config_base |= (unsigned long)mapping; 514 523 515 524 if (!hwc->sample_period) { 516 525 hwc->sample_period = armpmu->max_period; ··· 527 542 528 543 static int armpmu_event_init(struct perf_event *event) 529 544 { 545 + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); 530 546 int err = 0; 547 + atomic_t *active_events = &armpmu->active_events; 531 548 532 - switch (event->attr.type) { 533 - case PERF_TYPE_RAW: 534 - case PERF_TYPE_HARDWARE: 535 - case PERF_TYPE_HW_CACHE: 536 - break; 537 - 538 - default: 549 + if (armpmu->map_event(event) == -ENOENT) 539 550 return -ENOENT; 540 - } 541 - 542 - if (!armpmu) 543 - return -ENODEV; 544 551 545 552 event->destroy = hw_perf_event_destroy; 546 553 547 - if (!atomic_inc_not_zero(&active_events)) { 548 - mutex_lock(&pmu_reserve_mutex); 549 - if (atomic_read(&active_events) == 0) { 550 - err = armpmu_reserve_hardware(); 551 - } 554 + if (!atomic_inc_not_zero(active_events)) { 555 + mutex_lock(&armpmu->reserve_mutex); 556 + if (atomic_read(active_events) == 0) 557 + err = armpmu_reserve_hardware(armpmu); 552 558 553 559 if (!err) 554 - atomic_inc(&active_events); 555 - mutex_unlock(&pmu_reserve_mutex); 560 + atomic_inc(active_events); 561 + mutex_unlock(&armpmu->reserve_mutex); 556 562 } 557 563 558 564 if (err) ··· 558 582 559 583 static void armpmu_enable(struct pmu *pmu) 560 584 { 561 - /* Enable all of the perf events on hardware. */ 562 - int idx, enabled = 0; 563 - struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); 564 - 565 - if (!armpmu) 566 - return; 567 - 568 - for (idx = 0; idx <= armpmu->num_events; ++idx) { 569 - struct perf_event *event = cpuc->events[idx]; 570 - 571 - if (!event) 572 - continue; 573 - 574 - armpmu->enable(&event->hw, idx); 575 - enabled = 1; 576 - } 585 + struct arm_pmu *armpmu = to_arm_pmu(pmu); 586 + struct pmu_hw_events *hw_events = armpmu->get_hw_events(); 587 + int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events); 577 588 578 589 if (enabled) 579 590 armpmu->start(); ··· 568 605 569 606 static void armpmu_disable(struct pmu *pmu) 570 607 { 571 - if (armpmu) 572 - armpmu->stop(); 608 + struct arm_pmu *armpmu = to_arm_pmu(pmu); 609 + armpmu->stop(); 573 610 } 574 611 575 - static struct pmu pmu = { 576 - .pmu_enable = armpmu_enable, 577 - .pmu_disable = armpmu_disable, 578 - .event_init = armpmu_event_init, 579 - .add = armpmu_add, 580 - .del = armpmu_del, 581 - .start = armpmu_start, 582 - .stop = armpmu_stop, 583 - .read = armpmu_read, 584 - }; 612 + static void __init armpmu_init(struct arm_pmu *armpmu) 613 + { 614 + atomic_set(&armpmu->active_events, 0); 615 + mutex_init(&armpmu->reserve_mutex); 616 + 617 + armpmu->pmu = (struct pmu) { 618 + .pmu_enable = armpmu_enable, 619 + .pmu_disable = armpmu_disable, 620 + .event_init = armpmu_event_init, 621 + .add = armpmu_add, 622 + .del = armpmu_del, 623 + .start = armpmu_start, 624 + .stop = armpmu_stop, 625 + .read = armpmu_read, 626 + }; 627 + } 628 + 629 + int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type) 630 + { 631 + armpmu_init(armpmu); 632 + return perf_pmu_register(&armpmu->pmu, name, type); 633 + } 585 634 586 635 /* Include the PMU-specific implementations. */ 587 636 #include "perf_event_xscale.c" ··· 605 630 * This requires SMP to be available, so exists as a separate initcall. 606 631 */ 607 632 static int __init 608 - armpmu_reset(void) 633 + cpu_pmu_reset(void) 609 634 { 610 - if (armpmu && armpmu->reset) 611 - return on_each_cpu(armpmu->reset, NULL, 1); 635 + if (cpu_pmu && cpu_pmu->reset) 636 + return on_each_cpu(cpu_pmu->reset, NULL, 1); 612 637 return 0; 613 638 } 614 - arch_initcall(armpmu_reset); 639 + arch_initcall(cpu_pmu_reset); 615 640 641 + /* 642 + * PMU platform driver and devicetree bindings. 643 + */ 644 + static struct of_device_id armpmu_of_device_ids[] = { 645 + {.compatible = "arm,cortex-a9-pmu"}, 646 + {.compatible = "arm,cortex-a8-pmu"}, 647 + {.compatible = "arm,arm1136-pmu"}, 648 + {.compatible = "arm,arm1176-pmu"}, 649 + {}, 650 + }; 651 + 652 + static struct platform_device_id armpmu_plat_device_ids[] = { 653 + {.name = "arm-pmu"}, 654 + {}, 655 + }; 656 + 657 + static int __devinit armpmu_device_probe(struct platform_device *pdev) 658 + { 659 + cpu_pmu->plat_device = pdev; 660 + return 0; 661 + } 662 + 663 + static struct platform_driver armpmu_driver = { 664 + .driver = { 665 + .name = "arm-pmu", 666 + .of_match_table = armpmu_of_device_ids, 667 + }, 668 + .probe = armpmu_device_probe, 669 + .id_table = armpmu_plat_device_ids, 670 + }; 671 + 672 + static int __init register_pmu_driver(void) 673 + { 674 + return platform_driver_register(&armpmu_driver); 675 + } 676 + device_initcall(register_pmu_driver); 677 + 678 + static struct pmu_hw_events *armpmu_get_cpu_events(void) 679 + { 680 + return &__get_cpu_var(cpu_hw_events); 681 + } 682 + 683 + static void __init cpu_pmu_init(struct arm_pmu *armpmu) 684 + { 685 + int cpu; 686 + for_each_possible_cpu(cpu) { 687 + struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu); 688 + events->events = per_cpu(hw_events, cpu); 689 + events->used_mask = per_cpu(used_mask, cpu); 690 + raw_spin_lock_init(&events->pmu_lock); 691 + } 692 + armpmu->get_hw_events = armpmu_get_cpu_events; 693 + armpmu->type = ARM_PMU_DEVICE_CPU; 694 + } 695 + 696 + /* 697 + * CPU PMU identification and registration. 698 + */ 616 699 static int __init 617 700 init_hw_perf_events(void) 618 701 { ··· 684 651 case 0xB360: /* ARM1136 */ 685 652 case 0xB560: /* ARM1156 */ 686 653 case 0xB760: /* ARM1176 */ 687 - armpmu = armv6pmu_init(); 654 + cpu_pmu = armv6pmu_init(); 688 655 break; 689 656 case 0xB020: /* ARM11mpcore */ 690 - armpmu = armv6mpcore_pmu_init(); 657 + cpu_pmu = armv6mpcore_pmu_init(); 691 658 break; 692 659 case 0xC080: /* Cortex-A8 */ 693 - armpmu = armv7_a8_pmu_init(); 660 + cpu_pmu = armv7_a8_pmu_init(); 694 661 break; 695 662 case 0xC090: /* Cortex-A9 */ 696 - armpmu = armv7_a9_pmu_init(); 663 + cpu_pmu = armv7_a9_pmu_init(); 697 664 break; 698 665 case 0xC050: /* Cortex-A5 */ 699 - armpmu = armv7_a5_pmu_init(); 666 + cpu_pmu = armv7_a5_pmu_init(); 700 667 break; 701 668 case 0xC0F0: /* Cortex-A15 */ 702 - armpmu = armv7_a15_pmu_init(); 669 + cpu_pmu = armv7_a15_pmu_init(); 703 670 break; 704 671 } 705 672 /* Intel CPUs [xscale]. */ ··· 707 674 part_number = (cpuid >> 13) & 0x7; 708 675 switch (part_number) { 709 676 case 1: 710 - armpmu = xscale1pmu_init(); 677 + cpu_pmu = xscale1pmu_init(); 711 678 break; 712 679 case 2: 713 - armpmu = xscale2pmu_init(); 680 + cpu_pmu = xscale2pmu_init(); 714 681 break; 715 682 } 716 683 } 717 684 718 - if (armpmu) { 685 + if (cpu_pmu) { 719 686 pr_info("enabled with %s PMU driver, %d counters available\n", 720 - armpmu->name, armpmu->num_events); 687 + cpu_pmu->name, cpu_pmu->num_events); 688 + cpu_pmu_init(cpu_pmu); 689 + armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW); 721 690 } else { 722 691 pr_info("no hardware support available\n"); 723 692 } 724 - 725 - perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); 726 693 727 694 return 0; 728 695 }
+59 -28
arch/arm/kernel/perf_event_v6.c
··· 54 54 }; 55 55 56 56 enum armv6_counters { 57 - ARMV6_CYCLE_COUNTER = 1, 57 + ARMV6_CYCLE_COUNTER = 0, 58 58 ARMV6_COUNTER0, 59 59 ARMV6_COUNTER1, 60 60 }; ··· 433 433 int idx) 434 434 { 435 435 unsigned long val, mask, evt, flags; 436 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 436 437 437 438 if (ARMV6_CYCLE_COUNTER == idx) { 438 439 mask = 0; ··· 455 454 * Mask out the current event and set the counter to count the event 456 455 * that we're interested in. 457 456 */ 458 - raw_spin_lock_irqsave(&pmu_lock, flags); 457 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 459 458 val = armv6_pmcr_read(); 460 459 val &= ~mask; 461 460 val |= evt; 462 461 armv6_pmcr_write(val); 463 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 462 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 463 + } 464 + 465 + static int counter_is_active(unsigned long pmcr, int idx) 466 + { 467 + unsigned long mask = 0; 468 + if (idx == ARMV6_CYCLE_COUNTER) 469 + mask = ARMV6_PMCR_CCOUNT_IEN; 470 + else if (idx == ARMV6_COUNTER0) 471 + mask = ARMV6_PMCR_COUNT0_IEN; 472 + else if (idx == ARMV6_COUNTER1) 473 + mask = ARMV6_PMCR_COUNT1_IEN; 474 + 475 + if (mask) 476 + return pmcr & mask; 477 + 478 + WARN_ONCE(1, "invalid counter number (%d)\n", idx); 479 + return 0; 464 480 } 465 481 466 482 static irqreturn_t ··· 486 468 { 487 469 unsigned long pmcr = armv6_pmcr_read(); 488 470 struct perf_sample_data data; 489 - struct cpu_hw_events *cpuc; 471 + struct pmu_hw_events *cpuc; 490 472 struct pt_regs *regs; 491 473 int idx; 492 474 ··· 505 487 perf_sample_data_init(&data, 0); 506 488 507 489 cpuc = &__get_cpu_var(cpu_hw_events); 508 - for (idx = 0; idx <= armpmu->num_events; ++idx) { 490 + for (idx = 0; idx < cpu_pmu->num_events; ++idx) { 509 491 struct perf_event *event = cpuc->events[idx]; 510 492 struct hw_perf_event *hwc; 511 493 512 - if (!test_bit(idx, cpuc->active_mask)) 494 + if (!counter_is_active(pmcr, idx)) 513 495 continue; 514 496 515 497 /* ··· 526 508 continue; 527 509 528 510 if (perf_event_overflow(event, &data, regs)) 529 - armpmu->disable(hwc, idx); 511 + cpu_pmu->disable(hwc, idx); 530 512 } 531 513 532 514 /* ··· 545 527 armv6pmu_start(void) 546 528 { 547 529 unsigned long flags, val; 530 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 548 531 549 - raw_spin_lock_irqsave(&pmu_lock, flags); 532 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 550 533 val = armv6_pmcr_read(); 551 534 val |= ARMV6_PMCR_ENABLE; 552 535 armv6_pmcr_write(val); 553 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 536 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 554 537 } 555 538 556 539 static void 557 540 armv6pmu_stop(void) 558 541 { 559 542 unsigned long flags, val; 543 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 560 544 561 - raw_spin_lock_irqsave(&pmu_lock, flags); 545 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 562 546 val = armv6_pmcr_read(); 563 547 val &= ~ARMV6_PMCR_ENABLE; 564 548 armv6_pmcr_write(val); 565 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 549 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 566 550 } 567 551 568 552 static int 569 - armv6pmu_get_event_idx(struct cpu_hw_events *cpuc, 553 + armv6pmu_get_event_idx(struct pmu_hw_events *cpuc, 570 554 struct hw_perf_event *event) 571 555 { 572 556 /* Always place a cycle counter into the cycle counter. */ ··· 598 578 int idx) 599 579 { 600 580 unsigned long val, mask, evt, flags; 581 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 601 582 602 583 if (ARMV6_CYCLE_COUNTER == idx) { 603 584 mask = ARMV6_PMCR_CCOUNT_IEN; ··· 619 598 * of ETM bus signal assertion cycles. The external reporting should 620 599 * be disabled and so this should never increment. 621 600 */ 622 - raw_spin_lock_irqsave(&pmu_lock, flags); 601 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 623 602 val = armv6_pmcr_read(); 624 603 val &= ~mask; 625 604 val |= evt; 626 605 armv6_pmcr_write(val); 627 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 606 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 628 607 } 629 608 630 609 static void ··· 632 611 int idx) 633 612 { 634 613 unsigned long val, mask, flags, evt = 0; 614 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 635 615 636 616 if (ARMV6_CYCLE_COUNTER == idx) { 637 617 mask = ARMV6_PMCR_CCOUNT_IEN; ··· 649 627 * Unlike UP ARMv6, we don't have a way of stopping the counters. We 650 628 * simply disable the interrupt reporting. 651 629 */ 652 - raw_spin_lock_irqsave(&pmu_lock, flags); 630 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 653 631 val = armv6_pmcr_read(); 654 632 val &= ~mask; 655 633 val |= evt; 656 634 armv6_pmcr_write(val); 657 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 635 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 658 636 } 659 637 660 - static const struct arm_pmu armv6pmu = { 638 + static int armv6_map_event(struct perf_event *event) 639 + { 640 + return map_cpu_event(event, &armv6_perf_map, 641 + &armv6_perf_cache_map, 0xFF); 642 + } 643 + 644 + static struct arm_pmu armv6pmu = { 661 645 .id = ARM_PERF_PMU_ID_V6, 662 646 .name = "v6", 663 647 .handle_irq = armv6pmu_handle_irq, ··· 674 646 .get_event_idx = armv6pmu_get_event_idx, 675 647 .start = armv6pmu_start, 676 648 .stop = armv6pmu_stop, 677 - .cache_map = &armv6_perf_cache_map, 678 - .event_map = &armv6_perf_map, 679 - .raw_event_mask = 0xFF, 649 + .map_event = armv6_map_event, 680 650 .num_events = 3, 681 651 .max_period = (1LLU << 32) - 1, 682 652 }; 683 653 684 - static const struct arm_pmu *__init armv6pmu_init(void) 654 + static struct arm_pmu *__init armv6pmu_init(void) 685 655 { 686 656 return &armv6pmu; 687 657 } ··· 691 665 * disable the interrupt reporting and update the event. When unthrottling we 692 666 * reset the period and enable the interrupt reporting. 693 667 */ 694 - static const struct arm_pmu armv6mpcore_pmu = { 668 + 669 + static int armv6mpcore_map_event(struct perf_event *event) 670 + { 671 + return map_cpu_event(event, &armv6mpcore_perf_map, 672 + &armv6mpcore_perf_cache_map, 0xFF); 673 + } 674 + 675 + static struct arm_pmu armv6mpcore_pmu = { 695 676 .id = ARM_PERF_PMU_ID_V6MP, 696 677 .name = "v6mpcore", 697 678 .handle_irq = armv6pmu_handle_irq, ··· 709 676 .get_event_idx = armv6pmu_get_event_idx, 710 677 .start = armv6pmu_start, 711 678 .stop = armv6pmu_stop, 712 - .cache_map = &armv6mpcore_perf_cache_map, 713 - .event_map = &armv6mpcore_perf_map, 714 - .raw_event_mask = 0xFF, 679 + .map_event = armv6mpcore_map_event, 715 680 .num_events = 3, 716 681 .max_period = (1LLU << 32) - 1, 717 682 }; 718 683 719 - static const struct arm_pmu *__init armv6mpcore_pmu_init(void) 684 + static struct arm_pmu *__init armv6mpcore_pmu_init(void) 720 685 { 721 686 return &armv6mpcore_pmu; 722 687 } 723 688 #else 724 - static const struct arm_pmu *__init armv6pmu_init(void) 689 + static struct arm_pmu *__init armv6pmu_init(void) 725 690 { 726 691 return NULL; 727 692 } 728 693 729 - static const struct arm_pmu *__init armv6mpcore_pmu_init(void) 694 + static struct arm_pmu *__init armv6mpcore_pmu_init(void) 730 695 { 731 696 return NULL; 732 697 }
+201 -204
arch/arm/kernel/perf_event_v7.c
··· 17 17 */ 18 18 19 19 #ifdef CONFIG_CPU_V7 20 + 21 + static struct arm_pmu armv7pmu; 22 + 20 23 /* 21 24 * Common ARMv7 event types 22 25 * ··· 679 676 }; 680 677 681 678 /* 682 - * Perf Events counters 679 + * Perf Events' indices 683 680 */ 684 - enum armv7_counters { 685 - ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */ 686 - ARMV7_COUNTER0 = 2, /* First event counter */ 687 - }; 681 + #define ARMV7_IDX_CYCLE_COUNTER 0 682 + #define ARMV7_IDX_COUNTER0 1 683 + #define ARMV7_IDX_COUNTER_LAST (ARMV7_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1) 688 684 689 - /* 690 - * The cycle counter is ARMV7_CYCLE_COUNTER. 691 - * The first event counter is ARMV7_COUNTER0. 692 - * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1). 693 - */ 694 - #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1) 685 + #define ARMV7_MAX_COUNTERS 32 686 + #define ARMV7_COUNTER_MASK (ARMV7_MAX_COUNTERS - 1) 695 687 696 688 /* 697 689 * ARMv7 low level PMNC access 698 690 */ 691 + 692 + /* 693 + * Perf Event to low level counters mapping 694 + */ 695 + #define ARMV7_IDX_TO_COUNTER(x) \ 696 + (((x) - ARMV7_IDX_COUNTER0) & ARMV7_COUNTER_MASK) 699 697 700 698 /* 701 699 * Per-CPU PMNC: config reg ··· 712 708 #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */ 713 709 714 710 /* 715 - * Available counters 716 - */ 717 - #define ARMV7_CNT0 0 /* First event counter */ 718 - #define ARMV7_CCNT 31 /* Cycle counter */ 719 - 720 - /* Perf Event to low level counters mapping */ 721 - #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0) 722 - 723 - /* 724 - * CNTENS: counters enable reg 725 - */ 726 - #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx)) 727 - #define ARMV7_CNTENS_C (1 << ARMV7_CCNT) 728 - 729 - /* 730 - * CNTENC: counters disable reg 731 - */ 732 - #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx)) 733 - #define ARMV7_CNTENC_C (1 << ARMV7_CCNT) 734 - 735 - /* 736 - * INTENS: counters overflow interrupt enable reg 737 - */ 738 - #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx)) 739 - #define ARMV7_INTENS_C (1 << ARMV7_CCNT) 740 - 741 - /* 742 - * INTENC: counters overflow interrupt disable reg 743 - */ 744 - #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx)) 745 - #define ARMV7_INTENC_C (1 << ARMV7_CCNT) 746 - 747 - /* 748 - * EVTSEL: Event selection reg 749 - */ 750 - #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */ 751 - 752 - /* 753 - * SELECT: Counter selection reg 754 - */ 755 - #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */ 756 - 757 - /* 758 711 * FLAG: counters overflow flag status reg 759 712 */ 760 - #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx)) 761 - #define ARMV7_FLAG_C (1 << ARMV7_CCNT) 762 713 #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */ 763 714 #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK 764 715 765 - static inline unsigned long armv7_pmnc_read(void) 716 + /* 717 + * PMXEVTYPER: Event selection reg 718 + */ 719 + #define ARMV7_EVTYPE_MASK 0xc00000ff /* Mask for writable bits */ 720 + #define ARMV7_EVTYPE_EVENT 0xff /* Mask for EVENT bits */ 721 + 722 + /* 723 + * Event filters for PMUv2 724 + */ 725 + #define ARMV7_EXCLUDE_PL1 (1 << 31) 726 + #define ARMV7_EXCLUDE_USER (1 << 30) 727 + #define ARMV7_INCLUDE_HYP (1 << 27) 728 + 729 + static inline u32 armv7_pmnc_read(void) 766 730 { 767 731 u32 val; 768 732 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val)); 769 733 return val; 770 734 } 771 735 772 - static inline void armv7_pmnc_write(unsigned long val) 736 + static inline void armv7_pmnc_write(u32 val) 773 737 { 774 738 val &= ARMV7_PMNC_MASK; 775 739 isb(); 776 740 asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val)); 777 741 } 778 742 779 - static inline int armv7_pmnc_has_overflowed(unsigned long pmnc) 743 + static inline int armv7_pmnc_has_overflowed(u32 pmnc) 780 744 { 781 745 return pmnc & ARMV7_OVERFLOWED_MASK; 782 746 } 783 747 784 - static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc, 785 - enum armv7_counters counter) 748 + static inline int armv7_pmnc_counter_valid(int idx) 749 + { 750 + return idx >= ARMV7_IDX_CYCLE_COUNTER && idx <= ARMV7_IDX_COUNTER_LAST; 751 + } 752 + 753 + static inline int armv7_pmnc_counter_has_overflowed(u32 pmnc, int idx) 786 754 { 787 755 int ret = 0; 756 + u32 counter; 788 757 789 - if (counter == ARMV7_CYCLE_COUNTER) 790 - ret = pmnc & ARMV7_FLAG_C; 791 - else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST)) 792 - ret = pmnc & ARMV7_FLAG_P(counter); 793 - else 758 + if (!armv7_pmnc_counter_valid(idx)) { 794 759 pr_err("CPU%u checking wrong counter %d overflow status\n", 795 - smp_processor_id(), counter); 760 + smp_processor_id(), idx); 761 + } else { 762 + counter = ARMV7_IDX_TO_COUNTER(idx); 763 + ret = pmnc & BIT(counter); 764 + } 796 765 797 766 return ret; 798 767 } 799 768 800 - static inline int armv7_pmnc_select_counter(unsigned int idx) 769 + static inline int armv7_pmnc_select_counter(int idx) 801 770 { 802 - u32 val; 771 + u32 counter; 803 772 804 - if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) { 805 - pr_err("CPU%u selecting wrong PMNC counter" 806 - " %d\n", smp_processor_id(), idx); 807 - return -1; 773 + if (!armv7_pmnc_counter_valid(idx)) { 774 + pr_err("CPU%u selecting wrong PMNC counter %d\n", 775 + smp_processor_id(), idx); 776 + return -EINVAL; 808 777 } 809 778 810 - val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK; 811 - asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val)); 779 + counter = ARMV7_IDX_TO_COUNTER(idx); 780 + asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (counter)); 812 781 isb(); 813 782 814 783 return idx; ··· 789 812 790 813 static inline u32 armv7pmu_read_counter(int idx) 791 814 { 792 - unsigned long value = 0; 815 + u32 value = 0; 793 816 794 - if (idx == ARMV7_CYCLE_COUNTER) 795 - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value)); 796 - else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) { 797 - if (armv7_pmnc_select_counter(idx) == idx) 798 - asm volatile("mrc p15, 0, %0, c9, c13, 2" 799 - : "=r" (value)); 800 - } else 817 + if (!armv7_pmnc_counter_valid(idx)) 801 818 pr_err("CPU%u reading wrong counter %d\n", 802 819 smp_processor_id(), idx); 820 + else if (idx == ARMV7_IDX_CYCLE_COUNTER) 821 + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value)); 822 + else if (armv7_pmnc_select_counter(idx) == idx) 823 + asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (value)); 803 824 804 825 return value; 805 826 } 806 827 807 828 static inline void armv7pmu_write_counter(int idx, u32 value) 808 829 { 809 - if (idx == ARMV7_CYCLE_COUNTER) 810 - asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value)); 811 - else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) { 812 - if (armv7_pmnc_select_counter(idx) == idx) 813 - asm volatile("mcr p15, 0, %0, c9, c13, 2" 814 - : : "r" (value)); 815 - } else 830 + if (!armv7_pmnc_counter_valid(idx)) 816 831 pr_err("CPU%u writing wrong counter %d\n", 817 832 smp_processor_id(), idx); 833 + else if (idx == ARMV7_IDX_CYCLE_COUNTER) 834 + asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value)); 835 + else if (armv7_pmnc_select_counter(idx) == idx) 836 + asm volatile("mcr p15, 0, %0, c9, c13, 2" : : "r" (value)); 818 837 } 819 838 820 - static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val) 839 + static inline void armv7_pmnc_write_evtsel(int idx, u32 val) 821 840 { 822 841 if (armv7_pmnc_select_counter(idx) == idx) { 823 - val &= ARMV7_EVTSEL_MASK; 842 + val &= ARMV7_EVTYPE_MASK; 824 843 asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val)); 825 844 } 826 845 } 827 846 828 - static inline u32 armv7_pmnc_enable_counter(unsigned int idx) 847 + static inline int armv7_pmnc_enable_counter(int idx) 829 848 { 830 - u32 val; 849 + u32 counter; 831 850 832 - if ((idx != ARMV7_CYCLE_COUNTER) && 833 - ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) { 834 - pr_err("CPU%u enabling wrong PMNC counter" 835 - " %d\n", smp_processor_id(), idx); 836 - return -1; 851 + if (!armv7_pmnc_counter_valid(idx)) { 852 + pr_err("CPU%u enabling wrong PMNC counter %d\n", 853 + smp_processor_id(), idx); 854 + return -EINVAL; 837 855 } 838 856 839 - if (idx == ARMV7_CYCLE_COUNTER) 840 - val = ARMV7_CNTENS_C; 841 - else 842 - val = ARMV7_CNTENS_P(idx); 843 - 844 - asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val)); 845 - 857 + counter = ARMV7_IDX_TO_COUNTER(idx); 858 + asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (BIT(counter))); 846 859 return idx; 847 860 } 848 861 849 - static inline u32 armv7_pmnc_disable_counter(unsigned int idx) 862 + static inline int armv7_pmnc_disable_counter(int idx) 850 863 { 851 - u32 val; 864 + u32 counter; 852 865 853 - 854 - if ((idx != ARMV7_CYCLE_COUNTER) && 855 - ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) { 856 - pr_err("CPU%u disabling wrong PMNC counter" 857 - " %d\n", smp_processor_id(), idx); 858 - return -1; 866 + if (!armv7_pmnc_counter_valid(idx)) { 867 + pr_err("CPU%u disabling wrong PMNC counter %d\n", 868 + smp_processor_id(), idx); 869 + return -EINVAL; 859 870 } 860 871 861 - if (idx == ARMV7_CYCLE_COUNTER) 862 - val = ARMV7_CNTENC_C; 863 - else 864 - val = ARMV7_CNTENC_P(idx); 865 - 866 - asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val)); 867 - 872 + counter = ARMV7_IDX_TO_COUNTER(idx); 873 + asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (BIT(counter))); 868 874 return idx; 869 875 } 870 876 871 - static inline u32 armv7_pmnc_enable_intens(unsigned int idx) 877 + static inline int armv7_pmnc_enable_intens(int idx) 872 878 { 873 - u32 val; 879 + u32 counter; 874 880 875 - if ((idx != ARMV7_CYCLE_COUNTER) && 876 - ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) { 877 - pr_err("CPU%u enabling wrong PMNC counter" 878 - " interrupt enable %d\n", smp_processor_id(), idx); 879 - return -1; 881 + if (!armv7_pmnc_counter_valid(idx)) { 882 + pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n", 883 + smp_processor_id(), idx); 884 + return -EINVAL; 880 885 } 881 886 882 - if (idx == ARMV7_CYCLE_COUNTER) 883 - val = ARMV7_INTENS_C; 884 - else 885 - val = ARMV7_INTENS_P(idx); 886 - 887 - asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val)); 888 - 887 + counter = ARMV7_IDX_TO_COUNTER(idx); 888 + asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (BIT(counter))); 889 889 return idx; 890 890 } 891 891 892 - static inline u32 armv7_pmnc_disable_intens(unsigned int idx) 892 + static inline int armv7_pmnc_disable_intens(int idx) 893 893 { 894 - u32 val; 894 + u32 counter; 895 895 896 - if ((idx != ARMV7_CYCLE_COUNTER) && 897 - ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) { 898 - pr_err("CPU%u disabling wrong PMNC counter" 899 - " interrupt enable %d\n", smp_processor_id(), idx); 900 - return -1; 896 + if (!armv7_pmnc_counter_valid(idx)) { 897 + pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n", 898 + smp_processor_id(), idx); 899 + return -EINVAL; 901 900 } 902 901 903 - if (idx == ARMV7_CYCLE_COUNTER) 904 - val = ARMV7_INTENC_C; 905 - else 906 - val = ARMV7_INTENC_P(idx); 907 - 908 - asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val)); 909 - 902 + counter = ARMV7_IDX_TO_COUNTER(idx); 903 + asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (BIT(counter))); 910 904 return idx; 911 905 } 912 906 ··· 921 973 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val)); 922 974 printk(KERN_INFO "CCNT =0x%08x\n", val); 923 975 924 - for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) { 976 + for (cnt = ARMV7_IDX_COUNTER0; cnt <= ARMV7_IDX_COUNTER_LAST; cnt++) { 925 977 armv7_pmnc_select_counter(cnt); 926 978 asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val)); 927 979 printk(KERN_INFO "CNT[%d] count =0x%08x\n", 928 - cnt-ARMV7_EVENT_CNT_TO_CNTx, val); 980 + ARMV7_IDX_TO_COUNTER(cnt), val); 929 981 asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val)); 930 982 printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n", 931 - cnt-ARMV7_EVENT_CNT_TO_CNTx, val); 983 + ARMV7_IDX_TO_COUNTER(cnt), val); 932 984 } 933 985 } 934 986 #endif ··· 936 988 static void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx) 937 989 { 938 990 unsigned long flags; 991 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 939 992 940 993 /* 941 994 * Enable counter and interrupt, and set the counter to count 942 995 * the event that we're interested in. 943 996 */ 944 - raw_spin_lock_irqsave(&pmu_lock, flags); 997 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 945 998 946 999 /* 947 1000 * Disable counter ··· 951 1002 952 1003 /* 953 1004 * Set event (if destined for PMNx counters) 954 - * We don't need to set the event if it's a cycle count 1005 + * We only need to set the event for the cycle counter if we 1006 + * have the ability to perform event filtering. 955 1007 */ 956 - if (idx != ARMV7_CYCLE_COUNTER) 1008 + if (armv7pmu.set_event_filter || idx != ARMV7_IDX_CYCLE_COUNTER) 957 1009 armv7_pmnc_write_evtsel(idx, hwc->config_base); 958 1010 959 1011 /* ··· 967 1017 */ 968 1018 armv7_pmnc_enable_counter(idx); 969 1019 970 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 1020 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 971 1021 } 972 1022 973 1023 static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx) 974 1024 { 975 1025 unsigned long flags; 1026 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 976 1027 977 1028 /* 978 1029 * Disable counter and interrupt 979 1030 */ 980 - raw_spin_lock_irqsave(&pmu_lock, flags); 1031 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 981 1032 982 1033 /* 983 1034 * Disable counter ··· 990 1039 */ 991 1040 armv7_pmnc_disable_intens(idx); 992 1041 993 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 1042 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 994 1043 } 995 1044 996 1045 static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev) 997 1046 { 998 - unsigned long pmnc; 1047 + u32 pmnc; 999 1048 struct perf_sample_data data; 1000 - struct cpu_hw_events *cpuc; 1049 + struct pmu_hw_events *cpuc; 1001 1050 struct pt_regs *regs; 1002 1051 int idx; 1003 1052 ··· 1020 1069 perf_sample_data_init(&data, 0); 1021 1070 1022 1071 cpuc = &__get_cpu_var(cpu_hw_events); 1023 - for (idx = 0; idx <= armpmu->num_events; ++idx) { 1072 + for (idx = 0; idx < cpu_pmu->num_events; ++idx) { 1024 1073 struct perf_event *event = cpuc->events[idx]; 1025 1074 struct hw_perf_event *hwc; 1026 - 1027 - if (!test_bit(idx, cpuc->active_mask)) 1028 - continue; 1029 1075 1030 1076 /* 1031 1077 * We have a single interrupt for all counters. Check that ··· 1038 1090 continue; 1039 1091 1040 1092 if (perf_event_overflow(event, &data, regs)) 1041 - armpmu->disable(hwc, idx); 1093 + cpu_pmu->disable(hwc, idx); 1042 1094 } 1043 1095 1044 1096 /* ··· 1056 1108 static void armv7pmu_start(void) 1057 1109 { 1058 1110 unsigned long flags; 1111 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 1059 1112 1060 - raw_spin_lock_irqsave(&pmu_lock, flags); 1113 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 1061 1114 /* Enable all counters */ 1062 1115 armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E); 1063 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 1116 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 1064 1117 } 1065 1118 1066 1119 static void armv7pmu_stop(void) 1067 1120 { 1068 1121 unsigned long flags; 1122 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 1069 1123 1070 - raw_spin_lock_irqsave(&pmu_lock, flags); 1124 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 1071 1125 /* Disable all counters */ 1072 1126 armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E); 1073 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 1127 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 1074 1128 } 1075 1129 1076 - static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc, 1130 + static int armv7pmu_get_event_idx(struct pmu_hw_events *cpuc, 1077 1131 struct hw_perf_event *event) 1078 1132 { 1079 1133 int idx; 1134 + unsigned long evtype = event->config_base & ARMV7_EVTYPE_EVENT; 1080 1135 1081 1136 /* Always place a cycle counter into the cycle counter. */ 1082 - if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) { 1083 - if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask)) 1137 + if (evtype == ARMV7_PERFCTR_CPU_CYCLES) { 1138 + if (test_and_set_bit(ARMV7_IDX_CYCLE_COUNTER, cpuc->used_mask)) 1084 1139 return -EAGAIN; 1085 1140 1086 - return ARMV7_CYCLE_COUNTER; 1087 - } else { 1088 - /* 1089 - * For anything other than a cycle counter, try and use 1090 - * the events counters 1091 - */ 1092 - for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) { 1093 - if (!test_and_set_bit(idx, cpuc->used_mask)) 1094 - return idx; 1095 - } 1096 - 1097 - /* The counters are all in use. */ 1098 - return -EAGAIN; 1141 + return ARMV7_IDX_CYCLE_COUNTER; 1099 1142 } 1143 + 1144 + /* 1145 + * For anything other than a cycle counter, try and use 1146 + * the events counters 1147 + */ 1148 + for (idx = ARMV7_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) { 1149 + if (!test_and_set_bit(idx, cpuc->used_mask)) 1150 + return idx; 1151 + } 1152 + 1153 + /* The counters are all in use. */ 1154 + return -EAGAIN; 1155 + } 1156 + 1157 + /* 1158 + * Add an event filter to a given event. This will only work for PMUv2 PMUs. 1159 + */ 1160 + static int armv7pmu_set_event_filter(struct hw_perf_event *event, 1161 + struct perf_event_attr *attr) 1162 + { 1163 + unsigned long config_base = 0; 1164 + 1165 + if (attr->exclude_idle) 1166 + return -EPERM; 1167 + if (attr->exclude_user) 1168 + config_base |= ARMV7_EXCLUDE_USER; 1169 + if (attr->exclude_kernel) 1170 + config_base |= ARMV7_EXCLUDE_PL1; 1171 + if (!attr->exclude_hv) 1172 + config_base |= ARMV7_INCLUDE_HYP; 1173 + 1174 + /* 1175 + * Install the filter into config_base as this is used to 1176 + * construct the event type. 1177 + */ 1178 + event->config_base = config_base; 1179 + 1180 + return 0; 1100 1181 } 1101 1182 1102 1183 static void armv7pmu_reset(void *info) 1103 1184 { 1104 - u32 idx, nb_cnt = armpmu->num_events; 1185 + u32 idx, nb_cnt = cpu_pmu->num_events; 1105 1186 1106 1187 /* The counter and interrupt enable registers are unknown at reset. */ 1107 - for (idx = 1; idx < nb_cnt; ++idx) 1188 + for (idx = ARMV7_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) 1108 1189 armv7pmu_disable_event(NULL, idx); 1109 1190 1110 1191 /* Initialize & Reset PMNC: C and P bits */ 1111 1192 armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C); 1193 + } 1194 + 1195 + static int armv7_a8_map_event(struct perf_event *event) 1196 + { 1197 + return map_cpu_event(event, &armv7_a8_perf_map, 1198 + &armv7_a8_perf_cache_map, 0xFF); 1199 + } 1200 + 1201 + static int armv7_a9_map_event(struct perf_event *event) 1202 + { 1203 + return map_cpu_event(event, &armv7_a9_perf_map, 1204 + &armv7_a9_perf_cache_map, 0xFF); 1205 + } 1206 + 1207 + static int armv7_a5_map_event(struct perf_event *event) 1208 + { 1209 + return map_cpu_event(event, &armv7_a5_perf_map, 1210 + &armv7_a5_perf_cache_map, 0xFF); 1211 + } 1212 + 1213 + static int armv7_a15_map_event(struct perf_event *event) 1214 + { 1215 + return map_cpu_event(event, &armv7_a15_perf_map, 1216 + &armv7_a15_perf_cache_map, 0xFF); 1112 1217 } 1113 1218 1114 1219 static struct arm_pmu armv7pmu = { ··· 1174 1173 .start = armv7pmu_start, 1175 1174 .stop = armv7pmu_stop, 1176 1175 .reset = armv7pmu_reset, 1177 - .raw_event_mask = 0xFF, 1178 1176 .max_period = (1LLU << 32) - 1, 1179 1177 }; 1180 1178 ··· 1188 1188 return nb_cnt + 1; 1189 1189 } 1190 1190 1191 - static const struct arm_pmu *__init armv7_a8_pmu_init(void) 1191 + static struct arm_pmu *__init armv7_a8_pmu_init(void) 1192 1192 { 1193 1193 armv7pmu.id = ARM_PERF_PMU_ID_CA8; 1194 1194 armv7pmu.name = "ARMv7 Cortex-A8"; 1195 - armv7pmu.cache_map = &armv7_a8_perf_cache_map; 1196 - armv7pmu.event_map = &armv7_a8_perf_map; 1195 + armv7pmu.map_event = armv7_a8_map_event; 1197 1196 armv7pmu.num_events = armv7_read_num_pmnc_events(); 1198 1197 return &armv7pmu; 1199 1198 } 1200 1199 1201 - static const struct arm_pmu *__init armv7_a9_pmu_init(void) 1200 + static struct arm_pmu *__init armv7_a9_pmu_init(void) 1202 1201 { 1203 1202 armv7pmu.id = ARM_PERF_PMU_ID_CA9; 1204 1203 armv7pmu.name = "ARMv7 Cortex-A9"; 1205 - armv7pmu.cache_map = &armv7_a9_perf_cache_map; 1206 - armv7pmu.event_map = &armv7_a9_perf_map; 1204 + armv7pmu.map_event = armv7_a9_map_event; 1207 1205 armv7pmu.num_events = armv7_read_num_pmnc_events(); 1208 1206 return &armv7pmu; 1209 1207 } 1210 1208 1211 - static const struct arm_pmu *__init armv7_a5_pmu_init(void) 1209 + static struct arm_pmu *__init armv7_a5_pmu_init(void) 1212 1210 { 1213 1211 armv7pmu.id = ARM_PERF_PMU_ID_CA5; 1214 1212 armv7pmu.name = "ARMv7 Cortex-A5"; 1215 - armv7pmu.cache_map = &armv7_a5_perf_cache_map; 1216 - armv7pmu.event_map = &armv7_a5_perf_map; 1213 + armv7pmu.map_event = armv7_a5_map_event; 1217 1214 armv7pmu.num_events = armv7_read_num_pmnc_events(); 1218 1215 return &armv7pmu; 1219 1216 } 1220 1217 1221 - static const struct arm_pmu *__init armv7_a15_pmu_init(void) 1218 + static struct arm_pmu *__init armv7_a15_pmu_init(void) 1222 1219 { 1223 1220 armv7pmu.id = ARM_PERF_PMU_ID_CA15; 1224 1221 armv7pmu.name = "ARMv7 Cortex-A15"; 1225 - armv7pmu.cache_map = &armv7_a15_perf_cache_map; 1226 - armv7pmu.event_map = &armv7_a15_perf_map; 1222 + armv7pmu.map_event = armv7_a15_map_event; 1227 1223 armv7pmu.num_events = armv7_read_num_pmnc_events(); 1224 + armv7pmu.set_event_filter = armv7pmu_set_event_filter; 1228 1225 return &armv7pmu; 1229 1226 } 1230 1227 #else 1231 - static const struct arm_pmu *__init armv7_a8_pmu_init(void) 1228 + static struct arm_pmu *__init armv7_a8_pmu_init(void) 1232 1229 { 1233 1230 return NULL; 1234 1231 } 1235 1232 1236 - static const struct arm_pmu *__init armv7_a9_pmu_init(void) 1233 + static struct arm_pmu *__init armv7_a9_pmu_init(void) 1237 1234 { 1238 1235 return NULL; 1239 1236 } 1240 1237 1241 - static const struct arm_pmu *__init armv7_a5_pmu_init(void) 1238 + static struct arm_pmu *__init armv7_a5_pmu_init(void) 1242 1239 { 1243 1240 return NULL; 1244 1241 } 1245 1242 1246 - static const struct arm_pmu *__init armv7_a15_pmu_init(void) 1243 + static struct arm_pmu *__init armv7_a15_pmu_init(void) 1247 1244 { 1248 1245 return NULL; 1249 1246 }
+47 -43
arch/arm/kernel/perf_event_xscale.c
··· 40 40 }; 41 41 42 42 enum xscale_counters { 43 - XSCALE_CYCLE_COUNTER = 1, 43 + XSCALE_CYCLE_COUNTER = 0, 44 44 XSCALE_COUNTER0, 45 45 XSCALE_COUNTER1, 46 46 XSCALE_COUNTER2, ··· 222 222 { 223 223 unsigned long pmnc; 224 224 struct perf_sample_data data; 225 - struct cpu_hw_events *cpuc; 225 + struct pmu_hw_events *cpuc; 226 226 struct pt_regs *regs; 227 227 int idx; 228 228 ··· 249 249 perf_sample_data_init(&data, 0); 250 250 251 251 cpuc = &__get_cpu_var(cpu_hw_events); 252 - for (idx = 0; idx <= armpmu->num_events; ++idx) { 252 + for (idx = 0; idx < cpu_pmu->num_events; ++idx) { 253 253 struct perf_event *event = cpuc->events[idx]; 254 254 struct hw_perf_event *hwc; 255 - 256 - if (!test_bit(idx, cpuc->active_mask)) 257 - continue; 258 255 259 256 if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx)) 260 257 continue; ··· 263 266 continue; 264 267 265 268 if (perf_event_overflow(event, &data, regs)) 266 - armpmu->disable(hwc, idx); 269 + cpu_pmu->disable(hwc, idx); 267 270 } 268 271 269 272 irq_work_run(); ··· 281 284 xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx) 282 285 { 283 286 unsigned long val, mask, evt, flags; 287 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 284 288 285 289 switch (idx) { 286 290 case XSCALE_CYCLE_COUNTER: ··· 303 305 return; 304 306 } 305 307 306 - raw_spin_lock_irqsave(&pmu_lock, flags); 308 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 307 309 val = xscale1pmu_read_pmnc(); 308 310 val &= ~mask; 309 311 val |= evt; 310 312 xscale1pmu_write_pmnc(val); 311 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 313 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 312 314 } 313 315 314 316 static void 315 317 xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx) 316 318 { 317 319 unsigned long val, mask, evt, flags; 320 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 318 321 319 322 switch (idx) { 320 323 case XSCALE_CYCLE_COUNTER: ··· 335 336 return; 336 337 } 337 338 338 - raw_spin_lock_irqsave(&pmu_lock, flags); 339 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 339 340 val = xscale1pmu_read_pmnc(); 340 341 val &= ~mask; 341 342 val |= evt; 342 343 xscale1pmu_write_pmnc(val); 343 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 344 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 344 345 } 345 346 346 347 static int 347 - xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc, 348 + xscale1pmu_get_event_idx(struct pmu_hw_events *cpuc, 348 349 struct hw_perf_event *event) 349 350 { 350 351 if (XSCALE_PERFCTR_CCNT == event->config_base) { ··· 367 368 xscale1pmu_start(void) 368 369 { 369 370 unsigned long flags, val; 371 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 370 372 371 - raw_spin_lock_irqsave(&pmu_lock, flags); 373 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 372 374 val = xscale1pmu_read_pmnc(); 373 375 val |= XSCALE_PMU_ENABLE; 374 376 xscale1pmu_write_pmnc(val); 375 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 377 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 376 378 } 377 379 378 380 static void 379 381 xscale1pmu_stop(void) 380 382 { 381 383 unsigned long flags, val; 384 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 382 385 383 - raw_spin_lock_irqsave(&pmu_lock, flags); 386 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 384 387 val = xscale1pmu_read_pmnc(); 385 388 val &= ~XSCALE_PMU_ENABLE; 386 389 xscale1pmu_write_pmnc(val); 387 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 390 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 388 391 } 389 392 390 393 static inline u32 ··· 425 424 } 426 425 } 427 426 428 - static const struct arm_pmu xscale1pmu = { 427 + static int xscale_map_event(struct perf_event *event) 428 + { 429 + return map_cpu_event(event, &xscale_perf_map, 430 + &xscale_perf_cache_map, 0xFF); 431 + } 432 + 433 + static struct arm_pmu xscale1pmu = { 429 434 .id = ARM_PERF_PMU_ID_XSCALE1, 430 435 .name = "xscale1", 431 436 .handle_irq = xscale1pmu_handle_irq, ··· 442 435 .get_event_idx = xscale1pmu_get_event_idx, 443 436 .start = xscale1pmu_start, 444 437 .stop = xscale1pmu_stop, 445 - .cache_map = &xscale_perf_cache_map, 446 - .event_map = &xscale_perf_map, 447 - .raw_event_mask = 0xFF, 438 + .map_event = xscale_map_event, 448 439 .num_events = 3, 449 440 .max_period = (1LLU << 32) - 1, 450 441 }; 451 442 452 - static const struct arm_pmu *__init xscale1pmu_init(void) 443 + static struct arm_pmu *__init xscale1pmu_init(void) 453 444 { 454 445 return &xscale1pmu; 455 446 } ··· 565 560 { 566 561 unsigned long pmnc, of_flags; 567 562 struct perf_sample_data data; 568 - struct cpu_hw_events *cpuc; 563 + struct pmu_hw_events *cpuc; 569 564 struct pt_regs *regs; 570 565 int idx; 571 566 ··· 586 581 perf_sample_data_init(&data, 0); 587 582 588 583 cpuc = &__get_cpu_var(cpu_hw_events); 589 - for (idx = 0; idx <= armpmu->num_events; ++idx) { 584 + for (idx = 0; idx < cpu_pmu->num_events; ++idx) { 590 585 struct perf_event *event = cpuc->events[idx]; 591 586 struct hw_perf_event *hwc; 592 - 593 - if (!test_bit(idx, cpuc->active_mask)) 594 - continue; 595 587 596 588 if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx)) 597 589 continue; ··· 600 598 continue; 601 599 602 600 if (perf_event_overflow(event, &data, regs)) 603 - armpmu->disable(hwc, idx); 601 + cpu_pmu->disable(hwc, idx); 604 602 } 605 603 606 604 irq_work_run(); ··· 618 616 xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx) 619 617 { 620 618 unsigned long flags, ien, evtsel; 619 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 621 620 622 621 ien = xscale2pmu_read_int_enable(); 623 622 evtsel = xscale2pmu_read_event_select(); ··· 652 649 return; 653 650 } 654 651 655 - raw_spin_lock_irqsave(&pmu_lock, flags); 652 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 656 653 xscale2pmu_write_event_select(evtsel); 657 654 xscale2pmu_write_int_enable(ien); 658 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 655 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 659 656 } 660 657 661 658 static void 662 659 xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx) 663 660 { 664 661 unsigned long flags, ien, evtsel; 662 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 665 663 666 664 ien = xscale2pmu_read_int_enable(); 667 665 evtsel = xscale2pmu_read_event_select(); ··· 696 692 return; 697 693 } 698 694 699 - raw_spin_lock_irqsave(&pmu_lock, flags); 695 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 700 696 xscale2pmu_write_event_select(evtsel); 701 697 xscale2pmu_write_int_enable(ien); 702 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 698 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 703 699 } 704 700 705 701 static int 706 - xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc, 702 + xscale2pmu_get_event_idx(struct pmu_hw_events *cpuc, 707 703 struct hw_perf_event *event) 708 704 { 709 705 int idx = xscale1pmu_get_event_idx(cpuc, event); ··· 722 718 xscale2pmu_start(void) 723 719 { 724 720 unsigned long flags, val; 721 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 725 722 726 - raw_spin_lock_irqsave(&pmu_lock, flags); 723 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 727 724 val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64; 728 725 val |= XSCALE_PMU_ENABLE; 729 726 xscale2pmu_write_pmnc(val); 730 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 727 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 731 728 } 732 729 733 730 static void 734 731 xscale2pmu_stop(void) 735 732 { 736 733 unsigned long flags, val; 734 + struct pmu_hw_events *events = cpu_pmu->get_hw_events(); 737 735 738 - raw_spin_lock_irqsave(&pmu_lock, flags); 736 + raw_spin_lock_irqsave(&events->pmu_lock, flags); 739 737 val = xscale2pmu_read_pmnc(); 740 738 val &= ~XSCALE_PMU_ENABLE; 741 739 xscale2pmu_write_pmnc(val); 742 - raw_spin_unlock_irqrestore(&pmu_lock, flags); 740 + raw_spin_unlock_irqrestore(&events->pmu_lock, flags); 743 741 } 744 742 745 743 static inline u32 ··· 792 786 } 793 787 } 794 788 795 - static const struct arm_pmu xscale2pmu = { 789 + static struct arm_pmu xscale2pmu = { 796 790 .id = ARM_PERF_PMU_ID_XSCALE2, 797 791 .name = "xscale2", 798 792 .handle_irq = xscale2pmu_handle_irq, ··· 803 797 .get_event_idx = xscale2pmu_get_event_idx, 804 798 .start = xscale2pmu_start, 805 799 .stop = xscale2pmu_stop, 806 - .cache_map = &xscale_perf_cache_map, 807 - .event_map = &xscale_perf_map, 808 - .raw_event_mask = 0xFF, 800 + .map_event = xscale_map_event, 809 801 .num_events = 5, 810 802 .max_period = (1LLU << 32) - 1, 811 803 }; 812 804 813 - static const struct arm_pmu *__init xscale2pmu_init(void) 805 + static struct arm_pmu *__init xscale2pmu_init(void) 814 806 { 815 807 return &xscale2pmu; 816 808 } 817 809 #else 818 - static const struct arm_pmu *__init xscale1pmu_init(void) 810 + static struct arm_pmu *__init xscale1pmu_init(void) 819 811 { 820 812 return NULL; 821 813 } 822 814 823 - static const struct arm_pmu *__init xscale2pmu_init(void) 815 + static struct arm_pmu *__init xscale2pmu_init(void) 824 816 { 825 817 return NULL; 826 818 }
+8 -174
arch/arm/kernel/pmu.c
··· 10 10 * 11 11 */ 12 12 13 - #define pr_fmt(fmt) "PMU: " fmt 14 - 15 - #include <linux/cpumask.h> 16 13 #include <linux/err.h> 17 - #include <linux/interrupt.h> 18 14 #include <linux/kernel.h> 19 15 #include <linux/module.h> 20 - #include <linux/of_device.h> 21 - #include <linux/platform_device.h> 22 16 23 17 #include <asm/pmu.h> 24 18 25 - static volatile long pmu_lock; 19 + /* 20 + * PMU locking to ensure mutual exclusion between different subsystems. 21 + */ 22 + static unsigned long pmu_lock[BITS_TO_LONGS(ARM_NUM_PMU_DEVICES)]; 26 23 27 - static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES]; 28 - 29 - static int __devinit pmu_register(struct platform_device *pdev, 30 - enum arm_pmu_type type) 31 - { 32 - if (type < 0 || type >= ARM_NUM_PMU_DEVICES) { 33 - pr_warning("received registration request for unknown " 34 - "PMU device type %d\n", type); 35 - return -EINVAL; 36 - } 37 - 38 - if (pmu_devices[type]) { 39 - pr_warning("rejecting duplicate registration of PMU device " 40 - "type %d.", type); 41 - return -ENOSPC; 42 - } 43 - 44 - pr_info("registered new PMU device of type %d\n", type); 45 - pmu_devices[type] = pdev; 46 - return 0; 47 - } 48 - 49 - #define OF_MATCH_PMU(_name, _type) { \ 50 - .compatible = _name, \ 51 - .data = (void *)_type, \ 52 - } 53 - 54 - #define OF_MATCH_CPU(name) OF_MATCH_PMU(name, ARM_PMU_DEVICE_CPU) 55 - 56 - static struct of_device_id armpmu_of_device_ids[] = { 57 - OF_MATCH_CPU("arm,cortex-a9-pmu"), 58 - OF_MATCH_CPU("arm,cortex-a8-pmu"), 59 - OF_MATCH_CPU("arm,arm1136-pmu"), 60 - OF_MATCH_CPU("arm,arm1176-pmu"), 61 - {}, 62 - }; 63 - 64 - #define PLAT_MATCH_PMU(_name, _type) { \ 65 - .name = _name, \ 66 - .driver_data = _type, \ 67 - } 68 - 69 - #define PLAT_MATCH_CPU(_name) PLAT_MATCH_PMU(_name, ARM_PMU_DEVICE_CPU) 70 - 71 - static struct platform_device_id armpmu_plat_device_ids[] = { 72 - PLAT_MATCH_CPU("arm-pmu"), 73 - {}, 74 - }; 75 - 76 - enum arm_pmu_type armpmu_device_type(struct platform_device *pdev) 77 - { 78 - const struct of_device_id *of_id; 79 - const struct platform_device_id *pdev_id; 80 - 81 - /* provided by of_device_id table */ 82 - if (pdev->dev.of_node) { 83 - of_id = of_match_device(armpmu_of_device_ids, &pdev->dev); 84 - BUG_ON(!of_id); 85 - return (enum arm_pmu_type)of_id->data; 86 - } 87 - 88 - /* Provided by platform_device_id table */ 89 - pdev_id = platform_get_device_id(pdev); 90 - BUG_ON(!pdev_id); 91 - return pdev_id->driver_data; 92 - } 93 - 94 - static int __devinit armpmu_device_probe(struct platform_device *pdev) 95 - { 96 - return pmu_register(pdev, armpmu_device_type(pdev)); 97 - } 98 - 99 - static struct platform_driver armpmu_driver = { 100 - .driver = { 101 - .name = "arm-pmu", 102 - .of_match_table = armpmu_of_device_ids, 103 - }, 104 - .probe = armpmu_device_probe, 105 - .id_table = armpmu_plat_device_ids, 106 - }; 107 - 108 - static int __init register_pmu_driver(void) 109 - { 110 - return platform_driver_register(&armpmu_driver); 111 - } 112 - device_initcall(register_pmu_driver); 113 - 114 - struct platform_device * 24 + int 115 25 reserve_pmu(enum arm_pmu_type type) 116 26 { 117 - struct platform_device *pdev; 118 - 119 - if (test_and_set_bit_lock(type, &pmu_lock)) { 120 - pdev = ERR_PTR(-EBUSY); 121 - } else if (pmu_devices[type] == NULL) { 122 - clear_bit_unlock(type, &pmu_lock); 123 - pdev = ERR_PTR(-ENODEV); 124 - } else { 125 - pdev = pmu_devices[type]; 126 - } 127 - 128 - return pdev; 27 + return test_and_set_bit_lock(type, pmu_lock) ? -EBUSY : 0; 129 28 } 130 29 EXPORT_SYMBOL_GPL(reserve_pmu); 131 30 132 - int 31 + void 133 32 release_pmu(enum arm_pmu_type type) 134 33 { 135 - if (WARN_ON(!pmu_devices[type])) 136 - return -EINVAL; 137 - clear_bit_unlock(type, &pmu_lock); 138 - return 0; 34 + clear_bit_unlock(type, pmu_lock); 139 35 } 140 - EXPORT_SYMBOL_GPL(release_pmu); 141 - 142 - static int 143 - set_irq_affinity(int irq, 144 - unsigned int cpu) 145 - { 146 - #ifdef CONFIG_SMP 147 - int err = irq_set_affinity(irq, cpumask_of(cpu)); 148 - if (err) 149 - pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", 150 - irq, cpu); 151 - return err; 152 - #else 153 - return -EINVAL; 154 - #endif 155 - } 156 - 157 - static int 158 - init_cpu_pmu(void) 159 - { 160 - int i, irqs, err = 0; 161 - struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU]; 162 - 163 - if (!pdev) 164 - return -ENODEV; 165 - 166 - irqs = pdev->num_resources; 167 - 168 - /* 169 - * If we have a single PMU interrupt that we can't shift, assume that 170 - * we're running on a uniprocessor machine and continue. 171 - */ 172 - if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0))) 173 - return 0; 174 - 175 - for (i = 0; i < irqs; ++i) { 176 - err = set_irq_affinity(platform_get_irq(pdev, i), i); 177 - if (err) 178 - break; 179 - } 180 - 181 - return err; 182 - } 183 - 184 - int 185 - init_pmu(enum arm_pmu_type type) 186 - { 187 - int err = 0; 188 - 189 - switch (type) { 190 - case ARM_PMU_DEVICE_CPU: 191 - err = init_cpu_pmu(); 192 - break; 193 - default: 194 - pr_warning("attempt to initialise PMU of unknown " 195 - "type %d\n", type); 196 - err = -EINVAL; 197 - } 198 - 199 - return err; 200 - } 201 - EXPORT_SYMBOL_GPL(init_pmu);
+2 -2
kernel/events/core.c
··· 5715 5715 pmu = idr_find(&pmu_idr, event->attr.type); 5716 5716 rcu_read_unlock(); 5717 5717 if (pmu) { 5718 + event->pmu = pmu; 5718 5719 ret = pmu->event_init(event); 5719 5720 if (ret) 5720 5721 pmu = ERR_PTR(ret); ··· 5723 5722 } 5724 5723 5725 5724 list_for_each_entry_rcu(pmu, &pmus, entry) { 5725 + event->pmu = pmu; 5726 5726 ret = pmu->event_init(event); 5727 5727 if (!ret) 5728 5728 goto unlock; ··· 5849 5847 kfree(event); 5850 5848 return ERR_PTR(err); 5851 5849 } 5852 - 5853 - event->pmu = pmu; 5854 5850 5855 5851 if (!event->parent) { 5856 5852 if (event->attach_state & PERF_ATTACH_TASK)