2 * Copyright 2016,2017 IBM Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #define pr_fmt(fmt) "xive: " fmt
12 #include <linux/types.h>
13 #include <linux/irq.h>
14 #include <linux/debugfs.h>
15 #include <linux/smp.h>
16 #include <linux/interrupt.h>
17 #include <linux/seq_file.h>
18 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/delay.h>
23 #include <linux/cpumask.h>
30 #include <asm/errno.h>
32 #include <asm/xive-regs.h>
34 #include <asm/kvm_ppc.h>
36 #include "xive-internal.h"
39 static u32 xive_provision_size;
40 static u32 *xive_provision_chips;
41 static u32 xive_provision_chip_count;
42 static u32 xive_queue_shift;
43 static u32 xive_pool_vps = XIVE_INVALID_VP;
44 static struct kmem_cache *xive_provision_cache;
45 static bool xive_has_single_esc;
47 int xive_native_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
49 __be64 flags, eoi_page, trig_page;
50 __be32 esb_shift, src_chip;
54 memset(data, 0, sizeof(*data));
56 rc = opal_xive_get_irq_info(hw_irq, &flags, &eoi_page, &trig_page,
57 &esb_shift, &src_chip);
59 pr_err("opal_xive_get_irq_info(0x%x) returned %lld\n",
64 opal_flags = be64_to_cpu(flags);
65 if (opal_flags & OPAL_XIVE_IRQ_STORE_EOI)
66 data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
67 if (opal_flags & OPAL_XIVE_IRQ_LSI)
68 data->flags |= XIVE_IRQ_FLAG_LSI;
69 if (opal_flags & OPAL_XIVE_IRQ_SHIFT_BUG)
70 data->flags |= XIVE_IRQ_FLAG_SHIFT_BUG;
71 if (opal_flags & OPAL_XIVE_IRQ_MASK_VIA_FW)
72 data->flags |= XIVE_IRQ_FLAG_MASK_FW;
73 if (opal_flags & OPAL_XIVE_IRQ_EOI_VIA_FW)
74 data->flags |= XIVE_IRQ_FLAG_EOI_FW;
75 data->eoi_page = be64_to_cpu(eoi_page);
76 data->trig_page = be64_to_cpu(trig_page);
77 data->esb_shift = be32_to_cpu(esb_shift);
78 data->src_chip = be32_to_cpu(src_chip);
80 data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
81 if (!data->eoi_mmio) {
82 pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
86 data->hw_irq = hw_irq;
90 if (data->trig_page == data->eoi_page) {
91 data->trig_mmio = data->eoi_mmio;
95 data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
96 if (!data->trig_mmio) {
97 pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
102 EXPORT_SYMBOL_GPL(xive_native_populate_irq_data);
104 int xive_native_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
109 rc = opal_xive_set_irq_config(hw_irq, target, prio, sw_irq);
114 return rc == 0 ? 0 : -ENXIO;
116 EXPORT_SYMBOL_GPL(xive_native_configure_irq);
119 /* This can be called multiple time to change a queue configuration */
120 int xive_native_configure_queue(u32 vp_id, struct xive_q *q, u8 prio,
121 __be32 *qpage, u32 order, bool can_escalate)
126 u64 flags, qpage_phys;
128 /* If there's an actual queue page, clean it */
132 qpage_phys = __pa(qpage);
136 /* Initialize the rest of the fields */
137 q->msk = order ? ((1u << (order - 2)) - 1) : 0;
141 rc = opal_xive_get_queue_info(vp_id, prio, NULL, NULL,
146 pr_err("Error %lld getting queue info prio %d\n", rc, prio);
150 q->eoi_phys = be64_to_cpu(qeoi_page_be);
153 flags = OPAL_XIVE_EQ_ALWAYS_NOTIFY | OPAL_XIVE_EQ_ENABLED;
155 /* Escalation needed ? */
157 q->esc_irq = be32_to_cpu(esc_irq_be);
158 flags |= OPAL_XIVE_EQ_ESCALATE;
161 /* Configure and enable the queue in HW */
163 rc = opal_xive_set_queue_info(vp_id, prio, qpage_phys, order, flags);
169 pr_err("Error %lld setting queue for prio %d\n", rc, prio);
173 * KVM code requires all of the above to be visible before
174 * q->qpage is set due to how it manages IPI EOIs
182 EXPORT_SYMBOL_GPL(xive_native_configure_queue);
184 static void __xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
188 /* Disable the queue in HW */
190 rc = opal_xive_set_queue_info(vp_id, prio, 0, 0, 0);
196 pr_err("Error %lld disabling queue for prio %d\n", rc, prio);
199 void xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
201 __xive_native_disable_queue(vp_id, q, prio);
203 EXPORT_SYMBOL_GPL(xive_native_disable_queue);
205 static int xive_native_setup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
207 struct xive_q *q = &xc->queue[prio];
210 qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
212 return PTR_ERR(qpage);
214 return xive_native_configure_queue(get_hard_smp_processor_id(cpu),
215 q, prio, qpage, xive_queue_shift, false);
218 static void xive_native_cleanup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
220 struct xive_q *q = &xc->queue[prio];
221 unsigned int alloc_order;
224 * We use the variant with no iounmap as this is called on exec
225 * from an IPI and iounmap isn't safe
227 __xive_native_disable_queue(get_hard_smp_processor_id(cpu), q, prio);
228 alloc_order = xive_alloc_order(xive_queue_shift);
229 free_pages((unsigned long)q->qpage, alloc_order);
233 static bool xive_native_match(struct device_node *node)
235 return of_device_is_compatible(node, "ibm,opal-xive-vc");
239 static int xive_native_get_ipi(unsigned int cpu, struct xive_cpu *xc)
241 struct device_node *np;
242 unsigned int chip_id;
245 /* Find the chip ID */
246 np = of_get_cpu_node(cpu, NULL);
248 if (of_property_read_u32(np, "ibm,chip-id", &chip_id) < 0)
252 /* Allocate an IPI and populate info about it */
254 irq = opal_xive_allocate_irq(chip_id);
255 if (irq == OPAL_BUSY) {
260 pr_err("Failed to allocate IPI on CPU %d\n", cpu);
268 #endif /* CONFIG_SMP */
270 u32 xive_native_alloc_irq(void)
275 rc = opal_xive_allocate_irq(OPAL_XIVE_ANY_CHIP);
284 EXPORT_SYMBOL_GPL(xive_native_alloc_irq);
286 void xive_native_free_irq(u32 irq)
289 s64 rc = opal_xive_free_irq(irq);
295 EXPORT_SYMBOL_GPL(xive_native_free_irq);
298 static void xive_native_put_ipi(unsigned int cpu, struct xive_cpu *xc)
306 rc = opal_xive_free_irq(xc->hw_ipi);
307 if (rc == OPAL_BUSY) {
315 #endif /* CONFIG_SMP */
317 static void xive_native_shutdown(void)
319 /* Switch the XIVE to emulation mode */
320 opal_xive_reset(OPAL_XIVE_MODE_EMU);
324 * Perform an "ack" cycle on the current thread, thus
325 * grabbing the pending active priorities and updating
326 * the CPPR to the most favored one.
328 static void xive_native_update_pending(struct xive_cpu *xc)
333 /* Perform the acknowledge hypervisor to register cycle */
334 ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_HV_REG));
336 /* Synchronize subsequent queue accesses */
340 * Grab the CPPR and the "HE" field which indicates the source
341 * of the hypervisor interrupt (if any)
344 he = GETFIELD(TM_QW3_NSR_HE, (ack >> 8));
346 case TM_QW3_NSR_HE_NONE: /* Nothing to see here */
348 case TM_QW3_NSR_HE_PHYS: /* Physical thread interrupt */
351 /* Mark the priority pending */
352 xc->pending_prio |= 1 << cppr;
355 * A new interrupt should never have a CPPR less favored
356 * than our current one.
358 if (cppr >= xc->cppr)
359 pr_err("CPU %d odd ack CPPR, got %d at %d\n",
360 smp_processor_id(), cppr, xc->cppr);
362 /* Update our idea of what the CPPR is */
365 case TM_QW3_NSR_HE_POOL: /* HV Pool interrupt (unused) */
366 case TM_QW3_NSR_HE_LSI: /* Legacy FW LSI (unused) */
367 pr_err("CPU %d got unexpected interrupt type HE=%d\n",
368 smp_processor_id(), he);
373 static void xive_native_eoi(u32 hw_irq)
376 * Not normally used except if specific interrupts need
377 * a workaround on EOI.
379 opal_int_eoi(hw_irq);
382 static void xive_native_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
389 if (xive_pool_vps == XIVE_INVALID_VP)
392 /* Check if pool VP already active, if it is, pull it */
393 if (in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2) & TM_QW2W2_VP)
394 in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
396 /* Enable the pool VP */
397 vp = xive_pool_vps + cpu;
398 pr_debug("CPU %d setting up pool VP 0x%x\n", cpu, vp);
400 rc = opal_xive_set_vp_info(vp, OPAL_XIVE_VP_ENABLED, 0);
406 pr_err("Failed to enable pool VP on CPU %d\n", cpu);
410 /* Grab it's CAM value */
411 rc = opal_xive_get_vp_info(vp, NULL, &vp_cam_be, NULL, NULL);
413 pr_err("Failed to get pool VP info CPU %d\n", cpu);
416 vp_cam = be64_to_cpu(vp_cam_be);
418 pr_debug("VP CAM = %llx\n", vp_cam);
420 /* Push it on the CPU (set LSMFB to 0xff to skip backlog scan) */
421 pr_debug("(Old HW value: %08x)\n",
422 in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2));
423 out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD0, 0xff);
424 out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2,
425 TM_QW2W2_VP | vp_cam);
426 pr_debug("(New HW value: %08x)\n",
427 in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2));
430 static void xive_native_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
435 if (xive_pool_vps == XIVE_INVALID_VP)
438 /* Pull the pool VP from the CPU */
439 in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
442 vp = xive_pool_vps + cpu;
444 rc = opal_xive_set_vp_info(vp, 0, 0);
451 void xive_native_sync_source(u32 hw_irq)
453 opal_xive_sync(XIVE_SYNC_EAS, hw_irq);
455 EXPORT_SYMBOL_GPL(xive_native_sync_source);
457 static const struct xive_ops xive_native_ops = {
458 .populate_irq_data = xive_native_populate_irq_data,
459 .configure_irq = xive_native_configure_irq,
460 .setup_queue = xive_native_setup_queue,
461 .cleanup_queue = xive_native_cleanup_queue,
462 .match = xive_native_match,
463 .shutdown = xive_native_shutdown,
464 .update_pending = xive_native_update_pending,
465 .eoi = xive_native_eoi,
466 .setup_cpu = xive_native_setup_cpu,
467 .teardown_cpu = xive_native_teardown_cpu,
468 .sync_source = xive_native_sync_source,
470 .get_ipi = xive_native_get_ipi,
471 .put_ipi = xive_native_put_ipi,
472 #endif /* CONFIG_SMP */
476 static bool xive_parse_provisioning(struct device_node *np)
480 if (of_property_read_u32(np, "ibm,xive-provision-page-size",
481 &xive_provision_size) < 0)
483 rc = of_property_count_elems_of_size(np, "ibm,xive-provision-chips", 4);
485 pr_err("Error %d getting provision chips array\n", rc);
488 xive_provision_chip_count = rc;
492 xive_provision_chips = kzalloc(4 * xive_provision_chip_count,
494 if (WARN_ON(!xive_provision_chips))
497 rc = of_property_read_u32_array(np, "ibm,xive-provision-chips",
498 xive_provision_chips,
499 xive_provision_chip_count);
501 pr_err("Error %d reading provision chips array\n", rc);
505 xive_provision_cache = kmem_cache_create("xive-provision",
509 if (!xive_provision_cache) {
510 pr_err("Failed to allocate provision cache\n");
516 static void xive_native_setup_pools(void)
518 /* Allocate a pool big enough */
519 pr_debug("XIVE: Allocating VP block for pool size %u\n", nr_cpu_ids);
521 xive_pool_vps = xive_native_alloc_vp_block(nr_cpu_ids);
522 if (WARN_ON(xive_pool_vps == XIVE_INVALID_VP))
523 pr_err("XIVE: Failed to allocate pool VP, KVM might not function\n");
525 pr_debug("XIVE: Pool VPs allocated at 0x%x for %u max CPUs\n",
526 xive_pool_vps, nr_cpu_ids);
529 u32 xive_native_default_eq_shift(void)
531 return xive_queue_shift;
533 EXPORT_SYMBOL_GPL(xive_native_default_eq_shift);
535 bool __init xive_native_init(void)
537 struct device_node *np;
540 struct property *prop;
546 if (xive_cmdline_disabled)
549 pr_devel("xive_native_init()\n");
550 np = of_find_compatible_node(NULL, NULL, "ibm,opal-xive-pe");
552 pr_devel("not found !\n");
555 pr_devel("Found %pOF\n", np);
557 /* Resource 1 is HV window */
558 if (of_address_to_resource(np, 1, &r)) {
559 pr_err("Failed to get thread mgmnt area resource\n");
562 tima = ioremap(r.start, resource_size(&r));
564 pr_err("Failed to map thread mgmnt area\n");
568 /* Read number of priorities */
569 if (of_property_read_u32(np, "ibm,xive-#priorities", &val) == 0)
572 /* Iterate the EQ sizes and pick one */
573 of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, p, val) {
574 xive_queue_shift = val;
575 if (val == PAGE_SHIFT)
579 /* Do we support single escalation */
580 if (of_get_property(np, "single-escalation-support", NULL) != NULL)
581 xive_has_single_esc = true;
583 /* Configure Thread Management areas for KVM */
584 for_each_possible_cpu(cpu)
585 kvmppc_set_xive_tima(cpu, r.start, tima);
587 /* Grab size of provisionning pages */
588 xive_parse_provisioning(np);
590 /* Switch the XIVE to exploitation mode */
591 rc = opal_xive_reset(OPAL_XIVE_MODE_EXPL);
593 pr_err("Switch to exploitation mode failed with error %lld\n", rc);
597 /* Setup some dummy HV pool VPs */
598 xive_native_setup_pools();
600 /* Initialize XIVE core with our backend */
601 if (!xive_core_init(&xive_native_ops, tima, TM_QW3_HV_PHYS,
603 opal_xive_reset(OPAL_XIVE_MODE_EMU);
606 pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
610 static bool xive_native_provision_pages(void)
615 for (i = 0; i < xive_provision_chip_count; i++) {
616 u32 chip = xive_provision_chips[i];
619 * XXX TODO: Try to make the allocation local to the node where
622 p = kmem_cache_alloc(xive_provision_cache, GFP_KERNEL);
624 pr_err("Failed to allocate provisioning page\n");
627 opal_xive_donate_page(chip, __pa(p));
632 u32 xive_native_alloc_vp_block(u32 max_vcpus)
637 order = fls(max_vcpus) - 1;
638 if (max_vcpus > (1 << order))
641 pr_debug("VP block alloc, for max VCPUs %d use order %d\n",
645 rc = opal_xive_alloc_vp_block(order);
650 case OPAL_XIVE_PROVISIONING:
651 if (!xive_native_provision_pages())
652 return XIVE_INVALID_VP;
656 pr_err("OPAL failed to allocate VCPUs order %d, err %lld\n",
658 return XIVE_INVALID_VP;
664 EXPORT_SYMBOL_GPL(xive_native_alloc_vp_block);
666 void xive_native_free_vp_block(u32 vp_base)
670 if (vp_base == XIVE_INVALID_VP)
673 rc = opal_xive_free_vp_block(vp_base);
675 pr_warn("OPAL error %lld freeing VP block\n", rc);
677 EXPORT_SYMBOL_GPL(xive_native_free_vp_block);
679 int xive_native_enable_vp(u32 vp_id, bool single_escalation)
682 u64 flags = OPAL_XIVE_VP_ENABLED;
684 if (single_escalation)
685 flags |= OPAL_XIVE_VP_SINGLE_ESCALATION;
687 rc = opal_xive_set_vp_info(vp_id, flags, 0);
692 return rc ? -EIO : 0;
694 EXPORT_SYMBOL_GPL(xive_native_enable_vp);
696 int xive_native_disable_vp(u32 vp_id)
701 rc = opal_xive_set_vp_info(vp_id, 0, 0);
706 return rc ? -EIO : 0;
708 EXPORT_SYMBOL_GPL(xive_native_disable_vp);
710 int xive_native_get_vp_info(u32 vp_id, u32 *out_cam_id, u32 *out_chip_id)
713 __be32 vp_chip_id_be;
716 rc = opal_xive_get_vp_info(vp_id, NULL, &vp_cam_be, NULL, &vp_chip_id_be);
719 *out_cam_id = be64_to_cpu(vp_cam_be) & 0xffffffffu;
720 *out_chip_id = be32_to_cpu(vp_chip_id_be);
724 EXPORT_SYMBOL_GPL(xive_native_get_vp_info);
726 bool xive_native_has_single_escalation(void)
728 return xive_has_single_esc;
730 EXPORT_SYMBOL_GPL(xive_native_has_single_escalation);