drm_exec.c 8.7 KB

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