radeon_fence.c 25 KB

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