Skip to content

Commit b66e2ee

Browse files
Suzuki K Poulosectmarinas
authored andcommitted
dma: Introduce generic dma_addr_*crypted helpers
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(). Suggested-by: Robin Murphy <robin.murphy@arm.com> Cc: Will Deacon <will@kernel.org> Cc: Jean-Philippe Brucker <jean-philippe@linaro.org> Cc: Robin Murphy <robin.murphy@arm.com> Cc: Steven Price <steven.price@arm.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Aneesh Kumar K.V <aneesh.kumar@kernel.org> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Reviewed-by: Gavin Shan <gshan@redhat.com> Acked-by: Marek Szyprowski <m.szyprowski@samsung.com> Fixes: 42be24a ("arm64: Enable memory encrypt for Realms") Acked-by: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20250227144150.1667735-3-suzuki.poulose@arm.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent c380931 commit b66e2ee

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

include/linux/dma-direct.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ static inline dma_addr_t dma_range_map_max(const struct bus_dma_region *map)
7878
#define phys_to_dma_unencrypted phys_to_dma
7979
#endif
8080
#else
81-
static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev,
82-
phys_addr_t paddr)
81+
static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
8382
{
8483
if (dev->dma_range_map)
8584
return translate_phys_to_dma(dev, paddr);
8685
return paddr;
8786
}
8887

88+
static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev,
89+
phys_addr_t paddr)
90+
{
91+
return dma_addr_unencrypted(__phys_to_dma(dev, paddr));
92+
}
8993
/*
9094
* If memory encryption is supported, phys_to_dma will set the memory encryption
9195
* 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,
9498
*/
9599
static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
96100
{
97-
return __sme_set(phys_to_dma_unencrypted(dev, paddr));
101+
return dma_addr_encrypted(__phys_to_dma(dev, paddr));
98102
}
99103

100104
static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr)
101105
{
102106
phys_addr_t paddr;
103107

104-
dma_addr = __sme_clr(dma_addr);
108+
dma_addr = dma_addr_canonical(dma_addr);
105109
if (dev->dma_range_map)
106110
paddr = translate_dma_to_phys(dev, dma_addr);
107111
else

include/linux/mem_encrypt.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,34 @@
2626
*/
2727
#define __sme_set(x) ((x) | sme_me_mask)
2828
#define __sme_clr(x) ((x) & ~sme_me_mask)
29+
30+
#define dma_addr_encrypted(x) __sme_set(x)
31+
#define dma_addr_canonical(x) __sme_clr(x)
32+
2933
#else
3034
#define __sme_set(x) (x)
3135
#define __sme_clr(x) (x)
3236
#endif
3337

38+
/*
39+
* dma_addr_encrypted() and dma_addr_unencrypted() are for converting a given DMA
40+
* address to the respective type of addressing.
41+
*
42+
* dma_addr_canonical() is used to reverse any conversions for encrypted/decrypted
43+
* back to the canonical address.
44+
*/
45+
#ifndef dma_addr_encrypted
46+
#define dma_addr_encrypted(x) (x)
47+
#endif
48+
49+
#ifndef dma_addr_unencrypted
50+
#define dma_addr_unencrypted(x) (x)
51+
#endif
52+
53+
#ifndef dma_addr_canonical
54+
#define dma_addr_canonical(x) (x)
55+
#endif
56+
3457
#endif /* __ASSEMBLY__ */
3558

3659
#endif /* __MEM_ENCRYPT_H__ */

0 commit comments

Comments
 (0)