2 * Copyright (C) ARM Limited 2011-2013. 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 #include "linux/mali_linux_trace.h"
20 #include "gator_events_mali_common.h"
23 * Check that the MALI_SUPPORT define is set to one of the allowable device codes.
25 #if (MALI_SUPPORT != MALI_T6xx)
26 #error MALI_SUPPORT set to an invalid device code: expecting MALI_T6xx
29 /* Counters for Mali-T6xx:
32 * They are tracepoints, but instead of reporting a number they report a START/STOP event.
33 * They are reported in Streamline as number of microseconds while that particular counter was active.
36 * They are tracepoints reporting a particular number.
37 * They are accumulated in sw_counter_data array until they are passed to Streamline, then they are zeroed.
40 * They are the same as software counters but their value is not zeroed.
43 /* Timeline (start/stop) activity */
44 static const char *timeline_event_names[] = {
79 /* The number of shader blocks in the enum above */
80 #define NUM_PM_SHADER (8)
82 /* Software Counters */
83 static const char *software_counter_names[] = {
97 /* Software Counters */
98 static const char *accumulators_names[] = {
103 TOTAL_ALLOC_PAGES = 0
106 #define FIRST_TIMELINE_EVENT (0)
107 #define NUMBER_OF_TIMELINE_EVENTS (sizeof(timeline_event_names) / sizeof(timeline_event_names[0]))
108 #define FIRST_SOFTWARE_COUNTER (FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS)
109 #define NUMBER_OF_SOFTWARE_COUNTERS (sizeof(software_counter_names) / sizeof(software_counter_names[0]))
110 #define FIRST_ACCUMULATOR (FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS)
111 #define NUMBER_OF_ACCUMULATORS (sizeof(accumulators_names) / sizeof(accumulators_names[0]))
112 #define NUMBER_OF_EVENTS (NUMBER_OF_TIMELINE_EVENTS + NUMBER_OF_SOFTWARE_COUNTERS + NUMBER_OF_ACCUMULATORS)
115 * gatorfs variables for counter enable state
117 static mali_counter counters[NUMBER_OF_EVENTS];
119 /* An array used to return the data we recorded
120 * as key,value pairs hence the *2
122 static unsigned long counter_dump[NUMBER_OF_EVENTS * 2];
125 * Array holding counter start times (in ns) for each counter. A zero here
126 * indicates that the activity monitored by this counter is not running.
128 static struct timespec timeline_event_starttime[NUMBER_OF_TIMELINE_EVENTS];
130 /* The data we have recorded */
131 static unsigned int timeline_data[NUMBER_OF_TIMELINE_EVENTS];
132 static unsigned int sw_counter_data[NUMBER_OF_SOFTWARE_COUNTERS];
133 static unsigned int accumulators_data[NUMBER_OF_ACCUMULATORS];
135 /* Hold the previous timestamp, used to calculate the sample interval. */
136 static struct timespec prev_timestamp;
139 * Returns the timespan (in microseconds) between the two specified timestamps.
141 * @param start Ptr to the start timestamp
142 * @param end Ptr to the end timestamp
144 * @return Number of microseconds between the two timestamps (can be negative if start follows end).
146 static inline long get_duration_us(const struct timespec *start, const struct timespec *end)
148 long event_duration_us = (end->tv_nsec - start->tv_nsec) / 1000;
149 event_duration_us += (end->tv_sec - start->tv_sec) * 1000000;
151 return event_duration_us;
154 static void record_timeline_event(unsigned int timeline_index, unsigned int type)
156 struct timespec event_timestamp;
157 struct timespec *event_start = &timeline_event_starttime[timeline_index];
161 /* Get the event time... */
162 getnstimeofday(&event_timestamp);
164 /* Remember the start time if the activity is not already started */
165 if (event_start->tv_sec == 0) {
166 *event_start = event_timestamp; /* Structure copy */
171 /* if the counter was started... */
172 if (event_start->tv_sec != 0) {
173 /* Get the event time... */
174 getnstimeofday(&event_timestamp);
176 /* Accumulate the duration in us */
177 timeline_data[timeline_index] += get_duration_us(event_start, &event_timestamp);
179 /* Reset the start time to indicate the activity is stopped. */
180 event_start->tv_sec = 0;
185 /* Other activity events are ignored. */
191 * Documentation about the following tracepoints is in mali_linux_trace.h
194 GATOR_DEFINE_PROBE(mali_pm_status, TP_PROTO(unsigned int event_id, unsigned long long value))
196 #define SHADER_PRESENT_LO 0x100 /* (RO) Shader core present bitmap, low word */
197 #define TILER_PRESENT_LO 0x110 /* (RO) Tiler core present bitmap, low word */
198 #define L2_PRESENT_LO 0x120 /* (RO) Level 2 cache present bitmap, low word */
199 #define BIT_AT(value, pos) ((value >> pos) & 1)
201 static unsigned long long previous_shader_bitmask = 0;
202 static unsigned long long previous_tiler_bitmask = 0;
203 static unsigned long long previous_l2_bitmask = 0;
206 case SHADER_PRESENT_LO:
208 unsigned long long changed_bitmask = previous_shader_bitmask ^ value;
211 for (pos = 0; pos < NUM_PM_SHADER; ++pos) {
212 if (BIT_AT(changed_bitmask, pos)) {
213 record_timeline_event(PM_SHADER_0 + pos, BIT_AT(value, pos) ? ACTIVITY_START : ACTIVITY_STOP);
217 previous_shader_bitmask = value;
221 case TILER_PRESENT_LO:
223 unsigned long long changed = previous_tiler_bitmask ^ value;
225 if (BIT_AT(changed, 0)) {
226 record_timeline_event(PM_TILER_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
229 previous_tiler_bitmask = value;
235 unsigned long long changed = previous_l2_bitmask ^ value;
237 if (BIT_AT(changed, 0)) {
238 record_timeline_event(PM_L2_0, BIT_AT(value, 0) ? ACTIVITY_START : ACTIVITY_STOP);
240 if (BIT_AT(changed, 4)) {
241 record_timeline_event(PM_L2_1, BIT_AT(value, 4) ? ACTIVITY_START : ACTIVITY_STOP);
244 previous_l2_bitmask = value;
249 /* No other blocks are supported at present */
253 #undef SHADER_PRESENT_LO
254 #undef TILER_PRESENT_LO
259 GATOR_DEFINE_PROBE(mali_page_fault_insert_pages, TP_PROTO(int event_id, unsigned long value))
261 /* We add to the previous since we may receive many tracepoints in one sample period */
262 sw_counter_data[MMU_PAGE_FAULT_0 + event_id] += value;
265 GATOR_DEFINE_PROBE(mali_mmu_as_in_use, TP_PROTO(int event_id))
267 record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_START);
270 GATOR_DEFINE_PROBE(mali_mmu_as_released, TP_PROTO(int event_id))
272 record_timeline_event(MMU_AS_0 + event_id, ACTIVITY_STOP);
275 GATOR_DEFINE_PROBE(mali_total_alloc_pages_change, TP_PROTO(long long int event_id))
277 accumulators_data[TOTAL_ALLOC_PAGES] = event_id;
280 static int create_files(struct super_block *sb, struct dentry *root)
284 * Create the filesystem for all events
286 int counter_index = 0;
287 const char *mali_name = gator_mali_get_mali_name();
289 for (event = FIRST_TIMELINE_EVENT; event < FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS; event++) {
290 if (gator_mali_create_file_system(mali_name, timeline_event_names[counter_index], sb, root, &counters[event]) != 0) {
296 for (event = FIRST_SOFTWARE_COUNTER; event < FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS; event++) {
297 if (gator_mali_create_file_system(mali_name, software_counter_names[counter_index], sb, root, &counters[event]) != 0) {
303 for (event = FIRST_ACCUMULATOR; event < FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS; event++) {
304 if (gator_mali_create_file_system(mali_name, accumulators_names[counter_index], sb, root, &counters[event]) != 0) {
313 static int register_tracepoints(void)
315 if (GATOR_REGISTER_TRACE(mali_pm_status)) {
316 pr_debug("gator: Mali-T6xx: mali_pm_status tracepoint failed to activate\n");
320 if (GATOR_REGISTER_TRACE(mali_page_fault_insert_pages)) {
321 pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages tracepoint failed to activate\n");
325 if (GATOR_REGISTER_TRACE(mali_mmu_as_in_use)) {
326 pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use tracepoint failed to activate\n");
330 if (GATOR_REGISTER_TRACE(mali_mmu_as_released)) {
331 pr_debug("gator: Mali-T6xx: mali_mmu_as_released tracepoint failed to activate\n");
335 if (GATOR_REGISTER_TRACE(mali_total_alloc_pages_change)) {
336 pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change tracepoint failed to activate\n");
340 pr_debug("gator: Mali-T6xx: start\n");
341 pr_debug("gator: Mali-T6xx: mali_pm_status probe is at %p\n", &probe_mali_pm_status);
342 pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages probe is at %p\n", &probe_mali_page_fault_insert_pages);
343 pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use probe is at %p\n", &probe_mali_mmu_as_in_use);
344 pr_debug("gator: Mali-T6xx: mali_mmu_as_released probe is at %p\n", &probe_mali_mmu_as_released);
345 pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change probe is at %p\n", &probe_mali_total_alloc_pages_change);
350 static int start(void)
354 /* Clean all data for the next capture */
355 for (cnt = 0; cnt < NUMBER_OF_TIMELINE_EVENTS; cnt++) {
356 timeline_event_starttime[cnt].tv_sec = timeline_event_starttime[cnt].tv_nsec = 0;
357 timeline_data[cnt] = 0;
360 for (cnt = 0; cnt < NUMBER_OF_SOFTWARE_COUNTERS; cnt++) {
361 sw_counter_data[cnt] = 0;
364 for (cnt = 0; cnt < NUMBER_OF_ACCUMULATORS; cnt++) {
365 accumulators_data[cnt] = 0;
368 /* Register tracepoints */
369 if (register_tracepoints() == 0) {
374 * Set the first timestamp for calculating the sample interval. The first interval could be quite long,
375 * since it will be the time between 'start' and the first 'read'.
376 * This means that timeline values will be divided by a big number for the first sample.
378 getnstimeofday(&prev_timestamp);
383 static void stop(void)
385 pr_debug("gator: Mali-T6xx: stop\n");
388 * It is safe to unregister traces even if they were not successfully
389 * registered, so no need to check.
391 GATOR_UNREGISTER_TRACE(mali_pm_status);
392 pr_debug("gator: Mali-T6xx: mali_pm_status tracepoint deactivated\n");
394 GATOR_UNREGISTER_TRACE(mali_page_fault_insert_pages);
395 pr_debug("gator: Mali-T6xx: mali_page_fault_insert_pages tracepoint deactivated\n");
397 GATOR_UNREGISTER_TRACE(mali_mmu_as_in_use);
398 pr_debug("gator: Mali-T6xx: mali_mmu_as_in_use tracepoint deactivated\n");
400 GATOR_UNREGISTER_TRACE(mali_mmu_as_released);
401 pr_debug("gator: Mali-T6xx: mali_mmu_as_released tracepoint deactivated\n");
403 GATOR_UNREGISTER_TRACE(mali_total_alloc_pages_change);
404 pr_debug("gator: Mali-T6xx: mali_total_alloc_pages_change tracepoint deactivated\n");
407 static int read(int **buffer)
411 long sample_interval_us = 0;
412 struct timespec read_timestamp;
414 if (!on_primary_core()) {
418 /* Get the start of this sample period. */
419 getnstimeofday(&read_timestamp);
422 * Calculate the sample interval if the previous sample time is valid.
423 * We use tv_sec since it will not be 0.
425 if (prev_timestamp.tv_sec != 0) {
426 sample_interval_us = get_duration_us(&prev_timestamp, &read_timestamp);
429 /* Structure copy. Update the previous timestamp. */
430 prev_timestamp = read_timestamp;
433 * Report the timeline counters (ACTIVITY_START/STOP)
435 for (cnt = FIRST_TIMELINE_EVENT; cnt < (FIRST_TIMELINE_EVENT + NUMBER_OF_TIMELINE_EVENTS); cnt++) {
436 mali_counter *counter = &counters[cnt];
437 if (counter->enabled) {
438 const int index = cnt - FIRST_TIMELINE_EVENT;
441 /* If the activity is still running, reset its start time to the start of this sample period
442 * to correct the count. Add the time up to the end of the sample onto the count. */
443 if (timeline_event_starttime[index].tv_sec != 0) {
444 const long event_duration = get_duration_us(&timeline_event_starttime[index], &read_timestamp);
445 timeline_data[index] += event_duration;
446 timeline_event_starttime[index] = read_timestamp; /* Activity is still running. */
449 if (sample_interval_us != 0) {
450 /* Convert the counter to a percent-of-sample value */
451 value = (timeline_data[index] * 100) / sample_interval_us;
453 pr_debug("gator: Mali-T6xx: setting value to zero\n");
457 /* Clear the counter value ready for the next sample. */
458 timeline_data[index] = 0;
460 counter_dump[len++] = counter->key;
461 counter_dump[len++] = value;
465 /* Report the software counters */
466 for (cnt = FIRST_SOFTWARE_COUNTER; cnt < (FIRST_SOFTWARE_COUNTER + NUMBER_OF_SOFTWARE_COUNTERS); cnt++) {
467 const mali_counter *counter = &counters[cnt];
468 if (counter->enabled) {
469 const int index = cnt - FIRST_SOFTWARE_COUNTER;
470 counter_dump[len++] = counter->key;
471 counter_dump[len++] = sw_counter_data[index];
472 /* Set the value to zero for the next time */
473 sw_counter_data[index] = 0;
477 /* Report the accumulators */
478 for (cnt = FIRST_ACCUMULATOR; cnt < (FIRST_ACCUMULATOR + NUMBER_OF_ACCUMULATORS); cnt++) {
479 const mali_counter *counter = &counters[cnt];
480 if (counter->enabled) {
481 const int index = cnt - FIRST_ACCUMULATOR;
482 counter_dump[len++] = counter->key;
483 counter_dump[len++] = accumulators_data[index];
484 /* Do not zero the accumulator */
488 /* Update the buffer */
490 *buffer = (int *)counter_dump;
496 static struct gator_interface gator_events_mali_t6xx_interface = {
497 .create_files = create_files,
503 extern int gator_events_mali_t6xx_init(void)
505 pr_debug("gator: Mali-T6xx: sw_counters init\n");
507 gator_mali_initialise_counters(counters, NUMBER_OF_EVENTS);
509 return gator_events_install(&gator_events_mali_t6xx_interface);
512 gator_events_init(gator_events_mali_t6xx_init);