1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation. 4 */ 5 6 #define pr_fmt(fmt) "xive-kvm: " fmt 7 8 #include <linux/kernel.h> 9 #include <linux/kvm_host.h> 10 #include <linux/err.h> 11 #include <linux/gfp.h> 12 #include <linux/spinlock.h> 13 #include <linux/delay.h> 14 #include <linux/percpu.h> 15 #include <linux/cpumask.h> 16 #include <linux/uaccess.h> 17 #include <asm/kvm_book3s.h> 18 #include <asm/kvm_ppc.h> 19 #include <asm/hvcall.h> 20 #include <asm/xics.h> 21 #include <asm/xive.h> 22 #include <asm/xive-regs.h> 23 #include <asm/debug.h> 24 #include <asm/debugfs.h> 25 #include <asm/time.h> 26 #include <asm/opal.h> 27 28 #include <linux/debugfs.h> 29 #include <linux/seq_file.h> 30 31 #include "book3s_xive.h" 32 33 34 /* 35 * Virtual mode variants of the hcalls for use on radix/radix 36 * with AIL. They require the VCPU's VP to be "pushed" 37 * 38 * We still instantiate them here because we use some of the 39 * generated utility functions as well in this file. 40 */ 41 #define XIVE_RUNTIME_CHECKS 42 #define X_PFX xive_vm_ 43 #define X_STATIC static 44 #define X_STAT_PFX stat_vm_ 45 #define __x_tima xive_tima 46 #define __x_eoi_page(xd) ((void __iomem *)((xd)->eoi_mmio)) 47 #define __x_trig_page(xd) ((void __iomem *)((xd)->trig_mmio)) 48 #define __x_writeb __raw_writeb 49 #define __x_readw __raw_readw 50 #define __x_readq __raw_readq 51 #define __x_writeq __raw_writeq 52 53 #include "book3s_xive_template.c" 54 55 /* 56 * We leave a gap of a couple of interrupts in the queue to 57 * account for the IPI and additional safety guard. 58 */ 59 #define XIVE_Q_GAP 2 60 61 /* 62 * Push a vcpu's context to the XIVE on guest entry. 63 * This assumes we are in virtual mode (MMU on) 64 */ 65 void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) 66 { 67 void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt; 68 u64 pq; 69 70 /* 71 * Nothing to do if the platform doesn't have a XIVE 72 * or this vCPU doesn't have its own XIVE context 73 * (e.g. because it's not using an in-kernel interrupt controller). 74 */ 75 if (!tima || !vcpu->arch.xive_cam_word) 76 return; 77 78 eieio(); 79 __raw_writeq(vcpu->arch.xive_saved_state.w01, tima + TM_QW1_OS); 80 __raw_writel(vcpu->arch.xive_cam_word, tima + TM_QW1_OS + TM_WORD2); 81 vcpu->arch.xive_pushed = 1; 82 eieio(); 83 84 /* 85 * We clear the irq_pending flag. There is a small chance of a 86 * race vs. the escalation interrupt happening on another 87 * processor setting it again, but the only consequence is to 88 * cause a spurious wakeup on the next H_CEDE, which is not an 89 * issue. 90 */ 91 vcpu->arch.irq_pending = 0; 92 93 /* 94 * In single escalation mode, if the escalation interrupt is 95 * on, we mask it. 96 */ 97 if (vcpu->arch.xive_esc_on) { 98 pq = __raw_readq((void __iomem *)(vcpu->arch.xive_esc_vaddr + 99 XIVE_ESB_SET_PQ_01)); 100 mb(); 101 102 /* 103 * We have a possible subtle race here: The escalation 104 * interrupt might have fired and be on its way to the 105 * host queue while we mask it, and if we unmask it 106 * early enough (re-cede right away), there is a 107 * theorical possibility that it fires again, thus 108 * landing in the target queue more than once which is 109 * a big no-no. 110 * 111 * Fortunately, solving this is rather easy. If the 112 * above load setting PQ to 01 returns a previous 113 * value where P is set, then we know the escalation 114 * interrupt is somewhere on its way to the host. In 115 * that case we simply don't clear the xive_esc_on 116 * flag below. It will be eventually cleared by the 117 * handler for the escalation interrupt. 118 * 119 * Then, when doing a cede, we check that flag again 120 * before re-enabling the escalation interrupt, and if 121 * set, we abort the cede. 122 */ 123 if (!(pq & XIVE_ESB_VAL_P)) 124 /* Now P is 0, we can clear the flag */ 125 vcpu->arch.xive_esc_on = 0; 126 } 127 } 128 EXPORT_SYMBOL_GPL(kvmppc_xive_push_vcpu); 129 130 /* 131 * This is a simple trigger for a generic XIVE IRQ. This must 132 * only be called for interrupts that support a trigger page 133 */ 134 static bool xive_irq_trigger(struct xive_irq_data *xd) 135 { 136 /* This should be only for MSIs */ 137 if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI)) 138 return false; 139 140 /* Those interrupts should always have a trigger page */ 141 if (WARN_ON(!xd->trig_mmio)) 142 return false; 143 144 out_be64(xd->trig_mmio, 0); 145 146 return true; 147 } 148 149 static irqreturn_t xive_esc_irq(int irq, void *data) 150 { 151 struct kvm_vcpu *vcpu = data; 152 153 vcpu->arch.irq_pending = 1; 154 smp_mb(); 155 if (vcpu->arch.ceded) 156 kvmppc_fast_vcpu_kick(vcpu); 157 158 /* Since we have the no-EOI flag, the interrupt is effectively 159 * disabled now. Clearing xive_esc_on means we won't bother 160 * doing so on the next entry. 161 * 162 * This also allows the entry code to know that if a PQ combination 163 * of 10 is observed while xive_esc_on is true, it means the queue 164 * contains an unprocessed escalation interrupt. We don't make use of 165 * that knowledge today but might (see comment in book3s_hv_rmhandler.S) 166 */ 167 vcpu->arch.xive_esc_on = false; 168 169 /* This orders xive_esc_on = false vs. subsequent stale_p = true */ 170 smp_wmb(); /* goes with smp_mb() in cleanup_single_escalation */ 171 172 return IRQ_HANDLED; 173 } 174 175 int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio, 176 bool single_escalation) 177 { 178 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 179 struct xive_q *q = &xc->queues[prio]; 180 char *name = NULL; 181 int rc; 182 183 /* Already there ? */ 184 if (xc->esc_virq[prio]) 185 return 0; 186 187 /* Hook up the escalation interrupt */ 188 xc->esc_virq[prio] = irq_create_mapping(NULL, q->esc_irq); 189 if (!xc->esc_virq[prio]) { 190 pr_err("Failed to map escalation interrupt for queue %d of VCPU %d\n", 191 prio, xc->server_num); 192 return -EIO; 193 } 194 195 if (single_escalation) 196 name = kasprintf(GFP_KERNEL, "kvm-%d-%d", 197 vcpu->kvm->arch.lpid, xc->server_num); 198 else 199 name = kasprintf(GFP_KERNEL, "kvm-%d-%d-%d", 200 vcpu->kvm->arch.lpid, xc->server_num, prio); 201 if (!name) { 202 pr_err("Failed to allocate escalation irq name for queue %d of VCPU %d\n", 203 prio, xc->server_num); 204 rc = -ENOMEM; 205 goto error; 206 } 207 208 pr_devel("Escalation %s irq %d (prio %d)\n", name, xc->esc_virq[prio], prio); 209 210 rc = request_irq(xc->esc_virq[prio], xive_esc_irq, 211 IRQF_NO_THREAD, name, vcpu); 212 if (rc) { 213 pr_err("Failed to request escalation interrupt for queue %d of VCPU %d\n", 214 prio, xc->server_num); 215 goto error; 216 } 217 xc->esc_virq_names[prio] = name; 218 219 /* In single escalation mode, we grab the ESB MMIO of the 220 * interrupt and mask it. Also populate the VCPU v/raddr 221 * of the ESB page for use by asm entry/exit code. Finally 222 * set the XIVE_IRQ_FLAG_NO_EOI flag which will prevent the 223 * core code from performing an EOI on the escalation 224 * interrupt, thus leaving it effectively masked after 225 * it fires once. 226 */ 227 if (single_escalation) { 228 struct irq_data *d = irq_get_irq_data(xc->esc_virq[prio]); 229 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d); 230 231 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01); 232 vcpu->arch.xive_esc_raddr = xd->eoi_page; 233 vcpu->arch.xive_esc_vaddr = (__force u64)xd->eoi_mmio; 234 xd->flags |= XIVE_IRQ_FLAG_NO_EOI; 235 } 236 237 return 0; 238 error: 239 irq_dispose_mapping(xc->esc_virq[prio]); 240 xc->esc_virq[prio] = 0; 241 kfree(name); 242 return rc; 243 } 244 245 static int xive_provision_queue(struct kvm_vcpu *vcpu, u8 prio) 246 { 247 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 248 struct kvmppc_xive *xive = xc->xive; 249 struct xive_q *q = &xc->queues[prio]; 250 void *qpage; 251 int rc; 252 253 if (WARN_ON(q->qpage)) 254 return 0; 255 256 /* Allocate the queue and retrieve infos on current node for now */ 257 qpage = (__be32 *)__get_free_pages(GFP_KERNEL, xive->q_page_order); 258 if (!qpage) { 259 pr_err("Failed to allocate queue %d for VCPU %d\n", 260 prio, xc->server_num); 261 return -ENOMEM; 262 } 263 memset(qpage, 0, 1 << xive->q_order); 264 265 /* 266 * Reconfigure the queue. This will set q->qpage only once the 267 * queue is fully configured. This is a requirement for prio 0 268 * as we will stop doing EOIs for every IPI as soon as we observe 269 * qpage being non-NULL, and instead will only EOI when we receive 270 * corresponding queue 0 entries 271 */ 272 rc = xive_native_configure_queue(xc->vp_id, q, prio, qpage, 273 xive->q_order, true); 274 if (rc) 275 pr_err("Failed to configure queue %d for VCPU %d\n", 276 prio, xc->server_num); 277 return rc; 278 } 279 280 /* Called with xive->lock held */ 281 static int xive_check_provisioning(struct kvm *kvm, u8 prio) 282 { 283 struct kvmppc_xive *xive = kvm->arch.xive; 284 struct kvm_vcpu *vcpu; 285 int i, rc; 286 287 lockdep_assert_held(&xive->lock); 288 289 /* Already provisioned ? */ 290 if (xive->qmap & (1 << prio)) 291 return 0; 292 293 pr_devel("Provisioning prio... %d\n", prio); 294 295 /* Provision each VCPU and enable escalations if needed */ 296 kvm_for_each_vcpu(i, vcpu, kvm) { 297 if (!vcpu->arch.xive_vcpu) 298 continue; 299 rc = xive_provision_queue(vcpu, prio); 300 if (rc == 0 && !xive->single_escalation) 301 kvmppc_xive_attach_escalation(vcpu, prio, 302 xive->single_escalation); 303 if (rc) 304 return rc; 305 } 306 307 /* Order previous stores and mark it as provisioned */ 308 mb(); 309 xive->qmap |= (1 << prio); 310 return 0; 311 } 312 313 static void xive_inc_q_pending(struct kvm *kvm, u32 server, u8 prio) 314 { 315 struct kvm_vcpu *vcpu; 316 struct kvmppc_xive_vcpu *xc; 317 struct xive_q *q; 318 319 /* Locate target server */ 320 vcpu = kvmppc_xive_find_server(kvm, server); 321 if (!vcpu) { 322 pr_warn("%s: Can't find server %d\n", __func__, server); 323 return; 324 } 325 xc = vcpu->arch.xive_vcpu; 326 if (WARN_ON(!xc)) 327 return; 328 329 q = &xc->queues[prio]; 330 atomic_inc(&q->pending_count); 331 } 332 333 static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio) 334 { 335 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 336 struct xive_q *q; 337 u32 max; 338 339 if (WARN_ON(!xc)) 340 return -ENXIO; 341 if (!xc->valid) 342 return -ENXIO; 343 344 q = &xc->queues[prio]; 345 if (WARN_ON(!q->qpage)) 346 return -ENXIO; 347 348 /* Calculate max number of interrupts in that queue. */ 349 max = (q->msk + 1) - XIVE_Q_GAP; 350 return atomic_add_unless(&q->count, 1, max) ? 0 : -EBUSY; 351 } 352 353 int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio) 354 { 355 struct kvm_vcpu *vcpu; 356 int i, rc; 357 358 /* Locate target server */ 359 vcpu = kvmppc_xive_find_server(kvm, *server); 360 if (!vcpu) { 361 pr_devel("Can't find server %d\n", *server); 362 return -EINVAL; 363 } 364 365 pr_devel("Finding irq target on 0x%x/%d...\n", *server, prio); 366 367 /* Try pick it */ 368 rc = xive_try_pick_queue(vcpu, prio); 369 if (rc == 0) 370 return rc; 371 372 pr_devel(" .. failed, looking up candidate...\n"); 373 374 /* Failed, pick another VCPU */ 375 kvm_for_each_vcpu(i, vcpu, kvm) { 376 if (!vcpu->arch.xive_vcpu) 377 continue; 378 rc = xive_try_pick_queue(vcpu, prio); 379 if (rc == 0) { 380 *server = vcpu->arch.xive_vcpu->server_num; 381 pr_devel(" found on 0x%x/%d\n", *server, prio); 382 return rc; 383 } 384 } 385 pr_devel(" no available target !\n"); 386 387 /* No available target ! */ 388 return -EBUSY; 389 } 390 391 static u8 xive_lock_and_mask(struct kvmppc_xive *xive, 392 struct kvmppc_xive_src_block *sb, 393 struct kvmppc_xive_irq_state *state) 394 { 395 struct xive_irq_data *xd; 396 u32 hw_num; 397 u8 old_prio; 398 u64 val; 399 400 /* 401 * Take the lock, set masked, try again if racing 402 * with H_EOI 403 */ 404 for (;;) { 405 arch_spin_lock(&sb->lock); 406 old_prio = state->guest_priority; 407 state->guest_priority = MASKED; 408 mb(); 409 if (!state->in_eoi) 410 break; 411 state->guest_priority = old_prio; 412 arch_spin_unlock(&sb->lock); 413 } 414 415 /* No change ? Bail */ 416 if (old_prio == MASKED) 417 return old_prio; 418 419 /* Get the right irq */ 420 kvmppc_xive_select_irq(state, &hw_num, &xd); 421 422 /* Set PQ to 10, return old P and old Q and remember them */ 423 val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_10); 424 state->old_p = !!(val & 2); 425 state->old_q = !!(val & 1); 426 427 /* 428 * Synchronize hardware to sensure the queues are updated when 429 * masking 430 */ 431 xive_native_sync_source(hw_num); 432 433 return old_prio; 434 } 435 436 static void xive_lock_for_unmask(struct kvmppc_xive_src_block *sb, 437 struct kvmppc_xive_irq_state *state) 438 { 439 /* 440 * Take the lock try again if racing with H_EOI 441 */ 442 for (;;) { 443 arch_spin_lock(&sb->lock); 444 if (!state->in_eoi) 445 break; 446 arch_spin_unlock(&sb->lock); 447 } 448 } 449 450 static void xive_finish_unmask(struct kvmppc_xive *xive, 451 struct kvmppc_xive_src_block *sb, 452 struct kvmppc_xive_irq_state *state, 453 u8 prio) 454 { 455 struct xive_irq_data *xd; 456 u32 hw_num; 457 458 /* If we aren't changing a thing, move on */ 459 if (state->guest_priority != MASKED) 460 goto bail; 461 462 /* Get the right irq */ 463 kvmppc_xive_select_irq(state, &hw_num, &xd); 464 465 /* Old Q set, set PQ to 11 */ 466 if (state->old_q) 467 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11); 468 469 /* 470 * If not old P, then perform an "effective" EOI, 471 * on the source. This will handle the cases where 472 * FW EOI is needed. 473 */ 474 if (!state->old_p) 475 xive_vm_source_eoi(hw_num, xd); 476 477 /* Synchronize ordering and mark unmasked */ 478 mb(); 479 bail: 480 state->guest_priority = prio; 481 } 482 483 /* 484 * Target an interrupt to a given server/prio, this will fallback 485 * to another server if necessary and perform the HW targetting 486 * updates as needed 487 * 488 * NOTE: Must be called with the state lock held 489 */ 490 static int xive_target_interrupt(struct kvm *kvm, 491 struct kvmppc_xive_irq_state *state, 492 u32 server, u8 prio) 493 { 494 struct kvmppc_xive *xive = kvm->arch.xive; 495 u32 hw_num; 496 int rc; 497 498 /* 499 * This will return a tentative server and actual 500 * priority. The count for that new target will have 501 * already been incremented. 502 */ 503 rc = kvmppc_xive_select_target(kvm, &server, prio); 504 505 /* 506 * We failed to find a target ? Not much we can do 507 * at least until we support the GIQ. 508 */ 509 if (rc) 510 return rc; 511 512 /* 513 * Increment the old queue pending count if there 514 * was one so that the old queue count gets adjusted later 515 * when observed to be empty. 516 */ 517 if (state->act_priority != MASKED) 518 xive_inc_q_pending(kvm, 519 state->act_server, 520 state->act_priority); 521 /* 522 * Update state and HW 523 */ 524 state->act_priority = prio; 525 state->act_server = server; 526 527 /* Get the right irq */ 528 kvmppc_xive_select_irq(state, &hw_num, NULL); 529 530 return xive_native_configure_irq(hw_num, 531 kvmppc_xive_vp(xive, server), 532 prio, state->number); 533 } 534 535 /* 536 * Targetting rules: In order to avoid losing track of 537 * pending interrupts accross mask and unmask, which would 538 * allow queue overflows, we implement the following rules: 539 * 540 * - Unless it was never enabled (or we run out of capacity) 541 * an interrupt is always targetted at a valid server/queue 542 * pair even when "masked" by the guest. This pair tends to 543 * be the last one used but it can be changed under some 544 * circumstances. That allows us to separate targetting 545 * from masking, we only handle accounting during (re)targetting, 546 * this also allows us to let an interrupt drain into its target 547 * queue after masking, avoiding complex schemes to remove 548 * interrupts out of remote processor queues. 549 * 550 * - When masking, we set PQ to 10 and save the previous value 551 * of P and Q. 552 * 553 * - When unmasking, if saved Q was set, we set PQ to 11 554 * otherwise we leave PQ to the HW state which will be either 555 * 10 if nothing happened or 11 if the interrupt fired while 556 * masked. Effectively we are OR'ing the previous Q into the 557 * HW Q. 558 * 559 * Then if saved P is clear, we do an effective EOI (Q->P->Trigger) 560 * which will unmask the interrupt and shoot a new one if Q was 561 * set. 562 * 563 * Otherwise (saved P is set) we leave PQ unchanged (so 10 or 11, 564 * effectively meaning an H_EOI from the guest is still expected 565 * for that interrupt). 566 * 567 * - If H_EOI occurs while masked, we clear the saved P. 568 * 569 * - When changing target, we account on the new target and 570 * increment a separate "pending" counter on the old one. 571 * This pending counter will be used to decrement the old 572 * target's count when its queue has been observed empty. 573 */ 574 575 int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server, 576 u32 priority) 577 { 578 struct kvmppc_xive *xive = kvm->arch.xive; 579 struct kvmppc_xive_src_block *sb; 580 struct kvmppc_xive_irq_state *state; 581 u8 new_act_prio; 582 int rc = 0; 583 u16 idx; 584 585 if (!xive) 586 return -ENODEV; 587 588 pr_devel("set_xive ! irq 0x%x server 0x%x prio %d\n", 589 irq, server, priority); 590 591 /* First, check provisioning of queues */ 592 if (priority != MASKED) { 593 mutex_lock(&xive->lock); 594 rc = xive_check_provisioning(xive->kvm, 595 xive_prio_from_guest(priority)); 596 mutex_unlock(&xive->lock); 597 } 598 if (rc) { 599 pr_devel(" provisioning failure %d !\n", rc); 600 return rc; 601 } 602 603 sb = kvmppc_xive_find_source(xive, irq, &idx); 604 if (!sb) 605 return -EINVAL; 606 state = &sb->irq_state[idx]; 607 608 /* 609 * We first handle masking/unmasking since the locking 610 * might need to be retried due to EOIs, we'll handle 611 * targetting changes later. These functions will return 612 * with the SB lock held. 613 * 614 * xive_lock_and_mask() will also set state->guest_priority 615 * but won't otherwise change other fields of the state. 616 * 617 * xive_lock_for_unmask will not actually unmask, this will 618 * be done later by xive_finish_unmask() once the targetting 619 * has been done, so we don't try to unmask an interrupt 620 * that hasn't yet been targetted. 621 */ 622 if (priority == MASKED) 623 xive_lock_and_mask(xive, sb, state); 624 else 625 xive_lock_for_unmask(sb, state); 626 627 628 /* 629 * Then we handle targetting. 630 * 631 * First calculate a new "actual priority" 632 */ 633 new_act_prio = state->act_priority; 634 if (priority != MASKED) 635 new_act_prio = xive_prio_from_guest(priority); 636 637 pr_devel(" new_act_prio=%x act_server=%x act_prio=%x\n", 638 new_act_prio, state->act_server, state->act_priority); 639 640 /* 641 * Then check if we actually need to change anything, 642 * 643 * The condition for re-targetting the interrupt is that 644 * we have a valid new priority (new_act_prio is not 0xff) 645 * and either the server or the priority changed. 646 * 647 * Note: If act_priority was ff and the new priority is 648 * also ff, we don't do anything and leave the interrupt 649 * untargetted. An attempt of doing an int_on on an 650 * untargetted interrupt will fail. If that is a problem 651 * we could initialize interrupts with valid default 652 */ 653 654 if (new_act_prio != MASKED && 655 (state->act_server != server || 656 state->act_priority != new_act_prio)) 657 rc = xive_target_interrupt(kvm, state, server, new_act_prio); 658 659 /* 660 * Perform the final unmasking of the interrupt source 661 * if necessary 662 */ 663 if (priority != MASKED) 664 xive_finish_unmask(xive, sb, state, priority); 665 666 /* 667 * Finally Update saved_priority to match. Only int_on/off 668 * set this field to a different value. 669 */ 670 state->saved_priority = priority; 671 672 arch_spin_unlock(&sb->lock); 673 return rc; 674 } 675 676 int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server, 677 u32 *priority) 678 { 679 struct kvmppc_xive *xive = kvm->arch.xive; 680 struct kvmppc_xive_src_block *sb; 681 struct kvmppc_xive_irq_state *state; 682 u16 idx; 683 684 if (!xive) 685 return -ENODEV; 686 687 sb = kvmppc_xive_find_source(xive, irq, &idx); 688 if (!sb) 689 return -EINVAL; 690 state = &sb->irq_state[idx]; 691 arch_spin_lock(&sb->lock); 692 *server = state->act_server; 693 *priority = state->guest_priority; 694 arch_spin_unlock(&sb->lock); 695 696 return 0; 697 } 698 699 int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) 700 { 701 struct kvmppc_xive *xive = kvm->arch.xive; 702 struct kvmppc_xive_src_block *sb; 703 struct kvmppc_xive_irq_state *state; 704 u16 idx; 705 706 if (!xive) 707 return -ENODEV; 708 709 sb = kvmppc_xive_find_source(xive, irq, &idx); 710 if (!sb) 711 return -EINVAL; 712 state = &sb->irq_state[idx]; 713 714 pr_devel("int_on(irq=0x%x)\n", irq); 715 716 /* 717 * Check if interrupt was not targetted 718 */ 719 if (state->act_priority == MASKED) { 720 pr_devel("int_on on untargetted interrupt\n"); 721 return -EINVAL; 722 } 723 724 /* If saved_priority is 0xff, do nothing */ 725 if (state->saved_priority == MASKED) 726 return 0; 727 728 /* 729 * Lock and unmask it. 730 */ 731 xive_lock_for_unmask(sb, state); 732 xive_finish_unmask(xive, sb, state, state->saved_priority); 733 arch_spin_unlock(&sb->lock); 734 735 return 0; 736 } 737 738 int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) 739 { 740 struct kvmppc_xive *xive = kvm->arch.xive; 741 struct kvmppc_xive_src_block *sb; 742 struct kvmppc_xive_irq_state *state; 743 u16 idx; 744 745 if (!xive) 746 return -ENODEV; 747 748 sb = kvmppc_xive_find_source(xive, irq, &idx); 749 if (!sb) 750 return -EINVAL; 751 state = &sb->irq_state[idx]; 752 753 pr_devel("int_off(irq=0x%x)\n", irq); 754 755 /* 756 * Lock and mask 757 */ 758 state->saved_priority = xive_lock_and_mask(xive, sb, state); 759 arch_spin_unlock(&sb->lock); 760 761 return 0; 762 } 763 764 static bool xive_restore_pending_irq(struct kvmppc_xive *xive, u32 irq) 765 { 766 struct kvmppc_xive_src_block *sb; 767 struct kvmppc_xive_irq_state *state; 768 u16 idx; 769 770 sb = kvmppc_xive_find_source(xive, irq, &idx); 771 if (!sb) 772 return false; 773 state = &sb->irq_state[idx]; 774 if (!state->valid) 775 return false; 776 777 /* 778 * Trigger the IPI. This assumes we never restore a pass-through 779 * interrupt which should be safe enough 780 */ 781 xive_irq_trigger(&state->ipi_data); 782 783 return true; 784 } 785 786 u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) 787 { 788 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 789 790 if (!xc) 791 return 0; 792 793 /* Return the per-cpu state for state saving/migration */ 794 return (u64)xc->cppr << KVM_REG_PPC_ICP_CPPR_SHIFT | 795 (u64)xc->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT | 796 (u64)0xff << KVM_REG_PPC_ICP_PPRI_SHIFT; 797 } 798 799 int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) 800 { 801 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 802 struct kvmppc_xive *xive = vcpu->kvm->arch.xive; 803 u8 cppr, mfrr; 804 u32 xisr; 805 806 if (!xc || !xive) 807 return -ENOENT; 808 809 /* Grab individual state fields. We don't use pending_pri */ 810 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT; 811 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) & 812 KVM_REG_PPC_ICP_XISR_MASK; 813 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT; 814 815 pr_devel("set_icp vcpu %d cppr=0x%x mfrr=0x%x xisr=0x%x\n", 816 xc->server_num, cppr, mfrr, xisr); 817 818 /* 819 * We can't update the state of a "pushed" VCPU, but that 820 * shouldn't happen because the vcpu->mutex makes running a 821 * vcpu mutually exclusive with doing one_reg get/set on it. 822 */ 823 if (WARN_ON(vcpu->arch.xive_pushed)) 824 return -EIO; 825 826 /* Update VCPU HW saved state */ 827 vcpu->arch.xive_saved_state.cppr = cppr; 828 xc->hw_cppr = xc->cppr = cppr; 829 830 /* 831 * Update MFRR state. If it's not 0xff, we mark the VCPU as 832 * having a pending MFRR change, which will re-evaluate the 833 * target. The VCPU will thus potentially get a spurious 834 * interrupt but that's not a big deal. 835 */ 836 xc->mfrr = mfrr; 837 if (mfrr < cppr) 838 xive_irq_trigger(&xc->vp_ipi_data); 839 840 /* 841 * Now saved XIRR is "interesting". It means there's something in 842 * the legacy "1 element" queue... for an IPI we simply ignore it, 843 * as the MFRR restore will handle that. For anything else we need 844 * to force a resend of the source. 845 * However the source may not have been setup yet. If that's the 846 * case, we keep that info and increment a counter in the xive to 847 * tell subsequent xive_set_source() to go look. 848 */ 849 if (xisr > XICS_IPI && !xive_restore_pending_irq(xive, xisr)) { 850 xc->delayed_irq = xisr; 851 xive->delayed_irqs++; 852 pr_devel(" xisr restore delayed\n"); 853 } 854 855 return 0; 856 } 857 858 int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq, 859 struct irq_desc *host_desc) 860 { 861 struct kvmppc_xive *xive = kvm->arch.xive; 862 struct kvmppc_xive_src_block *sb; 863 struct kvmppc_xive_irq_state *state; 864 struct irq_data *host_data = irq_desc_get_irq_data(host_desc); 865 unsigned int host_irq = irq_desc_get_irq(host_desc); 866 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(host_data); 867 u16 idx; 868 u8 prio; 869 int rc; 870 871 if (!xive) 872 return -ENODEV; 873 874 pr_devel("set_mapped girq 0x%lx host HW irq 0x%x...\n",guest_irq, hw_irq); 875 876 sb = kvmppc_xive_find_source(xive, guest_irq, &idx); 877 if (!sb) 878 return -EINVAL; 879 state = &sb->irq_state[idx]; 880 881 /* 882 * Mark the passed-through interrupt as going to a VCPU, 883 * this will prevent further EOIs and similar operations 884 * from the XIVE code. It will also mask the interrupt 885 * to either PQ=10 or 11 state, the latter if the interrupt 886 * is pending. This will allow us to unmask or retrigger it 887 * after routing it to the guest with a simple EOI. 888 * 889 * The "state" argument is a "token", all it needs is to be 890 * non-NULL to switch to passed-through or NULL for the 891 * other way around. We may not yet have an actual VCPU 892 * target here and we don't really care. 893 */ 894 rc = irq_set_vcpu_affinity(host_irq, state); 895 if (rc) { 896 pr_err("Failed to set VCPU affinity for irq %d\n", host_irq); 897 return rc; 898 } 899 900 /* 901 * Mask and read state of IPI. We need to know if its P bit 902 * is set as that means it's potentially already using a 903 * queue entry in the target 904 */ 905 prio = xive_lock_and_mask(xive, sb, state); 906 pr_devel(" old IPI prio %02x P:%d Q:%d\n", prio, 907 state->old_p, state->old_q); 908 909 /* Turn the IPI hard off */ 910 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01); 911 912 /* 913 * Reset ESB guest mapping. Needed when ESB pages are exposed 914 * to the guest in XIVE native mode 915 */ 916 if (xive->ops && xive->ops->reset_mapped) 917 xive->ops->reset_mapped(kvm, guest_irq); 918 919 /* Grab info about irq */ 920 state->pt_number = hw_irq; 921 state->pt_data = irq_data_get_irq_handler_data(host_data); 922 923 /* 924 * Configure the IRQ to match the existing configuration of 925 * the IPI if it was already targetted. Otherwise this will 926 * mask the interrupt in a lossy way (act_priority is 0xff) 927 * which is fine for a never started interrupt. 928 */ 929 xive_native_configure_irq(hw_irq, 930 kvmppc_xive_vp(xive, state->act_server), 931 state->act_priority, state->number); 932 933 /* 934 * We do an EOI to enable the interrupt (and retrigger if needed) 935 * if the guest has the interrupt unmasked and the P bit was *not* 936 * set in the IPI. If it was set, we know a slot may still be in 937 * use in the target queue thus we have to wait for a guest 938 * originated EOI 939 */ 940 if (prio != MASKED && !state->old_p) 941 xive_vm_source_eoi(hw_irq, state->pt_data); 942 943 /* Clear old_p/old_q as they are no longer relevant */ 944 state->old_p = state->old_q = false; 945 946 /* Restore guest prio (unlocks EOI) */ 947 mb(); 948 state->guest_priority = prio; 949 arch_spin_unlock(&sb->lock); 950 951 return 0; 952 } 953 EXPORT_SYMBOL_GPL(kvmppc_xive_set_mapped); 954 955 int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq, 956 struct irq_desc *host_desc) 957 { 958 struct kvmppc_xive *xive = kvm->arch.xive; 959 struct kvmppc_xive_src_block *sb; 960 struct kvmppc_xive_irq_state *state; 961 unsigned int host_irq = irq_desc_get_irq(host_desc); 962 u16 idx; 963 u8 prio; 964 int rc; 965 966 if (!xive) 967 return -ENODEV; 968 969 pr_devel("clr_mapped girq 0x%lx...\n", guest_irq); 970 971 sb = kvmppc_xive_find_source(xive, guest_irq, &idx); 972 if (!sb) 973 return -EINVAL; 974 state = &sb->irq_state[idx]; 975 976 /* 977 * Mask and read state of IRQ. We need to know if its P bit 978 * is set as that means it's potentially already using a 979 * queue entry in the target 980 */ 981 prio = xive_lock_and_mask(xive, sb, state); 982 pr_devel(" old IRQ prio %02x P:%d Q:%d\n", prio, 983 state->old_p, state->old_q); 984 985 /* 986 * If old_p is set, the interrupt is pending, we switch it to 987 * PQ=11. This will force a resend in the host so the interrupt 988 * isn't lost to whatver host driver may pick it up 989 */ 990 if (state->old_p) 991 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_11); 992 993 /* Release the passed-through interrupt to the host */ 994 rc = irq_set_vcpu_affinity(host_irq, NULL); 995 if (rc) { 996 pr_err("Failed to clr VCPU affinity for irq %d\n", host_irq); 997 return rc; 998 } 999 1000 /* Forget about the IRQ */ 1001 state->pt_number = 0; 1002 state->pt_data = NULL; 1003 1004 /* 1005 * Reset ESB guest mapping. Needed when ESB pages are exposed 1006 * to the guest in XIVE native mode 1007 */ 1008 if (xive->ops && xive->ops->reset_mapped) { 1009 xive->ops->reset_mapped(kvm, guest_irq); 1010 } 1011 1012 /* Reconfigure the IPI */ 1013 xive_native_configure_irq(state->ipi_number, 1014 kvmppc_xive_vp(xive, state->act_server), 1015 state->act_priority, state->number); 1016 1017 /* 1018 * If old_p is set (we have a queue entry potentially 1019 * occupied) or the interrupt is masked, we set the IPI 1020 * to PQ=10 state. Otherwise we just re-enable it (PQ=00). 1021 */ 1022 if (prio == MASKED || state->old_p) 1023 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_10); 1024 else 1025 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_00); 1026 1027 /* Restore guest prio (unlocks EOI) */ 1028 mb(); 1029 state->guest_priority = prio; 1030 arch_spin_unlock(&sb->lock); 1031 1032 return 0; 1033 } 1034 EXPORT_SYMBOL_GPL(kvmppc_xive_clr_mapped); 1035 1036 void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu) 1037 { 1038 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 1039 struct kvm *kvm = vcpu->kvm; 1040 struct kvmppc_xive *xive = kvm->arch.xive; 1041 int i, j; 1042 1043 for (i = 0; i <= xive->max_sbid; i++) { 1044 struct kvmppc_xive_src_block *sb = xive->src_blocks[i]; 1045 1046 if (!sb) 1047 continue; 1048 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) { 1049 struct kvmppc_xive_irq_state *state = &sb->irq_state[j]; 1050 1051 if (!state->valid) 1052 continue; 1053 if (state->act_priority == MASKED) 1054 continue; 1055 if (state->act_server != xc->server_num) 1056 continue; 1057 1058 /* Clean it up */ 1059 arch_spin_lock(&sb->lock); 1060 state->act_priority = MASKED; 1061 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01); 1062 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0); 1063 if (state->pt_number) { 1064 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01); 1065 xive_native_configure_irq(state->pt_number, 0, MASKED, 0); 1066 } 1067 arch_spin_unlock(&sb->lock); 1068 } 1069 } 1070 1071 /* Disable vcpu's escalation interrupt */ 1072 if (vcpu->arch.xive_esc_on) { 1073 __raw_readq((void __iomem *)(vcpu->arch.xive_esc_vaddr + 1074 XIVE_ESB_SET_PQ_01)); 1075 vcpu->arch.xive_esc_on = false; 1076 } 1077 1078 /* 1079 * Clear pointers to escalation interrupt ESB. 1080 * This is safe because the vcpu->mutex is held, preventing 1081 * any other CPU from concurrently executing a KVM_RUN ioctl. 1082 */ 1083 vcpu->arch.xive_esc_vaddr = 0; 1084 vcpu->arch.xive_esc_raddr = 0; 1085 } 1086 1087 /* 1088 * In single escalation mode, the escalation interrupt is marked so 1089 * that EOI doesn't re-enable it, but just sets the stale_p flag to 1090 * indicate that the P bit has already been dealt with. However, the 1091 * assembly code that enters the guest sets PQ to 00 without clearing 1092 * stale_p (because it has no easy way to address it). Hence we have 1093 * to adjust stale_p before shutting down the interrupt. 1094 */ 1095 void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu, 1096 struct kvmppc_xive_vcpu *xc, int irq) 1097 { 1098 struct irq_data *d = irq_get_irq_data(irq); 1099 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d); 1100 1101 /* 1102 * This slightly odd sequence gives the right result 1103 * (i.e. stale_p set if xive_esc_on is false) even if 1104 * we race with xive_esc_irq() and xive_irq_eoi(). 1105 */ 1106 xd->stale_p = false; 1107 smp_mb(); /* paired with smb_wmb in xive_esc_irq */ 1108 if (!vcpu->arch.xive_esc_on) 1109 xd->stale_p = true; 1110 } 1111 1112 void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) 1113 { 1114 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 1115 struct kvmppc_xive *xive = vcpu->kvm->arch.xive; 1116 int i; 1117 1118 if (!kvmppc_xics_enabled(vcpu)) 1119 return; 1120 1121 if (!xc) 1122 return; 1123 1124 pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num); 1125 1126 /* Ensure no interrupt is still routed to that VP */ 1127 xc->valid = false; 1128 kvmppc_xive_disable_vcpu_interrupts(vcpu); 1129 1130 /* Mask the VP IPI */ 1131 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01); 1132 1133 /* Free escalations */ 1134 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) { 1135 if (xc->esc_virq[i]) { 1136 if (xc->xive->single_escalation) 1137 xive_cleanup_single_escalation(vcpu, xc, 1138 xc->esc_virq[i]); 1139 free_irq(xc->esc_virq[i], vcpu); 1140 irq_dispose_mapping(xc->esc_virq[i]); 1141 kfree(xc->esc_virq_names[i]); 1142 } 1143 } 1144 1145 /* Disable the VP */ 1146 xive_native_disable_vp(xc->vp_id); 1147 1148 /* Clear the cam word so guest entry won't try to push context */ 1149 vcpu->arch.xive_cam_word = 0; 1150 1151 /* Free the queues */ 1152 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) { 1153 struct xive_q *q = &xc->queues[i]; 1154 1155 xive_native_disable_queue(xc->vp_id, q, i); 1156 if (q->qpage) { 1157 free_pages((unsigned long)q->qpage, 1158 xive->q_page_order); 1159 q->qpage = NULL; 1160 } 1161 } 1162 1163 /* Free the IPI */ 1164 if (xc->vp_ipi) { 1165 xive_cleanup_irq_data(&xc->vp_ipi_data); 1166 xive_native_free_irq(xc->vp_ipi); 1167 } 1168 /* Free the VP */ 1169 kfree(xc); 1170 1171 /* Cleanup the vcpu */ 1172 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT; 1173 vcpu->arch.xive_vcpu = NULL; 1174 } 1175 1176 static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu) 1177 { 1178 /* We have a block of xive->nr_servers VPs. We just need to check 1179 * packed vCPU ids are below that. 1180 */ 1181 return kvmppc_pack_vcpu_id(xive->kvm, cpu) < xive->nr_servers; 1182 } 1183 1184 int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp) 1185 { 1186 u32 vp_id; 1187 1188 if (!kvmppc_xive_vcpu_id_valid(xive, cpu)) { 1189 pr_devel("Out of bounds !\n"); 1190 return -EINVAL; 1191 } 1192 1193 if (xive->vp_base == XIVE_INVALID_VP) { 1194 xive->vp_base = xive_native_alloc_vp_block(xive->nr_servers); 1195 pr_devel("VP_Base=%x nr_servers=%d\n", xive->vp_base, xive->nr_servers); 1196 1197 if (xive->vp_base == XIVE_INVALID_VP) 1198 return -ENOSPC; 1199 } 1200 1201 vp_id = kvmppc_xive_vp(xive, cpu); 1202 if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) { 1203 pr_devel("Duplicate !\n"); 1204 return -EEXIST; 1205 } 1206 1207 *vp = vp_id; 1208 1209 return 0; 1210 } 1211 1212 int kvmppc_xive_connect_vcpu(struct kvm_device *dev, 1213 struct kvm_vcpu *vcpu, u32 cpu) 1214 { 1215 struct kvmppc_xive *xive = dev->private; 1216 struct kvmppc_xive_vcpu *xc; 1217 int i, r = -EBUSY; 1218 u32 vp_id; 1219 1220 pr_devel("connect_vcpu(cpu=%d)\n", cpu); 1221 1222 if (dev->ops != &kvm_xive_ops) { 1223 pr_devel("Wrong ops !\n"); 1224 return -EPERM; 1225 } 1226 if (xive->kvm != vcpu->kvm) 1227 return -EPERM; 1228 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT) 1229 return -EBUSY; 1230 1231 /* We need to synchronize with queue provisioning */ 1232 mutex_lock(&xive->lock); 1233 1234 r = kvmppc_xive_compute_vp_id(xive, cpu, &vp_id); 1235 if (r) 1236 goto bail; 1237 1238 xc = kzalloc(sizeof(*xc), GFP_KERNEL); 1239 if (!xc) { 1240 r = -ENOMEM; 1241 goto bail; 1242 } 1243 1244 vcpu->arch.xive_vcpu = xc; 1245 xc->xive = xive; 1246 xc->vcpu = vcpu; 1247 xc->server_num = cpu; 1248 xc->vp_id = vp_id; 1249 xc->mfrr = 0xff; 1250 xc->valid = true; 1251 1252 r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id); 1253 if (r) 1254 goto bail; 1255 1256 /* Configure VCPU fields for use by assembly push/pull */ 1257 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000); 1258 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO); 1259 1260 /* Allocate IPI */ 1261 xc->vp_ipi = xive_native_alloc_irq(); 1262 if (!xc->vp_ipi) { 1263 pr_err("Failed to allocate xive irq for VCPU IPI\n"); 1264 r = -EIO; 1265 goto bail; 1266 } 1267 pr_devel(" IPI=0x%x\n", xc->vp_ipi); 1268 1269 r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data); 1270 if (r) 1271 goto bail; 1272 1273 /* 1274 * Enable the VP first as the single escalation mode will 1275 * affect escalation interrupts numbering 1276 */ 1277 r = xive_native_enable_vp(xc->vp_id, xive->single_escalation); 1278 if (r) { 1279 pr_err("Failed to enable VP in OPAL, err %d\n", r); 1280 goto bail; 1281 } 1282 1283 /* 1284 * Initialize queues. Initially we set them all for no queueing 1285 * and we enable escalation for queue 0 only which we'll use for 1286 * our mfrr change notifications. If the VCPU is hot-plugged, we 1287 * do handle provisioning however based on the existing "map" 1288 * of enabled queues. 1289 */ 1290 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) { 1291 struct xive_q *q = &xc->queues[i]; 1292 1293 /* Single escalation, no queue 7 */ 1294 if (i == 7 && xive->single_escalation) 1295 break; 1296 1297 /* Is queue already enabled ? Provision it */ 1298 if (xive->qmap & (1 << i)) { 1299 r = xive_provision_queue(vcpu, i); 1300 if (r == 0 && !xive->single_escalation) 1301 kvmppc_xive_attach_escalation( 1302 vcpu, i, xive->single_escalation); 1303 if (r) 1304 goto bail; 1305 } else { 1306 r = xive_native_configure_queue(xc->vp_id, 1307 q, i, NULL, 0, true); 1308 if (r) { 1309 pr_err("Failed to configure queue %d for VCPU %d\n", 1310 i, cpu); 1311 goto bail; 1312 } 1313 } 1314 } 1315 1316 /* If not done above, attach priority 0 escalation */ 1317 r = kvmppc_xive_attach_escalation(vcpu, 0, xive->single_escalation); 1318 if (r) 1319 goto bail; 1320 1321 /* Route the IPI */ 1322 r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI); 1323 if (!r) 1324 xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00); 1325 1326 bail: 1327 mutex_unlock(&xive->lock); 1328 if (r) { 1329 kvmppc_xive_cleanup_vcpu(vcpu); 1330 return r; 1331 } 1332 1333 vcpu->arch.irq_type = KVMPPC_IRQ_XICS; 1334 return 0; 1335 } 1336 1337 /* 1338 * Scanning of queues before/after migration save 1339 */ 1340 static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq) 1341 { 1342 struct kvmppc_xive_src_block *sb; 1343 struct kvmppc_xive_irq_state *state; 1344 u16 idx; 1345 1346 sb = kvmppc_xive_find_source(xive, irq, &idx); 1347 if (!sb) 1348 return; 1349 1350 state = &sb->irq_state[idx]; 1351 1352 /* Some sanity checking */ 1353 if (!state->valid) { 1354 pr_err("invalid irq 0x%x in cpu queue!\n", irq); 1355 return; 1356 } 1357 1358 /* 1359 * If the interrupt is in a queue it should have P set. 1360 * We warn so that gets reported. A backtrace isn't useful 1361 * so no need to use a WARN_ON. 1362 */ 1363 if (!state->saved_p) 1364 pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq); 1365 1366 /* Set flag */ 1367 state->in_queue = true; 1368 } 1369 1370 static void xive_pre_save_mask_irq(struct kvmppc_xive *xive, 1371 struct kvmppc_xive_src_block *sb, 1372 u32 irq) 1373 { 1374 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq]; 1375 1376 if (!state->valid) 1377 return; 1378 1379 /* Mask and save state, this will also sync HW queues */ 1380 state->saved_scan_prio = xive_lock_and_mask(xive, sb, state); 1381 1382 /* Transfer P and Q */ 1383 state->saved_p = state->old_p; 1384 state->saved_q = state->old_q; 1385 1386 /* Unlock */ 1387 arch_spin_unlock(&sb->lock); 1388 } 1389 1390 static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive, 1391 struct kvmppc_xive_src_block *sb, 1392 u32 irq) 1393 { 1394 struct kvmppc_xive_irq_state *state = &sb->irq_state[irq]; 1395 1396 if (!state->valid) 1397 return; 1398 1399 /* 1400 * Lock / exclude EOI (not technically necessary if the 1401 * guest isn't running concurrently. If this becomes a 1402 * performance issue we can probably remove the lock. 1403 */ 1404 xive_lock_for_unmask(sb, state); 1405 1406 /* Restore mask/prio if it wasn't masked */ 1407 if (state->saved_scan_prio != MASKED) 1408 xive_finish_unmask(xive, sb, state, state->saved_scan_prio); 1409 1410 /* Unlock */ 1411 arch_spin_unlock(&sb->lock); 1412 } 1413 1414 static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q) 1415 { 1416 u32 idx = q->idx; 1417 u32 toggle = q->toggle; 1418 u32 irq; 1419 1420 do { 1421 irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle); 1422 if (irq > XICS_IPI) 1423 xive_pre_save_set_queued(xive, irq); 1424 } while(irq); 1425 } 1426 1427 static void xive_pre_save_scan(struct kvmppc_xive *xive) 1428 { 1429 struct kvm_vcpu *vcpu = NULL; 1430 int i, j; 1431 1432 /* 1433 * See comment in xive_get_source() about how this 1434 * work. Collect a stable state for all interrupts 1435 */ 1436 for (i = 0; i <= xive->max_sbid; i++) { 1437 struct kvmppc_xive_src_block *sb = xive->src_blocks[i]; 1438 if (!sb) 1439 continue; 1440 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) 1441 xive_pre_save_mask_irq(xive, sb, j); 1442 } 1443 1444 /* Then scan the queues and update the "in_queue" flag */ 1445 kvm_for_each_vcpu(i, vcpu, xive->kvm) { 1446 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 1447 if (!xc) 1448 continue; 1449 for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) { 1450 if (xc->queues[j].qpage) 1451 xive_pre_save_queue(xive, &xc->queues[j]); 1452 } 1453 } 1454 1455 /* Finally restore interrupt states */ 1456 for (i = 0; i <= xive->max_sbid; i++) { 1457 struct kvmppc_xive_src_block *sb = xive->src_blocks[i]; 1458 if (!sb) 1459 continue; 1460 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) 1461 xive_pre_save_unmask_irq(xive, sb, j); 1462 } 1463 } 1464 1465 static void xive_post_save_scan(struct kvmppc_xive *xive) 1466 { 1467 u32 i, j; 1468 1469 /* Clear all the in_queue flags */ 1470 for (i = 0; i <= xive->max_sbid; i++) { 1471 struct kvmppc_xive_src_block *sb = xive->src_blocks[i]; 1472 if (!sb) 1473 continue; 1474 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) 1475 sb->irq_state[j].in_queue = false; 1476 } 1477 1478 /* Next get_source() will do a new scan */ 1479 xive->saved_src_count = 0; 1480 } 1481 1482 /* 1483 * This returns the source configuration and state to user space. 1484 */ 1485 static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr) 1486 { 1487 struct kvmppc_xive_src_block *sb; 1488 struct kvmppc_xive_irq_state *state; 1489 u64 __user *ubufp = (u64 __user *) addr; 1490 u64 val, prio; 1491 u16 idx; 1492 1493 sb = kvmppc_xive_find_source(xive, irq, &idx); 1494 if (!sb) 1495 return -ENOENT; 1496 1497 state = &sb->irq_state[idx]; 1498 1499 if (!state->valid) 1500 return -ENOENT; 1501 1502 pr_devel("get_source(%ld)...\n", irq); 1503 1504 /* 1505 * So to properly save the state into something that looks like a 1506 * XICS migration stream we cannot treat interrupts individually. 1507 * 1508 * We need, instead, mask them all (& save their previous PQ state) 1509 * to get a stable state in the HW, then sync them to ensure that 1510 * any interrupt that had already fired hits its queue, and finally 1511 * scan all the queues to collect which interrupts are still present 1512 * in the queues, so we can set the "pending" flag on them and 1513 * they can be resent on restore. 1514 * 1515 * So we do it all when the "first" interrupt gets saved, all the 1516 * state is collected at that point, the rest of xive_get_source() 1517 * will merely collect and convert that state to the expected 1518 * userspace bit mask. 1519 */ 1520 if (xive->saved_src_count == 0) 1521 xive_pre_save_scan(xive); 1522 xive->saved_src_count++; 1523 1524 /* Convert saved state into something compatible with xics */ 1525 val = state->act_server; 1526 prio = state->saved_scan_prio; 1527 1528 if (prio == MASKED) { 1529 val |= KVM_XICS_MASKED; 1530 prio = state->saved_priority; 1531 } 1532 val |= prio << KVM_XICS_PRIORITY_SHIFT; 1533 if (state->lsi) { 1534 val |= KVM_XICS_LEVEL_SENSITIVE; 1535 if (state->saved_p) 1536 val |= KVM_XICS_PENDING; 1537 } else { 1538 if (state->saved_p) 1539 val |= KVM_XICS_PRESENTED; 1540 1541 if (state->saved_q) 1542 val |= KVM_XICS_QUEUED; 1543 1544 /* 1545 * We mark it pending (which will attempt a re-delivery) 1546 * if we are in a queue *or* we were masked and had 1547 * Q set which is equivalent to the XICS "masked pending" 1548 * state 1549 */ 1550 if (state->in_queue || (prio == MASKED && state->saved_q)) 1551 val |= KVM_XICS_PENDING; 1552 } 1553 1554 /* 1555 * If that was the last interrupt saved, reset the 1556 * in_queue flags 1557 */ 1558 if (xive->saved_src_count == xive->src_count) 1559 xive_post_save_scan(xive); 1560 1561 /* Copy the result to userspace */ 1562 if (put_user(val, ubufp)) 1563 return -EFAULT; 1564 1565 return 0; 1566 } 1567 1568 struct kvmppc_xive_src_block *kvmppc_xive_create_src_block( 1569 struct kvmppc_xive *xive, int irq) 1570 { 1571 struct kvmppc_xive_src_block *sb; 1572 int i, bid; 1573 1574 bid = irq >> KVMPPC_XICS_ICS_SHIFT; 1575 1576 mutex_lock(&xive->lock); 1577 1578 /* block already exists - somebody else got here first */ 1579 if (xive->src_blocks[bid]) 1580 goto out; 1581 1582 /* Create the ICS */ 1583 sb = kzalloc(sizeof(*sb), GFP_KERNEL); 1584 if (!sb) 1585 goto out; 1586 1587 sb->id = bid; 1588 1589 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) { 1590 sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i; 1591 sb->irq_state[i].eisn = 0; 1592 sb->irq_state[i].guest_priority = MASKED; 1593 sb->irq_state[i].saved_priority = MASKED; 1594 sb->irq_state[i].act_priority = MASKED; 1595 } 1596 smp_wmb(); 1597 xive->src_blocks[bid] = sb; 1598 1599 if (bid > xive->max_sbid) 1600 xive->max_sbid = bid; 1601 1602 out: 1603 mutex_unlock(&xive->lock); 1604 return xive->src_blocks[bid]; 1605 } 1606 1607 static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq) 1608 { 1609 struct kvm *kvm = xive->kvm; 1610 struct kvm_vcpu *vcpu = NULL; 1611 int i; 1612 1613 kvm_for_each_vcpu(i, vcpu, kvm) { 1614 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 1615 1616 if (!xc) 1617 continue; 1618 1619 if (xc->delayed_irq == irq) { 1620 xc->delayed_irq = 0; 1621 xive->delayed_irqs--; 1622 return true; 1623 } 1624 } 1625 return false; 1626 } 1627 1628 static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr) 1629 { 1630 struct kvmppc_xive_src_block *sb; 1631 struct kvmppc_xive_irq_state *state; 1632 u64 __user *ubufp = (u64 __user *) addr; 1633 u16 idx; 1634 u64 val; 1635 u8 act_prio, guest_prio; 1636 u32 server; 1637 int rc = 0; 1638 1639 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS) 1640 return -ENOENT; 1641 1642 pr_devel("set_source(irq=0x%lx)\n", irq); 1643 1644 /* Find the source */ 1645 sb = kvmppc_xive_find_source(xive, irq, &idx); 1646 if (!sb) { 1647 pr_devel("No source, creating source block...\n"); 1648 sb = kvmppc_xive_create_src_block(xive, irq); 1649 if (!sb) { 1650 pr_devel("Failed to create block...\n"); 1651 return -ENOMEM; 1652 } 1653 } 1654 state = &sb->irq_state[idx]; 1655 1656 /* Read user passed data */ 1657 if (get_user(val, ubufp)) { 1658 pr_devel("fault getting user info !\n"); 1659 return -EFAULT; 1660 } 1661 1662 server = val & KVM_XICS_DESTINATION_MASK; 1663 guest_prio = val >> KVM_XICS_PRIORITY_SHIFT; 1664 1665 pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n", 1666 val, server, guest_prio); 1667 1668 /* 1669 * If the source doesn't already have an IPI, allocate 1670 * one and get the corresponding data 1671 */ 1672 if (!state->ipi_number) { 1673 state->ipi_number = xive_native_alloc_irq(); 1674 if (state->ipi_number == 0) { 1675 pr_devel("Failed to allocate IPI !\n"); 1676 return -ENOMEM; 1677 } 1678 xive_native_populate_irq_data(state->ipi_number, &state->ipi_data); 1679 pr_devel(" src_ipi=0x%x\n", state->ipi_number); 1680 } 1681 1682 /* 1683 * We use lock_and_mask() to set us in the right masked 1684 * state. We will override that state from the saved state 1685 * further down, but this will handle the cases of interrupts 1686 * that need FW masking. We set the initial guest_priority to 1687 * 0 before calling it to ensure it actually performs the masking. 1688 */ 1689 state->guest_priority = 0; 1690 xive_lock_and_mask(xive, sb, state); 1691 1692 /* 1693 * Now, we select a target if we have one. If we don't we 1694 * leave the interrupt untargetted. It means that an interrupt 1695 * can become "untargetted" accross migration if it was masked 1696 * by set_xive() but there is little we can do about it. 1697 */ 1698 1699 /* First convert prio and mark interrupt as untargetted */ 1700 act_prio = xive_prio_from_guest(guest_prio); 1701 state->act_priority = MASKED; 1702 1703 /* 1704 * We need to drop the lock due to the mutex below. Hopefully 1705 * nothing is touching that interrupt yet since it hasn't been 1706 * advertized to a running guest yet 1707 */ 1708 arch_spin_unlock(&sb->lock); 1709 1710 /* If we have a priority target the interrupt */ 1711 if (act_prio != MASKED) { 1712 /* First, check provisioning of queues */ 1713 mutex_lock(&xive->lock); 1714 rc = xive_check_provisioning(xive->kvm, act_prio); 1715 mutex_unlock(&xive->lock); 1716 1717 /* Target interrupt */ 1718 if (rc == 0) 1719 rc = xive_target_interrupt(xive->kvm, state, 1720 server, act_prio); 1721 /* 1722 * If provisioning or targetting failed, leave it 1723 * alone and masked. It will remain disabled until 1724 * the guest re-targets it. 1725 */ 1726 } 1727 1728 /* 1729 * Find out if this was a delayed irq stashed in an ICP, 1730 * in which case, treat it as pending 1731 */ 1732 if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) { 1733 val |= KVM_XICS_PENDING; 1734 pr_devel(" Found delayed ! forcing PENDING !\n"); 1735 } 1736 1737 /* Cleanup the SW state */ 1738 state->old_p = false; 1739 state->old_q = false; 1740 state->lsi = false; 1741 state->asserted = false; 1742 1743 /* Restore LSI state */ 1744 if (val & KVM_XICS_LEVEL_SENSITIVE) { 1745 state->lsi = true; 1746 if (val & KVM_XICS_PENDING) 1747 state->asserted = true; 1748 pr_devel(" LSI ! Asserted=%d\n", state->asserted); 1749 } 1750 1751 /* 1752 * Restore P and Q. If the interrupt was pending, we 1753 * force Q and !P, which will trigger a resend. 1754 * 1755 * That means that a guest that had both an interrupt 1756 * pending (queued) and Q set will restore with only 1757 * one instance of that interrupt instead of 2, but that 1758 * is perfectly fine as coalescing interrupts that haven't 1759 * been presented yet is always allowed. 1760 */ 1761 if (val & KVM_XICS_PRESENTED && !(val & KVM_XICS_PENDING)) 1762 state->old_p = true; 1763 if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING) 1764 state->old_q = true; 1765 1766 pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q); 1767 1768 /* 1769 * If the interrupt was unmasked, update guest priority and 1770 * perform the appropriate state transition and do a 1771 * re-trigger if necessary. 1772 */ 1773 if (val & KVM_XICS_MASKED) { 1774 pr_devel(" masked, saving prio\n"); 1775 state->guest_priority = MASKED; 1776 state->saved_priority = guest_prio; 1777 } else { 1778 pr_devel(" unmasked, restoring to prio %d\n", guest_prio); 1779 xive_finish_unmask(xive, sb, state, guest_prio); 1780 state->saved_priority = guest_prio; 1781 } 1782 1783 /* Increment the number of valid sources and mark this one valid */ 1784 if (!state->valid) 1785 xive->src_count++; 1786 state->valid = true; 1787 1788 return 0; 1789 } 1790 1791 int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, 1792 bool line_status) 1793 { 1794 struct kvmppc_xive *xive = kvm->arch.xive; 1795 struct kvmppc_xive_src_block *sb; 1796 struct kvmppc_xive_irq_state *state; 1797 u16 idx; 1798 1799 if (!xive) 1800 return -ENODEV; 1801 1802 sb = kvmppc_xive_find_source(xive, irq, &idx); 1803 if (!sb) 1804 return -EINVAL; 1805 1806 /* Perform locklessly .... (we need to do some RCUisms here...) */ 1807 state = &sb->irq_state[idx]; 1808 if (!state->valid) 1809 return -EINVAL; 1810 1811 /* We don't allow a trigger on a passed-through interrupt */ 1812 if (state->pt_number) 1813 return -EINVAL; 1814 1815 if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL) 1816 state->asserted = 1; 1817 else if (level == 0 || level == KVM_INTERRUPT_UNSET) { 1818 state->asserted = 0; 1819 return 0; 1820 } 1821 1822 /* Trigger the IPI */ 1823 xive_irq_trigger(&state->ipi_data); 1824 1825 return 0; 1826 } 1827 1828 int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr) 1829 { 1830 u32 __user *ubufp = (u32 __user *) addr; 1831 u32 nr_servers; 1832 int rc = 0; 1833 1834 if (get_user(nr_servers, ubufp)) 1835 return -EFAULT; 1836 1837 pr_devel("%s nr_servers=%u\n", __func__, nr_servers); 1838 1839 if (!nr_servers || nr_servers > KVM_MAX_VCPU_ID) 1840 return -EINVAL; 1841 1842 mutex_lock(&xive->lock); 1843 if (xive->vp_base != XIVE_INVALID_VP) 1844 /* The VP block is allocated once and freed when the device 1845 * is released. Better not allow to change its size since its 1846 * used by connect_vcpu to validate vCPU ids are valid (eg, 1847 * setting it back to a higher value could allow connect_vcpu 1848 * to come up with a VP id that goes beyond the VP block, which 1849 * is likely to cause a crash in OPAL). 1850 */ 1851 rc = -EBUSY; 1852 else if (nr_servers > KVM_MAX_VCPUS) 1853 /* We don't need more servers. Higher vCPU ids get packed 1854 * down below KVM_MAX_VCPUS by kvmppc_pack_vcpu_id(). 1855 */ 1856 xive->nr_servers = KVM_MAX_VCPUS; 1857 else 1858 xive->nr_servers = nr_servers; 1859 1860 mutex_unlock(&xive->lock); 1861 1862 return rc; 1863 } 1864 1865 static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) 1866 { 1867 struct kvmppc_xive *xive = dev->private; 1868 1869 /* We honor the existing XICS ioctl */ 1870 switch (attr->group) { 1871 case KVM_DEV_XICS_GRP_SOURCES: 1872 return xive_set_source(xive, attr->attr, attr->addr); 1873 case KVM_DEV_XICS_GRP_CTRL: 1874 switch (attr->attr) { 1875 case KVM_DEV_XICS_NR_SERVERS: 1876 return kvmppc_xive_set_nr_servers(xive, attr->addr); 1877 } 1878 } 1879 return -ENXIO; 1880 } 1881 1882 static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) 1883 { 1884 struct kvmppc_xive *xive = dev->private; 1885 1886 /* We honor the existing XICS ioctl */ 1887 switch (attr->group) { 1888 case KVM_DEV_XICS_GRP_SOURCES: 1889 return xive_get_source(xive, attr->attr, attr->addr); 1890 } 1891 return -ENXIO; 1892 } 1893 1894 static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr) 1895 { 1896 /* We honor the same limits as XICS, at least for now */ 1897 switch (attr->group) { 1898 case KVM_DEV_XICS_GRP_SOURCES: 1899 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ && 1900 attr->attr < KVMPPC_XICS_NR_IRQS) 1901 return 0; 1902 break; 1903 case KVM_DEV_XICS_GRP_CTRL: 1904 switch (attr->attr) { 1905 case KVM_DEV_XICS_NR_SERVERS: 1906 return 0; 1907 } 1908 } 1909 return -ENXIO; 1910 } 1911 1912 static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd) 1913 { 1914 xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01); 1915 xive_native_configure_irq(hw_num, 0, MASKED, 0); 1916 } 1917 1918 void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb) 1919 { 1920 int i; 1921 1922 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) { 1923 struct kvmppc_xive_irq_state *state = &sb->irq_state[i]; 1924 1925 if (!state->valid) 1926 continue; 1927 1928 kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data); 1929 xive_cleanup_irq_data(&state->ipi_data); 1930 xive_native_free_irq(state->ipi_number); 1931 1932 /* Pass-through, cleanup too but keep IRQ hw data */ 1933 if (state->pt_number) 1934 kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data); 1935 1936 state->valid = false; 1937 } 1938 } 1939 1940 /* 1941 * Called when device fd is closed. kvm->lock is held. 1942 */ 1943 static void kvmppc_xive_release(struct kvm_device *dev) 1944 { 1945 struct kvmppc_xive *xive = dev->private; 1946 struct kvm *kvm = xive->kvm; 1947 struct kvm_vcpu *vcpu; 1948 int i; 1949 1950 pr_devel("Releasing xive device\n"); 1951 1952 /* 1953 * Since this is the device release function, we know that 1954 * userspace does not have any open fd referring to the 1955 * device. Therefore there can not be any of the device 1956 * attribute set/get functions being executed concurrently, 1957 * and similarly, the connect_vcpu and set/clr_mapped 1958 * functions also cannot be being executed. 1959 */ 1960 1961 debugfs_remove(xive->dentry); 1962 1963 /* 1964 * We should clean up the vCPU interrupt presenters first. 1965 */ 1966 kvm_for_each_vcpu(i, vcpu, kvm) { 1967 /* 1968 * Take vcpu->mutex to ensure that no one_reg get/set ioctl 1969 * (i.e. kvmppc_xive_[gs]et_icp) can be done concurrently. 1970 * Holding the vcpu->mutex also means that the vcpu cannot 1971 * be executing the KVM_RUN ioctl, and therefore it cannot 1972 * be executing the XIVE push or pull code or accessing 1973 * the XIVE MMIO regions. 1974 */ 1975 mutex_lock(&vcpu->mutex); 1976 kvmppc_xive_cleanup_vcpu(vcpu); 1977 mutex_unlock(&vcpu->mutex); 1978 } 1979 1980 /* 1981 * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type 1982 * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe 1983 * against xive code getting called during vcpu execution or 1984 * set/get one_reg operations. 1985 */ 1986 kvm->arch.xive = NULL; 1987 1988 /* Mask and free interrupts */ 1989 for (i = 0; i <= xive->max_sbid; i++) { 1990 if (xive->src_blocks[i]) 1991 kvmppc_xive_free_sources(xive->src_blocks[i]); 1992 kfree(xive->src_blocks[i]); 1993 xive->src_blocks[i] = NULL; 1994 } 1995 1996 if (xive->vp_base != XIVE_INVALID_VP) 1997 xive_native_free_vp_block(xive->vp_base); 1998 1999 /* 2000 * A reference of the kvmppc_xive pointer is now kept under 2001 * the xive_devices struct of the machine for reuse. It is 2002 * freed when the VM is destroyed for now until we fix all the 2003 * execution paths. 2004 */ 2005 2006 kfree(dev); 2007 } 2008 2009 /* 2010 * When the guest chooses the interrupt mode (XICS legacy or XIVE 2011 * native), the VM will switch of KVM device. The previous device will 2012 * be "released" before the new one is created. 2013 * 2014 * Until we are sure all execution paths are well protected, provide a 2015 * fail safe (transitional) method for device destruction, in which 2016 * the XIVE device pointer is recycled and not directly freed. 2017 */ 2018 struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type) 2019 { 2020 struct kvmppc_xive **kvm_xive_device = type == KVM_DEV_TYPE_XIVE ? 2021 &kvm->arch.xive_devices.native : 2022 &kvm->arch.xive_devices.xics_on_xive; 2023 struct kvmppc_xive *xive = *kvm_xive_device; 2024 2025 if (!xive) { 2026 xive = kzalloc(sizeof(*xive), GFP_KERNEL); 2027 *kvm_xive_device = xive; 2028 } else { 2029 memset(xive, 0, sizeof(*xive)); 2030 } 2031 2032 return xive; 2033 } 2034 2035 /* 2036 * Create a XICS device with XIVE backend. kvm->lock is held. 2037 */ 2038 static int kvmppc_xive_create(struct kvm_device *dev, u32 type) 2039 { 2040 struct kvmppc_xive *xive; 2041 struct kvm *kvm = dev->kvm; 2042 2043 pr_devel("Creating xive for partition\n"); 2044 2045 /* Already there ? */ 2046 if (kvm->arch.xive) 2047 return -EEXIST; 2048 2049 xive = kvmppc_xive_get_device(kvm, type); 2050 if (!xive) 2051 return -ENOMEM; 2052 2053 dev->private = xive; 2054 xive->dev = dev; 2055 xive->kvm = kvm; 2056 mutex_init(&xive->lock); 2057 2058 /* We use the default queue size set by the host */ 2059 xive->q_order = xive_native_default_eq_shift(); 2060 if (xive->q_order < PAGE_SHIFT) 2061 xive->q_page_order = 0; 2062 else 2063 xive->q_page_order = xive->q_order - PAGE_SHIFT; 2064 2065 /* VP allocation is delayed to the first call to connect_vcpu */ 2066 xive->vp_base = XIVE_INVALID_VP; 2067 /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets 2068 * on a POWER9 system. 2069 */ 2070 xive->nr_servers = KVM_MAX_VCPUS; 2071 2072 xive->single_escalation = xive_native_has_single_escalation(); 2073 2074 kvm->arch.xive = xive; 2075 return 0; 2076 } 2077 2078 int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu) 2079 { 2080 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 2081 unsigned int i; 2082 2083 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) { 2084 struct xive_q *q = &xc->queues[i]; 2085 u32 i0, i1, idx; 2086 2087 if (!q->qpage && !xc->esc_virq[i]) 2088 continue; 2089 2090 if (q->qpage) { 2091 seq_printf(m, " q[%d]: ", i); 2092 idx = q->idx; 2093 i0 = be32_to_cpup(q->qpage + idx); 2094 idx = (idx + 1) & q->msk; 2095 i1 = be32_to_cpup(q->qpage + idx); 2096 seq_printf(m, "T=%d %08x %08x...\n", q->toggle, 2097 i0, i1); 2098 } 2099 if (xc->esc_virq[i]) { 2100 struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]); 2101 struct xive_irq_data *xd = 2102 irq_data_get_irq_handler_data(d); 2103 u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET); 2104 2105 seq_printf(m, " ESC %d %c%c EOI @%llx", 2106 xc->esc_virq[i], 2107 (pq & XIVE_ESB_VAL_P) ? 'P' : '-', 2108 (pq & XIVE_ESB_VAL_Q) ? 'Q' : '-', 2109 xd->eoi_page); 2110 seq_puts(m, "\n"); 2111 } 2112 } 2113 return 0; 2114 } 2115 2116 void kvmppc_xive_debug_show_sources(struct seq_file *m, 2117 struct kvmppc_xive_src_block *sb) 2118 { 2119 int i; 2120 2121 seq_puts(m, " LISN HW/CHIP TYPE PQ EISN CPU/PRIO\n"); 2122 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) { 2123 struct kvmppc_xive_irq_state *state = &sb->irq_state[i]; 2124 struct xive_irq_data *xd; 2125 u64 pq; 2126 u32 hw_num; 2127 2128 if (!state->valid) 2129 continue; 2130 2131 kvmppc_xive_select_irq(state, &hw_num, &xd); 2132 2133 pq = xive_vm_esb_load(xd, XIVE_ESB_GET); 2134 2135 seq_printf(m, "%08x %08x/%02x", state->number, hw_num, 2136 xd->src_chip); 2137 if (state->lsi) 2138 seq_printf(m, " %cLSI", state->asserted ? '^' : ' '); 2139 else 2140 seq_puts(m, " MSI"); 2141 2142 seq_printf(m, " %s %c%c %08x % 4d/%d", 2143 state->ipi_number == hw_num ? "IPI" : " PT", 2144 pq & XIVE_ESB_VAL_P ? 'P' : '-', 2145 pq & XIVE_ESB_VAL_Q ? 'Q' : '-', 2146 state->eisn, state->act_server, 2147 state->act_priority); 2148 2149 seq_puts(m, "\n"); 2150 } 2151 } 2152 2153 static int xive_debug_show(struct seq_file *m, void *private) 2154 { 2155 struct kvmppc_xive *xive = m->private; 2156 struct kvm *kvm = xive->kvm; 2157 struct kvm_vcpu *vcpu; 2158 u64 t_rm_h_xirr = 0; 2159 u64 t_rm_h_ipoll = 0; 2160 u64 t_rm_h_cppr = 0; 2161 u64 t_rm_h_eoi = 0; 2162 u64 t_rm_h_ipi = 0; 2163 u64 t_vm_h_xirr = 0; 2164 u64 t_vm_h_ipoll = 0; 2165 u64 t_vm_h_cppr = 0; 2166 u64 t_vm_h_eoi = 0; 2167 u64 t_vm_h_ipi = 0; 2168 unsigned int i; 2169 2170 if (!kvm) 2171 return 0; 2172 2173 seq_puts(m, "=========\nVCPU state\n=========\n"); 2174 2175 kvm_for_each_vcpu(i, vcpu, kvm) { 2176 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; 2177 2178 if (!xc) 2179 continue; 2180 2181 seq_printf(m, "VCPU %d: VP:%#x/%02x\n" 2182 " CPPR:%#x HWCPPR:%#x MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n", 2183 xc->server_num, xc->vp_id, xc->vp_chip_id, 2184 xc->cppr, xc->hw_cppr, 2185 xc->mfrr, xc->pending, 2186 xc->stat_rm_h_xirr, xc->stat_vm_h_xirr); 2187 2188 kvmppc_xive_debug_show_queues(m, vcpu); 2189 2190 t_rm_h_xirr += xc->stat_rm_h_xirr; 2191 t_rm_h_ipoll += xc->stat_rm_h_ipoll; 2192 t_rm_h_cppr += xc->stat_rm_h_cppr; 2193 t_rm_h_eoi += xc->stat_rm_h_eoi; 2194 t_rm_h_ipi += xc->stat_rm_h_ipi; 2195 t_vm_h_xirr += xc->stat_vm_h_xirr; 2196 t_vm_h_ipoll += xc->stat_vm_h_ipoll; 2197 t_vm_h_cppr += xc->stat_vm_h_cppr; 2198 t_vm_h_eoi += xc->stat_vm_h_eoi; 2199 t_vm_h_ipi += xc->stat_vm_h_ipi; 2200 } 2201 2202 seq_puts(m, "Hcalls totals\n"); 2203 seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr); 2204 seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll); 2205 seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr); 2206 seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi); 2207 seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi); 2208 2209 seq_puts(m, "=========\nSources\n=========\n"); 2210 2211 for (i = 0; i <= xive->max_sbid; i++) { 2212 struct kvmppc_xive_src_block *sb = xive->src_blocks[i]; 2213 2214 if (sb) { 2215 arch_spin_lock(&sb->lock); 2216 kvmppc_xive_debug_show_sources(m, sb); 2217 arch_spin_unlock(&sb->lock); 2218 } 2219 } 2220 2221 return 0; 2222 } 2223 2224 DEFINE_SHOW_ATTRIBUTE(xive_debug); 2225 2226 static void xive_debugfs_init(struct kvmppc_xive *xive) 2227 { 2228 char *name; 2229 2230 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive); 2231 if (!name) { 2232 pr_err("%s: no memory for name\n", __func__); 2233 return; 2234 } 2235 2236 xive->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root, 2237 xive, &xive_debug_fops); 2238 2239 pr_debug("%s: created %s\n", __func__, name); 2240 kfree(name); 2241 } 2242 2243 static void kvmppc_xive_init(struct kvm_device *dev) 2244 { 2245 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private; 2246 2247 /* Register some debug interfaces */ 2248 xive_debugfs_init(xive); 2249 } 2250 2251 struct kvm_device_ops kvm_xive_ops = { 2252 .name = "kvm-xive", 2253 .create = kvmppc_xive_create, 2254 .init = kvmppc_xive_init, 2255 .release = kvmppc_xive_release, 2256 .set_attr = xive_set_attr, 2257 .get_attr = xive_get_attr, 2258 .has_attr = xive_has_attr, 2259 }; 2260 2261 void kvmppc_xive_init_module(void) 2262 { 2263 __xive_vm_h_xirr = xive_vm_h_xirr; 2264 __xive_vm_h_ipoll = xive_vm_h_ipoll; 2265 __xive_vm_h_ipi = xive_vm_h_ipi; 2266 __xive_vm_h_cppr = xive_vm_h_cppr; 2267 __xive_vm_h_eoi = xive_vm_h_eoi; 2268 } 2269 2270 void kvmppc_xive_exit_module(void) 2271 { 2272 __xive_vm_h_xirr = NULL; 2273 __xive_vm_h_ipoll = NULL; 2274 __xive_vm_h_ipi = NULL; 2275 __xive_vm_h_cppr = NULL; 2276 __xive_vm_h_eoi = NULL; 2277 } 2278