amdgpu_fence.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Dave Airlie
  30. */
  31. #include <linux/seq_file.h>
  32. #include <linux/atomic.h>
  33. #include <linux/wait.h>
  34. #include <linux/kref.h>
  35. #include <linux/slab.h>
  36. #include <linux/firmware.h>
  37. #include <drm/drmP.h>
  38. #include "amdgpu.h"
  39. #include "amdgpu_trace.h"
  40. /*
  41. * Fences
  42. * Fences mark an event in the GPUs pipeline and are used
  43. * for GPU/CPU synchronization. When the fence is written,
  44. * it is expected that all buffers associated with that fence
  45. * are no longer in use by the associated ring on the GPU and
  46. * that the the relevant GPU caches have been flushed.
  47. */
  48. /**
  49. * amdgpu_fence_write - write a fence value
  50. *
  51. * @ring: ring the fence is associated with
  52. * @seq: sequence number to write
  53. *
  54. * Writes a fence value to memory (all asics).
  55. */
  56. static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
  57. {
  58. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  59. if (drv->cpu_addr)
  60. *drv->cpu_addr = cpu_to_le32(seq);
  61. }
  62. /**
  63. * amdgpu_fence_read - read a fence value
  64. *
  65. * @ring: ring the fence is associated with
  66. *
  67. * Reads a fence value from memory (all asics).
  68. * Returns the value of the fence read from memory.
  69. */
  70. static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
  71. {
  72. struct amdgpu_fence_driver *drv = &ring->fence_drv;
  73. u32 seq = 0;
  74. if (drv->cpu_addr)
  75. seq = le32_to_cpu(*drv->cpu_addr);
  76. else
  77. seq = lower_32_bits(atomic64_read(&drv->last_seq));
  78. return seq;
  79. }
  80. /**
  81. * amdgpu_fence_schedule_check - schedule lockup check
  82. *
  83. * @ring: pointer to struct amdgpu_ring
  84. *
  85. * Queues a delayed work item to check for lockups.
  86. */
  87. static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring)
  88. {
  89. /*
  90. * Do not reset the timer here with mod_delayed_work,
  91. * this can livelock in an interaction with TTM delayed destroy.
  92. */
  93. queue_delayed_work(system_power_efficient_wq,
  94. &ring->fence_drv.lockup_work,
  95. AMDGPU_FENCE_JIFFIES_TIMEOUT);
  96. }
  97. /**
  98. * amdgpu_fence_emit - emit a fence on the requested ring
  99. *
  100. * @ring: ring the fence is associated with
  101. * @owner: creator of the fence
  102. * @fence: amdgpu fence object
  103. *
  104. * Emits a fence command on the requested ring (all asics).
  105. * Returns 0 on success, -ENOMEM on failure.
  106. */
  107. int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
  108. struct amdgpu_fence **fence)
  109. {
  110. struct amdgpu_device *adev = ring->adev;
  111. /* we are protected by the ring emission mutex */
  112. *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
  113. if ((*fence) == NULL) {
  114. return -ENOMEM;
  115. }
  116. (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
  117. (*fence)->ring = ring;
  118. (*fence)->owner = owner;
  119. fence_init(&(*fence)->base, &amdgpu_fence_ops,
  120. &ring->fence_drv.fence_queue.lock,
  121. adev->fence_context + ring->idx,
  122. (*fence)->seq);
  123. amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
  124. (*fence)->seq,
  125. AMDGPU_FENCE_FLAG_INT);
  126. trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
  127. return 0;
  128. }
  129. /**
  130. * amdgpu_fence_check_signaled - callback from fence_queue
  131. *
  132. * this function is called with fence_queue lock held, which is also used
  133. * for the fence locking itself, so unlocked variants are used for
  134. * fence_signal, and remove_wait_queue.
  135. */
  136. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  137. {
  138. struct amdgpu_fence *fence;
  139. struct amdgpu_device *adev;
  140. u64 seq;
  141. int ret;
  142. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  143. adev = fence->ring->adev;
  144. /*
  145. * We cannot use amdgpu_fence_process here because we're already
  146. * in the waitqueue, in a call from wake_up_all.
  147. */
  148. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  149. if (seq >= fence->seq) {
  150. ret = fence_signal_locked(&fence->base);
  151. if (!ret)
  152. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  153. else
  154. FENCE_TRACE(&fence->base, "was already signaled\n");
  155. __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
  156. fence_put(&fence->base);
  157. } else
  158. FENCE_TRACE(&fence->base, "pending\n");
  159. return 0;
  160. }
  161. /**
  162. * amdgpu_fence_activity - check for fence activity
  163. *
  164. * @ring: pointer to struct amdgpu_ring
  165. *
  166. * Checks the current fence value and calculates the last
  167. * signalled fence value. Returns true if activity occured
  168. * on the ring, and the fence_queue should be waken up.
  169. */
  170. static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
  171. {
  172. uint64_t seq, last_seq, last_emitted;
  173. unsigned count_loop = 0;
  174. bool wake = false;
  175. /* Note there is a scenario here for an infinite loop but it's
  176. * very unlikely to happen. For it to happen, the current polling
  177. * process need to be interrupted by another process and another
  178. * process needs to update the last_seq btw the atomic read and
  179. * xchg of the current process.
  180. *
  181. * More over for this to go in infinite loop there need to be
  182. * continuously new fence signaled ie amdgpu_fence_read needs
  183. * to return a different value each time for both the currently
  184. * polling process and the other process that xchg the last_seq
  185. * btw atomic read and xchg of the current process. And the
  186. * value the other process set as last seq must be higher than
  187. * the seq value we just read. Which means that current process
  188. * need to be interrupted after amdgpu_fence_read and before
  189. * atomic xchg.
  190. *
  191. * To be even more safe we count the number of time we loop and
  192. * we bail after 10 loop just accepting the fact that we might
  193. * have temporarly set the last_seq not to the true real last
  194. * seq but to an older one.
  195. */
  196. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  197. do {
  198. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  199. seq = amdgpu_fence_read(ring);
  200. seq |= last_seq & 0xffffffff00000000LL;
  201. if (seq < last_seq) {
  202. seq &= 0xffffffff;
  203. seq |= last_emitted & 0xffffffff00000000LL;
  204. }
  205. if (seq <= last_seq || seq > last_emitted) {
  206. break;
  207. }
  208. /* If we loop over we don't want to return without
  209. * checking if a fence is signaled as it means that the
  210. * seq we just read is different from the previous on.
  211. */
  212. wake = true;
  213. last_seq = seq;
  214. if ((count_loop++) > 10) {
  215. /* We looped over too many time leave with the
  216. * fact that we might have set an older fence
  217. * seq then the current real last seq as signaled
  218. * by the hw.
  219. */
  220. break;
  221. }
  222. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  223. if (seq < last_emitted)
  224. amdgpu_fence_schedule_check(ring);
  225. return wake;
  226. }
  227. /**
  228. * amdgpu_fence_check_lockup - check for hardware lockup
  229. *
  230. * @work: delayed work item
  231. *
  232. * Checks for fence activity and if there is none probe
  233. * the hardware if a lockup occured.
  234. */
  235. static void amdgpu_fence_check_lockup(struct work_struct *work)
  236. {
  237. struct amdgpu_fence_driver *fence_drv;
  238. struct amdgpu_ring *ring;
  239. fence_drv = container_of(work, struct amdgpu_fence_driver,
  240. lockup_work.work);
  241. ring = fence_drv->ring;
  242. if (!down_read_trylock(&ring->adev->exclusive_lock)) {
  243. /* just reschedule the check if a reset is going on */
  244. amdgpu_fence_schedule_check(ring);
  245. return;
  246. }
  247. if (amdgpu_fence_activity(ring)) {
  248. wake_up_all(&ring->fence_drv.fence_queue);
  249. }
  250. else if (amdgpu_ring_is_lockup(ring)) {
  251. /* good news we believe it's a lockup */
  252. dev_warn(ring->adev->dev, "GPU lockup (current fence id "
  253. "0x%016llx last fence id 0x%016llx on ring %d)\n",
  254. (uint64_t)atomic64_read(&fence_drv->last_seq),
  255. fence_drv->sync_seq[ring->idx], ring->idx);
  256. /* remember that we need an reset */
  257. ring->adev->needs_reset = true;
  258. wake_up_all(&ring->fence_drv.fence_queue);
  259. }
  260. up_read(&ring->adev->exclusive_lock);
  261. }
  262. /**
  263. * amdgpu_fence_process - process a fence
  264. *
  265. * @adev: amdgpu_device pointer
  266. * @ring: ring index the fence is associated with
  267. *
  268. * Checks the current fence value and wakes the fence queue
  269. * if the sequence number has increased (all asics).
  270. */
  271. void amdgpu_fence_process(struct amdgpu_ring *ring)
  272. {
  273. uint64_t seq, last_seq, last_emitted;
  274. unsigned count_loop = 0;
  275. bool wake = false;
  276. unsigned long irqflags;
  277. /* Note there is a scenario here for an infinite loop but it's
  278. * very unlikely to happen. For it to happen, the current polling
  279. * process need to be interrupted by another process and another
  280. * process needs to update the last_seq btw the atomic read and
  281. * xchg of the current process.
  282. *
  283. * More over for this to go in infinite loop there need to be
  284. * continuously new fence signaled ie amdgpu_fence_read needs
  285. * to return a different value each time for both the currently
  286. * polling process and the other process that xchg the last_seq
  287. * btw atomic read and xchg of the current process. And the
  288. * value the other process set as last seq must be higher than
  289. * the seq value we just read. Which means that current process
  290. * need to be interrupted after amdgpu_fence_read and before
  291. * atomic xchg.
  292. *
  293. * To be even more safe we count the number of time we loop and
  294. * we bail after 10 loop just accepting the fact that we might
  295. * have temporarly set the last_seq not to the true real last
  296. * seq but to an older one.
  297. */
  298. spin_lock_irqsave(&ring->fence_lock, irqflags);
  299. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  300. do {
  301. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  302. seq = amdgpu_fence_read(ring);
  303. seq |= last_seq & 0xffffffff00000000LL;
  304. if (seq < last_seq) {
  305. seq &= 0xffffffff;
  306. seq |= last_emitted & 0xffffffff00000000LL;
  307. }
  308. if (seq <= last_seq || seq > last_emitted) {
  309. break;
  310. }
  311. /* If we loop over we don't want to return without
  312. * checking if a fence is signaled as it means that the
  313. * seq we just read is different from the previous on.
  314. */
  315. wake = true;
  316. last_seq = seq;
  317. if ((count_loop++) > 10) {
  318. /* We looped over too many time leave with the
  319. * fact that we might have set an older fence
  320. * seq then the current real last seq as signaled
  321. * by the hw.
  322. */
  323. break;
  324. }
  325. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  326. if (wake)
  327. wake_up_all(&ring->fence_drv.fence_queue);
  328. spin_unlock_irqrestore(&ring->fence_lock, irqflags);
  329. }
  330. /**
  331. * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
  332. *
  333. * @ring: ring the fence is associated with
  334. * @seq: sequence number
  335. *
  336. * Check if the last signaled fence sequnce number is >= the requested
  337. * sequence number (all asics).
  338. * Returns true if the fence has signaled (current fence value
  339. * is >= requested value) or false if it has not (current fence
  340. * value is < the requested value. Helper function for
  341. * amdgpu_fence_signaled().
  342. */
  343. static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
  344. {
  345. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  346. return true;
  347. /* poll new last sequence at least once */
  348. amdgpu_fence_process(ring);
  349. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  350. return true;
  351. return false;
  352. }
  353. static bool amdgpu_fence_is_signaled(struct fence *f)
  354. {
  355. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  356. struct amdgpu_ring *ring = fence->ring;
  357. struct amdgpu_device *adev = ring->adev;
  358. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  359. return true;
  360. if (down_read_trylock(&adev->exclusive_lock)) {
  361. amdgpu_fence_process(ring);
  362. up_read(&adev->exclusive_lock);
  363. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  364. return true;
  365. }
  366. return false;
  367. }
  368. /**
  369. * amdgpu_fence_enable_signaling - enable signalling on fence
  370. * @fence: fence
  371. *
  372. * This function is called with fence_queue lock held, and adds a callback
  373. * to fence_queue that checks if this fence is signaled, and if so it
  374. * signals the fence and removes itself.
  375. */
  376. static bool amdgpu_fence_enable_signaling(struct fence *f)
  377. {
  378. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  379. struct amdgpu_ring *ring = fence->ring;
  380. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  381. return false;
  382. fence->fence_wake.flags = 0;
  383. fence->fence_wake.private = NULL;
  384. fence->fence_wake.func = amdgpu_fence_check_signaled;
  385. __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
  386. fence_get(f);
  387. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  388. return true;
  389. }
  390. /*
  391. * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
  392. * @ring: ring to wait on for the seq number
  393. * @seq: seq number wait for
  394. * @intr: if interruptible
  395. * @timeout: jiffies before time out
  396. *
  397. * return value:
  398. * 0: time out but seq not signaled, and gpu not hang
  399. * X (X > 0): seq signaled and X means how many jiffies remains before time out
  400. * -EDEADL: GPU hang before time out
  401. * -ESYSRESTART: interrupted before seq signaled
  402. * -EINVAL: some paramter is not valid
  403. */
  404. static long amdgpu_fence_ring_wait_seq_timeout(struct amdgpu_ring *ring, uint64_t seq,
  405. bool intr, long timeout)
  406. {
  407. struct amdgpu_device *adev = ring->adev;
  408. long r = 0;
  409. bool signaled = false;
  410. BUG_ON(!ring);
  411. if (seq > ring->fence_drv.sync_seq[ring->idx])
  412. return -EINVAL;
  413. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  414. return timeout;
  415. while (1) {
  416. if (intr) {
  417. r = wait_event_interruptible_timeout(ring->fence_drv.fence_queue, (
  418. (signaled = amdgpu_fence_seq_signaled(ring, seq))
  419. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  420. if (r == -ERESTARTSYS) /* interrupted */
  421. return r;
  422. } else {
  423. r = wait_event_timeout(ring->fence_drv.fence_queue, (
  424. (signaled = amdgpu_fence_seq_signaled(ring, seq))
  425. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  426. }
  427. if (signaled) {
  428. /* seq signaled */
  429. if (timeout == MAX_SCHEDULE_TIMEOUT)
  430. return timeout;
  431. return (timeout - AMDGPU_FENCE_JIFFIES_TIMEOUT - r);
  432. }
  433. else if (adev->needs_reset) {
  434. return -EDEADLK;
  435. }
  436. /* check if it's a lockup */
  437. if (amdgpu_ring_is_lockup(ring)) {
  438. uint64_t last_seq = atomic64_read(&ring->fence_drv.last_seq);
  439. /* ring lookup */
  440. dev_warn(adev->dev, "GPU lockup (waiting for "
  441. "0x%016llx last fence id 0x%016llx on"
  442. " ring %d)\n",
  443. seq, last_seq, ring->idx);
  444. wake_up_all(&ring->fence_drv.fence_queue);
  445. return -EDEADLK;
  446. }
  447. if (timeout < MAX_SCHEDULE_TIMEOUT) {
  448. timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
  449. if (timeout < 1)
  450. return 0;
  451. }
  452. }
  453. }
  454. /**
  455. * amdgpu_fence_wait - wait for a fence to signal
  456. *
  457. * @fence: amdgpu fence object
  458. * @intr: use interruptable sleep
  459. *
  460. * Wait for the requested fence to signal (all asics).
  461. * @intr selects whether to use interruptable (true) or non-interruptable
  462. * (false) sleep when waiting for the fence.
  463. * Returns 0 if the fence has passed, error for all other cases.
  464. */
  465. int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
  466. {
  467. long r;
  468. r = fence_wait_timeout(&fence->base, intr, MAX_SCHEDULE_TIMEOUT);
  469. if (r < 0)
  470. return r;
  471. return 0;
  472. }
  473. /**
  474. * amdgpu_fence_wait_next - wait for the next fence to signal
  475. *
  476. * @adev: amdgpu device pointer
  477. * @ring: ring index the fence is associated with
  478. *
  479. * Wait for the next fence on the requested ring to signal (all asics).
  480. * Returns 0 if the next fence has passed, error for all other cases.
  481. * Caller must hold ring lock.
  482. */
  483. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  484. {
  485. long r;
  486. uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  487. if (seq >= ring->fence_drv.sync_seq[ring->idx])
  488. return -ENOENT;
  489. r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
  490. if (r < 0)
  491. return r;
  492. return 0;
  493. }
  494. /**
  495. * amdgpu_fence_wait_empty - wait for all fences to signal
  496. *
  497. * @adev: amdgpu device pointer
  498. * @ring: ring index the fence is associated with
  499. *
  500. * Wait for all fences on the requested ring to signal (all asics).
  501. * Returns 0 if the fences have passed, error for all other cases.
  502. * Caller must hold ring lock.
  503. */
  504. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  505. {
  506. long r;
  507. uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
  508. if (!seq)
  509. return 0;
  510. r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
  511. if (r < 0) {
  512. if (r == -EDEADLK)
  513. return -EDEADLK;
  514. dev_err(ring->adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
  515. ring->idx, r);
  516. }
  517. return 0;
  518. }
  519. /**
  520. * amdgpu_fence_ref - take a ref on a fence
  521. *
  522. * @fence: amdgpu fence object
  523. *
  524. * Take a reference on a fence (all asics).
  525. * Returns the fence.
  526. */
  527. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  528. {
  529. fence_get(&fence->base);
  530. return fence;
  531. }
  532. /**
  533. * amdgpu_fence_unref - remove a ref on a fence
  534. *
  535. * @fence: amdgpu fence object
  536. *
  537. * Remove a reference on a fence (all asics).
  538. */
  539. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  540. {
  541. struct amdgpu_fence *tmp = *fence;
  542. *fence = NULL;
  543. if (tmp)
  544. fence_put(&tmp->base);
  545. }
  546. /**
  547. * amdgpu_fence_count_emitted - get the count of emitted fences
  548. *
  549. * @ring: ring the fence is associated with
  550. *
  551. * Get the number of fences emitted on the requested ring (all asics).
  552. * Returns the number of emitted fences on the ring. Used by the
  553. * dynpm code to ring track activity.
  554. */
  555. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  556. {
  557. uint64_t emitted;
  558. /* We are not protected by ring lock when reading the last sequence
  559. * but it's ok to report slightly wrong fence count here.
  560. */
  561. amdgpu_fence_process(ring);
  562. emitted = ring->fence_drv.sync_seq[ring->idx]
  563. - atomic64_read(&ring->fence_drv.last_seq);
  564. /* to avoid 32bits warp around */
  565. if (emitted > 0x10000000)
  566. emitted = 0x10000000;
  567. return (unsigned)emitted;
  568. }
  569. /**
  570. * amdgpu_fence_need_sync - do we need a semaphore
  571. *
  572. * @fence: amdgpu fence object
  573. * @dst_ring: which ring to check against
  574. *
  575. * Check if the fence needs to be synced against another ring
  576. * (all asics). If so, we need to emit a semaphore.
  577. * Returns true if we need to sync with another ring, false if
  578. * not.
  579. */
  580. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  581. struct amdgpu_ring *dst_ring)
  582. {
  583. struct amdgpu_fence_driver *fdrv;
  584. if (!fence)
  585. return false;
  586. if (fence->ring == dst_ring)
  587. return false;
  588. /* we are protected by the ring mutex */
  589. fdrv = &dst_ring->fence_drv;
  590. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  591. return false;
  592. return true;
  593. }
  594. /**
  595. * amdgpu_fence_note_sync - record the sync point
  596. *
  597. * @fence: amdgpu fence object
  598. * @dst_ring: which ring to check against
  599. *
  600. * Note the sequence number at which point the fence will
  601. * be synced with the requested ring (all asics).
  602. */
  603. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  604. struct amdgpu_ring *dst_ring)
  605. {
  606. struct amdgpu_fence_driver *dst, *src;
  607. unsigned i;
  608. if (!fence)
  609. return;
  610. if (fence->ring == dst_ring)
  611. return;
  612. /* we are protected by the ring mutex */
  613. src = &fence->ring->fence_drv;
  614. dst = &dst_ring->fence_drv;
  615. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  616. if (i == dst_ring->idx)
  617. continue;
  618. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  619. }
  620. }
  621. /**
  622. * amdgpu_fence_driver_start_ring - make the fence driver
  623. * ready for use on the requested ring.
  624. *
  625. * @ring: ring to start the fence driver on
  626. * @irq_src: interrupt source to use for this ring
  627. * @irq_type: interrupt type to use for this ring
  628. *
  629. * Make the fence driver ready for processing (all asics).
  630. * Not all asics have all rings, so each asic will only
  631. * start the fence driver on the rings it has.
  632. * Returns 0 for success, errors for failure.
  633. */
  634. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  635. struct amdgpu_irq_src *irq_src,
  636. unsigned irq_type)
  637. {
  638. struct amdgpu_device *adev = ring->adev;
  639. uint64_t index;
  640. if (ring != &adev->uvd.ring) {
  641. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  642. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  643. } else {
  644. /* put fence directly behind firmware */
  645. index = ALIGN(adev->uvd.fw->size, 8);
  646. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  647. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  648. }
  649. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  650. amdgpu_irq_get(adev, irq_src, irq_type);
  651. ring->fence_drv.irq_src = irq_src;
  652. ring->fence_drv.irq_type = irq_type;
  653. ring->fence_drv.initialized = true;
  654. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  655. "cpu addr 0x%p\n", ring->idx,
  656. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  657. return 0;
  658. }
  659. /**
  660. * amdgpu_fence_driver_init_ring - init the fence driver
  661. * for the requested ring.
  662. *
  663. * @ring: ring to init the fence driver on
  664. *
  665. * Init the fence driver for the requested ring (all asics).
  666. * Helper function for amdgpu_fence_driver_init().
  667. */
  668. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  669. {
  670. int i;
  671. ring->fence_drv.cpu_addr = NULL;
  672. ring->fence_drv.gpu_addr = 0;
  673. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  674. ring->fence_drv.sync_seq[i] = 0;
  675. atomic64_set(&ring->fence_drv.last_seq, 0);
  676. ring->fence_drv.initialized = false;
  677. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  678. amdgpu_fence_check_lockup);
  679. ring->fence_drv.ring = ring;
  680. if (amdgpu_enable_scheduler) {
  681. ring->scheduler = amd_sched_create((void *)ring->adev,
  682. &amdgpu_sched_ops,
  683. ring->idx, 5, 0,
  684. amdgpu_sched_hw_submission);
  685. if (!ring->scheduler)
  686. DRM_ERROR("Failed to create scheduler on ring %d.\n",
  687. ring->idx);
  688. }
  689. }
  690. /**
  691. * amdgpu_fence_driver_init - init the fence driver
  692. * for all possible rings.
  693. *
  694. * @adev: amdgpu device pointer
  695. *
  696. * Init the fence driver for all possible rings (all asics).
  697. * Not all asics have all rings, so each asic will only
  698. * start the fence driver on the rings it has using
  699. * amdgpu_fence_driver_start_ring().
  700. * Returns 0 for success.
  701. */
  702. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  703. {
  704. if (amdgpu_debugfs_fence_init(adev))
  705. dev_err(adev->dev, "fence debugfs file creation failed\n");
  706. return 0;
  707. }
  708. /**
  709. * amdgpu_fence_driver_fini - tear down the fence driver
  710. * for all possible rings.
  711. *
  712. * @adev: amdgpu device pointer
  713. *
  714. * Tear down the fence driver for all possible rings (all asics).
  715. */
  716. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  717. {
  718. int i, r;
  719. mutex_lock(&adev->ring_lock);
  720. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  721. struct amdgpu_ring *ring = adev->rings[i];
  722. if (!ring || !ring->fence_drv.initialized)
  723. continue;
  724. r = amdgpu_fence_wait_empty(ring);
  725. if (r) {
  726. /* no need to trigger GPU reset as we are unloading */
  727. amdgpu_fence_driver_force_completion(adev);
  728. }
  729. wake_up_all(&ring->fence_drv.fence_queue);
  730. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  731. ring->fence_drv.irq_type);
  732. if (ring->scheduler)
  733. amd_sched_destroy(ring->scheduler);
  734. ring->fence_drv.initialized = false;
  735. }
  736. mutex_unlock(&adev->ring_lock);
  737. }
  738. /**
  739. * amdgpu_fence_driver_suspend - suspend the fence driver
  740. * for all possible rings.
  741. *
  742. * @adev: amdgpu device pointer
  743. *
  744. * Suspend the fence driver for all possible rings (all asics).
  745. */
  746. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
  747. {
  748. int i, r;
  749. mutex_lock(&adev->ring_lock);
  750. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  751. struct amdgpu_ring *ring = adev->rings[i];
  752. if (!ring || !ring->fence_drv.initialized)
  753. continue;
  754. /* wait for gpu to finish processing current batch */
  755. r = amdgpu_fence_wait_empty(ring);
  756. if (r) {
  757. /* delay GPU reset to resume */
  758. amdgpu_fence_driver_force_completion(adev);
  759. }
  760. /* disable the interrupt */
  761. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  762. ring->fence_drv.irq_type);
  763. }
  764. mutex_unlock(&adev->ring_lock);
  765. }
  766. /**
  767. * amdgpu_fence_driver_resume - resume the fence driver
  768. * for all possible rings.
  769. *
  770. * @adev: amdgpu device pointer
  771. *
  772. * Resume the fence driver for all possible rings (all asics).
  773. * Not all asics have all rings, so each asic will only
  774. * start the fence driver on the rings it has using
  775. * amdgpu_fence_driver_start_ring().
  776. * Returns 0 for success.
  777. */
  778. void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
  779. {
  780. int i;
  781. mutex_lock(&adev->ring_lock);
  782. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  783. struct amdgpu_ring *ring = adev->rings[i];
  784. if (!ring || !ring->fence_drv.initialized)
  785. continue;
  786. /* enable the interrupt */
  787. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  788. ring->fence_drv.irq_type);
  789. }
  790. mutex_unlock(&adev->ring_lock);
  791. }
  792. /**
  793. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  794. *
  795. * @adev: amdgpu device pointer
  796. *
  797. * In case of GPU reset failure make sure no process keep waiting on fence
  798. * that will never complete.
  799. */
  800. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  801. {
  802. int i;
  803. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  804. struct amdgpu_ring *ring = adev->rings[i];
  805. if (!ring || !ring->fence_drv.initialized)
  806. continue;
  807. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  808. }
  809. }
  810. /*
  811. * Fence debugfs
  812. */
  813. #if defined(CONFIG_DEBUG_FS)
  814. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  815. {
  816. struct drm_info_node *node = (struct drm_info_node *)m->private;
  817. struct drm_device *dev = node->minor->dev;
  818. struct amdgpu_device *adev = dev->dev_private;
  819. int i, j;
  820. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  821. struct amdgpu_ring *ring = adev->rings[i];
  822. if (!ring || !ring->fence_drv.initialized)
  823. continue;
  824. amdgpu_fence_process(ring);
  825. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  826. seq_printf(m, "Last signaled fence 0x%016llx\n",
  827. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  828. seq_printf(m, "Last emitted 0x%016llx\n",
  829. ring->fence_drv.sync_seq[i]);
  830. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  831. struct amdgpu_ring *other = adev->rings[j];
  832. if (i != j && other && other->fence_drv.initialized &&
  833. ring->fence_drv.sync_seq[j])
  834. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  835. j, ring->fence_drv.sync_seq[j]);
  836. }
  837. }
  838. return 0;
  839. }
  840. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  841. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  842. };
  843. #endif
  844. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  845. {
  846. #if defined(CONFIG_DEBUG_FS)
  847. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  848. #else
  849. return 0;
  850. #endif
  851. }
  852. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  853. {
  854. return "amdgpu";
  855. }
  856. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  857. {
  858. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  859. return (const char *)fence->ring->name;
  860. }
  861. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  862. {
  863. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  864. }
  865. static inline bool amdgpu_test_signaled_any(struct amdgpu_fence **fences)
  866. {
  867. int idx;
  868. struct amdgpu_fence *fence;
  869. idx = 0;
  870. for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
  871. fence = fences[idx];
  872. if (fence) {
  873. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  874. return true;
  875. }
  876. }
  877. return false;
  878. }
  879. struct amdgpu_wait_cb {
  880. struct fence_cb base;
  881. struct task_struct *task;
  882. };
  883. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  884. {
  885. struct amdgpu_wait_cb *wait =
  886. container_of(cb, struct amdgpu_wait_cb, base);
  887. wake_up_process(wait->task);
  888. }
  889. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  890. signed long t)
  891. {
  892. struct amdgpu_fence *array[AMDGPU_MAX_RINGS];
  893. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  894. struct amdgpu_device *adev = fence->ring->adev;
  895. memset(&array[0], 0, sizeof(array));
  896. array[0] = fence;
  897. return amdgpu_fence_wait_any(adev, array, intr, t);
  898. }
  899. /* wait until any fence in array signaled */
  900. signed long amdgpu_fence_wait_any(struct amdgpu_device *adev,
  901. struct amdgpu_fence **array, bool intr, signed long t)
  902. {
  903. long idx = 0;
  904. struct amdgpu_wait_cb cb[AMDGPU_MAX_RINGS];
  905. struct amdgpu_fence *fence;
  906. BUG_ON(!array);
  907. for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
  908. fence = array[idx];
  909. if (fence) {
  910. cb[idx].task = current;
  911. if (fence_add_callback(&fence->base,
  912. &cb[idx].base, amdgpu_fence_wait_cb))
  913. return t; /* return if fence is already signaled */
  914. }
  915. }
  916. while (t > 0) {
  917. if (intr)
  918. set_current_state(TASK_INTERRUPTIBLE);
  919. else
  920. set_current_state(TASK_UNINTERRUPTIBLE);
  921. /*
  922. * amdgpu_test_signaled_any must be called after
  923. * set_current_state to prevent a race with wake_up_process
  924. */
  925. if (amdgpu_test_signaled_any(array))
  926. break;
  927. if (adev->needs_reset) {
  928. t = -EDEADLK;
  929. break;
  930. }
  931. t = schedule_timeout(t);
  932. if (t > 0 && intr && signal_pending(current))
  933. t = -ERESTARTSYS;
  934. }
  935. __set_current_state(TASK_RUNNING);
  936. idx = 0;
  937. for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
  938. fence = array[idx];
  939. if (fence)
  940. fence_remove_callback(&fence->base, &cb[idx].base);
  941. }
  942. return t;
  943. }
  944. const struct fence_ops amdgpu_fence_ops = {
  945. .get_driver_name = amdgpu_fence_get_driver_name,
  946. .get_timeline_name = amdgpu_fence_get_timeline_name,
  947. .enable_signaling = amdgpu_fence_enable_signaling,
  948. .signaled = amdgpu_fence_is_signaled,
  949. .wait = amdgpu_fence_default_wait,
  950. .release = NULL,
  951. };