xref: /linux/drivers/gpu/drm/panthor/panthor_gem.h (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
1 /* SPDX-License-Identifier: GPL-2.0 or MIT */
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3 /* Copyright 2023 Collabora ltd. */
4 
5 #ifndef __PANTHOR_GEM_H__
6 #define __PANTHOR_GEM_H__
7 
8 #include <drm/drm_gem_shmem_helper.h>
9 #include <drm/drm_mm.h>
10 
11 #include <linux/iosys-map.h>
12 #include <linux/rwsem.h>
13 
14 struct panthor_vm;
15 
16 #define PANTHOR_BO_LABEL_MAXLEN	4096
17 
18 enum panthor_debugfs_gem_state_flags {
19 	PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT = 0,
20 	PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT = 1,
21 
22 	/** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED: GEM BO is PRIME imported. */
23 	PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT),
24 
25 	/** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED: GEM BO is PRIME exported. */
26 	PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT),
27 };
28 
29 enum panthor_debugfs_gem_usage_flags {
30 	PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT = 0,
31 	PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT = 1,
32 
33 	/** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL: BO is for kernel use only. */
34 	PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL = BIT(PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT),
35 
36 	/** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED: BO is mapped on the FW VM. */
37 	PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED = BIT(PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT),
38 
39 	/** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_INITIALIZED: BO is ready for DebugFS display. */
40 	PANTHOR_DEBUGFS_GEM_USAGE_FLAG_INITIALIZED = BIT(31),
41 };
42 
43 /**
44  * struct panthor_gem_debugfs - GEM object's DebugFS list information
45  */
46 struct panthor_gem_debugfs {
47 	/**
48 	 * @node: Node used to insert the object in the device-wide list of
49 	 * GEM objects, to display information about it through a DebugFS file.
50 	 */
51 	struct list_head node;
52 
53 	/** @creator: Information about the UM process which created the GEM. */
54 	struct {
55 		/** @creator.process_name: Group leader name in owning thread's process */
56 		char process_name[TASK_COMM_LEN];
57 
58 		/** @creator.tgid: PID of the thread's group leader within its process */
59 		pid_t tgid;
60 	} creator;
61 
62 	/** @flags: Combination of panthor_debugfs_gem_usage_flags flags */
63 	u32 flags;
64 };
65 
66 /**
67  * struct panthor_gem_object - Driver specific GEM object.
68  */
69 struct panthor_gem_object {
70 	/** @base: Inherit from drm_gem_shmem_object. */
71 	struct drm_gem_shmem_object base;
72 
73 	/**
74 	 * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object
75 	 * is attached to.
76 	 *
77 	 * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a
78 	 * different VM will fail.
79 	 *
80 	 * All FW memory objects have this field set to the root GEM of the MCU
81 	 * VM.
82 	 */
83 	struct drm_gem_object *exclusive_vm_root_gem;
84 
85 	/**
86 	 * @gpuva_list_lock: Custom GPUVA lock.
87 	 *
88 	 * Used to protect insertion of drm_gpuva elements to the
89 	 * drm_gem_object.gpuva.list list.
90 	 *
91 	 * We can't use the GEM resv for that, because drm_gpuva_link() is
92 	 * called in a dma-signaling path, where we're not allowed to take
93 	 * resv locks.
94 	 */
95 	struct mutex gpuva_list_lock;
96 
97 	/** @flags: Combination of drm_panthor_bo_flags flags. */
98 	u32 flags;
99 
100 	/**
101 	 * @label: BO tagging fields. The label can be assigned within the
102 	 * driver itself or through a specific IOCTL.
103 	 */
104 	struct {
105 		/**
106 		 * @label.str: Pointer to NULL-terminated string,
107 		 */
108 		const char *str;
109 
110 		/** @lock.str: Protects access to the @label.str field. */
111 		struct mutex lock;
112 	} label;
113 
114 #ifdef CONFIG_DEBUG_FS
115 	struct panthor_gem_debugfs debugfs;
116 #endif
117 };
118 
119 /**
120  * struct panthor_kernel_bo - Kernel buffer object.
121  *
122  * These objects are only manipulated by the kernel driver and not
123  * directly exposed to the userspace. The GPU address of a kernel
124  * BO might be passed to userspace though.
125  */
126 struct panthor_kernel_bo {
127 	/**
128 	 * @obj: The GEM object backing this kernel buffer object.
129 	 */
130 	struct drm_gem_object *obj;
131 
132 	/**
133 	 * @vm: VM this private buffer is attached to.
134 	 */
135 	struct panthor_vm *vm;
136 
137 	/**
138 	 * @va_node: VA space allocated to this GEM.
139 	 */
140 	struct drm_mm_node va_node;
141 
142 	/**
143 	 * @kmap: Kernel CPU mapping of @gem.
144 	 */
145 	void *kmap;
146 };
147 
148 static inline
149 struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj)
150 {
151 	return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base);
152 }
153 
154 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size);
155 
156 int
157 panthor_gem_create_with_handle(struct drm_file *file,
158 			       struct drm_device *ddev,
159 			       struct panthor_vm *exclusive_vm,
160 			       u64 *size, u32 flags, uint32_t *handle);
161 
162 void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label);
163 void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label);
164 
165 static inline u64
166 panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
167 {
168 	return bo->va_node.start;
169 }
170 
171 static inline size_t
172 panthor_kernel_bo_size(struct panthor_kernel_bo *bo)
173 {
174 	return bo->obj->size;
175 }
176 
177 static inline int
178 panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo)
179 {
180 	struct iosys_map map;
181 	int ret;
182 
183 	if (bo->kmap)
184 		return 0;
185 
186 	ret = drm_gem_vmap(bo->obj, &map);
187 	if (ret)
188 		return ret;
189 
190 	bo->kmap = map.vaddr;
191 	return 0;
192 }
193 
194 static inline void
195 panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo)
196 {
197 	if (bo->kmap) {
198 		struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap);
199 
200 		drm_gem_vunmap(bo->obj, &map);
201 		bo->kmap = NULL;
202 	}
203 }
204 
205 struct panthor_kernel_bo *
206 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
207 			 size_t size, u32 bo_flags, u32 vm_map_flags,
208 			 u64 gpu_va, const char *name);
209 
210 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo);
211 
212 #ifdef CONFIG_DEBUG_FS
213 void panthor_gem_debugfs_print_bos(struct panthor_device *pfdev,
214 				   struct seq_file *m);
215 #endif
216 
217 #endif /* __PANTHOR_GEM_H__ */
218