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 * .. note:: 88 * 89 * This member should not be accessed directly, but instead by 90 * calling shmem_gem_from_pvr_gem(). 91 */ 92 struct drm_gem_shmem_object base; 93 94 /** 95 * @flags: Options set at creation-time. Some of these options apply to 96 * the creation operation itself (which are stored here for reference) 97 * with the remainder used for mapping options to both the device and 98 * CPU. These are used every time this object is mapped, but may be 99 * changed after creation. 100 * 101 * Must be a combination of DRM_PVR_BO_* and/or PVR_BO_* flags. 102 */ 103 u64 flags; 104 105 }; 106 107 static_assert(offsetof(struct pvr_gem_object, base) == 0, 108 "offsetof(struct pvr_gem_object, base) not zero"); 109 110 #define shmem_gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base) 111 112 #define shmem_gem_to_pvr_gem(shmem_obj) container_of_const(shmem_obj, struct pvr_gem_object, base) 113 114 #define gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base.base) 115 116 #define gem_to_pvr_gem(gem_obj) container_of_const(gem_obj, struct pvr_gem_object, base.base) 117 118 /* Functions defined in pvr_gem.c */ 119 120 struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size); 121 122 struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, 123 size_t size, u64 flags); 124 125 int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, 126 struct pvr_file *pvr_file, u32 *handle); 127 struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file, 128 u32 handle); 129 130 static __always_inline struct sg_table * 131 pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) 132 { 133 return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj)); 134 } 135 136 void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj); 137 void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj); 138 139 int pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, 140 dma_addr_t *dma_addr_out); 141 142 /** 143 * pvr_gem_object_get() - Acquire reference on pvr_gem_object 144 * @pvr_obj: Pointer to object to acquire reference on. 145 */ 146 static __always_inline void 147 pvr_gem_object_get(struct pvr_gem_object *pvr_obj) 148 { 149 drm_gem_object_get(gem_from_pvr_gem(pvr_obj)); 150 } 151 152 /** 153 * pvr_gem_object_put() - Release reference on pvr_gem_object 154 * @pvr_obj: Pointer to object to release reference on. 155 */ 156 static __always_inline void 157 pvr_gem_object_put(struct pvr_gem_object *pvr_obj) 158 { 159 drm_gem_object_put(gem_from_pvr_gem(pvr_obj)); 160 } 161 162 static __always_inline size_t 163 pvr_gem_object_size(struct pvr_gem_object *pvr_obj) 164 { 165 return gem_from_pvr_gem(pvr_obj)->size; 166 } 167 168 #endif /* PVR_GEM_H */ 169