Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 Linaro
0004  * Author: Christoffer Dall <christoffer.dall@linaro.org>
0005  */
0006 
0007 #include <linux/cpu.h>
0008 #include <linux/debugfs.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/kvm_host.h>
0011 #include <linux/seq_file.h>
0012 #include <kvm/arm_vgic.h>
0013 #include <asm/kvm_mmu.h>
0014 #include "vgic.h"
0015 
0016 /*
0017  * Structure to control looping through the entire vgic state.  We start at
0018  * zero for each field and move upwards.  So, if dist_id is 0 we print the
0019  * distributor info.  When dist_id is 1, we have already printed it and move
0020  * on.
0021  *
0022  * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
0023  * so on.
0024  */
0025 struct vgic_state_iter {
0026     int nr_cpus;
0027     int nr_spis;
0028     int nr_lpis;
0029     int dist_id;
0030     int vcpu_id;
0031     int intid;
0032     int lpi_idx;
0033     u32 *lpi_array;
0034 };
0035 
0036 static void iter_next(struct vgic_state_iter *iter)
0037 {
0038     if (iter->dist_id == 0) {
0039         iter->dist_id++;
0040         return;
0041     }
0042 
0043     iter->intid++;
0044     if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
0045         ++iter->vcpu_id < iter->nr_cpus)
0046         iter->intid = 0;
0047 
0048     if (iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS)) {
0049         if (iter->lpi_idx < iter->nr_lpis)
0050             iter->intid = iter->lpi_array[iter->lpi_idx];
0051         iter->lpi_idx++;
0052     }
0053 }
0054 
0055 static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
0056               loff_t pos)
0057 {
0058     int nr_cpus = atomic_read(&kvm->online_vcpus);
0059 
0060     memset(iter, 0, sizeof(*iter));
0061 
0062     iter->nr_cpus = nr_cpus;
0063     iter->nr_spis = kvm->arch.vgic.nr_spis;
0064     if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
0065         iter->nr_lpis = vgic_copy_lpi_list(kvm, NULL, &iter->lpi_array);
0066         if (iter->nr_lpis < 0)
0067             iter->nr_lpis = 0;
0068     }
0069 
0070     /* Fast forward to the right position if needed */
0071     while (pos--)
0072         iter_next(iter);
0073 }
0074 
0075 static bool end_of_vgic(struct vgic_state_iter *iter)
0076 {
0077     return iter->dist_id > 0 &&
0078         iter->vcpu_id == iter->nr_cpus &&
0079         iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
0080         iter->lpi_idx > iter->nr_lpis;
0081 }
0082 
0083 static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
0084 {
0085     struct kvm *kvm = s->private;
0086     struct vgic_state_iter *iter;
0087 
0088     mutex_lock(&kvm->lock);
0089     iter = kvm->arch.vgic.iter;
0090     if (iter) {
0091         iter = ERR_PTR(-EBUSY);
0092         goto out;
0093     }
0094 
0095     iter = kmalloc(sizeof(*iter), GFP_KERNEL);
0096     if (!iter) {
0097         iter = ERR_PTR(-ENOMEM);
0098         goto out;
0099     }
0100 
0101     iter_init(kvm, iter, *pos);
0102     kvm->arch.vgic.iter = iter;
0103 
0104     if (end_of_vgic(iter))
0105         iter = NULL;
0106 out:
0107     mutex_unlock(&kvm->lock);
0108     return iter;
0109 }
0110 
0111 static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
0112 {
0113     struct kvm *kvm = s->private;
0114     struct vgic_state_iter *iter = kvm->arch.vgic.iter;
0115 
0116     ++*pos;
0117     iter_next(iter);
0118     if (end_of_vgic(iter))
0119         iter = NULL;
0120     return iter;
0121 }
0122 
0123 static void vgic_debug_stop(struct seq_file *s, void *v)
0124 {
0125     struct kvm *kvm = s->private;
0126     struct vgic_state_iter *iter;
0127 
0128     /*
0129      * If the seq file wasn't properly opened, there's nothing to clearn
0130      * up.
0131      */
0132     if (IS_ERR(v))
0133         return;
0134 
0135     mutex_lock(&kvm->lock);
0136     iter = kvm->arch.vgic.iter;
0137     kfree(iter->lpi_array);
0138     kfree(iter);
0139     kvm->arch.vgic.iter = NULL;
0140     mutex_unlock(&kvm->lock);
0141 }
0142 
0143 static void print_dist_state(struct seq_file *s, struct vgic_dist *dist)
0144 {
0145     bool v3 = dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3;
0146 
0147     seq_printf(s, "Distributor\n");
0148     seq_printf(s, "===========\n");
0149     seq_printf(s, "vgic_model:\t%s\n", v3 ? "GICv3" : "GICv2");
0150     seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
0151     if (v3)
0152         seq_printf(s, "nr_lpis:\t%d\n", dist->lpi_list_count);
0153     seq_printf(s, "enabled:\t%d\n", dist->enabled);
0154     seq_printf(s, "\n");
0155 
0156     seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
0157     seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
0158     seq_printf(s, "G=group\n");
0159 }
0160 
0161 static void print_header(struct seq_file *s, struct vgic_irq *irq,
0162              struct kvm_vcpu *vcpu)
0163 {
0164     int id = 0;
0165     char *hdr = "SPI ";
0166 
0167     if (vcpu) {
0168         hdr = "VCPU";
0169         id = vcpu->vcpu_id;
0170     }
0171 
0172     seq_printf(s, "\n");
0173     seq_printf(s, "%s%2d TYP   ID TGT_ID PLAEHCG     HWID   TARGET SRC PRI VCPU_ID\n", hdr, id);
0174     seq_printf(s, "----------------------------------------------------------------\n");
0175 }
0176 
0177 static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
0178                 struct kvm_vcpu *vcpu)
0179 {
0180     char *type;
0181     bool pending;
0182 
0183     if (irq->intid < VGIC_NR_SGIS)
0184         type = "SGI";
0185     else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
0186         type = "PPI";
0187     else if (irq->intid < VGIC_MAX_SPI)
0188         type = "SPI";
0189     else
0190         type = "LPI";
0191 
0192     if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
0193         print_header(s, irq, vcpu);
0194 
0195     pending = irq->pending_latch;
0196     if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
0197         int err;
0198 
0199         err = irq_get_irqchip_state(irq->host_irq,
0200                         IRQCHIP_STATE_PENDING,
0201                         &pending);
0202         WARN_ON_ONCE(err);
0203     }
0204 
0205     seq_printf(s, "       %s %4d "
0206               "    %2d "
0207               "%d%d%d%d%d%d%d "
0208               "%8d "
0209               "%8x "
0210               " %2x "
0211               "%3d "
0212               "     %2d "
0213               "\n",
0214             type, irq->intid,
0215             (irq->target_vcpu) ? irq->target_vcpu->vcpu_id : -1,
0216             pending,
0217             irq->line_level,
0218             irq->active,
0219             irq->enabled,
0220             irq->hw,
0221             irq->config == VGIC_CONFIG_LEVEL,
0222             irq->group,
0223             irq->hwintid,
0224             irq->mpidr,
0225             irq->source,
0226             irq->priority,
0227             (irq->vcpu) ? irq->vcpu->vcpu_id : -1);
0228 }
0229 
0230 static int vgic_debug_show(struct seq_file *s, void *v)
0231 {
0232     struct kvm *kvm = s->private;
0233     struct vgic_state_iter *iter = v;
0234     struct vgic_irq *irq;
0235     struct kvm_vcpu *vcpu = NULL;
0236     unsigned long flags;
0237 
0238     if (iter->dist_id == 0) {
0239         print_dist_state(s, &kvm->arch.vgic);
0240         return 0;
0241     }
0242 
0243     if (!kvm->arch.vgic.initialized)
0244         return 0;
0245 
0246     if (iter->vcpu_id < iter->nr_cpus)
0247         vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
0248 
0249     irq = vgic_get_irq(kvm, vcpu, iter->intid);
0250     if (!irq) {
0251         seq_printf(s, "       LPI %4d freed\n", iter->intid);
0252         return 0;
0253     }
0254 
0255     raw_spin_lock_irqsave(&irq->irq_lock, flags);
0256     print_irq_state(s, irq, vcpu);
0257     raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
0258 
0259     vgic_put_irq(kvm, irq);
0260     return 0;
0261 }
0262 
0263 static const struct seq_operations vgic_debug_sops = {
0264     .start = vgic_debug_start,
0265     .next  = vgic_debug_next,
0266     .stop  = vgic_debug_stop,
0267     .show  = vgic_debug_show
0268 };
0269 
0270 DEFINE_SEQ_ATTRIBUTE(vgic_debug);
0271 
0272 void vgic_debug_init(struct kvm *kvm)
0273 {
0274     debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm,
0275                 &vgic_debug_fops);
0276 }
0277 
0278 void vgic_debug_destroy(struct kvm *kvm)
0279 {
0280 }