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 */
27 /* Maximum number of uncore counters */
28 /* + 1 for the cci-400 cycles counter */
29 /* + 1 for the CCN-5xx cycles counter */
30 #define UCCNT (CCI_400 + 1 + CCN_5XX + 1)
32 /* Default to 0 if unable to probe the revision which was the previous behavior */
33 #define DEFAULT_CCI_REVISION 0
35 /* A gator_attr is needed for every counter */
37 /* Set once in gator_events_perf_pmu_*_init - the name of the event in the gatorfs */
39 /* Exposed in gatorfs - set by gatord to enable this counter */
40 unsigned long enabled;
41 /* Set once in gator_events_perf_pmu_*_init - the perf type to use, see perf_type_id in the perf_event.h header file. */
43 /* Exposed in gatorfs - set by gatord to select the event to collect */
45 /* Exposed in gatorfs - set by gatord with the sample period to use and enable EBS for this counter */
47 /* Exposed as read only in gatorfs - set once in __attr_init as the key to use in the APC data */
51 /* Per-core counter attributes */
52 static struct gator_attr attrs[CNTMAX];
53 /* Number of initialized per-core counters */
54 static int attr_count;
55 /* Uncore counter attributes */
56 static struct gator_attr uc_attrs[UCCNT];
57 /* Number of initialized uncore counters */
58 static int uc_attr_count;
65 struct perf_event *pevent;
66 struct perf_event_attr *pevent_attr;
69 static DEFINE_PER_CPU(struct gator_event[CNTMAX], events);
70 static struct gator_event uc_events[UCCNT];
71 static DEFINE_PER_CPU(int[(CNTMAX + UCCNT)*2], perf_cnt);
73 static void gator_events_perf_pmu_stop(void);
75 static int __create_files(struct super_block *sb, struct dentry *root, struct gator_attr *const attr)
79 if (attr->name[0] == '\0')
81 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)
101 for (cnt = 0; cnt < uc_attr_count; cnt++) {
102 if (__create_files(sb, root, &uc_attrs[cnt]) != 0)
109 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
110 static void ebs_overflow_handler(struct perf_event *event, int unused, struct perf_sample_data *data, struct pt_regs *regs)
112 static void ebs_overflow_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
115 gator_backtrace_handler(regs);
118 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
119 static void dummy_handler(struct perf_event *event, int unused, struct perf_sample_data *data, struct pt_regs *regs)
121 static void dummy_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
124 /* Required as perf_event_create_kernel_counter() requires an overflow handler, even though all we do is poll */
127 static int gator_events_perf_pmu_read(int **buffer, bool sched_switch);
129 static int gator_events_perf_pmu_online(int **buffer, bool migrate)
131 return gator_events_perf_pmu_read(buffer, false);
134 static void __online_dispatch(int cpu, bool migrate, struct gator_attr *const attr, struct gator_event *const event)
136 perf_overflow_handler_t handler;
140 if (event->pevent != NULL || event->pevent_attr == 0 || migrate)
144 handler = ebs_overflow_handler;
146 handler = dummy_handler;
148 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
149 event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler);
151 event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler, 0);
153 if (IS_ERR(event->pevent)) {
154 pr_debug("gator: unable to online a counter on cpu %d\n", cpu);
155 event->pevent = NULL;
159 if (event->pevent->state != PERF_EVENT_STATE_ACTIVE) {
160 pr_debug("gator: inactive counter on cpu %d\n", cpu);
161 perf_event_release_kernel(event->pevent);
162 event->pevent = NULL;
167 static void gator_events_perf_pmu_online_dispatch(int cpu, bool migrate)
171 cpu = pcpu_to_lcpu(cpu);
173 for (cnt = 0; cnt < attr_count; cnt++)
174 __online_dispatch(cpu, migrate, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
177 for (cnt = 0; cnt < uc_attr_count; cnt++)
178 __online_dispatch(cpu, migrate, &uc_attrs[cnt], &uc_events[cnt]);
182 static void __offline_dispatch(int cpu, struct gator_event *const event)
184 struct perf_event *pe = NULL;
188 event->pevent = NULL;
192 perf_event_release_kernel(pe);
195 static void gator_events_perf_pmu_offline_dispatch(int cpu, bool migrate)
201 cpu = pcpu_to_lcpu(cpu);
203 for (cnt = 0; cnt < attr_count; cnt++)
204 __offline_dispatch(cpu, &per_cpu(events, cpu)[cnt]);
207 for (cnt = 0; cnt < uc_attr_count; cnt++)
208 __offline_dispatch(cpu, &uc_events[cnt]);
212 static int __check_ebs(struct gator_attr *const attr)
214 if (attr->count > 0) {
215 if (!event_based_sampling) {
216 event_based_sampling = true;
218 pr_warning("gator: Only one ebs counter is allowed\n");
226 static int __start(struct gator_attr *const attr, struct gator_event *const event)
228 u32 size = sizeof(struct perf_event_attr);
230 event->pevent = NULL;
231 /* Skip disabled counters */
237 event->prev_delta = 0;
238 event->pevent_attr = kmalloc(size, GFP_KERNEL);
239 if (!event->pevent_attr) {
240 gator_events_perf_pmu_stop();
244 memset(event->pevent_attr, 0, size);
245 event->pevent_attr->type = attr->type;
246 event->pevent_attr->size = size;
247 event->pevent_attr->config = attr->event;
248 event->pevent_attr->sample_period = attr->count;
249 event->pevent_attr->pinned = 1;
254 static int gator_events_perf_pmu_start(void)
258 event_based_sampling = false;
259 for (cnt = 0; cnt < attr_count; cnt++) {
260 if (__check_ebs(&attrs[cnt]) != 0)
264 for (cnt = 0; cnt < uc_attr_count; cnt++) {
265 if (__check_ebs(&uc_attrs[cnt]) != 0)
269 for_each_present_cpu(cpu) {
270 for (cnt = 0; cnt < attr_count; cnt++) {
271 if (__start(&attrs[cnt], &per_cpu(events, cpu)[cnt]) != 0)
276 for (cnt = 0; cnt < uc_attr_count; cnt++) {
277 if (__start(&uc_attrs[cnt], &uc_events[cnt]) != 0)
284 static void __event_stop(struct gator_event *const event)
286 kfree(event->pevent_attr);
287 event->pevent_attr = NULL;
290 static void __attr_stop(struct gator_attr *const attr)
297 static void gator_events_perf_pmu_stop(void)
299 unsigned int cnt, cpu;
301 for_each_present_cpu(cpu) {
302 for (cnt = 0; cnt < attr_count; cnt++)
303 __event_stop(&per_cpu(events, cpu)[cnt]);
306 for (cnt = 0; cnt < uc_attr_count; cnt++)
307 __event_stop(&uc_events[cnt]);
309 for (cnt = 0; cnt < attr_count; cnt++)
310 __attr_stop(&attrs[cnt]);
312 for (cnt = 0; cnt < uc_attr_count; cnt++)
313 __attr_stop(&uc_attrs[cnt]);
316 static void __read(int *const len, int cpu, struct gator_attr *const attr, struct gator_event *const event)
319 struct perf_event *const ev = event->pevent;
321 if (ev != NULL && ev->state == PERF_EVENT_STATE_ACTIVE) {
322 /* After creating the perf counter in __online_dispatch, there
323 * is a race condition between gator_events_perf_pmu_online and
324 * gator_events_perf_pmu_read. So have
325 * gator_events_perf_pmu_online call gator_events_perf_pmu_read
326 * and in __read check to see if it's the first call after
327 * __online_dispatch and if so, run the online code.
331 event->prev = event->curr = local64_read(&ev->count);
332 event->prev_delta = 0;
333 per_cpu(perf_cnt, cpu)[(*len)++] = attr->key;
334 per_cpu(perf_cnt, cpu)[(*len)++] = 0;
338 event->curr = local64_read(&ev->count);
339 delta = event->curr - event->prev;
340 if (delta != 0 || delta != event->prev_delta) {
341 event->prev_delta = delta;
342 event->prev = event->curr;
343 per_cpu(perf_cnt, cpu)[(*len)++] = attr->key;
346 per_cpu(perf_cnt, cpu)[(*len)++] = delta;
352 static int gator_events_perf_pmu_read(int **buffer, bool sched_switch)
355 const int cpu = get_logical_cpu();
357 for (cnt = 0; cnt < attr_count; cnt++)
358 __read(&len, cpu, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
361 for (cnt = 0; cnt < uc_attr_count; cnt++)
362 __read(&len, cpu, &uc_attrs[cnt], &uc_events[cnt]);
366 *buffer = per_cpu(perf_cnt, cpu);
371 static struct gator_interface gator_events_perf_pmu_interface = {
372 .create_files = gator_events_perf_pmu_create_files,
373 .start = gator_events_perf_pmu_start,
374 .stop = gator_events_perf_pmu_stop,
375 .online = gator_events_perf_pmu_online,
376 .online_dispatch = gator_events_perf_pmu_online_dispatch,
377 .offline_dispatch = gator_events_perf_pmu_offline_dispatch,
378 .read = gator_events_perf_pmu_read,
381 static void __attr_init(struct gator_attr *const attr)
383 attr->name[0] = '\0';
388 attr->key = gator_events_get_key();
393 static const struct of_device_id arm_cci_matches[] = {
394 {.compatible = "arm,cci-400" },
398 static int probe_cci_revision(void)
400 struct device_node *np;
402 void __iomem *cci_ctrl_base;
404 int ret = DEFAULT_CCI_REVISION;
406 np = of_find_matching_node(NULL, arm_cci_matches);
410 if (of_address_to_resource(np, 0, &res))
413 cci_ctrl_base = ioremap(res.start, resource_size(&res));
415 rev = (readl_relaxed(cci_ctrl_base + 0xfe8) >> 4) & 0xf;
422 iounmap(cci_ctrl_base);
432 static int probe_cci_revision(void)
434 return DEFAULT_CCI_REVISION;
439 static void gator_events_perf_pmu_uncore_init(const char *const name, const int type, const int count)
443 snprintf(uc_attrs[uc_attr_count].name, sizeof(uc_attrs[uc_attr_count].name), "%s_ccnt", name);
444 uc_attrs[uc_attr_count].type = type;
447 for (cnt = 0; cnt < count; ++cnt, ++uc_attr_count) {
448 struct gator_attr *const attr = &uc_attrs[uc_attr_count];
450 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", name, cnt);
455 static void gator_events_perf_pmu_cci_init(const int type)
457 const char *cci_name;
459 switch (probe_cci_revision()) {
461 cci_name = "CCI_400";
464 cci_name = "CCI_400-r1";
467 pr_debug("gator: unrecognized cci-400 revision\n");
471 gator_events_perf_pmu_uncore_init(cci_name, type, CCI_400);
474 static void gator_events_perf_pmu_cpu_init(const struct gator_cpu *const gator_cpu, const int type)
478 snprintf(attrs[attr_count].name, sizeof(attrs[attr_count].name), "%s_ccnt", gator_cpu->pmnc_name);
479 attrs[attr_count].type = type;
482 for (cnt = 0; cnt < gator_cpu->pmnc_counters; ++cnt, ++attr_count) {
483 struct gator_attr *const attr = &attrs[attr_count];
485 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", gator_cpu->pmnc_name, cnt);
490 int gator_events_perf_pmu_init(void)
492 struct perf_event_attr pea;
493 struct perf_event *pe;
494 const struct gator_cpu *gator_cpu;
498 bool found_cpu = false;
500 for (cnt = 0; cnt < CNTMAX; cnt++)
501 __attr_init(&attrs[cnt]);
502 for (cnt = 0; cnt < UCCNT; cnt++)
503 __attr_init(&uc_attrs[cnt]);
505 memset(&pea, 0, sizeof(pea));
506 pea.size = sizeof(pea);
510 for (type = PERF_TYPE_MAX; type < 0x20; ++type) {
513 /* A particular PMU may work on some but not all cores, so try on each core */
515 for_each_present_cpu(cpu) {
516 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
517 pe = perf_event_create_kernel_counter(&pea, cpu, 0, dummy_handler);
519 pe = perf_event_create_kernel_counter(&pea, cpu, 0, dummy_handler, 0);
524 /* Assume that valid PMUs are contiguous */
527 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
528 pe = perf_event_create_kernel_counter(&pea, 0, 0, dummy_handler);
530 pe = perf_event_create_kernel_counter(&pea, 0, 0, dummy_handler, 0);
536 if (pe->pmu != NULL && type == pe->pmu->type) {
537 if (strcmp("CCI", pe->pmu->name) == 0 || strcmp("CCI_400", pe->pmu->name) == 0 || strcmp("CCI_400-r1", pe->pmu->name) == 0) {
538 gator_events_perf_pmu_cci_init(type);
539 } else if (strcmp("ccn", pe->pmu->name) == 0) {
540 gator_events_perf_pmu_uncore_init("ARM_CCN_5XX", type, CCN_5XX);
541 } else if ((gator_cpu = gator_find_cpu_by_pmu_name(pe->pmu->name)) != NULL) {
543 gator_events_perf_pmu_cpu_init(gator_cpu, type);
545 /* Initialize gator_attrs for dynamic PMUs here */
548 perf_event_release_kernel(pe);
552 const struct gator_cpu *const gator_cpu = gator_find_cpu_by_cpuid(gator_cpuid());
554 if (gator_cpu == NULL)
556 gator_events_perf_pmu_cpu_init(gator_cpu, PERF_TYPE_RAW);
559 /* Initialize gator_attrs for non-dynamic PMUs here */
561 if (attr_count > CNTMAX) {
562 pr_err("gator: Too many perf counters\n");
566 if (uc_attr_count > UCCNT) {
567 pr_err("gator: Too many perf uncore counters\n");
571 return gator_events_install(&gator_events_perf_pmu_interface);