2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
11 // gator_events_armvX.c is used for Linux 2.6.x
12 #if GATOR_PERF_PMU_SUPPORT
16 #include <linux/of_address.h>
18 #include <linux/perf_event.h>
19 #include <linux/slab.h>
21 extern bool event_based_sampling;
23 // Maximum number of per-core counters - currently reserves enough space for two full hardware PMUs for big.LITTLE
26 // Maximum number of uncore counters
27 // + 1 for the cci-400 cycles counter
28 #define UCCNT (CCI_400 + 1)
30 // Default to 0 if unable to probe the revision which was the previous behavior
31 #define DEFAULT_CCI_REVISION 0
33 // A gator_attr is needed for every counter
35 // Set once in gator_events_perf_pmu_*_init - the name of the event in the gatorfs
37 // Exposed in gatorfs - set by gatord to enable this counter
38 unsigned long enabled;
39 // Set once in gator_events_perf_pmu_*_init - the perf type to use, see perf_type_id in the perf_event.h header file.
41 // Exposed in gatorfs - set by gatord to select the event to collect
43 // Exposed in gatorfs - set by gatord with the sample period to use and enable EBS for this counter
45 // Exposed as read only in gatorfs - set once in __attr_init as the key to use in the APC data
49 // Per-core counter attributes
50 static struct gator_attr attrs[CNTMAX];
51 // Number of initialized per-core counters
52 static int attr_count;
53 // Uncore counter attributes
54 static struct gator_attr uc_attrs[UCCNT];
55 // Number of initialized uncore counters
56 static int uc_attr_count;
63 struct perf_event *pevent;
64 struct perf_event_attr *pevent_attr;
67 static DEFINE_PER_CPU(struct gator_event[CNTMAX], events);
68 static struct gator_event uc_events[UCCNT];
69 static DEFINE_PER_CPU(int[(CNTMAX + UCCNT)*2], perf_cnt);
71 static void gator_events_perf_pmu_stop(void);
73 static int __create_files(struct super_block *sb, struct dentry *root, struct gator_attr *const attr)
77 if (attr->name[0] == '\0') {
80 dir = gatorfs_mkdir(sb, root, attr->name);
84 gatorfs_create_ulong(sb, dir, "enabled", &attr->enabled);
85 gatorfs_create_ulong(sb, dir, "count", &attr->count);
86 gatorfs_create_ro_ulong(sb, dir, "key", &attr->key);
87 gatorfs_create_ulong(sb, dir, "event", &attr->event);
92 static int gator_events_perf_pmu_create_files(struct super_block *sb, struct dentry *root)
96 for (cnt = 0; cnt < attr_count; cnt++) {
97 if (__create_files(sb, root, &attrs[cnt]) != 0) {
102 for (cnt = 0; cnt < uc_attr_count; cnt++) {
103 if (__create_files(sb, root, &uc_attrs[cnt]) != 0) {
111 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
112 static void ebs_overflow_handler(struct perf_event *event, int unused, struct perf_sample_data *data, struct pt_regs *regs)
114 static void ebs_overflow_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
117 gator_backtrace_handler(regs);
120 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
121 static void dummy_handler(struct perf_event *event, int unused, struct perf_sample_data *data, struct pt_regs *regs)
123 static void dummy_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
126 // Required as perf_event_create_kernel_counter() requires an overflow handler, even though all we do is poll
129 static int gator_events_perf_pmu_read(int **buffer);
131 static int gator_events_perf_pmu_online(int **buffer, bool migrate)
133 return gator_events_perf_pmu_read(buffer);
136 static void __online_dispatch(int cpu, bool migrate, struct gator_attr *const attr, struct gator_event *const event)
138 perf_overflow_handler_t handler;
142 if (event->pevent != NULL || event->pevent_attr == 0 || migrate) {
146 if (attr->count > 0) {
147 handler = ebs_overflow_handler;
149 handler = dummy_handler;
152 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
153 event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler);
155 event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler, 0);
157 if (IS_ERR(event->pevent)) {
158 pr_debug("gator: unable to online a counter on cpu %d\n", cpu);
159 event->pevent = NULL;
163 if (event->pevent->state != PERF_EVENT_STATE_ACTIVE) {
164 pr_debug("gator: inactive counter on cpu %d\n", cpu);
165 perf_event_release_kernel(event->pevent);
166 event->pevent = NULL;
171 static void gator_events_perf_pmu_online_dispatch(int cpu, bool migrate)
175 cpu = pcpu_to_lcpu(cpu);
177 for (cnt = 0; cnt < attr_count; cnt++) {
178 __online_dispatch(cpu, migrate, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
182 for (cnt = 0; cnt < uc_attr_count; cnt++) {
183 __online_dispatch(cpu, migrate, &uc_attrs[cnt], &uc_events[cnt]);
188 static void __offline_dispatch(int cpu, struct gator_event *const event)
190 struct perf_event *pe = NULL;
194 event->pevent = NULL;
198 perf_event_release_kernel(pe);
202 static void gator_events_perf_pmu_offline_dispatch(int cpu, bool migrate)
209 cpu = pcpu_to_lcpu(cpu);
211 for (cnt = 0; cnt < attr_count; cnt++) {
212 __offline_dispatch(cpu, &per_cpu(events, cpu)[cnt]);
216 for (cnt = 0; cnt < uc_attr_count; cnt++) {
217 __offline_dispatch(cpu, &uc_events[cnt]);
222 static int __check_ebs(struct gator_attr *const attr)
224 if (attr->count > 0) {
225 if (!event_based_sampling) {
226 event_based_sampling = true;
228 printk(KERN_WARNING "gator: Only one ebs counter is allowed\n");
236 static int __start(struct gator_attr *const attr, struct gator_event *const event)
238 u32 size = sizeof(struct perf_event_attr);
240 event->pevent = NULL;
241 if (!attr->enabled) { // Skip disabled counters
247 event->prev_delta = 0;
248 event->pevent_attr = kmalloc(size, GFP_KERNEL);
249 if (!event->pevent_attr) {
250 gator_events_perf_pmu_stop();
254 memset(event->pevent_attr, 0, size);
255 event->pevent_attr->type = attr->type;
256 event->pevent_attr->size = size;
257 event->pevent_attr->config = attr->event;
258 event->pevent_attr->sample_period = attr->count;
259 event->pevent_attr->pinned = 1;
264 static int gator_events_perf_pmu_start(void)
268 event_based_sampling = false;
269 for (cnt = 0; cnt < attr_count; cnt++) {
270 if (__check_ebs(&attrs[cnt]) != 0) {
275 for (cnt = 0; cnt < uc_attr_count; cnt++) {
276 if (__check_ebs(&uc_attrs[cnt]) != 0) {
281 for_each_present_cpu(cpu) {
282 for (cnt = 0; cnt < attr_count; cnt++) {
283 if (__start(&attrs[cnt], &per_cpu(events, cpu)[cnt]) != 0) {
289 for (cnt = 0; cnt < uc_attr_count; cnt++) {
290 if (__start(&uc_attrs[cnt], &uc_events[cnt]) != 0) {
298 static void __event_stop(struct gator_event *const event)
300 if (event->pevent_attr) {
301 kfree(event->pevent_attr);
302 event->pevent_attr = NULL;
306 static void __attr_stop(struct gator_attr *const attr)
313 static void gator_events_perf_pmu_stop(void)
315 unsigned int cnt, cpu;
317 for_each_present_cpu(cpu) {
318 for (cnt = 0; cnt < attr_count; cnt++) {
319 __event_stop(&per_cpu(events, cpu)[cnt]);
323 for (cnt = 0; cnt < uc_attr_count; cnt++) {
324 __event_stop(&uc_events[cnt]);
327 for (cnt = 0; cnt < attr_count; cnt++) {
328 __attr_stop(&attrs[cnt]);
331 for (cnt = 0; cnt < uc_attr_count; cnt++) {
332 __attr_stop(&uc_attrs[cnt]);
336 static void __read(int *const len, int cpu, struct gator_attr *const attr, struct gator_event *const event)
340 struct perf_event *const ev = event->pevent;
341 if (ev != NULL && ev->state == PERF_EVENT_STATE_ACTIVE) {
342 /* After creating the perf counter in __online_dispatch, there
343 * is a race condition between gator_events_perf_pmu_online and
344 * gator_events_perf_pmu_read. So have
345 * gator_events_perf_pmu_online call gator_events_perf_pmu_read
346 * and in __read check to see if it's the first call after
347 * __online_dispatch and if so, run the online code.
351 event->prev = event->curr = local64_read(&ev->count);
352 event->prev_delta = 0;
353 per_cpu(perf_cnt, cpu)[(*len)++] = attr->key;
354 per_cpu(perf_cnt, cpu)[(*len)++] = 0;
358 event->curr = local64_read(&ev->count);
359 delta = event->curr - event->prev;
360 if (delta != 0 || delta != event->prev_delta) {
361 event->prev_delta = delta;
362 event->prev = event->curr;
363 per_cpu(perf_cnt, cpu)[(*len)++] = attr->key;
367 per_cpu(perf_cnt, cpu)[(*len)++] = delta;
373 static int gator_events_perf_pmu_read(int **buffer)
376 const int cpu = get_logical_cpu();
378 for (cnt = 0; cnt < attr_count; cnt++) {
379 __read(&len, cpu, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
383 for (cnt = 0; cnt < uc_attr_count; cnt++) {
384 __read(&len, cpu, &uc_attrs[cnt], &uc_events[cnt]);
389 *buffer = per_cpu(perf_cnt, cpu);
395 static struct gator_interface gator_events_perf_pmu_interface = {
396 .create_files = gator_events_perf_pmu_create_files,
397 .start = gator_events_perf_pmu_start,
398 .stop = gator_events_perf_pmu_stop,
399 .online = gator_events_perf_pmu_online,
400 .online_dispatch = gator_events_perf_pmu_online_dispatch,
401 .offline_dispatch = gator_events_perf_pmu_offline_dispatch,
402 .read = gator_events_perf_pmu_read,
405 static void __attr_init(struct gator_attr *const attr)
407 attr->name[0] = '\0';
412 attr->key = gator_events_get_key();
417 static const struct of_device_id arm_cci_matches[] = {
418 {.compatible = "arm,cci-400" },
422 static int probe_cci_revision(void)
424 struct device_node *np;
426 void __iomem *cci_ctrl_base;
428 int ret = DEFAULT_CCI_REVISION;
430 np = of_find_matching_node(NULL, arm_cci_matches);
435 if (of_address_to_resource(np, 0, &res)) {
439 cci_ctrl_base = ioremap(res.start, resource_size(&res));
441 rev = (readl_relaxed(cci_ctrl_base + 0xfe8) >> 4) & 0xf;
445 } else if (rev <= 6) {
449 iounmap(cci_ctrl_base);
459 static int probe_cci_revision(void)
461 return DEFAULT_CCI_REVISION;
466 static void gator_events_perf_pmu_cci_init(const int type)
469 const char *cci_name;
471 switch (probe_cci_revision()) {
473 cci_name = "CCI_400";
476 cci_name = "CCI_400-r1";
479 pr_debug("gator: unrecognized cci-400 revision\n");
483 snprintf(uc_attrs[uc_attr_count].name, sizeof(uc_attrs[uc_attr_count].name), "%s_ccnt", cci_name);
484 uc_attrs[uc_attr_count].type = type;
487 for (cnt = 0; cnt < CCI_400; ++cnt, ++uc_attr_count) {
488 struct gator_attr *const attr = &uc_attrs[uc_attr_count];
489 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", cci_name, cnt);
494 static void gator_events_perf_pmu_cpu_init(const struct gator_cpu *const gator_cpu, const int type)
498 snprintf(attrs[attr_count].name, sizeof(attrs[attr_count].name), "%s_ccnt", gator_cpu->pmnc_name);
499 attrs[attr_count].type = type;
502 for (cnt = 0; cnt < gator_cpu->pmnc_counters; ++cnt, ++attr_count) {
503 struct gator_attr *const attr = &attrs[attr_count];
504 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", gator_cpu->pmnc_name, cnt);
509 int gator_events_perf_pmu_init(void)
511 struct perf_event_attr pea;
512 struct perf_event *pe;
513 const struct gator_cpu *gator_cpu;
517 bool found_cpu = false;
519 for (cnt = 0; cnt < CNTMAX; cnt++) {
520 __attr_init(&attrs[cnt]);
522 for (cnt = 0; cnt < UCCNT; cnt++) {
523 __attr_init(&uc_attrs[cnt]);
526 memset(&pea, 0, sizeof(pea));
527 pea.size = sizeof(pea);
531 for (type = PERF_TYPE_MAX; type < 0x20; ++type) {
534 // A particular PMU may work on some but not all cores, so try on each core
536 for_each_present_cpu(cpu) {
537 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
538 pe = perf_event_create_kernel_counter(&pea, cpu, 0, dummy_handler);
540 pe = perf_event_create_kernel_counter(&pea, cpu, 0, dummy_handler, 0);
546 // Assume that valid PMUs are contiguous
551 if (pe->pmu != NULL && type == pe->pmu->type) {
552 if (strcmp("CCI", pe->pmu->name) == 0 || strcmp("CCI_400", pe->pmu->name) == 0 || strcmp("CCI_400-r1", pe->pmu->name) == 0) {
553 gator_events_perf_pmu_cci_init(type);
554 } else if ((gator_cpu = gator_find_cpu_by_pmu_name(pe->pmu->name)) != NULL) {
556 gator_events_perf_pmu_cpu_init(gator_cpu, type);
558 // Initialize gator_attrs for dynamic PMUs here
561 perf_event_release_kernel(pe);
565 const struct gator_cpu *const gator_cpu = gator_find_cpu_by_cpuid(gator_cpuid());
566 if (gator_cpu == NULL) {
569 gator_events_perf_pmu_cpu_init(gator_cpu, PERF_TYPE_RAW);
572 // Initialize gator_attrs for non-dynamic PMUs here
574 if (attr_count > CNTMAX) {
575 printk(KERN_ERR "gator: Too many perf counters\n");
579 if (uc_attr_count > UCCNT) {
580 printk(KERN_ERR "gator: Too many perf uncore counters\n");
584 return gator_events_install(&gator_events_perf_pmu_interface);