xref: /linux/arch/powerpc/platforms/pseries/svm.c (revision 919464deeca24e5bf13b6c8efd0b1d25cc43866f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Secure VM platform
4  *
5  * Copyright 2018 IBM Corporation
6  * Author: Anshuman Khandual <khandual@linux.vnet.ibm.com>
7  */
8 
9 #include <linux/mm.h>
10 #include <linux/memblock.h>
11 #include <linux/mem_encrypt.h>
12 #include <linux/cc_platform.h>
13 #include <linux/mem_encrypt.h>
14 #include <asm/machdep.h>
15 #include <asm/svm.h>
16 #include <asm/swiotlb.h>
17 #include <asm/ultravisor.h>
18 #include <asm/dtl.h>
19 
20 static int __init init_svm(void)
21 {
22 	if (!is_secure_guest())
23 		return 0;
24 
25 	/* Don't release the SWIOTLB buffer. */
26 	ppc_swiotlb_enable = 1;
27 
28 	/*
29 	 * Since the guest memory is inaccessible to the host, devices always
30 	 * need to use the SWIOTLB buffer for DMA even if dma_capable() says
31 	 * otherwise.
32 	 */
33 	ppc_swiotlb_flags |= SWIOTLB_ANY | SWIOTLB_FORCE;
34 
35 	/* Share the SWIOTLB buffer with the host. */
36 	swiotlb_update_mem_attributes();
37 
38 	return 0;
39 }
40 machine_early_initcall(pseries, init_svm);
41 
42 int set_memory_encrypted(unsigned long addr, int numpages)
43 {
44 	if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
45 		return 0;
46 
47 	if (!PAGE_ALIGNED(addr))
48 		return -EINVAL;
49 
50 	uv_unshare_page(PHYS_PFN(__pa(addr)), numpages);
51 
52 	return 0;
53 }
54 
55 int set_memory_decrypted(unsigned long addr, int numpages)
56 {
57 	if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
58 		return 0;
59 
60 	if (!PAGE_ALIGNED(addr))
61 		return -EINVAL;
62 
63 	uv_share_page(PHYS_PFN(__pa(addr)), numpages);
64 
65 	return 0;
66 }
67 
68 /* There's one dispatch log per CPU. */
69 #define NR_DTL_PAGE (DISPATCH_LOG_BYTES * CONFIG_NR_CPUS / PAGE_SIZE)
70 
71 static struct page *dtl_page_store[NR_DTL_PAGE];
72 static long dtl_nr_pages;
73 
74 static bool is_dtl_page_shared(struct page *page)
75 {
76 	long i;
77 
78 	for (i = 0; i < dtl_nr_pages; i++)
79 		if (dtl_page_store[i] == page)
80 			return true;
81 
82 	return false;
83 }
84 
85 void dtl_cache_ctor(void *addr)
86 {
87 	unsigned long pfn = PHYS_PFN(__pa(addr));
88 	struct page *page = pfn_to_page(pfn);
89 
90 	if (!is_dtl_page_shared(page)) {
91 		dtl_page_store[dtl_nr_pages] = page;
92 		dtl_nr_pages++;
93 		WARN_ON(dtl_nr_pages >= NR_DTL_PAGE);
94 		uv_share_page(pfn, 1);
95 	}
96 }
97