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>
23 #include <asm/debugfs.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
29 #include "book3s_xive.h"
31 static u8 xive_vm_esb_load(struct xive_irq_data *xd, u32 offset)
36 * The KVM XIVE native device does not use the XIVE_ESB_SET_PQ_10
37 * load operation, so there is no need to enforce load-after-store
41 val = in_be64(xd->eoi_mmio + offset);
45 static void kvmppc_xive_native_cleanup_queue(struct kvm_vcpu *vcpu, int prio)
47 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
48 struct xive_q *q = &xc->queues[prio];
50 xive_native_disable_queue(xc->vp_id, q, prio);
52 put_page(virt_to_page(q->qpage));
57 static int kvmppc_xive_native_configure_queue(u32 vp_id, struct xive_q *q,
58 u8 prio, __be32 *qpage,
59 u32 order, bool can_escalate)
62 __be32 *qpage_prev = q->qpage;
64 rc = xive_native_configure_queue(vp_id, q, prio, qpage, order,
70 put_page(virt_to_page(qpage_prev));
75 void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu)
77 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
80 if (!kvmppc_xive_enabled(vcpu))
86 pr_devel("native_cleanup_vcpu(cpu=%d)\n", xc->server_num);
88 /* Ensure no interrupt is still routed to that VP */
90 kvmppc_xive_disable_vcpu_interrupts(vcpu);
92 /* Free escalations */
93 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
94 /* Free the escalation irq */
95 if (xc->esc_virq[i]) {
96 if (xc->xive->single_escalation)
97 xive_cleanup_single_escalation(vcpu, xc,
99 free_irq(xc->esc_virq[i], vcpu);
100 irq_dispose_mapping(xc->esc_virq[i]);
101 kfree(xc->esc_virq_names[i]);
107 xive_native_disable_vp(xc->vp_id);
109 /* Clear the cam word so guest entry won't try to push context */
110 vcpu->arch.xive_cam_word = 0;
112 /* Free the queues */
113 for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
114 kvmppc_xive_native_cleanup_queue(vcpu, i);
120 /* Cleanup the vcpu */
121 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
122 vcpu->arch.xive_vcpu = NULL;
125 int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
126 struct kvm_vcpu *vcpu, u32 server_num)
128 struct kvmppc_xive *xive = dev->private;
129 struct kvmppc_xive_vcpu *xc = NULL;
133 pr_devel("native_connect_vcpu(server=%d)\n", server_num);
135 if (dev->ops != &kvm_xive_native_ops) {
136 pr_devel("Wrong ops !\n");
139 if (xive->kvm != vcpu->kvm)
141 if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
144 mutex_lock(&xive->lock);
146 rc = kvmppc_xive_compute_vp_id(xive, server_num, &vp_id);
150 xc = kzalloc(sizeof(*xc), GFP_KERNEL);
156 vcpu->arch.xive_vcpu = xc;
159 xc->server_num = server_num;
163 vcpu->arch.irq_type = KVMPPC_IRQ_XIVE;
165 rc = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
167 pr_err("Failed to get VP info from OPAL: %d\n", rc);
172 * Enable the VP first as the single escalation mode will
173 * affect escalation interrupts numbering
175 rc = xive_native_enable_vp(xc->vp_id, xive->single_escalation);
177 pr_err("Failed to enable VP in OPAL: %d\n", rc);
181 /* Configure VCPU fields for use by assembly push/pull */
182 vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
183 vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
185 /* TODO: reset all queues to a clean state ? */
187 mutex_unlock(&xive->lock);
189 kvmppc_xive_native_cleanup_vcpu(vcpu);
195 * Device passthrough support
197 static int kvmppc_xive_native_reset_mapped(struct kvm *kvm, unsigned long irq)
199 struct kvmppc_xive *xive = kvm->arch.xive;
200 pgoff_t esb_pgoff = KVM_XIVE_ESB_PAGE_OFFSET + irq * 2;
202 if (irq >= KVMPPC_XIVE_NR_IRQS)
206 * Clear the ESB pages of the IRQ number being mapped (or
207 * unmapped) into the guest and let the the VM fault handler
208 * repopulate with the appropriate ESB pages (device or IC)
210 pr_debug("clearing esb pages for girq 0x%lx\n", irq);
211 mutex_lock(&xive->mapping_lock);
213 unmap_mapping_range(xive->mapping,
214 esb_pgoff << PAGE_SHIFT,
215 2ull << PAGE_SHIFT, 1);
216 mutex_unlock(&xive->mapping_lock);
220 static struct kvmppc_xive_ops kvmppc_xive_native_ops = {
221 .reset_mapped = kvmppc_xive_native_reset_mapped,
224 static vm_fault_t xive_native_esb_fault(struct vm_fault *vmf)
226 struct vm_area_struct *vma = vmf->vma;
227 struct kvm_device *dev = vma->vm_file->private_data;
228 struct kvmppc_xive *xive = dev->private;
229 struct kvmppc_xive_src_block *sb;
230 struct kvmppc_xive_irq_state *state;
231 struct xive_irq_data *xd;
239 * Linux/KVM uses a two pages ESB setting, one for trigger and
242 page_offset = vmf->pgoff - vma->vm_pgoff;
243 irq = page_offset / 2;
245 sb = kvmppc_xive_find_source(xive, irq, &src);
247 pr_devel("%s: source %lx not found !\n", __func__, irq);
248 return VM_FAULT_SIGBUS;
251 state = &sb->irq_state[src];
253 /* Some sanity checking */
255 pr_devel("%s: source %lx invalid !\n", __func__, irq);
256 return VM_FAULT_SIGBUS;
259 kvmppc_xive_select_irq(state, &hw_num, &xd);
261 arch_spin_lock(&sb->lock);
264 * first/even page is for trigger
265 * second/odd page is for EOI and management.
267 page = page_offset % 2 ? xd->eoi_page : xd->trig_page;
268 arch_spin_unlock(&sb->lock);
270 if (WARN_ON(!page)) {
271 pr_err("%s: accessing invalid ESB page for source %lx !\n",
273 return VM_FAULT_SIGBUS;
276 vmf_insert_pfn(vma, vmf->address, page >> PAGE_SHIFT);
277 return VM_FAULT_NOPAGE;
280 static const struct vm_operations_struct xive_native_esb_vmops = {
281 .fault = xive_native_esb_fault,
284 static vm_fault_t xive_native_tima_fault(struct vm_fault *vmf)
286 struct vm_area_struct *vma = vmf->vma;
288 switch (vmf->pgoff - vma->vm_pgoff) {
289 case 0: /* HW - forbid access */
290 case 1: /* HV - forbid access */
291 return VM_FAULT_SIGBUS;
293 vmf_insert_pfn(vma, vmf->address, xive_tima_os >> PAGE_SHIFT);
294 return VM_FAULT_NOPAGE;
295 case 3: /* USER - TODO */
297 return VM_FAULT_SIGBUS;
301 static const struct vm_operations_struct xive_native_tima_vmops = {
302 .fault = xive_native_tima_fault,
305 static int kvmppc_xive_native_mmap(struct kvm_device *dev,
306 struct vm_area_struct *vma)
308 struct kvmppc_xive *xive = dev->private;
310 /* We only allow mappings at fixed offset for now */
311 if (vma->vm_pgoff == KVM_XIVE_TIMA_PAGE_OFFSET) {
312 if (vma_pages(vma) > 4)
314 vma->vm_ops = &xive_native_tima_vmops;
315 } else if (vma->vm_pgoff == KVM_XIVE_ESB_PAGE_OFFSET) {
316 if (vma_pages(vma) > KVMPPC_XIVE_NR_IRQS * 2)
318 vma->vm_ops = &xive_native_esb_vmops;
323 vma->vm_flags |= VM_IO | VM_PFNMAP;
324 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
327 * Grab the KVM device file address_space to be able to clear
328 * the ESB pages mapping when a device is passed-through into
331 xive->mapping = vma->vm_file->f_mapping;
335 static int kvmppc_xive_native_set_source(struct kvmppc_xive *xive, long irq,
338 struct kvmppc_xive_src_block *sb;
339 struct kvmppc_xive_irq_state *state;
340 u64 __user *ubufp = (u64 __user *) addr;
345 pr_devel("%s irq=0x%lx\n", __func__, irq);
347 if (irq < KVMPPC_XIVE_FIRST_IRQ || irq >= KVMPPC_XIVE_NR_IRQS)
350 sb = kvmppc_xive_find_source(xive, irq, &idx);
352 pr_debug("No source, creating source block...\n");
353 sb = kvmppc_xive_create_src_block(xive, irq);
355 pr_err("Failed to create block...\n");
359 state = &sb->irq_state[idx];
361 if (get_user(val, ubufp)) {
362 pr_err("fault getting user info !\n");
366 arch_spin_lock(&sb->lock);
369 * If the source doesn't already have an IPI, allocate
370 * one and get the corresponding data
372 if (!state->ipi_number) {
373 state->ipi_number = xive_native_alloc_irq();
374 if (state->ipi_number == 0) {
375 pr_err("Failed to allocate IRQ !\n");
379 xive_native_populate_irq_data(state->ipi_number,
381 pr_debug("%s allocated hw_irq=0x%x for irq=0x%lx\n", __func__,
382 state->ipi_number, irq);
385 /* Restore LSI state */
386 if (val & KVM_XIVE_LEVEL_SENSITIVE) {
388 if (val & KVM_XIVE_LEVEL_ASSERTED)
389 state->asserted = true;
390 pr_devel(" LSI ! Asserted=%d\n", state->asserted);
393 /* Mask IRQ to start with */
394 state->act_server = 0;
395 state->act_priority = MASKED;
396 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
397 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
399 /* Increment the number of valid sources and mark this one valid */
407 arch_spin_unlock(&sb->lock);
412 static int kvmppc_xive_native_update_source_config(struct kvmppc_xive *xive,
413 struct kvmppc_xive_src_block *sb,
414 struct kvmppc_xive_irq_state *state,
415 u32 server, u8 priority, bool masked,
418 struct kvm *kvm = xive->kvm;
422 arch_spin_lock(&sb->lock);
424 if (state->act_server == server && state->act_priority == priority &&
428 pr_devel("new_act_prio=%d new_act_server=%d mask=%d act_server=%d act_prio=%d\n",
429 priority, server, masked, state->act_server,
430 state->act_priority);
432 kvmppc_xive_select_irq(state, &hw_num, NULL);
434 if (priority != MASKED && !masked) {
435 rc = kvmppc_xive_select_target(kvm, &server, priority);
439 state->act_priority = priority;
440 state->act_server = server;
443 rc = xive_native_configure_irq(hw_num,
444 kvmppc_xive_vp(xive, server),
447 state->act_priority = MASKED;
448 state->act_server = 0;
451 rc = xive_native_configure_irq(hw_num, 0, MASKED, 0);
455 arch_spin_unlock(&sb->lock);
459 static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive,
462 struct kvmppc_xive_src_block *sb;
463 struct kvmppc_xive_irq_state *state;
464 u64 __user *ubufp = (u64 __user *) addr;
472 sb = kvmppc_xive_find_source(xive, irq, &src);
476 state = &sb->irq_state[src];
481 if (get_user(kvm_cfg, ubufp))
484 pr_devel("%s irq=0x%lx cfg=%016llx\n", __func__, irq, kvm_cfg);
486 priority = (kvm_cfg & KVM_XIVE_SOURCE_PRIORITY_MASK) >>
487 KVM_XIVE_SOURCE_PRIORITY_SHIFT;
488 server = (kvm_cfg & KVM_XIVE_SOURCE_SERVER_MASK) >>
489 KVM_XIVE_SOURCE_SERVER_SHIFT;
490 masked = (kvm_cfg & KVM_XIVE_SOURCE_MASKED_MASK) >>
491 KVM_XIVE_SOURCE_MASKED_SHIFT;
492 eisn = (kvm_cfg & KVM_XIVE_SOURCE_EISN_MASK) >>
493 KVM_XIVE_SOURCE_EISN_SHIFT;
495 if (priority != xive_prio_from_guest(priority)) {
496 pr_err("invalid priority for queue %d for VCPU %d\n",
501 return kvmppc_xive_native_update_source_config(xive, sb, state, server,
502 priority, masked, eisn);
505 static int kvmppc_xive_native_sync_source(struct kvmppc_xive *xive,
508 struct kvmppc_xive_src_block *sb;
509 struct kvmppc_xive_irq_state *state;
510 struct xive_irq_data *xd;
515 pr_devel("%s irq=0x%lx", __func__, irq);
517 sb = kvmppc_xive_find_source(xive, irq, &src);
521 state = &sb->irq_state[src];
525 arch_spin_lock(&sb->lock);
528 kvmppc_xive_select_irq(state, &hw_num, &xd);
529 xive_native_sync_source(hw_num);
533 arch_spin_unlock(&sb->lock);
537 static int xive_native_validate_queue_size(u32 qshift)
540 * We only support 64K pages for the moment. This is also
541 * advertised in the DT property "ibm,xive-eq-sizes"
544 case 0: /* EQ reset */
555 static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive,
556 long eq_idx, u64 addr)
558 struct kvm *kvm = xive->kvm;
559 struct kvm_vcpu *vcpu;
560 struct kvmppc_xive_vcpu *xc;
561 void __user *ubufp = (void __user *) addr;
564 struct kvm_ppc_xive_eq kvm_eq;
570 unsigned long page_size;
574 * Demangle priority/server tuple from the EQ identifier
576 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
577 KVM_XIVE_EQ_PRIORITY_SHIFT;
578 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
579 KVM_XIVE_EQ_SERVER_SHIFT;
581 if (copy_from_user(&kvm_eq, ubufp, sizeof(kvm_eq)))
584 vcpu = kvmppc_xive_find_server(kvm, server);
586 pr_err("Can't find server %d\n", server);
589 xc = vcpu->arch.xive_vcpu;
591 if (priority != xive_prio_from_guest(priority)) {
592 pr_err("Trying to restore invalid queue %d for VCPU %d\n",
596 q = &xc->queues[priority];
598 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
599 __func__, server, priority, kvm_eq.flags,
600 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
602 /* reset queue and disable queueing */
603 if (!kvm_eq.qshift) {
607 rc = kvmppc_xive_native_configure_queue(xc->vp_id, q, priority,
610 pr_err("Failed to reset queue %d for VCPU %d: %d\n",
611 priority, xc->server_num, rc);
619 * sPAPR specifies a "Unconditional Notify (n) flag" for the
620 * H_INT_SET_QUEUE_CONFIG hcall which forces notification
621 * without using the coalescing mechanisms provided by the
622 * XIVE END ESBs. This is required on KVM as notification
623 * using the END ESBs is not supported.
625 if (kvm_eq.flags != KVM_XIVE_EQ_ALWAYS_NOTIFY) {
626 pr_err("invalid flags %d\n", kvm_eq.flags);
630 rc = xive_native_validate_queue_size(kvm_eq.qshift);
632 pr_err("invalid queue size %d\n", kvm_eq.qshift);
636 if (kvm_eq.qaddr & ((1ull << kvm_eq.qshift) - 1)) {
637 pr_err("queue page is not aligned %llx/%llx\n", kvm_eq.qaddr,
638 1ull << kvm_eq.qshift);
642 srcu_idx = srcu_read_lock(&kvm->srcu);
643 gfn = gpa_to_gfn(kvm_eq.qaddr);
645 page_size = kvm_host_page_size(vcpu, gfn);
646 if (1ull << kvm_eq.qshift > page_size) {
647 srcu_read_unlock(&kvm->srcu, srcu_idx);
648 pr_warn("Incompatible host page size %lx!\n", page_size);
652 page = gfn_to_page(kvm, gfn);
653 if (is_error_page(page)) {
654 srcu_read_unlock(&kvm->srcu, srcu_idx);
655 pr_err("Couldn't get queue page %llx!\n", kvm_eq.qaddr);
659 qaddr = page_to_virt(page) + (kvm_eq.qaddr & ~PAGE_MASK);
660 srcu_read_unlock(&kvm->srcu, srcu_idx);
663 * Backup the queue page guest address to the mark EQ page
664 * dirty for migration.
666 q->guest_qaddr = kvm_eq.qaddr;
667 q->guest_qshift = kvm_eq.qshift;
670 * Unconditional Notification is forced by default at the
671 * OPAL level because the use of END ESBs is not supported by
674 rc = kvmppc_xive_native_configure_queue(xc->vp_id, q, priority,
675 (__be32 *) qaddr, kvm_eq.qshift, true);
677 pr_err("Failed to configure queue %d for VCPU %d: %d\n",
678 priority, xc->server_num, rc);
684 * Only restore the queue state when needed. When doing the
685 * H_INT_SET_SOURCE_CONFIG hcall, it should not.
687 if (kvm_eq.qtoggle != 1 || kvm_eq.qindex != 0) {
688 rc = xive_native_set_queue_state(xc->vp_id, priority,
695 rc = kvmppc_xive_attach_escalation(vcpu, priority,
696 xive->single_escalation);
699 kvmppc_xive_native_cleanup_queue(vcpu, priority);
703 static int kvmppc_xive_native_get_queue_config(struct kvmppc_xive *xive,
704 long eq_idx, u64 addr)
706 struct kvm *kvm = xive->kvm;
707 struct kvm_vcpu *vcpu;
708 struct kvmppc_xive_vcpu *xc;
710 void __user *ubufp = (u64 __user *) addr;
713 struct kvm_ppc_xive_eq kvm_eq;
722 * Demangle priority/server tuple from the EQ identifier
724 priority = (eq_idx & KVM_XIVE_EQ_PRIORITY_MASK) >>
725 KVM_XIVE_EQ_PRIORITY_SHIFT;
726 server = (eq_idx & KVM_XIVE_EQ_SERVER_MASK) >>
727 KVM_XIVE_EQ_SERVER_SHIFT;
729 vcpu = kvmppc_xive_find_server(kvm, server);
731 pr_err("Can't find server %d\n", server);
734 xc = vcpu->arch.xive_vcpu;
736 if (priority != xive_prio_from_guest(priority)) {
737 pr_err("invalid priority for queue %d for VCPU %d\n",
741 q = &xc->queues[priority];
743 memset(&kvm_eq, 0, sizeof(kvm_eq));
748 rc = xive_native_get_queue_info(xc->vp_id, priority, &qaddr, &qshift,
749 &qeoi_page, &escalate_irq, &qflags);
754 if (qflags & OPAL_XIVE_EQ_ALWAYS_NOTIFY)
755 kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
757 kvm_eq.qshift = q->guest_qshift;
758 kvm_eq.qaddr = q->guest_qaddr;
760 rc = xive_native_get_queue_state(xc->vp_id, priority, &kvm_eq.qtoggle,
765 pr_devel("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n",
766 __func__, server, priority, kvm_eq.flags,
767 kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex);
769 if (copy_to_user(ubufp, &kvm_eq, sizeof(kvm_eq)))
775 static void kvmppc_xive_reset_sources(struct kvmppc_xive_src_block *sb)
779 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
780 struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
785 if (state->act_priority == MASKED)
789 state->act_server = 0;
790 state->act_priority = MASKED;
791 xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
792 xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
793 if (state->pt_number) {
794 xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
795 xive_native_configure_irq(state->pt_number,
801 static int kvmppc_xive_reset(struct kvmppc_xive *xive)
803 struct kvm *kvm = xive->kvm;
804 struct kvm_vcpu *vcpu;
807 pr_devel("%s\n", __func__);
809 mutex_lock(&xive->lock);
811 kvm_for_each_vcpu(i, vcpu, kvm) {
812 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
818 kvmppc_xive_disable_vcpu_interrupts(vcpu);
820 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
822 /* Single escalation, no queue 7 */
823 if (prio == 7 && xive->single_escalation)
826 if (xc->esc_virq[prio]) {
827 free_irq(xc->esc_virq[prio], vcpu);
828 irq_dispose_mapping(xc->esc_virq[prio]);
829 kfree(xc->esc_virq_names[prio]);
830 xc->esc_virq[prio] = 0;
833 kvmppc_xive_native_cleanup_queue(vcpu, prio);
837 for (i = 0; i <= xive->max_sbid; i++) {
838 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
841 arch_spin_lock(&sb->lock);
842 kvmppc_xive_reset_sources(sb);
843 arch_spin_unlock(&sb->lock);
847 mutex_unlock(&xive->lock);
852 static void kvmppc_xive_native_sync_sources(struct kvmppc_xive_src_block *sb)
856 for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
857 struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
858 struct xive_irq_data *xd;
865 * The struct kvmppc_xive_irq_state reflects the state
866 * of the EAS configuration and not the state of the
867 * source. The source is masked setting the PQ bits to
868 * '-Q', which is what is being done before calling
869 * the KVM_DEV_XIVE_EQ_SYNC control.
871 * If a source EAS is configured, OPAL syncs the XIVE
872 * IC of the source and the XIVE IC of the previous
875 * So it should be fine ignoring MASKED sources as
876 * they have been synced already.
878 if (state->act_priority == MASKED)
881 kvmppc_xive_select_irq(state, &hw_num, &xd);
882 xive_native_sync_source(hw_num);
883 xive_native_sync_queue(hw_num);
887 static int kvmppc_xive_native_vcpu_eq_sync(struct kvm_vcpu *vcpu)
889 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
896 for (prio = 0; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
897 struct xive_q *q = &xc->queues[prio];
902 /* Mark EQ page dirty for migration */
903 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
904 mark_page_dirty(vcpu->kvm, gpa_to_gfn(q->guest_qaddr));
905 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
910 static int kvmppc_xive_native_eq_sync(struct kvmppc_xive *xive)
912 struct kvm *kvm = xive->kvm;
913 struct kvm_vcpu *vcpu;
916 pr_devel("%s\n", __func__);
918 mutex_lock(&xive->lock);
919 for (i = 0; i <= xive->max_sbid; i++) {
920 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
923 arch_spin_lock(&sb->lock);
924 kvmppc_xive_native_sync_sources(sb);
925 arch_spin_unlock(&sb->lock);
929 kvm_for_each_vcpu(i, vcpu, kvm) {
930 kvmppc_xive_native_vcpu_eq_sync(vcpu);
932 mutex_unlock(&xive->lock);
937 static int kvmppc_xive_native_set_attr(struct kvm_device *dev,
938 struct kvm_device_attr *attr)
940 struct kvmppc_xive *xive = dev->private;
942 switch (attr->group) {
943 case KVM_DEV_XIVE_GRP_CTRL:
944 switch (attr->attr) {
945 case KVM_DEV_XIVE_RESET:
946 return kvmppc_xive_reset(xive);
947 case KVM_DEV_XIVE_EQ_SYNC:
948 return kvmppc_xive_native_eq_sync(xive);
949 case KVM_DEV_XIVE_NR_SERVERS:
950 return kvmppc_xive_set_nr_servers(xive, attr->addr);
953 case KVM_DEV_XIVE_GRP_SOURCE:
954 return kvmppc_xive_native_set_source(xive, attr->attr,
956 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
957 return kvmppc_xive_native_set_source_config(xive, attr->attr,
959 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
960 return kvmppc_xive_native_set_queue_config(xive, attr->attr,
962 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
963 return kvmppc_xive_native_sync_source(xive, attr->attr,
969 static int kvmppc_xive_native_get_attr(struct kvm_device *dev,
970 struct kvm_device_attr *attr)
972 struct kvmppc_xive *xive = dev->private;
974 switch (attr->group) {
975 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
976 return kvmppc_xive_native_get_queue_config(xive, attr->attr,
982 static int kvmppc_xive_native_has_attr(struct kvm_device *dev,
983 struct kvm_device_attr *attr)
985 switch (attr->group) {
986 case KVM_DEV_XIVE_GRP_CTRL:
987 switch (attr->attr) {
988 case KVM_DEV_XIVE_RESET:
989 case KVM_DEV_XIVE_EQ_SYNC:
990 case KVM_DEV_XIVE_NR_SERVERS:
994 case KVM_DEV_XIVE_GRP_SOURCE:
995 case KVM_DEV_XIVE_GRP_SOURCE_CONFIG:
996 case KVM_DEV_XIVE_GRP_SOURCE_SYNC:
997 if (attr->attr >= KVMPPC_XIVE_FIRST_IRQ &&
998 attr->attr < KVMPPC_XIVE_NR_IRQS)
1001 case KVM_DEV_XIVE_GRP_EQ_CONFIG:
1008 * Called when device fd is closed. kvm->lock is held.
1010 static void kvmppc_xive_native_release(struct kvm_device *dev)
1012 struct kvmppc_xive *xive = dev->private;
1013 struct kvm *kvm = xive->kvm;
1014 struct kvm_vcpu *vcpu;
1017 pr_devel("Releasing xive native device\n");
1020 * Clear the KVM device file address_space which is used to
1021 * unmap the ESB pages when a device is passed-through.
1023 mutex_lock(&xive->mapping_lock);
1024 xive->mapping = NULL;
1025 mutex_unlock(&xive->mapping_lock);
1028 * Since this is the device release function, we know that
1029 * userspace does not have any open fd or mmap referring to
1030 * the device. Therefore there can not be any of the
1031 * device attribute set/get, mmap, or page fault functions
1032 * being executed concurrently, and similarly, the
1033 * connect_vcpu and set/clr_mapped functions also cannot
1034 * be being executed.
1037 debugfs_remove(xive->dentry);
1040 * We should clean up the vCPU interrupt presenters first.
1042 kvm_for_each_vcpu(i, vcpu, kvm) {
1044 * Take vcpu->mutex to ensure that no one_reg get/set ioctl
1045 * (i.e. kvmppc_xive_native_[gs]et_vp) can be being done.
1046 * Holding the vcpu->mutex also means that the vcpu cannot
1047 * be executing the KVM_RUN ioctl, and therefore it cannot
1048 * be executing the XIVE push or pull code or accessing
1049 * the XIVE MMIO regions.
1051 mutex_lock(&vcpu->mutex);
1052 kvmppc_xive_native_cleanup_vcpu(vcpu);
1053 mutex_unlock(&vcpu->mutex);
1057 * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
1058 * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
1059 * against xive code getting called during vcpu execution or
1060 * set/get one_reg operations.
1062 kvm->arch.xive = NULL;
1064 for (i = 0; i <= xive->max_sbid; i++) {
1065 if (xive->src_blocks[i])
1066 kvmppc_xive_free_sources(xive->src_blocks[i]);
1067 kfree(xive->src_blocks[i]);
1068 xive->src_blocks[i] = NULL;
1071 if (xive->vp_base != XIVE_INVALID_VP)
1072 xive_native_free_vp_block(xive->vp_base);
1075 * A reference of the kvmppc_xive pointer is now kept under
1076 * the xive_devices struct of the machine for reuse. It is
1077 * freed when the VM is destroyed for now until we fix all the
1085 * Create a XIVE device. kvm->lock is held.
1087 static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
1089 struct kvmppc_xive *xive;
1090 struct kvm *kvm = dev->kvm;
1092 pr_devel("Creating xive native device\n");
1097 xive = kvmppc_xive_get_device(kvm, type);
1101 dev->private = xive;
1104 mutex_init(&xive->mapping_lock);
1105 mutex_init(&xive->lock);
1107 /* VP allocation is delayed to the first call to connect_vcpu */
1108 xive->vp_base = XIVE_INVALID_VP;
1109 /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
1110 * on a POWER9 system.
1112 xive->nr_servers = KVM_MAX_VCPUS;
1114 xive->single_escalation = xive_native_has_single_escalation();
1115 xive->ops = &kvmppc_xive_native_ops;
1117 kvm->arch.xive = xive;
1122 * Interrupt Pending Buffer (IPB) offset
1124 #define TM_IPB_SHIFT 40
1125 #define TM_IPB_MASK (((u64) 0xFF) << TM_IPB_SHIFT)
1127 int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1129 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1133 if (!kvmppc_xive_enabled(vcpu))
1139 /* Thread context registers. We only care about IPB and CPPR */
1140 val->xive_timaval[0] = vcpu->arch.xive_saved_state.w01;
1142 /* Get the VP state from OPAL */
1143 rc = xive_native_get_vp_state(xc->vp_id, &opal_state);
1148 * Capture the backup of IPB register in the NVT structure and
1149 * merge it in our KVM VP state.
1151 val->xive_timaval[0] |= cpu_to_be64(opal_state & TM_IPB_MASK);
1153 pr_devel("%s NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x opal=%016llx\n",
1155 vcpu->arch.xive_saved_state.nsr,
1156 vcpu->arch.xive_saved_state.cppr,
1157 vcpu->arch.xive_saved_state.ipb,
1158 vcpu->arch.xive_saved_state.pipr,
1159 vcpu->arch.xive_saved_state.w01,
1160 (u32) vcpu->arch.xive_cam_word, opal_state);
1165 int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu, union kvmppc_one_reg *val)
1167 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1168 struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
1170 pr_devel("%s w01=%016llx vp=%016llx\n", __func__,
1171 val->xive_timaval[0], val->xive_timaval[1]);
1173 if (!kvmppc_xive_enabled(vcpu))
1179 /* We can't update the state of a "pushed" VCPU */
1180 if (WARN_ON(vcpu->arch.xive_pushed))
1184 * Restore the thread context registers. IPB and CPPR should
1185 * be the only ones that matter.
1187 vcpu->arch.xive_saved_state.w01 = val->xive_timaval[0];
1190 * There is no need to restore the XIVE internal state (IPB
1191 * stored in the NVT) as the IPB register was merged in KVM VP
1192 * state when captured.
1197 bool kvmppc_xive_native_supported(void)
1199 return xive_native_has_queue_state_support();
1202 static int xive_native_debug_show(struct seq_file *m, void *private)
1204 struct kvmppc_xive *xive = m->private;
1205 struct kvm *kvm = xive->kvm;
1206 struct kvm_vcpu *vcpu;
1212 seq_puts(m, "=========\nVCPU state\n=========\n");
1214 kvm_for_each_vcpu(i, vcpu, kvm) {
1215 struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
1220 seq_printf(m, "VCPU %d: VP=%#x/%02x\n"
1221 " NSR=%02x CPPR=%02x IBP=%02x PIPR=%02x w01=%016llx w2=%08x\n",
1222 xc->server_num, xc->vp_id, xc->vp_chip_id,
1223 vcpu->arch.xive_saved_state.nsr,
1224 vcpu->arch.xive_saved_state.cppr,
1225 vcpu->arch.xive_saved_state.ipb,
1226 vcpu->arch.xive_saved_state.pipr,
1227 be64_to_cpu(vcpu->arch.xive_saved_state.w01),
1228 be32_to_cpu(vcpu->arch.xive_cam_word));
1230 kvmppc_xive_debug_show_queues(m, vcpu);
1233 seq_puts(m, "=========\nSources\n=========\n");
1235 for (i = 0; i <= xive->max_sbid; i++) {
1236 struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
1239 arch_spin_lock(&sb->lock);
1240 kvmppc_xive_debug_show_sources(m, sb);
1241 arch_spin_unlock(&sb->lock);
1248 DEFINE_SHOW_ATTRIBUTE(xive_native_debug);
1250 static void xive_native_debugfs_init(struct kvmppc_xive *xive)
1254 name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
1256 pr_err("%s: no memory for name\n", __func__);
1260 xive->dentry = debugfs_create_file(name, 0444, powerpc_debugfs_root,
1261 xive, &xive_native_debug_fops);
1263 pr_debug("%s: created %s\n", __func__, name);
1267 static void kvmppc_xive_native_init(struct kvm_device *dev)
1269 struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
1271 /* Register some debug interfaces */
1272 xive_native_debugfs_init(xive);
1275 struct kvm_device_ops kvm_xive_native_ops = {
1276 .name = "kvm-xive-native",
1277 .create = kvmppc_xive_native_create,
1278 .init = kvmppc_xive_native_init,
1279 .release = kvmppc_xive_native_release,
1280 .set_attr = kvmppc_xive_native_set_attr,
1281 .get_attr = kvmppc_xive_native_get_attr,
1282 .has_attr = kvmppc_xive_native_has_attr,
1283 .mmap = kvmppc_xive_native_mmap,