amdgpu_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 <linux/debugfs.h>
  32. #include <drm/drmP.h>
  33. #include <drm/amdgpu_drm.h>
  34. #include "amdgpu.h"
  35. #include "atom.h"
  36. /*
  37. * Rings
  38. * Most engines on the GPU are fed via ring buffers. Ring
  39. * buffers are areas of GPU accessible memory that the host
  40. * writes commands into and the GPU reads commands out of.
  41. * There is a rptr (read pointer) that determines where the
  42. * GPU is currently reading, and a wptr (write pointer)
  43. * which determines where the host has written. When the
  44. * pointers are equal, the ring is idle. When the host
  45. * writes commands to the ring buffer, it increments the
  46. * wptr. The GPU then starts fetching commands and executes
  47. * them until the pointers are equal again.
  48. */
  49. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  50. struct amdgpu_ring *ring);
  51. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring);
  52. /**
  53. * amdgpu_ring_alloc - allocate space on the ring buffer
  54. *
  55. * @adev: amdgpu_device pointer
  56. * @ring: amdgpu_ring structure holding ring information
  57. * @ndw: number of dwords to allocate in the ring buffer
  58. *
  59. * Allocate @ndw dwords in the ring buffer (all asics).
  60. * Returns 0 on success, error on failure.
  61. */
  62. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
  63. {
  64. /* Align requested size with padding so unlock_commit can
  65. * pad safely */
  66. ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask;
  67. /* Make sure we aren't trying to allocate more space
  68. * than the maximum for one submission
  69. */
  70. if (WARN_ON_ONCE(ndw > ring->max_dw))
  71. return -ENOMEM;
  72. ring->count_dw = ndw;
  73. ring->wptr_old = ring->wptr;
  74. if (ring->funcs->begin_use)
  75. ring->funcs->begin_use(ring);
  76. return 0;
  77. }
  78. /** amdgpu_ring_insert_nop - insert NOP packets
  79. *
  80. * @ring: amdgpu_ring structure holding ring information
  81. * @count: the number of NOP packets to insert
  82. *
  83. * This is the generic insert_nop function for rings except SDMA
  84. */
  85. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  86. {
  87. int i;
  88. for (i = 0; i < count; i++)
  89. amdgpu_ring_write(ring, ring->funcs->nop);
  90. }
  91. /** amdgpu_ring_generic_pad_ib - pad IB with NOP packets
  92. *
  93. * @ring: amdgpu_ring structure holding ring information
  94. * @ib: IB to add NOP packets to
  95. *
  96. * This is the generic pad_ib function for rings except SDMA
  97. */
  98. void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  99. {
  100. while (ib->length_dw & ring->funcs->align_mask)
  101. ib->ptr[ib->length_dw++] = ring->funcs->nop;
  102. }
  103. /**
  104. * amdgpu_ring_commit - tell the GPU to execute the new
  105. * commands on the ring buffer
  106. *
  107. * @adev: amdgpu_device pointer
  108. * @ring: amdgpu_ring structure holding ring information
  109. *
  110. * Update the wptr (write pointer) to tell the GPU to
  111. * execute new commands on the ring buffer (all asics).
  112. */
  113. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  114. {
  115. uint32_t count;
  116. /* We pad to match fetch size */
  117. count = ring->funcs->align_mask + 1 -
  118. (ring->wptr & ring->funcs->align_mask);
  119. count %= ring->funcs->align_mask + 1;
  120. ring->funcs->insert_nop(ring, count);
  121. mb();
  122. amdgpu_ring_set_wptr(ring);
  123. if (ring->funcs->end_use)
  124. ring->funcs->end_use(ring);
  125. }
  126. /**
  127. * amdgpu_ring_undo - reset the wptr
  128. *
  129. * @ring: amdgpu_ring structure holding ring information
  130. *
  131. * Reset the driver's copy of the wptr (all asics).
  132. */
  133. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  134. {
  135. ring->wptr = ring->wptr_old;
  136. if (ring->funcs->end_use)
  137. ring->funcs->end_use(ring);
  138. }
  139. /**
  140. * amdgpu_ring_priority_put - restore a ring's priority
  141. *
  142. * @ring: amdgpu_ring structure holding the information
  143. * @priority: target priority
  144. *
  145. * Release a request for executing at @priority
  146. */
  147. void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
  148. enum drm_sched_priority priority)
  149. {
  150. int i;
  151. if (!ring->funcs->set_priority)
  152. return;
  153. if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
  154. return;
  155. /* no need to restore if the job is already at the lowest priority */
  156. if (priority == DRM_SCHED_PRIORITY_NORMAL)
  157. return;
  158. mutex_lock(&ring->priority_mutex);
  159. /* something higher prio is executing, no need to decay */
  160. if (ring->priority > priority)
  161. goto out_unlock;
  162. /* decay priority to the next level with a job available */
  163. for (i = priority; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  164. if (i == DRM_SCHED_PRIORITY_NORMAL
  165. || atomic_read(&ring->num_jobs[i])) {
  166. ring->priority = i;
  167. ring->funcs->set_priority(ring, i);
  168. break;
  169. }
  170. }
  171. out_unlock:
  172. mutex_unlock(&ring->priority_mutex);
  173. }
  174. /**
  175. * amdgpu_ring_priority_get - change the ring's priority
  176. *
  177. * @ring: amdgpu_ring structure holding the information
  178. * @priority: target priority
  179. *
  180. * Request a ring's priority to be raised to @priority (refcounted).
  181. */
  182. void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
  183. enum drm_sched_priority priority)
  184. {
  185. if (!ring->funcs->set_priority)
  186. return;
  187. if (atomic_inc_return(&ring->num_jobs[priority]) <= 0)
  188. return;
  189. mutex_lock(&ring->priority_mutex);
  190. if (priority <= ring->priority)
  191. goto out_unlock;
  192. ring->priority = priority;
  193. ring->funcs->set_priority(ring, priority);
  194. out_unlock:
  195. mutex_unlock(&ring->priority_mutex);
  196. }
  197. /**
  198. * amdgpu_ring_init - init driver ring struct.
  199. *
  200. * @adev: amdgpu_device pointer
  201. * @ring: amdgpu_ring structure holding ring information
  202. * @max_ndw: maximum number of dw for ring alloc
  203. * @nop: nop packet for this ring
  204. *
  205. * Initialize the driver information for the selected ring (all asics).
  206. * Returns 0 on success, error on failure.
  207. */
  208. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  209. unsigned max_dw, struct amdgpu_irq_src *irq_src,
  210. unsigned irq_type)
  211. {
  212. int r, i;
  213. int sched_hw_submission = amdgpu_sched_hw_submission;
  214. /* Set the hw submission limit higher for KIQ because
  215. * it's used for a number of gfx/compute tasks by both
  216. * KFD and KGD which may have outstanding fences and
  217. * it doesn't really use the gpu scheduler anyway;
  218. * KIQ tasks get submitted directly to the ring.
  219. */
  220. if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
  221. sched_hw_submission = max(sched_hw_submission, 256);
  222. if (ring->adev == NULL) {
  223. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  224. return -EINVAL;
  225. ring->adev = adev;
  226. ring->idx = adev->num_rings++;
  227. adev->rings[ring->idx] = ring;
  228. r = amdgpu_fence_driver_init_ring(ring, sched_hw_submission);
  229. if (r)
  230. return r;
  231. }
  232. r = amdgpu_device_wb_get(adev, &ring->rptr_offs);
  233. if (r) {
  234. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  235. return r;
  236. }
  237. r = amdgpu_device_wb_get(adev, &ring->wptr_offs);
  238. if (r) {
  239. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  240. return r;
  241. }
  242. r = amdgpu_device_wb_get(adev, &ring->fence_offs);
  243. if (r) {
  244. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  245. return r;
  246. }
  247. r = amdgpu_device_wb_get(adev, &ring->cond_exe_offs);
  248. if (r) {
  249. dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
  250. return r;
  251. }
  252. ring->cond_exe_gpu_addr = adev->wb.gpu_addr + (ring->cond_exe_offs * 4);
  253. ring->cond_exe_cpu_addr = &adev->wb.wb[ring->cond_exe_offs];
  254. /* always set cond_exec_polling to CONTINUE */
  255. *ring->cond_exe_cpu_addr = 1;
  256. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  257. if (r) {
  258. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  259. return r;
  260. }
  261. ring->ring_size = roundup_pow_of_two(max_dw * 4 * sched_hw_submission);
  262. ring->buf_mask = (ring->ring_size / 4) - 1;
  263. ring->ptr_mask = ring->funcs->support_64bit_ptrs ?
  264. 0xffffffffffffffff : ring->buf_mask;
  265. /* Allocate ring buffer */
  266. if (ring->ring_obj == NULL) {
  267. r = amdgpu_bo_create_kernel(adev, ring->ring_size + ring->funcs->extra_dw, PAGE_SIZE,
  268. AMDGPU_GEM_DOMAIN_GTT,
  269. &ring->ring_obj,
  270. &ring->gpu_addr,
  271. (void **)&ring->ring);
  272. if (r) {
  273. dev_err(adev->dev, "(%d) ring create failed\n", r);
  274. return r;
  275. }
  276. amdgpu_ring_clear_ring(ring);
  277. }
  278. ring->max_dw = max_dw;
  279. ring->priority = DRM_SCHED_PRIORITY_NORMAL;
  280. mutex_init(&ring->priority_mutex);
  281. for (i = 0; i < DRM_SCHED_PRIORITY_MAX; ++i)
  282. atomic_set(&ring->num_jobs[i], 0);
  283. if (amdgpu_debugfs_ring_init(adev, ring)) {
  284. DRM_ERROR("Failed to register debugfs file for rings !\n");
  285. }
  286. return 0;
  287. }
  288. /**
  289. * amdgpu_ring_fini - tear down the driver ring struct.
  290. *
  291. * @adev: amdgpu_device pointer
  292. * @ring: amdgpu_ring structure holding ring information
  293. *
  294. * Tear down the driver information for the selected ring (all asics).
  295. */
  296. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  297. {
  298. ring->ready = false;
  299. /* Not to finish a ring which is not initialized */
  300. if (!(ring->adev) || !(ring->adev->rings[ring->idx]))
  301. return;
  302. amdgpu_device_wb_free(ring->adev, ring->rptr_offs);
  303. amdgpu_device_wb_free(ring->adev, ring->wptr_offs);
  304. amdgpu_device_wb_free(ring->adev, ring->cond_exe_offs);
  305. amdgpu_device_wb_free(ring->adev, ring->fence_offs);
  306. amdgpu_bo_free_kernel(&ring->ring_obj,
  307. &ring->gpu_addr,
  308. (void **)&ring->ring);
  309. amdgpu_debugfs_ring_fini(ring);
  310. dma_fence_put(ring->vmid_wait);
  311. ring->vmid_wait = NULL;
  312. ring->me = 0;
  313. ring->adev->rings[ring->idx] = NULL;
  314. }
  315. /**
  316. * amdgpu_ring_emit_reg_write_reg_wait_helper - ring helper
  317. *
  318. * @adev: amdgpu_device pointer
  319. * @reg0: register to write
  320. * @reg1: register to wait on
  321. * @ref: reference value to write/wait on
  322. * @mask: mask to wait on
  323. *
  324. * Helper for rings that don't support write and wait in a
  325. * single oneshot packet.
  326. */
  327. void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
  328. uint32_t reg0, uint32_t reg1,
  329. uint32_t ref, uint32_t mask)
  330. {
  331. amdgpu_ring_emit_wreg(ring, reg0, ref);
  332. amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask);
  333. }
  334. /**
  335. * amdgpu_ring_soft_recovery - try to soft recover a ring lockup
  336. *
  337. * @ring: ring to try the recovery on
  338. * @vmid: VMID we try to get going again
  339. * @fence: timedout fence
  340. *
  341. * Tries to get a ring proceeding again when it is stuck.
  342. */
  343. bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid,
  344. struct dma_fence *fence)
  345. {
  346. ktime_t deadline = ktime_add_us(ktime_get(), 10000);
  347. if (!ring->funcs->soft_recovery)
  348. return false;
  349. atomic_inc(&ring->adev->gpu_reset_counter);
  350. while (!dma_fence_is_signaled(fence) &&
  351. ktime_to_ns(ktime_sub(deadline, ktime_get())) > 0)
  352. ring->funcs->soft_recovery(ring, vmid);
  353. return dma_fence_is_signaled(fence);
  354. }
  355. /*
  356. * Debugfs info
  357. */
  358. #if defined(CONFIG_DEBUG_FS)
  359. /* Layout of file is 12 bytes consisting of
  360. * - rptr
  361. * - wptr
  362. * - driver's copy of wptr
  363. *
  364. * followed by n-words of ring data
  365. */
  366. static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf,
  367. size_t size, loff_t *pos)
  368. {
  369. struct amdgpu_ring *ring = file_inode(f)->i_private;
  370. int r, i;
  371. uint32_t value, result, early[3];
  372. if (*pos & 3 || size & 3)
  373. return -EINVAL;
  374. result = 0;
  375. if (*pos < 12) {
  376. early[0] = amdgpu_ring_get_rptr(ring) & ring->buf_mask;
  377. early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask;
  378. early[2] = ring->wptr & ring->buf_mask;
  379. for (i = *pos / 4; i < 3 && size; i++) {
  380. r = put_user(early[i], (uint32_t *)buf);
  381. if (r)
  382. return r;
  383. buf += 4;
  384. result += 4;
  385. size -= 4;
  386. *pos += 4;
  387. }
  388. }
  389. while (size) {
  390. if (*pos >= (ring->ring_size + 12))
  391. return result;
  392. value = ring->ring[(*pos - 12)/4];
  393. r = put_user(value, (uint32_t*)buf);
  394. if (r)
  395. return r;
  396. buf += 4;
  397. result += 4;
  398. size -= 4;
  399. *pos += 4;
  400. }
  401. return result;
  402. }
  403. static const struct file_operations amdgpu_debugfs_ring_fops = {
  404. .owner = THIS_MODULE,
  405. .read = amdgpu_debugfs_ring_read,
  406. .llseek = default_llseek
  407. };
  408. #endif
  409. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  410. struct amdgpu_ring *ring)
  411. {
  412. #if defined(CONFIG_DEBUG_FS)
  413. struct drm_minor *minor = adev->ddev->primary;
  414. struct dentry *ent, *root = minor->debugfs_root;
  415. char name[32];
  416. sprintf(name, "amdgpu_ring_%s", ring->name);
  417. ent = debugfs_create_file(name,
  418. S_IFREG | S_IRUGO, root,
  419. ring, &amdgpu_debugfs_ring_fops);
  420. if (!ent)
  421. return -ENOMEM;
  422. i_size_write(ent->d_inode, ring->ring_size + 12);
  423. ring->ent = ent;
  424. #endif
  425. return 0;
  426. }
  427. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring)
  428. {
  429. #if defined(CONFIG_DEBUG_FS)
  430. debugfs_remove(ring->ent);
  431. #endif
  432. }