msm_gem.h 10 KB

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