amdgpu_fence.c 21 KB

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