18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014 The Linux Foundation. All rights reserved. 48c2ecf20Sopenharmony_ci * Copyright (C) 2013 Red Hat 58c2ecf20Sopenharmony_ci * Author: Rob Clark <robdclark@gmail.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/component.h> 108c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 118c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <drm/drm_irq.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "vc4_drv.h" 168c2ecf20Sopenharmony_ci#include "vc4_regs.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 v3d_regs[] = { 198c2ecf20Sopenharmony_ci VC4_REG32(V3D_IDENT0), 208c2ecf20Sopenharmony_ci VC4_REG32(V3D_IDENT1), 218c2ecf20Sopenharmony_ci VC4_REG32(V3D_IDENT2), 228c2ecf20Sopenharmony_ci VC4_REG32(V3D_SCRATCH), 238c2ecf20Sopenharmony_ci VC4_REG32(V3D_L2CACTL), 248c2ecf20Sopenharmony_ci VC4_REG32(V3D_SLCACTL), 258c2ecf20Sopenharmony_ci VC4_REG32(V3D_INTCTL), 268c2ecf20Sopenharmony_ci VC4_REG32(V3D_INTENA), 278c2ecf20Sopenharmony_ci VC4_REG32(V3D_INTDIS), 288c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT0CS), 298c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT1CS), 308c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT0EA), 318c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT1EA), 328c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT0CA), 338c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT1CA), 348c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT00RA0), 358c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT01RA0), 368c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT0LC), 378c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT1LC), 388c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT0PC), 398c2ecf20Sopenharmony_ci VC4_REG32(V3D_CT1PC), 408c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCS), 418c2ecf20Sopenharmony_ci VC4_REG32(V3D_BFC), 428c2ecf20Sopenharmony_ci VC4_REG32(V3D_RFC), 438c2ecf20Sopenharmony_ci VC4_REG32(V3D_BPCA), 448c2ecf20Sopenharmony_ci VC4_REG32(V3D_BPCS), 458c2ecf20Sopenharmony_ci VC4_REG32(V3D_BPOA), 468c2ecf20Sopenharmony_ci VC4_REG32(V3D_BPOS), 478c2ecf20Sopenharmony_ci VC4_REG32(V3D_BXCF), 488c2ecf20Sopenharmony_ci VC4_REG32(V3D_SQRSV0), 498c2ecf20Sopenharmony_ci VC4_REG32(V3D_SQRSV1), 508c2ecf20Sopenharmony_ci VC4_REG32(V3D_SQCNTL), 518c2ecf20Sopenharmony_ci VC4_REG32(V3D_SRQPC), 528c2ecf20Sopenharmony_ci VC4_REG32(V3D_SRQUA), 538c2ecf20Sopenharmony_ci VC4_REG32(V3D_SRQUL), 548c2ecf20Sopenharmony_ci VC4_REG32(V3D_SRQCS), 558c2ecf20Sopenharmony_ci VC4_REG32(V3D_VPACNTL), 568c2ecf20Sopenharmony_ci VC4_REG32(V3D_VPMBASE), 578c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRC), 588c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRE), 598c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(0)), 608c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(0)), 618c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(1)), 628c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(1)), 638c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(2)), 648c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(2)), 658c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(3)), 668c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(3)), 678c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(4)), 688c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(4)), 698c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(5)), 708c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(5)), 718c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(6)), 728c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(6)), 738c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(7)), 748c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(7)), 758c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(8)), 768c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(8)), 778c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(9)), 788c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(9)), 798c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(10)), 808c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(10)), 818c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(11)), 828c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(11)), 838c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(12)), 848c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(12)), 858c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(13)), 868c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(13)), 878c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(14)), 888c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(14)), 898c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTR(15)), 908c2ecf20Sopenharmony_ci VC4_REG32(V3D_PCTRS(15)), 918c2ecf20Sopenharmony_ci VC4_REG32(V3D_DBGE), 928c2ecf20Sopenharmony_ci VC4_REG32(V3D_FDBGO), 938c2ecf20Sopenharmony_ci VC4_REG32(V3D_FDBGB), 948c2ecf20Sopenharmony_ci VC4_REG32(V3D_FDBGR), 958c2ecf20Sopenharmony_ci VC4_REG32(V3D_FDBGS), 968c2ecf20Sopenharmony_ci VC4_REG32(V3D_ERRSTAT), 978c2ecf20Sopenharmony_ci}; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct drm_info_node *node = (struct drm_info_node *)m->private; 1028c2ecf20Sopenharmony_ci struct drm_device *dev = node->minor->dev; 1038c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 1048c2ecf20Sopenharmony_ci int ret = vc4_v3d_pm_get(vc4); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (ret == 0) { 1078c2ecf20Sopenharmony_ci uint32_t ident1 = V3D_READ(V3D_IDENT1); 1088c2ecf20Sopenharmony_ci uint32_t nslc = VC4_GET_FIELD(ident1, V3D_IDENT1_NSLC); 1098c2ecf20Sopenharmony_ci uint32_t tups = VC4_GET_FIELD(ident1, V3D_IDENT1_TUPS); 1108c2ecf20Sopenharmony_ci uint32_t qups = VC4_GET_FIELD(ident1, V3D_IDENT1_QUPS); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci seq_printf(m, "Revision: %d\n", 1138c2ecf20Sopenharmony_ci VC4_GET_FIELD(ident1, V3D_IDENT1_REV)); 1148c2ecf20Sopenharmony_ci seq_printf(m, "Slices: %d\n", nslc); 1158c2ecf20Sopenharmony_ci seq_printf(m, "TMUs: %d\n", nslc * tups); 1168c2ecf20Sopenharmony_ci seq_printf(m, "QPUs: %d\n", nslc * qups); 1178c2ecf20Sopenharmony_ci seq_printf(m, "Semaphores: %d\n", 1188c2ecf20Sopenharmony_ci VC4_GET_FIELD(ident1, V3D_IDENT1_NSEM)); 1198c2ecf20Sopenharmony_ci vc4_v3d_pm_put(vc4); 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return 0; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/** 1268c2ecf20Sopenharmony_ci * Wraps pm_runtime_get_sync() in a refcount, so that we can reliably 1278c2ecf20Sopenharmony_ci * get the pm_runtime refcount to 0 in vc4_reset(). 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ciint 1308c2ecf20Sopenharmony_civc4_v3d_pm_get(struct vc4_dev *vc4) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci mutex_lock(&vc4->power_lock); 1338c2ecf20Sopenharmony_ci if (vc4->power_refcount++ == 0) { 1348c2ecf20Sopenharmony_ci int ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (ret < 0) { 1378c2ecf20Sopenharmony_ci vc4->power_refcount--; 1388c2ecf20Sopenharmony_ci mutex_unlock(&vc4->power_lock); 1398c2ecf20Sopenharmony_ci return ret; 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci mutex_unlock(&vc4->power_lock); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci return 0; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_civoid 1488c2ecf20Sopenharmony_civc4_v3d_pm_put(struct vc4_dev *vc4) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci mutex_lock(&vc4->power_lock); 1518c2ecf20Sopenharmony_ci if (--vc4->power_refcount == 0) { 1528c2ecf20Sopenharmony_ci pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev); 1538c2ecf20Sopenharmony_ci pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev); 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci mutex_unlock(&vc4->power_lock); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic void vc4_v3d_init_hw(struct drm_device *dev) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci /* Take all the memory that would have been reserved for user 1638c2ecf20Sopenharmony_ci * QPU programs, since we don't have an interface for running 1648c2ecf20Sopenharmony_ci * them, anyway. 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci V3D_WRITE(V3D_VPMBASE, 0); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciint vc4_v3d_get_bin_slot(struct vc4_dev *vc4) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci struct drm_device *dev = &vc4->base; 1728c2ecf20Sopenharmony_ci unsigned long irqflags; 1738c2ecf20Sopenharmony_ci int slot; 1748c2ecf20Sopenharmony_ci uint64_t seqno = 0; 1758c2ecf20Sopenharmony_ci struct vc4_exec_info *exec; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_citry_again: 1788c2ecf20Sopenharmony_ci spin_lock_irqsave(&vc4->job_lock, irqflags); 1798c2ecf20Sopenharmony_ci slot = ffs(~vc4->bin_alloc_used); 1808c2ecf20Sopenharmony_ci if (slot != 0) { 1818c2ecf20Sopenharmony_ci /* Switch from ffs() bit index to a 0-based index. */ 1828c2ecf20Sopenharmony_ci slot--; 1838c2ecf20Sopenharmony_ci vc4->bin_alloc_used |= BIT(slot); 1848c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&vc4->job_lock, irqflags); 1858c2ecf20Sopenharmony_ci return slot; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* Couldn't find an open slot. Wait for render to complete 1898c2ecf20Sopenharmony_ci * and try again. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci exec = vc4_last_render_job(vc4); 1928c2ecf20Sopenharmony_ci if (exec) 1938c2ecf20Sopenharmony_ci seqno = exec->seqno; 1948c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&vc4->job_lock, irqflags); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci if (seqno) { 1978c2ecf20Sopenharmony_ci int ret = vc4_wait_for_seqno(dev, seqno, ~0ull, true); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci if (ret == 0) 2008c2ecf20Sopenharmony_ci goto try_again; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return -ENOMEM; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci/** 2098c2ecf20Sopenharmony_ci * bin_bo_alloc() - allocates the memory that will be used for 2108c2ecf20Sopenharmony_ci * tile binning. 2118c2ecf20Sopenharmony_ci * 2128c2ecf20Sopenharmony_ci * The binner has a limitation that the addresses in the tile state 2138c2ecf20Sopenharmony_ci * buffer that point into the tile alloc buffer or binner overflow 2148c2ecf20Sopenharmony_ci * memory only have 28 bits (256MB), and the top 4 on the bus for 2158c2ecf20Sopenharmony_ci * tile alloc references end up coming from the tile state buffer's 2168c2ecf20Sopenharmony_ci * address. 2178c2ecf20Sopenharmony_ci * 2188c2ecf20Sopenharmony_ci * To work around this, we allocate a single large buffer while V3D is 2198c2ecf20Sopenharmony_ci * in use, make sure that it has the top 4 bits constant across its 2208c2ecf20Sopenharmony_ci * entire extent, and then put the tile state, tile alloc, and binner 2218c2ecf20Sopenharmony_ci * overflow memory inside that buffer. 2228c2ecf20Sopenharmony_ci * 2238c2ecf20Sopenharmony_ci * This creates a limitation where we may not be able to execute a job 2248c2ecf20Sopenharmony_ci * if it doesn't fit within the buffer that we allocated up front. 2258c2ecf20Sopenharmony_ci * However, it turns out that 16MB is "enough for anybody", and 2268c2ecf20Sopenharmony_ci * real-world applications run into allocation failures from the 2278c2ecf20Sopenharmony_ci * overall CMA pool before they make scenes complicated enough to run 2288c2ecf20Sopenharmony_ci * out of bin space. 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_cistatic int bin_bo_alloc(struct vc4_dev *vc4) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct vc4_v3d *v3d = vc4->v3d; 2338c2ecf20Sopenharmony_ci uint32_t size = 16 * 1024 * 1024; 2348c2ecf20Sopenharmony_ci int ret = 0; 2358c2ecf20Sopenharmony_ci struct list_head list; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci if (!v3d) 2388c2ecf20Sopenharmony_ci return -ENODEV; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci /* We may need to try allocating more than once to get a BO 2418c2ecf20Sopenharmony_ci * that doesn't cross 256MB. Track the ones we've allocated 2428c2ecf20Sopenharmony_ci * that failed so far, so that we can free them when we've got 2438c2ecf20Sopenharmony_ci * one that succeeded (if we freed them right away, our next 2448c2ecf20Sopenharmony_ci * allocation would probably be the same chunk of memory). 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&list); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci while (true) { 2498c2ecf20Sopenharmony_ci struct vc4_bo *bo = vc4_bo_create(&vc4->base, size, true, 2508c2ecf20Sopenharmony_ci VC4_BO_TYPE_BIN); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci if (IS_ERR(bo)) { 2538c2ecf20Sopenharmony_ci ret = PTR_ERR(bo); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci dev_err(&v3d->pdev->dev, 2568c2ecf20Sopenharmony_ci "Failed to allocate memory for tile binning: " 2578c2ecf20Sopenharmony_ci "%d. You may need to enable CMA or give it " 2588c2ecf20Sopenharmony_ci "more memory.", 2598c2ecf20Sopenharmony_ci ret); 2608c2ecf20Sopenharmony_ci break; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* Check if this BO won't trigger the addressing bug. */ 2648c2ecf20Sopenharmony_ci if ((bo->base.paddr & 0xf0000000) == 2658c2ecf20Sopenharmony_ci ((bo->base.paddr + bo->base.base.size - 1) & 0xf0000000)) { 2668c2ecf20Sopenharmony_ci vc4->bin_bo = bo; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci /* Set up for allocating 512KB chunks of 2698c2ecf20Sopenharmony_ci * binner memory. The biggest allocation we 2708c2ecf20Sopenharmony_ci * need to do is for the initial tile alloc + 2718c2ecf20Sopenharmony_ci * tile state buffer. We can render to a 2728c2ecf20Sopenharmony_ci * maximum of ((2048*2048) / (32*32) = 4096 2738c2ecf20Sopenharmony_ci * tiles in a frame (until we do floating 2748c2ecf20Sopenharmony_ci * point rendering, at which point it would be 2758c2ecf20Sopenharmony_ci * 8192). Tile state is 48b/tile (rounded to 2768c2ecf20Sopenharmony_ci * a page), and tile alloc is 32b/tile 2778c2ecf20Sopenharmony_ci * (rounded to a page), plus a page of extra, 2788c2ecf20Sopenharmony_ci * for a total of 320kb for our worst-case. 2798c2ecf20Sopenharmony_ci * We choose 512kb so that it divides evenly 2808c2ecf20Sopenharmony_ci * into our 16MB, and the rest of the 512kb 2818c2ecf20Sopenharmony_ci * will be used as storage for the overflow 2828c2ecf20Sopenharmony_ci * from the initial 32b CL per bin. 2838c2ecf20Sopenharmony_ci */ 2848c2ecf20Sopenharmony_ci vc4->bin_alloc_size = 512 * 1024; 2858c2ecf20Sopenharmony_ci vc4->bin_alloc_used = 0; 2868c2ecf20Sopenharmony_ci vc4->bin_alloc_overflow = 0; 2878c2ecf20Sopenharmony_ci WARN_ON_ONCE(sizeof(vc4->bin_alloc_used) * 8 != 2888c2ecf20Sopenharmony_ci bo->base.base.size / vc4->bin_alloc_size); 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci kref_init(&vc4->bin_bo_kref); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* Enable the out-of-memory interrupt to set our 2938c2ecf20Sopenharmony_ci * newly-allocated binner BO, potentially from an 2948c2ecf20Sopenharmony_ci * already-pending-but-masked interrupt. 2958c2ecf20Sopenharmony_ci */ 2968c2ecf20Sopenharmony_ci V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci break; 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci /* Put it on the list to free later, and try again. */ 3028c2ecf20Sopenharmony_ci list_add(&bo->unref_head, &list); 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci /* Free all the BOs we allocated but didn't choose. */ 3068c2ecf20Sopenharmony_ci while (!list_empty(&list)) { 3078c2ecf20Sopenharmony_ci struct vc4_bo *bo = list_last_entry(&list, 3088c2ecf20Sopenharmony_ci struct vc4_bo, unref_head); 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci list_del(&bo->unref_head); 3118c2ecf20Sopenharmony_ci drm_gem_object_put(&bo->base.base); 3128c2ecf20Sopenharmony_ci } 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci return ret; 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ciint vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used) 3188c2ecf20Sopenharmony_ci{ 3198c2ecf20Sopenharmony_ci int ret = 0; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci mutex_lock(&vc4->bin_bo_lock); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci if (used && *used) 3248c2ecf20Sopenharmony_ci goto complete; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci if (vc4->bin_bo) 3278c2ecf20Sopenharmony_ci kref_get(&vc4->bin_bo_kref); 3288c2ecf20Sopenharmony_ci else 3298c2ecf20Sopenharmony_ci ret = bin_bo_alloc(vc4); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci if (ret == 0 && used) 3328c2ecf20Sopenharmony_ci *used = true; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_cicomplete: 3358c2ecf20Sopenharmony_ci mutex_unlock(&vc4->bin_bo_lock); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci return ret; 3388c2ecf20Sopenharmony_ci} 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_cistatic void bin_bo_release(struct kref *ref) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = container_of(ref, struct vc4_dev, bin_bo_kref); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(!vc4->bin_bo)) 3458c2ecf20Sopenharmony_ci return; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci drm_gem_object_put(&vc4->bin_bo->base.base); 3488c2ecf20Sopenharmony_ci vc4->bin_bo = NULL; 3498c2ecf20Sopenharmony_ci} 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_civoid vc4_v3d_bin_bo_put(struct vc4_dev *vc4) 3528c2ecf20Sopenharmony_ci{ 3538c2ecf20Sopenharmony_ci mutex_lock(&vc4->bin_bo_lock); 3548c2ecf20Sopenharmony_ci kref_put(&vc4->bin_bo_kref, bin_bo_release); 3558c2ecf20Sopenharmony_ci mutex_unlock(&vc4->bin_bo_lock); 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 3598c2ecf20Sopenharmony_cistatic int vc4_v3d_runtime_suspend(struct device *dev) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci struct vc4_v3d *v3d = dev_get_drvdata(dev); 3628c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = v3d->vc4; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci vc4_irq_uninstall(&vc4->base); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci clk_disable_unprepare(v3d->clk); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci return 0; 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_cistatic int vc4_v3d_runtime_resume(struct device *dev) 3728c2ecf20Sopenharmony_ci{ 3738c2ecf20Sopenharmony_ci struct vc4_v3d *v3d = dev_get_drvdata(dev); 3748c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = v3d->vc4; 3758c2ecf20Sopenharmony_ci int ret; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci ret = clk_prepare_enable(v3d->clk); 3788c2ecf20Sopenharmony_ci if (ret != 0) 3798c2ecf20Sopenharmony_ci return ret; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci vc4_v3d_init_hw(&vc4->base); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci /* We disabled the IRQ as part of vc4_irq_uninstall in suspend. */ 3848c2ecf20Sopenharmony_ci enable_irq(vc4->base.irq); 3858c2ecf20Sopenharmony_ci vc4_irq_postinstall(&vc4->base); 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci return 0; 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ci#endif 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic int vc4_v3d_bind(struct device *dev, struct device *master, void *data) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 3948c2ecf20Sopenharmony_ci struct drm_device *drm = dev_get_drvdata(master); 3958c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(drm); 3968c2ecf20Sopenharmony_ci struct vc4_v3d *v3d = NULL; 3978c2ecf20Sopenharmony_ci int ret; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci v3d = devm_kzalloc(&pdev->dev, sizeof(*v3d), GFP_KERNEL); 4008c2ecf20Sopenharmony_ci if (!v3d) 4018c2ecf20Sopenharmony_ci return -ENOMEM; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci dev_set_drvdata(dev, v3d); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci v3d->pdev = pdev; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci v3d->regs = vc4_ioremap_regs(pdev, 0); 4088c2ecf20Sopenharmony_ci if (IS_ERR(v3d->regs)) 4098c2ecf20Sopenharmony_ci return PTR_ERR(v3d->regs); 4108c2ecf20Sopenharmony_ci v3d->regset.base = v3d->regs; 4118c2ecf20Sopenharmony_ci v3d->regset.regs = v3d_regs; 4128c2ecf20Sopenharmony_ci v3d->regset.nregs = ARRAY_SIZE(v3d_regs); 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci vc4->v3d = v3d; 4158c2ecf20Sopenharmony_ci v3d->vc4 = vc4; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci v3d->clk = devm_clk_get(dev, NULL); 4188c2ecf20Sopenharmony_ci if (IS_ERR(v3d->clk)) { 4198c2ecf20Sopenharmony_ci int ret = PTR_ERR(v3d->clk); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci if (ret == -ENOENT) { 4228c2ecf20Sopenharmony_ci /* bcm2835 didn't have a clock reference in the DT. */ 4238c2ecf20Sopenharmony_ci ret = 0; 4248c2ecf20Sopenharmony_ci v3d->clk = NULL; 4258c2ecf20Sopenharmony_ci } else { 4268c2ecf20Sopenharmony_ci if (ret != -EPROBE_DEFER) 4278c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get V3D clock: %d\n", 4288c2ecf20Sopenharmony_ci ret); 4298c2ecf20Sopenharmony_ci return ret; 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if (V3D_READ(V3D_IDENT0) != V3D_EXPECTED_IDENT0) { 4348c2ecf20Sopenharmony_ci DRM_ERROR("V3D_IDENT0 read 0x%08x instead of 0x%08x\n", 4358c2ecf20Sopenharmony_ci V3D_READ(V3D_IDENT0), V3D_EXPECTED_IDENT0); 4368c2ecf20Sopenharmony_ci return -EINVAL; 4378c2ecf20Sopenharmony_ci } 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci ret = clk_prepare_enable(v3d->clk); 4408c2ecf20Sopenharmony_ci if (ret != 0) 4418c2ecf20Sopenharmony_ci return ret; 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci /* Reset the binner overflow address/size at setup, to be sure 4448c2ecf20Sopenharmony_ci * we don't reuse an old one. 4458c2ecf20Sopenharmony_ci */ 4468c2ecf20Sopenharmony_ci V3D_WRITE(V3D_BPOA, 0); 4478c2ecf20Sopenharmony_ci V3D_WRITE(V3D_BPOS, 0); 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci vc4_v3d_init_hw(drm); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci ret = drm_irq_install(drm, platform_get_irq(pdev, 0)); 4528c2ecf20Sopenharmony_ci if (ret) { 4538c2ecf20Sopenharmony_ci DRM_ERROR("Failed to install IRQ handler\n"); 4548c2ecf20Sopenharmony_ci return ret; 4558c2ecf20Sopenharmony_ci } 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci pm_runtime_set_active(dev); 4588c2ecf20Sopenharmony_ci pm_runtime_use_autosuspend(dev); 4598c2ecf20Sopenharmony_ci pm_runtime_set_autosuspend_delay(dev, 40); /* a little over 2 frames. */ 4608c2ecf20Sopenharmony_ci pm_runtime_enable(dev); 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci vc4_debugfs_add_file(drm, "v3d_ident", vc4_v3d_debugfs_ident, NULL); 4638c2ecf20Sopenharmony_ci vc4_debugfs_add_regset32(drm, "v3d_regs", &v3d->regset); 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci return 0; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_cistatic void vc4_v3d_unbind(struct device *dev, struct device *master, 4698c2ecf20Sopenharmony_ci void *data) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct drm_device *drm = dev_get_drvdata(master); 4728c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(drm); 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci pm_runtime_disable(dev); 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci drm_irq_uninstall(drm); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci /* Disable the binner's overflow memory address, so the next 4798c2ecf20Sopenharmony_ci * driver probe (if any) doesn't try to reuse our old 4808c2ecf20Sopenharmony_ci * allocation. 4818c2ecf20Sopenharmony_ci */ 4828c2ecf20Sopenharmony_ci V3D_WRITE(V3D_BPOA, 0); 4838c2ecf20Sopenharmony_ci V3D_WRITE(V3D_BPOS, 0); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci vc4->v3d = NULL; 4868c2ecf20Sopenharmony_ci} 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_cistatic const struct dev_pm_ops vc4_v3d_pm_ops = { 4898c2ecf20Sopenharmony_ci SET_RUNTIME_PM_OPS(vc4_v3d_runtime_suspend, vc4_v3d_runtime_resume, NULL) 4908c2ecf20Sopenharmony_ci}; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic const struct component_ops vc4_v3d_ops = { 4938c2ecf20Sopenharmony_ci .bind = vc4_v3d_bind, 4948c2ecf20Sopenharmony_ci .unbind = vc4_v3d_unbind, 4958c2ecf20Sopenharmony_ci}; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic int vc4_v3d_dev_probe(struct platform_device *pdev) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci return component_add(&pdev->dev, &vc4_v3d_ops); 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic int vc4_v3d_dev_remove(struct platform_device *pdev) 5038c2ecf20Sopenharmony_ci{ 5048c2ecf20Sopenharmony_ci component_del(&pdev->dev, &vc4_v3d_ops); 5058c2ecf20Sopenharmony_ci return 0; 5068c2ecf20Sopenharmony_ci} 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ciconst struct of_device_id vc4_v3d_dt_match[] = { 5098c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2835-v3d" }, 5108c2ecf20Sopenharmony_ci { .compatible = "brcm,cygnus-v3d" }, 5118c2ecf20Sopenharmony_ci { .compatible = "brcm,vc4-v3d" }, 5128c2ecf20Sopenharmony_ci {} 5138c2ecf20Sopenharmony_ci}; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_cistruct platform_driver vc4_v3d_driver = { 5168c2ecf20Sopenharmony_ci .probe = vc4_v3d_dev_probe, 5178c2ecf20Sopenharmony_ci .remove = vc4_v3d_dev_remove, 5188c2ecf20Sopenharmony_ci .driver = { 5198c2ecf20Sopenharmony_ci .name = "vc4_v3d", 5208c2ecf20Sopenharmony_ci .of_match_table = vc4_v3d_dt_match, 5218c2ecf20Sopenharmony_ci .pm = &vc4_v3d_pm_ops, 5228c2ecf20Sopenharmony_ci }, 5238c2ecf20Sopenharmony_ci}; 524