blob: b5fbf76179522c4905192c60bb86e0fce7ae4c19 [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>
Thomas Meyer4bb817ed2017-09-03 14:19:31 +020022#include <linux/kernel.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000023#include <linux/err.h>
24#include <linux/slab.h>
25#include <linux/preempt.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010027#include <linux/sched/stat.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040029#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000030#include <linux/fs.h>
31#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100032#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000033#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000034#include <linux/spinlock.h>
35#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000036#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010037#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110038#include <linux/debugfs.h>
Benjamin Herrenschmidtd3989142017-04-05 17:54:51 +100039#include <linux/gfp.h>
40#include <linux/vmalloc.h>
41#include <linux/highmem.h>
42#include <linux/hugetlb.h>
43#include <linux/kvm_irqfd.h>
44#include <linux/irqbypass.h>
45#include <linux/module.h>
46#include <linux/compiler.h>
47#include <linux/of.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000048
49#include <asm/reg.h>
Paul Mackerras57900692017-05-16 16:41:20 +100050#include <asm/ppc-opcode.h>
51#include <asm/disassemble.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000052#include <asm/cputable.h>
53#include <asm/cacheflush.h>
54#include <asm/tlbflush.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080055#include <linux/uaccess.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000056#include <asm/io.h>
57#include <asm/kvm_ppc.h>
58#include <asm/kvm_book3s.h>
59#include <asm/mmu_context.h>
60#include <asm/lppaca.h>
61#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000062#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000063#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000064#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010065#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000066#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110067#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053068#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100069#include <asm/pnv-pci.h>
Paul Mackerras7a840842016-11-16 22:25:20 +110070#include <asm/mmu.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110071#include <asm/opal.h>
72#include <asm/xics.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100073#include <asm/xive.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053075#include "book3s.h"
76
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060077#define CREATE_TRACE_POINTS
78#include "trace_hv.h"
79
Paul Mackerrasde56a942011-06-29 00:21:34 +000080/* #define EXIT_DEBUG */
81/* #define EXIT_DEBUG_SIMPLE */
82/* #define EXIT_DEBUG_INT */
83
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000084/* Used to indicate that a guest page fault needs to be handled */
85#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100086/* Used to indicate that a guest passthrough interrupt needs to be handled */
87#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000088
Paul Mackerrasc7b67672012-10-15 01:18:07 +000089/* Used as a "null" value for timebase values */
90#define TB_NIL (~(u64)0)
91
Paul Mackerras699a0ea2014-06-02 11:02:59 +100092static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
93
Paul Mackerrasb4deba52015-07-02 20:38:16 +100094static int dynamic_mt_modes = 6;
95module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
96MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
Paul Mackerrasec257162015-06-24 21:18:03 +100097static int target_smt_mode;
98module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
99MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +1000100
Paul Mackerras516f7892017-10-16 16:11:57 +1100101static bool indep_threads_mode = true;
102module_param(indep_threads_mode, bool, S_IRUGO | S_IWUSR);
103MODULE_PARM_DESC(indep_threads_mode, "Independent-threads mode (only on POWER9)");
104
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600105#ifdef CONFIG_KVM_XICS
106static struct kernel_param_ops module_param_ops = {
107 .set = param_set_int,
108 .get = param_get_int,
109};
110
Suresh Warrier644abbb2016-08-19 15:35:54 +1000111module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
112 S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
114
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600115module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
116 S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
118#endif
119
Paul Mackerras19ccb762011-07-23 17:42:46 +1000120static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +0000121static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras1b151ce2017-09-13 15:53:48 +1000122static void kvmppc_setup_partition_table(struct kvm *kvm);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000123
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000124static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
125 int *ip)
126{
127 int i = *ip;
128 struct kvm_vcpu *vcpu;
129
130 while (++i < MAX_SMT_THREADS) {
131 vcpu = READ_ONCE(vc->runnable_threads[i]);
132 if (vcpu) {
133 *ip = i;
134 return vcpu;
135 }
136 }
137 return NULL;
138}
139
140/* Used to traverse the list of runnable threads for a given vcore */
141#define for_each_runnable_thread(i, vcpu, vc) \
142 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
143
Paul Mackerras66feed62015-03-28 14:21:12 +1100144static bool kvmppc_ipi_thread(int cpu)
145{
Paul Mackerras1704a812016-11-18 08:47:08 +1100146 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
147
148 /* On POWER9 we can use msgsnd to IPI any cpu */
149 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
150 msg |= get_hard_smp_processor_id(cpu);
151 smp_mb();
152 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
153 return true;
154 }
155
Paul Mackerras66feed62015-03-28 14:21:12 +1100156 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
157 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
158 preempt_disable();
159 if (cpu_first_thread_sibling(cpu) ==
160 cpu_first_thread_sibling(smp_processor_id())) {
Paul Mackerras66feed62015-03-28 14:21:12 +1100161 msg |= cpu_thread_in_core(cpu);
162 smp_mb();
163 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
164 preempt_enable();
165 return true;
166 }
167 preempt_enable();
168 }
169
170#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrasf7257582016-11-18 09:02:08 +1100171 if (cpu >= 0 && cpu < nr_cpu_ids) {
172 if (paca[cpu].kvm_hstate.xics_phys) {
173 xics_wake_cpu(cpu);
174 return true;
175 }
176 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
Paul Mackerras66feed62015-03-28 14:21:12 +1100177 return true;
178 }
179#endif
180
181 return false;
182}
183
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530184static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000185{
Paul Mackerrasec257162015-06-24 21:18:03 +1000186 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100187 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000188
189 wqp = kvm_arch_vcpu_wq(vcpu);
Davidlohr Bueso267ad7b2017-09-13 13:08:23 -0700190 if (swq_has_sleeper(wqp)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +0100191 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000192 ++vcpu->stat.halt_wakeup;
193 }
194
Paul Mackerras3deda5e2016-12-20 14:02:29 +1100195 cpu = READ_ONCE(vcpu->arch.thread_cpu);
196 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100197 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000198
199 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000200 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100201 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
202 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000203}
204
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000205/*
206 * We use the vcpu_load/put functions to measure stolen time.
207 * Stolen time is counted as time when either the vcpu is able to
208 * run as part of a virtual core, but the task running the vcore
209 * is preempted or sleeping, or when the vcpu needs something done
210 * in the kernel by the task running the vcpu, but that task is
211 * preempted or sleeping. Those two things have to be counted
212 * separately, since one of the vcpu tasks will take on the job
213 * of running the core, and the other vcpu tasks in the vcore will
214 * sleep waiting for it to do that, but that sleep shouldn't count
215 * as stolen time.
216 *
217 * Hence we accumulate stolen time when the vcpu can run as part of
218 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
219 * needs its task to do other things in the kernel (for example,
220 * service a page fault) in busy_stolen. We don't accumulate
221 * stolen time for a vcore when it is inactive, or for a vcpu
222 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
223 * a misnomer; it means that the vcpu task is not executing in
224 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
225 * the kernel. We don't have any way of dividing up that time
226 * between time that the vcpu is genuinely stopped, time that
227 * the task is actively working on behalf of the vcpu, and time
228 * that the task is preempted, so we don't count any of it as
229 * stolen.
230 *
231 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100232 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
233 * lock. The stolen times are measured in units of timebase ticks.
234 * (Note that the != TB_NIL checks below are purely defensive;
235 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000236 */
237
Paul Mackerrasec257162015-06-24 21:18:03 +1000238static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
239{
240 unsigned long flags;
241
242 spin_lock_irqsave(&vc->stoltb_lock, flags);
243 vc->preempt_tb = mftb();
244 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
245}
246
247static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
248{
249 unsigned long flags;
250
251 spin_lock_irqsave(&vc->stoltb_lock, flags);
252 if (vc->preempt_tb != TB_NIL) {
253 vc->stolen_tb += mftb() - vc->preempt_tb;
254 vc->preempt_tb = TB_NIL;
255 }
256 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
257}
258
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530259static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000260{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000261 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100262 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000263
Paul Mackerras2711e242014-12-04 16:43:28 +1100264 /*
265 * We can test vc->runner without taking the vcore lock,
266 * because only this task ever sets vc->runner to this
267 * vcpu, and once it is set to this vcpu, only this task
268 * ever sets it to NULL.
269 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000270 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
271 kvmppc_core_end_stolen(vc);
272
Paul Mackerras2711e242014-12-04 16:43:28 +1100273 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000274 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
275 vcpu->arch.busy_preempt != TB_NIL) {
276 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
277 vcpu->arch.busy_preempt = TB_NIL;
278 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100279 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000280}
281
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530282static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000283{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000284 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100285 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000286
Paul Mackerrasec257162015-06-24 21:18:03 +1000287 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
288 kvmppc_core_start_stolen(vc);
289
Paul Mackerras2711e242014-12-04 16:43:28 +1100290 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000291 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
292 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100293 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000294}
295
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530296static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000297{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100298 /*
299 * Check for illegal transactional state bit combination
300 * and if we find it, force the TS field to a safe state.
301 */
302 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
303 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000304 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000305 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000306}
307
Thomas Huth5358a962015-05-22 09:25:02 +0200308static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000309{
310 vcpu->arch.pvr = pvr;
311}
312
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100313/* Dummy value used in computing PCR value below */
314#define PCR_ARCH_300 (PCR_ARCH_207 << 1)
315
Thomas Huth5358a962015-05-22 09:25:02 +0200316static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000317{
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100318 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000319 struct kvmppc_vcore *vc = vcpu->arch.vcore;
320
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100321 /* We can (emulate) our own architecture version and anything older */
322 if (cpu_has_feature(CPU_FTR_ARCH_300))
323 host_pcr_bit = PCR_ARCH_300;
324 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
325 host_pcr_bit = PCR_ARCH_207;
326 else if (cpu_has_feature(CPU_FTR_ARCH_206))
327 host_pcr_bit = PCR_ARCH_206;
328 else
329 host_pcr_bit = PCR_ARCH_205;
330
331 /* Determine lowest PCR bit needed to run guest in given PVR level */
332 guest_pcr_bit = host_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000333 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000334 switch (arch_compat) {
335 case PVR_ARCH_205:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100336 guest_pcr_bit = PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000337 break;
338 case PVR_ARCH_206:
339 case PVR_ARCH_206p:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100340 guest_pcr_bit = PCR_ARCH_206;
Paul Mackerras5557ae02014-01-08 21:25:24 +1100341 break;
342 case PVR_ARCH_207:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100343 guest_pcr_bit = PCR_ARCH_207;
344 break;
345 case PVR_ARCH_300:
346 guest_pcr_bit = PCR_ARCH_300;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000347 break;
348 default:
349 return -EINVAL;
350 }
351 }
352
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100353 /* Check requested PCR bits don't exceed our capabilities */
354 if (guest_pcr_bit > host_pcr_bit)
355 return -EINVAL;
356
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000357 spin_lock(&vc->lock);
358 vc->arch_compat = arch_compat;
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100359 /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
360 vc->pcr = host_pcr_bit - guest_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000361 spin_unlock(&vc->lock);
362
363 return 0;
364}
365
Thomas Huth5358a962015-05-22 09:25:02 +0200366static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000367{
368 int r;
369
370 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
371 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
372 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
373 for (r = 0; r < 16; ++r)
374 pr_err("r%2d = %.16lx r%d = %.16lx\n",
375 r, kvmppc_get_gpr(vcpu, r),
376 r+16, kvmppc_get_gpr(vcpu, r+16));
377 pr_err("ctr = %.16lx lr = %.16lx\n",
378 vcpu->arch.ctr, vcpu->arch.lr);
379 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
380 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
381 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
382 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
383 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
384 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
385 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
386 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
387 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
388 pr_err("fault dar = %.16lx dsisr = %.8x\n",
389 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
390 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
391 for (r = 0; r < vcpu->arch.slb_max; ++r)
392 pr_err(" ESID = %.16llx VSID = %.16llx\n",
393 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
394 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000395 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000396 vcpu->arch.last_inst);
397}
398
Thomas Huth5358a962015-05-22 09:25:02 +0200399static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000400{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100401 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000402
403 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100404 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405 mutex_unlock(&kvm->lock);
406 return ret;
407}
408
409static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
410{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000411 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200412 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000413}
414
Paul Mackerras55b665b2012-09-25 20:33:06 +0000415static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
416 unsigned long addr, unsigned long len)
417{
418 /* check address is cacheline aligned */
419 if (addr & (L1_CACHE_BYTES - 1))
420 return -EINVAL;
421 spin_lock(&vcpu->arch.vpa_update_lock);
422 if (v->next_gpa != addr || v->len != len) {
423 v->next_gpa = addr;
424 v->len = addr ? len : 0;
425 v->update_pending = 1;
426 }
427 spin_unlock(&vcpu->arch.vpa_update_lock);
428 return 0;
429}
430
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000431/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
432struct reg_vpa {
433 u32 dummy;
434 union {
Alexander Graf02407552014-06-11 10:34:19 +0200435 __be16 hword;
436 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000437 } length;
438};
439
440static int vpa_is_registered(struct kvmppc_vpa *vpap)
441{
442 if (vpap->update_pending)
443 return vpap->next_gpa != 0;
444 return vpap->pinned_addr != NULL;
445}
446
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000447static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
448 unsigned long flags,
449 unsigned long vcpuid, unsigned long vpa)
450{
451 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000452 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000453 void *va;
454 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000455 int err;
456 int subfunc;
457 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000458
459 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
460 if (!tvcpu)
461 return H_PARAMETER;
462
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000463 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
464 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
465 subfunc == H_VPA_REG_SLB) {
466 /* Registering new area - address must be cache-line aligned */
467 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000468 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000469
470 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000471 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
472 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000473 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000474 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200475 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000476 else
Alexander Graf02407552014-06-11 10:34:19 +0200477 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000478 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000479
480 /* Check length */
481 if (len > nb || len < sizeof(struct reg_vpa))
482 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000483 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000484 vpa = 0;
485 len = 0;
486 }
487
488 err = H_PARAMETER;
489 vpap = NULL;
490 spin_lock(&tvcpu->arch.vpa_update_lock);
491
492 switch (subfunc) {
493 case H_VPA_REG_VPA: /* register VPA */
Nicholas Piggineaac112e2017-08-13 11:33:38 +1000494 /*
495 * The size of our lppaca is 1kB because of the way we align
496 * it for the guest to avoid crossing a 4kB boundary. We only
497 * use 640 bytes of the structure though, so we should accept
498 * clients that set a size of 640.
499 */
500 if (len < 640)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000501 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000502 vpap = &tvcpu->arch.vpa;
503 err = 0;
504 break;
505
506 case H_VPA_REG_DTL: /* register DTL */
507 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000508 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000509 len -= len % sizeof(struct dtl_entry);
510
511 /* Check that they have previously registered a VPA */
512 err = H_RESOURCE;
513 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000514 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000515
516 vpap = &tvcpu->arch.dtl;
517 err = 0;
518 break;
519
520 case H_VPA_REG_SLB: /* register SLB shadow buffer */
521 /* Check that they have previously registered a VPA */
522 err = H_RESOURCE;
523 if (!vpa_is_registered(&tvcpu->arch.vpa))
524 break;
525
526 vpap = &tvcpu->arch.slb_shadow;
527 err = 0;
528 break;
529
530 case H_VPA_DEREG_VPA: /* deregister VPA */
531 /* Check they don't still have a DTL or SLB buf registered */
532 err = H_RESOURCE;
533 if (vpa_is_registered(&tvcpu->arch.dtl) ||
534 vpa_is_registered(&tvcpu->arch.slb_shadow))
535 break;
536
537 vpap = &tvcpu->arch.vpa;
538 err = 0;
539 break;
540
541 case H_VPA_DEREG_DTL: /* deregister DTL */
542 vpap = &tvcpu->arch.dtl;
543 err = 0;
544 break;
545
546 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
547 vpap = &tvcpu->arch.slb_shadow;
548 err = 0;
549 break;
550 }
551
552 if (vpap) {
553 vpap->next_gpa = vpa;
554 vpap->len = len;
555 vpap->update_pending = 1;
556 }
557
558 spin_unlock(&tvcpu->arch.vpa_update_lock);
559
560 return err;
561}
562
Paul Mackerras081f3232012-06-01 20:20:24 +1000563static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000564{
Paul Mackerras081f3232012-06-01 20:20:24 +1000565 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000566 void *va;
567 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000568 unsigned long gpa;
569
570 /*
571 * We need to pin the page pointed to by vpap->next_gpa,
572 * but we can't call kvmppc_pin_guest_page under the lock
573 * as it does get_user_pages() and down_read(). So we
574 * have to drop the lock, pin the page, then get the lock
575 * again and check that a new area didn't get registered
576 * in the meantime.
577 */
578 for (;;) {
579 gpa = vpap->next_gpa;
580 spin_unlock(&vcpu->arch.vpa_update_lock);
581 va = NULL;
582 nb = 0;
583 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000584 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000585 spin_lock(&vcpu->arch.vpa_update_lock);
586 if (gpa == vpap->next_gpa)
587 break;
588 /* sigh... unpin that one and try again */
589 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000590 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000591 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000592
593 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000594 if (va && nb < vpap->len) {
595 /*
596 * If it's now too short, it must be that userspace
597 * has changed the mappings underlying guest memory,
598 * so unregister the region.
599 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000600 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000601 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000602 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000603 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000604 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
605 vpap->dirty);
606 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000607 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000608 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000609 if (va)
610 vpap->pinned_end = va + vpap->len;
611}
Paul Mackerras93e60242011-12-12 12:28:55 +0000612
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000613static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
614{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000615 if (!(vcpu->arch.vpa.update_pending ||
616 vcpu->arch.slb_shadow.update_pending ||
617 vcpu->arch.dtl.update_pending))
618 return;
619
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000620 spin_lock(&vcpu->arch.vpa_update_lock);
621 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000622 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000623 if (vcpu->arch.vpa.pinned_addr)
624 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000625 }
626 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000627 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000628 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
629 vcpu->arch.dtl_index = 0;
630 }
631 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000632 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000633 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000634}
635
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000636/*
637 * Return the accumulated stolen time for the vcore up until `now'.
638 * The caller should hold the vcore lock.
639 */
640static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
641{
642 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100643 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000644
Paul Mackerras2711e242014-12-04 16:43:28 +1100645 spin_lock_irqsave(&vc->stoltb_lock, flags);
646 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000647 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100648 vc->preempt_tb != TB_NIL)
649 p += now - vc->preempt_tb;
650 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000651 return p;
652}
653
Paul Mackerras0456ec42012-02-03 00:56:21 +0000654static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
655 struct kvmppc_vcore *vc)
656{
657 struct dtl_entry *dt;
658 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000659 unsigned long stolen;
660 unsigned long core_stolen;
661 u64 now;
Paul Mackerras8b24e692017-06-26 15:45:51 +1000662 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000663
664 dt = vcpu->arch.dtl_ptr;
665 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000666 now = mftb();
667 core_stolen = vcore_stolen_time(vc, now);
668 stolen = core_stolen - vcpu->arch.stolen_logged;
669 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerras8b24e692017-06-26 15:45:51 +1000670 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000671 stolen += vcpu->arch.busy_stolen;
672 vcpu->arch.busy_stolen = 0;
Paul Mackerras8b24e692017-06-26 15:45:51 +1000673 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000674 if (!dt || !vpa)
675 return;
676 memset(dt, 0, sizeof(struct dtl_entry));
677 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200678 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
679 dt->timebase = cpu_to_be64(now + vc->tb_offset);
680 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
681 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
682 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000683 ++dt;
684 if (dt == vcpu->arch.dtl.pinned_end)
685 dt = vcpu->arch.dtl.pinned_addr;
686 vcpu->arch.dtl_ptr = dt;
687 /* order writing *dt vs. writing vpa->dtl_idx */
688 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200689 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000690 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000691}
692
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000693/* See if there is a doorbell interrupt pending for a vcpu */
694static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
695{
696 int thr;
697 struct kvmppc_vcore *vc;
698
Paul Mackerras57900692017-05-16 16:41:20 +1000699 if (vcpu->arch.doorbell_request)
700 return true;
701 /*
702 * Ensure that the read of vcore->dpdes comes after the read
703 * of vcpu->doorbell_request. This barrier matches the
704 * lwsync in book3s_hv_rmhandlers.S just before the
705 * fast_guest_return label.
706 */
707 smp_rmb();
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000708 vc = vcpu->arch.vcore;
709 thr = vcpu->vcpu_id - vc->first_vcpuid;
710 return !!(vc->dpdes & (1 << thr));
711}
712
Michael Neuling96423822014-06-02 11:03:01 +1000713static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
714{
715 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
716 return true;
717 if ((!vcpu->arch.vcore->arch_compat) &&
718 cpu_has_feature(CPU_FTR_ARCH_207S))
719 return true;
720 return false;
721}
722
723static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
724 unsigned long resource, unsigned long value1,
725 unsigned long value2)
726{
727 switch (resource) {
728 case H_SET_MODE_RESOURCE_SET_CIABR:
729 if (!kvmppc_power8_compatible(vcpu))
730 return H_P2;
731 if (value2)
732 return H_P4;
733 if (mflags)
734 return H_UNSUPPORTED_FLAG_START;
735 /* Guests can't breakpoint the hypervisor */
736 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
737 return H_P3;
738 vcpu->arch.ciabr = value1;
739 return H_SUCCESS;
740 case H_SET_MODE_RESOURCE_SET_DAWR:
741 if (!kvmppc_power8_compatible(vcpu))
742 return H_P2;
743 if (mflags)
744 return H_UNSUPPORTED_FLAG_START;
745 if (value2 & DABRX_HYP)
746 return H_P4;
747 vcpu->arch.dawr = value1;
748 vcpu->arch.dawrx = value2;
749 return H_SUCCESS;
750 default:
751 return H_TOO_HARD;
752 }
753}
754
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100755static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
756{
757 struct kvmppc_vcore *vcore = target->arch.vcore;
758
759 /*
760 * We expect to have been called by the real mode handler
761 * (kvmppc_rm_h_confer()) which would have directly returned
762 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
763 * have useful work to do and should not confer) so we don't
764 * recheck that here.
765 */
766
767 spin_lock(&vcore->lock);
768 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000769 vcore->vcore_state != VCORE_INACTIVE &&
770 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100771 target = vcore->runner;
772 spin_unlock(&vcore->lock);
773
774 return kvm_vcpu_yield_to(target);
775}
776
777static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
778{
779 int yield_count = 0;
780 struct lppaca *lppaca;
781
782 spin_lock(&vcpu->arch.vpa_update_lock);
783 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
784 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100785 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100786 spin_unlock(&vcpu->arch.vpa_update_lock);
787 return yield_count;
788}
789
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000790int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
791{
792 unsigned long req = kvmppc_get_gpr(vcpu, 3);
793 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100794 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000795 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000796 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000797
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000798 if (req <= MAX_HCALL_OPCODE &&
799 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
800 return RESUME_HOST;
801
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000802 switch (req) {
803 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000804 break;
805 case H_PROD:
806 target = kvmppc_get_gpr(vcpu, 4);
807 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
808 if (!tvcpu) {
809 ret = H_PARAMETER;
810 break;
811 }
812 tvcpu->arch.prodded = 1;
813 smp_mb();
Paul Mackerras8464c882016-12-06 20:42:05 +1100814 if (tvcpu->arch.ceded)
815 kvmppc_fast_vcpu_kick_hv(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000816 break;
817 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000818 target = kvmppc_get_gpr(vcpu, 4);
819 if (target == -1)
820 break;
821 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
822 if (!tvcpu) {
823 ret = H_PARAMETER;
824 break;
825 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100826 yield_count = kvmppc_get_gpr(vcpu, 5);
827 if (kvmppc_get_yield_count(tvcpu) != yield_count)
828 break;
829 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000830 break;
831 case H_REGISTER_VPA:
832 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
833 kvmppc_get_gpr(vcpu, 5),
834 kvmppc_get_gpr(vcpu, 6));
835 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000836 case H_RTAS:
837 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
838 return RESUME_HOST;
839
Paul Mackerrasc9438092013-11-16 17:46:05 +1100840 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000841 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100842 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000843
844 if (rc == -ENOENT)
845 return RESUME_HOST;
846 else if (rc == 0)
847 break;
848
849 /* Send the error out to userspace via KVM_RUN */
850 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100851 case H_LOGICAL_CI_LOAD:
852 ret = kvmppc_h_logical_ci_load(vcpu);
853 if (ret == H_TOO_HARD)
854 return RESUME_HOST;
855 break;
856 case H_LOGICAL_CI_STORE:
857 ret = kvmppc_h_logical_ci_store(vcpu);
858 if (ret == H_TOO_HARD)
859 return RESUME_HOST;
860 break;
Michael Neuling96423822014-06-02 11:03:01 +1000861 case H_SET_MODE:
862 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
863 kvmppc_get_gpr(vcpu, 5),
864 kvmppc_get_gpr(vcpu, 6),
865 kvmppc_get_gpr(vcpu, 7));
866 if (ret == H_TOO_HARD)
867 return RESUME_HOST;
868 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000869 case H_XIRR:
870 case H_CPPR:
871 case H_EOI:
872 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000873 case H_IPOLL:
874 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000875 if (kvmppc_xics_enabled(vcpu)) {
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000876 if (xive_enabled()) {
877 ret = H_NOT_AVAILABLE;
878 return RESUME_GUEST;
879 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000880 ret = kvmppc_xics_hcall(vcpu, req);
881 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100882 }
883 return RESUME_HOST;
884 case H_PUT_TCE:
885 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
886 kvmppc_get_gpr(vcpu, 5),
887 kvmppc_get_gpr(vcpu, 6));
888 if (ret == H_TOO_HARD)
889 return RESUME_HOST;
890 break;
891 case H_PUT_TCE_INDIRECT:
892 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
893 kvmppc_get_gpr(vcpu, 5),
894 kvmppc_get_gpr(vcpu, 6),
895 kvmppc_get_gpr(vcpu, 7));
896 if (ret == H_TOO_HARD)
897 return RESUME_HOST;
898 break;
899 case H_STUFF_TCE:
900 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
901 kvmppc_get_gpr(vcpu, 5),
902 kvmppc_get_gpr(vcpu, 6),
903 kvmppc_get_gpr(vcpu, 7));
904 if (ret == H_TOO_HARD)
905 return RESUME_HOST;
906 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000907 default:
908 return RESUME_HOST;
909 }
910 kvmppc_set_gpr(vcpu, 3, ret);
911 vcpu->arch.hcall_needed = 0;
912 return RESUME_GUEST;
913}
914
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000915static int kvmppc_hcall_impl_hv(unsigned long cmd)
916{
917 switch (cmd) {
918 case H_CEDE:
919 case H_PROD:
920 case H_CONFER:
921 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000922 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100923 case H_LOGICAL_CI_LOAD:
924 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000925#ifdef CONFIG_KVM_XICS
926 case H_XIRR:
927 case H_CPPR:
928 case H_EOI:
929 case H_IPI:
930 case H_IPOLL:
931 case H_XIRR_X:
932#endif
933 return 1;
934 }
935
936 /* See if it's in the real-mode table */
937 return kvmppc_hcall_impl_hv_realmode(cmd);
938}
939
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530940static int kvmppc_emulate_debug_inst(struct kvm_run *run,
941 struct kvm_vcpu *vcpu)
942{
943 u32 last_inst;
944
945 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
946 EMULATE_DONE) {
947 /*
948 * Fetch failed, so return to guest and
949 * try executing it again.
950 */
951 return RESUME_GUEST;
952 }
953
954 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
955 run->exit_reason = KVM_EXIT_DEBUG;
956 run->debug.arch.address = kvmppc_get_pc(vcpu);
957 return RESUME_HOST;
958 } else {
959 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
960 return RESUME_GUEST;
961 }
962}
963
Paul Mackerras57900692017-05-16 16:41:20 +1000964static void do_nothing(void *x)
965{
966}
967
968static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
969{
970 int thr, cpu, pcpu, nthreads;
971 struct kvm_vcpu *v;
972 unsigned long dpdes;
973
974 nthreads = vcpu->kvm->arch.emul_smt_mode;
975 dpdes = 0;
976 cpu = vcpu->vcpu_id & ~(nthreads - 1);
977 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
978 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
979 if (!v)
980 continue;
981 /*
982 * If the vcpu is currently running on a physical cpu thread,
983 * interrupt it in order to pull it out of the guest briefly,
984 * which will update its vcore->dpdes value.
985 */
986 pcpu = READ_ONCE(v->cpu);
987 if (pcpu >= 0)
988 smp_call_function_single(pcpu, do_nothing, NULL, 1);
989 if (kvmppc_doorbell_pending(v))
990 dpdes |= 1 << thr;
991 }
992 return dpdes;
993}
994
995/*
996 * On POWER9, emulate doorbell-related instructions in order to
997 * give the guest the illusion of running on a multi-threaded core.
998 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
999 * and mfspr DPDES.
1000 */
1001static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1002{
1003 u32 inst, rb, thr;
1004 unsigned long arg;
1005 struct kvm *kvm = vcpu->kvm;
1006 struct kvm_vcpu *tvcpu;
1007
1008 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1009 return EMULATE_FAIL;
1010 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
1011 return RESUME_GUEST;
1012 if (get_op(inst) != 31)
1013 return EMULATE_FAIL;
1014 rb = get_rb(inst);
1015 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1016 switch (get_xop(inst)) {
1017 case OP_31_XOP_MSGSNDP:
1018 arg = kvmppc_get_gpr(vcpu, rb);
1019 if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
1020 break;
1021 arg &= 0x3f;
1022 if (arg >= kvm->arch.emul_smt_mode)
1023 break;
1024 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1025 if (!tvcpu)
1026 break;
1027 if (!tvcpu->arch.doorbell_request) {
1028 tvcpu->arch.doorbell_request = 1;
1029 kvmppc_fast_vcpu_kick_hv(tvcpu);
1030 }
1031 break;
1032 case OP_31_XOP_MSGCLRP:
1033 arg = kvmppc_get_gpr(vcpu, rb);
1034 if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
1035 break;
1036 vcpu->arch.vcore->dpdes = 0;
1037 vcpu->arch.doorbell_request = 0;
1038 break;
1039 case OP_31_XOP_MFSPR:
1040 switch (get_sprn(inst)) {
1041 case SPRN_TIR:
1042 arg = thr;
1043 break;
1044 case SPRN_DPDES:
1045 arg = kvmppc_read_dpdes(vcpu);
1046 break;
1047 default:
1048 return EMULATE_FAIL;
1049 }
1050 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1051 break;
1052 default:
1053 return EMULATE_FAIL;
1054 }
1055 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1056 return RESUME_GUEST;
1057}
1058
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301059static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
1060 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001061{
1062 int r = RESUME_HOST;
1063
1064 vcpu->stat.sum_exits++;
1065
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11001066 /*
1067 * This can happen if an interrupt occurs in the last stages
1068 * of guest entry or the first stages of guest exit (i.e. after
1069 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1070 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1071 * That can happen due to a bug, or due to a machine check
1072 * occurring at just the wrong time.
1073 */
1074 if (vcpu->arch.shregs.msr & MSR_HV) {
1075 printk(KERN_EMERG "KVM trap in HV mode!\n");
1076 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1077 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1078 vcpu->arch.shregs.msr);
1079 kvmppc_dump_regs(vcpu);
1080 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1081 run->hw.hardware_exit_reason = vcpu->arch.trap;
1082 return RESUME_HOST;
1083 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001084 run->exit_reason = KVM_EXIT_UNKNOWN;
1085 run->ready_for_interrupt_injection = 1;
1086 switch (vcpu->arch.trap) {
1087 /* We're good on these - the host merely wanted to get our attention */
1088 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1089 vcpu->stat.dec_exits++;
1090 r = RESUME_GUEST;
1091 break;
1092 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +11001093 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerras84f71392016-11-22 14:30:14 +11001094 case BOOK3S_INTERRUPT_H_VIRT:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001095 vcpu->stat.ext_intr_exits++;
1096 r = RESUME_GUEST;
1097 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +11001098 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
1099 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001100 case BOOK3S_INTERRUPT_PERFMON:
1101 r = RESUME_GUEST;
1102 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001103 case BOOK3S_INTERRUPT_MACHINE_CHECK:
Aravinda Prasade20bbd32017-05-11 16:33:37 +05301104 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1105 run->exit_reason = KVM_EXIT_NMI;
1106 run->hw.hardware_exit_reason = vcpu->arch.trap;
1107 /* Clear out the old NMI status from run->flags */
1108 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1109 /* Now set the NMI status */
1110 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1111 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1112 else
1113 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1114
1115 r = RESUME_HOST;
1116 /* Print the MCE event to host console. */
1117 machine_check_print_event_info(&vcpu->arch.mce_evt, false);
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001118 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001119 case BOOK3S_INTERRUPT_PROGRAM:
1120 {
1121 ulong flags;
1122 /*
1123 * Normally program interrupts are delivered directly
1124 * to the guest by the hardware, but we can get here
1125 * as a result of a hypervisor emulation interrupt
1126 * (e40) getting turned into a 700 by BML RTAS.
1127 */
1128 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
1129 kvmppc_core_queue_program(vcpu, flags);
1130 r = RESUME_GUEST;
1131 break;
1132 }
1133 case BOOK3S_INTERRUPT_SYSCALL:
1134 {
1135 /* hcall - punt to userspace */
1136 int i;
1137
Liu Ping Fan27025a62013-11-19 14:12:48 +08001138 /* hypercall with MSR_PR has already been handled in rmode,
1139 * and never reaches here.
1140 */
1141
Paul Mackerrasde56a942011-06-29 00:21:34 +00001142 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1143 for (i = 0; i < 9; ++i)
1144 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1145 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1146 vcpu->arch.hcall_needed = 1;
1147 r = RESUME_HOST;
1148 break;
1149 }
1150 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001151 * We get these next two if the guest accesses a page which it thinks
1152 * it has mapped but which is not actually present, either because
1153 * it is for an emulated I/O device or because the corresonding
1154 * host page has been paged out. Any other HDSI/HISI interrupts
1155 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001156 */
1157 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001158 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001159 break;
1160 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001161 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1162 vcpu->arch.fault_dsisr = 0;
1163 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001164 break;
1165 /*
1166 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301167 * If the guest debug is disabled, generate a program interrupt
1168 * to the guest. If guest debug is enabled, we need to check
1169 * whether the instruction is a software breakpoint instruction.
1170 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001171 */
1172 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001173 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1174 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1175 swab32(vcpu->arch.emul_inst) :
1176 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301177 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1178 r = kvmppc_emulate_debug_inst(run, vcpu);
1179 } else {
1180 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1181 r = RESUME_GUEST;
1182 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001183 break;
1184 /*
1185 * This occurs if the guest (kernel or userspace), does something that
Paul Mackerras57900692017-05-16 16:41:20 +10001186 * is prohibited by HFSCR.
1187 * On POWER9, this could be a doorbell instruction that we need
1188 * to emulate.
1189 * Otherwise, we just generate a program interrupt to the guest.
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001190 */
1191 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
Paul Mackerras57900692017-05-16 16:41:20 +10001192 r = EMULATE_FAIL;
1193 if ((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG)
1194 r = kvmppc_emulate_doorbell_instr(vcpu);
1195 if (r == EMULATE_FAIL) {
1196 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1197 r = RESUME_GUEST;
1198 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001199 break;
Suresh Warrierf7af5202016-08-19 15:35:52 +10001200 case BOOK3S_INTERRUPT_HV_RM_HARD:
1201 r = RESUME_PASSTHROUGH;
1202 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001203 default:
1204 kvmppc_dump_regs(vcpu);
1205 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1206 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1207 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001208 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001209 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001210 break;
1211 }
1212
Paul Mackerrasde56a942011-06-29 00:21:34 +00001213 return r;
1214}
1215
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301216static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1217 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001218{
1219 int i;
1220
Paul Mackerrasde56a942011-06-29 00:21:34 +00001221 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301222 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001223 for (i = 0; i < vcpu->arch.slb_max; i++) {
1224 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1225 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1226 }
1227
1228 return 0;
1229}
1230
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301231static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1232 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001233{
1234 int i, j;
1235
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001236 /* Only accept the same PVR as the host's, since we can't spoof it */
1237 if (sregs->pvr != vcpu->arch.pvr)
1238 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001239
1240 j = 0;
1241 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1242 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1243 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1244 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1245 ++j;
1246 }
1247 }
1248 vcpu->arch.slb_max = j;
1249
1250 return 0;
1251}
1252
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001253static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1254 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001255{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001256 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001257 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1258 u64 mask;
1259
Paul Mackerras8f902b02015-03-20 20:39:38 +11001260 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001261 spin_lock(&vc->lock);
1262 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001263 * If ILE (interrupt little-endian) has changed, update the
1264 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1265 */
1266 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001267 struct kvm_vcpu *vcpu;
1268 int i;
1269
Anton Blanchardd6829162014-01-08 21:25:30 +11001270 kvm_for_each_vcpu(i, vcpu, kvm) {
1271 if (vcpu->arch.vcore != vc)
1272 continue;
1273 if (new_lpcr & LPCR_ILE)
1274 vcpu->arch.intr_msr |= MSR_LE;
1275 else
1276 vcpu->arch.intr_msr &= ~MSR_LE;
1277 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001278 }
1279
1280 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001281 * Userspace can only modify DPFD (default prefetch depth),
1282 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11001283 * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.).
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001284 */
1285 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001286 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1287 mask |= LPCR_AIL;
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001288 /*
1289 * On POWER9, allow userspace to enable large decrementer for the
1290 * guest, whether or not the host has it enabled.
1291 */
1292 if (cpu_has_feature(CPU_FTR_ARCH_300))
1293 mask |= LPCR_LD;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001294
1295 /* Broken 32-bit version of LPCR must not clear top bits */
1296 if (preserve_top32)
1297 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001298 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1299 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001300 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001301}
1302
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301303static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1304 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001305{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001306 int r = 0;
1307 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001308
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001309 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301310 case KVM_REG_PPC_DEBUG_INST:
1311 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1312 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001313 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001314 *val = get_reg_val(id, 0);
1315 break;
1316 case KVM_REG_PPC_DABR:
1317 *val = get_reg_val(id, vcpu->arch.dabr);
1318 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001319 case KVM_REG_PPC_DABRX:
1320 *val = get_reg_val(id, vcpu->arch.dabrx);
1321 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001322 case KVM_REG_PPC_DSCR:
1323 *val = get_reg_val(id, vcpu->arch.dscr);
1324 break;
1325 case KVM_REG_PPC_PURR:
1326 *val = get_reg_val(id, vcpu->arch.purr);
1327 break;
1328 case KVM_REG_PPC_SPURR:
1329 *val = get_reg_val(id, vcpu->arch.spurr);
1330 break;
1331 case KVM_REG_PPC_AMR:
1332 *val = get_reg_val(id, vcpu->arch.amr);
1333 break;
1334 case KVM_REG_PPC_UAMOR:
1335 *val = get_reg_val(id, vcpu->arch.uamor);
1336 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001337 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001338 i = id - KVM_REG_PPC_MMCR0;
1339 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1340 break;
1341 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1342 i = id - KVM_REG_PPC_PMC1;
1343 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001344 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001345 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1346 i = id - KVM_REG_PPC_SPMC1;
1347 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1348 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001349 case KVM_REG_PPC_SIAR:
1350 *val = get_reg_val(id, vcpu->arch.siar);
1351 break;
1352 case KVM_REG_PPC_SDAR:
1353 *val = get_reg_val(id, vcpu->arch.sdar);
1354 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001355 case KVM_REG_PPC_SIER:
1356 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001357 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001358 case KVM_REG_PPC_IAMR:
1359 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001360 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001361 case KVM_REG_PPC_PSPB:
1362 *val = get_reg_val(id, vcpu->arch.pspb);
1363 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001364 case KVM_REG_PPC_DPDES:
1365 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1366 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001367 case KVM_REG_PPC_VTB:
1368 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
1369 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001370 case KVM_REG_PPC_DAWR:
1371 *val = get_reg_val(id, vcpu->arch.dawr);
1372 break;
1373 case KVM_REG_PPC_DAWRX:
1374 *val = get_reg_val(id, vcpu->arch.dawrx);
1375 break;
1376 case KVM_REG_PPC_CIABR:
1377 *val = get_reg_val(id, vcpu->arch.ciabr);
1378 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001379 case KVM_REG_PPC_CSIGR:
1380 *val = get_reg_val(id, vcpu->arch.csigr);
1381 break;
1382 case KVM_REG_PPC_TACR:
1383 *val = get_reg_val(id, vcpu->arch.tacr);
1384 break;
1385 case KVM_REG_PPC_TCSCR:
1386 *val = get_reg_val(id, vcpu->arch.tcscr);
1387 break;
1388 case KVM_REG_PPC_PID:
1389 *val = get_reg_val(id, vcpu->arch.pid);
1390 break;
1391 case KVM_REG_PPC_ACOP:
1392 *val = get_reg_val(id, vcpu->arch.acop);
1393 break;
1394 case KVM_REG_PPC_WORT:
1395 *val = get_reg_val(id, vcpu->arch.wort);
1396 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001397 case KVM_REG_PPC_TIDR:
1398 *val = get_reg_val(id, vcpu->arch.tid);
1399 break;
1400 case KVM_REG_PPC_PSSCR:
1401 *val = get_reg_val(id, vcpu->arch.psscr);
1402 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001403 case KVM_REG_PPC_VPA_ADDR:
1404 spin_lock(&vcpu->arch.vpa_update_lock);
1405 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1406 spin_unlock(&vcpu->arch.vpa_update_lock);
1407 break;
1408 case KVM_REG_PPC_VPA_SLB:
1409 spin_lock(&vcpu->arch.vpa_update_lock);
1410 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1411 val->vpaval.length = vcpu->arch.slb_shadow.len;
1412 spin_unlock(&vcpu->arch.vpa_update_lock);
1413 break;
1414 case KVM_REG_PPC_VPA_DTL:
1415 spin_lock(&vcpu->arch.vpa_update_lock);
1416 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1417 val->vpaval.length = vcpu->arch.dtl.len;
1418 spin_unlock(&vcpu->arch.vpa_update_lock);
1419 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001420 case KVM_REG_PPC_TB_OFFSET:
1421 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1422 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001423 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001424 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001425 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1426 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001427 case KVM_REG_PPC_PPR:
1428 *val = get_reg_val(id, vcpu->arch.ppr);
1429 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001430#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1431 case KVM_REG_PPC_TFHAR:
1432 *val = get_reg_val(id, vcpu->arch.tfhar);
1433 break;
1434 case KVM_REG_PPC_TFIAR:
1435 *val = get_reg_val(id, vcpu->arch.tfiar);
1436 break;
1437 case KVM_REG_PPC_TEXASR:
1438 *val = get_reg_val(id, vcpu->arch.texasr);
1439 break;
1440 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1441 i = id - KVM_REG_PPC_TM_GPR0;
1442 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1443 break;
1444 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1445 {
1446 int j;
1447 i = id - KVM_REG_PPC_TM_VSR0;
1448 if (i < 32)
1449 for (j = 0; j < TS_FPRWIDTH; j++)
1450 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1451 else {
1452 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1453 val->vval = vcpu->arch.vr_tm.vr[i-32];
1454 else
1455 r = -ENXIO;
1456 }
1457 break;
1458 }
1459 case KVM_REG_PPC_TM_CR:
1460 *val = get_reg_val(id, vcpu->arch.cr_tm);
1461 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001462 case KVM_REG_PPC_TM_XER:
1463 *val = get_reg_val(id, vcpu->arch.xer_tm);
1464 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001465 case KVM_REG_PPC_TM_LR:
1466 *val = get_reg_val(id, vcpu->arch.lr_tm);
1467 break;
1468 case KVM_REG_PPC_TM_CTR:
1469 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1470 break;
1471 case KVM_REG_PPC_TM_FPSCR:
1472 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1473 break;
1474 case KVM_REG_PPC_TM_AMR:
1475 *val = get_reg_val(id, vcpu->arch.amr_tm);
1476 break;
1477 case KVM_REG_PPC_TM_PPR:
1478 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1479 break;
1480 case KVM_REG_PPC_TM_VRSAVE:
1481 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1482 break;
1483 case KVM_REG_PPC_TM_VSCR:
1484 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1485 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1486 else
1487 r = -ENXIO;
1488 break;
1489 case KVM_REG_PPC_TM_DSCR:
1490 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1491 break;
1492 case KVM_REG_PPC_TM_TAR:
1493 *val = get_reg_val(id, vcpu->arch.tar_tm);
1494 break;
1495#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001496 case KVM_REG_PPC_ARCH_COMPAT:
1497 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1498 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001499 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001500 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001501 break;
1502 }
1503
1504 return r;
1505}
1506
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301507static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1508 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001509{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001510 int r = 0;
1511 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001512 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001513
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001514 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001515 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001516 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001517 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001518 r = -EINVAL;
1519 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001520 case KVM_REG_PPC_DABR:
1521 vcpu->arch.dabr = set_reg_val(id, *val);
1522 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001523 case KVM_REG_PPC_DABRX:
1524 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1525 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001526 case KVM_REG_PPC_DSCR:
1527 vcpu->arch.dscr = set_reg_val(id, *val);
1528 break;
1529 case KVM_REG_PPC_PURR:
1530 vcpu->arch.purr = set_reg_val(id, *val);
1531 break;
1532 case KVM_REG_PPC_SPURR:
1533 vcpu->arch.spurr = set_reg_val(id, *val);
1534 break;
1535 case KVM_REG_PPC_AMR:
1536 vcpu->arch.amr = set_reg_val(id, *val);
1537 break;
1538 case KVM_REG_PPC_UAMOR:
1539 vcpu->arch.uamor = set_reg_val(id, *val);
1540 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001541 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001542 i = id - KVM_REG_PPC_MMCR0;
1543 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1544 break;
1545 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1546 i = id - KVM_REG_PPC_PMC1;
1547 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1548 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001549 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1550 i = id - KVM_REG_PPC_SPMC1;
1551 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1552 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001553 case KVM_REG_PPC_SIAR:
1554 vcpu->arch.siar = set_reg_val(id, *val);
1555 break;
1556 case KVM_REG_PPC_SDAR:
1557 vcpu->arch.sdar = set_reg_val(id, *val);
1558 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001559 case KVM_REG_PPC_SIER:
1560 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001561 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001562 case KVM_REG_PPC_IAMR:
1563 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001564 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001565 case KVM_REG_PPC_PSPB:
1566 vcpu->arch.pspb = set_reg_val(id, *val);
1567 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001568 case KVM_REG_PPC_DPDES:
1569 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1570 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001571 case KVM_REG_PPC_VTB:
1572 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
1573 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001574 case KVM_REG_PPC_DAWR:
1575 vcpu->arch.dawr = set_reg_val(id, *val);
1576 break;
1577 case KVM_REG_PPC_DAWRX:
1578 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1579 break;
1580 case KVM_REG_PPC_CIABR:
1581 vcpu->arch.ciabr = set_reg_val(id, *val);
1582 /* Don't allow setting breakpoints in hypervisor code */
1583 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1584 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1585 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001586 case KVM_REG_PPC_CSIGR:
1587 vcpu->arch.csigr = set_reg_val(id, *val);
1588 break;
1589 case KVM_REG_PPC_TACR:
1590 vcpu->arch.tacr = set_reg_val(id, *val);
1591 break;
1592 case KVM_REG_PPC_TCSCR:
1593 vcpu->arch.tcscr = set_reg_val(id, *val);
1594 break;
1595 case KVM_REG_PPC_PID:
1596 vcpu->arch.pid = set_reg_val(id, *val);
1597 break;
1598 case KVM_REG_PPC_ACOP:
1599 vcpu->arch.acop = set_reg_val(id, *val);
1600 break;
1601 case KVM_REG_PPC_WORT:
1602 vcpu->arch.wort = set_reg_val(id, *val);
1603 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001604 case KVM_REG_PPC_TIDR:
1605 vcpu->arch.tid = set_reg_val(id, *val);
1606 break;
1607 case KVM_REG_PPC_PSSCR:
1608 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
1609 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001610 case KVM_REG_PPC_VPA_ADDR:
1611 addr = set_reg_val(id, *val);
1612 r = -EINVAL;
1613 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1614 vcpu->arch.dtl.next_gpa))
1615 break;
1616 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1617 break;
1618 case KVM_REG_PPC_VPA_SLB:
1619 addr = val->vpaval.addr;
1620 len = val->vpaval.length;
1621 r = -EINVAL;
1622 if (addr && !vcpu->arch.vpa.next_gpa)
1623 break;
1624 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1625 break;
1626 case KVM_REG_PPC_VPA_DTL:
1627 addr = val->vpaval.addr;
1628 len = val->vpaval.length;
1629 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001630 if (addr && (len < sizeof(struct dtl_entry) ||
1631 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001632 break;
1633 len -= len % sizeof(struct dtl_entry);
1634 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1635 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001636 case KVM_REG_PPC_TB_OFFSET:
Paul Mackerras3d3efb62017-06-06 14:35:30 +10001637 /*
1638 * POWER9 DD1 has an erratum where writing TBU40 causes
1639 * the timebase to lose ticks. So we don't let the
1640 * timebase offset be changed on P9 DD1. (It is
1641 * initialized to zero.)
1642 */
1643 if (cpu_has_feature(CPU_FTR_POWER9_DD1))
1644 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001645 /* round up to multiple of 2^24 */
1646 vcpu->arch.vcore->tb_offset =
1647 ALIGN(set_reg_val(id, *val), 1UL << 24);
1648 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001649 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001650 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1651 break;
1652 case KVM_REG_PPC_LPCR_64:
1653 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001654 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001655 case KVM_REG_PPC_PPR:
1656 vcpu->arch.ppr = set_reg_val(id, *val);
1657 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001658#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1659 case KVM_REG_PPC_TFHAR:
1660 vcpu->arch.tfhar = set_reg_val(id, *val);
1661 break;
1662 case KVM_REG_PPC_TFIAR:
1663 vcpu->arch.tfiar = set_reg_val(id, *val);
1664 break;
1665 case KVM_REG_PPC_TEXASR:
1666 vcpu->arch.texasr = set_reg_val(id, *val);
1667 break;
1668 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1669 i = id - KVM_REG_PPC_TM_GPR0;
1670 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1671 break;
1672 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1673 {
1674 int j;
1675 i = id - KVM_REG_PPC_TM_VSR0;
1676 if (i < 32)
1677 for (j = 0; j < TS_FPRWIDTH; j++)
1678 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1679 else
1680 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1681 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1682 else
1683 r = -ENXIO;
1684 break;
1685 }
1686 case KVM_REG_PPC_TM_CR:
1687 vcpu->arch.cr_tm = set_reg_val(id, *val);
1688 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001689 case KVM_REG_PPC_TM_XER:
1690 vcpu->arch.xer_tm = set_reg_val(id, *val);
1691 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001692 case KVM_REG_PPC_TM_LR:
1693 vcpu->arch.lr_tm = set_reg_val(id, *val);
1694 break;
1695 case KVM_REG_PPC_TM_CTR:
1696 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1697 break;
1698 case KVM_REG_PPC_TM_FPSCR:
1699 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1700 break;
1701 case KVM_REG_PPC_TM_AMR:
1702 vcpu->arch.amr_tm = set_reg_val(id, *val);
1703 break;
1704 case KVM_REG_PPC_TM_PPR:
1705 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1706 break;
1707 case KVM_REG_PPC_TM_VRSAVE:
1708 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1709 break;
1710 case KVM_REG_PPC_TM_VSCR:
1711 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1712 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1713 else
1714 r = - ENXIO;
1715 break;
1716 case KVM_REG_PPC_TM_DSCR:
1717 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1718 break;
1719 case KVM_REG_PPC_TM_TAR:
1720 vcpu->arch.tar_tm = set_reg_val(id, *val);
1721 break;
1722#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001723 case KVM_REG_PPC_ARCH_COMPAT:
1724 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1725 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001726 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001727 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001728 break;
1729 }
1730
1731 return r;
1732}
1733
Paul Mackerras45c940b2016-11-18 17:43:30 +11001734/*
1735 * On POWER9, threads are independent and can be in different partitions.
1736 * Therefore we consider each thread to be a subcore.
1737 * There is a restriction that all threads have to be in the same
1738 * MMU mode (radix or HPT), unfortunately, but since we only support
1739 * HPT guests on a HPT host so far, that isn't an impediment yet.
1740 */
Paul Mackerras516f7892017-10-16 16:11:57 +11001741static int threads_per_vcore(struct kvm *kvm)
Paul Mackerras45c940b2016-11-18 17:43:30 +11001742{
Paul Mackerras516f7892017-10-16 16:11:57 +11001743 if (kvm->arch.threads_indep)
Paul Mackerras45c940b2016-11-18 17:43:30 +11001744 return 1;
1745 return threads_per_subcore;
1746}
1747
Stewart Smithde9bdd12014-07-18 14:18:42 +10001748static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1749{
1750 struct kvmppc_vcore *vcore;
1751
1752 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1753
1754 if (vcore == NULL)
1755 return NULL;
1756
Stewart Smithde9bdd12014-07-18 14:18:42 +10001757 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001758 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001759 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001760 vcore->preempt_tb = TB_NIL;
1761 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras3c313522017-02-06 13:24:41 +11001762 vcore->first_vcpuid = core * kvm->arch.smt_mode;
Stewart Smithde9bdd12014-07-18 14:18:42 +10001763 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001764 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001765
1766 return vcore;
1767}
1768
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001769#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1770static struct debugfs_timings_element {
1771 const char *name;
1772 size_t offset;
1773} timings[] = {
1774 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1775 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1776 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1777 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1778 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1779};
1780
Thomas Meyer4bb817ed2017-09-03 14:19:31 +02001781#define N_TIMINGS (ARRAY_SIZE(timings))
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001782
1783struct debugfs_timings_state {
1784 struct kvm_vcpu *vcpu;
1785 unsigned int buflen;
1786 char buf[N_TIMINGS * 100];
1787};
1788
1789static int debugfs_timings_open(struct inode *inode, struct file *file)
1790{
1791 struct kvm_vcpu *vcpu = inode->i_private;
1792 struct debugfs_timings_state *p;
1793
1794 p = kzalloc(sizeof(*p), GFP_KERNEL);
1795 if (!p)
1796 return -ENOMEM;
1797
1798 kvm_get_kvm(vcpu->kvm);
1799 p->vcpu = vcpu;
1800 file->private_data = p;
1801
1802 return nonseekable_open(inode, file);
1803}
1804
1805static int debugfs_timings_release(struct inode *inode, struct file *file)
1806{
1807 struct debugfs_timings_state *p = file->private_data;
1808
1809 kvm_put_kvm(p->vcpu->kvm);
1810 kfree(p);
1811 return 0;
1812}
1813
1814static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1815 size_t len, loff_t *ppos)
1816{
1817 struct debugfs_timings_state *p = file->private_data;
1818 struct kvm_vcpu *vcpu = p->vcpu;
1819 char *s, *buf_end;
1820 struct kvmhv_tb_accumulator tb;
1821 u64 count;
1822 loff_t pos;
1823 ssize_t n;
1824 int i, loops;
1825 bool ok;
1826
1827 if (!p->buflen) {
1828 s = p->buf;
1829 buf_end = s + sizeof(p->buf);
1830 for (i = 0; i < N_TIMINGS; ++i) {
1831 struct kvmhv_tb_accumulator *acc;
1832
1833 acc = (struct kvmhv_tb_accumulator *)
1834 ((unsigned long)vcpu + timings[i].offset);
1835 ok = false;
1836 for (loops = 0; loops < 1000; ++loops) {
1837 count = acc->seqcount;
1838 if (!(count & 1)) {
1839 smp_rmb();
1840 tb = *acc;
1841 smp_rmb();
1842 if (count == acc->seqcount) {
1843 ok = true;
1844 break;
1845 }
1846 }
1847 udelay(1);
1848 }
1849 if (!ok)
1850 snprintf(s, buf_end - s, "%s: stuck\n",
1851 timings[i].name);
1852 else
1853 snprintf(s, buf_end - s,
1854 "%s: %llu %llu %llu %llu\n",
1855 timings[i].name, count / 2,
1856 tb_to_ns(tb.tb_total),
1857 tb_to_ns(tb.tb_min),
1858 tb_to_ns(tb.tb_max));
1859 s += strlen(s);
1860 }
1861 p->buflen = s - p->buf;
1862 }
1863
1864 pos = *ppos;
1865 if (pos >= p->buflen)
1866 return 0;
1867 if (len > p->buflen - pos)
1868 len = p->buflen - pos;
1869 n = copy_to_user(buf, p->buf + pos, len);
1870 if (n) {
1871 if (n == len)
1872 return -EFAULT;
1873 len -= n;
1874 }
1875 *ppos = pos + len;
1876 return len;
1877}
1878
1879static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1880 size_t len, loff_t *ppos)
1881{
1882 return -EACCES;
1883}
1884
1885static const struct file_operations debugfs_timings_ops = {
1886 .owner = THIS_MODULE,
1887 .open = debugfs_timings_open,
1888 .release = debugfs_timings_release,
1889 .read = debugfs_timings_read,
1890 .write = debugfs_timings_write,
1891 .llseek = generic_file_llseek,
1892};
1893
1894/* Create a debugfs directory for the vcpu */
1895static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1896{
1897 char buf[16];
1898 struct kvm *kvm = vcpu->kvm;
1899
1900 snprintf(buf, sizeof(buf), "vcpu%u", id);
1901 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1902 return;
1903 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1904 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1905 return;
1906 vcpu->arch.debugfs_timings =
1907 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1908 vcpu, &debugfs_timings_ops);
1909}
1910
1911#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1912static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1913{
1914}
1915#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1916
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301917static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1918 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001919{
1920 struct kvm_vcpu *vcpu;
Paul Mackerras3c313522017-02-06 13:24:41 +11001921 int err;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001922 int core;
1923 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001924
Paul Mackerras371fefd2011-06-29 00:23:08 +00001925 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001926 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001927 if (!vcpu)
1928 goto out;
1929
1930 err = kvm_vcpu_init(vcpu, kvm, id);
1931 if (err)
1932 goto free_vcpu;
1933
1934 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001935#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1936 /*
1937 * The shared struct is never shared on HV,
1938 * so we can always use host endianness
1939 */
1940#ifdef __BIG_ENDIAN__
1941 vcpu->arch.shared_big_endian = true;
1942#else
1943 vcpu->arch.shared_big_endian = false;
1944#endif
1945#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001946 vcpu->arch.mmcr[0] = MMCR0_FC;
1947 vcpu->arch.ctrl = CTRL_RUNLATCH;
1948 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301949 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001950 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001951 spin_lock_init(&vcpu->arch.tbacct_lock);
1952 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001953 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001954
Paul Mackerras769377f2017-02-15 14:30:17 +11001955 /*
1956 * Set the default HFSCR for the guest from the host value.
1957 * This value is only used on POWER9.
1958 * On POWER9 DD1, TM doesn't work, so we make sure to
1959 * prevent the guest from using it.
Paul Mackerras57900692017-05-16 16:41:20 +10001960 * On POWER9, we want to virtualize the doorbell facility, so we
1961 * turn off the HFSCR bit, which causes those instructions to trap.
Paul Mackerras769377f2017-02-15 14:30:17 +11001962 */
1963 vcpu->arch.hfscr = mfspr(SPRN_HFSCR);
1964 if (!cpu_has_feature(CPU_FTR_TM))
1965 vcpu->arch.hfscr &= ~HFSCR_TM;
Paul Mackerras57900692017-05-16 16:41:20 +10001966 if (cpu_has_feature(CPU_FTR_ARCH_300))
1967 vcpu->arch.hfscr &= ~HFSCR_MSGP;
Paul Mackerras769377f2017-02-15 14:30:17 +11001968
Paul Mackerrasde56a942011-06-29 00:21:34 +00001969 kvmppc_mmu_book3s_hv_init(vcpu);
1970
Paul Mackerras8455d792012-10-15 01:17:42 +00001971 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001972
1973 init_waitqueue_head(&vcpu->arch.cpu_run);
1974
1975 mutex_lock(&kvm->lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11001976 vcore = NULL;
1977 err = -EINVAL;
1978 core = id / kvm->arch.smt_mode;
1979 if (core < KVM_MAX_VCORES) {
1980 vcore = kvm->arch.vcores[core];
1981 if (!vcore) {
1982 err = -ENOMEM;
1983 vcore = kvmppc_vcore_create(kvm, core);
1984 kvm->arch.vcores[core] = vcore;
1985 kvm->arch.online_vcores++;
1986 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001987 }
1988 mutex_unlock(&kvm->lock);
1989
1990 if (!vcore)
1991 goto free_vcpu;
1992
1993 spin_lock(&vcore->lock);
1994 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001995 spin_unlock(&vcore->lock);
1996 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001997 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001998 vcpu->arch.thread_cpu = -1;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11001999 vcpu->arch.prev_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002000
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002001 vcpu->arch.cpu_type = KVM_CPU_3S_64;
2002 kvmppc_sanity_check(vcpu);
2003
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002004 debugfs_vcpu_init(vcpu, id);
2005
Paul Mackerrasde56a942011-06-29 00:21:34 +00002006 return vcpu;
2007
2008free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02002009 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002010out:
2011 return ERR_PTR(err);
2012}
2013
Paul Mackerras3c313522017-02-06 13:24:41 +11002014static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
2015 unsigned long flags)
2016{
2017 int err;
Paul Mackerras57900692017-05-16 16:41:20 +10002018 int esmt = 0;
Paul Mackerras3c313522017-02-06 13:24:41 +11002019
2020 if (flags)
2021 return -EINVAL;
2022 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
2023 return -EINVAL;
2024 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
2025 /*
2026 * On POWER8 (or POWER7), the threading mode is "strict",
2027 * so we pack smt_mode vcpus per vcore.
2028 */
2029 if (smt_mode > threads_per_subcore)
2030 return -EINVAL;
2031 } else {
2032 /*
2033 * On POWER9, the threading mode is "loose",
2034 * so each vcpu gets its own vcore.
2035 */
Paul Mackerras57900692017-05-16 16:41:20 +10002036 esmt = smt_mode;
Paul Mackerras3c313522017-02-06 13:24:41 +11002037 smt_mode = 1;
2038 }
2039 mutex_lock(&kvm->lock);
2040 err = -EBUSY;
2041 if (!kvm->arch.online_vcores) {
2042 kvm->arch.smt_mode = smt_mode;
Paul Mackerras57900692017-05-16 16:41:20 +10002043 kvm->arch.emul_smt_mode = esmt;
Paul Mackerras3c313522017-02-06 13:24:41 +11002044 err = 0;
2045 }
2046 mutex_unlock(&kvm->lock);
2047
2048 return err;
2049}
2050
Paul Mackerrasc35635e2013-04-18 19:51:04 +00002051static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
2052{
2053 if (vpa->pinned_addr)
2054 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
2055 vpa->dirty);
2056}
2057
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302058static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002059{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002060 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00002061 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
2062 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
2063 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002064 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002065 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02002066 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002067}
2068
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302069static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
2070{
2071 /* Indicate we want to get back into the guest */
2072 return 1;
2073}
2074
Paul Mackerras19ccb762011-07-23 17:42:46 +10002075static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002076{
Paul Mackerras19ccb762011-07-23 17:42:46 +10002077 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002078
Paul Mackerras19ccb762011-07-23 17:42:46 +10002079 now = get_tb();
2080 if (now > vcpu->arch.dec_expires) {
2081 /* decrementer has already gone negative */
2082 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002083 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002084 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002085 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002086 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
2087 / tb_ticks_per_sec;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002088 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002089 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002090}
2091
Paul Mackerras19ccb762011-07-23 17:42:46 +10002092static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002093{
Paul Mackerras19ccb762011-07-23 17:42:46 +10002094 vcpu->arch.ceded = 0;
2095 if (vcpu->arch.timer_running) {
2096 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
2097 vcpu->arch.timer_running = 0;
2098 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002099}
2100
Paul Mackerras8b24e692017-06-26 15:45:51 +10002101extern int __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002102
2103static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
2104 struct kvm_vcpu *vcpu)
2105{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002106 u64 now;
2107
Paul Mackerras371fefd2011-06-29 00:23:08 +00002108 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2109 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11002110 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002111 now = mftb();
2112 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
2113 vcpu->arch.stolen_logged;
2114 vcpu->arch.busy_preempt = now;
2115 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11002116 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002117 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002118 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002119}
2120
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002121static int kvmppc_grab_hwthread(int cpu)
2122{
2123 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10002124 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002125
2126 tpaca = &paca[cpu];
2127
2128 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00002129 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002130 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002131 tpaca->kvm_hstate.napping = 0;
2132 smp_wmb();
2133 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002134
2135 /*
2136 * If the thread is already executing in the kernel (e.g. handling
2137 * a stray interrupt), wait for it to get back to nap mode.
2138 * The smp_mb() is to ensure that our setting of hwthread_req
2139 * is visible before we look at hwthread_state, so if this
2140 * races with the code at system_reset_pSeries and the thread
2141 * misses our setting of hwthread_req, we are sure to see its
2142 * setting of hwthread_state, and vice versa.
2143 */
2144 smp_mb();
2145 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
2146 if (--timeout <= 0) {
2147 pr_err("KVM: couldn't grab cpu %d\n", cpu);
2148 return -EBUSY;
2149 }
2150 udelay(1);
2151 }
2152 return 0;
2153}
2154
2155static void kvmppc_release_hwthread(int cpu)
2156{
2157 struct paca_struct *tpaca;
2158
2159 tpaca = &paca[cpu];
Paul Mackerras31a4d442017-10-19 15:14:20 +11002160 tpaca->kvm_hstate.hwthread_req = 0;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002161 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002162 tpaca->kvm_hstate.kvm_vcore = NULL;
2163 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002164}
2165
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002166static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
2167{
2168 int i;
2169
2170 cpu = cpu_first_thread_sibling(cpu);
2171 cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
2172 /*
2173 * Make sure setting of bit in need_tlb_flush precedes
2174 * testing of cpu_in_guest bits. The matching barrier on
2175 * the other side is the first smp_mb() in kvmppc_run_core().
2176 */
2177 smp_mb();
2178 for (i = 0; i < threads_per_core; ++i)
2179 if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest))
2180 smp_call_function_single(cpu + i, do_nothing, NULL, 1);
2181}
2182
Paul Mackerras8b24e692017-06-26 15:45:51 +10002183static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
2184{
2185 struct kvm *kvm = vcpu->kvm;
2186
2187 /*
2188 * With radix, the guest can do TLB invalidations itself,
2189 * and it could choose to use the local form (tlbiel) if
2190 * it is invalidating a translation that has only ever been
2191 * used on one vcpu. However, that doesn't mean it has
2192 * only ever been used on one physical cpu, since vcpus
2193 * can move around between pcpus. To cope with this, when
2194 * a vcpu moves from one pcpu to another, we need to tell
2195 * any vcpus running on the same core as this vcpu previously
2196 * ran to flush the TLB. The TLB is shared between threads,
2197 * so we use a single bit in .need_tlb_flush for all 4 threads.
2198 */
2199 if (vcpu->arch.prev_cpu != pcpu) {
2200 if (vcpu->arch.prev_cpu >= 0 &&
2201 cpu_first_thread_sibling(vcpu->arch.prev_cpu) !=
2202 cpu_first_thread_sibling(pcpu))
2203 radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu);
2204 vcpu->arch.prev_cpu = pcpu;
2205 }
2206}
2207
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002208static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002209{
2210 int cpu;
2211 struct paca_struct *tpaca;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002212 struct kvm *kvm = vc->kvm;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002213
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002214 cpu = vc->pcpu;
2215 if (vcpu) {
2216 if (vcpu->arch.timer_running) {
2217 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
2218 vcpu->arch.timer_running = 0;
2219 }
2220 cpu += vcpu->arch.ptid;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002221 vcpu->cpu = vc->pcpu;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002222 vcpu->arch.thread_cpu = cpu;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002223 cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002224 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002225 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002226 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002227 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10002228 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002229 smp_wmb();
Paul Mackerras898b25b2017-06-22 15:08:42 +10002230 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002231 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11002232 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002233}
2234
Paul Mackerras516f7892017-10-16 16:11:57 +11002235static void kvmppc_wait_for_nap(int n_threads)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002236{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002237 int cpu = smp_processor_id();
2238 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002239
Paul Mackerras45c940b2016-11-18 17:43:30 +11002240 if (n_threads <= 1)
2241 return;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002242 for (loops = 0; loops < 1000000; ++loops) {
2243 /*
2244 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002245 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002246 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002247 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002248 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002249 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002250 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002251 break;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002252 if (i == n_threads) {
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002253 HMT_medium();
2254 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002255 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002256 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00002257 }
2258 HMT_medium();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002259 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002260 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002261 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002262}
2263
2264/*
2265 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00002266 * this core are off-line. Then grab the threads so they can't
2267 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00002268 */
2269static int on_primary_thread(void)
2270{
2271 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10002272 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002273
Michael Ellerman3102f782014-05-23 18:15:29 +10002274 /* Are we on a primary subcore? */
2275 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00002276 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10002277
2278 thr = 0;
2279 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002280 if (cpu_online(cpu + thr))
2281 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00002282
2283 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10002284 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002285 if (kvmppc_grab_hwthread(cpu + thr)) {
2286 /* Couldn't grab one; let the others go */
2287 do {
2288 kvmppc_release_hwthread(cpu + thr);
2289 } while (--thr > 0);
2290 return 0;
2291 }
2292 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002293 return 1;
2294}
2295
Paul Mackerrasec257162015-06-24 21:18:03 +10002296/*
2297 * A list of virtual cores for each physical CPU.
2298 * These are vcores that could run but their runner VCPU tasks are
2299 * (or may be) preempted.
2300 */
2301struct preempted_vcore_list {
2302 struct list_head list;
2303 spinlock_t lock;
2304};
2305
2306static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
2307
2308static void init_vcore_lists(void)
2309{
2310 int cpu;
2311
2312 for_each_possible_cpu(cpu) {
2313 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
2314 spin_lock_init(&lp->lock);
2315 INIT_LIST_HEAD(&lp->list);
2316 }
2317}
2318
2319static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
2320{
2321 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2322
2323 vc->vcore_state = VCORE_PREEMPT;
2324 vc->pcpu = smp_processor_id();
Paul Mackerras516f7892017-10-16 16:11:57 +11002325 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002326 spin_lock(&lp->lock);
2327 list_add_tail(&vc->preempt_list, &lp->list);
2328 spin_unlock(&lp->lock);
2329 }
2330
2331 /* Start accumulating stolen time */
2332 kvmppc_core_start_stolen(vc);
2333}
2334
2335static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
2336{
Paul Mackerras402813f2015-07-16 17:11:13 +10002337 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10002338
2339 kvmppc_core_end_stolen(vc);
2340 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10002341 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002342 spin_lock(&lp->lock);
2343 list_del_init(&vc->preempt_list);
2344 spin_unlock(&lp->lock);
2345 }
2346 vc->vcore_state = VCORE_INACTIVE;
2347}
2348
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002349/*
2350 * This stores information about the virtual cores currently
2351 * assigned to a physical core.
2352 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002353struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002354 int n_subcores;
2355 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002356 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002357 int subcore_threads[MAX_SUBCORES];
Paul Mackerras898b25b2017-06-22 15:08:42 +10002358 struct kvmppc_vcore *vc[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002359};
2360
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002361/*
2362 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
Paul Mackerras516f7892017-10-16 16:11:57 +11002363 * respectively in 2-way micro-threading (split-core) mode on POWER8.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002364 */
2365static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2366
Paul Mackerrasec257162015-06-24 21:18:03 +10002367static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2368{
2369 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002370 cip->n_subcores = 1;
2371 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002372 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002373 cip->subcore_threads[0] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002374 cip->vc[0] = vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002375}
2376
2377static bool subcore_config_ok(int n_subcores, int n_threads)
2378{
Paul Mackerras516f7892017-10-16 16:11:57 +11002379 /*
2380 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way split-core
2381 * mode, with one thread per subcore.
2382 */
2383 if (cpu_has_feature(CPU_FTR_ARCH_300))
2384 return n_subcores <= 4 && n_threads == 1;
2385
2386 /* On POWER8, can only dynamically split if unsplit to begin with */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002387 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2388 return false;
2389 if (n_subcores > MAX_SUBCORES)
2390 return false;
2391 if (n_subcores > 1) {
2392 if (!(dynamic_mt_modes & 2))
2393 n_subcores = 4;
2394 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2395 return false;
2396 }
2397
2398 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002399}
2400
Paul Mackerras898b25b2017-06-22 15:08:42 +10002401static void init_vcore_to_run(struct kvmppc_vcore *vc)
Paul Mackerrasec257162015-06-24 21:18:03 +10002402{
Paul Mackerrasec257162015-06-24 21:18:03 +10002403 vc->entry_exit_map = 0;
2404 vc->in_guest = 0;
2405 vc->napping_threads = 0;
2406 vc->conferring_threads = 0;
2407}
2408
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002409static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2410{
2411 int n_threads = vc->num_threads;
2412 int sub;
2413
2414 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2415 return false;
2416
2417 if (n_threads < cip->max_subcore_threads)
2418 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002419 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002420 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002421 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002422
2423 sub = cip->n_subcores;
2424 ++cip->n_subcores;
2425 cip->total_threads += vc->num_threads;
2426 cip->subcore_threads[sub] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002427 cip->vc[sub] = vc;
2428 init_vcore_to_run(vc);
2429 list_del_init(&vc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002430
2431 return true;
2432}
2433
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002434/*
2435 * Work out whether it is possible to piggyback the execution of
2436 * vcore *pvc onto the execution of the other vcores described in *cip.
2437 */
2438static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2439 int target_threads)
2440{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002441 if (cip->total_threads + pvc->num_threads > target_threads)
2442 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002443
Paul Mackerrasb0090312016-09-15 16:27:41 +10002444 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002445}
2446
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002447static void prepare_threads(struct kvmppc_vcore *vc)
2448{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002449 int i;
2450 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002451
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002452 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002453 if (signal_pending(vcpu->arch.run_task))
2454 vcpu->arch.ret = -EINTR;
Paul Mackerras18c36402017-09-13 16:00:10 +10002455 else if (kvm_is_radix(vc->kvm) != radix_enabled())
2456 /* can't actually run HPT guest on radix host yet... */
2457 vcpu->arch.ret = -EINVAL;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002458 else if (vcpu->arch.vpa.update_pending ||
2459 vcpu->arch.slb_shadow.update_pending ||
2460 vcpu->arch.dtl.update_pending)
2461 vcpu->arch.ret = RESUME_GUEST;
2462 else
2463 continue;
2464 kvmppc_remove_runnable(vc, vcpu);
2465 wake_up(&vcpu->arch.cpu_run);
2466 }
2467}
2468
Paul Mackerrasec257162015-06-24 21:18:03 +10002469static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002470{
Paul Mackerrasec257162015-06-24 21:18:03 +10002471 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2472 struct kvmppc_vcore *pvc, *vcnext;
2473
2474 spin_lock(&lp->lock);
2475 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2476 if (!spin_trylock(&pvc->lock))
2477 continue;
2478 prepare_threads(pvc);
2479 if (!pvc->n_runnable) {
2480 list_del_init(&pvc->preempt_list);
2481 if (pvc->runner == NULL) {
2482 pvc->vcore_state = VCORE_INACTIVE;
2483 kvmppc_core_end_stolen(pvc);
2484 }
2485 spin_unlock(&pvc->lock);
2486 continue;
2487 }
2488 if (!can_piggyback(pvc, cip, target_threads)) {
2489 spin_unlock(&pvc->lock);
2490 continue;
2491 }
2492 kvmppc_core_end_stolen(pvc);
2493 pvc->vcore_state = VCORE_PIGGYBACK;
2494 if (cip->total_threads >= target_threads)
2495 break;
2496 }
2497 spin_unlock(&lp->lock);
2498}
2499
Paul Mackerras8b24e692017-06-26 15:45:51 +10002500static bool recheck_signals(struct core_info *cip)
2501{
2502 int sub, i;
2503 struct kvm_vcpu *vcpu;
2504
2505 for (sub = 0; sub < cip->n_subcores; ++sub)
2506 for_each_runnable_thread(i, vcpu, cip->vc[sub])
2507 if (signal_pending(vcpu->arch.run_task))
2508 return true;
2509 return false;
2510}
2511
Paul Mackerrasec257162015-06-24 21:18:03 +10002512static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2513{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002514 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002515 u64 now;
2516 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002517 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002518
Paul Mackerrasec257162015-06-24 21:18:03 +10002519 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002520 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002521 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002522 /* cancel pending dec exception if dec is positive */
2523 if (now < vcpu->arch.dec_expires &&
2524 kvmppc_core_pending_dec(vcpu))
2525 kvmppc_core_dequeue_dec(vcpu);
2526
2527 trace_kvm_guest_exit(vcpu);
2528
2529 ret = RESUME_GUEST;
2530 if (vcpu->arch.trap)
2531 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2532 vcpu->arch.run_task);
2533
2534 vcpu->arch.ret = ret;
2535 vcpu->arch.trap = 0;
2536
Paul Mackerrasec257162015-06-24 21:18:03 +10002537 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2538 if (vcpu->arch.pending_exceptions)
2539 kvmppc_core_prepare_to_enter(vcpu);
2540 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002541 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002542 else
2543 ++still_running;
2544 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002545 kvmppc_remove_runnable(vc, vcpu);
2546 wake_up(&vcpu->arch.cpu_run);
2547 }
2548 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002549 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002550 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002551 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002552 } else if (vc->runner) {
2553 vc->vcore_state = VCORE_PREEMPT;
2554 kvmppc_core_start_stolen(vc);
2555 } else {
2556 vc->vcore_state = VCORE_INACTIVE;
2557 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002558 if (vc->n_runnable > 0 && vc->runner == NULL) {
2559 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002560 i = -1;
2561 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10002562 wake_up(&vcpu->arch.cpu_run);
2563 }
2564 }
2565 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002566}
2567
Paul Mackerras371fefd2011-06-29 00:23:08 +00002568/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002569 * Clear core from the list of active host cores as we are about to
2570 * enter the guest. Only do this if it is the primary thread of the
2571 * core (not if a subcore) that is entering the guest.
2572 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002573static inline int kvmppc_clear_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002574{
2575 int core;
2576
2577 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002578 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002579 /*
2580 * Memory barrier can be omitted here as we will do a smp_wmb()
2581 * later in kvmppc_start_thread and we need ensure that state is
2582 * visible to other CPUs only after we enter guest.
2583 */
2584 core = cpu >> threads_shift;
2585 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002586 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002587}
2588
2589/*
2590 * Advertise this core as an active host core since we exited the guest
2591 * Only need to do this if it is the primary thread of the core that is
2592 * exiting.
2593 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002594static inline int kvmppc_set_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002595{
2596 int core;
2597
2598 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002599 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002600
2601 /*
2602 * Memory barrier can be omitted here because we do a spin_unlock
2603 * immediately after this which provides the memory barrier.
2604 */
2605 core = cpu >> threads_shift;
2606 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002607 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002608}
2609
Paul Mackerras8b24e692017-06-26 15:45:51 +10002610static void set_irq_happened(int trap)
2611{
2612 switch (trap) {
2613 case BOOK3S_INTERRUPT_EXTERNAL:
2614 local_paca->irq_happened |= PACA_IRQ_EE;
2615 break;
2616 case BOOK3S_INTERRUPT_H_DOORBELL:
2617 local_paca->irq_happened |= PACA_IRQ_DBELL;
2618 break;
2619 case BOOK3S_INTERRUPT_HMI:
2620 local_paca->irq_happened |= PACA_IRQ_HMI;
2621 break;
2622 }
2623}
2624
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002625/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002626 * Run a set of guest threads on a physical core.
2627 * Called with vc->lock held.
2628 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002629static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002630{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002631 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002632 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002633 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002634 struct core_info core_info;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002635 struct kvmppc_vcore *pvc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002636 struct kvm_split_mode split_info, *sip;
2637 int split, subcore_size, active;
2638 int sub;
2639 bool thr0_done;
2640 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002641 int pcpu, thr;
2642 int target_threads;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002643 int controlled_threads;
Paul Mackerras8b24e692017-06-26 15:45:51 +10002644 int trap;
Paul Mackerras516f7892017-10-16 16:11:57 +11002645 bool is_power8;
Paul Mackerras081f3232012-06-01 20:20:24 +10002646
2647 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002648 * Remove from the list any threads that have a signal pending
2649 * or need a VPA update done
2650 */
2651 prepare_threads(vc);
2652
2653 /* if the runner is no longer runnable, let the caller pick a new one */
2654 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2655 return;
2656
2657 /*
2658 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002659 */
Paul Mackerras898b25b2017-06-22 15:08:42 +10002660 init_vcore_to_run(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002661 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002662
2663 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11002664 * Number of threads that we will be controlling: the same as
2665 * the number of threads per subcore, except on POWER9,
2666 * where it's 1 because the threads are (mostly) independent.
2667 */
Paul Mackerras516f7892017-10-16 16:11:57 +11002668 controlled_threads = threads_per_vcore(vc->kvm);
Paul Mackerras45c940b2016-11-18 17:43:30 +11002669
2670 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002671 * Make sure we are running on primary threads, and that secondary
2672 * threads are offline. Also check if the number of threads in this
2673 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002674 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002675 if ((controlled_threads > 1) &&
Michael Ellerman3102f782014-05-23 18:15:29 +10002676 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002677 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002678 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002679 kvmppc_remove_runnable(vc, vcpu);
2680 wake_up(&vcpu->arch.cpu_run);
2681 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002682 goto out;
2683 }
2684
Paul Mackerrasec257162015-06-24 21:18:03 +10002685 /*
2686 * See if we could run any other vcores on the physical core
2687 * along with this one.
2688 */
2689 init_core_info(&core_info, vc);
2690 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002691 target_threads = controlled_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002692 if (target_smt_mode && target_smt_mode < target_threads)
2693 target_threads = target_smt_mode;
2694 if (vc->num_threads < target_threads)
2695 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002696
Paul Mackerras8b24e692017-06-26 15:45:51 +10002697 /*
2698 * On radix, arrange for TLB flushing if necessary.
2699 * This has to be done before disabling interrupts since
2700 * it uses smp_call_function().
2701 */
2702 pcpu = smp_processor_id();
2703 if (kvm_is_radix(vc->kvm)) {
2704 for (sub = 0; sub < core_info.n_subcores; ++sub)
2705 for_each_runnable_thread(i, vcpu, core_info.vc[sub])
2706 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
2707 }
2708
2709 /*
2710 * Hard-disable interrupts, and check resched flag and signals.
2711 * If we need to reschedule or deliver a signal, clean up
2712 * and return without going into the guest(s).
2713 */
2714 local_irq_disable();
2715 hard_irq_disable();
2716 if (lazy_irq_pending() || need_resched() ||
2717 recheck_signals(&core_info)) {
2718 local_irq_enable();
2719 vc->vcore_state = VCORE_INACTIVE;
2720 /* Unlock all except the primary vcore */
2721 for (sub = 1; sub < core_info.n_subcores; ++sub) {
2722 pvc = core_info.vc[sub];
2723 /* Put back on to the preempted vcores list */
2724 kvmppc_vcore_preempt(pvc);
2725 spin_unlock(&pvc->lock);
2726 }
2727 for (i = 0; i < controlled_threads; ++i)
2728 kvmppc_release_hwthread(pcpu + i);
2729 return;
2730 }
2731
2732 kvmppc_clear_host_core(pcpu);
2733
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002734 /* Decide on micro-threading (split-core) mode */
2735 subcore_size = threads_per_subcore;
2736 cmd_bit = stat_bit = 0;
2737 split = core_info.n_subcores;
2738 sip = NULL;
Paul Mackerras516f7892017-10-16 16:11:57 +11002739 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S)
2740 && !cpu_has_feature(CPU_FTR_ARCH_300);
2741
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002742 if (split > 1) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002743 sip = &split_info;
2744 memset(&split_info, 0, sizeof(split_info));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002745 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10002746 split_info.vc[sub] = core_info.vc[sub];
Paul Mackerras516f7892017-10-16 16:11:57 +11002747
2748 if (is_power8) {
2749 if (split == 2 && (dynamic_mt_modes & 2)) {
2750 cmd_bit = HID0_POWER8_1TO2LPAR;
2751 stat_bit = HID0_POWER8_2LPARMODE;
2752 } else {
2753 split = 4;
2754 cmd_bit = HID0_POWER8_1TO4LPAR;
2755 stat_bit = HID0_POWER8_4LPARMODE;
2756 }
2757 subcore_size = MAX_SMT_THREADS / split;
2758 split_info.rpr = mfspr(SPRN_RPR);
2759 split_info.pmmar = mfspr(SPRN_PMMAR);
2760 split_info.ldbar = mfspr(SPRN_LDBAR);
2761 split_info.subcore_size = subcore_size;
2762 } else {
2763 split_info.subcore_size = 1;
2764 }
2765
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002766 /* order writes to split_info before kvm_split_mode pointer */
2767 smp_wmb();
2768 }
Paul Mackerras45c940b2016-11-18 17:43:30 +11002769 for (thr = 0; thr < controlled_threads; ++thr)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002770 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2771
Paul Mackerras516f7892017-10-16 16:11:57 +11002772 /* Initiate micro-threading (split-core) on POWER8 if required */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002773 if (cmd_bit) {
2774 unsigned long hid0 = mfspr(SPRN_HID0);
2775
2776 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2777 mb();
2778 mtspr(SPRN_HID0, hid0);
2779 isync();
2780 for (;;) {
2781 hid0 = mfspr(SPRN_HID0);
2782 if (hid0 & stat_bit)
2783 break;
2784 cpu_relax();
2785 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002786 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002787
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002788 /* Start all the threads */
2789 active = 0;
2790 for (sub = 0; sub < core_info.n_subcores; ++sub) {
Paul Mackerras516f7892017-10-16 16:11:57 +11002791 thr = is_power8 ? subcore_thread_map[sub] : sub;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002792 thr0_done = false;
2793 active |= 1 << thr;
Paul Mackerras898b25b2017-06-22 15:08:42 +10002794 pvc = core_info.vc[sub];
2795 pvc->pcpu = pcpu + thr;
2796 for_each_runnable_thread(i, vcpu, pvc) {
2797 kvmppc_start_thread(vcpu, pvc);
2798 kvmppc_create_dtl_entry(vcpu, pvc);
2799 trace_kvm_guest_enter(vcpu);
2800 if (!vcpu->arch.ptid)
2801 thr0_done = true;
2802 active |= 1 << (thr + vcpu->arch.ptid);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002803 }
Paul Mackerras898b25b2017-06-22 15:08:42 +10002804 /*
2805 * We need to start the first thread of each subcore
2806 * even if it doesn't have a vcpu.
2807 */
2808 if (!thr0_done)
2809 kvmppc_start_thread(NULL, pvc);
2810 thr += pvc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002811 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302812
2813 /*
2814 * Ensure that split_info.do_nap is set after setting
2815 * the vcore pointer in the PACA of the secondaries.
2816 */
2817 smp_mb();
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302818
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002819 /*
2820 * When doing micro-threading, poke the inactive threads as well.
2821 * This gets them to the nap instruction after kvm_do_nap,
2822 * which reduces the time taken to unsplit later.
2823 */
Paul Mackerras516f7892017-10-16 16:11:57 +11002824 if (cmd_bit) {
2825 split_info.do_nap = 1; /* ask secondaries to nap when done */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002826 for (thr = 1; thr < threads_per_subcore; ++thr)
2827 if (!(active & (1 << thr)))
2828 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerras516f7892017-10-16 16:11:57 +11002829 }
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002830
Paul Mackerras2f12f032012-10-15 01:17:17 +00002831 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002832 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002833
2834 trace_kvmppc_run_core(vc, 0);
2835
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002836 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10002837 spin_unlock(&core_info.vc[sub]->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002838
Paul Mackerras8b24e692017-06-26 15:45:51 +10002839 /*
2840 * Interrupts will be enabled once we get into the guest,
2841 * so tell lockdep that we're about to enable interrupts.
2842 */
2843 trace_hardirqs_on();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002844
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002845 guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002846
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002847 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002848
Paul Mackerras8b24e692017-06-26 15:45:51 +10002849 trap = __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002850
Paul Mackerrasec257162015-06-24 21:18:03 +10002851 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2852
Paul Mackerras8b24e692017-06-26 15:45:51 +10002853 guest_exit();
2854
2855 trace_hardirqs_off();
2856 set_irq_happened(trap);
2857
Paul Mackerrasec257162015-06-24 21:18:03 +10002858 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002859 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002860 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002861
Paul Mackerras371fefd2011-06-29 00:23:08 +00002862 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras516f7892017-10-16 16:11:57 +11002863 kvmppc_wait_for_nap(controlled_threads);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002864
2865 /* Return to whole-core mode if we split the core earlier */
Paul Mackerras516f7892017-10-16 16:11:57 +11002866 if (cmd_bit) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002867 unsigned long hid0 = mfspr(SPRN_HID0);
2868 unsigned long loops = 0;
2869
2870 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2871 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2872 mb();
2873 mtspr(SPRN_HID0, hid0);
2874 isync();
2875 for (;;) {
2876 hid0 = mfspr(SPRN_HID0);
2877 if (!(hid0 & stat_bit))
2878 break;
2879 cpu_relax();
2880 ++loops;
2881 }
2882 split_info.do_nap = 0;
2883 }
2884
Paul Mackerras8b24e692017-06-26 15:45:51 +10002885 kvmppc_set_host_core(pcpu);
2886
2887 local_irq_enable();
2888
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002889 /* Let secondaries go back to the offline loop */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002890 for (i = 0; i < controlled_threads; ++i) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002891 kvmppc_release_hwthread(pcpu + i);
2892 if (sip && sip->napped[i])
2893 kvmppc_ipi_thread(pcpu + i);
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002894 cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002895 }
2896
Paul Mackerras371fefd2011-06-29 00:23:08 +00002897 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002898
Paul Mackerras371fefd2011-06-29 00:23:08 +00002899 /* make sure updates to secondary vcpu structs are visible now */
2900 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002901
Paul Mackerras898b25b2017-06-22 15:08:42 +10002902 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2903 pvc = core_info.vc[sub];
2904 post_guest_process(pvc, pvc == vc);
2905 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00002906
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002907 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002908 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002909
Paul Mackerrasde56a942011-06-29 00:21:34 +00002910 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002911 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002912 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002913}
2914
Paul Mackerras19ccb762011-07-23 17:42:46 +10002915/*
2916 * Wait for some other vcpu thread to execute us, and
2917 * wake us up when we need to handle something in the host.
2918 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002919static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2920 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002921{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002922 DEFINE_WAIT(wait);
2923
Paul Mackerras19ccb762011-07-23 17:42:46 +10002924 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002925 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2926 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002927 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002928 spin_lock(&vc->lock);
2929 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002930 finish_wait(&vcpu->arch.cpu_run, &wait);
2931}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002932
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002933static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
2934{
2935 /* 10us base */
2936 if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
2937 vc->halt_poll_ns = 10000;
2938 else
2939 vc->halt_poll_ns *= halt_poll_ns_grow;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002940}
2941
2942static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
2943{
2944 if (halt_poll_ns_shrink == 0)
2945 vc->halt_poll_ns = 0;
2946 else
2947 vc->halt_poll_ns /= halt_poll_ns_shrink;
2948}
2949
Paul Mackerrasee3308a2017-06-20 15:46:12 +10002950#ifdef CONFIG_KVM_XICS
2951static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
2952{
2953 if (!xive_enabled())
2954 return false;
2955 return vcpu->arch.xive_saved_state.pipr <
2956 vcpu->arch.xive_saved_state.cppr;
2957}
2958#else
2959static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
2960{
2961 return false;
2962}
2963#endif /* CONFIG_KVM_XICS */
2964
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10002965static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
2966{
2967 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
Paul Mackerrasee3308a2017-06-20 15:46:12 +10002968 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10002969 return true;
2970
2971 return false;
2972}
2973
Suraj Jitindar Singh908a0932016-10-14 11:53:23 +11002974/*
2975 * Check to see if any of the runnable vcpus on the vcore have pending
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002976 * exceptions or are no longer ceded
2977 */
2978static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
2979{
2980 struct kvm_vcpu *vcpu;
2981 int i;
2982
2983 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10002984 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002985 return 1;
2986 }
2987
2988 return 0;
2989}
2990
Paul Mackerras19ccb762011-07-23 17:42:46 +10002991/*
2992 * All the vcpus in this vcore are idle, so wait for a decrementer
2993 * or external interrupt to one of the vcpus. vc->lock is held.
2994 */
2995static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2996{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002997 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002998 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002999 u64 block_ns;
Marcelo Tosatti85773702016-02-19 09:46:39 +01003000 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11003001
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003002 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003003 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003004 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003005 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
3006 ++vc->runner->stat.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003007
3008 vc->vcore_state = VCORE_POLLING;
3009 spin_unlock(&vc->lock);
3010
3011 do {
3012 if (kvmppc_vcore_check_block(vc)) {
3013 do_sleep = 0;
3014 break;
3015 }
3016 cur = ktime_get();
3017 } while (single_task_running() && ktime_before(cur, stop));
3018
3019 spin_lock(&vc->lock);
3020 vc->vcore_state = VCORE_INACTIVE;
3021
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003022 if (!do_sleep) {
3023 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003024 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003025 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003026 }
3027
Marcelo Tosatti85773702016-02-19 09:46:39 +01003028 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11003029
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003030 if (kvmppc_vcore_check_block(vc)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01003031 finish_swait(&vc->wq, &wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003032 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003033 /* If we polled, count this as a successful poll */
3034 if (vc->halt_poll_ns)
3035 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003036 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11003037 }
3038
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003039 start_wait = ktime_get();
3040
Paul Mackerras19ccb762011-07-23 17:42:46 +10003041 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003042 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003043 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00003044 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01003045 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003046 spin_lock(&vc->lock);
3047 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003048 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003049 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003050
3051 cur = ktime_get();
3052
3053out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10003054 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
3055
3056 /* Attribute wait time */
3057 if (do_sleep) {
3058 vc->runner->stat.halt_wait_ns +=
3059 ktime_to_ns(cur) - ktime_to_ns(start_wait);
3060 /* Attribute failed poll time */
3061 if (vc->halt_poll_ns)
3062 vc->runner->stat.halt_poll_fail_ns +=
3063 ktime_to_ns(start_wait) -
3064 ktime_to_ns(start_poll);
3065 } else {
3066 /* Attribute successful poll time */
3067 if (vc->halt_poll_ns)
3068 vc->runner->stat.halt_poll_success_ns +=
3069 ktime_to_ns(cur) -
3070 ktime_to_ns(start_poll);
3071 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003072
3073 /* Adjust poll time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11003074 if (halt_poll_ns) {
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003075 if (block_ns <= vc->halt_poll_ns)
3076 ;
3077 /* We slept and blocked for longer than the max halt time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11003078 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003079 shrink_halt_poll_ns(vc);
3080 /* We slept and our poll time is too small */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11003081 else if (vc->halt_poll_ns < halt_poll_ns &&
3082 block_ns < halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003083 grow_halt_poll_ns(vc);
Suraj Jitindar Singhe03f3922016-10-14 11:53:21 +11003084 if (vc->halt_poll_ns > halt_poll_ns)
3085 vc->halt_poll_ns = halt_poll_ns;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10003086 } else
3087 vc->halt_poll_ns = 0;
3088
3089 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003090}
3091
3092static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
3093{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003094 int n_ceded, i;
Paul Mackerras19ccb762011-07-23 17:42:46 +10003095 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003096 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00003097
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003098 trace_kvmppc_run_vcpu_enter(vcpu);
3099
Paul Mackerras371fefd2011-06-29 00:23:08 +00003100 kvm_run->exit_reason = 0;
3101 vcpu->arch.ret = RESUME_GUEST;
3102 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00003103 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003104
Paul Mackerras371fefd2011-06-29 00:23:08 +00003105 /*
3106 * Synchronize with other threads in this virtual core
3107 */
3108 vc = vcpu->arch.vcore;
3109 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003110 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003111 vcpu->arch.run_task = current;
3112 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003113 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10003114 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003115 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003116 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003117 ++vc->n_runnable;
3118
Paul Mackerras19ccb762011-07-23 17:42:46 +10003119 /*
3120 * This happens the first time this is called for a vcpu.
3121 * If the vcore is already running, we may be able to start
3122 * this thread straight away and have it join in.
3123 */
Paul Mackerras8455d792012-10-15 01:17:42 +00003124 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003125 if (vc->vcore_state == VCORE_PIGGYBACK) {
Paul Mackerras898b25b2017-06-22 15:08:42 +10003126 if (spin_trylock(&vc->lock)) {
3127 if (vc->vcore_state == VCORE_RUNNING &&
3128 !VCORE_IS_EXITING(vc)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003129 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003130 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10003131 trace_kvm_guest_enter(vcpu);
3132 }
Paul Mackerras898b25b2017-06-22 15:08:42 +10003133 spin_unlock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10003134 }
3135 } else if (vc->vcore_state == VCORE_RUNNING &&
3136 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00003137 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003138 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003139 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00003140 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01003141 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003142 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003143
Paul Mackerras8455d792012-10-15 01:17:42 +00003144 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10003145
3146 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
3147 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003148 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
3149 kvmppc_vcore_end_preempt(vc);
3150
Paul Mackerras8455d792012-10-15 01:17:42 +00003151 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003152 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003153 continue;
3154 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003155 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06003156 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003157 if (signal_pending(v->arch.run_task)) {
3158 kvmppc_remove_runnable(vc, v);
3159 v->stat.signal_exits++;
3160 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
3161 v->arch.ret = -EINTR;
3162 wake_up(&v->arch.cpu_run);
3163 }
3164 }
Paul Mackerras8455d792012-10-15 01:17:42 +00003165 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
3166 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00003167 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003168 for_each_runnable_thread(i, v, vc) {
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10003169 if (!kvmppc_vcpu_woken(v))
Paul Mackerras8455d792012-10-15 01:17:42 +00003170 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00003171 else
3172 v->arch.ceded = 0;
3173 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003174 vc->runner = vcpu;
3175 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00003176 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03003177 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003178 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003179 /* Let something else run */
3180 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10003181 if (vc->vcore_state == VCORE_PREEMPT)
3182 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003183 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00003184 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003185 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00003186 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003187 }
3188
Paul Mackerras8455d792012-10-15 01:17:42 +00003189 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
3190 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10003191 vc->vcore_state == VCORE_EXITING ||
3192 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10003193 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00003194
Paul Mackerras5fc3e642015-09-18 13:13:44 +10003195 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
3196 kvmppc_vcore_end_preempt(vc);
3197
Paul Mackerras8455d792012-10-15 01:17:42 +00003198 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
3199 kvmppc_remove_runnable(vc, vcpu);
3200 vcpu->stat.signal_exits++;
3201 kvm_run->exit_reason = KVM_EXIT_INTR;
3202 vcpu->arch.ret = -EINTR;
3203 }
3204
3205 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
3206 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003207 i = -1;
3208 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00003209 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003210 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003211
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003212 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003213 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003214 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003215}
3216
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303217static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00003218{
3219 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00003220 int srcu_idx;
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003221 unsigned long ebb_regs[3] = {}; /* shut up GCC */
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003222 unsigned long user_tar = 0;
3223 unsigned int user_vrsave;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003224 struct kvm *kvm;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00003225
Alexander Grafaf8f38b2011-08-10 13:57:08 +02003226 if (!vcpu->arch.sane) {
3227 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3228 return -EINVAL;
3229 }
3230
Paul Mackerras46a704f2017-06-15 16:10:27 +10003231 /*
3232 * Don't allow entry with a suspended transaction, because
3233 * the guest entry/exit code will lose it.
3234 * If the guest has TM enabled, save away their TM-related SPRs
3235 * (they will get restored by the TM unavailable interrupt).
3236 */
3237#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
3238 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
3239 (current->thread.regs->msr & MSR_TM)) {
3240 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
3241 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3242 run->fail_entry.hardware_entry_failure_reason = 0;
3243 return -EINVAL;
3244 }
Paul Mackerrase4705712017-07-21 13:57:14 +10003245 /* Enable TM so we can read the TM SPRs */
3246 mtmsr(mfmsr() | MSR_TM);
Paul Mackerras46a704f2017-06-15 16:10:27 +10003247 current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
3248 current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
3249 current->thread.tm_texasr = mfspr(SPRN_TEXASR);
3250 current->thread.regs->msr &= ~MSR_TM;
3251 }
3252#endif
3253
Scott Wood25051b5a2011-11-08 18:23:23 -06003254 kvmppc_core_prepare_to_enter(vcpu);
3255
Paul Mackerras19ccb762011-07-23 17:42:46 +10003256 /* No need to go into the guest when all we'll do is come back out */
3257 if (signal_pending(current)) {
3258 run->exit_reason = KVM_EXIT_INTR;
3259 return -EINTR;
3260 }
3261
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003262 kvm = vcpu->kvm;
3263 atomic_inc(&kvm->arch.vcpus_running);
3264 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00003265 smp_mb();
3266
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003267 /* On the first time here, set up MMU if necessary */
3268 if (!vcpu->kvm->arch.mmu_ready) {
3269 mutex_lock(&kvm->lock);
3270 r = 0;
3271 if (!kvm->arch.mmu_ready) {
3272 if (!kvm_is_radix(vcpu->kvm))
3273 r = kvmppc_hv_setup_htab_rma(vcpu);
3274 if (!r) {
3275 if (cpu_has_feature(CPU_FTR_ARCH_300))
3276 kvmppc_setup_partition_table(kvm);
3277 kvm->arch.mmu_ready = 1;
3278 }
3279 }
3280 mutex_unlock(&kvm->lock);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003281 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00003282 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003283 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10003284
Anton Blanchard579e6332015-10-29 11:44:09 +11003285 flush_all_to_thread(current);
3286
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003287 /* Save userspace EBB and other register values */
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003288 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
3289 ebb_regs[0] = mfspr(SPRN_EBBHR);
3290 ebb_regs[1] = mfspr(SPRN_EBBRR);
3291 ebb_regs[2] = mfspr(SPRN_BESCR);
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003292 user_tar = mfspr(SPRN_TAR);
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003293 }
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003294 user_vrsave = mfspr(SPRN_VRSAVE);
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003295
Paul Mackerras19ccb762011-07-23 17:42:46 +10003296 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00003297 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003298 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10003299
Paul Mackerrasa8606e22011-06-29 00:22:05 +00003300 do {
3301 r = kvmppc_run_vcpu(run, vcpu);
3302
3303 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
3304 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003305 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00003306 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003307 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06003308 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00003309 } else if (r == RESUME_PAGE_FAULT) {
3310 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3311 r = kvmppc_book3s_hv_page_fault(run, vcpu,
3312 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
3313 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003314 } else if (r == RESUME_PASSTHROUGH) {
3315 if (WARN_ON(xive_enabled()))
3316 r = H_SUCCESS;
3317 else
3318 r = kvmppc_xics_rm_complete(vcpu, 0);
3319 }
Greg Kurze59d24e2014-02-06 17:36:56 +01003320 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00003321
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003322 /* Restore userspace EBB and other register values */
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003323 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
3324 mtspr(SPRN_EBBHR, ebb_regs[0]);
3325 mtspr(SPRN_EBBRR, ebb_regs[1]);
3326 mtspr(SPRN_BESCR, ebb_regs[2]);
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003327 mtspr(SPRN_TAR, user_tar);
3328 mtspr(SPRN_FSCR, current->thread.fscr);
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003329 }
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10003330 mtspr(SPRN_VRSAVE, user_vrsave);
Paul Mackerrasca8efa12017-06-06 16:47:22 +10003331
Paul Mackerras32fad282012-05-04 02:32:53 +00003332 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003333 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00003334 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00003335 return r;
3336}
3337
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003338static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003339 int shift, int sllp)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003340{
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003341 (*sps)->page_shift = shift;
3342 (*sps)->slb_enc = sllp;
3343 (*sps)->enc[0].page_shift = shift;
3344 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05303345 /*
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003346 * Add 16MB MPSS support (may get filtered out by userspace)
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05303347 */
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003348 if (shift != 24) {
3349 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
3350 if (penc != -1) {
3351 (*sps)->enc[1].page_shift = 24;
3352 (*sps)->enc[1].pte_enc = penc;
3353 }
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05303354 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003355 (*sps)++;
3356}
3357
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303358static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
3359 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003360{
3361 struct kvm_ppc_one_seg_page_size *sps;
3362
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003363 /*
Paul Mackerrase3bfed12017-08-25 19:53:39 +10003364 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
3365 * POWER7 doesn't support keys for instruction accesses,
3366 * POWER8 and POWER9 do.
3367 */
3368 info->data_keys = 32;
3369 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
3370
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003371 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
3372 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
3373 info->slb_size = 32;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003374
3375 /* We only support these sizes for now, and no muti-size segments */
3376 sps = &info->sps[0];
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10003377 kvmppc_add_seg_page_size(&sps, 12, 0);
3378 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
3379 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00003380
3381 return 0;
3382}
3383
Paul Mackerras82ed3612011-12-15 02:03:22 +00003384/*
3385 * Get (and clear) the dirty memory log for a memory slot.
3386 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303387static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
3388 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003389{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003390 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003391 struct kvm_memory_slot *memslot;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003392 int i, r;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003393 unsigned long n;
Paul Mackerrase641a312017-10-26 16:39:19 +11003394 unsigned long *buf, *p;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003395 struct kvm_vcpu *vcpu;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003396
3397 mutex_lock(&kvm->slots_lock);
3398
3399 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07003400 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003401 goto out;
3402
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003403 slots = kvm_memslots(kvm);
3404 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003405 r = -ENOENT;
3406 if (!memslot->dirty_bitmap)
3407 goto out;
3408
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003409 /*
Paul Mackerrase641a312017-10-26 16:39:19 +11003410 * Use second half of bitmap area because both HPT and radix
3411 * accumulate bits in the first half.
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003412 */
Paul Mackerras82ed3612011-12-15 02:03:22 +00003413 n = kvm_dirty_bitmap_bytes(memslot);
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003414 buf = memslot->dirty_bitmap + n / sizeof(long);
3415 memset(buf, 0, n);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003416
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003417 if (kvm_is_radix(kvm))
3418 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
3419 else
3420 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003421 if (r)
3422 goto out;
3423
Paul Mackerrase641a312017-10-26 16:39:19 +11003424 /*
3425 * We accumulate dirty bits in the first half of the
3426 * memslot's dirty_bitmap area, for when pages are paged
3427 * out or modified by the host directly. Pick up these
3428 * bits and add them to the map.
3429 */
3430 p = memslot->dirty_bitmap;
3431 for (i = 0; i < n / sizeof(long); ++i)
3432 buf[i] |= xchg(&p[i], 0);
3433
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003434 /* Harvest dirty bits from VPA and DTL updates */
3435 /* Note: we never modify the SLB shadow buffer areas */
3436 kvm_for_each_vcpu(i, vcpu, kvm) {
3437 spin_lock(&vcpu->arch.vpa_update_lock);
3438 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
3439 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
3440 spin_unlock(&vcpu->arch.vpa_update_lock);
3441 }
3442
Paul Mackerras82ed3612011-12-15 02:03:22 +00003443 r = -EFAULT;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11003444 if (copy_to_user(log->dirty_bitmap, buf, n))
Paul Mackerras82ed3612011-12-15 02:03:22 +00003445 goto out;
3446
3447 r = 0;
3448out:
3449 mutex_unlock(&kvm->slots_lock);
3450 return r;
3451}
3452
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303453static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
3454 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003455{
3456 if (!dont || free->arch.rmap != dont->arch.rmap) {
3457 vfree(free->arch.rmap);
3458 free->arch.rmap = NULL;
3459 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003460}
3461
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303462static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
3463 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003464{
3465 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
3466 if (!slot->arch.rmap)
3467 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003468
3469 return 0;
3470}
3471
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303472static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
3473 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003474 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003475{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003476 return 0;
3477}
3478
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303479static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003480 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02003481 const struct kvm_memory_slot *old,
3482 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003483{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003484 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003485
Yongji Xiea56ee9f2016-11-04 13:55:12 +08003486 /*
3487 * If we are making a new memslot, it might make
3488 * some address that was previously cached as emulated
3489 * MMIO be no longer emulated MMIO, so invalidate
3490 * all the caches of emulated MMIO translations.
3491 */
3492 if (npages)
3493 atomic64_inc(&kvm->arch.mmio_update);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003494}
3495
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003496/*
3497 * Update LPCR values in kvm->arch and in vcores.
3498 * Caller must hold kvm->lock.
3499 */
3500void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
3501{
3502 long int i;
3503 u32 cores_done = 0;
3504
3505 if ((kvm->arch.lpcr & mask) == lpcr)
3506 return;
3507
3508 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
3509
3510 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3511 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3512 if (!vc)
3513 continue;
3514 spin_lock(&vc->lock);
3515 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
3516 spin_unlock(&vc->lock);
3517 if (++cores_done >= kvm->arch.online_vcores)
3518 break;
3519 }
3520}
3521
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303522static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
3523{
3524 return;
3525}
3526
Paul Mackerras7a840842016-11-16 22:25:20 +11003527static void kvmppc_setup_partition_table(struct kvm *kvm)
3528{
3529 unsigned long dw0, dw1;
3530
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003531 if (!kvm_is_radix(kvm)) {
3532 /* PS field - page size for VRMA */
3533 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
3534 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
3535 /* HTABSIZE and HTABORG fields */
3536 dw0 |= kvm->arch.sdr1;
Paul Mackerras7a840842016-11-16 22:25:20 +11003537
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003538 /* Second dword as set by userspace */
3539 dw1 = kvm->arch.process_table;
3540 } else {
3541 dw0 = PATB_HR | radix__get_tree_size() |
3542 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
3543 dw1 = PATB_GR | kvm->arch.process_table;
3544 }
Paul Mackerras7a840842016-11-16 22:25:20 +11003545
3546 mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
3547}
3548
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003549/*
3550 * Set up HPT (hashed page table) and RMA (real-mode area).
3551 * Must be called with kvm->lock held.
3552 */
Paul Mackerras32fad282012-05-04 02:32:53 +00003553static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003554{
3555 int err = 0;
3556 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003557 unsigned long hva;
3558 struct kvm_memory_slot *memslot;
3559 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003560 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003561 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003562 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003563
Paul Mackerras32fad282012-05-04 02:32:53 +00003564 /* Allocate hashed page table (if not done already) and reset it */
David Gibson3f9d4f52016-12-20 16:49:00 +11003565 if (!kvm->arch.hpt.virt) {
David Gibsonaae07772016-12-20 16:49:02 +11003566 int order = KVM_DEFAULT_HPT_ORDER;
3567 struct kvm_hpt_info info;
3568
3569 err = kvmppc_allocate_hpt(&info, order);
3570 /* If we get here, it means userspace didn't specify a
3571 * size explicitly. So, try successively smaller
3572 * sizes if the default failed. */
3573 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
3574 err = kvmppc_allocate_hpt(&info, order);
3575
3576 if (err < 0) {
Paul Mackerras32fad282012-05-04 02:32:53 +00003577 pr_err("KVM: Couldn't alloc HPT\n");
3578 goto out;
3579 }
David Gibsonaae07772016-12-20 16:49:02 +11003580
3581 kvmppc_set_hpt(kvm, &info);
Paul Mackerras32fad282012-05-04 02:32:53 +00003582 }
3583
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003584 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003585 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003586 memslot = gfn_to_memslot(kvm, 0);
3587
3588 /* We must have some memory at 0 by now */
3589 err = -EINVAL;
3590 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003591 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003592
3593 /* Look up the VMA for the start of this memory slot */
3594 hva = memslot->userspace_addr;
3595 down_read(&current->mm->mmap_sem);
3596 vma = find_vma(current->mm, hva);
3597 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3598 goto up_out;
3599
3600 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003601 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003602
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003603 up_read(&current->mm->mmap_sem);
3604
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003605 /* We can handle 4k, 64k or 16M pages in the VRMA */
3606 err = -EINVAL;
3607 if (!(psize == 0x1000 || psize == 0x10000 ||
3608 psize == 0x1000000))
3609 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003610
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003611 senc = slb_pgsize_encoding(psize);
3612 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3613 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003614 /* Create HPTEs in the hash page table for the VRMA */
3615 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003616
Paul Mackerras7a840842016-11-16 22:25:20 +11003617 /* Update VRMASD field in the LPCR */
3618 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
3619 /* the -4 is to account for senc values starting at 0x10 */
3620 lpcr = senc << (LPCR_VRMASD_SH - 4);
3621 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerras7a840842016-11-16 22:25:20 +11003622 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003623
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003624 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003625 smp_wmb();
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003626 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003627 out_srcu:
3628 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003629 out:
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003630 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003631
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003632 up_out:
3633 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003634 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003635}
3636
Paul Mackerras18c36402017-09-13 16:00:10 +10003637/* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
3638int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
3639{
3640 kvmppc_free_radix(kvm);
3641 kvmppc_update_lpcr(kvm, LPCR_VPM1,
3642 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
3643 kvmppc_rmap_reset(kvm);
3644 kvm->arch.radix = 0;
3645 kvm->arch.process_table = 0;
3646 return 0;
3647}
3648
3649/* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
3650int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
3651{
3652 int err;
3653
3654 err = kvmppc_init_vm_radix(kvm);
3655 if (err)
3656 return err;
3657
3658 kvmppc_free_hpt(&kvm->arch.hpt);
3659 kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR,
3660 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
3661 kvm->arch.radix = 1;
3662 return 0;
3663}
3664
Suresh Warrier79b6c242015-12-17 14:59:06 -06003665#ifdef CONFIG_KVM_XICS
3666/*
3667 * Allocate a per-core structure for managing state about which cores are
3668 * running in the host versus the guest and for exchanging data between
3669 * real mode KVM and CPU running in the host.
3670 * This is only done for the first VM.
3671 * The allocated structure stays even if all VMs have stopped.
3672 * It is only freed when the kvm-hv module is unloaded.
3673 * It's OK for this routine to fail, we just don't support host
3674 * core operations like redirecting H_IPI wakeups.
3675 */
3676void kvmppc_alloc_host_rm_ops(void)
3677{
3678 struct kvmppc_host_rm_ops *ops;
3679 unsigned long l_ops;
3680 int cpu, core;
3681 int size;
3682
3683 /* Not the first time here ? */
3684 if (kvmppc_host_rm_ops_hv != NULL)
3685 return;
3686
3687 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3688 if (!ops)
3689 return;
3690
3691 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3692 ops->rm_core = kzalloc(size, GFP_KERNEL);
3693
3694 if (!ops->rm_core) {
3695 kfree(ops);
3696 return;
3697 }
3698
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02003699 cpus_read_lock();
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003700
Suresh Warrier79b6c242015-12-17 14:59:06 -06003701 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3702 if (!cpu_online(cpu))
3703 continue;
3704
3705 core = cpu >> threads_shift;
3706 ops->rm_core[core].rm_state.in_host = 1;
3707 }
3708
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003709 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3710
Suresh Warrier79b6c242015-12-17 14:59:06 -06003711 /*
3712 * Make the contents of the kvmppc_host_rm_ops structure visible
3713 * to other CPUs before we assign it to the global variable.
3714 * Do an atomic assignment (no locks used here), but if someone
3715 * beats us to it, just free our copy and return.
3716 */
3717 smp_wmb();
3718 l_ops = (unsigned long) ops;
3719
3720 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02003721 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003722 kfree(ops->rm_core);
3723 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003724 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003725 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003726
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02003727 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
3728 "ppc/kvm_book3s:prepare",
3729 kvmppc_set_host_core,
3730 kvmppc_clear_host_core);
3731 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003732}
3733
3734void kvmppc_free_host_rm_ops(void)
3735{
3736 if (kvmppc_host_rm_ops_hv) {
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003737 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003738 kfree(kvmppc_host_rm_ops_hv->rm_core);
3739 kfree(kvmppc_host_rm_ops_hv);
3740 kvmppc_host_rm_ops_hv = NULL;
3741 }
3742}
3743#endif
3744
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303745static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003746{
Paul Mackerras32fad282012-05-04 02:32:53 +00003747 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003748 char buf[32];
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003749 int ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003750
Paul Mackerras32fad282012-05-04 02:32:53 +00003751 /* Allocate the guest's logical partition ID */
3752
3753 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003754 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003755 return -ENOMEM;
3756 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003757
Suresh Warrier79b6c242015-12-17 14:59:06 -06003758 kvmppc_alloc_host_rm_ops();
3759
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003760 /*
3761 * Since we don't flush the TLB when tearing down a VM,
3762 * and this lpid might have previously been used,
3763 * make sure we flush on each core before running the new VM.
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003764 * On POWER9, the tlbie in mmu_partition_table_set_entry()
3765 * does this flush for us.
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003766 */
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003767 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3768 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003769
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003770 /* Start out with the default set of hcalls enabled */
3771 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3772 sizeof(kvm->arch.enabled_hcalls));
3773
Paul Mackerras7a840842016-11-16 22:25:20 +11003774 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3775 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003776
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003777 /* Init LPCR for virtual RMA mode */
3778 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3779 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3780 lpcr &= LPCR_PECE | LPCR_LPES;
3781 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3782 LPCR_VPM0 | LPCR_VPM1;
3783 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3784 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3785 /* On POWER8 turn on online bit to enable PURR/SPURR */
3786 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3787 lpcr |= LPCR_ONL;
Paul Mackerras84f71392016-11-22 14:30:14 +11003788 /*
3789 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
3790 * Set HVICE bit to enable hypervisor virtualization interrupts.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003791 * Set HEIC to prevent OS interrupts to go to hypervisor (should
3792 * be unnecessary but better safe than sorry in case we re-enable
3793 * EE in HV mode with this LPCR still set)
Paul Mackerras84f71392016-11-22 14:30:14 +11003794 */
3795 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerras7a840842016-11-16 22:25:20 +11003796 lpcr &= ~LPCR_VPM0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003797 lpcr |= LPCR_HVICE | LPCR_HEIC;
3798
3799 /*
3800 * If xive is enabled, we route 0x500 interrupts directly
3801 * to the guest.
3802 */
3803 if (xive_enabled())
3804 lpcr |= LPCR_LPES;
Paul Mackerras84f71392016-11-22 14:30:14 +11003805 }
3806
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003807 /*
Paul Mackerras18c36402017-09-13 16:00:10 +10003808 * If the host uses radix, the guest starts out as radix.
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003809 */
3810 if (radix_enabled()) {
3811 kvm->arch.radix = 1;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10003812 kvm->arch.mmu_ready = 1;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003813 lpcr &= ~LPCR_VPM1;
3814 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
3815 ret = kvmppc_init_vm_radix(kvm);
3816 if (ret) {
3817 kvmppc_free_lpid(kvm->arch.lpid);
3818 return ret;
3819 }
3820 kvmppc_setup_partition_table(kvm);
3821 }
3822
Paul Mackerras9e368f22011-06-29 00:40:08 +00003823 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003824
David Gibson5e985962016-12-20 16:49:05 +11003825 /* Initialization for future HPT resizes */
3826 kvm->arch.resize_hpt = NULL;
3827
Paul Mackerras512691d2012-10-15 01:15:41 +00003828 /*
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003829 * Work out how many sets the TLB has, for the use of
3830 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
3831 */
Paul Mackerras18c36402017-09-13 16:00:10 +10003832 if (radix_enabled())
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003833 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
3834 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003835 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
3836 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
3837 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
3838 else
3839 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
3840
3841 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003842 * Track that we now have a HV mode VM active. This blocks secondary
3843 * CPU threads from coming online.
Paul Mackerras516f7892017-10-16 16:11:57 +11003844 * On POWER9, we only need to do this if the "indep_threads_mode"
3845 * module parameter has been set to N.
Paul Mackerras512691d2012-10-15 01:15:41 +00003846 */
Paul Mackerras516f7892017-10-16 16:11:57 +11003847 if (cpu_has_feature(CPU_FTR_ARCH_300))
3848 kvm->arch.threads_indep = indep_threads_mode;
3849 if (!kvm->arch.threads_indep)
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003850 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003851
Paul Mackerrase23a8082015-03-28 14:21:01 +11003852 /*
Paul Mackerras3c313522017-02-06 13:24:41 +11003853 * Initialize smt_mode depending on processor.
3854 * POWER8 and earlier have to use "strict" threading, where
3855 * all vCPUs in a vcore have to run on the same (sub)core,
3856 * whereas on POWER9 the threads can each run a different
3857 * guest.
3858 */
3859 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3860 kvm->arch.smt_mode = threads_per_subcore;
3861 else
3862 kvm->arch.smt_mode = 1;
Paul Mackerras57900692017-05-16 16:41:20 +10003863 kvm->arch.emul_smt_mode = 1;
Paul Mackerras3c313522017-02-06 13:24:41 +11003864
3865 /*
Paul Mackerrase23a8082015-03-28 14:21:01 +11003866 * Create a debugfs directory for the VM
3867 */
3868 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3869 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3870 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3871 kvmppc_mmu_debugfs_init(kvm);
3872
David Gibson54738c02011-06-29 00:22:41 +00003873 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003874}
3875
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303876static void kvmppc_free_vcores(struct kvm *kvm)
3877{
3878 long int i;
3879
Paul Mackerras23316312015-10-21 16:03:14 +11003880 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303881 kfree(kvm->arch.vcores[i]);
3882 kvm->arch.online_vcores = 0;
3883}
3884
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303885static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003886{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003887 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3888
Paul Mackerras516f7892017-10-16 16:11:57 +11003889 if (!kvm->arch.threads_indep)
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003890 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003891
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303892 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003893
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11003894 kvmppc_free_lpid(kvm->arch.lpid);
3895
Paul Mackerras5a319352017-01-30 21:21:46 +11003896 if (kvm_is_radix(kvm))
3897 kvmppc_free_radix(kvm);
3898 else
David Gibsonaae07772016-12-20 16:49:02 +11003899 kvmppc_free_hpt(&kvm->arch.hpt);
Suresh Warrierc57875f2016-08-19 15:35:50 +10003900
3901 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003902}
3903
3904/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303905static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3906 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003907{
3908 return EMULATE_FAIL;
3909}
3910
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303911static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3912 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003913{
3914 return EMULATE_FAIL;
3915}
3916
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303917static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3918 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003919{
3920 return EMULATE_FAIL;
3921}
3922
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303923static int kvmppc_core_check_processor_compat_hv(void)
3924{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003925 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3926 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303927 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003928
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303929 return 0;
3930}
3931
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003932#ifdef CONFIG_KVM_XICS
3933
3934void kvmppc_free_pimap(struct kvm *kvm)
3935{
3936 kfree(kvm->arch.pimap);
3937}
3938
Suresh Warrierc57875f2016-08-19 15:35:50 +10003939static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003940{
3941 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
3942}
Suresh Warrierc57875f2016-08-19 15:35:50 +10003943
3944static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3945{
3946 struct irq_desc *desc;
3947 struct kvmppc_irq_map *irq_map;
3948 struct kvmppc_passthru_irqmap *pimap;
3949 struct irq_chip *chip;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003950 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003951
Suresh Warrier644abbb2016-08-19 15:35:54 +10003952 if (!kvm_irq_bypass)
3953 return 1;
3954
Suresh Warrierc57875f2016-08-19 15:35:50 +10003955 desc = irq_to_desc(host_irq);
3956 if (!desc)
3957 return -EIO;
3958
3959 mutex_lock(&kvm->lock);
3960
3961 pimap = kvm->arch.pimap;
3962 if (pimap == NULL) {
3963 /* First call, allocate structure to hold IRQ map */
3964 pimap = kvmppc_alloc_pimap();
3965 if (pimap == NULL) {
3966 mutex_unlock(&kvm->lock);
3967 return -ENOMEM;
3968 }
3969 kvm->arch.pimap = pimap;
3970 }
3971
3972 /*
3973 * For now, we only support interrupts for which the EOI operation
3974 * is an OPAL call followed by a write to XIRR, since that's
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003975 * what our real-mode EOI code does, or a XIVE interrupt
Suresh Warrierc57875f2016-08-19 15:35:50 +10003976 */
3977 chip = irq_data_get_irq_chip(&desc->irq_data);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10003978 if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
Suresh Warrierc57875f2016-08-19 15:35:50 +10003979 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
3980 host_irq, guest_gsi);
3981 mutex_unlock(&kvm->lock);
3982 return -ENOENT;
3983 }
3984
3985 /*
3986 * See if we already have an entry for this guest IRQ number.
3987 * If it's mapped to a hardware IRQ number, that's an error,
3988 * otherwise re-use this entry.
3989 */
3990 for (i = 0; i < pimap->n_mapped; i++) {
3991 if (guest_gsi == pimap->mapped[i].v_hwirq) {
3992 if (pimap->mapped[i].r_hwirq) {
3993 mutex_unlock(&kvm->lock);
3994 return -EINVAL;
3995 }
3996 break;
3997 }
3998 }
3999
4000 if (i == KVMPPC_PIRQ_MAPPED) {
4001 mutex_unlock(&kvm->lock);
4002 return -EAGAIN; /* table is full */
4003 }
4004
4005 irq_map = &pimap->mapped[i];
4006
4007 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10004008 irq_map->desc = desc;
4009
Suresh Warriere3c13e52016-08-19 15:35:51 +10004010 /*
4011 * Order the above two stores before the next to serialize with
4012 * the KVM real mode handler.
4013 */
4014 smp_wmb();
4015 irq_map->r_hwirq = desc->irq_data.hwirq;
4016
Suresh Warrierc57875f2016-08-19 15:35:50 +10004017 if (i == pimap->n_mapped)
4018 pimap->n_mapped++;
4019
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004020 if (xive_enabled())
4021 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
4022 else
4023 kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
4024 if (rc)
4025 irq_map->r_hwirq = 0;
Paul Mackerras5d375192016-08-19 15:35:56 +10004026
Suresh Warrierc57875f2016-08-19 15:35:50 +10004027 mutex_unlock(&kvm->lock);
4028
4029 return 0;
4030}
4031
4032static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
4033{
4034 struct irq_desc *desc;
4035 struct kvmppc_passthru_irqmap *pimap;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004036 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10004037
Suresh Warrier644abbb2016-08-19 15:35:54 +10004038 if (!kvm_irq_bypass)
4039 return 0;
4040
Suresh Warrierc57875f2016-08-19 15:35:50 +10004041 desc = irq_to_desc(host_irq);
4042 if (!desc)
4043 return -EIO;
4044
4045 mutex_lock(&kvm->lock);
Markus Elfringa1c52e12017-01-20 11:00:08 +01004046 if (!kvm->arch.pimap)
4047 goto unlock;
Suresh Warrierc57875f2016-08-19 15:35:50 +10004048
Suresh Warrierc57875f2016-08-19 15:35:50 +10004049 pimap = kvm->arch.pimap;
4050
4051 for (i = 0; i < pimap->n_mapped; i++) {
4052 if (guest_gsi == pimap->mapped[i].v_hwirq)
4053 break;
4054 }
4055
4056 if (i == pimap->n_mapped) {
4057 mutex_unlock(&kvm->lock);
4058 return -ENODEV;
4059 }
4060
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004061 if (xive_enabled())
4062 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
4063 else
4064 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
Paul Mackerras5d375192016-08-19 15:35:56 +10004065
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004066 /* invalidate the entry (what do do on error from the above ?) */
Suresh Warrierc57875f2016-08-19 15:35:50 +10004067 pimap->mapped[i].r_hwirq = 0;
4068
4069 /*
4070 * We don't free this structure even when the count goes to
4071 * zero. The structure is freed when we destroy the VM.
4072 */
Markus Elfringa1c52e12017-01-20 11:00:08 +01004073 unlock:
Suresh Warrierc57875f2016-08-19 15:35:50 +10004074 mutex_unlock(&kvm->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004075 return rc;
Suresh Warrierc57875f2016-08-19 15:35:50 +10004076}
4077
4078static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
4079 struct irq_bypass_producer *prod)
4080{
4081 int ret = 0;
4082 struct kvm_kernel_irqfd *irqfd =
4083 container_of(cons, struct kvm_kernel_irqfd, consumer);
4084
4085 irqfd->producer = prod;
4086
4087 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
4088 if (ret)
4089 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
4090 prod->irq, irqfd->gsi, ret);
4091
4092 return ret;
4093}
4094
4095static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
4096 struct irq_bypass_producer *prod)
4097{
4098 int ret;
4099 struct kvm_kernel_irqfd *irqfd =
4100 container_of(cons, struct kvm_kernel_irqfd, consumer);
4101
4102 irqfd->producer = NULL;
4103
4104 /*
4105 * When producer of consumer is unregistered, we change back to
4106 * default external interrupt handling mode - KVM real mode
4107 * will switch back to host.
4108 */
4109 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
4110 if (ret)
4111 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
4112 prod->irq, irqfd->gsi, ret);
4113}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10004114#endif
4115
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304116static long kvm_arch_vm_ioctl_hv(struct file *filp,
4117 unsigned int ioctl, unsigned long arg)
4118{
4119 struct kvm *kvm __maybe_unused = filp->private_data;
4120 void __user *argp = (void __user *)arg;
4121 long r;
4122
4123 switch (ioctl) {
4124
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304125 case KVM_PPC_ALLOCATE_HTAB: {
4126 u32 htab_order;
4127
4128 r = -EFAULT;
4129 if (get_user(htab_order, (u32 __user *)argp))
4130 break;
David Gibsonf98a8bf2016-12-20 16:49:03 +11004131 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304132 if (r)
4133 break;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304134 r = 0;
4135 break;
4136 }
4137
4138 case KVM_PPC_GET_HTAB_FD: {
4139 struct kvm_get_htab_fd ghf;
4140
4141 r = -EFAULT;
4142 if (copy_from_user(&ghf, argp, sizeof(ghf)))
4143 break;
4144 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
4145 break;
4146 }
4147
David Gibson5e985962016-12-20 16:49:05 +11004148 case KVM_PPC_RESIZE_HPT_PREPARE: {
4149 struct kvm_ppc_resize_hpt rhpt;
4150
4151 r = -EFAULT;
4152 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
4153 break;
4154
4155 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
4156 break;
4157 }
4158
4159 case KVM_PPC_RESIZE_HPT_COMMIT: {
4160 struct kvm_ppc_resize_hpt rhpt;
4161
4162 r = -EFAULT;
4163 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
4164 break;
4165
4166 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
4167 break;
4168 }
4169
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304170 default:
4171 r = -ENOTTY;
4172 }
4173
4174 return r;
4175}
4176
Paul Mackerras699a0ea2014-06-02 11:02:59 +10004177/*
4178 * List of hcall numbers to enable by default.
4179 * For compatibility with old userspace, we enable by default
4180 * all hcalls that were implemented before the hcall-enabling
4181 * facility was added. Note this list should not include H_RTAS.
4182 */
4183static unsigned int default_hcall_list[] = {
4184 H_REMOVE,
4185 H_ENTER,
4186 H_READ,
4187 H_PROTECT,
4188 H_BULK_REMOVE,
4189 H_GET_TCE,
4190 H_PUT_TCE,
4191 H_SET_DABR,
4192 H_SET_XDABR,
4193 H_CEDE,
4194 H_PROD,
4195 H_CONFER,
4196 H_REGISTER_VPA,
4197#ifdef CONFIG_KVM_XICS
4198 H_EOI,
4199 H_CPPR,
4200 H_IPI,
4201 H_IPOLL,
4202 H_XIRR,
4203 H_XIRR_X,
4204#endif
4205 0
4206};
4207
4208static void init_default_hcalls(void)
4209{
4210 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10004211 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10004212
Paul Mackerrasae2113a2014-06-02 11:03:00 +10004213 for (i = 0; default_hcall_list[i]; ++i) {
4214 hcall = default_hcall_list[i];
4215 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
4216 __set_bit(hcall / 4, default_enabled_hcalls);
4217 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10004218}
4219
Paul Mackerrasc9270132017-01-30 21:21:41 +11004220static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
4221{
Paul Mackerras468808b2017-01-30 21:21:42 +11004222 unsigned long lpcr;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11004223 int radix;
Paul Mackerras18c36402017-09-13 16:00:10 +10004224 int err;
Paul Mackerras468808b2017-01-30 21:21:42 +11004225
4226 /* If not on a POWER9, reject it */
4227 if (!cpu_has_feature(CPU_FTR_ARCH_300))
4228 return -ENODEV;
4229
4230 /* If any unknown flags set, reject it */
4231 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
4232 return -EINVAL;
4233
Paul Mackerras468808b2017-01-30 21:21:42 +11004234 /* GR (guest radix) bit in process_table field must match */
Paul Mackerras18c36402017-09-13 16:00:10 +10004235 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11004236 if (!!(cfg->process_table & PATB_GR) != radix)
Paul Mackerras468808b2017-01-30 21:21:42 +11004237 return -EINVAL;
4238
4239 /* Process table size field must be reasonable, i.e. <= 24 */
4240 if ((cfg->process_table & PRTS_MASK) > 24)
4241 return -EINVAL;
4242
Paul Mackerras18c36402017-09-13 16:00:10 +10004243 /* We can change a guest to/from radix now, if the host is radix */
4244 if (radix && !radix_enabled())
4245 return -EINVAL;
4246
Paul Mackerrascf5f6f32017-09-11 16:05:30 +10004247 mutex_lock(&kvm->lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10004248 if (radix != kvm_is_radix(kvm)) {
4249 if (kvm->arch.mmu_ready) {
4250 kvm->arch.mmu_ready = 0;
4251 /* order mmu_ready vs. vcpus_running */
4252 smp_mb();
4253 if (atomic_read(&kvm->arch.vcpus_running)) {
4254 kvm->arch.mmu_ready = 1;
4255 err = -EBUSY;
4256 goto out_unlock;
4257 }
4258 }
4259 if (radix)
4260 err = kvmppc_switch_mmu_to_radix(kvm);
4261 else
4262 err = kvmppc_switch_mmu_to_hpt(kvm);
4263 if (err)
4264 goto out_unlock;
4265 }
4266
Paul Mackerras468808b2017-01-30 21:21:42 +11004267 kvm->arch.process_table = cfg->process_table;
4268 kvmppc_setup_partition_table(kvm);
4269
4270 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
4271 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
Paul Mackerras18c36402017-09-13 16:00:10 +10004272 err = 0;
Paul Mackerras468808b2017-01-30 21:21:42 +11004273
Paul Mackerras18c36402017-09-13 16:00:10 +10004274 out_unlock:
4275 mutex_unlock(&kvm->lock);
4276 return err;
Paul Mackerrasc9270132017-01-30 21:21:41 +11004277}
4278
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304279static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304280 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
4281 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
4282 .get_one_reg = kvmppc_get_one_reg_hv,
4283 .set_one_reg = kvmppc_set_one_reg_hv,
4284 .vcpu_load = kvmppc_core_vcpu_load_hv,
4285 .vcpu_put = kvmppc_core_vcpu_put_hv,
4286 .set_msr = kvmppc_set_msr_hv,
4287 .vcpu_run = kvmppc_vcpu_run_hv,
4288 .vcpu_create = kvmppc_core_vcpu_create_hv,
4289 .vcpu_free = kvmppc_core_vcpu_free_hv,
4290 .check_requests = kvmppc_core_check_requests_hv,
4291 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
4292 .flush_memslot = kvmppc_core_flush_memslot_hv,
4293 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
4294 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
4295 .unmap_hva = kvm_unmap_hva_hv,
4296 .unmap_hva_range = kvm_unmap_hva_range_hv,
4297 .age_hva = kvm_age_hva_hv,
4298 .test_age_hva = kvm_test_age_hva_hv,
4299 .set_spte_hva = kvm_set_spte_hva_hv,
4300 .mmu_destroy = kvmppc_mmu_destroy_hv,
4301 .free_memslot = kvmppc_core_free_memslot_hv,
4302 .create_memslot = kvmppc_core_create_memslot_hv,
4303 .init_vm = kvmppc_core_init_vm_hv,
4304 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304305 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
4306 .emulate_op = kvmppc_core_emulate_op_hv,
4307 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
4308 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
4309 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
4310 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10004311 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10004312#ifdef CONFIG_KVM_XICS
4313 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
4314 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
4315#endif
Paul Mackerrasc9270132017-01-30 21:21:41 +11004316 .configure_mmu = kvmhv_configure_mmu,
4317 .get_rmmu_info = kvmhv_get_rmmu_info,
Paul Mackerras3c313522017-02-06 13:24:41 +11004318 .set_smt_mode = kvmhv_set_smt_mode,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304319};
4320
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05304321static int kvm_init_subcore_bitmap(void)
4322{
4323 int i, j;
4324 int nr_cores = cpu_nr_cores();
4325 struct sibling_subcore_state *sibling_subcore_state;
4326
4327 for (i = 0; i < nr_cores; i++) {
4328 int first_cpu = i * threads_per_core;
4329 int node = cpu_to_node(first_cpu);
4330
4331 /* Ignore if it is already allocated. */
4332 if (paca[first_cpu].sibling_subcore_state)
4333 continue;
4334
4335 sibling_subcore_state =
4336 kmalloc_node(sizeof(struct sibling_subcore_state),
4337 GFP_KERNEL, node);
4338 if (!sibling_subcore_state)
4339 return -ENOMEM;
4340
4341 memset(sibling_subcore_state, 0,
4342 sizeof(struct sibling_subcore_state));
4343
4344 for (j = 0; j < threads_per_core; j++) {
4345 int cpu = first_cpu + j;
4346
4347 paca[cpu].sibling_subcore_state = sibling_subcore_state;
4348 }
4349 }
4350 return 0;
4351}
4352
Paul Mackerras5a319352017-01-30 21:21:46 +11004353static int kvmppc_radix_possible(void)
4354{
4355 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
4356}
4357
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304358static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00004359{
4360 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304361 /*
4362 * FIXME!! Do we need to check on all cpus ?
4363 */
4364 r = kvmppc_core_check_processor_compat_hv();
4365 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11004366 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004367
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05304368 r = kvm_init_subcore_bitmap();
4369 if (r)
4370 return r;
4371
Paul Mackerrasf7257582016-11-18 09:02:08 +11004372 /*
4373 * We need a way of accessing the XICS interrupt controller,
4374 * either directly, via paca[cpu].kvm_hstate.xics_phys, or
4375 * indirectly, via OPAL.
4376 */
4377#ifdef CONFIG_SMP
Paul Mackerrasfb7dcf72017-04-28 08:23:16 +10004378 if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +11004379 struct device_node *np;
4380
4381 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
4382 if (!np) {
4383 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
4384 return -ENODEV;
4385 }
4386 }
4387#endif
4388
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304389 kvm_ops_hv.owner = THIS_MODULE;
4390 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004391
Paul Mackerras699a0ea2014-06-02 11:02:59 +10004392 init_default_hcalls();
4393
Paul Mackerrasec257162015-06-24 21:18:03 +10004394 init_vcore_lists();
4395
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304396 r = kvmppc_mmu_hv_init();
Paul Mackerras5a319352017-01-30 21:21:46 +11004397 if (r)
4398 return r;
4399
4400 if (kvmppc_radix_possible())
4401 r = kvmppc_radix_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00004402 return r;
4403}
4404
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304405static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00004406{
Suresh Warrier79b6c242015-12-17 14:59:06 -06004407 kvmppc_free_host_rm_ops();
Paul Mackerras5a319352017-01-30 21:21:46 +11004408 if (kvmppc_radix_possible())
4409 kvmppc_radix_exit();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05304410 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004411}
4412
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304413module_init(kvmppc_book3s_init_hv);
4414module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05304415MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01004416MODULE_ALIAS_MISCDEV(KVM_MINOR);
4417MODULE_ALIAS("devname:kvm");