From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795445635; b=hMOen4OLPzSEd2vu6JLyeJvDVRPvYH6XNzabzV96sm5G40MbXeq9iQ0JMWxHhVAtRh0I BMzM60dE45L7GQbBPAFYzgJDwFVVmTFAEXFi4yX8ZlSz1YIwy+hBOI8KE0ookLnrw03ON YIacy4LGM1V5nH92z6RQPGABJ0NU6KhPP7/qTee+6Gm/Ms24newD+08LZsM/4X4n3Ppen Yx9dFEbFeHmFiPV4MUv6fFGygLA9Qr8DIfRjq1jdUegJPamUAHiEFdK6MuXzLvMGGtuij ecVRiyyT7FPjN+vqEv0nTaQQjRS32+jSjvgOGwkLqW9Tk9o5UUjynfbF4za/17zm5fA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795445635; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Fq6yFeIpFYUq9GidwudNaGDfTK+jNPkZlihcuSaBGS4=; b=oG2LesaBeT0+Yi0ihWDx7bUEcyAeGsXwh4CXySjATQgybGhihkyHdwrGDfod7CnA7w1s ByfFaO1i/aBzDkrxJnTnFReX/izu80b8zoOVAZ0dLqRgWkaFPCd2unKFEK2vqCK2tJ9Gj yrAJRRQqd6250tqcY0Wb1SmYEdeFosY6twWAzZeRrJSL0bAuQ5c/irNbMTNc6/yCOy9LA I1PHpgxL7VJWzsv2Nbuq59C0GK0d0NhG9O9qLF3s9cuKW28O3fhO5zKt28HT8SgBbVXbq /fLZiSpSI++AGaZjSFxuhMNR5i9m3kIUsZVazKr9s92M8aOAiKYLvv2Butg2hAahzBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=YA2T3ppX; arc=none smtp.remote-ip=209.85.128.50 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A016060A383D for ; Mon, 23 Nov 2026 14:53:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A016060A383D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=YA2T3ppX Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4a35ac4475dso25197885e9.2 for ; Mon, 23 Nov 2026 06:53:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1795445633; x=1796050433; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Fq6yFeIpFYUq9GidwudNaGDfTK+jNPkZlihcuSaBGS4=; b=YA2T3ppXFNUv5MeozaFJxF0hWIaY5sY/O/Cr5/HXQHSalX+x1VVq0hijpFPhRawDR2 7UXBlTYZtCU/AdLXl595/7qrnZrVJRORdwtYMrEqONC4kszO873DywoWVzzcjo5P7oq2 B0NKjvppyszO2YKqPtTSXk2shRHXa2c3JVm8ZcDD/yrO16fR0n5BQqpJNqyaNunGXL8g WuAJgECR4uEqAYDGhbU7YA6lrDEok2/Px+pEIQmgvH8ytFsY4GguHZ9v/WPX22c9TJTd oVRBwbo2KEkje5Dl4X+X3XQvj4A95M6isfzCMJc94cQmmKe3ZkCmHnCfy5AhaI2oJ/lb dVOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795445633; x=1796050433; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Fq6yFeIpFYUq9GidwudNaGDfTK+jNPkZlihcuSaBGS4=; b=HvG+4NNAZvW1LslZ0/oOE1zFNTAMbcnhmLfTCf7kbE8BMDvNEiDADXqtWmnA9cgrMM ZMICKKmgBJnZxF8G71jpcz7W3BrwHUiDzhGGGhHBOBvn95FVQ35zLdZEaGLbjbfrEF82 j6GBfBPKXy0zp4k9Ca9OiHBtUGJJbfXdC1Il23iG7xW8RlXh1rJKAGo0RCBwMXnC4fWz OWBuc3bt1g1FzZpBF5HGIAqyqkz44eUIZNNLt2iBtW/NQAIWDvNtAD9/EO8YSIe0U5ul WOotCq+dqqCzK4bjuHykOq+ghmVZ3PS9AStI2ZwLD28E3VHjUQG8PxZqpIeKFDxjnKxn q/NA== X-Forwarded-Encrypted: i=1; AL+hErFeQzRLH7wB7SPUgjsNPAWACZG7LAhmhSLPWTUBhpNYnxI+dFowXUqsdpi3Rub8SGcywvY/V3TxKgBlAjc=@vger.kernel.org X-Gm-Message-State: APuU4Z/+esPzYjEPrNyodzGABmSvoACmM+wUfOPFoX7UI3AfJR9A/Ktn 4IAGDw0IhTj0iM+tDq3ucOx+/Uwrfej+YQzRo6gZHx91QUEW9YEAg9K1W5ADKN+kq/A= X-Gm-Gg: AZFdTZeEstYi+mRTs3cHiipMxRb81AgGFaTk26HPMPsx5SRWtcqO0Q8yfNCvuqibvaX ZKKg+9wp+HBJcgjEkFNsiHEYymBodWIPML9rNiJV+MxRxtr0Hx1iyn8yNBBUqECZgtqnpEBSUxk yJ+5AYcc7g+KpkhivzC1pPDVWzTtwUi0g2tFjQEDHZsDn5oqIwWNNOYeFlZtyz8N8jjsACA86Vb GiQQGoGA6o/H7OqYGOTi/0KSTH/hDR8iJ2pCZUpcniFdx0KJKprzfZ1oZiAFmHUx2Y2VXyMaHgN ix+HZftO+7hy1svIs0x8yBtA/9Ga1oFVVo5Ci50M9GjXuQl3LSgPk1T7f65aScakMmG+zbU+wLb 7zGirV8UlucLS5oDgPZsq4vVykkH42qykOA3FkxPI6G2KQOXOFPN0oIljBxGT7dA4dfWZ1pz5Ws KnaMwhuOYmw8e8aSdlX4Ef778I0LCh9rz4OVMjvfGZ0IYUIGLyVR4wewLr3V7I0vmn3HCzqWQ7H +W/M2VA7rXyviL9mEAYqjJTEt0gTBpmzxfFyPW+mkQUrMVvquEYcGvg3zmDGa7lSZ9IsBknJXGN nl0BGeCoY5VnF6Q8zadnYyy+F0Y= X-Received: by 2002:a05:600c:a11:b0:4a2:e0c:2b0a with SMTP id 5b1f17b1804b1-4bb888120famr244703865e9.16.1795445632908; Mon, 23 Nov 2026 06:53:52 -0800 (PST) Received: from localhost ([82.192.120.76]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853368d3sm362775075e9.1.2026.11.23.06.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2026 06:53:52 -0800 (PST) From: Andrea della Porta To: Vinod Koul Cc: Frank Li , dmaengine@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, Florian Fainelli , Ray Jui , Scott Branden , Broadcom internal kernel review list , Saenz Julienne , Stefan Wahren , Dave Stevenson , Phil Elwell , Dom Cobley , Maxime Ripard , Robin Murphy , linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrea della Porta Subject: [PATCH v3 14/28] dmaengine: bcm2835: Use dma_map_resource() to translate slave addresses Date: Mon, 23 Nov 2026 15:56:52 +0100 Message-ID: <20261123145728.29082-15-andrea.porta@suse.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20261123145728.29082-1-andrea.porta@suse.com> References: <20261123145728.29082-1-andrea.porta@suse.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Slave addresses in struct dma_slave_config are CPU physical addresses and it is up to the DMA controller driver to translate them into the addresses it has to program into the hardware. The BCM2835 DMA driver instead uses them verbatim, which only works because its clients pass pre-translated bus addresses read straight from their DT node, bypassing the bus translations described there. Translate the addresses with dma_map_resource(), which applies the dma-ranges of the DMA controller since the conversion of dma_map_resource() to dma_map_phys() with DMA_ATTR_MMIO, and map them once in device_config() instead of at every prep call. The mappings are released when the channel is reconfigured or freed. Existing device trees only describe the RAM in dma-ranges, so dma_map_resource() would fail for any peripheral. Detect this case at probe time, by looking for the controller registers themselves in the dma-ranges of the closest ancestor bus, and fall back to reverting the translation given by the ranges property of the parent bus, which is how the VideoCore bus addresses of the peripherals have been derived so far. Addresses not covered by those ranges are passed through, so that clients still providing bus addresses keep working. The fallback translation is a platform callback, as different kinds of channels see the peripherals at different addresses; implement the one of the legacy channels. Link: https://lore.kernel.org/linux-arm-kernel/ee19a95d-fe1e-4f3f-bc81-bdef38475469@arm.com/ Link: https://lore.kernel.org/all/CAPY8ntByJYzSv0kTAc1kY0Dp=vwrzcA0oWiPpyg7x7_BQwGSnA@mail.gmail.com/ Signed-off-by: Andrea della Porta --- drivers/dma/bcm2835-dma.c | 180 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 5 deletions(-) diff --git a/drivers/dma/bcm2835-dma.c b/drivers/dma/bcm2835-dma.c index f8b2202096d6..3ff30c41dc63 100644 --- a/drivers/dma/bcm2835-dma.c +++ b/drivers/dma/bcm2835-dma.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "virt-dma.h" @@ -43,12 +44,16 @@ * @base: base address of register map * @zero_page: bus address of zero page (to detect transactions copying from * zero page and avoid accessing memory if so) + * @cfg: platform specific description of the DMA channels + * @map_resource: the peripheral window is described by dma-ranges, so slave + * addresses can be translated with dma_map_resource() */ struct bcm2835_dmadev { struct dma_device ddev; void __iomem *base; dma_addr_t zero_page; const struct bcm2835_dma_cfg *cfg; + bool map_resource; }; struct bcm_dma_cb { @@ -70,10 +75,24 @@ struct bcm2835_cb_entry { dma_addr_t paddr; }; +/** + * struct bcm2835_slave_addr - bus address of a slave FIFO + * @addr: address as seen by the DMA controller + * @size: size of the mapping + * @mapped: @addr has been obtained from dma_map_resource() + */ +struct bcm2835_slave_addr { + dma_addr_t addr; + size_t size; + bool mapped; +}; + struct bcm2835_chan { struct virt_dma_chan vc; struct dma_slave_config cfg; + struct bcm2835_slave_addr slave_src; + struct bcm2835_slave_addr slave_dst; unsigned int dreq; int ch; @@ -106,6 +125,8 @@ struct bcm2835_chan { * @read_addr: read the current memory side address of a channel * @cs_flags: per-channel flags for the CS register * @abort: stop and reset a channel + * @legacy_bus_addr: translate a slave address for device trees which do not + * describe the peripheral window in dma-ranges */ struct bcm2835_dma_cfg { u32 cs_reg; @@ -138,6 +159,8 @@ struct bcm2835_dma_cfg { enum dma_transfer_direction direction); u32 (*cs_flags)(struct bcm2835_chan *c); void (*abort)(struct bcm2835_chan *c); + + u64 (*legacy_bus_addr)(struct device *dev, phys_addr_t phys); }; struct bcm2835_desc { @@ -798,11 +821,30 @@ static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan) c->irq_flags, "DMA IRQ", chan); } +static void bcm2835_dma_unmap_slave_addr(struct dma_chan *chan, + struct bcm2835_slave_addr *sa, + enum dma_data_direction dir) +{ + if (sa->mapped) + dma_unmap_resource(chan->device->dev, sa->addr, sa->size, dir, 0); + + memset(sa, 0, sizeof(*sa)); +} + +static void bcm2835_dma_unmap_slave_addrs(struct dma_chan *chan) +{ + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); + + bcm2835_dma_unmap_slave_addr(chan, &c->slave_src, DMA_FROM_DEVICE); + bcm2835_dma_unmap_slave_addr(chan, &c->slave_dst, DMA_TO_DEVICE); +} + static void bcm2835_dma_free_chan_resources(struct dma_chan *chan) { struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); vchan_free_chan_resources(&c->vc); + bcm2835_dma_unmap_slave_addrs(chan); free_irq(c->irq_number, chan); dma_pool_destroy(c->cb_pool); @@ -925,11 +967,11 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg( if (direction == DMA_DEV_TO_MEM) { if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) return NULL; - src = c->cfg.src_addr; + src = c->slave_src.addr; } else { if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) return NULL; - dst = c->cfg.dst_addr; + dst = c->slave_dst.addr; } /* count frames in sg list */ @@ -987,12 +1029,12 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic( if (direction == DMA_DEV_TO_MEM) { if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) return NULL; - src = c->cfg.src_addr; + src = c->slave_src.addr; dst = buf_addr; } else { if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) return NULL; - dst = c->cfg.dst_addr; + dst = c->slave_dst.addr; src = buf_addr; } @@ -1019,10 +1061,87 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic( return vchan_tx_prep(&c->vc, &d->vd, flags); } +/* + * Device trees which do not describe the peripheral window in dma-ranges + * are handled by assuming that the legacy DMA channels see the peripherals + * at the same address as the bus they sit on, i.e. by reverting the + * translation described by the ranges property of the parent bus. This is + * how the VideoCore bus addresses have always been derived. Addresses which + * are not CPU physical addresses of that bus are passed through unchanged, + * as legacy clients used to provide bus addresses. + */ +static u64 bcm2835_dma_legacy_bus_addr(struct device *dev, phys_addr_t phys) +{ + struct device_node *bus = of_get_parent(dev->of_node); + struct of_range_parser parser; + struct of_range range; + u64 addr = phys; + + if (bus && !of_range_parser_init(&parser, bus)) { + for_each_of_range(&parser, &range) { + if (phys >= range.cpu_addr && + phys - range.cpu_addr < range.size) { + addr = range.bus_addr + (phys - range.cpu_addr); + break; + } + } + } + + of_node_put(bus); + + return addr; +} + +static int bcm2835_dma_map_slave_addr(struct dma_chan *chan, + struct bcm2835_slave_addr *sa, + phys_addr_t phys, + enum dma_slave_buswidth width, + enum dma_data_direction dir) +{ + struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device); + struct device *dev = chan->device->dev; + size_t size = width ? width : DMA_SLAVE_BUSWIDTH_4_BYTES; + + if (!phys) + return 0; + + if (!od->map_resource) { + sa->addr = od->cfg->legacy_bus_addr(dev, phys); + return 0; + } + + sa->addr = dma_map_resource(dev, phys, size, dir, 0); + if (dma_mapping_error(dev, sa->addr)) { + dev_err(dev, "failed to map slave address %pa\n", &phys); + sa->addr = 0; + return -EINVAL; + } + + sa->size = size; + sa->mapped = true; + + return 0; +} + static int bcm2835_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg) { struct bcm2835_chan *c = to_bcm2835_dma_chan(chan); + int ret; + + bcm2835_dma_unmap_slave_addrs(chan); + + ret = bcm2835_dma_map_slave_addr(chan, &c->slave_src, cfg->src_addr, + cfg->src_addr_width, DMA_FROM_DEVICE); + if (ret) + return ret; + + ret = bcm2835_dma_map_slave_addr(chan, &c->slave_dst, cfg->dst_addr, + cfg->dst_addr_width, DMA_TO_DEVICE); + if (ret) { + bcm2835_dma_unmap_slave_addrs(chan); + return ret; + } c->cfg = *cfg; @@ -1116,6 +1235,8 @@ static const struct bcm2835_dma_cfg bcm2835_data = { .read_addr = bcm2835_dma_read_addr, .cs_flags = bcm2835_dma_cs_flags, .abort = bcm2835_dma_abort_chan, + + .legacy_bus_addr = bcm2835_dma_legacy_bus_addr, }; static const struct of_device_id bcm2835_dma_of_match[] = { @@ -1157,6 +1278,48 @@ static int bcm2835_dma_suspend_late(struct device *dev) return 0; } +/* + * The DMA controller registers sit in the same peripheral window as the + * FIFOs of its clients, so the window is described by dma-ranges if and + * only if the controller registers are. + */ +static bool bcm2835_dma_mmio_in_dma_ranges(struct device *dev, + phys_addr_t mmio) +{ + struct of_range_parser parser; + struct of_range range; + struct device_node *np; + bool found = false; + + for (np = of_get_parent(dev->of_node); np; np = of_get_next_parent(np)) + if (of_property_present(np, "dma-ranges")) + break; + + /* + * Without any dma-ranges the translation is unknown: the peripherals + * are not at their CPU physical address on any of these SoCs. + */ + if (!np) + return false; + + if (!of_pci_dma_range_parser_init(&parser, np)) { + /* An empty dma-ranges describes a 1:1 translation too */ + found = !of_range_count(&parser); + + for_each_of_range(&parser, &range) { + if (mmio >= range.cpu_addr && + mmio - range.cpu_addr < range.size) { + found = true; + break; + } + } + } + + of_node_put(np); + + return found; +} + static const struct dev_pm_ops bcm2835_dma_pm_ops = { LATE_SYSTEM_SLEEP_PM_OPS(bcm2835_dma_suspend_late, NULL) }; @@ -1164,6 +1327,7 @@ static const struct dev_pm_ops bcm2835_dma_pm_ops = { static int bcm2835_dma_probe(struct platform_device *pdev) { struct bcm2835_dmadev *od; + struct resource *res; void __iomem *base; int rc; int i, j; @@ -1192,12 +1356,18 @@ static int bcm2835_dma_probe(struct platform_device *pdev) dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF); - base = devm_platform_ioremap_resource(pdev, 0); + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); if (IS_ERR(base)) return PTR_ERR(base); od->base = base; + od->map_resource = bcm2835_dma_mmio_in_dma_ranges(&pdev->dev, + res->start); + if (!od->map_resource) + dev_info(&pdev->dev, + "peripheral window missing in dma-ranges, please update DT blob\n"); + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask); dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask); dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask); -- 2.51.0