msm_gem.h 15 KB

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