amdgpu_fence.c 27 KB

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