2 * Copyright (C) 2011-2016 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;
81 _mali_uk_gp_start_job_s copy_of_uargs;
83 job = _mali_osk_calloc(1, sizeof(struct mali_gp_job));
85 job->finished_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_GP_FINISHED, sizeof(_mali_uk_gp_job_finished_s));
86 if (NULL == job->finished_notification) {
90 job->oom_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_GP_STALLED, sizeof(_mali_uk_gp_job_suspended_s));
91 if (NULL == job->oom_notification) {
95 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_gp_start_job_s))) {
99 perf_counter_flag = mali_gp_job_get_perf_counter_flag(job);
101 /* case when no counters came from user space
102 * so pass the debugfs / DS-5 provided global ones to the job object */
103 if (!((perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC0_ENABLE) ||
104 (perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC1_ENABLE))) {
105 mali_gp_job_set_perf_counter_src0(job, mali_gp_job_get_gp_counter_src0());
106 mali_gp_job_set_perf_counter_src1(job, mali_gp_job_get_gp_counter_src1());
109 _mali_osk_list_init(&job->list);
110 job->session = session;
112 job->heap_current_addr = job->uargs.frame_registers[4];
113 job->perf_counter_value0 = 0;
114 job->perf_counter_value1 = 0;
115 job->pid = _mali_osk_get_pid();
116 job->tid = _mali_osk_get_tid();
119 INIT_LIST_HEAD(&job->varying_alloc);
120 INIT_LIST_HEAD(&job->vary_todo);
123 if (job->uargs.deferred_mem_num > session->allocation_mgr.mali_allocation_num) {
124 MALI_PRINT_ERROR(("Mali GP job: The number of varying buffer to defer bind is invalid !\n"));
128 /* add varying allocation list*/
129 if (job->uargs.deferred_mem_num > 0) {
130 /* copy varying list from user space*/
131 job->varying_list = _mali_osk_calloc(1, sizeof(u32) * job->uargs.deferred_mem_num);
132 if (!job->varying_list) {
133 MALI_PRINT_ERROR(("Mali GP job: allocate varying_list failed varying_alloc_num = %d !\n", job->uargs.deferred_mem_num));
137 if (0 != _mali_osk_copy_from_user(©_of_uargs, uargs, sizeof(_mali_uk_gp_start_job_s))) {
140 memory_list = (u32 __user *)(uintptr_t)copy_of_uargs.deferred_mem_list;
142 if (0 != _mali_osk_copy_from_user(job->varying_list, memory_list, sizeof(u32) * job->uargs.deferred_mem_num)) {
143 MALI_PRINT_ERROR(("Mali GP job: Failed to copy varying list from user space!\n"));
147 if (unlikely(_mali_gp_add_varying_allocations(session, job, job->varying_list,
148 job->uargs.deferred_mem_num))) {
149 MALI_PRINT_ERROR(("Mali GP job: _mali_gp_add_varying_allocations failed!\n"));
153 /* do preparetion for each allocation */
154 list_for_each_entry_safe(alloc_node, tmp_node, &job->varying_alloc, node) {
155 if (unlikely(_MALI_OSK_ERR_OK != mali_mem_defer_bind_allocation_prepare(alloc_node->alloc, &job->vary_todo, &job->required_varying_memsize))) {
156 MALI_PRINT_ERROR(("Mali GP job: mali_mem_defer_bind_allocation_prepare failed!\n"));
161 _mali_gp_del_varying_allocations(job);
163 /* bind varying here, to avoid memory latency issue. */
165 struct mali_defer_mem_block dmem_block;
167 INIT_LIST_HEAD(&dmem_block.free_pages);
168 atomic_set(&dmem_block.num_free_pages, 0);
170 if (mali_mem_prepare_mem_for_job(job, &dmem_block)) {
171 MALI_PRINT_ERROR(("Mali GP job: mali_mem_prepare_mem_for_job failed!\n"));
174 if (_MALI_OSK_ERR_OK != mali_mem_defer_bind(job, &dmem_block)) {
175 MALI_PRINT_ERROR(("gp job create, mali_mem_defer_bind failed! GP %x fail!", job));
180 if (job->uargs.varying_memsize > MALI_UK_BIG_VARYING_SIZE) {
184 job->pp_tracker = pp_tracker;
185 if (NULL != job->pp_tracker) {
186 /* Take a reference on PP job's tracker that will be released when the GP
188 mali_timeline_system_tracker_get(session->timeline_system, pp_tracker);
191 mali_timeline_tracker_init(&job->tracker, MALI_TIMELINE_TRACKER_GP, NULL, job);
192 mali_timeline_fence_copy_uk_fence(&(job->tracker.fence), &(job->uargs.fence));
196 MALI_PRINT_ERROR(("Mali GP job: _mali_osk_calloc failed!\n"));
202 _mali_osk_free(job->varying_list);
203 /* Handle allocate fail here, free all varying node */
205 struct mali_backend_bind_list *bkn, *bkn_tmp;
206 list_for_each_entry_safe(bkn, bkn_tmp , &job->vary_todo, node) {
207 list_del(&bkn->node);
212 _mali_osk_notification_delete(job->oom_notification);
214 _mali_osk_notification_delete(job->finished_notification);
220 void mali_gp_job_delete(struct mali_gp_job *job)
222 struct mali_backend_bind_list *bkn, *bkn_tmp;
223 MALI_DEBUG_ASSERT_POINTER(job);
224 MALI_DEBUG_ASSERT(NULL == job->pp_tracker);
225 MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->list));
226 _mali_osk_free(job->varying_list);
228 /* Handle allocate fail here, free all varying node */
229 list_for_each_entry_safe(bkn, bkn_tmp , &job->vary_todo, node) {
230 list_del(&bkn->node);
234 mali_mem_defer_dmem_free(job);
236 /* de-allocate the pre-allocated oom notifications */
237 if (NULL != job->oom_notification) {
238 _mali_osk_notification_delete(job->oom_notification);
239 job->oom_notification = NULL;
241 if (NULL != job->finished_notification) {
242 _mali_osk_notification_delete(job->finished_notification);
243 job->finished_notification = NULL;
249 void mali_gp_job_list_add(struct mali_gp_job *job, _mali_osk_list_t *list)
251 struct mali_gp_job *iter;
252 struct mali_gp_job *tmp;
254 MALI_DEBUG_ASSERT_POINTER(job);
255 MALI_DEBUG_ASSERT_SCHEDULER_LOCK_HELD();
257 /* Find position in list/queue where job should be added. */
258 _MALI_OSK_LIST_FOREACHENTRY_REVERSE(iter, tmp, list,
259 struct mali_gp_job, list) {
261 /* A span is used to handle job ID wrapping. */
262 bool job_is_after = (mali_gp_job_get_id(job) -
263 mali_gp_job_get_id(iter)) <
264 MALI_SCHEDULER_JOB_ID_SPAN;
271 _mali_osk_list_add(&job->list, &iter->list);
274 u32 mali_gp_job_get_gp_counter_src0(void)
276 return gp_counter_src0;
279 void mali_gp_job_set_gp_counter_src0(u32 counter)
281 gp_counter_src0 = counter;
284 u32 mali_gp_job_get_gp_counter_src1(void)
286 return gp_counter_src1;
289 void mali_gp_job_set_gp_counter_src1(u32 counter)
291 gp_counter_src1 = counter;
294 mali_scheduler_mask mali_gp_job_signal_pp_tracker(struct mali_gp_job *job, mali_bool success)
296 mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
298 MALI_DEBUG_ASSERT_POINTER(job);
300 if (NULL != job->pp_tracker) {
301 schedule_mask |= mali_timeline_system_tracker_put(job->session->timeline_system, job->pp_tracker, MALI_FALSE == success);
302 job->pp_tracker = NULL;
305 return schedule_mask;