drm_suballoc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2011 Red Hat Inc.
  4. * Copyright 2023 Intel Corporation.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * The above copyright notice and this permission notice (including the
  24. * next paragraph) shall be included in all copies or substantial portions
  25. * of the Software.
  26. *
  27. */
  28. /* Algorithm:
  29. *
  30. * We store the last allocated bo in "hole", we always try to allocate
  31. * after the last allocated bo. Principle is that in a linear GPU ring
  32. * progression was is after last is the oldest bo we allocated and thus
  33. * the first one that should no longer be in use by the GPU.
  34. *
  35. * If it's not the case we skip over the bo after last to the closest
  36. * done bo if such one exist. If none exist and we are not asked to
  37. * block we report failure to allocate.
  38. *
  39. * If we are asked to block we wait on all the oldest fence of all
  40. * rings. We just wait for any of those fence to complete.
  41. */
  42. #include <drm/drm_suballoc.h>
  43. #include <drm/drm_print.h>
  44. #include <linux/slab.h>
  45. #include <linux/sched.h>
  46. #include <linux/wait.h>
  47. #include <linux/dma-fence.h>
  48. static void drm_suballoc_remove_locked(struct drm_suballoc *sa);
  49. static void drm_suballoc_try_free(struct drm_suballoc_manager *sa_manager);
  50. /**
  51. * drm_suballoc_manager_init() - Initialise the drm_suballoc_manager
  52. * @sa_manager: pointer to the sa_manager
  53. * @size: number of bytes we want to suballocate
  54. * @align: alignment for each suballocated chunk
  55. *
  56. * Prepares the suballocation manager for suballocations.
  57. */
  58. void drm_suballoc_manager_init(struct drm_suballoc_manager *sa_manager,
  59. size_t size, size_t align)
  60. {
  61. unsigned int i;
  62. BUILD_BUG_ON(!is_power_of_2(DRM_SUBALLOC_MAX_QUEUES));
  63. if (!align)
  64. align = 1;
  65. /* alignment must be a power of 2 */
  66. if (WARN_ON_ONCE(align & (align - 1)))
  67. align = roundup_pow_of_two(align);
  68. init_waitqueue_head(&sa_manager->wq);
  69. sa_manager->size = size;
  70. sa_manager->align = align;
  71. sa_manager->hole = &sa_manager->olist;
  72. INIT_LIST_HEAD(&sa_manager->olist);
  73. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  74. INIT_LIST_HEAD(&sa_manager->flist[i]);
  75. }
  76. EXPORT_SYMBOL(drm_suballoc_manager_init);
  77. /**
  78. * drm_suballoc_manager_fini() - Destroy the drm_suballoc_manager
  79. * @sa_manager: pointer to the sa_manager
  80. *
  81. * Cleans up the suballocation manager after use. All fences added
  82. * with drm_suballoc_free() must be signaled, or we cannot clean up
  83. * the entire manager.
  84. */
  85. void drm_suballoc_manager_fini(struct drm_suballoc_manager *sa_manager)
  86. {
  87. struct drm_suballoc *sa, *tmp;
  88. if (!sa_manager->size)
  89. return;
  90. if (!list_empty(&sa_manager->olist)) {
  91. sa_manager->hole = &sa_manager->olist;
  92. drm_suballoc_try_free(sa_manager);
  93. if (!list_empty(&sa_manager->olist))
  94. DRM_ERROR("sa_manager is not empty, clearing anyway\n");
  95. }
  96. list_for_each_entry_safe(sa, tmp, &sa_manager->olist, olist) {
  97. drm_suballoc_remove_locked(sa);
  98. }
  99. sa_manager->size = 0;
  100. }
  101. EXPORT_SYMBOL(drm_suballoc_manager_fini);
  102. static void drm_suballoc_remove_locked(struct drm_suballoc *sa)
  103. {
  104. struct drm_suballoc_manager *sa_manager = sa->manager;
  105. if (sa_manager->hole == &sa->olist)
  106. sa_manager->hole = sa->olist.prev;
  107. list_del_init(&sa->olist);
  108. list_del_init(&sa->flist);
  109. dma_fence_put(sa->fence);
  110. kfree(sa);
  111. }
  112. static void drm_suballoc_try_free(struct drm_suballoc_manager *sa_manager)
  113. {
  114. struct drm_suballoc *sa, *tmp;
  115. if (sa_manager->hole->next == &sa_manager->olist)
  116. return;
  117. sa = list_entry(sa_manager->hole->next, struct drm_suballoc, olist);
  118. list_for_each_entry_safe_from(sa, tmp, &sa_manager->olist, olist) {
  119. if (!sa->fence || !dma_fence_is_signaled(sa->fence))
  120. return;
  121. drm_suballoc_remove_locked(sa);
  122. }
  123. }
  124. static size_t drm_suballoc_hole_soffset(struct drm_suballoc_manager *sa_manager)
  125. {
  126. struct list_head *hole = sa_manager->hole;
  127. if (hole != &sa_manager->olist)
  128. return list_entry(hole, struct drm_suballoc, olist)->eoffset;
  129. return 0;
  130. }
  131. static size_t drm_suballoc_hole_eoffset(struct drm_suballoc_manager *sa_manager)
  132. {
  133. struct list_head *hole = sa_manager->hole;
  134. if (hole->next != &sa_manager->olist)
  135. return list_entry(hole->next, struct drm_suballoc, olist)->soffset;
  136. return sa_manager->size;
  137. }
  138. static bool drm_suballoc_try_alloc(struct drm_suballoc_manager *sa_manager,
  139. struct drm_suballoc *sa,
  140. size_t size, size_t align)
  141. {
  142. size_t soffset, eoffset, wasted;
  143. soffset = drm_suballoc_hole_soffset(sa_manager);
  144. eoffset = drm_suballoc_hole_eoffset(sa_manager);
  145. wasted = round_up(soffset, align) - soffset;
  146. if ((eoffset - soffset) >= (size + wasted)) {
  147. soffset += wasted;
  148. sa->manager = sa_manager;
  149. sa->soffset = soffset;
  150. sa->eoffset = soffset + size;
  151. list_add(&sa->olist, sa_manager->hole);
  152. INIT_LIST_HEAD(&sa->flist);
  153. sa_manager->hole = &sa->olist;
  154. return true;
  155. }
  156. return false;
  157. }
  158. static bool __drm_suballoc_event(struct drm_suballoc_manager *sa_manager,
  159. size_t size, size_t align)
  160. {
  161. size_t soffset, eoffset, wasted;
  162. unsigned int i;
  163. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  164. if (!list_empty(&sa_manager->flist[i]))
  165. return true;
  166. soffset = drm_suballoc_hole_soffset(sa_manager);
  167. eoffset = drm_suballoc_hole_eoffset(sa_manager);
  168. wasted = round_up(soffset, align) - soffset;
  169. return ((eoffset - soffset) >= (size + wasted));
  170. }
  171. /**
  172. * drm_suballoc_event() - Check if we can stop waiting
  173. * @sa_manager: pointer to the sa_manager
  174. * @size: number of bytes we want to allocate
  175. * @align: alignment we need to match
  176. *
  177. * Return: true if either there is a fence we can wait for or
  178. * enough free memory to satisfy the allocation directly.
  179. * false otherwise.
  180. */
  181. static bool drm_suballoc_event(struct drm_suballoc_manager *sa_manager,
  182. size_t size, size_t align)
  183. {
  184. bool ret;
  185. spin_lock(&sa_manager->wq.lock);
  186. ret = __drm_suballoc_event(sa_manager, size, align);
  187. spin_unlock(&sa_manager->wq.lock);
  188. return ret;
  189. }
  190. static bool drm_suballoc_next_hole(struct drm_suballoc_manager *sa_manager,
  191. struct dma_fence **fences,
  192. unsigned int *tries)
  193. {
  194. struct drm_suballoc *best_bo = NULL;
  195. unsigned int i, best_idx;
  196. size_t soffset, best, tmp;
  197. /* if hole points to the end of the buffer */
  198. if (sa_manager->hole->next == &sa_manager->olist) {
  199. /* try again with its beginning */
  200. sa_manager->hole = &sa_manager->olist;
  201. return true;
  202. }
  203. soffset = drm_suballoc_hole_soffset(sa_manager);
  204. /* to handle wrap around we add sa_manager->size */
  205. best = sa_manager->size * 2;
  206. /* go over all fence list and try to find the closest sa
  207. * of the current last
  208. */
  209. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i) {
  210. struct drm_suballoc *sa;
  211. fences[i] = NULL;
  212. if (list_empty(&sa_manager->flist[i]))
  213. continue;
  214. sa = list_first_entry(&sa_manager->flist[i],
  215. struct drm_suballoc, flist);
  216. if (!dma_fence_is_signaled(sa->fence)) {
  217. fences[i] = sa->fence;
  218. continue;
  219. }
  220. /* limit the number of tries each freelist gets */
  221. if (tries[i] > 2)
  222. continue;
  223. tmp = sa->soffset;
  224. if (tmp < soffset) {
  225. /* wrap around, pretend it's after */
  226. tmp += sa_manager->size;
  227. }
  228. tmp -= soffset;
  229. if (tmp < best) {
  230. /* this sa bo is the closest one */
  231. best = tmp;
  232. best_idx = i;
  233. best_bo = sa;
  234. }
  235. }
  236. if (best_bo) {
  237. ++tries[best_idx];
  238. sa_manager->hole = best_bo->olist.prev;
  239. /*
  240. * We know that this one is signaled,
  241. * so it's safe to remove it.
  242. */
  243. drm_suballoc_remove_locked(best_bo);
  244. return true;
  245. }
  246. return false;
  247. }
  248. /**
  249. * drm_suballoc_new() - Make a suballocation.
  250. * @sa_manager: pointer to the sa_manager
  251. * @size: number of bytes we want to suballocate.
  252. * @gfp: gfp flags used for memory allocation. Typically GFP_KERNEL but
  253. * the argument is provided for suballocations from reclaim context or
  254. * where the caller wants to avoid pipelining rather than wait for
  255. * reclaim.
  256. * @intr: Whether to perform waits interruptible. This should typically
  257. * always be true, unless the caller needs to propagate a
  258. * non-interruptible context from above layers.
  259. * @align: Alignment. Must not exceed the default manager alignment.
  260. * If @align is zero, then the manager alignment is used.
  261. *
  262. * Try to make a suballocation of size @size, which will be rounded
  263. * up to the alignment specified in specified in drm_suballoc_manager_init().
  264. *
  265. * Return: a new suballocated bo, or an ERR_PTR.
  266. */
  267. struct drm_suballoc *
  268. drm_suballoc_new(struct drm_suballoc_manager *sa_manager, size_t size,
  269. gfp_t gfp, bool intr, size_t align)
  270. {
  271. struct dma_fence *fences[DRM_SUBALLOC_MAX_QUEUES];
  272. unsigned int tries[DRM_SUBALLOC_MAX_QUEUES];
  273. unsigned int count;
  274. int i, r;
  275. struct drm_suballoc *sa;
  276. if (WARN_ON_ONCE(align > sa_manager->align))
  277. return ERR_PTR(-EINVAL);
  278. if (WARN_ON_ONCE(size > sa_manager->size || !size))
  279. return ERR_PTR(-EINVAL);
  280. if (!align)
  281. align = sa_manager->align;
  282. sa = kmalloc(sizeof(*sa), gfp);
  283. if (!sa)
  284. return ERR_PTR(-ENOMEM);
  285. sa->manager = sa_manager;
  286. sa->fence = NULL;
  287. INIT_LIST_HEAD(&sa->olist);
  288. INIT_LIST_HEAD(&sa->flist);
  289. spin_lock(&sa_manager->wq.lock);
  290. do {
  291. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  292. tries[i] = 0;
  293. do {
  294. drm_suballoc_try_free(sa_manager);
  295. if (drm_suballoc_try_alloc(sa_manager, sa,
  296. size, align)) {
  297. spin_unlock(&sa_manager->wq.lock);
  298. return sa;
  299. }
  300. /* see if we can skip over some allocations */
  301. } while (drm_suballoc_next_hole(sa_manager, fences, tries));
  302. for (i = 0, count = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  303. if (fences[i])
  304. fences[count++] = dma_fence_get(fences[i]);
  305. if (count) {
  306. long t;
  307. spin_unlock(&sa_manager->wq.lock);
  308. t = dma_fence_wait_any_timeout(fences, count, intr,
  309. MAX_SCHEDULE_TIMEOUT,
  310. NULL);
  311. for (i = 0; i < count; ++i)
  312. dma_fence_put(fences[i]);
  313. r = (t > 0) ? 0 : t;
  314. spin_lock(&sa_manager->wq.lock);
  315. } else if (intr) {
  316. /* if we have nothing to wait for block */
  317. r = wait_event_interruptible_locked
  318. (sa_manager->wq,
  319. __drm_suballoc_event(sa_manager, size, align));
  320. } else {
  321. spin_unlock(&sa_manager->wq.lock);
  322. wait_event(sa_manager->wq,
  323. drm_suballoc_event(sa_manager, size, align));
  324. r = 0;
  325. spin_lock(&sa_manager->wq.lock);
  326. }
  327. } while (!r);
  328. spin_unlock(&sa_manager->wq.lock);
  329. kfree(sa);
  330. return ERR_PTR(r);
  331. }
  332. EXPORT_SYMBOL(drm_suballoc_new);
  333. /**
  334. * drm_suballoc_free - Free a suballocation
  335. * @suballoc: pointer to the suballocation
  336. * @fence: fence that signals when suballocation is idle
  337. *
  338. * Free the suballocation. The suballocation can be re-used after @fence signals.
  339. */
  340. void drm_suballoc_free(struct drm_suballoc *suballoc,
  341. struct dma_fence *fence)
  342. {
  343. struct drm_suballoc_manager *sa_manager;
  344. if (!suballoc)
  345. return;
  346. sa_manager = suballoc->manager;
  347. spin_lock(&sa_manager->wq.lock);
  348. if (fence && !dma_fence_is_signaled(fence)) {
  349. u32 idx;
  350. suballoc->fence = dma_fence_get(fence);
  351. idx = fence->context & (DRM_SUBALLOC_MAX_QUEUES - 1);
  352. list_add_tail(&suballoc->flist, &sa_manager->flist[idx]);
  353. } else {
  354. drm_suballoc_remove_locked(suballoc);
  355. }
  356. wake_up_all_locked(&sa_manager->wq);
  357. spin_unlock(&sa_manager->wq.lock);
  358. }
  359. EXPORT_SYMBOL(drm_suballoc_free);
  360. #ifdef CONFIG_DEBUG_FS
  361. void drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
  362. struct drm_printer *p,
  363. unsigned long long suballoc_base)
  364. {
  365. struct drm_suballoc *i;
  366. spin_lock(&sa_manager->wq.lock);
  367. list_for_each_entry(i, &sa_manager->olist, olist) {
  368. unsigned long long soffset = i->soffset;
  369. unsigned long long eoffset = i->eoffset;
  370. if (&i->olist == sa_manager->hole)
  371. drm_puts(p, ">");
  372. else
  373. drm_puts(p, " ");
  374. drm_printf(p, "[0x%010llx 0x%010llx] size %8lld",
  375. suballoc_base + soffset, suballoc_base + eoffset,
  376. eoffset - soffset);
  377. if (i->fence)
  378. drm_printf(p, " protected by 0x%016llx on context %llu",
  379. (unsigned long long)i->fence->seqno,
  380. (unsigned long long)i->fence->context);
  381. drm_puts(p, "\n");
  382. }
  383. spin_unlock(&sa_manager->wq.lock);
  384. }
  385. EXPORT_SYMBOL(drm_suballoc_dump_debug_info);
  386. #endif
  387. MODULE_AUTHOR("Multiple");
  388. MODULE_DESCRIPTION("Range suballocator helper");
  389. MODULE_LICENSE("Dual MIT/GPL");