tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / r4p0_rel0 / common / mali_pp_job.c
1 /*
2  * Copyright (C) 2011-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_pp.h"
12 #include "mali_pp_job.h"
13 #include "mali_dma.h"
14 #include "mali_osk.h"
15 #include "mali_osk_list.h"
16 #include "mali_kernel_common.h"
17 #include "mali_uk_types.h"
18 #include "mali_pp_scheduler.h"
19 #if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
20 #include "linux/mali_memory_dma_buf.h"
21 #endif
22
23 static u32 pp_counter_src0 = MALI_HW_CORE_NO_COUNTER;   /**< Performance counter 0, MALI_HW_CORE_NO_COUNTER for disabled */
24 static u32 pp_counter_src1 = MALI_HW_CORE_NO_COUNTER;   /**< Performance counter 1, MALI_HW_CORE_NO_COUNTER for disabled */
25 static _mali_osk_atomic_t pp_counter_per_sub_job_count; /**< Number of values in the two arrays which is != MALI_HW_CORE_NO_COUNTER */
26 static u32 pp_counter_per_sub_job_src0[_MALI_PP_MAX_SUB_JOBS] = { MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER };
27 static u32 pp_counter_per_sub_job_src1[_MALI_PP_MAX_SUB_JOBS] = { MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER, MALI_HW_CORE_NO_COUNTER };
28
29 void mali_pp_job_initialize(void)
30 {
31         _mali_osk_atomic_init(&pp_counter_per_sub_job_count, 0);
32 }
33
34 void mali_pp_job_terminate(void)
35 {
36         _mali_osk_atomic_term(&pp_counter_per_sub_job_count);
37 }
38
39 struct mali_pp_job *mali_pp_job_create(struct mali_session_data *session, _mali_uk_pp_start_job_s *uargs, u32 id)
40 {
41         struct mali_pp_job *job;
42         u32 perf_counter_flag;
43
44         job = _mali_osk_calloc(1, sizeof(struct mali_pp_job));
45         if (NULL != job) {
46                 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_pp_start_job_s))) {
47                         goto fail;
48                 }
49
50                 if (job->uargs.num_cores > _MALI_PP_MAX_SUB_JOBS) {
51                         MALI_PRINT_ERROR(("Mali PP job: Too many sub jobs specified in job object\n"));
52                         goto fail;
53                 }
54
55                 if (!mali_pp_job_use_no_notification(job)) {
56                         job->finished_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_PP_FINISHED, sizeof(_mali_uk_pp_job_finished_s));
57                         if (NULL == job->finished_notification) goto fail;
58                 }
59
60                 perf_counter_flag = mali_pp_job_get_perf_counter_flag(job);
61
62                 /* case when no counters came from user space
63                  * so pass the debugfs / DS-5 provided global ones to the job object */
64                 if (!((perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC0_ENABLE) ||
65                       (perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC1_ENABLE))) {
66                         u32 sub_job_count = _mali_osk_atomic_read(&pp_counter_per_sub_job_count);
67
68                         /* These counters apply for all virtual jobs, and where no per sub job counter is specified */
69                         job->uargs.perf_counter_src0 = pp_counter_src0;
70                         job->uargs.perf_counter_src1 = pp_counter_src1;
71
72                         /* We only copy the per sub job array if it is enabled with at least one counter */
73                         if (0 < sub_job_count) {
74                                 job->perf_counter_per_sub_job_count = sub_job_count;
75                                 _mali_osk_memcpy(job->perf_counter_per_sub_job_src0, pp_counter_per_sub_job_src0, sizeof(pp_counter_per_sub_job_src0));
76                                 _mali_osk_memcpy(job->perf_counter_per_sub_job_src1, pp_counter_per_sub_job_src1, sizeof(pp_counter_per_sub_job_src1));
77                         }
78                 }
79
80                 _mali_osk_list_init(&job->list);
81                 job->session = session;
82                 _mali_osk_list_init(&job->session_list);
83                 job->id = id;
84
85                 job->sub_jobs_num = job->uargs.num_cores ? job->uargs.num_cores : 1;
86                 job->pid = _mali_osk_get_pid();
87                 job->tid = _mali_osk_get_tid();
88
89                 job->num_memory_cookies = job->uargs.num_memory_cookies;
90                 if (job->num_memory_cookies > 0) {
91                         u32 size;
92
93                         if (job->uargs.num_memory_cookies > session->descriptor_mapping->current_nr_mappings) {
94                                 MALI_PRINT_ERROR(("Mali PP job: Too many memory cookies specified in job object\n"));
95                                 goto fail;
96                         }
97
98                         size = sizeof(*job->uargs.memory_cookies) * job->num_memory_cookies;
99
100                         job->memory_cookies = _mali_osk_malloc(size);
101                         if (NULL == job->memory_cookies) {
102                                 MALI_PRINT_ERROR(("Mali PP job: Failed to allocate %d bytes of memory cookies!\n", size));
103                                 goto fail;
104                         }
105
106                         if (0 != _mali_osk_copy_from_user(job->memory_cookies, job->uargs.memory_cookies, size)) {
107                                 MALI_PRINT_ERROR(("Mali PP job: Failed to copy %d bytes of memory cookies from user!\n", size));
108                                 goto fail;
109                         }
110
111 #if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
112                         job->num_dma_bufs = job->num_memory_cookies;
113                         job->dma_bufs = _mali_osk_calloc(job->num_dma_bufs, sizeof(struct mali_dma_buf_attachment *));
114                         if (NULL == job->dma_bufs) {
115                                 MALI_PRINT_ERROR(("Mali PP job: Failed to allocate dma_bufs array!\n"));
116                                 goto fail;
117                         }
118 #endif
119                 }
120
121                 /* Prepare DMA command buffer to start job, if it is virtual. */
122                 if (mali_pp_job_is_virtual(job)) {
123                         struct mali_pp_core *core;
124                         _mali_osk_errcode_t err =  mali_dma_get_cmd_buf(&job->dma_cmd_buf);
125
126                         if (_MALI_OSK_ERR_OK != err) {
127                                 MALI_PRINT_ERROR(("Mali PP job: Failed to allocate DMA command buffer\n"));
128                                 goto fail;
129                         }
130
131                         core = mali_pp_scheduler_get_virtual_pp();
132                         MALI_DEBUG_ASSERT_POINTER(core);
133
134                         mali_pp_job_dma_cmd_prepare(core, job, 0, MALI_FALSE, &job->dma_cmd_buf);
135                 }
136
137                 if (_MALI_OSK_ERR_OK != mali_pp_job_check(job)) {
138                         /* Not a valid job. */
139                         goto fail;
140                 }
141
142                 mali_timeline_tracker_init(&job->tracker, MALI_TIMELINE_TRACKER_PP, NULL, job);
143                 mali_timeline_fence_copy_uk_fence(&(job->tracker.fence), &(job->uargs.fence));
144
145                 return job;
146         }
147
148 fail:
149         if (NULL != job) {
150                 mali_pp_job_delete(job);
151         }
152
153         return NULL;
154 }
155
156 void mali_pp_job_delete(struct mali_pp_job *job)
157 {
158         mali_dma_put_cmd_buf(&job->dma_cmd_buf);
159         if (NULL != job->finished_notification) {
160                 _mali_osk_notification_delete(job->finished_notification);
161         }
162
163         _mali_osk_free(job->memory_cookies);
164
165 #if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
166         /* Unmap buffers attached to job */
167         if (0 < job->num_dma_bufs) {
168                 mali_dma_buf_unmap_job(job);
169         }
170
171         _mali_osk_free(job->dma_bufs);
172 #endif /* CONFIG_DMA_SHARED_BUFFER */
173
174         _mali_osk_free(job);
175 }
176
177 u32 mali_pp_job_get_perf_counter_src0(struct mali_pp_job *job, u32 sub_job)
178 {
179         /* Virtual jobs always use the global job counter (or if there are per sub job counters at all) */
180         if (mali_pp_job_is_virtual(job) || 0 == job->perf_counter_per_sub_job_count) {
181                 return job->uargs.perf_counter_src0;
182         }
183
184         /* Use per sub job counter if enabled... */
185         if (MALI_HW_CORE_NO_COUNTER != job->perf_counter_per_sub_job_src0[sub_job]) {
186                 return job->perf_counter_per_sub_job_src0[sub_job];
187         }
188
189         /* ...else default to global job counter */
190         return job->uargs.perf_counter_src0;
191 }
192
193 u32 mali_pp_job_get_perf_counter_src1(struct mali_pp_job *job, u32 sub_job)
194 {
195         /* Virtual jobs always use the global job counter (or if there are per sub job counters at all) */
196         if (mali_pp_job_is_virtual(job) || 0 == job->perf_counter_per_sub_job_count) {
197                 /* Virtual jobs always use the global job counter */
198                 return job->uargs.perf_counter_src1;
199         }
200
201         /* Use per sub job counter if enabled... */
202         if (MALI_HW_CORE_NO_COUNTER != job->perf_counter_per_sub_job_src1[sub_job]) {
203                 return job->perf_counter_per_sub_job_src1[sub_job];
204         }
205
206         /* ...else default to global job counter */
207         return job->uargs.perf_counter_src1;
208 }
209
210 void mali_pp_job_set_pp_counter_global_src0(u32 counter)
211 {
212         pp_counter_src0 = counter;
213 }
214
215 void mali_pp_job_set_pp_counter_global_src1(u32 counter)
216 {
217         pp_counter_src1 = counter;
218 }
219
220 void mali_pp_job_set_pp_counter_sub_job_src0(u32 sub_job, u32 counter)
221 {
222         MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
223
224         if (MALI_HW_CORE_NO_COUNTER == pp_counter_per_sub_job_src0[sub_job]) {
225                 /* increment count since existing counter was disabled */
226                 _mali_osk_atomic_inc(&pp_counter_per_sub_job_count);
227         }
228
229         if (MALI_HW_CORE_NO_COUNTER == counter) {
230                 /* decrement count since new counter is disabled */
231                 _mali_osk_atomic_dec(&pp_counter_per_sub_job_count);
232         }
233
234         /* PS: A change from MALI_HW_CORE_NO_COUNTER to MALI_HW_CORE_NO_COUNTER will inc and dec, result will be 0 change */
235
236         pp_counter_per_sub_job_src0[sub_job] = counter;
237 }
238
239 void mali_pp_job_set_pp_counter_sub_job_src1(u32 sub_job, u32 counter)
240 {
241         MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
242
243         if (MALI_HW_CORE_NO_COUNTER == pp_counter_per_sub_job_src1[sub_job]) {
244                 /* increment count since existing counter was disabled */
245                 _mali_osk_atomic_inc(&pp_counter_per_sub_job_count);
246         }
247
248         if (MALI_HW_CORE_NO_COUNTER == counter) {
249                 /* decrement count since new counter is disabled */
250                 _mali_osk_atomic_dec(&pp_counter_per_sub_job_count);
251         }
252
253         /* PS: A change from MALI_HW_CORE_NO_COUNTER to MALI_HW_CORE_NO_COUNTER will inc and dec, result will be 0 change */
254
255         pp_counter_per_sub_job_src1[sub_job] = counter;
256 }
257
258 u32 mali_pp_job_get_pp_counter_global_src0(void)
259 {
260         return pp_counter_src0;
261 }
262
263 u32 mali_pp_job_get_pp_counter_global_src1(void)
264 {
265         return pp_counter_src1;
266 }
267
268 u32 mali_pp_job_get_pp_counter_sub_job_src0(u32 sub_job)
269 {
270         MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
271         return pp_counter_per_sub_job_src0[sub_job];
272 }
273
274 u32 mali_pp_job_get_pp_counter_sub_job_src1(u32 sub_job)
275 {
276         MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
277         return pp_counter_per_sub_job_src1[sub_job];
278 }