xref: /openbmc/linux/arch/x86/mm/mem_encrypt_amd.c (revision ff3cfcb0)
1dbca5e1aSKuppuswamy Sathyanarayanan // SPDX-License-Identifier: GPL-2.0-only
2dbca5e1aSKuppuswamy Sathyanarayanan /*
3dbca5e1aSKuppuswamy Sathyanarayanan  * AMD Memory Encryption Support
4dbca5e1aSKuppuswamy Sathyanarayanan  *
5dbca5e1aSKuppuswamy Sathyanarayanan  * Copyright (C) 2016 Advanced Micro Devices, Inc.
6dbca5e1aSKuppuswamy Sathyanarayanan  *
7dbca5e1aSKuppuswamy Sathyanarayanan  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8dbca5e1aSKuppuswamy Sathyanarayanan  */
9dbca5e1aSKuppuswamy Sathyanarayanan 
10dbca5e1aSKuppuswamy Sathyanarayanan #define DISABLE_BRANCH_PROFILING
11dbca5e1aSKuppuswamy Sathyanarayanan 
12dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/linkage.h>
13dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/init.h>
14dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/mm.h>
15dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/dma-direct.h>
16dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/swiotlb.h>
17dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/mem_encrypt.h>
18dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/device.h>
19dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/kernel.h>
20dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/bitops.h>
21dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/dma-mapping.h>
22dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/virtio_config.h>
23a603002eSJuergen Gross #include <linux/virtio_anchor.h>
24dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/cc_platform.h>
25dbca5e1aSKuppuswamy Sathyanarayanan 
26dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/tlbflush.h>
27dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/fixmap.h>
28dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/setup.h>
294867fbbdSAnshuman Khandual #include <asm/mem_encrypt.h>
30dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/bootparam.h>
31dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/set_memory.h>
32dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/cacheflush.h>
33dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/processor-flags.h>
34dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/msr.h>
35dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/cmdline.h>
365e5ccff6SBrijesh Singh #include <asm/sev.h>
37dbca5e1aSKuppuswamy Sathyanarayanan 
38dbca5e1aSKuppuswamy Sathyanarayanan #include "mm_internal.h"
39dbca5e1aSKuppuswamy Sathyanarayanan 
40dbca5e1aSKuppuswamy Sathyanarayanan /*
41dbca5e1aSKuppuswamy Sathyanarayanan  * Since SME related variables are set early in the boot process they must
42dbca5e1aSKuppuswamy Sathyanarayanan  * reside in the .data section so as not to be zeroed out when the .bss
43dbca5e1aSKuppuswamy Sathyanarayanan  * section is later cleared.
44dbca5e1aSKuppuswamy Sathyanarayanan  */
45dbca5e1aSKuppuswamy Sathyanarayanan u64 sme_me_mask __section(".data") = 0;
46dbca5e1aSKuppuswamy Sathyanarayanan u64 sev_status __section(".data") = 0;
47dbca5e1aSKuppuswamy Sathyanarayanan u64 sev_check_data __section(".data") = 0;
48dbca5e1aSKuppuswamy Sathyanarayanan EXPORT_SYMBOL(sme_me_mask);
49dbca5e1aSKuppuswamy Sathyanarayanan 
50dbca5e1aSKuppuswamy Sathyanarayanan /* Buffer used for early in-place encryption by BSP, no locking needed */
51dbca5e1aSKuppuswamy Sathyanarayanan static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
52dbca5e1aSKuppuswamy Sathyanarayanan 
53dbca5e1aSKuppuswamy Sathyanarayanan /*
545e5ccff6SBrijesh Singh  * SNP-specific routine which needs to additionally change the page state from
555e5ccff6SBrijesh Singh  * private to shared before copying the data from the source to destination and
565e5ccff6SBrijesh Singh  * restore after the copy.
575e5ccff6SBrijesh Singh  */
585e5ccff6SBrijesh Singh static inline void __init snp_memcpy(void *dst, void *src, size_t sz,
595e5ccff6SBrijesh Singh 				     unsigned long paddr, bool decrypt)
605e5ccff6SBrijesh Singh {
615e5ccff6SBrijesh Singh 	unsigned long npages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
625e5ccff6SBrijesh Singh 
635e5ccff6SBrijesh Singh 	if (decrypt) {
645e5ccff6SBrijesh Singh 		/*
655e5ccff6SBrijesh Singh 		 * @paddr needs to be accessed decrypted, mark the page shared in
665e5ccff6SBrijesh Singh 		 * the RMP table before copying it.
675e5ccff6SBrijesh Singh 		 */
685e5ccff6SBrijesh Singh 		early_snp_set_memory_shared((unsigned long)__va(paddr), paddr, npages);
695e5ccff6SBrijesh Singh 
705e5ccff6SBrijesh Singh 		memcpy(dst, src, sz);
715e5ccff6SBrijesh Singh 
725e5ccff6SBrijesh Singh 		/* Restore the page state after the memcpy. */
735e5ccff6SBrijesh Singh 		early_snp_set_memory_private((unsigned long)__va(paddr), paddr, npages);
745e5ccff6SBrijesh Singh 	} else {
755e5ccff6SBrijesh Singh 		/*
765e5ccff6SBrijesh Singh 		 * @paddr need to be accessed encrypted, no need for the page state
775e5ccff6SBrijesh Singh 		 * change.
785e5ccff6SBrijesh Singh 		 */
795e5ccff6SBrijesh Singh 		memcpy(dst, src, sz);
805e5ccff6SBrijesh Singh 	}
815e5ccff6SBrijesh Singh }
825e5ccff6SBrijesh Singh 
835e5ccff6SBrijesh Singh /*
84dbca5e1aSKuppuswamy Sathyanarayanan  * This routine does not change the underlying encryption setting of the
85dbca5e1aSKuppuswamy Sathyanarayanan  * page(s) that map this memory. It assumes that eventually the memory is
86dbca5e1aSKuppuswamy Sathyanarayanan  * meant to be accessed as either encrypted or decrypted but the contents
87dbca5e1aSKuppuswamy Sathyanarayanan  * are currently not in the desired state.
88dbca5e1aSKuppuswamy Sathyanarayanan  *
89dbca5e1aSKuppuswamy Sathyanarayanan  * This routine follows the steps outlined in the AMD64 Architecture
90dbca5e1aSKuppuswamy Sathyanarayanan  * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
91dbca5e1aSKuppuswamy Sathyanarayanan  */
92dbca5e1aSKuppuswamy Sathyanarayanan static void __init __sme_early_enc_dec(resource_size_t paddr,
93dbca5e1aSKuppuswamy Sathyanarayanan 				       unsigned long size, bool enc)
94dbca5e1aSKuppuswamy Sathyanarayanan {
95dbca5e1aSKuppuswamy Sathyanarayanan 	void *src, *dst;
96dbca5e1aSKuppuswamy Sathyanarayanan 	size_t len;
97dbca5e1aSKuppuswamy Sathyanarayanan 
98dbca5e1aSKuppuswamy Sathyanarayanan 	if (!sme_me_mask)
99dbca5e1aSKuppuswamy Sathyanarayanan 		return;
100dbca5e1aSKuppuswamy Sathyanarayanan 
101dbca5e1aSKuppuswamy Sathyanarayanan 	wbinvd();
102dbca5e1aSKuppuswamy Sathyanarayanan 
103dbca5e1aSKuppuswamy Sathyanarayanan 	/*
104dbca5e1aSKuppuswamy Sathyanarayanan 	 * There are limited number of early mapping slots, so map (at most)
105dbca5e1aSKuppuswamy Sathyanarayanan 	 * one page at time.
106dbca5e1aSKuppuswamy Sathyanarayanan 	 */
107dbca5e1aSKuppuswamy Sathyanarayanan 	while (size) {
108dbca5e1aSKuppuswamy Sathyanarayanan 		len = min_t(size_t, sizeof(sme_early_buffer), size);
109dbca5e1aSKuppuswamy Sathyanarayanan 
110dbca5e1aSKuppuswamy Sathyanarayanan 		/*
111dbca5e1aSKuppuswamy Sathyanarayanan 		 * Create mappings for the current and desired format of
112dbca5e1aSKuppuswamy Sathyanarayanan 		 * the memory. Use a write-protected mapping for the source.
113dbca5e1aSKuppuswamy Sathyanarayanan 		 */
114dbca5e1aSKuppuswamy Sathyanarayanan 		src = enc ? early_memremap_decrypted_wp(paddr, len) :
115dbca5e1aSKuppuswamy Sathyanarayanan 			    early_memremap_encrypted_wp(paddr, len);
116dbca5e1aSKuppuswamy Sathyanarayanan 
117dbca5e1aSKuppuswamy Sathyanarayanan 		dst = enc ? early_memremap_encrypted(paddr, len) :
118dbca5e1aSKuppuswamy Sathyanarayanan 			    early_memremap_decrypted(paddr, len);
119dbca5e1aSKuppuswamy Sathyanarayanan 
120dbca5e1aSKuppuswamy Sathyanarayanan 		/*
121dbca5e1aSKuppuswamy Sathyanarayanan 		 * If a mapping can't be obtained to perform the operation,
122dbca5e1aSKuppuswamy Sathyanarayanan 		 * then eventual access of that area in the desired mode
123dbca5e1aSKuppuswamy Sathyanarayanan 		 * will cause a crash.
124dbca5e1aSKuppuswamy Sathyanarayanan 		 */
125dbca5e1aSKuppuswamy Sathyanarayanan 		BUG_ON(!src || !dst);
126dbca5e1aSKuppuswamy Sathyanarayanan 
127dbca5e1aSKuppuswamy Sathyanarayanan 		/*
128dbca5e1aSKuppuswamy Sathyanarayanan 		 * Use a temporary buffer, of cache-line multiple size, to
129dbca5e1aSKuppuswamy Sathyanarayanan 		 * avoid data corruption as documented in the APM.
130dbca5e1aSKuppuswamy Sathyanarayanan 		 */
1315e5ccff6SBrijesh Singh 		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
1325e5ccff6SBrijesh Singh 			snp_memcpy(sme_early_buffer, src, len, paddr, enc);
1335e5ccff6SBrijesh Singh 			snp_memcpy(dst, sme_early_buffer, len, paddr, !enc);
1345e5ccff6SBrijesh Singh 		} else {
135dbca5e1aSKuppuswamy Sathyanarayanan 			memcpy(sme_early_buffer, src, len);
136dbca5e1aSKuppuswamy Sathyanarayanan 			memcpy(dst, sme_early_buffer, len);
1375e5ccff6SBrijesh Singh 		}
138dbca5e1aSKuppuswamy Sathyanarayanan 
139dbca5e1aSKuppuswamy Sathyanarayanan 		early_memunmap(dst, len);
140dbca5e1aSKuppuswamy Sathyanarayanan 		early_memunmap(src, len);
141dbca5e1aSKuppuswamy Sathyanarayanan 
142dbca5e1aSKuppuswamy Sathyanarayanan 		paddr += len;
143dbca5e1aSKuppuswamy Sathyanarayanan 		size -= len;
144dbca5e1aSKuppuswamy Sathyanarayanan 	}
145dbca5e1aSKuppuswamy Sathyanarayanan }
146dbca5e1aSKuppuswamy Sathyanarayanan 
147dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
148dbca5e1aSKuppuswamy Sathyanarayanan {
149dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_enc_dec(paddr, size, true);
150dbca5e1aSKuppuswamy Sathyanarayanan }
151dbca5e1aSKuppuswamy Sathyanarayanan 
152dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
153dbca5e1aSKuppuswamy Sathyanarayanan {
154dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_enc_dec(paddr, size, false);
155dbca5e1aSKuppuswamy Sathyanarayanan }
156dbca5e1aSKuppuswamy Sathyanarayanan 
157dbca5e1aSKuppuswamy Sathyanarayanan static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
158dbca5e1aSKuppuswamy Sathyanarayanan 					     bool map)
159dbca5e1aSKuppuswamy Sathyanarayanan {
160dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
161dbca5e1aSKuppuswamy Sathyanarayanan 	pmdval_t pmd_flags, pmd;
162dbca5e1aSKuppuswamy Sathyanarayanan 
163dbca5e1aSKuppuswamy Sathyanarayanan 	/* Use early_pmd_flags but remove the encryption mask */
164dbca5e1aSKuppuswamy Sathyanarayanan 	pmd_flags = __sme_clr(early_pmd_flags);
165dbca5e1aSKuppuswamy Sathyanarayanan 
166dbca5e1aSKuppuswamy Sathyanarayanan 	do {
167dbca5e1aSKuppuswamy Sathyanarayanan 		pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
168dbca5e1aSKuppuswamy Sathyanarayanan 		__early_make_pgtable((unsigned long)vaddr, pmd);
169dbca5e1aSKuppuswamy Sathyanarayanan 
170dbca5e1aSKuppuswamy Sathyanarayanan 		vaddr += PMD_SIZE;
171dbca5e1aSKuppuswamy Sathyanarayanan 		paddr += PMD_SIZE;
172dbca5e1aSKuppuswamy Sathyanarayanan 		size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
173dbca5e1aSKuppuswamy Sathyanarayanan 	} while (size);
174dbca5e1aSKuppuswamy Sathyanarayanan 
175dbca5e1aSKuppuswamy Sathyanarayanan 	flush_tlb_local();
176dbca5e1aSKuppuswamy Sathyanarayanan }
177dbca5e1aSKuppuswamy Sathyanarayanan 
178dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_unmap_bootdata(char *real_mode_data)
179dbca5e1aSKuppuswamy Sathyanarayanan {
180dbca5e1aSKuppuswamy Sathyanarayanan 	struct boot_params *boot_data;
181dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long cmdline_paddr;
182dbca5e1aSKuppuswamy Sathyanarayanan 
183dbca5e1aSKuppuswamy Sathyanarayanan 	if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
184dbca5e1aSKuppuswamy Sathyanarayanan 		return;
185dbca5e1aSKuppuswamy Sathyanarayanan 
186dbca5e1aSKuppuswamy Sathyanarayanan 	/* Get the command line address before unmapping the real_mode_data */
187dbca5e1aSKuppuswamy Sathyanarayanan 	boot_data = (struct boot_params *)real_mode_data;
188dbca5e1aSKuppuswamy Sathyanarayanan 	cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
189dbca5e1aSKuppuswamy Sathyanarayanan 
190dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
191dbca5e1aSKuppuswamy Sathyanarayanan 
192dbca5e1aSKuppuswamy Sathyanarayanan 	if (!cmdline_paddr)
193dbca5e1aSKuppuswamy Sathyanarayanan 		return;
194dbca5e1aSKuppuswamy Sathyanarayanan 
195dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
196dbca5e1aSKuppuswamy Sathyanarayanan }
197dbca5e1aSKuppuswamy Sathyanarayanan 
198dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_map_bootdata(char *real_mode_data)
199dbca5e1aSKuppuswamy Sathyanarayanan {
200dbca5e1aSKuppuswamy Sathyanarayanan 	struct boot_params *boot_data;
201dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long cmdline_paddr;
202dbca5e1aSKuppuswamy Sathyanarayanan 
203dbca5e1aSKuppuswamy Sathyanarayanan 	if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
204dbca5e1aSKuppuswamy Sathyanarayanan 		return;
205dbca5e1aSKuppuswamy Sathyanarayanan 
206dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
207dbca5e1aSKuppuswamy Sathyanarayanan 
208dbca5e1aSKuppuswamy Sathyanarayanan 	/* Get the command line address after mapping the real_mode_data */
209dbca5e1aSKuppuswamy Sathyanarayanan 	boot_data = (struct boot_params *)real_mode_data;
210dbca5e1aSKuppuswamy Sathyanarayanan 	cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
211dbca5e1aSKuppuswamy Sathyanarayanan 
212dbca5e1aSKuppuswamy Sathyanarayanan 	if (!cmdline_paddr)
213dbca5e1aSKuppuswamy Sathyanarayanan 		return;
214dbca5e1aSKuppuswamy Sathyanarayanan 
215dbca5e1aSKuppuswamy Sathyanarayanan 	__sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
216dbca5e1aSKuppuswamy Sathyanarayanan }
217dbca5e1aSKuppuswamy Sathyanarayanan 
218dbca5e1aSKuppuswamy Sathyanarayanan void __init sev_setup_arch(void)
219dbca5e1aSKuppuswamy Sathyanarayanan {
220dbca5e1aSKuppuswamy Sathyanarayanan 	phys_addr_t total_mem = memblock_phys_mem_size();
221dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long size;
222dbca5e1aSKuppuswamy Sathyanarayanan 
223dbca5e1aSKuppuswamy Sathyanarayanan 	if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
224dbca5e1aSKuppuswamy Sathyanarayanan 		return;
225dbca5e1aSKuppuswamy Sathyanarayanan 
226dbca5e1aSKuppuswamy Sathyanarayanan 	/*
227dbca5e1aSKuppuswamy Sathyanarayanan 	 * For SEV, all DMA has to occur via shared/unencrypted pages.
228dbca5e1aSKuppuswamy Sathyanarayanan 	 * SEV uses SWIOTLB to make this happen without changing device
229dbca5e1aSKuppuswamy Sathyanarayanan 	 * drivers. However, depending on the workload being run, the
230dbca5e1aSKuppuswamy Sathyanarayanan 	 * default 64MB of SWIOTLB may not be enough and SWIOTLB may
231dbca5e1aSKuppuswamy Sathyanarayanan 	 * run out of buffers for DMA, resulting in I/O errors and/or
232dbca5e1aSKuppuswamy Sathyanarayanan 	 * performance degradation especially with high I/O workloads.
233dbca5e1aSKuppuswamy Sathyanarayanan 	 *
234dbca5e1aSKuppuswamy Sathyanarayanan 	 * Adjust the default size of SWIOTLB for SEV guests using
235dbca5e1aSKuppuswamy Sathyanarayanan 	 * a percentage of guest memory for SWIOTLB buffers.
236dbca5e1aSKuppuswamy Sathyanarayanan 	 * Also, as the SWIOTLB bounce buffer memory is allocated
237dbca5e1aSKuppuswamy Sathyanarayanan 	 * from low memory, ensure that the adjusted size is within
238dbca5e1aSKuppuswamy Sathyanarayanan 	 * the limits of low available memory.
239dbca5e1aSKuppuswamy Sathyanarayanan 	 *
240dbca5e1aSKuppuswamy Sathyanarayanan 	 * The percentage of guest memory used here for SWIOTLB buffers
241dbca5e1aSKuppuswamy Sathyanarayanan 	 * is more of an approximation of the static adjustment which
242dbca5e1aSKuppuswamy Sathyanarayanan 	 * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
243dbca5e1aSKuppuswamy Sathyanarayanan 	 */
244dbca5e1aSKuppuswamy Sathyanarayanan 	size = total_mem * 6 / 100;
245dbca5e1aSKuppuswamy Sathyanarayanan 	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
246dbca5e1aSKuppuswamy Sathyanarayanan 	swiotlb_adjust_size(size);
2473f9dfbebSJuergen Gross 
2483f9dfbebSJuergen Gross 	/* Set restricted memory access for virtio. */
249a603002eSJuergen Gross 	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
250dbca5e1aSKuppuswamy Sathyanarayanan }
251dbca5e1aSKuppuswamy Sathyanarayanan 
252dbca5e1aSKuppuswamy Sathyanarayanan static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
253dbca5e1aSKuppuswamy Sathyanarayanan {
254dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long pfn = 0;
255dbca5e1aSKuppuswamy Sathyanarayanan 	pgprot_t prot;
256dbca5e1aSKuppuswamy Sathyanarayanan 
257dbca5e1aSKuppuswamy Sathyanarayanan 	switch (level) {
258dbca5e1aSKuppuswamy Sathyanarayanan 	case PG_LEVEL_4K:
259dbca5e1aSKuppuswamy Sathyanarayanan 		pfn = pte_pfn(*kpte);
260dbca5e1aSKuppuswamy Sathyanarayanan 		prot = pte_pgprot(*kpte);
261dbca5e1aSKuppuswamy Sathyanarayanan 		break;
262dbca5e1aSKuppuswamy Sathyanarayanan 	case PG_LEVEL_2M:
263dbca5e1aSKuppuswamy Sathyanarayanan 		pfn = pmd_pfn(*(pmd_t *)kpte);
264dbca5e1aSKuppuswamy Sathyanarayanan 		prot = pmd_pgprot(*(pmd_t *)kpte);
265dbca5e1aSKuppuswamy Sathyanarayanan 		break;
266dbca5e1aSKuppuswamy Sathyanarayanan 	case PG_LEVEL_1G:
267dbca5e1aSKuppuswamy Sathyanarayanan 		pfn = pud_pfn(*(pud_t *)kpte);
268dbca5e1aSKuppuswamy Sathyanarayanan 		prot = pud_pgprot(*(pud_t *)kpte);
269dbca5e1aSKuppuswamy Sathyanarayanan 		break;
270dbca5e1aSKuppuswamy Sathyanarayanan 	default:
271dbca5e1aSKuppuswamy Sathyanarayanan 		WARN_ONCE(1, "Invalid level for kpte\n");
272dbca5e1aSKuppuswamy Sathyanarayanan 		return 0;
273dbca5e1aSKuppuswamy Sathyanarayanan 	}
274dbca5e1aSKuppuswamy Sathyanarayanan 
275dbca5e1aSKuppuswamy Sathyanarayanan 	if (ret_prot)
276dbca5e1aSKuppuswamy Sathyanarayanan 		*ret_prot = prot;
277dbca5e1aSKuppuswamy Sathyanarayanan 
278dbca5e1aSKuppuswamy Sathyanarayanan 	return pfn;
279dbca5e1aSKuppuswamy Sathyanarayanan }
280dbca5e1aSKuppuswamy Sathyanarayanan 
2811e8c5971SBrijesh Singh static bool amd_enc_tlb_flush_required(bool enc)
2821e8c5971SBrijesh Singh {
2831e8c5971SBrijesh Singh 	return true;
2841e8c5971SBrijesh Singh }
2851e8c5971SBrijesh Singh 
2861e8c5971SBrijesh Singh static bool amd_enc_cache_flush_required(void)
2871e8c5971SBrijesh Singh {
2881e8c5971SBrijesh Singh 	return !cpu_feature_enabled(X86_FEATURE_SME_COHERENT);
2891e8c5971SBrijesh Singh }
2901e8c5971SBrijesh Singh 
2911e8c5971SBrijesh Singh static void enc_dec_hypercall(unsigned long vaddr, int npages, bool enc)
292dbca5e1aSKuppuswamy Sathyanarayanan {
293dbca5e1aSKuppuswamy Sathyanarayanan #ifdef CONFIG_PARAVIRT
294dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long sz = npages << PAGE_SHIFT;
295dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long vaddr_end = vaddr + sz;
296dbca5e1aSKuppuswamy Sathyanarayanan 
297dbca5e1aSKuppuswamy Sathyanarayanan 	while (vaddr < vaddr_end) {
298dbca5e1aSKuppuswamy Sathyanarayanan 		int psize, pmask, level;
299dbca5e1aSKuppuswamy Sathyanarayanan 		unsigned long pfn;
300dbca5e1aSKuppuswamy Sathyanarayanan 		pte_t *kpte;
301dbca5e1aSKuppuswamy Sathyanarayanan 
302dbca5e1aSKuppuswamy Sathyanarayanan 		kpte = lookup_address(vaddr, &level);
303dbca5e1aSKuppuswamy Sathyanarayanan 		if (!kpte || pte_none(*kpte)) {
304dbca5e1aSKuppuswamy Sathyanarayanan 			WARN_ONCE(1, "kpte lookup for vaddr\n");
305dbca5e1aSKuppuswamy Sathyanarayanan 			return;
306dbca5e1aSKuppuswamy Sathyanarayanan 		}
307dbca5e1aSKuppuswamy Sathyanarayanan 
308dbca5e1aSKuppuswamy Sathyanarayanan 		pfn = pg_level_to_pfn(level, kpte, NULL);
309dbca5e1aSKuppuswamy Sathyanarayanan 		if (!pfn)
310dbca5e1aSKuppuswamy Sathyanarayanan 			continue;
311dbca5e1aSKuppuswamy Sathyanarayanan 
312dbca5e1aSKuppuswamy Sathyanarayanan 		psize = page_level_size(level);
313dbca5e1aSKuppuswamy Sathyanarayanan 		pmask = page_level_mask(level);
314dbca5e1aSKuppuswamy Sathyanarayanan 
315dbca5e1aSKuppuswamy Sathyanarayanan 		notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT, enc);
316dbca5e1aSKuppuswamy Sathyanarayanan 
317dbca5e1aSKuppuswamy Sathyanarayanan 		vaddr = (vaddr & pmask) + psize;
318dbca5e1aSKuppuswamy Sathyanarayanan 	}
319dbca5e1aSKuppuswamy Sathyanarayanan #endif
320dbca5e1aSKuppuswamy Sathyanarayanan }
321dbca5e1aSKuppuswamy Sathyanarayanan 
3221e8c5971SBrijesh Singh static void amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
3231e8c5971SBrijesh Singh {
324dc3f3d24SBrijesh Singh 	/*
325dc3f3d24SBrijesh Singh 	 * To maintain the security guarantees of SEV-SNP guests, make sure
326dc3f3d24SBrijesh Singh 	 * to invalidate the memory before encryption attribute is cleared.
327dc3f3d24SBrijesh Singh 	 */
328dc3f3d24SBrijesh Singh 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc)
329dc3f3d24SBrijesh Singh 		snp_set_memory_shared(vaddr, npages);
3301e8c5971SBrijesh Singh }
3311e8c5971SBrijesh Singh 
3321e8c5971SBrijesh Singh /* Return true unconditionally: return value doesn't matter for the SEV side */
3331e8c5971SBrijesh Singh static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc)
3341e8c5971SBrijesh Singh {
335dc3f3d24SBrijesh Singh 	/*
336dc3f3d24SBrijesh Singh 	 * After memory is mapped encrypted in the page table, validate it
337dc3f3d24SBrijesh Singh 	 * so that it is consistent with the page table updates.
338dc3f3d24SBrijesh Singh 	 */
339dc3f3d24SBrijesh Singh 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && enc)
340dc3f3d24SBrijesh Singh 		snp_set_memory_private(vaddr, npages);
341dc3f3d24SBrijesh Singh 
3421e8c5971SBrijesh Singh 	if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
3431e8c5971SBrijesh Singh 		enc_dec_hypercall(vaddr, npages, enc);
3441e8c5971SBrijesh Singh 
3451e8c5971SBrijesh Singh 	return true;
3461e8c5971SBrijesh Singh }
3471e8c5971SBrijesh Singh 
348dbca5e1aSKuppuswamy Sathyanarayanan static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
349dbca5e1aSKuppuswamy Sathyanarayanan {
350dbca5e1aSKuppuswamy Sathyanarayanan 	pgprot_t old_prot, new_prot;
351dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long pfn, pa, size;
352dbca5e1aSKuppuswamy Sathyanarayanan 	pte_t new_pte;
353dbca5e1aSKuppuswamy Sathyanarayanan 
354dbca5e1aSKuppuswamy Sathyanarayanan 	pfn = pg_level_to_pfn(level, kpte, &old_prot);
355dbca5e1aSKuppuswamy Sathyanarayanan 	if (!pfn)
356dbca5e1aSKuppuswamy Sathyanarayanan 		return;
357dbca5e1aSKuppuswamy Sathyanarayanan 
358dbca5e1aSKuppuswamy Sathyanarayanan 	new_prot = old_prot;
359dbca5e1aSKuppuswamy Sathyanarayanan 	if (enc)
360dbca5e1aSKuppuswamy Sathyanarayanan 		pgprot_val(new_prot) |= _PAGE_ENC;
361dbca5e1aSKuppuswamy Sathyanarayanan 	else
362dbca5e1aSKuppuswamy Sathyanarayanan 		pgprot_val(new_prot) &= ~_PAGE_ENC;
363dbca5e1aSKuppuswamy Sathyanarayanan 
364dbca5e1aSKuppuswamy Sathyanarayanan 	/* If prot is same then do nothing. */
365dbca5e1aSKuppuswamy Sathyanarayanan 	if (pgprot_val(old_prot) == pgprot_val(new_prot))
366dbca5e1aSKuppuswamy Sathyanarayanan 		return;
367dbca5e1aSKuppuswamy Sathyanarayanan 
368dbca5e1aSKuppuswamy Sathyanarayanan 	pa = pfn << PAGE_SHIFT;
369dbca5e1aSKuppuswamy Sathyanarayanan 	size = page_level_size(level);
370dbca5e1aSKuppuswamy Sathyanarayanan 
371dbca5e1aSKuppuswamy Sathyanarayanan 	/*
372dbca5e1aSKuppuswamy Sathyanarayanan 	 * We are going to perform in-place en-/decryption and change the
373dbca5e1aSKuppuswamy Sathyanarayanan 	 * physical page attribute from C=1 to C=0 or vice versa. Flush the
374dbca5e1aSKuppuswamy Sathyanarayanan 	 * caches to ensure that data gets accessed with the correct C-bit.
375dbca5e1aSKuppuswamy Sathyanarayanan 	 */
376dbca5e1aSKuppuswamy Sathyanarayanan 	clflush_cache_range(__va(pa), size);
377dbca5e1aSKuppuswamy Sathyanarayanan 
378dbca5e1aSKuppuswamy Sathyanarayanan 	/* Encrypt/decrypt the contents in-place */
3795e5ccff6SBrijesh Singh 	if (enc) {
380dbca5e1aSKuppuswamy Sathyanarayanan 		sme_early_encrypt(pa, size);
3815e5ccff6SBrijesh Singh 	} else {
382dbca5e1aSKuppuswamy Sathyanarayanan 		sme_early_decrypt(pa, size);
383dbca5e1aSKuppuswamy Sathyanarayanan 
3845e5ccff6SBrijesh Singh 		/*
3855e5ccff6SBrijesh Singh 		 * ON SNP, the page state in the RMP table must happen
3865e5ccff6SBrijesh Singh 		 * before the page table updates.
3875e5ccff6SBrijesh Singh 		 */
3885e5ccff6SBrijesh Singh 		early_snp_set_memory_shared((unsigned long)__va(pa), pa, 1);
3895e5ccff6SBrijesh Singh 	}
3905e5ccff6SBrijesh Singh 
391dbca5e1aSKuppuswamy Sathyanarayanan 	/* Change the page encryption mask. */
392dbca5e1aSKuppuswamy Sathyanarayanan 	new_pte = pfn_pte(pfn, new_prot);
393dbca5e1aSKuppuswamy Sathyanarayanan 	set_pte_atomic(kpte, new_pte);
3945e5ccff6SBrijesh Singh 
3955e5ccff6SBrijesh Singh 	/*
3965e5ccff6SBrijesh Singh 	 * If page is set encrypted in the page table, then update the RMP table to
3975e5ccff6SBrijesh Singh 	 * add this page as private.
3985e5ccff6SBrijesh Singh 	 */
3995e5ccff6SBrijesh Singh 	if (enc)
4005e5ccff6SBrijesh Singh 		early_snp_set_memory_private((unsigned long)__va(pa), pa, 1);
401dbca5e1aSKuppuswamy Sathyanarayanan }
402dbca5e1aSKuppuswamy Sathyanarayanan 
403dbca5e1aSKuppuswamy Sathyanarayanan static int __init early_set_memory_enc_dec(unsigned long vaddr,
404dbca5e1aSKuppuswamy Sathyanarayanan 					   unsigned long size, bool enc)
405dbca5e1aSKuppuswamy Sathyanarayanan {
406dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long vaddr_end, vaddr_next, start;
407dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long psize, pmask;
408dbca5e1aSKuppuswamy Sathyanarayanan 	int split_page_size_mask;
409dbca5e1aSKuppuswamy Sathyanarayanan 	int level, ret;
410dbca5e1aSKuppuswamy Sathyanarayanan 	pte_t *kpte;
411dbca5e1aSKuppuswamy Sathyanarayanan 
412dbca5e1aSKuppuswamy Sathyanarayanan 	start = vaddr;
413dbca5e1aSKuppuswamy Sathyanarayanan 	vaddr_next = vaddr;
414dbca5e1aSKuppuswamy Sathyanarayanan 	vaddr_end = vaddr + size;
415dbca5e1aSKuppuswamy Sathyanarayanan 
416dbca5e1aSKuppuswamy Sathyanarayanan 	for (; vaddr < vaddr_end; vaddr = vaddr_next) {
417dbca5e1aSKuppuswamy Sathyanarayanan 		kpte = lookup_address(vaddr, &level);
418dbca5e1aSKuppuswamy Sathyanarayanan 		if (!kpte || pte_none(*kpte)) {
419dbca5e1aSKuppuswamy Sathyanarayanan 			ret = 1;
420dbca5e1aSKuppuswamy Sathyanarayanan 			goto out;
421dbca5e1aSKuppuswamy Sathyanarayanan 		}
422dbca5e1aSKuppuswamy Sathyanarayanan 
423dbca5e1aSKuppuswamy Sathyanarayanan 		if (level == PG_LEVEL_4K) {
424dbca5e1aSKuppuswamy Sathyanarayanan 			__set_clr_pte_enc(kpte, level, enc);
425dbca5e1aSKuppuswamy Sathyanarayanan 			vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
426dbca5e1aSKuppuswamy Sathyanarayanan 			continue;
427dbca5e1aSKuppuswamy Sathyanarayanan 		}
428dbca5e1aSKuppuswamy Sathyanarayanan 
429dbca5e1aSKuppuswamy Sathyanarayanan 		psize = page_level_size(level);
430dbca5e1aSKuppuswamy Sathyanarayanan 		pmask = page_level_mask(level);
431dbca5e1aSKuppuswamy Sathyanarayanan 
432dbca5e1aSKuppuswamy Sathyanarayanan 		/*
433dbca5e1aSKuppuswamy Sathyanarayanan 		 * Check whether we can change the large page in one go.
434dbca5e1aSKuppuswamy Sathyanarayanan 		 * We request a split when the address is not aligned and
435dbca5e1aSKuppuswamy Sathyanarayanan 		 * the number of pages to set/clear encryption bit is smaller
436dbca5e1aSKuppuswamy Sathyanarayanan 		 * than the number of pages in the large page.
437dbca5e1aSKuppuswamy Sathyanarayanan 		 */
438dbca5e1aSKuppuswamy Sathyanarayanan 		if (vaddr == (vaddr & pmask) &&
439dbca5e1aSKuppuswamy Sathyanarayanan 		    ((vaddr_end - vaddr) >= psize)) {
440dbca5e1aSKuppuswamy Sathyanarayanan 			__set_clr_pte_enc(kpte, level, enc);
441dbca5e1aSKuppuswamy Sathyanarayanan 			vaddr_next = (vaddr & pmask) + psize;
442dbca5e1aSKuppuswamy Sathyanarayanan 			continue;
443dbca5e1aSKuppuswamy Sathyanarayanan 		}
444dbca5e1aSKuppuswamy Sathyanarayanan 
445dbca5e1aSKuppuswamy Sathyanarayanan 		/*
446dbca5e1aSKuppuswamy Sathyanarayanan 		 * The virtual address is part of a larger page, create the next
447dbca5e1aSKuppuswamy Sathyanarayanan 		 * level page table mapping (4K or 2M). If it is part of a 2M
448dbca5e1aSKuppuswamy Sathyanarayanan 		 * page then we request a split of the large page into 4K
449dbca5e1aSKuppuswamy Sathyanarayanan 		 * chunks. A 1GB large page is split into 2M pages, resp.
450dbca5e1aSKuppuswamy Sathyanarayanan 		 */
451dbca5e1aSKuppuswamy Sathyanarayanan 		if (level == PG_LEVEL_2M)
452dbca5e1aSKuppuswamy Sathyanarayanan 			split_page_size_mask = 0;
453dbca5e1aSKuppuswamy Sathyanarayanan 		else
454dbca5e1aSKuppuswamy Sathyanarayanan 			split_page_size_mask = 1 << PG_LEVEL_2M;
455dbca5e1aSKuppuswamy Sathyanarayanan 
456dbca5e1aSKuppuswamy Sathyanarayanan 		/*
457dbca5e1aSKuppuswamy Sathyanarayanan 		 * kernel_physical_mapping_change() does not flush the TLBs, so
458dbca5e1aSKuppuswamy Sathyanarayanan 		 * a TLB flush is required after we exit from the for loop.
459dbca5e1aSKuppuswamy Sathyanarayanan 		 */
460dbca5e1aSKuppuswamy Sathyanarayanan 		kernel_physical_mapping_change(__pa(vaddr & pmask),
461dbca5e1aSKuppuswamy Sathyanarayanan 					       __pa((vaddr_end & pmask) + psize),
462dbca5e1aSKuppuswamy Sathyanarayanan 					       split_page_size_mask);
463dbca5e1aSKuppuswamy Sathyanarayanan 	}
464dbca5e1aSKuppuswamy Sathyanarayanan 
465dbca5e1aSKuppuswamy Sathyanarayanan 	ret = 0;
466dbca5e1aSKuppuswamy Sathyanarayanan 
4671e8c5971SBrijesh Singh 	early_set_mem_enc_dec_hypercall(start, PAGE_ALIGN(size) >> PAGE_SHIFT, enc);
468dbca5e1aSKuppuswamy Sathyanarayanan out:
469dbca5e1aSKuppuswamy Sathyanarayanan 	__flush_tlb_all();
470dbca5e1aSKuppuswamy Sathyanarayanan 	return ret;
471dbca5e1aSKuppuswamy Sathyanarayanan }
472dbca5e1aSKuppuswamy Sathyanarayanan 
473dbca5e1aSKuppuswamy Sathyanarayanan int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
474dbca5e1aSKuppuswamy Sathyanarayanan {
475dbca5e1aSKuppuswamy Sathyanarayanan 	return early_set_memory_enc_dec(vaddr, size, false);
476dbca5e1aSKuppuswamy Sathyanarayanan }
477dbca5e1aSKuppuswamy Sathyanarayanan 
478dbca5e1aSKuppuswamy Sathyanarayanan int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
479dbca5e1aSKuppuswamy Sathyanarayanan {
480dbca5e1aSKuppuswamy Sathyanarayanan 	return early_set_memory_enc_dec(vaddr, size, true);
481dbca5e1aSKuppuswamy Sathyanarayanan }
482dbca5e1aSKuppuswamy Sathyanarayanan 
483dbca5e1aSKuppuswamy Sathyanarayanan void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, bool enc)
484dbca5e1aSKuppuswamy Sathyanarayanan {
4851e8c5971SBrijesh Singh 	enc_dec_hypercall(vaddr, npages, enc);
4861e8c5971SBrijesh Singh }
4871e8c5971SBrijesh Singh 
4881e8c5971SBrijesh Singh void __init sme_early_init(void)
4891e8c5971SBrijesh Singh {
4901e8c5971SBrijesh Singh 	if (!sme_me_mask)
4911e8c5971SBrijesh Singh 		return;
4921e8c5971SBrijesh Singh 
4931e8c5971SBrijesh Singh 	early_pmd_flags = __sme_set(early_pmd_flags);
4941e8c5971SBrijesh Singh 
4951e8c5971SBrijesh Singh 	__supported_pte_mask = __sme_set(__supported_pte_mask);
4961e8c5971SBrijesh Singh 
4971e8c5971SBrijesh Singh 	/* Update the protection map with memory encryption mask */
4984867fbbdSAnshuman Khandual 	add_encrypt_protection_map();
4991e8c5971SBrijesh Singh 
5001e8c5971SBrijesh Singh 	x86_platform.guest.enc_status_change_prepare = amd_enc_status_change_prepare;
5011e8c5971SBrijesh Singh 	x86_platform.guest.enc_status_change_finish  = amd_enc_status_change_finish;
5021e8c5971SBrijesh Singh 	x86_platform.guest.enc_tlb_flush_required    = amd_enc_tlb_flush_required;
5031e8c5971SBrijesh Singh 	x86_platform.guest.enc_cache_flush_required  = amd_enc_cache_flush_required;
504*ff3cfcb0SThomas Gleixner 
505*ff3cfcb0SThomas Gleixner 	/*
506*ff3cfcb0SThomas Gleixner 	 * AMD-SEV-ES intercepts the RDMSR to read the X2APIC ID in the
507*ff3cfcb0SThomas Gleixner 	 * parallel bringup low level code. That raises #VC which cannot be
508*ff3cfcb0SThomas Gleixner 	 * handled there.
509*ff3cfcb0SThomas Gleixner 	 * It does not provide a RDMSR GHCB protocol so the early startup
510*ff3cfcb0SThomas Gleixner 	 * code cannot directly communicate with the secure firmware. The
511*ff3cfcb0SThomas Gleixner 	 * alternative solution to retrieve the APIC ID via CPUID(0xb),
512*ff3cfcb0SThomas Gleixner 	 * which is covered by the GHCB protocol, is not viable either
513*ff3cfcb0SThomas Gleixner 	 * because there is no enforcement of the CPUID(0xb) provided
514*ff3cfcb0SThomas Gleixner 	 * "initial" APIC ID to be the same as the real APIC ID.
515*ff3cfcb0SThomas Gleixner 	 * Disable parallel bootup.
516*ff3cfcb0SThomas Gleixner 	 */
517*ff3cfcb0SThomas Gleixner 	if (sev_status & MSR_AMD64_SEV_ES_ENABLED)
518*ff3cfcb0SThomas Gleixner 		x86_cpuinit.parallel_bringup = false;
519dbca5e1aSKuppuswamy Sathyanarayanan }
520dbca5e1aSKuppuswamy Sathyanarayanan 
521dbca5e1aSKuppuswamy Sathyanarayanan void __init mem_encrypt_free_decrypted_mem(void)
522dbca5e1aSKuppuswamy Sathyanarayanan {
523dbca5e1aSKuppuswamy Sathyanarayanan 	unsigned long vaddr, vaddr_end, npages;
524dbca5e1aSKuppuswamy Sathyanarayanan 	int r;
525dbca5e1aSKuppuswamy Sathyanarayanan 
526dbca5e1aSKuppuswamy Sathyanarayanan 	vaddr = (unsigned long)__start_bss_decrypted_unused;
527dbca5e1aSKuppuswamy Sathyanarayanan 	vaddr_end = (unsigned long)__end_bss_decrypted;
528dbca5e1aSKuppuswamy Sathyanarayanan 	npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
529dbca5e1aSKuppuswamy Sathyanarayanan 
530dbca5e1aSKuppuswamy Sathyanarayanan 	/*
531c7b5254bSMichael Kelley 	 * If the unused memory range was mapped decrypted, change the encryption
532c7b5254bSMichael Kelley 	 * attribute from decrypted to encrypted before freeing it. Base the
533c7b5254bSMichael Kelley 	 * re-encryption on the same condition used for the decryption in
534c7b5254bSMichael Kelley 	 * sme_postprocess_startup(). Higher level abstractions, such as
535c7b5254bSMichael Kelley 	 * CC_ATTR_MEM_ENCRYPT, aren't necessarily equivalent in a Hyper-V VM
536c7b5254bSMichael Kelley 	 * using vTOM, where sme_me_mask is always zero.
537dbca5e1aSKuppuswamy Sathyanarayanan 	 */
538c7b5254bSMichael Kelley 	if (sme_me_mask) {
539dbca5e1aSKuppuswamy Sathyanarayanan 		r = set_memory_encrypted(vaddr, npages);
540dbca5e1aSKuppuswamy Sathyanarayanan 		if (r) {
541dbca5e1aSKuppuswamy Sathyanarayanan 			pr_warn("failed to free unused decrypted pages\n");
542dbca5e1aSKuppuswamy Sathyanarayanan 			return;
543dbca5e1aSKuppuswamy Sathyanarayanan 		}
544dbca5e1aSKuppuswamy Sathyanarayanan 	}
545dbca5e1aSKuppuswamy Sathyanarayanan 
546dbca5e1aSKuppuswamy Sathyanarayanan 	free_init_pages("unused decrypted", vaddr, vaddr_end);
547dbca5e1aSKuppuswamy Sathyanarayanan }
548