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
2590191d07SPeter Maydell #include "qemu/osdep.h"
26da34e65cSMarkus Armbruster #include "qapi/error.h"
27d85937e6SScott Wood #include <sys/ioctl.h>
28d85937e6SScott Wood #include "hw/ppc/openpic.h"
298d085cf0SMark Cave-Ayland #include "hw/ppc/openpic_kvm.h"
30d85937e6SScott Wood #include "hw/pci/msi.h"
31a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
32d85937e6SScott Wood #include "hw/sysbus.h"
33d85937e6SScott Wood #include "sysemu/kvm.h"
34d85937e6SScott Wood #include "qemu/log.h"
350b8fa32fSMarkus Armbruster #include "qemu/module.h"
36db1015e9SEduardo Habkost #include "qom/object.h"
37d85937e6SScott Wood
38af354f19SAlexander Graf #define GCR_RESET 0x80000000
39af354f19SAlexander Graf
408063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(KVMOpenPICState, KVM_OPENPIC)
41dd49c038SAndreas Färber
42db1015e9SEduardo Habkost struct KVMOpenPICState {
43dd49c038SAndreas Färber /*< private >*/
44dd49c038SAndreas Färber SysBusDevice parent_obj;
45dd49c038SAndreas Färber /*< public >*/
46dd49c038SAndreas Färber
47d85937e6SScott Wood MemoryRegion mem;
48d85937e6SScott Wood MemoryListener mem_listener;
49d85937e6SScott Wood uint32_t fd;
50d85937e6SScott Wood uint32_t model;
519ac58dc5SAlexander Graf hwaddr mapped;
52db1015e9SEduardo Habkost };
53d85937e6SScott Wood
kvm_openpic_set_irq(void * opaque,int n_IRQ,int level)54d85937e6SScott Wood static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
55d85937e6SScott Wood {
56d85937e6SScott Wood kvm_set_irq(kvm_state, n_IRQ, level);
57d85937e6SScott Wood }
58d85937e6SScott Wood
kvm_openpic_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)59d85937e6SScott Wood static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
60d85937e6SScott Wood unsigned size)
61d85937e6SScott Wood {
62d85937e6SScott Wood KVMOpenPICState *opp = opaque;
63d85937e6SScott Wood struct kvm_device_attr attr;
64d85937e6SScott Wood uint32_t val32 = val;
65d85937e6SScott Wood int ret;
66d85937e6SScott Wood
67d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_REGISTER;
68d85937e6SScott Wood attr.attr = addr;
69d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)&val32;
70d85937e6SScott Wood
71d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
72d85937e6SScott Wood if (ret < 0) {
73d85937e6SScott Wood qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
74d85937e6SScott Wood strerror(errno), attr.attr);
75d85937e6SScott Wood }
76d85937e6SScott Wood }
77d85937e6SScott Wood
kvm_openpic_reset(DeviceState * d)78af354f19SAlexander Graf static void kvm_openpic_reset(DeviceState *d)
79af354f19SAlexander Graf {
80af354f19SAlexander Graf KVMOpenPICState *opp = KVM_OPENPIC(d);
81af354f19SAlexander Graf
82af354f19SAlexander Graf /* Trigger the GCR.RESET bit to reset the PIC */
83af354f19SAlexander Graf kvm_openpic_write(opp, 0x1020, GCR_RESET, sizeof(uint32_t));
84af354f19SAlexander Graf }
85af354f19SAlexander Graf
kvm_openpic_read(void * opaque,hwaddr addr,unsigned size)86d85937e6SScott Wood static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
87d85937e6SScott Wood {
88d85937e6SScott Wood KVMOpenPICState *opp = opaque;
89d85937e6SScott Wood struct kvm_device_attr attr;
90d85937e6SScott Wood uint32_t val = 0xdeadbeef;
91d85937e6SScott Wood int ret;
92d85937e6SScott Wood
93d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_REGISTER;
94d85937e6SScott Wood attr.attr = addr;
95d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)&val;
96d85937e6SScott Wood
97d85937e6SScott Wood ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr);
98d85937e6SScott Wood if (ret < 0) {
99d85937e6SScott Wood qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
100d85937e6SScott Wood strerror(errno), attr.attr);
101d85937e6SScott Wood return 0;
102d85937e6SScott Wood }
103d85937e6SScott Wood
104d85937e6SScott Wood return val;
105d85937e6SScott Wood }
106d85937e6SScott Wood
107d85937e6SScott Wood static const MemoryRegionOps kvm_openpic_mem_ops = {
108d85937e6SScott Wood .write = kvm_openpic_write,
109d85937e6SScott Wood .read = kvm_openpic_read,
110d85937e6SScott Wood .endianness = DEVICE_BIG_ENDIAN,
111d85937e6SScott Wood .impl = {
112d85937e6SScott Wood .min_access_size = 4,
113d85937e6SScott Wood .max_access_size = 4,
114d85937e6SScott Wood },
115d85937e6SScott Wood };
116d85937e6SScott Wood
kvm_openpic_region_add(MemoryListener * listener,MemoryRegionSection * section)117d85937e6SScott Wood static void kvm_openpic_region_add(MemoryListener *listener,
118d85937e6SScott Wood MemoryRegionSection *section)
119d85937e6SScott Wood {
120d85937e6SScott Wood KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
121d85937e6SScott Wood mem_listener);
122d85937e6SScott Wood struct kvm_device_attr attr;
123d85937e6SScott Wood uint64_t reg_base;
124d85937e6SScott Wood int ret;
125d85937e6SScott Wood
12687d8354dSAlexander Graf /* Ignore events on regions that are not us */
12787d8354dSAlexander Graf if (section->mr != &opp->mem) {
12887d8354dSAlexander Graf return;
12987d8354dSAlexander Graf }
13087d8354dSAlexander Graf
1319ac58dc5SAlexander Graf if (opp->mapped) {
1329ac58dc5SAlexander Graf /*
1339ac58dc5SAlexander Graf * We can only map the MPIC once. Since we are already mapped,
1349ac58dc5SAlexander Graf * the best we can do is ignore new maps.
1359ac58dc5SAlexander Graf */
1369ac58dc5SAlexander Graf return;
1379ac58dc5SAlexander Graf }
1389ac58dc5SAlexander Graf
139d85937e6SScott Wood reg_base = section->offset_within_address_space;
1409ac58dc5SAlexander Graf opp->mapped = reg_base;
141d85937e6SScott Wood
142d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_MISC;
143d85937e6SScott Wood attr.attr = KVM_DEV_MPIC_BASE_ADDR;
144d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)®_base;
145d85937e6SScott Wood
146d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
147d85937e6SScott Wood if (ret < 0) {
148d85937e6SScott Wood fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
149d85937e6SScott Wood strerror(errno), reg_base);
150d85937e6SScott Wood }
151d85937e6SScott Wood }
152d85937e6SScott Wood
kvm_openpic_region_del(MemoryListener * listener,MemoryRegionSection * section)153d85937e6SScott Wood static void kvm_openpic_region_del(MemoryListener *listener,
154d85937e6SScott Wood MemoryRegionSection *section)
155d85937e6SScott Wood {
156d85937e6SScott Wood KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
157d85937e6SScott Wood mem_listener);
158d85937e6SScott Wood struct kvm_device_attr attr;
159d85937e6SScott Wood uint64_t reg_base = 0;
160d85937e6SScott Wood int ret;
161d85937e6SScott Wood
16287d8354dSAlexander Graf /* Ignore events on regions that are not us */
16387d8354dSAlexander Graf if (section->mr != &opp->mem) {
16487d8354dSAlexander Graf return;
16587d8354dSAlexander Graf }
16687d8354dSAlexander Graf
1679ac58dc5SAlexander Graf if (section->offset_within_address_space != opp->mapped) {
1689ac58dc5SAlexander Graf /*
1699ac58dc5SAlexander Graf * We can only map the MPIC once. This mapping was a secondary
1709ac58dc5SAlexander Graf * one that we couldn't fulfill. Ignore it.
1719ac58dc5SAlexander Graf */
1729ac58dc5SAlexander Graf return;
1739ac58dc5SAlexander Graf }
1749ac58dc5SAlexander Graf opp->mapped = 0;
1759ac58dc5SAlexander Graf
176d85937e6SScott Wood attr.group = KVM_DEV_MPIC_GRP_MISC;
177d85937e6SScott Wood attr.attr = KVM_DEV_MPIC_BASE_ADDR;
178d85937e6SScott Wood attr.addr = (uint64_t)(unsigned long)®_base;
179d85937e6SScott Wood
180d85937e6SScott Wood ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
181d85937e6SScott Wood if (ret < 0) {
182d85937e6SScott Wood fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
183d85937e6SScott Wood strerror(errno), reg_base);
184d85937e6SScott Wood }
185d85937e6SScott Wood }
186d85937e6SScott Wood
kvm_openpic_init(Object * obj)187dd49c038SAndreas Färber static void kvm_openpic_init(Object *obj)
188d85937e6SScott Wood {
189dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(obj);
190dd49c038SAndreas Färber
1911437c94bSPaolo Bonzini memory_region_init_io(&opp->mem, OBJECT(opp), &kvm_openpic_mem_ops, opp,
192dd49c038SAndreas Färber "kvm-openpic", 0x40000);
193dd49c038SAndreas Färber }
194dd49c038SAndreas Färber
kvm_openpic_realize(DeviceState * dev,Error ** errp)195dd49c038SAndreas Färber static void kvm_openpic_realize(DeviceState *dev, Error **errp)
196dd49c038SAndreas Färber {
197dd49c038SAndreas Färber SysBusDevice *d = SYS_BUS_DEVICE(dev);
198dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(dev);
199d85937e6SScott Wood KVMState *s = kvm_state;
200d85937e6SScott Wood int kvm_openpic_model;
201d85937e6SScott Wood struct kvm_create_device cd = {0};
202d85937e6SScott Wood int ret, i;
203d85937e6SScott Wood
204d85937e6SScott Wood if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
205dd49c038SAndreas Färber error_setg(errp, "Kernel is lacking Device Control API");
206dd49c038SAndreas Färber return;
207d85937e6SScott Wood }
208d85937e6SScott Wood
209d85937e6SScott Wood switch (opp->model) {
210d85937e6SScott Wood case OPENPIC_MODEL_FSL_MPIC_20:
211d85937e6SScott Wood kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
212d85937e6SScott Wood break;
213d85937e6SScott Wood
214d85937e6SScott Wood case OPENPIC_MODEL_FSL_MPIC_42:
215d85937e6SScott Wood kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
216d85937e6SScott Wood break;
217d85937e6SScott Wood
218d85937e6SScott Wood default:
219dd49c038SAndreas Färber error_setg(errp, "Unsupported OpenPIC model %" PRIu32, opp->model);
220dd49c038SAndreas Färber return;
221d85937e6SScott Wood }
222d85937e6SScott Wood
223d85937e6SScott Wood cd.type = kvm_openpic_model;
224d85937e6SScott Wood ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
225d85937e6SScott Wood if (ret < 0) {
226dd49c038SAndreas Färber error_setg(errp, "Can't create device %d: %s",
227dd49c038SAndreas Färber cd.type, strerror(errno));
228dd49c038SAndreas Färber return;
229d85937e6SScott Wood }
230d85937e6SScott Wood opp->fd = cd.fd;
231d85937e6SScott Wood
232dd49c038SAndreas Färber sysbus_init_mmio(d, &opp->mem);
233dd49c038SAndreas Färber qdev_init_gpio_in(dev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
234d85937e6SScott Wood
235d85937e6SScott Wood opp->mem_listener.region_add = kvm_openpic_region_add;
2366f1834a2SPrasad Joshi opp->mem_listener.region_del = kvm_openpic_region_del;
237142518bdSPeter Xu opp->mem_listener.name = "openpic-kvm";
238d85937e6SScott Wood memory_listener_register(&opp->mem_listener, &address_space_memory);
239d85937e6SScott Wood
240d85937e6SScott Wood /* indicate pic capabilities */
241226419d6SMichael S. Tsirkin msi_nonbroken = true;
242d85937e6SScott Wood kvm_kernel_irqchip = true;
243d85937e6SScott Wood kvm_async_interrupts_allowed = true;
244d85937e6SScott Wood
245d85937e6SScott Wood /* set up irq routing */
246d85937e6SScott Wood kvm_init_irq_routing(kvm_state);
247d85937e6SScott Wood for (i = 0; i < 256; ++i) {
248d85937e6SScott Wood kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
249d85937e6SScott Wood }
250d85937e6SScott Wood
251d85937e6SScott Wood kvm_msi_via_irqfd_allowed = true;
252d85937e6SScott Wood kvm_gsi_routing_allowed = true;
253d85937e6SScott Wood
254d85937e6SScott Wood kvm_irqchip_commit_routes(s);
255d85937e6SScott Wood }
256d85937e6SScott Wood
kvm_openpic_connect_vcpu(DeviceState * d,CPUState * cs)257d85937e6SScott Wood int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
258d85937e6SScott Wood {
259dd49c038SAndreas Färber KVMOpenPICState *opp = KVM_OPENPIC(d);
260d85937e6SScott Wood
26148add816SCornelia Huck return kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_MPIC, 0, opp->fd,
26248add816SCornelia Huck kvm_arch_vcpu_id(cs));
263d85937e6SScott Wood }
264d85937e6SScott Wood
265d85937e6SScott Wood static Property kvm_openpic_properties[] = {
266d85937e6SScott Wood DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
267d85937e6SScott Wood OPENPIC_MODEL_FSL_MPIC_20),
268d85937e6SScott Wood DEFINE_PROP_END_OF_LIST(),
269d85937e6SScott Wood };
270d85937e6SScott Wood
kvm_openpic_class_init(ObjectClass * oc,void * data)271dd49c038SAndreas Färber static void kvm_openpic_class_init(ObjectClass *oc, void *data)
272d85937e6SScott Wood {
273dd49c038SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc);
274d85937e6SScott Wood
275dd49c038SAndreas Färber dc->realize = kvm_openpic_realize;
2764f67d30bSMarc-André Lureau device_class_set_props(dc, kvm_openpic_properties);
277*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, kvm_openpic_reset);
27829f8dd66SLaurent Vivier set_bit(DEVICE_CATEGORY_MISC, dc->categories);
279d85937e6SScott Wood }
280d85937e6SScott Wood
281d85937e6SScott Wood static const TypeInfo kvm_openpic_info = {
282dd49c038SAndreas Färber .name = TYPE_KVM_OPENPIC,
283d85937e6SScott Wood .parent = TYPE_SYS_BUS_DEVICE,
284d85937e6SScott Wood .instance_size = sizeof(KVMOpenPICState),
285dd49c038SAndreas Färber .instance_init = kvm_openpic_init,
286d85937e6SScott Wood .class_init = kvm_openpic_class_init,
287d85937e6SScott Wood };
288d85937e6SScott Wood
kvm_openpic_register_types(void)289d85937e6SScott Wood static void kvm_openpic_register_types(void)
290d85937e6SScott Wood {
291d85937e6SScott Wood type_register_static(&kvm_openpic_info);
292d85937e6SScott Wood }
293d85937e6SScott Wood
294d85937e6SScott Wood type_init(kvm_openpic_register_types)
295