amdgpu_fence.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. }
  800. /**
  801. * amdgpu_fence_driver_init - init the fence driver
  802. * for all possible rings.
  803. *
  804. * @adev: amdgpu device pointer
  805. *
  806. * Init the fence driver for all possible rings (all asics).
  807. * Not all asics have all rings, so each asic will only
  808. * start the fence driver on the rings it has using
  809. * amdgpu_fence_driver_start_ring().
  810. * Returns 0 for success.
  811. */
  812. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  813. {
  814. init_waitqueue_head(&adev->fence_queue);
  815. if (amdgpu_debugfs_fence_init(adev))
  816. dev_err(adev->dev, "fence debugfs file creation failed\n");
  817. return 0;
  818. }
  819. /**
  820. * amdgpu_fence_driver_fini - tear down the fence driver
  821. * for all possible rings.
  822. *
  823. * @adev: amdgpu device pointer
  824. *
  825. * Tear down the fence driver for all possible rings (all asics).
  826. */
  827. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  828. {
  829. int i, r;
  830. mutex_lock(&adev->ring_lock);
  831. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  832. struct amdgpu_ring *ring = adev->rings[i];
  833. if (!ring || !ring->fence_drv.initialized)
  834. continue;
  835. r = amdgpu_fence_wait_empty(ring);
  836. if (r) {
  837. /* no need to trigger GPU reset as we are unloading */
  838. amdgpu_fence_driver_force_completion(adev);
  839. }
  840. wake_up_all(&adev->fence_queue);
  841. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  842. ring->fence_drv.irq_type);
  843. ring->fence_drv.initialized = false;
  844. }
  845. mutex_unlock(&adev->ring_lock);
  846. }
  847. /**
  848. * amdgpu_fence_driver_suspend - suspend the fence driver
  849. * for all possible rings.
  850. *
  851. * @adev: amdgpu device pointer
  852. *
  853. * Suspend the fence driver for all possible rings (all asics).
  854. */
  855. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  856. {
  857. int i, r;
  858. mutex_lock(&adev->ring_lock);
  859. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  860. struct amdgpu_ring *ring = adev->rings[i];
  861. if (!ring || !ring->fence_drv.initialized)
  862. continue;
  863. /* wait for gpu to finish processing current batch */
  864. r = amdgpu_fence_wait_empty(ring);
  865. if (r) {
  866. /* delay GPU reset to resume */
  867. amdgpu_fence_driver_force_completion(adev);
  868. }
  869. /* disable the interrupt */
  870. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  871. ring->fence_drv.irq_type);
  872. }
  873. mutex_unlock(&adev->ring_lock);
  874. }
  875. /**
  876. * amdgpu_fence_driver_resume - resume the fence driver
  877. * for all possible rings.
  878. *
  879. * @adev: amdgpu device pointer
  880. *
  881. * Resume the fence driver for all possible rings (all asics).
  882. * Not all asics have all rings, so each asic will only
  883. * start the fence driver on the rings it has using
  884. * amdgpu_fence_driver_start_ring().
  885. * Returns 0 for success.
  886. */
  887. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  888. {
  889. int i;
  890. mutex_lock(&adev->ring_lock);
  891. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  892. struct amdgpu_ring *ring = adev->rings[i];
  893. if (!ring || !ring->fence_drv.initialized)
  894. continue;
  895. /* enable the interrupt */
  896. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  897. ring->fence_drv.irq_type);
  898. }
  899. mutex_unlock(&adev->ring_lock);
  900. }
  901. /**
  902. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  903. *
  904. * @adev: amdgpu device pointer
  905. *
  906. * In case of GPU reset failure make sure no process keep waiting on fence
  907. * that will never complete.
  908. */
  909. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  910. {
  911. int i;
  912. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  913. struct amdgpu_ring *ring = adev->rings[i];
  914. if (!ring || !ring->fence_drv.initialized)
  915. continue;
  916. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  917. }
  918. }
  919. /*
  920. * Fence debugfs
  921. */
  922. #if defined(CONFIG_DEBUG_FS)
  923. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  924. {
  925. struct drm_info_node *node = (struct drm_info_node *)m->private;
  926. struct drm_device *dev = node->minor->dev;
  927. struct amdgpu_device *adev = dev->dev_private;
  928. int i, j;
  929. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  930. struct amdgpu_ring *ring = adev->rings[i];
  931. if (!ring || !ring->fence_drv.initialized)
  932. continue;
  933. amdgpu_fence_process(ring);
  934. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  935. seq_printf(m, "Last signaled fence 0x%016llx\n",
  936. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  937. seq_printf(m, "Last emitted 0x%016llx\n",
  938. ring->fence_drv.sync_seq[i]);
  939. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  940. struct amdgpu_ring *other = adev->rings[j];
  941. if (i != j && other && other->fence_drv.initialized &&
  942. ring->fence_drv.sync_seq[j])
  943. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  944. j, ring->fence_drv.sync_seq[j]);
  945. }
  946. }
  947. return 0;
  948. }
  949. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  950. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  951. };
  952. #endif
  953. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  954. {
  955. #if defined(CONFIG_DEBUG_FS)
  956. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  957. #else
  958. return 0;
  959. #endif
  960. }
  961. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  962. {
  963. return "amdgpu";
  964. }
  965. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  966. {
  967. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  968. return (const char *)fence->ring->name;
  969. }
  970. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  971. {
  972. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  973. }
  974. struct amdgpu_wait_cb {
  975. struct fence_cb base;
  976. struct task_struct *task;
  977. };
  978. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  979. {
  980. struct amdgpu_wait_cb *wait =
  981. container_of(cb, struct amdgpu_wait_cb, base);
  982. wake_up_process(wait->task);
  983. }
  984. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  985. signed long t)
  986. {
  987. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  988. struct amdgpu_device *adev = fence->ring->adev;
  989. struct amdgpu_wait_cb cb;
  990. cb.task = current;
  991. if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
  992. return t;
  993. while (t > 0) {
  994. if (intr)
  995. set_current_state(TASK_INTERRUPTIBLE);
  996. else
  997. set_current_state(TASK_UNINTERRUPTIBLE);
  998. /*
  999. * amdgpu_test_signaled must be called after
  1000. * set_current_state to prevent a race with wake_up_process
  1001. */
  1002. if (amdgpu_test_signaled(fence))
  1003. break;
  1004. if (adev->needs_reset) {
  1005. t = -EDEADLK;
  1006. break;
  1007. }
  1008. t = schedule_timeout(t);
  1009. if (t > 0 && intr && signal_pending(current))
  1010. t = -ERESTARTSYS;
  1011. }
  1012. __set_current_state(TASK_RUNNING);
  1013. fence_remove_callback(f, &cb.base);
  1014. return t;
  1015. }
  1016. const struct fence_ops amdgpu_fence_ops = {
  1017. .get_driver_name = amdgpu_fence_get_driver_name,
  1018. .get_timeline_name = amdgpu_fence_get_timeline_name,
  1019. .enable_signaling = amdgpu_fence_enable_signaling,
  1020. .signaled = amdgpu_fence_is_signaled,
  1021. .wait = amdgpu_fence_default_wait,
  1022. .release = NULL,
  1023. };