Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1709350pxb; Mon, 8 Mar 2021 04:36:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJypAjR1OH2TkjlH3RK4vjMj3O1GQ2d9dkah7i85VmpG+S3V+6uMayG6IL7VZjWno4tt1ifp X-Received: by 2002:a17:906:4c56:: with SMTP id d22mr15370274ejw.426.1615206998052; Mon, 08 Mar 2021 04:36:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615206998; cv=none; d=google.com; s=arc-20160816; b=gQiPu1r6YR2Y+5pt8Tr6oMpKn2qNpYtirFDIy2D4HeQXsCsHCWCi0odQRPgaGzBn4t gr3APvOP7BN7FWV8pgIstyqqzjJYKq8/Z48fxXpW/hO8Lv0MqPYOcHcCbC7QMrbOF7RZ vV0XQWB+v3KbtGe2x99HLdrvAum4ab9tr3Eu08oSirr+2I2kwRxy37zUZAMG8QRxl5Q8 EBy7stqyD1OmYdbQWqyMuc+JQ6oE8ZtDZe72Px61DkUXx2+oxCEjmQisB6xw3S51Q6IM L1M7KpL+QfAwv+La9dIEj2uIkNUaz/sETvcTYjghqdlHSenQSoYYjyYyl2xC5EMAwJf7 ms8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=toclmHxgfLDJOOHjT8bRc+MCDJUJFEwMjU9nZ2UR2Ws=; b=NU1+Wz+w880Piu9j5jq49+GrTYNJ3LZBbJhlgAYpWGutL9u6RSsLBpzKrCIgEHm1rN 8lR9FHuZSIhLt1bRhqiKOrHL0v6uF9S8DXO7T6m98zP42sE/7jfd4EMZ3jIucdNhdktY 9vxTNJt10YA7gpvEzpjMa3RcH8+F+ESKIV66IFyBxa240/Qz3DsoL5ve4P7wtqNkivq4 VeA82jJoVAkc8yw0W/e9L2O09GsjrgSCNbqPkqcFnkeSzDUVeSWxsmux47+DKLHg8nzv vgWPaKFWkempUA9XqcMCndwFv6OSllVGP68MqNo/UrE1uKbTSr8kJ/8UvhiuLpWGhOTi NsNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GBGtE3kD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z5si7037727edp.552.2021.03.08.04.36.15; Mon, 08 Mar 2021 04:36:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GBGtE3kD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231395AbhCHMev (ORCPT + 99 others); Mon, 8 Mar 2021 07:34:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:42926 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229674AbhCHMeO (ORCPT ); Mon, 8 Mar 2021 07:34:14 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5C545651C3; Mon, 8 Mar 2021 12:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206853; bh=ZJ2P9Sn9EZ2ds0a2af3EMkmaDD1/NP4KPE/I4wlxXfg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GBGtE3kDXJP1c9pyvC6NMcaxY+L9TEULppeCfjmwm4ldJlxv3dZPENtO2wGoWZn8/ l4MJU2nMFWnk3C3JrQ5teHZhVoLll/j9/jJsmN+RLVqTGvG3ZgtC5ovtqoC7SMUZA7 RIx0nsU++yzNU0Ttc/YqSZO+pa5nKKx8UcQw8UOQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ard Biesheuvel , Nicolas Saenz Julienne , Jeremy Linton , Lorenzo Pieralisi , Hanjun Guo , Rob Herring , Christoph Hellwig , Robin Murphy , Sudeep Holla , Anshuman Khandual , Catalin Marinas , Jing Xiangfeng Subject: [PATCH 5.10 29/42] arm64: mm: Set ZONE_DMA size based on early IORT scan Date: Mon, 8 Mar 2021 13:30:55 +0100 Message-Id: <20210308122719.557568557@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122718.120213856@linuxfoundation.org> References: <20210308122718.120213856@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ard Biesheuvel commit 2b8652936f0ca9ca2e6c984ae76c7bfcda1b3f22 upstream We recently introduced a 1 GB sized ZONE_DMA to cater for platforms incorporating masters that can address less than 32 bits of DMA, in particular the Raspberry Pi 4, which has 4 or 8 GB of DRAM, but has peripherals that can only address up to 1 GB (and its PCIe host bridge can only access the bottom 3 GB) Instructing the DMA layer about these limitations is straight-forward, even though we had to fix some issues regarding memory limits set in the IORT for named components, and regarding the handling of ACPI _DMA methods. However, the DMA layer also needs to be able to allocate memory that is guaranteed to meet those DMA constraints, for bounce buffering as well as allocating the backing for consistent mappings. This is why the 1 GB ZONE_DMA was introduced recently. Unfortunately, it turns out the having a 1 GB ZONE_DMA as well as a ZONE_DMA32 causes problems with kdump, and potentially in other places where allocations cannot cross zone boundaries. Therefore, we should avoid having two separate DMA zones when possible. So let's do an early scan of the IORT, and only create the ZONE_DMA if we encounter any devices that need it. This puts the burden on the firmware to describe such limitations in the IORT, which may be redundant (and less precise) if _DMA methods are also being provided. However, it should be noted that this situation is highly unusual for arm64 ACPI machines. Also, the DMA subsystem still gives precedence to the _DMA method if implemented, and so we will not lose the ability to perform streaming DMA outside the ZONE_DMA if the _DMA method permits it. [nsaenz: unified implementation with DT's counterpart] Signed-off-by: Ard Biesheuvel Signed-off-by: Nicolas Saenz Julienne Tested-by: Jeremy Linton Acked-by: Lorenzo Pieralisi Acked-by: Hanjun Guo Cc: Jeremy Linton Cc: Lorenzo Pieralisi Cc: Nicolas Saenz Julienne Cc: Rob Herring Cc: Christoph Hellwig Cc: Robin Murphy Cc: Hanjun Guo Cc: Sudeep Holla Cc: Anshuman Khandual Link: https://lore.kernel.org/r/20201119175400.9995-7-nsaenzjulienne@suse.de Signed-off-by: Catalin Marinas Cc: Signed-off-by: Jing Xiangfeng Signed-off-by: Greg Kroah-Hartman --- arch/arm64/mm/init.c | 5 +++- drivers/acpi/arm64/iort.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/acpi_iort.h | 4 +++ 3 files changed, 63 insertions(+), 1 deletion(-) --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -186,11 +187,13 @@ static phys_addr_t __init max_zone_phys( static void __init zone_sizes_init(unsigned long min, unsigned long max) { unsigned long max_zone_pfns[MAX_NR_ZONES] = {0}; + unsigned int __maybe_unused acpi_zone_dma_bits; unsigned int __maybe_unused dt_zone_dma_bits; #ifdef CONFIG_ZONE_DMA + acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address()); dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL)); - zone_dma_bits = min(32U, dt_zone_dma_bits); + zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits); arm64_dma_phys_limit = max_zone_phys(zone_dma_bits); max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit); #endif --- a/drivers/acpi/arm64/iort.c +++ b/drivers/acpi/arm64/iort.c @@ -1730,3 +1730,58 @@ void __init acpi_iort_init(void) iort_init_platform_devices(); } + +#ifdef CONFIG_ZONE_DMA +/* + * Extract the highest CPU physical address accessible to all DMA masters in + * the system. PHYS_ADDR_MAX is returned when no constrained device is found. + */ +phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void) +{ + phys_addr_t limit = PHYS_ADDR_MAX; + struct acpi_iort_node *node, *end; + struct acpi_table_iort *iort; + acpi_status status; + int i; + + if (acpi_disabled) + return limit; + + status = acpi_get_table(ACPI_SIG_IORT, 0, + (struct acpi_table_header **)&iort); + if (ACPI_FAILURE(status)) + return limit; + + node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset); + end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length); + + for (i = 0; i < iort->node_count; i++) { + if (node >= end) + break; + + switch (node->type) { + struct acpi_iort_named_component *ncomp; + struct acpi_iort_root_complex *rc; + phys_addr_t local_limit; + + case ACPI_IORT_NODE_NAMED_COMPONENT: + ncomp = (struct acpi_iort_named_component *)node->node_data; + local_limit = DMA_BIT_MASK(ncomp->memory_address_limit); + limit = min_not_zero(limit, local_limit); + break; + + case ACPI_IORT_NODE_PCI_ROOT_COMPLEX: + if (node->revision < 1) + break; + + rc = (struct acpi_iort_root_complex *)node->node_data; + local_limit = DMA_BIT_MASK(rc->memory_address_limit); + limit = min_not_zero(limit, local_limit); + break; + } + node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length); + } + acpi_put_table(&iort->header); + return limit; +} +#endif --- a/include/linux/acpi_iort.h +++ b/include/linux/acpi_iort.h @@ -38,6 +38,7 @@ void iort_dma_setup(struct device *dev, const struct iommu_ops *iort_iommu_configure_id(struct device *dev, const u32 *id_in); int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head); +phys_addr_t acpi_iort_dma_get_max_cpu_address(void); #else static inline void acpi_iort_init(void) { } static inline u32 iort_msi_map_id(struct device *dev, u32 id) @@ -55,6 +56,9 @@ static inline const struct iommu_ops *io static inline int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) { return 0; } + +static inline phys_addr_t acpi_iort_dma_get_max_cpu_address(void) +{ return PHYS_ADDR_MAX; } #endif #endif /* __ACPI_IORT_H__ */