radeon_sa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. */
  30. /* Algorithm:
  31. *
  32. * We store the last allocated bo in "hole", we always try to allocate
  33. * after the last allocated bo. Principle is that in a linear GPU ring
  34. * progression was is after last is the oldest bo we allocated and thus
  35. * the first one that should no longer be in use by the GPU.
  36. *
  37. * If it's not the case we skip over the bo after last to the closest
  38. * done bo if such one exist. If none exist and we are not asked to
  39. * block we report failure to allocate.
  40. *
  41. * If we are asked to block we wait on all the oldest fence of all
  42. * rings. We just wait for any of those fence to complete.
  43. */
  44. #include <drm/drmP.h>
  45. #include "radeon.h"
  46. static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo);
  47. static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager);
  48. int radeon_sa_bo_manager_init(struct radeon_device *rdev,
  49. struct radeon_sa_manager *sa_manager,
  50. unsigned size, u32 align, u32 domain, u32 flags)
  51. {
  52. int i, r;
  53. init_waitqueue_head(&sa_manager->wq);
  54. sa_manager->bo = NULL;
  55. sa_manager->size = size;
  56. sa_manager->domain = domain;
  57. sa_manager->align = align;
  58. sa_manager->hole = &sa_manager->olist;
  59. INIT_LIST_HEAD(&sa_manager->olist);
  60. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  61. INIT_LIST_HEAD(&sa_manager->flist[i]);
  62. }
  63. r = radeon_bo_create(rdev, size, align, true,
  64. domain, flags, NULL, NULL, &sa_manager->bo);
  65. if (r) {
  66. dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
  67. return r;
  68. }
  69. return r;
  70. }
  71. void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
  72. struct radeon_sa_manager *sa_manager)
  73. {
  74. struct radeon_sa_bo *sa_bo, *tmp;
  75. if (!list_empty(&sa_manager->olist)) {
  76. sa_manager->hole = &sa_manager->olist,
  77. radeon_sa_bo_try_free(sa_manager);
  78. if (!list_empty(&sa_manager->olist)) {
  79. dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
  80. }
  81. }
  82. list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
  83. radeon_sa_bo_remove_locked(sa_bo);
  84. }
  85. radeon_bo_unref(&sa_manager->bo);
  86. sa_manager->size = 0;
  87. }
  88. int radeon_sa_bo_manager_start(struct radeon_device *rdev,
  89. struct radeon_sa_manager *sa_manager)
  90. {
  91. int r;
  92. if (sa_manager->bo == NULL) {
  93. dev_err(rdev->dev, "no bo for sa manager\n");
  94. return -EINVAL;
  95. }
  96. /* map the buffer */
  97. r = radeon_bo_reserve(sa_manager->bo, false);
  98. if (r) {
  99. dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
  100. return r;
  101. }
  102. r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
  103. if (r) {
  104. radeon_bo_unreserve(sa_manager->bo);
  105. dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
  106. return r;
  107. }
  108. r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
  109. radeon_bo_unreserve(sa_manager->bo);
  110. return r;
  111. }
  112. int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
  113. struct radeon_sa_manager *sa_manager)
  114. {
  115. int r;
  116. if (sa_manager->bo == NULL) {
  117. dev_err(rdev->dev, "no bo for sa manager\n");
  118. return -EINVAL;
  119. }
  120. r = radeon_bo_reserve(sa_manager->bo, false);
  121. if (!r) {
  122. radeon_bo_kunmap(sa_manager->bo);
  123. radeon_bo_unpin(sa_manager->bo);
  124. radeon_bo_unreserve(sa_manager->bo);
  125. }
  126. return r;
  127. }
  128. static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo)
  129. {
  130. struct radeon_sa_manager *sa_manager = sa_bo->manager;
  131. if (sa_manager->hole == &sa_bo->olist) {
  132. sa_manager->hole = sa_bo->olist.prev;
  133. }
  134. list_del_init(&sa_bo->olist);
  135. list_del_init(&sa_bo->flist);
  136. radeon_fence_unref(&sa_bo->fence);
  137. kfree(sa_bo);
  138. }
  139. static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager)
  140. {
  141. struct radeon_sa_bo *sa_bo, *tmp;
  142. if (sa_manager->hole->next == &sa_manager->olist)
  143. return;
  144. sa_bo = list_entry(sa_manager->hole->next, struct radeon_sa_bo, olist);
  145. list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
  146. if (sa_bo->fence == NULL || !radeon_fence_signaled(sa_bo->fence)) {
  147. return;
  148. }
  149. radeon_sa_bo_remove_locked(sa_bo);
  150. }
  151. }
  152. static inline unsigned radeon_sa_bo_hole_soffset(struct radeon_sa_manager *sa_manager)
  153. {
  154. struct list_head *hole = sa_manager->hole;
  155. if (hole != &sa_manager->olist) {
  156. return list_entry(hole, struct radeon_sa_bo, olist)->eoffset;
  157. }
  158. return 0;
  159. }
  160. static inline unsigned radeon_sa_bo_hole_eoffset(struct radeon_sa_manager *sa_manager)
  161. {
  162. struct list_head *hole = sa_manager->hole;
  163. if (hole->next != &sa_manager->olist) {
  164. return list_entry(hole->next, struct radeon_sa_bo, olist)->soffset;
  165. }
  166. return sa_manager->size;
  167. }
  168. static bool radeon_sa_bo_try_alloc(struct radeon_sa_manager *sa_manager,
  169. struct radeon_sa_bo *sa_bo,
  170. unsigned size, unsigned align)
  171. {
  172. unsigned soffset, eoffset, wasted;
  173. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  174. eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
  175. wasted = (align - (soffset % align)) % align;
  176. if ((eoffset - soffset) >= (size + wasted)) {
  177. soffset += wasted;
  178. sa_bo->manager = sa_manager;
  179. sa_bo->soffset = soffset;
  180. sa_bo->eoffset = soffset + size;
  181. list_add(&sa_bo->olist, sa_manager->hole);
  182. INIT_LIST_HEAD(&sa_bo->flist);
  183. sa_manager->hole = &sa_bo->olist;
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * radeon_sa_event - Check if we can stop waiting
  190. *
  191. * @sa_manager: pointer to the sa_manager
  192. * @size: number of bytes we want to allocate
  193. * @align: alignment we need to match
  194. *
  195. * Check if either there is a fence we can wait for or
  196. * enough free memory to satisfy the allocation directly
  197. */
  198. static bool radeon_sa_event(struct radeon_sa_manager *sa_manager,
  199. unsigned size, unsigned align)
  200. {
  201. unsigned soffset, eoffset, wasted;
  202. int i;
  203. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  204. if (!list_empty(&sa_manager->flist[i])) {
  205. return true;
  206. }
  207. }
  208. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  209. eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
  210. wasted = (align - (soffset % align)) % align;
  211. if ((eoffset - soffset) >= (size + wasted)) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager,
  217. struct radeon_fence **fences,
  218. unsigned *tries)
  219. {
  220. struct radeon_sa_bo *best_bo = NULL;
  221. unsigned i, soffset, best, tmp;
  222. /* if hole points to the end of the buffer */
  223. if (sa_manager->hole->next == &sa_manager->olist) {
  224. /* try again with its beginning */
  225. sa_manager->hole = &sa_manager->olist;
  226. return true;
  227. }
  228. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  229. /* to handle wrap around we add sa_manager->size */
  230. best = sa_manager->size * 2;
  231. /* go over all fence list and try to find the closest sa_bo
  232. * of the current last
  233. */
  234. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  235. struct radeon_sa_bo *sa_bo;
  236. if (list_empty(&sa_manager->flist[i])) {
  237. continue;
  238. }
  239. sa_bo = list_first_entry(&sa_manager->flist[i],
  240. struct radeon_sa_bo, flist);
  241. if (!radeon_fence_signaled(sa_bo->fence)) {
  242. fences[i] = sa_bo->fence;
  243. continue;
  244. }
  245. /* limit the number of tries each ring gets */
  246. if (tries[i] > 2) {
  247. continue;
  248. }
  249. tmp = sa_bo->soffset;
  250. if (tmp < soffset) {
  251. /* wrap around, pretend it's after */
  252. tmp += sa_manager->size;
  253. }
  254. tmp -= soffset;
  255. if (tmp < best) {
  256. /* this sa bo is the closest one */
  257. best = tmp;
  258. best_bo = sa_bo;
  259. }
  260. }
  261. if (best_bo) {
  262. ++tries[best_bo->fence->ring];
  263. sa_manager->hole = best_bo->olist.prev;
  264. /* we knew that this one is signaled,
  265. so it's save to remote it */
  266. radeon_sa_bo_remove_locked(best_bo);
  267. return true;
  268. }
  269. return false;
  270. }
  271. int radeon_sa_bo_new(struct radeon_device *rdev,
  272. struct radeon_sa_manager *sa_manager,
  273. struct radeon_sa_bo **sa_bo,
  274. unsigned size, unsigned align)
  275. {
  276. struct radeon_fence *fences[RADEON_NUM_RINGS];
  277. unsigned tries[RADEON_NUM_RINGS];
  278. int i, r;
  279. BUG_ON(align > sa_manager->align);
  280. BUG_ON(size > sa_manager->size);
  281. *sa_bo = kmalloc(sizeof(struct radeon_sa_bo), GFP_KERNEL);
  282. if ((*sa_bo) == NULL) {
  283. return -ENOMEM;
  284. }
  285. (*sa_bo)->manager = sa_manager;
  286. (*sa_bo)->fence = NULL;
  287. INIT_LIST_HEAD(&(*sa_bo)->olist);
  288. INIT_LIST_HEAD(&(*sa_bo)->flist);
  289. spin_lock(&sa_manager->wq.lock);
  290. do {
  291. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  292. fences[i] = NULL;
  293. tries[i] = 0;
  294. }
  295. do {
  296. radeon_sa_bo_try_free(sa_manager);
  297. if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo,
  298. size, align)) {
  299. spin_unlock(&sa_manager->wq.lock);
  300. return 0;
  301. }
  302. /* see if we can skip over some allocations */
  303. } while (radeon_sa_bo_next_hole(sa_manager, fences, tries));
  304. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  305. radeon_fence_ref(fences[i]);
  306. spin_unlock(&sa_manager->wq.lock);
  307. r = radeon_fence_wait_any(rdev, fences, false);
  308. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  309. radeon_fence_unref(&fences[i]);
  310. spin_lock(&sa_manager->wq.lock);
  311. /* if we have nothing to wait for block */
  312. if (r == -ENOENT) {
  313. r = wait_event_interruptible_locked(
  314. sa_manager->wq,
  315. radeon_sa_event(sa_manager, size, align)
  316. );
  317. }
  318. } while (!r);
  319. spin_unlock(&sa_manager->wq.lock);
  320. kfree(*sa_bo);
  321. *sa_bo = NULL;
  322. return r;
  323. }
  324. void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo,
  325. struct radeon_fence *fence)
  326. {
  327. struct radeon_sa_manager *sa_manager;
  328. if (sa_bo == NULL || *sa_bo == NULL) {
  329. return;
  330. }
  331. sa_manager = (*sa_bo)->manager;
  332. spin_lock(&sa_manager->wq.lock);
  333. if (fence && !radeon_fence_signaled(fence)) {
  334. (*sa_bo)->fence = radeon_fence_ref(fence);
  335. list_add_tail(&(*sa_bo)->flist,
  336. &sa_manager->flist[fence->ring]);
  337. } else {
  338. radeon_sa_bo_remove_locked(*sa_bo);
  339. }
  340. wake_up_all_locked(&sa_manager->wq);
  341. spin_unlock(&sa_manager->wq.lock);
  342. *sa_bo = NULL;
  343. }
  344. #if defined(CONFIG_DEBUG_FS)
  345. void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
  346. struct seq_file *m)
  347. {
  348. struct radeon_sa_bo *i;
  349. spin_lock(&sa_manager->wq.lock);
  350. list_for_each_entry(i, &sa_manager->olist, olist) {
  351. uint64_t soffset = i->soffset + sa_manager->gpu_addr;
  352. uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
  353. if (&i->olist == sa_manager->hole) {
  354. seq_printf(m, ">");
  355. } else {
  356. seq_printf(m, " ");
  357. }
  358. seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
  359. soffset, eoffset, eoffset - soffset);
  360. if (i->fence) {
  361. seq_printf(m, " protected by 0x%016llx on ring %d",
  362. i->fence->seq, i->fence->ring);
  363. }
  364. seq_printf(m, "\n");
  365. }
  366. spin_unlock(&sa_manager->wq.lock);
  367. }
  368. #endif