xref: /linux/drivers/gpu/drm/imagination/pvr_drv.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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/mod_devicetable.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/of_device.h>
37 #include <linux/of_platform.h>
38 #include <linux/platform_device.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/xarray.h>
41 
42 /**
43  * DOC: PowerVR (Series 6 and later) and IMG Graphics Driver
44  *
45  * This driver supports the following PowerVR/IMG graphics cores from Imagination Technologies:
46  *
47  * * AXE-1-16M (found in Texas Instruments AM62)
48  * * BXS-4-64 MC1 (found in Texas Instruments J721S2/AM68)
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 	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 	args->size = sizeof(query);
600 	return 0;
601 }
602 
603 /**
604  * pvr_ioctl_dev_query() - IOCTL to copy information about a device
605  * @drm_dev: [IN] DRM device.
606  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
607  *                     &struct drm_pvr_ioctl_dev_query_args.
608  * @file: [IN] DRM file private data.
609  *
610  * Called from userspace with %DRM_IOCTL_PVR_DEV_QUERY.
611  * If the given receiving struct pointer is NULL, or the indicated size is too
612  * small, the expected size of the struct type will be returned in the size
613  * argument field.
614  *
615  * Return:
616  *  * 0 on success or when fetching the size with args->pointer == NULL, or
617  *  * -%E2BIG if the indicated size of the receiving struct is less than is
618  *    required to contain the copied data, or
619  *  * -%EINVAL if the indicated struct type is unknown, or
620  *  * -%ENOMEM if local memory could not be allocated, or
621  *  * -%EFAULT if local memory could not be copied to userspace.
622  */
623 static int
624 pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args,
625 		    struct drm_file *file)
626 {
627 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
628 	struct drm_pvr_ioctl_dev_query_args *args = raw_args;
629 	int idx;
630 	int ret = -EINVAL;
631 
632 	if (!drm_dev_enter(drm_dev, &idx))
633 		return -EIO;
634 
635 	switch ((enum drm_pvr_dev_query)args->type) {
636 	case DRM_PVR_DEV_QUERY_GPU_INFO_GET:
637 		ret = pvr_dev_query_gpu_info_get(pvr_dev, args);
638 		break;
639 
640 	case DRM_PVR_DEV_QUERY_RUNTIME_INFO_GET:
641 		ret = pvr_dev_query_runtime_info_get(pvr_dev, args);
642 		break;
643 
644 	case DRM_PVR_DEV_QUERY_QUIRKS_GET:
645 		ret = pvr_dev_query_quirks_get(pvr_dev, args);
646 		break;
647 
648 	case DRM_PVR_DEV_QUERY_ENHANCEMENTS_GET:
649 		ret = pvr_dev_query_enhancements_get(pvr_dev, args);
650 		break;
651 
652 	case DRM_PVR_DEV_QUERY_HEAP_INFO_GET:
653 		ret = pvr_heap_info_get(pvr_dev, args);
654 		break;
655 
656 	case DRM_PVR_DEV_QUERY_STATIC_DATA_AREAS_GET:
657 		ret = pvr_static_data_areas_get(pvr_dev, args);
658 		break;
659 	}
660 
661 	drm_dev_exit(idx);
662 
663 	return ret;
664 }
665 
666 /**
667  * pvr_ioctl_create_context() - IOCTL to create a context
668  * @drm_dev: [IN] DRM device.
669  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
670  *                     &struct drm_pvr_ioctl_create_context_args.
671  * @file: [IN] DRM file private data.
672  *
673  * Called from userspace with %DRM_IOCTL_PVR_CREATE_CONTEXT.
674  *
675  * Return:
676  *  * 0 on success, or
677  *  * -%EINVAL if provided arguments are invalid, or
678  *  * -%EFAULT if arguments can't be copied from userspace, or
679  *  * Any error returned by pvr_create_render_context().
680  */
681 static int
682 pvr_ioctl_create_context(struct drm_device *drm_dev, void *raw_args,
683 			 struct drm_file *file)
684 {
685 	struct drm_pvr_ioctl_create_context_args *args = raw_args;
686 	struct pvr_file *pvr_file = file->driver_priv;
687 	int idx;
688 	int ret;
689 
690 	if (!drm_dev_enter(drm_dev, &idx))
691 		return -EIO;
692 
693 	ret = pvr_context_create(pvr_file, args);
694 
695 	drm_dev_exit(idx);
696 
697 	return ret;
698 }
699 
700 /**
701  * pvr_ioctl_destroy_context() - IOCTL to destroy a context
702  * @drm_dev: [IN] DRM device.
703  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
704  *                     &struct drm_pvr_ioctl_destroy_context_args.
705  * @file: [IN] DRM file private data.
706  *
707  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_CONTEXT.
708  *
709  * Return:
710  *  * 0 on success, or
711  *  * -%EINVAL if context not in context list.
712  */
713 static int
714 pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args,
715 			  struct drm_file *file)
716 {
717 	struct drm_pvr_ioctl_destroy_context_args *args = raw_args;
718 	struct pvr_file *pvr_file = file->driver_priv;
719 
720 	if (args->_padding_4)
721 		return -EINVAL;
722 
723 	return pvr_context_destroy(pvr_file, args->handle);
724 }
725 
726 /**
727  * pvr_ioctl_create_free_list() - IOCTL to create a free list
728  * @drm_dev: [IN] DRM device.
729  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
730  *                     &struct drm_pvr_ioctl_create_free_list_args.
731  * @file: [IN] DRM file private data.
732  *
733  * Called from userspace with %DRM_IOCTL_PVR_CREATE_FREE_LIST.
734  *
735  * Return:
736  *  * 0 on success, or
737  *  * Any error returned by pvr_free_list_create().
738  */
739 static int
740 pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args,
741 			   struct drm_file *file)
742 {
743 	struct drm_pvr_ioctl_create_free_list_args *args = raw_args;
744 	struct pvr_file *pvr_file = to_pvr_file(file);
745 	struct pvr_free_list *free_list;
746 	int idx;
747 	int err;
748 
749 	if (!drm_dev_enter(drm_dev, &idx))
750 		return -EIO;
751 
752 	free_list = pvr_free_list_create(pvr_file, args);
753 	if (IS_ERR(free_list)) {
754 		err = PTR_ERR(free_list);
755 		goto err_drm_dev_exit;
756 	}
757 
758 	/* Allocate object handle for userspace. */
759 	err = xa_alloc(&pvr_file->free_list_handles,
760 		       &args->handle,
761 		       free_list,
762 		       xa_limit_32b,
763 		       GFP_KERNEL);
764 	if (err < 0)
765 		goto err_cleanup;
766 
767 	drm_dev_exit(idx);
768 
769 	return 0;
770 
771 err_cleanup:
772 	pvr_free_list_put(free_list);
773 
774 err_drm_dev_exit:
775 	drm_dev_exit(idx);
776 
777 	return err;
778 }
779 
780 /**
781  * pvr_ioctl_destroy_free_list() - IOCTL to destroy a free list
782  * @drm_dev: [IN] DRM device.
783  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
784  *                 &struct drm_pvr_ioctl_destroy_free_list_args.
785  * @file: [IN] DRM file private data.
786  *
787  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_FREE_LIST.
788  *
789  * Return:
790  *  * 0 on success, or
791  *  * -%EINVAL if free list not in object list.
792  */
793 static int
794 pvr_ioctl_destroy_free_list(struct drm_device *drm_dev, void *raw_args,
795 			    struct drm_file *file)
796 {
797 	struct drm_pvr_ioctl_destroy_free_list_args *args = raw_args;
798 	struct pvr_file *pvr_file = to_pvr_file(file);
799 	struct pvr_free_list *free_list;
800 
801 	if (args->_padding_4)
802 		return -EINVAL;
803 
804 	free_list = xa_erase(&pvr_file->free_list_handles, args->handle);
805 	if (!free_list)
806 		return -EINVAL;
807 
808 	pvr_free_list_put(free_list);
809 	return 0;
810 }
811 
812 /**
813  * pvr_ioctl_create_hwrt_dataset() - IOCTL to create a HWRT dataset
814  * @drm_dev: [IN] DRM device.
815  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
816  *                     &struct drm_pvr_ioctl_create_hwrt_dataset_args.
817  * @file: [IN] DRM file private data.
818  *
819  * Called from userspace with %DRM_IOCTL_PVR_CREATE_HWRT_DATASET.
820  *
821  * Return:
822  *  * 0 on success, or
823  *  * Any error returned by pvr_hwrt_dataset_create().
824  */
825 static int
826 pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
827 			      struct drm_file *file)
828 {
829 	struct drm_pvr_ioctl_create_hwrt_dataset_args *args = raw_args;
830 	struct pvr_file *pvr_file = to_pvr_file(file);
831 	struct pvr_hwrt_dataset *hwrt;
832 	int idx;
833 	int err;
834 
835 	if (!drm_dev_enter(drm_dev, &idx))
836 		return -EIO;
837 
838 	hwrt = pvr_hwrt_dataset_create(pvr_file, args);
839 	if (IS_ERR(hwrt)) {
840 		err = PTR_ERR(hwrt);
841 		goto err_drm_dev_exit;
842 	}
843 
844 	/* Allocate object handle for userspace. */
845 	err = xa_alloc(&pvr_file->hwrt_handles,
846 		       &args->handle,
847 		       hwrt,
848 		       xa_limit_32b,
849 		       GFP_KERNEL);
850 	if (err < 0)
851 		goto err_cleanup;
852 
853 	drm_dev_exit(idx);
854 
855 	return 0;
856 
857 err_cleanup:
858 	pvr_hwrt_dataset_put(hwrt);
859 
860 err_drm_dev_exit:
861 	drm_dev_exit(idx);
862 
863 	return err;
864 }
865 
866 /**
867  * pvr_ioctl_destroy_hwrt_dataset() - IOCTL to destroy a HWRT dataset
868  * @drm_dev: [IN] DRM device.
869  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
870  *                 &struct drm_pvr_ioctl_destroy_hwrt_dataset_args.
871  * @file: [IN] DRM file private data.
872  *
873  * Called from userspace with %DRM_IOCTL_PVR_DESTROY_HWRT_DATASET.
874  *
875  * Return:
876  *  * 0 on success, or
877  *  * -%EINVAL if HWRT dataset not in object list.
878  */
879 static int
880 pvr_ioctl_destroy_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
881 			       struct drm_file *file)
882 {
883 	struct drm_pvr_ioctl_destroy_hwrt_dataset_args *args = raw_args;
884 	struct pvr_file *pvr_file = to_pvr_file(file);
885 	struct pvr_hwrt_dataset *hwrt;
886 
887 	if (args->_padding_4)
888 		return -EINVAL;
889 
890 	hwrt = xa_erase(&pvr_file->hwrt_handles, args->handle);
891 	if (!hwrt)
892 		return -EINVAL;
893 
894 	pvr_hwrt_dataset_put(hwrt);
895 	return 0;
896 }
897 
898 /**
899  * pvr_ioctl_create_vm_context() - IOCTL to create a VM context
900  * @drm_dev: [IN] DRM device.
901  * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
902  *                     &struct drm_pvr_ioctl_create_vm_context_args.
903  * @file: [IN] DRM file private data.
904  *
905  * Called from userspace with %DRM_IOCTL_PVR_CREATE_VM_CONTEXT.
906  *
907  * Return:
908  *  * 0 on success, or
909  *  * Any error returned by pvr_vm_create_context().
910  */
911 static int
912 pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args,
913 			    struct drm_file *file)
914 {
915 	struct drm_pvr_ioctl_create_vm_context_args *args = raw_args;
916 	struct pvr_file *pvr_file = to_pvr_file(file);
917 	struct pvr_vm_context *vm_ctx;
918 	int idx;
919 	int err;
920 
921 	if (!drm_dev_enter(drm_dev, &idx))
922 		return -EIO;
923 
924 	if (args->_padding_4) {
925 		err = -EINVAL;
926 		goto err_drm_dev_exit;
927 	}
928 
929 	vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true);
930 	if (IS_ERR(vm_ctx)) {
931 		err = PTR_ERR(vm_ctx);
932 		goto err_drm_dev_exit;
933 	}
934 
935 	/* Allocate object handle for userspace. */
936 	err = xa_alloc(&pvr_file->vm_ctx_handles,
937 		       &args->handle,
938 		       vm_ctx,
939 		       xa_limit_32b,
940 		       GFP_KERNEL);
941 	if (err < 0)
942 		goto err_cleanup;
943 
944 	drm_dev_exit(idx);
945 
946 	return 0;
947 
948 err_cleanup:
949 	pvr_vm_context_put(vm_ctx);
950 
951 err_drm_dev_exit:
952 	drm_dev_exit(idx);
953 
954 	return err;
955 }
956 
957 /**
958  * pvr_ioctl_destroy_vm_context() - IOCTL to destroy a VM context
959 * @drm_dev: [IN] DRM device.
960 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
961 *                 &struct drm_pvr_ioctl_destroy_vm_context_args.
962 * @file: [IN] DRM file private data.
963 *
964 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_VM_CONTEXT.
965 *
966 * Return:
967 *  * 0 on success, or
968 *  * -%EINVAL if object not in object list.
969  */
970 static int
971 pvr_ioctl_destroy_vm_context(struct drm_device *drm_dev, void *raw_args,
972 			     struct drm_file *file)
973 {
974 	struct drm_pvr_ioctl_destroy_vm_context_args *args = raw_args;
975 	struct pvr_file *pvr_file = to_pvr_file(file);
976 	struct pvr_vm_context *vm_ctx;
977 
978 	if (args->_padding_4)
979 		return -EINVAL;
980 
981 	vm_ctx = xa_erase(&pvr_file->vm_ctx_handles, args->handle);
982 	if (!vm_ctx)
983 		return -EINVAL;
984 
985 	pvr_vm_context_put(vm_ctx);
986 	return 0;
987 }
988 
989 /**
990  * pvr_ioctl_vm_map() - IOCTL to map buffer to GPU address space.
991  * @drm_dev: [IN] DRM device.
992  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
993  *                 &struct drm_pvr_ioctl_vm_map_args.
994  * @file: [IN] DRM file private data.
995  *
996  * Called from userspace with %DRM_IOCTL_PVR_VM_MAP.
997  *
998  * Return:
999  *  * 0 on success,
1000  *  * -%EINVAL if &drm_pvr_ioctl_vm_op_map_args.flags is not zero,
1001  *  * -%EINVAL if the bounds specified by &drm_pvr_ioctl_vm_op_map_args.offset
1002  *    and &drm_pvr_ioctl_vm_op_map_args.size are not valid or do not fall
1003  *    within the buffer object specified by
1004  *    &drm_pvr_ioctl_vm_op_map_args.handle,
1005  *  * -%EINVAL if the bounds specified by
1006  *    &drm_pvr_ioctl_vm_op_map_args.device_addr and
1007  *    &drm_pvr_ioctl_vm_op_map_args.size do not form a valid device-virtual
1008  *    address range which falls entirely within a single heap, or
1009  *  * -%ENOENT if &drm_pvr_ioctl_vm_op_map_args.handle does not refer to a
1010  *    valid PowerVR buffer object.
1011  */
1012 static int
1013 pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args,
1014 		 struct drm_file *file)
1015 {
1016 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1017 	struct drm_pvr_ioctl_vm_map_args *args = raw_args;
1018 	struct pvr_file *pvr_file = to_pvr_file(file);
1019 	struct pvr_vm_context *vm_ctx;
1020 
1021 	struct pvr_gem_object *pvr_obj;
1022 	size_t pvr_obj_size;
1023 
1024 	u64 offset_plus_size;
1025 	int idx;
1026 	int err;
1027 
1028 	if (!drm_dev_enter(drm_dev, &idx))
1029 		return -EIO;
1030 
1031 	/* Initial validation of args. */
1032 	if (args->_padding_14) {
1033 		err = -EINVAL;
1034 		goto err_drm_dev_exit;
1035 	}
1036 
1037 	if (args->flags != 0 ||
1038 	    check_add_overflow(args->offset, args->size, &offset_plus_size) ||
1039 	    !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) {
1040 		err = -EINVAL;
1041 		goto err_drm_dev_exit;
1042 	}
1043 
1044 	vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1045 	if (!vm_ctx) {
1046 		err = -EINVAL;
1047 		goto err_drm_dev_exit;
1048 	}
1049 
1050 	pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
1051 	if (!pvr_obj) {
1052 		err = -ENOENT;
1053 		goto err_put_vm_context;
1054 	}
1055 
1056 	pvr_obj_size = pvr_gem_object_size(pvr_obj);
1057 
1058 	/*
1059 	 * Validate offset and size args. The alignment of these will be
1060 	 * checked when mapping; for now just check that they're within valid
1061 	 * bounds
1062 	 */
1063 	if (args->offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) {
1064 		err = -EINVAL;
1065 		goto err_put_pvr_object;
1066 	}
1067 
1068 	err = pvr_vm_map(vm_ctx, pvr_obj, args->offset,
1069 			 args->device_addr, args->size);
1070 	if (err)
1071 		goto err_put_pvr_object;
1072 
1073 	/*
1074 	 * In order to set up the mapping, we needed a reference to &pvr_obj.
1075 	 * However, pvr_vm_map() obtains and stores its own reference, so we
1076 	 * must release ours before returning.
1077 	 */
1078 
1079 err_put_pvr_object:
1080 	pvr_gem_object_put(pvr_obj);
1081 
1082 err_put_vm_context:
1083 	pvr_vm_context_put(vm_ctx);
1084 
1085 err_drm_dev_exit:
1086 	drm_dev_exit(idx);
1087 
1088 	return err;
1089 }
1090 
1091 /**
1092  * pvr_ioctl_vm_unmap() - IOCTL to unmap buffer from GPU address space.
1093  * @drm_dev: [IN] DRM device.
1094  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1095  *                 &struct drm_pvr_ioctl_vm_unmap_args.
1096  * @file: [IN] DRM file private data.
1097  *
1098  * Called from userspace with %DRM_IOCTL_PVR_VM_UNMAP.
1099  *
1100  * Return:
1101  *  * 0 on success,
1102  *  * -%EINVAL if &drm_pvr_ioctl_vm_op_unmap_args.device_addr is not a valid
1103  *    device page-aligned device-virtual address, or
1104  *  * -%ENOENT if there is currently no PowerVR buffer object mapped at
1105  *    &drm_pvr_ioctl_vm_op_unmap_args.device_addr.
1106  */
1107 static int
1108 pvr_ioctl_vm_unmap(struct drm_device *drm_dev, void *raw_args,
1109 		   struct drm_file *file)
1110 {
1111 	struct drm_pvr_ioctl_vm_unmap_args *args = raw_args;
1112 	struct pvr_file *pvr_file = to_pvr_file(file);
1113 	struct pvr_vm_context *vm_ctx;
1114 	int err;
1115 
1116 	/* Initial validation of args. */
1117 	if (args->_padding_4)
1118 		return -EINVAL;
1119 
1120 	vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1121 	if (!vm_ctx)
1122 		return -EINVAL;
1123 
1124 	err = pvr_vm_unmap(vm_ctx, args->device_addr, args->size);
1125 
1126 	pvr_vm_context_put(vm_ctx);
1127 
1128 	return err;
1129 }
1130 
1131 /*
1132  * pvr_ioctl_submit_job() - IOCTL to submit a job to the GPU
1133  * @drm_dev: [IN] DRM device.
1134  * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1135  *                 &struct drm_pvr_ioctl_submit_job_args.
1136  * @file: [IN] DRM file private data.
1137  *
1138  * Called from userspace with %DRM_IOCTL_PVR_SUBMIT_JOB.
1139  *
1140  * Return:
1141  *  * 0 on success, or
1142  *  * -%EINVAL if arguments are invalid.
1143  */
1144 static int
1145 pvr_ioctl_submit_jobs(struct drm_device *drm_dev, void *raw_args,
1146 		      struct drm_file *file)
1147 {
1148 	struct drm_pvr_ioctl_submit_jobs_args *args = raw_args;
1149 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1150 	struct pvr_file *pvr_file = to_pvr_file(file);
1151 	int idx;
1152 	int err;
1153 
1154 	trace_pvr_job_submit_ioctl(pvr_dev, args->jobs.count);
1155 
1156 	if (!drm_dev_enter(drm_dev, &idx))
1157 		return -EIO;
1158 
1159 	err = pvr_submit_jobs(pvr_dev, pvr_file, args);
1160 
1161 	drm_dev_exit(idx);
1162 
1163 	return err;
1164 }
1165 
1166 int
1167 pvr_get_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, void *out)
1168 {
1169 	if (usr_stride < min_stride)
1170 		return -EINVAL;
1171 
1172 	return copy_struct_from_user(out, obj_size, u64_to_user_ptr(usr_ptr), usr_stride);
1173 }
1174 
1175 int
1176 pvr_set_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, const void *in)
1177 {
1178 	if (usr_stride < min_stride)
1179 		return -EINVAL;
1180 
1181 	if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_stride, obj_size)))
1182 		return -EFAULT;
1183 
1184 	if (usr_stride > obj_size &&
1185 	    clear_user(u64_to_user_ptr(usr_ptr + obj_size), usr_stride - obj_size)) {
1186 		return -EFAULT;
1187 	}
1188 
1189 	return 0;
1190 }
1191 
1192 int
1193 pvr_get_uobj_array(const struct drm_pvr_obj_array *in, u32 min_stride, u32 obj_size, void **out)
1194 {
1195 	int ret = 0;
1196 	void *out_alloc;
1197 
1198 	if (in->stride < min_stride)
1199 		return -EINVAL;
1200 
1201 	if (!in->count)
1202 		return 0;
1203 
1204 	out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
1205 	if (!out_alloc)
1206 		return -ENOMEM;
1207 
1208 	if (obj_size == in->stride) {
1209 		if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
1210 				   (unsigned long)obj_size * in->count))
1211 			ret = -EFAULT;
1212 	} else {
1213 		void __user *in_ptr = u64_to_user_ptr(in->array);
1214 		void *out_ptr = out_alloc;
1215 
1216 		for (u32 i = 0; i < in->count; i++) {
1217 			ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
1218 			if (ret)
1219 				break;
1220 
1221 			out_ptr += obj_size;
1222 			in_ptr += in->stride;
1223 		}
1224 	}
1225 
1226 	if (ret) {
1227 		kvfree(out_alloc);
1228 		return ret;
1229 	}
1230 
1231 	*out = out_alloc;
1232 	return 0;
1233 }
1234 
1235 int
1236 pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_size,
1237 		   const void *in)
1238 {
1239 	if (out->stride < min_stride)
1240 		return -EINVAL;
1241 
1242 	if (!out->count)
1243 		return 0;
1244 
1245 	if (obj_size == out->stride) {
1246 		if (copy_to_user(u64_to_user_ptr(out->array), in,
1247 				 (unsigned long)obj_size * out->count))
1248 			return -EFAULT;
1249 	} else {
1250 		u32 cpy_elem_size = min_t(u32, out->stride, obj_size);
1251 		void __user *out_ptr = u64_to_user_ptr(out->array);
1252 		const void *in_ptr = in;
1253 
1254 		for (u32 i = 0; i < out->count; i++) {
1255 			if (copy_to_user(out_ptr, in_ptr, cpy_elem_size))
1256 				return -EFAULT;
1257 
1258 			out_ptr += obj_size;
1259 			in_ptr += out->stride;
1260 		}
1261 
1262 		if (out->stride > obj_size &&
1263 		    clear_user(u64_to_user_ptr(out->array + obj_size),
1264 			       out->stride - obj_size)) {
1265 			return -EFAULT;
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