1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3 *
4 * (C) COPYRIGHT 2011-2022 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 #include <mali_kbase.h>
23
24 #include <linux/dma-buf.h>
25 #include <asm/cacheflush.h>
26 #if IS_ENABLED(CONFIG_SYNC_FILE)
27 #include <mali_kbase_sync.h>
28 #endif
29 #include <linux/dma-mapping.h>
30 #include <uapi/gpu/arm/bifrost/mali_base_kernel.h>
31 #include <mali_kbase_hwaccess_time.h>
32 #include <mali_kbase_kinstr_jm.h>
33 #include <mali_kbase_mem_linux.h>
34 #include <tl/mali_kbase_tracepoints.h>
35 #include <mali_linux_trace.h>
36 #include <linux/version.h>
37 #include <linux/ktime.h>
38 #include <linux/pfn.h>
39 #include <linux/sched.h>
40 #include <linux/kernel.h>
41 #include <linux/cache.h>
42
43 #if !MALI_USE_CSF
44 /**
45 * DOC: This file implements the logic behind software only jobs that are
46 * executed within the driver rather than being handed over to the GPU.
47 */
48
kbasep_add_waiting_soft_job(struct kbase_jd_atom * katom)49 static void kbasep_add_waiting_soft_job(struct kbase_jd_atom *katom)
50 {
51 struct kbase_context *kctx = katom->kctx;
52 unsigned long lflags;
53
54 spin_lock_irqsave(&kctx->waiting_soft_jobs_lock, lflags);
55 list_add_tail(&katom->queue, &kctx->waiting_soft_jobs);
56 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
57 }
58
kbasep_remove_waiting_soft_job(struct kbase_jd_atom * katom)59 void kbasep_remove_waiting_soft_job(struct kbase_jd_atom *katom)
60 {
61 struct kbase_context *kctx = katom->kctx;
62 unsigned long lflags;
63
64 spin_lock_irqsave(&kctx->waiting_soft_jobs_lock, lflags);
65 list_del(&katom->queue);
66 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
67 }
68
kbasep_add_waiting_with_timeout(struct kbase_jd_atom * katom)69 static void kbasep_add_waiting_with_timeout(struct kbase_jd_atom *katom)
70 {
71 struct kbase_context *kctx = katom->kctx;
72
73 /* Record the start time of this atom so we could cancel it at
74 * the right time.
75 */
76 katom->start_timestamp = ktime_get_raw();
77
78 /* Add the atom to the waiting list before the timer is
79 * (re)started to make sure that it gets processed.
80 */
81 kbasep_add_waiting_soft_job(katom);
82
83 /* Schedule timeout of this atom after a period if it is not active */
84 if (!timer_pending(&kctx->soft_job_timeout)) {
85 int timeout_ms = atomic_read(
86 &kctx->kbdev->js_data.soft_job_timeout_ms);
87 mod_timer(&kctx->soft_job_timeout,
88 jiffies + msecs_to_jiffies(timeout_ms));
89 }
90 }
91
kbasep_read_soft_event_status(struct kbase_context * kctx,u64 evt,unsigned char * status)92 static int kbasep_read_soft_event_status(
93 struct kbase_context *kctx, u64 evt, unsigned char *status)
94 {
95 unsigned char *mapped_evt;
96 struct kbase_vmap_struct map;
97
98 mapped_evt = kbase_vmap_prot(kctx, evt, sizeof(*mapped_evt),
99 KBASE_REG_CPU_RD, &map);
100 if (!mapped_evt)
101 return -EFAULT;
102
103 *status = *mapped_evt;
104
105 kbase_vunmap(kctx, &map);
106
107 return 0;
108 }
109
kbasep_write_soft_event_status(struct kbase_context * kctx,u64 evt,unsigned char new_status)110 static int kbasep_write_soft_event_status(
111 struct kbase_context *kctx, u64 evt, unsigned char new_status)
112 {
113 unsigned char *mapped_evt;
114 struct kbase_vmap_struct map;
115
116 if ((new_status != BASE_JD_SOFT_EVENT_SET) &&
117 (new_status != BASE_JD_SOFT_EVENT_RESET))
118 return -EINVAL;
119
120 mapped_evt = kbase_vmap_prot(kctx, evt, sizeof(*mapped_evt),
121 KBASE_REG_CPU_WR, &map);
122 if (!mapped_evt)
123 return -EFAULT;
124
125 *mapped_evt = new_status;
126
127 kbase_vunmap(kctx, &map);
128
129 return 0;
130 }
131
kbase_dump_cpu_gpu_time(struct kbase_jd_atom * katom)132 static int kbase_dump_cpu_gpu_time(struct kbase_jd_atom *katom)
133 {
134 struct kbase_vmap_struct map;
135 void *user_result;
136 struct timespec64 ts;
137 struct base_dump_cpu_gpu_counters data;
138 u64 system_time = 0ULL;
139 u64 cycle_counter;
140 u64 jc = katom->jc;
141 struct kbase_context *kctx = katom->kctx;
142 int pm_active_err;
143
144 memset(&data, 0, sizeof(data));
145
146 /* Take the PM active reference as late as possible - otherwise, it could
147 * delay suspend until we process the atom (which may be at the end of a
148 * long chain of dependencies
149 */
150 #ifdef CONFIG_MALI_ARBITER_SUPPORT
151 atomic_inc(&kctx->kbdev->pm.gpu_users_waiting);
152 #endif /* CONFIG_MALI_ARBITER_SUPPORT */
153 pm_active_err = kbase_pm_context_active_handle_suspend(kctx->kbdev, KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE);
154 if (pm_active_err) {
155 struct kbasep_js_device_data *js_devdata = &kctx->kbdev->js_data;
156
157 /* We're suspended - queue this on the list of suspended jobs
158 * Use dep_item[1], because dep_item[0] was previously in use
159 * for 'waiting_soft_jobs'.
160 */
161 mutex_lock(&js_devdata->runpool_mutex);
162 list_add_tail(&katom->dep_item[1], &js_devdata->suspended_soft_jobs_list);
163 mutex_unlock(&js_devdata->runpool_mutex);
164
165 /* Also adding this to the list of waiting soft job */
166 kbasep_add_waiting_soft_job(katom);
167
168 return pm_active_err;
169 }
170 #ifdef CONFIG_MALI_ARBITER_SUPPORT
171 else
172 atomic_dec(&kctx->kbdev->pm.gpu_users_waiting);
173 #endif /* CONFIG_MALI_ARBITER_SUPPORT */
174
175 kbase_backend_get_gpu_time(kctx->kbdev, &cycle_counter, &system_time,
176 &ts);
177
178 kbase_pm_context_idle(kctx->kbdev);
179
180 data.sec = ts.tv_sec;
181 data.usec = ts.tv_nsec / 1000;
182 data.system_time = system_time;
183 data.cycle_counter = cycle_counter;
184
185 /* Assume this atom will be cancelled until we know otherwise */
186 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
187
188 /* GPU_WR access is checked on the range for returning the result to
189 * userspace for the following reasons:
190 * - security, this is currently how imported user bufs are checked.
191 * - userspace ddk guaranteed to assume region was mapped as GPU_WR
192 */
193 user_result = kbase_vmap_prot(kctx, jc, sizeof(data), KBASE_REG_GPU_WR, &map);
194 if (!user_result)
195 return 0;
196
197 memcpy(user_result, &data, sizeof(data));
198
199 kbase_vunmap(kctx, &map);
200
201 /* Atom was fine - mark it as done */
202 katom->event_code = BASE_JD_EVENT_DONE;
203
204 return 0;
205 }
206
207 #if IS_ENABLED(CONFIG_SYNC_FILE)
208 /* Called by the explicit fence mechanism when a fence wait has completed */
kbase_soft_event_wait_callback(struct kbase_jd_atom * katom)209 void kbase_soft_event_wait_callback(struct kbase_jd_atom *katom)
210 {
211 struct kbase_context *kctx = katom->kctx;
212
213 mutex_lock(&kctx->jctx.lock);
214 kbasep_remove_waiting_soft_job(katom);
215 kbase_finish_soft_job(katom);
216 if (kbase_jd_done_nolock(katom, true))
217 kbase_js_sched_all(kctx->kbdev);
218 mutex_unlock(&kctx->jctx.lock);
219 }
220 #endif
221
kbasep_soft_event_complete_job(struct work_struct * work)222 static void kbasep_soft_event_complete_job(struct work_struct *work)
223 {
224 struct kbase_jd_atom *katom = container_of(work, struct kbase_jd_atom,
225 work);
226 struct kbase_context *kctx = katom->kctx;
227 int resched;
228
229 mutex_lock(&kctx->jctx.lock);
230 resched = kbase_jd_done_nolock(katom, true);
231 mutex_unlock(&kctx->jctx.lock);
232
233 if (resched)
234 kbase_js_sched_all(kctx->kbdev);
235 }
236
kbasep_complete_triggered_soft_events(struct kbase_context * kctx,u64 evt)237 void kbasep_complete_triggered_soft_events(struct kbase_context *kctx, u64 evt)
238 {
239 int cancel_timer = 1;
240 struct list_head *entry, *tmp;
241 unsigned long lflags;
242
243 spin_lock_irqsave(&kctx->waiting_soft_jobs_lock, lflags);
244 list_for_each_safe(entry, tmp, &kctx->waiting_soft_jobs) {
245 struct kbase_jd_atom *katom = list_entry(
246 entry, struct kbase_jd_atom, queue);
247
248 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
249 case BASE_JD_REQ_SOFT_EVENT_WAIT:
250 if (katom->jc == evt) {
251 list_del(&katom->queue);
252
253 katom->event_code = BASE_JD_EVENT_DONE;
254 INIT_WORK(&katom->work,
255 kbasep_soft_event_complete_job);
256 queue_work(kctx->jctx.job_done_wq,
257 &katom->work);
258 } else {
259 /* There are still other waiting jobs, we cannot
260 * cancel the timer yet.
261 */
262 cancel_timer = 0;
263 }
264 break;
265 #ifdef CONFIG_MALI_BIFROST_FENCE_DEBUG
266 case BASE_JD_REQ_SOFT_FENCE_WAIT:
267 /* Keep the timer running if fence debug is enabled and
268 * there are waiting fence jobs.
269 */
270 cancel_timer = 0;
271 break;
272 #endif
273 }
274 }
275
276 if (cancel_timer)
277 del_timer(&kctx->soft_job_timeout);
278 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
279 }
280
281 #ifdef CONFIG_MALI_BIFROST_FENCE_DEBUG
kbase_fence_debug_check_atom(struct kbase_jd_atom * katom)282 static void kbase_fence_debug_check_atom(struct kbase_jd_atom *katom)
283 {
284 struct kbase_context *kctx = katom->kctx;
285 struct device *dev = kctx->kbdev->dev;
286 int i;
287
288 for (i = 0; i < 2; i++) {
289 struct kbase_jd_atom *dep;
290
291 list_for_each_entry(dep, &katom->dep_head[i], dep_item[i]) {
292 if (dep->status == KBASE_JD_ATOM_STATE_UNUSED ||
293 dep->status == KBASE_JD_ATOM_STATE_COMPLETED)
294 continue;
295
296 if ((dep->core_req & BASE_JD_REQ_SOFT_JOB_TYPE)
297 == BASE_JD_REQ_SOFT_FENCE_TRIGGER) {
298 /* Found blocked trigger fence. */
299 struct kbase_sync_fence_info info;
300
301 if (!kbase_sync_fence_in_info_get(dep, &info)) {
302 dev_warn(dev,
303 "\tVictim trigger atom %d fence [%pK] %s: %s\n",
304 kbase_jd_atom_id(kctx, dep),
305 info.fence,
306 info.name,
307 kbase_sync_status_string(info.status));
308 }
309 }
310
311 kbase_fence_debug_check_atom(dep);
312 }
313 }
314 }
315
kbase_fence_debug_wait_timeout(struct kbase_jd_atom * katom)316 static void kbase_fence_debug_wait_timeout(struct kbase_jd_atom *katom)
317 {
318 struct kbase_context *kctx = katom->kctx;
319 struct device *dev = katom->kctx->kbdev->dev;
320 int timeout_ms = atomic_read(&kctx->kbdev->js_data.soft_job_timeout_ms);
321 unsigned long lflags;
322 struct kbase_sync_fence_info info;
323
324 spin_lock_irqsave(&kctx->waiting_soft_jobs_lock, lflags);
325
326 if (kbase_sync_fence_in_info_get(katom, &info)) {
327 /* Fence must have signaled just after timeout. */
328 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
329 return;
330 }
331
332 dev_warn(dev, "ctx %d_%d: Atom %d still waiting for fence [%pK] after %dms\n",
333 kctx->tgid, kctx->id,
334 kbase_jd_atom_id(kctx, katom),
335 info.fence, timeout_ms);
336 dev_warn(dev, "\tGuilty fence [%pK] %s: %s\n",
337 info.fence, info.name,
338 kbase_sync_status_string(info.status));
339
340 /* Search for blocked trigger atoms */
341 kbase_fence_debug_check_atom(katom);
342
343 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
344
345 kbase_sync_fence_in_dump(katom);
346 }
347
348 struct kbase_fence_debug_work {
349 struct kbase_jd_atom *katom;
350 struct work_struct work;
351 };
352
kbase_fence_debug_wait_timeout_worker(struct work_struct * work)353 static void kbase_fence_debug_wait_timeout_worker(struct work_struct *work)
354 {
355 struct kbase_fence_debug_work *w = container_of(work,
356 struct kbase_fence_debug_work, work);
357 struct kbase_jd_atom *katom = w->katom;
358 struct kbase_context *kctx = katom->kctx;
359
360 mutex_lock(&kctx->jctx.lock);
361 kbase_fence_debug_wait_timeout(katom);
362 mutex_unlock(&kctx->jctx.lock);
363
364 kfree(w);
365 }
366
kbase_fence_debug_timeout(struct kbase_jd_atom * katom)367 static void kbase_fence_debug_timeout(struct kbase_jd_atom *katom)
368 {
369 struct kbase_fence_debug_work *work;
370 struct kbase_context *kctx = katom->kctx;
371
372 /* Enqueue fence debug worker. Use job_done_wq to get
373 * debug print ordered with job completion.
374 */
375 work = kzalloc(sizeof(struct kbase_fence_debug_work), GFP_ATOMIC);
376 /* Ignore allocation failure. */
377 if (work) {
378 work->katom = katom;
379 INIT_WORK(&work->work, kbase_fence_debug_wait_timeout_worker);
380 queue_work(kctx->jctx.job_done_wq, &work->work);
381 }
382 }
383 #endif /* CONFIG_MALI_BIFROST_FENCE_DEBUG */
384
kbasep_soft_job_timeout_worker(struct timer_list * timer)385 void kbasep_soft_job_timeout_worker(struct timer_list *timer)
386 {
387 struct kbase_context *kctx = container_of(timer, struct kbase_context,
388 soft_job_timeout);
389 u32 timeout_ms = (u32)atomic_read(
390 &kctx->kbdev->js_data.soft_job_timeout_ms);
391 ktime_t cur_time = ktime_get_raw();
392 bool restarting = false;
393 unsigned long lflags;
394 struct list_head *entry, *tmp;
395
396 spin_lock_irqsave(&kctx->waiting_soft_jobs_lock, lflags);
397 list_for_each_safe(entry, tmp, &kctx->waiting_soft_jobs) {
398 struct kbase_jd_atom *katom = list_entry(entry,
399 struct kbase_jd_atom, queue);
400 s64 elapsed_time = ktime_to_ms(ktime_sub(cur_time,
401 katom->start_timestamp));
402
403 if (elapsed_time < (s64)timeout_ms) {
404 restarting = true;
405 continue;
406 }
407
408 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
409 case BASE_JD_REQ_SOFT_EVENT_WAIT:
410 /* Take it out of the list to ensure that it
411 * will be cancelled in all cases
412 */
413 list_del(&katom->queue);
414
415 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
416 INIT_WORK(&katom->work, kbasep_soft_event_complete_job);
417 queue_work(kctx->jctx.job_done_wq, &katom->work);
418 break;
419 #ifdef CONFIG_MALI_BIFROST_FENCE_DEBUG
420 case BASE_JD_REQ_SOFT_FENCE_WAIT:
421 kbase_fence_debug_timeout(katom);
422 break;
423 #endif
424 }
425 }
426
427 if (restarting)
428 mod_timer(timer, jiffies + msecs_to_jiffies(timeout_ms));
429 spin_unlock_irqrestore(&kctx->waiting_soft_jobs_lock, lflags);
430 }
431
kbasep_soft_event_wait(struct kbase_jd_atom * katom)432 static int kbasep_soft_event_wait(struct kbase_jd_atom *katom)
433 {
434 struct kbase_context *kctx = katom->kctx;
435 unsigned char status;
436
437 /* The status of this soft-job is stored in jc */
438 if (kbasep_read_soft_event_status(kctx, katom->jc, &status)) {
439 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
440 return 0;
441 }
442
443 if (status == BASE_JD_SOFT_EVENT_SET)
444 return 0; /* Event already set, nothing to do */
445
446 kbasep_add_waiting_with_timeout(katom);
447
448 return 1;
449 }
450
kbasep_soft_event_update_locked(struct kbase_jd_atom * katom,unsigned char new_status)451 static void kbasep_soft_event_update_locked(struct kbase_jd_atom *katom,
452 unsigned char new_status)
453 {
454 /* Complete jobs waiting on the same event */
455 struct kbase_context *kctx = katom->kctx;
456
457 if (kbasep_write_soft_event_status(kctx, katom->jc, new_status) != 0) {
458 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
459 return;
460 }
461
462 if (new_status == BASE_JD_SOFT_EVENT_SET)
463 kbasep_complete_triggered_soft_events(kctx, katom->jc);
464 }
465
466 /**
467 * kbase_soft_event_update() - Update soft event state
468 * @kctx: Pointer to context
469 * @event: Event to update
470 * @new_status: New status value of event
471 *
472 * Update the event, and wake up any atoms waiting for the event.
473 *
474 * Return: 0 on success, a negative error code on failure.
475 */
kbase_soft_event_update(struct kbase_context * kctx,u64 event,unsigned char new_status)476 int kbase_soft_event_update(struct kbase_context *kctx,
477 u64 event,
478 unsigned char new_status)
479 {
480 int err = 0;
481
482 mutex_lock(&kctx->jctx.lock);
483
484 if (kbasep_write_soft_event_status(kctx, event, new_status)) {
485 err = -ENOENT;
486 goto out;
487 }
488
489 if (new_status == BASE_JD_SOFT_EVENT_SET)
490 kbasep_complete_triggered_soft_events(kctx, event);
491
492 out:
493 mutex_unlock(&kctx->jctx.lock);
494
495 return err;
496 }
497
kbasep_soft_event_cancel_job(struct kbase_jd_atom * katom)498 static void kbasep_soft_event_cancel_job(struct kbase_jd_atom *katom)
499 {
500 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
501 if (kbase_jd_done_nolock(katom, true))
502 kbase_js_sched_all(katom->kctx->kbdev);
503 }
504
505 #if IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST
kbase_debug_copy_finish(struct kbase_jd_atom * katom)506 static void kbase_debug_copy_finish(struct kbase_jd_atom *katom)
507 {
508 struct kbase_debug_copy_buffer *buffers = katom->softjob_data;
509 unsigned int i;
510 unsigned int nr = katom->nr_extres;
511
512 if (!buffers)
513 return;
514
515 kbase_gpu_vm_lock(katom->kctx);
516 for (i = 0; i < nr; i++) {
517 int p;
518 struct kbase_mem_phy_alloc *gpu_alloc = buffers[i].gpu_alloc;
519
520 if (!buffers[i].pages)
521 break;
522 for (p = 0; p < buffers[i].nr_pages; p++) {
523 struct page *pg = buffers[i].pages[p];
524
525 if (pg)
526 put_page(pg);
527 }
528 if (buffers[i].is_vmalloc)
529 vfree(buffers[i].pages);
530 else
531 kfree(buffers[i].pages);
532 if (gpu_alloc) {
533 switch (gpu_alloc->type) {
534 case KBASE_MEM_TYPE_IMPORTED_USER_BUF:
535 {
536 kbase_free_user_buffer(&buffers[i]);
537 break;
538 }
539 default:
540 /* Nothing to be done. */
541 break;
542 }
543 kbase_mem_phy_alloc_put(gpu_alloc);
544 }
545 }
546 kbase_gpu_vm_unlock(katom->kctx);
547 kfree(buffers);
548
549 katom->softjob_data = NULL;
550 }
551
kbase_debug_copy_prepare(struct kbase_jd_atom * katom)552 static int kbase_debug_copy_prepare(struct kbase_jd_atom *katom)
553 {
554 struct kbase_debug_copy_buffer *buffers;
555 struct base_jd_debug_copy_buffer *user_buffers = NULL;
556 unsigned int i;
557 unsigned int nr = katom->nr_extres;
558 int ret = 0;
559 void __user *user_structs = (void __user *)(uintptr_t)katom->jc;
560
561 if (!user_structs)
562 return -EINVAL;
563
564 buffers = kcalloc(nr, sizeof(*buffers), GFP_KERNEL);
565 if (!buffers) {
566 ret = -ENOMEM;
567 goto out_cleanup;
568 }
569 katom->softjob_data = buffers;
570
571 user_buffers = kmalloc_array(nr, sizeof(*user_buffers), GFP_KERNEL);
572
573 if (!user_buffers) {
574 ret = -ENOMEM;
575 goto out_cleanup;
576 }
577
578 ret = copy_from_user(user_buffers, user_structs,
579 sizeof(*user_buffers)*nr);
580 if (ret) {
581 ret = -EFAULT;
582 goto out_cleanup;
583 }
584
585 for (i = 0; i < nr; i++) {
586 u64 addr = user_buffers[i].address;
587 u64 page_addr = addr & PAGE_MASK;
588 u64 end_page_addr = addr + user_buffers[i].size - 1;
589 u64 last_page_addr = end_page_addr & PAGE_MASK;
590 int nr_pages = (last_page_addr-page_addr)/PAGE_SIZE+1;
591 int pinned_pages;
592 struct kbase_va_region *reg;
593 struct base_external_resource user_extres;
594
595 if (!addr)
596 continue;
597
598 if (last_page_addr < page_addr) {
599 ret = -EINVAL;
600 goto out_cleanup;
601 }
602
603 buffers[i].nr_pages = nr_pages;
604 buffers[i].offset = addr & ~PAGE_MASK;
605 if (buffers[i].offset >= PAGE_SIZE) {
606 ret = -EINVAL;
607 goto out_cleanup;
608 }
609 buffers[i].size = user_buffers[i].size;
610
611 if (nr_pages > (KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD /
612 sizeof(struct page *))) {
613 buffers[i].is_vmalloc = true;
614 buffers[i].pages = vzalloc(nr_pages *
615 sizeof(struct page *));
616 } else {
617 buffers[i].is_vmalloc = false;
618 buffers[i].pages = kcalloc(nr_pages,
619 sizeof(struct page *), GFP_KERNEL);
620 }
621
622 if (!buffers[i].pages) {
623 ret = -ENOMEM;
624 goto out_cleanup;
625 }
626
627 pinned_pages = get_user_pages_fast(page_addr,
628 nr_pages,
629 1, /* Write */
630 buffers[i].pages);
631 if (pinned_pages < 0) {
632 /* get_user_pages_fast has failed - page array is not
633 * valid. Don't try to release any pages.
634 */
635 buffers[i].nr_pages = 0;
636
637 ret = pinned_pages;
638 goto out_cleanup;
639 }
640 if (pinned_pages != nr_pages) {
641 /* Adjust number of pages, so that we only attempt to
642 * release pages in the array that we know are valid.
643 */
644 buffers[i].nr_pages = pinned_pages;
645
646 ret = -EINVAL;
647 goto out_cleanup;
648 }
649
650 user_extres = user_buffers[i].extres;
651 if (user_extres.ext_resource == 0ULL) {
652 ret = -EINVAL;
653 goto out_cleanup;
654 }
655
656 kbase_gpu_vm_lock(katom->kctx);
657 reg = kbase_region_tracker_find_region_enclosing_address(
658 katom->kctx, user_extres.ext_resource &
659 ~BASE_EXT_RES_ACCESS_EXCLUSIVE);
660
661 if (kbase_is_region_invalid_or_free(reg) ||
662 reg->gpu_alloc == NULL) {
663 ret = -EINVAL;
664 goto out_unlock;
665 }
666
667 buffers[i].gpu_alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
668 buffers[i].nr_extres_pages = reg->nr_pages;
669
670 if (reg->nr_pages*PAGE_SIZE != buffers[i].size)
671 dev_warn(katom->kctx->kbdev->dev, "Copy buffer is not of same size as the external resource to copy.\n");
672
673 switch (reg->gpu_alloc->type) {
674 case KBASE_MEM_TYPE_IMPORTED_USER_BUF:
675 {
676 struct kbase_mem_phy_alloc *alloc = reg->gpu_alloc;
677 const unsigned long nr_pages = alloc->imported.user_buf.nr_pages;
678 const unsigned long start = alloc->imported.user_buf.address;
679
680 if (alloc->imported.user_buf.mm != current->mm) {
681 ret = -EINVAL;
682 goto out_unlock;
683 }
684 buffers[i].extres_pages = kcalloc(nr_pages,
685 sizeof(struct page *), GFP_KERNEL);
686 if (!buffers[i].extres_pages) {
687 ret = -ENOMEM;
688 goto out_unlock;
689 }
690 kbase_gpu_vm_unlock(katom->kctx);
691 ret = get_user_pages_fast(start, nr_pages, 0, buffers[i].extres_pages);
692 kbase_gpu_vm_lock(katom->kctx);
693 if (ret != nr_pages) {
694 /* Adjust number of pages, so that we only
695 * attempt to release pages in the array that we
696 * know are valid.
697 */
698 if (ret < 0)
699 buffers[i].nr_extres_pages = 0;
700 else
701 buffers[i].nr_extres_pages = ret;
702
703 goto out_unlock;
704 }
705 ret = 0;
706 break;
707 }
708 default:
709 /* Nothing to be done. */
710 break;
711 }
712 kbase_gpu_vm_unlock(katom->kctx);
713 }
714 kfree(user_buffers);
715
716 return ret;
717
718 out_unlock:
719 kbase_gpu_vm_unlock(katom->kctx);
720
721 out_cleanup:
722 /* Frees allocated memory for kbase_debug_copy_job struct, including
723 * members, and sets jc to 0
724 */
725 kbase_debug_copy_finish(katom);
726 kfree(user_buffers);
727
728 return ret;
729 }
730
731 #if KERNEL_VERSION(5, 6, 0) <= LINUX_VERSION_CODE
dma_buf_kmap_page(struct kbase_mem_phy_alloc * gpu_alloc,unsigned long page_num,struct page ** page)732 static void *dma_buf_kmap_page(struct kbase_mem_phy_alloc *gpu_alloc,
733 unsigned long page_num, struct page **page)
734 {
735 struct sg_table *sgt = gpu_alloc->imported.umm.sgt;
736 struct sg_page_iter sg_iter;
737 unsigned long page_index = 0;
738
739 if (WARN_ON(gpu_alloc->type != KBASE_MEM_TYPE_IMPORTED_UMM))
740 return NULL;
741
742 if (!sgt)
743 return NULL;
744
745 if (WARN_ON(page_num >= gpu_alloc->nents))
746 return NULL;
747
748 for_each_sg_page(sgt->sgl, &sg_iter, sgt->nents, 0) {
749 if (page_index == page_num) {
750 *page = sg_page_iter_page(&sg_iter);
751
752 return kmap(*page);
753 }
754 page_index++;
755 }
756
757 return NULL;
758 }
759 #endif
760
761 /**
762 * kbase_mem_copy_from_extres() - Copy from external resources.
763 *
764 * @kctx: kbase context within which the copying is to take place.
765 * @buf_data: Pointer to the information about external resources:
766 * pages pertaining to the external resource, number of
767 * pages to copy.
768 *
769 * Return: 0 on success, error code otherwise.
770 */
kbase_mem_copy_from_extres(struct kbase_context * kctx,struct kbase_debug_copy_buffer * buf_data)771 static int kbase_mem_copy_from_extres(struct kbase_context *kctx,
772 struct kbase_debug_copy_buffer *buf_data)
773 {
774 unsigned int i;
775 unsigned int target_page_nr = 0;
776 struct page **pages = buf_data->pages;
777 u64 offset = buf_data->offset;
778 size_t extres_size = buf_data->nr_extres_pages*PAGE_SIZE;
779 size_t to_copy = min(extres_size, buf_data->size);
780 struct kbase_mem_phy_alloc *gpu_alloc = buf_data->gpu_alloc;
781 int ret = 0;
782 size_t dma_to_copy;
783
784 KBASE_DEBUG_ASSERT(pages != NULL);
785
786 kbase_gpu_vm_lock(kctx);
787 if (!gpu_alloc) {
788 ret = -EINVAL;
789 goto out_unlock;
790 }
791
792 switch (gpu_alloc->type) {
793 case KBASE_MEM_TYPE_IMPORTED_USER_BUF:
794 {
795 for (i = 0; i < buf_data->nr_extres_pages &&
796 target_page_nr < buf_data->nr_pages; i++) {
797 struct page *pg = buf_data->extres_pages[i];
798 void *extres_page = kmap(pg);
799
800 if (extres_page) {
801 ret = kbase_mem_copy_to_pinned_user_pages(
802 pages, extres_page, &to_copy,
803 buf_data->nr_pages,
804 &target_page_nr, offset);
805 kunmap(pg);
806 if (ret)
807 goto out_unlock;
808 }
809 }
810 }
811 break;
812 case KBASE_MEM_TYPE_IMPORTED_UMM: {
813 struct dma_buf *dma_buf = gpu_alloc->imported.umm.dma_buf;
814
815 KBASE_DEBUG_ASSERT(dma_buf != NULL);
816 if (dma_buf->size > buf_data->nr_extres_pages * PAGE_SIZE)
817 dev_warn(kctx->kbdev->dev, "External resources buffer size mismatch");
818
819 dma_to_copy = min(dma_buf->size,
820 (size_t)(buf_data->nr_extres_pages * PAGE_SIZE));
821 ret = dma_buf_begin_cpu_access(dma_buf, DMA_FROM_DEVICE);
822 if (ret)
823 goto out_unlock;
824
825 for (i = 0; i < dma_to_copy/PAGE_SIZE &&
826 target_page_nr < buf_data->nr_pages; i++) {
827 #if KERNEL_VERSION(5, 6, 0) <= LINUX_VERSION_CODE
828 struct page *pg;
829 void *extres_page = dma_buf_kmap_page(gpu_alloc, i, &pg);
830 #else
831 void *extres_page = dma_buf_kmap(dma_buf, i);
832 #endif
833 if (extres_page) {
834 ret = kbase_mem_copy_to_pinned_user_pages(
835 pages, extres_page, &to_copy,
836 buf_data->nr_pages,
837 &target_page_nr, offset);
838
839 #if KERNEL_VERSION(5, 6, 0) <= LINUX_VERSION_CODE
840 kunmap(pg);
841 #else
842 dma_buf_kunmap(dma_buf, i, extres_page);
843 #endif
844 if (ret)
845 break;
846 }
847 }
848 dma_buf_end_cpu_access(dma_buf, DMA_FROM_DEVICE);
849 break;
850 }
851 default:
852 ret = -EINVAL;
853 }
854 out_unlock:
855 kbase_gpu_vm_unlock(kctx);
856 return ret;
857 }
858
kbase_debug_copy(struct kbase_jd_atom * katom)859 static int kbase_debug_copy(struct kbase_jd_atom *katom)
860 {
861 struct kbase_debug_copy_buffer *buffers = katom->softjob_data;
862 unsigned int i;
863
864 if (WARN_ON(!buffers))
865 return -EINVAL;
866
867 for (i = 0; i < katom->nr_extres; i++) {
868 int res = kbase_mem_copy_from_extres(katom->kctx, &buffers[i]);
869
870 if (res)
871 return res;
872 }
873
874 return 0;
875 }
876 #endif /* IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST */
877 #endif /* !MALI_USE_CSF */
878
879 #define KBASEP_JIT_ALLOC_GPU_ADDR_ALIGNMENT ((u32)0x7)
880
kbasep_jit_alloc_validate(struct kbase_context * kctx,struct base_jit_alloc_info * info)881 int kbasep_jit_alloc_validate(struct kbase_context *kctx,
882 struct base_jit_alloc_info *info)
883 {
884 int j;
885 /* If the ID is zero, then fail the job */
886 if (info->id == 0)
887 return -EINVAL;
888
889 /* Sanity check that the PA fits within the VA */
890 if (info->va_pages < info->commit_pages)
891 return -EINVAL;
892
893 /* Ensure the GPU address is correctly aligned */
894 if ((info->gpu_alloc_addr & KBASEP_JIT_ALLOC_GPU_ADDR_ALIGNMENT) != 0)
895 return -EINVAL;
896
897 /* Interface version 2 (introduced with kernel driver version 11.5)
898 * onward has padding and a flags member to validate.
899 *
900 * Note: To support earlier versions the extra bytes will have been set
901 * to 0 by the caller.
902 */
903
904 /* Check padding is all zeroed */
905 for (j = 0; j < sizeof(info->padding); j++) {
906 if (info->padding[j] != 0)
907 return -EINVAL;
908 }
909
910 /* Only valid flags shall be set */
911 if (info->flags & ~(BASE_JIT_ALLOC_VALID_FLAGS))
912 return -EINVAL;
913
914 #if !MALI_JIT_PRESSURE_LIMIT_BASE
915 /* If just-in-time memory allocation pressure limit feature is disabled,
916 * heap_info_gpu_addr must be zeroed-out
917 */
918 if (info->heap_info_gpu_addr)
919 return -EINVAL;
920 #endif
921
922 #if !MALI_USE_CSF
923 /* If BASE_JIT_ALLOC_HEAP_INFO_IS_SIZE is set, heap_info_gpu_addr
924 * cannot be 0
925 */
926 if ((info->flags & BASE_JIT_ALLOC_HEAP_INFO_IS_SIZE) &&
927 !info->heap_info_gpu_addr)
928 return -EINVAL;
929 #endif /* !MALI_USE_CSF */
930
931 return 0;
932 }
933
934 #if !MALI_USE_CSF
935
kbase_jit_allocate_prepare(struct kbase_jd_atom * katom)936 static int kbase_jit_allocate_prepare(struct kbase_jd_atom *katom)
937 {
938 __user u8 *data = (__user u8 *)(uintptr_t) katom->jc;
939 struct base_jit_alloc_info *info;
940 struct kbase_context *kctx = katom->kctx;
941 struct kbase_device *kbdev = kctx->kbdev;
942 u32 count;
943 int ret;
944 u32 i;
945
946 if (!kbase_mem_allow_alloc(kctx)) {
947 dev_dbg(kbdev->dev, "Invalid attempt to allocate JIT memory by %s/%d for ctx %d_%d",
948 current->comm, current->pid, kctx->tgid, kctx->id);
949 ret = -EINVAL;
950 goto fail;
951 }
952
953 /* For backwards compatibility, and to prevent reading more than 1 jit
954 * info struct on jit version 1
955 */
956 if (katom->nr_extres == 0)
957 katom->nr_extres = 1;
958 count = katom->nr_extres;
959
960 /* Sanity checks */
961 if (!data || count > kctx->jit_max_allocations ||
962 count > ARRAY_SIZE(kctx->jit_alloc)) {
963 ret = -EINVAL;
964 goto fail;
965 }
966
967 /* Copy the information for safe access and future storage */
968 info = kmalloc_array(count, sizeof(*info), GFP_KERNEL);
969 if (!info) {
970 ret = -ENOMEM;
971 goto fail;
972 }
973
974 katom->softjob_data = info;
975
976 for (i = 0; i < count; i++, info++, data += sizeof(*info)) {
977 if (copy_from_user(info, data, sizeof(*info)) != 0) {
978 ret = -EINVAL;
979 goto free_info;
980 }
981
982 ret = kbasep_jit_alloc_validate(kctx, info);
983 if (ret)
984 goto free_info;
985 KBASE_TLSTREAM_TL_ATTRIB_ATOM_JITALLOCINFO(
986 kbdev, katom, info->va_pages, info->commit_pages,
987 info->extension, info->id, info->bin_id,
988 info->max_allocations, info->flags, info->usage_id);
989 }
990
991 katom->jit_blocked = false;
992
993 lockdep_assert_held(&kctx->jctx.lock);
994 list_add_tail(&katom->jit_node, &kctx->jctx.jit_atoms_head);
995
996 /*
997 * Note:
998 * The provided info->gpu_alloc_addr isn't validated here as
999 * userland can cache allocations which means that even
1000 * though the region is valid it doesn't represent the
1001 * same thing it used to.
1002 *
1003 * Complete validation of va_pages, commit_pages and extension
1004 * isn't done here as it will be done during the call to
1005 * kbase_mem_alloc.
1006 */
1007 return 0;
1008
1009 free_info:
1010 kfree(katom->softjob_data);
1011 katom->softjob_data = NULL;
1012 fail:
1013 return ret;
1014 }
1015
kbase_jit_free_get_ids(struct kbase_jd_atom * katom)1016 static u8 *kbase_jit_free_get_ids(struct kbase_jd_atom *katom)
1017 {
1018 if (WARN_ON((katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) !=
1019 BASE_JD_REQ_SOFT_JIT_FREE))
1020 return NULL;
1021
1022 return (u8 *) katom->softjob_data;
1023 }
1024
kbase_jit_add_to_pending_alloc_list(struct kbase_jd_atom * katom)1025 static void kbase_jit_add_to_pending_alloc_list(struct kbase_jd_atom *katom)
1026 {
1027 struct kbase_context *kctx = katom->kctx;
1028 struct list_head *target_list_head = NULL;
1029 struct kbase_jd_atom *entry;
1030
1031 list_for_each_entry(entry, &kctx->jctx.jit_pending_alloc, queue) {
1032 if (katom->age < entry->age) {
1033 target_list_head = &entry->queue;
1034 break;
1035 }
1036 }
1037
1038 if (target_list_head == NULL)
1039 target_list_head = &kctx->jctx.jit_pending_alloc;
1040
1041 list_add_tail(&katom->queue, target_list_head);
1042 }
1043
kbase_jit_allocate_process(struct kbase_jd_atom * katom)1044 static int kbase_jit_allocate_process(struct kbase_jd_atom *katom)
1045 {
1046 struct kbase_context *kctx = katom->kctx;
1047 struct kbase_device *kbdev = kctx->kbdev;
1048 struct base_jit_alloc_info *info;
1049 struct kbase_va_region *reg;
1050 struct kbase_vmap_struct mapping;
1051 u64 *ptr, new_addr;
1052 u32 count = katom->nr_extres;
1053 u32 i;
1054 bool ignore_pressure_limit = false;
1055
1056 trace_sysgraph(SGR_SUBMIT, kctx->id,
1057 kbase_jd_atom_id(kctx, katom));
1058
1059 if (katom->jit_blocked) {
1060 list_del(&katom->queue);
1061 katom->jit_blocked = false;
1062 }
1063
1064 info = katom->softjob_data;
1065 if (WARN_ON(!info)) {
1066 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1067 return 0;
1068 }
1069
1070 for (i = 0; i < count; i++, info++) {
1071 /* The JIT ID is still in use so fail the allocation */
1072 if (kctx->jit_alloc[info->id]) {
1073 katom->event_code = BASE_JD_EVENT_MEM_GROWTH_FAILED;
1074 return 0;
1075 }
1076 }
1077
1078 #if MALI_JIT_PRESSURE_LIMIT_BASE
1079 /*
1080 * If this is the only JIT_ALLOC atom in-flight or if JIT pressure limit
1081 * is disabled at the context scope, then bypass JIT pressure limit
1082 * logic in kbase_jit_allocate().
1083 */
1084 if (!kbase_ctx_flag(kctx, KCTX_JPL_ENABLED)
1085 || (kctx->jit_current_allocations == 0)) {
1086 ignore_pressure_limit = true;
1087 }
1088 #else
1089 ignore_pressure_limit = true;
1090 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1091
1092 for (i = 0, info = katom->softjob_data; i < count; i++, info++) {
1093 if (kctx->jit_alloc[info->id]) {
1094 /* The JIT ID is duplicated in this atom. Roll back
1095 * previous allocations and fail.
1096 */
1097 u32 j;
1098
1099 info = katom->softjob_data;
1100 for (j = 0; j < i; j++, info++) {
1101 kbase_jit_free(kctx, kctx->jit_alloc[info->id]);
1102 kctx->jit_alloc[info->id] =
1103 KBASE_RESERVED_REG_JIT_ALLOC;
1104 }
1105
1106 katom->event_code = BASE_JD_EVENT_MEM_GROWTH_FAILED;
1107 return 0;
1108 }
1109
1110 /* Create a JIT allocation */
1111 reg = kbase_jit_allocate(kctx, info, ignore_pressure_limit);
1112 if (!reg) {
1113 struct kbase_jd_atom *jit_atom;
1114 bool can_block = false;
1115
1116 lockdep_assert_held(&kctx->jctx.lock);
1117
1118 list_for_each_entry(jit_atom, &kctx->jctx.jit_atoms_head, jit_node) {
1119 if (jit_atom == katom)
1120 break;
1121
1122 if ((jit_atom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) ==
1123 BASE_JD_REQ_SOFT_JIT_FREE) {
1124 u8 *free_ids = kbase_jit_free_get_ids(jit_atom);
1125
1126 if (free_ids && *free_ids &&
1127 kctx->jit_alloc[*free_ids]) {
1128 /* A JIT free which is active and
1129 * submitted before this atom
1130 */
1131 can_block = true;
1132 break;
1133 }
1134 }
1135 }
1136
1137 if (!can_block) {
1138 /* Mark the failed allocation as well as the
1139 * other un-attempted allocations in the set,
1140 * so we know they are in use even if the
1141 * allocation itself failed.
1142 */
1143 for (; i < count; i++, info++) {
1144 kctx->jit_alloc[info->id] =
1145 KBASE_RESERVED_REG_JIT_ALLOC;
1146 }
1147
1148 katom->event_code = BASE_JD_EVENT_MEM_GROWTH_FAILED;
1149 dev_warn_ratelimited(kbdev->dev, "JIT alloc softjob failed: atom id %d\n",
1150 kbase_jd_atom_id(kctx, katom));
1151 return 0;
1152 }
1153
1154 /* There are pending frees for an active allocation
1155 * so we should wait to see whether they free the
1156 * memory. Add to the list of atoms for which JIT
1157 * allocation is pending.
1158 */
1159 kbase_jit_add_to_pending_alloc_list(katom);
1160 katom->jit_blocked = true;
1161
1162 /* Rollback, the whole set will be re-attempted */
1163 while (i-- > 0) {
1164 info--;
1165 kbase_jit_free(kctx, kctx->jit_alloc[info->id]);
1166 kctx->jit_alloc[info->id] = NULL;
1167 }
1168
1169 return 1;
1170 }
1171
1172 /* Bind it to the user provided ID. */
1173 kctx->jit_alloc[info->id] = reg;
1174 }
1175
1176 for (i = 0, info = katom->softjob_data; i < count; i++, info++) {
1177 u64 entry_mmu_flags = 0;
1178 /*
1179 * Write the address of the JIT allocation to the user provided
1180 * GPU allocation.
1181 */
1182 ptr = kbase_vmap_prot(kctx, info->gpu_alloc_addr, sizeof(*ptr),
1183 KBASE_REG_CPU_WR, &mapping);
1184 if (!ptr) {
1185 /*
1186 * Leave the allocations "live" as the JIT free atom
1187 * will be submitted anyway.
1188 */
1189 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1190 return 0;
1191 }
1192
1193 reg = kctx->jit_alloc[info->id];
1194 new_addr = reg->start_pfn << PAGE_SHIFT;
1195 *ptr = new_addr;
1196
1197 #if defined(CONFIG_MALI_VECTOR_DUMP)
1198 /*
1199 * Retrieve the mmu flags for JIT allocation
1200 * only if dumping is enabled
1201 */
1202 entry_mmu_flags = kbase_mmu_create_ate(kbdev,
1203 (struct tagged_addr){ 0 }, reg->flags,
1204 MIDGARD_MMU_BOTTOMLEVEL, kctx->jit_group_id);
1205 #endif
1206
1207 KBASE_TLSTREAM_TL_ATTRIB_ATOM_JIT(
1208 kbdev, katom, info->gpu_alloc_addr, new_addr,
1209 info->flags, entry_mmu_flags, info->id,
1210 info->commit_pages, info->extension, info->va_pages);
1211 kbase_vunmap(kctx, &mapping);
1212
1213 kbase_trace_jit_report_gpu_mem(kctx, reg,
1214 KBASE_JIT_REPORT_ON_ALLOC_OR_FREE);
1215 }
1216
1217 katom->event_code = BASE_JD_EVENT_DONE;
1218
1219 return 0;
1220 }
1221
kbase_jit_allocate_finish(struct kbase_jd_atom * katom)1222 static void kbase_jit_allocate_finish(struct kbase_jd_atom *katom)
1223 {
1224 struct base_jit_alloc_info *info;
1225
1226 lockdep_assert_held(&katom->kctx->jctx.lock);
1227
1228 if (WARN_ON(!katom->softjob_data))
1229 return;
1230
1231 /* Remove atom from jit_atoms_head list */
1232 list_del(&katom->jit_node);
1233
1234 if (katom->jit_blocked) {
1235 list_del(&katom->queue);
1236 katom->jit_blocked = false;
1237 }
1238
1239 info = katom->softjob_data;
1240 /* Free the info structure */
1241 kfree(info);
1242 }
1243
kbase_jit_free_prepare(struct kbase_jd_atom * katom)1244 static int kbase_jit_free_prepare(struct kbase_jd_atom *katom)
1245 {
1246 struct kbase_context *kctx = katom->kctx;
1247 struct kbase_device *kbdev = kctx->kbdev;
1248 __user void *data = (__user void *)(uintptr_t) katom->jc;
1249 u8 *ids;
1250 u32 count = MAX(katom->nr_extres, 1);
1251 u32 i;
1252 int ret;
1253
1254 /* Sanity checks */
1255 if (count > ARRAY_SIZE(kctx->jit_alloc)) {
1256 ret = -EINVAL;
1257 goto fail;
1258 }
1259
1260 /* Copy the information for safe access and future storage */
1261 ids = kmalloc_array(count, sizeof(*ids), GFP_KERNEL);
1262 if (!ids) {
1263 ret = -ENOMEM;
1264 goto fail;
1265 }
1266
1267 lockdep_assert_held(&kctx->jctx.lock);
1268 katom->softjob_data = ids;
1269
1270 /* For backwards compatibility */
1271 if (katom->nr_extres) {
1272 /* Fail the job if there is no list of ids */
1273 if (!data) {
1274 ret = -EINVAL;
1275 goto free_info;
1276 }
1277
1278 if (copy_from_user(ids, data, sizeof(*ids)*count) != 0) {
1279 ret = -EINVAL;
1280 goto free_info;
1281 }
1282 } else {
1283 katom->nr_extres = 1;
1284 *ids = (u8)katom->jc;
1285 }
1286 for (i = 0; i < count; i++)
1287 KBASE_TLSTREAM_TL_ATTRIB_ATOM_JITFREEINFO(kbdev, katom, ids[i]);
1288
1289 list_add_tail(&katom->jit_node, &kctx->jctx.jit_atoms_head);
1290
1291 return 0;
1292
1293 free_info:
1294 kfree(katom->softjob_data);
1295 katom->softjob_data = NULL;
1296 fail:
1297 return ret;
1298 }
1299
kbase_jit_free_process(struct kbase_jd_atom * katom)1300 static void kbase_jit_free_process(struct kbase_jd_atom *katom)
1301 {
1302 struct kbase_context *kctx = katom->kctx;
1303 u8 *ids = kbase_jit_free_get_ids(katom);
1304 u32 count = katom->nr_extres;
1305 u32 i;
1306
1307 if (ids == NULL) {
1308 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1309 return;
1310 }
1311
1312 for (i = 0; i < count; i++, ids++) {
1313 /*
1314 * If the ID is zero or it is not in use yet then fail the job.
1315 */
1316 if ((*ids == 0) || (kctx->jit_alloc[*ids] == NULL)) {
1317 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1318 return;
1319 }
1320 }
1321 }
1322
kbasep_jit_finish_worker(struct work_struct * work)1323 static void kbasep_jit_finish_worker(struct work_struct *work)
1324 {
1325 struct kbase_jd_atom *katom = container_of(work, struct kbase_jd_atom,
1326 work);
1327 struct kbase_context *kctx = katom->kctx;
1328 int resched;
1329
1330 mutex_lock(&kctx->jctx.lock);
1331 kbase_finish_soft_job(katom);
1332 resched = kbase_jd_done_nolock(katom, true);
1333 mutex_unlock(&kctx->jctx.lock);
1334
1335 if (resched)
1336 kbase_js_sched_all(kctx->kbdev);
1337 }
1338
kbase_jit_retry_pending_alloc(struct kbase_context * kctx)1339 void kbase_jit_retry_pending_alloc(struct kbase_context *kctx)
1340 {
1341 LIST_HEAD(jit_pending_alloc_list);
1342 struct list_head *i, *tmp;
1343
1344 list_splice_tail_init(&kctx->jctx.jit_pending_alloc,
1345 &jit_pending_alloc_list);
1346
1347 list_for_each_safe(i, tmp, &jit_pending_alloc_list) {
1348 struct kbase_jd_atom *pending_atom = list_entry(i,
1349 struct kbase_jd_atom, queue);
1350 KBASE_TLSTREAM_TL_EVENT_ATOM_SOFTJOB_START(kctx->kbdev, pending_atom);
1351 kbase_kinstr_jm_atom_sw_start(pending_atom);
1352 if (kbase_jit_allocate_process(pending_atom) == 0) {
1353 /* Atom has completed */
1354 INIT_WORK(&pending_atom->work,
1355 kbasep_jit_finish_worker);
1356 queue_work(kctx->jctx.job_done_wq, &pending_atom->work);
1357 }
1358 KBASE_TLSTREAM_TL_EVENT_ATOM_SOFTJOB_END(kctx->kbdev, pending_atom);
1359 kbase_kinstr_jm_atom_sw_stop(pending_atom);
1360 }
1361 }
1362
kbase_jit_free_finish(struct kbase_jd_atom * katom)1363 static void kbase_jit_free_finish(struct kbase_jd_atom *katom)
1364 {
1365 struct kbase_context *kctx = katom->kctx;
1366 u8 *ids;
1367 size_t j;
1368
1369 lockdep_assert_held(&kctx->jctx.lock);
1370
1371 ids = kbase_jit_free_get_ids(katom);
1372 if (WARN_ON(ids == NULL))
1373 return;
1374
1375 /* Remove this atom from the jit_atoms_head list */
1376 list_del(&katom->jit_node);
1377
1378 for (j = 0; j != katom->nr_extres; ++j) {
1379 if ((ids[j] != 0) && (kctx->jit_alloc[ids[j]] != NULL)) {
1380 /*
1381 * If the ID is valid but the allocation request failed
1382 * still succeed this soft job but don't try and free
1383 * the allocation.
1384 */
1385 if (kctx->jit_alloc[ids[j]] !=
1386 KBASE_RESERVED_REG_JIT_ALLOC) {
1387 KBASE_TLSTREAM_TL_JIT_USEDPAGES(kctx->kbdev,
1388 kctx->jit_alloc[ids[j]]->
1389 gpu_alloc->nents, ids[j]);
1390 kbase_jit_free(kctx, kctx->jit_alloc[ids[j]]);
1391 }
1392 kctx->jit_alloc[ids[j]] = NULL;
1393 }
1394 }
1395 /* Free the list of ids */
1396 kfree(ids);
1397
1398 kbase_jit_retry_pending_alloc(kctx);
1399 }
1400
kbase_ext_res_prepare(struct kbase_jd_atom * katom)1401 static int kbase_ext_res_prepare(struct kbase_jd_atom *katom)
1402 {
1403 __user struct base_external_resource_list *user_ext_res;
1404 struct base_external_resource_list *ext_res;
1405 u64 count = 0;
1406 size_t copy_size;
1407
1408 user_ext_res = (__user struct base_external_resource_list *)
1409 (uintptr_t) katom->jc;
1410
1411 /* Fail the job if there is no info structure */
1412 if (!user_ext_res)
1413 return -EINVAL;
1414
1415 if (copy_from_user(&count, &user_ext_res->count, sizeof(u64)) != 0)
1416 return -EINVAL;
1417
1418 /* Is the number of external resources in range? */
1419 if (!count || count > BASE_EXT_RES_COUNT_MAX)
1420 return -EINVAL;
1421
1422 /* Copy the information for safe access and future storage */
1423 copy_size = sizeof(*ext_res);
1424 copy_size += sizeof(struct base_external_resource) * (count - 1);
1425 ext_res = memdup_user(user_ext_res, copy_size);
1426 if (IS_ERR(ext_res))
1427 return PTR_ERR(ext_res);
1428
1429 /*
1430 * Overwrite the count with the first value incase it was changed
1431 * after the fact.
1432 */
1433 ext_res->count = count;
1434
1435 katom->softjob_data = ext_res;
1436
1437 return 0;
1438 }
1439
kbase_ext_res_process(struct kbase_jd_atom * katom,bool map)1440 static void kbase_ext_res_process(struct kbase_jd_atom *katom, bool map)
1441 {
1442 struct base_external_resource_list *ext_res;
1443 int i;
1444 bool failed = false;
1445
1446 ext_res = katom->softjob_data;
1447 if (!ext_res)
1448 goto failed_jc;
1449
1450 kbase_gpu_vm_lock(katom->kctx);
1451
1452 for (i = 0; i < ext_res->count; i++) {
1453 u64 gpu_addr;
1454
1455 gpu_addr = ext_res->ext_res[i].ext_resource &
1456 ~BASE_EXT_RES_ACCESS_EXCLUSIVE;
1457 if (map) {
1458 if (!kbase_sticky_resource_acquire(katom->kctx,
1459 gpu_addr))
1460 goto failed_loop;
1461 } else {
1462 if (!kbase_sticky_resource_release_force(katom->kctx, NULL,
1463 gpu_addr))
1464 failed = true;
1465 }
1466 }
1467
1468 /*
1469 * In the case of unmap we continue unmapping other resources in the
1470 * case of failure but will always report failure if _any_ unmap
1471 * request fails.
1472 */
1473 if (failed)
1474 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1475 else
1476 katom->event_code = BASE_JD_EVENT_DONE;
1477
1478 kbase_gpu_vm_unlock(katom->kctx);
1479
1480 return;
1481
1482 failed_loop:
1483 while (i > 0) {
1484 u64 const gpu_addr = ext_res->ext_res[i - 1].ext_resource &
1485 ~BASE_EXT_RES_ACCESS_EXCLUSIVE;
1486
1487 kbase_sticky_resource_release_force(katom->kctx, NULL, gpu_addr);
1488
1489 --i;
1490 }
1491
1492 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1493 kbase_gpu_vm_unlock(katom->kctx);
1494
1495 failed_jc:
1496 return;
1497 }
1498
kbase_ext_res_finish(struct kbase_jd_atom * katom)1499 static void kbase_ext_res_finish(struct kbase_jd_atom *katom)
1500 {
1501 struct base_external_resource_list *ext_res;
1502
1503 ext_res = katom->softjob_data;
1504 /* Free the info structure */
1505 kfree(ext_res);
1506 }
1507
kbase_process_soft_job(struct kbase_jd_atom * katom)1508 int kbase_process_soft_job(struct kbase_jd_atom *katom)
1509 {
1510 int ret = 0;
1511 struct kbase_context *kctx = katom->kctx;
1512 struct kbase_device *kbdev = kctx->kbdev;
1513
1514 KBASE_TLSTREAM_TL_EVENT_ATOM_SOFTJOB_START(kbdev, katom);
1515 kbase_kinstr_jm_atom_sw_start(katom);
1516
1517 trace_sysgraph(SGR_SUBMIT, kctx->id,
1518 kbase_jd_atom_id(kctx, katom));
1519
1520 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
1521 case BASE_JD_REQ_SOFT_DUMP_CPU_GPU_TIME:
1522 ret = kbase_dump_cpu_gpu_time(katom);
1523 break;
1524
1525 #if IS_ENABLED(CONFIG_SYNC_FILE)
1526 case BASE_JD_REQ_SOFT_FENCE_TRIGGER:
1527 katom->event_code = kbase_sync_fence_out_trigger(katom,
1528 katom->event_code == BASE_JD_EVENT_DONE ?
1529 0 : -EFAULT);
1530 break;
1531 case BASE_JD_REQ_SOFT_FENCE_WAIT:
1532 {
1533 ret = kbase_sync_fence_in_wait(katom);
1534
1535 if (ret == 1) {
1536 #ifdef CONFIG_MALI_BIFROST_FENCE_DEBUG
1537 kbasep_add_waiting_with_timeout(katom);
1538 #else
1539 kbasep_add_waiting_soft_job(katom);
1540 #endif
1541 }
1542 break;
1543 }
1544 #endif
1545 case BASE_JD_REQ_SOFT_EVENT_WAIT:
1546 ret = kbasep_soft_event_wait(katom);
1547 break;
1548 case BASE_JD_REQ_SOFT_EVENT_SET:
1549 kbasep_soft_event_update_locked(katom, BASE_JD_SOFT_EVENT_SET);
1550 break;
1551 case BASE_JD_REQ_SOFT_EVENT_RESET:
1552 kbasep_soft_event_update_locked(katom, BASE_JD_SOFT_EVENT_RESET);
1553 break;
1554 #if IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST
1555 case BASE_JD_REQ_SOFT_DEBUG_COPY:
1556 {
1557 int res = kbase_debug_copy(katom);
1558
1559 if (res)
1560 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1561 break;
1562 }
1563 #endif /* IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST */
1564 case BASE_JD_REQ_SOFT_JIT_ALLOC:
1565 ret = kbase_jit_allocate_process(katom);
1566 break;
1567 case BASE_JD_REQ_SOFT_JIT_FREE:
1568 kbase_jit_free_process(katom);
1569 break;
1570 case BASE_JD_REQ_SOFT_EXT_RES_MAP:
1571 kbase_ext_res_process(katom, true);
1572 break;
1573 case BASE_JD_REQ_SOFT_EXT_RES_UNMAP:
1574 kbase_ext_res_process(katom, false);
1575 break;
1576 }
1577
1578 /* Atom is complete */
1579 KBASE_TLSTREAM_TL_EVENT_ATOM_SOFTJOB_END(kbdev, katom);
1580 kbase_kinstr_jm_atom_sw_stop(katom);
1581 return ret;
1582 }
1583
kbase_cancel_soft_job(struct kbase_jd_atom * katom)1584 void kbase_cancel_soft_job(struct kbase_jd_atom *katom)
1585 {
1586 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
1587 #if IS_ENABLED(CONFIG_SYNC_FILE)
1588 case BASE_JD_REQ_SOFT_FENCE_WAIT:
1589 kbase_sync_fence_in_cancel_wait(katom);
1590 break;
1591 #endif
1592 case BASE_JD_REQ_SOFT_EVENT_WAIT:
1593 kbasep_soft_event_cancel_job(katom);
1594 break;
1595 default:
1596 /* This soft-job doesn't support cancellation! */
1597 KBASE_DEBUG_ASSERT(0);
1598 }
1599 }
1600
kbase_prepare_soft_job(struct kbase_jd_atom * katom)1601 int kbase_prepare_soft_job(struct kbase_jd_atom *katom)
1602 {
1603 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
1604 case BASE_JD_REQ_SOFT_DUMP_CPU_GPU_TIME:
1605 {
1606 if (!IS_ALIGNED(katom->jc, cache_line_size()))
1607 return -EINVAL;
1608 }
1609 break;
1610 #if IS_ENABLED(CONFIG_SYNC_FILE)
1611 case BASE_JD_REQ_SOFT_FENCE_TRIGGER:
1612 {
1613 struct base_fence fence;
1614 int fd;
1615
1616 if (copy_from_user(&fence,
1617 (__user void *)(uintptr_t)katom->jc,
1618 sizeof(fence)) != 0)
1619 return -EINVAL;
1620
1621 fd = kbase_sync_fence_out_create(katom,
1622 fence.basep.stream_fd);
1623 if (fd < 0)
1624 return -EINVAL;
1625
1626 fence.basep.fd = fd;
1627 if (copy_to_user((__user void *)(uintptr_t)katom->jc,
1628 &fence, sizeof(fence)) != 0) {
1629 kbase_sync_fence_out_remove(katom);
1630 /* fd should have been closed here, but there's
1631 * no good way of doing that. Since
1632 * copy_to_user() very rarely fails, and the fd
1633 * will get closed on process termination this
1634 * won't be a problem.
1635 */
1636 fence.basep.fd = -EINVAL;
1637 return -EINVAL;
1638 }
1639 }
1640 break;
1641 case BASE_JD_REQ_SOFT_FENCE_WAIT:
1642 {
1643 struct base_fence fence;
1644 int ret;
1645
1646 if (copy_from_user(&fence,
1647 (__user void *)(uintptr_t)katom->jc,
1648 sizeof(fence)) != 0)
1649 return -EINVAL;
1650
1651 /* Get a reference to the fence object */
1652 ret = kbase_sync_fence_in_from_fd(katom,
1653 fence.basep.fd);
1654 if (ret < 0)
1655 return ret;
1656 }
1657 break;
1658 #endif /* CONFIG_SYNC_FILE */
1659 case BASE_JD_REQ_SOFT_JIT_ALLOC:
1660 return kbase_jit_allocate_prepare(katom);
1661 case BASE_JD_REQ_SOFT_JIT_FREE:
1662 return kbase_jit_free_prepare(katom);
1663 case BASE_JD_REQ_SOFT_EVENT_WAIT:
1664 case BASE_JD_REQ_SOFT_EVENT_SET:
1665 case BASE_JD_REQ_SOFT_EVENT_RESET:
1666 if (katom->jc == 0)
1667 return -EINVAL;
1668 break;
1669 #if IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST
1670 case BASE_JD_REQ_SOFT_DEBUG_COPY:
1671 return kbase_debug_copy_prepare(katom);
1672 #endif /* IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST */
1673 case BASE_JD_REQ_SOFT_EXT_RES_MAP:
1674 return kbase_ext_res_prepare(katom);
1675 case BASE_JD_REQ_SOFT_EXT_RES_UNMAP:
1676 return kbase_ext_res_prepare(katom);
1677 default:
1678 /* Unsupported soft-job */
1679 return -EINVAL;
1680 }
1681 return 0;
1682 }
1683
kbase_finish_soft_job(struct kbase_jd_atom * katom)1684 void kbase_finish_soft_job(struct kbase_jd_atom *katom)
1685 {
1686 trace_sysgraph(SGR_COMPLETE, katom->kctx->id,
1687 kbase_jd_atom_id(katom->kctx, katom));
1688
1689 switch (katom->core_req & BASE_JD_REQ_SOFT_JOB_TYPE) {
1690 case BASE_JD_REQ_SOFT_DUMP_CPU_GPU_TIME:
1691 /* Nothing to do */
1692 break;
1693 #if IS_ENABLED(CONFIG_SYNC_FILE)
1694 case BASE_JD_REQ_SOFT_FENCE_TRIGGER:
1695 /* If fence has not yet been signaled, do it now */
1696 kbase_sync_fence_out_trigger(katom, katom->event_code ==
1697 BASE_JD_EVENT_DONE ? 0 : -EFAULT);
1698 break;
1699 case BASE_JD_REQ_SOFT_FENCE_WAIT:
1700 /* Release katom's reference to fence object */
1701 kbase_sync_fence_in_remove(katom);
1702 break;
1703 #endif /* CONFIG_SYNC_FILE */
1704 #if IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST
1705 case BASE_JD_REQ_SOFT_DEBUG_COPY:
1706 kbase_debug_copy_finish(katom);
1707 break;
1708 #endif /* IS_ENABLED(CONFIG_MALI_VECTOR_DUMP) || MALI_UNIT_TEST */
1709 case BASE_JD_REQ_SOFT_JIT_ALLOC:
1710 kbase_jit_allocate_finish(katom);
1711 break;
1712 case BASE_JD_REQ_SOFT_EXT_RES_MAP:
1713 kbase_ext_res_finish(katom);
1714 break;
1715 case BASE_JD_REQ_SOFT_EXT_RES_UNMAP:
1716 kbase_ext_res_finish(katom);
1717 break;
1718 case BASE_JD_REQ_SOFT_JIT_FREE:
1719 kbase_jit_free_finish(katom);
1720 break;
1721 }
1722 }
1723
kbase_resume_suspended_soft_jobs(struct kbase_device * kbdev)1724 void kbase_resume_suspended_soft_jobs(struct kbase_device *kbdev)
1725 {
1726 LIST_HEAD(local_suspended_soft_jobs);
1727 struct kbase_jd_atom *tmp_iter;
1728 struct kbase_jd_atom *katom_iter;
1729 struct kbasep_js_device_data *js_devdata;
1730 bool resched = false;
1731
1732 KBASE_DEBUG_ASSERT(kbdev);
1733
1734 js_devdata = &kbdev->js_data;
1735
1736 /* Move out the entire list */
1737 mutex_lock(&js_devdata->runpool_mutex);
1738 list_splice_init(&js_devdata->suspended_soft_jobs_list,
1739 &local_suspended_soft_jobs);
1740 mutex_unlock(&js_devdata->runpool_mutex);
1741
1742 /*
1743 * Each atom must be detached from the list and ran separately -
1744 * it could be re-added to the old list, but this is unlikely
1745 */
1746 list_for_each_entry_safe(katom_iter, tmp_iter,
1747 &local_suspended_soft_jobs, dep_item[1]) {
1748 struct kbase_context *kctx = katom_iter->kctx;
1749
1750 mutex_lock(&kctx->jctx.lock);
1751
1752 /* Remove from the global list */
1753 list_del(&katom_iter->dep_item[1]);
1754 /* Remove from the context's list of waiting soft jobs */
1755 kbasep_remove_waiting_soft_job(katom_iter);
1756
1757 if (kbase_process_soft_job(katom_iter) == 0) {
1758 kbase_finish_soft_job(katom_iter);
1759 resched |= kbase_jd_done_nolock(katom_iter, true);
1760 #ifdef CONFIG_MALI_ARBITER_SUPPORT
1761 atomic_dec(&kbdev->pm.gpu_users_waiting);
1762 #endif /* CONFIG_MALI_ARBITER_SUPPORT */
1763 }
1764 mutex_unlock(&kctx->jctx.lock);
1765 }
1766
1767 if (resched)
1768 kbase_js_sched_all(kbdev);
1769 }
1770 #endif /* !MALI_USE_CSF */
1771