amdgpu_ring.c 15 KB

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