]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kvm/book3s_hv.c
powerpc: kvm: optimize "sc 1" as fast return
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kvm / book3s_hv.c
CommitLineData
de56a948
PM
1/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
66b15db6 27#include <linux/export.h>
de56a948
PM
28#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#include <linux/cpumask.h>
aa04b4cc
PM
31#include <linux/spinlock.h>
32#include <linux/page-flags.h>
2c9097e4 33#include <linux/srcu.h>
de56a948
PM
34
35#include <asm/reg.h>
36#include <asm/cputable.h>
37#include <asm/cacheflush.h>
38#include <asm/tlbflush.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41#include <asm/kvm_ppc.h>
42#include <asm/kvm_book3s.h>
43#include <asm/mmu_context.h>
44#include <asm/lppaca.h>
45#include <asm/processor.h>
371fefd6 46#include <asm/cputhreads.h>
aa04b4cc 47#include <asm/page.h>
de1d9248 48#include <asm/hvcall.h>
ae3a197e 49#include <asm/switch_to.h>
512691d4 50#include <asm/smp.h>
de56a948 51#include <linux/gfp.h>
de56a948
PM
52#include <linux/vmalloc.h>
53#include <linux/highmem.h>
c77162de 54#include <linux/hugetlb.h>
2ba9f0d8 55#include <linux/module.h>
de56a948 56
3a167bea
AK
57#include "book3s.h"
58
de56a948
PM
59/* #define EXIT_DEBUG */
60/* #define EXIT_DEBUG_SIMPLE */
61/* #define EXIT_DEBUG_INT */
62
913d3ff9
PM
63/* Used to indicate that a guest page fault needs to be handled */
64#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
65
c7b67670
PM
66/* Used as a "null" value for timebase values */
67#define TB_NIL (~(u64)0)
68
19ccb76a 69static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
32fad281 70static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
19ccb76a 71
3a167bea 72static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
54695c30
BH
73{
74 int me;
75 int cpu = vcpu->cpu;
76 wait_queue_head_t *wqp;
77
78 wqp = kvm_arch_vcpu_wq(vcpu);
79 if (waitqueue_active(wqp)) {
80 wake_up_interruptible(wqp);
81 ++vcpu->stat.halt_wakeup;
82 }
83
84 me = get_cpu();
85
86 /* CPU points to the first thread of the core */
87 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
88 int real_cpu = cpu + vcpu->arch.ptid;
89 if (paca[real_cpu].kvm_hstate.xics_phys)
90 xics_wake_cpu(real_cpu);
91 else if (cpu_online(cpu))
92 smp_send_reschedule(cpu);
93 }
94 put_cpu();
95}
96
c7b67670
PM
97/*
98 * We use the vcpu_load/put functions to measure stolen time.
99 * Stolen time is counted as time when either the vcpu is able to
100 * run as part of a virtual core, but the task running the vcore
101 * is preempted or sleeping, or when the vcpu needs something done
102 * in the kernel by the task running the vcpu, but that task is
103 * preempted or sleeping. Those two things have to be counted
104 * separately, since one of the vcpu tasks will take on the job
105 * of running the core, and the other vcpu tasks in the vcore will
106 * sleep waiting for it to do that, but that sleep shouldn't count
107 * as stolen time.
108 *
109 * Hence we accumulate stolen time when the vcpu can run as part of
110 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
111 * needs its task to do other things in the kernel (for example,
112 * service a page fault) in busy_stolen. We don't accumulate
113 * stolen time for a vcore when it is inactive, or for a vcpu
114 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
115 * a misnomer; it means that the vcpu task is not executing in
116 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
117 * the kernel. We don't have any way of dividing up that time
118 * between time that the vcpu is genuinely stopped, time that
119 * the task is actively working on behalf of the vcpu, and time
120 * that the task is preempted, so we don't count any of it as
121 * stolen.
122 *
123 * Updates to busy_stolen are protected by arch.tbacct_lock;
124 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
125 * of the vcpu that has taken responsibility for running the vcore
126 * (i.e. vc->runner). The stolen times are measured in units of
127 * timebase ticks. (Note that the != TB_NIL checks below are
128 * purely defensive; they should never fail.)
129 */
130
3a167bea 131static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
de56a948 132{
0456ec4f
PM
133 struct kvmppc_vcore *vc = vcpu->arch.vcore;
134
c7b67670
PM
135 spin_lock(&vcpu->arch.tbacct_lock);
136 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
137 vc->preempt_tb != TB_NIL) {
0456ec4f 138 vc->stolen_tb += mftb() - vc->preempt_tb;
c7b67670
PM
139 vc->preempt_tb = TB_NIL;
140 }
141 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
142 vcpu->arch.busy_preempt != TB_NIL) {
143 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
144 vcpu->arch.busy_preempt = TB_NIL;
145 }
146 spin_unlock(&vcpu->arch.tbacct_lock);
de56a948
PM
147}
148
3a167bea 149static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
de56a948 150{
0456ec4f
PM
151 struct kvmppc_vcore *vc = vcpu->arch.vcore;
152
c7b67670 153 spin_lock(&vcpu->arch.tbacct_lock);
0456ec4f
PM
154 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
155 vc->preempt_tb = mftb();
c7b67670
PM
156 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
157 vcpu->arch.busy_preempt = mftb();
158 spin_unlock(&vcpu->arch.tbacct_lock);
de56a948
PM
159}
160
3a167bea 161static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
de56a948
PM
162{
163 vcpu->arch.shregs.msr = msr;
19ccb76a 164 kvmppc_end_cede(vcpu);
de56a948
PM
165}
166
3a167bea 167void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
de56a948
PM
168{
169 vcpu->arch.pvr = pvr;
170}
171
388cc6e1
PM
172int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
173{
174 unsigned long pcr = 0;
175 struct kvmppc_vcore *vc = vcpu->arch.vcore;
176
177 if (arch_compat) {
178 if (!cpu_has_feature(CPU_FTR_ARCH_206))
179 return -EINVAL; /* 970 has no compat mode support */
180
181 switch (arch_compat) {
182 case PVR_ARCH_205:
183 pcr = PCR_ARCH_205;
184 break;
185 case PVR_ARCH_206:
186 case PVR_ARCH_206p:
187 break;
188 default:
189 return -EINVAL;
190 }
191 }
192
193 spin_lock(&vc->lock);
194 vc->arch_compat = arch_compat;
195 vc->pcr = pcr;
196 spin_unlock(&vc->lock);
197
198 return 0;
199}
200
de56a948
PM
201void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
202{
203 int r;
204
205 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
206 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
207 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
208 for (r = 0; r < 16; ++r)
209 pr_err("r%2d = %.16lx r%d = %.16lx\n",
210 r, kvmppc_get_gpr(vcpu, r),
211 r+16, kvmppc_get_gpr(vcpu, r+16));
212 pr_err("ctr = %.16lx lr = %.16lx\n",
213 vcpu->arch.ctr, vcpu->arch.lr);
214 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
215 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
216 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
217 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
218 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
219 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
220 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
221 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
222 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
223 pr_err("fault dar = %.16lx dsisr = %.8x\n",
224 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
225 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
226 for (r = 0; r < vcpu->arch.slb_max; ++r)
227 pr_err(" ESID = %.16llx VSID = %.16llx\n",
228 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
229 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
a0144e2a 230 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
de56a948
PM
231 vcpu->arch.last_inst);
232}
233
a8606e20
PM
234struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
235{
236 int r;
237 struct kvm_vcpu *v, *ret = NULL;
238
239 mutex_lock(&kvm->lock);
240 kvm_for_each_vcpu(r, v, kvm) {
241 if (v->vcpu_id == id) {
242 ret = v;
243 break;
244 }
245 }
246 mutex_unlock(&kvm->lock);
247 return ret;
248}
249
250static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
251{
f13c13a0 252 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
a8606e20
PM
253 vpa->yield_count = 1;
254}
255
55b665b0
PM
256static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
257 unsigned long addr, unsigned long len)
258{
259 /* check address is cacheline aligned */
260 if (addr & (L1_CACHE_BYTES - 1))
261 return -EINVAL;
262 spin_lock(&vcpu->arch.vpa_update_lock);
263 if (v->next_gpa != addr || v->len != len) {
264 v->next_gpa = addr;
265 v->len = addr ? len : 0;
266 v->update_pending = 1;
267 }
268 spin_unlock(&vcpu->arch.vpa_update_lock);
269 return 0;
270}
271
2e25aa5f
PM
272/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
273struct reg_vpa {
274 u32 dummy;
275 union {
276 u16 hword;
277 u32 word;
278 } length;
279};
280
281static int vpa_is_registered(struct kvmppc_vpa *vpap)
282{
283 if (vpap->update_pending)
284 return vpap->next_gpa != 0;
285 return vpap->pinned_addr != NULL;
286}
287
a8606e20
PM
288static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
289 unsigned long flags,
290 unsigned long vcpuid, unsigned long vpa)
291{
292 struct kvm *kvm = vcpu->kvm;
93e60249 293 unsigned long len, nb;
a8606e20
PM
294 void *va;
295 struct kvm_vcpu *tvcpu;
2e25aa5f
PM
296 int err;
297 int subfunc;
298 struct kvmppc_vpa *vpap;
a8606e20
PM
299
300 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
301 if (!tvcpu)
302 return H_PARAMETER;
303
2e25aa5f
PM
304 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
305 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
306 subfunc == H_VPA_REG_SLB) {
307 /* Registering new area - address must be cache-line aligned */
308 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
a8606e20 309 return H_PARAMETER;
2e25aa5f
PM
310
311 /* convert logical addr to kernel addr and read length */
93e60249
PM
312 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
313 if (va == NULL)
b2b2f165 314 return H_PARAMETER;
2e25aa5f
PM
315 if (subfunc == H_VPA_REG_VPA)
316 len = ((struct reg_vpa *)va)->length.hword;
a8606e20 317 else
2e25aa5f 318 len = ((struct reg_vpa *)va)->length.word;
c35635ef 319 kvmppc_unpin_guest_page(kvm, va, vpa, false);
2e25aa5f
PM
320
321 /* Check length */
322 if (len > nb || len < sizeof(struct reg_vpa))
323 return H_PARAMETER;
324 } else {
325 vpa = 0;
326 len = 0;
327 }
328
329 err = H_PARAMETER;
330 vpap = NULL;
331 spin_lock(&tvcpu->arch.vpa_update_lock);
332
333 switch (subfunc) {
334 case H_VPA_REG_VPA: /* register VPA */
335 if (len < sizeof(struct lppaca))
a8606e20 336 break;
2e25aa5f
PM
337 vpap = &tvcpu->arch.vpa;
338 err = 0;
339 break;
340
341 case H_VPA_REG_DTL: /* register DTL */
342 if (len < sizeof(struct dtl_entry))
a8606e20 343 break;
2e25aa5f
PM
344 len -= len % sizeof(struct dtl_entry);
345
346 /* Check that they have previously registered a VPA */
347 err = H_RESOURCE;
348 if (!vpa_is_registered(&tvcpu->arch.vpa))
a8606e20 349 break;
2e25aa5f
PM
350
351 vpap = &tvcpu->arch.dtl;
352 err = 0;
353 break;
354
355 case H_VPA_REG_SLB: /* register SLB shadow buffer */
356 /* Check that they have previously registered a VPA */
357 err = H_RESOURCE;
358 if (!vpa_is_registered(&tvcpu->arch.vpa))
a8606e20 359 break;
2e25aa5f
PM
360
361 vpap = &tvcpu->arch.slb_shadow;
362 err = 0;
363 break;
364
365 case H_VPA_DEREG_VPA: /* deregister VPA */
366 /* Check they don't still have a DTL or SLB buf registered */
367 err = H_RESOURCE;
368 if (vpa_is_registered(&tvcpu->arch.dtl) ||
369 vpa_is_registered(&tvcpu->arch.slb_shadow))
a8606e20 370 break;
2e25aa5f
PM
371
372 vpap = &tvcpu->arch.vpa;
373 err = 0;
374 break;
375
376 case H_VPA_DEREG_DTL: /* deregister DTL */
377 vpap = &tvcpu->arch.dtl;
378 err = 0;
379 break;
380
381 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
382 vpap = &tvcpu->arch.slb_shadow;
383 err = 0;
384 break;
385 }
386
387 if (vpap) {
388 vpap->next_gpa = vpa;
389 vpap->len = len;
390 vpap->update_pending = 1;
a8606e20 391 }
93e60249 392
2e25aa5f
PM
393 spin_unlock(&tvcpu->arch.vpa_update_lock);
394
93e60249 395 return err;
a8606e20
PM
396}
397
081f323b 398static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
2e25aa5f 399{
081f323b 400 struct kvm *kvm = vcpu->kvm;
2e25aa5f
PM
401 void *va;
402 unsigned long nb;
081f323b 403 unsigned long gpa;
2e25aa5f 404
081f323b
PM
405 /*
406 * We need to pin the page pointed to by vpap->next_gpa,
407 * but we can't call kvmppc_pin_guest_page under the lock
408 * as it does get_user_pages() and down_read(). So we
409 * have to drop the lock, pin the page, then get the lock
410 * again and check that a new area didn't get registered
411 * in the meantime.
412 */
413 for (;;) {
414 gpa = vpap->next_gpa;
415 spin_unlock(&vcpu->arch.vpa_update_lock);
416 va = NULL;
417 nb = 0;
418 if (gpa)
c35635ef 419 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
081f323b
PM
420 spin_lock(&vcpu->arch.vpa_update_lock);
421 if (gpa == vpap->next_gpa)
422 break;
423 /* sigh... unpin that one and try again */
424 if (va)
c35635ef 425 kvmppc_unpin_guest_page(kvm, va, gpa, false);
081f323b
PM
426 }
427
428 vpap->update_pending = 0;
429 if (va && nb < vpap->len) {
430 /*
431 * If it's now too short, it must be that userspace
432 * has changed the mappings underlying guest memory,
433 * so unregister the region.
434 */
c35635ef 435 kvmppc_unpin_guest_page(kvm, va, gpa, false);
081f323b 436 va = NULL;
2e25aa5f
PM
437 }
438 if (vpap->pinned_addr)
c35635ef
PM
439 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
440 vpap->dirty);
441 vpap->gpa = gpa;
2e25aa5f 442 vpap->pinned_addr = va;
c35635ef 443 vpap->dirty = false;
2e25aa5f
PM
444 if (va)
445 vpap->pinned_end = va + vpap->len;
446}
447
448static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
449{
2f12f034
PM
450 if (!(vcpu->arch.vpa.update_pending ||
451 vcpu->arch.slb_shadow.update_pending ||
452 vcpu->arch.dtl.update_pending))
453 return;
454
2e25aa5f
PM
455 spin_lock(&vcpu->arch.vpa_update_lock);
456 if (vcpu->arch.vpa.update_pending) {
081f323b 457 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
55b665b0
PM
458 if (vcpu->arch.vpa.pinned_addr)
459 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
2e25aa5f
PM
460 }
461 if (vcpu->arch.dtl.update_pending) {
081f323b 462 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
2e25aa5f
PM
463 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
464 vcpu->arch.dtl_index = 0;
465 }
466 if (vcpu->arch.slb_shadow.update_pending)
081f323b 467 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
2e25aa5f
PM
468 spin_unlock(&vcpu->arch.vpa_update_lock);
469}
470
c7b67670
PM
471/*
472 * Return the accumulated stolen time for the vcore up until `now'.
473 * The caller should hold the vcore lock.
474 */
475static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
476{
477 u64 p;
478
479 /*
480 * If we are the task running the vcore, then since we hold
481 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
482 * can't be updated, so we don't need the tbacct_lock.
483 * If the vcore is inactive, it can't become active (since we
484 * hold the vcore lock), so the vcpu load/put functions won't
485 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
486 */
487 if (vc->vcore_state != VCORE_INACTIVE &&
488 vc->runner->arch.run_task != current) {
489 spin_lock(&vc->runner->arch.tbacct_lock);
490 p = vc->stolen_tb;
491 if (vc->preempt_tb != TB_NIL)
492 p += now - vc->preempt_tb;
493 spin_unlock(&vc->runner->arch.tbacct_lock);
494 } else {
495 p = vc->stolen_tb;
496 }
497 return p;
498}
499
0456ec4f
PM
500static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
501 struct kvmppc_vcore *vc)
502{
503 struct dtl_entry *dt;
504 struct lppaca *vpa;
c7b67670
PM
505 unsigned long stolen;
506 unsigned long core_stolen;
507 u64 now;
0456ec4f
PM
508
509 dt = vcpu->arch.dtl_ptr;
510 vpa = vcpu->arch.vpa.pinned_addr;
c7b67670
PM
511 now = mftb();
512 core_stolen = vcore_stolen_time(vc, now);
513 stolen = core_stolen - vcpu->arch.stolen_logged;
514 vcpu->arch.stolen_logged = core_stolen;
515 spin_lock(&vcpu->arch.tbacct_lock);
516 stolen += vcpu->arch.busy_stolen;
517 vcpu->arch.busy_stolen = 0;
518 spin_unlock(&vcpu->arch.tbacct_lock);
0456ec4f
PM
519 if (!dt || !vpa)
520 return;
521 memset(dt, 0, sizeof(struct dtl_entry));
522 dt->dispatch_reason = 7;
523 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
93b0f4dc 524 dt->timebase = now + vc->tb_offset;
c7b67670 525 dt->enqueue_to_dispatch_time = stolen;
0456ec4f
PM
526 dt->srr0 = kvmppc_get_pc(vcpu);
527 dt->srr1 = vcpu->arch.shregs.msr;
528 ++dt;
529 if (dt == vcpu->arch.dtl.pinned_end)
530 dt = vcpu->arch.dtl.pinned_addr;
531 vcpu->arch.dtl_ptr = dt;
532 /* order writing *dt vs. writing vpa->dtl_idx */
533 smp_wmb();
534 vpa->dtl_idx = ++vcpu->arch.dtl_index;
c35635ef 535 vcpu->arch.dtl.dirty = true;
0456ec4f
PM
536}
537
a8606e20
PM
538int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
539{
540 unsigned long req = kvmppc_get_gpr(vcpu, 3);
541 unsigned long target, ret = H_SUCCESS;
542 struct kvm_vcpu *tvcpu;
8e591cb7 543 int idx, rc;
a8606e20
PM
544
545 switch (req) {
c77162de 546 case H_ENTER:
2c9097e4 547 idx = srcu_read_lock(&vcpu->kvm->srcu);
c77162de
PM
548 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
549 kvmppc_get_gpr(vcpu, 5),
550 kvmppc_get_gpr(vcpu, 6),
551 kvmppc_get_gpr(vcpu, 7));
2c9097e4 552 srcu_read_unlock(&vcpu->kvm->srcu, idx);
c77162de 553 break;
a8606e20 554 case H_CEDE:
a8606e20
PM
555 break;
556 case H_PROD:
557 target = kvmppc_get_gpr(vcpu, 4);
558 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
559 if (!tvcpu) {
560 ret = H_PARAMETER;
561 break;
562 }
563 tvcpu->arch.prodded = 1;
564 smp_mb();
565 if (vcpu->arch.ceded) {
566 if (waitqueue_active(&vcpu->wq)) {
567 wake_up_interruptible(&vcpu->wq);
568 vcpu->stat.halt_wakeup++;
569 }
570 }
571 break;
572 case H_CONFER:
42d7604d
PM
573 target = kvmppc_get_gpr(vcpu, 4);
574 if (target == -1)
575 break;
576 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
577 if (!tvcpu) {
578 ret = H_PARAMETER;
579 break;
580 }
581 kvm_vcpu_yield_to(tvcpu);
a8606e20
PM
582 break;
583 case H_REGISTER_VPA:
584 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
585 kvmppc_get_gpr(vcpu, 5),
586 kvmppc_get_gpr(vcpu, 6));
587 break;
8e591cb7
ME
588 case H_RTAS:
589 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
590 return RESUME_HOST;
591
592 rc = kvmppc_rtas_hcall(vcpu);
593
594 if (rc == -ENOENT)
595 return RESUME_HOST;
596 else if (rc == 0)
597 break;
598
599 /* Send the error out to userspace via KVM_RUN */
600 return rc;
bc5ad3f3
BH
601
602 case H_XIRR:
603 case H_CPPR:
604 case H_EOI:
605 case H_IPI:
8e44ddc3
PM
606 case H_IPOLL:
607 case H_XIRR_X:
bc5ad3f3
BH
608 if (kvmppc_xics_enabled(vcpu)) {
609 ret = kvmppc_xics_hcall(vcpu, req);
610 break;
611 } /* fallthrough */
a8606e20
PM
612 default:
613 return RESUME_HOST;
614 }
615 kvmppc_set_gpr(vcpu, 3, ret);
616 vcpu->arch.hcall_needed = 0;
617 return RESUME_GUEST;
618}
619
3a167bea
AK
620static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
621 struct task_struct *tsk)
de56a948
PM
622{
623 int r = RESUME_HOST;
624
625 vcpu->stat.sum_exits++;
626
627 run->exit_reason = KVM_EXIT_UNKNOWN;
628 run->ready_for_interrupt_injection = 1;
629 switch (vcpu->arch.trap) {
630 /* We're good on these - the host merely wanted to get our attention */
631 case BOOK3S_INTERRUPT_HV_DECREMENTER:
632 vcpu->stat.dec_exits++;
633 r = RESUME_GUEST;
634 break;
635 case BOOK3S_INTERRUPT_EXTERNAL:
636 vcpu->stat.ext_intr_exits++;
637 r = RESUME_GUEST;
638 break;
639 case BOOK3S_INTERRUPT_PERFMON:
640 r = RESUME_GUEST;
641 break;
b4072df4
PM
642 case BOOK3S_INTERRUPT_MACHINE_CHECK:
643 /*
644 * Deliver a machine check interrupt to the guest.
645 * We have to do this, even if the host has handled the
646 * machine check, because machine checks use SRR0/1 and
647 * the interrupt might have trashed guest state in them.
648 */
649 kvmppc_book3s_queue_irqprio(vcpu,
650 BOOK3S_INTERRUPT_MACHINE_CHECK);
651 r = RESUME_GUEST;
652 break;
de56a948
PM
653 case BOOK3S_INTERRUPT_PROGRAM:
654 {
655 ulong flags;
656 /*
657 * Normally program interrupts are delivered directly
658 * to the guest by the hardware, but we can get here
659 * as a result of a hypervisor emulation interrupt
660 * (e40) getting turned into a 700 by BML RTAS.
661 */
662 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
663 kvmppc_core_queue_program(vcpu, flags);
664 r = RESUME_GUEST;
665 break;
666 }
667 case BOOK3S_INTERRUPT_SYSCALL:
668 {
669 /* hcall - punt to userspace */
670 int i;
671
27025a60
LPF
672 /* hypercall with MSR_PR has already been handled in rmode,
673 * and never reaches here.
674 */
675
de56a948
PM
676 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
677 for (i = 0; i < 9; ++i)
678 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
679 run->exit_reason = KVM_EXIT_PAPR_HCALL;
680 vcpu->arch.hcall_needed = 1;
681 r = RESUME_HOST;
682 break;
683 }
684 /*
342d3db7
PM
685 * We get these next two if the guest accesses a page which it thinks
686 * it has mapped but which is not actually present, either because
687 * it is for an emulated I/O device or because the corresonding
688 * host page has been paged out. Any other HDSI/HISI interrupts
689 * have been handled already.
de56a948
PM
690 */
691 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
913d3ff9 692 r = RESUME_PAGE_FAULT;
de56a948
PM
693 break;
694 case BOOK3S_INTERRUPT_H_INST_STORAGE:
913d3ff9
PM
695 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
696 vcpu->arch.fault_dsisr = 0;
697 r = RESUME_PAGE_FAULT;
de56a948
PM
698 break;
699 /*
700 * This occurs if the guest executes an illegal instruction.
701 * We just generate a program interrupt to the guest, since
702 * we don't emulate any guest instructions at this stage.
703 */
704 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
705 kvmppc_core_queue_program(vcpu, 0x80000);
706 r = RESUME_GUEST;
707 break;
708 default:
709 kvmppc_dump_regs(vcpu);
710 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
711 vcpu->arch.trap, kvmppc_get_pc(vcpu),
712 vcpu->arch.shregs.msr);
f3271d4c 713 run->hw.hardware_exit_reason = vcpu->arch.trap;
de56a948 714 r = RESUME_HOST;
de56a948
PM
715 break;
716 }
717
de56a948
PM
718 return r;
719}
720
3a167bea
AK
721static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
722 struct kvm_sregs *sregs)
de56a948
PM
723{
724 int i;
725
de56a948 726 memset(sregs, 0, sizeof(struct kvm_sregs));
87916442 727 sregs->pvr = vcpu->arch.pvr;
de56a948
PM
728 for (i = 0; i < vcpu->arch.slb_max; i++) {
729 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
730 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
731 }
732
733 return 0;
734}
735
3a167bea
AK
736static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
737 struct kvm_sregs *sregs)
de56a948
PM
738{
739 int i, j;
740
3a167bea 741 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
de56a948
PM
742
743 j = 0;
744 for (i = 0; i < vcpu->arch.slb_nr; i++) {
745 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
746 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
747 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
748 ++j;
749 }
750 }
751 vcpu->arch.slb_max = j;
752
753 return 0;
754}
755
a0144e2a
PM
756static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
757{
758 struct kvmppc_vcore *vc = vcpu->arch.vcore;
759 u64 mask;
760
761 spin_lock(&vc->lock);
762 /*
763 * Userspace can only modify DPFD (default prefetch depth),
764 * ILE (interrupt little-endian) and TC (translation control).
765 */
766 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
767 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
768 spin_unlock(&vc->lock);
769}
770
3a167bea
AK
771static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
772 union kvmppc_one_reg *val)
31f3438e 773{
a136a8bd
PM
774 int r = 0;
775 long int i;
31f3438e 776
a136a8bd 777 switch (id) {
31f3438e 778 case KVM_REG_PPC_HIOR:
a136a8bd
PM
779 *val = get_reg_val(id, 0);
780 break;
781 case KVM_REG_PPC_DABR:
782 *val = get_reg_val(id, vcpu->arch.dabr);
783 break;
784 case KVM_REG_PPC_DSCR:
785 *val = get_reg_val(id, vcpu->arch.dscr);
786 break;
787 case KVM_REG_PPC_PURR:
788 *val = get_reg_val(id, vcpu->arch.purr);
789 break;
790 case KVM_REG_PPC_SPURR:
791 *val = get_reg_val(id, vcpu->arch.spurr);
792 break;
793 case KVM_REG_PPC_AMR:
794 *val = get_reg_val(id, vcpu->arch.amr);
795 break;
796 case KVM_REG_PPC_UAMOR:
797 *val = get_reg_val(id, vcpu->arch.uamor);
798 break;
799 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
800 i = id - KVM_REG_PPC_MMCR0;
801 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
802 break;
803 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
804 i = id - KVM_REG_PPC_PMC1;
805 *val = get_reg_val(id, vcpu->arch.pmc[i]);
31f3438e 806 break;
14941789
PM
807 case KVM_REG_PPC_SIAR:
808 *val = get_reg_val(id, vcpu->arch.siar);
809 break;
810 case KVM_REG_PPC_SDAR:
811 *val = get_reg_val(id, vcpu->arch.sdar);
812 break;
a8bd19ef
PM
813#ifdef CONFIG_VSX
814 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
815 if (cpu_has_feature(CPU_FTR_VSX)) {
816 /* VSX => FP reg i is stored in arch.vsr[2*i] */
817 long int i = id - KVM_REG_PPC_FPR0;
818 *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
819 } else {
820 /* let generic code handle it */
821 r = -EINVAL;
822 }
823 break;
824 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
825 if (cpu_has_feature(CPU_FTR_VSX)) {
826 long int i = id - KVM_REG_PPC_VSR0;
827 val->vsxval[0] = vcpu->arch.vsr[2 * i];
828 val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
829 } else {
830 r = -ENXIO;
831 }
832 break;
833#endif /* CONFIG_VSX */
55b665b0
PM
834 case KVM_REG_PPC_VPA_ADDR:
835 spin_lock(&vcpu->arch.vpa_update_lock);
836 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
837 spin_unlock(&vcpu->arch.vpa_update_lock);
838 break;
839 case KVM_REG_PPC_VPA_SLB:
840 spin_lock(&vcpu->arch.vpa_update_lock);
841 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
842 val->vpaval.length = vcpu->arch.slb_shadow.len;
843 spin_unlock(&vcpu->arch.vpa_update_lock);
844 break;
845 case KVM_REG_PPC_VPA_DTL:
846 spin_lock(&vcpu->arch.vpa_update_lock);
847 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
848 val->vpaval.length = vcpu->arch.dtl.len;
849 spin_unlock(&vcpu->arch.vpa_update_lock);
850 break;
93b0f4dc
PM
851 case KVM_REG_PPC_TB_OFFSET:
852 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
853 break;
a0144e2a
PM
854 case KVM_REG_PPC_LPCR:
855 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
856 break;
4b8473c9
PM
857 case KVM_REG_PPC_PPR:
858 *val = get_reg_val(id, vcpu->arch.ppr);
859 break;
388cc6e1
PM
860 case KVM_REG_PPC_ARCH_COMPAT:
861 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
862 break;
31f3438e 863 default:
a136a8bd 864 r = -EINVAL;
31f3438e
PM
865 break;
866 }
867
868 return r;
869}
870
3a167bea
AK
871static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
872 union kvmppc_one_reg *val)
31f3438e 873{
a136a8bd
PM
874 int r = 0;
875 long int i;
55b665b0 876 unsigned long addr, len;
31f3438e 877
a136a8bd 878 switch (id) {
31f3438e 879 case KVM_REG_PPC_HIOR:
31f3438e 880 /* Only allow this to be set to zero */
a136a8bd 881 if (set_reg_val(id, *val))
31f3438e
PM
882 r = -EINVAL;
883 break;
a136a8bd
PM
884 case KVM_REG_PPC_DABR:
885 vcpu->arch.dabr = set_reg_val(id, *val);
886 break;
887 case KVM_REG_PPC_DSCR:
888 vcpu->arch.dscr = set_reg_val(id, *val);
889 break;
890 case KVM_REG_PPC_PURR:
891 vcpu->arch.purr = set_reg_val(id, *val);
892 break;
893 case KVM_REG_PPC_SPURR:
894 vcpu->arch.spurr = set_reg_val(id, *val);
895 break;
896 case KVM_REG_PPC_AMR:
897 vcpu->arch.amr = set_reg_val(id, *val);
898 break;
899 case KVM_REG_PPC_UAMOR:
900 vcpu->arch.uamor = set_reg_val(id, *val);
901 break;
902 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
903 i = id - KVM_REG_PPC_MMCR0;
904 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
905 break;
906 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
907 i = id - KVM_REG_PPC_PMC1;
908 vcpu->arch.pmc[i] = set_reg_val(id, *val);
909 break;
14941789
PM
910 case KVM_REG_PPC_SIAR:
911 vcpu->arch.siar = set_reg_val(id, *val);
912 break;
913 case KVM_REG_PPC_SDAR:
914 vcpu->arch.sdar = set_reg_val(id, *val);
915 break;
a8bd19ef
PM
916#ifdef CONFIG_VSX
917 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
918 if (cpu_has_feature(CPU_FTR_VSX)) {
919 /* VSX => FP reg i is stored in arch.vsr[2*i] */
920 long int i = id - KVM_REG_PPC_FPR0;
921 vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
922 } else {
923 /* let generic code handle it */
924 r = -EINVAL;
925 }
926 break;
927 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
928 if (cpu_has_feature(CPU_FTR_VSX)) {
929 long int i = id - KVM_REG_PPC_VSR0;
930 vcpu->arch.vsr[2 * i] = val->vsxval[0];
931 vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
932 } else {
933 r = -ENXIO;
934 }
935 break;
936#endif /* CONFIG_VSX */
55b665b0
PM
937 case KVM_REG_PPC_VPA_ADDR:
938 addr = set_reg_val(id, *val);
939 r = -EINVAL;
940 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
941 vcpu->arch.dtl.next_gpa))
942 break;
943 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
944 break;
945 case KVM_REG_PPC_VPA_SLB:
946 addr = val->vpaval.addr;
947 len = val->vpaval.length;
948 r = -EINVAL;
949 if (addr && !vcpu->arch.vpa.next_gpa)
950 break;
951 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
952 break;
953 case KVM_REG_PPC_VPA_DTL:
954 addr = val->vpaval.addr;
955 len = val->vpaval.length;
956 r = -EINVAL;
9f8c8c78
PM
957 if (addr && (len < sizeof(struct dtl_entry) ||
958 !vcpu->arch.vpa.next_gpa))
55b665b0
PM
959 break;
960 len -= len % sizeof(struct dtl_entry);
961 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
962 break;
93b0f4dc
PM
963 case KVM_REG_PPC_TB_OFFSET:
964 /* round up to multiple of 2^24 */
965 vcpu->arch.vcore->tb_offset =
966 ALIGN(set_reg_val(id, *val), 1UL << 24);
967 break;
a0144e2a
PM
968 case KVM_REG_PPC_LPCR:
969 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
970 break;
4b8473c9
PM
971 case KVM_REG_PPC_PPR:
972 vcpu->arch.ppr = set_reg_val(id, *val);
973 break;
388cc6e1
PM
974 case KVM_REG_PPC_ARCH_COMPAT:
975 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
976 break;
31f3438e 977 default:
a136a8bd 978 r = -EINVAL;
31f3438e
PM
979 break;
980 }
981
982 return r;
983}
984
3a167bea
AK
985static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
986 unsigned int id)
de56a948
PM
987{
988 struct kvm_vcpu *vcpu;
371fefd6
PM
989 int err = -EINVAL;
990 int core;
991 struct kvmppc_vcore *vcore;
de56a948 992
371fefd6
PM
993 core = id / threads_per_core;
994 if (core >= KVM_MAX_VCORES)
995 goto out;
996
997 err = -ENOMEM;
6b75e6bf 998 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
de56a948
PM
999 if (!vcpu)
1000 goto out;
1001
1002 err = kvm_vcpu_init(vcpu, kvm, id);
1003 if (err)
1004 goto free_vcpu;
1005
1006 vcpu->arch.shared = &vcpu->arch.shregs;
de56a948
PM
1007 vcpu->arch.mmcr[0] = MMCR0_FC;
1008 vcpu->arch.ctrl = CTRL_RUNLATCH;
1009 /* default to host PVR, since we can't spoof it */
3a167bea 1010 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
2e25aa5f 1011 spin_lock_init(&vcpu->arch.vpa_update_lock);
c7b67670
PM
1012 spin_lock_init(&vcpu->arch.tbacct_lock);
1013 vcpu->arch.busy_preempt = TB_NIL;
de56a948 1014
de56a948
PM
1015 kvmppc_mmu_book3s_hv_init(vcpu);
1016
8455d79e 1017 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
371fefd6
PM
1018
1019 init_waitqueue_head(&vcpu->arch.cpu_run);
1020
1021 mutex_lock(&kvm->lock);
1022 vcore = kvm->arch.vcores[core];
1023 if (!vcore) {
1024 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1025 if (vcore) {
1026 INIT_LIST_HEAD(&vcore->runnable_threads);
1027 spin_lock_init(&vcore->lock);
19ccb76a 1028 init_waitqueue_head(&vcore->wq);
c7b67670 1029 vcore->preempt_tb = TB_NIL;
a0144e2a 1030 vcore->lpcr = kvm->arch.lpcr;
371fefd6
PM
1031 }
1032 kvm->arch.vcores[core] = vcore;
1b400ba0 1033 kvm->arch.online_vcores++;
371fefd6
PM
1034 }
1035 mutex_unlock(&kvm->lock);
1036
1037 if (!vcore)
1038 goto free_vcpu;
1039
1040 spin_lock(&vcore->lock);
1041 ++vcore->num_threads;
371fefd6
PM
1042 spin_unlock(&vcore->lock);
1043 vcpu->arch.vcore = vcore;
1044
af8f38b3
AG
1045 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1046 kvmppc_sanity_check(vcpu);
1047
de56a948
PM
1048 return vcpu;
1049
1050free_vcpu:
6b75e6bf 1051 kmem_cache_free(kvm_vcpu_cache, vcpu);
de56a948
PM
1052out:
1053 return ERR_PTR(err);
1054}
1055
c35635ef
PM
1056static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1057{
1058 if (vpa->pinned_addr)
1059 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1060 vpa->dirty);
1061}
1062
3a167bea 1063static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
de56a948 1064{
2e25aa5f 1065 spin_lock(&vcpu->arch.vpa_update_lock);
c35635ef
PM
1066 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1067 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1068 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
2e25aa5f 1069 spin_unlock(&vcpu->arch.vpa_update_lock);
de56a948 1070 kvm_vcpu_uninit(vcpu);
6b75e6bf 1071 kmem_cache_free(kvm_vcpu_cache, vcpu);
de56a948
PM
1072}
1073
3a167bea
AK
1074static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1075{
1076 /* Indicate we want to get back into the guest */
1077 return 1;
1078}
1079
19ccb76a 1080static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
371fefd6 1081{
19ccb76a 1082 unsigned long dec_nsec, now;
371fefd6 1083
19ccb76a
PM
1084 now = get_tb();
1085 if (now > vcpu->arch.dec_expires) {
1086 /* decrementer has already gone negative */
1087 kvmppc_core_queue_dec(vcpu);
7e28e60e 1088 kvmppc_core_prepare_to_enter(vcpu);
19ccb76a 1089 return;
371fefd6 1090 }
19ccb76a
PM
1091 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1092 / tb_ticks_per_sec;
1093 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1094 HRTIMER_MODE_REL);
1095 vcpu->arch.timer_running = 1;
371fefd6
PM
1096}
1097
19ccb76a 1098static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
371fefd6 1099{
19ccb76a
PM
1100 vcpu->arch.ceded = 0;
1101 if (vcpu->arch.timer_running) {
1102 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1103 vcpu->arch.timer_running = 0;
1104 }
371fefd6
PM
1105}
1106
de56a948
PM
1107extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1108
371fefd6
PM
1109static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1110 struct kvm_vcpu *vcpu)
de56a948 1111{
c7b67670
PM
1112 u64 now;
1113
371fefd6
PM
1114 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1115 return;
c7b67670
PM
1116 spin_lock(&vcpu->arch.tbacct_lock);
1117 now = mftb();
1118 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1119 vcpu->arch.stolen_logged;
1120 vcpu->arch.busy_preempt = now;
1121 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1122 spin_unlock(&vcpu->arch.tbacct_lock);
371fefd6 1123 --vc->n_runnable;
371fefd6
PM
1124 list_del(&vcpu->arch.run_list);
1125}
1126
f0888f70
PM
1127static int kvmppc_grab_hwthread(int cpu)
1128{
1129 struct paca_struct *tpaca;
1130 long timeout = 1000;
1131
1132 tpaca = &paca[cpu];
1133
1134 /* Ensure the thread won't go into the kernel if it wakes */
1135 tpaca->kvm_hstate.hwthread_req = 1;
7b444c67 1136 tpaca->kvm_hstate.kvm_vcpu = NULL;
f0888f70
PM
1137
1138 /*
1139 * If the thread is already executing in the kernel (e.g. handling
1140 * a stray interrupt), wait for it to get back to nap mode.
1141 * The smp_mb() is to ensure that our setting of hwthread_req
1142 * is visible before we look at hwthread_state, so if this
1143 * races with the code at system_reset_pSeries and the thread
1144 * misses our setting of hwthread_req, we are sure to see its
1145 * setting of hwthread_state, and vice versa.
1146 */
1147 smp_mb();
1148 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1149 if (--timeout <= 0) {
1150 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1151 return -EBUSY;
1152 }
1153 udelay(1);
1154 }
1155 return 0;
1156}
1157
1158static void kvmppc_release_hwthread(int cpu)
1159{
1160 struct paca_struct *tpaca;
1161
1162 tpaca = &paca[cpu];
1163 tpaca->kvm_hstate.hwthread_req = 0;
1164 tpaca->kvm_hstate.kvm_vcpu = NULL;
1165}
1166
371fefd6
PM
1167static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1168{
1169 int cpu;
1170 struct paca_struct *tpaca;
1171 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1172
19ccb76a
PM
1173 if (vcpu->arch.timer_running) {
1174 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1175 vcpu->arch.timer_running = 0;
1176 }
371fefd6
PM
1177 cpu = vc->pcpu + vcpu->arch.ptid;
1178 tpaca = &paca[cpu];
1179 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1180 tpaca->kvm_hstate.kvm_vcore = vc;
19ccb76a
PM
1181 tpaca->kvm_hstate.napping = 0;
1182 vcpu->cpu = vc->pcpu;
371fefd6 1183 smp_wmb();
251da038 1184#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
371fefd6 1185 if (vcpu->arch.ptid) {
371fefd6
PM
1186 xics_wake_cpu(cpu);
1187 ++vc->n_woken;
de56a948 1188 }
371fefd6
PM
1189#endif
1190}
de56a948 1191
371fefd6
PM
1192static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1193{
1194 int i;
1195
1196 HMT_low();
1197 i = 0;
1198 while (vc->nap_count < vc->n_woken) {
1199 if (++i >= 1000000) {
1200 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1201 vc->nap_count, vc->n_woken);
1202 break;
1203 }
1204 cpu_relax();
1205 }
1206 HMT_medium();
1207}
1208
1209/*
1210 * Check that we are on thread 0 and that any other threads in
7b444c67
PM
1211 * this core are off-line. Then grab the threads so they can't
1212 * enter the kernel.
371fefd6
PM
1213 */
1214static int on_primary_thread(void)
1215{
1216 int cpu = smp_processor_id();
1217 int thr = cpu_thread_in_core(cpu);
1218
1219 if (thr)
1220 return 0;
1221 while (++thr < threads_per_core)
1222 if (cpu_online(cpu + thr))
1223 return 0;
7b444c67
PM
1224
1225 /* Grab all hw threads so they can't go into the kernel */
1226 for (thr = 1; thr < threads_per_core; ++thr) {
1227 if (kvmppc_grab_hwthread(cpu + thr)) {
1228 /* Couldn't grab one; let the others go */
1229 do {
1230 kvmppc_release_hwthread(cpu + thr);
1231 } while (--thr > 0);
1232 return 0;
1233 }
1234 }
371fefd6
PM
1235 return 1;
1236}
1237
1238/*
1239 * Run a set of guest threads on a physical core.
1240 * Called with vc->lock held.
1241 */
913d3ff9 1242static void kvmppc_run_core(struct kvmppc_vcore *vc)
371fefd6 1243{
19ccb76a 1244 struct kvm_vcpu *vcpu, *vcpu0, *vnext;
371fefd6
PM
1245 long ret;
1246 u64 now;
081f323b 1247 int ptid, i, need_vpa_update;
2c9097e4 1248 int srcu_idx;
913d3ff9 1249 struct kvm_vcpu *vcpus_to_update[threads_per_core];
371fefd6
PM
1250
1251 /* don't start if any threads have a signal pending */
081f323b
PM
1252 need_vpa_update = 0;
1253 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
371fefd6 1254 if (signal_pending(vcpu->arch.run_task))
913d3ff9
PM
1255 return;
1256 if (vcpu->arch.vpa.update_pending ||
1257 vcpu->arch.slb_shadow.update_pending ||
1258 vcpu->arch.dtl.update_pending)
1259 vcpus_to_update[need_vpa_update++] = vcpu;
081f323b
PM
1260 }
1261
1262 /*
1263 * Initialize *vc, in particular vc->vcore_state, so we can
1264 * drop the vcore lock if necessary.
1265 */
1266 vc->n_woken = 0;
1267 vc->nap_count = 0;
1268 vc->entry_exit_count = 0;
2f12f034 1269 vc->vcore_state = VCORE_STARTING;
081f323b
PM
1270 vc->in_guest = 0;
1271 vc->napping_threads = 0;
1272
1273 /*
1274 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1275 * which can't be called with any spinlocks held.
1276 */
1277 if (need_vpa_update) {
1278 spin_unlock(&vc->lock);
913d3ff9
PM
1279 for (i = 0; i < need_vpa_update; ++i)
1280 kvmppc_update_vpas(vcpus_to_update[i]);
081f323b
PM
1281 spin_lock(&vc->lock);
1282 }
de56a948 1283
19ccb76a
PM
1284 /*
1285 * Assign physical thread IDs, first to non-ceded vcpus
1286 * and then to ceded ones.
1287 */
1288 ptid = 0;
1289 vcpu0 = NULL;
1290 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1291 if (!vcpu->arch.ceded) {
1292 if (!ptid)
1293 vcpu0 = vcpu;
1294 vcpu->arch.ptid = ptid++;
1295 }
1296 }
c7b67670
PM
1297 if (!vcpu0)
1298 goto out; /* nothing to run; should never happen */
19ccb76a
PM
1299 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1300 if (vcpu->arch.ceded)
1301 vcpu->arch.ptid = ptid++;
1302
7b444c67
PM
1303 /*
1304 * Make sure we are running on thread 0, and that
1305 * secondary threads are offline.
1306 */
1307 if (threads_per_core > 1 && !on_primary_thread()) {
1308 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1309 vcpu->arch.ret = -EBUSY;
1310 goto out;
1311 }
1312
371fefd6 1313 vc->pcpu = smp_processor_id();
2e25aa5f 1314 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
371fefd6 1315 kvmppc_start_thread(vcpu);
0456ec4f 1316 kvmppc_create_dtl_entry(vcpu, vc);
2e25aa5f 1317 }
371fefd6 1318
2f12f034 1319 vc->vcore_state = VCORE_RUNNING;
19ccb76a 1320 preempt_disable();
371fefd6 1321 spin_unlock(&vc->lock);
de56a948 1322
371fefd6 1323 kvm_guest_enter();
2c9097e4
PM
1324
1325 srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1326
19ccb76a 1327 __kvmppc_vcore_entry(NULL, vcpu0);
de56a948 1328
371fefd6 1329 spin_lock(&vc->lock);
19ccb76a
PM
1330 /* disable sending of IPIs on virtual external irqs */
1331 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1332 vcpu->cpu = -1;
1333 /* wait for secondary threads to finish writing their state to memory */
371fefd6
PM
1334 if (vc->nap_count < vc->n_woken)
1335 kvmppc_wait_for_nap(vc);
2f12f034
PM
1336 for (i = 0; i < threads_per_core; ++i)
1337 kvmppc_release_hwthread(vc->pcpu + i);
371fefd6 1338 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
19ccb76a 1339 vc->vcore_state = VCORE_EXITING;
371fefd6
PM
1340 spin_unlock(&vc->lock);
1341
2c9097e4
PM
1342 srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1343
371fefd6
PM
1344 /* make sure updates to secondary vcpu structs are visible now */
1345 smp_mb();
de56a948
PM
1346 kvm_guest_exit();
1347
1348 preempt_enable();
1349 kvm_resched(vcpu);
1350
913d3ff9 1351 spin_lock(&vc->lock);
de56a948 1352 now = get_tb();
371fefd6
PM
1353 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1354 /* cancel pending dec exception if dec is positive */
1355 if (now < vcpu->arch.dec_expires &&
1356 kvmppc_core_pending_dec(vcpu))
1357 kvmppc_core_dequeue_dec(vcpu);
19ccb76a
PM
1358
1359 ret = RESUME_GUEST;
1360 if (vcpu->arch.trap)
3a167bea
AK
1361 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1362 vcpu->arch.run_task);
19ccb76a 1363
371fefd6
PM
1364 vcpu->arch.ret = ret;
1365 vcpu->arch.trap = 0;
19ccb76a
PM
1366
1367 if (vcpu->arch.ceded) {
1368 if (ret != RESUME_GUEST)
1369 kvmppc_end_cede(vcpu);
1370 else
1371 kvmppc_set_timer(vcpu);
1372 }
371fefd6 1373 }
de56a948
PM
1374
1375 out:
19ccb76a 1376 vc->vcore_state = VCORE_INACTIVE;
371fefd6
PM
1377 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1378 arch.run_list) {
1379 if (vcpu->arch.ret != RESUME_GUEST) {
1380 kvmppc_remove_runnable(vc, vcpu);
1381 wake_up(&vcpu->arch.cpu_run);
1382 }
1383 }
371fefd6
PM
1384}
1385
19ccb76a
PM
1386/*
1387 * Wait for some other vcpu thread to execute us, and
1388 * wake us up when we need to handle something in the host.
1389 */
1390static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
371fefd6 1391{
371fefd6
PM
1392 DEFINE_WAIT(wait);
1393
19ccb76a
PM
1394 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1395 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1396 schedule();
1397 finish_wait(&vcpu->arch.cpu_run, &wait);
1398}
1399
1400/*
1401 * All the vcpus in this vcore are idle, so wait for a decrementer
1402 * or external interrupt to one of the vcpus. vc->lock is held.
1403 */
1404static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1405{
1406 DEFINE_WAIT(wait);
19ccb76a
PM
1407
1408 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1409 vc->vcore_state = VCORE_SLEEPING;
1410 spin_unlock(&vc->lock);
913d3ff9 1411 schedule();
19ccb76a
PM
1412 finish_wait(&vc->wq, &wait);
1413 spin_lock(&vc->lock);
1414 vc->vcore_state = VCORE_INACTIVE;
1415}
371fefd6 1416
19ccb76a
PM
1417static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1418{
1419 int n_ceded;
19ccb76a
PM
1420 struct kvmppc_vcore *vc;
1421 struct kvm_vcpu *v, *vn;
9e368f29 1422
371fefd6
PM
1423 kvm_run->exit_reason = 0;
1424 vcpu->arch.ret = RESUME_GUEST;
1425 vcpu->arch.trap = 0;
2f12f034 1426 kvmppc_update_vpas(vcpu);
371fefd6 1427
371fefd6
PM
1428 /*
1429 * Synchronize with other threads in this virtual core
1430 */
1431 vc = vcpu->arch.vcore;
1432 spin_lock(&vc->lock);
19ccb76a 1433 vcpu->arch.ceded = 0;
371fefd6
PM
1434 vcpu->arch.run_task = current;
1435 vcpu->arch.kvm_run = kvm_run;
c7b67670 1436 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
19ccb76a 1437 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
c7b67670 1438 vcpu->arch.busy_preempt = TB_NIL;
371fefd6
PM
1439 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1440 ++vc->n_runnable;
1441
19ccb76a
PM
1442 /*
1443 * This happens the first time this is called for a vcpu.
1444 * If the vcore is already running, we may be able to start
1445 * this thread straight away and have it join in.
1446 */
8455d79e 1447 if (!signal_pending(current)) {
19ccb76a
PM
1448 if (vc->vcore_state == VCORE_RUNNING &&
1449 VCORE_EXIT_COUNT(vc) == 0) {
1450 vcpu->arch.ptid = vc->n_runnable - 1;
2f12f034 1451 kvmppc_create_dtl_entry(vcpu, vc);
19ccb76a 1452 kvmppc_start_thread(vcpu);
8455d79e
PM
1453 } else if (vc->vcore_state == VCORE_SLEEPING) {
1454 wake_up(&vc->wq);
371fefd6
PM
1455 }
1456
8455d79e 1457 }
371fefd6 1458
19ccb76a
PM
1459 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1460 !signal_pending(current)) {
8455d79e 1461 if (vc->vcore_state != VCORE_INACTIVE) {
19ccb76a
PM
1462 spin_unlock(&vc->lock);
1463 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1464 spin_lock(&vc->lock);
1465 continue;
1466 }
19ccb76a
PM
1467 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1468 arch.run_list) {
7e28e60e 1469 kvmppc_core_prepare_to_enter(v);
19ccb76a
PM
1470 if (signal_pending(v->arch.run_task)) {
1471 kvmppc_remove_runnable(vc, v);
1472 v->stat.signal_exits++;
1473 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1474 v->arch.ret = -EINTR;
1475 wake_up(&v->arch.cpu_run);
1476 }
1477 }
8455d79e
PM
1478 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1479 break;
1480 vc->runner = vcpu;
1481 n_ceded = 0;
4619ac88 1482 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
8455d79e
PM
1483 if (!v->arch.pending_exceptions)
1484 n_ceded += v->arch.ceded;
4619ac88
PM
1485 else
1486 v->arch.ceded = 0;
1487 }
8455d79e
PM
1488 if (n_ceded == vc->n_runnable)
1489 kvmppc_vcore_blocked(vc);
1490 else
1491 kvmppc_run_core(vc);
0456ec4f 1492 vc->runner = NULL;
19ccb76a 1493 }
371fefd6 1494
8455d79e
PM
1495 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1496 (vc->vcore_state == VCORE_RUNNING ||
1497 vc->vcore_state == VCORE_EXITING)) {
1498 spin_unlock(&vc->lock);
1499 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1500 spin_lock(&vc->lock);
1501 }
1502
1503 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1504 kvmppc_remove_runnable(vc, vcpu);
1505 vcpu->stat.signal_exits++;
1506 kvm_run->exit_reason = KVM_EXIT_INTR;
1507 vcpu->arch.ret = -EINTR;
1508 }
1509
1510 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1511 /* Wake up some vcpu to run the core */
1512 v = list_first_entry(&vc->runnable_threads,
1513 struct kvm_vcpu, arch.run_list);
1514 wake_up(&v->arch.cpu_run);
371fefd6
PM
1515 }
1516
371fefd6 1517 spin_unlock(&vc->lock);
371fefd6 1518 return vcpu->arch.ret;
de56a948
PM
1519}
1520
3a167bea 1521static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
a8606e20
PM
1522{
1523 int r;
913d3ff9 1524 int srcu_idx;
a8606e20 1525
af8f38b3
AG
1526 if (!vcpu->arch.sane) {
1527 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1528 return -EINVAL;
1529 }
1530
25051b5a
SW
1531 kvmppc_core_prepare_to_enter(vcpu);
1532
19ccb76a
PM
1533 /* No need to go into the guest when all we'll do is come back out */
1534 if (signal_pending(current)) {
1535 run->exit_reason = KVM_EXIT_INTR;
1536 return -EINTR;
1537 }
1538
32fad281
PM
1539 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1540 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1541 smp_mb();
1542
1543 /* On the first time here, set up HTAB and VRMA or RMA */
c77162de 1544 if (!vcpu->kvm->arch.rma_setup_done) {
32fad281 1545 r = kvmppc_hv_setup_htab_rma(vcpu);
c77162de 1546 if (r)
32fad281 1547 goto out;
c77162de 1548 }
19ccb76a
PM
1549
1550 flush_fp_to_thread(current);
1551 flush_altivec_to_thread(current);
1552 flush_vsx_to_thread(current);
1553 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
342d3db7 1554 vcpu->arch.pgdir = current->mm->pgd;
c7b67670 1555 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
19ccb76a 1556
a8606e20
PM
1557 do {
1558 r = kvmppc_run_vcpu(run, vcpu);
1559
1560 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1561 !(vcpu->arch.shregs.msr & MSR_PR)) {
1562 r = kvmppc_pseries_do_hcall(vcpu);
7e28e60e 1563 kvmppc_core_prepare_to_enter(vcpu);
913d3ff9
PM
1564 } else if (r == RESUME_PAGE_FAULT) {
1565 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1566 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1567 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1568 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
a8606e20
PM
1569 }
1570 } while (r == RESUME_GUEST);
32fad281
PM
1571
1572 out:
c7b67670 1573 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
32fad281 1574 atomic_dec(&vcpu->kvm->arch.vcpus_running);
a8606e20
PM
1575 return r;
1576}
1577
54738c09 1578
aa04b4cc 1579/* Work out RMLS (real mode limit selector) field value for a given RMA size.
9e368f29 1580 Assumes POWER7 or PPC970. */
aa04b4cc
PM
1581static inline int lpcr_rmls(unsigned long rma_size)
1582{
1583 switch (rma_size) {
1584 case 32ul << 20: /* 32 MB */
9e368f29
PM
1585 if (cpu_has_feature(CPU_FTR_ARCH_206))
1586 return 8; /* only supported on POWER7 */
1587 return -1;
aa04b4cc
PM
1588 case 64ul << 20: /* 64 MB */
1589 return 3;
1590 case 128ul << 20: /* 128 MB */
1591 return 7;
1592 case 256ul << 20: /* 256 MB */
1593 return 4;
1594 case 1ul << 30: /* 1 GB */
1595 return 2;
1596 case 16ul << 30: /* 16 GB */
1597 return 1;
1598 case 256ul << 30: /* 256 GB */
1599 return 0;
1600 default:
1601 return -1;
1602 }
1603}
1604
1605static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1606{
aa04b4cc 1607 struct page *page;
6c45b810 1608 struct kvm_rma_info *ri = vma->vm_file->private_data;
aa04b4cc 1609
6c45b810 1610 if (vmf->pgoff >= kvm_rma_pages)
aa04b4cc
PM
1611 return VM_FAULT_SIGBUS;
1612
1613 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1614 get_page(page);
1615 vmf->page = page;
1616 return 0;
1617}
1618
1619static const struct vm_operations_struct kvm_rma_vm_ops = {
1620 .fault = kvm_rma_fault,
1621};
1622
1623static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1624{
314e51b9 1625 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
aa04b4cc
PM
1626 vma->vm_ops = &kvm_rma_vm_ops;
1627 return 0;
1628}
1629
1630static int kvm_rma_release(struct inode *inode, struct file *filp)
1631{
6c45b810 1632 struct kvm_rma_info *ri = filp->private_data;
aa04b4cc
PM
1633
1634 kvm_release_rma(ri);
1635 return 0;
1636}
1637
75ef9de1 1638static const struct file_operations kvm_rma_fops = {
aa04b4cc
PM
1639 .mmap = kvm_rma_mmap,
1640 .release = kvm_rma_release,
1641};
1642
3a167bea
AK
1643static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1644 struct kvm_allocate_rma *ret)
aa04b4cc 1645{
aa04b4cc 1646 long fd;
6c45b810
AK
1647 struct kvm_rma_info *ri;
1648 /*
1649 * Only do this on PPC970 in HV mode
1650 */
1651 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1652 !cpu_has_feature(CPU_FTR_ARCH_201))
1653 return -EINVAL;
1654
1655 if (!kvm_rma_pages)
1656 return -EINVAL;
aa04b4cc
PM
1657
1658 ri = kvm_alloc_rma();
1659 if (!ri)
1660 return -ENOMEM;
1661
2f84d5ea 1662 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
aa04b4cc
PM
1663 if (fd < 0)
1664 kvm_release_rma(ri);
1665
6c45b810 1666 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
aa04b4cc
PM
1667 return fd;
1668}
1669
5b74716e
BH
1670static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1671 int linux_psize)
1672{
1673 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1674
1675 if (!def->shift)
1676 return;
1677 (*sps)->page_shift = def->shift;
1678 (*sps)->slb_enc = def->sllp;
1679 (*sps)->enc[0].page_shift = def->shift;
b1022fbd
AK
1680 /*
1681 * Only return base page encoding. We don't want to return
1682 * all the supporting pte_enc, because our H_ENTER doesn't
1683 * support MPSS yet. Once they do, we can start passing all
1684 * support pte_enc here
1685 */
1686 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
5b74716e
BH
1687 (*sps)++;
1688}
1689
3a167bea
AK
1690static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1691 struct kvm_ppc_smmu_info *info)
5b74716e
BH
1692{
1693 struct kvm_ppc_one_seg_page_size *sps;
1694
1695 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1696 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1697 info->flags |= KVM_PPC_1T_SEGMENTS;
1698 info->slb_size = mmu_slb_size;
1699
1700 /* We only support these sizes for now, and no muti-size segments */
1701 sps = &info->sps[0];
1702 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1703 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1704 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1705
1706 return 0;
1707}
1708
82ed3616
PM
1709/*
1710 * Get (and clear) the dirty memory log for a memory slot.
1711 */
3a167bea
AK
1712static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1713 struct kvm_dirty_log *log)
82ed3616
PM
1714{
1715 struct kvm_memory_slot *memslot;
1716 int r;
1717 unsigned long n;
1718
1719 mutex_lock(&kvm->slots_lock);
1720
1721 r = -EINVAL;
bbacc0c1 1722 if (log->slot >= KVM_USER_MEM_SLOTS)
82ed3616
PM
1723 goto out;
1724
1725 memslot = id_to_memslot(kvm->memslots, log->slot);
1726 r = -ENOENT;
1727 if (!memslot->dirty_bitmap)
1728 goto out;
1729
1730 n = kvm_dirty_bitmap_bytes(memslot);
1731 memset(memslot->dirty_bitmap, 0, n);
1732
dfe49dbd 1733 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
82ed3616
PM
1734 if (r)
1735 goto out;
1736
1737 r = -EFAULT;
1738 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1739 goto out;
1740
1741 r = 0;
1742out:
1743 mutex_unlock(&kvm->slots_lock);
1744 return r;
1745}
1746
a66b48c3 1747static void unpin_slot(struct kvm_memory_slot *memslot)
de56a948 1748{
a66b48c3
PM
1749 unsigned long *physp;
1750 unsigned long j, npages, pfn;
1751 struct page *page;
aa04b4cc 1752
a66b48c3
PM
1753 physp = memslot->arch.slot_phys;
1754 npages = memslot->npages;
1755 if (!physp)
1756 return;
1757 for (j = 0; j < npages; j++) {
1758 if (!(physp[j] & KVMPPC_GOT_PAGE))
1759 continue;
1760 pfn = physp[j] >> PAGE_SHIFT;
1761 page = pfn_to_page(pfn);
1762 SetPageDirty(page);
1763 put_page(page);
1764 }
1765}
1766
3a167bea
AK
1767static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1768 struct kvm_memory_slot *dont)
a66b48c3
PM
1769{
1770 if (!dont || free->arch.rmap != dont->arch.rmap) {
1771 vfree(free->arch.rmap);
1772 free->arch.rmap = NULL;
b2b2f165 1773 }
a66b48c3
PM
1774 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1775 unpin_slot(free);
1776 vfree(free->arch.slot_phys);
1777 free->arch.slot_phys = NULL;
1778 }
1779}
1780
3a167bea
AK
1781static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1782 unsigned long npages)
a66b48c3
PM
1783{
1784 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1785 if (!slot->arch.rmap)
1786 return -ENOMEM;
1787 slot->arch.slot_phys = NULL;
aa04b4cc 1788
c77162de
PM
1789 return 0;
1790}
aa04b4cc 1791
3a167bea
AK
1792static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1793 struct kvm_memory_slot *memslot,
1794 struct kvm_userspace_memory_region *mem)
c77162de 1795{
a66b48c3 1796 unsigned long *phys;
c77162de 1797
a66b48c3
PM
1798 /* Allocate a slot_phys array if needed */
1799 phys = memslot->arch.slot_phys;
1800 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1801 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1802 if (!phys)
1803 return -ENOMEM;
1804 memslot->arch.slot_phys = phys;
aa04b4cc 1805 }
a66b48c3
PM
1806
1807 return 0;
c77162de
PM
1808}
1809
3a167bea
AK
1810static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1811 struct kvm_userspace_memory_region *mem,
1812 const struct kvm_memory_slot *old)
c77162de 1813{
dfe49dbd
PM
1814 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1815 struct kvm_memory_slot *memslot;
1816
8482644a 1817 if (npages && old->npages) {
dfe49dbd
PM
1818 /*
1819 * If modifying a memslot, reset all the rmap dirty bits.
1820 * If this is a new memslot, we don't need to do anything
1821 * since the rmap array starts out as all zeroes,
1822 * i.e. no pages are dirty.
1823 */
1824 memslot = id_to_memslot(kvm->memslots, mem->slot);
1825 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1826 }
c77162de
PM
1827}
1828
a0144e2a
PM
1829/*
1830 * Update LPCR values in kvm->arch and in vcores.
1831 * Caller must hold kvm->lock.
1832 */
1833void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1834{
1835 long int i;
1836 u32 cores_done = 0;
1837
1838 if ((kvm->arch.lpcr & mask) == lpcr)
1839 return;
1840
1841 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1842
1843 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1844 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1845 if (!vc)
1846 continue;
1847 spin_lock(&vc->lock);
1848 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1849 spin_unlock(&vc->lock);
1850 if (++cores_done >= kvm->arch.online_vcores)
1851 break;
1852 }
1853}
1854
3a167bea
AK
1855static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1856{
1857 return;
1858}
1859
32fad281 1860static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
c77162de
PM
1861{
1862 int err = 0;
1863 struct kvm *kvm = vcpu->kvm;
6c45b810 1864 struct kvm_rma_info *ri = NULL;
c77162de
PM
1865 unsigned long hva;
1866 struct kvm_memory_slot *memslot;
1867 struct vm_area_struct *vma;
a0144e2a
PM
1868 unsigned long lpcr = 0, senc;
1869 unsigned long lpcr_mask = 0;
c77162de
PM
1870 unsigned long psize, porder;
1871 unsigned long rma_size;
1872 unsigned long rmls;
1873 unsigned long *physp;
da9d1d7f 1874 unsigned long i, npages;
2c9097e4 1875 int srcu_idx;
c77162de
PM
1876
1877 mutex_lock(&kvm->lock);
1878 if (kvm->arch.rma_setup_done)
1879 goto out; /* another vcpu beat us to it */
aa04b4cc 1880
32fad281
PM
1881 /* Allocate hashed page table (if not done already) and reset it */
1882 if (!kvm->arch.hpt_virt) {
1883 err = kvmppc_alloc_hpt(kvm, NULL);
1884 if (err) {
1885 pr_err("KVM: Couldn't alloc HPT\n");
1886 goto out;
1887 }
1888 }
1889
c77162de 1890 /* Look up the memslot for guest physical address 0 */
2c9097e4 1891 srcu_idx = srcu_read_lock(&kvm->srcu);
c77162de 1892 memslot = gfn_to_memslot(kvm, 0);
aa04b4cc 1893
c77162de
PM
1894 /* We must have some memory at 0 by now */
1895 err = -EINVAL;
1896 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2c9097e4 1897 goto out_srcu;
c77162de
PM
1898
1899 /* Look up the VMA for the start of this memory slot */
1900 hva = memslot->userspace_addr;
1901 down_read(&current->mm->mmap_sem);
1902 vma = find_vma(current->mm, hva);
1903 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1904 goto up_out;
1905
1906 psize = vma_kernel_pagesize(vma);
da9d1d7f 1907 porder = __ilog2(psize);
c77162de
PM
1908
1909 /* Is this one of our preallocated RMAs? */
1910 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1911 hva == vma->vm_start)
1912 ri = vma->vm_file->private_data;
1913
1914 up_read(&current->mm->mmap_sem);
1915
1916 if (!ri) {
1917 /* On POWER7, use VRMA; on PPC970, give up */
1918 err = -EPERM;
1919 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1920 pr_err("KVM: CPU requires an RMO\n");
2c9097e4 1921 goto out_srcu;
c77162de
PM
1922 }
1923
da9d1d7f
PM
1924 /* We can handle 4k, 64k or 16M pages in the VRMA */
1925 err = -EINVAL;
1926 if (!(psize == 0x1000 || psize == 0x10000 ||
1927 psize == 0x1000000))
2c9097e4 1928 goto out_srcu;
da9d1d7f 1929
c77162de 1930 /* Update VRMASD field in the LPCR */
da9d1d7f 1931 senc = slb_pgsize_encoding(psize);
697d3899
PM
1932 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1933 (VRMA_VSID << SLB_VSID_SHIFT_1T);
a0144e2a
PM
1934 lpcr_mask = LPCR_VRMASD;
1935 /* the -4 is to account for senc values starting at 0x10 */
1936 lpcr = senc << (LPCR_VRMASD_SH - 4);
c77162de
PM
1937
1938 /* Create HPTEs in the hash page table for the VRMA */
da9d1d7f 1939 kvmppc_map_vrma(vcpu, memslot, porder);
c77162de
PM
1940
1941 } else {
1942 /* Set up to use an RMO region */
6c45b810 1943 rma_size = kvm_rma_pages;
c77162de
PM
1944 if (rma_size > memslot->npages)
1945 rma_size = memslot->npages;
1946 rma_size <<= PAGE_SHIFT;
aa04b4cc 1947 rmls = lpcr_rmls(rma_size);
c77162de 1948 err = -EINVAL;
5d226ae5 1949 if ((long)rmls < 0) {
c77162de 1950 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2c9097e4 1951 goto out_srcu;
aa04b4cc
PM
1952 }
1953 atomic_inc(&ri->use_count);
1954 kvm->arch.rma = ri;
9e368f29
PM
1955
1956 /* Update LPCR and RMOR */
9e368f29
PM
1957 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1958 /* PPC970; insert RMLS value (split field) in HID4 */
a0144e2a
PM
1959 lpcr_mask = (1ul << HID4_RMLS0_SH) |
1960 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1961 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
9e368f29
PM
1962 ((rmls & 3) << HID4_RMLS2_SH);
1963 /* RMOR is also in HID4 */
1964 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1965 << HID4_RMOR_SH;
1966 } else {
1967 /* POWER7 */
a0144e2a
PM
1968 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1969 lpcr = rmls << LPCR_RMLS_SH;
6c45b810 1970 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
9e368f29 1971 }
c77162de 1972 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
aa04b4cc 1973 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
aa04b4cc 1974
c77162de 1975 /* Initialize phys addrs of pages in RMO */
6c45b810 1976 npages = kvm_rma_pages;
da9d1d7f 1977 porder = __ilog2(npages);
a66b48c3
PM
1978 physp = memslot->arch.slot_phys;
1979 if (physp) {
1980 if (npages > memslot->npages)
1981 npages = memslot->npages;
1982 spin_lock(&kvm->arch.slot_phys_lock);
1983 for (i = 0; i < npages; ++i)
1984 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1985 porder;
1986 spin_unlock(&kvm->arch.slot_phys_lock);
1987 }
aa04b4cc
PM
1988 }
1989
a0144e2a
PM
1990 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
1991
c77162de
PM
1992 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1993 smp_wmb();
1994 kvm->arch.rma_setup_done = 1;
1995 err = 0;
2c9097e4
PM
1996 out_srcu:
1997 srcu_read_unlock(&kvm->srcu, srcu_idx);
c77162de
PM
1998 out:
1999 mutex_unlock(&kvm->lock);
2000 return err;
b2b2f165 2001
c77162de
PM
2002 up_out:
2003 up_read(&current->mm->mmap_sem);
505d6421 2004 goto out_srcu;
de56a948
PM
2005}
2006
3a167bea 2007static int kvmppc_core_init_vm_hv(struct kvm *kvm)
de56a948 2008{
32fad281 2009 unsigned long lpcr, lpid;
de56a948 2010
32fad281
PM
2011 /* Allocate the guest's logical partition ID */
2012
2013 lpid = kvmppc_alloc_lpid();
5d226ae5 2014 if ((long)lpid < 0)
32fad281
PM
2015 return -ENOMEM;
2016 kvm->arch.lpid = lpid;
de56a948 2017
1b400ba0
PM
2018 /*
2019 * Since we don't flush the TLB when tearing down a VM,
2020 * and this lpid might have previously been used,
2021 * make sure we flush on each core before running the new VM.
2022 */
2023 cpumask_setall(&kvm->arch.need_tlb_flush);
2024
aa04b4cc 2025 kvm->arch.rma = NULL;
aa04b4cc 2026
9e368f29 2027 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
aa04b4cc 2028
9e368f29
PM
2029 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2030 /* PPC970; HID4 is effectively the LPCR */
9e368f29
PM
2031 kvm->arch.host_lpid = 0;
2032 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2033 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2034 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2035 ((lpid & 0xf) << HID4_LPID5_SH);
2036 } else {
2037 /* POWER7; init LPCR for virtual RMA mode */
2038 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2039 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2040 lpcr &= LPCR_PECE | LPCR_LPES;
2041 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
697d3899
PM
2042 LPCR_VPM0 | LPCR_VPM1;
2043 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2044 (VRMA_VSID << SLB_VSID_SHIFT_1T);
9e368f29
PM
2045 }
2046 kvm->arch.lpcr = lpcr;
aa04b4cc 2047
342d3db7 2048 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
c77162de 2049 spin_lock_init(&kvm->arch.slot_phys_lock);
512691d4
PM
2050
2051 /*
2052 * Don't allow secondary CPU threads to come online
2053 * while any KVM VMs exist.
2054 */
2055 inhibit_secondary_onlining();
2056
54738c09 2057 return 0;
de56a948
PM
2058}
2059
f1378b1c
PM
2060static void kvmppc_free_vcores(struct kvm *kvm)
2061{
2062 long int i;
2063
2064 for (i = 0; i < KVM_MAX_VCORES; ++i)
2065 kfree(kvm->arch.vcores[i]);
2066 kvm->arch.online_vcores = 0;
2067}
2068
3a167bea 2069static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
de56a948 2070{
512691d4
PM
2071 uninhibit_secondary_onlining();
2072
f1378b1c 2073 kvmppc_free_vcores(kvm);
aa04b4cc
PM
2074 if (kvm->arch.rma) {
2075 kvm_release_rma(kvm->arch.rma);
2076 kvm->arch.rma = NULL;
2077 }
2078
de56a948
PM
2079 kvmppc_free_hpt(kvm);
2080}
2081
3a167bea
AK
2082/* We don't need to emulate any privileged instructions or dcbz */
2083static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2084 unsigned int inst, int *advance)
de56a948 2085{
3a167bea 2086 return EMULATE_FAIL;
de56a948
PM
2087}
2088
3a167bea
AK
2089static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2090 ulong spr_val)
de56a948
PM
2091{
2092 return EMULATE_FAIL;
2093}
2094
3a167bea
AK
2095static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2096 ulong *spr_val)
de56a948
PM
2097{
2098 return EMULATE_FAIL;
2099}
2100
3a167bea 2101static int kvmppc_core_check_processor_compat_hv(void)
de56a948 2102{
3a167bea
AK
2103 if (!cpu_has_feature(CPU_FTR_HVMODE))
2104 return -EIO;
2105 return 0;
de56a948
PM
2106}
2107
3a167bea
AK
2108static long kvm_arch_vm_ioctl_hv(struct file *filp,
2109 unsigned int ioctl, unsigned long arg)
2110{
2111 struct kvm *kvm __maybe_unused = filp->private_data;
2112 void __user *argp = (void __user *)arg;
2113 long r;
2114
2115 switch (ioctl) {
2116
2117 case KVM_ALLOCATE_RMA: {
2118 struct kvm_allocate_rma rma;
2119 struct kvm *kvm = filp->private_data;
2120
2121 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2122 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2123 r = -EFAULT;
2124 break;
2125 }
2126
2127 case KVM_PPC_ALLOCATE_HTAB: {
2128 u32 htab_order;
2129
2130 r = -EFAULT;
2131 if (get_user(htab_order, (u32 __user *)argp))
2132 break;
2133 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2134 if (r)
2135 break;
2136 r = -EFAULT;
2137 if (put_user(htab_order, (u32 __user *)argp))
2138 break;
2139 r = 0;
2140 break;
2141 }
2142
2143 case KVM_PPC_GET_HTAB_FD: {
2144 struct kvm_get_htab_fd ghf;
2145
2146 r = -EFAULT;
2147 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2148 break;
2149 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2150 break;
2151 }
2152
2153 default:
2154 r = -ENOTTY;
2155 }
2156
2157 return r;
2158}
2159
cbbc58d4 2160static struct kvmppc_ops kvm_ops_hv = {
3a167bea
AK
2161 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2162 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2163 .get_one_reg = kvmppc_get_one_reg_hv,
2164 .set_one_reg = kvmppc_set_one_reg_hv,
2165 .vcpu_load = kvmppc_core_vcpu_load_hv,
2166 .vcpu_put = kvmppc_core_vcpu_put_hv,
2167 .set_msr = kvmppc_set_msr_hv,
2168 .vcpu_run = kvmppc_vcpu_run_hv,
2169 .vcpu_create = kvmppc_core_vcpu_create_hv,
2170 .vcpu_free = kvmppc_core_vcpu_free_hv,
2171 .check_requests = kvmppc_core_check_requests_hv,
2172 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2173 .flush_memslot = kvmppc_core_flush_memslot_hv,
2174 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2175 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2176 .unmap_hva = kvm_unmap_hva_hv,
2177 .unmap_hva_range = kvm_unmap_hva_range_hv,
2178 .age_hva = kvm_age_hva_hv,
2179 .test_age_hva = kvm_test_age_hva_hv,
2180 .set_spte_hva = kvm_set_spte_hva_hv,
2181 .mmu_destroy = kvmppc_mmu_destroy_hv,
2182 .free_memslot = kvmppc_core_free_memslot_hv,
2183 .create_memslot = kvmppc_core_create_memslot_hv,
2184 .init_vm = kvmppc_core_init_vm_hv,
2185 .destroy_vm = kvmppc_core_destroy_vm_hv,
3a167bea
AK
2186 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2187 .emulate_op = kvmppc_core_emulate_op_hv,
2188 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2189 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2190 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2191 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2192};
2193
2194static int kvmppc_book3s_init_hv(void)
de56a948
PM
2195{
2196 int r;
cbbc58d4
AK
2197 /*
2198 * FIXME!! Do we need to check on all cpus ?
2199 */
2200 r = kvmppc_core_check_processor_compat_hv();
2201 if (r < 0)
de56a948
PM
2202 return r;
2203
cbbc58d4
AK
2204 kvm_ops_hv.owner = THIS_MODULE;
2205 kvmppc_hv_ops = &kvm_ops_hv;
de56a948 2206
cbbc58d4 2207 r = kvmppc_mmu_hv_init();
de56a948
PM
2208 return r;
2209}
2210
3a167bea 2211static void kvmppc_book3s_exit_hv(void)
de56a948 2212{
cbbc58d4 2213 kvmppc_hv_ops = NULL;
de56a948
PM
2214}
2215
3a167bea
AK
2216module_init(kvmppc_book3s_init_hv);
2217module_exit(kvmppc_book3s_exit_hv);
2ba9f0d8 2218MODULE_LICENSE("GPL");