amdgpu_fence.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. return 0;
  127. }
  128. /**
  129. * amdgpu_fence_activity - check for fence activity
  130. *
  131. * @ring: pointer to struct amdgpu_ring
  132. *
  133. * Checks the current fence value and calculates the last
  134. * signalled fence value. Returns true if activity occured
  135. * on the ring, and the fence_queue should be waken up.
  136. */
  137. static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
  138. {
  139. uint64_t seq, last_seq, last_emitted;
  140. unsigned count_loop = 0;
  141. bool wake = false;
  142. /* Note there is a scenario here for an infinite loop but it's
  143. * very unlikely to happen. For it to happen, the current polling
  144. * process need to be interrupted by another process and another
  145. * process needs to update the last_seq btw the atomic read and
  146. * xchg of the current process.
  147. *
  148. * More over for this to go in infinite loop there need to be
  149. * continuously new fence signaled ie amdgpu_fence_read needs
  150. * to return a different value each time for both the currently
  151. * polling process and the other process that xchg the last_seq
  152. * btw atomic read and xchg of the current process. And the
  153. * value the other process set as last seq must be higher than
  154. * the seq value we just read. Which means that current process
  155. * need to be interrupted after amdgpu_fence_read and before
  156. * atomic xchg.
  157. *
  158. * To be even more safe we count the number of time we loop and
  159. * we bail after 10 loop just accepting the fact that we might
  160. * have temporarly set the last_seq not to the true real last
  161. * seq but to an older one.
  162. */
  163. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  164. do {
  165. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  166. seq = amdgpu_fence_read(ring);
  167. seq |= last_seq & 0xffffffff00000000LL;
  168. if (seq < last_seq) {
  169. seq &= 0xffffffff;
  170. seq |= last_emitted & 0xffffffff00000000LL;
  171. }
  172. if (seq <= last_seq || seq > last_emitted) {
  173. break;
  174. }
  175. /* If we loop over we don't want to return without
  176. * checking if a fence is signaled as it means that the
  177. * seq we just read is different from the previous on.
  178. */
  179. wake = true;
  180. last_seq = seq;
  181. if ((count_loop++) > 10) {
  182. /* We looped over too many time leave with the
  183. * fact that we might have set an older fence
  184. * seq then the current real last seq as signaled
  185. * by the hw.
  186. */
  187. break;
  188. }
  189. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  190. if (seq < last_emitted)
  191. amdgpu_fence_schedule_check(ring);
  192. return wake;
  193. }
  194. /**
  195. * amdgpu_fence_check_lockup - check for hardware lockup
  196. *
  197. * @work: delayed work item
  198. *
  199. * Checks for fence activity and if there is none probe
  200. * the hardware if a lockup occured.
  201. */
  202. static void amdgpu_fence_check_lockup(struct work_struct *work)
  203. {
  204. struct amdgpu_fence_driver *fence_drv;
  205. struct amdgpu_ring *ring;
  206. fence_drv = container_of(work, struct amdgpu_fence_driver,
  207. lockup_work.work);
  208. ring = fence_drv->ring;
  209. if (amdgpu_fence_activity(ring))
  210. wake_up_all(&ring->fence_drv.fence_queue);
  211. }
  212. /**
  213. * amdgpu_fence_process - process a fence
  214. *
  215. * @adev: amdgpu_device pointer
  216. * @ring: ring index the fence is associated with
  217. *
  218. * Checks the current fence value and wakes the fence queue
  219. * if the sequence number has increased (all asics).
  220. */
  221. void amdgpu_fence_process(struct amdgpu_ring *ring)
  222. {
  223. if (amdgpu_fence_activity(ring))
  224. wake_up_all(&ring->fence_drv.fence_queue);
  225. }
  226. /**
  227. * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
  228. *
  229. * @ring: ring the fence is associated with
  230. * @seq: sequence number
  231. *
  232. * Check if the last signaled fence sequnce number is >= the requested
  233. * sequence number (all asics).
  234. * Returns true if the fence has signaled (current fence value
  235. * is >= requested value) or false if it has not (current fence
  236. * value is < the requested value. Helper function for
  237. * amdgpu_fence_signaled().
  238. */
  239. static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
  240. {
  241. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  242. return true;
  243. /* poll new last sequence at least once */
  244. amdgpu_fence_process(ring);
  245. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  246. return true;
  247. return false;
  248. }
  249. /*
  250. * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
  251. * @ring: ring to wait on for the seq number
  252. * @seq: seq number wait for
  253. *
  254. * return value:
  255. * 0: seq signaled, and gpu not hang
  256. * -EDEADL: GPU hang detected
  257. * -EINVAL: some paramter is not valid
  258. */
  259. static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
  260. {
  261. bool signaled = false;
  262. BUG_ON(!ring);
  263. if (seq > ring->fence_drv.sync_seq[ring->idx])
  264. return -EINVAL;
  265. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  266. return 0;
  267. amdgpu_fence_schedule_check(ring);
  268. wait_event(ring->fence_drv.fence_queue, (
  269. (signaled = amdgpu_fence_seq_signaled(ring, seq))));
  270. if (signaled)
  271. return 0;
  272. else
  273. return -EDEADLK;
  274. }
  275. /**
  276. * amdgpu_fence_wait_next - wait for the next fence to signal
  277. *
  278. * @adev: amdgpu device pointer
  279. * @ring: ring index the fence is associated with
  280. *
  281. * Wait for the next fence on the requested ring to signal (all asics).
  282. * Returns 0 if the next fence has passed, error for all other cases.
  283. * Caller must hold ring lock.
  284. */
  285. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  286. {
  287. uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  288. if (seq >= ring->fence_drv.sync_seq[ring->idx])
  289. return -ENOENT;
  290. return amdgpu_fence_ring_wait_seq(ring, seq);
  291. }
  292. /**
  293. * amdgpu_fence_wait_empty - wait for all fences to signal
  294. *
  295. * @adev: amdgpu device pointer
  296. * @ring: ring index the fence is associated with
  297. *
  298. * Wait for all fences on the requested ring to signal (all asics).
  299. * Returns 0 if the fences have passed, error for all other cases.
  300. * Caller must hold ring lock.
  301. */
  302. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  303. {
  304. uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
  305. if (!seq)
  306. return 0;
  307. return amdgpu_fence_ring_wait_seq(ring, seq);
  308. }
  309. /**
  310. * amdgpu_fence_count_emitted - get the count of emitted fences
  311. *
  312. * @ring: ring the fence is associated with
  313. *
  314. * Get the number of fences emitted on the requested ring (all asics).
  315. * Returns the number of emitted fences on the ring. Used by the
  316. * dynpm code to ring track activity.
  317. */
  318. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  319. {
  320. uint64_t emitted;
  321. /* We are not protected by ring lock when reading the last sequence
  322. * but it's ok to report slightly wrong fence count here.
  323. */
  324. amdgpu_fence_process(ring);
  325. emitted = ring->fence_drv.sync_seq[ring->idx]
  326. - atomic64_read(&ring->fence_drv.last_seq);
  327. /* to avoid 32bits warp around */
  328. if (emitted > 0x10000000)
  329. emitted = 0x10000000;
  330. return (unsigned)emitted;
  331. }
  332. /**
  333. * amdgpu_fence_need_sync - do we need a semaphore
  334. *
  335. * @fence: amdgpu fence object
  336. * @dst_ring: which ring to check against
  337. *
  338. * Check if the fence needs to be synced against another ring
  339. * (all asics). If so, we need to emit a semaphore.
  340. * Returns true if we need to sync with another ring, false if
  341. * not.
  342. */
  343. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  344. struct amdgpu_ring *dst_ring)
  345. {
  346. struct amdgpu_fence_driver *fdrv;
  347. if (!fence)
  348. return false;
  349. if (fence->ring == dst_ring)
  350. return false;
  351. /* we are protected by the ring mutex */
  352. fdrv = &dst_ring->fence_drv;
  353. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  354. return false;
  355. return true;
  356. }
  357. /**
  358. * amdgpu_fence_note_sync - record the sync point
  359. *
  360. * @fence: amdgpu fence object
  361. * @dst_ring: which ring to check against
  362. *
  363. * Note the sequence number at which point the fence will
  364. * be synced with the requested ring (all asics).
  365. */
  366. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  367. struct amdgpu_ring *dst_ring)
  368. {
  369. struct amdgpu_fence_driver *dst, *src;
  370. unsigned i;
  371. if (!fence)
  372. return;
  373. if (fence->ring == dst_ring)
  374. return;
  375. /* we are protected by the ring mutex */
  376. src = &fence->ring->fence_drv;
  377. dst = &dst_ring->fence_drv;
  378. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  379. if (i == dst_ring->idx)
  380. continue;
  381. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  382. }
  383. }
  384. /**
  385. * amdgpu_fence_driver_start_ring - make the fence driver
  386. * ready for use on the requested ring.
  387. *
  388. * @ring: ring to start the fence driver on
  389. * @irq_src: interrupt source to use for this ring
  390. * @irq_type: interrupt type to use for this ring
  391. *
  392. * Make the fence driver ready for processing (all asics).
  393. * Not all asics have all rings, so each asic will only
  394. * start the fence driver on the rings it has.
  395. * Returns 0 for success, errors for failure.
  396. */
  397. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  398. struct amdgpu_irq_src *irq_src,
  399. unsigned irq_type)
  400. {
  401. struct amdgpu_device *adev = ring->adev;
  402. uint64_t index;
  403. if (ring != &adev->uvd.ring) {
  404. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  405. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  406. } else {
  407. /* put fence directly behind firmware */
  408. index = ALIGN(adev->uvd.fw->size, 8);
  409. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  410. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  411. }
  412. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  413. amdgpu_irq_get(adev, irq_src, irq_type);
  414. ring->fence_drv.irq_src = irq_src;
  415. ring->fence_drv.irq_type = irq_type;
  416. ring->fence_drv.initialized = true;
  417. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  418. "cpu addr 0x%p\n", ring->idx,
  419. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  420. return 0;
  421. }
  422. /**
  423. * amdgpu_fence_driver_init_ring - init the fence driver
  424. * for the requested ring.
  425. *
  426. * @ring: ring to init the fence driver on
  427. *
  428. * Init the fence driver for the requested ring (all asics).
  429. * Helper function for amdgpu_fence_driver_init().
  430. */
  431. int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  432. {
  433. int i, r;
  434. ring->fence_drv.cpu_addr = NULL;
  435. ring->fence_drv.gpu_addr = 0;
  436. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  437. ring->fence_drv.sync_seq[i] = 0;
  438. atomic64_set(&ring->fence_drv.last_seq, 0);
  439. ring->fence_drv.initialized = false;
  440. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  441. amdgpu_fence_check_lockup);
  442. ring->fence_drv.ring = ring;
  443. init_waitqueue_head(&ring->fence_drv.fence_queue);
  444. if (amdgpu_enable_scheduler) {
  445. long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
  446. if (timeout == 0) {
  447. /*
  448. * FIXME:
  449. * Delayed workqueue cannot use it directly,
  450. * so the scheduler will not use delayed workqueue if
  451. * MAX_SCHEDULE_TIMEOUT is set.
  452. * Currently keep it simple and silly.
  453. */
  454. timeout = MAX_SCHEDULE_TIMEOUT;
  455. }
  456. r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
  457. amdgpu_sched_hw_submission,
  458. timeout, ring->name);
  459. if (r) {
  460. DRM_ERROR("Failed to create scheduler on ring %s.\n",
  461. ring->name);
  462. return r;
  463. }
  464. }
  465. return 0;
  466. }
  467. /**
  468. * amdgpu_fence_driver_init - init the fence driver
  469. * for all possible rings.
  470. *
  471. * @adev: amdgpu device pointer
  472. *
  473. * Init the fence driver for all possible rings (all asics).
  474. * Not all asics have all rings, so each asic will only
  475. * start the fence driver on the rings it has using
  476. * amdgpu_fence_driver_start_ring().
  477. * Returns 0 for success.
  478. */
  479. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  480. {
  481. if (amdgpu_debugfs_fence_init(adev))
  482. dev_err(adev->dev, "fence debugfs file creation failed\n");
  483. return 0;
  484. }
  485. /**
  486. * amdgpu_fence_driver_fini - tear down the fence driver
  487. * for all possible rings.
  488. *
  489. * @adev: amdgpu device pointer
  490. *
  491. * Tear down the fence driver for all possible rings (all asics).
  492. */
  493. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  494. {
  495. int i, r;
  496. mutex_lock(&adev->ring_lock);
  497. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  498. struct amdgpu_ring *ring = adev->rings[i];
  499. if (!ring || !ring->fence_drv.initialized)
  500. continue;
  501. r = amdgpu_fence_wait_empty(ring);
  502. if (r) {
  503. /* no need to trigger GPU reset as we are unloading */
  504. amdgpu_fence_driver_force_completion(adev);
  505. }
  506. wake_up_all(&ring->fence_drv.fence_queue);
  507. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  508. ring->fence_drv.irq_type);
  509. amd_sched_fini(&ring->sched);
  510. ring->fence_drv.initialized = false;
  511. }
  512. mutex_unlock(&adev->ring_lock);
  513. }
  514. /**
  515. * amdgpu_fence_driver_suspend - suspend the fence driver
  516. * for all possible rings.
  517. *
  518. * @adev: amdgpu device pointer
  519. *
  520. * Suspend the fence driver for all possible rings (all asics).
  521. */
  522. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  523. {
  524. int i, r;
  525. mutex_lock(&adev->ring_lock);
  526. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  527. struct amdgpu_ring *ring = adev->rings[i];
  528. if (!ring || !ring->fence_drv.initialized)
  529. continue;
  530. /* wait for gpu to finish processing current batch */
  531. r = amdgpu_fence_wait_empty(ring);
  532. if (r) {
  533. /* delay GPU reset to resume */
  534. amdgpu_fence_driver_force_completion(adev);
  535. }
  536. /* disable the interrupt */
  537. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  538. ring->fence_drv.irq_type);
  539. }
  540. mutex_unlock(&adev->ring_lock);
  541. }
  542. /**
  543. * amdgpu_fence_driver_resume - resume the fence driver
  544. * for all possible rings.
  545. *
  546. * @adev: amdgpu device pointer
  547. *
  548. * Resume the fence driver for all possible rings (all asics).
  549. * Not all asics have all rings, so each asic will only
  550. * start the fence driver on the rings it has using
  551. * amdgpu_fence_driver_start_ring().
  552. * Returns 0 for success.
  553. */
  554. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  555. {
  556. int i;
  557. mutex_lock(&adev->ring_lock);
  558. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  559. struct amdgpu_ring *ring = adev->rings[i];
  560. if (!ring || !ring->fence_drv.initialized)
  561. continue;
  562. /* enable the interrupt */
  563. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  564. ring->fence_drv.irq_type);
  565. }
  566. mutex_unlock(&adev->ring_lock);
  567. }
  568. /**
  569. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  570. *
  571. * @adev: amdgpu device pointer
  572. *
  573. * In case of GPU reset failure make sure no process keep waiting on fence
  574. * that will never complete.
  575. */
  576. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  577. {
  578. int i;
  579. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  580. struct amdgpu_ring *ring = adev->rings[i];
  581. if (!ring || !ring->fence_drv.initialized)
  582. continue;
  583. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  584. }
  585. }
  586. /*
  587. * Common fence implementation
  588. */
  589. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  590. {
  591. return "amdgpu";
  592. }
  593. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  594. {
  595. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  596. return (const char *)fence->ring->name;
  597. }
  598. /**
  599. * amdgpu_fence_is_signaled - test if fence is signaled
  600. *
  601. * @f: fence to test
  602. *
  603. * Test the fence sequence number if it is already signaled. If it isn't
  604. * signaled start fence processing. Returns True if the fence is signaled.
  605. */
  606. static bool amdgpu_fence_is_signaled(struct fence *f)
  607. {
  608. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  609. struct amdgpu_ring *ring = fence->ring;
  610. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  611. return true;
  612. amdgpu_fence_process(ring);
  613. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  614. return true;
  615. return false;
  616. }
  617. /**
  618. * amdgpu_fence_check_signaled - callback from fence_queue
  619. *
  620. * this function is called with fence_queue lock held, which is also used
  621. * for the fence locking itself, so unlocked variants are used for
  622. * fence_signal, and remove_wait_queue.
  623. */
  624. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  625. {
  626. struct amdgpu_fence *fence;
  627. struct amdgpu_device *adev;
  628. u64 seq;
  629. int ret;
  630. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  631. adev = fence->ring->adev;
  632. /*
  633. * We cannot use amdgpu_fence_process here because we're already
  634. * in the waitqueue, in a call from wake_up_all.
  635. */
  636. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  637. if (seq >= fence->seq) {
  638. ret = fence_signal_locked(&fence->base);
  639. if (!ret)
  640. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  641. else
  642. FENCE_TRACE(&fence->base, "was already signaled\n");
  643. __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
  644. fence_put(&fence->base);
  645. } else
  646. FENCE_TRACE(&fence->base, "pending\n");
  647. return 0;
  648. }
  649. /**
  650. * amdgpu_fence_enable_signaling - enable signalling on fence
  651. * @fence: fence
  652. *
  653. * This function is called with fence_queue lock held, and adds a callback
  654. * to fence_queue that checks if this fence is signaled, and if so it
  655. * signals the fence and removes itself.
  656. */
  657. static bool amdgpu_fence_enable_signaling(struct fence *f)
  658. {
  659. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  660. struct amdgpu_ring *ring = fence->ring;
  661. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  662. return false;
  663. fence->fence_wake.flags = 0;
  664. fence->fence_wake.private = NULL;
  665. fence->fence_wake.func = amdgpu_fence_check_signaled;
  666. __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
  667. fence_get(f);
  668. amdgpu_fence_schedule_check(ring);
  669. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  670. return true;
  671. }
  672. const struct fence_ops amdgpu_fence_ops = {
  673. .get_driver_name = amdgpu_fence_get_driver_name,
  674. .get_timeline_name = amdgpu_fence_get_timeline_name,
  675. .enable_signaling = amdgpu_fence_enable_signaling,
  676. .signaled = amdgpu_fence_is_signaled,
  677. .wait = fence_default_wait,
  678. .release = NULL,
  679. };
  680. /*
  681. * Fence debugfs
  682. */
  683. #if defined(CONFIG_DEBUG_FS)
  684. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  685. {
  686. struct drm_info_node *node = (struct drm_info_node *)m->private;
  687. struct drm_device *dev = node->minor->dev;
  688. struct amdgpu_device *adev = dev->dev_private;
  689. int i, j;
  690. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  691. struct amdgpu_ring *ring = adev->rings[i];
  692. if (!ring || !ring->fence_drv.initialized)
  693. continue;
  694. amdgpu_fence_process(ring);
  695. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  696. seq_printf(m, "Last signaled fence 0x%016llx\n",
  697. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  698. seq_printf(m, "Last emitted 0x%016llx\n",
  699. ring->fence_drv.sync_seq[i]);
  700. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  701. struct amdgpu_ring *other = adev->rings[j];
  702. if (i != j && other && other->fence_drv.initialized &&
  703. ring->fence_drv.sync_seq[j])
  704. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  705. j, ring->fence_drv.sync_seq[j]);
  706. }
  707. }
  708. return 0;
  709. }
  710. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  711. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  712. };
  713. #endif
  714. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  715. {
  716. #if defined(CONFIG_DEBUG_FS)
  717. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  718. #else
  719. return 0;
  720. #endif
  721. }