xref: /linux/drivers/gpu/drm/imagination/pvr_gem.h (revision 021bc4b9)
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 this
48  *       flag to override this behaviour and map the object cached.
49  *
50  * Firmware options
51  *    :PVR_BO_FW_NO_CLEAR_ON_RESET: By default, all FW objects are cleared and reinitialised on hard
52  *       reset. Set this flag to override this behaviour and preserve buffer contents on reset.
53  */
54 #define PVR_BO_CPU_CACHED BIT_ULL(63)
55 
56 #define PVR_BO_FW_NO_CLEAR_ON_RESET BIT_ULL(62)
57 
58 #define PVR_BO_KERNEL_FLAGS_MASK (PVR_BO_CPU_CACHED | PVR_BO_FW_NO_CLEAR_ON_RESET)
59 
60 /* Bits 61..3 are undefined. */
61 /* Bits 2..0 are defined in the UAPI. */
62 
63 /* Other utilities. */
64 #define PVR_BO_UNDEFINED_MASK ~(PVR_BO_KERNEL_FLAGS_MASK | DRM_PVR_BO_FLAGS_MASK)
65 
66 /*
67  * All firmware-mapped memory uses (mostly) the same flags. Specifically,
68  * firmware-mapped memory should be:
69  *  * Read/write on the device,
70  *  * Read/write on the CPU, and
71  *  * Write-combined on the CPU.
72  *
73  * The only variation is in caching on the device.
74  */
75 #define PVR_BO_FW_FLAGS_DEVICE_CACHED (ULL(0))
76 #define PVR_BO_FW_FLAGS_DEVICE_UNCACHED DRM_PVR_BO_BYPASS_DEVICE_CACHE
77 
78 /**
79  * struct pvr_gem_object - powervr-specific wrapper for &struct drm_gem_object
80  */
81 struct pvr_gem_object {
82 	/**
83 	 * @base: The underlying &struct drm_gem_shmem_object.
84 	 *
85 	 * Do not access this member directly, instead call
86 	 * shem_gem_from_pvr_gem().
87 	 */
88 	struct drm_gem_shmem_object base;
89 
90 	/**
91 	 * @flags: Options set at creation-time. Some of these options apply to
92 	 * the creation operation itself (which are stored here for reference)
93 	 * with the remainder used for mapping options to both the device and
94 	 * CPU. These are used every time this object is mapped, but may be
95 	 * changed after creation.
96 	 *
97 	 * Must be a combination of DRM_PVR_BO_* and/or PVR_BO_* flags.
98 	 *
99 	 * .. note::
100 	 *
101 	 *    This member is declared const to indicate that none of these
102 	 *    options may change or be changed throughout the object's
103 	 *    lifetime.
104 	 */
105 	u64 flags;
106 
107 };
108 
109 static_assert(offsetof(struct pvr_gem_object, base) == 0,
110 	      "offsetof(struct pvr_gem_object, base) not zero");
111 
112 #define shmem_gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base)
113 
114 #define shmem_gem_to_pvr_gem(shmem_obj) container_of_const(shmem_obj, struct pvr_gem_object, base)
115 
116 #define gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base.base)
117 
118 #define gem_to_pvr_gem(gem_obj) container_of_const(gem_obj, struct pvr_gem_object, base.base)
119 
120 /* Functions defined in pvr_gem.c */
121 
122 struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size);
123 
124 struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev,
125 					     size_t size, u64 flags);
126 
127 int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj,
128 			       struct pvr_file *pvr_file, u32 *handle);
129 struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file,
130 						  u32 handle);
131 
132 static __always_inline struct sg_table *
133 pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj)
134 {
135 	return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj));
136 }
137 
138 void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj);
139 void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj);
140 
141 int pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset,
142 			 dma_addr_t *dma_addr_out);
143 
144 /**
145  * pvr_gem_object_get() - Acquire reference on pvr_gem_object
146  * @pvr_obj: Pointer to object to acquire reference on.
147  */
148 static __always_inline void
149 pvr_gem_object_get(struct pvr_gem_object *pvr_obj)
150 {
151 	drm_gem_object_get(gem_from_pvr_gem(pvr_obj));
152 }
153 
154 /**
155  * pvr_gem_object_put() - Release reference on pvr_gem_object
156  * @pvr_obj: Pointer to object to release reference on.
157  */
158 static __always_inline void
159 pvr_gem_object_put(struct pvr_gem_object *pvr_obj)
160 {
161 	drm_gem_object_put(gem_from_pvr_gem(pvr_obj));
162 }
163 
164 static __always_inline size_t
165 pvr_gem_object_size(struct pvr_gem_object *pvr_obj)
166 {
167 	return gem_from_pvr_gem(pvr_obj)->size;
168 }
169 
170 #endif /* PVR_GEM_H */
171