xref: /linux/drivers/gpu/drm/msm/msm_gem.h (revision fdc290ff4ab19c7e0dde36c4cd1e2771b61f6bf5)
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 "msm_mmu.h"
11 #include <linux/kref.h>
12 #include <linux/dma-resv.h>
13 #include "drm/drm_exec.h"
14 #include "drm/drm_gpuvm.h"
15 #include "drm/gpu_scheduler.h"
16 #include "msm_drv.h"
17 
18 /* Make all GEM related WARN_ON()s ratelimited.. when things go wrong they
19  * tend to go wrong 1000s of times in a short timespan.
20  */
21 #define GEM_WARN_ON(x)  WARN_RATELIMIT(x, "%s", __stringify(x))
22 
23 /* Additional internal-use only BO flags: */
24 #define MSM_BO_STOLEN        0x10000000    /* try to use stolen/splash memory */
25 #define MSM_BO_MAP_PRIV      0x20000000    /* use IOMMU_PRIV when mapping */
26 
27 /**
28  * struct msm_gem_vm_log_entry - An entry in the VM log
29  *
30  * For userspace managed VMs, a log of recent VM updates is tracked and
31  * captured in GPU devcore dumps, to aid debugging issues caused by (for
32  * example) incorrectly synchronized VM updates
33  */
34 struct msm_gem_vm_log_entry {
35 	const char *op;
36 	uint64_t iova;
37 	uint64_t range;
38 	int queue_id;
39 };
40 
41 /**
42  * struct msm_gem_vm - VM object
43  *
44  * A VM object representing a GPU (or display or GMU or ...) virtual address
45  * space.
46  *
47  * In the case of GPU, if per-process address spaces are supported, the address
48  * space is split into two VMs, which map to TTBR0 and TTBR1 in the SMMU.  TTBR0
49  * is used for userspace objects, and is unique per msm_context/drm_file, while
50  * TTBR1 is the same for all processes.  (The kernel controlled ringbuffer and
51  * a few other kernel controlled buffers live in TTBR1.)
52  *
53  * The GPU TTBR0 vm can be managed by userspace or by the kernel, depending on
54  * whether userspace supports VM_BIND.  All other vm's are managed by the kernel.
55  * (Managed by kernel means the kernel is responsible for VA allocation.)
56  *
57  * Note that because VM_BIND allows a given BO to be mapped multiple times in
58  * a VM, and therefore have multiple VMA's in a VM, there is an extra object
59  * provided by drm_gpuvm infrastructure.. the drm_gpuvm_bo, which is not
60  * embedded in any larger driver structure.  The GEM object holds a list of
61  * drm_gpuvm_bo, which in turn holds a list of msm_gem_vma.  A linked vma
62  * holds a reference to the vm_bo, and drops it when the vma is unlinked.
63  * So we just need to call drm_gpuvm_bo_obtain_locked() to return a ref to an
64  * existing vm_bo, or create a new one.  Once the vma is linked, the ref
65  * to the vm_bo can be dropped (since the vma is holding one).
66  */
67 struct msm_gem_vm {
68 	/** @base: Inherit from drm_gpuvm. */
69 	struct drm_gpuvm base;
70 
71 	/**
72 	 * @sched: Scheduler used for asynchronous VM_BIND request.
73 	 *
74 	 * Unused for kernel managed VMs (where all operations are synchronous).
75 	 */
76 	struct drm_gpu_scheduler sched;
77 
78 	/**
79 	 * @prealloc_throttle: Used to throttle VM_BIND ops if too much pre-
80 	 * allocated memory is in flight.
81 	 *
82 	 * Because we have to pre-allocate pgtable pages for the worst case
83 	 * (ie. new mappings do not share any PTEs with existing mappings)
84 	 * we could end up consuming a lot of resources transiently.  The
85 	 * prealloc_throttle puts an upper bound on that.
86 	 */
87 	struct {
88 		/** @wait: Notified when preallocated resources are released */
89 		wait_queue_head_t wait;
90 
91 		/**
92 		 * @in_flight: The # of preallocated pgtable pages in-flight
93 		 * for queued VM_BIND jobs.
94 		 */
95 		atomic_t in_flight;
96 	} prealloc_throttle;
97 
98 	/**
99 	 * @mm: Memory management for kernel managed VA allocations
100 	 *
101 	 * Only used for kernel managed VMs, unused for user managed VMs.
102 	 *
103 	 * Protected by vm lock.  See msm_gem_lock_vm_and_obj(), for ex.
104 	 */
105 	struct drm_mm mm;
106 
107 	/** @mmu: The mmu object which manages the pgtables */
108 	struct msm_mmu *mmu;
109 
110 	/** @mmu_lock: Protects access to the mmu */
111 	struct mutex mmu_lock;
112 
113 	/**
114 	 * @pid: For address spaces associated with a specific process, this
115 	 * will be non-NULL:
116 	 */
117 	struct pid *pid;
118 
119 	/** @last_fence: Fence for last pending work scheduled on the VM */
120 	struct dma_fence *last_fence;
121 
122 	/** @log: A log of recent VM updates */
123 	struct msm_gem_vm_log_entry *log;
124 
125 	/** @log_shift: length of @log is (1 << @log_shift) */
126 	uint32_t log_shift;
127 
128 	/** @log_idx: index of next @log entry to write */
129 	uint32_t log_idx;
130 
131 	/** @faults: the number of GPU hangs associated with this address space */
132 	int faults;
133 
134 	/** @managed: is this a kernel managed VM? */
135 	bool managed;
136 
137 	/**
138 	 * @unusable: True if the VM has turned unusable because something
139 	 * bad happened during an asynchronous request.
140 	 *
141 	 * We don't try to recover from such failures, because this implies
142 	 * informing userspace about the specific operation that failed, and
143 	 * hoping the userspace driver can replay things from there. This all
144 	 * sounds very complicated for little gain.
145 	 *
146 	 * Instead, we should just flag the VM as unusable, and fail any
147 	 * further request targeting this VM.
148 	 *
149 	 * As an analogy, this would be mapped to a VK_ERROR_DEVICE_LOST
150 	 * situation, where the logical device needs to be re-created.
151 	 */
152 	bool unusable;
153 };
154 #define to_msm_vm(x) container_of(x, struct msm_gem_vm, base)
155 
156 struct drm_gpuvm *
157 msm_gem_vm_create(struct drm_device *drm, struct msm_mmu *mmu, const char *name,
158 		  u64 va_start, u64 va_size, bool managed);
159 
160 void msm_gem_vm_close(struct drm_gpuvm *gpuvm);
161 void msm_gem_vm_unusable(struct drm_gpuvm *gpuvm);
162 
163 struct msm_fence_context;
164 
165 #define MSM_VMA_DUMP (DRM_GPUVA_USERBITS << 0)
166 
167 /**
168  * struct msm_gem_vma - a VMA mapping
169  *
170  * Represents a combination of a GEM object plus a VM.
171  */
172 struct msm_gem_vma {
173 	/** @base: inherit from drm_gpuva */
174 	struct drm_gpuva base;
175 
176 	/**
177 	 * @node: mm node for VA allocation
178 	 *
179 	 * Only used by kernel managed VMs
180 	 */
181 	struct drm_mm_node node;
182 
183 	/** @mapped: Is this VMA mapped? */
184 	bool mapped;
185 };
186 #define to_msm_vma(x) container_of(x, struct msm_gem_vma, base)
187 
188 struct drm_gpuva *
189 msm_gem_vma_new(struct drm_gpuvm *vm, struct drm_gem_object *obj,
190 		u64 offset, u64 range_start, u64 range_end);
191 void msm_gem_vma_unmap(struct drm_gpuva *vma, const char *reason);
192 int msm_gem_vma_map(struct drm_gpuva *vma, int prot, struct sg_table *sgt);
193 void msm_gem_vma_close(struct drm_gpuva *vma);
194 
195 struct msm_gem_object {
196 	struct drm_gem_object base;
197 
198 	uint32_t flags;
199 
200 	/**
201 	 * madv: are the backing pages purgeable?
202 	 *
203 	 * Protected by obj lock and LRU lock
204 	 */
205 	uint8_t madv;
206 
207 	/**
208 	 * count of active vmap'ing
209 	 */
210 	uint8_t vmap_count;
211 
212 	/**
213 	 * Node in list of all objects (mainly for debugfs, protected by
214 	 * priv->obj_lock
215 	 */
216 	struct list_head node;
217 
218 	struct page **pages;
219 	struct sg_table *sgt;
220 	void *vaddr;
221 
222 	char name[32]; /* Identifier to print for the debugfs files */
223 
224 	/* userspace metadata backchannel */
225 	void *metadata;
226 	u32 metadata_size;
227 
228 	/**
229 	 * pin_count: Number of times the pages are pinned
230 	 *
231 	 * Protected by LRU lock.
232 	 */
233 	int pin_count;
234 
235 	/**
236 	 * @vma_ref: Reference count of VMA users.
237 	 *
238 	 * With the vm_bo/vma holding a reference to the GEM object, we'd
239 	 * otherwise have to actively tear down a VMA when, for example,
240 	 * a buffer is unpinned for scanout, vs. the pre-drm_gpuvm approach
241 	 * where a VMA did not hold a reference to the BO, but instead was
242 	 * implicitly torn down when the BO was freed.
243 	 *
244 	 * To regain the lazy VMA teardown, we use the @vma_ref.  It is
245 	 * incremented for any of the following:
246 	 *
247 	 * 1) the BO is exported as a dma_buf
248 	 * 2) the BO has open userspace handle
249 	 *
250 	 * All of those conditions will hold an reference to the BO,
251 	 * preventing it from being freed.  So lazily keeping around the
252 	 * VMA will not prevent the BO from being freed.  (Or rather, the
253 	 * reference loop is harmless in this case.)
254 	 *
255 	 * When the @vma_ref drops to zero, then kms->vm VMA will be
256 	 * torn down.
257 	 */
258 	atomic_t vma_ref;
259 };
260 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
261 
262 void msm_gem_vma_get(struct drm_gem_object *obj);
263 void msm_gem_vma_put(struct drm_gem_object *obj);
264 
265 int msm_gem_prot(struct drm_gem_object *obj);
266 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct drm_gpuva *vma);
267 void msm_gem_unpin_locked(struct drm_gem_object *obj);
268 void msm_gem_unpin_active(struct drm_gem_object *obj);
269 struct drm_gpuva *msm_gem_get_vma_locked(struct drm_gem_object *obj,
270 					 struct drm_gpuvm *vm);
271 int msm_gem_get_iova(struct drm_gem_object *obj, struct drm_gpuvm *vm,
272 		     uint64_t *iova);
273 int msm_gem_set_iova(struct drm_gem_object *obj, struct drm_gpuvm *vm,
274 		     uint64_t iova);
275 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
276 				   struct drm_gpuvm *vm, uint64_t *iova,
277 				   u64 range_start, u64 range_end);
278 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj, struct drm_gpuvm *vm,
279 			     uint64_t *iova);
280 void msm_gem_unpin_iova(struct drm_gem_object *obj, struct drm_gpuvm *vm);
281 void msm_gem_pin_obj_locked(struct drm_gem_object *obj);
282 struct page **msm_gem_get_pages_locked(struct drm_gem_object *obj, unsigned madv);
283 struct page **msm_gem_pin_pages_locked(struct drm_gem_object *obj);
284 void msm_gem_unpin_pages_locked(struct drm_gem_object *obj);
285 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
286 		struct drm_mode_create_dumb *args);
287 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
288 void *msm_gem_get_vaddr(struct drm_gem_object *obj);
289 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
290 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
291 void msm_gem_put_vaddr(struct drm_gem_object *obj);
292 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
293 bool msm_gem_active(struct drm_gem_object *obj);
294 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
295 int msm_gem_cpu_fini(struct drm_gem_object *obj);
296 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
297 		size_t size, uint32_t flags, uint32_t *handle, char *name);
298 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
299 		size_t size, uint32_t flags, struct drm_gem_object *r_obj);
300 void *msm_gem_kernel_new(struct drm_device *dev, size_t size, uint32_t flags,
301 			 struct drm_gpuvm *vm, struct drm_gem_object **bo,
302 			 uint64_t *iova);
303 void msm_gem_kernel_put(struct drm_gem_object *bo, struct drm_gpuvm *vm);
304 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
305 				      struct dma_buf_attachment *attach,
306 				      struct sg_table *sgt);
307 __printf(2, 3)
308 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
309 
310 #ifdef CONFIG_DEBUG_FS
311 struct msm_gem_stats {
312 	struct {
313 		unsigned count;
314 		size_t size;
315 	} all, active, resident, purgeable, purged;
316 };
317 
318 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
319 		struct msm_gem_stats *stats);
320 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
321 #endif
322 
323 static inline void
324 msm_gem_lock(struct drm_gem_object *obj)
325 {
326 	dma_resv_lock(obj->resv, NULL);
327 }
328 
329 static inline bool __must_check
330 msm_gem_trylock(struct drm_gem_object *obj)
331 {
332 	return dma_resv_trylock(obj->resv);
333 }
334 
335 static inline int
336 msm_gem_lock_interruptible(struct drm_gem_object *obj)
337 {
338 	return dma_resv_lock_interruptible(obj->resv, NULL);
339 }
340 
341 static inline void
342 msm_gem_unlock(struct drm_gem_object *obj)
343 {
344 	dma_resv_unlock(obj->resv);
345 }
346 
347 /**
348  * msm_gem_lock_vm_and_obj() - Helper to lock an obj + VM
349  * @exec: the exec context helper which will be initalized
350  * @obj: the GEM object to lock
351  * @vm: the VM to lock
352  *
353  * Operations which modify a VM frequently need to lock both the VM and
354  * the object being mapped/unmapped/etc.  This helper uses drm_exec to
355  * acquire both locks, dealing with potential deadlock/backoff scenarios
356  * which arise when multiple locks are involved.
357  */
358 static inline int
359 msm_gem_lock_vm_and_obj(struct drm_exec *exec,
360 			struct drm_gem_object *obj,
361 			struct drm_gpuvm *vm)
362 {
363 	int ret = 0;
364 
365 	drm_exec_init(exec, 0, 2);
366 	drm_exec_until_all_locked (exec) {
367 		ret = drm_exec_lock_obj(exec, drm_gpuvm_resv_obj(vm));
368 		if (!ret && (obj->resv != drm_gpuvm_resv(vm)))
369 			ret = drm_exec_lock_obj(exec, obj);
370 		drm_exec_retry_on_contention(exec);
371 		if (GEM_WARN_ON(ret))
372 			break;
373 	}
374 
375 	return ret;
376 }
377 
378 static inline void
379 msm_gem_assert_locked(struct drm_gem_object *obj)
380 {
381 	/*
382 	 * Destroying the object is a special case.. msm_gem_free_object()
383 	 * calls many things that WARN_ON if the obj lock is not held.  But
384 	 * acquiring the obj lock in msm_gem_free_object() can cause a
385 	 * locking order inversion between reservation_ww_class_mutex and
386 	 * fs_reclaim.
387 	 *
388 	 * This deadlock is not actually possible, because no one should
389 	 * be already holding the lock when msm_gem_free_object() is called.
390 	 * Unfortunately lockdep is not aware of this detail.  So when the
391 	 * refcount drops to zero, we pretend it is already locked.
392 	 */
393 	lockdep_assert_once(
394 		(kref_read(&obj->refcount) == 0) ||
395 		(lockdep_is_held(&obj->resv->lock.base) != LOCK_STATE_NOT_HELD)
396 	);
397 }
398 
399 /* imported/exported objects are not purgeable: */
400 static inline bool is_unpurgeable(struct msm_gem_object *msm_obj)
401 {
402 	return drm_gem_is_imported(&msm_obj->base) || msm_obj->pin_count;
403 }
404 
405 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
406 {
407 	return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
408 			!is_unpurgeable(msm_obj);
409 }
410 
411 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
412 {
413 	msm_gem_assert_locked(&msm_obj->base);
414 	return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
415 }
416 
417 static inline bool is_unevictable(struct msm_gem_object *msm_obj)
418 {
419 	return is_unpurgeable(msm_obj) || msm_obj->vaddr;
420 }
421 
422 void msm_gem_purge(struct drm_gem_object *obj);
423 void msm_gem_evict(struct drm_gem_object *obj);
424 void msm_gem_vunmap(struct drm_gem_object *obj);
425 
426 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
427  * associated with the cmdstream submission for synchronization (and
428  * make it easier to unwind when things go wrong, etc).
429  */
430 struct msm_gem_submit {
431 	struct drm_sched_job base;
432 	struct kref ref;
433 	struct drm_device *dev;
434 	struct msm_gpu *gpu;
435 	struct drm_gpuvm *vm;
436 	struct list_head node;   /* node in ring submit list */
437 	struct drm_exec exec;
438 	uint32_t seqno;		/* Sequence number of the submit on the ring */
439 
440 	/* Hw fence, which is created when the scheduler executes the job, and
441 	 * is signaled when the hw finishes (via seqno write from cmdstream)
442 	 */
443 	struct dma_fence *hw_fence;
444 
445 	/* Userspace visible fence, which is signaled by the scheduler after
446 	 * the hw_fence is signaled.
447 	 */
448 	struct dma_fence *user_fence;
449 
450 	int fence_id;       /* key into queue->fence_idr */
451 	struct msm_gpu_submitqueue *queue;
452 	struct pid *pid;    /* submitting process */
453 	bool bos_pinned : 1;
454 	bool fault_dumped:1;/* Limit devcoredump dumping to one per submit */
455 	bool in_rb : 1;     /* "sudo" mode, copy cmds into RB */
456 	bool has_exec : 1;  /* @exec is initialized. */
457 	struct msm_ringbuffer *ring;
458 	unsigned int nr_cmds;
459 	unsigned int nr_bos;
460 	u32 ident;	   /* A "identifier" for the submit for logging */
461 	struct {
462 		uint32_t type;
463 		uint32_t size;  /* in dwords */
464 		uint64_t iova;
465 		uint32_t offset;/* in dwords */
466 		uint32_t idx;   /* cmdstream buffer idx in bos[] */
467 		uint32_t nr_relocs;
468 		struct drm_msm_gem_submit_reloc *relocs;
469 	} *cmd;  /* array of size nr_cmds */
470 	struct {
471 		uint32_t flags;
472 		union {
473 			struct drm_gem_object *obj;
474 			uint32_t handle;
475 		};
476 		struct drm_gpuvm_bo *vm_bo;
477 		uint64_t iova;
478 	} bos[];
479 };
480 
481 static inline struct msm_gem_submit *to_msm_submit(struct drm_sched_job *job)
482 {
483 	return container_of(job, struct msm_gem_submit, base);
484 }
485 
486 void __msm_gem_submit_destroy(struct kref *kref);
487 
488 static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
489 {
490 	kref_get(&submit->ref);
491 }
492 
493 static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
494 {
495 	kref_put(&submit->ref, __msm_gem_submit_destroy);
496 }
497 
498 void msm_submit_retire(struct msm_gem_submit *submit);
499 
500 #endif /* __MSM_GEM_H__ */
501