amdgpu_fence.c 22 KB

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