2 * Copyright (C) ARM Limited 2011-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.
12 #include <linux/module.h>
13 #include <linux/time.h>
14 #include <linux/math64.h>
15 #include <linux/slab.h>
18 #ifdef MALI_DIR_MIDGARD
19 /* New DDK Directory structure with kernel/drivers/gpu/arm/midgard*/
20 #include "mali_linux_trace.h"
22 /* Old DDK Directory structure with kernel/drivers/gpu/arm/t6xx*/
23 #include "linux/mali_linux_trace.h"
26 #include "gator_events_mali_common.h"
29 * Check that the MALI_SUPPORT define is set to one of the allowable device codes.
31 #if (MALI_SUPPORT != MALI_MIDGARD)
32 #error MALI_SUPPORT set to an invalid device code: expecting MALI_MIDGARD
35 static const char mali_name[] = "Midgard";
37 /* Counters for Mali-Midgard:
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.
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.
48 * They are the same as software counters but their value is not zeroed.
51 /* Timeline (start/stop) activity */
52 static const char *const timeline_event_names[] = {
87 /* The number of shader blocks in the enum above */
88 #define NUM_PM_SHADER (8)
90 /* Software Counters */
91 static const char *const software_counter_names[] = {
105 /* Software Counters */
106 static const char *const accumulators_names[] = {
111 TOTAL_ALLOC_PAGES = 0
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)
124 * gatorfs variables for counter enable state
126 static struct mali_counter counters[NUMBER_OF_EVENTS];
127 static unsigned long filmstrip_event;
129 /* An array used to return the data we recorded
130 * as key,value pairs hence the *2
132 static int counter_dump[NUMBER_OF_EVENTS * 2];
135 * Array holding counter start times (in ns) for each counter. A zero
136 * here indicates that the activity monitored by this counter is not
139 static struct timespec timeline_event_starttime[NUMBER_OF_TIMELINE_EVENTS];
141 /* The data we have recorded */
142 static unsigned int timeline_data[NUMBER_OF_TIMELINE_EVENTS];
143 static unsigned int sw_counter_data[NUMBER_OF_SOFTWARE_COUNTERS];
144 static unsigned int accumulators_data[NUMBER_OF_ACCUMULATORS];
146 /* Hold the previous timestamp, used to calculate the sample interval. */
147 static struct timespec prev_timestamp;
150 * Returns the timespan (in microseconds) between the two specified timestamps.
152 * @param start Ptr to the start timestamp
153 * @param end Ptr to the end timestamp
155 * @return Number of microseconds between the two timestamps (can be negative if start follows end).
157 static inline long get_duration_us(const struct timespec *start, const struct timespec *end)
159 long event_duration_us = (end->tv_nsec - start->tv_nsec) / 1000;
161 event_duration_us += (end->tv_sec - start->tv_sec) * 1000000;
163 return event_duration_us;
166 static void record_timeline_event(unsigned int timeline_index, unsigned int type)
168 struct timespec event_timestamp;
169 struct timespec *event_start = &timeline_event_starttime[timeline_index];
173 /* Get the event time... */
174 getnstimeofday(&event_timestamp);
176 /* Remember the start time if the activity is not already started */
177 if (event_start->tv_sec == 0)
178 *event_start = event_timestamp; /* Structure copy */
182 /* if the counter was started... */
183 if (event_start->tv_sec != 0) {
184 /* Get the event time... */
185 getnstimeofday(&event_timestamp);
187 /* Accumulate the duration in us */
188 timeline_data[timeline_index] += get_duration_us(event_start, &event_timestamp);
190 /* Reset the start time to indicate the activity is stopped. */
191 event_start->tv_sec = 0;
196 /* Other activity events are ignored. */
202 * Documentation about the following tracepoints is in mali_linux_trace.h
205 GATOR_DEFINE_PROBE(mali_pm_status, TP_PROTO(unsigned int event_id, unsigned long long value))
207 #define SHADER_PRESENT_LO 0x100 /* (RO) Shader core present bitmap, low word */
208 #define TILER_PRESENT_LO 0x110 /* (RO) Tiler core present bitmap, low word */
209 #define L2_PRESENT_LO 0x120 /* (RO) Level 2 cache present bitmap, low word */
210 #define BIT_AT(value, pos) ((value >> pos) & 1)
212 static unsigned long long previous_shader_bitmask;
213 static unsigned long long previous_tiler_bitmask;
214 static unsigned long long previous_l2_bitmask;
217 case SHADER_PRESENT_LO:
219 unsigned long long changed_bitmask = previous_shader_bitmask ^ value;
222 for (pos = 0; pos < NUM_PM_SHADER; ++pos) {
223 if (BIT_AT(changed_bitmask, pos))
224 record_timeline_event(PM_SHADER_0 + pos, BIT_AT(value, pos) ? ACTIVITY_START : ACTIVITY_STOP);
227 previous_shader_bitmask = value;
231 case TILER_PRESENT_LO:
233 unsigned long long changed = previous_tiler_bitmask ^ value;
235 if (BIT_AT(changed, 0))
236 record_timeline_event(PM_TILER_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
238 previous_tiler_bitmask = value;
244 unsigned long long changed = previous_l2_bitmask ^ value;
246 if (BIT_AT(changed, 0))
247 record_timeline_event(PM_L2_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
248 if (BIT_AT(changed, 4))
249 record_timeline_event(PM_L2_1, BIT_AT(value, 4) ? ACTIVITY_START : ACTIVITY_STOP);
251 previous_l2_bitmask = value;
256 /* No other blocks are supported at present */
260 #undef SHADER_PRESENT_LO
261 #undef TILER_PRESENT_LO
266 GATOR_DEFINE_PROBE(mali_page_fault_insert_pages, TP_PROTO(int event_id, unsigned long value))
268 /* We add to the previous since we may receive many tracepoints in one sample period */
269 sw_counter_data[MMU_PAGE_FAULT_0 + event_id] += value;
272 GATOR_DEFINE_PROBE(mali_mmu_as_in_use, TP_PROTO(int event_id))
274 record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_START);
277 GATOR_DEFINE_PROBE(mali_mmu_as_released, TP_PROTO(int event_id))
279 record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_STOP);
282 GATOR_DEFINE_PROBE(mali_total_alloc_pages_change, TP_PROTO(long long int event_id))
284 accumulators_data[TOTAL_ALLOC_PAGES] = event_id;
287 static int create_files(struct super_block *sb, struct dentry *root)
291 * Create the filesystem for all events
293 int counter_index = 0;
294 mali_profiling_control_type *mali_control;
296 for (event = FIRST_TIMELINE_EVENT; event < FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS; event++) {
297 if (gator_mali_create_file_system(mali_name, timeline_event_names[counter_index], sb, root, &counters[event], NULL) != 0)
302 for (event = FIRST_SOFTWARE_COUNTER; event < FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS; event++) {
303 if (gator_mali_create_file_system(mali_name, software_counter_names[counter_index], sb, root, &counters[event], NULL) != 0)
308 for (event = FIRST_ACCUMULATOR; event < FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS; event++) {
309 if (gator_mali_create_file_system(mali_name, accumulators_names[counter_index], sb, root, &counters[event], NULL) != 0)
314 mali_control = symbol_get(_mali_profiling_control);
316 if (gator_mali_create_file_system(mali_name, "Filmstrip_cnt0", sb, root, &counters[FILMSTRIP], &filmstrip_event) != 0)
318 symbol_put(_mali_profiling_control);
324 static int register_tracepoints(void)
326 if (GATOR_REGISTER_TRACE(mali_pm_status)) {
327 pr_debug("gator: Mali-Midgard: mali_pm_status tracepoint failed to activate\n");
331 if (GATOR_REGISTER_TRACE(mali_page_fault_insert_pages)) {
332 pr_debug("gator: Mali-Midgard: mali_page_fault_insert_pages tracepoint failed to activate\n");
336 if (GATOR_REGISTER_TRACE(mali_mmu_as_in_use)) {
337 pr_debug("gator: Mali-Midgard: mali_mmu_as_in_use tracepoint failed to activate\n");
341 if (GATOR_REGISTER_TRACE(mali_mmu_as_released)) {
342 pr_debug("gator: Mali-Midgard: mali_mmu_as_released tracepoint failed to activate\n");
346 if (GATOR_REGISTER_TRACE(mali_total_alloc_pages_change)) {
347 pr_debug("gator: Mali-Midgard: mali_total_alloc_pages_change tracepoint failed to activate\n");
351 pr_debug("gator: Mali-Midgard: start\n");
352 pr_debug("gator: Mali-Midgard: mali_pm_status probe is at %p\n", &probe_mali_pm_status);
353 pr_debug("gator: Mali-Midgard: mali_page_fault_insert_pages probe is at %p\n", &probe_mali_page_fault_insert_pages);
354 pr_debug("gator: Mali-Midgard: mali_mmu_as_in_use probe is at %p\n", &probe_mali_mmu_as_in_use);
355 pr_debug("gator: Mali-Midgard: mali_mmu_as_released probe is at %p\n", &probe_mali_mmu_as_released);
356 pr_debug("gator: Mali-Midgard: mali_total_alloc_pages_change probe is at %p\n", &probe_mali_total_alloc_pages_change);
361 static int start(void)
364 mali_profiling_control_type *mali_control;
366 /* Clean all data for the next capture */
367 for (cnt = 0; cnt < NUMBER_OF_TIMELINE_EVENTS; cnt++) {
368 timeline_event_starttime[cnt].tv_sec = timeline_event_starttime[cnt].tv_nsec = 0;
369 timeline_data[cnt] = 0;
372 for (cnt = 0; cnt < NUMBER_OF_SOFTWARE_COUNTERS; cnt++)
373 sw_counter_data[cnt] = 0;
375 for (cnt = 0; cnt < NUMBER_OF_ACCUMULATORS; cnt++)
376 accumulators_data[cnt] = 0;
378 /* Register tracepoints */
379 if (register_tracepoints() == 0)
382 /* Generic control interface for Mali DDK. */
383 mali_control = symbol_get(_mali_profiling_control);
385 /* The event attribute in the XML file keeps the actual frame rate. */
386 unsigned int enabled = counters[FILMSTRIP].enabled ? 1 : 0;
387 unsigned int rate = filmstrip_event & 0xff;
388 unsigned int resize_factor = (filmstrip_event >> 8) & 0xff;
390 pr_debug("gator: mali online _mali_profiling_control symbol @ %p\n", mali_control);
392 #define FBDUMP_CONTROL_ENABLE (1)
393 #define FBDUMP_CONTROL_RATE (2)
394 #define FBDUMP_CONTROL_RESIZE_FACTOR (4)
395 mali_control(FBDUMP_CONTROL_ENABLE, enabled);
396 mali_control(FBDUMP_CONTROL_RATE, rate);
397 mali_control(FBDUMP_CONTROL_RESIZE_FACTOR, resize_factor);
399 pr_debug("gator: sent mali_control enabled=%d, rate=%d, resize_factor=%d\n", enabled, rate, resize_factor);
401 symbol_put(_mali_profiling_control);
403 pr_err("gator: mali online _mali_profiling_control symbol not found\n");
407 * Set the first timestamp for calculating the sample interval. The first interval could be quite long,
408 * since it will be the time between 'start' and the first 'read'.
409 * This means that timeline values will be divided by a big number for the first sample.
411 getnstimeofday(&prev_timestamp);
416 static void stop(void)
418 mali_profiling_control_type *mali_control;
420 pr_debug("gator: Mali-Midgard: stop\n");
423 * It is safe to unregister traces even if they were not successfully
424 * registered, so no need to check.
426 GATOR_UNREGISTER_TRACE(mali_pm_status);
427 pr_debug("gator: Mali-Midgard: mali_pm_status tracepoint deactivated\n");
429 GATOR_UNREGISTER_TRACE(mali_page_fault_insert_pages);
430 pr_debug("gator: Mali-Midgard: mali_page_fault_insert_pages tracepoint deactivated\n");
432 GATOR_UNREGISTER_TRACE(mali_mmu_as_in_use);
433 pr_debug("gator: Mali-Midgard: mali_mmu_as_in_use tracepoint deactivated\n");
435 GATOR_UNREGISTER_TRACE(mali_mmu_as_released);
436 pr_debug("gator: Mali-Midgard: mali_mmu_as_released tracepoint deactivated\n");
438 GATOR_UNREGISTER_TRACE(mali_total_alloc_pages_change);
439 pr_debug("gator: Mali-Midgard: mali_total_alloc_pages_change tracepoint deactivated\n");
441 /* Generic control interface for Mali DDK. */
442 mali_control = symbol_get(_mali_profiling_control);
444 pr_debug("gator: mali offline _mali_profiling_control symbol @ %p\n", mali_control);
446 mali_control(FBDUMP_CONTROL_ENABLE, 0);
448 symbol_put(_mali_profiling_control);
450 pr_err("gator: mali offline _mali_profiling_control symbol not found\n");
454 static int read(int **buffer, bool sched_switch)
458 long sample_interval_us = 0;
459 struct timespec read_timestamp;
461 if (!on_primary_core())
464 /* Get the start of this sample period. */
465 getnstimeofday(&read_timestamp);
468 * Calculate the sample interval if the previous sample time is valid.
469 * We use tv_sec since it will not be 0.
471 if (prev_timestamp.tv_sec != 0)
472 sample_interval_us = get_duration_us(&prev_timestamp, &read_timestamp);
474 /* Structure copy. Update the previous timestamp. */
475 prev_timestamp = read_timestamp;
478 * Report the timeline counters (ACTIVITY_START/STOP)
480 for (cnt = FIRST_TIMELINE_EVENT; cnt < (FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS); cnt++) {
481 struct mali_counter *counter = &counters[cnt];
483 if (counter->enabled) {
484 const int index = cnt - FIRST_TIMELINE_EVENT;
487 /* If the activity is still running, reset its start time to the
488 * start of this sample period to correct the count. Add the
489 * time up to the end of the sample onto the count.
491 if (timeline_event_starttime[index].tv_sec != 0) {
492 const long event_duration = get_duration_us(&timeline_event_starttime[index], &read_timestamp);
494 timeline_data[index] += event_duration;
495 timeline_event_starttime[index] = read_timestamp; /* Activity is still running. */
498 if (sample_interval_us != 0) {
499 /* Convert the counter to a percent-of-sample value */
500 value = (timeline_data[index] * 100) / sample_interval_us;
502 pr_debug("gator: Mali-Midgard: setting value to zero\n");
506 /* Clear the counter value ready for the next sample. */
507 timeline_data[index] = 0;
509 counter_dump[len++] = counter->key;
510 counter_dump[len++] = value;
514 /* Report the software counters */
515 for (cnt = FIRST_SOFTWARE_COUNTER; cnt < (FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS); cnt++) {
516 const struct mali_counter *counter = &counters[cnt];
518 if (counter->enabled) {
519 const int index = cnt - FIRST_SOFTWARE_COUNTER;
521 counter_dump[len++] = counter->key;
522 counter_dump[len++] = sw_counter_data[index];
523 /* Set the value to zero for the next time */
524 sw_counter_data[index] = 0;
528 /* Report the accumulators */
529 for (cnt = FIRST_ACCUMULATOR; cnt < (FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS); cnt++) {
530 const struct mali_counter *counter = &counters[cnt];
532 if (counter->enabled) {
533 const int index = cnt - FIRST_ACCUMULATOR;
535 counter_dump[len++] = counter->key;
536 counter_dump[len++] = accumulators_data[index];
537 /* Do not zero the accumulator */
541 /* Update the buffer */
543 *buffer = counter_dump;
548 static struct gator_interface gator_events_mali_midgard_interface = {
549 .create_files = create_files,
555 extern int gator_events_mali_midgard_init(void)
557 pr_debug("gator: Mali-Midgard: sw_counters init\n");
559 gator_mali_initialise_counters(counters, NUMBER_OF_EVENTS);
561 return gator_events_install(&gator_events_mali_midgard_interface);