2 * Copyright (C) 2016 Linaro
3 * Author: Christoffer Dall <christoffer.dall@linaro.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/cpu.h>
19 #include <linux/debugfs.h>
20 #include <linux/interrupt.h>
21 #include <linux/kvm_host.h>
22 #include <linux/seq_file.h>
23 #include <kvm/arm_vgic.h>
24 #include <asm/kvm_mmu.h>
28 * Structure to control looping through the entire vgic state. We start at
29 * zero for each field and move upwards. So, if dist_id is 0 we print the
30 * distributor info. When dist_id is 1, we have already printed it and move
33 * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
36 struct vgic_state_iter {
47 static void iter_next(struct vgic_state_iter *iter)
49 if (iter->dist_id == 0) {
55 if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
56 ++iter->vcpu_id < iter->nr_cpus)
59 if (iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS)) {
60 if (iter->lpi_idx < iter->nr_lpis)
61 iter->intid = iter->lpi_array[iter->lpi_idx];
66 static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
69 int nr_cpus = atomic_read(&kvm->online_vcpus);
71 memset(iter, 0, sizeof(*iter));
73 iter->nr_cpus = nr_cpus;
74 iter->nr_spis = kvm->arch.vgic.nr_spis;
75 if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
76 iter->nr_lpis = vgic_copy_lpi_list(kvm, NULL, &iter->lpi_array);
77 if (iter->nr_lpis < 0)
81 /* Fast forward to the right position if needed */
86 static bool end_of_vgic(struct vgic_state_iter *iter)
88 return iter->dist_id > 0 &&
89 iter->vcpu_id == iter->nr_cpus &&
90 iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
91 iter->lpi_idx > iter->nr_lpis;
94 static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
96 struct kvm *kvm = (struct kvm *)s->private;
97 struct vgic_state_iter *iter;
99 mutex_lock(&kvm->lock);
100 iter = kvm->arch.vgic.iter;
102 iter = ERR_PTR(-EBUSY);
106 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
108 iter = ERR_PTR(-ENOMEM);
112 iter_init(kvm, iter, *pos);
113 kvm->arch.vgic.iter = iter;
115 if (end_of_vgic(iter))
118 mutex_unlock(&kvm->lock);
122 static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
124 struct kvm *kvm = (struct kvm *)s->private;
125 struct vgic_state_iter *iter = kvm->arch.vgic.iter;
129 if (end_of_vgic(iter))
134 static void vgic_debug_stop(struct seq_file *s, void *v)
136 struct kvm *kvm = (struct kvm *)s->private;
137 struct vgic_state_iter *iter;
140 * If the seq file wasn't properly opened, there's nothing to clearn
146 mutex_lock(&kvm->lock);
147 iter = kvm->arch.vgic.iter;
148 kfree(iter->lpi_array);
150 kvm->arch.vgic.iter = NULL;
151 mutex_unlock(&kvm->lock);
154 static void print_dist_state(struct seq_file *s, struct vgic_dist *dist)
156 bool v3 = dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3;
158 seq_printf(s, "Distributor\n");
159 seq_printf(s, "===========\n");
160 seq_printf(s, "vgic_model:\t%s\n", v3 ? "GICv3" : "GICv2");
161 seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
163 seq_printf(s, "nr_lpis:\t%d\n", dist->lpi_list_count);
164 seq_printf(s, "enabled:\t%d\n", dist->enabled);
167 seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
168 seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
169 seq_printf(s, "G=group\n");
172 static void print_header(struct seq_file *s, struct vgic_irq *irq,
173 struct kvm_vcpu *vcpu)
184 seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHCG HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
185 seq_printf(s, "----------------------------------------------------------------\n");
188 static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
189 struct kvm_vcpu *vcpu)
192 if (irq->intid < VGIC_NR_SGIS)
194 else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
196 else if (irq->intid < VGIC_MAX_SPI)
201 if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
202 print_header(s, irq, vcpu);
204 seq_printf(s, " %s %4d "
214 (irq->target_vcpu) ? irq->target_vcpu->vcpu_id : -1,
220 irq->config == VGIC_CONFIG_LEVEL,
226 (irq->vcpu) ? irq->vcpu->vcpu_id : -1);
229 static int vgic_debug_show(struct seq_file *s, void *v)
231 struct kvm *kvm = (struct kvm *)s->private;
232 struct vgic_state_iter *iter = (struct vgic_state_iter *)v;
233 struct vgic_irq *irq;
234 struct kvm_vcpu *vcpu = NULL;
237 if (iter->dist_id == 0) {
238 print_dist_state(s, &kvm->arch.vgic);
242 if (!kvm->arch.vgic.initialized)
245 if (iter->vcpu_id < iter->nr_cpus)
246 vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
248 irq = vgic_get_irq(kvm, vcpu, iter->intid);
250 seq_printf(s, " LPI %4d freed\n", iter->intid);
254 spin_lock_irqsave(&irq->irq_lock, flags);
255 print_irq_state(s, irq, vcpu);
256 spin_unlock_irqrestore(&irq->irq_lock, flags);
258 vgic_put_irq(kvm, irq);
262 static const struct seq_operations vgic_debug_seq_ops = {
263 .start = vgic_debug_start,
264 .next = vgic_debug_next,
265 .stop = vgic_debug_stop,
266 .show = vgic_debug_show
269 static int debug_open(struct inode *inode, struct file *file)
272 ret = seq_open(file, &vgic_debug_seq_ops);
274 struct seq_file *seq;
275 /* seq_open will have modified file->private_data */
276 seq = file->private_data;
277 seq->private = inode->i_private;
283 static const struct file_operations vgic_debug_fops = {
284 .owner = THIS_MODULE,
288 .release = seq_release
291 void vgic_debug_init(struct kvm *kvm)
293 debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm,
297 void vgic_debug_destroy(struct kvm *kvm)