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 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/time.h>
14 #include <linux/math64.h>
17 #ifdef MALI_DIR_MIDGARD
18 /* New DDK Directory structure with kernel/drivers/gpu/arm/midgard*/
19 #include "mali_linux_trace.h"
21 /* Old DDK Directory structure with kernel/drivers/gpu/arm/t6xx*/
22 #include "linux/mali_linux_trace.h"
27 * Taken from MALI_PROFILING_EVENT_TYPE_* items in Mali DDK.
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
35 /* Note whether tracepoints have been registered */
36 static int mali_timeline_trace_registered;
37 static int mali_job_slots_trace_registered;
47 #if defined(MALI_SUPPORT)
49 struct mali_activity {
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);
61 /* Only one event should be running on a unit and core at a time (ie, a start
62 * event can only be followed by a stop and vice versa), but because the kernel
63 * only knows when a job is enqueued and not started, it is possible for a
64 * start1, start2, stop1, stop2. Change it back into start1, stop1, start2,
65 * stop2 by queueing up start2 and releasing it when stop1 is received.
68 static int mali_activity_index(int core, int key)
72 for (i = 0; i < ARRAY_SIZE(mali_activities); ++i) {
73 if ((mali_activities[i].core == core) && (mali_activities[i].key == key)) {
76 if ((mali_activities[i].core == 0) && (mali_activities[i].key == 0)) {
77 mali_activities[i].core = core;
78 mali_activities[i].key = key;
82 BUG_ON(i >= ARRAY_SIZE(mali_activities));
87 static void mali_activity_enqueue(int core, int key, int activity, int pid)
92 spin_lock(&mali_activities_lock);
93 i = mali_activity_index(core, key);
95 count = mali_activities[i].count;
97 ++mali_activities[i].count;
99 mali_activities[i].last_activity = activity;
100 mali_activities[i].last_pid = pid;
102 spin_unlock(&mali_activities_lock);
105 gator_marshal_activity_switch(core, key, activity, pid);
109 static void mali_activity_stop(int core, int key)
113 int last_activity = 0;
116 spin_lock(&mali_activities_lock);
117 i = mali_activity_index(core, key);
119 if (mali_activities[i].count == 0) {
120 spin_unlock(&mali_activities_lock);
123 --mali_activities[i].count;
124 count = mali_activities[i].count;
126 last_activity = mali_activities[i].last_activity;
127 last_pid = mali_activities[i].last_pid;
129 spin_unlock(&mali_activities_lock);
131 gator_marshal_activity_switch(core, key, 0, 0);
133 gator_marshal_activity_switch(core, key, last_activity, last_pid);
137 void mali_activity_clear(mali_counter mali_activity[], size_t mali_activity_size)
143 for (activity = 0; activity < mali_activity_size; ++activity) {
144 cores = mali_activity[activity].cores;
148 for (core = 0; core < cores; ++core) {
149 if (mali_activity[activity].enabled) {
150 gator_marshal_activity_switch(core, mali_activity[activity].key, 0, 0);
158 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_T6xx)
159 #include "gator_events_mali_4xx.h"
162 * Taken from MALI_PROFILING_EVENT_CHANNEL_* in Mali DDK.
165 EVENT_CHANNEL_SOFTWARE = 0,
166 EVENT_CHANNEL_VP0 = 1,
167 EVENT_CHANNEL_FP0 = 5,
175 EVENT_CHANNEL_GPU = 21
179 * These events are applicable when the type MALI_PROFILING_EVENT_TYPE_SINGLE is used from the GPU channel
182 EVENT_REASON_SINGLE_GPU_NONE = 0,
183 EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE = 1,
186 mali_counter mali_activity[2];
188 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))
190 unsigned int component, state;
192 // do as much work as possible before disabling interrupts
193 component = (event_id >> 16) & 0xFF; // component is an 8-bit field
194 state = (event_id >> 24) & 0xF; // state is a 4-bit field
197 case EVENT_TYPE_START:
198 if (component == EVENT_CHANNEL_VP0) {
199 /* tgid = d0; pid = d1; */
200 if (mali_activity[1].enabled) {
201 mali_activity_enqueue(0, mali_activity[1].key, 1, d1);
203 } else if (component >= EVENT_CHANNEL_FP0 && component <= EVENT_CHANNEL_FP7) {
204 /* tgid = d0; pid = d1; */
205 if (mali_activity[0].enabled) {
206 mali_activity_enqueue(component - EVENT_CHANNEL_FP0, mali_activity[0].key, 1, d1);
211 case EVENT_TYPE_STOP:
212 if (component == EVENT_CHANNEL_VP0) {
213 if (mali_activity[1].enabled) {
214 mali_activity_stop(0, mali_activity[1].key);
216 } else if (component >= EVENT_CHANNEL_FP0 && component <= EVENT_CHANNEL_FP7) {
217 if (mali_activity[0].enabled) {
218 mali_activity_stop(component - EVENT_CHANNEL_FP0, mali_activity[0].key);
223 case EVENT_TYPE_SINGLE:
224 if (component == EVENT_CHANNEL_GPU) {
225 unsigned int reason = (event_id & 0xffff);
227 if (reason == EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE) {
228 gator_events_mali_log_dvfs_event(d0, d1);
239 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_T6xx)
241 mali_counter mali_activity[3];
243 #if defined(MALI_JOB_SLOTS_EVENT_CHANGED)
244 GATOR_DEFINE_PROBE(mali_job_slots_event, TP_PROTO(unsigned int event_id, unsigned int tgid, unsigned int pid, unsigned char job_id))
246 GATOR_DEFINE_PROBE(mali_job_slots_event, TP_PROTO(unsigned int event_id, unsigned int tgid, unsigned int pid))
249 unsigned int component, state, unit;
250 #if !defined(MALI_JOB_SLOTS_EVENT_CHANGED)
251 unsigned char job_id = 0;
254 component = (event_id >> 16) & 0xFF; // component is an 8-bit field
255 state = (event_id >> 24) & 0xF; // state is a 4-bit field
268 unit = GPU_UNIT_NONE;
271 if (unit != GPU_UNIT_NONE) {
273 case EVENT_TYPE_START:
274 if (mali_activity[component].enabled) {
275 mali_activity_enqueue(0, mali_activity[component].key, 1, (pid != 0 ? pid : tgid));
278 case EVENT_TYPE_STOP:
279 default: // Some jobs can be soft-stopped, so ensure that this terminates the activity trace.
280 if (mali_activity[component].enabled) {
281 mali_activity_stop(0, mali_activity[component].key);
289 static int gator_trace_gpu_start(void)
292 * Returns nonzero for installation failed
293 * Absence of gpu trace points is not an error
296 #if defined(MALI_SUPPORT)
297 memset(&mali_activities, 0, sizeof(mali_activities));
299 mali_timeline_trace_registered = mali_job_slots_trace_registered = 0;
301 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_T6xx)
302 mali_activity_clear(mali_activity, ARRAY_SIZE(mali_activity));
303 if (!GATOR_REGISTER_TRACE(mali_timeline_event)) {
304 mali_timeline_trace_registered = 1;
308 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_T6xx)
309 mali_activity_clear(mali_activity, ARRAY_SIZE(mali_activity));
310 if (!GATOR_REGISTER_TRACE(mali_job_slots_event)) {
311 mali_job_slots_trace_registered = 1;
318 static void gator_trace_gpu_stop(void)
320 #if defined(MALI_SUPPORT) && (MALI_SUPPORT != MALI_T6xx)
321 if (mali_timeline_trace_registered) {
322 GATOR_UNREGISTER_TRACE(mali_timeline_event);
326 #if defined(MALI_SUPPORT) && (MALI_SUPPORT == MALI_T6xx)
327 if (mali_job_slots_trace_registered) {
328 GATOR_UNREGISTER_TRACE(mali_job_slots_event);
332 mali_timeline_trace_registered = mali_job_slots_trace_registered = 0;