2 * Copyright (C) 2011-2012 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"
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"
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 };
29 void mali_pp_job_initialize(void)
31 _mali_osk_atomic_init(&pp_counter_per_sub_job_count, 0);
34 void mali_pp_job_terminate(void)
36 _mali_osk_atomic_term(&pp_counter_per_sub_job_count);
39 struct mali_pp_job *mali_pp_job_create(struct mali_session_data *session, _mali_uk_pp_start_job_s *uargs, u32 id)
41 struct mali_pp_job *job;
42 u32 perf_counter_flag;
44 job = _mali_osk_calloc(1, sizeof(struct mali_pp_job));
46 if (0 != _mali_osk_copy_from_user(&job->uargs, uargs, sizeof(_mali_uk_pp_start_job_s))) {
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"));
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;
60 perf_counter_flag = mali_pp_job_get_perf_counter_flag(job);
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);
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;
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));
80 _mali_osk_list_init(&job->list);
81 job->session = session;
82 _mali_osk_list_init(&job->session_list);
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();
89 job->num_memory_cookies = job->uargs.num_memory_cookies;
90 if (job->num_memory_cookies > 0) {
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"));
98 size = sizeof(*job->uargs.memory_cookies) * job->num_memory_cookies;
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));
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));
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"));
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);
126 if (_MALI_OSK_ERR_OK != err) {
127 MALI_PRINT_ERROR(("Mali PP job: Failed to allocate DMA command buffer\n"));
131 core = mali_pp_scheduler_get_virtual_pp();
132 MALI_DEBUG_ASSERT_POINTER(core);
134 mali_pp_job_dma_cmd_prepare(core, job, 0, MALI_FALSE, &job->dma_cmd_buf);
137 if (_MALI_OSK_ERR_OK != mali_pp_job_check(job)) {
138 /* Not a valid job. */
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));
150 mali_pp_job_delete(job);
156 void mali_pp_job_delete(struct mali_pp_job *job)
158 mali_dma_put_cmd_buf(&job->dma_cmd_buf);
159 if (NULL != job->finished_notification) {
160 _mali_osk_notification_delete(job->finished_notification);
163 _mali_osk_free(job->memory_cookies);
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);
171 _mali_osk_free(job->dma_bufs);
172 #endif /* CONFIG_DMA_SHARED_BUFFER */
177 u32 mali_pp_job_get_perf_counter_src0(struct mali_pp_job *job, u32 sub_job)
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;
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];
189 /* ...else default to global job counter */
190 return job->uargs.perf_counter_src0;
193 u32 mali_pp_job_get_perf_counter_src1(struct mali_pp_job *job, u32 sub_job)
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;
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];
206 /* ...else default to global job counter */
207 return job->uargs.perf_counter_src1;
210 void mali_pp_job_set_pp_counter_global_src0(u32 counter)
212 pp_counter_src0 = counter;
215 void mali_pp_job_set_pp_counter_global_src1(u32 counter)
217 pp_counter_src1 = counter;
220 void mali_pp_job_set_pp_counter_sub_job_src0(u32 sub_job, u32 counter)
222 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
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);
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);
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 */
236 pp_counter_per_sub_job_src0[sub_job] = counter;
239 void mali_pp_job_set_pp_counter_sub_job_src1(u32 sub_job, u32 counter)
241 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
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);
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);
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 */
255 pp_counter_per_sub_job_src1[sub_job] = counter;
258 u32 mali_pp_job_get_pp_counter_global_src0(void)
260 return pp_counter_src0;
263 u32 mali_pp_job_get_pp_counter_global_src1(void)
265 return pp_counter_src1;
268 u32 mali_pp_job_get_pp_counter_sub_job_src0(u32 sub_job)
270 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
271 return pp_counter_per_sub_job_src0[sub_job];
274 u32 mali_pp_job_get_pp_counter_sub_job_src1(u32 sub_job)
276 MALI_DEBUG_ASSERT(sub_job < _MALI_PP_MAX_SUB_JOBS);
277 return pp_counter_per_sub_job_src1[sub_job];