xref: /openbmc/qemu/hw/intc/arm_gicv3_its_kvm.c (revision 0b1183e3)
1 /*
2  * KVM-based ITS implementation for a GICv3-based system
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  * Written by Pavel Fedin <p.fedin@samsung.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "hw/intc/arm_gicv3_its_common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm.h"
26 #include "kvm_arm.h"
27 #include "migration/blocker.h"
28 
29 #define TYPE_KVM_ARM_ITS "arm-its-kvm"
30 #define KVM_ARM_ITS(obj) OBJECT_CHECK(GICv3ITSState, (obj), TYPE_KVM_ARM_ITS)
31 
32 static int kvm_its_send_msi(GICv3ITSState *s, uint32_t value, uint16_t devid)
33 {
34     struct kvm_msi msi;
35 
36     if (unlikely(!s->translater_gpa_known)) {
37         MemoryRegion *mr = &s->iomem_its_translation;
38         MemoryRegionSection mrs;
39 
40         mrs = memory_region_find(mr, 0, 1);
41         memory_region_unref(mrs.mr);
42         s->gits_translater_gpa = mrs.offset_within_address_space + 0x40;
43         s->translater_gpa_known = true;
44     }
45 
46     msi.address_lo = extract64(s->gits_translater_gpa, 0, 32);
47     msi.address_hi = extract64(s->gits_translater_gpa, 32, 32);
48     msi.data = le32_to_cpu(value);
49     msi.flags = KVM_MSI_VALID_DEVID;
50     msi.devid = devid;
51     memset(msi.pad, 0, sizeof(msi.pad));
52 
53     return kvm_vm_ioctl(kvm_state, KVM_SIGNAL_MSI, &msi);
54 }
55 
56 /**
57  * vm_change_state_handler - VM change state callback aiming at flushing
58  * ITS tables into guest RAM
59  *
60  * The tables get flushed to guest RAM whenever the VM gets stopped.
61  */
62 static void vm_change_state_handler(void *opaque, int running,
63                                     RunState state)
64 {
65     GICv3ITSState *s = (GICv3ITSState *)opaque;
66     Error *err = NULL;
67     int ret;
68 
69     if (running) {
70         return;
71     }
72 
73     ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
74                             KVM_DEV_ARM_ITS_SAVE_TABLES, NULL, true, &err);
75     if (err) {
76         error_report_err(err);
77     }
78     if (ret < 0 && ret != -EFAULT) {
79         abort();
80     }
81 }
82 
83 static void kvm_arm_its_realize(DeviceState *dev, Error **errp)
84 {
85     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
86     Error *local_err = NULL;
87 
88     s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_ITS, false);
89     if (s->dev_fd < 0) {
90         error_setg_errno(errp, -s->dev_fd, "error creating in-kernel ITS");
91         return;
92     }
93 
94     /* explicit init of the ITS */
95     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
96                       KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
97 
98     /* register the base address */
99     kvm_arm_register_device(&s->iomem_its_cntrl, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
100                             KVM_VGIC_ITS_ADDR_TYPE, s->dev_fd);
101 
102     gicv3_its_init_mmio(s, NULL);
103 
104     if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
105         GITS_CTLR)) {
106         error_setg(&s->migration_blocker, "This operating system kernel "
107                    "does not support vITS migration");
108         migrate_add_blocker(s->migration_blocker, &local_err);
109         if (local_err) {
110             error_propagate(errp, local_err);
111             error_free(s->migration_blocker);
112             return;
113         }
114     }
115 
116     kvm_msi_use_devid = true;
117     kvm_gsi_direct_mapping = false;
118     kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
119 
120     qemu_add_vm_change_state_handler(vm_change_state_handler, s);
121 }
122 
123 static void kvm_arm_its_init(Object *obj)
124 {
125     GICv3ITSState *s = KVM_ARM_ITS(obj);
126 
127     object_property_add_link(obj, "parent-gicv3",
128                              "kvm-arm-gicv3", (Object **)&s->gicv3,
129                              object_property_allow_set_link,
130                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
131                              &error_abort);
132 }
133 
134 /**
135  * kvm_arm_its_pre_save - handles the saving of ITS registers.
136  * ITS tables are flushed into guest RAM separately and earlier,
137  * through the VM change state handler, since at the moment pre_save()
138  * is called, the guest RAM has already been saved.
139  */
140 static void kvm_arm_its_pre_save(GICv3ITSState *s)
141 {
142     int i;
143 
144     for (i = 0; i < 8; i++) {
145         kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
146                           GITS_BASER + i * 8, &s->baser[i], false,
147                           &error_abort);
148     }
149 
150     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
151                       GITS_CTLR, &s->ctlr, false, &error_abort);
152 
153     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
154                       GITS_CBASER, &s->cbaser, false, &error_abort);
155 
156     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
157                       GITS_CREADR, &s->creadr, false, &error_abort);
158 
159     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
160                       GITS_CWRITER, &s->cwriter, false, &error_abort);
161 
162     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
163                       GITS_IIDR, &s->iidr, false, &error_abort);
164 }
165 
166 /**
167  * kvm_arm_its_post_load - Restore both the ITS registers and tables
168  */
169 static void kvm_arm_its_post_load(GICv3ITSState *s)
170 {
171     int i;
172 
173     if (!s->iidr) {
174         return;
175     }
176 
177     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
178                       GITS_IIDR, &s->iidr, true, &error_abort);
179 
180     /*
181      * must be written before GITS_CREADR since GITS_CBASER write
182      * access resets GITS_CREADR.
183      */
184     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
185                       GITS_CBASER, &s->cbaser, true, &error_abort);
186 
187     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
188                       GITS_CREADR, &s->creadr, true, &error_abort);
189 
190     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
191                       GITS_CWRITER, &s->cwriter, true, &error_abort);
192 
193 
194     for (i = 0; i < 8; i++) {
195         kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
196                           GITS_BASER + i * 8, &s->baser[i], true,
197                           &error_abort);
198     }
199 
200     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
201                       KVM_DEV_ARM_ITS_RESTORE_TABLES, NULL, true,
202                       &error_abort);
203 
204     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
205                       GITS_CTLR, &s->ctlr, true, &error_abort);
206 }
207 
208 static void kvm_arm_its_class_init(ObjectClass *klass, void *data)
209 {
210     DeviceClass *dc = DEVICE_CLASS(klass);
211     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
212 
213     dc->realize = kvm_arm_its_realize;
214     icc->send_msi = kvm_its_send_msi;
215     icc->pre_save = kvm_arm_its_pre_save;
216     icc->post_load = kvm_arm_its_post_load;
217 }
218 
219 static const TypeInfo kvm_arm_its_info = {
220     .name = TYPE_KVM_ARM_ITS,
221     .parent = TYPE_ARM_GICV3_ITS_COMMON,
222     .instance_size = sizeof(GICv3ITSState),
223     .instance_init = kvm_arm_its_init,
224     .class_init = kvm_arm_its_class_init,
225 };
226 
227 static void kvm_arm_its_register_types(void)
228 {
229     type_register_static(&kvm_arm_its_info);
230 }
231 
232 type_init(kvm_arm_its_register_types)
233