1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2016,2017 IBM Corporation.
6 #define pr_fmt(fmt) "xive: " fmt
8 #include <linux/types.h>
10 #include <linux/debugfs.h>
11 #include <linux/smp.h>
12 #include <linux/interrupt.h>
13 #include <linux/seq_file.h>
14 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/delay.h>
19 #include <linux/cpumask.h>
26 #include <asm/errno.h>
28 #include <asm/xive-regs.h>
30 #include <asm/kvm_ppc.h>
32 #include "xive-internal.h"
35 static u32 xive_provision_size;
36 static u32 *xive_provision_chips;
37 static u32 xive_provision_chip_count;
38 static u32 xive_queue_shift;
39 static u32 xive_pool_vps = XIVE_INVALID_VP;
40 static struct kmem_cache *xive_provision_cache;
41 static bool xive_has_single_esc;
43 int xive_native_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
45 __be64 flags, eoi_page, trig_page;
46 __be32 esb_shift, src_chip;
50 memset(data, 0, sizeof(*data));
52 rc = opal_xive_get_irq_info(hw_irq, &flags, &eoi_page, &trig_page,
53 &esb_shift, &src_chip);
55 pr_err("opal_xive_get_irq_info(0x%x) returned %lld\n",
60 opal_flags = be64_to_cpu(flags);
61 if (opal_flags & OPAL_XIVE_IRQ_STORE_EOI)
62 data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
63 if (opal_flags & OPAL_XIVE_IRQ_LSI)
64 data->flags |= XIVE_IRQ_FLAG_LSI;
65 if (opal_flags & OPAL_XIVE_IRQ_SHIFT_BUG)
66 data->flags |= XIVE_IRQ_FLAG_SHIFT_BUG;
67 if (opal_flags & OPAL_XIVE_IRQ_MASK_VIA_FW)
68 data->flags |= XIVE_IRQ_FLAG_MASK_FW;
69 if (opal_flags & OPAL_XIVE_IRQ_EOI_VIA_FW)
70 data->flags |= XIVE_IRQ_FLAG_EOI_FW;
71 data->eoi_page = be64_to_cpu(eoi_page);
72 data->trig_page = be64_to_cpu(trig_page);
73 data->esb_shift = be32_to_cpu(esb_shift);
74 data->src_chip = be32_to_cpu(src_chip);
76 data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
77 if (!data->eoi_mmio) {
78 pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
82 data->hw_irq = hw_irq;
86 if (data->trig_page == data->eoi_page) {
87 data->trig_mmio = data->eoi_mmio;
91 data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
92 if (!data->trig_mmio) {
93 pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
98 EXPORT_SYMBOL_GPL(xive_native_populate_irq_data);
100 int xive_native_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
105 rc = opal_xive_set_irq_config(hw_irq, target, prio, sw_irq);
108 msleep(OPAL_BUSY_DELAY_MS);
110 return rc == 0 ? 0 : -ENXIO;
112 EXPORT_SYMBOL_GPL(xive_native_configure_irq);
115 /* This can be called multiple time to change a queue configuration */
116 int xive_native_configure_queue(u32 vp_id, struct xive_q *q, u8 prio,
117 __be32 *qpage, u32 order, bool can_escalate)
122 u64 flags, qpage_phys;
124 /* If there's an actual queue page, clean it */
128 qpage_phys = __pa(qpage);
132 /* Initialize the rest of the fields */
133 q->msk = order ? ((1u << (order - 2)) - 1) : 0;
137 rc = opal_xive_get_queue_info(vp_id, prio, NULL, NULL,
142 pr_err("Error %lld getting queue info prio %d\n", rc, prio);
146 q->eoi_phys = be64_to_cpu(qeoi_page_be);
149 flags = OPAL_XIVE_EQ_ALWAYS_NOTIFY | OPAL_XIVE_EQ_ENABLED;
151 /* Escalation needed ? */
153 q->esc_irq = be32_to_cpu(esc_irq_be);
154 flags |= OPAL_XIVE_EQ_ESCALATE;
157 /* Configure and enable the queue in HW */
159 rc = opal_xive_set_queue_info(vp_id, prio, qpage_phys, order, flags);
162 msleep(OPAL_BUSY_DELAY_MS);
165 pr_err("Error %lld setting queue for prio %d\n", rc, prio);
169 * KVM code requires all of the above to be visible before
170 * q->qpage is set due to how it manages IPI EOIs
178 EXPORT_SYMBOL_GPL(xive_native_configure_queue);
180 static void __xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
184 /* Disable the queue in HW */
186 rc = opal_xive_set_queue_info(vp_id, prio, 0, 0, 0);
189 msleep(OPAL_BUSY_DELAY_MS);
192 pr_err("Error %lld disabling queue for prio %d\n", rc, prio);
195 void xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
197 __xive_native_disable_queue(vp_id, q, prio);
199 EXPORT_SYMBOL_GPL(xive_native_disable_queue);
201 static int xive_native_setup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
203 struct xive_q *q = &xc->queue[prio];
206 qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
208 return PTR_ERR(qpage);
210 return xive_native_configure_queue(get_hard_smp_processor_id(cpu),
211 q, prio, qpage, xive_queue_shift, false);
214 static void xive_native_cleanup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
216 struct xive_q *q = &xc->queue[prio];
217 unsigned int alloc_order;
220 * We use the variant with no iounmap as this is called on exec
221 * from an IPI and iounmap isn't safe
223 __xive_native_disable_queue(get_hard_smp_processor_id(cpu), q, prio);
224 alloc_order = xive_alloc_order(xive_queue_shift);
225 free_pages((unsigned long)q->qpage, alloc_order);
229 static bool xive_native_match(struct device_node *node)
231 return of_device_is_compatible(node, "ibm,opal-xive-vc");
234 static s64 opal_xive_allocate_irq(u32 chip_id)
236 s64 irq = opal_xive_allocate_irq_raw(chip_id);
239 * Old versions of skiboot can incorrectly return 0xffffffff to
240 * indicate no space, fix it up here.
242 return irq == 0xffffffff ? OPAL_RESOURCE : irq;
246 static int xive_native_get_ipi(unsigned int cpu, struct xive_cpu *xc)
250 /* Allocate an IPI and populate info about it */
252 irq = opal_xive_allocate_irq(xc->chip_id);
253 if (irq == OPAL_BUSY) {
254 msleep(OPAL_BUSY_DELAY_MS);
258 pr_err("Failed to allocate IPI on CPU %d\n", cpu);
266 #endif /* CONFIG_SMP */
268 u32 xive_native_alloc_irq(void)
273 rc = opal_xive_allocate_irq(OPAL_XIVE_ANY_CHIP);
276 msleep(OPAL_BUSY_DELAY_MS);
282 EXPORT_SYMBOL_GPL(xive_native_alloc_irq);
284 void xive_native_free_irq(u32 irq)
287 s64 rc = opal_xive_free_irq(irq);
290 msleep(OPAL_BUSY_DELAY_MS);
293 EXPORT_SYMBOL_GPL(xive_native_free_irq);
296 static void xive_native_put_ipi(unsigned int cpu, struct xive_cpu *xc)
304 rc = opal_xive_free_irq(xc->hw_ipi);
305 if (rc == OPAL_BUSY) {
306 msleep(OPAL_BUSY_DELAY_MS);
313 #endif /* CONFIG_SMP */
315 static void xive_native_shutdown(void)
317 /* Switch the XIVE to emulation mode */
318 opal_xive_reset(OPAL_XIVE_MODE_EMU);
322 * Perform an "ack" cycle on the current thread, thus
323 * grabbing the pending active priorities and updating
324 * the CPPR to the most favored one.
326 static void xive_native_update_pending(struct xive_cpu *xc)
331 /* Perform the acknowledge hypervisor to register cycle */
332 ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_HV_REG));
334 /* Synchronize subsequent queue accesses */
338 * Grab the CPPR and the "HE" field which indicates the source
339 * of the hypervisor interrupt (if any)
342 he = (ack >> 8) >> 6;
344 case TM_QW3_NSR_HE_NONE: /* Nothing to see here */
346 case TM_QW3_NSR_HE_PHYS: /* Physical thread interrupt */
349 /* Mark the priority pending */
350 xc->pending_prio |= 1 << cppr;
353 * A new interrupt should never have a CPPR less favored
354 * than our current one.
356 if (cppr >= xc->cppr)
357 pr_err("CPU %d odd ack CPPR, got %d at %d\n",
358 smp_processor_id(), cppr, xc->cppr);
360 /* Update our idea of what the CPPR is */
363 case TM_QW3_NSR_HE_POOL: /* HV Pool interrupt (unused) */
364 case TM_QW3_NSR_HE_LSI: /* Legacy FW LSI (unused) */
365 pr_err("CPU %d got unexpected interrupt type HE=%d\n",
366 smp_processor_id(), he);
371 static void xive_native_eoi(u32 hw_irq)
374 * Not normally used except if specific interrupts need
375 * a workaround on EOI.
377 opal_int_eoi(hw_irq);
380 static void xive_native_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
387 if (xive_pool_vps == XIVE_INVALID_VP)
390 /* Check if pool VP already active, if it is, pull it */
391 if (in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2) & TM_QW2W2_VP)
392 in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
394 /* Enable the pool VP */
395 vp = xive_pool_vps + cpu;
397 rc = opal_xive_set_vp_info(vp, OPAL_XIVE_VP_ENABLED, 0);
400 msleep(OPAL_BUSY_DELAY_MS);
403 pr_err("Failed to enable pool VP on CPU %d\n", cpu);
407 /* Grab it's CAM value */
408 rc = opal_xive_get_vp_info(vp, NULL, &vp_cam_be, NULL, NULL);
410 pr_err("Failed to get pool VP info CPU %d\n", cpu);
413 vp_cam = be64_to_cpu(vp_cam_be);
415 /* Push it on the CPU (set LSMFB to 0xff to skip backlog scan) */
416 out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD0, 0xff);
417 out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2, TM_QW2W2_VP | vp_cam);
420 static void xive_native_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
425 if (xive_pool_vps == XIVE_INVALID_VP)
428 /* Pull the pool VP from the CPU */
429 in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
432 vp = xive_pool_vps + cpu;
434 rc = opal_xive_set_vp_info(vp, 0, 0);
437 msleep(OPAL_BUSY_DELAY_MS);
441 void xive_native_sync_source(u32 hw_irq)
443 opal_xive_sync(XIVE_SYNC_EAS, hw_irq);
445 EXPORT_SYMBOL_GPL(xive_native_sync_source);
447 void xive_native_sync_queue(u32 hw_irq)
449 opal_xive_sync(XIVE_SYNC_QUEUE, hw_irq);
451 EXPORT_SYMBOL_GPL(xive_native_sync_queue);
453 static const struct xive_ops xive_native_ops = {
454 .populate_irq_data = xive_native_populate_irq_data,
455 .configure_irq = xive_native_configure_irq,
456 .setup_queue = xive_native_setup_queue,
457 .cleanup_queue = xive_native_cleanup_queue,
458 .match = xive_native_match,
459 .shutdown = xive_native_shutdown,
460 .update_pending = xive_native_update_pending,
461 .eoi = xive_native_eoi,
462 .setup_cpu = xive_native_setup_cpu,
463 .teardown_cpu = xive_native_teardown_cpu,
464 .sync_source = xive_native_sync_source,
466 .get_ipi = xive_native_get_ipi,
467 .put_ipi = xive_native_put_ipi,
468 #endif /* CONFIG_SMP */
472 static bool xive_parse_provisioning(struct device_node *np)
476 if (of_property_read_u32(np, "ibm,xive-provision-page-size",
477 &xive_provision_size) < 0)
479 rc = of_property_count_elems_of_size(np, "ibm,xive-provision-chips", 4);
481 pr_err("Error %d getting provision chips array\n", rc);
484 xive_provision_chip_count = rc;
488 xive_provision_chips = kcalloc(4, xive_provision_chip_count,
490 if (WARN_ON(!xive_provision_chips))
493 rc = of_property_read_u32_array(np, "ibm,xive-provision-chips",
494 xive_provision_chips,
495 xive_provision_chip_count);
497 pr_err("Error %d reading provision chips array\n", rc);
501 xive_provision_cache = kmem_cache_create("xive-provision",
505 if (!xive_provision_cache) {
506 pr_err("Failed to allocate provision cache\n");
512 static void xive_native_setup_pools(void)
514 /* Allocate a pool big enough */
515 pr_debug("XIVE: Allocating VP block for pool size %u\n", nr_cpu_ids);
517 xive_pool_vps = xive_native_alloc_vp_block(nr_cpu_ids);
518 if (WARN_ON(xive_pool_vps == XIVE_INVALID_VP))
519 pr_err("XIVE: Failed to allocate pool VP, KVM might not function\n");
521 pr_debug("XIVE: Pool VPs allocated at 0x%x for %u max CPUs\n",
522 xive_pool_vps, nr_cpu_ids);
525 u32 xive_native_default_eq_shift(void)
527 return xive_queue_shift;
529 EXPORT_SYMBOL_GPL(xive_native_default_eq_shift);
531 unsigned long xive_tima_os;
532 EXPORT_SYMBOL_GPL(xive_tima_os);
534 bool __init xive_native_init(void)
536 struct device_node *np;
539 struct property *prop;
545 if (xive_cmdline_disabled)
548 pr_devel("xive_native_init()\n");
549 np = of_find_compatible_node(NULL, NULL, "ibm,opal-xive-pe");
551 pr_devel("not found !\n");
554 pr_devel("Found %pOF\n", np);
556 /* Resource 1 is HV window */
557 if (of_address_to_resource(np, 1, &r)) {
558 pr_err("Failed to get thread mgmnt area resource\n");
561 tima = ioremap(r.start, resource_size(&r));
563 pr_err("Failed to map thread mgmnt area\n");
567 /* Read number of priorities */
568 if (of_property_read_u32(np, "ibm,xive-#priorities", &val) == 0)
571 /* Iterate the EQ sizes and pick one */
572 of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, p, val) {
573 xive_queue_shift = val;
574 if (val == PAGE_SHIFT)
578 /* Do we support single escalation */
579 if (of_get_property(np, "single-escalation-support", NULL) != NULL)
580 xive_has_single_esc = true;
582 /* Configure Thread Management areas for KVM */
583 for_each_possible_cpu(cpu)
584 kvmppc_set_xive_tima(cpu, r.start, tima);
586 /* Resource 2 is OS window */
587 if (of_address_to_resource(np, 2, &r)) {
588 pr_err("Failed to get thread mgmnt area resource\n");
592 xive_tima_os = r.start;
594 /* Grab size of provisionning pages */
595 xive_parse_provisioning(np);
597 /* Switch the XIVE to exploitation mode */
598 rc = opal_xive_reset(OPAL_XIVE_MODE_EXPL);
600 pr_err("Switch to exploitation mode failed with error %lld\n", rc);
604 /* Setup some dummy HV pool VPs */
605 xive_native_setup_pools();
607 /* Initialize XIVE core with our backend */
608 if (!xive_core_init(&xive_native_ops, tima, TM_QW3_HV_PHYS,
610 opal_xive_reset(OPAL_XIVE_MODE_EMU);
613 pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
617 static bool xive_native_provision_pages(void)
622 for (i = 0; i < xive_provision_chip_count; i++) {
623 u32 chip = xive_provision_chips[i];
626 * XXX TODO: Try to make the allocation local to the node where
629 p = kmem_cache_alloc(xive_provision_cache, GFP_KERNEL);
631 pr_err("Failed to allocate provisioning page\n");
634 opal_xive_donate_page(chip, __pa(p));
639 u32 xive_native_alloc_vp_block(u32 max_vcpus)
644 order = fls(max_vcpus) - 1;
645 if (max_vcpus > (1 << order))
648 pr_debug("VP block alloc, for max VCPUs %d use order %d\n",
652 rc = opal_xive_alloc_vp_block(order);
655 msleep(OPAL_BUSY_DELAY_MS);
657 case OPAL_XIVE_PROVISIONING:
658 if (!xive_native_provision_pages())
659 return XIVE_INVALID_VP;
663 pr_err("OPAL failed to allocate VCPUs order %d, err %lld\n",
665 return XIVE_INVALID_VP;
671 EXPORT_SYMBOL_GPL(xive_native_alloc_vp_block);
673 void xive_native_free_vp_block(u32 vp_base)
677 if (vp_base == XIVE_INVALID_VP)
680 rc = opal_xive_free_vp_block(vp_base);
682 pr_warn("OPAL error %lld freeing VP block\n", rc);
684 EXPORT_SYMBOL_GPL(xive_native_free_vp_block);
686 int xive_native_enable_vp(u32 vp_id, bool single_escalation)
689 u64 flags = OPAL_XIVE_VP_ENABLED;
691 if (single_escalation)
692 flags |= OPAL_XIVE_VP_SINGLE_ESCALATION;
694 rc = opal_xive_set_vp_info(vp_id, flags, 0);
697 msleep(OPAL_BUSY_DELAY_MS);
699 return rc ? -EIO : 0;
701 EXPORT_SYMBOL_GPL(xive_native_enable_vp);
703 int xive_native_disable_vp(u32 vp_id)
708 rc = opal_xive_set_vp_info(vp_id, 0, 0);
711 msleep(OPAL_BUSY_DELAY_MS);
713 return rc ? -EIO : 0;
715 EXPORT_SYMBOL_GPL(xive_native_disable_vp);
717 int xive_native_get_vp_info(u32 vp_id, u32 *out_cam_id, u32 *out_chip_id)
720 __be32 vp_chip_id_be;
723 rc = opal_xive_get_vp_info(vp_id, NULL, &vp_cam_be, NULL, &vp_chip_id_be);
726 *out_cam_id = be64_to_cpu(vp_cam_be) & 0xffffffffu;
727 *out_chip_id = be32_to_cpu(vp_chip_id_be);
731 EXPORT_SYMBOL_GPL(xive_native_get_vp_info);
733 bool xive_native_has_single_escalation(void)
735 return xive_has_single_esc;
737 EXPORT_SYMBOL_GPL(xive_native_has_single_escalation);
739 int xive_native_get_queue_info(u32 vp_id, u32 prio,
743 u32 *out_escalate_irq,
753 rc = opal_xive_get_queue_info(vp_id, prio, &qpage, &qsize,
754 &qeoi_page, &escalate_irq, &qflags);
756 pr_err("OPAL failed to get queue info for VCPU %d/%d : %lld\n",
762 *out_qpage = be64_to_cpu(qpage);
764 *out_qsize = be32_to_cpu(qsize);
766 *out_qeoi_page = be64_to_cpu(qeoi_page);
767 if (out_escalate_irq)
768 *out_escalate_irq = be32_to_cpu(escalate_irq);
770 *out_qflags = be64_to_cpu(qflags);
774 EXPORT_SYMBOL_GPL(xive_native_get_queue_info);
776 int xive_native_get_queue_state(u32 vp_id, u32 prio, u32 *qtoggle, u32 *qindex)
782 rc = opal_xive_get_queue_state(vp_id, prio, &opal_qtoggle,
785 pr_err("OPAL failed to get queue state for VCPU %d/%d : %lld\n",
791 *qtoggle = be32_to_cpu(opal_qtoggle);
793 *qindex = be32_to_cpu(opal_qindex);
797 EXPORT_SYMBOL_GPL(xive_native_get_queue_state);
799 int xive_native_set_queue_state(u32 vp_id, u32 prio, u32 qtoggle, u32 qindex)
803 rc = opal_xive_set_queue_state(vp_id, prio, qtoggle, qindex);
805 pr_err("OPAL failed to set queue state for VCPU %d/%d : %lld\n",
812 EXPORT_SYMBOL_GPL(xive_native_set_queue_state);
814 bool xive_native_has_queue_state_support(void)
816 return opal_check_token(OPAL_XIVE_GET_QUEUE_STATE) &&
817 opal_check_token(OPAL_XIVE_SET_QUEUE_STATE);
819 EXPORT_SYMBOL_GPL(xive_native_has_queue_state_support);
821 int xive_native_get_vp_state(u32 vp_id, u64 *out_state)
826 rc = opal_xive_get_vp_state(vp_id, &state);
828 pr_err("OPAL failed to get vp state for VCPU %d : %lld\n",
834 *out_state = be64_to_cpu(state);
837 EXPORT_SYMBOL_GPL(xive_native_get_vp_state);