]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/powerpc/kvm/book3s_hv.c
KVM: PPC: Book3S HV: Fix preempted vcore list locking
[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>
398a76c6 34#include <linux/miscdevice.h>
e23a808b 35#include <linux/debugfs.h>
de56a948
PM
36
37#include <asm/reg.h>
38#include <asm/cputable.h>
9678cdaa 39#include <asm/cache.h>
de56a948
PM
40#include <asm/cacheflush.h>
41#include <asm/tlbflush.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
371fefd6 49#include <asm/cputhreads.h>
aa04b4cc 50#include <asm/page.h>
de1d9248 51#include <asm/hvcall.h>
ae3a197e 52#include <asm/switch_to.h>
512691d4 53#include <asm/smp.h>
66feed61 54#include <asm/dbell.h>
de56a948 55#include <linux/gfp.h>
de56a948
PM
56#include <linux/vmalloc.h>
57#include <linux/highmem.h>
c77162de 58#include <linux/hugetlb.h>
2ba9f0d8 59#include <linux/module.h>
de56a948 60
3a167bea
AK
61#include "book3s.h"
62
3c78f78a
SW
63#define CREATE_TRACE_POINTS
64#include "trace_hv.h"
65
de56a948
PM
66/* #define EXIT_DEBUG */
67/* #define EXIT_DEBUG_SIMPLE */
68/* #define EXIT_DEBUG_INT */
69
913d3ff9
PM
70/* Used to indicate that a guest page fault needs to be handled */
71#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
72
c7b67670
PM
73/* Used as a "null" value for timebase values */
74#define TB_NIL (~(u64)0)
75
699a0ea0
PM
76static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
77
9678cdaa
SS
78#if defined(CONFIG_PPC_64K_PAGES)
79#define MPP_BUFFER_ORDER 0
80#elif defined(CONFIG_PPC_4K_PAGES)
81#define MPP_BUFFER_ORDER 3
82#endif
83
b4deba5c
PM
84static int dynamic_mt_modes = 6;
85module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
86MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
ec257165
PM
87static int target_smt_mode;
88module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
89MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
9678cdaa 90
19ccb76a 91static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
32fad281 92static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
19ccb76a 93
66feed61
PM
94static bool kvmppc_ipi_thread(int cpu)
95{
96 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
97 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
98 preempt_disable();
99 if (cpu_first_thread_sibling(cpu) ==
100 cpu_first_thread_sibling(smp_processor_id())) {
101 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
102 msg |= cpu_thread_in_core(cpu);
103 smp_mb();
104 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
105 preempt_enable();
106 return true;
107 }
108 preempt_enable();
109 }
110
111#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
112 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
113 xics_wake_cpu(cpu);
114 return true;
115 }
116#endif
117
118 return false;
119}
120
3a167bea 121static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
54695c30 122{
ec257165 123 int cpu;
54695c30
BH
124 wait_queue_head_t *wqp;
125
126 wqp = kvm_arch_vcpu_wq(vcpu);
127 if (waitqueue_active(wqp)) {
128 wake_up_interruptible(wqp);
129 ++vcpu->stat.halt_wakeup;
130 }
131
ec257165 132 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
66feed61 133 return;
54695c30
BH
134
135 /* CPU points to the first thread of the core */
ec257165 136 cpu = vcpu->cpu;
66feed61
PM
137 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
138 smp_send_reschedule(cpu);
54695c30
BH
139}
140
c7b67670
PM
141/*
142 * We use the vcpu_load/put functions to measure stolen time.
143 * Stolen time is counted as time when either the vcpu is able to
144 * run as part of a virtual core, but the task running the vcore
145 * is preempted or sleeping, or when the vcpu needs something done
146 * in the kernel by the task running the vcpu, but that task is
147 * preempted or sleeping. Those two things have to be counted
148 * separately, since one of the vcpu tasks will take on the job
149 * of running the core, and the other vcpu tasks in the vcore will
150 * sleep waiting for it to do that, but that sleep shouldn't count
151 * as stolen time.
152 *
153 * Hence we accumulate stolen time when the vcpu can run as part of
154 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
155 * needs its task to do other things in the kernel (for example,
156 * service a page fault) in busy_stolen. We don't accumulate
157 * stolen time for a vcore when it is inactive, or for a vcpu
158 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
159 * a misnomer; it means that the vcpu task is not executing in
160 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
161 * the kernel. We don't have any way of dividing up that time
162 * between time that the vcpu is genuinely stopped, time that
163 * the task is actively working on behalf of the vcpu, and time
164 * that the task is preempted, so we don't count any of it as
165 * stolen.
166 *
167 * Updates to busy_stolen are protected by arch.tbacct_lock;
2711e248
PM
168 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
169 * lock. The stolen times are measured in units of timebase ticks.
170 * (Note that the != TB_NIL checks below are purely defensive;
171 * they should never fail.)
c7b67670
PM
172 */
173
ec257165
PM
174static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
175{
176 unsigned long flags;
177
178 spin_lock_irqsave(&vc->stoltb_lock, flags);
179 vc->preempt_tb = mftb();
180 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
181}
182
183static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
184{
185 unsigned long flags;
186
187 spin_lock_irqsave(&vc->stoltb_lock, flags);
188 if (vc->preempt_tb != TB_NIL) {
189 vc->stolen_tb += mftb() - vc->preempt_tb;
190 vc->preempt_tb = TB_NIL;
191 }
192 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
193}
194
3a167bea 195static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
de56a948 196{
0456ec4f 197 struct kvmppc_vcore *vc = vcpu->arch.vcore;
bf3d32e1 198 unsigned long flags;
0456ec4f 199
2711e248
PM
200 /*
201 * We can test vc->runner without taking the vcore lock,
202 * because only this task ever sets vc->runner to this
203 * vcpu, and once it is set to this vcpu, only this task
204 * ever sets it to NULL.
205 */
ec257165
PM
206 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
207 kvmppc_core_end_stolen(vc);
208
2711e248 209 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
c7b67670
PM
210 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
211 vcpu->arch.busy_preempt != TB_NIL) {
212 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
213 vcpu->arch.busy_preempt = TB_NIL;
214 }
bf3d32e1 215 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
de56a948
PM
216}
217
3a167bea 218static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
de56a948 219{
0456ec4f 220 struct kvmppc_vcore *vc = vcpu->arch.vcore;
bf3d32e1 221 unsigned long flags;
0456ec4f 222
ec257165
PM
223 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
224 kvmppc_core_start_stolen(vc);
225
2711e248 226 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
c7b67670
PM
227 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
228 vcpu->arch.busy_preempt = mftb();
bf3d32e1 229 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
de56a948
PM
230}
231
3a167bea 232static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
de56a948
PM
233{
234 vcpu->arch.shregs.msr = msr;
19ccb76a 235 kvmppc_end_cede(vcpu);
de56a948
PM
236}
237
5358a963 238static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
de56a948
PM
239{
240 vcpu->arch.pvr = pvr;
241}
242
5358a963 243static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
388cc6e1
PM
244{
245 unsigned long pcr = 0;
246 struct kvmppc_vcore *vc = vcpu->arch.vcore;
247
248 if (arch_compat) {
388cc6e1
PM
249 switch (arch_compat) {
250 case PVR_ARCH_205:
5557ae0e
PM
251 /*
252 * If an arch bit is set in PCR, all the defined
253 * higher-order arch bits also have to be set.
254 */
255 pcr = PCR_ARCH_206 | PCR_ARCH_205;
388cc6e1
PM
256 break;
257 case PVR_ARCH_206:
258 case PVR_ARCH_206p:
5557ae0e
PM
259 pcr = PCR_ARCH_206;
260 break;
261 case PVR_ARCH_207:
388cc6e1
PM
262 break;
263 default:
264 return -EINVAL;
265 }
5557ae0e
PM
266
267 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
268 /* POWER7 can't emulate POWER8 */
269 if (!(pcr & PCR_ARCH_206))
270 return -EINVAL;
271 pcr &= ~PCR_ARCH_206;
272 }
388cc6e1
PM
273 }
274
275 spin_lock(&vc->lock);
276 vc->arch_compat = arch_compat;
277 vc->pcr = pcr;
278 spin_unlock(&vc->lock);
279
280 return 0;
281}
282
5358a963 283static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
de56a948
PM
284{
285 int r;
286
287 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
288 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
289 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
290 for (r = 0; r < 16; ++r)
291 pr_err("r%2d = %.16lx r%d = %.16lx\n",
292 r, kvmppc_get_gpr(vcpu, r),
293 r+16, kvmppc_get_gpr(vcpu, r+16));
294 pr_err("ctr = %.16lx lr = %.16lx\n",
295 vcpu->arch.ctr, vcpu->arch.lr);
296 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
297 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
298 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
299 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
300 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
301 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
302 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
303 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
304 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
305 pr_err("fault dar = %.16lx dsisr = %.8x\n",
306 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
307 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
308 for (r = 0; r < vcpu->arch.slb_max; ++r)
309 pr_err(" ESID = %.16llx VSID = %.16llx\n",
310 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
311 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
a0144e2a 312 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
de56a948
PM
313 vcpu->arch.last_inst);
314}
315
5358a963 316static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
a8606e20
PM
317{
318 int r;
319 struct kvm_vcpu *v, *ret = NULL;
320
321 mutex_lock(&kvm->lock);
322 kvm_for_each_vcpu(r, v, kvm) {
323 if (v->vcpu_id == id) {
324 ret = v;
325 break;
326 }
327 }
328 mutex_unlock(&kvm->lock);
329 return ret;
330}
331
332static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
333{
f13c13a0 334 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
02407552 335 vpa->yield_count = cpu_to_be32(1);
a8606e20
PM
336}
337
55b665b0
PM
338static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
339 unsigned long addr, unsigned long len)
340{
341 /* check address is cacheline aligned */
342 if (addr & (L1_CACHE_BYTES - 1))
343 return -EINVAL;
344 spin_lock(&vcpu->arch.vpa_update_lock);
345 if (v->next_gpa != addr || v->len != len) {
346 v->next_gpa = addr;
347 v->len = addr ? len : 0;
348 v->update_pending = 1;
349 }
350 spin_unlock(&vcpu->arch.vpa_update_lock);
351 return 0;
352}
353
2e25aa5f
PM
354/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
355struct reg_vpa {
356 u32 dummy;
357 union {
02407552
AG
358 __be16 hword;
359 __be32 word;
2e25aa5f
PM
360 } length;
361};
362
363static int vpa_is_registered(struct kvmppc_vpa *vpap)
364{
365 if (vpap->update_pending)
366 return vpap->next_gpa != 0;
367 return vpap->pinned_addr != NULL;
368}
369
a8606e20
PM
370static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
371 unsigned long flags,
372 unsigned long vcpuid, unsigned long vpa)
373{
374 struct kvm *kvm = vcpu->kvm;
93e60249 375 unsigned long len, nb;
a8606e20
PM
376 void *va;
377 struct kvm_vcpu *tvcpu;
2e25aa5f
PM
378 int err;
379 int subfunc;
380 struct kvmppc_vpa *vpap;
a8606e20
PM
381
382 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
383 if (!tvcpu)
384 return H_PARAMETER;
385
2e25aa5f
PM
386 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
387 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
388 subfunc == H_VPA_REG_SLB) {
389 /* Registering new area - address must be cache-line aligned */
390 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
a8606e20 391 return H_PARAMETER;
2e25aa5f
PM
392
393 /* convert logical addr to kernel addr and read length */
93e60249
PM
394 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
395 if (va == NULL)
b2b2f165 396 return H_PARAMETER;
2e25aa5f 397 if (subfunc == H_VPA_REG_VPA)
02407552 398 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
a8606e20 399 else
02407552 400 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
c35635ef 401 kvmppc_unpin_guest_page(kvm, va, vpa, false);
2e25aa5f
PM
402
403 /* Check length */
404 if (len > nb || len < sizeof(struct reg_vpa))
405 return H_PARAMETER;
406 } else {
407 vpa = 0;
408 len = 0;
409 }
410
411 err = H_PARAMETER;
412 vpap = NULL;
413 spin_lock(&tvcpu->arch.vpa_update_lock);
414
415 switch (subfunc) {
416 case H_VPA_REG_VPA: /* register VPA */
417 if (len < sizeof(struct lppaca))
a8606e20 418 break;
2e25aa5f
PM
419 vpap = &tvcpu->arch.vpa;
420 err = 0;
421 break;
422
423 case H_VPA_REG_DTL: /* register DTL */
424 if (len < sizeof(struct dtl_entry))
a8606e20 425 break;
2e25aa5f
PM
426 len -= len % sizeof(struct dtl_entry);
427
428 /* Check that they have previously registered a VPA */
429 err = H_RESOURCE;
430 if (!vpa_is_registered(&tvcpu->arch.vpa))
a8606e20 431 break;
2e25aa5f
PM
432
433 vpap = &tvcpu->arch.dtl;
434 err = 0;
435 break;
436
437 case H_VPA_REG_SLB: /* register SLB shadow buffer */
438 /* Check that they have previously registered a VPA */
439 err = H_RESOURCE;
440 if (!vpa_is_registered(&tvcpu->arch.vpa))
a8606e20 441 break;
2e25aa5f
PM
442
443 vpap = &tvcpu->arch.slb_shadow;
444 err = 0;
445 break;
446
447 case H_VPA_DEREG_VPA: /* deregister VPA */
448 /* Check they don't still have a DTL or SLB buf registered */
449 err = H_RESOURCE;
450 if (vpa_is_registered(&tvcpu->arch.dtl) ||
451 vpa_is_registered(&tvcpu->arch.slb_shadow))
a8606e20 452 break;
2e25aa5f
PM
453
454 vpap = &tvcpu->arch.vpa;
455 err = 0;
456 break;
457
458 case H_VPA_DEREG_DTL: /* deregister DTL */
459 vpap = &tvcpu->arch.dtl;
460 err = 0;
461 break;
462
463 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
464 vpap = &tvcpu->arch.slb_shadow;
465 err = 0;
466 break;
467 }
468
469 if (vpap) {
470 vpap->next_gpa = vpa;
471 vpap->len = len;
472 vpap->update_pending = 1;
a8606e20 473 }
93e60249 474
2e25aa5f
PM
475 spin_unlock(&tvcpu->arch.vpa_update_lock);
476
93e60249 477 return err;
a8606e20
PM
478}
479
081f323b 480static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
2e25aa5f 481{
081f323b 482 struct kvm *kvm = vcpu->kvm;
2e25aa5f
PM
483 void *va;
484 unsigned long nb;
081f323b 485 unsigned long gpa;
2e25aa5f 486
081f323b
PM
487 /*
488 * We need to pin the page pointed to by vpap->next_gpa,
489 * but we can't call kvmppc_pin_guest_page under the lock
490 * as it does get_user_pages() and down_read(). So we
491 * have to drop the lock, pin the page, then get the lock
492 * again and check that a new area didn't get registered
493 * in the meantime.
494 */
495 for (;;) {
496 gpa = vpap->next_gpa;
497 spin_unlock(&vcpu->arch.vpa_update_lock);
498 va = NULL;
499 nb = 0;
500 if (gpa)
c35635ef 501 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
081f323b
PM
502 spin_lock(&vcpu->arch.vpa_update_lock);
503 if (gpa == vpap->next_gpa)
504 break;
505 /* sigh... unpin that one and try again */
506 if (va)
c35635ef 507 kvmppc_unpin_guest_page(kvm, va, gpa, false);
081f323b
PM
508 }
509
510 vpap->update_pending = 0;
511 if (va && nb < vpap->len) {
512 /*
513 * If it's now too short, it must be that userspace
514 * has changed the mappings underlying guest memory,
515 * so unregister the region.
516 */
c35635ef 517 kvmppc_unpin_guest_page(kvm, va, gpa, false);
081f323b 518 va = NULL;
2e25aa5f
PM
519 }
520 if (vpap->pinned_addr)
c35635ef
PM
521 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
522 vpap->dirty);
523 vpap->gpa = gpa;
2e25aa5f 524 vpap->pinned_addr = va;
c35635ef 525 vpap->dirty = false;
2e25aa5f
PM
526 if (va)
527 vpap->pinned_end = va + vpap->len;
528}
529
530static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
531{
2f12f034
PM
532 if (!(vcpu->arch.vpa.update_pending ||
533 vcpu->arch.slb_shadow.update_pending ||
534 vcpu->arch.dtl.update_pending))
535 return;
536
2e25aa5f
PM
537 spin_lock(&vcpu->arch.vpa_update_lock);
538 if (vcpu->arch.vpa.update_pending) {
081f323b 539 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
55b665b0
PM
540 if (vcpu->arch.vpa.pinned_addr)
541 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
2e25aa5f
PM
542 }
543 if (vcpu->arch.dtl.update_pending) {
081f323b 544 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
2e25aa5f
PM
545 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
546 vcpu->arch.dtl_index = 0;
547 }
548 if (vcpu->arch.slb_shadow.update_pending)
081f323b 549 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
2e25aa5f
PM
550 spin_unlock(&vcpu->arch.vpa_update_lock);
551}
552
c7b67670
PM
553/*
554 * Return the accumulated stolen time for the vcore up until `now'.
555 * The caller should hold the vcore lock.
556 */
557static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
558{
559 u64 p;
2711e248 560 unsigned long flags;
c7b67670 561
2711e248
PM
562 spin_lock_irqsave(&vc->stoltb_lock, flags);
563 p = vc->stolen_tb;
c7b67670 564 if (vc->vcore_state != VCORE_INACTIVE &&
2711e248
PM
565 vc->preempt_tb != TB_NIL)
566 p += now - vc->preempt_tb;
567 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
c7b67670
PM
568 return p;
569}
570
0456ec4f
PM
571static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
572 struct kvmppc_vcore *vc)
573{
574 struct dtl_entry *dt;
575 struct lppaca *vpa;
c7b67670
PM
576 unsigned long stolen;
577 unsigned long core_stolen;
578 u64 now;
0456ec4f
PM
579
580 dt = vcpu->arch.dtl_ptr;
581 vpa = vcpu->arch.vpa.pinned_addr;
c7b67670
PM
582 now = mftb();
583 core_stolen = vcore_stolen_time(vc, now);
584 stolen = core_stolen - vcpu->arch.stolen_logged;
585 vcpu->arch.stolen_logged = core_stolen;
bf3d32e1 586 spin_lock_irq(&vcpu->arch.tbacct_lock);
c7b67670
PM
587 stolen += vcpu->arch.busy_stolen;
588 vcpu->arch.busy_stolen = 0;
bf3d32e1 589 spin_unlock_irq(&vcpu->arch.tbacct_lock);
0456ec4f
PM
590 if (!dt || !vpa)
591 return;
592 memset(dt, 0, sizeof(struct dtl_entry));
593 dt->dispatch_reason = 7;
02407552
AG
594 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
595 dt->timebase = cpu_to_be64(now + vc->tb_offset);
596 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
597 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
598 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
0456ec4f
PM
599 ++dt;
600 if (dt == vcpu->arch.dtl.pinned_end)
601 dt = vcpu->arch.dtl.pinned_addr;
602 vcpu->arch.dtl_ptr = dt;
603 /* order writing *dt vs. writing vpa->dtl_idx */
604 smp_wmb();
02407552 605 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
c35635ef 606 vcpu->arch.dtl.dirty = true;
0456ec4f
PM
607}
608
9642382e
MN
609static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
610{
611 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
612 return true;
613 if ((!vcpu->arch.vcore->arch_compat) &&
614 cpu_has_feature(CPU_FTR_ARCH_207S))
615 return true;
616 return false;
617}
618
619static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
620 unsigned long resource, unsigned long value1,
621 unsigned long value2)
622{
623 switch (resource) {
624 case H_SET_MODE_RESOURCE_SET_CIABR:
625 if (!kvmppc_power8_compatible(vcpu))
626 return H_P2;
627 if (value2)
628 return H_P4;
629 if (mflags)
630 return H_UNSUPPORTED_FLAG_START;
631 /* Guests can't breakpoint the hypervisor */
632 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
633 return H_P3;
634 vcpu->arch.ciabr = value1;
635 return H_SUCCESS;
636 case H_SET_MODE_RESOURCE_SET_DAWR:
637 if (!kvmppc_power8_compatible(vcpu))
638 return H_P2;
639 if (mflags)
640 return H_UNSUPPORTED_FLAG_START;
641 if (value2 & DABRX_HYP)
642 return H_P4;
643 vcpu->arch.dawr = value1;
644 vcpu->arch.dawrx = value2;
645 return H_SUCCESS;
646 default:
647 return H_TOO_HARD;
648 }
649}
650
90fd09f8
SB
651static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
652{
653 struct kvmppc_vcore *vcore = target->arch.vcore;
654
655 /*
656 * We expect to have been called by the real mode handler
657 * (kvmppc_rm_h_confer()) which would have directly returned
658 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
659 * have useful work to do and should not confer) so we don't
660 * recheck that here.
661 */
662
663 spin_lock(&vcore->lock);
664 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
ec257165
PM
665 vcore->vcore_state != VCORE_INACTIVE &&
666 vcore->runner)
90fd09f8
SB
667 target = vcore->runner;
668 spin_unlock(&vcore->lock);
669
670 return kvm_vcpu_yield_to(target);
671}
672
673static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
674{
675 int yield_count = 0;
676 struct lppaca *lppaca;
677
678 spin_lock(&vcpu->arch.vpa_update_lock);
679 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
680 if (lppaca)
ecb6d618 681 yield_count = be32_to_cpu(lppaca->yield_count);
90fd09f8
SB
682 spin_unlock(&vcpu->arch.vpa_update_lock);
683 return yield_count;
684}
685
a8606e20
PM
686int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
687{
688 unsigned long req = kvmppc_get_gpr(vcpu, 3);
689 unsigned long target, ret = H_SUCCESS;
90fd09f8 690 int yield_count;
a8606e20 691 struct kvm_vcpu *tvcpu;
8e591cb7 692 int idx, rc;
a8606e20 693
699a0ea0
PM
694 if (req <= MAX_HCALL_OPCODE &&
695 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
696 return RESUME_HOST;
697
a8606e20
PM
698 switch (req) {
699 case H_CEDE:
a8606e20
PM
700 break;
701 case H_PROD:
702 target = kvmppc_get_gpr(vcpu, 4);
703 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
704 if (!tvcpu) {
705 ret = H_PARAMETER;
706 break;
707 }
708 tvcpu->arch.prodded = 1;
709 smp_mb();
710 if (vcpu->arch.ceded) {
711 if (waitqueue_active(&vcpu->wq)) {
712 wake_up_interruptible(&vcpu->wq);
713 vcpu->stat.halt_wakeup++;
714 }
715 }
716 break;
717 case H_CONFER:
42d7604d
PM
718 target = kvmppc_get_gpr(vcpu, 4);
719 if (target == -1)
720 break;
721 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
722 if (!tvcpu) {
723 ret = H_PARAMETER;
724 break;
725 }
90fd09f8
SB
726 yield_count = kvmppc_get_gpr(vcpu, 5);
727 if (kvmppc_get_yield_count(tvcpu) != yield_count)
728 break;
729 kvm_arch_vcpu_yield_to(tvcpu);
a8606e20
PM
730 break;
731 case H_REGISTER_VPA:
732 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
733 kvmppc_get_gpr(vcpu, 5),
734 kvmppc_get_gpr(vcpu, 6));
735 break;
8e591cb7
ME
736 case H_RTAS:
737 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
738 return RESUME_HOST;
739
c9438092 740 idx = srcu_read_lock(&vcpu->kvm->srcu);
8e591cb7 741 rc = kvmppc_rtas_hcall(vcpu);
c9438092 742 srcu_read_unlock(&vcpu->kvm->srcu, idx);
8e591cb7
ME
743
744 if (rc == -ENOENT)
745 return RESUME_HOST;
746 else if (rc == 0)
747 break;
748
749 /* Send the error out to userspace via KVM_RUN */
750 return rc;
99342cf8
DG
751 case H_LOGICAL_CI_LOAD:
752 ret = kvmppc_h_logical_ci_load(vcpu);
753 if (ret == H_TOO_HARD)
754 return RESUME_HOST;
755 break;
756 case H_LOGICAL_CI_STORE:
757 ret = kvmppc_h_logical_ci_store(vcpu);
758 if (ret == H_TOO_HARD)
759 return RESUME_HOST;
760 break;
9642382e
MN
761 case H_SET_MODE:
762 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
763 kvmppc_get_gpr(vcpu, 5),
764 kvmppc_get_gpr(vcpu, 6),
765 kvmppc_get_gpr(vcpu, 7));
766 if (ret == H_TOO_HARD)
767 return RESUME_HOST;
768 break;
bc5ad3f3
BH
769 case H_XIRR:
770 case H_CPPR:
771 case H_EOI:
772 case H_IPI:
8e44ddc3
PM
773 case H_IPOLL:
774 case H_XIRR_X:
bc5ad3f3
BH
775 if (kvmppc_xics_enabled(vcpu)) {
776 ret = kvmppc_xics_hcall(vcpu, req);
777 break;
778 } /* fallthrough */
a8606e20
PM
779 default:
780 return RESUME_HOST;
781 }
782 kvmppc_set_gpr(vcpu, 3, ret);
783 vcpu->arch.hcall_needed = 0;
784 return RESUME_GUEST;
785}
786
ae2113a4
PM
787static int kvmppc_hcall_impl_hv(unsigned long cmd)
788{
789 switch (cmd) {
790 case H_CEDE:
791 case H_PROD:
792 case H_CONFER:
793 case H_REGISTER_VPA:
9642382e 794 case H_SET_MODE:
99342cf8
DG
795 case H_LOGICAL_CI_LOAD:
796 case H_LOGICAL_CI_STORE:
ae2113a4
PM
797#ifdef CONFIG_KVM_XICS
798 case H_XIRR:
799 case H_CPPR:
800 case H_EOI:
801 case H_IPI:
802 case H_IPOLL:
803 case H_XIRR_X:
804#endif
805 return 1;
806 }
807
808 /* See if it's in the real-mode table */
809 return kvmppc_hcall_impl_hv_realmode(cmd);
810}
811
a59c1d9e
MS
812static int kvmppc_emulate_debug_inst(struct kvm_run *run,
813 struct kvm_vcpu *vcpu)
814{
815 u32 last_inst;
816
817 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
818 EMULATE_DONE) {
819 /*
820 * Fetch failed, so return to guest and
821 * try executing it again.
822 */
823 return RESUME_GUEST;
824 }
825
826 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
827 run->exit_reason = KVM_EXIT_DEBUG;
828 run->debug.arch.address = kvmppc_get_pc(vcpu);
829 return RESUME_HOST;
830 } else {
831 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
832 return RESUME_GUEST;
833 }
834}
835
3a167bea
AK
836static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
837 struct task_struct *tsk)
de56a948
PM
838{
839 int r = RESUME_HOST;
840
841 vcpu->stat.sum_exits++;
842
843 run->exit_reason = KVM_EXIT_UNKNOWN;
844 run->ready_for_interrupt_injection = 1;
845 switch (vcpu->arch.trap) {
846 /* We're good on these - the host merely wanted to get our attention */
847 case BOOK3S_INTERRUPT_HV_DECREMENTER:
848 vcpu->stat.dec_exits++;
849 r = RESUME_GUEST;
850 break;
851 case BOOK3S_INTERRUPT_EXTERNAL:
5d00f66b 852 case BOOK3S_INTERRUPT_H_DOORBELL:
de56a948
PM
853 vcpu->stat.ext_intr_exits++;
854 r = RESUME_GUEST;
855 break;
dee6f24c
MS
856 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
857 case BOOK3S_INTERRUPT_HMI:
de56a948
PM
858 case BOOK3S_INTERRUPT_PERFMON:
859 r = RESUME_GUEST;
860 break;
b4072df4
PM
861 case BOOK3S_INTERRUPT_MACHINE_CHECK:
862 /*
863 * Deliver a machine check interrupt to the guest.
864 * We have to do this, even if the host has handled the
865 * machine check, because machine checks use SRR0/1 and
866 * the interrupt might have trashed guest state in them.
867 */
868 kvmppc_book3s_queue_irqprio(vcpu,
869 BOOK3S_INTERRUPT_MACHINE_CHECK);
870 r = RESUME_GUEST;
871 break;
de56a948
PM
872 case BOOK3S_INTERRUPT_PROGRAM:
873 {
874 ulong flags;
875 /*
876 * Normally program interrupts are delivered directly
877 * to the guest by the hardware, but we can get here
878 * as a result of a hypervisor emulation interrupt
879 * (e40) getting turned into a 700 by BML RTAS.
880 */
881 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
882 kvmppc_core_queue_program(vcpu, flags);
883 r = RESUME_GUEST;
884 break;
885 }
886 case BOOK3S_INTERRUPT_SYSCALL:
887 {
888 /* hcall - punt to userspace */
889 int i;
890
27025a60
LPF
891 /* hypercall with MSR_PR has already been handled in rmode,
892 * and never reaches here.
893 */
894
de56a948
PM
895 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
896 for (i = 0; i < 9; ++i)
897 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
898 run->exit_reason = KVM_EXIT_PAPR_HCALL;
899 vcpu->arch.hcall_needed = 1;
900 r = RESUME_HOST;
901 break;
902 }
903 /*
342d3db7
PM
904 * We get these next two if the guest accesses a page which it thinks
905 * it has mapped but which is not actually present, either because
906 * it is for an emulated I/O device or because the corresonding
907 * host page has been paged out. Any other HDSI/HISI interrupts
908 * have been handled already.
de56a948
PM
909 */
910 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
913d3ff9 911 r = RESUME_PAGE_FAULT;
de56a948
PM
912 break;
913 case BOOK3S_INTERRUPT_H_INST_STORAGE:
913d3ff9
PM
914 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
915 vcpu->arch.fault_dsisr = 0;
916 r = RESUME_PAGE_FAULT;
de56a948
PM
917 break;
918 /*
919 * This occurs if the guest executes an illegal instruction.
a59c1d9e
MS
920 * If the guest debug is disabled, generate a program interrupt
921 * to the guest. If guest debug is enabled, we need to check
922 * whether the instruction is a software breakpoint instruction.
923 * Accordingly return to Guest or Host.
de56a948
PM
924 */
925 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
4a157d61
PM
926 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
927 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
928 swab32(vcpu->arch.emul_inst) :
929 vcpu->arch.emul_inst;
a59c1d9e
MS
930 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
931 r = kvmppc_emulate_debug_inst(run, vcpu);
932 } else {
933 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
934 r = RESUME_GUEST;
935 }
bd3048b8
ME
936 break;
937 /*
938 * This occurs if the guest (kernel or userspace), does something that
939 * is prohibited by HFSCR. We just generate a program interrupt to
940 * the guest.
941 */
942 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
943 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
de56a948
PM
944 r = RESUME_GUEST;
945 break;
946 default:
947 kvmppc_dump_regs(vcpu);
948 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
949 vcpu->arch.trap, kvmppc_get_pc(vcpu),
950 vcpu->arch.shregs.msr);
f3271d4c 951 run->hw.hardware_exit_reason = vcpu->arch.trap;
de56a948 952 r = RESUME_HOST;
de56a948
PM
953 break;
954 }
955
de56a948
PM
956 return r;
957}
958
3a167bea
AK
959static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
960 struct kvm_sregs *sregs)
de56a948
PM
961{
962 int i;
963
de56a948 964 memset(sregs, 0, sizeof(struct kvm_sregs));
87916442 965 sregs->pvr = vcpu->arch.pvr;
de56a948
PM
966 for (i = 0; i < vcpu->arch.slb_max; i++) {
967 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
968 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
969 }
970
971 return 0;
972}
973
3a167bea
AK
974static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
975 struct kvm_sregs *sregs)
de56a948
PM
976{
977 int i, j;
978
9333e6c4
PM
979 /* Only accept the same PVR as the host's, since we can't spoof it */
980 if (sregs->pvr != vcpu->arch.pvr)
981 return -EINVAL;
de56a948
PM
982
983 j = 0;
984 for (i = 0; i < vcpu->arch.slb_nr; i++) {
985 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
986 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
987 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
988 ++j;
989 }
990 }
991 vcpu->arch.slb_max = j;
992
993 return 0;
994}
995
a0840240
AK
996static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
997 bool preserve_top32)
a0144e2a 998{
8f902b00 999 struct kvm *kvm = vcpu->kvm;
a0144e2a
PM
1000 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1001 u64 mask;
1002
8f902b00 1003 mutex_lock(&kvm->lock);
a0144e2a 1004 spin_lock(&vc->lock);
d682916a
AB
1005 /*
1006 * If ILE (interrupt little-endian) has changed, update the
1007 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1008 */
1009 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
d682916a
AB
1010 struct kvm_vcpu *vcpu;
1011 int i;
1012
d682916a
AB
1013 kvm_for_each_vcpu(i, vcpu, kvm) {
1014 if (vcpu->arch.vcore != vc)
1015 continue;
1016 if (new_lpcr & LPCR_ILE)
1017 vcpu->arch.intr_msr |= MSR_LE;
1018 else
1019 vcpu->arch.intr_msr &= ~MSR_LE;
1020 }
d682916a
AB
1021 }
1022
a0144e2a
PM
1023 /*
1024 * Userspace can only modify DPFD (default prefetch depth),
1025 * ILE (interrupt little-endian) and TC (translation control).
e0622bd9 1026 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
a0144e2a
PM
1027 */
1028 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
e0622bd9
PM
1029 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1030 mask |= LPCR_AIL;
a0840240
AK
1031
1032 /* Broken 32-bit version of LPCR must not clear top bits */
1033 if (preserve_top32)
1034 mask &= 0xFFFFFFFF;
a0144e2a
PM
1035 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1036 spin_unlock(&vc->lock);
8f902b00 1037 mutex_unlock(&kvm->lock);
a0144e2a
PM
1038}
1039
3a167bea
AK
1040static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1041 union kvmppc_one_reg *val)
31f3438e 1042{
a136a8bd
PM
1043 int r = 0;
1044 long int i;
31f3438e 1045
a136a8bd 1046 switch (id) {
a59c1d9e
MS
1047 case KVM_REG_PPC_DEBUG_INST:
1048 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1049 break;
31f3438e 1050 case KVM_REG_PPC_HIOR:
a136a8bd
PM
1051 *val = get_reg_val(id, 0);
1052 break;
1053 case KVM_REG_PPC_DABR:
1054 *val = get_reg_val(id, vcpu->arch.dabr);
1055 break;
8563bf52
PM
1056 case KVM_REG_PPC_DABRX:
1057 *val = get_reg_val(id, vcpu->arch.dabrx);
1058 break;
a136a8bd
PM
1059 case KVM_REG_PPC_DSCR:
1060 *val = get_reg_val(id, vcpu->arch.dscr);
1061 break;
1062 case KVM_REG_PPC_PURR:
1063 *val = get_reg_val(id, vcpu->arch.purr);
1064 break;
1065 case KVM_REG_PPC_SPURR:
1066 *val = get_reg_val(id, vcpu->arch.spurr);
1067 break;
1068 case KVM_REG_PPC_AMR:
1069 *val = get_reg_val(id, vcpu->arch.amr);
1070 break;
1071 case KVM_REG_PPC_UAMOR:
1072 *val = get_reg_val(id, vcpu->arch.uamor);
1073 break;
b005255e 1074 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
a136a8bd
PM
1075 i = id - KVM_REG_PPC_MMCR0;
1076 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1077 break;
1078 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1079 i = id - KVM_REG_PPC_PMC1;
1080 *val = get_reg_val(id, vcpu->arch.pmc[i]);
31f3438e 1081 break;
b005255e
MN
1082 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1083 i = id - KVM_REG_PPC_SPMC1;
1084 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1085 break;
14941789
PM
1086 case KVM_REG_PPC_SIAR:
1087 *val = get_reg_val(id, vcpu->arch.siar);
1088 break;
1089 case KVM_REG_PPC_SDAR:
1090 *val = get_reg_val(id, vcpu->arch.sdar);
1091 break;
b005255e
MN
1092 case KVM_REG_PPC_SIER:
1093 *val = get_reg_val(id, vcpu->arch.sier);
a8bd19ef 1094 break;
b005255e
MN
1095 case KVM_REG_PPC_IAMR:
1096 *val = get_reg_val(id, vcpu->arch.iamr);
1097 break;
b005255e
MN
1098 case KVM_REG_PPC_PSPB:
1099 *val = get_reg_val(id, vcpu->arch.pspb);
1100 break;
b005255e
MN
1101 case KVM_REG_PPC_DPDES:
1102 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1103 break;
1104 case KVM_REG_PPC_DAWR:
1105 *val = get_reg_val(id, vcpu->arch.dawr);
1106 break;
1107 case KVM_REG_PPC_DAWRX:
1108 *val = get_reg_val(id, vcpu->arch.dawrx);
1109 break;
1110 case KVM_REG_PPC_CIABR:
1111 *val = get_reg_val(id, vcpu->arch.ciabr);
1112 break;
b005255e
MN
1113 case KVM_REG_PPC_CSIGR:
1114 *val = get_reg_val(id, vcpu->arch.csigr);
1115 break;
1116 case KVM_REG_PPC_TACR:
1117 *val = get_reg_val(id, vcpu->arch.tacr);
1118 break;
1119 case KVM_REG_PPC_TCSCR:
1120 *val = get_reg_val(id, vcpu->arch.tcscr);
1121 break;
1122 case KVM_REG_PPC_PID:
1123 *val = get_reg_val(id, vcpu->arch.pid);
1124 break;
1125 case KVM_REG_PPC_ACOP:
1126 *val = get_reg_val(id, vcpu->arch.acop);
1127 break;
1128 case KVM_REG_PPC_WORT:
1129 *val = get_reg_val(id, vcpu->arch.wort);
a8bd19ef 1130 break;
55b665b0
PM
1131 case KVM_REG_PPC_VPA_ADDR:
1132 spin_lock(&vcpu->arch.vpa_update_lock);
1133 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1134 spin_unlock(&vcpu->arch.vpa_update_lock);
1135 break;
1136 case KVM_REG_PPC_VPA_SLB:
1137 spin_lock(&vcpu->arch.vpa_update_lock);
1138 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1139 val->vpaval.length = vcpu->arch.slb_shadow.len;
1140 spin_unlock(&vcpu->arch.vpa_update_lock);
1141 break;
1142 case KVM_REG_PPC_VPA_DTL:
1143 spin_lock(&vcpu->arch.vpa_update_lock);
1144 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1145 val->vpaval.length = vcpu->arch.dtl.len;
1146 spin_unlock(&vcpu->arch.vpa_update_lock);
1147 break;
93b0f4dc
PM
1148 case KVM_REG_PPC_TB_OFFSET:
1149 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1150 break;
a0144e2a 1151 case KVM_REG_PPC_LPCR:
a0840240 1152 case KVM_REG_PPC_LPCR_64:
a0144e2a
PM
1153 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1154 break;
4b8473c9
PM
1155 case KVM_REG_PPC_PPR:
1156 *val = get_reg_val(id, vcpu->arch.ppr);
1157 break;
a7d80d01
MN
1158#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1159 case KVM_REG_PPC_TFHAR:
1160 *val = get_reg_val(id, vcpu->arch.tfhar);
1161 break;
1162 case KVM_REG_PPC_TFIAR:
1163 *val = get_reg_val(id, vcpu->arch.tfiar);
1164 break;
1165 case KVM_REG_PPC_TEXASR:
1166 *val = get_reg_val(id, vcpu->arch.texasr);
1167 break;
1168 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1169 i = id - KVM_REG_PPC_TM_GPR0;
1170 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1171 break;
1172 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1173 {
1174 int j;
1175 i = id - KVM_REG_PPC_TM_VSR0;
1176 if (i < 32)
1177 for (j = 0; j < TS_FPRWIDTH; j++)
1178 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1179 else {
1180 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1181 val->vval = vcpu->arch.vr_tm.vr[i-32];
1182 else
1183 r = -ENXIO;
1184 }
1185 break;
1186 }
1187 case KVM_REG_PPC_TM_CR:
1188 *val = get_reg_val(id, vcpu->arch.cr_tm);
1189 break;
1190 case KVM_REG_PPC_TM_LR:
1191 *val = get_reg_val(id, vcpu->arch.lr_tm);
1192 break;
1193 case KVM_REG_PPC_TM_CTR:
1194 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1195 break;
1196 case KVM_REG_PPC_TM_FPSCR:
1197 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1198 break;
1199 case KVM_REG_PPC_TM_AMR:
1200 *val = get_reg_val(id, vcpu->arch.amr_tm);
1201 break;
1202 case KVM_REG_PPC_TM_PPR:
1203 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1204 break;
1205 case KVM_REG_PPC_TM_VRSAVE:
1206 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1207 break;
1208 case KVM_REG_PPC_TM_VSCR:
1209 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1210 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1211 else
1212 r = -ENXIO;
1213 break;
1214 case KVM_REG_PPC_TM_DSCR:
1215 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1216 break;
1217 case KVM_REG_PPC_TM_TAR:
1218 *val = get_reg_val(id, vcpu->arch.tar_tm);
1219 break;
1220#endif
388cc6e1
PM
1221 case KVM_REG_PPC_ARCH_COMPAT:
1222 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1223 break;
31f3438e 1224 default:
a136a8bd 1225 r = -EINVAL;
31f3438e
PM
1226 break;
1227 }
1228
1229 return r;
1230}
1231
3a167bea
AK
1232static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1233 union kvmppc_one_reg *val)
31f3438e 1234{
a136a8bd
PM
1235 int r = 0;
1236 long int i;
55b665b0 1237 unsigned long addr, len;
31f3438e 1238
a136a8bd 1239 switch (id) {
31f3438e 1240 case KVM_REG_PPC_HIOR:
31f3438e 1241 /* Only allow this to be set to zero */
a136a8bd 1242 if (set_reg_val(id, *val))
31f3438e
PM
1243 r = -EINVAL;
1244 break;
a136a8bd
PM
1245 case KVM_REG_PPC_DABR:
1246 vcpu->arch.dabr = set_reg_val(id, *val);
1247 break;
8563bf52
PM
1248 case KVM_REG_PPC_DABRX:
1249 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1250 break;
a136a8bd
PM
1251 case KVM_REG_PPC_DSCR:
1252 vcpu->arch.dscr = set_reg_val(id, *val);
1253 break;
1254 case KVM_REG_PPC_PURR:
1255 vcpu->arch.purr = set_reg_val(id, *val);
1256 break;
1257 case KVM_REG_PPC_SPURR:
1258 vcpu->arch.spurr = set_reg_val(id, *val);
1259 break;
1260 case KVM_REG_PPC_AMR:
1261 vcpu->arch.amr = set_reg_val(id, *val);
1262 break;
1263 case KVM_REG_PPC_UAMOR:
1264 vcpu->arch.uamor = set_reg_val(id, *val);
1265 break;
b005255e 1266 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
a136a8bd
PM
1267 i = id - KVM_REG_PPC_MMCR0;
1268 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1269 break;
1270 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1271 i = id - KVM_REG_PPC_PMC1;
1272 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1273 break;
b005255e
MN
1274 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1275 i = id - KVM_REG_PPC_SPMC1;
1276 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1277 break;
14941789
PM
1278 case KVM_REG_PPC_SIAR:
1279 vcpu->arch.siar = set_reg_val(id, *val);
1280 break;
1281 case KVM_REG_PPC_SDAR:
1282 vcpu->arch.sdar = set_reg_val(id, *val);
1283 break;
b005255e
MN
1284 case KVM_REG_PPC_SIER:
1285 vcpu->arch.sier = set_reg_val(id, *val);
a8bd19ef 1286 break;
b005255e
MN
1287 case KVM_REG_PPC_IAMR:
1288 vcpu->arch.iamr = set_reg_val(id, *val);
1289 break;
b005255e
MN
1290 case KVM_REG_PPC_PSPB:
1291 vcpu->arch.pspb = set_reg_val(id, *val);
1292 break;
b005255e
MN
1293 case KVM_REG_PPC_DPDES:
1294 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1295 break;
1296 case KVM_REG_PPC_DAWR:
1297 vcpu->arch.dawr = set_reg_val(id, *val);
1298 break;
1299 case KVM_REG_PPC_DAWRX:
1300 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1301 break;
1302 case KVM_REG_PPC_CIABR:
1303 vcpu->arch.ciabr = set_reg_val(id, *val);
1304 /* Don't allow setting breakpoints in hypervisor code */
1305 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1306 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1307 break;
b005255e
MN
1308 case KVM_REG_PPC_CSIGR:
1309 vcpu->arch.csigr = set_reg_val(id, *val);
1310 break;
1311 case KVM_REG_PPC_TACR:
1312 vcpu->arch.tacr = set_reg_val(id, *val);
1313 break;
1314 case KVM_REG_PPC_TCSCR:
1315 vcpu->arch.tcscr = set_reg_val(id, *val);
1316 break;
1317 case KVM_REG_PPC_PID:
1318 vcpu->arch.pid = set_reg_val(id, *val);
1319 break;
1320 case KVM_REG_PPC_ACOP:
1321 vcpu->arch.acop = set_reg_val(id, *val);
1322 break;
1323 case KVM_REG_PPC_WORT:
1324 vcpu->arch.wort = set_reg_val(id, *val);
a8bd19ef 1325 break;
55b665b0
PM
1326 case KVM_REG_PPC_VPA_ADDR:
1327 addr = set_reg_val(id, *val);
1328 r = -EINVAL;
1329 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1330 vcpu->arch.dtl.next_gpa))
1331 break;
1332 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1333 break;
1334 case KVM_REG_PPC_VPA_SLB:
1335 addr = val->vpaval.addr;
1336 len = val->vpaval.length;
1337 r = -EINVAL;
1338 if (addr && !vcpu->arch.vpa.next_gpa)
1339 break;
1340 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1341 break;
1342 case KVM_REG_PPC_VPA_DTL:
1343 addr = val->vpaval.addr;
1344 len = val->vpaval.length;
1345 r = -EINVAL;
9f8c8c78
PM
1346 if (addr && (len < sizeof(struct dtl_entry) ||
1347 !vcpu->arch.vpa.next_gpa))
55b665b0
PM
1348 break;
1349 len -= len % sizeof(struct dtl_entry);
1350 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1351 break;
93b0f4dc
PM
1352 case KVM_REG_PPC_TB_OFFSET:
1353 /* round up to multiple of 2^24 */
1354 vcpu->arch.vcore->tb_offset =
1355 ALIGN(set_reg_val(id, *val), 1UL << 24);
1356 break;
a0144e2a 1357 case KVM_REG_PPC_LPCR:
a0840240
AK
1358 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1359 break;
1360 case KVM_REG_PPC_LPCR_64:
1361 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
a0144e2a 1362 break;
4b8473c9
PM
1363 case KVM_REG_PPC_PPR:
1364 vcpu->arch.ppr = set_reg_val(id, *val);
1365 break;
a7d80d01
MN
1366#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1367 case KVM_REG_PPC_TFHAR:
1368 vcpu->arch.tfhar = set_reg_val(id, *val);
1369 break;
1370 case KVM_REG_PPC_TFIAR:
1371 vcpu->arch.tfiar = set_reg_val(id, *val);
1372 break;
1373 case KVM_REG_PPC_TEXASR:
1374 vcpu->arch.texasr = set_reg_val(id, *val);
1375 break;
1376 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1377 i = id - KVM_REG_PPC_TM_GPR0;
1378 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1379 break;
1380 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1381 {
1382 int j;
1383 i = id - KVM_REG_PPC_TM_VSR0;
1384 if (i < 32)
1385 for (j = 0; j < TS_FPRWIDTH; j++)
1386 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1387 else
1388 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1389 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1390 else
1391 r = -ENXIO;
1392 break;
1393 }
1394 case KVM_REG_PPC_TM_CR:
1395 vcpu->arch.cr_tm = set_reg_val(id, *val);
1396 break;
1397 case KVM_REG_PPC_TM_LR:
1398 vcpu->arch.lr_tm = set_reg_val(id, *val);
1399 break;
1400 case KVM_REG_PPC_TM_CTR:
1401 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1402 break;
1403 case KVM_REG_PPC_TM_FPSCR:
1404 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1405 break;
1406 case KVM_REG_PPC_TM_AMR:
1407 vcpu->arch.amr_tm = set_reg_val(id, *val);
1408 break;
1409 case KVM_REG_PPC_TM_PPR:
1410 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1411 break;
1412 case KVM_REG_PPC_TM_VRSAVE:
1413 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1414 break;
1415 case KVM_REG_PPC_TM_VSCR:
1416 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1417 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1418 else
1419 r = - ENXIO;
1420 break;
1421 case KVM_REG_PPC_TM_DSCR:
1422 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1423 break;
1424 case KVM_REG_PPC_TM_TAR:
1425 vcpu->arch.tar_tm = set_reg_val(id, *val);
1426 break;
1427#endif
388cc6e1
PM
1428 case KVM_REG_PPC_ARCH_COMPAT:
1429 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1430 break;
31f3438e 1431 default:
a136a8bd 1432 r = -EINVAL;
31f3438e
PM
1433 break;
1434 }
1435
1436 return r;
1437}
1438
de9bdd1a
SS
1439static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1440{
1441 struct kvmppc_vcore *vcore;
1442
1443 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1444
1445 if (vcore == NULL)
1446 return NULL;
1447
1448 INIT_LIST_HEAD(&vcore->runnable_threads);
1449 spin_lock_init(&vcore->lock);
2711e248 1450 spin_lock_init(&vcore->stoltb_lock);
de9bdd1a
SS
1451 init_waitqueue_head(&vcore->wq);
1452 vcore->preempt_tb = TB_NIL;
1453 vcore->lpcr = kvm->arch.lpcr;
1454 vcore->first_vcpuid = core * threads_per_subcore;
1455 vcore->kvm = kvm;
ec257165 1456 INIT_LIST_HEAD(&vcore->preempt_list);
de9bdd1a 1457
9678cdaa
SS
1458 vcore->mpp_buffer_is_valid = false;
1459
1460 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1461 vcore->mpp_buffer = (void *)__get_free_pages(
1462 GFP_KERNEL|__GFP_ZERO,
1463 MPP_BUFFER_ORDER);
1464
de9bdd1a
SS
1465 return vcore;
1466}
1467
b6c295df
PM
1468#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1469static struct debugfs_timings_element {
1470 const char *name;
1471 size_t offset;
1472} timings[] = {
1473 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1474 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1475 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1476 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1477 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1478};
1479
1480#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1481
1482struct debugfs_timings_state {
1483 struct kvm_vcpu *vcpu;
1484 unsigned int buflen;
1485 char buf[N_TIMINGS * 100];
1486};
1487
1488static int debugfs_timings_open(struct inode *inode, struct file *file)
1489{
1490 struct kvm_vcpu *vcpu = inode->i_private;
1491 struct debugfs_timings_state *p;
1492
1493 p = kzalloc(sizeof(*p), GFP_KERNEL);
1494 if (!p)
1495 return -ENOMEM;
1496
1497 kvm_get_kvm(vcpu->kvm);
1498 p->vcpu = vcpu;
1499 file->private_data = p;
1500
1501 return nonseekable_open(inode, file);
1502}
1503
1504static int debugfs_timings_release(struct inode *inode, struct file *file)
1505{
1506 struct debugfs_timings_state *p = file->private_data;
1507
1508 kvm_put_kvm(p->vcpu->kvm);
1509 kfree(p);
1510 return 0;
1511}
1512
1513static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1514 size_t len, loff_t *ppos)
1515{
1516 struct debugfs_timings_state *p = file->private_data;
1517 struct kvm_vcpu *vcpu = p->vcpu;
1518 char *s, *buf_end;
1519 struct kvmhv_tb_accumulator tb;
1520 u64 count;
1521 loff_t pos;
1522 ssize_t n;
1523 int i, loops;
1524 bool ok;
1525
1526 if (!p->buflen) {
1527 s = p->buf;
1528 buf_end = s + sizeof(p->buf);
1529 for (i = 0; i < N_TIMINGS; ++i) {
1530 struct kvmhv_tb_accumulator *acc;
1531
1532 acc = (struct kvmhv_tb_accumulator *)
1533 ((unsigned long)vcpu + timings[i].offset);
1534 ok = false;
1535 for (loops = 0; loops < 1000; ++loops) {
1536 count = acc->seqcount;
1537 if (!(count & 1)) {
1538 smp_rmb();
1539 tb = *acc;
1540 smp_rmb();
1541 if (count == acc->seqcount) {
1542 ok = true;
1543 break;
1544 }
1545 }
1546 udelay(1);
1547 }
1548 if (!ok)
1549 snprintf(s, buf_end - s, "%s: stuck\n",
1550 timings[i].name);
1551 else
1552 snprintf(s, buf_end - s,
1553 "%s: %llu %llu %llu %llu\n",
1554 timings[i].name, count / 2,
1555 tb_to_ns(tb.tb_total),
1556 tb_to_ns(tb.tb_min),
1557 tb_to_ns(tb.tb_max));
1558 s += strlen(s);
1559 }
1560 p->buflen = s - p->buf;
1561 }
1562
1563 pos = *ppos;
1564 if (pos >= p->buflen)
1565 return 0;
1566 if (len > p->buflen - pos)
1567 len = p->buflen - pos;
1568 n = copy_to_user(buf, p->buf + pos, len);
1569 if (n) {
1570 if (n == len)
1571 return -EFAULT;
1572 len -= n;
1573 }
1574 *ppos = pos + len;
1575 return len;
1576}
1577
1578static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1579 size_t len, loff_t *ppos)
1580{
1581 return -EACCES;
1582}
1583
1584static const struct file_operations debugfs_timings_ops = {
1585 .owner = THIS_MODULE,
1586 .open = debugfs_timings_open,
1587 .release = debugfs_timings_release,
1588 .read = debugfs_timings_read,
1589 .write = debugfs_timings_write,
1590 .llseek = generic_file_llseek,
1591};
1592
1593/* Create a debugfs directory for the vcpu */
1594static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1595{
1596 char buf[16];
1597 struct kvm *kvm = vcpu->kvm;
1598
1599 snprintf(buf, sizeof(buf), "vcpu%u", id);
1600 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1601 return;
1602 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1603 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1604 return;
1605 vcpu->arch.debugfs_timings =
1606 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1607 vcpu, &debugfs_timings_ops);
1608}
1609
1610#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1611static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1612{
1613}
1614#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1615
3a167bea
AK
1616static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1617 unsigned int id)
de56a948
PM
1618{
1619 struct kvm_vcpu *vcpu;
371fefd6
PM
1620 int err = -EINVAL;
1621 int core;
1622 struct kvmppc_vcore *vcore;
de56a948 1623
3102f784 1624 core = id / threads_per_subcore;
371fefd6
PM
1625 if (core >= KVM_MAX_VCORES)
1626 goto out;
1627
1628 err = -ENOMEM;
6b75e6bf 1629 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
de56a948
PM
1630 if (!vcpu)
1631 goto out;
1632
1633 err = kvm_vcpu_init(vcpu, kvm, id);
1634 if (err)
1635 goto free_vcpu;
1636
1637 vcpu->arch.shared = &vcpu->arch.shregs;
5deb8e7a
AG
1638#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1639 /*
1640 * The shared struct is never shared on HV,
1641 * so we can always use host endianness
1642 */
1643#ifdef __BIG_ENDIAN__
1644 vcpu->arch.shared_big_endian = true;
1645#else
1646 vcpu->arch.shared_big_endian = false;
1647#endif
1648#endif
de56a948
PM
1649 vcpu->arch.mmcr[0] = MMCR0_FC;
1650 vcpu->arch.ctrl = CTRL_RUNLATCH;
1651 /* default to host PVR, since we can't spoof it */
3a167bea 1652 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
2e25aa5f 1653 spin_lock_init(&vcpu->arch.vpa_update_lock);
c7b67670
PM
1654 spin_lock_init(&vcpu->arch.tbacct_lock);
1655 vcpu->arch.busy_preempt = TB_NIL;
d682916a 1656 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
de56a948 1657
de56a948
PM
1658 kvmppc_mmu_book3s_hv_init(vcpu);
1659
8455d79e 1660 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
371fefd6
PM
1661
1662 init_waitqueue_head(&vcpu->arch.cpu_run);
1663
1664 mutex_lock(&kvm->lock);
1665 vcore = kvm->arch.vcores[core];
1666 if (!vcore) {
de9bdd1a 1667 vcore = kvmppc_vcore_create(kvm, core);
371fefd6 1668 kvm->arch.vcores[core] = vcore;
1b400ba0 1669 kvm->arch.online_vcores++;
371fefd6
PM
1670 }
1671 mutex_unlock(&kvm->lock);
1672
1673 if (!vcore)
1674 goto free_vcpu;
1675
1676 spin_lock(&vcore->lock);
1677 ++vcore->num_threads;
371fefd6
PM
1678 spin_unlock(&vcore->lock);
1679 vcpu->arch.vcore = vcore;
e0b7ec05 1680 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
ec257165 1681 vcpu->arch.thread_cpu = -1;
371fefd6 1682
af8f38b3
AG
1683 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1684 kvmppc_sanity_check(vcpu);
1685
b6c295df
PM
1686 debugfs_vcpu_init(vcpu, id);
1687
de56a948
PM
1688 return vcpu;
1689
1690free_vcpu:
6b75e6bf 1691 kmem_cache_free(kvm_vcpu_cache, vcpu);
de56a948
PM
1692out:
1693 return ERR_PTR(err);
1694}
1695
c35635ef
PM
1696static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1697{
1698 if (vpa->pinned_addr)
1699 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1700 vpa->dirty);
1701}
1702
3a167bea 1703static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
de56a948 1704{
2e25aa5f 1705 spin_lock(&vcpu->arch.vpa_update_lock);
c35635ef
PM
1706 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1707 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1708 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
2e25aa5f 1709 spin_unlock(&vcpu->arch.vpa_update_lock);
de56a948 1710 kvm_vcpu_uninit(vcpu);
6b75e6bf 1711 kmem_cache_free(kvm_vcpu_cache, vcpu);
de56a948
PM
1712}
1713
3a167bea
AK
1714static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1715{
1716 /* Indicate we want to get back into the guest */
1717 return 1;
1718}
1719
19ccb76a 1720static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
371fefd6 1721{
19ccb76a 1722 unsigned long dec_nsec, now;
371fefd6 1723
19ccb76a
PM
1724 now = get_tb();
1725 if (now > vcpu->arch.dec_expires) {
1726 /* decrementer has already gone negative */
1727 kvmppc_core_queue_dec(vcpu);
7e28e60e 1728 kvmppc_core_prepare_to_enter(vcpu);
19ccb76a 1729 return;
371fefd6 1730 }
19ccb76a
PM
1731 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1732 / tb_ticks_per_sec;
1733 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1734 HRTIMER_MODE_REL);
1735 vcpu->arch.timer_running = 1;
371fefd6
PM
1736}
1737
19ccb76a 1738static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
371fefd6 1739{
19ccb76a
PM
1740 vcpu->arch.ceded = 0;
1741 if (vcpu->arch.timer_running) {
1742 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1743 vcpu->arch.timer_running = 0;
1744 }
371fefd6
PM
1745}
1746
e0b7ec05 1747extern void __kvmppc_vcore_entry(void);
de56a948 1748
371fefd6
PM
1749static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1750 struct kvm_vcpu *vcpu)
de56a948 1751{
c7b67670
PM
1752 u64 now;
1753
371fefd6
PM
1754 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1755 return;
bf3d32e1 1756 spin_lock_irq(&vcpu->arch.tbacct_lock);
c7b67670
PM
1757 now = mftb();
1758 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1759 vcpu->arch.stolen_logged;
1760 vcpu->arch.busy_preempt = now;
1761 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
bf3d32e1 1762 spin_unlock_irq(&vcpu->arch.tbacct_lock);
371fefd6 1763 --vc->n_runnable;
371fefd6
PM
1764 list_del(&vcpu->arch.run_list);
1765}
1766
f0888f70
PM
1767static int kvmppc_grab_hwthread(int cpu)
1768{
1769 struct paca_struct *tpaca;
b754c739 1770 long timeout = 10000;
f0888f70
PM
1771
1772 tpaca = &paca[cpu];
1773
1774 /* Ensure the thread won't go into the kernel if it wakes */
7b444c67 1775 tpaca->kvm_hstate.kvm_vcpu = NULL;
b4deba5c 1776 tpaca->kvm_hstate.kvm_vcore = NULL;
5d5b99cd
PM
1777 tpaca->kvm_hstate.napping = 0;
1778 smp_wmb();
1779 tpaca->kvm_hstate.hwthread_req = 1;
f0888f70
PM
1780
1781 /*
1782 * If the thread is already executing in the kernel (e.g. handling
1783 * a stray interrupt), wait for it to get back to nap mode.
1784 * The smp_mb() is to ensure that our setting of hwthread_req
1785 * is visible before we look at hwthread_state, so if this
1786 * races with the code at system_reset_pSeries and the thread
1787 * misses our setting of hwthread_req, we are sure to see its
1788 * setting of hwthread_state, and vice versa.
1789 */
1790 smp_mb();
1791 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1792 if (--timeout <= 0) {
1793 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1794 return -EBUSY;
1795 }
1796 udelay(1);
1797 }
1798 return 0;
1799}
1800
1801static void kvmppc_release_hwthread(int cpu)
1802{
1803 struct paca_struct *tpaca;
1804
1805 tpaca = &paca[cpu];
1806 tpaca->kvm_hstate.hwthread_req = 0;
1807 tpaca->kvm_hstate.kvm_vcpu = NULL;
b4deba5c
PM
1808 tpaca->kvm_hstate.kvm_vcore = NULL;
1809 tpaca->kvm_hstate.kvm_split_mode = NULL;
f0888f70
PM
1810}
1811
b4deba5c 1812static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
371fefd6
PM
1813{
1814 int cpu;
1815 struct paca_struct *tpaca;
ec257165 1816 struct kvmppc_vcore *mvc = vc->master_vcore;
371fefd6 1817
b4deba5c
PM
1818 cpu = vc->pcpu;
1819 if (vcpu) {
1820 if (vcpu->arch.timer_running) {
1821 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1822 vcpu->arch.timer_running = 0;
1823 }
1824 cpu += vcpu->arch.ptid;
1825 vcpu->cpu = mvc->pcpu;
1826 vcpu->arch.thread_cpu = cpu;
19ccb76a 1827 }
371fefd6 1828 tpaca = &paca[cpu];
b4deba5c 1829 tpaca->kvm_hstate.kvm_vcpu = vcpu;
ec257165 1830 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
ec257165 1831 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
371fefd6 1832 smp_wmb();
b4deba5c 1833 tpaca->kvm_hstate.kvm_vcore = mvc;
5d5b99cd 1834 if (cpu != smp_processor_id())
66feed61 1835 kvmppc_ipi_thread(cpu);
371fefd6 1836}
de56a948 1837
5d5b99cd 1838static void kvmppc_wait_for_nap(void)
371fefd6 1839{
5d5b99cd
PM
1840 int cpu = smp_processor_id();
1841 int i, loops;
371fefd6 1842
5d5b99cd
PM
1843 for (loops = 0; loops < 1000000; ++loops) {
1844 /*
1845 * Check if all threads are finished.
b4deba5c 1846 * We set the vcore pointer when starting a thread
5d5b99cd 1847 * and the thread clears it when finished, so we look
b4deba5c 1848 * for any threads that still have a non-NULL vcore ptr.
5d5b99cd
PM
1849 */
1850 for (i = 1; i < threads_per_subcore; ++i)
b4deba5c 1851 if (paca[cpu + i].kvm_hstate.kvm_vcore)
5d5b99cd
PM
1852 break;
1853 if (i == threads_per_subcore) {
1854 HMT_medium();
1855 return;
371fefd6 1856 }
5d5b99cd 1857 HMT_low();
371fefd6
PM
1858 }
1859 HMT_medium();
5d5b99cd 1860 for (i = 1; i < threads_per_subcore; ++i)
b4deba5c 1861 if (paca[cpu + i].kvm_hstate.kvm_vcore)
5d5b99cd 1862 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
371fefd6
PM
1863}
1864
1865/*
1866 * Check that we are on thread 0 and that any other threads in
7b444c67
PM
1867 * this core are off-line. Then grab the threads so they can't
1868 * enter the kernel.
371fefd6
PM
1869 */
1870static int on_primary_thread(void)
1871{
1872 int cpu = smp_processor_id();
3102f784 1873 int thr;
371fefd6 1874
3102f784
ME
1875 /* Are we on a primary subcore? */
1876 if (cpu_thread_in_subcore(cpu))
371fefd6 1877 return 0;
3102f784
ME
1878
1879 thr = 0;
1880 while (++thr < threads_per_subcore)
371fefd6
PM
1881 if (cpu_online(cpu + thr))
1882 return 0;
7b444c67
PM
1883
1884 /* Grab all hw threads so they can't go into the kernel */
3102f784 1885 for (thr = 1; thr < threads_per_subcore; ++thr) {
7b444c67
PM
1886 if (kvmppc_grab_hwthread(cpu + thr)) {
1887 /* Couldn't grab one; let the others go */
1888 do {
1889 kvmppc_release_hwthread(cpu + thr);
1890 } while (--thr > 0);
1891 return 0;
1892 }
1893 }
371fefd6
PM
1894 return 1;
1895}
1896
9678cdaa
SS
1897static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1898{
1899 phys_addr_t phy_addr, mpp_addr;
1900
1901 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1902 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1903
1904 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1905 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1906
1907 vc->mpp_buffer_is_valid = true;
1908}
1909
1910static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1911{
1912 phys_addr_t phy_addr, mpp_addr;
1913
1914 phy_addr = virt_to_phys(vc->mpp_buffer);
1915 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1916
1917 /* We must abort any in-progress save operations to ensure
1918 * the table is valid so that prefetch engine knows when to
1919 * stop prefetching. */
1920 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1921 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1922}
1923
ec257165
PM
1924/*
1925 * A list of virtual cores for each physical CPU.
1926 * These are vcores that could run but their runner VCPU tasks are
1927 * (or may be) preempted.
1928 */
1929struct preempted_vcore_list {
1930 struct list_head list;
1931 spinlock_t lock;
1932};
1933
1934static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1935
1936static void init_vcore_lists(void)
1937{
1938 int cpu;
1939
1940 for_each_possible_cpu(cpu) {
1941 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1942 spin_lock_init(&lp->lock);
1943 INIT_LIST_HEAD(&lp->list);
1944 }
1945}
1946
1947static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1948{
1949 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1950
1951 vc->vcore_state = VCORE_PREEMPT;
1952 vc->pcpu = smp_processor_id();
1953 if (vc->num_threads < threads_per_subcore) {
1954 spin_lock(&lp->lock);
1955 list_add_tail(&vc->preempt_list, &lp->list);
1956 spin_unlock(&lp->lock);
1957 }
1958
1959 /* Start accumulating stolen time */
1960 kvmppc_core_start_stolen(vc);
1961}
1962
1963static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
1964{
402813fe 1965 struct preempted_vcore_list *lp;
ec257165
PM
1966
1967 kvmppc_core_end_stolen(vc);
1968 if (!list_empty(&vc->preempt_list)) {
402813fe 1969 lp = &per_cpu(preempted_vcores, vc->pcpu);
ec257165
PM
1970 spin_lock(&lp->lock);
1971 list_del_init(&vc->preempt_list);
1972 spin_unlock(&lp->lock);
1973 }
1974 vc->vcore_state = VCORE_INACTIVE;
1975}
1976
b4deba5c
PM
1977/*
1978 * This stores information about the virtual cores currently
1979 * assigned to a physical core.
1980 */
ec257165 1981struct core_info {
b4deba5c
PM
1982 int n_subcores;
1983 int max_subcore_threads;
ec257165 1984 int total_threads;
b4deba5c
PM
1985 int subcore_threads[MAX_SUBCORES];
1986 struct kvm *subcore_vm[MAX_SUBCORES];
1987 struct list_head vcs[MAX_SUBCORES];
ec257165
PM
1988};
1989
b4deba5c
PM
1990/*
1991 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
1992 * respectively in 2-way micro-threading (split-core) mode.
1993 */
1994static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
1995
ec257165
PM
1996static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
1997{
b4deba5c
PM
1998 int sub;
1999
ec257165 2000 memset(cip, 0, sizeof(*cip));
b4deba5c
PM
2001 cip->n_subcores = 1;
2002 cip->max_subcore_threads = vc->num_threads;
ec257165 2003 cip->total_threads = vc->num_threads;
b4deba5c
PM
2004 cip->subcore_threads[0] = vc->num_threads;
2005 cip->subcore_vm[0] = vc->kvm;
2006 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2007 INIT_LIST_HEAD(&cip->vcs[sub]);
2008 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2009}
2010
2011static bool subcore_config_ok(int n_subcores, int n_threads)
2012{
2013 /* Can only dynamically split if unsplit to begin with */
2014 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2015 return false;
2016 if (n_subcores > MAX_SUBCORES)
2017 return false;
2018 if (n_subcores > 1) {
2019 if (!(dynamic_mt_modes & 2))
2020 n_subcores = 4;
2021 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2022 return false;
2023 }
2024
2025 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
ec257165
PM
2026}
2027
2028static void init_master_vcore(struct kvmppc_vcore *vc)
2029{
2030 vc->master_vcore = vc;
2031 vc->entry_exit_map = 0;
2032 vc->in_guest = 0;
2033 vc->napping_threads = 0;
2034 vc->conferring_threads = 0;
2035}
2036
2037/*
b4deba5c
PM
2038 * See if the existing subcores can be split into 3 (or fewer) subcores
2039 * of at most two threads each, so we can fit in another vcore. This
2040 * assumes there are at most two subcores and at most 6 threads in total.
ec257165 2041 */
b4deba5c
PM
2042static bool can_split_piggybacked_subcores(struct core_info *cip)
2043{
2044 int sub, new_sub;
2045 int large_sub = -1;
2046 int thr;
2047 int n_subcores = cip->n_subcores;
2048 struct kvmppc_vcore *vc, *vcnext;
2049 struct kvmppc_vcore *master_vc = NULL;
2050
2051 for (sub = 0; sub < cip->n_subcores; ++sub) {
2052 if (cip->subcore_threads[sub] <= 2)
2053 continue;
2054 if (large_sub >= 0)
2055 return false;
2056 large_sub = sub;
2057 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2058 preempt_list);
2059 if (vc->num_threads > 2)
2060 return false;
2061 n_subcores += (cip->subcore_threads[sub] - 1) >> 1;
2062 }
2063 if (n_subcores > 3 || large_sub < 0)
2064 return false;
2065
2066 /*
2067 * Seems feasible, so go through and move vcores to new subcores.
2068 * Note that when we have two or more vcores in one subcore,
2069 * all those vcores must have only one thread each.
2070 */
2071 new_sub = cip->n_subcores;
2072 thr = 0;
2073 sub = large_sub;
2074 list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) {
2075 if (thr >= 2) {
2076 list_del(&vc->preempt_list);
2077 list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]);
2078 /* vc->num_threads must be 1 */
2079 if (++cip->subcore_threads[new_sub] == 1) {
2080 cip->subcore_vm[new_sub] = vc->kvm;
2081 init_master_vcore(vc);
2082 master_vc = vc;
2083 ++cip->n_subcores;
2084 } else {
2085 vc->master_vcore = master_vc;
2086 ++new_sub;
2087 }
2088 }
2089 thr += vc->num_threads;
2090 }
2091 cip->subcore_threads[large_sub] = 2;
2092 cip->max_subcore_threads = 2;
2093
2094 return true;
2095}
2096
2097static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2098{
2099 int n_threads = vc->num_threads;
2100 int sub;
2101
2102 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2103 return false;
2104
2105 if (n_threads < cip->max_subcore_threads)
2106 n_threads = cip->max_subcore_threads;
2107 if (subcore_config_ok(cip->n_subcores + 1, n_threads)) {
2108 cip->max_subcore_threads = n_threads;
2109 } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 &&
2110 vc->num_threads <= 2) {
2111 /*
2112 * We may be able to fit another subcore in by
2113 * splitting an existing subcore with 3 or 4
2114 * threads into two 2-thread subcores, or one
2115 * with 5 or 6 threads into three subcores.
2116 * We can only do this if those subcores have
2117 * piggybacked virtual cores.
2118 */
2119 if (!can_split_piggybacked_subcores(cip))
2120 return false;
2121 } else {
2122 return false;
2123 }
2124
2125 sub = cip->n_subcores;
2126 ++cip->n_subcores;
2127 cip->total_threads += vc->num_threads;
2128 cip->subcore_threads[sub] = vc->num_threads;
2129 cip->subcore_vm[sub] = vc->kvm;
2130 init_master_vcore(vc);
2131 list_del(&vc->preempt_list);
2132 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2133
2134 return true;
2135}
2136
2137static bool can_piggyback_subcore(struct kvmppc_vcore *pvc,
2138 struct core_info *cip, int sub)
ec257165
PM
2139{
2140 struct kvmppc_vcore *vc;
b4deba5c 2141 int n_thr;
ec257165 2142
b4deba5c
PM
2143 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2144 preempt_list);
ec257165
PM
2145
2146 /* require same VM and same per-core reg values */
2147 if (pvc->kvm != vc->kvm ||
2148 pvc->tb_offset != vc->tb_offset ||
2149 pvc->pcr != vc->pcr ||
2150 pvc->lpcr != vc->lpcr)
2151 return false;
2152
2153 /* P8 guest with > 1 thread per core would see wrong TIR value */
2154 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2155 (vc->num_threads > 1 || pvc->num_threads > 1))
2156 return false;
2157
b4deba5c
PM
2158 n_thr = cip->subcore_threads[sub] + pvc->num_threads;
2159 if (n_thr > cip->max_subcore_threads) {
2160 if (!subcore_config_ok(cip->n_subcores, n_thr))
2161 return false;
2162 cip->max_subcore_threads = n_thr;
2163 }
ec257165
PM
2164
2165 cip->total_threads += pvc->num_threads;
b4deba5c 2166 cip->subcore_threads[sub] = n_thr;
ec257165
PM
2167 pvc->master_vcore = vc;
2168 list_del(&pvc->preempt_list);
b4deba5c 2169 list_add_tail(&pvc->preempt_list, &cip->vcs[sub]);
ec257165
PM
2170
2171 return true;
2172}
2173
b4deba5c
PM
2174/*
2175 * Work out whether it is possible to piggyback the execution of
2176 * vcore *pvc onto the execution of the other vcores described in *cip.
2177 */
2178static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2179 int target_threads)
2180{
2181 int sub;
2182
2183 if (cip->total_threads + pvc->num_threads > target_threads)
2184 return false;
2185 for (sub = 0; sub < cip->n_subcores; ++sub)
2186 if (cip->subcore_threads[sub] &&
2187 can_piggyback_subcore(pvc, cip, sub))
2188 return true;
2189
2190 if (can_dynamic_split(pvc, cip))
2191 return true;
2192
2193 return false;
2194}
2195
d911f0be
PM
2196static void prepare_threads(struct kvmppc_vcore *vc)
2197{
2198 struct kvm_vcpu *vcpu, *vnext;
2199
2200 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2201 arch.run_list) {
2202 if (signal_pending(vcpu->arch.run_task))
2203 vcpu->arch.ret = -EINTR;
2204 else if (vcpu->arch.vpa.update_pending ||
2205 vcpu->arch.slb_shadow.update_pending ||
2206 vcpu->arch.dtl.update_pending)
2207 vcpu->arch.ret = RESUME_GUEST;
2208 else
2209 continue;
2210 kvmppc_remove_runnable(vc, vcpu);
2211 wake_up(&vcpu->arch.cpu_run);
2212 }
2213}
2214
ec257165
PM
2215static void collect_piggybacks(struct core_info *cip, int target_threads)
2216{
2217 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2218 struct kvmppc_vcore *pvc, *vcnext;
2219
2220 spin_lock(&lp->lock);
2221 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2222 if (!spin_trylock(&pvc->lock))
2223 continue;
2224 prepare_threads(pvc);
2225 if (!pvc->n_runnable) {
2226 list_del_init(&pvc->preempt_list);
2227 if (pvc->runner == NULL) {
2228 pvc->vcore_state = VCORE_INACTIVE;
2229 kvmppc_core_end_stolen(pvc);
2230 }
2231 spin_unlock(&pvc->lock);
2232 continue;
2233 }
2234 if (!can_piggyback(pvc, cip, target_threads)) {
2235 spin_unlock(&pvc->lock);
2236 continue;
2237 }
2238 kvmppc_core_end_stolen(pvc);
2239 pvc->vcore_state = VCORE_PIGGYBACK;
2240 if (cip->total_threads >= target_threads)
2241 break;
2242 }
2243 spin_unlock(&lp->lock);
2244}
2245
2246static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
25fedfca 2247{
ec257165 2248 int still_running = 0;
25fedfca
PM
2249 u64 now;
2250 long ret;
2251 struct kvm_vcpu *vcpu, *vnext;
2252
ec257165 2253 spin_lock(&vc->lock);
25fedfca
PM
2254 now = get_tb();
2255 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2256 arch.run_list) {
2257 /* cancel pending dec exception if dec is positive */
2258 if (now < vcpu->arch.dec_expires &&
2259 kvmppc_core_pending_dec(vcpu))
2260 kvmppc_core_dequeue_dec(vcpu);
2261
2262 trace_kvm_guest_exit(vcpu);
2263
2264 ret = RESUME_GUEST;
2265 if (vcpu->arch.trap)
2266 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2267 vcpu->arch.run_task);
2268
2269 vcpu->arch.ret = ret;
2270 vcpu->arch.trap = 0;
2271
ec257165
PM
2272 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2273 if (vcpu->arch.pending_exceptions)
2274 kvmppc_core_prepare_to_enter(vcpu);
2275 if (vcpu->arch.ceded)
25fedfca 2276 kvmppc_set_timer(vcpu);
ec257165
PM
2277 else
2278 ++still_running;
2279 } else {
25fedfca
PM
2280 kvmppc_remove_runnable(vc, vcpu);
2281 wake_up(&vcpu->arch.cpu_run);
2282 }
2283 }
ec257165
PM
2284 list_del_init(&vc->preempt_list);
2285 if (!is_master) {
2286 vc->vcore_state = vc->runner ? VCORE_PREEMPT : VCORE_INACTIVE;
2287 if (still_running > 0)
2288 kvmppc_vcore_preempt(vc);
2289 if (vc->n_runnable > 0 && vc->runner == NULL) {
2290 /* make sure there's a candidate runner awake */
2291 vcpu = list_first_entry(&vc->runnable_threads,
2292 struct kvm_vcpu, arch.run_list);
2293 wake_up(&vcpu->arch.cpu_run);
2294 }
2295 }
2296 spin_unlock(&vc->lock);
25fedfca
PM
2297}
2298
371fefd6
PM
2299/*
2300 * Run a set of guest threads on a physical core.
2301 * Called with vc->lock held.
2302 */
66feed61 2303static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
371fefd6 2304{
17d48901 2305 struct kvm_vcpu *vcpu, *vnext;
d911f0be 2306 int i;
2c9097e4 2307 int srcu_idx;
ec257165
PM
2308 struct core_info core_info;
2309 struct kvmppc_vcore *pvc, *vcnext;
b4deba5c
PM
2310 struct kvm_split_mode split_info, *sip;
2311 int split, subcore_size, active;
2312 int sub;
2313 bool thr0_done;
2314 unsigned long cmd_bit, stat_bit;
ec257165
PM
2315 int pcpu, thr;
2316 int target_threads;
371fefd6 2317
d911f0be
PM
2318 /*
2319 * Remove from the list any threads that have a signal pending
2320 * or need a VPA update done
2321 */
2322 prepare_threads(vc);
2323
2324 /* if the runner is no longer runnable, let the caller pick a new one */
2325 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2326 return;
081f323b
PM
2327
2328 /*
d911f0be 2329 * Initialize *vc.
081f323b 2330 */
ec257165 2331 init_master_vcore(vc);
2711e248 2332 vc->preempt_tb = TB_NIL;
081f323b 2333
7b444c67 2334 /*
3102f784
ME
2335 * Make sure we are running on primary threads, and that secondary
2336 * threads are offline. Also check if the number of threads in this
2337 * guest are greater than the current system threads per guest.
7b444c67 2338 */
3102f784
ME
2339 if ((threads_per_core > 1) &&
2340 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
17d48901
PM
2341 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2342 arch.run_list) {
7b444c67 2343 vcpu->arch.ret = -EBUSY;
25fedfca
PM
2344 kvmppc_remove_runnable(vc, vcpu);
2345 wake_up(&vcpu->arch.cpu_run);
2346 }
7b444c67
PM
2347 goto out;
2348 }
2349
ec257165
PM
2350 /*
2351 * See if we could run any other vcores on the physical core
2352 * along with this one.
2353 */
2354 init_core_info(&core_info, vc);
2355 pcpu = smp_processor_id();
2356 target_threads = threads_per_subcore;
2357 if (target_smt_mode && target_smt_mode < target_threads)
2358 target_threads = target_smt_mode;
2359 if (vc->num_threads < target_threads)
2360 collect_piggybacks(&core_info, target_threads);
3102f784 2361
b4deba5c
PM
2362 /* Decide on micro-threading (split-core) mode */
2363 subcore_size = threads_per_subcore;
2364 cmd_bit = stat_bit = 0;
2365 split = core_info.n_subcores;
2366 sip = NULL;
2367 if (split > 1) {
2368 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2369 if (split == 2 && (dynamic_mt_modes & 2)) {
2370 cmd_bit = HID0_POWER8_1TO2LPAR;
2371 stat_bit = HID0_POWER8_2LPARMODE;
2372 } else {
2373 split = 4;
2374 cmd_bit = HID0_POWER8_1TO4LPAR;
2375 stat_bit = HID0_POWER8_4LPARMODE;
2376 }
2377 subcore_size = MAX_SMT_THREADS / split;
2378 sip = &split_info;
2379 memset(&split_info, 0, sizeof(split_info));
2380 split_info.rpr = mfspr(SPRN_RPR);
2381 split_info.pmmar = mfspr(SPRN_PMMAR);
2382 split_info.ldbar = mfspr(SPRN_LDBAR);
2383 split_info.subcore_size = subcore_size;
2384 for (sub = 0; sub < core_info.n_subcores; ++sub)
2385 split_info.master_vcs[sub] =
2386 list_first_entry(&core_info.vcs[sub],
2387 struct kvmppc_vcore, preempt_list);
2388 /* order writes to split_info before kvm_split_mode pointer */
2389 smp_wmb();
2390 }
2391 pcpu = smp_processor_id();
2392 for (thr = 0; thr < threads_per_subcore; ++thr)
2393 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2394
2395 /* Initiate micro-threading (split-core) if required */
2396 if (cmd_bit) {
2397 unsigned long hid0 = mfspr(SPRN_HID0);
2398
2399 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2400 mb();
2401 mtspr(SPRN_HID0, hid0);
2402 isync();
2403 for (;;) {
2404 hid0 = mfspr(SPRN_HID0);
2405 if (hid0 & stat_bit)
2406 break;
2407 cpu_relax();
ec257165 2408 }
b4deba5c 2409 split_info.do_nap = 1; /* ask secondaries to nap when done */
2e25aa5f 2410 }
371fefd6 2411
b4deba5c
PM
2412 /* Start all the threads */
2413 active = 0;
2414 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2415 thr = subcore_thread_map[sub];
2416 thr0_done = false;
2417 active |= 1 << thr;
2418 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2419 pvc->pcpu = pcpu + thr;
2420 list_for_each_entry(vcpu, &pvc->runnable_threads,
2421 arch.run_list) {
2422 kvmppc_start_thread(vcpu, pvc);
2423 kvmppc_create_dtl_entry(vcpu, pvc);
2424 trace_kvm_guest_enter(vcpu);
2425 if (!vcpu->arch.ptid)
2426 thr0_done = true;
2427 active |= 1 << (thr + vcpu->arch.ptid);
2428 }
2429 /*
2430 * We need to start the first thread of each subcore
2431 * even if it doesn't have a vcpu.
2432 */
2433 if (pvc->master_vcore == pvc && !thr0_done)
2434 kvmppc_start_thread(NULL, pvc);
2435 thr += pvc->num_threads;
2436 }
2437 }
2438 /*
2439 * When doing micro-threading, poke the inactive threads as well.
2440 * This gets them to the nap instruction after kvm_do_nap,
2441 * which reduces the time taken to unsplit later.
2442 */
2443 if (split > 1)
2444 for (thr = 1; thr < threads_per_subcore; ++thr)
2445 if (!(active & (1 << thr)))
2446 kvmppc_ipi_thread(pcpu + thr);
e0b7ec05 2447
2f12f034 2448 vc->vcore_state = VCORE_RUNNING;
19ccb76a 2449 preempt_disable();
3c78f78a
SW
2450
2451 trace_kvmppc_run_core(vc, 0);
2452
b4deba5c
PM
2453 for (sub = 0; sub < core_info.n_subcores; ++sub)
2454 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2455 spin_unlock(&pvc->lock);
de56a948 2456
371fefd6 2457 kvm_guest_enter();
2c9097e4 2458
e0b7ec05 2459 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
2c9097e4 2460
9678cdaa
SS
2461 if (vc->mpp_buffer_is_valid)
2462 kvmppc_start_restoring_l2_cache(vc);
2463
e0b7ec05 2464 __kvmppc_vcore_entry();
de56a948 2465
9678cdaa
SS
2466 if (vc->mpp_buffer)
2467 kvmppc_start_saving_l2_cache(vc);
2468
ec257165
PM
2469 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2470
2471 spin_lock(&vc->lock);
2472 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
2473 vc->vcore_state = VCORE_EXITING;
2474
19ccb76a 2475 /* wait for secondary threads to finish writing their state to memory */
5d5b99cd 2476 kvmppc_wait_for_nap();
b4deba5c
PM
2477
2478 /* Return to whole-core mode if we split the core earlier */
2479 if (split > 1) {
2480 unsigned long hid0 = mfspr(SPRN_HID0);
2481 unsigned long loops = 0;
2482
2483 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2484 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2485 mb();
2486 mtspr(SPRN_HID0, hid0);
2487 isync();
2488 for (;;) {
2489 hid0 = mfspr(SPRN_HID0);
2490 if (!(hid0 & stat_bit))
2491 break;
2492 cpu_relax();
2493 ++loops;
2494 }
2495 split_info.do_nap = 0;
2496 }
2497
2498 /* Let secondaries go back to the offline loop */
2499 for (i = 0; i < threads_per_subcore; ++i) {
2500 kvmppc_release_hwthread(pcpu + i);
2501 if (sip && sip->napped[i])
2502 kvmppc_ipi_thread(pcpu + i);
2503 }
2504
371fefd6
PM
2505 spin_unlock(&vc->lock);
2506
2507 /* make sure updates to secondary vcpu structs are visible now */
2508 smp_mb();
de56a948
PM
2509 kvm_guest_exit();
2510
b4deba5c
PM
2511 for (sub = 0; sub < core_info.n_subcores; ++sub)
2512 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2513 preempt_list)
2514 post_guest_process(pvc, pvc == vc);
de56a948 2515
913d3ff9 2516 spin_lock(&vc->lock);
ec257165 2517 preempt_enable();
de56a948
PM
2518
2519 out:
19ccb76a 2520 vc->vcore_state = VCORE_INACTIVE;
3c78f78a 2521 trace_kvmppc_run_core(vc, 1);
371fefd6
PM
2522}
2523
19ccb76a
PM
2524/*
2525 * Wait for some other vcpu thread to execute us, and
2526 * wake us up when we need to handle something in the host.
2527 */
ec257165
PM
2528static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2529 struct kvm_vcpu *vcpu, int wait_state)
371fefd6 2530{
371fefd6
PM
2531 DEFINE_WAIT(wait);
2532
19ccb76a 2533 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
ec257165
PM
2534 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2535 spin_unlock(&vc->lock);
19ccb76a 2536 schedule();
ec257165
PM
2537 spin_lock(&vc->lock);
2538 }
19ccb76a
PM
2539 finish_wait(&vcpu->arch.cpu_run, &wait);
2540}
2541
2542/*
2543 * All the vcpus in this vcore are idle, so wait for a decrementer
2544 * or external interrupt to one of the vcpus. vc->lock is held.
2545 */
2546static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2547{
1bc5d59c
SW
2548 struct kvm_vcpu *vcpu;
2549 int do_sleep = 1;
2550
19ccb76a 2551 DEFINE_WAIT(wait);
19ccb76a
PM
2552
2553 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1bc5d59c
SW
2554
2555 /*
2556 * Check one last time for pending exceptions and ceded state after
2557 * we put ourselves on the wait queue
2558 */
2559 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2560 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2561 do_sleep = 0;
2562 break;
2563 }
2564 }
2565
2566 if (!do_sleep) {
2567 finish_wait(&vc->wq, &wait);
2568 return;
2569 }
2570
19ccb76a 2571 vc->vcore_state = VCORE_SLEEPING;
3c78f78a 2572 trace_kvmppc_vcore_blocked(vc, 0);
19ccb76a 2573 spin_unlock(&vc->lock);
913d3ff9 2574 schedule();
19ccb76a
PM
2575 finish_wait(&vc->wq, &wait);
2576 spin_lock(&vc->lock);
2577 vc->vcore_state = VCORE_INACTIVE;
3c78f78a 2578 trace_kvmppc_vcore_blocked(vc, 1);
19ccb76a 2579}
371fefd6 2580
19ccb76a
PM
2581static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2582{
2583 int n_ceded;
19ccb76a
PM
2584 struct kvmppc_vcore *vc;
2585 struct kvm_vcpu *v, *vn;
9e368f29 2586
3c78f78a
SW
2587 trace_kvmppc_run_vcpu_enter(vcpu);
2588
371fefd6
PM
2589 kvm_run->exit_reason = 0;
2590 vcpu->arch.ret = RESUME_GUEST;
2591 vcpu->arch.trap = 0;
2f12f034 2592 kvmppc_update_vpas(vcpu);
371fefd6 2593
371fefd6
PM
2594 /*
2595 * Synchronize with other threads in this virtual core
2596 */
2597 vc = vcpu->arch.vcore;
2598 spin_lock(&vc->lock);
19ccb76a 2599 vcpu->arch.ceded = 0;
371fefd6
PM
2600 vcpu->arch.run_task = current;
2601 vcpu->arch.kvm_run = kvm_run;
c7b67670 2602 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
19ccb76a 2603 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
c7b67670 2604 vcpu->arch.busy_preempt = TB_NIL;
371fefd6
PM
2605 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2606 ++vc->n_runnable;
2607
19ccb76a
PM
2608 /*
2609 * This happens the first time this is called for a vcpu.
2610 * If the vcore is already running, we may be able to start
2611 * this thread straight away and have it join in.
2612 */
8455d79e 2613 if (!signal_pending(current)) {
ec257165
PM
2614 if (vc->vcore_state == VCORE_PIGGYBACK) {
2615 struct kvmppc_vcore *mvc = vc->master_vcore;
2616 if (spin_trylock(&mvc->lock)) {
2617 if (mvc->vcore_state == VCORE_RUNNING &&
2618 !VCORE_IS_EXITING(mvc)) {
2619 kvmppc_create_dtl_entry(vcpu, vc);
b4deba5c 2620 kvmppc_start_thread(vcpu, vc);
ec257165
PM
2621 trace_kvm_guest_enter(vcpu);
2622 }
2623 spin_unlock(&mvc->lock);
2624 }
2625 } else if (vc->vcore_state == VCORE_RUNNING &&
2626 !VCORE_IS_EXITING(vc)) {
2f12f034 2627 kvmppc_create_dtl_entry(vcpu, vc);
b4deba5c 2628 kvmppc_start_thread(vcpu, vc);
3c78f78a 2629 trace_kvm_guest_enter(vcpu);
8455d79e
PM
2630 } else if (vc->vcore_state == VCORE_SLEEPING) {
2631 wake_up(&vc->wq);
371fefd6
PM
2632 }
2633
8455d79e 2634 }
371fefd6 2635
19ccb76a
PM
2636 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2637 !signal_pending(current)) {
ec257165
PM
2638 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2639 kvmppc_vcore_end_preempt(vc);
2640
8455d79e 2641 if (vc->vcore_state != VCORE_INACTIVE) {
ec257165 2642 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
19ccb76a
PM
2643 continue;
2644 }
19ccb76a
PM
2645 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2646 arch.run_list) {
7e28e60e 2647 kvmppc_core_prepare_to_enter(v);
19ccb76a
PM
2648 if (signal_pending(v->arch.run_task)) {
2649 kvmppc_remove_runnable(vc, v);
2650 v->stat.signal_exits++;
2651 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2652 v->arch.ret = -EINTR;
2653 wake_up(&v->arch.cpu_run);
2654 }
2655 }
8455d79e
PM
2656 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2657 break;
8455d79e 2658 n_ceded = 0;
4619ac88 2659 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
8455d79e
PM
2660 if (!v->arch.pending_exceptions)
2661 n_ceded += v->arch.ceded;
4619ac88
PM
2662 else
2663 v->arch.ceded = 0;
2664 }
25fedfca
PM
2665 vc->runner = vcpu;
2666 if (n_ceded == vc->n_runnable) {
8455d79e 2667 kvmppc_vcore_blocked(vc);
25fedfca 2668 } else if (should_resched()) {
ec257165 2669 kvmppc_vcore_preempt(vc);
25fedfca
PM
2670 /* Let something else run */
2671 cond_resched_lock(&vc->lock);
ec257165
PM
2672 if (vc->vcore_state == VCORE_PREEMPT)
2673 kvmppc_vcore_end_preempt(vc);
25fedfca 2674 } else {
8455d79e 2675 kvmppc_run_core(vc);
25fedfca 2676 }
0456ec4f 2677 vc->runner = NULL;
19ccb76a 2678 }
371fefd6 2679
8455d79e
PM
2680 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2681 (vc->vcore_state == VCORE_RUNNING ||
ec257165
PM
2682 vc->vcore_state == VCORE_EXITING))
2683 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
8455d79e
PM
2684
2685 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2686 kvmppc_remove_runnable(vc, vcpu);
2687 vcpu->stat.signal_exits++;
2688 kvm_run->exit_reason = KVM_EXIT_INTR;
2689 vcpu->arch.ret = -EINTR;
2690 }
2691
2692 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2693 /* Wake up some vcpu to run the core */
2694 v = list_first_entry(&vc->runnable_threads,
2695 struct kvm_vcpu, arch.run_list);
2696 wake_up(&v->arch.cpu_run);
371fefd6
PM
2697 }
2698
3c78f78a 2699 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
371fefd6 2700 spin_unlock(&vc->lock);
371fefd6 2701 return vcpu->arch.ret;
de56a948
PM
2702}
2703
3a167bea 2704static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
a8606e20
PM
2705{
2706 int r;
913d3ff9 2707 int srcu_idx;
a8606e20 2708
af8f38b3
AG
2709 if (!vcpu->arch.sane) {
2710 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2711 return -EINVAL;
2712 }
2713
25051b5a
SW
2714 kvmppc_core_prepare_to_enter(vcpu);
2715
19ccb76a
PM
2716 /* No need to go into the guest when all we'll do is come back out */
2717 if (signal_pending(current)) {
2718 run->exit_reason = KVM_EXIT_INTR;
2719 return -EINTR;
2720 }
2721
32fad281 2722 atomic_inc(&vcpu->kvm->arch.vcpus_running);
31037eca 2723 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
32fad281
PM
2724 smp_mb();
2725
c17b98cf 2726 /* On the first time here, set up HTAB and VRMA */
31037eca 2727 if (!vcpu->kvm->arch.hpte_setup_done) {
32fad281 2728 r = kvmppc_hv_setup_htab_rma(vcpu);
c77162de 2729 if (r)
32fad281 2730 goto out;
c77162de 2731 }
19ccb76a
PM
2732
2733 flush_fp_to_thread(current);
2734 flush_altivec_to_thread(current);
2735 flush_vsx_to_thread(current);
2736 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
342d3db7 2737 vcpu->arch.pgdir = current->mm->pgd;
c7b67670 2738 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
19ccb76a 2739
a8606e20
PM
2740 do {
2741 r = kvmppc_run_vcpu(run, vcpu);
2742
2743 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2744 !(vcpu->arch.shregs.msr & MSR_PR)) {
3c78f78a 2745 trace_kvm_hcall_enter(vcpu);
a8606e20 2746 r = kvmppc_pseries_do_hcall(vcpu);
3c78f78a 2747 trace_kvm_hcall_exit(vcpu, r);
7e28e60e 2748 kvmppc_core_prepare_to_enter(vcpu);
913d3ff9
PM
2749 } else if (r == RESUME_PAGE_FAULT) {
2750 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2751 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2752 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2753 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
a8606e20 2754 }
e59d24e6 2755 } while (is_kvmppc_resume_guest(r));
32fad281
PM
2756
2757 out:
c7b67670 2758 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
32fad281 2759 atomic_dec(&vcpu->kvm->arch.vcpus_running);
a8606e20
PM
2760 return r;
2761}
2762
5b74716e
BH
2763static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2764 int linux_psize)
2765{
2766 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2767
2768 if (!def->shift)
2769 return;
2770 (*sps)->page_shift = def->shift;
2771 (*sps)->slb_enc = def->sllp;
2772 (*sps)->enc[0].page_shift = def->shift;
b1022fbd 2773 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
1f365bb0
AK
2774 /*
2775 * Add 16MB MPSS support if host supports it
2776 */
2777 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2778 (*sps)->enc[1].page_shift = 24;
2779 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2780 }
5b74716e
BH
2781 (*sps)++;
2782}
2783
3a167bea
AK
2784static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2785 struct kvm_ppc_smmu_info *info)
5b74716e
BH
2786{
2787 struct kvm_ppc_one_seg_page_size *sps;
2788
2789 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2790 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2791 info->flags |= KVM_PPC_1T_SEGMENTS;
2792 info->slb_size = mmu_slb_size;
2793
2794 /* We only support these sizes for now, and no muti-size segments */
2795 sps = &info->sps[0];
2796 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2797 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2798 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2799
2800 return 0;
2801}
2802
82ed3616
PM
2803/*
2804 * Get (and clear) the dirty memory log for a memory slot.
2805 */
3a167bea
AK
2806static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2807 struct kvm_dirty_log *log)
82ed3616 2808{
9f6b8029 2809 struct kvm_memslots *slots;
82ed3616
PM
2810 struct kvm_memory_slot *memslot;
2811 int r;
2812 unsigned long n;
2813
2814 mutex_lock(&kvm->slots_lock);
2815
2816 r = -EINVAL;
bbacc0c1 2817 if (log->slot >= KVM_USER_MEM_SLOTS)
82ed3616
PM
2818 goto out;
2819
9f6b8029
PB
2820 slots = kvm_memslots(kvm);
2821 memslot = id_to_memslot(slots, log->slot);
82ed3616
PM
2822 r = -ENOENT;
2823 if (!memslot->dirty_bitmap)
2824 goto out;
2825
2826 n = kvm_dirty_bitmap_bytes(memslot);
2827 memset(memslot->dirty_bitmap, 0, n);
2828
dfe49dbd 2829 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
82ed3616
PM
2830 if (r)
2831 goto out;
2832
2833 r = -EFAULT;
2834 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2835 goto out;
2836
2837 r = 0;
2838out:
2839 mutex_unlock(&kvm->slots_lock);
2840 return r;
2841}
2842
3a167bea
AK
2843static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2844 struct kvm_memory_slot *dont)
a66b48c3
PM
2845{
2846 if (!dont || free->arch.rmap != dont->arch.rmap) {
2847 vfree(free->arch.rmap);
2848 free->arch.rmap = NULL;
b2b2f165 2849 }
a66b48c3
PM
2850}
2851
3a167bea
AK
2852static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2853 unsigned long npages)
a66b48c3
PM
2854{
2855 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2856 if (!slot->arch.rmap)
2857 return -ENOMEM;
aa04b4cc 2858
c77162de
PM
2859 return 0;
2860}
aa04b4cc 2861
3a167bea
AK
2862static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2863 struct kvm_memory_slot *memslot,
09170a49 2864 const struct kvm_userspace_memory_region *mem)
c77162de 2865{
a66b48c3 2866 return 0;
c77162de
PM
2867}
2868
3a167bea 2869static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
09170a49 2870 const struct kvm_userspace_memory_region *mem,
f36f3f28
PB
2871 const struct kvm_memory_slot *old,
2872 const struct kvm_memory_slot *new)
c77162de 2873{
dfe49dbd 2874 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
9f6b8029 2875 struct kvm_memslots *slots;
dfe49dbd
PM
2876 struct kvm_memory_slot *memslot;
2877
8482644a 2878 if (npages && old->npages) {
dfe49dbd
PM
2879 /*
2880 * If modifying a memslot, reset all the rmap dirty bits.
2881 * If this is a new memslot, we don't need to do anything
2882 * since the rmap array starts out as all zeroes,
2883 * i.e. no pages are dirty.
2884 */
9f6b8029
PB
2885 slots = kvm_memslots(kvm);
2886 memslot = id_to_memslot(slots, mem->slot);
dfe49dbd
PM
2887 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2888 }
c77162de
PM
2889}
2890
a0144e2a
PM
2891/*
2892 * Update LPCR values in kvm->arch and in vcores.
2893 * Caller must hold kvm->lock.
2894 */
2895void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2896{
2897 long int i;
2898 u32 cores_done = 0;
2899
2900 if ((kvm->arch.lpcr & mask) == lpcr)
2901 return;
2902
2903 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2904
2905 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2906 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2907 if (!vc)
2908 continue;
2909 spin_lock(&vc->lock);
2910 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2911 spin_unlock(&vc->lock);
2912 if (++cores_done >= kvm->arch.online_vcores)
2913 break;
2914 }
2915}
2916
3a167bea
AK
2917static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2918{
2919 return;
2920}
2921
32fad281 2922static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
c77162de
PM
2923{
2924 int err = 0;
2925 struct kvm *kvm = vcpu->kvm;
c77162de
PM
2926 unsigned long hva;
2927 struct kvm_memory_slot *memslot;
2928 struct vm_area_struct *vma;
a0144e2a 2929 unsigned long lpcr = 0, senc;
c77162de 2930 unsigned long psize, porder;
2c9097e4 2931 int srcu_idx;
c77162de
PM
2932
2933 mutex_lock(&kvm->lock);
31037eca 2934 if (kvm->arch.hpte_setup_done)
c77162de 2935 goto out; /* another vcpu beat us to it */
aa04b4cc 2936
32fad281
PM
2937 /* Allocate hashed page table (if not done already) and reset it */
2938 if (!kvm->arch.hpt_virt) {
2939 err = kvmppc_alloc_hpt(kvm, NULL);
2940 if (err) {
2941 pr_err("KVM: Couldn't alloc HPT\n");
2942 goto out;
2943 }
2944 }
2945
c77162de 2946 /* Look up the memslot for guest physical address 0 */
2c9097e4 2947 srcu_idx = srcu_read_lock(&kvm->srcu);
c77162de 2948 memslot = gfn_to_memslot(kvm, 0);
aa04b4cc 2949
c77162de
PM
2950 /* We must have some memory at 0 by now */
2951 err = -EINVAL;
2952 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2c9097e4 2953 goto out_srcu;
c77162de
PM
2954
2955 /* Look up the VMA for the start of this memory slot */
2956 hva = memslot->userspace_addr;
2957 down_read(&current->mm->mmap_sem);
2958 vma = find_vma(current->mm, hva);
2959 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2960 goto up_out;
2961
2962 psize = vma_kernel_pagesize(vma);
da9d1d7f 2963 porder = __ilog2(psize);
c77162de 2964
c77162de
PM
2965 up_read(&current->mm->mmap_sem);
2966
c17b98cf
PM
2967 /* We can handle 4k, 64k or 16M pages in the VRMA */
2968 err = -EINVAL;
2969 if (!(psize == 0x1000 || psize == 0x10000 ||
2970 psize == 0x1000000))
2971 goto out_srcu;
c77162de 2972
c17b98cf
PM
2973 /* Update VRMASD field in the LPCR */
2974 senc = slb_pgsize_encoding(psize);
2975 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2976 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2977 /* the -4 is to account for senc values starting at 0x10 */
2978 lpcr = senc << (LPCR_VRMASD_SH - 4);
c77162de 2979
c17b98cf
PM
2980 /* Create HPTEs in the hash page table for the VRMA */
2981 kvmppc_map_vrma(vcpu, memslot, porder);
aa04b4cc 2982
c17b98cf 2983 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
a0144e2a 2984
31037eca 2985 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
c77162de 2986 smp_wmb();
31037eca 2987 kvm->arch.hpte_setup_done = 1;
c77162de 2988 err = 0;
2c9097e4
PM
2989 out_srcu:
2990 srcu_read_unlock(&kvm->srcu, srcu_idx);
c77162de
PM
2991 out:
2992 mutex_unlock(&kvm->lock);
2993 return err;
b2b2f165 2994
c77162de
PM
2995 up_out:
2996 up_read(&current->mm->mmap_sem);
505d6421 2997 goto out_srcu;
de56a948
PM
2998}
2999
3a167bea 3000static int kvmppc_core_init_vm_hv(struct kvm *kvm)
de56a948 3001{
32fad281 3002 unsigned long lpcr, lpid;
e23a808b 3003 char buf[32];
de56a948 3004
32fad281
PM
3005 /* Allocate the guest's logical partition ID */
3006
3007 lpid = kvmppc_alloc_lpid();
5d226ae5 3008 if ((long)lpid < 0)
32fad281
PM
3009 return -ENOMEM;
3010 kvm->arch.lpid = lpid;
de56a948 3011
1b400ba0
PM
3012 /*
3013 * Since we don't flush the TLB when tearing down a VM,
3014 * and this lpid might have previously been used,
3015 * make sure we flush on each core before running the new VM.
3016 */
3017 cpumask_setall(&kvm->arch.need_tlb_flush);
3018
699a0ea0
PM
3019 /* Start out with the default set of hcalls enabled */
3020 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3021 sizeof(kvm->arch.enabled_hcalls));
3022
9e368f29 3023 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
aa04b4cc 3024
c17b98cf
PM
3025 /* Init LPCR for virtual RMA mode */
3026 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3027 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3028 lpcr &= LPCR_PECE | LPCR_LPES;
3029 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3030 LPCR_VPM0 | LPCR_VPM1;
3031 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3032 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3033 /* On POWER8 turn on online bit to enable PURR/SPURR */
3034 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3035 lpcr |= LPCR_ONL;
9e368f29 3036 kvm->arch.lpcr = lpcr;
aa04b4cc 3037
512691d4 3038 /*
441c19c8
ME
3039 * Track that we now have a HV mode VM active. This blocks secondary
3040 * CPU threads from coming online.
512691d4 3041 */
441c19c8 3042 kvm_hv_vm_activated();
512691d4 3043
e23a808b
PM
3044 /*
3045 * Create a debugfs directory for the VM
3046 */
3047 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3048 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3049 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3050 kvmppc_mmu_debugfs_init(kvm);
3051
54738c09 3052 return 0;
de56a948
PM
3053}
3054
f1378b1c
PM
3055static void kvmppc_free_vcores(struct kvm *kvm)
3056{
3057 long int i;
3058
9678cdaa
SS
3059 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3060 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
3061 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3062 free_pages((unsigned long)vc->mpp_buffer,
3063 MPP_BUFFER_ORDER);
3064 }
f1378b1c 3065 kfree(kvm->arch.vcores[i]);
9678cdaa 3066 }
f1378b1c
PM
3067 kvm->arch.online_vcores = 0;
3068}
3069
3a167bea 3070static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
de56a948 3071{
e23a808b
PM
3072 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3073
441c19c8 3074 kvm_hv_vm_deactivated();
512691d4 3075
f1378b1c 3076 kvmppc_free_vcores(kvm);
aa04b4cc 3077
de56a948
PM
3078 kvmppc_free_hpt(kvm);
3079}
3080
3a167bea
AK
3081/* We don't need to emulate any privileged instructions or dcbz */
3082static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3083 unsigned int inst, int *advance)
de56a948 3084{
3a167bea 3085 return EMULATE_FAIL;
de56a948
PM
3086}
3087
3a167bea
AK
3088static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3089 ulong spr_val)
de56a948
PM
3090{
3091 return EMULATE_FAIL;
3092}
3093
3a167bea
AK
3094static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3095 ulong *spr_val)
de56a948
PM
3096{
3097 return EMULATE_FAIL;
3098}
3099
3a167bea 3100static int kvmppc_core_check_processor_compat_hv(void)
de56a948 3101{
c17b98cf
PM
3102 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3103 !cpu_has_feature(CPU_FTR_ARCH_206))
3a167bea
AK
3104 return -EIO;
3105 return 0;
de56a948
PM
3106}
3107
3a167bea
AK
3108static long kvm_arch_vm_ioctl_hv(struct file *filp,
3109 unsigned int ioctl, unsigned long arg)
3110{
3111 struct kvm *kvm __maybe_unused = filp->private_data;
3112 void __user *argp = (void __user *)arg;
3113 long r;
3114
3115 switch (ioctl) {
3116
3a167bea
AK
3117 case KVM_PPC_ALLOCATE_HTAB: {
3118 u32 htab_order;
3119
3120 r = -EFAULT;
3121 if (get_user(htab_order, (u32 __user *)argp))
3122 break;
3123 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3124 if (r)
3125 break;
3126 r = -EFAULT;
3127 if (put_user(htab_order, (u32 __user *)argp))
3128 break;
3129 r = 0;
3130 break;
3131 }
3132
3133 case KVM_PPC_GET_HTAB_FD: {
3134 struct kvm_get_htab_fd ghf;
3135
3136 r = -EFAULT;
3137 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3138 break;
3139 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3140 break;
3141 }
3142
3143 default:
3144 r = -ENOTTY;
3145 }
3146
3147 return r;
3148}
3149
699a0ea0
PM
3150/*
3151 * List of hcall numbers to enable by default.
3152 * For compatibility with old userspace, we enable by default
3153 * all hcalls that were implemented before the hcall-enabling
3154 * facility was added. Note this list should not include H_RTAS.
3155 */
3156static unsigned int default_hcall_list[] = {
3157 H_REMOVE,
3158 H_ENTER,
3159 H_READ,
3160 H_PROTECT,
3161 H_BULK_REMOVE,
3162 H_GET_TCE,
3163 H_PUT_TCE,
3164 H_SET_DABR,
3165 H_SET_XDABR,
3166 H_CEDE,
3167 H_PROD,
3168 H_CONFER,
3169 H_REGISTER_VPA,
3170#ifdef CONFIG_KVM_XICS
3171 H_EOI,
3172 H_CPPR,
3173 H_IPI,
3174 H_IPOLL,
3175 H_XIRR,
3176 H_XIRR_X,
3177#endif
3178 0
3179};
3180
3181static void init_default_hcalls(void)
3182{
3183 int i;
ae2113a4 3184 unsigned int hcall;
699a0ea0 3185
ae2113a4
PM
3186 for (i = 0; default_hcall_list[i]; ++i) {
3187 hcall = default_hcall_list[i];
3188 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3189 __set_bit(hcall / 4, default_enabled_hcalls);
3190 }
699a0ea0
PM
3191}
3192
cbbc58d4 3193static struct kvmppc_ops kvm_ops_hv = {
3a167bea
AK
3194 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3195 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3196 .get_one_reg = kvmppc_get_one_reg_hv,
3197 .set_one_reg = kvmppc_set_one_reg_hv,
3198 .vcpu_load = kvmppc_core_vcpu_load_hv,
3199 .vcpu_put = kvmppc_core_vcpu_put_hv,
3200 .set_msr = kvmppc_set_msr_hv,
3201 .vcpu_run = kvmppc_vcpu_run_hv,
3202 .vcpu_create = kvmppc_core_vcpu_create_hv,
3203 .vcpu_free = kvmppc_core_vcpu_free_hv,
3204 .check_requests = kvmppc_core_check_requests_hv,
3205 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3206 .flush_memslot = kvmppc_core_flush_memslot_hv,
3207 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3208 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3209 .unmap_hva = kvm_unmap_hva_hv,
3210 .unmap_hva_range = kvm_unmap_hva_range_hv,
3211 .age_hva = kvm_age_hva_hv,
3212 .test_age_hva = kvm_test_age_hva_hv,
3213 .set_spte_hva = kvm_set_spte_hva_hv,
3214 .mmu_destroy = kvmppc_mmu_destroy_hv,
3215 .free_memslot = kvmppc_core_free_memslot_hv,
3216 .create_memslot = kvmppc_core_create_memslot_hv,
3217 .init_vm = kvmppc_core_init_vm_hv,
3218 .destroy_vm = kvmppc_core_destroy_vm_hv,
3a167bea
AK
3219 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3220 .emulate_op = kvmppc_core_emulate_op_hv,
3221 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3222 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3223 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3224 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
ae2113a4 3225 .hcall_implemented = kvmppc_hcall_impl_hv,
3a167bea
AK
3226};
3227
3228static int kvmppc_book3s_init_hv(void)
de56a948
PM
3229{
3230 int r;
cbbc58d4
AK
3231 /*
3232 * FIXME!! Do we need to check on all cpus ?
3233 */
3234 r = kvmppc_core_check_processor_compat_hv();
3235 if (r < 0)
739e2425 3236 return -ENODEV;
de56a948 3237
cbbc58d4
AK
3238 kvm_ops_hv.owner = THIS_MODULE;
3239 kvmppc_hv_ops = &kvm_ops_hv;
de56a948 3240
699a0ea0
PM
3241 init_default_hcalls();
3242
ec257165
PM
3243 init_vcore_lists();
3244
cbbc58d4 3245 r = kvmppc_mmu_hv_init();
de56a948
PM
3246 return r;
3247}
3248
3a167bea 3249static void kvmppc_book3s_exit_hv(void)
de56a948 3250{
cbbc58d4 3251 kvmppc_hv_ops = NULL;
de56a948
PM
3252}
3253
3a167bea
AK
3254module_init(kvmppc_book3s_init_hv);
3255module_exit(kvmppc_book3s_exit_hv);
2ba9f0d8 3256MODULE_LICENSE("GPL");
398a76c6
AG
3257MODULE_ALIAS_MISCDEV(KVM_MINOR);
3258MODULE_ALIAS("devname:kvm");