openpic: move KVM-specific declarations into separate openpic_kvm.h file
[qemu/ar7.git] / hw / intc / openpic_kvm.c
blobf1a59e5a8519d9a56a1b5025fac242c2ba961e88
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include <sys/ioctl.h>
30 #include "exec/address-spaces.h"
31 #include "hw/hw.h"
32 #include "hw/ppc/openpic.h"
33 #include "hw/ppc/openpic_kvm.h"
34 #include "hw/pci/msi.h"
35 #include "hw/sysbus.h"
36 #include "sysemu/kvm.h"
37 #include "qemu/log.h"
39 #define GCR_RESET 0x80000000
41 #define KVM_OPENPIC(obj) \
42 OBJECT_CHECK(KVMOpenPICState, (obj), TYPE_KVM_OPENPIC)
44 typedef struct KVMOpenPICState {
45 /*< private >*/
46 SysBusDevice parent_obj;
47 /*< public >*/
49 MemoryRegion mem;
50 MemoryListener mem_listener;
51 uint32_t fd;
52 uint32_t model;
53 hwaddr mapped;
54 } KVMOpenPICState;
56 static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
58 kvm_set_irq(kvm_state, n_IRQ, level);
61 static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
62 unsigned size)
64 KVMOpenPICState *opp = opaque;
65 struct kvm_device_attr attr;
66 uint32_t val32 = val;
67 int ret;
69 attr.group = KVM_DEV_MPIC_GRP_REGISTER;
70 attr.attr = addr;
71 attr.addr = (uint64_t)(unsigned long)&val32;
73 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
74 if (ret < 0) {
75 qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
76 strerror(errno), attr.attr);
80 static void kvm_openpic_reset(DeviceState *d)
82 KVMOpenPICState *opp = KVM_OPENPIC(d);
84 /* Trigger the GCR.RESET bit to reset the PIC */
85 kvm_openpic_write(opp, 0x1020, GCR_RESET, sizeof(uint32_t));
88 static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
90 KVMOpenPICState *opp = opaque;
91 struct kvm_device_attr attr;
92 uint32_t val = 0xdeadbeef;
93 int ret;
95 attr.group = KVM_DEV_MPIC_GRP_REGISTER;
96 attr.attr = addr;
97 attr.addr = (uint64_t)(unsigned long)&val;
99 ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr);
100 if (ret < 0) {
101 qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
102 strerror(errno), attr.attr);
103 return 0;
106 return val;
109 static const MemoryRegionOps kvm_openpic_mem_ops = {
110 .write = kvm_openpic_write,
111 .read = kvm_openpic_read,
112 .endianness = DEVICE_BIG_ENDIAN,
113 .impl = {
114 .min_access_size = 4,
115 .max_access_size = 4,
119 static void kvm_openpic_region_add(MemoryListener *listener,
120 MemoryRegionSection *section)
122 KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
123 mem_listener);
124 struct kvm_device_attr attr;
125 uint64_t reg_base;
126 int ret;
128 if (section->fv != address_space_to_flatview(&address_space_memory)) {
129 abort();
132 /* Ignore events on regions that are not us */
133 if (section->mr != &opp->mem) {
134 return;
137 if (opp->mapped) {
139 * We can only map the MPIC once. Since we are already mapped,
140 * the best we can do is ignore new maps.
142 return;
145 reg_base = section->offset_within_address_space;
146 opp->mapped = reg_base;
148 attr.group = KVM_DEV_MPIC_GRP_MISC;
149 attr.attr = KVM_DEV_MPIC_BASE_ADDR;
150 attr.addr = (uint64_t)(unsigned long)&reg_base;
152 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
153 if (ret < 0) {
154 fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
155 strerror(errno), reg_base);
159 static void kvm_openpic_region_del(MemoryListener *listener,
160 MemoryRegionSection *section)
162 KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
163 mem_listener);
164 struct kvm_device_attr attr;
165 uint64_t reg_base = 0;
166 int ret;
168 /* Ignore events on regions that are not us */
169 if (section->mr != &opp->mem) {
170 return;
173 if (section->offset_within_address_space != opp->mapped) {
175 * We can only map the MPIC once. This mapping was a secondary
176 * one that we couldn't fulfill. Ignore it.
178 return;
180 opp->mapped = 0;
182 attr.group = KVM_DEV_MPIC_GRP_MISC;
183 attr.attr = KVM_DEV_MPIC_BASE_ADDR;
184 attr.addr = (uint64_t)(unsigned long)&reg_base;
186 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
187 if (ret < 0) {
188 fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
189 strerror(errno), reg_base);
193 static void kvm_openpic_init(Object *obj)
195 KVMOpenPICState *opp = KVM_OPENPIC(obj);
197 memory_region_init_io(&opp->mem, OBJECT(opp), &kvm_openpic_mem_ops, opp,
198 "kvm-openpic", 0x40000);
201 static void kvm_openpic_realize(DeviceState *dev, Error **errp)
203 SysBusDevice *d = SYS_BUS_DEVICE(dev);
204 KVMOpenPICState *opp = KVM_OPENPIC(dev);
205 KVMState *s = kvm_state;
206 int kvm_openpic_model;
207 struct kvm_create_device cd = {0};
208 int ret, i;
210 if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
211 error_setg(errp, "Kernel is lacking Device Control API");
212 return;
215 switch (opp->model) {
216 case OPENPIC_MODEL_FSL_MPIC_20:
217 kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
218 break;
220 case OPENPIC_MODEL_FSL_MPIC_42:
221 kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
222 break;
224 default:
225 error_setg(errp, "Unsupported OpenPIC model %" PRIu32, opp->model);
226 return;
229 cd.type = kvm_openpic_model;
230 ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
231 if (ret < 0) {
232 error_setg(errp, "Can't create device %d: %s",
233 cd.type, strerror(errno));
234 return;
236 opp->fd = cd.fd;
238 sysbus_init_mmio(d, &opp->mem);
239 qdev_init_gpio_in(dev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
241 opp->mem_listener.region_add = kvm_openpic_region_add;
242 opp->mem_listener.region_del = kvm_openpic_region_del;
243 memory_listener_register(&opp->mem_listener, &address_space_memory);
245 /* indicate pic capabilities */
246 msi_nonbroken = true;
247 kvm_kernel_irqchip = true;
248 kvm_async_interrupts_allowed = true;
250 /* set up irq routing */
251 kvm_init_irq_routing(kvm_state);
252 for (i = 0; i < 256; ++i) {
253 kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
256 kvm_msi_via_irqfd_allowed = true;
257 kvm_gsi_routing_allowed = true;
259 kvm_irqchip_commit_routes(s);
262 int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
264 KVMOpenPICState *opp = KVM_OPENPIC(d);
266 return kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_MPIC, 0, opp->fd,
267 kvm_arch_vcpu_id(cs));
270 static Property kvm_openpic_properties[] = {
271 DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
272 OPENPIC_MODEL_FSL_MPIC_20),
273 DEFINE_PROP_END_OF_LIST(),
276 static void kvm_openpic_class_init(ObjectClass *oc, void *data)
278 DeviceClass *dc = DEVICE_CLASS(oc);
280 dc->realize = kvm_openpic_realize;
281 dc->props = kvm_openpic_properties;
282 dc->reset = kvm_openpic_reset;
283 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
286 static const TypeInfo kvm_openpic_info = {
287 .name = TYPE_KVM_OPENPIC,
288 .parent = TYPE_SYS_BUS_DEVICE,
289 .instance_size = sizeof(KVMOpenPICState),
290 .instance_init = kvm_openpic_init,
291 .class_init = kvm_openpic_class_init,
294 static void kvm_openpic_register_types(void)
296 type_register_static(&kvm_openpic_info);
299 type_init(kvm_openpic_register_types)