amdgpu_fence.c 21 KB

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