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