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 <asm/uaccess.h>
16 #include <asm/kvm_book3s.h>
17 #include <asm/kvm_ppc.h>
18 #include <asm/hvcall.h>
20 #include <asm/xive-regs.h>
21 #include <asm/debug.h>
22 #include <asm/debugfs.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)
34 if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
35 offset |= offset << 4;
37 val = in_be64(xd->eoi_mmio + offset);
41 static void kvmppc_xive_native_cleanup_queue(struct kvm_vcpu *vcpu, int prio)
43 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
44 struct xive_q *q = &xc->queues[prio];
46 xive_native_disable_queue(xc->vp_id, q, prio);
48 put_page(virt_to_page(q->qpage));
53 void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu)
55 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
58 if (!kvmppc_xive_enabled(vcpu))
64 pr_devel("native_cleanup_vcpu(cpu=%d)\n", xc->server_num);
66 /* Ensure no interrupt is still routed to that VP */
68 kvmppc_xive_disable_vcpu_interrupts(vcpu);
71 xive_native_disable_vp(xc->vp_id);
73 /* Free the queues & associated interrupts */
74 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
75 /* Free the escalation irq */
76 if (xc->esc_virq[i]) {
77 free_irq(xc->esc_virq[i], vcpu);
78 irq_dispose_mapping(xc->esc_virq[i]);
79 kfree(xc->esc_virq_names[i]);
84 kvmppc_xive_native_cleanup_queue(vcpu, i);
90 /* Cleanup the vcpu */
91 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
92 vcpu->arch.xive_vcpu = NULL;
95 int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
96 struct kvm_vcpu *vcpu, u32 server_num)
98 struct kvmppc_xive *xive = dev->private;
99 struct kvmppc_xive_vcpu *xc = NULL;
102 pr_devel("native_connect_vcpu(server=%d)\n", server_num);
104 if (dev->ops != &kvm_xive_native_ops) {
105 pr_devel("Wrong ops !\n");
108 if (xive->kvm != vcpu->kvm)
110 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
112 if (server_num >= (KVM_MAX_VCPUS * vcpu->kvm->arch.emul_smt_mode)) {
113 pr_devel("Out of bounds !\n");
117 mutex_lock(&xive->lock);
119 if (kvmppc_xive_find_server(vcpu->kvm, server_num)) {
120 pr_devel("Duplicate !\n");
125 xc = kzalloc(sizeof(*xc), GFP_KERNEL);
131 vcpu->arch.xive_vcpu = xc;
134 xc->server_num = server_num;
136 xc->vp_id = kvmppc_xive_vp(xive, server_num);
138 vcpu->arch.irq_type = KVMPPC_IRQ_XIVE;
140 rc = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
142 pr_err("Failed to get VP info from OPAL: %d\n", rc);
147 * Enable the VP first as the single escalation mode will
148 * affect escalation interrupts numbering
150 rc = xive_native_enable_vp(xc->vp_id, xive->single_escalation);
152 pr_err("Failed to enable VP in OPAL: %d\n", rc);
156 /* Configure VCPU fields for use by assembly push/pull */
157 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
158 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
160 /* TODO: reset all queues to a clean state ? */
162 mutex_unlock(&xive->lock);
164 kvmppc_xive_native_cleanup_vcpu(vcpu);
170 * Device passthrough support
172 static int kvmppc_xive_native_reset_mapped(struct kvm *kvm, unsigned long irq)
174 struct kvmppc_xive *xive = kvm->arch.xive;
175 pgoff_t esb_pgoff = KVM_XIVE_ESB_PAGE_OFFSET + irq * 2;
177 if (irq >= KVMPPC_XIVE_NR_IRQS)
181 * Clear the ESB pages of the IRQ number being mapped (or
182 * unmapped) into the guest and let the the VM fault handler
183 * repopulate with the appropriate ESB pages (device or IC)
185 pr_debug("clearing esb pages for girq 0x%lx\n", irq);
186 mutex_lock(&xive->mapping_lock);
188 unmap_mapping_range(xive->mapping,
189 esb_pgoff << PAGE_SHIFT,
190 2ull << PAGE_SHIFT, 1);
191 mutex_unlock(&xive->mapping_lock);
195 static struct kvmppc_xive_ops kvmppc_xive_native_ops = {
196 .reset_mapped = kvmppc_xive_native_reset_mapped,
199 static vm_fault_t xive_native_esb_fault(struct vm_fault *vmf)
201 struct vm_area_struct *vma = vmf->vma;
202 struct kvm_device *dev = vma->vm_file->private_data;
203 struct kvmppc_xive *xive = dev->private;
204 struct kvmppc_xive_src_block *sb;
205 struct kvmppc_xive_irq_state *state;
206 struct xive_irq_data *xd;
214 * Linux/KVM uses a two pages ESB setting, one for trigger and
217 page_offset = vmf->pgoff - vma->vm_pgoff;
218 irq = page_offset / 2;
220 sb = kvmppc_xive_find_source(xive, irq, &src);
222 pr_devel("%s: source %lx not found !\n", __func__, irq);
223 return VM_FAULT_SIGBUS;
226 state = &sb->irq_state[src];
227 kvmppc_xive_select_irq(state, &hw_num, &xd);
229 arch_spin_lock(&sb->lock);
232 * first/even page is for trigger
233 * second/odd page is for EOI and management.
235 page = page_offset % 2 ? xd->eoi_page : xd->trig_page;
236 arch_spin_unlock(&sb->lock);
238 if (WARN_ON(!page)) {
239 pr_err("%s: accessing invalid ESB page for source %lx !\n",
241 return VM_FAULT_SIGBUS;
244 vmf_insert_pfn(vma, vmf->address, page >> PAGE_SHIFT);
245 return VM_FAULT_NOPAGE;
248 static const struct vm_operations_struct xive_native_esb_vmops = {
249 .fault = xive_native_esb_fault,
252 static vm_fault_t xive_native_tima_fault(struct vm_fault *vmf)
254 struct vm_area_struct *vma = vmf->vma;
256 switch (vmf->pgoff - vma->vm_pgoff) {
257 case 0: /* HW - forbid access */
258 case 1: /* HV - forbid access */
259 return VM_FAULT_SIGBUS;
261 vmf_insert_pfn(vma, vmf->address, xive_tima_os >> PAGE_SHIFT);
262 return VM_FAULT_NOPAGE;
263 case 3: /* USER - TODO */
265 return VM_FAULT_SIGBUS;
269 static const struct vm_operations_struct xive_native_tima_vmops = {
270 .fault = xive_native_tima_fault,
273 static int kvmppc_xive_native_mmap(struct kvm_device *dev,
274 struct vm_area_struct *vma)
276 struct kvmppc_xive *xive = dev->private;
278 /* We only allow mappings at fixed offset for now */
279 if (vma->vm_pgoff == KVM_XIVE_TIMA_PAGE_OFFSET) {
280 if (vma_pages(vma) > 4)
282 vma->vm_ops = &xive_native_tima_vmops;
283 } else if (vma->vm_pgoff == KVM_XIVE_ESB_PAGE_OFFSET) {
284 if (vma_pages(vma) > KVMPPC_XIVE_NR_IRQS * 2)
286 vma->vm_ops = &xive_native_esb_vmops;
291 vma->vm_flags |= VM_IO | VM_PFNMAP;
292 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
295 * Grab the KVM device file address_space to be able to clear
296 * the ESB pages mapping when a device is passed-through into
299 xive->mapping = vma->vm_file->f_mapping;
303 static int kvmppc_xive_native_set_source(struct kvmppc_xive *xive, long irq,
306 struct kvmppc_xive_src_block *sb;
307 struct kvmppc_xive_irq_state *state;
308 u64 __user *ubufp = (u64 __user *) addr;
313 pr_devel("%s irq=0x%lx\n", __func__, irq);
315 if (irq < KVMPPC_XIVE_FIRST_IRQ || irq >= KVMPPC_XIVE_NR_IRQS)
318 sb = kvmppc_xive_find_source(xive, irq, &idx);
320 pr_debug("No source, creating source block...\n");
321 sb = kvmppc_xive_create_src_block(xive, irq);
323 pr_err("Failed to create block...\n");
327 state = &sb->irq_state[idx];
329 if (get_user(val, ubufp)) {
330 pr_err("fault getting user info !\n");
334 arch_spin_lock(&sb->lock);
337 * If the source doesn't already have an IPI, allocate
338 * one and get the corresponding data
340 if (!state->ipi_number) {
341 state->ipi_number = xive_native_alloc_irq();
342 if (state->ipi_number == 0) {
343 pr_err("Failed to allocate IRQ !\n");
347 xive_native_populate_irq_data(state->ipi_number,
349 pr_debug("%s allocated hw_irq=0x%x for irq=0x%lx\n", __func__,
350 state->ipi_number, irq);
353 /* Restore LSI state */
354 if (val & KVM_XIVE_LEVEL_SENSITIVE) {
356 if (val & KVM_XIVE_LEVEL_ASSERTED)
357 state->asserted = true;
358 pr_devel(" LSI ! Asserted=%d\n", state->asserted);
361 /* Mask IRQ to start with */
362 state->act_server = 0;
363 state->act_priority = MASKED;
364 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
365 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
367 /* Increment the number of valid sources and mark this one valid */
375 arch_spin_unlock(&sb->lock);
380 static int kvmppc_xive_native_update_source_config(struct kvmppc_xive *xive,
381 struct kvmppc_xive_src_block *sb,
382 struct kvmppc_xive_irq_state *state,
383 u32 server, u8 priority, bool masked,
386 struct kvm *kvm = xive->kvm;
390 arch_spin_lock(&sb->lock);
392 if (state->act_server == server && state->act_priority == priority &&
396 pr_devel("new_act_prio=%d new_act_server=%d mask=%d act_server=%d act_prio=%d\n",
397 priority, server, masked, state->act_server,
398 state->act_priority);
400 kvmppc_xive_select_irq(state, &hw_num, NULL);
402 if (priority != MASKED && !masked) {
403 rc = kvmppc_xive_select_target(kvm, &server, priority);
407 state->act_priority = priority;
408 state->act_server = server;
411 rc = xive_native_configure_irq(hw_num,
412 kvmppc_xive_vp(xive, server),
415 state->act_priority = MASKED;
416 state->act_server = 0;
419 rc = xive_native_configure_irq(hw_num, 0, MASKED, 0);
423 arch_spin_unlock(&sb->lock);
427 static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive,
430 struct kvmppc_xive_src_block *sb;
431 struct kvmppc_xive_irq_state *state;
432 u64 __user *ubufp = (u64 __user *) addr;
440 sb = kvmppc_xive_find_source(xive, irq, &src);
444 state = &sb->irq_state[src];
449 if (get_user(kvm_cfg, ubufp))
452 pr_devel("%s irq=0x%lx cfg=%016llx\n", __func__, irq, kvm_cfg);
454 priority = (kvm_cfg & KVM_XIVE_SOURCE_PRIORITY_MASK) >>
455 KVM_XIVE_SOURCE_PRIORITY_SHIFT;
456 server = (kvm_cfg & KVM_XIVE_SOURCE_SERVER_MASK) >>
457 KVM_XIVE_SOURCE_SERVER_SHIFT;
458 masked = (kvm_cfg & KVM_XIVE_SOURCE_MASKED_MASK) >>
459 KVM_XIVE_SOURCE_MASKED_SHIFT;
460 eisn = (kvm_cfg & KVM_XIVE_SOURCE_EISN_MASK) >>
461 KVM_XIVE_SOURCE_EISN_SHIFT;
463 if (priority != xive_prio_from_guest(priority)) {
464 pr_err("invalid priority for queue %d for VCPU %d\n",
469 return kvmppc_xive_native_update_source_config(xive, sb, state, server,
470 priority, masked, eisn);
473 static int kvmppc_xive_native_sync_source(struct kvmppc_xive *xive,
476 struct kvmppc_xive_src_block *sb;
477 struct kvmppc_xive_irq_state *state;
478 struct xive_irq_data *xd;
483 pr_devel("%s irq=0x%lx", __func__, irq);
485 sb = kvmppc_xive_find_source(xive, irq, &src);
489 state = &sb->irq_state[src];
493 arch_spin_lock(&sb->lock);
496 kvmppc_xive_select_irq(state, &hw_num, &xd);
497 xive_native_sync_source(hw_num);
501 arch_spin_unlock(&sb->lock);
505 static int xive_native_validate_queue_size(u32 qshift)
508 * We only support 64K pages for the moment. This is also
509 * advertised in the DT property "ibm,xive-eq-sizes"
512 case 0: /* EQ reset */
523 static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive,
524 long eq_idx, u64 addr)
526 struct kvm *kvm = xive->kvm;
527 struct kvm_vcpu *vcpu;
528 struct kvmppc_xive_vcpu *xc;
529 void __user *ubufp = (void __user *) addr;
532 struct kvm_ppc_xive_eq kvm_eq;
538 unsigned long page_size;
542 * Demangle priority/server tuple from the EQ identifier
544 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
545 KVM_XIVE_EQ_PRIORITY_SHIFT;
546 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
547 KVM_XIVE_EQ_SERVER_SHIFT;
549 if (copy_from_user(&kvm_eq, ubufp, sizeof(kvm_eq)))
552 vcpu = kvmppc_xive_find_server(kvm, server);
554 pr_err("Can't find server %d\n", server);
557 xc = vcpu->arch.xive_vcpu;
559 if (priority != xive_prio_from_guest(priority)) {
560 pr_err("Trying to restore invalid queue %d for VCPU %d\n",
564 q = &xc->queues[priority];
566 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
567 __func__, server, priority, kvm_eq.flags,
568 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
570 /* reset queue and disable queueing */
571 if (!kvm_eq.qshift) {
575 rc = xive_native_configure_queue(xc->vp_id, q, priority,
578 pr_err("Failed to reset queue %d for VCPU %d: %d\n",
579 priority, xc->server_num, rc);
584 put_page(virt_to_page(q->qpage));
592 * sPAPR specifies a "Unconditional Notify (n) flag" for the
593 * H_INT_SET_QUEUE_CONFIG hcall which forces notification
594 * without using the coalescing mechanisms provided by the
595 * XIVE END ESBs. This is required on KVM as notification
596 * using the END ESBs is not supported.
598 if (kvm_eq.flags != KVM_XIVE_EQ_ALWAYS_NOTIFY) {
599 pr_err("invalid flags %d\n", kvm_eq.flags);
603 rc = xive_native_validate_queue_size(kvm_eq.qshift);
605 pr_err("invalid queue size %d\n", kvm_eq.qshift);
609 if (kvm_eq.qaddr & ((1ull << kvm_eq.qshift) - 1)) {
610 pr_err("queue page is not aligned %llx/%llx\n", kvm_eq.qaddr,
611 1ull << kvm_eq.qshift);
615 srcu_idx = srcu_read_lock(&kvm->srcu);
616 gfn = gpa_to_gfn(kvm_eq.qaddr);
617 page = gfn_to_page(kvm, gfn);
618 if (is_error_page(page)) {
619 srcu_read_unlock(&kvm->srcu, srcu_idx);
620 pr_err("Couldn't get queue page %llx!\n", kvm_eq.qaddr);
624 page_size = kvm_host_page_size(kvm, gfn);
625 if (1ull << kvm_eq.qshift > page_size) {
626 srcu_read_unlock(&kvm->srcu, srcu_idx);
627 pr_warn("Incompatible host page size %lx!\n", page_size);
631 qaddr = page_to_virt(page) + (kvm_eq.qaddr & ~PAGE_MASK);
632 srcu_read_unlock(&kvm->srcu, srcu_idx);
635 * Backup the queue page guest address to the mark EQ page
636 * dirty for migration.
638 q->guest_qaddr = kvm_eq.qaddr;
639 q->guest_qshift = kvm_eq.qshift;
642 * Unconditional Notification is forced by default at the
643 * OPAL level because the use of END ESBs is not supported by
646 rc = xive_native_configure_queue(xc->vp_id, q, priority,
647 (__be32 *) qaddr, kvm_eq.qshift, true);
649 pr_err("Failed to configure queue %d for VCPU %d: %d\n",
650 priority, xc->server_num, rc);
656 * Only restore the queue state when needed. When doing the
657 * H_INT_SET_SOURCE_CONFIG hcall, it should not.
659 if (kvm_eq.qtoggle != 1 || kvm_eq.qindex != 0) {
660 rc = xive_native_set_queue_state(xc->vp_id, priority,
667 rc = kvmppc_xive_attach_escalation(vcpu, priority,
668 xive->single_escalation);
671 kvmppc_xive_native_cleanup_queue(vcpu, priority);
675 static int kvmppc_xive_native_get_queue_config(struct kvmppc_xive *xive,
676 long eq_idx, u64 addr)
678 struct kvm *kvm = xive->kvm;
679 struct kvm_vcpu *vcpu;
680 struct kvmppc_xive_vcpu *xc;
682 void __user *ubufp = (u64 __user *) addr;
685 struct kvm_ppc_xive_eq kvm_eq;
694 * Demangle priority/server tuple from the EQ identifier
696 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
697 KVM_XIVE_EQ_PRIORITY_SHIFT;
698 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
699 KVM_XIVE_EQ_SERVER_SHIFT;
701 vcpu = kvmppc_xive_find_server(kvm, server);
703 pr_err("Can't find server %d\n", server);
706 xc = vcpu->arch.xive_vcpu;
708 if (priority != xive_prio_from_guest(priority)) {
709 pr_err("invalid priority for queue %d for VCPU %d\n",
713 q = &xc->queues[priority];
715 memset(&kvm_eq, 0, sizeof(kvm_eq));
720 rc = xive_native_get_queue_info(xc->vp_id, priority, &qaddr, &qshift,
721 &qeoi_page, &escalate_irq, &qflags);
726 if (qflags & OPAL_XIVE_EQ_ALWAYS_NOTIFY)
727 kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
729 kvm_eq.qshift = q->guest_qshift;
730 kvm_eq.qaddr = q->guest_qaddr;
732 rc = xive_native_get_queue_state(xc->vp_id, priority, &kvm_eq.qtoggle,
737 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
738 __func__, server, priority, kvm_eq.flags,
739 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
741 if (copy_to_user(ubufp, &kvm_eq, sizeof(kvm_eq)))
747 static void kvmppc_xive_reset_sources(struct kvmppc_xive_src_block *sb)
751 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
752 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
757 if (state->act_priority == MASKED)
761 state->act_server = 0;
762 state->act_priority = MASKED;
763 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
764 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
765 if (state->pt_number) {
766 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
767 xive_native_configure_irq(state->pt_number,
773 static int kvmppc_xive_reset(struct kvmppc_xive *xive)
775 struct kvm *kvm = xive->kvm;
776 struct kvm_vcpu *vcpu;
779 pr_devel("%s\n", __func__);
781 mutex_lock(&xive->lock);
783 kvm_for_each_vcpu(i, vcpu, kvm) {
784 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
790 kvmppc_xive_disable_vcpu_interrupts(vcpu);
792 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
794 /* Single escalation, no queue 7 */
795 if (prio == 7 && xive->single_escalation)
798 if (xc->esc_virq[prio]) {
799 free_irq(xc->esc_virq[prio], vcpu);
800 irq_dispose_mapping(xc->esc_virq[prio]);
801 kfree(xc->esc_virq_names[prio]);
802 xc->esc_virq[prio] = 0;
805 kvmppc_xive_native_cleanup_queue(vcpu, prio);
809 for (i = 0; i <= xive->max_sbid; i++) {
810 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
813 arch_spin_lock(&sb->lock);
814 kvmppc_xive_reset_sources(sb);
815 arch_spin_unlock(&sb->lock);
819 mutex_unlock(&xive->lock);
824 static void kvmppc_xive_native_sync_sources(struct kvmppc_xive_src_block *sb)
828 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
829 struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
830 struct xive_irq_data *xd;
837 * The struct kvmppc_xive_irq_state reflects the state
838 * of the EAS configuration and not the state of the
839 * source. The source is masked setting the PQ bits to
840 * '-Q', which is what is being done before calling
841 * the KVM_DEV_XIVE_EQ_SYNC control.
843 * If a source EAS is configured, OPAL syncs the XIVE
844 * IC of the source and the XIVE IC of the previous
847 * So it should be fine ignoring MASKED sources as
848 * they have been synced already.
850 if (state->act_priority == MASKED)
853 kvmppc_xive_select_irq(state, &hw_num, &xd);
854 xive_native_sync_source(hw_num);
855 xive_native_sync_queue(hw_num);
859 static int kvmppc_xive_native_vcpu_eq_sync(struct kvm_vcpu *vcpu)
861 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
868 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
869 struct xive_q *q = &xc->queues[prio];
874 /* Mark EQ page dirty for migration */
875 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
876 mark_page_dirty(vcpu->kvm, gpa_to_gfn(q->guest_qaddr));
877 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
882 static int kvmppc_xive_native_eq_sync(struct kvmppc_xive *xive)
884 struct kvm *kvm = xive->kvm;
885 struct kvm_vcpu *vcpu;
888 pr_devel("%s\n", __func__);
890 mutex_lock(&xive->lock);
891 for (i = 0; i <= xive->max_sbid; i++) {
892 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
895 arch_spin_lock(&sb->lock);
896 kvmppc_xive_native_sync_sources(sb);
897 arch_spin_unlock(&sb->lock);
901 kvm_for_each_vcpu(i, vcpu, kvm) {
902 kvmppc_xive_native_vcpu_eq_sync(vcpu);
904 mutex_unlock(&xive->lock);
909 static int kvmppc_xive_native_set_attr(struct kvm_device *dev,
910 struct kvm_device_attr *attr)
912 struct kvmppc_xive *xive = dev->private;
914 switch (attr->group) {
915 case KVM_DEV_XIVE_GRP_CTRL:
916 switch (attr->attr) {
917 case KVM_DEV_XIVE_RESET:
918 return kvmppc_xive_reset(xive);
919 case KVM_DEV_XIVE_EQ_SYNC:
920 return kvmppc_xive_native_eq_sync(xive);
923 case KVM_DEV_XIVE_GRP_SOURCE:
924 return kvmppc_xive_native_set_source(xive, attr->attr,
926 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
927 return kvmppc_xive_native_set_source_config(xive, attr->attr,
929 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
930 return kvmppc_xive_native_set_queue_config(xive, attr->attr,
932 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
933 return kvmppc_xive_native_sync_source(xive, attr->attr,
939 static int kvmppc_xive_native_get_attr(struct kvm_device *dev,
940 struct kvm_device_attr *attr)
942 struct kvmppc_xive *xive = dev->private;
944 switch (attr->group) {
945 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
946 return kvmppc_xive_native_get_queue_config(xive, attr->attr,
952 static int kvmppc_xive_native_has_attr(struct kvm_device *dev,
953 struct kvm_device_attr *attr)
955 switch (attr->group) {
956 case KVM_DEV_XIVE_GRP_CTRL:
957 switch (attr->attr) {
958 case KVM_DEV_XIVE_RESET:
959 case KVM_DEV_XIVE_EQ_SYNC:
963 case KVM_DEV_XIVE_GRP_SOURCE:
964 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
965 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
966 if (attr->attr >= KVMPPC_XIVE_FIRST_IRQ &&
967 attr->attr < KVMPPC_XIVE_NR_IRQS)
970 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
977 * Called when device fd is closed. kvm->lock is held.
979 static void kvmppc_xive_native_release(struct kvm_device *dev)
981 struct kvmppc_xive *xive = dev->private;
982 struct kvm *kvm = xive->kvm;
983 struct kvm_vcpu *vcpu;
986 pr_devel("Releasing xive native device\n");
989 * Clear the KVM device file address_space which is used to
990 * unmap the ESB pages when a device is passed-through.
992 mutex_lock(&xive->mapping_lock);
993 xive->mapping = NULL;
994 mutex_unlock(&xive->mapping_lock);
997 * Since this is the device release function, we know that
998 * userspace does not have any open fd or mmap referring to
999 * the device. Therefore there can not be any of the
1000 * device attribute set/get, mmap, or page fault functions
1001 * being executed concurrently, and similarly, the
1002 * connect_vcpu and set/clr_mapped functions also cannot
1003 * be being executed.
1006 debugfs_remove(xive->dentry);
1009 * We should clean up the vCPU interrupt presenters first.
1011 kvm_for_each_vcpu(i, vcpu, kvm) {
1013 * Take vcpu->mutex to ensure that no one_reg get/set ioctl
1014 * (i.e. kvmppc_xive_native_[gs]et_vp) can be being done.
1015 * Holding the vcpu->mutex also means that the vcpu cannot
1016 * be executing the KVM_RUN ioctl, and therefore it cannot
1017 * be executing the XIVE push or pull code or accessing
1018 * the XIVE MMIO regions.
1020 mutex_lock(&vcpu->mutex);
1021 kvmppc_xive_native_cleanup_vcpu(vcpu);
1022 mutex_unlock(&vcpu->mutex);
1026 * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
1027 * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
1028 * against xive code getting called during vcpu execution or
1029 * set/get one_reg operations.
1031 kvm->arch.xive = NULL;
1033 for (i = 0; i <= xive->max_sbid; i++) {
1034 if (xive->src_blocks[i])
1035 kvmppc_xive_free_sources(xive->src_blocks[i]);
1036 kfree(xive->src_blocks[i]);
1037 xive->src_blocks[i] = NULL;
1040 if (xive->vp_base != XIVE_INVALID_VP)
1041 xive_native_free_vp_block(xive->vp_base);
1044 * A reference of the kvmppc_xive pointer is now kept under
1045 * the xive_devices struct of the machine for reuse. It is
1046 * freed when the VM is destroyed for now until we fix all the
1054 * Create a XIVE device. kvm->lock is held.
1056 static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
1058 struct kvmppc_xive *xive;
1059 struct kvm *kvm = dev->kvm;
1062 pr_devel("Creating xive native device\n");
1067 xive = kvmppc_xive_get_device(kvm, type);
1071 dev->private = xive;
1074 kvm->arch.xive = xive;
1075 mutex_init(&xive->mapping_lock);
1076 mutex_init(&xive->lock);
1079 * Allocate a bunch of VPs. KVM_MAX_VCPUS is a large value for
1080 * a default. Getting the max number of CPUs the VM was
1081 * configured with would improve our usage of the XIVE VP space.
1083 xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
1084 pr_devel("VP_Base=%x\n", xive->vp_base);
1086 if (xive->vp_base == XIVE_INVALID_VP)
1089 xive->single_escalation = xive_native_has_single_escalation();
1090 xive->ops = &kvmppc_xive_native_ops;
1099 * Interrupt Pending Buffer (IPB) offset
1101 #define TM_IPB_SHIFT 40
1102 #define TM_IPB_MASK (((u64) 0xFF) << TM_IPB_SHIFT)
1104 int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1106 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1110 if (!kvmppc_xive_enabled(vcpu))
1116 /* Thread context registers. We only care about IPB and CPPR */
1117 val->xive_timaval[0] = vcpu->arch.xive_saved_state.w01;
1119 /* Get the VP state from OPAL */
1120 rc = xive_native_get_vp_state(xc->vp_id, &opal_state);
1125 * Capture the backup of IPB register in the NVT structure and
1126 * merge it in our KVM VP state.
1128 val->xive_timaval[0] |= cpu_to_be64(opal_state & TM_IPB_MASK);
1130 pr_devel("%s NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x opal=%016llx\n",
1132 vcpu->arch.xive_saved_state.nsr,
1133 vcpu->arch.xive_saved_state.cppr,
1134 vcpu->arch.xive_saved_state.ipb,
1135 vcpu->arch.xive_saved_state.pipr,
1136 vcpu->arch.xive_saved_state.w01,
1137 (u32) vcpu->arch.xive_cam_word, opal_state);
1142 int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1144 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1145 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
1147 pr_devel("%s w01=%016llx vp=%016llx\n", __func__,
1148 val->xive_timaval[0], val->xive_timaval[1]);
1150 if (!kvmppc_xive_enabled(vcpu))
1156 /* We can't update the state of a "pushed" VCPU */
1157 if (WARN_ON(vcpu->arch.xive_pushed))
1161 * Restore the thread context registers. IPB and CPPR should
1162 * be the only ones that matter.
1164 vcpu->arch.xive_saved_state.w01 = val->xive_timaval[0];
1167 * There is no need to restore the XIVE internal state (IPB
1168 * stored in the NVT) as the IPB register was merged in KVM VP
1169 * state when captured.
1174 static int xive_native_debug_show(struct seq_file *m, void *private)
1176 struct kvmppc_xive *xive = m->private;
1177 struct kvm *kvm = xive->kvm;
1178 struct kvm_vcpu *vcpu;
1184 seq_puts(m, "=========\nVCPU state\n=========\n");
1186 kvm_for_each_vcpu(i, vcpu, kvm) {
1187 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1192 seq_printf(m, "cpu server %#x NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x\n",
1194 vcpu->arch.xive_saved_state.nsr,
1195 vcpu->arch.xive_saved_state.cppr,
1196 vcpu->arch.xive_saved_state.ipb,
1197 vcpu->arch.xive_saved_state.pipr,
1198 vcpu->arch.xive_saved_state.w01,
1199 (u32) vcpu->arch.xive_cam_word);
1201 kvmppc_xive_debug_show_queues(m, vcpu);
1207 static int xive_native_debug_open(struct inode *inode, struct file *file)
1209 return single_open(file, xive_native_debug_show, inode->i_private);
1212 static const struct file_operations xive_native_debug_fops = {
1213 .open = xive_native_debug_open,
1215 .llseek = seq_lseek,
1216 .release = single_release,
1219 static void xive_native_debugfs_init(struct kvmppc_xive *xive)
1223 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
1225 pr_err("%s: no memory for name\n", __func__);
1229 xive->dentry = debugfs_create_file(name, 0444, powerpc_debugfs_root,
1230 xive, &xive_native_debug_fops);
1232 pr_debug("%s: created %s\n", __func__, name);
1236 static void kvmppc_xive_native_init(struct kvm_device *dev)
1238 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
1240 /* Register some debug interfaces */
1241 xive_native_debugfs_init(xive);
1244 struct kvm_device_ops kvm_xive_native_ops = {
1245 .name = "kvm-xive-native",
1246 .create = kvmppc_xive_native_create,
1247 .init = kvmppc_xive_native_init,
1248 .release = kvmppc_xive_native_release,
1249 .set_attr = kvmppc_xive_native_set_attr,
1250 .get_attr = kvmppc_xive_native_get_attr,
1251 .has_attr = kvmppc_xive_native_has_attr,
1252 .mmap = kvmppc_xive_native_mmap,
1255 void kvmppc_xive_native_init_module(void)
1260 void kvmppc_xive_native_exit_module(void)