MALI: rockchip: upgrade utgard DDK to r6p0-01rel1
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / common / mali_gp_job.c
1 /*
2  * Copyright (C) 2011-2015 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_gp_job.h"
12 #include "mali_osk.h"
13 #include "mali_osk_list.h"
14 #include "mali_uk_types.h"
15 #include "mali_memory_virtual.h"
16 #include "mali_memory_defer_bind.h"
17
18 static u32 gp_counter_src0 = MALI_HW_CORE_NO_COUNTER;      /**< Performance counter 0, MALI_HW_CORE_NO_COUNTER for disabled */
19 static u32 gp_counter_src1 = MALI_HW_CORE_NO_COUNTER;           /**< Performance counter 1, MALI_HW_CORE_NO_COUNTER for disabled */
20 static void _mali_gp_del_varying_allocations(struct mali_gp_job *job);
21
22
23 static int _mali_gp_add_varying_allocations(struct mali_session_data *session,
24                 struct mali_gp_job *job,
25                 u32 *alloc,
26                 u32 num)
27 {
28         int i = 0;
29         struct mali_gp_allocation_node *alloc_node;
30         mali_mem_allocation *mali_alloc = NULL;
31         struct mali_vma_node *mali_vma_node = NULL;
32
33         for (i = 0 ; i < num ; i++) {
34                 MALI_DEBUG_ASSERT(alloc[i]);
35                 alloc_node = _mali_osk_calloc(1, sizeof(struct mali_gp_allocation_node));
36                 if (alloc_node) {
37                         INIT_LIST_HEAD(&alloc_node->node);
38                         /* find mali allocation structure by vaddress*/
39                         mali_vma_node = mali_vma_offset_search(&session->allocation_mgr, alloc[i], 0);
40
41                         if (likely(mali_vma_node)) {
42                                 mali_alloc = container_of(mali_vma_node, struct mali_mem_allocation, mali_vma_node);
43                                 MALI_DEBUG_ASSERT(alloc[i] == mali_vma_node->vm_node.start);
44                         } else {
45                                 MALI_DEBUG_PRINT(1, ("ERROE!_mali_gp_add_varying_allocations,can't find allocation %d by address =0x%x, num=%d\n", i, alloc[i], num));
46                                 _mali_osk_free(alloc_node);
47                                 goto fail;
48                         }
49                         alloc_node->alloc = mali_alloc;
50                         /* add to gp job varying alloc list*/
51                         list_move(&alloc_node->node, &job->varying_alloc);
52                 } else
53                         goto fail;
54         }
55
56         return 0;
57 fail:
58         MALI_DEBUG_PRINT(1, ("ERROE!_mali_gp_add_varying_allocations,failed to alloc memory!\n"));
59         _mali_gp_del_varying_allocations(job);
60         return -1;
61 }
62
63
64 static void _mali_gp_del_varying_allocations(struct mali_gp_job *job)
65 {
66         struct mali_gp_allocation_node *alloc_node, *tmp_node;
67
68         list_for_each_entry_safe(alloc_node, tmp_node, &job->varying_alloc, node) {
69                 list_del(&alloc_node->node);
70                 kfree(alloc_node);
71         }
72         INIT_LIST_HEAD(&job->varying_alloc);
73 }
74
75 struct mali_gp_job *mali_gp_job_create(struct mali_session_data *session, _mali_uk_gp_start_job_s *uargs, u32 id, struct mali_timeline_tracker *pp_tracker)
76 {
77         struct mali_gp_job *job;
78         u32 perf_counter_flag;
79         u32 __user *memory_list = NULL;
80         struct mali_gp_allocation_node *alloc_node, *tmp_node;
81
82         job = _mali_osk_calloc(1, sizeof(struct mali_gp_job));
83         if (NULL != job) {
84                 job->finished_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_GP_FINISHED, sizeof(_mali_uk_gp_job_finished_s));
85                 if (NULL == job->finished_notification) {
86                         goto fail3;
87                 }
88
89                 job->oom_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_GP_STALLED, sizeof(_mali_uk_gp_job_suspended_s));
90                 if (NULL == job->oom_notification) {
91                         goto fail2;
92                 }
93
94                 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_gp_start_job_s))) {
95                         goto fail1;
96                 }
97
98                 perf_counter_flag = mali_gp_job_get_perf_counter_flag(job);
99
100                 /* case when no counters came from user space
101                  * so pass the debugfs / DS-5 provided global ones to the job object */
102                 if (!((perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC0_ENABLE) ||
103                       (perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC1_ENABLE))) {
104                         mali_gp_job_set_perf_counter_src0(job, mali_gp_job_get_gp_counter_src0());
105                         mali_gp_job_set_perf_counter_src1(job, mali_gp_job_get_gp_counter_src1());
106                 }
107
108                 _mali_osk_list_init(&job->list);
109                 job->session = session;
110                 job->id = id;
111                 job->heap_current_addr = job->uargs.frame_registers[4];
112                 job->perf_counter_value0 = 0;
113                 job->perf_counter_value1 = 0;
114                 job->pid = _mali_osk_get_pid();
115                 job->tid = _mali_osk_get_tid();
116
117
118                 INIT_LIST_HEAD(&job->varying_alloc);
119                 INIT_LIST_HEAD(&job->vary_todo);
120                 job->dmem = NULL;
121
122                 if (job->uargs.varying_alloc_num > session->allocation_mgr.mali_allocation_num) {
123                         MALI_PRINT_ERROR(("Mali GP job: The number of  varying buffer to defer bind  is invalid !\n"));
124                         goto fail1;
125                 }
126
127                 /* add varying allocation list*/
128                 if (job->uargs.varying_alloc_num > 0) {
129                         /* copy varying list from user space*/
130                         job->varying_list = _mali_osk_calloc(1, sizeof(u32) * job->uargs.varying_alloc_num);
131                         if (!job->varying_list) {
132                                 MALI_PRINT_ERROR(("Mali GP job: allocate varying_list failed varying_alloc_num = %d !\n", job->uargs.varying_alloc_num));
133                                 goto fail1;
134                         }
135
136                         memory_list = (u32 __user *)(uintptr_t)uargs->varying_alloc_list;
137
138                         if (0 != _mali_osk_copy_from_user(job->varying_list, memory_list, sizeof(u32) * job->uargs.varying_alloc_num)) {
139                                 MALI_PRINT_ERROR(("Mali GP job: Failed to copy varying list from user space!\n"));
140                                 goto fail;
141                         }
142
143                         if (unlikely(_mali_gp_add_varying_allocations(session, job, job->varying_list,
144                                         job->uargs.varying_alloc_num))) {
145                                 MALI_PRINT_ERROR(("Mali GP job: _mali_gp_add_varying_allocations failed!\n"));
146                                 goto fail;
147                         }
148
149                         /* do preparetion for each allocation */
150                         list_for_each_entry_safe(alloc_node, tmp_node, &job->varying_alloc, node) {
151                                 if (unlikely(_MALI_OSK_ERR_OK != mali_mem_defer_bind_allocation_prepare(alloc_node->alloc, &job->vary_todo, &job->required_varying_memsize))) {
152                                         MALI_PRINT_ERROR(("Mali GP job: mali_mem_defer_bind_allocation_prepare failed!\n"));
153                                         goto fail;
154                                 }
155                         }
156
157                         _mali_gp_del_varying_allocations(job);
158
159                         /* bind varying here, to avoid memory latency issue. */
160                         {
161                                 struct mali_defer_mem_block dmem_block;
162
163                                 INIT_LIST_HEAD(&dmem_block.free_pages);
164                                 atomic_set(&dmem_block.num_free_pages, 0);
165
166                                 if (mali_mem_prepare_mem_for_job(job, &dmem_block)) {
167                                         MALI_PRINT_ERROR(("Mali GP job: mali_mem_prepare_mem_for_job failed!\n"));
168                                         goto fail;
169                                 }
170                                 if (_MALI_OSK_ERR_OK != mali_mem_defer_bind(job, &dmem_block)) {
171                                         MALI_PRINT_ERROR(("gp job create, mali_mem_defer_bind failed! GP %x fail!", job));
172                                         goto fail;
173                                 }
174                         }
175
176                         if (uargs->varying_memsize > MALI_UK_BIG_VARYING_SIZE) {
177                                 job->big_job = 1;
178                         }
179                 }
180                 job->pp_tracker = pp_tracker;
181                 if (NULL != job->pp_tracker) {
182                         /* Take a reference on PP job's tracker that will be released when the GP
183                            job is done. */
184                         mali_timeline_system_tracker_get(session->timeline_system, pp_tracker);
185                 }
186
187                 mali_timeline_tracker_init(&job->tracker, MALI_TIMELINE_TRACKER_GP, NULL, job);
188                 mali_timeline_fence_copy_uk_fence(&(job->tracker.fence), &(job->uargs.fence));
189
190                 return job;
191         } else {
192                 MALI_PRINT_ERROR(("Mali GP job: _mali_osk_calloc failed!\n"));
193                 return NULL;
194         }
195
196
197 fail:
198         _mali_osk_free(job->varying_list);
199         /* Handle allocate fail here, free all varying node */
200         {
201                 struct mali_backend_bind_list *bkn, *bkn_tmp;
202                 list_for_each_entry_safe(bkn, bkn_tmp , &job->vary_todo, node) {
203                         list_del(&bkn->node);
204                         _mali_osk_free(bkn);
205                 }
206         }
207 fail1:
208         _mali_osk_notification_delete(job->oom_notification);
209 fail2:
210         _mali_osk_notification_delete(job->finished_notification);
211 fail3:
212         _mali_osk_free(job);
213         return NULL;
214 }
215
216 void mali_gp_job_delete(struct mali_gp_job *job)
217 {
218         struct mali_backend_bind_list *bkn, *bkn_tmp;
219         MALI_DEBUG_ASSERT_POINTER(job);
220         MALI_DEBUG_ASSERT(NULL == job->pp_tracker);
221         MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->list));
222         _mali_osk_free(job->varying_list);
223
224         /* Handle allocate fail here, free all varying node */
225         list_for_each_entry_safe(bkn, bkn_tmp , &job->vary_todo, node) {
226                 list_del(&bkn->node);
227                 _mali_osk_free(bkn);
228         }
229
230         mali_mem_defer_dmem_free(job);
231
232         /* de-allocate the pre-allocated oom notifications */
233         if (NULL != job->oom_notification) {
234                 _mali_osk_notification_delete(job->oom_notification);
235                 job->oom_notification = NULL;
236         }
237         if (NULL != job->finished_notification) {
238                 _mali_osk_notification_delete(job->finished_notification);
239                 job->finished_notification = NULL;
240         }
241
242         _mali_osk_free(job);
243 }
244
245 void mali_gp_job_list_add(struct mali_gp_job *job, _mali_osk_list_t *list)
246 {
247         struct mali_gp_job *iter;
248         struct mali_gp_job *tmp;
249
250         MALI_DEBUG_ASSERT_POINTER(job);
251         MALI_DEBUG_ASSERT_SCHEDULER_LOCK_HELD();
252
253         /* Find position in list/queue where job should be added. */
254         _MALI_OSK_LIST_FOREACHENTRY_REVERSE(iter, tmp, list,
255                                             struct mali_gp_job, list) {
256
257                 /* A span is used to handle job ID wrapping. */
258                 bool job_is_after = (mali_gp_job_get_id(job) -
259                                      mali_gp_job_get_id(iter)) <
260                                     MALI_SCHEDULER_JOB_ID_SPAN;
261
262                 if (job_is_after) {
263                         break;
264                 }
265         }
266
267         _mali_osk_list_add(&job->list, &iter->list);
268 }
269
270 u32 mali_gp_job_get_gp_counter_src0(void)
271 {
272         return gp_counter_src0;
273 }
274
275 void mali_gp_job_set_gp_counter_src0(u32 counter)
276 {
277         gp_counter_src0 = counter;
278 }
279
280 u32 mali_gp_job_get_gp_counter_src1(void)
281 {
282         return gp_counter_src1;
283 }
284
285 void mali_gp_job_set_gp_counter_src1(u32 counter)
286 {
287         gp_counter_src1 = counter;
288 }
289
290 mali_scheduler_mask mali_gp_job_signal_pp_tracker(struct mali_gp_job *job, mali_bool success)
291 {
292         mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
293
294         MALI_DEBUG_ASSERT_POINTER(job);
295
296         if (NULL != job->pp_tracker) {
297                 schedule_mask |= mali_timeline_system_tracker_put(job->session->timeline_system, job->pp_tracker, MALI_FALSE == success);
298                 job->pp_tracker = NULL;
299         }
300
301         return schedule_mask;
302 }