xref: /openbmc/qemu/hw/intc/openpic_kvm.c (revision d85937e683f6ff4d68293cb24c780fb1f6820d2c)
1*d85937e6SScott Wood /*
2*d85937e6SScott Wood  * KVM in-kernel OpenPIC
3*d85937e6SScott Wood  *
4*d85937e6SScott Wood  * Copyright 2013 Freescale Semiconductor, Inc.
5*d85937e6SScott Wood  *
6*d85937e6SScott Wood  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*d85937e6SScott Wood  * of this software and associated documentation files (the "Software"), to deal
8*d85937e6SScott Wood  * in the Software without restriction, including without limitation the rights
9*d85937e6SScott Wood  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*d85937e6SScott Wood  * copies of the Software, and to permit persons to whom the Software is
11*d85937e6SScott Wood  * furnished to do so, subject to the following conditions:
12*d85937e6SScott Wood  *
13*d85937e6SScott Wood  * The above copyright notice and this permission notice shall be included in
14*d85937e6SScott Wood  * all copies or substantial portions of the Software.
15*d85937e6SScott Wood  *
16*d85937e6SScott Wood  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*d85937e6SScott Wood  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*d85937e6SScott Wood  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*d85937e6SScott Wood  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*d85937e6SScott Wood  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*d85937e6SScott Wood  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22*d85937e6SScott Wood  * THE SOFTWARE.
23*d85937e6SScott Wood  */
24*d85937e6SScott Wood 
25*d85937e6SScott Wood #include <sys/ioctl.h>
26*d85937e6SScott Wood #include "exec/address-spaces.h"
27*d85937e6SScott Wood #include "hw/hw.h"
28*d85937e6SScott Wood #include "hw/ppc/openpic.h"
29*d85937e6SScott Wood #include "hw/pci/msi.h"
30*d85937e6SScott Wood #include "hw/sysbus.h"
31*d85937e6SScott Wood #include "sysemu/kvm.h"
32*d85937e6SScott Wood #include "qemu/log.h"
33*d85937e6SScott Wood 
34*d85937e6SScott Wood typedef struct KVMOpenPICState {
35*d85937e6SScott Wood     SysBusDevice busdev;
36*d85937e6SScott Wood     MemoryRegion mem;
37*d85937e6SScott Wood     MemoryListener mem_listener;
38*d85937e6SScott Wood     uint32_t fd;
39*d85937e6SScott Wood     uint32_t model;
40*d85937e6SScott Wood } KVMOpenPICState;
41*d85937e6SScott Wood 
42*d85937e6SScott Wood static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
43*d85937e6SScott Wood {
44*d85937e6SScott Wood     kvm_set_irq(kvm_state, n_IRQ, level);
45*d85937e6SScott Wood }
46*d85937e6SScott Wood 
47*d85937e6SScott Wood static void kvm_openpic_reset(DeviceState *d)
48*d85937e6SScott Wood {
49*d85937e6SScott Wood     qemu_log_mask(LOG_UNIMP, "%s: unimplemented\n", __func__);
50*d85937e6SScott Wood }
51*d85937e6SScott Wood 
52*d85937e6SScott Wood static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
53*d85937e6SScott Wood                               unsigned size)
54*d85937e6SScott Wood {
55*d85937e6SScott Wood     KVMOpenPICState *opp = opaque;
56*d85937e6SScott Wood     struct kvm_device_attr attr;
57*d85937e6SScott Wood     uint32_t val32 = val;
58*d85937e6SScott Wood     int ret;
59*d85937e6SScott Wood 
60*d85937e6SScott Wood     attr.group = KVM_DEV_MPIC_GRP_REGISTER;
61*d85937e6SScott Wood     attr.attr = addr;
62*d85937e6SScott Wood     attr.addr = (uint64_t)(unsigned long)&val32;
63*d85937e6SScott Wood 
64*d85937e6SScott Wood     ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
65*d85937e6SScott Wood     if (ret < 0) {
66*d85937e6SScott Wood         qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
67*d85937e6SScott Wood                       strerror(errno), attr.attr);
68*d85937e6SScott Wood     }
69*d85937e6SScott Wood }
70*d85937e6SScott Wood 
71*d85937e6SScott Wood static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
72*d85937e6SScott Wood {
73*d85937e6SScott Wood     KVMOpenPICState *opp = opaque;
74*d85937e6SScott Wood     struct kvm_device_attr attr;
75*d85937e6SScott Wood     uint32_t val = 0xdeadbeef;
76*d85937e6SScott Wood     int ret;
77*d85937e6SScott Wood 
78*d85937e6SScott Wood     attr.group = KVM_DEV_MPIC_GRP_REGISTER;
79*d85937e6SScott Wood     attr.attr = addr;
80*d85937e6SScott Wood     attr.addr = (uint64_t)(unsigned long)&val;
81*d85937e6SScott Wood 
82*d85937e6SScott Wood     ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr);
83*d85937e6SScott Wood     if (ret < 0) {
84*d85937e6SScott Wood         qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
85*d85937e6SScott Wood                       strerror(errno), attr.attr);
86*d85937e6SScott Wood         return 0;
87*d85937e6SScott Wood     }
88*d85937e6SScott Wood 
89*d85937e6SScott Wood     return val;
90*d85937e6SScott Wood }
91*d85937e6SScott Wood 
92*d85937e6SScott Wood static const MemoryRegionOps kvm_openpic_mem_ops = {
93*d85937e6SScott Wood     .write = kvm_openpic_write,
94*d85937e6SScott Wood     .read  = kvm_openpic_read,
95*d85937e6SScott Wood     .endianness = DEVICE_BIG_ENDIAN,
96*d85937e6SScott Wood     .impl = {
97*d85937e6SScott Wood         .min_access_size = 4,
98*d85937e6SScott Wood         .max_access_size = 4,
99*d85937e6SScott Wood     },
100*d85937e6SScott Wood };
101*d85937e6SScott Wood 
102*d85937e6SScott Wood static void kvm_openpic_region_add(MemoryListener *listener,
103*d85937e6SScott Wood                                    MemoryRegionSection *section)
104*d85937e6SScott Wood {
105*d85937e6SScott Wood     KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
106*d85937e6SScott Wood                                         mem_listener);
107*d85937e6SScott Wood     struct kvm_device_attr attr;
108*d85937e6SScott Wood     uint64_t reg_base;
109*d85937e6SScott Wood     int ret;
110*d85937e6SScott Wood 
111*d85937e6SScott Wood     if (section->address_space != &address_space_memory) {
112*d85937e6SScott Wood         abort();
113*d85937e6SScott Wood     }
114*d85937e6SScott Wood 
115*d85937e6SScott Wood     reg_base = section->offset_within_address_space;
116*d85937e6SScott Wood 
117*d85937e6SScott Wood     attr.group = KVM_DEV_MPIC_GRP_MISC;
118*d85937e6SScott Wood     attr.attr = KVM_DEV_MPIC_BASE_ADDR;
119*d85937e6SScott Wood     attr.addr = (uint64_t)(unsigned long)&reg_base;
120*d85937e6SScott Wood 
121*d85937e6SScott Wood     ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
122*d85937e6SScott Wood     if (ret < 0) {
123*d85937e6SScott Wood         fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
124*d85937e6SScott Wood                 strerror(errno), reg_base);
125*d85937e6SScott Wood     }
126*d85937e6SScott Wood }
127*d85937e6SScott Wood 
128*d85937e6SScott Wood static void kvm_openpic_region_del(MemoryListener *listener,
129*d85937e6SScott Wood                                    MemoryRegionSection *section)
130*d85937e6SScott Wood {
131*d85937e6SScott Wood     KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
132*d85937e6SScott Wood                                         mem_listener);
133*d85937e6SScott Wood     struct kvm_device_attr attr;
134*d85937e6SScott Wood     uint64_t reg_base = 0;
135*d85937e6SScott Wood     int ret;
136*d85937e6SScott Wood 
137*d85937e6SScott Wood     attr.group = KVM_DEV_MPIC_GRP_MISC;
138*d85937e6SScott Wood     attr.attr = KVM_DEV_MPIC_BASE_ADDR;
139*d85937e6SScott Wood     attr.addr = (uint64_t)(unsigned long)&reg_base;
140*d85937e6SScott Wood 
141*d85937e6SScott Wood     ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
142*d85937e6SScott Wood     if (ret < 0) {
143*d85937e6SScott Wood         fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
144*d85937e6SScott Wood                 strerror(errno), reg_base);
145*d85937e6SScott Wood     }
146*d85937e6SScott Wood }
147*d85937e6SScott Wood 
148*d85937e6SScott Wood static int kvm_openpic_init(SysBusDevice *dev)
149*d85937e6SScott Wood {
150*d85937e6SScott Wood     KVMState *s = kvm_state;
151*d85937e6SScott Wood     KVMOpenPICState *opp = FROM_SYSBUS(typeof(*opp), dev);
152*d85937e6SScott Wood     int kvm_openpic_model;
153*d85937e6SScott Wood     struct kvm_create_device cd = {0};
154*d85937e6SScott Wood     int ret, i;
155*d85937e6SScott Wood 
156*d85937e6SScott Wood     if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
157*d85937e6SScott Wood         return -EINVAL;
158*d85937e6SScott Wood     }
159*d85937e6SScott Wood 
160*d85937e6SScott Wood     switch (opp->model) {
161*d85937e6SScott Wood     case OPENPIC_MODEL_FSL_MPIC_20:
162*d85937e6SScott Wood         kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
163*d85937e6SScott Wood         break;
164*d85937e6SScott Wood 
165*d85937e6SScott Wood     case OPENPIC_MODEL_FSL_MPIC_42:
166*d85937e6SScott Wood         kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
167*d85937e6SScott Wood         break;
168*d85937e6SScott Wood 
169*d85937e6SScott Wood     default:
170*d85937e6SScott Wood         return -EINVAL;
171*d85937e6SScott Wood     }
172*d85937e6SScott Wood 
173*d85937e6SScott Wood     cd.type = kvm_openpic_model;
174*d85937e6SScott Wood     ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
175*d85937e6SScott Wood     if (ret < 0) {
176*d85937e6SScott Wood         qemu_log_mask(LOG_UNIMP, "%s: can't create device %d: %s\n",
177*d85937e6SScott Wood                       __func__, cd.type, strerror(errno));
178*d85937e6SScott Wood         return -EINVAL;
179*d85937e6SScott Wood     }
180*d85937e6SScott Wood     opp->fd = cd.fd;
181*d85937e6SScott Wood 
182*d85937e6SScott Wood     memory_region_init_io(&opp->mem, &kvm_openpic_mem_ops, opp,
183*d85937e6SScott Wood                           "kvm-openpic", 0x40000);
184*d85937e6SScott Wood 
185*d85937e6SScott Wood     sysbus_init_mmio(dev, &opp->mem);
186*d85937e6SScott Wood     qdev_init_gpio_in(&dev->qdev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
187*d85937e6SScott Wood 
188*d85937e6SScott Wood     opp->mem_listener.region_add = kvm_openpic_region_add;
189*d85937e6SScott Wood     opp->mem_listener.region_add = kvm_openpic_region_del;
190*d85937e6SScott Wood     memory_listener_register(&opp->mem_listener, &address_space_memory);
191*d85937e6SScott Wood 
192*d85937e6SScott Wood     /* indicate pic capabilities */
193*d85937e6SScott Wood     msi_supported = true;
194*d85937e6SScott Wood     kvm_kernel_irqchip = true;
195*d85937e6SScott Wood     kvm_async_interrupts_allowed = true;
196*d85937e6SScott Wood 
197*d85937e6SScott Wood     /* set up irq routing */
198*d85937e6SScott Wood     kvm_init_irq_routing(kvm_state);
199*d85937e6SScott Wood     for (i = 0; i < 256; ++i) {
200*d85937e6SScott Wood         kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
201*d85937e6SScott Wood     }
202*d85937e6SScott Wood 
203*d85937e6SScott Wood     kvm_irqfds_allowed = true;
204*d85937e6SScott Wood     kvm_msi_via_irqfd_allowed = true;
205*d85937e6SScott Wood     kvm_gsi_routing_allowed = true;
206*d85937e6SScott Wood 
207*d85937e6SScott Wood     kvm_irqchip_commit_routes(s);
208*d85937e6SScott Wood 
209*d85937e6SScott Wood     return 0;
210*d85937e6SScott Wood }
211*d85937e6SScott Wood 
212*d85937e6SScott Wood int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
213*d85937e6SScott Wood {
214*d85937e6SScott Wood     KVMOpenPICState *opp = FROM_SYSBUS(typeof(*opp), SYS_BUS_DEVICE(d));
215*d85937e6SScott Wood     struct kvm_enable_cap encap = {};
216*d85937e6SScott Wood 
217*d85937e6SScott Wood     encap.cap = KVM_CAP_IRQ_MPIC;
218*d85937e6SScott Wood     encap.args[0] = opp->fd;
219*d85937e6SScott Wood     encap.args[1] = cs->cpu_index;
220*d85937e6SScott Wood 
221*d85937e6SScott Wood     return kvm_vcpu_ioctl(cs, KVM_ENABLE_CAP, &encap);
222*d85937e6SScott Wood }
223*d85937e6SScott Wood 
224*d85937e6SScott Wood static Property kvm_openpic_properties[] = {
225*d85937e6SScott Wood     DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
226*d85937e6SScott Wood                        OPENPIC_MODEL_FSL_MPIC_20),
227*d85937e6SScott Wood     DEFINE_PROP_END_OF_LIST(),
228*d85937e6SScott Wood };
229*d85937e6SScott Wood 
230*d85937e6SScott Wood static void kvm_openpic_class_init(ObjectClass *klass, void *data)
231*d85937e6SScott Wood {
232*d85937e6SScott Wood     DeviceClass *dc = DEVICE_CLASS(klass);
233*d85937e6SScott Wood     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
234*d85937e6SScott Wood 
235*d85937e6SScott Wood     k->init = kvm_openpic_init;
236*d85937e6SScott Wood     dc->props = kvm_openpic_properties;
237*d85937e6SScott Wood     dc->reset = kvm_openpic_reset;
238*d85937e6SScott Wood }
239*d85937e6SScott Wood 
240*d85937e6SScott Wood static const TypeInfo kvm_openpic_info = {
241*d85937e6SScott Wood     .name          = "kvm-openpic",
242*d85937e6SScott Wood     .parent        = TYPE_SYS_BUS_DEVICE,
243*d85937e6SScott Wood     .instance_size = sizeof(KVMOpenPICState),
244*d85937e6SScott Wood     .class_init    = kvm_openpic_class_init,
245*d85937e6SScott Wood };
246*d85937e6SScott Wood 
247*d85937e6SScott Wood static void kvm_openpic_register_types(void)
248*d85937e6SScott Wood {
249*d85937e6SScott Wood     type_register_static(&kvm_openpic_info);
250*d85937e6SScott Wood }
251*d85937e6SScott Wood 
252*d85937e6SScott Wood type_init(kvm_openpic_register_types)
253