amdgpu_fence.c 21 KB

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