blob: 5f08ed070ae599da611ee9107d367045ca689db0 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000043#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000049#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000051#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000053#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110054#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053055#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100056#include <asm/pnv-pci.h>
Paul Mackerras7a840842016-11-16 22:25:20 +110057#include <asm/mmu.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110058#include <asm/opal.h>
59#include <asm/xics.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000060#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000061#include <linux/vmalloc.h>
62#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000063#include <linux/hugetlb.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100064#include <linux/kvm_irqfd.h>
65#include <linux/irqbypass.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053066#include <linux/module.h>
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +100067#include <linux/compiler.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110068#include <linux/of.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000069
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053070#include "book3s.h"
71
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060072#define CREATE_TRACE_POINTS
73#include "trace_hv.h"
74
Paul Mackerrasde56a942011-06-29 00:21:34 +000075/* #define EXIT_DEBUG */
76/* #define EXIT_DEBUG_SIMPLE */
77/* #define EXIT_DEBUG_INT */
78
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000079/* Used to indicate that a guest page fault needs to be handled */
80#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100081/* Used to indicate that a guest passthrough interrupt needs to be handled */
82#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000083
Paul Mackerrasc7b67672012-10-15 01:18:07 +000084/* Used as a "null" value for timebase values */
85#define TB_NIL (~(u64)0)
86
Paul Mackerras699a0ea2014-06-02 11:02:59 +100087static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
88
Paul Mackerrasb4deba52015-07-02 20:38:16 +100089static int dynamic_mt_modes = 6;
90module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
91MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
Paul Mackerrasec257162015-06-24 21:18:03 +100092static int target_smt_mode;
93module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100095
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -060096#ifdef CONFIG_KVM_XICS
97static struct kernel_param_ops module_param_ops = {
98 .set = param_set_int,
99 .get = param_get_int,
100};
101
Suresh Warrier644abbb2016-08-19 15:35:54 +1000102module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
103 S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
105
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600106module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
107 S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
109#endif
110
Paul Mackerras19ccb762011-07-23 17:42:46 +1000111static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +0000112static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000113
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000114static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
115 int *ip)
116{
117 int i = *ip;
118 struct kvm_vcpu *vcpu;
119
120 while (++i < MAX_SMT_THREADS) {
121 vcpu = READ_ONCE(vc->runnable_threads[i]);
122 if (vcpu) {
123 *ip = i;
124 return vcpu;
125 }
126 }
127 return NULL;
128}
129
130/* Used to traverse the list of runnable threads for a given vcore */
131#define for_each_runnable_thread(i, vcpu, vc) \
132 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
133
Paul Mackerras66feed62015-03-28 14:21:12 +1100134static bool kvmppc_ipi_thread(int cpu)
135{
Paul Mackerras1704a812016-11-18 08:47:08 +1100136 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
137
138 /* On POWER9 we can use msgsnd to IPI any cpu */
139 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
140 msg |= get_hard_smp_processor_id(cpu);
141 smp_mb();
142 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
143 return true;
144 }
145
Paul Mackerras66feed62015-03-28 14:21:12 +1100146 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
147 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
148 preempt_disable();
149 if (cpu_first_thread_sibling(cpu) ==
150 cpu_first_thread_sibling(smp_processor_id())) {
Paul Mackerras66feed62015-03-28 14:21:12 +1100151 msg |= cpu_thread_in_core(cpu);
152 smp_mb();
153 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
154 preempt_enable();
155 return true;
156 }
157 preempt_enable();
158 }
159
160#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrasf7257582016-11-18 09:02:08 +1100161 if (cpu >= 0 && cpu < nr_cpu_ids) {
162 if (paca[cpu].kvm_hstate.xics_phys) {
163 xics_wake_cpu(cpu);
164 return true;
165 }
166 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
Paul Mackerras66feed62015-03-28 14:21:12 +1100167 return true;
168 }
169#endif
170
171 return false;
172}
173
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530174static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000175{
Paul Mackerrasec257162015-06-24 21:18:03 +1000176 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100177 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000178
179 wqp = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100180 if (swait_active(wqp)) {
181 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000182 ++vcpu->stat.halt_wakeup;
183 }
184
Paul Mackerrasec257162015-06-24 21:18:03 +1000185 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100186 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000187
188 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000189 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100190 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
191 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000192}
193
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000194/*
195 * We use the vcpu_load/put functions to measure stolen time.
196 * Stolen time is counted as time when either the vcpu is able to
197 * run as part of a virtual core, but the task running the vcore
198 * is preempted or sleeping, or when the vcpu needs something done
199 * in the kernel by the task running the vcpu, but that task is
200 * preempted or sleeping. Those two things have to be counted
201 * separately, since one of the vcpu tasks will take on the job
202 * of running the core, and the other vcpu tasks in the vcore will
203 * sleep waiting for it to do that, but that sleep shouldn't count
204 * as stolen time.
205 *
206 * Hence we accumulate stolen time when the vcpu can run as part of
207 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
208 * needs its task to do other things in the kernel (for example,
209 * service a page fault) in busy_stolen. We don't accumulate
210 * stolen time for a vcore when it is inactive, or for a vcpu
211 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
212 * a misnomer; it means that the vcpu task is not executing in
213 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
214 * the kernel. We don't have any way of dividing up that time
215 * between time that the vcpu is genuinely stopped, time that
216 * the task is actively working on behalf of the vcpu, and time
217 * that the task is preempted, so we don't count any of it as
218 * stolen.
219 *
220 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100221 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
222 * lock. The stolen times are measured in units of timebase ticks.
223 * (Note that the != TB_NIL checks below are purely defensive;
224 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000225 */
226
Paul Mackerrasec257162015-06-24 21:18:03 +1000227static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
228{
229 unsigned long flags;
230
231 spin_lock_irqsave(&vc->stoltb_lock, flags);
232 vc->preempt_tb = mftb();
233 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
234}
235
236static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
237{
238 unsigned long flags;
239
240 spin_lock_irqsave(&vc->stoltb_lock, flags);
241 if (vc->preempt_tb != TB_NIL) {
242 vc->stolen_tb += mftb() - vc->preempt_tb;
243 vc->preempt_tb = TB_NIL;
244 }
245 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
246}
247
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530248static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000249{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000250 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100251 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000252
Paul Mackerras2711e242014-12-04 16:43:28 +1100253 /*
254 * We can test vc->runner without taking the vcore lock,
255 * because only this task ever sets vc->runner to this
256 * vcpu, and once it is set to this vcpu, only this task
257 * ever sets it to NULL.
258 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000259 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
260 kvmppc_core_end_stolen(vc);
261
Paul Mackerras2711e242014-12-04 16:43:28 +1100262 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000263 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
264 vcpu->arch.busy_preempt != TB_NIL) {
265 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
266 vcpu->arch.busy_preempt = TB_NIL;
267 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100268 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000269}
270
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530271static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000272{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000273 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100274 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000275
Paul Mackerrasec257162015-06-24 21:18:03 +1000276 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
277 kvmppc_core_start_stolen(vc);
278
Paul Mackerras2711e242014-12-04 16:43:28 +1100279 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000280 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
281 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100282 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000283}
284
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530285static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000286{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100287 /*
288 * Check for illegal transactional state bit combination
289 * and if we find it, force the TS field to a safe state.
290 */
291 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
292 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000293 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000294 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000295}
296
Thomas Huth5358a962015-05-22 09:25:02 +0200297static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000298{
299 vcpu->arch.pvr = pvr;
300}
301
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100302/* Dummy value used in computing PCR value below */
303#define PCR_ARCH_300 (PCR_ARCH_207 << 1)
304
Thomas Huth5358a962015-05-22 09:25:02 +0200305static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000306{
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100307 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000308 struct kvmppc_vcore *vc = vcpu->arch.vcore;
309
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100310 /* We can (emulate) our own architecture version and anything older */
311 if (cpu_has_feature(CPU_FTR_ARCH_300))
312 host_pcr_bit = PCR_ARCH_300;
313 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
314 host_pcr_bit = PCR_ARCH_207;
315 else if (cpu_has_feature(CPU_FTR_ARCH_206))
316 host_pcr_bit = PCR_ARCH_206;
317 else
318 host_pcr_bit = PCR_ARCH_205;
319
320 /* Determine lowest PCR bit needed to run guest in given PVR level */
321 guest_pcr_bit = host_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000322 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000323 switch (arch_compat) {
324 case PVR_ARCH_205:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100325 guest_pcr_bit = PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000326 break;
327 case PVR_ARCH_206:
328 case PVR_ARCH_206p:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100329 guest_pcr_bit = PCR_ARCH_206;
Paul Mackerras5557ae02014-01-08 21:25:24 +1100330 break;
331 case PVR_ARCH_207:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100332 guest_pcr_bit = PCR_ARCH_207;
333 break;
334 case PVR_ARCH_300:
335 guest_pcr_bit = PCR_ARCH_300;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000336 break;
337 default:
338 return -EINVAL;
339 }
340 }
341
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100342 /* Check requested PCR bits don't exceed our capabilities */
343 if (guest_pcr_bit > host_pcr_bit)
344 return -EINVAL;
345
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000346 spin_lock(&vc->lock);
347 vc->arch_compat = arch_compat;
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100348 /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
349 vc->pcr = host_pcr_bit - guest_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000350 spin_unlock(&vc->lock);
351
352 return 0;
353}
354
Thomas Huth5358a962015-05-22 09:25:02 +0200355static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000356{
357 int r;
358
359 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
360 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
361 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
362 for (r = 0; r < 16; ++r)
363 pr_err("r%2d = %.16lx r%d = %.16lx\n",
364 r, kvmppc_get_gpr(vcpu, r),
365 r+16, kvmppc_get_gpr(vcpu, r+16));
366 pr_err("ctr = %.16lx lr = %.16lx\n",
367 vcpu->arch.ctr, vcpu->arch.lr);
368 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
369 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
370 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
371 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
372 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
373 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
374 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
375 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
376 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
377 pr_err("fault dar = %.16lx dsisr = %.8x\n",
378 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
379 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
380 for (r = 0; r < vcpu->arch.slb_max; ++r)
381 pr_err(" ESID = %.16llx VSID = %.16llx\n",
382 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
383 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000384 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000385 vcpu->arch.last_inst);
386}
387
Thomas Huth5358a962015-05-22 09:25:02 +0200388static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000389{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100390 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000391
392 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100393 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000394 mutex_unlock(&kvm->lock);
395 return ret;
396}
397
398static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
399{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000400 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200401 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000402}
403
Paul Mackerras55b665b2012-09-25 20:33:06 +0000404static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
405 unsigned long addr, unsigned long len)
406{
407 /* check address is cacheline aligned */
408 if (addr & (L1_CACHE_BYTES - 1))
409 return -EINVAL;
410 spin_lock(&vcpu->arch.vpa_update_lock);
411 if (v->next_gpa != addr || v->len != len) {
412 v->next_gpa = addr;
413 v->len = addr ? len : 0;
414 v->update_pending = 1;
415 }
416 spin_unlock(&vcpu->arch.vpa_update_lock);
417 return 0;
418}
419
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000420/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
421struct reg_vpa {
422 u32 dummy;
423 union {
Alexander Graf02407552014-06-11 10:34:19 +0200424 __be16 hword;
425 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000426 } length;
427};
428
429static int vpa_is_registered(struct kvmppc_vpa *vpap)
430{
431 if (vpap->update_pending)
432 return vpap->next_gpa != 0;
433 return vpap->pinned_addr != NULL;
434}
435
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000436static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
437 unsigned long flags,
438 unsigned long vcpuid, unsigned long vpa)
439{
440 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000441 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000442 void *va;
443 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000444 int err;
445 int subfunc;
446 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000447
448 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
449 if (!tvcpu)
450 return H_PARAMETER;
451
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000452 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
453 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
454 subfunc == H_VPA_REG_SLB) {
455 /* Registering new area - address must be cache-line aligned */
456 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000457 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000458
459 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000460 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
461 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000462 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000463 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200464 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000465 else
Alexander Graf02407552014-06-11 10:34:19 +0200466 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000467 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000468
469 /* Check length */
470 if (len > nb || len < sizeof(struct reg_vpa))
471 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000472 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000473 vpa = 0;
474 len = 0;
475 }
476
477 err = H_PARAMETER;
478 vpap = NULL;
479 spin_lock(&tvcpu->arch.vpa_update_lock);
480
481 switch (subfunc) {
482 case H_VPA_REG_VPA: /* register VPA */
483 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000484 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000485 vpap = &tvcpu->arch.vpa;
486 err = 0;
487 break;
488
489 case H_VPA_REG_DTL: /* register DTL */
490 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000491 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000492 len -= len % sizeof(struct dtl_entry);
493
494 /* Check that they have previously registered a VPA */
495 err = H_RESOURCE;
496 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000497 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000498
499 vpap = &tvcpu->arch.dtl;
500 err = 0;
501 break;
502
503 case H_VPA_REG_SLB: /* register SLB shadow buffer */
504 /* Check that they have previously registered a VPA */
505 err = H_RESOURCE;
506 if (!vpa_is_registered(&tvcpu->arch.vpa))
507 break;
508
509 vpap = &tvcpu->arch.slb_shadow;
510 err = 0;
511 break;
512
513 case H_VPA_DEREG_VPA: /* deregister VPA */
514 /* Check they don't still have a DTL or SLB buf registered */
515 err = H_RESOURCE;
516 if (vpa_is_registered(&tvcpu->arch.dtl) ||
517 vpa_is_registered(&tvcpu->arch.slb_shadow))
518 break;
519
520 vpap = &tvcpu->arch.vpa;
521 err = 0;
522 break;
523
524 case H_VPA_DEREG_DTL: /* deregister DTL */
525 vpap = &tvcpu->arch.dtl;
526 err = 0;
527 break;
528
529 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
530 vpap = &tvcpu->arch.slb_shadow;
531 err = 0;
532 break;
533 }
534
535 if (vpap) {
536 vpap->next_gpa = vpa;
537 vpap->len = len;
538 vpap->update_pending = 1;
539 }
540
541 spin_unlock(&tvcpu->arch.vpa_update_lock);
542
543 return err;
544}
545
Paul Mackerras081f3232012-06-01 20:20:24 +1000546static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000547{
Paul Mackerras081f3232012-06-01 20:20:24 +1000548 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000549 void *va;
550 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000551 unsigned long gpa;
552
553 /*
554 * We need to pin the page pointed to by vpap->next_gpa,
555 * but we can't call kvmppc_pin_guest_page under the lock
556 * as it does get_user_pages() and down_read(). So we
557 * have to drop the lock, pin the page, then get the lock
558 * again and check that a new area didn't get registered
559 * in the meantime.
560 */
561 for (;;) {
562 gpa = vpap->next_gpa;
563 spin_unlock(&vcpu->arch.vpa_update_lock);
564 va = NULL;
565 nb = 0;
566 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000567 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000568 spin_lock(&vcpu->arch.vpa_update_lock);
569 if (gpa == vpap->next_gpa)
570 break;
571 /* sigh... unpin that one and try again */
572 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000573 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000574 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000575
576 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000577 if (va && nb < vpap->len) {
578 /*
579 * If it's now too short, it must be that userspace
580 * has changed the mappings underlying guest memory,
581 * so unregister the region.
582 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000583 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000584 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000585 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000586 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000587 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
588 vpap->dirty);
589 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000590 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000591 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000592 if (va)
593 vpap->pinned_end = va + vpap->len;
594}
Paul Mackerras93e60242011-12-12 12:28:55 +0000595
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000596static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
597{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000598 if (!(vcpu->arch.vpa.update_pending ||
599 vcpu->arch.slb_shadow.update_pending ||
600 vcpu->arch.dtl.update_pending))
601 return;
602
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000603 spin_lock(&vcpu->arch.vpa_update_lock);
604 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000605 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000606 if (vcpu->arch.vpa.pinned_addr)
607 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000608 }
609 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000610 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000611 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
612 vcpu->arch.dtl_index = 0;
613 }
614 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000615 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000616 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000617}
618
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000619/*
620 * Return the accumulated stolen time for the vcore up until `now'.
621 * The caller should hold the vcore lock.
622 */
623static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
624{
625 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100626 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000627
Paul Mackerras2711e242014-12-04 16:43:28 +1100628 spin_lock_irqsave(&vc->stoltb_lock, flags);
629 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000630 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100631 vc->preempt_tb != TB_NIL)
632 p += now - vc->preempt_tb;
633 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000634 return p;
635}
636
Paul Mackerras0456ec42012-02-03 00:56:21 +0000637static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
638 struct kvmppc_vcore *vc)
639{
640 struct dtl_entry *dt;
641 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000642 unsigned long stolen;
643 unsigned long core_stolen;
644 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000645
646 dt = vcpu->arch.dtl_ptr;
647 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000648 now = mftb();
649 core_stolen = vcore_stolen_time(vc, now);
650 stolen = core_stolen - vcpu->arch.stolen_logged;
651 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100652 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000653 stolen += vcpu->arch.busy_stolen;
654 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100655 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000656 if (!dt || !vpa)
657 return;
658 memset(dt, 0, sizeof(struct dtl_entry));
659 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200660 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
661 dt->timebase = cpu_to_be64(now + vc->tb_offset);
662 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
663 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
664 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000665 ++dt;
666 if (dt == vcpu->arch.dtl.pinned_end)
667 dt = vcpu->arch.dtl.pinned_addr;
668 vcpu->arch.dtl_ptr = dt;
669 /* order writing *dt vs. writing vpa->dtl_idx */
670 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200671 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000672 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000673}
674
Michael Neuling96423822014-06-02 11:03:01 +1000675static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
676{
677 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
678 return true;
679 if ((!vcpu->arch.vcore->arch_compat) &&
680 cpu_has_feature(CPU_FTR_ARCH_207S))
681 return true;
682 return false;
683}
684
685static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
686 unsigned long resource, unsigned long value1,
687 unsigned long value2)
688{
689 switch (resource) {
690 case H_SET_MODE_RESOURCE_SET_CIABR:
691 if (!kvmppc_power8_compatible(vcpu))
692 return H_P2;
693 if (value2)
694 return H_P4;
695 if (mflags)
696 return H_UNSUPPORTED_FLAG_START;
697 /* Guests can't breakpoint the hypervisor */
698 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
699 return H_P3;
700 vcpu->arch.ciabr = value1;
701 return H_SUCCESS;
702 case H_SET_MODE_RESOURCE_SET_DAWR:
703 if (!kvmppc_power8_compatible(vcpu))
704 return H_P2;
705 if (mflags)
706 return H_UNSUPPORTED_FLAG_START;
707 if (value2 & DABRX_HYP)
708 return H_P4;
709 vcpu->arch.dawr = value1;
710 vcpu->arch.dawrx = value2;
711 return H_SUCCESS;
712 default:
713 return H_TOO_HARD;
714 }
715}
716
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100717static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
718{
719 struct kvmppc_vcore *vcore = target->arch.vcore;
720
721 /*
722 * We expect to have been called by the real mode handler
723 * (kvmppc_rm_h_confer()) which would have directly returned
724 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
725 * have useful work to do and should not confer) so we don't
726 * recheck that here.
727 */
728
729 spin_lock(&vcore->lock);
730 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000731 vcore->vcore_state != VCORE_INACTIVE &&
732 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100733 target = vcore->runner;
734 spin_unlock(&vcore->lock);
735
736 return kvm_vcpu_yield_to(target);
737}
738
739static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
740{
741 int yield_count = 0;
742 struct lppaca *lppaca;
743
744 spin_lock(&vcpu->arch.vpa_update_lock);
745 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
746 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100747 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100748 spin_unlock(&vcpu->arch.vpa_update_lock);
749 return yield_count;
750}
751
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000752int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
753{
754 unsigned long req = kvmppc_get_gpr(vcpu, 3);
755 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100756 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000757 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000758 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000759
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000760 if (req <= MAX_HCALL_OPCODE &&
761 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
762 return RESUME_HOST;
763
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000764 switch (req) {
765 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000766 break;
767 case H_PROD:
768 target = kvmppc_get_gpr(vcpu, 4);
769 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
770 if (!tvcpu) {
771 ret = H_PARAMETER;
772 break;
773 }
774 tvcpu->arch.prodded = 1;
775 smp_mb();
776 if (vcpu->arch.ceded) {
Marcelo Tosatti85773702016-02-19 09:46:39 +0100777 if (swait_active(&vcpu->wq)) {
778 swake_up(&vcpu->wq);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000779 vcpu->stat.halt_wakeup++;
780 }
781 }
782 break;
783 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000784 target = kvmppc_get_gpr(vcpu, 4);
785 if (target == -1)
786 break;
787 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
788 if (!tvcpu) {
789 ret = H_PARAMETER;
790 break;
791 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100792 yield_count = kvmppc_get_gpr(vcpu, 5);
793 if (kvmppc_get_yield_count(tvcpu) != yield_count)
794 break;
795 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000796 break;
797 case H_REGISTER_VPA:
798 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
799 kvmppc_get_gpr(vcpu, 5),
800 kvmppc_get_gpr(vcpu, 6));
801 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000802 case H_RTAS:
803 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
804 return RESUME_HOST;
805
Paul Mackerrasc9438092013-11-16 17:46:05 +1100806 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000807 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100808 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000809
810 if (rc == -ENOENT)
811 return RESUME_HOST;
812 else if (rc == 0)
813 break;
814
815 /* Send the error out to userspace via KVM_RUN */
816 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100817 case H_LOGICAL_CI_LOAD:
818 ret = kvmppc_h_logical_ci_load(vcpu);
819 if (ret == H_TOO_HARD)
820 return RESUME_HOST;
821 break;
822 case H_LOGICAL_CI_STORE:
823 ret = kvmppc_h_logical_ci_store(vcpu);
824 if (ret == H_TOO_HARD)
825 return RESUME_HOST;
826 break;
Michael Neuling96423822014-06-02 11:03:01 +1000827 case H_SET_MODE:
828 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
829 kvmppc_get_gpr(vcpu, 5),
830 kvmppc_get_gpr(vcpu, 6),
831 kvmppc_get_gpr(vcpu, 7));
832 if (ret == H_TOO_HARD)
833 return RESUME_HOST;
834 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000835 case H_XIRR:
836 case H_CPPR:
837 case H_EOI:
838 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000839 case H_IPOLL:
840 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000841 if (kvmppc_xics_enabled(vcpu)) {
842 ret = kvmppc_xics_hcall(vcpu, req);
843 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100844 }
845 return RESUME_HOST;
846 case H_PUT_TCE:
847 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
848 kvmppc_get_gpr(vcpu, 5),
849 kvmppc_get_gpr(vcpu, 6));
850 if (ret == H_TOO_HARD)
851 return RESUME_HOST;
852 break;
853 case H_PUT_TCE_INDIRECT:
854 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
855 kvmppc_get_gpr(vcpu, 5),
856 kvmppc_get_gpr(vcpu, 6),
857 kvmppc_get_gpr(vcpu, 7));
858 if (ret == H_TOO_HARD)
859 return RESUME_HOST;
860 break;
861 case H_STUFF_TCE:
862 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
863 kvmppc_get_gpr(vcpu, 5),
864 kvmppc_get_gpr(vcpu, 6),
865 kvmppc_get_gpr(vcpu, 7));
866 if (ret == H_TOO_HARD)
867 return RESUME_HOST;
868 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000869 default:
870 return RESUME_HOST;
871 }
872 kvmppc_set_gpr(vcpu, 3, ret);
873 vcpu->arch.hcall_needed = 0;
874 return RESUME_GUEST;
875}
876
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000877static int kvmppc_hcall_impl_hv(unsigned long cmd)
878{
879 switch (cmd) {
880 case H_CEDE:
881 case H_PROD:
882 case H_CONFER:
883 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000884 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100885 case H_LOGICAL_CI_LOAD:
886 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000887#ifdef CONFIG_KVM_XICS
888 case H_XIRR:
889 case H_CPPR:
890 case H_EOI:
891 case H_IPI:
892 case H_IPOLL:
893 case H_XIRR_X:
894#endif
895 return 1;
896 }
897
898 /* See if it's in the real-mode table */
899 return kvmppc_hcall_impl_hv_realmode(cmd);
900}
901
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530902static int kvmppc_emulate_debug_inst(struct kvm_run *run,
903 struct kvm_vcpu *vcpu)
904{
905 u32 last_inst;
906
907 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
908 EMULATE_DONE) {
909 /*
910 * Fetch failed, so return to guest and
911 * try executing it again.
912 */
913 return RESUME_GUEST;
914 }
915
916 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
917 run->exit_reason = KVM_EXIT_DEBUG;
918 run->debug.arch.address = kvmppc_get_pc(vcpu);
919 return RESUME_HOST;
920 } else {
921 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
922 return RESUME_GUEST;
923 }
924}
925
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530926static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
927 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000928{
929 int r = RESUME_HOST;
930
931 vcpu->stat.sum_exits++;
932
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100933 /*
934 * This can happen if an interrupt occurs in the last stages
935 * of guest entry or the first stages of guest exit (i.e. after
936 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
937 * and before setting it to KVM_GUEST_MODE_HOST_HV).
938 * That can happen due to a bug, or due to a machine check
939 * occurring at just the wrong time.
940 */
941 if (vcpu->arch.shregs.msr & MSR_HV) {
942 printk(KERN_EMERG "KVM trap in HV mode!\n");
943 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
944 vcpu->arch.trap, kvmppc_get_pc(vcpu),
945 vcpu->arch.shregs.msr);
946 kvmppc_dump_regs(vcpu);
947 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
948 run->hw.hardware_exit_reason = vcpu->arch.trap;
949 return RESUME_HOST;
950 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000951 run->exit_reason = KVM_EXIT_UNKNOWN;
952 run->ready_for_interrupt_injection = 1;
953 switch (vcpu->arch.trap) {
954 /* We're good on these - the host merely wanted to get our attention */
955 case BOOK3S_INTERRUPT_HV_DECREMENTER:
956 vcpu->stat.dec_exits++;
957 r = RESUME_GUEST;
958 break;
959 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100960 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerras84f71392016-11-22 14:30:14 +1100961 case BOOK3S_INTERRUPT_H_VIRT:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000962 vcpu->stat.ext_intr_exits++;
963 r = RESUME_GUEST;
964 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100965 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
966 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000967 case BOOK3S_INTERRUPT_PERFMON:
968 r = RESUME_GUEST;
969 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000970 case BOOK3S_INTERRUPT_MACHINE_CHECK:
971 /*
972 * Deliver a machine check interrupt to the guest.
973 * We have to do this, even if the host has handled the
974 * machine check, because machine checks use SRR0/1 and
975 * the interrupt might have trashed guest state in them.
976 */
977 kvmppc_book3s_queue_irqprio(vcpu,
978 BOOK3S_INTERRUPT_MACHINE_CHECK);
979 r = RESUME_GUEST;
980 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000981 case BOOK3S_INTERRUPT_PROGRAM:
982 {
983 ulong flags;
984 /*
985 * Normally program interrupts are delivered directly
986 * to the guest by the hardware, but we can get here
987 * as a result of a hypervisor emulation interrupt
988 * (e40) getting turned into a 700 by BML RTAS.
989 */
990 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
991 kvmppc_core_queue_program(vcpu, flags);
992 r = RESUME_GUEST;
993 break;
994 }
995 case BOOK3S_INTERRUPT_SYSCALL:
996 {
997 /* hcall - punt to userspace */
998 int i;
999
Liu Ping Fan27025a62013-11-19 14:12:48 +08001000 /* hypercall with MSR_PR has already been handled in rmode,
1001 * and never reaches here.
1002 */
1003
Paul Mackerrasde56a942011-06-29 00:21:34 +00001004 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1005 for (i = 0; i < 9; ++i)
1006 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1007 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1008 vcpu->arch.hcall_needed = 1;
1009 r = RESUME_HOST;
1010 break;
1011 }
1012 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001013 * We get these next two if the guest accesses a page which it thinks
1014 * it has mapped but which is not actually present, either because
1015 * it is for an emulated I/O device or because the corresonding
1016 * host page has been paged out. Any other HDSI/HISI interrupts
1017 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001018 */
1019 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001020 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001021 break;
1022 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001023 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1024 vcpu->arch.fault_dsisr = 0;
1025 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001026 break;
1027 /*
1028 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301029 * If the guest debug is disabled, generate a program interrupt
1030 * to the guest. If guest debug is enabled, we need to check
1031 * whether the instruction is a software breakpoint instruction.
1032 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001033 */
1034 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001035 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1036 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1037 swab32(vcpu->arch.emul_inst) :
1038 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301039 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1040 r = kvmppc_emulate_debug_inst(run, vcpu);
1041 } else {
1042 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1043 r = RESUME_GUEST;
1044 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001045 break;
1046 /*
1047 * This occurs if the guest (kernel or userspace), does something that
1048 * is prohibited by HFSCR. We just generate a program interrupt to
1049 * the guest.
1050 */
1051 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1052 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001053 r = RESUME_GUEST;
1054 break;
Suresh Warrierf7af5202016-08-19 15:35:52 +10001055 case BOOK3S_INTERRUPT_HV_RM_HARD:
1056 r = RESUME_PASSTHROUGH;
1057 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001058 default:
1059 kvmppc_dump_regs(vcpu);
1060 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1061 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1062 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001063 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001064 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001065 break;
1066 }
1067
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068 return r;
1069}
1070
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301071static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1072 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001073{
1074 int i;
1075
Paul Mackerrasde56a942011-06-29 00:21:34 +00001076 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301077 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001078 for (i = 0; i < vcpu->arch.slb_max; i++) {
1079 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1080 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1081 }
1082
1083 return 0;
1084}
1085
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301086static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1087 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001088{
1089 int i, j;
1090
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001091 /* Only accept the same PVR as the host's, since we can't spoof it */
1092 if (sregs->pvr != vcpu->arch.pvr)
1093 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001094
1095 j = 0;
1096 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1097 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1098 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1099 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1100 ++j;
1101 }
1102 }
1103 vcpu->arch.slb_max = j;
1104
1105 return 0;
1106}
1107
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001108static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1109 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001110{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001111 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001112 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1113 u64 mask;
1114
Paul Mackerras8f902b02015-03-20 20:39:38 +11001115 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001116 spin_lock(&vc->lock);
1117 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001118 * If ILE (interrupt little-endian) has changed, update the
1119 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1120 */
1121 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001122 struct kvm_vcpu *vcpu;
1123 int i;
1124
Anton Blanchardd6829162014-01-08 21:25:30 +11001125 kvm_for_each_vcpu(i, vcpu, kvm) {
1126 if (vcpu->arch.vcore != vc)
1127 continue;
1128 if (new_lpcr & LPCR_ILE)
1129 vcpu->arch.intr_msr |= MSR_LE;
1130 else
1131 vcpu->arch.intr_msr &= ~MSR_LE;
1132 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001133 }
1134
1135 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001136 * Userspace can only modify DPFD (default prefetch depth),
1137 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001138 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001139 */
1140 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001141 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1142 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001143
1144 /* Broken 32-bit version of LPCR must not clear top bits */
1145 if (preserve_top32)
1146 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001147 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1148 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001149 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001150}
1151
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301152static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1153 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001154{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001155 int r = 0;
1156 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001157
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001158 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301159 case KVM_REG_PPC_DEBUG_INST:
1160 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1161 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001162 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001163 *val = get_reg_val(id, 0);
1164 break;
1165 case KVM_REG_PPC_DABR:
1166 *val = get_reg_val(id, vcpu->arch.dabr);
1167 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001168 case KVM_REG_PPC_DABRX:
1169 *val = get_reg_val(id, vcpu->arch.dabrx);
1170 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001171 case KVM_REG_PPC_DSCR:
1172 *val = get_reg_val(id, vcpu->arch.dscr);
1173 break;
1174 case KVM_REG_PPC_PURR:
1175 *val = get_reg_val(id, vcpu->arch.purr);
1176 break;
1177 case KVM_REG_PPC_SPURR:
1178 *val = get_reg_val(id, vcpu->arch.spurr);
1179 break;
1180 case KVM_REG_PPC_AMR:
1181 *val = get_reg_val(id, vcpu->arch.amr);
1182 break;
1183 case KVM_REG_PPC_UAMOR:
1184 *val = get_reg_val(id, vcpu->arch.uamor);
1185 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001186 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001187 i = id - KVM_REG_PPC_MMCR0;
1188 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1189 break;
1190 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1191 i = id - KVM_REG_PPC_PMC1;
1192 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001193 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001194 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1195 i = id - KVM_REG_PPC_SPMC1;
1196 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1197 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001198 case KVM_REG_PPC_SIAR:
1199 *val = get_reg_val(id, vcpu->arch.siar);
1200 break;
1201 case KVM_REG_PPC_SDAR:
1202 *val = get_reg_val(id, vcpu->arch.sdar);
1203 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001204 case KVM_REG_PPC_SIER:
1205 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001206 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001207 case KVM_REG_PPC_IAMR:
1208 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001209 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001210 case KVM_REG_PPC_PSPB:
1211 *val = get_reg_val(id, vcpu->arch.pspb);
1212 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001213 case KVM_REG_PPC_DPDES:
1214 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1215 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001216 case KVM_REG_PPC_VTB:
1217 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
1218 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001219 case KVM_REG_PPC_DAWR:
1220 *val = get_reg_val(id, vcpu->arch.dawr);
1221 break;
1222 case KVM_REG_PPC_DAWRX:
1223 *val = get_reg_val(id, vcpu->arch.dawrx);
1224 break;
1225 case KVM_REG_PPC_CIABR:
1226 *val = get_reg_val(id, vcpu->arch.ciabr);
1227 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001228 case KVM_REG_PPC_CSIGR:
1229 *val = get_reg_val(id, vcpu->arch.csigr);
1230 break;
1231 case KVM_REG_PPC_TACR:
1232 *val = get_reg_val(id, vcpu->arch.tacr);
1233 break;
1234 case KVM_REG_PPC_TCSCR:
1235 *val = get_reg_val(id, vcpu->arch.tcscr);
1236 break;
1237 case KVM_REG_PPC_PID:
1238 *val = get_reg_val(id, vcpu->arch.pid);
1239 break;
1240 case KVM_REG_PPC_ACOP:
1241 *val = get_reg_val(id, vcpu->arch.acop);
1242 break;
1243 case KVM_REG_PPC_WORT:
1244 *val = get_reg_val(id, vcpu->arch.wort);
1245 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001246 case KVM_REG_PPC_TIDR:
1247 *val = get_reg_val(id, vcpu->arch.tid);
1248 break;
1249 case KVM_REG_PPC_PSSCR:
1250 *val = get_reg_val(id, vcpu->arch.psscr);
1251 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001252 case KVM_REG_PPC_VPA_ADDR:
1253 spin_lock(&vcpu->arch.vpa_update_lock);
1254 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1255 spin_unlock(&vcpu->arch.vpa_update_lock);
1256 break;
1257 case KVM_REG_PPC_VPA_SLB:
1258 spin_lock(&vcpu->arch.vpa_update_lock);
1259 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1260 val->vpaval.length = vcpu->arch.slb_shadow.len;
1261 spin_unlock(&vcpu->arch.vpa_update_lock);
1262 break;
1263 case KVM_REG_PPC_VPA_DTL:
1264 spin_lock(&vcpu->arch.vpa_update_lock);
1265 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1266 val->vpaval.length = vcpu->arch.dtl.len;
1267 spin_unlock(&vcpu->arch.vpa_update_lock);
1268 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001269 case KVM_REG_PPC_TB_OFFSET:
1270 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1271 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001272 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001273 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001274 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1275 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001276 case KVM_REG_PPC_PPR:
1277 *val = get_reg_val(id, vcpu->arch.ppr);
1278 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001279#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1280 case KVM_REG_PPC_TFHAR:
1281 *val = get_reg_val(id, vcpu->arch.tfhar);
1282 break;
1283 case KVM_REG_PPC_TFIAR:
1284 *val = get_reg_val(id, vcpu->arch.tfiar);
1285 break;
1286 case KVM_REG_PPC_TEXASR:
1287 *val = get_reg_val(id, vcpu->arch.texasr);
1288 break;
1289 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1290 i = id - KVM_REG_PPC_TM_GPR0;
1291 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1292 break;
1293 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1294 {
1295 int j;
1296 i = id - KVM_REG_PPC_TM_VSR0;
1297 if (i < 32)
1298 for (j = 0; j < TS_FPRWIDTH; j++)
1299 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1300 else {
1301 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1302 val->vval = vcpu->arch.vr_tm.vr[i-32];
1303 else
1304 r = -ENXIO;
1305 }
1306 break;
1307 }
1308 case KVM_REG_PPC_TM_CR:
1309 *val = get_reg_val(id, vcpu->arch.cr_tm);
1310 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001311 case KVM_REG_PPC_TM_XER:
1312 *val = get_reg_val(id, vcpu->arch.xer_tm);
1313 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001314 case KVM_REG_PPC_TM_LR:
1315 *val = get_reg_val(id, vcpu->arch.lr_tm);
1316 break;
1317 case KVM_REG_PPC_TM_CTR:
1318 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1319 break;
1320 case KVM_REG_PPC_TM_FPSCR:
1321 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1322 break;
1323 case KVM_REG_PPC_TM_AMR:
1324 *val = get_reg_val(id, vcpu->arch.amr_tm);
1325 break;
1326 case KVM_REG_PPC_TM_PPR:
1327 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1328 break;
1329 case KVM_REG_PPC_TM_VRSAVE:
1330 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1331 break;
1332 case KVM_REG_PPC_TM_VSCR:
1333 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1334 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1335 else
1336 r = -ENXIO;
1337 break;
1338 case KVM_REG_PPC_TM_DSCR:
1339 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1340 break;
1341 case KVM_REG_PPC_TM_TAR:
1342 *val = get_reg_val(id, vcpu->arch.tar_tm);
1343 break;
1344#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001345 case KVM_REG_PPC_ARCH_COMPAT:
1346 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1347 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001348 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001349 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001350 break;
1351 }
1352
1353 return r;
1354}
1355
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301356static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1357 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001358{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001359 int r = 0;
1360 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001361 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001362
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001363 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001364 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001365 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001366 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001367 r = -EINVAL;
1368 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001369 case KVM_REG_PPC_DABR:
1370 vcpu->arch.dabr = set_reg_val(id, *val);
1371 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001372 case KVM_REG_PPC_DABRX:
1373 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1374 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001375 case KVM_REG_PPC_DSCR:
1376 vcpu->arch.dscr = set_reg_val(id, *val);
1377 break;
1378 case KVM_REG_PPC_PURR:
1379 vcpu->arch.purr = set_reg_val(id, *val);
1380 break;
1381 case KVM_REG_PPC_SPURR:
1382 vcpu->arch.spurr = set_reg_val(id, *val);
1383 break;
1384 case KVM_REG_PPC_AMR:
1385 vcpu->arch.amr = set_reg_val(id, *val);
1386 break;
1387 case KVM_REG_PPC_UAMOR:
1388 vcpu->arch.uamor = set_reg_val(id, *val);
1389 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001390 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001391 i = id - KVM_REG_PPC_MMCR0;
1392 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1393 break;
1394 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1395 i = id - KVM_REG_PPC_PMC1;
1396 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1397 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001398 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1399 i = id - KVM_REG_PPC_SPMC1;
1400 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1401 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001402 case KVM_REG_PPC_SIAR:
1403 vcpu->arch.siar = set_reg_val(id, *val);
1404 break;
1405 case KVM_REG_PPC_SDAR:
1406 vcpu->arch.sdar = set_reg_val(id, *val);
1407 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001408 case KVM_REG_PPC_SIER:
1409 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001410 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001411 case KVM_REG_PPC_IAMR:
1412 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001413 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001414 case KVM_REG_PPC_PSPB:
1415 vcpu->arch.pspb = set_reg_val(id, *val);
1416 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001417 case KVM_REG_PPC_DPDES:
1418 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1419 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001420 case KVM_REG_PPC_VTB:
1421 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
1422 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001423 case KVM_REG_PPC_DAWR:
1424 vcpu->arch.dawr = set_reg_val(id, *val);
1425 break;
1426 case KVM_REG_PPC_DAWRX:
1427 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1428 break;
1429 case KVM_REG_PPC_CIABR:
1430 vcpu->arch.ciabr = set_reg_val(id, *val);
1431 /* Don't allow setting breakpoints in hypervisor code */
1432 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1433 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1434 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001435 case KVM_REG_PPC_CSIGR:
1436 vcpu->arch.csigr = set_reg_val(id, *val);
1437 break;
1438 case KVM_REG_PPC_TACR:
1439 vcpu->arch.tacr = set_reg_val(id, *val);
1440 break;
1441 case KVM_REG_PPC_TCSCR:
1442 vcpu->arch.tcscr = set_reg_val(id, *val);
1443 break;
1444 case KVM_REG_PPC_PID:
1445 vcpu->arch.pid = set_reg_val(id, *val);
1446 break;
1447 case KVM_REG_PPC_ACOP:
1448 vcpu->arch.acop = set_reg_val(id, *val);
1449 break;
1450 case KVM_REG_PPC_WORT:
1451 vcpu->arch.wort = set_reg_val(id, *val);
1452 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001453 case KVM_REG_PPC_TIDR:
1454 vcpu->arch.tid = set_reg_val(id, *val);
1455 break;
1456 case KVM_REG_PPC_PSSCR:
1457 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
1458 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001459 case KVM_REG_PPC_VPA_ADDR:
1460 addr = set_reg_val(id, *val);
1461 r = -EINVAL;
1462 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1463 vcpu->arch.dtl.next_gpa))
1464 break;
1465 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1466 break;
1467 case KVM_REG_PPC_VPA_SLB:
1468 addr = val->vpaval.addr;
1469 len = val->vpaval.length;
1470 r = -EINVAL;
1471 if (addr && !vcpu->arch.vpa.next_gpa)
1472 break;
1473 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1474 break;
1475 case KVM_REG_PPC_VPA_DTL:
1476 addr = val->vpaval.addr;
1477 len = val->vpaval.length;
1478 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001479 if (addr && (len < sizeof(struct dtl_entry) ||
1480 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001481 break;
1482 len -= len % sizeof(struct dtl_entry);
1483 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1484 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001485 case KVM_REG_PPC_TB_OFFSET:
1486 /* round up to multiple of 2^24 */
1487 vcpu->arch.vcore->tb_offset =
1488 ALIGN(set_reg_val(id, *val), 1UL << 24);
1489 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001490 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001491 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1492 break;
1493 case KVM_REG_PPC_LPCR_64:
1494 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001495 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001496 case KVM_REG_PPC_PPR:
1497 vcpu->arch.ppr = set_reg_val(id, *val);
1498 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001499#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1500 case KVM_REG_PPC_TFHAR:
1501 vcpu->arch.tfhar = set_reg_val(id, *val);
1502 break;
1503 case KVM_REG_PPC_TFIAR:
1504 vcpu->arch.tfiar = set_reg_val(id, *val);
1505 break;
1506 case KVM_REG_PPC_TEXASR:
1507 vcpu->arch.texasr = set_reg_val(id, *val);
1508 break;
1509 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1510 i = id - KVM_REG_PPC_TM_GPR0;
1511 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1512 break;
1513 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1514 {
1515 int j;
1516 i = id - KVM_REG_PPC_TM_VSR0;
1517 if (i < 32)
1518 for (j = 0; j < TS_FPRWIDTH; j++)
1519 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1520 else
1521 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1522 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1523 else
1524 r = -ENXIO;
1525 break;
1526 }
1527 case KVM_REG_PPC_TM_CR:
1528 vcpu->arch.cr_tm = set_reg_val(id, *val);
1529 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11001530 case KVM_REG_PPC_TM_XER:
1531 vcpu->arch.xer_tm = set_reg_val(id, *val);
1532 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001533 case KVM_REG_PPC_TM_LR:
1534 vcpu->arch.lr_tm = set_reg_val(id, *val);
1535 break;
1536 case KVM_REG_PPC_TM_CTR:
1537 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1538 break;
1539 case KVM_REG_PPC_TM_FPSCR:
1540 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1541 break;
1542 case KVM_REG_PPC_TM_AMR:
1543 vcpu->arch.amr_tm = set_reg_val(id, *val);
1544 break;
1545 case KVM_REG_PPC_TM_PPR:
1546 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1547 break;
1548 case KVM_REG_PPC_TM_VRSAVE:
1549 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1550 break;
1551 case KVM_REG_PPC_TM_VSCR:
1552 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1553 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1554 else
1555 r = - ENXIO;
1556 break;
1557 case KVM_REG_PPC_TM_DSCR:
1558 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1559 break;
1560 case KVM_REG_PPC_TM_TAR:
1561 vcpu->arch.tar_tm = set_reg_val(id, *val);
1562 break;
1563#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001564 case KVM_REG_PPC_ARCH_COMPAT:
1565 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1566 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001567 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001568 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001569 break;
1570 }
1571
1572 return r;
1573}
1574
Paul Mackerras45c940b2016-11-18 17:43:30 +11001575/*
1576 * On POWER9, threads are independent and can be in different partitions.
1577 * Therefore we consider each thread to be a subcore.
1578 * There is a restriction that all threads have to be in the same
1579 * MMU mode (radix or HPT), unfortunately, but since we only support
1580 * HPT guests on a HPT host so far, that isn't an impediment yet.
1581 */
1582static int threads_per_vcore(void)
1583{
1584 if (cpu_has_feature(CPU_FTR_ARCH_300))
1585 return 1;
1586 return threads_per_subcore;
1587}
1588
Stewart Smithde9bdd12014-07-18 14:18:42 +10001589static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1590{
1591 struct kvmppc_vcore *vcore;
1592
1593 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1594
1595 if (vcore == NULL)
1596 return NULL;
1597
Stewart Smithde9bdd12014-07-18 14:18:42 +10001598 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001599 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001600 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001601 vcore->preempt_tb = TB_NIL;
1602 vcore->lpcr = kvm->arch.lpcr;
Paul Mackerras45c940b2016-11-18 17:43:30 +11001603 vcore->first_vcpuid = core * threads_per_vcore();
Stewart Smithde9bdd12014-07-18 14:18:42 +10001604 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001605 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001606
1607 return vcore;
1608}
1609
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001610#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1611static struct debugfs_timings_element {
1612 const char *name;
1613 size_t offset;
1614} timings[] = {
1615 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1616 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1617 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1618 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1619 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1620};
1621
1622#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1623
1624struct debugfs_timings_state {
1625 struct kvm_vcpu *vcpu;
1626 unsigned int buflen;
1627 char buf[N_TIMINGS * 100];
1628};
1629
1630static int debugfs_timings_open(struct inode *inode, struct file *file)
1631{
1632 struct kvm_vcpu *vcpu = inode->i_private;
1633 struct debugfs_timings_state *p;
1634
1635 p = kzalloc(sizeof(*p), GFP_KERNEL);
1636 if (!p)
1637 return -ENOMEM;
1638
1639 kvm_get_kvm(vcpu->kvm);
1640 p->vcpu = vcpu;
1641 file->private_data = p;
1642
1643 return nonseekable_open(inode, file);
1644}
1645
1646static int debugfs_timings_release(struct inode *inode, struct file *file)
1647{
1648 struct debugfs_timings_state *p = file->private_data;
1649
1650 kvm_put_kvm(p->vcpu->kvm);
1651 kfree(p);
1652 return 0;
1653}
1654
1655static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1656 size_t len, loff_t *ppos)
1657{
1658 struct debugfs_timings_state *p = file->private_data;
1659 struct kvm_vcpu *vcpu = p->vcpu;
1660 char *s, *buf_end;
1661 struct kvmhv_tb_accumulator tb;
1662 u64 count;
1663 loff_t pos;
1664 ssize_t n;
1665 int i, loops;
1666 bool ok;
1667
1668 if (!p->buflen) {
1669 s = p->buf;
1670 buf_end = s + sizeof(p->buf);
1671 for (i = 0; i < N_TIMINGS; ++i) {
1672 struct kvmhv_tb_accumulator *acc;
1673
1674 acc = (struct kvmhv_tb_accumulator *)
1675 ((unsigned long)vcpu + timings[i].offset);
1676 ok = false;
1677 for (loops = 0; loops < 1000; ++loops) {
1678 count = acc->seqcount;
1679 if (!(count & 1)) {
1680 smp_rmb();
1681 tb = *acc;
1682 smp_rmb();
1683 if (count == acc->seqcount) {
1684 ok = true;
1685 break;
1686 }
1687 }
1688 udelay(1);
1689 }
1690 if (!ok)
1691 snprintf(s, buf_end - s, "%s: stuck\n",
1692 timings[i].name);
1693 else
1694 snprintf(s, buf_end - s,
1695 "%s: %llu %llu %llu %llu\n",
1696 timings[i].name, count / 2,
1697 tb_to_ns(tb.tb_total),
1698 tb_to_ns(tb.tb_min),
1699 tb_to_ns(tb.tb_max));
1700 s += strlen(s);
1701 }
1702 p->buflen = s - p->buf;
1703 }
1704
1705 pos = *ppos;
1706 if (pos >= p->buflen)
1707 return 0;
1708 if (len > p->buflen - pos)
1709 len = p->buflen - pos;
1710 n = copy_to_user(buf, p->buf + pos, len);
1711 if (n) {
1712 if (n == len)
1713 return -EFAULT;
1714 len -= n;
1715 }
1716 *ppos = pos + len;
1717 return len;
1718}
1719
1720static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1721 size_t len, loff_t *ppos)
1722{
1723 return -EACCES;
1724}
1725
1726static const struct file_operations debugfs_timings_ops = {
1727 .owner = THIS_MODULE,
1728 .open = debugfs_timings_open,
1729 .release = debugfs_timings_release,
1730 .read = debugfs_timings_read,
1731 .write = debugfs_timings_write,
1732 .llseek = generic_file_llseek,
1733};
1734
1735/* Create a debugfs directory for the vcpu */
1736static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1737{
1738 char buf[16];
1739 struct kvm *kvm = vcpu->kvm;
1740
1741 snprintf(buf, sizeof(buf), "vcpu%u", id);
1742 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1743 return;
1744 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1745 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1746 return;
1747 vcpu->arch.debugfs_timings =
1748 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1749 vcpu, &debugfs_timings_ops);
1750}
1751
1752#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1753static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1754{
1755}
1756#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1757
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301758static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1759 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001760{
1761 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001762 int err = -EINVAL;
1763 int core;
1764 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001765
Paul Mackerras45c940b2016-11-18 17:43:30 +11001766 core = id / threads_per_vcore();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001767 if (core >= KVM_MAX_VCORES)
1768 goto out;
1769
1770 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001771 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001772 if (!vcpu)
1773 goto out;
1774
1775 err = kvm_vcpu_init(vcpu, kvm, id);
1776 if (err)
1777 goto free_vcpu;
1778
1779 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001780#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1781 /*
1782 * The shared struct is never shared on HV,
1783 * so we can always use host endianness
1784 */
1785#ifdef __BIG_ENDIAN__
1786 vcpu->arch.shared_big_endian = true;
1787#else
1788 vcpu->arch.shared_big_endian = false;
1789#endif
1790#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001791 vcpu->arch.mmcr[0] = MMCR0_FC;
1792 vcpu->arch.ctrl = CTRL_RUNLATCH;
1793 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301794 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001795 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001796 spin_lock_init(&vcpu->arch.tbacct_lock);
1797 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001798 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001799
Paul Mackerrasde56a942011-06-29 00:21:34 +00001800 kvmppc_mmu_book3s_hv_init(vcpu);
1801
Paul Mackerras8455d792012-10-15 01:17:42 +00001802 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001803
1804 init_waitqueue_head(&vcpu->arch.cpu_run);
1805
1806 mutex_lock(&kvm->lock);
1807 vcore = kvm->arch.vcores[core];
1808 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001809 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001810 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001811 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001812 }
1813 mutex_unlock(&kvm->lock);
1814
1815 if (!vcore)
1816 goto free_vcpu;
1817
1818 spin_lock(&vcore->lock);
1819 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001820 spin_unlock(&vcore->lock);
1821 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001822 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001823 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001824
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001825 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1826 kvmppc_sanity_check(vcpu);
1827
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001828 debugfs_vcpu_init(vcpu, id);
1829
Paul Mackerrasde56a942011-06-29 00:21:34 +00001830 return vcpu;
1831
1832free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001833 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001834out:
1835 return ERR_PTR(err);
1836}
1837
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001838static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1839{
1840 if (vpa->pinned_addr)
1841 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1842 vpa->dirty);
1843}
1844
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301845static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001846{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001847 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001848 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1849 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1850 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001851 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001852 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001853 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001854}
1855
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301856static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1857{
1858 /* Indicate we want to get back into the guest */
1859 return 1;
1860}
1861
Paul Mackerras19ccb762011-07-23 17:42:46 +10001862static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001863{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001864 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001865
Paul Mackerras19ccb762011-07-23 17:42:46 +10001866 now = get_tb();
1867 if (now > vcpu->arch.dec_expires) {
1868 /* decrementer has already gone negative */
1869 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001870 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001871 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001872 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001873 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1874 / tb_ticks_per_sec;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001875 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001876 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001877}
1878
Paul Mackerras19ccb762011-07-23 17:42:46 +10001879static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001880{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001881 vcpu->arch.ceded = 0;
1882 if (vcpu->arch.timer_running) {
1883 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1884 vcpu->arch.timer_running = 0;
1885 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001886}
1887
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001888extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001889
1890static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1891 struct kvm_vcpu *vcpu)
1892{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001893 u64 now;
1894
Paul Mackerras371fefd2011-06-29 00:23:08 +00001895 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1896 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001897 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001898 now = mftb();
1899 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1900 vcpu->arch.stolen_logged;
1901 vcpu->arch.busy_preempt = now;
1902 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001903 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001904 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10001905 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001906}
1907
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001908static int kvmppc_grab_hwthread(int cpu)
1909{
1910 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001911 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001912
1913 tpaca = &paca[cpu];
1914
1915 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001916 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001917 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001918 tpaca->kvm_hstate.napping = 0;
1919 smp_wmb();
1920 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001921
1922 /*
1923 * If the thread is already executing in the kernel (e.g. handling
1924 * a stray interrupt), wait for it to get back to nap mode.
1925 * The smp_mb() is to ensure that our setting of hwthread_req
1926 * is visible before we look at hwthread_state, so if this
1927 * races with the code at system_reset_pSeries and the thread
1928 * misses our setting of hwthread_req, we are sure to see its
1929 * setting of hwthread_state, and vice versa.
1930 */
1931 smp_mb();
1932 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1933 if (--timeout <= 0) {
1934 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1935 return -EBUSY;
1936 }
1937 udelay(1);
1938 }
1939 return 0;
1940}
1941
1942static void kvmppc_release_hwthread(int cpu)
1943{
1944 struct paca_struct *tpaca;
1945
1946 tpaca = &paca[cpu];
1947 tpaca->kvm_hstate.hwthread_req = 0;
1948 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001949 tpaca->kvm_hstate.kvm_vcore = NULL;
1950 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001951}
1952
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001953static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001954{
1955 int cpu;
1956 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001957 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001958
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001959 cpu = vc->pcpu;
1960 if (vcpu) {
1961 if (vcpu->arch.timer_running) {
1962 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1963 vcpu->arch.timer_running = 0;
1964 }
1965 cpu += vcpu->arch.ptid;
1966 vcpu->cpu = mvc->pcpu;
1967 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001968 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001969 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001970 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001971 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001972 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001973 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001974 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001975 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001976 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001977}
1978
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001979static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001980{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001981 int cpu = smp_processor_id();
1982 int i, loops;
Paul Mackerras45c940b2016-11-18 17:43:30 +11001983 int n_threads = threads_per_vcore();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001984
Paul Mackerras45c940b2016-11-18 17:43:30 +11001985 if (n_threads <= 1)
1986 return;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001987 for (loops = 0; loops < 1000000; ++loops) {
1988 /*
1989 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001990 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001991 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001992 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001993 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11001994 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001995 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001996 break;
Paul Mackerras45c940b2016-11-18 17:43:30 +11001997 if (i == n_threads) {
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001998 HMT_medium();
1999 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002000 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002001 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00002002 }
2003 HMT_medium();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002004 for (i = 1; i < n_threads; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002005 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002006 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002007}
2008
2009/*
2010 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00002011 * this core are off-line. Then grab the threads so they can't
2012 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00002013 */
2014static int on_primary_thread(void)
2015{
2016 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10002017 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002018
Michael Ellerman3102f782014-05-23 18:15:29 +10002019 /* Are we on a primary subcore? */
2020 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00002021 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10002022
2023 thr = 0;
2024 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002025 if (cpu_online(cpu + thr))
2026 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00002027
2028 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10002029 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002030 if (kvmppc_grab_hwthread(cpu + thr)) {
2031 /* Couldn't grab one; let the others go */
2032 do {
2033 kvmppc_release_hwthread(cpu + thr);
2034 } while (--thr > 0);
2035 return 0;
2036 }
2037 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002038 return 1;
2039}
2040
Paul Mackerrasec257162015-06-24 21:18:03 +10002041/*
2042 * A list of virtual cores for each physical CPU.
2043 * These are vcores that could run but their runner VCPU tasks are
2044 * (or may be) preempted.
2045 */
2046struct preempted_vcore_list {
2047 struct list_head list;
2048 spinlock_t lock;
2049};
2050
2051static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
2052
2053static void init_vcore_lists(void)
2054{
2055 int cpu;
2056
2057 for_each_possible_cpu(cpu) {
2058 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
2059 spin_lock_init(&lp->lock);
2060 INIT_LIST_HEAD(&lp->list);
2061 }
2062}
2063
2064static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
2065{
2066 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2067
2068 vc->vcore_state = VCORE_PREEMPT;
2069 vc->pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002070 if (vc->num_threads < threads_per_vcore()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002071 spin_lock(&lp->lock);
2072 list_add_tail(&vc->preempt_list, &lp->list);
2073 spin_unlock(&lp->lock);
2074 }
2075
2076 /* Start accumulating stolen time */
2077 kvmppc_core_start_stolen(vc);
2078}
2079
2080static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
2081{
Paul Mackerras402813f2015-07-16 17:11:13 +10002082 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10002083
2084 kvmppc_core_end_stolen(vc);
2085 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10002086 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002087 spin_lock(&lp->lock);
2088 list_del_init(&vc->preempt_list);
2089 spin_unlock(&lp->lock);
2090 }
2091 vc->vcore_state = VCORE_INACTIVE;
2092}
2093
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002094/*
2095 * This stores information about the virtual cores currently
2096 * assigned to a physical core.
2097 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002098struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002099 int n_subcores;
2100 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002101 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002102 int subcore_threads[MAX_SUBCORES];
2103 struct kvm *subcore_vm[MAX_SUBCORES];
2104 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002105};
2106
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002107/*
2108 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
2109 * respectively in 2-way micro-threading (split-core) mode.
2110 */
2111static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2112
Paul Mackerrasec257162015-06-24 21:18:03 +10002113static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2114{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002115 int sub;
2116
Paul Mackerrasec257162015-06-24 21:18:03 +10002117 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002118 cip->n_subcores = 1;
2119 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002120 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002121 cip->subcore_threads[0] = vc->num_threads;
2122 cip->subcore_vm[0] = vc->kvm;
2123 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2124 INIT_LIST_HEAD(&cip->vcs[sub]);
2125 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2126}
2127
2128static bool subcore_config_ok(int n_subcores, int n_threads)
2129{
2130 /* Can only dynamically split if unsplit to begin with */
2131 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2132 return false;
2133 if (n_subcores > MAX_SUBCORES)
2134 return false;
2135 if (n_subcores > 1) {
2136 if (!(dynamic_mt_modes & 2))
2137 n_subcores = 4;
2138 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2139 return false;
2140 }
2141
2142 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002143}
2144
2145static void init_master_vcore(struct kvmppc_vcore *vc)
2146{
2147 vc->master_vcore = vc;
2148 vc->entry_exit_map = 0;
2149 vc->in_guest = 0;
2150 vc->napping_threads = 0;
2151 vc->conferring_threads = 0;
2152}
2153
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002154static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2155{
2156 int n_threads = vc->num_threads;
2157 int sub;
2158
2159 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2160 return false;
2161
2162 if (n_threads < cip->max_subcore_threads)
2163 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002164 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002165 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10002166 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002167
2168 sub = cip->n_subcores;
2169 ++cip->n_subcores;
2170 cip->total_threads += vc->num_threads;
2171 cip->subcore_threads[sub] = vc->num_threads;
2172 cip->subcore_vm[sub] = vc->kvm;
2173 init_master_vcore(vc);
Wei Yongjun28d057c2016-10-17 15:15:50 +00002174 list_move_tail(&vc->preempt_list, &cip->vcs[sub]);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002175
2176 return true;
2177}
2178
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002179/*
2180 * Work out whether it is possible to piggyback the execution of
2181 * vcore *pvc onto the execution of the other vcores described in *cip.
2182 */
2183static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2184 int target_threads)
2185{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002186 if (cip->total_threads + pvc->num_threads > target_threads)
2187 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002188
Paul Mackerrasb0090312016-09-15 16:27:41 +10002189 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002190}
2191
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002192static void prepare_threads(struct kvmppc_vcore *vc)
2193{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002194 int i;
2195 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002196
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002197 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002198 if (signal_pending(vcpu->arch.run_task))
2199 vcpu->arch.ret = -EINTR;
2200 else if (vcpu->arch.vpa.update_pending ||
2201 vcpu->arch.slb_shadow.update_pending ||
2202 vcpu->arch.dtl.update_pending)
2203 vcpu->arch.ret = RESUME_GUEST;
2204 else
2205 continue;
2206 kvmppc_remove_runnable(vc, vcpu);
2207 wake_up(&vcpu->arch.cpu_run);
2208 }
2209}
2210
Paul Mackerrasec257162015-06-24 21:18:03 +10002211static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002212{
Paul Mackerrasec257162015-06-24 21:18:03 +10002213 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2214 struct kvmppc_vcore *pvc, *vcnext;
2215
2216 spin_lock(&lp->lock);
2217 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2218 if (!spin_trylock(&pvc->lock))
2219 continue;
2220 prepare_threads(pvc);
2221 if (!pvc->n_runnable) {
2222 list_del_init(&pvc->preempt_list);
2223 if (pvc->runner == NULL) {
2224 pvc->vcore_state = VCORE_INACTIVE;
2225 kvmppc_core_end_stolen(pvc);
2226 }
2227 spin_unlock(&pvc->lock);
2228 continue;
2229 }
2230 if (!can_piggyback(pvc, cip, target_threads)) {
2231 spin_unlock(&pvc->lock);
2232 continue;
2233 }
2234 kvmppc_core_end_stolen(pvc);
2235 pvc->vcore_state = VCORE_PIGGYBACK;
2236 if (cip->total_threads >= target_threads)
2237 break;
2238 }
2239 spin_unlock(&lp->lock);
2240}
2241
2242static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2243{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002244 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002245 u64 now;
2246 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002247 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002248
Paul Mackerrasec257162015-06-24 21:18:03 +10002249 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002250 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002251 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002252 /* cancel pending dec exception if dec is positive */
2253 if (now < vcpu->arch.dec_expires &&
2254 kvmppc_core_pending_dec(vcpu))
2255 kvmppc_core_dequeue_dec(vcpu);
2256
2257 trace_kvm_guest_exit(vcpu);
2258
2259 ret = RESUME_GUEST;
2260 if (vcpu->arch.trap)
2261 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2262 vcpu->arch.run_task);
2263
2264 vcpu->arch.ret = ret;
2265 vcpu->arch.trap = 0;
2266
Paul Mackerrasec257162015-06-24 21:18:03 +10002267 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2268 if (vcpu->arch.pending_exceptions)
2269 kvmppc_core_prepare_to_enter(vcpu);
2270 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002271 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002272 else
2273 ++still_running;
2274 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002275 kvmppc_remove_runnable(vc, vcpu);
2276 wake_up(&vcpu->arch.cpu_run);
2277 }
2278 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002279 list_del_init(&vc->preempt_list);
2280 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002281 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002282 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002283 } else if (vc->runner) {
2284 vc->vcore_state = VCORE_PREEMPT;
2285 kvmppc_core_start_stolen(vc);
2286 } else {
2287 vc->vcore_state = VCORE_INACTIVE;
2288 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002289 if (vc->n_runnable > 0 && vc->runner == NULL) {
2290 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002291 i = -1;
2292 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10002293 wake_up(&vcpu->arch.cpu_run);
2294 }
2295 }
2296 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002297}
2298
Paul Mackerras371fefd2011-06-29 00:23:08 +00002299/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002300 * Clear core from the list of active host cores as we are about to
2301 * enter the guest. Only do this if it is the primary thread of the
2302 * core (not if a subcore) that is entering the guest.
2303 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002304static inline int kvmppc_clear_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002305{
2306 int core;
2307
2308 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002309 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002310 /*
2311 * Memory barrier can be omitted here as we will do a smp_wmb()
2312 * later in kvmppc_start_thread and we need ensure that state is
2313 * visible to other CPUs only after we enter guest.
2314 */
2315 core = cpu >> threads_shift;
2316 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002317 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002318}
2319
2320/*
2321 * Advertise this core as an active host core since we exited the guest
2322 * Only need to do this if it is the primary thread of the core that is
2323 * exiting.
2324 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002325static inline int kvmppc_set_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002326{
2327 int core;
2328
2329 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002330 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002331
2332 /*
2333 * Memory barrier can be omitted here because we do a spin_unlock
2334 * immediately after this which provides the memory barrier.
2335 */
2336 core = cpu >> threads_shift;
2337 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01002338 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002339}
2340
2341/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002342 * Run a set of guest threads on a physical core.
2343 * Called with vc->lock held.
2344 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002345static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002346{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002347 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002348 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002349 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002350 struct core_info core_info;
2351 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002352 struct kvm_split_mode split_info, *sip;
2353 int split, subcore_size, active;
2354 int sub;
2355 bool thr0_done;
2356 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002357 int pcpu, thr;
2358 int target_threads;
Paul Mackerras45c940b2016-11-18 17:43:30 +11002359 int controlled_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002360
2361 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002362 * Remove from the list any threads that have a signal pending
2363 * or need a VPA update done
2364 */
2365 prepare_threads(vc);
2366
2367 /* if the runner is no longer runnable, let the caller pick a new one */
2368 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2369 return;
2370
2371 /*
2372 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002373 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002374 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002375 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002376
2377 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11002378 * Number of threads that we will be controlling: the same as
2379 * the number of threads per subcore, except on POWER9,
2380 * where it's 1 because the threads are (mostly) independent.
2381 */
2382 controlled_threads = threads_per_vcore();
2383
2384 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002385 * Make sure we are running on primary threads, and that secondary
2386 * threads are offline. Also check if the number of threads in this
2387 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002388 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002389 if ((controlled_threads > 1) &&
Michael Ellerman3102f782014-05-23 18:15:29 +10002390 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002391 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002392 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002393 kvmppc_remove_runnable(vc, vcpu);
2394 wake_up(&vcpu->arch.cpu_run);
2395 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002396 goto out;
2397 }
2398
Paul Mackerrasec257162015-06-24 21:18:03 +10002399 /*
2400 * See if we could run any other vcores on the physical core
2401 * along with this one.
2402 */
2403 init_core_info(&core_info, vc);
2404 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002405 target_threads = controlled_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002406 if (target_smt_mode && target_smt_mode < target_threads)
2407 target_threads = target_smt_mode;
2408 if (vc->num_threads < target_threads)
2409 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002410
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002411 /* Decide on micro-threading (split-core) mode */
2412 subcore_size = threads_per_subcore;
2413 cmd_bit = stat_bit = 0;
2414 split = core_info.n_subcores;
2415 sip = NULL;
2416 if (split > 1) {
2417 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2418 if (split == 2 && (dynamic_mt_modes & 2)) {
2419 cmd_bit = HID0_POWER8_1TO2LPAR;
2420 stat_bit = HID0_POWER8_2LPARMODE;
2421 } else {
2422 split = 4;
2423 cmd_bit = HID0_POWER8_1TO4LPAR;
2424 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002425 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002426 subcore_size = MAX_SMT_THREADS / split;
2427 sip = &split_info;
2428 memset(&split_info, 0, sizeof(split_info));
2429 split_info.rpr = mfspr(SPRN_RPR);
2430 split_info.pmmar = mfspr(SPRN_PMMAR);
2431 split_info.ldbar = mfspr(SPRN_LDBAR);
2432 split_info.subcore_size = subcore_size;
2433 for (sub = 0; sub < core_info.n_subcores; ++sub)
2434 split_info.master_vcs[sub] =
2435 list_first_entry(&core_info.vcs[sub],
2436 struct kvmppc_vcore, preempt_list);
2437 /* order writes to split_info before kvm_split_mode pointer */
2438 smp_wmb();
2439 }
2440 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11002441 for (thr = 0; thr < controlled_threads; ++thr)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002442 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2443
2444 /* Initiate micro-threading (split-core) if required */
2445 if (cmd_bit) {
2446 unsigned long hid0 = mfspr(SPRN_HID0);
2447
2448 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2449 mb();
2450 mtspr(SPRN_HID0, hid0);
2451 isync();
2452 for (;;) {
2453 hid0 = mfspr(SPRN_HID0);
2454 if (hid0 & stat_bit)
2455 break;
2456 cpu_relax();
2457 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002458 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002459
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002460 kvmppc_clear_host_core(pcpu);
2461
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002462 /* Start all the threads */
2463 active = 0;
2464 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2465 thr = subcore_thread_map[sub];
2466 thr0_done = false;
2467 active |= 1 << thr;
2468 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2469 pvc->pcpu = pcpu + thr;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002470 for_each_runnable_thread(i, vcpu, pvc) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002471 kvmppc_start_thread(vcpu, pvc);
2472 kvmppc_create_dtl_entry(vcpu, pvc);
2473 trace_kvm_guest_enter(vcpu);
2474 if (!vcpu->arch.ptid)
2475 thr0_done = true;
2476 active |= 1 << (thr + vcpu->arch.ptid);
2477 }
2478 /*
2479 * We need to start the first thread of each subcore
2480 * even if it doesn't have a vcpu.
2481 */
2482 if (pvc->master_vcore == pvc && !thr0_done)
2483 kvmppc_start_thread(NULL, pvc);
2484 thr += pvc->num_threads;
2485 }
2486 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302487
2488 /*
2489 * Ensure that split_info.do_nap is set after setting
2490 * the vcore pointer in the PACA of the secondaries.
2491 */
2492 smp_mb();
2493 if (cmd_bit)
2494 split_info.do_nap = 1; /* ask secondaries to nap when done */
2495
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002496 /*
2497 * When doing micro-threading, poke the inactive threads as well.
2498 * This gets them to the nap instruction after kvm_do_nap,
2499 * which reduces the time taken to unsplit later.
2500 */
2501 if (split > 1)
2502 for (thr = 1; thr < threads_per_subcore; ++thr)
2503 if (!(active & (1 << thr)))
2504 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002505
Paul Mackerras2f12f032012-10-15 01:17:17 +00002506 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002507 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002508
2509 trace_kvmppc_run_core(vc, 0);
2510
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002511 for (sub = 0; sub < core_info.n_subcores; ++sub)
2512 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2513 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002514
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002515 guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002516
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002517 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002518
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002519 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002520
Paul Mackerrasec257162015-06-24 21:18:03 +10002521 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2522
2523 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002524 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002525 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002526
Paul Mackerras371fefd2011-06-29 00:23:08 +00002527 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002528 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002529
2530 /* Return to whole-core mode if we split the core earlier */
2531 if (split > 1) {
2532 unsigned long hid0 = mfspr(SPRN_HID0);
2533 unsigned long loops = 0;
2534
2535 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2536 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2537 mb();
2538 mtspr(SPRN_HID0, hid0);
2539 isync();
2540 for (;;) {
2541 hid0 = mfspr(SPRN_HID0);
2542 if (!(hid0 & stat_bit))
2543 break;
2544 cpu_relax();
2545 ++loops;
2546 }
2547 split_info.do_nap = 0;
2548 }
2549
2550 /* Let secondaries go back to the offline loop */
Paul Mackerras45c940b2016-11-18 17:43:30 +11002551 for (i = 0; i < controlled_threads; ++i) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002552 kvmppc_release_hwthread(pcpu + i);
2553 if (sip && sip->napped[i])
2554 kvmppc_ipi_thread(pcpu + i);
2555 }
2556
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002557 kvmppc_set_host_core(pcpu);
2558
Paul Mackerras371fefd2011-06-29 00:23:08 +00002559 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002560
Paul Mackerras371fefd2011-06-29 00:23:08 +00002561 /* make sure updates to secondary vcpu structs are visible now */
2562 smp_mb();
Paolo Bonzini6edaa532016-06-15 15:18:26 +02002563 guest_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002564
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002565 for (sub = 0; sub < core_info.n_subcores; ++sub)
2566 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2567 preempt_list)
2568 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002569
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002570 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002571 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002572
Paul Mackerrasde56a942011-06-29 00:21:34 +00002573 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002574 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002575 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002576}
2577
Paul Mackerras19ccb762011-07-23 17:42:46 +10002578/*
2579 * Wait for some other vcpu thread to execute us, and
2580 * wake us up when we need to handle something in the host.
2581 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002582static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2583 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002584{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002585 DEFINE_WAIT(wait);
2586
Paul Mackerras19ccb762011-07-23 17:42:46 +10002587 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002588 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2589 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002590 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002591 spin_lock(&vc->lock);
2592 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002593 finish_wait(&vcpu->arch.cpu_run, &wait);
2594}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002595
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002596static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
2597{
2598 /* 10us base */
2599 if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
2600 vc->halt_poll_ns = 10000;
2601 else
2602 vc->halt_poll_ns *= halt_poll_ns_grow;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002603}
2604
2605static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
2606{
2607 if (halt_poll_ns_shrink == 0)
2608 vc->halt_poll_ns = 0;
2609 else
2610 vc->halt_poll_ns /= halt_poll_ns_shrink;
2611}
2612
Suraj Jitindar Singh908a0932016-10-14 11:53:23 +11002613/*
2614 * Check to see if any of the runnable vcpus on the vcore have pending
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002615 * exceptions or are no longer ceded
2616 */
2617static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
2618{
2619 struct kvm_vcpu *vcpu;
2620 int i;
2621
2622 for_each_runnable_thread(i, vcpu, vc) {
2623 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded)
2624 return 1;
2625 }
2626
2627 return 0;
2628}
2629
Paul Mackerras19ccb762011-07-23 17:42:46 +10002630/*
2631 * All the vcpus in this vcore are idle, so wait for a decrementer
2632 * or external interrupt to one of the vcpus. vc->lock is held.
2633 */
2634static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2635{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002636 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002637 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002638 u64 block_ns;
Marcelo Tosatti85773702016-02-19 09:46:39 +01002639 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002640
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002641 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002642 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002643 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002644 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
2645 ++vc->runner->stat.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002646
2647 vc->vcore_state = VCORE_POLLING;
2648 spin_unlock(&vc->lock);
2649
2650 do {
2651 if (kvmppc_vcore_check_block(vc)) {
2652 do_sleep = 0;
2653 break;
2654 }
2655 cur = ktime_get();
2656 } while (single_task_running() && ktime_before(cur, stop));
2657
2658 spin_lock(&vc->lock);
2659 vc->vcore_state = VCORE_INACTIVE;
2660
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002661 if (!do_sleep) {
2662 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002663 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002664 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002665 }
2666
Marcelo Tosatti85773702016-02-19 09:46:39 +01002667 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002668
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002669 if (kvmppc_vcore_check_block(vc)) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002670 finish_swait(&vc->wq, &wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002671 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002672 /* If we polled, count this as a successful poll */
2673 if (vc->halt_poll_ns)
2674 ++vc->runner->stat.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002675 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002676 }
2677
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002678 start_wait = ktime_get();
2679
Paul Mackerras19ccb762011-07-23 17:42:46 +10002680 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002681 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002682 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002683 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01002684 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002685 spin_lock(&vc->lock);
2686 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002687 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002688 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002689
2690 cur = ktime_get();
2691
2692out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10002693 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
2694
2695 /* Attribute wait time */
2696 if (do_sleep) {
2697 vc->runner->stat.halt_wait_ns +=
2698 ktime_to_ns(cur) - ktime_to_ns(start_wait);
2699 /* Attribute failed poll time */
2700 if (vc->halt_poll_ns)
2701 vc->runner->stat.halt_poll_fail_ns +=
2702 ktime_to_ns(start_wait) -
2703 ktime_to_ns(start_poll);
2704 } else {
2705 /* Attribute successful poll time */
2706 if (vc->halt_poll_ns)
2707 vc->runner->stat.halt_poll_success_ns +=
2708 ktime_to_ns(cur) -
2709 ktime_to_ns(start_poll);
2710 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002711
2712 /* Adjust poll time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002713 if (halt_poll_ns) {
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002714 if (block_ns <= vc->halt_poll_ns)
2715 ;
2716 /* We slept and blocked for longer than the max halt time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002717 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002718 shrink_halt_poll_ns(vc);
2719 /* We slept and our poll time is too small */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11002720 else if (vc->halt_poll_ns < halt_poll_ns &&
2721 block_ns < halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002722 grow_halt_poll_ns(vc);
Suraj Jitindar Singhe03f3922016-10-14 11:53:21 +11002723 if (vc->halt_poll_ns > halt_poll_ns)
2724 vc->halt_poll_ns = halt_poll_ns;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10002725 } else
2726 vc->halt_poll_ns = 0;
2727
2728 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002729}
2730
2731static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2732{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002733 int n_ceded, i;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002734 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002735 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002736
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002737 trace_kvmppc_run_vcpu_enter(vcpu);
2738
Paul Mackerras371fefd2011-06-29 00:23:08 +00002739 kvm_run->exit_reason = 0;
2740 vcpu->arch.ret = RESUME_GUEST;
2741 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002742 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002743
Paul Mackerras371fefd2011-06-29 00:23:08 +00002744 /*
2745 * Synchronize with other threads in this virtual core
2746 */
2747 vc = vcpu->arch.vcore;
2748 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002749 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002750 vcpu->arch.run_task = current;
2751 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002752 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002753 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002754 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002755 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002756 ++vc->n_runnable;
2757
Paul Mackerras19ccb762011-07-23 17:42:46 +10002758 /*
2759 * This happens the first time this is called for a vcpu.
2760 * If the vcore is already running, we may be able to start
2761 * this thread straight away and have it join in.
2762 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002763 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002764 if (vc->vcore_state == VCORE_PIGGYBACK) {
2765 struct kvmppc_vcore *mvc = vc->master_vcore;
2766 if (spin_trylock(&mvc->lock)) {
2767 if (mvc->vcore_state == VCORE_RUNNING &&
2768 !VCORE_IS_EXITING(mvc)) {
2769 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002770 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002771 trace_kvm_guest_enter(vcpu);
2772 }
2773 spin_unlock(&mvc->lock);
2774 }
2775 } else if (vc->vcore_state == VCORE_RUNNING &&
2776 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002777 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002778 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002779 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002780 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002781 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002782 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002783
Paul Mackerras8455d792012-10-15 01:17:42 +00002784 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002785
2786 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2787 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002788 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2789 kvmppc_vcore_end_preempt(vc);
2790
Paul Mackerras8455d792012-10-15 01:17:42 +00002791 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002792 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002793 continue;
2794 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002795 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002796 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002797 if (signal_pending(v->arch.run_task)) {
2798 kvmppc_remove_runnable(vc, v);
2799 v->stat.signal_exits++;
2800 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2801 v->arch.ret = -EINTR;
2802 wake_up(&v->arch.cpu_run);
2803 }
2804 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002805 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2806 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002807 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002808 for_each_runnable_thread(i, v, vc) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002809 if (!v->arch.pending_exceptions)
2810 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002811 else
2812 v->arch.ceded = 0;
2813 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002814 vc->runner = vcpu;
2815 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002816 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002817 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002818 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002819 /* Let something else run */
2820 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002821 if (vc->vcore_state == VCORE_PREEMPT)
2822 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002823 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002824 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002825 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002826 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002827 }
2828
Paul Mackerras8455d792012-10-15 01:17:42 +00002829 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2830 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002831 vc->vcore_state == VCORE_EXITING ||
2832 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002833 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002834
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002835 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2836 kvmppc_vcore_end_preempt(vc);
2837
Paul Mackerras8455d792012-10-15 01:17:42 +00002838 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2839 kvmppc_remove_runnable(vc, vcpu);
2840 vcpu->stat.signal_exits++;
2841 kvm_run->exit_reason = KVM_EXIT_INTR;
2842 vcpu->arch.ret = -EINTR;
2843 }
2844
2845 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2846 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10002847 i = -1;
2848 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00002849 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002850 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002851
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002852 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002853 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002854 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002855}
2856
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302857static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002858{
2859 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002860 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002861
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002862 if (!vcpu->arch.sane) {
2863 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2864 return -EINVAL;
2865 }
2866
Scott Wood25051b5a2011-11-08 18:23:23 -06002867 kvmppc_core_prepare_to_enter(vcpu);
2868
Paul Mackerras19ccb762011-07-23 17:42:46 +10002869 /* No need to go into the guest when all we'll do is come back out */
2870 if (signal_pending(current)) {
2871 run->exit_reason = KVM_EXIT_INTR;
2872 return -EINTR;
2873 }
2874
Paul Mackerras32fad282012-05-04 02:32:53 +00002875 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002876 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002877 smp_mb();
2878
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002879 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002880 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002881 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002882 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002883 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002884 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002885
Anton Blanchard579e6332015-10-29 11:44:09 +11002886 flush_all_to_thread(current);
2887
Paul Mackerras19ccb762011-07-23 17:42:46 +10002888 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002889 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002890 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002891
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002892 do {
2893 r = kvmppc_run_vcpu(run, vcpu);
2894
2895 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2896 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002897 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002898 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002899 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002900 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002901 } else if (r == RESUME_PAGE_FAULT) {
2902 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2903 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2904 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2905 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Suresh Warrierf7af5202016-08-19 15:35:52 +10002906 } else if (r == RESUME_PASSTHROUGH)
2907 r = kvmppc_xics_rm_complete(vcpu, 0);
Greg Kurze59d24e2014-02-06 17:36:56 +01002908 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002909
2910 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002911 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002912 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002913 return r;
2914}
2915
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002916static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2917 int linux_psize)
2918{
2919 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2920
2921 if (!def->shift)
2922 return;
2923 (*sps)->page_shift = def->shift;
2924 (*sps)->slb_enc = def->sllp;
2925 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002926 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302927 /*
2928 * Add 16MB MPSS support if host supports it
2929 */
2930 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2931 (*sps)->enc[1].page_shift = 24;
2932 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2933 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002934 (*sps)++;
2935}
2936
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302937static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2938 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002939{
2940 struct kvm_ppc_one_seg_page_size *sps;
2941
2942 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2943 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2944 info->flags |= KVM_PPC_1T_SEGMENTS;
2945 info->slb_size = mmu_slb_size;
2946
2947 /* We only support these sizes for now, and no muti-size segments */
2948 sps = &info->sps[0];
2949 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2950 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2951 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2952
2953 return 0;
2954}
2955
Paul Mackerras82ed3612011-12-15 02:03:22 +00002956/*
2957 * Get (and clear) the dirty memory log for a memory slot.
2958 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302959static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2960 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002961{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002962 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00002963 struct kvm_memory_slot *memslot;
2964 int r;
2965 unsigned long n;
2966
2967 mutex_lock(&kvm->slots_lock);
2968
2969 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002970 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002971 goto out;
2972
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002973 slots = kvm_memslots(kvm);
2974 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002975 r = -ENOENT;
2976 if (!memslot->dirty_bitmap)
2977 goto out;
2978
2979 n = kvm_dirty_bitmap_bytes(memslot);
2980 memset(memslot->dirty_bitmap, 0, n);
2981
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002982 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002983 if (r)
2984 goto out;
2985
2986 r = -EFAULT;
2987 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2988 goto out;
2989
2990 r = 0;
2991out:
2992 mutex_unlock(&kvm->slots_lock);
2993 return r;
2994}
2995
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302996static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2997 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002998{
2999 if (!dont || free->arch.rmap != dont->arch.rmap) {
3000 vfree(free->arch.rmap);
3001 free->arch.rmap = NULL;
3002 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003003}
3004
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303005static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
3006 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003007{
3008 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
3009 if (!slot->arch.rmap)
3010 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003011
3012 return 0;
3013}
3014
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303015static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
3016 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003017 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003018{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00003019 return 0;
3020}
3021
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303022static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02003023 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02003024 const struct kvm_memory_slot *old,
3025 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003026{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003027 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003028 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003029 struct kvm_memory_slot *memslot;
3030
Yongji Xiea56ee9f2016-11-04 13:55:12 +08003031 /*
3032 * If we are making a new memslot, it might make
3033 * some address that was previously cached as emulated
3034 * MMIO be no longer emulated MMIO, so invalidate
3035 * all the caches of emulated MMIO translations.
3036 */
3037 if (npages)
3038 atomic64_inc(&kvm->arch.mmio_update);
3039
Takuya Yoshikawa84826442013-02-27 19:45:25 +09003040 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003041 /*
3042 * If modifying a memslot, reset all the rmap dirty bits.
3043 * If this is a new memslot, we don't need to do anything
3044 * since the rmap array starts out as all zeroes,
3045 * i.e. no pages are dirty.
3046 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02003047 slots = kvm_memslots(kvm);
3048 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00003049 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
3050 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003051}
3052
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003053/*
3054 * Update LPCR values in kvm->arch and in vcores.
3055 * Caller must hold kvm->lock.
3056 */
3057void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
3058{
3059 long int i;
3060 u32 cores_done = 0;
3061
3062 if ((kvm->arch.lpcr & mask) == lpcr)
3063 return;
3064
3065 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
3066
3067 for (i = 0; i < KVM_MAX_VCORES; ++i) {
3068 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
3069 if (!vc)
3070 continue;
3071 spin_lock(&vc->lock);
3072 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
3073 spin_unlock(&vc->lock);
3074 if (++cores_done >= kvm->arch.online_vcores)
3075 break;
3076 }
3077}
3078
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303079static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
3080{
3081 return;
3082}
3083
Paul Mackerras7a840842016-11-16 22:25:20 +11003084static void kvmppc_setup_partition_table(struct kvm *kvm)
3085{
3086 unsigned long dw0, dw1;
3087
3088 /* PS field - page size for VRMA */
3089 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
3090 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
3091 /* HTABSIZE and HTABORG fields */
3092 dw0 |= kvm->arch.sdr1;
3093
3094 /* Second dword has GR=0; other fields are unused since UPRT=0 */
3095 dw1 = 0;
3096
3097 mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
3098}
3099
Paul Mackerras32fad282012-05-04 02:32:53 +00003100static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003101{
3102 int err = 0;
3103 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003104 unsigned long hva;
3105 struct kvm_memory_slot *memslot;
3106 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003107 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003108 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003109 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003110
3111 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003112 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003113 goto out; /* another vcpu beat us to it */
3114
Paul Mackerras32fad282012-05-04 02:32:53 +00003115 /* Allocate hashed page table (if not done already) and reset it */
3116 if (!kvm->arch.hpt_virt) {
3117 err = kvmppc_alloc_hpt(kvm, NULL);
3118 if (err) {
3119 pr_err("KVM: Couldn't alloc HPT\n");
3120 goto out;
3121 }
3122 }
3123
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003124 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003125 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003126 memslot = gfn_to_memslot(kvm, 0);
3127
3128 /* We must have some memory at 0 by now */
3129 err = -EINVAL;
3130 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003131 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003132
3133 /* Look up the VMA for the start of this memory slot */
3134 hva = memslot->userspace_addr;
3135 down_read(&current->mm->mmap_sem);
3136 vma = find_vma(current->mm, hva);
3137 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3138 goto up_out;
3139
3140 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003141 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003142
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003143 up_read(&current->mm->mmap_sem);
3144
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003145 /* We can handle 4k, 64k or 16M pages in the VRMA */
3146 err = -EINVAL;
3147 if (!(psize == 0x1000 || psize == 0x10000 ||
3148 psize == 0x1000000))
3149 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003150
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003151 senc = slb_pgsize_encoding(psize);
3152 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3153 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003154 /* Create HPTEs in the hash page table for the VRMA */
3155 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003156
Paul Mackerras7a840842016-11-16 22:25:20 +11003157 /* Update VRMASD field in the LPCR */
3158 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
3159 /* the -4 is to account for senc values starting at 0x10 */
3160 lpcr = senc << (LPCR_VRMASD_SH - 4);
3161 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
3162 } else {
3163 kvmppc_setup_partition_table(kvm);
3164 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003165
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003166 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003167 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003168 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003169 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003170 out_srcu:
3171 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003172 out:
3173 mutex_unlock(&kvm->lock);
3174 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003175
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003176 up_out:
3177 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003178 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003179}
3180
Suresh Warrier79b6c242015-12-17 14:59:06 -06003181#ifdef CONFIG_KVM_XICS
3182/*
3183 * Allocate a per-core structure for managing state about which cores are
3184 * running in the host versus the guest and for exchanging data between
3185 * real mode KVM and CPU running in the host.
3186 * This is only done for the first VM.
3187 * The allocated structure stays even if all VMs have stopped.
3188 * It is only freed when the kvm-hv module is unloaded.
3189 * It's OK for this routine to fail, we just don't support host
3190 * core operations like redirecting H_IPI wakeups.
3191 */
3192void kvmppc_alloc_host_rm_ops(void)
3193{
3194 struct kvmppc_host_rm_ops *ops;
3195 unsigned long l_ops;
3196 int cpu, core;
3197 int size;
3198
3199 /* Not the first time here ? */
3200 if (kvmppc_host_rm_ops_hv != NULL)
3201 return;
3202
3203 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3204 if (!ops)
3205 return;
3206
3207 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3208 ops->rm_core = kzalloc(size, GFP_KERNEL);
3209
3210 if (!ops->rm_core) {
3211 kfree(ops);
3212 return;
3213 }
3214
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003215 get_online_cpus();
3216
Suresh Warrier79b6c242015-12-17 14:59:06 -06003217 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3218 if (!cpu_online(cpu))
3219 continue;
3220
3221 core = cpu >> threads_shift;
3222 ops->rm_core[core].rm_state.in_host = 1;
3223 }
3224
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003225 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3226
Suresh Warrier79b6c242015-12-17 14:59:06 -06003227 /*
3228 * Make the contents of the kvmppc_host_rm_ops structure visible
3229 * to other CPUs before we assign it to the global variable.
3230 * Do an atomic assignment (no locks used here), but if someone
3231 * beats us to it, just free our copy and return.
3232 */
3233 smp_wmb();
3234 l_ops = (unsigned long) ops;
3235
3236 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003237 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003238 kfree(ops->rm_core);
3239 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003240 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003241 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003242
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003243 cpuhp_setup_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE,
3244 "ppc/kvm_book3s:prepare",
3245 kvmppc_set_host_core,
3246 kvmppc_clear_host_core);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003247 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003248}
3249
3250void kvmppc_free_host_rm_ops(void)
3251{
3252 if (kvmppc_host_rm_ops_hv) {
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003253 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003254 kfree(kvmppc_host_rm_ops_hv->rm_core);
3255 kfree(kvmppc_host_rm_ops_hv);
3256 kvmppc_host_rm_ops_hv = NULL;
3257 }
3258}
3259#endif
3260
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303261static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003262{
Paul Mackerras32fad282012-05-04 02:32:53 +00003263 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003264 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00003265
Paul Mackerras32fad282012-05-04 02:32:53 +00003266 /* Allocate the guest's logical partition ID */
3267
3268 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003269 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003270 return -ENOMEM;
3271 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003272
Suresh Warrier79b6c242015-12-17 14:59:06 -06003273 kvmppc_alloc_host_rm_ops();
3274
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003275 /*
3276 * Since we don't flush the TLB when tearing down a VM,
3277 * and this lpid might have previously been used,
3278 * make sure we flush on each core before running the new VM.
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003279 * On POWER9, the tlbie in mmu_partition_table_set_entry()
3280 * does this flush for us.
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003281 */
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003282 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3283 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003284
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003285 /* Start out with the default set of hcalls enabled */
3286 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3287 sizeof(kvm->arch.enabled_hcalls));
3288
Paul Mackerras7a840842016-11-16 22:25:20 +11003289 if (!cpu_has_feature(CPU_FTR_ARCH_300))
3290 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003291
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003292 /* Init LPCR for virtual RMA mode */
3293 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3294 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3295 lpcr &= LPCR_PECE | LPCR_LPES;
3296 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3297 LPCR_VPM0 | LPCR_VPM1;
3298 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3299 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3300 /* On POWER8 turn on online bit to enable PURR/SPURR */
3301 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3302 lpcr |= LPCR_ONL;
Paul Mackerras84f71392016-11-22 14:30:14 +11003303 /*
3304 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
3305 * Set HVICE bit to enable hypervisor virtualization interrupts.
3306 */
3307 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerras7a840842016-11-16 22:25:20 +11003308 lpcr &= ~LPCR_VPM0;
Paul Mackerras84f71392016-11-22 14:30:14 +11003309 lpcr |= LPCR_HVICE;
3310 }
3311
Paul Mackerras9e368f22011-06-29 00:40:08 +00003312 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003313
Paul Mackerras512691d2012-10-15 01:15:41 +00003314 /*
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003315 * Work out how many sets the TLB has, for the use of
3316 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
3317 */
3318 if (cpu_has_feature(CPU_FTR_ARCH_300))
3319 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
3320 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
3321 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
3322 else
3323 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
3324
3325 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003326 * Track that we now have a HV mode VM active. This blocks secondary
3327 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00003328 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10003329 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003330
Paul Mackerrase23a8082015-03-28 14:21:01 +11003331 /*
3332 * Create a debugfs directory for the VM
3333 */
3334 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3335 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3336 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3337 kvmppc_mmu_debugfs_init(kvm);
3338
David Gibson54738c02011-06-29 00:22:41 +00003339 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003340}
3341
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303342static void kvmppc_free_vcores(struct kvm *kvm)
3343{
3344 long int i;
3345
Paul Mackerras23316312015-10-21 16:03:14 +11003346 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303347 kfree(kvm->arch.vcores[i]);
3348 kvm->arch.online_vcores = 0;
3349}
3350
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303351static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003352{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003353 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3354
Michael Ellerman441c19c2014-05-23 18:15:25 +10003355 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003356
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303357 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003358
Paul Mackerrasde56a942011-06-29 00:21:34 +00003359 kvmppc_free_hpt(kvm);
Suresh Warrierc57875f2016-08-19 15:35:50 +10003360
3361 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003362}
3363
3364/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303365static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3366 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003367{
3368 return EMULATE_FAIL;
3369}
3370
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303371static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3372 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003373{
3374 return EMULATE_FAIL;
3375}
3376
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303377static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3378 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003379{
3380 return EMULATE_FAIL;
3381}
3382
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303383static int kvmppc_core_check_processor_compat_hv(void)
3384{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003385 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3386 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303387 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003388 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11003389 * Disable KVM for Power9 in radix mode.
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003390 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11003391 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003392 return -EIO;
3393
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303394 return 0;
3395}
3396
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003397#ifdef CONFIG_KVM_XICS
3398
3399void kvmppc_free_pimap(struct kvm *kvm)
3400{
3401 kfree(kvm->arch.pimap);
3402}
3403
Suresh Warrierc57875f2016-08-19 15:35:50 +10003404static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003405{
3406 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
3407}
Suresh Warrierc57875f2016-08-19 15:35:50 +10003408
3409static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3410{
3411 struct irq_desc *desc;
3412 struct kvmppc_irq_map *irq_map;
3413 struct kvmppc_passthru_irqmap *pimap;
3414 struct irq_chip *chip;
3415 int i;
3416
Suresh Warrier644abbb2016-08-19 15:35:54 +10003417 if (!kvm_irq_bypass)
3418 return 1;
3419
Suresh Warrierc57875f2016-08-19 15:35:50 +10003420 desc = irq_to_desc(host_irq);
3421 if (!desc)
3422 return -EIO;
3423
3424 mutex_lock(&kvm->lock);
3425
3426 pimap = kvm->arch.pimap;
3427 if (pimap == NULL) {
3428 /* First call, allocate structure to hold IRQ map */
3429 pimap = kvmppc_alloc_pimap();
3430 if (pimap == NULL) {
3431 mutex_unlock(&kvm->lock);
3432 return -ENOMEM;
3433 }
3434 kvm->arch.pimap = pimap;
3435 }
3436
3437 /*
3438 * For now, we only support interrupts for which the EOI operation
3439 * is an OPAL call followed by a write to XIRR, since that's
3440 * what our real-mode EOI code does.
3441 */
3442 chip = irq_data_get_irq_chip(&desc->irq_data);
3443 if (!chip || !is_pnv_opal_msi(chip)) {
3444 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
3445 host_irq, guest_gsi);
3446 mutex_unlock(&kvm->lock);
3447 return -ENOENT;
3448 }
3449
3450 /*
3451 * See if we already have an entry for this guest IRQ number.
3452 * If it's mapped to a hardware IRQ number, that's an error,
3453 * otherwise re-use this entry.
3454 */
3455 for (i = 0; i < pimap->n_mapped; i++) {
3456 if (guest_gsi == pimap->mapped[i].v_hwirq) {
3457 if (pimap->mapped[i].r_hwirq) {
3458 mutex_unlock(&kvm->lock);
3459 return -EINVAL;
3460 }
3461 break;
3462 }
3463 }
3464
3465 if (i == KVMPPC_PIRQ_MAPPED) {
3466 mutex_unlock(&kvm->lock);
3467 return -EAGAIN; /* table is full */
3468 }
3469
3470 irq_map = &pimap->mapped[i];
3471
3472 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10003473 irq_map->desc = desc;
3474
Suresh Warriere3c13e52016-08-19 15:35:51 +10003475 /*
3476 * Order the above two stores before the next to serialize with
3477 * the KVM real mode handler.
3478 */
3479 smp_wmb();
3480 irq_map->r_hwirq = desc->irq_data.hwirq;
3481
Suresh Warrierc57875f2016-08-19 15:35:50 +10003482 if (i == pimap->n_mapped)
3483 pimap->n_mapped++;
3484
Paul Mackerras5d375192016-08-19 15:35:56 +10003485 kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
3486
Suresh Warrierc57875f2016-08-19 15:35:50 +10003487 mutex_unlock(&kvm->lock);
3488
3489 return 0;
3490}
3491
3492static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
3493{
3494 struct irq_desc *desc;
3495 struct kvmppc_passthru_irqmap *pimap;
3496 int i;
3497
Suresh Warrier644abbb2016-08-19 15:35:54 +10003498 if (!kvm_irq_bypass)
3499 return 0;
3500
Suresh Warrierc57875f2016-08-19 15:35:50 +10003501 desc = irq_to_desc(host_irq);
3502 if (!desc)
3503 return -EIO;
3504
3505 mutex_lock(&kvm->lock);
3506
3507 if (kvm->arch.pimap == NULL) {
3508 mutex_unlock(&kvm->lock);
3509 return 0;
3510 }
3511 pimap = kvm->arch.pimap;
3512
3513 for (i = 0; i < pimap->n_mapped; i++) {
3514 if (guest_gsi == pimap->mapped[i].v_hwirq)
3515 break;
3516 }
3517
3518 if (i == pimap->n_mapped) {
3519 mutex_unlock(&kvm->lock);
3520 return -ENODEV;
3521 }
3522
Paul Mackerras5d375192016-08-19 15:35:56 +10003523 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
3524
Suresh Warrierc57875f2016-08-19 15:35:50 +10003525 /* invalidate the entry */
3526 pimap->mapped[i].r_hwirq = 0;
3527
3528 /*
3529 * We don't free this structure even when the count goes to
3530 * zero. The structure is freed when we destroy the VM.
3531 */
3532
3533 mutex_unlock(&kvm->lock);
3534 return 0;
3535}
3536
3537static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
3538 struct irq_bypass_producer *prod)
3539{
3540 int ret = 0;
3541 struct kvm_kernel_irqfd *irqfd =
3542 container_of(cons, struct kvm_kernel_irqfd, consumer);
3543
3544 irqfd->producer = prod;
3545
3546 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3547 if (ret)
3548 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
3549 prod->irq, irqfd->gsi, ret);
3550
3551 return ret;
3552}
3553
3554static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
3555 struct irq_bypass_producer *prod)
3556{
3557 int ret;
3558 struct kvm_kernel_irqfd *irqfd =
3559 container_of(cons, struct kvm_kernel_irqfd, consumer);
3560
3561 irqfd->producer = NULL;
3562
3563 /*
3564 * When producer of consumer is unregistered, we change back to
3565 * default external interrupt handling mode - KVM real mode
3566 * will switch back to host.
3567 */
3568 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
3569 if (ret)
3570 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
3571 prod->irq, irqfd->gsi, ret);
3572}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10003573#endif
3574
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303575static long kvm_arch_vm_ioctl_hv(struct file *filp,
3576 unsigned int ioctl, unsigned long arg)
3577{
3578 struct kvm *kvm __maybe_unused = filp->private_data;
3579 void __user *argp = (void __user *)arg;
3580 long r;
3581
3582 switch (ioctl) {
3583
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303584 case KVM_PPC_ALLOCATE_HTAB: {
3585 u32 htab_order;
3586
3587 r = -EFAULT;
3588 if (get_user(htab_order, (u32 __user *)argp))
3589 break;
3590 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3591 if (r)
3592 break;
3593 r = -EFAULT;
3594 if (put_user(htab_order, (u32 __user *)argp))
3595 break;
3596 r = 0;
3597 break;
3598 }
3599
3600 case KVM_PPC_GET_HTAB_FD: {
3601 struct kvm_get_htab_fd ghf;
3602
3603 r = -EFAULT;
3604 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3605 break;
3606 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3607 break;
3608 }
3609
3610 default:
3611 r = -ENOTTY;
3612 }
3613
3614 return r;
3615}
3616
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003617/*
3618 * List of hcall numbers to enable by default.
3619 * For compatibility with old userspace, we enable by default
3620 * all hcalls that were implemented before the hcall-enabling
3621 * facility was added. Note this list should not include H_RTAS.
3622 */
3623static unsigned int default_hcall_list[] = {
3624 H_REMOVE,
3625 H_ENTER,
3626 H_READ,
3627 H_PROTECT,
3628 H_BULK_REMOVE,
3629 H_GET_TCE,
3630 H_PUT_TCE,
3631 H_SET_DABR,
3632 H_SET_XDABR,
3633 H_CEDE,
3634 H_PROD,
3635 H_CONFER,
3636 H_REGISTER_VPA,
3637#ifdef CONFIG_KVM_XICS
3638 H_EOI,
3639 H_CPPR,
3640 H_IPI,
3641 H_IPOLL,
3642 H_XIRR,
3643 H_XIRR_X,
3644#endif
3645 0
3646};
3647
3648static void init_default_hcalls(void)
3649{
3650 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003651 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003652
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003653 for (i = 0; default_hcall_list[i]; ++i) {
3654 hcall = default_hcall_list[i];
3655 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3656 __set_bit(hcall / 4, default_enabled_hcalls);
3657 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003658}
3659
Paul Mackerrasc9270132017-01-30 21:21:41 +11003660/* dummy implementations for now */
3661static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
3662{
3663 return -EINVAL;
3664}
3665
3666static int kvmhv_get_rmmu_info(struct kvm *kvm, struct kvm_ppc_rmmu_info *info)
3667{
3668 return -EINVAL;
3669}
3670
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303671static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303672 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3673 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3674 .get_one_reg = kvmppc_get_one_reg_hv,
3675 .set_one_reg = kvmppc_set_one_reg_hv,
3676 .vcpu_load = kvmppc_core_vcpu_load_hv,
3677 .vcpu_put = kvmppc_core_vcpu_put_hv,
3678 .set_msr = kvmppc_set_msr_hv,
3679 .vcpu_run = kvmppc_vcpu_run_hv,
3680 .vcpu_create = kvmppc_core_vcpu_create_hv,
3681 .vcpu_free = kvmppc_core_vcpu_free_hv,
3682 .check_requests = kvmppc_core_check_requests_hv,
3683 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3684 .flush_memslot = kvmppc_core_flush_memslot_hv,
3685 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3686 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3687 .unmap_hva = kvm_unmap_hva_hv,
3688 .unmap_hva_range = kvm_unmap_hva_range_hv,
3689 .age_hva = kvm_age_hva_hv,
3690 .test_age_hva = kvm_test_age_hva_hv,
3691 .set_spte_hva = kvm_set_spte_hva_hv,
3692 .mmu_destroy = kvmppc_mmu_destroy_hv,
3693 .free_memslot = kvmppc_core_free_memslot_hv,
3694 .create_memslot = kvmppc_core_create_memslot_hv,
3695 .init_vm = kvmppc_core_init_vm_hv,
3696 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303697 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3698 .emulate_op = kvmppc_core_emulate_op_hv,
3699 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3700 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3701 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3702 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003703 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10003704#ifdef CONFIG_KVM_XICS
3705 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
3706 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
3707#endif
Paul Mackerrasc9270132017-01-30 21:21:41 +11003708 .configure_mmu = kvmhv_configure_mmu,
3709 .get_rmmu_info = kvmhv_get_rmmu_info,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303710};
3711
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303712static int kvm_init_subcore_bitmap(void)
3713{
3714 int i, j;
3715 int nr_cores = cpu_nr_cores();
3716 struct sibling_subcore_state *sibling_subcore_state;
3717
3718 for (i = 0; i < nr_cores; i++) {
3719 int first_cpu = i * threads_per_core;
3720 int node = cpu_to_node(first_cpu);
3721
3722 /* Ignore if it is already allocated. */
3723 if (paca[first_cpu].sibling_subcore_state)
3724 continue;
3725
3726 sibling_subcore_state =
3727 kmalloc_node(sizeof(struct sibling_subcore_state),
3728 GFP_KERNEL, node);
3729 if (!sibling_subcore_state)
3730 return -ENOMEM;
3731
3732 memset(sibling_subcore_state, 0,
3733 sizeof(struct sibling_subcore_state));
3734
3735 for (j = 0; j < threads_per_core; j++) {
3736 int cpu = first_cpu + j;
3737
3738 paca[cpu].sibling_subcore_state = sibling_subcore_state;
3739 }
3740 }
3741 return 0;
3742}
3743
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303744static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003745{
3746 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303747 /*
3748 * FIXME!! Do we need to check on all cpus ?
3749 */
3750 r = kvmppc_core_check_processor_compat_hv();
3751 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003752 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003753
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303754 r = kvm_init_subcore_bitmap();
3755 if (r)
3756 return r;
3757
Paul Mackerrasf7257582016-11-18 09:02:08 +11003758 /*
3759 * We need a way of accessing the XICS interrupt controller,
3760 * either directly, via paca[cpu].kvm_hstate.xics_phys, or
3761 * indirectly, via OPAL.
3762 */
3763#ifdef CONFIG_SMP
3764 if (!get_paca()->kvm_hstate.xics_phys) {
3765 struct device_node *np;
3766
3767 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
3768 if (!np) {
3769 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
3770 return -ENODEV;
3771 }
3772 }
3773#endif
3774
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303775 kvm_ops_hv.owner = THIS_MODULE;
3776 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003777
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003778 init_default_hcalls();
3779
Paul Mackerrasec257162015-06-24 21:18:03 +10003780 init_vcore_lists();
3781
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303782 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003783 return r;
3784}
3785
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303786static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003787{
Suresh Warrier79b6c242015-12-17 14:59:06 -06003788 kvmppc_free_host_rm_ops();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303789 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003790}
3791
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303792module_init(kvmppc_book3s_init_hv);
3793module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05303794MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01003795MODULE_ALIAS_MISCDEV(KVM_MINOR);
3796MODULE_ALIAS("devname:kvm");
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11003797