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