blob: 649f476ac84e631b638883afd0658c6a7905ab0b [file] [log] [blame]
Scott Woodd85937e2013-06-12 15:32:51 -05001/*
2 * KVM in-kernel OpenPIC
3 *
4 * Copyright 2013 Freescale Semiconductor, Inc.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25#include <sys/ioctl.h>
26#include "exec/address-spaces.h"
27#include "hw/hw.h"
28#include "hw/ppc/openpic.h"
29#include "hw/pci/msi.h"
30#include "hw/sysbus.h"
31#include "sysemu/kvm.h"
32#include "qemu/log.h"
33
Alexander Grafaf354f12014-05-22 17:12:23 +020034#define GCR_RESET 0x80000000
35
Andreas Färberdd49c032013-06-16 21:30:40 +020036#define KVM_OPENPIC(obj) \
37 OBJECT_CHECK(KVMOpenPICState, (obj), TYPE_KVM_OPENPIC)
38
Scott Woodd85937e2013-06-12 15:32:51 -050039typedef struct KVMOpenPICState {
Andreas Färberdd49c032013-06-16 21:30:40 +020040 /*< private >*/
41 SysBusDevice parent_obj;
42 /*< public >*/
43
Scott Woodd85937e2013-06-12 15:32:51 -050044 MemoryRegion mem;
45 MemoryListener mem_listener;
46 uint32_t fd;
47 uint32_t model;
Alexander Graf9ac58dc2014-09-11 12:22:57 +020048 hwaddr mapped;
Scott Woodd85937e2013-06-12 15:32:51 -050049} KVMOpenPICState;
50
51static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
52{
53 kvm_set_irq(kvm_state, n_IRQ, level);
54}
55
Scott Woodd85937e2013-06-12 15:32:51 -050056static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
57 unsigned size)
58{
59 KVMOpenPICState *opp = opaque;
60 struct kvm_device_attr attr;
61 uint32_t val32 = val;
62 int ret;
63
64 attr.group = KVM_DEV_MPIC_GRP_REGISTER;
65 attr.attr = addr;
66 attr.addr = (uint64_t)(unsigned long)&val32;
67
68 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
69 if (ret < 0) {
70 qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
71 strerror(errno), attr.attr);
72 }
73}
74
Alexander Grafaf354f12014-05-22 17:12:23 +020075static void kvm_openpic_reset(DeviceState *d)
76{
77 KVMOpenPICState *opp = KVM_OPENPIC(d);
78
79 /* Trigger the GCR.RESET bit to reset the PIC */
80 kvm_openpic_write(opp, 0x1020, GCR_RESET, sizeof(uint32_t));
81}
82
Scott Woodd85937e2013-06-12 15:32:51 -050083static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
84{
85 KVMOpenPICState *opp = opaque;
86 struct kvm_device_attr attr;
87 uint32_t val = 0xdeadbeef;
88 int ret;
89
90 attr.group = KVM_DEV_MPIC_GRP_REGISTER;
91 attr.attr = addr;
92 attr.addr = (uint64_t)(unsigned long)&val;
93
94 ret = ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr);
95 if (ret < 0) {
96 qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__,
97 strerror(errno), attr.attr);
98 return 0;
99 }
100
101 return val;
102}
103
104static const MemoryRegionOps kvm_openpic_mem_ops = {
105 .write = kvm_openpic_write,
106 .read = kvm_openpic_read,
107 .endianness = DEVICE_BIG_ENDIAN,
108 .impl = {
109 .min_access_size = 4,
110 .max_access_size = 4,
111 },
112};
113
114static void kvm_openpic_region_add(MemoryListener *listener,
115 MemoryRegionSection *section)
116{
117 KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
118 mem_listener);
119 struct kvm_device_attr attr;
120 uint64_t reg_base;
121 int ret;
122
123 if (section->address_space != &address_space_memory) {
124 abort();
125 }
126
Alexander Graf87d83542014-04-02 11:41:58 +0200127 /* Ignore events on regions that are not us */
128 if (section->mr != &opp->mem) {
129 return;
130 }
131
Alexander Graf9ac58dc2014-09-11 12:22:57 +0200132 if (opp->mapped) {
133 /*
134 * We can only map the MPIC once. Since we are already mapped,
135 * the best we can do is ignore new maps.
136 */
137 return;
138 }
139
Scott Woodd85937e2013-06-12 15:32:51 -0500140 reg_base = section->offset_within_address_space;
Alexander Graf9ac58dc2014-09-11 12:22:57 +0200141 opp->mapped = reg_base;
Scott Woodd85937e2013-06-12 15:32:51 -0500142
143 attr.group = KVM_DEV_MPIC_GRP_MISC;
144 attr.attr = KVM_DEV_MPIC_BASE_ADDR;
145 attr.addr = (uint64_t)(unsigned long)&reg_base;
146
147 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
148 if (ret < 0) {
149 fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
150 strerror(errno), reg_base);
151 }
152}
153
154static void kvm_openpic_region_del(MemoryListener *listener,
155 MemoryRegionSection *section)
156{
157 KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
158 mem_listener);
159 struct kvm_device_attr attr;
160 uint64_t reg_base = 0;
161 int ret;
162
Alexander Graf87d83542014-04-02 11:41:58 +0200163 /* Ignore events on regions that are not us */
164 if (section->mr != &opp->mem) {
165 return;
166 }
167
Alexander Graf9ac58dc2014-09-11 12:22:57 +0200168 if (section->offset_within_address_space != opp->mapped) {
169 /*
170 * We can only map the MPIC once. This mapping was a secondary
171 * one that we couldn't fulfill. Ignore it.
172 */
173 return;
174 }
175 opp->mapped = 0;
176
Scott Woodd85937e2013-06-12 15:32:51 -0500177 attr.group = KVM_DEV_MPIC_GRP_MISC;
178 attr.attr = KVM_DEV_MPIC_BASE_ADDR;
179 attr.addr = (uint64_t)(unsigned long)&reg_base;
180
181 ret = ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr);
182 if (ret < 0) {
183 fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__,
184 strerror(errno), reg_base);
185 }
186}
187
Andreas Färberdd49c032013-06-16 21:30:40 +0200188static void kvm_openpic_init(Object *obj)
Scott Woodd85937e2013-06-12 15:32:51 -0500189{
Andreas Färberdd49c032013-06-16 21:30:40 +0200190 KVMOpenPICState *opp = KVM_OPENPIC(obj);
191
Paolo Bonzini1437c942013-06-06 21:25:08 -0400192 memory_region_init_io(&opp->mem, OBJECT(opp), &kvm_openpic_mem_ops, opp,
Andreas Färberdd49c032013-06-16 21:30:40 +0200193 "kvm-openpic", 0x40000);
194}
195
196static void kvm_openpic_realize(DeviceState *dev, Error **errp)
197{
198 SysBusDevice *d = SYS_BUS_DEVICE(dev);
199 KVMOpenPICState *opp = KVM_OPENPIC(dev);
Scott Woodd85937e2013-06-12 15:32:51 -0500200 KVMState *s = kvm_state;
Scott Woodd85937e2013-06-12 15:32:51 -0500201 int kvm_openpic_model;
202 struct kvm_create_device cd = {0};
203 int ret, i;
204
205 if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
Andreas Färberdd49c032013-06-16 21:30:40 +0200206 error_setg(errp, "Kernel is lacking Device Control API");
207 return;
Scott Woodd85937e2013-06-12 15:32:51 -0500208 }
209
210 switch (opp->model) {
211 case OPENPIC_MODEL_FSL_MPIC_20:
212 kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
213 break;
214
215 case OPENPIC_MODEL_FSL_MPIC_42:
216 kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
217 break;
218
219 default:
Andreas Färberdd49c032013-06-16 21:30:40 +0200220 error_setg(errp, "Unsupported OpenPIC model %" PRIu32, opp->model);
221 return;
Scott Woodd85937e2013-06-12 15:32:51 -0500222 }
223
224 cd.type = kvm_openpic_model;
225 ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
226 if (ret < 0) {
Andreas Färberdd49c032013-06-16 21:30:40 +0200227 error_setg(errp, "Can't create device %d: %s",
228 cd.type, strerror(errno));
229 return;
Scott Woodd85937e2013-06-12 15:32:51 -0500230 }
231 opp->fd = cd.fd;
232
Andreas Färberdd49c032013-06-16 21:30:40 +0200233 sysbus_init_mmio(d, &opp->mem);
234 qdev_init_gpio_in(dev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
Scott Woodd85937e2013-06-12 15:32:51 -0500235
236 opp->mem_listener.region_add = kvm_openpic_region_add;
Prasad Joshi6f1834a2014-03-23 14:58:39 +0530237 opp->mem_listener.region_del = kvm_openpic_region_del;
Scott Woodd85937e2013-06-12 15:32:51 -0500238 memory_listener_register(&opp->mem_listener, &address_space_memory);
239
240 /* indicate pic capabilities */
241 msi_supported = true;
242 kvm_kernel_irqchip = true;
243 kvm_async_interrupts_allowed = true;
244
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);
249 }
250
Scott Woodd85937e2013-06-12 15:32:51 -0500251 kvm_msi_via_irqfd_allowed = true;
252 kvm_gsi_routing_allowed = true;
253
254 kvm_irqchip_commit_routes(s);
Scott Woodd85937e2013-06-12 15:32:51 -0500255}
256
257int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
258{
Andreas Färberdd49c032013-06-16 21:30:40 +0200259 KVMOpenPICState *opp = KVM_OPENPIC(d);
Scott Woodd85937e2013-06-12 15:32:51 -0500260
Cornelia Huck48add812014-04-09 17:21:57 +0200261 return kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_MPIC, 0, opp->fd,
262 kvm_arch_vcpu_id(cs));
Scott Woodd85937e2013-06-12 15:32:51 -0500263}
264
265static Property kvm_openpic_properties[] = {
266 DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
267 OPENPIC_MODEL_FSL_MPIC_20),
268 DEFINE_PROP_END_OF_LIST(),
269};
270
Andreas Färberdd49c032013-06-16 21:30:40 +0200271static void kvm_openpic_class_init(ObjectClass *oc, void *data)
Scott Woodd85937e2013-06-12 15:32:51 -0500272{
Andreas Färberdd49c032013-06-16 21:30:40 +0200273 DeviceClass *dc = DEVICE_CLASS(oc);
Scott Woodd85937e2013-06-12 15:32:51 -0500274
Andreas Färberdd49c032013-06-16 21:30:40 +0200275 dc->realize = kvm_openpic_realize;
Scott Woodd85937e2013-06-12 15:32:51 -0500276 dc->props = kvm_openpic_properties;
277 dc->reset = kvm_openpic_reset;
Laurent Vivier29f8dd62015-09-26 18:22:12 +0200278 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Scott Woodd85937e2013-06-12 15:32:51 -0500279}
280
281static const TypeInfo kvm_openpic_info = {
Andreas Färberdd49c032013-06-16 21:30:40 +0200282 .name = TYPE_KVM_OPENPIC,
Scott Woodd85937e2013-06-12 15:32:51 -0500283 .parent = TYPE_SYS_BUS_DEVICE,
284 .instance_size = sizeof(KVMOpenPICState),
Andreas Färberdd49c032013-06-16 21:30:40 +0200285 .instance_init = kvm_openpic_init,
Scott Woodd85937e2013-06-12 15:32:51 -0500286 .class_init = kvm_openpic_class_init,
287};
288
289static void kvm_openpic_register_types(void)
290{
291 type_register_static(&kvm_openpic_info);
292}
293
294type_init(kvm_openpic_register_types)