v3d_gem.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2014-2018 Broadcom */
  3. #include <linux/device.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/io.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/reset.h>
  9. #include <linux/sched/signal.h>
  10. #include <linux/uaccess.h>
  11. #include <drm/drm_managed.h>
  12. #include "v3d_drv.h"
  13. #include "v3d_regs.h"
  14. #include "v3d_trace.h"
  15. static void
  16. v3d_init_core(struct v3d_dev *v3d, int core)
  17. {
  18. /* Set OVRTMUOUT, which means that the texture sampler uniform
  19. * configuration's tmu output type field is used, instead of
  20. * using the hardware default behavior based on the texture
  21. * type. If you want the default behavior, you can still put
  22. * "2" in the indirect texture state's output_type field.
  23. */
  24. if (v3d->ver < 40)
  25. V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
  26. /* Whenever we flush the L2T cache, we always want to flush
  27. * the whole thing.
  28. */
  29. V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
  30. V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
  31. }
  32. /* Sets invariant state for the HW. */
  33. static void
  34. v3d_init_hw_state(struct v3d_dev *v3d)
  35. {
  36. v3d_init_core(v3d, 0);
  37. }
  38. static void
  39. v3d_idle_axi(struct v3d_dev *v3d, int core)
  40. {
  41. V3D_CORE_WRITE(core, V3D_GMP_CFG(v3d->ver), V3D_GMP_CFG_STOP_REQ);
  42. if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS(v3d->ver)) &
  43. (V3D_GMP_STATUS_RD_COUNT_MASK |
  44. V3D_GMP_STATUS_WR_COUNT_MASK |
  45. V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
  46. DRM_ERROR("Failed to wait for safe GMP shutdown\n");
  47. }
  48. }
  49. static void
  50. v3d_idle_gca(struct v3d_dev *v3d)
  51. {
  52. if (v3d->ver >= 41)
  53. return;
  54. V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
  55. if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
  56. V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
  57. V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
  58. DRM_ERROR("Failed to wait for safe GCA shutdown\n");
  59. }
  60. }
  61. static void
  62. v3d_reset_by_bridge(struct v3d_dev *v3d)
  63. {
  64. int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
  65. if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
  66. V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
  67. V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
  68. V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
  69. /* GFXH-1383: The SW_INIT may cause a stray write to address 0
  70. * of the unit, so reset it to its power-on value here.
  71. */
  72. V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
  73. } else {
  74. WARN_ON_ONCE(V3D_GET_FIELD(version,
  75. V3D_TOP_GR_BRIDGE_MAJOR) != 7);
  76. V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
  77. V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
  78. V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
  79. }
  80. }
  81. static void
  82. v3d_reset_v3d(struct v3d_dev *v3d)
  83. {
  84. if (v3d->reset)
  85. reset_control_reset(v3d->reset);
  86. else
  87. v3d_reset_by_bridge(v3d);
  88. v3d_init_hw_state(v3d);
  89. }
  90. void
  91. v3d_reset(struct v3d_dev *v3d)
  92. {
  93. struct drm_device *dev = &v3d->drm;
  94. DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n");
  95. DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n",
  96. V3D_CORE_READ(0, V3D_ERR_STAT));
  97. trace_v3d_reset_begin(dev);
  98. /* XXX: only needed for safe powerdown, not reset. */
  99. if (false)
  100. v3d_idle_axi(v3d, 0);
  101. v3d_irq_disable(v3d);
  102. v3d_idle_gca(v3d);
  103. v3d_reset_v3d(v3d);
  104. v3d_mmu_set_page_table(v3d);
  105. v3d_irq_reset(v3d);
  106. v3d_perfmon_stop(v3d, v3d->active_perfmon, false);
  107. trace_v3d_reset_end(dev);
  108. }
  109. static void
  110. v3d_flush_l3(struct v3d_dev *v3d)
  111. {
  112. if (v3d->ver < 41) {
  113. u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
  114. V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
  115. gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
  116. if (v3d->ver < 33) {
  117. V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
  118. gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
  119. }
  120. }
  121. }
  122. /* Invalidates the (read-only) L2C cache. This was the L2 cache for
  123. * uniforms and instructions on V3D 3.2.
  124. */
  125. static void
  126. v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
  127. {
  128. if (v3d->ver > 32)
  129. return;
  130. V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
  131. V3D_L2CACTL_L2CCLR |
  132. V3D_L2CACTL_L2CENA);
  133. }
  134. /* Invalidates texture L2 cachelines */
  135. static void
  136. v3d_flush_l2t(struct v3d_dev *v3d, int core)
  137. {
  138. /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
  139. * need to wait for completion before dispatching the job --
  140. * L2T accesses will be stalled until the flush has completed.
  141. * However, we do need to make sure we don't try to trigger a
  142. * new flush while the L2_CLEAN queue is trying to
  143. * synchronously clean after a job.
  144. */
  145. mutex_lock(&v3d->cache_clean_lock);
  146. V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
  147. V3D_L2TCACTL_L2TFLS |
  148. V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
  149. mutex_unlock(&v3d->cache_clean_lock);
  150. }
  151. /* Cleans texture L1 and L2 cachelines (writing back dirty data).
  152. *
  153. * For cleaning, which happens from the CACHE_CLEAN queue after CSD has
  154. * executed, we need to make sure that the clean is done before
  155. * signaling job completion. So, we synchronously wait before
  156. * returning, and we make sure that L2 invalidates don't happen in the
  157. * meantime to confuse our are-we-done checks.
  158. */
  159. void
  160. v3d_clean_caches(struct v3d_dev *v3d)
  161. {
  162. struct drm_device *dev = &v3d->drm;
  163. int core = 0;
  164. trace_v3d_cache_clean_begin(dev);
  165. V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
  166. if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
  167. V3D_L2TCACTL_TMUWCF), 100)) {
  168. DRM_ERROR("Timeout waiting for TMU write combiner flush\n");
  169. }
  170. mutex_lock(&v3d->cache_clean_lock);
  171. V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
  172. V3D_L2TCACTL_L2TFLS |
  173. V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM));
  174. if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
  175. V3D_L2TCACTL_L2TFLS), 100)) {
  176. DRM_ERROR("Timeout waiting for L2T clean\n");
  177. }
  178. mutex_unlock(&v3d->cache_clean_lock);
  179. trace_v3d_cache_clean_end(dev);
  180. }
  181. /* Invalidates the slice caches. These are read-only caches. */
  182. static void
  183. v3d_invalidate_slices(struct v3d_dev *v3d, int core)
  184. {
  185. V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
  186. V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
  187. V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
  188. V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  189. V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  190. }
  191. void
  192. v3d_invalidate_caches(struct v3d_dev *v3d)
  193. {
  194. /* Invalidate the caches from the outside in. That way if
  195. * another CL's concurrent use of nearby memory were to pull
  196. * an invalidated cacheline back in, we wouldn't leave stale
  197. * data in the inner cache.
  198. */
  199. v3d_flush_l3(v3d);
  200. v3d_invalidate_l2c(v3d, 0);
  201. v3d_flush_l2t(v3d, 0);
  202. v3d_invalidate_slices(v3d, 0);
  203. }
  204. int
  205. v3d_gem_init(struct drm_device *dev)
  206. {
  207. struct v3d_dev *v3d = to_v3d_dev(dev);
  208. u32 pt_size = 4096 * 1024;
  209. int ret, i;
  210. for (i = 0; i < V3D_MAX_QUEUES; i++) {
  211. struct v3d_queue_state *queue = &v3d->queue[i];
  212. queue->fence_context = dma_fence_context_alloc(1);
  213. memset(&queue->stats, 0, sizeof(queue->stats));
  214. seqcount_init(&queue->stats.lock);
  215. }
  216. spin_lock_init(&v3d->mm_lock);
  217. spin_lock_init(&v3d->job_lock);
  218. ret = drmm_mutex_init(dev, &v3d->bo_lock);
  219. if (ret)
  220. return ret;
  221. ret = drmm_mutex_init(dev, &v3d->reset_lock);
  222. if (ret)
  223. return ret;
  224. ret = drmm_mutex_init(dev, &v3d->sched_lock);
  225. if (ret)
  226. return ret;
  227. ret = drmm_mutex_init(dev, &v3d->cache_clean_lock);
  228. if (ret)
  229. return ret;
  230. /* Note: We don't allocate address 0. Various bits of HW
  231. * treat 0 as special, such as the occlusion query counters
  232. * where 0 means "disabled".
  233. */
  234. drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
  235. v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size,
  236. &v3d->pt_paddr,
  237. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  238. if (!v3d->pt) {
  239. drm_mm_takedown(&v3d->mm);
  240. dev_err(v3d->drm.dev,
  241. "Failed to allocate page tables. Please ensure you have DMA enabled.\n");
  242. return -ENOMEM;
  243. }
  244. v3d_init_hw_state(v3d);
  245. v3d_mmu_set_page_table(v3d);
  246. ret = v3d_sched_init(v3d);
  247. if (ret) {
  248. drm_mm_takedown(&v3d->mm);
  249. dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
  250. v3d->pt_paddr);
  251. }
  252. return 0;
  253. }
  254. void
  255. v3d_gem_destroy(struct drm_device *dev)
  256. {
  257. struct v3d_dev *v3d = to_v3d_dev(dev);
  258. v3d_sched_fini(v3d);
  259. /* Waiting for jobs to finish would need to be done before
  260. * unregistering V3D.
  261. */
  262. WARN_ON(v3d->bin_job);
  263. WARN_ON(v3d->render_job);
  264. WARN_ON(v3d->tfu_job);
  265. WARN_ON(v3d->csd_job);
  266. drm_mm_takedown(&v3d->mm);
  267. dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
  268. v3d->pt_paddr);
  269. }