From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6F451C282C6 for ; Fri, 28 Feb 2025 18:25:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=HFY3A5FEoFKCvewQbEXprYR4rMM5a2iEW5C0VAdLtgs=; b=y2BhjUPCbYVf3tuWgWLLBp+ibU ULej41yuIj0Bq7wIRSJEFkLjgBsi/qsLtqL3Ny0lcVeZ2tD8XeUj0U6kmpvLKp8vhrOgq8Tyn2NuU 7Q1PvN6ksMq/aVaGAsWI/9vmccewnCf3aWzQFE5WiWvdE8jmv9KG586r/so0/OtJM1ERotaPo/nZH Rn61HfETLu3vZPwvADDZB+jBH94AAcv9Ux+Q2VSYGuAcJ7MstK1TNX6pM2NTOXZ6L/gJMTWgIUnZ5 hQNKEWYaBI/pvwt9/c3hOKlFjeedQXgA6AZwpQ92NWzTGJsYm4rvVPeLV+5H4OQg7Q/BD1SkKeYhO 2NzyknkA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1to536-0000000C7j0-222U; Fri, 28 Feb 2025 18:25:24 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1to51X-0000000C7M5-0KUx for linux-arm-kernel@lists.infradead.org; Fri, 28 Feb 2025 18:23:48 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6A7C6150C; Fri, 28 Feb 2025 10:23:59 -0800 (PST) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CC9453F6A8; Fri, 28 Feb 2025 10:23:42 -0800 (PST) Message-ID: <4f057e37-7f6c-4daa-b99a-122aa417f0e3@arm.com> Date: Fri, 28 Feb 2025 18:23:41 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 2/3] dma: Introduce generic dma_addr_*crypted helpers To: Suzuki K Poulose , linux-kernel@vger.kernel.org Cc: will@kernel.org, catalin.marinas@arm.com, maz@kernel.org, steven.price@arm.com, aneesh.kumar@kernel.org, gshan@redhat.com, linux-arm-kernel@lists.infradead.org, Jean-Philippe Brucker , Christoph Hellwig , Marek Szyprowski , Tom Lendacky References: <20250227144150.1667735-1-suzuki.poulose@arm.com> <20250227144150.1667735-3-suzuki.poulose@arm.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20250227144150.1667735-3-suzuki.poulose@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250228_102347_223573_45DA58CD X-CRM114-Status: GOOD ( 28.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 27/02/2025 2:41 pm, Suzuki K Poulose wrote: > AMD SME added __sme_set/__sme_clr primitives to modify the DMA address for > encrypted/decrypted traffic. However this doesn't fit in with other models, > e.g., Arm CCA where the meanings are the opposite. i.e., "decrypted" traffic > has a bit set and "encrypted" traffic has the top bit cleared. > > In preparation for adding the support for Arm CCA DMA conversions, convert the > existing primitives to more generic ones that can be provided by the backends. > i.e., add helpers to > 1. dma_addr_encrypted - Convert a DMA address to "encrypted" [ == __sme_set() ] > 2. dma_addr_unencrypted - Convert a DMA address to "decrypted" [ None exists today ] > 3. dma_addr_canonical - Clear any "encryption"/"decryption" bits from DMA > address [ SME uses __sme_clr() ] and convert to a canonical DMA address. > > Since the original __sme_xxx helpers come from linux/mem_encrypt.h, use that > as the home for the new definitions and provide dummy ones when none is provided > by the architectures. > > With the above, phys_to_dma_unencrypted() uses the newly added dma_addr_unencrypted() > helper and to make it a bit more easier to read and avoid double conversion, > provide __phys_to_dma(). Yeah, I'm happy with "canonical" being the least-worst option for now; I certainly can't think of anything snappier yet still sufficiently clear. Reviewed-by: Robin Murphy > Suggested-by: Robin Murphy > Cc: Will Deacon > Cc: Jean-Philippe Brucker > Cc: Catalin Marinas > Cc: Robin Murphy > Cc: Steven Price > Cc: Christoph Hellwig > Cc: Marek Szyprowski > Cc: Tom Lendacky > Cc: Aneesh Kumar K.V > Signed-off-by: Suzuki K Poulose > --- > Changes since v2: > - Rename helpers- s/dma_*crypted/dma_addr_*crypted (Robin) > --- > include/linux/dma-direct.h | 12 ++++++++---- > include/linux/mem_encrypt.h | 23 +++++++++++++++++++++++ > 2 files changed, 31 insertions(+), 4 deletions(-) > > diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h > index d20ecc24cb0f..f3bc0bcd7098 100644 > --- a/include/linux/dma-direct.h > +++ b/include/linux/dma-direct.h > @@ -78,14 +78,18 @@ static inline dma_addr_t dma_range_map_max(const struct bus_dma_region *map) > #define phys_to_dma_unencrypted phys_to_dma > #endif > #else > -static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, > - phys_addr_t paddr) > +static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) > { > if (dev->dma_range_map) > return translate_phys_to_dma(dev, paddr); > return paddr; > } > > +static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, > + phys_addr_t paddr) > +{ > + return dma_addr_unencrypted(__phys_to_dma(dev, paddr)); > +} > /* > * If memory encryption is supported, phys_to_dma will set the memory encryption > * bit in the DMA address, and dma_to_phys will clear it. > @@ -94,14 +98,14 @@ static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, > */ > static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) > { > - return __sme_set(phys_to_dma_unencrypted(dev, paddr)); > + return dma_addr_encrypted(__phys_to_dma(dev, paddr)); > } > > static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr) > { > phys_addr_t paddr; > > - dma_addr = __sme_clr(dma_addr); > + dma_addr = dma_addr_canonical(dma_addr); > if (dev->dma_range_map) > paddr = translate_dma_to_phys(dev, dma_addr); > else > diff --git a/include/linux/mem_encrypt.h b/include/linux/mem_encrypt.h > index ae4526389261..07584c5e36fb 100644 > --- a/include/linux/mem_encrypt.h > +++ b/include/linux/mem_encrypt.h > @@ -26,11 +26,34 @@ > */ > #define __sme_set(x) ((x) | sme_me_mask) > #define __sme_clr(x) ((x) & ~sme_me_mask) > + > +#define dma_addr_encrypted(x) __sme_set(x) > +#define dma_addr_canonical(x) __sme_clr(x) > + > #else > #define __sme_set(x) (x) > #define __sme_clr(x) (x) > #endif > > +/* > + * dma_addr_encrypted() and dma_addr_unencrypted() are for converting a given DMA > + * address to the respective type of addressing. > + * > + * dma_addr_canonical() is used to reverse any conversions for encrypted/decrypted > + * back to the canonical address. > + */ > +#ifndef dma_addr_encrypted > +#define dma_addr_encrypted(x) (x) > +#endif > + > +#ifndef dma_addr_unencrypted > +#define dma_addr_unencrypted(x) (x) > +#endif > + > +#ifndef dma_addr_canonical > +#define dma_addr_canonical(x) (x) > +#endif > + > #endif /* __ASSEMBLY__ */ > > #endif /* __MEM_ENCRYPT_H__ */