xref: /openbmc/linux/arch/x86/mm/mem_encrypt.c (revision aa74c44b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Memory Encryption Support Common Code
4  *
5  * Copyright (C) 2016 Advanced Micro Devices, Inc.
6  *
7  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8  */
9 
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>
15 #include <linux/virtio_config.h>
16 
17 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
18 bool force_dma_unencrypted(struct device *dev)
19 {
20 	/*
21 	 * For SEV, all DMA must be to unencrypted addresses.
22 	 */
23 	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
24 		return true;
25 
26 	/*
27 	 * For SME, all DMA must be to unencrypted addresses if the
28 	 * device does not support DMA to addresses that include the
29 	 * encryption mask.
30 	 */
31 	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
32 		u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
33 		u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
34 						dev->bus_dma_limit);
35 
36 		if (dma_dev_mask <= dma_enc_mask)
37 			return true;
38 	}
39 
40 	return false;
41 }
42 
43 static void print_mem_encrypt_feature_info(void)
44 {
45 	pr_info("AMD Memory Encryption Features active:");
46 
47 	/* Secure Memory Encryption */
48 	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
49 		/*
50 		 * SME is mutually exclusive with any of the SEV
51 		 * features below.
52 		 */
53 		pr_cont(" SME\n");
54 		return;
55 	}
56 
57 	/* Secure Encrypted Virtualization */
58 	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
59 		pr_cont(" SEV");
60 
61 	/* Encrypted Register State */
62 	if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
63 		pr_cont(" SEV-ES");
64 
65 	pr_cont("\n");
66 }
67 
68 /* Architecture __weak replacement functions */
69 void __init mem_encrypt_init(void)
70 {
71 	if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
72 		return;
73 
74 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
75 	swiotlb_update_mem_attributes();
76 
77 	print_mem_encrypt_feature_info();
78 }
79 
80 int arch_has_restricted_virtio_memory_access(void)
81 {
82 	return cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT);
83 }
84 EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access);
85