amdgpu_ring.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. /** amdgpu_ring_insert_nop - insert NOP packets
  127. *
  128. * @ring: amdgpu_ring structure holding ring information
  129. * @count: the number of NOP packets to insert
  130. *
  131. * This is the generic insert_nop function for rings except SDMA
  132. */
  133. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  134. {
  135. int i;
  136. for (i = 0; i < count; i++)
  137. amdgpu_ring_write(ring, ring->nop);
  138. }
  139. /**
  140. * amdgpu_ring_commit - tell the GPU to execute the new
  141. * commands on the ring buffer
  142. *
  143. * @adev: amdgpu_device pointer
  144. * @ring: amdgpu_ring structure holding ring information
  145. *
  146. * Update the wptr (write pointer) to tell the GPU to
  147. * execute new commands on the ring buffer (all asics).
  148. */
  149. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  150. {
  151. uint32_t count;
  152. /* We pad to match fetch size */
  153. count = ring->align_mask + 1 - (ring->wptr & ring->align_mask);
  154. count %= ring->align_mask + 1;
  155. ring->funcs->insert_nop(ring, count);
  156. mb();
  157. amdgpu_ring_set_wptr(ring);
  158. }
  159. /**
  160. * amdgpu_ring_unlock_commit - tell the GPU to execute the new
  161. * commands on the ring buffer and unlock it
  162. *
  163. * @ring: amdgpu_ring structure holding ring information
  164. *
  165. * Call amdgpu_ring_commit() then unlock the ring (all asics).
  166. */
  167. void amdgpu_ring_unlock_commit(struct amdgpu_ring *ring)
  168. {
  169. amdgpu_ring_commit(ring);
  170. mutex_unlock(ring->ring_lock);
  171. }
  172. /**
  173. * amdgpu_ring_undo - reset the wptr
  174. *
  175. * @ring: amdgpu_ring structure holding ring information
  176. *
  177. * Reset the driver's copy of the wptr (all asics).
  178. */
  179. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  180. {
  181. ring->wptr = ring->wptr_old;
  182. }
  183. /**
  184. * amdgpu_ring_unlock_undo - reset the wptr and unlock the ring
  185. *
  186. * @ring: amdgpu_ring structure holding ring information
  187. *
  188. * Call amdgpu_ring_undo() then unlock the ring (all asics).
  189. */
  190. void amdgpu_ring_unlock_undo(struct amdgpu_ring *ring)
  191. {
  192. amdgpu_ring_undo(ring);
  193. mutex_unlock(ring->ring_lock);
  194. }
  195. /**
  196. * amdgpu_ring_lockup_update - update lockup variables
  197. *
  198. * @ring: amdgpu_ring structure holding ring information
  199. *
  200. * Update the last rptr value and timestamp (all asics).
  201. */
  202. void amdgpu_ring_lockup_update(struct amdgpu_ring *ring)
  203. {
  204. atomic_set(&ring->last_rptr, amdgpu_ring_get_rptr(ring));
  205. atomic64_set(&ring->last_activity, jiffies_64);
  206. }
  207. /**
  208. * amdgpu_ring_test_lockup() - check if ring is lockedup by recording information
  209. * @ring: amdgpu_ring structure holding ring information
  210. *
  211. */
  212. bool amdgpu_ring_test_lockup(struct amdgpu_ring *ring)
  213. {
  214. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  215. uint64_t last = atomic64_read(&ring->last_activity);
  216. uint64_t elapsed;
  217. if (rptr != atomic_read(&ring->last_rptr)) {
  218. /* ring is still working, no lockup */
  219. amdgpu_ring_lockup_update(ring);
  220. return false;
  221. }
  222. elapsed = jiffies_to_msecs(jiffies_64 - last);
  223. if (amdgpu_lockup_timeout && elapsed >= amdgpu_lockup_timeout) {
  224. dev_err(ring->adev->dev, "ring %d stalled for more than %llumsec\n",
  225. ring->idx, elapsed);
  226. return true;
  227. }
  228. /* give a chance to the GPU ... */
  229. return false;
  230. }
  231. /**
  232. * amdgpu_ring_backup - Back up the content of a ring
  233. *
  234. * @ring: the ring we want to back up
  235. *
  236. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  237. */
  238. unsigned amdgpu_ring_backup(struct amdgpu_ring *ring,
  239. uint32_t **data)
  240. {
  241. unsigned size, ptr, i;
  242. /* just in case lock the ring */
  243. mutex_lock(ring->ring_lock);
  244. *data = NULL;
  245. if (ring->ring_obj == NULL) {
  246. mutex_unlock(ring->ring_lock);
  247. return 0;
  248. }
  249. /* it doesn't make sense to save anything if all fences are signaled */
  250. if (!amdgpu_fence_count_emitted(ring)) {
  251. mutex_unlock(ring->ring_lock);
  252. return 0;
  253. }
  254. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  255. size = ring->wptr + (ring->ring_size / 4);
  256. size -= ptr;
  257. size &= ring->ptr_mask;
  258. if (size == 0) {
  259. mutex_unlock(ring->ring_lock);
  260. return 0;
  261. }
  262. /* and then save the content of the ring */
  263. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  264. if (!*data) {
  265. mutex_unlock(ring->ring_lock);
  266. return 0;
  267. }
  268. for (i = 0; i < size; ++i) {
  269. (*data)[i] = ring->ring[ptr++];
  270. ptr &= ring->ptr_mask;
  271. }
  272. mutex_unlock(ring->ring_lock);
  273. return size;
  274. }
  275. /**
  276. * amdgpu_ring_restore - append saved commands to the ring again
  277. *
  278. * @ring: ring to append commands to
  279. * @size: number of dwords we want to write
  280. * @data: saved commands
  281. *
  282. * Allocates space on the ring and restore the previously saved commands.
  283. */
  284. int amdgpu_ring_restore(struct amdgpu_ring *ring,
  285. unsigned size, uint32_t *data)
  286. {
  287. int i, r;
  288. if (!size || !data)
  289. return 0;
  290. /* restore the saved ring content */
  291. r = amdgpu_ring_lock(ring, size);
  292. if (r)
  293. return r;
  294. for (i = 0; i < size; ++i) {
  295. amdgpu_ring_write(ring, data[i]);
  296. }
  297. amdgpu_ring_unlock_commit(ring);
  298. kfree(data);
  299. return 0;
  300. }
  301. /**
  302. * amdgpu_ring_init - init driver ring struct.
  303. *
  304. * @adev: amdgpu_device pointer
  305. * @ring: amdgpu_ring structure holding ring information
  306. * @ring_size: size of the ring
  307. * @nop: nop packet for this ring
  308. *
  309. * Initialize the driver information for the selected ring (all asics).
  310. * Returns 0 on success, error on failure.
  311. */
  312. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  313. unsigned ring_size, u32 nop, u32 align_mask,
  314. struct amdgpu_irq_src *irq_src, unsigned irq_type,
  315. enum amdgpu_ring_type ring_type)
  316. {
  317. u32 rb_bufsz;
  318. int r;
  319. if (ring->adev == NULL) {
  320. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  321. return -EINVAL;
  322. ring->adev = adev;
  323. ring->idx = adev->num_rings++;
  324. adev->rings[ring->idx] = ring;
  325. amdgpu_fence_driver_init_ring(ring);
  326. }
  327. init_waitqueue_head(&ring->fence_drv.fence_queue);
  328. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  329. if (r) {
  330. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  331. return r;
  332. }
  333. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  334. if (r) {
  335. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  336. return r;
  337. }
  338. r = amdgpu_wb_get(adev, &ring->fence_offs);
  339. if (r) {
  340. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  341. return r;
  342. }
  343. r = amdgpu_wb_get(adev, &ring->next_rptr_offs);
  344. if (r) {
  345. dev_err(adev->dev, "(%d) ring next_rptr wb alloc failed\n", r);
  346. return r;
  347. }
  348. ring->next_rptr_gpu_addr = adev->wb.gpu_addr + (ring->next_rptr_offs * 4);
  349. ring->next_rptr_cpu_addr = &adev->wb.wb[ring->next_rptr_offs];
  350. spin_lock_init(&ring->fence_lock);
  351. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  352. if (r) {
  353. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  354. return r;
  355. }
  356. ring->ring_lock = &adev->ring_lock;
  357. /* Align ring size */
  358. rb_bufsz = order_base_2(ring_size / 8);
  359. ring_size = (1 << (rb_bufsz + 1)) * 4;
  360. ring->ring_size = ring_size;
  361. ring->align_mask = align_mask;
  362. ring->nop = nop;
  363. ring->type = ring_type;
  364. /* Allocate ring buffer */
  365. if (ring->ring_obj == NULL) {
  366. r = amdgpu_bo_create(adev, ring->ring_size, PAGE_SIZE, true,
  367. AMDGPU_GEM_DOMAIN_GTT, 0,
  368. NULL, &ring->ring_obj);
  369. if (r) {
  370. dev_err(adev->dev, "(%d) ring create failed\n", r);
  371. return r;
  372. }
  373. r = amdgpu_bo_reserve(ring->ring_obj, false);
  374. if (unlikely(r != 0))
  375. return r;
  376. r = amdgpu_bo_pin(ring->ring_obj, AMDGPU_GEM_DOMAIN_GTT,
  377. &ring->gpu_addr);
  378. if (r) {
  379. amdgpu_bo_unreserve(ring->ring_obj);
  380. dev_err(adev->dev, "(%d) ring pin failed\n", r);
  381. return r;
  382. }
  383. r = amdgpu_bo_kmap(ring->ring_obj,
  384. (void **)&ring->ring);
  385. amdgpu_bo_unreserve(ring->ring_obj);
  386. if (r) {
  387. dev_err(adev->dev, "(%d) ring map failed\n", r);
  388. return r;
  389. }
  390. }
  391. ring->ptr_mask = (ring->ring_size / 4) - 1;
  392. ring->ring_free_dw = ring->ring_size / 4;
  393. if (amdgpu_debugfs_ring_init(adev, ring)) {
  394. DRM_ERROR("Failed to register debugfs file for rings !\n");
  395. }
  396. amdgpu_ring_lockup_update(ring);
  397. return 0;
  398. }
  399. /**
  400. * amdgpu_ring_fini - tear down the driver ring struct.
  401. *
  402. * @adev: amdgpu_device pointer
  403. * @ring: amdgpu_ring structure holding ring information
  404. *
  405. * Tear down the driver information for the selected ring (all asics).
  406. */
  407. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  408. {
  409. int r;
  410. struct amdgpu_bo *ring_obj;
  411. if (ring->ring_lock == NULL)
  412. return;
  413. mutex_lock(ring->ring_lock);
  414. ring_obj = ring->ring_obj;
  415. ring->ready = false;
  416. ring->ring = NULL;
  417. ring->ring_obj = NULL;
  418. mutex_unlock(ring->ring_lock);
  419. amdgpu_wb_free(ring->adev, ring->fence_offs);
  420. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  421. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  422. amdgpu_wb_free(ring->adev, ring->next_rptr_offs);
  423. if (ring_obj) {
  424. r = amdgpu_bo_reserve(ring_obj, false);
  425. if (likely(r == 0)) {
  426. amdgpu_bo_kunmap(ring_obj);
  427. amdgpu_bo_unpin(ring_obj);
  428. amdgpu_bo_unreserve(ring_obj);
  429. }
  430. amdgpu_bo_unref(&ring_obj);
  431. }
  432. }
  433. /*
  434. * Debugfs info
  435. */
  436. #if defined(CONFIG_DEBUG_FS)
  437. static int amdgpu_debugfs_ring_info(struct seq_file *m, void *data)
  438. {
  439. struct drm_info_node *node = (struct drm_info_node *) m->private;
  440. struct drm_device *dev = node->minor->dev;
  441. struct amdgpu_device *adev = dev->dev_private;
  442. int roffset = *(int*)node->info_ent->data;
  443. struct amdgpu_ring *ring = (void *)(((uint8_t*)adev) + roffset);
  444. uint32_t rptr, wptr, rptr_next;
  445. unsigned count, i, j;
  446. amdgpu_ring_free_size(ring);
  447. count = (ring->ring_size / 4) - ring->ring_free_dw;
  448. wptr = amdgpu_ring_get_wptr(ring);
  449. seq_printf(m, "wptr: 0x%08x [%5d]\n",
  450. wptr, wptr);
  451. rptr = amdgpu_ring_get_rptr(ring);
  452. seq_printf(m, "rptr: 0x%08x [%5d]\n",
  453. rptr, rptr);
  454. rptr_next = ~0;
  455. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
  456. ring->wptr, ring->wptr);
  457. seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
  458. ring->last_semaphore_signal_addr);
  459. seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
  460. ring->last_semaphore_wait_addr);
  461. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  462. seq_printf(m, "%u dwords in ring\n", count);
  463. if (!ring->ready)
  464. return 0;
  465. /* print 8 dw before current rptr as often it's the last executed
  466. * packet that is the root issue
  467. */
  468. i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  469. for (j = 0; j <= (count + 32); j++) {
  470. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  471. if (rptr == i)
  472. seq_puts(m, " *");
  473. if (rptr_next == i)
  474. seq_puts(m, " #");
  475. seq_puts(m, "\n");
  476. i = (i + 1) & ring->ptr_mask;
  477. }
  478. return 0;
  479. }
  480. /* TODO: clean this up !*/
  481. static int amdgpu_gfx_index = offsetof(struct amdgpu_device, gfx.gfx_ring[0]);
  482. static int cayman_cp1_index = offsetof(struct amdgpu_device, gfx.compute_ring[0]);
  483. static int cayman_cp2_index = offsetof(struct amdgpu_device, gfx.compute_ring[1]);
  484. static int amdgpu_dma1_index = offsetof(struct amdgpu_device, sdma[0].ring);
  485. static int amdgpu_dma2_index = offsetof(struct amdgpu_device, sdma[1].ring);
  486. static int r600_uvd_index = offsetof(struct amdgpu_device, uvd.ring);
  487. static int si_vce1_index = offsetof(struct amdgpu_device, vce.ring[0]);
  488. static int si_vce2_index = offsetof(struct amdgpu_device, vce.ring[1]);
  489. static struct drm_info_list amdgpu_debugfs_ring_info_list[] = {
  490. {"amdgpu_ring_gfx", amdgpu_debugfs_ring_info, 0, &amdgpu_gfx_index},
  491. {"amdgpu_ring_cp1", amdgpu_debugfs_ring_info, 0, &cayman_cp1_index},
  492. {"amdgpu_ring_cp2", amdgpu_debugfs_ring_info, 0, &cayman_cp2_index},
  493. {"amdgpu_ring_dma1", amdgpu_debugfs_ring_info, 0, &amdgpu_dma1_index},
  494. {"amdgpu_ring_dma2", amdgpu_debugfs_ring_info, 0, &amdgpu_dma2_index},
  495. {"amdgpu_ring_uvd", amdgpu_debugfs_ring_info, 0, &r600_uvd_index},
  496. {"amdgpu_ring_vce1", amdgpu_debugfs_ring_info, 0, &si_vce1_index},
  497. {"amdgpu_ring_vce2", amdgpu_debugfs_ring_info, 0, &si_vce2_index},
  498. };
  499. #endif
  500. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
  501. {
  502. #if defined(CONFIG_DEBUG_FS)
  503. unsigned i;
  504. for (i = 0; i < ARRAY_SIZE(amdgpu_debugfs_ring_info_list); ++i) {
  505. struct drm_info_list *info = &amdgpu_debugfs_ring_info_list[i];
  506. int roffset = *(int*)amdgpu_debugfs_ring_info_list[i].data;
  507. struct amdgpu_ring *other = (void *)(((uint8_t*)adev) + roffset);
  508. unsigned r;
  509. if (other != ring)
  510. continue;
  511. r = amdgpu_debugfs_add_files(adev, info, 1);
  512. if (r)
  513. return r;
  514. }
  515. #endif
  516. return 0;
  517. }