xref: /openbmc/qemu/hw/intc/arm_gicv3_its_kvm.c (revision 795c40b8)
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 static void kvm_arm_its_realize(DeviceState *dev, Error **errp)
57 {
58     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
59     Error *local_err = NULL;
60 
61     /*
62      * Block migration of a KVM GICv3 ITS device: the API for saving and
63      * restoring the state in the kernel is not yet available
64      */
65     error_setg(&s->migration_blocker, "vITS migration is not implemented");
66     migrate_add_blocker(s->migration_blocker, &local_err);
67     if (local_err) {
68         error_propagate(errp, local_err);
69         error_free(s->migration_blocker);
70         return;
71     }
72 
73     s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_ITS, false);
74     if (s->dev_fd < 0) {
75         error_setg_errno(errp, -s->dev_fd, "error creating in-kernel ITS");
76         return;
77     }
78 
79     /* explicit init of the ITS */
80     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
81                       KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true);
82 
83     /* register the base address */
84     kvm_arm_register_device(&s->iomem_its_cntrl, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
85                             KVM_VGIC_ITS_ADDR_TYPE, s->dev_fd);
86 
87     gicv3_its_init_mmio(s, NULL);
88 
89     kvm_msi_use_devid = true;
90     kvm_gsi_direct_mapping = false;
91     kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
92 }
93 
94 static void kvm_arm_its_init(Object *obj)
95 {
96     GICv3ITSState *s = KVM_ARM_ITS(obj);
97 
98     object_property_add_link(obj, "parent-gicv3",
99                              "kvm-arm-gicv3", (Object **)&s->gicv3,
100                              object_property_allow_set_link,
101                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
102                              &error_abort);
103 }
104 
105 static void kvm_arm_its_class_init(ObjectClass *klass, void *data)
106 {
107     DeviceClass *dc = DEVICE_CLASS(klass);
108     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
109 
110     dc->realize = kvm_arm_its_realize;
111     icc->send_msi = kvm_its_send_msi;
112 }
113 
114 static const TypeInfo kvm_arm_its_info = {
115     .name = TYPE_KVM_ARM_ITS,
116     .parent = TYPE_ARM_GICV3_ITS_COMMON,
117     .instance_size = sizeof(GICv3ITSState),
118     .instance_init = kvm_arm_its_init,
119     .class_init = kvm_arm_its_class_init,
120 };
121 
122 static void kvm_arm_its_register_types(void)
123 {
124     type_register_static(&kvm_arm_its_info);
125 }
126 
127 type_init(kvm_arm_its_register_types)
128