amdgpu_fence.c 25 KB

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