amdgpu_fence.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. /**
  49. * amdgpu_fence_write - write a fence value
  50. *
  51. * @ring: ring the fence is associated with
  52. * @seq: sequence number to write
  53. *
  54. * Writes a fence value to memory (all asics).
  55. */
  56. static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
  57. {
  58. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  59. if (drv->cpu_addr)
  60. *drv->cpu_addr = cpu_to_le32(seq);
  61. }
  62. /**
  63. * amdgpu_fence_read - read a fence value
  64. *
  65. * @ring: ring the fence is associated with
  66. *
  67. * Reads a fence value from memory (all asics).
  68. * Returns the value of the fence read from memory.
  69. */
  70. static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
  71. {
  72. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  73. u32 seq = 0;
  74. if (drv->cpu_addr)
  75. seq = le32_to_cpu(*drv->cpu_addr);
  76. else
  77. seq = lower_32_bits(atomic64_read(&drv->last_seq));
  78. return seq;
  79. }
  80. /**
  81. * amdgpu_fence_schedule_check - schedule lockup check
  82. *
  83. * @ring: pointer to struct amdgpu_ring
  84. *
  85. * Queues a delayed work item to check for lockups.
  86. */
  87. static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring)
  88. {
  89. /*
  90. * Do not reset the timer here with mod_delayed_work,
  91. * this can livelock in an interaction with TTM delayed destroy.
  92. */
  93. queue_delayed_work(system_power_efficient_wq,
  94. &ring->fence_drv.lockup_work,
  95. AMDGPU_FENCE_JIFFIES_TIMEOUT);
  96. }
  97. /**
  98. * amdgpu_fence_emit - emit a fence on the requested ring
  99. *
  100. * @ring: ring the fence is associated with
  101. * @owner: creator of the fence
  102. * @fence: amdgpu fence object
  103. *
  104. * Emits a fence command on the requested ring (all asics).
  105. * Returns 0 on success, -ENOMEM on failure.
  106. */
  107. int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
  108. struct amdgpu_fence **fence)
  109. {
  110. struct amdgpu_device *adev = ring->adev;
  111. /* we are protected by the ring emission mutex */
  112. *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
  113. if ((*fence) == NULL) {
  114. return -ENOMEM;
  115. }
  116. (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
  117. (*fence)->ring = ring;
  118. (*fence)->owner = owner;
  119. fence_init(&(*fence)->base, &amdgpu_fence_ops,
  120. &adev->fence_queue.lock, adev->fence_context + ring->idx,
  121. (*fence)->seq);
  122. amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
  123. (*fence)->seq,
  124. AMDGPU_FENCE_FLAG_INT);
  125. trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
  126. return 0;
  127. }
  128. /**
  129. * amdgpu_fence_recreate - recreate a fence from an user fence
  130. *
  131. * @ring: ring the fence is associated with
  132. * @owner: creator of the fence
  133. * @seq: user fence sequence number
  134. * @fence: resulting amdgpu fence object
  135. *
  136. * Recreates a fence command from the user fence sequence number (all asics).
  137. * Returns 0 on success, -ENOMEM on failure.
  138. */
  139. int amdgpu_fence_recreate(struct amdgpu_ring *ring, void *owner,
  140. uint64_t seq, struct amdgpu_fence **fence)
  141. {
  142. struct amdgpu_device *adev = ring->adev;
  143. if (seq > ring->fence_drv.sync_seq[ring->idx])
  144. return -EINVAL;
  145. *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
  146. if ((*fence) == NULL)
  147. return -ENOMEM;
  148. (*fence)->seq = seq;
  149. (*fence)->ring = ring;
  150. (*fence)->owner = owner;
  151. fence_init(&(*fence)->base, &amdgpu_fence_ops,
  152. &adev->fence_queue.lock, adev->fence_context + ring->idx,
  153. (*fence)->seq);
  154. return 0;
  155. }
  156. /**
  157. * amdgpu_fence_check_signaled - callback from fence_queue
  158. *
  159. * this function is called with fence_queue lock held, which is also used
  160. * for the fence locking itself, so unlocked variants are used for
  161. * fence_signal, and remove_wait_queue.
  162. */
  163. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  164. {
  165. struct amdgpu_fence *fence;
  166. struct amdgpu_device *adev;
  167. u64 seq;
  168. int ret;
  169. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  170. adev = fence->ring->adev;
  171. /*
  172. * We cannot use amdgpu_fence_process here because we're already
  173. * in the waitqueue, in a call from wake_up_all.
  174. */
  175. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  176. if (seq >= fence->seq) {
  177. ret = fence_signal_locked(&fence->base);
  178. if (!ret)
  179. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  180. else
  181. FENCE_TRACE(&fence->base, "was already signaled\n");
  182. amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src,
  183. fence->ring->fence_drv.irq_type);
  184. __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
  185. fence_put(&fence->base);
  186. } else
  187. FENCE_TRACE(&fence->base, "pending\n");
  188. return 0;
  189. }
  190. /**
  191. * amdgpu_fence_activity - check for fence activity
  192. *
  193. * @ring: pointer to struct amdgpu_ring
  194. *
  195. * Checks the current fence value and calculates the last
  196. * signalled fence value. Returns true if activity occured
  197. * on the ring, and the fence_queue should be waken up.
  198. */
  199. static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
  200. {
  201. uint64_t seq, last_seq, last_emitted;
  202. unsigned count_loop = 0;
  203. bool wake = false;
  204. /* Note there is a scenario here for an infinite loop but it's
  205. * very unlikely to happen. For it to happen, the current polling
  206. * process need to be interrupted by another process and another
  207. * process needs to update the last_seq btw the atomic read and
  208. * xchg of the current process.
  209. *
  210. * More over for this to go in infinite loop there need to be
  211. * continuously new fence signaled ie amdgpu_fence_read needs
  212. * to return a different value each time for both the currently
  213. * polling process and the other process that xchg the last_seq
  214. * btw atomic read and xchg of the current process. And the
  215. * value the other process set as last seq must be higher than
  216. * the seq value we just read. Which means that current process
  217. * need to be interrupted after amdgpu_fence_read and before
  218. * atomic xchg.
  219. *
  220. * To be even more safe we count the number of time we loop and
  221. * we bail after 10 loop just accepting the fact that we might
  222. * have temporarly set the last_seq not to the true real last
  223. * seq but to an older one.
  224. */
  225. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  226. do {
  227. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  228. seq = amdgpu_fence_read(ring);
  229. seq |= last_seq & 0xffffffff00000000LL;
  230. if (seq < last_seq) {
  231. seq &= 0xffffffff;
  232. seq |= last_emitted & 0xffffffff00000000LL;
  233. }
  234. if (seq <= last_seq || seq > last_emitted) {
  235. break;
  236. }
  237. /* If we loop over we don't want to return without
  238. * checking if a fence is signaled as it means that the
  239. * seq we just read is different from the previous on.
  240. */
  241. wake = true;
  242. last_seq = seq;
  243. if ((count_loop++) > 10) {
  244. /* We looped over too many time leave with the
  245. * fact that we might have set an older fence
  246. * seq then the current real last seq as signaled
  247. * by the hw.
  248. */
  249. break;
  250. }
  251. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  252. if (seq < last_emitted)
  253. amdgpu_fence_schedule_check(ring);
  254. return wake;
  255. }
  256. /**
  257. * amdgpu_fence_check_lockup - check for hardware lockup
  258. *
  259. * @work: delayed work item
  260. *
  261. * Checks for fence activity and if there is none probe
  262. * the hardware if a lockup occured.
  263. */
  264. static void amdgpu_fence_check_lockup(struct work_struct *work)
  265. {
  266. struct amdgpu_fence_driver *fence_drv;
  267. struct amdgpu_ring *ring;
  268. fence_drv = container_of(work, struct amdgpu_fence_driver,
  269. lockup_work.work);
  270. ring = fence_drv->ring;
  271. if (!down_read_trylock(&ring->adev->exclusive_lock)) {
  272. /* just reschedule the check if a reset is going on */
  273. amdgpu_fence_schedule_check(ring);
  274. return;
  275. }
  276. if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) {
  277. fence_drv->delayed_irq = false;
  278. amdgpu_irq_update(ring->adev, fence_drv->irq_src,
  279. fence_drv->irq_type);
  280. }
  281. if (amdgpu_fence_activity(ring))
  282. wake_up_all(&ring->adev->fence_queue);
  283. else if (amdgpu_ring_is_lockup(ring)) {
  284. /* good news we believe it's a lockup */
  285. dev_warn(ring->adev->dev, "GPU lockup (current fence id "
  286. "0x%016llx last fence id 0x%016llx on ring %d)\n",
  287. (uint64_t)atomic64_read(&fence_drv->last_seq),
  288. fence_drv->sync_seq[ring->idx], ring->idx);
  289. /* remember that we need an reset */
  290. ring->adev->needs_reset = true;
  291. wake_up_all(&ring->adev->fence_queue);
  292. }
  293. up_read(&ring->adev->exclusive_lock);
  294. }
  295. /**
  296. * amdgpu_fence_process - process a fence
  297. *
  298. * @adev: amdgpu_device pointer
  299. * @ring: ring index the fence is associated with
  300. *
  301. * Checks the current fence value and wakes the fence queue
  302. * if the sequence number has increased (all asics).
  303. */
  304. void amdgpu_fence_process(struct amdgpu_ring *ring)
  305. {
  306. uint64_t seq, last_seq, last_emitted;
  307. unsigned count_loop = 0;
  308. bool wake = false;
  309. /* Note there is a scenario here for an infinite loop but it's
  310. * very unlikely to happen. For it to happen, the current polling
  311. * process need to be interrupted by another process and another
  312. * process needs to update the last_seq btw the atomic read and
  313. * xchg of the current process.
  314. *
  315. * More over for this to go in infinite loop there need to be
  316. * continuously new fence signaled ie amdgpu_fence_read needs
  317. * to return a different value each time for both the currently
  318. * polling process and the other process that xchg the last_seq
  319. * btw atomic read and xchg of the current process. And the
  320. * value the other process set as last seq must be higher than
  321. * the seq value we just read. Which means that current process
  322. * need to be interrupted after amdgpu_fence_read and before
  323. * atomic xchg.
  324. *
  325. * To be even more safe we count the number of time we loop and
  326. * we bail after 10 loop just accepting the fact that we might
  327. * have temporarly set the last_seq not to the true real last
  328. * seq but to an older one.
  329. */
  330. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  331. do {
  332. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  333. seq = amdgpu_fence_read(ring);
  334. seq |= last_seq & 0xffffffff00000000LL;
  335. if (seq < last_seq) {
  336. seq &= 0xffffffff;
  337. seq |= last_emitted & 0xffffffff00000000LL;
  338. }
  339. if (seq <= last_seq || seq > last_emitted) {
  340. break;
  341. }
  342. /* If we loop over we don't want to return without
  343. * checking if a fence is signaled as it means that the
  344. * seq we just read is different from the previous on.
  345. */
  346. wake = true;
  347. last_seq = seq;
  348. if ((count_loop++) > 10) {
  349. /* We looped over too many time leave with the
  350. * fact that we might have set an older fence
  351. * seq then the current real last seq as signaled
  352. * by the hw.
  353. */
  354. break;
  355. }
  356. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  357. if (wake)
  358. wake_up_all(&ring->adev->fence_queue);
  359. }
  360. /**
  361. * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
  362. *
  363. * @ring: ring the fence is associated with
  364. * @seq: sequence number
  365. *
  366. * Check if the last signaled fence sequnce number is >= the requested
  367. * sequence number (all asics).
  368. * Returns true if the fence has signaled (current fence value
  369. * is >= requested value) or false if it has not (current fence
  370. * value is < the requested value. Helper function for
  371. * amdgpu_fence_signaled().
  372. */
  373. static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
  374. {
  375. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  376. return true;
  377. /* poll new last sequence at least once */
  378. amdgpu_fence_process(ring);
  379. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  380. return true;
  381. return false;
  382. }
  383. static bool amdgpu_fence_is_signaled(struct fence *f)
  384. {
  385. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  386. struct amdgpu_ring *ring = fence->ring;
  387. struct amdgpu_device *adev = ring->adev;
  388. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  389. return true;
  390. if (down_read_trylock(&adev->exclusive_lock)) {
  391. amdgpu_fence_process(ring);
  392. up_read(&adev->exclusive_lock);
  393. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  394. return true;
  395. }
  396. return false;
  397. }
  398. /**
  399. * amdgpu_fence_enable_signaling - enable signalling on fence
  400. * @fence: fence
  401. *
  402. * This function is called with fence_queue lock held, and adds a callback
  403. * to fence_queue that checks if this fence is signaled, and if so it
  404. * signals the fence and removes itself.
  405. */
  406. static bool amdgpu_fence_enable_signaling(struct fence *f)
  407. {
  408. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  409. struct amdgpu_ring *ring = fence->ring;
  410. struct amdgpu_device *adev = ring->adev;
  411. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  412. return false;
  413. if (down_read_trylock(&adev->exclusive_lock)) {
  414. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  415. ring->fence_drv.irq_type);
  416. if (amdgpu_fence_activity(ring))
  417. wake_up_all_locked(&adev->fence_queue);
  418. /* did fence get signaled after we enabled the sw irq? */
  419. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) {
  420. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  421. ring->fence_drv.irq_type);
  422. up_read(&adev->exclusive_lock);
  423. return false;
  424. }
  425. up_read(&adev->exclusive_lock);
  426. } else {
  427. /* we're probably in a lockup, lets not fiddle too much */
  428. if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src,
  429. ring->fence_drv.irq_type))
  430. ring->fence_drv.delayed_irq = true;
  431. amdgpu_fence_schedule_check(ring);
  432. }
  433. fence->fence_wake.flags = 0;
  434. fence->fence_wake.private = NULL;
  435. fence->fence_wake.func = amdgpu_fence_check_signaled;
  436. __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
  437. fence_get(f);
  438. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  439. return true;
  440. }
  441. /**
  442. * amdgpu_fence_signaled - check if a fence has signaled
  443. *
  444. * @fence: amdgpu fence object
  445. *
  446. * Check if the requested fence has signaled (all asics).
  447. * Returns true if the fence has signaled or false if it has not.
  448. */
  449. bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
  450. {
  451. if (!fence)
  452. return true;
  453. if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
  454. if (!fence_signal(&fence->base))
  455. FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
  456. return true;
  457. }
  458. return false;
  459. }
  460. /**
  461. * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
  462. *
  463. * @adev: amdgpu device pointer
  464. * @seq: sequence numbers
  465. *
  466. * Check if the last signaled fence sequnce number is >= the requested
  467. * sequence number (all asics).
  468. * Returns true if any has signaled (current value is >= requested value)
  469. * or false if it has not. Helper function for amdgpu_fence_wait_seq.
  470. */
  471. static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
  472. {
  473. unsigned i;
  474. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  475. if (!adev->rings[i] || !seq[i])
  476. continue;
  477. if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
  478. return true;
  479. }
  480. return false;
  481. }
  482. /**
  483. * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
  484. *
  485. * @adev: amdgpu device pointer
  486. * @target_seq: sequence number(s) we want to wait for
  487. * @intr: use interruptable sleep
  488. * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
  489. *
  490. * Wait for the requested sequence number(s) to be written by any ring
  491. * (all asics). Sequnce number array is indexed by ring id.
  492. * @intr selects whether to use interruptable (true) or non-interruptable
  493. * (false) sleep when waiting for the sequence number. Helper function
  494. * for amdgpu_fence_wait_*().
  495. * Returns remaining time if the sequence number has passed, 0 when
  496. * the wait timeout, or an error for all other cases.
  497. * -EDEADLK is returned when a GPU lockup has been detected.
  498. */
  499. static long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev,
  500. u64 *target_seq, bool intr,
  501. long timeout)
  502. {
  503. uint64_t last_seq[AMDGPU_MAX_RINGS];
  504. bool signaled;
  505. int i;
  506. long r;
  507. if (timeout == 0) {
  508. return amdgpu_fence_any_seq_signaled(adev, target_seq);
  509. }
  510. while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
  511. /* Save current sequence values, used to check for GPU lockups */
  512. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  513. struct amdgpu_ring *ring = adev->rings[i];
  514. if (!ring || !target_seq[i])
  515. continue;
  516. last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
  517. trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
  518. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  519. ring->fence_drv.irq_type);
  520. }
  521. if (intr) {
  522. r = wait_event_interruptible_timeout(adev->fence_queue, (
  523. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  524. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  525. } else {
  526. r = wait_event_timeout(adev->fence_queue, (
  527. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  528. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  529. }
  530. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  531. struct amdgpu_ring *ring = adev->rings[i];
  532. if (!ring || !target_seq[i])
  533. continue;
  534. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  535. ring->fence_drv.irq_type);
  536. trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
  537. }
  538. if (unlikely(r < 0))
  539. return r;
  540. if (unlikely(!signaled)) {
  541. if (adev->needs_reset)
  542. return -EDEADLK;
  543. /* we were interrupted for some reason and fence
  544. * isn't signaled yet, resume waiting */
  545. if (r)
  546. continue;
  547. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  548. struct amdgpu_ring *ring = adev->rings[i];
  549. if (!ring || !target_seq[i])
  550. continue;
  551. if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
  552. break;
  553. }
  554. if (i != AMDGPU_MAX_RINGS)
  555. continue;
  556. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  557. if (!adev->rings[i] || !target_seq[i])
  558. continue;
  559. if (amdgpu_ring_is_lockup(adev->rings[i]))
  560. break;
  561. }
  562. if (i < AMDGPU_MAX_RINGS) {
  563. /* good news we believe it's a lockup */
  564. dev_warn(adev->dev, "GPU lockup (waiting for "
  565. "0x%016llx last fence id 0x%016llx on"
  566. " ring %d)\n",
  567. target_seq[i], last_seq[i], i);
  568. /* remember that we need an reset */
  569. adev->needs_reset = true;
  570. wake_up_all(&adev->fence_queue);
  571. return -EDEADLK;
  572. }
  573. if (timeout < MAX_SCHEDULE_TIMEOUT) {
  574. timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
  575. if (timeout <= 0) {
  576. return 0;
  577. }
  578. }
  579. }
  580. }
  581. return timeout;
  582. }
  583. /**
  584. * amdgpu_fence_wait - wait for a fence to signal
  585. *
  586. * @fence: amdgpu fence object
  587. * @intr: use interruptable sleep
  588. *
  589. * Wait for the requested fence to signal (all asics).
  590. * @intr selects whether to use interruptable (true) or non-interruptable
  591. * (false) sleep when waiting for the fence.
  592. * Returns 0 if the fence has passed, error for all other cases.
  593. */
  594. int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
  595. {
  596. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  597. long r;
  598. seq[fence->ring->idx] = fence->seq;
  599. r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  600. if (r < 0) {
  601. return r;
  602. }
  603. r = fence_signal(&fence->base);
  604. if (!r)
  605. FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
  606. return 0;
  607. }
  608. /**
  609. * amdgpu_fence_wait_any - wait for a fence to signal on any ring
  610. *
  611. * @adev: amdgpu device pointer
  612. * @fences: amdgpu fence object(s)
  613. * @intr: use interruptable sleep
  614. *
  615. * Wait for any requested fence to signal (all asics). Fence
  616. * array is indexed by ring id. @intr selects whether to use
  617. * interruptable (true) or non-interruptable (false) sleep when
  618. * waiting for the fences. Used by the suballocator.
  619. * Returns 0 if any fence has passed, error for all other cases.
  620. */
  621. int amdgpu_fence_wait_any(struct amdgpu_device *adev,
  622. struct amdgpu_fence **fences,
  623. bool intr)
  624. {
  625. uint64_t seq[AMDGPU_MAX_RINGS];
  626. unsigned i, num_rings = 0;
  627. long r;
  628. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  629. seq[i] = 0;
  630. if (!fences[i]) {
  631. continue;
  632. }
  633. seq[i] = fences[i]->seq;
  634. ++num_rings;
  635. }
  636. /* nothing to wait for ? */
  637. if (num_rings == 0)
  638. return -ENOENT;
  639. r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  640. if (r < 0) {
  641. return r;
  642. }
  643. return 0;
  644. }
  645. /**
  646. * amdgpu_fence_wait_next - wait for the next fence to signal
  647. *
  648. * @adev: amdgpu device pointer
  649. * @ring: ring index the fence is associated with
  650. *
  651. * Wait for the next fence on the requested ring to signal (all asics).
  652. * Returns 0 if the next fence has passed, error for all other cases.
  653. * Caller must hold ring lock.
  654. */
  655. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  656. {
  657. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  658. long r;
  659. seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  660. if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
  661. /* nothing to wait for, last_seq is
  662. already the last emited fence */
  663. return -ENOENT;
  664. }
  665. r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  666. if (r < 0)
  667. return r;
  668. return 0;
  669. }
  670. /**
  671. * amdgpu_fence_wait_empty - wait for all fences to signal
  672. *
  673. * @adev: amdgpu device pointer
  674. * @ring: ring index the fence is associated with
  675. *
  676. * Wait for all fences on the requested ring to signal (all asics).
  677. * Returns 0 if the fences have passed, error for all other cases.
  678. * Caller must hold ring lock.
  679. */
  680. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  681. {
  682. struct amdgpu_device *adev = ring->adev;
  683. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  684. long r;
  685. seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
  686. if (!seq[ring->idx])
  687. return 0;
  688. r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  689. if (r < 0) {
  690. if (r == -EDEADLK)
  691. return -EDEADLK;
  692. dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
  693. ring->idx, r);
  694. }
  695. return 0;
  696. }
  697. /**
  698. * amdgpu_fence_ref - take a ref on a fence
  699. *
  700. * @fence: amdgpu fence object
  701. *
  702. * Take a reference on a fence (all asics).
  703. * Returns the fence.
  704. */
  705. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  706. {
  707. fence_get(&fence->base);
  708. return fence;
  709. }
  710. /**
  711. * amdgpu_fence_unref - remove a ref on a fence
  712. *
  713. * @fence: amdgpu fence object
  714. *
  715. * Remove a reference on a fence (all asics).
  716. */
  717. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  718. {
  719. struct amdgpu_fence *tmp = *fence;
  720. *fence = NULL;
  721. if (tmp)
  722. fence_put(&tmp->base);
  723. }
  724. /**
  725. * amdgpu_fence_count_emitted - get the count of emitted fences
  726. *
  727. * @ring: ring the fence is associated with
  728. *
  729. * Get the number of fences emitted on the requested ring (all asics).
  730. * Returns the number of emitted fences on the ring. Used by the
  731. * dynpm code to ring track activity.
  732. */
  733. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  734. {
  735. uint64_t emitted;
  736. /* We are not protected by ring lock when reading the last sequence
  737. * but it's ok to report slightly wrong fence count here.
  738. */
  739. amdgpu_fence_process(ring);
  740. emitted = ring->fence_drv.sync_seq[ring->idx]
  741. - atomic64_read(&ring->fence_drv.last_seq);
  742. /* to avoid 32bits warp around */
  743. if (emitted > 0x10000000)
  744. emitted = 0x10000000;
  745. return (unsigned)emitted;
  746. }
  747. /**
  748. * amdgpu_fence_need_sync - do we need a semaphore
  749. *
  750. * @fence: amdgpu fence object
  751. * @dst_ring: which ring to check against
  752. *
  753. * Check if the fence needs to be synced against another ring
  754. * (all asics). If so, we need to emit a semaphore.
  755. * Returns true if we need to sync with another ring, false if
  756. * not.
  757. */
  758. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  759. struct amdgpu_ring *dst_ring)
  760. {
  761. struct amdgpu_fence_driver *fdrv;
  762. if (!fence)
  763. return false;
  764. if (fence->ring == dst_ring)
  765. return false;
  766. /* we are protected by the ring mutex */
  767. fdrv = &dst_ring->fence_drv;
  768. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  769. return false;
  770. return true;
  771. }
  772. /**
  773. * amdgpu_fence_note_sync - record the sync point
  774. *
  775. * @fence: amdgpu fence object
  776. * @dst_ring: which ring to check against
  777. *
  778. * Note the sequence number at which point the fence will
  779. * be synced with the requested ring (all asics).
  780. */
  781. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  782. struct amdgpu_ring *dst_ring)
  783. {
  784. struct amdgpu_fence_driver *dst, *src;
  785. unsigned i;
  786. if (!fence)
  787. return;
  788. if (fence->ring == dst_ring)
  789. return;
  790. /* we are protected by the ring mutex */
  791. src = &fence->ring->fence_drv;
  792. dst = &dst_ring->fence_drv;
  793. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  794. if (i == dst_ring->idx)
  795. continue;
  796. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  797. }
  798. }
  799. /**
  800. * amdgpu_fence_driver_start_ring - make the fence driver
  801. * ready for use on the requested ring.
  802. *
  803. * @ring: ring to start the fence driver on
  804. * @irq_src: interrupt source to use for this ring
  805. * @irq_type: interrupt type to use for this ring
  806. *
  807. * Make the fence driver ready for processing (all asics).
  808. * Not all asics have all rings, so each asic will only
  809. * start the fence driver on the rings it has.
  810. * Returns 0 for success, errors for failure.
  811. */
  812. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  813. struct amdgpu_irq_src *irq_src,
  814. unsigned irq_type)
  815. {
  816. struct amdgpu_device *adev = ring->adev;
  817. uint64_t index;
  818. if (ring != &adev->uvd.ring) {
  819. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  820. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  821. } else {
  822. /* put fence directly behind firmware */
  823. index = ALIGN(adev->uvd.fw->size, 8);
  824. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  825. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  826. }
  827. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  828. ring->fence_drv.initialized = true;
  829. ring->fence_drv.irq_src = irq_src;
  830. ring->fence_drv.irq_type = irq_type;
  831. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  832. "cpu addr 0x%p\n", ring->idx,
  833. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  834. return 0;
  835. }
  836. /**
  837. * amdgpu_fence_driver_init_ring - init the fence driver
  838. * for the requested ring.
  839. *
  840. * @ring: ring to init the fence driver on
  841. *
  842. * Init the fence driver for the requested ring (all asics).
  843. * Helper function for amdgpu_fence_driver_init().
  844. */
  845. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  846. {
  847. int i;
  848. ring->fence_drv.cpu_addr = NULL;
  849. ring->fence_drv.gpu_addr = 0;
  850. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  851. ring->fence_drv.sync_seq[i] = 0;
  852. atomic64_set(&ring->fence_drv.last_seq, 0);
  853. ring->fence_drv.initialized = false;
  854. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  855. amdgpu_fence_check_lockup);
  856. ring->fence_drv.ring = ring;
  857. }
  858. /**
  859. * amdgpu_fence_driver_init - init the fence driver
  860. * for all possible rings.
  861. *
  862. * @adev: amdgpu device pointer
  863. *
  864. * Init the fence driver for all possible rings (all asics).
  865. * Not all asics have all rings, so each asic will only
  866. * start the fence driver on the rings it has using
  867. * amdgpu_fence_driver_start_ring().
  868. * Returns 0 for success.
  869. */
  870. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  871. {
  872. init_waitqueue_head(&adev->fence_queue);
  873. if (amdgpu_debugfs_fence_init(adev))
  874. dev_err(adev->dev, "fence debugfs file creation failed\n");
  875. return 0;
  876. }
  877. /**
  878. * amdgpu_fence_driver_fini - tear down the fence driver
  879. * for all possible rings.
  880. *
  881. * @adev: amdgpu device pointer
  882. *
  883. * Tear down the fence driver for all possible rings (all asics).
  884. */
  885. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  886. {
  887. int i, r;
  888. mutex_lock(&adev->ring_lock);
  889. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  890. struct amdgpu_ring *ring = adev->rings[i];
  891. if (!ring || !ring->fence_drv.initialized)
  892. continue;
  893. r = amdgpu_fence_wait_empty(ring);
  894. if (r) {
  895. /* no need to trigger GPU reset as we are unloading */
  896. amdgpu_fence_driver_force_completion(adev);
  897. }
  898. wake_up_all(&adev->fence_queue);
  899. ring->fence_drv.initialized = false;
  900. }
  901. mutex_unlock(&adev->ring_lock);
  902. }
  903. /**
  904. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  905. *
  906. * @adev: amdgpu device pointer
  907. *
  908. * In case of GPU reset failure make sure no process keep waiting on fence
  909. * that will never complete.
  910. */
  911. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  912. {
  913. int i;
  914. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  915. struct amdgpu_ring *ring = adev->rings[i];
  916. if (!ring || !ring->fence_drv.initialized)
  917. continue;
  918. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  919. }
  920. }
  921. /*
  922. * Fence debugfs
  923. */
  924. #if defined(CONFIG_DEBUG_FS)
  925. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  926. {
  927. struct drm_info_node *node = (struct drm_info_node *)m->private;
  928. struct drm_device *dev = node->minor->dev;
  929. struct amdgpu_device *adev = dev->dev_private;
  930. int i, j;
  931. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  932. struct amdgpu_ring *ring = adev->rings[i];
  933. if (!ring || !ring->fence_drv.initialized)
  934. continue;
  935. amdgpu_fence_process(ring);
  936. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  937. seq_printf(m, "Last signaled fence 0x%016llx\n",
  938. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  939. seq_printf(m, "Last emitted 0x%016llx\n",
  940. ring->fence_drv.sync_seq[i]);
  941. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  942. struct amdgpu_ring *other = adev->rings[j];
  943. if (i != j && other && other->fence_drv.initialized &&
  944. ring->fence_drv.sync_seq[j])
  945. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  946. j, ring->fence_drv.sync_seq[j]);
  947. }
  948. }
  949. return 0;
  950. }
  951. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  952. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  953. };
  954. #endif
  955. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  956. {
  957. #if defined(CONFIG_DEBUG_FS)
  958. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  959. #else
  960. return 0;
  961. #endif
  962. }
  963. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  964. {
  965. return "amdgpu";
  966. }
  967. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  968. {
  969. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  970. return (const char *)fence->ring->name;
  971. }
  972. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  973. {
  974. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  975. }
  976. struct amdgpu_wait_cb {
  977. struct fence_cb base;
  978. struct task_struct *task;
  979. };
  980. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  981. {
  982. struct amdgpu_wait_cb *wait =
  983. container_of(cb, struct amdgpu_wait_cb, base);
  984. wake_up_process(wait->task);
  985. }
  986. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  987. signed long t)
  988. {
  989. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  990. struct amdgpu_device *adev = fence->ring->adev;
  991. struct amdgpu_wait_cb cb;
  992. cb.task = current;
  993. if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
  994. return t;
  995. while (t > 0) {
  996. if (intr)
  997. set_current_state(TASK_INTERRUPTIBLE);
  998. else
  999. set_current_state(TASK_UNINTERRUPTIBLE);
  1000. /*
  1001. * amdgpu_test_signaled must be called after
  1002. * set_current_state to prevent a race with wake_up_process
  1003. */
  1004. if (amdgpu_test_signaled(fence))
  1005. break;
  1006. if (adev->needs_reset) {
  1007. t = -EDEADLK;
  1008. break;
  1009. }
  1010. t = schedule_timeout(t);
  1011. if (t > 0 && intr && signal_pending(current))
  1012. t = -ERESTARTSYS;
  1013. }
  1014. __set_current_state(TASK_RUNNING);
  1015. fence_remove_callback(f, &cb.base);
  1016. return t;
  1017. }
  1018. const struct fence_ops amdgpu_fence_ops = {
  1019. .get_driver_name = amdgpu_fence_get_driver_name,
  1020. .get_timeline_name = amdgpu_fence_get_timeline_name,
  1021. .enable_signaling = amdgpu_fence_enable_signaling,
  1022. .signaled = amdgpu_fence_is_signaled,
  1023. .wait = amdgpu_fence_default_wait,
  1024. .release = NULL,
  1025. };