radeon_fence.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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, ring, (*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. *
  276. * Wait for the requested sequence number(s) to be written by any ring
  277. * (all asics). Sequnce number array is indexed by ring id.
  278. * @intr selects whether to use interruptable (true) or non-interruptable
  279. * (false) sleep when waiting for the sequence number. Helper function
  280. * for radeon_fence_wait_*().
  281. * Returns 0 if the sequence number has passed, error for all other cases.
  282. * -EDEADLK is returned when a GPU lockup has been detected.
  283. */
  284. static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 *target_seq,
  285. bool intr)
  286. {
  287. uint64_t last_seq[RADEON_NUM_RINGS];
  288. bool signaled;
  289. int i, r;
  290. while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
  291. /* Save current sequence values, used to check for GPU lockups */
  292. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  293. if (!target_seq[i])
  294. continue;
  295. last_seq[i] = atomic64_read(&rdev->fence_drv[i].last_seq);
  296. trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
  297. radeon_irq_kms_sw_irq_get(rdev, i);
  298. }
  299. if (intr) {
  300. r = wait_event_interruptible_timeout(rdev->fence_queue, (
  301. (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
  302. || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
  303. } else {
  304. r = wait_event_timeout(rdev->fence_queue, (
  305. (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
  306. || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
  307. }
  308. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  309. if (!target_seq[i])
  310. continue;
  311. radeon_irq_kms_sw_irq_put(rdev, i);
  312. trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
  313. }
  314. if (unlikely(r < 0))
  315. return r;
  316. if (unlikely(!signaled)) {
  317. if (rdev->needs_reset)
  318. return -EDEADLK;
  319. /* we were interrupted for some reason and fence
  320. * isn't signaled yet, resume waiting */
  321. if (r)
  322. continue;
  323. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  324. if (!target_seq[i])
  325. continue;
  326. if (last_seq[i] != atomic64_read(&rdev->fence_drv[i].last_seq))
  327. break;
  328. }
  329. if (i != RADEON_NUM_RINGS)
  330. continue;
  331. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  332. if (!target_seq[i])
  333. continue;
  334. if (radeon_ring_is_lockup(rdev, i, &rdev->ring[i]))
  335. break;
  336. }
  337. if (i < RADEON_NUM_RINGS) {
  338. /* good news we believe it's a lockup */
  339. dev_warn(rdev->dev, "GPU lockup (waiting for "
  340. "0x%016llx last fence id 0x%016llx on"
  341. " ring %d)\n",
  342. target_seq[i], last_seq[i], i);
  343. /* remember that we need an reset */
  344. rdev->needs_reset = true;
  345. wake_up_all(&rdev->fence_queue);
  346. return -EDEADLK;
  347. }
  348. }
  349. }
  350. return 0;
  351. }
  352. /**
  353. * radeon_fence_wait - wait for a fence to signal
  354. *
  355. * @fence: radeon fence object
  356. * @intr: use interruptable sleep
  357. *
  358. * Wait for the requested fence to signal (all asics).
  359. * @intr selects whether to use interruptable (true) or non-interruptable
  360. * (false) sleep when waiting for the fence.
  361. * Returns 0 if the fence has passed, error for all other cases.
  362. */
  363. int radeon_fence_wait(struct radeon_fence *fence, bool intr)
  364. {
  365. uint64_t seq[RADEON_NUM_RINGS] = {};
  366. int r;
  367. if (fence == NULL) {
  368. WARN(1, "Querying an invalid fence : %p !\n", fence);
  369. return -EINVAL;
  370. }
  371. seq[fence->ring] = fence->seq;
  372. if (seq[fence->ring] == RADEON_FENCE_SIGNALED_SEQ)
  373. return 0;
  374. r = radeon_fence_wait_seq(fence->rdev, seq, intr);
  375. if (r)
  376. return r;
  377. fence->seq = RADEON_FENCE_SIGNALED_SEQ;
  378. return 0;
  379. }
  380. /**
  381. * radeon_fence_wait_any - wait for a fence to signal on any ring
  382. *
  383. * @rdev: radeon device pointer
  384. * @fences: radeon fence object(s)
  385. * @intr: use interruptable sleep
  386. *
  387. * Wait for any requested fence to signal (all asics). Fence
  388. * array is indexed by ring id. @intr selects whether to use
  389. * interruptable (true) or non-interruptable (false) sleep when
  390. * waiting for the fences. Used by the suballocator.
  391. * Returns 0 if any fence has passed, error for all other cases.
  392. */
  393. int radeon_fence_wait_any(struct radeon_device *rdev,
  394. struct radeon_fence **fences,
  395. bool intr)
  396. {
  397. uint64_t seq[RADEON_NUM_RINGS];
  398. unsigned i, num_rings = 0;
  399. int r;
  400. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  401. seq[i] = 0;
  402. if (!fences[i]) {
  403. continue;
  404. }
  405. seq[i] = fences[i]->seq;
  406. ++num_rings;
  407. /* test if something was allready signaled */
  408. if (seq[i] == RADEON_FENCE_SIGNALED_SEQ)
  409. return 0;
  410. }
  411. /* nothing to wait for ? */
  412. if (num_rings == 0)
  413. return -ENOENT;
  414. r = radeon_fence_wait_seq(rdev, seq, intr);
  415. if (r) {
  416. return r;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * radeon_fence_wait_next - wait for the next fence to signal
  422. *
  423. * @rdev: radeon device pointer
  424. * @ring: ring index the fence is associated with
  425. *
  426. * Wait for the next fence on the requested ring to signal (all asics).
  427. * Returns 0 if the next fence has passed, error for all other cases.
  428. * Caller must hold ring lock.
  429. */
  430. int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
  431. {
  432. uint64_t seq[RADEON_NUM_RINGS] = {};
  433. seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
  434. if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
  435. /* nothing to wait for, last_seq is
  436. already the last emited fence */
  437. return -ENOENT;
  438. }
  439. return radeon_fence_wait_seq(rdev, seq, false);
  440. }
  441. /**
  442. * radeon_fence_wait_empty - wait for all fences to signal
  443. *
  444. * @rdev: radeon device pointer
  445. * @ring: ring index the fence is associated with
  446. *
  447. * Wait for all fences on the requested ring to signal (all asics).
  448. * Returns 0 if the fences have passed, error for all other cases.
  449. * Caller must hold ring lock.
  450. */
  451. int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
  452. {
  453. uint64_t seq[RADEON_NUM_RINGS] = {};
  454. int r;
  455. seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
  456. if (!seq[ring])
  457. return 0;
  458. r = radeon_fence_wait_seq(rdev, seq, false);
  459. if (r) {
  460. if (r == -EDEADLK)
  461. return -EDEADLK;
  462. dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
  463. ring, r);
  464. }
  465. return 0;
  466. }
  467. /**
  468. * radeon_fence_ref - take a ref on a fence
  469. *
  470. * @fence: radeon fence object
  471. *
  472. * Take a reference on a fence (all asics).
  473. * Returns the fence.
  474. */
  475. struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
  476. {
  477. kref_get(&fence->kref);
  478. return fence;
  479. }
  480. /**
  481. * radeon_fence_unref - remove a ref on a fence
  482. *
  483. * @fence: radeon fence object
  484. *
  485. * Remove a reference on a fence (all asics).
  486. */
  487. void radeon_fence_unref(struct radeon_fence **fence)
  488. {
  489. struct radeon_fence *tmp = *fence;
  490. *fence = NULL;
  491. if (tmp) {
  492. kref_put(&tmp->kref, radeon_fence_destroy);
  493. }
  494. }
  495. /**
  496. * radeon_fence_count_emitted - get the count of emitted fences
  497. *
  498. * @rdev: radeon device pointer
  499. * @ring: ring index the fence is associated with
  500. *
  501. * Get the number of fences emitted on the requested ring (all asics).
  502. * Returns the number of emitted fences on the ring. Used by the
  503. * dynpm code to ring track activity.
  504. */
  505. unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
  506. {
  507. uint64_t emitted;
  508. /* We are not protected by ring lock when reading the last sequence
  509. * but it's ok to report slightly wrong fence count here.
  510. */
  511. radeon_fence_process(rdev, ring);
  512. emitted = rdev->fence_drv[ring].sync_seq[ring]
  513. - atomic64_read(&rdev->fence_drv[ring].last_seq);
  514. /* to avoid 32bits warp around */
  515. if (emitted > 0x10000000) {
  516. emitted = 0x10000000;
  517. }
  518. return (unsigned)emitted;
  519. }
  520. /**
  521. * radeon_fence_need_sync - do we need a semaphore
  522. *
  523. * @fence: radeon fence object
  524. * @dst_ring: which ring to check against
  525. *
  526. * Check if the fence needs to be synced against another ring
  527. * (all asics). If so, we need to emit a semaphore.
  528. * Returns true if we need to sync with another ring, false if
  529. * not.
  530. */
  531. bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
  532. {
  533. struct radeon_fence_driver *fdrv;
  534. if (!fence) {
  535. return false;
  536. }
  537. if (fence->ring == dst_ring) {
  538. return false;
  539. }
  540. /* we are protected by the ring mutex */
  541. fdrv = &fence->rdev->fence_drv[dst_ring];
  542. if (fence->seq <= fdrv->sync_seq[fence->ring]) {
  543. return false;
  544. }
  545. return true;
  546. }
  547. /**
  548. * radeon_fence_note_sync - record the sync point
  549. *
  550. * @fence: radeon fence object
  551. * @dst_ring: which ring to check against
  552. *
  553. * Note the sequence number at which point the fence will
  554. * be synced with the requested ring (all asics).
  555. */
  556. void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
  557. {
  558. struct radeon_fence_driver *dst, *src;
  559. unsigned i;
  560. if (!fence) {
  561. return;
  562. }
  563. if (fence->ring == dst_ring) {
  564. return;
  565. }
  566. /* we are protected by the ring mutex */
  567. src = &fence->rdev->fence_drv[fence->ring];
  568. dst = &fence->rdev->fence_drv[dst_ring];
  569. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  570. if (i == dst_ring) {
  571. continue;
  572. }
  573. dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
  574. }
  575. }
  576. /**
  577. * radeon_fence_driver_start_ring - make the fence driver
  578. * ready for use on the requested ring.
  579. *
  580. * @rdev: radeon device pointer
  581. * @ring: ring index to start the fence driver on
  582. *
  583. * Make the fence driver ready for processing (all asics).
  584. * Not all asics have all rings, so each asic will only
  585. * start the fence driver on the rings it has.
  586. * Returns 0 for success, errors for failure.
  587. */
  588. int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
  589. {
  590. uint64_t index;
  591. int r;
  592. radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
  593. if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
  594. rdev->fence_drv[ring].scratch_reg = 0;
  595. if (ring != R600_RING_TYPE_UVD_INDEX) {
  596. index = R600_WB_EVENT_OFFSET + ring * 4;
  597. rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
  598. rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
  599. index;
  600. } else {
  601. /* put fence directly behind firmware */
  602. index = ALIGN(rdev->uvd_fw->size, 8);
  603. rdev->fence_drv[ring].cpu_addr = rdev->uvd.cpu_addr + index;
  604. rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
  605. }
  606. } else {
  607. r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
  608. if (r) {
  609. dev_err(rdev->dev, "fence failed to get scratch register\n");
  610. return r;
  611. }
  612. index = RADEON_WB_SCRATCH_OFFSET +
  613. rdev->fence_drv[ring].scratch_reg -
  614. rdev->scratch.reg_base;
  615. rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
  616. rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
  617. }
  618. radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
  619. rdev->fence_drv[ring].initialized = true;
  620. dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016llx and cpu addr 0x%p\n",
  621. ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
  622. return 0;
  623. }
  624. /**
  625. * radeon_fence_driver_init_ring - init the fence driver
  626. * for the requested ring.
  627. *
  628. * @rdev: radeon device pointer
  629. * @ring: ring index to start the fence driver on
  630. *
  631. * Init the fence driver for the requested ring (all asics).
  632. * Helper function for radeon_fence_driver_init().
  633. */
  634. static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
  635. {
  636. int i;
  637. rdev->fence_drv[ring].scratch_reg = -1;
  638. rdev->fence_drv[ring].cpu_addr = NULL;
  639. rdev->fence_drv[ring].gpu_addr = 0;
  640. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  641. rdev->fence_drv[ring].sync_seq[i] = 0;
  642. atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
  643. rdev->fence_drv[ring].initialized = false;
  644. }
  645. /**
  646. * radeon_fence_driver_init - init the fence driver
  647. * for all possible rings.
  648. *
  649. * @rdev: radeon device pointer
  650. *
  651. * Init the fence driver for all possible rings (all asics).
  652. * Not all asics have all rings, so each asic will only
  653. * start the fence driver on the rings it has using
  654. * radeon_fence_driver_start_ring().
  655. * Returns 0 for success.
  656. */
  657. int radeon_fence_driver_init(struct radeon_device *rdev)
  658. {
  659. int ring;
  660. init_waitqueue_head(&rdev->fence_queue);
  661. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  662. radeon_fence_driver_init_ring(rdev, ring);
  663. }
  664. if (radeon_debugfs_fence_init(rdev)) {
  665. dev_err(rdev->dev, "fence debugfs file creation failed\n");
  666. }
  667. return 0;
  668. }
  669. /**
  670. * radeon_fence_driver_fini - tear down the fence driver
  671. * for all possible rings.
  672. *
  673. * @rdev: radeon device pointer
  674. *
  675. * Tear down the fence driver for all possible rings (all asics).
  676. */
  677. void radeon_fence_driver_fini(struct radeon_device *rdev)
  678. {
  679. int ring, r;
  680. mutex_lock(&rdev->ring_lock);
  681. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  682. if (!rdev->fence_drv[ring].initialized)
  683. continue;
  684. r = radeon_fence_wait_empty(rdev, ring);
  685. if (r) {
  686. /* no need to trigger GPU reset as we are unloading */
  687. radeon_fence_driver_force_completion(rdev);
  688. }
  689. wake_up_all(&rdev->fence_queue);
  690. radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
  691. rdev->fence_drv[ring].initialized = false;
  692. }
  693. mutex_unlock(&rdev->ring_lock);
  694. }
  695. /**
  696. * radeon_fence_driver_force_completion - force all fence waiter to complete
  697. *
  698. * @rdev: radeon device pointer
  699. *
  700. * In case of GPU reset failure make sure no process keep waiting on fence
  701. * that will never complete.
  702. */
  703. void radeon_fence_driver_force_completion(struct radeon_device *rdev)
  704. {
  705. int ring;
  706. for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
  707. if (!rdev->fence_drv[ring].initialized)
  708. continue;
  709. radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
  710. }
  711. }
  712. /*
  713. * Fence debugfs
  714. */
  715. #if defined(CONFIG_DEBUG_FS)
  716. static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
  717. {
  718. struct drm_info_node *node = (struct drm_info_node *)m->private;
  719. struct drm_device *dev = node->minor->dev;
  720. struct radeon_device *rdev = dev->dev_private;
  721. int i, j;
  722. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  723. if (!rdev->fence_drv[i].initialized)
  724. continue;
  725. radeon_fence_process(rdev, i);
  726. seq_printf(m, "--- ring %d ---\n", i);
  727. seq_printf(m, "Last signaled fence 0x%016llx\n",
  728. (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
  729. seq_printf(m, "Last emitted 0x%016llx\n",
  730. rdev->fence_drv[i].sync_seq[i]);
  731. for (j = 0; j < RADEON_NUM_RINGS; ++j) {
  732. if (i != j && rdev->fence_drv[j].initialized)
  733. seq_printf(m, "Last sync to ring %d 0x%016llx\n",
  734. j, rdev->fence_drv[i].sync_seq[j]);
  735. }
  736. }
  737. return 0;
  738. }
  739. /**
  740. * radeon_debugfs_gpu_reset - manually trigger a gpu reset
  741. *
  742. * Manually trigger a gpu reset at the next fence wait.
  743. */
  744. static int radeon_debugfs_gpu_reset(struct seq_file *m, void *data)
  745. {
  746. struct drm_info_node *node = (struct drm_info_node *) m->private;
  747. struct drm_device *dev = node->minor->dev;
  748. struct radeon_device *rdev = dev->dev_private;
  749. down_read(&rdev->exclusive_lock);
  750. seq_printf(m, "%d\n", rdev->needs_reset);
  751. rdev->needs_reset = true;
  752. up_read(&rdev->exclusive_lock);
  753. return 0;
  754. }
  755. static struct drm_info_list radeon_debugfs_fence_list[] = {
  756. {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
  757. {"radeon_gpu_reset", &radeon_debugfs_gpu_reset, 0, NULL}
  758. };
  759. #endif
  760. int radeon_debugfs_fence_init(struct radeon_device *rdev)
  761. {
  762. #if defined(CONFIG_DEBUG_FS)
  763. return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 2);
  764. #else
  765. return 0;
  766. #endif
  767. }