Merge remote-tracking branch 'lsk/v3.10/topic/clk-divider' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_perf_pmu.c
1 /**
2  * Copyright (C) ARM Limited 2010-2014. All rights reserved.
3  *
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.
7  */
8
9 #include "gator.h"
10
11 /* gator_events_armvX.c is used for Linux 2.6.x */
12 #if GATOR_PERF_PMU_SUPPORT
13
14 #include <linux/io.h>
15 #ifdef CONFIG_OF
16 #include <linux/of_address.h>
17 #endif
18 #include <linux/perf_event.h>
19 #include <linux/slab.h>
20
21 extern bool event_based_sampling;
22
23 /* Maximum number of per-core counters - currently reserves enough space for two full hardware PMUs for big.LITTLE */
24 #define CNTMAX 16
25 #define CCI_400 4
26 #define CCN_5XX 8
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)
31
32 /* Default to 0 if unable to probe the revision which was the previous behavior */
33 #define DEFAULT_CCI_REVISION 0
34
35 /* A gator_attr is needed for every counter */
36 struct gator_attr {
37         /* Set once in gator_events_perf_pmu_*_init - the name of the event in the gatorfs */
38         char name[40];
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. */
42         unsigned long type;
43         /* Exposed in gatorfs - set by gatord to select the event to collect */
44         unsigned long event;
45         /* Exposed in gatorfs - set by gatord with the sample period to use and enable EBS for this counter */
46         unsigned long count;
47         /* Exposed as read only in gatorfs - set once in __attr_init as the key to use in the APC data */
48         unsigned long key;
49 };
50
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;
59
60 struct gator_event {
61         int curr;
62         int prev;
63         int prev_delta;
64         bool zero;
65         struct perf_event *pevent;
66         struct perf_event_attr *pevent_attr;
67 };
68
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);
72
73 static void gator_events_perf_pmu_stop(void);
74
75 static int __create_files(struct super_block *sb, struct dentry *root, struct gator_attr *const attr)
76 {
77         struct dentry *dir;
78
79         if (attr->name[0] == '\0')
80                 return 0;
81         dir = gatorfs_mkdir(sb, root, attr->name);
82         if (!dir)
83                 return -1;
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);
88
89         return 0;
90 }
91
92 static int gator_events_perf_pmu_create_files(struct super_block *sb, struct dentry *root)
93 {
94         int cnt;
95
96         for (cnt = 0; cnt < attr_count; cnt++) {
97                 if (__create_files(sb, root, &attrs[cnt]) != 0)
98                         return -1;
99         }
100
101         for (cnt = 0; cnt < uc_attr_count; cnt++) {
102                 if (__create_files(sb, root, &uc_attrs[cnt]) != 0)
103                         return -1;
104         }
105
106         return 0;
107 }
108
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)
111 #else
112 static void ebs_overflow_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
113 #endif
114 {
115         gator_backtrace_handler(regs);
116 }
117
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)
120 #else
121 static void dummy_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
122 #endif
123 {
124         /* Required as perf_event_create_kernel_counter() requires an overflow handler, even though all we do is poll */
125 }
126
127 static int gator_events_perf_pmu_read(int **buffer, bool sched_switch);
128
129 static int gator_events_perf_pmu_online(int **buffer, bool migrate)
130 {
131         return gator_events_perf_pmu_read(buffer, false);
132 }
133
134 static void __online_dispatch(int cpu, bool migrate, struct gator_attr *const attr, struct gator_event *const event)
135 {
136         perf_overflow_handler_t handler;
137
138         event->zero = true;
139
140         if (event->pevent != NULL || event->pevent_attr == 0 || migrate)
141                 return;
142
143         if (attr->count > 0)
144                 handler = ebs_overflow_handler;
145         else
146                 handler = dummy_handler;
147
148 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
149         event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler);
150 #else
151         event->pevent = perf_event_create_kernel_counter(event->pevent_attr, cpu, 0, handler, 0);
152 #endif
153         if (IS_ERR(event->pevent)) {
154                 pr_debug("gator: unable to online a counter on cpu %d\n", cpu);
155                 event->pevent = NULL;
156                 return;
157         }
158
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;
163                 return;
164         }
165 }
166
167 static void gator_events_perf_pmu_online_dispatch(int cpu, bool migrate)
168 {
169         int cnt;
170
171         cpu = pcpu_to_lcpu(cpu);
172
173         for (cnt = 0; cnt < attr_count; cnt++)
174                 __online_dispatch(cpu, migrate, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
175
176         if (cpu == 0) {
177                 for (cnt = 0; cnt < uc_attr_count; cnt++)
178                         __online_dispatch(cpu, migrate, &uc_attrs[cnt], &uc_events[cnt]);
179         }
180 }
181
182 static void __offline_dispatch(int cpu, struct gator_event *const event)
183 {
184         struct perf_event *pe = NULL;
185
186         if (event->pevent) {
187                 pe = event->pevent;
188                 event->pevent = NULL;
189         }
190
191         if (pe)
192                 perf_event_release_kernel(pe);
193 }
194
195 static void gator_events_perf_pmu_offline_dispatch(int cpu, bool migrate)
196 {
197         int cnt;
198
199         if (migrate)
200                 return;
201         cpu = pcpu_to_lcpu(cpu);
202
203         for (cnt = 0; cnt < attr_count; cnt++)
204                 __offline_dispatch(cpu, &per_cpu(events, cpu)[cnt]);
205
206         if (cpu == 0) {
207                 for (cnt = 0; cnt < uc_attr_count; cnt++)
208                         __offline_dispatch(cpu, &uc_events[cnt]);
209         }
210 }
211
212 static int __check_ebs(struct gator_attr *const attr)
213 {
214         if (attr->count > 0) {
215                 if (!event_based_sampling) {
216                         event_based_sampling = true;
217                 } else {
218                         pr_warning("gator: Only one ebs counter is allowed\n");
219                         return -1;
220                 }
221         }
222
223         return 0;
224 }
225
226 static int __start(struct gator_attr *const attr, struct gator_event *const event)
227 {
228         u32 size = sizeof(struct perf_event_attr);
229
230         event->pevent = NULL;
231         /* Skip disabled counters */
232         if (!attr->enabled)
233                 return 0;
234
235         event->prev = 0;
236         event->curr = 0;
237         event->prev_delta = 0;
238         event->pevent_attr = kmalloc(size, GFP_KERNEL);
239         if (!event->pevent_attr) {
240                 gator_events_perf_pmu_stop();
241                 return -1;
242         }
243
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;
250
251         return 0;
252 }
253
254 static int gator_events_perf_pmu_start(void)
255 {
256         int cnt, cpu;
257
258         event_based_sampling = false;
259         for (cnt = 0; cnt < attr_count; cnt++) {
260                 if (__check_ebs(&attrs[cnt]) != 0)
261                         return -1;
262         }
263
264         for (cnt = 0; cnt < uc_attr_count; cnt++) {
265                 if (__check_ebs(&uc_attrs[cnt]) != 0)
266                         return -1;
267         }
268
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)
272                                 return -1;
273                 }
274         }
275
276         for (cnt = 0; cnt < uc_attr_count; cnt++) {
277                 if (__start(&uc_attrs[cnt], &uc_events[cnt]) != 0)
278                         return -1;
279         }
280
281         return 0;
282 }
283
284 static void __event_stop(struct gator_event *const event)
285 {
286         kfree(event->pevent_attr);
287         event->pevent_attr = NULL;
288 }
289
290 static void __attr_stop(struct gator_attr *const attr)
291 {
292         attr->enabled = 0;
293         attr->event = 0;
294         attr->count = 0;
295 }
296
297 static void gator_events_perf_pmu_stop(void)
298 {
299         unsigned int cnt, cpu;
300
301         for_each_present_cpu(cpu) {
302                 for (cnt = 0; cnt < attr_count; cnt++)
303                         __event_stop(&per_cpu(events, cpu)[cnt]);
304         }
305
306         for (cnt = 0; cnt < uc_attr_count; cnt++)
307                 __event_stop(&uc_events[cnt]);
308
309         for (cnt = 0; cnt < attr_count; cnt++)
310                 __attr_stop(&attrs[cnt]);
311
312         for (cnt = 0; cnt < uc_attr_count; cnt++)
313                 __attr_stop(&uc_attrs[cnt]);
314 }
315
316 static void __read(int *const len, int cpu, struct gator_attr *const attr, struct gator_event *const event)
317 {
318         int delta;
319         struct perf_event *const ev = event->pevent;
320
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.
328                  */
329                 if (event->zero) {
330                         ev->pmu->read(ev);
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;
335                         event->zero = false;
336                 } else {
337                         ev->pmu->read(ev);
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;
344                                 if (delta < 0)
345                                         delta *= -1;
346                                 per_cpu(perf_cnt, cpu)[(*len)++] = delta;
347                         }
348                 }
349         }
350 }
351
352 static int gator_events_perf_pmu_read(int **buffer, bool sched_switch)
353 {
354         int cnt, len = 0;
355         const int cpu = get_logical_cpu();
356
357         for (cnt = 0; cnt < attr_count; cnt++)
358                 __read(&len, cpu, &attrs[cnt], &per_cpu(events, cpu)[cnt]);
359
360         if (cpu == 0) {
361                 for (cnt = 0; cnt < uc_attr_count; cnt++)
362                         __read(&len, cpu, &uc_attrs[cnt], &uc_events[cnt]);
363         }
364
365         if (buffer)
366                 *buffer = per_cpu(perf_cnt, cpu);
367
368         return len;
369 }
370
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,
379 };
380
381 static void __attr_init(struct gator_attr *const attr)
382 {
383         attr->name[0] = '\0';
384         attr->enabled = 0;
385         attr->type = 0;
386         attr->event = 0;
387         attr->count = 0;
388         attr->key = gator_events_get_key();
389 }
390
391 #ifdef CONFIG_OF
392
393 static const struct of_device_id arm_cci_matches[] = {
394         {.compatible = "arm,cci-400" },
395         {},
396 };
397
398 static int probe_cci_revision(void)
399 {
400         struct device_node *np;
401         struct resource res;
402         void __iomem *cci_ctrl_base;
403         int rev;
404         int ret = DEFAULT_CCI_REVISION;
405
406         np = of_find_matching_node(NULL, arm_cci_matches);
407         if (!np)
408                 return ret;
409
410         if (of_address_to_resource(np, 0, &res))
411                 goto node_put;
412
413         cci_ctrl_base = ioremap(res.start, resource_size(&res));
414
415         rev = (readl_relaxed(cci_ctrl_base + 0xfe8) >> 4) & 0xf;
416
417         if (rev <= 4)
418                 ret = 0;
419         else if (rev <= 6)
420                 ret = 1;
421
422         iounmap(cci_ctrl_base);
423
424  node_put:
425         of_node_put(np);
426
427         return ret;
428 }
429
430 #else
431
432 static int probe_cci_revision(void)
433 {
434         return DEFAULT_CCI_REVISION;
435 }
436
437 #endif
438
439 static void gator_events_perf_pmu_uncore_init(const char *const name, const int type, const int count)
440 {
441         int cnt;
442
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;
445         ++uc_attr_count;
446
447         for (cnt = 0; cnt < count; ++cnt, ++uc_attr_count) {
448                 struct gator_attr *const attr = &uc_attrs[uc_attr_count];
449
450                 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", name, cnt);
451                 attr->type = type;
452         }
453 }
454
455 static void gator_events_perf_pmu_cci_init(const int type)
456 {
457         const char *cci_name;
458
459         switch (probe_cci_revision()) {
460         case 0:
461                 cci_name = "CCI_400";
462                 break;
463         case 1:
464                 cci_name = "CCI_400-r1";
465                 break;
466         default:
467                 pr_debug("gator: unrecognized cci-400 revision\n");
468                 return;
469         }
470
471         gator_events_perf_pmu_uncore_init(cci_name, type, CCI_400);
472 }
473
474 static void gator_events_perf_pmu_cpu_init(const struct gator_cpu *const gator_cpu, const int type)
475 {
476         int cnt;
477
478         snprintf(attrs[attr_count].name, sizeof(attrs[attr_count].name), "%s_ccnt", gator_cpu->pmnc_name);
479         attrs[attr_count].type = type;
480         ++attr_count;
481
482         for (cnt = 0; cnt < gator_cpu->pmnc_counters; ++cnt, ++attr_count) {
483                 struct gator_attr *const attr = &attrs[attr_count];
484
485                 snprintf(attr->name, sizeof(attr->name), "%s_cnt%d", gator_cpu->pmnc_name, cnt);
486                 attr->type = type;
487         }
488 }
489
490 int gator_events_perf_pmu_init(void)
491 {
492         struct perf_event_attr pea;
493         struct perf_event *pe;
494         const struct gator_cpu *gator_cpu;
495         int type;
496         int cpu;
497         int cnt;
498         bool found_cpu = false;
499
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]);
504
505         memset(&pea, 0, sizeof(pea));
506         pea.size = sizeof(pea);
507         pea.config = 0xFF;
508         attr_count = 0;
509         uc_attr_count = 0;
510         for (type = PERF_TYPE_MAX; type < 0x20; ++type) {
511                 pea.type = type;
512
513                 /* A particular PMU may work on some but not all cores, so try on each core */
514                 pe = NULL;
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);
518 #else
519                         pe = perf_event_create_kernel_counter(&pea, cpu, 0, dummy_handler, 0);
520 #endif
521                         if (!IS_ERR(pe))
522                                 break;
523                 }
524                 /* Assume that valid PMUs are contiguous */
525                 if (IS_ERR(pe)) {
526                         pea.config = 0xff00;
527 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
528                         pe = perf_event_create_kernel_counter(&pea, 0, 0, dummy_handler);
529 #else
530                         pe = perf_event_create_kernel_counter(&pea, 0, 0, dummy_handler, 0);
531 #endif
532                         if (IS_ERR(pe))
533                                 break;
534                 }
535
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) {
542                                 found_cpu = true;
543                                 gator_events_perf_pmu_cpu_init(gator_cpu, type);
544                         }
545                         /* Initialize gator_attrs for dynamic PMUs here */
546                 }
547
548                 perf_event_release_kernel(pe);
549         }
550
551         if (!found_cpu) {
552                 const struct gator_cpu *const gator_cpu = gator_find_cpu_by_cpuid(gator_cpuid());
553
554                 if (gator_cpu == NULL)
555                         return -1;
556                 gator_events_perf_pmu_cpu_init(gator_cpu, PERF_TYPE_RAW);
557         }
558
559         /* Initialize gator_attrs for non-dynamic PMUs here */
560
561         if (attr_count > CNTMAX) {
562                 pr_err("gator: Too many perf counters\n");
563                 return -1;
564         }
565
566         if (uc_attr_count > UCCNT) {
567                 pr_err("gator: Too many perf uncore counters\n");
568                 return -1;
569         }
570
571         return gator_events_install(&gator_events_perf_pmu_interface);
572 }
573
574 #endif