blob: 400a5992b121de35817b2dbb24188fb849af3a00 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010026#include <linux/sched/stat.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040028#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000029#include <linux/fs.h>
30#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100031#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000032#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000033#include <linux/spinlock.h>
34#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000035#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010036#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110037#include <linux/debugfs.h>
Benjamin Herrenschmidtd3989142017-04-05 17:54:51 +100038#include <linux/gfp.h>
39#include <linux/vmalloc.h>
40#include <linux/highmem.h>
41#include <linux/hugetlb.h>
42#include <linux/kvm_irqfd.h>
43#include <linux/irqbypass.h>
44#include <linux/module.h>
45#include <linux/compiler.h>
46#include <linux/of.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000047
48#include <asm/reg.h>
49#include <asm/cputable.h>
50#include <asm/cacheflush.h>
51#include <asm/tlbflush.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080052#include <linux/uaccess.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <asm/io.h>
54#include <asm/kvm_ppc.h>
55#include <asm/kvm_book3s.h>
56#include <asm/mmu_context.h>
57#include <asm/lppaca.h>
58#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000059#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000060#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000061#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010062#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000063#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110064#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053065#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100066#include <asm/pnv-pci.h>
Paul Mackerras7a840842016-11-16 22:25:20 +110067#include <asm/mmu.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110068#include <asm/opal.h>
69#include <asm/xics.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100070#include <asm/xive.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000071
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053072#include "book3s.h"
73
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060074#define CREATE_TRACE_POINTS
75#include "trace_hv.h"
76
Paul Mackerrasde56a942011-06-29 00:21:34 +000077/* #define EXIT_DEBUG */
78/* #define EXIT_DEBUG_SIMPLE */
79/* #define EXIT_DEBUG_INT */
80
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000081/* Used to indicate that a guest page fault needs to be handled */
82#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100083/* Used to indicate that a guest passthrough interrupt needs to be handled */
84#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000085
Paul Mackerrasc7b67672012-10-15 01:18:07 +000086/* Used as a "null" value for timebase values */
87#define TB_NIL (~(u64)0)
88
Paul Mackerras699a0ea2014-06-02 11:02:59 +100089static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
90
Paul Mackerrasb4deba52015-07-02 20:38:16 +100091static int dynamic_mt_modes = 6;
92module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
93MODULE_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 +100094static int target_smt_mode;
95module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
96MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100097
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -060098#ifdef CONFIG_KVM_XICS
99static struct kernel_param_ops module_param_ops = {
100 .set = param_set_int,
101 .get = param_get_int,
102};
103
Suresh Warrier644abbb2016-08-19 15:35:54 +1000104module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
105 S_IRUGO | S_IWUSR);
106MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
107
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600108module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
109 S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
111#endif
112
Paul Mackerras19ccb762011-07-23 17:42:46 +1000113static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +0000114static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000115
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000116static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
117 int *ip)
118{
119 int i = *ip;
120 struct kvm_vcpu *vcpu;
121
122 while (++i < MAX_SMT_THREADS) {
123 vcpu = READ_ONCE(vc->runnable_threads[i]);
124 if (vcpu) {
125 *ip = i;
126 return vcpu;
127 }
128 }
129 return NULL;
130}
131
132/* Used to traverse the list of runnable threads for a given vcore */
133#define for_each_runnable_thread(i, vcpu, vc) \
134 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
135
Paul Mackerras66feed62015-03-28 14:21:12 +1100136static bool kvmppc_ipi_thread(int cpu)
137{
Paul Mackerras1704a812016-11-18 08:47:08 +1100138 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
139
140 /* On POWER9 we can use msgsnd to IPI any cpu */
141 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
142 msg |= get_hard_smp_processor_id(cpu);
143 smp_mb();
144 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
145 return true;
146 }
147
Paul Mackerras66feed62015-03-28 14:21:12 +1100148 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
149 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
150 preempt_disable();
151 if (cpu_first_thread_sibling(cpu) ==
152 cpu_first_thread_sibling(smp_processor_id())) {
Paul Mackerras66feed62015-03-28 14:21:12 +1100153 msg |= cpu_thread_in_core(cpu);
154 smp_mb();
155 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
156 preempt_enable();
157 return true;
158 }
159 preempt_enable();
160 }
161
162#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrasf7257582016-11-18 09:02:08 +1100163 if (cpu >= 0 && cpu < nr_cpu_ids) {
164 if (paca[cpu].kvm_hstate.xics_phys) {
165 xics_wake_cpu(cpu);
166 return true;
167 }
168 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
Paul Mackerras66feed62015-03-28 14:21:12 +1100169 return true;
170 }
171#endif
172
173 return false;
174}
175
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530176static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000177{
Paul Mackerrasec257162015-06-24 21:18:03 +1000178 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100179 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000180
181 wqp = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100182 if (swait_active(wqp)) {
183 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000184 ++vcpu->stat.halt_wakeup;
185 }
186
Paul Mackerras3deda5e2016-12-20 14:02:29 +1100187 cpu = READ_ONCE(vcpu->arch.thread_cpu);
188 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100189 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000190
191 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000192 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100193 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
194 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000195}
196
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000197/*
198 * We use the vcpu_load/put functions to measure stolen time.
199 * Stolen time is counted as time when either the vcpu is able to
200 * run as part of a virtual core, but the task running the vcore
201 * is preempted or sleeping, or when the vcpu needs something done
202 * in the kernel by the task running the vcpu, but that task is
203 * preempted or sleeping. Those two things have to be counted
204 * separately, since one of the vcpu tasks will take on the job
205 * of running the core, and the other vcpu tasks in the vcore will
206 * sleep waiting for it to do that, but that sleep shouldn't count
207 * as stolen time.
208 *
209 * Hence we accumulate stolen time when the vcpu can run as part of
210 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
211 * needs its task to do other things in the kernel (for example,
212 * service a page fault) in busy_stolen. We don't accumulate
213 * stolen time for a vcore when it is inactive, or for a vcpu
214 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
215 * a misnomer; it means that the vcpu task is not executing in
216 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
217 * the kernel. We don't have any way of dividing up that time
218 * between time that the vcpu is genuinely stopped, time that
219 * the task is actively working on behalf of the vcpu, and time
220 * that the task is preempted, so we don't count any of it as
221 * stolen.
222 *
223 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100224 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
225 * lock. The stolen times are measured in units of timebase ticks.
226 * (Note that the != TB_NIL checks below are purely defensive;
227 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000228 */
229
Paul Mackerrasec257162015-06-24 21:18:03 +1000230static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
231{
232 unsigned long flags;
233
234 spin_lock_irqsave(&vc->stoltb_lock, flags);
235 vc->preempt_tb = mftb();
236 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
237}
238
239static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
240{
241 unsigned long flags;
242
243 spin_lock_irqsave(&vc->stoltb_lock, flags);
244 if (vc->preempt_tb != TB_NIL) {
245 vc->stolen_tb += mftb() - vc->preempt_tb;
246 vc->preempt_tb = TB_NIL;
247 }
248 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
249}
250
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530251static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000252{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000253 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100254 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000255
Paul Mackerras2711e242014-12-04 16:43:28 +1100256 /*
257 * We can test vc->runner without taking the vcore lock,
258 * because only this task ever sets vc->runner to this
259 * vcpu, and once it is set to this vcpu, only this task
260 * ever sets it to NULL.
261 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000262 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
263 kvmppc_core_end_stolen(vc);
264
Paul Mackerras2711e242014-12-04 16:43:28 +1100265 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000266 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
267 vcpu->arch.busy_preempt != TB_NIL) {
268 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
269 vcpu->arch.busy_preempt = TB_NIL;
270 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100271 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000272}
273
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530274static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000275{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000276 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100277 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000278
Paul Mackerrasec257162015-06-24 21:18:03 +1000279 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
280 kvmppc_core_start_stolen(vc);
281
Paul Mackerras2711e242014-12-04 16:43:28 +1100282 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000283 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
284 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100285 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000286}
287
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530288static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000289{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100290 /*
291 * Check for illegal transactional state bit combination
292 * and if we find it, force the TS field to a safe state.
293 */
294 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
295 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000296 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000297 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000298}
299
Thomas Huth5358a962015-05-22 09:25:02 +0200300static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000301{
302 vcpu->arch.pvr = pvr;
303}
304
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100305/* Dummy value used in computing PCR value below */
306#define PCR_ARCH_300 (PCR_ARCH_207 << 1)
307
Thomas Huth5358a962015-05-22 09:25:02 +0200308static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000309{
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100310 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000311 struct kvmppc_vcore *vc = vcpu->arch.vcore;
312
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100313 /* We can (emulate) our own architecture version and anything older */
314 if (cpu_has_feature(CPU_FTR_ARCH_300))
315 host_pcr_bit = PCR_ARCH_300;
316 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
317 host_pcr_bit = PCR_ARCH_207;
318 else if (cpu_has_feature(CPU_FTR_ARCH_206))
319 host_pcr_bit = PCR_ARCH_206;
320 else
321 host_pcr_bit = PCR_ARCH_205;
322
323 /* Determine lowest PCR bit needed to run guest in given PVR level */
324 guest_pcr_bit = host_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000325 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000326 switch (arch_compat) {
327 case PVR_ARCH_205:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100328 guest_pcr_bit = PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000329 break;
330 case PVR_ARCH_206:
331 case PVR_ARCH_206p:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100332 guest_pcr_bit = PCR_ARCH_206;
Paul Mackerras5557ae02014-01-08 21:25:24 +1100333 break;
334 case PVR_ARCH_207:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100335 guest_pcr_bit = PCR_ARCH_207;
336 break;
337 case PVR_ARCH_300:
338 guest_pcr_bit = PCR_ARCH_300;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000339 break;
340 default:
341 return -EINVAL;
342 }
343 }
344
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100345 /* Check requested PCR bits don't exceed our capabilities */
346 if (guest_pcr_bit > host_pcr_bit)
347 return -EINVAL;
348
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000349 spin_lock(&vc->lock);
350 vc->arch_compat = arch_compat;
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100351 /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
352 vc->pcr = host_pcr_bit - guest_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000353 spin_unlock(&vc->lock);
354
355 return 0;
356}
357
Thomas Huth5358a962015-05-22 09:25:02 +0200358static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000359{
360 int r;
361
362 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
363 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
364 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
365 for (r = 0; r < 16; ++r)
366 pr_err("r%2d = %.16lx r%d = %.16lx\n",
367 r, kvmppc_get_gpr(vcpu, r),
368 r+16, kvmppc_get_gpr(vcpu, r+16));
369 pr_err("ctr = %.16lx lr = %.16lx\n",
370 vcpu->arch.ctr, vcpu->arch.lr);
371 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
372 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
373 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
374 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
375 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
376 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
377 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
378 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
379 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
380 pr_err("fault dar = %.16lx dsisr = %.8x\n",
381 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
382 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
383 for (r = 0; r < vcpu->arch.slb_max; ++r)
384 pr_err(" ESID = %.16llx VSID = %.16llx\n",
385 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
386 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000387 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000388 vcpu->arch.last_inst);
389}
390
Thomas Huth5358a962015-05-22 09:25:02 +0200391static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000392{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100393 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000394
395 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100396 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000397 mutex_unlock(&kvm->lock);
398 return ret;
399}
400
401static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
402{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000403 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200404 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405}
406
Paul Mackerras55b665b2012-09-25 20:33:06 +0000407static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
408 unsigned long addr, unsigned long len)
409{
410 /* check address is cacheline aligned */
411 if (addr & (L1_CACHE_BYTES - 1))
412 return -EINVAL;
413 spin_lock(&vcpu->arch.vpa_update_lock);
414 if (v->next_gpa != addr || v->len != len) {
415 v->next_gpa = addr;
416 v->len = addr ? len : 0;
417 v->update_pending = 1;
418 }
419 spin_unlock(&vcpu->arch.vpa_update_lock);
420 return 0;
421}
422
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000423/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
424struct reg_vpa {
425 u32 dummy;
426 union {
Alexander Graf02407552014-06-11 10:34:19 +0200427 __be16 hword;
428 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000429 } length;
430};
431
432static int vpa_is_registered(struct kvmppc_vpa *vpap)
433{
434 if (vpap->update_pending)
435 return vpap->next_gpa != 0;
436 return vpap->pinned_addr != NULL;
437}
438
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000439static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
440 unsigned long flags,
441 unsigned long vcpuid, unsigned long vpa)
442{
443 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000444 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000445 void *va;
446 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000447 int err;
448 int subfunc;
449 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000450
451 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
452 if (!tvcpu)
453 return H_PARAMETER;
454
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000455 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
456 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
457 subfunc == H_VPA_REG_SLB) {
458 /* Registering new area - address must be cache-line aligned */
459 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000460 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000461
462 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000463 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
464 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000465 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000466 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200467 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000468 else
Alexander Graf02407552014-06-11 10:34:19 +0200469 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000470 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000471
472 /* Check length */
473 if (len > nb || len < sizeof(struct reg_vpa))
474 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000475 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000476 vpa = 0;
477 len = 0;
478 }
479
480 err = H_PARAMETER;
481 vpap = NULL;
482 spin_lock(&tvcpu->arch.vpa_update_lock);
483
484 switch (subfunc) {
485 case H_VPA_REG_VPA: /* register VPA */
486 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000487 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000488 vpap = &tvcpu->arch.vpa;
489 err = 0;
490 break;
491
492 case H_VPA_REG_DTL: /* register DTL */
493 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000494 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000495 len -= len % sizeof(struct dtl_entry);
496
497 /* Check that they have previously registered a VPA */
498 err = H_RESOURCE;
499 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000500 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000501
502 vpap = &tvcpu->arch.dtl;
503 err = 0;
504 break;
505
506 case H_VPA_REG_SLB: /* register SLB shadow buffer */
507 /* Check that they have previously registered a VPA */
508 err = H_RESOURCE;
509 if (!vpa_is_registered(&tvcpu->arch.vpa))
510 break;
511
512 vpap = &tvcpu->arch.slb_shadow;
513 err = 0;
514 break;
515
516 case H_VPA_DEREG_VPA: /* deregister VPA */
517 /* Check they don't still have a DTL or SLB buf registered */
518 err = H_RESOURCE;
519 if (vpa_is_registered(&tvcpu->arch.dtl) ||
520 vpa_is_registered(&tvcpu->arch.slb_shadow))
521 break;
522
523 vpap = &tvcpu->arch.vpa;
524 err = 0;
525 break;
526
527 case H_VPA_DEREG_DTL: /* deregister DTL */
528 vpap = &tvcpu->arch.dtl;
529 err = 0;
530 break;
531
532 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
533 vpap = &tvcpu->arch.slb_shadow;
534 err = 0;
535 break;
536 }
537
538 if (vpap) {
539 vpap->next_gpa = vpa;
540 vpap->len = len;
541 vpap->update_pending = 1;
542 }
543
544 spin_unlock(&tvcpu->arch.vpa_update_lock);
545
546 return err;
547}
548
Paul Mackerras081f3232012-06-01 20:20:24 +1000549static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000550{
Paul Mackerras081f3232012-06-01 20:20:24 +1000551 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000552 void *va;
553 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000554 unsigned long gpa;
555
556 /*
557 * We need to pin the page pointed to by vpap->next_gpa,
558 * but we can't call kvmppc_pin_guest_page under the lock
559 * as it does get_user_pages() and down_read(). So we
560 * have to drop the lock, pin the page, then get the lock
561 * again and check that a new area didn't get registered
562 * in the meantime.
563 */
564 for (;;) {
565 gpa = vpap->next_gpa;
566 spin_unlock(&vcpu->arch.vpa_update_lock);
567 va = NULL;
568 nb = 0;
569 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000570 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000571 spin_lock(&vcpu->arch.vpa_update_lock);
572 if (gpa == vpap->next_gpa)
573 break;
574 /* sigh... unpin that one and try again */
575 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000576 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000577 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000578
579 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000580 if (va && nb < vpap->len) {
581 /*
582 * If it's now too short, it must be that userspace
583 * has changed the mappings underlying guest memory,
584 * so unregister the region.
585 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000586 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000587 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000588 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000589 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000590 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
591 vpap->dirty);
592 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000593 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000594 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000595 if (va)
596 vpap->pinned_end = va + vpap->len;
597}
Paul Mackerras93e60242011-12-12 12:28:55 +0000598
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000599static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
600{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000601 if (!(vcpu->arch.vpa.update_pending ||
602 vcpu->arch.slb_shadow.update_pending ||
603 vcpu->arch.dtl.update_pending))
604 return;
605
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000606 spin_lock(&vcpu->arch.vpa_update_lock);
607 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000608 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000609 if (vcpu->arch.vpa.pinned_addr)
610 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000611 }
612 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000613 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000614 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
615 vcpu->arch.dtl_index = 0;
616 }
617 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000618 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000619 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000620}
621
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000622/*
623 * Return the accumulated stolen time for the vcore up until `now'.
624 * The caller should hold the vcore lock.
625 */
626static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
627{
628 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100629 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000630
Paul Mackerras2711e242014-12-04 16:43:28 +1100631 spin_lock_irqsave(&vc->stoltb_lock, flags);
632 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000633 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100634 vc->preempt_tb != TB_NIL)
635 p += now - vc->preempt_tb;
636 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000637 return p;
638}
639
Paul Mackerras0456ec42012-02-03 00:56:21 +0000640static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
641 struct kvmppc_vcore *vc)
642{
643 struct dtl_entry *dt;
644 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000645 unsigned long stolen;
646 unsigned long core_stolen;
647 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000648
649 dt = vcpu->arch.dtl_ptr;
650 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000651 now = mftb();
652 core_stolen = vcore_stolen_time(vc, now);
653 stolen = core_stolen - vcpu->arch.stolen_logged;
654 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100655 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000656 stolen += vcpu->arch.busy_stolen;
657 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100658 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000659 if (!dt || !vpa)
660 return;
661 memset(dt, 0, sizeof(struct dtl_entry));
662 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200663 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
664 dt->timebase = cpu_to_be64(now + vc->tb_offset);
665 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
666 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
667 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000668 ++dt;
669 if (dt == vcpu->arch.dtl.pinned_end)
670 dt = vcpu->arch.dtl.pinned_addr;
671 vcpu->arch.dtl_ptr = dt;
672 /* order writing *dt vs. writing vpa->dtl_idx */
673 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200674 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000675 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000676}
677
Michael Neuling96423822014-06-02 11:03:01 +1000678static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
679{
680 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
681 return true;
682 if ((!vcpu->arch.vcore->arch_compat) &&
683 cpu_has_feature(CPU_FTR_ARCH_207S))
684 return true;
685 return false;
686}
687
688static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
689 unsigned long resource, unsigned long value1,
690 unsigned long value2)
691{
692 switch (resource) {
693 case H_SET_MODE_RESOURCE_SET_CIABR:
694 if (!kvmppc_power8_compatible(vcpu))
695 return H_P2;
696 if (value2)
697 return H_P4;
698 if (mflags)
699 return H_UNSUPPORTED_FLAG_START;
700 /* Guests can't breakpoint the hypervisor */
701 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
702 return H_P3;
703 vcpu->arch.ciabr = value1;
704 return H_SUCCESS;
705 case H_SET_MODE_RESOURCE_SET_DAWR:
706 if (!kvmppc_power8_compatible(vcpu))
707 return H_P2;
708 if (mflags)
709 return H_UNSUPPORTED_FLAG_START;
710 if (value2 & DABRX_HYP)
711 return H_P4;
712 vcpu->arch.dawr = value1;
713 vcpu->arch.dawrx = value2;
714 return H_SUCCESS;
715 default:
716 return H_TOO_HARD;
717 }
718}
719
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100720static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
721{
722 struct kvmppc_vcore *vcore = target->arch.vcore;
723
724 /*
725 * We expect to have been called by the real mode handler
726 * (kvmppc_rm_h_confer()) which would have directly returned
727 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
728 * have useful work to do and should not confer) so we don't
729 * recheck that here.
730 */
731
732 spin_lock(&vcore->lock);
733 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000734 vcore->vcore_state != VCORE_INACTIVE &&
735 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100736 target = vcore->runner;
737 spin_unlock(&vcore->lock);
738
739 return kvm_vcpu_yield_to(target);
740}
741
742static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
743{
744 int yield_count = 0;
745 struct lppaca *lppaca;
746
747 spin_lock(&vcpu->arch.vpa_update_lock);
748 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
749 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100750 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100751 spin_unlock(&vcpu->arch.vpa_update_lock);
752 return yield_count;
753}
754
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000755int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
756{
757 unsigned long req = kvmppc_get_gpr(vcpu, 3);
758 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100759 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000760 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000761 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000762
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000763 if (req <= MAX_HCALL_OPCODE &&
764 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
765 return RESUME_HOST;
766
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000767 switch (req) {
768 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000769 break;
770 case H_PROD:
771 target = kvmppc_get_gpr(vcpu, 4);
772 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
773 if (!tvcpu) {
774 ret = H_PARAMETER;
775 break;
776 }
777 tvcpu->arch.prodded = 1;
778 smp_mb();
Paul Mackerras8464c882016-12-06 20:42:05 +1100779 if (tvcpu->arch.ceded)
780 kvmppc_fast_vcpu_kick_hv(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000781 break;
782 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000783 target = kvmppc_get_gpr(vcpu, 4);
784 if (target == -1)
785 break;
786 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
787 if (!tvcpu) {
788 ret = H_PARAMETER;
789 break;
790 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100791 yield_count = kvmppc_get_gpr(vcpu, 5);
792 if (kvmppc_get_yield_count(tvcpu) != yield_count)
793 break;
794 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000795 break;
796 case H_REGISTER_VPA:
797 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
798 kvmppc_get_gpr(vcpu, 5),
799 kvmppc_get_gpr(vcpu, 6));
800 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000801 case H_RTAS:
802 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
803 return RESUME_HOST;
804
Paul Mackerrasc9438092013-11-16 17:46:05 +1100805 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000806 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100807 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000808
809 if (rc == -ENOENT)
810 return RESUME_HOST;
811 else if (rc == 0)
812 break;
813
814 /* Send the error out to userspace via KVM_RUN */
815 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100816 case H_LOGICAL_CI_LOAD:
817 ret = kvmppc_h_logical_ci_load(vcpu);
818 if (ret == H_TOO_HARD)
819 return RESUME_HOST;
820 break;
821 case H_LOGICAL_CI_STORE:
822 ret = kvmppc_h_logical_ci_store(vcpu);
823 if (ret == H_TOO_HARD)
824 return RESUME_HOST;
825 break;
Michael Neuling96423822014-06-02 11:03:01 +1000826 case H_SET_MODE:
827 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
828 kvmppc_get_gpr(vcpu, 5),
829 kvmppc_get_gpr(vcpu, 6),
830 kvmppc_get_gpr(vcpu, 7));
831 if (ret == H_TOO_HARD)
832 return RESUME_HOST;
833 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000834 case H_XIRR:
835 case H_CPPR:
836 case H_EOI:
837 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000838 case H_IPOLL:
839 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000840 if (kvmppc_xics_enabled(vcpu)) {
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000841 if (xive_enabled()) {
842 ret = H_NOT_AVAILABLE;
843 return RESUME_GUEST;
844 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000845 ret = kvmppc_xics_hcall(vcpu, req);
846 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100847 }
848 return RESUME_HOST;
849 case H_PUT_TCE:
850 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
851 kvmppc_get_gpr(vcpu, 5),
852 kvmppc_get_gpr(vcpu, 6));
853 if (ret == H_TOO_HARD)
854 return RESUME_HOST;
855 break;
856 case H_PUT_TCE_INDIRECT:
857 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
858 kvmppc_get_gpr(vcpu, 5),
859 kvmppc_get_gpr(vcpu, 6),
860 kvmppc_get_gpr(vcpu, 7));
861 if (ret == H_TOO_HARD)
862 return RESUME_HOST;
863 break;
864 case H_STUFF_TCE:
865 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
866 kvmppc_get_gpr(vcpu, 5),
867 kvmppc_get_gpr(vcpu, 6),
868 kvmppc_get_gpr(vcpu, 7));
869 if (ret == H_TOO_HARD)
870 return RESUME_HOST;
871 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000872 default:
873 return RESUME_HOST;
874 }
875 kvmppc_set_gpr(vcpu, 3, ret);
876 vcpu->arch.hcall_needed = 0;
877 return RESUME_GUEST;
878}
879
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000880static int kvmppc_hcall_impl_hv(unsigned long cmd)
881{
882 switch (cmd) {
883 case H_CEDE:
884 case H_PROD:
885 case H_CONFER:
886 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000887 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100888 case H_LOGICAL_CI_LOAD:
889 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000890#ifdef CONFIG_KVM_XICS
891 case H_XIRR:
892 case H_CPPR:
893 case H_EOI:
894 case H_IPI:
895 case H_IPOLL:
896 case H_XIRR_X:
897#endif
898 return 1;
899 }
900
901 /* See if it's in the real-mode table */
902 return kvmppc_hcall_impl_hv_realmode(cmd);
903}
904
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530905static int kvmppc_emulate_debug_inst(struct kvm_run *run,
906 struct kvm_vcpu *vcpu)
907{
908 u32 last_inst;
909
910 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
911 EMULATE_DONE) {
912 /*
913 * Fetch failed, so return to guest and
914 * try executing it again.
915 */
916 return RESUME_GUEST;
917 }
918
919 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
920 run->exit_reason = KVM_EXIT_DEBUG;
921 run->debug.arch.address = kvmppc_get_pc(vcpu);
922 return RESUME_HOST;
923 } else {
924 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
925 return RESUME_GUEST;
926 }
927}
928
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530929static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
930 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000931{
932 int r = RESUME_HOST;
933
934 vcpu->stat.sum_exits++;
935
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100936 /*
937 * This can happen if an interrupt occurs in the last stages
938 * of guest entry or the first stages of guest exit (i.e. after
939 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
940 * and before setting it to KVM_GUEST_MODE_HOST_HV).
941 * That can happen due to a bug, or due to a machine check
942 * occurring at just the wrong time.
943 */
944 if (vcpu->arch.shregs.msr & MSR_HV) {
945 printk(KERN_EMERG "KVM trap in HV mode!\n");
946 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
947 vcpu->arch.trap, kvmppc_get_pc(vcpu),
948 vcpu->arch.shregs.msr);
949 kvmppc_dump_regs(vcpu);
950 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
951 run->hw.hardware_exit_reason = vcpu->arch.trap;
952 return RESUME_HOST;
953 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000954 run->exit_reason = KVM_EXIT_UNKNOWN;
955 run->ready_for_interrupt_injection = 1;
956 switch (vcpu->arch.trap) {
957 /* We're good on these - the host merely wanted to get our attention */
958 case BOOK3S_INTERRUPT_HV_DECREMENTER:
959 vcpu->stat.dec_exits++;
960 r = RESUME_GUEST;
961 break;
962 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100963 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerras84f71392016-11-22 14:30:14 +1100964 case BOOK3S_INTERRUPT_H_VIRT:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000965 vcpu->stat.ext_intr_exits++;
966 r = RESUME_GUEST;
967 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100968 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
969 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000970 case BOOK3S_INTERRUPT_PERFMON:
971 r = RESUME_GUEST;
972 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000973 case BOOK3S_INTERRUPT_MACHINE_CHECK:
974 /*
975 * Deliver a machine check interrupt to the guest.
976 * We have to do this, even if the host has handled the
977 * machine check, because machine checks use SRR0/1 and
978 * the interrupt might have trashed guest state in them.
979 */
980 kvmppc_book3s_queue_irqprio(vcpu,
981 BOOK3S_INTERRUPT_MACHINE_CHECK);
982 r = RESUME_GUEST;
983 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000984 case BOOK3S_INTERRUPT_PROGRAM:
985 {
986 ulong flags;
987 /*
988 * Normally program interrupts are delivered directly
989 * to the guest by the hardware, but we can get here
990 * as a result of a hypervisor emulation interrupt
991 * (e40) getting turned into a 700 by BML RTAS.
992 */
993 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
994 kvmppc_core_queue_program(vcpu, flags);
995 r = RESUME_GUEST;
996 break;
997 }
998 case BOOK3S_INTERRUPT_SYSCALL:
999 {
1000 /* hcall - punt to userspace */
1001 int i;
1002
Liu Ping Fan27025a62013-11-19 14:12:48 +08001003 /* hypercall with MSR_PR has already been handled in rmode,
1004 * and never reaches here.
1005 */
1006
Paul Mackerrasde56a942011-06-29 00:21:34 +00001007 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1008 for (i = 0; i < 9; ++i)
1009 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1010 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1011 vcpu->arch.hcall_needed = 1;
1012 r = RESUME_HOST;
1013 break;
1014 }
1015 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001016 * We get these next two if the guest accesses a page which it thinks
1017 * it has mapped but which is not actually present, either because
1018 * it is for an emulated I/O device or because the corresonding
1019 * host page has been paged out. Any other HDSI/HISI interrupts
1020 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001021 */
1022 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001023 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001024 break;
1025 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001026 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1027 vcpu->arch.fault_dsisr = 0;
1028 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001029 break;
1030 /*
1031 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301032 * If the guest debug is disabled, generate a program interrupt
1033 * to the guest. If guest debug is enabled, we need to check
1034 * whether the instruction is a software breakpoint instruction.
1035 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001036 */
1037 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001038 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1039 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1040 swab32(vcpu->arch.emul_inst) :
1041 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301042 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1043 r = kvmppc_emulate_debug_inst(run, vcpu);
1044 } else {
1045 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1046 r = RESUME_GUEST;
1047 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001048 break;
1049 /*
1050 * This occurs if the guest (kernel or userspace), does something that
1051 * is prohibited by HFSCR. We just generate a program interrupt to
1052 * the guest.
1053 */
1054 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1055 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001056 r = RESUME_GUEST;
1057 break;
Suresh Warrierf7af5202016-08-19 15:35:52 +10001058 case BOOK3S_INTERRUPT_HV_RM_HARD:
1059 r = RESUME_PASSTHROUGH;
1060 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001061 default:
1062 kvmppc_dump_regs(vcpu);
1063 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1064 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1065 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001066 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001067 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068 break;
1069 }
1070
Paul Mackerrasde56a942011-06-29 00:21:34 +00001071 return r;
1072}
1073
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301074static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1075 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001076{
1077 int i;
1078
Paul Mackerrasde56a942011-06-29 00:21:34 +00001079 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301080 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001081 for (i = 0; i < vcpu->arch.slb_max; i++) {
1082 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1083 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1084 }
1085
1086 return 0;
1087}
1088
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301089static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1090 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001091{
1092 int i, j;
1093
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001094 /* Only accept the same PVR as the host's, since we can't spoof it */
1095 if (sregs->pvr != vcpu->arch.pvr)
1096 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001097
1098 j = 0;
1099 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1100 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1101 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1102 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1103 ++j;
1104 }
1105 }
1106 vcpu->arch.slb_max = j;
1107
1108 return 0;
1109}
1110
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001111static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1112 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001113{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001114 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001115 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1116 u64 mask;
1117
Paul Mackerras8f902b02015-03-20 20:39:38 +11001118 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001119 spin_lock(&vc->lock);
1120 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001121 * If ILE (interrupt little-endian) has changed, update the
1122 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1123 */
1124 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001125 struct kvm_vcpu *vcpu;
1126 int i;
1127
Anton Blanchardd6829162014-01-08 21:25:30 +11001128 kvm_for_each_vcpu(i, vcpu, kvm) {
1129 if (vcpu->arch.vcore != vc)
1130 continue;
1131 if (new_lpcr & LPCR_ILE)
1132 vcpu->arch.intr_msr |= MSR_LE;
1133 else
1134 vcpu->arch.intr_msr &= ~MSR_LE;
1135 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001136 }
1137
1138 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001139 * Userspace can only modify DPFD (default prefetch depth),
1140 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11001141 * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.).
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001142 */
1143 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001144 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1145 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001146
1147 /* Broken 32-bit version of LPCR must not clear top bits */
1148 if (preserve_top32)
1149 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001150 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1151 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001152 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001153}
1154
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301155static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1156 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001157{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001158 int r = 0;
1159 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001160
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001161 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301162 case KVM_REG_PPC_DEBUG_INST:
1163 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1164 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001165 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001166 *val = get_reg_val(id, 0);
1167 break;
1168 case KVM_REG_PPC_DABR:
1169 *val = get_reg_val(id, vcpu->arch.dabr);
1170 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001171 case KVM_REG_PPC_DABRX:
1172 *val = get_reg_val(id, vcpu->arch.dabrx);
1173 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001174 case KVM_REG_PPC_DSCR:
1175 *val = get_reg_val(id, vcpu->arch.dscr);
1176 break;
1177 case KVM_REG_PPC_PURR:
1178 *val = get_reg_val(id, vcpu->arch.purr);
1179 break;
1180 case KVM_REG_PPC_SPURR:
1181 *val = get_reg_val(id, vcpu->arch.spurr);
1182 break;
1183 case KVM_REG_PPC_AMR:
1184 *val = get_reg_val(id, vcpu->arch.amr);
1185 break;
1186 case KVM_REG_PPC_UAMOR:
1187 *val = get_reg_val(id, vcpu->arch.uamor);
1188 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001189 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001190 i = id - KVM_REG_PPC_MMCR0;
1191 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1192 break;
1193 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1194 i = id - KVM_REG_PPC_PMC1;
1195 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001196 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001197 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1198 i = id - KVM_REG_PPC_SPMC1;
1199 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1200 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001201 case KVM_REG_PPC_SIAR:
1202 *val = get_reg_val(id, vcpu->arch.siar);
1203 break;
1204 case KVM_REG_PPC_SDAR:
1205 *val = get_reg_val(id, vcpu->arch.sdar);
1206 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001207 case KVM_REG_PPC_SIER:
1208 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001209 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001210 case KVM_REG_PPC_IAMR:
1211 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001212 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001213 case KVM_REG_PPC_PSPB:
1214 *val = get_reg_val(id, vcpu->arch.pspb);
1215 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001216 case KVM_REG_PPC_DPDES:
1217 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1218 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001219 case KVM_REG_PPC_VTB:
1220 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
1221 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001222 case KVM_REG_PPC_DAWR:
1223 *val = get_reg_val(id, vcpu->arch.dawr);
1224 break;
1225 case KVM_REG_PPC_DAWRX:
1226 *val = get_reg_val(id, vcpu->arch.dawrx);
1227 break;
1228 case KVM_REG_PPC_CIABR:
1229 *val = get_reg_val(id, vcpu->arch.ciabr);
1230 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001231 case KVM_REG_PPC_CSIGR:
1232 *val = get_reg_val(id, vcpu->arch.csigr);
1233 break;
1234 case KVM_REG_PPC_TACR:
1235 *val = get_reg_val(id, vcpu->arch.tacr);
1236 break;
1237 case KVM_REG_PPC_TCSCR:
1238 *val = get_reg_val(id, vcpu->arch.tcscr);
1239 break;
1240 case KVM_REG_PPC_PID:
1241 *val = get_reg_val(id, vcpu->arch.pid);
1242 break;
1243 case KVM_REG_PPC_ACOP:
1244 *val = get_reg_val(id, vcpu->arch.acop);
1245 break;
1246 case KVM_REG_PPC_WORT:
1247 *val = get_reg_val(id, vcpu->arch.wort);
1248 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001249 case KVM_REG_PPC_TIDR:
1250 *val = get_reg_val(id, vcpu->arch.tid);
1251 break;
1252 case KVM_REG_PPC_PSSCR:
1253 *val = get_reg_val(id, vcpu->arch.psscr);
1254 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001255 case KVM_REG_PPC_VPA_ADDR:
1256 spin_lock(&vcpu->arch.vpa_update_lock);
1257 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1258 spin_unlock(&vcpu->arch.vpa_update_lock);
1259 break;
1260 case KVM_REG_PPC_VPA_SLB:
1261 spin_lock(&vcpu->arch.vpa_update_lock);
1262 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1263 val->vpaval.length = vcpu->arch.slb_shadow.len;
1264 spin_unlock(&vcpu->arch.vpa_update_lock);
1265 break;
1266 case KVM_REG_PPC_VPA_DTL:
1267 spin_lock(&vcpu->arch.vpa_update_lock);
1268 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1269 val->vpaval.length = vcpu->arch.dtl.len;
1270 spin_unlock(&vcpu->arch.vpa_update_lock);
1271 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001272 case KVM_REG_PPC_TB_OFFSET:
1273 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1274 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001275 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001276 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001277 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1278 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001279 case KVM_REG_PPC_PPR:
1280 *val = get_reg_val(id, vcpu->arch.ppr);
1281 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001282#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1283 case KVM_REG_PPC_TFHAR:
1284 *val = get_reg_val(id, vcpu->arch.tfhar);
1285 break;
1286 case KVM_REG_PPC_TFIAR:
1287 *val = get_reg_val(id, vcpu->arch.tfiar);
1288 break;
1289 case KVM_REG_PPC_TEXASR:
1290 *val = get_reg_val(id, vcpu->arch.texasr);
1291 break;
1292 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1293 i = id - KVM_REG_PPC_TM_GPR0;
1294 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1295 break;
1296 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1297 {
1298 int j;
1299 i = id - KVM_REG_PPC_TM_VSR0;
1300 if (i < 32)
1301 for (j = 0; j < TS_FPRWIDTH; j++)
1302 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1303 else {
1304 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1305 val->vval = vcpu->arch.vr_tm.vr[i-32];
1306 else
1307 r = -ENXIO;
1308 }
1309 break;
1310 }
1311 case KVM_REG_PPC_TM_CR:
1312 *val = get_reg_val(id, vcpu->arch.cr_tm);
1313 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001314 case KVM_REG_PPC_TM_XER:
1315 *val = get_reg_val(id, vcpu->arch.xer_tm);
1316 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001317 case KVM_REG_PPC_TM_LR:
1318 *val = get_reg_val(id, vcpu->arch.lr_tm);
1319 break;
1320 case KVM_REG_PPC_TM_CTR:
1321 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1322 break;
1323 case KVM_REG_PPC_TM_FPSCR:
1324 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1325 break;
1326 case KVM_REG_PPC_TM_AMR:
1327 *val = get_reg_val(id, vcpu->arch.amr_tm);
1328 break;
1329 case KVM_REG_PPC_TM_PPR:
1330 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1331 break;
1332 case KVM_REG_PPC_TM_VRSAVE:
1333 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1334 break;
1335 case KVM_REG_PPC_TM_VSCR:
1336 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1337 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1338 else
1339 r = -ENXIO;
1340 break;
1341 case KVM_REG_PPC_TM_DSCR:
1342 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1343 break;
1344 case KVM_REG_PPC_TM_TAR:
1345 *val = get_reg_val(id, vcpu->arch.tar_tm);
1346 break;
1347#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001348 case KVM_REG_PPC_ARCH_COMPAT:
1349 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1350 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001351 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001352 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001353 break;
1354 }
1355
1356 return r;
1357}
1358
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301359static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1360 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001361{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001362 int r = 0;
1363 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001364 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001365
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001366 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001367 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001368 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001369 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001370 r = -EINVAL;
1371 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001372 case KVM_REG_PPC_DABR:
1373 vcpu->arch.dabr = set_reg_val(id, *val);
1374 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001375 case KVM_REG_PPC_DABRX:
1376 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1377 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001378 case KVM_REG_PPC_DSCR:
1379 vcpu->arch.dscr = set_reg_val(id, *val);
1380 break;
1381 case KVM_REG_PPC_PURR:
1382 vcpu->arch.purr = set_reg_val(id, *val);
1383 break;
1384 case KVM_REG_PPC_SPURR:
1385 vcpu->arch.spurr = set_reg_val(id, *val);
1386 break;
1387 case KVM_REG_PPC_AMR:
1388 vcpu->arch.amr = set_reg_val(id, *val);
1389 break;
1390 case KVM_REG_PPC_UAMOR:
1391 vcpu->arch.uamor = set_reg_val(id, *val);
1392 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001393 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001394 i = id - KVM_REG_PPC_MMCR0;
1395 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1396 break;
1397 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1398 i = id - KVM_REG_PPC_PMC1;
1399 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1400 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001401 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1402 i = id - KVM_REG_PPC_SPMC1;
1403 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1404 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001405 case KVM_REG_PPC_SIAR:
1406 vcpu->arch.siar = set_reg_val(id, *val);
1407 break;
1408 case KVM_REG_PPC_SDAR:
1409 vcpu->arch.sdar = set_reg_val(id, *val);
1410 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001411 case KVM_REG_PPC_SIER:
1412 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001413 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001414 case KVM_REG_PPC_IAMR:
1415 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001416 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001417 case KVM_REG_PPC_PSPB:
1418 vcpu->arch.pspb = set_reg_val(id, *val);
1419 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001420 case KVM_REG_PPC_DPDES:
1421 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1422 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001423 case KVM_REG_PPC_VTB:
1424 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
1425 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001426 case KVM_REG_PPC_DAWR:
1427 vcpu->arch.dawr = set_reg_val(id, *val);
1428 break;
1429 case KVM_REG_PPC_DAWRX:
1430 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1431 break;
1432 case KVM_REG_PPC_CIABR:
1433 vcpu->arch.ciabr = set_reg_val(id, *val);
1434 /* Don't allow setting breakpoints in hypervisor code */
1435 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1436 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1437 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001438 case KVM_REG_PPC_CSIGR:
1439 vcpu->arch.csigr = set_reg_val(id, *val);
1440 break;
1441 case KVM_REG_PPC_TACR:
1442 vcpu->arch.tacr = set_reg_val(id, *val);
1443 break;
1444 case KVM_REG_PPC_TCSCR:
1445 vcpu->arch.tcscr = set_reg_val(id, *val);
1446 break;
1447 case KVM_REG_PPC_PID:
1448 vcpu->arch.pid = set_reg_val(id, *val);
1449 break;
1450 case KVM_REG_PPC_ACOP:
1451 vcpu->arch.acop = set_reg_val(id, *val);
1452 break;
1453 case KVM_REG_PPC_WORT:
1454 vcpu->arch.wort = set_reg_val(id, *val);
1455 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001456 case KVM_REG_PPC_TIDR:
1457 vcpu->arch.tid = set_reg_val(id, *val);
1458 break;
1459 case KVM_REG_PPC_PSSCR:
1460 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
1461 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001462 case KVM_REG_PPC_VPA_ADDR:
1463 addr = set_reg_val(id, *val);
1464 r = -EINVAL;
1465 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1466 vcpu->arch.dtl.next_gpa))
1467 break;
1468 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1469 break;
1470 case KVM_REG_PPC_VPA_SLB:
1471 addr = val->vpaval.addr;
1472 len = val->vpaval.length;
1473 r = -EINVAL;
1474 if (addr && !vcpu->arch.vpa.next_gpa)
1475 break;
1476 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1477 break;
1478 case KVM_REG_PPC_VPA_DTL:
1479 addr = val->vpaval.addr;
1480 len = val->vpaval.length;
1481 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001482 if (addr && (len < sizeof(struct dtl_entry) ||
1483 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001484 break;
1485 len -= len % sizeof(struct dtl_entry);
1486 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1487 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001488 case KVM_REG_PPC_TB_OFFSET:
1489 /* round up to multiple of 2^24 */
1490 vcpu->arch.vcore->tb_offset =
1491 ALIGN(set_reg_val(id, *val), 1UL << 24);
1492 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001493 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001494 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1495 break;
1496 case KVM_REG_PPC_LPCR_64:
1497 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001498 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001499 case KVM_REG_PPC_PPR:
1500 vcpu->arch.ppr = set_reg_val(id, *val);
1501 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001502#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1503 case KVM_REG_PPC_TFHAR:
1504 vcpu->arch.tfhar = set_reg_val(id, *val);
1505 break;
1506 case KVM_REG_PPC_TFIAR:
1507 vcpu->arch.tfiar = set_reg_val(id, *val);
1508 break;
1509 case KVM_REG_PPC_TEXASR:
1510 vcpu->arch.texasr = set_reg_val(id, *val);
1511 break;
1512 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1513 i = id - KVM_REG_PPC_TM_GPR0;
1514 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1515 break;
1516 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1517 {
1518 int j;
1519 i = id - KVM_REG_PPC_TM_VSR0;
1520 if (i < 32)
1521 for (j = 0; j < TS_FPRWIDTH; j++)
1522 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1523 else
1524 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1525 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1526 else
1527 r = -ENXIO;
1528 break;
1529 }
1530 case KVM_REG_PPC_TM_CR:
1531 vcpu->arch.cr_tm = set_reg_val(id, *val);
1532 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001533 case KVM_REG_PPC_TM_XER:
1534 vcpu->arch.xer_tm = set_reg_val(id, *val);
1535 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001536 case KVM_REG_PPC_TM_LR:
1537 vcpu->arch.lr_tm = set_reg_val(id, *val);
1538 break;
1539 case KVM_REG_PPC_TM_CTR:
1540 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1541 break;
1542 case KVM_REG_PPC_TM_FPSCR:
1543 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1544 break;
1545 case KVM_REG_PPC_TM_AMR:
1546 vcpu->arch.amr_tm = set_reg_val(id, *val);
1547 break;
1548 case KVM_REG_PPC_TM_PPR:
1549 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1550 break;
1551 case KVM_REG_PPC_TM_VRSAVE:
1552 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1553 break;
1554 case KVM_REG_PPC_TM_VSCR:
1555 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1556 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1557 else
1558 r = - ENXIO;
1559 break;
1560 case KVM_REG_PPC_TM_DSCR:
1561 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1562 break;
1563 case KVM_REG_PPC_TM_TAR:
1564 vcpu->arch.tar_tm = set_reg_val(id, *val);
1565 break;
1566#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001567 case KVM_REG_PPC_ARCH_COMPAT:
1568 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1569 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001570 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001571 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001572 break;
1573 }
1574
1575 return r;
1576}
1577
Paul Mackerras45c940b2016-11-18 17:43:30 +11001578/*
1579 * On POWER9, threads are independent and can be in different partitions.
1580 * Therefore we consider each thread to be a subcore.
1581 * There is a restriction that all threads have to be in the same
1582 * MMU mode (radix or HPT), unfortunately, but since we only support
1583 * HPT guests on a HPT host so far, that isn't an impediment yet.
1584 */
1585static int threads_per_vcore(void)
1586{
1587 if (cpu_has_feature(CPU_FTR_ARCH_300))
1588 return 1;
1589 return threads_per_subcore;
1590}
1591
Stewart Smithde9bdd12014-07-18 14:18:42 +10001592static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1593{
1594 struct kvmppc_vcore *vcore;
1595
1596 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1597
1598 if (vcore == NULL)
1599 return NULL;
1600
Stewart Smithde9bdd12014-07-18 14:18:42 +10001601 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001602 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001603 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001604 vcore->preempt_tb = TB_NIL;
1605 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras45c940b2016-11-18 17:43:30 +11001606 vcore->first_vcpuid = core * threads_per_vcore();
Stewart Smithde9bdd12014-07-18 14:18:42 +10001607 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001608 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001609
1610 return vcore;
1611}
1612
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001613#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1614static struct debugfs_timings_element {
1615 const char *name;
1616 size_t offset;
1617} timings[] = {
1618 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1619 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1620 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1621 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1622 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1623};
1624
1625#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1626
1627struct debugfs_timings_state {
1628 struct kvm_vcpu *vcpu;
1629 unsigned int buflen;
1630 char buf[N_TIMINGS * 100];
1631};
1632
1633static int debugfs_timings_open(struct inode *inode, struct file *file)
1634{
1635 struct kvm_vcpu *vcpu = inode->i_private;
1636 struct debugfs_timings_state *p;
1637
1638 p = kzalloc(sizeof(*p), GFP_KERNEL);
1639 if (!p)
1640 return -ENOMEM;
1641
1642 kvm_get_kvm(vcpu->kvm);
1643 p->vcpu = vcpu;
1644 file->private_data = p;
1645
1646 return nonseekable_open(inode, file);
1647}
1648
1649static int debugfs_timings_release(struct inode *inode, struct file *file)
1650{
1651 struct debugfs_timings_state *p = file->private_data;
1652
1653 kvm_put_kvm(p->vcpu->kvm);
1654 kfree(p);
1655 return 0;
1656}
1657
1658static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1659 size_t len, loff_t *ppos)
1660{
1661 struct debugfs_timings_state *p = file->private_data;
1662 struct kvm_vcpu *vcpu = p->vcpu;
1663 char *s, *buf_end;
1664 struct kvmhv_tb_accumulator tb;
1665 u64 count;
1666 loff_t pos;
1667 ssize_t n;
1668 int i, loops;
1669 bool ok;
1670
1671 if (!p->buflen) {
1672 s = p->buf;
1673 buf_end = s + sizeof(p->buf);
1674 for (i = 0; i < N_TIMINGS; ++i) {
1675 struct kvmhv_tb_accumulator *acc;
1676
1677 acc = (struct kvmhv_tb_accumulator *)
1678 ((unsigned long)vcpu + timings[i].offset);
1679 ok = false;
1680 for (loops = 0; loops < 1000; ++loops) {
1681 count = acc->seqcount;
1682 if (!(count & 1)) {
1683 smp_rmb();
1684 tb = *acc;
1685 smp_rmb();
1686 if (count == acc->seqcount) {
1687 ok = true;
1688 break;
1689 }
1690 }
1691 udelay(1);
1692 }
1693 if (!ok)
1694 snprintf(s, buf_end - s, "%s: stuck\n",
1695 timings[i].name);
1696 else
1697 snprintf(s, buf_end - s,
1698 "%s: %llu %llu %llu %llu\n",
1699 timings[i].name, count / 2,
1700 tb_to_ns(tb.tb_total),
1701 tb_to_ns(tb.tb_min),
1702 tb_to_ns(tb.tb_max));
1703 s += strlen(s);
1704 }
1705 p->buflen = s - p->buf;
1706 }
1707
1708 pos = *ppos;
1709 if (pos >= p->buflen)
1710 return 0;
1711 if (len > p->buflen - pos)
1712 len = p->buflen - pos;
1713 n = copy_to_user(buf, p->buf + pos, len);
1714 if (n) {
1715 if (n == len)
1716 return -EFAULT;
1717 len -= n;
1718 }
1719 *ppos = pos + len;
1720 return len;
1721}
1722
1723static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1724 size_t len, loff_t *ppos)
1725{
1726 return -EACCES;
1727}
1728
1729static const struct file_operations debugfs_timings_ops = {
1730 .owner = THIS_MODULE,
1731 .open = debugfs_timings_open,
1732 .release = debugfs_timings_release,
1733 .read = debugfs_timings_read,
1734 .write = debugfs_timings_write,
1735 .llseek = generic_file_llseek,
1736};
1737
1738/* Create a debugfs directory for the vcpu */
1739static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1740{
1741 char buf[16];
1742 struct kvm *kvm = vcpu->kvm;
1743
1744 snprintf(buf, sizeof(buf), "vcpu%u", id);
1745 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1746 return;
1747 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1748 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1749 return;
1750 vcpu->arch.debugfs_timings =
1751 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1752 vcpu, &debugfs_timings_ops);
1753}
1754
1755#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1756static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1757{
1758}
1759#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1760
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301761static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1762 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001763{
1764 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001765 int err = -EINVAL;
1766 int core;
1767 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001768
Paul Mackerras45c940b2016-11-18 17:43:30 +11001769 core = id / threads_per_vcore();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001770 if (core >= KVM_MAX_VCORES)
1771 goto out;
1772
1773 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001774 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001775 if (!vcpu)
1776 goto out;
1777
1778 err = kvm_vcpu_init(vcpu, kvm, id);
1779 if (err)
1780 goto free_vcpu;
1781
1782 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001783#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1784 /*
1785 * The shared struct is never shared on HV,
1786 * so we can always use host endianness
1787 */
1788#ifdef __BIG_ENDIAN__
1789 vcpu->arch.shared_big_endian = true;
1790#else
1791 vcpu->arch.shared_big_endian = false;
1792#endif
1793#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001794 vcpu->arch.mmcr[0] = MMCR0_FC;
1795 vcpu->arch.ctrl = CTRL_RUNLATCH;
1796 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301797 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001798 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001799 spin_lock_init(&vcpu->arch.tbacct_lock);
1800 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001801 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001802
Paul Mackerrasde56a942011-06-29 00:21:34 +00001803 kvmppc_mmu_book3s_hv_init(vcpu);
1804
Paul Mackerras8455d792012-10-15 01:17:42 +00001805 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001806
1807 init_waitqueue_head(&vcpu->arch.cpu_run);
1808
1809 mutex_lock(&kvm->lock);
1810 vcore = kvm->arch.vcores[core];
1811 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001812 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001813 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001814 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001815 }
1816 mutex_unlock(&kvm->lock);
1817
1818 if (!vcore)
1819 goto free_vcpu;
1820
1821 spin_lock(&vcore->lock);
1822 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001823 spin_unlock(&vcore->lock);
1824 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001825 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001826 vcpu->arch.thread_cpu = -1;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11001827 vcpu->arch.prev_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001828
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001829 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1830 kvmppc_sanity_check(vcpu);
1831
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001832 debugfs_vcpu_init(vcpu, id);
1833
Paul Mackerrasde56a942011-06-29 00:21:34 +00001834 return vcpu;
1835
1836free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001837 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001838out:
1839 return ERR_PTR(err);
1840}
1841
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001842static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1843{
1844 if (vpa->pinned_addr)
1845 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1846 vpa->dirty);
1847}
1848
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301849static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001850{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001851 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001852 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1853 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1854 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001855 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001856 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001857 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001858}
1859
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301860static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1861{
1862 /* Indicate we want to get back into the guest */
1863 return 1;
1864}
1865
Paul Mackerras19ccb762011-07-23 17:42:46 +10001866static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001867{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001868 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001869
Paul Mackerras19ccb762011-07-23 17:42:46 +10001870 now = get_tb();
1871 if (now > vcpu->arch.dec_expires) {
1872 /* decrementer has already gone negative */
1873 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001874 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001875 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001876 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001877 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1878 / tb_ticks_per_sec;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001879 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001880 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001881}
1882
Paul Mackerras19ccb762011-07-23 17:42:46 +10001883static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001884{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001885 vcpu->arch.ceded = 0;
1886 if (vcpu->arch.timer_running) {
1887 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1888 vcpu->arch.timer_running = 0;
1889 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001890}
1891
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001892extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001893
1894static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1895 struct kvm_vcpu *vcpu)
1896{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001897 u64 now;
1898
Paul Mackerras371fefd2011-06-29 00:23:08 +00001899 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1900 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001901 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001902 now = mftb();
1903 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1904 vcpu->arch.stolen_logged;
1905 vcpu->arch.busy_preempt = now;
1906 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001907 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001908 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10001909 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001910}
1911
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001912static int kvmppc_grab_hwthread(int cpu)
1913{
1914 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001915 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001916
1917 tpaca = &paca[cpu];
1918
1919 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001920 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001921 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001922 tpaca->kvm_hstate.napping = 0;
1923 smp_wmb();
1924 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001925
1926 /*
1927 * If the thread is already executing in the kernel (e.g. handling
1928 * a stray interrupt), wait for it to get back to nap mode.
1929 * The smp_mb() is to ensure that our setting of hwthread_req
1930 * is visible before we look at hwthread_state, so if this
1931 * races with the code at system_reset_pSeries and the thread
1932 * misses our setting of hwthread_req, we are sure to see its
1933 * setting of hwthread_state, and vice versa.
1934 */
1935 smp_mb();
1936 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1937 if (--timeout <= 0) {
1938 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1939 return -EBUSY;
1940 }
1941 udelay(1);
1942 }
1943 return 0;
1944}
1945
1946static void kvmppc_release_hwthread(int cpu)
1947{
1948 struct paca_struct *tpaca;
1949
1950 tpaca = &paca[cpu];
1951 tpaca->kvm_hstate.hwthread_req = 0;
1952 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001953 tpaca->kvm_hstate.kvm_vcore = NULL;
1954 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001955}
1956
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11001957static void do_nothing(void *x)
1958{
1959}
1960
1961static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
1962{
1963 int i;
1964
1965 cpu = cpu_first_thread_sibling(cpu);
1966 cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
1967 /*
1968 * Make sure setting of bit in need_tlb_flush precedes
1969 * testing of cpu_in_guest bits. The matching barrier on
1970 * the other side is the first smp_mb() in kvmppc_run_core().
1971 */
1972 smp_mb();
1973 for (i = 0; i < threads_per_core; ++i)
1974 if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest))
1975 smp_call_function_single(cpu + i, do_nothing, NULL, 1);
1976}
1977
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001978static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001979{
1980 int cpu;
1981 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001982 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11001983 struct kvm *kvm = vc->kvm;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001984
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001985 cpu = vc->pcpu;
1986 if (vcpu) {
1987 if (vcpu->arch.timer_running) {
1988 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1989 vcpu->arch.timer_running = 0;
1990 }
1991 cpu += vcpu->arch.ptid;
1992 vcpu->cpu = mvc->pcpu;
1993 vcpu->arch.thread_cpu = cpu;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11001994
1995 /*
1996 * With radix, the guest can do TLB invalidations itself,
1997 * and it could choose to use the local form (tlbiel) if
1998 * it is invalidating a translation that has only ever been
1999 * used on one vcpu. However, that doesn't mean it has
2000 * only ever been used on one physical cpu, since vcpus
2001 * can move around between pcpus. To cope with this, when
2002 * a vcpu moves from one pcpu to another, we need to tell
2003 * any vcpus running on the same core as this vcpu previously
2004 * ran to flush the TLB. The TLB is shared between threads,
2005 * so we use a single bit in .need_tlb_flush for all 4 threads.
2006 */
2007 if (kvm_is_radix(kvm) && vcpu->arch.prev_cpu != cpu) {
2008 if (vcpu->arch.prev_cpu >= 0 &&
2009 cpu_first_thread_sibling(vcpu->arch.prev_cpu) !=
2010 cpu_first_thread_sibling(cpu))
2011 radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu);
2012 vcpu->arch.prev_cpu = cpu;
2013 }
2014 cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002015 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002016 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002017 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10002018 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10002019 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002020 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002021 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002022 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11002023 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002024}
2025
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002026static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002027{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002028 int cpu = smp_processor_id();
2029 int i, loops;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002030 int n_threads = threads_per_vcore();
Paul Mackerras371fefd2011-06-29 00:23:08 +00002031
Paul Mackerras45c940b2016-11-18 17:43:30 +11002032 if (n_threads <= 1)
2033 return;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002034 for (loops = 0; loops < 1000000; ++loops) {
2035 /*
2036 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002037 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002038 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002039 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002040 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002041 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002042 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002043 break;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002044 if (i == n_threads) {
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002045 HMT_medium();
2046 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002047 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002048 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00002049 }
2050 HMT_medium();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002051 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002052 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002053 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002054}
2055
2056/*
2057 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00002058 * this core are off-line. Then grab the threads so they can't
2059 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00002060 */
2061static int on_primary_thread(void)
2062{
2063 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10002064 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002065
Michael Ellerman3102f782014-05-23 18:15:29 +10002066 /* Are we on a primary subcore? */
2067 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00002068 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10002069
2070 thr = 0;
2071 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002072 if (cpu_online(cpu + thr))
2073 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00002074
2075 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10002076 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002077 if (kvmppc_grab_hwthread(cpu + thr)) {
2078 /* Couldn't grab one; let the others go */
2079 do {
2080 kvmppc_release_hwthread(cpu + thr);
2081 } while (--thr > 0);
2082 return 0;
2083 }
2084 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002085 return 1;
2086}
2087
Paul Mackerrasec257162015-06-24 21:18:03 +10002088/*
2089 * A list of virtual cores for each physical CPU.
2090 * These are vcores that could run but their runner VCPU tasks are
2091 * (or may be) preempted.
2092 */
2093struct preempted_vcore_list {
2094 struct list_head list;
2095 spinlock_t lock;
2096};
2097
2098static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
2099
2100static void init_vcore_lists(void)
2101{
2102 int cpu;
2103
2104 for_each_possible_cpu(cpu) {
2105 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
2106 spin_lock_init(&lp->lock);
2107 INIT_LIST_HEAD(&lp->list);
2108 }
2109}
2110
2111static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
2112{
2113 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2114
2115 vc->vcore_state = VCORE_PREEMPT;
2116 vc->pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002117 if (vc->num_threads < threads_per_vcore()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002118 spin_lock(&lp->lock);
2119 list_add_tail(&vc->preempt_list, &lp->list);
2120 spin_unlock(&lp->lock);
2121 }
2122
2123 /* Start accumulating stolen time */
2124 kvmppc_core_start_stolen(vc);
2125}
2126
2127static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
2128{
Paul Mackerras402813f2015-07-16 17:11:13 +10002129 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10002130
2131 kvmppc_core_end_stolen(vc);
2132 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10002133 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002134 spin_lock(&lp->lock);
2135 list_del_init(&vc->preempt_list);
2136 spin_unlock(&lp->lock);
2137 }
2138 vc->vcore_state = VCORE_INACTIVE;
2139}
2140
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002141/*
2142 * This stores information about the virtual cores currently
2143 * assigned to a physical core.
2144 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002145struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002146 int n_subcores;
2147 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002148 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002149 int subcore_threads[MAX_SUBCORES];
2150 struct kvm *subcore_vm[MAX_SUBCORES];
2151 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002152};
2153
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002154/*
2155 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
2156 * respectively in 2-way micro-threading (split-core) mode.
2157 */
2158static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2159
Paul Mackerrasec257162015-06-24 21:18:03 +10002160static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2161{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002162 int sub;
2163
Paul Mackerrasec257162015-06-24 21:18:03 +10002164 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002165 cip->n_subcores = 1;
2166 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002167 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002168 cip->subcore_threads[0] = vc->num_threads;
2169 cip->subcore_vm[0] = vc->kvm;
2170 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2171 INIT_LIST_HEAD(&cip->vcs[sub]);
2172 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2173}
2174
2175static bool subcore_config_ok(int n_subcores, int n_threads)
2176{
2177 /* Can only dynamically split if unsplit to begin with */
2178 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2179 return false;
2180 if (n_subcores > MAX_SUBCORES)
2181 return false;
2182 if (n_subcores > 1) {
2183 if (!(dynamic_mt_modes & 2))
2184 n_subcores = 4;
2185 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2186 return false;
2187 }
2188
2189 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002190}
2191
2192static void init_master_vcore(struct kvmppc_vcore *vc)
2193{
2194 vc->master_vcore = vc;
2195 vc->entry_exit_map = 0;
2196 vc->in_guest = 0;
2197 vc->napping_threads = 0;
2198 vc->conferring_threads = 0;
2199}
2200
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002201static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2202{
2203 int n_threads = vc->num_threads;
2204 int sub;
2205
2206 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2207 return false;
2208
2209 if (n_threads < cip->max_subcore_threads)
2210 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002211 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002212 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002213 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002214
2215 sub = cip->n_subcores;
2216 ++cip->n_subcores;
2217 cip->total_threads += vc->num_threads;
2218 cip->subcore_threads[sub] = vc->num_threads;
2219 cip->subcore_vm[sub] = vc->kvm;
2220 init_master_vcore(vc);
Wei Yongjun28d057c2016-10-17 15:15:50 +00002221 list_move_tail(&vc->preempt_list, &cip->vcs[sub]);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002222
2223 return true;
2224}
2225
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002226/*
2227 * Work out whether it is possible to piggyback the execution of
2228 * vcore *pvc onto the execution of the other vcores described in *cip.
2229 */
2230static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2231 int target_threads)
2232{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002233 if (cip->total_threads + pvc->num_threads > target_threads)
2234 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002235
Paul Mackerrasb0090312016-09-15 16:27:41 +10002236 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002237}
2238
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002239static void prepare_threads(struct kvmppc_vcore *vc)
2240{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002241 int i;
2242 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002243
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002244 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002245 if (signal_pending(vcpu->arch.run_task))
2246 vcpu->arch.ret = -EINTR;
2247 else if (vcpu->arch.vpa.update_pending ||
2248 vcpu->arch.slb_shadow.update_pending ||
2249 vcpu->arch.dtl.update_pending)
2250 vcpu->arch.ret = RESUME_GUEST;
2251 else
2252 continue;
2253 kvmppc_remove_runnable(vc, vcpu);
2254 wake_up(&vcpu->arch.cpu_run);
2255 }
2256}
2257
Paul Mackerrasec257162015-06-24 21:18:03 +10002258static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002259{
Paul Mackerrasec257162015-06-24 21:18:03 +10002260 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2261 struct kvmppc_vcore *pvc, *vcnext;
2262
2263 spin_lock(&lp->lock);
2264 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2265 if (!spin_trylock(&pvc->lock))
2266 continue;
2267 prepare_threads(pvc);
2268 if (!pvc->n_runnable) {
2269 list_del_init(&pvc->preempt_list);
2270 if (pvc->runner == NULL) {
2271 pvc->vcore_state = VCORE_INACTIVE;
2272 kvmppc_core_end_stolen(pvc);
2273 }
2274 spin_unlock(&pvc->lock);
2275 continue;
2276 }
2277 if (!can_piggyback(pvc, cip, target_threads)) {
2278 spin_unlock(&pvc->lock);
2279 continue;
2280 }
2281 kvmppc_core_end_stolen(pvc);
2282 pvc->vcore_state = VCORE_PIGGYBACK;
2283 if (cip->total_threads >= target_threads)
2284 break;
2285 }
2286 spin_unlock(&lp->lock);
2287}
2288
2289static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2290{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002291 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002292 u64 now;
2293 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002294 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002295
Paul Mackerrasec257162015-06-24 21:18:03 +10002296 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002297 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002298 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002299 /* cancel pending dec exception if dec is positive */
2300 if (now < vcpu->arch.dec_expires &&
2301 kvmppc_core_pending_dec(vcpu))
2302 kvmppc_core_dequeue_dec(vcpu);
2303
2304 trace_kvm_guest_exit(vcpu);
2305
2306 ret = RESUME_GUEST;
2307 if (vcpu->arch.trap)
2308 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2309 vcpu->arch.run_task);
2310
2311 vcpu->arch.ret = ret;
2312 vcpu->arch.trap = 0;
2313
Paul Mackerrasec257162015-06-24 21:18:03 +10002314 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2315 if (vcpu->arch.pending_exceptions)
2316 kvmppc_core_prepare_to_enter(vcpu);
2317 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002318 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002319 else
2320 ++still_running;
2321 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002322 kvmppc_remove_runnable(vc, vcpu);
2323 wake_up(&vcpu->arch.cpu_run);
2324 }
2325 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002326 list_del_init(&vc->preempt_list);
2327 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002328 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002329 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002330 } else if (vc->runner) {
2331 vc->vcore_state = VCORE_PREEMPT;
2332 kvmppc_core_start_stolen(vc);
2333 } else {
2334 vc->vcore_state = VCORE_INACTIVE;
2335 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002336 if (vc->n_runnable > 0 && vc->runner == NULL) {
2337 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002338 i = -1;
2339 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10002340 wake_up(&vcpu->arch.cpu_run);
2341 }
2342 }
2343 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002344}
2345
Paul Mackerras371fefd2011-06-29 00:23:08 +00002346/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002347 * Clear core from the list of active host cores as we are about to
2348 * enter the guest. Only do this if it is the primary thread of the
2349 * core (not if a subcore) that is entering the guest.
2350 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002351static inline int kvmppc_clear_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002352{
2353 int core;
2354
2355 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002356 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002357 /*
2358 * Memory barrier can be omitted here as we will do a smp_wmb()
2359 * later in kvmppc_start_thread and we need ensure that state is
2360 * visible to other CPUs only after we enter guest.
2361 */
2362 core = cpu >> threads_shift;
2363 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002364 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002365}
2366
2367/*
2368 * Advertise this core as an active host core since we exited the guest
2369 * Only need to do this if it is the primary thread of the core that is
2370 * exiting.
2371 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002372static inline int kvmppc_set_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002373{
2374 int core;
2375
2376 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002377 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002378
2379 /*
2380 * Memory barrier can be omitted here because we do a spin_unlock
2381 * immediately after this which provides the memory barrier.
2382 */
2383 core = cpu >> threads_shift;
2384 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002385 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002386}
2387
2388/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002389 * Run a set of guest threads on a physical core.
2390 * Called with vc->lock held.
2391 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002392static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002393{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002394 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002395 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002396 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002397 struct core_info core_info;
2398 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002399 struct kvm_split_mode split_info, *sip;
2400 int split, subcore_size, active;
2401 int sub;
2402 bool thr0_done;
2403 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002404 int pcpu, thr;
2405 int target_threads;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002406 int controlled_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002407
2408 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002409 * Remove from the list any threads that have a signal pending
2410 * or need a VPA update done
2411 */
2412 prepare_threads(vc);
2413
2414 /* if the runner is no longer runnable, let the caller pick a new one */
2415 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2416 return;
2417
2418 /*
2419 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002420 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002421 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002422 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002423
2424 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11002425 * Number of threads that we will be controlling: the same as
2426 * the number of threads per subcore, except on POWER9,
2427 * where it's 1 because the threads are (mostly) independent.
2428 */
2429 controlled_threads = threads_per_vcore();
2430
2431 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002432 * Make sure we are running on primary threads, and that secondary
2433 * threads are offline. Also check if the number of threads in this
2434 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002435 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002436 if ((controlled_threads > 1) &&
Michael Ellerman3102f782014-05-23 18:15:29 +10002437 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002438 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002439 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002440 kvmppc_remove_runnable(vc, vcpu);
2441 wake_up(&vcpu->arch.cpu_run);
2442 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002443 goto out;
2444 }
2445
Paul Mackerrasec257162015-06-24 21:18:03 +10002446 /*
2447 * See if we could run any other vcores on the physical core
2448 * along with this one.
2449 */
2450 init_core_info(&core_info, vc);
2451 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002452 target_threads = controlled_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002453 if (target_smt_mode && target_smt_mode < target_threads)
2454 target_threads = target_smt_mode;
2455 if (vc->num_threads < target_threads)
2456 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002457
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002458 /* Decide on micro-threading (split-core) mode */
2459 subcore_size = threads_per_subcore;
2460 cmd_bit = stat_bit = 0;
2461 split = core_info.n_subcores;
2462 sip = NULL;
2463 if (split > 1) {
2464 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2465 if (split == 2 && (dynamic_mt_modes & 2)) {
2466 cmd_bit = HID0_POWER8_1TO2LPAR;
2467 stat_bit = HID0_POWER8_2LPARMODE;
2468 } else {
2469 split = 4;
2470 cmd_bit = HID0_POWER8_1TO4LPAR;
2471 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002472 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002473 subcore_size = MAX_SMT_THREADS / split;
2474 sip = &split_info;
2475 memset(&split_info, 0, sizeof(split_info));
2476 split_info.rpr = mfspr(SPRN_RPR);
2477 split_info.pmmar = mfspr(SPRN_PMMAR);
2478 split_info.ldbar = mfspr(SPRN_LDBAR);
2479 split_info.subcore_size = subcore_size;
2480 for (sub = 0; sub < core_info.n_subcores; ++sub)
2481 split_info.master_vcs[sub] =
2482 list_first_entry(&core_info.vcs[sub],
2483 struct kvmppc_vcore, preempt_list);
2484 /* order writes to split_info before kvm_split_mode pointer */
2485 smp_wmb();
2486 }
2487 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002488 for (thr = 0; thr < controlled_threads; ++thr)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002489 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2490
2491 /* Initiate micro-threading (split-core) if required */
2492 if (cmd_bit) {
2493 unsigned long hid0 = mfspr(SPRN_HID0);
2494
2495 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2496 mb();
2497 mtspr(SPRN_HID0, hid0);
2498 isync();
2499 for (;;) {
2500 hid0 = mfspr(SPRN_HID0);
2501 if (hid0 & stat_bit)
2502 break;
2503 cpu_relax();
2504 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002505 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002506
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002507 kvmppc_clear_host_core(pcpu);
2508
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002509 /* Start all the threads */
2510 active = 0;
2511 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2512 thr = subcore_thread_map[sub];
2513 thr0_done = false;
2514 active |= 1 << thr;
2515 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2516 pvc->pcpu = pcpu + thr;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002517 for_each_runnable_thread(i, vcpu, pvc) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002518 kvmppc_start_thread(vcpu, pvc);
2519 kvmppc_create_dtl_entry(vcpu, pvc);
2520 trace_kvm_guest_enter(vcpu);
2521 if (!vcpu->arch.ptid)
2522 thr0_done = true;
2523 active |= 1 << (thr + vcpu->arch.ptid);
2524 }
2525 /*
2526 * We need to start the first thread of each subcore
2527 * even if it doesn't have a vcpu.
2528 */
2529 if (pvc->master_vcore == pvc && !thr0_done)
2530 kvmppc_start_thread(NULL, pvc);
2531 thr += pvc->num_threads;
2532 }
2533 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302534
2535 /*
2536 * Ensure that split_info.do_nap is set after setting
2537 * the vcore pointer in the PACA of the secondaries.
2538 */
2539 smp_mb();
2540 if (cmd_bit)
2541 split_info.do_nap = 1; /* ask secondaries to nap when done */
2542
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002543 /*
2544 * When doing micro-threading, poke the inactive threads as well.
2545 * This gets them to the nap instruction after kvm_do_nap,
2546 * which reduces the time taken to unsplit later.
2547 */
2548 if (split > 1)
2549 for (thr = 1; thr < threads_per_subcore; ++thr)
2550 if (!(active & (1 << thr)))
2551 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002552
Paul Mackerras2f12f032012-10-15 01:17:17 +00002553 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002554 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002555
2556 trace_kvmppc_run_core(vc, 0);
2557
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002558 for (sub = 0; sub < core_info.n_subcores; ++sub)
2559 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2560 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002561
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002562 guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002563
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002564 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002565
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002566 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002567
Paul Mackerrasec257162015-06-24 21:18:03 +10002568 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2569
2570 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002571 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002572 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002573
Paul Mackerras371fefd2011-06-29 00:23:08 +00002574 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002575 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002576
2577 /* Return to whole-core mode if we split the core earlier */
2578 if (split > 1) {
2579 unsigned long hid0 = mfspr(SPRN_HID0);
2580 unsigned long loops = 0;
2581
2582 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2583 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2584 mb();
2585 mtspr(SPRN_HID0, hid0);
2586 isync();
2587 for (;;) {
2588 hid0 = mfspr(SPRN_HID0);
2589 if (!(hid0 & stat_bit))
2590 break;
2591 cpu_relax();
2592 ++loops;
2593 }
2594 split_info.do_nap = 0;
2595 }
2596
2597 /* Let secondaries go back to the offline loop */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002598 for (i = 0; i < controlled_threads; ++i) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002599 kvmppc_release_hwthread(pcpu + i);
2600 if (sip && sip->napped[i])
2601 kvmppc_ipi_thread(pcpu + i);
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002602 cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002603 }
2604
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002605 kvmppc_set_host_core(pcpu);
2606
Paul Mackerras371fefd2011-06-29 00:23:08 +00002607 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002608
Paul Mackerras371fefd2011-06-29 00:23:08 +00002609 /* make sure updates to secondary vcpu structs are visible now */
2610 smp_mb();
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002611 guest_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002612
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002613 for (sub = 0; sub < core_info.n_subcores; ++sub)
2614 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2615 preempt_list)
2616 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002617
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002618 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002619 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002620
Paul Mackerrasde56a942011-06-29 00:21:34 +00002621 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002622 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002623 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002624}
2625
Paul Mackerras19ccb762011-07-23 17:42:46 +10002626/*
2627 * Wait for some other vcpu thread to execute us, and
2628 * wake us up when we need to handle something in the host.
2629 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002630static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2631 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002632{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002633 DEFINE_WAIT(wait);
2634
Paul Mackerras19ccb762011-07-23 17:42:46 +10002635 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002636 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2637 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002638 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002639 spin_lock(&vc->lock);
2640 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002641 finish_wait(&vcpu->arch.cpu_run, &wait);
2642}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002643
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002644static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
2645{
2646 /* 10us base */
2647 if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
2648 vc->halt_poll_ns = 10000;
2649 else
2650 vc->halt_poll_ns *= halt_poll_ns_grow;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002651}
2652
2653static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
2654{
2655 if (halt_poll_ns_shrink == 0)
2656 vc->halt_poll_ns = 0;
2657 else
2658 vc->halt_poll_ns /= halt_poll_ns_shrink;
2659}
2660
Suraj Jitindar Singh908a0932016-10-14 11:53:23 +11002661/*
2662 * Check to see if any of the runnable vcpus on the vcore have pending
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002663 * exceptions or are no longer ceded
2664 */
2665static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
2666{
2667 struct kvm_vcpu *vcpu;
2668 int i;
2669
2670 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras8464c882016-12-06 20:42:05 +11002671 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded ||
2672 vcpu->arch.prodded)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002673 return 1;
2674 }
2675
2676 return 0;
2677}
2678
Paul Mackerras19ccb762011-07-23 17:42:46 +10002679/*
2680 * All the vcpus in this vcore are idle, so wait for a decrementer
2681 * or external interrupt to one of the vcpus. vc->lock is held.
2682 */
2683static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2684{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002685 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002686 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002687 u64 block_ns;
Marcelo Tosatti85773702016-02-19 09:46:39 +01002688 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002689
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002690 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002691 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002692 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002693 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
2694 ++vc->runner->stat.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002695
2696 vc->vcore_state = VCORE_POLLING;
2697 spin_unlock(&vc->lock);
2698
2699 do {
2700 if (kvmppc_vcore_check_block(vc)) {
2701 do_sleep = 0;
2702 break;
2703 }
2704 cur = ktime_get();
2705 } while (single_task_running() && ktime_before(cur, stop));
2706
2707 spin_lock(&vc->lock);
2708 vc->vcore_state = VCORE_INACTIVE;
2709
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002710 if (!do_sleep) {
2711 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002712 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002713 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002714 }
2715
Marcelo Tosatti85773702016-02-19 09:46:39 +01002716 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002717
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002718 if (kvmppc_vcore_check_block(vc)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002719 finish_swait(&vc->wq, &wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002720 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002721 /* If we polled, count this as a successful poll */
2722 if (vc->halt_poll_ns)
2723 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002724 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002725 }
2726
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002727 start_wait = ktime_get();
2728
Paul Mackerras19ccb762011-07-23 17:42:46 +10002729 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002730 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002731 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002732 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01002733 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002734 spin_lock(&vc->lock);
2735 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002736 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002737 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002738
2739 cur = ktime_get();
2740
2741out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002742 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
2743
2744 /* Attribute wait time */
2745 if (do_sleep) {
2746 vc->runner->stat.halt_wait_ns +=
2747 ktime_to_ns(cur) - ktime_to_ns(start_wait);
2748 /* Attribute failed poll time */
2749 if (vc->halt_poll_ns)
2750 vc->runner->stat.halt_poll_fail_ns +=
2751 ktime_to_ns(start_wait) -
2752 ktime_to_ns(start_poll);
2753 } else {
2754 /* Attribute successful poll time */
2755 if (vc->halt_poll_ns)
2756 vc->runner->stat.halt_poll_success_ns +=
2757 ktime_to_ns(cur) -
2758 ktime_to_ns(start_poll);
2759 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002760
2761 /* Adjust poll time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002762 if (halt_poll_ns) {
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002763 if (block_ns <= vc->halt_poll_ns)
2764 ;
2765 /* We slept and blocked for longer than the max halt time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002766 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002767 shrink_halt_poll_ns(vc);
2768 /* We slept and our poll time is too small */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002769 else if (vc->halt_poll_ns < halt_poll_ns &&
2770 block_ns < halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002771 grow_halt_poll_ns(vc);
Suraj Jitindar Singhe03f3922016-10-14 11:53:21 +11002772 if (vc->halt_poll_ns > halt_poll_ns)
2773 vc->halt_poll_ns = halt_poll_ns;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002774 } else
2775 vc->halt_poll_ns = 0;
2776
2777 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002778}
2779
2780static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2781{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002782 int n_ceded, i;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002783 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002784 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002785
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002786 trace_kvmppc_run_vcpu_enter(vcpu);
2787
Paul Mackerras371fefd2011-06-29 00:23:08 +00002788 kvm_run->exit_reason = 0;
2789 vcpu->arch.ret = RESUME_GUEST;
2790 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002791 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002792
Paul Mackerras371fefd2011-06-29 00:23:08 +00002793 /*
2794 * Synchronize with other threads in this virtual core
2795 */
2796 vc = vcpu->arch.vcore;
2797 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002798 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002799 vcpu->arch.run_task = current;
2800 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002801 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002802 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002803 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002804 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002805 ++vc->n_runnable;
2806
Paul Mackerras19ccb762011-07-23 17:42:46 +10002807 /*
2808 * This happens the first time this is called for a vcpu.
2809 * If the vcore is already running, we may be able to start
2810 * this thread straight away and have it join in.
2811 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002812 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002813 if (vc->vcore_state == VCORE_PIGGYBACK) {
2814 struct kvmppc_vcore *mvc = vc->master_vcore;
2815 if (spin_trylock(&mvc->lock)) {
2816 if (mvc->vcore_state == VCORE_RUNNING &&
2817 !VCORE_IS_EXITING(mvc)) {
2818 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002819 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002820 trace_kvm_guest_enter(vcpu);
2821 }
2822 spin_unlock(&mvc->lock);
2823 }
2824 } else if (vc->vcore_state == VCORE_RUNNING &&
2825 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002826 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002827 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002828 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002829 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002830 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002831 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002832
Paul Mackerras8455d792012-10-15 01:17:42 +00002833 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002834
2835 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2836 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002837 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2838 kvmppc_vcore_end_preempt(vc);
2839
Paul Mackerras8455d792012-10-15 01:17:42 +00002840 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002841 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002842 continue;
2843 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002844 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002845 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002846 if (signal_pending(v->arch.run_task)) {
2847 kvmppc_remove_runnable(vc, v);
2848 v->stat.signal_exits++;
2849 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2850 v->arch.ret = -EINTR;
2851 wake_up(&v->arch.cpu_run);
2852 }
2853 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002854 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2855 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002856 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002857 for_each_runnable_thread(i, v, vc) {
Paul Mackerras8464c882016-12-06 20:42:05 +11002858 if (!v->arch.pending_exceptions && !v->arch.prodded)
Paul Mackerras8455d792012-10-15 01:17:42 +00002859 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002860 else
2861 v->arch.ceded = 0;
2862 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002863 vc->runner = vcpu;
2864 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002865 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002866 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002867 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002868 /* Let something else run */
2869 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002870 if (vc->vcore_state == VCORE_PREEMPT)
2871 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002872 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002873 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002874 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002875 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002876 }
2877
Paul Mackerras8455d792012-10-15 01:17:42 +00002878 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2879 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002880 vc->vcore_state == VCORE_EXITING ||
2881 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002882 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002883
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002884 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2885 kvmppc_vcore_end_preempt(vc);
2886
Paul Mackerras8455d792012-10-15 01:17:42 +00002887 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2888 kvmppc_remove_runnable(vc, vcpu);
2889 vcpu->stat.signal_exits++;
2890 kvm_run->exit_reason = KVM_EXIT_INTR;
2891 vcpu->arch.ret = -EINTR;
2892 }
2893
2894 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2895 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002896 i = -1;
2897 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00002898 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002899 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002900
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002901 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002902 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002903 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002904}
2905
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302906static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002907{
2908 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002909 int srcu_idx;
Paul Mackerrasca8efa12017-06-06 16:47:22 +10002910 unsigned long ebb_regs[3] = {}; /* shut up GCC */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002911
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002912 if (!vcpu->arch.sane) {
2913 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2914 return -EINVAL;
2915 }
2916
Scott Wood25051b5a2011-11-08 18:23:23 -06002917 kvmppc_core_prepare_to_enter(vcpu);
2918
Paul Mackerras19ccb762011-07-23 17:42:46 +10002919 /* No need to go into the guest when all we'll do is come back out */
2920 if (signal_pending(current)) {
2921 run->exit_reason = KVM_EXIT_INTR;
2922 return -EINTR;
2923 }
2924
Paul Mackerras32fad282012-05-04 02:32:53 +00002925 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002926 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002927 smp_mb();
2928
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002929 /* On the first time here, set up HTAB and VRMA */
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11002930 if (!kvm_is_radix(vcpu->kvm) && !vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002931 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002932 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002933 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002934 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002935
Anton Blanchard579e6332015-10-29 11:44:09 +11002936 flush_all_to_thread(current);
2937
Paul Mackerrasca8efa12017-06-06 16:47:22 +10002938 /* Save userspace EBB register values */
2939 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
2940 ebb_regs[0] = mfspr(SPRN_EBBHR);
2941 ebb_regs[1] = mfspr(SPRN_EBBRR);
2942 ebb_regs[2] = mfspr(SPRN_BESCR);
2943 }
2944
Paul Mackerras19ccb762011-07-23 17:42:46 +10002945 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002946 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002947 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002948
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002949 do {
2950 r = kvmppc_run_vcpu(run, vcpu);
2951
2952 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2953 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002954 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002955 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002956 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002957 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002958 } else if (r == RESUME_PAGE_FAULT) {
2959 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2960 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2961 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2962 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002963 } else if (r == RESUME_PASSTHROUGH) {
2964 if (WARN_ON(xive_enabled()))
2965 r = H_SUCCESS;
2966 else
2967 r = kvmppc_xics_rm_complete(vcpu, 0);
2968 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002969 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002970
Paul Mackerrasca8efa12017-06-06 16:47:22 +10002971 /* Restore userspace EBB register values */
2972 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
2973 mtspr(SPRN_EBBHR, ebb_regs[0]);
2974 mtspr(SPRN_EBBRR, ebb_regs[1]);
2975 mtspr(SPRN_BESCR, ebb_regs[2]);
2976 }
2977
Paul Mackerras32fad282012-05-04 02:32:53 +00002978 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002979 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002980 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002981 return r;
2982}
2983
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002984static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2985 int linux_psize)
2986{
2987 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2988
2989 if (!def->shift)
2990 return;
2991 (*sps)->page_shift = def->shift;
2992 (*sps)->slb_enc = def->sllp;
2993 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002994 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302995 /*
2996 * Add 16MB MPSS support if host supports it
2997 */
2998 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2999 (*sps)->enc[1].page_shift = 24;
3000 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
3001 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003002 (*sps)++;
3003}
3004
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303005static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
3006 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003007{
3008 struct kvm_ppc_one_seg_page_size *sps;
3009
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003010 /*
3011 * Since we don't yet support HPT guests on a radix host,
3012 * return an error if the host uses radix.
3013 */
3014 if (radix_enabled())
3015 return -EINVAL;
3016
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003017 info->flags = KVM_PPC_PAGE_SIZES_REAL;
3018 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
3019 info->flags |= KVM_PPC_1T_SEGMENTS;
3020 info->slb_size = mmu_slb_size;
3021
3022 /* We only support these sizes for now, and no muti-size segments */
3023 sps = &info->sps[0];
3024 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
3025 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
3026 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
3027
3028 return 0;
3029}
3030
Paul Mackerras82ed3612011-12-15 02:03:22 +00003031/*
3032 * Get (and clear) the dirty memory log for a memory slot.
3033 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303034static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
3035 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003036{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003037 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003038 struct kvm_memory_slot *memslot;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003039 int i, r;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003040 unsigned long n;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003041 unsigned long *buf;
3042 struct kvm_vcpu *vcpu;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003043
3044 mutex_lock(&kvm->slots_lock);
3045
3046 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07003047 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003048 goto out;
3049
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003050 slots = kvm_memslots(kvm);
3051 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003052 r = -ENOENT;
3053 if (!memslot->dirty_bitmap)
3054 goto out;
3055
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003056 /*
3057 * Use second half of bitmap area because radix accumulates
3058 * bits in the first half.
3059 */
Paul Mackerras82ed3612011-12-15 02:03:22 +00003060 n = kvm_dirty_bitmap_bytes(memslot);
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003061 buf = memslot->dirty_bitmap + n / sizeof(long);
3062 memset(buf, 0, n);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003063
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003064 if (kvm_is_radix(kvm))
3065 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
3066 else
3067 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003068 if (r)
3069 goto out;
3070
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003071 /* Harvest dirty bits from VPA and DTL updates */
3072 /* Note: we never modify the SLB shadow buffer areas */
3073 kvm_for_each_vcpu(i, vcpu, kvm) {
3074 spin_lock(&vcpu->arch.vpa_update_lock);
3075 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
3076 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
3077 spin_unlock(&vcpu->arch.vpa_update_lock);
3078 }
3079
Paul Mackerras82ed3612011-12-15 02:03:22 +00003080 r = -EFAULT;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003081 if (copy_to_user(log->dirty_bitmap, buf, n))
Paul Mackerras82ed3612011-12-15 02:03:22 +00003082 goto out;
3083
3084 r = 0;
3085out:
3086 mutex_unlock(&kvm->slots_lock);
3087 return r;
3088}
3089
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303090static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
3091 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003092{
3093 if (!dont || free->arch.rmap != dont->arch.rmap) {
3094 vfree(free->arch.rmap);
3095 free->arch.rmap = NULL;
3096 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003097}
3098
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303099static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
3100 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003101{
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003102 /*
3103 * For now, if radix_enabled() then we only support radix guests,
3104 * and in that case we don't need the rmap array.
3105 */
3106 if (radix_enabled()) {
3107 slot->arch.rmap = NULL;
3108 return 0;
3109 }
3110
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003111 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
3112 if (!slot->arch.rmap)
3113 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003114
3115 return 0;
3116}
3117
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303118static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
3119 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003120 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003121{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003122 return 0;
3123}
3124
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303125static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003126 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02003127 const struct kvm_memory_slot *old,
3128 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003129{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003130 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003131 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003132 struct kvm_memory_slot *memslot;
3133
Yongji Xiea56ee9f2016-11-04 13:55:12 +08003134 /*
3135 * If we are making a new memslot, it might make
3136 * some address that was previously cached as emulated
3137 * MMIO be no longer emulated MMIO, so invalidate
3138 * all the caches of emulated MMIO translations.
3139 */
3140 if (npages)
3141 atomic64_inc(&kvm->arch.mmio_update);
3142
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003143 if (npages && old->npages && !kvm_is_radix(kvm)) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003144 /*
3145 * If modifying a memslot, reset all the rmap dirty bits.
3146 * If this is a new memslot, we don't need to do anything
3147 * since the rmap array starts out as all zeroes,
3148 * i.e. no pages are dirty.
3149 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003150 slots = kvm_memslots(kvm);
3151 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003152 kvmppc_hv_get_dirty_log_hpt(kvm, memslot, NULL);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003153 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003154}
3155
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003156/*
3157 * Update LPCR values in kvm->arch and in vcores.
3158 * Caller must hold kvm->lock.
3159 */
3160void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
3161{
3162 long int i;
3163 u32 cores_done = 0;
3164
3165 if ((kvm->arch.lpcr & mask) == lpcr)
3166 return;
3167
3168 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
3169
3170 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3171 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3172 if (!vc)
3173 continue;
3174 spin_lock(&vc->lock);
3175 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
3176 spin_unlock(&vc->lock);
3177 if (++cores_done >= kvm->arch.online_vcores)
3178 break;
3179 }
3180}
3181
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303182static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
3183{
3184 return;
3185}
3186
Paul Mackerras7a840842016-11-16 22:25:20 +11003187static void kvmppc_setup_partition_table(struct kvm *kvm)
3188{
3189 unsigned long dw0, dw1;
3190
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003191 if (!kvm_is_radix(kvm)) {
3192 /* PS field - page size for VRMA */
3193 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
3194 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
3195 /* HTABSIZE and HTABORG fields */
3196 dw0 |= kvm->arch.sdr1;
Paul Mackerras7a840842016-11-16 22:25:20 +11003197
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003198 /* Second dword as set by userspace */
3199 dw1 = kvm->arch.process_table;
3200 } else {
3201 dw0 = PATB_HR | radix__get_tree_size() |
3202 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
3203 dw1 = PATB_GR | kvm->arch.process_table;
3204 }
Paul Mackerras7a840842016-11-16 22:25:20 +11003205
3206 mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
3207}
3208
Paul Mackerras32fad282012-05-04 02:32:53 +00003209static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003210{
3211 int err = 0;
3212 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003213 unsigned long hva;
3214 struct kvm_memory_slot *memslot;
3215 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003216 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003217 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003218 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003219
3220 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003221 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003222 goto out; /* another vcpu beat us to it */
3223
Paul Mackerras32fad282012-05-04 02:32:53 +00003224 /* Allocate hashed page table (if not done already) and reset it */
David Gibson3f9d4f52016-12-20 16:49:00 +11003225 if (!kvm->arch.hpt.virt) {
David Gibsonaae07772016-12-20 16:49:02 +11003226 int order = KVM_DEFAULT_HPT_ORDER;
3227 struct kvm_hpt_info info;
3228
3229 err = kvmppc_allocate_hpt(&info, order);
3230 /* If we get here, it means userspace didn't specify a
3231 * size explicitly. So, try successively smaller
3232 * sizes if the default failed. */
3233 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
3234 err = kvmppc_allocate_hpt(&info, order);
3235
3236 if (err < 0) {
Paul Mackerras32fad282012-05-04 02:32:53 +00003237 pr_err("KVM: Couldn't alloc HPT\n");
3238 goto out;
3239 }
David Gibsonaae07772016-12-20 16:49:02 +11003240
3241 kvmppc_set_hpt(kvm, &info);
Paul Mackerras32fad282012-05-04 02:32:53 +00003242 }
3243
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003244 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003245 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003246 memslot = gfn_to_memslot(kvm, 0);
3247
3248 /* We must have some memory at 0 by now */
3249 err = -EINVAL;
3250 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003251 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003252
3253 /* Look up the VMA for the start of this memory slot */
3254 hva = memslot->userspace_addr;
3255 down_read(&current->mm->mmap_sem);
3256 vma = find_vma(current->mm, hva);
3257 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3258 goto up_out;
3259
3260 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003261 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003262
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003263 up_read(&current->mm->mmap_sem);
3264
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003265 /* We can handle 4k, 64k or 16M pages in the VRMA */
3266 err = -EINVAL;
3267 if (!(psize == 0x1000 || psize == 0x10000 ||
3268 psize == 0x1000000))
3269 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003270
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003271 senc = slb_pgsize_encoding(psize);
3272 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3273 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003274 /* Create HPTEs in the hash page table for the VRMA */
3275 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003276
Paul Mackerras7a840842016-11-16 22:25:20 +11003277 /* Update VRMASD field in the LPCR */
3278 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
3279 /* the -4 is to account for senc values starting at 0x10 */
3280 lpcr = senc << (LPCR_VRMASD_SH - 4);
3281 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
3282 } else {
3283 kvmppc_setup_partition_table(kvm);
3284 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003285
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003286 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003287 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003288 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003289 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003290 out_srcu:
3291 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003292 out:
3293 mutex_unlock(&kvm->lock);
3294 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003295
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003296 up_out:
3297 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003298 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003299}
3300
Suresh Warrier79b6c242015-12-17 14:59:06 -06003301#ifdef CONFIG_KVM_XICS
3302/*
3303 * Allocate a per-core structure for managing state about which cores are
3304 * running in the host versus the guest and for exchanging data between
3305 * real mode KVM and CPU running in the host.
3306 * This is only done for the first VM.
3307 * The allocated structure stays even if all VMs have stopped.
3308 * It is only freed when the kvm-hv module is unloaded.
3309 * It's OK for this routine to fail, we just don't support host
3310 * core operations like redirecting H_IPI wakeups.
3311 */
3312void kvmppc_alloc_host_rm_ops(void)
3313{
3314 struct kvmppc_host_rm_ops *ops;
3315 unsigned long l_ops;
3316 int cpu, core;
3317 int size;
3318
3319 /* Not the first time here ? */
3320 if (kvmppc_host_rm_ops_hv != NULL)
3321 return;
3322
3323 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3324 if (!ops)
3325 return;
3326
3327 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3328 ops->rm_core = kzalloc(size, GFP_KERNEL);
3329
3330 if (!ops->rm_core) {
3331 kfree(ops);
3332 return;
3333 }
3334
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003335 get_online_cpus();
3336
Suresh Warrier79b6c242015-12-17 14:59:06 -06003337 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3338 if (!cpu_online(cpu))
3339 continue;
3340
3341 core = cpu >> threads_shift;
3342 ops->rm_core[core].rm_state.in_host = 1;
3343 }
3344
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003345 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3346
Suresh Warrier79b6c242015-12-17 14:59:06 -06003347 /*
3348 * Make the contents of the kvmppc_host_rm_ops structure visible
3349 * to other CPUs before we assign it to the global variable.
3350 * Do an atomic assignment (no locks used here), but if someone
3351 * beats us to it, just free our copy and return.
3352 */
3353 smp_wmb();
3354 l_ops = (unsigned long) ops;
3355
3356 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003357 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003358 kfree(ops->rm_core);
3359 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003360 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003361 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003362
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003363 cpuhp_setup_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE,
3364 "ppc/kvm_book3s:prepare",
3365 kvmppc_set_host_core,
3366 kvmppc_clear_host_core);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003367 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003368}
3369
3370void kvmppc_free_host_rm_ops(void)
3371{
3372 if (kvmppc_host_rm_ops_hv) {
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003373 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003374 kfree(kvmppc_host_rm_ops_hv->rm_core);
3375 kfree(kvmppc_host_rm_ops_hv);
3376 kvmppc_host_rm_ops_hv = NULL;
3377 }
3378}
3379#endif
3380
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303381static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003382{
Paul Mackerras32fad282012-05-04 02:32:53 +00003383 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003384 char buf[32];
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003385 int ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003386
Paul Mackerras32fad282012-05-04 02:32:53 +00003387 /* Allocate the guest's logical partition ID */
3388
3389 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003390 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003391 return -ENOMEM;
3392 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003393
Suresh Warrier79b6c242015-12-17 14:59:06 -06003394 kvmppc_alloc_host_rm_ops();
3395
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003396 /*
3397 * Since we don't flush the TLB when tearing down a VM,
3398 * and this lpid might have previously been used,
3399 * make sure we flush on each core before running the new VM.
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003400 * On POWER9, the tlbie in mmu_partition_table_set_entry()
3401 * does this flush for us.
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003402 */
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003403 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3404 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003405
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003406 /* Start out with the default set of hcalls enabled */
3407 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3408 sizeof(kvm->arch.enabled_hcalls));
3409
Paul Mackerras7a840842016-11-16 22:25:20 +11003410 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3411 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003412
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003413 /* Init LPCR for virtual RMA mode */
3414 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3415 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3416 lpcr &= LPCR_PECE | LPCR_LPES;
3417 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3418 LPCR_VPM0 | LPCR_VPM1;
3419 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3420 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3421 /* On POWER8 turn on online bit to enable PURR/SPURR */
3422 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3423 lpcr |= LPCR_ONL;
Paul Mackerras84f71392016-11-22 14:30:14 +11003424 /*
3425 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
3426 * Set HVICE bit to enable hypervisor virtualization interrupts.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003427 * Set HEIC to prevent OS interrupts to go to hypervisor (should
3428 * be unnecessary but better safe than sorry in case we re-enable
3429 * EE in HV mode with this LPCR still set)
Paul Mackerras84f71392016-11-22 14:30:14 +11003430 */
3431 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerras7a840842016-11-16 22:25:20 +11003432 lpcr &= ~LPCR_VPM0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003433 lpcr |= LPCR_HVICE | LPCR_HEIC;
3434
3435 /*
3436 * If xive is enabled, we route 0x500 interrupts directly
3437 * to the guest.
3438 */
3439 if (xive_enabled())
3440 lpcr |= LPCR_LPES;
Paul Mackerras84f71392016-11-22 14:30:14 +11003441 }
3442
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003443 /*
3444 * For now, if the host uses radix, the guest must be radix.
3445 */
3446 if (radix_enabled()) {
3447 kvm->arch.radix = 1;
3448 lpcr &= ~LPCR_VPM1;
3449 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
3450 ret = kvmppc_init_vm_radix(kvm);
3451 if (ret) {
3452 kvmppc_free_lpid(kvm->arch.lpid);
3453 return ret;
3454 }
3455 kvmppc_setup_partition_table(kvm);
3456 }
3457
Paul Mackerras9e368f22011-06-29 00:40:08 +00003458 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003459
David Gibson5e985962016-12-20 16:49:05 +11003460 /* Initialization for future HPT resizes */
3461 kvm->arch.resize_hpt = NULL;
3462
Paul Mackerras512691d2012-10-15 01:15:41 +00003463 /*
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003464 * Work out how many sets the TLB has, for the use of
3465 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
3466 */
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003467 if (kvm_is_radix(kvm))
3468 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
3469 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003470 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
3471 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
3472 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
3473 else
3474 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
3475
3476 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003477 * Track that we now have a HV mode VM active. This blocks secondary
3478 * CPU threads from coming online.
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003479 * On POWER9, we only need to do this for HPT guests on a radix
3480 * host, which is not yet supported.
Paul Mackerras512691d2012-10-15 01:15:41 +00003481 */
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003482 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3483 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003484
Paul Mackerrase23a8082015-03-28 14:21:01 +11003485 /*
3486 * Create a debugfs directory for the VM
3487 */
3488 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3489 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3490 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3491 kvmppc_mmu_debugfs_init(kvm);
3492
David Gibson54738c02011-06-29 00:22:41 +00003493 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003494}
3495
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303496static void kvmppc_free_vcores(struct kvm *kvm)
3497{
3498 long int i;
3499
Paul Mackerras23316312015-10-21 16:03:14 +11003500 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303501 kfree(kvm->arch.vcores[i]);
3502 kvm->arch.online_vcores = 0;
3503}
3504
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303505static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003506{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003507 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3508
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003509 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3510 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003511
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303512 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003513
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003514 kvmppc_free_lpid(kvm->arch.lpid);
3515
Paul Mackerras5a319352017-01-30 21:21:46 +11003516 if (kvm_is_radix(kvm))
3517 kvmppc_free_radix(kvm);
3518 else
David Gibsonaae07772016-12-20 16:49:02 +11003519 kvmppc_free_hpt(&kvm->arch.hpt);
Suresh Warrierc57875f2016-08-19 15:35:50 +10003520
3521 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003522}
3523
3524/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303525static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3526 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003527{
3528 return EMULATE_FAIL;
3529}
3530
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303531static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3532 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003533{
3534 return EMULATE_FAIL;
3535}
3536
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303537static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3538 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003539{
3540 return EMULATE_FAIL;
3541}
3542
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303543static int kvmppc_core_check_processor_compat_hv(void)
3544{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003545 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3546 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303547 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003548
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303549 return 0;
3550}
3551
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003552#ifdef CONFIG_KVM_XICS
3553
3554void kvmppc_free_pimap(struct kvm *kvm)
3555{
3556 kfree(kvm->arch.pimap);
3557}
3558
Suresh Warrierc57875f2016-08-19 15:35:50 +10003559static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003560{
3561 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
3562}
Suresh Warrierc57875f2016-08-19 15:35:50 +10003563
3564static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3565{
3566 struct irq_desc *desc;
3567 struct kvmppc_irq_map *irq_map;
3568 struct kvmppc_passthru_irqmap *pimap;
3569 struct irq_chip *chip;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003570 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003571
Suresh Warrier644abbb2016-08-19 15:35:54 +10003572 if (!kvm_irq_bypass)
3573 return 1;
3574
Suresh Warrierc57875f2016-08-19 15:35:50 +10003575 desc = irq_to_desc(host_irq);
3576 if (!desc)
3577 return -EIO;
3578
3579 mutex_lock(&kvm->lock);
3580
3581 pimap = kvm->arch.pimap;
3582 if (pimap == NULL) {
3583 /* First call, allocate structure to hold IRQ map */
3584 pimap = kvmppc_alloc_pimap();
3585 if (pimap == NULL) {
3586 mutex_unlock(&kvm->lock);
3587 return -ENOMEM;
3588 }
3589 kvm->arch.pimap = pimap;
3590 }
3591
3592 /*
3593 * For now, we only support interrupts for which the EOI operation
3594 * is an OPAL call followed by a write to XIRR, since that's
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003595 * what our real-mode EOI code does, or a XIVE interrupt
Suresh Warrierc57875f2016-08-19 15:35:50 +10003596 */
3597 chip = irq_data_get_irq_chip(&desc->irq_data);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003598 if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
Suresh Warrierc57875f2016-08-19 15:35:50 +10003599 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
3600 host_irq, guest_gsi);
3601 mutex_unlock(&kvm->lock);
3602 return -ENOENT;
3603 }
3604
3605 /*
3606 * See if we already have an entry for this guest IRQ number.
3607 * If it's mapped to a hardware IRQ number, that's an error,
3608 * otherwise re-use this entry.
3609 */
3610 for (i = 0; i < pimap->n_mapped; i++) {
3611 if (guest_gsi == pimap->mapped[i].v_hwirq) {
3612 if (pimap->mapped[i].r_hwirq) {
3613 mutex_unlock(&kvm->lock);
3614 return -EINVAL;
3615 }
3616 break;
3617 }
3618 }
3619
3620 if (i == KVMPPC_PIRQ_MAPPED) {
3621 mutex_unlock(&kvm->lock);
3622 return -EAGAIN; /* table is full */
3623 }
3624
3625 irq_map = &pimap->mapped[i];
3626
3627 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003628 irq_map->desc = desc;
3629
Suresh Warriere3c13e52016-08-19 15:35:51 +10003630 /*
3631 * Order the above two stores before the next to serialize with
3632 * the KVM real mode handler.
3633 */
3634 smp_wmb();
3635 irq_map->r_hwirq = desc->irq_data.hwirq;
3636
Suresh Warrierc57875f2016-08-19 15:35:50 +10003637 if (i == pimap->n_mapped)
3638 pimap->n_mapped++;
3639
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003640 if (xive_enabled())
3641 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
3642 else
3643 kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
3644 if (rc)
3645 irq_map->r_hwirq = 0;
Paul Mackerras5d375192016-08-19 15:35:56 +10003646
Suresh Warrierc57875f2016-08-19 15:35:50 +10003647 mutex_unlock(&kvm->lock);
3648
3649 return 0;
3650}
3651
3652static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3653{
3654 struct irq_desc *desc;
3655 struct kvmppc_passthru_irqmap *pimap;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003656 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003657
Suresh Warrier644abbb2016-08-19 15:35:54 +10003658 if (!kvm_irq_bypass)
3659 return 0;
3660
Suresh Warrierc57875f2016-08-19 15:35:50 +10003661 desc = irq_to_desc(host_irq);
3662 if (!desc)
3663 return -EIO;
3664
3665 mutex_lock(&kvm->lock);
Markus Elfringa1c52e12017-01-20 11:00:08 +01003666 if (!kvm->arch.pimap)
3667 goto unlock;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003668
Suresh Warrierc57875f2016-08-19 15:35:50 +10003669 pimap = kvm->arch.pimap;
3670
3671 for (i = 0; i < pimap->n_mapped; i++) {
3672 if (guest_gsi == pimap->mapped[i].v_hwirq)
3673 break;
3674 }
3675
3676 if (i == pimap->n_mapped) {
3677 mutex_unlock(&kvm->lock);
3678 return -ENODEV;
3679 }
3680
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003681 if (xive_enabled())
3682 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
3683 else
3684 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
Paul Mackerras5d375192016-08-19 15:35:56 +10003685
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003686 /* invalidate the entry (what do do on error from the above ?) */
Suresh Warrierc57875f2016-08-19 15:35:50 +10003687 pimap->mapped[i].r_hwirq = 0;
3688
3689 /*
3690 * We don't free this structure even when the count goes to
3691 * zero. The structure is freed when we destroy the VM.
3692 */
Markus Elfringa1c52e12017-01-20 11:00:08 +01003693 unlock:
Suresh Warrierc57875f2016-08-19 15:35:50 +10003694 mutex_unlock(&kvm->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003695 return rc;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003696}
3697
3698static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
3699 struct irq_bypass_producer *prod)
3700{
3701 int ret = 0;
3702 struct kvm_kernel_irqfd *irqfd =
3703 container_of(cons, struct kvm_kernel_irqfd, consumer);
3704
3705 irqfd->producer = prod;
3706
3707 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3708 if (ret)
3709 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
3710 prod->irq, irqfd->gsi, ret);
3711
3712 return ret;
3713}
3714
3715static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
3716 struct irq_bypass_producer *prod)
3717{
3718 int ret;
3719 struct kvm_kernel_irqfd *irqfd =
3720 container_of(cons, struct kvm_kernel_irqfd, consumer);
3721
3722 irqfd->producer = NULL;
3723
3724 /*
3725 * When producer of consumer is unregistered, we change back to
3726 * default external interrupt handling mode - KVM real mode
3727 * will switch back to host.
3728 */
3729 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3730 if (ret)
3731 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
3732 prod->irq, irqfd->gsi, ret);
3733}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003734#endif
3735
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303736static long kvm_arch_vm_ioctl_hv(struct file *filp,
3737 unsigned int ioctl, unsigned long arg)
3738{
3739 struct kvm *kvm __maybe_unused = filp->private_data;
3740 void __user *argp = (void __user *)arg;
3741 long r;
3742
3743 switch (ioctl) {
3744
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303745 case KVM_PPC_ALLOCATE_HTAB: {
3746 u32 htab_order;
3747
3748 r = -EFAULT;
3749 if (get_user(htab_order, (u32 __user *)argp))
3750 break;
David Gibsonf98a8bf2016-12-20 16:49:03 +11003751 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303752 if (r)
3753 break;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303754 r = 0;
3755 break;
3756 }
3757
3758 case KVM_PPC_GET_HTAB_FD: {
3759 struct kvm_get_htab_fd ghf;
3760
3761 r = -EFAULT;
3762 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3763 break;
3764 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3765 break;
3766 }
3767
David Gibson5e985962016-12-20 16:49:05 +11003768 case KVM_PPC_RESIZE_HPT_PREPARE: {
3769 struct kvm_ppc_resize_hpt rhpt;
3770
3771 r = -EFAULT;
3772 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
3773 break;
3774
3775 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
3776 break;
3777 }
3778
3779 case KVM_PPC_RESIZE_HPT_COMMIT: {
3780 struct kvm_ppc_resize_hpt rhpt;
3781
3782 r = -EFAULT;
3783 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
3784 break;
3785
3786 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
3787 break;
3788 }
3789
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303790 default:
3791 r = -ENOTTY;
3792 }
3793
3794 return r;
3795}
3796
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003797/*
3798 * List of hcall numbers to enable by default.
3799 * For compatibility with old userspace, we enable by default
3800 * all hcalls that were implemented before the hcall-enabling
3801 * facility was added. Note this list should not include H_RTAS.
3802 */
3803static unsigned int default_hcall_list[] = {
3804 H_REMOVE,
3805 H_ENTER,
3806 H_READ,
3807 H_PROTECT,
3808 H_BULK_REMOVE,
3809 H_GET_TCE,
3810 H_PUT_TCE,
3811 H_SET_DABR,
3812 H_SET_XDABR,
3813 H_CEDE,
3814 H_PROD,
3815 H_CONFER,
3816 H_REGISTER_VPA,
3817#ifdef CONFIG_KVM_XICS
3818 H_EOI,
3819 H_CPPR,
3820 H_IPI,
3821 H_IPOLL,
3822 H_XIRR,
3823 H_XIRR_X,
3824#endif
3825 0
3826};
3827
3828static void init_default_hcalls(void)
3829{
3830 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003831 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003832
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003833 for (i = 0; default_hcall_list[i]; ++i) {
3834 hcall = default_hcall_list[i];
3835 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3836 __set_bit(hcall / 4, default_enabled_hcalls);
3837 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003838}
3839
Paul Mackerrasc9270132017-01-30 21:21:41 +11003840static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
3841{
Paul Mackerras468808b2017-01-30 21:21:42 +11003842 unsigned long lpcr;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003843 int radix;
Paul Mackerras468808b2017-01-30 21:21:42 +11003844
3845 /* If not on a POWER9, reject it */
3846 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3847 return -ENODEV;
3848
3849 /* If any unknown flags set, reject it */
3850 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
3851 return -EINVAL;
3852
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003853 /* We can't change a guest to/from radix yet */
3854 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
3855 if (radix != kvm_is_radix(kvm))
Paul Mackerras468808b2017-01-30 21:21:42 +11003856 return -EINVAL;
3857
3858 /* GR (guest radix) bit in process_table field must match */
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003859 if (!!(cfg->process_table & PATB_GR) != radix)
Paul Mackerras468808b2017-01-30 21:21:42 +11003860 return -EINVAL;
3861
3862 /* Process table size field must be reasonable, i.e. <= 24 */
3863 if ((cfg->process_table & PRTS_MASK) > 24)
3864 return -EINVAL;
3865
3866 kvm->arch.process_table = cfg->process_table;
3867 kvmppc_setup_partition_table(kvm);
3868
3869 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
3870 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
3871
3872 return 0;
Paul Mackerrasc9270132017-01-30 21:21:41 +11003873}
3874
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303875static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303876 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3877 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3878 .get_one_reg = kvmppc_get_one_reg_hv,
3879 .set_one_reg = kvmppc_set_one_reg_hv,
3880 .vcpu_load = kvmppc_core_vcpu_load_hv,
3881 .vcpu_put = kvmppc_core_vcpu_put_hv,
3882 .set_msr = kvmppc_set_msr_hv,
3883 .vcpu_run = kvmppc_vcpu_run_hv,
3884 .vcpu_create = kvmppc_core_vcpu_create_hv,
3885 .vcpu_free = kvmppc_core_vcpu_free_hv,
3886 .check_requests = kvmppc_core_check_requests_hv,
3887 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3888 .flush_memslot = kvmppc_core_flush_memslot_hv,
3889 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3890 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3891 .unmap_hva = kvm_unmap_hva_hv,
3892 .unmap_hva_range = kvm_unmap_hva_range_hv,
3893 .age_hva = kvm_age_hva_hv,
3894 .test_age_hva = kvm_test_age_hva_hv,
3895 .set_spte_hva = kvm_set_spte_hva_hv,
3896 .mmu_destroy = kvmppc_mmu_destroy_hv,
3897 .free_memslot = kvmppc_core_free_memslot_hv,
3898 .create_memslot = kvmppc_core_create_memslot_hv,
3899 .init_vm = kvmppc_core_init_vm_hv,
3900 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303901 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3902 .emulate_op = kvmppc_core_emulate_op_hv,
3903 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3904 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3905 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3906 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003907 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10003908#ifdef CONFIG_KVM_XICS
3909 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
3910 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
3911#endif
Paul Mackerrasc9270132017-01-30 21:21:41 +11003912 .configure_mmu = kvmhv_configure_mmu,
3913 .get_rmmu_info = kvmhv_get_rmmu_info,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303914};
3915
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303916static int kvm_init_subcore_bitmap(void)
3917{
3918 int i, j;
3919 int nr_cores = cpu_nr_cores();
3920 struct sibling_subcore_state *sibling_subcore_state;
3921
3922 for (i = 0; i < nr_cores; i++) {
3923 int first_cpu = i * threads_per_core;
3924 int node = cpu_to_node(first_cpu);
3925
3926 /* Ignore if it is already allocated. */
3927 if (paca[first_cpu].sibling_subcore_state)
3928 continue;
3929
3930 sibling_subcore_state =
3931 kmalloc_node(sizeof(struct sibling_subcore_state),
3932 GFP_KERNEL, node);
3933 if (!sibling_subcore_state)
3934 return -ENOMEM;
3935
3936 memset(sibling_subcore_state, 0,
3937 sizeof(struct sibling_subcore_state));
3938
3939 for (j = 0; j < threads_per_core; j++) {
3940 int cpu = first_cpu + j;
3941
3942 paca[cpu].sibling_subcore_state = sibling_subcore_state;
3943 }
3944 }
3945 return 0;
3946}
3947
Paul Mackerras5a319352017-01-30 21:21:46 +11003948static int kvmppc_radix_possible(void)
3949{
3950 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
3951}
3952
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303953static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003954{
3955 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303956 /*
3957 * FIXME!! Do we need to check on all cpus ?
3958 */
3959 r = kvmppc_core_check_processor_compat_hv();
3960 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003961 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003962
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303963 r = kvm_init_subcore_bitmap();
3964 if (r)
3965 return r;
3966
Paul Mackerrasf7257582016-11-18 09:02:08 +11003967 /*
3968 * We need a way of accessing the XICS interrupt controller,
3969 * either directly, via paca[cpu].kvm_hstate.xics_phys, or
3970 * indirectly, via OPAL.
3971 */
3972#ifdef CONFIG_SMP
Paul Mackerrasfb7dcf72017-04-28 08:23:16 +10003973 if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +11003974 struct device_node *np;
3975
3976 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
3977 if (!np) {
3978 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
3979 return -ENODEV;
3980 }
3981 }
3982#endif
3983
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303984 kvm_ops_hv.owner = THIS_MODULE;
3985 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003986
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003987 init_default_hcalls();
3988
Paul Mackerrasec257162015-06-24 21:18:03 +10003989 init_vcore_lists();
3990
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303991 r = kvmppc_mmu_hv_init();
Paul Mackerras5a319352017-01-30 21:21:46 +11003992 if (r)
3993 return r;
3994
3995 if (kvmppc_radix_possible())
3996 r = kvmppc_radix_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003997 return r;
3998}
3999
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304000static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00004001{
Suresh Warrier79b6c242015-12-17 14:59:06 -06004002 kvmppc_free_host_rm_ops();
Paul Mackerras5a319352017-01-30 21:21:46 +11004003 if (kvmppc_radix_possible())
4004 kvmppc_radix_exit();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304005 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004006}
4007
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304008module_init(kvmppc_book3s_init_hv);
4009module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05304010MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01004011MODULE_ALIAS_MISCDEV(KVM_MINOR);
4012MODULE_ALIAS("devname:kvm");
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11004013