xref: /openbmc/qemu/hw/intc/xics_kvm.c (revision 5b262bb6)
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 typedef struct KVMXICSState {
44     XICSState parent_obj;
45 
46     int kernel_xics_fd;
47 } KVMXICSState;
48 
49 /*
50  * ICP-KVM
51  */
52 static void icp_get_kvm_state(ICPState *ss)
53 {
54     uint64_t state;
55     struct kvm_one_reg reg = {
56         .id = KVM_REG_PPC_ICP_STATE,
57         .addr = (uintptr_t)&state,
58     };
59     int ret;
60 
61     /* ICP for this CPU thread is not in use, exiting */
62     if (!ss->cs) {
63         return;
64     }
65 
66     ret = kvm_vcpu_ioctl(ss->cs, KVM_GET_ONE_REG, &reg);
67     if (ret != 0) {
68         error_report("Unable to retrieve KVM interrupt controller state"
69                 " for CPU %ld: %s", kvm_arch_vcpu_id(ss->cs), strerror(errno));
70         exit(1);
71     }
72 
73     ss->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
74     ss->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
75         & KVM_REG_PPC_ICP_MFRR_MASK;
76     ss->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
77         & KVM_REG_PPC_ICP_PPRI_MASK;
78 }
79 
80 static int icp_set_kvm_state(ICPState *ss, int version_id)
81 {
82     uint64_t state;
83     struct kvm_one_reg reg = {
84         .id = KVM_REG_PPC_ICP_STATE,
85         .addr = (uintptr_t)&state,
86     };
87     int ret;
88 
89     /* ICP for this CPU thread is not in use, exiting */
90     if (!ss->cs) {
91         return 0;
92     }
93 
94     state = ((uint64_t)ss->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
95         | ((uint64_t)ss->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
96         | ((uint64_t)ss->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
97 
98     ret = kvm_vcpu_ioctl(ss->cs, KVM_SET_ONE_REG, &reg);
99     if (ret != 0) {
100         error_report("Unable to restore KVM interrupt controller state (0x%"
101                 PRIx64 ") for CPU %ld: %s", state, kvm_arch_vcpu_id(ss->cs),
102                 strerror(errno));
103         return ret;
104     }
105 
106     return 0;
107 }
108 
109 static void icp_kvm_reset(DeviceState *dev)
110 {
111     ICPState *icp = ICP(dev);
112 
113     icp->xirr = 0;
114     icp->pending_priority = 0xff;
115     icp->mfrr = 0xff;
116 
117     /* Make all outputs as deasserted only if the CPU thread is in use */
118     if (icp->output) {
119         qemu_set_irq(icp->output, 0);
120     }
121 
122     icp_set_kvm_state(icp, 1);
123 }
124 
125 static void icp_kvm_class_init(ObjectClass *klass, void *data)
126 {
127     DeviceClass *dc = DEVICE_CLASS(klass);
128     ICPStateClass *icpc = ICP_CLASS(klass);
129 
130     dc->reset = icp_kvm_reset;
131     icpc->pre_save = icp_get_kvm_state;
132     icpc->post_load = icp_set_kvm_state;
133 }
134 
135 static const TypeInfo icp_kvm_info = {
136     .name = TYPE_KVM_ICP,
137     .parent = TYPE_ICP,
138     .instance_size = sizeof(ICPState),
139     .class_init = icp_kvm_class_init,
140     .class_size = sizeof(ICPStateClass),
141 };
142 
143 /*
144  * ICS-KVM
145  */
146 static void ics_get_kvm_state(ICSState *ics)
147 {
148     KVMXICSState *xicskvm = XICS_SPAPR_KVM(ics->xics);
149     uint64_t state;
150     struct kvm_device_attr attr = {
151         .flags = 0,
152         .group = KVM_DEV_XICS_GRP_SOURCES,
153         .addr = (uint64_t)(uintptr_t)&state,
154     };
155     int i;
156 
157     for (i = 0; i < ics->nr_irqs; i++) {
158         ICSIRQState *irq = &ics->irqs[i];
159         int ret;
160 
161         attr.attr = i + ics->offset;
162 
163         ret = ioctl(xicskvm->kernel_xics_fd, KVM_GET_DEVICE_ATTR, &attr);
164         if (ret != 0) {
165             error_report("Unable to retrieve KVM interrupt controller state"
166                     " for IRQ %d: %s", i + ics->offset, strerror(errno));
167             exit(1);
168         }
169 
170         irq->server = state & KVM_XICS_DESTINATION_MASK;
171         irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
172             & KVM_XICS_PRIORITY_MASK;
173         /*
174          * To be consistent with the software emulation in xics.c, we
175          * split out the masked state + priority that we get from the
176          * kernel into 'current priority' (0xff if masked) and
177          * 'saved priority' (if masked, this is the priority the
178          * interrupt had before it was masked).  Masking and unmasking
179          * are done with the ibm,int-off and ibm,int-on RTAS calls.
180          */
181         if (state & KVM_XICS_MASKED) {
182             irq->priority = 0xff;
183         } else {
184             irq->priority = irq->saved_priority;
185         }
186 
187         if (state & KVM_XICS_PENDING) {
188             if (state & KVM_XICS_LEVEL_SENSITIVE) {
189                 irq->status |= XICS_STATUS_ASSERTED;
190             } else {
191                 /*
192                  * A pending edge-triggered interrupt (or MSI)
193                  * must have been rejected previously when we
194                  * first detected it and tried to deliver it,
195                  * so mark it as pending and previously rejected
196                  * for consistency with how xics.c works.
197                  */
198                 irq->status |= XICS_STATUS_MASKED_PENDING
199                     | XICS_STATUS_REJECTED;
200             }
201         }
202     }
203 }
204 
205 static int ics_set_kvm_state(ICSState *ics, int version_id)
206 {
207     KVMXICSState *xicskvm = XICS_SPAPR_KVM(ics->xics);
208     uint64_t state;
209     struct kvm_device_attr attr = {
210         .flags = 0,
211         .group = KVM_DEV_XICS_GRP_SOURCES,
212         .addr = (uint64_t)(uintptr_t)&state,
213     };
214     int i;
215 
216     for (i = 0; i < ics->nr_irqs; i++) {
217         ICSIRQState *irq = &ics->irqs[i];
218         int ret;
219 
220         attr.attr = i + ics->offset;
221 
222         state = irq->server;
223         state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
224             << KVM_XICS_PRIORITY_SHIFT;
225         if (irq->priority != irq->saved_priority) {
226             assert(irq->priority == 0xff);
227             state |= KVM_XICS_MASKED;
228         }
229 
230         if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
231             state |= KVM_XICS_LEVEL_SENSITIVE;
232             if (irq->status & XICS_STATUS_ASSERTED) {
233                 state |= KVM_XICS_PENDING;
234             }
235         } else {
236             if (irq->status & XICS_STATUS_MASKED_PENDING) {
237                 state |= KVM_XICS_PENDING;
238             }
239         }
240 
241         ret = ioctl(xicskvm->kernel_xics_fd, KVM_SET_DEVICE_ATTR, &attr);
242         if (ret != 0) {
243             error_report("Unable to restore KVM interrupt controller state"
244                     " for IRQs %d: %s", i + ics->offset, strerror(errno));
245             return ret;
246         }
247     }
248 
249     return 0;
250 }
251 
252 static void ics_kvm_set_irq(void *opaque, int srcno, int val)
253 {
254     ICSState *ics = opaque;
255     struct kvm_irq_level args;
256     int rc;
257 
258     args.irq = srcno + ics->offset;
259     if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
260         if (!val) {
261             return;
262         }
263         args.level = KVM_INTERRUPT_SET;
264     } else {
265         args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
266     }
267     rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
268     if (rc < 0) {
269         perror("kvm_irq_line");
270     }
271 }
272 
273 static void ics_kvm_reset(DeviceState *dev)
274 {
275     ICSState *ics = ICS(dev);
276     int i;
277     uint8_t flags[ics->nr_irqs];
278 
279     for (i = 0; i < ics->nr_irqs; i++) {
280         flags[i] = ics->irqs[i].flags;
281     }
282 
283     memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
284 
285     for (i = 0; i < ics->nr_irqs; i++) {
286         ics->irqs[i].priority = 0xff;
287         ics->irqs[i].saved_priority = 0xff;
288         ics->irqs[i].flags = flags[i];
289     }
290 
291     ics_set_kvm_state(ics, 1);
292 }
293 
294 static void ics_kvm_realize(DeviceState *dev, Error **errp)
295 {
296     ICSState *ics = ICS(dev);
297 
298     if (!ics->nr_irqs) {
299         error_setg(errp, "Number of interrupts needs to be greater 0");
300         return;
301     }
302     ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
303     ics->qirqs = qemu_allocate_irqs(ics_kvm_set_irq, ics, ics->nr_irqs);
304 }
305 
306 static void ics_kvm_class_init(ObjectClass *klass, void *data)
307 {
308     DeviceClass *dc = DEVICE_CLASS(klass);
309     ICSStateClass *icsc = ICS_CLASS(klass);
310 
311     dc->realize = ics_kvm_realize;
312     dc->reset = ics_kvm_reset;
313     icsc->pre_save = ics_get_kvm_state;
314     icsc->post_load = ics_set_kvm_state;
315 }
316 
317 static const TypeInfo ics_kvm_info = {
318     .name = TYPE_KVM_ICS,
319     .parent = TYPE_ICS,
320     .instance_size = sizeof(ICSState),
321     .class_init = ics_kvm_class_init,
322 };
323 
324 /*
325  * XICS-KVM
326  */
327 static void xics_kvm_cpu_setup(XICSState *xics, PowerPCCPU *cpu)
328 {
329     CPUState *cs;
330     ICPState *ss;
331     KVMXICSState *xicskvm = XICS_SPAPR_KVM(xics);
332     int ret;
333 
334     cs = CPU(cpu);
335     ss = &xics->ss[cs->cpu_index];
336 
337     assert(cs->cpu_index < xics->nr_servers);
338     if (xicskvm->kernel_xics_fd == -1) {
339         abort();
340     }
341 
342     /*
343      * If we are reusing a parked vCPU fd corresponding to the CPU
344      * which was hot-removed earlier we don't have to renable
345      * KVM_CAP_IRQ_XICS capability again.
346      */
347     if (ss->cap_irq_xics_enabled) {
348         return;
349     }
350 
351     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, xicskvm->kernel_xics_fd,
352                               kvm_arch_vcpu_id(cs));
353     if (ret < 0) {
354         error_report("Unable to connect CPU%ld to kernel XICS: %s",
355                      kvm_arch_vcpu_id(cs), strerror(errno));
356         exit(1);
357     }
358     ss->cap_irq_xics_enabled = true;
359 }
360 
361 static void xics_kvm_set_nr_irqs(XICSState *xics, uint32_t nr_irqs,
362                                  Error **errp)
363 {
364     xics->nr_irqs = xics->ics->nr_irqs = nr_irqs;
365 }
366 
367 static void xics_kvm_set_nr_servers(XICSState *xics, uint32_t nr_servers,
368                                     Error **errp)
369 {
370     int i;
371 
372     xics->nr_servers = nr_servers;
373 
374     xics->ss = g_malloc0(xics->nr_servers * sizeof(ICPState));
375     for (i = 0; i < xics->nr_servers; i++) {
376         char buffer[32];
377         object_initialize(&xics->ss[i], sizeof(xics->ss[i]), TYPE_KVM_ICP);
378         snprintf(buffer, sizeof(buffer), "icp[%d]", i);
379         object_property_add_child(OBJECT(xics), buffer, OBJECT(&xics->ss[i]),
380                                   errp);
381     }
382 }
383 
384 static void rtas_dummy(PowerPCCPU *cpu, sPAPRMachineState *spapr,
385                        uint32_t token,
386                        uint32_t nargs, target_ulong args,
387                        uint32_t nret, target_ulong rets)
388 {
389     error_report("pseries: %s must never be called for in-kernel XICS",
390                  __func__);
391 }
392 
393 static void xics_kvm_realize(DeviceState *dev, Error **errp)
394 {
395     KVMXICSState *xicskvm = XICS_SPAPR_KVM(dev);
396     XICSState *xics = XICS_COMMON(dev);
397     int i, rc;
398     Error *error = NULL;
399     struct kvm_create_device xics_create_device = {
400         .type = KVM_DEV_TYPE_XICS,
401         .flags = 0,
402     };
403 
404     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
405         error_setg(errp,
406                    "KVM and IRQ_XICS capability must be present for in-kernel XICS");
407         goto fail;
408     }
409 
410     spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_dummy);
411     spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_dummy);
412     spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_dummy);
413     spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_dummy);
414 
415     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
416     if (rc < 0) {
417         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,set-xive");
418         goto fail;
419     }
420 
421     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
422     if (rc < 0) {
423         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,get-xive");
424         goto fail;
425     }
426 
427     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
428     if (rc < 0) {
429         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-on");
430         goto fail;
431     }
432 
433     rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
434     if (rc < 0) {
435         error_setg(errp, "kvmppc_define_rtas_kernel_token: ibm,int-off");
436         goto fail;
437     }
438 
439     /* Create the kernel ICP */
440     rc = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &xics_create_device);
441     if (rc < 0) {
442         error_setg_errno(errp, -rc, "Error on KVM_CREATE_DEVICE for XICS");
443         goto fail;
444     }
445 
446     xicskvm->kernel_xics_fd = xics_create_device.fd;
447 
448     object_property_set_bool(OBJECT(xics->ics), true, "realized", &error);
449     if (error) {
450         error_propagate(errp, error);
451         goto fail;
452     }
453 
454     assert(xics->nr_servers);
455     for (i = 0; i < xics->nr_servers; i++) {
456         object_property_set_bool(OBJECT(&xics->ss[i]), true, "realized",
457                                  &error);
458         if (error) {
459             error_propagate(errp, error);
460             goto fail;
461         }
462     }
463 
464     kvm_kernel_irqchip = true;
465     kvm_msi_via_irqfd_allowed = true;
466     kvm_gsi_direct_mapping = true;
467 
468     return;
469 
470 fail:
471     kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
472     kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
473     kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
474     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
475 }
476 
477 static void xics_kvm_initfn(Object *obj)
478 {
479     XICSState *xics = XICS_COMMON(obj);
480 
481     xics->ics = ICS(object_new(TYPE_KVM_ICS));
482     object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
483     xics->ics->xics = xics;
484 }
485 
486 static void xics_kvm_class_init(ObjectClass *oc, void *data)
487 {
488     DeviceClass *dc = DEVICE_CLASS(oc);
489     XICSStateClass *xsc = XICS_COMMON_CLASS(oc);
490 
491     dc->realize = xics_kvm_realize;
492     xsc->cpu_setup = xics_kvm_cpu_setup;
493     xsc->set_nr_irqs = xics_kvm_set_nr_irqs;
494     xsc->set_nr_servers = xics_kvm_set_nr_servers;
495 }
496 
497 static const TypeInfo xics_spapr_kvm_info = {
498     .name          = TYPE_XICS_SPAPR_KVM,
499     .parent        = TYPE_XICS_COMMON,
500     .instance_size = sizeof(KVMXICSState),
501     .class_init    = xics_kvm_class_init,
502     .instance_init = xics_kvm_initfn,
503 };
504 
505 static void xics_kvm_register_types(void)
506 {
507     type_register_static(&xics_spapr_kvm_info);
508     type_register_static(&ics_kvm_info);
509     type_register_static(&icp_kvm_info);
510 }
511 
512 type_init(xics_kvm_register_types)
513