1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 The Linux Foundation. All rights reserved. 4 * Copyright (C) 2013 Red Hat 5 * Author: Rob Clark <robdclark@gmail.com> 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/component.h> 10 #include <linux/platform_device.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/reset.h> 13 14 #include <drm/drm_print.h> 15 16 #include "vc4_drv.h" 17 #include "vc4_regs.h" 18 19 static const struct debugfs_reg32 v3d_regs[] = { 20 VC4_REG32(V3D_IDENT0), 21 VC4_REG32(V3D_IDENT1), 22 VC4_REG32(V3D_IDENT2), 23 VC4_REG32(V3D_SCRATCH), 24 VC4_REG32(V3D_L2CACTL), 25 VC4_REG32(V3D_SLCACTL), 26 VC4_REG32(V3D_INTCTL), 27 VC4_REG32(V3D_INTENA), 28 VC4_REG32(V3D_INTDIS), 29 VC4_REG32(V3D_CT0CS), 30 VC4_REG32(V3D_CT1CS), 31 VC4_REG32(V3D_CT0EA), 32 VC4_REG32(V3D_CT1EA), 33 VC4_REG32(V3D_CT0CA), 34 VC4_REG32(V3D_CT1CA), 35 VC4_REG32(V3D_CT00RA0), 36 VC4_REG32(V3D_CT01RA0), 37 VC4_REG32(V3D_CT0LC), 38 VC4_REG32(V3D_CT1LC), 39 VC4_REG32(V3D_CT0PC), 40 VC4_REG32(V3D_CT1PC), 41 VC4_REG32(V3D_PCS), 42 VC4_REG32(V3D_BFC), 43 VC4_REG32(V3D_RFC), 44 VC4_REG32(V3D_BPCA), 45 VC4_REG32(V3D_BPCS), 46 VC4_REG32(V3D_BPOA), 47 VC4_REG32(V3D_BPOS), 48 VC4_REG32(V3D_BXCF), 49 VC4_REG32(V3D_SQRSV0), 50 VC4_REG32(V3D_SQRSV1), 51 VC4_REG32(V3D_SQCNTL), 52 VC4_REG32(V3D_SRQPC), 53 VC4_REG32(V3D_SRQUA), 54 VC4_REG32(V3D_SRQUL), 55 VC4_REG32(V3D_SRQCS), 56 VC4_REG32(V3D_VPACNTL), 57 VC4_REG32(V3D_VPMBASE), 58 VC4_REG32(V3D_PCTRC), 59 VC4_REG32(V3D_PCTRE), 60 VC4_REG32(V3D_PCTR(0)), 61 VC4_REG32(V3D_PCTRS(0)), 62 VC4_REG32(V3D_PCTR(1)), 63 VC4_REG32(V3D_PCTRS(1)), 64 VC4_REG32(V3D_PCTR(2)), 65 VC4_REG32(V3D_PCTRS(2)), 66 VC4_REG32(V3D_PCTR(3)), 67 VC4_REG32(V3D_PCTRS(3)), 68 VC4_REG32(V3D_PCTR(4)), 69 VC4_REG32(V3D_PCTRS(4)), 70 VC4_REG32(V3D_PCTR(5)), 71 VC4_REG32(V3D_PCTRS(5)), 72 VC4_REG32(V3D_PCTR(6)), 73 VC4_REG32(V3D_PCTRS(6)), 74 VC4_REG32(V3D_PCTR(7)), 75 VC4_REG32(V3D_PCTRS(7)), 76 VC4_REG32(V3D_PCTR(8)), 77 VC4_REG32(V3D_PCTRS(8)), 78 VC4_REG32(V3D_PCTR(9)), 79 VC4_REG32(V3D_PCTRS(9)), 80 VC4_REG32(V3D_PCTR(10)), 81 VC4_REG32(V3D_PCTRS(10)), 82 VC4_REG32(V3D_PCTR(11)), 83 VC4_REG32(V3D_PCTRS(11)), 84 VC4_REG32(V3D_PCTR(12)), 85 VC4_REG32(V3D_PCTRS(12)), 86 VC4_REG32(V3D_PCTR(13)), 87 VC4_REG32(V3D_PCTRS(13)), 88 VC4_REG32(V3D_PCTR(14)), 89 VC4_REG32(V3D_PCTRS(14)), 90 VC4_REG32(V3D_PCTR(15)), 91 VC4_REG32(V3D_PCTRS(15)), 92 VC4_REG32(V3D_DBGE), 93 VC4_REG32(V3D_FDBGO), 94 VC4_REG32(V3D_FDBGB), 95 VC4_REG32(V3D_FDBGR), 96 VC4_REG32(V3D_FDBGS), 97 VC4_REG32(V3D_ERRSTAT), 98 }; 99 100 static int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused) 101 { 102 struct drm_debugfs_entry *entry = m->private; 103 struct drm_device *dev = entry->dev; 104 struct vc4_dev *vc4 = to_vc4_dev(dev); 105 int ret = vc4_v3d_pm_get(vc4); 106 107 if (ret == 0) { 108 uint32_t ident1 = V3D_READ(V3D_IDENT1); 109 uint32_t nslc = VC4_GET_FIELD(ident1, V3D_IDENT1_NSLC); 110 uint32_t tups = VC4_GET_FIELD(ident1, V3D_IDENT1_TUPS); 111 uint32_t qups = VC4_GET_FIELD(ident1, V3D_IDENT1_QUPS); 112 113 seq_printf(m, "Revision: %d\n", 114 VC4_GET_FIELD(ident1, V3D_IDENT1_REV)); 115 seq_printf(m, "Slices: %d\n", nslc); 116 seq_printf(m, "TMUs: %d\n", nslc * tups); 117 seq_printf(m, "QPUs: %d\n", nslc * qups); 118 seq_printf(m, "Semaphores: %d\n", 119 VC4_GET_FIELD(ident1, V3D_IDENT1_NSEM)); 120 vc4_v3d_pm_put(vc4); 121 } 122 123 return 0; 124 } 125 126 int 127 vc4_v3d_pm_get(struct vc4_dev *vc4) 128 { 129 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 130 return -ENODEV; 131 132 return pm_runtime_resume_and_get(&vc4->v3d->pdev->dev); 133 } 134 135 void 136 vc4_v3d_pm_put(struct vc4_dev *vc4) 137 { 138 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 139 return; 140 141 pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev); 142 } 143 144 void vc4_v3d_init_hw(struct drm_device *dev) 145 { 146 struct vc4_dev *vc4 = to_vc4_dev(dev); 147 148 /* Take all the memory that would have been reserved for user 149 * QPU programs, since we don't have an interface for running 150 * them, anyway. 151 */ 152 V3D_WRITE(V3D_VPMBASE, 0); 153 } 154 155 int vc4_v3d_get_bin_slot(struct vc4_dev *vc4) 156 { 157 struct drm_device *dev = &vc4->base; 158 unsigned long irqflags; 159 int slot; 160 uint64_t seqno = 0; 161 struct vc4_exec_info *exec; 162 163 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 164 return -ENODEV; 165 166 try_again: 167 spin_lock_irqsave(&vc4->job_lock, irqflags); 168 slot = ffs(~vc4->bin_alloc_used); 169 if (slot != 0) { 170 /* Switch from ffs() bit index to a 0-based index. */ 171 slot--; 172 vc4->bin_alloc_used |= BIT(slot); 173 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 174 return slot; 175 } 176 177 /* Couldn't find an open slot. Wait for render to complete 178 * and try again. 179 */ 180 exec = vc4_last_render_job(vc4); 181 if (exec) 182 seqno = exec->seqno; 183 spin_unlock_irqrestore(&vc4->job_lock, irqflags); 184 185 if (seqno) { 186 int ret = vc4_wait_for_seqno(dev, seqno, ~0ull, true); 187 188 if (ret == 0) 189 goto try_again; 190 191 return ret; 192 } 193 194 return -ENOMEM; 195 } 196 197 /* 198 * bin_bo_alloc() - allocates the memory that will be used for 199 * tile binning. 200 * 201 * The binner has a limitation that the addresses in the tile state 202 * buffer that point into the tile alloc buffer or binner overflow 203 * memory only have 28 bits (256MB), and the top 4 on the bus for 204 * tile alloc references end up coming from the tile state buffer's 205 * address. 206 * 207 * To work around this, we allocate a single large buffer while V3D is 208 * in use, make sure that it has the top 4 bits constant across its 209 * entire extent, and then put the tile state, tile alloc, and binner 210 * overflow memory inside that buffer. 211 * 212 * This creates a limitation where we may not be able to execute a job 213 * if it doesn't fit within the buffer that we allocated up front. 214 * However, it turns out that 16MB is "enough for anybody", and 215 * real-world applications run into allocation failures from the 216 * overall DMA pool before they make scenes complicated enough to run 217 * out of bin space. 218 */ 219 static int bin_bo_alloc(struct vc4_dev *vc4) 220 { 221 struct vc4_v3d *v3d = vc4->v3d; 222 uint32_t size = 16 * 1024 * 1024; 223 int ret = 0; 224 struct list_head list; 225 226 if (!v3d) 227 return -ENODEV; 228 229 /* We may need to try allocating more than once to get a BO 230 * that doesn't cross 256MB. Track the ones we've allocated 231 * that failed so far, so that we can free them when we've got 232 * one that succeeded (if we freed them right away, our next 233 * allocation would probably be the same chunk of memory). 234 */ 235 INIT_LIST_HEAD(&list); 236 237 while (true) { 238 struct vc4_bo *bo = vc4_bo_create(&vc4->base, size, true, 239 VC4_BO_TYPE_BIN); 240 241 if (IS_ERR(bo)) { 242 ret = PTR_ERR(bo); 243 244 dev_err(&v3d->pdev->dev, 245 "Failed to allocate memory for tile binning: " 246 "%d. You may need to enable DMA or give it " 247 "more memory.", 248 ret); 249 break; 250 } 251 252 /* Check if this BO won't trigger the addressing bug. */ 253 if ((bo->base.dma_addr & 0xf0000000) == 254 ((bo->base.dma_addr + bo->base.base.size - 1) & 0xf0000000)) { 255 vc4->bin_bo = bo; 256 257 /* Set up for allocating 512KB chunks of 258 * binner memory. The biggest allocation we 259 * need to do is for the initial tile alloc + 260 * tile state buffer. We can render to a 261 * maximum of ((2048*2048) / (32*32) = 4096 262 * tiles in a frame (until we do floating 263 * point rendering, at which point it would be 264 * 8192). Tile state is 48b/tile (rounded to 265 * a page), and tile alloc is 32b/tile 266 * (rounded to a page), plus a page of extra, 267 * for a total of 320kb for our worst-case. 268 * We choose 512kb so that it divides evenly 269 * into our 16MB, and the rest of the 512kb 270 * will be used as storage for the overflow 271 * from the initial 32b CL per bin. 272 */ 273 vc4->bin_alloc_size = 512 * 1024; 274 vc4->bin_alloc_used = 0; 275 vc4->bin_alloc_overflow = 0; 276 WARN_ON_ONCE(sizeof(vc4->bin_alloc_used) * 8 != 277 bo->base.base.size / vc4->bin_alloc_size); 278 279 kref_init(&vc4->bin_bo_kref); 280 281 /* Enable the out-of-memory interrupt to set our 282 * newly-allocated binner BO, potentially from an 283 * already-pending-but-masked interrupt. 284 */ 285 V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM); 286 287 break; 288 } 289 290 /* Put it on the list to free later, and try again. */ 291 list_add(&bo->unref_head, &list); 292 } 293 294 /* Free all the BOs we allocated but didn't choose. */ 295 while (!list_empty(&list)) { 296 struct vc4_bo *bo = list_last_entry(&list, 297 struct vc4_bo, unref_head); 298 299 list_del(&bo->unref_head); 300 drm_gem_object_put(&bo->base.base); 301 } 302 303 return ret; 304 } 305 306 int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used) 307 { 308 int ret = 0; 309 310 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 311 return -ENODEV; 312 313 mutex_lock(&vc4->bin_bo_lock); 314 315 if (used && *used) 316 goto complete; 317 318 if (vc4->bin_bo) 319 kref_get(&vc4->bin_bo_kref); 320 else 321 ret = bin_bo_alloc(vc4); 322 323 if (ret == 0 && used) 324 *used = true; 325 326 complete: 327 mutex_unlock(&vc4->bin_bo_lock); 328 329 return ret; 330 } 331 332 static void bin_bo_release(struct kref *ref) 333 { 334 struct vc4_dev *vc4 = container_of(ref, struct vc4_dev, bin_bo_kref); 335 336 if (WARN_ON_ONCE(!vc4->bin_bo)) 337 return; 338 339 drm_gem_object_put(&vc4->bin_bo->base.base); 340 vc4->bin_bo = NULL; 341 } 342 343 void vc4_v3d_bin_bo_put(struct vc4_dev *vc4) 344 { 345 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 346 return; 347 348 mutex_lock(&vc4->bin_bo_lock); 349 kref_put(&vc4->bin_bo_kref, bin_bo_release); 350 mutex_unlock(&vc4->bin_bo_lock); 351 } 352 353 #ifdef CONFIG_PM 354 static int vc4_v3d_runtime_suspend(struct device *dev) 355 { 356 struct vc4_v3d *v3d = dev_get_drvdata(dev); 357 struct vc4_dev *vc4 = v3d->vc4; 358 359 vc4_irq_disable(&vc4->base); 360 361 clk_disable_unprepare(v3d->clk); 362 363 return 0; 364 } 365 366 static int vc4_v3d_runtime_resume(struct device *dev) 367 { 368 struct vc4_v3d *v3d = dev_get_drvdata(dev); 369 struct vc4_dev *vc4 = v3d->vc4; 370 int ret; 371 372 ret = clk_prepare_enable(v3d->clk); 373 if (ret != 0) 374 return ret; 375 376 vc4_v3d_init_hw(&vc4->base); 377 378 vc4_irq_enable(&vc4->base); 379 380 return 0; 381 } 382 #endif 383 384 int vc4_v3d_debugfs_init(struct drm_minor *minor) 385 { 386 struct drm_device *drm = minor->dev; 387 struct vc4_dev *vc4 = to_vc4_dev(drm); 388 struct vc4_v3d *v3d = vc4->v3d; 389 390 if (!vc4->v3d) 391 return -ENODEV; 392 393 drm_debugfs_add_file(drm, "v3d_ident", vc4_v3d_debugfs_ident, NULL); 394 395 vc4_debugfs_add_regset32(drm, "v3d_regs", &v3d->regset); 396 397 return 0; 398 } 399 400 static int vc4_v3d_bind(struct device *dev, struct device *master, void *data) 401 { 402 struct platform_device *pdev = to_platform_device(dev); 403 struct drm_device *drm = dev_get_drvdata(master); 404 struct vc4_dev *vc4 = to_vc4_dev(drm); 405 struct vc4_v3d *v3d = NULL; 406 int ret; 407 408 v3d = devm_kzalloc(&pdev->dev, sizeof(*v3d), GFP_KERNEL); 409 if (!v3d) 410 return -ENOMEM; 411 412 dev_set_drvdata(dev, v3d); 413 414 v3d->pdev = pdev; 415 416 v3d->regs = vc4_ioremap_regs(pdev, 0); 417 if (IS_ERR(v3d->regs)) 418 return PTR_ERR(v3d->regs); 419 v3d->regset.base = v3d->regs; 420 v3d->regset.regs = v3d_regs; 421 v3d->regset.nregs = ARRAY_SIZE(v3d_regs); 422 423 vc4->v3d = v3d; 424 v3d->vc4 = vc4; 425 426 v3d->clk = devm_clk_get_optional(dev, NULL); 427 if (IS_ERR(v3d->clk)) 428 return dev_err_probe(dev, PTR_ERR(v3d->clk), "Failed to get V3D clock\n"); 429 430 v3d->reset = devm_reset_control_get_optional_exclusive(dev, NULL); 431 if (IS_ERR(v3d->reset)) 432 return dev_err_probe(dev, PTR_ERR(v3d->reset), 433 "Failed to get reset control\n"); 434 435 if (!v3d->reset) 436 drm_warn(drm, "No V3D reset line in the device tree"); 437 438 ret = platform_get_irq(pdev, 0); 439 if (ret < 0) 440 return ret; 441 vc4->irq = ret; 442 443 ret = devm_pm_runtime_enable(dev); 444 if (ret) 445 return ret; 446 447 ret = pm_runtime_resume_and_get(dev); 448 if (ret) 449 return ret; 450 451 if (V3D_READ(V3D_IDENT0) != V3D_EXPECTED_IDENT0) { 452 drm_err(drm, "V3D_IDENT0 read 0x%08x instead of 0x%08x\n", 453 V3D_READ(V3D_IDENT0), V3D_EXPECTED_IDENT0); 454 ret = -EINVAL; 455 goto err_put_runtime_pm; 456 } 457 458 /* Reset the binner overflow address/size at setup, to be sure 459 * we don't reuse an old one. 460 */ 461 V3D_WRITE(V3D_BPOA, 0); 462 V3D_WRITE(V3D_BPOS, 0); 463 464 ret = vc4_irq_install(drm, vc4->irq); 465 if (ret) { 466 drm_err(drm, "Failed to install IRQ handler\n"); 467 goto err_put_runtime_pm; 468 } 469 470 pm_runtime_use_autosuspend(dev); 471 pm_runtime_set_autosuspend_delay(dev, 40); /* a little over 2 frames. */ 472 pm_runtime_put_autosuspend(dev); 473 474 return 0; 475 476 err_put_runtime_pm: 477 pm_runtime_put(dev); 478 479 return ret; 480 } 481 482 static void vc4_v3d_unbind(struct device *dev, struct device *master, 483 void *data) 484 { 485 struct drm_device *drm = data; 486 struct vc4_dev *vc4 = to_vc4_dev(drm); 487 488 vc4_irq_uninstall(drm); 489 490 /* Disable the binner's overflow memory address, so the next 491 * driver probe (if any) doesn't try to reuse our old 492 * allocation. 493 */ 494 V3D_WRITE(V3D_BPOA, 0); 495 V3D_WRITE(V3D_BPOS, 0); 496 497 vc4->v3d = NULL; 498 } 499 500 static const struct dev_pm_ops vc4_v3d_pm_ops = { 501 SET_RUNTIME_PM_OPS(vc4_v3d_runtime_suspend, vc4_v3d_runtime_resume, NULL) 502 }; 503 504 static const struct component_ops vc4_v3d_ops = { 505 .bind = vc4_v3d_bind, 506 .unbind = vc4_v3d_unbind, 507 }; 508 509 static int vc4_v3d_dev_probe(struct platform_device *pdev) 510 { 511 return component_add(&pdev->dev, &vc4_v3d_ops); 512 } 513 514 static void vc4_v3d_dev_remove(struct platform_device *pdev) 515 { 516 component_del(&pdev->dev, &vc4_v3d_ops); 517 } 518 519 const struct of_device_id vc4_v3d_dt_match[] = { 520 { .compatible = "brcm,bcm2835-v3d" }, 521 { .compatible = "brcm,cygnus-v3d" }, 522 {} 523 }; 524 525 struct platform_driver vc4_v3d_driver = { 526 .probe = vc4_v3d_dev_probe, 527 .remove = vc4_v3d_dev_remove, 528 .driver = { 529 .name = "vc4_v3d", 530 .of_match_table = vc4_v3d_dt_match, 531 .pm = &vc4_v3d_pm_ops, 532 }, 533 }; 534