amdgpu_fence.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. &adev->fence_queue.lock, adev->fence_context + ring->idx,
  121. (*fence)->seq);
  122. amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
  123. (*fence)->seq,
  124. AMDGPU_FENCE_FLAG_INT);
  125. trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
  126. return 0;
  127. }
  128. /**
  129. * amdgpu_fence_check_signaled - callback from fence_queue
  130. *
  131. * this function is called with fence_queue lock held, which is also used
  132. * for the fence locking itself, so unlocked variants are used for
  133. * fence_signal, and remove_wait_queue.
  134. */
  135. static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
  136. {
  137. struct amdgpu_fence *fence;
  138. struct amdgpu_device *adev;
  139. u64 seq;
  140. int ret;
  141. fence = container_of(wait, struct amdgpu_fence, fence_wake);
  142. adev = fence->ring->adev;
  143. /*
  144. * We cannot use amdgpu_fence_process here because we're already
  145. * in the waitqueue, in a call from wake_up_all.
  146. */
  147. seq = atomic64_read(&fence->ring->fence_drv.last_seq);
  148. if (seq >= fence->seq) {
  149. ret = fence_signal_locked(&fence->base);
  150. if (!ret)
  151. FENCE_TRACE(&fence->base, "signaled from irq context\n");
  152. else
  153. FENCE_TRACE(&fence->base, "was already signaled\n");
  154. amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src,
  155. fence->ring->fence_drv.irq_type);
  156. __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
  157. fence_put(&fence->base);
  158. } else
  159. FENCE_TRACE(&fence->base, "pending\n");
  160. return 0;
  161. }
  162. /**
  163. * amdgpu_fence_activity - check for fence activity
  164. *
  165. * @ring: pointer to struct amdgpu_ring
  166. *
  167. * Checks the current fence value and calculates the last
  168. * signalled fence value. Returns true if activity occured
  169. * on the ring, and the fence_queue should be waken up.
  170. */
  171. static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
  172. {
  173. uint64_t seq, last_seq, last_emitted;
  174. unsigned count_loop = 0;
  175. bool wake = false;
  176. /* Note there is a scenario here for an infinite loop but it's
  177. * very unlikely to happen. For it to happen, the current polling
  178. * process need to be interrupted by another process and another
  179. * process needs to update the last_seq btw the atomic read and
  180. * xchg of the current process.
  181. *
  182. * More over for this to go in infinite loop there need to be
  183. * continuously new fence signaled ie amdgpu_fence_read needs
  184. * to return a different value each time for both the currently
  185. * polling process and the other process that xchg the last_seq
  186. * btw atomic read and xchg of the current process. And the
  187. * value the other process set as last seq must be higher than
  188. * the seq value we just read. Which means that current process
  189. * need to be interrupted after amdgpu_fence_read and before
  190. * atomic xchg.
  191. *
  192. * To be even more safe we count the number of time we loop and
  193. * we bail after 10 loop just accepting the fact that we might
  194. * have temporarly set the last_seq not to the true real last
  195. * seq but to an older one.
  196. */
  197. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  198. do {
  199. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  200. seq = amdgpu_fence_read(ring);
  201. seq |= last_seq & 0xffffffff00000000LL;
  202. if (seq < last_seq) {
  203. seq &= 0xffffffff;
  204. seq |= last_emitted & 0xffffffff00000000LL;
  205. }
  206. if (seq <= last_seq || seq > last_emitted) {
  207. break;
  208. }
  209. /* If we loop over we don't want to return without
  210. * checking if a fence is signaled as it means that the
  211. * seq we just read is different from the previous on.
  212. */
  213. wake = true;
  214. last_seq = seq;
  215. if ((count_loop++) > 10) {
  216. /* We looped over too many time leave with the
  217. * fact that we might have set an older fence
  218. * seq then the current real last seq as signaled
  219. * by the hw.
  220. */
  221. break;
  222. }
  223. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  224. if (seq < last_emitted)
  225. amdgpu_fence_schedule_check(ring);
  226. return wake;
  227. }
  228. /**
  229. * amdgpu_fence_check_lockup - check for hardware lockup
  230. *
  231. * @work: delayed work item
  232. *
  233. * Checks for fence activity and if there is none probe
  234. * the hardware if a lockup occured.
  235. */
  236. static void amdgpu_fence_check_lockup(struct work_struct *work)
  237. {
  238. struct amdgpu_fence_driver *fence_drv;
  239. struct amdgpu_ring *ring;
  240. fence_drv = container_of(work, struct amdgpu_fence_driver,
  241. lockup_work.work);
  242. ring = fence_drv->ring;
  243. if (!down_read_trylock(&ring->adev->exclusive_lock)) {
  244. /* just reschedule the check if a reset is going on */
  245. amdgpu_fence_schedule_check(ring);
  246. return;
  247. }
  248. if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) {
  249. fence_drv->delayed_irq = false;
  250. amdgpu_irq_update(ring->adev, fence_drv->irq_src,
  251. fence_drv->irq_type);
  252. }
  253. if (amdgpu_fence_activity(ring))
  254. wake_up_all(&ring->adev->fence_queue);
  255. else if (amdgpu_ring_is_lockup(ring)) {
  256. /* good news we believe it's a lockup */
  257. dev_warn(ring->adev->dev, "GPU lockup (current fence id "
  258. "0x%016llx last fence id 0x%016llx on ring %d)\n",
  259. (uint64_t)atomic64_read(&fence_drv->last_seq),
  260. fence_drv->sync_seq[ring->idx], ring->idx);
  261. /* remember that we need an reset */
  262. ring->adev->needs_reset = true;
  263. wake_up_all(&ring->adev->fence_queue);
  264. }
  265. up_read(&ring->adev->exclusive_lock);
  266. }
  267. /**
  268. * amdgpu_fence_process - process a fence
  269. *
  270. * @adev: amdgpu_device pointer
  271. * @ring: ring index the fence is associated with
  272. *
  273. * Checks the current fence value and wakes the fence queue
  274. * if the sequence number has increased (all asics).
  275. */
  276. void amdgpu_fence_process(struct amdgpu_ring *ring)
  277. {
  278. uint64_t seq, last_seq, last_emitted;
  279. unsigned count_loop = 0;
  280. bool wake = false;
  281. /* Note there is a scenario here for an infinite loop but it's
  282. * very unlikely to happen. For it to happen, the current polling
  283. * process need to be interrupted by another process and another
  284. * process needs to update the last_seq btw the atomic read and
  285. * xchg of the current process.
  286. *
  287. * More over for this to go in infinite loop there need to be
  288. * continuously new fence signaled ie amdgpu_fence_read needs
  289. * to return a different value each time for both the currently
  290. * polling process and the other process that xchg the last_seq
  291. * btw atomic read and xchg of the current process. And the
  292. * value the other process set as last seq must be higher than
  293. * the seq value we just read. Which means that current process
  294. * need to be interrupted after amdgpu_fence_read and before
  295. * atomic xchg.
  296. *
  297. * To be even more safe we count the number of time we loop and
  298. * we bail after 10 loop just accepting the fact that we might
  299. * have temporarly set the last_seq not to the true real last
  300. * seq but to an older one.
  301. */
  302. last_seq = atomic64_read(&ring->fence_drv.last_seq);
  303. do {
  304. last_emitted = ring->fence_drv.sync_seq[ring->idx];
  305. seq = amdgpu_fence_read(ring);
  306. seq |= last_seq & 0xffffffff00000000LL;
  307. if (seq < last_seq) {
  308. seq &= 0xffffffff;
  309. seq |= last_emitted & 0xffffffff00000000LL;
  310. }
  311. if (seq <= last_seq || seq > last_emitted) {
  312. break;
  313. }
  314. /* If we loop over we don't want to return without
  315. * checking if a fence is signaled as it means that the
  316. * seq we just read is different from the previous on.
  317. */
  318. wake = true;
  319. last_seq = seq;
  320. if ((count_loop++) > 10) {
  321. /* We looped over too many time leave with the
  322. * fact that we might have set an older fence
  323. * seq then the current real last seq as signaled
  324. * by the hw.
  325. */
  326. break;
  327. }
  328. } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
  329. if (wake)
  330. wake_up_all(&ring->adev->fence_queue);
  331. }
  332. /**
  333. * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
  334. *
  335. * @ring: ring the fence is associated with
  336. * @seq: sequence number
  337. *
  338. * Check if the last signaled fence sequnce number is >= the requested
  339. * sequence number (all asics).
  340. * Returns true if the fence has signaled (current fence value
  341. * is >= requested value) or false if it has not (current fence
  342. * value is < the requested value. Helper function for
  343. * amdgpu_fence_signaled().
  344. */
  345. static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
  346. {
  347. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  348. return true;
  349. /* poll new last sequence at least once */
  350. amdgpu_fence_process(ring);
  351. if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
  352. return true;
  353. return false;
  354. }
  355. static bool amdgpu_fence_is_signaled(struct fence *f)
  356. {
  357. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  358. struct amdgpu_ring *ring = fence->ring;
  359. struct amdgpu_device *adev = ring->adev;
  360. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  361. return true;
  362. if (down_read_trylock(&adev->exclusive_lock)) {
  363. amdgpu_fence_process(ring);
  364. up_read(&adev->exclusive_lock);
  365. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  366. return true;
  367. }
  368. return false;
  369. }
  370. /**
  371. * amdgpu_fence_enable_signaling - enable signalling on fence
  372. * @fence: fence
  373. *
  374. * This function is called with fence_queue lock held, and adds a callback
  375. * to fence_queue that checks if this fence is signaled, and if so it
  376. * signals the fence and removes itself.
  377. */
  378. static bool amdgpu_fence_enable_signaling(struct fence *f)
  379. {
  380. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  381. struct amdgpu_ring *ring = fence->ring;
  382. struct amdgpu_device *adev = ring->adev;
  383. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
  384. return false;
  385. if (down_read_trylock(&adev->exclusive_lock)) {
  386. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  387. ring->fence_drv.irq_type);
  388. if (amdgpu_fence_activity(ring))
  389. wake_up_all_locked(&adev->fence_queue);
  390. /* did fence get signaled after we enabled the sw irq? */
  391. if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) {
  392. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  393. ring->fence_drv.irq_type);
  394. up_read(&adev->exclusive_lock);
  395. return false;
  396. }
  397. up_read(&adev->exclusive_lock);
  398. } else {
  399. /* we're probably in a lockup, lets not fiddle too much */
  400. if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src,
  401. ring->fence_drv.irq_type))
  402. ring->fence_drv.delayed_irq = true;
  403. amdgpu_fence_schedule_check(ring);
  404. }
  405. fence->fence_wake.flags = 0;
  406. fence->fence_wake.private = NULL;
  407. fence->fence_wake.func = amdgpu_fence_check_signaled;
  408. __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
  409. fence_get(f);
  410. FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
  411. return true;
  412. }
  413. /**
  414. * amdgpu_fence_signaled - check if a fence has signaled
  415. *
  416. * @fence: amdgpu fence object
  417. *
  418. * Check if the requested fence has signaled (all asics).
  419. * Returns true if the fence has signaled or false if it has not.
  420. */
  421. bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
  422. {
  423. if (!fence)
  424. return true;
  425. if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
  426. if (!fence_signal(&fence->base))
  427. FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
  428. return true;
  429. }
  430. return false;
  431. }
  432. /**
  433. * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
  434. *
  435. * @adev: amdgpu device pointer
  436. * @seq: sequence numbers
  437. *
  438. * Check if the last signaled fence sequnce number is >= the requested
  439. * sequence number (all asics).
  440. * Returns true if any has signaled (current value is >= requested value)
  441. * or false if it has not. Helper function for amdgpu_fence_wait_seq.
  442. */
  443. static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
  444. {
  445. unsigned i;
  446. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  447. if (!adev->rings[i] || !seq[i])
  448. continue;
  449. if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
  450. return true;
  451. }
  452. return false;
  453. }
  454. /**
  455. * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
  456. *
  457. * @adev: amdgpu device pointer
  458. * @target_seq: sequence number(s) we want to wait for
  459. * @intr: use interruptable sleep
  460. * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
  461. *
  462. * Wait for the requested sequence number(s) to be written by any ring
  463. * (all asics). Sequnce number array is indexed by ring id.
  464. * @intr selects whether to use interruptable (true) or non-interruptable
  465. * (false) sleep when waiting for the sequence number. Helper function
  466. * for amdgpu_fence_wait_*().
  467. * Returns remaining time if the sequence number has passed, 0 when
  468. * the wait timeout, or an error for all other cases.
  469. * -EDEADLK is returned when a GPU lockup has been detected.
  470. */
  471. static long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev,
  472. u64 *target_seq, bool intr,
  473. long timeout)
  474. {
  475. uint64_t last_seq[AMDGPU_MAX_RINGS];
  476. bool signaled;
  477. int i;
  478. long r;
  479. if (timeout == 0) {
  480. return amdgpu_fence_any_seq_signaled(adev, target_seq);
  481. }
  482. while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
  483. /* Save current sequence values, used to check for GPU lockups */
  484. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  485. struct amdgpu_ring *ring = adev->rings[i];
  486. if (!ring || !target_seq[i])
  487. continue;
  488. last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
  489. trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
  490. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  491. ring->fence_drv.irq_type);
  492. }
  493. if (intr) {
  494. r = wait_event_interruptible_timeout(adev->fence_queue, (
  495. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  496. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  497. } else {
  498. r = wait_event_timeout(adev->fence_queue, (
  499. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  500. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  501. }
  502. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  503. struct amdgpu_ring *ring = adev->rings[i];
  504. if (!ring || !target_seq[i])
  505. continue;
  506. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  507. ring->fence_drv.irq_type);
  508. trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
  509. }
  510. if (unlikely(r < 0))
  511. return r;
  512. if (unlikely(!signaled)) {
  513. if (adev->needs_reset)
  514. return -EDEADLK;
  515. /* we were interrupted for some reason and fence
  516. * isn't signaled yet, resume waiting */
  517. if (r)
  518. continue;
  519. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  520. struct amdgpu_ring *ring = adev->rings[i];
  521. if (!ring || !target_seq[i])
  522. continue;
  523. if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
  524. break;
  525. }
  526. if (i != AMDGPU_MAX_RINGS)
  527. continue;
  528. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  529. if (!adev->rings[i] || !target_seq[i])
  530. continue;
  531. if (amdgpu_ring_is_lockup(adev->rings[i]))
  532. break;
  533. }
  534. if (i < AMDGPU_MAX_RINGS) {
  535. /* good news we believe it's a lockup */
  536. dev_warn(adev->dev, "GPU lockup (waiting for "
  537. "0x%016llx last fence id 0x%016llx on"
  538. " ring %d)\n",
  539. target_seq[i], last_seq[i], i);
  540. /* remember that we need an reset */
  541. adev->needs_reset = true;
  542. wake_up_all(&adev->fence_queue);
  543. return -EDEADLK;
  544. }
  545. if (timeout < MAX_SCHEDULE_TIMEOUT) {
  546. timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
  547. if (timeout <= 0) {
  548. return 0;
  549. }
  550. }
  551. }
  552. }
  553. return timeout;
  554. }
  555. /**
  556. * amdgpu_fence_wait - wait for a fence to signal
  557. *
  558. * @fence: amdgpu fence object
  559. * @intr: use interruptable sleep
  560. *
  561. * Wait for the requested fence to signal (all asics).
  562. * @intr selects whether to use interruptable (true) or non-interruptable
  563. * (false) sleep when waiting for the fence.
  564. * Returns 0 if the fence has passed, error for all other cases.
  565. */
  566. int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
  567. {
  568. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  569. long r;
  570. seq[fence->ring->idx] = fence->seq;
  571. r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  572. if (r < 0) {
  573. return r;
  574. }
  575. r = fence_signal(&fence->base);
  576. if (!r)
  577. FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
  578. return 0;
  579. }
  580. /**
  581. * amdgpu_fence_wait_any - wait for a fence to signal on any ring
  582. *
  583. * @adev: amdgpu device pointer
  584. * @fences: amdgpu fence object(s)
  585. * @intr: use interruptable sleep
  586. *
  587. * Wait for any requested fence to signal (all asics). Fence
  588. * array is indexed by ring id. @intr selects whether to use
  589. * interruptable (true) or non-interruptable (false) sleep when
  590. * waiting for the fences. Used by the suballocator.
  591. * Returns 0 if any fence has passed, error for all other cases.
  592. */
  593. int amdgpu_fence_wait_any(struct amdgpu_device *adev,
  594. struct amdgpu_fence **fences,
  595. bool intr)
  596. {
  597. uint64_t seq[AMDGPU_MAX_RINGS];
  598. unsigned i, num_rings = 0;
  599. long r;
  600. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  601. seq[i] = 0;
  602. if (!fences[i]) {
  603. continue;
  604. }
  605. seq[i] = fences[i]->seq;
  606. ++num_rings;
  607. }
  608. /* nothing to wait for ? */
  609. if (num_rings == 0)
  610. return -ENOENT;
  611. r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  612. if (r < 0) {
  613. return r;
  614. }
  615. return 0;
  616. }
  617. /**
  618. * amdgpu_fence_wait_next - wait for the next fence to signal
  619. *
  620. * @adev: amdgpu device pointer
  621. * @ring: ring index the fence is associated with
  622. *
  623. * Wait for the next fence on the requested ring to signal (all asics).
  624. * Returns 0 if the next fence has passed, error for all other cases.
  625. * Caller must hold ring lock.
  626. */
  627. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  628. {
  629. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  630. long r;
  631. seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  632. if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
  633. /* nothing to wait for, last_seq is
  634. already the last emited fence */
  635. return -ENOENT;
  636. }
  637. r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  638. if (r < 0)
  639. return r;
  640. return 0;
  641. }
  642. /**
  643. * amdgpu_fence_wait_empty - wait for all fences to signal
  644. *
  645. * @adev: amdgpu device pointer
  646. * @ring: ring index the fence is associated with
  647. *
  648. * Wait for all fences on the requested ring to signal (all asics).
  649. * Returns 0 if the fences have passed, error for all other cases.
  650. * Caller must hold ring lock.
  651. */
  652. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  653. {
  654. struct amdgpu_device *adev = ring->adev;
  655. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  656. long r;
  657. seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
  658. if (!seq[ring->idx])
  659. return 0;
  660. r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  661. if (r < 0) {
  662. if (r == -EDEADLK)
  663. return -EDEADLK;
  664. dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
  665. ring->idx, r);
  666. }
  667. return 0;
  668. }
  669. /**
  670. * amdgpu_fence_ref - take a ref on a fence
  671. *
  672. * @fence: amdgpu fence object
  673. *
  674. * Take a reference on a fence (all asics).
  675. * Returns the fence.
  676. */
  677. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  678. {
  679. fence_get(&fence->base);
  680. return fence;
  681. }
  682. /**
  683. * amdgpu_fence_unref - remove a ref on a fence
  684. *
  685. * @fence: amdgpu fence object
  686. *
  687. * Remove a reference on a fence (all asics).
  688. */
  689. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  690. {
  691. struct amdgpu_fence *tmp = *fence;
  692. *fence = NULL;
  693. if (tmp)
  694. fence_put(&tmp->base);
  695. }
  696. /**
  697. * amdgpu_fence_count_emitted - get the count of emitted fences
  698. *
  699. * @ring: ring the fence is associated with
  700. *
  701. * Get the number of fences emitted on the requested ring (all asics).
  702. * Returns the number of emitted fences on the ring. Used by the
  703. * dynpm code to ring track activity.
  704. */
  705. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  706. {
  707. uint64_t emitted;
  708. /* We are not protected by ring lock when reading the last sequence
  709. * but it's ok to report slightly wrong fence count here.
  710. */
  711. amdgpu_fence_process(ring);
  712. emitted = ring->fence_drv.sync_seq[ring->idx]
  713. - atomic64_read(&ring->fence_drv.last_seq);
  714. /* to avoid 32bits warp around */
  715. if (emitted > 0x10000000)
  716. emitted = 0x10000000;
  717. return (unsigned)emitted;
  718. }
  719. /**
  720. * amdgpu_fence_need_sync - do we need a semaphore
  721. *
  722. * @fence: amdgpu fence object
  723. * @dst_ring: which ring to check against
  724. *
  725. * Check if the fence needs to be synced against another ring
  726. * (all asics). If so, we need to emit a semaphore.
  727. * Returns true if we need to sync with another ring, false if
  728. * not.
  729. */
  730. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  731. struct amdgpu_ring *dst_ring)
  732. {
  733. struct amdgpu_fence_driver *fdrv;
  734. if (!fence)
  735. return false;
  736. if (fence->ring == dst_ring)
  737. return false;
  738. /* we are protected by the ring mutex */
  739. fdrv = &dst_ring->fence_drv;
  740. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  741. return false;
  742. return true;
  743. }
  744. /**
  745. * amdgpu_fence_note_sync - record the sync point
  746. *
  747. * @fence: amdgpu fence object
  748. * @dst_ring: which ring to check against
  749. *
  750. * Note the sequence number at which point the fence will
  751. * be synced with the requested ring (all asics).
  752. */
  753. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  754. struct amdgpu_ring *dst_ring)
  755. {
  756. struct amdgpu_fence_driver *dst, *src;
  757. unsigned i;
  758. if (!fence)
  759. return;
  760. if (fence->ring == dst_ring)
  761. return;
  762. /* we are protected by the ring mutex */
  763. src = &fence->ring->fence_drv;
  764. dst = &dst_ring->fence_drv;
  765. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  766. if (i == dst_ring->idx)
  767. continue;
  768. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  769. }
  770. }
  771. /**
  772. * amdgpu_fence_driver_start_ring - make the fence driver
  773. * ready for use on the requested ring.
  774. *
  775. * @ring: ring to start the fence driver on
  776. * @irq_src: interrupt source to use for this ring
  777. * @irq_type: interrupt type to use for this ring
  778. *
  779. * Make the fence driver ready for processing (all asics).
  780. * Not all asics have all rings, so each asic will only
  781. * start the fence driver on the rings it has.
  782. * Returns 0 for success, errors for failure.
  783. */
  784. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  785. struct amdgpu_irq_src *irq_src,
  786. unsigned irq_type)
  787. {
  788. struct amdgpu_device *adev = ring->adev;
  789. uint64_t index;
  790. if (ring != &adev->uvd.ring) {
  791. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  792. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  793. } else {
  794. /* put fence directly behind firmware */
  795. index = ALIGN(adev->uvd.fw->size, 8);
  796. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  797. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  798. }
  799. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  800. ring->fence_drv.initialized = true;
  801. ring->fence_drv.irq_src = irq_src;
  802. ring->fence_drv.irq_type = irq_type;
  803. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  804. "cpu addr 0x%p\n", ring->idx,
  805. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  806. return 0;
  807. }
  808. /**
  809. * amdgpu_fence_driver_init_ring - init the fence driver
  810. * for the requested ring.
  811. *
  812. * @ring: ring to init the fence driver on
  813. *
  814. * Init the fence driver for the requested ring (all asics).
  815. * Helper function for amdgpu_fence_driver_init().
  816. */
  817. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  818. {
  819. int i;
  820. ring->fence_drv.cpu_addr = NULL;
  821. ring->fence_drv.gpu_addr = 0;
  822. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  823. ring->fence_drv.sync_seq[i] = 0;
  824. atomic64_set(&ring->fence_drv.last_seq, 0);
  825. ring->fence_drv.initialized = false;
  826. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  827. amdgpu_fence_check_lockup);
  828. ring->fence_drv.ring = ring;
  829. }
  830. /**
  831. * amdgpu_fence_driver_init - init the fence driver
  832. * for all possible rings.
  833. *
  834. * @adev: amdgpu device pointer
  835. *
  836. * Init the fence driver for all possible rings (all asics).
  837. * Not all asics have all rings, so each asic will only
  838. * start the fence driver on the rings it has using
  839. * amdgpu_fence_driver_start_ring().
  840. * Returns 0 for success.
  841. */
  842. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  843. {
  844. init_waitqueue_head(&adev->fence_queue);
  845. if (amdgpu_debugfs_fence_init(adev))
  846. dev_err(adev->dev, "fence debugfs file creation failed\n");
  847. return 0;
  848. }
  849. /**
  850. * amdgpu_fence_driver_fini - tear down the fence driver
  851. * for all possible rings.
  852. *
  853. * @adev: amdgpu device pointer
  854. *
  855. * Tear down the fence driver for all possible rings (all asics).
  856. */
  857. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  858. {
  859. int i, r;
  860. mutex_lock(&adev->ring_lock);
  861. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  862. struct amdgpu_ring *ring = adev->rings[i];
  863. if (!ring || !ring->fence_drv.initialized)
  864. continue;
  865. r = amdgpu_fence_wait_empty(ring);
  866. if (r) {
  867. /* no need to trigger GPU reset as we are unloading */
  868. amdgpu_fence_driver_force_completion(adev);
  869. }
  870. wake_up_all(&adev->fence_queue);
  871. ring->fence_drv.initialized = false;
  872. }
  873. mutex_unlock(&adev->ring_lock);
  874. }
  875. /**
  876. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  877. *
  878. * @adev: amdgpu device pointer
  879. *
  880. * In case of GPU reset failure make sure no process keep waiting on fence
  881. * that will never complete.
  882. */
  883. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  884. {
  885. int i;
  886. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  887. struct amdgpu_ring *ring = adev->rings[i];
  888. if (!ring || !ring->fence_drv.initialized)
  889. continue;
  890. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  891. }
  892. }
  893. /*
  894. * Fence debugfs
  895. */
  896. #if defined(CONFIG_DEBUG_FS)
  897. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  898. {
  899. struct drm_info_node *node = (struct drm_info_node *)m->private;
  900. struct drm_device *dev = node->minor->dev;
  901. struct amdgpu_device *adev = dev->dev_private;
  902. int i, j;
  903. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  904. struct amdgpu_ring *ring = adev->rings[i];
  905. if (!ring || !ring->fence_drv.initialized)
  906. continue;
  907. amdgpu_fence_process(ring);
  908. seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
  909. seq_printf(m, "Last signaled fence 0x%016llx\n",
  910. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  911. seq_printf(m, "Last emitted 0x%016llx\n",
  912. ring->fence_drv.sync_seq[i]);
  913. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  914. struct amdgpu_ring *other = adev->rings[j];
  915. if (i != j && other && other->fence_drv.initialized &&
  916. ring->fence_drv.sync_seq[j])
  917. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  918. j, ring->fence_drv.sync_seq[j]);
  919. }
  920. }
  921. return 0;
  922. }
  923. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  924. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  925. };
  926. #endif
  927. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  928. {
  929. #if defined(CONFIG_DEBUG_FS)
  930. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  931. #else
  932. return 0;
  933. #endif
  934. }
  935. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  936. {
  937. return "amdgpu";
  938. }
  939. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  940. {
  941. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  942. return (const char *)fence->ring->name;
  943. }
  944. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  945. {
  946. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  947. }
  948. struct amdgpu_wait_cb {
  949. struct fence_cb base;
  950. struct task_struct *task;
  951. };
  952. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  953. {
  954. struct amdgpu_wait_cb *wait =
  955. container_of(cb, struct amdgpu_wait_cb, base);
  956. wake_up_process(wait->task);
  957. }
  958. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  959. signed long t)
  960. {
  961. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  962. struct amdgpu_device *adev = fence->ring->adev;
  963. struct amdgpu_wait_cb cb;
  964. cb.task = current;
  965. if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
  966. return t;
  967. while (t > 0) {
  968. if (intr)
  969. set_current_state(TASK_INTERRUPTIBLE);
  970. else
  971. set_current_state(TASK_UNINTERRUPTIBLE);
  972. /*
  973. * amdgpu_test_signaled must be called after
  974. * set_current_state to prevent a race with wake_up_process
  975. */
  976. if (amdgpu_test_signaled(fence))
  977. break;
  978. if (adev->needs_reset) {
  979. t = -EDEADLK;
  980. break;
  981. }
  982. t = schedule_timeout(t);
  983. if (t > 0 && intr && signal_pending(current))
  984. t = -ERESTARTSYS;
  985. }
  986. __set_current_state(TASK_RUNNING);
  987. fence_remove_callback(f, &cb.base);
  988. return t;
  989. }
  990. const struct fence_ops amdgpu_fence_ops = {
  991. .get_driver_name = amdgpu_fence_get_driver_name,
  992. .get_timeline_name = amdgpu_fence_get_timeline_name,
  993. .enable_signaling = amdgpu_fence_enable_signaling,
  994. .signaled = amdgpu_fence_is_signaled,
  995. .wait = amdgpu_fence_default_wait,
  996. .release = NULL,
  997. };