drm_exec.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. #include <drm/drm_exec.h>
  3. #include <drm/drm_gem.h>
  4. #include <linux/dma-resv.h>
  5. /**
  6. * DOC: Overview
  7. *
  8. * This component mainly abstracts the retry loop necessary for locking
  9. * multiple GEM objects while preparing hardware operations (e.g. command
  10. * submissions, page table updates etc..).
  11. *
  12. * If a contention is detected while locking a GEM object the cleanup procedure
  13. * unlocks all previously locked GEM objects and locks the contended one first
  14. * before locking any further objects.
  15. *
  16. * After an object is locked fences slots can optionally be reserved on the
  17. * dma_resv object inside the GEM object.
  18. *
  19. * A typical usage pattern should look like this::
  20. *
  21. * struct drm_gem_object *obj;
  22. * struct drm_exec exec;
  23. * unsigned long index;
  24. * int ret;
  25. *
  26. * drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
  27. * drm_exec_until_all_locked(&exec) {
  28. * ret = drm_exec_prepare_obj(&exec, boA, 1);
  29. * drm_exec_retry_on_contention(&exec);
  30. * if (ret)
  31. * goto error;
  32. *
  33. * ret = drm_exec_prepare_obj(&exec, boB, 1);
  34. * drm_exec_retry_on_contention(&exec);
  35. * if (ret)
  36. * goto error;
  37. * }
  38. *
  39. * drm_exec_for_each_locked_object(&exec, index, obj) {
  40. * dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
  41. * ...
  42. * }
  43. * drm_exec_fini(&exec);
  44. *
  45. * See struct dma_exec for more details.
  46. */
  47. /* Dummy value used to initially enter the retry loop */
  48. #define DRM_EXEC_DUMMY ((void *)~0)
  49. /* Unlock all objects and drop references */
  50. static void drm_exec_unlock_all(struct drm_exec *exec)
  51. {
  52. struct drm_gem_object *obj;
  53. unsigned long index;
  54. drm_exec_for_each_locked_object_reverse(exec, index, obj) {
  55. dma_resv_unlock(obj->resv);
  56. drm_gem_object_put(obj);
  57. }
  58. drm_gem_object_put(exec->prelocked);
  59. exec->prelocked = NULL;
  60. }
  61. /**
  62. * drm_exec_init - initialize a drm_exec object
  63. * @exec: the drm_exec object to initialize
  64. * @flags: controls locking behavior, see DRM_EXEC_* defines
  65. * @nr: the initial # of objects
  66. *
  67. * Initialize the object and make sure that we can track locked objects.
  68. *
  69. * If nr is non-zero then it is used as the initial objects table size.
  70. * In either case, the table will grow (be re-allocated) on demand.
  71. */
  72. void drm_exec_init(struct drm_exec *exec, u32 flags, unsigned nr)
  73. {
  74. if (!nr)
  75. nr = PAGE_SIZE / sizeof(void *);
  76. exec->flags = flags;
  77. exec->objects = kvmalloc_array(nr, sizeof(void *), GFP_KERNEL);
  78. /* If allocation here fails, just delay that till the first use */
  79. exec->max_objects = exec->objects ? nr : 0;
  80. exec->num_objects = 0;
  81. exec->contended = DRM_EXEC_DUMMY;
  82. exec->prelocked = NULL;
  83. }
  84. EXPORT_SYMBOL(drm_exec_init);
  85. /**
  86. * drm_exec_fini - finalize a drm_exec object
  87. * @exec: the drm_exec object to finalize
  88. *
  89. * Unlock all locked objects, drop the references to objects and free all memory
  90. * used for tracking the state.
  91. */
  92. void drm_exec_fini(struct drm_exec *exec)
  93. {
  94. drm_exec_unlock_all(exec);
  95. kvfree(exec->objects);
  96. if (exec->contended != DRM_EXEC_DUMMY) {
  97. drm_gem_object_put(exec->contended);
  98. ww_acquire_fini(&exec->ticket);
  99. }
  100. }
  101. EXPORT_SYMBOL(drm_exec_fini);
  102. /**
  103. * drm_exec_cleanup - cleanup when contention is detected
  104. * @exec: the drm_exec object to cleanup
  105. *
  106. * Cleanup the current state and return true if we should stay inside the retry
  107. * loop, false if there wasn't any contention detected and we can keep the
  108. * objects locked.
  109. */
  110. bool drm_exec_cleanup(struct drm_exec *exec)
  111. {
  112. if (likely(!exec->contended)) {
  113. ww_acquire_done(&exec->ticket);
  114. return false;
  115. }
  116. if (likely(exec->contended == DRM_EXEC_DUMMY)) {
  117. exec->contended = NULL;
  118. ww_acquire_init(&exec->ticket, &reservation_ww_class);
  119. return true;
  120. }
  121. drm_exec_unlock_all(exec);
  122. exec->num_objects = 0;
  123. return true;
  124. }
  125. EXPORT_SYMBOL(drm_exec_cleanup);
  126. /* Track the locked object in the array */
  127. static int drm_exec_obj_locked(struct drm_exec *exec,
  128. struct drm_gem_object *obj)
  129. {
  130. if (unlikely(exec->num_objects == exec->max_objects)) {
  131. size_t size = exec->max_objects * sizeof(void *);
  132. void *tmp;
  133. tmp = kvrealloc(exec->objects, size + PAGE_SIZE, GFP_KERNEL);
  134. if (!tmp)
  135. return -ENOMEM;
  136. exec->objects = tmp;
  137. exec->max_objects += PAGE_SIZE / sizeof(void *);
  138. }
  139. drm_gem_object_get(obj);
  140. exec->objects[exec->num_objects++] = obj;
  141. return 0;
  142. }
  143. /* Make sure the contended object is locked first */
  144. static int drm_exec_lock_contended(struct drm_exec *exec)
  145. {
  146. struct drm_gem_object *obj = exec->contended;
  147. int ret;
  148. if (likely(!obj))
  149. return 0;
  150. /* Always cleanup the contention so that error handling can kick in */
  151. exec->contended = NULL;
  152. if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {
  153. ret = dma_resv_lock_slow_interruptible(obj->resv,
  154. &exec->ticket);
  155. if (unlikely(ret))
  156. goto error_dropref;
  157. } else {
  158. dma_resv_lock_slow(obj->resv, &exec->ticket);
  159. }
  160. ret = drm_exec_obj_locked(exec, obj);
  161. if (unlikely(ret))
  162. goto error_unlock;
  163. exec->prelocked = obj;
  164. return 0;
  165. error_unlock:
  166. dma_resv_unlock(obj->resv);
  167. error_dropref:
  168. drm_gem_object_put(obj);
  169. return ret;
  170. }
  171. /**
  172. * drm_exec_lock_obj - lock a GEM object for use
  173. * @exec: the drm_exec object with the state
  174. * @obj: the GEM object to lock
  175. *
  176. * Lock a GEM object for use and grab a reference to it.
  177. *
  178. * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
  179. * already locked (can be suppressed by setting the DRM_EXEC_IGNORE_DUPLICATES
  180. * flag), -ENOMEM when memory allocation failed and zero for success.
  181. */
  182. int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj)
  183. {
  184. int ret;
  185. ret = drm_exec_lock_contended(exec);
  186. if (unlikely(ret))
  187. return ret;
  188. if (exec->prelocked == obj) {
  189. drm_gem_object_put(exec->prelocked);
  190. exec->prelocked = NULL;
  191. return 0;
  192. }
  193. if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT)
  194. ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
  195. else
  196. ret = dma_resv_lock(obj->resv, &exec->ticket);
  197. if (unlikely(ret == -EDEADLK)) {
  198. drm_gem_object_get(obj);
  199. exec->contended = obj;
  200. return -EDEADLK;
  201. }
  202. if (unlikely(ret == -EALREADY) &&
  203. exec->flags & DRM_EXEC_IGNORE_DUPLICATES)
  204. return 0;
  205. if (unlikely(ret))
  206. return ret;
  207. ret = drm_exec_obj_locked(exec, obj);
  208. if (ret)
  209. goto error_unlock;
  210. return 0;
  211. error_unlock:
  212. dma_resv_unlock(obj->resv);
  213. return ret;
  214. }
  215. EXPORT_SYMBOL(drm_exec_lock_obj);
  216. /**
  217. * drm_exec_unlock_obj - unlock a GEM object in this exec context
  218. * @exec: the drm_exec object with the state
  219. * @obj: the GEM object to unlock
  220. *
  221. * Unlock the GEM object and remove it from the collection of locked objects.
  222. * Should only be used to unlock the most recently locked objects. It's not time
  223. * efficient to unlock objects locked long ago.
  224. */
  225. void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj)
  226. {
  227. unsigned int i;
  228. for (i = exec->num_objects; i--;) {
  229. if (exec->objects[i] == obj) {
  230. dma_resv_unlock(obj->resv);
  231. for (++i; i < exec->num_objects; ++i)
  232. exec->objects[i - 1] = exec->objects[i];
  233. --exec->num_objects;
  234. drm_gem_object_put(obj);
  235. return;
  236. }
  237. }
  238. }
  239. EXPORT_SYMBOL(drm_exec_unlock_obj);
  240. /**
  241. * drm_exec_prepare_obj - prepare a GEM object for use
  242. * @exec: the drm_exec object with the state
  243. * @obj: the GEM object to prepare
  244. * @num_fences: how many fences to reserve
  245. *
  246. * Prepare a GEM object for use by locking it and reserving fence slots.
  247. *
  248. * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
  249. * already locked, -ENOMEM when memory allocation failed and zero for success.
  250. */
  251. int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
  252. unsigned int num_fences)
  253. {
  254. int ret;
  255. ret = drm_exec_lock_obj(exec, obj);
  256. if (ret)
  257. return ret;
  258. ret = dma_resv_reserve_fences(obj->resv, num_fences);
  259. if (ret) {
  260. drm_exec_unlock_obj(exec, obj);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(drm_exec_prepare_obj);
  266. /**
  267. * drm_exec_prepare_array - helper to prepare an array of objects
  268. * @exec: the drm_exec object with the state
  269. * @objects: array of GEM object to prepare
  270. * @num_objects: number of GEM objects in the array
  271. * @num_fences: number of fences to reserve on each GEM object
  272. *
  273. * Prepares all GEM objects in an array, aborts on first error.
  274. * Reserves @num_fences on each GEM object after locking it.
  275. *
  276. * Returns: -EDEADLOCK on contention, -EALREADY when object is already locked,
  277. * -ENOMEM when memory allocation failed and zero for success.
  278. */
  279. int drm_exec_prepare_array(struct drm_exec *exec,
  280. struct drm_gem_object **objects,
  281. unsigned int num_objects,
  282. unsigned int num_fences)
  283. {
  284. int ret;
  285. for (unsigned int i = 0; i < num_objects; ++i) {
  286. ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
  287. if (unlikely(ret))
  288. return ret;
  289. }
  290. return 0;
  291. }
  292. EXPORT_SYMBOL(drm_exec_prepare_array);
  293. MODULE_DESCRIPTION("DRM execution context");
  294. MODULE_LICENSE("Dual MIT/GPL");