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