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