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