1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Linaro
4 * Author: Christoffer Dall <christoffer.dall@linaro.org>
8 #include <linux/debugfs.h>
9 #include <linux/interrupt.h>
10 #include <linux/kvm_host.h>
11 #include <linux/seq_file.h>
12 #include <kvm/arm_vgic.h>
13 #include <asm/kvm_mmu.h>
17 * Structure to control looping through the entire vgic state. We start at
18 * zero for each field and move upwards. So, if dist_id is 0 we print the
19 * distributor info. When dist_id is 1, we have already printed it and move
22 * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
25 struct vgic_state_iter
{
36 static void iter_next(struct vgic_state_iter
*iter
)
38 if (iter
->dist_id
== 0) {
44 if (iter
->intid
== VGIC_NR_PRIVATE_IRQS
&&
45 ++iter
->vcpu_id
< iter
->nr_cpus
)
48 if (iter
->intid
>= (iter
->nr_spis
+ VGIC_NR_PRIVATE_IRQS
)) {
49 if (iter
->lpi_idx
< iter
->nr_lpis
)
50 iter
->intid
= iter
->lpi_array
[iter
->lpi_idx
];
55 static void iter_init(struct kvm
*kvm
, struct vgic_state_iter
*iter
,
58 int nr_cpus
= atomic_read(&kvm
->online_vcpus
);
60 memset(iter
, 0, sizeof(*iter
));
62 iter
->nr_cpus
= nr_cpus
;
63 iter
->nr_spis
= kvm
->arch
.vgic
.nr_spis
;
64 if (kvm
->arch
.vgic
.vgic_model
== KVM_DEV_TYPE_ARM_VGIC_V3
) {
65 iter
->nr_lpis
= vgic_copy_lpi_list(kvm
, NULL
, &iter
->lpi_array
);
66 if (iter
->nr_lpis
< 0)
70 /* Fast forward to the right position if needed */
75 static bool end_of_vgic(struct vgic_state_iter
*iter
)
77 return iter
->dist_id
> 0 &&
78 iter
->vcpu_id
== iter
->nr_cpus
&&
79 iter
->intid
>= (iter
->nr_spis
+ VGIC_NR_PRIVATE_IRQS
) &&
80 iter
->lpi_idx
> iter
->nr_lpis
;
83 static void *vgic_debug_start(struct seq_file
*s
, loff_t
*pos
)
85 struct kvm
*kvm
= (struct kvm
*)s
->private;
86 struct vgic_state_iter
*iter
;
88 mutex_lock(&kvm
->lock
);
89 iter
= kvm
->arch
.vgic
.iter
;
91 iter
= ERR_PTR(-EBUSY
);
95 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
97 iter
= ERR_PTR(-ENOMEM
);
101 iter_init(kvm
, iter
, *pos
);
102 kvm
->arch
.vgic
.iter
= iter
;
104 if (end_of_vgic(iter
))
107 mutex_unlock(&kvm
->lock
);
111 static void *vgic_debug_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
113 struct kvm
*kvm
= (struct kvm
*)s
->private;
114 struct vgic_state_iter
*iter
= kvm
->arch
.vgic
.iter
;
118 if (end_of_vgic(iter
))
123 static void vgic_debug_stop(struct seq_file
*s
, void *v
)
125 struct kvm
*kvm
= (struct kvm
*)s
->private;
126 struct vgic_state_iter
*iter
;
129 * If the seq file wasn't properly opened, there's nothing to clearn
135 mutex_lock(&kvm
->lock
);
136 iter
= kvm
->arch
.vgic
.iter
;
137 kfree(iter
->lpi_array
);
139 kvm
->arch
.vgic
.iter
= NULL
;
140 mutex_unlock(&kvm
->lock
);
143 static void print_dist_state(struct seq_file
*s
, struct vgic_dist
*dist
)
145 bool v3
= dist
->vgic_model
== KVM_DEV_TYPE_ARM_VGIC_V3
;
147 seq_printf(s
, "Distributor\n");
148 seq_printf(s
, "===========\n");
149 seq_printf(s
, "vgic_model:\t%s\n", v3
? "GICv3" : "GICv2");
150 seq_printf(s
, "nr_spis:\t%d\n", dist
->nr_spis
);
152 seq_printf(s
, "nr_lpis:\t%d\n", dist
->lpi_list_count
);
153 seq_printf(s
, "enabled:\t%d\n", dist
->enabled
);
156 seq_printf(s
, "P=pending_latch, L=line_level, A=active\n");
157 seq_printf(s
, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
158 seq_printf(s
, "G=group\n");
161 static void print_header(struct seq_file
*s
, struct vgic_irq
*irq
,
162 struct kvm_vcpu
*vcpu
)
173 seq_printf(s
, "%s%2d TYP ID TGT_ID PLAEHCG HWID TARGET SRC PRI VCPU_ID\n", hdr
, id
);
174 seq_printf(s
, "----------------------------------------------------------------\n");
177 static void print_irq_state(struct seq_file
*s
, struct vgic_irq
*irq
,
178 struct kvm_vcpu
*vcpu
)
181 if (irq
->intid
< VGIC_NR_SGIS
)
183 else if (irq
->intid
< VGIC_NR_PRIVATE_IRQS
)
185 else if (irq
->intid
< VGIC_MAX_SPI
)
190 if (irq
->intid
==0 || irq
->intid
== VGIC_NR_PRIVATE_IRQS
)
191 print_header(s
, irq
, vcpu
);
193 seq_printf(s
, " %s %4d "
203 (irq
->target_vcpu
) ? irq
->target_vcpu
->vcpu_id
: -1,
209 irq
->config
== VGIC_CONFIG_LEVEL
,
215 (irq
->vcpu
) ? irq
->vcpu
->vcpu_id
: -1);
218 static int vgic_debug_show(struct seq_file
*s
, void *v
)
220 struct kvm
*kvm
= (struct kvm
*)s
->private;
221 struct vgic_state_iter
*iter
= (struct vgic_state_iter
*)v
;
222 struct vgic_irq
*irq
;
223 struct kvm_vcpu
*vcpu
= NULL
;
226 if (iter
->dist_id
== 0) {
227 print_dist_state(s
, &kvm
->arch
.vgic
);
231 if (!kvm
->arch
.vgic
.initialized
)
234 if (iter
->vcpu_id
< iter
->nr_cpus
)
235 vcpu
= kvm_get_vcpu(kvm
, iter
->vcpu_id
);
237 irq
= vgic_get_irq(kvm
, vcpu
, iter
->intid
);
239 seq_printf(s
, " LPI %4d freed\n", iter
->intid
);
243 raw_spin_lock_irqsave(&irq
->irq_lock
, flags
);
244 print_irq_state(s
, irq
, vcpu
);
245 raw_spin_unlock_irqrestore(&irq
->irq_lock
, flags
);
247 vgic_put_irq(kvm
, irq
);
251 static const struct seq_operations vgic_debug_seq_ops
= {
252 .start
= vgic_debug_start
,
253 .next
= vgic_debug_next
,
254 .stop
= vgic_debug_stop
,
255 .show
= vgic_debug_show
258 static int debug_open(struct inode
*inode
, struct file
*file
)
261 ret
= seq_open(file
, &vgic_debug_seq_ops
);
263 struct seq_file
*seq
;
264 /* seq_open will have modified file->private_data */
265 seq
= file
->private_data
;
266 seq
->private = inode
->i_private
;
272 static const struct file_operations vgic_debug_fops
= {
273 .owner
= THIS_MODULE
,
277 .release
= seq_release
280 void vgic_debug_init(struct kvm
*kvm
)
282 debugfs_create_file("vgic-state", 0444, kvm
->debugfs_dentry
, kvm
,
286 void vgic_debug_destroy(struct kvm
*kvm
)