msm_gem.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. */
  6. #ifndef __MSM_GEM_H__
  7. #define __MSM_GEM_H__
  8. #include <linux/kref.h>
  9. #include <linux/dma-resv.h>
  10. #include "drm/gpu_scheduler.h"
  11. #include "msm_drv.h"
  12. /* Make all GEM related WARN_ON()s ratelimited.. when things go wrong they
  13. * tend to go wrong 1000s of times in a short timespan.
  14. */
  15. #define GEM_WARN_ON(x) WARN_RATELIMIT(x, "%s", __stringify(x))
  16. /* Additional internal-use only BO flags: */
  17. #define MSM_BO_STOLEN 0x10000000 /* try to use stolen/splash memory */
  18. #define MSM_BO_MAP_PRIV 0x20000000 /* use IOMMU_PRIV when mapping */
  19. struct msm_gem_address_space {
  20. const char *name;
  21. /* NOTE: mm managed at the page level, size is in # of pages
  22. * and position mm_node->start is in # of pages:
  23. */
  24. struct drm_mm mm;
  25. spinlock_t lock; /* Protects drm_mm node allocation/removal */
  26. struct msm_mmu *mmu;
  27. struct kref kref;
  28. /* For address spaces associated with a specific process, this
  29. * will be non-NULL:
  30. */
  31. struct pid *pid;
  32. /* @faults: the number of GPU hangs associated with this address space */
  33. int faults;
  34. /** @va_start: lowest possible address to allocate */
  35. uint64_t va_start;
  36. /** @va_size: the size of the address space (in bytes) */
  37. uint64_t va_size;
  38. };
  39. struct msm_gem_address_space *
  40. msm_gem_address_space_get(struct msm_gem_address_space *aspace);
  41. void msm_gem_address_space_put(struct msm_gem_address_space *aspace);
  42. struct msm_gem_address_space *
  43. msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
  44. u64 va_start, u64 size);
  45. struct msm_fence_context;
  46. struct msm_gem_vma {
  47. struct drm_mm_node node;
  48. uint64_t iova;
  49. struct msm_gem_address_space *aspace;
  50. struct list_head list; /* node in msm_gem_object::vmas */
  51. bool mapped;
  52. int inuse;
  53. uint32_t fence_mask;
  54. uint32_t fence[MSM_GPU_MAX_RINGS];
  55. struct msm_fence_context *fctx[MSM_GPU_MAX_RINGS];
  56. };
  57. int msm_gem_init_vma(struct msm_gem_address_space *aspace,
  58. struct msm_gem_vma *vma, int size,
  59. u64 range_start, u64 range_end);
  60. bool msm_gem_vma_inuse(struct msm_gem_vma *vma);
  61. void msm_gem_purge_vma(struct msm_gem_address_space *aspace,
  62. struct msm_gem_vma *vma);
  63. void msm_gem_unpin_vma(struct msm_gem_vma *vma);
  64. void msm_gem_unpin_vma_fenced(struct msm_gem_vma *vma, struct msm_fence_context *fctx);
  65. int msm_gem_map_vma(struct msm_gem_address_space *aspace,
  66. struct msm_gem_vma *vma, int prot,
  67. struct sg_table *sgt, int size);
  68. void msm_gem_close_vma(struct msm_gem_address_space *aspace,
  69. struct msm_gem_vma *vma);
  70. struct msm_gem_object {
  71. struct drm_gem_object base;
  72. uint32_t flags;
  73. /**
  74. * Advice: are the backing pages purgeable?
  75. */
  76. uint8_t madv;
  77. /**
  78. * count of active vmap'ing
  79. */
  80. uint8_t vmap_count;
  81. /**
  82. * Node in list of all objects (mainly for debugfs, protected by
  83. * priv->obj_lock
  84. */
  85. struct list_head node;
  86. struct page **pages;
  87. struct sg_table *sgt;
  88. void *vaddr;
  89. struct list_head vmas; /* list of msm_gem_vma */
  90. /* For physically contiguous buffers. Used when we don't have
  91. * an IOMMU. Also used for stolen/splashscreen buffer.
  92. */
  93. struct drm_mm_node *vram_node;
  94. char name[32]; /* Identifier to print for the debugfs files */
  95. int pin_count;
  96. };
  97. #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
  98. uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
  99. int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma);
  100. void msm_gem_unpin_locked(struct drm_gem_object *obj);
  101. struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
  102. struct msm_gem_address_space *aspace);
  103. int msm_gem_get_iova(struct drm_gem_object *obj,
  104. struct msm_gem_address_space *aspace, uint64_t *iova);
  105. int msm_gem_set_iova(struct drm_gem_object *obj,
  106. struct msm_gem_address_space *aspace, uint64_t iova);
  107. int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
  108. struct msm_gem_address_space *aspace, uint64_t *iova,
  109. u64 range_start, u64 range_end);
  110. int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
  111. struct msm_gem_address_space *aspace, uint64_t *iova);
  112. void msm_gem_unpin_iova(struct drm_gem_object *obj,
  113. struct msm_gem_address_space *aspace);
  114. struct page **msm_gem_pin_pages(struct drm_gem_object *obj);
  115. void msm_gem_unpin_pages(struct drm_gem_object *obj);
  116. int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  117. struct drm_mode_create_dumb *args);
  118. int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  119. uint32_t handle, uint64_t *offset);
  120. void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
  121. void *msm_gem_get_vaddr(struct drm_gem_object *obj);
  122. void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
  123. void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
  124. void msm_gem_put_vaddr(struct drm_gem_object *obj);
  125. int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
  126. bool msm_gem_active(struct drm_gem_object *obj);
  127. int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
  128. int msm_gem_cpu_fini(struct drm_gem_object *obj);
  129. int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
  130. uint32_t size, uint32_t flags, uint32_t *handle, char *name);
  131. struct drm_gem_object *msm_gem_new(struct drm_device *dev,
  132. uint32_t size, uint32_t flags);
  133. void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
  134. uint32_t flags, struct msm_gem_address_space *aspace,
  135. struct drm_gem_object **bo, uint64_t *iova);
  136. void msm_gem_kernel_put(struct drm_gem_object *bo,
  137. struct msm_gem_address_space *aspace);
  138. struct drm_gem_object *msm_gem_import(struct drm_device *dev,
  139. struct dma_buf *dmabuf, struct sg_table *sgt);
  140. __printf(2, 3)
  141. void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
  142. #ifdef CONFIG_DEBUG_FS
  143. struct msm_gem_stats {
  144. struct {
  145. unsigned count;
  146. size_t size;
  147. } all, active, resident, purgeable, purged;
  148. };
  149. void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
  150. struct msm_gem_stats *stats);
  151. void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
  152. #endif
  153. static inline void
  154. msm_gem_lock(struct drm_gem_object *obj)
  155. {
  156. dma_resv_lock(obj->resv, NULL);
  157. }
  158. static inline int
  159. msm_gem_lock_interruptible(struct drm_gem_object *obj)
  160. {
  161. return dma_resv_lock_interruptible(obj->resv, NULL);
  162. }
  163. static inline void
  164. msm_gem_unlock(struct drm_gem_object *obj)
  165. {
  166. dma_resv_unlock(obj->resv);
  167. }
  168. static inline void
  169. msm_gem_assert_locked(struct drm_gem_object *obj)
  170. {
  171. /*
  172. * Destroying the object is a special case.. msm_gem_free_object()
  173. * calls many things that WARN_ON if the obj lock is not held. But
  174. * acquiring the obj lock in msm_gem_free_object() can cause a
  175. * locking order inversion between reservation_ww_class_mutex and
  176. * fs_reclaim.
  177. *
  178. * This deadlock is not actually possible, because no one should
  179. * be already holding the lock when msm_gem_free_object() is called.
  180. * Unfortunately lockdep is not aware of this detail. So when the
  181. * refcount drops to zero, we pretend it is already locked.
  182. */
  183. lockdep_assert_once(
  184. (kref_read(&obj->refcount) == 0) ||
  185. (lockdep_is_held(&obj->resv->lock.base) != LOCK_STATE_NOT_HELD)
  186. );
  187. }
  188. /* imported/exported objects are not purgeable: */
  189. static inline bool is_unpurgeable(struct msm_gem_object *msm_obj)
  190. {
  191. return msm_obj->base.import_attach || msm_obj->pin_count;
  192. }
  193. static inline bool is_purgeable(struct msm_gem_object *msm_obj)
  194. {
  195. return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
  196. !is_unpurgeable(msm_obj);
  197. }
  198. static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
  199. {
  200. msm_gem_assert_locked(&msm_obj->base);
  201. return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
  202. }
  203. static inline bool is_unevictable(struct msm_gem_object *msm_obj)
  204. {
  205. return is_unpurgeable(msm_obj) || msm_obj->vaddr;
  206. }
  207. void msm_gem_purge(struct drm_gem_object *obj);
  208. void msm_gem_evict(struct drm_gem_object *obj);
  209. void msm_gem_vunmap(struct drm_gem_object *obj);
  210. /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
  211. * associated with the cmdstream submission for synchronization (and
  212. * make it easier to unwind when things go wrong, etc).
  213. */
  214. struct msm_gem_submit {
  215. struct drm_sched_job base;
  216. struct kref ref;
  217. struct drm_device *dev;
  218. struct msm_gpu *gpu;
  219. struct msm_gem_address_space *aspace;
  220. struct list_head node; /* node in ring submit list */
  221. struct ww_acquire_ctx ticket;
  222. uint32_t seqno; /* Sequence number of the submit on the ring */
  223. /* Hw fence, which is created when the scheduler executes the job, and
  224. * is signaled when the hw finishes (via seqno write from cmdstream)
  225. */
  226. struct dma_fence *hw_fence;
  227. /* Userspace visible fence, which is signaled by the scheduler after
  228. * the hw_fence is signaled.
  229. */
  230. struct dma_fence *user_fence;
  231. int fence_id; /* key into queue->fence_idr */
  232. struct msm_gpu_submitqueue *queue;
  233. struct pid *pid; /* submitting process */
  234. bool fault_dumped; /* Limit devcoredump dumping to one per submit */
  235. bool valid; /* true if no cmdstream patching needed */
  236. bool in_rb; /* "sudo" mode, copy cmds into RB */
  237. struct msm_ringbuffer *ring;
  238. unsigned int nr_cmds;
  239. unsigned int nr_bos;
  240. u32 ident; /* A "identifier" for the submit for logging */
  241. struct {
  242. uint32_t type;
  243. uint32_t size; /* in dwords */
  244. uint64_t iova;
  245. uint32_t offset;/* in dwords */
  246. uint32_t idx; /* cmdstream buffer idx in bos[] */
  247. uint32_t nr_relocs;
  248. struct drm_msm_gem_submit_reloc *relocs;
  249. } *cmd; /* array of size nr_cmds */
  250. struct {
  251. /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
  252. #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */
  253. #define BO_LOCKED 0x4000 /* obj lock is held */
  254. #define BO_OBJ_PINNED 0x2000 /* obj (pages) is pinned and on active list */
  255. #define BO_VMA_PINNED 0x1000 /* vma (virtual address) is pinned */
  256. uint32_t flags;
  257. union {
  258. struct msm_gem_object *obj;
  259. uint32_t handle;
  260. };
  261. uint64_t iova;
  262. struct msm_gem_vma *vma;
  263. } bos[];
  264. };
  265. static inline struct msm_gem_submit *to_msm_submit(struct drm_sched_job *job)
  266. {
  267. return container_of(job, struct msm_gem_submit, base);
  268. }
  269. void __msm_gem_submit_destroy(struct kref *kref);
  270. static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
  271. {
  272. kref_get(&submit->ref);
  273. }
  274. static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
  275. {
  276. kref_put(&submit->ref, __msm_gem_submit_destroy);
  277. }
  278. void msm_submit_retire(struct msm_gem_submit *submit);
  279. /* helper to determine of a buffer in submit should be dumped, used for both
  280. * devcoredump and debugfs cmdstream dumping:
  281. */
  282. static inline bool
  283. should_dump(struct msm_gem_submit *submit, int idx)
  284. {
  285. extern bool rd_full;
  286. return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
  287. }
  288. #endif /* __MSM_GEM_H__ */