1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
4 #include "pvr_context.h"
5 #include "pvr_debugfs.h"
6 #include "pvr_device.h"
8 #include "pvr_free_list.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"
19 #include <uapi/drm/pvr_drm.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_drv.h>
23 #include <drm/drm_file.h>
24 #include <drm/drm_gem.h>
25 #include <drm/drm_ioctl.h>
27 #include <linux/err.h>
28 #include <linux/export.h>
30 #include <linux/kernel.h>
31 #include <linux/mod_devicetable.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/of_device.h>
35 #include <linux/of_platform.h>
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/xarray.h>
41 * DOC: PowerVR (Series 6 and later) and IMG Graphics Driver
43 * This driver supports the following PowerVR/IMG graphics cores from Imagination Technologies:
45 * * AXE-1-16M (found in Texas Instruments AM62)
49 * pvr_ioctl_create_bo() - IOCTL to create a GEM buffer object.
50 * @drm_dev: [IN] Target DRM device.
51 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
52 * &struct drm_pvr_ioctl_create_bo_args.
53 * @file: [IN] DRM file-private data.
55 * Called from userspace with %DRM_IOCTL_PVR_CREATE_BO.
59 * * -%EINVAL if the value of &drm_pvr_ioctl_create_bo_args.size is zero
60 * or wider than &typedef size_t,
61 * * -%EINVAL if any bits in &drm_pvr_ioctl_create_bo_args.flags that are
62 * reserved or undefined are set,
63 * * -%EINVAL if any padding fields in &drm_pvr_ioctl_create_bo_args are not
65 * * Any error encountered while creating the object (see
66 * pvr_gem_object_create()), or
67 * * Any error encountered while transferring ownership of the object into a
68 * userspace-accessible handle (see pvr_gem_object_into_handle()).
71 pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args,
72 struct drm_file *file)
74 struct drm_pvr_ioctl_create_bo_args *args = raw_args;
75 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
76 struct pvr_file *pvr_file = to_pvr_file(file);
78 struct pvr_gem_object *pvr_obj;
79 size_t sanitized_size;
84 if (!drm_dev_enter(drm_dev, &idx))
87 /* All padding fields must be zeroed. */
88 if (args->_padding_c != 0) {
90 goto err_drm_dev_exit;
94 * On 64-bit platforms (our primary target), size_t is a u64. However,
95 * on other architectures we have to check for overflow when casting
96 * down to size_t from u64.
98 * We also disallow zero-sized allocations, and reserved (kernel-only)
101 if (args->size > SIZE_MAX || args->size == 0 || args->flags &
102 ~DRM_PVR_BO_FLAGS_MASK || args->size & (PVR_DEVICE_PAGE_SIZE - 1)) {
104 goto err_drm_dev_exit;
107 sanitized_size = (size_t)args->size;
110 * Create a buffer object and transfer ownership to a userspace-
113 pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags);
114 if (IS_ERR(pvr_obj)) {
115 err = PTR_ERR(pvr_obj);
116 goto err_drm_dev_exit;
119 /* This function will not modify &args->handle unless it succeeds. */
120 err = pvr_gem_object_into_handle(pvr_obj, pvr_file, &args->handle);
122 goto err_destroy_obj;
130 * GEM objects are refcounted, so there is no explicit destructor
131 * function. Instead, we release the singular reference we currently
132 * hold on the object and let GEM take care of the rest.
134 pvr_gem_object_put(pvr_obj);
143 * pvr_ioctl_get_bo_mmap_offset() - IOCTL to generate a "fake" offset to be
144 * used when calling mmap() from userspace to map the given GEM buffer object
145 * @drm_dev: [IN] DRM device (unused).
146 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
147 * &struct drm_pvr_ioctl_get_bo_mmap_offset_args.
148 * @file: [IN] DRM file private data.
150 * Called from userspace with %DRM_IOCTL_PVR_GET_BO_MMAP_OFFSET.
152 * This IOCTL does *not* perform an mmap. See the docs on
153 * &struct drm_pvr_ioctl_get_bo_mmap_offset_args for details.
157 * * -%ENOENT if the handle does not reference a valid GEM buffer object,
158 * * -%EINVAL if any padding fields in &struct
159 * drm_pvr_ioctl_get_bo_mmap_offset_args are not zero, or
160 * * Any error returned by drm_gem_create_mmap_offset().
163 pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args,
164 struct drm_file *file)
166 struct drm_pvr_ioctl_get_bo_mmap_offset_args *args = raw_args;
167 struct pvr_file *pvr_file = to_pvr_file(file);
168 struct pvr_gem_object *pvr_obj;
169 struct drm_gem_object *gem_obj;
173 if (!drm_dev_enter(drm_dev, &idx))
176 /* All padding fields must be zeroed. */
177 if (args->_padding_4 != 0) {
179 goto err_drm_dev_exit;
183 * Obtain a kernel reference to the buffer object. This reference is
184 * counted and must be manually dropped before returning. If a buffer
185 * object cannot be found for the specified handle, return -%ENOENT (No
186 * such file or directory).
188 pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
191 goto err_drm_dev_exit;
194 gem_obj = gem_from_pvr_gem(pvr_obj);
197 * Allocate a fake offset which can be used in userspace calls to mmap
198 * on the DRM device file. If this fails, return the error code. This
199 * operation is idempotent.
201 ret = drm_gem_create_mmap_offset(gem_obj);
203 /* Drop our reference to the buffer object. */
204 drm_gem_object_put(gem_obj);
205 goto err_drm_dev_exit;
209 * Read out the fake offset allocated by the earlier call to
210 * drm_gem_create_mmap_offset.
212 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
214 /* Drop our reference to the buffer object. */
215 pvr_gem_object_put(pvr_obj);
223 static __always_inline u64
224 pvr_fw_version_packed(u32 major, u32 minor)
226 return ((u64)major << 32) | minor;
230 rogue_get_common_store_partition_space_size(struct pvr_device *pvr_dev)
232 u32 max_partitions = 0;
236 PVR_FEATURE_VALUE(pvr_dev, tile_size_x, &tile_size_x);
237 PVR_FEATURE_VALUE(pvr_dev, tile_size_y, &tile_size_y);
238 PVR_FEATURE_VALUE(pvr_dev, max_partitions, &max_partitions);
240 if (tile_size_x == 16 && tile_size_y == 16) {
241 u32 usc_min_output_registers_per_pix = 0;
243 PVR_FEATURE_VALUE(pvr_dev, usc_min_output_registers_per_pix,
244 &usc_min_output_registers_per_pix);
246 return tile_size_x * tile_size_y * max_partitions *
247 usc_min_output_registers_per_pix;
250 return max_partitions * 1024;
254 rogue_get_common_store_alloc_region_size(struct pvr_device *pvr_dev)
256 u32 common_store_size_in_dwords = 512 * 4 * 4;
257 u32 alloc_region_size;
259 PVR_FEATURE_VALUE(pvr_dev, common_store_size_in_dwords, &common_store_size_in_dwords);
261 alloc_region_size = common_store_size_in_dwords - (256U * 4U) -
262 rogue_get_common_store_partition_space_size(pvr_dev);
264 if (PVR_HAS_QUIRK(pvr_dev, 44079)) {
265 u32 common_store_split_point = (768U * 4U * 4U);
267 return min(common_store_split_point - (256U * 4U), alloc_region_size);
270 return alloc_region_size;
274 rogue_get_num_phantoms(struct pvr_device *pvr_dev)
276 u32 num_clusters = 1;
278 PVR_FEATURE_VALUE(pvr_dev, num_clusters, &num_clusters);
280 return ROGUE_REQ_NUM_PHANTOMS(num_clusters);
284 rogue_get_max_coeffs(struct pvr_device *pvr_dev)
286 u32 max_coeff_additional_portion = ROGUE_MAX_VERTEX_SHARED_REGISTERS;
287 u32 pending_allocation_shared_regs = 2U * 1024U;
288 u32 pending_allocation_coeff_regs = 0U;
289 u32 num_phantoms = rogue_get_num_phantoms(pvr_dev);
290 u32 tiles_in_flight = 0;
291 u32 max_coeff_pixel_portion;
293 PVR_FEATURE_VALUE(pvr_dev, isp_max_tiles_in_flight, &tiles_in_flight);
294 max_coeff_pixel_portion = DIV_ROUND_UP(tiles_in_flight, num_phantoms);
295 max_coeff_pixel_portion *= ROGUE_MAX_PIXEL_SHARED_REGISTERS;
298 * Compute tasks on cores with BRN48492 and without compute overlap may lock
299 * up without two additional lines of coeffs.
301 if (PVR_HAS_QUIRK(pvr_dev, 48492) && !PVR_HAS_FEATURE(pvr_dev, compute_overlap))
302 pending_allocation_coeff_regs = 2U * 1024U;
304 if (PVR_HAS_ENHANCEMENT(pvr_dev, 38748))
305 pending_allocation_shared_regs = 0;
307 if (PVR_HAS_ENHANCEMENT(pvr_dev, 38020))
308 max_coeff_additional_portion += ROGUE_MAX_COMPUTE_SHARED_REGISTERS;
310 return rogue_get_common_store_alloc_region_size(pvr_dev) + pending_allocation_coeff_regs -
311 (max_coeff_pixel_portion + max_coeff_additional_portion +
312 pending_allocation_shared_regs);
316 rogue_get_cdm_max_local_mem_size_regs(struct pvr_device *pvr_dev)
318 u32 available_coeffs_in_dwords = rogue_get_max_coeffs(pvr_dev);
320 if (PVR_HAS_QUIRK(pvr_dev, 48492) && PVR_HAS_FEATURE(pvr_dev, roguexe) &&
321 !PVR_HAS_FEATURE(pvr_dev, compute_overlap)) {
322 /* Driver must not use the 2 reserved lines. */
323 available_coeffs_in_dwords -= ROGUE_CSRM_LINE_SIZE_IN_DWORDS * 2;
327 * The maximum amount of local memory available to a kernel is the minimum
328 * of the total number of coefficient registers available and the max common
329 * store allocation size which can be made by the CDM.
331 * If any coeff lines are reserved for tessellation or pixel then we need to
332 * subtract those too.
334 return min(available_coeffs_in_dwords, (u32)ROGUE_MAX_PER_KERNEL_LOCAL_MEM_SIZE_REGS);
338 * pvr_dev_query_gpu_info_get()
339 * @pvr_dev: Device pointer.
340 * @args: [IN] Device query arguments containing a pointer to a userspace
341 * struct drm_pvr_dev_query_gpu_info.
343 * If the query object pointer is NULL, the size field is updated with the
344 * expected size of the query object.
347 * * 0 on success, or if size is requested using a NULL pointer, or
348 * * -%E2BIG if the indicated length of the allocation is less than is
349 * required to contain the copied data, or
350 * * -%EFAULT if local memory could not be copied to userspace.
353 pvr_dev_query_gpu_info_get(struct pvr_device *pvr_dev,
354 struct drm_pvr_ioctl_dev_query_args *args)
356 struct drm_pvr_dev_query_gpu_info gpu_info = {0};
359 if (!args->pointer) {
360 args->size = sizeof(struct drm_pvr_dev_query_gpu_info);
365 pvr_gpu_id_to_packed_bvnc(&pvr_dev->gpu_id);
366 gpu_info.num_phantoms = rogue_get_num_phantoms(pvr_dev);
368 err = PVR_UOBJ_SET(args->pointer, args->size, gpu_info);
372 if (args->size > sizeof(gpu_info))
373 args->size = sizeof(gpu_info);
378 * pvr_dev_query_runtime_info_get()
379 * @pvr_dev: Device pointer.
380 * @args: [IN] Device query arguments containing a pointer to a userspace
381 * struct drm_pvr_dev_query_runtime_info.
383 * If the query object pointer is NULL, the size field is updated with the
384 * expected size of the query object.
387 * * 0 on success, or if size is requested using a NULL pointer, or
388 * * -%E2BIG if the indicated length of the allocation is less than is
389 * required to contain the copied data, or
390 * * -%EFAULT if local memory could not be copied to userspace.
393 pvr_dev_query_runtime_info_get(struct pvr_device *pvr_dev,
394 struct drm_pvr_ioctl_dev_query_args *args)
396 struct drm_pvr_dev_query_runtime_info runtime_info = {0};
399 if (!args->pointer) {
400 args->size = sizeof(struct drm_pvr_dev_query_runtime_info);
404 runtime_info.free_list_min_pages =
405 pvr_get_free_list_min_pages(pvr_dev);
406 runtime_info.free_list_max_pages =
407 ROGUE_PM_MAX_FREELIST_SIZE / ROGUE_PM_PAGE_SIZE;
408 runtime_info.common_store_alloc_region_size =
409 rogue_get_common_store_alloc_region_size(pvr_dev);
410 runtime_info.common_store_partition_space_size =
411 rogue_get_common_store_partition_space_size(pvr_dev);
412 runtime_info.max_coeffs = rogue_get_max_coeffs(pvr_dev);
413 runtime_info.cdm_max_local_mem_size_regs =
414 rogue_get_cdm_max_local_mem_size_regs(pvr_dev);
416 err = PVR_UOBJ_SET(args->pointer, args->size, runtime_info);
420 if (args->size > sizeof(runtime_info))
421 args->size = sizeof(runtime_info);
426 * pvr_dev_query_quirks_get() - Unpack array of quirks at the address given
427 * in a struct drm_pvr_dev_query_quirks, or gets the amount of space required
429 * @pvr_dev: Device pointer.
430 * @args: [IN] Device query arguments containing a pointer to a userspace
431 * struct drm_pvr_dev_query_query_quirks.
433 * If the query object pointer is NULL, the size field is updated with the
434 * expected size of the query object.
435 * If the userspace pointer in the query object is NULL, or the count is
436 * short, no data is copied.
437 * The count field will be updated to that copied, or if either pointer is
438 * NULL, that which would have been copied.
439 * The size field in the query object will be updated to the size copied.
442 * * 0 on success, or if size/count is requested using a NULL pointer, or
443 * * -%EINVAL if args contained non-zero reserved fields, or
444 * * -%E2BIG if the indicated length of the allocation is less than is
445 * required to contain the copied data, or
446 * * -%EFAULT if local memory could not be copied to userspace.
449 pvr_dev_query_quirks_get(struct pvr_device *pvr_dev,
450 struct drm_pvr_ioctl_dev_query_args *args)
453 * @FIXME - hardcoding of numbers here is intended as an
454 * intermediate step so the UAPI can be fixed, but requires a
455 * a refactor in the future to store them in a more appropriate
458 static const u32 umd_quirks_musthave[] = {
463 static const u32 umd_quirks[] = {
467 struct drm_pvr_dev_query_quirks query;
468 u32 out[ARRAY_SIZE(umd_quirks_musthave) + ARRAY_SIZE(umd_quirks)];
469 size_t out_musthave_count = 0;
470 size_t out_count = 0;
473 if (!args->pointer) {
474 args->size = sizeof(struct drm_pvr_dev_query_quirks);
478 err = PVR_UOBJ_GET(query, args->size, args->pointer);
482 if (query._padding_c)
485 for (int i = 0; i < ARRAY_SIZE(umd_quirks_musthave); i++) {
486 if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks_musthave[i])) {
487 out[out_count++] = umd_quirks_musthave[i];
488 out_musthave_count++;
492 for (int i = 0; i < ARRAY_SIZE(umd_quirks); i++) {
493 if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks[i]))
494 out[out_count++] = umd_quirks[i];
499 if (query.count < out_count)
502 if (copy_to_user(u64_to_user_ptr(query.quirks), out,
503 out_count * sizeof(u32))) {
507 query.musthave_count = out_musthave_count;
510 query.count = out_count;
511 err = PVR_UOBJ_SET(args->pointer, args->size, query);
515 args->size = sizeof(query);
520 * pvr_dev_query_enhancements_get() - Unpack array of enhancements at the
521 * address given in a struct drm_pvr_dev_query_enhancements, or gets the amount
522 * of space required for it.
523 * @pvr_dev: Device pointer.
524 * @args: [IN] Device query arguments containing a pointer to a userspace
525 * struct drm_pvr_dev_query_enhancements.
527 * If the query object pointer is NULL, the size field is updated with the
528 * expected size of the query object.
529 * If the userspace pointer in the query object is NULL, or the count is
530 * short, no data is copied.
531 * The count field will be updated to that copied, or if either pointer is
532 * NULL, that which would have been copied.
533 * The size field in the query object will be updated to the size copied.
536 * * 0 on success, or if size/count is requested using a NULL pointer, or
537 * * -%EINVAL if args contained non-zero reserved fields, or
538 * * -%E2BIG if the indicated length of the allocation is less than is
539 * required to contain the copied data, or
540 * * -%EFAULT if local memory could not be copied to userspace.
543 pvr_dev_query_enhancements_get(struct pvr_device *pvr_dev,
544 struct drm_pvr_ioctl_dev_query_args *args)
547 * @FIXME - hardcoding of numbers here is intended as an
548 * intermediate step so the UAPI can be fixed, but requires a
549 * a refactor in the future to store them in a more appropriate
552 const u32 umd_enhancements[] = {
556 struct drm_pvr_dev_query_enhancements query;
557 u32 out[ARRAY_SIZE(umd_enhancements)];
561 if (!args->pointer) {
562 args->size = sizeof(struct drm_pvr_dev_query_enhancements);
566 err = PVR_UOBJ_GET(query, args->size, args->pointer);
570 if (query._padding_a)
572 if (query._padding_c)
575 for (int i = 0; i < ARRAY_SIZE(umd_enhancements); i++) {
576 if (pvr_device_has_uapi_enhancement(pvr_dev, umd_enhancements[i]))
577 out[out_idx++] = umd_enhancements[i];
580 if (!query.enhancements)
582 if (query.count < out_idx)
585 if (copy_to_user(u64_to_user_ptr(query.enhancements), out,
586 out_idx * sizeof(u32))) {
591 query.count = out_idx;
592 err = PVR_UOBJ_SET(args->pointer, args->size, query);
596 args->size = sizeof(query);
601 * pvr_ioctl_dev_query() - IOCTL to copy information about a device
602 * @drm_dev: [IN] DRM device.
603 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
604 * &struct drm_pvr_ioctl_dev_query_args.
605 * @file: [IN] DRM file private data.
607 * Called from userspace with %DRM_IOCTL_PVR_DEV_QUERY.
608 * If the given receiving struct pointer is NULL, or the indicated size is too
609 * small, the expected size of the struct type will be returned in the size
613 * * 0 on success or when fetching the size with args->pointer == NULL, or
614 * * -%E2BIG if the indicated size of the receiving struct is less than is
615 * required to contain the copied data, or
616 * * -%EINVAL if the indicated struct type is unknown, or
617 * * -%ENOMEM if local memory could not be allocated, or
618 * * -%EFAULT if local memory could not be copied to userspace.
621 pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args,
622 struct drm_file *file)
624 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
625 struct drm_pvr_ioctl_dev_query_args *args = raw_args;
629 if (!drm_dev_enter(drm_dev, &idx))
632 switch ((enum drm_pvr_dev_query)args->type) {
633 case DRM_PVR_DEV_QUERY_GPU_INFO_GET:
634 ret = pvr_dev_query_gpu_info_get(pvr_dev, args);
637 case DRM_PVR_DEV_QUERY_RUNTIME_INFO_GET:
638 ret = pvr_dev_query_runtime_info_get(pvr_dev, args);
641 case DRM_PVR_DEV_QUERY_QUIRKS_GET:
642 ret = pvr_dev_query_quirks_get(pvr_dev, args);
645 case DRM_PVR_DEV_QUERY_ENHANCEMENTS_GET:
646 ret = pvr_dev_query_enhancements_get(pvr_dev, args);
649 case DRM_PVR_DEV_QUERY_HEAP_INFO_GET:
650 ret = pvr_heap_info_get(pvr_dev, args);
653 case DRM_PVR_DEV_QUERY_STATIC_DATA_AREAS_GET:
654 ret = pvr_static_data_areas_get(pvr_dev, args);
664 * pvr_ioctl_create_context() - IOCTL to create a context
665 * @drm_dev: [IN] DRM device.
666 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
667 * &struct drm_pvr_ioctl_create_context_args.
668 * @file: [IN] DRM file private data.
670 * Called from userspace with %DRM_IOCTL_PVR_CREATE_CONTEXT.
674 * * -%EINVAL if provided arguments are invalid, or
675 * * -%EFAULT if arguments can't be copied from userspace, or
676 * * Any error returned by pvr_create_render_context().
679 pvr_ioctl_create_context(struct drm_device *drm_dev, void *raw_args,
680 struct drm_file *file)
682 struct drm_pvr_ioctl_create_context_args *args = raw_args;
683 struct pvr_file *pvr_file = file->driver_priv;
687 if (!drm_dev_enter(drm_dev, &idx))
690 ret = pvr_context_create(pvr_file, args);
698 * pvr_ioctl_destroy_context() - IOCTL to destroy a context
699 * @drm_dev: [IN] DRM device.
700 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
701 * &struct drm_pvr_ioctl_destroy_context_args.
702 * @file: [IN] DRM file private data.
704 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_CONTEXT.
708 * * -%EINVAL if context not in context list.
711 pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args,
712 struct drm_file *file)
714 struct drm_pvr_ioctl_destroy_context_args *args = raw_args;
715 struct pvr_file *pvr_file = file->driver_priv;
717 if (args->_padding_4)
720 return pvr_context_destroy(pvr_file, args->handle);
724 * pvr_ioctl_create_free_list() - IOCTL to create a free list
725 * @drm_dev: [IN] DRM device.
726 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
727 * &struct drm_pvr_ioctl_create_free_list_args.
728 * @file: [IN] DRM file private data.
730 * Called from userspace with %DRM_IOCTL_PVR_CREATE_FREE_LIST.
734 * * Any error returned by pvr_free_list_create().
737 pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args,
738 struct drm_file *file)
740 struct drm_pvr_ioctl_create_free_list_args *args = raw_args;
741 struct pvr_file *pvr_file = to_pvr_file(file);
742 struct pvr_free_list *free_list;
746 if (!drm_dev_enter(drm_dev, &idx))
749 free_list = pvr_free_list_create(pvr_file, args);
750 if (IS_ERR(free_list)) {
751 err = PTR_ERR(free_list);
752 goto err_drm_dev_exit;
755 /* Allocate object handle for userspace. */
756 err = xa_alloc(&pvr_file->free_list_handles,
769 pvr_free_list_put(free_list);
778 * pvr_ioctl_destroy_free_list() - IOCTL to destroy a free list
779 * @drm_dev: [IN] DRM device.
780 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
781 * &struct drm_pvr_ioctl_destroy_free_list_args.
782 * @file: [IN] DRM file private data.
784 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_FREE_LIST.
788 * * -%EINVAL if free list not in object list.
791 pvr_ioctl_destroy_free_list(struct drm_device *drm_dev, void *raw_args,
792 struct drm_file *file)
794 struct drm_pvr_ioctl_destroy_free_list_args *args = raw_args;
795 struct pvr_file *pvr_file = to_pvr_file(file);
796 struct pvr_free_list *free_list;
798 if (args->_padding_4)
801 free_list = xa_erase(&pvr_file->free_list_handles, args->handle);
805 pvr_free_list_put(free_list);
810 * pvr_ioctl_create_hwrt_dataset() - IOCTL to create a HWRT dataset
811 * @drm_dev: [IN] DRM device.
812 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
813 * &struct drm_pvr_ioctl_create_hwrt_dataset_args.
814 * @file: [IN] DRM file private data.
816 * Called from userspace with %DRM_IOCTL_PVR_CREATE_HWRT_DATASET.
820 * * Any error returned by pvr_hwrt_dataset_create().
823 pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
824 struct drm_file *file)
826 struct drm_pvr_ioctl_create_hwrt_dataset_args *args = raw_args;
827 struct pvr_file *pvr_file = to_pvr_file(file);
828 struct pvr_hwrt_dataset *hwrt;
832 if (!drm_dev_enter(drm_dev, &idx))
835 hwrt = pvr_hwrt_dataset_create(pvr_file, args);
838 goto err_drm_dev_exit;
841 /* Allocate object handle for userspace. */
842 err = xa_alloc(&pvr_file->hwrt_handles,
855 pvr_hwrt_dataset_put(hwrt);
864 * pvr_ioctl_destroy_hwrt_dataset() - IOCTL to destroy a HWRT dataset
865 * @drm_dev: [IN] DRM device.
866 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
867 * &struct drm_pvr_ioctl_destroy_hwrt_dataset_args.
868 * @file: [IN] DRM file private data.
870 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_HWRT_DATASET.
874 * * -%EINVAL if HWRT dataset not in object list.
877 pvr_ioctl_destroy_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
878 struct drm_file *file)
880 struct drm_pvr_ioctl_destroy_hwrt_dataset_args *args = raw_args;
881 struct pvr_file *pvr_file = to_pvr_file(file);
882 struct pvr_hwrt_dataset *hwrt;
884 if (args->_padding_4)
887 hwrt = xa_erase(&pvr_file->hwrt_handles, args->handle);
891 pvr_hwrt_dataset_put(hwrt);
896 * pvr_ioctl_create_vm_context() - IOCTL to create a VM context
897 * @drm_dev: [IN] DRM device.
898 * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
899 * &struct drm_pvr_ioctl_create_vm_context_args.
900 * @file: [IN] DRM file private data.
902 * Called from userspace with %DRM_IOCTL_PVR_CREATE_VM_CONTEXT.
906 * * Any error returned by pvr_vm_create_context().
909 pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args,
910 struct drm_file *file)
912 struct drm_pvr_ioctl_create_vm_context_args *args = raw_args;
913 struct pvr_file *pvr_file = to_pvr_file(file);
914 struct pvr_vm_context *vm_ctx;
918 if (!drm_dev_enter(drm_dev, &idx))
921 if (args->_padding_4) {
923 goto err_drm_dev_exit;
926 vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true);
927 if (IS_ERR(vm_ctx)) {
928 err = PTR_ERR(vm_ctx);
929 goto err_drm_dev_exit;
932 /* Allocate object handle for userspace. */
933 err = xa_alloc(&pvr_file->vm_ctx_handles,
946 pvr_vm_context_put(vm_ctx);
955 * pvr_ioctl_destroy_vm_context() - IOCTL to destroy a VM context
956 * @drm_dev: [IN] DRM device.
957 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
958 * &struct drm_pvr_ioctl_destroy_vm_context_args.
959 * @file: [IN] DRM file private data.
961 * Called from userspace with %DRM_IOCTL_PVR_DESTROY_VM_CONTEXT.
964 * * 0 on success, or
965 * * -%EINVAL if object not in object list.
968 pvr_ioctl_destroy_vm_context(struct drm_device *drm_dev, void *raw_args,
969 struct drm_file *file)
971 struct drm_pvr_ioctl_destroy_vm_context_args *args = raw_args;
972 struct pvr_file *pvr_file = to_pvr_file(file);
973 struct pvr_vm_context *vm_ctx;
975 if (args->_padding_4)
978 vm_ctx = xa_erase(&pvr_file->vm_ctx_handles, args->handle);
982 pvr_vm_context_put(vm_ctx);
987 * pvr_ioctl_vm_map() - IOCTL to map buffer to GPU address space.
988 * @drm_dev: [IN] DRM device.
989 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
990 * &struct drm_pvr_ioctl_vm_map_args.
991 * @file: [IN] DRM file private data.
993 * Called from userspace with %DRM_IOCTL_PVR_VM_MAP.
997 * * -%EINVAL if &drm_pvr_ioctl_vm_op_map_args.flags is not zero,
998 * * -%EINVAL if the bounds specified by &drm_pvr_ioctl_vm_op_map_args.offset
999 * and &drm_pvr_ioctl_vm_op_map_args.size are not valid or do not fall
1000 * within the buffer object specified by
1001 * &drm_pvr_ioctl_vm_op_map_args.handle,
1002 * * -%EINVAL if the bounds specified by
1003 * &drm_pvr_ioctl_vm_op_map_args.device_addr and
1004 * &drm_pvr_ioctl_vm_op_map_args.size do not form a valid device-virtual
1005 * address range which falls entirely within a single heap, or
1006 * * -%ENOENT if &drm_pvr_ioctl_vm_op_map_args.handle does not refer to a
1007 * valid PowerVR buffer object.
1010 pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args,
1011 struct drm_file *file)
1013 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1014 struct drm_pvr_ioctl_vm_map_args *args = raw_args;
1015 struct pvr_file *pvr_file = to_pvr_file(file);
1016 struct pvr_vm_context *vm_ctx;
1018 struct pvr_gem_object *pvr_obj;
1019 size_t pvr_obj_size;
1021 u64 offset_plus_size;
1025 if (!drm_dev_enter(drm_dev, &idx))
1028 /* Initial validation of args. */
1029 if (args->_padding_14) {
1031 goto err_drm_dev_exit;
1034 if (args->flags != 0 ||
1035 check_add_overflow(args->offset, args->size, &offset_plus_size) ||
1036 !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) {
1038 goto err_drm_dev_exit;
1041 vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1044 goto err_drm_dev_exit;
1047 pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
1050 goto err_put_vm_context;
1053 pvr_obj_size = pvr_gem_object_size(pvr_obj);
1056 * Validate offset and size args. The alignment of these will be
1057 * checked when mapping; for now just check that they're within valid
1060 if (args->offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) {
1062 goto err_put_pvr_object;
1065 err = pvr_vm_map(vm_ctx, pvr_obj, args->offset,
1066 args->device_addr, args->size);
1068 goto err_put_pvr_object;
1071 * In order to set up the mapping, we needed a reference to &pvr_obj.
1072 * However, pvr_vm_map() obtains and stores its own reference, so we
1073 * must release ours before returning.
1077 pvr_gem_object_put(pvr_obj);
1080 pvr_vm_context_put(vm_ctx);
1089 * pvr_ioctl_vm_unmap() - IOCTL to unmap buffer from GPU address space.
1090 * @drm_dev: [IN] DRM device.
1091 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1092 * &struct drm_pvr_ioctl_vm_unmap_args.
1093 * @file: [IN] DRM file private data.
1095 * Called from userspace with %DRM_IOCTL_PVR_VM_UNMAP.
1099 * * -%EINVAL if &drm_pvr_ioctl_vm_op_unmap_args.device_addr is not a valid
1100 * device page-aligned device-virtual address, or
1101 * * -%ENOENT if there is currently no PowerVR buffer object mapped at
1102 * &drm_pvr_ioctl_vm_op_unmap_args.device_addr.
1105 pvr_ioctl_vm_unmap(struct drm_device *drm_dev, void *raw_args,
1106 struct drm_file *file)
1108 struct drm_pvr_ioctl_vm_unmap_args *args = raw_args;
1109 struct pvr_file *pvr_file = to_pvr_file(file);
1110 struct pvr_vm_context *vm_ctx;
1113 /* Initial validation of args. */
1114 if (args->_padding_4)
1117 vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
1121 err = pvr_vm_unmap(vm_ctx, args->device_addr, args->size);
1123 pvr_vm_context_put(vm_ctx);
1129 * pvr_ioctl_submit_job() - IOCTL to submit a job to the GPU
1130 * @drm_dev: [IN] DRM device.
1131 * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
1132 * &struct drm_pvr_ioctl_submit_job_args.
1133 * @file: [IN] DRM file private data.
1135 * Called from userspace with %DRM_IOCTL_PVR_SUBMIT_JOB.
1138 * * 0 on success, or
1139 * * -%EINVAL if arguments are invalid.
1142 pvr_ioctl_submit_jobs(struct drm_device *drm_dev, void *raw_args,
1143 struct drm_file *file)
1145 struct drm_pvr_ioctl_submit_jobs_args *args = raw_args;
1146 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1147 struct pvr_file *pvr_file = to_pvr_file(file);
1151 if (!drm_dev_enter(drm_dev, &idx))
1154 err = pvr_submit_jobs(pvr_dev, pvr_file, args);
1162 pvr_get_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, void *out)
1164 if (usr_stride < min_stride)
1167 return copy_struct_from_user(out, obj_size, u64_to_user_ptr(usr_ptr), usr_stride);
1171 pvr_set_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, const void *in)
1173 if (usr_stride < min_stride)
1176 if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_stride, obj_size)))
1179 if (usr_stride > obj_size &&
1180 clear_user(u64_to_user_ptr(usr_ptr + obj_size), usr_stride - obj_size)) {
1188 pvr_get_uobj_array(const struct drm_pvr_obj_array *in, u32 min_stride, u32 obj_size, void **out)
1193 if (in->stride < min_stride)
1199 out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
1203 if (obj_size == in->stride) {
1204 if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
1205 (unsigned long)obj_size * in->count))
1208 void __user *in_ptr = u64_to_user_ptr(in->array);
1209 void *out_ptr = out_alloc;
1211 for (u32 i = 0; i < in->count; i++) {
1212 ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
1216 out_ptr += obj_size;
1217 in_ptr += in->stride;
1231 pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_size,
1234 if (out->stride < min_stride)
1240 if (obj_size == out->stride) {
1241 if (copy_to_user(u64_to_user_ptr(out->array), in,
1242 (unsigned long)obj_size * out->count))
1245 u32 cpy_elem_size = min_t(u32, out->stride, obj_size);
1246 void __user *out_ptr = u64_to_user_ptr(out->array);
1247 const void *in_ptr = in;
1249 for (u32 i = 0; i < out->count; i++) {
1250 if (copy_to_user(out_ptr, in_ptr, cpy_elem_size))
1253 out_ptr += obj_size;
1254 in_ptr += out->stride;
1257 if (out->stride > obj_size &&
1258 clear_user(u64_to_user_ptr(out->array + obj_size),
1259 out->stride - obj_size)) {
1267 #define DRM_PVR_IOCTL(_name, _func, _flags) \
1268 DRM_IOCTL_DEF_DRV(PVR_##_name, pvr_ioctl_##_func, _flags)
1270 /* clang-format off */
1272 static const struct drm_ioctl_desc pvr_drm_driver_ioctls[] = {
1273 DRM_PVR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
1274 DRM_PVR_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW),
1275 DRM_PVR_IOCTL(GET_BO_MMAP_OFFSET, get_bo_mmap_offset, DRM_RENDER_ALLOW),
1276 DRM_PVR_IOCTL(CREATE_VM_CONTEXT, create_vm_context, DRM_RENDER_ALLOW),
1277 DRM_PVR_IOCTL(DESTROY_VM_CONTEXT, destroy_vm_context, DRM_RENDER_ALLOW),
1278 DRM_PVR_IOCTL(VM_MAP, vm_map, DRM_RENDER_ALLOW),
1279 DRM_PVR_IOCTL(VM_UNMAP, vm_unmap, DRM_RENDER_ALLOW),
1280 DRM_PVR_IOCTL(CREATE_CONTEXT, create_context, DRM_RENDER_ALLOW),
1281 DRM_PVR_IOCTL(DESTROY_CONTEXT, destroy_context, DRM_RENDER_ALLOW),
1282 DRM_PVR_IOCTL(CREATE_FREE_LIST, create_free_list, DRM_RENDER_ALLOW),
1283 DRM_PVR_IOCTL(DESTROY_FREE_LIST, destroy_free_list, DRM_RENDER_ALLOW),
1284 DRM_PVR_IOCTL(CREATE_HWRT_DATASET, create_hwrt_dataset, DRM_RENDER_ALLOW),
1285 DRM_PVR_IOCTL(DESTROY_HWRT_DATASET, destroy_hwrt_dataset, DRM_RENDER_ALLOW),
1286 DRM_PVR_IOCTL(SUBMIT_JOBS, submit_jobs, DRM_RENDER_ALLOW),
1289 /* clang-format on */
1291 #undef DRM_PVR_IOCTL
1294 * pvr_drm_driver_open() - Driver callback when a new &struct drm_file is opened
1295 * @drm_dev: [IN] DRM device.
1296 * @file: [IN] DRM file private data.
1298 * Allocates powervr-specific file private data (&struct pvr_file).
1300 * Registered in &pvr_drm_driver.
1304 * * -%ENOMEM if the allocation of a &struct ipvr_file fails, or
1305 * * Any error returned by pvr_memory_context_init().
1308 pvr_drm_driver_open(struct drm_device *drm_dev, struct drm_file *file)
1310 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1311 struct pvr_file *pvr_file;
1313 pvr_file = kzalloc(sizeof(*pvr_file), GFP_KERNEL);
1318 * Store reference to base DRM file private data for use by
1321 pvr_file->file = file;
1324 * Store reference to powervr-specific outer device struct in file
1325 * private data for convenient access.
1327 pvr_file->pvr_dev = pvr_dev;
1329 xa_init_flags(&pvr_file->ctx_handles, XA_FLAGS_ALLOC1);
1330 xa_init_flags(&pvr_file->free_list_handles, XA_FLAGS_ALLOC1);
1331 xa_init_flags(&pvr_file->hwrt_handles, XA_FLAGS_ALLOC1);
1332 xa_init_flags(&pvr_file->vm_ctx_handles, XA_FLAGS_ALLOC1);
1335 * Store reference to powervr-specific file private data in DRM file
1338 file->driver_priv = pvr_file;
1344 * pvr_drm_driver_postclose() - One of the driver callbacks when a &struct
1345 * drm_file is closed.
1346 * @drm_dev: [IN] DRM device (unused).
1347 * @file: [IN] DRM file private data.
1349 * Frees powervr-specific file private data (&struct pvr_file).
1351 * Registered in &pvr_drm_driver.
1354 pvr_drm_driver_postclose(__always_unused struct drm_device *drm_dev,
1355 struct drm_file *file)
1357 struct pvr_file *pvr_file = to_pvr_file(file);
1359 /* Kill remaining contexts. */
1360 pvr_destroy_contexts_for_file(pvr_file);
1362 /* Drop references on any remaining objects. */
1363 pvr_destroy_free_lists_for_file(pvr_file);
1364 pvr_destroy_hwrt_datasets_for_file(pvr_file);
1365 pvr_destroy_vm_contexts_for_file(pvr_file);
1368 file->driver_priv = NULL;
1371 DEFINE_DRM_GEM_FOPS(pvr_drm_driver_fops);
1373 static struct drm_driver pvr_drm_driver = {
1374 .driver_features = DRIVER_GEM | DRIVER_GEM_GPUVA | DRIVER_RENDER |
1375 DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE,
1376 .open = pvr_drm_driver_open,
1377 .postclose = pvr_drm_driver_postclose,
1378 .ioctls = pvr_drm_driver_ioctls,
1379 .num_ioctls = ARRAY_SIZE(pvr_drm_driver_ioctls),
1380 .fops = &pvr_drm_driver_fops,
1381 #if defined(CONFIG_DEBUG_FS)
1382 .debugfs_init = pvr_debugfs_init,
1385 .name = PVR_DRIVER_NAME,
1386 .desc = PVR_DRIVER_DESC,
1387 .date = PVR_DRIVER_DATE,
1388 .major = PVR_DRIVER_MAJOR,
1389 .minor = PVR_DRIVER_MINOR,
1390 .patchlevel = PVR_DRIVER_PATCHLEVEL,
1392 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
1393 .gem_create_object = pvr_gem_create_object,
1397 pvr_probe(struct platform_device *plat_dev)
1399 struct pvr_device *pvr_dev;
1400 struct drm_device *drm_dev;
1403 pvr_dev = devm_drm_dev_alloc(&plat_dev->dev, &pvr_drm_driver,
1404 struct pvr_device, base);
1405 if (IS_ERR(pvr_dev))
1406 return PTR_ERR(pvr_dev);
1408 drm_dev = &pvr_dev->base;
1410 platform_set_drvdata(plat_dev, drm_dev);
1412 init_rwsem(&pvr_dev->reset_sem);
1414 pvr_context_device_init(pvr_dev);
1416 err = pvr_queue_device_init(pvr_dev);
1418 goto err_context_fini;
1420 devm_pm_runtime_enable(&plat_dev->dev);
1421 pm_runtime_mark_last_busy(&plat_dev->dev);
1423 pm_runtime_set_autosuspend_delay(&plat_dev->dev, 50);
1424 pm_runtime_use_autosuspend(&plat_dev->dev);
1425 pvr_watchdog_init(pvr_dev);
1427 err = pvr_device_init(pvr_dev);
1429 goto err_watchdog_fini;
1431 err = drm_dev_register(drm_dev, 0);
1433 goto err_device_fini;
1435 xa_init_flags(&pvr_dev->free_list_ids, XA_FLAGS_ALLOC1);
1436 xa_init_flags(&pvr_dev->job_ids, XA_FLAGS_ALLOC1);
1441 pvr_device_fini(pvr_dev);
1444 pvr_watchdog_fini(pvr_dev);
1446 pvr_queue_device_fini(pvr_dev);
1449 pvr_context_device_fini(pvr_dev);
1454 static void pvr_remove(struct platform_device *plat_dev)
1456 struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
1457 struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
1459 WARN_ON(!xa_empty(&pvr_dev->job_ids));
1460 WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
1462 xa_destroy(&pvr_dev->job_ids);
1463 xa_destroy(&pvr_dev->free_list_ids);
1465 pm_runtime_suspend(drm_dev->dev);
1466 pvr_device_fini(pvr_dev);
1467 drm_dev_unplug(drm_dev);
1468 pvr_watchdog_fini(pvr_dev);
1469 pvr_queue_device_fini(pvr_dev);
1470 pvr_context_device_fini(pvr_dev);
1473 static const struct of_device_id dt_match[] = {
1474 { .compatible = "img,img-axe", .data = NULL },
1477 MODULE_DEVICE_TABLE(of, dt_match);
1479 static const struct dev_pm_ops pvr_pm_ops = {
1480 RUNTIME_PM_OPS(pvr_power_device_suspend, pvr_power_device_resume, pvr_power_device_idle)
1483 static struct platform_driver pvr_driver = {
1485 .remove_new = pvr_remove,
1487 .name = PVR_DRIVER_NAME,
1489 .of_match_table = dt_match,
1492 module_platform_driver(pvr_driver);
1494 MODULE_AUTHOR("Imagination Technologies Ltd.");
1495 MODULE_DESCRIPTION(PVR_DRIVER_DESC);
1496 MODULE_LICENSE("Dual MIT/GPL");
1497 MODULE_IMPORT_NS(DMA_BUF);
1498 MODULE_FIRMWARE("powervr/rogue_33.15.11.3_v1.fw");