radeon_ring.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/seq_file.h>
  29. #include "drmP.h"
  30. #include "radeon_drm.h"
  31. #include "radeon_reg.h"
  32. #include "radeon.h"
  33. #include "atom.h"
  34. int radeon_debugfs_ib_init(struct radeon_device *rdev);
  35. /*
  36. * IB.
  37. */
  38. int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
  39. {
  40. struct radeon_fence *fence;
  41. struct radeon_ib *nib;
  42. int r = 0, i, c;
  43. *ib = NULL;
  44. r = radeon_fence_create(rdev, &fence);
  45. if (r) {
  46. dev_err(rdev->dev, "failed to create fence for new IB\n");
  47. return r;
  48. }
  49. mutex_lock(&rdev->ib_pool.mutex);
  50. for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
  51. i &= (RADEON_IB_POOL_SIZE - 1);
  52. if (rdev->ib_pool.ibs[i].free) {
  53. nib = &rdev->ib_pool.ibs[i];
  54. break;
  55. }
  56. }
  57. if (nib == NULL) {
  58. /* This should never happen, it means we allocated all
  59. * IB and haven't scheduled one yet, return EBUSY to
  60. * userspace hoping that on ioctl recall we get better
  61. * luck
  62. */
  63. dev_err(rdev->dev, "no free indirect buffer !\n");
  64. mutex_unlock(&rdev->ib_pool.mutex);
  65. radeon_fence_unref(&fence);
  66. return -EBUSY;
  67. }
  68. rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  69. nib->free = false;
  70. if (nib->fence) {
  71. mutex_unlock(&rdev->ib_pool.mutex);
  72. r = radeon_fence_wait(nib->fence, false);
  73. if (r) {
  74. dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
  75. nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
  76. mutex_lock(&rdev->ib_pool.mutex);
  77. nib->free = true;
  78. mutex_unlock(&rdev->ib_pool.mutex);
  79. radeon_fence_unref(&fence);
  80. return r;
  81. }
  82. mutex_lock(&rdev->ib_pool.mutex);
  83. }
  84. radeon_fence_unref(&nib->fence);
  85. nib->fence = fence;
  86. nib->length_dw = 0;
  87. mutex_unlock(&rdev->ib_pool.mutex);
  88. *ib = nib;
  89. return 0;
  90. }
  91. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  92. {
  93. struct radeon_ib *tmp = *ib;
  94. *ib = NULL;
  95. if (tmp == NULL) {
  96. return;
  97. }
  98. if (!tmp->fence->emited)
  99. radeon_fence_unref(&tmp->fence);
  100. mutex_lock(&rdev->ib_pool.mutex);
  101. tmp->free = true;
  102. mutex_unlock(&rdev->ib_pool.mutex);
  103. }
  104. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  105. {
  106. int r = 0;
  107. if (!ib->length_dw || !rdev->cp.ready) {
  108. /* TODO: Nothings in the ib we should report. */
  109. DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
  110. return -EINVAL;
  111. }
  112. /* 64 dwords should be enough for fence too */
  113. r = radeon_ring_lock(rdev, 64);
  114. if (r) {
  115. DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
  116. return r;
  117. }
  118. radeon_ring_ib_execute(rdev, ib);
  119. radeon_fence_emit(rdev, ib->fence);
  120. mutex_lock(&rdev->ib_pool.mutex);
  121. /* once scheduled IB is considered free and protected by the fence */
  122. ib->free = true;
  123. mutex_unlock(&rdev->ib_pool.mutex);
  124. radeon_ring_unlock_commit(rdev);
  125. return 0;
  126. }
  127. int radeon_ib_pool_init(struct radeon_device *rdev)
  128. {
  129. void *ptr;
  130. uint64_t gpu_addr;
  131. int i;
  132. int r = 0;
  133. if (rdev->ib_pool.robj)
  134. return 0;
  135. /* Allocate 1M object buffer */
  136. r = radeon_bo_create(rdev, NULL, RADEON_IB_POOL_SIZE*64*1024,
  137. true, RADEON_GEM_DOMAIN_GTT,
  138. &rdev->ib_pool.robj);
  139. if (r) {
  140. DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  141. return r;
  142. }
  143. r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  144. if (unlikely(r != 0))
  145. return r;
  146. r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  147. if (r) {
  148. radeon_bo_unreserve(rdev->ib_pool.robj);
  149. DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  150. return r;
  151. }
  152. r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  153. radeon_bo_unreserve(rdev->ib_pool.robj);
  154. if (r) {
  155. DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
  156. return r;
  157. }
  158. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  159. unsigned offset;
  160. offset = i * 64 * 1024;
  161. rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  162. rdev->ib_pool.ibs[i].ptr = ptr + offset;
  163. rdev->ib_pool.ibs[i].idx = i;
  164. rdev->ib_pool.ibs[i].length_dw = 0;
  165. rdev->ib_pool.ibs[i].free = true;
  166. }
  167. rdev->ib_pool.head_id = 0;
  168. rdev->ib_pool.ready = true;
  169. DRM_INFO("radeon: ib pool ready.\n");
  170. if (radeon_debugfs_ib_init(rdev)) {
  171. DRM_ERROR("Failed to register debugfs file for IB !\n");
  172. }
  173. return r;
  174. }
  175. void radeon_ib_pool_fini(struct radeon_device *rdev)
  176. {
  177. int r;
  178. if (!rdev->ib_pool.ready) {
  179. return;
  180. }
  181. mutex_lock(&rdev->ib_pool.mutex);
  182. if (rdev->ib_pool.robj) {
  183. r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  184. if (likely(r == 0)) {
  185. radeon_bo_kunmap(rdev->ib_pool.robj);
  186. radeon_bo_unpin(rdev->ib_pool.robj);
  187. radeon_bo_unreserve(rdev->ib_pool.robj);
  188. }
  189. radeon_bo_unref(&rdev->ib_pool.robj);
  190. rdev->ib_pool.robj = NULL;
  191. }
  192. mutex_unlock(&rdev->ib_pool.mutex);
  193. }
  194. /*
  195. * Ring.
  196. */
  197. void radeon_ring_free_size(struct radeon_device *rdev)
  198. {
  199. if (rdev->family >= CHIP_R600)
  200. rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  201. else
  202. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  203. /* This works because ring_size is a power of 2 */
  204. rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  205. rdev->cp.ring_free_dw -= rdev->cp.wptr;
  206. rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  207. if (!rdev->cp.ring_free_dw) {
  208. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  209. }
  210. }
  211. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  212. {
  213. int r;
  214. /* Align requested size with padding so unlock_commit can
  215. * pad safely */
  216. ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  217. mutex_lock(&rdev->cp.mutex);
  218. while (ndw > (rdev->cp.ring_free_dw - 1)) {
  219. radeon_ring_free_size(rdev);
  220. if (ndw < rdev->cp.ring_free_dw) {
  221. break;
  222. }
  223. r = radeon_fence_wait_next(rdev);
  224. if (r) {
  225. mutex_unlock(&rdev->cp.mutex);
  226. return r;
  227. }
  228. }
  229. rdev->cp.count_dw = ndw;
  230. rdev->cp.wptr_old = rdev->cp.wptr;
  231. return 0;
  232. }
  233. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  234. {
  235. unsigned count_dw_pad;
  236. unsigned i;
  237. /* We pad to match fetch size */
  238. count_dw_pad = (rdev->cp.align_mask + 1) -
  239. (rdev->cp.wptr & rdev->cp.align_mask);
  240. for (i = 0; i < count_dw_pad; i++) {
  241. radeon_ring_write(rdev, 2 << 30);
  242. }
  243. DRM_MEMORYBARRIER();
  244. radeon_cp_commit(rdev);
  245. mutex_unlock(&rdev->cp.mutex);
  246. }
  247. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  248. {
  249. rdev->cp.wptr = rdev->cp.wptr_old;
  250. mutex_unlock(&rdev->cp.mutex);
  251. }
  252. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  253. {
  254. int r;
  255. rdev->cp.ring_size = ring_size;
  256. /* Allocate ring buffer */
  257. if (rdev->cp.ring_obj == NULL) {
  258. r = radeon_bo_create(rdev, NULL, rdev->cp.ring_size, true,
  259. RADEON_GEM_DOMAIN_GTT,
  260. &rdev->cp.ring_obj);
  261. if (r) {
  262. dev_err(rdev->dev, "(%d) ring create failed\n", r);
  263. return r;
  264. }
  265. r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  266. if (unlikely(r != 0))
  267. return r;
  268. r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
  269. &rdev->cp.gpu_addr);
  270. if (r) {
  271. radeon_bo_unreserve(rdev->cp.ring_obj);
  272. dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  273. return r;
  274. }
  275. r = radeon_bo_kmap(rdev->cp.ring_obj,
  276. (void **)&rdev->cp.ring);
  277. radeon_bo_unreserve(rdev->cp.ring_obj);
  278. if (r) {
  279. dev_err(rdev->dev, "(%d) ring map failed\n", r);
  280. return r;
  281. }
  282. }
  283. rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  284. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  285. return 0;
  286. }
  287. void radeon_ring_fini(struct radeon_device *rdev)
  288. {
  289. int r;
  290. mutex_lock(&rdev->cp.mutex);
  291. if (rdev->cp.ring_obj) {
  292. r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  293. if (likely(r == 0)) {
  294. radeon_bo_kunmap(rdev->cp.ring_obj);
  295. radeon_bo_unpin(rdev->cp.ring_obj);
  296. radeon_bo_unreserve(rdev->cp.ring_obj);
  297. }
  298. radeon_bo_unref(&rdev->cp.ring_obj);
  299. rdev->cp.ring = NULL;
  300. rdev->cp.ring_obj = NULL;
  301. }
  302. mutex_unlock(&rdev->cp.mutex);
  303. }
  304. /*
  305. * Debugfs info
  306. */
  307. #if defined(CONFIG_DEBUG_FS)
  308. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  309. {
  310. struct drm_info_node *node = (struct drm_info_node *) m->private;
  311. struct radeon_ib *ib = node->info_ent->data;
  312. unsigned i;
  313. if (ib == NULL) {
  314. return 0;
  315. }
  316. seq_printf(m, "IB %04u\n", ib->idx);
  317. seq_printf(m, "IB fence %p\n", ib->fence);
  318. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  319. for (i = 0; i < ib->length_dw; i++) {
  320. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  321. }
  322. return 0;
  323. }
  324. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  325. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  326. #endif
  327. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  328. {
  329. #if defined(CONFIG_DEBUG_FS)
  330. unsigned i;
  331. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  332. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  333. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  334. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  335. radeon_debugfs_ib_list[i].driver_features = 0;
  336. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  337. }
  338. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  339. RADEON_IB_POOL_SIZE);
  340. #else
  341. return 0;
  342. #endif
  343. }