a5xx_preempt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include "msm_gem.h"
  14. #include "a5xx_gpu.h"
  15. /*
  16. * Try to transition the preemption state from old to new. Return
  17. * true on success or false if the original state wasn't 'old'
  18. */
  19. static inline bool try_preempt_state(struct a5xx_gpu *a5xx_gpu,
  20. enum preempt_state old, enum preempt_state new)
  21. {
  22. enum preempt_state cur = atomic_cmpxchg(&a5xx_gpu->preempt_state,
  23. old, new);
  24. return (cur == old);
  25. }
  26. /*
  27. * Force the preemption state to the specified state. This is used in cases
  28. * where the current state is known and won't change
  29. */
  30. static inline void set_preempt_state(struct a5xx_gpu *gpu,
  31. enum preempt_state new)
  32. {
  33. /*
  34. * preempt_state may be read by other cores trying to trigger a
  35. * preemption or in the interrupt handler so barriers are needed
  36. * before...
  37. */
  38. smp_mb__before_atomic();
  39. atomic_set(&gpu->preempt_state, new);
  40. /* ... and after*/
  41. smp_mb__after_atomic();
  42. }
  43. /* Write the most recent wptr for the given ring into the hardware */
  44. static inline void update_wptr(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  45. {
  46. unsigned long flags;
  47. uint32_t wptr;
  48. if (!ring)
  49. return;
  50. spin_lock_irqsave(&ring->lock, flags);
  51. wptr = get_wptr(ring);
  52. spin_unlock_irqrestore(&ring->lock, flags);
  53. gpu_write(gpu, REG_A5XX_CP_RB_WPTR, wptr);
  54. }
  55. /* Return the highest priority ringbuffer with something in it */
  56. static struct msm_ringbuffer *get_next_ring(struct msm_gpu *gpu)
  57. {
  58. unsigned long flags;
  59. int i;
  60. for (i = 0; i < gpu->nr_rings; i++) {
  61. bool empty;
  62. struct msm_ringbuffer *ring = gpu->rb[i];
  63. spin_lock_irqsave(&ring->lock, flags);
  64. empty = (get_wptr(ring) == ring->memptrs->rptr);
  65. spin_unlock_irqrestore(&ring->lock, flags);
  66. if (!empty)
  67. return ring;
  68. }
  69. return NULL;
  70. }
  71. static void a5xx_preempt_timer(struct timer_list *t)
  72. {
  73. struct a5xx_gpu *a5xx_gpu = from_timer(a5xx_gpu, t, preempt_timer);
  74. struct msm_gpu *gpu = &a5xx_gpu->base.base;
  75. struct drm_device *dev = gpu->dev;
  76. struct msm_drm_private *priv = dev->dev_private;
  77. if (!try_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED, PREEMPT_FAULTED))
  78. return;
  79. dev_err(dev->dev, "%s: preemption timed out\n", gpu->name);
  80. queue_work(priv->wq, &gpu->recover_work);
  81. }
  82. /* Try to trigger a preemption switch */
  83. void a5xx_preempt_trigger(struct msm_gpu *gpu)
  84. {
  85. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  86. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  87. unsigned long flags;
  88. struct msm_ringbuffer *ring;
  89. if (gpu->nr_rings == 1)
  90. return;
  91. /*
  92. * Try to start preemption by moving from NONE to START. If
  93. * unsuccessful, a preemption is already in flight
  94. */
  95. if (!try_preempt_state(a5xx_gpu, PREEMPT_NONE, PREEMPT_START))
  96. return;
  97. /* Get the next ring to preempt to */
  98. ring = get_next_ring(gpu);
  99. /*
  100. * If no ring is populated or the highest priority ring is the current
  101. * one do nothing except to update the wptr to the latest and greatest
  102. */
  103. if (!ring || (a5xx_gpu->cur_ring == ring)) {
  104. /*
  105. * Its possible that while a preemption request is in progress
  106. * from an irq context, a user context trying to submit might
  107. * fail to update the write pointer, because it determines
  108. * that the preempt state is not PREEMPT_NONE.
  109. *
  110. * Close the race by introducing an intermediate
  111. * state PREEMPT_ABORT to let the submit path
  112. * know that the ringbuffer is not going to change
  113. * and can safely update the write pointer.
  114. */
  115. set_preempt_state(a5xx_gpu, PREEMPT_ABORT);
  116. update_wptr(gpu, a5xx_gpu->cur_ring);
  117. set_preempt_state(a5xx_gpu, PREEMPT_NONE);
  118. return;
  119. }
  120. /* Make sure the wptr doesn't update while we're in motion */
  121. spin_lock_irqsave(&ring->lock, flags);
  122. a5xx_gpu->preempt[ring->id]->wptr = get_wptr(ring);
  123. spin_unlock_irqrestore(&ring->lock, flags);
  124. /* Set the address of the incoming preemption record */
  125. gpu_write64(gpu, REG_A5XX_CP_CONTEXT_SWITCH_RESTORE_ADDR_LO,
  126. REG_A5XX_CP_CONTEXT_SWITCH_RESTORE_ADDR_HI,
  127. a5xx_gpu->preempt_iova[ring->id]);
  128. a5xx_gpu->next_ring = ring;
  129. /* Start a timer to catch a stuck preemption */
  130. mod_timer(&a5xx_gpu->preempt_timer, jiffies + msecs_to_jiffies(10000));
  131. /* Set the preemption state to triggered */
  132. set_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED);
  133. /* Make sure everything is written before hitting the button */
  134. wmb();
  135. /* And actually start the preemption */
  136. gpu_write(gpu, REG_A5XX_CP_CONTEXT_SWITCH_CNTL, 1);
  137. }
  138. void a5xx_preempt_irq(struct msm_gpu *gpu)
  139. {
  140. uint32_t status;
  141. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  142. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  143. struct drm_device *dev = gpu->dev;
  144. struct msm_drm_private *priv = dev->dev_private;
  145. if (!try_preempt_state(a5xx_gpu, PREEMPT_TRIGGERED, PREEMPT_PENDING))
  146. return;
  147. /* Delete the preemption watchdog timer */
  148. del_timer(&a5xx_gpu->preempt_timer);
  149. /*
  150. * The hardware should be setting CP_CONTEXT_SWITCH_CNTL to zero before
  151. * firing the interrupt, but there is a non zero chance of a hardware
  152. * condition or a software race that could set it again before we have a
  153. * chance to finish. If that happens, log and go for recovery
  154. */
  155. status = gpu_read(gpu, REG_A5XX_CP_CONTEXT_SWITCH_CNTL);
  156. if (unlikely(status)) {
  157. set_preempt_state(a5xx_gpu, PREEMPT_FAULTED);
  158. dev_err(dev->dev, "%s: Preemption failed to complete\n",
  159. gpu->name);
  160. queue_work(priv->wq, &gpu->recover_work);
  161. return;
  162. }
  163. a5xx_gpu->cur_ring = a5xx_gpu->next_ring;
  164. a5xx_gpu->next_ring = NULL;
  165. update_wptr(gpu, a5xx_gpu->cur_ring);
  166. set_preempt_state(a5xx_gpu, PREEMPT_NONE);
  167. }
  168. void a5xx_preempt_hw_init(struct msm_gpu *gpu)
  169. {
  170. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  171. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  172. int i;
  173. for (i = 0; i < gpu->nr_rings; i++) {
  174. a5xx_gpu->preempt[i]->wptr = 0;
  175. a5xx_gpu->preempt[i]->rptr = 0;
  176. a5xx_gpu->preempt[i]->rbase = gpu->rb[i]->iova;
  177. }
  178. /* Write a 0 to signal that we aren't switching pagetables */
  179. gpu_write64(gpu, REG_A5XX_CP_CONTEXT_SWITCH_SMMU_INFO_LO,
  180. REG_A5XX_CP_CONTEXT_SWITCH_SMMU_INFO_HI, 0);
  181. /* Reset the preemption state */
  182. set_preempt_state(a5xx_gpu, PREEMPT_NONE);
  183. /* Always come up on rb 0 */
  184. a5xx_gpu->cur_ring = gpu->rb[0];
  185. }
  186. static int preempt_init_ring(struct a5xx_gpu *a5xx_gpu,
  187. struct msm_ringbuffer *ring)
  188. {
  189. struct adreno_gpu *adreno_gpu = &a5xx_gpu->base;
  190. struct msm_gpu *gpu = &adreno_gpu->base;
  191. struct a5xx_preempt_record *ptr;
  192. struct drm_gem_object *bo = NULL;
  193. u64 iova = 0;
  194. ptr = msm_gem_kernel_new(gpu->dev,
  195. A5XX_PREEMPT_RECORD_SIZE + A5XX_PREEMPT_COUNTER_SIZE,
  196. MSM_BO_UNCACHED, gpu->aspace, &bo, &iova);
  197. if (IS_ERR(ptr))
  198. return PTR_ERR(ptr);
  199. a5xx_gpu->preempt_bo[ring->id] = bo;
  200. a5xx_gpu->preempt_iova[ring->id] = iova;
  201. a5xx_gpu->preempt[ring->id] = ptr;
  202. /* Set up the defaults on the preemption record */
  203. ptr->magic = A5XX_PREEMPT_RECORD_MAGIC;
  204. ptr->info = 0;
  205. ptr->data = 0;
  206. ptr->cntl = MSM_GPU_RB_CNTL_DEFAULT;
  207. ptr->rptr_addr = rbmemptr(ring, rptr);
  208. ptr->counter = iova + A5XX_PREEMPT_RECORD_SIZE;
  209. return 0;
  210. }
  211. void a5xx_preempt_fini(struct msm_gpu *gpu)
  212. {
  213. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  214. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  215. int i;
  216. for (i = 0; i < gpu->nr_rings; i++) {
  217. if (!a5xx_gpu->preempt_bo[i])
  218. continue;
  219. msm_gem_put_vaddr(a5xx_gpu->preempt_bo[i]);
  220. if (a5xx_gpu->preempt_iova[i])
  221. msm_gem_put_iova(a5xx_gpu->preempt_bo[i], gpu->aspace);
  222. drm_gem_object_unreference(a5xx_gpu->preempt_bo[i]);
  223. a5xx_gpu->preempt_bo[i] = NULL;
  224. }
  225. }
  226. void a5xx_preempt_init(struct msm_gpu *gpu)
  227. {
  228. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  229. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  230. int i;
  231. /* No preemption if we only have one ring */
  232. if (gpu->nr_rings <= 1)
  233. return;
  234. for (i = 0; i < gpu->nr_rings; i++) {
  235. if (preempt_init_ring(a5xx_gpu, gpu->rb[i])) {
  236. /*
  237. * On any failure our adventure is over. Clean up and
  238. * set nr_rings to 1 to force preemption off
  239. */
  240. a5xx_preempt_fini(gpu);
  241. gpu->nr_rings = 1;
  242. return;
  243. }
  244. }
  245. timer_setup(&a5xx_gpu->preempt_timer, a5xx_preempt_timer, 0);
  246. }