amdgpu_fence.c 32 KB

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