gator: Version 5.19
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_mali_t6xx.c
1 /**
2  * Copyright (C) ARM Limited 2011-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
10 #include "gator.h"
11
12 #include <linux/module.h>
13 #include <linux/time.h>
14 #include <linux/math64.h>
15 #include <linux/slab.h>
16 #include <asm/io.h>
17
18 #ifdef MALI_DIR_MIDGARD
19 /* New DDK Directory structure with kernel/drivers/gpu/arm/midgard*/
20 #include "mali_linux_trace.h"
21 #else
22 /* Old DDK Directory structure with kernel/drivers/gpu/arm/t6xx*/
23 #include "linux/mali_linux_trace.h"
24 #endif
25
26 #include "gator_events_mali_common.h"
27
28 /*
29  * Check that the MALI_SUPPORT define is set to one of the allowable device codes.
30  */
31 #if (MALI_SUPPORT != MALI_T6xx)
32 #error MALI_SUPPORT set to an invalid device code: expecting MALI_T6xx
33 #endif
34
35 static const char mali_name[] = "Mali-T6xx";
36
37 /* Counters for Mali-T6xx:
38  *
39  *  - Timeline events
40  *    They are tracepoints, but instead of reporting a number they report a START/STOP event.
41  *    They are reported in Streamline as number of microseconds while that particular counter was active.
42  *
43  *  - SW counters
44  *    They are tracepoints reporting a particular number.
45  *    They are accumulated in sw_counter_data array until they are passed to Streamline, then they are zeroed.
46  *
47  *  - Accumulators
48  *    They are the same as software counters but their value is not zeroed.
49  */
50
51 /* Timeline (start/stop) activity */
52 static const char *timeline_event_names[] = {
53         "PM_SHADER_0",
54         "PM_SHADER_1",
55         "PM_SHADER_2",
56         "PM_SHADER_3",
57         "PM_SHADER_4",
58         "PM_SHADER_5",
59         "PM_SHADER_6",
60         "PM_SHADER_7",
61         "PM_TILER_0",
62         "PM_L2_0",
63         "PM_L2_1",
64         "MMU_AS_0",
65         "MMU_AS_1",
66         "MMU_AS_2",
67         "MMU_AS_3"
68 };
69
70 enum {
71         PM_SHADER_0 = 0,
72         PM_SHADER_1,
73         PM_SHADER_2,
74         PM_SHADER_3,
75         PM_SHADER_4,
76         PM_SHADER_5,
77         PM_SHADER_6,
78         PM_SHADER_7,
79         PM_TILER_0,
80         PM_L2_0,
81         PM_L2_1,
82         MMU_AS_0,
83         MMU_AS_1,
84         MMU_AS_2,
85         MMU_AS_3
86 };
87 /* The number of shader blocks in the enum above */
88 #define NUM_PM_SHADER (8)
89
90 /* Software Counters */
91 static const char *software_counter_names[] = {
92         "MMU_PAGE_FAULT_0",
93         "MMU_PAGE_FAULT_1",
94         "MMU_PAGE_FAULT_2",
95         "MMU_PAGE_FAULT_3"
96 };
97
98 enum {
99         MMU_PAGE_FAULT_0 = 0,
100         MMU_PAGE_FAULT_1,
101         MMU_PAGE_FAULT_2,
102         MMU_PAGE_FAULT_3
103 };
104
105 /* Software Counters */
106 static const char *accumulators_names[] = {
107         "TOTAL_ALLOC_PAGES"
108 };
109
110 enum {
111         TOTAL_ALLOC_PAGES = 0
112 };
113
114 #define FIRST_TIMELINE_EVENT (0)
115 #define NUMBER_OF_TIMELINE_EVENTS (sizeof(timeline_event_names) / sizeof(timeline_event_names[0]))
116 #define FIRST_SOFTWARE_COUNTER (FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS)
117 #define NUMBER_OF_SOFTWARE_COUNTERS (sizeof(software_counter_names) / sizeof(software_counter_names[0]))
118 #define FIRST_ACCUMULATOR (FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS)
119 #define NUMBER_OF_ACCUMULATORS (sizeof(accumulators_names) / sizeof(accumulators_names[0]))
120 #define FILMSTRIP (NUMBER_OF_TIMELINE_EVENTS + NUMBER_OF_SOFTWARE_COUNTERS + NUMBER_OF_ACCUMULATORS)
121 #define NUMBER_OF_EVENTS (NUMBER_OF_TIMELINE_EVENTS + NUMBER_OF_SOFTWARE_COUNTERS + NUMBER_OF_ACCUMULATORS + 1)
122
123 /*
124  * gatorfs variables for counter enable state
125  */
126 static mali_counter counters[NUMBER_OF_EVENTS];
127 static unsigned long filmstrip_event;
128
129 /* An array used to return the data we recorded
130  * as key,value pairs hence the *2
131  */
132 static unsigned long counter_dump[NUMBER_OF_EVENTS * 2];
133
134 /*
135  * Array holding counter start times (in ns) for each counter.  A zero here
136  * indicates that the activity monitored by this counter is not running.
137  */
138 static struct timespec timeline_event_starttime[NUMBER_OF_TIMELINE_EVENTS];
139
140 /* The data we have recorded */
141 static unsigned int timeline_data[NUMBER_OF_TIMELINE_EVENTS];
142 static unsigned int sw_counter_data[NUMBER_OF_SOFTWARE_COUNTERS];
143 static unsigned int accumulators_data[NUMBER_OF_ACCUMULATORS];
144
145 /* Hold the previous timestamp, used to calculate the sample interval. */
146 static struct timespec prev_timestamp;
147
148 /**
149  * Returns the timespan (in microseconds) between the two specified timestamps.
150  *
151  * @param start Ptr to the start timestamp
152  * @param end Ptr to the end timestamp
153  *
154  * @return Number of microseconds between the two timestamps (can be negative if start follows end).
155  */
156 static inline long get_duration_us(const struct timespec *start, const struct timespec *end)
157 {
158         long event_duration_us = (end->tv_nsec - start->tv_nsec) / 1000;
159         event_duration_us += (end->tv_sec - start->tv_sec) * 1000000;
160
161         return event_duration_us;
162 }
163
164 static void record_timeline_event(unsigned int timeline_index, unsigned int type)
165 {
166         struct timespec event_timestamp;
167         struct timespec *event_start = &timeline_event_starttime[timeline_index];
168
169         switch (type) {
170         case ACTIVITY_START:
171                 /* Get the event time... */
172                 getnstimeofday(&event_timestamp);
173
174                 /* Remember the start time if the activity is not already started */
175                 if (event_start->tv_sec == 0) {
176                         *event_start = event_timestamp; /* Structure copy */
177                 }
178                 break;
179
180         case ACTIVITY_STOP:
181                 /* if the counter was started... */
182                 if (event_start->tv_sec != 0) {
183                         /* Get the event time... */
184                         getnstimeofday(&event_timestamp);
185
186                         /* Accumulate the duration in us */
187                         timeline_data[timeline_index] += get_duration_us(event_start, &event_timestamp);
188
189                         /* Reset the start time to indicate the activity is stopped. */
190                         event_start->tv_sec = 0;
191                 }
192                 break;
193
194         default:
195                 /* Other activity events are ignored. */
196                 break;
197         }
198 }
199
200 /*
201  * Documentation about the following tracepoints is in mali_linux_trace.h
202  */
203
204 GATOR_DEFINE_PROBE(mali_pm_status, TP_PROTO(unsigned int event_id, unsigned long long value))
205 {
206 #define SHADER_PRESENT_LO       0x100   /* (RO) Shader core present bitmap, low word */
207 #define TILER_PRESENT_LO        0x110   /* (RO) Tiler core present bitmap, low word */
208 #define L2_PRESENT_LO           0x120   /* (RO) Level 2 cache present bitmap, low word */
209 #define BIT_AT(value, pos) ((value >> pos) & 1)
210
211         static unsigned long long previous_shader_bitmask = 0;
212         static unsigned long long previous_tiler_bitmask = 0;
213         static unsigned long long previous_l2_bitmask = 0;
214
215         switch (event_id) {
216         case SHADER_PRESENT_LO:
217                 {
218                         unsigned long long changed_bitmask = previous_shader_bitmask ^ value;
219                         int pos;
220
221                         for (pos = 0; pos < NUM_PM_SHADER; ++pos) {
222                                 if (BIT_AT(changed_bitmask, pos)) {
223                                         record_timeline_event(PM_SHADER_0 + pos, BIT_AT(value, pos) ? ACTIVITY_START : ACTIVITY_STOP);
224                                 }
225                         }
226
227                         previous_shader_bitmask = value;
228                         break;
229                 }
230
231         case TILER_PRESENT_LO:
232                 {
233                         unsigned long long changed = previous_tiler_bitmask ^ value;
234
235                         if (BIT_AT(changed, 0)) {
236                                 record_timeline_event(PM_TILER_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
237                         }
238
239                         previous_tiler_bitmask = value;
240                         break;
241                 }
242
243         case L2_PRESENT_LO:
244                 {
245                         unsigned long long changed = previous_l2_bitmask ^ value;
246
247                         if (BIT_AT(changed, 0)) {
248                                 record_timeline_event(PM_L2_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
249                         }
250                         if (BIT_AT(changed, 4)) {
251                                 record_timeline_event(PM_L2_1, BIT_AT(value, 4) ? ACTIVITY_START : ACTIVITY_STOP);
252                         }
253
254                         previous_l2_bitmask = value;
255                         break;
256                 }
257
258         default:
259                 /* No other blocks are supported at present */
260                 break;
261         }
262
263 #undef SHADER_PRESENT_LO
264 #undef TILER_PRESENT_LO
265 #undef L2_PRESENT_LO
266 #undef BIT_AT
267 }
268
269 GATOR_DEFINE_PROBE(mali_page_fault_insert_pages, TP_PROTO(int event_id, unsigned long value))
270 {
271         /* We add to the previous since we may receive many tracepoints in one sample period */
272         sw_counter_data[MMU_PAGE_FAULT_0 + event_id] += value;
273 }
274
275 GATOR_DEFINE_PROBE(mali_mmu_as_in_use, TP_PROTO(int event_id))
276 {
277         record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_START);
278 }
279
280 GATOR_DEFINE_PROBE(mali_mmu_as_released, TP_PROTO(int event_id))
281 {
282         record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_STOP);
283 }
284
285 GATOR_DEFINE_PROBE(mali_total_alloc_pages_change, TP_PROTO(long long int event_id))
286 {
287         accumulators_data[TOTAL_ALLOC_PAGES] = event_id;
288 }
289
290 static int create_files(struct super_block *sb, struct dentry *root)
291 {
292         int event;
293         /*
294          * Create the filesystem for all events
295          */
296         int counter_index = 0;
297         mali_profiling_control_type *mali_control;
298
299         for (event = FIRST_TIMELINE_EVENT; event < FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS; event++) {
300                 if (gator_mali_create_file_system(mali_name, timeline_event_names[counter_index], sb, root, &counters[event], NULL) != 0) {
301                         return -1;
302                 }
303                 counter_index++;
304         }
305         counter_index = 0;
306         for (event = FIRST_SOFTWARE_COUNTER; event < FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS; event++) {
307                 if (gator_mali_create_file_system(mali_name, software_counter_names[counter_index], sb, root, &counters[event], NULL) != 0) {
308                         return -1;
309                 }
310                 counter_index++;
311         }
312         counter_index = 0;
313         for (event = FIRST_ACCUMULATOR; event < FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS; event++) {
314                 if (gator_mali_create_file_system(mali_name, accumulators_names[counter_index], sb, root, &counters[event], NULL) != 0) {
315                         return -1;
316                 }
317                 counter_index++;
318         }
319
320         mali_control = symbol_get(_mali_profiling_control);
321         if (mali_control) {
322                 if (gator_mali_create_file_system(mali_name, "Filmstrip_cnt0", sb, root, &counters[FILMSTRIP], &filmstrip_event) != 0) {
323                         return -1;
324                 }
325                 symbol_put(_mali_profiling_control);
326         }
327
328         return 0;
329 }
330
331 static int register_tracepoints(void)
332 {
333         if (GATOR_REGISTER_TRACE(mali_pm_status)) {
334                 pr_debug("gator: Mali-T6xx: mali_pm_status tracepoint failed to activate\n");
335                 return 0;
336         }
337
338         if (GATOR_REGISTER_TRACE(mali_page_fault_insert_pages)) {
339                 pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages tracepoint failed to activate\n");
340                 return 0;
341         }
342
343         if (GATOR_REGISTER_TRACE(mali_mmu_as_in_use)) {
344                 pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use tracepoint failed to activate\n");
345                 return 0;
346         }
347
348         if (GATOR_REGISTER_TRACE(mali_mmu_as_released)) {
349                 pr_debug("gator: Mali-T6xx: mali_mmu_as_released tracepoint failed to activate\n");
350                 return 0;
351         }
352
353         if (GATOR_REGISTER_TRACE(mali_total_alloc_pages_change)) {
354                 pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change tracepoint failed to activate\n");
355                 return 0;
356         }
357
358         pr_debug("gator: Mali-T6xx: start\n");
359         pr_debug("gator: Mali-T6xx: mali_pm_status probe is at %p\n", &probe_mali_pm_status);
360         pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages probe is at %p\n", &probe_mali_page_fault_insert_pages);
361         pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use probe is at %p\n", &probe_mali_mmu_as_in_use);
362         pr_debug("gator: Mali-T6xx: mali_mmu_as_released probe is at %p\n", &probe_mali_mmu_as_released);
363         pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change probe is at %p\n", &probe_mali_total_alloc_pages_change);
364
365         return 1;
366 }
367
368 static int start(void)
369 {
370         unsigned int cnt;
371         mali_profiling_control_type *mali_control;
372
373         /* Clean all data for the next capture */
374         for (cnt = 0; cnt < NUMBER_OF_TIMELINE_EVENTS; cnt++) {
375                 timeline_event_starttime[cnt].tv_sec = timeline_event_starttime[cnt].tv_nsec = 0;
376                 timeline_data[cnt] = 0;
377         }
378
379         for (cnt = 0; cnt < NUMBER_OF_SOFTWARE_COUNTERS; cnt++) {
380                 sw_counter_data[cnt] = 0;
381         }
382
383         for (cnt = 0; cnt < NUMBER_OF_ACCUMULATORS; cnt++) {
384                 accumulators_data[cnt] = 0;
385         }
386
387         /* Register tracepoints */
388         if (register_tracepoints() == 0) {
389                 return -1;
390         }
391
392         /* Generic control interface for Mali DDK. */
393         mali_control = symbol_get(_mali_profiling_control);
394         if (mali_control) {
395                 /* The event attribute in the XML file keeps the actual frame rate. */
396                 unsigned int enabled = counters[FILMSTRIP].enabled ? 1 : 0;
397                 unsigned int rate = filmstrip_event & 0xff;
398                 unsigned int resize_factor = (filmstrip_event >> 8) & 0xff;
399
400                 pr_debug("gator: mali online _mali_profiling_control symbol @ %p\n", mali_control);
401
402 #define FBDUMP_CONTROL_ENABLE (1)
403 #define FBDUMP_CONTROL_RATE (2)
404 #define FBDUMP_CONTROL_RESIZE_FACTOR (4)
405                 mali_control(FBDUMP_CONTROL_ENABLE, enabled);
406                 mali_control(FBDUMP_CONTROL_RATE, rate);
407                 mali_control(FBDUMP_CONTROL_RESIZE_FACTOR, resize_factor);
408
409                 pr_debug("gator: sent mali_control enabled=%d, rate=%d, resize_factor=%d\n", enabled, rate, resize_factor);
410
411                 symbol_put(_mali_profiling_control);
412         } else {
413                 printk("gator: mali online _mali_profiling_control symbol not found\n");
414         }
415
416         /*
417          * Set the first timestamp for calculating the sample interval. The first interval could be quite long,
418          * since it will be the time between 'start' and the first 'read'.
419          * This means that timeline values will be divided by a big number for the first sample.
420          */
421         getnstimeofday(&prev_timestamp);
422
423         return 0;
424 }
425
426 static void stop(void)
427 {
428         mali_profiling_control_type *mali_control;
429
430         pr_debug("gator: Mali-T6xx: stop\n");
431
432         /*
433          * It is safe to unregister traces even if they were not successfully
434          * registered, so no need to check.
435          */
436         GATOR_UNREGISTER_TRACE(mali_pm_status);
437         pr_debug("gator: Mali-T6xx: mali_pm_status tracepoint deactivated\n");
438
439         GATOR_UNREGISTER_TRACE(mali_page_fault_insert_pages);
440         pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages tracepoint deactivated\n");
441
442         GATOR_UNREGISTER_TRACE(mali_mmu_as_in_use);
443         pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use tracepoint deactivated\n");
444
445         GATOR_UNREGISTER_TRACE(mali_mmu_as_released);
446         pr_debug("gator: Mali-T6xx: mali_mmu_as_released tracepoint deactivated\n");
447
448         GATOR_UNREGISTER_TRACE(mali_total_alloc_pages_change);
449         pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change tracepoint deactivated\n");
450
451         /* Generic control interface for Mali DDK. */
452         mali_control = symbol_get(_mali_profiling_control);
453         if (mali_control) {
454                 pr_debug("gator: mali offline _mali_profiling_control symbol @ %p\n", mali_control);
455
456                 mali_control(FBDUMP_CONTROL_ENABLE, 0);
457
458                 symbol_put(_mali_profiling_control);
459         } else {
460                 printk("gator: mali offline _mali_profiling_control symbol not found\n");
461         }
462 }
463
464 static int read(int **buffer)
465 {
466         int cnt;
467         int len = 0;
468         long sample_interval_us = 0;
469         struct timespec read_timestamp;
470
471         if (!on_primary_core()) {
472                 return 0;
473         }
474
475         /* Get the start of this sample period. */
476         getnstimeofday(&read_timestamp);
477
478         /*
479          * Calculate the sample interval if the previous sample time is valid.
480          * We use tv_sec since it will not be 0.
481          */
482         if (prev_timestamp.tv_sec != 0) {
483                 sample_interval_us = get_duration_us(&prev_timestamp, &read_timestamp);
484         }
485
486         /* Structure copy. Update the previous timestamp. */
487         prev_timestamp = read_timestamp;
488
489         /*
490          * Report the timeline counters (ACTIVITY_START/STOP)
491          */
492         for (cnt = FIRST_TIMELINE_EVENT; cnt < (FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS); cnt++) {
493                 mali_counter *counter = &counters[cnt];
494                 if (counter->enabled) {
495                         const int index = cnt - FIRST_TIMELINE_EVENT;
496                         unsigned int value;
497
498                         /* If the activity is still running, reset its start time to the start of this sample period
499                          * to correct the count.  Add the time up to the end of the sample onto the count. */
500                         if (timeline_event_starttime[index].tv_sec != 0) {
501                                 const long event_duration = get_duration_us(&timeline_event_starttime[index], &read_timestamp);
502                                 timeline_data[index] += event_duration;
503                                 timeline_event_starttime[index] = read_timestamp;       /* Activity is still running. */
504                         }
505
506                         if (sample_interval_us != 0) {
507                                 /* Convert the counter to a percent-of-sample value */
508                                 value = (timeline_data[index] * 100) / sample_interval_us;
509                         } else {
510                                 pr_debug("gator: Mali-T6xx: setting value to zero\n");
511                                 value = 0;
512                         }
513
514                         /* Clear the counter value ready for the next sample. */
515                         timeline_data[index] = 0;
516
517                         counter_dump[len++] = counter->key;
518                         counter_dump[len++] = value;
519                 }
520         }
521
522         /* Report the software counters */
523         for (cnt = FIRST_SOFTWARE_COUNTER; cnt < (FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS); cnt++) {
524                 const mali_counter *counter = &counters[cnt];
525                 if (counter->enabled) {
526                         const int index = cnt - FIRST_SOFTWARE_COUNTER;
527                         counter_dump[len++] = counter->key;
528                         counter_dump[len++] = sw_counter_data[index];
529                         /* Set the value to zero for the next time */
530                         sw_counter_data[index] = 0;
531                 }
532         }
533
534         /* Report the accumulators */
535         for (cnt = FIRST_ACCUMULATOR; cnt < (FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS); cnt++) {
536                 const mali_counter *counter = &counters[cnt];
537                 if (counter->enabled) {
538                         const int index = cnt - FIRST_ACCUMULATOR;
539                         counter_dump[len++] = counter->key;
540                         counter_dump[len++] = accumulators_data[index];
541                         /* Do not zero the accumulator */
542                 }
543         }
544
545         /* Update the buffer */
546         if (buffer) {
547                 *buffer = (int *)counter_dump;
548         }
549
550         return len;
551 }
552
553 static struct gator_interface gator_events_mali_t6xx_interface = {
554         .create_files = create_files,
555         .start = start,
556         .stop = stop,
557         .read = read
558 };
559
560 extern int gator_events_mali_t6xx_init(void)
561 {
562         pr_debug("gator: Mali-T6xx: sw_counters init\n");
563
564         gator_mali_initialise_counters(counters, NUMBER_OF_EVENTS);
565
566         return gator_events_install(&gator_events_mali_t6xx_interface);
567 }