Merge remote-tracking branch 'lsk/v3.10/topic/mailbox' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / kvm / powerpc.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 IBM Corp. 2007
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19  */
20
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <linux/file.h>
29 #include <asm/cputable.h>
30 #include <asm/uaccess.h>
31 #include <asm/kvm_ppc.h>
32 #include <asm/tlbflush.h>
33 #include <asm/cputhreads.h>
34 #include <asm/irqflags.h>
35 #include "timing.h"
36 #include "irq.h"
37 #include "../mm/mmu_decl.h"
38
39 #define CREATE_TRACE_POINTS
40 #include "trace.h"
41
42 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
43 {
44         return !!(v->arch.pending_exceptions) ||
45                v->requests;
46 }
47
48 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
49 {
50         return 1;
51 }
52
53 #ifndef CONFIG_KVM_BOOK3S_64_HV
54 /*
55  * Common checks before entering the guest world.  Call with interrupts
56  * disabled.
57  *
58  * returns:
59  *
60  * == 1 if we're ready to go into guest state
61  * <= 0 if we need to go back to the host with return value
62  */
63 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
64 {
65         int r = 1;
66
67         WARN_ON_ONCE(!irqs_disabled());
68         while (true) {
69                 if (need_resched()) {
70                         local_irq_enable();
71                         cond_resched();
72                         local_irq_disable();
73                         continue;
74                 }
75
76                 if (signal_pending(current)) {
77                         kvmppc_account_exit(vcpu, SIGNAL_EXITS);
78                         vcpu->run->exit_reason = KVM_EXIT_INTR;
79                         r = -EINTR;
80                         break;
81                 }
82
83                 vcpu->mode = IN_GUEST_MODE;
84
85                 /*
86                  * Reading vcpu->requests must happen after setting vcpu->mode,
87                  * so we don't miss a request because the requester sees
88                  * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
89                  * before next entering the guest (and thus doesn't IPI).
90                  */
91                 smp_mb();
92
93                 if (vcpu->requests) {
94                         /* Make sure we process requests preemptable */
95                         local_irq_enable();
96                         trace_kvm_check_requests(vcpu);
97                         r = kvmppc_core_check_requests(vcpu);
98                         local_irq_disable();
99                         if (r > 0)
100                                 continue;
101                         break;
102                 }
103
104                 if (kvmppc_core_prepare_to_enter(vcpu)) {
105                         /* interrupts got enabled in between, so we
106                            are back at square 1 */
107                         continue;
108                 }
109
110 #ifdef CONFIG_PPC64
111                 /* lazy EE magic */
112                 hard_irq_disable();
113                 if (lazy_irq_pending()) {
114                         /* Got an interrupt in between, try again */
115                         local_irq_enable();
116                         local_irq_disable();
117                         kvm_guest_exit();
118                         continue;
119                 }
120
121                 trace_hardirqs_on();
122 #endif
123
124                 kvm_guest_enter();
125                 break;
126         }
127
128         return r;
129 }
130 #endif /* CONFIG_KVM_BOOK3S_64_HV */
131
132 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
133 {
134         int nr = kvmppc_get_gpr(vcpu, 11);
135         int r;
136         unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
137         unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
138         unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
139         unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
140         unsigned long r2 = 0;
141
142         if (!(vcpu->arch.shared->msr & MSR_SF)) {
143                 /* 32 bit mode */
144                 param1 &= 0xffffffff;
145                 param2 &= 0xffffffff;
146                 param3 &= 0xffffffff;
147                 param4 &= 0xffffffff;
148         }
149
150         switch (nr) {
151         case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
152         {
153                 vcpu->arch.magic_page_pa = param1;
154                 vcpu->arch.magic_page_ea = param2;
155
156                 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
157
158                 r = EV_SUCCESS;
159                 break;
160         }
161         case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
162                 r = EV_SUCCESS;
163 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
164                 /* XXX Missing magic page on 44x */
165                 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
166 #endif
167
168                 /* Second return value is in r4 */
169                 break;
170         case EV_HCALL_TOKEN(EV_IDLE):
171                 r = EV_SUCCESS;
172                 kvm_vcpu_block(vcpu);
173                 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
174                 break;
175         default:
176                 r = EV_UNIMPLEMENTED;
177                 break;
178         }
179
180         kvmppc_set_gpr(vcpu, 4, r2);
181
182         return r;
183 }
184
185 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
186 {
187         int r = false;
188
189         /* We have to know what CPU to virtualize */
190         if (!vcpu->arch.pvr)
191                 goto out;
192
193         /* PAPR only works with book3s_64 */
194         if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
195                 goto out;
196
197 #ifdef CONFIG_KVM_BOOK3S_64_HV
198         /* HV KVM can only do PAPR mode for now */
199         if (!vcpu->arch.papr_enabled)
200                 goto out;
201 #endif
202
203 #ifdef CONFIG_KVM_BOOKE_HV
204         if (!cpu_has_feature(CPU_FTR_EMB_HV))
205                 goto out;
206 #endif
207
208         r = true;
209
210 out:
211         vcpu->arch.sane = r;
212         return r ? 0 : -EINVAL;
213 }
214
215 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
216 {
217         enum emulation_result er;
218         int r;
219
220         er = kvmppc_emulate_instruction(run, vcpu);
221         switch (er) {
222         case EMULATE_DONE:
223                 /* Future optimization: only reload non-volatiles if they were
224                  * actually modified. */
225                 r = RESUME_GUEST_NV;
226                 break;
227         case EMULATE_DO_MMIO:
228                 run->exit_reason = KVM_EXIT_MMIO;
229                 /* We must reload nonvolatiles because "update" load/store
230                  * instructions modify register state. */
231                 /* Future optimization: only reload non-volatiles if they were
232                  * actually modified. */
233                 r = RESUME_HOST_NV;
234                 break;
235         case EMULATE_FAIL:
236                 /* XXX Deliver Program interrupt to guest. */
237                 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
238                        kvmppc_get_last_inst(vcpu));
239                 r = RESUME_HOST;
240                 break;
241         default:
242                 WARN_ON(1);
243                 r = RESUME_GUEST;
244         }
245
246         return r;
247 }
248
249 int kvm_arch_hardware_enable(void)
250 {
251         return 0;
252 }
253
254 int kvm_arch_hardware_setup(void)
255 {
256         return 0;
257 }
258
259 void kvm_arch_check_processor_compat(void *rtn)
260 {
261         *(int *)rtn = kvmppc_core_check_processor_compat();
262 }
263
264 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
265 {
266         if (type)
267                 return -EINVAL;
268
269         return kvmppc_core_init_vm(kvm);
270 }
271
272 void kvm_arch_destroy_vm(struct kvm *kvm)
273 {
274         unsigned int i;
275         struct kvm_vcpu *vcpu;
276
277         kvm_for_each_vcpu(i, vcpu, kvm)
278                 kvm_arch_vcpu_free(vcpu);
279
280         mutex_lock(&kvm->lock);
281         for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
282                 kvm->vcpus[i] = NULL;
283
284         atomic_set(&kvm->online_vcpus, 0);
285
286         kvmppc_core_destroy_vm(kvm);
287
288         mutex_unlock(&kvm->lock);
289 }
290
291 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
292 {
293         int r;
294
295         switch (ext) {
296 #ifdef CONFIG_BOOKE
297         case KVM_CAP_PPC_BOOKE_SREGS:
298         case KVM_CAP_PPC_BOOKE_WATCHDOG:
299         case KVM_CAP_PPC_EPR:
300 #else
301         case KVM_CAP_PPC_SEGSTATE:
302         case KVM_CAP_PPC_HIOR:
303         case KVM_CAP_PPC_PAPR:
304 #endif
305         case KVM_CAP_PPC_UNSET_IRQ:
306         case KVM_CAP_PPC_IRQ_LEVEL:
307         case KVM_CAP_ENABLE_CAP:
308         case KVM_CAP_ONE_REG:
309         case KVM_CAP_IOEVENTFD:
310         case KVM_CAP_DEVICE_CTRL:
311                 r = 1;
312                 break;
313 #ifndef CONFIG_KVM_BOOK3S_64_HV
314         case KVM_CAP_PPC_PAIRED_SINGLES:
315         case KVM_CAP_PPC_OSI:
316         case KVM_CAP_PPC_GET_PVINFO:
317 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
318         case KVM_CAP_SW_TLB:
319 #endif
320 #ifdef CONFIG_KVM_MPIC
321         case KVM_CAP_IRQ_MPIC:
322 #endif
323                 r = 1;
324                 break;
325         case KVM_CAP_COALESCED_MMIO:
326                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
327                 break;
328 #endif
329 #ifdef CONFIG_PPC_BOOK3S_64
330         case KVM_CAP_SPAPR_TCE:
331         case KVM_CAP_PPC_ALLOC_HTAB:
332         case KVM_CAP_PPC_RTAS:
333 #ifdef CONFIG_KVM_XICS
334         case KVM_CAP_IRQ_XICS:
335 #endif
336                 r = 1;
337                 break;
338 #endif /* CONFIG_PPC_BOOK3S_64 */
339 #ifdef CONFIG_KVM_BOOK3S_64_HV
340         case KVM_CAP_PPC_SMT:
341                 r = threads_per_core;
342                 break;
343         case KVM_CAP_PPC_RMA:
344                 r = 1;
345                 /* PPC970 requires an RMA */
346                 if (cpu_has_feature(CPU_FTR_ARCH_201))
347                         r = 2;
348                 break;
349 #endif
350         case KVM_CAP_SYNC_MMU:
351 #ifdef CONFIG_KVM_BOOK3S_64_HV
352                 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
353 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
354                 r = 1;
355 #else
356                 r = 0;
357                 break;
358 #endif
359 #ifdef CONFIG_KVM_BOOK3S_64_HV
360         case KVM_CAP_PPC_HTAB_FD:
361                 r = 1;
362                 break;
363 #endif
364                 break;
365         case KVM_CAP_NR_VCPUS:
366                 /*
367                  * Recommending a number of CPUs is somewhat arbitrary; we
368                  * return the number of present CPUs for -HV (since a host
369                  * will have secondary threads "offline"), and for other KVM
370                  * implementations just count online CPUs.
371                  */
372 #ifdef CONFIG_KVM_BOOK3S_64_HV
373                 r = num_present_cpus();
374 #else
375                 r = num_online_cpus();
376 #endif
377                 break;
378         case KVM_CAP_MAX_VCPUS:
379                 r = KVM_MAX_VCPUS;
380                 break;
381 #ifdef CONFIG_PPC_BOOK3S_64
382         case KVM_CAP_PPC_GET_SMMU_INFO:
383                 r = 1;
384                 break;
385 #endif
386         default:
387                 r = 0;
388                 break;
389         }
390         return r;
391
392 }
393
394 long kvm_arch_dev_ioctl(struct file *filp,
395                         unsigned int ioctl, unsigned long arg)
396 {
397         return -EINVAL;
398 }
399
400 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
401                            struct kvm_memory_slot *dont)
402 {
403         kvmppc_core_free_memslot(kvm, free, dont);
404 }
405
406 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
407                             unsigned long npages)
408 {
409         return kvmppc_core_create_memslot(kvm, slot, npages);
410 }
411
412 int kvm_arch_prepare_memory_region(struct kvm *kvm,
413                                    struct kvm_memory_slot *memslot,
414                                    struct kvm_userspace_memory_region *mem,
415                                    enum kvm_mr_change change)
416 {
417         return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
418 }
419
420 void kvm_arch_commit_memory_region(struct kvm *kvm,
421                                    struct kvm_userspace_memory_region *mem,
422                                    const struct kvm_memory_slot *old,
423                                    enum kvm_mr_change change)
424 {
425         kvmppc_core_commit_memory_region(kvm, mem, old);
426 }
427
428 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
429                                    struct kvm_memory_slot *slot)
430 {
431         kvmppc_core_flush_memslot(kvm, slot);
432 }
433
434 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
435 {
436         struct kvm_vcpu *vcpu;
437         vcpu = kvmppc_core_vcpu_create(kvm, id);
438         if (!IS_ERR(vcpu)) {
439                 vcpu->arch.wqp = &vcpu->wq;
440                 kvmppc_create_vcpu_debugfs(vcpu, id);
441         }
442         return vcpu;
443 }
444
445 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
446 {
447         return 0;
448 }
449
450 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
451 {
452         /* Make sure we're not using the vcpu anymore */
453         hrtimer_cancel(&vcpu->arch.dec_timer);
454         tasklet_kill(&vcpu->arch.tasklet);
455
456         kvmppc_remove_vcpu_debugfs(vcpu);
457
458         switch (vcpu->arch.irq_type) {
459         case KVMPPC_IRQ_MPIC:
460                 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
461                 break;
462         case KVMPPC_IRQ_XICS:
463                 kvmppc_xics_free_icp(vcpu);
464                 break;
465         }
466
467         kvmppc_core_vcpu_free(vcpu);
468 }
469
470 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
471 {
472         kvm_arch_vcpu_free(vcpu);
473 }
474
475 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
476 {
477         return kvmppc_core_pending_dec(vcpu);
478 }
479
480 /*
481  * low level hrtimer wake routine. Because this runs in hardirq context
482  * we schedule a tasklet to do the real work.
483  */
484 enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
485 {
486         struct kvm_vcpu *vcpu;
487
488         vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
489         tasklet_schedule(&vcpu->arch.tasklet);
490
491         return HRTIMER_NORESTART;
492 }
493
494 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
495 {
496         int ret;
497
498         hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
499         tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
500         vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
501         vcpu->arch.dec_expires = ~(u64)0;
502
503 #ifdef CONFIG_KVM_EXIT_TIMING
504         mutex_init(&vcpu->arch.exit_timing_lock);
505 #endif
506         ret = kvmppc_subarch_vcpu_init(vcpu);
507         return ret;
508 }
509
510 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
511 {
512         kvmppc_mmu_destroy(vcpu);
513         kvmppc_subarch_vcpu_uninit(vcpu);
514 }
515
516 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
517 {
518 #ifdef CONFIG_BOOKE
519         /*
520          * vrsave (formerly usprg0) isn't used by Linux, but may
521          * be used by the guest.
522          *
523          * On non-booke this is associated with Altivec and
524          * is handled by code in book3s.c.
525          */
526         mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
527 #endif
528         kvmppc_core_vcpu_load(vcpu, cpu);
529 }
530
531 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
532 {
533         kvmppc_core_vcpu_put(vcpu);
534 #ifdef CONFIG_BOOKE
535         vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
536 #endif
537 }
538
539 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
540                                      struct kvm_run *run)
541 {
542         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
543 }
544
545 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
546                                       struct kvm_run *run)
547 {
548         u64 uninitialized_var(gpr);
549
550         if (run->mmio.len > sizeof(gpr)) {
551                 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
552                 return;
553         }
554
555         if (vcpu->arch.mmio_is_bigendian) {
556                 switch (run->mmio.len) {
557                 case 8: gpr = *(u64 *)run->mmio.data; break;
558                 case 4: gpr = *(u32 *)run->mmio.data; break;
559                 case 2: gpr = *(u16 *)run->mmio.data; break;
560                 case 1: gpr = *(u8 *)run->mmio.data; break;
561                 }
562         } else {
563                 /* Convert BE data from userland back to LE. */
564                 switch (run->mmio.len) {
565                 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
566                 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
567                 case 1: gpr = *(u8 *)run->mmio.data; break;
568                 }
569         }
570
571         if (vcpu->arch.mmio_sign_extend) {
572                 switch (run->mmio.len) {
573 #ifdef CONFIG_PPC64
574                 case 4:
575                         gpr = (s64)(s32)gpr;
576                         break;
577 #endif
578                 case 2:
579                         gpr = (s64)(s16)gpr;
580                         break;
581                 case 1:
582                         gpr = (s64)(s8)gpr;
583                         break;
584                 }
585         }
586
587         kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
588
589         switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
590         case KVM_MMIO_REG_GPR:
591                 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
592                 break;
593         case KVM_MMIO_REG_FPR:
594                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
595                 break;
596 #ifdef CONFIG_PPC_BOOK3S
597         case KVM_MMIO_REG_QPR:
598                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
599                 break;
600         case KVM_MMIO_REG_FQPR:
601                 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
602                 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
603                 break;
604 #endif
605         default:
606                 BUG();
607         }
608 }
609
610 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
611                        unsigned int rt, unsigned int bytes, int is_bigendian)
612 {
613         int idx, ret;
614
615         if (bytes > sizeof(run->mmio.data)) {
616                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
617                        run->mmio.len);
618         }
619
620         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
621         run->mmio.len = bytes;
622         run->mmio.is_write = 0;
623
624         vcpu->arch.io_gpr = rt;
625         vcpu->arch.mmio_is_bigendian = is_bigendian;
626         vcpu->mmio_needed = 1;
627         vcpu->mmio_is_write = 0;
628         vcpu->arch.mmio_sign_extend = 0;
629
630         idx = srcu_read_lock(&vcpu->kvm->srcu);
631
632         ret = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
633                               bytes, &run->mmio.data);
634
635         srcu_read_unlock(&vcpu->kvm->srcu, idx);
636
637         if (!ret) {
638                 kvmppc_complete_mmio_load(vcpu, run);
639                 vcpu->mmio_needed = 0;
640                 return EMULATE_DONE;
641         }
642
643         return EMULATE_DO_MMIO;
644 }
645
646 /* Same as above, but sign extends */
647 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
648                         unsigned int rt, unsigned int bytes, int is_bigendian)
649 {
650         int r;
651
652         vcpu->arch.mmio_sign_extend = 1;
653         r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
654
655         return r;
656 }
657
658 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
659                         u64 val, unsigned int bytes, int is_bigendian)
660 {
661         void *data = run->mmio.data;
662         int idx, ret;
663
664         if (bytes > sizeof(run->mmio.data)) {
665                 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
666                        run->mmio.len);
667         }
668
669         run->mmio.phys_addr = vcpu->arch.paddr_accessed;
670         run->mmio.len = bytes;
671         run->mmio.is_write = 1;
672         vcpu->mmio_needed = 1;
673         vcpu->mmio_is_write = 1;
674
675         /* Store the value at the lowest bytes in 'data'. */
676         if (is_bigendian) {
677                 switch (bytes) {
678                 case 8: *(u64 *)data = val; break;
679                 case 4: *(u32 *)data = val; break;
680                 case 2: *(u16 *)data = val; break;
681                 case 1: *(u8  *)data = val; break;
682                 }
683         } else {
684                 /* Store LE value into 'data'. */
685                 switch (bytes) {
686                 case 4: st_le32(data, val); break;
687                 case 2: st_le16(data, val); break;
688                 case 1: *(u8 *)data = val; break;
689                 }
690         }
691
692         idx = srcu_read_lock(&vcpu->kvm->srcu);
693
694         ret = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
695                                bytes, &run->mmio.data);
696
697         srcu_read_unlock(&vcpu->kvm->srcu, idx);
698
699         if (!ret) {
700                 vcpu->mmio_needed = 0;
701                 return EMULATE_DONE;
702         }
703
704         return EMULATE_DO_MMIO;
705 }
706
707 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
708 {
709         int r;
710         sigset_t sigsaved;
711
712         if (vcpu->sigset_active)
713                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
714
715         if (vcpu->mmio_needed) {
716                 if (!vcpu->mmio_is_write)
717                         kvmppc_complete_mmio_load(vcpu, run);
718                 vcpu->mmio_needed = 0;
719         } else if (vcpu->arch.dcr_needed) {
720                 if (!vcpu->arch.dcr_is_write)
721                         kvmppc_complete_dcr_load(vcpu, run);
722                 vcpu->arch.dcr_needed = 0;
723         } else if (vcpu->arch.osi_needed) {
724                 u64 *gprs = run->osi.gprs;
725                 int i;
726
727                 for (i = 0; i < 32; i++)
728                         kvmppc_set_gpr(vcpu, i, gprs[i]);
729                 vcpu->arch.osi_needed = 0;
730         } else if (vcpu->arch.hcall_needed) {
731                 int i;
732
733                 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
734                 for (i = 0; i < 9; ++i)
735                         kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
736                 vcpu->arch.hcall_needed = 0;
737 #ifdef CONFIG_BOOKE
738         } else if (vcpu->arch.epr_needed) {
739                 kvmppc_set_epr(vcpu, run->epr.epr);
740                 vcpu->arch.epr_needed = 0;
741 #endif
742         }
743
744         r = kvmppc_vcpu_run(run, vcpu);
745
746         if (vcpu->sigset_active)
747                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
748
749         return r;
750 }
751
752 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
753 {
754         if (irq->irq == KVM_INTERRUPT_UNSET) {
755                 kvmppc_core_dequeue_external(vcpu);
756                 return 0;
757         }
758
759         kvmppc_core_queue_external(vcpu, irq);
760
761         kvm_vcpu_kick(vcpu);
762
763         return 0;
764 }
765
766 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
767                                      struct kvm_enable_cap *cap)
768 {
769         int r;
770
771         if (cap->flags)
772                 return -EINVAL;
773
774         switch (cap->cap) {
775         case KVM_CAP_PPC_OSI:
776                 r = 0;
777                 vcpu->arch.osi_enabled = true;
778                 break;
779         case KVM_CAP_PPC_PAPR:
780                 r = 0;
781                 vcpu->arch.papr_enabled = true;
782                 break;
783         case KVM_CAP_PPC_EPR:
784                 r = 0;
785                 if (cap->args[0])
786                         vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
787                 else
788                         vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
789                 break;
790 #ifdef CONFIG_BOOKE
791         case KVM_CAP_PPC_BOOKE_WATCHDOG:
792                 r = 0;
793                 vcpu->arch.watchdog_enabled = true;
794                 break;
795 #endif
796 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
797         case KVM_CAP_SW_TLB: {
798                 struct kvm_config_tlb cfg;
799                 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
800
801                 r = -EFAULT;
802                 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
803                         break;
804
805                 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
806                 break;
807         }
808 #endif
809 #ifdef CONFIG_KVM_MPIC
810         case KVM_CAP_IRQ_MPIC: {
811                 struct file *filp;
812                 struct kvm_device *dev;
813
814                 r = -EBADF;
815                 filp = fget(cap->args[0]);
816                 if (!filp)
817                         break;
818
819                 r = -EPERM;
820                 dev = kvm_device_from_filp(filp);
821                 if (dev)
822                         r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
823
824                 fput(filp);
825                 break;
826         }
827 #endif
828 #ifdef CONFIG_KVM_XICS
829         case KVM_CAP_IRQ_XICS: {
830                 struct file *filp;
831                 struct kvm_device *dev;
832
833                 r = -EBADF;
834                 filp = fget(cap->args[0]);
835                 if (!filp)
836                         break;
837
838                 r = -EPERM;
839                 dev = kvm_device_from_filp(filp);
840                 if (dev)
841                         r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
842
843                 fput(filp);
844                 break;
845         }
846 #endif /* CONFIG_KVM_XICS */
847         default:
848                 r = -EINVAL;
849                 break;
850         }
851
852         if (!r)
853                 r = kvmppc_sanity_check(vcpu);
854
855         return r;
856 }
857
858 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
859                                     struct kvm_mp_state *mp_state)
860 {
861         return -EINVAL;
862 }
863
864 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
865                                     struct kvm_mp_state *mp_state)
866 {
867         return -EINVAL;
868 }
869
870 long kvm_arch_vcpu_ioctl(struct file *filp,
871                          unsigned int ioctl, unsigned long arg)
872 {
873         struct kvm_vcpu *vcpu = filp->private_data;
874         void __user *argp = (void __user *)arg;
875         long r;
876
877         switch (ioctl) {
878         case KVM_INTERRUPT: {
879                 struct kvm_interrupt irq;
880                 r = -EFAULT;
881                 if (copy_from_user(&irq, argp, sizeof(irq)))
882                         goto out;
883                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
884                 goto out;
885         }
886
887         case KVM_ENABLE_CAP:
888         {
889                 struct kvm_enable_cap cap;
890                 r = -EFAULT;
891                 if (copy_from_user(&cap, argp, sizeof(cap)))
892                         goto out;
893                 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
894                 break;
895         }
896
897         case KVM_SET_ONE_REG:
898         case KVM_GET_ONE_REG:
899         {
900                 struct kvm_one_reg reg;
901                 r = -EFAULT;
902                 if (copy_from_user(&reg, argp, sizeof(reg)))
903                         goto out;
904                 if (ioctl == KVM_SET_ONE_REG)
905                         r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
906                 else
907                         r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
908                 break;
909         }
910
911 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
912         case KVM_DIRTY_TLB: {
913                 struct kvm_dirty_tlb dirty;
914                 r = -EFAULT;
915                 if (copy_from_user(&dirty, argp, sizeof(dirty)))
916                         goto out;
917                 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
918                 break;
919         }
920 #endif
921         default:
922                 r = -EINVAL;
923         }
924
925 out:
926         return r;
927 }
928
929 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
930 {
931         return VM_FAULT_SIGBUS;
932 }
933
934 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
935 {
936         u32 inst_nop = 0x60000000;
937 #ifdef CONFIG_KVM_BOOKE_HV
938         u32 inst_sc1 = 0x44000022;
939         pvinfo->hcall[0] = inst_sc1;
940         pvinfo->hcall[1] = inst_nop;
941         pvinfo->hcall[2] = inst_nop;
942         pvinfo->hcall[3] = inst_nop;
943 #else
944         u32 inst_lis = 0x3c000000;
945         u32 inst_ori = 0x60000000;
946         u32 inst_sc = 0x44000002;
947         u32 inst_imm_mask = 0xffff;
948
949         /*
950          * The hypercall to get into KVM from within guest context is as
951          * follows:
952          *
953          *    lis r0, r0, KVM_SC_MAGIC_R0@h
954          *    ori r0, KVM_SC_MAGIC_R0@l
955          *    sc
956          *    nop
957          */
958         pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
959         pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
960         pvinfo->hcall[2] = inst_sc;
961         pvinfo->hcall[3] = inst_nop;
962 #endif
963
964         pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
965
966         return 0;
967 }
968
969 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
970                           bool line_status)
971 {
972         if (!irqchip_in_kernel(kvm))
973                 return -ENXIO;
974
975         irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
976                                         irq_event->irq, irq_event->level,
977                                         line_status);
978         return 0;
979 }
980
981 long kvm_arch_vm_ioctl(struct file *filp,
982                        unsigned int ioctl, unsigned long arg)
983 {
984         struct kvm *kvm __maybe_unused = filp->private_data;
985         void __user *argp = (void __user *)arg;
986         long r;
987
988         switch (ioctl) {
989         case KVM_PPC_GET_PVINFO: {
990                 struct kvm_ppc_pvinfo pvinfo;
991                 memset(&pvinfo, 0, sizeof(pvinfo));
992                 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
993                 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
994                         r = -EFAULT;
995                         goto out;
996                 }
997
998                 break;
999         }
1000 #ifdef CONFIG_PPC_BOOK3S_64
1001         case KVM_CREATE_SPAPR_TCE: {
1002                 struct kvm_create_spapr_tce create_tce;
1003
1004                 r = -EFAULT;
1005                 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
1006                         goto out;
1007                 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
1008                 goto out;
1009         }
1010 #endif /* CONFIG_PPC_BOOK3S_64 */
1011
1012 #ifdef CONFIG_KVM_BOOK3S_64_HV
1013         case KVM_ALLOCATE_RMA: {
1014                 struct kvm_allocate_rma rma;
1015                 struct kvm *kvm = filp->private_data;
1016
1017                 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
1018                 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
1019                         r = -EFAULT;
1020                 break;
1021         }
1022
1023         case KVM_PPC_ALLOCATE_HTAB: {
1024                 u32 htab_order;
1025
1026                 r = -EFAULT;
1027                 if (get_user(htab_order, (u32 __user *)argp))
1028                         break;
1029                 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
1030                 if (r)
1031                         break;
1032                 r = -EFAULT;
1033                 if (put_user(htab_order, (u32 __user *)argp))
1034                         break;
1035                 r = 0;
1036                 break;
1037         }
1038
1039         case KVM_PPC_GET_HTAB_FD: {
1040                 struct kvm_get_htab_fd ghf;
1041
1042                 r = -EFAULT;
1043                 if (copy_from_user(&ghf, argp, sizeof(ghf)))
1044                         break;
1045                 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
1046                 break;
1047         }
1048 #endif /* CONFIG_KVM_BOOK3S_64_HV */
1049
1050 #ifdef CONFIG_PPC_BOOK3S_64
1051         case KVM_PPC_GET_SMMU_INFO: {
1052                 struct kvm_ppc_smmu_info info;
1053
1054                 memset(&info, 0, sizeof(info));
1055                 r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
1056                 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1057                         r = -EFAULT;
1058                 break;
1059         }
1060         case KVM_PPC_RTAS_DEFINE_TOKEN: {
1061                 struct kvm *kvm = filp->private_data;
1062
1063                 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
1064                 break;
1065         }
1066 #endif /* CONFIG_PPC_BOOK3S_64 */
1067         default:
1068                 r = -ENOTTY;
1069         }
1070
1071 out:
1072         return r;
1073 }
1074
1075 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
1076 static unsigned long nr_lpids;
1077
1078 long kvmppc_alloc_lpid(void)
1079 {
1080         long lpid;
1081
1082         do {
1083                 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
1084                 if (lpid >= nr_lpids) {
1085                         pr_err("%s: No LPIDs free\n", __func__);
1086                         return -ENOMEM;
1087                 }
1088         } while (test_and_set_bit(lpid, lpid_inuse));
1089
1090         return lpid;
1091 }
1092
1093 void kvmppc_claim_lpid(long lpid)
1094 {
1095         set_bit(lpid, lpid_inuse);
1096 }
1097
1098 void kvmppc_free_lpid(long lpid)
1099 {
1100         clear_bit(lpid, lpid_inuse);
1101 }
1102
1103 void kvmppc_init_lpid(unsigned long nr_lpids_param)
1104 {
1105         nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
1106         memset(lpid_inuse, 0, sizeof(lpid_inuse));
1107 }
1108
1109 int kvm_arch_init(void *opaque)
1110 {
1111         return 0;
1112 }