xref: /linux/drivers/gpu/drm/imagination/pvr_drv.c (revision 88b8c6ae2ccb3ef9dbb04c8e13a4d1a98c42e922)
1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
3 
4 #include "pvr_context.h"
5 #include "pvr_debugfs.h"
6 #include "pvr_device.h"
7 #include "pvr_drv.h"
8 #include "pvr_free_list.h"
9 #include "pvr_gem.h"
10 #include "pvr_hwrt.h"
11 #include "pvr_job.h"
12 #include "pvr_mmu.h"
13 #include "pvr_power.h"
14 #include "pvr_rogue_defs.h"
15 #include "pvr_rogue_fwif_client.h"
16 #include "pvr_rogue_fwif_shared.h"
17 #include "pvr_trace.h"
18 #include "pvr_vm.h"
19 
20 #include <uapi/drm/pvr_drm.h>
21 
22 #include <drm/drm_device.h>
23 #include <drm/drm_drv.h>
24 #include <drm/drm_file.h>
25 #include <drm/drm_gem.h>
26 #include <drm/drm_ioctl.h>
27 
28 #include <linux/err.h>
29 #include <linux/export.h>
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/of_device.h>
36 #include <linux/of_platform.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/xarray.h>
40 
41 /**
42  * DOC: PowerVR (Series 6 and later) and IMG Graphics Driver
43  *
44  * This driver supports the following PowerVR/IMG graphics cores from Imagination Technologies:
45  *
46  * * AXE-1-16M (found in Texas Instruments AM62)
47  * * BXS-4-64 MC1 (found in Texas Instruments J721S2/AM68)
48  */
49 
50 /**
51  * pvr_ioctl_create_bo() - IOCTL to create a GEM buffer object.
52  * @drm_dev: [IN] Target DRM device.
53  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
54  * &struct drm_pvr_ioctl_create_bo_args.
55  * @file: [IN] DRM file-private data.
56  *
57  * Called from userspace with %DRM_IOCTL_PVR_CREATE_BO.
58  *
59  * Return:
60  *  * 0 on success,
61  *  * -%EINVAL if the value of &drm_pvr_ioctl_create_bo_args.size is zero
62  *    or wider than &typedef size_t,
63  *  * -%EINVAL if any bits in &drm_pvr_ioctl_create_bo_args.flags that are
64  *    reserved or undefined are set,
65  *  * -%EINVAL if any padding fields in &drm_pvr_ioctl_create_bo_args are not
66  *    zero,
67  *  * Any error encountered while creating the object (see
68  *    pvr_gem_object_create()), or
69  *  * Any error encountered while transferring ownership of the object into a
70  *    userspace-accessible handle (see pvr_gem_object_into_handle()).
71  */
72 static int
73 pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args,
74 		    struct drm_file *file)
75 {
76 	struct drm_pvr_ioctl_create_bo_args *args = raw_args;
77 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
78 	struct pvr_file *pvr_file = to_pvr_file(file);
79 
80 	struct pvr_gem_object *pvr_obj;
81 	size_t sanitized_size;
82 
83 	int idx;
84 	int err;
85 
86 	if (!drm_dev_enter(drm_dev, &idx))
87 		return -EIO;
88 
89 	/* All padding fields must be zeroed. */
90 	if (args->_padding_c != 0) {
91 		err = -EINVAL;
92 		goto err_drm_dev_exit;
93 	}
94 
95 	/*
96 	 * On 64-bit platforms (our primary target), size_t is a u64. However,
97 	 * on other architectures we have to check for overflow when casting
98 	 * down to size_t from u64.
99 	 *
100 	 * We also disallow zero-sized allocations, and reserved (kernel-only)
101 	 * flags.
102 	 */
103 	if (args->size > SIZE_MAX || args->size == 0 || args->flags &
104 	    ~DRM_PVR_BO_FLAGS_MASK || args->size & (PVR_DEVICE_PAGE_SIZE - 1)) {
105 		err = -EINVAL;
106 		goto err_drm_dev_exit;
107 	}
108 
109 	sanitized_size = (size_t)args->size;
110 
111 	/*
112 	 * Create a buffer object and transfer ownership to a userspace-
113 	 * accessible handle.
114 	 */
115 	pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags);
116 	if (IS_ERR(pvr_obj)) {
117 		err = PTR_ERR(pvr_obj);
118 		goto err_drm_dev_exit;
119 	}
120 
121 	/* This function will not modify &args->handle unless it succeeds. */
122 	err = pvr_gem_object_into_handle(pvr_obj, pvr_file, &args->handle);
123 	if (err)
124 		goto err_destroy_obj;
125 
126 	drm_dev_exit(idx);
127 
128 	return 0;
129 
130 err_destroy_obj:
131 	/*
132 	 * GEM objects are refcounted, so there is no explicit destructor
133 	 * function. Instead, we release the singular reference we currently
134 	 * hold on the object and let GEM take care of the rest.
135 	 */
136 	pvr_gem_object_put(pvr_obj);
137 
138 err_drm_dev_exit:
139 	drm_dev_exit(idx);
140 
141 	return err;
142 }
143 
144 /**
145  * pvr_ioctl_get_bo_mmap_offset() - IOCTL to generate a "fake" offset to be
146  * used when calling mmap() from userspace to map the given GEM buffer object
147  * @drm_dev: [IN] DRM device (unused).
148  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
149  *                     &struct drm_pvr_ioctl_get_bo_mmap_offset_args.
150  * @file: [IN] DRM file private data.
151  *
152  * Called from userspace with %DRM_IOCTL_PVR_GET_BO_MMAP_OFFSET.
153  *
154  * This IOCTL does *not* perform an mmap. See the docs on
155  * &struct drm_pvr_ioctl_get_bo_mmap_offset_args for details.
156  *
157  * Return:
158  *  * 0 on success,
159  *  * -%ENOENT if the handle does not reference a valid GEM buffer object,
160  *  * -%EINVAL if any padding fields in &struct
161  *    drm_pvr_ioctl_get_bo_mmap_offset_args are not zero, or
162  *  * Any error returned by drm_gem_create_mmap_offset().
163  */
164 static int
165 pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args,
166 			     struct drm_file *file)
167 {
168 	struct drm_pvr_ioctl_get_bo_mmap_offset_args *args = raw_args;
169 	struct pvr_file *pvr_file = to_pvr_file(file);
170 	struct pvr_gem_object *pvr_obj;
171 	struct drm_gem_object *gem_obj;
172 	int idx;
173 	int ret;
174 
175 	if (!drm_dev_enter(drm_dev, &idx))
176 		return -EIO;
177 
178 	/* All padding fields must be zeroed. */
179 	if (args->_padding_4 != 0) {
180 		ret = -EINVAL;
181 		goto err_drm_dev_exit;
182 	}
183 
184 	/*
185 	 * Obtain a kernel reference to the buffer object. This reference is
186 	 * counted and must be manually dropped before returning. If a buffer
187 	 * object cannot be found for the specified handle, return -%ENOENT (No
188 	 * such file or directory).
189 	 */
190 	pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
191 	if (!pvr_obj) {
192 		ret = -ENOENT;
193 		goto err_drm_dev_exit;
194 	}
195 
196 	gem_obj = gem_from_pvr_gem(pvr_obj);
197 
198 	/*
199 	 * Allocate a fake offset which can be used in userspace calls to mmap
200 	 * on the DRM device file. If this fails, return the error code. This
201 	 * operation is idempotent.
202 	 */
203 	ret = drm_gem_create_mmap_offset(gem_obj);
204 	if (ret != 0) {
205 		/* Drop our reference to the buffer object. */
206 		drm_gem_object_put(gem_obj);
207 		goto err_drm_dev_exit;
208 	}
209 
210 	/*
211 	 * Read out the fake offset allocated by the earlier call to
212 	 * drm_gem_create_mmap_offset.
213 	 */
214 	args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
215 
216 	/* Drop our reference to the buffer object. */
217 	pvr_gem_object_put(pvr_obj);
218 
219 err_drm_dev_exit:
220 	drm_dev_exit(idx);
221 
222 	return ret;
223 }
224 
225 static __always_inline __maybe_unused u64
226 pvr_fw_version_packed(u32 major, u32 minor)
227 {
228 	return ((u64)major << 32) | minor;
229 }
230 
231 static u32
232 rogue_get_common_store_partition_space_size(struct pvr_device *pvr_dev)
233 {
234 	u32 max_partitions = 0;
235 	u32 tile_size_x = 0;
236 	u32 tile_size_y = 0;
237 
238 	PVR_FEATURE_VALUE(pvr_dev, tile_size_x, &tile_size_x);
239 	PVR_FEATURE_VALUE(pvr_dev, tile_size_y, &tile_size_y);
240 	PVR_FEATURE_VALUE(pvr_dev, max_partitions, &max_partitions);
241 
242 	if (tile_size_x == 16 && tile_size_y == 16) {
243 		u32 usc_min_output_registers_per_pix = 0;
244 
245 		PVR_FEATURE_VALUE(pvr_dev, usc_min_output_registers_per_pix,
246 				  &usc_min_output_registers_per_pix);
247 
248 		return tile_size_x * tile_size_y * max_partitions *
249 		       usc_min_output_registers_per_pix;
250 	}
251 
252 	return max_partitions * 1024;
253 }
254 
255 static u32
256 rogue_get_common_store_alloc_region_size(struct pvr_device *pvr_dev)
257 {
258 	u32 common_store_size_in_dwords = 512 * 4 * 4;
259 	u32 alloc_region_size;
260 
261 	PVR_FEATURE_VALUE(pvr_dev, common_store_size_in_dwords, &common_store_size_in_dwords);
262 
263 	alloc_region_size = common_store_size_in_dwords - (256U * 4U) -
264 			    rogue_get_common_store_partition_space_size(pvr_dev);
265 
266 	if (PVR_HAS_QUIRK(pvr_dev, 44079)) {
267 		u32 common_store_split_point = (768U * 4U * 4U);
268 
269 		return min(common_store_split_point - (256U * 4U), alloc_region_size);
270 	}
271 
272 	return alloc_region_size;
273 }
274 
275 static inline u32
276 rogue_get_num_phantoms(struct pvr_device *pvr_dev)
277 {
278 	u32 num_clusters = 1;
279 
280 	PVR_FEATURE_VALUE(pvr_dev, num_clusters, &num_clusters);
281 
282 	return ROGUE_REQ_NUM_PHANTOMS(num_clusters);
283 }
284 
285 static inline u32
286 rogue_get_max_coeffs(struct pvr_device *pvr_dev)
287 {
288 	u32 max_coeff_additional_portion = ROGUE_MAX_VERTEX_SHARED_REGISTERS;
289 	u32 pending_allocation_shared_regs = 2U * 1024U;
290 	u32 pending_allocation_coeff_regs = 0U;
291 	u32 num_phantoms = rogue_get_num_phantoms(pvr_dev);
292 	u32 tiles_in_flight = 0;
293 	u32 max_coeff_pixel_portion;
294 
295 	PVR_FEATURE_VALUE(pvr_dev, isp_max_tiles_in_flight, &tiles_in_flight);
296 	max_coeff_pixel_portion = DIV_ROUND_UP(tiles_in_flight, num_phantoms);
297 	max_coeff_pixel_portion *= ROGUE_MAX_PIXEL_SHARED_REGISTERS;
298 
299 	/*
300 	 * Compute tasks on cores with BRN48492 and without compute overlap may lock
301 	 * up without two additional lines of coeffs.
302 	 */
303 	if (PVR_HAS_QUIRK(pvr_dev, 48492) && !PVR_HAS_FEATURE(pvr_dev, compute_overlap))
304 		pending_allocation_coeff_regs = 2U * 1024U;
305 
306 	if (PVR_HAS_ENHANCEMENT(pvr_dev, 38748))
307 		pending_allocation_shared_regs = 0;
308 
309 	if (PVR_HAS_ENHANCEMENT(pvr_dev, 38020))
310 		max_coeff_additional_portion += ROGUE_MAX_COMPUTE_SHARED_REGISTERS;
311 
312 	return rogue_get_common_store_alloc_region_size(pvr_dev) + pending_allocation_coeff_regs -
313 		(max_coeff_pixel_portion + max_coeff_additional_portion +
314 		 pending_allocation_shared_regs);
315 }
316 
317 static inline u32
318 rogue_get_cdm_max_local_mem_size_regs(struct pvr_device *pvr_dev)
319 {
320 	u32 available_coeffs_in_dwords = rogue_get_max_coeffs(pvr_dev);
321 
322 	if (PVR_HAS_QUIRK(pvr_dev, 48492) && PVR_HAS_FEATURE(pvr_dev, roguexe) &&
323 	    !PVR_HAS_FEATURE(pvr_dev, compute_overlap)) {
324 		/* Driver must not use the 2 reserved lines. */
325 		available_coeffs_in_dwords -= ROGUE_CSRM_LINE_SIZE_IN_DWORDS * 2;
326 	}
327 
328 	/*
329 	 * The maximum amount of local memory available to a kernel is the minimum
330 	 * of the total number of coefficient registers available and the max common
331 	 * store allocation size which can be made by the CDM.
332 	 *
333 	 * If any coeff lines are reserved for tessellation or pixel then we need to
334 	 * subtract those too.
335 	 */
336 	return min(available_coeffs_in_dwords, (u32)ROGUE_MAX_PER_KERNEL_LOCAL_MEM_SIZE_REGS);
337 }
338 
339 /**
340  * pvr_dev_query_gpu_info_get()
341  * @pvr_dev: Device pointer.
342  * @args: [IN] Device query arguments containing a pointer to a userspace
343  *        struct drm_pvr_dev_query_gpu_info.
344  *
345  * If the query object pointer is NULL, the size field is updated with the
346  * expected size of the query object.
347  *
348  * Returns:
349  *  * 0 on success, or if size is requested using a NULL pointer, or
350  *  * -%E2BIG if the indicated length of the allocation is less than is
351  *    required to contain the copied data, or
352  *  * -%EFAULT if local memory could not be copied to userspace.
353  */
354 static int
355 pvr_dev_query_gpu_info_get(struct pvr_device *pvr_dev,
356 			   struct drm_pvr_ioctl_dev_query_args *args)
357 {
358 	struct drm_pvr_dev_query_gpu_info gpu_info = {0};
359 	int err;
360 
361 	if (!args->pointer) {
362 		args->size = sizeof(struct drm_pvr_dev_query_gpu_info);
363 		return 0;
364 	}
365 
366 	gpu_info.gpu_id =
367 		pvr_gpu_id_to_packed_bvnc(&pvr_dev->gpu_id);
368 	gpu_info.num_phantoms = rogue_get_num_phantoms(pvr_dev);
369 
370 	err = PVR_UOBJ_SET(args->pointer, args->size, gpu_info);
371 	if (err < 0)
372 		return err;
373 
374 	if (args->size > sizeof(gpu_info))
375 		args->size = sizeof(gpu_info);
376 	return 0;
377 }
378 
379 /**
380  * pvr_dev_query_runtime_info_get()
381  * @pvr_dev: Device pointer.
382  * @args: [IN] Device query arguments containing a pointer to a userspace
383  *        struct drm_pvr_dev_query_runtime_info.
384  *
385  * If the query object pointer is NULL, the size field is updated with the
386  * expected size of the query object.
387  *
388  * Returns:
389  *  * 0 on success, or if size is requested using a NULL pointer, or
390  *  * -%E2BIG if the indicated length of the allocation is less than is
391  *    required to contain the copied data, or
392  *  * -%EFAULT if local memory could not be copied to userspace.
393  */
394 static int
395 pvr_dev_query_runtime_info_get(struct pvr_device *pvr_dev,
396 			       struct drm_pvr_ioctl_dev_query_args *args)
397 {
398 	struct drm_pvr_dev_query_runtime_info runtime_info = {0};
399 	int err;
400 
401 	if (!args->pointer) {
402 		args->size = sizeof(struct drm_pvr_dev_query_runtime_info);
403 		return 0;
404 	}
405 
406 	runtime_info.free_list_min_pages =
407 		pvr_get_free_list_min_pages(pvr_dev);
408 	runtime_info.free_list_max_pages =
409 		ROGUE_PM_MAX_FREELIST_SIZE / ROGUE_PM_PAGE_SIZE;
410 	runtime_info.common_store_alloc_region_size =
411 		rogue_get_common_store_alloc_region_size(pvr_dev);
412 	runtime_info.common_store_partition_space_size =
413 		rogue_get_common_store_partition_space_size(pvr_dev);
414 	runtime_info.max_coeffs = rogue_get_max_coeffs(pvr_dev);
415 	runtime_info.cdm_max_local_mem_size_regs =
416 		rogue_get_cdm_max_local_mem_size_regs(pvr_dev);
417 
418 	err = PVR_UOBJ_SET(args->pointer, args->size, runtime_info);
419 	if (err < 0)
420 		return err;
421 
422 	if (args->size > sizeof(runtime_info))
423 		args->size = sizeof(runtime_info);
424 	return 0;
425 }
426 
427 /**
428  * pvr_dev_query_quirks_get() - Unpack array of quirks at the address given
429  * in a struct drm_pvr_dev_query_quirks, or gets the amount of space required
430  * for it.
431  * @pvr_dev: Device pointer.
432  * @args: [IN] Device query arguments containing a pointer to a userspace
433  *        struct drm_pvr_dev_query_query_quirks.
434  *
435  * If the query object pointer is NULL, the size field is updated with the
436  * expected size of the query object.
437  * If the userspace pointer in the query object is NULL, or the count is
438  * short, no data is copied.
439  * The count field will be updated to that copied, or if either pointer is
440  * NULL, that which would have been copied.
441  * The size field in the query object will be updated to the size copied.
442  *
443  * Returns:
444  *  * 0 on success, or if size/count is requested using a NULL pointer, or
445  *  * -%EINVAL if args contained non-zero reserved fields, or
446  *  * -%E2BIG if the indicated length of the allocation is less than is
447  *    required to contain the copied data, or
448  *  * -%EFAULT if local memory could not be copied to userspace.
449  */
450 static int
451 pvr_dev_query_quirks_get(struct pvr_device *pvr_dev,
452 			 struct drm_pvr_ioctl_dev_query_args *args)
453 {
454 	/*
455 	 * @FIXME - hardcoding of numbers here is intended as an
456 	 * intermediate step so the UAPI can be fixed, but requires a
457 	 * a refactor in the future to store them in a more appropriate
458 	 * location
459 	 */
460 	static const u32 umd_quirks_musthave[] = {
461 		47217,
462 		49927,
463 		62269,
464 	};
465 	static const u32 umd_quirks[] = {
466 		48545,
467 		51764,
468 	};
469 	struct drm_pvr_dev_query_quirks query;
470 	u32 out[ARRAY_SIZE(umd_quirks_musthave) + ARRAY_SIZE(umd_quirks)];
471 	size_t out_musthave_count = 0;
472 	size_t out_count = 0;
473 	int err;
474 
475 	if (!args->pointer) {
476 		args->size = sizeof(struct drm_pvr_dev_query_quirks);
477 		return 0;
478 	}
479 
480 	err = PVR_UOBJ_GET(query, args->size, args->pointer);
481 
482 	if (err < 0)
483 		return err;
484 	if (query._padding_c)
485 		return -EINVAL;
486 
487 	for (int i = 0; i < ARRAY_SIZE(umd_quirks_musthave); i++) {
488 		if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks_musthave[i])) {
489 			out[out_count++] = umd_quirks_musthave[i];
490 			out_musthave_count++;
491 		}
492 	}
493 
494 	for (int i = 0; i < ARRAY_SIZE(umd_quirks); i++) {
495 		if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks[i]))
496 			out[out_count++] = umd_quirks[i];
497 	}
498 
499 	if (!query.quirks)
500 		goto copy_out;
501 	if (query.count < out_count)
502 		return -E2BIG;
503 
504 	if (copy_to_user(u64_to_user_ptr(query.quirks), out,
505 			 out_count * sizeof(u32))) {
506 		return -EFAULT;
507 	}
508 
509 	query.musthave_count = out_musthave_count;
510 
511 copy_out:
512 	query.count = out_count;
513 	err = PVR_UOBJ_SET(args->pointer, args->size, query);
514 	if (err < 0)
515 		return err;
516 
517 	if (args->size > sizeof(query))
518 		args->size = sizeof(query);
519 	return 0;
520 }
521 
522 /**
523  * pvr_dev_query_enhancements_get() - Unpack array of enhancements at the
524  * address given in a struct drm_pvr_dev_query_enhancements, or gets the amount
525  * of space required for it.
526  * @pvr_dev: Device pointer.
527  * @args: [IN] Device query arguments containing a pointer to a userspace
528  *        struct drm_pvr_dev_query_enhancements.
529  *
530  * If the query object pointer is NULL, the size field is updated with the
531  * expected size of the query object.
532  * If the userspace pointer in the query object is NULL, or the count is
533  * short, no data is copied.
534  * The count field will be updated to that copied, or if either pointer is
535  * NULL, that which would have been copied.
536  * The size field in the query object will be updated to the size copied.
537  *
538  * Returns:
539  *  * 0 on success, or if size/count is requested using a NULL pointer, or
540  *  * -%EINVAL if args contained non-zero reserved fields, or
541  *  * -%E2BIG if the indicated length of the allocation is less than is
542  *    required to contain the copied data, or
543  *  * -%EFAULT if local memory could not be copied to userspace.
544  */
545 static int
546 pvr_dev_query_enhancements_get(struct pvr_device *pvr_dev,
547 			       struct drm_pvr_ioctl_dev_query_args *args)
548 {
549 	/*
550 	 * @FIXME - hardcoding of numbers here is intended as an
551 	 * intermediate step so the UAPI can be fixed, but requires a
552 	 * a refactor in the future to store them in a more appropriate
553 	 * location
554 	 */
555 	const u32 umd_enhancements[] = {
556 		35421,
557 		42064,
558 	};
559 	struct drm_pvr_dev_query_enhancements query;
560 	u32 out[ARRAY_SIZE(umd_enhancements)];
561 	size_t out_idx = 0;
562 	int err;
563 
564 	if (!args->pointer) {
565 		args->size = sizeof(struct drm_pvr_dev_query_enhancements);
566 		return 0;
567 	}
568 
569 	err = PVR_UOBJ_GET(query, args->size, args->pointer);
570 
571 	if (err < 0)
572 		return err;
573 	if (query._padding_a)
574 		return -EINVAL;
575 	if (query._padding_c)
576 		return -EINVAL;
577 
578 	for (int i = 0; i < ARRAY_SIZE(umd_enhancements); i++) {
579 		if (pvr_device_has_uapi_enhancement(pvr_dev, umd_enhancements[i]))
580 			out[out_idx++] = umd_enhancements[i];
581 	}
582 
583 	if (!query.enhancements)
584 		goto copy_out;
585 	if (query.count < out_idx)
586 		return -E2BIG;
587 
588 	if (copy_to_user(u64_to_user_ptr(query.enhancements), out,
589 			 out_idx * sizeof(u32))) {
590 		return -EFAULT;
591 	}
592 
593 copy_out:
594 	query.count = out_idx;
595 	err = PVR_UOBJ_SET(args->pointer, args->size, query);
596 	if (err < 0)
597 		return err;
598 
599 	if (args->size > sizeof(query))
600 		args->size = sizeof(query);
601 	return 0;
602 }
603 
604 /**
605  * pvr_ioctl_dev_query() - IOCTL to copy information about a device
606  * @drm_dev: [IN] DRM device.
607  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
608  *                     &struct drm_pvr_ioctl_dev_query_args.
609  * @file: [IN] DRM file private data.
610  *
611  * Called from userspace with %DRM_IOCTL_PVR_DEV_QUERY.
612  * If the given receiving struct pointer is NULL, or the indicated size is too
613  * small, the expected size of the struct type will be returned in the size
614  * argument field.
615  *
616  * Return:
617  *  * 0 on success or when fetching the size with args->pointer == NULL, or
618  *  * -%E2BIG if the indicated size of the receiving struct is less than is
619  *    required to contain the copied data, or
620  *  * -%EINVAL if the indicated struct type is unknown, or
621  *  * -%ENOMEM if local memory could not be allocated, or
622  *  * -%EFAULT if local memory could not be copied to userspace.
623  */
624 static int
625 pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args,
626 		    struct drm_file *file)
627 {
628 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
629 	struct drm_pvr_ioctl_dev_query_args *args = raw_args;
630 	int idx;
631 	int ret = -EINVAL;
632 
633 	if (!drm_dev_enter(drm_dev, &idx))
634 		return -EIO;
635 
636 	switch ((enum drm_pvr_dev_query)args->type) {
637 	case DRM_PVR_DEV_QUERY_GPU_INFO_GET:
638 		ret = pvr_dev_query_gpu_info_get(pvr_dev, args);
639 		break;
640 
641 	case DRM_PVR_DEV_QUERY_RUNTIME_INFO_GET:
642 		ret = pvr_dev_query_runtime_info_get(pvr_dev, args);
643 		break;
644 
645 	case DRM_PVR_DEV_QUERY_QUIRKS_GET:
646 		ret = pvr_dev_query_quirks_get(pvr_dev, args);
647 		break;
648 
649 	case DRM_PVR_DEV_QUERY_ENHANCEMENTS_GET:
650 		ret = pvr_dev_query_enhancements_get(pvr_dev, args);
651 		break;
652 
653 	case DRM_PVR_DEV_QUERY_HEAP_INFO_GET:
654 		ret = pvr_heap_info_get(pvr_dev, args);
655 		break;
656 
657 	case DRM_PVR_DEV_QUERY_STATIC_DATA_AREAS_GET:
658 		ret = pvr_static_data_areas_get(pvr_dev, args);
659 		break;
660 	}
661 
662 	drm_dev_exit(idx);
663 
664 	return ret;
665 }
666 
667 /**
668  * pvr_ioctl_create_context() - IOCTL to create a context
669  * @drm_dev: [IN] DRM device.
670  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
671  *                     &struct drm_pvr_ioctl_create_context_args.
672  * @file: [IN] DRM file private data.
673  *
674  * Called from userspace with %DRM_IOCTL_PVR_CREATE_CONTEXT.
675  *
676  * Return:
677  *  * 0 on success, or
678  *  * -%EINVAL if provided arguments are invalid, or
679  *  * -%EFAULT if arguments can't be copied from userspace, or
680  *  * Any error returned by pvr_create_render_context().
681  */
682 static int
683 pvr_ioctl_create_context(struct drm_device *drm_dev, void *raw_args,
684 			 struct drm_file *file)
685 {
686 	struct drm_pvr_ioctl_create_context_args *args = raw_args;
687 	struct pvr_file *pvr_file = file->driver_priv;
688 	int idx;
689 	int ret;
690 
691 	if (!drm_dev_enter(drm_dev, &idx))
692 		return -EIO;
693 
694 	ret = pvr_context_create(pvr_file, args);
695 
696 	drm_dev_exit(idx);
697 
698 	return ret;
699 }
700 
701 /**
702  * pvr_ioctl_destroy_context() - IOCTL to destroy a context
703  * @drm_dev: [IN] DRM device.
704  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
705  *                     &struct drm_pvr_ioctl_destroy_context_args.
706  * @file: [IN] DRM file private data.
707  *
708  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_CONTEXT.
709  *
710  * Return:
711  *  * 0 on success, or
712  *  * -%EINVAL if context not in context list.
713  */
714 static int
715 pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args,
716 			  struct drm_file *file)
717 {
718 	struct drm_pvr_ioctl_destroy_context_args *args = raw_args;
719 	struct pvr_file *pvr_file = file->driver_priv;
720 
721 	if (args->_padding_4)
722 		return -EINVAL;
723 
724 	return pvr_context_destroy(pvr_file, args->handle);
725 }
726 
727 /**
728  * pvr_ioctl_create_free_list() - IOCTL to create a free list
729  * @drm_dev: [IN] DRM device.
730  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
731  *                     &struct drm_pvr_ioctl_create_free_list_args.
732  * @file: [IN] DRM file private data.
733  *
734  * Called from userspace with %DRM_IOCTL_PVR_CREATE_FREE_LIST.
735  *
736  * Return:
737  *  * 0 on success, or
738  *  * Any error returned by pvr_free_list_create().
739  */
740 static int
741 pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args,
742 			   struct drm_file *file)
743 {
744 	struct drm_pvr_ioctl_create_free_list_args *args = raw_args;
745 	struct pvr_file *pvr_file = to_pvr_file(file);
746 	struct pvr_free_list *free_list;
747 	int idx;
748 	int err;
749 
750 	if (!drm_dev_enter(drm_dev, &idx))
751 		return -EIO;
752 
753 	free_list = pvr_free_list_create(pvr_file, args);
754 	if (IS_ERR(free_list)) {
755 		err = PTR_ERR(free_list);
756 		goto err_drm_dev_exit;
757 	}
758 
759 	/* Allocate object handle for userspace. */
760 	err = xa_alloc(&pvr_file->free_list_handles,
761 		       &args->handle,
762 		       free_list,
763 		       xa_limit_32b,
764 		       GFP_KERNEL);
765 	if (err < 0)
766 		goto err_cleanup;
767 
768 	drm_dev_exit(idx);
769 
770 	return 0;
771 
772 err_cleanup:
773 	pvr_free_list_put(free_list);
774 
775 err_drm_dev_exit:
776 	drm_dev_exit(idx);
777 
778 	return err;
779 }
780 
781 /**
782  * pvr_ioctl_destroy_free_list() - IOCTL to destroy a free list
783  * @drm_dev: [IN] DRM device.
784  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
785  *                 &struct drm_pvr_ioctl_destroy_free_list_args.
786  * @file: [IN] DRM file private data.
787  *
788  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_FREE_LIST.
789  *
790  * Return:
791  *  * 0 on success, or
792  *  * -%EINVAL if free list not in object list.
793  */
794 static int
795 pvr_ioctl_destroy_free_list(struct drm_device *drm_dev, void *raw_args,
796 			    struct drm_file *file)
797 {
798 	struct drm_pvr_ioctl_destroy_free_list_args *args = raw_args;
799 	struct pvr_file *pvr_file = to_pvr_file(file);
800 	struct pvr_free_list *free_list;
801 
802 	if (args->_padding_4)
803 		return -EINVAL;
804 
805 	free_list = xa_erase(&pvr_file->free_list_handles, args->handle);
806 	if (!free_list)
807 		return -EINVAL;
808 
809 	pvr_free_list_put(free_list);
810 	return 0;
811 }
812 
813 /**
814  * pvr_ioctl_create_hwrt_dataset() - IOCTL to create a HWRT dataset
815  * @drm_dev: [IN] DRM device.
816  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
817  *                     &struct drm_pvr_ioctl_create_hwrt_dataset_args.
818  * @file: [IN] DRM file private data.
819  *
820  * Called from userspace with %DRM_IOCTL_PVR_CREATE_HWRT_DATASET.
821  *
822  * Return:
823  *  * 0 on success, or
824  *  * Any error returned by pvr_hwrt_dataset_create().
825  */
826 static int
827 pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
828 			      struct drm_file *file)
829 {
830 	struct drm_pvr_ioctl_create_hwrt_dataset_args *args = raw_args;
831 	struct pvr_file *pvr_file = to_pvr_file(file);
832 	struct pvr_hwrt_dataset *hwrt;
833 	int idx;
834 	int err;
835 
836 	if (!drm_dev_enter(drm_dev, &idx))
837 		return -EIO;
838 
839 	hwrt = pvr_hwrt_dataset_create(pvr_file, args);
840 	if (IS_ERR(hwrt)) {
841 		err = PTR_ERR(hwrt);
842 		goto err_drm_dev_exit;
843 	}
844 
845 	/* Allocate object handle for userspace. */
846 	err = xa_alloc(&pvr_file->hwrt_handles,
847 		       &args->handle,
848 		       hwrt,
849 		       xa_limit_32b,
850 		       GFP_KERNEL);
851 	if (err < 0)
852 		goto err_cleanup;
853 
854 	drm_dev_exit(idx);
855 
856 	return 0;
857 
858 err_cleanup:
859 	pvr_hwrt_dataset_put(hwrt);
860 
861 err_drm_dev_exit:
862 	drm_dev_exit(idx);
863 
864 	return err;
865 }
866 
867 /**
868  * pvr_ioctl_destroy_hwrt_dataset() - IOCTL to destroy a HWRT dataset
869  * @drm_dev: [IN] DRM device.
870  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
871  *                 &struct drm_pvr_ioctl_destroy_hwrt_dataset_args.
872  * @file: [IN] DRM file private data.
873  *
874  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_HWRT_DATASET.
875  *
876  * Return:
877  *  * 0 on success, or
878  *  * -%EINVAL if HWRT dataset not in object list.
879  */
880 static int
881 pvr_ioctl_destroy_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
882 			       struct drm_file *file)
883 {
884 	struct drm_pvr_ioctl_destroy_hwrt_dataset_args *args = raw_args;
885 	struct pvr_file *pvr_file = to_pvr_file(file);
886 	struct pvr_hwrt_dataset *hwrt;
887 
888 	if (args->_padding_4)
889 		return -EINVAL;
890 
891 	hwrt = xa_erase(&pvr_file->hwrt_handles, args->handle);
892 	if (!hwrt)
893 		return -EINVAL;
894 
895 	pvr_hwrt_dataset_put(hwrt);
896 	return 0;
897 }
898 
899 /**
900  * pvr_ioctl_create_vm_context() - IOCTL to create a VM context
901  * @drm_dev: [IN] DRM device.
902  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
903  *                     &struct drm_pvr_ioctl_create_vm_context_args.
904  * @file: [IN] DRM file private data.
905  *
906  * Called from userspace with %DRM_IOCTL_PVR_CREATE_VM_CONTEXT.
907  *
908  * Return:
909  *  * 0 on success, or
910  *  * Any error returned by pvr_vm_create_context().
911  */
912 static int
913 pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args,
914 			    struct drm_file *file)
915 {
916 	struct drm_pvr_ioctl_create_vm_context_args *args = raw_args;
917 	struct pvr_file *pvr_file = to_pvr_file(file);
918 	struct pvr_vm_context *vm_ctx;
919 	int idx;
920 	int err;
921 
922 	if (!drm_dev_enter(drm_dev, &idx))
923 		return -EIO;
924 
925 	if (args->_padding_4) {
926 		err = -EINVAL;
927 		goto err_drm_dev_exit;
928 	}
929 
930 	vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true);
931 	if (IS_ERR(vm_ctx)) {
932 		err = PTR_ERR(vm_ctx);
933 		goto err_drm_dev_exit;
934 	}
935 
936 	/* Allocate object handle for userspace. */
937 	err = xa_alloc(&pvr_file->vm_ctx_handles,
938 		       &args->handle,
939 		       vm_ctx,
940 		       xa_limit_32b,
941 		       GFP_KERNEL);
942 	if (err < 0)
943 		goto err_cleanup;
944 
945 	drm_dev_exit(idx);
946 
947 	return 0;
948 
949 err_cleanup:
950 	pvr_vm_context_put(vm_ctx);
951 
952 err_drm_dev_exit:
953 	drm_dev_exit(idx);
954 
955 	return err;
956 }
957 
958 /**
959  * pvr_ioctl_destroy_vm_context() - IOCTL to destroy a VM context
960 * @drm_dev: [IN] DRM device.
961 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
962 *                 &struct drm_pvr_ioctl_destroy_vm_context_args.
963 * @file: [IN] DRM file private data.
964 *
965 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_VM_CONTEXT.
966 *
967 * Return:
968 *  * 0 on success, or
969 *  * -%EINVAL if object not in object list.
970  */
971 static int
972 pvr_ioctl_destroy_vm_context(struct drm_device *drm_dev, void *raw_args,
973 			     struct drm_file *file)
974 {
975 	struct drm_pvr_ioctl_destroy_vm_context_args *args = raw_args;
976 	struct pvr_file *pvr_file = to_pvr_file(file);
977 	struct pvr_vm_context *vm_ctx;
978 
979 	if (args->_padding_4)
980 		return -EINVAL;
981 
982 	vm_ctx = xa_erase(&pvr_file->vm_ctx_handles, args->handle);
983 	if (!vm_ctx)
984 		return -EINVAL;
985 
986 	pvr_vm_context_put(vm_ctx);
987 	return 0;
988 }
989 
990 /**
991  * pvr_ioctl_vm_map() - IOCTL to map buffer to GPU address space.
992  * @drm_dev: [IN] DRM device.
993  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
994  *                 &struct drm_pvr_ioctl_vm_map_args.
995  * @file: [IN] DRM file private data.
996  *
997  * Called from userspace with %DRM_IOCTL_PVR_VM_MAP.
998  *
999  * Return:
1000  *  * 0 on success,
1001  *  * -%EINVAL if &drm_pvr_ioctl_vm_op_map_args.flags is not zero,
1002  *  * -%EINVAL if the bounds specified by &drm_pvr_ioctl_vm_op_map_args.offset
1003  *    and &drm_pvr_ioctl_vm_op_map_args.size are not valid or do not fall
1004  *    within the buffer object specified by
1005  *    &drm_pvr_ioctl_vm_op_map_args.handle,
1006  *  * -%EINVAL if the bounds specified by
1007  *    &drm_pvr_ioctl_vm_op_map_args.device_addr and
1008  *    &drm_pvr_ioctl_vm_op_map_args.size do not form a valid device-virtual
1009  *    address range which falls entirely within a single heap, or
1010  *  * -%ENOENT if &drm_pvr_ioctl_vm_op_map_args.handle does not refer to a
1011  *    valid PowerVR buffer object.
1012  */
1013 static int
1014 pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args,
1015 		 struct drm_file *file)
1016 {
1017 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1018 	struct drm_pvr_ioctl_vm_map_args *args = raw_args;
1019 	struct pvr_file *pvr_file = to_pvr_file(file);
1020 	struct pvr_vm_context *vm_ctx;
1021 
1022 	struct pvr_gem_object *pvr_obj;
1023 	size_t pvr_obj_size;
1024 
1025 	u64 offset_plus_size;
1026 	int idx;
1027 	int err;
1028 
1029 	if (!drm_dev_enter(drm_dev, &idx))
1030 		return -EIO;
1031 
1032 	/* Initial validation of args. */
1033 	if (args->_padding_14) {
1034 		err = -EINVAL;
1035 		goto err_drm_dev_exit;
1036 	}
1037 
1038 	if (args->flags != 0 ||
1039 	    check_add_overflow(args->offset, args->size, &offset_plus_size) ||
1040 	    !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) {
1041 		err = -EINVAL;
1042 		goto err_drm_dev_exit;
1043 	}
1044 
1045 	vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1046 	if (!vm_ctx) {
1047 		err = -EINVAL;
1048 		goto err_drm_dev_exit;
1049 	}
1050 
1051 	pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
1052 	if (!pvr_obj) {
1053 		err = -ENOENT;
1054 		goto err_put_vm_context;
1055 	}
1056 
1057 	pvr_obj_size = pvr_gem_object_size(pvr_obj);
1058 
1059 	/*
1060 	 * Validate offset and size args. The alignment of these will be
1061 	 * checked when mapping; for now just check that they're within valid
1062 	 * bounds
1063 	 */
1064 	if (args->offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) {
1065 		err = -EINVAL;
1066 		goto err_put_pvr_object;
1067 	}
1068 
1069 	err = pvr_vm_map(vm_ctx, pvr_obj, args->offset,
1070 			 args->device_addr, args->size);
1071 	if (err)
1072 		goto err_put_pvr_object;
1073 
1074 	/*
1075 	 * In order to set up the mapping, we needed a reference to &pvr_obj.
1076 	 * However, pvr_vm_map() obtains and stores its own reference, so we
1077 	 * must release ours before returning.
1078 	 */
1079 
1080 err_put_pvr_object:
1081 	pvr_gem_object_put(pvr_obj);
1082 
1083 err_put_vm_context:
1084 	pvr_vm_context_put(vm_ctx);
1085 
1086 err_drm_dev_exit:
1087 	drm_dev_exit(idx);
1088 
1089 	return err;
1090 }
1091 
1092 /**
1093  * pvr_ioctl_vm_unmap() - IOCTL to unmap buffer from GPU address space.
1094  * @drm_dev: [IN] DRM device.
1095  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1096  *                 &struct drm_pvr_ioctl_vm_unmap_args.
1097  * @file: [IN] DRM file private data.
1098  *
1099  * Called from userspace with %DRM_IOCTL_PVR_VM_UNMAP.
1100  *
1101  * Return:
1102  *  * 0 on success,
1103  *  * -%EINVAL if &drm_pvr_ioctl_vm_op_unmap_args.device_addr is not a valid
1104  *    device page-aligned device-virtual address, or
1105  *  * -%ENOENT if there is currently no PowerVR buffer object mapped at
1106  *    &drm_pvr_ioctl_vm_op_unmap_args.device_addr.
1107  */
1108 static int
1109 pvr_ioctl_vm_unmap(struct drm_device *drm_dev, void *raw_args,
1110 		   struct drm_file *file)
1111 {
1112 	struct drm_pvr_ioctl_vm_unmap_args *args = raw_args;
1113 	struct pvr_file *pvr_file = to_pvr_file(file);
1114 	struct pvr_vm_context *vm_ctx;
1115 	int err;
1116 
1117 	/* Initial validation of args. */
1118 	if (args->_padding_4)
1119 		return -EINVAL;
1120 
1121 	vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1122 	if (!vm_ctx)
1123 		return -EINVAL;
1124 
1125 	err = pvr_vm_unmap(vm_ctx, args->device_addr, args->size);
1126 
1127 	pvr_vm_context_put(vm_ctx);
1128 
1129 	return err;
1130 }
1131 
1132 /*
1133  * pvr_ioctl_submit_job() - IOCTL to submit a job to the GPU
1134  * @drm_dev: [IN] DRM device.
1135  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1136  *                 &struct drm_pvr_ioctl_submit_job_args.
1137  * @file: [IN] DRM file private data.
1138  *
1139  * Called from userspace with %DRM_IOCTL_PVR_SUBMIT_JOB.
1140  *
1141  * Return:
1142  *  * 0 on success, or
1143  *  * -%EINVAL if arguments are invalid.
1144  */
1145 static int
1146 pvr_ioctl_submit_jobs(struct drm_device *drm_dev, void *raw_args,
1147 		      struct drm_file *file)
1148 {
1149 	struct drm_pvr_ioctl_submit_jobs_args *args = raw_args;
1150 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1151 	struct pvr_file *pvr_file = to_pvr_file(file);
1152 	int idx;
1153 	int err;
1154 
1155 	trace_pvr_job_submit_ioctl(pvr_dev, args->jobs.count);
1156 
1157 	if (!drm_dev_enter(drm_dev, &idx))
1158 		return -EIO;
1159 
1160 	err = pvr_submit_jobs(pvr_dev, pvr_file, args);
1161 
1162 	drm_dev_exit(idx);
1163 
1164 	return err;
1165 }
1166 
1167 int
1168 pvr_get_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, void *out)
1169 {
1170 	if (usr_stride < min_stride)
1171 		return -EINVAL;
1172 
1173 	return copy_struct_from_user(out, obj_size, u64_to_user_ptr(usr_ptr), usr_stride);
1174 }
1175 
1176 int
1177 pvr_set_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, const void *in)
1178 {
1179 	if (usr_stride < min_stride)
1180 		return -EINVAL;
1181 
1182 	if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_stride, obj_size)))
1183 		return -EFAULT;
1184 
1185 	if (usr_stride > obj_size &&
1186 	    clear_user(u64_to_user_ptr(usr_ptr + obj_size), usr_stride - obj_size)) {
1187 		return -EFAULT;
1188 	}
1189 
1190 	return 0;
1191 }
1192 
1193 int
1194 pvr_get_uobj_array(const struct drm_pvr_obj_array *in, u32 min_stride, u32 obj_size, void **out)
1195 {
1196 	int ret = 0;
1197 	void *out_alloc;
1198 
1199 	if (in->stride < min_stride)
1200 		return -EINVAL;
1201 
1202 	if (!in->count)
1203 		return 0;
1204 
1205 	out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
1206 	if (!out_alloc)
1207 		return -ENOMEM;
1208 
1209 	if (obj_size == in->stride) {
1210 		if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
1211 				   (unsigned long)obj_size * in->count))
1212 			ret = -EFAULT;
1213 	} else {
1214 		void __user *in_ptr = u64_to_user_ptr(in->array);
1215 		void *out_ptr = out_alloc;
1216 
1217 		for (u32 i = 0; i < in->count; i++) {
1218 			ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
1219 			if (ret)
1220 				break;
1221 
1222 			out_ptr += obj_size;
1223 			in_ptr += in->stride;
1224 		}
1225 	}
1226 
1227 	if (ret) {
1228 		kvfree(out_alloc);
1229 		return ret;
1230 	}
1231 
1232 	*out = out_alloc;
1233 	return 0;
1234 }
1235 
1236 int
1237 pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_size,
1238 		   const void *in)
1239 {
1240 	if (out->stride < min_stride)
1241 		return -EINVAL;
1242 
1243 	if (!out->count)
1244 		return 0;
1245 
1246 	if (obj_size == out->stride) {
1247 		if (copy_to_user(u64_to_user_ptr(out->array), in,
1248 				 (unsigned long)obj_size * out->count))
1249 			return -EFAULT;
1250 	} else {
1251 		u32 cpy_elem_size = min_t(u32, out->stride, obj_size);
1252 		void __user *out_ptr = u64_to_user_ptr(out->array);
1253 		const void *in_ptr = in;
1254 
1255 		for (u32 i = 0; i < out->count; i++) {
1256 			if (copy_to_user(out_ptr, in_ptr, cpy_elem_size))
1257 				return -EFAULT;
1258 
1259 			if (out->stride > obj_size &&
1260 			    clear_user(out_ptr + cpy_elem_size, out->stride - obj_size)) {
1261 				return -EFAULT;
1262 			}
1263 
1264 			out_ptr += out->stride;
1265 			in_ptr += obj_size;
1266 		}
1267 	}
1268 
1269 	return 0;
1270 }
1271 
1272 #define DRM_PVR_IOCTL(_name, _func, _flags) \
1273 	DRM_IOCTL_DEF_DRV(PVR_##_name, pvr_ioctl_##_func, _flags)
1274 
1275 /* clang-format off */
1276 
1277 static const struct drm_ioctl_desc pvr_drm_driver_ioctls[] = {
1278 	DRM_PVR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
1279 	DRM_PVR_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW),
1280 	DRM_PVR_IOCTL(GET_BO_MMAP_OFFSET, get_bo_mmap_offset, DRM_RENDER_ALLOW),
1281 	DRM_PVR_IOCTL(CREATE_VM_CONTEXT, create_vm_context, DRM_RENDER_ALLOW),
1282 	DRM_PVR_IOCTL(DESTROY_VM_CONTEXT, destroy_vm_context, DRM_RENDER_ALLOW),
1283 	DRM_PVR_IOCTL(VM_MAP, vm_map, DRM_RENDER_ALLOW),
1284 	DRM_PVR_IOCTL(VM_UNMAP, vm_unmap, DRM_RENDER_ALLOW),
1285 	DRM_PVR_IOCTL(CREATE_CONTEXT, create_context, DRM_RENDER_ALLOW),
1286 	DRM_PVR_IOCTL(DESTROY_CONTEXT, destroy_context, DRM_RENDER_ALLOW),
1287 	DRM_PVR_IOCTL(CREATE_FREE_LIST, create_free_list, DRM_RENDER_ALLOW),
1288 	DRM_PVR_IOCTL(DESTROY_FREE_LIST, destroy_free_list, DRM_RENDER_ALLOW),
1289 	DRM_PVR_IOCTL(CREATE_HWRT_DATASET, create_hwrt_dataset, DRM_RENDER_ALLOW),
1290 	DRM_PVR_IOCTL(DESTROY_HWRT_DATASET, destroy_hwrt_dataset, DRM_RENDER_ALLOW),
1291 	DRM_PVR_IOCTL(SUBMIT_JOBS, submit_jobs, DRM_RENDER_ALLOW),
1292 };
1293 
1294 /* clang-format on */
1295 
1296 #undef DRM_PVR_IOCTL
1297 
1298 /**
1299  * pvr_drm_driver_open() - Driver callback when a new &struct drm_file is opened
1300  * @drm_dev: [IN] DRM device.
1301  * @file: [IN] DRM file private data.
1302  *
1303  * Allocates powervr-specific file private data (&struct pvr_file).
1304  *
1305  * Registered in &pvr_drm_driver.
1306  *
1307  * Return:
1308  *  * 0 on success,
1309  *  * -%ENOMEM if the allocation of a &struct ipvr_file fails, or
1310  *  * Any error returned by pvr_memory_context_init().
1311  */
1312 static int
1313 pvr_drm_driver_open(struct drm_device *drm_dev, struct drm_file *file)
1314 {
1315 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1316 	struct pvr_file *pvr_file;
1317 
1318 	pvr_file = kzalloc_obj(*pvr_file);
1319 	if (!pvr_file)
1320 		return -ENOMEM;
1321 
1322 	/*
1323 	 * Store reference to base DRM file private data for use by
1324 	 * from_pvr_file.
1325 	 */
1326 	pvr_file->file = file;
1327 
1328 	/*
1329 	 * Store reference to powervr-specific outer device struct in file
1330 	 * private data for convenient access.
1331 	 */
1332 	pvr_file->pvr_dev = pvr_dev;
1333 
1334 	INIT_LIST_HEAD(&pvr_file->contexts);
1335 
1336 	xa_init_flags(&pvr_file->ctx_handles, XA_FLAGS_ALLOC1);
1337 	xa_init_flags(&pvr_file->free_list_handles, XA_FLAGS_ALLOC1);
1338 	xa_init_flags(&pvr_file->hwrt_handles, XA_FLAGS_ALLOC1);
1339 	xa_init_flags(&pvr_file->vm_ctx_handles, XA_FLAGS_ALLOC1);
1340 
1341 	/*
1342 	 * Store reference to powervr-specific file private data in DRM file
1343 	 * private data.
1344 	 */
1345 	file->driver_priv = pvr_file;
1346 
1347 	return 0;
1348 }
1349 
1350 /**
1351  * pvr_drm_driver_postclose() - One of the driver callbacks when a &struct
1352  * drm_file is closed.
1353  * @drm_dev: [IN] DRM device (unused).
1354  * @file: [IN] DRM file private data.
1355  *
1356  * Frees powervr-specific file private data (&struct pvr_file).
1357  *
1358  * Registered in &pvr_drm_driver.
1359  */
1360 static void
1361 pvr_drm_driver_postclose(__always_unused struct drm_device *drm_dev,
1362 			 struct drm_file *file)
1363 {
1364 	struct pvr_file *pvr_file = to_pvr_file(file);
1365 
1366 	/* Kill remaining contexts. */
1367 	pvr_destroy_contexts_for_file(pvr_file);
1368 
1369 	/* Drop references on any remaining objects. */
1370 	pvr_destroy_free_lists_for_file(pvr_file);
1371 	pvr_destroy_hwrt_datasets_for_file(pvr_file);
1372 	pvr_destroy_vm_contexts_for_file(pvr_file);
1373 
1374 	kfree(pvr_file);
1375 	file->driver_priv = NULL;
1376 }
1377 
1378 DEFINE_DRM_GEM_FOPS(pvr_drm_driver_fops);
1379 
1380 static struct drm_driver pvr_drm_driver = {
1381 	.driver_features = DRIVER_GEM | DRIVER_GEM_GPUVA | DRIVER_RENDER |
1382 			   DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE,
1383 	.open = pvr_drm_driver_open,
1384 	.postclose = pvr_drm_driver_postclose,
1385 	.ioctls = pvr_drm_driver_ioctls,
1386 	.num_ioctls = ARRAY_SIZE(pvr_drm_driver_ioctls),
1387 	.fops = &pvr_drm_driver_fops,
1388 #if defined(CONFIG_DEBUG_FS)
1389 	.debugfs_init = pvr_debugfs_init,
1390 #endif
1391 
1392 	.name = PVR_DRIVER_NAME,
1393 	.desc = PVR_DRIVER_DESC,
1394 	.major = PVR_DRIVER_MAJOR,
1395 	.minor = PVR_DRIVER_MINOR,
1396 	.patchlevel = PVR_DRIVER_PATCHLEVEL,
1397 
1398 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
1399 	.gem_create_object = pvr_gem_create_object,
1400 };
1401 
1402 static int
1403 pvr_probe(struct platform_device *plat_dev)
1404 {
1405 	struct pvr_device *pvr_dev;
1406 	struct drm_device *drm_dev;
1407 	int err;
1408 
1409 	pvr_dev = devm_drm_dev_alloc(&plat_dev->dev, &pvr_drm_driver,
1410 				     struct pvr_device, base);
1411 	if (IS_ERR(pvr_dev))
1412 		return PTR_ERR(pvr_dev);
1413 
1414 	drm_dev = &pvr_dev->base;
1415 
1416 	platform_set_drvdata(plat_dev, drm_dev);
1417 
1418 	err = pvr_power_domains_init(pvr_dev);
1419 	if (err)
1420 		return err;
1421 
1422 	init_rwsem(&pvr_dev->reset_sem);
1423 
1424 	pvr_context_device_init(pvr_dev);
1425 
1426 	err = pvr_queue_device_init(pvr_dev);
1427 	if (err)
1428 		goto err_context_fini;
1429 
1430 	devm_pm_runtime_enable(&plat_dev->dev);
1431 	pm_runtime_mark_last_busy(&plat_dev->dev);
1432 
1433 	pm_runtime_set_autosuspend_delay(&plat_dev->dev, 50);
1434 	pm_runtime_use_autosuspend(&plat_dev->dev);
1435 	pvr_watchdog_init(pvr_dev);
1436 
1437 	err = pvr_device_init(pvr_dev);
1438 	if (err)
1439 		goto err_watchdog_fini;
1440 
1441 	err = drm_dev_register(drm_dev, 0);
1442 	if (err)
1443 		goto err_device_fini;
1444 
1445 	xa_init_flags(&pvr_dev->free_list_ids, XA_FLAGS_ALLOC1);
1446 	xa_init_flags(&pvr_dev->job_ids, XA_FLAGS_ALLOC1);
1447 
1448 	return 0;
1449 
1450 err_device_fini:
1451 	pvr_device_fini(pvr_dev);
1452 
1453 err_watchdog_fini:
1454 	pvr_watchdog_fini(pvr_dev);
1455 
1456 	pvr_queue_device_fini(pvr_dev);
1457 
1458 err_context_fini:
1459 	pvr_context_device_fini(pvr_dev);
1460 
1461 	pvr_power_domains_fini(pvr_dev);
1462 
1463 	return err;
1464 }
1465 
1466 static void pvr_remove(struct platform_device *plat_dev)
1467 {
1468 	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
1469 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1470 
1471 	WARN_ON(!xa_empty(&pvr_dev->job_ids));
1472 	WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
1473 
1474 	xa_destroy(&pvr_dev->job_ids);
1475 	xa_destroy(&pvr_dev->free_list_ids);
1476 
1477 	pm_runtime_suspend(drm_dev->dev);
1478 	pvr_device_fini(pvr_dev);
1479 	drm_dev_unplug(drm_dev);
1480 	pvr_watchdog_fini(pvr_dev);
1481 	pvr_queue_device_fini(pvr_dev);
1482 	pvr_context_device_fini(pvr_dev);
1483 	pvr_power_domains_fini(pvr_dev);
1484 }
1485 
1486 static const struct pvr_device_data pvr_device_data_manual = {
1487 	.pwr_ops = &pvr_power_sequence_ops_manual,
1488 };
1489 
1490 static const struct pvr_device_data pvr_device_data_pwrseq = {
1491 	.pwr_ops = &pvr_power_sequence_ops_pwrseq,
1492 };
1493 
1494 static const struct of_device_id dt_match[] = {
1495 	{
1496 		.compatible = "thead,th1520-gpu",
1497 		.data = &pvr_device_data_pwrseq,
1498 	},
1499 	{
1500 		.compatible = "img,img-rogue",
1501 		.data = &pvr_device_data_manual,
1502 	},
1503 
1504 	/*
1505 	 * This legacy compatible string was introduced early on before the more generic
1506 	 * "img,img-rogue" was added. Keep it around here for compatibility, but never use
1507 	 * "img,img-axe" in new devicetrees.
1508 	 */
1509 	{
1510 		.compatible = "img,img-axe",
1511 		.data = &pvr_device_data_manual,
1512 	},
1513 	{}
1514 };
1515 MODULE_DEVICE_TABLE(of, dt_match);
1516 
1517 static const struct dev_pm_ops pvr_pm_ops = {
1518 	RUNTIME_PM_OPS(pvr_power_device_suspend, pvr_power_device_resume, pvr_power_device_idle)
1519 };
1520 
1521 static struct platform_driver pvr_driver = {
1522 	.probe = pvr_probe,
1523 	.remove = pvr_remove,
1524 	.driver = {
1525 		.name = PVR_DRIVER_NAME,
1526 		.pm = &pvr_pm_ops,
1527 		.of_match_table = dt_match,
1528 	},
1529 };
1530 module_platform_driver(pvr_driver);
1531 
1532 MODULE_AUTHOR("Imagination Technologies Ltd.");
1533 MODULE_DESCRIPTION(PVR_DRIVER_DESC);
1534 MODULE_LICENSE("Dual MIT/GPL");
1535 MODULE_IMPORT_NS("DMA_BUF");
1536 MODULE_FIRMWARE("powervr/rogue_33.15.11.3_v1.fw");
1537 MODULE_FIRMWARE("powervr/rogue_36.52.104.182_v1.fw");
1538 MODULE_FIRMWARE("powervr/rogue_36.53.104.796_v1.fw");
1539