1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
9 #include <linux/component.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
16 static const struct debugfs_reg32 v3d_regs[] = {
17 VC4_REG32(V3D_IDENT0),
18 VC4_REG32(V3D_IDENT1),
19 VC4_REG32(V3D_IDENT2),
20 VC4_REG32(V3D_SCRATCH),
21 VC4_REG32(V3D_L2CACTL),
22 VC4_REG32(V3D_SLCACTL),
23 VC4_REG32(V3D_INTCTL),
24 VC4_REG32(V3D_INTENA),
25 VC4_REG32(V3D_INTDIS),
32 VC4_REG32(V3D_CT00RA0),
33 VC4_REG32(V3D_CT01RA0),
46 VC4_REG32(V3D_SQRSV0),
47 VC4_REG32(V3D_SQRSV1),
48 VC4_REG32(V3D_SQCNTL),
53 VC4_REG32(V3D_VPACNTL),
54 VC4_REG32(V3D_VPMBASE),
57 VC4_REG32(V3D_PCTR(0)),
58 VC4_REG32(V3D_PCTRS(0)),
59 VC4_REG32(V3D_PCTR(1)),
60 VC4_REG32(V3D_PCTRS(1)),
61 VC4_REG32(V3D_PCTR(2)),
62 VC4_REG32(V3D_PCTRS(2)),
63 VC4_REG32(V3D_PCTR(3)),
64 VC4_REG32(V3D_PCTRS(3)),
65 VC4_REG32(V3D_PCTR(4)),
66 VC4_REG32(V3D_PCTRS(4)),
67 VC4_REG32(V3D_PCTR(5)),
68 VC4_REG32(V3D_PCTRS(5)),
69 VC4_REG32(V3D_PCTR(6)),
70 VC4_REG32(V3D_PCTRS(6)),
71 VC4_REG32(V3D_PCTR(7)),
72 VC4_REG32(V3D_PCTRS(7)),
73 VC4_REG32(V3D_PCTR(8)),
74 VC4_REG32(V3D_PCTRS(8)),
75 VC4_REG32(V3D_PCTR(9)),
76 VC4_REG32(V3D_PCTRS(9)),
77 VC4_REG32(V3D_PCTR(10)),
78 VC4_REG32(V3D_PCTRS(10)),
79 VC4_REG32(V3D_PCTR(11)),
80 VC4_REG32(V3D_PCTRS(11)),
81 VC4_REG32(V3D_PCTR(12)),
82 VC4_REG32(V3D_PCTRS(12)),
83 VC4_REG32(V3D_PCTR(13)),
84 VC4_REG32(V3D_PCTRS(13)),
85 VC4_REG32(V3D_PCTR(14)),
86 VC4_REG32(V3D_PCTRS(14)),
87 VC4_REG32(V3D_PCTR(15)),
88 VC4_REG32(V3D_PCTRS(15)),
94 VC4_REG32(V3D_ERRSTAT),
97 static int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused)
99 struct drm_debugfs_entry *entry = m->private;
100 struct drm_device *dev = entry->dev;
101 struct vc4_dev *vc4 = to_vc4_dev(dev);
102 int ret = vc4_v3d_pm_get(vc4);
105 uint32_t ident1 = V3D_READ(V3D_IDENT1);
106 uint32_t nslc = VC4_GET_FIELD(ident1, V3D_IDENT1_NSLC);
107 uint32_t tups = VC4_GET_FIELD(ident1, V3D_IDENT1_TUPS);
108 uint32_t qups = VC4_GET_FIELD(ident1, V3D_IDENT1_QUPS);
110 seq_printf(m, "Revision: %d\n",
111 VC4_GET_FIELD(ident1, V3D_IDENT1_REV));
112 seq_printf(m, "Slices: %d\n", nslc);
113 seq_printf(m, "TMUs: %d\n", nslc * tups);
114 seq_printf(m, "QPUs: %d\n", nslc * qups);
115 seq_printf(m, "Semaphores: %d\n",
116 VC4_GET_FIELD(ident1, V3D_IDENT1_NSEM));
124 * Wraps pm_runtime_get_sync() in a refcount, so that we can reliably
125 * get the pm_runtime refcount to 0 in vc4_reset().
128 vc4_v3d_pm_get(struct vc4_dev *vc4)
130 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
133 mutex_lock(&vc4->power_lock);
134 if (vc4->power_refcount++ == 0) {
135 int ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
138 vc4->power_refcount--;
139 mutex_unlock(&vc4->power_lock);
143 mutex_unlock(&vc4->power_lock);
149 vc4_v3d_pm_put(struct vc4_dev *vc4)
151 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
154 mutex_lock(&vc4->power_lock);
155 if (--vc4->power_refcount == 0) {
156 pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
157 pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
159 mutex_unlock(&vc4->power_lock);
162 static void vc4_v3d_init_hw(struct drm_device *dev)
164 struct vc4_dev *vc4 = to_vc4_dev(dev);
166 /* Take all the memory that would have been reserved for user
167 * QPU programs, since we don't have an interface for running
170 V3D_WRITE(V3D_VPMBASE, 0);
173 int vc4_v3d_get_bin_slot(struct vc4_dev *vc4)
175 struct drm_device *dev = &vc4->base;
176 unsigned long irqflags;
179 struct vc4_exec_info *exec;
181 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
185 spin_lock_irqsave(&vc4->job_lock, irqflags);
186 slot = ffs(~vc4->bin_alloc_used);
188 /* Switch from ffs() bit index to a 0-based index. */
190 vc4->bin_alloc_used |= BIT(slot);
191 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
195 /* Couldn't find an open slot. Wait for render to complete
198 exec = vc4_last_render_job(vc4);
201 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
204 int ret = vc4_wait_for_seqno(dev, seqno, ~0ull, true);
216 * bin_bo_alloc() - allocates the memory that will be used for
219 * The binner has a limitation that the addresses in the tile state
220 * buffer that point into the tile alloc buffer or binner overflow
221 * memory only have 28 bits (256MB), and the top 4 on the bus for
222 * tile alloc references end up coming from the tile state buffer's
225 * To work around this, we allocate a single large buffer while V3D is
226 * in use, make sure that it has the top 4 bits constant across its
227 * entire extent, and then put the tile state, tile alloc, and binner
228 * overflow memory inside that buffer.
230 * This creates a limitation where we may not be able to execute a job
231 * if it doesn't fit within the buffer that we allocated up front.
232 * However, it turns out that 16MB is "enough for anybody", and
233 * real-world applications run into allocation failures from the
234 * overall DMA pool before they make scenes complicated enough to run
237 static int bin_bo_alloc(struct vc4_dev *vc4)
239 struct vc4_v3d *v3d = vc4->v3d;
240 uint32_t size = 16 * 1024 * 1024;
242 struct list_head list;
247 /* We may need to try allocating more than once to get a BO
248 * that doesn't cross 256MB. Track the ones we've allocated
249 * that failed so far, so that we can free them when we've got
250 * one that succeeded (if we freed them right away, our next
251 * allocation would probably be the same chunk of memory).
253 INIT_LIST_HEAD(&list);
256 struct vc4_bo *bo = vc4_bo_create(&vc4->base, size, true,
262 dev_err(&v3d->pdev->dev,
263 "Failed to allocate memory for tile binning: "
264 "%d. You may need to enable DMA or give it "
270 /* Check if this BO won't trigger the addressing bug. */
271 if ((bo->base.dma_addr & 0xf0000000) ==
272 ((bo->base.dma_addr + bo->base.base.size - 1) & 0xf0000000)) {
275 /* Set up for allocating 512KB chunks of
276 * binner memory. The biggest allocation we
277 * need to do is for the initial tile alloc +
278 * tile state buffer. We can render to a
279 * maximum of ((2048*2048) / (32*32) = 4096
280 * tiles in a frame (until we do floating
281 * point rendering, at which point it would be
282 * 8192). Tile state is 48b/tile (rounded to
283 * a page), and tile alloc is 32b/tile
284 * (rounded to a page), plus a page of extra,
285 * for a total of 320kb for our worst-case.
286 * We choose 512kb so that it divides evenly
287 * into our 16MB, and the rest of the 512kb
288 * will be used as storage for the overflow
289 * from the initial 32b CL per bin.
291 vc4->bin_alloc_size = 512 * 1024;
292 vc4->bin_alloc_used = 0;
293 vc4->bin_alloc_overflow = 0;
294 WARN_ON_ONCE(sizeof(vc4->bin_alloc_used) * 8 !=
295 bo->base.base.size / vc4->bin_alloc_size);
297 kref_init(&vc4->bin_bo_kref);
299 /* Enable the out-of-memory interrupt to set our
300 * newly-allocated binner BO, potentially from an
301 * already-pending-but-masked interrupt.
303 V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
308 /* Put it on the list to free later, and try again. */
309 list_add(&bo->unref_head, &list);
312 /* Free all the BOs we allocated but didn't choose. */
313 while (!list_empty(&list)) {
314 struct vc4_bo *bo = list_last_entry(&list,
315 struct vc4_bo, unref_head);
317 list_del(&bo->unref_head);
318 drm_gem_object_put(&bo->base.base);
324 int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used)
328 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
331 mutex_lock(&vc4->bin_bo_lock);
337 kref_get(&vc4->bin_bo_kref);
339 ret = bin_bo_alloc(vc4);
341 if (ret == 0 && used)
345 mutex_unlock(&vc4->bin_bo_lock);
350 static void bin_bo_release(struct kref *ref)
352 struct vc4_dev *vc4 = container_of(ref, struct vc4_dev, bin_bo_kref);
354 if (WARN_ON_ONCE(!vc4->bin_bo))
357 drm_gem_object_put(&vc4->bin_bo->base.base);
361 void vc4_v3d_bin_bo_put(struct vc4_dev *vc4)
363 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
366 mutex_lock(&vc4->bin_bo_lock);
367 kref_put(&vc4->bin_bo_kref, bin_bo_release);
368 mutex_unlock(&vc4->bin_bo_lock);
372 static int vc4_v3d_runtime_suspend(struct device *dev)
374 struct vc4_v3d *v3d = dev_get_drvdata(dev);
375 struct vc4_dev *vc4 = v3d->vc4;
377 vc4_irq_disable(&vc4->base);
379 clk_disable_unprepare(v3d->clk);
384 static int vc4_v3d_runtime_resume(struct device *dev)
386 struct vc4_v3d *v3d = dev_get_drvdata(dev);
387 struct vc4_dev *vc4 = v3d->vc4;
390 ret = clk_prepare_enable(v3d->clk);
394 vc4_v3d_init_hw(&vc4->base);
396 vc4_irq_enable(&vc4->base);
402 int vc4_v3d_debugfs_init(struct drm_minor *minor)
404 struct drm_device *drm = minor->dev;
405 struct vc4_dev *vc4 = to_vc4_dev(drm);
406 struct vc4_v3d *v3d = vc4->v3d;
411 drm_debugfs_add_file(drm, "v3d_ident", vc4_v3d_debugfs_ident, NULL);
413 vc4_debugfs_add_regset32(drm, "v3d_regs", &v3d->regset);
418 static int vc4_v3d_bind(struct device *dev, struct device *master, void *data)
420 struct platform_device *pdev = to_platform_device(dev);
421 struct drm_device *drm = dev_get_drvdata(master);
422 struct vc4_dev *vc4 = to_vc4_dev(drm);
423 struct vc4_v3d *v3d = NULL;
426 v3d = devm_kzalloc(&pdev->dev, sizeof(*v3d), GFP_KERNEL);
430 dev_set_drvdata(dev, v3d);
434 v3d->regs = vc4_ioremap_regs(pdev, 0);
435 if (IS_ERR(v3d->regs))
436 return PTR_ERR(v3d->regs);
437 v3d->regset.base = v3d->regs;
438 v3d->regset.regs = v3d_regs;
439 v3d->regset.nregs = ARRAY_SIZE(v3d_regs);
444 v3d->clk = devm_clk_get_optional(dev, NULL);
445 if (IS_ERR(v3d->clk))
446 return dev_err_probe(dev, PTR_ERR(v3d->clk), "Failed to get V3D clock\n");
448 ret = platform_get_irq(pdev, 0);
453 ret = devm_pm_runtime_enable(dev);
457 ret = pm_runtime_resume_and_get(dev);
461 if (V3D_READ(V3D_IDENT0) != V3D_EXPECTED_IDENT0) {
462 drm_err(drm, "V3D_IDENT0 read 0x%08x instead of 0x%08x\n",
463 V3D_READ(V3D_IDENT0), V3D_EXPECTED_IDENT0);
465 goto err_put_runtime_pm;
468 /* Reset the binner overflow address/size at setup, to be sure
469 * we don't reuse an old one.
471 V3D_WRITE(V3D_BPOA, 0);
472 V3D_WRITE(V3D_BPOS, 0);
474 ret = vc4_irq_install(drm, vc4->irq);
476 drm_err(drm, "Failed to install IRQ handler\n");
477 goto err_put_runtime_pm;
480 pm_runtime_use_autosuspend(dev);
481 pm_runtime_set_autosuspend_delay(dev, 40); /* a little over 2 frames. */
491 static void vc4_v3d_unbind(struct device *dev, struct device *master,
494 struct drm_device *drm = dev_get_drvdata(master);
495 struct vc4_dev *vc4 = to_vc4_dev(drm);
497 vc4_irq_uninstall(drm);
499 /* Disable the binner's overflow memory address, so the next
500 * driver probe (if any) doesn't try to reuse our old
503 V3D_WRITE(V3D_BPOA, 0);
504 V3D_WRITE(V3D_BPOS, 0);
509 static const struct dev_pm_ops vc4_v3d_pm_ops = {
510 SET_RUNTIME_PM_OPS(vc4_v3d_runtime_suspend, vc4_v3d_runtime_resume, NULL)
513 static const struct component_ops vc4_v3d_ops = {
514 .bind = vc4_v3d_bind,
515 .unbind = vc4_v3d_unbind,
518 static int vc4_v3d_dev_probe(struct platform_device *pdev)
520 return component_add(&pdev->dev, &vc4_v3d_ops);
523 static void vc4_v3d_dev_remove(struct platform_device *pdev)
525 component_del(&pdev->dev, &vc4_v3d_ops);
528 const struct of_device_id vc4_v3d_dt_match[] = {
529 { .compatible = "brcm,bcm2835-v3d" },
530 { .compatible = "brcm,cygnus-v3d" },
531 { .compatible = "brcm,vc4-v3d" },
535 struct platform_driver vc4_v3d_driver = {
536 .probe = vc4_v3d_dev_probe,
537 .remove = vc4_v3d_dev_remove,
540 .of_match_table = vc4_v3d_dt_match,
541 .pm = &vc4_v3d_pm_ops,