Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android' into...
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_mali_t6xx_hw.c
1 /**
2  * Copyright (C) ARM Limited 2012-2013. 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 /* Mali T6xx DDK includes */
19 #include "linux/mali_linux_trace.h"
20 #include "kbase/src/common/mali_kbase.h"
21 #include "kbase/src/linux/mali_kbase_mem_linux.h"
22
23 #include "gator_events_mali_common.h"
24
25 /* If API version is not specified then assume API version 1. */
26 #ifndef MALI_DDK_GATOR_API_VERSION
27 #define MALI_DDK_GATOR_API_VERSION 1
28 #endif
29
30 #if (MALI_DDK_GATOR_API_VERSION != 1) && (MALI_DDK_GATOR_API_VERSION != 2)
31 #error MALI_DDK_GATOR_API_VERSION is invalid (must be 1 for r1/r2 DDK, or 2 for r3 DDK).
32 #endif
33
34 /*
35  * Mali-T6xx
36  */
37 typedef struct kbase_device *kbase_find_device_type(int);
38 typedef kbase_context *kbase_create_context_type(kbase_device *);
39 typedef void kbase_destroy_context_type(kbase_context *);
40
41 #if MALI_DDK_GATOR_API_VERSION == 1
42 typedef void *kbase_va_alloc_type(kbase_context *, u32);
43 typedef void kbase_va_free_type(kbase_context *, void *);
44 #elif MALI_DDK_GATOR_API_VERSION == 2
45 typedef void *kbase_va_alloc_type(kbase_context *, u32, kbase_hwc_dma_mapping * handle);
46 typedef void kbase_va_free_type(kbase_context *, kbase_hwc_dma_mapping * handle);
47 #endif
48
49 typedef mali_error kbase_instr_hwcnt_enable_type(kbase_context *, kbase_uk_hwcnt_setup *);
50 typedef mali_error kbase_instr_hwcnt_disable_type(kbase_context *);
51 typedef mali_error kbase_instr_hwcnt_clear_type(kbase_context *);
52 typedef mali_error kbase_instr_hwcnt_dump_irq_type(kbase_context *);
53 typedef mali_bool kbase_instr_hwcnt_dump_complete_type(kbase_context *, mali_bool *);
54
55 static kbase_find_device_type *kbase_find_device_symbol;
56 static kbase_create_context_type *kbase_create_context_symbol;
57 static kbase_va_alloc_type *kbase_va_alloc_symbol;
58 static kbase_instr_hwcnt_enable_type *kbase_instr_hwcnt_enable_symbol;
59 static kbase_instr_hwcnt_clear_type *kbase_instr_hwcnt_clear_symbol;
60 static kbase_instr_hwcnt_dump_irq_type *kbase_instr_hwcnt_dump_irq_symbol;
61 static kbase_instr_hwcnt_dump_complete_type *kbase_instr_hwcnt_dump_complete_symbol;
62 static kbase_instr_hwcnt_disable_type *kbase_instr_hwcnt_disable_symbol;
63 static kbase_va_free_type *kbase_va_free_symbol;
64 static kbase_destroy_context_type *kbase_destroy_context_symbol;
65
66 static long shader_present_low = 0;
67
68 /** The interval between reads, in ns.
69  *
70  * Earlier we introduced
71  * a 'hold off for 1ms after last read' to resolve MIDBASE-2178 and MALINE-724.
72  * However, the 1ms hold off is too long if no context switches occur as there is a race
73  * between this value and the tick of the read clock in gator which is also 1ms. If we 'miss' the
74  * current read, the counter values are effectively 'spread' over 2ms and the values seen are half
75  * what they should be (since Streamline averages over sample time). In the presence of context switches
76  * this spread can vary and markedly affect the counters.  Currently there is no 'proper' solution to
77  * this, but empirically we have found that reducing the minimum read interval to 950us causes the
78  * counts to be much more stable.
79  */
80 static const int READ_INTERVAL_NSEC = 950000;
81
82 #if GATOR_TEST
83 #include "gator_events_mali_t6xx_hw_test.c"
84 #endif
85
86 /* Blocks for HW counters */
87 enum {
88         JM_BLOCK = 0,
89         TILER_BLOCK,
90         SHADER_BLOCK,
91         MMU_BLOCK
92 };
93
94 /* Counters for Mali-T6xx:
95  *
96  *  - HW counters, 4 blocks
97  *    For HW counters we need strings to create /dev/gator/events files.
98  *    Enums are not needed because the position of the HW name in the array is the same
99  *    of the corresponding value in the received block of memory.
100  *    HW counters are requested by calculating a bitmask, passed then to the driver.
101  *    Every millisecond a HW counters dump is requested, and if the previous has been completed they are read.
102  */
103
104 /* Hardware Counters */
105 static const char *const hardware_counter_names[] = {
106         /* Job Manager */
107         "",
108         "",
109         "",
110         "",
111         "MESSAGES_SENT",
112         "MESSAGES_RECEIVED",
113         "GPU_ACTIVE",           /* 6 */
114         "IRQ_ACTIVE",
115         "JS0_JOBS",
116         "JS0_TASKS",
117         "JS0_ACTIVE",
118         "",
119         "JS0_WAIT_READ",
120         "JS0_WAIT_ISSUE",
121         "JS0_WAIT_DEPEND",
122         "JS0_WAIT_FINISH",
123         "JS1_JOBS",
124         "JS1_TASKS",
125         "JS1_ACTIVE",
126         "",
127         "JS1_WAIT_READ",
128         "JS1_WAIT_ISSUE",
129         "JS1_WAIT_DEPEND",
130         "JS1_WAIT_FINISH",
131         "JS2_JOBS",
132         "JS2_TASKS",
133         "JS2_ACTIVE",
134         "",
135         "JS2_WAIT_READ",
136         "JS2_WAIT_ISSUE",
137         "JS2_WAIT_DEPEND",
138         "JS2_WAIT_FINISH",
139         "JS3_JOBS",
140         "JS3_TASKS",
141         "JS3_ACTIVE",
142         "",
143         "JS3_WAIT_READ",
144         "JS3_WAIT_ISSUE",
145         "JS3_WAIT_DEPEND",
146         "JS3_WAIT_FINISH",
147         "JS4_JOBS",
148         "JS4_TASKS",
149         "JS4_ACTIVE",
150         "",
151         "JS4_WAIT_READ",
152         "JS4_WAIT_ISSUE",
153         "JS4_WAIT_DEPEND",
154         "JS4_WAIT_FINISH",
155         "JS5_JOBS",
156         "JS5_TASKS",
157         "JS5_ACTIVE",
158         "",
159         "JS5_WAIT_READ",
160         "JS5_WAIT_ISSUE",
161         "JS5_WAIT_DEPEND",
162         "JS5_WAIT_FINISH",
163         "JS6_JOBS",
164         "JS6_TASKS",
165         "JS6_ACTIVE",
166         "",
167         "JS6_WAIT_READ",
168         "JS6_WAIT_ISSUE",
169         "JS6_WAIT_DEPEND",
170         "JS6_WAIT_FINISH",
171
172         /*Tiler */
173         "",
174         "",
175         "",
176         "JOBS_PROCESSED",
177         "TRIANGLES",
178         "QUADS",
179         "POLYGONS",
180         "POINTS",
181         "LINES",
182         "VCACHE_HIT",
183         "VCACHE_MISS",
184         "FRONT_FACING",
185         "BACK_FACING",
186         "PRIM_VISIBLE",
187         "PRIM_CULLED",
188         "PRIM_CLIPPED",
189         "LEVEL0",
190         "LEVEL1",
191         "LEVEL2",
192         "LEVEL3",
193         "LEVEL4",
194         "LEVEL5",
195         "LEVEL6",
196         "LEVEL7",
197         "COMMAND_1",
198         "COMMAND_2",
199         "COMMAND_3",
200         "COMMAND_4",
201         "COMMAND_4_7",
202         "COMMAND_8_15",
203         "COMMAND_16_63",
204         "COMMAND_64",
205         "COMPRESS_IN",
206         "COMPRESS_OUT",
207         "COMPRESS_FLUSH",
208         "TIMESTAMPS",
209         "PCACHE_HIT",
210         "PCACHE_MISS",
211         "PCACHE_LINE",
212         "PCACHE_STALL",
213         "WRBUF_HIT",
214         "WRBUF_MISS",
215         "WRBUF_LINE",
216         "WRBUF_PARTIAL",
217         "WRBUF_STALL",
218         "ACTIVE",
219         "LOADING_DESC",
220         "INDEX_WAIT",
221         "INDEX_RANGE_WAIT",
222         "VERTEX_WAIT",
223         "PCACHE_WAIT",
224         "WRBUF_WAIT",
225         "BUS_READ",
226         "BUS_WRITE",
227         "",
228         "",
229         "",
230         "",
231         "",
232         "UTLB_STALL",
233         "UTLB_REPLAY_MISS",
234         "UTLB_REPLAY_FULL",
235         "UTLB_NEW_MISS",
236         "UTLB_HIT",
237
238         /* Shader Core */
239         "",
240         "",
241         "",
242         "SHADER_CORE_ACTIVE",
243         "FRAG_ACTIVE",
244         "FRAG_PRIMATIVES",
245         "FRAG_PRIMATIVES_DROPPED",
246         "FRAG_CYCLE_DESC",
247         "FRAG_CYCLES_PLR",
248         "FRAG_CYCLES_VERT",
249         "FRAG_CYCLES_TRISETUP",
250         "FRAG_CYCLES_RAST",
251         "FRAG_THREADS",
252         "FRAG_DUMMY_THREADS",
253         "FRAG_QUADS_RAST",
254         "FRAG_QUADS_EZS_TEST",
255         "FRAG_QUADS_EZS_KILLED",
256         "FRAG_QUADS_LZS_TEST",
257         "FRAG_QUADS_LZS_KILLED",
258         "FRAG_CYCLE_NO_TILE",
259         "FRAG_NUM_TILES",
260         "FRAG_TRANS_ELIM",
261         "COMPUTE_ACTIVE",
262         "COMPUTE_TASKS",
263         "COMPUTE_THREADS",
264         "COMPUTE_CYCLES_DESC",
265         "TRIPIPE_ACTIVE",
266         "ARITH_WORDS",
267         "ARITH_CYCLES_REG",
268         "ARITH_CYCLES_L0",
269         "ARITH_FRAG_DEPEND",
270         "LS_WORDS",
271         "LS_ISSUES",
272         "LS_RESTARTS",
273         "LS_REISSUES_MISS",
274         "LS_REISSUES_VD",
275         "LS_REISSUE_ATTRIB_MISS",
276         "LS_NO_WB",
277         "TEX_WORDS",
278         "TEX_BUBBLES",
279         "TEX_WORDS_L0",
280         "TEX_WORDS_DESC",
281         "TEX_THREADS",
282         "TEX_RECIRC_FMISS",
283         "TEX_RECIRC_DESC",
284         "TEX_RECIRC_MULTI",
285         "TEX_RECIRC_PMISS",
286         "TEX_RECIRC_CONF",
287         "LSC_READ_HITS",
288         "LSC_READ_MISSES",
289         "LSC_WRITE_HITS",
290         "LSC_WRITE_MISSES",
291         "LSC_ATOMIC_HITS",
292         "LSC_ATOMIC_MISSES",
293         "LSC_LINE_FETCHES",
294         "LSC_DIRTY_LINE",
295         "LSC_SNOOPS",
296         "AXI_TLB_STALL",
297         "AXI_TLB_MIESS",
298         "AXI_TLB_TRANSACTION",
299         "LS_TLB_MISS",
300         "LS_TLB_HIT",
301         "AXI_BEATS_READ",
302         "AXI_BEATS_WRITTEN",
303
304         /*L2 and MMU */
305         "",
306         "",
307         "",
308         "",
309         "MMU_HIT",
310         "MMU_NEW_MISS",
311         "MMU_REPLAY_FULL",
312         "MMU_REPLAY_MISS",
313         "MMU_TABLE_WALK",
314         "",
315         "",
316         "",
317         "",
318         "",
319         "",
320         "",
321         "UTLB_HIT",
322         "UTLB_NEW_MISS",
323         "UTLB_REPLAY_FULL",
324         "UTLB_REPLAY_MISS",
325         "UTLB_STALL",
326         "",
327         "",
328         "",
329         "",
330         "",
331         "",
332         "",
333         "",
334         "",
335         "L2_WRITE_BEATS",
336         "L2_READ_BEATS",
337         "L2_ANY_LOOKUP",
338         "L2_READ_LOOKUP",
339         "L2_SREAD_LOOKUP",
340         "L2_READ_REPLAY",
341         "L2_READ_SNOOP",
342         "L2_READ_HIT",
343         "L2_CLEAN_MISS",
344         "L2_WRITE_LOOKUP",
345         "L2_SWRITE_LOOKUP",
346         "L2_WRITE_REPLAY",
347         "L2_WRITE_SNOOP",
348         "L2_WRITE_HIT",
349         "L2_EXT_READ_FULL",
350         "L2_EXT_READ_HALF",
351         "L2_EXT_WRITE_FULL",
352         "L2_EXT_WRITE_HALF",
353         "L2_EXT_READ",
354         "L2_EXT_READ_LINE",
355         "L2_EXT_WRITE",
356         "L2_EXT_WRITE_LINE",
357         "L2_EXT_WRITE_SMALL",
358         "L2_EXT_BARRIER",
359         "L2_EXT_AR_STALL",
360         "L2_EXT_R_BUF_FULL",
361         "L2_EXT_RD_BUF_FULL",
362         "L2_EXT_R_RAW",
363         "L2_EXT_W_STALL",
364         "L2_EXT_W_BUF_FULL",
365         "L2_EXT_R_W_HAZARD",
366         "L2_TAG_HAZARD",
367         "L2_SNOOP_FULL",
368         "L2_REPLAY_FULL"
369 };
370
371 #define NUMBER_OF_HARDWARE_COUNTERS (sizeof(hardware_counter_names) / sizeof(hardware_counter_names[0]))
372
373 #define GET_HW_BLOCK(c) (((c) >> 6) & 0x3)
374 #define GET_COUNTER_OFFSET(c) ((c) & 0x3f)
375
376 /* Memory to dump hardware counters into */
377 static void *kernel_dump_buffer;
378
379 #if MALI_DDK_GATOR_API_VERSION == 2
380 /* DMA state used to manage lifetime of the buffer */
381 kbase_hwc_dma_mapping kernel_dump_buffer_handle;
382 #endif
383
384 /* kbase context and device */
385 static kbase_context *kbcontext = NULL;
386 static struct kbase_device *kbdevice = NULL;
387
388 /*
389  * The following function has no external prototype in older DDK revisions.  When the DDK
390  * is updated then this should be removed.
391  */
392 struct kbase_device *kbase_find_device(int minor);
393
394 static volatile bool kbase_device_busy = false;
395 static unsigned int num_hardware_counters_enabled;
396
397 /*
398  * gatorfs variables for counter enable state
399  */
400 static mali_counter counters[NUMBER_OF_HARDWARE_COUNTERS];
401
402 /* An array used to return the data we recorded
403  * as key,value pairs hence the *2
404  */
405 static unsigned long counter_dump[NUMBER_OF_HARDWARE_COUNTERS * 2];
406
407 #define SYMBOL_GET(FUNCTION, ERROR_COUNT) \
408         if(FUNCTION ## _symbol) \
409         { \
410                 printk("gator: mali " #FUNCTION " symbol was already registered\n"); \
411                 (ERROR_COUNT)++; \
412         } \
413         else \
414         { \
415                 FUNCTION ## _symbol = symbol_get(FUNCTION); \
416                 if(! FUNCTION ## _symbol) \
417                 { \
418                         printk("gator: mali online " #FUNCTION " symbol not found\n"); \
419                         (ERROR_COUNT)++; \
420                 } \
421         }
422
423 #define SYMBOL_CLEANUP(FUNCTION) \
424         if(FUNCTION ## _symbol) \
425         { \
426         symbol_put(FUNCTION); \
427         FUNCTION ## _symbol = NULL; \
428         }
429
430 /**
431  * Execute symbol_get for all the Mali symbols and check for success.
432  * @return the number of symbols not loaded.
433  */
434 static int init_symbols(void)
435 {
436         int error_count = 0;
437         SYMBOL_GET(kbase_find_device, error_count);
438         SYMBOL_GET(kbase_create_context, error_count);
439         SYMBOL_GET(kbase_va_alloc, error_count);
440         SYMBOL_GET(kbase_instr_hwcnt_enable, error_count);
441         SYMBOL_GET(kbase_instr_hwcnt_clear, error_count);
442         SYMBOL_GET(kbase_instr_hwcnt_dump_irq, error_count);
443         SYMBOL_GET(kbase_instr_hwcnt_dump_complete, error_count);
444         SYMBOL_GET(kbase_instr_hwcnt_disable, error_count);
445         SYMBOL_GET(kbase_va_free, error_count);
446         SYMBOL_GET(kbase_destroy_context, error_count);
447
448         return error_count;
449 }
450
451 /**
452  * Execute symbol_put for all the registered Mali symbols.
453  */
454 static void clean_symbols(void)
455 {
456         SYMBOL_CLEANUP(kbase_find_device);
457         SYMBOL_CLEANUP(kbase_create_context);
458         SYMBOL_CLEANUP(kbase_va_alloc);
459         SYMBOL_CLEANUP(kbase_instr_hwcnt_enable);
460         SYMBOL_CLEANUP(kbase_instr_hwcnt_clear);
461         SYMBOL_CLEANUP(kbase_instr_hwcnt_dump_irq);
462         SYMBOL_CLEANUP(kbase_instr_hwcnt_dump_complete);
463         SYMBOL_CLEANUP(kbase_instr_hwcnt_disable);
464         SYMBOL_CLEANUP(kbase_va_free);
465         SYMBOL_CLEANUP(kbase_destroy_context);
466 }
467
468 /**
469  * Determines whether a read should take place
470  * @param current_time The current time, obtained from getnstimeofday()
471  * @param prev_time_s The number of seconds at the previous read attempt.
472  * @param next_read_time_ns The time (in ns) when the next read should be allowed.
473  *
474  * Note that this function has been separated out here to allow it to be tested.
475  */
476 static int is_read_scheduled(const struct timespec *current_time, u32 *prev_time_s, s32 *next_read_time_ns)
477 {
478         /* If the current ns count rolls over a second, roll the next read time too. */
479         if (current_time->tv_sec != *prev_time_s) {
480                 *next_read_time_ns = *next_read_time_ns - NSEC_PER_SEC;
481         }
482
483         /* Abort the read if the next read time has not arrived. */
484         if (current_time->tv_nsec < *next_read_time_ns) {
485                 return 0;
486         }
487
488         /* Set the next read some fixed time after this one, and update the read timestamp. */
489         *next_read_time_ns = current_time->tv_nsec + READ_INTERVAL_NSEC;
490
491         *prev_time_s = current_time->tv_sec;
492         return 1;
493 }
494
495 static int start(void)
496 {
497         kbase_uk_hwcnt_setup setup;
498         mali_error err;
499         int cnt;
500         u16 bitmask[] = { 0, 0, 0, 0 };
501         unsigned long long shadersPresent = 0;
502
503         /* Setup HW counters */
504         num_hardware_counters_enabled = 0;
505
506         if (NUMBER_OF_HARDWARE_COUNTERS != 256) {
507                 pr_debug("Unexpected number of hardware counters defined: expecting 256, got %d\n", NUMBER_OF_HARDWARE_COUNTERS);
508         }
509
510         /* Calculate enable bitmasks based on counters_enabled array */
511         for (cnt = 0; cnt < NUMBER_OF_HARDWARE_COUNTERS; cnt++) {
512                 const mali_counter *counter = &counters[cnt];
513                 if (counter->enabled) {
514                         int block = GET_HW_BLOCK(cnt);
515                         int enable_bit = GET_COUNTER_OFFSET(cnt) / 4;
516                         bitmask[block] |= (1 << enable_bit);
517                         pr_debug("gator: Mali-T6xx: hardware counter %s selected [%d]\n", hardware_counter_names[cnt], cnt);
518                         num_hardware_counters_enabled++;
519                 }
520         }
521
522         /* Create a kbase context for HW counters */
523         if (num_hardware_counters_enabled > 0) {
524                 if (init_symbols() > 0) {
525                         clean_symbols();
526                         /* No Mali driver code entrypoints found - not a fault. */
527                         return 0;
528                 }
529
530                 kbdevice = kbase_find_device_symbol(-1);
531
532                 /* If we already got a context, fail */
533                 if (kbcontext) {
534                         pr_debug("gator: Mali-T6xx: error context already present\n");
535                         goto out;
536                 }
537
538                 /* kbcontext will only be valid after all the Mali symbols are loaded successfully */
539                 kbcontext = kbase_create_context_symbol(kbdevice);
540                 if (!kbcontext) {
541                         pr_debug("gator: Mali-T6xx: error creating kbase context\n");
542                         goto out;
543                 }
544
545
546                 /* See if we can get the number of shader cores */
547                 shadersPresent = kbdevice->shader_present_bitmap;
548                 shader_present_low = (unsigned long)shadersPresent;
549
550                 /*
551                  * The amount of memory needed to store the dump (bytes)
552                  * DUMP_SIZE = number of core groups
553                  *             * number of blocks (always 8 for midgard)
554                  *             * number of counters per block (always 64 for midgard)
555                  *             * number of bytes per counter (always 4 in midgard)
556                  * For a Mali-T6xx with a single core group = 1 * 8 * 64 * 4 = 2048
557                  * For a Mali-T6xx with a dual core group   = 2 * 8 * 64 * 4 = 4096
558                  */
559 #if MALI_DDK_GATOR_API_VERSION == 1
560                 kernel_dump_buffer = kbase_va_alloc_symbol(kbcontext, 4096);
561 #elif MALI_DDK_GATOR_API_VERSION == 2
562                 kernel_dump_buffer = kbase_va_alloc_symbol(kbcontext, 4096, &kernel_dump_buffer_handle);
563 #endif
564                 if (!kernel_dump_buffer) {
565                         pr_debug("gator: Mali-T6xx: error trying to allocate va\n");
566                         goto destroy_context;
567                 }
568
569                 setup.dump_buffer = (uintptr_t)kernel_dump_buffer;
570                 setup.jm_bm = bitmask[JM_BLOCK];
571                 setup.tiler_bm = bitmask[TILER_BLOCK];
572                 setup.shader_bm = bitmask[SHADER_BLOCK];
573                 setup.mmu_l2_bm = bitmask[MMU_BLOCK];
574                 /* These counters do not exist on Mali-T60x */
575                 setup.l3_cache_bm = 0;
576
577                 /* Use kbase API to enable hardware counters and provide dump buffer */
578                 err = kbase_instr_hwcnt_enable_symbol(kbcontext, &setup);
579                 if (err != MALI_ERROR_NONE) {
580                         pr_debug("gator: Mali-T6xx: can't setup hardware counters\n");
581                         goto free_buffer;
582                 }
583                 pr_debug("gator: Mali-T6xx: hardware counters enabled\n");
584                 kbase_instr_hwcnt_clear_symbol(kbcontext);
585                 pr_debug("gator: Mali-T6xx: hardware counters cleared \n");
586
587                 kbase_device_busy = false;
588         }
589
590         return 0;
591
592 free_buffer:
593 #if MALI_DDK_GATOR_API_VERSION == 1
594         kbase_va_free_symbol(kbcontext, kernel_dump_buffer);
595 #elif MALI_DDK_GATOR_API_VERSION == 2
596         kbase_va_free_symbol(kbcontext, &kernel_dump_buffer_handle);
597 #endif
598
599 destroy_context:
600         kbase_destroy_context_symbol(kbcontext);
601
602 out:
603         clean_symbols();
604         return -1;
605 }
606
607 static void stop(void)
608 {
609         unsigned int cnt;
610         kbase_context *temp_kbcontext;
611
612         pr_debug("gator: Mali-T6xx: stop\n");
613
614         /* Set all counters as disabled */
615         for (cnt = 0; cnt < NUMBER_OF_HARDWARE_COUNTERS; cnt++) {
616                 counters[cnt].enabled = 0;
617         }
618
619         /* Destroy the context for HW counters */
620         if (num_hardware_counters_enabled > 0 && kbcontext != NULL) {
621                 /*
622                  * Set the global variable to NULL before destroying it, because
623                  * other function will check this before using it.
624                  */
625                 temp_kbcontext = kbcontext;
626                 kbcontext = NULL;
627
628                 kbase_instr_hwcnt_disable_symbol(temp_kbcontext);
629
630 #if MALI_DDK_GATOR_API_VERSION == 1
631                 kbase_va_free_symbol(temp_kbcontext, kernel_dump_buffer);
632 #elif MALI_DDK_GATOR_API_VERSION == 2
633                 kbase_va_free_symbol(temp_kbcontext, &kernel_dump_buffer_handle);
634 #endif
635
636                 kbase_destroy_context_symbol(temp_kbcontext);
637
638                 pr_debug("gator: Mali-T6xx: hardware counters stopped\n");
639
640                 clean_symbols();
641         }
642 }
643
644 static int read(int **buffer)
645 {
646         int cnt;
647         int len = 0;
648         u32 value = 0;
649         mali_bool success;
650
651         struct timespec current_time;
652         static u32 prev_time_s = 0;
653         static s32 next_read_time_ns = 0;
654
655         if (!on_primary_core()) {
656                 return 0;
657         }
658
659         getnstimeofday(&current_time);
660
661         /*
662          * Discard reads unless a respectable time has passed.  This reduces the load on the GPU without sacrificing
663          * accuracy on the Streamline display.
664          */
665         if (!is_read_scheduled(&current_time, &prev_time_s, &next_read_time_ns)) {
666                 return 0;
667         }
668
669         /*
670          * Report the HW counters
671          * Only process hardware counters if at least one of the hardware counters is enabled.
672          */
673         if (num_hardware_counters_enabled > 0) {
674                 const unsigned int vithar_blocks[] = {
675                         0x700,  /* VITHAR_JOB_MANAGER,     Block 0 */
676                         0x400,  /* VITHAR_TILER,           Block 1 */
677                         0x000,  /* VITHAR_SHADER_CORE,     Block 2 */
678                         0x500   /* VITHAR_MEMORY_SYSTEM,   Block 3 */
679                 };
680
681                 if (!kbcontext) {
682                         return -1;
683                 }
684
685                 /* Mali symbols can be called safely since a kbcontext is valid */
686                 if (kbase_instr_hwcnt_dump_complete_symbol(kbcontext, &success) == MALI_TRUE) {
687                         kbase_device_busy = false;
688
689                         if (success == MALI_TRUE) {
690                                 /* Cycle through hardware counters and accumulate totals */
691                                 for (cnt = 0; cnt < NUMBER_OF_HARDWARE_COUNTERS; cnt++) {
692                                         const mali_counter *counter = &counters[cnt];
693                                         if (counter->enabled) {
694                                                 const int block = GET_HW_BLOCK(cnt);
695                                                 const int counter_offset = GET_COUNTER_OFFSET(cnt);
696
697                                                 const char* block_base_address = (char*)kernel_dump_buffer + vithar_blocks[block];
698
699                                                 /* If counter belongs to shader block need to take into account all cores */
700                                                 if (block == SHADER_BLOCK) {
701                                                         int i = 0;
702                                                         int shader_core_count = 0;
703                                                         value = 0;
704
705                                                         for (i = 0; i < 4; i++) {
706                                                                 if ((shader_present_low >> i) & 1) {
707                                                                         value += *((u32*) (block_base_address + (0x100 * i)) + counter_offset);
708                                                                         shader_core_count++;
709                                                                 }
710                                                         }
711
712                                                         for (i = 0; i < 4; i++) {
713                                                                 if((shader_present_low >> (i+4)) & 1) {
714                                                                         value += *((u32*)(block_base_address + (0x100 * i) + 0x800) + counter_offset);
715                                                                         shader_core_count++;
716                                                                 }
717                                                         }
718
719                                                         /* Need to total by number of cores to produce an average */
720                                                         if (shader_core_count != 0) {
721                                                                 value /= shader_core_count;
722                                                         }
723                                                 } else {
724                                                         value = *((u32*)block_base_address + counter_offset);
725                                                 }
726
727                                                 counter_dump[len++] = counter->key;
728                                                 counter_dump[len++] = value;
729                                         }
730                                 }
731                         }
732                 }
733
734                 if (!kbase_device_busy) {
735                         kbase_device_busy = true;
736                         kbase_instr_hwcnt_dump_irq_symbol(kbcontext);
737                 }
738         }
739
740         /* Update the buffer */
741         if (buffer) {
742                 *buffer = (int *)counter_dump;
743         }
744
745         return len;
746 }
747
748 static int create_files(struct super_block *sb, struct dentry *root)
749 {
750         unsigned int event;
751         /*
752          * Create the filesystem for all events
753          */
754         int counter_index = 0;
755         const char *mali_name = gator_mali_get_mali_name();
756
757         for (event = 0; event < NUMBER_OF_HARDWARE_COUNTERS; event++) {
758                 if (gator_mali_create_file_system(mali_name, hardware_counter_names[counter_index], sb, root, &counters[event], NULL) != 0)
759                         return -1;
760                 counter_index++;
761         }
762
763         return 0;
764 }
765
766 static struct gator_interface gator_events_mali_t6xx_interface = {
767         .create_files = create_files,
768         .start = start,
769         .stop = stop,
770         .read = read
771 };
772
773 int gator_events_mali_t6xx_hw_init(void)
774 {
775         pr_debug("gator: Mali-T6xx: sw_counters init\n");
776
777 #if GATOR_TEST
778         test_all_is_read_scheduled();
779 #endif
780
781         gator_mali_initialise_counters(counters, NUMBER_OF_HARDWARE_COUNTERS);
782
783         return gator_events_install(&gator_events_mali_t6xx_interface);
784 }