1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2019, IBM Corporation.
6 #define pr_fmt(fmt) "xive-kvm: " fmt
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/file.h>
15 #include <linux/irqdomain.h>
16 #include <asm/uaccess.h>
17 #include <asm/kvm_book3s.h>
18 #include <asm/kvm_ppc.h>
19 #include <asm/hvcall.h>
21 #include <asm/xive-regs.h>
22 #include <asm/debug.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
28 #include "book3s_xive.h"
30 static u8 xive_vm_esb_load(struct xive_irq_data *xd, u32 offset)
35 * The KVM XIVE native device does not use the XIVE_ESB_SET_PQ_10
36 * load operation, so there is no need to enforce load-after-store
40 val = in_be64(xd->eoi_mmio + offset);
44 static void kvmppc_xive_native_cleanup_queue(struct kvm_vcpu *vcpu, int prio)
46 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
47 struct xive_q *q = &xc->queues[prio];
49 xive_native_disable_queue(xc->vp_id, q, prio);
51 put_page(virt_to_page(q->qpage));
56 static int kvmppc_xive_native_configure_queue(u32 vp_id, struct xive_q *q,
57 u8 prio, __be32 *qpage,
58 u32 order, bool can_escalate)
61 __be32 *qpage_prev = q->qpage;
63 rc = xive_native_configure_queue(vp_id, q, prio, qpage, order,
69 put_page(virt_to_page(qpage_prev));
74 void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu)
76 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
79 if (!kvmppc_xive_enabled(vcpu))
85 pr_devel("native_cleanup_vcpu(cpu=%d)\n", xc->server_num);
87 /* Ensure no interrupt is still routed to that VP */
89 kvmppc_xive_disable_vcpu_interrupts(vcpu);
91 /* Free escalations */
92 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
93 /* Free the escalation irq */
94 if (xc->esc_virq[i]) {
95 if (kvmppc_xive_has_single_escalation(xc->xive))
96 xive_cleanup_single_escalation(vcpu, xc,
98 free_irq(xc->esc_virq[i], vcpu);
99 irq_dispose_mapping(xc->esc_virq[i]);
100 kfree(xc->esc_virq_names[i]);
106 xive_native_disable_vp(xc->vp_id);
108 /* Clear the cam word so guest entry won't try to push context */
109 vcpu->arch.xive_cam_word = 0;
111 /* Free the queues */
112 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
113 kvmppc_xive_native_cleanup_queue(vcpu, i);
119 /* Cleanup the vcpu */
120 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
121 vcpu->arch.xive_vcpu = NULL;
124 int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
125 struct kvm_vcpu *vcpu, u32 server_num)
127 struct kvmppc_xive *xive = dev->private;
128 struct kvmppc_xive_vcpu *xc = NULL;
132 pr_devel("native_connect_vcpu(server=%d)\n", server_num);
134 if (dev->ops != &kvm_xive_native_ops) {
135 pr_devel("Wrong ops !\n");
138 if (xive->kvm != vcpu->kvm)
140 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
143 mutex_lock(&xive->lock);
145 rc = kvmppc_xive_compute_vp_id(xive, server_num, &vp_id);
149 xc = kzalloc(sizeof(*xc), GFP_KERNEL);
155 vcpu->arch.xive_vcpu = xc;
158 xc->server_num = server_num;
162 vcpu->arch.irq_type = KVMPPC_IRQ_XIVE;
164 rc = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
166 pr_err("Failed to get VP info from OPAL: %d\n", rc);
170 if (!kvmppc_xive_check_save_restore(vcpu)) {
171 pr_err("inconsistent save-restore setup for VCPU %d\n", server_num);
177 * Enable the VP first as the single escalation mode will
178 * affect escalation interrupts numbering
180 rc = xive_native_enable_vp(xc->vp_id, kvmppc_xive_has_single_escalation(xive));
182 pr_err("Failed to enable VP in OPAL: %d\n", rc);
186 /* Configure VCPU fields for use by assembly push/pull */
187 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
188 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
190 /* TODO: reset all queues to a clean state ? */
192 mutex_unlock(&xive->lock);
194 kvmppc_xive_native_cleanup_vcpu(vcpu);
200 * Device passthrough support
202 static int kvmppc_xive_native_reset_mapped(struct kvm *kvm, unsigned long irq)
204 struct kvmppc_xive *xive = kvm->arch.xive;
205 pgoff_t esb_pgoff = KVM_XIVE_ESB_PAGE_OFFSET + irq * 2;
207 if (irq >= KVMPPC_XIVE_NR_IRQS)
211 * Clear the ESB pages of the IRQ number being mapped (or
212 * unmapped) into the guest and let the the VM fault handler
213 * repopulate with the appropriate ESB pages (device or IC)
215 pr_debug("clearing esb pages for girq 0x%lx\n", irq);
216 mutex_lock(&xive->mapping_lock);
218 unmap_mapping_range(xive->mapping,
219 esb_pgoff << PAGE_SHIFT,
220 2ull << PAGE_SHIFT, 1);
221 mutex_unlock(&xive->mapping_lock);
225 static struct kvmppc_xive_ops kvmppc_xive_native_ops = {
226 .reset_mapped = kvmppc_xive_native_reset_mapped,
229 static vm_fault_t xive_native_esb_fault(struct vm_fault *vmf)
231 struct vm_area_struct *vma = vmf->vma;
232 struct kvm_device *dev = vma->vm_file->private_data;
233 struct kvmppc_xive *xive = dev->private;
234 struct kvmppc_xive_src_block *sb;
235 struct kvmppc_xive_irq_state *state;
236 struct xive_irq_data *xd;
244 * Linux/KVM uses a two pages ESB setting, one for trigger and
247 page_offset = vmf->pgoff - vma->vm_pgoff;
248 irq = page_offset / 2;
250 sb = kvmppc_xive_find_source(xive, irq, &src);
252 pr_devel("%s: source %lx not found !\n", __func__, irq);
253 return VM_FAULT_SIGBUS;
256 state = &sb->irq_state[src];
258 /* Some sanity checking */
260 pr_devel("%s: source %lx invalid !\n", __func__, irq);
261 return VM_FAULT_SIGBUS;
264 kvmppc_xive_select_irq(state, &hw_num, &xd);
266 arch_spin_lock(&sb->lock);
269 * first/even page is for trigger
270 * second/odd page is for EOI and management.
272 page = page_offset % 2 ? xd->eoi_page : xd->trig_page;
273 arch_spin_unlock(&sb->lock);
275 if (WARN_ON(!page)) {
276 pr_err("%s: accessing invalid ESB page for source %lx !\n",
278 return VM_FAULT_SIGBUS;
281 vmf_insert_pfn(vma, vmf->address, page >> PAGE_SHIFT);
282 return VM_FAULT_NOPAGE;
285 static const struct vm_operations_struct xive_native_esb_vmops = {
286 .fault = xive_native_esb_fault,
289 static vm_fault_t xive_native_tima_fault(struct vm_fault *vmf)
291 struct vm_area_struct *vma = vmf->vma;
293 switch (vmf->pgoff - vma->vm_pgoff) {
294 case 0: /* HW - forbid access */
295 case 1: /* HV - forbid access */
296 return VM_FAULT_SIGBUS;
298 vmf_insert_pfn(vma, vmf->address, xive_tima_os >> PAGE_SHIFT);
299 return VM_FAULT_NOPAGE;
300 case 3: /* USER - TODO */
302 return VM_FAULT_SIGBUS;
306 static const struct vm_operations_struct xive_native_tima_vmops = {
307 .fault = xive_native_tima_fault,
310 static int kvmppc_xive_native_mmap(struct kvm_device *dev,
311 struct vm_area_struct *vma)
313 struct kvmppc_xive *xive = dev->private;
315 /* We only allow mappings at fixed offset for now */
316 if (vma->vm_pgoff == KVM_XIVE_TIMA_PAGE_OFFSET) {
317 if (vma_pages(vma) > 4)
319 vma->vm_ops = &xive_native_tima_vmops;
320 } else if (vma->vm_pgoff == KVM_XIVE_ESB_PAGE_OFFSET) {
321 if (vma_pages(vma) > KVMPPC_XIVE_NR_IRQS * 2)
323 vma->vm_ops = &xive_native_esb_vmops;
328 vma->vm_flags |= VM_IO | VM_PFNMAP;
329 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
332 * Grab the KVM device file address_space to be able to clear
333 * the ESB pages mapping when a device is passed-through into
336 xive->mapping = vma->vm_file->f_mapping;
340 static int kvmppc_xive_native_set_source(struct kvmppc_xive *xive, long irq,
343 struct kvmppc_xive_src_block *sb;
344 struct kvmppc_xive_irq_state *state;
345 u64 __user *ubufp = (u64 __user *) addr;
350 pr_devel("%s irq=0x%lx\n", __func__, irq);
352 if (irq < KVMPPC_XIVE_FIRST_IRQ || irq >= KVMPPC_XIVE_NR_IRQS)
355 sb = kvmppc_xive_find_source(xive, irq, &idx);
357 pr_debug("No source, creating source block...\n");
358 sb = kvmppc_xive_create_src_block(xive, irq);
360 pr_err("Failed to create block...\n");
364 state = &sb->irq_state[idx];
366 if (get_user(val, ubufp)) {
367 pr_err("fault getting user info !\n");
371 arch_spin_lock(&sb->lock);
374 * If the source doesn't already have an IPI, allocate
375 * one and get the corresponding data
377 if (!state->ipi_number) {
378 state->ipi_number = xive_native_alloc_irq();
379 if (state->ipi_number == 0) {
380 pr_err("Failed to allocate IRQ !\n");
384 xive_native_populate_irq_data(state->ipi_number,
386 pr_debug("%s allocated hw_irq=0x%x for irq=0x%lx\n", __func__,
387 state->ipi_number, irq);
390 /* Restore LSI state */
391 if (val & KVM_XIVE_LEVEL_SENSITIVE) {
393 if (val & KVM_XIVE_LEVEL_ASSERTED)
394 state->asserted = true;
395 pr_devel(" LSI ! Asserted=%d\n", state->asserted);
398 /* Mask IRQ to start with */
399 state->act_server = 0;
400 state->act_priority = MASKED;
401 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
402 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
404 /* Increment the number of valid sources and mark this one valid */
412 arch_spin_unlock(&sb->lock);
417 static int kvmppc_xive_native_update_source_config(struct kvmppc_xive *xive,
418 struct kvmppc_xive_src_block *sb,
419 struct kvmppc_xive_irq_state *state,
420 u32 server, u8 priority, bool masked,
423 struct kvm *kvm = xive->kvm;
427 arch_spin_lock(&sb->lock);
429 if (state->act_server == server && state->act_priority == priority &&
433 pr_devel("new_act_prio=%d new_act_server=%d mask=%d act_server=%d act_prio=%d\n",
434 priority, server, masked, state->act_server,
435 state->act_priority);
437 kvmppc_xive_select_irq(state, &hw_num, NULL);
439 if (priority != MASKED && !masked) {
440 rc = kvmppc_xive_select_target(kvm, &server, priority);
444 state->act_priority = priority;
445 state->act_server = server;
448 rc = xive_native_configure_irq(hw_num,
449 kvmppc_xive_vp(xive, server),
452 state->act_priority = MASKED;
453 state->act_server = 0;
456 rc = xive_native_configure_irq(hw_num, 0, MASKED, 0);
460 arch_spin_unlock(&sb->lock);
464 static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive,
467 struct kvmppc_xive_src_block *sb;
468 struct kvmppc_xive_irq_state *state;
469 u64 __user *ubufp = (u64 __user *) addr;
477 sb = kvmppc_xive_find_source(xive, irq, &src);
481 state = &sb->irq_state[src];
486 if (get_user(kvm_cfg, ubufp))
489 pr_devel("%s irq=0x%lx cfg=%016llx\n", __func__, irq, kvm_cfg);
491 priority = (kvm_cfg & KVM_XIVE_SOURCE_PRIORITY_MASK) >>
492 KVM_XIVE_SOURCE_PRIORITY_SHIFT;
493 server = (kvm_cfg & KVM_XIVE_SOURCE_SERVER_MASK) >>
494 KVM_XIVE_SOURCE_SERVER_SHIFT;
495 masked = (kvm_cfg & KVM_XIVE_SOURCE_MASKED_MASK) >>
496 KVM_XIVE_SOURCE_MASKED_SHIFT;
497 eisn = (kvm_cfg & KVM_XIVE_SOURCE_EISN_MASK) >>
498 KVM_XIVE_SOURCE_EISN_SHIFT;
500 if (priority != xive_prio_from_guest(priority)) {
501 pr_err("invalid priority for queue %d for VCPU %d\n",
506 return kvmppc_xive_native_update_source_config(xive, sb, state, server,
507 priority, masked, eisn);
510 static int kvmppc_xive_native_sync_source(struct kvmppc_xive *xive,
513 struct kvmppc_xive_src_block *sb;
514 struct kvmppc_xive_irq_state *state;
515 struct xive_irq_data *xd;
520 pr_devel("%s irq=0x%lx", __func__, irq);
522 sb = kvmppc_xive_find_source(xive, irq, &src);
526 state = &sb->irq_state[src];
530 arch_spin_lock(&sb->lock);
533 kvmppc_xive_select_irq(state, &hw_num, &xd);
534 xive_native_sync_source(hw_num);
538 arch_spin_unlock(&sb->lock);
542 static int xive_native_validate_queue_size(u32 qshift)
545 * We only support 64K pages for the moment. This is also
546 * advertised in the DT property "ibm,xive-eq-sizes"
549 case 0: /* EQ reset */
560 static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive,
561 long eq_idx, u64 addr)
563 struct kvm *kvm = xive->kvm;
564 struct kvm_vcpu *vcpu;
565 struct kvmppc_xive_vcpu *xc;
566 void __user *ubufp = (void __user *) addr;
569 struct kvm_ppc_xive_eq kvm_eq;
575 unsigned long page_size;
579 * Demangle priority/server tuple from the EQ identifier
581 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
582 KVM_XIVE_EQ_PRIORITY_SHIFT;
583 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
584 KVM_XIVE_EQ_SERVER_SHIFT;
586 if (copy_from_user(&kvm_eq, ubufp, sizeof(kvm_eq)))
589 vcpu = kvmppc_xive_find_server(kvm, server);
591 pr_err("Can't find server %d\n", server);
594 xc = vcpu->arch.xive_vcpu;
596 if (priority != xive_prio_from_guest(priority)) {
597 pr_err("Trying to restore invalid queue %d for VCPU %d\n",
601 q = &xc->queues[priority];
603 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
604 __func__, server, priority, kvm_eq.flags,
605 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
607 /* reset queue and disable queueing */
608 if (!kvm_eq.qshift) {
612 rc = kvmppc_xive_native_configure_queue(xc->vp_id, q, priority,
615 pr_err("Failed to reset queue %d for VCPU %d: %d\n",
616 priority, xc->server_num, rc);
624 * sPAPR specifies a "Unconditional Notify (n) flag" for the
625 * H_INT_SET_QUEUE_CONFIG hcall which forces notification
626 * without using the coalescing mechanisms provided by the
627 * XIVE END ESBs. This is required on KVM as notification
628 * using the END ESBs is not supported.
630 if (kvm_eq.flags != KVM_XIVE_EQ_ALWAYS_NOTIFY) {
631 pr_err("invalid flags %d\n", kvm_eq.flags);
635 rc = xive_native_validate_queue_size(kvm_eq.qshift);
637 pr_err("invalid queue size %d\n", kvm_eq.qshift);
641 if (kvm_eq.qaddr & ((1ull << kvm_eq.qshift) - 1)) {
642 pr_err("queue page is not aligned %llx/%llx\n", kvm_eq.qaddr,
643 1ull << kvm_eq.qshift);
647 srcu_idx = srcu_read_lock(&kvm->srcu);
648 gfn = gpa_to_gfn(kvm_eq.qaddr);
650 page_size = kvm_host_page_size(vcpu, gfn);
651 if (1ull << kvm_eq.qshift > page_size) {
652 srcu_read_unlock(&kvm->srcu, srcu_idx);
653 pr_warn("Incompatible host page size %lx!\n", page_size);
657 page = gfn_to_page(kvm, gfn);
658 if (is_error_page(page)) {
659 srcu_read_unlock(&kvm->srcu, srcu_idx);
660 pr_err("Couldn't get queue page %llx!\n", kvm_eq.qaddr);
664 qaddr = page_to_virt(page) + (kvm_eq.qaddr & ~PAGE_MASK);
665 srcu_read_unlock(&kvm->srcu, srcu_idx);
668 * Backup the queue page guest address to the mark EQ page
669 * dirty for migration.
671 q->guest_qaddr = kvm_eq.qaddr;
672 q->guest_qshift = kvm_eq.qshift;
675 * Unconditional Notification is forced by default at the
676 * OPAL level because the use of END ESBs is not supported by
679 rc = kvmppc_xive_native_configure_queue(xc->vp_id, q, priority,
680 (__be32 *) qaddr, kvm_eq.qshift, true);
682 pr_err("Failed to configure queue %d for VCPU %d: %d\n",
683 priority, xc->server_num, rc);
689 * Only restore the queue state when needed. When doing the
690 * H_INT_SET_SOURCE_CONFIG hcall, it should not.
692 if (kvm_eq.qtoggle != 1 || kvm_eq.qindex != 0) {
693 rc = xive_native_set_queue_state(xc->vp_id, priority,
700 rc = kvmppc_xive_attach_escalation(vcpu, priority,
701 kvmppc_xive_has_single_escalation(xive));
704 kvmppc_xive_native_cleanup_queue(vcpu, priority);
708 static int kvmppc_xive_native_get_queue_config(struct kvmppc_xive *xive,
709 long eq_idx, u64 addr)
711 struct kvm *kvm = xive->kvm;
712 struct kvm_vcpu *vcpu;
713 struct kvmppc_xive_vcpu *xc;
715 void __user *ubufp = (u64 __user *) addr;
718 struct kvm_ppc_xive_eq kvm_eq;
727 * Demangle priority/server tuple from the EQ identifier
729 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
730 KVM_XIVE_EQ_PRIORITY_SHIFT;
731 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
732 KVM_XIVE_EQ_SERVER_SHIFT;
734 vcpu = kvmppc_xive_find_server(kvm, server);
736 pr_err("Can't find server %d\n", server);
739 xc = vcpu->arch.xive_vcpu;
741 if (priority != xive_prio_from_guest(priority)) {
742 pr_err("invalid priority for queue %d for VCPU %d\n",
746 q = &xc->queues[priority];
748 memset(&kvm_eq, 0, sizeof(kvm_eq));
753 rc = xive_native_get_queue_info(xc->vp_id, priority, &qaddr, &qshift,
754 &qeoi_page, &escalate_irq, &qflags);
759 if (qflags & OPAL_XIVE_EQ_ALWAYS_NOTIFY)
760 kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
762 kvm_eq.qshift = q->guest_qshift;
763 kvm_eq.qaddr = q->guest_qaddr;
765 rc = xive_native_get_queue_state(xc->vp_id, priority, &kvm_eq.qtoggle,
770 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
771 __func__, server, priority, kvm_eq.flags,
772 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
774 if (copy_to_user(ubufp, &kvm_eq, sizeof(kvm_eq)))
780 static void kvmppc_xive_reset_sources(struct kvmppc_xive_src_block *sb)
784 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
785 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
790 if (state->act_priority == MASKED)
794 state->act_server = 0;
795 state->act_priority = MASKED;
796 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
797 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
798 if (state->pt_number) {
799 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
800 xive_native_configure_irq(state->pt_number,
806 static int kvmppc_xive_reset(struct kvmppc_xive *xive)
808 struct kvm *kvm = xive->kvm;
809 struct kvm_vcpu *vcpu;
812 pr_devel("%s\n", __func__);
814 mutex_lock(&xive->lock);
816 kvm_for_each_vcpu(i, vcpu, kvm) {
817 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
823 kvmppc_xive_disable_vcpu_interrupts(vcpu);
825 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
827 /* Single escalation, no queue 7 */
828 if (prio == 7 && kvmppc_xive_has_single_escalation(xive))
831 if (xc->esc_virq[prio]) {
832 free_irq(xc->esc_virq[prio], vcpu);
833 irq_dispose_mapping(xc->esc_virq[prio]);
834 kfree(xc->esc_virq_names[prio]);
835 xc->esc_virq[prio] = 0;
838 kvmppc_xive_native_cleanup_queue(vcpu, prio);
842 for (i = 0; i <= xive->max_sbid; i++) {
843 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
846 arch_spin_lock(&sb->lock);
847 kvmppc_xive_reset_sources(sb);
848 arch_spin_unlock(&sb->lock);
852 mutex_unlock(&xive->lock);
857 static void kvmppc_xive_native_sync_sources(struct kvmppc_xive_src_block *sb)
861 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
862 struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
863 struct xive_irq_data *xd;
870 * The struct kvmppc_xive_irq_state reflects the state
871 * of the EAS configuration and not the state of the
872 * source. The source is masked setting the PQ bits to
873 * '-Q', which is what is being done before calling
874 * the KVM_DEV_XIVE_EQ_SYNC control.
876 * If a source EAS is configured, OPAL syncs the XIVE
877 * IC of the source and the XIVE IC of the previous
880 * So it should be fine ignoring MASKED sources as
881 * they have been synced already.
883 if (state->act_priority == MASKED)
886 kvmppc_xive_select_irq(state, &hw_num, &xd);
887 xive_native_sync_source(hw_num);
888 xive_native_sync_queue(hw_num);
892 static int kvmppc_xive_native_vcpu_eq_sync(struct kvm_vcpu *vcpu)
894 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
901 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
902 struct xive_q *q = &xc->queues[prio];
907 /* Mark EQ page dirty for migration */
908 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
909 mark_page_dirty(vcpu->kvm, gpa_to_gfn(q->guest_qaddr));
910 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
915 static int kvmppc_xive_native_eq_sync(struct kvmppc_xive *xive)
917 struct kvm *kvm = xive->kvm;
918 struct kvm_vcpu *vcpu;
921 pr_devel("%s\n", __func__);
923 mutex_lock(&xive->lock);
924 for (i = 0; i <= xive->max_sbid; i++) {
925 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
928 arch_spin_lock(&sb->lock);
929 kvmppc_xive_native_sync_sources(sb);
930 arch_spin_unlock(&sb->lock);
934 kvm_for_each_vcpu(i, vcpu, kvm) {
935 kvmppc_xive_native_vcpu_eq_sync(vcpu);
937 mutex_unlock(&xive->lock);
942 static int kvmppc_xive_native_set_attr(struct kvm_device *dev,
943 struct kvm_device_attr *attr)
945 struct kvmppc_xive *xive = dev->private;
947 switch (attr->group) {
948 case KVM_DEV_XIVE_GRP_CTRL:
949 switch (attr->attr) {
950 case KVM_DEV_XIVE_RESET:
951 return kvmppc_xive_reset(xive);
952 case KVM_DEV_XIVE_EQ_SYNC:
953 return kvmppc_xive_native_eq_sync(xive);
954 case KVM_DEV_XIVE_NR_SERVERS:
955 return kvmppc_xive_set_nr_servers(xive, attr->addr);
958 case KVM_DEV_XIVE_GRP_SOURCE:
959 return kvmppc_xive_native_set_source(xive, attr->attr,
961 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
962 return kvmppc_xive_native_set_source_config(xive, attr->attr,
964 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
965 return kvmppc_xive_native_set_queue_config(xive, attr->attr,
967 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
968 return kvmppc_xive_native_sync_source(xive, attr->attr,
974 static int kvmppc_xive_native_get_attr(struct kvm_device *dev,
975 struct kvm_device_attr *attr)
977 struct kvmppc_xive *xive = dev->private;
979 switch (attr->group) {
980 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
981 return kvmppc_xive_native_get_queue_config(xive, attr->attr,
987 static int kvmppc_xive_native_has_attr(struct kvm_device *dev,
988 struct kvm_device_attr *attr)
990 switch (attr->group) {
991 case KVM_DEV_XIVE_GRP_CTRL:
992 switch (attr->attr) {
993 case KVM_DEV_XIVE_RESET:
994 case KVM_DEV_XIVE_EQ_SYNC:
995 case KVM_DEV_XIVE_NR_SERVERS:
999 case KVM_DEV_XIVE_GRP_SOURCE:
1000 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
1001 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
1002 if (attr->attr >= KVMPPC_XIVE_FIRST_IRQ &&
1003 attr->attr < KVMPPC_XIVE_NR_IRQS)
1006 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
1013 * Called when device fd is closed. kvm->lock is held.
1015 static void kvmppc_xive_native_release(struct kvm_device *dev)
1017 struct kvmppc_xive *xive = dev->private;
1018 struct kvm *kvm = xive->kvm;
1019 struct kvm_vcpu *vcpu;
1022 pr_devel("Releasing xive native device\n");
1025 * Clear the KVM device file address_space which is used to
1026 * unmap the ESB pages when a device is passed-through.
1028 mutex_lock(&xive->mapping_lock);
1029 xive->mapping = NULL;
1030 mutex_unlock(&xive->mapping_lock);
1033 * Since this is the device release function, we know that
1034 * userspace does not have any open fd or mmap referring to
1035 * the device. Therefore there can not be any of the
1036 * device attribute set/get, mmap, or page fault functions
1037 * being executed concurrently, and similarly, the
1038 * connect_vcpu and set/clr_mapped functions also cannot
1039 * be being executed.
1042 debugfs_remove(xive->dentry);
1045 * We should clean up the vCPU interrupt presenters first.
1047 kvm_for_each_vcpu(i, vcpu, kvm) {
1049 * Take vcpu->mutex to ensure that no one_reg get/set ioctl
1050 * (i.e. kvmppc_xive_native_[gs]et_vp) can be being done.
1051 * Holding the vcpu->mutex also means that the vcpu cannot
1052 * be executing the KVM_RUN ioctl, and therefore it cannot
1053 * be executing the XIVE push or pull code or accessing
1054 * the XIVE MMIO regions.
1056 mutex_lock(&vcpu->mutex);
1057 kvmppc_xive_native_cleanup_vcpu(vcpu);
1058 mutex_unlock(&vcpu->mutex);
1062 * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
1063 * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
1064 * against xive code getting called during vcpu execution or
1065 * set/get one_reg operations.
1067 kvm->arch.xive = NULL;
1069 for (i = 0; i <= xive->max_sbid; i++) {
1070 if (xive->src_blocks[i])
1071 kvmppc_xive_free_sources(xive->src_blocks[i]);
1072 kfree(xive->src_blocks[i]);
1073 xive->src_blocks[i] = NULL;
1076 if (xive->vp_base != XIVE_INVALID_VP)
1077 xive_native_free_vp_block(xive->vp_base);
1080 * A reference of the kvmppc_xive pointer is now kept under
1081 * the xive_devices struct of the machine for reuse. It is
1082 * freed when the VM is destroyed for now until we fix all the
1090 * Create a XIVE device. kvm->lock is held.
1092 static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
1094 struct kvmppc_xive *xive;
1095 struct kvm *kvm = dev->kvm;
1097 pr_devel("Creating xive native device\n");
1102 xive = kvmppc_xive_get_device(kvm, type);
1106 dev->private = xive;
1109 mutex_init(&xive->mapping_lock);
1110 mutex_init(&xive->lock);
1112 /* VP allocation is delayed to the first call to connect_vcpu */
1113 xive->vp_base = XIVE_INVALID_VP;
1114 /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
1115 * on a POWER9 system.
1117 xive->nr_servers = KVM_MAX_VCPUS;
1119 if (xive_native_has_single_escalation())
1120 xive->flags |= KVMPPC_XIVE_FLAG_SINGLE_ESCALATION;
1122 if (xive_native_has_save_restore())
1123 xive->flags |= KVMPPC_XIVE_FLAG_SAVE_RESTORE;
1125 xive->ops = &kvmppc_xive_native_ops;
1127 kvm->arch.xive = xive;
1132 * Interrupt Pending Buffer (IPB) offset
1134 #define TM_IPB_SHIFT 40
1135 #define TM_IPB_MASK (((u64) 0xFF) << TM_IPB_SHIFT)
1137 int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1139 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1143 if (!kvmppc_xive_enabled(vcpu))
1149 /* Thread context registers. We only care about IPB and CPPR */
1150 val->xive_timaval[0] = vcpu->arch.xive_saved_state.w01;
1152 /* Get the VP state from OPAL */
1153 rc = xive_native_get_vp_state(xc->vp_id, &opal_state);
1158 * Capture the backup of IPB register in the NVT structure and
1159 * merge it in our KVM VP state.
1161 val->xive_timaval[0] |= cpu_to_be64(opal_state & TM_IPB_MASK);
1163 pr_devel("%s NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x opal=%016llx\n",
1165 vcpu->arch.xive_saved_state.nsr,
1166 vcpu->arch.xive_saved_state.cppr,
1167 vcpu->arch.xive_saved_state.ipb,
1168 vcpu->arch.xive_saved_state.pipr,
1169 vcpu->arch.xive_saved_state.w01,
1170 (u32) vcpu->arch.xive_cam_word, opal_state);
1175 int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1177 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1178 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
1180 pr_devel("%s w01=%016llx vp=%016llx\n", __func__,
1181 val->xive_timaval[0], val->xive_timaval[1]);
1183 if (!kvmppc_xive_enabled(vcpu))
1189 /* We can't update the state of a "pushed" VCPU */
1190 if (WARN_ON(vcpu->arch.xive_pushed))
1194 * Restore the thread context registers. IPB and CPPR should
1195 * be the only ones that matter.
1197 vcpu->arch.xive_saved_state.w01 = val->xive_timaval[0];
1200 * There is no need to restore the XIVE internal state (IPB
1201 * stored in the NVT) as the IPB register was merged in KVM VP
1202 * state when captured.
1207 bool kvmppc_xive_native_supported(void)
1209 return xive_native_has_queue_state_support();
1212 static int xive_native_debug_show(struct seq_file *m, void *private)
1214 struct kvmppc_xive *xive = m->private;
1215 struct kvm *kvm = xive->kvm;
1216 struct kvm_vcpu *vcpu;
1222 seq_puts(m, "=========\nVCPU state\n=========\n");
1224 kvm_for_each_vcpu(i, vcpu, kvm) {
1225 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1230 seq_printf(m, "VCPU %d: VP=%#x/%02x\n"
1231 " NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x\n",
1232 xc->server_num, xc->vp_id, xc->vp_chip_id,
1233 vcpu->arch.xive_saved_state.nsr,
1234 vcpu->arch.xive_saved_state.cppr,
1235 vcpu->arch.xive_saved_state.ipb,
1236 vcpu->arch.xive_saved_state.pipr,
1237 be64_to_cpu(vcpu->arch.xive_saved_state.w01),
1238 be32_to_cpu(vcpu->arch.xive_cam_word));
1240 kvmppc_xive_debug_show_queues(m, vcpu);
1243 seq_puts(m, "=========\nSources\n=========\n");
1245 for (i = 0; i <= xive->max_sbid; i++) {
1246 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1249 arch_spin_lock(&sb->lock);
1250 kvmppc_xive_debug_show_sources(m, sb);
1251 arch_spin_unlock(&sb->lock);
1258 DEFINE_SHOW_ATTRIBUTE(xive_native_debug);
1260 static void xive_native_debugfs_init(struct kvmppc_xive *xive)
1264 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
1266 pr_err("%s: no memory for name\n", __func__);
1270 xive->dentry = debugfs_create_file(name, 0444, arch_debugfs_dir,
1271 xive, &xive_native_debug_fops);
1273 pr_debug("%s: created %s\n", __func__, name);
1277 static void kvmppc_xive_native_init(struct kvm_device *dev)
1279 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
1281 /* Register some debug interfaces */
1282 xive_native_debugfs_init(xive);
1285 struct kvm_device_ops kvm_xive_native_ops = {
1286 .name = "kvm-xive-native",
1287 .create = kvmppc_xive_native_create,
1288 .init = kvmppc_xive_native_init,
1289 .release = kvmppc_xive_native_release,
1290 .set_attr = kvmppc_xive_native_set_attr,
1291 .get_attr = kvmppc_xive_native_get_attr,
1292 .has_attr = kvmppc_xive_native_has_attr,
1293 .mmap = kvmppc_xive_native_mmap,