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