tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / r4p0_rel0 / linux / mali_profiling_internal.c
1 /*
2  * Copyright (C) 2010-2012 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 "mali_kernel_common.h"
12 #include "mali_osk.h"
13 #include "mali_osk_mali.h"
14 #include "mali_ukk.h"
15 #include "mali_timestamp.h"
16 #include "mali_osk_profiling.h"
17 #include "mali_user_settings_db.h"
18 #include "mali_profiling_internal.h"
19
20 typedef struct mali_profiling_entry {
21         u64 timestamp;
22         u32 event_id;
23         u32 data[5];
24 } mali_profiling_entry;
25
26 typedef enum mali_profiling_state {
27         MALI_PROFILING_STATE_UNINITIALIZED,
28         MALI_PROFILING_STATE_IDLE,
29         MALI_PROFILING_STATE_RUNNING,
30         MALI_PROFILING_STATE_RETURN,
31 } mali_profiling_state;
32
33 static _mali_osk_mutex_t *lock = NULL;
34 static mali_profiling_state prof_state = MALI_PROFILING_STATE_UNINITIALIZED;
35 static mali_profiling_entry* profile_entries = NULL;
36 static _mali_osk_atomic_t profile_insert_index;
37 static u32 profile_mask = 0;
38
39 static inline void add_event(u32 event_id, u32 data0, u32 data1, u32 data2, u32 data3, u32 data4);
40
41 void probe_mali_timeline_event(void *data, TP_PROTO(unsigned int event_id, unsigned int d0, unsigned int d1, unsigned
42                                int d2, unsigned int d3, unsigned int d4))
43 {
44         add_event(event_id, d0, d1, d2, d3, d4);
45 }
46
47 _mali_osk_errcode_t _mali_internal_profiling_init(mali_bool auto_start)
48 {
49         profile_entries = NULL;
50         profile_mask = 0;
51         _mali_osk_atomic_init(&profile_insert_index, 0);
52
53         lock = _mali_osk_mutex_init(_MALI_OSK_LOCKFLAG_ORDERED, _MALI_OSK_LOCK_ORDER_PROFILING);
54         if (NULL == lock) {
55                 return _MALI_OSK_ERR_FAULT;
56         }
57
58         prof_state = MALI_PROFILING_STATE_IDLE;
59
60         if (MALI_TRUE == auto_start) {
61                 u32 limit = MALI_PROFILING_MAX_BUFFER_ENTRIES; /* Use maximum buffer size */
62
63                 mali_set_user_setting(_MALI_UK_USER_SETTING_SW_EVENTS_ENABLE, MALI_TRUE);
64                 if (_MALI_OSK_ERR_OK != _mali_internal_profiling_start(&limit)) {
65                         return _MALI_OSK_ERR_FAULT;
66                 }
67         }
68
69         return _MALI_OSK_ERR_OK;
70 }
71
72 void _mali_internal_profiling_term(void)
73 {
74         u32 count;
75
76         /* Ensure profiling is stopped */
77         _mali_internal_profiling_stop(&count);
78
79         prof_state = MALI_PROFILING_STATE_UNINITIALIZED;
80
81         if (NULL != profile_entries) {
82                 _mali_osk_vfree(profile_entries);
83                 profile_entries = NULL;
84         }
85
86         if (NULL != lock) {
87                 _mali_osk_mutex_term(lock);
88                 lock = NULL;
89         }
90 }
91
92 _mali_osk_errcode_t _mali_internal_profiling_start(u32 * limit)
93 {
94         _mali_osk_errcode_t ret;
95         mali_profiling_entry *new_profile_entries;
96
97         _mali_osk_mutex_wait(lock);
98
99         if (MALI_PROFILING_STATE_RUNNING == prof_state) {
100                 _mali_osk_mutex_signal(lock);
101                 return _MALI_OSK_ERR_BUSY;
102         }
103
104         new_profile_entries = _mali_osk_valloc(*limit * sizeof(mali_profiling_entry));
105
106         if (NULL == new_profile_entries) {
107                 _mali_osk_vfree(new_profile_entries);
108                 return _MALI_OSK_ERR_NOMEM;
109         }
110
111         if (MALI_PROFILING_MAX_BUFFER_ENTRIES < *limit) {
112                 *limit = MALI_PROFILING_MAX_BUFFER_ENTRIES;
113         }
114
115         profile_mask = 1;
116         while (profile_mask <= *limit) {
117                 profile_mask <<= 1;
118         }
119         profile_mask >>= 1;
120
121         *limit = profile_mask;
122
123         profile_mask--; /* turns the power of two into a mask of one less */
124
125         if (MALI_PROFILING_STATE_IDLE != prof_state) {
126                 _mali_osk_mutex_signal(lock);
127                 _mali_osk_vfree(new_profile_entries);
128                 return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
129         }
130
131         profile_entries = new_profile_entries;
132
133         ret = _mali_timestamp_reset();
134
135         if (_MALI_OSK_ERR_OK == ret) {
136                 prof_state = MALI_PROFILING_STATE_RUNNING;
137         } else {
138                 _mali_osk_vfree(profile_entries);
139                 profile_entries = NULL;
140         }
141
142         register_trace_mali_timeline_event(probe_mali_timeline_event, NULL);
143
144         _mali_osk_mutex_signal(lock);
145         return ret;
146 }
147
148 static inline void add_event(u32 event_id, u32 data0, u32 data1, u32 data2, u32 data3, u32 data4)
149 {
150         u32 cur_index = (_mali_osk_atomic_inc_return(&profile_insert_index) - 1) & profile_mask;
151
152         profile_entries[cur_index].timestamp = _mali_timestamp_get();
153         profile_entries[cur_index].event_id = event_id;
154         profile_entries[cur_index].data[0] = data0;
155         profile_entries[cur_index].data[1] = data1;
156         profile_entries[cur_index].data[2] = data2;
157         profile_entries[cur_index].data[3] = data3;
158         profile_entries[cur_index].data[4] = data4;
159
160         /* If event is "leave API function", add current memory usage to the event
161          * as data point 4.  This is used in timeline profiling to indicate how
162          * much memory was used when leaving a function. */
163         if (event_id == (MALI_PROFILING_EVENT_TYPE_SINGLE|MALI_PROFILING_EVENT_CHANNEL_SOFTWARE|MALI_PROFILING_EVENT_REASON_SINGLE_SW_LEAVE_API_FUNC)) {
164                 profile_entries[cur_index].data[4] = _mali_ukk_report_memory_usage();
165         }
166 }
167
168 _mali_osk_errcode_t _mali_internal_profiling_stop(u32 * count)
169 {
170         _mali_osk_mutex_wait(lock);
171
172         if (MALI_PROFILING_STATE_RUNNING != prof_state) {
173                 _mali_osk_mutex_signal(lock);
174                 return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
175         }
176
177         /* go into return state (user to retreive events), no more events will be added after this */
178         prof_state = MALI_PROFILING_STATE_RETURN;
179
180         unregister_trace_mali_timeline_event(probe_mali_timeline_event, NULL);
181
182         _mali_osk_mutex_signal(lock);
183
184         tracepoint_synchronize_unregister();
185
186         *count = _mali_osk_atomic_read(&profile_insert_index);
187         if (*count > profile_mask) *count = profile_mask;
188
189         return _MALI_OSK_ERR_OK;
190 }
191
192 u32 _mali_internal_profiling_get_count(void)
193 {
194         u32 retval = 0;
195
196         _mali_osk_mutex_wait(lock);
197         if (MALI_PROFILING_STATE_RETURN == prof_state) {
198                 retval = _mali_osk_atomic_read(&profile_insert_index);
199                 if (retval > profile_mask) retval = profile_mask;
200         }
201         _mali_osk_mutex_signal(lock);
202
203         return retval;
204 }
205
206 _mali_osk_errcode_t _mali_internal_profiling_get_event(u32 index, u64* timestamp, u32* event_id, u32 data[5])
207 {
208         u32 raw_index = _mali_osk_atomic_read(&profile_insert_index);
209
210         _mali_osk_mutex_wait(lock);
211
212         if (index < profile_mask) {
213                 if ((raw_index & ~profile_mask) != 0) {
214                         index += raw_index;
215                         index &= profile_mask;
216                 }
217
218                 if (prof_state != MALI_PROFILING_STATE_RETURN) {
219                         _mali_osk_mutex_signal(lock);
220                         return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
221                 }
222
223                 if(index >= raw_index) {
224                         _mali_osk_mutex_signal(lock);
225                         return _MALI_OSK_ERR_FAULT;
226                 }
227
228                 *timestamp = profile_entries[index].timestamp;
229                 *event_id = profile_entries[index].event_id;
230                 data[0] = profile_entries[index].data[0];
231                 data[1] = profile_entries[index].data[1];
232                 data[2] = profile_entries[index].data[2];
233                 data[3] = profile_entries[index].data[3];
234                 data[4] = profile_entries[index].data[4];
235         } else {
236                 _mali_osk_mutex_signal(lock);
237                 return _MALI_OSK_ERR_FAULT;
238         }
239
240         _mali_osk_mutex_signal(lock);
241         return _MALI_OSK_ERR_OK;
242 }
243
244 _mali_osk_errcode_t _mali_internal_profiling_clear(void)
245 {
246         _mali_osk_mutex_wait(lock);
247
248         if (MALI_PROFILING_STATE_RETURN != prof_state) {
249                 _mali_osk_mutex_signal(lock);
250                 return _MALI_OSK_ERR_INVALID_ARGS; /* invalid to call this function in this state */
251         }
252
253         prof_state = MALI_PROFILING_STATE_IDLE;
254         profile_mask = 0;
255         _mali_osk_atomic_init(&profile_insert_index, 0);
256
257         if (NULL != profile_entries) {
258                 _mali_osk_vfree(profile_entries);
259                 profile_entries = NULL;
260         }
261
262         _mali_osk_mutex_signal(lock);
263         return _MALI_OSK_ERR_OK;
264 }
265
266 mali_bool _mali_internal_profiling_is_recording(void)
267 {
268         return prof_state == MALI_PROFILING_STATE_RUNNING ? MALI_TRUE : MALI_FALSE;
269 }
270
271 mali_bool _mali_internal_profiling_have_recording(void)
272 {
273         return prof_state == MALI_PROFILING_STATE_RETURN ? MALI_TRUE : MALI_FALSE;
274 }