xref: /openbmc/qemu/hw/intc/xics_kvm.c (revision 79e42085)
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 "hw/hw.h"
33 #include "trace.h"
34 #include "sysemu/kvm.h"
35 #include "hw/ppc/spapr.h"
36 #include "hw/ppc/spapr_cpu_core.h"
37 #include "hw/ppc/xics.h"
38 #include "hw/ppc/xics_spapr.h"
39 #include "kvm_ppc.h"
40 #include "qemu/config-file.h"
41 #include "qemu/error-report.h"
42 
43 #include <sys/ioctl.h>
44 
45 static int kernel_xics_fd = -1;
46 
47 typedef struct KVMEnabledICP {
48     unsigned long vcpu_id;
49     QLIST_ENTRY(KVMEnabledICP) node;
50 } KVMEnabledICP;
51 
52 static QLIST_HEAD(, KVMEnabledICP)
53     kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
54 
55 static void kvm_disable_icps(void)
56 {
57     KVMEnabledICP *enabled_icp, *next;
58 
59     QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
60         QLIST_REMOVE(enabled_icp, node);
61         g_free(enabled_icp);
62     }
63 }
64 
65 /*
66  * ICP-KVM
67  */
68 void icp_get_kvm_state(ICPState *icp)
69 {
70     uint64_t state;
71     int ret;
72 
73     /* The KVM XICS device is not in use */
74     if (kernel_xics_fd == -1) {
75         return;
76     }
77 
78     /* ICP for this CPU thread is not in use, exiting */
79     if (!icp->cs) {
80         return;
81     }
82 
83     ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
84     if (ret != 0) {
85         error_report("Unable to retrieve KVM interrupt controller state"
86                 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
87         exit(1);
88     }
89 
90     icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
91     icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
92         & KVM_REG_PPC_ICP_MFRR_MASK;
93     icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
94         & KVM_REG_PPC_ICP_PPRI_MASK;
95 }
96 
97 static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
98 {
99     icp_get_kvm_state(arg.host_ptr);
100 }
101 
102 void icp_synchronize_state(ICPState *icp)
103 {
104     if (icp->cs) {
105         run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
106     }
107 }
108 
109 int icp_set_kvm_state(ICPState *icp)
110 {
111     uint64_t state;
112     int ret;
113 
114     /* The KVM XICS device is not in use */
115     if (kernel_xics_fd == -1) {
116         return 0;
117     }
118 
119     /* ICP for this CPU thread is not in use, exiting */
120     if (!icp->cs) {
121         return 0;
122     }
123 
124     state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
125         | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
126         | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
127 
128     ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
129     if (ret != 0) {
130         error_report("Unable to restore KVM interrupt controller state (0x%"
131                 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
132                 strerror(errno));
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)
244 {
245     uint64_t state;
246     Error *local_err = NULL;
247     ICSIRQState *irq = &ics->irqs[srcno];
248     int ret;
249 
250     /* The KVM XICS device is not in use */
251     if (kernel_xics_fd == -1) {
252         return 0;
253     }
254 
255     state = irq->server;
256     state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
257         << KVM_XICS_PRIORITY_SHIFT;
258     if (irq->priority != irq->saved_priority) {
259         assert(irq->priority == 0xff);
260         state |= KVM_XICS_MASKED;
261     }
262 
263     if (irq->flags & XICS_FLAGS_IRQ_LSI) {
264         state |= KVM_XICS_LEVEL_SENSITIVE;
265         if (irq->status & XICS_STATUS_ASSERTED) {
266             state |= KVM_XICS_PENDING;
267         }
268     } else {
269         if (irq->status & XICS_STATUS_MASKED_PENDING) {
270             state |= KVM_XICS_PENDING;
271         }
272     }
273     if (irq->status & XICS_STATUS_PRESENTED) {
274         state |= KVM_XICS_PRESENTED;
275     }
276     if (irq->status & XICS_STATUS_QUEUED) {
277         state |= KVM_XICS_QUEUED;
278     }
279 
280     ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
281                             srcno + ics->offset, &state, true, &local_err);
282     if (local_err) {
283         error_report_err(local_err);
284         return ret;
285     }
286 
287     return 0;
288 }
289 
290 int ics_set_kvm_state(ICSState *ics)
291 {
292     int i;
293 
294     /* The KVM XICS device is not in use */
295     if (kernel_xics_fd == -1) {
296         return 0;
297     }
298 
299     for (i = 0; i < ics->nr_irqs; i++) {
300         int ret;
301 
302         ret = ics_set_kvm_state_one(ics, i);
303         if (ret) {
304             return ret;
305         }
306     }
307 
308     return 0;
309 }
310 
311 void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
312 {
313     struct kvm_irq_level args;
314     int rc;
315 
316     /* The KVM XICS device should be in use */
317     assert(kernel_xics_fd != -1);
318 
319     args.irq = srcno + ics->offset;
320     if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
321         if (!val) {
322             return;
323         }
324         args.level = KVM_INTERRUPT_SET;
325     } else {
326         args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
327     }
328     rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
329     if (rc < 0) {
330         perror("kvm_irq_line");
331     }
332 }
333 
334 static void rtas_dummy(PowerPCCPU *cpu, SpaprMachineState *spapr,
335                        uint32_t token,
336                        uint32_t nargs, target_ulong args,
337                        uint32_t nret, target_ulong rets)
338 {
339     error_report("pseries: %s must never be called for in-kernel XICS",
340                  __func__);
341 }
342 
343 int xics_kvm_init(SpaprMachineState *spapr, Error **errp)
344 {
345     int rc;
346     CPUState *cs;
347     Error *local_err = NULL;
348 
349     /*
350      * The KVM XICS device already in use. This is the case when
351      * rebooting under the XICS-only interrupt mode.
352      */
353     if (kernel_xics_fd != -1) {
354         return 0;
355     }
356 
357     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
358         error_setg(errp,
359                    "KVM and IRQ_XICS capability must be present for in-kernel XICS");
360         goto fail;
361     }
362 
363     spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
364     spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
365     spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
366     spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
367 
368     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
369     if (rc < 0) {
370         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
371         goto fail;
372     }
373 
374     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
375     if (rc < 0) {
376         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
377         goto fail;
378     }
379 
380     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
381     if (rc < 0) {
382         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
383         goto fail;
384     }
385 
386     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
387     if (rc < 0) {
388         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
389         goto fail;
390     }
391 
392     /* Create the KVM XICS device */
393     rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
394     if (rc < 0) {
395         error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
396         goto fail;
397     }
398 
399     kernel_xics_fd = rc;
400     kvm_kernel_irqchip = true;
401     kvm_msi_via_irqfd_allowed = true;
402     kvm_gsi_direct_mapping = true;
403 
404     /* Create the presenters */
405     CPU_FOREACH(cs) {
406         PowerPCCPU *cpu = POWERPC_CPU(cs);
407 
408         icp_kvm_realize(DEVICE(spapr_cpu_state(cpu)->icp), &local_err);
409         if (local_err) {
410             error_propagate(errp, local_err);
411             goto fail;
412         }
413     }
414 
415     /* Update the KVM sources */
416     ics_set_kvm_state(spapr->ics);
417 
418     /* Connect the presenters to the initial VCPUs of the machine */
419     CPU_FOREACH(cs) {
420         PowerPCCPU *cpu = POWERPC_CPU(cs);
421         icp_set_kvm_state(spapr_cpu_state(cpu)->icp);
422     }
423 
424     return 0;
425 
426 fail:
427     kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
428     kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
429     kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
430     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
431     return -1;
432 }
433 
434 void xics_kvm_disconnect(SpaprMachineState *spapr, Error **errp)
435 {
436     /* The KVM XICS device is not in use */
437     if (kernel_xics_fd == -1) {
438         return;
439     }
440 
441     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
442         error_setg(errp,
443                    "KVM and IRQ_XICS capability must be present for KVM XICS device");
444         return;
445     }
446 
447     /*
448      * Only on P9 using the XICS-on XIVE KVM device:
449      *
450      * When the KVM device fd is closed, the device is destroyed and
451      * removed from the list of devices of the VM. The VCPU presenters
452      * are also detached from the device.
453      */
454     close(kernel_xics_fd);
455     kernel_xics_fd = -1;
456 
457     spapr_rtas_unregister(RTAS_IBM_SET_XIVE);
458     spapr_rtas_unregister(RTAS_IBM_GET_XIVE);
459     spapr_rtas_unregister(RTAS_IBM_INT_OFF);
460     spapr_rtas_unregister(RTAS_IBM_INT_ON);
461 
462     kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
463     kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
464     kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
465     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
466 
467     kvm_kernel_irqchip = false;
468     kvm_msi_via_irqfd_allowed = false;
469     kvm_gsi_direct_mapping = false;
470 
471     /* Clear the presenter from the VCPUs */
472     kvm_disable_icps();
473 }
474