amdgpu_ring.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. * Christian König
  28. */
  29. #include <linux/seq_file.h>
  30. #include <linux/slab.h>
  31. #include <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "atom.h"
  35. /*
  36. * Rings
  37. * Most engines on the GPU are fed via ring buffers. Ring
  38. * buffers are areas of GPU accessible memory that the host
  39. * writes commands into and the GPU reads commands out of.
  40. * There is a rptr (read pointer) that determines where the
  41. * GPU is currently reading, and a wptr (write pointer)
  42. * which determines where the host has written. When the
  43. * pointers are equal, the ring is idle. When the host
  44. * writes commands to the ring buffer, it increments the
  45. * wptr. The GPU then starts fetching commands and executes
  46. * them until the pointers are equal again.
  47. */
  48. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring);
  49. /**
  50. * amdgpu_ring_free_size - update the free size
  51. *
  52. * @adev: amdgpu_device pointer
  53. * @ring: amdgpu_ring structure holding ring information
  54. *
  55. * Update the free dw slots in the ring buffer (all asics).
  56. */
  57. void amdgpu_ring_free_size(struct amdgpu_ring *ring)
  58. {
  59. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  60. /* This works because ring_size is a power of 2 */
  61. ring->ring_free_dw = rptr + (ring->ring_size / 4);
  62. ring->ring_free_dw -= ring->wptr;
  63. ring->ring_free_dw &= ring->ptr_mask;
  64. if (!ring->ring_free_dw) {
  65. /* this is an empty ring */
  66. ring->ring_free_dw = ring->ring_size / 4;
  67. /* update lockup info to avoid false positive */
  68. amdgpu_ring_lockup_update(ring);
  69. }
  70. }
  71. /**
  72. * amdgpu_ring_alloc - allocate space on the ring buffer
  73. *
  74. * @adev: amdgpu_device pointer
  75. * @ring: amdgpu_ring structure holding ring information
  76. * @ndw: number of dwords to allocate in the ring buffer
  77. *
  78. * Allocate @ndw dwords in the ring buffer (all asics).
  79. * Returns 0 on success, error on failure.
  80. */
  81. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
  82. {
  83. int r;
  84. /* make sure we aren't trying to allocate more space than there is on the ring */
  85. if (ndw > (ring->ring_size / 4))
  86. return -ENOMEM;
  87. /* Align requested size with padding so unlock_commit can
  88. * pad safely */
  89. amdgpu_ring_free_size(ring);
  90. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  91. while (ndw > (ring->ring_free_dw - 1)) {
  92. amdgpu_ring_free_size(ring);
  93. if (ndw < ring->ring_free_dw) {
  94. break;
  95. }
  96. r = amdgpu_fence_wait_next(ring);
  97. if (r)
  98. return r;
  99. }
  100. ring->count_dw = ndw;
  101. ring->wptr_old = ring->wptr;
  102. return 0;
  103. }
  104. /**
  105. * amdgpu_ring_lock - lock the ring and allocate space on it
  106. *
  107. * @adev: amdgpu_device pointer
  108. * @ring: amdgpu_ring structure holding ring information
  109. * @ndw: number of dwords to allocate in the ring buffer
  110. *
  111. * Lock the ring and allocate @ndw dwords in the ring buffer
  112. * (all asics).
  113. * Returns 0 on success, error on failure.
  114. */
  115. int amdgpu_ring_lock(struct amdgpu_ring *ring, unsigned ndw)
  116. {
  117. int r;
  118. mutex_lock(ring->ring_lock);
  119. r = amdgpu_ring_alloc(ring, ndw);
  120. if (r) {
  121. mutex_unlock(ring->ring_lock);
  122. return r;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * amdgpu_ring_commit - tell the GPU to execute the new
  128. * commands on the ring buffer
  129. *
  130. * @adev: amdgpu_device pointer
  131. * @ring: amdgpu_ring structure holding ring information
  132. *
  133. * Update the wptr (write pointer) to tell the GPU to
  134. * execute new commands on the ring buffer (all asics).
  135. */
  136. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  137. {
  138. /* We pad to match fetch size */
  139. while (ring->wptr & ring->align_mask) {
  140. amdgpu_ring_write(ring, ring->nop);
  141. }
  142. mb();
  143. amdgpu_ring_set_wptr(ring);
  144. }
  145. /**
  146. * amdgpu_ring_unlock_commit - tell the GPU to execute the new
  147. * commands on the ring buffer and unlock it
  148. *
  149. * @ring: amdgpu_ring structure holding ring information
  150. *
  151. * Call amdgpu_ring_commit() then unlock the ring (all asics).
  152. */
  153. void amdgpu_ring_unlock_commit(struct amdgpu_ring *ring)
  154. {
  155. amdgpu_ring_commit(ring);
  156. mutex_unlock(ring->ring_lock);
  157. }
  158. /**
  159. * amdgpu_ring_undo - reset the wptr
  160. *
  161. * @ring: amdgpu_ring structure holding ring information
  162. *
  163. * Reset the driver's copy of the wptr (all asics).
  164. */
  165. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  166. {
  167. ring->wptr = ring->wptr_old;
  168. }
  169. /**
  170. * amdgpu_ring_unlock_undo - reset the wptr and unlock the ring
  171. *
  172. * @ring: amdgpu_ring structure holding ring information
  173. *
  174. * Call amdgpu_ring_undo() then unlock the ring (all asics).
  175. */
  176. void amdgpu_ring_unlock_undo(struct amdgpu_ring *ring)
  177. {
  178. amdgpu_ring_undo(ring);
  179. mutex_unlock(ring->ring_lock);
  180. }
  181. /**
  182. * amdgpu_ring_lockup_update - update lockup variables
  183. *
  184. * @ring: amdgpu_ring structure holding ring information
  185. *
  186. * Update the last rptr value and timestamp (all asics).
  187. */
  188. void amdgpu_ring_lockup_update(struct amdgpu_ring *ring)
  189. {
  190. atomic_set(&ring->last_rptr, amdgpu_ring_get_rptr(ring));
  191. atomic64_set(&ring->last_activity, jiffies_64);
  192. }
  193. /**
  194. * amdgpu_ring_test_lockup() - check if ring is lockedup by recording information
  195. * @ring: amdgpu_ring structure holding ring information
  196. *
  197. */
  198. bool amdgpu_ring_test_lockup(struct amdgpu_ring *ring)
  199. {
  200. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  201. uint64_t last = atomic64_read(&ring->last_activity);
  202. uint64_t elapsed;
  203. if (rptr != atomic_read(&ring->last_rptr)) {
  204. /* ring is still working, no lockup */
  205. amdgpu_ring_lockup_update(ring);
  206. return false;
  207. }
  208. elapsed = jiffies_to_msecs(jiffies_64 - last);
  209. if (amdgpu_lockup_timeout && elapsed >= amdgpu_lockup_timeout) {
  210. dev_err(ring->adev->dev, "ring %d stalled for more than %llumsec\n",
  211. ring->idx, elapsed);
  212. return true;
  213. }
  214. /* give a chance to the GPU ... */
  215. return false;
  216. }
  217. /**
  218. * amdgpu_ring_backup - Back up the content of a ring
  219. *
  220. * @ring: the ring we want to back up
  221. *
  222. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  223. */
  224. unsigned amdgpu_ring_backup(struct amdgpu_ring *ring,
  225. uint32_t **data)
  226. {
  227. unsigned size, ptr, i;
  228. /* just in case lock the ring */
  229. mutex_lock(ring->ring_lock);
  230. *data = NULL;
  231. if (ring->ring_obj == NULL) {
  232. mutex_unlock(ring->ring_lock);
  233. return 0;
  234. }
  235. /* it doesn't make sense to save anything if all fences are signaled */
  236. if (!amdgpu_fence_count_emitted(ring)) {
  237. mutex_unlock(ring->ring_lock);
  238. return 0;
  239. }
  240. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  241. size = ring->wptr + (ring->ring_size / 4);
  242. size -= ptr;
  243. size &= ring->ptr_mask;
  244. if (size == 0) {
  245. mutex_unlock(ring->ring_lock);
  246. return 0;
  247. }
  248. /* and then save the content of the ring */
  249. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  250. if (!*data) {
  251. mutex_unlock(ring->ring_lock);
  252. return 0;
  253. }
  254. for (i = 0; i < size; ++i) {
  255. (*data)[i] = ring->ring[ptr++];
  256. ptr &= ring->ptr_mask;
  257. }
  258. mutex_unlock(ring->ring_lock);
  259. return size;
  260. }
  261. /**
  262. * amdgpu_ring_restore - append saved commands to the ring again
  263. *
  264. * @ring: ring to append commands to
  265. * @size: number of dwords we want to write
  266. * @data: saved commands
  267. *
  268. * Allocates space on the ring and restore the previously saved commands.
  269. */
  270. int amdgpu_ring_restore(struct amdgpu_ring *ring,
  271. unsigned size, uint32_t *data)
  272. {
  273. int i, r;
  274. if (!size || !data)
  275. return 0;
  276. /* restore the saved ring content */
  277. r = amdgpu_ring_lock(ring, size);
  278. if (r)
  279. return r;
  280. for (i = 0; i < size; ++i) {
  281. amdgpu_ring_write(ring, data[i]);
  282. }
  283. amdgpu_ring_unlock_commit(ring);
  284. kfree(data);
  285. return 0;
  286. }
  287. /**
  288. * amdgpu_ring_init - init driver ring struct.
  289. *
  290. * @adev: amdgpu_device pointer
  291. * @ring: amdgpu_ring structure holding ring information
  292. * @ring_size: size of the ring
  293. * @nop: nop packet for this ring
  294. *
  295. * Initialize the driver information for the selected ring (all asics).
  296. * Returns 0 on success, error on failure.
  297. */
  298. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  299. unsigned ring_size, u32 nop, u32 align_mask,
  300. struct amdgpu_irq_src *irq_src, unsigned irq_type,
  301. enum amdgpu_ring_type ring_type)
  302. {
  303. u32 rb_bufsz;
  304. int r;
  305. if (ring->adev == NULL) {
  306. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  307. return -EINVAL;
  308. ring->adev = adev;
  309. ring->idx = adev->num_rings++;
  310. adev->rings[ring->idx] = ring;
  311. amdgpu_fence_driver_init_ring(ring);
  312. }
  313. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  314. if (r) {
  315. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  316. return r;
  317. }
  318. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  319. if (r) {
  320. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  321. return r;
  322. }
  323. r = amdgpu_wb_get(adev, &ring->fence_offs);
  324. if (r) {
  325. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  326. return r;
  327. }
  328. r = amdgpu_wb_get(adev, &ring->next_rptr_offs);
  329. if (r) {
  330. dev_err(adev->dev, "(%d) ring next_rptr wb alloc failed\n", r);
  331. return r;
  332. }
  333. ring->next_rptr_gpu_addr = adev->wb.gpu_addr + (ring->next_rptr_offs * 4);
  334. ring->next_rptr_cpu_addr = &adev->wb.wb[ring->next_rptr_offs];
  335. spin_lock_init(&ring->fence_lock);
  336. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  337. if (r) {
  338. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  339. return r;
  340. }
  341. ring->ring_lock = &adev->ring_lock;
  342. /* Align ring size */
  343. rb_bufsz = order_base_2(ring_size / 8);
  344. ring_size = (1 << (rb_bufsz + 1)) * 4;
  345. ring->ring_size = ring_size;
  346. ring->align_mask = align_mask;
  347. ring->nop = nop;
  348. ring->type = ring_type;
  349. /* Allocate ring buffer */
  350. if (ring->ring_obj == NULL) {
  351. r = amdgpu_bo_create(adev, ring->ring_size, PAGE_SIZE, true,
  352. AMDGPU_GEM_DOMAIN_GTT, 0,
  353. NULL, &ring->ring_obj);
  354. if (r) {
  355. dev_err(adev->dev, "(%d) ring create failed\n", r);
  356. return r;
  357. }
  358. r = amdgpu_bo_reserve(ring->ring_obj, false);
  359. if (unlikely(r != 0))
  360. return r;
  361. r = amdgpu_bo_pin(ring->ring_obj, AMDGPU_GEM_DOMAIN_GTT,
  362. &ring->gpu_addr);
  363. if (r) {
  364. amdgpu_bo_unreserve(ring->ring_obj);
  365. dev_err(adev->dev, "(%d) ring pin failed\n", r);
  366. return r;
  367. }
  368. r = amdgpu_bo_kmap(ring->ring_obj,
  369. (void **)&ring->ring);
  370. amdgpu_bo_unreserve(ring->ring_obj);
  371. if (r) {
  372. dev_err(adev->dev, "(%d) ring map failed\n", r);
  373. return r;
  374. }
  375. }
  376. ring->ptr_mask = (ring->ring_size / 4) - 1;
  377. ring->ring_free_dw = ring->ring_size / 4;
  378. if (amdgpu_debugfs_ring_init(adev, ring)) {
  379. DRM_ERROR("Failed to register debugfs file for rings !\n");
  380. }
  381. amdgpu_ring_lockup_update(ring);
  382. return 0;
  383. }
  384. /**
  385. * amdgpu_ring_fini - tear down the driver ring struct.
  386. *
  387. * @adev: amdgpu_device pointer
  388. * @ring: amdgpu_ring structure holding ring information
  389. *
  390. * Tear down the driver information for the selected ring (all asics).
  391. */
  392. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  393. {
  394. int r;
  395. struct amdgpu_bo *ring_obj;
  396. if (ring->ring_lock == NULL)
  397. return;
  398. mutex_lock(ring->ring_lock);
  399. ring_obj = ring->ring_obj;
  400. ring->ready = false;
  401. ring->ring = NULL;
  402. ring->ring_obj = NULL;
  403. mutex_unlock(ring->ring_lock);
  404. amdgpu_wb_free(ring->adev, ring->fence_offs);
  405. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  406. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  407. amdgpu_wb_free(ring->adev, ring->next_rptr_offs);
  408. if (ring_obj) {
  409. r = amdgpu_bo_reserve(ring_obj, false);
  410. if (likely(r == 0)) {
  411. amdgpu_bo_kunmap(ring_obj);
  412. amdgpu_bo_unpin(ring_obj);
  413. amdgpu_bo_unreserve(ring_obj);
  414. }
  415. amdgpu_bo_unref(&ring_obj);
  416. }
  417. }
  418. /*
  419. * Debugfs info
  420. */
  421. #if defined(CONFIG_DEBUG_FS)
  422. static int amdgpu_debugfs_ring_info(struct seq_file *m, void *data)
  423. {
  424. struct drm_info_node *node = (struct drm_info_node *) m->private;
  425. struct drm_device *dev = node->minor->dev;
  426. struct amdgpu_device *adev = dev->dev_private;
  427. int roffset = *(int*)node->info_ent->data;
  428. struct amdgpu_ring *ring = (void *)(((uint8_t*)adev) + roffset);
  429. uint32_t rptr, wptr, rptr_next;
  430. unsigned count, i, j;
  431. amdgpu_ring_free_size(ring);
  432. count = (ring->ring_size / 4) - ring->ring_free_dw;
  433. wptr = amdgpu_ring_get_wptr(ring);
  434. seq_printf(m, "wptr: 0x%08x [%5d]\n",
  435. wptr, wptr);
  436. rptr = amdgpu_ring_get_rptr(ring);
  437. seq_printf(m, "rptr: 0x%08x [%5d]\n",
  438. rptr, rptr);
  439. rptr_next = ~0;
  440. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
  441. ring->wptr, ring->wptr);
  442. seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
  443. ring->last_semaphore_signal_addr);
  444. seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
  445. ring->last_semaphore_wait_addr);
  446. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  447. seq_printf(m, "%u dwords in ring\n", count);
  448. if (!ring->ready)
  449. return 0;
  450. /* print 8 dw before current rptr as often it's the last executed
  451. * packet that is the root issue
  452. */
  453. i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  454. for (j = 0; j <= (count + 32); j++) {
  455. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  456. if (rptr == i)
  457. seq_puts(m, " *");
  458. if (rptr_next == i)
  459. seq_puts(m, " #");
  460. seq_puts(m, "\n");
  461. i = (i + 1) & ring->ptr_mask;
  462. }
  463. return 0;
  464. }
  465. /* TODO: clean this up !*/
  466. static int amdgpu_gfx_index = offsetof(struct amdgpu_device, gfx.gfx_ring[0]);
  467. static int cayman_cp1_index = offsetof(struct amdgpu_device, gfx.compute_ring[0]);
  468. static int cayman_cp2_index = offsetof(struct amdgpu_device, gfx.compute_ring[1]);
  469. static int amdgpu_dma1_index = offsetof(struct amdgpu_device, sdma[0].ring);
  470. static int amdgpu_dma2_index = offsetof(struct amdgpu_device, sdma[1].ring);
  471. static int r600_uvd_index = offsetof(struct amdgpu_device, uvd.ring);
  472. static int si_vce1_index = offsetof(struct amdgpu_device, vce.ring[0]);
  473. static int si_vce2_index = offsetof(struct amdgpu_device, vce.ring[1]);
  474. static struct drm_info_list amdgpu_debugfs_ring_info_list[] = {
  475. {"amdgpu_ring_gfx", amdgpu_debugfs_ring_info, 0, &amdgpu_gfx_index},
  476. {"amdgpu_ring_cp1", amdgpu_debugfs_ring_info, 0, &cayman_cp1_index},
  477. {"amdgpu_ring_cp2", amdgpu_debugfs_ring_info, 0, &cayman_cp2_index},
  478. {"amdgpu_ring_dma1", amdgpu_debugfs_ring_info, 0, &amdgpu_dma1_index},
  479. {"amdgpu_ring_dma2", amdgpu_debugfs_ring_info, 0, &amdgpu_dma2_index},
  480. {"amdgpu_ring_uvd", amdgpu_debugfs_ring_info, 0, &r600_uvd_index},
  481. {"amdgpu_ring_vce1", amdgpu_debugfs_ring_info, 0, &si_vce1_index},
  482. {"amdgpu_ring_vce2", amdgpu_debugfs_ring_info, 0, &si_vce2_index},
  483. };
  484. #endif
  485. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
  486. {
  487. #if defined(CONFIG_DEBUG_FS)
  488. unsigned i;
  489. for (i = 0; i < ARRAY_SIZE(amdgpu_debugfs_ring_info_list); ++i) {
  490. struct drm_info_list *info = &amdgpu_debugfs_ring_info_list[i];
  491. int roffset = *(int*)amdgpu_debugfs_ring_info_list[i].data;
  492. struct amdgpu_ring *other = (void *)(((uint8_t*)adev) + roffset);
  493. unsigned r;
  494. if (other != ring)
  495. continue;
  496. r = amdgpu_debugfs_add_files(adev, info, 1);
  497. if (r)
  498. return r;
  499. }
  500. #endif
  501. return 0;
  502. }