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.
12 #include "mali_pp_job.h"
14 #include "mali_osk_list.h"
15 #include "mali_kernel_common.h"
16 #include "mali_uk_types.h"
17 #include "mali_executor.h"
18 #if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
19 #include "linux/mali_memory_dma_buf.h"
21 #include "mali_memory_swap_alloc.h"
22 #include "mali_scheduler.h"
24 static u32 pp_counter_src0 = MALI_HW_CORE_NO_COUNTER; /**< Performance counter 0, MALI_HW_CORE_NO_COUNTER for disabled */
25 static u32 pp_counter_src1 = MALI_HW_CORE_NO_COUNTER; /**< Performance counter 1, MALI_HW_CORE_NO_COUNTER for disabled */
26 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 */
27 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 };
28 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 };
30 void mali_pp_job_initialize(void)
32 _mali_osk_atomic_init(&pp_counter_per_sub_job_count, 0);
35 void mali_pp_job_terminate(void)
37 _mali_osk_atomic_term(&pp_counter_per_sub_job_count);
40 struct mali_pp_job *mali_pp_job_create(struct mali_session_data *session,
41 _mali_uk_pp_start_job_s __user *uargs, u32 id)
43 struct mali_pp_job *job;
44 u32 perf_counter_flag;
46 job = _mali_osk_calloc(1, sizeof(struct mali_pp_job));
49 _mali_osk_list_init(&job->list);
50 _mali_osk_list_init(&job->session_fb_lookup_list);
51 _mali_osk_atomic_inc(&session->number_of_pp_jobs);
53 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_pp_start_job_s))) {
57 if (job->uargs.num_cores > _MALI_PP_MAX_SUB_JOBS) {
58 MALI_PRINT_ERROR(("Mali PP job: Too many sub jobs specified in job object\n"));
62 if (!mali_pp_job_use_no_notification(job)) {
63 job->finished_notification = _mali_osk_notification_create(_MALI_NOTIFICATION_PP_FINISHED, sizeof(_mali_uk_pp_job_finished_s));
64 if (NULL == job->finished_notification) goto fail;
67 perf_counter_flag = mali_pp_job_get_perf_counter_flag(job);
69 /* case when no counters came from user space
70 * so pass the debugfs / DS-5 provided global ones to the job object */
71 if (!((perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC0_ENABLE) ||
72 (perf_counter_flag & _MALI_PERFORMANCE_COUNTER_FLAG_SRC1_ENABLE))) {
73 u32 sub_job_count = _mali_osk_atomic_read(&pp_counter_per_sub_job_count);
75 /* These counters apply for all virtual jobs, and where no per sub job counter is specified */
76 job->uargs.perf_counter_src0 = pp_counter_src0;
77 job->uargs.perf_counter_src1 = pp_counter_src1;
79 /* We only copy the per sub job array if it is enabled with at least one counter */
80 if (0 < sub_job_count) {
81 job->perf_counter_per_sub_job_count = sub_job_count;
82 _mali_osk_memcpy(job->perf_counter_per_sub_job_src0, pp_counter_per_sub_job_src0, sizeof(pp_counter_per_sub_job_src0));
83 _mali_osk_memcpy(job->perf_counter_per_sub_job_src1, pp_counter_per_sub_job_src1, sizeof(pp_counter_per_sub_job_src1));
87 job->session = session;
90 job->sub_jobs_num = job->uargs.num_cores ? job->uargs.num_cores : 1;
91 job->pid = _mali_osk_get_pid();
92 job->tid = _mali_osk_get_tid();
94 _mali_osk_atomic_init(&job->sub_jobs_completed, 0);
95 _mali_osk_atomic_init(&job->sub_job_errors, 0);
96 job->swap_status = MALI_NO_SWAP_IN;
97 job->user_notification = MALI_FALSE;
98 job->num_pp_cores_in_virtual = 0;
100 if (job->uargs.num_memory_cookies > session->allocation_mgr.mali_allocation_num) {
101 MALI_PRINT_ERROR(("Mali PP job: The number of memory cookies is invalid !\n"));
105 if (job->uargs.num_memory_cookies > 0) {
107 u32 __user *memory_cookies = (u32 __user *)(uintptr_t)job->uargs.memory_cookies;
109 size = sizeof(*memory_cookies) * (job->uargs.num_memory_cookies);
111 job->memory_cookies = _mali_osk_malloc(size);
112 if (NULL == job->memory_cookies) {
113 MALI_PRINT_ERROR(("Mali PP job: Failed to allocate %d bytes of memory cookies!\n", size));
117 if (0 != _mali_osk_copy_from_user(job->memory_cookies, memory_cookies, size)) {
118 MALI_PRINT_ERROR(("Mali PP job: Failed to copy %d bytes of memory cookies from user!\n", size));
123 if (_MALI_OSK_ERR_OK != mali_pp_job_check(job)) {
124 /* Not a valid job. */
128 mali_timeline_tracker_init(&job->tracker, MALI_TIMELINE_TRACKER_PP, NULL, job);
129 mali_timeline_fence_copy_uk_fence(&(job->tracker.fence), &(job->uargs.fence));
131 mali_mem_swap_in_pages(job);
138 mali_pp_job_delete(job);
144 void mali_pp_job_delete(struct mali_pp_job *job)
146 struct mali_session_data *session;
148 MALI_DEBUG_ASSERT_POINTER(job);
149 MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->list));
150 MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->session_fb_lookup_list));
152 session = mali_pp_job_get_session(job);
153 MALI_DEBUG_ASSERT_POINTER(session);
155 if (NULL != job->memory_cookies) {
156 #if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
157 /* Unmap buffers attached to job */
158 mali_dma_buf_unmap_job(job);
160 if (MALI_NO_SWAP_IN != job->swap_status) {
161 mali_mem_swap_out_pages(job);
164 _mali_osk_free(job->memory_cookies);
167 if (job->user_notification) {
168 mali_scheduler_return_pp_job_to_user(job,
169 job->num_pp_cores_in_virtual);
172 if (NULL != job->finished_notification) {
173 _mali_osk_notification_delete(job->finished_notification);
176 _mali_osk_atomic_term(&job->sub_jobs_completed);
177 _mali_osk_atomic_term(&job->sub_job_errors);
178 _mali_osk_atomic_dec(&session->number_of_pp_jobs);
181 _mali_osk_wait_queue_wake_up(session->wait_queue);
184 void mali_pp_job_list_add(struct mali_pp_job *job, _mali_osk_list_t *list)
186 struct mali_pp_job *iter;
187 struct mali_pp_job *tmp;
189 MALI_DEBUG_ASSERT_POINTER(job);
190 MALI_DEBUG_ASSERT_SCHEDULER_LOCK_HELD();
192 /* Find position in list/queue where job should be added. */
193 _MALI_OSK_LIST_FOREACHENTRY_REVERSE(iter, tmp, list,
194 struct mali_pp_job, list) {
195 /* job should be started after iter if iter is in progress. */
196 if (0 < iter->sub_jobs_started) {
201 * job should be started after iter if it has a higher
202 * job id. A span is used to handle job id wrapping.
204 if ((mali_pp_job_get_id(job) -
205 mali_pp_job_get_id(iter)) <
206 MALI_SCHEDULER_JOB_ID_SPAN) {
211 _mali_osk_list_add(&job->list, &iter->list);
215 u32 mali_pp_job_get_perf_counter_src0(struct mali_pp_job *job, u32 sub_job)
217 /* Virtual jobs always use the global job counter (or if there are per sub job counters at all) */
218 if (mali_pp_job_is_virtual(job) || 0 == job->perf_counter_per_sub_job_count) {
219 return job->uargs.perf_counter_src0;
222 /* Use per sub job counter if enabled... */
223 if (MALI_HW_CORE_NO_COUNTER != job->perf_counter_per_sub_job_src0[sub_job]) {
224 return job->perf_counter_per_sub_job_src0[sub_job];
227 /* ...else default to global job counter */
228 return job->uargs.perf_counter_src0;
231 u32 mali_pp_job_get_perf_counter_src1(struct mali_pp_job *job, u32 sub_job)
233 /* Virtual jobs always use the global job counter (or if there are per sub job counters at all) */
234 if (mali_pp_job_is_virtual(job) || 0 == job->perf_counter_per_sub_job_count) {
235 /* Virtual jobs always use the global job counter */
236 return job->uargs.perf_counter_src1;
239 /* Use per sub job counter if enabled... */
240 if (MALI_HW_CORE_NO_COUNTER != job->perf_counter_per_sub_job_src1[sub_job]) {
241 return job->perf_counter_per_sub_job_src1[sub_job];
244 /* ...else default to global job counter */
245 return job->uargs.perf_counter_src1;
248 void mali_pp_job_set_pp_counter_global_src0(u32 counter)
250 pp_counter_src0 = counter;
253 void mali_pp_job_set_pp_counter_global_src1(u32 counter)
255 pp_counter_src1 = counter;
258 void mali_pp_job_set_pp_counter_sub_job_src0(u32 sub_job, u32 counter)
260 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
262 if (MALI_HW_CORE_NO_COUNTER == pp_counter_per_sub_job_src0[sub_job]) {
263 /* increment count since existing counter was disabled */
264 _mali_osk_atomic_inc(&pp_counter_per_sub_job_count);
267 if (MALI_HW_CORE_NO_COUNTER == counter) {
268 /* decrement count since new counter is disabled */
269 _mali_osk_atomic_dec(&pp_counter_per_sub_job_count);
272 /* PS: A change from MALI_HW_CORE_NO_COUNTER to MALI_HW_CORE_NO_COUNTER will inc and dec, result will be 0 change */
274 pp_counter_per_sub_job_src0[sub_job] = counter;
277 void mali_pp_job_set_pp_counter_sub_job_src1(u32 sub_job, u32 counter)
279 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
281 if (MALI_HW_CORE_NO_COUNTER == pp_counter_per_sub_job_src1[sub_job]) {
282 /* increment count since existing counter was disabled */
283 _mali_osk_atomic_inc(&pp_counter_per_sub_job_count);
286 if (MALI_HW_CORE_NO_COUNTER == counter) {
287 /* decrement count since new counter is disabled */
288 _mali_osk_atomic_dec(&pp_counter_per_sub_job_count);
291 /* PS: A change from MALI_HW_CORE_NO_COUNTER to MALI_HW_CORE_NO_COUNTER will inc and dec, result will be 0 change */
293 pp_counter_per_sub_job_src1[sub_job] = counter;
296 u32 mali_pp_job_get_pp_counter_global_src0(void)
298 return pp_counter_src0;
301 u32 mali_pp_job_get_pp_counter_global_src1(void)
303 return pp_counter_src1;
306 u32 mali_pp_job_get_pp_counter_sub_job_src0(u32 sub_job)
308 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
309 return pp_counter_per_sub_job_src0[sub_job];
312 u32 mali_pp_job_get_pp_counter_sub_job_src1(u32 sub_job)
314 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
315 return pp_counter_per_sub_job_src1[sub_job];