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