xref: /openbmc/qemu/hw/intc/xics_kvm.c (revision 0b1183e3)
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 "kvm_ppc.h"
38 #include "qemu/config-file.h"
39 #include "qemu/error-report.h"
40 
41 #include <sys/ioctl.h>
42 
43 static int kernel_xics_fd = -1;
44 
45 typedef struct KVMEnabledICP {
46     unsigned long vcpu_id;
47     QLIST_ENTRY(KVMEnabledICP) node;
48 } KVMEnabledICP;
49 
50 static QLIST_HEAD(, KVMEnabledICP)
51     kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
52 
53 /*
54  * ICP-KVM
55  */
56 static void icp_get_kvm_state(ICPState *icp)
57 {
58     uint64_t state;
59     struct kvm_one_reg reg = {
60         .id = KVM_REG_PPC_ICP_STATE,
61         .addr = (uintptr_t)&state,
62     };
63     int ret;
64 
65     /* ICP for this CPU thread is not in use, exiting */
66     if (!icp->cs) {
67         return;
68     }
69 
70     ret = kvm_vcpu_ioctl(icp->cs, KVM_GET_ONE_REG, &reg);
71     if (ret != 0) {
72         error_report("Unable to retrieve KVM interrupt controller state"
73                 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
74         exit(1);
75     }
76 
77     icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
78     icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
79         & KVM_REG_PPC_ICP_MFRR_MASK;
80     icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
81         & KVM_REG_PPC_ICP_PPRI_MASK;
82 }
83 
84 static int icp_set_kvm_state(ICPState *icp, int version_id)
85 {
86     uint64_t state;
87     struct kvm_one_reg reg = {
88         .id = KVM_REG_PPC_ICP_STATE,
89         .addr = (uintptr_t)&state,
90     };
91     int ret;
92 
93     /* ICP for this CPU thread is not in use, exiting */
94     if (!icp->cs) {
95         return 0;
96     }
97 
98     state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
99         | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
100         | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
101 
102     ret = kvm_vcpu_ioctl(icp->cs, KVM_SET_ONE_REG, &reg);
103     if (ret != 0) {
104         error_report("Unable to restore KVM interrupt controller state (0x%"
105                 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(icp->cs),
106                 strerror(errno));
107         return ret;
108     }
109 
110     return 0;
111 }
112 
113 static void icp_kvm_reset(ICPState *icp)
114 {
115     icp_set_kvm_state(icp, 1);
116 }
117 
118 static void icp_kvm_realize(ICPState *icp, Error **errp)
119 {
120     CPUState *cs = icp->cs;
121     KVMEnabledICP *enabled_icp;
122     unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
123     int ret;
124 
125     if (kernel_xics_fd == -1) {
126         abort();
127     }
128 
129     /*
130      * If we are reusing a parked vCPU fd corresponding to the CPU
131      * which was hot-removed earlier we don't have to renable
132      * KVM_CAP_IRQ_XICS capability again.
133      */
134     QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
135         if (enabled_icp->vcpu_id == vcpu_id) {
136             return;
137         }
138     }
139 
140     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
141     if (ret < 0) {
142         error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id,
143                    strerror(errno));
144         return;
145     }
146     enabled_icp = g_malloc(sizeof(*enabled_icp));
147     enabled_icp->vcpu_id = vcpu_id;
148     QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
149 }
150 
151 static void icp_kvm_class_init(ObjectClass *klass, void *data)
152 {
153     ICPStateClass *icpc = ICP_CLASS(klass);
154 
155     icpc->pre_save = icp_get_kvm_state;
156     icpc->post_load = icp_set_kvm_state;
157     icpc->realize = icp_kvm_realize;
158     icpc->reset = icp_kvm_reset;
159 }
160 
161 static const TypeInfo icp_kvm_info = {
162     .name = TYPE_KVM_ICP,
163     .parent = TYPE_ICP,
164     .instance_size = sizeof(ICPState),
165     .class_init = icp_kvm_class_init,
166     .class_size = sizeof(ICPStateClass),
167 };
168 
169 /*
170  * ICS-KVM
171  */
172 static void ics_get_kvm_state(ICSState *ics)
173 {
174     uint64_t state;
175     struct kvm_device_attr attr = {
176         .flags = 0,
177         .group = KVM_DEV_XICS_GRP_SOURCES,
178         .addr = (uint64_t)(uintptr_t)&state,
179     };
180     int i;
181 
182     for (i = 0; i < ics->nr_irqs; i++) {
183         ICSIRQState *irq = &ics->irqs[i];
184         int ret;
185 
186         attr.attr = i + ics->offset;
187 
188         ret = ioctl(kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
189         if (ret != 0) {
190             error_report("Unable to retrieve KVM interrupt controller state"
191                     " for IRQ %d: %s", i + ics->offset, strerror(errno));
192             exit(1);
193         }
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 static int ics_set_kvm_state(ICSState *ics, int version_id)
238 {
239     uint64_t state;
240     struct kvm_device_attr attr = {
241         .flags = 0,
242         .group = KVM_DEV_XICS_GRP_SOURCES,
243         .addr = (uint64_t)(uintptr_t)&state,
244     };
245     int i;
246 
247     for (i = 0; i < ics->nr_irqs; i++) {
248         ICSIRQState *irq = &ics->irqs[i];
249         int ret;
250 
251         attr.attr = i + ics->offset;
252 
253         state = irq->server;
254         state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
255             << KVM_XICS_PRIORITY_SHIFT;
256         if (irq->priority != irq->saved_priority) {
257             assert(irq->priority == 0xff);
258             state |= KVM_XICS_MASKED;
259         }
260 
261         if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
262             state |= KVM_XICS_LEVEL_SENSITIVE;
263             if (irq->status & XICS_STATUS_ASSERTED) {
264                 state |= KVM_XICS_PENDING;
265             }
266         } else {
267             if (irq->status & XICS_STATUS_MASKED_PENDING) {
268                 state |= KVM_XICS_PENDING;
269             }
270         }
271         if (irq->status & XICS_STATUS_PRESENTED) {
272                 state |= KVM_XICS_PRESENTED;
273         }
274         if (irq->status & XICS_STATUS_QUEUED) {
275                 state |= KVM_XICS_QUEUED;
276         }
277 
278         ret = ioctl(kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
279         if (ret != 0) {
280             error_report("Unable to restore KVM interrupt controller state"
281                     " for IRQs %d: %s", i + ics->offset, strerror(errno));
282             return ret;
283         }
284     }
285 
286     return 0;
287 }
288 
289 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
290 {
291     ICSState *ics = opaque;
292     struct kvm_irq_level args;
293     int rc;
294 
295     args.irq = srcno + ics->offset;
296     if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
297         if (!val) {
298             return;
299         }
300         args.level = KVM_INTERRUPT_SET;
301     } else {
302         args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
303     }
304     rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
305     if (rc < 0) {
306         perror("kvm_irq_line");
307     }
308 }
309 
310 static void ics_kvm_reset(void *dev)
311 {
312     ICSState *ics = ICS_SIMPLE(dev);
313     int i;
314     uint8_t flags[ics->nr_irqs];
315 
316     for (i = 0; i < ics->nr_irqs; i++) {
317         flags[i] = ics->irqs[i].flags;
318     }
319 
320     memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
321 
322     for (i = 0; i < ics->nr_irqs; i++) {
323         ics->irqs[i].priority = 0xff;
324         ics->irqs[i].saved_priority = 0xff;
325         ics->irqs[i].flags = flags[i];
326     }
327 
328     ics_set_kvm_state(ics, 1);
329 }
330 
331 static void ics_kvm_realize(ICSState *ics, Error **errp)
332 {
333     if (!ics->nr_irqs) {
334         error_setg(errp, "Number of interrupts needs to be greater 0");
335         return;
336     }
337     ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
338     ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
339 
340     qemu_register_reset(ics_kvm_reset, ics);
341 }
342 
343 static void ics_kvm_class_init(ObjectClass *klass, void *data)
344 {
345     ICSStateClass *icsc = ICS_BASE_CLASS(klass);
346 
347     icsc->realize = ics_kvm_realize;
348     icsc->pre_save = ics_get_kvm_state;
349     icsc->post_load = ics_set_kvm_state;
350 }
351 
352 static const TypeInfo ics_kvm_info = {
353     .name = TYPE_ICS_KVM,
354     .parent = TYPE_ICS_SIMPLE,
355     .instance_size = sizeof(ICSState),
356     .class_init = ics_kvm_class_init,
357 };
358 
359 /*
360  * XICS-KVM
361  */
362 
363 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr,
364                        uint32_t token,
365                        uint32_t nargs, target_ulong args,
366                        uint32_t nret, target_ulong rets)
367 {
368     error_report("pseries: %s must never be called for in-kernel XICS",
369                  __func__);
370 }
371 
372 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp)
373 {
374     int rc;
375     struct kvm_create_device xics_create_device = {
376         .type = KVM_DEV_TYPE_XICS,
377         .flags = 0,
378     };
379 
380     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
381         error_setg(errp,
382                    "KVM and IRQ_XICS capability must be present for in-kernel XICS");
383         goto fail;
384     }
385 
386     spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
387     spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
388     spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
389     spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
390 
391     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
392     if (rc < 0) {
393         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
394         goto fail;
395     }
396 
397     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
398     if (rc < 0) {
399         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
400         goto fail;
401     }
402 
403     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
404     if (rc < 0) {
405         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
406         goto fail;
407     }
408 
409     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
410     if (rc < 0) {
411         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
412         goto fail;
413     }
414 
415     /* Create the kernel ICP */
416     rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
417     if (rc < 0) {
418         error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
419         goto fail;
420     }
421 
422     kernel_xics_fd = xics_create_device.fd;
423 
424     kvm_kernel_irqchip = true;
425     kvm_msi_via_irqfd_allowed = true;
426     kvm_gsi_direct_mapping = true;
427 
428     return rc;
429 
430 fail:
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     return -1;
436 }
437 
438 static void xics_kvm_register_types(void)
439 {
440     type_register_static(&ics_kvm_info);
441     type_register_static(&icp_kvm_info);
442 }
443 
444 type_init(xics_kvm_register_types)
445