amdgpu_fence.c 31 KB

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