KVM: PPC: Add @page_shift to kvmppc_spapr_tce_table
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_64_vio_hv.c
CommitLineData
54738c09
DG
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
d3695aa4 17 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
54738c09
DG
18 */
19
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/highmem.h>
25#include <linux/gfp.h>
26#include <linux/slab.h>
27#include <linux/hugetlb.h>
28#include <linux/list.h>
29
30#include <asm/tlbflush.h>
31#include <asm/kvm_ppc.h>
32#include <asm/kvm_book3s.h>
33#include <asm/mmu-hash64.h>
d3695aa4 34#include <asm/mmu_context.h>
54738c09
DG
35#include <asm/hvcall.h>
36#include <asm/synch.h>
37#include <asm/ppc-opcode.h>
38#include <asm/kvm_host.h>
39#include <asm/udbg.h>
fcbb2ce6 40#include <asm/iommu.h>
5ee7af18 41#include <asm/tce.h>
d3695aa4 42#include <asm/iommu.h>
54738c09
DG
43
44#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
45
fcbb2ce6
AK
46/*
47 * Finds a TCE table descriptor by LIOBN.
48 *
49 * WARNING: This will be called in real or virtual mode on HV KVM and virtual
50 * mode on PR KVM
51 */
d3695aa4 52struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu,
fcbb2ce6
AK
53 unsigned long liobn)
54{
55 struct kvm *kvm = vcpu->kvm;
56 struct kvmppc_spapr_tce_table *stt;
57
366baf28 58 list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
fcbb2ce6
AK
59 if (stt->liobn == liobn)
60 return stt;
61
62 return NULL;
63}
d3695aa4 64EXPORT_SYMBOL_GPL(kvmppc_find_table);
fcbb2ce6
AK
65
66/*
67 * Validates IO address.
68 *
69 * WARNING: This will be called in real-mode on HV KVM and virtual
70 * mode on PR KVM
71 */
5ee7af18 72long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
fcbb2ce6
AK
73 unsigned long ioba, unsigned long npages)
74{
fe26e527
AK
75 unsigned long mask = (1ULL << stt->page_shift) - 1;
76 unsigned long idx = ioba >> stt->page_shift;
fcbb2ce6 77
fe26e527 78 if ((ioba & mask) || (idx + npages > stt->size) || (idx + npages < idx))
fcbb2ce6
AK
79 return H_PARAMETER;
80
81 return H_SUCCESS;
82}
5ee7af18
AK
83EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);
84
85/*
86 * Validates TCE address.
87 * At the moment flags and page mask are validated.
88 * As the host kernel does not access those addresses (just puts them
89 * to the table and user space is supposed to process them), we can skip
90 * checking other things (such as TCE is a guest RAM address or the page
91 * was actually allocated).
92 *
93 * WARNING: This will be called in real-mode on HV KVM and virtual
94 * mode on PR KVM
95 */
96long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
97{
fe26e527
AK
98 unsigned long page_mask = ~((1ULL << stt->page_shift) - 1);
99 unsigned long mask = ~(page_mask | TCE_PCI_WRITE | TCE_PCI_READ);
5ee7af18
AK
100
101 if (tce & mask)
102 return H_PARAMETER;
103
104 return H_SUCCESS;
105}
106EXPORT_SYMBOL_GPL(kvmppc_tce_validate);
107
108/* Note on the use of page_address() in real mode,
109 *
110 * It is safe to use page_address() in real mode on ppc64 because
111 * page_address() is always defined as lowmem_page_address()
112 * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
113 * operation and does not access page struct.
114 *
115 * Theoretically page_address() could be defined different
116 * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
117 * would have to be enabled.
118 * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
119 * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
120 * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
121 * is not expected to be enabled on ppc32, page_address()
122 * is safe for ppc32 as well.
123 *
124 * WARNING: This will be called in real-mode on HV KVM and virtual
125 * mode on PR KVM
126 */
127static u64 *kvmppc_page_address(struct page *page)
128{
129#if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
130#error TODO: fix to avoid page_address() here
131#endif
132 return (u64 *) page_address(page);
133}
134
135/*
136 * Handles TCE requests for emulated devices.
137 * Puts guest TCE values to the table and expects user space to convert them.
138 * Called in both real and virtual modes.
139 * Cannot fail so kvmppc_tce_validate must be called before it.
140 *
141 * WARNING: This will be called in real-mode on HV KVM and virtual
142 * mode on PR KVM
143 */
144void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
145 unsigned long idx, unsigned long tce)
146{
147 struct page *page;
148 u64 *tbl;
149
150 page = stt->pages[idx / TCES_PER_PAGE];
151 tbl = kvmppc_page_address(page);
152
153 tbl[idx % TCES_PER_PAGE] = tce;
154}
155EXPORT_SYMBOL_GPL(kvmppc_tce_put);
fcbb2ce6 156
d3695aa4
AK
157long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
158 unsigned long *ua, unsigned long **prmap)
159{
160 unsigned long gfn = gpa >> PAGE_SHIFT;
161 struct kvm_memory_slot *memslot;
162
163 memslot = search_memslots(kvm_memslots(kvm), gfn);
164 if (!memslot)
165 return -EINVAL;
166
167 *ua = __gfn_to_hva_memslot(memslot, gfn) |
168 (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
169
170#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
171 if (prmap)
172 *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
173#endif
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
178
179#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
54738c09
DG
180long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
181 unsigned long ioba, unsigned long tce)
182{
fcbb2ce6
AK
183 struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
184 long ret;
54738c09
DG
185
186 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
187 /* liobn, ioba, tce); */
188
fcbb2ce6
AK
189 if (!stt)
190 return H_TOO_HARD;
191
192 ret = kvmppc_ioba_validate(stt, ioba, 1);
193 if (ret != H_SUCCESS)
194 return ret;
195
5ee7af18
AK
196 ret = kvmppc_tce_validate(stt, tce);
197 if (ret != H_SUCCESS)
198 return ret;
fcbb2ce6 199
fe26e527 200 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce);
fcbb2ce6
AK
201
202 return H_SUCCESS;
54738c09 203}
066212e0 204EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
69e9fbb2 205
d3695aa4
AK
206static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
207 unsigned long ua, unsigned long *phpa)
208{
209 pte_t *ptep, pte;
210 unsigned shift = 0;
211
212 ptep = __find_linux_pte_or_hugepte(vcpu->arch.pgdir, ua, NULL, &shift);
213 if (!ptep || !pte_present(*ptep))
214 return -ENXIO;
215 pte = *ptep;
216
217 if (!shift)
218 shift = PAGE_SHIFT;
219
220 /* Avoid handling anything potentially complicated in realmode */
221 if (shift > PAGE_SHIFT)
222 return -EAGAIN;
223
224 if (!pte_young(pte))
225 return -EAGAIN;
226
227 *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
228 (ua & ~PAGE_MASK);
229
230 return 0;
231}
232
233long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
234 unsigned long liobn, unsigned long ioba,
235 unsigned long tce_list, unsigned long npages)
236{
237 struct kvmppc_spapr_tce_table *stt;
238 long i, ret = H_SUCCESS;
239 unsigned long tces, entry, ua = 0;
240 unsigned long *rmap = NULL;
241
242 stt = kvmppc_find_table(vcpu, liobn);
243 if (!stt)
244 return H_TOO_HARD;
245
fe26e527 246 entry = ioba >> stt->page_shift;
d3695aa4
AK
247 /*
248 * The spec says that the maximum size of the list is 512 TCEs
249 * so the whole table addressed resides in 4K page
250 */
251 if (npages > 512)
252 return H_PARAMETER;
253
254 if (tce_list & (SZ_4K - 1))
255 return H_PARAMETER;
256
257 ret = kvmppc_ioba_validate(stt, ioba, npages);
258 if (ret != H_SUCCESS)
259 return ret;
260
261 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
262 return H_TOO_HARD;
263
264 rmap = (void *) vmalloc_to_phys(rmap);
265
266 /*
267 * Synchronize with the MMU notifier callbacks in
268 * book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.).
269 * While we have the rmap lock, code running on other CPUs
270 * cannot finish unmapping the host real page that backs
271 * this guest real page, so we are OK to access the host
272 * real page.
273 */
274 lock_rmap(rmap);
275 if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
276 ret = H_TOO_HARD;
277 goto unlock_exit;
278 }
279
280 for (i = 0; i < npages; ++i) {
281 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
282
283 ret = kvmppc_tce_validate(stt, tce);
284 if (ret != H_SUCCESS)
285 goto unlock_exit;
286
287 kvmppc_tce_put(stt, entry + i, tce);
288 }
289
290unlock_exit:
291 unlock_rmap(rmap);
292
293 return ret;
294}
295
296long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
297 unsigned long liobn, unsigned long ioba,
298 unsigned long tce_value, unsigned long npages)
299{
300 struct kvmppc_spapr_tce_table *stt;
301 long i, ret;
302
303 stt = kvmppc_find_table(vcpu, liobn);
304 if (!stt)
305 return H_TOO_HARD;
306
307 ret = kvmppc_ioba_validate(stt, ioba, npages);
308 if (ret != H_SUCCESS)
309 return ret;
310
311 /* Check permission bits only to allow userspace poison TCE for debug */
312 if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
313 return H_PARAMETER;
314
fe26e527
AK
315 for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
316 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
d3695aa4
AK
317
318 return H_SUCCESS;
319}
320EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);
321
69e9fbb2
LD
322long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
323 unsigned long ioba)
324{
fcbb2ce6
AK
325 struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
326 long ret;
327 unsigned long idx;
328 struct page *page;
329 u64 *tbl;
69e9fbb2 330
fcbb2ce6
AK
331 if (!stt)
332 return H_TOO_HARD;
69e9fbb2 333
fcbb2ce6
AK
334 ret = kvmppc_ioba_validate(stt, ioba, 1);
335 if (ret != H_SUCCESS)
336 return ret;
69e9fbb2 337
fe26e527 338 idx = ioba >> stt->page_shift;
fcbb2ce6
AK
339 page = stt->pages[idx / TCES_PER_PAGE];
340 tbl = (u64 *)page_address(page);
69e9fbb2 341
fcbb2ce6 342 vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE];
69e9fbb2 343
fcbb2ce6 344 return H_SUCCESS;
69e9fbb2
LD
345}
346EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
d3695aa4
AK
347
348#endif /* KVM_BOOK3S_HV_POSSIBLE */