radeon_ring.c 12 KB

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