kill dentry_update_name_case()
[sfrench/cifs-2.6.git] / arch / powerpc / kvm / book3s_64_vio_hv.c
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>
17  * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
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/book3s/64/mmu-hash.h>
34 #include <asm/mmu_context.h>
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>
40 #include <asm/iommu.h>
41 #include <asm/tce.h>
42 #include <asm/pte-walk.h>
43
44 #ifdef CONFIG_BUG
45
46 #define WARN_ON_ONCE_RM(condition)      ({                      \
47         static bool __section(.data.unlikely) __warned;         \
48         int __ret_warn_once = !!(condition);                    \
49                                                                 \
50         if (unlikely(__ret_warn_once && !__warned)) {           \
51                 __warned = true;                                \
52                 pr_err("WARN_ON_ONCE_RM: (%s) at %s:%u\n",      \
53                                 __stringify(condition),         \
54                                 __func__, __LINE__);            \
55                 dump_stack();                                   \
56         }                                                       \
57         unlikely(__ret_warn_once);                              \
58 })
59
60 #else
61
62 #define WARN_ON_ONCE_RM(condition) ({                           \
63         int __ret_warn_on = !!(condition);                      \
64         unlikely(__ret_warn_on);                                \
65 })
66
67 #endif
68
69 #define TCES_PER_PAGE   (PAGE_SIZE / sizeof(u64))
70
71 /*
72  * Finds a TCE table descriptor by LIOBN.
73  *
74  * WARNING: This will be called in real or virtual mode on HV KVM and virtual
75  *          mode on PR KVM
76  */
77 struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
78                 unsigned long liobn)
79 {
80         struct kvmppc_spapr_tce_table *stt;
81
82         list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
83                 if (stt->liobn == liobn)
84                         return stt;
85
86         return NULL;
87 }
88 EXPORT_SYMBOL_GPL(kvmppc_find_table);
89
90 /*
91  * Validates TCE address.
92  * At the moment flags and page mask are validated.
93  * As the host kernel does not access those addresses (just puts them
94  * to the table and user space is supposed to process them), we can skip
95  * checking other things (such as TCE is a guest RAM address or the page
96  * was actually allocated).
97  *
98  * WARNING: This will be called in real-mode on HV KVM and virtual
99  *          mode on PR KVM
100  */
101 long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
102 {
103         unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
104         enum dma_data_direction dir = iommu_tce_direction(tce);
105
106         /* Allow userspace to poison TCE table */
107         if (dir == DMA_NONE)
108                 return H_SUCCESS;
109
110         if (iommu_tce_check_gpa(stt->page_shift, gpa))
111                 return H_PARAMETER;
112
113         return H_SUCCESS;
114 }
115 EXPORT_SYMBOL_GPL(kvmppc_tce_validate);
116
117 /* Note on the use of page_address() in real mode,
118  *
119  * It is safe to use page_address() in real mode on ppc64 because
120  * page_address() is always defined as lowmem_page_address()
121  * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
122  * operation and does not access page struct.
123  *
124  * Theoretically page_address() could be defined different
125  * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
126  * would have to be enabled.
127  * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
128  * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
129  * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
130  * is not expected to be enabled on ppc32, page_address()
131  * is safe for ppc32 as well.
132  *
133  * WARNING: This will be called in real-mode on HV KVM and virtual
134  *          mode on PR KVM
135  */
136 static u64 *kvmppc_page_address(struct page *page)
137 {
138 #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
139 #error TODO: fix to avoid page_address() here
140 #endif
141         return (u64 *) page_address(page);
142 }
143
144 /*
145  * Handles TCE requests for emulated devices.
146  * Puts guest TCE values to the table and expects user space to convert them.
147  * Called in both real and virtual modes.
148  * Cannot fail so kvmppc_tce_validate must be called before it.
149  *
150  * WARNING: This will be called in real-mode on HV KVM and virtual
151  *          mode on PR KVM
152  */
153 void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
154                 unsigned long idx, unsigned long tce)
155 {
156         struct page *page;
157         u64 *tbl;
158
159         idx -= stt->offset;
160         page = stt->pages[idx / TCES_PER_PAGE];
161         tbl = kvmppc_page_address(page);
162
163         tbl[idx % TCES_PER_PAGE] = tce;
164 }
165 EXPORT_SYMBOL_GPL(kvmppc_tce_put);
166
167 long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
168                 unsigned long *ua, unsigned long **prmap)
169 {
170         unsigned long gfn = gpa >> PAGE_SHIFT;
171         struct kvm_memory_slot *memslot;
172
173         memslot = search_memslots(kvm_memslots(kvm), gfn);
174         if (!memslot)
175                 return -EINVAL;
176
177         *ua = __gfn_to_hva_memslot(memslot, gfn) |
178                 (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
179
180 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
181         if (prmap)
182                 *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
183 #endif
184
185         return 0;
186 }
187 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
188
189 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
190 static void kvmppc_rm_clear_tce(struct iommu_table *tbl, unsigned long entry)
191 {
192         unsigned long hpa = 0;
193         enum dma_data_direction dir = DMA_NONE;
194
195         iommu_tce_xchg_rm(tbl, entry, &hpa, &dir);
196 }
197
198 static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm,
199                 struct iommu_table *tbl, unsigned long entry)
200 {
201         struct mm_iommu_table_group_mem_t *mem = NULL;
202         const unsigned long pgsize = 1ULL << tbl->it_page_shift;
203         unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
204
205         if (!pua)
206                 /* it_userspace allocation might be delayed */
207                 return H_TOO_HARD;
208
209         pua = (void *) vmalloc_to_phys(pua);
210         if (WARN_ON_ONCE_RM(!pua))
211                 return H_HARDWARE;
212
213         mem = mm_iommu_lookup_rm(kvm->mm, *pua, pgsize);
214         if (!mem)
215                 return H_TOO_HARD;
216
217         mm_iommu_mapped_dec(mem);
218
219         *pua = 0;
220
221         return H_SUCCESS;
222 }
223
224 static long kvmppc_rm_tce_iommu_do_unmap(struct kvm *kvm,
225                 struct iommu_table *tbl, unsigned long entry)
226 {
227         enum dma_data_direction dir = DMA_NONE;
228         unsigned long hpa = 0;
229         long ret;
230
231         if (iommu_tce_xchg_rm(tbl, entry, &hpa, &dir))
232                 /*
233                  * real mode xchg can fail if struct page crosses
234                  * a page boundary
235                  */
236                 return H_TOO_HARD;
237
238         if (dir == DMA_NONE)
239                 return H_SUCCESS;
240
241         ret = kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
242         if (ret)
243                 iommu_tce_xchg_rm(tbl, entry, &hpa, &dir);
244
245         return ret;
246 }
247
248 static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm,
249                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
250                 unsigned long entry)
251 {
252         unsigned long i, ret = H_SUCCESS;
253         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
254         unsigned long io_entry = entry * subpages;
255
256         for (i = 0; i < subpages; ++i) {
257                 ret = kvmppc_rm_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
258                 if (ret != H_SUCCESS)
259                         break;
260         }
261
262         return ret;
263 }
264
265 static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
266                 unsigned long entry, unsigned long ua,
267                 enum dma_data_direction dir)
268 {
269         long ret;
270         unsigned long hpa = 0;
271         unsigned long *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
272         struct mm_iommu_table_group_mem_t *mem;
273
274         if (!pua)
275                 /* it_userspace allocation might be delayed */
276                 return H_TOO_HARD;
277
278         mem = mm_iommu_lookup_rm(kvm->mm, ua, 1ULL << tbl->it_page_shift);
279         if (!mem)
280                 return H_TOO_HARD;
281
282         if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, &hpa)))
283                 return H_HARDWARE;
284
285         pua = (void *) vmalloc_to_phys(pua);
286         if (WARN_ON_ONCE_RM(!pua))
287                 return H_HARDWARE;
288
289         if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem)))
290                 return H_CLOSED;
291
292         ret = iommu_tce_xchg_rm(tbl, entry, &hpa, &dir);
293         if (ret) {
294                 mm_iommu_mapped_dec(mem);
295                 /*
296                  * real mode xchg can fail if struct page crosses
297                  * a page boundary
298                  */
299                 return H_TOO_HARD;
300         }
301
302         if (dir != DMA_NONE)
303                 kvmppc_rm_tce_iommu_mapped_dec(kvm, tbl, entry);
304
305         *pua = ua;
306
307         return 0;
308 }
309
310 static long kvmppc_rm_tce_iommu_map(struct kvm *kvm,
311                 struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
312                 unsigned long entry, unsigned long ua,
313                 enum dma_data_direction dir)
314 {
315         unsigned long i, pgoff, ret = H_SUCCESS;
316         unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
317         unsigned long io_entry = entry * subpages;
318
319         for (i = 0, pgoff = 0; i < subpages;
320                         ++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {
321
322                 ret = kvmppc_rm_tce_iommu_do_map(kvm, tbl,
323                                 io_entry + i, ua + pgoff, dir);
324                 if (ret != H_SUCCESS)
325                         break;
326         }
327
328         return ret;
329 }
330
331 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
332                 unsigned long ioba, unsigned long tce)
333 {
334         struct kvmppc_spapr_tce_table *stt;
335         long ret;
336         struct kvmppc_spapr_tce_iommu_table *stit;
337         unsigned long entry, ua = 0;
338         enum dma_data_direction dir;
339
340         /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
341         /*          liobn, ioba, tce); */
342
343         /* For radix, we might be in virtual mode, so punt */
344         if (kvm_is_radix(vcpu->kvm))
345                 return H_TOO_HARD;
346
347         stt = kvmppc_find_table(vcpu->kvm, liobn);
348         if (!stt)
349                 return H_TOO_HARD;
350
351         ret = kvmppc_ioba_validate(stt, ioba, 1);
352         if (ret != H_SUCCESS)
353                 return ret;
354
355         ret = kvmppc_tce_validate(stt, tce);
356         if (ret != H_SUCCESS)
357                 return ret;
358
359         dir = iommu_tce_direction(tce);
360         if ((dir != DMA_NONE) && kvmppc_gpa_to_ua(vcpu->kvm,
361                         tce & ~(TCE_PCI_READ | TCE_PCI_WRITE), &ua, NULL))
362                 return H_PARAMETER;
363
364         entry = ioba >> stt->page_shift;
365
366         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
367                 if (dir == DMA_NONE)
368                         ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
369                                         stit->tbl, entry);
370                 else
371                         ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
372                                         stit->tbl, entry, ua, dir);
373
374                 if (ret == H_SUCCESS)
375                         continue;
376
377                 if (ret == H_TOO_HARD)
378                         return ret;
379
380                 WARN_ON_ONCE_RM(1);
381                 kvmppc_rm_clear_tce(stit->tbl, entry);
382         }
383
384         kvmppc_tce_put(stt, entry, tce);
385
386         return H_SUCCESS;
387 }
388
389 static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
390                 unsigned long ua, unsigned long *phpa)
391 {
392         pte_t *ptep, pte;
393         unsigned shift = 0;
394
395         /*
396          * Called in real mode with MSR_EE = 0. We are safe here.
397          * It is ok to do the lookup with arch.pgdir here, because
398          * we are doing this on secondary cpus and current task there
399          * is not the hypervisor. Also this is safe against THP in the
400          * host, because an IPI to primary thread will wait for the secondary
401          * to exit which will agains result in the below page table walk
402          * to finish.
403          */
404         ptep = __find_linux_pte(vcpu->arch.pgdir, ua, NULL, &shift);
405         if (!ptep || !pte_present(*ptep))
406                 return -ENXIO;
407         pte = *ptep;
408
409         if (!shift)
410                 shift = PAGE_SHIFT;
411
412         /* Avoid handling anything potentially complicated in realmode */
413         if (shift > PAGE_SHIFT)
414                 return -EAGAIN;
415
416         if (!pte_young(pte))
417                 return -EAGAIN;
418
419         *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
420                         (ua & ~PAGE_MASK);
421
422         return 0;
423 }
424
425 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
426                 unsigned long liobn, unsigned long ioba,
427                 unsigned long tce_list, unsigned long npages)
428 {
429         struct kvmppc_spapr_tce_table *stt;
430         long i, ret = H_SUCCESS;
431         unsigned long tces, entry, ua = 0;
432         unsigned long *rmap = NULL;
433         bool prereg = false;
434         struct kvmppc_spapr_tce_iommu_table *stit;
435
436         /* For radix, we might be in virtual mode, so punt */
437         if (kvm_is_radix(vcpu->kvm))
438                 return H_TOO_HARD;
439
440         stt = kvmppc_find_table(vcpu->kvm, liobn);
441         if (!stt)
442                 return H_TOO_HARD;
443
444         entry = ioba >> stt->page_shift;
445         /*
446          * The spec says that the maximum size of the list is 512 TCEs
447          * so the whole table addressed resides in 4K page
448          */
449         if (npages > 512)
450                 return H_PARAMETER;
451
452         if (tce_list & (SZ_4K - 1))
453                 return H_PARAMETER;
454
455         ret = kvmppc_ioba_validate(stt, ioba, npages);
456         if (ret != H_SUCCESS)
457                 return ret;
458
459         if (mm_iommu_preregistered(vcpu->kvm->mm)) {
460                 /*
461                  * We get here if guest memory was pre-registered which
462                  * is normally VFIO case and gpa->hpa translation does not
463                  * depend on hpt.
464                  */
465                 struct mm_iommu_table_group_mem_t *mem;
466
467                 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL))
468                         return H_TOO_HARD;
469
470                 mem = mm_iommu_lookup_rm(vcpu->kvm->mm, ua, IOMMU_PAGE_SIZE_4K);
471                 if (mem)
472                         prereg = mm_iommu_ua_to_hpa_rm(mem, ua, &tces) == 0;
473         }
474
475         if (!prereg) {
476                 /*
477                  * This is usually a case of a guest with emulated devices only
478                  * when TCE list is not in preregistered memory.
479                  * We do not require memory to be preregistered in this case
480                  * so lock rmap and do __find_linux_pte_or_hugepte().
481                  */
482                 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
483                         return H_TOO_HARD;
484
485                 rmap = (void *) vmalloc_to_phys(rmap);
486                 if (WARN_ON_ONCE_RM(!rmap))
487                         return H_HARDWARE;
488
489                 /*
490                  * Synchronize with the MMU notifier callbacks in
491                  * book3s_64_mmu_hv.c (kvm_unmap_hva_range_hv etc.).
492                  * While we have the rmap lock, code running on other CPUs
493                  * cannot finish unmapping the host real page that backs
494                  * this guest real page, so we are OK to access the host
495                  * real page.
496                  */
497                 lock_rmap(rmap);
498                 if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
499                         ret = H_TOO_HARD;
500                         goto unlock_exit;
501                 }
502         }
503
504         for (i = 0; i < npages; ++i) {
505                 unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
506
507                 ret = kvmppc_tce_validate(stt, tce);
508                 if (ret != H_SUCCESS)
509                         goto unlock_exit;
510
511                 ua = 0;
512                 if (kvmppc_gpa_to_ua(vcpu->kvm,
513                                 tce & ~(TCE_PCI_READ | TCE_PCI_WRITE),
514                                 &ua, NULL))
515                         return H_PARAMETER;
516
517                 list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
518                         ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, stt,
519                                         stit->tbl, entry + i, ua,
520                                         iommu_tce_direction(tce));
521
522                         if (ret == H_SUCCESS)
523                                 continue;
524
525                         if (ret == H_TOO_HARD)
526                                 goto unlock_exit;
527
528                         WARN_ON_ONCE_RM(1);
529                         kvmppc_rm_clear_tce(stit->tbl, entry);
530                 }
531
532                 kvmppc_tce_put(stt, entry + i, tce);
533         }
534
535 unlock_exit:
536         if (rmap)
537                 unlock_rmap(rmap);
538
539         return ret;
540 }
541
542 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
543                 unsigned long liobn, unsigned long ioba,
544                 unsigned long tce_value, unsigned long npages)
545 {
546         struct kvmppc_spapr_tce_table *stt;
547         long i, ret;
548         struct kvmppc_spapr_tce_iommu_table *stit;
549
550         /* For radix, we might be in virtual mode, so punt */
551         if (kvm_is_radix(vcpu->kvm))
552                 return H_TOO_HARD;
553
554         stt = kvmppc_find_table(vcpu->kvm, liobn);
555         if (!stt)
556                 return H_TOO_HARD;
557
558         ret = kvmppc_ioba_validate(stt, ioba, npages);
559         if (ret != H_SUCCESS)
560                 return ret;
561
562         /* Check permission bits only to allow userspace poison TCE for debug */
563         if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
564                 return H_PARAMETER;
565
566         list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
567                 unsigned long entry = ioba >> stt->page_shift;
568
569                 for (i = 0; i < npages; ++i) {
570                         ret = kvmppc_rm_tce_iommu_unmap(vcpu->kvm, stt,
571                                         stit->tbl, entry + i);
572
573                         if (ret == H_SUCCESS)
574                                 continue;
575
576                         if (ret == H_TOO_HARD)
577                                 return ret;
578
579                         WARN_ON_ONCE_RM(1);
580                         kvmppc_rm_clear_tce(stit->tbl, entry);
581                 }
582         }
583
584         for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
585                 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
586
587         return H_SUCCESS;
588 }
589
590 /* This can be called in either virtual mode or real mode */
591 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
592                       unsigned long ioba)
593 {
594         struct kvmppc_spapr_tce_table *stt;
595         long ret;
596         unsigned long idx;
597         struct page *page;
598         u64 *tbl;
599
600         stt = kvmppc_find_table(vcpu->kvm, liobn);
601         if (!stt)
602                 return H_TOO_HARD;
603
604         ret = kvmppc_ioba_validate(stt, ioba, 1);
605         if (ret != H_SUCCESS)
606                 return ret;
607
608         idx = (ioba >> stt->page_shift) - stt->offset;
609         page = stt->pages[idx / TCES_PER_PAGE];
610         tbl = (u64 *)page_address(page);
611
612         vcpu->arch.regs.gpr[4] = tbl[idx % TCES_PER_PAGE];
613
614         return H_SUCCESS;
615 }
616 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
617
618 #endif /* KVM_BOOK3S_HV_POSSIBLE */