blob: ad8dc4ccdaab9e1a83104dd92ce9ac389d8c129c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Paul Mackerrasde56a942011-06-29 00:21:34 +00002/*
3 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
4 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 *
6 * Authors:
7 * Paul Mackerras <paulus@au1.ibm.com>
8 * Alexander Graf <agraf@suse.de>
9 * Kevin Wolf <mail@kevin-wolf.de>
10 *
11 * Description: KVM functions specific to running on Book 3S
12 * processors in hypervisor mode (specifically POWER7 and later).
13 *
14 * This file is derived from arch/powerpc/kvm/book3s.c,
15 * by Alexander Graf <agraf@suse.de>.
Paul Mackerrasde56a942011-06-29 00:21:34 +000016 */
17
18#include <linux/kvm_host.h>
Thomas Meyer4bb817ed2017-09-03 14:19:31 +020019#include <linux/kernel.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000020#include <linux/err.h>
21#include <linux/slab.h>
22#include <linux/preempt.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010024#include <linux/sched/stat.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000025#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040026#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <linux/fs.h>
28#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100029#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000030#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000031#include <linux/spinlock.h>
32#include <linux/page-flags.h>
Paul Mackerras2c9097e42012-09-11 13:27:01 +000033#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010034#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110035#include <linux/debugfs.h>
Benjamin Herrenschmidtd3989142017-04-05 17:54:51 +100036#include <linux/gfp.h>
37#include <linux/vmalloc.h>
38#include <linux/highmem.h>
39#include <linux/hugetlb.h>
40#include <linux/kvm_irqfd.h>
41#include <linux/irqbypass.h>
42#include <linux/module.h>
43#include <linux/compiler.h>
44#include <linux/of.h>
Christophe Leroye6f63902022-03-08 20:20:25 +010045#include <linux/irqdomain.h>
Valentin Schneider4c8c3c72023-03-07 14:35:56 +000046#include <linux/smp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000047
Luke Dashjrd6ee76d2018-08-16 21:36:26 +000048#include <asm/ftrace.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000049#include <asm/reg.h>
Paul Mackerras57900692017-05-16 16:41:20 +100050#include <asm/ppc-opcode.h>
Nicholas Piggin6de66382017-11-05 23:33:55 +110051#include <asm/asm-prototypes.h>
Paul Mackerras4bad7772018-10-08 16:31:06 +110052#include <asm/archrandom.h>
Nicholas Piggine303c082018-04-01 15:50:35 +100053#include <asm/debug.h>
Paul Mackerras57900692017-05-16 16:41:20 +100054#include <asm/disassemble.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055#include <asm/cputable.h>
56#include <asm/cacheflush.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080057#include <linux/uaccess.h>
Nicholas Piggin3a965702021-01-30 23:08:38 +100058#include <asm/interrupt.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000059#include <asm/io.h>
60#include <asm/kvm_ppc.h>
61#include <asm/kvm_book3s.h>
62#include <asm/mmu_context.h>
63#include <asm/lppaca.h>
Nicholas Piggin17826632021-08-12 02:00:43 +100064#include <asm/pmc.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000065#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000066#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000067#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000068#include <asm/hvcall.h>
David Howellsae3a197e2012-03-28 18:30:02 +010069#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000070#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110071#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053072#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100073#include <asm/pnv-pci.h>
Paul Mackerras7a840842016-11-16 22:25:20 +110074#include <asm/mmu.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110075#include <asm/opal.h>
76#include <asm/xics.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100077#include <asm/xive.h>
Michael Neulingc1fe190c2019-04-01 17:03:12 +110078#include <asm/hw_breakpoint.h>
Bharata B Raoca9f4942019-11-25 08:36:26 +053079#include <asm/kvm_book3s_uvmem.h>
Bharata B Raoc3262252019-11-25 08:36:29 +053080#include <asm/ultravisor.h>
Peter Zijlstrad6bdceb2020-05-29 22:41:01 +020081#include <asm/dtl.h>
Bharata B Raof0c6fbb2021-06-21 14:20:00 +053082#include <asm/plpar_wrappers.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000083
Valentin Schneider4c8c3c72023-03-07 14:35:56 +000084#include <trace/events/ipi.h>
85
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053086#include "book3s.h"
Nicholas Pigginaabcaf62021-11-23 19:52:07 +100087#include "book3s_hv.h"
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053088
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060089#define CREATE_TRACE_POINTS
90#include "trace_hv.h"
91
Paul Mackerrasde56a942011-06-29 00:21:34 +000092/* #define EXIT_DEBUG */
93/* #define EXIT_DEBUG_SIMPLE */
94/* #define EXIT_DEBUG_INT */
95
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000096/* Used to indicate that a guest page fault needs to be handled */
97#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100098/* Used to indicate that a guest passthrough interrupt needs to be handled */
99#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000100
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000101/* Used as a "null" value for timebase values */
102#define TB_NIL (~(u64)0)
103
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000104static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
105
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000106static int dynamic_mt_modes = 6;
Russell Currey57ad583f2017-01-12 14:54:13 +1100107module_param(dynamic_mt_modes, int, 0644);
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000108MODULE_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 +1000109static int target_smt_mode;
Russell Currey57ad583f2017-01-12 14:54:13 +1100110module_param(target_smt_mode, int, 0644);
Paul Mackerrasec257162015-06-24 21:18:03 +1000111MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +1000112
Paul Mackerrasaa227862018-09-12 10:42:12 +1000113static bool one_vm_per_core;
114module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
Nicholas Pigginaaae8c72021-05-28 19:07:42 +1000115MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
Paul Mackerrasaa227862018-09-12 10:42:12 +1000116
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600117#ifdef CONFIG_KVM_XICS
Joe Perchesa4f1d942020-10-03 17:18:06 -0700118static const struct kernel_param_ops module_param_ops = {
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600119 .set = param_set_int,
120 .get = param_get_int,
121};
122
Russell Currey57ad583f2017-01-12 14:54:13 +1100123module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
Suresh Warrier644abbb2016-08-19 15:35:54 +1000124MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
125
Russell Currey57ad583f2017-01-12 14:54:13 +1100126module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600127MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
128#endif
129
Paul Mackerrasaa069a92018-09-21 20:02:01 +1000130/* If set, guests are allowed to create and control nested guests */
131static bool nested = true;
132module_param(nested, bool, S_IRUGO | S_IWUSR);
133MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
134
Paul Mackerras32fad282012-05-04 02:32:53 +0000135static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000136
Paul Mackerras7aa15842018-04-20 19:53:22 +1000137/*
138 * RWMR values for POWER8. These control the rate at which PURR
139 * and SPURR count and should be set according to the number of
140 * online threads in the vcore being run.
141 */
Nicholas Mc Guire0abb75b2018-07-07 11:07:25 +0200142#define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
143#define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
144#define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
145#define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
146#define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
147#define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
148#define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
149#define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
Paul Mackerras7aa15842018-04-20 19:53:22 +1000150
151static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
152 RWMR_RPA_P8_1THREAD,
153 RWMR_RPA_P8_1THREAD,
154 RWMR_RPA_P8_2THREAD,
155 RWMR_RPA_P8_3THREAD,
156 RWMR_RPA_P8_4THREAD,
157 RWMR_RPA_P8_5THREAD,
158 RWMR_RPA_P8_6THREAD,
159 RWMR_RPA_P8_7THREAD,
160 RWMR_RPA_P8_8THREAD,
161};
162
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000163static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
164 int *ip)
165{
166 int i = *ip;
167 struct kvm_vcpu *vcpu;
168
169 while (++i < MAX_SMT_THREADS) {
170 vcpu = READ_ONCE(vc->runnable_threads[i]);
171 if (vcpu) {
172 *ip = i;
173 return vcpu;
174 }
175 }
176 return NULL;
177}
178
179/* Used to traverse the list of runnable threads for a given vcore */
180#define for_each_runnable_thread(i, vcpu, vc) \
181 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
182
Paul Mackerras66feed62015-03-28 14:21:12 +1100183static bool kvmppc_ipi_thread(int cpu)
184{
Paul Mackerras1704a812016-11-18 08:47:08 +1100185 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
186
Paul Mackerrasf3c18e92018-10-08 16:31:05 +1100187 /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
188 if (kvmhv_on_pseries())
189 return false;
190
Paul Mackerras1704a812016-11-18 08:47:08 +1100191 /* On POWER9 we can use msgsnd to IPI any cpu */
192 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
193 msg |= get_hard_smp_processor_id(cpu);
194 smp_mb();
195 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
196 return true;
197 }
198
Paul Mackerras66feed62015-03-28 14:21:12 +1100199 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
200 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
201 preempt_disable();
202 if (cpu_first_thread_sibling(cpu) ==
203 cpu_first_thread_sibling(smp_processor_id())) {
Paul Mackerras66feed62015-03-28 14:21:12 +1100204 msg |= cpu_thread_in_core(cpu);
205 smp_mb();
206 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
207 preempt_enable();
208 return true;
209 }
210 preempt_enable();
211 }
212
213#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrasf7257582016-11-18 09:02:08 +1100214 if (cpu >= 0 && cpu < nr_cpu_ids) {
Nicholas Piggind2e60072018-02-14 01:08:12 +1000215 if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +1100216 xics_wake_cpu(cpu);
217 return true;
218 }
219 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
Paul Mackerras66feed62015-03-28 14:21:12 +1100220 return true;
221 }
222#endif
223
224 return false;
225}
226
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530227static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000228{
Paul Mackerrasec257162015-06-24 21:18:03 +1000229 int cpu;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700230 struct rcuwait *waitp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000231
Nicholas Pigginc7fa8482022-03-03 15:33:10 +1000232 /*
233 * rcuwait_wake_up contains smp_mb() which orders prior stores that
234 * create pending work vs below loads of cpu fields. The other side
235 * is the barrier in vcpu run that orders setting the cpu fields vs
236 * testing for pending work.
237 */
238
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700239 waitp = kvm_arch_vcpu_get_wait(vcpu);
240 if (rcuwait_wake_up(waitp))
Jing Zhang0193cc92021-06-18 22:27:03 +0000241 ++vcpu->stat.generic.halt_wakeup;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000242
Paul Mackerras3deda5e2016-12-20 14:02:29 +1100243 cpu = READ_ONCE(vcpu->arch.thread_cpu);
244 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100245 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000246
247 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000248 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100249 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
250 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000251}
252
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000253/*
254 * We use the vcpu_load/put functions to measure stolen time.
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000255 *
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000256 * Stolen time is counted as time when either the vcpu is able to
257 * run as part of a virtual core, but the task running the vcore
258 * is preempted or sleeping, or when the vcpu needs something done
259 * in the kernel by the task running the vcpu, but that task is
260 * preempted or sleeping. Those two things have to be counted
261 * separately, since one of the vcpu tasks will take on the job
262 * of running the core, and the other vcpu tasks in the vcore will
263 * sleep waiting for it to do that, but that sleep shouldn't count
264 * as stolen time.
265 *
266 * Hence we accumulate stolen time when the vcpu can run as part of
267 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
268 * needs its task to do other things in the kernel (for example,
269 * service a page fault) in busy_stolen. We don't accumulate
270 * stolen time for a vcore when it is inactive, or for a vcpu
271 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
272 * a misnomer; it means that the vcpu task is not executing in
273 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
274 * the kernel. We don't have any way of dividing up that time
275 * between time that the vcpu is genuinely stopped, time that
276 * the task is actively working on behalf of the vcpu, and time
277 * that the task is preempted, so we don't count any of it as
278 * stolen.
279 *
280 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100281 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
282 * lock. The stolen times are measured in units of timebase ticks.
283 * (Note that the != TB_NIL checks below are purely defensive;
284 * they should never fail.)
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000285 *
286 * The POWER9 path is simpler, one vcpu per virtual core so the
287 * former case does not exist. If a vcpu is preempted when it is
288 * BUSY_IN_HOST and not ceded or otherwise blocked, then accumulate
289 * the stolen cycles in busy_stolen. RUNNING is not a preemptible
290 * state in the P9 path.
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000291 */
292
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000293static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc, u64 tb)
Paul Mackerrasec257162015-06-24 21:18:03 +1000294{
295 unsigned long flags;
296
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000297 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
298
Paul Mackerrasec257162015-06-24 21:18:03 +1000299 spin_lock_irqsave(&vc->stoltb_lock, flags);
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000300 vc->preempt_tb = tb;
Paul Mackerrasec257162015-06-24 21:18:03 +1000301 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
302}
303
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000304static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc, u64 tb)
Paul Mackerrasec257162015-06-24 21:18:03 +1000305{
306 unsigned long flags;
307
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000308 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
309
Paul Mackerrasec257162015-06-24 21:18:03 +1000310 spin_lock_irqsave(&vc->stoltb_lock, flags);
311 if (vc->preempt_tb != TB_NIL) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000312 vc->stolen_tb += tb - vc->preempt_tb;
Paul Mackerrasec257162015-06-24 21:18:03 +1000313 vc->preempt_tb = TB_NIL;
314 }
315 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
316}
317
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530318static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000319{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000320 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100321 unsigned long flags;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000322 u64 now;
323
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000324 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
325 if (vcpu->arch.busy_preempt != TB_NIL) {
326 WARN_ON_ONCE(vcpu->arch.state != KVMPPC_VCPU_BUSY_IN_HOST);
327 vc->stolen_tb += mftb() - vcpu->arch.busy_preempt;
328 vcpu->arch.busy_preempt = TB_NIL;
329 }
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000330 return;
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000331 }
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000332
333 now = mftb();
Paul Mackerras0456ec42012-02-03 00:56:21 +0000334
Paul Mackerras2711e242014-12-04 16:43:28 +1100335 /*
336 * We can test vc->runner without taking the vcore lock,
337 * because only this task ever sets vc->runner to this
338 * vcpu, and once it is set to this vcpu, only this task
339 * ever sets it to NULL.
340 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000341 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000342 kvmppc_core_end_stolen(vc, now);
Paul Mackerrasec257162015-06-24 21:18:03 +1000343
Paul Mackerras2711e242014-12-04 16:43:28 +1100344 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000345 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
346 vcpu->arch.busy_preempt != TB_NIL) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000347 vcpu->arch.busy_stolen += now - vcpu->arch.busy_preempt;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000348 vcpu->arch.busy_preempt = TB_NIL;
349 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100350 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000351}
352
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530353static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000354{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000355 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100356 unsigned long flags;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000357 u64 now;
358
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000359 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
360 /*
361 * In the P9 path, RUNNABLE is not preemptible
362 * (nor takes host interrupts)
363 */
364 WARN_ON_ONCE(vcpu->arch.state == KVMPPC_VCPU_RUNNABLE);
365 /*
366 * Account stolen time when preempted while the vcpu task is
367 * running in the kernel (but not in qemu, which is INACTIVE).
368 */
369 if (task_is_running(current) &&
370 vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
371 vcpu->arch.busy_preempt = mftb();
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000372 return;
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000373 }
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000374
375 now = mftb();
Paul Mackerras0456ec42012-02-03 00:56:21 +0000376
Paul Mackerrasec257162015-06-24 21:18:03 +1000377 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000378 kvmppc_core_start_stolen(vc, now);
Paul Mackerrasec257162015-06-24 21:18:03 +1000379
Paul Mackerras2711e242014-12-04 16:43:28 +1100380 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000381 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000382 vcpu->arch.busy_preempt = now;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100383 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000384}
385
Thomas Huth5358a962015-05-22 09:25:02 +0200386static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000387{
388 vcpu->arch.pvr = pvr;
389}
390
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000391/* Dummy value used in computing PCR value below */
392#define PCR_ARCH_31 (PCR_ARCH_300 << 1)
393
Amit Machhiwal20c8c4d2024-02-07 11:15:26 +0530394static inline unsigned long map_pcr_to_cap(unsigned long pcr)
395{
396 unsigned long cap = 0;
397
398 switch (pcr) {
399 case PCR_ARCH_300:
400 cap = H_GUEST_CAP_POWER9;
401 break;
402 case PCR_ARCH_31:
403 cap = H_GUEST_CAP_POWER10;
404 break;
405 default:
406 break;
407 }
408
409 return cap;
410}
411
Thomas Huth5358a962015-05-22 09:25:02 +0200412static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000413{
Jordan Niethe19d31c52023-09-14 13:05:59 +1000414 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0, cap = 0;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000415 struct kvmppc_vcore *vc = vcpu->arch.vcore;
416
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100417 /* We can (emulate) our own architecture version and anything older */
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000418 if (cpu_has_feature(CPU_FTR_ARCH_31))
419 host_pcr_bit = PCR_ARCH_31;
420 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100421 host_pcr_bit = PCR_ARCH_300;
422 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
423 host_pcr_bit = PCR_ARCH_207;
424 else if (cpu_has_feature(CPU_FTR_ARCH_206))
425 host_pcr_bit = PCR_ARCH_206;
426 else
427 host_pcr_bit = PCR_ARCH_205;
428
429 /* Determine lowest PCR bit needed to run guest in given PVR level */
430 guest_pcr_bit = host_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000431 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000432 switch (arch_compat) {
433 case PVR_ARCH_205:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100434 guest_pcr_bit = PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000435 break;
436 case PVR_ARCH_206:
437 case PVR_ARCH_206p:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100438 guest_pcr_bit = PCR_ARCH_206;
Paul Mackerras5557ae02014-01-08 21:25:24 +1100439 break;
440 case PVR_ARCH_207:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100441 guest_pcr_bit = PCR_ARCH_207;
442 break;
443 case PVR_ARCH_300:
444 guest_pcr_bit = PCR_ARCH_300;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000445 break;
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000446 case PVR_ARCH_31:
Madhavan Srinivasanc2ed0872024-02-21 15:46:22 +1100447 case PVR_ARCH_31_P11:
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000448 guest_pcr_bit = PCR_ARCH_31;
449 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000450 default:
451 return -EINVAL;
452 }
453 }
454
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100455 /* Check requested PCR bits don't exceed our capabilities */
456 if (guest_pcr_bit > host_pcr_bit)
457 return -EINVAL;
458
Jordan Niethe19d31c52023-09-14 13:05:59 +1000459 if (kvmhv_on_pseries() && kvmhv_is_nestedv2()) {
Amit Machhiwal20c8c4d2024-02-07 11:15:26 +0530460 /*
461 * 'arch_compat == 0' would mean the guest should default to
462 * L1's compatibility. In this case, the guest would pick
463 * host's PCR and evaluate the corresponding capabilities.
464 */
465 cap = map_pcr_to_cap(guest_pcr_bit);
Jordan Niethe19d31c52023-09-14 13:05:59 +1000466 if (!(cap & nested_capabilities))
467 return -EINVAL;
468 }
469
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000470 spin_lock(&vc->lock);
471 vc->arch_compat = arch_compat;
Jordan Niethe19d31c52023-09-14 13:05:59 +1000472 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LOGICAL_PVR);
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000473 /*
474 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
475 * Also set all reserved PCR bits
476 */
477 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000478 spin_unlock(&vc->lock);
479
480 return 0;
481}
482
Thomas Huth5358a962015-05-22 09:25:02 +0200483static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000484{
485 int r;
486
487 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
488 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
Simon Guo173c5202018-05-07 14:20:08 +0800489 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000490 for (r = 0; r < 16; ++r)
491 pr_err("r%2d = %.16lx r%d = %.16lx\n",
492 r, kvmppc_get_gpr(vcpu, r),
493 r+16, kvmppc_get_gpr(vcpu, r+16));
494 pr_err("ctr = %.16lx lr = %.16lx\n",
Simon Guo173c5202018-05-07 14:20:08 +0800495 vcpu->arch.regs.ctr, vcpu->arch.regs.link);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000496 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
497 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
498 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
499 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
500 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
501 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
Paul Mackerrasfd0944b2018-10-08 16:30:58 +1100502 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
503 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000504 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
505 pr_err("fault dar = %.16lx dsisr = %.8x\n",
506 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
507 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
508 for (r = 0; r < vcpu->arch.slb_max; ++r)
509 pr_err(" ESID = %.16llx VSID = %.16llx\n",
510 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
Paul Mackerras953e3732023-03-08 17:35:23 +1100511 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.16lx\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000512 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000513 vcpu->arch.last_inst);
514}
515
Thomas Huth5358a962015-05-22 09:25:02 +0200516static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000517{
Paul Mackerras5a3f4932019-05-23 16:36:32 +1000518 return kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000519}
520
521static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
522{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000523 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200524 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000525}
526
Paul Mackerras55b665b2012-09-25 20:33:06 +0000527static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
528 unsigned long addr, unsigned long len)
529{
530 /* check address is cacheline aligned */
531 if (addr & (L1_CACHE_BYTES - 1))
532 return -EINVAL;
533 spin_lock(&vcpu->arch.vpa_update_lock);
534 if (v->next_gpa != addr || v->len != len) {
535 v->next_gpa = addr;
536 v->len = addr ? len : 0;
537 v->update_pending = 1;
538 }
539 spin_unlock(&vcpu->arch.vpa_update_lock);
540 return 0;
541}
542
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000543/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
544struct reg_vpa {
545 u32 dummy;
546 union {
Alexander Graf02407552014-06-11 10:34:19 +0200547 __be16 hword;
548 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000549 } length;
550};
551
552static int vpa_is_registered(struct kvmppc_vpa *vpap)
553{
554 if (vpap->update_pending)
555 return vpap->next_gpa != 0;
556 return vpap->pinned_addr != NULL;
557}
558
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000559static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
560 unsigned long flags,
561 unsigned long vcpuid, unsigned long vpa)
562{
563 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000564 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000565 void *va;
566 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000567 int err;
568 int subfunc;
569 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000570
571 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
572 if (!tvcpu)
573 return H_PARAMETER;
574
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000575 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
576 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
577 subfunc == H_VPA_REG_SLB) {
578 /* Registering new area - address must be cache-line aligned */
579 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000580 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000581
582 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000583 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
584 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000585 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000586 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200587 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000588 else
Alexander Graf02407552014-06-11 10:34:19 +0200589 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000590 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000591
592 /* Check length */
593 if (len > nb || len < sizeof(struct reg_vpa))
594 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000595 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000596 vpa = 0;
597 len = 0;
598 }
599
600 err = H_PARAMETER;
601 vpap = NULL;
602 spin_lock(&tvcpu->arch.vpa_update_lock);
603
604 switch (subfunc) {
605 case H_VPA_REG_VPA: /* register VPA */
Nicholas Piggineaac112e2017-08-13 11:33:38 +1000606 /*
607 * The size of our lppaca is 1kB because of the way we align
608 * it for the guest to avoid crossing a 4kB boundary. We only
609 * use 640 bytes of the structure though, so we should accept
610 * clients that set a size of 640.
611 */
Nicholas Piggin499dcd42018-02-14 01:08:13 +1000612 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
613 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000614 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000615 vpap = &tvcpu->arch.vpa;
616 err = 0;
617 break;
618
619 case H_VPA_REG_DTL: /* register DTL */
620 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000621 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000622 len -= len % sizeof(struct dtl_entry);
623
624 /* Check that they have previously registered a VPA */
625 err = H_RESOURCE;
626 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000627 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000628
629 vpap = &tvcpu->arch.dtl;
630 err = 0;
631 break;
632
633 case H_VPA_REG_SLB: /* register SLB shadow buffer */
634 /* Check that they have previously registered a VPA */
635 err = H_RESOURCE;
636 if (!vpa_is_registered(&tvcpu->arch.vpa))
637 break;
638
639 vpap = &tvcpu->arch.slb_shadow;
640 err = 0;
641 break;
642
643 case H_VPA_DEREG_VPA: /* deregister VPA */
644 /* Check they don't still have a DTL or SLB buf registered */
645 err = H_RESOURCE;
646 if (vpa_is_registered(&tvcpu->arch.dtl) ||
647 vpa_is_registered(&tvcpu->arch.slb_shadow))
648 break;
649
650 vpap = &tvcpu->arch.vpa;
651 err = 0;
652 break;
653
654 case H_VPA_DEREG_DTL: /* deregister DTL */
655 vpap = &tvcpu->arch.dtl;
656 err = 0;
657 break;
658
659 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
660 vpap = &tvcpu->arch.slb_shadow;
661 err = 0;
662 break;
663 }
664
665 if (vpap) {
666 vpap->next_gpa = vpa;
667 vpap->len = len;
668 vpap->update_pending = 1;
669 }
670
671 spin_unlock(&tvcpu->arch.vpa_update_lock);
672
673 return err;
674}
675
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530676static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap,
677 struct kvmppc_vpa *old_vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000678{
Paul Mackerras081f3232012-06-01 20:20:24 +1000679 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000680 void *va;
681 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000682 unsigned long gpa;
683
684 /*
685 * We need to pin the page pointed to by vpap->next_gpa,
686 * but we can't call kvmppc_pin_guest_page under the lock
687 * as it does get_user_pages() and down_read(). So we
688 * have to drop the lock, pin the page, then get the lock
689 * again and check that a new area didn't get registered
690 * in the meantime.
691 */
692 for (;;) {
693 gpa = vpap->next_gpa;
694 spin_unlock(&vcpu->arch.vpa_update_lock);
695 va = NULL;
696 nb = 0;
697 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000698 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000699 spin_lock(&vcpu->arch.vpa_update_lock);
700 if (gpa == vpap->next_gpa)
701 break;
702 /* sigh... unpin that one and try again */
703 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000704 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000705 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000706
707 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000708 if (va && nb < vpap->len) {
709 /*
710 * If it's now too short, it must be that userspace
711 * has changed the mappings underlying guest memory,
712 * so unregister the region.
713 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000714 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000715 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000716 }
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530717 *old_vpap = *vpap;
718
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000719 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000720 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000721 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000722 if (va)
723 vpap->pinned_end = va + vpap->len;
724}
Paul Mackerras93e60242011-12-12 12:28:55 +0000725
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000726static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
727{
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530728 struct kvm *kvm = vcpu->kvm;
729 struct kvmppc_vpa old_vpa = { 0 };
730
Paul Mackerras2f12f032012-10-15 01:17:17 +0000731 if (!(vcpu->arch.vpa.update_pending ||
732 vcpu->arch.slb_shadow.update_pending ||
733 vcpu->arch.dtl.update_pending))
734 return;
735
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000736 spin_lock(&vcpu->arch.vpa_update_lock);
737 if (vcpu->arch.vpa.update_pending) {
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530738 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa, &old_vpa);
739 if (old_vpa.pinned_addr) {
740 if (kvmhv_is_nestedv2())
741 kvmhv_nestedv2_set_vpa(vcpu, ~0ull);
742 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
743 old_vpa.dirty);
744 }
745 if (vcpu->arch.vpa.pinned_addr) {
Paul Mackerras55b665b2012-09-25 20:33:06 +0000746 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530747 if (kvmhv_is_nestedv2())
748 kvmhv_nestedv2_set_vpa(vcpu, __pa(vcpu->arch.vpa.pinned_addr));
749 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000750 }
751 if (vcpu->arch.dtl.update_pending) {
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530752 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl, &old_vpa);
753 if (old_vpa.pinned_addr)
754 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
755 old_vpa.dirty);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000756 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
757 vcpu->arch.dtl_index = 0;
758 }
Jordan Niethedb1dcfae2023-12-01 18:56:15 +0530759 if (vcpu->arch.slb_shadow.update_pending) {
760 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow, &old_vpa);
761 if (old_vpa.pinned_addr)
762 kvmppc_unpin_guest_page(kvm, old_vpa.pinned_addr, old_vpa.gpa,
763 old_vpa.dirty);
764 }
765
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000766 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000767}
768
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000769/*
770 * Return the accumulated stolen time for the vcore up until `now'.
771 * The caller should hold the vcore lock.
772 */
773static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
774{
775 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100776 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000777
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000778 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
779
Paul Mackerras2711e242014-12-04 16:43:28 +1100780 spin_lock_irqsave(&vc->stoltb_lock, flags);
781 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000782 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100783 vc->preempt_tb != TB_NIL)
784 p += now - vc->preempt_tb;
785 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000786 return p;
787}
788
Nicholas Piggin617326f2021-11-23 19:52:29 +1000789static void __kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
Nicholas Piggine4335f52022-09-08 23:25:45 +1000790 struct lppaca *vpa,
Nicholas Piggin617326f2021-11-23 19:52:29 +1000791 unsigned int pcpu, u64 now,
792 unsigned long stolen)
Paul Mackerras0456ec42012-02-03 00:56:21 +0000793{
794 struct dtl_entry *dt;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000795
796 dt = vcpu->arch.dtl_ptr;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000797
Nicholas Piggine4335f52022-09-08 23:25:45 +1000798 if (!dt)
Paul Mackerras0456ec42012-02-03 00:56:21 +0000799 return;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000800
Paul Mackerras0456ec42012-02-03 00:56:21 +0000801 dt->dispatch_reason = 7;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000802 dt->preempt_reason = 0;
803 dt->processor_id = cpu_to_be16(pcpu + vcpu->arch.ptid);
Alexander Graf02407552014-06-11 10:34:19 +0200804 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
Nicholas Piggin617326f2021-11-23 19:52:29 +1000805 dt->ready_to_enqueue_time = 0;
806 dt->waiting_to_ready_time = 0;
807 dt->timebase = cpu_to_be64(now);
808 dt->fault_addr = 0;
Alexander Graf02407552014-06-11 10:34:19 +0200809 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
810 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Nicholas Piggin617326f2021-11-23 19:52:29 +1000811
Paul Mackerras0456ec42012-02-03 00:56:21 +0000812 ++dt;
813 if (dt == vcpu->arch.dtl.pinned_end)
814 dt = vcpu->arch.dtl.pinned_addr;
815 vcpu->arch.dtl_ptr = dt;
816 /* order writing *dt vs. writing vpa->dtl_idx */
817 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200818 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Nicholas Piggine4335f52022-09-08 23:25:45 +1000819
820 /* vcpu->arch.dtl.dirty is set by the caller */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000821}
822
Nicholas Piggine4335f52022-09-08 23:25:45 +1000823static void kvmppc_update_vpa_dispatch(struct kvm_vcpu *vcpu,
824 struct kvmppc_vcore *vc)
Nicholas Piggin1a5486b2022-09-08 23:25:44 +1000825{
Nicholas Piggine4335f52022-09-08 23:25:45 +1000826 struct lppaca *vpa;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000827 unsigned long stolen;
828 unsigned long core_stolen;
829 u64 now;
830 unsigned long flags;
831
Nicholas Piggine4335f52022-09-08 23:25:45 +1000832 vpa = vcpu->arch.vpa.pinned_addr;
833 if (!vpa)
834 return;
835
Nicholas Piggin617326f2021-11-23 19:52:29 +1000836 now = mftb();
837
838 core_stolen = vcore_stolen_time(vc, now);
839 stolen = core_stolen - vcpu->arch.stolen_logged;
840 vcpu->arch.stolen_logged = core_stolen;
841 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
842 stolen += vcpu->arch.busy_stolen;
843 vcpu->arch.busy_stolen = 0;
844 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
845
Nicholas Piggine4335f52022-09-08 23:25:45 +1000846 vpa->enqueue_dispatch_tb = cpu_to_be64(be64_to_cpu(vpa->enqueue_dispatch_tb) + stolen);
847
Jordan Niethec8ae9b32023-09-14 13:05:54 +1000848 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now + kvmppc_get_tb_offset(vcpu), stolen);
Nicholas Piggine4335f52022-09-08 23:25:45 +1000849
850 vcpu->arch.vpa.dirty = true;
851}
852
853static void kvmppc_update_vpa_dispatch_p9(struct kvm_vcpu *vcpu,
854 struct kvmppc_vcore *vc,
855 u64 now)
856{
857 struct lppaca *vpa;
858 unsigned long stolen;
859 unsigned long stolen_delta;
860
861 vpa = vcpu->arch.vpa.pinned_addr;
862 if (!vpa)
863 return;
864
865 stolen = vc->stolen_tb;
866 stolen_delta = stolen - vcpu->arch.stolen_logged;
867 vcpu->arch.stolen_logged = stolen;
868
869 vpa->enqueue_dispatch_tb = cpu_to_be64(stolen);
870
871 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now, stolen_delta);
872
873 vcpu->arch.vpa.dirty = true;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000874}
875
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000876/* See if there is a doorbell interrupt pending for a vcpu */
877static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
878{
879 int thr;
880 struct kvmppc_vcore *vc;
881
Paul Mackerras57900692017-05-16 16:41:20 +1000882 if (vcpu->arch.doorbell_request)
883 return true;
Nicholas Piggin63983262021-11-23 19:52:30 +1000884 if (cpu_has_feature(CPU_FTR_ARCH_300))
885 return false;
Paul Mackerras57900692017-05-16 16:41:20 +1000886 /*
887 * Ensure that the read of vcore->dpdes comes after the read
888 * of vcpu->doorbell_request. This barrier matches the
Palmer Dabbelt6fabc9f2019-04-25 12:53:39 -0700889 * smp_wmb() in kvmppc_guest_entry_inject().
Paul Mackerras57900692017-05-16 16:41:20 +1000890 */
891 smp_rmb();
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000892 vc = vcpu->arch.vcore;
893 thr = vcpu->vcpu_id - vc->first_vcpuid;
894 return !!(vc->dpdes & (1 << thr));
895}
896
Michael Neuling96423822014-06-02 11:03:01 +1000897static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
898{
Jordan Niethec8ae9b32023-09-14 13:05:54 +1000899 if (kvmppc_get_arch_compat(vcpu) >= PVR_ARCH_207)
Michael Neuling96423822014-06-02 11:03:01 +1000900 return true;
Jordan Niethec8ae9b32023-09-14 13:05:54 +1000901 if ((!kvmppc_get_arch_compat(vcpu)) &&
Michael Neuling96423822014-06-02 11:03:01 +1000902 cpu_has_feature(CPU_FTR_ARCH_207S))
903 return true;
904 return false;
905}
906
907static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
908 unsigned long resource, unsigned long value1,
909 unsigned long value2)
910{
911 switch (resource) {
912 case H_SET_MODE_RESOURCE_SET_CIABR:
913 if (!kvmppc_power8_compatible(vcpu))
914 return H_P2;
915 if (value2)
916 return H_P4;
917 if (mflags)
918 return H_UNSUPPORTED_FLAG_START;
919 /* Guests can't breakpoint the hypervisor */
920 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
921 return H_P3;
Jordan Nietheebc88ea2023-09-14 13:05:55 +1000922 kvmppc_set_ciabr_hv(vcpu, value1);
Michael Neuling96423822014-06-02 11:03:01 +1000923 return H_SUCCESS;
Ravi Bangoria6f3fe292020-07-23 14:38:10 +0530924 case H_SET_MODE_RESOURCE_SET_DAWR0:
Michael Neuling96423822014-06-02 11:03:01 +1000925 if (!kvmppc_power8_compatible(vcpu))
926 return H_P2;
Michael Neuling398e7122018-03-27 15:37:20 +1100927 if (!ppc_breakpoint_available())
928 return H_P2;
Michael Neuling96423822014-06-02 11:03:01 +1000929 if (mflags)
930 return H_UNSUPPORTED_FLAG_START;
931 if (value2 & DABRX_HYP)
932 return H_P4;
Jordan Nietheebc88ea2023-09-14 13:05:55 +1000933 kvmppc_set_dawr0_hv(vcpu, value1);
934 kvmppc_set_dawrx0_hv(vcpu, value2);
Michael Neuling96423822014-06-02 11:03:01 +1000935 return H_SUCCESS;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +0530936 case H_SET_MODE_RESOURCE_SET_DAWR1:
937 if (!kvmppc_power8_compatible(vcpu))
938 return H_P2;
939 if (!ppc_breakpoint_available())
940 return H_P2;
941 if (!cpu_has_feature(CPU_FTR_DAWR1))
942 return H_P2;
943 if (!vcpu->kvm->arch.dawr1_enabled)
944 return H_FUNCTION;
945 if (mflags)
946 return H_UNSUPPORTED_FLAG_START;
947 if (value2 & DABRX_HYP)
948 return H_P4;
Jordan Nietheebc88ea2023-09-14 13:05:55 +1000949 kvmppc_set_dawr1_hv(vcpu, value1);
950 kvmppc_set_dawrx1_hv(vcpu, value2);
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +0530951 return H_SUCCESS;
Nicholas Piggin55d70042019-10-02 16:00:25 +1000952 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
Nicholas Pigginbcc92a02021-04-12 11:48:37 +1000953 /*
954 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
955 * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
956 */
Nicholas Piggin2e1ae9cd2021-05-28 19:07:41 +1000957 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
958 kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
Nicholas Piggin55d70042019-10-02 16:00:25 +1000959 return H_UNSUPPORTED_FLAG_START;
960 return H_TOO_HARD;
Michael Neuling96423822014-06-02 11:03:01 +1000961 default:
962 return H_TOO_HARD;
963 }
964}
965
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +1100966/* Copy guest memory in place - must reside within a single memslot */
967static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
968 unsigned long len)
969{
970 struct kvm_memory_slot *to_memslot = NULL;
971 struct kvm_memory_slot *from_memslot = NULL;
972 unsigned long to_addr, from_addr;
973 int r;
974
975 /* Get HPA for from address */
976 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
977 if (!from_memslot)
978 return -EFAULT;
979 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
980 << PAGE_SHIFT))
981 return -EINVAL;
982 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
983 if (kvm_is_error_hva(from_addr))
984 return -EFAULT;
985 from_addr |= (from & (PAGE_SIZE - 1));
986
987 /* Get HPA for to address */
988 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
989 if (!to_memslot)
990 return -EFAULT;
991 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
992 << PAGE_SHIFT))
993 return -EINVAL;
994 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
995 if (kvm_is_error_hva(to_addr))
996 return -EFAULT;
997 to_addr |= (to & (PAGE_SIZE - 1));
998
999 /* Perform copy */
1000 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
1001 len);
1002 if (r)
1003 return -EFAULT;
1004 mark_page_dirty(kvm, to >> PAGE_SHIFT);
1005 return 0;
1006}
1007
1008static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
1009 unsigned long dest, unsigned long src)
1010{
1011 u64 pg_sz = SZ_4K; /* 4K page size */
1012 u64 pg_mask = SZ_4K - 1;
1013 int ret;
1014
1015 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
1016 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
1017 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
1018 return H_PARAMETER;
1019
1020 /* dest (and src if copy_page flag set) must be page aligned */
1021 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
1022 return H_PARAMETER;
1023
1024 /* zero and/or copy the page as determined by the flags */
1025 if (flags & H_COPY_PAGE) {
1026 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
1027 if (ret < 0)
1028 return H_PARAMETER;
1029 } else if (flags & H_ZERO_PAGE) {
1030 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
1031 if (ret < 0)
1032 return H_PARAMETER;
1033 }
1034
1035 /* We can ignore the remaining flags */
1036
1037 return H_SUCCESS;
1038}
1039
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001040static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
1041{
1042 struct kvmppc_vcore *vcore = target->arch.vcore;
1043
1044 /*
1045 * We expect to have been called by the real mode handler
1046 * (kvmppc_rm_h_confer()) which would have directly returned
1047 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
1048 * have useful work to do and should not confer) so we don't
1049 * recheck that here.
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001050 *
1051 * In the case of the P9 single vcpu per vcore case, the real
1052 * mode handler is not called but no other threads are in the
1053 * source vcore.
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001054 */
Nicholas Pigginecb6a722021-11-23 19:52:28 +10001055 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
1056 spin_lock(&vcore->lock);
1057 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
1058 vcore->vcore_state != VCORE_INACTIVE &&
1059 vcore->runner)
1060 target = vcore->runner;
1061 spin_unlock(&vcore->lock);
1062 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001063
1064 return kvm_vcpu_yield_to(target);
1065}
1066
1067static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
1068{
1069 int yield_count = 0;
1070 struct lppaca *lppaca;
1071
1072 spin_lock(&vcpu->arch.vpa_update_lock);
1073 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
1074 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +11001075 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001076 spin_unlock(&vcpu->arch.vpa_update_lock);
1077 return yield_count;
1078}
1079
Bharata B Rao53324b52021-06-21 14:20:01 +05301080/*
1081 * H_RPT_INVALIDATE hcall handler for nested guests.
1082 *
1083 * Handles only nested process-scoped invalidation requests in L0.
1084 */
1085static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
1086{
1087 unsigned long type = kvmppc_get_gpr(vcpu, 6);
1088 unsigned long pid, pg_sizes, start, end;
1089
1090 /*
1091 * The partition-scoped invalidations aren't handled here in L0.
1092 */
1093 if (type & H_RPTI_TYPE_NESTED)
1094 return RESUME_HOST;
1095
1096 pid = kvmppc_get_gpr(vcpu, 4);
1097 pg_sizes = kvmppc_get_gpr(vcpu, 7);
1098 start = kvmppc_get_gpr(vcpu, 8);
1099 end = kvmppc_get_gpr(vcpu, 9);
1100
1101 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
1102 type, pg_sizes, start, end);
1103
1104 kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
1105 return RESUME_GUEST;
1106}
1107
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301108static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
1109 unsigned long id, unsigned long target,
1110 unsigned long type, unsigned long pg_sizes,
1111 unsigned long start, unsigned long end)
1112{
1113 if (!kvm_is_radix(vcpu->kvm))
1114 return H_UNSUPPORTED;
1115
1116 if (end < start)
1117 return H_P5;
1118
1119 /*
1120 * Partition-scoped invalidation for nested guests.
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301121 */
Bharata B Rao53324b52021-06-21 14:20:01 +05301122 if (type & H_RPTI_TYPE_NESTED) {
1123 if (!nesting_enabled(vcpu->kvm))
1124 return H_FUNCTION;
1125
1126 /* Support only cores as target */
1127 if (target != H_RPTI_TARGET_CMMU)
1128 return H_P2;
1129
1130 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
1131 start, end);
1132 }
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301133
1134 /*
1135 * Process-scoped invalidation for L1 guests.
1136 */
1137 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
1138 type, pg_sizes, start, end);
1139 return H_SUCCESS;
1140}
1141
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001142int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
1143{
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001144 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001145 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1146 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001147 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001148 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001149 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001150
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001151 if (req <= MAX_HCALL_OPCODE &&
1152 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
1153 return RESUME_HOST;
1154
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001155 switch (req) {
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001156 case H_REMOVE:
1157 ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4),
1158 kvmppc_get_gpr(vcpu, 5),
1159 kvmppc_get_gpr(vcpu, 6));
1160 if (ret == H_TOO_HARD)
1161 return RESUME_HOST;
1162 break;
1163 case H_ENTER:
1164 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
1165 kvmppc_get_gpr(vcpu, 5),
1166 kvmppc_get_gpr(vcpu, 6),
1167 kvmppc_get_gpr(vcpu, 7));
1168 if (ret == H_TOO_HARD)
1169 return RESUME_HOST;
1170 break;
1171 case H_READ:
1172 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
1173 kvmppc_get_gpr(vcpu, 5));
1174 if (ret == H_TOO_HARD)
1175 return RESUME_HOST;
1176 break;
1177 case H_CLEAR_MOD:
1178 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
1179 kvmppc_get_gpr(vcpu, 5));
1180 if (ret == H_TOO_HARD)
1181 return RESUME_HOST;
1182 break;
1183 case H_CLEAR_REF:
1184 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
1185 kvmppc_get_gpr(vcpu, 5));
1186 if (ret == H_TOO_HARD)
1187 return RESUME_HOST;
1188 break;
1189 case H_PROTECT:
1190 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
1191 kvmppc_get_gpr(vcpu, 5),
1192 kvmppc_get_gpr(vcpu, 6));
1193 if (ret == H_TOO_HARD)
1194 return RESUME_HOST;
1195 break;
1196 case H_BULK_REMOVE:
1197 ret = kvmppc_h_bulk_remove(vcpu);
1198 if (ret == H_TOO_HARD)
1199 return RESUME_HOST;
1200 break;
1201
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001202 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001203 break;
1204 case H_PROD:
1205 target = kvmppc_get_gpr(vcpu, 4);
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001206 tvcpu = kvmppc_find_vcpu(kvm, target);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001207 if (!tvcpu) {
1208 ret = H_PARAMETER;
1209 break;
1210 }
1211 tvcpu->arch.prodded = 1;
Nicholas Pigginc7fa8482022-03-03 15:33:10 +10001212 smp_mb(); /* This orders prodded store vs ceded load */
Paul Mackerras8464c882016-12-06 20:42:05 +11001213 if (tvcpu->arch.ceded)
1214 kvmppc_fast_vcpu_kick_hv(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001215 break;
1216 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +10001217 target = kvmppc_get_gpr(vcpu, 4);
1218 if (target == -1)
1219 break;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001220 tvcpu = kvmppc_find_vcpu(kvm, target);
Paul Mackerras42d76042013-09-06 13:23:21 +10001221 if (!tvcpu) {
1222 ret = H_PARAMETER;
1223 break;
1224 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001225 yield_count = kvmppc_get_gpr(vcpu, 5);
1226 if (kvmppc_get_yield_count(tvcpu) != yield_count)
1227 break;
1228 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001229 break;
1230 case H_REGISTER_VPA:
1231 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
1232 kvmppc_get_gpr(vcpu, 5),
1233 kvmppc_get_gpr(vcpu, 6));
1234 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001235 case H_RTAS:
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001236 if (list_empty(&kvm->arch.rtas_tokens))
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001237 return RESUME_HOST;
1238
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001239 idx = srcu_read_lock(&kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001240 rc = kvmppc_rtas_hcall(vcpu);
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001241 srcu_read_unlock(&kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001242
1243 if (rc == -ENOENT)
1244 return RESUME_HOST;
1245 else if (rc == 0)
1246 break;
1247
1248 /* Send the error out to userspace via KVM_RUN */
1249 return rc;
David Gibson99342cf82015-02-05 11:53:25 +11001250 case H_LOGICAL_CI_LOAD:
1251 ret = kvmppc_h_logical_ci_load(vcpu);
1252 if (ret == H_TOO_HARD)
1253 return RESUME_HOST;
1254 break;
1255 case H_LOGICAL_CI_STORE:
1256 ret = kvmppc_h_logical_ci_store(vcpu);
1257 if (ret == H_TOO_HARD)
1258 return RESUME_HOST;
1259 break;
Michael Neuling96423822014-06-02 11:03:01 +10001260 case H_SET_MODE:
1261 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
1262 kvmppc_get_gpr(vcpu, 5),
1263 kvmppc_get_gpr(vcpu, 6),
1264 kvmppc_get_gpr(vcpu, 7));
1265 if (ret == H_TOO_HARD)
1266 return RESUME_HOST;
1267 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001268 case H_XIRR:
1269 case H_CPPR:
1270 case H_EOI:
1271 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +00001272 case H_IPOLL:
1273 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001274 if (kvmppc_xics_enabled(vcpu)) {
Paul Mackerras03f95332019-02-04 22:07:20 +11001275 if (xics_on_xive()) {
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001276 ret = H_NOT_AVAILABLE;
1277 return RESUME_GUEST;
1278 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001279 ret = kvmppc_xics_hcall(vcpu, req);
1280 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11001281 }
1282 return RESUME_HOST;
Paul Mackerras4bad7772018-10-08 16:31:06 +11001283 case H_SET_DABR:
1284 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
1285 break;
1286 case H_SET_XDABR:
1287 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
1288 kvmppc_get_gpr(vcpu, 5));
1289 break;
Jordan Niethee40542a2019-02-21 14:28:48 +11001290#ifdef CONFIG_SPAPR_TCE_IOMMU
Paul Mackerras4bad7772018-10-08 16:31:06 +11001291 case H_GET_TCE:
1292 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1293 kvmppc_get_gpr(vcpu, 5));
1294 if (ret == H_TOO_HARD)
1295 return RESUME_HOST;
1296 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11001297 case H_PUT_TCE:
1298 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1299 kvmppc_get_gpr(vcpu, 5),
1300 kvmppc_get_gpr(vcpu, 6));
1301 if (ret == H_TOO_HARD)
1302 return RESUME_HOST;
1303 break;
1304 case H_PUT_TCE_INDIRECT:
1305 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
1306 kvmppc_get_gpr(vcpu, 5),
1307 kvmppc_get_gpr(vcpu, 6),
1308 kvmppc_get_gpr(vcpu, 7));
1309 if (ret == H_TOO_HARD)
1310 return RESUME_HOST;
1311 break;
1312 case H_STUFF_TCE:
1313 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1314 kvmppc_get_gpr(vcpu, 5),
1315 kvmppc_get_gpr(vcpu, 6),
1316 kvmppc_get_gpr(vcpu, 7));
1317 if (ret == H_TOO_HARD)
1318 return RESUME_HOST;
1319 break;
Jordan Niethee40542a2019-02-21 14:28:48 +11001320#endif
Jordan Niethe0e85b7d2023-09-14 13:05:50 +10001321 case H_RANDOM: {
1322 unsigned long rand;
1323
1324 if (!arch_get_random_seed_longs(&rand, 1))
Paul Mackerras4bad7772018-10-08 16:31:06 +11001325 ret = H_HARDWARE;
Jordan Niethe0e85b7d2023-09-14 13:05:50 +10001326 kvmppc_set_gpr(vcpu, 4, rand);
Paul Mackerras4bad7772018-10-08 16:31:06 +11001327 break;
Jordan Niethe0e85b7d2023-09-14 13:05:50 +10001328 }
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301329 case H_RPT_INVALIDATE:
1330 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
1331 kvmppc_get_gpr(vcpu, 5),
1332 kvmppc_get_gpr(vcpu, 6),
1333 kvmppc_get_gpr(vcpu, 7),
1334 kvmppc_get_gpr(vcpu, 8),
1335 kvmppc_get_gpr(vcpu, 9));
1336 break;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001337
1338 case H_SET_PARTITION_TABLE:
1339 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001340 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001341 ret = kvmhv_set_partition_table(vcpu);
1342 break;
1343 case H_ENTER_NESTED:
1344 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001345 if (!nesting_enabled(kvm))
Paul Mackerras360cae32018-10-08 16:31:04 +11001346 break;
1347 ret = kvmhv_enter_nested_guest(vcpu);
1348 if (ret == H_INTERRUPT) {
1349 kvmppc_set_gpr(vcpu, 3, 0);
Michael Roth6c08ec12018-11-08 21:27:23 -06001350 vcpu->arch.hcall_needed = 0;
Paul Mackerras360cae32018-10-08 16:31:04 +11001351 return -EINTR;
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001352 } else if (ret == H_TOO_HARD) {
1353 kvmppc_set_gpr(vcpu, 3, 0);
1354 vcpu->arch.hcall_needed = 0;
1355 return RESUME_HOST;
Paul Mackerras360cae32018-10-08 16:31:04 +11001356 }
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001357 break;
1358 case H_TLB_INVALIDATE:
1359 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001360 if (nesting_enabled(kvm))
Paul Mackerrasaa069a92018-09-21 20:02:01 +10001361 ret = kvmhv_do_nested_tlbie(vcpu);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001362 break;
Suraj Jitindar Singh6ff887b2018-12-14 16:29:09 +11001363 case H_COPY_TOFROM_GUEST:
1364 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001365 if (nesting_enabled(kvm))
Suraj Jitindar Singh6ff887b2018-12-14 16:29:09 +11001366 ret = kvmhv_copy_tofrom_guest_nested(vcpu);
1367 break;
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +11001368 case H_PAGE_INIT:
1369 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
1370 kvmppc_get_gpr(vcpu, 5),
1371 kvmppc_get_gpr(vcpu, 6));
1372 break;
Bharata B Raoca9f4942019-11-25 08:36:26 +05301373 case H_SVM_PAGE_IN:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001374 ret = H_UNSUPPORTED;
1375 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001376 ret = kvmppc_h_svm_page_in(kvm,
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001377 kvmppc_get_gpr(vcpu, 4),
1378 kvmppc_get_gpr(vcpu, 5),
1379 kvmppc_get_gpr(vcpu, 6));
Bharata B Raoca9f4942019-11-25 08:36:26 +05301380 break;
1381 case H_SVM_PAGE_OUT:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001382 ret = H_UNSUPPORTED;
1383 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001384 ret = kvmppc_h_svm_page_out(kvm,
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001385 kvmppc_get_gpr(vcpu, 4),
1386 kvmppc_get_gpr(vcpu, 5),
1387 kvmppc_get_gpr(vcpu, 6));
Bharata B Raoca9f4942019-11-25 08:36:26 +05301388 break;
1389 case H_SVM_INIT_START:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001390 ret = H_UNSUPPORTED;
1391 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001392 ret = kvmppc_h_svm_init_start(kvm);
Bharata B Raoca9f4942019-11-25 08:36:26 +05301393 break;
1394 case H_SVM_INIT_DONE:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001395 ret = H_UNSUPPORTED;
1396 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001397 ret = kvmppc_h_svm_init_done(kvm);
Bharata B Raoca9f4942019-11-25 08:36:26 +05301398 break;
Sukadev Bhattiprolu3a439702020-01-06 18:02:37 -08001399 case H_SVM_INIT_ABORT:
Laurent Dufoure3326ae2020-05-20 19:43:08 +02001400 /*
1401 * Even if that call is made by the Ultravisor, the SSR1 value
1402 * is the guest context one, with the secure bit clear as it has
1403 * not yet been secured. So we can't check it here.
1404 * Instead the kvm->arch.secure_guest flag is checked inside
1405 * kvmppc_h_svm_init_abort().
1406 */
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001407 ret = kvmppc_h_svm_init_abort(kvm);
Sukadev Bhattiprolu3a439702020-01-06 18:02:37 -08001408 break;
Bharata B Raoca9f4942019-11-25 08:36:26 +05301409
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001410 default:
1411 return RESUME_HOST;
1412 }
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001413 WARN_ON_ONCE(ret == H_TOO_HARD);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001414 kvmppc_set_gpr(vcpu, 3, ret);
1415 vcpu->arch.hcall_needed = 0;
1416 return RESUME_GUEST;
1417}
1418
Paul Mackerras4bad7772018-10-08 16:31:06 +11001419/*
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001420 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
1421 * handlers in book3s_hv_rmhandlers.S.
1422 *
Paul Mackerras4bad7772018-10-08 16:31:06 +11001423 * This has to be done early, not in kvmppc_pseries_do_hcall(), so
1424 * that the cede logic in kvmppc_run_single_vcpu() works properly.
1425 */
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001426static void kvmppc_cede(struct kvm_vcpu *vcpu)
Paul Mackerras4bad7772018-10-08 16:31:06 +11001427{
Jordan Niethe6de2e832023-09-14 13:05:56 +10001428 __kvmppc_set_msr_hv(vcpu, __kvmppc_get_msr_hv(vcpu) | MSR_EE);
Paul Mackerras4bad7772018-10-08 16:31:06 +11001429 vcpu->arch.ceded = 1;
1430 smp_mb();
1431 if (vcpu->arch.prodded) {
1432 vcpu->arch.prodded = 0;
1433 smp_mb();
1434 vcpu->arch.ceded = 0;
1435 }
1436}
1437
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001438static int kvmppc_hcall_impl_hv(unsigned long cmd)
1439{
1440 switch (cmd) {
1441 case H_CEDE:
1442 case H_PROD:
1443 case H_CONFER:
1444 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +10001445 case H_SET_MODE:
Alexey Kardashevskiycad32d92022-05-06 15:37:55 +10001446#ifdef CONFIG_SPAPR_TCE_IOMMU
1447 case H_GET_TCE:
1448 case H_PUT_TCE:
1449 case H_PUT_TCE_INDIRECT:
1450 case H_STUFF_TCE:
1451#endif
David Gibson99342cf82015-02-05 11:53:25 +11001452 case H_LOGICAL_CI_LOAD:
1453 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001454#ifdef CONFIG_KVM_XICS
1455 case H_XIRR:
1456 case H_CPPR:
1457 case H_EOI:
1458 case H_IPI:
1459 case H_IPOLL:
1460 case H_XIRR_X:
1461#endif
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +11001462 case H_PAGE_INIT:
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301463 case H_RPT_INVALIDATE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001464 return 1;
1465 }
1466
1467 /* See if it's in the real-mode table */
1468 return kvmppc_hcall_impl_hv_realmode(cmd);
1469}
1470
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001471static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301472{
Paul Mackerrasacf17872023-03-08 17:34:48 +11001473 ppc_inst_t last_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301474
1475 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
1476 EMULATE_DONE) {
1477 /*
1478 * Fetch failed, so return to guest and
1479 * try executing it again.
1480 */
1481 return RESUME_GUEST;
1482 }
1483
Paul Mackerrasacf17872023-03-08 17:34:48 +11001484 if (ppc_inst_val(last_inst) == KVMPPC_INST_SW_BREAKPOINT) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001485 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
1486 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301487 return RESUME_HOST;
1488 } else {
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001489 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1490 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301491 return RESUME_GUEST;
1492 }
1493}
1494
Paul Mackerras57900692017-05-16 16:41:20 +10001495static void do_nothing(void *x)
1496{
1497}
1498
1499static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
1500{
1501 int thr, cpu, pcpu, nthreads;
1502 struct kvm_vcpu *v;
1503 unsigned long dpdes;
1504
1505 nthreads = vcpu->kvm->arch.emul_smt_mode;
1506 dpdes = 0;
1507 cpu = vcpu->vcpu_id & ~(nthreads - 1);
1508 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
1509 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
1510 if (!v)
1511 continue;
1512 /*
1513 * If the vcpu is currently running on a physical cpu thread,
1514 * interrupt it in order to pull it out of the guest briefly,
1515 * which will update its vcore->dpdes value.
1516 */
1517 pcpu = READ_ONCE(v->cpu);
1518 if (pcpu >= 0)
1519 smp_call_function_single(pcpu, do_nothing, NULL, 1);
1520 if (kvmppc_doorbell_pending(v))
1521 dpdes |= 1 << thr;
1522 }
1523 return dpdes;
1524}
1525
1526/*
1527 * On POWER9, emulate doorbell-related instructions in order to
1528 * give the guest the illusion of running on a multi-threaded core.
1529 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
1530 * and mfspr DPDES.
1531 */
1532static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1533{
1534 u32 inst, rb, thr;
1535 unsigned long arg;
1536 struct kvm *kvm = vcpu->kvm;
1537 struct kvm_vcpu *tvcpu;
Paul Mackerrasacf17872023-03-08 17:34:48 +11001538 ppc_inst_t pinst;
Paul Mackerras57900692017-05-16 16:41:20 +10001539
Paul Mackerrasacf17872023-03-08 17:34:48 +11001540 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst) != EMULATE_DONE)
Paul Mackerras57900692017-05-16 16:41:20 +10001541 return RESUME_GUEST;
Paul Mackerrasacf17872023-03-08 17:34:48 +11001542 inst = ppc_inst_val(pinst);
Paul Mackerras57900692017-05-16 16:41:20 +10001543 if (get_op(inst) != 31)
1544 return EMULATE_FAIL;
1545 rb = get_rb(inst);
1546 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1547 switch (get_xop(inst)) {
1548 case OP_31_XOP_MSGSNDP:
1549 arg = kvmppc_get_gpr(vcpu, rb);
Leonardo Bras87fb4972020-12-08 18:57:08 -03001550 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
Paul Mackerras57900692017-05-16 16:41:20 +10001551 break;
Leonardo Bras87fb4972020-12-08 18:57:08 -03001552 arg &= 0x7f;
Paul Mackerras57900692017-05-16 16:41:20 +10001553 if (arg >= kvm->arch.emul_smt_mode)
1554 break;
1555 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1556 if (!tvcpu)
1557 break;
1558 if (!tvcpu->arch.doorbell_request) {
1559 tvcpu->arch.doorbell_request = 1;
1560 kvmppc_fast_vcpu_kick_hv(tvcpu);
1561 }
1562 break;
1563 case OP_31_XOP_MSGCLRP:
1564 arg = kvmppc_get_gpr(vcpu, rb);
Leonardo Bras87fb4972020-12-08 18:57:08 -03001565 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
Paul Mackerras57900692017-05-16 16:41:20 +10001566 break;
1567 vcpu->arch.vcore->dpdes = 0;
1568 vcpu->arch.doorbell_request = 0;
1569 break;
1570 case OP_31_XOP_MFSPR:
1571 switch (get_sprn(inst)) {
1572 case SPRN_TIR:
1573 arg = thr;
1574 break;
1575 case SPRN_DPDES:
1576 arg = kvmppc_read_dpdes(vcpu);
1577 break;
1578 default:
1579 return EMULATE_FAIL;
1580 }
1581 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1582 break;
1583 default:
1584 return EMULATE_FAIL;
1585 }
1586 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1587 return RESUME_GUEST;
1588}
1589
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001590/*
1591 * If the lppaca had pmcregs_in_use clear when we exited the guest, then
1592 * HFSCR_PM is cleared for next entry. If the guest then tries to access
1593 * the PMU SPRs, we get this facility unavailable interrupt. Putting HFSCR_PM
1594 * back in the guest HFSCR will cause the next entry to load the PMU SPRs and
1595 * allow the guest access to continue.
1596 */
1597static int kvmppc_pmu_unavailable(struct kvm_vcpu *vcpu)
1598{
1599 if (!(vcpu->arch.hfscr_permitted & HFSCR_PM))
1600 return EMULATE_FAIL;
1601
Jordan Nietheebc88ea2023-09-14 13:05:55 +10001602 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PM);
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001603
1604 return RESUME_GUEST;
1605}
1606
Nicholas Piggina3e18ca2021-11-23 19:52:14 +10001607static int kvmppc_ebb_unavailable(struct kvm_vcpu *vcpu)
1608{
1609 if (!(vcpu->arch.hfscr_permitted & HFSCR_EBB))
1610 return EMULATE_FAIL;
1611
Jordan Nietheebc88ea2023-09-14 13:05:55 +10001612 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_EBB);
Nicholas Piggina3e18ca2021-11-23 19:52:14 +10001613
1614 return RESUME_GUEST;
1615}
1616
Nicholas Piggin022ecb92021-11-23 19:52:15 +10001617static int kvmppc_tm_unavailable(struct kvm_vcpu *vcpu)
1618{
1619 if (!(vcpu->arch.hfscr_permitted & HFSCR_TM))
1620 return EMULATE_FAIL;
1621
Jordan Nietheebc88ea2023-09-14 13:05:55 +10001622 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
Nicholas Piggin022ecb92021-11-23 19:52:15 +10001623
1624 return RESUME_GUEST;
1625}
1626
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001627static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301628 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001629{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001630 struct kvm_run *run = vcpu->run;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001631 int r = RESUME_HOST;
1632
1633 vcpu->stat.sum_exits++;
1634
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11001635 /*
1636 * This can happen if an interrupt occurs in the last stages
1637 * of guest entry or the first stages of guest exit (i.e. after
1638 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1639 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1640 * That can happen due to a bug, or due to a machine check
1641 * occurring at just the wrong time.
1642 */
Jordan Niethea9a3de52023-12-01 18:56:13 +05301643 if (!kvmhv_is_nestedv2() && (__kvmppc_get_msr_hv(vcpu) & MSR_HV)) {
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11001644 printk(KERN_EMERG "KVM trap in HV mode!\n");
1645 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1646 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1647 vcpu->arch.shregs.msr);
1648 kvmppc_dump_regs(vcpu);
1649 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1650 run->hw.hardware_exit_reason = vcpu->arch.trap;
1651 return RESUME_HOST;
1652 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001653 run->exit_reason = KVM_EXIT_UNKNOWN;
1654 run->ready_for_interrupt_injection = 1;
1655 switch (vcpu->arch.trap) {
1656 /* We're good on these - the host merely wanted to get our attention */
Nicholas Piggin46dea772021-11-23 19:52:22 +10001657 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
1658 WARN_ON_ONCE(1); /* Should never happen */
1659 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
1660 fallthrough;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001661 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1662 vcpu->stat.dec_exits++;
1663 r = RESUME_GUEST;
1664 break;
1665 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +11001666 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerras84f71392016-11-22 14:30:14 +11001667 case BOOK3S_INTERRUPT_H_VIRT:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001668 vcpu->stat.ext_intr_exits++;
1669 r = RESUME_GUEST;
1670 break;
Nicholas Piggin6de66382017-11-05 23:33:55 +11001671 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +11001672 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001673 case BOOK3S_INTERRUPT_PERFMON:
Nicholas Piggin6de66382017-11-05 23:33:55 +11001674 case BOOK3S_INTERRUPT_SYSTEM_RESET:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001675 r = RESUME_GUEST;
1676 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001677 case BOOK3S_INTERRUPT_MACHINE_CHECK: {
1678 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1679 DEFAULT_RATELIMIT_BURST);
1680 /*
1681 * Print the MCE event to host console. Ratelimit so the guest
1682 * can't flood the host log.
1683 */
1684 if (__ratelimit(&rs))
1685 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
Paul Mackerras884dfb72019-02-21 13:38:49 +11001686
1687 /*
1688 * If the guest can do FWNMI, exit to userspace so it can
1689 * deliver a FWNMI to the guest.
1690 * Otherwise we synthesize a machine check for the guest
1691 * so that it knows that the machine check occurred.
1692 */
1693 if (!vcpu->kvm->arch.fwnmi_enabled) {
Jordan Niethe6de2e832023-09-14 13:05:56 +10001694 ulong flags = (__kvmppc_get_msr_hv(vcpu) & 0x083c0000) |
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001695 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
Paul Mackerras884dfb72019-02-21 13:38:49 +11001696 kvmppc_core_queue_machine_check(vcpu, flags);
1697 r = RESUME_GUEST;
1698 break;
1699 }
1700
Aravinda Prasade20bbd32017-05-11 16:33:37 +05301701 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1702 run->exit_reason = KVM_EXIT_NMI;
1703 run->hw.hardware_exit_reason = vcpu->arch.trap;
1704 /* Clear out the old NMI status from run->flags */
1705 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1706 /* Now set the NMI status */
1707 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1708 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1709 else
1710 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1711
1712 r = RESUME_HOST;
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001713 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001714 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001715 case BOOK3S_INTERRUPT_PROGRAM:
1716 {
1717 ulong flags;
1718 /*
1719 * Normally program interrupts are delivered directly
1720 * to the guest by the hardware, but we can get here
1721 * as a result of a hypervisor emulation interrupt
1722 * (e40) getting turned into a 700 by BML RTAS.
1723 */
Jordan Niethe6de2e832023-09-14 13:05:56 +10001724 flags = (__kvmppc_get_msr_hv(vcpu) & 0x1f0000ull) |
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001725 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001726 kvmppc_core_queue_program(vcpu, flags);
1727 r = RESUME_GUEST;
1728 break;
1729 }
1730 case BOOK3S_INTERRUPT_SYSCALL:
1731 {
Paul Mackerrasde56a942011-06-29 00:21:34 +00001732 int i;
1733
Jordan Niethe63ccae72023-12-01 18:56:08 +05301734 if (!kvmhv_is_nestedv2() && unlikely(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001735 /*
1736 * Guest userspace executed sc 1. This can only be
1737 * reached by the P9 path because the old path
1738 * handles this case in realmode hcall handlers.
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001739 */
Nicholas Pigginac3c8b42021-05-28 19:07:49 +10001740 if (!kvmhv_vcpu_is_radix(vcpu)) {
1741 /*
1742 * A guest could be running PR KVM, so this
1743 * may be a PR KVM hcall. It must be reflected
1744 * to the guest kernel as a sc interrupt.
1745 */
1746 kvmppc_core_queue_syscall(vcpu);
1747 } else {
1748 /*
1749 * Radix guests can not run PR KVM or nested HV
1750 * hash guests which might run PR KVM, so this
1751 * is always a privilege fault. Send a program
1752 * check to guest kernel.
1753 */
1754 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
1755 }
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001756 r = RESUME_GUEST;
1757 break;
1758 }
Liu Ping Fan27025a62013-11-19 14:12:48 +08001759
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001760 /*
1761 * hcall - gather args and set exit_reason. This will next be
1762 * handled by kvmppc_pseries_do_hcall which may be able to deal
1763 * with it and resume guest, or may punt to userspace.
1764 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001765 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1766 for (i = 0; i < 9; ++i)
1767 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1768 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1769 vcpu->arch.hcall_needed = 1;
1770 r = RESUME_HOST;
1771 break;
1772 }
1773 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001774 * We get these next two if the guest accesses a page which it thinks
1775 * it has mapped but which is not actually present, either because
1776 * it is for an emulated I/O device or because the corresonding
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001777 * host page has been paged out.
1778 *
1779 * Any other HDSI/HISI interrupts have been handled already for P7/8
1780 * guests. For POWER9 hash guests not using rmhandlers, basic hash
1781 * fault handling is done here.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001782 */
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001783 case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
1784 unsigned long vsid;
1785 long err;
1786
Nicholas Piggind55b1ec2021-11-23 19:52:12 +10001787 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
1788 unlikely(vcpu->arch.fault_dsisr == HDSISR_CANARY)) {
Nicholas Piggin89d35b22021-05-28 19:07:34 +10001789 r = RESUME_GUEST; /* Just retry if it's the canary */
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001790 break;
1791 }
1792
1793 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1794 /*
1795 * Radix doesn't require anything, and pre-ISAv3.0 hash
1796 * already attempted to handle this in rmhandlers. The
1797 * hash fault handling below is v3 only (it uses ASDR
1798 * via fault_gpa).
1799 */
1800 r = RESUME_PAGE_FAULT;
1801 break;
1802 }
1803
1804 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
1805 kvmppc_core_queue_data_storage(vcpu,
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001806 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001807 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1808 r = RESUME_GUEST;
1809 break;
1810 }
1811
Jordan Niethe6de2e832023-09-14 13:05:56 +10001812 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_DR))
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001813 vsid = vcpu->kvm->arch.vrma_slb_v;
1814 else
1815 vsid = vcpu->arch.fault_gpa;
1816
1817 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1818 vsid, vcpu->arch.fault_dsisr, true);
1819 if (err == 0) {
1820 r = RESUME_GUEST;
1821 } else if (err == -1 || err == -2) {
1822 r = RESUME_PAGE_FAULT;
1823 } else {
1824 kvmppc_core_queue_data_storage(vcpu,
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001825 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001826 vcpu->arch.fault_dar, err);
1827 r = RESUME_GUEST;
1828 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001829 break;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001830 }
1831 case BOOK3S_INTERRUPT_H_INST_STORAGE: {
1832 unsigned long vsid;
1833 long err;
1834
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001835 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
Jordan Niethe6de2e832023-09-14 13:05:56 +10001836 vcpu->arch.fault_dsisr = __kvmppc_get_msr_hv(vcpu) &
Paul Mackerras32eb1502018-10-08 16:30:56 +11001837 DSISR_SRR1_MATCH_64S;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001838 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1839 /*
1840 * Radix doesn't require anything, and pre-ISAv3.0 hash
1841 * already attempted to handle this in rmhandlers. The
1842 * hash fault handling below is v3 only (it uses ASDR
1843 * via fault_gpa).
1844 */
Jordan Niethe6de2e832023-09-14 13:05:56 +10001845 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001846 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1847 r = RESUME_PAGE_FAULT;
1848 break;
1849 }
1850
1851 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
1852 kvmppc_core_queue_inst_storage(vcpu,
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001853 vcpu->arch.fault_dsisr |
1854 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001855 r = RESUME_GUEST;
1856 break;
1857 }
1858
Jordan Niethe6de2e832023-09-14 13:05:56 +10001859 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_IR))
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001860 vsid = vcpu->kvm->arch.vrma_slb_v;
1861 else
1862 vsid = vcpu->arch.fault_gpa;
1863
1864 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1865 vsid, vcpu->arch.fault_dsisr, false);
1866 if (err == 0) {
1867 r = RESUME_GUEST;
1868 } else if (err == -1) {
1869 r = RESUME_PAGE_FAULT;
1870 } else {
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001871 kvmppc_core_queue_inst_storage(vcpu,
1872 err | (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001873 r = RESUME_GUEST;
1874 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001875 break;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001876 }
1877
Paul Mackerrasde56a942011-06-29 00:21:34 +00001878 /*
1879 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301880 * If the guest debug is disabled, generate a program interrupt
1881 * to the guest. If guest debug is enabled, we need to check
1882 * whether the instruction is a software breakpoint instruction.
1883 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001884 */
1885 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001886 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1887 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1888 swab32(vcpu->arch.emul_inst) :
1889 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301890 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001891 r = kvmppc_emulate_debug_inst(vcpu);
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301892 } else {
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001893 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1894 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301895 r = RESUME_GUEST;
1896 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001897 break;
Nicholas Piggind82b3922021-08-12 02:00:38 +10001898
1899#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1900 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1901 /*
1902 * This occurs for various TM-related instructions that
1903 * we need to emulate on POWER9 DD2.2. We have already
1904 * handled the cases where the guest was in real-suspend
1905 * mode and was transitioning to transactional state.
1906 */
1907 r = kvmhv_p9_tm_emulation(vcpu);
1908 if (r != -1)
1909 break;
1910 fallthrough; /* go to facility unavailable handler */
1911#endif
1912
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001913 /*
1914 * This occurs if the guest (kernel or userspace), does something that
Paul Mackerras57900692017-05-16 16:41:20 +10001915 * is prohibited by HFSCR.
1916 * On POWER9, this could be a doorbell instruction that we need
1917 * to emulate.
1918 * Otherwise, we just generate a program interrupt to the guest.
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001919 */
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001920 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
Jordan Nietheebc88ea2023-09-14 13:05:55 +10001921 u64 cause = kvmppc_get_hfscr_hv(vcpu) >> 56;
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001922
Paul Mackerras57900692017-05-16 16:41:20 +10001923 r = EMULATE_FAIL;
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001924 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Gautam Menghani9739ff42024-07-16 17:22:04 +05301925 switch (cause) {
1926 case FSCR_MSGP_LG:
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001927 r = kvmppc_emulate_doorbell_instr(vcpu);
Gautam Menghani9739ff42024-07-16 17:22:04 +05301928 break;
1929 case FSCR_PM_LG:
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001930 r = kvmppc_pmu_unavailable(vcpu);
Gautam Menghani9739ff42024-07-16 17:22:04 +05301931 break;
1932 case FSCR_EBB_LG:
Nicholas Piggina3e18ca2021-11-23 19:52:14 +10001933 r = kvmppc_ebb_unavailable(vcpu);
Gautam Menghani9739ff42024-07-16 17:22:04 +05301934 break;
1935 case FSCR_TM_LG:
Nicholas Piggin022ecb92021-11-23 19:52:15 +10001936 r = kvmppc_tm_unavailable(vcpu);
Gautam Menghani9739ff42024-07-16 17:22:04 +05301937 break;
1938 default:
1939 break;
1940 }
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001941 }
Paul Mackerras57900692017-05-16 16:41:20 +10001942 if (r == EMULATE_FAIL) {
Nicholas Piggin6cd5c1d2023-03-30 20:32:24 +10001943 kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
1944 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED));
Paul Mackerras57900692017-05-16 16:41:20 +10001945 r = RESUME_GUEST;
1946 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001947 break;
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001948 }
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001949
Suresh Warrierf7af5202016-08-19 15:35:52 +10001950 case BOOK3S_INTERRUPT_HV_RM_HARD:
1951 r = RESUME_PASSTHROUGH;
1952 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001953 default:
1954 kvmppc_dump_regs(vcpu);
1955 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1956 vcpu->arch.trap, kvmppc_get_pc(vcpu),
Jordan Niethe6de2e832023-09-14 13:05:56 +10001957 __kvmppc_get_msr_hv(vcpu));
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001958 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001959 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001960 break;
1961 }
1962
Paul Mackerrasde56a942011-06-29 00:21:34 +00001963 return r;
1964}
1965
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001966static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
Paul Mackerras360cae32018-10-08 16:31:04 +11001967{
1968 int r;
1969 int srcu_idx;
1970
1971 vcpu->stat.sum_exits++;
1972
1973 /*
1974 * This can happen if an interrupt occurs in the last stages
1975 * of guest entry or the first stages of guest exit (i.e. after
1976 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1977 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1978 * That can happen due to a bug, or due to a machine check
1979 * occurring at just the wrong time.
1980 */
Jordan Niethe6de2e832023-09-14 13:05:56 +10001981 if (__kvmppc_get_msr_hv(vcpu) & MSR_HV) {
Paul Mackerras360cae32018-10-08 16:31:04 +11001982 pr_emerg("KVM trap in HV mode while nested!\n");
1983 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1984 vcpu->arch.trap, kvmppc_get_pc(vcpu),
Jordan Niethe6de2e832023-09-14 13:05:56 +10001985 __kvmppc_get_msr_hv(vcpu));
Paul Mackerras360cae32018-10-08 16:31:04 +11001986 kvmppc_dump_regs(vcpu);
1987 return RESUME_HOST;
1988 }
1989 switch (vcpu->arch.trap) {
1990 /* We're good on these - the host merely wanted to get our attention */
1991 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1992 vcpu->stat.dec_exits++;
1993 r = RESUME_GUEST;
1994 break;
1995 case BOOK3S_INTERRUPT_EXTERNAL:
1996 vcpu->stat.ext_intr_exits++;
1997 r = RESUME_HOST;
1998 break;
1999 case BOOK3S_INTERRUPT_H_DOORBELL:
2000 case BOOK3S_INTERRUPT_H_VIRT:
2001 vcpu->stat.ext_intr_exits++;
2002 r = RESUME_GUEST;
2003 break;
Nicholas Piggin46dea772021-11-23 19:52:22 +10002004 /* These need to go to the nested HV */
2005 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
2006 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
2007 vcpu->stat.dec_exits++;
2008 r = RESUME_HOST;
2009 break;
Paul Mackerras360cae32018-10-08 16:31:04 +11002010 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
2011 case BOOK3S_INTERRUPT_HMI:
2012 case BOOK3S_INTERRUPT_PERFMON:
2013 case BOOK3S_INTERRUPT_SYSTEM_RESET:
2014 r = RESUME_GUEST;
2015 break;
2016 case BOOK3S_INTERRUPT_MACHINE_CHECK:
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10002017 {
2018 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
2019 DEFAULT_RATELIMIT_BURST);
Paul Mackerras360cae32018-10-08 16:31:04 +11002020 /* Pass the machine check to the L1 guest */
2021 r = RESUME_HOST;
2022 /* Print the MCE event to host console. */
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10002023 if (__ratelimit(&rs))
2024 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
Paul Mackerras360cae32018-10-08 16:31:04 +11002025 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10002026 }
Paul Mackerras360cae32018-10-08 16:31:04 +11002027 /*
2028 * We get these next two if the guest accesses a page which it thinks
2029 * it has mapped but which is not actually present, either because
2030 * it is for an emulated I/O device or because the corresonding
2031 * host page has been paged out.
2032 */
2033 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
2034 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08002035 r = kvmhv_nested_page_fault(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11002036 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2037 break;
2038 case BOOK3S_INTERRUPT_H_INST_STORAGE:
2039 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
2040 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
2041 DSISR_SRR1_MATCH_64S;
Jordan Niethe6de2e832023-09-14 13:05:56 +10002042 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE)
Paul Mackerras360cae32018-10-08 16:31:04 +11002043 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
2044 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08002045 r = kvmhv_nested_page_fault(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11002046 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2047 break;
2048
2049#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2050 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
2051 /*
2052 * This occurs for various TM-related instructions that
2053 * we need to emulate on POWER9 DD2.2. We have already
2054 * handled the cases where the guest was in real-suspend
2055 * mode and was transitioning to transactional state.
2056 */
2057 r = kvmhv_p9_tm_emulation(vcpu);
Nicholas Piggind82b3922021-08-12 02:00:38 +10002058 if (r != -1)
2059 break;
2060 fallthrough; /* go to facility unavailable handler */
Paul Mackerras360cae32018-10-08 16:31:04 +11002061#endif
2062
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10002063 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
2064 u64 cause = vcpu->arch.hfscr >> 56;
2065
2066 /*
2067 * Only pass HFU interrupts to the L1 if the facility is
2068 * permitted but disabled by the L1's HFSCR, otherwise
2069 * the interrupt does not make sense to the L1 so turn
2070 * it into a HEAI.
2071 */
2072 if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) ||
Nicholas Piggin22f7ff02022-01-22 20:55:30 +10002073 (vcpu->arch.nested_hfscr & (1UL << cause))) {
Paul Mackerrasacf17872023-03-08 17:34:48 +11002074 ppc_inst_t pinst;
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10002075 vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST;
2076
2077 /*
2078 * If the fetch failed, return to guest and
2079 * try executing it again.
2080 */
Paul Mackerrasacf17872023-03-08 17:34:48 +11002081 r = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
2082 vcpu->arch.emul_inst = ppc_inst_val(pinst);
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10002083 if (r != EMULATE_DONE)
2084 r = RESUME_GUEST;
2085 else
2086 r = RESUME_HOST;
2087 } else {
2088 r = RESUME_HOST;
2089 }
2090
Nicholas Piggind82b3922021-08-12 02:00:38 +10002091 break;
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10002092 }
Nicholas Piggind82b3922021-08-12 02:00:38 +10002093
Paul Mackerras360cae32018-10-08 16:31:04 +11002094 case BOOK3S_INTERRUPT_HV_RM_HARD:
2095 vcpu->arch.trap = 0;
2096 r = RESUME_GUEST;
Paul Mackerras03f95332019-02-04 22:07:20 +11002097 if (!xics_on_xive())
Paul Mackerras360cae32018-10-08 16:31:04 +11002098 kvmppc_xics_rm_complete(vcpu, 0);
2099 break;
Bharata B Rao53324b52021-06-21 14:20:01 +05302100 case BOOK3S_INTERRUPT_SYSCALL:
2101 {
2102 unsigned long req = kvmppc_get_gpr(vcpu, 3);
2103
2104 /*
2105 * The H_RPT_INVALIDATE hcalls issued by nested
2106 * guests for process-scoped invalidations when
2107 * GTSE=0, are handled here in L0.
2108 */
2109 if (req == H_RPT_INVALIDATE) {
2110 r = kvmppc_nested_h_rpt_invalidate(vcpu);
2111 break;
2112 }
2113
2114 r = RESUME_HOST;
2115 break;
2116 }
Paul Mackerras360cae32018-10-08 16:31:04 +11002117 default:
2118 r = RESUME_HOST;
2119 break;
2120 }
2121
2122 return r;
2123}
2124
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302125static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
2126 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002127{
2128 int i;
2129
Paul Mackerrasde56a942011-06-29 00:21:34 +00002130 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05302131 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002132 for (i = 0; i < vcpu->arch.slb_max; i++) {
2133 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
2134 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
2135 }
2136
2137 return 0;
2138}
2139
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302140static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
2141 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002142{
2143 int i, j;
2144
Paul Mackerras9333e6c2014-09-02 16:14:43 +10002145 /* Only accept the same PVR as the host's, since we can't spoof it */
2146 if (sregs->pvr != vcpu->arch.pvr)
2147 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002148
2149 j = 0;
2150 for (i = 0; i < vcpu->arch.slb_nr; i++) {
2151 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
2152 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
2153 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
2154 ++j;
2155 }
2156 }
2157 vcpu->arch.slb_max = j;
2158
2159 return 0;
2160}
2161
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002162/*
2163 * Enforce limits on guest LPCR values based on hardware availability,
2164 * guest configuration, and possibly hypervisor support and security
2165 * concerns.
2166 */
2167unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
2168{
Nicholas Piggin72c15282021-04-12 11:48:38 +10002169 /* LPCR_TC only applies to HPT guests */
2170 if (kvm_is_radix(kvm))
2171 lpcr &= ~LPCR_TC;
2172
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002173 /* On POWER8 and above, userspace can modify AIL */
2174 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2175 lpcr &= ~LPCR_AIL;
Nicholas Pigginbcc92a02021-04-12 11:48:37 +10002176 if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
2177 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
Nicholas Piggin2e1ae9cd2021-05-28 19:07:41 +10002178 /*
2179 * On some POWER9s we force AIL off for radix guests to prevent
2180 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
2181 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
2182 * be cached, which the host TLB management does not expect.
2183 */
2184 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
2185 lpcr &= ~LPCR_AIL;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002186
2187 /*
2188 * On POWER9, allow userspace to enable large decrementer for the
2189 * guest, whether or not the host has it enabled.
2190 */
2191 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2192 lpcr &= ~LPCR_LD;
2193
2194 return lpcr;
2195}
2196
2197static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
2198{
2199 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
2200 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
2201 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
2202 }
2203}
2204
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002205static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
2206 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002207{
Paul Mackerras8f902b02015-03-20 20:39:38 +11002208 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002209 struct kvmppc_vcore *vc = vcpu->arch.vcore;
2210 u64 mask;
2211
2212 spin_lock(&vc->lock);
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002213
2214 /*
2215 * Userspace can only modify
2216 * DPFD (default prefetch depth), ILE (interrupt little-endian),
2217 * TC (translation control), AIL (alternate interrupt location),
2218 * LD (large decrementer).
2219 * These are subject to restrictions from kvmppc_filter_lcpr_hv().
2220 */
2221 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
2222
2223 /* Broken 32-bit version of LPCR must not clear top bits */
2224 if (preserve_top32)
2225 mask &= 0xFFFFFFFF;
2226
2227 new_lpcr = kvmppc_filter_lpcr_hv(kvm,
2228 (vc->lpcr & ~mask) | (new_lpcr & mask));
2229
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002230 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11002231 * If ILE (interrupt little-endian) has changed, update the
2232 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
2233 */
2234 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11002235 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +00002236 unsigned long i;
Anton Blanchardd6829162014-01-08 21:25:30 +11002237
Anton Blanchardd6829162014-01-08 21:25:30 +11002238 kvm_for_each_vcpu(i, vcpu, kvm) {
2239 if (vcpu->arch.vcore != vc)
2240 continue;
2241 if (new_lpcr & LPCR_ILE)
2242 vcpu->arch.intr_msr |= MSR_LE;
2243 else
2244 vcpu->arch.intr_msr &= ~MSR_LE;
2245 }
Anton Blanchardd6829162014-01-08 21:25:30 +11002246 }
2247
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002248 vc->lpcr = new_lpcr;
Jordan Niethe19d31c52023-09-14 13:05:59 +10002249 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002250
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002251 spin_unlock(&vc->lock);
2252}
2253
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302254static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2255 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00002256{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002257 int r = 0;
2258 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00002259
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002260 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05302261 case KVM_REG_PPC_DEBUG_INST:
2262 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
2263 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002264 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002265 *val = get_reg_val(id, 0);
2266 break;
2267 case KVM_REG_PPC_DABR:
2268 *val = get_reg_val(id, vcpu->arch.dabr);
2269 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11002270 case KVM_REG_PPC_DABRX:
2271 *val = get_reg_val(id, vcpu->arch.dabrx);
2272 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002273 case KVM_REG_PPC_DSCR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002274 *val = get_reg_val(id, kvmppc_get_dscr_hv(vcpu));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002275 break;
2276 case KVM_REG_PPC_PURR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002277 *val = get_reg_val(id, kvmppc_get_purr_hv(vcpu));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002278 break;
2279 case KVM_REG_PPC_SPURR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002280 *val = get_reg_val(id, kvmppc_get_spurr_hv(vcpu));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002281 break;
2282 case KVM_REG_PPC_AMR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002283 *val = get_reg_val(id, kvmppc_get_amr_hv(vcpu));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002284 break;
2285 case KVM_REG_PPC_UAMOR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002286 *val = get_reg_val(id, kvmppc_get_uamor_hv(vcpu));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002287 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002288 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002289 i = id - KVM_REG_PPC_MMCR0;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002290 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, i));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002291 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002292 case KVM_REG_PPC_MMCR2:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002293 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 2));
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002294 break;
2295 case KVM_REG_PPC_MMCRA:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002296 *val = get_reg_val(id, kvmppc_get_mmcra_hv(vcpu));
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002297 break;
2298 case KVM_REG_PPC_MMCRS:
2299 *val = get_reg_val(id, vcpu->arch.mmcrs);
2300 break;
Athira Rajeev5752fe02020-07-17 10:38:17 -04002301 case KVM_REG_PPC_MMCR3:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002302 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 3));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002303 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002304 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2305 i = id - KVM_REG_PPC_PMC1;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002306 *val = get_reg_val(id, kvmppc_get_pmc_hv(vcpu, i));
Paul Mackerras31f34382011-12-12 12:26:50 +00002307 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002308 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2309 i = id - KVM_REG_PPC_SPMC1;
2310 *val = get_reg_val(id, vcpu->arch.spmc[i]);
2311 break;
Paul Mackerras14941782013-09-06 13:11:18 +10002312 case KVM_REG_PPC_SIAR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002313 *val = get_reg_val(id, kvmppc_get_siar_hv(vcpu));
Paul Mackerras14941782013-09-06 13:11:18 +10002314 break;
2315 case KVM_REG_PPC_SDAR:
Shivaprasad G Bhat009f6f42024-06-05 13:06:28 +00002316 *val = get_reg_val(id, kvmppc_get_sdar_hv(vcpu));
Paul Mackerras14941782013-09-06 13:11:18 +10002317 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002318 case KVM_REG_PPC_SIER:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002319 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 0));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002320 break;
2321 case KVM_REG_PPC_SIER2:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002322 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 1));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002323 break;
2324 case KVM_REG_PPC_SIER3:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002325 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 2));
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002326 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002327 case KVM_REG_PPC_IAMR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002328 *val = get_reg_val(id, kvmppc_get_iamr_hv(vcpu));
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002329 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002330 case KVM_REG_PPC_PSPB:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002331 *val = get_reg_val(id, kvmppc_get_pspb_hv(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002332 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002333 case KVM_REG_PPC_DPDES:
Paul Mackerrasff42df42019-08-27 11:35:40 +10002334 /*
2335 * On POWER9, where we are emulating msgsndp etc.,
2336 * we return 1 bit for each vcpu, which can come from
2337 * either vcore->dpdes or doorbell_request.
2338 * On POWER8, doorbell_request is 0.
2339 */
Nicholas Piggin63983262021-11-23 19:52:30 +10002340 if (cpu_has_feature(CPU_FTR_ARCH_300))
2341 *val = get_reg_val(id, vcpu->arch.doorbell_request);
2342 else
2343 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
Michael Neulingb005255e2014-01-08 21:25:21 +11002344 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002345 case KVM_REG_PPC_VTB:
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002346 *val = get_reg_val(id, kvmppc_get_vtb(vcpu));
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002347 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002348 case KVM_REG_PPC_DAWR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002349 *val = get_reg_val(id, kvmppc_get_dawr0_hv(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002350 break;
2351 case KVM_REG_PPC_DAWRX:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002352 *val = get_reg_val(id, kvmppc_get_dawrx0_hv(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002353 break;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302354 case KVM_REG_PPC_DAWR1:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002355 *val = get_reg_val(id, kvmppc_get_dawr1_hv(vcpu));
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302356 break;
2357 case KVM_REG_PPC_DAWRX1:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002358 *val = get_reg_val(id, kvmppc_get_dawrx1_hv(vcpu));
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302359 break;
Shivaprasad G Bhat1a1e6862024-06-05 13:06:42 +00002360 case KVM_REG_PPC_DEXCR:
2361 *val = get_reg_val(id, kvmppc_get_dexcr_hv(vcpu));
2362 break;
Shivaprasad G Bhate9eb7902024-06-05 13:07:15 +00002363 case KVM_REG_PPC_HASHKEYR:
2364 *val = get_reg_val(id, kvmppc_get_hashkeyr_hv(vcpu));
2365 break;
Shivaprasad G Bhat9a0d2f42024-06-05 13:07:39 +00002366 case KVM_REG_PPC_HASHPKEYR:
2367 *val = get_reg_val(id, kvmppc_get_hashpkeyr_hv(vcpu));
2368 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002369 case KVM_REG_PPC_CIABR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002370 *val = get_reg_val(id, kvmppc_get_ciabr_hv(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002371 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002372 case KVM_REG_PPC_CSIGR:
2373 *val = get_reg_val(id, vcpu->arch.csigr);
2374 break;
2375 case KVM_REG_PPC_TACR:
2376 *val = get_reg_val(id, vcpu->arch.tacr);
2377 break;
2378 case KVM_REG_PPC_TCSCR:
2379 *val = get_reg_val(id, vcpu->arch.tcscr);
2380 break;
2381 case KVM_REG_PPC_PID:
Jordan Niethe7028ac82023-09-14 13:05:53 +10002382 *val = get_reg_val(id, kvmppc_get_pid(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002383 break;
2384 case KVM_REG_PPC_ACOP:
2385 *val = get_reg_val(id, vcpu->arch.acop);
2386 break;
2387 case KVM_REG_PPC_WORT:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002388 *val = get_reg_val(id, kvmppc_get_wort_hv(vcpu));
Michael Neulingb005255e2014-01-08 21:25:21 +11002389 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11002390 case KVM_REG_PPC_TIDR:
2391 *val = get_reg_val(id, vcpu->arch.tid);
2392 break;
2393 case KVM_REG_PPC_PSSCR:
2394 *val = get_reg_val(id, vcpu->arch.psscr);
2395 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002396 case KVM_REG_PPC_VPA_ADDR:
2397 spin_lock(&vcpu->arch.vpa_update_lock);
2398 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
2399 spin_unlock(&vcpu->arch.vpa_update_lock);
2400 break;
2401 case KVM_REG_PPC_VPA_SLB:
2402 spin_lock(&vcpu->arch.vpa_update_lock);
2403 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
2404 val->vpaval.length = vcpu->arch.slb_shadow.len;
2405 spin_unlock(&vcpu->arch.vpa_update_lock);
2406 break;
2407 case KVM_REG_PPC_VPA_DTL:
2408 spin_lock(&vcpu->arch.vpa_update_lock);
2409 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
2410 val->vpaval.length = vcpu->arch.dtl.len;
2411 spin_unlock(&vcpu->arch.vpa_update_lock);
2412 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002413 case KVM_REG_PPC_TB_OFFSET:
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002414 *val = get_reg_val(id, kvmppc_get_tb_offset(vcpu));
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002415 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002416 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002417 case KVM_REG_PPC_LPCR_64:
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002418 *val = get_reg_val(id, kvmppc_get_lpcr(vcpu));
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002419 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002420 case KVM_REG_PPC_PPR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002421 *val = get_reg_val(id, kvmppc_get_ppr_hv(vcpu));
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002422 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002423#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2424 case KVM_REG_PPC_TFHAR:
2425 *val = get_reg_val(id, vcpu->arch.tfhar);
2426 break;
2427 case KVM_REG_PPC_TFIAR:
2428 *val = get_reg_val(id, vcpu->arch.tfiar);
2429 break;
2430 case KVM_REG_PPC_TEXASR:
2431 *val = get_reg_val(id, vcpu->arch.texasr);
2432 break;
2433 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2434 i = id - KVM_REG_PPC_TM_GPR0;
2435 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
2436 break;
2437 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2438 {
2439 int j;
2440 i = id - KVM_REG_PPC_TM_VSR0;
2441 if (i < 32)
2442 for (j = 0; j < TS_FPRWIDTH; j++)
2443 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
2444 else {
2445 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2446 val->vval = vcpu->arch.vr_tm.vr[i-32];
2447 else
2448 r = -ENXIO;
2449 }
2450 break;
2451 }
2452 case KVM_REG_PPC_TM_CR:
2453 *val = get_reg_val(id, vcpu->arch.cr_tm);
2454 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11002455 case KVM_REG_PPC_TM_XER:
2456 *val = get_reg_val(id, vcpu->arch.xer_tm);
2457 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002458 case KVM_REG_PPC_TM_LR:
2459 *val = get_reg_val(id, vcpu->arch.lr_tm);
2460 break;
2461 case KVM_REG_PPC_TM_CTR:
2462 *val = get_reg_val(id, vcpu->arch.ctr_tm);
2463 break;
2464 case KVM_REG_PPC_TM_FPSCR:
2465 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
2466 break;
2467 case KVM_REG_PPC_TM_AMR:
2468 *val = get_reg_val(id, vcpu->arch.amr_tm);
2469 break;
2470 case KVM_REG_PPC_TM_PPR:
2471 *val = get_reg_val(id, vcpu->arch.ppr_tm);
2472 break;
2473 case KVM_REG_PPC_TM_VRSAVE:
2474 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
2475 break;
2476 case KVM_REG_PPC_TM_VSCR:
2477 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2478 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
2479 else
2480 r = -ENXIO;
2481 break;
2482 case KVM_REG_PPC_TM_DSCR:
2483 *val = get_reg_val(id, vcpu->arch.dscr_tm);
2484 break;
2485 case KVM_REG_PPC_TM_TAR:
2486 *val = get_reg_val(id, vcpu->arch.tar_tm);
2487 break;
2488#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10002489 case KVM_REG_PPC_ARCH_COMPAT:
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002490 *val = get_reg_val(id, kvmppc_get_arch_compat(vcpu));
Paul Mackerras388cc6e2013-09-21 14:35:02 +10002491 break;
Paul Mackerras58555642018-01-12 20:55:20 +11002492 case KVM_REG_PPC_DEC_EXPIRY:
Jordan Niethe7028ac82023-09-14 13:05:53 +10002493 *val = get_reg_val(id, kvmppc_get_dec_expires(vcpu));
Paul Mackerras58555642018-01-12 20:55:20 +11002494 break;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002495 case KVM_REG_PPC_ONLINE:
2496 *val = get_reg_val(id, vcpu->arch.online);
2497 break;
Paul Mackerras30323412018-10-08 16:31:13 +11002498 case KVM_REG_PPC_PTCR:
2499 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
2500 break;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002501 case KVM_REG_PPC_FSCR:
2502 *val = get_reg_val(id, kvmppc_get_fscr_hv(vcpu));
2503 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002504 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002505 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00002506 break;
2507 }
2508
2509 return r;
2510}
2511
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302512static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2513 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00002514{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002515 int r = 0;
2516 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002517 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00002518
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002519 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00002520 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00002521 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002522 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00002523 r = -EINVAL;
2524 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002525 case KVM_REG_PPC_DABR:
2526 vcpu->arch.dabr = set_reg_val(id, *val);
2527 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11002528 case KVM_REG_PPC_DABRX:
2529 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
2530 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002531 case KVM_REG_PPC_DSCR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002532 kvmppc_set_dscr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002533 break;
2534 case KVM_REG_PPC_PURR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002535 kvmppc_set_purr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002536 break;
2537 case KVM_REG_PPC_SPURR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002538 kvmppc_set_spurr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002539 break;
2540 case KVM_REG_PPC_AMR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002541 kvmppc_set_amr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002542 break;
2543 case KVM_REG_PPC_UAMOR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002544 kvmppc_set_uamor_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002545 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002546 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002547 i = id - KVM_REG_PPC_MMCR0;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002548 kvmppc_set_mmcr_hv(vcpu, i, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002549 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002550 case KVM_REG_PPC_MMCR2:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002551 kvmppc_set_mmcr_hv(vcpu, 2, set_reg_val(id, *val));
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002552 break;
2553 case KVM_REG_PPC_MMCRA:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002554 kvmppc_set_mmcra_hv(vcpu, set_reg_val(id, *val));
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002555 break;
2556 case KVM_REG_PPC_MMCRS:
2557 vcpu->arch.mmcrs = set_reg_val(id, *val);
2558 break;
Athira Rajeev5752fe02020-07-17 10:38:17 -04002559 case KVM_REG_PPC_MMCR3:
Shivaprasad G Bhatf9ca6a12024-06-05 13:06:16 +00002560 kvmppc_set_mmcr_hv(vcpu, 3, set_reg_val(id, *val));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002561 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002562 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2563 i = id - KVM_REG_PPC_PMC1;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002564 kvmppc_set_pmc_hv(vcpu, i, set_reg_val(id, *val));
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002565 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002566 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2567 i = id - KVM_REG_PPC_SPMC1;
2568 vcpu->arch.spmc[i] = set_reg_val(id, *val);
2569 break;
Paul Mackerras14941782013-09-06 13:11:18 +10002570 case KVM_REG_PPC_SIAR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002571 kvmppc_set_siar_hv(vcpu, set_reg_val(id, *val));
Paul Mackerras14941782013-09-06 13:11:18 +10002572 break;
2573 case KVM_REG_PPC_SDAR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002574 kvmppc_set_sdar_hv(vcpu, set_reg_val(id, *val));
Paul Mackerras14941782013-09-06 13:11:18 +10002575 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002576 case KVM_REG_PPC_SIER:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002577 kvmppc_set_sier_hv(vcpu, 0, set_reg_val(id, *val));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002578 break;
2579 case KVM_REG_PPC_SIER2:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002580 kvmppc_set_sier_hv(vcpu, 1, set_reg_val(id, *val));
Athira Rajeev5752fe02020-07-17 10:38:17 -04002581 break;
2582 case KVM_REG_PPC_SIER3:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002583 kvmppc_set_sier_hv(vcpu, 2, set_reg_val(id, *val));
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002584 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002585 case KVM_REG_PPC_IAMR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002586 kvmppc_set_iamr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002587 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002588 case KVM_REG_PPC_PSPB:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002589 kvmppc_set_pspb_hv(vcpu, set_reg_val(id, *val));
Michael Neulingb005255e2014-01-08 21:25:21 +11002590 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002591 case KVM_REG_PPC_DPDES:
Nicholas Piggin63983262021-11-23 19:52:30 +10002592 if (cpu_has_feature(CPU_FTR_ARCH_300))
2593 vcpu->arch.doorbell_request = set_reg_val(id, *val) & 1;
2594 else
2595 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
Michael Neulingb005255e2014-01-08 21:25:21 +11002596 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002597 case KVM_REG_PPC_VTB:
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002598 kvmppc_set_vtb(vcpu, set_reg_val(id, *val));
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002599 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002600 case KVM_REG_PPC_DAWR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002601 kvmppc_set_dawr0_hv(vcpu, set_reg_val(id, *val));
Michael Neulingb005255e2014-01-08 21:25:21 +11002602 break;
2603 case KVM_REG_PPC_DAWRX:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002604 kvmppc_set_dawrx0_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
Michael Neulingb005255e2014-01-08 21:25:21 +11002605 break;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302606 case KVM_REG_PPC_DAWR1:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002607 kvmppc_set_dawr1_hv(vcpu, set_reg_val(id, *val));
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302608 break;
2609 case KVM_REG_PPC_DAWRX1:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002610 kvmppc_set_dawrx1_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP);
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302611 break;
Shivaprasad G Bhat1a1e6862024-06-05 13:06:42 +00002612 case KVM_REG_PPC_DEXCR:
2613 kvmppc_set_dexcr_hv(vcpu, set_reg_val(id, *val));
2614 break;
Shivaprasad G Bhate9eb7902024-06-05 13:07:15 +00002615 case KVM_REG_PPC_HASHKEYR:
2616 kvmppc_set_hashkeyr_hv(vcpu, set_reg_val(id, *val));
2617 break;
Shivaprasad G Bhat9a0d2f42024-06-05 13:07:39 +00002618 case KVM_REG_PPC_HASHPKEYR:
2619 kvmppc_set_hashpkeyr_hv(vcpu, set_reg_val(id, *val));
2620 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002621 case KVM_REG_PPC_CIABR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002622 kvmppc_set_ciabr_hv(vcpu, set_reg_val(id, *val));
Michael Neulingb005255e2014-01-08 21:25:21 +11002623 /* Don't allow setting breakpoints in hypervisor code */
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002624 if ((kvmppc_get_ciabr_hv(vcpu) & CIABR_PRIV) == CIABR_PRIV_HYPER)
2625 kvmppc_set_ciabr_hv(vcpu, kvmppc_get_ciabr_hv(vcpu) & ~CIABR_PRIV);
Michael Neulingb005255e2014-01-08 21:25:21 +11002626 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002627 case KVM_REG_PPC_CSIGR:
2628 vcpu->arch.csigr = set_reg_val(id, *val);
2629 break;
2630 case KVM_REG_PPC_TACR:
2631 vcpu->arch.tacr = set_reg_val(id, *val);
2632 break;
2633 case KVM_REG_PPC_TCSCR:
2634 vcpu->arch.tcscr = set_reg_val(id, *val);
2635 break;
2636 case KVM_REG_PPC_PID:
Jordan Niethe7028ac82023-09-14 13:05:53 +10002637 kvmppc_set_pid(vcpu, set_reg_val(id, *val));
Michael Neulingb005255e2014-01-08 21:25:21 +11002638 break;
2639 case KVM_REG_PPC_ACOP:
2640 vcpu->arch.acop = set_reg_val(id, *val);
2641 break;
2642 case KVM_REG_PPC_WORT:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002643 kvmppc_set_wort_hv(vcpu, set_reg_val(id, *val));
Michael Neulingb005255e2014-01-08 21:25:21 +11002644 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11002645 case KVM_REG_PPC_TIDR:
2646 vcpu->arch.tid = set_reg_val(id, *val);
2647 break;
2648 case KVM_REG_PPC_PSSCR:
2649 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
2650 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002651 case KVM_REG_PPC_VPA_ADDR:
2652 addr = set_reg_val(id, *val);
2653 r = -EINVAL;
2654 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
2655 vcpu->arch.dtl.next_gpa))
2656 break;
2657 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
2658 break;
2659 case KVM_REG_PPC_VPA_SLB:
2660 addr = val->vpaval.addr;
2661 len = val->vpaval.length;
2662 r = -EINVAL;
2663 if (addr && !vcpu->arch.vpa.next_gpa)
2664 break;
2665 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
2666 break;
2667 case KVM_REG_PPC_VPA_DTL:
2668 addr = val->vpaval.addr;
2669 len = val->vpaval.length;
2670 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00002671 if (addr && (len < sizeof(struct dtl_entry) ||
2672 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00002673 break;
2674 len -= len % sizeof(struct dtl_entry);
2675 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
2676 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002677 case KVM_REG_PPC_TB_OFFSET:
Fabiano Rosas0a5bfb82022-08-16 19:25:17 -03002678 {
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002679 /* round up to multiple of 2^24 */
Fabiano Rosas0a5bfb82022-08-16 19:25:17 -03002680 u64 tb_offset = ALIGN(set_reg_val(id, *val), 1UL << 24);
2681
2682 /*
2683 * Now that we know the timebase offset, update the
2684 * decrementer expiry with a guest timebase value. If
2685 * the userspace does not set DEC_EXPIRY, this ensures
2686 * a migrated vcpu at least starts with an expired
2687 * decrementer, which is better than a large one that
2688 * causes a hang.
2689 */
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002690 kvmppc_set_tb_offset(vcpu, tb_offset);
Jordan Niethe7028ac82023-09-14 13:05:53 +10002691 if (!kvmppc_get_dec_expires(vcpu) && tb_offset)
2692 kvmppc_set_dec_expires(vcpu, get_tb() + tb_offset);
Fabiano Rosas0a5bfb82022-08-16 19:25:17 -03002693
Jordan Niethec8ae9b32023-09-14 13:05:54 +10002694 kvmppc_set_tb_offset(vcpu, tb_offset);
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002695 break;
Fabiano Rosas0a5bfb82022-08-16 19:25:17 -03002696 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002697 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002698 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
2699 break;
2700 case KVM_REG_PPC_LPCR_64:
2701 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002702 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002703 case KVM_REG_PPC_PPR:
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002704 kvmppc_set_ppr_hv(vcpu, set_reg_val(id, *val));
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002705 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002706#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2707 case KVM_REG_PPC_TFHAR:
2708 vcpu->arch.tfhar = set_reg_val(id, *val);
2709 break;
2710 case KVM_REG_PPC_TFIAR:
2711 vcpu->arch.tfiar = set_reg_val(id, *val);
2712 break;
2713 case KVM_REG_PPC_TEXASR:
2714 vcpu->arch.texasr = set_reg_val(id, *val);
2715 break;
2716 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2717 i = id - KVM_REG_PPC_TM_GPR0;
2718 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
2719 break;
2720 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2721 {
2722 int j;
2723 i = id - KVM_REG_PPC_TM_VSR0;
2724 if (i < 32)
2725 for (j = 0; j < TS_FPRWIDTH; j++)
2726 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
2727 else
2728 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2729 vcpu->arch.vr_tm.vr[i-32] = val->vval;
2730 else
2731 r = -ENXIO;
2732 break;
2733 }
2734 case KVM_REG_PPC_TM_CR:
2735 vcpu->arch.cr_tm = set_reg_val(id, *val);
2736 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11002737 case KVM_REG_PPC_TM_XER:
2738 vcpu->arch.xer_tm = set_reg_val(id, *val);
2739 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002740 case KVM_REG_PPC_TM_LR:
2741 vcpu->arch.lr_tm = set_reg_val(id, *val);
2742 break;
2743 case KVM_REG_PPC_TM_CTR:
2744 vcpu->arch.ctr_tm = set_reg_val(id, *val);
2745 break;
2746 case KVM_REG_PPC_TM_FPSCR:
2747 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
2748 break;
2749 case KVM_REG_PPC_TM_AMR:
2750 vcpu->arch.amr_tm = set_reg_val(id, *val);
2751 break;
2752 case KVM_REG_PPC_TM_PPR:
2753 vcpu->arch.ppr_tm = set_reg_val(id, *val);
2754 break;
2755 case KVM_REG_PPC_TM_VRSAVE:
2756 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
2757 break;
2758 case KVM_REG_PPC_TM_VSCR:
2759 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2760 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
2761 else
2762 r = - ENXIO;
2763 break;
2764 case KVM_REG_PPC_TM_DSCR:
2765 vcpu->arch.dscr_tm = set_reg_val(id, *val);
2766 break;
2767 case KVM_REG_PPC_TM_TAR:
2768 vcpu->arch.tar_tm = set_reg_val(id, *val);
2769 break;
2770#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10002771 case KVM_REG_PPC_ARCH_COMPAT:
2772 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
2773 break;
Paul Mackerras58555642018-01-12 20:55:20 +11002774 case KVM_REG_PPC_DEC_EXPIRY:
Jordan Niethe7028ac82023-09-14 13:05:53 +10002775 kvmppc_set_dec_expires(vcpu, set_reg_val(id, *val));
Paul Mackerras58555642018-01-12 20:55:20 +11002776 break;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002777 case KVM_REG_PPC_ONLINE:
Paul Mackerras7aa15842018-04-20 19:53:22 +10002778 i = set_reg_val(id, *val);
2779 if (i && !vcpu->arch.online)
2780 atomic_inc(&vcpu->arch.vcore->online_count);
2781 else if (!i && vcpu->arch.online)
2782 atomic_dec(&vcpu->arch.vcore->online_count);
2783 vcpu->arch.online = i;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002784 break;
Paul Mackerras30323412018-10-08 16:31:13 +11002785 case KVM_REG_PPC_PTCR:
2786 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
2787 break;
Jordan Nietheebc88ea2023-09-14 13:05:55 +10002788 case KVM_REG_PPC_FSCR:
2789 kvmppc_set_fscr_hv(vcpu, set_reg_val(id, *val));
2790 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002791 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002792 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00002793 break;
2794 }
2795
2796 return r;
2797}
2798
Paul Mackerras45c940b2016-11-18 17:43:30 +11002799/*
2800 * On POWER9, threads are independent and can be in different partitions.
2801 * Therefore we consider each thread to be a subcore.
2802 * There is a restriction that all threads have to be in the same
2803 * MMU mode (radix or HPT), unfortunately, but since we only support
2804 * HPT guests on a HPT host so far, that isn't an impediment yet.
2805 */
Paul Mackerras516f7892017-10-16 16:11:57 +11002806static int threads_per_vcore(struct kvm *kvm)
Paul Mackerras45c940b2016-11-18 17:43:30 +11002807{
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10002808 if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras45c940b2016-11-18 17:43:30 +11002809 return 1;
2810 return threads_per_subcore;
2811}
2812
Sam Bobroff1e175d22018-07-25 16:12:02 +10002813static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
Stewart Smithde9bdd12014-07-18 14:18:42 +10002814{
2815 struct kvmppc_vcore *vcore;
2816
2817 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
2818
2819 if (vcore == NULL)
2820 return NULL;
2821
Stewart Smithde9bdd12014-07-18 14:18:42 +10002822 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11002823 spin_lock_init(&vcore->stoltb_lock);
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002824 rcuwait_init(&vcore->wait);
Stewart Smithde9bdd12014-07-18 14:18:42 +10002825 vcore->preempt_tb = TB_NIL;
2826 vcore->lpcr = kvm->arch.lpcr;
Sam Bobroff1e175d22018-07-25 16:12:02 +10002827 vcore->first_vcpuid = id;
Stewart Smithde9bdd12014-07-18 14:18:42 +10002828 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10002829 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10002830
2831 return vcore;
2832}
2833
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002834#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2835static struct debugfs_timings_element {
2836 const char *name;
2837 size_t offset;
2838} timings[] = {
Fabiano Rosasc3fa64c2022-05-25 10:05:52 -03002839#ifdef CONFIG_KVM_BOOK3S_HV_P9_TIMING
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03002840 {"vcpu_entry", offsetof(struct kvm_vcpu, arch.vcpu_entry)},
2841 {"guest_entry", offsetof(struct kvm_vcpu, arch.guest_entry)},
2842 {"in_guest", offsetof(struct kvm_vcpu, arch.in_guest)},
2843 {"guest_exit", offsetof(struct kvm_vcpu, arch.guest_exit)},
2844 {"vcpu_exit", offsetof(struct kvm_vcpu, arch.vcpu_exit)},
2845 {"hypercall", offsetof(struct kvm_vcpu, arch.hcall)},
2846 {"page_fault", offsetof(struct kvm_vcpu, arch.pg_fault)},
Fabiano Rosasc3fa64c2022-05-25 10:05:52 -03002847#else
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002848 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
2849 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
2850 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
2851 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
2852 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
Fabiano Rosasc3fa64c2022-05-25 10:05:52 -03002853#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002854};
2855
Thomas Meyer4bb817ed2017-09-03 14:19:31 +02002856#define N_TIMINGS (ARRAY_SIZE(timings))
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002857
2858struct debugfs_timings_state {
2859 struct kvm_vcpu *vcpu;
2860 unsigned int buflen;
2861 char buf[N_TIMINGS * 100];
2862};
2863
2864static int debugfs_timings_open(struct inode *inode, struct file *file)
2865{
2866 struct kvm_vcpu *vcpu = inode->i_private;
2867 struct debugfs_timings_state *p;
2868
2869 p = kzalloc(sizeof(*p), GFP_KERNEL);
2870 if (!p)
2871 return -ENOMEM;
2872
2873 kvm_get_kvm(vcpu->kvm);
2874 p->vcpu = vcpu;
2875 file->private_data = p;
2876
2877 return nonseekable_open(inode, file);
2878}
2879
2880static int debugfs_timings_release(struct inode *inode, struct file *file)
2881{
2882 struct debugfs_timings_state *p = file->private_data;
2883
2884 kvm_put_kvm(p->vcpu->kvm);
2885 kfree(p);
2886 return 0;
2887}
2888
2889static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
2890 size_t len, loff_t *ppos)
2891{
2892 struct debugfs_timings_state *p = file->private_data;
2893 struct kvm_vcpu *vcpu = p->vcpu;
2894 char *s, *buf_end;
2895 struct kvmhv_tb_accumulator tb;
2896 u64 count;
2897 loff_t pos;
2898 ssize_t n;
2899 int i, loops;
2900 bool ok;
2901
2902 if (!p->buflen) {
2903 s = p->buf;
2904 buf_end = s + sizeof(p->buf);
2905 for (i = 0; i < N_TIMINGS; ++i) {
2906 struct kvmhv_tb_accumulator *acc;
2907
2908 acc = (struct kvmhv_tb_accumulator *)
2909 ((unsigned long)vcpu + timings[i].offset);
2910 ok = false;
2911 for (loops = 0; loops < 1000; ++loops) {
2912 count = acc->seqcount;
2913 if (!(count & 1)) {
2914 smp_rmb();
2915 tb = *acc;
2916 smp_rmb();
2917 if (count == acc->seqcount) {
2918 ok = true;
2919 break;
2920 }
2921 }
2922 udelay(1);
2923 }
2924 if (!ok)
2925 snprintf(s, buf_end - s, "%s: stuck\n",
2926 timings[i].name);
2927 else
2928 snprintf(s, buf_end - s,
2929 "%s: %llu %llu %llu %llu\n",
2930 timings[i].name, count / 2,
2931 tb_to_ns(tb.tb_total),
2932 tb_to_ns(tb.tb_min),
2933 tb_to_ns(tb.tb_max));
2934 s += strlen(s);
2935 }
2936 p->buflen = s - p->buf;
2937 }
2938
2939 pos = *ppos;
2940 if (pos >= p->buflen)
2941 return 0;
2942 if (len > p->buflen - pos)
2943 len = p->buflen - pos;
2944 n = copy_to_user(buf, p->buf + pos, len);
2945 if (n) {
2946 if (n == len)
2947 return -EFAULT;
2948 len -= n;
2949 }
2950 *ppos = pos + len;
2951 return len;
2952}
2953
2954static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
2955 size_t len, loff_t *ppos)
2956{
2957 return -EACCES;
2958}
2959
2960static const struct file_operations debugfs_timings_ops = {
2961 .owner = THIS_MODULE,
2962 .open = debugfs_timings_open,
2963 .release = debugfs_timings_release,
2964 .read = debugfs_timings_read,
2965 .write = debugfs_timings_write,
2966 .llseek = generic_file_llseek,
2967};
2968
2969/* Create a debugfs directory for the vcpu */
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11002970static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002971{
Fabiano Rosasc3fa64c2022-05-25 10:05:52 -03002972 if (cpu_has_feature(CPU_FTR_ARCH_300) == IS_ENABLED(CONFIG_KVM_BOOK3S_HV_P9_TIMING))
2973 debugfs_create_file("timings", 0444, debugfs_dentry, vcpu,
2974 &debugfs_timings_ops);
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11002975 return 0;
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002976}
2977
2978#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11002979static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002980{
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11002981 return 0;
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002982}
2983#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2984
Sean Christophersonff030fd2019-12-18 13:55:00 -08002985static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002986{
Paul Mackerras3c313522017-02-06 13:24:41 +11002987 int err;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002988 int core;
2989 struct kvmppc_vcore *vcore;
Sean Christophersonff030fd2019-12-18 13:55:00 -08002990 struct kvm *kvm;
2991 unsigned int id;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002992
Sean Christophersonff030fd2019-12-18 13:55:00 -08002993 kvm = vcpu->kvm;
2994 id = vcpu->vcpu_id;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002995
2996 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02002997#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2998 /*
2999 * The shared struct is never shared on HV,
3000 * so we can always use host endianness
3001 */
3002#ifdef __BIG_ENDIAN__
3003 vcpu->arch.shared_big_endian = true;
3004#else
3005 vcpu->arch.shared_big_endian = false;
3006#endif
3007#endif
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003008
Jordan Niethe19d31c52023-09-14 13:05:59 +10003009 if (kvmhv_is_nestedv2()) {
3010 err = kvmhv_nestedv2_vcpu_create(vcpu, &vcpu->arch.nestedv2_io);
3011 if (err < 0)
3012 return err;
3013 }
3014
3015 kvmppc_set_mmcr_hv(vcpu, 0, MMCR0_FC);
Nicholas Piggin245ebf82021-11-23 19:51:49 +10003016 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003017 kvmppc_set_mmcr_hv(vcpu, 0, kvmppc_get_mmcr_hv(vcpu, 0) | MMCR0_PMCCEXT);
3018 kvmppc_set_mmcra_hv(vcpu, MMCRA_BHRB_DISABLE);
Nicholas Piggin245ebf82021-11-23 19:51:49 +10003019 }
3020
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003021 kvmppc_set_ctrl_hv(vcpu, CTRL_RUNLATCH);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003022 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303023 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00003024 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003025 spin_lock_init(&vcpu->arch.tbacct_lock);
3026 vcpu->arch.busy_preempt = TB_NIL;
Jordan Niethe6de2e832023-09-14 13:05:56 +10003027 __kvmppc_set_msr_hv(vcpu, MSR_ME);
Anton Blanchardd6829162014-01-08 21:25:30 +11003028 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003029
Paul Mackerras769377f2017-02-15 14:30:17 +11003030 /*
3031 * Set the default HFSCR for the guest from the host value.
Paul Mackerrasa3800ef2023-03-08 17:36:11 +11003032 * This value is only used on POWER9 and later.
3033 * On >= POWER9, we want to virtualize the doorbell facility, so we
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11003034 * don't set the HFSCR_MSGP bit, and that causes those instructions
3035 * to trap and then we emulate them.
Paul Mackerras769377f2017-02-15 14:30:17 +11003036 */
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003037 kvmppc_set_hfscr_hv(vcpu, HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
3038 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP);
Paul Mackerrasa3800ef2023-03-08 17:36:11 +11003039
3040 /* On POWER10 and later, allow prefixed instructions */
3041 if (cpu_has_feature(CPU_FTR_ARCH_31))
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003042 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PREFIX);
Paul Mackerrasa3800ef2023-03-08 17:36:11 +11003043
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11003044 if (cpu_has_feature(CPU_FTR_HVMODE)) {
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003045 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & mfspr(SPRN_HFSCR));
3046
Nicholas Pigginbd31ecf2021-07-16 12:43:09 +10003047#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11003048 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003049 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM);
Nicholas Pigginbd31ecf2021-07-16 12:43:09 +10003050#endif
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11003051 }
3052 if (cpu_has_feature(CPU_FTR_TM_COMP))
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003053 vcpu->arch.hfscr |= HFSCR_TM;
Paul Mackerras769377f2017-02-15 14:30:17 +11003054
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003055 vcpu->arch.hfscr_permitted = kvmppc_get_hfscr_hv(vcpu);
Nicholas Piggin8b210a82021-08-12 02:00:40 +10003056
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10003057 /*
Nicholas Piggin022ecb92021-11-23 19:52:15 +10003058 * PM, EBB, TM are demand-faulted so start with it clear.
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10003059 */
Jordan Nietheebc88ea2023-09-14 13:05:55 +10003060 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & ~(HFSCR_PM | HFSCR_EBB | HFSCR_TM));
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10003061
Paul Mackerrasde56a942011-06-29 00:21:34 +00003062 kvmppc_mmu_book3s_hv_init(vcpu);
3063
Paul Mackerras8455d792012-10-15 01:17:42 +00003064 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003065
3066 init_waitqueue_head(&vcpu->arch.cpu_run);
3067
3068 mutex_lock(&kvm->lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11003069 vcore = NULL;
3070 err = -EINVAL;
Sam Bobroff1e175d22018-07-25 16:12:02 +10003071 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerrasb5c6f762018-07-26 15:38:41 +10003072 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
3073 pr_devel("KVM: VCPU ID too high\n");
3074 core = KVM_MAX_VCORES;
3075 } else {
3076 BUG_ON(kvm->arch.smt_mode != 1);
3077 core = kvmppc_pack_vcpu_id(kvm, id);
3078 }
Sam Bobroff1e175d22018-07-25 16:12:02 +10003079 } else {
3080 core = id / kvm->arch.smt_mode;
3081 }
Paul Mackerras3c313522017-02-06 13:24:41 +11003082 if (core < KVM_MAX_VCORES) {
3083 vcore = kvm->arch.vcores[core];
Sam Bobroff1e175d22018-07-25 16:12:02 +10003084 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
3085 pr_devel("KVM: collision on id %u", id);
3086 vcore = NULL;
3087 } else if (!vcore) {
Paul Mackerras0d4ee882019-05-23 16:35:34 +10003088 /*
3089 * Take mmu_setup_lock for mutual exclusion
3090 * with kvmppc_update_lpcr().
3091 */
Paul Mackerras3c313522017-02-06 13:24:41 +11003092 err = -ENOMEM;
Sam Bobroff1e175d22018-07-25 16:12:02 +10003093 vcore = kvmppc_vcore_create(kvm,
3094 id & ~(kvm->arch.smt_mode - 1));
Paul Mackerras0d4ee882019-05-23 16:35:34 +10003095 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11003096 kvm->arch.vcores[core] = vcore;
3097 kvm->arch.online_vcores++;
Paul Mackerras0d4ee882019-05-23 16:35:34 +10003098 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11003099 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003100 }
3101 mutex_unlock(&kvm->lock);
3102
3103 if (!vcore)
Sean Christophersonff030fd2019-12-18 13:55:00 -08003104 return err;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003105
3106 spin_lock(&vcore->lock);
3107 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003108 spin_unlock(&vcore->lock);
3109 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11003110 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10003111 vcpu->arch.thread_cpu = -1;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003112 vcpu->arch.prev_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003113
Alexander Grafaf8f38b2011-08-10 13:57:08 +02003114 vcpu->arch.cpu_type = KVM_CPU_3S_64;
3115 kvmppc_sanity_check(vcpu);
3116
Sean Christophersonc50bfbd2019-12-18 13:54:57 -08003117 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003118}
3119
Paul Mackerras3c313522017-02-06 13:24:41 +11003120static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
3121 unsigned long flags)
3122{
3123 int err;
Paul Mackerras57900692017-05-16 16:41:20 +10003124 int esmt = 0;
Paul Mackerras3c313522017-02-06 13:24:41 +11003125
3126 if (flags)
3127 return -EINVAL;
3128 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
3129 return -EINVAL;
3130 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
3131 /*
3132 * On POWER8 (or POWER7), the threading mode is "strict",
3133 * so we pack smt_mode vcpus per vcore.
3134 */
3135 if (smt_mode > threads_per_subcore)
3136 return -EINVAL;
3137 } else {
3138 /*
3139 * On POWER9, the threading mode is "loose",
3140 * so each vcpu gets its own vcore.
3141 */
Paul Mackerras57900692017-05-16 16:41:20 +10003142 esmt = smt_mode;
Paul Mackerras3c313522017-02-06 13:24:41 +11003143 smt_mode = 1;
3144 }
3145 mutex_lock(&kvm->lock);
3146 err = -EBUSY;
3147 if (!kvm->arch.online_vcores) {
3148 kvm->arch.smt_mode = smt_mode;
Paul Mackerras57900692017-05-16 16:41:20 +10003149 kvm->arch.emul_smt_mode = esmt;
Paul Mackerras3c313522017-02-06 13:24:41 +11003150 err = 0;
3151 }
3152 mutex_unlock(&kvm->lock);
3153
3154 return err;
3155}
3156
Paul Mackerrasc35635e2013-04-18 19:51:04 +00003157static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
3158{
3159 if (vpa->pinned_addr)
3160 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
3161 vpa->dirty);
3162}
3163
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303164static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003165{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00003166 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00003167 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
3168 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
3169 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00003170 spin_unlock(&vcpu->arch.vpa_update_lock);
Jordan Niethe19d31c52023-09-14 13:05:59 +10003171 if (kvmhv_is_nestedv2())
3172 kvmhv_nestedv2_vcpu_free(vcpu, &vcpu->arch.nestedv2_io);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003173}
3174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303175static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
3176{
3177 /* Indicate we want to get back into the guest */
3178 return 1;
3179}
3180
Paul Mackerras19ccb762011-07-23 17:42:46 +10003181static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003182{
Paul Mackerras19ccb762011-07-23 17:42:46 +10003183 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003184
Paul Mackerras19ccb762011-07-23 17:42:46 +10003185 now = get_tb();
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10003186 if (now > kvmppc_dec_expires_host_tb(vcpu)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10003187 /* decrementer has already gone negative */
3188 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06003189 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003190 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003191 }
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10003192 dec_nsec = tb_to_ns(kvmppc_dec_expires_host_tb(vcpu) - now);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003193 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
Paul Mackerras19ccb762011-07-23 17:42:46 +10003194 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003195}
3196
Paul Mackerras8b24e692017-06-26 15:45:51 +10003197extern int __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003198
3199static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003200 struct kvm_vcpu *vcpu, u64 tb)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003201{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003202 u64 now;
3203
Paul Mackerras371fefd2011-06-29 00:23:08 +00003204 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
3205 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11003206 spin_lock_irq(&vcpu->arch.tbacct_lock);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003207 now = tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00003208 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
3209 vcpu->arch.stolen_logged;
3210 vcpu->arch.busy_preempt = now;
3211 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11003212 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003213 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003214 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003215}
3216
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003217static int kvmppc_grab_hwthread(int cpu)
3218{
3219 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10003220 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003221
Nicholas Piggind2e60072018-02-14 01:08:12 +10003222 tpaca = paca_ptrs[cpu];
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003223
3224 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00003225 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003226 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003227 tpaca->kvm_hstate.napping = 0;
3228 smp_wmb();
3229 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003230
3231 /*
3232 * If the thread is already executing in the kernel (e.g. handling
3233 * a stray interrupt), wait for it to get back to nap mode.
3234 * The smp_mb() is to ensure that our setting of hwthread_req
3235 * is visible before we look at hwthread_state, so if this
3236 * races with the code at system_reset_pSeries and the thread
3237 * misses our setting of hwthread_req, we are sure to see its
3238 * setting of hwthread_state, and vice versa.
3239 */
3240 smp_mb();
3241 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
3242 if (--timeout <= 0) {
3243 pr_err("KVM: couldn't grab cpu %d\n", cpu);
3244 return -EBUSY;
3245 }
3246 udelay(1);
3247 }
3248 return 0;
3249}
3250
3251static void kvmppc_release_hwthread(int cpu)
3252{
3253 struct paca_struct *tpaca;
3254
Nicholas Piggind2e60072018-02-14 01:08:12 +10003255 tpaca = paca_ptrs[cpu];
Paul Mackerras31a4d442017-10-19 15:14:20 +11003256 tpaca->kvm_hstate.hwthread_req = 0;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003257 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003258 tpaca->kvm_hstate.kvm_vcore = NULL;
3259 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003260}
3261
Nicholas Piggin434398a2021-11-23 19:52:27 +10003262static DEFINE_PER_CPU(struct kvm *, cpu_in_guest);
3263
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003264static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
3265{
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003266 struct kvm_nested_guest *nested = vcpu->arch.nested;
Nicholas Piggin434398a2021-11-23 19:52:27 +10003267 cpumask_t *need_tlb_flush;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003268 int i;
3269
Nicholas Piggin434398a2021-11-23 19:52:27 +10003270 if (nested)
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003271 need_tlb_flush = &nested->need_tlb_flush;
Nicholas Piggin434398a2021-11-23 19:52:27 +10003272 else
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003273 need_tlb_flush = &kvm->arch.need_tlb_flush;
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003274
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003275 cpu = cpu_first_tlb_thread_sibling(cpu);
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003276 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
3277 i += cpu_tlb_thread_sibling_step())
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003278 cpumask_set_cpu(i, need_tlb_flush);
3279
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003280 /*
Nicholas Piggin434398a2021-11-23 19:52:27 +10003281 * Make sure setting of bit in need_tlb_flush precedes testing of
3282 * cpu_in_guest. The matching barrier on the other side is hwsync
3283 * when switching to guest MMU mode, which happens between
3284 * cpu_in_guest being set to the guest kvm, and need_tlb_flush bit
3285 * being tested.
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003286 */
3287 smp_mb();
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003288
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003289 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
Nicholas Piggin434398a2021-11-23 19:52:27 +10003290 i += cpu_tlb_thread_sibling_step()) {
3291 struct kvm *running = *per_cpu_ptr(&cpu_in_guest, i);
3292
3293 if (running == kvm)
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003294 smp_call_function_single(i, do_nothing, NULL, 1);
Nicholas Piggin434398a2021-11-23 19:52:27 +10003295 }
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003296}
3297
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003298static void do_migrate_away_vcpu(void *arg)
3299{
3300 struct kvm_vcpu *vcpu = arg;
3301 struct kvm *kvm = vcpu->kvm;
3302
3303 /*
3304 * If the guest has GTSE, it may execute tlbie, so do a eieio; tlbsync;
3305 * ptesync sequence on the old CPU before migrating to a new one, in
3306 * case we interrupted the guest between a tlbie ; eieio ;
3307 * tlbsync; ptesync sequence.
3308 *
3309 * Otherwise, ptesync is sufficient for ordering tlbiel sequences.
3310 */
3311 if (kvm->arch.lpcr & LPCR_GTSE)
3312 asm volatile("eieio; tlbsync; ptesync");
3313 else
3314 asm volatile("ptesync");
Paul Mackerras371fefd2011-06-29 00:23:08 +00003315}
3316
Paul Mackerras8b24e692017-06-26 15:45:51 +10003317static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
3318{
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003319 struct kvm_nested_guest *nested = vcpu->arch.nested;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003320 struct kvm *kvm = vcpu->kvm;
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003321 int prev_cpu;
3322
3323 if (!cpu_has_feature(CPU_FTR_HVMODE))
3324 return;
3325
3326 if (nested)
3327 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
3328 else
3329 prev_cpu = vcpu->arch.prev_cpu;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003330
3331 /*
3332 * With radix, the guest can do TLB invalidations itself,
3333 * and it could choose to use the local form (tlbiel) if
3334 * it is invalidating a translation that has only ever been
3335 * used on one vcpu. However, that doesn't mean it has
3336 * only ever been used on one physical cpu, since vcpus
3337 * can move around between pcpus. To cope with this, when
3338 * a vcpu moves from one pcpu to another, we need to tell
3339 * any vcpus running on the same core as this vcpu previously
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003340 * ran to flush the TLB.
Paul Mackerras8b24e692017-06-26 15:45:51 +10003341 */
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003342 if (prev_cpu != pcpu) {
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003343 if (prev_cpu >= 0) {
3344 if (cpu_first_tlb_thread_sibling(prev_cpu) !=
3345 cpu_first_tlb_thread_sibling(pcpu))
3346 radix_flush_cpu(kvm, prev_cpu, vcpu);
3347
3348 smp_call_function_single(prev_cpu,
3349 do_migrate_away_vcpu, vcpu, 1);
3350 }
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003351 if (nested)
3352 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
3353 else
3354 vcpu->arch.prev_cpu = pcpu;
3355 }
3356}
3357
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003358static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003359{
3360 int cpu;
3361 struct paca_struct *tpaca;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003362
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003363 cpu = vc->pcpu;
3364 if (vcpu) {
3365 if (vcpu->arch.timer_running) {
3366 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
3367 vcpu->arch.timer_running = 0;
3368 }
3369 cpu += vcpu->arch.ptid;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003370 vcpu->cpu = vc->pcpu;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003371 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10003372 }
Nicholas Piggind2e60072018-02-14 01:08:12 +10003373 tpaca = paca_ptrs[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003374 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003375 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003376 tpaca->kvm_hstate.fake_suspend = 0;
Paul Mackerrasec257162015-06-24 21:18:03 +10003377 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10003378 smp_wmb();
Paul Mackerras898b25b2017-06-22 15:08:42 +10003379 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003380 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11003381 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003382}
3383
Paul Mackerras516f7892017-10-16 16:11:57 +11003384static void kvmppc_wait_for_nap(int n_threads)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003385{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003386 int cpu = smp_processor_id();
3387 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003388
Paul Mackerras45c940b2016-11-18 17:43:30 +11003389 if (n_threads <= 1)
3390 return;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003391 for (loops = 0; loops < 1000000; ++loops) {
3392 /*
3393 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003394 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003395 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003396 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003397 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11003398 for (i = 1; i < n_threads; ++i)
Nicholas Piggind2e60072018-02-14 01:08:12 +10003399 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003400 break;
Paul Mackerras45c940b2016-11-18 17:43:30 +11003401 if (i == n_threads) {
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003402 HMT_medium();
3403 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003404 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003405 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00003406 }
3407 HMT_medium();
Paul Mackerras45c940b2016-11-18 17:43:30 +11003408 for (i = 1; i < n_threads; ++i)
Nicholas Piggind2e60072018-02-14 01:08:12 +10003409 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003410 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003411}
3412
3413/*
3414 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00003415 * this core are off-line. Then grab the threads so they can't
3416 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00003417 */
3418static int on_primary_thread(void)
3419{
3420 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10003421 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003422
Michael Ellerman3102f782014-05-23 18:15:29 +10003423 /* Are we on a primary subcore? */
3424 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00003425 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10003426
3427 thr = 0;
3428 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003429 if (cpu_online(cpu + thr))
3430 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00003431
3432 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10003433 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00003434 if (kvmppc_grab_hwthread(cpu + thr)) {
3435 /* Couldn't grab one; let the others go */
3436 do {
3437 kvmppc_release_hwthread(cpu + thr);
3438 } while (--thr > 0);
3439 return 0;
3440 }
3441 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003442 return 1;
3443}
3444
Paul Mackerrasec257162015-06-24 21:18:03 +10003445/*
3446 * A list of virtual cores for each physical CPU.
3447 * These are vcores that could run but their runner VCPU tasks are
3448 * (or may be) preempted.
3449 */
3450struct preempted_vcore_list {
3451 struct list_head list;
3452 spinlock_t lock;
3453};
3454
3455static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
3456
3457static void init_vcore_lists(void)
3458{
3459 int cpu;
3460
3461 for_each_possible_cpu(cpu) {
3462 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
3463 spin_lock_init(&lp->lock);
3464 INIT_LIST_HEAD(&lp->list);
3465 }
3466}
3467
3468static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
3469{
3470 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3471
Nicholas Pigginecb6a722021-11-23 19:52:28 +10003472 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3473
Paul Mackerrasec257162015-06-24 21:18:03 +10003474 vc->vcore_state = VCORE_PREEMPT;
3475 vc->pcpu = smp_processor_id();
Paul Mackerras516f7892017-10-16 16:11:57 +11003476 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003477 spin_lock(&lp->lock);
3478 list_add_tail(&vc->preempt_list, &lp->list);
3479 spin_unlock(&lp->lock);
3480 }
3481
3482 /* Start accumulating stolen time */
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003483 kvmppc_core_start_stolen(vc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003484}
3485
3486static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
3487{
Paul Mackerras402813f2015-07-16 17:11:13 +10003488 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10003489
Nicholas Pigginecb6a722021-11-23 19:52:28 +10003490 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3491
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003492 kvmppc_core_end_stolen(vc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003493 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10003494 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10003495 spin_lock(&lp->lock);
3496 list_del_init(&vc->preempt_list);
3497 spin_unlock(&lp->lock);
3498 }
3499 vc->vcore_state = VCORE_INACTIVE;
3500}
3501
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003502/*
3503 * This stores information about the virtual cores currently
3504 * assigned to a physical core.
3505 */
Paul Mackerrasec257162015-06-24 21:18:03 +10003506struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003507 int n_subcores;
3508 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003509 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003510 int subcore_threads[MAX_SUBCORES];
Paul Mackerras898b25b2017-06-22 15:08:42 +10003511 struct kvmppc_vcore *vc[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10003512};
3513
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003514/*
3515 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
Paul Mackerras516f7892017-10-16 16:11:57 +11003516 * respectively in 2-way micro-threading (split-core) mode on POWER8.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003517 */
3518static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
3519
Paul Mackerrasec257162015-06-24 21:18:03 +10003520static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
3521{
3522 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003523 cip->n_subcores = 1;
3524 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003525 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003526 cip->subcore_threads[0] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003527 cip->vc[0] = vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003528}
3529
3530static bool subcore_config_ok(int n_subcores, int n_threads)
3531{
Paul Mackerras516f7892017-10-16 16:11:57 +11003532 /*
Paul Mackerras00608e12018-01-11 16:54:26 +11003533 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
3534 * split-core mode, with one thread per subcore.
Paul Mackerras516f7892017-10-16 16:11:57 +11003535 */
3536 if (cpu_has_feature(CPU_FTR_ARCH_300))
3537 return n_subcores <= 4 && n_threads == 1;
3538
3539 /* On POWER8, can only dynamically split if unsplit to begin with */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003540 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
3541 return false;
3542 if (n_subcores > MAX_SUBCORES)
3543 return false;
3544 if (n_subcores > 1) {
3545 if (!(dynamic_mt_modes & 2))
3546 n_subcores = 4;
3547 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
3548 return false;
3549 }
3550
3551 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10003552}
3553
Paul Mackerras898b25b2017-06-22 15:08:42 +10003554static void init_vcore_to_run(struct kvmppc_vcore *vc)
Paul Mackerrasec257162015-06-24 21:18:03 +10003555{
Paul Mackerrasec257162015-06-24 21:18:03 +10003556 vc->entry_exit_map = 0;
3557 vc->in_guest = 0;
3558 vc->napping_threads = 0;
3559 vc->conferring_threads = 0;
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003560 vc->tb_offset_applied = 0;
Paul Mackerrasec257162015-06-24 21:18:03 +10003561}
3562
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003563static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
3564{
3565 int n_threads = vc->num_threads;
3566 int sub;
3567
3568 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
3569 return false;
3570
Paul Mackerrasaa227862018-09-12 10:42:12 +10003571 /* In one_vm_per_core mode, require all vcores to be from the same vm */
3572 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
3573 return false;
3574
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003575 if (n_threads < cip->max_subcore_threads)
3576 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10003577 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003578 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10003579 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003580
3581 sub = cip->n_subcores;
3582 ++cip->n_subcores;
3583 cip->total_threads += vc->num_threads;
3584 cip->subcore_threads[sub] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003585 cip->vc[sub] = vc;
3586 init_vcore_to_run(vc);
3587 list_del_init(&vc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003588
3589 return true;
3590}
3591
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003592/*
3593 * Work out whether it is possible to piggyback the execution of
3594 * vcore *pvc onto the execution of the other vcores described in *cip.
3595 */
3596static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
3597 int target_threads)
3598{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003599 if (cip->total_threads + pvc->num_threads > target_threads)
3600 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003601
Paul Mackerrasb0090312016-09-15 16:27:41 +10003602 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003603}
3604
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003605static void prepare_threads(struct kvmppc_vcore *vc)
3606{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003607 int i;
3608 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003609
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003610 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003611 if (signal_pending(vcpu->arch.run_task))
3612 vcpu->arch.ret = -EINTR;
3613 else if (vcpu->arch.vpa.update_pending ||
3614 vcpu->arch.slb_shadow.update_pending ||
3615 vcpu->arch.dtl.update_pending)
3616 vcpu->arch.ret = RESUME_GUEST;
3617 else
3618 continue;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003619 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003620 wake_up(&vcpu->arch.cpu_run);
3621 }
3622}
3623
Paul Mackerrasec257162015-06-24 21:18:03 +10003624static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003625{
Paul Mackerrasec257162015-06-24 21:18:03 +10003626 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3627 struct kvmppc_vcore *pvc, *vcnext;
3628
3629 spin_lock(&lp->lock);
3630 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
3631 if (!spin_trylock(&pvc->lock))
3632 continue;
3633 prepare_threads(pvc);
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003634 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003635 list_del_init(&pvc->preempt_list);
3636 if (pvc->runner == NULL) {
3637 pvc->vcore_state = VCORE_INACTIVE;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003638 kvmppc_core_end_stolen(pvc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003639 }
3640 spin_unlock(&pvc->lock);
3641 continue;
3642 }
3643 if (!can_piggyback(pvc, cip, target_threads)) {
3644 spin_unlock(&pvc->lock);
3645 continue;
3646 }
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003647 kvmppc_core_end_stolen(pvc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003648 pvc->vcore_state = VCORE_PIGGYBACK;
3649 if (cip->total_threads >= target_threads)
3650 break;
3651 }
3652 spin_unlock(&lp->lock);
3653}
3654
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003655static bool recheck_signals_and_mmu(struct core_info *cip)
Paul Mackerras8b24e692017-06-26 15:45:51 +10003656{
3657 int sub, i;
3658 struct kvm_vcpu *vcpu;
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003659 struct kvmppc_vcore *vc;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003660
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003661 for (sub = 0; sub < cip->n_subcores; ++sub) {
3662 vc = cip->vc[sub];
3663 if (!vc->kvm->arch.mmu_ready)
3664 return true;
3665 for_each_runnable_thread(i, vcpu, vc)
Paul Mackerras8b24e692017-06-26 15:45:51 +10003666 if (signal_pending(vcpu->arch.run_task))
3667 return true;
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003668 }
Paul Mackerras8b24e692017-06-26 15:45:51 +10003669 return false;
3670}
3671
Paul Mackerrasec257162015-06-24 21:18:03 +10003672static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
3673{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003674 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003675 u64 now;
3676 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003677 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003678
Paul Mackerrasec257162015-06-24 21:18:03 +10003679 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003680 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003681 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras53655dd2018-10-08 16:30:54 +11003682 /*
3683 * It's safe to unlock the vcore in the loop here, because
3684 * for_each_runnable_thread() is safe against removal of
3685 * the vcpu, and the vcore state is VCORE_EXITING here,
3686 * so any vcpus becoming runnable will have their arch.trap
3687 * set to zero and can't actually run in the guest.
3688 */
3689 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003690 /* cancel pending dec exception if dec is positive */
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10003691 if (now < kvmppc_dec_expires_host_tb(vcpu) &&
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003692 kvmppc_core_pending_dec(vcpu))
3693 kvmppc_core_dequeue_dec(vcpu);
3694
3695 trace_kvm_guest_exit(vcpu);
3696
3697 ret = RESUME_GUEST;
3698 if (vcpu->arch.trap)
Tianjia Zhang8c99d342020-04-27 12:35:11 +08003699 ret = kvmppc_handle_exit_hv(vcpu,
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003700 vcpu->arch.run_task);
3701
3702 vcpu->arch.ret = ret;
3703 vcpu->arch.trap = 0;
3704
Paul Mackerras53655dd2018-10-08 16:30:54 +11003705 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10003706 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
3707 if (vcpu->arch.pending_exceptions)
3708 kvmppc_core_prepare_to_enter(vcpu);
3709 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003710 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10003711 else
3712 ++still_running;
3713 } else {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003714 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003715 wake_up(&vcpu->arch.cpu_run);
3716 }
3717 }
Paul Mackerrasec257162015-06-24 21:18:03 +10003718 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10003719 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003720 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10003721 } else if (vc->runner) {
3722 vc->vcore_state = VCORE_PREEMPT;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003723 kvmppc_core_start_stolen(vc, mftb());
Paul Mackerras563a1e92015-07-16 17:11:14 +10003724 } else {
3725 vc->vcore_state = VCORE_INACTIVE;
3726 }
Paul Mackerrasec257162015-06-24 21:18:03 +10003727 if (vc->n_runnable > 0 && vc->runner == NULL) {
3728 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003729 i = -1;
3730 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10003731 wake_up(&vcpu->arch.cpu_run);
3732 }
3733 }
3734 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003735}
3736
Paul Mackerras371fefd2011-06-29 00:23:08 +00003737/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003738 * Clear core from the list of active host cores as we are about to
3739 * enter the guest. Only do this if it is the primary thread of the
3740 * core (not if a subcore) that is entering the guest.
3741 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003742static inline int kvmppc_clear_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003743{
3744 int core;
3745
3746 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003747 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003748 /*
3749 * Memory barrier can be omitted here as we will do a smp_wmb()
3750 * later in kvmppc_start_thread and we need ensure that state is
3751 * visible to other CPUs only after we enter guest.
3752 */
3753 core = cpu >> threads_shift;
3754 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003755 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003756}
3757
3758/*
3759 * Advertise this core as an active host core since we exited the guest
3760 * Only need to do this if it is the primary thread of the core that is
3761 * exiting.
3762 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003763static inline int kvmppc_set_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003764{
3765 int core;
3766
3767 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003768 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003769
3770 /*
3771 * Memory barrier can be omitted here because we do a spin_unlock
3772 * immediately after this which provides the memory barrier.
3773 */
3774 core = cpu >> threads_shift;
3775 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003776 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003777}
3778
Paul Mackerras8b24e692017-06-26 15:45:51 +10003779static void set_irq_happened(int trap)
3780{
3781 switch (trap) {
3782 case BOOK3S_INTERRUPT_EXTERNAL:
3783 local_paca->irq_happened |= PACA_IRQ_EE;
3784 break;
3785 case BOOK3S_INTERRUPT_H_DOORBELL:
3786 local_paca->irq_happened |= PACA_IRQ_DBELL;
3787 break;
3788 case BOOK3S_INTERRUPT_HMI:
3789 local_paca->irq_happened |= PACA_IRQ_HMI;
3790 break;
Nicholas Piggin6de66382017-11-05 23:33:55 +11003791 case BOOK3S_INTERRUPT_SYSTEM_RESET:
3792 replay_system_reset();
3793 break;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003794 }
3795}
3796
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003797/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00003798 * Run a set of guest threads on a physical core.
3799 * Called with vc->lock held.
3800 */
Paul Mackerras66feed62015-03-28 14:21:12 +11003801static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003802{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003803 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003804 int i;
Paul Mackerras2c9097e42012-09-11 13:27:01 +00003805 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10003806 struct core_info core_info;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003807 struct kvmppc_vcore *pvc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003808 struct kvm_split_mode split_info, *sip;
3809 int split, subcore_size, active;
3810 int sub;
3811 bool thr0_done;
3812 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10003813 int pcpu, thr;
3814 int target_threads;
Paul Mackerras45c940b2016-11-18 17:43:30 +11003815 int controlled_threads;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003816 int trap;
Paul Mackerras516f7892017-10-16 16:11:57 +11003817 bool is_power8;
Paul Mackerras081f3232012-06-01 20:20:24 +10003818
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10003819 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
3820 return;
3821
Paul Mackerras081f3232012-06-01 20:20:24 +10003822 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003823 * Remove from the list any threads that have a signal pending
3824 * or need a VPA update done
3825 */
3826 prepare_threads(vc);
3827
3828 /* if the runner is no longer runnable, let the caller pick a new one */
3829 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
3830 return;
3831
3832 /*
3833 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10003834 */
Paul Mackerras898b25b2017-06-22 15:08:42 +10003835 init_vcore_to_run(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11003836 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10003837
3838 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11003839 * Number of threads that we will be controlling: the same as
3840 * the number of threads per subcore, except on POWER9,
3841 * where it's 1 because the threads are (mostly) independent.
3842 */
Paul Mackerras516f7892017-10-16 16:11:57 +11003843 controlled_threads = threads_per_vcore(vc->kvm);
Paul Mackerras45c940b2016-11-18 17:43:30 +11003844
3845 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10003846 * Make sure we are running on primary threads, and that secondary
3847 * threads are offline. Also check if the number of threads in this
3848 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00003849 */
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003850 if ((controlled_threads > 1) &&
3851 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003852 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00003853 vcpu->arch.ret = -EBUSY;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003854 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003855 wake_up(&vcpu->arch.cpu_run);
3856 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00003857 goto out;
3858 }
3859
Paul Mackerrasec257162015-06-24 21:18:03 +10003860 /*
3861 * See if we could run any other vcores on the physical core
3862 * along with this one.
3863 */
3864 init_core_info(&core_info, vc);
3865 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11003866 target_threads = controlled_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003867 if (target_smt_mode && target_smt_mode < target_threads)
3868 target_threads = target_smt_mode;
3869 if (vc->num_threads < target_threads)
3870 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10003871
Paul Mackerras8b24e692017-06-26 15:45:51 +10003872 /*
Paul Mackerras8b24e692017-06-26 15:45:51 +10003873 * Hard-disable interrupts, and check resched flag and signals.
3874 * If we need to reschedule or deliver a signal, clean up
3875 * and return without going into the guest(s).
Paul Mackerras072df812017-11-09 14:30:24 +11003876 * If the mmu_ready flag has been cleared, don't go into the
Paul Mackerras38c53af2017-11-08 14:44:04 +11003877 * guest because that means a HPT resize operation is in progress.
Paul Mackerras8b24e692017-06-26 15:45:51 +10003878 */
3879 local_irq_disable();
3880 hard_irq_disable();
3881 if (lazy_irq_pending() || need_resched() ||
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003882 recheck_signals_and_mmu(&core_info)) {
Paul Mackerras8b24e692017-06-26 15:45:51 +10003883 local_irq_enable();
3884 vc->vcore_state = VCORE_INACTIVE;
3885 /* Unlock all except the primary vcore */
3886 for (sub = 1; sub < core_info.n_subcores; ++sub) {
3887 pvc = core_info.vc[sub];
3888 /* Put back on to the preempted vcores list */
3889 kvmppc_vcore_preempt(pvc);
3890 spin_unlock(&pvc->lock);
3891 }
3892 for (i = 0; i < controlled_threads; ++i)
3893 kvmppc_release_hwthread(pcpu + i);
3894 return;
3895 }
3896
3897 kvmppc_clear_host_core(pcpu);
3898
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003899 /* Decide on micro-threading (split-core) mode */
3900 subcore_size = threads_per_subcore;
3901 cmd_bit = stat_bit = 0;
3902 split = core_info.n_subcores;
3903 sip = NULL;
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10003904 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
Paul Mackerras516f7892017-10-16 16:11:57 +11003905
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003906 if (split > 1) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003907 sip = &split_info;
3908 memset(&split_info, 0, sizeof(split_info));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003909 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10003910 split_info.vc[sub] = core_info.vc[sub];
Paul Mackerras516f7892017-10-16 16:11:57 +11003911
3912 if (is_power8) {
3913 if (split == 2 && (dynamic_mt_modes & 2)) {
3914 cmd_bit = HID0_POWER8_1TO2LPAR;
3915 stat_bit = HID0_POWER8_2LPARMODE;
3916 } else {
3917 split = 4;
3918 cmd_bit = HID0_POWER8_1TO4LPAR;
3919 stat_bit = HID0_POWER8_4LPARMODE;
3920 }
3921 subcore_size = MAX_SMT_THREADS / split;
3922 split_info.rpr = mfspr(SPRN_RPR);
3923 split_info.pmmar = mfspr(SPRN_PMMAR);
3924 split_info.ldbar = mfspr(SPRN_LDBAR);
3925 split_info.subcore_size = subcore_size;
3926 } else {
3927 split_info.subcore_size = 1;
3928 }
3929
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003930 /* order writes to split_info before kvm_split_mode pointer */
3931 smp_wmb();
3932 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003933
Paul Mackerrasc0101502017-10-19 14:11:23 +11003934 for (thr = 0; thr < controlled_threads; ++thr) {
Nicholas Piggind2e60072018-02-14 01:08:12 +10003935 struct paca_struct *paca = paca_ptrs[pcpu + thr];
3936
Nicholas Piggind2e60072018-02-14 01:08:12 +10003937 paca->kvm_hstate.napping = 0;
3938 paca->kvm_hstate.kvm_split_mode = sip;
Paul Mackerrasc0101502017-10-19 14:11:23 +11003939 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003940
Paul Mackerras516f7892017-10-16 16:11:57 +11003941 /* Initiate micro-threading (split-core) on POWER8 if required */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003942 if (cmd_bit) {
3943 unsigned long hid0 = mfspr(SPRN_HID0);
3944
3945 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
3946 mb();
3947 mtspr(SPRN_HID0, hid0);
3948 isync();
3949 for (;;) {
3950 hid0 = mfspr(SPRN_HID0);
3951 if (hid0 & stat_bit)
3952 break;
3953 cpu_relax();
3954 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00003955 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003956
Paul Mackerras7aa15842018-04-20 19:53:22 +10003957 /*
3958 * On POWER8, set RWMR register.
3959 * Since it only affects PURR and SPURR, it doesn't affect
3960 * the host, so we don't save/restore the host value.
3961 */
3962 if (is_power8) {
3963 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
3964 int n_online = atomic_read(&vc->online_count);
3965
3966 /*
3967 * Use the 8-thread value if we're doing split-core
3968 * or if the vcore's online count looks bogus.
3969 */
3970 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
3971 n_online >= 1 && n_online <= MAX_SMT_THREADS)
3972 rwmr_val = p8_rwmr_values[n_online];
3973 mtspr(SPRN_RWMR, rwmr_val);
3974 }
3975
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003976 /* Start all the threads */
3977 active = 0;
3978 for (sub = 0; sub < core_info.n_subcores; ++sub) {
Paul Mackerras516f7892017-10-16 16:11:57 +11003979 thr = is_power8 ? subcore_thread_map[sub] : sub;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003980 thr0_done = false;
3981 active |= 1 << thr;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003982 pvc = core_info.vc[sub];
3983 pvc->pcpu = pcpu + thr;
3984 for_each_runnable_thread(i, vcpu, pvc) {
Nicholas Pigginc7fa8482022-03-03 15:33:10 +10003985 /*
3986 * XXX: is kvmppc_start_thread called too late here?
3987 * It updates vcpu->cpu and vcpu->arch.thread_cpu
3988 * which are used by kvmppc_fast_vcpu_kick_hv(), but
3989 * kick is called after new exceptions become available
3990 * and exceptions are checked earlier than here, by
3991 * kvmppc_core_prepare_to_enter.
3992 */
Paul Mackerras898b25b2017-06-22 15:08:42 +10003993 kvmppc_start_thread(vcpu, pvc);
Nicholas Piggine4335f52022-09-08 23:25:45 +10003994 kvmppc_update_vpa_dispatch(vcpu, pvc);
Paul Mackerras898b25b2017-06-22 15:08:42 +10003995 trace_kvm_guest_enter(vcpu);
3996 if (!vcpu->arch.ptid)
3997 thr0_done = true;
3998 active |= 1 << (thr + vcpu->arch.ptid);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003999 }
Paul Mackerras898b25b2017-06-22 15:08:42 +10004000 /*
4001 * We need to start the first thread of each subcore
4002 * even if it doesn't have a vcpu.
4003 */
4004 if (!thr0_done)
4005 kvmppc_start_thread(NULL, pvc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004006 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05304007
4008 /*
4009 * Ensure that split_info.do_nap is set after setting
4010 * the vcore pointer in the PACA of the secondaries.
4011 */
4012 smp_mb();
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05304013
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004014 /*
4015 * When doing micro-threading, poke the inactive threads as well.
4016 * This gets them to the nap instruction after kvm_do_nap,
4017 * which reduces the time taken to unsplit later.
4018 */
Nicholas Pigginb1b16972021-01-18 16:28:06 +10004019 if (cmd_bit) {
Paul Mackerras516f7892017-10-16 16:11:57 +11004020 split_info.do_nap = 1; /* ask secondaries to nap when done */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004021 for (thr = 1; thr < threads_per_subcore; ++thr)
4022 if (!(active & (1 << thr)))
4023 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerras516f7892017-10-16 16:11:57 +11004024 }
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11004025
Paul Mackerras2f12f032012-10-15 01:17:17 +00004026 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004027 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004028
4029 trace_kvmppc_run_core(vc, 0);
4030
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004031 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10004032 spin_unlock(&core_info.vc[sub]->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00004033
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004034 guest_timing_enter_irqoff();
Paul Mackerras2c9097e42012-09-11 13:27:01 +00004035
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11004036 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e42012-09-11 13:27:01 +00004037
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004038 guest_state_enter_irqoff();
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +05304039 this_cpu_disable_ftrace();
4040
Paul Mackerras8b24e692017-06-26 15:45:51 +10004041 trap = __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10004042
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +05304043 this_cpu_enable_ftrace();
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004044 guest_state_exit_irqoff();
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +05304045
Paul Mackerrasec257162015-06-24 21:18:03 +10004046 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
4047
Paul Mackerras8b24e692017-06-26 15:45:51 +10004048 set_irq_happened(trap);
4049
Paul Mackerrasec257162015-06-24 21:18:03 +10004050 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004051 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10004052 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004053
Paul Mackerras371fefd2011-06-29 00:23:08 +00004054 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras516f7892017-10-16 16:11:57 +11004055 kvmppc_wait_for_nap(controlled_threads);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004056
4057 /* Return to whole-core mode if we split the core earlier */
Paul Mackerras516f7892017-10-16 16:11:57 +11004058 if (cmd_bit) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004059 unsigned long hid0 = mfspr(SPRN_HID0);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004060
4061 hid0 &= ~HID0_POWER8_DYNLPARDIS;
4062 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
4063 mb();
4064 mtspr(SPRN_HID0, hid0);
4065 isync();
4066 for (;;) {
4067 hid0 = mfspr(SPRN_HID0);
4068 if (!(hid0 & stat_bit))
4069 break;
4070 cpu_relax();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004071 }
Nicholas Pigginb1b16972021-01-18 16:28:06 +10004072 split_info.do_nap = 0;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004073 }
4074
Paul Mackerras8b24e692017-06-26 15:45:51 +10004075 kvmppc_set_host_core(pcpu);
4076
Laurent Vivier235cee12021-10-28 00:21:50 +10004077 if (!vtime_accounting_enabled_this_cpu()) {
4078 local_irq_enable();
4079 /*
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004080 * Service IRQs here before guest_timing_exit_irqoff() so any
Laurent Vivier235cee12021-10-28 00:21:50 +10004081 * ticks that occurred while running the guest are accounted to
4082 * the guest. If vtime accounting is enabled, accounting uses
4083 * TB rather than ticks, so it can be done without enabling
4084 * interrupts here, which has the problem that it accounts
4085 * interrupt processing overhead to the host.
4086 */
4087 local_irq_disable();
4088 }
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004089 guest_timing_exit_irqoff();
Nicholas Piggin1126652862021-01-30 23:08:12 +10004090
Paul Mackerras8b24e692017-06-26 15:45:51 +10004091 local_irq_enable();
4092
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004093 /* Let secondaries go back to the offline loop */
Paul Mackerras45c940b2016-11-18 17:43:30 +11004094 for (i = 0; i < controlled_threads; ++i) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004095 kvmppc_release_hwthread(pcpu + i);
4096 if (sip && sip->napped[i])
4097 kvmppc_ipi_thread(pcpu + i);
4098 }
4099
Paul Mackerras371fefd2011-06-29 00:23:08 +00004100 spin_unlock(&vc->lock);
Paul Mackerras2c9097e42012-09-11 13:27:01 +00004101
Paul Mackerras371fefd2011-06-29 00:23:08 +00004102 /* make sure updates to secondary vcpu structs are visible now */
4103 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00004104
Paul Mackerras36ee41d2018-01-30 10:51:32 +11004105 preempt_enable();
4106
Paul Mackerras898b25b2017-06-22 15:08:42 +10004107 for (sub = 0; sub < core_info.n_subcores; ++sub) {
4108 pvc = core_info.vc[sub];
4109 post_guest_process(pvc, pvc == vc);
4110 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00004111
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004112 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00004113
Paul Mackerrasde56a942011-06-29 00:21:34 +00004114 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10004115 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004116 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004117}
4118
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004119static inline bool hcall_is_xics(unsigned long req)
4120{
4121 return req == H_EOI || req == H_CPPR || req == H_IPI ||
4122 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
4123}
4124
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004125static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu)
Paul Mackerras95a64322018-10-08 16:30:55 +11004126{
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004127 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
4128 if (lp) {
Paul Mackerras95a64322018-10-08 16:30:55 +11004129 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
4130 lp->yield_count = cpu_to_be32(yield_count);
4131 vcpu->arch.vpa.dirty = 1;
4132 }
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004133}
Paul Mackerras95a64322018-10-08 16:30:55 +11004134
Gautam Menghanie1f288d2024-05-20 23:27:40 +05304135/* Helper functions for reading L2's stats from L1's VPA */
4136#ifdef CONFIG_PPC_PSERIES
4137static DEFINE_PER_CPU(u64, l1_to_l2_cs);
4138static DEFINE_PER_CPU(u64, l2_to_l1_cs);
4139static DEFINE_PER_CPU(u64, l2_runtime_agg);
4140
4141int kvmhv_get_l2_counters_status(void)
4142{
4143 return firmware_has_feature(FW_FEATURE_LPAR) &&
4144 get_lppaca()->l2_counters_enable;
4145}
4146
4147void kvmhv_set_l2_counters_status(int cpu, bool status)
4148{
4149 if (!firmware_has_feature(FW_FEATURE_LPAR))
4150 return;
4151 if (status)
4152 lppaca_of(cpu).l2_counters_enable = 1;
4153 else
4154 lppaca_of(cpu).l2_counters_enable = 0;
4155}
4156
4157int kmvhv_counters_tracepoint_regfunc(void)
4158{
4159 int cpu;
4160
4161 for_each_present_cpu(cpu) {
4162 kvmhv_set_l2_counters_status(cpu, true);
4163 }
4164 return 0;
4165}
4166
4167void kmvhv_counters_tracepoint_unregfunc(void)
4168{
4169 int cpu;
4170
4171 for_each_present_cpu(cpu) {
4172 kvmhv_set_l2_counters_status(cpu, false);
4173 }
4174}
4175
4176static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
4177{
4178 struct lppaca *lp = get_lppaca();
4179 u64 l1_to_l2_ns, l2_to_l1_ns, l2_runtime_ns;
4180 u64 *l1_to_l2_cs_ptr = this_cpu_ptr(&l1_to_l2_cs);
4181 u64 *l2_to_l1_cs_ptr = this_cpu_ptr(&l2_to_l1_cs);
4182 u64 *l2_runtime_agg_ptr = this_cpu_ptr(&l2_runtime_agg);
4183
4184 l1_to_l2_ns = tb_to_ns(be64_to_cpu(lp->l1_to_l2_cs_tb));
4185 l2_to_l1_ns = tb_to_ns(be64_to_cpu(lp->l2_to_l1_cs_tb));
4186 l2_runtime_ns = tb_to_ns(be64_to_cpu(lp->l2_runtime_tb));
4187 trace_kvmppc_vcpu_stats(vcpu, l1_to_l2_ns - *l1_to_l2_cs_ptr,
4188 l2_to_l1_ns - *l2_to_l1_cs_ptr,
4189 l2_runtime_ns - *l2_runtime_agg_ptr);
4190 *l1_to_l2_cs_ptr = l1_to_l2_ns;
4191 *l2_to_l1_cs_ptr = l2_to_l1_ns;
4192 *l2_runtime_agg_ptr = l2_runtime_ns;
4193}
4194
4195#else
4196int kvmhv_get_l2_counters_status(void)
4197{
4198 return 0;
4199}
4200
4201static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
4202{
4203}
4204#endif
4205
Jordan Niethe19d31c52023-09-14 13:05:59 +10004206static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit,
4207 unsigned long lpcr, u64 *tb)
4208{
4209 struct kvmhv_nestedv2_io *io;
4210 unsigned long msr, i;
4211 int trap;
4212 long rc;
4213
Gautam Menghani54ec2bd2024-06-05 17:09:10 +05304214 if (vcpu->arch.doorbell_request) {
4215 vcpu->arch.doorbell_request = 0;
4216 kvmppc_set_dpdes(vcpu, 1);
4217 }
4218
Jordan Niethe19d31c52023-09-14 13:05:59 +10004219 io = &vcpu->arch.nestedv2_io;
4220
4221 msr = mfmsr();
4222 kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
4223 if (lazy_irq_pending())
4224 return 0;
4225
4226 rc = kvmhv_nestedv2_flush_vcpu(vcpu, time_limit);
4227 if (rc < 0)
4228 return -EINVAL;
4229
Jordan Nietheec0f6632023-12-01 18:56:10 +05304230 kvmppc_gse_put_u64(io->vcpu_run_input, KVMPPC_GSID_LPCR, lpcr);
4231
Jordan Niethe19d31c52023-09-14 13:05:59 +10004232 accumulate_time(vcpu, &vcpu->arch.in_guest);
4233 rc = plpar_guest_run_vcpu(0, vcpu->kvm->arch.lpid, vcpu->vcpu_id,
4234 &trap, &i);
4235
4236 if (rc != H_SUCCESS) {
4237 pr_err("KVM Guest Run VCPU hcall failed\n");
4238 if (rc == H_INVALID_ELEMENT_ID)
4239 pr_err("KVM: Guest Run VCPU invalid element id at %ld\n", i);
4240 else if (rc == H_INVALID_ELEMENT_SIZE)
4241 pr_err("KVM: Guest Run VCPU invalid element size at %ld\n", i);
4242 else if (rc == H_INVALID_ELEMENT_VALUE)
4243 pr_err("KVM: Guest Run VCPU invalid element value at %ld\n", i);
4244 return -EINVAL;
4245 }
4246 accumulate_time(vcpu, &vcpu->arch.guest_exit);
4247
4248 *tb = mftb();
4249 kvmppc_gsm_reset(io->vcpu_message);
4250 kvmppc_gsm_reset(io->vcore_message);
4251 kvmppc_gsbm_zero(&io->valids);
4252
4253 rc = kvmhv_nestedv2_parse_output(vcpu);
4254 if (rc < 0)
4255 return -EINVAL;
4256
4257 timer_rearm_host_dec(*tb);
4258
Gautam Menghanie1f288d2024-05-20 23:27:40 +05304259 /* Record context switch and guest_run_time data */
4260 if (kvmhv_get_l2_counters_status())
4261 do_trace_nested_cs_time(vcpu);
4262
Jordan Niethe19d31c52023-09-14 13:05:59 +10004263 return trap;
4264}
4265
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004266/* call our hypervisor to load up HV regs and go */
4267static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb)
4268{
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004269 unsigned long host_psscr;
Nicholas Piggind5f48012021-11-23 19:52:09 +10004270 unsigned long msr;
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004271 struct hv_guest_state hvregs;
Nicholas Piggind5f48012021-11-23 19:52:09 +10004272 struct p9_host_os_sprs host_os_sprs;
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004273 s64 dec;
Nicholas Piggind5f48012021-11-23 19:52:09 +10004274 int trap;
Paul Mackerras95a64322018-10-08 16:30:55 +11004275
Nicholas Pigginb49c65c2021-11-23 19:52:23 +10004276 msr = mfmsr();
Paul Mackerras95a64322018-10-08 16:30:55 +11004277
Nicholas Piggind5f48012021-11-23 19:52:09 +10004278 save_p9_host_os_sprs(&host_os_sprs);
Paul Mackerras95a64322018-10-08 16:30:55 +11004279
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10004280 /*
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004281 * We need to save and restore the guest visible part of the
4282 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
4283 * doesn't do this for us. Note only required if pseries since
4284 * this is done in kvmhv_vcpu_entry_p9() below otherwise.
4285 */
4286 host_psscr = mfspr(SPRN_PSSCR_PR);
Nicholas Piggind5f48012021-11-23 19:52:09 +10004287
Nicholas Pigginb49c65c2021-11-23 19:52:23 +10004288 kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
Nicholas Piggind5f48012021-11-23 19:52:09 +10004289 if (lazy_irq_pending())
4290 return 0;
4291
Nicholas Piggind5f48012021-11-23 19:52:09 +10004292 if (unlikely(load_vcpu_state(vcpu, &host_os_sprs)))
4293 msr = mfmsr(); /* TM restore can update msr */
4294
Nicholas Piggina089a682021-11-23 19:52:19 +10004295 if (vcpu->arch.psscr != host_psscr)
4296 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
4297
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004298 kvmhv_save_hv_regs(vcpu, &hvregs);
4299 hvregs.lpcr = lpcr;
Fabiano Rosas1d1cd0f2022-04-25 11:21:51 -03004300 hvregs.amor = ~0;
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004301 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
4302 hvregs.version = HV_GUEST_STATE_VERSION;
4303 if (vcpu->arch.nested) {
4304 hvregs.lpid = vcpu->arch.nested->shadow_lpid;
4305 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
4306 } else {
4307 hvregs.lpid = vcpu->kvm->arch.lpid;
4308 hvregs.vcpu_token = vcpu->vcpu_id;
4309 }
4310 hvregs.hdec_expiry = time_limit;
4311
4312 /*
4313 * When setting DEC, we must always deal with irq_work_raise
4314 * via NMI vs setting DEC. The problem occurs right as we
4315 * switch into guest mode if a NMI hits and sets pending work
4316 * and sets DEC, then that will apply to the guest and not
4317 * bring us back to the host.
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10004318 *
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004319 * irq_work_raise could check a flag (or possibly LPCR[HDICE]
4320 * for example) and set HDEC to 1? That wouldn't solve the
4321 * nested hv case which needs to abort the hcall or zero the
4322 * time limit.
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10004323 *
4324 * XXX: Another day's problem.
4325 */
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004326 mtspr(SPRN_DEC, kvmppc_dec_expires_host_tb(vcpu) - *tb);
4327
4328 mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
4329 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
Nicholas Piggin3e7b3372021-11-23 19:52:11 +10004330 switch_pmu_to_guest(vcpu, &host_os_sprs);
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03004331 accumulate_time(vcpu, &vcpu->arch.in_guest);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004332 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
4333 __pa(&vcpu->arch.regs));
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03004334 accumulate_time(vcpu, &vcpu->arch.guest_exit);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004335 kvmhv_restore_hv_return_state(vcpu, &hvregs);
Nicholas Piggin3e7b3372021-11-23 19:52:11 +10004336 switch_pmu_to_host(vcpu, &host_os_sprs);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004337 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
4338 vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
4339 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
4340 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004341
Nicholas Piggind5f48012021-11-23 19:52:09 +10004342 store_vcpu_state(vcpu);
4343
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004344 dec = mfspr(SPRN_DEC);
4345 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
4346 dec = (s32) dec;
4347 *tb = mftb();
Jordan Niethec8ae9b32023-09-14 13:05:54 +10004348 vcpu->arch.dec_expires = dec + (*tb + kvmppc_get_tb_offset(vcpu));
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004349
Nicholas Piggind5f48012021-11-23 19:52:09 +10004350 timer_rearm_host_dec(*tb);
4351
4352 restore_p9_host_os_sprs(vcpu, &host_os_sprs);
Nicholas Piggina089a682021-11-23 19:52:19 +10004353 if (vcpu->arch.psscr != host_psscr)
4354 mtspr(SPRN_PSSCR_PR, host_psscr);
Nicholas Piggind5f48012021-11-23 19:52:09 +10004355
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004356 return trap;
4357}
4358
Paul Mackerras95a64322018-10-08 16:30:55 +11004359/*
4360 * Guest entry for POWER9 and later CPUs.
4361 */
4362static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004363 unsigned long lpcr, u64 *tb)
Paul Mackerras95a64322018-10-08 16:30:55 +11004364{
Nicholas Piggin42b4a2b2022-03-03 15:33:13 +10004365 struct kvm *kvm = vcpu->kvm;
4366 struct kvm_nested_guest *nested = vcpu->arch.nested;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004367 u64 next_timer;
Nicholas Piggin57dc0ee2021-11-23 19:51:51 +10004368 int trap;
Paul Mackerras95a64322018-10-08 16:30:55 +11004369
Nicholas Piggin4ebbd072021-11-23 19:51:42 +10004370 next_timer = timer_get_next_tb();
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004371 if (*tb >= next_timer)
Paul Mackerras95a64322018-10-08 16:30:55 +11004372 return BOOK3S_INTERRUPT_HV_DECREMENTER;
Nicholas Piggin4ebbd072021-11-23 19:51:42 +10004373 if (next_timer < time_limit)
4374 time_limit = next_timer;
Nicholas Piggin46dea772021-11-23 19:52:22 +10004375 else if (*tb >= time_limit) /* nested time limit */
4376 return BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER;
Paul Mackerras95a64322018-10-08 16:30:55 +11004377
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004378 vcpu->arch.ceded = 0;
4379
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004380 vcpu_vpa_increment_dispatch(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004381
Paul Mackerras360cae32018-10-08 16:31:04 +11004382 if (kvmhv_on_pseries()) {
Jordan Niethe19d31c52023-09-14 13:05:59 +10004383 if (kvmhv_is_nestedv1())
4384 trap = kvmhv_vcpu_entry_p9_nested(vcpu, time_limit, lpcr, tb);
4385 else
4386 trap = kvmhv_vcpu_entry_nestedv2(vcpu, time_limit, lpcr, tb);
Paul Mackerras4bad7772018-10-08 16:31:06 +11004387
4388 /* H_CEDE has to be handled now, not later */
Nicholas Piggin42b4a2b2022-03-03 15:33:13 +10004389 if (trap == BOOK3S_INTERRUPT_SYSCALL && !nested &&
Paul Mackerras4bad7772018-10-08 16:31:06 +11004390 kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004391 kvmppc_cede(vcpu);
Michael Roth1f50cc12020-03-10 16:11:28 -05004392 kvmppc_set_gpr(vcpu, 3, 0);
Paul Mackerras4bad7772018-10-08 16:31:06 +11004393 trap = 0;
4394 }
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10004395
Nicholas Piggin42b4a2b2022-03-03 15:33:13 +10004396 } else if (nested) {
Nicholas Piggin434398a2021-11-23 19:52:27 +10004397 __this_cpu_write(cpu_in_guest, kvm);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004398 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
Nicholas Piggin434398a2021-11-23 19:52:27 +10004399 __this_cpu_write(cpu_in_guest, NULL);
Paul Mackerras360cae32018-10-08 16:31:04 +11004400
Nicholas Piggin42b4a2b2022-03-03 15:33:13 +10004401 } else {
Nicholas Piggin09512c22021-05-28 19:07:31 +10004402 kvmppc_xive_push_vcpu(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11004403
Paul Mackerras360cae32018-10-08 16:31:04 +11004404 __this_cpu_write(cpu_in_guest, kvm);
Nicholas Pigginc00366e22021-05-28 19:07:35 +10004405 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
Nicholas Piggin434398a2021-11-23 19:52:27 +10004406 __this_cpu_write(cpu_in_guest, NULL);
4407
Nicholas Piggin42b4a2b2022-03-03 15:33:13 +10004408 if (trap == BOOK3S_INTERRUPT_SYSCALL &&
Jordan Niethe6de2e832023-09-14 13:05:56 +10004409 !(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004410 unsigned long req = kvmppc_get_gpr(vcpu, 3);
Nicholas Piggin09512c22021-05-28 19:07:31 +10004411
Nicholas Piggin11681b72022-03-03 15:33:14 +10004412 /*
4413 * XIVE rearm and XICS hcalls must be handled
4414 * before xive context is pulled (is this
4415 * true?)
4416 */
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004417 if (req == H_CEDE) {
Nicholas Piggin11681b72022-03-03 15:33:14 +10004418 /* H_CEDE has to be handled now */
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004419 kvmppc_cede(vcpu);
Nicholas Pigginad5ace92022-03-03 15:33:12 +10004420 if (!kvmppc_xive_rearm_escalation(vcpu)) {
4421 /*
4422 * Pending escalation so abort
4423 * the cede.
4424 */
4425 vcpu->arch.ceded = 0;
4426 }
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004427 kvmppc_set_gpr(vcpu, 3, 0);
4428 trap = 0;
4429
Nicholas Piggin11681b72022-03-03 15:33:14 +10004430 } else if (req == H_ENTER_NESTED) {
4431 /*
4432 * L2 should not run with the L1
4433 * context so rearm and pull it.
4434 */
4435 if (!kvmppc_xive_rearm_escalation(vcpu)) {
4436 /*
4437 * Pending escalation so abort
4438 * H_ENTER_NESTED.
4439 */
4440 kvmppc_set_gpr(vcpu, 3, 0);
4441 trap = 0;
4442 }
4443
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004444 } else if (hcall_is_xics(req)) {
4445 int ret;
4446
4447 ret = kvmppc_xive_xics_hcall(vcpu, req);
4448 if (ret != H_TOO_HARD) {
4449 kvmppc_set_gpr(vcpu, 3, ret);
4450 trap = 0;
4451 }
4452 }
4453 }
4454 kvmppc_xive_pull_vcpu(vcpu);
Nicholas Piggin89d35b22021-05-28 19:07:34 +10004455
Nicholas Piggin434398a2021-11-23 19:52:27 +10004456 if (kvm_is_radix(kvm))
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004457 vcpu->arch.slb_max = 0;
Paul Mackerras360cae32018-10-08 16:31:04 +11004458 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004459
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004460 vcpu_vpa_increment_dispatch(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004461
4462 return trap;
4463}
4464
4465/*
Paul Mackerras19ccb762011-07-23 17:42:46 +10004466 * Wait for some other vcpu thread to execute us, and
4467 * wake us up when we need to handle something in the host.
4468 */
Paul Mackerrasec257162015-06-24 21:18:03 +10004469static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
4470 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00004471{
Paul Mackerras371fefd2011-06-29 00:23:08 +00004472 DEFINE_WAIT(wait);
4473
Paul Mackerras19ccb762011-07-23 17:42:46 +10004474 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10004475 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4476 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004477 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10004478 spin_lock(&vc->lock);
4479 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10004480 finish_wait(&vcpu->arch.cpu_run, &wait);
4481}
Paul Mackerras371fefd2011-06-29 00:23:08 +00004482
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004483static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
4484{
Nir Weiner7fa08e72019-01-27 12:17:14 +02004485 if (!halt_poll_ns_grow)
4486 return;
4487
Nir Weinerdee339b2019-01-27 12:17:16 +02004488 vc->halt_poll_ns *= halt_poll_ns_grow;
4489 if (vc->halt_poll_ns < halt_poll_ns_grow_start)
Nir Weiner49113d32019-01-27 12:17:15 +02004490 vc->halt_poll_ns = halt_poll_ns_grow_start;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004491}
4492
4493static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
4494{
4495 if (halt_poll_ns_shrink == 0)
4496 vc->halt_poll_ns = 0;
4497 else
4498 vc->halt_poll_ns /= halt_poll_ns_shrink;
4499}
4500
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004501#ifdef CONFIG_KVM_XICS
4502static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4503{
Paul Mackerras03f95332019-02-04 22:07:20 +11004504 if (!xics_on_xive())
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004505 return false;
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +11004506 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004507 vcpu->arch.xive_saved_state.cppr;
4508}
4509#else
4510static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4511{
4512 return false;
4513}
4514#endif /* CONFIG_KVM_XICS */
4515
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004516static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
4517{
4518 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004519 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004520 return true;
4521
4522 return false;
4523}
4524
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004525static bool kvmppc_vcpu_check_block(struct kvm_vcpu *vcpu)
4526{
4527 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
4528 return true;
4529 return false;
4530}
4531
Suraj Jitindar Singh908a0932016-10-14 11:53:23 +11004532/*
4533 * Check to see if any of the runnable vcpus on the vcore have pending
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004534 * exceptions or are no longer ceded
4535 */
4536static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
4537{
4538 struct kvm_vcpu *vcpu;
4539 int i;
4540
4541 for_each_runnable_thread(i, vcpu, vc) {
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004542 if (kvmppc_vcpu_check_block(vcpu))
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004543 return 1;
4544 }
4545
4546 return 0;
4547}
4548
Paul Mackerras19ccb762011-07-23 17:42:46 +10004549/*
4550 * All the vcpus in this vcore are idle, so wait for a decrementer
4551 * or external interrupt to one of the vcpus. vc->lock is held.
4552 */
4553static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
4554{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004555 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004556 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004557 u64 block_ns;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11004558
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004559 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
4560
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004561 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004562 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004563 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004564 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
Jing Zhang0193cc92021-06-18 22:27:03 +00004565 ++vc->runner->stat.generic.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004566
4567 vc->vcore_state = VCORE_POLLING;
4568 spin_unlock(&vc->lock);
4569
4570 do {
4571 if (kvmppc_vcore_check_block(vc)) {
4572 do_sleep = 0;
4573 break;
4574 }
4575 cur = ktime_get();
Wanpeng Li6bd5b742021-05-18 05:00:31 -07004576 } while (kvm_vcpu_can_poll(cur, stop));
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004577
4578 spin_lock(&vc->lock);
4579 vc->vcore_state = VCORE_INACTIVE;
4580
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004581 if (!do_sleep) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004582 ++vc->runner->stat.generic.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004583 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004584 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004585 }
4586
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004587 prepare_to_rcuwait(&vc->wait);
4588 set_current_state(TASK_INTERRUPTIBLE);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004589 if (kvmppc_vcore_check_block(vc)) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004590 finish_rcuwait(&vc->wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004591 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004592 /* If we polled, count this as a successful poll */
4593 if (vc->halt_poll_ns)
Jing Zhang0193cc92021-06-18 22:27:03 +00004594 ++vc->runner->stat.generic.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004595 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11004596 }
4597
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004598 start_wait = ktime_get();
4599
Paul Mackerras19ccb762011-07-23 17:42:46 +10004600 vc->vcore_state = VCORE_SLEEPING;
Fabiano Rosasad55bae2022-03-28 18:58:31 -03004601 trace_kvmppc_vcore_blocked(vc->runner, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004602 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004603 schedule();
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004604 finish_rcuwait(&vc->wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004605 spin_lock(&vc->lock);
4606 vc->vcore_state = VCORE_INACTIVE;
Fabiano Rosasad55bae2022-03-28 18:58:31 -03004607 trace_kvmppc_vcore_blocked(vc->runner, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004608 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004609
4610 cur = ktime_get();
4611
4612out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004613 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
4614
4615 /* Attribute wait time */
4616 if (do_sleep) {
Jing Zhang87bcc5f2021-08-02 16:56:32 +00004617 vc->runner->stat.generic.halt_wait_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004618 ktime_to_ns(cur) - ktime_to_ns(start_wait);
Jing Zhang8ccba532021-08-02 16:56:33 +00004619 KVM_STATS_LOG_HIST_UPDATE(
4620 vc->runner->stat.generic.halt_wait_hist,
4621 ktime_to_ns(cur) - ktime_to_ns(start_wait));
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004622 /* Attribute failed poll time */
Jing Zhang8ccba532021-08-02 16:56:33 +00004623 if (vc->halt_poll_ns) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004624 vc->runner->stat.generic.halt_poll_fail_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004625 ktime_to_ns(start_wait) -
4626 ktime_to_ns(start_poll);
Jing Zhang8ccba532021-08-02 16:56:33 +00004627 KVM_STATS_LOG_HIST_UPDATE(
4628 vc->runner->stat.generic.halt_poll_fail_hist,
4629 ktime_to_ns(start_wait) -
4630 ktime_to_ns(start_poll));
4631 }
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004632 } else {
4633 /* Attribute successful poll time */
Jing Zhang8ccba532021-08-02 16:56:33 +00004634 if (vc->halt_poll_ns) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004635 vc->runner->stat.generic.halt_poll_success_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004636 ktime_to_ns(cur) -
4637 ktime_to_ns(start_poll);
Jing Zhang8ccba532021-08-02 16:56:33 +00004638 KVM_STATS_LOG_HIST_UPDATE(
4639 vc->runner->stat.generic.halt_poll_success_hist,
4640 ktime_to_ns(cur) - ktime_to_ns(start_poll));
4641 }
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004642 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004643
4644 /* Adjust poll time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004645 if (halt_poll_ns) {
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004646 if (block_ns <= vc->halt_poll_ns)
4647 ;
4648 /* We slept and blocked for longer than the max halt time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004649 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004650 shrink_halt_poll_ns(vc);
4651 /* We slept and our poll time is too small */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004652 else if (vc->halt_poll_ns < halt_poll_ns &&
4653 block_ns < halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004654 grow_halt_poll_ns(vc);
Suraj Jitindar Singhe03f3922016-10-14 11:53:21 +11004655 if (vc->halt_poll_ns > halt_poll_ns)
4656 vc->halt_poll_ns = halt_poll_ns;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004657 } else
4658 vc->halt_poll_ns = 0;
4659
4660 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004661}
4662
Paul Mackerras360cae32018-10-08 16:31:04 +11004663/*
4664 * This never fails for a radix guest, as none of the operations it does
4665 * for a radix guest can fail or have a way to report failure.
Paul Mackerras360cae32018-10-08 16:31:04 +11004666 */
Paul Mackerras432953b2017-11-09 15:37:10 +11004667static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
4668{
4669 int r = 0;
4670 struct kvm *kvm = vcpu->kvm;
4671
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004672 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004673 if (!kvm->arch.mmu_ready) {
4674 if (!kvm_is_radix(kvm))
4675 r = kvmppc_hv_setup_htab_rma(vcpu);
4676 if (!r) {
4677 if (cpu_has_feature(CPU_FTR_ARCH_300))
4678 kvmppc_setup_partition_table(kvm);
4679 kvm->arch.mmu_ready = 1;
4680 }
4681 }
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004682 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004683 return r;
4684}
4685
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004686static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
Paul Mackerras19ccb762011-07-23 17:42:46 +10004687{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004688 struct kvm_run *run = vcpu->run;
Paul Mackerras38c53af2017-11-08 14:44:04 +11004689 int n_ceded, i, r;
Paul Mackerras19ccb762011-07-23 17:42:46 +10004690 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004691 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00004692
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004693 trace_kvmppc_run_vcpu_enter(vcpu);
4694
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004695 run->exit_reason = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004696 vcpu->arch.ret = RESUME_GUEST;
4697 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00004698 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004699
Paul Mackerras371fefd2011-06-29 00:23:08 +00004700 /*
4701 * Synchronize with other threads in this virtual core
4702 */
4703 vc = vcpu->arch.vcore;
4704 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004705 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004706 vcpu->arch.run_task = current;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004707 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10004708 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004709 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004710 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004711 ++vc->n_runnable;
4712
Paul Mackerras19ccb762011-07-23 17:42:46 +10004713 /*
4714 * This happens the first time this is called for a vcpu.
4715 * If the vcore is already running, we may be able to start
4716 * this thread straight away and have it join in.
4717 */
Paul Mackerras8455d792012-10-15 01:17:42 +00004718 if (!signal_pending(current)) {
Paul Mackerrasc0093f12017-11-20 16:12:25 +11004719 if ((vc->vcore_state == VCORE_PIGGYBACK ||
4720 vc->vcore_state == VCORE_RUNNING) &&
Paul Mackerrasec257162015-06-24 21:18:03 +10004721 !VCORE_IS_EXITING(vc)) {
Nicholas Piggine4335f52022-09-08 23:25:45 +10004722 kvmppc_update_vpa_dispatch(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004723 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004724 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00004725 } else if (vc->vcore_state == VCORE_SLEEPING) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004726 rcuwait_wake_up(&vc->wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004727 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00004728
Paul Mackerras8455d792012-10-15 01:17:42 +00004729 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10004730
4731 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4732 !signal_pending(current)) {
Paul Mackerras072df812017-11-09 14:30:24 +11004733 /* See if the MMU is ready to go */
4734 if (!vcpu->kvm->arch.mmu_ready) {
Paul Mackerras38c53af2017-11-08 14:44:04 +11004735 spin_unlock(&vc->lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004736 r = kvmhv_setup_mmu(vcpu);
Paul Mackerras38c53af2017-11-08 14:44:04 +11004737 spin_lock(&vc->lock);
4738 if (r) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004739 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4740 run->fail_entry.
Paul Mackerras432953b2017-11-09 15:37:10 +11004741 hardware_entry_failure_reason = 0;
Paul Mackerras38c53af2017-11-08 14:44:04 +11004742 vcpu->arch.ret = r;
4743 break;
4744 }
4745 }
4746
Paul Mackerrasec257162015-06-24 21:18:03 +10004747 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4748 kvmppc_vcore_end_preempt(vc);
4749
Paul Mackerras8455d792012-10-15 01:17:42 +00004750 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10004751 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004752 continue;
4753 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004754 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06004755 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004756 if (signal_pending(v->arch.run_task)) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004757 kvmppc_remove_runnable(vc, v, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10004758 v->stat.signal_exits++;
Tianjia Zhang2610a572020-04-27 12:35:10 +08004759 v->run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras19ccb762011-07-23 17:42:46 +10004760 v->arch.ret = -EINTR;
4761 wake_up(&v->arch.cpu_run);
4762 }
4763 }
Paul Mackerras8455d792012-10-15 01:17:42 +00004764 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
4765 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00004766 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004767 for_each_runnable_thread(i, v, vc) {
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004768 if (!kvmppc_vcpu_woken(v))
Paul Mackerras8455d792012-10-15 01:17:42 +00004769 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00004770 else
4771 v->arch.ceded = 0;
4772 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004773 vc->runner = vcpu;
4774 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00004775 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03004776 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10004777 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004778 /* Let something else run */
4779 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10004780 if (vc->vcore_state == VCORE_PREEMPT)
4781 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004782 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00004783 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004784 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00004785 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004786 }
4787
Paul Mackerras8455d792012-10-15 01:17:42 +00004788 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4789 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10004790 vc->vcore_state == VCORE_EXITING ||
4791 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10004792 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00004793
Paul Mackerras5fc3e642015-09-18 13:13:44 +10004794 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4795 kvmppc_vcore_end_preempt(vc);
4796
Paul Mackerras8455d792012-10-15 01:17:42 +00004797 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004798 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras8455d792012-10-15 01:17:42 +00004799 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004800 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras8455d792012-10-15 01:17:42 +00004801 vcpu->arch.ret = -EINTR;
4802 }
4803
4804 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
4805 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004806 i = -1;
4807 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00004808 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004809 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00004810
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004811 trace_kvmppc_run_vcpu_exit(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004812 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004813 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004814}
4815
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004816int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
Paul Mackerras360cae32018-10-08 16:31:04 +11004817 unsigned long lpcr)
Paul Mackerras95a64322018-10-08 16:30:55 +11004818{
Sean Christopherson63fa47b2021-12-13 17:45:56 +00004819 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004820 struct kvm_run *run = vcpu->run;
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11004821 int trap, r, pcpu;
Nicholas Piggin48013cb2021-05-28 19:07:32 +10004822 int srcu_idx;
Paul Mackerras95a64322018-10-08 16:30:55 +11004823 struct kvmppc_vcore *vc;
4824 struct kvm *kvm = vcpu->kvm;
Paul Mackerras360cae32018-10-08 16:31:04 +11004825 struct kvm_nested_guest *nested = vcpu->arch.nested;
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004826 unsigned long flags;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004827 u64 tb;
Paul Mackerras95a64322018-10-08 16:30:55 +11004828
4829 trace_kvmppc_run_vcpu_enter(vcpu);
4830
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004831 run->exit_reason = 0;
Paul Mackerras95a64322018-10-08 16:30:55 +11004832 vcpu->arch.ret = RESUME_GUEST;
4833 vcpu->arch.trap = 0;
4834
4835 vc = vcpu->arch.vcore;
4836 vcpu->arch.ceded = 0;
4837 vcpu->arch.run_task = current;
Paul Mackerras95a64322018-10-08 16:30:55 +11004838 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
Paul Mackerras95a64322018-10-08 16:30:55 +11004839
4840 /* See if the MMU is ready to go */
Nicholas Piggin4c9a6892021-11-23 19:52:26 +10004841 if (unlikely(!kvm->arch.mmu_ready)) {
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004842 r = kvmhv_setup_mmu(vcpu);
4843 if (r) {
4844 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4845 run->fail_entry.hardware_entry_failure_reason = 0;
4846 vcpu->arch.ret = r;
4847 return r;
4848 }
4849 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004850
4851 if (need_resched())
4852 cond_resched();
4853
4854 kvmppc_update_vpas(vcpu);
4855
Paul Mackerras95a64322018-10-08 16:30:55 +11004856 preempt_disable();
4857 pcpu = smp_processor_id();
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004858 if (kvm_is_radix(kvm))
4859 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004860
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004861 /* flags save not required, but irq_pmu has no disable/enable API */
4862 powerpc_local_irq_pmu_save(flags);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004863
Nicholas Piggin1a5486b2022-09-08 23:25:44 +10004864 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4865
Paul Mackerras95a64322018-10-08 16:30:55 +11004866 if (signal_pending(current))
4867 goto sigpend;
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004868 if (need_resched() || !kvm->arch.mmu_ready)
Paul Mackerras95a64322018-10-08 16:30:55 +11004869 goto out;
4870
Nicholas Pigginc7fa8482022-03-03 15:33:10 +10004871 vcpu->cpu = pcpu;
4872 vcpu->arch.thread_cpu = pcpu;
4873 vc->pcpu = pcpu;
4874 local_paca->kvm_hstate.kvm_vcpu = vcpu;
4875 local_paca->kvm_hstate.ptid = 0;
4876 local_paca->kvm_hstate.fake_suspend = 0;
4877
4878 /*
4879 * Orders set cpu/thread_cpu vs testing for pending interrupts and
4880 * doorbells below. The other side is when these fields are set vs
4881 * kvmppc_fast_vcpu_kick_hv reading the cpu/thread_cpu fields to
4882 * kick a vCPU to notice the pending interrupt.
4883 */
4884 smp_mb();
4885
Paul Mackerras360cae32018-10-08 16:31:04 +11004886 if (!nested) {
4887 kvmppc_core_prepare_to_enter(vcpu);
Nicholas Pigginecd10702023-12-01 18:56:11 +05304888 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
4889 &vcpu->arch.pending_exceptions) ||
4890 xive_interrupt_pending(vcpu)) {
4891 /*
4892 * For nested HV, don't synthesize but always pass MER,
4893 * the L0 will be able to optimise that more
4894 * effectively than manipulating registers directly.
4895 */
4896 if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
Nicholas Piggin026728d2022-03-03 15:33:11 +10004897 kvmppc_inject_interrupt_hv(vcpu,
Nicholas Pigginecd10702023-12-01 18:56:11 +05304898 BOOK3S_INTERRUPT_EXTERNAL, 0);
4899 else
4900 lpcr |= LPCR_MER;
Gautam Menghania3738302024-10-28 14:34:09 +05304901 } else {
4902 /*
4903 * L1's copy of L2's LPCR (vcpu->arch.vcore->lpcr) can get its MER bit
4904 * unexpectedly set - for e.g. during NMI handling when all register
4905 * states are synchronized from L0 to L1. L1 needs to inform L0 about
4906 * MER=1 only when there are pending external interrupts.
4907 * In the above if check, MER bit is set if there are pending
4908 * external interrupts. Hence, explicity mask off MER bit
4909 * here as otherwise it may generate spurious interrupts in L2 KVM
4910 * causing an endless loop, which results in L2 guest getting hung.
4911 */
4912 lpcr &= ~LPCR_MER;
Nicholas Piggin026728d2022-03-03 15:33:11 +10004913 }
Paul Mackerras360cae32018-10-08 16:31:04 +11004914 } else if (vcpu->arch.pending_exceptions ||
4915 vcpu->arch.doorbell_request ||
4916 xive_interrupt_pending(vcpu)) {
4917 vcpu->arch.ret = RESUME_HOST;
4918 goto out;
4919 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004920
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004921 if (vcpu->arch.timer_running) {
4922 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
4923 vcpu->arch.timer_running = 0;
4924 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004925
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004926 tb = mftb();
4927
Jordan Niethec8ae9b32023-09-14 13:05:54 +10004928 kvmppc_update_vpa_dispatch_p9(vcpu, vc, tb + kvmppc_get_tb_offset(vcpu));
Paul Mackerras95a64322018-10-08 16:30:55 +11004929
Paul Mackerras95a64322018-10-08 16:30:55 +11004930 trace_kvm_guest_enter(vcpu);
4931
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004932 guest_timing_enter_irqoff();
Paul Mackerras95a64322018-10-08 16:30:55 +11004933
4934 srcu_idx = srcu_read_lock(&kvm->srcu);
4935
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004936 guest_state_enter_irqoff();
Paul Mackerras95a64322018-10-08 16:30:55 +11004937 this_cpu_disable_ftrace();
4938
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004939 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr, &tb);
Paul Mackerras95a64322018-10-08 16:30:55 +11004940 vcpu->arch.trap = trap;
4941
4942 this_cpu_enable_ftrace();
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004943 guest_state_exit_irqoff();
Paul Mackerras95a64322018-10-08 16:30:55 +11004944
4945 srcu_read_unlock(&kvm->srcu, srcu_idx);
4946
Paul Mackerras95a64322018-10-08 16:30:55 +11004947 set_irq_happened(trap);
4948
Nicholas Pigginbc91c042022-09-08 23:25:41 +10004949 vcpu->cpu = -1;
4950 vcpu->arch.thread_cpu = -1;
Nicholas Piggin1a5486b2022-09-08 23:25:44 +10004951 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Nicholas Pigginbc91c042022-09-08 23:25:41 +10004952
Laurent Vivier235cee12021-10-28 00:21:50 +10004953 if (!vtime_accounting_enabled_this_cpu()) {
Nicholas Pigginc953f752022-09-08 23:25:42 +10004954 powerpc_local_irq_pmu_restore(flags);
Laurent Vivier235cee12021-10-28 00:21:50 +10004955 /*
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004956 * Service IRQs here before guest_timing_exit_irqoff() so any
Laurent Vivier235cee12021-10-28 00:21:50 +10004957 * ticks that occurred while running the guest are accounted to
4958 * the guest. If vtime accounting is enabled, accounting uses
4959 * TB rather than ticks, so it can be done without enabling
4960 * interrupts here, which has the problem that it accounts
4961 * interrupt processing overhead to the host.
4962 */
Nicholas Pigginc953f752022-09-08 23:25:42 +10004963 powerpc_local_irq_pmu_save(flags);
Laurent Vivier235cee12021-10-28 00:21:50 +10004964 }
Nicholas Pigginb31bc242022-09-08 23:25:43 +10004965 guest_timing_exit_irqoff();
Nicholas Piggin1126652862021-01-30 23:08:12 +10004966
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004967 powerpc_local_irq_pmu_restore(flags);
Paul Mackerras95a64322018-10-08 16:30:55 +11004968
4969 preempt_enable();
4970
Suraj Jitindar Singh3c25ab32019-06-20 11:46:51 +10004971 /*
4972 * cancel pending decrementer exception if DEC is now positive, or if
4973 * entering a nested guest in which case the decrementer is now owned
4974 * by L2 and the L1 decrementer is provided in hdec_expires
4975 */
Vaibhav Jain7be6ce72024-04-15 09:27:29 +05304976 if (kvmppc_core_pending_dec(vcpu) &&
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004977 ((tb < kvmppc_dec_expires_host_tb(vcpu)) ||
Suraj Jitindar Singh3c25ab32019-06-20 11:46:51 +10004978 (trap == BOOK3S_INTERRUPT_SYSCALL &&
4979 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
Paul Mackerras95a64322018-10-08 16:30:55 +11004980 kvmppc_core_dequeue_dec(vcpu);
4981
4982 trace_kvm_guest_exit(vcpu);
4983 r = RESUME_GUEST;
Paul Mackerras360cae32018-10-08 16:31:04 +11004984 if (trap) {
4985 if (!nested)
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004986 r = kvmppc_handle_exit_hv(vcpu, current);
Paul Mackerras360cae32018-10-08 16:31:04 +11004987 else
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004988 r = kvmppc_handle_nested_exit(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11004989 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004990 vcpu->arch.ret = r;
4991
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004992 if (is_kvmppc_resume_guest(r) && !kvmppc_vcpu_check_block(vcpu)) {
Paul Mackerras95a64322018-10-08 16:30:55 +11004993 kvmppc_set_timer(vcpu);
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004994
Sean Christopherson63fa47b2021-12-13 17:45:56 +00004995 prepare_to_rcuwait(wait);
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004996 for (;;) {
4997 set_current_state(TASK_INTERRUPTIBLE);
Paul Mackerras95a64322018-10-08 16:30:55 +11004998 if (signal_pending(current)) {
4999 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005000 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras95a64322018-10-08 16:30:55 +11005001 vcpu->arch.ret = -EINTR;
5002 break;
5003 }
Nicholas Pigginecb6a722021-11-23 19:52:28 +10005004
5005 if (kvmppc_vcpu_check_block(vcpu))
5006 break;
5007
Fabiano Rosasad55bae2022-03-28 18:58:31 -03005008 trace_kvmppc_vcore_blocked(vcpu, 0);
Nicholas Pigginecb6a722021-11-23 19:52:28 +10005009 schedule();
Fabiano Rosasad55bae2022-03-28 18:58:31 -03005010 trace_kvmppc_vcore_blocked(vcpu, 1);
Paul Mackerras95a64322018-10-08 16:30:55 +11005011 }
Sean Christopherson63fa47b2021-12-13 17:45:56 +00005012 finish_rcuwait(wait);
Paul Mackerras95a64322018-10-08 16:30:55 +11005013 }
5014 vcpu->arch.ceded = 0;
5015
Paul Mackerras95a64322018-10-08 16:30:55 +11005016 done:
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005017 trace_kvmppc_run_vcpu_exit(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11005018
5019 return vcpu->arch.ret;
5020
5021 sigpend:
5022 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005023 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras95a64322018-10-08 16:30:55 +11005024 vcpu->arch.ret = -EINTR;
5025 out:
Nicholas Pigginc7fa8482022-03-03 15:33:10 +10005026 vcpu->cpu = -1;
5027 vcpu->arch.thread_cpu = -1;
Nicholas Piggin1a5486b2022-09-08 23:25:44 +10005028 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10005029 powerpc_local_irq_pmu_restore(flags);
Paul Mackerras95a64322018-10-08 16:30:55 +11005030 preempt_enable();
5031 goto done;
5032}
5033
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005034static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005035{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005036 struct kvm_run *run = vcpu->run;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005037 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00005038 int srcu_idx;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005039 struct kvm *kvm;
Nicholas Piggin34e119c92021-11-23 19:51:57 +10005040 unsigned long msr;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005041
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005042 start_timing(vcpu, &vcpu->arch.vcpu_entry);
5043
Alexander Grafaf8f38b2011-08-10 13:57:08 +02005044 if (!vcpu->arch.sane) {
5045 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5046 return -EINVAL;
5047 }
5048
Nicholas Piggin34e119c92021-11-23 19:51:57 +10005049 /* No need to go into the guest when all we'll do is come back out */
5050 if (signal_pending(current)) {
5051 run->exit_reason = KVM_EXIT_INTR;
5052 return -EINTR;
5053 }
5054
5055#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras46a704f2017-06-15 16:10:27 +10005056 /*
5057 * Don't allow entry with a suspended transaction, because
5058 * the guest entry/exit code will lose it.
Paul Mackerras46a704f2017-06-15 16:10:27 +10005059 */
Paul Mackerras46a704f2017-06-15 16:10:27 +10005060 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
5061 (current->thread.regs->msr & MSR_TM)) {
5062 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
5063 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5064 run->fail_entry.hardware_entry_failure_reason = 0;
5065 return -EINVAL;
5066 }
Paul Mackerras46a704f2017-06-15 16:10:27 +10005067 }
5068#endif
5069
Paul Mackerras7aa15842018-04-20 19:53:22 +10005070 /*
5071 * Force online to 1 for the sake of old userspace which doesn't
5072 * set it.
5073 */
5074 if (!vcpu->arch.online) {
5075 atomic_inc(&vcpu->arch.vcore->online_count);
5076 vcpu->arch.online = 1;
5077 }
5078
Scott Wood25051b5a2011-11-08 18:23:23 -06005079 kvmppc_core_prepare_to_enter(vcpu);
5080
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005081 kvm = vcpu->kvm;
5082 atomic_inc(&kvm->arch.vcpus_running);
5083 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00005084 smp_mb();
5085
Nicholas Piggin34e119c92021-11-23 19:51:57 +10005086 msr = 0;
5087 if (IS_ENABLED(CONFIG_PPC_FPU))
5088 msr |= MSR_FP;
5089 if (cpu_has_feature(CPU_FTR_ALTIVEC))
5090 msr |= MSR_VEC;
5091 if (cpu_has_feature(CPU_FTR_VSX))
5092 msr |= MSR_VSX;
Nicholas Piggin022ecb92021-11-23 19:52:15 +10005093 if ((cpu_has_feature(CPU_FTR_TM) ||
5094 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) &&
Jordan Nietheebc88ea2023-09-14 13:05:55 +10005095 (kvmppc_get_hfscr_hv(vcpu) & HFSCR_TM))
Nicholas Piggin34e119c92021-11-23 19:51:57 +10005096 msr |= MSR_TM;
5097 msr = msr_check_and_set(msr);
Anton Blanchard579e6332015-10-29 11:44:09 +11005098
Nicholas Piggin34e119c92021-11-23 19:51:57 +10005099 kvmppc_save_user_regs();
Paul Mackerrasca8efa12017-06-06 16:47:22 +10005100
Nicholas Piggin52367562021-11-23 19:52:16 +10005101 kvmppc_save_current_sprs();
Paul Mackerras19ccb762011-07-23 17:42:46 +10005102
Nicholas Pigginecb6a722021-11-23 19:52:28 +10005103 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5104 vcpu->arch.waitp = &vcpu->arch.vcore->wait;
Leonardo Bras8a9c8922019-11-26 19:36:30 -03005105 vcpu->arch.pgdir = kvm->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00005106 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10005107
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005108 do {
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005109 accumulate_time(vcpu, &vcpu->arch.guest_entry);
Nicholas Piggin0bf7e1b2021-05-28 19:07:51 +10005110 if (cpu_has_feature(CPU_FTR_ARCH_300))
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005111 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
Paul Mackerras360cae32018-10-08 16:31:04 +11005112 vcpu->arch.vcore->lpcr);
Paul Mackerras95a64322018-10-08 16:30:55 +11005113 else
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005114 r = kvmppc_run_vcpu(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005115
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10005116 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005117 accumulate_time(vcpu, &vcpu->arch.hcall);
5118
Jordan Niethe63ccae72023-12-01 18:56:08 +05305119 if (!kvmhv_is_nestedv2() && WARN_ON_ONCE(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) {
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10005120 /*
5121 * These should have been caught reflected
5122 * into the guest by now. Final sanity check:
5123 * don't allow userspace to execute hcalls in
5124 * the hypervisor.
5125 */
5126 r = RESUME_GUEST;
5127 continue;
5128 }
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06005129 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005130 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06005131 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06005132 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00005133 } else if (r == RESUME_PAGE_FAULT) {
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005134 accumulate_time(vcpu, &vcpu->arch.pg_fault);
Paul Mackerras432953b2017-11-09 15:37:10 +11005135 srcu_idx = srcu_read_lock(&kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005136 r = kvmppc_book3s_hv_page_fault(vcpu,
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00005137 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
Paul Mackerras432953b2017-11-09 15:37:10 +11005138 srcu_read_unlock(&kvm->srcu, srcu_idx);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005139 } else if (r == RESUME_PASSTHROUGH) {
Paul Mackerras03f95332019-02-04 22:07:20 +11005140 if (WARN_ON(xics_on_xive()))
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005141 r = H_SUCCESS;
5142 else
5143 r = kvmppc_xics_rm_complete(vcpu, 0);
5144 }
Greg Kurze59d24e2014-02-06 17:36:56 +01005145 } while (is_kvmppc_resume_guest(r));
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005146 accumulate_time(vcpu, &vcpu->arch.vcpu_exit);
Paul Mackerras32fad282012-05-04 02:32:53 +00005147
Paul Mackerrasc7b67672012-10-15 01:18:07 +00005148 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras432953b2017-11-09 15:37:10 +11005149 atomic_dec(&kvm->arch.vcpus_running);
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10005150
5151 srr_regs_clobbered();
5152
Fabiano Rosasb44bb1b2022-05-25 10:05:54 -03005153 end_timing(vcpu);
5154
Paul Mackerrasa8606e22011-06-29 00:22:05 +00005155 return r;
5156}
5157
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005158static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005159 int shift, int sllp)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005160{
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005161 (*sps)->page_shift = shift;
5162 (*sps)->slb_enc = sllp;
5163 (*sps)->enc[0].page_shift = shift;
5164 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05305165 /*
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005166 * Add 16MB MPSS support (may get filtered out by userspace)
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05305167 */
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005168 if (shift != 24) {
5169 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
5170 if (penc != -1) {
5171 (*sps)->enc[1].page_shift = 24;
5172 (*sps)->enc[1].pte_enc = penc;
5173 }
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05305174 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005175 (*sps)++;
5176}
5177
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305178static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
5179 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005180{
5181 struct kvm_ppc_one_seg_page_size *sps;
5182
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005183 /*
Paul Mackerrase3bfed12017-08-25 19:53:39 +10005184 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
5185 * POWER7 doesn't support keys for instruction accesses,
5186 * POWER8 and POWER9 do.
5187 */
5188 info->data_keys = 32;
5189 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
5190
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005191 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
5192 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
5193 info->slb_size = 32;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005194
5195 /* We only support these sizes for now, and no muti-size segments */
5196 sps = &info->sps[0];
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10005197 kvmppc_add_seg_page_size(&sps, 12, 0);
5198 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
5199 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005200
Paul Mackerras901f8c32018-10-08 14:24:30 +11005201 /* If running as a nested hypervisor, we don't support HPT guests */
5202 if (kvmhv_on_pseries())
5203 info->flags |= KVM_PPC_NO_HASH;
5204
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00005205 return 0;
5206}
5207
Paul Mackerras82ed3612011-12-15 02:03:22 +00005208/*
5209 * Get (and clear) the dirty memory log for a memory slot.
5210 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305211static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
5212 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00005213{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02005214 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00005215 struct kvm_memory_slot *memslot;
Marc Zyngier46808a42021-11-16 16:04:02 +00005216 int r;
5217 unsigned long n, i;
Paul Mackerrase641a312017-10-26 16:39:19 +11005218 unsigned long *buf, *p;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005219 struct kvm_vcpu *vcpu;
Paul Mackerras82ed3612011-12-15 02:03:22 +00005220
5221 mutex_lock(&kvm->slots_lock);
5222
5223 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07005224 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00005225 goto out;
5226
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02005227 slots = kvm_memslots(kvm);
5228 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00005229 r = -ENOENT;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08005230 if (!memslot || !memslot->dirty_bitmap)
Paul Mackerras82ed3612011-12-15 02:03:22 +00005231 goto out;
5232
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005233 /*
Paul Mackerrase641a312017-10-26 16:39:19 +11005234 * Use second half of bitmap area because both HPT and radix
5235 * accumulate bits in the first half.
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005236 */
Paul Mackerras82ed3612011-12-15 02:03:22 +00005237 n = kvm_dirty_bitmap_bytes(memslot);
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005238 buf = memslot->dirty_bitmap + n / sizeof(long);
5239 memset(buf, 0, n);
Paul Mackerras82ed3612011-12-15 02:03:22 +00005240
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005241 if (kvm_is_radix(kvm))
5242 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
5243 else
5244 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
Paul Mackerras82ed3612011-12-15 02:03:22 +00005245 if (r)
5246 goto out;
5247
Paul Mackerrase641a312017-10-26 16:39:19 +11005248 /*
5249 * We accumulate dirty bits in the first half of the
5250 * memslot's dirty_bitmap area, for when pages are paged
5251 * out or modified by the host directly. Pick up these
5252 * bits and add them to the map.
5253 */
5254 p = memslot->dirty_bitmap;
5255 for (i = 0; i < n / sizeof(long); ++i)
5256 buf[i] |= xchg(&p[i], 0);
5257
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005258 /* Harvest dirty bits from VPA and DTL updates */
5259 /* Note: we never modify the SLB shadow buffer areas */
5260 kvm_for_each_vcpu(i, vcpu, kvm) {
5261 spin_lock(&vcpu->arch.vpa_update_lock);
5262 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
5263 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
5264 spin_unlock(&vcpu->arch.vpa_update_lock);
5265 }
5266
Paul Mackerras82ed3612011-12-15 02:03:22 +00005267 r = -EFAULT;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11005268 if (copy_to_user(log->dirty_bitmap, buf, n))
Paul Mackerras82ed3612011-12-15 02:03:22 +00005269 goto out;
5270
5271 r = 0;
5272out:
5273 mutex_unlock(&kvm->slots_lock);
5274 return r;
5275}
5276
Sean Christophersone96c81e2020-02-18 13:07:27 -08005277static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00005278{
Sean Christophersone96c81e2020-02-18 13:07:27 -08005279 vfree(slot->arch.rmap);
5280 slot->arch.rmap = NULL;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00005281}
5282
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305283static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
Sean Christopherson537a17b2021-12-06 20:54:11 +01005284 const struct kvm_memory_slot *old,
5285 struct kvm_memory_slot *new,
5286 enum kvm_mr_change change)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00005287{
Sean Christopherson82307e62020-02-18 13:07:18 -08005288 if (change == KVM_MR_CREATE) {
Paolo Bonzini5a213b92021-12-19 15:27:21 +01005289 unsigned long size = array_size(new->npages, sizeof(*new->arch.rmap));
Alexey Kardashevskiy511d25d2021-09-01 18:45:12 +10005290
5291 if ((size >> PAGE_SHIFT) > totalram_pages())
5292 return -ENOMEM;
5293
Paolo Bonzini5a213b92021-12-19 15:27:21 +01005294 new->arch.rmap = vzalloc(size);
Sean Christopherson537a17b2021-12-06 20:54:11 +01005295 if (!new->arch.rmap)
Sean Christopherson82307e62020-02-18 13:07:18 -08005296 return -ENOMEM;
Sean Christopherson537a17b2021-12-06 20:54:11 +01005297 } else if (change != KVM_MR_DELETE) {
5298 new->arch.rmap = old->arch.rmap;
Sean Christopherson82307e62020-02-18 13:07:18 -08005299 }
5300
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00005301 return 0;
5302}
5303
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305304static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Sean Christopherson537a17b2021-12-06 20:54:11 +01005305 struct kvm_memory_slot *old,
Bharata B Raof032b732018-12-12 15:15:30 +11005306 const struct kvm_memory_slot *new,
5307 enum kvm_mr_change change)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005308{
Yongji Xiea56ee9f2016-11-04 13:55:12 +08005309 /*
Sean Christophersoneaaaed12021-12-06 20:54:14 +01005310 * If we are creating or modifying a memslot, it might make
Yongji Xiea56ee9f2016-11-04 13:55:12 +08005311 * some address that was previously cached as emulated
5312 * MMIO be no longer emulated MMIO, so invalidate
5313 * all the caches of emulated MMIO translations.
5314 */
Sean Christophersoneaaaed12021-12-06 20:54:14 +01005315 if (change != KVM_MR_DELETE)
Yongji Xiea56ee9f2016-11-04 13:55:12 +08005316 atomic64_inc(&kvm->arch.mmio_update);
Paul Mackerras5af3e9d2018-12-12 15:17:17 +11005317
5318 /*
5319 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
5320 * have already called kvm_arch_flush_shadow_memslot() to
5321 * flush shadow mappings. For KVM_MR_CREATE we have no
5322 * previous mappings. So the only case to handle is
5323 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
5324 * has been changed.
5325 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
5326 * to get rid of any THP PTEs in the partition-scoped page tables
5327 * so we can track dirtiness at the page level; we flush when
5328 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
5329 * using THP PTEs.
5330 */
5331 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
5332 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
5333 kvmppc_radix_flush_memslot(kvm, old);
Bharata B Raoc3262252019-11-25 08:36:29 +05305334 /*
5335 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
5336 */
5337 if (!kvm->arch.secure_guest)
5338 return;
5339
5340 switch (change) {
5341 case KVM_MR_CREATE:
Laurent Dufoura2ce7202020-07-27 11:07:18 -07005342 /*
5343 * @TODO kvmppc_uvmem_memslot_create() can fail and
5344 * return error. Fix this.
5345 */
5346 kvmppc_uvmem_memslot_create(kvm, new);
Bharata B Raoc3262252019-11-25 08:36:29 +05305347 break;
5348 case KVM_MR_DELETE:
Laurent Dufoura2ce7202020-07-27 11:07:18 -07005349 kvmppc_uvmem_memslot_delete(kvm, old);
Bharata B Raoc3262252019-11-25 08:36:29 +05305350 break;
5351 default:
5352 /* TODO: Handle KVM_MR_MOVE */
5353 break;
5354 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005355}
5356
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005357/*
5358 * Update LPCR values in kvm->arch and in vcores.
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005359 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
5360 * of kvm->arch.lpcr update).
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005361 */
5362void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
5363{
5364 long int i;
5365 u32 cores_done = 0;
5366
5367 if ((kvm->arch.lpcr & mask) == lpcr)
5368 return;
5369
5370 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
5371
5372 for (i = 0; i < KVM_MAX_VCORES; ++i) {
5373 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
5374 if (!vc)
5375 continue;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10005376
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005377 spin_lock(&vc->lock);
5378 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10005379 verify_lpcr(kvm, vc->lpcr);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005380 spin_unlock(&vc->lock);
5381 if (++cores_done >= kvm->arch.online_vcores)
5382 break;
5383 }
Jordan Niethe19d31c52023-09-14 13:05:59 +10005384
5385 if (kvmhv_is_nestedv2()) {
5386 struct kvm_vcpu *vcpu;
5387
5388 kvm_for_each_vcpu(i, vcpu, kvm) {
5389 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR);
5390 }
5391 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005392}
5393
Paul Mackerrasded13fc2017-11-22 14:38:53 +11005394void kvmppc_setup_partition_table(struct kvm *kvm)
Paul Mackerras7a840842016-11-16 22:25:20 +11005395{
5396 unsigned long dw0, dw1;
5397
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005398 if (!kvm_is_radix(kvm)) {
5399 /* PS field - page size for VRMA */
5400 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
5401 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
5402 /* HTABSIZE and HTABORG fields */
5403 dw0 |= kvm->arch.sdr1;
Paul Mackerras7a840842016-11-16 22:25:20 +11005404
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005405 /* Second dword as set by userspace */
5406 dw1 = kvm->arch.process_table;
5407 } else {
5408 dw0 = PATB_HR | radix__get_tree_size() |
5409 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
5410 dw1 = PATB_GR | kvm->arch.process_table;
5411 }
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005412 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
Paul Mackerras7a840842016-11-16 22:25:20 +11005413}
5414
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005415/*
5416 * Set up HPT (hashed page table) and RMA (real-mode area).
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005417 * Must be called with kvm->arch.mmu_setup_lock held.
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005418 */
Paul Mackerras32fad282012-05-04 02:32:53 +00005419static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005420{
5421 int err = 0;
5422 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005423 unsigned long hva;
5424 struct kvm_memory_slot *memslot;
5425 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005426 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005427 unsigned long psize, porder;
Paul Mackerras2c9097e42012-09-11 13:27:01 +00005428 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005429
Paul Mackerras32fad282012-05-04 02:32:53 +00005430 /* Allocate hashed page table (if not done already) and reset it */
David Gibson3f9d4f52016-12-20 16:49:00 +11005431 if (!kvm->arch.hpt.virt) {
David Gibsonaae07772016-12-20 16:49:02 +11005432 int order = KVM_DEFAULT_HPT_ORDER;
5433 struct kvm_hpt_info info;
5434
5435 err = kvmppc_allocate_hpt(&info, order);
5436 /* If we get here, it means userspace didn't specify a
5437 * size explicitly. So, try successively smaller
5438 * sizes if the default failed. */
5439 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
5440 err = kvmppc_allocate_hpt(&info, order);
5441
5442 if (err < 0) {
Paul Mackerras32fad282012-05-04 02:32:53 +00005443 pr_err("KVM: Couldn't alloc HPT\n");
5444 goto out;
5445 }
David Gibsonaae07772016-12-20 16:49:02 +11005446
5447 kvmppc_set_hpt(kvm, &info);
Paul Mackerras32fad282012-05-04 02:32:53 +00005448 }
5449
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005450 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e42012-09-11 13:27:01 +00005451 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005452 memslot = gfn_to_memslot(kvm, 0);
5453
5454 /* We must have some memory at 0 by now */
5455 err = -EINVAL;
5456 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e42012-09-11 13:27:01 +00005457 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005458
5459 /* Look up the VMA for the start of this memory slot */
5460 hva = memslot->userspace_addr;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005461 mmap_read_lock(kvm->mm);
Liam Howlett900c83f2021-06-28 19:39:05 -07005462 vma = vma_lookup(kvm->mm, hva);
5463 if (!vma || (vma->vm_flags & VM_IO))
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005464 goto up_out;
5465
5466 psize = vma_kernel_pagesize(vma);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005467
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005468 mmap_read_unlock(kvm->mm);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005469
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005470 /* We can handle 4k, 64k or 16M pages in the VRMA */
Paul Mackerrasdebd5742018-03-02 15:38:04 +11005471 if (psize >= 0x1000000)
5472 psize = 0x1000000;
5473 else if (psize >= 0x10000)
5474 psize = 0x10000;
5475 else
5476 psize = 0x1000;
5477 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005478
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005479 senc = slb_pgsize_encoding(psize);
5480 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
5481 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005482 /* Create HPTEs in the hash page table for the VRMA */
5483 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005484
Paul Mackerras7a840842016-11-16 22:25:20 +11005485 /* Update VRMASD field in the LPCR */
5486 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
5487 /* the -4 is to account for senc values starting at 0x10 */
5488 lpcr = senc << (LPCR_VRMASD_SH - 4);
5489 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerras7a840842016-11-16 22:25:20 +11005490 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005491
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005492 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005493 smp_wmb();
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005494 err = 0;
Paul Mackerras2c9097e42012-09-11 13:27:01 +00005495 out_srcu:
5496 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005497 out:
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005498 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005499
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005500 up_out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005501 mmap_read_unlock(kvm->mm);
Lai Jiangshan505d6422013-03-16 00:50:49 +08005502 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005503}
5504
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005505/*
5506 * Must be called with kvm->arch.mmu_setup_lock held and
5507 * mmu_ready = 0 and no vcpus running.
5508 */
Paul Mackerras18c36402017-09-13 16:00:10 +10005509int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
5510{
Nicholas Piggineacc8182021-11-23 19:51:46 +10005511 unsigned long lpcr, lpcr_mask;
5512
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005513 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005514 kvmhv_release_all_nested(kvm);
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005515 kvmppc_rmap_reset(kvm);
5516 kvm->arch.process_table = 0;
Sean Christophersonb1c53562021-04-01 17:56:53 -07005517 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005518 spin_lock(&kvm->mmu_lock);
5519 kvm->arch.radix = 0;
5520 spin_unlock(&kvm->mmu_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005521 kvmppc_free_radix(kvm);
Nicholas Piggineacc8182021-11-23 19:51:46 +10005522
5523 lpcr = LPCR_VPM1;
5524 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5525 if (cpu_has_feature(CPU_FTR_ARCH_31))
5526 lpcr_mask |= LPCR_HAIL;
5527 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5528
Paul Mackerras18c36402017-09-13 16:00:10 +10005529 return 0;
5530}
5531
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005532/*
5533 * Must be called with kvm->arch.mmu_setup_lock held and
5534 * mmu_ready = 0 and no vcpus running.
5535 */
Paul Mackerras18c36402017-09-13 16:00:10 +10005536int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
5537{
Nicholas Piggineacc8182021-11-23 19:51:46 +10005538 unsigned long lpcr, lpcr_mask;
Paul Mackerras18c36402017-09-13 16:00:10 +10005539 int err;
5540
5541 err = kvmppc_init_vm_radix(kvm);
5542 if (err)
5543 return err;
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005544 kvmppc_rmap_reset(kvm);
Sean Christophersonb1c53562021-04-01 17:56:53 -07005545 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005546 spin_lock(&kvm->mmu_lock);
5547 kvm->arch.radix = 1;
5548 spin_unlock(&kvm->mmu_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005549 kvmppc_free_hpt(&kvm->arch.hpt);
Nicholas Piggineacc8182021-11-23 19:51:46 +10005550
5551 lpcr = LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5552 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5553 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5554 lpcr_mask |= LPCR_HAIL;
5555 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5556 (kvm->arch.host_lpcr & LPCR_HAIL))
5557 lpcr |= LPCR_HAIL;
5558 }
5559 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5560
Paul Mackerras18c36402017-09-13 16:00:10 +10005561 return 0;
5562}
5563
Suresh Warrier79b6c242015-12-17 14:59:06 -06005564#ifdef CONFIG_KVM_XICS
5565/*
5566 * Allocate a per-core structure for managing state about which cores are
5567 * running in the host versus the guest and for exchanging data between
5568 * real mode KVM and CPU running in the host.
5569 * This is only done for the first VM.
5570 * The allocated structure stays even if all VMs have stopped.
5571 * It is only freed when the kvm-hv module is unloaded.
5572 * It's OK for this routine to fail, we just don't support host
5573 * core operations like redirecting H_IPI wakeups.
5574 */
5575void kvmppc_alloc_host_rm_ops(void)
5576{
5577 struct kvmppc_host_rm_ops *ops;
5578 unsigned long l_ops;
5579 int cpu, core;
5580 int size;
5581
Nicholas Pigginecb6a722021-11-23 19:52:28 +10005582 if (cpu_has_feature(CPU_FTR_ARCH_300))
5583 return;
5584
Suresh Warrier79b6c242015-12-17 14:59:06 -06005585 /* Not the first time here ? */
5586 if (kvmppc_host_rm_ops_hv != NULL)
5587 return;
5588
5589 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
5590 if (!ops)
5591 return;
5592
5593 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
5594 ops->rm_core = kzalloc(size, GFP_KERNEL);
5595
5596 if (!ops->rm_core) {
5597 kfree(ops);
5598 return;
5599 }
5600
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005601 cpus_read_lock();
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005602
Suresh Warrier79b6c242015-12-17 14:59:06 -06005603 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
5604 if (!cpu_online(cpu))
5605 continue;
5606
5607 core = cpu >> threads_shift;
5608 ops->rm_core[core].rm_state.in_host = 1;
5609 }
5610
Suresh Warrier0c2a6602015-12-17 14:59:09 -06005611 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
5612
Suresh Warrier79b6c242015-12-17 14:59:06 -06005613 /*
5614 * Make the contents of the kvmppc_host_rm_ops structure visible
5615 * to other CPUs before we assign it to the global variable.
5616 * Do an atomic assignment (no locks used here), but if someone
5617 * beats us to it, just free our copy and return.
5618 */
5619 smp_wmb();
5620 l_ops = (unsigned long) ops;
5621
5622 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005623 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06005624 kfree(ops->rm_core);
5625 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005626 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06005627 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005628
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005629 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
5630 "ppc/kvm_book3s:prepare",
5631 kvmppc_set_host_core,
5632 kvmppc_clear_host_core);
5633 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06005634}
5635
5636void kvmppc_free_host_rm_ops(void)
5637{
5638 if (kvmppc_host_rm_ops_hv) {
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01005639 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
Suresh Warrier79b6c242015-12-17 14:59:06 -06005640 kfree(kvmppc_host_rm_ops_hv->rm_core);
5641 kfree(kvmppc_host_rm_ops_hv);
5642 kvmppc_host_rm_ops_hv = NULL;
5643 }
5644}
5645#endif
5646
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305647static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005648{
Paul Mackerras32fad282012-05-04 02:32:53 +00005649 unsigned long lpcr, lpid;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005650 int ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005651
Bharata B Raoca9f4942019-11-25 08:36:26 +05305652 mutex_init(&kvm->arch.uvmem_lock);
5653 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005654 mutex_init(&kvm->arch.mmu_setup_lock);
5655
Paul Mackerras32fad282012-05-04 02:32:53 +00005656 /* Allocate the guest's logical partition ID */
5657
Jordan Niethe19d31c52023-09-14 13:05:59 +10005658 if (!kvmhv_is_nestedv2()) {
5659 lpid = kvmppc_alloc_lpid();
5660 if ((long)lpid < 0)
5661 return -ENOMEM;
5662 kvm->arch.lpid = lpid;
5663 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00005664
Suresh Warrier79b6c242015-12-17 14:59:06 -06005665 kvmppc_alloc_host_rm_ops();
5666
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005667 kvmhv_vm_nested_init(kvm);
5668
Jordan Niethe19d31c52023-09-14 13:05:59 +10005669 if (kvmhv_is_nestedv2()) {
5670 long rc;
5671 unsigned long guest_id;
5672
5673 rc = plpar_guest_create(0, &guest_id);
5674
5675 if (rc != H_SUCCESS)
5676 pr_err("KVM: Create Guest hcall failed, rc=%ld\n", rc);
5677
5678 switch (rc) {
5679 case H_PARAMETER:
5680 case H_FUNCTION:
5681 case H_STATE:
5682 return -EINVAL;
5683 case H_NOT_ENOUGH_RESOURCES:
5684 case H_ABORTED:
5685 return -ENOMEM;
5686 case H_AUTHORITY:
5687 return -EPERM;
5688 case H_NOT_AVAILABLE:
5689 return -EBUSY;
5690 }
5691 kvm->arch.lpid = guest_id;
5692 }
5693
5694
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005695 /*
5696 * Since we don't flush the TLB when tearing down a VM,
5697 * and this lpid might have previously been used,
5698 * make sure we flush on each core before running the new VM.
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005699 * On POWER9, the tlbie in mmu_partition_table_set_entry()
5700 * does this flush for us.
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005701 */
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005702 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5703 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005704
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005705 /* Start out with the default set of hcalls enabled */
5706 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
5707 sizeof(kvm->arch.enabled_hcalls));
5708
Paul Mackerras7a840842016-11-16 22:25:20 +11005709 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5710 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005711
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005712 /* Init LPCR for virtual RMA mode */
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11005713 if (cpu_has_feature(CPU_FTR_HVMODE)) {
5714 kvm->arch.host_lpid = mfspr(SPRN_LPID);
5715 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
5716 lpcr &= LPCR_PECE | LPCR_LPES;
5717 } else {
Nicholas Piggin2852ebf2022-03-03 15:33:15 +10005718 /*
5719 * The L2 LPES mode will be set by the L0 according to whether
5720 * or not it needs to take external interrupts in HV mode.
5721 */
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11005722 lpcr = 0;
5723 }
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005724 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
5725 LPCR_VPM0 | LPCR_VPM1;
5726 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
5727 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5728 /* On POWER8 turn on online bit to enable PURR/SPURR */
5729 if (cpu_has_feature(CPU_FTR_ARCH_207S))
5730 lpcr |= LPCR_ONL;
Paul Mackerras84f71392016-11-22 14:30:14 +11005731 /*
5732 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
5733 * Set HVICE bit to enable hypervisor virtualization interrupts.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005734 * Set HEIC to prevent OS interrupts to go to hypervisor (should
5735 * be unnecessary but better safe than sorry in case we re-enable
5736 * EE in HV mode with this LPCR still set)
Paul Mackerras84f71392016-11-22 14:30:14 +11005737 */
5738 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerras7a840842016-11-16 22:25:20 +11005739 lpcr &= ~LPCR_VPM0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005740 lpcr |= LPCR_HVICE | LPCR_HEIC;
5741
5742 /*
5743 * If xive is enabled, we route 0x500 interrupts directly
5744 * to the guest.
5745 */
Paul Mackerras03f95332019-02-04 22:07:20 +11005746 if (xics_on_xive())
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005747 lpcr |= LPCR_LPES;
Paul Mackerras84f71392016-11-22 14:30:14 +11005748 }
5749
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005750 /*
Paul Mackerras18c36402017-09-13 16:00:10 +10005751 * If the host uses radix, the guest starts out as radix.
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005752 */
5753 if (radix_enabled()) {
5754 kvm->arch.radix = 1;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005755 kvm->arch.mmu_ready = 1;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005756 lpcr &= ~LPCR_VPM1;
5757 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
Nicholas Piggineacc8182021-11-23 19:51:46 +10005758 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5759 cpu_has_feature(CPU_FTR_ARCH_31) &&
5760 (kvm->arch.host_lpcr & LPCR_HAIL))
5761 lpcr |= LPCR_HAIL;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005762 ret = kvmppc_init_vm_radix(kvm);
5763 if (ret) {
Jordan Niethe19d31c52023-09-14 13:05:59 +10005764 if (kvmhv_is_nestedv2())
5765 plpar_guest_delete(0, kvm->arch.lpid);
5766 else
5767 kvmppc_free_lpid(kvm->arch.lpid);
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005768 return ret;
5769 }
5770 kvmppc_setup_partition_table(kvm);
5771 }
5772
Nicholas Piggin67145ef2021-04-12 11:48:36 +10005773 verify_lpcr(kvm, lpcr);
Paul Mackerras9e368f22011-06-29 00:40:08 +00005774 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005775
David Gibson5e985962016-12-20 16:49:05 +11005776 /* Initialization for future HPT resizes */
5777 kvm->arch.resize_hpt = NULL;
5778
Paul Mackerras512691d2012-10-15 01:15:41 +00005779 /*
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005780 * Work out how many sets the TLB has, for the use of
5781 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
5782 */
Aneesh Kumar K.Ve8063942020-10-07 11:03:05 +05305783 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5784 /*
5785 * P10 will flush all the congruence class with a single tlbiel
5786 */
5787 kvm->arch.tlb_sets = 1;
5788 } else if (radix_enabled())
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005789 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
5790 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005791 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
5792 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
5793 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
5794 else
5795 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
5796
5797 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10005798 * Track that we now have a HV mode VM active. This blocks secondary
5799 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00005800 */
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10005801 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005802 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00005803
Paul Mackerrase23a8082015-03-28 14:21:01 +11005804 /*
Paul Mackerras3c313522017-02-06 13:24:41 +11005805 * Initialize smt_mode depending on processor.
5806 * POWER8 and earlier have to use "strict" threading, where
5807 * all vCPUs in a vcore have to run on the same (sub)core,
5808 * whereas on POWER9 the threads can each run a different
5809 * guest.
5810 */
5811 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5812 kvm->arch.smt_mode = threads_per_subcore;
5813 else
5814 kvm->arch.smt_mode = 1;
Paul Mackerras57900692017-05-16 16:41:20 +10005815 kvm->arch.emul_smt_mode = 1;
Paul Mackerras3c313522017-02-06 13:24:41 +11005816
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11005817 return 0;
5818}
5819
5820static int kvmppc_arch_create_vm_debugfs_hv(struct kvm *kvm)
5821{
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02005822 kvmppc_mmu_debugfs_init(kvm);
Paul Mackerras9a94d3e2018-10-08 16:30:57 +11005823 if (radix_enabled())
5824 kvmhv_radix_debugfs_init(kvm);
David Gibson54738c02011-06-29 00:22:41 +00005825 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005826}
5827
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305828static void kvmppc_free_vcores(struct kvm *kvm)
5829{
5830 long int i;
5831
Paul Mackerras23316312015-10-21 16:03:14 +11005832 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305833 kfree(kvm->arch.vcores[i]);
5834 kvm->arch.online_vcores = 0;
5835}
5836
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305837static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005838{
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10005839 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005840 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00005841
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305842 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005843
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005844
Paul Mackerras5a319352017-01-30 21:21:46 +11005845 if (kvm_is_radix(kvm))
5846 kvmppc_free_radix(kvm);
5847 else
David Gibsonaae07772016-12-20 16:49:02 +11005848 kvmppc_free_hpt(&kvm->arch.hpt);
Suresh Warrierc57875f2016-08-19 15:35:50 +10005849
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005850 /* Perform global invalidation and return lpid to the pool */
5851 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005852 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005853 kvmhv_release_all_nested(kvm);
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005854 kvm->arch.process_table = 0;
Paul Mackerrasd89c69f2019-12-18 11:43:06 +11005855 if (kvm->arch.secure_guest)
5856 uv_svm_terminate(kvm->arch.lpid);
Jordan Niethe19d31c52023-09-14 13:05:59 +10005857 if (!kvmhv_is_nestedv2())
5858 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005859 }
Bharata B Raoca9f4942019-11-25 08:36:26 +05305860
Jordan Niethe7d370e12023-12-01 18:56:06 +05305861 if (kvmhv_is_nestedv2()) {
5862 kvmhv_flush_lpid(kvm->arch.lpid);
Jordan Niethe19d31c52023-09-14 13:05:59 +10005863 plpar_guest_delete(0, kvm->arch.lpid);
Jordan Niethe7d370e12023-12-01 18:56:06 +05305864 } else {
Jordan Niethe19d31c52023-09-14 13:05:59 +10005865 kvmppc_free_lpid(kvm->arch.lpid);
Jordan Niethe7d370e12023-12-01 18:56:06 +05305866 }
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005867
Suresh Warrierc57875f2016-08-19 15:35:50 +10005868 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00005869}
5870
5871/* We don't need to emulate any privileged instructions or dcbz */
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005872static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305873 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005874{
5875 return EMULATE_FAIL;
5876}
5877
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305878static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
5879 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005880{
5881 return EMULATE_FAIL;
5882}
5883
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305884static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
5885 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005886{
5887 return EMULATE_FAIL;
5888}
5889
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305890static int kvmppc_core_check_processor_compat_hv(void)
5891{
Paul Mackerrasde760db2018-10-08 16:31:16 +11005892 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5893 cpu_has_feature(CPU_FTR_ARCH_206))
5894 return 0;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10005895
Paul Mackerrasde760db2018-10-08 16:31:16 +11005896 /* POWER9 in radix mode is capable of being a nested hypervisor. */
5897 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
5898 return 0;
5899
5900 return -EIO;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305901}
5902
Suresh Warrier8daaafc2016-08-19 15:35:48 +10005903#ifdef CONFIG_KVM_XICS
5904
5905void kvmppc_free_pimap(struct kvm *kvm)
5906{
5907 kfree(kvm->arch.pimap);
5908}
5909
Suresh Warrierc57875f2016-08-19 15:35:50 +10005910static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10005911{
5912 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
5913}
Suresh Warrierc57875f2016-08-19 15:35:50 +10005914
5915static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5916{
5917 struct irq_desc *desc;
5918 struct kvmppc_irq_map *irq_map;
5919 struct kvmppc_passthru_irqmap *pimap;
5920 struct irq_chip *chip;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005921 int i, rc = 0;
Cédric Le Goater17530812021-07-01 15:27:48 +02005922 struct irq_data *host_data;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005923
Suresh Warrier644abbb2016-08-19 15:35:54 +10005924 if (!kvm_irq_bypass)
5925 return 1;
5926
Suresh Warrierc57875f2016-08-19 15:35:50 +10005927 desc = irq_to_desc(host_irq);
5928 if (!desc)
5929 return -EIO;
5930
5931 mutex_lock(&kvm->lock);
5932
5933 pimap = kvm->arch.pimap;
5934 if (pimap == NULL) {
5935 /* First call, allocate structure to hold IRQ map */
5936 pimap = kvmppc_alloc_pimap();
5937 if (pimap == NULL) {
5938 mutex_unlock(&kvm->lock);
5939 return -ENOMEM;
5940 }
5941 kvm->arch.pimap = pimap;
5942 }
5943
5944 /*
5945 * For now, we only support interrupts for which the EOI operation
5946 * is an OPAL call followed by a write to XIRR, since that's
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005947 * what our real-mode EOI code does, or a XIVE interrupt
Suresh Warrierc57875f2016-08-19 15:35:50 +10005948 */
5949 chip = irq_data_get_irq_chip(&desc->irq_data);
Cédric Le Goaterba418a02021-07-01 15:27:31 +02005950 if (!chip || !is_pnv_opal_msi(chip)) {
Suresh Warrierc57875f2016-08-19 15:35:50 +10005951 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
5952 host_irq, guest_gsi);
5953 mutex_unlock(&kvm->lock);
5954 return -ENOENT;
5955 }
5956
5957 /*
5958 * See if we already have an entry for this guest IRQ number.
5959 * If it's mapped to a hardware IRQ number, that's an error,
5960 * otherwise re-use this entry.
5961 */
5962 for (i = 0; i < pimap->n_mapped; i++) {
5963 if (guest_gsi == pimap->mapped[i].v_hwirq) {
5964 if (pimap->mapped[i].r_hwirq) {
5965 mutex_unlock(&kvm->lock);
5966 return -EINVAL;
5967 }
5968 break;
5969 }
5970 }
5971
5972 if (i == KVMPPC_PIRQ_MAPPED) {
5973 mutex_unlock(&kvm->lock);
5974 return -EAGAIN; /* table is full */
5975 }
5976
5977 irq_map = &pimap->mapped[i];
5978
5979 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005980 irq_map->desc = desc;
5981
Suresh Warriere3c13e52016-08-19 15:35:51 +10005982 /*
5983 * Order the above two stores before the next to serialize with
5984 * the KVM real mode handler.
5985 */
5986 smp_wmb();
Cédric Le Goater17530812021-07-01 15:27:48 +02005987
5988 /*
5989 * The 'host_irq' number is mapped in the PCI-MSI domain but
5990 * the underlying calls, which will EOI the interrupt in real
5991 * mode, need an HW IRQ number mapped in the XICS IRQ domain.
5992 */
5993 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
5994 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
Suresh Warriere3c13e52016-08-19 15:35:51 +10005995
Suresh Warrierc57875f2016-08-19 15:35:50 +10005996 if (i == pimap->n_mapped)
5997 pimap->n_mapped++;
5998
Paul Mackerras03f95332019-02-04 22:07:20 +11005999 if (xics_on_xive())
Cédric Le Goatere5e78b12021-07-01 15:27:32 +02006000 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10006001 else
Cédric Le Goater17530812021-07-01 15:27:48 +02006002 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10006003 if (rc)
6004 irq_map->r_hwirq = 0;
Paul Mackerras5d375192016-08-19 15:35:56 +10006005
Suresh Warrierc57875f2016-08-19 15:35:50 +10006006 mutex_unlock(&kvm->lock);
6007
6008 return 0;
6009}
6010
6011static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
6012{
6013 struct irq_desc *desc;
6014 struct kvmppc_passthru_irqmap *pimap;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10006015 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10006016
Suresh Warrier644abbb2016-08-19 15:35:54 +10006017 if (!kvm_irq_bypass)
6018 return 0;
6019
Suresh Warrierc57875f2016-08-19 15:35:50 +10006020 desc = irq_to_desc(host_irq);
6021 if (!desc)
6022 return -EIO;
6023
6024 mutex_lock(&kvm->lock);
Markus Elfringa1c52e12017-01-20 11:00:08 +01006025 if (!kvm->arch.pimap)
6026 goto unlock;
Suresh Warrierc57875f2016-08-19 15:35:50 +10006027
Suresh Warrierc57875f2016-08-19 15:35:50 +10006028 pimap = kvm->arch.pimap;
6029
6030 for (i = 0; i < pimap->n_mapped; i++) {
6031 if (guest_gsi == pimap->mapped[i].v_hwirq)
6032 break;
6033 }
6034
6035 if (i == pimap->n_mapped) {
6036 mutex_unlock(&kvm->lock);
6037 return -ENODEV;
6038 }
6039
Paul Mackerras03f95332019-02-04 22:07:20 +11006040 if (xics_on_xive())
Cédric Le Goatere5e78b12021-07-01 15:27:32 +02006041 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10006042 else
6043 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
Paul Mackerras5d375192016-08-19 15:35:56 +10006044
Michael Ellerman2b461882022-07-18 19:51:58 +10006045 /* invalidate the entry (what to do on error from the above ?) */
Suresh Warrierc57875f2016-08-19 15:35:50 +10006046 pimap->mapped[i].r_hwirq = 0;
6047
6048 /*
6049 * We don't free this structure even when the count goes to
6050 * zero. The structure is freed when we destroy the VM.
6051 */
Markus Elfringa1c52e12017-01-20 11:00:08 +01006052 unlock:
Suresh Warrierc57875f2016-08-19 15:35:50 +10006053 mutex_unlock(&kvm->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10006054 return rc;
Suresh Warrierc57875f2016-08-19 15:35:50 +10006055}
6056
6057static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
6058 struct irq_bypass_producer *prod)
6059{
6060 int ret = 0;
6061 struct kvm_kernel_irqfd *irqfd =
6062 container_of(cons, struct kvm_kernel_irqfd, consumer);
6063
6064 irqfd->producer = prod;
6065
6066 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
6067 if (ret)
6068 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
6069 prod->irq, irqfd->gsi, ret);
6070
6071 return ret;
6072}
6073
6074static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
6075 struct irq_bypass_producer *prod)
6076{
6077 int ret;
6078 struct kvm_kernel_irqfd *irqfd =
6079 container_of(cons, struct kvm_kernel_irqfd, consumer);
6080
6081 irqfd->producer = NULL;
6082
6083 /*
6084 * When producer of consumer is unregistered, we change back to
6085 * default external interrupt handling mode - KVM real mode
6086 * will switch back to host.
6087 */
6088 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
6089 if (ret)
6090 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
6091 prod->irq, irqfd->gsi, ret);
6092}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10006093#endif
6094
Thomas Huth67c48662e2023-02-08 15:01:00 +01006095static int kvm_arch_vm_ioctl_hv(struct file *filp,
6096 unsigned int ioctl, unsigned long arg)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306097{
6098 struct kvm *kvm __maybe_unused = filp->private_data;
6099 void __user *argp = (void __user *)arg;
Thomas Huth67c48662e2023-02-08 15:01:00 +01006100 int r;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306101
6102 switch (ioctl) {
6103
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306104 case KVM_PPC_ALLOCATE_HTAB: {
6105 u32 htab_order;
6106
Fabiano Rosas05e6295d2020-09-11 01:16:07 -03006107 /* If we're a nested hypervisor, we currently only support radix */
6108 if (kvmhv_on_pseries()) {
6109 r = -EOPNOTSUPP;
6110 break;
6111 }
6112
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306113 r = -EFAULT;
6114 if (get_user(htab_order, (u32 __user *)argp))
6115 break;
David Gibsonf98a8bf2016-12-20 16:49:03 +11006116 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306117 if (r)
6118 break;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306119 r = 0;
6120 break;
6121 }
6122
6123 case KVM_PPC_GET_HTAB_FD: {
6124 struct kvm_get_htab_fd ghf;
6125
6126 r = -EFAULT;
6127 if (copy_from_user(&ghf, argp, sizeof(ghf)))
6128 break;
6129 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
6130 break;
6131 }
6132
David Gibson5e985962016-12-20 16:49:05 +11006133 case KVM_PPC_RESIZE_HPT_PREPARE: {
6134 struct kvm_ppc_resize_hpt rhpt;
6135
6136 r = -EFAULT;
6137 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
6138 break;
6139
6140 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
6141 break;
6142 }
6143
6144 case KVM_PPC_RESIZE_HPT_COMMIT: {
6145 struct kvm_ppc_resize_hpt rhpt;
6146
6147 r = -EFAULT;
6148 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
6149 break;
6150
6151 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
6152 break;
6153 }
6154
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306155 default:
6156 r = -ENOTTY;
6157 }
6158
6159 return r;
6160}
6161
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006162/*
6163 * List of hcall numbers to enable by default.
6164 * For compatibility with old userspace, we enable by default
6165 * all hcalls that were implemented before the hcall-enabling
6166 * facility was added. Note this list should not include H_RTAS.
6167 */
6168static unsigned int default_hcall_list[] = {
6169 H_REMOVE,
6170 H_ENTER,
6171 H_READ,
6172 H_PROTECT,
6173 H_BULK_REMOVE,
Nicholas Piggin0fd85cb82021-04-12 11:48:41 +10006174#ifdef CONFIG_SPAPR_TCE_IOMMU
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006175 H_GET_TCE,
6176 H_PUT_TCE,
Nicholas Piggin0fd85cb82021-04-12 11:48:41 +10006177#endif
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006178 H_SET_DABR,
6179 H_SET_XDABR,
6180 H_CEDE,
6181 H_PROD,
6182 H_CONFER,
6183 H_REGISTER_VPA,
6184#ifdef CONFIG_KVM_XICS
6185 H_EOI,
6186 H_CPPR,
6187 H_IPI,
6188 H_IPOLL,
6189 H_XIRR,
6190 H_XIRR_X,
6191#endif
6192 0
6193};
6194
6195static void init_default_hcalls(void)
6196{
6197 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10006198 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006199
Paul Mackerrasae2113a2014-06-02 11:03:00 +10006200 for (i = 0; default_hcall_list[i]; ++i) {
6201 hcall = default_hcall_list[i];
6202 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
6203 __set_bit(hcall / 4, default_enabled_hcalls);
6204 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006205}
6206
Paul Mackerrasc9270132017-01-30 21:21:41 +11006207static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
6208{
Paul Mackerras468808b2017-01-30 21:21:42 +11006209 unsigned long lpcr;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11006210 int radix;
Paul Mackerras18c36402017-09-13 16:00:10 +10006211 int err;
Paul Mackerras468808b2017-01-30 21:21:42 +11006212
6213 /* If not on a POWER9, reject it */
6214 if (!cpu_has_feature(CPU_FTR_ARCH_300))
6215 return -ENODEV;
6216
6217 /* If any unknown flags set, reject it */
6218 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
6219 return -EINVAL;
6220
Paul Mackerras468808b2017-01-30 21:21:42 +11006221 /* GR (guest radix) bit in process_table field must match */
Paul Mackerras18c36402017-09-13 16:00:10 +10006222 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11006223 if (!!(cfg->process_table & PATB_GR) != radix)
Paul Mackerras468808b2017-01-30 21:21:42 +11006224 return -EINVAL;
6225
6226 /* Process table size field must be reasonable, i.e. <= 24 */
6227 if ((cfg->process_table & PRTS_MASK) > 24)
6228 return -EINVAL;
6229
Paul Mackerras18c36402017-09-13 16:00:10 +10006230 /* We can change a guest to/from radix now, if the host is radix */
6231 if (radix && !radix_enabled())
6232 return -EINVAL;
6233
Paul Mackerrasde760db2018-10-08 16:31:16 +11006234 /* If we're a nested hypervisor, we currently only support radix */
6235 if (kvmhv_on_pseries() && !radix)
6236 return -EINVAL;
6237
Paul Mackerras0d4ee882019-05-23 16:35:34 +10006238 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10006239 if (radix != kvm_is_radix(kvm)) {
6240 if (kvm->arch.mmu_ready) {
6241 kvm->arch.mmu_ready = 0;
6242 /* order mmu_ready vs. vcpus_running */
6243 smp_mb();
6244 if (atomic_read(&kvm->arch.vcpus_running)) {
6245 kvm->arch.mmu_ready = 1;
6246 err = -EBUSY;
6247 goto out_unlock;
6248 }
6249 }
6250 if (radix)
6251 err = kvmppc_switch_mmu_to_radix(kvm);
6252 else
6253 err = kvmppc_switch_mmu_to_hpt(kvm);
6254 if (err)
6255 goto out_unlock;
6256 }
6257
Paul Mackerras468808b2017-01-30 21:21:42 +11006258 kvm->arch.process_table = cfg->process_table;
6259 kvmppc_setup_partition_table(kvm);
6260
6261 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
6262 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
Paul Mackerras18c36402017-09-13 16:00:10 +10006263 err = 0;
Paul Mackerras468808b2017-01-30 21:21:42 +11006264
Paul Mackerras18c36402017-09-13 16:00:10 +10006265 out_unlock:
Paul Mackerras0d4ee882019-05-23 16:35:34 +10006266 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10006267 return err;
Paul Mackerrasc9270132017-01-30 21:21:41 +11006268}
6269
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006270static int kvmhv_enable_nested(struct kvm *kvm)
6271{
6272 if (!nested)
6273 return -EPERM;
Nicholas Piggincbcff8b2021-05-28 19:07:46 +10006274 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006275 return -ENODEV;
Nicholas Piggin0bf7e1b2021-05-28 19:07:51 +10006276 if (!radix_enabled())
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006277 return -ENODEV;
Jordan Niethe19d31c52023-09-14 13:05:59 +10006278 if (kvmhv_is_nestedv2())
6279 return -ENODEV;
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006280
6281 /* kvm == NULL means the caller is testing if the capability exists */
6282 if (kvm)
6283 kvm->arch.nested_enable = true;
6284 return 0;
6285}
6286
Suraj Jitindar Singhdceadcf92018-12-14 16:29:06 +11006287static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
6288 int size)
6289{
6290 int rc = -EINVAL;
6291
6292 if (kvmhv_vcpu_is_radix(vcpu)) {
6293 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
6294
6295 if (rc > 0)
6296 rc = -EINVAL;
6297 }
6298
6299 /* For now quadrants are the only way to access nested guest memory */
6300 if (rc && vcpu->arch.nested)
6301 rc = -EAGAIN;
6302
6303 return rc;
6304}
6305
6306static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
6307 int size)
6308{
6309 int rc = -EINVAL;
6310
6311 if (kvmhv_vcpu_is_radix(vcpu)) {
6312 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
6313
6314 if (rc > 0)
6315 rc = -EINVAL;
6316 }
6317
6318 /* For now quadrants are the only way to access nested guest memory */
6319 if (rc && vcpu->arch.nested)
6320 rc = -EAGAIN;
6321
6322 return rc;
6323}
6324
Bharata B Rao22945682019-11-25 08:36:30 +05306325static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
6326{
6327 unpin_vpa(kvm, vpa);
6328 vpa->gpa = 0;
6329 vpa->pinned_addr = NULL;
6330 vpa->dirty = false;
6331 vpa->update_pending = 0;
6332}
6333
6334/*
Paul Mackerras9a5788c2020-03-19 15:29:55 +11006335 * Enable a guest to become a secure VM, or test whether
6336 * that could be enabled.
6337 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
6338 * tested (kvm == NULL) or enabled (kvm != NULL).
6339 */
6340static int kvmhv_enable_svm(struct kvm *kvm)
6341{
6342 if (!kvmppc_uvmem_available())
6343 return -EINVAL;
6344 if (kvm)
6345 kvm->arch.svm_enabled = 1;
6346 return 0;
6347}
6348
6349/*
Bharata B Rao22945682019-11-25 08:36:30 +05306350 * IOCTL handler to turn off secure mode of guest
6351 *
6352 * - Release all device pages
6353 * - Issue ucall to terminate the guest on the UV side
6354 * - Unpin the VPA pages.
6355 * - Reinit the partition scoped page tables
6356 */
6357static int kvmhv_svm_off(struct kvm *kvm)
6358{
6359 struct kvm_vcpu *vcpu;
6360 int mmu_was_ready;
6361 int srcu_idx;
6362 int ret = 0;
Marc Zyngier46808a42021-11-16 16:04:02 +00006363 unsigned long i;
Bharata B Rao22945682019-11-25 08:36:30 +05306364
6365 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
6366 return ret;
6367
6368 mutex_lock(&kvm->arch.mmu_setup_lock);
6369 mmu_was_ready = kvm->arch.mmu_ready;
6370 if (kvm->arch.mmu_ready) {
6371 kvm->arch.mmu_ready = 0;
6372 /* order mmu_ready vs. vcpus_running */
6373 smp_mb();
6374 if (atomic_read(&kvm->arch.vcpus_running)) {
6375 kvm->arch.mmu_ready = 1;
6376 ret = -EBUSY;
6377 goto out;
6378 }
6379 }
6380
6381 srcu_idx = srcu_read_lock(&kvm->srcu);
Sean Christophersoneed52e42023-10-27 11:22:04 -07006382 for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
Bharata B Rao22945682019-11-25 08:36:30 +05306383 struct kvm_memory_slot *memslot;
6384 struct kvm_memslots *slots = __kvm_memslots(kvm, i);
Maciej S. Szmigieroa54d8062021-12-06 20:54:30 +01006385 int bkt;
Bharata B Rao22945682019-11-25 08:36:30 +05306386
6387 if (!slots)
6388 continue;
6389
Maciej S. Szmigieroa54d8062021-12-06 20:54:30 +01006390 kvm_for_each_memslot(memslot, bkt, slots) {
Sukadev Bhattiproluce477a72019-12-19 13:51:45 -08006391 kvmppc_uvmem_drop_pages(memslot, kvm, true);
Bharata B Rao22945682019-11-25 08:36:30 +05306392 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
6393 }
6394 }
6395 srcu_read_unlock(&kvm->srcu, srcu_idx);
6396
6397 ret = uv_svm_terminate(kvm->arch.lpid);
6398 if (ret != U_SUCCESS) {
6399 ret = -EINVAL;
6400 goto out;
6401 }
6402
6403 /*
6404 * When secure guest is reset, all the guest pages are sent
6405 * to UV via UV_PAGE_IN before the non-boot vcpus get a
6406 * chance to run and unpin their VPA pages. Unpinning of all
6407 * VPA pages is done here explicitly so that VPA pages
6408 * can be migrated to the secure side.
6409 *
6410 * This is required to for the secure SMP guest to reboot
6411 * correctly.
6412 */
6413 kvm_for_each_vcpu(i, vcpu, kvm) {
6414 spin_lock(&vcpu->arch.vpa_update_lock);
6415 unpin_vpa_reset(kvm, &vcpu->arch.dtl);
6416 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
6417 unpin_vpa_reset(kvm, &vcpu->arch.vpa);
6418 spin_unlock(&vcpu->arch.vpa_update_lock);
6419 }
6420
6421 kvmppc_setup_partition_table(kvm);
6422 kvm->arch.secure_guest = 0;
6423 kvm->arch.mmu_ready = mmu_was_ready;
6424out:
6425 mutex_unlock(&kvm->arch.mmu_setup_lock);
6426 return ret;
6427}
6428
Ravi Bangoriad9a47ed2020-12-16 16:12:19 +05306429static int kvmhv_enable_dawr1(struct kvm *kvm)
6430{
6431 if (!cpu_has_feature(CPU_FTR_DAWR1))
6432 return -ENODEV;
6433
6434 /* kvm == NULL means the caller is testing if the capability exists */
6435 if (kvm)
6436 kvm->arch.dawr1_enabled = true;
6437 return 0;
6438}
6439
Fabiano Rosasa7220762021-02-05 13:41:54 -03006440static bool kvmppc_hash_v3_possible(void)
6441{
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10006442 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Fabiano Rosasa7220762021-02-05 13:41:54 -03006443 return false;
6444
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10006445 if (!cpu_has_feature(CPU_FTR_HVMODE))
6446 return false;
6447
6448 /*
6449 * POWER9 chips before version 2.02 can't have some threads in
6450 * HPT mode and some in radix mode on the same core.
6451 */
6452 if (radix_enabled()) {
6453 unsigned int pvr = mfspr(SPRN_PVR);
6454 if ((pvr >> 16) == PVR_POWER9 &&
6455 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
6456 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
6457 return false;
6458 }
6459
6460 return true;
Fabiano Rosasa7220762021-02-05 13:41:54 -03006461}
6462
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306463static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306464 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
6465 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
6466 .get_one_reg = kvmppc_get_one_reg_hv,
6467 .set_one_reg = kvmppc_set_one_reg_hv,
6468 .vcpu_load = kvmppc_core_vcpu_load_hv,
6469 .vcpu_put = kvmppc_core_vcpu_put_hv,
Nicholas Piggin87a45e02019-10-02 16:00:22 +10006470 .inject_interrupt = kvmppc_inject_interrupt_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306471 .set_msr = kvmppc_set_msr_hv,
6472 .vcpu_run = kvmppc_vcpu_run_hv,
6473 .vcpu_create = kvmppc_core_vcpu_create_hv,
6474 .vcpu_free = kvmppc_core_vcpu_free_hv,
6475 .check_requests = kvmppc_core_check_requests_hv,
6476 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
6477 .flush_memslot = kvmppc_core_flush_memslot_hv,
6478 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
6479 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
Sean Christophersonb1c53562021-04-01 17:56:53 -07006480 .unmap_gfn_range = kvm_unmap_gfn_range_hv,
6481 .age_gfn = kvm_age_gfn_hv,
6482 .test_age_gfn = kvm_test_age_gfn_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306483 .free_memslot = kvmppc_core_free_memslot_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306484 .init_vm = kvmppc_core_init_vm_hv,
6485 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306486 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
6487 .emulate_op = kvmppc_core_emulate_op_hv,
6488 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
6489 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
6490 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
6491 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10006492 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10006493#ifdef CONFIG_KVM_XICS
6494 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
6495 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
6496#endif
Paul Mackerrasc9270132017-01-30 21:21:41 +11006497 .configure_mmu = kvmhv_configure_mmu,
6498 .get_rmmu_info = kvmhv_get_rmmu_info,
Paul Mackerras3c313522017-02-06 13:24:41 +11006499 .set_smt_mode = kvmhv_set_smt_mode,
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006500 .enable_nested = kvmhv_enable_nested,
Suraj Jitindar Singhdceadcf92018-12-14 16:29:06 +11006501 .load_from_eaddr = kvmhv_load_from_eaddr,
6502 .store_to_eaddr = kvmhv_store_to_eaddr,
Paul Mackerras9a5788c2020-03-19 15:29:55 +11006503 .enable_svm = kvmhv_enable_svm,
Bharata B Rao22945682019-11-25 08:36:30 +05306504 .svm_off = kvmhv_svm_off,
Ravi Bangoriad9a47ed2020-12-16 16:12:19 +05306505 .enable_dawr1 = kvmhv_enable_dawr1,
Fabiano Rosasa7220762021-02-05 13:41:54 -03006506 .hash_v3_possible = kvmppc_hash_v3_possible,
Alexey Kardashevskiyfaf01ae2022-01-11 11:54:04 +11006507 .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv,
6508 .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306509};
6510
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306511static int kvm_init_subcore_bitmap(void)
6512{
6513 int i, j;
6514 int nr_cores = cpu_nr_cores();
6515 struct sibling_subcore_state *sibling_subcore_state;
6516
6517 for (i = 0; i < nr_cores; i++) {
6518 int first_cpu = i * threads_per_core;
6519 int node = cpu_to_node(first_cpu);
6520
6521 /* Ignore if it is already allocated. */
Nicholas Piggind2e60072018-02-14 01:08:12 +10006522 if (paca_ptrs[first_cpu]->sibling_subcore_state)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306523 continue;
6524
6525 sibling_subcore_state =
wangbo08434ab2019-01-07 20:15:52 +08006526 kzalloc_node(sizeof(struct sibling_subcore_state),
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306527 GFP_KERNEL, node);
6528 if (!sibling_subcore_state)
6529 return -ENOMEM;
6530
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306531
6532 for (j = 0; j < threads_per_core; j++) {
6533 int cpu = first_cpu + j;
6534
Nicholas Piggind2e60072018-02-14 01:08:12 +10006535 paca_ptrs[cpu]->sibling_subcore_state =
6536 sibling_subcore_state;
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306537 }
6538 }
6539 return 0;
6540}
6541
Paul Mackerras5a319352017-01-30 21:21:46 +11006542static int kvmppc_radix_possible(void)
6543{
6544 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
6545}
6546
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306547static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00006548{
6549 int r;
Nicholas Piggin2275d7b2019-09-03 01:29:31 +10006550
6551 if (!tlbie_capable) {
6552 pr_err("KVM-HV: Host does not support TLBIE\n");
6553 return -ENODEV;
6554 }
6555
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306556 /*
6557 * FIXME!! Do we need to check on all cpus ?
6558 */
6559 r = kvmppc_core_check_processor_compat_hv();
6560 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11006561 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00006562
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11006563 r = kvmhv_nested_init();
6564 if (r)
6565 return r;
6566
Nicholas Piggin9c5a4322021-11-23 19:52:31 +10006567 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
6568 r = kvm_init_subcore_bitmap();
6569 if (r)
Fabiano Rosas175be7e2022-01-25 12:57:34 -03006570 goto err;
Nicholas Piggin9c5a4322021-11-23 19:52:31 +10006571 }
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306572
Paul Mackerrasf7257582016-11-18 09:02:08 +11006573 /*
6574 * We need a way of accessing the XICS interrupt controller,
Nicholas Piggind2e60072018-02-14 01:08:12 +10006575 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
Paul Mackerrasf7257582016-11-18 09:02:08 +11006576 * indirectly, via OPAL.
6577 */
6578#ifdef CONFIG_SMP
Paul Mackerras03f95332019-02-04 22:07:20 +11006579 if (!xics_on_xive() && !kvmhv_on_pseries() &&
Paul Mackerrasf3c18e92018-10-08 16:31:05 +11006580 !local_paca->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +11006581 struct device_node *np;
6582
6583 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
6584 if (!np) {
6585 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
Fabiano Rosas175be7e2022-01-25 12:57:34 -03006586 r = -ENODEV;
6587 goto err;
Paul Mackerrasf7257582016-11-18 09:02:08 +11006588 }
Nicholas Mc Guire51eaa082018-07-07 08:53:07 +02006589 /* presence of intc confirmed - node can be dropped again */
6590 of_node_put(np);
Paul Mackerrasf7257582016-11-18 09:02:08 +11006591 }
6592#endif
6593
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006594 init_default_hcalls();
6595
Paul Mackerrasec257162015-06-24 21:18:03 +10006596 init_vcore_lists();
6597
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306598 r = kvmppc_mmu_hv_init();
Paul Mackerras5a319352017-01-30 21:21:46 +11006599 if (r)
Fabiano Rosas175be7e2022-01-25 12:57:34 -03006600 goto err;
Paul Mackerras5a319352017-01-30 21:21:46 +11006601
Fabiano Rosas69ab6ac2022-01-25 12:57:32 -03006602 if (kvmppc_radix_possible()) {
Paul Mackerras5a319352017-01-30 21:21:46 +11006603 r = kvmppc_radix_init();
Fabiano Rosas69ab6ac2022-01-25 12:57:32 -03006604 if (r)
Fabiano Rosas175be7e2022-01-25 12:57:34 -03006605 goto err;
Fabiano Rosas69ab6ac2022-01-25 12:57:32 -03006606 }
Paul Mackerras00608e12018-01-11 16:54:26 +11006607
Bharata B Raoca9f4942019-11-25 08:36:26 +05306608 r = kvmppc_uvmem_init();
Fabiano Rosasc5d0d772022-01-25 12:57:33 -03006609 if (r < 0) {
Bharata B Raoca9f4942019-11-25 08:36:26 +05306610 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
Fabiano Rosasc5d0d772022-01-25 12:57:33 -03006611 return r;
6612 }
Bharata B Raoca9f4942019-11-25 08:36:26 +05306613
Fabiano Rosasc5d0d772022-01-25 12:57:33 -03006614 kvm_ops_hv.owner = THIS_MODULE;
6615 kvmppc_hv_ops = &kvm_ops_hv;
6616
6617 return 0;
Fabiano Rosas175be7e2022-01-25 12:57:34 -03006618
6619err:
6620 kvmhv_nested_exit();
6621 kvmppc_radix_exit();
6622
6623 return r;
Paul Mackerrasde56a942011-06-29 00:21:34 +00006624}
6625
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306626static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00006627{
Bharata B Raoca9f4942019-11-25 08:36:26 +05306628 kvmppc_uvmem_free();
Suresh Warrier79b6c242015-12-17 14:59:06 -06006629 kvmppc_free_host_rm_ops();
Paul Mackerras5a319352017-01-30 21:21:46 +11006630 if (kvmppc_radix_possible())
6631 kvmppc_radix_exit();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306632 kvmppc_hv_ops = NULL;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11006633 kvmhv_nested_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00006634}
6635
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306636module_init(kvmppc_book3s_init_hv);
6637module_exit(kvmppc_book3s_exit_hv);
Jeff Johnsonca8dad02024-06-15 08:18:59 -07006638MODULE_DESCRIPTION("KVM on Book3S (POWER8 and later) in hypervisor mode");
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05306639MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01006640MODULE_ALIAS_MISCDEV(KVM_MINOR);
6641MODULE_ALIAS("devname:kvm");