amdgpu_fence.c 25 KB

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