drm_suballoc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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/export.h>
  45. #include <linux/slab.h>
  46. #include <linux/sched.h>
  47. #include <linux/wait.h>
  48. #include <linux/dma-fence.h>
  49. static void drm_suballoc_remove_locked(struct drm_suballoc *sa);
  50. static void drm_suballoc_try_free(struct drm_suballoc_manager *sa_manager);
  51. /**
  52. * drm_suballoc_manager_init() - Initialise the drm_suballoc_manager
  53. * @sa_manager: pointer to the sa_manager
  54. * @size: number of bytes we want to suballocate
  55. * @align: alignment for each suballocated chunk
  56. *
  57. * Prepares the suballocation manager for suballocations.
  58. */
  59. void drm_suballoc_manager_init(struct drm_suballoc_manager *sa_manager,
  60. size_t size, size_t align)
  61. {
  62. unsigned int i;
  63. BUILD_BUG_ON(!is_power_of_2(DRM_SUBALLOC_MAX_QUEUES));
  64. if (!align)
  65. align = 1;
  66. /* alignment must be a power of 2 */
  67. if (WARN_ON_ONCE(align & (align - 1)))
  68. align = roundup_pow_of_two(align);
  69. init_waitqueue_head(&sa_manager->wq);
  70. sa_manager->size = size;
  71. sa_manager->align = align;
  72. sa_manager->hole = &sa_manager->olist;
  73. INIT_LIST_HEAD(&sa_manager->olist);
  74. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  75. INIT_LIST_HEAD(&sa_manager->flist[i]);
  76. }
  77. EXPORT_SYMBOL(drm_suballoc_manager_init);
  78. /**
  79. * drm_suballoc_manager_fini() - Destroy the drm_suballoc_manager
  80. * @sa_manager: pointer to the sa_manager
  81. *
  82. * Cleans up the suballocation manager after use. All fences added
  83. * with drm_suballoc_free() must be signaled, or we cannot clean up
  84. * the entire manager.
  85. */
  86. void drm_suballoc_manager_fini(struct drm_suballoc_manager *sa_manager)
  87. {
  88. struct drm_suballoc *sa, *tmp;
  89. if (!sa_manager->size)
  90. return;
  91. if (!list_empty(&sa_manager->olist)) {
  92. sa_manager->hole = &sa_manager->olist;
  93. drm_suballoc_try_free(sa_manager);
  94. if (!list_empty(&sa_manager->olist))
  95. DRM_ERROR("sa_manager is not empty, clearing anyway\n");
  96. }
  97. list_for_each_entry_safe(sa, tmp, &sa_manager->olist, olist) {
  98. drm_suballoc_remove_locked(sa);
  99. }
  100. sa_manager->size = 0;
  101. }
  102. EXPORT_SYMBOL(drm_suballoc_manager_fini);
  103. static void drm_suballoc_remove_locked(struct drm_suballoc *sa)
  104. {
  105. struct drm_suballoc_manager *sa_manager = sa->manager;
  106. if (sa_manager->hole == &sa->olist)
  107. sa_manager->hole = sa->olist.prev;
  108. list_del_init(&sa->olist);
  109. list_del_init(&sa->flist);
  110. dma_fence_put(sa->fence);
  111. kfree(sa);
  112. }
  113. static void drm_suballoc_try_free(struct drm_suballoc_manager *sa_manager)
  114. {
  115. struct drm_suballoc *sa, *tmp;
  116. if (sa_manager->hole->next == &sa_manager->olist)
  117. return;
  118. sa = list_entry(sa_manager->hole->next, struct drm_suballoc, olist);
  119. list_for_each_entry_safe_from(sa, tmp, &sa_manager->olist, olist) {
  120. if (!sa->fence || !dma_fence_is_signaled(sa->fence))
  121. return;
  122. drm_suballoc_remove_locked(sa);
  123. }
  124. }
  125. static size_t drm_suballoc_hole_soffset(struct drm_suballoc_manager *sa_manager)
  126. {
  127. struct list_head *hole = sa_manager->hole;
  128. if (hole != &sa_manager->olist)
  129. return list_entry(hole, struct drm_suballoc, olist)->eoffset;
  130. return 0;
  131. }
  132. static size_t drm_suballoc_hole_eoffset(struct drm_suballoc_manager *sa_manager)
  133. {
  134. struct list_head *hole = sa_manager->hole;
  135. if (hole->next != &sa_manager->olist)
  136. return list_entry(hole->next, struct drm_suballoc, olist)->soffset;
  137. return sa_manager->size;
  138. }
  139. static bool drm_suballoc_try_alloc(struct drm_suballoc_manager *sa_manager,
  140. struct drm_suballoc *sa,
  141. size_t size, size_t align)
  142. {
  143. size_t soffset, eoffset, wasted;
  144. soffset = drm_suballoc_hole_soffset(sa_manager);
  145. eoffset = drm_suballoc_hole_eoffset(sa_manager);
  146. wasted = round_up(soffset, align) - soffset;
  147. if ((eoffset - soffset) >= (size + wasted)) {
  148. soffset += wasted;
  149. sa->manager = sa_manager;
  150. sa->soffset = soffset;
  151. sa->eoffset = soffset + size;
  152. list_add(&sa->olist, sa_manager->hole);
  153. INIT_LIST_HEAD(&sa->flist);
  154. sa_manager->hole = &sa->olist;
  155. return true;
  156. }
  157. return false;
  158. }
  159. static bool __drm_suballoc_event(struct drm_suballoc_manager *sa_manager,
  160. size_t size, size_t align)
  161. {
  162. size_t soffset, eoffset, wasted;
  163. unsigned int i;
  164. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  165. if (!list_empty(&sa_manager->flist[i]))
  166. return true;
  167. soffset = drm_suballoc_hole_soffset(sa_manager);
  168. eoffset = drm_suballoc_hole_eoffset(sa_manager);
  169. wasted = round_up(soffset, align) - soffset;
  170. return ((eoffset - soffset) >= (size + wasted));
  171. }
  172. /**
  173. * drm_suballoc_event() - Check if we can stop waiting
  174. * @sa_manager: pointer to the sa_manager
  175. * @size: number of bytes we want to allocate
  176. * @align: alignment we need to match
  177. *
  178. * Return: true if either there is a fence we can wait for or
  179. * enough free memory to satisfy the allocation directly.
  180. * false otherwise.
  181. */
  182. static bool drm_suballoc_event(struct drm_suballoc_manager *sa_manager,
  183. size_t size, size_t align)
  184. {
  185. bool ret;
  186. spin_lock(&sa_manager->wq.lock);
  187. ret = __drm_suballoc_event(sa_manager, size, align);
  188. spin_unlock(&sa_manager->wq.lock);
  189. return ret;
  190. }
  191. static bool drm_suballoc_next_hole(struct drm_suballoc_manager *sa_manager,
  192. struct dma_fence **fences,
  193. unsigned int *tries)
  194. {
  195. struct drm_suballoc *best_bo = NULL;
  196. unsigned int i, best_idx;
  197. size_t soffset, best, tmp;
  198. /* if hole points to the end of the buffer */
  199. if (sa_manager->hole->next == &sa_manager->olist) {
  200. /* try again with its beginning */
  201. sa_manager->hole = &sa_manager->olist;
  202. return true;
  203. }
  204. soffset = drm_suballoc_hole_soffset(sa_manager);
  205. /* to handle wrap around we add sa_manager->size */
  206. best = sa_manager->size * 2;
  207. /* go over all fence list and try to find the closest sa
  208. * of the current last
  209. */
  210. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i) {
  211. struct drm_suballoc *sa;
  212. fences[i] = NULL;
  213. if (list_empty(&sa_manager->flist[i]))
  214. continue;
  215. sa = list_first_entry(&sa_manager->flist[i],
  216. struct drm_suballoc, flist);
  217. if (!dma_fence_is_signaled(sa->fence)) {
  218. fences[i] = sa->fence;
  219. continue;
  220. }
  221. /* limit the number of tries each freelist gets */
  222. if (tries[i] > 2)
  223. continue;
  224. tmp = sa->soffset;
  225. if (tmp < soffset) {
  226. /* wrap around, pretend it's after */
  227. tmp += sa_manager->size;
  228. }
  229. tmp -= soffset;
  230. if (tmp < best) {
  231. /* this sa bo is the closest one */
  232. best = tmp;
  233. best_idx = i;
  234. best_bo = sa;
  235. }
  236. }
  237. if (best_bo) {
  238. ++tries[best_idx];
  239. sa_manager->hole = best_bo->olist.prev;
  240. /*
  241. * We know that this one is signaled,
  242. * so it's safe to remove it.
  243. */
  244. drm_suballoc_remove_locked(best_bo);
  245. return true;
  246. }
  247. return false;
  248. }
  249. /**
  250. * drm_suballoc_new() - Make a suballocation.
  251. * @sa_manager: pointer to the sa_manager
  252. * @size: number of bytes we want to suballocate.
  253. * @gfp: gfp flags used for memory allocation. Typically GFP_KERNEL but
  254. * the argument is provided for suballocations from reclaim context or
  255. * where the caller wants to avoid pipelining rather than wait for
  256. * reclaim.
  257. * @intr: Whether to perform waits interruptible. This should typically
  258. * always be true, unless the caller needs to propagate a
  259. * non-interruptible context from above layers.
  260. * @align: Alignment. Must not exceed the default manager alignment.
  261. * If @align is zero, then the manager alignment is used.
  262. *
  263. * Try to make a suballocation of size @size, which will be rounded
  264. * up to the alignment specified in specified in drm_suballoc_manager_init().
  265. *
  266. * Return: a new suballocated bo, or an ERR_PTR.
  267. */
  268. struct drm_suballoc *
  269. drm_suballoc_new(struct drm_suballoc_manager *sa_manager, size_t size,
  270. gfp_t gfp, bool intr, size_t align)
  271. {
  272. struct dma_fence *fences[DRM_SUBALLOC_MAX_QUEUES];
  273. unsigned int tries[DRM_SUBALLOC_MAX_QUEUES];
  274. unsigned int count;
  275. int i, r;
  276. struct drm_suballoc *sa;
  277. if (WARN_ON_ONCE(align > sa_manager->align))
  278. return ERR_PTR(-EINVAL);
  279. if (WARN_ON_ONCE(size > sa_manager->size || !size))
  280. return ERR_PTR(-EINVAL);
  281. if (!align)
  282. align = sa_manager->align;
  283. sa = kmalloc_obj(*sa, gfp);
  284. if (!sa)
  285. return ERR_PTR(-ENOMEM);
  286. sa->manager = sa_manager;
  287. sa->fence = NULL;
  288. INIT_LIST_HEAD(&sa->olist);
  289. INIT_LIST_HEAD(&sa->flist);
  290. spin_lock(&sa_manager->wq.lock);
  291. do {
  292. for (i = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  293. tries[i] = 0;
  294. do {
  295. drm_suballoc_try_free(sa_manager);
  296. if (drm_suballoc_try_alloc(sa_manager, sa,
  297. size, align)) {
  298. spin_unlock(&sa_manager->wq.lock);
  299. return sa;
  300. }
  301. /* see if we can skip over some allocations */
  302. } while (drm_suballoc_next_hole(sa_manager, fences, tries));
  303. for (i = 0, count = 0; i < DRM_SUBALLOC_MAX_QUEUES; ++i)
  304. if (fences[i])
  305. fences[count++] = dma_fence_get(fences[i]);
  306. if (count) {
  307. long t;
  308. spin_unlock(&sa_manager->wq.lock);
  309. t = dma_fence_wait_any_timeout(fences, count, intr,
  310. MAX_SCHEDULE_TIMEOUT,
  311. NULL);
  312. for (i = 0; i < count; ++i)
  313. dma_fence_put(fences[i]);
  314. r = (t > 0) ? 0 : t;
  315. spin_lock(&sa_manager->wq.lock);
  316. } else if (intr) {
  317. /* if we have nothing to wait for block */
  318. r = wait_event_interruptible_locked
  319. (sa_manager->wq,
  320. __drm_suballoc_event(sa_manager, size, align));
  321. } else {
  322. spin_unlock(&sa_manager->wq.lock);
  323. wait_event(sa_manager->wq,
  324. drm_suballoc_event(sa_manager, size, align));
  325. r = 0;
  326. spin_lock(&sa_manager->wq.lock);
  327. }
  328. } while (!r);
  329. spin_unlock(&sa_manager->wq.lock);
  330. kfree(sa);
  331. return ERR_PTR(r);
  332. }
  333. EXPORT_SYMBOL(drm_suballoc_new);
  334. /**
  335. * drm_suballoc_free - Free a suballocation
  336. * @suballoc: pointer to the suballocation
  337. * @fence: fence that signals when suballocation is idle
  338. *
  339. * Free the suballocation. The suballocation can be re-used after @fence signals.
  340. */
  341. void drm_suballoc_free(struct drm_suballoc *suballoc,
  342. struct dma_fence *fence)
  343. {
  344. struct drm_suballoc_manager *sa_manager;
  345. if (!suballoc)
  346. return;
  347. sa_manager = suballoc->manager;
  348. spin_lock(&sa_manager->wq.lock);
  349. if (fence && !dma_fence_is_signaled(fence)) {
  350. u32 idx;
  351. suballoc->fence = dma_fence_get(fence);
  352. idx = fence->context & (DRM_SUBALLOC_MAX_QUEUES - 1);
  353. list_add_tail(&suballoc->flist, &sa_manager->flist[idx]);
  354. } else {
  355. drm_suballoc_remove_locked(suballoc);
  356. }
  357. wake_up_all_locked(&sa_manager->wq);
  358. spin_unlock(&sa_manager->wq.lock);
  359. }
  360. EXPORT_SYMBOL(drm_suballoc_free);
  361. #ifdef CONFIG_DEBUG_FS
  362. void drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
  363. struct drm_printer *p,
  364. unsigned long long suballoc_base)
  365. {
  366. struct drm_suballoc *i;
  367. spin_lock(&sa_manager->wq.lock);
  368. list_for_each_entry(i, &sa_manager->olist, olist) {
  369. unsigned long long soffset = i->soffset;
  370. unsigned long long eoffset = i->eoffset;
  371. if (&i->olist == sa_manager->hole)
  372. drm_puts(p, ">");
  373. else
  374. drm_puts(p, " ");
  375. drm_printf(p, "[0x%010llx 0x%010llx] size %8lld",
  376. suballoc_base + soffset, suballoc_base + eoffset,
  377. eoffset - soffset);
  378. if (i->fence)
  379. drm_printf(p, " protected by 0x%016llx on context %llu",
  380. (unsigned long long)i->fence->seqno,
  381. (unsigned long long)i->fence->context);
  382. drm_puts(p, "\n");
  383. }
  384. spin_unlock(&sa_manager->wq.lock);
  385. }
  386. EXPORT_SYMBOL(drm_suballoc_dump_debug_info);
  387. #endif
  388. MODULE_AUTHOR("Multiple");
  389. MODULE_DESCRIPTION("Range suballocator helper");
  390. MODULE_LICENSE("Dual MIT/GPL");