UPSTREAM: DT/arm,gic-v3: Documment PPI partition support
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / linux / mali_osk_profiling.c
1 /*
2  * Copyright (C) 2012-2014 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #include <linux/module.h>
12
13 #include <mali_profiling_gator_api.h>
14 #include "mali_kernel_common.h"
15 #include "mali_osk.h"
16 #include "mali_ukk.h"
17 #include "mali_uk_types.h"
18 #include "mali_osk_profiling.h"
19 #include "mali_linux_trace.h"
20 #include "mali_gp.h"
21 #include "mali_pp.h"
22 #include "mali_l2_cache.h"
23 #include "mali_user_settings_db.h"
24 #include "mali_executor.h"
25
26 _mali_osk_errcode_t _mali_osk_profiling_init(mali_bool auto_start)
27 {
28         if (MALI_TRUE == auto_start) {
29                 mali_set_user_setting(_MALI_UK_USER_SETTING_SW_EVENTS_ENABLE, MALI_TRUE);
30         }
31
32         return _MALI_OSK_ERR_OK;
33 }
34
35 void _mali_osk_profiling_term(void)
36 {
37         /* Nothing to do */
38 }
39
40 void _mali_osk_profiling_report_sw_counters(u32 *counters)
41 {
42         trace_mali_sw_counters(_mali_osk_get_pid(), _mali_osk_get_tid(), NULL, counters);
43 }
44
45 void _mali_osk_profiling_memory_usage_get(u32 *memory_usage)
46 {
47         *memory_usage = _mali_ukk_report_memory_usage();
48 }
49
50 _mali_osk_errcode_t _mali_ukk_profiling_add_event(_mali_uk_profiling_add_event_s *args)
51 {
52         /* Always add process and thread identificator in the first two data elements for events from user space */
53         _mali_osk_profiling_add_event(args->event_id, _mali_osk_get_pid(), _mali_osk_get_tid(), args->data[2], args->data[3], args->data[4]);
54
55         return _MALI_OSK_ERR_OK;
56 }
57
58 _mali_osk_errcode_t _mali_ukk_sw_counters_report(_mali_uk_sw_counters_report_s *args)
59 {
60         u32 *counters = (u32 *)(uintptr_t)args->counters;
61
62         _mali_osk_profiling_report_sw_counters(counters);
63
64         return _MALI_OSK_ERR_OK;
65 }
66
67 _mali_osk_errcode_t _mali_ukk_profiling_memory_usage_get(_mali_uk_profiling_memory_usage_get_s *args)
68 {
69         _mali_osk_profiling_memory_usage_get(&args->memory_usage);
70         return _MALI_OSK_ERR_OK;
71 }
72
73 /**
74  * Called by gator.ko to set HW counters
75  *
76  * @param counter_id The counter ID.
77  * @param event_id Event ID that the counter should count (HW counter value from TRM).
78  *
79  * @return 1 on success, 0 on failure.
80  */
81 int _mali_profiling_set_event(u32 counter_id, s32 event_id)
82 {
83         if (COUNTER_VP_0_C0 == counter_id) {
84                 mali_gp_job_set_gp_counter_src0(event_id);
85         } else if (COUNTER_VP_0_C1 == counter_id) {
86                 mali_gp_job_set_gp_counter_src1(event_id);
87         } else if (COUNTER_FP_0_C0 <= counter_id && COUNTER_FP_7_C1 >= counter_id) {
88                 /*
89                  * Two compatibility notes for this function:
90                  *
91                  * 1) Previously the DDK allowed per core counters.
92                  *
93                  *    This did not make much sense on Mali-450 with the "virtual PP core" concept,
94                  *    so this option was removed, and only the same pair of HW counters was allowed on all cores,
95                  *    beginning with r3p2 release.
96                  *
97                  *    Starting with r4p0, it is now possible to set different HW counters for the different sub jobs.
98                  *    This should be almost the same, since sub job 0 is designed to run on core 0,
99                  *    sub job 1 on core 1, and so on.
100                  *
101                  *    The scheduling of PP sub jobs is not predictable, and this often led to situations where core 0 ran 2
102                  *    sub jobs, while for instance core 1 ran zero. Having the counters set per sub job would thus increase
103                  *    the predictability of the returned data (as you would be guaranteed data for all the selected HW counters).
104                  *
105                  *    PS: Core scaling needs to be disabled in order to use this reliably (goes for both solutions).
106                  *
107                  *    The framework/#defines with Gator still indicates that the counter is for a particular core,
108                  *    but this is internally used as a sub job ID instead (no translation needed).
109                  *
110                  *  2) Global/default vs per sub job counters
111                  *
112                  *     Releases before r3p2 had only per PP core counters.
113                  *     r3p2 releases had only one set of default/global counters which applied to all PP cores
114                  *     Starting with r4p0, we have both a set of default/global counters,
115                  *     and individual counters per sub job (equal to per core).
116                  *
117                  *     To keep compatibility with Gator/DS-5/streamline, the following scheme is used:
118                  *
119                  *     r3p2 release; only counters set for core 0 is handled,
120                  *     this is applied as the default/global set of counters, and will thus affect all cores.
121                  *
122                  *     r4p0 release; counters set for core 0 is applied as both the global/default set of counters,
123                  *     and counters for sub job 0.
124                  *     Counters set for core 1-7 is only applied for the corresponding sub job.
125                  *
126                  *     This should allow the DS-5/Streamline GUI to have a simple mode where it only allows setting the
127                  *     values for core 0, and thus this will be applied to all PP sub jobs/cores.
128                  *     Advanced mode will also be supported, where individual pairs of HW counters can be selected.
129                  *
130                  *     The GUI will (until it is updated) still refer to cores instead of sub jobs, but this is probably
131                  *     something we can live with!
132                  *
133                  *     Mali-450 note: Each job is not divided into a deterministic number of sub jobs, as the HW DLBU
134                  *     automatically distributes the load between whatever number of cores is available at this particular time.
135                  *     A normal PP job on Mali-450 is thus considered a single (virtual) job, and it will thus only be possible
136                  *     to use a single pair of HW counters (even if the job ran on multiple PP cores).
137                  *     In other words, only the global/default pair of PP HW counters will be used for normal Mali-450 jobs.
138                  */
139                 u32 sub_job = (counter_id - COUNTER_FP_0_C0) >> 1;
140                 u32 counter_src = (counter_id - COUNTER_FP_0_C0) & 1;
141                 if (0 == counter_src) {
142                         mali_pp_job_set_pp_counter_sub_job_src0(sub_job, event_id);
143                         if (0 == sub_job) {
144                                 mali_pp_job_set_pp_counter_global_src0(event_id);
145                         }
146                 } else {
147                         mali_pp_job_set_pp_counter_sub_job_src1(sub_job, event_id);
148                         if (0 == sub_job) {
149                                 mali_pp_job_set_pp_counter_global_src1(event_id);
150                         }
151                 }
152         } else if (COUNTER_L2_0_C0 <= counter_id && COUNTER_L2_2_C1 >= counter_id) {
153                 u32 core_id = (counter_id - COUNTER_L2_0_C0) >> 1;
154                 struct mali_l2_cache_core *l2_cache_core = mali_l2_cache_core_get_glob_l2_core(core_id);
155
156                 if (NULL != l2_cache_core) {
157                         u32 counter_src = (counter_id - COUNTER_L2_0_C0) & 1;
158                         mali_l2_cache_core_set_counter_src(l2_cache_core,
159                                                            counter_src, event_id);
160                 }
161         } else {
162                 return 0; /* Failure, unknown event */
163         }
164
165         return 1; /* success */
166 }
167
168 /**
169  * Called by gator.ko to retrieve the L2 cache counter values for all L2 cache cores.
170  * The L2 cache counters are unique in that they are polled by gator, rather than being
171  * transmitted via the tracepoint mechanism.
172  *
173  * @param values Pointer to a _mali_profiling_l2_counter_values structure where
174  *               the counter sources and values will be output
175  * @return 0 if all went well; otherwise, return the mask with the bits set for the powered off cores
176  */
177 u32 _mali_profiling_get_l2_counters(_mali_profiling_l2_counter_values *values)
178 {
179         u32 l2_cores_num = mali_l2_cache_core_get_glob_num_l2_cores();
180         u32 i;
181
182         MALI_DEBUG_ASSERT(l2_cores_num <= 3);
183
184         for (i = 0; i < l2_cores_num; i++) {
185                 struct mali_l2_cache_core *l2_cache = mali_l2_cache_core_get_glob_l2_core(i);
186
187                 if (NULL == l2_cache) {
188                         continue;
189                 }
190
191                 mali_l2_cache_core_get_counter_values(l2_cache,
192                                                       &values->cores[i].source0,
193                                                       &values->cores[i].value0,
194                                                       &values->cores[i].source1,
195                                                       &values->cores[i].value1);
196         }
197
198         return 0;
199 }
200
201 /**
202  * Called by gator to control the production of profiling information at runtime.
203  */
204 void _mali_profiling_control(u32 action, u32 value)
205 {
206         switch (action) {
207         case FBDUMP_CONTROL_ENABLE:
208                 mali_set_user_setting(_MALI_UK_USER_SETTING_COLORBUFFER_CAPTURE_ENABLED, (value == 0 ? MALI_FALSE : MALI_TRUE));
209                 break;
210         case FBDUMP_CONTROL_RATE:
211                 mali_set_user_setting(_MALI_UK_USER_SETTING_BUFFER_CAPTURE_N_FRAMES, value);
212                 break;
213         case SW_COUNTER_ENABLE:
214                 mali_set_user_setting(_MALI_UK_USER_SETTING_SW_COUNTER_ENABLED, value);
215                 break;
216         case FBDUMP_CONTROL_RESIZE_FACTOR:
217                 mali_set_user_setting(_MALI_UK_USER_SETTING_BUFFER_CAPTURE_RESIZE_FACTOR, value);
218                 break;
219         default:
220                 break;  /* Ignore unimplemented actions */
221         }
222 }
223
224 /**
225  * Called by gator to get mali api version.
226  */
227 u32 _mali_profiling_get_api_version(void)
228 {
229         return MALI_PROFILING_API_VERSION;
230 }
231
232 /**
233 * Called by gator to get the data about Mali instance in use:
234 * product id, version, number of cores
235 */
236 void _mali_profiling_get_mali_version(struct _mali_profiling_mali_version *values)
237 {
238         values->mali_product_id = (u32)mali_kernel_core_get_product_id();
239         values->mali_version_major = mali_kernel_core_get_gpu_major_version();
240         values->mali_version_minor = mali_kernel_core_get_gpu_minor_version();
241         values->num_of_l2_cores = mali_l2_cache_core_get_glob_num_l2_cores();
242         values->num_of_fp_cores = mali_executor_get_num_cores_total();
243         values->num_of_vp_cores = 1;
244 }
245
246
247 EXPORT_SYMBOL(_mali_profiling_set_event);
248 EXPORT_SYMBOL(_mali_profiling_get_l2_counters);
249 EXPORT_SYMBOL(_mali_profiling_control);
250 EXPORT_SYMBOL(_mali_profiling_get_api_version);
251 EXPORT_SYMBOL(_mali_profiling_get_mali_version);