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/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/cpumask.h>
22 #include <linux/delay.h>
28 #include <asm/errno.h>
30 #include <asm/xive-regs.h>
31 #include <asm/hvcall.h>
33 #include "xive-internal.h"
35 static u32 xive_queue_shift;
37 struct xive_irq_bitmap {
38 unsigned long *bitmap;
42 struct list_head list;
45 static LIST_HEAD(xive_irq_bitmaps);
47 static int xive_irq_bitmap_add(int base, int count)
49 struct xive_irq_bitmap *xibm;
51 xibm = kzalloc(sizeof(*xibm), GFP_ATOMIC);
55 spin_lock_init(&xibm->lock);
58 xibm->bitmap = kzalloc(xibm->count, GFP_KERNEL);
59 list_add(&xibm->list, &xive_irq_bitmaps);
61 pr_info("Using IRQ range [%x-%x]", xibm->base,
62 xibm->base + xibm->count - 1);
66 static int __xive_irq_bitmap_alloc(struct xive_irq_bitmap *xibm)
70 irq = find_first_zero_bit(xibm->bitmap, xibm->count);
71 if (irq != xibm->count) {
72 set_bit(irq, xibm->bitmap);
81 static int xive_irq_bitmap_alloc(void)
83 struct xive_irq_bitmap *xibm;
87 list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
88 spin_lock_irqsave(&xibm->lock, flags);
89 irq = __xive_irq_bitmap_alloc(xibm);
90 spin_unlock_irqrestore(&xibm->lock, flags);
97 static void xive_irq_bitmap_free(int irq)
100 struct xive_irq_bitmap *xibm;
102 list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
103 if ((irq >= xibm->base) && (irq < xibm->base + xibm->count)) {
104 spin_lock_irqsave(&xibm->lock, flags);
105 clear_bit(irq - xibm->base, xibm->bitmap);
106 spin_unlock_irqrestore(&xibm->lock, flags);
113 /* Based on the similar routines in RTAS */
114 static unsigned int plpar_busy_delay_time(long rc)
118 if (H_IS_LONG_BUSY(rc)) {
119 ms = get_longbusy_msecs(rc);
120 } else if (rc == H_BUSY) {
121 ms = 10; /* seems appropriate for XIVE hcalls */
127 static unsigned int plpar_busy_delay(int rc)
131 ms = plpar_busy_delay_time(rc);
139 * Note: this call has a partition wide scope and can take a while to
140 * complete. If it returns H_LONG_BUSY_* it should be retried
143 static long plpar_int_reset(unsigned long flags)
148 rc = plpar_hcall_norets(H_INT_RESET, flags);
149 } while (plpar_busy_delay(rc));
152 pr_err("H_INT_RESET failed %ld\n", rc);
157 static long plpar_int_get_source_info(unsigned long flags,
159 unsigned long *src_flags,
160 unsigned long *eoi_page,
161 unsigned long *trig_page,
162 unsigned long *esb_shift)
164 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
168 rc = plpar_hcall(H_INT_GET_SOURCE_INFO, retbuf, flags, lisn);
169 } while (plpar_busy_delay(rc));
172 pr_err("H_INT_GET_SOURCE_INFO lisn=%ld failed %ld\n", lisn, rc);
176 *src_flags = retbuf[0];
177 *eoi_page = retbuf[1];
178 *trig_page = retbuf[2];
179 *esb_shift = retbuf[3];
181 pr_devel("H_INT_GET_SOURCE_INFO flags=%lx eoi=%lx trig=%lx shift=%lx\n",
182 retbuf[0], retbuf[1], retbuf[2], retbuf[3]);
187 #define XIVE_SRC_SET_EISN (1ull << (63 - 62))
188 #define XIVE_SRC_MASK (1ull << (63 - 63)) /* unused */
190 static long plpar_int_set_source_config(unsigned long flags,
192 unsigned long target,
194 unsigned long sw_irq)
199 pr_devel("H_INT_SET_SOURCE_CONFIG flags=%lx lisn=%lx target=%lx prio=%lx sw_irq=%lx\n",
200 flags, lisn, target, prio, sw_irq);
204 rc = plpar_hcall_norets(H_INT_SET_SOURCE_CONFIG, flags, lisn,
205 target, prio, sw_irq);
206 } while (plpar_busy_delay(rc));
209 pr_err("H_INT_SET_SOURCE_CONFIG lisn=%ld target=%lx prio=%lx failed %ld\n",
210 lisn, target, prio, rc);
217 static long plpar_int_get_queue_info(unsigned long flags,
218 unsigned long target,
219 unsigned long priority,
220 unsigned long *esn_page,
221 unsigned long *esn_size)
223 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
227 rc = plpar_hcall(H_INT_GET_QUEUE_INFO, retbuf, flags, target,
229 } while (plpar_busy_delay(rc));
232 pr_err("H_INT_GET_QUEUE_INFO cpu=%ld prio=%ld failed %ld\n",
233 target, priority, rc);
237 *esn_page = retbuf[0];
238 *esn_size = retbuf[1];
240 pr_devel("H_INT_GET_QUEUE_INFO page=%lx size=%lx\n",
241 retbuf[0], retbuf[1]);
246 #define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
248 static long plpar_int_set_queue_config(unsigned long flags,
249 unsigned long target,
250 unsigned long priority,
256 pr_devel("H_INT_SET_QUEUE_CONFIG flags=%lx target=%lx priority=%lx qpage=%lx qsize=%lx\n",
257 flags, target, priority, qpage, qsize);
260 rc = plpar_hcall_norets(H_INT_SET_QUEUE_CONFIG, flags, target,
261 priority, qpage, qsize);
262 } while (plpar_busy_delay(rc));
265 pr_err("H_INT_SET_QUEUE_CONFIG cpu=%ld prio=%ld qpage=%lx returned %ld\n",
266 target, priority, qpage, rc);
273 static long plpar_int_sync(unsigned long flags, unsigned long lisn)
278 rc = plpar_hcall_norets(H_INT_SYNC, flags, lisn);
279 } while (plpar_busy_delay(rc));
282 pr_err("H_INT_SYNC lisn=%ld returned %ld\n", lisn, rc);
289 #define XIVE_ESB_FLAG_STORE (1ull << (63 - 63))
291 static long plpar_int_esb(unsigned long flags,
293 unsigned long offset,
294 unsigned long in_data,
295 unsigned long *out_data)
297 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
300 pr_devel("H_INT_ESB flags=%lx lisn=%lx offset=%lx in=%lx\n",
301 flags, lisn, offset, in_data);
304 rc = plpar_hcall(H_INT_ESB, retbuf, flags, lisn, offset,
306 } while (plpar_busy_delay(rc));
309 pr_err("H_INT_ESB lisn=%ld offset=%ld returned %ld\n",
314 *out_data = retbuf[0];
319 static u64 xive_spapr_esb_rw(u32 lisn, u32 offset, u64 data, bool write)
321 unsigned long read_data;
324 rc = plpar_int_esb(write ? XIVE_ESB_FLAG_STORE : 0,
325 lisn, offset, data, &read_data);
329 return write ? 0 : read_data;
332 #define XIVE_SRC_H_INT_ESB (1ull << (63 - 60))
333 #define XIVE_SRC_LSI (1ull << (63 - 61))
334 #define XIVE_SRC_TRIGGER (1ull << (63 - 62))
335 #define XIVE_SRC_STORE_EOI (1ull << (63 - 63))
337 static int xive_spapr_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
341 unsigned long eoi_page;
342 unsigned long trig_page;
343 unsigned long esb_shift;
345 memset(data, 0, sizeof(*data));
347 rc = plpar_int_get_source_info(0, hw_irq, &flags, &eoi_page, &trig_page,
352 if (flags & XIVE_SRC_H_INT_ESB)
353 data->flags |= XIVE_IRQ_FLAG_H_INT_ESB;
354 if (flags & XIVE_SRC_STORE_EOI)
355 data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
356 if (flags & XIVE_SRC_LSI)
357 data->flags |= XIVE_IRQ_FLAG_LSI;
358 data->eoi_page = eoi_page;
359 data->esb_shift = esb_shift;
360 data->trig_page = trig_page;
362 data->hw_irq = hw_irq;
365 * No chip-id for the sPAPR backend. This has an impact how we
366 * pick a target. See xive_pick_irq_target().
368 data->src_chip = XIVE_INVALID_CHIP_ID;
371 * When the H_INT_ESB flag is set, the H_INT_ESB hcall should
372 * be used for interrupt management. Skip the remapping of the
373 * ESB pages which are not available.
375 if (data->flags & XIVE_IRQ_FLAG_H_INT_ESB)
378 data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
379 if (!data->eoi_mmio) {
380 pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
384 /* Full function page supports trigger */
385 if (flags & XIVE_SRC_TRIGGER) {
386 data->trig_mmio = data->eoi_mmio;
390 data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
391 if (!data->trig_mmio) {
392 iounmap(data->eoi_mmio);
393 pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
399 static int xive_spapr_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
403 rc = plpar_int_set_source_config(XIVE_SRC_SET_EISN, hw_irq, target,
406 return rc == 0 ? 0 : -ENXIO;
409 /* This can be called multiple time to change a queue configuration */
410 static int xive_spapr_configure_queue(u32 target, struct xive_q *q, u8 prio,
411 __be32 *qpage, u32 order)
414 unsigned long esn_page;
415 unsigned long esn_size;
416 u64 flags, qpage_phys;
418 /* If there's an actual queue page, clean it */
422 qpage_phys = __pa(qpage);
427 /* Initialize the rest of the fields */
428 q->msk = order ? ((1u << (order - 2)) - 1) : 0;
432 rc = plpar_int_get_queue_info(0, target, prio, &esn_page, &esn_size);
434 pr_err("Error %lld getting queue info CPU %d prio %d\n", rc,
440 /* TODO: add support for the notification page */
441 q->eoi_phys = esn_page;
443 /* Default is to always notify */
444 flags = XIVE_EQ_ALWAYS_NOTIFY;
446 /* Configure and enable the queue in HW */
447 rc = plpar_int_set_queue_config(flags, target, prio, qpage_phys, order);
449 pr_err("Error %lld setting queue for CPU %d prio %d\n", rc,
459 static int xive_spapr_setup_queue(unsigned int cpu, struct xive_cpu *xc,
462 struct xive_q *q = &xc->queue[prio];
465 qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
467 return PTR_ERR(qpage);
469 return xive_spapr_configure_queue(get_hard_smp_processor_id(cpu),
470 q, prio, qpage, xive_queue_shift);
473 static void xive_spapr_cleanup_queue(unsigned int cpu, struct xive_cpu *xc,
476 struct xive_q *q = &xc->queue[prio];
477 unsigned int alloc_order;
479 int hw_cpu = get_hard_smp_processor_id(cpu);
481 rc = plpar_int_set_queue_config(0, hw_cpu, prio, 0, 0);
483 pr_err("Error %ld setting queue for CPU %d prio %d\n", rc,
486 alloc_order = xive_alloc_order(xive_queue_shift);
487 free_pages((unsigned long)q->qpage, alloc_order);
491 static bool xive_spapr_match(struct device_node *node)
493 /* Ignore cascaded controllers for the moment */
498 static int xive_spapr_get_ipi(unsigned int cpu, struct xive_cpu *xc)
500 int irq = xive_irq_bitmap_alloc();
503 pr_err("Failed to allocate IPI on CPU %d\n", cpu);
511 static void xive_spapr_put_ipi(unsigned int cpu, struct xive_cpu *xc)
513 if (xc->hw_ipi == XIVE_BAD_IRQ)
516 xive_irq_bitmap_free(xc->hw_ipi);
517 xc->hw_ipi = XIVE_BAD_IRQ;
519 #endif /* CONFIG_SMP */
521 static void xive_spapr_shutdown(void)
527 * Perform an "ack" cycle on the current thread. Grab the pending
528 * active priorities and update the CPPR to the most favored one.
530 static void xive_spapr_update_pending(struct xive_cpu *xc)
536 * Perform the "Acknowledge O/S to Register" cycle.
538 * Let's speedup the access to the TIMA using the raw I/O
539 * accessor as we don't need the synchronisation routine of
540 * the higher level ones
542 ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_OS_REG));
544 /* Synchronize subsequent queue accesses */
548 * Grab the CPPR and the "NSR" field which indicates the source
549 * of the interrupt (if any)
554 if (nsr & TM_QW1_NSR_EO) {
557 /* Mark the priority pending */
558 xc->pending_prio |= 1 << cppr;
561 * A new interrupt should never have a CPPR less favored
562 * than our current one.
564 if (cppr >= xc->cppr)
565 pr_err("CPU %d odd ack CPPR, got %d at %d\n",
566 smp_processor_id(), cppr, xc->cppr);
568 /* Update our idea of what the CPPR is */
573 static void xive_spapr_eoi(u32 hw_irq)
578 static void xive_spapr_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
580 /* Only some debug on the TIMA settings */
581 pr_debug("(HW value: %08x %08x %08x)\n",
582 in_be32(xive_tima + TM_QW1_OS + TM_WORD0),
583 in_be32(xive_tima + TM_QW1_OS + TM_WORD1),
584 in_be32(xive_tima + TM_QW1_OS + TM_WORD2));
587 static void xive_spapr_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
592 static void xive_spapr_sync_source(u32 hw_irq)
594 /* Specs are unclear on what this is doing */
595 plpar_int_sync(0, hw_irq);
598 static const struct xive_ops xive_spapr_ops = {
599 .populate_irq_data = xive_spapr_populate_irq_data,
600 .configure_irq = xive_spapr_configure_irq,
601 .setup_queue = xive_spapr_setup_queue,
602 .cleanup_queue = xive_spapr_cleanup_queue,
603 .match = xive_spapr_match,
604 .shutdown = xive_spapr_shutdown,
605 .update_pending = xive_spapr_update_pending,
606 .eoi = xive_spapr_eoi,
607 .setup_cpu = xive_spapr_setup_cpu,
608 .teardown_cpu = xive_spapr_teardown_cpu,
609 .sync_source = xive_spapr_sync_source,
610 .esb_rw = xive_spapr_esb_rw,
612 .get_ipi = xive_spapr_get_ipi,
613 .put_ipi = xive_spapr_put_ipi,
614 #endif /* CONFIG_SMP */
619 * get max priority from "/ibm,plat-res-int-priorities"
621 static bool xive_get_max_prio(u8 *max_prio)
623 struct device_node *rootdn;
628 rootdn = of_find_node_by_path("/");
630 pr_err("not root node found !\n");
634 reg = of_get_property(rootdn, "ibm,plat-res-int-priorities", &len);
637 pr_err("Failed to read 'ibm,plat-res-int-priorities' property\n");
641 if (len % (2 * sizeof(u32)) != 0) {
642 pr_err("invalid 'ibm,plat-res-int-priorities' property\n");
646 /* HW supports priorities in the range [0-7] and 0xFF is a
647 * wildcard priority used to mask. We scan the ranges reserved
648 * by the hypervisor to find the lowest priority we can use.
651 for (prio = 0; prio < 8; prio++) {
655 for (i = 0; i < len / (2 * sizeof(u32)); i++) {
656 int base = be32_to_cpu(reg[2 * i]);
657 int range = be32_to_cpu(reg[2 * i + 1]);
659 if (prio >= base && prio < base + range)
668 pr_err("no valid priority found in 'ibm,plat-res-int-priorities'\n");
676 bool __init xive_spapr_init(void)
678 struct device_node *np;
681 struct property *prop;
688 if (xive_cmdline_disabled)
691 pr_devel("%s()\n", __func__);
692 np = of_find_compatible_node(NULL, NULL, "ibm,power-ivpe");
694 pr_devel("not found !\n");
697 pr_devel("Found %s\n", np->full_name);
699 /* Resource 1 is the OS ring TIMA */
700 if (of_address_to_resource(np, 1, &r)) {
701 pr_err("Failed to get thread mgmnt area resource\n");
704 tima = ioremap(r.start, resource_size(&r));
706 pr_err("Failed to map thread mgmnt area\n");
710 if (!xive_get_max_prio(&max_prio))
713 /* Feed the IRQ number allocator with the ranges given in the DT */
714 reg = of_get_property(np, "ibm,xive-lisn-ranges", &len);
716 pr_err("Failed to read 'ibm,xive-lisn-ranges' property\n");
720 if (len % (2 * sizeof(u32)) != 0) {
721 pr_err("invalid 'ibm,xive-lisn-ranges' property\n");
725 for (i = 0; i < len / (2 * sizeof(u32)); i++, reg += 2)
726 xive_irq_bitmap_add(be32_to_cpu(reg[0]),
727 be32_to_cpu(reg[1]));
729 /* Iterate the EQ sizes and pick one */
730 of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, reg, val) {
731 xive_queue_shift = val;
732 if (val == PAGE_SHIFT)
736 /* Initialize XIVE core with our backend */
737 if (!xive_core_init(&xive_spapr_ops, tima, TM_QW1_OS, max_prio))
740 pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));