amdgpu_fence.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, u64 *target_seq,
  472. bool intr, long timeout)
  473. {
  474. uint64_t last_seq[AMDGPU_MAX_RINGS];
  475. bool signaled;
  476. int i, r;
  477. if (timeout == 0) {
  478. return amdgpu_fence_any_seq_signaled(adev, target_seq);
  479. }
  480. while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
  481. /* Save current sequence values, used to check for GPU lockups */
  482. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  483. struct amdgpu_ring *ring = adev->rings[i];
  484. if (!ring || !target_seq[i])
  485. continue;
  486. last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
  487. trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
  488. amdgpu_irq_get(adev, ring->fence_drv.irq_src,
  489. ring->fence_drv.irq_type);
  490. }
  491. if (intr) {
  492. r = wait_event_interruptible_timeout(adev->fence_queue, (
  493. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  494. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  495. } else {
  496. r = wait_event_timeout(adev->fence_queue, (
  497. (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
  498. || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
  499. }
  500. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  501. struct amdgpu_ring *ring = adev->rings[i];
  502. if (!ring || !target_seq[i])
  503. continue;
  504. amdgpu_irq_put(adev, ring->fence_drv.irq_src,
  505. ring->fence_drv.irq_type);
  506. trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
  507. }
  508. if (unlikely(r < 0))
  509. return r;
  510. if (unlikely(!signaled)) {
  511. if (adev->needs_reset)
  512. return -EDEADLK;
  513. /* we were interrupted for some reason and fence
  514. * isn't signaled yet, resume waiting */
  515. if (r)
  516. continue;
  517. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  518. struct amdgpu_ring *ring = adev->rings[i];
  519. if (!ring || !target_seq[i])
  520. continue;
  521. if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
  522. break;
  523. }
  524. if (i != AMDGPU_MAX_RINGS)
  525. continue;
  526. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  527. if (!adev->rings[i] || !target_seq[i])
  528. continue;
  529. if (amdgpu_ring_is_lockup(adev->rings[i]))
  530. break;
  531. }
  532. if (i < AMDGPU_MAX_RINGS) {
  533. /* good news we believe it's a lockup */
  534. dev_warn(adev->dev, "GPU lockup (waiting for "
  535. "0x%016llx last fence id 0x%016llx on"
  536. " ring %d)\n",
  537. target_seq[i], last_seq[i], i);
  538. /* remember that we need an reset */
  539. adev->needs_reset = true;
  540. wake_up_all(&adev->fence_queue);
  541. return -EDEADLK;
  542. }
  543. if (timeout < MAX_SCHEDULE_TIMEOUT) {
  544. timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
  545. if (timeout <= 0) {
  546. return 0;
  547. }
  548. }
  549. }
  550. }
  551. return timeout;
  552. }
  553. /**
  554. * amdgpu_fence_wait - wait for a fence to signal
  555. *
  556. * @fence: amdgpu fence object
  557. * @intr: use interruptable sleep
  558. *
  559. * Wait for the requested fence to signal (all asics).
  560. * @intr selects whether to use interruptable (true) or non-interruptable
  561. * (false) sleep when waiting for the fence.
  562. * Returns 0 if the fence has passed, error for all other cases.
  563. */
  564. int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
  565. {
  566. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  567. long r;
  568. seq[fence->ring->idx] = fence->seq;
  569. r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  570. if (r < 0) {
  571. return r;
  572. }
  573. r = fence_signal(&fence->base);
  574. if (!r)
  575. FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
  576. return 0;
  577. }
  578. /**
  579. * amdgpu_fence_wait_any - wait for a fence to signal on any ring
  580. *
  581. * @adev: amdgpu device pointer
  582. * @fences: amdgpu fence object(s)
  583. * @intr: use interruptable sleep
  584. *
  585. * Wait for any requested fence to signal (all asics). Fence
  586. * array is indexed by ring id. @intr selects whether to use
  587. * interruptable (true) or non-interruptable (false) sleep when
  588. * waiting for the fences. Used by the suballocator.
  589. * Returns 0 if any fence has passed, error for all other cases.
  590. */
  591. int amdgpu_fence_wait_any(struct amdgpu_device *adev,
  592. struct amdgpu_fence **fences,
  593. bool intr)
  594. {
  595. uint64_t seq[AMDGPU_MAX_RINGS];
  596. unsigned i, num_rings = 0;
  597. long r;
  598. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  599. seq[i] = 0;
  600. if (!fences[i]) {
  601. continue;
  602. }
  603. seq[i] = fences[i]->seq;
  604. ++num_rings;
  605. }
  606. /* nothing to wait for ? */
  607. if (num_rings == 0)
  608. return -ENOENT;
  609. r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
  610. if (r < 0) {
  611. return r;
  612. }
  613. return 0;
  614. }
  615. /**
  616. * amdgpu_fence_wait_next - wait for the next fence to signal
  617. *
  618. * @adev: amdgpu device pointer
  619. * @ring: ring index the fence is associated with
  620. *
  621. * Wait for the next fence on the requested ring to signal (all asics).
  622. * Returns 0 if the next fence has passed, error for all other cases.
  623. * Caller must hold ring lock.
  624. */
  625. int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
  626. {
  627. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  628. long r;
  629. seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
  630. if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
  631. /* nothing to wait for, last_seq is
  632. already the last emited fence */
  633. return -ENOENT;
  634. }
  635. r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  636. if (r < 0)
  637. return r;
  638. return 0;
  639. }
  640. /**
  641. * amdgpu_fence_wait_empty - wait for all fences to signal
  642. *
  643. * @adev: amdgpu device pointer
  644. * @ring: ring index the fence is associated with
  645. *
  646. * Wait for all fences on the requested ring to signal (all asics).
  647. * Returns 0 if the fences have passed, error for all other cases.
  648. * Caller must hold ring lock.
  649. */
  650. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
  651. {
  652. struct amdgpu_device *adev = ring->adev;
  653. uint64_t seq[AMDGPU_MAX_RINGS] = {};
  654. long r;
  655. seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
  656. if (!seq[ring->idx])
  657. return 0;
  658. r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
  659. if (r < 0) {
  660. if (r == -EDEADLK)
  661. return -EDEADLK;
  662. dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
  663. ring->idx, r);
  664. }
  665. return 0;
  666. }
  667. /**
  668. * amdgpu_fence_ref - take a ref on a fence
  669. *
  670. * @fence: amdgpu fence object
  671. *
  672. * Take a reference on a fence (all asics).
  673. * Returns the fence.
  674. */
  675. struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
  676. {
  677. fence_get(&fence->base);
  678. return fence;
  679. }
  680. /**
  681. * amdgpu_fence_unref - remove a ref on a fence
  682. *
  683. * @fence: amdgpu fence object
  684. *
  685. * Remove a reference on a fence (all asics).
  686. */
  687. void amdgpu_fence_unref(struct amdgpu_fence **fence)
  688. {
  689. struct amdgpu_fence *tmp = *fence;
  690. *fence = NULL;
  691. if (tmp)
  692. fence_put(&tmp->base);
  693. }
  694. /**
  695. * amdgpu_fence_count_emitted - get the count of emitted fences
  696. *
  697. * @ring: ring the fence is associated with
  698. *
  699. * Get the number of fences emitted on the requested ring (all asics).
  700. * Returns the number of emitted fences on the ring. Used by the
  701. * dynpm code to ring track activity.
  702. */
  703. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
  704. {
  705. uint64_t emitted;
  706. /* We are not protected by ring lock when reading the last sequence
  707. * but it's ok to report slightly wrong fence count here.
  708. */
  709. amdgpu_fence_process(ring);
  710. emitted = ring->fence_drv.sync_seq[ring->idx]
  711. - atomic64_read(&ring->fence_drv.last_seq);
  712. /* to avoid 32bits warp around */
  713. if (emitted > 0x10000000)
  714. emitted = 0x10000000;
  715. return (unsigned)emitted;
  716. }
  717. /**
  718. * amdgpu_fence_need_sync - do we need a semaphore
  719. *
  720. * @fence: amdgpu fence object
  721. * @dst_ring: which ring to check against
  722. *
  723. * Check if the fence needs to be synced against another ring
  724. * (all asics). If so, we need to emit a semaphore.
  725. * Returns true if we need to sync with another ring, false if
  726. * not.
  727. */
  728. bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
  729. struct amdgpu_ring *dst_ring)
  730. {
  731. struct amdgpu_fence_driver *fdrv;
  732. if (!fence)
  733. return false;
  734. if (fence->ring == dst_ring)
  735. return false;
  736. /* we are protected by the ring mutex */
  737. fdrv = &dst_ring->fence_drv;
  738. if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
  739. return false;
  740. return true;
  741. }
  742. /**
  743. * amdgpu_fence_note_sync - record the sync point
  744. *
  745. * @fence: amdgpu fence object
  746. * @dst_ring: which ring to check against
  747. *
  748. * Note the sequence number at which point the fence will
  749. * be synced with the requested ring (all asics).
  750. */
  751. void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
  752. struct amdgpu_ring *dst_ring)
  753. {
  754. struct amdgpu_fence_driver *dst, *src;
  755. unsigned i;
  756. if (!fence)
  757. return;
  758. if (fence->ring == dst_ring)
  759. return;
  760. /* we are protected by the ring mutex */
  761. src = &fence->ring->fence_drv;
  762. dst = &dst_ring->fence_drv;
  763. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  764. if (i == dst_ring->idx)
  765. continue;
  766. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  767. }
  768. }
  769. /**
  770. * amdgpu_fence_driver_start_ring - make the fence driver
  771. * ready for use on the requested ring.
  772. *
  773. * @ring: ring to start the fence driver on
  774. * @irq_src: interrupt source to use for this ring
  775. * @irq_type: interrupt type to use for this ring
  776. *
  777. * Make the fence driver ready for processing (all asics).
  778. * Not all asics have all rings, so each asic will only
  779. * start the fence driver on the rings it has.
  780. * Returns 0 for success, errors for failure.
  781. */
  782. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  783. struct amdgpu_irq_src *irq_src,
  784. unsigned irq_type)
  785. {
  786. struct amdgpu_device *adev = ring->adev;
  787. uint64_t index;
  788. if (ring != &adev->uvd.ring) {
  789. ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
  790. ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
  791. } else {
  792. /* put fence directly behind firmware */
  793. index = ALIGN(adev->uvd.fw->size, 8);
  794. ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
  795. ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
  796. }
  797. amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
  798. ring->fence_drv.initialized = true;
  799. ring->fence_drv.irq_src = irq_src;
  800. ring->fence_drv.irq_type = irq_type;
  801. dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
  802. "cpu addr 0x%p\n", ring->idx,
  803. ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
  804. return 0;
  805. }
  806. /**
  807. * amdgpu_fence_driver_init_ring - init the fence driver
  808. * for the requested ring.
  809. *
  810. * @ring: ring to init the fence driver on
  811. *
  812. * Init the fence driver for the requested ring (all asics).
  813. * Helper function for amdgpu_fence_driver_init().
  814. */
  815. void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
  816. {
  817. int i;
  818. ring->fence_drv.cpu_addr = NULL;
  819. ring->fence_drv.gpu_addr = 0;
  820. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  821. ring->fence_drv.sync_seq[i] = 0;
  822. atomic64_set(&ring->fence_drv.last_seq, 0);
  823. ring->fence_drv.initialized = false;
  824. INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
  825. amdgpu_fence_check_lockup);
  826. ring->fence_drv.ring = ring;
  827. }
  828. /**
  829. * amdgpu_fence_driver_init - init the fence driver
  830. * for all possible rings.
  831. *
  832. * @adev: amdgpu device pointer
  833. *
  834. * Init the fence driver for all possible rings (all asics).
  835. * Not all asics have all rings, so each asic will only
  836. * start the fence driver on the rings it has using
  837. * amdgpu_fence_driver_start_ring().
  838. * Returns 0 for success.
  839. */
  840. int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  841. {
  842. init_waitqueue_head(&adev->fence_queue);
  843. if (amdgpu_debugfs_fence_init(adev))
  844. dev_err(adev->dev, "fence debugfs file creation failed\n");
  845. return 0;
  846. }
  847. /**
  848. * amdgpu_fence_driver_fini - tear down the fence driver
  849. * for all possible rings.
  850. *
  851. * @adev: amdgpu device pointer
  852. *
  853. * Tear down the fence driver for all possible rings (all asics).
  854. */
  855. void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  856. {
  857. int i, r;
  858. mutex_lock(&adev->ring_lock);
  859. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  860. struct amdgpu_ring *ring = adev->rings[i];
  861. if (!ring || !ring->fence_drv.initialized)
  862. continue;
  863. r = amdgpu_fence_wait_empty(ring);
  864. if (r) {
  865. /* no need to trigger GPU reset as we are unloading */
  866. amdgpu_fence_driver_force_completion(adev);
  867. }
  868. wake_up_all(&adev->fence_queue);
  869. ring->fence_drv.initialized = false;
  870. }
  871. mutex_unlock(&adev->ring_lock);
  872. }
  873. /**
  874. * amdgpu_fence_driver_force_completion - force all fence waiter to complete
  875. *
  876. * @adev: amdgpu device pointer
  877. *
  878. * In case of GPU reset failure make sure no process keep waiting on fence
  879. * that will never complete.
  880. */
  881. void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
  882. {
  883. int i;
  884. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  885. struct amdgpu_ring *ring = adev->rings[i];
  886. if (!ring || !ring->fence_drv.initialized)
  887. continue;
  888. amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
  889. }
  890. }
  891. /*
  892. * Fence debugfs
  893. */
  894. #if defined(CONFIG_DEBUG_FS)
  895. static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
  896. {
  897. struct drm_info_node *node = (struct drm_info_node *)m->private;
  898. struct drm_device *dev = node->minor->dev;
  899. struct amdgpu_device *adev = dev->dev_private;
  900. int i, j;
  901. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  902. struct amdgpu_ring *ring = adev->rings[i];
  903. if (!ring || !ring->fence_drv.initialized)
  904. continue;
  905. amdgpu_fence_process(ring);
  906. seq_printf(m, "--- ring %d ---\n", i);
  907. seq_printf(m, "Last signaled fence 0x%016llx\n",
  908. (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
  909. seq_printf(m, "Last emitted 0x%016llx\n",
  910. ring->fence_drv.sync_seq[i]);
  911. for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
  912. struct amdgpu_ring *other = adev->rings[j];
  913. if (i != j && other && other->fence_drv.initialized)
  914. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  915. j, ring->fence_drv.sync_seq[j]);
  916. }
  917. }
  918. return 0;
  919. }
  920. static struct drm_info_list amdgpu_debugfs_fence_list[] = {
  921. {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
  922. };
  923. #endif
  924. int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
  925. {
  926. #if defined(CONFIG_DEBUG_FS)
  927. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
  928. #else
  929. return 0;
  930. #endif
  931. }
  932. static const char *amdgpu_fence_get_driver_name(struct fence *fence)
  933. {
  934. return "amdgpu";
  935. }
  936. static const char *amdgpu_fence_get_timeline_name(struct fence *f)
  937. {
  938. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  939. return (const char *)fence->ring->name;
  940. }
  941. static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
  942. {
  943. return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
  944. }
  945. struct amdgpu_wait_cb {
  946. struct fence_cb base;
  947. struct task_struct *task;
  948. };
  949. static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
  950. {
  951. struct amdgpu_wait_cb *wait =
  952. container_of(cb, struct amdgpu_wait_cb, base);
  953. wake_up_process(wait->task);
  954. }
  955. static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
  956. signed long t)
  957. {
  958. struct amdgpu_fence *fence = to_amdgpu_fence(f);
  959. struct amdgpu_device *adev = fence->ring->adev;
  960. struct amdgpu_wait_cb cb;
  961. cb.task = current;
  962. if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
  963. return t;
  964. while (t > 0) {
  965. if (intr)
  966. set_current_state(TASK_INTERRUPTIBLE);
  967. else
  968. set_current_state(TASK_UNINTERRUPTIBLE);
  969. /*
  970. * amdgpu_test_signaled must be called after
  971. * set_current_state to prevent a race with wake_up_process
  972. */
  973. if (amdgpu_test_signaled(fence))
  974. break;
  975. if (adev->needs_reset) {
  976. t = -EDEADLK;
  977. break;
  978. }
  979. t = schedule_timeout(t);
  980. if (t > 0 && intr && signal_pending(current))
  981. t = -ERESTARTSYS;
  982. }
  983. __set_current_state(TASK_RUNNING);
  984. fence_remove_callback(f, &cb.base);
  985. return t;
  986. }
  987. const struct fence_ops amdgpu_fence_ops = {
  988. .get_driver_name = amdgpu_fence_get_driver_name,
  989. .get_timeline_name = amdgpu_fence_get_timeline_name,
  990. .enable_signaling = amdgpu_fence_enable_signaling,
  991. .signaled = amdgpu_fence_is_signaled,
  992. .wait = amdgpu_fence_default_wait,
  993. .release = NULL,
  994. };