radeon_fence.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 "radeon_reg.h"
  39. #include "radeon.h"
  40. #include "radeon_trace.h"
  41. /*
  42. * Fences
  43. * Fences mark an event in the GPUs pipeline and are used
  44. * for GPU/CPU synchronization. When the fence is written,
  45. * it is expected that all buffers associated with that fence
  46. * are no longer in use by the associated ring on the GPU and
  47. * that the the relevant GPU caches have been flushed. Whether
  48. * we use a scratch register or memory location depends on the asic
  49. * and whether writeback is enabled.
  50. */
  51. /**
  52. * radeon_fence_write - write a fence value
  53. *
  54. * @rdev: radeon_device pointer
  55. * @seq: sequence number to write
  56. * @ring: ring index the fence is associated with
  57. *
  58. * Writes a fence value to memory or a scratch register (all asics).
  59. */
  60. static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
  61. {
  62. struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
  63. if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
  64. if (drv->cpu_addr) {
  65. *drv->cpu_addr = cpu_to_le32(seq);
  66. }
  67. } else {
  68. WREG32(drv->scratch_reg, seq);
  69. }
  70. }
  71. /**
  72. * radeon_fence_read - read a fence value
  73. *
  74. * @rdev: radeon_device pointer
  75. * @ring: ring index the fence is associated with
  76. *
  77. * Reads a fence value from memory or a scratch register (all asics).
  78. * Returns the value of the fence read from memory or register.
  79. */
  80. static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
  81. {
  82. struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
  83. u32 seq = 0;
  84. if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
  85. if (drv->cpu_addr) {
  86. seq = le32_to_cpu(*drv->cpu_addr);
  87. } else {
  88. seq = lower_32_bits(atomic64_read(&drv->last_seq));
  89. }
  90. } else {
  91. seq = RREG32(drv->scratch_reg);
  92. }
  93. return seq;
  94. }
  95. /**
  96. * radeon_fence_emit - emit a fence on the requested ring
  97. *
  98. * @rdev: radeon_device pointer
  99. * @fence: radeon fence object
  100. * @ring: ring index the fence is associated with
  101. *
  102. * Emits a fence command on the requested ring (all asics).
  103. * Returns 0 on success, -ENOMEM on failure.
  104. */
  105. int radeon_fence_emit(struct radeon_device *rdev,
  106. struct radeon_fence **fence,
  107. int ring)
  108. {
  109. /* we are protected by the ring emission mutex */
  110. *fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
  111. if ((*fence) == NULL) {
  112. return -ENOMEM;
  113. }
  114. kref_init(&((*fence)->kref));
  115. (*fence)->rdev = rdev;
  116. (*fence)->seq = ++rdev->fence_drv[ring].sync_seq[ring];
  117. (*fence)->ring = ring;
  118. radeon_fence_ring_emit(rdev, ring, *fence);
  119. trace_radeon_fence_emit(rdev->ddev, (*fence)->seq);
  120. return 0;
  121. }
  122. /**
  123. * radeon_fence_process - process a fence
  124. *
  125. * @rdev: radeon_device pointer
  126. * @ring: ring index the fence is associated with
  127. *
  128. * Checks the current fence value and wakes the fence queue
  129. * if the sequence number has increased (all asics).
  130. */
  131. void radeon_fence_process(struct radeon_device *rdev, int ring)
  132. {
  133. uint64_t seq, last_seq, last_emitted;
  134. unsigned count_loop = 0;
  135. bool wake = false;
  136. /* Note there is a scenario here for an infinite loop but it's
  137. * very unlikely to happen. For it to happen, the current polling
  138. * process need to be interrupted by another process and another
  139. * process needs to update the last_seq btw the atomic read and
  140. * xchg of the current process.
  141. *
  142. * More over for this to go in infinite loop there need to be
  143. * continuously new fence signaled ie radeon_fence_read needs
  144. * to return a different value each time for both the currently
  145. * polling process and the other process that xchg the last_seq
  146. * btw atomic read and xchg of the current process. And the
  147. * value the other process set as last seq must be higher than
  148. * the seq value we just read. Which means that current process
  149. * need to be interrupted after radeon_fence_read and before
  150. * atomic xchg.
  151. *
  152. * To be even more safe we count the number of time we loop and
  153. * we bail after 10 loop just accepting the fact that we might
  154. * have temporarly set the last_seq not to the true real last
  155. * seq but to an older one.
  156. */
  157. last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
  158. do {
  159. last_emitted = rdev->fence_drv[ring].sync_seq[ring];
  160. seq = radeon_fence_read(rdev, ring);
  161. seq |= last_seq & 0xffffffff00000000LL;
  162. if (seq < last_seq) {
  163. seq &= 0xffffffff;
  164. seq |= last_emitted & 0xffffffff00000000LL;
  165. }
  166. if (seq <= last_seq || seq > last_emitted) {
  167. break;
  168. }
  169. /* If we loop over we don't want to return without
  170. * checking if a fence is signaled as it means that the
  171. * seq we just read is different from the previous on.
  172. */
  173. wake = true;
  174. last_seq = seq;
  175. if ((count_loop++) > 10) {
  176. /* We looped over too many time leave with the
  177. * fact that we might have set an older fence
  178. * seq then the current real last seq as signaled
  179. * by the hw.
  180. */
  181. break;
  182. }
  183. } while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
  184. if (wake)
  185. wake_up_all(&rdev->fence_queue);
  186. }
  187. /**
  188. * radeon_fence_destroy - destroy a fence
  189. *
  190. * @kref: fence kref
  191. *
  192. * Frees the fence object (all asics).
  193. */
  194. static void radeon_fence_destroy(struct kref *kref)
  195. {
  196. struct radeon_fence *fence;
  197. fence = container_of(kref, struct radeon_fence, kref);
  198. kfree(fence);
  199. }
  200. /**
  201. * radeon_fence_seq_signaled - check if a fence sequence number has signaled
  202. *
  203. * @rdev: radeon device pointer
  204. * @seq: sequence number
  205. * @ring: ring index the fence is associated with
  206. *
  207. * Check if the last signaled fence sequnce number is >= the requested
  208. * sequence number (all asics).
  209. * Returns true if the fence has signaled (current fence value
  210. * is >= requested value) or false if it has not (current fence
  211. * value is < the requested value. Helper function for
  212. * radeon_fence_signaled().
  213. */
  214. static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
  215. u64 seq, unsigned ring)
  216. {
  217. if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
  218. return true;
  219. }
  220. /* poll new last sequence at least once */
  221. radeon_fence_process(rdev, ring);
  222. if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. /**
  228. * radeon_fence_signaled - check if a fence has signaled
  229. *
  230. * @fence: radeon fence object
  231. *
  232. * Check if the requested fence has signaled (all asics).
  233. * Returns true if the fence has signaled or false if it has not.
  234. */
  235. bool radeon_fence_signaled(struct radeon_fence *fence)
  236. {
  237. if (!fence) {
  238. return true;
  239. }
  240. if (fence->seq == RADEON_FENCE_SIGNALED_SEQ) {
  241. return true;
  242. }
  243. if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
  244. fence->seq = RADEON_FENCE_SIGNALED_SEQ;
  245. return true;
  246. }
  247. return false;
  248. }
  249. /**
  250. * radeon_fence_any_seq_signaled - check if any sequence number is signaled
  251. *
  252. * @rdev: radeon device pointer
  253. * @seq: sequence numbers
  254. *
  255. * Check if the last signaled fence sequnce number is >= the requested
  256. * sequence number (all asics).
  257. * Returns true if any has signaled (current value is >= requested value)
  258. * or false if it has not. Helper function for radeon_fence_wait_seq.
  259. */
  260. static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
  261. {
  262. unsigned i;
  263. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  264. if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
  265. return true;
  266. }
  267. return false;
  268. }
  269. /**
  270. * radeon_fence_wait_seq - wait for a specific sequence numbers
  271. *
  272. * @rdev: radeon device pointer
  273. * @target_seq: sequence number(s) we want to wait for
  274. * @intr: use interruptable sleep
  275. * @lock_ring: whether the ring should be locked or not
  276. *
  277. * Wait for the requested sequence number(s) to be written by any ring
  278. * (all asics). Sequnce number array is indexed by ring id.
  279. * @intr selects whether to use interruptable (true) or non-interruptable
  280. * (false) sleep when waiting for the sequence number. Helper function
  281. * for radeon_fence_wait_*().
  282. * Returns 0 if the sequence number has passed, error for all other cases.
  283. * -EDEADLK is returned when a GPU lockup has been detected.
  284. */
  285. static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 *target_seq,
  286. bool intr, bool lock_ring)
  287. {
  288. uint64_t last_seq[RADEON_NUM_RINGS];
  289. bool signaled;
  290. int i, r;
  291. while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
  292. /* Save current sequence values, used to check for GPU lockups */
  293. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  294. if (!target_seq[i])
  295. continue;
  296. last_seq[i] = atomic64_read(&rdev->fence_drv[i].last_seq);
  297. trace_radeon_fence_wait_begin(rdev->ddev, target_seq[i]);
  298. radeon_irq_kms_sw_irq_get(rdev, i);
  299. }
  300. if (intr) {
  301. r = wait_event_interruptible_timeout(rdev->fence_queue, (
  302. (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
  303. || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
  304. } else {
  305. r = wait_event_timeout(rdev->fence_queue, (
  306. (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
  307. || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
  308. }
  309. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  310. if (!target_seq[i])
  311. continue;
  312. radeon_irq_kms_sw_irq_put(rdev, i);
  313. trace_radeon_fence_wait_end(rdev->ddev, target_seq[i]);
  314. }
  315. if (unlikely(r < 0))
  316. return r;
  317. if (unlikely(!signaled)) {
  318. if (rdev->needs_reset)
  319. return -EDEADLK;
  320. /* we were interrupted for some reason and fence
  321. * isn't signaled yet, resume waiting */
  322. if (r)
  323. continue;
  324. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  325. if (!target_seq[i])
  326. continue;
  327. if (last_seq[i] != atomic64_read(&rdev->fence_drv[i].last_seq))
  328. break;
  329. }
  330. if (i != RADEON_NUM_RINGS)
  331. continue;
  332. if (lock_ring)
  333. mutex_lock(&rdev->ring_lock);
  334. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  335. if (!target_seq[i])
  336. continue;
  337. if (radeon_ring_is_lockup(rdev, i, &rdev->ring[i]))
  338. break;
  339. }
  340. if (i < RADEON_NUM_RINGS) {
  341. /* good news we believe it's a lockup */
  342. dev_warn(rdev->dev, "GPU lockup (waiting for "
  343. "0x%016llx last fence id 0x%016llx on"
  344. " ring %d)\n",
  345. target_seq[i], last_seq[i], i);
  346. /* remember that we need an reset */
  347. rdev->needs_reset = true;
  348. if (lock_ring)
  349. mutex_unlock(&rdev->ring_lock);
  350. wake_up_all(&rdev->fence_queue);
  351. return -EDEADLK;
  352. }
  353. if (lock_ring)
  354. mutex_unlock(&rdev->ring_lock);
  355. }
  356. }
  357. return 0;
  358. }
  359. /**
  360. * radeon_fence_wait - wait for a fence to signal
  361. *
  362. * @fence: radeon fence object
  363. * @intr: use interruptable sleep
  364. *
  365. * Wait for the requested fence to signal (all asics).
  366. * @intr selects whether to use interruptable (true) or non-interruptable
  367. * (false) sleep when waiting for the fence.
  368. * Returns 0 if the fence has passed, error for all other cases.
  369. */
  370. int radeon_fence_wait(struct radeon_fence *fence, bool intr)
  371. {
  372. uint64_t seq[RADEON_NUM_RINGS] = {};
  373. int r;
  374. if (fence == NULL) {
  375. WARN(1, "Querying an invalid fence : %p !\n", fence);
  376. return -EINVAL;
  377. }
  378. seq[fence->ring] = fence->seq;
  379. if (seq[fence->ring] == RADEON_FENCE_SIGNALED_SEQ)
  380. return 0;
  381. r = radeon_fence_wait_seq(fence->rdev, seq, intr, true);
  382. if (r)
  383. return r;
  384. fence->seq = RADEON_FENCE_SIGNALED_SEQ;
  385. return 0;
  386. }
  387. /**
  388. * radeon_fence_wait_any - wait for a fence to signal on any ring
  389. *
  390. * @rdev: radeon device pointer
  391. * @fences: radeon fence object(s)
  392. * @intr: use interruptable sleep
  393. *
  394. * Wait for any requested fence to signal (all asics). Fence
  395. * array is indexed by ring id. @intr selects whether to use
  396. * interruptable (true) or non-interruptable (false) sleep when
  397. * waiting for the fences. Used by the suballocator.
  398. * Returns 0 if any fence has passed, error for all other cases.
  399. */
  400. int radeon_fence_wait_any(struct radeon_device *rdev,
  401. struct radeon_fence **fences,
  402. bool intr)
  403. {
  404. uint64_t seq[RADEON_NUM_RINGS];
  405. unsigned i, num_rings = 0;
  406. int r;
  407. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  408. seq[i] = 0;
  409. if (!fences[i]) {
  410. continue;
  411. }
  412. seq[i] = fences[i]->seq;
  413. ++num_rings;
  414. /* test if something was allready signaled */
  415. if (seq[i] == RADEON_FENCE_SIGNALED_SEQ)
  416. return 0;
  417. }
  418. /* nothing to wait for ? */
  419. if (num_rings == 0)
  420. return -ENOENT;
  421. r = radeon_fence_wait_seq(rdev, seq, intr, true);
  422. if (r) {
  423. return r;
  424. }
  425. return 0;
  426. }
  427. /**
  428. * radeon_fence_wait_locked - wait for a fence to signal
  429. *
  430. * @fence: radeon fence object
  431. *
  432. * Wait for the requested fence to signal (all asics).
  433. * Returns 0 if the fence has passed, error for all other cases.
  434. */
  435. int radeon_fence_wait_locked(struct radeon_fence *fence)
  436. {
  437. uint64_t seq[RADEON_NUM_RINGS] = {};
  438. int r;
  439. if (fence == NULL) {
  440. WARN(1, "Querying an invalid fence : %p !\n", fence);
  441. return -EINVAL;
  442. }
  443. seq[fence->ring] = fence->seq;
  444. if (seq[fence->ring] == RADEON_FENCE_SIGNALED_SEQ)
  445. return 0;
  446. r = radeon_fence_wait_seq(fence->rdev, seq, false, false);
  447. if (r)
  448. return r;
  449. fence->seq = RADEON_FENCE_SIGNALED_SEQ;
  450. return 0;
  451. }
  452. /**
  453. * radeon_fence_wait_next_locked - wait for the next fence to signal
  454. *
  455. * @rdev: radeon device pointer
  456. * @ring: ring index the fence is associated with
  457. *
  458. * Wait for the next fence on the requested ring to signal (all asics).
  459. * Returns 0 if the next fence has passed, error for all other cases.
  460. * Caller must hold ring lock.
  461. */
  462. int radeon_fence_wait_next_locked(struct radeon_device *rdev, int ring)
  463. {
  464. uint64_t seq[RADEON_NUM_RINGS] = {};
  465. seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
  466. if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
  467. /* nothing to wait for, last_seq is
  468. already the last emited fence */
  469. return -ENOENT;
  470. }
  471. return radeon_fence_wait_seq(rdev, seq, false, false);
  472. }
  473. /**
  474. * radeon_fence_wait_empty_locked - wait for all fences to signal
  475. *
  476. * @rdev: radeon device pointer
  477. * @ring: ring index the fence is associated with
  478. *
  479. * Wait for all fences on the requested ring to signal (all asics).
  480. * Returns 0 if the fences have passed, error for all other cases.
  481. * Caller must hold ring lock.
  482. */
  483. int radeon_fence_wait_empty_locked(struct radeon_device *rdev, int ring)
  484. {
  485. uint64_t seq[RADEON_NUM_RINGS] = {};
  486. int r;
  487. seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
  488. if (!seq[ring])
  489. return 0;
  490. r = radeon_fence_wait_seq(rdev, seq, false, false);
  491. if (r) {
  492. if (r == -EDEADLK)
  493. return -EDEADLK;
  494. dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
  495. ring, r);
  496. }
  497. return 0;
  498. }
  499. /**
  500. * radeon_fence_ref - take a ref on a fence
  501. *
  502. * @fence: radeon fence object
  503. *
  504. * Take a reference on a fence (all asics).
  505. * Returns the fence.
  506. */
  507. struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
  508. {
  509. kref_get(&fence->kref);
  510. return fence;
  511. }
  512. /**
  513. * radeon_fence_unref - remove a ref on a fence
  514. *
  515. * @fence: radeon fence object
  516. *
  517. * Remove a reference on a fence (all asics).
  518. */
  519. void radeon_fence_unref(struct radeon_fence **fence)
  520. {
  521. struct radeon_fence *tmp = *fence;
  522. *fence = NULL;
  523. if (tmp) {
  524. kref_put(&tmp->kref, radeon_fence_destroy);
  525. }
  526. }
  527. /**
  528. * radeon_fence_count_emitted - get the count of emitted fences
  529. *
  530. * @rdev: radeon device pointer
  531. * @ring: ring index the fence is associated with
  532. *
  533. * Get the number of fences emitted on the requested ring (all asics).
  534. * Returns the number of emitted fences on the ring. Used by the
  535. * dynpm code to ring track activity.
  536. */
  537. unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
  538. {
  539. uint64_t emitted;
  540. /* We are not protected by ring lock when reading the last sequence
  541. * but it's ok to report slightly wrong fence count here.
  542. */
  543. radeon_fence_process(rdev, ring);
  544. emitted = rdev->fence_drv[ring].sync_seq[ring]
  545. - atomic64_read(&rdev->fence_drv[ring].last_seq);
  546. /* to avoid 32bits warp around */
  547. if (emitted > 0x10000000) {
  548. emitted = 0x10000000;
  549. }
  550. return (unsigned)emitted;
  551. }
  552. /**
  553. * radeon_fence_need_sync - do we need a semaphore
  554. *
  555. * @fence: radeon fence object
  556. * @dst_ring: which ring to check against
  557. *
  558. * Check if the fence needs to be synced against another ring
  559. * (all asics). If so, we need to emit a semaphore.
  560. * Returns true if we need to sync with another ring, false if
  561. * not.
  562. */
  563. bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
  564. {
  565. struct radeon_fence_driver *fdrv;
  566. if (!fence) {
  567. return false;
  568. }
  569. if (fence->ring == dst_ring) {
  570. return false;
  571. }
  572. /* we are protected by the ring mutex */
  573. fdrv = &fence->rdev->fence_drv[dst_ring];
  574. if (fence->seq <= fdrv->sync_seq[fence->ring]) {
  575. return false;
  576. }
  577. return true;
  578. }
  579. /**
  580. * radeon_fence_note_sync - record the sync point
  581. *
  582. * @fence: radeon fence object
  583. * @dst_ring: which ring to check against
  584. *
  585. * Note the sequence number at which point the fence will
  586. * be synced with the requested ring (all asics).
  587. */
  588. void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
  589. {
  590. struct radeon_fence_driver *dst, *src;
  591. unsigned i;
  592. if (!fence) {
  593. return;
  594. }
  595. if (fence->ring == dst_ring) {
  596. return;
  597. }
  598. /* we are protected by the ring mutex */
  599. src = &fence->rdev->fence_drv[fence->ring];
  600. dst = &fence->rdev->fence_drv[dst_ring];
  601. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  602. if (i == dst_ring) {
  603. continue;
  604. }
  605. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  606. }
  607. }
  608. /**
  609. * radeon_fence_driver_start_ring - make the fence driver
  610. * ready for use on the requested ring.
  611. *
  612. * @rdev: radeon device pointer
  613. * @ring: ring index to start the fence driver on
  614. *
  615. * Make the fence driver ready for processing (all asics).
  616. * Not all asics have all rings, so each asic will only
  617. * start the fence driver on the rings it has.
  618. * Returns 0 for success, errors for failure.
  619. */
  620. int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
  621. {
  622. uint64_t index;
  623. int r;
  624. radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
  625. if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
  626. rdev->fence_drv[ring].scratch_reg = 0;
  627. if (ring != R600_RING_TYPE_UVD_INDEX) {
  628. index = R600_WB_EVENT_OFFSET + ring * 4;
  629. rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
  630. rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
  631. index;
  632. } else {
  633. /* put fence directly behind firmware */
  634. index = ALIGN(rdev->uvd_fw->size, 8);
  635. rdev->fence_drv[ring].cpu_addr = rdev->uvd.cpu_addr + index;
  636. rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
  637. }
  638. } else {
  639. r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
  640. if (r) {
  641. dev_err(rdev->dev, "fence failed to get scratch register\n");
  642. return r;
  643. }
  644. index = RADEON_WB_SCRATCH_OFFSET +
  645. rdev->fence_drv[ring].scratch_reg -
  646. rdev->scratch.reg_base;
  647. rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
  648. rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
  649. }
  650. radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
  651. rdev->fence_drv[ring].initialized = true;
  652. dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016llx and cpu addr 0x%p\n",
  653. ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
  654. return 0;
  655. }
  656. /**
  657. * radeon_fence_driver_init_ring - init the fence driver
  658. * for the requested ring.
  659. *
  660. * @rdev: radeon device pointer
  661. * @ring: ring index to start the fence driver on
  662. *
  663. * Init the fence driver for the requested ring (all asics).
  664. * Helper function for radeon_fence_driver_init().
  665. */
  666. static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
  667. {
  668. int i;
  669. rdev->fence_drv[ring].scratch_reg = -1;
  670. rdev->fence_drv[ring].cpu_addr = NULL;
  671. rdev->fence_drv[ring].gpu_addr = 0;
  672. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  673. rdev->fence_drv[ring].sync_seq[i] = 0;
  674. atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
  675. rdev->fence_drv[ring].initialized = false;
  676. }
  677. /**
  678. * radeon_fence_driver_init - init the fence driver
  679. * for all possible rings.
  680. *
  681. * @rdev: radeon device pointer
  682. *
  683. * Init the fence driver for all possible rings (all asics).
  684. * Not all asics have all rings, so each asic will only
  685. * start the fence driver on the rings it has using
  686. * radeon_fence_driver_start_ring().
  687. * Returns 0 for success.
  688. */
  689. int radeon_fence_driver_init(struct radeon_device *rdev)
  690. {
  691. int ring;
  692. init_waitqueue_head(&rdev->fence_queue);
  693. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  694. radeon_fence_driver_init_ring(rdev, ring);
  695. }
  696. if (radeon_debugfs_fence_init(rdev)) {
  697. dev_err(rdev->dev, "fence debugfs file creation failed\n");
  698. }
  699. return 0;
  700. }
  701. /**
  702. * radeon_fence_driver_fini - tear down the fence driver
  703. * for all possible rings.
  704. *
  705. * @rdev: radeon device pointer
  706. *
  707. * Tear down the fence driver for all possible rings (all asics).
  708. */
  709. void radeon_fence_driver_fini(struct radeon_device *rdev)
  710. {
  711. int ring, r;
  712. mutex_lock(&rdev->ring_lock);
  713. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  714. if (!rdev->fence_drv[ring].initialized)
  715. continue;
  716. r = radeon_fence_wait_empty_locked(rdev, ring);
  717. if (r) {
  718. /* no need to trigger GPU reset as we are unloading */
  719. radeon_fence_driver_force_completion(rdev);
  720. }
  721. wake_up_all(&rdev->fence_queue);
  722. radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
  723. rdev->fence_drv[ring].initialized = false;
  724. }
  725. mutex_unlock(&rdev->ring_lock);
  726. }
  727. /**
  728. * radeon_fence_driver_force_completion - force all fence waiter to complete
  729. *
  730. * @rdev: radeon device pointer
  731. *
  732. * In case of GPU reset failure make sure no process keep waiting on fence
  733. * that will never complete.
  734. */
  735. void radeon_fence_driver_force_completion(struct radeon_device *rdev)
  736. {
  737. int ring;
  738. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  739. if (!rdev->fence_drv[ring].initialized)
  740. continue;
  741. radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
  742. }
  743. }
  744. /*
  745. * Fence debugfs
  746. */
  747. #if defined(CONFIG_DEBUG_FS)
  748. static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
  749. {
  750. struct drm_info_node *node = (struct drm_info_node *)m->private;
  751. struct drm_device *dev = node->minor->dev;
  752. struct radeon_device *rdev = dev->dev_private;
  753. int i, j;
  754. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  755. if (!rdev->fence_drv[i].initialized)
  756. continue;
  757. seq_printf(m, "--- ring %d ---\n", i);
  758. seq_printf(m, "Last signaled fence 0x%016llx\n",
  759. (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
  760. seq_printf(m, "Last emitted 0x%016llx\n",
  761. rdev->fence_drv[i].sync_seq[i]);
  762. for (j = 0; j < RADEON_NUM_RINGS; ++j) {
  763. if (i != j && rdev->fence_drv[j].initialized)
  764. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  765. j, rdev->fence_drv[i].sync_seq[j]);
  766. }
  767. }
  768. return 0;
  769. }
  770. static struct drm_info_list radeon_debugfs_fence_list[] = {
  771. {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
  772. };
  773. #endif
  774. int radeon_debugfs_fence_init(struct radeon_device *rdev)
  775. {
  776. #if defined(CONFIG_DEBUG_FS)
  777. return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);
  778. #else
  779. return 0;
  780. #endif
  781. }