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 {
44 static void iter_next(struct vgic_state_iter *iter)
46 if (iter->dist_id == 0) {
52 if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
53 ++iter->vcpu_id < iter->nr_cpus)
57 static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
60 int nr_cpus = atomic_read(&kvm->online_vcpus);
62 memset(iter, 0, sizeof(*iter));
64 iter->nr_cpus = nr_cpus;
65 iter->nr_spis = kvm->arch.vgic.nr_spis;
67 /* Fast forward to the right position if needed */
72 static bool end_of_vgic(struct vgic_state_iter *iter)
74 return iter->dist_id > 0 &&
75 iter->vcpu_id == iter->nr_cpus &&
76 (iter->intid - VGIC_NR_PRIVATE_IRQS) == iter->nr_spis;
79 static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
81 struct kvm *kvm = (struct kvm *)s->private;
82 struct vgic_state_iter *iter;
84 mutex_lock(&kvm->lock);
85 iter = kvm->arch.vgic.iter;
87 iter = ERR_PTR(-EBUSY);
91 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
93 iter = ERR_PTR(-ENOMEM);
97 iter_init(kvm, iter, *pos);
98 kvm->arch.vgic.iter = iter;
100 if (end_of_vgic(iter))
103 mutex_unlock(&kvm->lock);
107 static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
109 struct kvm *kvm = (struct kvm *)s->private;
110 struct vgic_state_iter *iter = kvm->arch.vgic.iter;
114 if (end_of_vgic(iter))
119 static void vgic_debug_stop(struct seq_file *s, void *v)
121 struct kvm *kvm = (struct kvm *)s->private;
122 struct vgic_state_iter *iter;
125 * If the seq file wasn't properly opened, there's nothing to clearn
131 mutex_lock(&kvm->lock);
132 iter = kvm->arch.vgic.iter;
134 kvm->arch.vgic.iter = NULL;
135 mutex_unlock(&kvm->lock);
138 static void print_dist_state(struct seq_file *s, struct vgic_dist *dist)
140 seq_printf(s, "Distributor\n");
141 seq_printf(s, "===========\n");
142 seq_printf(s, "vgic_model:\t%s\n",
143 (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) ?
145 seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
146 seq_printf(s, "enabled:\t%d\n", dist->enabled);
149 seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
150 seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
153 static void print_header(struct seq_file *s, struct vgic_irq *irq,
154 struct kvm_vcpu *vcpu)
165 seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHC HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
166 seq_printf(s, "---------------------------------------------------------------\n");
169 static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
170 struct kvm_vcpu *vcpu)
173 if (irq->intid < VGIC_NR_SGIS)
175 else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
180 if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
181 print_header(s, irq, vcpu);
183 seq_printf(s, " %s %4d "
193 (irq->target_vcpu) ? irq->target_vcpu->vcpu_id : -1,
199 irq->config == VGIC_CONFIG_LEVEL,
204 (irq->vcpu) ? irq->vcpu->vcpu_id : -1);
208 static int vgic_debug_show(struct seq_file *s, void *v)
210 struct kvm *kvm = (struct kvm *)s->private;
211 struct vgic_state_iter *iter = (struct vgic_state_iter *)v;
212 struct vgic_irq *irq;
213 struct kvm_vcpu *vcpu = NULL;
215 if (iter->dist_id == 0) {
216 print_dist_state(s, &kvm->arch.vgic);
220 if (!kvm->arch.vgic.initialized)
223 if (iter->vcpu_id < iter->nr_cpus) {
224 vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
225 irq = &vcpu->arch.vgic_cpu.private_irqs[iter->intid];
227 irq = &kvm->arch.vgic.spis[iter->intid - VGIC_NR_PRIVATE_IRQS];
230 spin_lock(&irq->irq_lock);
231 print_irq_state(s, irq, vcpu);
232 spin_unlock(&irq->irq_lock);
237 static const struct seq_operations vgic_debug_seq_ops = {
238 .start = vgic_debug_start,
239 .next = vgic_debug_next,
240 .stop = vgic_debug_stop,
241 .show = vgic_debug_show
244 static int debug_open(struct inode *inode, struct file *file)
247 ret = seq_open(file, &vgic_debug_seq_ops);
249 struct seq_file *seq;
250 /* seq_open will have modified file->private_data */
251 seq = file->private_data;
252 seq->private = inode->i_private;
258 static const struct file_operations vgic_debug_fops = {
259 .owner = THIS_MODULE,
263 .release = seq_release
266 int vgic_debug_init(struct kvm *kvm)
268 if (!kvm->debugfs_dentry)
271 if (!debugfs_create_file("vgic-state", 0444,
280 int vgic_debug_destroy(struct kvm *kvm)