radeon_ring.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. unsigned long i;
  43. int r = 0;
  44. *ib = NULL;
  45. r = radeon_fence_create(rdev, &fence);
  46. if (r) {
  47. DRM_ERROR("failed to create fence for new IB\n");
  48. return r;
  49. }
  50. mutex_lock(&rdev->ib_pool.mutex);
  51. i = find_first_zero_bit(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  52. if (i < RADEON_IB_POOL_SIZE) {
  53. set_bit(i, rdev->ib_pool.alloc_bm);
  54. rdev->ib_pool.ibs[i].length_dw = 0;
  55. *ib = &rdev->ib_pool.ibs[i];
  56. goto out;
  57. }
  58. if (list_empty(&rdev->ib_pool.scheduled_ibs)) {
  59. /* we go do nothings here */
  60. DRM_ERROR("all IB allocated none scheduled.\n");
  61. r = -EINVAL;
  62. goto out;
  63. }
  64. /* get the first ib on the scheduled list */
  65. nib = list_entry(rdev->ib_pool.scheduled_ibs.next,
  66. struct radeon_ib, list);
  67. if (nib->fence == NULL) {
  68. /* we go do nothings here */
  69. DRM_ERROR("IB %lu scheduled without a fence.\n", nib->idx);
  70. r = -EINVAL;
  71. goto out;
  72. }
  73. r = radeon_fence_wait(nib->fence, false);
  74. if (r) {
  75. DRM_ERROR("radeon: IB(%lu:0x%016lX:%u)\n", nib->idx,
  76. (unsigned long)nib->gpu_addr, nib->length_dw);
  77. DRM_ERROR("radeon: GPU lockup detected, fail to get a IB\n");
  78. goto out;
  79. }
  80. radeon_fence_unref(&nib->fence);
  81. nib->length_dw = 0;
  82. list_del(&nib->list);
  83. INIT_LIST_HEAD(&nib->list);
  84. *ib = nib;
  85. out:
  86. mutex_unlock(&rdev->ib_pool.mutex);
  87. if (r) {
  88. radeon_fence_unref(&fence);
  89. } else {
  90. (*ib)->fence = fence;
  91. }
  92. return r;
  93. }
  94. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  95. {
  96. struct radeon_ib *tmp = *ib;
  97. *ib = NULL;
  98. if (tmp == NULL) {
  99. return;
  100. }
  101. mutex_lock(&rdev->ib_pool.mutex);
  102. if (!list_empty(&tmp->list) && !radeon_fence_signaled(tmp->fence)) {
  103. /* IB is scheduled & not signaled don't do anythings */
  104. mutex_unlock(&rdev->ib_pool.mutex);
  105. return;
  106. }
  107. list_del(&tmp->list);
  108. if (tmp->fence) {
  109. radeon_fence_unref(&tmp->fence);
  110. }
  111. tmp->length_dw = 0;
  112. clear_bit(tmp->idx, rdev->ib_pool.alloc_bm);
  113. mutex_unlock(&rdev->ib_pool.mutex);
  114. }
  115. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  116. {
  117. int r = 0;
  118. mutex_lock(&rdev->ib_pool.mutex);
  119. if (!ib->length_dw || !rdev->cp.ready) {
  120. /* TODO: Nothings in the ib we should report. */
  121. mutex_unlock(&rdev->ib_pool.mutex);
  122. DRM_ERROR("radeon: couldn't schedule IB(%lu).\n", ib->idx);
  123. return -EINVAL;
  124. }
  125. /* 64 dwords should be enough for fence too */
  126. r = radeon_ring_lock(rdev, 64);
  127. if (r) {
  128. DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
  129. mutex_unlock(&rdev->ib_pool.mutex);
  130. return r;
  131. }
  132. radeon_ring_ib_execute(rdev, ib);
  133. radeon_fence_emit(rdev, ib->fence);
  134. radeon_ring_unlock_commit(rdev);
  135. list_add_tail(&ib->list, &rdev->ib_pool.scheduled_ibs);
  136. mutex_unlock(&rdev->ib_pool.mutex);
  137. return 0;
  138. }
  139. int radeon_ib_pool_init(struct radeon_device *rdev)
  140. {
  141. void *ptr;
  142. uint64_t gpu_addr;
  143. int i;
  144. int r = 0;
  145. /* Allocate 1M object buffer */
  146. INIT_LIST_HEAD(&rdev->ib_pool.scheduled_ibs);
  147. r = radeon_object_create(rdev, NULL, RADEON_IB_POOL_SIZE*64*1024,
  148. true, RADEON_GEM_DOMAIN_GTT,
  149. false, &rdev->ib_pool.robj);
  150. if (r) {
  151. DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  152. return r;
  153. }
  154. r = radeon_object_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  155. if (r) {
  156. DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  157. return r;
  158. }
  159. r = radeon_object_kmap(rdev->ib_pool.robj, &ptr);
  160. if (r) {
  161. DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
  162. return r;
  163. }
  164. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  165. unsigned offset;
  166. offset = i * 64 * 1024;
  167. rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  168. rdev->ib_pool.ibs[i].ptr = ptr + offset;
  169. rdev->ib_pool.ibs[i].idx = i;
  170. rdev->ib_pool.ibs[i].length_dw = 0;
  171. INIT_LIST_HEAD(&rdev->ib_pool.ibs[i].list);
  172. }
  173. bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  174. rdev->ib_pool.ready = true;
  175. DRM_INFO("radeon: ib pool ready.\n");
  176. if (radeon_debugfs_ib_init(rdev)) {
  177. DRM_ERROR("Failed to register debugfs file for IB !\n");
  178. }
  179. return r;
  180. }
  181. void radeon_ib_pool_fini(struct radeon_device *rdev)
  182. {
  183. if (!rdev->ib_pool.ready) {
  184. return;
  185. }
  186. mutex_lock(&rdev->ib_pool.mutex);
  187. bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  188. if (rdev->ib_pool.robj) {
  189. radeon_object_kunmap(rdev->ib_pool.robj);
  190. radeon_object_unref(&rdev->ib_pool.robj);
  191. rdev->ib_pool.robj = NULL;
  192. }
  193. mutex_unlock(&rdev->ib_pool.mutex);
  194. }
  195. /*
  196. * Ring.
  197. */
  198. void radeon_ring_free_size(struct radeon_device *rdev)
  199. {
  200. if (rdev->family >= CHIP_R600)
  201. rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  202. else
  203. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  204. /* This works because ring_size is a power of 2 */
  205. rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  206. rdev->cp.ring_free_dw -= rdev->cp.wptr;
  207. rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  208. if (!rdev->cp.ring_free_dw) {
  209. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  210. }
  211. }
  212. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  213. {
  214. int r;
  215. /* Align requested size with padding so unlock_commit can
  216. * pad safely */
  217. ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  218. mutex_lock(&rdev->cp.mutex);
  219. while (ndw > (rdev->cp.ring_free_dw - 1)) {
  220. radeon_ring_free_size(rdev);
  221. if (ndw < rdev->cp.ring_free_dw) {
  222. break;
  223. }
  224. r = radeon_fence_wait_next(rdev);
  225. if (r) {
  226. mutex_unlock(&rdev->cp.mutex);
  227. return r;
  228. }
  229. }
  230. rdev->cp.count_dw = ndw;
  231. rdev->cp.wptr_old = rdev->cp.wptr;
  232. return 0;
  233. }
  234. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  235. {
  236. unsigned count_dw_pad;
  237. unsigned i;
  238. /* We pad to match fetch size */
  239. count_dw_pad = (rdev->cp.align_mask + 1) -
  240. (rdev->cp.wptr & rdev->cp.align_mask);
  241. for (i = 0; i < count_dw_pad; i++) {
  242. radeon_ring_write(rdev, 2 << 30);
  243. }
  244. DRM_MEMORYBARRIER();
  245. radeon_cp_commit(rdev);
  246. mutex_unlock(&rdev->cp.mutex);
  247. }
  248. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  249. {
  250. rdev->cp.wptr = rdev->cp.wptr_old;
  251. mutex_unlock(&rdev->cp.mutex);
  252. }
  253. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  254. {
  255. int r;
  256. rdev->cp.ring_size = ring_size;
  257. /* Allocate ring buffer */
  258. if (rdev->cp.ring_obj == NULL) {
  259. r = radeon_object_create(rdev, NULL, rdev->cp.ring_size,
  260. true,
  261. RADEON_GEM_DOMAIN_GTT,
  262. false,
  263. &rdev->cp.ring_obj);
  264. if (r) {
  265. DRM_ERROR("radeon: failed to create ring buffer (%d).\n", r);
  266. mutex_unlock(&rdev->cp.mutex);
  267. return r;
  268. }
  269. r = radeon_object_pin(rdev->cp.ring_obj,
  270. RADEON_GEM_DOMAIN_GTT,
  271. &rdev->cp.gpu_addr);
  272. if (r) {
  273. DRM_ERROR("radeon: failed to pin ring buffer (%d).\n", r);
  274. mutex_unlock(&rdev->cp.mutex);
  275. return r;
  276. }
  277. r = radeon_object_kmap(rdev->cp.ring_obj,
  278. (void **)&rdev->cp.ring);
  279. if (r) {
  280. DRM_ERROR("radeon: failed to map ring buffer (%d).\n", r);
  281. mutex_unlock(&rdev->cp.mutex);
  282. return r;
  283. }
  284. }
  285. rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  286. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  287. return 0;
  288. }
  289. void radeon_ring_fini(struct radeon_device *rdev)
  290. {
  291. mutex_lock(&rdev->cp.mutex);
  292. if (rdev->cp.ring_obj) {
  293. radeon_object_kunmap(rdev->cp.ring_obj);
  294. radeon_object_unpin(rdev->cp.ring_obj);
  295. radeon_object_unref(&rdev->cp.ring_obj);
  296. rdev->cp.ring = NULL;
  297. rdev->cp.ring_obj = NULL;
  298. }
  299. mutex_unlock(&rdev->cp.mutex);
  300. }
  301. /*
  302. * Debugfs info
  303. */
  304. #if defined(CONFIG_DEBUG_FS)
  305. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  306. {
  307. struct drm_info_node *node = (struct drm_info_node *) m->private;
  308. struct radeon_ib *ib = node->info_ent->data;
  309. unsigned i;
  310. if (ib == NULL) {
  311. return 0;
  312. }
  313. seq_printf(m, "IB %04lu\n", ib->idx);
  314. seq_printf(m, "IB fence %p\n", ib->fence);
  315. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  316. for (i = 0; i < ib->length_dw; i++) {
  317. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  318. }
  319. return 0;
  320. }
  321. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  322. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  323. #endif
  324. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  325. {
  326. #if defined(CONFIG_DEBUG_FS)
  327. unsigned i;
  328. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  329. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  330. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  331. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  332. radeon_debugfs_ib_list[i].driver_features = 0;
  333. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  334. }
  335. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  336. RADEON_IB_POOL_SIZE);
  337. #else
  338. return 0;
  339. #endif
  340. }