vgic-debug.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (C) 2016 Linaro
  3. * Author: Christoffer Dall <christoffer.dall@linaro.org>
  4. *
  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.
  8. *
  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.
  13. *
  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/>.
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/seq_file.h>
  22. #include <kvm/arm_vgic.h>
  23. #include <asm/kvm_mmu.h>
  24. #include "vgic.h"
  25. /*
  26. * Structure to control looping through the entire vgic state. We start at
  27. * zero for each field and move upwards. So, if dist_id is 0 we print the
  28. * distributor info. When dist_id is 1, we have already printed it and move
  29. * on.
  30. *
  31. * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
  32. * so on.
  33. */
  34. struct vgic_state_iter {
  35. int nr_cpus;
  36. int nr_spis;
  37. int nr_lpis;
  38. int dist_id;
  39. int vcpu_id;
  40. int intid;
  41. int lpi_idx;
  42. u32 *lpi_array;
  43. };
  44. static void iter_next(struct vgic_state_iter *iter)
  45. {
  46. if (iter->dist_id == 0) {
  47. iter->dist_id++;
  48. return;
  49. }
  50. iter->intid++;
  51. if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
  52. ++iter->vcpu_id < iter->nr_cpus)
  53. iter->intid = 0;
  54. if (iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS)) {
  55. if (iter->lpi_idx < iter->nr_lpis)
  56. iter->intid = iter->lpi_array[iter->lpi_idx];
  57. iter->lpi_idx++;
  58. }
  59. }
  60. static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
  61. loff_t pos)
  62. {
  63. int nr_cpus = atomic_read(&kvm->online_vcpus);
  64. memset(iter, 0, sizeof(*iter));
  65. iter->nr_cpus = nr_cpus;
  66. iter->nr_spis = kvm->arch.vgic.nr_spis;
  67. if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  68. iter->nr_lpis = vgic_copy_lpi_list(kvm, NULL, &iter->lpi_array);
  69. if (iter->nr_lpis < 0)
  70. iter->nr_lpis = 0;
  71. }
  72. /* Fast forward to the right position if needed */
  73. while (pos--)
  74. iter_next(iter);
  75. }
  76. static bool end_of_vgic(struct vgic_state_iter *iter)
  77. {
  78. return iter->dist_id > 0 &&
  79. iter->vcpu_id == iter->nr_cpus &&
  80. iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
  81. iter->lpi_idx > iter->nr_lpis;
  82. }
  83. static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
  84. {
  85. struct kvm *kvm = (struct kvm *)s->private;
  86. struct vgic_state_iter *iter;
  87. mutex_lock(&kvm->lock);
  88. iter = kvm->arch.vgic.iter;
  89. if (iter) {
  90. iter = ERR_PTR(-EBUSY);
  91. goto out;
  92. }
  93. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  94. if (!iter) {
  95. iter = ERR_PTR(-ENOMEM);
  96. goto out;
  97. }
  98. iter_init(kvm, iter, *pos);
  99. kvm->arch.vgic.iter = iter;
  100. if (end_of_vgic(iter))
  101. iter = NULL;
  102. out:
  103. mutex_unlock(&kvm->lock);
  104. return iter;
  105. }
  106. static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
  107. {
  108. struct kvm *kvm = (struct kvm *)s->private;
  109. struct vgic_state_iter *iter = kvm->arch.vgic.iter;
  110. ++*pos;
  111. iter_next(iter);
  112. if (end_of_vgic(iter))
  113. iter = NULL;
  114. return iter;
  115. }
  116. static void vgic_debug_stop(struct seq_file *s, void *v)
  117. {
  118. struct kvm *kvm = (struct kvm *)s->private;
  119. struct vgic_state_iter *iter;
  120. /*
  121. * If the seq file wasn't properly opened, there's nothing to clearn
  122. * up.
  123. */
  124. if (IS_ERR(v))
  125. return;
  126. mutex_lock(&kvm->lock);
  127. iter = kvm->arch.vgic.iter;
  128. kfree(iter->lpi_array);
  129. kfree(iter);
  130. kvm->arch.vgic.iter = NULL;
  131. mutex_unlock(&kvm->lock);
  132. }
  133. static void print_dist_state(struct seq_file *s, struct vgic_dist *dist)
  134. {
  135. bool v3 = dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3;
  136. seq_printf(s, "Distributor\n");
  137. seq_printf(s, "===========\n");
  138. seq_printf(s, "vgic_model:\t%s\n", v3 ? "GICv3" : "GICv2");
  139. seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
  140. if (v3)
  141. seq_printf(s, "nr_lpis:\t%d\n", dist->lpi_list_count);
  142. seq_printf(s, "enabled:\t%d\n", dist->enabled);
  143. seq_printf(s, "\n");
  144. seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
  145. seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
  146. seq_printf(s, "G=group\n");
  147. }
  148. static void print_header(struct seq_file *s, struct vgic_irq *irq,
  149. struct kvm_vcpu *vcpu)
  150. {
  151. int id = 0;
  152. char *hdr = "SPI ";
  153. if (vcpu) {
  154. hdr = "VCPU";
  155. id = vcpu->vcpu_id;
  156. }
  157. seq_printf(s, "\n");
  158. seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHCG HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
  159. seq_printf(s, "----------------------------------------------------------------\n");
  160. }
  161. static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
  162. struct kvm_vcpu *vcpu)
  163. {
  164. char *type;
  165. if (irq->intid < VGIC_NR_SGIS)
  166. type = "SGI";
  167. else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
  168. type = "PPI";
  169. else if (irq->intid < VGIC_MAX_SPI)
  170. type = "SPI";
  171. else
  172. type = "LPI";
  173. if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
  174. print_header(s, irq, vcpu);
  175. seq_printf(s, " %s %4d "
  176. " %2d "
  177. "%d%d%d%d%d%d%d "
  178. "%8d "
  179. "%8x "
  180. " %2x "
  181. "%3d "
  182. " %2d "
  183. "\n",
  184. type, irq->intid,
  185. (irq->target_vcpu) ? irq->target_vcpu->vcpu_id : -1,
  186. irq->pending_latch,
  187. irq->line_level,
  188. irq->active,
  189. irq->enabled,
  190. irq->hw,
  191. irq->config == VGIC_CONFIG_LEVEL,
  192. irq->group,
  193. irq->hwintid,
  194. irq->mpidr,
  195. irq->source,
  196. irq->priority,
  197. (irq->vcpu) ? irq->vcpu->vcpu_id : -1);
  198. }
  199. static int vgic_debug_show(struct seq_file *s, void *v)
  200. {
  201. struct kvm *kvm = (struct kvm *)s->private;
  202. struct vgic_state_iter *iter = (struct vgic_state_iter *)v;
  203. struct vgic_irq *irq;
  204. struct kvm_vcpu *vcpu = NULL;
  205. unsigned long flags;
  206. if (iter->dist_id == 0) {
  207. print_dist_state(s, &kvm->arch.vgic);
  208. return 0;
  209. }
  210. if (!kvm->arch.vgic.initialized)
  211. return 0;
  212. if (iter->vcpu_id < iter->nr_cpus)
  213. vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
  214. irq = vgic_get_irq(kvm, vcpu, iter->intid);
  215. if (!irq) {
  216. seq_printf(s, " LPI %4d freed\n", iter->intid);
  217. return 0;
  218. }
  219. spin_lock_irqsave(&irq->irq_lock, flags);
  220. print_irq_state(s, irq, vcpu);
  221. spin_unlock_irqrestore(&irq->irq_lock, flags);
  222. vgic_put_irq(kvm, irq);
  223. return 0;
  224. }
  225. static const struct seq_operations vgic_debug_seq_ops = {
  226. .start = vgic_debug_start,
  227. .next = vgic_debug_next,
  228. .stop = vgic_debug_stop,
  229. .show = vgic_debug_show
  230. };
  231. static int debug_open(struct inode *inode, struct file *file)
  232. {
  233. int ret;
  234. ret = seq_open(file, &vgic_debug_seq_ops);
  235. if (!ret) {
  236. struct seq_file *seq;
  237. /* seq_open will have modified file->private_data */
  238. seq = file->private_data;
  239. seq->private = inode->i_private;
  240. }
  241. return ret;
  242. };
  243. static const struct file_operations vgic_debug_fops = {
  244. .owner = THIS_MODULE,
  245. .open = debug_open,
  246. .read = seq_read,
  247. .llseek = seq_lseek,
  248. .release = seq_release
  249. };
  250. void vgic_debug_init(struct kvm *kvm)
  251. {
  252. debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm,
  253. &vgic_debug_fops);
  254. }
  255. void vgic_debug_destroy(struct kvm *kvm)
  256. {
  257. }