From patchwork Wed Jul 31 15:47:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 11068553 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DA896912 for ; Wed, 31 Jul 2019 15:51:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C62061FF28 for ; Wed, 31 Jul 2019 15:51:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B97EC204FB; Wed, 31 Jul 2019 15:51:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5ED912623D for ; Wed, 31 Jul 2019 15:51:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=j1X4/joCc6KNkDcOi0VtLru0+xlcTLgzKApYDnlp3mI=; b=JB1Yu6gfVmWBXB efElAxJHsvGaqKr1WFTzzoe659F+1JgpZ9nkCbAAx4Ydd7gV4z4VSTC6xXML3p+GRX/2P/M/6SngP bj8LYkbQmBT+cH+HyJqERc4cBPfgQ6AaTFE+FhvYMCzSoeg2QczrXp4d9SxMUi/I87lw+zfvDvEt5 1Pocz4nFlfxfeZyxWipXfCDJTSZqdJFgB2ulULcDwZ5HqMToVIZuklb/0ka2da1rBt9Opq2IXBxFP VDRGUJpbKeuacnftNaN9wdTdWwdBI2tyyeqyCl8QEiZmzvEtTSdKFVFiZWaCHna0vYI88ka71rTz+ aW5ozZe8GxHPYKfZZ7UQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hsqt4-0004nn-Km; Wed, 31 Jul 2019 15:51:34 +0000 Received: from mx2.suse.de ([195.135.220.15] helo=mx1.suse.de) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hsqph-0007ai-Fd; Wed, 31 Jul 2019 15:48:07 +0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 01F74AFE4; Wed, 31 Jul 2019 15:48:04 +0000 (UTC) From: Nicolas Saenz Julienne To: catalin.marinas@arm.com, hch@lst.de, wahrenst@gmx.net, marc.zyngier@arm.com, Robin Murphy , linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, linux-mm@kvack.org, Rob Herring , Frank Rowand Subject: [PATCH 3/8] of/fdt: add function to get the SoC wide DMA addressable memory size Date: Wed, 31 Jul 2019 17:47:46 +0200 Message-Id: <20190731154752.16557-4-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190731154752.16557-1-nsaenzjulienne@suse.de> References: <20190731154752.16557-1-nsaenzjulienne@suse.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190731_084805_850422_BBB39BEB X-CRM114-Status: GOOD ( 11.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: phill@raspberryi.org, f.fainelli@gmail.com, linux-kernel@vger.kernel.org, eric@anholt.net, mbrugger@suse.com, linux-rpi-kernel@lists.infradead.org, akpm@linux-foundation.org, will@kernel.org, nsaenzjulienne@suse.de, m.szyprowski@samsung.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Some SoCs might have multiple interconnects each with their own DMA addressing limitations. This function parses the 'dma-ranges' on each of them and tries to guess the maximum SoC wide DMA addressable memory size. This is specially useful for arch code in order to properly setup CMA and memory zones. Signed-off-by: Nicolas Saenz Julienne --- drivers/of/fdt.c | 72 ++++++++++++++++++++++++++++++++++++++++++ include/linux/of_fdt.h | 2 ++ 2 files changed, 74 insertions(+) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 9cdf14b9aaab..f2444c61a136 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -953,6 +953,78 @@ int __init early_init_dt_scan_chosen_stdout(void) } #endif +/** + * early_init_dt_dma_zone_size - Look at all 'dma-ranges' and provide the + * maximum common dmable memory size. + * + * Some devices might have multiple interconnects each with their own DMA + * addressing limitations. For example the Raspberry Pi 4 has the following: + * + * soc { + * dma-ranges = <0xc0000000 0x0 0x00000000 0x3c000000>; + * [...] + * } + * + * v3dbus { + * dma-ranges = <0x00000000 0x0 0x00000000 0x3c000000>; + * [...] + * } + * + * scb { + * dma-ranges = <0x0 0x00000000 0x0 0x00000000 0xfc000000>; + * [...] + * } + * + * Here the area addressable by all devices is [0x00000000-0x3bffffff]. Hence + * the function will write in 'data' a size of 0x3c000000. + * + * Note that the implementation assumes all interconnects have the same physical + * memory view and that the mapping always start at the beginning of RAM. + */ +int __init early_init_dt_dma_zone_size(unsigned long node, const char *uname, + int depth, void *data) +{ + const char *type = of_get_flat_dt_prop(node, "device_type", NULL); + u64 phys_addr, dma_addr, size; + u64 *dma_zone_size = data; + int dma_addr_cells; + const __be32 *reg; + const void *prop; + int len; + + if (depth == 0) + *dma_zone_size = 0; + + /* + * We avoid pci host controllers as they have their own way of using + * 'dma-ranges'. + */ + if (type && !strcmp(type, "pci")) + return 0; + + reg = of_get_flat_dt_prop(node, "dma-ranges", &len); + if (!reg) + return 0; + + prop = of_get_flat_dt_prop(node, "#address-cells", NULL); + if (prop) + dma_addr_cells = be32_to_cpup(prop); + else + dma_addr_cells = 1; /* arm64's default addr_cell size */ + + if (len < (dma_addr_cells + dt_root_addr_cells + dt_root_size_cells)) + return 0; + + dma_addr = dt_mem_next_cell(dma_addr_cells, ®); + phys_addr = dt_mem_next_cell(dt_root_addr_cells, ®); + size = dt_mem_next_cell(dt_root_size_cells, ®); + + if (!*dma_zone_size || *dma_zone_size > size) + *dma_zone_size = size; + + return 0; +} + /** * early_init_dt_scan_root - fetch the top level address and size cells */ diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h index acf820e88952..2ad36b7bd4fa 100644 --- a/include/linux/of_fdt.h +++ b/include/linux/of_fdt.h @@ -72,6 +72,8 @@ extern int early_init_dt_reserve_memory_arch(phys_addr_t base, phys_addr_t size, bool no_map); extern u64 dt_mem_next_cell(int s, const __be32 **cellp); +extern int early_init_dt_dma_zone_size(unsigned long node, const char *uname, + int depth, void *data); /* Early flat tree scan hooks */ extern int early_init_dt_scan_root(unsigned long node, const char *uname, int depth, void *data);