xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem.h (revision 0d4bb5e4)
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 "drm/gpu_scheduler.h"
13 #include "msm_drv.h"
14 
15 /* Make all GEM related WARN_ON()s ratelimited.. when things go wrong they
16  * tend to go wrong 1000s of times in a short timespan.
17  */
18 #define GEM_WARN_ON(x)  WARN_RATELIMIT(x, "%s", __stringify(x))
19 
20 /* Additional internal-use only BO flags: */
21 #define MSM_BO_STOLEN        0x10000000    /* try to use stolen/splash memory */
22 #define MSM_BO_MAP_PRIV      0x20000000    /* use IOMMU_PRIV when mapping */
23 
24 struct msm_gem_address_space {
25 	const char *name;
26 	/* NOTE: mm managed at the page level, size is in # of pages
27 	 * and position mm_node->start is in # of pages:
28 	 */
29 	struct drm_mm mm;
30 	spinlock_t lock; /* Protects drm_mm node allocation/removal */
31 	struct msm_mmu *mmu;
32 	struct kref kref;
33 
34 	/* For address spaces associated with a specific process, this
35 	 * will be non-NULL:
36 	 */
37 	struct pid *pid;
38 
39 	/* @faults: the number of GPU hangs associated with this address space */
40 	int faults;
41 
42 	/** @va_start: lowest possible address to allocate */
43 	uint64_t va_start;
44 
45 	/** @va_size: the size of the address space (in bytes) */
46 	uint64_t va_size;
47 };
48 
49 struct msm_gem_address_space *
50 msm_gem_address_space_get(struct msm_gem_address_space *aspace);
51 
52 void msm_gem_address_space_put(struct msm_gem_address_space *aspace);
53 
54 struct msm_gem_address_space *
55 msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
56 		u64 va_start, u64 size);
57 
58 struct msm_fence_context;
59 
60 struct msm_gem_vma {
61 	struct drm_mm_node node;
62 	uint64_t iova;
63 	struct msm_gem_address_space *aspace;
64 	struct list_head list;    /* node in msm_gem_object::vmas */
65 	bool mapped;
66 	int inuse;
67 	uint32_t fence_mask;
68 	uint32_t fence[MSM_GPU_MAX_RINGS];
69 	struct msm_fence_context *fctx[MSM_GPU_MAX_RINGS];
70 };
71 
72 int msm_gem_init_vma(struct msm_gem_address_space *aspace,
73 		struct msm_gem_vma *vma, int size,
74 		u64 range_start, u64 range_end);
75 bool msm_gem_vma_inuse(struct msm_gem_vma *vma);
76 void msm_gem_purge_vma(struct msm_gem_address_space *aspace,
77 		struct msm_gem_vma *vma);
78 void msm_gem_unpin_vma(struct msm_gem_vma *vma);
79 void msm_gem_unpin_vma_fenced(struct msm_gem_vma *vma, struct msm_fence_context *fctx);
80 int msm_gem_map_vma(struct msm_gem_address_space *aspace,
81 		struct msm_gem_vma *vma, int prot,
82 		struct sg_table *sgt, int size);
83 void msm_gem_close_vma(struct msm_gem_address_space *aspace,
84 		struct msm_gem_vma *vma);
85 
86 struct msm_gem_object {
87 	struct drm_gem_object base;
88 
89 	uint32_t flags;
90 
91 	/**
92 	 * Advice: are the backing pages purgeable?
93 	 */
94 	uint8_t madv;
95 
96 	/**
97 	 * Is object on inactive_dontneed list (ie. counted in priv->shrinkable_count)?
98 	 */
99 	bool dontneed : 1;
100 
101 	/**
102 	 * Is object evictable (ie. counted in priv->evictable_count)?
103 	 */
104 	bool evictable : 1;
105 
106 	/**
107 	 * count of active vmap'ing
108 	 */
109 	uint8_t vmap_count;
110 
111 	/**
112 	 * Node in list of all objects (mainly for debugfs, protected by
113 	 * priv->obj_lock
114 	 */
115 	struct list_head node;
116 
117 	/**
118 	 * An object is either:
119 	 *  inactive - on priv->inactive_dontneed or priv->inactive_willneed
120 	 *     (depending on purgeability status)
121 	 *  active   - on one one of the gpu's active_list..  well, at
122 	 *     least for now we don't have (I don't think) hw sync between
123 	 *     2d and 3d one devices which have both, meaning we need to
124 	 *     block on submit if a bo is already on other ring
125 	 */
126 	struct list_head mm_list;
127 
128 	struct page **pages;
129 	struct sg_table *sgt;
130 	void *vaddr;
131 
132 	struct list_head vmas;    /* list of msm_gem_vma */
133 
134 	/* For physically contiguous buffers.  Used when we don't have
135 	 * an IOMMU.  Also used for stolen/splashscreen buffer.
136 	 */
137 	struct drm_mm_node *vram_node;
138 
139 	char name[32]; /* Identifier to print for the debugfs files */
140 
141 	int active_count;
142 	int pin_count;
143 };
144 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
145 
146 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
147 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma);
148 void msm_gem_unpin_locked(struct drm_gem_object *obj);
149 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
150 					   struct msm_gem_address_space *aspace);
151 int msm_gem_get_iova(struct drm_gem_object *obj,
152 		struct msm_gem_address_space *aspace, uint64_t *iova);
153 int msm_gem_set_iova(struct drm_gem_object *obj,
154 		struct msm_gem_address_space *aspace, uint64_t iova);
155 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
156 		struct msm_gem_address_space *aspace, uint64_t *iova,
157 		u64 range_start, u64 range_end);
158 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
159 		struct msm_gem_address_space *aspace, uint64_t *iova);
160 void msm_gem_unpin_iova(struct drm_gem_object *obj,
161 		struct msm_gem_address_space *aspace);
162 struct page **msm_gem_get_pages(struct drm_gem_object *obj);
163 void msm_gem_put_pages(struct drm_gem_object *obj);
164 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
165 		struct drm_mode_create_dumb *args);
166 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
167 		uint32_t handle, uint64_t *offset);
168 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
169 void *msm_gem_get_vaddr(struct drm_gem_object *obj);
170 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
171 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
172 void msm_gem_put_vaddr(struct drm_gem_object *obj);
173 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
174 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu);
175 void msm_gem_active_put(struct drm_gem_object *obj);
176 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
177 int msm_gem_cpu_fini(struct drm_gem_object *obj);
178 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
179 		uint32_t size, uint32_t flags, uint32_t *handle, char *name);
180 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
181 		uint32_t size, uint32_t flags);
182 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
183 		uint32_t flags, struct msm_gem_address_space *aspace,
184 		struct drm_gem_object **bo, uint64_t *iova);
185 void msm_gem_kernel_put(struct drm_gem_object *bo,
186 		struct msm_gem_address_space *aspace);
187 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
188 		struct dma_buf *dmabuf, struct sg_table *sgt);
189 __printf(2, 3)
190 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
191 
192 #ifdef CONFIG_DEBUG_FS
193 struct msm_gem_stats {
194 	struct {
195 		unsigned count;
196 		size_t size;
197 	} all, active, resident, purgeable, purged;
198 };
199 
200 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
201 		struct msm_gem_stats *stats);
202 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
203 #endif
204 
205 static inline void
206 msm_gem_lock(struct drm_gem_object *obj)
207 {
208 	dma_resv_lock(obj->resv, NULL);
209 }
210 
211 static inline bool __must_check
212 msm_gem_trylock(struct drm_gem_object *obj)
213 {
214 	return dma_resv_trylock(obj->resv);
215 }
216 
217 static inline int
218 msm_gem_lock_interruptible(struct drm_gem_object *obj)
219 {
220 	return dma_resv_lock_interruptible(obj->resv, NULL);
221 }
222 
223 static inline void
224 msm_gem_unlock(struct drm_gem_object *obj)
225 {
226 	dma_resv_unlock(obj->resv);
227 }
228 
229 static inline bool
230 msm_gem_is_locked(struct drm_gem_object *obj)
231 {
232 	/*
233 	 * Destroying the object is a special case.. msm_gem_free_object()
234 	 * calls many things that WARN_ON if the obj lock is not held.  But
235 	 * acquiring the obj lock in msm_gem_free_object() can cause a
236 	 * locking order inversion between reservation_ww_class_mutex and
237 	 * fs_reclaim.
238 	 *
239 	 * This deadlock is not actually possible, because no one should
240 	 * be already holding the lock when msm_gem_free_object() is called.
241 	 * Unfortunately lockdep is not aware of this detail.  So when the
242 	 * refcount drops to zero, we pretend it is already locked.
243 	 */
244 	return dma_resv_is_locked(obj->resv) || (kref_read(&obj->refcount) == 0);
245 }
246 
247 static inline bool is_active(struct msm_gem_object *msm_obj)
248 {
249 	GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
250 	return msm_obj->active_count;
251 }
252 
253 /* imported/exported objects are not purgeable: */
254 static inline bool is_unpurgeable(struct msm_gem_object *msm_obj)
255 {
256 	return msm_obj->base.import_attach || msm_obj->pin_count;
257 }
258 
259 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
260 {
261 	return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
262 			!is_unpurgeable(msm_obj);
263 }
264 
265 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
266 {
267 	GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
268 	return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
269 }
270 
271 static inline void mark_purgeable(struct msm_gem_object *msm_obj)
272 {
273 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
274 
275 	GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock));
276 
277 	if (is_unpurgeable(msm_obj))
278 		return;
279 
280 	if (GEM_WARN_ON(msm_obj->dontneed))
281 		return;
282 
283 	priv->shrinkable_count += msm_obj->base.size >> PAGE_SHIFT;
284 	msm_obj->dontneed = true;
285 }
286 
287 static inline void mark_unpurgeable(struct msm_gem_object *msm_obj)
288 {
289 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
290 
291 	GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock));
292 
293 	if (is_unpurgeable(msm_obj))
294 		return;
295 
296 	if (GEM_WARN_ON(!msm_obj->dontneed))
297 		return;
298 
299 	priv->shrinkable_count -= msm_obj->base.size >> PAGE_SHIFT;
300 	GEM_WARN_ON(priv->shrinkable_count < 0);
301 	msm_obj->dontneed = false;
302 }
303 
304 static inline bool is_unevictable(struct msm_gem_object *msm_obj)
305 {
306 	return is_unpurgeable(msm_obj) || msm_obj->vaddr;
307 }
308 
309 static inline void mark_evictable(struct msm_gem_object *msm_obj)
310 {
311 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
312 
313 	WARN_ON(!mutex_is_locked(&priv->mm_lock));
314 
315 	if (is_unevictable(msm_obj))
316 		return;
317 
318 	if (WARN_ON(msm_obj->evictable))
319 		return;
320 
321 	priv->evictable_count += msm_obj->base.size >> PAGE_SHIFT;
322 	msm_obj->evictable = true;
323 }
324 
325 static inline void mark_unevictable(struct msm_gem_object *msm_obj)
326 {
327 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
328 
329 	WARN_ON(!mutex_is_locked(&priv->mm_lock));
330 
331 	if (is_unevictable(msm_obj))
332 		return;
333 
334 	if (WARN_ON(!msm_obj->evictable))
335 		return;
336 
337 	priv->evictable_count -= msm_obj->base.size >> PAGE_SHIFT;
338 	WARN_ON(priv->evictable_count < 0);
339 	msm_obj->evictable = false;
340 }
341 
342 void msm_gem_purge(struct drm_gem_object *obj);
343 void msm_gem_evict(struct drm_gem_object *obj);
344 void msm_gem_vunmap(struct drm_gem_object *obj);
345 
346 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
347  * associated with the cmdstream submission for synchronization (and
348  * make it easier to unwind when things go wrong, etc).
349  */
350 struct msm_gem_submit {
351 	struct drm_sched_job base;
352 	struct kref ref;
353 	struct drm_device *dev;
354 	struct msm_gpu *gpu;
355 	struct msm_gem_address_space *aspace;
356 	struct list_head node;   /* node in ring submit list */
357 	struct ww_acquire_ctx ticket;
358 	uint32_t seqno;		/* Sequence number of the submit on the ring */
359 
360 	/* Hw fence, which is created when the scheduler executes the job, and
361 	 * is signaled when the hw finishes (via seqno write from cmdstream)
362 	 */
363 	struct dma_fence *hw_fence;
364 
365 	/* Userspace visible fence, which is signaled by the scheduler after
366 	 * the hw_fence is signaled.
367 	 */
368 	struct dma_fence *user_fence;
369 
370 	int fence_id;       /* key into queue->fence_idr */
371 	struct msm_gpu_submitqueue *queue;
372 	struct pid *pid;    /* submitting process */
373 	bool fault_dumped;  /* Limit devcoredump dumping to one per submit */
374 	bool valid;         /* true if no cmdstream patching needed */
375 	bool in_rb;         /* "sudo" mode, copy cmds into RB */
376 	struct msm_ringbuffer *ring;
377 	unsigned int nr_cmds;
378 	unsigned int nr_bos;
379 	u32 ident;	   /* A "identifier" for the submit for logging */
380 	struct {
381 		uint32_t type;
382 		uint32_t size;  /* in dwords */
383 		uint64_t iova;
384 		uint32_t offset;/* in dwords */
385 		uint32_t idx;   /* cmdstream buffer idx in bos[] */
386 		uint32_t nr_relocs;
387 		struct drm_msm_gem_submit_reloc *relocs;
388 	} *cmd;  /* array of size nr_cmds */
389 	struct {
390 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
391 #define BO_VALID	0x8000	/* is current addr in cmdstream correct/valid? */
392 #define BO_LOCKED	0x4000	/* obj lock is held */
393 #define BO_ACTIVE	0x2000	/* active refcnt is held */
394 #define BO_OBJ_PINNED	0x1000	/* obj (pages) is pinned and on active list */
395 #define BO_VMA_PINNED	0x0800	/* vma (virtual address) is pinned */
396 		uint32_t flags;
397 		union {
398 			struct msm_gem_object *obj;
399 			uint32_t handle;
400 		};
401 		uint64_t iova;
402 		struct msm_gem_vma *vma;
403 	} bos[];
404 };
405 
406 static inline struct msm_gem_submit *to_msm_submit(struct drm_sched_job *job)
407 {
408 	return container_of(job, struct msm_gem_submit, base);
409 }
410 
411 void __msm_gem_submit_destroy(struct kref *kref);
412 
413 static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
414 {
415 	kref_get(&submit->ref);
416 }
417 
418 static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
419 {
420 	kref_put(&submit->ref, __msm_gem_submit_destroy);
421 }
422 
423 void msm_submit_retire(struct msm_gem_submit *submit);
424 
425 /* helper to determine of a buffer in submit should be dumped, used for both
426  * devcoredump and debugfs cmdstream dumping:
427  */
428 static inline bool
429 should_dump(struct msm_gem_submit *submit, int idx)
430 {
431 	extern bool rd_full;
432 	return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
433 }
434 
435 #endif /* __MSM_GEM_H__ */
436