| 1 | /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ |
| 2 | /* Copyright (c) 2023 Imagination Technologies Ltd. */ |
| 3 | |
| 4 | #ifndef PVR_GEM_H |
| 5 | #define PVR_GEM_H |
| 6 | |
| 7 | #include "pvr_rogue_heap_config.h" |
| 8 | #include "pvr_rogue_meta.h" |
| 9 | |
| 10 | #include <uapi/drm/pvr_drm.h> |
| 11 | |
| 12 | #include <drm/drm_gem.h> |
| 13 | #include <drm/drm_gem_shmem_helper.h> |
| 14 | #include <drm/drm_mm.h> |
| 15 | |
| 16 | #include <linux/bitfield.h> |
| 17 | #include <linux/bits.h> |
| 18 | #include <linux/const.h> |
| 19 | #include <linux/compiler_attributes.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/refcount.h> |
| 23 | #include <linux/scatterlist.h> |
| 24 | #include <linux/sizes.h> |
| 25 | #include <linux/types.h> |
| 26 | |
| 27 | /* Forward declaration from "pvr_device.h". */ |
| 28 | struct pvr_device; |
| 29 | struct pvr_file; |
| 30 | |
| 31 | /** |
| 32 | * DOC: Flags for DRM_IOCTL_PVR_CREATE_BO (kernel-only) |
| 33 | * |
| 34 | * Kernel-only values allowed in &pvr_gem_object->flags. The majority of options |
| 35 | * for this field are specified in the UAPI header "pvr_drm.h" with a |
| 36 | * DRM_PVR_BO_ prefix. To distinguish these internal options (which must exist |
| 37 | * in ranges marked as "reserved" in the UAPI header), we drop the DRM prefix. |
| 38 | * The public options should be used directly, DRM prefix and all. |
| 39 | * |
| 40 | * To avoid potentially confusing gaps in the UAPI options, these kernel-only |
| 41 | * options are specified "in reverse", starting at bit 63. |
| 42 | * |
| 43 | * We use "reserved" to refer to bits defined here and not exposed in the UAPI. |
| 44 | * Bits not defined anywhere are "undefined". |
| 45 | * |
| 46 | * CPU mapping options |
| 47 | * :PVR_BO_CPU_CACHED: By default, all GEM objects are mapped write-combined on the CPU. Set |
| 48 | * this flag to override this behaviour and map the object cached. If the dma_coherent |
| 49 | * property is present in devicetree, all allocations will be mapped as if this flag was set. |
| 50 | * This does not require any additional consideration at allocation time. |
| 51 | * |
| 52 | * Firmware options |
| 53 | * :PVR_BO_FW_NO_CLEAR_ON_RESET: By default, all FW objects are cleared and reinitialised on hard |
| 54 | * reset. Set this flag to override this behaviour and preserve buffer contents on reset. |
| 55 | */ |
| 56 | #define PVR_BO_CPU_CACHED BIT_ULL(63) |
| 57 | |
| 58 | #define PVR_BO_FW_NO_CLEAR_ON_RESET BIT_ULL(62) |
| 59 | |
| 60 | #define PVR_BO_KERNEL_FLAGS_MASK (PVR_BO_CPU_CACHED | PVR_BO_FW_NO_CLEAR_ON_RESET) |
| 61 | |
| 62 | /* Bits 61..3 are undefined. */ |
| 63 | /* Bits 2..0 are defined in the UAPI. */ |
| 64 | |
| 65 | /* Other utilities. */ |
| 66 | #define PVR_BO_UNDEFINED_MASK ~(PVR_BO_KERNEL_FLAGS_MASK | DRM_PVR_BO_FLAGS_MASK) |
| 67 | |
| 68 | /* |
| 69 | * All firmware-mapped memory uses (mostly) the same flags. Specifically, |
| 70 | * firmware-mapped memory should be: |
| 71 | * * Read/write on the device, |
| 72 | * * Read/write on the CPU, and |
| 73 | * * Write-combined on the CPU. |
| 74 | * |
| 75 | * The only variation is in caching on the device. |
| 76 | */ |
| 77 | #define PVR_BO_FW_FLAGS_DEVICE_CACHED (ULL(0)) |
| 78 | #define PVR_BO_FW_FLAGS_DEVICE_UNCACHED DRM_PVR_BO_BYPASS_DEVICE_CACHE |
| 79 | |
| 80 | /** |
| 81 | * struct pvr_gem_object - powervr-specific wrapper for &struct drm_gem_object |
| 82 | */ |
| 83 | struct pvr_gem_object { |
| 84 | /** |
| 85 | * @base: The underlying &struct drm_gem_shmem_object. |
| 86 | * |
| 87 | * Do not access this member directly, instead call |
| 88 | * shem_gem_from_pvr_gem(). |
| 89 | */ |
| 90 | struct drm_gem_shmem_object base; |
| 91 | |
| 92 | /** |
| 93 | * @flags: Options set at creation-time. Some of these options apply to |
| 94 | * the creation operation itself (which are stored here for reference) |
| 95 | * with the remainder used for mapping options to both the device and |
| 96 | * CPU. These are used every time this object is mapped, but may be |
| 97 | * changed after creation. |
| 98 | * |
| 99 | * Must be a combination of DRM_PVR_BO_* and/or PVR_BO_* flags. |
| 100 | * |
| 101 | * .. note:: |
| 102 | * |
| 103 | * This member is declared const to indicate that none of these |
| 104 | * options may change or be changed throughout the object's |
| 105 | * lifetime. |
| 106 | */ |
| 107 | u64 flags; |
| 108 | |
| 109 | }; |
| 110 | |
| 111 | static_assert(offsetof(struct pvr_gem_object, base) == 0, |
| 112 | "offsetof(struct pvr_gem_object, base) not zero" ); |
| 113 | |
| 114 | #define shmem_gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base) |
| 115 | |
| 116 | #define shmem_gem_to_pvr_gem(shmem_obj) container_of_const(shmem_obj, struct pvr_gem_object, base) |
| 117 | |
| 118 | #define gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base.base) |
| 119 | |
| 120 | #define gem_to_pvr_gem(gem_obj) container_of_const(gem_obj, struct pvr_gem_object, base.base) |
| 121 | |
| 122 | /* Functions defined in pvr_gem.c */ |
| 123 | |
| 124 | struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size); |
| 125 | |
| 126 | struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, |
| 127 | size_t size, u64 flags); |
| 128 | |
| 129 | int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, |
| 130 | struct pvr_file *pvr_file, u32 *handle); |
| 131 | struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file, |
| 132 | u32 handle); |
| 133 | |
| 134 | static __always_inline struct sg_table * |
| 135 | pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) |
| 136 | { |
| 137 | return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj)); |
| 138 | } |
| 139 | |
| 140 | void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj); |
| 141 | void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj); |
| 142 | |
| 143 | int pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, |
| 144 | dma_addr_t *dma_addr_out); |
| 145 | |
| 146 | /** |
| 147 | * pvr_gem_object_get() - Acquire reference on pvr_gem_object |
| 148 | * @pvr_obj: Pointer to object to acquire reference on. |
| 149 | */ |
| 150 | static __always_inline void |
| 151 | pvr_gem_object_get(struct pvr_gem_object *pvr_obj) |
| 152 | { |
| 153 | drm_gem_object_get(gem_from_pvr_gem(pvr_obj)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * pvr_gem_object_put() - Release reference on pvr_gem_object |
| 158 | * @pvr_obj: Pointer to object to release reference on. |
| 159 | */ |
| 160 | static __always_inline void |
| 161 | pvr_gem_object_put(struct pvr_gem_object *pvr_obj) |
| 162 | { |
| 163 | drm_gem_object_put(gem_from_pvr_gem(pvr_obj)); |
| 164 | } |
| 165 | |
| 166 | static __always_inline size_t |
| 167 | pvr_gem_object_size(struct pvr_gem_object *pvr_obj) |
| 168 | { |
| 169 | return gem_from_pvr_gem(pvr_obj)->size; |
| 170 | } |
| 171 | |
| 172 | #endif /* PVR_GEM_H */ |
| 173 | |