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