Skip to content

Commit 503a628

Browse files
Julien Grallchazy
authored andcommitted
KVM: arm/arm64: vgic: Rely on the GIC driver to parse the firmware tables
Currently, the firmware tables are parsed 2 times: once in the GIC drivers, the other time when initializing the vGIC. It means code duplication and make more tedious to add the support for another firmware table (like ACPI). Use the recently introduced helper gic_get_kvm_info() to get information about the virtual GIC. With this change, the virtual GIC becomes agnostic to the firmware table and KVM will be able to initialize the vGIC on ACPI. Signed-off-by: Julien Grall <julien.grall@arm.com> Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
1 parent 29c2d6f commit 503a628

4 files changed

Lines changed: 73 additions & 92 deletions

File tree

‎include/kvm/arm_vgic.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/spinlock.h>
2626
#include <linux/types.h>
2727
#include <kvm/iodev.h>
28+
#include <linux/irqchip/arm-gic-common.h>
2829

2930
#define VGIC_NR_IRQS_LEGACY 256
3031
#define VGIC_NR_SGIS 16
@@ -353,15 +354,15 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, struct irq_phys_map *map);
353354
#define vgic_initialized(k) (!!((k)->arch.vgic.nr_cpus))
354355
#define vgic_ready(k) ((k)->arch.vgic.ready)
355356

356-
int vgic_v2_probe(struct device_node *vgic_node,
357+
int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info,
357358
const struct vgic_ops **ops,
358359
const struct vgic_params **params);
359360
#ifdef CONFIG_KVM_ARM_VGIC_V3
360-
int vgic_v3_probe(struct device_node *vgic_node,
361+
int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info,
361362
const struct vgic_ops **ops,
362363
const struct vgic_params **params);
363364
#else
364-
static inline int vgic_v3_probe(struct device_node *vgic_node,
365+
static inline int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info,
365366
const struct vgic_ops **ops,
366367
const struct vgic_params **params)
367368
{

‎virt/kvm/arm/vgic-v2.c‎

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
#include <linux/kvm_host.h>
2121
#include <linux/interrupt.h>
2222
#include <linux/io.h>
23-
#include <linux/of.h>
24-
#include <linux/of_address.h>
25-
#include <linux/of_irq.h>
2623

2724
#include <linux/irqchip/arm-gic.h>
2825

@@ -186,38 +183,39 @@ static void vgic_cpu_init_lrs(void *params)
186183
}
187184

188185
/**
189-
* vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
190-
* @node: pointer to the DT node
191-
* @ops: address of a pointer to the GICv2 operations
192-
* @params: address of a pointer to HW-specific parameters
186+
* vgic_v2_probe - probe for a GICv2 compatible interrupt controller
187+
* @gic_kvm_info: pointer to the GIC description
188+
* @ops: address of a pointer to the GICv2 operations
189+
* @params: address of a pointer to HW-specific parameters
193190
*
194191
* Returns 0 if a GICv2 has been found, with the low level operations
195192
* in *ops and the HW parameters in *params. Returns an error code
196193
* otherwise.
197194
*/
198-
int vgic_v2_probe(struct device_node *vgic_node,
199-
const struct vgic_ops **ops,
200-
const struct vgic_params **params)
195+
int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info,
196+
const struct vgic_ops **ops,
197+
const struct vgic_params **params)
201198
{
202199
int ret;
203-
struct resource vctrl_res;
204-
struct resource vcpu_res;
205200
struct vgic_params *vgic = &vgic_v2_params;
201+
const struct resource *vctrl_res = &gic_kvm_info->vctrl;
202+
const struct resource *vcpu_res = &gic_kvm_info->vcpu;
206203

207-
vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
208-
if (!vgic->maint_irq) {
209-
kvm_err("error getting vgic maintenance irq from DT\n");
204+
if (!gic_kvm_info->maint_irq) {
205+
kvm_err("error getting vgic maintenance irq\n");
210206
ret = -ENXIO;
211207
goto out;
212208
}
209+
vgic->maint_irq = gic_kvm_info->maint_irq;
213210

214-
ret = of_address_to_resource(vgic_node, 2, &vctrl_res);
215-
if (ret) {
216-
kvm_err("Cannot obtain GICH resource\n");
211+
if (!gic_kvm_info->vctrl.start) {
212+
kvm_err("GICH not present in the firmware table\n");
213+
ret = -ENXIO;
217214
goto out;
218215
}
219216

220-
vgic->vctrl_base = of_iomap(vgic_node, 2);
217+
vgic->vctrl_base = ioremap(gic_kvm_info->vctrl.start,
218+
resource_size(&gic_kvm_info->vctrl));
221219
if (!vgic->vctrl_base) {
222220
kvm_err("Cannot ioremap GICH\n");
223221
ret = -ENOMEM;
@@ -228,29 +226,23 @@ int vgic_v2_probe(struct device_node *vgic_node,
228226
vgic->nr_lr = (vgic->nr_lr & 0x3f) + 1;
229227

230228
ret = create_hyp_io_mappings(vgic->vctrl_base,
231-
vgic->vctrl_base + resource_size(&vctrl_res),
232-
vctrl_res.start);
229+
vgic->vctrl_base + resource_size(vctrl_res),
230+
vctrl_res->start);
233231
if (ret) {
234232
kvm_err("Cannot map VCTRL into hyp\n");
235233
goto out_unmap;
236234
}
237235

238-
if (of_address_to_resource(vgic_node, 3, &vcpu_res)) {
239-
kvm_err("Cannot obtain GICV resource\n");
240-
ret = -ENXIO;
241-
goto out_unmap;
242-
}
243-
244-
if (!PAGE_ALIGNED(vcpu_res.start)) {
236+
if (!PAGE_ALIGNED(vcpu_res->start)) {
245237
kvm_err("GICV physical address 0x%llx not page aligned\n",
246-
(unsigned long long)vcpu_res.start);
238+
(unsigned long long)vcpu_res->start);
247239
ret = -ENXIO;
248240
goto out_unmap;
249241
}
250242

251-
if (!PAGE_ALIGNED(resource_size(&vcpu_res))) {
243+
if (!PAGE_ALIGNED(resource_size(vcpu_res))) {
252244
kvm_err("GICV size 0x%llx not a multiple of page size 0x%lx\n",
253-
(unsigned long long)resource_size(&vcpu_res),
245+
(unsigned long long)resource_size(vcpu_res),
254246
PAGE_SIZE);
255247
ret = -ENXIO;
256248
goto out_unmap;
@@ -259,10 +251,10 @@ int vgic_v2_probe(struct device_node *vgic_node,
259251
vgic->can_emulate_gicv2 = true;
260252
kvm_register_device_ops(&kvm_arm_vgic_v2_ops, KVM_DEV_TYPE_ARM_VGIC_V2);
261253

262-
vgic->vcpu_base = vcpu_res.start;
254+
vgic->vcpu_base = vcpu_res->start;
263255

264-
kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
265-
vctrl_res.start, vgic->maint_irq);
256+
kvm_info("GICH base=0x%llx, GICV base=0x%llx, IRQ=%d\n",
257+
gic_kvm_info->vctrl.start, vgic->vcpu_base, vgic->maint_irq);
266258

267259
vgic->type = VGIC_V2;
268260
vgic->max_gic_vcpus = VGIC_V2_MAX_CPUS;
@@ -276,6 +268,5 @@ int vgic_v2_probe(struct device_node *vgic_node,
276268
out_unmap:
277269
iounmap(vgic->vctrl_base);
278270
out:
279-
of_node_put(vgic_node);
280271
return ret;
281272
}

‎virt/kvm/arm/vgic-v3.c‎

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
#include <linux/kvm_host.h>
2121
#include <linux/interrupt.h>
2222
#include <linux/io.h>
23-
#include <linux/of.h>
24-
#include <linux/of_address.h>
25-
#include <linux/of_irq.h>
2623

2724
#include <linux/irqchip/arm-gic-v3.h>
25+
#include <linux/irqchip/arm-gic-common.h>
2826

2927
#include <asm/kvm_emulate.h>
3028
#include <asm/kvm_arm.h>
@@ -222,30 +220,24 @@ static void vgic_cpu_init_lrs(void *params)
222220
}
223221

224222
/**
225-
* vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
226-
* @node: pointer to the DT node
227-
* @ops: address of a pointer to the GICv3 operations
228-
* @params: address of a pointer to HW-specific parameters
223+
* vgic_v3_probe - probe for a GICv3 compatible interrupt controller
224+
* @gic_kvm_info: pointer to the GIC description
225+
* @ops: address of a pointer to the GICv3 operations
226+
* @params: address of a pointer to HW-specific parameters
229227
*
230228
* Returns 0 if a GICv3 has been found, with the low level operations
231229
* in *ops and the HW parameters in *params. Returns an error code
232230
* otherwise.
233231
*/
234-
int vgic_v3_probe(struct device_node *vgic_node,
232+
int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info,
235233
const struct vgic_ops **ops,
236234
const struct vgic_params **params)
237235
{
238236
int ret = 0;
239-
u32 gicv_idx;
240-
struct resource vcpu_res;
241237
struct vgic_params *vgic = &vgic_v3_params;
238+
const struct resource *vcpu_res = &gic_kvm_info->vcpu;
242239

243-
vgic->maint_irq = irq_of_parse_and_map(vgic_node, 0);
244-
if (!vgic->maint_irq) {
245-
kvm_err("error getting vgic maintenance irq from DT\n");
246-
ret = -ENXIO;
247-
goto out;
248-
}
240+
vgic->maint_irq = gic_kvm_info->maint_irq;
249241

250242
ich_vtr_el2 = kvm_call_hyp(__vgic_v3_get_ich_vtr_el2);
251243

@@ -256,24 +248,19 @@ int vgic_v3_probe(struct device_node *vgic_node,
256248
vgic->nr_lr = (ich_vtr_el2 & 0xf) + 1;
257249
vgic->can_emulate_gicv2 = false;
258250

259-
if (of_property_read_u32(vgic_node, "#redistributor-regions", &gicv_idx))
260-
gicv_idx = 1;
261-
262-
gicv_idx += 3; /* Also skip GICD, GICC, GICH */
263-
if (of_address_to_resource(vgic_node, gicv_idx, &vcpu_res)) {
251+
if (!vcpu_res->start) {
264252
kvm_info("GICv3: no GICV resource entry\n");
265253
vgic->vcpu_base = 0;
266-
} else if (!PAGE_ALIGNED(vcpu_res.start)) {
254+
} else if (!PAGE_ALIGNED(vcpu_res->start)) {
267255
pr_warn("GICV physical address 0x%llx not page aligned\n",
268-
(unsigned long long)vcpu_res.start);
256+
(unsigned long long)vcpu_res->start);
269257
vgic->vcpu_base = 0;
270-
} else if (!PAGE_ALIGNED(resource_size(&vcpu_res))) {
258+
} else if (!PAGE_ALIGNED(resource_size(vcpu_res))) {
271259
pr_warn("GICV size 0x%llx not a multiple of page size 0x%lx\n",
272-
(unsigned long long)resource_size(&vcpu_res),
260+
(unsigned long long)resource_size(vcpu_res),
273261
PAGE_SIZE);
274-
vgic->vcpu_base = 0;
275262
} else {
276-
vgic->vcpu_base = vcpu_res.start;
263+
vgic->vcpu_base = vcpu_res->start;
277264
vgic->can_emulate_gicv2 = true;
278265
kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
279266
KVM_DEV_TYPE_ARM_VGIC_V2);
@@ -286,15 +273,13 @@ int vgic_v3_probe(struct device_node *vgic_node,
286273
vgic->type = VGIC_V3;
287274
vgic->max_gic_vcpus = VGIC_V3_MAX_CPUS;
288275

289-
kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
290-
vcpu_res.start, vgic->maint_irq);
276+
kvm_info("GICV base=0x%llx, IRQ=%d\n",
277+
vgic->vcpu_base, vgic->maint_irq);
291278

292279
on_each_cpu(vgic_cpu_init_lrs, vgic, 1);
293280

294281
*ops = &vgic_v3_ops;
295282
*params = vgic;
296283

297-
out:
298-
of_node_put(vgic_node);
299284
return ret;
300285
}

‎virt/kvm/arm/vgic.c‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
#include <linux/kvm_host.h>
2222
#include <linux/interrupt.h>
2323
#include <linux/io.h>
24-
#include <linux/of.h>
25-
#include <linux/of_address.h>
26-
#include <linux/of_irq.h>
24+
#include <linux/irq.h>
2725
#include <linux/rculist.h>
2826
#include <linux/uaccess.h>
2927

@@ -33,6 +31,7 @@
3331
#include <trace/events/kvm.h>
3432
#include <asm/kvm.h>
3533
#include <kvm/iodev.h>
34+
#include <linux/irqchip/arm-gic-common.h>
3635

3736
#define CREATE_TRACE_POINTS
3837
#include "trace.h"
@@ -2389,33 +2388,38 @@ static struct notifier_block vgic_cpu_nb = {
23892388
.notifier_call = vgic_cpu_notify,
23902389
};
23912390

2392-
static const struct of_device_id vgic_ids[] = {
2393-
{ .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
2394-
{ .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
2395-
{ .compatible = "arm,gic-400", .data = vgic_v2_probe, },
2396-
{ .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
2397-
{},
2398-
};
2399-
2400-
int kvm_vgic_hyp_init(void)
2391+
static int kvm_vgic_probe(void)
24012392
{
2402-
const struct of_device_id *matched_id;
2403-
const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
2404-
const struct vgic_params **);
2405-
struct device_node *vgic_node;
2393+
const struct gic_kvm_info *gic_kvm_info;
24062394
int ret;
24072395

2408-
vgic_node = of_find_matching_node_and_match(NULL,
2409-
vgic_ids, &matched_id);
2410-
if (!vgic_node) {
2411-
kvm_err("error: no compatible GIC node found\n");
2396+
gic_kvm_info = gic_get_kvm_info();
2397+
if (!gic_kvm_info)
24122398
return -ENODEV;
2399+
2400+
switch (gic_kvm_info->type) {
2401+
case GIC_V2:
2402+
ret = vgic_v2_probe(gic_kvm_info, &vgic_ops, &vgic);
2403+
break;
2404+
case GIC_V3:
2405+
ret = vgic_v3_probe(gic_kvm_info, &vgic_ops, &vgic);
2406+
break;
2407+
default:
2408+
ret = -ENODEV;
24132409
}
24142410

2415-
vgic_probe = matched_id->data;
2416-
ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
2417-
if (ret)
2411+
return ret;
2412+
}
2413+
2414+
int kvm_vgic_hyp_init(void)
2415+
{
2416+
int ret;
2417+
2418+
ret = kvm_vgic_probe();
2419+
if (ret) {
2420+
kvm_err("error: KVM vGIC probing failed\n");
24182421
return ret;
2422+
}
24192423

24202424
ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
24212425
"vgic", kvm_get_running_vcpus());

0 commit comments

Comments
 (0)