amdgpu_fence.c 31 KB

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