Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp736386pxu; Fri, 23 Oct 2020 12:00:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyc6S0inXOJH95NoG/v02oDQDPSObGln8iPzTW20rVD9vjcU+dFiIjopMFDEg1Pp4B4DiR+ X-Received: by 2002:aa7:c683:: with SMTP id n3mr3606905edq.146.1603479613962; Fri, 23 Oct 2020 12:00:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603479613; cv=none; d=google.com; s=arc-20160816; b=tx7PEcMNFxhEdOIy8eJ/z3P7yRho3FuDFa+Vkkndyv5rYVYSeP+ax2P3EFmFgXb7t4 GkZrWJFGZvHaixCDwAaeRZAS66yu14bAqwTcazoxYUAHw/3GqJEH+LLTGKAVMFv0NgmY epzAuY+/UdNtKtMRlD2s08QJYrc2b6kcAkiKQQsTR67DySb3uLqVP7sqTX3VuOffm13U 9kqFWmV1fyC2FreXpy+g1J4Muw/ivLaQbZjCDksQnZPEQTROA0rSd8fTSe3vV6fOrDoF G7gb01vj6ptllUdvnciozTc9/lWFgeCZS6Z3sV7igJzbZ1gs5IeQtHw9ckIZ112mUtqw n3xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=VIrGQP7MeikqujJs/r2fZR9vPRP+sja5exPEwiMSznk=; b=Zk0SYT2aJ4ehpIW6OCriXfPYYXbVecVj0P3H9kNEvzry23nAjAeJYHtn9JADUrL3SJ 7H46RNlrngSJA+1vwoX41uP8VXWtakdD5YhCHFO6lkL+nf3TohNA0ghj+sQnQ5QIJAjZ U5E8lTPykOTvRk6oD7aZ+Vwo5LgVvT24GQ9pk1FfFtkiEsVaQUY1AsiqvB2Uap/wvhPa THPRlqMPASlRKy9IeRJRjSf+sMfoSpKuWXJxqLWDZ16XZyLGsw7wM9ZmzvY7duXmXmz9 nBAhxUQpKTX/vWB9+mZFptuEsfaYZldjEHIwhSP4qcF51KvS3DzhthL3xNI41PTV1J5j n/iw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si1398265ejt.206.2020.10.23.11.59.51; Fri, 23 Oct 2020 12:00:13 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753602AbgJWRiS (ORCPT + 99 others); Fri, 23 Oct 2020 13:38:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:35486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751573AbgJWRiR (ORCPT ); Fri, 23 Oct 2020 13:38:17 -0400 Received: from gaia (unknown [95.145.162.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 26A7721527; Fri, 23 Oct 2020 17:38:13 +0000 (UTC) Date: Fri, 23 Oct 2020 18:38:11 +0100 From: Catalin Marinas To: Nicolas Saenz Julienne Cc: robh+dt@kernel.org, hch@lst.de, ardb@kernel.org, linux-kernel@vger.kernel.org, robin.murphy@arm.com, linux-arm-kernel@lists.infradead.org, linux-rpi-kernel@lists.infradead.org, jeremy.linton@arm.com, iommu@lists.linux-foundation.org, devicetree@vger.kernel.org, will@kernel.org, lorenzo.pieralisi@arm.com, guohanjun@huawei.com Subject: Re: [PATCH v4 5/7] arm64: mm: Set ZONE_DMA size based on devicetree's dma-ranges Message-ID: <20201023173810.GH25736@gaia> References: <20201021123437.21538-1-nsaenzjulienne@suse.de> <20201021123437.21538-6-nsaenzjulienne@suse.de> <20201022180632.GI1229@gaia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 23, 2020 at 05:27:49PM +0200, Nicolas Saenz Julienne wrote: > On Thu, 2020-10-22 at 19:06 +0100, Catalin Marinas wrote: > > On Wed, Oct 21, 2020 at 02:34:35PM +0200, Nicolas Saenz Julienne wrote: > > > @@ -188,9 +186,11 @@ static phys_addr_t __init max_zone_phys(unsigned int zone_bits) > > > 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 dt_zone_dma_bits; > > > > > > #ifdef CONFIG_ZONE_DMA > > > - zone_dma_bits = ARM64_ZONE_DMA_BITS; > > > + dt_zone_dma_bits = ilog2(of_dma_get_max_cpu_address(NULL)); > > > + zone_dma_bits = min(32U, dt_zone_dma_bits); > > > > A thought: can we remove the min here and expand ZONE_DMA to whatever > > dt_zone_dma_bits says? More on this below. > > On most platforms we'd get PHYS_ADDR_MAX, or something bigger than the actual > amount of RAM. Which would ultimately create a system wide ZONE_DMA. At first > sight, I don't see it breaking dma-direct in any way. > > On the other hand, there is a big amount of MMIO devices out there that can > only handle 32-bit addressing. Be it PCI cards or actual IP cores. To make > things worse, this limitation is often expressed in the driver, not FW (with > dma_set_mask() and friends). If those devices aren't behind an IOMMU we have be > able to provide at least 32-bit addressable memory. See this comment from > dma_direct_supported(): > > /* > * Because 32-bit DMA masks are so common we expect every architecture > * to be able to satisfy them - either by not supporting more physical > * memory, or by providing a ZONE_DMA32. If neither is the case, the > * architecture needs to use an IOMMU instead of the direct mapping. > */ > > I think, for the common case, we're stuck with at least one zone spanning the > 32-bit address space. You are right, I guess it makes sense to keep a 32-bit zone as not all devices would be described as such. > > > arm64_dma_phys_limit = max_zone_phys(zone_dma_bits); > > > max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit); > > > #endif > > > > I was talking earlier to Ard and Robin on the ZONE_DMA32 history and the > > need for max_zone_phys(). This was rather theoretical, the Seattle > > platform has all RAM starting above 4GB and that led to an empty > > ZONE_DMA32 originally. The max_zone_phys() hack was meant to lift > > ZONE_DMA32 into the bottom of the RAM, on the assumption that such > > 32-bit devices would have a DMA offset hardwired. We are not aware of > > any such case on arm64 systems and even on Seattle, IIUC 32-bit devices > > only work if they are behind an SMMU (so no hardwired offset). > > > > In hindsight, it would have made more sense on platforms with RAM above > > 4GB to expand ZONE_DMA32 to cover the whole memory (so empty > > ZONE_NORMAL). Something like: > > > > diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c > > index a53c1e0fb017..7d5e3dd85617 100644 > > --- a/arch/arm64/mm/init.c > > +++ b/arch/arm64/mm/init.c > > @@ -187,8 +187,12 @@ static void __init reserve_elfcorehdr(void) > > */ > > static phys_addr_t __init max_zone_phys(unsigned int zone_bits) > > { > > - phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, zone_bits); > > - return min(offset + (1ULL << zone_bits), memblock_end_of_DRAM()); > > + phys_addr_t zone_mask = 1ULL << zone_bits; > > + > > + if (!(memblock_start_of_DRAM() & zone_mask)) > > + zone_mask = PHYS_ADDR_MAX; > > + > > + return min(zone_mask, memblock_end_of_DRAM()); > > } > > > > static void __init zone_sizes_init(unsigned long min, unsigned long max) > > > > I don't think this makes any difference for ZONE_DMA unless a > > broken DT or IORT reports the max CPU address below the start of DRAM. > > > > There's a minor issue if of_dma_get_max_cpu_address() matches > > memblock_end_of_DRAM() but they are not a power of 2. We'd be left with > > a bit of RAM at the end in ZONE_NORMAL due to ilog2 truncation. > > I agree it makes no sense to create more than one zone when the beginning of > RAM is located above the 32-bit address space. I'm all for disregarding the > possibility of hardwired offsets. As a bonus, as we already discussed some time > ago, this is something that never played well with current dma-direct code[1]. > > [1] https://lkml.org/lkml/2020/9/8/377 Maybe this one is still worth fixing, at least for consistency. But it's not urgent. My diff above has a side-effect that if dt_zone_dma_bits is below the start of DRAM, ZONE_DMA gets expanded to PHYS_ADDR_MAX. If this was 32-bit, that's fine but if it was, say, 30-bit because of some firmware misdescription with RAM starting at 2GB, we end up with no ZONE_DMA32. I think max_zone_phys() could cap this at 32, as a safety mechanism: static phys_addr_t __init max_zone_phys(unsigned int zone_bits) { phys_addr_t zone_mask = (1ULL << zone_bits) - 1; phys_addr_t phys_start = memblock_start_of_DRAM(); if (!(phys_start & U32_MAX)) zone_mask = PHYS_ADDR_MAX; else if (!(phys_start & zone_mask)) zone_mask = U32_MAX; return min(zone_mask + 1, memblock_end_of_DRAM()); } Assuming I got the shifting right, arm64_dma_phys_limit becomes: arm64_dma_phys_limit = max_zone_phys(zone_dma_bits, 32); -- Catalin