]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/xics_kvm.c
Merge remote-tracking branch 'remotes/dg-gitlab/tags/ppc-for-6.0-20210119' into staging
[mirror_qemu.git] / hw / intc / xics_kvm.c
CommitLineData
11ad93f6
DG
1/*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3 *
4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation
5 *
6 * Copyright (c) 2013 David Gibson, IBM Corporation.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
0d75590d 28#include "qemu/osdep.h"
da34e65c 29#include "qapi/error.h"
4771d756
PB
30#include "qemu-common.h"
31#include "cpu.h"
11ad93f6 32#include "trace.h"
77ac58dd 33#include "sysemu/kvm.h"
11ad93f6 34#include "hw/ppc/spapr.h"
3f777abc 35#include "hw/ppc/spapr_cpu_core.h"
11ad93f6 36#include "hw/ppc/xics.h"
a51d5afc 37#include "hw/ppc/xics_spapr.h"
11ad93f6
DG
38#include "kvm_ppc.h"
39#include "qemu/config-file.h"
40#include "qemu/error-report.h"
41
42#include <sys/ioctl.h>
43
729f8a4f
CLG
44static int kernel_xics_fd = -1;
45
de86eccc
GK
46typedef struct KVMEnabledICP {
47 unsigned long vcpu_id;
48 QLIST_ENTRY(KVMEnabledICP) node;
49} KVMEnabledICP;
50
51static QLIST_HEAD(, KVMEnabledICP)
52 kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
53
56b11587
CLG
54static void kvm_disable_icps(void)
55{
56 KVMEnabledICP *enabled_icp, *next;
57
58 QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
59 QLIST_REMOVE(enabled_icp, node);
60 g_free(enabled_icp);
61 }
62}
63
11ad93f6
DG
64/*
65 * ICP-KVM
66 */
0e5c7fad 67void icp_get_kvm_state(ICPState *icp)
11ad93f6
DG
68{
69 uint64_t state;
11ad93f6
DG
70 int ret;
71
3bf84e99
CLG
72 /* The KVM XICS device is not in use */
73 if (kernel_xics_fd == -1) {
74 return;
75 }
76
11ad93f6 77 /* ICP for this CPU thread is not in use, exiting */
8e4fba20 78 if (!icp->cs) {
11ad93f6
DG
79 return;
80 }
81
bf358b54 82 ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
11ad93f6
DG
83 if (ret != 0) {
84 error_report("Unable to retrieve KVM interrupt controller state"
8e4fba20 85 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
11ad93f6
DG
86 exit(1);
87 }
88
8e4fba20
CLG
89 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
90 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
11ad93f6 91 & KVM_REG_PPC_ICP_MFRR_MASK;
8e4fba20 92 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
11ad93f6
DG
93 & KVM_REG_PPC_ICP_PPRI_MASK;
94}
95
dcb556fc
GK
96static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
97{
98 icp_get_kvm_state(arg.host_ptr);
99}
100
0e5c7fad 101void icp_synchronize_state(ICPState *icp)
dcb556fc
GK
102{
103 if (icp->cs) {
104 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
105 }
106}
107
330a21e3 108int icp_set_kvm_state(ICPState *icp, Error **errp)
11ad93f6
DG
109{
110 uint64_t state;
11ad93f6
DG
111 int ret;
112
3bf84e99
CLG
113 /* The KVM XICS device is not in use */
114 if (kernel_xics_fd == -1) {
115 return 0;
116 }
117
11ad93f6 118 /* ICP for this CPU thread is not in use, exiting */
8e4fba20 119 if (!icp->cs) {
11ad93f6
DG
120 return 0;
121 }
122
8e4fba20
CLG
123 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
124 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
125 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
11ad93f6 126
bf358b54 127 ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
330a21e3
GK
128 if (ret < 0) {
129 error_setg_errno(errp, -ret,
130 "Unable to restore KVM interrupt controller state (0x%"
131 PRIx64 ") for CPU %ld", state,
132 kvm_arch_vcpu_id(icp->cs));
11ad93f6
DG
133 return ret;
134 }
135
136 return 0;
137}
138
8e6e6efe 139void icp_kvm_realize(DeviceState *dev, Error **errp)
f0232434 140{
a028dd42 141 ICPState *icp = ICP(dev);
a028dd42 142 CPUState *cs;
de86eccc 143 KVMEnabledICP *enabled_icp;
a028dd42 144 unsigned long vcpu_id;
f0232434
CLG
145 int ret;
146
3bf84e99 147 /* The KVM XICS device is not in use */
f0232434 148 if (kernel_xics_fd == -1) {
3bf84e99 149 return;
f0232434
CLG
150 }
151
a028dd42
CLG
152 cs = icp->cs;
153 vcpu_id = kvm_arch_vcpu_id(cs);
154
f0232434
CLG
155 /*
156 * If we are reusing a parked vCPU fd corresponding to the CPU
157 * which was hot-removed earlier we don't have to renable
158 * KVM_CAP_IRQ_XICS capability again.
159 */
de86eccc
GK
160 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
161 if (enabled_icp->vcpu_id == vcpu_id) {
162 return;
163 }
f0232434
CLG
164 }
165
de86eccc 166 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
f0232434 167 if (ret < 0) {
894ea3ec
GK
168 Error *local_err = NULL;
169
170 error_setg(&local_err, "Unable to connect CPU%ld to kernel XICS: %s",
171 vcpu_id, strerror(errno));
172 if (errno == ENOSPC) {
173 error_append_hint(&local_err, "Try -smp maxcpus=N with N < %u\n",
174 MACHINE(qdev_get_machine())->smp.max_cpus);
175 }
176 error_propagate(errp, local_err);
b1fd36c3 177 return;
f0232434 178 }
de86eccc
GK
179 enabled_icp = g_malloc(sizeof(*enabled_icp));
180 enabled_icp->vcpu_id = vcpu_id;
181 QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
f0232434
CLG
182}
183
11ad93f6
DG
184/*
185 * ICS-KVM
186 */
d80b2ccf 187void ics_get_kvm_state(ICSState *ics)
11ad93f6 188{
11ad93f6 189 uint64_t state;
11ad93f6
DG
190 int i;
191
3bf84e99
CLG
192 /* The KVM XICS device is not in use */
193 if (kernel_xics_fd == -1) {
194 return;
195 }
196
11ad93f6
DG
197 for (i = 0; i < ics->nr_irqs; i++) {
198 ICSIRQState *irq = &ics->irqs[i];
11ad93f6 199
4c3539d4
CLG
200 if (ics_irq_free(ics, i)) {
201 continue;
202 }
203
bf358b54 204 kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
50beeb68 205 i + ics->offset, &state, false, &error_fatal);
11ad93f6
DG
206
207 irq->server = state & KVM_XICS_DESTINATION_MASK;
208 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
209 & KVM_XICS_PRIORITY_MASK;
210 /*
211 * To be consistent with the software emulation in xics.c, we
212 * split out the masked state + priority that we get from the
213 * kernel into 'current priority' (0xff if masked) and
214 * 'saved priority' (if masked, this is the priority the
215 * interrupt had before it was masked). Masking and unmasking
216 * are done with the ibm,int-off and ibm,int-on RTAS calls.
217 */
218 if (state & KVM_XICS_MASKED) {
219 irq->priority = 0xff;
220 } else {
221 irq->priority = irq->saved_priority;
222 }
223
063cb7cb 224 irq->status = 0;
11ad93f6
DG
225 if (state & KVM_XICS_PENDING) {
226 if (state & KVM_XICS_LEVEL_SENSITIVE) {
227 irq->status |= XICS_STATUS_ASSERTED;
228 } else {
229 /*
230 * A pending edge-triggered interrupt (or MSI)
231 * must have been rejected previously when we
232 * first detected it and tried to deliver it,
233 * so mark it as pending and previously rejected
234 * for consistency with how xics.c works.
235 */
236 irq->status |= XICS_STATUS_MASKED_PENDING
237 | XICS_STATUS_REJECTED;
238 }
239 }
229e16fd
SB
240 if (state & KVM_XICS_PRESENTED) {
241 irq->status |= XICS_STATUS_PRESENTED;
242 }
243 if (state & KVM_XICS_QUEUED) {
244 irq->status |= XICS_STATUS_QUEUED;
245 }
11ad93f6
DG
246 }
247}
248
d80b2ccf 249void ics_synchronize_state(ICSState *ics)
dcb556fc
GK
250{
251 ics_get_kvm_state(ics);
252}
253
330a21e3 254int ics_set_kvm_state_one(ICSState *ics, int srcno, Error **errp)
11ad93f6 255{
11ad93f6 256 uint64_t state;
6cead90c
GK
257 ICSIRQState *irq = &ics->irqs[srcno];
258 int ret;
11ad93f6 259
3bf84e99
CLG
260 /* The KVM XICS device is not in use */
261 if (kernel_xics_fd == -1) {
262 return 0;
263 }
264
6cead90c
GK
265 state = irq->server;
266 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
267 << KVM_XICS_PRIORITY_SHIFT;
268 if (irq->priority != irq->saved_priority) {
269 assert(irq->priority == 0xff);
38298611
GK
270 }
271
272 if (irq->priority == 0xff) {
6cead90c
GK
273 state |= KVM_XICS_MASKED;
274 }
11ad93f6 275
6cead90c
GK
276 if (irq->flags & XICS_FLAGS_IRQ_LSI) {
277 state |= KVM_XICS_LEVEL_SENSITIVE;
278 if (irq->status & XICS_STATUS_ASSERTED) {
279 state |= KVM_XICS_PENDING;
11ad93f6 280 }
6cead90c
GK
281 } else {
282 if (irq->status & XICS_STATUS_MASKED_PENDING) {
283 state |= KVM_XICS_PENDING;
229e16fd 284 }
6cead90c
GK
285 }
286 if (irq->status & XICS_STATUS_PRESENTED) {
287 state |= KVM_XICS_PRESENTED;
288 }
289 if (irq->status & XICS_STATUS_QUEUED) {
290 state |= KVM_XICS_QUEUED;
291 }
292
293 ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
330a21e3
GK
294 srcno + ics->offset, &state, true, errp);
295 if (ret < 0) {
6cead90c
GK
296 return ret;
297 }
298
299 return 0;
300}
301
330a21e3 302int ics_set_kvm_state(ICSState *ics, Error **errp)
6cead90c
GK
303{
304 int i;
305
3bf84e99
CLG
306 /* The KVM XICS device is not in use */
307 if (kernel_xics_fd == -1) {
308 return 0;
309 }
310
6cead90c
GK
311 for (i = 0; i < ics->nr_irqs; i++) {
312 int ret;
11ad93f6 313
4c3539d4
CLG
314 if (ics_irq_free(ics, i)) {
315 continue;
316 }
317
668f62ec 318 ret = ics_set_kvm_state_one(ics, i, errp);
330a21e3 319 if (ret < 0) {
11ad93f6
DG
320 return ret;
321 }
322 }
323
324 return 0;
325}
326
557b4567 327void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
11ad93f6 328{
11ad93f6
DG
329 struct kvm_irq_level args;
330 int rc;
331
3bf84e99
CLG
332 /* The KVM XICS device should be in use */
333 assert(kernel_xics_fd != -1);
334
11ad93f6 335 args.irq = srcno + ics->offset;
4af88944 336 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
11ad93f6
DG
337 if (!val) {
338 return;
339 }
340 args.level = KVM_INTERRUPT_SET;
341 } else {
342 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
343 }
344 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
345 if (rc < 0) {
346 perror("kvm_irq_line");
347 }
348}
349
4ffb7496
GK
350int xics_kvm_connect(SpaprInterruptController *intc, uint32_t nr_servers,
351 Error **errp)
11ad93f6 352{
98a39a79 353 ICSState *ics = ICS_SPAPR(intc);
817bb6a4 354 int rc;
3f777abc
CLG
355 CPUState *cs;
356 Error *local_err = NULL;
357
358 /*
359 * The KVM XICS device already in use. This is the case when
360 * rebooting under the XICS-only interrupt mode.
361 */
362 if (kernel_xics_fd != -1) {
363 return 0;
364 }
11ad93f6
DG
365
366 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
367 error_setg(errp,
368 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
64fb9621 369 return -1;
11ad93f6
DG
370 }
371
3a3b8502 372 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
11ad93f6 373 if (rc < 0) {
ab3d15fa
GK
374 error_setg_errno(&local_err, -rc,
375 "kvmppc_define_rtas_kernel_token: ibm,set-xive");
11ad93f6
DG
376 goto fail;
377 }
378
3a3b8502 379 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
11ad93f6 380 if (rc < 0) {
ab3d15fa
GK
381 error_setg_errno(&local_err, -rc,
382 "kvmppc_define_rtas_kernel_token: ibm,get-xive");
11ad93f6
DG
383 goto fail;
384 }
385
3a3b8502 386 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
11ad93f6 387 if (rc < 0) {
ab3d15fa
GK
388 error_setg_errno(&local_err, -rc,
389 "kvmppc_define_rtas_kernel_token: ibm,int-on");
11ad93f6
DG
390 goto fail;
391 }
392
3a3b8502 393 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
11ad93f6 394 if (rc < 0) {
ab3d15fa
GK
395 error_setg_errno(&local_err, -rc,
396 "kvmppc_define_rtas_kernel_token: ibm,int-off");
11ad93f6
DG
397 goto fail;
398 }
399
bf358b54
CLG
400 /* Create the KVM XICS device */
401 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
11ad93f6 402 if (rc < 0) {
ab3d15fa 403 error_setg_errno(&local_err, -rc, "Error on KVM_CREATE_DEVICE for XICS");
11ad93f6
DG
404 goto fail;
405 }
406
894ea3ec
GK
407 /* Tell KVM about the # of VCPUs we may have (POWER9 and newer only) */
408 if (kvm_device_check_attr(rc, KVM_DEV_XICS_GRP_CTRL,
409 KVM_DEV_XICS_NR_SERVERS)) {
410 if (kvm_device_access(rc, KVM_DEV_XICS_GRP_CTRL,
411 KVM_DEV_XICS_NR_SERVERS, &nr_servers, true,
412 &local_err)) {
413 goto fail;
414 }
415 }
416
bf358b54 417 kernel_xics_fd = rc;
9554233c 418 kvm_kernel_irqchip = true;
9554233c
AK
419 kvm_msi_via_irqfd_allowed = true;
420 kvm_gsi_direct_mapping = true;
421
3f777abc
CLG
422 /* Create the presenters */
423 CPU_FOREACH(cs) {
424 PowerPCCPU *cpu = POWERPC_CPU(cs);
425
426 icp_kvm_realize(DEVICE(spapr_cpu_state(cpu)->icp), &local_err);
427 if (local_err) {
3f777abc
CLG
428 goto fail;
429 }
430 }
431
432 /* Update the KVM sources */
98a39a79 433 ics_set_kvm_state(ics, &local_err);
330a21e3
GK
434 if (local_err) {
435 goto fail;
436 }
3f777abc
CLG
437
438 /* Connect the presenters to the initial VCPUs of the machine */
439 CPU_FOREACH(cs) {
440 PowerPCCPU *cpu = POWERPC_CPU(cs);
330a21e3
GK
441 icp_set_kvm_state(spapr_cpu_state(cpu)->icp, &local_err);
442 if (local_err) {
443 goto fail;
444 }
3f777abc
CLG
445 }
446
bf358b54 447 return 0;
11ad93f6
DG
448
449fail:
ab3d15fa 450 error_propagate(errp, local_err);
98a39a79 451 xics_kvm_disconnect(intc);
2192a930 452 return -1;
11ad93f6 453}
56b11587 454
98a39a79 455void xics_kvm_disconnect(SpaprInterruptController *intc)
56b11587 456{
56b11587
CLG
457 /*
458 * Only on P9 using the XICS-on XIVE KVM device:
459 *
460 * When the KVM device fd is closed, the device is destroyed and
461 * removed from the list of devices of the VM. The VCPU presenters
462 * are also detached from the device.
463 */
4812f261
GK
464 if (kernel_xics_fd != -1) {
465 close(kernel_xics_fd);
466 kernel_xics_fd = -1;
467 }
56b11587 468
56b11587
CLG
469 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
470 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
471 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
472 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
473
474 kvm_kernel_irqchip = false;
475 kvm_msi_via_irqfd_allowed = false;
476 kvm_gsi_direct_mapping = false;
477
478 /* Clear the presenter from the VCPUs */
479 kvm_disable_icps();
480}
7abc0c6d
GK
481
482/*
483 * This is a heuristic to detect older KVMs on POWER9 hosts that don't
484 * support destruction of a KVM XICS device while the VM is running.
485 * Required to start a spapr machine with ic-mode=dual,kernel-irqchip=on.
486 */
0b66209d 487bool xics_kvm_has_broken_disconnect(void)
7abc0c6d
GK
488{
489 int rc;
490
491 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
492 if (rc < 0) {
493 /*
494 * The error is ignored on purpose. The KVM XICS setup code
495 * will catch it again anyway. The goal here is to see if
496 * close() actually destroys the device or not.
497 */
498 return false;
499 }
500
501 close(rc);
502
503 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
504 if (rc >= 0) {
505 close(rc);
506 return false;
507 }
508
509 return errno == EEXIST;
510}