radeon_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. INIT_LIST_HEAD(&tmp->list);
  109. if (tmp->fence) {
  110. radeon_fence_unref(&tmp->fence);
  111. }
  112. tmp->length_dw = 0;
  113. clear_bit(tmp->idx, rdev->ib_pool.alloc_bm);
  114. mutex_unlock(&rdev->ib_pool.mutex);
  115. }
  116. static void radeon_ib_align(struct radeon_device *rdev, struct radeon_ib *ib)
  117. {
  118. while ((ib->length_dw & rdev->cp.align_mask)) {
  119. ib->ptr[ib->length_dw++] = PACKET2(0);
  120. }
  121. }
  122. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  123. {
  124. int r = 0;
  125. mutex_lock(&rdev->ib_pool.mutex);
  126. radeon_ib_align(rdev, ib);
  127. if (!ib->length_dw || !rdev->cp.ready) {
  128. /* TODO: Nothings in the ib we should report. */
  129. mutex_unlock(&rdev->ib_pool.mutex);
  130. DRM_ERROR("radeon: couldn't schedule IB(%lu).\n", ib->idx);
  131. return -EINVAL;
  132. }
  133. /* 64 dwords should be enough for fence too */
  134. r = radeon_ring_lock(rdev, 64);
  135. if (r) {
  136. DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
  137. mutex_unlock(&rdev->ib_pool.mutex);
  138. return r;
  139. }
  140. radeon_ring_write(rdev, PACKET0(RADEON_CP_IB_BASE, 1));
  141. radeon_ring_write(rdev, ib->gpu_addr);
  142. radeon_ring_write(rdev, ib->length_dw);
  143. radeon_fence_emit(rdev, ib->fence);
  144. radeon_ring_unlock_commit(rdev);
  145. list_add_tail(&ib->list, &rdev->ib_pool.scheduled_ibs);
  146. mutex_unlock(&rdev->ib_pool.mutex);
  147. return 0;
  148. }
  149. int radeon_ib_pool_init(struct radeon_device *rdev)
  150. {
  151. void *ptr;
  152. uint64_t gpu_addr;
  153. int i;
  154. int r = 0;
  155. /* Allocate 1M object buffer */
  156. INIT_LIST_HEAD(&rdev->ib_pool.scheduled_ibs);
  157. r = radeon_object_create(rdev, NULL, RADEON_IB_POOL_SIZE*64*1024,
  158. true, RADEON_GEM_DOMAIN_GTT,
  159. false, &rdev->ib_pool.robj);
  160. if (r) {
  161. DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  162. return r;
  163. }
  164. r = radeon_object_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  165. if (r) {
  166. DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  167. return r;
  168. }
  169. r = radeon_object_kmap(rdev->ib_pool.robj, &ptr);
  170. if (r) {
  171. DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
  172. return r;
  173. }
  174. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  175. unsigned offset;
  176. offset = i * 64 * 1024;
  177. rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  178. rdev->ib_pool.ibs[i].ptr = ptr + offset;
  179. rdev->ib_pool.ibs[i].idx = i;
  180. rdev->ib_pool.ibs[i].length_dw = 0;
  181. INIT_LIST_HEAD(&rdev->ib_pool.ibs[i].list);
  182. }
  183. bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  184. rdev->ib_pool.ready = true;
  185. DRM_INFO("radeon: ib pool ready.\n");
  186. if (radeon_debugfs_ib_init(rdev)) {
  187. DRM_ERROR("Failed to register debugfs file for IB !\n");
  188. }
  189. return r;
  190. }
  191. void radeon_ib_pool_fini(struct radeon_device *rdev)
  192. {
  193. if (!rdev->ib_pool.ready) {
  194. return;
  195. }
  196. mutex_lock(&rdev->ib_pool.mutex);
  197. bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  198. if (rdev->ib_pool.robj) {
  199. radeon_object_kunmap(rdev->ib_pool.robj);
  200. radeon_object_unref(&rdev->ib_pool.robj);
  201. rdev->ib_pool.robj = NULL;
  202. }
  203. mutex_unlock(&rdev->ib_pool.mutex);
  204. }
  205. int radeon_ib_test(struct radeon_device *rdev)
  206. {
  207. struct radeon_ib *ib;
  208. uint32_t scratch;
  209. uint32_t tmp = 0;
  210. unsigned i;
  211. int r;
  212. r = radeon_scratch_get(rdev, &scratch);
  213. if (r) {
  214. DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
  215. return r;
  216. }
  217. WREG32(scratch, 0xCAFEDEAD);
  218. r = radeon_ib_get(rdev, &ib);
  219. if (r) {
  220. return r;
  221. }
  222. ib->ptr[0] = PACKET0(scratch, 0);
  223. ib->ptr[1] = 0xDEADBEEF;
  224. ib->ptr[2] = PACKET2(0);
  225. ib->ptr[3] = PACKET2(0);
  226. ib->ptr[4] = PACKET2(0);
  227. ib->ptr[5] = PACKET2(0);
  228. ib->ptr[6] = PACKET2(0);
  229. ib->ptr[7] = PACKET2(0);
  230. ib->length_dw = 8;
  231. r = radeon_ib_schedule(rdev, ib);
  232. if (r) {
  233. radeon_scratch_free(rdev, scratch);
  234. radeon_ib_free(rdev, &ib);
  235. return r;
  236. }
  237. r = radeon_fence_wait(ib->fence, false);
  238. if (r) {
  239. return r;
  240. }
  241. for (i = 0; i < rdev->usec_timeout; i++) {
  242. tmp = RREG32(scratch);
  243. if (tmp == 0xDEADBEEF) {
  244. break;
  245. }
  246. DRM_UDELAY(1);
  247. }
  248. if (i < rdev->usec_timeout) {
  249. DRM_INFO("ib test succeeded in %u usecs\n", i);
  250. } else {
  251. DRM_ERROR("radeon: ib test failed (sracth(0x%04X)=0x%08X)\n",
  252. scratch, tmp);
  253. r = -EINVAL;
  254. }
  255. radeon_scratch_free(rdev, scratch);
  256. radeon_ib_free(rdev, &ib);
  257. return r;
  258. }
  259. /*
  260. * Ring.
  261. */
  262. void radeon_ring_free_size(struct radeon_device *rdev)
  263. {
  264. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  265. /* This works because ring_size is a power of 2 */
  266. rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  267. rdev->cp.ring_free_dw -= rdev->cp.wptr;
  268. rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  269. if (!rdev->cp.ring_free_dw) {
  270. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  271. }
  272. }
  273. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  274. {
  275. int r;
  276. /* Align requested size with padding so unlock_commit can
  277. * pad safely */
  278. ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  279. mutex_lock(&rdev->cp.mutex);
  280. while (ndw > (rdev->cp.ring_free_dw - 1)) {
  281. radeon_ring_free_size(rdev);
  282. if (ndw < rdev->cp.ring_free_dw) {
  283. break;
  284. }
  285. r = radeon_fence_wait_next(rdev);
  286. if (r) {
  287. mutex_unlock(&rdev->cp.mutex);
  288. return r;
  289. }
  290. }
  291. rdev->cp.count_dw = ndw;
  292. rdev->cp.wptr_old = rdev->cp.wptr;
  293. return 0;
  294. }
  295. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  296. {
  297. unsigned count_dw_pad;
  298. unsigned i;
  299. /* We pad to match fetch size */
  300. count_dw_pad = (rdev->cp.align_mask + 1) -
  301. (rdev->cp.wptr & rdev->cp.align_mask);
  302. for (i = 0; i < count_dw_pad; i++) {
  303. radeon_ring_write(rdev, PACKET2(0));
  304. }
  305. DRM_MEMORYBARRIER();
  306. WREG32(RADEON_CP_RB_WPTR, rdev->cp.wptr);
  307. (void)RREG32(RADEON_CP_RB_WPTR);
  308. mutex_unlock(&rdev->cp.mutex);
  309. }
  310. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  311. {
  312. rdev->cp.wptr = rdev->cp.wptr_old;
  313. mutex_unlock(&rdev->cp.mutex);
  314. }
  315. int radeon_ring_test(struct radeon_device *rdev)
  316. {
  317. uint32_t scratch;
  318. uint32_t tmp = 0;
  319. unsigned i;
  320. int r;
  321. r = radeon_scratch_get(rdev, &scratch);
  322. if (r) {
  323. DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
  324. return r;
  325. }
  326. WREG32(scratch, 0xCAFEDEAD);
  327. r = radeon_ring_lock(rdev, 2);
  328. if (r) {
  329. DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
  330. radeon_scratch_free(rdev, scratch);
  331. return r;
  332. }
  333. radeon_ring_write(rdev, PACKET0(scratch, 0));
  334. radeon_ring_write(rdev, 0xDEADBEEF);
  335. radeon_ring_unlock_commit(rdev);
  336. for (i = 0; i < rdev->usec_timeout; i++) {
  337. tmp = RREG32(scratch);
  338. if (tmp == 0xDEADBEEF) {
  339. break;
  340. }
  341. DRM_UDELAY(1);
  342. }
  343. if (i < rdev->usec_timeout) {
  344. DRM_INFO("ring test succeeded in %d usecs\n", i);
  345. } else {
  346. DRM_ERROR("radeon: ring test failed (sracth(0x%04X)=0x%08X)\n",
  347. scratch, tmp);
  348. r = -EINVAL;
  349. }
  350. radeon_scratch_free(rdev, scratch);
  351. return r;
  352. }
  353. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  354. {
  355. int r;
  356. rdev->cp.ring_size = ring_size;
  357. /* Allocate ring buffer */
  358. if (rdev->cp.ring_obj == NULL) {
  359. r = radeon_object_create(rdev, NULL, rdev->cp.ring_size,
  360. true,
  361. RADEON_GEM_DOMAIN_GTT,
  362. false,
  363. &rdev->cp.ring_obj);
  364. if (r) {
  365. DRM_ERROR("radeon: failed to create ring buffer (%d).\n", r);
  366. mutex_unlock(&rdev->cp.mutex);
  367. return r;
  368. }
  369. r = radeon_object_pin(rdev->cp.ring_obj,
  370. RADEON_GEM_DOMAIN_GTT,
  371. &rdev->cp.gpu_addr);
  372. if (r) {
  373. DRM_ERROR("radeon: failed to pin ring buffer (%d).\n", r);
  374. mutex_unlock(&rdev->cp.mutex);
  375. return r;
  376. }
  377. r = radeon_object_kmap(rdev->cp.ring_obj,
  378. (void **)&rdev->cp.ring);
  379. if (r) {
  380. DRM_ERROR("radeon: failed to map ring buffer (%d).\n", r);
  381. mutex_unlock(&rdev->cp.mutex);
  382. return r;
  383. }
  384. }
  385. rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  386. rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  387. return 0;
  388. }
  389. void radeon_ring_fini(struct radeon_device *rdev)
  390. {
  391. mutex_lock(&rdev->cp.mutex);
  392. if (rdev->cp.ring_obj) {
  393. radeon_object_kunmap(rdev->cp.ring_obj);
  394. radeon_object_unpin(rdev->cp.ring_obj);
  395. radeon_object_unref(&rdev->cp.ring_obj);
  396. rdev->cp.ring = NULL;
  397. rdev->cp.ring_obj = NULL;
  398. }
  399. mutex_unlock(&rdev->cp.mutex);
  400. }
  401. /*
  402. * Debugfs info
  403. */
  404. #if defined(CONFIG_DEBUG_FS)
  405. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  406. {
  407. struct drm_info_node *node = (struct drm_info_node *) m->private;
  408. struct radeon_ib *ib = node->info_ent->data;
  409. unsigned i;
  410. if (ib == NULL) {
  411. return 0;
  412. }
  413. seq_printf(m, "IB %04lu\n", ib->idx);
  414. seq_printf(m, "IB fence %p\n", ib->fence);
  415. seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  416. for (i = 0; i < ib->length_dw; i++) {
  417. seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  418. }
  419. return 0;
  420. }
  421. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  422. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  423. #endif
  424. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  425. {
  426. #if defined(CONFIG_DEBUG_FS)
  427. unsigned i;
  428. for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  429. sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  430. radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  431. radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  432. radeon_debugfs_ib_list[i].driver_features = 0;
  433. radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  434. }
  435. return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  436. RADEON_IB_POOL_SIZE);
  437. #else
  438. return 0;
  439. #endif
  440. }