xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem.h (revision 413d6ed3)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #ifndef __MSM_GEM_H__
8 #define __MSM_GEM_H__
9 
10 #include <linux/kref.h>
11 #include <linux/dma-resv.h>
12 #include "msm_drv.h"
13 
14 /* Make all GEM related WARN_ON()s ratelimited.. when things go wrong they
15  * tend to go wrong 1000s of times in a short timespan.
16  */
17 #define GEM_WARN_ON(x)  WARN_RATELIMIT(x, "%s", __stringify(x))
18 
19 /* Additional internal-use only BO flags: */
20 #define MSM_BO_STOLEN        0x10000000    /* try to use stolen/splash memory */
21 #define MSM_BO_MAP_PRIV      0x20000000    /* use IOMMU_PRIV when mapping */
22 
23 struct msm_gem_address_space {
24 	const char *name;
25 	/* NOTE: mm managed at the page level, size is in # of pages
26 	 * and position mm_node->start is in # of pages:
27 	 */
28 	struct drm_mm mm;
29 	spinlock_t lock; /* Protects drm_mm node allocation/removal */
30 	struct msm_mmu *mmu;
31 	struct kref kref;
32 
33 	/* For address spaces associated with a specific process, this
34 	 * will be non-NULL:
35 	 */
36 	struct pid *pid;
37 };
38 
39 struct msm_gem_vma {
40 	struct drm_mm_node node;
41 	uint64_t iova;
42 	struct msm_gem_address_space *aspace;
43 	struct list_head list;    /* node in msm_gem_object::vmas */
44 	bool mapped;
45 	int inuse;
46 };
47 
48 struct msm_gem_object {
49 	struct drm_gem_object base;
50 
51 	uint32_t flags;
52 
53 	/**
54 	 * Advice: are the backing pages purgeable?
55 	 */
56 	uint8_t madv;
57 
58 	/**
59 	 * Is object on inactive_dontneed list (ie. counted in priv->shrinkable_count)?
60 	 */
61 	bool dontneed : 1;
62 
63 	/**
64 	 * Is object evictable (ie. counted in priv->evictable_count)?
65 	 */
66 	bool evictable : 1;
67 
68 	/**
69 	 * count of active vmap'ing
70 	 */
71 	uint8_t vmap_count;
72 
73 	/**
74 	 * Node in list of all objects (mainly for debugfs, protected by
75 	 * priv->obj_lock
76 	 */
77 	struct list_head node;
78 
79 	/**
80 	 * An object is either:
81 	 *  inactive - on priv->inactive_dontneed or priv->inactive_willneed
82 	 *     (depending on purgeability status)
83 	 *  active   - on one one of the gpu's active_list..  well, at
84 	 *     least for now we don't have (I don't think) hw sync between
85 	 *     2d and 3d one devices which have both, meaning we need to
86 	 *     block on submit if a bo is already on other ring
87 	 */
88 	struct list_head mm_list;
89 
90 	/* Transiently in the process of submit ioctl, objects associated
91 	 * with the submit are on submit->bo_list.. this only lasts for
92 	 * the duration of the ioctl, so one bo can never be on multiple
93 	 * submit lists.
94 	 */
95 	struct list_head submit_entry;
96 
97 	struct page **pages;
98 	struct sg_table *sgt;
99 	void *vaddr;
100 
101 	struct list_head vmas;    /* list of msm_gem_vma */
102 
103 	/* For physically contiguous buffers.  Used when we don't have
104 	 * an IOMMU.  Also used for stolen/splashscreen buffer.
105 	 */
106 	struct drm_mm_node *vram_node;
107 
108 	char name[32]; /* Identifier to print for the debugfs files */
109 
110 	int active_count;
111 	int pin_count;
112 };
113 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
114 
115 int msm_gem_mmap_obj(struct drm_gem_object *obj,
116 			struct vm_area_struct *vma);
117 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
118 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
119 int msm_gem_get_iova(struct drm_gem_object *obj,
120 		struct msm_gem_address_space *aspace, uint64_t *iova);
121 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
122 		struct msm_gem_address_space *aspace, uint64_t *iova,
123 		u64 range_start, u64 range_end);
124 int msm_gem_get_and_pin_iova_locked(struct drm_gem_object *obj,
125 		struct msm_gem_address_space *aspace, uint64_t *iova);
126 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
127 		struct msm_gem_address_space *aspace, uint64_t *iova);
128 uint64_t msm_gem_iova(struct drm_gem_object *obj,
129 		struct msm_gem_address_space *aspace);
130 void msm_gem_unpin_iova_locked(struct drm_gem_object *obj,
131 		struct msm_gem_address_space *aspace);
132 void msm_gem_unpin_iova(struct drm_gem_object *obj,
133 		struct msm_gem_address_space *aspace);
134 struct page **msm_gem_get_pages(struct drm_gem_object *obj);
135 void msm_gem_put_pages(struct drm_gem_object *obj);
136 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
137 		struct drm_mode_create_dumb *args);
138 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
139 		uint32_t handle, uint64_t *offset);
140 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
141 void *msm_gem_get_vaddr(struct drm_gem_object *obj);
142 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
143 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
144 void msm_gem_put_vaddr(struct drm_gem_object *obj);
145 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
146 int msm_gem_sync_object(struct drm_gem_object *obj,
147 		struct msm_fence_context *fctx, bool exclusive);
148 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu);
149 void msm_gem_active_put(struct drm_gem_object *obj);
150 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
151 int msm_gem_cpu_fini(struct drm_gem_object *obj);
152 void msm_gem_free_object(struct drm_gem_object *obj);
153 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
154 		uint32_t size, uint32_t flags, uint32_t *handle, char *name);
155 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
156 		uint32_t size, uint32_t flags);
157 struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
158 		uint32_t size, uint32_t flags);
159 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
160 		uint32_t flags, struct msm_gem_address_space *aspace,
161 		struct drm_gem_object **bo, uint64_t *iova);
162 void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
163 		uint32_t flags, struct msm_gem_address_space *aspace,
164 		struct drm_gem_object **bo, uint64_t *iova);
165 void msm_gem_kernel_put(struct drm_gem_object *bo,
166 		struct msm_gem_address_space *aspace, bool locked);
167 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
168 		struct dma_buf *dmabuf, struct sg_table *sgt);
169 __printf(2, 3)
170 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
171 
172 #ifdef CONFIG_DEBUG_FS
173 struct msm_gem_stats {
174 	struct {
175 		unsigned count;
176 		size_t size;
177 	} all, active, resident, purgeable, purged;
178 };
179 
180 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
181 		struct msm_gem_stats *stats);
182 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
183 #endif
184 
185 static inline void
186 msm_gem_lock(struct drm_gem_object *obj)
187 {
188 	dma_resv_lock(obj->resv, NULL);
189 }
190 
191 static inline bool __must_check
192 msm_gem_trylock(struct drm_gem_object *obj)
193 {
194 	return dma_resv_trylock(obj->resv);
195 }
196 
197 static inline int
198 msm_gem_lock_interruptible(struct drm_gem_object *obj)
199 {
200 	return dma_resv_lock_interruptible(obj->resv, NULL);
201 }
202 
203 static inline void
204 msm_gem_unlock(struct drm_gem_object *obj)
205 {
206 	dma_resv_unlock(obj->resv);
207 }
208 
209 static inline bool
210 msm_gem_is_locked(struct drm_gem_object *obj)
211 {
212 	return dma_resv_is_locked(obj->resv);
213 }
214 
215 static inline bool is_active(struct msm_gem_object *msm_obj)
216 {
217 	GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
218 	return msm_obj->active_count;
219 }
220 
221 /* imported/exported objects are not purgeable: */
222 static inline bool is_unpurgeable(struct msm_gem_object *msm_obj)
223 {
224 	return msm_obj->base.import_attach || msm_obj->pin_count;
225 }
226 
227 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
228 {
229 	return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
230 			!is_unpurgeable(msm_obj);
231 }
232 
233 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
234 {
235 	GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
236 	return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
237 }
238 
239 static inline void mark_purgeable(struct msm_gem_object *msm_obj)
240 {
241 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
242 
243 	GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock));
244 
245 	if (is_unpurgeable(msm_obj))
246 		return;
247 
248 	if (GEM_WARN_ON(msm_obj->dontneed))
249 		return;
250 
251 	priv->shrinkable_count += msm_obj->base.size >> PAGE_SHIFT;
252 	msm_obj->dontneed = true;
253 }
254 
255 static inline void mark_unpurgeable(struct msm_gem_object *msm_obj)
256 {
257 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
258 
259 	GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock));
260 
261 	if (is_unpurgeable(msm_obj))
262 		return;
263 
264 	if (GEM_WARN_ON(!msm_obj->dontneed))
265 		return;
266 
267 	priv->shrinkable_count -= msm_obj->base.size >> PAGE_SHIFT;
268 	GEM_WARN_ON(priv->shrinkable_count < 0);
269 	msm_obj->dontneed = false;
270 }
271 
272 static inline bool is_unevictable(struct msm_gem_object *msm_obj)
273 {
274 	return is_unpurgeable(msm_obj) || msm_obj->vaddr;
275 }
276 
277 static inline void mark_evictable(struct msm_gem_object *msm_obj)
278 {
279 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
280 
281 	WARN_ON(!mutex_is_locked(&priv->mm_lock));
282 
283 	if (is_unevictable(msm_obj))
284 		return;
285 
286 	if (WARN_ON(msm_obj->evictable))
287 		return;
288 
289 	priv->evictable_count += msm_obj->base.size >> PAGE_SHIFT;
290 	msm_obj->evictable = true;
291 }
292 
293 static inline void mark_unevictable(struct msm_gem_object *msm_obj)
294 {
295 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
296 
297 	WARN_ON(!mutex_is_locked(&priv->mm_lock));
298 
299 	if (is_unevictable(msm_obj))
300 		return;
301 
302 	if (WARN_ON(!msm_obj->evictable))
303 		return;
304 
305 	priv->evictable_count -= msm_obj->base.size >> PAGE_SHIFT;
306 	WARN_ON(priv->evictable_count < 0);
307 	msm_obj->evictable = false;
308 }
309 
310 void msm_gem_purge(struct drm_gem_object *obj);
311 void msm_gem_evict(struct drm_gem_object *obj);
312 void msm_gem_vunmap(struct drm_gem_object *obj);
313 
314 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
315  * associated with the cmdstream submission for synchronization (and
316  * make it easier to unwind when things go wrong, etc).  This only
317  * lasts for the duration of the submit-ioctl.
318  */
319 struct msm_gem_submit {
320 	struct kref ref;
321 	struct drm_device *dev;
322 	struct msm_gpu *gpu;
323 	struct msm_gem_address_space *aspace;
324 	struct list_head node;   /* node in ring submit list */
325 	struct list_head bo_list;
326 	struct ww_acquire_ctx ticket;
327 	uint32_t seqno;		/* Sequence number of the submit on the ring */
328 	struct dma_fence *fence;
329 	struct msm_gpu_submitqueue *queue;
330 	struct pid *pid;    /* submitting process */
331 	bool valid;         /* true if no cmdstream patching needed */
332 	bool in_rb;         /* "sudo" mode, copy cmds into RB */
333 	struct msm_ringbuffer *ring;
334 	struct msm_file_private *ctx;
335 	unsigned int nr_cmds;
336 	unsigned int nr_bos;
337 	u32 ident;	   /* A "identifier" for the submit for logging */
338 	struct {
339 		uint32_t type;
340 		uint32_t size;  /* in dwords */
341 		uint64_t iova;
342 		uint32_t offset;/* in dwords */
343 		uint32_t idx;   /* cmdstream buffer idx in bos[] */
344 		uint32_t nr_relocs;
345 		struct drm_msm_gem_submit_reloc *relocs;
346 	} *cmd;  /* array of size nr_cmds */
347 	struct {
348 		uint32_t flags;
349 		union {
350 			struct msm_gem_object *obj;
351 			uint32_t handle;
352 		};
353 		uint64_t iova;
354 	} bos[];
355 };
356 
357 void __msm_gem_submit_destroy(struct kref *kref);
358 
359 static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
360 {
361 	kref_get(&submit->ref);
362 }
363 
364 static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
365 {
366 	kref_put(&submit->ref, __msm_gem_submit_destroy);
367 }
368 
369 /* helper to determine of a buffer in submit should be dumped, used for both
370  * devcoredump and debugfs cmdstream dumping:
371  */
372 static inline bool
373 should_dump(struct msm_gem_submit *submit, int idx)
374 {
375 	extern bool rd_full;
376 	return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
377 }
378 
379 #endif /* __MSM_GEM_H__ */
380