amdgpu_fence.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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[ring->idx];
  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[ring->idx];
  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_timeout - 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. * -EDEADL: GPU hang detected
  248. * -EINVAL: some paramter is not valid
  249. */
  250. static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
  251. {
  252. bool signaled = false;
  253. BUG_ON(!ring);
  254. if (seq > ring->fence_drv.sync_seq[ring->idx])
  255. return -EINVAL;
  256. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  257. return 0;
  258. amdgpu_fence_schedule_fallback(ring);
  259. wait_event(ring->fence_drv.fence_queue, (
  260. (signaled = amdgpu_fence_seq_signaled(ring, seq))));
  261. if (signaled)
  262. return 0;
  263. else
  264. return -EDEADLK;
  265. }
  266. /**
  267. * amdgpu_fence_wait_next - wait for the next fence to signal
  268. *
  269. * @adev: amdgpu device pointer
  270. * @ring: ring index the fence is associated with
  271. *
  272. * Wait for the next fence on the requested ring to signal (all asics).
  273. * Returns 0 if the next fence has passed, error for all other cases.
  274. * Caller must hold ring lock.
  275. */
  276. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  277. {
  278. uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  279. if (seq >= ring->fence_drv.sync_seq[ring->idx])
  280. return -ENOENT;
  281. return amdgpu_fence_ring_wait_seq(ring, seq);
  282. }
  283. /**
  284. * amdgpu_fence_wait_empty - wait for all fences to signal
  285. *
  286. * @adev: amdgpu device pointer
  287. * @ring: ring index the fence is associated with
  288. *
  289. * Wait for all fences on the requested ring to signal (all asics).
  290. * Returns 0 if the fences have passed, error for all other cases.
  291. * Caller must hold ring lock.
  292. */
  293. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  294. {
  295. uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
  296. if (!seq)
  297. return 0;
  298. return amdgpu_fence_ring_wait_seq(ring, seq);
  299. }
  300. /**
  301. * amdgpu_fence_count_emitted - get the count of emitted fences
  302. *
  303. * @ring: ring the fence is associated with
  304. *
  305. * Get the number of fences emitted on the requested ring (all asics).
  306. * Returns the number of emitted fences on the ring. Used by the
  307. * dynpm code to ring track activity.
  308. */
  309. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  310. {
  311. uint64_t emitted;
  312. /* We are not protected by ring lock when reading the last sequence
  313. * but it's ok to report slightly wrong fence count here.
  314. */
  315. amdgpu_fence_process(ring);
  316. emitted = ring->fence_drv.sync_seq[ring->idx]
  317. - atomic64_read(&ring->fence_drv.last_seq);
  318. /* to avoid 32bits warp around */
  319. if (emitted > 0x10000000)
  320. emitted = 0x10000000;
  321. return (unsigned)emitted;
  322. }
  323. /**
  324. * amdgpu_fence_need_sync - do we need a semaphore
  325. *
  326. * @fence: amdgpu fence object
  327. * @dst_ring: which ring to check against
  328. *
  329. * Check if the fence needs to be synced against another ring
  330. * (all asics). If so, we need to emit a semaphore.
  331. * Returns true if we need to sync with another ring, false if
  332. * not.
  333. */
  334. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  335. struct amdgpu_ring *dst_ring)
  336. {
  337. struct amdgpu_fence_driver *fdrv;
  338. if (!fence)
  339. return false;
  340. if (fence->ring == dst_ring)
  341. return false;
  342. /* we are protected by the ring mutex */
  343. fdrv = &dst_ring->fence_drv;
  344. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  345. return false;
  346. return true;
  347. }
  348. /**
  349. * amdgpu_fence_note_sync - record the sync point
  350. *
  351. * @fence: amdgpu fence object
  352. * @dst_ring: which ring to check against
  353. *
  354. * Note the sequence number at which point the fence will
  355. * be synced with the requested ring (all asics).
  356. */
  357. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  358. struct amdgpu_ring *dst_ring)
  359. {
  360. struct amdgpu_fence_driver *dst, *src;
  361. unsigned i;
  362. if (!fence)
  363. return;
  364. if (fence->ring == dst_ring)
  365. return;
  366. /* we are protected by the ring mutex */
  367. src = &fence->ring->fence_drv;
  368. dst = &dst_ring->fence_drv;
  369. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  370. if (i == dst_ring->idx)
  371. continue;
  372. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  373. }
  374. }
  375. /**
  376. * amdgpu_fence_driver_start_ring - make the fence driver
  377. * ready for use on the requested ring.
  378. *
  379. * @ring: ring to start the fence driver on
  380. * @irq_src: interrupt source to use for this ring
  381. * @irq_type: interrupt type to use for this ring
  382. *
  383. * Make the fence driver ready for processing (all asics).
  384. * Not all asics have all rings, so each asic will only
  385. * start the fence driver on the rings it has.
  386. * Returns 0 for success, errors for failure.
  387. */
  388. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  389. struct amdgpu_irq_src *irq_src,
  390. unsigned irq_type)
  391. {
  392. struct amdgpu_device *adev = ring->adev;
  393. uint64_t index;
  394. if (ring != &adev->uvd.ring) {
  395. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  396. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  397. } else {
  398. /* put fence directly behind firmware */
  399. index = ALIGN(adev->uvd.fw->size, 8);
  400. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  401. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  402. }
  403. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  404. amdgpu_irq_get(adev, irq_src, irq_type);
  405. ring->fence_drv.irq_src = irq_src;
  406. ring->fence_drv.irq_type = irq_type;
  407. ring->fence_drv.initialized = true;
  408. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  409. "cpu addr 0x%p\n", ring->idx,
  410. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  411. return 0;
  412. }
  413. /**
  414. * amdgpu_fence_driver_init_ring - init the fence driver
  415. * for the requested ring.
  416. *
  417. * @ring: ring to init the fence driver on
  418. *
  419. * Init the fence driver for the requested ring (all asics).
  420. * Helper function for amdgpu_fence_driver_init().
  421. */
  422. int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  423. {
  424. int i, r;
  425. ring->fence_drv.cpu_addr = NULL;
  426. ring->fence_drv.gpu_addr = 0;
  427. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  428. ring->fence_drv.sync_seq[i] = 0;
  429. atomic64_set(&ring->fence_drv.last_seq, 0);
  430. ring->fence_drv.initialized = false;
  431. setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
  432. (unsigned long)ring);
  433. init_waitqueue_head(&ring->fence_drv.fence_queue);
  434. if (amdgpu_enable_scheduler) {
  435. long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
  436. if (timeout == 0) {
  437. /*
  438. * FIXME:
  439. * Delayed workqueue cannot use it directly,
  440. * so the scheduler will not use delayed workqueue if
  441. * MAX_SCHEDULE_TIMEOUT is set.
  442. * Currently keep it simple and silly.
  443. */
  444. timeout = MAX_SCHEDULE_TIMEOUT;
  445. }
  446. r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
  447. amdgpu_sched_hw_submission,
  448. timeout, ring->name);
  449. if (r) {
  450. DRM_ERROR("Failed to create scheduler on ring %s.\n",
  451. ring->name);
  452. return r;
  453. }
  454. }
  455. return 0;
  456. }
  457. /**
  458. * amdgpu_fence_driver_init - init the fence driver
  459. * for all possible rings.
  460. *
  461. * @adev: amdgpu device pointer
  462. *
  463. * Init the fence driver for all possible rings (all asics).
  464. * Not all asics have all rings, so each asic will only
  465. * start the fence driver on the rings it has using
  466. * amdgpu_fence_driver_start_ring().
  467. * Returns 0 for success.
  468. */
  469. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  470. {
  471. if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
  472. amdgpu_fence_slab = kmem_cache_create(
  473. "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
  474. SLAB_HWCACHE_ALIGN, NULL);
  475. if (!amdgpu_fence_slab)
  476. return -ENOMEM;
  477. }
  478. if (amdgpu_debugfs_fence_init(adev))
  479. dev_err(adev->dev, "fence debugfs file creation failed\n");
  480. return 0;
  481. }
  482. /**
  483. * amdgpu_fence_driver_fini - tear down the fence driver
  484. * for all possible rings.
  485. *
  486. * @adev: amdgpu device pointer
  487. *
  488. * Tear down the fence driver for all possible rings (all asics).
  489. */
  490. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  491. {
  492. int i, r;
  493. if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
  494. kmem_cache_destroy(amdgpu_fence_slab);
  495. mutex_lock(&adev->ring_lock);
  496. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  497. struct amdgpu_ring *ring = adev->rings[i];
  498. if (!ring || !ring->fence_drv.initialized)
  499. continue;
  500. r = amdgpu_fence_wait_empty(ring);
  501. if (r) {
  502. /* no need to trigger GPU reset as we are unloading */
  503. amdgpu_fence_driver_force_completion(adev);
  504. }
  505. wake_up_all(&ring->fence_drv.fence_queue);
  506. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  507. ring->fence_drv.irq_type);
  508. amd_sched_fini(&ring->sched);
  509. del_timer_sync(&ring->fence_drv.fallback_timer);
  510. ring->fence_drv.initialized = false;
  511. }
  512. mutex_unlock(&adev->ring_lock);
  513. }
  514. /**
  515. * amdgpu_fence_driver_suspend - suspend the fence driver
  516. * for all possible rings.
  517. *
  518. * @adev: amdgpu device pointer
  519. *
  520. * Suspend the fence driver for all possible rings (all asics).
  521. */
  522. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  523. {
  524. int i, r;
  525. mutex_lock(&adev->ring_lock);
  526. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  527. struct amdgpu_ring *ring = adev->rings[i];
  528. if (!ring || !ring->fence_drv.initialized)
  529. continue;
  530. /* wait for gpu to finish processing current batch */
  531. r = amdgpu_fence_wait_empty(ring);
  532. if (r) {
  533. /* delay GPU reset to resume */
  534. amdgpu_fence_driver_force_completion(adev);
  535. }
  536. /* disable the interrupt */
  537. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  538. ring->fence_drv.irq_type);
  539. }
  540. mutex_unlock(&adev->ring_lock);
  541. }
  542. /**
  543. * amdgpu_fence_driver_resume - resume the fence driver
  544. * for all possible rings.
  545. *
  546. * @adev: amdgpu device pointer
  547. *
  548. * Resume the fence driver for all possible rings (all asics).
  549. * Not all asics have all rings, so each asic will only
  550. * start the fence driver on the rings it has using
  551. * amdgpu_fence_driver_start_ring().
  552. * Returns 0 for success.
  553. */
  554. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  555. {
  556. int i;
  557. mutex_lock(&adev->ring_lock);
  558. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  559. struct amdgpu_ring *ring = adev->rings[i];
  560. if (!ring || !ring->fence_drv.initialized)
  561. continue;
  562. /* enable the interrupt */
  563. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  564. ring->fence_drv.irq_type);
  565. }
  566. mutex_unlock(&adev->ring_lock);
  567. }
  568. /**
  569. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  570. *
  571. * @adev: amdgpu device pointer
  572. *
  573. * In case of GPU reset failure make sure no process keep waiting on fence
  574. * that will never complete.
  575. */
  576. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  577. {
  578. int i;
  579. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  580. struct amdgpu_ring *ring = adev->rings[i];
  581. if (!ring || !ring->fence_drv.initialized)
  582. continue;
  583. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  584. }
  585. }
  586. /*
  587. * Common fence implementation
  588. */
  589. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  590. {
  591. return "amdgpu";
  592. }
  593. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  594. {
  595. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  596. return (const char *)fence->ring->name;
  597. }
  598. /**
  599. * amdgpu_fence_is_signaled - test if fence is signaled
  600. *
  601. * @f: fence to test
  602. *
  603. * Test the fence sequence number if it is already signaled. If it isn't
  604. * signaled start fence processing. Returns True if the fence is signaled.
  605. */
  606. static bool amdgpu_fence_is_signaled(struct fence *f)
  607. {
  608. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  609. struct amdgpu_ring *ring = fence->ring;
  610. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  611. return true;
  612. amdgpu_fence_process(ring);
  613. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  614. return true;
  615. return false;
  616. }
  617. /**
  618. * amdgpu_fence_check_signaled - callback from fence_queue
  619. *
  620. * this function is called with fence_queue lock held, which is also used
  621. * for the fence locking itself, so unlocked variants are used for
  622. * fence_signal, and remove_wait_queue.
  623. */
  624. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  625. {
  626. struct amdgpu_fence *fence;
  627. struct amdgpu_device *adev;
  628. u64 seq;
  629. int ret;
  630. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  631. adev = fence->ring->adev;
  632. /*
  633. * We cannot use amdgpu_fence_process here because we're already
  634. * in the waitqueue, in a call from wake_up_all.
  635. */
  636. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  637. if (seq >= fence->seq) {
  638. ret = fence_signal_locked(&fence->base);
  639. if (!ret)
  640. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  641. else
  642. FENCE_TRACE(&fence->base, "was already signaled\n");
  643. __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
  644. fence_put(&fence->base);
  645. } else
  646. FENCE_TRACE(&fence->base, "pending\n");
  647. return 0;
  648. }
  649. /**
  650. * amdgpu_fence_enable_signaling - enable signalling on fence
  651. * @fence: fence
  652. *
  653. * This function is called with fence_queue lock held, and adds a callback
  654. * to fence_queue that checks if this fence is signaled, and if so it
  655. * signals the fence and removes itself.
  656. */
  657. static bool amdgpu_fence_enable_signaling(struct fence *f)
  658. {
  659. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  660. struct amdgpu_ring *ring = fence->ring;
  661. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  662. return false;
  663. fence->fence_wake.flags = 0;
  664. fence->fence_wake.private = NULL;
  665. fence->fence_wake.func = amdgpu_fence_check_signaled;
  666. __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
  667. fence_get(f);
  668. if (!timer_pending(&ring->fence_drv.fallback_timer))
  669. amdgpu_fence_schedule_fallback(ring);
  670. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  671. return true;
  672. }
  673. static void amdgpu_fence_release(struct fence *f)
  674. {
  675. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  676. kmem_cache_free(amdgpu_fence_slab, fence);
  677. }
  678. const struct fence_ops amdgpu_fence_ops = {
  679. .get_driver_name = amdgpu_fence_get_driver_name,
  680. .get_timeline_name = amdgpu_fence_get_timeline_name,
  681. .enable_signaling = amdgpu_fence_enable_signaling,
  682. .signaled = amdgpu_fence_is_signaled,
  683. .wait = fence_default_wait,
  684. .release = amdgpu_fence_release,
  685. };
  686. /*
  687. * Fence debugfs
  688. */
  689. #if defined(CONFIG_DEBUG_FS)
  690. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  691. {
  692. struct drm_info_node *node = (struct drm_info_node *)m->private;
  693. struct drm_device *dev = node->minor->dev;
  694. struct amdgpu_device *adev = dev->dev_private;
  695. int i, j;
  696. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  697. struct amdgpu_ring *ring = adev->rings[i];
  698. if (!ring || !ring->fence_drv.initialized)
  699. continue;
  700. amdgpu_fence_process(ring);
  701. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  702. seq_printf(m, "Last signaled fence 0x%016llx\n",
  703. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  704. seq_printf(m, "Last emitted 0x%016llx\n",
  705. ring->fence_drv.sync_seq[i]);
  706. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  707. struct amdgpu_ring *other = adev->rings[j];
  708. if (i != j && other && other->fence_drv.initialized &&
  709. ring->fence_drv.sync_seq[j])
  710. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  711. j, ring->fence_drv.sync_seq[j]);
  712. }
  713. }
  714. return 0;
  715. }
  716. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  717. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  718. };
  719. #endif
  720. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  721. {
  722. #if defined(CONFIG_DEBUG_FS)
  723. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  724. #else
  725. return 0;
  726. #endif
  727. }