radeon_sa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "drmP.h"
  45. #include "drm.h"
  46. #include "radeon.h"
  47. static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo);
  48. static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager);
  49. int radeon_sa_bo_manager_init(struct radeon_device *rdev,
  50. struct radeon_sa_manager *sa_manager,
  51. unsigned size, u32 domain)
  52. {
  53. int i, r;
  54. spin_lock_init(&sa_manager->lock);
  55. sa_manager->bo = NULL;
  56. sa_manager->size = size;
  57. sa_manager->domain = domain;
  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, RADEON_GPU_PAGE_SIZE, true,
  64. RADEON_GEM_DOMAIN_CPU, 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. static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager,
  189. struct radeon_fence **fences,
  190. unsigned *tries)
  191. {
  192. struct radeon_sa_bo *best_bo = NULL;
  193. unsigned i, soffset, best, tmp;
  194. /* if hole points to the end of the buffer */
  195. if (sa_manager->hole->next == &sa_manager->olist) {
  196. /* try again with its beginning */
  197. sa_manager->hole = &sa_manager->olist;
  198. return true;
  199. }
  200. soffset = radeon_sa_bo_hole_soffset(sa_manager);
  201. /* to handle wrap around we add sa_manager->size */
  202. best = sa_manager->size * 2;
  203. /* go over all fence list and try to find the closest sa_bo
  204. * of the current last
  205. */
  206. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  207. struct radeon_sa_bo *sa_bo;
  208. if (list_empty(&sa_manager->flist[i])) {
  209. continue;
  210. }
  211. sa_bo = list_first_entry(&sa_manager->flist[i],
  212. struct radeon_sa_bo, flist);
  213. if (!radeon_fence_signaled(sa_bo->fence)) {
  214. fences[i] = sa_bo->fence;
  215. continue;
  216. }
  217. /* limit the number of tries each ring gets */
  218. if (tries[i] > 2) {
  219. continue;
  220. }
  221. tmp = sa_bo->soffset;
  222. if (tmp < soffset) {
  223. /* wrap around, pretend it's after */
  224. tmp += sa_manager->size;
  225. }
  226. tmp -= soffset;
  227. if (tmp < best) {
  228. /* this sa bo is the closest one */
  229. best = tmp;
  230. best_bo = sa_bo;
  231. }
  232. }
  233. if (best_bo) {
  234. ++tries[best_bo->fence->ring];
  235. sa_manager->hole = best_bo->olist.prev;
  236. /* we knew that this one is signaled,
  237. so it's save to remote it */
  238. radeon_sa_bo_remove_locked(best_bo);
  239. return true;
  240. }
  241. return false;
  242. }
  243. int radeon_sa_bo_new(struct radeon_device *rdev,
  244. struct radeon_sa_manager *sa_manager,
  245. struct radeon_sa_bo **sa_bo,
  246. unsigned size, unsigned align, bool block)
  247. {
  248. struct radeon_fence *fences[RADEON_NUM_RINGS];
  249. unsigned tries[RADEON_NUM_RINGS];
  250. int i, r = -ENOMEM;
  251. BUG_ON(align > RADEON_GPU_PAGE_SIZE);
  252. BUG_ON(size > sa_manager->size);
  253. *sa_bo = kmalloc(sizeof(struct radeon_sa_bo), GFP_KERNEL);
  254. if ((*sa_bo) == NULL) {
  255. return -ENOMEM;
  256. }
  257. (*sa_bo)->manager = sa_manager;
  258. (*sa_bo)->fence = NULL;
  259. INIT_LIST_HEAD(&(*sa_bo)->olist);
  260. INIT_LIST_HEAD(&(*sa_bo)->flist);
  261. spin_lock(&sa_manager->lock);
  262. do {
  263. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  264. fences[i] = NULL;
  265. tries[i] = 0;
  266. }
  267. do {
  268. radeon_sa_bo_try_free(sa_manager);
  269. if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo,
  270. size, align)) {
  271. spin_unlock(&sa_manager->lock);
  272. return 0;
  273. }
  274. /* see if we can skip over some allocations */
  275. } while (radeon_sa_bo_next_hole(sa_manager, fences, tries));
  276. if (block) {
  277. spin_unlock(&sa_manager->lock);
  278. r = radeon_fence_wait_any(rdev, fences, false);
  279. spin_lock(&sa_manager->lock);
  280. if (r) {
  281. /* if we have nothing to wait for we
  282. are practically out of memory */
  283. if (r == -ENOENT) {
  284. r = -ENOMEM;
  285. }
  286. goto out_err;
  287. }
  288. }
  289. } while (block);
  290. out_err:
  291. spin_unlock(&sa_manager->lock);
  292. kfree(*sa_bo);
  293. *sa_bo = NULL;
  294. return r;
  295. }
  296. void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo,
  297. struct radeon_fence *fence)
  298. {
  299. struct radeon_sa_manager *sa_manager;
  300. if (sa_bo == NULL || *sa_bo == NULL) {
  301. return;
  302. }
  303. sa_manager = (*sa_bo)->manager;
  304. spin_lock(&sa_manager->lock);
  305. if (fence && fence->seq && fence->seq < RADEON_FENCE_NOTEMITED_SEQ) {
  306. (*sa_bo)->fence = radeon_fence_ref(fence);
  307. list_add_tail(&(*sa_bo)->flist,
  308. &sa_manager->flist[fence->ring]);
  309. } else {
  310. radeon_sa_bo_remove_locked(*sa_bo);
  311. }
  312. spin_unlock(&sa_manager->lock);
  313. *sa_bo = NULL;
  314. }
  315. #if defined(CONFIG_DEBUG_FS)
  316. void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
  317. struct seq_file *m)
  318. {
  319. struct radeon_sa_bo *i;
  320. spin_lock(&sa_manager->lock);
  321. list_for_each_entry(i, &sa_manager->olist, olist) {
  322. if (&i->olist == sa_manager->hole) {
  323. seq_printf(m, ">");
  324. } else {
  325. seq_printf(m, " ");
  326. }
  327. seq_printf(m, "[0x%08x 0x%08x] size %8d",
  328. i->soffset, i->eoffset, i->eoffset - i->soffset);
  329. if (i->fence) {
  330. seq_printf(m, " protected by 0x%016llx on ring %d",
  331. i->fence->seq, i->fence->ring);
  332. }
  333. seq_printf(m, "\n");
  334. }
  335. spin_unlock(&sa_manager->lock);
  336. }
  337. #endif