xref: /openbmc/qemu/hw/intc/xics_kvm.c (revision 3bf84e99c823987704d1324cf0e34e7597e737a5)
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/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)
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_report("Unable to restore KVM interrupt controller state (0x%"
130                 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
131                 strerror(errno));
132         return ret;
133     }
134 
135     return 0;
136 }
137 
138 void icp_kvm_realize(DeviceState *dev, Error **errp)
139 {
140     ICPState *icp = ICP(dev);
141     CPUState *cs;
142     KVMEnabledICP *enabled_icp;
143     unsigned long vcpu_id;
144     int ret;
145 
146     /* The KVM XICS device is not in use */
147     if (kernel_xics_fd == -1) {
148         return;
149     }
150 
151     cs = icp->cs;
152     vcpu_id = kvm_arch_vcpu_id(cs);
153 
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      */
159     QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
160         if (enabled_icp->vcpu_id == vcpu_id) {
161             return;
162         }
163     }
164 
165     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
166     if (ret < 0) {
167         error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id,
168                    strerror(errno));
169         return;
170     }
171     enabled_icp = g_malloc(sizeof(*enabled_icp));
172     enabled_icp->vcpu_id = vcpu_id;
173     QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
174 }
175 
176 /*
177  * ICS-KVM
178  */
179 void ics_get_kvm_state(ICSState *ics)
180 {
181     uint64_t state;
182     int i;
183 
184     /* The KVM XICS device is not in use */
185     if (kernel_xics_fd == -1) {
186         return;
187     }
188 
189     for (i = 0; i < ics->nr_irqs; i++) {
190         ICSIRQState *irq = &ics->irqs[i];
191 
192         kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
193                           i + ics->offset, &state, false, &error_fatal);
194 
195         irq->server = state & KVM_XICS_DESTINATION_MASK;
196         irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
197             & KVM_XICS_PRIORITY_MASK;
198         /*
199          * To be consistent with the software emulation in xics.c, we
200          * split out the masked state + priority that we get from the
201          * kernel into 'current priority' (0xff if masked) and
202          * 'saved priority' (if masked, this is the priority the
203          * interrupt had before it was masked).  Masking and unmasking
204          * are done with the ibm,int-off and ibm,int-on RTAS calls.
205          */
206         if (state & KVM_XICS_MASKED) {
207             irq->priority = 0xff;
208         } else {
209             irq->priority = irq->saved_priority;
210         }
211 
212         irq->status = 0;
213         if (state & KVM_XICS_PENDING) {
214             if (state & KVM_XICS_LEVEL_SENSITIVE) {
215                 irq->status |= XICS_STATUS_ASSERTED;
216             } else {
217                 /*
218                  * A pending edge-triggered interrupt (or MSI)
219                  * must have been rejected previously when we
220                  * first detected it and tried to deliver it,
221                  * so mark it as pending and previously rejected
222                  * for consistency with how xics.c works.
223                  */
224                 irq->status |= XICS_STATUS_MASKED_PENDING
225                     | XICS_STATUS_REJECTED;
226             }
227         }
228         if (state & KVM_XICS_PRESENTED) {
229                 irq->status |= XICS_STATUS_PRESENTED;
230         }
231         if (state & KVM_XICS_QUEUED) {
232                 irq->status |= XICS_STATUS_QUEUED;
233         }
234     }
235 }
236 
237 void ics_synchronize_state(ICSState *ics)
238 {
239     ics_get_kvm_state(ics);
240 }
241 
242 int ics_set_kvm_state_one(ICSState *ics, int srcno)
243 {
244     uint64_t state;
245     Error *local_err = NULL;
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         state |= KVM_XICS_MASKED;
260     }
261 
262     if (irq->flags & XICS_FLAGS_IRQ_LSI) {
263         state |= KVM_XICS_LEVEL_SENSITIVE;
264         if (irq->status & XICS_STATUS_ASSERTED) {
265             state |= KVM_XICS_PENDING;
266         }
267     } else {
268         if (irq->status & XICS_STATUS_MASKED_PENDING) {
269             state |= KVM_XICS_PENDING;
270         }
271     }
272     if (irq->status & XICS_STATUS_PRESENTED) {
273         state |= KVM_XICS_PRESENTED;
274     }
275     if (irq->status & XICS_STATUS_QUEUED) {
276         state |= KVM_XICS_QUEUED;
277     }
278 
279     ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
280                             srcno + ics->offset, &state, true, &local_err);
281     if (local_err) {
282         error_report_err(local_err);
283         return ret;
284     }
285 
286     return 0;
287 }
288 
289 int ics_set_kvm_state(ICSState *ics)
290 {
291     int i;
292 
293     /* The KVM XICS device is not in use */
294     if (kernel_xics_fd == -1) {
295         return 0;
296     }
297 
298     for (i = 0; i < ics->nr_irqs; i++) {
299         int ret;
300 
301         ret = ics_set_kvm_state_one(ics, i);
302         if (ret) {
303             return ret;
304         }
305     }
306 
307     return 0;
308 }
309 
310 void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
311 {
312     struct kvm_irq_level args;
313     int rc;
314 
315     /* The KVM XICS device should be in use */
316     assert(kernel_xics_fd != -1);
317 
318     args.irq = srcno + ics->offset;
319     if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
320         if (!val) {
321             return;
322         }
323         args.level = KVM_INTERRUPT_SET;
324     } else {
325         args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
326     }
327     rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
328     if (rc < 0) {
329         perror("kvm_irq_line");
330     }
331 }
332 
333 static void rtas_dummy(PowerPCCPU *cpu, SpaprMachineState *spapr,
334                        uint32_t token,
335                        uint32_t nargs, target_ulong args,
336                        uint32_t nret, target_ulong rets)
337 {
338     error_report("pseries: %s must never be called for in-kernel XICS",
339                  __func__);
340 }
341 
342 int xics_kvm_init(SpaprMachineState *spapr, Error **errp)
343 {
344     int rc;
345 
346     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
347         error_setg(errp,
348                    "KVM and IRQ_XICS capability must be present for in-kernel XICS");
349         goto fail;
350     }
351 
352     spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
353     spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
354     spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
355     spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
356 
357     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
358     if (rc < 0) {
359         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
360         goto fail;
361     }
362 
363     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
364     if (rc < 0) {
365         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
366         goto fail;
367     }
368 
369     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
370     if (rc < 0) {
371         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
372         goto fail;
373     }
374 
375     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
376     if (rc < 0) {
377         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
378         goto fail;
379     }
380 
381     /* Create the KVM XICS device */
382     rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
383     if (rc < 0) {
384         error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
385         goto fail;
386     }
387 
388     kernel_xics_fd = rc;
389     kvm_kernel_irqchip = true;
390     kvm_msi_via_irqfd_allowed = true;
391     kvm_gsi_direct_mapping = true;
392 
393     return 0;
394 
395 fail:
396     kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
397     kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
398     kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
399     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
400     return -1;
401 }
402 
403 void xics_kvm_disconnect(SpaprMachineState *spapr, Error **errp)
404 {
405     /* The KVM XICS device is not in use */
406     if (kernel_xics_fd == -1) {
407         return;
408     }
409 
410     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
411         error_setg(errp,
412                    "KVM and IRQ_XICS capability must be present for KVM XICS device");
413         return;
414     }
415 
416     /*
417      * Only on P9 using the XICS-on XIVE KVM device:
418      *
419      * When the KVM device fd is closed, the device is destroyed and
420      * removed from the list of devices of the VM. The VCPU presenters
421      * are also detached from the device.
422      */
423     close(kernel_xics_fd);
424     kernel_xics_fd = -1;
425 
426     spapr_rtas_unregister(RTAS_IBM_SET_XIVE);
427     spapr_rtas_unregister(RTAS_IBM_GET_XIVE);
428     spapr_rtas_unregister(RTAS_IBM_INT_OFF);
429     spapr_rtas_unregister(RTAS_IBM_INT_ON);
430 
431     kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
432     kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
433     kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
434     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
435 
436     kvm_kernel_irqchip = false;
437     kvm_msi_via_irqfd_allowed = false;
438     kvm_gsi_direct_mapping = false;
439 
440     /* Clear the presenter from the VCPUs */
441     kvm_disable_icps();
442 }
443