1 // SPDX-License-Identifier: GPL-2.0-only
3 * Memory Encryption Support Common Code
5 * Copyright (C) 2016 Advanced Micro Devices, Inc.
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
10 #include <linux/dma-direct.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/swiotlb.h>
13 #include <linux/cc_platform.h>
14 #include <linux/mem_encrypt.h>
16 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
17 bool force_dma_unencrypted(struct device *dev)
20 * For SEV, all DMA must be to unencrypted addresses.
22 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
26 * For SME, all DMA must be to unencrypted addresses if the
27 * device does not support DMA to addresses that include the
30 if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
31 u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
32 u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
35 if (dma_dev_mask <= dma_enc_mask)
42 static void print_mem_encrypt_feature_info(void)
44 pr_info("Memory Encryption Features active:");
46 if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
47 pr_cont(" Intel TDX\n");
53 /* Secure Memory Encryption */
54 if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
56 * SME is mutually exclusive with any of the SEV
63 /* Secure Encrypted Virtualization */
64 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
67 /* Encrypted Register State */
68 if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
71 /* Secure Nested Paging */
72 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
78 /* Architecture __weak replacement functions */
79 void __init mem_encrypt_init(void)
81 if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
84 /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
85 swiotlb_update_mem_attributes();
87 print_mem_encrypt_feature_info();