amdgpu_fence.c 19 KB

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