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