radeon_sa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 domain)
  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->hole = &sa_manager->olist;
  58. INIT_LIST_HEAD(&sa_manager->olist);
  59. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  60. INIT_LIST_HEAD(&sa_manager->flist[i]);
  61. }
  62. r = radeon_bo_create(rdev, size, RADEON_GPU_PAGE_SIZE, true,
  63. RADEON_GEM_DOMAIN_CPU, NULL, &sa_manager->bo);
  64. if (r) {
  65. dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
  66. return r;
  67. }
  68. return r;
  69. }
  70. void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
  71. struct radeon_sa_manager *sa_manager)
  72. {
  73. struct radeon_sa_bo *sa_bo, *tmp;
  74. if (!list_empty(&sa_manager->olist)) {
  75. sa_manager->hole = &sa_manager->olist,
  76. radeon_sa_bo_try_free(sa_manager);
  77. if (!list_empty(&sa_manager->olist)) {
  78. dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
  79. }
  80. }
  81. list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
  82. radeon_sa_bo_remove_locked(sa_bo);
  83. }
  84. radeon_bo_unref(&sa_manager->bo);
  85. sa_manager->size = 0;
  86. }
  87. int radeon_sa_bo_manager_start(struct radeon_device *rdev,
  88. struct radeon_sa_manager *sa_manager)
  89. {
  90. int r;
  91. if (sa_manager->bo == NULL) {
  92. dev_err(rdev->dev, "no bo for sa manager\n");
  93. return -EINVAL;
  94. }
  95. /* map the buffer */
  96. r = radeon_bo_reserve(sa_manager->bo, false);
  97. if (r) {
  98. dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
  99. return r;
  100. }
  101. r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
  102. if (r) {
  103. radeon_bo_unreserve(sa_manager->bo);
  104. dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
  105. return r;
  106. }
  107. r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
  108. radeon_bo_unreserve(sa_manager->bo);
  109. return r;
  110. }
  111. int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
  112. struct radeon_sa_manager *sa_manager)
  113. {
  114. int r;
  115. if (sa_manager->bo == NULL) {
  116. dev_err(rdev->dev, "no bo for sa manager\n");
  117. return -EINVAL;
  118. }
  119. r = radeon_bo_reserve(sa_manager->bo, false);
  120. if (!r) {
  121. radeon_bo_kunmap(sa_manager->bo);
  122. radeon_bo_unpin(sa_manager->bo);
  123. radeon_bo_unreserve(sa_manager->bo);
  124. }
  125. return r;
  126. }
  127. static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo)
  128. {
  129. struct radeon_sa_manager *sa_manager = sa_bo->manager;
  130. if (sa_manager->hole == &sa_bo->olist) {
  131. sa_manager->hole = sa_bo->olist.prev;
  132. }
  133. list_del_init(&sa_bo->olist);
  134. list_del_init(&sa_bo->flist);
  135. radeon_fence_unref(&sa_bo->fence);
  136. kfree(sa_bo);
  137. }
  138. static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager)
  139. {
  140. struct radeon_sa_bo *sa_bo, *tmp;
  141. if (sa_manager->hole->next == &sa_manager->olist)
  142. return;
  143. sa_bo = list_entry(sa_manager->hole->next, struct radeon_sa_bo, olist);
  144. list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
  145. if (sa_bo->fence == NULL || !radeon_fence_signaled(sa_bo->fence)) {
  146. return;
  147. }
  148. radeon_sa_bo_remove_locked(sa_bo);
  149. }
  150. }
  151. static inline unsigned radeon_sa_bo_hole_soffset(struct radeon_sa_manager *sa_manager)
  152. {
  153. struct list_head *hole = sa_manager->hole;
  154. if (hole != &sa_manager->olist) {
  155. return list_entry(hole, struct radeon_sa_bo, olist)->eoffset;
  156. }
  157. return 0;
  158. }
  159. static inline unsigned radeon_sa_bo_hole_eoffset(struct radeon_sa_manager *sa_manager)
  160. {
  161. struct list_head *hole = sa_manager->hole;
  162. if (hole->next != &sa_manager->olist) {
  163. return list_entry(hole->next, struct radeon_sa_bo, olist)->soffset;
  164. }
  165. return sa_manager->size;
  166. }
  167. static bool radeon_sa_bo_try_alloc(struct radeon_sa_manager *sa_manager,
  168. struct radeon_sa_bo *sa_bo,
  169. unsigned size, unsigned align)
  170. {
  171. unsigned soffset, eoffset, wasted;
  172. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  173. eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
  174. wasted = (align - (soffset % align)) % align;
  175. if ((eoffset - soffset) >= (size + wasted)) {
  176. soffset += wasted;
  177. sa_bo->manager = sa_manager;
  178. sa_bo->soffset = soffset;
  179. sa_bo->eoffset = soffset + size;
  180. list_add(&sa_bo->olist, sa_manager->hole);
  181. INIT_LIST_HEAD(&sa_bo->flist);
  182. sa_manager->hole = &sa_bo->olist;
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * radeon_sa_event - Check if we can stop waiting
  189. *
  190. * @sa_manager: pointer to the sa_manager
  191. * @size: number of bytes we want to allocate
  192. * @align: alignment we need to match
  193. *
  194. * Check if either there is a fence we can wait for or
  195. * enough free memory to satisfy the allocation directly
  196. */
  197. static bool radeon_sa_event(struct radeon_sa_manager *sa_manager,
  198. unsigned size, unsigned align)
  199. {
  200. unsigned soffset, eoffset, wasted;
  201. int i;
  202. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  203. if (!list_empty(&sa_manager->flist[i])) {
  204. return true;
  205. }
  206. }
  207. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  208. eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
  209. wasted = (align - (soffset % align)) % align;
  210. if ((eoffset - soffset) >= (size + wasted)) {
  211. return true;
  212. }
  213. return false;
  214. }
  215. static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager,
  216. struct radeon_fence **fences,
  217. unsigned *tries)
  218. {
  219. struct radeon_sa_bo *best_bo = NULL;
  220. unsigned i, soffset, best, tmp;
  221. /* if hole points to the end of the buffer */
  222. if (sa_manager->hole->next == &sa_manager->olist) {
  223. /* try again with its beginning */
  224. sa_manager->hole = &sa_manager->olist;
  225. return true;
  226. }
  227. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  228. /* to handle wrap around we add sa_manager->size */
  229. best = sa_manager->size * 2;
  230. /* go over all fence list and try to find the closest sa_bo
  231. * of the current last
  232. */
  233. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  234. struct radeon_sa_bo *sa_bo;
  235. if (list_empty(&sa_manager->flist[i])) {
  236. continue;
  237. }
  238. sa_bo = list_first_entry(&sa_manager->flist[i],
  239. struct radeon_sa_bo, flist);
  240. if (!radeon_fence_signaled(sa_bo->fence)) {
  241. fences[i] = sa_bo->fence;
  242. continue;
  243. }
  244. /* limit the number of tries each ring gets */
  245. if (tries[i] > 2) {
  246. continue;
  247. }
  248. tmp = sa_bo->soffset;
  249. if (tmp < soffset) {
  250. /* wrap around, pretend it's after */
  251. tmp += sa_manager->size;
  252. }
  253. tmp -= soffset;
  254. if (tmp < best) {
  255. /* this sa bo is the closest one */
  256. best = tmp;
  257. best_bo = sa_bo;
  258. }
  259. }
  260. if (best_bo) {
  261. ++tries[best_bo->fence->ring];
  262. sa_manager->hole = best_bo->olist.prev;
  263. /* we knew that this one is signaled,
  264. so it's save to remote it */
  265. radeon_sa_bo_remove_locked(best_bo);
  266. return true;
  267. }
  268. return false;
  269. }
  270. int radeon_sa_bo_new(struct radeon_device *rdev,
  271. struct radeon_sa_manager *sa_manager,
  272. struct radeon_sa_bo **sa_bo,
  273. unsigned size, unsigned align, bool block)
  274. {
  275. struct radeon_fence *fences[RADEON_NUM_RINGS];
  276. unsigned tries[RADEON_NUM_RINGS];
  277. int i, r;
  278. BUG_ON(align > RADEON_GPU_PAGE_SIZE);
  279. BUG_ON(size > sa_manager->size);
  280. *sa_bo = kmalloc(sizeof(struct radeon_sa_bo), GFP_KERNEL);
  281. if ((*sa_bo) == NULL) {
  282. return -ENOMEM;
  283. }
  284. (*sa_bo)->manager = sa_manager;
  285. (*sa_bo)->fence = NULL;
  286. INIT_LIST_HEAD(&(*sa_bo)->olist);
  287. INIT_LIST_HEAD(&(*sa_bo)->flist);
  288. spin_lock(&sa_manager->wq.lock);
  289. do {
  290. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  291. fences[i] = NULL;
  292. tries[i] = 0;
  293. }
  294. do {
  295. radeon_sa_bo_try_free(sa_manager);
  296. if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo,
  297. size, align)) {
  298. spin_unlock(&sa_manager->wq.lock);
  299. return 0;
  300. }
  301. /* see if we can skip over some allocations */
  302. } while (radeon_sa_bo_next_hole(sa_manager, fences, tries));
  303. spin_unlock(&sa_manager->wq.lock);
  304. r = radeon_fence_wait_any(rdev, fences, false);
  305. spin_lock(&sa_manager->wq.lock);
  306. /* if we have nothing to wait for block */
  307. if (r == -ENOENT && block) {
  308. r = wait_event_interruptible_locked(
  309. sa_manager->wq,
  310. radeon_sa_event(sa_manager, size, align)
  311. );
  312. } else if (r == -ENOENT) {
  313. r = -ENOMEM;
  314. }
  315. } while (!r);
  316. spin_unlock(&sa_manager->wq.lock);
  317. kfree(*sa_bo);
  318. *sa_bo = NULL;
  319. return r;
  320. }
  321. void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo,
  322. struct radeon_fence *fence)
  323. {
  324. struct radeon_sa_manager *sa_manager;
  325. if (sa_bo == NULL || *sa_bo == NULL) {
  326. return;
  327. }
  328. sa_manager = (*sa_bo)->manager;
  329. spin_lock(&sa_manager->wq.lock);
  330. if (fence && !radeon_fence_signaled(fence)) {
  331. (*sa_bo)->fence = radeon_fence_ref(fence);
  332. list_add_tail(&(*sa_bo)->flist,
  333. &sa_manager->flist[fence->ring]);
  334. } else {
  335. radeon_sa_bo_remove_locked(*sa_bo);
  336. }
  337. wake_up_all_locked(&sa_manager->wq);
  338. spin_unlock(&sa_manager->wq.lock);
  339. *sa_bo = NULL;
  340. }
  341. #if defined(CONFIG_DEBUG_FS)
  342. void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
  343. struct seq_file *m)
  344. {
  345. struct radeon_sa_bo *i;
  346. spin_lock(&sa_manager->wq.lock);
  347. list_for_each_entry(i, &sa_manager->olist, olist) {
  348. if (&i->olist == sa_manager->hole) {
  349. seq_printf(m, ">");
  350. } else {
  351. seq_printf(m, " ");
  352. }
  353. seq_printf(m, "[0x%08x 0x%08x] size %8d",
  354. i->soffset, i->eoffset, i->eoffset - i->soffset);
  355. if (i->fence) {
  356. seq_printf(m, " protected by 0x%016llx on ring %d",
  357. i->fence->seq, i->fence->ring);
  358. }
  359. seq_printf(m, "\n");
  360. }
  361. spin_unlock(&sa_manager->wq.lock);
  362. }
  363. #endif