xref: /linux/drivers/gpu/drm/panthor/panthor_gem.h (revision 6dfebeee296cbb3296f06c28f3b2d053ec8374e7)
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 
40 /**
41  * struct panthor_gem_debugfs - GEM object's DebugFS list information
42  */
43 struct panthor_gem_debugfs {
44 	/**
45 	 * @node: Node used to insert the object in the device-wide list of
46 	 * GEM objects, to display information about it through a DebugFS file.
47 	 */
48 	struct list_head node;
49 
50 	/** @creator: Information about the UM process which created the GEM. */
51 	struct {
52 		/** @creator.process_name: Group leader name in owning thread's process */
53 		char process_name[TASK_COMM_LEN];
54 
55 		/** @creator.tgid: PID of the thread's group leader within its process */
56 		pid_t tgid;
57 	} creator;
58 
59 	/** @flags: Combination of panthor_debugfs_gem_usage_flags flags */
60 	u32 flags;
61 };
62 
63 /**
64  * struct panthor_gem_object - Driver specific GEM object.
65  */
66 struct panthor_gem_object {
67 	/** @base: Inherit from drm_gem_shmem_object. */
68 	struct drm_gem_shmem_object base;
69 
70 	/**
71 	 * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object
72 	 * is attached to.
73 	 *
74 	 * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a
75 	 * different VM will fail.
76 	 *
77 	 * All FW memory objects have this field set to the root GEM of the MCU
78 	 * VM.
79 	 */
80 	struct drm_gem_object *exclusive_vm_root_gem;
81 
82 	/** @flags: Combination of drm_panthor_bo_flags flags. */
83 	u32 flags;
84 
85 	/**
86 	 * @label: BO tagging fields. The label can be assigned within the
87 	 * driver itself or through a specific IOCTL.
88 	 */
89 	struct {
90 		/**
91 		 * @label.str: Pointer to NULL-terminated string,
92 		 */
93 		const char *str;
94 
95 		/** @lock.str: Protects access to the @label.str field. */
96 		struct mutex lock;
97 	} label;
98 
99 #ifdef CONFIG_DEBUG_FS
100 	struct panthor_gem_debugfs debugfs;
101 #endif
102 };
103 
104 /**
105  * struct panthor_kernel_bo - Kernel buffer object.
106  *
107  * These objects are only manipulated by the kernel driver and not
108  * directly exposed to the userspace. The GPU address of a kernel
109  * BO might be passed to userspace though.
110  */
111 struct panthor_kernel_bo {
112 	/**
113 	 * @obj: The GEM object backing this kernel buffer object.
114 	 */
115 	struct drm_gem_object *obj;
116 
117 	/**
118 	 * @vm: VM this private buffer is attached to.
119 	 */
120 	struct panthor_vm *vm;
121 
122 	/**
123 	 * @va_node: VA space allocated to this GEM.
124 	 */
125 	struct drm_mm_node va_node;
126 
127 	/**
128 	 * @kmap: Kernel CPU mapping of @gem.
129 	 */
130 	void *kmap;
131 };
132 
133 static inline
134 struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj)
135 {
136 	return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base);
137 }
138 
139 void panthor_gem_init(struct panthor_device *ptdev);
140 
141 struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size);
142 
143 int
144 panthor_gem_create_with_handle(struct drm_file *file,
145 			       struct drm_device *ddev,
146 			       struct panthor_vm *exclusive_vm,
147 			       u64 *size, u32 flags, uint32_t *handle);
148 
149 void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label);
150 void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label);
151 int panthor_gem_sync(struct drm_gem_object *obj,
152 		     u32 type, u64 offset, u64 size);
153 
154 struct drm_gem_object *
155 panthor_gem_prime_import(struct drm_device *dev,
156 			 struct dma_buf *dma_buf);
157 
158 static inline u64
159 panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
160 {
161 	return bo->va_node.start;
162 }
163 
164 static inline size_t
165 panthor_kernel_bo_size(struct panthor_kernel_bo *bo)
166 {
167 	return bo->obj->size;
168 }
169 
170 static inline int
171 panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo)
172 {
173 	struct iosys_map map;
174 	int ret;
175 
176 	if (bo->kmap)
177 		return 0;
178 
179 	ret = drm_gem_vmap(bo->obj, &map);
180 	if (ret)
181 		return ret;
182 
183 	bo->kmap = map.vaddr;
184 	return 0;
185 }
186 
187 static inline void
188 panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo)
189 {
190 	if (bo->kmap) {
191 		struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap);
192 
193 		drm_gem_vunmap(bo->obj, &map);
194 		bo->kmap = NULL;
195 	}
196 }
197 
198 struct panthor_kernel_bo *
199 panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
200 			 size_t size, u32 bo_flags, u32 vm_map_flags,
201 			 u64 gpu_va, const char *name);
202 
203 void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo);
204 
205 #ifdef CONFIG_DEBUG_FS
206 void panthor_gem_debugfs_print_bos(struct panthor_device *pfdev,
207 				   struct seq_file *m);
208 #endif
209 
210 #endif /* __PANTHOR_GEM_H__ */
211