amdgpu_fence.c 32 KB

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