amdgpu_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. amdgpu_ring_lru_touch(ring->adev, ring);
  126. }
  127. /**
  128. * amdgpu_ring_undo - reset the wptr
  129. *
  130. * @ring: amdgpu_ring structure holding ring information
  131. *
  132. * Reset the driver's copy of the wptr (all asics).
  133. */
  134. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  135. {
  136. ring->wptr = ring->wptr_old;
  137. if (ring->funcs->end_use)
  138. ring->funcs->end_use(ring);
  139. }
  140. /**
  141. * amdgpu_ring_init - init driver ring struct.
  142. *
  143. * @adev: amdgpu_device pointer
  144. * @ring: amdgpu_ring structure holding ring information
  145. * @max_ndw: maximum number of dw for ring alloc
  146. * @nop: nop packet for this ring
  147. *
  148. * Initialize the driver information for the selected ring (all asics).
  149. * Returns 0 on success, error on failure.
  150. */
  151. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  152. unsigned max_dw, struct amdgpu_irq_src *irq_src,
  153. unsigned irq_type)
  154. {
  155. int r;
  156. if (ring->adev == NULL) {
  157. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  158. return -EINVAL;
  159. ring->adev = adev;
  160. ring->idx = adev->num_rings++;
  161. adev->rings[ring->idx] = ring;
  162. r = amdgpu_fence_driver_init_ring(ring,
  163. amdgpu_sched_hw_submission);
  164. if (r)
  165. return r;
  166. }
  167. if (ring->funcs->support_64bit_ptrs) {
  168. r = amdgpu_wb_get_64bit(adev, &ring->rptr_offs);
  169. if (r) {
  170. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  171. return r;
  172. }
  173. r = amdgpu_wb_get_64bit(adev, &ring->wptr_offs);
  174. if (r) {
  175. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  176. return r;
  177. }
  178. } else {
  179. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  180. if (r) {
  181. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  182. return r;
  183. }
  184. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  185. if (r) {
  186. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  187. return r;
  188. }
  189. }
  190. r = amdgpu_wb_get(adev, &ring->fence_offs);
  191. if (r) {
  192. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  193. return r;
  194. }
  195. r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
  196. if (r) {
  197. dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
  198. return r;
  199. }
  200. ring->cond_exe_gpu_addr = adev->wb.gpu_addr + (ring->cond_exe_offs * 4);
  201. ring->cond_exe_cpu_addr = &adev->wb.wb[ring->cond_exe_offs];
  202. /* always set cond_exec_polling to CONTINUE */
  203. *ring->cond_exe_cpu_addr = 1;
  204. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  205. if (r) {
  206. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  207. return r;
  208. }
  209. ring->ring_size = roundup_pow_of_two(max_dw * 4 *
  210. amdgpu_sched_hw_submission);
  211. ring->buf_mask = (ring->ring_size / 4) - 1;
  212. ring->ptr_mask = ring->funcs->support_64bit_ptrs ?
  213. 0xffffffffffffffff : ring->buf_mask;
  214. /* Allocate ring buffer */
  215. if (ring->ring_obj == NULL) {
  216. r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
  217. AMDGPU_GEM_DOMAIN_GTT,
  218. &ring->ring_obj,
  219. &ring->gpu_addr,
  220. (void **)&ring->ring);
  221. if (r) {
  222. dev_err(adev->dev, "(%d) ring create failed\n", r);
  223. return r;
  224. }
  225. amdgpu_ring_clear_ring(ring);
  226. }
  227. ring->max_dw = max_dw;
  228. INIT_LIST_HEAD(&ring->lru_list);
  229. amdgpu_ring_lru_touch(adev, ring);
  230. if (amdgpu_debugfs_ring_init(adev, ring)) {
  231. DRM_ERROR("Failed to register debugfs file for rings !\n");
  232. }
  233. return 0;
  234. }
  235. /**
  236. * amdgpu_ring_fini - tear down the driver ring struct.
  237. *
  238. * @adev: amdgpu_device pointer
  239. * @ring: amdgpu_ring structure holding ring information
  240. *
  241. * Tear down the driver information for the selected ring (all asics).
  242. */
  243. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  244. {
  245. ring->ready = false;
  246. if (ring->funcs->support_64bit_ptrs) {
  247. amdgpu_wb_free_64bit(ring->adev, ring->cond_exe_offs);
  248. amdgpu_wb_free_64bit(ring->adev, ring->fence_offs);
  249. amdgpu_wb_free_64bit(ring->adev, ring->rptr_offs);
  250. amdgpu_wb_free_64bit(ring->adev, ring->wptr_offs);
  251. } else {
  252. amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
  253. amdgpu_wb_free(ring->adev, ring->fence_offs);
  254. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  255. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  256. }
  257. amdgpu_bo_free_kernel(&ring->ring_obj,
  258. &ring->gpu_addr,
  259. (void **)&ring->ring);
  260. amdgpu_debugfs_ring_fini(ring);
  261. ring->adev->rings[ring->idx] = NULL;
  262. }
  263. static void amdgpu_ring_lru_touch_locked(struct amdgpu_device *adev,
  264. struct amdgpu_ring *ring)
  265. {
  266. /* list_move_tail handles the case where ring isn't part of the list */
  267. list_move_tail(&ring->lru_list, &adev->ring_lru_list);
  268. }
  269. static bool amdgpu_ring_is_blacklisted(struct amdgpu_ring *ring,
  270. int *blacklist, int num_blacklist)
  271. {
  272. int i;
  273. for (i = 0; i < num_blacklist; i++) {
  274. if (ring->idx == blacklist[i])
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * amdgpu_ring_lru_get - get the least recently used ring for a HW IP block
  281. *
  282. * @adev: amdgpu_device pointer
  283. * @type: amdgpu_ring_type enum
  284. * @blacklist: blacklisted ring ids array
  285. * @num_blacklist: number of entries in @blacklist
  286. * @ring: output ring
  287. *
  288. * Retrieve the amdgpu_ring structure for the least recently used ring of
  289. * a specific IP block (all asics).
  290. * Returns 0 on success, error on failure.
  291. */
  292. int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type, int *blacklist,
  293. int num_blacklist, struct amdgpu_ring **ring)
  294. {
  295. struct amdgpu_ring *entry;
  296. /* List is sorted in LRU order, find first entry corresponding
  297. * to the desired HW IP */
  298. *ring = NULL;
  299. spin_lock(&adev->ring_lru_list_lock);
  300. list_for_each_entry(entry, &adev->ring_lru_list, lru_list) {
  301. if (entry->funcs->type != type)
  302. continue;
  303. if (amdgpu_ring_is_blacklisted(entry, blacklist, num_blacklist))
  304. continue;
  305. *ring = entry;
  306. amdgpu_ring_lru_touch_locked(adev, *ring);
  307. break;
  308. }
  309. spin_unlock(&adev->ring_lru_list_lock);
  310. if (!*ring) {
  311. DRM_ERROR("Ring LRU contains no entries for ring type:%d\n", type);
  312. return -EINVAL;
  313. }
  314. return 0;
  315. }
  316. /**
  317. * amdgpu_ring_lru_touch - mark a ring as recently being used
  318. *
  319. * @adev: amdgpu_device pointer
  320. * @ring: ring to touch
  321. *
  322. * Move @ring to the tail of the lru list
  323. */
  324. void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring)
  325. {
  326. spin_lock(&adev->ring_lru_list_lock);
  327. amdgpu_ring_lru_touch_locked(adev, ring);
  328. spin_unlock(&adev->ring_lru_list_lock);
  329. }
  330. /*
  331. * Debugfs info
  332. */
  333. #if defined(CONFIG_DEBUG_FS)
  334. /* Layout of file is 12 bytes consisting of
  335. * - rptr
  336. * - wptr
  337. * - driver's copy of wptr
  338. *
  339. * followed by n-words of ring data
  340. */
  341. static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf,
  342. size_t size, loff_t *pos)
  343. {
  344. struct amdgpu_ring *ring = file_inode(f)->i_private;
  345. int r, i;
  346. uint32_t value, result, early[3];
  347. if (*pos & 3 || size & 3)
  348. return -EINVAL;
  349. result = 0;
  350. if (*pos < 12) {
  351. early[0] = amdgpu_ring_get_rptr(ring);
  352. early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask;
  353. early[2] = ring->wptr & ring->buf_mask;
  354. for (i = *pos / 4; i < 3 && size; i++) {
  355. r = put_user(early[i], (uint32_t *)buf);
  356. if (r)
  357. return r;
  358. buf += 4;
  359. result += 4;
  360. size -= 4;
  361. *pos += 4;
  362. }
  363. }
  364. while (size) {
  365. if (*pos >= (ring->ring_size + 12))
  366. return result;
  367. value = ring->ring[(*pos - 12)/4];
  368. r = put_user(value, (uint32_t*)buf);
  369. if (r)
  370. return r;
  371. buf += 4;
  372. result += 4;
  373. size -= 4;
  374. *pos += 4;
  375. }
  376. return result;
  377. }
  378. static const struct file_operations amdgpu_debugfs_ring_fops = {
  379. .owner = THIS_MODULE,
  380. .read = amdgpu_debugfs_ring_read,
  381. .llseek = default_llseek
  382. };
  383. #endif
  384. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  385. struct amdgpu_ring *ring)
  386. {
  387. #if defined(CONFIG_DEBUG_FS)
  388. struct drm_minor *minor = adev->ddev->primary;
  389. struct dentry *ent, *root = minor->debugfs_root;
  390. char name[32];
  391. sprintf(name, "amdgpu_ring_%s", ring->name);
  392. ent = debugfs_create_file(name,
  393. S_IFREG | S_IRUGO, root,
  394. ring, &amdgpu_debugfs_ring_fops);
  395. if (!ent)
  396. return -ENOMEM;
  397. i_size_write(ent->d_inode, ring->ring_size + 12);
  398. ring->ent = ent;
  399. #endif
  400. return 0;
  401. }
  402. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring)
  403. {
  404. #if defined(CONFIG_DEBUG_FS)
  405. debugfs_remove(ring->ent);
  406. #endif
  407. }