• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2018-2021 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 #include <tl/mali_kbase_tracepoints.h>
24 #include <mali_kbase_ctx_sched.h>
25 #include "device/mali_kbase_device.h"
26 #include "mali_kbase_csf.h"
27 #include <linux/export.h>
28 
29 #if IS_ENABLED(CONFIG_SYNC_FILE)
30 #include "mali_kbase_fence.h"
31 #include "mali_kbase_sync.h"
32 
33 static DEFINE_SPINLOCK(kbase_csf_fence_lock);
34 #endif
35 
36 static void kcpu_queue_process(struct kbase_kcpu_command_queue *kcpu_queue,
37 			       bool drain_queue);
38 
39 static void kcpu_queue_process_worker(struct work_struct *data);
40 
kbase_kcpu_map_import_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_import_info * import_info,struct kbase_kcpu_command * current_command)41 static int kbase_kcpu_map_import_prepare(
42 		struct kbase_kcpu_command_queue *kcpu_queue,
43 		struct base_kcpu_command_import_info *import_info,
44 		struct kbase_kcpu_command *current_command)
45 {
46 	struct kbase_context *const kctx = kcpu_queue->kctx;
47 	struct kbase_va_region *reg;
48 	int ret = 0;
49 
50 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
51 
52 	/* Take the processes mmap lock */
53 	down_read(kbase_mem_get_process_mmap_lock());
54 	kbase_gpu_vm_lock(kctx);
55 
56 	reg = kbase_region_tracker_find_region_enclosing_address(kctx,
57 					import_info->handle);
58 
59 	if (kbase_is_region_invalid_or_free(reg) ||
60 	    !kbase_mem_is_imported(reg->gpu_alloc->type)) {
61 		ret = -EINVAL;
62 		goto out;
63 	}
64 
65 	if (reg->gpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
66 		/* Pin the physical pages backing the user buffer while
67 		 * we are in the process context and holding the mmap lock.
68 		 * The dma mapping & GPU mapping of the pages would be done
69 		 * when the MAP_IMPORT operation is executed.
70 		 *
71 		 * Though the pages would be pinned, no reference is taken
72 		 * on the physical pages tracking object. When the last
73 		 * reference to the tracking object is dropped the pages
74 		 * would be unpinned if they weren't unpinned before.
75 		 */
76 		ret = kbase_jd_user_buf_pin_pages(kctx, reg);
77 		if (ret)
78 			goto out;
79 	}
80 
81 	current_command->type = BASE_KCPU_COMMAND_TYPE_MAP_IMPORT;
82 	current_command->info.import.gpu_va = import_info->handle;
83 
84 out:
85 	kbase_gpu_vm_unlock(kctx);
86 	/* Release the processes mmap lock */
87 	up_read(kbase_mem_get_process_mmap_lock());
88 
89 	return ret;
90 }
91 
kbase_kcpu_unmap_import_prepare_internal(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_import_info * import_info,struct kbase_kcpu_command * current_command,enum base_kcpu_command_type type)92 static int kbase_kcpu_unmap_import_prepare_internal(
93 		struct kbase_kcpu_command_queue *kcpu_queue,
94 		struct base_kcpu_command_import_info *import_info,
95 		struct kbase_kcpu_command *current_command,
96 		enum base_kcpu_command_type type)
97 {
98 	struct kbase_context *const kctx = kcpu_queue->kctx;
99 	struct kbase_va_region *reg;
100 	int ret = 0;
101 
102 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
103 
104 	kbase_gpu_vm_lock(kctx);
105 
106 	reg = kbase_region_tracker_find_region_enclosing_address(kctx,
107 					import_info->handle);
108 
109 	if (kbase_is_region_invalid_or_free(reg) ||
110 	    !kbase_mem_is_imported(reg->gpu_alloc->type)) {
111 		ret = -EINVAL;
112 		goto out;
113 	}
114 
115 	if (reg->gpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
116 		/* The pages should have been pinned when MAP_IMPORT
117 		 * was enqueued previously.
118 		 */
119 		if (reg->gpu_alloc->nents !=
120 		    reg->gpu_alloc->imported.user_buf.nr_pages) {
121 			ret = -EINVAL;
122 			goto out;
123 		}
124 	}
125 
126 	current_command->type = type;
127 	current_command->info.import.gpu_va = import_info->handle;
128 
129 out:
130 	kbase_gpu_vm_unlock(kctx);
131 
132 	return ret;
133 }
134 
kbase_kcpu_unmap_import_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_import_info * import_info,struct kbase_kcpu_command * current_command)135 static int kbase_kcpu_unmap_import_prepare(
136 		struct kbase_kcpu_command_queue *kcpu_queue,
137 		struct base_kcpu_command_import_info *import_info,
138 		struct kbase_kcpu_command *current_command)
139 {
140 	return kbase_kcpu_unmap_import_prepare_internal(kcpu_queue,
141 			import_info, current_command,
142 			BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT);
143 }
144 
kbase_kcpu_unmap_import_force_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_import_info * import_info,struct kbase_kcpu_command * current_command)145 static int kbase_kcpu_unmap_import_force_prepare(
146 		struct kbase_kcpu_command_queue *kcpu_queue,
147 		struct base_kcpu_command_import_info *import_info,
148 		struct kbase_kcpu_command *current_command)
149 {
150 	return kbase_kcpu_unmap_import_prepare_internal(kcpu_queue,
151 			import_info, current_command,
152 			BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT_FORCE);
153 }
154 
155 /**
156  * kbase_jit_add_to_pending_alloc_list() - Pend JIT allocation
157  *
158  * @queue: The queue containing this JIT allocation
159  * @cmd:   The JIT allocation that is blocking this queue
160  */
kbase_jit_add_to_pending_alloc_list(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command * cmd)161 static void kbase_jit_add_to_pending_alloc_list(
162 		struct kbase_kcpu_command_queue *queue,
163 		struct kbase_kcpu_command *cmd)
164 {
165 	struct kbase_context *const kctx = queue->kctx;
166 	struct list_head *target_list_head =
167 			&kctx->csf.kcpu_queues.jit_blocked_queues;
168 	struct kbase_kcpu_command_queue *blocked_queue;
169 
170 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
171 
172 	list_for_each_entry(blocked_queue,
173 			&kctx->csf.kcpu_queues.jit_blocked_queues,
174 			jit_blocked) {
175 		struct kbase_kcpu_command const*const jit_alloc_cmd =
176 				&blocked_queue->commands[blocked_queue->start_offset];
177 
178 		WARN_ON(jit_alloc_cmd->type != BASE_KCPU_COMMAND_TYPE_JIT_ALLOC);
179 		if (cmd->enqueue_ts < jit_alloc_cmd->enqueue_ts) {
180 			target_list_head = &blocked_queue->jit_blocked;
181 			break;
182 		}
183 	}
184 
185 	list_add_tail(&queue->jit_blocked, target_list_head);
186 }
187 
188 /**
189  * kbase_kcpu_jit_allocate_process() - Process JIT allocation
190  *
191  * @queue: The queue containing this JIT allocation
192  * @cmd:   The JIT allocation command
193  *
194  * Return:
195  * * 0       - allocation OK
196  * * -EINVAL - missing info or JIT ID still in use
197  * * -EAGAIN - Retry
198  * * -ENOMEM - no memory. unable to allocate
199  */
kbase_kcpu_jit_allocate_process(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command * cmd)200 static int kbase_kcpu_jit_allocate_process(
201 		struct kbase_kcpu_command_queue *queue,
202 		struct kbase_kcpu_command *cmd)
203 {
204 	struct kbase_context *const kctx = queue->kctx;
205 	struct kbase_kcpu_command_jit_alloc_info *alloc_info =
206 			&cmd->info.jit_alloc;
207 	struct base_jit_alloc_info *info = alloc_info->info;
208 	struct kbase_vmap_struct mapping;
209 	struct kbase_va_region *reg;
210 	u32 count = alloc_info->count;
211 	u64 *ptr, new_addr;
212 	u32 i;
213 	int ret;
214 
215 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
216 
217 	if (alloc_info->blocked) {
218 		list_del(&queue->jit_blocked);
219 		alloc_info->blocked = false;
220 	}
221 
222 	if (WARN_ON(!info))
223 		return -EINVAL;
224 
225 	/* Check if all JIT IDs are not in use */
226 	for (i = 0; i < count; i++, info++) {
227 		/* The JIT ID is still in use so fail the allocation */
228 		if (kctx->jit_alloc[info->id]) {
229 			dev_dbg(kctx->kbdev->dev, "JIT ID still in use");
230 			return -EINVAL;
231 		}
232 	}
233 
234 	/* Now start the allocation loop */
235 	for (i = 0, info = alloc_info->info; i < count; i++, info++) {
236 		/* Create a JIT allocation */
237 		reg = kbase_jit_allocate(kctx, info, true);
238 		if (!reg) {
239 			bool can_block = false;
240 			struct kbase_kcpu_command const *jit_cmd;
241 
242 			list_for_each_entry(jit_cmd, &kctx->csf.kcpu_queues.jit_cmds_head, info.jit_alloc.node) {
243 				if (jit_cmd == cmd)
244 					break;
245 
246 				if (jit_cmd->type == BASE_KCPU_COMMAND_TYPE_JIT_FREE) {
247 					u8 const*const free_ids = jit_cmd->info.jit_free.ids;
248 
249 					if (free_ids && *free_ids && kctx->jit_alloc[*free_ids]) {
250 						/*
251 						 * A JIT free which is active
252 						 * and submitted before this
253 						 * command.
254 						 */
255 						can_block = true;
256 						break;
257 					}
258 				}
259 			}
260 
261 			if (!can_block) {
262 				/*
263 				 * No prior JIT_FREE command is active. Roll
264 				 * back previous allocations and fail.
265 				 */
266 				dev_warn_ratelimited(kctx->kbdev->dev, "JIT alloc command failed: %pK\n", cmd);
267 				ret = -ENOMEM;
268 				goto fail;
269 			}
270 
271 			/* There are pending frees for an active allocation
272 			 * so we should wait to see whether they free the
273 			 * memory. Add to the list of atoms for which JIT
274 			 * allocation is pending.
275 			 */
276 			kbase_jit_add_to_pending_alloc_list(queue, cmd);
277 			alloc_info->blocked = true;
278 
279 			/* Rollback, the whole set will be re-attempted */
280 			while (i-- > 0) {
281 				info--;
282 				kbase_jit_free(kctx, kctx->jit_alloc[info->id]);
283 				kctx->jit_alloc[info->id] = NULL;
284 			}
285 
286 			return -EAGAIN;
287 		}
288 
289 		/* Bind it to the user provided ID. */
290 		kctx->jit_alloc[info->id] = reg;
291 	}
292 
293 	for (i = 0, info = alloc_info->info; i < count; i++, info++) {
294 		/*
295 		 * Write the address of the JIT allocation to the user provided
296 		 * GPU allocation.
297 		 */
298 		ptr = kbase_vmap_prot(kctx, info->gpu_alloc_addr, sizeof(*ptr),
299 				KBASE_REG_CPU_WR, &mapping);
300 		if (!ptr) {
301 			ret = -ENOMEM;
302 			goto fail;
303 		}
304 
305 		reg = kctx->jit_alloc[info->id];
306 		new_addr = reg->start_pfn << PAGE_SHIFT;
307 		*ptr = new_addr;
308 		kbase_vunmap(kctx, &mapping);
309 	}
310 
311 	return 0;
312 
313 fail:
314 	/* Roll back completely */
315 	for (i = 0, info = alloc_info->info; i < count; i++, info++) {
316 		/* Free the allocations that were successful.
317 		 * Mark all the allocations including the failed one and the
318 		 * other un-attempted allocations in the set, so we know they
319 		 * are in use.
320 		 */
321 		if (kctx->jit_alloc[info->id])
322 			kbase_jit_free(kctx, kctx->jit_alloc[info->id]);
323 
324 		kctx->jit_alloc[info->id] = KBASE_RESERVED_REG_JIT_ALLOC;
325 	}
326 
327 	return ret;
328 }
329 
kbase_kcpu_jit_allocate_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_jit_alloc_info * alloc_info,struct kbase_kcpu_command * current_command)330 static int kbase_kcpu_jit_allocate_prepare(
331 		struct kbase_kcpu_command_queue *kcpu_queue,
332 		struct base_kcpu_command_jit_alloc_info *alloc_info,
333 		struct kbase_kcpu_command *current_command)
334 {
335 	struct kbase_context *const kctx = kcpu_queue->kctx;
336 	void __user *data = u64_to_user_ptr(alloc_info->info);
337 	struct base_jit_alloc_info *info;
338 	u32 count = alloc_info->count;
339 	int ret = 0;
340 	u32 i;
341 
342 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
343 
344 	if (!data || count > kcpu_queue->kctx->jit_max_allocations ||
345 			count > ARRAY_SIZE(kctx->jit_alloc)) {
346 		ret = -EINVAL;
347 		goto out;
348 	}
349 
350 	info = kmalloc_array(count, sizeof(*info), GFP_KERNEL);
351 	if (!info) {
352 		ret = -ENOMEM;
353 		goto out;
354 	}
355 
356 	if (copy_from_user(info, data, sizeof(*info) * count) != 0) {
357 		ret = -EINVAL;
358 		goto out_free;
359 	}
360 
361 	for (i = 0; i < count; i++) {
362 		ret = kbasep_jit_alloc_validate(kctx, &info[i]);
363 		if (ret)
364 			goto out_free;
365 	}
366 
367 	/* Search for duplicate JIT ids */
368 	for (i = 0; i < (count - 1); i++) {
369 		u32 j;
370 
371 		for (j = (i + 1); j < count; j++) {
372 			if (info[i].id == info[j].id) {
373 				ret = -EINVAL;
374 				goto out_free;
375 			}
376 		}
377 	}
378 
379 	current_command->type = BASE_KCPU_COMMAND_TYPE_JIT_ALLOC;
380 	list_add_tail(&current_command->info.jit_alloc.node,
381 			&kctx->csf.kcpu_queues.jit_cmds_head);
382 	current_command->info.jit_alloc.info = info;
383 	current_command->info.jit_alloc.count = count;
384 	current_command->info.jit_alloc.blocked = false;
385 
386 	return 0;
387 out_free:
388 	kfree(info);
389 out:
390 	return ret;
391 }
392 
393 /**
394  * kbase_kcpu_jit_allocate_finish() - Finish handling the JIT_ALLOC command
395  *
396  * @queue: The queue containing this JIT allocation
397  * @cmd:  The JIT allocation command
398  */
kbase_kcpu_jit_allocate_finish(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command * cmd)399 static void kbase_kcpu_jit_allocate_finish(
400 		struct kbase_kcpu_command_queue *queue,
401 		struct kbase_kcpu_command *cmd)
402 {
403 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
404 
405 	/* Remove this command from the jit_cmds_head list */
406 	list_del(&cmd->info.jit_alloc.node);
407 
408 	/*
409 	 * If we get to this point we must have already cleared the blocked
410 	 * flag, otherwise it'd be a bug.
411 	 */
412 	if (WARN_ON(cmd->info.jit_alloc.blocked)) {
413 		list_del(&queue->jit_blocked);
414 		cmd->info.jit_alloc.blocked = false;
415 	}
416 
417 	kfree(cmd->info.jit_alloc.info);
418 }
419 
420 /**
421  * kbase_kcpu_jit_retry_pending_allocs() - Retry blocked JIT_ALLOC commands
422  *
423  * @kctx: The context containing the blocked JIT_ALLOC commands
424  */
kbase_kcpu_jit_retry_pending_allocs(struct kbase_context * kctx)425 static void kbase_kcpu_jit_retry_pending_allocs(struct kbase_context *kctx)
426 {
427 	struct kbase_kcpu_command_queue *blocked_queue;
428 
429 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
430 
431 	/*
432 	 * Reschedule all queues blocked by JIT_ALLOC commands.
433 	 * NOTE: This code traverses the list of blocked queues directly. It
434 	 * only works as long as the queued works are not executed at the same
435 	 * time. This precondition is true since we're holding the
436 	 * kbase_csf_kcpu_queue_context.lock .
437 	 */
438 	list_for_each_entry(blocked_queue,
439 			&kctx->csf.kcpu_queues.jit_blocked_queues, jit_blocked)
440 		queue_work(kctx->csf.kcpu_queues.wq, &blocked_queue->work);
441 }
442 
kbase_kcpu_jit_free_process(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command * const cmd)443 static int kbase_kcpu_jit_free_process(struct kbase_kcpu_command_queue *queue,
444 				       struct kbase_kcpu_command *const cmd)
445 {
446 	struct kbase_kcpu_command_jit_free_info const *const free_info =
447 		&cmd->info.jit_free;
448 	u8 const *const ids = free_info->ids;
449 	u32 const count = free_info->count;
450 	u32 i;
451 	int rc = 0;
452 	struct kbase_context *kctx = queue->kctx;
453 
454 	if (WARN_ON(!ids))
455 		return -EINVAL;
456 
457 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
458 
459 	KBASE_TLSTREAM_TL_KBASE_ARRAY_BEGIN_KCPUQUEUE_EXECUTE_JIT_FREE_END(
460 		queue->kctx->kbdev, queue);
461 
462 	for (i = 0; i < count; i++) {
463 		u64 pages_used = 0;
464 		int item_err = 0;
465 
466 		if (!kctx->jit_alloc[ids[i]]) {
467 			dev_dbg(kctx->kbdev->dev, "invalid JIT free ID");
468 			rc = -EINVAL;
469 			item_err = rc;
470 		} else {
471 			struct kbase_va_region *const reg = kctx->jit_alloc[ids[i]];
472 
473 			/*
474 			 * If the ID is valid but the allocation request failed, still
475 			 * succeed this command but don't try and free the allocation.
476 			 */
477 			if (reg != KBASE_RESERVED_REG_JIT_ALLOC) {
478 				pages_used = reg->gpu_alloc->nents;
479 				kbase_jit_free(kctx, reg);
480 			}
481 
482 			kctx->jit_alloc[ids[i]] = NULL;
483 		}
484 
485 		KBASE_TLSTREAM_TL_KBASE_ARRAY_ITEM_KCPUQUEUE_EXECUTE_JIT_FREE_END(
486 			queue->kctx->kbdev, queue, item_err, pages_used);
487 	}
488 
489 	/* Free the list of ids */
490 	kfree(ids);
491 
492 	/*
493 	 * Remove this command from the jit_cmds_head list and retry pending
494 	 * allocations.
495 	 */
496 	list_del(&cmd->info.jit_free.node);
497 	kbase_kcpu_jit_retry_pending_allocs(kctx);
498 
499 	return rc;
500 }
501 
kbase_kcpu_jit_free_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_jit_free_info * free_info,struct kbase_kcpu_command * current_command)502 static int kbase_kcpu_jit_free_prepare(
503 		struct kbase_kcpu_command_queue *kcpu_queue,
504 		struct base_kcpu_command_jit_free_info *free_info,
505 		struct kbase_kcpu_command *current_command)
506 {
507 	struct kbase_context *const kctx = kcpu_queue->kctx;
508 	void __user *data = u64_to_user_ptr(free_info->ids);
509 	u8 *ids;
510 	u32 count = free_info->count;
511 	int ret;
512 	u32 i;
513 
514 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
515 
516 	/* Sanity checks */
517 	if (!count || count > ARRAY_SIZE(kctx->jit_alloc)) {
518 		ret = -EINVAL;
519 		goto out;
520 	}
521 
522 	/* Copy the information for safe access and future storage */
523 	ids = kmalloc_array(count, sizeof(*ids), GFP_KERNEL);
524 	if (!ids) {
525 		ret = -ENOMEM;
526 		goto out;
527 	}
528 
529 	if (!data) {
530 		ret = -EINVAL;
531 		goto out_free;
532 	}
533 
534 	if (copy_from_user(ids, data, sizeof(*ids) * count)) {
535 		ret = -EINVAL;
536 		goto out_free;
537 	}
538 
539 	for (i = 0; i < count; i++) {
540 		/* Fail the command if ID sent is zero */
541 		if (!ids[i]) {
542 			ret = -EINVAL;
543 			goto out_free;
544 		}
545 	}
546 
547 	/* Search for duplicate JIT ids */
548 	for (i = 0; i < (count - 1); i++) {
549 		u32 j;
550 
551 		for (j = (i + 1); j < count; j++) {
552 			if (ids[i] == ids[j]) {
553 				ret = -EINVAL;
554 				goto out_free;
555 			}
556 		}
557 	}
558 
559 	current_command->type = BASE_KCPU_COMMAND_TYPE_JIT_FREE;
560 	list_add_tail(&current_command->info.jit_free.node,
561 			&kctx->csf.kcpu_queues.jit_cmds_head);
562 	current_command->info.jit_free.ids = ids;
563 	current_command->info.jit_free.count = count;
564 
565 	return 0;
566 out_free:
567 	kfree(ids);
568 out:
569 	return ret;
570 }
571 
kbase_csf_queue_group_suspend_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_group_suspend_info * suspend_buf,struct kbase_kcpu_command * current_command)572 static int kbase_csf_queue_group_suspend_prepare(
573 		struct kbase_kcpu_command_queue *kcpu_queue,
574 		struct base_kcpu_command_group_suspend_info *suspend_buf,
575 		struct kbase_kcpu_command *current_command)
576 {
577 	struct kbase_context *const kctx = kcpu_queue->kctx;
578 	struct kbase_suspend_copy_buffer *sus_buf = NULL;
579 	const u32 csg_suspend_buf_size =
580 		kctx->kbdev->csf.global_iface.groups[0].suspend_size;
581 	u64 addr = suspend_buf->buffer;
582 	u64 page_addr = addr & PAGE_MASK;
583 	u64 end_addr = addr + csg_suspend_buf_size - 1;
584 	u64 last_page_addr = end_addr & PAGE_MASK;
585 	int nr_pages = (last_page_addr - page_addr) / PAGE_SIZE + 1;
586 	int pinned_pages = 0, ret = 0;
587 	struct kbase_va_region *reg;
588 
589 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
590 
591 	if (suspend_buf->size < csg_suspend_buf_size)
592 		return -EINVAL;
593 
594 	ret = kbase_csf_queue_group_handle_is_valid(kctx,
595 			suspend_buf->group_handle);
596 	if (ret)
597 		return ret;
598 
599 	sus_buf = kzalloc(sizeof(*sus_buf), GFP_KERNEL);
600 	if (!sus_buf)
601 		return -ENOMEM;
602 
603 	sus_buf->size = csg_suspend_buf_size;
604 	sus_buf->nr_pages = nr_pages;
605 	sus_buf->offset = addr & ~PAGE_MASK;
606 
607 	sus_buf->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
608 	if (!sus_buf->pages) {
609 		ret = -ENOMEM;
610 		goto out_clean_sus_buf;
611 	}
612 
613 	/* Check if the page_addr is a valid GPU VA from SAME_VA zone,
614 	 * otherwise consider it is a CPU VA corresponding to the Host
615 	 * memory allocated by userspace.
616 	 */
617 	kbase_gpu_vm_lock(kctx);
618 	reg = kbase_region_tracker_find_region_enclosing_address(kctx,
619 							page_addr);
620 
621 	if (kbase_is_region_invalid_or_free(reg)) {
622 		kbase_gpu_vm_unlock(kctx);
623 		pinned_pages = get_user_pages_fast(page_addr, nr_pages, 1,
624 							sus_buf->pages);
625 		kbase_gpu_vm_lock(kctx);
626 
627 		if (pinned_pages < 0) {
628 			ret = pinned_pages;
629 			goto out_clean_pages;
630 		}
631 		if (pinned_pages != nr_pages) {
632 			ret = -EINVAL;
633 			goto out_clean_pages;
634 		}
635 	} else {
636 		struct tagged_addr *page_array;
637 		u64 start, end, i;
638 
639 		if (!(reg->flags & BASE_MEM_SAME_VA) ||
640 				reg->nr_pages < nr_pages ||
641 				kbase_reg_current_backed_size(reg) !=
642 					reg->nr_pages) {
643 			ret = -EINVAL;
644 			goto out_clean_pages;
645 		}
646 
647 		start = PFN_DOWN(page_addr) - reg->start_pfn;
648 		end = start + nr_pages;
649 
650 		if (end > reg->nr_pages) {
651 			ret = -EINVAL;
652 			goto out_clean_pages;
653 		}
654 
655 		sus_buf->cpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
656 		kbase_mem_phy_alloc_kernel_mapped(reg->cpu_alloc);
657 		page_array = kbase_get_cpu_phy_pages(reg);
658 		page_array += start;
659 
660 		for (i = 0; i < nr_pages; i++, page_array++)
661 			sus_buf->pages[i] = as_page(*page_array);
662 	}
663 
664 	kbase_gpu_vm_unlock(kctx);
665 	current_command->type = BASE_KCPU_COMMAND_TYPE_GROUP_SUSPEND;
666 	current_command->info.suspend_buf_copy.sus_buf = sus_buf;
667 	current_command->info.suspend_buf_copy.group_handle =
668 				suspend_buf->group_handle;
669 	return ret;
670 
671 out_clean_pages:
672 	kbase_gpu_vm_unlock(kctx);
673 	kfree(sus_buf->pages);
674 out_clean_sus_buf:
675 	kfree(sus_buf);
676 
677 	return ret;
678 }
679 
kbase_csf_queue_group_suspend_process(struct kbase_context * kctx,struct kbase_suspend_copy_buffer * sus_buf,u8 group_handle)680 static int kbase_csf_queue_group_suspend_process(struct kbase_context *kctx,
681 		struct kbase_suspend_copy_buffer *sus_buf,
682 		u8 group_handle)
683 {
684 	return kbase_csf_queue_group_suspend(kctx, sus_buf, group_handle);
685 }
686 
event_cqs_callback(void * param)687 static enum kbase_csf_event_callback_action event_cqs_callback(void *param)
688 {
689 	struct kbase_kcpu_command_queue *kcpu_queue =
690 		(struct kbase_kcpu_command_queue *)param;
691 	struct kbase_context *const kctx = kcpu_queue->kctx;
692 
693 	queue_work(kctx->csf.kcpu_queues.wq, &kcpu_queue->work);
694 
695 	return KBASE_CSF_EVENT_CALLBACK_KEEP;
696 }
697 
cleanup_cqs_wait(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_wait_info * cqs_wait)698 static void cleanup_cqs_wait(struct kbase_kcpu_command_queue *queue,
699 		struct kbase_kcpu_command_cqs_wait_info *cqs_wait)
700 {
701 	WARN_ON(!cqs_wait->nr_objs);
702 	WARN_ON(!cqs_wait->objs);
703 	WARN_ON(!cqs_wait->signaled);
704 	WARN_ON(!queue->cqs_wait_count);
705 
706 	if (--queue->cqs_wait_count == 0) {
707 		kbase_csf_event_wait_remove(queue->kctx,
708 				event_cqs_callback, queue);
709 	}
710 
711 	kfree(cqs_wait->signaled);
712 	kfree(cqs_wait->objs);
713 	cqs_wait->signaled = NULL;
714 	cqs_wait->objs = NULL;
715 }
716 
kbase_kcpu_cqs_wait_process(struct kbase_device * kbdev,struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_wait_info * cqs_wait)717 static int kbase_kcpu_cqs_wait_process(struct kbase_device *kbdev,
718 		struct kbase_kcpu_command_queue *queue,
719 		struct kbase_kcpu_command_cqs_wait_info *cqs_wait)
720 {
721 	u32 i;
722 
723 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
724 
725 	if (WARN_ON(!cqs_wait->objs))
726 		return -EINVAL;
727 
728 	/* Skip the CQS waits that have already been signaled when processing */
729 	for (i = find_first_zero_bit(cqs_wait->signaled, cqs_wait->nr_objs); i < cqs_wait->nr_objs; i++) {
730 		if (!test_bit(i, cqs_wait->signaled)) {
731 			struct kbase_vmap_struct *mapping;
732 			bool sig_set;
733 			u32 *evt = (u32 *)kbase_phy_alloc_mapping_get(queue->kctx,
734 						cqs_wait->objs[i].addr, &mapping);
735 
736 			if (!queue->command_started) {
737 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_CQS_WAIT_START(
738 					kbdev, queue);
739 				queue->command_started = true;
740 				KBASE_KTRACE_ADD_CSF_KCPU(kbdev, CQS_WAIT_START,
741 						   queue, cqs_wait->nr_objs, 0);
742 			}
743 
744 			if (!evt) {
745 				dev_warn(kbdev->dev,
746 					"Sync memory %llx already freed", cqs_wait->objs[i].addr);
747 				queue->has_error = true;
748 				return -EINVAL;
749 			}
750 
751 			sig_set = evt[BASEP_EVENT_VAL_INDEX] > cqs_wait->objs[i].val;
752 			if (sig_set) {
753 				bool error = false;
754 
755 				bitmap_set(cqs_wait->signaled, i, 1);
756 				if ((cqs_wait->inherit_err_flags & (1U << i)) &&
757 				    evt[BASEP_EVENT_ERR_INDEX] > 0) {
758 					queue->has_error = true;
759 					error = true;
760 				}
761 
762 				KBASE_KTRACE_ADD_CSF_KCPU(kbdev, CQS_WAIT_END,
763 						queue, cqs_wait->objs[i].addr,
764 						error);
765 
766 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_CQS_WAIT_END(
767 					kbdev, queue,
768 					evt[BASEP_EVENT_ERR_INDEX]);
769 				queue->command_started = false;
770 			}
771 
772 			kbase_phy_alloc_mapping_put(queue->kctx, mapping);
773 
774 			if (!sig_set)
775 				break;
776 		}
777 	}
778 
779 	/* For the queue to progress further, all cqs objects should get
780 	 * signaled.
781 	 */
782 	return bitmap_full(cqs_wait->signaled, cqs_wait->nr_objs);
783 }
784 
kbase_kcpu_cqs_wait_prepare(struct kbase_kcpu_command_queue * queue,struct base_kcpu_command_cqs_wait_info * cqs_wait_info,struct kbase_kcpu_command * current_command)785 static int kbase_kcpu_cqs_wait_prepare(struct kbase_kcpu_command_queue *queue,
786 		struct base_kcpu_command_cqs_wait_info *cqs_wait_info,
787 		struct kbase_kcpu_command *current_command)
788 {
789 	struct base_cqs_wait_info *objs;
790 	unsigned int nr_objs = cqs_wait_info->nr_objs;
791 
792 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
793 
794 	if (nr_objs > BASEP_KCPU_CQS_MAX_NUM_OBJS)
795 		return -EINVAL;
796 
797 	if (!nr_objs)
798 		return -EINVAL;
799 
800 	objs = kcalloc(nr_objs, sizeof(*objs), GFP_KERNEL);
801 	if (!objs)
802 		return -ENOMEM;
803 
804 	if (copy_from_user(objs, u64_to_user_ptr(cqs_wait_info->objs),
805 			nr_objs * sizeof(*objs))) {
806 		kfree(objs);
807 		return -ENOMEM;
808 	}
809 
810 	if (++queue->cqs_wait_count == 1) {
811 		if (kbase_csf_event_wait_add(queue->kctx,
812 				event_cqs_callback, queue)) {
813 			kfree(objs);
814 			queue->cqs_wait_count--;
815 			return -ENOMEM;
816 		}
817 	}
818 
819 	current_command->type = BASE_KCPU_COMMAND_TYPE_CQS_WAIT;
820 	current_command->info.cqs_wait.nr_objs = nr_objs;
821 	current_command->info.cqs_wait.objs = objs;
822 	current_command->info.cqs_wait.inherit_err_flags =
823 					cqs_wait_info->inherit_err_flags;
824 
825 	current_command->info.cqs_wait.signaled = kcalloc(BITS_TO_LONGS(nr_objs),
826 		sizeof(*current_command->info.cqs_wait.signaled), GFP_KERNEL);
827 	if (!current_command->info.cqs_wait.signaled) {
828 		if (--queue->cqs_wait_count == 0) {
829 			kbase_csf_event_wait_remove(queue->kctx,
830 				event_cqs_callback, queue);
831 		}
832 
833 		kfree(objs);
834 		return -ENOMEM;
835 	}
836 
837 	return 0;
838 }
839 
kbase_kcpu_cqs_set_process(struct kbase_device * kbdev,struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_set_info * cqs_set)840 static void kbase_kcpu_cqs_set_process(struct kbase_device *kbdev,
841 		struct kbase_kcpu_command_queue *queue,
842 		struct kbase_kcpu_command_cqs_set_info *cqs_set)
843 {
844 	unsigned int i;
845 
846 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
847 
848 	if (WARN_ON(!cqs_set->objs))
849 		return;
850 
851 	for (i = 0; i < cqs_set->nr_objs; i++) {
852 		struct kbase_vmap_struct *mapping;
853 		u32 *evt;
854 
855 		evt = (u32 *)kbase_phy_alloc_mapping_get(
856 			queue->kctx, cqs_set->objs[i].addr, &mapping);
857 
858 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_CQS_SET(kbdev, queue,
859 								  evt ? 0 : 1);
860 
861 		if (!evt) {
862 			dev_warn(kbdev->dev,
863 				"Sync memory %llx already freed", cqs_set->objs[i].addr);
864 			queue->has_error = true;
865 		} else {
866 			evt[BASEP_EVENT_ERR_INDEX] = queue->has_error;
867 			/* Set to signaled */
868 			evt[BASEP_EVENT_VAL_INDEX]++;
869 			kbase_phy_alloc_mapping_put(queue->kctx, mapping);
870 
871 			KBASE_KTRACE_ADD_CSF_KCPU(kbdev, CQS_SET,
872 					queue, cqs_set->objs[i].addr,
873 					evt[BASEP_EVENT_ERR_INDEX]);
874 		}
875 	}
876 
877 	kbase_csf_event_signal_notify_gpu(queue->kctx);
878 
879 	kfree(cqs_set->objs);
880 	cqs_set->objs = NULL;
881 }
882 
kbase_kcpu_cqs_set_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_cqs_set_info * cqs_set_info,struct kbase_kcpu_command * current_command)883 static int kbase_kcpu_cqs_set_prepare(
884 		struct kbase_kcpu_command_queue *kcpu_queue,
885 		struct base_kcpu_command_cqs_set_info *cqs_set_info,
886 		struct kbase_kcpu_command *current_command)
887 {
888 	struct kbase_context *const kctx = kcpu_queue->kctx;
889 	struct base_cqs_set *objs;
890 	unsigned int nr_objs = cqs_set_info->nr_objs;
891 
892 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
893 
894 	if (nr_objs > BASEP_KCPU_CQS_MAX_NUM_OBJS)
895 		return -EINVAL;
896 
897 	if (!nr_objs)
898 		return -EINVAL;
899 
900 	objs = kcalloc(nr_objs, sizeof(*objs), GFP_KERNEL);
901 	if (!objs)
902 		return -ENOMEM;
903 
904 	if (copy_from_user(objs, u64_to_user_ptr(cqs_set_info->objs),
905 			nr_objs * sizeof(*objs))) {
906 		kfree(objs);
907 		return -ENOMEM;
908 	}
909 
910 	current_command->type = BASE_KCPU_COMMAND_TYPE_CQS_SET;
911 	current_command->info.cqs_set.nr_objs = nr_objs;
912 	current_command->info.cqs_set.objs = objs;
913 
914 	return 0;
915 }
916 
cleanup_cqs_wait_operation(struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_wait_operation_info * cqs_wait_operation)917 static void cleanup_cqs_wait_operation(struct kbase_kcpu_command_queue *queue,
918 		struct kbase_kcpu_command_cqs_wait_operation_info *cqs_wait_operation)
919 {
920 	WARN_ON(!cqs_wait_operation->nr_objs);
921 	WARN_ON(!cqs_wait_operation->objs);
922 	WARN_ON(!cqs_wait_operation->signaled);
923 	WARN_ON(!queue->cqs_wait_count);
924 
925 	if (--queue->cqs_wait_count == 0) {
926 		kbase_csf_event_wait_remove(queue->kctx,
927 				event_cqs_callback, queue);
928 	}
929 
930 	kfree(cqs_wait_operation->signaled);
931 	kfree(cqs_wait_operation->objs);
932 	cqs_wait_operation->signaled = NULL;
933 	cqs_wait_operation->objs = NULL;
934 }
935 
kbase_kcpu_cqs_wait_operation_process(struct kbase_device * kbdev,struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_wait_operation_info * cqs_wait_operation)936 static int kbase_kcpu_cqs_wait_operation_process(struct kbase_device *kbdev,
937 		struct kbase_kcpu_command_queue *queue,
938 		struct kbase_kcpu_command_cqs_wait_operation_info *cqs_wait_operation)
939 {
940 	u32 i;
941 
942 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
943 
944 	if (WARN_ON(!cqs_wait_operation->objs))
945 		return -EINVAL;
946 
947 	/* Skip the CQS waits that have already been signaled when processing */
948 	for (i = find_first_zero_bit(cqs_wait_operation->signaled, cqs_wait_operation->nr_objs); i < cqs_wait_operation->nr_objs; i++) {
949 		if (!test_bit(i, cqs_wait_operation->signaled)) {
950 			struct kbase_vmap_struct *mapping;
951 			bool sig_set;
952 			u64 *evt = (u64 *)kbase_phy_alloc_mapping_get(queue->kctx,
953 						cqs_wait_operation->objs[i].addr, &mapping);
954 
955 			/* GPUCORE-28172 RDT to review */
956 			if (!queue->command_started)
957 				queue->command_started = true;
958 
959 			if (!evt) {
960 				dev_warn(kbdev->dev,
961 					"Sync memory %llx already freed", cqs_wait_operation->objs[i].addr);
962 				queue->has_error = true;
963 				return -EINVAL;
964 			}
965 
966 			switch (cqs_wait_operation->objs[i].operation) {
967 			case BASEP_CQS_WAIT_OPERATION_LE:
968 				sig_set = *evt <= cqs_wait_operation->objs[i].val;
969 				break;
970 			case BASEP_CQS_WAIT_OPERATION_GT:
971 				sig_set = *evt > cqs_wait_operation->objs[i].val;
972 				break;
973 			default:
974 				dev_dbg(kbdev->dev,
975 					"Unsupported CQS wait operation %d", cqs_wait_operation->objs[i].operation);
976 
977 				kbase_phy_alloc_mapping_put(queue->kctx, mapping);
978 				queue->has_error = true;
979 
980 				return -EINVAL;
981 			}
982 
983 			/* Increment evt up to the error_state value depending on the CQS data type */
984 			switch (cqs_wait_operation->objs[i].data_type) {
985 			default:
986 				dev_dbg(kbdev->dev, "Unreachable data_type=%d", cqs_wait_operation->objs[i].data_type);
987 				/* Fallthrough - hint to compiler that there's really only 2 options at present */
988 				fallthrough;
989 			case BASEP_CQS_DATA_TYPE_U32:
990 				evt = (u64 *)((u8 *)evt + sizeof(u32));
991 				break;
992 			case BASEP_CQS_DATA_TYPE_U64:
993 				evt = (u64 *)((u8 *)evt + sizeof(u64));
994 				break;
995 			}
996 
997 			if (sig_set) {
998 				bitmap_set(cqs_wait_operation->signaled, i, 1);
999 				if ((cqs_wait_operation->inherit_err_flags & (1U << i)) &&
1000 				    *evt > 0) {
1001 					queue->has_error = true;
1002 				}
1003 
1004 				/* GPUCORE-28172 RDT to review */
1005 
1006 				queue->command_started = false;
1007 			}
1008 
1009 			kbase_phy_alloc_mapping_put(queue->kctx, mapping);
1010 
1011 			if (!sig_set)
1012 				break;
1013 		}
1014 	}
1015 
1016 	/* For the queue to progress further, all cqs objects should get
1017 	 * signaled.
1018 	 */
1019 	return bitmap_full(cqs_wait_operation->signaled, cqs_wait_operation->nr_objs);
1020 }
1021 
kbase_kcpu_cqs_wait_operation_prepare(struct kbase_kcpu_command_queue * queue,struct base_kcpu_command_cqs_wait_operation_info * cqs_wait_operation_info,struct kbase_kcpu_command * current_command)1022 static int kbase_kcpu_cqs_wait_operation_prepare(struct kbase_kcpu_command_queue *queue,
1023 		struct base_kcpu_command_cqs_wait_operation_info *cqs_wait_operation_info,
1024 		struct kbase_kcpu_command *current_command)
1025 {
1026 	struct base_cqs_wait_operation_info *objs;
1027 	unsigned int nr_objs = cqs_wait_operation_info->nr_objs;
1028 
1029 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
1030 
1031 	if (nr_objs > BASEP_KCPU_CQS_MAX_NUM_OBJS)
1032 		return -EINVAL;
1033 
1034 	if (!nr_objs)
1035 		return -EINVAL;
1036 
1037 	objs = kcalloc(nr_objs, sizeof(*objs), GFP_KERNEL);
1038 	if (!objs)
1039 		return -ENOMEM;
1040 
1041 	if (copy_from_user(objs, u64_to_user_ptr(cqs_wait_operation_info->objs),
1042 			nr_objs * sizeof(*objs))) {
1043 		kfree(objs);
1044 		return -ENOMEM;
1045 	}
1046 
1047 	if (++queue->cqs_wait_count == 1) {
1048 		if (kbase_csf_event_wait_add(queue->kctx,
1049 				event_cqs_callback, queue)) {
1050 			kfree(objs);
1051 			queue->cqs_wait_count--;
1052 			return -ENOMEM;
1053 		}
1054 	}
1055 
1056 	current_command->type = BASE_KCPU_COMMAND_TYPE_CQS_WAIT_OPERATION;
1057 	current_command->info.cqs_wait_operation.nr_objs = nr_objs;
1058 	current_command->info.cqs_wait_operation.objs = objs;
1059 	current_command->info.cqs_wait_operation.inherit_err_flags =
1060 					cqs_wait_operation_info->inherit_err_flags;
1061 
1062 	current_command->info.cqs_wait_operation.signaled = kcalloc(BITS_TO_LONGS(nr_objs),
1063 		sizeof(*current_command->info.cqs_wait_operation.signaled), GFP_KERNEL);
1064 	if (!current_command->info.cqs_wait_operation.signaled) {
1065 		if (--queue->cqs_wait_count == 0) {
1066 			kbase_csf_event_wait_remove(queue->kctx,
1067 				event_cqs_callback, queue);
1068 		}
1069 
1070 		kfree(objs);
1071 		return -ENOMEM;
1072 	}
1073 
1074 	return 0;
1075 }
1076 
kbase_kcpu_cqs_set_operation_process(struct kbase_device * kbdev,struct kbase_kcpu_command_queue * queue,struct kbase_kcpu_command_cqs_set_operation_info * cqs_set_operation)1077 static void kbase_kcpu_cqs_set_operation_process(
1078 		struct kbase_device *kbdev,
1079 		struct kbase_kcpu_command_queue *queue,
1080 		struct kbase_kcpu_command_cqs_set_operation_info *cqs_set_operation)
1081 {
1082 	unsigned int i;
1083 
1084 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
1085 
1086 	if (WARN_ON(!cqs_set_operation->objs))
1087 		return;
1088 
1089 	for (i = 0; i < cqs_set_operation->nr_objs; i++) {
1090 		struct kbase_vmap_struct *mapping;
1091 		u64 *evt;
1092 
1093 		evt = (u64 *)kbase_phy_alloc_mapping_get(
1094 			queue->kctx, cqs_set_operation->objs[i].addr, &mapping);
1095 
1096 		/* GPUCORE-28172 RDT to review */
1097 
1098 		if (!evt) {
1099 			dev_warn(kbdev->dev,
1100 				"Sync memory %llx already freed", cqs_set_operation->objs[i].addr);
1101 			queue->has_error = true;
1102 		} else {
1103 			switch (cqs_set_operation->objs[i].operation) {
1104 			case BASEP_CQS_SET_OPERATION_ADD:
1105 				*evt += cqs_set_operation->objs[i].val;
1106 				break;
1107 			case BASEP_CQS_SET_OPERATION_SET:
1108 				*evt = cqs_set_operation->objs[i].val;
1109 				break;
1110 			default:
1111 				dev_dbg(kbdev->dev,
1112 					"Unsupported CQS set operation %d", cqs_set_operation->objs[i].operation);
1113 				queue->has_error = true;
1114 				break;
1115 			}
1116 
1117 			/* Increment evt up to the error_state value depending on the CQS data type */
1118 			switch (cqs_set_operation->objs[i].data_type) {
1119 			default:
1120 				dev_dbg(kbdev->dev, "Unreachable data_type=%d", cqs_set_operation->objs[i].data_type);
1121 				/* Fallthrough - hint to compiler that there's really only 2 options at present */
1122 				fallthrough;
1123 			case BASEP_CQS_DATA_TYPE_U32:
1124 				evt = (u64 *)((u8 *)evt + sizeof(u32));
1125 				break;
1126 			case BASEP_CQS_DATA_TYPE_U64:
1127 				evt = (u64 *)((u8 *)evt + sizeof(u64));
1128 				break;
1129 			}
1130 
1131 			/* GPUCORE-28172 RDT to review */
1132 
1133 			/* Always propagate errors */
1134 			*evt = queue->has_error;
1135 
1136 			kbase_phy_alloc_mapping_put(queue->kctx, mapping);
1137 		}
1138 	}
1139 
1140 	kbase_csf_event_signal_notify_gpu(queue->kctx);
1141 
1142 	kfree(cqs_set_operation->objs);
1143 	cqs_set_operation->objs = NULL;
1144 }
1145 
kbase_kcpu_cqs_set_operation_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_cqs_set_operation_info * cqs_set_operation_info,struct kbase_kcpu_command * current_command)1146 static int kbase_kcpu_cqs_set_operation_prepare(
1147 		struct kbase_kcpu_command_queue *kcpu_queue,
1148 		struct base_kcpu_command_cqs_set_operation_info *cqs_set_operation_info,
1149 		struct kbase_kcpu_command *current_command)
1150 {
1151 	struct kbase_context *const kctx = kcpu_queue->kctx;
1152 	struct base_cqs_set_operation_info *objs;
1153 	unsigned int nr_objs = cqs_set_operation_info->nr_objs;
1154 
1155 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
1156 
1157 	if (nr_objs > BASEP_KCPU_CQS_MAX_NUM_OBJS)
1158 		return -EINVAL;
1159 
1160 	if (!nr_objs)
1161 		return -EINVAL;
1162 
1163 	objs = kcalloc(nr_objs, sizeof(*objs), GFP_KERNEL);
1164 	if (!objs)
1165 		return -ENOMEM;
1166 
1167 	if (copy_from_user(objs, u64_to_user_ptr(cqs_set_operation_info->objs),
1168 			nr_objs * sizeof(*objs))) {
1169 		kfree(objs);
1170 		return -ENOMEM;
1171 	}
1172 
1173 	current_command->type = BASE_KCPU_COMMAND_TYPE_CQS_SET_OPERATION;
1174 	current_command->info.cqs_set_operation.nr_objs = nr_objs;
1175 	current_command->info.cqs_set_operation.objs = objs;
1176 
1177 	return 0;
1178 }
1179 
1180 #if IS_ENABLED(CONFIG_SYNC_FILE)
1181 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
kbase_csf_fence_wait_callback(struct fence * fence,struct fence_cb * cb)1182 static void kbase_csf_fence_wait_callback(struct fence *fence,
1183 			struct fence_cb *cb)
1184 #else
1185 static void kbase_csf_fence_wait_callback(struct dma_fence *fence,
1186 			struct dma_fence_cb *cb)
1187 #endif
1188 {
1189 	struct kbase_kcpu_command_fence_info *fence_info = container_of(cb,
1190 			struct kbase_kcpu_command_fence_info, fence_cb);
1191 	struct kbase_kcpu_command_queue *kcpu_queue = fence_info->kcpu_queue;
1192 	struct kbase_context *const kctx = kcpu_queue->kctx;
1193 
1194 	KBASE_KTRACE_ADD_CSF_KCPU(kctx->kbdev, FENCE_WAIT_END, kcpu_queue,
1195 				  fence->context, fence->seqno);
1196 
1197 	/* Resume kcpu command queue processing. */
1198 	queue_work(kctx->csf.kcpu_queues.wq, &kcpu_queue->work);
1199 }
1200 
kbase_kcpu_fence_wait_cancel(struct kbase_kcpu_command_queue * kcpu_queue,struct kbase_kcpu_command_fence_info * fence_info)1201 static void kbase_kcpu_fence_wait_cancel(
1202 		struct kbase_kcpu_command_queue *kcpu_queue,
1203 		struct kbase_kcpu_command_fence_info *fence_info)
1204 {
1205 	struct kbase_context *const kctx = kcpu_queue->kctx;
1206 
1207 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
1208 
1209 	if (WARN_ON(!fence_info->fence))
1210 		return;
1211 
1212 	if (kcpu_queue->fence_wait_processed) {
1213 		bool removed = dma_fence_remove_callback(fence_info->fence,
1214 				&fence_info->fence_cb);
1215 
1216 		if (removed)
1217 			KBASE_KTRACE_ADD_CSF_KCPU(kctx->kbdev, FENCE_WAIT_END,
1218 					kcpu_queue, fence_info->fence->context,
1219 					fence_info->fence->seqno);
1220 	}
1221 
1222 	/* Release the reference which is kept by the kcpu_queue */
1223 	kbase_fence_put(fence_info->fence);
1224 	kcpu_queue->fence_wait_processed = false;
1225 
1226 	fence_info->fence = NULL;
1227 }
1228 
1229 /**
1230  * kbase_kcpu_fence_wait_process() - Process the kcpu fence wait command
1231  *
1232  * @kcpu_queue: The queue containing the fence wait command
1233  * @fence_info: Reference to a fence for which the command is waiting
1234  *
1235  * Return: 0 if fence wait is blocked, 1 if it is unblocked, negative error if
1236  *         an error has occurred and fence should no longer be waited on.
1237  */
kbase_kcpu_fence_wait_process(struct kbase_kcpu_command_queue * kcpu_queue,struct kbase_kcpu_command_fence_info * fence_info)1238 static int kbase_kcpu_fence_wait_process(
1239 		struct kbase_kcpu_command_queue *kcpu_queue,
1240 		struct kbase_kcpu_command_fence_info *fence_info)
1241 {
1242 	int fence_status = 0;
1243 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
1244 	struct fence *fence;
1245 #else
1246 	struct dma_fence *fence;
1247 #endif
1248 
1249 	lockdep_assert_held(&kcpu_queue->kctx->csf.kcpu_queues.lock);
1250 
1251 	if (WARN_ON(!fence_info->fence))
1252 		return -EINVAL;
1253 
1254 	fence = fence_info->fence;
1255 
1256 	if (kcpu_queue->fence_wait_processed) {
1257 		fence_status = dma_fence_get_status(fence);
1258 	} else {
1259 		int cb_err = dma_fence_add_callback(fence,
1260 			&fence_info->fence_cb,
1261 			kbase_csf_fence_wait_callback);
1262 
1263 		KBASE_KTRACE_ADD_CSF_KCPU(kcpu_queue->kctx->kbdev,
1264 					  FENCE_WAIT_START, kcpu_queue,
1265 					  fence->context, fence->seqno);
1266 		fence_status = cb_err;
1267 		if (cb_err == 0)
1268 			kcpu_queue->fence_wait_processed = true;
1269 		else if (cb_err == -ENOENT)
1270 			fence_status = dma_fence_get_status(fence);
1271 	}
1272 
1273 	/*
1274 	 * At this point fence status can contain 3 types of values:
1275 	 * - Value 0 to represent that fence in question is not signalled yet
1276 	 * - Value 1 to represent that fence in question is signalled without
1277 	 *   errors
1278 	 * - Negative error code to represent that some error has occurred such
1279 	 *   that waiting on it is no longer valid.
1280 	 */
1281 
1282 	if (fence_status)
1283 		kbase_kcpu_fence_wait_cancel(kcpu_queue, fence_info);
1284 
1285 	return fence_status;
1286 }
1287 
kbase_kcpu_fence_wait_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_fence_info * fence_info,struct kbase_kcpu_command * current_command)1288 static int kbase_kcpu_fence_wait_prepare(
1289 		struct kbase_kcpu_command_queue *kcpu_queue,
1290 		struct base_kcpu_command_fence_info *fence_info,
1291 		struct kbase_kcpu_command *current_command)
1292 {
1293 	struct kbase_context *const kctx = kcpu_queue->kctx;
1294 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
1295 	struct fence *fence_in;
1296 #else
1297 	struct dma_fence *fence_in;
1298 #endif
1299 	struct base_fence fence;
1300 
1301 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
1302 
1303 	if (copy_from_user(&fence, u64_to_user_ptr(fence_info->fence),
1304 			sizeof(fence)))
1305 		return -ENOMEM;
1306 
1307 	fence_in = sync_file_get_fence(fence.basep.fd);
1308 
1309 	if (!fence_in)
1310 		return -ENOENT;
1311 
1312 	current_command->type = BASE_KCPU_COMMAND_TYPE_FENCE_WAIT;
1313 	current_command->info.fence.fence = fence_in;
1314 	current_command->info.fence.kcpu_queue = kcpu_queue;
1315 
1316 	return 0;
1317 }
1318 
kbase_kcpu_fence_signal_process(struct kbase_kcpu_command_queue * kcpu_queue,struct kbase_kcpu_command_fence_info * fence_info)1319 static int kbase_kcpu_fence_signal_process(
1320 		struct kbase_kcpu_command_queue *kcpu_queue,
1321 		struct kbase_kcpu_command_fence_info *fence_info)
1322 {
1323 	struct kbase_context *const kctx = kcpu_queue->kctx;
1324 	int ret;
1325 
1326 	if (WARN_ON(!fence_info->fence))
1327 		return -EINVAL;
1328 
1329 	ret = dma_fence_signal(fence_info->fence);
1330 
1331 	if (unlikely(ret < 0)) {
1332 		dev_warn(kctx->kbdev->dev,
1333 			"fence_signal() failed with %d\n", ret);
1334 	}
1335 
1336 	KBASE_KTRACE_ADD_CSF_KCPU(kctx->kbdev, FENCE_SIGNAL, kcpu_queue,
1337 				  fence_info->fence->context,
1338 				  fence_info->fence->seqno);
1339 
1340 	dma_fence_put(fence_info->fence);
1341 	fence_info->fence = NULL;
1342 
1343 	return ret;
1344 }
1345 
kbase_kcpu_fence_signal_prepare(struct kbase_kcpu_command_queue * kcpu_queue,struct base_kcpu_command_fence_info * fence_info,struct kbase_kcpu_command * current_command)1346 static int kbase_kcpu_fence_signal_prepare(
1347 		struct kbase_kcpu_command_queue *kcpu_queue,
1348 		struct base_kcpu_command_fence_info *fence_info,
1349 		struct kbase_kcpu_command *current_command)
1350 {
1351 	struct kbase_context *const kctx = kcpu_queue->kctx;
1352 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
1353 	struct fence *fence_out;
1354 #else
1355 	struct dma_fence *fence_out;
1356 #endif
1357 	struct base_fence fence;
1358 	struct sync_file *sync_file;
1359 	int ret = 0;
1360 	int fd;
1361 
1362 	lockdep_assert_held(&kctx->csf.kcpu_queues.lock);
1363 
1364 	if (copy_from_user(&fence, u64_to_user_ptr(fence_info->fence),
1365 			sizeof(fence)))
1366 		return -EFAULT;
1367 
1368 	fence_out = kzalloc(sizeof(*fence_out), GFP_KERNEL);
1369 	if (!fence_out)
1370 		return -ENOMEM;
1371 
1372 	dma_fence_init(fence_out,
1373 		       &kbase_fence_ops,
1374 		       &kbase_csf_fence_lock,
1375 		       kcpu_queue->fence_context,
1376 		       ++kcpu_queue->fence_seqno);
1377 
1378 #if (KERNEL_VERSION(4, 9, 67) >= LINUX_VERSION_CODE)
1379 	/* Take an extra reference to the fence on behalf of the sync file.
1380 	 * This is only needded on older kernels where sync_file_create()
1381 	 * does not take its own reference. This was changed in v4.9.68
1382 	 * where sync_file_create() now takes its own reference.
1383 	 */
1384 	dma_fence_get(fence_out);
1385 #endif
1386 
1387 	/* create a sync_file fd representing the fence */
1388 	sync_file = sync_file_create(fence_out);
1389 	if (!sync_file) {
1390 #if (KERNEL_VERSION(4, 9, 67) >= LINUX_VERSION_CODE)
1391 		dma_fence_put(fence_out);
1392 #endif
1393 		ret = -ENOMEM;
1394 		goto file_create_fail;
1395 	}
1396 
1397 	fd = get_unused_fd_flags(O_CLOEXEC);
1398 	if (fd < 0) {
1399 		ret = fd;
1400 		goto fd_flags_fail;
1401 	}
1402 
1403 	fd_install(fd, sync_file->file);
1404 
1405 	fence.basep.fd = fd;
1406 
1407 	current_command->type = BASE_KCPU_COMMAND_TYPE_FENCE_SIGNAL;
1408 	current_command->info.fence.fence = fence_out;
1409 
1410 	if (copy_to_user(u64_to_user_ptr(fence_info->fence), &fence,
1411 			sizeof(fence))) {
1412 		ret = -EFAULT;
1413 		goto fd_flags_fail;
1414 	}
1415 
1416 	return 0;
1417 
1418 fd_flags_fail:
1419 	fput(sync_file->file);
1420 file_create_fail:
1421 	dma_fence_put(fence_out);
1422 
1423 	return ret;
1424 }
1425 #endif /* CONFIG_SYNC_FILE */
1426 
kcpu_queue_process_worker(struct work_struct * data)1427 static void kcpu_queue_process_worker(struct work_struct *data)
1428 {
1429 	struct kbase_kcpu_command_queue *queue = container_of(data,
1430 				struct kbase_kcpu_command_queue, work);
1431 
1432 	mutex_lock(&queue->kctx->csf.kcpu_queues.lock);
1433 
1434 	kcpu_queue_process(queue, false);
1435 
1436 	mutex_unlock(&queue->kctx->csf.kcpu_queues.lock);
1437 }
1438 
delete_queue(struct kbase_context * kctx,u32 id)1439 static int delete_queue(struct kbase_context *kctx, u32 id)
1440 {
1441 	int err = 0;
1442 
1443 	mutex_lock(&kctx->csf.kcpu_queues.lock);
1444 
1445 	if ((id < KBASEP_MAX_KCPU_QUEUES) && kctx->csf.kcpu_queues.array[id]) {
1446 		struct kbase_kcpu_command_queue *queue =
1447 					kctx->csf.kcpu_queues.array[id];
1448 
1449 		KBASE_KTRACE_ADD_CSF_KCPU(kctx->kbdev, KCPU_QUEUE_DESTROY,
1450 			queue, queue->num_pending_cmds, queue->cqs_wait_count);
1451 
1452 		/* Drain the remaining work for this queue first and go past
1453 		 * all the waits.
1454 		 */
1455 		kcpu_queue_process(queue, true);
1456 
1457 		/* All commands should have been processed */
1458 		WARN_ON(queue->num_pending_cmds);
1459 
1460 		/* All CQS wait commands should have been cleaned up */
1461 		WARN_ON(queue->cqs_wait_count);
1462 
1463 		kctx->csf.kcpu_queues.array[id] = NULL;
1464 		bitmap_clear(kctx->csf.kcpu_queues.in_use, id, 1);
1465 
1466 		/* Fire the tracepoint with the mutex held to enforce correct
1467 		 * ordering with the summary stream.
1468 		 */
1469 		KBASE_TLSTREAM_TL_KBASE_DEL_KCPUQUEUE(kctx->kbdev, queue);
1470 
1471 		mutex_unlock(&kctx->csf.kcpu_queues.lock);
1472 
1473 		cancel_work_sync(&queue->work);
1474 
1475 		kfree(queue);
1476 	} else {
1477 		dev_dbg(kctx->kbdev->dev,
1478 			"Attempt to delete a non-existent KCPU queue");
1479 		mutex_unlock(&kctx->csf.kcpu_queues.lock);
1480 		err = -EINVAL;
1481 	}
1482 	return err;
1483 }
1484 
KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_INFO(struct kbase_device * kbdev,const struct kbase_kcpu_command_queue * queue,const struct kbase_kcpu_command_jit_alloc_info * jit_alloc,int alloc_status)1485 static void KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_INFO(
1486 	struct kbase_device *kbdev,
1487 	const struct kbase_kcpu_command_queue *queue,
1488 	const struct kbase_kcpu_command_jit_alloc_info *jit_alloc,
1489 	int alloc_status)
1490 {
1491 	u8 i;
1492 
1493 	KBASE_TLSTREAM_TL_KBASE_ARRAY_BEGIN_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(
1494 		kbdev, queue);
1495 	for (i = 0; i < jit_alloc->count; i++) {
1496 		const u8 id = jit_alloc->info[i].id;
1497 		const struct kbase_va_region *reg = queue->kctx->jit_alloc[id];
1498 		u64 gpu_alloc_addr = 0;
1499 		u64 mmu_flags = 0;
1500 
1501 		if ((alloc_status == 0) && !WARN_ON(!reg) &&
1502 		    !WARN_ON(reg == KBASE_RESERVED_REG_JIT_ALLOC)) {
1503 #ifdef CONFIG_MALI_VECTOR_DUMP
1504 			struct tagged_addr phy = {0};
1505 #endif /* CONFIG_MALI_VECTOR_DUMP */
1506 
1507 			gpu_alloc_addr = reg->start_pfn << PAGE_SHIFT;
1508 #ifdef CONFIG_MALI_VECTOR_DUMP
1509 			mmu_flags = kbase_mmu_create_ate(kbdev,
1510 				phy, reg->flags,
1511 				MIDGARD_MMU_BOTTOMLEVEL,
1512 				queue->kctx->jit_group_id);
1513 #endif /* CONFIG_MALI_VECTOR_DUMP */
1514 		}
1515 		KBASE_TLSTREAM_TL_KBASE_ARRAY_ITEM_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(
1516 			kbdev, queue, alloc_status, gpu_alloc_addr, mmu_flags);
1517 	}
1518 }
1519 
KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(struct kbase_device * kbdev,const struct kbase_kcpu_command_queue * queue)1520 static void KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(
1521 	struct kbase_device *kbdev,
1522 	const struct kbase_kcpu_command_queue *queue)
1523 {
1524 	KBASE_TLSTREAM_TL_KBASE_ARRAY_END_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(
1525 		kbdev, queue);
1526 }
1527 
KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_FREE_END(struct kbase_device * kbdev,const struct kbase_kcpu_command_queue * queue)1528 static void KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_FREE_END(
1529 	struct kbase_device *kbdev,
1530 	const struct kbase_kcpu_command_queue *queue)
1531 {
1532 	KBASE_TLSTREAM_TL_KBASE_ARRAY_END_KCPUQUEUE_EXECUTE_JIT_FREE_END(
1533 		kbdev, queue);
1534 }
1535 
kcpu_queue_process(struct kbase_kcpu_command_queue * queue,bool drain_queue)1536 static void kcpu_queue_process(struct kbase_kcpu_command_queue *queue,
1537 			       bool drain_queue)
1538 {
1539 	struct kbase_device *kbdev = queue->kctx->kbdev;
1540 	bool process_next = true;
1541 	size_t i;
1542 
1543 	lockdep_assert_held(&queue->kctx->csf.kcpu_queues.lock);
1544 
1545 	for (i = 0; i != queue->num_pending_cmds; ++i) {
1546 		struct kbase_kcpu_command *cmd =
1547 			&queue->commands[(u8)(queue->start_offset + i)];
1548 		int status;
1549 
1550 		switch (cmd->type) {
1551 		case BASE_KCPU_COMMAND_TYPE_FENCE_WAIT:
1552 			if (!queue->command_started) {
1553 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_FENCE_WAIT_START(
1554 					kbdev, queue);
1555 				queue->command_started = true;
1556 			}
1557 
1558 			status = 0;
1559 #if IS_ENABLED(CONFIG_SYNC_FILE)
1560 			if (drain_queue) {
1561 				kbase_kcpu_fence_wait_cancel(queue,
1562 					&cmd->info.fence);
1563 			} else {
1564 				status = kbase_kcpu_fence_wait_process(queue,
1565 					&cmd->info.fence);
1566 
1567 				if (status == 0)
1568 					process_next = false;
1569 				else if (status < 0)
1570 					queue->has_error = true;
1571 			}
1572 #else
1573 			dev_warn(kbdev->dev,
1574 				"unexpected fence wait command found\n");
1575 
1576 			status = -EINVAL;
1577 			queue->has_error = true;
1578 #endif
1579 
1580 			if (process_next) {
1581 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_FENCE_WAIT_END(
1582 					kbdev, queue, status < 0 ? status : 0);
1583 				queue->command_started = false;
1584 			}
1585 			break;
1586 		case BASE_KCPU_COMMAND_TYPE_FENCE_SIGNAL:
1587 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_FENCE_SIGNAL_START(
1588 				kbdev, queue);
1589 
1590 			status = 0;
1591 
1592 #if IS_ENABLED(CONFIG_SYNC_FILE)
1593 			status = kbase_kcpu_fence_signal_process(
1594 				queue, &cmd->info.fence);
1595 
1596 			if (status < 0)
1597 				queue->has_error = true;
1598 #else
1599 			dev_warn(kbdev->dev,
1600 				"unexpected fence signal command found\n");
1601 
1602 			status = -EINVAL;
1603 			queue->has_error = true;
1604 #endif
1605 
1606 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_FENCE_SIGNAL_END(
1607 				kbdev, queue, status);
1608 			break;
1609 		case BASE_KCPU_COMMAND_TYPE_CQS_WAIT:
1610 			status = kbase_kcpu_cqs_wait_process(kbdev, queue,
1611 						&cmd->info.cqs_wait);
1612 
1613 			if (!status && !drain_queue) {
1614 				process_next = false;
1615 			} else {
1616 				/* Either all CQS objects were signaled or
1617 				 * there was an error or the queue itself is
1618 				 * being deleted.
1619 				 * In all cases can move to the next command.
1620 				 * TBD: handle the error
1621 				 */
1622 				cleanup_cqs_wait(queue,	&cmd->info.cqs_wait);
1623 			}
1624 
1625 			break;
1626 		case BASE_KCPU_COMMAND_TYPE_CQS_SET:
1627 			kbase_kcpu_cqs_set_process(kbdev, queue,
1628 				&cmd->info.cqs_set);
1629 
1630 			break;
1631 		case BASE_KCPU_COMMAND_TYPE_CQS_WAIT_OPERATION:
1632 			status = kbase_kcpu_cqs_wait_operation_process(kbdev, queue,
1633 						&cmd->info.cqs_wait_operation);
1634 
1635 			if (!status && !drain_queue) {
1636 				process_next = false;
1637 			} else {
1638 				/* Either all CQS objects were signaled or
1639 				 * there was an error or the queue itself is
1640 				 * being deleted.
1641 				 * In all cases can move to the next command.
1642 				 * TBD: handle the error
1643 				 */
1644 				cleanup_cqs_wait_operation(queue,	&cmd->info.cqs_wait_operation);
1645 			}
1646 
1647 			break;
1648 		case BASE_KCPU_COMMAND_TYPE_CQS_SET_OPERATION:
1649 			kbase_kcpu_cqs_set_operation_process(kbdev, queue,
1650 				&cmd->info.cqs_set_operation);
1651 
1652 			break;
1653 		case BASE_KCPU_COMMAND_TYPE_ERROR_BARRIER:
1654 			/* Clear the queue's error state */
1655 			queue->has_error = false;
1656 
1657 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_ERROR_BARRIER(
1658 				kbdev, queue);
1659 			break;
1660 		case BASE_KCPU_COMMAND_TYPE_MAP_IMPORT: {
1661 			struct kbase_ctx_ext_res_meta *meta = NULL;
1662 
1663 			if (!drain_queue) {
1664 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_MAP_IMPORT_START(
1665 					kbdev, queue);
1666 
1667 				kbase_gpu_vm_lock(queue->kctx);
1668 				meta = kbase_sticky_resource_acquire(
1669 					queue->kctx, cmd->info.import.gpu_va);
1670 				kbase_gpu_vm_unlock(queue->kctx);
1671 
1672 				if (meta == NULL) {
1673 					queue->has_error = true;
1674 					dev_dbg(
1675 						kbdev->dev,
1676 						"failed to map an external resource");
1677 				}
1678 
1679 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_MAP_IMPORT_END(
1680 					kbdev, queue, meta ? 0 : 1);
1681 			}
1682 			break;
1683 		}
1684 		case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT: {
1685 			bool ret;
1686 
1687 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_UNMAP_IMPORT_START(
1688 				kbdev, queue);
1689 
1690 			kbase_gpu_vm_lock(queue->kctx);
1691 			ret = kbase_sticky_resource_release(
1692 				queue->kctx, NULL, cmd->info.import.gpu_va);
1693 			kbase_gpu_vm_unlock(queue->kctx);
1694 
1695 			if (!ret) {
1696 				queue->has_error = true;
1697 				dev_dbg(kbdev->dev,
1698 						"failed to release the reference. resource not found");
1699 			}
1700 
1701 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_UNMAP_IMPORT_END(
1702 				kbdev, queue, ret ? 0 : 1);
1703 			break;
1704 		}
1705 		case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT_FORCE: {
1706 			bool ret;
1707 
1708 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_UNMAP_IMPORT_FORCE_START(
1709 					kbdev, queue);
1710 
1711 			kbase_gpu_vm_lock(queue->kctx);
1712 			ret = kbase_sticky_resource_release_force(
1713 				queue->kctx, NULL, cmd->info.import.gpu_va);
1714 			kbase_gpu_vm_unlock(queue->kctx);
1715 
1716 			if (!ret) {
1717 				queue->has_error = true;
1718 				dev_dbg(kbdev->dev,
1719 						"failed to release the reference. resource not found");
1720 			}
1721 
1722 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_UNMAP_IMPORT_FORCE_END(
1723 				kbdev, queue, ret ? 0 : 1);
1724 			break;
1725 		}
1726 		case BASE_KCPU_COMMAND_TYPE_JIT_ALLOC:
1727 		{
1728 			if (drain_queue) {
1729 				/* We still need to call this function to clean the JIT alloc info up */
1730 				kbase_kcpu_jit_allocate_finish(queue, cmd);
1731 			} else {
1732 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_START(
1733 					kbdev, queue);
1734 
1735 				status = kbase_kcpu_jit_allocate_process(queue,
1736 									 cmd);
1737 				if (status == -EAGAIN) {
1738 					process_next = false;
1739 				} else {
1740 					if (status != 0)
1741 						queue->has_error = true;
1742 
1743 					KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_INFO(
1744 						kbdev, queue,
1745 						&cmd->info.jit_alloc, status);
1746 
1747 					kbase_kcpu_jit_allocate_finish(queue,
1748 								       cmd);
1749 					KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_ALLOC_END(
1750 						kbdev, queue);
1751 				}
1752 			}
1753 
1754 			break;
1755 		}
1756 		case BASE_KCPU_COMMAND_TYPE_JIT_FREE:
1757 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_FREE_START(
1758 				kbdev, queue);
1759 
1760 			status = kbase_kcpu_jit_free_process(queue, cmd);
1761 			if (status)
1762 				queue->has_error = true;
1763 
1764 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_JIT_FREE_END(
1765 				kbdev, queue);
1766 			break;
1767 		case BASE_KCPU_COMMAND_TYPE_GROUP_SUSPEND: {
1768 			struct kbase_suspend_copy_buffer *sus_buf =
1769 					cmd->info.suspend_buf_copy.sus_buf;
1770 
1771 			if (!drain_queue) {
1772 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_GROUP_SUSPEND_START(
1773 					kbdev, queue);
1774 
1775 				status = kbase_csf_queue_group_suspend_process(
1776 					queue->kctx, sus_buf,
1777 					cmd->info.suspend_buf_copy.group_handle);
1778 				if (status)
1779 					queue->has_error = true;
1780 
1781 				KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_EXECUTE_GROUP_SUSPEND_END(
1782 					kbdev, queue, status);
1783 
1784 				if (!sus_buf->cpu_alloc) {
1785 					int i;
1786 
1787 					for (i = 0; i < sus_buf->nr_pages; i++)
1788 						put_page(sus_buf->pages[i]);
1789 				} else {
1790 					kbase_mem_phy_alloc_kernel_unmapped(
1791 						sus_buf->cpu_alloc);
1792 					kbase_mem_phy_alloc_put(
1793 						sus_buf->cpu_alloc);
1794 				}
1795 			}
1796 
1797 			kfree(sus_buf->pages);
1798 			kfree(sus_buf);
1799 			break;
1800 		}
1801 		default:
1802 			dev_dbg(kbdev->dev,
1803 				"Unrecognized command type");
1804 			break;
1805 		} /* switch */
1806 
1807 		/*TBD: error handling */
1808 
1809 		if (!process_next)
1810 			break;
1811 	}
1812 
1813 	if (i > 0) {
1814 		queue->start_offset += i;
1815 		queue->num_pending_cmds -= i;
1816 
1817 		/* If an attempt to enqueue commands failed then we must raise
1818 		 * an event in case the client wants to retry now that there is
1819 		 * free space in the buffer.
1820 		 */
1821 		if (queue->enqueue_failed) {
1822 			queue->enqueue_failed = false;
1823 			kbase_csf_event_signal_cpu_only(queue->kctx);
1824 		}
1825 	}
1826 }
1827 
kcpu_queue_get_space(struct kbase_kcpu_command_queue * queue)1828 static size_t kcpu_queue_get_space(struct kbase_kcpu_command_queue *queue)
1829 {
1830 	return KBASEP_KCPU_QUEUE_SIZE - queue->num_pending_cmds;
1831 }
1832 
KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_COMMAND(const struct kbase_kcpu_command_queue * queue,const struct kbase_kcpu_command * cmd)1833 static void KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_COMMAND(
1834 	const struct kbase_kcpu_command_queue *queue,
1835 	const struct kbase_kcpu_command *cmd)
1836 {
1837 	struct kbase_device *kbdev = queue->kctx->kbdev;
1838 
1839 	switch (cmd->type) {
1840 	case BASE_KCPU_COMMAND_TYPE_FENCE_WAIT:
1841 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_FENCE_WAIT(
1842 			kbdev, queue, cmd->info.fence.fence);
1843 		break;
1844 	case BASE_KCPU_COMMAND_TYPE_FENCE_SIGNAL:
1845 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_FENCE_SIGNAL(
1846 			kbdev, queue, cmd->info.fence.fence);
1847 		break;
1848 	case BASE_KCPU_COMMAND_TYPE_CQS_WAIT:
1849 	{
1850 		const struct base_cqs_wait_info *waits =
1851 			cmd->info.cqs_wait.objs;
1852 		u32 inherit_err_flags = cmd->info.cqs_wait.inherit_err_flags;
1853 		unsigned int i;
1854 
1855 		for (i = 0; i < cmd->info.cqs_wait.nr_objs; i++) {
1856 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_CQS_WAIT(
1857 				kbdev, queue, waits[i].addr, waits[i].val,
1858 				(inherit_err_flags & ((u32)1 << i)) ? 1 : 0);
1859 		}
1860 		break;
1861 	}
1862 	case BASE_KCPU_COMMAND_TYPE_CQS_SET:
1863 	{
1864 		const struct base_cqs_set *sets = cmd->info.cqs_set.objs;
1865 		unsigned int i;
1866 
1867 		for (i = 0; i < cmd->info.cqs_set.nr_objs; i++) {
1868 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_CQS_SET(
1869 				kbdev, queue, sets[i].addr);
1870 		}
1871 		break;
1872 	}
1873 	case BASE_KCPU_COMMAND_TYPE_CQS_WAIT_OPERATION:
1874 	{
1875 		/* GPUCORE-28172 RDT to review */
1876 		break;
1877 	}
1878 	case BASE_KCPU_COMMAND_TYPE_CQS_SET_OPERATION:
1879 	{
1880 		/* GPUCORE-28172 RDT to review */
1881 		break;
1882 	}
1883 	case BASE_KCPU_COMMAND_TYPE_ERROR_BARRIER:
1884 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_ERROR_BARRIER(kbdev,
1885 									queue);
1886 		break;
1887 	case BASE_KCPU_COMMAND_TYPE_MAP_IMPORT:
1888 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_MAP_IMPORT(
1889 			kbdev, queue, cmd->info.import.gpu_va);
1890 		break;
1891 	case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT:
1892 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_UNMAP_IMPORT(
1893 			kbdev, queue, cmd->info.import.gpu_va);
1894 		break;
1895 	case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT_FORCE:
1896 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_UNMAP_IMPORT_FORCE(
1897 			kbdev, queue, cmd->info.import.gpu_va);
1898 		break;
1899 	case BASE_KCPU_COMMAND_TYPE_JIT_ALLOC:
1900 	{
1901 		u8 i;
1902 
1903 		KBASE_TLSTREAM_TL_KBASE_ARRAY_BEGIN_KCPUQUEUE_ENQUEUE_JIT_ALLOC(
1904 			kbdev, queue);
1905 		for (i = 0; i < cmd->info.jit_alloc.count; i++) {
1906 			const struct base_jit_alloc_info *info =
1907 				&cmd->info.jit_alloc.info[i];
1908 
1909 			KBASE_TLSTREAM_TL_KBASE_ARRAY_ITEM_KCPUQUEUE_ENQUEUE_JIT_ALLOC(
1910 				kbdev, queue, info->gpu_alloc_addr,
1911 				info->va_pages, info->commit_pages,
1912 				info->extension, info->id, info->bin_id,
1913 				info->max_allocations, info->flags,
1914 				info->usage_id);
1915 		}
1916 		KBASE_TLSTREAM_TL_KBASE_ARRAY_END_KCPUQUEUE_ENQUEUE_JIT_ALLOC(
1917 			kbdev, queue);
1918 		break;
1919 	}
1920 	case BASE_KCPU_COMMAND_TYPE_JIT_FREE:
1921 	{
1922 		u8 i;
1923 
1924 		KBASE_TLSTREAM_TL_KBASE_ARRAY_BEGIN_KCPUQUEUE_ENQUEUE_JIT_FREE(
1925 			kbdev, queue);
1926 		for (i = 0; i < cmd->info.jit_free.count; i++) {
1927 			KBASE_TLSTREAM_TL_KBASE_ARRAY_ITEM_KCPUQUEUE_ENQUEUE_JIT_FREE(
1928 				kbdev, queue, cmd->info.jit_free.ids[i]);
1929 		}
1930 		KBASE_TLSTREAM_TL_KBASE_ARRAY_END_KCPUQUEUE_ENQUEUE_JIT_FREE(
1931 			kbdev, queue);
1932 		break;
1933 	}
1934 	case BASE_KCPU_COMMAND_TYPE_GROUP_SUSPEND:
1935 		KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_GROUP_SUSPEND(
1936 			kbdev, queue, cmd->info.suspend_buf_copy.sus_buf,
1937 			cmd->info.suspend_buf_copy.group_handle);
1938 		break;
1939 	}
1940 }
1941 
kbase_csf_kcpu_queue_enqueue(struct kbase_context * kctx,struct kbase_ioctl_kcpu_queue_enqueue * enq)1942 int kbase_csf_kcpu_queue_enqueue(struct kbase_context *kctx,
1943 			struct kbase_ioctl_kcpu_queue_enqueue *enq)
1944 {
1945 	struct kbase_kcpu_command_queue *queue = NULL;
1946 	void __user *user_cmds = u64_to_user_ptr(enq->addr);
1947 	int ret = 0;
1948 	u32 i;
1949 
1950 	/* The offset to the first command that is being processed or yet to
1951 	 * be processed is of u8 type, so the number of commands inside the
1952 	 * queue cannot be more than 256.
1953 	 */
1954 	BUILD_BUG_ON(KBASEP_KCPU_QUEUE_SIZE > 256);
1955 
1956 	/* Whilst the backend interface allows enqueueing multiple commands in
1957 	 * a single operation, the Base interface does not expose any mechanism
1958 	 * to do so. And also right now the handling is missing for the case
1959 	 * where multiple commands are submitted and the enqueue of one of the
1960 	 * command in the set fails after successfully enqueuing other commands
1961 	 * in the set.
1962 	 */
1963 	if (enq->nr_commands != 1) {
1964 		dev_dbg(kctx->kbdev->dev,
1965 			"More than one commands enqueued");
1966 		return -EINVAL;
1967 	}
1968 
1969 	mutex_lock(&kctx->csf.kcpu_queues.lock);
1970 
1971 	if (!kctx->csf.kcpu_queues.array[enq->id]) {
1972 		ret = -EINVAL;
1973 		goto out;
1974 	}
1975 
1976 	queue = kctx->csf.kcpu_queues.array[enq->id];
1977 
1978 	if (kcpu_queue_get_space(queue) < enq->nr_commands) {
1979 		ret = -EBUSY;
1980 		queue->enqueue_failed = true;
1981 		goto out;
1982 	}
1983 
1984 	/* Copy all command's info to the command buffer.
1985 	 * Note: it would be more efficient to process all commands in-line
1986 	 * until we encounter an unresolved CQS_ / FENCE_WAIT, however, the
1987 	 * interface allows multiple commands to be enqueued so we must account
1988 	 * for the possibility to roll back.
1989 	 */
1990 
1991 	for (i = 0; (i != enq->nr_commands) && !ret; ++i, ++kctx->csf.kcpu_queues.num_cmds) {
1992 		struct kbase_kcpu_command *kcpu_cmd =
1993 			&queue->commands[(u8)(queue->start_offset + queue->num_pending_cmds + i)];
1994 		struct base_kcpu_command command;
1995 		unsigned int j;
1996 
1997 		if (copy_from_user(&command, user_cmds, sizeof(command))) {
1998 			ret = -EFAULT;
1999 			goto out;
2000 		}
2001 
2002 		user_cmds = (void __user *)((uintptr_t)user_cmds +
2003 				sizeof(struct base_kcpu_command));
2004 
2005 		for (j = 0; j < sizeof(command.padding); j++) {
2006 			if (command.padding[j] != 0) {
2007 				dev_dbg(kctx->kbdev->dev,
2008 					"base_kcpu_command padding not 0\n");
2009 				ret = -EINVAL;
2010 				goto out;
2011 			}
2012 		}
2013 
2014 		kcpu_cmd->enqueue_ts = kctx->csf.kcpu_queues.num_cmds;
2015 		switch (command.type) {
2016 		case BASE_KCPU_COMMAND_TYPE_FENCE_WAIT:
2017 #if IS_ENABLED(CONFIG_SYNC_FILE)
2018 			ret = kbase_kcpu_fence_wait_prepare(queue,
2019 						&command.info.fence, kcpu_cmd);
2020 #else
2021 			ret = -EINVAL;
2022 			dev_warn(kctx->kbdev->dev, "fence wait command unsupported\n");
2023 #endif
2024 			break;
2025 		case BASE_KCPU_COMMAND_TYPE_FENCE_SIGNAL:
2026 #if IS_ENABLED(CONFIG_SYNC_FILE)
2027 			ret = kbase_kcpu_fence_signal_prepare(queue,
2028 						&command.info.fence, kcpu_cmd);
2029 #else
2030 			ret = -EINVAL;
2031 			dev_warn(kctx->kbdev->dev, "fence signal command unsupported\n");
2032 #endif
2033 			break;
2034 		case BASE_KCPU_COMMAND_TYPE_CQS_WAIT:
2035 			ret = kbase_kcpu_cqs_wait_prepare(queue,
2036 					&command.info.cqs_wait, kcpu_cmd);
2037 			break;
2038 		case BASE_KCPU_COMMAND_TYPE_CQS_SET:
2039 			ret = kbase_kcpu_cqs_set_prepare(queue,
2040 					&command.info.cqs_set, kcpu_cmd);
2041 			break;
2042 		case BASE_KCPU_COMMAND_TYPE_CQS_WAIT_OPERATION:
2043 			ret = kbase_kcpu_cqs_wait_operation_prepare(queue,
2044 					&command.info.cqs_wait_operation, kcpu_cmd);
2045 			break;
2046 		case BASE_KCPU_COMMAND_TYPE_CQS_SET_OPERATION:
2047 			ret = kbase_kcpu_cqs_set_operation_prepare(queue,
2048 					&command.info.cqs_set_operation, kcpu_cmd);
2049 			break;
2050 		case BASE_KCPU_COMMAND_TYPE_ERROR_BARRIER:
2051 			kcpu_cmd->type = BASE_KCPU_COMMAND_TYPE_ERROR_BARRIER;
2052 			ret = 0;
2053 			break;
2054 		case BASE_KCPU_COMMAND_TYPE_MAP_IMPORT:
2055 			ret = kbase_kcpu_map_import_prepare(queue,
2056 					&command.info.import, kcpu_cmd);
2057 			break;
2058 		case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT:
2059 			ret = kbase_kcpu_unmap_import_prepare(queue,
2060 					&command.info.import, kcpu_cmd);
2061 			break;
2062 		case BASE_KCPU_COMMAND_TYPE_UNMAP_IMPORT_FORCE:
2063 			ret = kbase_kcpu_unmap_import_force_prepare(queue,
2064 					&command.info.import, kcpu_cmd);
2065 			break;
2066 		case BASE_KCPU_COMMAND_TYPE_JIT_ALLOC:
2067 			ret = kbase_kcpu_jit_allocate_prepare(queue,
2068 					&command.info.jit_alloc, kcpu_cmd);
2069 			break;
2070 		case BASE_KCPU_COMMAND_TYPE_JIT_FREE:
2071 			ret = kbase_kcpu_jit_free_prepare(queue,
2072 					&command.info.jit_free, kcpu_cmd);
2073 			break;
2074 		case BASE_KCPU_COMMAND_TYPE_GROUP_SUSPEND:
2075 			ret = kbase_csf_queue_group_suspend_prepare(queue,
2076 					&command.info.suspend_buf_copy,
2077 					kcpu_cmd);
2078 			break;
2079 		default:
2080 			dev_dbg(queue->kctx->kbdev->dev,
2081 				"Unknown command type %u", command.type);
2082 			ret = -EINVAL;
2083 			break;
2084 		}
2085 	}
2086 
2087 	if (!ret) {
2088 		/* We only instrument the enqueues after all commands have been
2089 		 * successfully enqueued, as if we do them during the enqueue
2090 		 * and there is an error, we won't be able to roll them back
2091 		 * like is done for the command enqueues themselves.
2092 		 */
2093 		for (i = 0; i != enq->nr_commands; ++i) {
2094 			u8 cmd_idx = (u8)(queue->start_offset + queue->num_pending_cmds + i);
2095 
2096 			KBASE_TLSTREAM_TL_KBASE_KCPUQUEUE_ENQUEUE_COMMAND(
2097 				queue, &queue->commands[cmd_idx]);
2098 		}
2099 
2100 		queue->num_pending_cmds += enq->nr_commands;
2101 		kcpu_queue_process(queue, false);
2102 	} else {
2103 		/* Roll back the number of enqueued commands */
2104 		kctx->csf.kcpu_queues.num_cmds -= i;
2105 	}
2106 
2107 out:
2108 	mutex_unlock(&kctx->csf.kcpu_queues.lock);
2109 
2110 	return ret;
2111 }
2112 
kbase_csf_kcpu_queue_context_init(struct kbase_context * kctx)2113 int kbase_csf_kcpu_queue_context_init(struct kbase_context *kctx)
2114 {
2115 	int idx;
2116 
2117 	bitmap_zero(kctx->csf.kcpu_queues.in_use, KBASEP_MAX_KCPU_QUEUES);
2118 
2119 	for (idx = 0; idx < KBASEP_MAX_KCPU_QUEUES; ++idx)
2120 		kctx->csf.kcpu_queues.array[idx] = NULL;
2121 
2122 	kctx->csf.kcpu_queues.wq = alloc_workqueue("mali_kbase_csf_kcpu",
2123 					WQ_UNBOUND | WQ_HIGHPRI, 0);
2124 	if (!kctx->csf.kcpu_queues.wq)
2125 		return -ENOMEM;
2126 
2127 	mutex_init(&kctx->csf.kcpu_queues.lock);
2128 
2129 	kctx->csf.kcpu_queues.num_cmds = 0;
2130 
2131 	return 0;
2132 }
2133 
kbase_csf_kcpu_queue_context_term(struct kbase_context * kctx)2134 void kbase_csf_kcpu_queue_context_term(struct kbase_context *kctx)
2135 {
2136 	while (!bitmap_empty(kctx->csf.kcpu_queues.in_use,
2137 			KBASEP_MAX_KCPU_QUEUES)) {
2138 		int id = find_first_bit(kctx->csf.kcpu_queues.in_use,
2139 				KBASEP_MAX_KCPU_QUEUES);
2140 
2141 		if (WARN_ON(!kctx->csf.kcpu_queues.array[id]))
2142 			clear_bit(id, kctx->csf.kcpu_queues.in_use);
2143 		else
2144 			(void)delete_queue(kctx, id);
2145 	}
2146 
2147 	destroy_workqueue(kctx->csf.kcpu_queues.wq);
2148 	mutex_destroy(&kctx->csf.kcpu_queues.lock);
2149 }
2150 
kbase_csf_kcpu_queue_delete(struct kbase_context * kctx,struct kbase_ioctl_kcpu_queue_delete * del)2151 int kbase_csf_kcpu_queue_delete(struct kbase_context *kctx,
2152 			struct kbase_ioctl_kcpu_queue_delete *del)
2153 {
2154 	return delete_queue(kctx, (u32)del->id);
2155 }
2156 
kbase_csf_kcpu_queue_new(struct kbase_context * kctx,struct kbase_ioctl_kcpu_queue_new * newq)2157 int kbase_csf_kcpu_queue_new(struct kbase_context *kctx,
2158 			struct kbase_ioctl_kcpu_queue_new *newq)
2159 {
2160 	struct kbase_kcpu_command_queue *queue;
2161 	int idx;
2162 	int ret = 0;
2163 
2164 	/* The queue id is of u8 type and we use the index of the kcpu_queues
2165 	 * array as an id, so the number of elements in the array can't be
2166 	 * more than 256.
2167 	 */
2168 	BUILD_BUG_ON(KBASEP_MAX_KCPU_QUEUES > 256);
2169 
2170 	mutex_lock(&kctx->csf.kcpu_queues.lock);
2171 
2172 	idx = find_first_zero_bit(kctx->csf.kcpu_queues.in_use,
2173 			KBASEP_MAX_KCPU_QUEUES);
2174 	if (idx >= (int)KBASEP_MAX_KCPU_QUEUES) {
2175 		ret = -ENOMEM;
2176 		goto out;
2177 	}
2178 
2179 	if (WARN_ON(kctx->csf.kcpu_queues.array[idx])) {
2180 		ret = -EINVAL;
2181 		goto out;
2182 	}
2183 
2184 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
2185 
2186 	if (!queue) {
2187 		ret = -ENOMEM;
2188 		goto out;
2189 	}
2190 
2191 	bitmap_set(kctx->csf.kcpu_queues.in_use, idx, 1);
2192 	kctx->csf.kcpu_queues.array[idx] = queue;
2193 	queue->kctx = kctx;
2194 	queue->start_offset = 0;
2195 	queue->num_pending_cmds = 0;
2196 #if IS_ENABLED(CONFIG_SYNC_FILE)
2197 	queue->fence_context = dma_fence_context_alloc(1);
2198 	queue->fence_seqno = 0;
2199 	queue->fence_wait_processed = false;
2200 #endif
2201 	queue->enqueue_failed = false;
2202 	queue->command_started = false;
2203 	INIT_LIST_HEAD(&queue->jit_blocked);
2204 	queue->has_error = false;
2205 	INIT_WORK(&queue->work, kcpu_queue_process_worker);
2206 	queue->id = idx;
2207 
2208 	newq->id = idx;
2209 
2210 	/* Fire the tracepoint with the mutex held to enforce correct ordering
2211 	 * with the summary stream.
2212 	 */
2213 	KBASE_TLSTREAM_TL_KBASE_NEW_KCPUQUEUE(
2214 		kctx->kbdev, queue, kctx->id, queue->num_pending_cmds);
2215 
2216 	KBASE_KTRACE_ADD_CSF_KCPU(kctx->kbdev, KCPU_QUEUE_NEW, queue,
2217 		queue->fence_context, 0);
2218 out:
2219 	mutex_unlock(&kctx->csf.kcpu_queues.lock);
2220 
2221 	return ret;
2222 }
2223