amdgpu_fence.c 20 KB

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