blob: 218cba2f56999aae0989ef0561cabe387dc585f6 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
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>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100030#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000031#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000032#include <linux/spinlock.h>
33#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000034#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010035#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110036#include <linux/debugfs.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000037
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>
Paul Mackerras371fefd2011-06-29 00:23:08 +000049#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000051#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000053#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110054#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053055#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100056#include <asm/pnv-pci.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000058#include <linux/vmalloc.h>
59#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000060#include <linux/hugetlb.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100061#include <linux/kvm_irqfd.h>
62#include <linux/irqbypass.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053063#include <linux/module.h>
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +100064#include <linux/compiler.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000065
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053066#include "book3s.h"
67
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060068#define CREATE_TRACE_POINTS
69#include "trace_hv.h"
70
Paul Mackerrasde56a942011-06-29 00:21:34 +000071/* #define EXIT_DEBUG */
72/* #define EXIT_DEBUG_SIMPLE */
73/* #define EXIT_DEBUG_INT */
74
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000075/* Used to indicate that a guest page fault needs to be handled */
76#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100077/* Used to indicate that a guest passthrough interrupt needs to be handled */
78#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000079
Paul Mackerrasc7b67672012-10-15 01:18:07 +000080/* Used as a "null" value for timebase values */
81#define TB_NIL (~(u64)0)
82
Paul Mackerras699a0ea2014-06-02 11:02:59 +100083static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
84
Paul Mackerrasb4deba52015-07-02 20:38:16 +100085static int dynamic_mt_modes = 6;
86module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
Paul Mackerrasec257162015-06-24 21:18:03 +100088static int target_smt_mode;
89module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
90MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100091
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -060092#ifdef CONFIG_KVM_XICS
93static struct kernel_param_ops module_param_ops = {
94 .set = param_set_int,
95 .get = param_get_int,
96};
97
Suresh Warrier644abbb2016-08-19 15:35:54 +100098module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
99 S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
101
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600102module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
103 S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
105#endif
106
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +1000107/* Maximum halt poll interval defaults to KVM_HALT_POLL_NS_DEFAULT */
108static unsigned int halt_poll_max_ns = KVM_HALT_POLL_NS_DEFAULT;
109module_param(halt_poll_max_ns, uint, S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(halt_poll_max_ns, "Maximum halt poll time in ns");
111
112/* Factor by which the vcore halt poll interval is grown, default is to double
113 */
114static unsigned int halt_poll_ns_grow = 2;
115module_param(halt_poll_ns_grow, int, S_IRUGO);
116MODULE_PARM_DESC(halt_poll_ns_grow, "Factor halt poll time is grown by");
117
118/* Factor by which the vcore halt poll interval is shrunk, default is to reset
119 */
120static unsigned int halt_poll_ns_shrink;
121module_param(halt_poll_ns_shrink, int, S_IRUGO);
122MODULE_PARM_DESC(halt_poll_ns_shrink, "Factor halt poll time is shrunk by");
123
Paul Mackerras19ccb762011-07-23 17:42:46 +1000124static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +0000125static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000126
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000127static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
128 int *ip)
129{
130 int i = *ip;
131 struct kvm_vcpu *vcpu;
132
133 while (++i < MAX_SMT_THREADS) {
134 vcpu = READ_ONCE(vc->runnable_threads[i]);
135 if (vcpu) {
136 *ip = i;
137 return vcpu;
138 }
139 }
140 return NULL;
141}
142
143/* Used to traverse the list of runnable threads for a given vcore */
144#define for_each_runnable_thread(i, vcpu, vc) \
145 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
146
Paul Mackerras66feed62015-03-28 14:21:12 +1100147static bool kvmppc_ipi_thread(int cpu)
148{
149 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
150 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
151 preempt_disable();
152 if (cpu_first_thread_sibling(cpu) ==
153 cpu_first_thread_sibling(smp_processor_id())) {
154 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
155 msg |= cpu_thread_in_core(cpu);
156 smp_mb();
157 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
158 preempt_enable();
159 return true;
160 }
161 preempt_enable();
162 }
163
164#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
165 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
166 xics_wake_cpu(cpu);
167 return true;
168 }
169#endif
170
171 return false;
172}
173
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530174static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000175{
Paul Mackerrasec257162015-06-24 21:18:03 +1000176 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100177 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000178
179 wqp = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100180 if (swait_active(wqp)) {
181 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000182 ++vcpu->stat.halt_wakeup;
183 }
184
Paul Mackerrasec257162015-06-24 21:18:03 +1000185 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100186 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000187
188 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000189 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100190 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
191 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000192}
193
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000194/*
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;
Paul Mackerras2711e242014-12-04 16:43:28 +1100221 * 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.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000225 */
226
Paul Mackerrasec257162015-06-24 21:18:03 +1000227static 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
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530248static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000249{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000250 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100251 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000252
Paul Mackerras2711e242014-12-04 16:43:28 +1100253 /*
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 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000259 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
260 kvmppc_core_end_stolen(vc);
261
Paul Mackerras2711e242014-12-04 16:43:28 +1100262 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000263 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 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100268 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000269}
270
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530271static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000272{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000273 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100274 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000275
Paul Mackerrasec257162015-06-24 21:18:03 +1000276 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
277 kvmppc_core_start_stolen(vc);
278
Paul Mackerras2711e242014-12-04 16:43:28 +1100279 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000280 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
281 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100282 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000283}
284
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530285static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000286{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100287 /*
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;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000293 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000294 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000295}
296
Thomas Huth5358a962015-05-22 09:25:02 +0200297static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000298{
299 vcpu->arch.pvr = pvr;
300}
301
Thomas Huth5358a962015-05-22 09:25:02 +0200302static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000303{
304 unsigned long pcr = 0;
305 struct kvmppc_vcore *vc = vcpu->arch.vcore;
306
307 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000308 switch (arch_compat) {
309 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100310 /*
311 * If an arch bit is set in PCR, all the defined
312 * higher-order arch bits also have to be set.
313 */
314 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000315 break;
316 case PVR_ARCH_206:
317 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100318 pcr = PCR_ARCH_206;
319 break;
320 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000321 break;
322 default:
323 return -EINVAL;
324 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100325
326 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
327 /* POWER7 can't emulate POWER8 */
328 if (!(pcr & PCR_ARCH_206))
329 return -EINVAL;
330 pcr &= ~PCR_ARCH_206;
331 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000332 }
333
334 spin_lock(&vc->lock);
335 vc->arch_compat = arch_compat;
336 vc->pcr = pcr;
337 spin_unlock(&vc->lock);
338
339 return 0;
340}
341
Thomas Huth5358a962015-05-22 09:25:02 +0200342static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000343{
344 int r;
345
346 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
347 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
348 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
349 for (r = 0; r < 16; ++r)
350 pr_err("r%2d = %.16lx r%d = %.16lx\n",
351 r, kvmppc_get_gpr(vcpu, r),
352 r+16, kvmppc_get_gpr(vcpu, r+16));
353 pr_err("ctr = %.16lx lr = %.16lx\n",
354 vcpu->arch.ctr, vcpu->arch.lr);
355 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
356 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
357 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
358 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
359 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
360 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
361 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
362 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
363 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
364 pr_err("fault dar = %.16lx dsisr = %.8x\n",
365 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
366 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
367 for (r = 0; r < vcpu->arch.slb_max; ++r)
368 pr_err(" ESID = %.16llx VSID = %.16llx\n",
369 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
370 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000371 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000372 vcpu->arch.last_inst);
373}
374
Thomas Huth5358a962015-05-22 09:25:02 +0200375static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000376{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100377 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000378
379 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100380 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000381 mutex_unlock(&kvm->lock);
382 return ret;
383}
384
385static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
386{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000387 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200388 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000389}
390
Paul Mackerras55b665b2012-09-25 20:33:06 +0000391static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
392 unsigned long addr, unsigned long len)
393{
394 /* check address is cacheline aligned */
395 if (addr & (L1_CACHE_BYTES - 1))
396 return -EINVAL;
397 spin_lock(&vcpu->arch.vpa_update_lock);
398 if (v->next_gpa != addr || v->len != len) {
399 v->next_gpa = addr;
400 v->len = addr ? len : 0;
401 v->update_pending = 1;
402 }
403 spin_unlock(&vcpu->arch.vpa_update_lock);
404 return 0;
405}
406
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000407/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
408struct reg_vpa {
409 u32 dummy;
410 union {
Alexander Graf02407552014-06-11 10:34:19 +0200411 __be16 hword;
412 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000413 } length;
414};
415
416static int vpa_is_registered(struct kvmppc_vpa *vpap)
417{
418 if (vpap->update_pending)
419 return vpap->next_gpa != 0;
420 return vpap->pinned_addr != NULL;
421}
422
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000423static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
424 unsigned long flags,
425 unsigned long vcpuid, unsigned long vpa)
426{
427 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000428 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000429 void *va;
430 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431 int err;
432 int subfunc;
433 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000434
435 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
436 if (!tvcpu)
437 return H_PARAMETER;
438
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000439 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
440 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
441 subfunc == H_VPA_REG_SLB) {
442 /* Registering new area - address must be cache-line aligned */
443 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000444 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000445
446 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000447 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
448 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000449 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000450 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200451 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000452 else
Alexander Graf02407552014-06-11 10:34:19 +0200453 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000454 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000455
456 /* Check length */
457 if (len > nb || len < sizeof(struct reg_vpa))
458 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000459 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000460 vpa = 0;
461 len = 0;
462 }
463
464 err = H_PARAMETER;
465 vpap = NULL;
466 spin_lock(&tvcpu->arch.vpa_update_lock);
467
468 switch (subfunc) {
469 case H_VPA_REG_VPA: /* register VPA */
470 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000471 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000472 vpap = &tvcpu->arch.vpa;
473 err = 0;
474 break;
475
476 case H_VPA_REG_DTL: /* register DTL */
477 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000478 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000479 len -= len % sizeof(struct dtl_entry);
480
481 /* Check that they have previously registered a VPA */
482 err = H_RESOURCE;
483 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000484 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485
486 vpap = &tvcpu->arch.dtl;
487 err = 0;
488 break;
489
490 case H_VPA_REG_SLB: /* register SLB shadow buffer */
491 /* Check that they have previously registered a VPA */
492 err = H_RESOURCE;
493 if (!vpa_is_registered(&tvcpu->arch.vpa))
494 break;
495
496 vpap = &tvcpu->arch.slb_shadow;
497 err = 0;
498 break;
499
500 case H_VPA_DEREG_VPA: /* deregister VPA */
501 /* Check they don't still have a DTL or SLB buf registered */
502 err = H_RESOURCE;
503 if (vpa_is_registered(&tvcpu->arch.dtl) ||
504 vpa_is_registered(&tvcpu->arch.slb_shadow))
505 break;
506
507 vpap = &tvcpu->arch.vpa;
508 err = 0;
509 break;
510
511 case H_VPA_DEREG_DTL: /* deregister DTL */
512 vpap = &tvcpu->arch.dtl;
513 err = 0;
514 break;
515
516 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
517 vpap = &tvcpu->arch.slb_shadow;
518 err = 0;
519 break;
520 }
521
522 if (vpap) {
523 vpap->next_gpa = vpa;
524 vpap->len = len;
525 vpap->update_pending = 1;
526 }
527
528 spin_unlock(&tvcpu->arch.vpa_update_lock);
529
530 return err;
531}
532
Paul Mackerras081f3232012-06-01 20:20:24 +1000533static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000534{
Paul Mackerras081f3232012-06-01 20:20:24 +1000535 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000536 void *va;
537 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000538 unsigned long gpa;
539
540 /*
541 * We need to pin the page pointed to by vpap->next_gpa,
542 * but we can't call kvmppc_pin_guest_page under the lock
543 * as it does get_user_pages() and down_read(). So we
544 * have to drop the lock, pin the page, then get the lock
545 * again and check that a new area didn't get registered
546 * in the meantime.
547 */
548 for (;;) {
549 gpa = vpap->next_gpa;
550 spin_unlock(&vcpu->arch.vpa_update_lock);
551 va = NULL;
552 nb = 0;
553 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000554 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000555 spin_lock(&vcpu->arch.vpa_update_lock);
556 if (gpa == vpap->next_gpa)
557 break;
558 /* sigh... unpin that one and try again */
559 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000560 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000561 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000562
563 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000564 if (va && nb < vpap->len) {
565 /*
566 * If it's now too short, it must be that userspace
567 * has changed the mappings underlying guest memory,
568 * so unregister the region.
569 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000570 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000571 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000572 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000573 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000574 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
575 vpap->dirty);
576 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000577 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000578 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000579 if (va)
580 vpap->pinned_end = va + vpap->len;
581}
Paul Mackerras93e60242011-12-12 12:28:55 +0000582
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000583static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
584{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000585 if (!(vcpu->arch.vpa.update_pending ||
586 vcpu->arch.slb_shadow.update_pending ||
587 vcpu->arch.dtl.update_pending))
588 return;
589
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000590 spin_lock(&vcpu->arch.vpa_update_lock);
591 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000592 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000593 if (vcpu->arch.vpa.pinned_addr)
594 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000595 }
596 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000597 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000598 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
599 vcpu->arch.dtl_index = 0;
600 }
601 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000602 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000603 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000604}
605
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000606/*
607 * Return the accumulated stolen time for the vcore up until `now'.
608 * The caller should hold the vcore lock.
609 */
610static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
611{
612 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100613 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000614
Paul Mackerras2711e242014-12-04 16:43:28 +1100615 spin_lock_irqsave(&vc->stoltb_lock, flags);
616 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000617 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100618 vc->preempt_tb != TB_NIL)
619 p += now - vc->preempt_tb;
620 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000621 return p;
622}
623
Paul Mackerras0456ec42012-02-03 00:56:21 +0000624static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
625 struct kvmppc_vcore *vc)
626{
627 struct dtl_entry *dt;
628 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000629 unsigned long stolen;
630 unsigned long core_stolen;
631 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000632
633 dt = vcpu->arch.dtl_ptr;
634 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000635 now = mftb();
636 core_stolen = vcore_stolen_time(vc, now);
637 stolen = core_stolen - vcpu->arch.stolen_logged;
638 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100639 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000640 stolen += vcpu->arch.busy_stolen;
641 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100642 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000643 if (!dt || !vpa)
644 return;
645 memset(dt, 0, sizeof(struct dtl_entry));
646 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200647 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
648 dt->timebase = cpu_to_be64(now + vc->tb_offset);
649 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
650 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
651 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000652 ++dt;
653 if (dt == vcpu->arch.dtl.pinned_end)
654 dt = vcpu->arch.dtl.pinned_addr;
655 vcpu->arch.dtl_ptr = dt;
656 /* order writing *dt vs. writing vpa->dtl_idx */
657 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200658 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000659 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000660}
661
Michael Neuling96423822014-06-02 11:03:01 +1000662static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
663{
664 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
665 return true;
666 if ((!vcpu->arch.vcore->arch_compat) &&
667 cpu_has_feature(CPU_FTR_ARCH_207S))
668 return true;
669 return false;
670}
671
672static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
673 unsigned long resource, unsigned long value1,
674 unsigned long value2)
675{
676 switch (resource) {
677 case H_SET_MODE_RESOURCE_SET_CIABR:
678 if (!kvmppc_power8_compatible(vcpu))
679 return H_P2;
680 if (value2)
681 return H_P4;
682 if (mflags)
683 return H_UNSUPPORTED_FLAG_START;
684 /* Guests can't breakpoint the hypervisor */
685 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
686 return H_P3;
687 vcpu->arch.ciabr = value1;
688 return H_SUCCESS;
689 case H_SET_MODE_RESOURCE_SET_DAWR:
690 if (!kvmppc_power8_compatible(vcpu))
691 return H_P2;
692 if (mflags)
693 return H_UNSUPPORTED_FLAG_START;
694 if (value2 & DABRX_HYP)
695 return H_P4;
696 vcpu->arch.dawr = value1;
697 vcpu->arch.dawrx = value2;
698 return H_SUCCESS;
699 default:
700 return H_TOO_HARD;
701 }
702}
703
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100704static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
705{
706 struct kvmppc_vcore *vcore = target->arch.vcore;
707
708 /*
709 * We expect to have been called by the real mode handler
710 * (kvmppc_rm_h_confer()) which would have directly returned
711 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
712 * have useful work to do and should not confer) so we don't
713 * recheck that here.
714 */
715
716 spin_lock(&vcore->lock);
717 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000718 vcore->vcore_state != VCORE_INACTIVE &&
719 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100720 target = vcore->runner;
721 spin_unlock(&vcore->lock);
722
723 return kvm_vcpu_yield_to(target);
724}
725
726static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
727{
728 int yield_count = 0;
729 struct lppaca *lppaca;
730
731 spin_lock(&vcpu->arch.vpa_update_lock);
732 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
733 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100734 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100735 spin_unlock(&vcpu->arch.vpa_update_lock);
736 return yield_count;
737}
738
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000739int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
740{
741 unsigned long req = kvmppc_get_gpr(vcpu, 3);
742 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100743 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000744 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000745 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000746
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000747 if (req <= MAX_HCALL_OPCODE &&
748 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
749 return RESUME_HOST;
750
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000751 switch (req) {
752 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000753 break;
754 case H_PROD:
755 target = kvmppc_get_gpr(vcpu, 4);
756 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
757 if (!tvcpu) {
758 ret = H_PARAMETER;
759 break;
760 }
761 tvcpu->arch.prodded = 1;
762 smp_mb();
763 if (vcpu->arch.ceded) {
Marcelo Tosatti85773702016-02-19 09:46:39 +0100764 if (swait_active(&vcpu->wq)) {
765 swake_up(&vcpu->wq);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000766 vcpu->stat.halt_wakeup++;
767 }
768 }
769 break;
770 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000771 target = kvmppc_get_gpr(vcpu, 4);
772 if (target == -1)
773 break;
774 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
775 if (!tvcpu) {
776 ret = H_PARAMETER;
777 break;
778 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100779 yield_count = kvmppc_get_gpr(vcpu, 5);
780 if (kvmppc_get_yield_count(tvcpu) != yield_count)
781 break;
782 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000783 break;
784 case H_REGISTER_VPA:
785 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
786 kvmppc_get_gpr(vcpu, 5),
787 kvmppc_get_gpr(vcpu, 6));
788 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000789 case H_RTAS:
790 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
791 return RESUME_HOST;
792
Paul Mackerrasc9438092013-11-16 17:46:05 +1100793 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000794 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100795 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000796
797 if (rc == -ENOENT)
798 return RESUME_HOST;
799 else if (rc == 0)
800 break;
801
802 /* Send the error out to userspace via KVM_RUN */
803 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100804 case H_LOGICAL_CI_LOAD:
805 ret = kvmppc_h_logical_ci_load(vcpu);
806 if (ret == H_TOO_HARD)
807 return RESUME_HOST;
808 break;
809 case H_LOGICAL_CI_STORE:
810 ret = kvmppc_h_logical_ci_store(vcpu);
811 if (ret == H_TOO_HARD)
812 return RESUME_HOST;
813 break;
Michael Neuling96423822014-06-02 11:03:01 +1000814 case H_SET_MODE:
815 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
816 kvmppc_get_gpr(vcpu, 5),
817 kvmppc_get_gpr(vcpu, 6),
818 kvmppc_get_gpr(vcpu, 7));
819 if (ret == H_TOO_HARD)
820 return RESUME_HOST;
821 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000822 case H_XIRR:
823 case H_CPPR:
824 case H_EOI:
825 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000826 case H_IPOLL:
827 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000828 if (kvmppc_xics_enabled(vcpu)) {
829 ret = kvmppc_xics_hcall(vcpu, req);
830 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100831 }
832 return RESUME_HOST;
833 case H_PUT_TCE:
834 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
835 kvmppc_get_gpr(vcpu, 5),
836 kvmppc_get_gpr(vcpu, 6));
837 if (ret == H_TOO_HARD)
838 return RESUME_HOST;
839 break;
840 case H_PUT_TCE_INDIRECT:
841 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
842 kvmppc_get_gpr(vcpu, 5),
843 kvmppc_get_gpr(vcpu, 6),
844 kvmppc_get_gpr(vcpu, 7));
845 if (ret == H_TOO_HARD)
846 return RESUME_HOST;
847 break;
848 case H_STUFF_TCE:
849 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
850 kvmppc_get_gpr(vcpu, 5),
851 kvmppc_get_gpr(vcpu, 6),
852 kvmppc_get_gpr(vcpu, 7));
853 if (ret == H_TOO_HARD)
854 return RESUME_HOST;
855 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000856 default:
857 return RESUME_HOST;
858 }
859 kvmppc_set_gpr(vcpu, 3, ret);
860 vcpu->arch.hcall_needed = 0;
861 return RESUME_GUEST;
862}
863
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000864static int kvmppc_hcall_impl_hv(unsigned long cmd)
865{
866 switch (cmd) {
867 case H_CEDE:
868 case H_PROD:
869 case H_CONFER:
870 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000871 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100872 case H_LOGICAL_CI_LOAD:
873 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000874#ifdef CONFIG_KVM_XICS
875 case H_XIRR:
876 case H_CPPR:
877 case H_EOI:
878 case H_IPI:
879 case H_IPOLL:
880 case H_XIRR_X:
881#endif
882 return 1;
883 }
884
885 /* See if it's in the real-mode table */
886 return kvmppc_hcall_impl_hv_realmode(cmd);
887}
888
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530889static int kvmppc_emulate_debug_inst(struct kvm_run *run,
890 struct kvm_vcpu *vcpu)
891{
892 u32 last_inst;
893
894 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
895 EMULATE_DONE) {
896 /*
897 * Fetch failed, so return to guest and
898 * try executing it again.
899 */
900 return RESUME_GUEST;
901 }
902
903 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
904 run->exit_reason = KVM_EXIT_DEBUG;
905 run->debug.arch.address = kvmppc_get_pc(vcpu);
906 return RESUME_HOST;
907 } else {
908 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
909 return RESUME_GUEST;
910 }
911}
912
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530913static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
914 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000915{
916 int r = RESUME_HOST;
917
918 vcpu->stat.sum_exits++;
919
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100920 /*
921 * This can happen if an interrupt occurs in the last stages
922 * of guest entry or the first stages of guest exit (i.e. after
923 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
924 * and before setting it to KVM_GUEST_MODE_HOST_HV).
925 * That can happen due to a bug, or due to a machine check
926 * occurring at just the wrong time.
927 */
928 if (vcpu->arch.shregs.msr & MSR_HV) {
929 printk(KERN_EMERG "KVM trap in HV mode!\n");
930 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
931 vcpu->arch.trap, kvmppc_get_pc(vcpu),
932 vcpu->arch.shregs.msr);
933 kvmppc_dump_regs(vcpu);
934 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
935 run->hw.hardware_exit_reason = vcpu->arch.trap;
936 return RESUME_HOST;
937 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000938 run->exit_reason = KVM_EXIT_UNKNOWN;
939 run->ready_for_interrupt_injection = 1;
940 switch (vcpu->arch.trap) {
941 /* We're good on these - the host merely wanted to get our attention */
942 case BOOK3S_INTERRUPT_HV_DECREMENTER:
943 vcpu->stat.dec_exits++;
944 r = RESUME_GUEST;
945 break;
946 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100947 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000948 vcpu->stat.ext_intr_exits++;
949 r = RESUME_GUEST;
950 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100951 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
952 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000953 case BOOK3S_INTERRUPT_PERFMON:
954 r = RESUME_GUEST;
955 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000956 case BOOK3S_INTERRUPT_MACHINE_CHECK:
957 /*
958 * Deliver a machine check interrupt to the guest.
959 * We have to do this, even if the host has handled the
960 * machine check, because machine checks use SRR0/1 and
961 * the interrupt might have trashed guest state in them.
962 */
963 kvmppc_book3s_queue_irqprio(vcpu,
964 BOOK3S_INTERRUPT_MACHINE_CHECK);
965 r = RESUME_GUEST;
966 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000967 case BOOK3S_INTERRUPT_PROGRAM:
968 {
969 ulong flags;
970 /*
971 * Normally program interrupts are delivered directly
972 * to the guest by the hardware, but we can get here
973 * as a result of a hypervisor emulation interrupt
974 * (e40) getting turned into a 700 by BML RTAS.
975 */
976 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
977 kvmppc_core_queue_program(vcpu, flags);
978 r = RESUME_GUEST;
979 break;
980 }
981 case BOOK3S_INTERRUPT_SYSCALL:
982 {
983 /* hcall - punt to userspace */
984 int i;
985
Liu Ping Fan27025a62013-11-19 14:12:48 +0800986 /* hypercall with MSR_PR has already been handled in rmode,
987 * and never reaches here.
988 */
989
Paul Mackerrasde56a942011-06-29 00:21:34 +0000990 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
991 for (i = 0; i < 9; ++i)
992 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
993 run->exit_reason = KVM_EXIT_PAPR_HCALL;
994 vcpu->arch.hcall_needed = 1;
995 r = RESUME_HOST;
996 break;
997 }
998 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000999 * We get these next two if the guest accesses a page which it thinks
1000 * it has mapped but which is not actually present, either because
1001 * it is for an emulated I/O device or because the corresonding
1002 * host page has been paged out. Any other HDSI/HISI interrupts
1003 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001004 */
1005 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001006 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001007 break;
1008 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001009 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1010 vcpu->arch.fault_dsisr = 0;
1011 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001012 break;
1013 /*
1014 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301015 * If the guest debug is disabled, generate a program interrupt
1016 * to the guest. If guest debug is enabled, we need to check
1017 * whether the instruction is a software breakpoint instruction.
1018 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001019 */
1020 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001021 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1022 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1023 swab32(vcpu->arch.emul_inst) :
1024 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301025 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1026 r = kvmppc_emulate_debug_inst(run, vcpu);
1027 } else {
1028 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1029 r = RESUME_GUEST;
1030 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001031 break;
1032 /*
1033 * This occurs if the guest (kernel or userspace), does something that
1034 * is prohibited by HFSCR. We just generate a program interrupt to
1035 * the guest.
1036 */
1037 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1038 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001039 r = RESUME_GUEST;
1040 break;
Suresh Warrierf7af5202016-08-19 15:35:52 +10001041 case BOOK3S_INTERRUPT_HV_RM_HARD:
1042 r = RESUME_PASSTHROUGH;
1043 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001044 default:
1045 kvmppc_dump_regs(vcpu);
1046 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1047 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1048 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001049 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001050 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001051 break;
1052 }
1053
Paul Mackerrasde56a942011-06-29 00:21:34 +00001054 return r;
1055}
1056
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301057static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1058 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001059{
1060 int i;
1061
Paul Mackerrasde56a942011-06-29 00:21:34 +00001062 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301063 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001064 for (i = 0; i < vcpu->arch.slb_max; i++) {
1065 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1066 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1067 }
1068
1069 return 0;
1070}
1071
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301072static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1073 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001074{
1075 int i, j;
1076
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001077 /* Only accept the same PVR as the host's, since we can't spoof it */
1078 if (sregs->pvr != vcpu->arch.pvr)
1079 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001080
1081 j = 0;
1082 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1083 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1084 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1085 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1086 ++j;
1087 }
1088 }
1089 vcpu->arch.slb_max = j;
1090
1091 return 0;
1092}
1093
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001094static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1095 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001096{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001097 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001098 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1099 u64 mask;
1100
Paul Mackerras8f902b02015-03-20 20:39:38 +11001101 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001102 spin_lock(&vc->lock);
1103 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001104 * If ILE (interrupt little-endian) has changed, update the
1105 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1106 */
1107 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001108 struct kvm_vcpu *vcpu;
1109 int i;
1110
Anton Blanchardd6829162014-01-08 21:25:30 +11001111 kvm_for_each_vcpu(i, vcpu, kvm) {
1112 if (vcpu->arch.vcore != vc)
1113 continue;
1114 if (new_lpcr & LPCR_ILE)
1115 vcpu->arch.intr_msr |= MSR_LE;
1116 else
1117 vcpu->arch.intr_msr &= ~MSR_LE;
1118 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001119 }
1120
1121 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001122 * Userspace can only modify DPFD (default prefetch depth),
1123 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001124 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001125 */
1126 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001127 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1128 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001129
1130 /* Broken 32-bit version of LPCR must not clear top bits */
1131 if (preserve_top32)
1132 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001133 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1134 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001135 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001136}
1137
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301138static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1139 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001140{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001141 int r = 0;
1142 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001143
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001144 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301145 case KVM_REG_PPC_DEBUG_INST:
1146 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1147 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001148 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001149 *val = get_reg_val(id, 0);
1150 break;
1151 case KVM_REG_PPC_DABR:
1152 *val = get_reg_val(id, vcpu->arch.dabr);
1153 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001154 case KVM_REG_PPC_DABRX:
1155 *val = get_reg_val(id, vcpu->arch.dabrx);
1156 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001157 case KVM_REG_PPC_DSCR:
1158 *val = get_reg_val(id, vcpu->arch.dscr);
1159 break;
1160 case KVM_REG_PPC_PURR:
1161 *val = get_reg_val(id, vcpu->arch.purr);
1162 break;
1163 case KVM_REG_PPC_SPURR:
1164 *val = get_reg_val(id, vcpu->arch.spurr);
1165 break;
1166 case KVM_REG_PPC_AMR:
1167 *val = get_reg_val(id, vcpu->arch.amr);
1168 break;
1169 case KVM_REG_PPC_UAMOR:
1170 *val = get_reg_val(id, vcpu->arch.uamor);
1171 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001172 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001173 i = id - KVM_REG_PPC_MMCR0;
1174 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1175 break;
1176 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1177 i = id - KVM_REG_PPC_PMC1;
1178 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001179 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001180 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1181 i = id - KVM_REG_PPC_SPMC1;
1182 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1183 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001184 case KVM_REG_PPC_SIAR:
1185 *val = get_reg_val(id, vcpu->arch.siar);
1186 break;
1187 case KVM_REG_PPC_SDAR:
1188 *val = get_reg_val(id, vcpu->arch.sdar);
1189 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001190 case KVM_REG_PPC_SIER:
1191 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001192 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001193 case KVM_REG_PPC_IAMR:
1194 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001195 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001196 case KVM_REG_PPC_PSPB:
1197 *val = get_reg_val(id, vcpu->arch.pspb);
1198 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001199 case KVM_REG_PPC_DPDES:
1200 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1201 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001202 case KVM_REG_PPC_VTB:
1203 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
1204 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001205 case KVM_REG_PPC_DAWR:
1206 *val = get_reg_val(id, vcpu->arch.dawr);
1207 break;
1208 case KVM_REG_PPC_DAWRX:
1209 *val = get_reg_val(id, vcpu->arch.dawrx);
1210 break;
1211 case KVM_REG_PPC_CIABR:
1212 *val = get_reg_val(id, vcpu->arch.ciabr);
1213 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001214 case KVM_REG_PPC_CSIGR:
1215 *val = get_reg_val(id, vcpu->arch.csigr);
1216 break;
1217 case KVM_REG_PPC_TACR:
1218 *val = get_reg_val(id, vcpu->arch.tacr);
1219 break;
1220 case KVM_REG_PPC_TCSCR:
1221 *val = get_reg_val(id, vcpu->arch.tcscr);
1222 break;
1223 case KVM_REG_PPC_PID:
1224 *val = get_reg_val(id, vcpu->arch.pid);
1225 break;
1226 case KVM_REG_PPC_ACOP:
1227 *val = get_reg_val(id, vcpu->arch.acop);
1228 break;
1229 case KVM_REG_PPC_WORT:
1230 *val = get_reg_val(id, vcpu->arch.wort);
1231 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001232 case KVM_REG_PPC_VPA_ADDR:
1233 spin_lock(&vcpu->arch.vpa_update_lock);
1234 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1235 spin_unlock(&vcpu->arch.vpa_update_lock);
1236 break;
1237 case KVM_REG_PPC_VPA_SLB:
1238 spin_lock(&vcpu->arch.vpa_update_lock);
1239 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1240 val->vpaval.length = vcpu->arch.slb_shadow.len;
1241 spin_unlock(&vcpu->arch.vpa_update_lock);
1242 break;
1243 case KVM_REG_PPC_VPA_DTL:
1244 spin_lock(&vcpu->arch.vpa_update_lock);
1245 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1246 val->vpaval.length = vcpu->arch.dtl.len;
1247 spin_unlock(&vcpu->arch.vpa_update_lock);
1248 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001249 case KVM_REG_PPC_TB_OFFSET:
1250 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1251 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001252 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001253 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001254 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1255 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001256 case KVM_REG_PPC_PPR:
1257 *val = get_reg_val(id, vcpu->arch.ppr);
1258 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001259#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1260 case KVM_REG_PPC_TFHAR:
1261 *val = get_reg_val(id, vcpu->arch.tfhar);
1262 break;
1263 case KVM_REG_PPC_TFIAR:
1264 *val = get_reg_val(id, vcpu->arch.tfiar);
1265 break;
1266 case KVM_REG_PPC_TEXASR:
1267 *val = get_reg_val(id, vcpu->arch.texasr);
1268 break;
1269 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1270 i = id - KVM_REG_PPC_TM_GPR0;
1271 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1272 break;
1273 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1274 {
1275 int j;
1276 i = id - KVM_REG_PPC_TM_VSR0;
1277 if (i < 32)
1278 for (j = 0; j < TS_FPRWIDTH; j++)
1279 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1280 else {
1281 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1282 val->vval = vcpu->arch.vr_tm.vr[i-32];
1283 else
1284 r = -ENXIO;
1285 }
1286 break;
1287 }
1288 case KVM_REG_PPC_TM_CR:
1289 *val = get_reg_val(id, vcpu->arch.cr_tm);
1290 break;
Paul Mackerras75b10532016-11-07 15:09:58 +11001291 case KVM_REG_PPC_TM_XER:
1292 *val = get_reg_val(id, vcpu->arch.xer_tm);
1293 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001294 case KVM_REG_PPC_TM_LR:
1295 *val = get_reg_val(id, vcpu->arch.lr_tm);
1296 break;
1297 case KVM_REG_PPC_TM_CTR:
1298 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1299 break;
1300 case KVM_REG_PPC_TM_FPSCR:
1301 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1302 break;
1303 case KVM_REG_PPC_TM_AMR:
1304 *val = get_reg_val(id, vcpu->arch.amr_tm);
1305 break;
1306 case KVM_REG_PPC_TM_PPR:
1307 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1308 break;
1309 case KVM_REG_PPC_TM_VRSAVE:
1310 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1311 break;
1312 case KVM_REG_PPC_TM_VSCR:
1313 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1314 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1315 else
1316 r = -ENXIO;
1317 break;
1318 case KVM_REG_PPC_TM_DSCR:
1319 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1320 break;
1321 case KVM_REG_PPC_TM_TAR:
1322 *val = get_reg_val(id, vcpu->arch.tar_tm);
1323 break;
1324#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001325 case KVM_REG_PPC_ARCH_COMPAT:
1326 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1327 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001328 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001329 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001330 break;
1331 }
1332
1333 return r;
1334}
1335
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301336static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1337 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001338{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001339 int r = 0;
1340 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001341 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001342
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001343 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001344 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001345 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001346 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001347 r = -EINVAL;
1348 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001349 case KVM_REG_PPC_DABR:
1350 vcpu->arch.dabr = set_reg_val(id, *val);
1351 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001352 case KVM_REG_PPC_DABRX:
1353 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1354 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001355 case KVM_REG_PPC_DSCR:
1356 vcpu->arch.dscr = set_reg_val(id, *val);
1357 break;
1358 case KVM_REG_PPC_PURR:
1359 vcpu->arch.purr = set_reg_val(id, *val);
1360 break;
1361 case KVM_REG_PPC_SPURR:
1362 vcpu->arch.spurr = set_reg_val(id, *val);
1363 break;
1364 case KVM_REG_PPC_AMR:
1365 vcpu->arch.amr = set_reg_val(id, *val);
1366 break;
1367 case KVM_REG_PPC_UAMOR:
1368 vcpu->arch.uamor = set_reg_val(id, *val);
1369 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001370 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001371 i = id - KVM_REG_PPC_MMCR0;
1372 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1373 break;
1374 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1375 i = id - KVM_REG_PPC_PMC1;
1376 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1377 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001378 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1379 i = id - KVM_REG_PPC_SPMC1;
1380 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1381 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001382 case KVM_REG_PPC_SIAR:
1383 vcpu->arch.siar = set_reg_val(id, *val);
1384 break;
1385 case KVM_REG_PPC_SDAR:
1386 vcpu->arch.sdar = set_reg_val(id, *val);
1387 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001388 case KVM_REG_PPC_SIER:
1389 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001390 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001391 case KVM_REG_PPC_IAMR:
1392 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001393 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001394 case KVM_REG_PPC_PSPB:
1395 vcpu->arch.pspb = set_reg_val(id, *val);
1396 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001397 case KVM_REG_PPC_DPDES:
1398 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1399 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001400 case KVM_REG_PPC_VTB:
1401 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
1402 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001403 case KVM_REG_PPC_DAWR:
1404 vcpu->arch.dawr = set_reg_val(id, *val);
1405 break;
1406 case KVM_REG_PPC_DAWRX:
1407 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1408 break;
1409 case KVM_REG_PPC_CIABR:
1410 vcpu->arch.ciabr = set_reg_val(id, *val);
1411 /* Don't allow setting breakpoints in hypervisor code */
1412 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1413 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1414 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001415 case KVM_REG_PPC_CSIGR:
1416 vcpu->arch.csigr = set_reg_val(id, *val);
1417 break;
1418 case KVM_REG_PPC_TACR:
1419 vcpu->arch.tacr = set_reg_val(id, *val);
1420 break;
1421 case KVM_REG_PPC_TCSCR:
1422 vcpu->arch.tcscr = set_reg_val(id, *val);
1423 break;
1424 case KVM_REG_PPC_PID:
1425 vcpu->arch.pid = set_reg_val(id, *val);
1426 break;
1427 case KVM_REG_PPC_ACOP:
1428 vcpu->arch.acop = set_reg_val(id, *val);
1429 break;
1430 case KVM_REG_PPC_WORT:
1431 vcpu->arch.wort = set_reg_val(id, *val);
1432 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001433 case KVM_REG_PPC_VPA_ADDR:
1434 addr = set_reg_val(id, *val);
1435 r = -EINVAL;
1436 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1437 vcpu->arch.dtl.next_gpa))
1438 break;
1439 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1440 break;
1441 case KVM_REG_PPC_VPA_SLB:
1442 addr = val->vpaval.addr;
1443 len = val->vpaval.length;
1444 r = -EINVAL;
1445 if (addr && !vcpu->arch.vpa.next_gpa)
1446 break;
1447 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1448 break;
1449 case KVM_REG_PPC_VPA_DTL:
1450 addr = val->vpaval.addr;
1451 len = val->vpaval.length;
1452 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001453 if (addr && (len < sizeof(struct dtl_entry) ||
1454 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001455 break;
1456 len -= len % sizeof(struct dtl_entry);
1457 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1458 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001459 case KVM_REG_PPC_TB_OFFSET:
1460 /* round up to multiple of 2^24 */
1461 vcpu->arch.vcore->tb_offset =
1462 ALIGN(set_reg_val(id, *val), 1UL << 24);
1463 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001464 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001465 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1466 break;
1467 case KVM_REG_PPC_LPCR_64:
1468 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001469 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001470 case KVM_REG_PPC_PPR:
1471 vcpu->arch.ppr = set_reg_val(id, *val);
1472 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001473#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1474 case KVM_REG_PPC_TFHAR:
1475 vcpu->arch.tfhar = set_reg_val(id, *val);
1476 break;
1477 case KVM_REG_PPC_TFIAR:
1478 vcpu->arch.tfiar = set_reg_val(id, *val);
1479 break;
1480 case KVM_REG_PPC_TEXASR:
1481 vcpu->arch.texasr = set_reg_val(id, *val);
1482 break;
1483 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1484 i = id - KVM_REG_PPC_TM_GPR0;
1485 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1486 break;
1487 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1488 {
1489 int j;
1490 i = id - KVM_REG_PPC_TM_VSR0;
1491 if (i < 32)
1492 for (j = 0; j < TS_FPRWIDTH; j++)
1493 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1494 else
1495 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1496 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1497 else
1498 r = -ENXIO;
1499 break;
1500 }
1501 case KVM_REG_PPC_TM_CR:
1502 vcpu->arch.cr_tm = set_reg_val(id, *val);
1503 break;
Paul Mackerras75b10532016-11-07 15:09:58 +11001504 case KVM_REG_PPC_TM_XER:
1505 vcpu->arch.xer_tm = set_reg_val(id, *val);
1506 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001507 case KVM_REG_PPC_TM_LR:
1508 vcpu->arch.lr_tm = set_reg_val(id, *val);
1509 break;
1510 case KVM_REG_PPC_TM_CTR:
1511 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1512 break;
1513 case KVM_REG_PPC_TM_FPSCR:
1514 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1515 break;
1516 case KVM_REG_PPC_TM_AMR:
1517 vcpu->arch.amr_tm = set_reg_val(id, *val);
1518 break;
1519 case KVM_REG_PPC_TM_PPR:
1520 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1521 break;
1522 case KVM_REG_PPC_TM_VRSAVE:
1523 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1524 break;
1525 case KVM_REG_PPC_TM_VSCR:
1526 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1527 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1528 else
1529 r = - ENXIO;
1530 break;
1531 case KVM_REG_PPC_TM_DSCR:
1532 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1533 break;
1534 case KVM_REG_PPC_TM_TAR:
1535 vcpu->arch.tar_tm = set_reg_val(id, *val);
1536 break;
1537#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001538 case KVM_REG_PPC_ARCH_COMPAT:
1539 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1540 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001541 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001542 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001543 break;
1544 }
1545
1546 return r;
1547}
1548
Stewart Smithde9bdd12014-07-18 14:18:42 +10001549static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1550{
1551 struct kvmppc_vcore *vcore;
1552
1553 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1554
1555 if (vcore == NULL)
1556 return NULL;
1557
Stewart Smithde9bdd12014-07-18 14:18:42 +10001558 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001559 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001560 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001561 vcore->preempt_tb = TB_NIL;
1562 vcore->lpcr = kvm->arch.lpcr;
1563 vcore->first_vcpuid = core * threads_per_subcore;
1564 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001565 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001566
1567 return vcore;
1568}
1569
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001570#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1571static struct debugfs_timings_element {
1572 const char *name;
1573 size_t offset;
1574} timings[] = {
1575 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1576 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1577 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1578 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1579 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1580};
1581
1582#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1583
1584struct debugfs_timings_state {
1585 struct kvm_vcpu *vcpu;
1586 unsigned int buflen;
1587 char buf[N_TIMINGS * 100];
1588};
1589
1590static int debugfs_timings_open(struct inode *inode, struct file *file)
1591{
1592 struct kvm_vcpu *vcpu = inode->i_private;
1593 struct debugfs_timings_state *p;
1594
1595 p = kzalloc(sizeof(*p), GFP_KERNEL);
1596 if (!p)
1597 return -ENOMEM;
1598
1599 kvm_get_kvm(vcpu->kvm);
1600 p->vcpu = vcpu;
1601 file->private_data = p;
1602
1603 return nonseekable_open(inode, file);
1604}
1605
1606static int debugfs_timings_release(struct inode *inode, struct file *file)
1607{
1608 struct debugfs_timings_state *p = file->private_data;
1609
1610 kvm_put_kvm(p->vcpu->kvm);
1611 kfree(p);
1612 return 0;
1613}
1614
1615static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1616 size_t len, loff_t *ppos)
1617{
1618 struct debugfs_timings_state *p = file->private_data;
1619 struct kvm_vcpu *vcpu = p->vcpu;
1620 char *s, *buf_end;
1621 struct kvmhv_tb_accumulator tb;
1622 u64 count;
1623 loff_t pos;
1624 ssize_t n;
1625 int i, loops;
1626 bool ok;
1627
1628 if (!p->buflen) {
1629 s = p->buf;
1630 buf_end = s + sizeof(p->buf);
1631 for (i = 0; i < N_TIMINGS; ++i) {
1632 struct kvmhv_tb_accumulator *acc;
1633
1634 acc = (struct kvmhv_tb_accumulator *)
1635 ((unsigned long)vcpu + timings[i].offset);
1636 ok = false;
1637 for (loops = 0; loops < 1000; ++loops) {
1638 count = acc->seqcount;
1639 if (!(count & 1)) {
1640 smp_rmb();
1641 tb = *acc;
1642 smp_rmb();
1643 if (count == acc->seqcount) {
1644 ok = true;
1645 break;
1646 }
1647 }
1648 udelay(1);
1649 }
1650 if (!ok)
1651 snprintf(s, buf_end - s, "%s: stuck\n",
1652 timings[i].name);
1653 else
1654 snprintf(s, buf_end - s,
1655 "%s: %llu %llu %llu %llu\n",
1656 timings[i].name, count / 2,
1657 tb_to_ns(tb.tb_total),
1658 tb_to_ns(tb.tb_min),
1659 tb_to_ns(tb.tb_max));
1660 s += strlen(s);
1661 }
1662 p->buflen = s - p->buf;
1663 }
1664
1665 pos = *ppos;
1666 if (pos >= p->buflen)
1667 return 0;
1668 if (len > p->buflen - pos)
1669 len = p->buflen - pos;
1670 n = copy_to_user(buf, p->buf + pos, len);
1671 if (n) {
1672 if (n == len)
1673 return -EFAULT;
1674 len -= n;
1675 }
1676 *ppos = pos + len;
1677 return len;
1678}
1679
1680static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1681 size_t len, loff_t *ppos)
1682{
1683 return -EACCES;
1684}
1685
1686static const struct file_operations debugfs_timings_ops = {
1687 .owner = THIS_MODULE,
1688 .open = debugfs_timings_open,
1689 .release = debugfs_timings_release,
1690 .read = debugfs_timings_read,
1691 .write = debugfs_timings_write,
1692 .llseek = generic_file_llseek,
1693};
1694
1695/* Create a debugfs directory for the vcpu */
1696static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1697{
1698 char buf[16];
1699 struct kvm *kvm = vcpu->kvm;
1700
1701 snprintf(buf, sizeof(buf), "vcpu%u", id);
1702 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1703 return;
1704 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1705 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1706 return;
1707 vcpu->arch.debugfs_timings =
1708 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1709 vcpu, &debugfs_timings_ops);
1710}
1711
1712#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1713static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1714{
1715}
1716#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1717
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301718static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1719 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001720{
1721 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001722 int err = -EINVAL;
1723 int core;
1724 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001725
Michael Ellerman3102f782014-05-23 18:15:29 +10001726 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727 if (core >= KVM_MAX_VCORES)
1728 goto out;
1729
1730 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001731 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001732 if (!vcpu)
1733 goto out;
1734
1735 err = kvm_vcpu_init(vcpu, kvm, id);
1736 if (err)
1737 goto free_vcpu;
1738
1739 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001740#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1741 /*
1742 * The shared struct is never shared on HV,
1743 * so we can always use host endianness
1744 */
1745#ifdef __BIG_ENDIAN__
1746 vcpu->arch.shared_big_endian = true;
1747#else
1748 vcpu->arch.shared_big_endian = false;
1749#endif
1750#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001751 vcpu->arch.mmcr[0] = MMCR0_FC;
1752 vcpu->arch.ctrl = CTRL_RUNLATCH;
1753 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301754 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001755 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001756 spin_lock_init(&vcpu->arch.tbacct_lock);
1757 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001758 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001759
Paul Mackerrasde56a942011-06-29 00:21:34 +00001760 kvmppc_mmu_book3s_hv_init(vcpu);
1761
Paul Mackerras8455d792012-10-15 01:17:42 +00001762 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001763
1764 init_waitqueue_head(&vcpu->arch.cpu_run);
1765
1766 mutex_lock(&kvm->lock);
1767 vcore = kvm->arch.vcores[core];
1768 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001769 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001770 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001771 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001772 }
1773 mutex_unlock(&kvm->lock);
1774
1775 if (!vcore)
1776 goto free_vcpu;
1777
1778 spin_lock(&vcore->lock);
1779 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001780 spin_unlock(&vcore->lock);
1781 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001782 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001783 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001784
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001785 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1786 kvmppc_sanity_check(vcpu);
1787
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001788 debugfs_vcpu_init(vcpu, id);
1789
Paul Mackerrasde56a942011-06-29 00:21:34 +00001790 return vcpu;
1791
1792free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001793 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001794out:
1795 return ERR_PTR(err);
1796}
1797
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001798static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1799{
1800 if (vpa->pinned_addr)
1801 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1802 vpa->dirty);
1803}
1804
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301805static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001806{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001807 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001808 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1809 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1810 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001811 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001812 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001813 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001814}
1815
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301816static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1817{
1818 /* Indicate we want to get back into the guest */
1819 return 1;
1820}
1821
Paul Mackerras19ccb762011-07-23 17:42:46 +10001822static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001823{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001824 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001825
Paul Mackerras19ccb762011-07-23 17:42:46 +10001826 now = get_tb();
1827 if (now > vcpu->arch.dec_expires) {
1828 /* decrementer has already gone negative */
1829 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001830 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001831 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001832 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001833 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1834 / tb_ticks_per_sec;
1835 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1836 HRTIMER_MODE_REL);
1837 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001838}
1839
Paul Mackerras19ccb762011-07-23 17:42:46 +10001840static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001841{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001842 vcpu->arch.ceded = 0;
1843 if (vcpu->arch.timer_running) {
1844 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1845 vcpu->arch.timer_running = 0;
1846 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001847}
1848
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001849extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001850
1851static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1852 struct kvm_vcpu *vcpu)
1853{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001854 u64 now;
1855
Paul Mackerras371fefd2011-06-29 00:23:08 +00001856 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1857 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001858 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001859 now = mftb();
1860 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1861 vcpu->arch.stolen_logged;
1862 vcpu->arch.busy_preempt = now;
1863 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001864 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001865 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10001866 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001867}
1868
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001869static int kvmppc_grab_hwthread(int cpu)
1870{
1871 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001872 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001873
1874 tpaca = &paca[cpu];
1875
1876 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001877 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001878 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001879 tpaca->kvm_hstate.napping = 0;
1880 smp_wmb();
1881 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001882
1883 /*
1884 * If the thread is already executing in the kernel (e.g. handling
1885 * a stray interrupt), wait for it to get back to nap mode.
1886 * The smp_mb() is to ensure that our setting of hwthread_req
1887 * is visible before we look at hwthread_state, so if this
1888 * races with the code at system_reset_pSeries and the thread
1889 * misses our setting of hwthread_req, we are sure to see its
1890 * setting of hwthread_state, and vice versa.
1891 */
1892 smp_mb();
1893 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1894 if (--timeout <= 0) {
1895 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1896 return -EBUSY;
1897 }
1898 udelay(1);
1899 }
1900 return 0;
1901}
1902
1903static void kvmppc_release_hwthread(int cpu)
1904{
1905 struct paca_struct *tpaca;
1906
1907 tpaca = &paca[cpu];
1908 tpaca->kvm_hstate.hwthread_req = 0;
1909 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001910 tpaca->kvm_hstate.kvm_vcore = NULL;
1911 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001912}
1913
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001914static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001915{
1916 int cpu;
1917 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001918 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001919
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001920 cpu = vc->pcpu;
1921 if (vcpu) {
1922 if (vcpu->arch.timer_running) {
1923 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1924 vcpu->arch.timer_running = 0;
1925 }
1926 cpu += vcpu->arch.ptid;
1927 vcpu->cpu = mvc->pcpu;
1928 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001929 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001930 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001931 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001932 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001933 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001934 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001935 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001936 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001937 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001938}
1939
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001940static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001941{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001942 int cpu = smp_processor_id();
1943 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001944
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001945 for (loops = 0; loops < 1000000; ++loops) {
1946 /*
1947 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001948 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001949 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001950 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001951 */
1952 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001953 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001954 break;
1955 if (i == threads_per_subcore) {
1956 HMT_medium();
1957 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001958 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001959 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001960 }
1961 HMT_medium();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001962 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001963 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001964 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001965}
1966
1967/*
1968 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001969 * this core are off-line. Then grab the threads so they can't
1970 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001971 */
1972static int on_primary_thread(void)
1973{
1974 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001975 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001976
Michael Ellerman3102f782014-05-23 18:15:29 +10001977 /* Are we on a primary subcore? */
1978 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001979 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001980
1981 thr = 0;
1982 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001983 if (cpu_online(cpu + thr))
1984 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001985
1986 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001987 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001988 if (kvmppc_grab_hwthread(cpu + thr)) {
1989 /* Couldn't grab one; let the others go */
1990 do {
1991 kvmppc_release_hwthread(cpu + thr);
1992 } while (--thr > 0);
1993 return 0;
1994 }
1995 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001996 return 1;
1997}
1998
Paul Mackerrasec257162015-06-24 21:18:03 +10001999/*
2000 * A list of virtual cores for each physical CPU.
2001 * These are vcores that could run but their runner VCPU tasks are
2002 * (or may be) preempted.
2003 */
2004struct preempted_vcore_list {
2005 struct list_head list;
2006 spinlock_t lock;
2007};
2008
2009static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
2010
2011static void init_vcore_lists(void)
2012{
2013 int cpu;
2014
2015 for_each_possible_cpu(cpu) {
2016 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
2017 spin_lock_init(&lp->lock);
2018 INIT_LIST_HEAD(&lp->list);
2019 }
2020}
2021
2022static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
2023{
2024 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2025
2026 vc->vcore_state = VCORE_PREEMPT;
2027 vc->pcpu = smp_processor_id();
2028 if (vc->num_threads < threads_per_subcore) {
2029 spin_lock(&lp->lock);
2030 list_add_tail(&vc->preempt_list, &lp->list);
2031 spin_unlock(&lp->lock);
2032 }
2033
2034 /* Start accumulating stolen time */
2035 kvmppc_core_start_stolen(vc);
2036}
2037
2038static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
2039{
Paul Mackerras402813f2015-07-16 17:11:13 +10002040 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10002041
2042 kvmppc_core_end_stolen(vc);
2043 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10002044 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002045 spin_lock(&lp->lock);
2046 list_del_init(&vc->preempt_list);
2047 spin_unlock(&lp->lock);
2048 }
2049 vc->vcore_state = VCORE_INACTIVE;
2050}
2051
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002052/*
2053 * This stores information about the virtual cores currently
2054 * assigned to a physical core.
2055 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002056struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002057 int n_subcores;
2058 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002059 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002060 int subcore_threads[MAX_SUBCORES];
2061 struct kvm *subcore_vm[MAX_SUBCORES];
2062 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002063};
2064
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002065/*
2066 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
2067 * respectively in 2-way micro-threading (split-core) mode.
2068 */
2069static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2070
Paul Mackerrasec257162015-06-24 21:18:03 +10002071static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2072{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002073 int sub;
2074
Paul Mackerrasec257162015-06-24 21:18:03 +10002075 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002076 cip->n_subcores = 1;
2077 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002078 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002079 cip->subcore_threads[0] = vc->num_threads;
2080 cip->subcore_vm[0] = vc->kvm;
2081 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2082 INIT_LIST_HEAD(&cip->vcs[sub]);
2083 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2084}
2085
2086static bool subcore_config_ok(int n_subcores, int n_threads)
2087{
2088 /* Can only dynamically split if unsplit to begin with */
2089 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2090 return false;
2091 if (n_subcores > MAX_SUBCORES)
2092 return false;
2093 if (n_subcores > 1) {
2094 if (!(dynamic_mt_modes & 2))
2095 n_subcores = 4;
2096 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2097 return false;
2098 }
2099
2100 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002101}
2102
2103static void init_master_vcore(struct kvmppc_vcore *vc)
2104{
2105 vc->master_vcore = vc;
2106 vc->entry_exit_map = 0;
2107 vc->in_guest = 0;
2108 vc->napping_threads = 0;
2109 vc->conferring_threads = 0;
2110}
2111
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002112static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2113{
2114 int n_threads = vc->num_threads;
2115 int sub;
2116
2117 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2118 return false;
2119
2120 if (n_threads < cip->max_subcore_threads)
2121 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002122 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002123 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002124 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002125
2126 sub = cip->n_subcores;
2127 ++cip->n_subcores;
2128 cip->total_threads += vc->num_threads;
2129 cip->subcore_threads[sub] = vc->num_threads;
2130 cip->subcore_vm[sub] = vc->kvm;
2131 init_master_vcore(vc);
2132 list_del(&vc->preempt_list);
2133 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2134
2135 return true;
2136}
2137
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002138/*
2139 * Work out whether it is possible to piggyback the execution of
2140 * vcore *pvc onto the execution of the other vcores described in *cip.
2141 */
2142static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2143 int target_threads)
2144{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002145 if (cip->total_threads + pvc->num_threads > target_threads)
2146 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002147
Paul Mackerrasb0090312016-09-15 16:27:41 +10002148 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002149}
2150
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002151static void prepare_threads(struct kvmppc_vcore *vc)
2152{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002153 int i;
2154 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002155
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002156 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002157 if (signal_pending(vcpu->arch.run_task))
2158 vcpu->arch.ret = -EINTR;
2159 else if (vcpu->arch.vpa.update_pending ||
2160 vcpu->arch.slb_shadow.update_pending ||
2161 vcpu->arch.dtl.update_pending)
2162 vcpu->arch.ret = RESUME_GUEST;
2163 else
2164 continue;
2165 kvmppc_remove_runnable(vc, vcpu);
2166 wake_up(&vcpu->arch.cpu_run);
2167 }
2168}
2169
Paul Mackerrasec257162015-06-24 21:18:03 +10002170static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002171{
Paul Mackerrasec257162015-06-24 21:18:03 +10002172 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2173 struct kvmppc_vcore *pvc, *vcnext;
2174
2175 spin_lock(&lp->lock);
2176 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2177 if (!spin_trylock(&pvc->lock))
2178 continue;
2179 prepare_threads(pvc);
2180 if (!pvc->n_runnable) {
2181 list_del_init(&pvc->preempt_list);
2182 if (pvc->runner == NULL) {
2183 pvc->vcore_state = VCORE_INACTIVE;
2184 kvmppc_core_end_stolen(pvc);
2185 }
2186 spin_unlock(&pvc->lock);
2187 continue;
2188 }
2189 if (!can_piggyback(pvc, cip, target_threads)) {
2190 spin_unlock(&pvc->lock);
2191 continue;
2192 }
2193 kvmppc_core_end_stolen(pvc);
2194 pvc->vcore_state = VCORE_PIGGYBACK;
2195 if (cip->total_threads >= target_threads)
2196 break;
2197 }
2198 spin_unlock(&lp->lock);
2199}
2200
2201static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2202{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002203 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002204 u64 now;
2205 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002206 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002207
Paul Mackerrasec257162015-06-24 21:18:03 +10002208 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002209 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002210 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002211 /* cancel pending dec exception if dec is positive */
2212 if (now < vcpu->arch.dec_expires &&
2213 kvmppc_core_pending_dec(vcpu))
2214 kvmppc_core_dequeue_dec(vcpu);
2215
2216 trace_kvm_guest_exit(vcpu);
2217
2218 ret = RESUME_GUEST;
2219 if (vcpu->arch.trap)
2220 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2221 vcpu->arch.run_task);
2222
2223 vcpu->arch.ret = ret;
2224 vcpu->arch.trap = 0;
2225
Paul Mackerrasec257162015-06-24 21:18:03 +10002226 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2227 if (vcpu->arch.pending_exceptions)
2228 kvmppc_core_prepare_to_enter(vcpu);
2229 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002230 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002231 else
2232 ++still_running;
2233 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002234 kvmppc_remove_runnable(vc, vcpu);
2235 wake_up(&vcpu->arch.cpu_run);
2236 }
2237 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002238 list_del_init(&vc->preempt_list);
2239 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002240 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002241 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002242 } else if (vc->runner) {
2243 vc->vcore_state = VCORE_PREEMPT;
2244 kvmppc_core_start_stolen(vc);
2245 } else {
2246 vc->vcore_state = VCORE_INACTIVE;
2247 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002248 if (vc->n_runnable > 0 && vc->runner == NULL) {
2249 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002250 i = -1;
2251 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10002252 wake_up(&vcpu->arch.cpu_run);
2253 }
2254 }
2255 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002256}
2257
Paul Mackerras371fefd2011-06-29 00:23:08 +00002258/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002259 * Clear core from the list of active host cores as we are about to
2260 * enter the guest. Only do this if it is the primary thread of the
2261 * core (not if a subcore) that is entering the guest.
2262 */
2263static inline void kvmppc_clear_host_core(int cpu)
2264{
2265 int core;
2266
2267 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2268 return;
2269 /*
2270 * Memory barrier can be omitted here as we will do a smp_wmb()
2271 * later in kvmppc_start_thread and we need ensure that state is
2272 * visible to other CPUs only after we enter guest.
2273 */
2274 core = cpu >> threads_shift;
2275 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
2276}
2277
2278/*
2279 * Advertise this core as an active host core since we exited the guest
2280 * Only need to do this if it is the primary thread of the core that is
2281 * exiting.
2282 */
2283static inline void kvmppc_set_host_core(int cpu)
2284{
2285 int core;
2286
2287 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2288 return;
2289
2290 /*
2291 * Memory barrier can be omitted here because we do a spin_unlock
2292 * immediately after this which provides the memory barrier.
2293 */
2294 core = cpu >> threads_shift;
2295 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
2296}
2297
2298/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002299 * Run a set of guest threads on a physical core.
2300 * Called with vc->lock held.
2301 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002302static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002303{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002304 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002305 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002306 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002307 struct core_info core_info;
2308 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002309 struct kvm_split_mode split_info, *sip;
2310 int split, subcore_size, active;
2311 int sub;
2312 bool thr0_done;
2313 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002314 int pcpu, thr;
2315 int target_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002316
2317 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002318 * Remove from the list any threads that have a signal pending
2319 * or need a VPA update done
2320 */
2321 prepare_threads(vc);
2322
2323 /* if the runner is no longer runnable, let the caller pick a new one */
2324 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2325 return;
2326
2327 /*
2328 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002329 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002330 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002331 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002332
2333 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002334 * Make sure we are running on primary threads, and that secondary
2335 * threads are offline. Also check if the number of threads in this
2336 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002337 */
Michael Ellerman3102f782014-05-23 18:15:29 +10002338 if ((threads_per_core > 1) &&
2339 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002340 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002341 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002342 kvmppc_remove_runnable(vc, vcpu);
2343 wake_up(&vcpu->arch.cpu_run);
2344 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002345 goto out;
2346 }
2347
Paul Mackerrasec257162015-06-24 21:18:03 +10002348 /*
2349 * See if we could run any other vcores on the physical core
2350 * along with this one.
2351 */
2352 init_core_info(&core_info, vc);
2353 pcpu = smp_processor_id();
2354 target_threads = threads_per_subcore;
2355 if (target_smt_mode && target_smt_mode < target_threads)
2356 target_threads = target_smt_mode;
2357 if (vc->num_threads < target_threads)
2358 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002359
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002360 /* Decide on micro-threading (split-core) mode */
2361 subcore_size = threads_per_subcore;
2362 cmd_bit = stat_bit = 0;
2363 split = core_info.n_subcores;
2364 sip = NULL;
2365 if (split > 1) {
2366 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2367 if (split == 2 && (dynamic_mt_modes & 2)) {
2368 cmd_bit = HID0_POWER8_1TO2LPAR;
2369 stat_bit = HID0_POWER8_2LPARMODE;
2370 } else {
2371 split = 4;
2372 cmd_bit = HID0_POWER8_1TO4LPAR;
2373 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002374 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002375 subcore_size = MAX_SMT_THREADS / split;
2376 sip = &split_info;
2377 memset(&split_info, 0, sizeof(split_info));
2378 split_info.rpr = mfspr(SPRN_RPR);
2379 split_info.pmmar = mfspr(SPRN_PMMAR);
2380 split_info.ldbar = mfspr(SPRN_LDBAR);
2381 split_info.subcore_size = subcore_size;
2382 for (sub = 0; sub < core_info.n_subcores; ++sub)
2383 split_info.master_vcs[sub] =
2384 list_first_entry(&core_info.vcs[sub],
2385 struct kvmppc_vcore, preempt_list);
2386 /* order writes to split_info before kvm_split_mode pointer */
2387 smp_wmb();
2388 }
2389 pcpu = smp_processor_id();
2390 for (thr = 0; thr < threads_per_subcore; ++thr)
2391 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2392
2393 /* Initiate micro-threading (split-core) if required */
2394 if (cmd_bit) {
2395 unsigned long hid0 = mfspr(SPRN_HID0);
2396
2397 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2398 mb();
2399 mtspr(SPRN_HID0, hid0);
2400 isync();
2401 for (;;) {
2402 hid0 = mfspr(SPRN_HID0);
2403 if (hid0 & stat_bit)
2404 break;
2405 cpu_relax();
2406 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002407 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002408
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002409 kvmppc_clear_host_core(pcpu);
2410
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002411 /* Start all the threads */
2412 active = 0;
2413 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2414 thr = subcore_thread_map[sub];
2415 thr0_done = false;
2416 active |= 1 << thr;
2417 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2418 pvc->pcpu = pcpu + thr;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002419 for_each_runnable_thread(i, vcpu, pvc) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002420 kvmppc_start_thread(vcpu, pvc);
2421 kvmppc_create_dtl_entry(vcpu, pvc);
2422 trace_kvm_guest_enter(vcpu);
2423 if (!vcpu->arch.ptid)
2424 thr0_done = true;
2425 active |= 1 << (thr + vcpu->arch.ptid);
2426 }
2427 /*
2428 * We need to start the first thread of each subcore
2429 * even if it doesn't have a vcpu.
2430 */
2431 if (pvc->master_vcore == pvc && !thr0_done)
2432 kvmppc_start_thread(NULL, pvc);
2433 thr += pvc->num_threads;
2434 }
2435 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302436
2437 /*
2438 * Ensure that split_info.do_nap is set after setting
2439 * the vcore pointer in the PACA of the secondaries.
2440 */
2441 smp_mb();
2442 if (cmd_bit)
2443 split_info.do_nap = 1; /* ask secondaries to nap when done */
2444
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002445 /*
2446 * When doing micro-threading, poke the inactive threads as well.
2447 * This gets them to the nap instruction after kvm_do_nap,
2448 * which reduces the time taken to unsplit later.
2449 */
2450 if (split > 1)
2451 for (thr = 1; thr < threads_per_subcore; ++thr)
2452 if (!(active & (1 << thr)))
2453 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002454
Paul Mackerras2f12f032012-10-15 01:17:17 +00002455 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002456 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002457
2458 trace_kvmppc_run_core(vc, 0);
2459
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002460 for (sub = 0; sub < core_info.n_subcores; ++sub)
2461 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2462 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002463
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002464 guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002465
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002466 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002467
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002468 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002469
Paul Mackerrasec257162015-06-24 21:18:03 +10002470 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2471
2472 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002473 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002474 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002475
Paul Mackerras371fefd2011-06-29 00:23:08 +00002476 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002477 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002478
2479 /* Return to whole-core mode if we split the core earlier */
2480 if (split > 1) {
2481 unsigned long hid0 = mfspr(SPRN_HID0);
2482 unsigned long loops = 0;
2483
2484 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2485 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2486 mb();
2487 mtspr(SPRN_HID0, hid0);
2488 isync();
2489 for (;;) {
2490 hid0 = mfspr(SPRN_HID0);
2491 if (!(hid0 & stat_bit))
2492 break;
2493 cpu_relax();
2494 ++loops;
2495 }
2496 split_info.do_nap = 0;
2497 }
2498
2499 /* Let secondaries go back to the offline loop */
2500 for (i = 0; i < threads_per_subcore; ++i) {
2501 kvmppc_release_hwthread(pcpu + i);
2502 if (sip && sip->napped[i])
2503 kvmppc_ipi_thread(pcpu + i);
2504 }
2505
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002506 kvmppc_set_host_core(pcpu);
2507
Paul Mackerras371fefd2011-06-29 00:23:08 +00002508 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002509
Paul Mackerras371fefd2011-06-29 00:23:08 +00002510 /* make sure updates to secondary vcpu structs are visible now */
2511 smp_mb();
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002512 guest_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002513
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002514 for (sub = 0; sub < core_info.n_subcores; ++sub)
2515 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2516 preempt_list)
2517 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002518
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002519 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002520 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002521
Paul Mackerrasde56a942011-06-29 00:21:34 +00002522 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002523 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002524 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002525}
2526
Paul Mackerras19ccb762011-07-23 17:42:46 +10002527/*
2528 * Wait for some other vcpu thread to execute us, and
2529 * wake us up when we need to handle something in the host.
2530 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002531static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2532 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002533{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002534 DEFINE_WAIT(wait);
2535
Paul Mackerras19ccb762011-07-23 17:42:46 +10002536 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002537 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2538 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002539 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002540 spin_lock(&vc->lock);
2541 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002542 finish_wait(&vcpu->arch.cpu_run, &wait);
2543}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002544
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002545static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
2546{
2547 /* 10us base */
2548 if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
2549 vc->halt_poll_ns = 10000;
2550 else
2551 vc->halt_poll_ns *= halt_poll_ns_grow;
2552
2553 if (vc->halt_poll_ns > halt_poll_max_ns)
2554 vc->halt_poll_ns = halt_poll_max_ns;
2555}
2556
2557static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
2558{
2559 if (halt_poll_ns_shrink == 0)
2560 vc->halt_poll_ns = 0;
2561 else
2562 vc->halt_poll_ns /= halt_poll_ns_shrink;
2563}
2564
2565/* Check to see if any of the runnable vcpus on the vcore have pending
2566 * exceptions or are no longer ceded
2567 */
2568static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
2569{
2570 struct kvm_vcpu *vcpu;
2571 int i;
2572
2573 for_each_runnable_thread(i, vcpu, vc) {
2574 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded)
2575 return 1;
2576 }
2577
2578 return 0;
2579}
2580
Paul Mackerras19ccb762011-07-23 17:42:46 +10002581/*
2582 * All the vcpus in this vcore are idle, so wait for a decrementer
2583 * or external interrupt to one of the vcpus. vc->lock is held.
2584 */
2585static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2586{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002587 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002588 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002589 u64 block_ns;
Marcelo Tosatti85773702016-02-19 09:46:39 +01002590 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002591
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002592 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002593 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002594 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002595 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
2596 ++vc->runner->stat.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002597
2598 vc->vcore_state = VCORE_POLLING;
2599 spin_unlock(&vc->lock);
2600
2601 do {
2602 if (kvmppc_vcore_check_block(vc)) {
2603 do_sleep = 0;
2604 break;
2605 }
2606 cur = ktime_get();
2607 } while (single_task_running() && ktime_before(cur, stop));
2608
2609 spin_lock(&vc->lock);
2610 vc->vcore_state = VCORE_INACTIVE;
2611
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002612 if (!do_sleep) {
2613 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002614 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002615 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002616 }
2617
Marcelo Tosatti85773702016-02-19 09:46:39 +01002618 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002619
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002620 if (kvmppc_vcore_check_block(vc)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002621 finish_swait(&vc->wq, &wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002622 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002623 /* If we polled, count this as a successful poll */
2624 if (vc->halt_poll_ns)
2625 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002626 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002627 }
2628
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002629 start_wait = ktime_get();
2630
Paul Mackerras19ccb762011-07-23 17:42:46 +10002631 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002632 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002633 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002634 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01002635 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002636 spin_lock(&vc->lock);
2637 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002638 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002639 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002640
2641 cur = ktime_get();
2642
2643out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002644 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
2645
2646 /* Attribute wait time */
2647 if (do_sleep) {
2648 vc->runner->stat.halt_wait_ns +=
2649 ktime_to_ns(cur) - ktime_to_ns(start_wait);
2650 /* Attribute failed poll time */
2651 if (vc->halt_poll_ns)
2652 vc->runner->stat.halt_poll_fail_ns +=
2653 ktime_to_ns(start_wait) -
2654 ktime_to_ns(start_poll);
2655 } else {
2656 /* Attribute successful poll time */
2657 if (vc->halt_poll_ns)
2658 vc->runner->stat.halt_poll_success_ns +=
2659 ktime_to_ns(cur) -
2660 ktime_to_ns(start_poll);
2661 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002662
2663 /* Adjust poll time */
2664 if (halt_poll_max_ns) {
2665 if (block_ns <= vc->halt_poll_ns)
2666 ;
2667 /* We slept and blocked for longer than the max halt time */
2668 else if (vc->halt_poll_ns && block_ns > halt_poll_max_ns)
2669 shrink_halt_poll_ns(vc);
2670 /* We slept and our poll time is too small */
2671 else if (vc->halt_poll_ns < halt_poll_max_ns &&
2672 block_ns < halt_poll_max_ns)
2673 grow_halt_poll_ns(vc);
2674 } else
2675 vc->halt_poll_ns = 0;
2676
2677 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002678}
2679
2680static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2681{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002682 int n_ceded, i;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002683 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002684 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002685
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002686 trace_kvmppc_run_vcpu_enter(vcpu);
2687
Paul Mackerras371fefd2011-06-29 00:23:08 +00002688 kvm_run->exit_reason = 0;
2689 vcpu->arch.ret = RESUME_GUEST;
2690 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002691 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002692
Paul Mackerras371fefd2011-06-29 00:23:08 +00002693 /*
2694 * Synchronize with other threads in this virtual core
2695 */
2696 vc = vcpu->arch.vcore;
2697 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002698 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002699 vcpu->arch.run_task = current;
2700 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002701 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002702 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002703 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002704 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002705 ++vc->n_runnable;
2706
Paul Mackerras19ccb762011-07-23 17:42:46 +10002707 /*
2708 * This happens the first time this is called for a vcpu.
2709 * If the vcore is already running, we may be able to start
2710 * this thread straight away and have it join in.
2711 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002712 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002713 if (vc->vcore_state == VCORE_PIGGYBACK) {
2714 struct kvmppc_vcore *mvc = vc->master_vcore;
2715 if (spin_trylock(&mvc->lock)) {
2716 if (mvc->vcore_state == VCORE_RUNNING &&
2717 !VCORE_IS_EXITING(mvc)) {
2718 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002719 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002720 trace_kvm_guest_enter(vcpu);
2721 }
2722 spin_unlock(&mvc->lock);
2723 }
2724 } else if (vc->vcore_state == VCORE_RUNNING &&
2725 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002726 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002727 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002728 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002729 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002730 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002731 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002732
Paul Mackerras8455d792012-10-15 01:17:42 +00002733 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002734
2735 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2736 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002737 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2738 kvmppc_vcore_end_preempt(vc);
2739
Paul Mackerras8455d792012-10-15 01:17:42 +00002740 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002741 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002742 continue;
2743 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002744 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002745 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002746 if (signal_pending(v->arch.run_task)) {
2747 kvmppc_remove_runnable(vc, v);
2748 v->stat.signal_exits++;
2749 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2750 v->arch.ret = -EINTR;
2751 wake_up(&v->arch.cpu_run);
2752 }
2753 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002754 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2755 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002756 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002757 for_each_runnable_thread(i, v, vc) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002758 if (!v->arch.pending_exceptions)
2759 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002760 else
2761 v->arch.ceded = 0;
2762 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002763 vc->runner = vcpu;
2764 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002765 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002766 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002767 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002768 /* Let something else run */
2769 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002770 if (vc->vcore_state == VCORE_PREEMPT)
2771 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002772 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002773 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002774 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002775 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002776 }
2777
Paul Mackerras8455d792012-10-15 01:17:42 +00002778 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2779 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002780 vc->vcore_state == VCORE_EXITING ||
2781 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002782 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002783
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002784 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2785 kvmppc_vcore_end_preempt(vc);
2786
Paul Mackerras8455d792012-10-15 01:17:42 +00002787 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2788 kvmppc_remove_runnable(vc, vcpu);
2789 vcpu->stat.signal_exits++;
2790 kvm_run->exit_reason = KVM_EXIT_INTR;
2791 vcpu->arch.ret = -EINTR;
2792 }
2793
2794 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2795 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002796 i = -1;
2797 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00002798 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002799 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002800
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002801 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002802 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002803 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002804}
2805
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302806static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002807{
2808 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002809 int srcu_idx;
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002810 unsigned long ebb_regs[3] = {}; /* shut up GCC */
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002811 unsigned long user_tar = 0;
2812 unsigned int user_vrsave;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002813
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002814 if (!vcpu->arch.sane) {
2815 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2816 return -EINVAL;
2817 }
2818
Paul Mackerras468aa932017-06-15 16:10:27 +10002819 /*
2820 * Don't allow entry with a suspended transaction, because
2821 * the guest entry/exit code will lose it.
2822 * If the guest has TM enabled, save away their TM-related SPRs
2823 * (they will get restored by the TM unavailable interrupt).
2824 */
2825#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2826 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
2827 (current->thread.regs->msr & MSR_TM)) {
2828 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
2829 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2830 run->fail_entry.hardware_entry_failure_reason = 0;
2831 return -EINVAL;
2832 }
Paul Mackerrasd745f0f2017-07-21 13:57:14 +10002833 /* Enable TM so we can read the TM SPRs */
2834 mtmsr(mfmsr() | MSR_TM);
Paul Mackerras468aa932017-06-15 16:10:27 +10002835 current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
2836 current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
2837 current->thread.tm_texasr = mfspr(SPRN_TEXASR);
2838 current->thread.regs->msr &= ~MSR_TM;
2839 }
2840#endif
2841
Scott Wood25051b52011-11-08 18:23:23 -06002842 kvmppc_core_prepare_to_enter(vcpu);
2843
Paul Mackerras19ccb762011-07-23 17:42:46 +10002844 /* No need to go into the guest when all we'll do is come back out */
2845 if (signal_pending(current)) {
2846 run->exit_reason = KVM_EXIT_INTR;
2847 return -EINTR;
2848 }
2849
Paul Mackerras32fad282012-05-04 02:32:53 +00002850 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002851 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002852 smp_mb();
2853
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002854 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002855 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002856 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002857 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002858 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002859 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002860
Anton Blanchard579e6332015-10-29 11:44:09 +11002861 flush_all_to_thread(current);
2862
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002863 /* Save userspace EBB and other register values */
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002864 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
2865 ebb_regs[0] = mfspr(SPRN_EBBHR);
2866 ebb_regs[1] = mfspr(SPRN_EBBRR);
2867 ebb_regs[2] = mfspr(SPRN_BESCR);
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002868 user_tar = mfspr(SPRN_TAR);
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002869 }
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002870 user_vrsave = mfspr(SPRN_VRSAVE);
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002871
Paul Mackerras19ccb762011-07-23 17:42:46 +10002872 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002873 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002874 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002875
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002876 do {
2877 r = kvmppc_run_vcpu(run, vcpu);
2878
2879 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2880 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002881 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002882 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002883 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002884 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002885 } else if (r == RESUME_PAGE_FAULT) {
2886 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2887 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2888 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2889 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Suresh Warrierf7af5202016-08-19 15:35:52 +10002890 } else if (r == RESUME_PASSTHROUGH)
2891 r = kvmppc_xics_rm_complete(vcpu, 0);
Greg Kurze59d24e2014-02-06 17:36:56 +01002892 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002893
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002894 /* Restore userspace EBB and other register values */
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002895 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
2896 mtspr(SPRN_EBBHR, ebb_regs[0]);
2897 mtspr(SPRN_EBBRR, ebb_regs[1]);
2898 mtspr(SPRN_BESCR, ebb_regs[2]);
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002899 mtspr(SPRN_TAR, user_tar);
2900 mtspr(SPRN_FSCR, current->thread.fscr);
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002901 }
Paul Mackerrase5cd34d2017-06-15 15:43:17 +10002902 mtspr(SPRN_VRSAVE, user_vrsave);
Paul Mackerras2f1527e2017-06-06 16:47:22 +10002903
Paul Mackerras32fad282012-05-04 02:32:53 +00002904 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002905 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002906 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002907 return r;
2908}
2909
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002910static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2911 int linux_psize)
2912{
2913 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2914
2915 if (!def->shift)
2916 return;
2917 (*sps)->page_shift = def->shift;
2918 (*sps)->slb_enc = def->sllp;
2919 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002920 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302921 /*
2922 * Add 16MB MPSS support if host supports it
2923 */
2924 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2925 (*sps)->enc[1].page_shift = 24;
2926 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2927 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002928 (*sps)++;
2929}
2930
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302931static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2932 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002933{
2934 struct kvm_ppc_one_seg_page_size *sps;
2935
2936 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2937 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2938 info->flags |= KVM_PPC_1T_SEGMENTS;
2939 info->slb_size = mmu_slb_size;
2940
2941 /* We only support these sizes for now, and no muti-size segments */
2942 sps = &info->sps[0];
2943 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2944 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2945 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2946
2947 return 0;
2948}
2949
Paul Mackerras82ed3612011-12-15 02:03:22 +00002950/*
2951 * Get (and clear) the dirty memory log for a memory slot.
2952 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302953static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2954 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002955{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002956 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00002957 struct kvm_memory_slot *memslot;
2958 int r;
2959 unsigned long n;
2960
2961 mutex_lock(&kvm->slots_lock);
2962
2963 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002964 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002965 goto out;
2966
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002967 slots = kvm_memslots(kvm);
2968 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002969 r = -ENOENT;
2970 if (!memslot->dirty_bitmap)
2971 goto out;
2972
2973 n = kvm_dirty_bitmap_bytes(memslot);
2974 memset(memslot->dirty_bitmap, 0, n);
2975
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002976 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002977 if (r)
2978 goto out;
2979
2980 r = -EFAULT;
2981 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2982 goto out;
2983
2984 r = 0;
2985out:
2986 mutex_unlock(&kvm->slots_lock);
2987 return r;
2988}
2989
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302990static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2991 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002992{
2993 if (!dont || free->arch.rmap != dont->arch.rmap) {
2994 vfree(free->arch.rmap);
2995 free->arch.rmap = NULL;
2996 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002997}
2998
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302999static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
3000 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003001{
3002 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
3003 if (!slot->arch.rmap)
3004 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003005
3006 return 0;
3007}
3008
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303009static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
3010 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003011 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003012{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003013 return 0;
3014}
3015
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303016static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003017 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02003018 const struct kvm_memory_slot *old,
3019 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003020{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003021 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003022 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003023 struct kvm_memory_slot *memslot;
3024
Takuya Yoshikawa84826442013-02-27 19:45:25 +09003025 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003026 /*
3027 * If modifying a memslot, reset all the rmap dirty bits.
3028 * If this is a new memslot, we don't need to do anything
3029 * since the rmap array starts out as all zeroes,
3030 * i.e. no pages are dirty.
3031 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003032 slots = kvm_memslots(kvm);
3033 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003034 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
3035 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003036}
3037
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003038/*
3039 * Update LPCR values in kvm->arch and in vcores.
3040 * Caller must hold kvm->lock.
3041 */
3042void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
3043{
3044 long int i;
3045 u32 cores_done = 0;
3046
3047 if ((kvm->arch.lpcr & mask) == lpcr)
3048 return;
3049
3050 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
3051
3052 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3053 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3054 if (!vc)
3055 continue;
3056 spin_lock(&vc->lock);
3057 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
3058 spin_unlock(&vc->lock);
3059 if (++cores_done >= kvm->arch.online_vcores)
3060 break;
3061 }
3062}
3063
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303064static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
3065{
3066 return;
3067}
3068
Paul Mackerras32fad282012-05-04 02:32:53 +00003069static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003070{
3071 int err = 0;
3072 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003073 unsigned long hva;
3074 struct kvm_memory_slot *memslot;
3075 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003076 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003077 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003078 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003079
3080 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003081 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003082 goto out; /* another vcpu beat us to it */
3083
Paul Mackerras32fad282012-05-04 02:32:53 +00003084 /* Allocate hashed page table (if not done already) and reset it */
3085 if (!kvm->arch.hpt_virt) {
3086 err = kvmppc_alloc_hpt(kvm, NULL);
3087 if (err) {
3088 pr_err("KVM: Couldn't alloc HPT\n");
3089 goto out;
3090 }
3091 }
3092
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003093 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003094 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003095 memslot = gfn_to_memslot(kvm, 0);
3096
3097 /* We must have some memory at 0 by now */
3098 err = -EINVAL;
3099 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003100 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003101
3102 /* Look up the VMA for the start of this memory slot */
3103 hva = memslot->userspace_addr;
3104 down_read(&current->mm->mmap_sem);
3105 vma = find_vma(current->mm, hva);
3106 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3107 goto up_out;
3108
3109 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003110 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003111
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003112 up_read(&current->mm->mmap_sem);
3113
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003114 /* We can handle 4k, 64k or 16M pages in the VRMA */
3115 err = -EINVAL;
3116 if (!(psize == 0x1000 || psize == 0x10000 ||
3117 psize == 0x1000000))
3118 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003119
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003120 /* Update VRMASD field in the LPCR */
3121 senc = slb_pgsize_encoding(psize);
3122 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3123 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3124 /* the -4 is to account for senc values starting at 0x10 */
3125 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003126
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003127 /* Create HPTEs in the hash page table for the VRMA */
3128 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003129
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003130 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003131
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003132 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003133 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003134 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003135 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003136 out_srcu:
3137 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003138 out:
3139 mutex_unlock(&kvm->lock);
3140 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003141
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003142 up_out:
3143 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003144 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003145}
3146
Suresh Warrier79b6c242015-12-17 14:59:06 -06003147#ifdef CONFIG_KVM_XICS
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003148static int kvmppc_cpu_notify(struct notifier_block *self, unsigned long action,
3149 void *hcpu)
3150{
3151 unsigned long cpu = (long)hcpu;
3152
3153 switch (action) {
3154 case CPU_UP_PREPARE:
3155 case CPU_UP_PREPARE_FROZEN:
3156 kvmppc_set_host_core(cpu);
3157 break;
3158
3159#ifdef CONFIG_HOTPLUG_CPU
3160 case CPU_DEAD:
3161 case CPU_DEAD_FROZEN:
3162 case CPU_UP_CANCELED:
3163 case CPU_UP_CANCELED_FROZEN:
3164 kvmppc_clear_host_core(cpu);
3165 break;
3166#endif
3167 default:
3168 break;
3169 }
3170
3171 return NOTIFY_OK;
3172}
3173
3174static struct notifier_block kvmppc_cpu_notifier = {
3175 .notifier_call = kvmppc_cpu_notify,
3176};
3177
Suresh Warrier79b6c242015-12-17 14:59:06 -06003178/*
3179 * Allocate a per-core structure for managing state about which cores are
3180 * running in the host versus the guest and for exchanging data between
3181 * real mode KVM and CPU running in the host.
3182 * This is only done for the first VM.
3183 * The allocated structure stays even if all VMs have stopped.
3184 * It is only freed when the kvm-hv module is unloaded.
3185 * It's OK for this routine to fail, we just don't support host
3186 * core operations like redirecting H_IPI wakeups.
3187 */
3188void kvmppc_alloc_host_rm_ops(void)
3189{
3190 struct kvmppc_host_rm_ops *ops;
3191 unsigned long l_ops;
3192 int cpu, core;
3193 int size;
3194
3195 /* Not the first time here ? */
3196 if (kvmppc_host_rm_ops_hv != NULL)
3197 return;
3198
3199 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3200 if (!ops)
3201 return;
3202
3203 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3204 ops->rm_core = kzalloc(size, GFP_KERNEL);
3205
3206 if (!ops->rm_core) {
3207 kfree(ops);
3208 return;
3209 }
3210
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003211 get_online_cpus();
3212
Suresh Warrier79b6c242015-12-17 14:59:06 -06003213 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3214 if (!cpu_online(cpu))
3215 continue;
3216
3217 core = cpu >> threads_shift;
3218 ops->rm_core[core].rm_state.in_host = 1;
3219 }
3220
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003221 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3222
Suresh Warrier79b6c242015-12-17 14:59:06 -06003223 /*
3224 * Make the contents of the kvmppc_host_rm_ops structure visible
3225 * to other CPUs before we assign it to the global variable.
3226 * Do an atomic assignment (no locks used here), but if someone
3227 * beats us to it, just free our copy and return.
3228 */
3229 smp_wmb();
3230 l_ops = (unsigned long) ops;
3231
3232 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003233 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003234 kfree(ops->rm_core);
3235 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003236 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003237 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003238
3239 register_cpu_notifier(&kvmppc_cpu_notifier);
3240
3241 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003242}
3243
3244void kvmppc_free_host_rm_ops(void)
3245{
3246 if (kvmppc_host_rm_ops_hv) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003247 unregister_cpu_notifier(&kvmppc_cpu_notifier);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003248 kfree(kvmppc_host_rm_ops_hv->rm_core);
3249 kfree(kvmppc_host_rm_ops_hv);
3250 kvmppc_host_rm_ops_hv = NULL;
3251 }
3252}
3253#endif
3254
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303255static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003256{
Paul Mackerras32fad282012-05-04 02:32:53 +00003257 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003258 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00003259
Paul Mackerras32fad282012-05-04 02:32:53 +00003260 /* Allocate the guest's logical partition ID */
3261
3262 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003263 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003264 return -ENOMEM;
3265 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003266
Suresh Warrier79b6c242015-12-17 14:59:06 -06003267 kvmppc_alloc_host_rm_ops();
3268
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003269 /*
3270 * Since we don't flush the TLB when tearing down a VM,
3271 * and this lpid might have previously been used,
3272 * make sure we flush on each core before running the new VM.
3273 */
3274 cpumask_setall(&kvm->arch.need_tlb_flush);
3275
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003276 /* Start out with the default set of hcalls enabled */
3277 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3278 sizeof(kvm->arch.enabled_hcalls));
3279
Paul Mackerras9e368f22011-06-29 00:40:08 +00003280 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003281
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003282 /* Init LPCR for virtual RMA mode */
3283 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3284 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3285 lpcr &= LPCR_PECE | LPCR_LPES;
3286 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3287 LPCR_VPM0 | LPCR_VPM1;
3288 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3289 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3290 /* On POWER8 turn on online bit to enable PURR/SPURR */
3291 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3292 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00003293 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003294
Paul Mackerras512691d2012-10-15 01:15:41 +00003295 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003296 * Track that we now have a HV mode VM active. This blocks secondary
3297 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00003298 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10003299 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003300
Paul Mackerrase23a8082015-03-28 14:21:01 +11003301 /*
3302 * Create a debugfs directory for the VM
3303 */
3304 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3305 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3306 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3307 kvmppc_mmu_debugfs_init(kvm);
3308
David Gibson54738c02011-06-29 00:22:41 +00003309 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003310}
3311
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303312static void kvmppc_free_vcores(struct kvm *kvm)
3313{
3314 long int i;
3315
Paul Mackerras23316312015-10-21 16:03:14 +11003316 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303317 kfree(kvm->arch.vcores[i]);
3318 kvm->arch.online_vcores = 0;
3319}
3320
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303321static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003322{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003323 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3324
Michael Ellerman441c19c2014-05-23 18:15:25 +10003325 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003326
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303327 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003328
Paul Mackerrasde56a942011-06-29 00:21:34 +00003329 kvmppc_free_hpt(kvm);
Suresh Warrierc57875f2016-08-19 15:35:50 +10003330
3331 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003332}
3333
3334/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303335static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3336 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003337{
3338 return EMULATE_FAIL;
3339}
3340
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303341static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3342 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003343{
3344 return EMULATE_FAIL;
3345}
3346
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303347static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3348 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003349{
3350 return EMULATE_FAIL;
3351}
3352
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303353static int kvmppc_core_check_processor_compat_hv(void)
3354{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003355 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3356 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303357 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003358 /*
3359 * Disable KVM for Power9, untill the required bits merged.
3360 */
3361 if (cpu_has_feature(CPU_FTR_ARCH_300))
3362 return -EIO;
3363
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303364 return 0;
3365}
3366
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003367#ifdef CONFIG_KVM_XICS
3368
3369void kvmppc_free_pimap(struct kvm *kvm)
3370{
3371 kfree(kvm->arch.pimap);
3372}
3373
Suresh Warrierc57875f2016-08-19 15:35:50 +10003374static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003375{
3376 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
3377}
Suresh Warrierc57875f2016-08-19 15:35:50 +10003378
3379static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3380{
3381 struct irq_desc *desc;
3382 struct kvmppc_irq_map *irq_map;
3383 struct kvmppc_passthru_irqmap *pimap;
3384 struct irq_chip *chip;
3385 int i;
3386
Suresh Warrier644abbb2016-08-19 15:35:54 +10003387 if (!kvm_irq_bypass)
3388 return 1;
3389
Suresh Warrierc57875f2016-08-19 15:35:50 +10003390 desc = irq_to_desc(host_irq);
3391 if (!desc)
3392 return -EIO;
3393
3394 mutex_lock(&kvm->lock);
3395
3396 pimap = kvm->arch.pimap;
3397 if (pimap == NULL) {
3398 /* First call, allocate structure to hold IRQ map */
3399 pimap = kvmppc_alloc_pimap();
3400 if (pimap == NULL) {
3401 mutex_unlock(&kvm->lock);
3402 return -ENOMEM;
3403 }
3404 kvm->arch.pimap = pimap;
3405 }
3406
3407 /*
3408 * For now, we only support interrupts for which the EOI operation
3409 * is an OPAL call followed by a write to XIRR, since that's
3410 * what our real-mode EOI code does.
3411 */
3412 chip = irq_data_get_irq_chip(&desc->irq_data);
3413 if (!chip || !is_pnv_opal_msi(chip)) {
3414 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
3415 host_irq, guest_gsi);
3416 mutex_unlock(&kvm->lock);
3417 return -ENOENT;
3418 }
3419
3420 /*
3421 * See if we already have an entry for this guest IRQ number.
3422 * If it's mapped to a hardware IRQ number, that's an error,
3423 * otherwise re-use this entry.
3424 */
3425 for (i = 0; i < pimap->n_mapped; i++) {
3426 if (guest_gsi == pimap->mapped[i].v_hwirq) {
3427 if (pimap->mapped[i].r_hwirq) {
3428 mutex_unlock(&kvm->lock);
3429 return -EINVAL;
3430 }
3431 break;
3432 }
3433 }
3434
3435 if (i == KVMPPC_PIRQ_MAPPED) {
3436 mutex_unlock(&kvm->lock);
3437 return -EAGAIN; /* table is full */
3438 }
3439
3440 irq_map = &pimap->mapped[i];
3441
3442 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003443 irq_map->desc = desc;
3444
Suresh Warriere3c13e52016-08-19 15:35:51 +10003445 /*
3446 * Order the above two stores before the next to serialize with
3447 * the KVM real mode handler.
3448 */
3449 smp_wmb();
3450 irq_map->r_hwirq = desc->irq_data.hwirq;
3451
Suresh Warrierc57875f2016-08-19 15:35:50 +10003452 if (i == pimap->n_mapped)
3453 pimap->n_mapped++;
3454
Paul Mackerras5d375192016-08-19 15:35:56 +10003455 kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
3456
Suresh Warrierc57875f2016-08-19 15:35:50 +10003457 mutex_unlock(&kvm->lock);
3458
3459 return 0;
3460}
3461
3462static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3463{
3464 struct irq_desc *desc;
3465 struct kvmppc_passthru_irqmap *pimap;
3466 int i;
3467
Suresh Warrier644abbb2016-08-19 15:35:54 +10003468 if (!kvm_irq_bypass)
3469 return 0;
3470
Suresh Warrierc57875f2016-08-19 15:35:50 +10003471 desc = irq_to_desc(host_irq);
3472 if (!desc)
3473 return -EIO;
3474
3475 mutex_lock(&kvm->lock);
3476
3477 if (kvm->arch.pimap == NULL) {
3478 mutex_unlock(&kvm->lock);
3479 return 0;
3480 }
3481 pimap = kvm->arch.pimap;
3482
3483 for (i = 0; i < pimap->n_mapped; i++) {
3484 if (guest_gsi == pimap->mapped[i].v_hwirq)
3485 break;
3486 }
3487
3488 if (i == pimap->n_mapped) {
3489 mutex_unlock(&kvm->lock);
3490 return -ENODEV;
3491 }
3492
Paul Mackerras5d375192016-08-19 15:35:56 +10003493 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
3494
Suresh Warrierc57875f2016-08-19 15:35:50 +10003495 /* invalidate the entry */
3496 pimap->mapped[i].r_hwirq = 0;
3497
3498 /*
3499 * We don't free this structure even when the count goes to
3500 * zero. The structure is freed when we destroy the VM.
3501 */
3502
3503 mutex_unlock(&kvm->lock);
3504 return 0;
3505}
3506
3507static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
3508 struct irq_bypass_producer *prod)
3509{
3510 int ret = 0;
3511 struct kvm_kernel_irqfd *irqfd =
3512 container_of(cons, struct kvm_kernel_irqfd, consumer);
3513
3514 irqfd->producer = prod;
3515
3516 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3517 if (ret)
3518 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
3519 prod->irq, irqfd->gsi, ret);
3520
3521 return ret;
3522}
3523
3524static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
3525 struct irq_bypass_producer *prod)
3526{
3527 int ret;
3528 struct kvm_kernel_irqfd *irqfd =
3529 container_of(cons, struct kvm_kernel_irqfd, consumer);
3530
3531 irqfd->producer = NULL;
3532
3533 /*
3534 * When producer of consumer is unregistered, we change back to
3535 * default external interrupt handling mode - KVM real mode
3536 * will switch back to host.
3537 */
3538 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3539 if (ret)
3540 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
3541 prod->irq, irqfd->gsi, ret);
3542}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003543#endif
3544
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303545static long kvm_arch_vm_ioctl_hv(struct file *filp,
3546 unsigned int ioctl, unsigned long arg)
3547{
3548 struct kvm *kvm __maybe_unused = filp->private_data;
3549 void __user *argp = (void __user *)arg;
3550 long r;
3551
3552 switch (ioctl) {
3553
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303554 case KVM_PPC_ALLOCATE_HTAB: {
3555 u32 htab_order;
3556
3557 r = -EFAULT;
3558 if (get_user(htab_order, (u32 __user *)argp))
3559 break;
3560 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3561 if (r)
3562 break;
3563 r = -EFAULT;
3564 if (put_user(htab_order, (u32 __user *)argp))
3565 break;
3566 r = 0;
3567 break;
3568 }
3569
3570 case KVM_PPC_GET_HTAB_FD: {
3571 struct kvm_get_htab_fd ghf;
3572
3573 r = -EFAULT;
3574 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3575 break;
3576 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3577 break;
3578 }
3579
3580 default:
3581 r = -ENOTTY;
3582 }
3583
3584 return r;
3585}
3586
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003587/*
3588 * List of hcall numbers to enable by default.
3589 * For compatibility with old userspace, we enable by default
3590 * all hcalls that were implemented before the hcall-enabling
3591 * facility was added. Note this list should not include H_RTAS.
3592 */
3593static unsigned int default_hcall_list[] = {
3594 H_REMOVE,
3595 H_ENTER,
3596 H_READ,
3597 H_PROTECT,
3598 H_BULK_REMOVE,
3599 H_GET_TCE,
3600 H_PUT_TCE,
3601 H_SET_DABR,
3602 H_SET_XDABR,
3603 H_CEDE,
3604 H_PROD,
3605 H_CONFER,
3606 H_REGISTER_VPA,
3607#ifdef CONFIG_KVM_XICS
3608 H_EOI,
3609 H_CPPR,
3610 H_IPI,
3611 H_IPOLL,
3612 H_XIRR,
3613 H_XIRR_X,
3614#endif
3615 0
3616};
3617
3618static void init_default_hcalls(void)
3619{
3620 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003621 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003622
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003623 for (i = 0; default_hcall_list[i]; ++i) {
3624 hcall = default_hcall_list[i];
3625 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3626 __set_bit(hcall / 4, default_enabled_hcalls);
3627 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003628}
3629
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303630static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303631 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3632 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3633 .get_one_reg = kvmppc_get_one_reg_hv,
3634 .set_one_reg = kvmppc_set_one_reg_hv,
3635 .vcpu_load = kvmppc_core_vcpu_load_hv,
3636 .vcpu_put = kvmppc_core_vcpu_put_hv,
3637 .set_msr = kvmppc_set_msr_hv,
3638 .vcpu_run = kvmppc_vcpu_run_hv,
3639 .vcpu_create = kvmppc_core_vcpu_create_hv,
3640 .vcpu_free = kvmppc_core_vcpu_free_hv,
3641 .check_requests = kvmppc_core_check_requests_hv,
3642 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3643 .flush_memslot = kvmppc_core_flush_memslot_hv,
3644 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3645 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3646 .unmap_hva = kvm_unmap_hva_hv,
3647 .unmap_hva_range = kvm_unmap_hva_range_hv,
3648 .age_hva = kvm_age_hva_hv,
3649 .test_age_hva = kvm_test_age_hva_hv,
3650 .set_spte_hva = kvm_set_spte_hva_hv,
3651 .mmu_destroy = kvmppc_mmu_destroy_hv,
3652 .free_memslot = kvmppc_core_free_memslot_hv,
3653 .create_memslot = kvmppc_core_create_memslot_hv,
3654 .init_vm = kvmppc_core_init_vm_hv,
3655 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303656 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3657 .emulate_op = kvmppc_core_emulate_op_hv,
3658 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3659 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3660 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3661 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003662 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10003663#ifdef CONFIG_KVM_XICS
3664 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
3665 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
3666#endif
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303667};
3668
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303669static int kvm_init_subcore_bitmap(void)
3670{
3671 int i, j;
3672 int nr_cores = cpu_nr_cores();
3673 struct sibling_subcore_state *sibling_subcore_state;
3674
3675 for (i = 0; i < nr_cores; i++) {
3676 int first_cpu = i * threads_per_core;
3677 int node = cpu_to_node(first_cpu);
3678
3679 /* Ignore if it is already allocated. */
3680 if (paca[first_cpu].sibling_subcore_state)
3681 continue;
3682
3683 sibling_subcore_state =
3684 kmalloc_node(sizeof(struct sibling_subcore_state),
3685 GFP_KERNEL, node);
3686 if (!sibling_subcore_state)
3687 return -ENOMEM;
3688
3689 memset(sibling_subcore_state, 0,
3690 sizeof(struct sibling_subcore_state));
3691
3692 for (j = 0; j < threads_per_core; j++) {
3693 int cpu = first_cpu + j;
3694
3695 paca[cpu].sibling_subcore_state = sibling_subcore_state;
3696 }
3697 }
3698 return 0;
3699}
3700
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303701static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003702{
3703 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303704 /*
3705 * FIXME!! Do we need to check on all cpus ?
3706 */
3707 r = kvmppc_core_check_processor_compat_hv();
3708 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003709 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003710
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303711 r = kvm_init_subcore_bitmap();
3712 if (r)
3713 return r;
3714
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303715 kvm_ops_hv.owner = THIS_MODULE;
3716 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003717
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003718 init_default_hcalls();
3719
Paul Mackerrasec257162015-06-24 21:18:03 +10003720 init_vcore_lists();
3721
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303722 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003723 return r;
3724}
3725
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303726static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003727{
Suresh Warrier79b6c242015-12-17 14:59:06 -06003728 kvmppc_free_host_rm_ops();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303729 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003730}
3731
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303732module_init(kvmppc_book3s_init_hv);
3733module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05303734MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01003735MODULE_ALIAS_MISCDEV(KVM_MINOR);
3736MODULE_ALIAS("devname:kvm");