amdgpu_fence.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Dave Airlie
  30. */
  31. #include <linux/seq_file.h>
  32. #include <linux/atomic.h>
  33. #include <linux/wait.h>
  34. #include <linux/kref.h>
  35. #include <linux/slab.h>
  36. #include <linux/firmware.h>
  37. #include <drm/drmP.h>
  38. #include "amdgpu.h"
  39. #include "amdgpu_trace.h"
  40. /*
  41. * Fences
  42. * Fences mark an event in the GPUs pipeline and are used
  43. * for GPU/CPU synchronization. When the fence is written,
  44. * it is expected that all buffers associated with that fence
  45. * are no longer in use by the associated ring on the GPU and
  46. * that the the relevant GPU caches have been flushed.
  47. */
  48. struct amdgpu_fence {
  49. struct fence base;
  50. /* RB, DMA, etc. */
  51. struct amdgpu_ring *ring;
  52. uint64_t seq;
  53. wait_queue_t fence_wake;
  54. };
  55. static struct kmem_cache *amdgpu_fence_slab;
  56. static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
  57. /*
  58. * Cast helper
  59. */
  60. static const struct fence_ops amdgpu_fence_ops;
  61. static inline struct amdgpu_fence *to_amdgpu_fence(struct fence *f)
  62. {
  63. struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
  64. if (__f->base.ops == &amdgpu_fence_ops)
  65. return __f;
  66. return NULL;
  67. }
  68. /**
  69. * amdgpu_fence_write - write a fence value
  70. *
  71. * @ring: ring the fence is associated with
  72. * @seq: sequence number to write
  73. *
  74. * Writes a fence value to memory (all asics).
  75. */
  76. static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
  77. {
  78. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  79. if (drv->cpu_addr)
  80. *drv->cpu_addr = cpu_to_le32(seq);
  81. }
  82. /**
  83. * amdgpu_fence_read - read a fence value
  84. *
  85. * @ring: ring the fence is associated with
  86. *
  87. * Reads a fence value from memory (all asics).
  88. * Returns the value of the fence read from memory.
  89. */
  90. static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
  91. {
  92. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  93. u32 seq = 0;
  94. if (drv->cpu_addr)
  95. seq = le32_to_cpu(*drv->cpu_addr);
  96. else
  97. seq = lower_32_bits(atomic64_read(&drv->last_seq));
  98. return seq;
  99. }
  100. /**
  101. * amdgpu_fence_emit - emit a fence on the requested ring
  102. *
  103. * @ring: ring the fence is associated with
  104. * @f: resulting fence object
  105. *
  106. * Emits a fence command on the requested ring (all asics).
  107. * Returns 0 on success, -ENOMEM on failure.
  108. */
  109. int amdgpu_fence_emit(struct amdgpu_ring *ring, struct fence **f)
  110. {
  111. struct amdgpu_device *adev = ring->adev;
  112. struct amdgpu_fence *fence;
  113. fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
  114. if (fence == NULL)
  115. return -ENOMEM;
  116. fence->seq = ++ring->fence_drv.sync_seq;
  117. fence->ring = ring;
  118. fence_init(&fence->base, &amdgpu_fence_ops,
  119. &ring->fence_drv.fence_queue.lock,
  120. adev->fence_context + ring->idx,
  121. fence->seq);
  122. amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
  123. fence->seq, AMDGPU_FENCE_FLAG_INT);
  124. *f = &fence->base;
  125. return 0;
  126. }
  127. /**
  128. * amdgpu_fence_schedule_fallback - schedule fallback check
  129. *
  130. * @ring: pointer to struct amdgpu_ring
  131. *
  132. * Start a timer as fallback to our interrupts.
  133. */
  134. static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
  135. {
  136. mod_timer(&ring->fence_drv.fallback_timer,
  137. jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
  138. }
  139. /**
  140. * amdgpu_fence_activity - check for fence activity
  141. *
  142. * @ring: pointer to struct amdgpu_ring
  143. *
  144. * Checks the current fence value and calculates the last
  145. * signalled fence value. Returns true if activity occured
  146. * on the ring, and the fence_queue should be waken up.
  147. */
  148. static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
  149. {
  150. uint64_t seq, last_seq, last_emitted;
  151. bool wake = false;
  152. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  153. do {
  154. last_emitted = ring->fence_drv.sync_seq;
  155. seq = amdgpu_fence_read(ring);
  156. seq |= last_seq & 0xffffffff00000000LL;
  157. if (seq < last_seq) {
  158. seq &= 0xffffffff;
  159. seq |= last_emitted & 0xffffffff00000000LL;
  160. }
  161. if (seq <= last_seq || seq > last_emitted)
  162. break;
  163. /* If we loop over we don't want to return without
  164. * checking if a fence is signaled as it means that the
  165. * seq we just read is different from the previous on.
  166. */
  167. wake = true;
  168. last_seq = seq;
  169. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  170. if (seq < last_emitted)
  171. amdgpu_fence_schedule_fallback(ring);
  172. return wake;
  173. }
  174. /**
  175. * amdgpu_fence_process - process a fence
  176. *
  177. * @adev: amdgpu_device pointer
  178. * @ring: ring index the fence is associated with
  179. *
  180. * Checks the current fence value and wakes the fence queue
  181. * if the sequence number has increased (all asics).
  182. */
  183. void amdgpu_fence_process(struct amdgpu_ring *ring)
  184. {
  185. if (amdgpu_fence_activity(ring))
  186. wake_up_all(&ring->fence_drv.fence_queue);
  187. }
  188. /**
  189. * amdgpu_fence_fallback - fallback for hardware interrupts
  190. *
  191. * @work: delayed work item
  192. *
  193. * Checks for fence activity.
  194. */
  195. static void amdgpu_fence_fallback(unsigned long arg)
  196. {
  197. struct amdgpu_ring *ring = (void *)arg;
  198. amdgpu_fence_process(ring);
  199. }
  200. /**
  201. * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
  202. *
  203. * @ring: ring the fence is associated with
  204. * @seq: sequence number
  205. *
  206. * Check if the last signaled fence sequnce number is >= the requested
  207. * sequence number (all asics).
  208. * Returns true if the fence has signaled (current fence value
  209. * is >= requested value) or false if it has not (current fence
  210. * value is < the requested value. Helper function for
  211. * amdgpu_fence_signaled().
  212. */
  213. static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
  214. {
  215. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  216. return true;
  217. /* poll new last sequence at least once */
  218. amdgpu_fence_process(ring);
  219. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  220. return true;
  221. return false;
  222. }
  223. /*
  224. * amdgpu_ring_wait_seq - wait for seq of the specific ring to signal
  225. * @ring: ring to wait on for the seq number
  226. * @seq: seq number wait for
  227. *
  228. * return value:
  229. * 0: seq signaled, and gpu not hang
  230. * -EINVAL: some paramter is not valid
  231. */
  232. static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
  233. {
  234. BUG_ON(!ring);
  235. if (seq > ring->fence_drv.sync_seq)
  236. return -EINVAL;
  237. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  238. return 0;
  239. amdgpu_fence_schedule_fallback(ring);
  240. wait_event(ring->fence_drv.fence_queue,
  241. amdgpu_fence_seq_signaled(ring, seq));
  242. return 0;
  243. }
  244. /**
  245. * amdgpu_fence_wait_empty - wait for all fences to signal
  246. *
  247. * @adev: amdgpu device pointer
  248. * @ring: ring index the fence is associated with
  249. *
  250. * Wait for all fences on the requested ring to signal (all asics).
  251. * Returns 0 if the fences have passed, error for all other cases.
  252. * Caller must hold ring lock.
  253. */
  254. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  255. {
  256. uint64_t seq = ring->fence_drv.sync_seq;
  257. if (!seq)
  258. return 0;
  259. return amdgpu_fence_ring_wait_seq(ring, seq);
  260. }
  261. /**
  262. * amdgpu_fence_count_emitted - get the count of emitted fences
  263. *
  264. * @ring: ring the fence is associated with
  265. *
  266. * Get the number of fences emitted on the requested ring (all asics).
  267. * Returns the number of emitted fences on the ring. Used by the
  268. * dynpm code to ring track activity.
  269. */
  270. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  271. {
  272. uint64_t emitted;
  273. /* We are not protected by ring lock when reading the last sequence
  274. * but it's ok to report slightly wrong fence count here.
  275. */
  276. amdgpu_fence_process(ring);
  277. emitted = ring->fence_drv.sync_seq
  278. - atomic64_read(&ring->fence_drv.last_seq);
  279. /* to avoid 32bits warp around */
  280. if (emitted > 0x10000000)
  281. emitted = 0x10000000;
  282. return (unsigned)emitted;
  283. }
  284. /**
  285. * amdgpu_fence_driver_start_ring - make the fence driver
  286. * ready for use on the requested ring.
  287. *
  288. * @ring: ring to start the fence driver on
  289. * @irq_src: interrupt source to use for this ring
  290. * @irq_type: interrupt type to use for this ring
  291. *
  292. * Make the fence driver ready for processing (all asics).
  293. * Not all asics have all rings, so each asic will only
  294. * start the fence driver on the rings it has.
  295. * Returns 0 for success, errors for failure.
  296. */
  297. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  298. struct amdgpu_irq_src *irq_src,
  299. unsigned irq_type)
  300. {
  301. struct amdgpu_device *adev = ring->adev;
  302. uint64_t index;
  303. if (ring != &adev->uvd.ring) {
  304. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  305. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  306. } else {
  307. /* put fence directly behind firmware */
  308. index = ALIGN(adev->uvd.fw->size, 8);
  309. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  310. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  311. }
  312. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  313. amdgpu_irq_get(adev, irq_src, irq_type);
  314. ring->fence_drv.irq_src = irq_src;
  315. ring->fence_drv.irq_type = irq_type;
  316. ring->fence_drv.initialized = true;
  317. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  318. "cpu addr 0x%p\n", ring->idx,
  319. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  320. return 0;
  321. }
  322. /**
  323. * amdgpu_fence_driver_init_ring - init the fence driver
  324. * for the requested ring.
  325. *
  326. * @ring: ring to init the fence driver on
  327. *
  328. * Init the fence driver for the requested ring (all asics).
  329. * Helper function for amdgpu_fence_driver_init().
  330. */
  331. int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  332. {
  333. long timeout;
  334. int r;
  335. ring->fence_drv.cpu_addr = NULL;
  336. ring->fence_drv.gpu_addr = 0;
  337. ring->fence_drv.sync_seq = 0;
  338. atomic64_set(&ring->fence_drv.last_seq, 0);
  339. ring->fence_drv.initialized = false;
  340. setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
  341. (unsigned long)ring);
  342. init_waitqueue_head(&ring->fence_drv.fence_queue);
  343. timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
  344. if (timeout == 0) {
  345. /*
  346. * FIXME:
  347. * Delayed workqueue cannot use it directly,
  348. * so the scheduler will not use delayed workqueue if
  349. * MAX_SCHEDULE_TIMEOUT is set.
  350. * Currently keep it simple and silly.
  351. */
  352. timeout = MAX_SCHEDULE_TIMEOUT;
  353. }
  354. r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
  355. amdgpu_sched_hw_submission,
  356. timeout, ring->name);
  357. if (r) {
  358. DRM_ERROR("Failed to create scheduler on ring %s.\n",
  359. ring->name);
  360. return r;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * amdgpu_fence_driver_init - init the fence driver
  366. * for all possible rings.
  367. *
  368. * @adev: amdgpu device pointer
  369. *
  370. * Init the fence driver for all possible rings (all asics).
  371. * Not all asics have all rings, so each asic will only
  372. * start the fence driver on the rings it has using
  373. * amdgpu_fence_driver_start_ring().
  374. * Returns 0 for success.
  375. */
  376. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  377. {
  378. if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
  379. amdgpu_fence_slab = kmem_cache_create(
  380. "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
  381. SLAB_HWCACHE_ALIGN, NULL);
  382. if (!amdgpu_fence_slab)
  383. return -ENOMEM;
  384. }
  385. if (amdgpu_debugfs_fence_init(adev))
  386. dev_err(adev->dev, "fence debugfs file creation failed\n");
  387. return 0;
  388. }
  389. /**
  390. * amdgpu_fence_driver_fini - tear down the fence driver
  391. * for all possible rings.
  392. *
  393. * @adev: amdgpu device pointer
  394. *
  395. * Tear down the fence driver for all possible rings (all asics).
  396. */
  397. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  398. {
  399. int i, r;
  400. if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
  401. kmem_cache_destroy(amdgpu_fence_slab);
  402. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  403. struct amdgpu_ring *ring = adev->rings[i];
  404. if (!ring || !ring->fence_drv.initialized)
  405. continue;
  406. r = amdgpu_fence_wait_empty(ring);
  407. if (r) {
  408. /* no need to trigger GPU reset as we are unloading */
  409. amdgpu_fence_driver_force_completion(adev);
  410. }
  411. wake_up_all(&ring->fence_drv.fence_queue);
  412. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  413. ring->fence_drv.irq_type);
  414. amd_sched_fini(&ring->sched);
  415. del_timer_sync(&ring->fence_drv.fallback_timer);
  416. ring->fence_drv.initialized = false;
  417. }
  418. }
  419. /**
  420. * amdgpu_fence_driver_suspend - suspend the fence driver
  421. * for all possible rings.
  422. *
  423. * @adev: amdgpu device pointer
  424. *
  425. * Suspend the fence driver for all possible rings (all asics).
  426. */
  427. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  428. {
  429. int i, r;
  430. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  431. struct amdgpu_ring *ring = adev->rings[i];
  432. if (!ring || !ring->fence_drv.initialized)
  433. continue;
  434. /* wait for gpu to finish processing current batch */
  435. r = amdgpu_fence_wait_empty(ring);
  436. if (r) {
  437. /* delay GPU reset to resume */
  438. amdgpu_fence_driver_force_completion(adev);
  439. }
  440. /* disable the interrupt */
  441. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  442. ring->fence_drv.irq_type);
  443. }
  444. }
  445. /**
  446. * amdgpu_fence_driver_resume - resume the fence driver
  447. * for all possible rings.
  448. *
  449. * @adev: amdgpu device pointer
  450. *
  451. * Resume the fence driver for all possible rings (all asics).
  452. * Not all asics have all rings, so each asic will only
  453. * start the fence driver on the rings it has using
  454. * amdgpu_fence_driver_start_ring().
  455. * Returns 0 for success.
  456. */
  457. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  458. {
  459. int i;
  460. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  461. struct amdgpu_ring *ring = adev->rings[i];
  462. if (!ring || !ring->fence_drv.initialized)
  463. continue;
  464. /* enable the interrupt */
  465. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  466. ring->fence_drv.irq_type);
  467. }
  468. }
  469. /**
  470. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  471. *
  472. * @adev: amdgpu device pointer
  473. *
  474. * In case of GPU reset failure make sure no process keep waiting on fence
  475. * that will never complete.
  476. */
  477. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  478. {
  479. int i;
  480. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  481. struct amdgpu_ring *ring = adev->rings[i];
  482. if (!ring || !ring->fence_drv.initialized)
  483. continue;
  484. amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
  485. }
  486. }
  487. /*
  488. * Common fence implementation
  489. */
  490. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  491. {
  492. return "amdgpu";
  493. }
  494. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  495. {
  496. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  497. return (const char *)fence->ring->name;
  498. }
  499. /**
  500. * amdgpu_fence_is_signaled - test if fence is signaled
  501. *
  502. * @f: fence to test
  503. *
  504. * Test the fence sequence number if it is already signaled. If it isn't
  505. * signaled start fence processing. Returns True if the fence is signaled.
  506. */
  507. static bool amdgpu_fence_is_signaled(struct fence *f)
  508. {
  509. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  510. struct amdgpu_ring *ring = fence->ring;
  511. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  512. return true;
  513. amdgpu_fence_process(ring);
  514. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  515. return true;
  516. return false;
  517. }
  518. /**
  519. * amdgpu_fence_check_signaled - callback from fence_queue
  520. *
  521. * this function is called with fence_queue lock held, which is also used
  522. * for the fence locking itself, so unlocked variants are used for
  523. * fence_signal, and remove_wait_queue.
  524. */
  525. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  526. {
  527. struct amdgpu_fence *fence;
  528. struct amdgpu_device *adev;
  529. u64 seq;
  530. int ret;
  531. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  532. adev = fence->ring->adev;
  533. /*
  534. * We cannot use amdgpu_fence_process here because we're already
  535. * in the waitqueue, in a call from wake_up_all.
  536. */
  537. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  538. if (seq >= fence->seq) {
  539. ret = fence_signal_locked(&fence->base);
  540. if (!ret)
  541. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  542. else
  543. FENCE_TRACE(&fence->base, "was already signaled\n");
  544. __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
  545. fence_put(&fence->base);
  546. } else
  547. FENCE_TRACE(&fence->base, "pending\n");
  548. return 0;
  549. }
  550. /**
  551. * amdgpu_fence_enable_signaling - enable signalling on fence
  552. * @fence: fence
  553. *
  554. * This function is called with fence_queue lock held, and adds a callback
  555. * to fence_queue that checks if this fence is signaled, and if so it
  556. * signals the fence and removes itself.
  557. */
  558. static bool amdgpu_fence_enable_signaling(struct fence *f)
  559. {
  560. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  561. struct amdgpu_ring *ring = fence->ring;
  562. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  563. return false;
  564. fence->fence_wake.flags = 0;
  565. fence->fence_wake.private = NULL;
  566. fence->fence_wake.func = amdgpu_fence_check_signaled;
  567. __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
  568. fence_get(f);
  569. if (!timer_pending(&ring->fence_drv.fallback_timer))
  570. amdgpu_fence_schedule_fallback(ring);
  571. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  572. return true;
  573. }
  574. static void amdgpu_fence_release(struct fence *f)
  575. {
  576. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  577. kmem_cache_free(amdgpu_fence_slab, fence);
  578. }
  579. static const struct fence_ops amdgpu_fence_ops = {
  580. .get_driver_name = amdgpu_fence_get_driver_name,
  581. .get_timeline_name = amdgpu_fence_get_timeline_name,
  582. .enable_signaling = amdgpu_fence_enable_signaling,
  583. .signaled = amdgpu_fence_is_signaled,
  584. .wait = fence_default_wait,
  585. .release = amdgpu_fence_release,
  586. };
  587. /*
  588. * Fence debugfs
  589. */
  590. #if defined(CONFIG_DEBUG_FS)
  591. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  592. {
  593. struct drm_info_node *node = (struct drm_info_node *)m->private;
  594. struct drm_device *dev = node->minor->dev;
  595. struct amdgpu_device *adev = dev->dev_private;
  596. int i;
  597. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  598. struct amdgpu_ring *ring = adev->rings[i];
  599. if (!ring || !ring->fence_drv.initialized)
  600. continue;
  601. amdgpu_fence_process(ring);
  602. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  603. seq_printf(m, "Last signaled fence 0x%016llx\n",
  604. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  605. seq_printf(m, "Last emitted 0x%016llx\n",
  606. ring->fence_drv.sync_seq);
  607. }
  608. return 0;
  609. }
  610. /**
  611. * amdgpu_debugfs_gpu_reset - manually trigger a gpu reset
  612. *
  613. * Manually trigger a gpu reset at the next fence wait.
  614. */
  615. static int amdgpu_debugfs_gpu_reset(struct seq_file *m, void *data)
  616. {
  617. struct drm_info_node *node = (struct drm_info_node *) m->private;
  618. struct drm_device *dev = node->minor->dev;
  619. struct amdgpu_device *adev = dev->dev_private;
  620. seq_printf(m, "gpu reset\n");
  621. amdgpu_gpu_reset(adev);
  622. return 0;
  623. }
  624. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  625. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  626. {"amdgpu_gpu_reset", &amdgpu_debugfs_gpu_reset, 0, NULL}
  627. };
  628. #endif
  629. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  630. {
  631. #if defined(CONFIG_DEBUG_FS)
  632. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
  633. #else
  634. return 0;
  635. #endif
  636. }