amdgpu_fence.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. *
  341. * return value:
  342. * 0: seq signaled, and gpu not hang
  343. * -EDEADL: GPU hang detected
  344. * -EINVAL: some paramter is not valid
  345. */
  346. static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
  347. {
  348. struct amdgpu_device *adev = ring->adev;
  349. bool signaled = false;
  350. BUG_ON(!ring);
  351. if (seq > ring->fence_drv.sync_seq[ring->idx])
  352. return -EINVAL;
  353. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  354. return 0;
  355. wait_event(ring->fence_drv.fence_queue, (
  356. (signaled = amdgpu_fence_seq_signaled(ring, seq))
  357. || adev->needs_reset));
  358. if (signaled)
  359. return 0;
  360. else
  361. return -EDEADLK;
  362. }
  363. /**
  364. * amdgpu_fence_wait_next - wait for the next fence to signal
  365. *
  366. * @adev: amdgpu device pointer
  367. * @ring: ring index the fence is associated with
  368. *
  369. * Wait for the next fence on the requested ring to signal (all asics).
  370. * Returns 0 if the next fence has passed, error for all other cases.
  371. * Caller must hold ring lock.
  372. */
  373. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  374. {
  375. uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  376. if (seq >= ring->fence_drv.sync_seq[ring->idx])
  377. return -ENOENT;
  378. return amdgpu_fence_ring_wait_seq(ring, seq);
  379. }
  380. /**
  381. * amdgpu_fence_wait_empty - wait for all fences to signal
  382. *
  383. * @adev: amdgpu device pointer
  384. * @ring: ring index the fence is associated with
  385. *
  386. * Wait for all fences on the requested ring to signal (all asics).
  387. * Returns 0 if the fences have passed, error for all other cases.
  388. * Caller must hold ring lock.
  389. */
  390. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  391. {
  392. uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
  393. if (!seq)
  394. return 0;
  395. return amdgpu_fence_ring_wait_seq(ring, seq);
  396. }
  397. /**
  398. * amdgpu_fence_ref - take a ref on a fence
  399. *
  400. * @fence: amdgpu fence object
  401. *
  402. * Take a reference on a fence (all asics).
  403. * Returns the fence.
  404. */
  405. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  406. {
  407. fence_get(&fence->base);
  408. return fence;
  409. }
  410. /**
  411. * amdgpu_fence_unref - remove a ref on a fence
  412. *
  413. * @fence: amdgpu fence object
  414. *
  415. * Remove a reference on a fence (all asics).
  416. */
  417. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  418. {
  419. struct amdgpu_fence *tmp = *fence;
  420. *fence = NULL;
  421. if (tmp)
  422. fence_put(&tmp->base);
  423. }
  424. /**
  425. * amdgpu_fence_count_emitted - get the count of emitted fences
  426. *
  427. * @ring: ring the fence is associated with
  428. *
  429. * Get the number of fences emitted on the requested ring (all asics).
  430. * Returns the number of emitted fences on the ring. Used by the
  431. * dynpm code to ring track activity.
  432. */
  433. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  434. {
  435. uint64_t emitted;
  436. /* We are not protected by ring lock when reading the last sequence
  437. * but it's ok to report slightly wrong fence count here.
  438. */
  439. amdgpu_fence_process(ring);
  440. emitted = ring->fence_drv.sync_seq[ring->idx]
  441. - atomic64_read(&ring->fence_drv.last_seq);
  442. /* to avoid 32bits warp around */
  443. if (emitted > 0x10000000)
  444. emitted = 0x10000000;
  445. return (unsigned)emitted;
  446. }
  447. /**
  448. * amdgpu_fence_need_sync - do we need a semaphore
  449. *
  450. * @fence: amdgpu fence object
  451. * @dst_ring: which ring to check against
  452. *
  453. * Check if the fence needs to be synced against another ring
  454. * (all asics). If so, we need to emit a semaphore.
  455. * Returns true if we need to sync with another ring, false if
  456. * not.
  457. */
  458. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  459. struct amdgpu_ring *dst_ring)
  460. {
  461. struct amdgpu_fence_driver *fdrv;
  462. if (!fence)
  463. return false;
  464. if (fence->ring == dst_ring)
  465. return false;
  466. /* we are protected by the ring mutex */
  467. fdrv = &dst_ring->fence_drv;
  468. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  469. return false;
  470. return true;
  471. }
  472. /**
  473. * amdgpu_fence_note_sync - record the sync point
  474. *
  475. * @fence: amdgpu fence object
  476. * @dst_ring: which ring to check against
  477. *
  478. * Note the sequence number at which point the fence will
  479. * be synced with the requested ring (all asics).
  480. */
  481. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  482. struct amdgpu_ring *dst_ring)
  483. {
  484. struct amdgpu_fence_driver *dst, *src;
  485. unsigned i;
  486. if (!fence)
  487. return;
  488. if (fence->ring == dst_ring)
  489. return;
  490. /* we are protected by the ring mutex */
  491. src = &fence->ring->fence_drv;
  492. dst = &dst_ring->fence_drv;
  493. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  494. if (i == dst_ring->idx)
  495. continue;
  496. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  497. }
  498. }
  499. /**
  500. * amdgpu_fence_driver_start_ring - make the fence driver
  501. * ready for use on the requested ring.
  502. *
  503. * @ring: ring to start the fence driver on
  504. * @irq_src: interrupt source to use for this ring
  505. * @irq_type: interrupt type to use for this ring
  506. *
  507. * Make the fence driver ready for processing (all asics).
  508. * Not all asics have all rings, so each asic will only
  509. * start the fence driver on the rings it has.
  510. * Returns 0 for success, errors for failure.
  511. */
  512. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  513. struct amdgpu_irq_src *irq_src,
  514. unsigned irq_type)
  515. {
  516. struct amdgpu_device *adev = ring->adev;
  517. uint64_t index;
  518. if (ring != &adev->uvd.ring) {
  519. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  520. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  521. } else {
  522. /* put fence directly behind firmware */
  523. index = ALIGN(adev->uvd.fw->size, 8);
  524. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  525. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  526. }
  527. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  528. amdgpu_irq_get(adev, irq_src, irq_type);
  529. ring->fence_drv.irq_src = irq_src;
  530. ring->fence_drv.irq_type = irq_type;
  531. ring->fence_drv.initialized = true;
  532. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  533. "cpu addr 0x%p\n", ring->idx,
  534. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  535. return 0;
  536. }
  537. /**
  538. * amdgpu_fence_driver_init_ring - init the fence driver
  539. * for the requested ring.
  540. *
  541. * @ring: ring to init the fence driver on
  542. *
  543. * Init the fence driver for the requested ring (all asics).
  544. * Helper function for amdgpu_fence_driver_init().
  545. */
  546. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  547. {
  548. int i;
  549. ring->fence_drv.cpu_addr = NULL;
  550. ring->fence_drv.gpu_addr = 0;
  551. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  552. ring->fence_drv.sync_seq[i] = 0;
  553. atomic64_set(&ring->fence_drv.last_seq, 0);
  554. ring->fence_drv.initialized = false;
  555. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  556. amdgpu_fence_check_lockup);
  557. ring->fence_drv.ring = ring;
  558. if (amdgpu_enable_scheduler) {
  559. ring->scheduler = amd_sched_create(&amdgpu_sched_ops,
  560. ring->idx,
  561. amdgpu_sched_hw_submission,
  562. (void *)ring->adev);
  563. if (!ring->scheduler)
  564. DRM_ERROR("Failed to create scheduler on ring %d.\n",
  565. ring->idx);
  566. }
  567. }
  568. /**
  569. * amdgpu_fence_driver_init - init the fence driver
  570. * for all possible rings.
  571. *
  572. * @adev: amdgpu device pointer
  573. *
  574. * Init the fence driver for all possible rings (all asics).
  575. * Not all asics have all rings, so each asic will only
  576. * start the fence driver on the rings it has using
  577. * amdgpu_fence_driver_start_ring().
  578. * Returns 0 for success.
  579. */
  580. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  581. {
  582. if (amdgpu_debugfs_fence_init(adev))
  583. dev_err(adev->dev, "fence debugfs file creation failed\n");
  584. return 0;
  585. }
  586. /**
  587. * amdgpu_fence_driver_fini - tear down the fence driver
  588. * for all possible rings.
  589. *
  590. * @adev: amdgpu device pointer
  591. *
  592. * Tear down the fence driver for all possible rings (all asics).
  593. */
  594. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  595. {
  596. int i, r;
  597. mutex_lock(&adev->ring_lock);
  598. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  599. struct amdgpu_ring *ring = adev->rings[i];
  600. if (!ring || !ring->fence_drv.initialized)
  601. continue;
  602. r = amdgpu_fence_wait_empty(ring);
  603. if (r) {
  604. /* no need to trigger GPU reset as we are unloading */
  605. amdgpu_fence_driver_force_completion(adev);
  606. }
  607. wake_up_all(&ring->fence_drv.fence_queue);
  608. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  609. ring->fence_drv.irq_type);
  610. if (ring->scheduler)
  611. amd_sched_destroy(ring->scheduler);
  612. ring->fence_drv.initialized = false;
  613. }
  614. mutex_unlock(&adev->ring_lock);
  615. }
  616. /**
  617. * amdgpu_fence_driver_suspend - suspend the fence driver
  618. * for all possible rings.
  619. *
  620. * @adev: amdgpu device pointer
  621. *
  622. * Suspend the fence driver for all possible rings (all asics).
  623. */
  624. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  625. {
  626. int i, r;
  627. mutex_lock(&adev->ring_lock);
  628. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  629. struct amdgpu_ring *ring = adev->rings[i];
  630. if (!ring || !ring->fence_drv.initialized)
  631. continue;
  632. /* wait for gpu to finish processing current batch */
  633. r = amdgpu_fence_wait_empty(ring);
  634. if (r) {
  635. /* delay GPU reset to resume */
  636. amdgpu_fence_driver_force_completion(adev);
  637. }
  638. /* disable the interrupt */
  639. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  640. ring->fence_drv.irq_type);
  641. }
  642. mutex_unlock(&adev->ring_lock);
  643. }
  644. /**
  645. * amdgpu_fence_driver_resume - resume the fence driver
  646. * for all possible rings.
  647. *
  648. * @adev: amdgpu device pointer
  649. *
  650. * Resume the fence driver for all possible rings (all asics).
  651. * Not all asics have all rings, so each asic will only
  652. * start the fence driver on the rings it has using
  653. * amdgpu_fence_driver_start_ring().
  654. * Returns 0 for success.
  655. */
  656. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  657. {
  658. int i;
  659. mutex_lock(&adev->ring_lock);
  660. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  661. struct amdgpu_ring *ring = adev->rings[i];
  662. if (!ring || !ring->fence_drv.initialized)
  663. continue;
  664. /* enable the interrupt */
  665. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  666. ring->fence_drv.irq_type);
  667. }
  668. mutex_unlock(&adev->ring_lock);
  669. }
  670. /**
  671. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  672. *
  673. * @adev: amdgpu device pointer
  674. *
  675. * In case of GPU reset failure make sure no process keep waiting on fence
  676. * that will never complete.
  677. */
  678. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  679. {
  680. int i;
  681. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  682. struct amdgpu_ring *ring = adev->rings[i];
  683. if (!ring || !ring->fence_drv.initialized)
  684. continue;
  685. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  686. }
  687. }
  688. /*
  689. * Fence debugfs
  690. */
  691. #if defined(CONFIG_DEBUG_FS)
  692. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  693. {
  694. struct drm_info_node *node = (struct drm_info_node *)m->private;
  695. struct drm_device *dev = node->minor->dev;
  696. struct amdgpu_device *adev = dev->dev_private;
  697. int i, j;
  698. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  699. struct amdgpu_ring *ring = adev->rings[i];
  700. if (!ring || !ring->fence_drv.initialized)
  701. continue;
  702. amdgpu_fence_process(ring);
  703. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  704. seq_printf(m, "Last signaled fence 0x%016llx\n",
  705. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  706. seq_printf(m, "Last emitted 0x%016llx\n",
  707. ring->fence_drv.sync_seq[i]);
  708. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  709. struct amdgpu_ring *other = adev->rings[j];
  710. if (i != j && other && other->fence_drv.initialized &&
  711. ring->fence_drv.sync_seq[j])
  712. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  713. j, ring->fence_drv.sync_seq[j]);
  714. }
  715. }
  716. return 0;
  717. }
  718. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  719. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  720. };
  721. #endif
  722. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  723. {
  724. #if defined(CONFIG_DEBUG_FS)
  725. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  726. #else
  727. return 0;
  728. #endif
  729. }
  730. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  731. {
  732. return "amdgpu";
  733. }
  734. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  735. {
  736. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  737. return (const char *)fence->ring->name;
  738. }
  739. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  740. {
  741. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  742. }
  743. static bool amdgpu_test_signaled_any(struct fence **fences, uint32_t count)
  744. {
  745. int idx;
  746. struct fence *fence;
  747. for (idx = 0; idx < count; ++idx) {
  748. fence = fences[idx];
  749. if (fence) {
  750. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. struct amdgpu_wait_cb {
  757. struct fence_cb base;
  758. struct task_struct *task;
  759. };
  760. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  761. {
  762. struct amdgpu_wait_cb *wait =
  763. container_of(cb, struct amdgpu_wait_cb, base);
  764. wake_up_process(wait->task);
  765. }
  766. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  767. signed long t)
  768. {
  769. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  770. struct amdgpu_device *adev = fence->ring->adev;
  771. return amdgpu_fence_wait_any(adev, &f, 1, intr, t);
  772. }
  773. /**
  774. * Wait the fence array with timeout
  775. *
  776. * @adev: amdgpu device
  777. * @array: the fence array with amdgpu fence pointer
  778. * @count: the number of the fence array
  779. * @intr: when sleep, set the current task interruptable or not
  780. * @t: timeout to wait
  781. *
  782. * It will return when any fence is signaled or timeout.
  783. */
  784. signed long amdgpu_fence_wait_any(struct amdgpu_device *adev,
  785. struct fence **array, uint32_t count,
  786. bool intr, signed long t)
  787. {
  788. struct amdgpu_wait_cb *cb;
  789. struct fence *fence;
  790. unsigned idx;
  791. BUG_ON(!array);
  792. cb = kcalloc(count, sizeof(struct amdgpu_wait_cb), GFP_KERNEL);
  793. if (cb == NULL) {
  794. t = -ENOMEM;
  795. goto err_free_cb;
  796. }
  797. for (idx = 0; idx < count; ++idx) {
  798. fence = array[idx];
  799. if (fence) {
  800. cb[idx].task = current;
  801. if (fence_add_callback(fence,
  802. &cb[idx].base, amdgpu_fence_wait_cb)) {
  803. /* The fence is already signaled */
  804. goto fence_rm_cb;
  805. }
  806. }
  807. }
  808. while (t > 0) {
  809. if (intr)
  810. set_current_state(TASK_INTERRUPTIBLE);
  811. else
  812. set_current_state(TASK_UNINTERRUPTIBLE);
  813. /*
  814. * amdgpu_test_signaled_any must be called after
  815. * set_current_state to prevent a race with wake_up_process
  816. */
  817. if (amdgpu_test_signaled_any(array, count))
  818. break;
  819. if (adev->needs_reset) {
  820. t = -EDEADLK;
  821. break;
  822. }
  823. t = schedule_timeout(t);
  824. if (t > 0 && intr && signal_pending(current))
  825. t = -ERESTARTSYS;
  826. }
  827. __set_current_state(TASK_RUNNING);
  828. fence_rm_cb:
  829. for (idx = 0; idx < count; ++idx) {
  830. fence = array[idx];
  831. if (fence && cb[idx].base.func)
  832. fence_remove_callback(fence, &cb[idx].base);
  833. }
  834. err_free_cb:
  835. kfree(cb);
  836. return t;
  837. }
  838. const struct fence_ops amdgpu_fence_ops = {
  839. .get_driver_name = amdgpu_fence_get_driver_name,
  840. .get_timeline_name = amdgpu_fence_get_timeline_name,
  841. .enable_signaling = amdgpu_fence_enable_signaling,
  842. .signaled = amdgpu_fence_is_signaled,
  843. .wait = amdgpu_fence_default_wait,
  844. .release = NULL,
  845. };