2 * Copyright (C) 2011-2015 ARM Limited. All rights reserved.
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.
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.
11 #include "mali_gp_job.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"
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);
23 static int _mali_gp_add_varying_allocations(struct mali_session_data *session,
24 struct mali_gp_job *job,
29 struct mali_gp_allocation_node *alloc_node;
30 mali_mem_allocation *mali_alloc = NULL;
31 struct mali_vma_node *mali_vma_node = NULL;
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));
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);
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);
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);
49 alloc_node->alloc = mali_alloc;
50 /* add to gp job varying alloc list*/
51 list_move(&alloc_node->node, &job->varying_alloc);
58 MALI_DEBUG_PRINT(1, ("ERROE!_mali_gp_add_varying_allocations,failed to alloc memory!\n"));
59 _mali_gp_del_varying_allocations(job);
64 static void _mali_gp_del_varying_allocations(struct mali_gp_job *job)
66 struct mali_gp_allocation_node *alloc_node, *tmp_node;
68 list_for_each_entry_safe(alloc_node, tmp_node, &job->varying_alloc, node) {
69 list_del(&alloc_node->node);
72 INIT_LIST_HEAD(&job->varying_alloc);
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)
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;
82 job = _mali_osk_calloc(1, sizeof(struct mali_gp_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) {
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) {
94 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_gp_start_job_s))) {
98 perf_counter_flag = mali_gp_job_get_perf_counter_flag(job);
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());
108 _mali_osk_list_init(&job->list);
109 job->session = session;
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();
118 INIT_LIST_HEAD(&job->varying_alloc);
119 INIT_LIST_HEAD(&job->vary_todo);
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"));
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));
136 memory_list = (u32 __user *)(uintptr_t)uargs->varying_alloc_list;
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"));
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"));
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"));
157 _mali_gp_del_varying_allocations(job);
159 /* bind varying here, to avoid memory latency issue. */
161 struct mali_defer_mem_block dmem_block;
163 INIT_LIST_HEAD(&dmem_block.free_pages);
164 atomic_set(&dmem_block.num_free_pages, 0);
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"));
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));
176 if (uargs->varying_memsize > MALI_UK_BIG_VARYING_SIZE) {
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
184 mali_timeline_system_tracker_get(session->timeline_system, pp_tracker);
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));
192 MALI_PRINT_ERROR(("Mali GP job: _mali_osk_calloc failed!\n"));
198 _mali_osk_free(job->varying_list);
199 /* Handle allocate fail here, free all varying node */
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);
208 _mali_osk_notification_delete(job->oom_notification);
210 _mali_osk_notification_delete(job->finished_notification);
216 void mali_gp_job_delete(struct mali_gp_job *job)
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);
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);
230 mali_mem_defer_dmem_free(job);
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;
237 if (NULL != job->finished_notification) {
238 _mali_osk_notification_delete(job->finished_notification);
239 job->finished_notification = NULL;
245 void mali_gp_job_list_add(struct mali_gp_job *job, _mali_osk_list_t *list)
247 struct mali_gp_job *iter;
248 struct mali_gp_job *tmp;
250 MALI_DEBUG_ASSERT_POINTER(job);
251 MALI_DEBUG_ASSERT_SCHEDULER_LOCK_HELD();
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) {
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;
267 _mali_osk_list_add(&job->list, &iter->list);
270 u32 mali_gp_job_get_gp_counter_src0(void)
272 return gp_counter_src0;
275 void mali_gp_job_set_gp_counter_src0(u32 counter)
277 gp_counter_src0 = counter;
280 u32 mali_gp_job_get_gp_counter_src1(void)
282 return gp_counter_src1;
285 void mali_gp_job_set_gp_counter_src1(u32 counter)
287 gp_counter_src1 = counter;
290 mali_scheduler_mask mali_gp_job_signal_pp_tracker(struct mali_gp_job *job, mali_bool success)
292 mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
294 MALI_DEBUG_ASSERT_POINTER(job);
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;
301 return schedule_mask;