vgic-debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Linaro
  4. * Author: Christoffer Dall <christoffer.dall@linaro.org>
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/seq_file.h>
  11. #include <kvm/arm_vgic.h>
  12. #include <asm/kvm_mmu.h>
  13. #include "vgic.h"
  14. /*
  15. * Structure to control looping through the entire vgic state. We start at
  16. * zero for each field and move upwards. So, if dist_id is 0 we print the
  17. * distributor info. When dist_id is 1, we have already printed it and move
  18. * on.
  19. *
  20. * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
  21. * so on.
  22. */
  23. struct vgic_state_iter {
  24. int nr_cpus;
  25. int nr_spis;
  26. int dist_id;
  27. int vcpu_id;
  28. unsigned long intid;
  29. };
  30. static void iter_next(struct kvm *kvm, struct vgic_state_iter *iter)
  31. {
  32. struct vgic_dist *dist = &kvm->arch.vgic;
  33. if (iter->dist_id == 0) {
  34. iter->dist_id++;
  35. return;
  36. }
  37. /*
  38. * Let the xarray drive the iterator after the last SPI, as the iterator
  39. * has exhausted the sequentially-allocated INTID space.
  40. */
  41. if (iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS - 1)) {
  42. if (iter->intid == VGIC_LPI_MAX_INTID + 1)
  43. return;
  44. rcu_read_lock();
  45. if (!xa_find_after(&dist->lpi_xa, &iter->intid,
  46. VGIC_LPI_MAX_INTID, XA_PRESENT))
  47. iter->intid = VGIC_LPI_MAX_INTID + 1;
  48. rcu_read_unlock();
  49. return;
  50. }
  51. iter->intid++;
  52. if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
  53. ++iter->vcpu_id < iter->nr_cpus)
  54. iter->intid = 0;
  55. }
  56. static int vgic_count_lpis(struct kvm *kvm)
  57. {
  58. struct vgic_dist *dist = &kvm->arch.vgic;
  59. struct vgic_irq *irq;
  60. unsigned long intid;
  61. int nr_lpis = 0;
  62. rcu_read_lock();
  63. xa_for_each(&dist->lpi_xa, intid, irq)
  64. nr_lpis++;
  65. rcu_read_unlock();
  66. return nr_lpis;
  67. }
  68. static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
  69. loff_t pos)
  70. {
  71. int nr_cpus = atomic_read(&kvm->online_vcpus);
  72. memset(iter, 0, sizeof(*iter));
  73. iter->nr_cpus = nr_cpus;
  74. iter->nr_spis = kvm->arch.vgic.nr_spis;
  75. /* Fast forward to the right position if needed */
  76. while (pos--)
  77. iter_next(kvm, iter);
  78. }
  79. static bool end_of_vgic(struct vgic_state_iter *iter)
  80. {
  81. return iter->dist_id > 0 &&
  82. iter->vcpu_id == iter->nr_cpus &&
  83. iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
  84. iter->intid > VGIC_LPI_MAX_INTID;
  85. }
  86. static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
  87. {
  88. struct kvm *kvm = s->private;
  89. struct vgic_state_iter *iter;
  90. iter = kmalloc_obj(*iter);
  91. if (!iter)
  92. return ERR_PTR(-ENOMEM);
  93. iter_init(kvm, iter, *pos);
  94. if (end_of_vgic(iter)) {
  95. kfree(iter);
  96. iter = NULL;
  97. }
  98. return iter;
  99. }
  100. static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
  101. {
  102. struct kvm *kvm = s->private;
  103. struct vgic_state_iter *iter = v;
  104. ++*pos;
  105. iter_next(kvm, iter);
  106. if (end_of_vgic(iter)) {
  107. kfree(iter);
  108. iter = NULL;
  109. }
  110. return iter;
  111. }
  112. static void vgic_debug_stop(struct seq_file *s, void *v)
  113. {
  114. struct vgic_state_iter *iter = v;
  115. if (IS_ERR_OR_NULL(v))
  116. return;
  117. kfree(iter);
  118. }
  119. static void print_dist_state(struct seq_file *s, struct vgic_dist *dist,
  120. struct vgic_state_iter *iter)
  121. {
  122. bool v3 = dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3;
  123. struct kvm *kvm = s->private;
  124. seq_printf(s, "Distributor\n");
  125. seq_printf(s, "===========\n");
  126. seq_printf(s, "vgic_model:\t%s\n", v3 ? "GICv3" : "GICv2");
  127. seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
  128. if (v3)
  129. seq_printf(s, "nr_lpis:\t%d\n", vgic_count_lpis(kvm));
  130. seq_printf(s, "enabled:\t%d\n", dist->enabled);
  131. seq_printf(s, "\n");
  132. seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
  133. seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
  134. seq_printf(s, "G=group\n");
  135. }
  136. static void print_header(struct seq_file *s, struct vgic_irq *irq,
  137. struct kvm_vcpu *vcpu)
  138. {
  139. int id = 0;
  140. char *hdr = "SPI ";
  141. if (vcpu) {
  142. hdr = "VCPU";
  143. id = vcpu->vcpu_idx;
  144. }
  145. seq_printf(s, "\n");
  146. seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHCG HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
  147. seq_printf(s, "----------------------------------------------------------------\n");
  148. }
  149. static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
  150. struct kvm_vcpu *vcpu)
  151. {
  152. char *type;
  153. bool pending;
  154. if (irq->intid < VGIC_NR_SGIS)
  155. type = "SGI";
  156. else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
  157. type = "PPI";
  158. else if (irq->intid < VGIC_MAX_SPI)
  159. type = "SPI";
  160. else
  161. type = "LPI";
  162. if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
  163. print_header(s, irq, vcpu);
  164. pending = irq->pending_latch;
  165. if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
  166. int err;
  167. err = irq_get_irqchip_state(irq->host_irq,
  168. IRQCHIP_STATE_PENDING,
  169. &pending);
  170. WARN_ON_ONCE(err);
  171. }
  172. seq_printf(s, " %s %4d "
  173. " %2d "
  174. "%d%d%d%d%d%d%d "
  175. "%8d "
  176. "%8x "
  177. " %2x "
  178. "%3d "
  179. " %2d "
  180. "\n",
  181. type, irq->intid,
  182. (irq->target_vcpu) ? irq->target_vcpu->vcpu_idx : -1,
  183. pending,
  184. irq->line_level,
  185. irq->active,
  186. irq->enabled,
  187. irq->hw,
  188. irq->config == VGIC_CONFIG_LEVEL,
  189. irq->group,
  190. irq->hwintid,
  191. irq->mpidr,
  192. irq->source,
  193. irq->priority,
  194. (irq->vcpu) ? irq->vcpu->vcpu_idx : -1);
  195. }
  196. static int vgic_debug_show(struct seq_file *s, void *v)
  197. {
  198. struct kvm *kvm = s->private;
  199. struct vgic_state_iter *iter = v;
  200. struct vgic_irq *irq;
  201. struct kvm_vcpu *vcpu = NULL;
  202. unsigned long flags;
  203. if (iter->dist_id == 0) {
  204. print_dist_state(s, &kvm->arch.vgic, iter);
  205. return 0;
  206. }
  207. if (!kvm->arch.vgic.initialized)
  208. return 0;
  209. if (iter->vcpu_id < iter->nr_cpus)
  210. vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
  211. if (iter->intid < VGIC_NR_PRIVATE_IRQS)
  212. irq = vgic_get_vcpu_irq(vcpu, iter->intid);
  213. else
  214. irq = vgic_get_irq(kvm, iter->intid);
  215. if (!irq)
  216. return 0;
  217. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  218. print_irq_state(s, irq, vcpu);
  219. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  220. vgic_put_irq(kvm, irq);
  221. return 0;
  222. }
  223. static const struct seq_operations vgic_debug_sops = {
  224. .start = vgic_debug_start,
  225. .next = vgic_debug_next,
  226. .stop = vgic_debug_stop,
  227. .show = vgic_debug_show
  228. };
  229. DEFINE_SEQ_ATTRIBUTE(vgic_debug);
  230. void vgic_debug_init(struct kvm *kvm)
  231. {
  232. debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm,
  233. &vgic_debug_fops);
  234. }
  235. void vgic_debug_destroy(struct kvm *kvm)
  236. {
  237. }
  238. /**
  239. * struct vgic_its_iter - Iterator for traversing VGIC ITS device tables.
  240. * @dev: Pointer to the current its_device being processed.
  241. * @ite: Pointer to the current its_ite within the device being processed.
  242. *
  243. * This structure is used to maintain the current position during iteration
  244. * over the ITS device tables. It holds pointers to both the current device
  245. * and the current ITE within that device.
  246. */
  247. struct vgic_its_iter {
  248. struct its_device *dev;
  249. struct its_ite *ite;
  250. };
  251. /**
  252. * end_of_iter - Checks if the iterator has reached the end.
  253. * @iter: The iterator to check.
  254. *
  255. * When the iterator completed processing the final ITE in the last device
  256. * table, it was marked to indicate the end of iteration by setting its
  257. * device and ITE pointers to NULL.
  258. * This function checks whether the iterator was marked as end.
  259. *
  260. * Return: True if the iterator is marked as end, false otherwise.
  261. */
  262. static inline bool end_of_iter(struct vgic_its_iter *iter)
  263. {
  264. return !iter->dev && !iter->ite;
  265. }
  266. /**
  267. * vgic_its_iter_next - Advances the iterator to the next entry in the ITS tables.
  268. * @its: The VGIC ITS structure.
  269. * @iter: The iterator to advance.
  270. *
  271. * This function moves the iterator to the next ITE within the current device,
  272. * or to the first ITE of the next device if the current ITE is the last in
  273. * the device. If the current device is the last device, the iterator is set
  274. * to indicate the end of iteration.
  275. */
  276. static void vgic_its_iter_next(struct vgic_its *its, struct vgic_its_iter *iter)
  277. {
  278. struct its_device *dev = iter->dev;
  279. struct its_ite *ite = iter->ite;
  280. if (!ite || list_is_last(&ite->ite_list, &dev->itt_head)) {
  281. if (list_is_last(&dev->dev_list, &its->device_list)) {
  282. dev = NULL;
  283. ite = NULL;
  284. } else {
  285. dev = list_next_entry(dev, dev_list);
  286. ite = list_first_entry_or_null(&dev->itt_head,
  287. struct its_ite,
  288. ite_list);
  289. }
  290. } else {
  291. ite = list_next_entry(ite, ite_list);
  292. }
  293. iter->dev = dev;
  294. iter->ite = ite;
  295. }
  296. /**
  297. * vgic_its_debug_start - Start function for the seq_file interface.
  298. * @s: The seq_file structure.
  299. * @pos: The starting position (offset).
  300. *
  301. * This function initializes the iterator to the beginning of the ITS tables
  302. * and advances it to the specified position. It acquires the its_lock mutex
  303. * to protect shared data.
  304. *
  305. * Return: An iterator pointer on success, NULL if no devices are found or
  306. * the end of the list is reached, or ERR_PTR(-ENOMEM) on memory
  307. * allocation failure.
  308. */
  309. static void *vgic_its_debug_start(struct seq_file *s, loff_t *pos)
  310. {
  311. struct vgic_its *its = s->private;
  312. struct vgic_its_iter *iter;
  313. struct its_device *dev;
  314. loff_t offset = *pos;
  315. mutex_lock(&its->its_lock);
  316. dev = list_first_entry_or_null(&its->device_list,
  317. struct its_device, dev_list);
  318. if (!dev)
  319. return NULL;
  320. iter = kmalloc_obj(*iter);
  321. if (!iter)
  322. return ERR_PTR(-ENOMEM);
  323. iter->dev = dev;
  324. iter->ite = list_first_entry_or_null(&dev->itt_head,
  325. struct its_ite, ite_list);
  326. while (!end_of_iter(iter) && offset--)
  327. vgic_its_iter_next(its, iter);
  328. if (end_of_iter(iter)) {
  329. kfree(iter);
  330. return NULL;
  331. }
  332. return iter;
  333. }
  334. /**
  335. * vgic_its_debug_next - Next function for the seq_file interface.
  336. * @s: The seq_file structure.
  337. * @v: The current iterator.
  338. * @pos: The current position (offset).
  339. *
  340. * This function advances the iterator to the next entry and increments the
  341. * position.
  342. *
  343. * Return: An iterator pointer on success, or NULL if the end of the list is
  344. * reached.
  345. */
  346. static void *vgic_its_debug_next(struct seq_file *s, void *v, loff_t *pos)
  347. {
  348. struct vgic_its *its = s->private;
  349. struct vgic_its_iter *iter = v;
  350. ++*pos;
  351. vgic_its_iter_next(its, iter);
  352. if (end_of_iter(iter)) {
  353. kfree(iter);
  354. return NULL;
  355. }
  356. return iter;
  357. }
  358. /**
  359. * vgic_its_debug_stop - Stop function for the seq_file interface.
  360. * @s: The seq_file structure.
  361. * @v: The current iterator.
  362. *
  363. * This function frees the iterator and releases the its_lock mutex.
  364. */
  365. static void vgic_its_debug_stop(struct seq_file *s, void *v)
  366. {
  367. struct vgic_its *its = s->private;
  368. struct vgic_its_iter *iter = v;
  369. if (!IS_ERR_OR_NULL(iter))
  370. kfree(iter);
  371. mutex_unlock(&its->its_lock);
  372. }
  373. /**
  374. * vgic_its_debug_show - Show function for the seq_file interface.
  375. * @s: The seq_file structure.
  376. * @v: The current iterator.
  377. *
  378. * This function formats and prints the ITS table entry information to the
  379. * seq_file output.
  380. *
  381. * Return: 0 on success.
  382. */
  383. static int vgic_its_debug_show(struct seq_file *s, void *v)
  384. {
  385. struct vgic_its_iter *iter = v;
  386. struct its_device *dev = iter->dev;
  387. struct its_ite *ite = iter->ite;
  388. if (!ite)
  389. return 0;
  390. if (list_is_first(&ite->ite_list, &dev->itt_head)) {
  391. seq_printf(s, "\n");
  392. seq_printf(s, "Device ID: 0x%x, Event ID Range: [0 - %llu]\n",
  393. dev->device_id, BIT_ULL(dev->num_eventid_bits) - 1);
  394. seq_printf(s, "EVENT_ID INTID HWINTID TARGET COL_ID HW\n");
  395. seq_printf(s, "-----------------------------------------------\n");
  396. }
  397. if (ite->irq && ite->collection) {
  398. seq_printf(s, "%8u %8u %8u %8u %8u %2d\n",
  399. ite->event_id, ite->irq->intid, ite->irq->hwintid,
  400. ite->collection->target_addr,
  401. ite->collection->collection_id, ite->irq->hw);
  402. }
  403. return 0;
  404. }
  405. static const struct seq_operations vgic_its_debug_sops = {
  406. .start = vgic_its_debug_start,
  407. .next = vgic_its_debug_next,
  408. .stop = vgic_its_debug_stop,
  409. .show = vgic_its_debug_show
  410. };
  411. DEFINE_SEQ_ATTRIBUTE(vgic_its_debug);
  412. /**
  413. * vgic_its_debug_init - Initializes the debugfs interface for VGIC ITS.
  414. * @dev: The KVM device structure.
  415. *
  416. * This function creates a debugfs file named "vgic-its-state@%its_base"
  417. * to expose the ITS table information.
  418. *
  419. * Return: 0 on success.
  420. */
  421. int vgic_its_debug_init(struct kvm_device *dev)
  422. {
  423. struct vgic_its *its = dev->private;
  424. char *name;
  425. name = kasprintf(GFP_KERNEL, "vgic-its-state@%llx", (u64)its->vgic_its_base);
  426. if (!name)
  427. return -ENOMEM;
  428. debugfs_create_file(name, 0444, dev->kvm->debugfs_dentry, its, &vgic_its_debug_fops);
  429. kfree(name);
  430. return 0;
  431. }
  432. void vgic_its_debug_destroy(struct kvm_device *dev)
  433. {
  434. }