amdgpu_fence.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  557. long r;
  558. seq[fence->ring->idx] = fence->seq;
  559. r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  560. if (r < 0) {
  561. return r;
  562. }
  563. r = fence_signal(&fence->base);
  564. if (!r)
  565. FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
  566. return 0;
  567. }
  568. /**
  569. * amdgpu_fence_wait_any - wait for a fence to signal on any ring
  570. *
  571. * @adev: amdgpu device pointer
  572. * @fences: amdgpu fence object(s)
  573. * @intr: use interruptable sleep
  574. *
  575. * Wait for any requested fence to signal (all asics). Fence
  576. * array is indexed by ring id. @intr selects whether to use
  577. * interruptable (true) or non-interruptable (false) sleep when
  578. * waiting for the fences. Used by the suballocator.
  579. * Returns 0 if any fence has passed, error for all other cases.
  580. */
  581. int amdgpu_fence_wait_any(struct amdgpu_device *adev,
  582. struct amdgpu_fence **fences,
  583. bool intr)
  584. {
  585. uint64_t seq[AMDGPU_MAX_RINGS];
  586. unsigned i, num_rings = 0;
  587. long r;
  588. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  589. seq[i] = 0;
  590. if (!fences[i]) {
  591. continue;
  592. }
  593. seq[i] = fences[i]->seq;
  594. ++num_rings;
  595. }
  596. /* nothing to wait for ? */
  597. if (num_rings == 0)
  598. return -ENOENT;
  599. r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  600. if (r < 0) {
  601. return r;
  602. }
  603. return 0;
  604. }
  605. /**
  606. * amdgpu_fence_wait_next - wait for the next fence to signal
  607. *
  608. * @adev: amdgpu device pointer
  609. * @ring: ring index the fence is associated with
  610. *
  611. * Wait for the next fence on the requested ring to signal (all asics).
  612. * Returns 0 if the next fence has passed, error for all other cases.
  613. * Caller must hold ring lock.
  614. */
  615. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  616. {
  617. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  618. long r;
  619. seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  620. if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
  621. /* nothing to wait for, last_seq is
  622. already the last emited fence */
  623. return -ENOENT;
  624. }
  625. r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  626. if (r < 0)
  627. return r;
  628. return 0;
  629. }
  630. /**
  631. * amdgpu_fence_wait_empty - wait for all fences to signal
  632. *
  633. * @adev: amdgpu device pointer
  634. * @ring: ring index the fence is associated with
  635. *
  636. * Wait for all fences on the requested ring to signal (all asics).
  637. * Returns 0 if the fences have passed, error for all other cases.
  638. * Caller must hold ring lock.
  639. */
  640. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  641. {
  642. struct amdgpu_device *adev = ring->adev;
  643. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  644. long r;
  645. seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
  646. if (!seq[ring->idx])
  647. return 0;
  648. r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  649. if (r < 0) {
  650. if (r == -EDEADLK)
  651. return -EDEADLK;
  652. dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
  653. ring->idx, r);
  654. }
  655. return 0;
  656. }
  657. /**
  658. * amdgpu_fence_ref - take a ref on a fence
  659. *
  660. * @fence: amdgpu fence object
  661. *
  662. * Take a reference on a fence (all asics).
  663. * Returns the fence.
  664. */
  665. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  666. {
  667. fence_get(&fence->base);
  668. return fence;
  669. }
  670. /**
  671. * amdgpu_fence_unref - remove a ref on a fence
  672. *
  673. * @fence: amdgpu fence object
  674. *
  675. * Remove a reference on a fence (all asics).
  676. */
  677. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  678. {
  679. struct amdgpu_fence *tmp = *fence;
  680. *fence = NULL;
  681. if (tmp)
  682. fence_put(&tmp->base);
  683. }
  684. /**
  685. * amdgpu_fence_count_emitted - get the count of emitted fences
  686. *
  687. * @ring: ring the fence is associated with
  688. *
  689. * Get the number of fences emitted on the requested ring (all asics).
  690. * Returns the number of emitted fences on the ring. Used by the
  691. * dynpm code to ring track activity.
  692. */
  693. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  694. {
  695. uint64_t emitted;
  696. /* We are not protected by ring lock when reading the last sequence
  697. * but it's ok to report slightly wrong fence count here.
  698. */
  699. amdgpu_fence_process(ring);
  700. emitted = ring->fence_drv.sync_seq[ring->idx]
  701. - atomic64_read(&ring->fence_drv.last_seq);
  702. /* to avoid 32bits warp around */
  703. if (emitted > 0x10000000)
  704. emitted = 0x10000000;
  705. return (unsigned)emitted;
  706. }
  707. /**
  708. * amdgpu_fence_need_sync - do we need a semaphore
  709. *
  710. * @fence: amdgpu fence object
  711. * @dst_ring: which ring to check against
  712. *
  713. * Check if the fence needs to be synced against another ring
  714. * (all asics). If so, we need to emit a semaphore.
  715. * Returns true if we need to sync with another ring, false if
  716. * not.
  717. */
  718. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  719. struct amdgpu_ring *dst_ring)
  720. {
  721. struct amdgpu_fence_driver *fdrv;
  722. if (!fence)
  723. return false;
  724. if (fence->ring == dst_ring)
  725. return false;
  726. /* we are protected by the ring mutex */
  727. fdrv = &dst_ring->fence_drv;
  728. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  729. return false;
  730. return true;
  731. }
  732. /**
  733. * amdgpu_fence_note_sync - record the sync point
  734. *
  735. * @fence: amdgpu fence object
  736. * @dst_ring: which ring to check against
  737. *
  738. * Note the sequence number at which point the fence will
  739. * be synced with the requested ring (all asics).
  740. */
  741. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  742. struct amdgpu_ring *dst_ring)
  743. {
  744. struct amdgpu_fence_driver *dst, *src;
  745. unsigned i;
  746. if (!fence)
  747. return;
  748. if (fence->ring == dst_ring)
  749. return;
  750. /* we are protected by the ring mutex */
  751. src = &fence->ring->fence_drv;
  752. dst = &dst_ring->fence_drv;
  753. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  754. if (i == dst_ring->idx)
  755. continue;
  756. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  757. }
  758. }
  759. /**
  760. * amdgpu_fence_driver_start_ring - make the fence driver
  761. * ready for use on the requested ring.
  762. *
  763. * @ring: ring to start the fence driver on
  764. * @irq_src: interrupt source to use for this ring
  765. * @irq_type: interrupt type to use for this ring
  766. *
  767. * Make the fence driver ready for processing (all asics).
  768. * Not all asics have all rings, so each asic will only
  769. * start the fence driver on the rings it has.
  770. * Returns 0 for success, errors for failure.
  771. */
  772. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  773. struct amdgpu_irq_src *irq_src,
  774. unsigned irq_type)
  775. {
  776. struct amdgpu_device *adev = ring->adev;
  777. uint64_t index;
  778. if (ring != &adev->uvd.ring) {
  779. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  780. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  781. } else {
  782. /* put fence directly behind firmware */
  783. index = ALIGN(adev->uvd.fw->size, 8);
  784. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  785. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  786. }
  787. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  788. amdgpu_irq_get(adev, irq_src, irq_type);
  789. ring->fence_drv.irq_src = irq_src;
  790. ring->fence_drv.irq_type = irq_type;
  791. ring->fence_drv.initialized = true;
  792. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  793. "cpu addr 0x%p\n", ring->idx,
  794. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  795. return 0;
  796. }
  797. /**
  798. * amdgpu_fence_driver_init_ring - init the fence driver
  799. * for the requested ring.
  800. *
  801. * @ring: ring to init the fence driver on
  802. *
  803. * Init the fence driver for the requested ring (all asics).
  804. * Helper function for amdgpu_fence_driver_init().
  805. */
  806. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  807. {
  808. int i;
  809. ring->fence_drv.cpu_addr = NULL;
  810. ring->fence_drv.gpu_addr = 0;
  811. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  812. ring->fence_drv.sync_seq[i] = 0;
  813. atomic64_set(&ring->fence_drv.last_seq, 0);
  814. ring->fence_drv.initialized = false;
  815. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  816. amdgpu_fence_check_lockup);
  817. ring->fence_drv.ring = ring;
  818. if (amdgpu_enable_scheduler) {
  819. ring->scheduler = amd_sched_create((void *)ring->adev,
  820. &amdgpu_sched_ops,
  821. ring->idx, 5, 0);
  822. if (!ring->scheduler)
  823. DRM_ERROR("Failed to create scheduler on ring %d.\n",
  824. ring->idx);
  825. }
  826. }
  827. /**
  828. * amdgpu_fence_driver_init - init the fence driver
  829. * for all possible rings.
  830. *
  831. * @adev: amdgpu device pointer
  832. *
  833. * Init the fence driver for all possible rings (all asics).
  834. * Not all asics have all rings, so each asic will only
  835. * start the fence driver on the rings it has using
  836. * amdgpu_fence_driver_start_ring().
  837. * Returns 0 for success.
  838. */
  839. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  840. {
  841. init_waitqueue_head(&adev->fence_queue);
  842. if (amdgpu_debugfs_fence_init(adev))
  843. dev_err(adev->dev, "fence debugfs file creation failed\n");
  844. return 0;
  845. }
  846. /**
  847. * amdgpu_fence_driver_fini - tear down the fence driver
  848. * for all possible rings.
  849. *
  850. * @adev: amdgpu device pointer
  851. *
  852. * Tear down the fence driver for all possible rings (all asics).
  853. */
  854. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  855. {
  856. int i, r;
  857. mutex_lock(&adev->ring_lock);
  858. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  859. struct amdgpu_ring *ring = adev->rings[i];
  860. if (!ring || !ring->fence_drv.initialized)
  861. continue;
  862. r = amdgpu_fence_wait_empty(ring);
  863. if (r) {
  864. /* no need to trigger GPU reset as we are unloading */
  865. amdgpu_fence_driver_force_completion(adev);
  866. }
  867. wake_up_all(&adev->fence_queue);
  868. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  869. ring->fence_drv.irq_type);
  870. if (ring->scheduler)
  871. amd_sched_destroy(ring->scheduler);
  872. ring->fence_drv.initialized = false;
  873. }
  874. mutex_unlock(&adev->ring_lock);
  875. }
  876. /**
  877. * amdgpu_fence_driver_suspend - suspend the fence driver
  878. * for all possible rings.
  879. *
  880. * @adev: amdgpu device pointer
  881. *
  882. * Suspend the fence driver for all possible rings (all asics).
  883. */
  884. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  885. {
  886. int i, r;
  887. mutex_lock(&adev->ring_lock);
  888. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  889. struct amdgpu_ring *ring = adev->rings[i];
  890. if (!ring || !ring->fence_drv.initialized)
  891. continue;
  892. /* wait for gpu to finish processing current batch */
  893. r = amdgpu_fence_wait_empty(ring);
  894. if (r) {
  895. /* delay GPU reset to resume */
  896. amdgpu_fence_driver_force_completion(adev);
  897. }
  898. /* disable the interrupt */
  899. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  900. ring->fence_drv.irq_type);
  901. }
  902. mutex_unlock(&adev->ring_lock);
  903. }
  904. /**
  905. * amdgpu_fence_driver_resume - resume the fence driver
  906. * for all possible rings.
  907. *
  908. * @adev: amdgpu device pointer
  909. *
  910. * Resume the fence driver for all possible rings (all asics).
  911. * Not all asics have all rings, so each asic will only
  912. * start the fence driver on the rings it has using
  913. * amdgpu_fence_driver_start_ring().
  914. * Returns 0 for success.
  915. */
  916. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  917. {
  918. int i;
  919. mutex_lock(&adev->ring_lock);
  920. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  921. struct amdgpu_ring *ring = adev->rings[i];
  922. if (!ring || !ring->fence_drv.initialized)
  923. continue;
  924. /* enable the interrupt */
  925. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  926. ring->fence_drv.irq_type);
  927. }
  928. mutex_unlock(&adev->ring_lock);
  929. }
  930. /**
  931. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  932. *
  933. * @adev: amdgpu device pointer
  934. *
  935. * In case of GPU reset failure make sure no process keep waiting on fence
  936. * that will never complete.
  937. */
  938. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  939. {
  940. int i;
  941. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  942. struct amdgpu_ring *ring = adev->rings[i];
  943. if (!ring || !ring->fence_drv.initialized)
  944. continue;
  945. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  946. }
  947. }
  948. /*
  949. * Fence debugfs
  950. */
  951. #if defined(CONFIG_DEBUG_FS)
  952. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  953. {
  954. struct drm_info_node *node = (struct drm_info_node *)m->private;
  955. struct drm_device *dev = node->minor->dev;
  956. struct amdgpu_device *adev = dev->dev_private;
  957. int i, j;
  958. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  959. struct amdgpu_ring *ring = adev->rings[i];
  960. if (!ring || !ring->fence_drv.initialized)
  961. continue;
  962. amdgpu_fence_process(ring);
  963. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  964. seq_printf(m, "Last signaled fence 0x%016llx\n",
  965. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  966. seq_printf(m, "Last emitted 0x%016llx\n",
  967. ring->fence_drv.sync_seq[i]);
  968. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  969. struct amdgpu_ring *other = adev->rings[j];
  970. if (i != j && other && other->fence_drv.initialized &&
  971. ring->fence_drv.sync_seq[j])
  972. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  973. j, ring->fence_drv.sync_seq[j]);
  974. }
  975. }
  976. return 0;
  977. }
  978. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  979. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  980. };
  981. #endif
  982. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  983. {
  984. #if defined(CONFIG_DEBUG_FS)
  985. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  986. #else
  987. return 0;
  988. #endif
  989. }
  990. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  991. {
  992. return "amdgpu";
  993. }
  994. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  995. {
  996. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  997. return (const char *)fence->ring->name;
  998. }
  999. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  1000. {
  1001. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  1002. }
  1003. struct amdgpu_wait_cb {
  1004. struct fence_cb base;
  1005. struct task_struct *task;
  1006. };
  1007. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  1008. {
  1009. struct amdgpu_wait_cb *wait =
  1010. container_of(cb, struct amdgpu_wait_cb, base);
  1011. wake_up_process(wait->task);
  1012. }
  1013. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  1014. signed long t)
  1015. {
  1016. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  1017. struct amdgpu_device *adev = fence->ring->adev;
  1018. struct amdgpu_wait_cb cb;
  1019. cb.task = current;
  1020. if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
  1021. return t;
  1022. while (t > 0) {
  1023. if (intr)
  1024. set_current_state(TASK_INTERRUPTIBLE);
  1025. else
  1026. set_current_state(TASK_UNINTERRUPTIBLE);
  1027. /*
  1028. * amdgpu_test_signaled must be called after
  1029. * set_current_state to prevent a race with wake_up_process
  1030. */
  1031. if (amdgpu_test_signaled(fence))
  1032. break;
  1033. if (adev->needs_reset) {
  1034. t = -EDEADLK;
  1035. break;
  1036. }
  1037. t = schedule_timeout(t);
  1038. if (t > 0 && intr && signal_pending(current))
  1039. t = -ERESTARTSYS;
  1040. }
  1041. __set_current_state(TASK_RUNNING);
  1042. fence_remove_callback(f, &cb.base);
  1043. return t;
  1044. }
  1045. const struct fence_ops amdgpu_fence_ops = {
  1046. .get_driver_name = amdgpu_fence_get_driver_name,
  1047. .get_timeline_name = amdgpu_fence_get_timeline_name,
  1048. .enable_signaling = amdgpu_fence_enable_signaling,
  1049. .signaled = amdgpu_fence_is_signaled,
  1050. .wait = amdgpu_fence_default_wait,
  1051. .release = NULL,
  1052. };