xref: /linux/arch/powerpc/platforms/pseries/svm.c (revision b83deaa741558babf4b8d51d34f6637ccfff1b26)
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/cc_platform.h>
12 #include <asm/machdep.h>
13 #include <asm/svm.h>
14 #include <asm/swiotlb.h>
15 #include <asm/ultravisor.h>
16 #include <asm/dtl.h>
17 
18 static int __init init_svm(void)
19 {
20 	if (!is_secure_guest())
21 		return 0;
22 
23 	/* Don't release the SWIOTLB buffer. */
24 	ppc_swiotlb_enable = 1;
25 
26 	/*
27 	 * Since the guest memory is inaccessible to the host, devices always
28 	 * need to use the SWIOTLB buffer for DMA even if dma_capable() says
29 	 * otherwise.
30 	 */
31 	swiotlb_force = SWIOTLB_FORCE;
32 
33 	/* Share the SWIOTLB buffer with the host. */
34 	swiotlb_update_mem_attributes();
35 
36 	return 0;
37 }
38 machine_early_initcall(pseries, init_svm);
39 
40 /*
41  * Initialize SWIOTLB. Essentially the same as swiotlb_init(), except that it
42  * can allocate the buffer anywhere in memory. Since the hypervisor doesn't have
43  * any addressing limitation, we don't need to allocate it in low addresses.
44  */
45 void __init svm_swiotlb_init(void)
46 {
47 	unsigned char *vstart;
48 	unsigned long bytes, io_tlb_nslabs;
49 
50 	io_tlb_nslabs = (swiotlb_size_or_default() >> IO_TLB_SHIFT);
51 	io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
52 
53 	bytes = io_tlb_nslabs << IO_TLB_SHIFT;
54 
55 	vstart = memblock_alloc(PAGE_ALIGN(bytes), PAGE_SIZE);
56 	if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, false))
57 		return;
58 
59 
60 	memblock_free(vstart, PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
61 	panic("SVM: Cannot allocate SWIOTLB buffer");
62 }
63 
64 int set_memory_encrypted(unsigned long addr, int numpages)
65 {
66 	if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
67 		return 0;
68 
69 	if (!PAGE_ALIGNED(addr))
70 		return -EINVAL;
71 
72 	uv_unshare_page(PHYS_PFN(__pa(addr)), numpages);
73 
74 	return 0;
75 }
76 
77 int set_memory_decrypted(unsigned long addr, int numpages)
78 {
79 	if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
80 		return 0;
81 
82 	if (!PAGE_ALIGNED(addr))
83 		return -EINVAL;
84 
85 	uv_share_page(PHYS_PFN(__pa(addr)), numpages);
86 
87 	return 0;
88 }
89 
90 /* There's one dispatch log per CPU. */
91 #define NR_DTL_PAGE (DISPATCH_LOG_BYTES * CONFIG_NR_CPUS / PAGE_SIZE)
92 
93 static struct page *dtl_page_store[NR_DTL_PAGE];
94 static long dtl_nr_pages;
95 
96 static bool is_dtl_page_shared(struct page *page)
97 {
98 	long i;
99 
100 	for (i = 0; i < dtl_nr_pages; i++)
101 		if (dtl_page_store[i] == page)
102 			return true;
103 
104 	return false;
105 }
106 
107 void dtl_cache_ctor(void *addr)
108 {
109 	unsigned long pfn = PHYS_PFN(__pa(addr));
110 	struct page *page = pfn_to_page(pfn);
111 
112 	if (!is_dtl_page_shared(page)) {
113 		dtl_page_store[dtl_nr_pages] = page;
114 		dtl_nr_pages++;
115 		WARN_ON(dtl_nr_pages >= NR_DTL_PAGE);
116 		uv_share_page(pfn, 1);
117 	}
118 }
119