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