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