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