blob: 30ff8ab5aba1f85421cdce694330d15e2555e208 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100030#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000031#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000032#include <linux/spinlock.h>
33#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000034#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010035#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110036#include <linux/debugfs.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000037
38#include <asm/reg.h>
39#include <asm/cputable.h>
40#include <asm/cacheflush.h>
41#include <asm/tlbflush.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000049#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000051#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000053#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110054#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053055#include <asm/hmi.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000056#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057#include <linux/vmalloc.h>
58#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000059#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053060#include <linux/module.h>
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +100061#include <linux/compiler.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000062
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053063#include "book3s.h"
64
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060065#define CREATE_TRACE_POINTS
66#include "trace_hv.h"
67
Paul Mackerrasde56a942011-06-29 00:21:34 +000068/* #define EXIT_DEBUG */
69/* #define EXIT_DEBUG_SIMPLE */
70/* #define EXIT_DEBUG_INT */
71
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000072/* Used to indicate that a guest page fault needs to be handled */
73#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
74
Paul Mackerrasc7b67672012-10-15 01:18:07 +000075/* Used as a "null" value for timebase values */
76#define TB_NIL (~(u64)0)
77
Paul Mackerras699a0ea2014-06-02 11:02:59 +100078static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
79
Paul Mackerrasb4deba52015-07-02 20:38:16 +100080static int dynamic_mt_modes = 6;
81module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
82MODULE_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 +100083static int target_smt_mode;
84module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100086
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -060087#ifdef CONFIG_KVM_XICS
88static struct kernel_param_ops module_param_ops = {
89 .set = param_set_int,
90 .get = param_get_int,
91};
92
93module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
94 S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
96#endif
97
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +100098/* Maximum halt poll interval defaults to KVM_HALT_POLL_NS_DEFAULT */
99static unsigned int halt_poll_max_ns = KVM_HALT_POLL_NS_DEFAULT;
100module_param(halt_poll_max_ns, uint, S_IRUGO | S_IWUSR);
101MODULE_PARM_DESC(halt_poll_max_ns, "Maximum halt poll time in ns");
102
103/* Factor by which the vcore halt poll interval is grown, default is to double
104 */
105static unsigned int halt_poll_ns_grow = 2;
106module_param(halt_poll_ns_grow, int, S_IRUGO);
107MODULE_PARM_DESC(halt_poll_ns_grow, "Factor halt poll time is grown by");
108
109/* Factor by which the vcore halt poll interval is shrunk, default is to reset
110 */
111static unsigned int halt_poll_ns_shrink;
112module_param(halt_poll_ns_shrink, int, S_IRUGO);
113MODULE_PARM_DESC(halt_poll_ns_shrink, "Factor halt poll time is shrunk by");
114
Paul Mackerras19ccb762011-07-23 17:42:46 +1000115static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +0000116static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000117
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000118static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
119 int *ip)
120{
121 int i = *ip;
122 struct kvm_vcpu *vcpu;
123
124 while (++i < MAX_SMT_THREADS) {
125 vcpu = READ_ONCE(vc->runnable_threads[i]);
126 if (vcpu) {
127 *ip = i;
128 return vcpu;
129 }
130 }
131 return NULL;
132}
133
134/* Used to traverse the list of runnable threads for a given vcore */
135#define for_each_runnable_thread(i, vcpu, vc) \
136 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
137
Paul Mackerras66feed62015-03-28 14:21:12 +1100138static bool kvmppc_ipi_thread(int cpu)
139{
140 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
141 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
142 preempt_disable();
143 if (cpu_first_thread_sibling(cpu) ==
144 cpu_first_thread_sibling(smp_processor_id())) {
145 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
146 msg |= cpu_thread_in_core(cpu);
147 smp_mb();
148 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
149 preempt_enable();
150 return true;
151 }
152 preempt_enable();
153 }
154
155#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
156 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
157 xics_wake_cpu(cpu);
158 return true;
159 }
160#endif
161
162 return false;
163}
164
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530165static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000166{
Paul Mackerrasec257162015-06-24 21:18:03 +1000167 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100168 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000169
170 wqp = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100171 if (swait_active(wqp)) {
172 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000173 ++vcpu->stat.halt_wakeup;
174 }
175
Paul Mackerrasec257162015-06-24 21:18:03 +1000176 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100177 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000178
179 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000180 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100181 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
182 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000183}
184
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000185/*
186 * We use the vcpu_load/put functions to measure stolen time.
187 * Stolen time is counted as time when either the vcpu is able to
188 * run as part of a virtual core, but the task running the vcore
189 * is preempted or sleeping, or when the vcpu needs something done
190 * in the kernel by the task running the vcpu, but that task is
191 * preempted or sleeping. Those two things have to be counted
192 * separately, since one of the vcpu tasks will take on the job
193 * of running the core, and the other vcpu tasks in the vcore will
194 * sleep waiting for it to do that, but that sleep shouldn't count
195 * as stolen time.
196 *
197 * Hence we accumulate stolen time when the vcpu can run as part of
198 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
199 * needs its task to do other things in the kernel (for example,
200 * service a page fault) in busy_stolen. We don't accumulate
201 * stolen time for a vcore when it is inactive, or for a vcpu
202 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
203 * a misnomer; it means that the vcpu task is not executing in
204 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
205 * the kernel. We don't have any way of dividing up that time
206 * between time that the vcpu is genuinely stopped, time that
207 * the task is actively working on behalf of the vcpu, and time
208 * that the task is preempted, so we don't count any of it as
209 * stolen.
210 *
211 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100212 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
213 * lock. The stolen times are measured in units of timebase ticks.
214 * (Note that the != TB_NIL checks below are purely defensive;
215 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000216 */
217
Paul Mackerrasec257162015-06-24 21:18:03 +1000218static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
219{
220 unsigned long flags;
221
222 spin_lock_irqsave(&vc->stoltb_lock, flags);
223 vc->preempt_tb = mftb();
224 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
225}
226
227static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
228{
229 unsigned long flags;
230
231 spin_lock_irqsave(&vc->stoltb_lock, flags);
232 if (vc->preempt_tb != TB_NIL) {
233 vc->stolen_tb += mftb() - vc->preempt_tb;
234 vc->preempt_tb = TB_NIL;
235 }
236 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
237}
238
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530239static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000240{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000241 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100242 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000243
Paul Mackerras2711e242014-12-04 16:43:28 +1100244 /*
245 * We can test vc->runner without taking the vcore lock,
246 * because only this task ever sets vc->runner to this
247 * vcpu, and once it is set to this vcpu, only this task
248 * ever sets it to NULL.
249 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000250 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
251 kvmppc_core_end_stolen(vc);
252
Paul Mackerras2711e242014-12-04 16:43:28 +1100253 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000254 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
255 vcpu->arch.busy_preempt != TB_NIL) {
256 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
257 vcpu->arch.busy_preempt = TB_NIL;
258 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100259 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000260}
261
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530262static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000263{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000264 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100265 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000266
Paul Mackerrasec257162015-06-24 21:18:03 +1000267 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
268 kvmppc_core_start_stolen(vc);
269
Paul Mackerras2711e242014-12-04 16:43:28 +1100270 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000271 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
272 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100273 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000274}
275
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530276static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000277{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100278 /*
279 * Check for illegal transactional state bit combination
280 * and if we find it, force the TS field to a safe state.
281 */
282 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
283 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000284 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000285 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000286}
287
Thomas Huth5358a962015-05-22 09:25:02 +0200288static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000289{
290 vcpu->arch.pvr = pvr;
291}
292
Thomas Huth5358a962015-05-22 09:25:02 +0200293static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000294{
295 unsigned long pcr = 0;
296 struct kvmppc_vcore *vc = vcpu->arch.vcore;
297
298 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000299 switch (arch_compat) {
300 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100301 /*
302 * If an arch bit is set in PCR, all the defined
303 * higher-order arch bits also have to be set.
304 */
305 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000306 break;
307 case PVR_ARCH_206:
308 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100309 pcr = PCR_ARCH_206;
310 break;
311 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000312 break;
313 default:
314 return -EINVAL;
315 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100316
317 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
318 /* POWER7 can't emulate POWER8 */
319 if (!(pcr & PCR_ARCH_206))
320 return -EINVAL;
321 pcr &= ~PCR_ARCH_206;
322 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000323 }
324
325 spin_lock(&vc->lock);
326 vc->arch_compat = arch_compat;
327 vc->pcr = pcr;
328 spin_unlock(&vc->lock);
329
330 return 0;
331}
332
Thomas Huth5358a962015-05-22 09:25:02 +0200333static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000334{
335 int r;
336
337 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
338 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
339 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
340 for (r = 0; r < 16; ++r)
341 pr_err("r%2d = %.16lx r%d = %.16lx\n",
342 r, kvmppc_get_gpr(vcpu, r),
343 r+16, kvmppc_get_gpr(vcpu, r+16));
344 pr_err("ctr = %.16lx lr = %.16lx\n",
345 vcpu->arch.ctr, vcpu->arch.lr);
346 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
347 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
348 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
349 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
350 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
351 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
352 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
353 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
354 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
355 pr_err("fault dar = %.16lx dsisr = %.8x\n",
356 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
357 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
358 for (r = 0; r < vcpu->arch.slb_max; ++r)
359 pr_err(" ESID = %.16llx VSID = %.16llx\n",
360 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
361 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000362 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000363 vcpu->arch.last_inst);
364}
365
Thomas Huth5358a962015-05-22 09:25:02 +0200366static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000367{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100368 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000369
370 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100371 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000372 mutex_unlock(&kvm->lock);
373 return ret;
374}
375
376static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
377{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000378 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200379 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000380}
381
Paul Mackerras55b665b2012-09-25 20:33:06 +0000382static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
383 unsigned long addr, unsigned long len)
384{
385 /* check address is cacheline aligned */
386 if (addr & (L1_CACHE_BYTES - 1))
387 return -EINVAL;
388 spin_lock(&vcpu->arch.vpa_update_lock);
389 if (v->next_gpa != addr || v->len != len) {
390 v->next_gpa = addr;
391 v->len = addr ? len : 0;
392 v->update_pending = 1;
393 }
394 spin_unlock(&vcpu->arch.vpa_update_lock);
395 return 0;
396}
397
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000398/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
399struct reg_vpa {
400 u32 dummy;
401 union {
Alexander Graf02407552014-06-11 10:34:19 +0200402 __be16 hword;
403 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000404 } length;
405};
406
407static int vpa_is_registered(struct kvmppc_vpa *vpap)
408{
409 if (vpap->update_pending)
410 return vpap->next_gpa != 0;
411 return vpap->pinned_addr != NULL;
412}
413
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000414static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
415 unsigned long flags,
416 unsigned long vcpuid, unsigned long vpa)
417{
418 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000419 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000420 void *va;
421 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000422 int err;
423 int subfunc;
424 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000425
426 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
427 if (!tvcpu)
428 return H_PARAMETER;
429
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000430 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
431 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
432 subfunc == H_VPA_REG_SLB) {
433 /* Registering new area - address must be cache-line aligned */
434 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000435 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000436
437 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000438 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
439 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000440 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000441 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200442 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000443 else
Alexander Graf02407552014-06-11 10:34:19 +0200444 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000445 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000446
447 /* Check length */
448 if (len > nb || len < sizeof(struct reg_vpa))
449 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000450 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000451 vpa = 0;
452 len = 0;
453 }
454
455 err = H_PARAMETER;
456 vpap = NULL;
457 spin_lock(&tvcpu->arch.vpa_update_lock);
458
459 switch (subfunc) {
460 case H_VPA_REG_VPA: /* register VPA */
461 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000462 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000463 vpap = &tvcpu->arch.vpa;
464 err = 0;
465 break;
466
467 case H_VPA_REG_DTL: /* register DTL */
468 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000469 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000470 len -= len % sizeof(struct dtl_entry);
471
472 /* Check that they have previously registered a VPA */
473 err = H_RESOURCE;
474 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000475 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000476
477 vpap = &tvcpu->arch.dtl;
478 err = 0;
479 break;
480
481 case H_VPA_REG_SLB: /* register SLB shadow buffer */
482 /* Check that they have previously registered a VPA */
483 err = H_RESOURCE;
484 if (!vpa_is_registered(&tvcpu->arch.vpa))
485 break;
486
487 vpap = &tvcpu->arch.slb_shadow;
488 err = 0;
489 break;
490
491 case H_VPA_DEREG_VPA: /* deregister VPA */
492 /* Check they don't still have a DTL or SLB buf registered */
493 err = H_RESOURCE;
494 if (vpa_is_registered(&tvcpu->arch.dtl) ||
495 vpa_is_registered(&tvcpu->arch.slb_shadow))
496 break;
497
498 vpap = &tvcpu->arch.vpa;
499 err = 0;
500 break;
501
502 case H_VPA_DEREG_DTL: /* deregister DTL */
503 vpap = &tvcpu->arch.dtl;
504 err = 0;
505 break;
506
507 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
508 vpap = &tvcpu->arch.slb_shadow;
509 err = 0;
510 break;
511 }
512
513 if (vpap) {
514 vpap->next_gpa = vpa;
515 vpap->len = len;
516 vpap->update_pending = 1;
517 }
518
519 spin_unlock(&tvcpu->arch.vpa_update_lock);
520
521 return err;
522}
523
Paul Mackerras081f3232012-06-01 20:20:24 +1000524static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000525{
Paul Mackerras081f3232012-06-01 20:20:24 +1000526 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000527 void *va;
528 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000529 unsigned long gpa;
530
531 /*
532 * We need to pin the page pointed to by vpap->next_gpa,
533 * but we can't call kvmppc_pin_guest_page under the lock
534 * as it does get_user_pages() and down_read(). So we
535 * have to drop the lock, pin the page, then get the lock
536 * again and check that a new area didn't get registered
537 * in the meantime.
538 */
539 for (;;) {
540 gpa = vpap->next_gpa;
541 spin_unlock(&vcpu->arch.vpa_update_lock);
542 va = NULL;
543 nb = 0;
544 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000545 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000546 spin_lock(&vcpu->arch.vpa_update_lock);
547 if (gpa == vpap->next_gpa)
548 break;
549 /* sigh... unpin that one and try again */
550 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000551 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000552 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000553
554 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000555 if (va && nb < vpap->len) {
556 /*
557 * If it's now too short, it must be that userspace
558 * has changed the mappings underlying guest memory,
559 * so unregister the region.
560 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000561 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000562 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000563 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000564 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000565 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
566 vpap->dirty);
567 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000568 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000569 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000570 if (va)
571 vpap->pinned_end = va + vpap->len;
572}
Paul Mackerras93e60242011-12-12 12:28:55 +0000573
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000574static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
575{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000576 if (!(vcpu->arch.vpa.update_pending ||
577 vcpu->arch.slb_shadow.update_pending ||
578 vcpu->arch.dtl.update_pending))
579 return;
580
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000581 spin_lock(&vcpu->arch.vpa_update_lock);
582 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000583 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000584 if (vcpu->arch.vpa.pinned_addr)
585 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000586 }
587 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000588 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000589 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
590 vcpu->arch.dtl_index = 0;
591 }
592 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000593 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000594 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000595}
596
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000597/*
598 * Return the accumulated stolen time for the vcore up until `now'.
599 * The caller should hold the vcore lock.
600 */
601static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
602{
603 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100604 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000605
Paul Mackerras2711e242014-12-04 16:43:28 +1100606 spin_lock_irqsave(&vc->stoltb_lock, flags);
607 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000608 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100609 vc->preempt_tb != TB_NIL)
610 p += now - vc->preempt_tb;
611 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000612 return p;
613}
614
Paul Mackerras0456ec42012-02-03 00:56:21 +0000615static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
616 struct kvmppc_vcore *vc)
617{
618 struct dtl_entry *dt;
619 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000620 unsigned long stolen;
621 unsigned long core_stolen;
622 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000623
624 dt = vcpu->arch.dtl_ptr;
625 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000626 now = mftb();
627 core_stolen = vcore_stolen_time(vc, now);
628 stolen = core_stolen - vcpu->arch.stolen_logged;
629 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100630 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000631 stolen += vcpu->arch.busy_stolen;
632 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100633 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000634 if (!dt || !vpa)
635 return;
636 memset(dt, 0, sizeof(struct dtl_entry));
637 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200638 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
639 dt->timebase = cpu_to_be64(now + vc->tb_offset);
640 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
641 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
642 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000643 ++dt;
644 if (dt == vcpu->arch.dtl.pinned_end)
645 dt = vcpu->arch.dtl.pinned_addr;
646 vcpu->arch.dtl_ptr = dt;
647 /* order writing *dt vs. writing vpa->dtl_idx */
648 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200649 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000650 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000651}
652
Michael Neuling96423822014-06-02 11:03:01 +1000653static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
654{
655 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
656 return true;
657 if ((!vcpu->arch.vcore->arch_compat) &&
658 cpu_has_feature(CPU_FTR_ARCH_207S))
659 return true;
660 return false;
661}
662
663static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
664 unsigned long resource, unsigned long value1,
665 unsigned long value2)
666{
667 switch (resource) {
668 case H_SET_MODE_RESOURCE_SET_CIABR:
669 if (!kvmppc_power8_compatible(vcpu))
670 return H_P2;
671 if (value2)
672 return H_P4;
673 if (mflags)
674 return H_UNSUPPORTED_FLAG_START;
675 /* Guests can't breakpoint the hypervisor */
676 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
677 return H_P3;
678 vcpu->arch.ciabr = value1;
679 return H_SUCCESS;
680 case H_SET_MODE_RESOURCE_SET_DAWR:
681 if (!kvmppc_power8_compatible(vcpu))
682 return H_P2;
683 if (mflags)
684 return H_UNSUPPORTED_FLAG_START;
685 if (value2 & DABRX_HYP)
686 return H_P4;
687 vcpu->arch.dawr = value1;
688 vcpu->arch.dawrx = value2;
689 return H_SUCCESS;
690 default:
691 return H_TOO_HARD;
692 }
693}
694
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100695static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
696{
697 struct kvmppc_vcore *vcore = target->arch.vcore;
698
699 /*
700 * We expect to have been called by the real mode handler
701 * (kvmppc_rm_h_confer()) which would have directly returned
702 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
703 * have useful work to do and should not confer) so we don't
704 * recheck that here.
705 */
706
707 spin_lock(&vcore->lock);
708 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000709 vcore->vcore_state != VCORE_INACTIVE &&
710 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100711 target = vcore->runner;
712 spin_unlock(&vcore->lock);
713
714 return kvm_vcpu_yield_to(target);
715}
716
717static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
718{
719 int yield_count = 0;
720 struct lppaca *lppaca;
721
722 spin_lock(&vcpu->arch.vpa_update_lock);
723 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
724 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100725 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100726 spin_unlock(&vcpu->arch.vpa_update_lock);
727 return yield_count;
728}
729
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000730int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
731{
732 unsigned long req = kvmppc_get_gpr(vcpu, 3);
733 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100734 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000735 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000736 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000737
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000738 if (req <= MAX_HCALL_OPCODE &&
739 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
740 return RESUME_HOST;
741
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000742 switch (req) {
743 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000744 break;
745 case H_PROD:
746 target = kvmppc_get_gpr(vcpu, 4);
747 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
748 if (!tvcpu) {
749 ret = H_PARAMETER;
750 break;
751 }
752 tvcpu->arch.prodded = 1;
753 smp_mb();
754 if (vcpu->arch.ceded) {
Marcelo Tosatti85773702016-02-19 09:46:39 +0100755 if (swait_active(&vcpu->wq)) {
756 swake_up(&vcpu->wq);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000757 vcpu->stat.halt_wakeup++;
758 }
759 }
760 break;
761 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000762 target = kvmppc_get_gpr(vcpu, 4);
763 if (target == -1)
764 break;
765 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
766 if (!tvcpu) {
767 ret = H_PARAMETER;
768 break;
769 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100770 yield_count = kvmppc_get_gpr(vcpu, 5);
771 if (kvmppc_get_yield_count(tvcpu) != yield_count)
772 break;
773 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000774 break;
775 case H_REGISTER_VPA:
776 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
777 kvmppc_get_gpr(vcpu, 5),
778 kvmppc_get_gpr(vcpu, 6));
779 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000780 case H_RTAS:
781 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
782 return RESUME_HOST;
783
Paul Mackerrasc9438092013-11-16 17:46:05 +1100784 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000785 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100786 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000787
788 if (rc == -ENOENT)
789 return RESUME_HOST;
790 else if (rc == 0)
791 break;
792
793 /* Send the error out to userspace via KVM_RUN */
794 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100795 case H_LOGICAL_CI_LOAD:
796 ret = kvmppc_h_logical_ci_load(vcpu);
797 if (ret == H_TOO_HARD)
798 return RESUME_HOST;
799 break;
800 case H_LOGICAL_CI_STORE:
801 ret = kvmppc_h_logical_ci_store(vcpu);
802 if (ret == H_TOO_HARD)
803 return RESUME_HOST;
804 break;
Michael Neuling96423822014-06-02 11:03:01 +1000805 case H_SET_MODE:
806 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
807 kvmppc_get_gpr(vcpu, 5),
808 kvmppc_get_gpr(vcpu, 6),
809 kvmppc_get_gpr(vcpu, 7));
810 if (ret == H_TOO_HARD)
811 return RESUME_HOST;
812 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000813 case H_XIRR:
814 case H_CPPR:
815 case H_EOI:
816 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000817 case H_IPOLL:
818 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000819 if (kvmppc_xics_enabled(vcpu)) {
820 ret = kvmppc_xics_hcall(vcpu, req);
821 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100822 }
823 return RESUME_HOST;
824 case H_PUT_TCE:
825 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
826 kvmppc_get_gpr(vcpu, 5),
827 kvmppc_get_gpr(vcpu, 6));
828 if (ret == H_TOO_HARD)
829 return RESUME_HOST;
830 break;
831 case H_PUT_TCE_INDIRECT:
832 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
833 kvmppc_get_gpr(vcpu, 5),
834 kvmppc_get_gpr(vcpu, 6),
835 kvmppc_get_gpr(vcpu, 7));
836 if (ret == H_TOO_HARD)
837 return RESUME_HOST;
838 break;
839 case H_STUFF_TCE:
840 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
841 kvmppc_get_gpr(vcpu, 5),
842 kvmppc_get_gpr(vcpu, 6),
843 kvmppc_get_gpr(vcpu, 7));
844 if (ret == H_TOO_HARD)
845 return RESUME_HOST;
846 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000847 default:
848 return RESUME_HOST;
849 }
850 kvmppc_set_gpr(vcpu, 3, ret);
851 vcpu->arch.hcall_needed = 0;
852 return RESUME_GUEST;
853}
854
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000855static int kvmppc_hcall_impl_hv(unsigned long cmd)
856{
857 switch (cmd) {
858 case H_CEDE:
859 case H_PROD:
860 case H_CONFER:
861 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000862 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100863 case H_LOGICAL_CI_LOAD:
864 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000865#ifdef CONFIG_KVM_XICS
866 case H_XIRR:
867 case H_CPPR:
868 case H_EOI:
869 case H_IPI:
870 case H_IPOLL:
871 case H_XIRR_X:
872#endif
873 return 1;
874 }
875
876 /* See if it's in the real-mode table */
877 return kvmppc_hcall_impl_hv_realmode(cmd);
878}
879
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530880static int kvmppc_emulate_debug_inst(struct kvm_run *run,
881 struct kvm_vcpu *vcpu)
882{
883 u32 last_inst;
884
885 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
886 EMULATE_DONE) {
887 /*
888 * Fetch failed, so return to guest and
889 * try executing it again.
890 */
891 return RESUME_GUEST;
892 }
893
894 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
895 run->exit_reason = KVM_EXIT_DEBUG;
896 run->debug.arch.address = kvmppc_get_pc(vcpu);
897 return RESUME_HOST;
898 } else {
899 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
900 return RESUME_GUEST;
901 }
902}
903
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530904static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
905 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000906{
907 int r = RESUME_HOST;
908
909 vcpu->stat.sum_exits++;
910
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100911 /*
912 * This can happen if an interrupt occurs in the last stages
913 * of guest entry or the first stages of guest exit (i.e. after
914 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
915 * and before setting it to KVM_GUEST_MODE_HOST_HV).
916 * That can happen due to a bug, or due to a machine check
917 * occurring at just the wrong time.
918 */
919 if (vcpu->arch.shregs.msr & MSR_HV) {
920 printk(KERN_EMERG "KVM trap in HV mode!\n");
921 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
922 vcpu->arch.trap, kvmppc_get_pc(vcpu),
923 vcpu->arch.shregs.msr);
924 kvmppc_dump_regs(vcpu);
925 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
926 run->hw.hardware_exit_reason = vcpu->arch.trap;
927 return RESUME_HOST;
928 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000929 run->exit_reason = KVM_EXIT_UNKNOWN;
930 run->ready_for_interrupt_injection = 1;
931 switch (vcpu->arch.trap) {
932 /* We're good on these - the host merely wanted to get our attention */
933 case BOOK3S_INTERRUPT_HV_DECREMENTER:
934 vcpu->stat.dec_exits++;
935 r = RESUME_GUEST;
936 break;
937 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100938 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000939 vcpu->stat.ext_intr_exits++;
940 r = RESUME_GUEST;
941 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100942 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
943 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000944 case BOOK3S_INTERRUPT_PERFMON:
945 r = RESUME_GUEST;
946 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000947 case BOOK3S_INTERRUPT_MACHINE_CHECK:
948 /*
949 * Deliver a machine check interrupt to the guest.
950 * We have to do this, even if the host has handled the
951 * machine check, because machine checks use SRR0/1 and
952 * the interrupt might have trashed guest state in them.
953 */
954 kvmppc_book3s_queue_irqprio(vcpu,
955 BOOK3S_INTERRUPT_MACHINE_CHECK);
956 r = RESUME_GUEST;
957 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000958 case BOOK3S_INTERRUPT_PROGRAM:
959 {
960 ulong flags;
961 /*
962 * Normally program interrupts are delivered directly
963 * to the guest by the hardware, but we can get here
964 * as a result of a hypervisor emulation interrupt
965 * (e40) getting turned into a 700 by BML RTAS.
966 */
967 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
968 kvmppc_core_queue_program(vcpu, flags);
969 r = RESUME_GUEST;
970 break;
971 }
972 case BOOK3S_INTERRUPT_SYSCALL:
973 {
974 /* hcall - punt to userspace */
975 int i;
976
Liu Ping Fan27025a62013-11-19 14:12:48 +0800977 /* hypercall with MSR_PR has already been handled in rmode,
978 * and never reaches here.
979 */
980
Paul Mackerrasde56a942011-06-29 00:21:34 +0000981 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
982 for (i = 0; i < 9; ++i)
983 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
984 run->exit_reason = KVM_EXIT_PAPR_HCALL;
985 vcpu->arch.hcall_needed = 1;
986 r = RESUME_HOST;
987 break;
988 }
989 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000990 * We get these next two if the guest accesses a page which it thinks
991 * it has mapped but which is not actually present, either because
992 * it is for an emulated I/O device or because the corresonding
993 * host page has been paged out. Any other HDSI/HISI interrupts
994 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000995 */
996 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000997 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000998 break;
999 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001000 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1001 vcpu->arch.fault_dsisr = 0;
1002 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001003 break;
1004 /*
1005 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301006 * If the guest debug is disabled, generate a program interrupt
1007 * to the guest. If guest debug is enabled, we need to check
1008 * whether the instruction is a software breakpoint instruction.
1009 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001010 */
1011 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001012 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1013 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1014 swab32(vcpu->arch.emul_inst) :
1015 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301016 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1017 r = kvmppc_emulate_debug_inst(run, vcpu);
1018 } else {
1019 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1020 r = RESUME_GUEST;
1021 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001022 break;
1023 /*
1024 * This occurs if the guest (kernel or userspace), does something that
1025 * is prohibited by HFSCR. We just generate a program interrupt to
1026 * the guest.
1027 */
1028 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1029 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001030 r = RESUME_GUEST;
1031 break;
1032 default:
1033 kvmppc_dump_regs(vcpu);
1034 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1035 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1036 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001037 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001038 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001039 break;
1040 }
1041
Paul Mackerrasde56a942011-06-29 00:21:34 +00001042 return r;
1043}
1044
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301045static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1046 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001047{
1048 int i;
1049
Paul Mackerrasde56a942011-06-29 00:21:34 +00001050 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301051 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001052 for (i = 0; i < vcpu->arch.slb_max; i++) {
1053 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1054 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1055 }
1056
1057 return 0;
1058}
1059
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301060static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1061 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001062{
1063 int i, j;
1064
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001065 /* Only accept the same PVR as the host's, since we can't spoof it */
1066 if (sregs->pvr != vcpu->arch.pvr)
1067 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068
1069 j = 0;
1070 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1071 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1072 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1073 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1074 ++j;
1075 }
1076 }
1077 vcpu->arch.slb_max = j;
1078
1079 return 0;
1080}
1081
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001082static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1083 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001084{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001085 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001086 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1087 u64 mask;
1088
Paul Mackerras8f902b02015-03-20 20:39:38 +11001089 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001090 spin_lock(&vc->lock);
1091 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001092 * If ILE (interrupt little-endian) has changed, update the
1093 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1094 */
1095 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001096 struct kvm_vcpu *vcpu;
1097 int i;
1098
Anton Blanchardd6829162014-01-08 21:25:30 +11001099 kvm_for_each_vcpu(i, vcpu, kvm) {
1100 if (vcpu->arch.vcore != vc)
1101 continue;
1102 if (new_lpcr & LPCR_ILE)
1103 vcpu->arch.intr_msr |= MSR_LE;
1104 else
1105 vcpu->arch.intr_msr &= ~MSR_LE;
1106 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001107 }
1108
1109 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001110 * Userspace can only modify DPFD (default prefetch depth),
1111 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001112 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001113 */
1114 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001115 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1116 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001117
1118 /* Broken 32-bit version of LPCR must not clear top bits */
1119 if (preserve_top32)
1120 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001121 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1122 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001123 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001124}
1125
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301126static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1127 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001128{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001129 int r = 0;
1130 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001131
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001132 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301133 case KVM_REG_PPC_DEBUG_INST:
1134 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1135 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001136 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001137 *val = get_reg_val(id, 0);
1138 break;
1139 case KVM_REG_PPC_DABR:
1140 *val = get_reg_val(id, vcpu->arch.dabr);
1141 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001142 case KVM_REG_PPC_DABRX:
1143 *val = get_reg_val(id, vcpu->arch.dabrx);
1144 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001145 case KVM_REG_PPC_DSCR:
1146 *val = get_reg_val(id, vcpu->arch.dscr);
1147 break;
1148 case KVM_REG_PPC_PURR:
1149 *val = get_reg_val(id, vcpu->arch.purr);
1150 break;
1151 case KVM_REG_PPC_SPURR:
1152 *val = get_reg_val(id, vcpu->arch.spurr);
1153 break;
1154 case KVM_REG_PPC_AMR:
1155 *val = get_reg_val(id, vcpu->arch.amr);
1156 break;
1157 case KVM_REG_PPC_UAMOR:
1158 *val = get_reg_val(id, vcpu->arch.uamor);
1159 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001160 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001161 i = id - KVM_REG_PPC_MMCR0;
1162 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1163 break;
1164 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1165 i = id - KVM_REG_PPC_PMC1;
1166 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001167 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001168 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1169 i = id - KVM_REG_PPC_SPMC1;
1170 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1171 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001172 case KVM_REG_PPC_SIAR:
1173 *val = get_reg_val(id, vcpu->arch.siar);
1174 break;
1175 case KVM_REG_PPC_SDAR:
1176 *val = get_reg_val(id, vcpu->arch.sdar);
1177 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001178 case KVM_REG_PPC_SIER:
1179 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001180 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001181 case KVM_REG_PPC_IAMR:
1182 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001183 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001184 case KVM_REG_PPC_PSPB:
1185 *val = get_reg_val(id, vcpu->arch.pspb);
1186 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001187 case KVM_REG_PPC_DPDES:
1188 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1189 break;
1190 case KVM_REG_PPC_DAWR:
1191 *val = get_reg_val(id, vcpu->arch.dawr);
1192 break;
1193 case KVM_REG_PPC_DAWRX:
1194 *val = get_reg_val(id, vcpu->arch.dawrx);
1195 break;
1196 case KVM_REG_PPC_CIABR:
1197 *val = get_reg_val(id, vcpu->arch.ciabr);
1198 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001199 case KVM_REG_PPC_CSIGR:
1200 *val = get_reg_val(id, vcpu->arch.csigr);
1201 break;
1202 case KVM_REG_PPC_TACR:
1203 *val = get_reg_val(id, vcpu->arch.tacr);
1204 break;
1205 case KVM_REG_PPC_TCSCR:
1206 *val = get_reg_val(id, vcpu->arch.tcscr);
1207 break;
1208 case KVM_REG_PPC_PID:
1209 *val = get_reg_val(id, vcpu->arch.pid);
1210 break;
1211 case KVM_REG_PPC_ACOP:
1212 *val = get_reg_val(id, vcpu->arch.acop);
1213 break;
1214 case KVM_REG_PPC_WORT:
1215 *val = get_reg_val(id, vcpu->arch.wort);
1216 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001217 case KVM_REG_PPC_VPA_ADDR:
1218 spin_lock(&vcpu->arch.vpa_update_lock);
1219 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1220 spin_unlock(&vcpu->arch.vpa_update_lock);
1221 break;
1222 case KVM_REG_PPC_VPA_SLB:
1223 spin_lock(&vcpu->arch.vpa_update_lock);
1224 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1225 val->vpaval.length = vcpu->arch.slb_shadow.len;
1226 spin_unlock(&vcpu->arch.vpa_update_lock);
1227 break;
1228 case KVM_REG_PPC_VPA_DTL:
1229 spin_lock(&vcpu->arch.vpa_update_lock);
1230 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1231 val->vpaval.length = vcpu->arch.dtl.len;
1232 spin_unlock(&vcpu->arch.vpa_update_lock);
1233 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001234 case KVM_REG_PPC_TB_OFFSET:
1235 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1236 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001237 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001238 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001239 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1240 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001241 case KVM_REG_PPC_PPR:
1242 *val = get_reg_val(id, vcpu->arch.ppr);
1243 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001244#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1245 case KVM_REG_PPC_TFHAR:
1246 *val = get_reg_val(id, vcpu->arch.tfhar);
1247 break;
1248 case KVM_REG_PPC_TFIAR:
1249 *val = get_reg_val(id, vcpu->arch.tfiar);
1250 break;
1251 case KVM_REG_PPC_TEXASR:
1252 *val = get_reg_val(id, vcpu->arch.texasr);
1253 break;
1254 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1255 i = id - KVM_REG_PPC_TM_GPR0;
1256 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1257 break;
1258 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1259 {
1260 int j;
1261 i = id - KVM_REG_PPC_TM_VSR0;
1262 if (i < 32)
1263 for (j = 0; j < TS_FPRWIDTH; j++)
1264 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1265 else {
1266 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1267 val->vval = vcpu->arch.vr_tm.vr[i-32];
1268 else
1269 r = -ENXIO;
1270 }
1271 break;
1272 }
1273 case KVM_REG_PPC_TM_CR:
1274 *val = get_reg_val(id, vcpu->arch.cr_tm);
1275 break;
1276 case KVM_REG_PPC_TM_LR:
1277 *val = get_reg_val(id, vcpu->arch.lr_tm);
1278 break;
1279 case KVM_REG_PPC_TM_CTR:
1280 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1281 break;
1282 case KVM_REG_PPC_TM_FPSCR:
1283 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1284 break;
1285 case KVM_REG_PPC_TM_AMR:
1286 *val = get_reg_val(id, vcpu->arch.amr_tm);
1287 break;
1288 case KVM_REG_PPC_TM_PPR:
1289 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1290 break;
1291 case KVM_REG_PPC_TM_VRSAVE:
1292 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1293 break;
1294 case KVM_REG_PPC_TM_VSCR:
1295 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1296 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1297 else
1298 r = -ENXIO;
1299 break;
1300 case KVM_REG_PPC_TM_DSCR:
1301 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1302 break;
1303 case KVM_REG_PPC_TM_TAR:
1304 *val = get_reg_val(id, vcpu->arch.tar_tm);
1305 break;
1306#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001307 case KVM_REG_PPC_ARCH_COMPAT:
1308 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1309 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001310 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001311 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001312 break;
1313 }
1314
1315 return r;
1316}
1317
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301318static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1319 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001320{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001321 int r = 0;
1322 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001323 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001324
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001325 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001326 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001327 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001328 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001329 r = -EINVAL;
1330 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001331 case KVM_REG_PPC_DABR:
1332 vcpu->arch.dabr = set_reg_val(id, *val);
1333 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001334 case KVM_REG_PPC_DABRX:
1335 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1336 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001337 case KVM_REG_PPC_DSCR:
1338 vcpu->arch.dscr = set_reg_val(id, *val);
1339 break;
1340 case KVM_REG_PPC_PURR:
1341 vcpu->arch.purr = set_reg_val(id, *val);
1342 break;
1343 case KVM_REG_PPC_SPURR:
1344 vcpu->arch.spurr = set_reg_val(id, *val);
1345 break;
1346 case KVM_REG_PPC_AMR:
1347 vcpu->arch.amr = set_reg_val(id, *val);
1348 break;
1349 case KVM_REG_PPC_UAMOR:
1350 vcpu->arch.uamor = set_reg_val(id, *val);
1351 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001352 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001353 i = id - KVM_REG_PPC_MMCR0;
1354 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1355 break;
1356 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1357 i = id - KVM_REG_PPC_PMC1;
1358 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1359 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001360 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1361 i = id - KVM_REG_PPC_SPMC1;
1362 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1363 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001364 case KVM_REG_PPC_SIAR:
1365 vcpu->arch.siar = set_reg_val(id, *val);
1366 break;
1367 case KVM_REG_PPC_SDAR:
1368 vcpu->arch.sdar = set_reg_val(id, *val);
1369 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001370 case KVM_REG_PPC_SIER:
1371 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001372 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001373 case KVM_REG_PPC_IAMR:
1374 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001375 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001376 case KVM_REG_PPC_PSPB:
1377 vcpu->arch.pspb = set_reg_val(id, *val);
1378 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001379 case KVM_REG_PPC_DPDES:
1380 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1381 break;
1382 case KVM_REG_PPC_DAWR:
1383 vcpu->arch.dawr = set_reg_val(id, *val);
1384 break;
1385 case KVM_REG_PPC_DAWRX:
1386 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1387 break;
1388 case KVM_REG_PPC_CIABR:
1389 vcpu->arch.ciabr = set_reg_val(id, *val);
1390 /* Don't allow setting breakpoints in hypervisor code */
1391 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1392 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1393 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001394 case KVM_REG_PPC_CSIGR:
1395 vcpu->arch.csigr = set_reg_val(id, *val);
1396 break;
1397 case KVM_REG_PPC_TACR:
1398 vcpu->arch.tacr = set_reg_val(id, *val);
1399 break;
1400 case KVM_REG_PPC_TCSCR:
1401 vcpu->arch.tcscr = set_reg_val(id, *val);
1402 break;
1403 case KVM_REG_PPC_PID:
1404 vcpu->arch.pid = set_reg_val(id, *val);
1405 break;
1406 case KVM_REG_PPC_ACOP:
1407 vcpu->arch.acop = set_reg_val(id, *val);
1408 break;
1409 case KVM_REG_PPC_WORT:
1410 vcpu->arch.wort = set_reg_val(id, *val);
1411 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001412 case KVM_REG_PPC_VPA_ADDR:
1413 addr = set_reg_val(id, *val);
1414 r = -EINVAL;
1415 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1416 vcpu->arch.dtl.next_gpa))
1417 break;
1418 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1419 break;
1420 case KVM_REG_PPC_VPA_SLB:
1421 addr = val->vpaval.addr;
1422 len = val->vpaval.length;
1423 r = -EINVAL;
1424 if (addr && !vcpu->arch.vpa.next_gpa)
1425 break;
1426 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1427 break;
1428 case KVM_REG_PPC_VPA_DTL:
1429 addr = val->vpaval.addr;
1430 len = val->vpaval.length;
1431 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001432 if (addr && (len < sizeof(struct dtl_entry) ||
1433 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001434 break;
1435 len -= len % sizeof(struct dtl_entry);
1436 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1437 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001438 case KVM_REG_PPC_TB_OFFSET:
1439 /* round up to multiple of 2^24 */
1440 vcpu->arch.vcore->tb_offset =
1441 ALIGN(set_reg_val(id, *val), 1UL << 24);
1442 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001443 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001444 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1445 break;
1446 case KVM_REG_PPC_LPCR_64:
1447 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001448 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001449 case KVM_REG_PPC_PPR:
1450 vcpu->arch.ppr = set_reg_val(id, *val);
1451 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001452#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1453 case KVM_REG_PPC_TFHAR:
1454 vcpu->arch.tfhar = set_reg_val(id, *val);
1455 break;
1456 case KVM_REG_PPC_TFIAR:
1457 vcpu->arch.tfiar = set_reg_val(id, *val);
1458 break;
1459 case KVM_REG_PPC_TEXASR:
1460 vcpu->arch.texasr = set_reg_val(id, *val);
1461 break;
1462 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1463 i = id - KVM_REG_PPC_TM_GPR0;
1464 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1465 break;
1466 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1467 {
1468 int j;
1469 i = id - KVM_REG_PPC_TM_VSR0;
1470 if (i < 32)
1471 for (j = 0; j < TS_FPRWIDTH; j++)
1472 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1473 else
1474 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1475 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1476 else
1477 r = -ENXIO;
1478 break;
1479 }
1480 case KVM_REG_PPC_TM_CR:
1481 vcpu->arch.cr_tm = set_reg_val(id, *val);
1482 break;
1483 case KVM_REG_PPC_TM_LR:
1484 vcpu->arch.lr_tm = set_reg_val(id, *val);
1485 break;
1486 case KVM_REG_PPC_TM_CTR:
1487 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1488 break;
1489 case KVM_REG_PPC_TM_FPSCR:
1490 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1491 break;
1492 case KVM_REG_PPC_TM_AMR:
1493 vcpu->arch.amr_tm = set_reg_val(id, *val);
1494 break;
1495 case KVM_REG_PPC_TM_PPR:
1496 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1497 break;
1498 case KVM_REG_PPC_TM_VRSAVE:
1499 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1500 break;
1501 case KVM_REG_PPC_TM_VSCR:
1502 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1503 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1504 else
1505 r = - ENXIO;
1506 break;
1507 case KVM_REG_PPC_TM_DSCR:
1508 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1509 break;
1510 case KVM_REG_PPC_TM_TAR:
1511 vcpu->arch.tar_tm = set_reg_val(id, *val);
1512 break;
1513#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001514 case KVM_REG_PPC_ARCH_COMPAT:
1515 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1516 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001517 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001518 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001519 break;
1520 }
1521
1522 return r;
1523}
1524
Stewart Smithde9bdd12014-07-18 14:18:42 +10001525static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1526{
1527 struct kvmppc_vcore *vcore;
1528
1529 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1530
1531 if (vcore == NULL)
1532 return NULL;
1533
Stewart Smithde9bdd12014-07-18 14:18:42 +10001534 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001535 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001536 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001537 vcore->preempt_tb = TB_NIL;
1538 vcore->lpcr = kvm->arch.lpcr;
1539 vcore->first_vcpuid = core * threads_per_subcore;
1540 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001541 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001542
1543 return vcore;
1544}
1545
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001546#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1547static struct debugfs_timings_element {
1548 const char *name;
1549 size_t offset;
1550} timings[] = {
1551 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1552 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1553 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1554 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1555 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1556};
1557
1558#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1559
1560struct debugfs_timings_state {
1561 struct kvm_vcpu *vcpu;
1562 unsigned int buflen;
1563 char buf[N_TIMINGS * 100];
1564};
1565
1566static int debugfs_timings_open(struct inode *inode, struct file *file)
1567{
1568 struct kvm_vcpu *vcpu = inode->i_private;
1569 struct debugfs_timings_state *p;
1570
1571 p = kzalloc(sizeof(*p), GFP_KERNEL);
1572 if (!p)
1573 return -ENOMEM;
1574
1575 kvm_get_kvm(vcpu->kvm);
1576 p->vcpu = vcpu;
1577 file->private_data = p;
1578
1579 return nonseekable_open(inode, file);
1580}
1581
1582static int debugfs_timings_release(struct inode *inode, struct file *file)
1583{
1584 struct debugfs_timings_state *p = file->private_data;
1585
1586 kvm_put_kvm(p->vcpu->kvm);
1587 kfree(p);
1588 return 0;
1589}
1590
1591static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1592 size_t len, loff_t *ppos)
1593{
1594 struct debugfs_timings_state *p = file->private_data;
1595 struct kvm_vcpu *vcpu = p->vcpu;
1596 char *s, *buf_end;
1597 struct kvmhv_tb_accumulator tb;
1598 u64 count;
1599 loff_t pos;
1600 ssize_t n;
1601 int i, loops;
1602 bool ok;
1603
1604 if (!p->buflen) {
1605 s = p->buf;
1606 buf_end = s + sizeof(p->buf);
1607 for (i = 0; i < N_TIMINGS; ++i) {
1608 struct kvmhv_tb_accumulator *acc;
1609
1610 acc = (struct kvmhv_tb_accumulator *)
1611 ((unsigned long)vcpu + timings[i].offset);
1612 ok = false;
1613 for (loops = 0; loops < 1000; ++loops) {
1614 count = acc->seqcount;
1615 if (!(count & 1)) {
1616 smp_rmb();
1617 tb = *acc;
1618 smp_rmb();
1619 if (count == acc->seqcount) {
1620 ok = true;
1621 break;
1622 }
1623 }
1624 udelay(1);
1625 }
1626 if (!ok)
1627 snprintf(s, buf_end - s, "%s: stuck\n",
1628 timings[i].name);
1629 else
1630 snprintf(s, buf_end - s,
1631 "%s: %llu %llu %llu %llu\n",
1632 timings[i].name, count / 2,
1633 tb_to_ns(tb.tb_total),
1634 tb_to_ns(tb.tb_min),
1635 tb_to_ns(tb.tb_max));
1636 s += strlen(s);
1637 }
1638 p->buflen = s - p->buf;
1639 }
1640
1641 pos = *ppos;
1642 if (pos >= p->buflen)
1643 return 0;
1644 if (len > p->buflen - pos)
1645 len = p->buflen - pos;
1646 n = copy_to_user(buf, p->buf + pos, len);
1647 if (n) {
1648 if (n == len)
1649 return -EFAULT;
1650 len -= n;
1651 }
1652 *ppos = pos + len;
1653 return len;
1654}
1655
1656static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1657 size_t len, loff_t *ppos)
1658{
1659 return -EACCES;
1660}
1661
1662static const struct file_operations debugfs_timings_ops = {
1663 .owner = THIS_MODULE,
1664 .open = debugfs_timings_open,
1665 .release = debugfs_timings_release,
1666 .read = debugfs_timings_read,
1667 .write = debugfs_timings_write,
1668 .llseek = generic_file_llseek,
1669};
1670
1671/* Create a debugfs directory for the vcpu */
1672static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1673{
1674 char buf[16];
1675 struct kvm *kvm = vcpu->kvm;
1676
1677 snprintf(buf, sizeof(buf), "vcpu%u", id);
1678 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1679 return;
1680 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1681 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1682 return;
1683 vcpu->arch.debugfs_timings =
1684 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1685 vcpu, &debugfs_timings_ops);
1686}
1687
1688#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1689static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1690{
1691}
1692#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1693
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301694static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1695 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001696{
1697 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001698 int err = -EINVAL;
1699 int core;
1700 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001701
Michael Ellerman3102f782014-05-23 18:15:29 +10001702 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001703 if (core >= KVM_MAX_VCORES)
1704 goto out;
1705
1706 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001707 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001708 if (!vcpu)
1709 goto out;
1710
1711 err = kvm_vcpu_init(vcpu, kvm, id);
1712 if (err)
1713 goto free_vcpu;
1714
1715 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001716#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1717 /*
1718 * The shared struct is never shared on HV,
1719 * so we can always use host endianness
1720 */
1721#ifdef __BIG_ENDIAN__
1722 vcpu->arch.shared_big_endian = true;
1723#else
1724 vcpu->arch.shared_big_endian = false;
1725#endif
1726#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001727 vcpu->arch.mmcr[0] = MMCR0_FC;
1728 vcpu->arch.ctrl = CTRL_RUNLATCH;
1729 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301730 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001731 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001732 spin_lock_init(&vcpu->arch.tbacct_lock);
1733 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001734 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001735
Paul Mackerrasde56a942011-06-29 00:21:34 +00001736 kvmppc_mmu_book3s_hv_init(vcpu);
1737
Paul Mackerras8455d792012-10-15 01:17:42 +00001738 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001739
1740 init_waitqueue_head(&vcpu->arch.cpu_run);
1741
1742 mutex_lock(&kvm->lock);
1743 vcore = kvm->arch.vcores[core];
1744 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001745 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001746 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001747 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001748 }
1749 mutex_unlock(&kvm->lock);
1750
1751 if (!vcore)
1752 goto free_vcpu;
1753
1754 spin_lock(&vcore->lock);
1755 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001756 spin_unlock(&vcore->lock);
1757 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001758 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001759 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001760
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001761 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1762 kvmppc_sanity_check(vcpu);
1763
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001764 debugfs_vcpu_init(vcpu, id);
1765
Paul Mackerrasde56a942011-06-29 00:21:34 +00001766 return vcpu;
1767
1768free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001769 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001770out:
1771 return ERR_PTR(err);
1772}
1773
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001774static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1775{
1776 if (vpa->pinned_addr)
1777 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1778 vpa->dirty);
1779}
1780
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301781static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001782{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001783 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001784 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1785 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1786 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001787 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001788 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001789 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001790}
1791
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301792static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1793{
1794 /* Indicate we want to get back into the guest */
1795 return 1;
1796}
1797
Paul Mackerras19ccb762011-07-23 17:42:46 +10001798static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001799{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001800 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001801
Paul Mackerras19ccb762011-07-23 17:42:46 +10001802 now = get_tb();
1803 if (now > vcpu->arch.dec_expires) {
1804 /* decrementer has already gone negative */
1805 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001806 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001807 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001808 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001809 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1810 / tb_ticks_per_sec;
1811 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1812 HRTIMER_MODE_REL);
1813 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001814}
1815
Paul Mackerras19ccb762011-07-23 17:42:46 +10001816static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001817{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001818 vcpu->arch.ceded = 0;
1819 if (vcpu->arch.timer_running) {
1820 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1821 vcpu->arch.timer_running = 0;
1822 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001823}
1824
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001825extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001826
1827static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1828 struct kvm_vcpu *vcpu)
1829{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001830 u64 now;
1831
Paul Mackerras371fefd2011-06-29 00:23:08 +00001832 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1833 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001834 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001835 now = mftb();
1836 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1837 vcpu->arch.stolen_logged;
1838 vcpu->arch.busy_preempt = now;
1839 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001840 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001841 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10001842 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001843}
1844
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001845static int kvmppc_grab_hwthread(int cpu)
1846{
1847 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001848 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001849
1850 tpaca = &paca[cpu];
1851
1852 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001853 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001854 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001855 tpaca->kvm_hstate.napping = 0;
1856 smp_wmb();
1857 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001858
1859 /*
1860 * If the thread is already executing in the kernel (e.g. handling
1861 * a stray interrupt), wait for it to get back to nap mode.
1862 * The smp_mb() is to ensure that our setting of hwthread_req
1863 * is visible before we look at hwthread_state, so if this
1864 * races with the code at system_reset_pSeries and the thread
1865 * misses our setting of hwthread_req, we are sure to see its
1866 * setting of hwthread_state, and vice versa.
1867 */
1868 smp_mb();
1869 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1870 if (--timeout <= 0) {
1871 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1872 return -EBUSY;
1873 }
1874 udelay(1);
1875 }
1876 return 0;
1877}
1878
1879static void kvmppc_release_hwthread(int cpu)
1880{
1881 struct paca_struct *tpaca;
1882
1883 tpaca = &paca[cpu];
1884 tpaca->kvm_hstate.hwthread_req = 0;
1885 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001886 tpaca->kvm_hstate.kvm_vcore = NULL;
1887 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001888}
1889
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001890static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001891{
1892 int cpu;
1893 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001894 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001895
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001896 cpu = vc->pcpu;
1897 if (vcpu) {
1898 if (vcpu->arch.timer_running) {
1899 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1900 vcpu->arch.timer_running = 0;
1901 }
1902 cpu += vcpu->arch.ptid;
1903 vcpu->cpu = mvc->pcpu;
1904 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001905 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001906 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001907 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001908 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001909 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001910 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001911 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001912 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001913 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001914}
1915
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001916static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001917{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001918 int cpu = smp_processor_id();
1919 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001920
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001921 for (loops = 0; loops < 1000000; ++loops) {
1922 /*
1923 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001924 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001925 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001926 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001927 */
1928 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001929 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001930 break;
1931 if (i == threads_per_subcore) {
1932 HMT_medium();
1933 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001934 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001935 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001936 }
1937 HMT_medium();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001938 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001939 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001940 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001941}
1942
1943/*
1944 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001945 * this core are off-line. Then grab the threads so they can't
1946 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001947 */
1948static int on_primary_thread(void)
1949{
1950 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001951 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001952
Michael Ellerman3102f782014-05-23 18:15:29 +10001953 /* Are we on a primary subcore? */
1954 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001955 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001956
1957 thr = 0;
1958 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001959 if (cpu_online(cpu + thr))
1960 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001961
1962 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001963 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001964 if (kvmppc_grab_hwthread(cpu + thr)) {
1965 /* Couldn't grab one; let the others go */
1966 do {
1967 kvmppc_release_hwthread(cpu + thr);
1968 } while (--thr > 0);
1969 return 0;
1970 }
1971 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001972 return 1;
1973}
1974
Paul Mackerrasec257162015-06-24 21:18:03 +10001975/*
1976 * A list of virtual cores for each physical CPU.
1977 * These are vcores that could run but their runner VCPU tasks are
1978 * (or may be) preempted.
1979 */
1980struct preempted_vcore_list {
1981 struct list_head list;
1982 spinlock_t lock;
1983};
1984
1985static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1986
1987static void init_vcore_lists(void)
1988{
1989 int cpu;
1990
1991 for_each_possible_cpu(cpu) {
1992 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1993 spin_lock_init(&lp->lock);
1994 INIT_LIST_HEAD(&lp->list);
1995 }
1996}
1997
1998static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1999{
2000 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2001
2002 vc->vcore_state = VCORE_PREEMPT;
2003 vc->pcpu = smp_processor_id();
2004 if (vc->num_threads < threads_per_subcore) {
2005 spin_lock(&lp->lock);
2006 list_add_tail(&vc->preempt_list, &lp->list);
2007 spin_unlock(&lp->lock);
2008 }
2009
2010 /* Start accumulating stolen time */
2011 kvmppc_core_start_stolen(vc);
2012}
2013
2014static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
2015{
Paul Mackerras402813f2015-07-16 17:11:13 +10002016 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10002017
2018 kvmppc_core_end_stolen(vc);
2019 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10002020 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002021 spin_lock(&lp->lock);
2022 list_del_init(&vc->preempt_list);
2023 spin_unlock(&lp->lock);
2024 }
2025 vc->vcore_state = VCORE_INACTIVE;
2026}
2027
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002028/*
2029 * This stores information about the virtual cores currently
2030 * assigned to a physical core.
2031 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002032struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002033 int n_subcores;
2034 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002035 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002036 int subcore_threads[MAX_SUBCORES];
2037 struct kvm *subcore_vm[MAX_SUBCORES];
2038 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002039};
2040
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002041/*
2042 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
2043 * respectively in 2-way micro-threading (split-core) mode.
2044 */
2045static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2046
Paul Mackerrasec257162015-06-24 21:18:03 +10002047static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2048{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002049 int sub;
2050
Paul Mackerrasec257162015-06-24 21:18:03 +10002051 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002052 cip->n_subcores = 1;
2053 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002054 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002055 cip->subcore_threads[0] = vc->num_threads;
2056 cip->subcore_vm[0] = vc->kvm;
2057 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2058 INIT_LIST_HEAD(&cip->vcs[sub]);
2059 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2060}
2061
2062static bool subcore_config_ok(int n_subcores, int n_threads)
2063{
2064 /* Can only dynamically split if unsplit to begin with */
2065 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2066 return false;
2067 if (n_subcores > MAX_SUBCORES)
2068 return false;
2069 if (n_subcores > 1) {
2070 if (!(dynamic_mt_modes & 2))
2071 n_subcores = 4;
2072 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2073 return false;
2074 }
2075
2076 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002077}
2078
2079static void init_master_vcore(struct kvmppc_vcore *vc)
2080{
2081 vc->master_vcore = vc;
2082 vc->entry_exit_map = 0;
2083 vc->in_guest = 0;
2084 vc->napping_threads = 0;
2085 vc->conferring_threads = 0;
2086}
2087
2088/*
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002089 * See if the existing subcores can be split into 3 (or fewer) subcores
2090 * of at most two threads each, so we can fit in another vcore. This
2091 * assumes there are at most two subcores and at most 6 threads in total.
Paul Mackerrasec257162015-06-24 21:18:03 +10002092 */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002093static bool can_split_piggybacked_subcores(struct core_info *cip)
2094{
2095 int sub, new_sub;
2096 int large_sub = -1;
2097 int thr;
2098 int n_subcores = cip->n_subcores;
2099 struct kvmppc_vcore *vc, *vcnext;
2100 struct kvmppc_vcore *master_vc = NULL;
2101
2102 for (sub = 0; sub < cip->n_subcores; ++sub) {
2103 if (cip->subcore_threads[sub] <= 2)
2104 continue;
2105 if (large_sub >= 0)
2106 return false;
2107 large_sub = sub;
2108 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2109 preempt_list);
2110 if (vc->num_threads > 2)
2111 return false;
2112 n_subcores += (cip->subcore_threads[sub] - 1) >> 1;
2113 }
Paul Mackerrasf74f2e22015-11-03 16:03:30 +11002114 if (large_sub < 0 || !subcore_config_ok(n_subcores + 1, 2))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002115 return false;
2116
2117 /*
2118 * Seems feasible, so go through and move vcores to new subcores.
2119 * Note that when we have two or more vcores in one subcore,
2120 * all those vcores must have only one thread each.
2121 */
2122 new_sub = cip->n_subcores;
2123 thr = 0;
2124 sub = large_sub;
2125 list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) {
2126 if (thr >= 2) {
2127 list_del(&vc->preempt_list);
2128 list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]);
2129 /* vc->num_threads must be 1 */
2130 if (++cip->subcore_threads[new_sub] == 1) {
2131 cip->subcore_vm[new_sub] = vc->kvm;
2132 init_master_vcore(vc);
2133 master_vc = vc;
2134 ++cip->n_subcores;
2135 } else {
2136 vc->master_vcore = master_vc;
2137 ++new_sub;
2138 }
2139 }
2140 thr += vc->num_threads;
2141 }
2142 cip->subcore_threads[large_sub] = 2;
2143 cip->max_subcore_threads = 2;
2144
2145 return true;
2146}
2147
2148static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2149{
2150 int n_threads = vc->num_threads;
2151 int sub;
2152
2153 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2154 return false;
2155
2156 if (n_threads < cip->max_subcore_threads)
2157 n_threads = cip->max_subcore_threads;
2158 if (subcore_config_ok(cip->n_subcores + 1, n_threads)) {
2159 cip->max_subcore_threads = n_threads;
2160 } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 &&
2161 vc->num_threads <= 2) {
2162 /*
2163 * We may be able to fit another subcore in by
2164 * splitting an existing subcore with 3 or 4
2165 * threads into two 2-thread subcores, or one
2166 * with 5 or 6 threads into three subcores.
2167 * We can only do this if those subcores have
2168 * piggybacked virtual cores.
2169 */
2170 if (!can_split_piggybacked_subcores(cip))
2171 return false;
2172 } else {
2173 return false;
2174 }
2175
2176 sub = cip->n_subcores;
2177 ++cip->n_subcores;
2178 cip->total_threads += vc->num_threads;
2179 cip->subcore_threads[sub] = vc->num_threads;
2180 cip->subcore_vm[sub] = vc->kvm;
2181 init_master_vcore(vc);
2182 list_del(&vc->preempt_list);
2183 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2184
2185 return true;
2186}
2187
2188static bool can_piggyback_subcore(struct kvmppc_vcore *pvc,
2189 struct core_info *cip, int sub)
Paul Mackerrasec257162015-06-24 21:18:03 +10002190{
2191 struct kvmppc_vcore *vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002192 int n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002193
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002194 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2195 preempt_list);
Paul Mackerrasec257162015-06-24 21:18:03 +10002196
2197 /* require same VM and same per-core reg values */
2198 if (pvc->kvm != vc->kvm ||
2199 pvc->tb_offset != vc->tb_offset ||
2200 pvc->pcr != vc->pcr ||
2201 pvc->lpcr != vc->lpcr)
2202 return false;
2203
2204 /* P8 guest with > 1 thread per core would see wrong TIR value */
2205 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2206 (vc->num_threads > 1 || pvc->num_threads > 1))
2207 return false;
2208
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002209 n_thr = cip->subcore_threads[sub] + pvc->num_threads;
2210 if (n_thr > cip->max_subcore_threads) {
2211 if (!subcore_config_ok(cip->n_subcores, n_thr))
2212 return false;
2213 cip->max_subcore_threads = n_thr;
2214 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002215
2216 cip->total_threads += pvc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002217 cip->subcore_threads[sub] = n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002218 pvc->master_vcore = vc;
2219 list_del(&pvc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002220 list_add_tail(&pvc->preempt_list, &cip->vcs[sub]);
Paul Mackerrasec257162015-06-24 21:18:03 +10002221
2222 return true;
2223}
2224
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002225/*
2226 * Work out whether it is possible to piggyback the execution of
2227 * vcore *pvc onto the execution of the other vcores described in *cip.
2228 */
2229static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2230 int target_threads)
2231{
2232 int sub;
2233
2234 if (cip->total_threads + pvc->num_threads > target_threads)
2235 return false;
2236 for (sub = 0; sub < cip->n_subcores; ++sub)
2237 if (cip->subcore_threads[sub] &&
2238 can_piggyback_subcore(pvc, cip, sub))
2239 return true;
2240
2241 if (can_dynamic_split(pvc, cip))
2242 return true;
2243
2244 return false;
2245}
2246
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002247static void prepare_threads(struct kvmppc_vcore *vc)
2248{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002249 int i;
2250 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002251
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002252 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002253 if (signal_pending(vcpu->arch.run_task))
2254 vcpu->arch.ret = -EINTR;
2255 else if (vcpu->arch.vpa.update_pending ||
2256 vcpu->arch.slb_shadow.update_pending ||
2257 vcpu->arch.dtl.update_pending)
2258 vcpu->arch.ret = RESUME_GUEST;
2259 else
2260 continue;
2261 kvmppc_remove_runnable(vc, vcpu);
2262 wake_up(&vcpu->arch.cpu_run);
2263 }
2264}
2265
Paul Mackerrasec257162015-06-24 21:18:03 +10002266static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002267{
Paul Mackerrasec257162015-06-24 21:18:03 +10002268 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2269 struct kvmppc_vcore *pvc, *vcnext;
2270
2271 spin_lock(&lp->lock);
2272 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2273 if (!spin_trylock(&pvc->lock))
2274 continue;
2275 prepare_threads(pvc);
2276 if (!pvc->n_runnable) {
2277 list_del_init(&pvc->preempt_list);
2278 if (pvc->runner == NULL) {
2279 pvc->vcore_state = VCORE_INACTIVE;
2280 kvmppc_core_end_stolen(pvc);
2281 }
2282 spin_unlock(&pvc->lock);
2283 continue;
2284 }
2285 if (!can_piggyback(pvc, cip, target_threads)) {
2286 spin_unlock(&pvc->lock);
2287 continue;
2288 }
2289 kvmppc_core_end_stolen(pvc);
2290 pvc->vcore_state = VCORE_PIGGYBACK;
2291 if (cip->total_threads >= target_threads)
2292 break;
2293 }
2294 spin_unlock(&lp->lock);
2295}
2296
2297static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2298{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002299 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002300 u64 now;
2301 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002302 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002303
Paul Mackerrasec257162015-06-24 21:18:03 +10002304 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002305 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002306 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002307 /* cancel pending dec exception if dec is positive */
2308 if (now < vcpu->arch.dec_expires &&
2309 kvmppc_core_pending_dec(vcpu))
2310 kvmppc_core_dequeue_dec(vcpu);
2311
2312 trace_kvm_guest_exit(vcpu);
2313
2314 ret = RESUME_GUEST;
2315 if (vcpu->arch.trap)
2316 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2317 vcpu->arch.run_task);
2318
2319 vcpu->arch.ret = ret;
2320 vcpu->arch.trap = 0;
2321
Paul Mackerrasec257162015-06-24 21:18:03 +10002322 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2323 if (vcpu->arch.pending_exceptions)
2324 kvmppc_core_prepare_to_enter(vcpu);
2325 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002326 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002327 else
2328 ++still_running;
2329 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002330 kvmppc_remove_runnable(vc, vcpu);
2331 wake_up(&vcpu->arch.cpu_run);
2332 }
2333 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002334 list_del_init(&vc->preempt_list);
2335 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002336 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002337 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002338 } else if (vc->runner) {
2339 vc->vcore_state = VCORE_PREEMPT;
2340 kvmppc_core_start_stolen(vc);
2341 } else {
2342 vc->vcore_state = VCORE_INACTIVE;
2343 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002344 if (vc->n_runnable > 0 && vc->runner == NULL) {
2345 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002346 i = -1;
2347 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10002348 wake_up(&vcpu->arch.cpu_run);
2349 }
2350 }
2351 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002352}
2353
Paul Mackerras371fefd2011-06-29 00:23:08 +00002354/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002355 * Clear core from the list of active host cores as we are about to
2356 * enter the guest. Only do this if it is the primary thread of the
2357 * core (not if a subcore) that is entering the guest.
2358 */
2359static inline void kvmppc_clear_host_core(int cpu)
2360{
2361 int core;
2362
2363 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2364 return;
2365 /*
2366 * Memory barrier can be omitted here as we will do a smp_wmb()
2367 * later in kvmppc_start_thread and we need ensure that state is
2368 * visible to other CPUs only after we enter guest.
2369 */
2370 core = cpu >> threads_shift;
2371 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
2372}
2373
2374/*
2375 * Advertise this core as an active host core since we exited the guest
2376 * Only need to do this if it is the primary thread of the core that is
2377 * exiting.
2378 */
2379static inline void kvmppc_set_host_core(int cpu)
2380{
2381 int core;
2382
2383 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2384 return;
2385
2386 /*
2387 * Memory barrier can be omitted here because we do a spin_unlock
2388 * immediately after this which provides the memory barrier.
2389 */
2390 core = cpu >> threads_shift;
2391 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
2392}
2393
2394/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002395 * Run a set of guest threads on a physical core.
2396 * Called with vc->lock held.
2397 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002398static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002399{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002400 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002401 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002402 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002403 struct core_info core_info;
2404 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002405 struct kvm_split_mode split_info, *sip;
2406 int split, subcore_size, active;
2407 int sub;
2408 bool thr0_done;
2409 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002410 int pcpu, thr;
2411 int target_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002412
2413 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002414 * Remove from the list any threads that have a signal pending
2415 * or need a VPA update done
2416 */
2417 prepare_threads(vc);
2418
2419 /* if the runner is no longer runnable, let the caller pick a new one */
2420 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2421 return;
2422
2423 /*
2424 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002425 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002426 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002427 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002428
2429 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002430 * Make sure we are running on primary threads, and that secondary
2431 * threads are offline. Also check if the number of threads in this
2432 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002433 */
Michael Ellerman3102f782014-05-23 18:15:29 +10002434 if ((threads_per_core > 1) &&
2435 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002436 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002437 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002438 kvmppc_remove_runnable(vc, vcpu);
2439 wake_up(&vcpu->arch.cpu_run);
2440 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002441 goto out;
2442 }
2443
Paul Mackerrasec257162015-06-24 21:18:03 +10002444 /*
2445 * See if we could run any other vcores on the physical core
2446 * along with this one.
2447 */
2448 init_core_info(&core_info, vc);
2449 pcpu = smp_processor_id();
2450 target_threads = threads_per_subcore;
2451 if (target_smt_mode && target_smt_mode < target_threads)
2452 target_threads = target_smt_mode;
2453 if (vc->num_threads < target_threads)
2454 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002455
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002456 /* Decide on micro-threading (split-core) mode */
2457 subcore_size = threads_per_subcore;
2458 cmd_bit = stat_bit = 0;
2459 split = core_info.n_subcores;
2460 sip = NULL;
2461 if (split > 1) {
2462 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2463 if (split == 2 && (dynamic_mt_modes & 2)) {
2464 cmd_bit = HID0_POWER8_1TO2LPAR;
2465 stat_bit = HID0_POWER8_2LPARMODE;
2466 } else {
2467 split = 4;
2468 cmd_bit = HID0_POWER8_1TO4LPAR;
2469 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002470 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002471 subcore_size = MAX_SMT_THREADS / split;
2472 sip = &split_info;
2473 memset(&split_info, 0, sizeof(split_info));
2474 split_info.rpr = mfspr(SPRN_RPR);
2475 split_info.pmmar = mfspr(SPRN_PMMAR);
2476 split_info.ldbar = mfspr(SPRN_LDBAR);
2477 split_info.subcore_size = subcore_size;
2478 for (sub = 0; sub < core_info.n_subcores; ++sub)
2479 split_info.master_vcs[sub] =
2480 list_first_entry(&core_info.vcs[sub],
2481 struct kvmppc_vcore, preempt_list);
2482 /* order writes to split_info before kvm_split_mode pointer */
2483 smp_wmb();
2484 }
2485 pcpu = smp_processor_id();
2486 for (thr = 0; thr < threads_per_subcore; ++thr)
2487 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2488
2489 /* Initiate micro-threading (split-core) if required */
2490 if (cmd_bit) {
2491 unsigned long hid0 = mfspr(SPRN_HID0);
2492
2493 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2494 mb();
2495 mtspr(SPRN_HID0, hid0);
2496 isync();
2497 for (;;) {
2498 hid0 = mfspr(SPRN_HID0);
2499 if (hid0 & stat_bit)
2500 break;
2501 cpu_relax();
2502 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002503 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002504
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002505 kvmppc_clear_host_core(pcpu);
2506
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002507 /* Start all the threads */
2508 active = 0;
2509 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2510 thr = subcore_thread_map[sub];
2511 thr0_done = false;
2512 active |= 1 << thr;
2513 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2514 pvc->pcpu = pcpu + thr;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002515 for_each_runnable_thread(i, vcpu, pvc) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002516 kvmppc_start_thread(vcpu, pvc);
2517 kvmppc_create_dtl_entry(vcpu, pvc);
2518 trace_kvm_guest_enter(vcpu);
2519 if (!vcpu->arch.ptid)
2520 thr0_done = true;
2521 active |= 1 << (thr + vcpu->arch.ptid);
2522 }
2523 /*
2524 * We need to start the first thread of each subcore
2525 * even if it doesn't have a vcpu.
2526 */
2527 if (pvc->master_vcore == pvc && !thr0_done)
2528 kvmppc_start_thread(NULL, pvc);
2529 thr += pvc->num_threads;
2530 }
2531 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302532
2533 /*
2534 * Ensure that split_info.do_nap is set after setting
2535 * the vcore pointer in the PACA of the secondaries.
2536 */
2537 smp_mb();
2538 if (cmd_bit)
2539 split_info.do_nap = 1; /* ask secondaries to nap when done */
2540
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002541 /*
2542 * When doing micro-threading, poke the inactive threads as well.
2543 * This gets them to the nap instruction after kvm_do_nap,
2544 * which reduces the time taken to unsplit later.
2545 */
2546 if (split > 1)
2547 for (thr = 1; thr < threads_per_subcore; ++thr)
2548 if (!(active & (1 << thr)))
2549 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002550
Paul Mackerras2f12f032012-10-15 01:17:17 +00002551 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002552 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002553
2554 trace_kvmppc_run_core(vc, 0);
2555
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002556 for (sub = 0; sub < core_info.n_subcores; ++sub)
2557 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2558 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002559
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002560 guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002561
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002562 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002563
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002564 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002565
Paul Mackerrasec257162015-06-24 21:18:03 +10002566 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2567
2568 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002569 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002570 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002571
Paul Mackerras371fefd2011-06-29 00:23:08 +00002572 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002573 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002574
2575 /* Return to whole-core mode if we split the core earlier */
2576 if (split > 1) {
2577 unsigned long hid0 = mfspr(SPRN_HID0);
2578 unsigned long loops = 0;
2579
2580 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2581 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2582 mb();
2583 mtspr(SPRN_HID0, hid0);
2584 isync();
2585 for (;;) {
2586 hid0 = mfspr(SPRN_HID0);
2587 if (!(hid0 & stat_bit))
2588 break;
2589 cpu_relax();
2590 ++loops;
2591 }
2592 split_info.do_nap = 0;
2593 }
2594
2595 /* Let secondaries go back to the offline loop */
2596 for (i = 0; i < threads_per_subcore; ++i) {
2597 kvmppc_release_hwthread(pcpu + i);
2598 if (sip && sip->napped[i])
2599 kvmppc_ipi_thread(pcpu + i);
2600 }
2601
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002602 kvmppc_set_host_core(pcpu);
2603
Paul Mackerras371fefd2011-06-29 00:23:08 +00002604 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002605
Paul Mackerras371fefd2011-06-29 00:23:08 +00002606 /* make sure updates to secondary vcpu structs are visible now */
2607 smp_mb();
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002608 guest_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002609
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002610 for (sub = 0; sub < core_info.n_subcores; ++sub)
2611 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2612 preempt_list)
2613 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002614
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002615 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002616 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002617
Paul Mackerrasde56a942011-06-29 00:21:34 +00002618 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002619 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002620 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002621}
2622
Paul Mackerras19ccb762011-07-23 17:42:46 +10002623/*
2624 * Wait for some other vcpu thread to execute us, and
2625 * wake us up when we need to handle something in the host.
2626 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002627static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2628 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002629{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002630 DEFINE_WAIT(wait);
2631
Paul Mackerras19ccb762011-07-23 17:42:46 +10002632 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002633 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2634 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002635 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002636 spin_lock(&vc->lock);
2637 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002638 finish_wait(&vcpu->arch.cpu_run, &wait);
2639}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002640
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002641static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
2642{
2643 /* 10us base */
2644 if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
2645 vc->halt_poll_ns = 10000;
2646 else
2647 vc->halt_poll_ns *= halt_poll_ns_grow;
2648
2649 if (vc->halt_poll_ns > halt_poll_max_ns)
2650 vc->halt_poll_ns = halt_poll_max_ns;
2651}
2652
2653static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
2654{
2655 if (halt_poll_ns_shrink == 0)
2656 vc->halt_poll_ns = 0;
2657 else
2658 vc->halt_poll_ns /= halt_poll_ns_shrink;
2659}
2660
2661/* Check to see if any of the runnable vcpus on the vcore have pending
2662 * exceptions or are no longer ceded
2663 */
2664static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
2665{
2666 struct kvm_vcpu *vcpu;
2667 int i;
2668
2669 for_each_runnable_thread(i, vcpu, vc) {
2670 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded)
2671 return 1;
2672 }
2673
2674 return 0;
2675}
2676
Paul Mackerras19ccb762011-07-23 17:42:46 +10002677/*
2678 * All the vcpus in this vcore are idle, so wait for a decrementer
2679 * or external interrupt to one of the vcpus. vc->lock is held.
2680 */
2681static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2682{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002683 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002684 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002685 u64 block_ns;
Marcelo Tosatti85773702016-02-19 09:46:39 +01002686 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002687
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002688 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002689 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002690 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002691 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
2692 ++vc->runner->stat.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002693
2694 vc->vcore_state = VCORE_POLLING;
2695 spin_unlock(&vc->lock);
2696
2697 do {
2698 if (kvmppc_vcore_check_block(vc)) {
2699 do_sleep = 0;
2700 break;
2701 }
2702 cur = ktime_get();
2703 } while (single_task_running() && ktime_before(cur, stop));
2704
2705 spin_lock(&vc->lock);
2706 vc->vcore_state = VCORE_INACTIVE;
2707
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002708 if (!do_sleep) {
2709 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002710 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002711 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002712 }
2713
Marcelo Tosatti85773702016-02-19 09:46:39 +01002714 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002715
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002716 if (kvmppc_vcore_check_block(vc)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002717 finish_swait(&vc->wq, &wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002718 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002719 /* If we polled, count this as a successful poll */
2720 if (vc->halt_poll_ns)
2721 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002722 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002723 }
2724
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002725 start_wait = ktime_get();
2726
Paul Mackerras19ccb762011-07-23 17:42:46 +10002727 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002728 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002729 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002730 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01002731 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002732 spin_lock(&vc->lock);
2733 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002734 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002735 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002736
2737 cur = ktime_get();
2738
2739out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002740 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
2741
2742 /* Attribute wait time */
2743 if (do_sleep) {
2744 vc->runner->stat.halt_wait_ns +=
2745 ktime_to_ns(cur) - ktime_to_ns(start_wait);
2746 /* Attribute failed poll time */
2747 if (vc->halt_poll_ns)
2748 vc->runner->stat.halt_poll_fail_ns +=
2749 ktime_to_ns(start_wait) -
2750 ktime_to_ns(start_poll);
2751 } else {
2752 /* Attribute successful poll time */
2753 if (vc->halt_poll_ns)
2754 vc->runner->stat.halt_poll_success_ns +=
2755 ktime_to_ns(cur) -
2756 ktime_to_ns(start_poll);
2757 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002758
2759 /* Adjust poll time */
2760 if (halt_poll_max_ns) {
2761 if (block_ns <= vc->halt_poll_ns)
2762 ;
2763 /* We slept and blocked for longer than the max halt time */
2764 else if (vc->halt_poll_ns && block_ns > halt_poll_max_ns)
2765 shrink_halt_poll_ns(vc);
2766 /* We slept and our poll time is too small */
2767 else if (vc->halt_poll_ns < halt_poll_max_ns &&
2768 block_ns < halt_poll_max_ns)
2769 grow_halt_poll_ns(vc);
2770 } else
2771 vc->halt_poll_ns = 0;
2772
2773 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002774}
2775
2776static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2777{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002778 int n_ceded, i;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002779 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002780 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002781
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002782 trace_kvmppc_run_vcpu_enter(vcpu);
2783
Paul Mackerras371fefd2011-06-29 00:23:08 +00002784 kvm_run->exit_reason = 0;
2785 vcpu->arch.ret = RESUME_GUEST;
2786 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002787 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002788
Paul Mackerras371fefd2011-06-29 00:23:08 +00002789 /*
2790 * Synchronize with other threads in this virtual core
2791 */
2792 vc = vcpu->arch.vcore;
2793 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002794 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002795 vcpu->arch.run_task = current;
2796 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002797 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002798 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002799 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002800 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002801 ++vc->n_runnable;
2802
Paul Mackerras19ccb762011-07-23 17:42:46 +10002803 /*
2804 * This happens the first time this is called for a vcpu.
2805 * If the vcore is already running, we may be able to start
2806 * this thread straight away and have it join in.
2807 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002808 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002809 if (vc->vcore_state == VCORE_PIGGYBACK) {
2810 struct kvmppc_vcore *mvc = vc->master_vcore;
2811 if (spin_trylock(&mvc->lock)) {
2812 if (mvc->vcore_state == VCORE_RUNNING &&
2813 !VCORE_IS_EXITING(mvc)) {
2814 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002815 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002816 trace_kvm_guest_enter(vcpu);
2817 }
2818 spin_unlock(&mvc->lock);
2819 }
2820 } else if (vc->vcore_state == VCORE_RUNNING &&
2821 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002822 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002823 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002824 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002825 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002826 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002827 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002828
Paul Mackerras8455d792012-10-15 01:17:42 +00002829 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002830
2831 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2832 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002833 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2834 kvmppc_vcore_end_preempt(vc);
2835
Paul Mackerras8455d792012-10-15 01:17:42 +00002836 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002837 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002838 continue;
2839 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002840 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002841 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002842 if (signal_pending(v->arch.run_task)) {
2843 kvmppc_remove_runnable(vc, v);
2844 v->stat.signal_exits++;
2845 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2846 v->arch.ret = -EINTR;
2847 wake_up(&v->arch.cpu_run);
2848 }
2849 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002850 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2851 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002852 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002853 for_each_runnable_thread(i, v, vc) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002854 if (!v->arch.pending_exceptions)
2855 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002856 else
2857 v->arch.ceded = 0;
2858 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002859 vc->runner = vcpu;
2860 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002861 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002862 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002863 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002864 /* Let something else run */
2865 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002866 if (vc->vcore_state == VCORE_PREEMPT)
2867 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002868 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002869 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002870 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002871 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002872 }
2873
Paul Mackerras8455d792012-10-15 01:17:42 +00002874 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2875 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002876 vc->vcore_state == VCORE_EXITING ||
2877 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002878 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002879
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002880 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2881 kvmppc_vcore_end_preempt(vc);
2882
Paul Mackerras8455d792012-10-15 01:17:42 +00002883 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2884 kvmppc_remove_runnable(vc, vcpu);
2885 vcpu->stat.signal_exits++;
2886 kvm_run->exit_reason = KVM_EXIT_INTR;
2887 vcpu->arch.ret = -EINTR;
2888 }
2889
2890 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2891 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002892 i = -1;
2893 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00002894 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002895 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002896
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002897 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002898 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002899 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002900}
2901
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302902static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002903{
2904 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002905 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002906
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002907 if (!vcpu->arch.sane) {
2908 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2909 return -EINVAL;
2910 }
2911
Scott Wood25051b5a2011-11-08 18:23:23 -06002912 kvmppc_core_prepare_to_enter(vcpu);
2913
Paul Mackerras19ccb762011-07-23 17:42:46 +10002914 /* No need to go into the guest when all we'll do is come back out */
2915 if (signal_pending(current)) {
2916 run->exit_reason = KVM_EXIT_INTR;
2917 return -EINTR;
2918 }
2919
Paul Mackerras32fad282012-05-04 02:32:53 +00002920 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002921 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002922 smp_mb();
2923
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002924 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002925 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002926 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002927 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002928 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002929 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002930
Anton Blanchard579e6332015-10-29 11:44:09 +11002931 flush_all_to_thread(current);
2932
Paul Mackerras19ccb762011-07-23 17:42:46 +10002933 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002934 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002935 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002936
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002937 do {
2938 r = kvmppc_run_vcpu(run, vcpu);
2939
2940 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2941 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002942 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002943 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002944 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002945 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002946 } else if (r == RESUME_PAGE_FAULT) {
2947 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2948 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2949 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2950 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002951 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002952 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002953
2954 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002955 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002956 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002957 return r;
2958}
2959
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002960static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2961 int linux_psize)
2962{
2963 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2964
2965 if (!def->shift)
2966 return;
2967 (*sps)->page_shift = def->shift;
2968 (*sps)->slb_enc = def->sllp;
2969 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002970 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302971 /*
2972 * Add 16MB MPSS support if host supports it
2973 */
2974 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2975 (*sps)->enc[1].page_shift = 24;
2976 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2977 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002978 (*sps)++;
2979}
2980
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302981static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2982 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002983{
2984 struct kvm_ppc_one_seg_page_size *sps;
2985
2986 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2987 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2988 info->flags |= KVM_PPC_1T_SEGMENTS;
2989 info->slb_size = mmu_slb_size;
2990
2991 /* We only support these sizes for now, and no muti-size segments */
2992 sps = &info->sps[0];
2993 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2994 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2995 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2996
2997 return 0;
2998}
2999
Paul Mackerras82ed3612011-12-15 02:03:22 +00003000/*
3001 * Get (and clear) the dirty memory log for a memory slot.
3002 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303003static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
3004 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003005{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003006 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00003007 struct kvm_memory_slot *memslot;
3008 int r;
3009 unsigned long n;
3010
3011 mutex_lock(&kvm->slots_lock);
3012
3013 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07003014 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00003015 goto out;
3016
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003017 slots = kvm_memslots(kvm);
3018 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003019 r = -ENOENT;
3020 if (!memslot->dirty_bitmap)
3021 goto out;
3022
3023 n = kvm_dirty_bitmap_bytes(memslot);
3024 memset(memslot->dirty_bitmap, 0, n);
3025
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003026 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00003027 if (r)
3028 goto out;
3029
3030 r = -EFAULT;
3031 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
3032 goto out;
3033
3034 r = 0;
3035out:
3036 mutex_unlock(&kvm->slots_lock);
3037 return r;
3038}
3039
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303040static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
3041 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003042{
3043 if (!dont || free->arch.rmap != dont->arch.rmap) {
3044 vfree(free->arch.rmap);
3045 free->arch.rmap = NULL;
3046 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003047}
3048
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303049static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
3050 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003051{
3052 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
3053 if (!slot->arch.rmap)
3054 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003055
3056 return 0;
3057}
3058
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303059static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
3060 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003061 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003062{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003063 return 0;
3064}
3065
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303066static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003067 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02003068 const struct kvm_memory_slot *old,
3069 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003070{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003071 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003072 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003073 struct kvm_memory_slot *memslot;
3074
Takuya Yoshikawa84826442013-02-27 19:45:25 +09003075 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003076 /*
3077 * If modifying a memslot, reset all the rmap dirty bits.
3078 * If this is a new memslot, we don't need to do anything
3079 * since the rmap array starts out as all zeroes,
3080 * i.e. no pages are dirty.
3081 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003082 slots = kvm_memslots(kvm);
3083 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003084 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
3085 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003086}
3087
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003088/*
3089 * Update LPCR values in kvm->arch and in vcores.
3090 * Caller must hold kvm->lock.
3091 */
3092void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
3093{
3094 long int i;
3095 u32 cores_done = 0;
3096
3097 if ((kvm->arch.lpcr & mask) == lpcr)
3098 return;
3099
3100 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
3101
3102 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3103 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3104 if (!vc)
3105 continue;
3106 spin_lock(&vc->lock);
3107 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
3108 spin_unlock(&vc->lock);
3109 if (++cores_done >= kvm->arch.online_vcores)
3110 break;
3111 }
3112}
3113
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303114static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
3115{
3116 return;
3117}
3118
Paul Mackerras32fad282012-05-04 02:32:53 +00003119static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003120{
3121 int err = 0;
3122 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003123 unsigned long hva;
3124 struct kvm_memory_slot *memslot;
3125 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003126 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003127 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003128 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003129
3130 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003131 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003132 goto out; /* another vcpu beat us to it */
3133
Paul Mackerras32fad282012-05-04 02:32:53 +00003134 /* Allocate hashed page table (if not done already) and reset it */
3135 if (!kvm->arch.hpt_virt) {
3136 err = kvmppc_alloc_hpt(kvm, NULL);
3137 if (err) {
3138 pr_err("KVM: Couldn't alloc HPT\n");
3139 goto out;
3140 }
3141 }
3142
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003143 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003144 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003145 memslot = gfn_to_memslot(kvm, 0);
3146
3147 /* We must have some memory at 0 by now */
3148 err = -EINVAL;
3149 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003150 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003151
3152 /* Look up the VMA for the start of this memory slot */
3153 hva = memslot->userspace_addr;
3154 down_read(&current->mm->mmap_sem);
3155 vma = find_vma(current->mm, hva);
3156 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3157 goto up_out;
3158
3159 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003160 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003161
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003162 up_read(&current->mm->mmap_sem);
3163
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003164 /* We can handle 4k, 64k or 16M pages in the VRMA */
3165 err = -EINVAL;
3166 if (!(psize == 0x1000 || psize == 0x10000 ||
3167 psize == 0x1000000))
3168 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003169
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003170 /* Update VRMASD field in the LPCR */
3171 senc = slb_pgsize_encoding(psize);
3172 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3173 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3174 /* the -4 is to account for senc values starting at 0x10 */
3175 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003176
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003177 /* Create HPTEs in the hash page table for the VRMA */
3178 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003179
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003180 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003181
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003182 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003183 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003184 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003185 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003186 out_srcu:
3187 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003188 out:
3189 mutex_unlock(&kvm->lock);
3190 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003191
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003192 up_out:
3193 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003194 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003195}
3196
Suresh Warrier79b6c242015-12-17 14:59:06 -06003197#ifdef CONFIG_KVM_XICS
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003198static int kvmppc_cpu_notify(struct notifier_block *self, unsigned long action,
3199 void *hcpu)
3200{
3201 unsigned long cpu = (long)hcpu;
3202
3203 switch (action) {
3204 case CPU_UP_PREPARE:
3205 case CPU_UP_PREPARE_FROZEN:
3206 kvmppc_set_host_core(cpu);
3207 break;
3208
3209#ifdef CONFIG_HOTPLUG_CPU
3210 case CPU_DEAD:
3211 case CPU_DEAD_FROZEN:
3212 case CPU_UP_CANCELED:
3213 case CPU_UP_CANCELED_FROZEN:
3214 kvmppc_clear_host_core(cpu);
3215 break;
3216#endif
3217 default:
3218 break;
3219 }
3220
3221 return NOTIFY_OK;
3222}
3223
3224static struct notifier_block kvmppc_cpu_notifier = {
3225 .notifier_call = kvmppc_cpu_notify,
3226};
3227
Suresh Warrier79b6c242015-12-17 14:59:06 -06003228/*
3229 * Allocate a per-core structure for managing state about which cores are
3230 * running in the host versus the guest and for exchanging data between
3231 * real mode KVM and CPU running in the host.
3232 * This is only done for the first VM.
3233 * The allocated structure stays even if all VMs have stopped.
3234 * It is only freed when the kvm-hv module is unloaded.
3235 * It's OK for this routine to fail, we just don't support host
3236 * core operations like redirecting H_IPI wakeups.
3237 */
3238void kvmppc_alloc_host_rm_ops(void)
3239{
3240 struct kvmppc_host_rm_ops *ops;
3241 unsigned long l_ops;
3242 int cpu, core;
3243 int size;
3244
3245 /* Not the first time here ? */
3246 if (kvmppc_host_rm_ops_hv != NULL)
3247 return;
3248
3249 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3250 if (!ops)
3251 return;
3252
3253 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3254 ops->rm_core = kzalloc(size, GFP_KERNEL);
3255
3256 if (!ops->rm_core) {
3257 kfree(ops);
3258 return;
3259 }
3260
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003261 get_online_cpus();
3262
Suresh Warrier79b6c242015-12-17 14:59:06 -06003263 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3264 if (!cpu_online(cpu))
3265 continue;
3266
3267 core = cpu >> threads_shift;
3268 ops->rm_core[core].rm_state.in_host = 1;
3269 }
3270
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003271 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3272
Suresh Warrier79b6c242015-12-17 14:59:06 -06003273 /*
3274 * Make the contents of the kvmppc_host_rm_ops structure visible
3275 * to other CPUs before we assign it to the global variable.
3276 * Do an atomic assignment (no locks used here), but if someone
3277 * beats us to it, just free our copy and return.
3278 */
3279 smp_wmb();
3280 l_ops = (unsigned long) ops;
3281
3282 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003283 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003284 kfree(ops->rm_core);
3285 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003286 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003287 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003288
3289 register_cpu_notifier(&kvmppc_cpu_notifier);
3290
3291 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003292}
3293
3294void kvmppc_free_host_rm_ops(void)
3295{
3296 if (kvmppc_host_rm_ops_hv) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003297 unregister_cpu_notifier(&kvmppc_cpu_notifier);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003298 kfree(kvmppc_host_rm_ops_hv->rm_core);
3299 kfree(kvmppc_host_rm_ops_hv);
3300 kvmppc_host_rm_ops_hv = NULL;
3301 }
3302}
3303#endif
3304
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303305static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003306{
Paul Mackerras32fad282012-05-04 02:32:53 +00003307 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003308 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00003309
Paul Mackerras32fad282012-05-04 02:32:53 +00003310 /* Allocate the guest's logical partition ID */
3311
3312 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003313 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003314 return -ENOMEM;
3315 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003316
Suresh Warrier79b6c242015-12-17 14:59:06 -06003317 kvmppc_alloc_host_rm_ops();
3318
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003319 /*
3320 * Since we don't flush the TLB when tearing down a VM,
3321 * and this lpid might have previously been used,
3322 * make sure we flush on each core before running the new VM.
3323 */
3324 cpumask_setall(&kvm->arch.need_tlb_flush);
3325
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003326 /* Start out with the default set of hcalls enabled */
3327 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3328 sizeof(kvm->arch.enabled_hcalls));
3329
Paul Mackerras9e368f22011-06-29 00:40:08 +00003330 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003331
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003332 /* Init LPCR for virtual RMA mode */
3333 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3334 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3335 lpcr &= LPCR_PECE | LPCR_LPES;
3336 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3337 LPCR_VPM0 | LPCR_VPM1;
3338 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3339 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3340 /* On POWER8 turn on online bit to enable PURR/SPURR */
3341 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3342 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00003343 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003344
Paul Mackerras512691d2012-10-15 01:15:41 +00003345 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003346 * Track that we now have a HV mode VM active. This blocks secondary
3347 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00003348 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10003349 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003350
Paul Mackerrase23a8082015-03-28 14:21:01 +11003351 /*
3352 * Create a debugfs directory for the VM
3353 */
3354 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3355 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3356 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3357 kvmppc_mmu_debugfs_init(kvm);
3358
David Gibson54738c02011-06-29 00:22:41 +00003359 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003360}
3361
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303362static void kvmppc_free_vcores(struct kvm *kvm)
3363{
3364 long int i;
3365
Paul Mackerras23316312015-10-21 16:03:14 +11003366 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303367 kfree(kvm->arch.vcores[i]);
3368 kvm->arch.online_vcores = 0;
3369}
3370
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303371static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003372{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003373 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3374
Michael Ellerman441c19c2014-05-23 18:15:25 +10003375 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003376
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303377 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003378
Paul Mackerrasde56a942011-06-29 00:21:34 +00003379 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003380}
3381
3382/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303383static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3384 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003385{
3386 return EMULATE_FAIL;
3387}
3388
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303389static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3390 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003391{
3392 return EMULATE_FAIL;
3393}
3394
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303395static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3396 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003397{
3398 return EMULATE_FAIL;
3399}
3400
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303401static int kvmppc_core_check_processor_compat_hv(void)
3402{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003403 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3404 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303405 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003406 /*
3407 * Disable KVM for Power9, untill the required bits merged.
3408 */
3409 if (cpu_has_feature(CPU_FTR_ARCH_300))
3410 return -EIO;
3411
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303412 return 0;
3413}
3414
3415static long kvm_arch_vm_ioctl_hv(struct file *filp,
3416 unsigned int ioctl, unsigned long arg)
3417{
3418 struct kvm *kvm __maybe_unused = filp->private_data;
3419 void __user *argp = (void __user *)arg;
3420 long r;
3421
3422 switch (ioctl) {
3423
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303424 case KVM_PPC_ALLOCATE_HTAB: {
3425 u32 htab_order;
3426
3427 r = -EFAULT;
3428 if (get_user(htab_order, (u32 __user *)argp))
3429 break;
3430 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3431 if (r)
3432 break;
3433 r = -EFAULT;
3434 if (put_user(htab_order, (u32 __user *)argp))
3435 break;
3436 r = 0;
3437 break;
3438 }
3439
3440 case KVM_PPC_GET_HTAB_FD: {
3441 struct kvm_get_htab_fd ghf;
3442
3443 r = -EFAULT;
3444 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3445 break;
3446 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3447 break;
3448 }
3449
3450 default:
3451 r = -ENOTTY;
3452 }
3453
3454 return r;
3455}
3456
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003457/*
3458 * List of hcall numbers to enable by default.
3459 * For compatibility with old userspace, we enable by default
3460 * all hcalls that were implemented before the hcall-enabling
3461 * facility was added. Note this list should not include H_RTAS.
3462 */
3463static unsigned int default_hcall_list[] = {
3464 H_REMOVE,
3465 H_ENTER,
3466 H_READ,
3467 H_PROTECT,
3468 H_BULK_REMOVE,
3469 H_GET_TCE,
3470 H_PUT_TCE,
3471 H_SET_DABR,
3472 H_SET_XDABR,
3473 H_CEDE,
3474 H_PROD,
3475 H_CONFER,
3476 H_REGISTER_VPA,
3477#ifdef CONFIG_KVM_XICS
3478 H_EOI,
3479 H_CPPR,
3480 H_IPI,
3481 H_IPOLL,
3482 H_XIRR,
3483 H_XIRR_X,
3484#endif
3485 0
3486};
3487
3488static void init_default_hcalls(void)
3489{
3490 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003491 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003492
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003493 for (i = 0; default_hcall_list[i]; ++i) {
3494 hcall = default_hcall_list[i];
3495 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3496 __set_bit(hcall / 4, default_enabled_hcalls);
3497 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003498}
3499
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303500static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303501 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3502 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3503 .get_one_reg = kvmppc_get_one_reg_hv,
3504 .set_one_reg = kvmppc_set_one_reg_hv,
3505 .vcpu_load = kvmppc_core_vcpu_load_hv,
3506 .vcpu_put = kvmppc_core_vcpu_put_hv,
3507 .set_msr = kvmppc_set_msr_hv,
3508 .vcpu_run = kvmppc_vcpu_run_hv,
3509 .vcpu_create = kvmppc_core_vcpu_create_hv,
3510 .vcpu_free = kvmppc_core_vcpu_free_hv,
3511 .check_requests = kvmppc_core_check_requests_hv,
3512 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3513 .flush_memslot = kvmppc_core_flush_memslot_hv,
3514 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3515 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3516 .unmap_hva = kvm_unmap_hva_hv,
3517 .unmap_hva_range = kvm_unmap_hva_range_hv,
3518 .age_hva = kvm_age_hva_hv,
3519 .test_age_hva = kvm_test_age_hva_hv,
3520 .set_spte_hva = kvm_set_spte_hva_hv,
3521 .mmu_destroy = kvmppc_mmu_destroy_hv,
3522 .free_memslot = kvmppc_core_free_memslot_hv,
3523 .create_memslot = kvmppc_core_create_memslot_hv,
3524 .init_vm = kvmppc_core_init_vm_hv,
3525 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303526 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3527 .emulate_op = kvmppc_core_emulate_op_hv,
3528 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3529 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3530 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3531 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003532 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303533};
3534
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303535static int kvm_init_subcore_bitmap(void)
3536{
3537 int i, j;
3538 int nr_cores = cpu_nr_cores();
3539 struct sibling_subcore_state *sibling_subcore_state;
3540
3541 for (i = 0; i < nr_cores; i++) {
3542 int first_cpu = i * threads_per_core;
3543 int node = cpu_to_node(first_cpu);
3544
3545 /* Ignore if it is already allocated. */
3546 if (paca[first_cpu].sibling_subcore_state)
3547 continue;
3548
3549 sibling_subcore_state =
3550 kmalloc_node(sizeof(struct sibling_subcore_state),
3551 GFP_KERNEL, node);
3552 if (!sibling_subcore_state)
3553 return -ENOMEM;
3554
3555 memset(sibling_subcore_state, 0,
3556 sizeof(struct sibling_subcore_state));
3557
3558 for (j = 0; j < threads_per_core; j++) {
3559 int cpu = first_cpu + j;
3560
3561 paca[cpu].sibling_subcore_state = sibling_subcore_state;
3562 }
3563 }
3564 return 0;
3565}
3566
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303567static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003568{
3569 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303570 /*
3571 * FIXME!! Do we need to check on all cpus ?
3572 */
3573 r = kvmppc_core_check_processor_compat_hv();
3574 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003575 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003576
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303577 r = kvm_init_subcore_bitmap();
3578 if (r)
3579 return r;
3580
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303581 kvm_ops_hv.owner = THIS_MODULE;
3582 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003583
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003584 init_default_hcalls();
3585
Paul Mackerrasec257162015-06-24 21:18:03 +10003586 init_vcore_lists();
3587
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303588 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003589 return r;
3590}
3591
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303592static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003593{
Suresh Warrier79b6c242015-12-17 14:59:06 -06003594 kvmppc_free_host_rm_ops();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303595 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003596}
3597
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303598module_init(kvmppc_book3s_init_hv);
3599module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05303600MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01003601MODULE_ALIAS_MISCDEV(KVM_MINOR);
3602MODULE_ALIAS("devname:kvm");