2 * KVM in-kernel OpenPIC
4 * Copyright 2013 Freescale Semiconductor, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include <sys/ioctl.h>
28 #include "hw/ppc/openpic.h"
29 #include "hw/ppc/openpic_kvm.h"
30 #include "hw/pci/msi.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/sysbus.h"
33 #include "sysemu/kvm.h"
35 #include "qemu/module.h"
36 #include "qom/object.h"
38 #define GCR_RESET 0x80000000
40 OBJECT_DECLARE_SIMPLE_TYPE(KVMOpenPICState
, KVM_OPENPIC
)
42 struct KVMOpenPICState
{
44 SysBusDevice parent_obj
;
48 MemoryListener mem_listener
;
54 static void kvm_openpic_set_irq(void *opaque
, int n_IRQ
, int level
)
56 kvm_set_irq(kvm_state
, n_IRQ
, level
);
59 static void kvm_openpic_write(void *opaque
, hwaddr addr
, uint64_t val
,
62 KVMOpenPICState
*opp
= opaque
;
63 struct kvm_device_attr attr
;
67 attr
.group
= KVM_DEV_MPIC_GRP_REGISTER
;
69 attr
.addr
= (uint64_t)(unsigned long)&val32
;
71 ret
= ioctl(opp
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
73 qemu_log_mask(LOG_UNIMP
, "%s: %s %" PRIx64
"\n", __func__
,
74 strerror(errno
), attr
.attr
);
78 static void kvm_openpic_reset(DeviceState
*d
)
80 KVMOpenPICState
*opp
= KVM_OPENPIC(d
);
82 /* Trigger the GCR.RESET bit to reset the PIC */
83 kvm_openpic_write(opp
, 0x1020, GCR_RESET
, sizeof(uint32_t));
86 static uint64_t kvm_openpic_read(void *opaque
, hwaddr addr
, unsigned size
)
88 KVMOpenPICState
*opp
= opaque
;
89 struct kvm_device_attr attr
;
90 uint32_t val
= 0xdeadbeef;
93 attr
.group
= KVM_DEV_MPIC_GRP_REGISTER
;
95 attr
.addr
= (uint64_t)(unsigned long)&val
;
97 ret
= ioctl(opp
->fd
, KVM_GET_DEVICE_ATTR
, &attr
);
99 qemu_log_mask(LOG_UNIMP
, "%s: %s %" PRIx64
"\n", __func__
,
100 strerror(errno
), attr
.attr
);
107 static const MemoryRegionOps kvm_openpic_mem_ops
= {
108 .write
= kvm_openpic_write
,
109 .read
= kvm_openpic_read
,
110 .endianness
= DEVICE_BIG_ENDIAN
,
112 .min_access_size
= 4,
113 .max_access_size
= 4,
117 static void kvm_openpic_region_add(MemoryListener
*listener
,
118 MemoryRegionSection
*section
)
120 KVMOpenPICState
*opp
= container_of(listener
, KVMOpenPICState
,
122 struct kvm_device_attr attr
;
126 /* Ignore events on regions that are not us */
127 if (section
->mr
!= &opp
->mem
) {
133 * We can only map the MPIC once. Since we are already mapped,
134 * the best we can do is ignore new maps.
139 reg_base
= section
->offset_within_address_space
;
140 opp
->mapped
= reg_base
;
142 attr
.group
= KVM_DEV_MPIC_GRP_MISC
;
143 attr
.attr
= KVM_DEV_MPIC_BASE_ADDR
;
144 attr
.addr
= (uint64_t)(unsigned long)®_base
;
146 ret
= ioctl(opp
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
148 fprintf(stderr
, "%s: %s %" PRIx64
"\n", __func__
,
149 strerror(errno
), reg_base
);
153 static void kvm_openpic_region_del(MemoryListener
*listener
,
154 MemoryRegionSection
*section
)
156 KVMOpenPICState
*opp
= container_of(listener
, KVMOpenPICState
,
158 struct kvm_device_attr attr
;
159 uint64_t reg_base
= 0;
162 /* Ignore events on regions that are not us */
163 if (section
->mr
!= &opp
->mem
) {
167 if (section
->offset_within_address_space
!= opp
->mapped
) {
169 * We can only map the MPIC once. This mapping was a secondary
170 * one that we couldn't fulfill. Ignore it.
176 attr
.group
= KVM_DEV_MPIC_GRP_MISC
;
177 attr
.attr
= KVM_DEV_MPIC_BASE_ADDR
;
178 attr
.addr
= (uint64_t)(unsigned long)®_base
;
180 ret
= ioctl(opp
->fd
, KVM_SET_DEVICE_ATTR
, &attr
);
182 fprintf(stderr
, "%s: %s %" PRIx64
"\n", __func__
,
183 strerror(errno
), reg_base
);
187 static void kvm_openpic_init(Object
*obj
)
189 KVMOpenPICState
*opp
= KVM_OPENPIC(obj
);
191 memory_region_init_io(&opp
->mem
, OBJECT(opp
), &kvm_openpic_mem_ops
, opp
,
192 "kvm-openpic", 0x40000);
195 static void kvm_openpic_realize(DeviceState
*dev
, Error
**errp
)
197 SysBusDevice
*d
= SYS_BUS_DEVICE(dev
);
198 KVMOpenPICState
*opp
= KVM_OPENPIC(dev
);
199 KVMState
*s
= kvm_state
;
200 int kvm_openpic_model
;
201 struct kvm_create_device cd
= {0};
204 if (!kvm_check_extension(s
, KVM_CAP_DEVICE_CTRL
)) {
205 error_setg(errp
, "Kernel is lacking Device Control API");
209 switch (opp
->model
) {
210 case OPENPIC_MODEL_FSL_MPIC_20
:
211 kvm_openpic_model
= KVM_DEV_TYPE_FSL_MPIC_20
;
214 case OPENPIC_MODEL_FSL_MPIC_42
:
215 kvm_openpic_model
= KVM_DEV_TYPE_FSL_MPIC_42
;
219 error_setg(errp
, "Unsupported OpenPIC model %" PRIu32
, opp
->model
);
223 cd
.type
= kvm_openpic_model
;
224 ret
= kvm_vm_ioctl(s
, KVM_CREATE_DEVICE
, &cd
);
226 error_setg(errp
, "Can't create device %d: %s",
227 cd
.type
, strerror(errno
));
232 sysbus_init_mmio(d
, &opp
->mem
);
233 qdev_init_gpio_in(dev
, kvm_openpic_set_irq
, OPENPIC_MAX_IRQ
);
235 opp
->mem_listener
.region_add
= kvm_openpic_region_add
;
236 opp
->mem_listener
.region_del
= kvm_openpic_region_del
;
237 opp
->mem_listener
.name
= "openpic-kvm";
238 memory_listener_register(&opp
->mem_listener
, &address_space_memory
);
240 /* indicate pic capabilities */
241 msi_nonbroken
= true;
242 kvm_kernel_irqchip
= true;
243 kvm_async_interrupts_allowed
= true;
245 /* set up irq routing */
246 kvm_init_irq_routing(kvm_state
);
247 for (i
= 0; i
< 256; ++i
) {
248 kvm_irqchip_add_irq_route(kvm_state
, i
, 0, i
);
251 kvm_msi_via_irqfd_allowed
= true;
252 kvm_gsi_routing_allowed
= true;
254 kvm_irqchip_commit_routes(s
);
257 int kvm_openpic_connect_vcpu(DeviceState
*d
, CPUState
*cs
)
259 KVMOpenPICState
*opp
= KVM_OPENPIC(d
);
261 return kvm_vcpu_enable_cap(cs
, KVM_CAP_IRQ_MPIC
, 0, opp
->fd
,
262 kvm_arch_vcpu_id(cs
));
265 static Property kvm_openpic_properties
[] = {
266 DEFINE_PROP_UINT32("model", KVMOpenPICState
, model
,
267 OPENPIC_MODEL_FSL_MPIC_20
),
268 DEFINE_PROP_END_OF_LIST(),
271 static void kvm_openpic_class_init(ObjectClass
*oc
, void *data
)
273 DeviceClass
*dc
= DEVICE_CLASS(oc
);
275 dc
->realize
= kvm_openpic_realize
;
276 device_class_set_props(dc
, kvm_openpic_properties
);
277 dc
->reset
= kvm_openpic_reset
;
278 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
281 static const TypeInfo kvm_openpic_info
= {
282 .name
= TYPE_KVM_OPENPIC
,
283 .parent
= TYPE_SYS_BUS_DEVICE
,
284 .instance_size
= sizeof(KVMOpenPICState
),
285 .instance_init
= kvm_openpic_init
,
286 .class_init
= kvm_openpic_class_init
,
289 static void kvm_openpic_register_types(void)
291 type_register_static(&kvm_openpic_info
);
294 type_init(kvm_openpic_register_types
)