Merge tag 'v3.10.72' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_trace_gpu.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 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/time.h>
14 #include <linux/math64.h>
15
16 #ifdef MALI_SUPPORT
17 #ifdef MALI_DIR_MIDGARD
18 /* New DDK Directory structure with kernel/drivers/gpu/arm/midgard*/
19 #include "mali_linux_trace.h"
20 #else
21 /* Old DDK Directory structure with kernel/drivers/gpu/arm/t6xx*/
22 #include "linux/mali_linux_trace.h"
23 #endif
24 #endif
25
26 /*
27  * Taken from MALI_PROFILING_EVENT_TYPE_* items in Mali DDK.
28  */
29 #define EVENT_TYPE_SINGLE  0
30 #define EVENT_TYPE_START   1
31 #define EVENT_TYPE_STOP    2
32 #define EVENT_TYPE_SUSPEND 3
33 #define EVENT_TYPE_RESUME  4
34
35 /* Note whether tracepoints have been registered */
36 static int mali_timeline_trace_registered;
37 static int mali_job_slots_trace_registered;
38
39 enum {
40         GPU_UNIT_NONE = 0,
41         GPU_UNIT_VP,
42         GPU_UNIT_FP,
43         GPU_UNIT_CL,
44         NUMBER_OF_GPU_UNITS
45 };
46
47 #if defined(MALI_SUPPORT)
48
49 struct mali_activity {
50         int core;
51         int key;
52         int count;
53         int last_activity;
54         int last_pid;
55 };
56
57 #define NUMBER_OF_GPU_CORES 16
58 static struct mali_activity mali_activities[NUMBER_OF_GPU_UNITS*NUMBER_OF_GPU_CORES];
59 static DEFINE_SPINLOCK(mali_activities_lock);
60
61 /* Only one event should be running on a unit and core at a time (ie,
62  * a start event can only be followed by a stop and vice versa), but
63  * because the kernel only knows when a job is enqueued and not
64  * started, it is possible for a start1, start2, stop1, stop2. Change
65  * it back into start1, stop1, start2, stop2 by queueing up start2 and
66  * releasing it when stop1 is received.
67  */
68
69 static int mali_activity_index(int core, int key)
70 {
71         int i;
72
73         for (i = 0; i < ARRAY_SIZE(mali_activities); ++i) {
74                 if ((mali_activities[i].core == core) && (mali_activities[i].key == key))
75                         break;
76                 if ((mali_activities[i].core == 0) && (mali_activities[i].key == 0)) {
77                         mali_activities[i].core = core;
78                         mali_activities[i].key = key;
79                         break;
80                 }
81         }
82         BUG_ON(i >= ARRAY_SIZE(mali_activities));
83
84         return i;
85 }
86
87 static void mali_activity_enqueue(int core, int key, int activity, int pid)
88 {
89         int i;
90         int count;
91
92         spin_lock(&mali_activities_lock);
93         i = mali_activity_index(core, key);
94
95         count = mali_activities[i].count;
96         BUG_ON(count < 0);
97         ++mali_activities[i].count;
98         if (count) {
99                 mali_activities[i].last_activity = activity;
100                 mali_activities[i].last_pid = pid;
101         }
102         spin_unlock(&mali_activities_lock);
103
104         if (!count)
105                 gator_marshal_activity_switch(core, key, activity, pid);
106 }
107
108 static void mali_activity_stop(int core, int key)
109 {
110         int i;
111         int count;
112         int last_activity = 0;
113         int last_pid = 0;
114
115         spin_lock(&mali_activities_lock);
116         i = mali_activity_index(core, key);
117
118         if (mali_activities[i].count == 0) {
119                 spin_unlock(&mali_activities_lock);
120                 return;
121         }
122         --mali_activities[i].count;
123         count = mali_activities[i].count;
124         if (count) {
125                 last_activity = mali_activities[i].last_activity;
126                 last_pid = mali_activities[i].last_pid;
127         }
128         spin_unlock(&mali_activities_lock);
129
130         gator_marshal_activity_switch(core, key, 0, 0);
131         if (count)
132                 gator_marshal_activity_switch(core, key, last_activity, last_pid);
133 }
134
135 void mali_activity_clear(struct mali_counter mali_activity[], size_t mali_activity_size)
136 {
137         int activity;
138         int cores;
139         int core;
140
141         for (activity = 0; activity < mali_activity_size; ++activity) {
142                 cores = mali_activity[activity].cores;
143                 if (cores < 0)
144                         cores = 1;
145                 for (core = 0; core < cores; ++core) {
146                         if (mali_activity[activity].enabled) {
147                                 preempt_disable();
148                                 gator_marshal_activity_switch(core, mali_activity[activity].key, 0, 0);
149                                 preempt_enable();
150                         }
151                 }
152         }
153 }
154
155 #endif
156
157 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_MIDGARD)
158 #include "gator_events_mali_4xx.h"
159
160 /*
161  * Taken from MALI_PROFILING_EVENT_CHANNEL_* in Mali DDK.
162  */
163 enum {
164         EVENT_CHANNEL_SOFTWARE = 0,
165         EVENT_CHANNEL_VP0 = 1,
166         EVENT_CHANNEL_FP0 = 5,
167         EVENT_CHANNEL_FP1,
168         EVENT_CHANNEL_FP2,
169         EVENT_CHANNEL_FP3,
170         EVENT_CHANNEL_FP4,
171         EVENT_CHANNEL_FP5,
172         EVENT_CHANNEL_FP6,
173         EVENT_CHANNEL_FP7,
174         EVENT_CHANNEL_GPU = 21
175 };
176
177 /**
178  * These events are applicable when the type MALI_PROFILING_EVENT_TYPE_SINGLE is used from the GPU channel
179  */
180 enum {
181         EVENT_REASON_SINGLE_GPU_NONE = 0,
182         EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE = 1,
183 };
184
185 struct mali_counter mali_activity[2];
186
187 GATOR_DEFINE_PROBE(mali_timeline_event, TP_PROTO(unsigned int event_id, unsigned int d0, unsigned int d1, unsigned int d2, unsigned int d3, unsigned int d4))
188 {
189         unsigned int component, state;
190
191         /* do as much work as possible before disabling interrupts */
192         component = (event_id >> 16) & 0xFF;    /* component is an 8-bit field */
193         state = (event_id >> 24) & 0xF; /* state is a 4-bit field */
194
195         switch (state) {
196         case EVENT_TYPE_START:
197                 if (component == EVENT_CHANNEL_VP0) {
198                         /* tgid = d0; pid = d1; */
199                         if (mali_activity[1].enabled)
200                                 mali_activity_enqueue(0, mali_activity[1].key, 1, d1);
201                 } else if (component >= EVENT_CHANNEL_FP0 && component <= EVENT_CHANNEL_FP7) {
202                         /* tgid = d0; pid = d1; */
203                         if (mali_activity[0].enabled)
204                                 mali_activity_enqueue(component - EVENT_CHANNEL_FP0, mali_activity[0].key, 1, d1);
205                 }
206                 break;
207
208         case EVENT_TYPE_STOP:
209                 if (component == EVENT_CHANNEL_VP0) {
210                         if (mali_activity[1].enabled)
211                                 mali_activity_stop(0, mali_activity[1].key);
212                 } else if (component >= EVENT_CHANNEL_FP0 && component <= EVENT_CHANNEL_FP7) {
213                         if (mali_activity[0].enabled)
214                                 mali_activity_stop(component - EVENT_CHANNEL_FP0, mali_activity[0].key);
215                 }
216                 break;
217
218         case EVENT_TYPE_SINGLE:
219                 if (component == EVENT_CHANNEL_GPU) {
220                         unsigned int reason = (event_id & 0xffff);
221
222                         if (reason == EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE)
223                                 gator_events_mali_log_dvfs_event(d0, d1);
224                 }
225                 break;
226
227         default:
228                 break;
229         }
230 }
231 #endif
232
233 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_MIDGARD)
234
235 struct mali_counter mali_activity[3];
236
237 #if defined(MALI_JOB_SLOTS_EVENT_CHANGED)
238 GATOR_DEFINE_PROBE(mali_job_slots_event, TP_PROTO(unsigned int event_id, unsigned int tgid, unsigned int pid, unsigned char job_id))
239 #else
240 GATOR_DEFINE_PROBE(mali_job_slots_event, TP_PROTO(unsigned int event_id, unsigned int tgid, unsigned int pid))
241 #endif
242 {
243         unsigned int component, state, unit;
244 #if !defined(MALI_JOB_SLOTS_EVENT_CHANGED)
245         unsigned char job_id = 0;
246 #endif
247
248         component = (event_id >> 16) & 0xFF;    /* component is an 8-bit field */
249         state = (event_id >> 24) & 0xF; /* state is a 4-bit field */
250
251         switch (component) {
252         case 0:
253                 unit = GPU_UNIT_FP;
254                 break;
255         case 1:
256                 unit = GPU_UNIT_VP;
257                 break;
258         case 2:
259                 unit = GPU_UNIT_CL;
260                 break;
261         default:
262                 unit = GPU_UNIT_NONE;
263         }
264
265         if (unit != GPU_UNIT_NONE) {
266                 switch (state) {
267                 case EVENT_TYPE_START:
268                         if (mali_activity[component].enabled)
269                                 mali_activity_enqueue(0, mali_activity[component].key, 1, (pid != 0 ? pid : tgid));
270                         break;
271                 case EVENT_TYPE_STOP:
272                 default: /* Some jobs can be soft-stopped, so ensure that this terminates the activity trace. */
273                         if (mali_activity[component].enabled)
274                                 mali_activity_stop(0, mali_activity[component].key);
275                         break;
276                 }
277         }
278 }
279 #endif
280
281 static int gator_trace_gpu_start(void)
282 {
283         /*
284          * Returns nonzero for installation failed
285          * Absence of gpu trace points is not an error
286          */
287
288 #if defined(MALI_SUPPORT)
289         memset(&mali_activities, 0, sizeof(mali_activities));
290 #endif
291         mali_timeline_trace_registered = mali_job_slots_trace_registered = 0;
292
293 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_MIDGARD)
294         mali_activity_clear(mali_activity, ARRAY_SIZE(mali_activity));
295         if (!GATOR_REGISTER_TRACE(mali_timeline_event))
296                 mali_timeline_trace_registered = 1;
297 #endif
298
299 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_MIDGARD)
300         mali_activity_clear(mali_activity, ARRAY_SIZE(mali_activity));
301         if (!GATOR_REGISTER_TRACE(mali_job_slots_event))
302                 mali_job_slots_trace_registered = 1;
303 #endif
304
305         return 0;
306 }
307
308 static void gator_trace_gpu_stop(void)
309 {
310 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_MIDGARD)
311         if (mali_timeline_trace_registered)
312                 GATOR_UNREGISTER_TRACE(mali_timeline_event);
313 #endif
314
315 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_MIDGARD)
316         if (mali_job_slots_trace_registered)
317                 GATOR_UNREGISTER_TRACE(mali_job_slots_event);
318 #endif
319
320         mali_timeline_trace_registered = mali_job_slots_trace_registered = 0;
321 }