amdgpu_fence.c 22 KB

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