blob: 797bb0636ab71682c2c1a5d4b62eb36f0e0cb695 [file] [log] [blame]
Cédric Le Goatereac1e732017-08-30 21:46:11 +02001/*
2 * Copyright 2016,2017 IBM Corporation.
3 *
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.
8 */
9
10#define pr_fmt(fmt) "xive: " fmt
11
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>
17#include <linux/of.h>
18#include <linux/slab.h>
19#include <linux/spinlock.h>
20#include <linux/cpumask.h>
21#include <linux/mm.h>
22
23#include <asm/prom.h>
24#include <asm/io.h>
25#include <asm/smp.h>
26#include <asm/irq.h>
27#include <asm/errno.h>
28#include <asm/xive.h>
29#include <asm/xive-regs.h>
30#include <asm/hvcall.h>
31
32#include "xive-internal.h"
33
34static u32 xive_queue_shift;
35
36struct xive_irq_bitmap {
37 unsigned long *bitmap;
38 unsigned int base;
39 unsigned int count;
40 spinlock_t lock;
41 struct list_head list;
42};
43
44static LIST_HEAD(xive_irq_bitmaps);
45
46static int xive_irq_bitmap_add(int base, int count)
47{
48 struct xive_irq_bitmap *xibm;
49
50 xibm = kzalloc(sizeof(*xibm), GFP_ATOMIC);
51 if (!xibm)
52 return -ENOMEM;
53
54 spin_lock_init(&xibm->lock);
55 xibm->base = base;
56 xibm->count = count;
57 xibm->bitmap = kzalloc(xibm->count, GFP_KERNEL);
58 list_add(&xibm->list, &xive_irq_bitmaps);
59
60 pr_info("Using IRQ range [%x-%x]", xibm->base,
61 xibm->base + xibm->count - 1);
62 return 0;
63}
64
65static int __xive_irq_bitmap_alloc(struct xive_irq_bitmap *xibm)
66{
67 int irq;
68
69 irq = find_first_zero_bit(xibm->bitmap, xibm->count);
70 if (irq != xibm->count) {
71 set_bit(irq, xibm->bitmap);
72 irq += xibm->base;
73 } else {
74 irq = -ENOMEM;
75 }
76
77 return irq;
78}
79
80static int xive_irq_bitmap_alloc(void)
81{
82 struct xive_irq_bitmap *xibm;
83 unsigned long flags;
84 int irq = -ENOENT;
85
86 list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
87 spin_lock_irqsave(&xibm->lock, flags);
88 irq = __xive_irq_bitmap_alloc(xibm);
89 spin_unlock_irqrestore(&xibm->lock, flags);
90 if (irq >= 0)
91 break;
92 }
93 return irq;
94}
95
96static void xive_irq_bitmap_free(int irq)
97{
98 unsigned long flags;
99 struct xive_irq_bitmap *xibm;
100
101 list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
102 if ((irq >= xibm->base) && (irq < xibm->base + xibm->count)) {
103 spin_lock_irqsave(&xibm->lock, flags);
104 clear_bit(irq - xibm->base, xibm->bitmap);
105 spin_unlock_irqrestore(&xibm->lock, flags);
106 break;
107 }
108 }
109}
110
111static long plpar_int_get_source_info(unsigned long flags,
112 unsigned long lisn,
113 unsigned long *src_flags,
114 unsigned long *eoi_page,
115 unsigned long *trig_page,
116 unsigned long *esb_shift)
117{
118 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
119 long rc;
120
121 rc = plpar_hcall(H_INT_GET_SOURCE_INFO, retbuf, flags, lisn);
122 if (rc) {
123 pr_err("H_INT_GET_SOURCE_INFO lisn=%ld failed %ld\n", lisn, rc);
124 return rc;
125 }
126
127 *src_flags = retbuf[0];
128 *eoi_page = retbuf[1];
129 *trig_page = retbuf[2];
130 *esb_shift = retbuf[3];
131
132 pr_devel("H_INT_GET_SOURCE_INFO flags=%lx eoi=%lx trig=%lx shift=%lx\n",
133 retbuf[0], retbuf[1], retbuf[2], retbuf[3]);
134
135 return 0;
136}
137
138#define XIVE_SRC_SET_EISN (1ull << (63 - 62))
139#define XIVE_SRC_MASK (1ull << (63 - 63)) /* unused */
140
141static long plpar_int_set_source_config(unsigned long flags,
142 unsigned long lisn,
143 unsigned long target,
144 unsigned long prio,
145 unsigned long sw_irq)
146{
147 long rc;
148
149
150 pr_devel("H_INT_SET_SOURCE_CONFIG flags=%lx lisn=%lx target=%lx prio=%lx sw_irq=%lx\n",
151 flags, lisn, target, prio, sw_irq);
152
153
154 rc = plpar_hcall_norets(H_INT_SET_SOURCE_CONFIG, flags, lisn,
155 target, prio, sw_irq);
156 if (rc) {
157 pr_err("H_INT_SET_SOURCE_CONFIG lisn=%ld target=%lx prio=%lx failed %ld\n",
158 lisn, target, prio, rc);
159 return rc;
160 }
161
162 return 0;
163}
164
165static long plpar_int_get_queue_info(unsigned long flags,
166 unsigned long target,
167 unsigned long priority,
168 unsigned long *esn_page,
169 unsigned long *esn_size)
170{
171 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
172 long rc;
173
174 rc = plpar_hcall(H_INT_GET_QUEUE_INFO, retbuf, flags, target, priority);
175 if (rc) {
176 pr_err("H_INT_GET_QUEUE_INFO cpu=%ld prio=%ld failed %ld\n",
177 target, priority, rc);
178 return rc;
179 }
180
181 *esn_page = retbuf[0];
182 *esn_size = retbuf[1];
183
184 pr_devel("H_INT_GET_QUEUE_INFO page=%lx size=%lx\n",
185 retbuf[0], retbuf[1]);
186
187 return 0;
188}
189
190#define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
191
192static long plpar_int_set_queue_config(unsigned long flags,
193 unsigned long target,
194 unsigned long priority,
195 unsigned long qpage,
196 unsigned long qsize)
197{
198 long rc;
199
200 pr_devel("H_INT_SET_QUEUE_CONFIG flags=%lx target=%lx priority=%lx qpage=%lx qsize=%lx\n",
201 flags, target, priority, qpage, qsize);
202
203 rc = plpar_hcall_norets(H_INT_SET_QUEUE_CONFIG, flags, target,
204 priority, qpage, qsize);
205 if (rc) {
206 pr_err("H_INT_SET_QUEUE_CONFIG cpu=%ld prio=%ld qpage=%lx returned %ld\n",
207 target, priority, qpage, rc);
208 return rc;
209 }
210
211 return 0;
212}
213
214static long plpar_int_sync(unsigned long flags, unsigned long lisn)
215{
216 long rc;
217
218 rc = plpar_hcall_norets(H_INT_SYNC, flags, lisn);
219 if (rc) {
220 pr_err("H_INT_SYNC lisn=%ld returned %ld\n", lisn, rc);
221 return rc;
222 }
223
224 return 0;
225}
226
227#define XIVE_SRC_H_INT_ESB (1ull << (63 - 60)) /* TODO */
228#define XIVE_SRC_LSI (1ull << (63 - 61))
229#define XIVE_SRC_TRIGGER (1ull << (63 - 62))
230#define XIVE_SRC_STORE_EOI (1ull << (63 - 63))
231
232static int xive_spapr_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
233{
234 long rc;
235 unsigned long flags;
236 unsigned long eoi_page;
237 unsigned long trig_page;
238 unsigned long esb_shift;
239
240 memset(data, 0, sizeof(*data));
241
242 rc = plpar_int_get_source_info(0, hw_irq, &flags, &eoi_page, &trig_page,
243 &esb_shift);
244 if (rc)
245 return -EINVAL;
246
247 if (flags & XIVE_SRC_STORE_EOI)
248 data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
249 if (flags & XIVE_SRC_LSI)
250 data->flags |= XIVE_IRQ_FLAG_LSI;
251 data->eoi_page = eoi_page;
252 data->esb_shift = esb_shift;
253 data->trig_page = trig_page;
254
255 /*
256 * No chip-id for the sPAPR backend. This has an impact how we
257 * pick a target. See xive_pick_irq_target().
258 */
259 data->src_chip = XIVE_INVALID_CHIP_ID;
260
261 data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
262 if (!data->eoi_mmio) {
263 pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
264 return -ENOMEM;
265 }
266
267 /* Full function page supports trigger */
268 if (flags & XIVE_SRC_TRIGGER) {
269 data->trig_mmio = data->eoi_mmio;
270 return 0;
271 }
272
273 data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
274 if (!data->trig_mmio) {
275 pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
276 return -ENOMEM;
277 }
278 return 0;
279}
280
281static int xive_spapr_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
282{
283 long rc;
284
285 rc = plpar_int_set_source_config(XIVE_SRC_SET_EISN, hw_irq, target,
286 prio, sw_irq);
287
288 return rc == 0 ? 0 : -ENXIO;
289}
290
291/* This can be called multiple time to change a queue configuration */
292static int xive_spapr_configure_queue(u32 target, struct xive_q *q, u8 prio,
293 __be32 *qpage, u32 order)
294{
295 s64 rc = 0;
296 unsigned long esn_page;
297 unsigned long esn_size;
298 u64 flags, qpage_phys;
299
300 /* If there's an actual queue page, clean it */
301 if (order) {
302 if (WARN_ON(!qpage))
303 return -EINVAL;
304 qpage_phys = __pa(qpage);
305 } else {
306 qpage_phys = 0;
307 }
308
309 /* Initialize the rest of the fields */
310 q->msk = order ? ((1u << (order - 2)) - 1) : 0;
311 q->idx = 0;
312 q->toggle = 0;
313
314 rc = plpar_int_get_queue_info(0, target, prio, &esn_page, &esn_size);
315 if (rc) {
316 pr_err("Error %lld getting queue info prio %d\n", rc, prio);
317 rc = -EIO;
318 goto fail;
319 }
320
321 /* TODO: add support for the notification page */
322 q->eoi_phys = esn_page;
323
324 /* Default is to always notify */
325 flags = XIVE_EQ_ALWAYS_NOTIFY;
326
327 /* Configure and enable the queue in HW */
328 rc = plpar_int_set_queue_config(flags, target, prio, qpage_phys, order);
329 if (rc) {
330 pr_err("Error %lld setting queue for prio %d\n", rc, prio);
331 rc = -EIO;
332 } else {
333 q->qpage = qpage;
334 }
335fail:
336 return rc;
337}
338
339static int xive_spapr_setup_queue(unsigned int cpu, struct xive_cpu *xc,
340 u8 prio)
341{
342 struct xive_q *q = &xc->queue[prio];
343 __be32 *qpage;
344
345 qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
346 if (IS_ERR(qpage))
347 return PTR_ERR(qpage);
348
349 return xive_spapr_configure_queue(cpu, q, prio, qpage,
350 xive_queue_shift);
351}
352
353static void xive_spapr_cleanup_queue(unsigned int cpu, struct xive_cpu *xc,
354 u8 prio)
355{
356 struct xive_q *q = &xc->queue[prio];
357 unsigned int alloc_order;
358 long rc;
359
360 rc = plpar_int_set_queue_config(0, cpu, prio, 0, 0);
361 if (rc)
362 pr_err("Error %ld setting queue for prio %d\n", rc, prio);
363
364 alloc_order = xive_alloc_order(xive_queue_shift);
365 free_pages((unsigned long)q->qpage, alloc_order);
366 q->qpage = NULL;
367}
368
369static bool xive_spapr_match(struct device_node *node)
370{
371 /* Ignore cascaded controllers for the moment */
372 return 1;
373}
374
375#ifdef CONFIG_SMP
376static int xive_spapr_get_ipi(unsigned int cpu, struct xive_cpu *xc)
377{
378 int irq = xive_irq_bitmap_alloc();
379
380 if (irq < 0) {
381 pr_err("Failed to allocate IPI on CPU %d\n", cpu);
382 return -ENXIO;
383 }
384
385 xc->hw_ipi = irq;
386 return 0;
387}
388
389static void xive_spapr_put_ipi(unsigned int cpu, struct xive_cpu *xc)
390{
391 xive_irq_bitmap_free(xc->hw_ipi);
392}
393#endif /* CONFIG_SMP */
394
395static void xive_spapr_shutdown(void)
396{
397 long rc;
398
399 rc = plpar_hcall_norets(H_INT_RESET, 0);
400 if (rc)
401 pr_err("H_INT_RESET failed %ld\n", rc);
402}
403
404/*
405 * Perform an "ack" cycle on the current thread. Grab the pending
406 * active priorities and update the CPPR to the most favored one.
407 */
408static void xive_spapr_update_pending(struct xive_cpu *xc)
409{
410 u8 nsr, cppr;
411 u16 ack;
412
413 /*
414 * Perform the "Acknowledge O/S to Register" cycle.
415 *
416 * Let's speedup the access to the TIMA using the raw I/O
417 * accessor as we don't need the synchronisation routine of
418 * the higher level ones
419 */
420 ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_OS_REG));
421
422 /* Synchronize subsequent queue accesses */
423 mb();
424
425 /*
426 * Grab the CPPR and the "NSR" field which indicates the source
427 * of the interrupt (if any)
428 */
429 cppr = ack & 0xff;
430 nsr = ack >> 8;
431
432 if (nsr & TM_QW1_NSR_EO) {
433 if (cppr == 0xff)
434 return;
435 /* Mark the priority pending */
436 xc->pending_prio |= 1 << cppr;
437
438 /*
439 * A new interrupt should never have a CPPR less favored
440 * than our current one.
441 */
442 if (cppr >= xc->cppr)
443 pr_err("CPU %d odd ack CPPR, got %d at %d\n",
444 smp_processor_id(), cppr, xc->cppr);
445
446 /* Update our idea of what the CPPR is */
447 xc->cppr = cppr;
448 }
449}
450
451static void xive_spapr_eoi(u32 hw_irq)
452{
453 /* Not used */;
454}
455
456static void xive_spapr_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
457{
458 /* Only some debug on the TIMA settings */
459 pr_debug("(HW value: %08x %08x %08x)\n",
460 in_be32(xive_tima + TM_QW1_OS + TM_WORD0),
461 in_be32(xive_tima + TM_QW1_OS + TM_WORD1),
462 in_be32(xive_tima + TM_QW1_OS + TM_WORD2));
463}
464
465static void xive_spapr_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
466{
467 /* Nothing to do */;
468}
469
470static void xive_spapr_sync_source(u32 hw_irq)
471{
472 /* Specs are unclear on what this is doing */
473 plpar_int_sync(0, hw_irq);
474}
475
476static const struct xive_ops xive_spapr_ops = {
477 .populate_irq_data = xive_spapr_populate_irq_data,
478 .configure_irq = xive_spapr_configure_irq,
479 .setup_queue = xive_spapr_setup_queue,
480 .cleanup_queue = xive_spapr_cleanup_queue,
481 .match = xive_spapr_match,
482 .shutdown = xive_spapr_shutdown,
483 .update_pending = xive_spapr_update_pending,
484 .eoi = xive_spapr_eoi,
485 .setup_cpu = xive_spapr_setup_cpu,
486 .teardown_cpu = xive_spapr_teardown_cpu,
487 .sync_source = xive_spapr_sync_source,
488#ifdef CONFIG_SMP
489 .get_ipi = xive_spapr_get_ipi,
490 .put_ipi = xive_spapr_put_ipi,
491#endif /* CONFIG_SMP */
492 .name = "spapr",
493};
494
495/*
496 * get max priority from "/ibm,plat-res-int-priorities"
497 */
498static bool xive_get_max_prio(u8 *max_prio)
499{
500 struct device_node *rootdn;
501 const __be32 *reg;
502 u32 len;
503 int prio, found;
504
505 rootdn = of_find_node_by_path("/");
506 if (!rootdn) {
507 pr_err("not root node found !\n");
508 return false;
509 }
510
511 reg = of_get_property(rootdn, "ibm,plat-res-int-priorities", &len);
512 if (!reg) {
513 pr_err("Failed to read 'ibm,plat-res-int-priorities' property\n");
514 return false;
515 }
516
517 if (len % (2 * sizeof(u32)) != 0) {
518 pr_err("invalid 'ibm,plat-res-int-priorities' property\n");
519 return false;
520 }
521
522 /* HW supports priorities in the range [0-7] and 0xFF is a
523 * wildcard priority used to mask. We scan the ranges reserved
524 * by the hypervisor to find the lowest priority we can use.
525 */
526 found = 0xFF;
527 for (prio = 0; prio < 8; prio++) {
528 int reserved = 0;
529 int i;
530
531 for (i = 0; i < len / (2 * sizeof(u32)); i++) {
532 int base = be32_to_cpu(reg[2 * i]);
533 int range = be32_to_cpu(reg[2 * i + 1]);
534
535 if (prio >= base && prio < base + range)
536 reserved++;
537 }
538
539 if (!reserved)
540 found = prio;
541 }
542
543 if (found == 0xFF) {
544 pr_err("no valid priority found in 'ibm,plat-res-int-priorities'\n");
545 return false;
546 }
547
548 *max_prio = found;
549 return true;
550}
551
552bool xive_spapr_init(void)
553{
554 struct device_node *np;
555 struct resource r;
556 void __iomem *tima;
557 struct property *prop;
558 u8 max_prio;
559 u32 val;
560 u32 len;
561 const __be32 *reg;
562 int i;
563
564 if (xive_cmdline_disabled)
565 return false;
566
567 pr_devel("%s()\n", __func__);
568 np = of_find_compatible_node(NULL, NULL, "ibm,power-ivpe");
569 if (!np) {
570 pr_devel("not found !\n");
571 return false;
572 }
573 pr_devel("Found %s\n", np->full_name);
574
575 /* Resource 1 is the OS ring TIMA */
576 if (of_address_to_resource(np, 1, &r)) {
577 pr_err("Failed to get thread mgmnt area resource\n");
578 return false;
579 }
580 tima = ioremap(r.start, resource_size(&r));
581 if (!tima) {
582 pr_err("Failed to map thread mgmnt area\n");
583 return false;
584 }
585
586 if (!xive_get_max_prio(&max_prio))
587 return false;
588
589 /* Feed the IRQ number allocator with the ranges given in the DT */
590 reg = of_get_property(np, "ibm,xive-lisn-ranges", &len);
591 if (!reg) {
592 pr_err("Failed to read 'ibm,xive-lisn-ranges' property\n");
593 return false;
594 }
595
596 if (len % (2 * sizeof(u32)) != 0) {
597 pr_err("invalid 'ibm,xive-lisn-ranges' property\n");
598 return false;
599 }
600
601 for (i = 0; i < len / (2 * sizeof(u32)); i++, reg += 2)
602 xive_irq_bitmap_add(be32_to_cpu(reg[0]),
603 be32_to_cpu(reg[1]));
604
605 /* Iterate the EQ sizes and pick one */
606 of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, reg, val) {
607 xive_queue_shift = val;
608 if (val == PAGE_SHIFT)
609 break;
610 }
611
612 /* Initialize XIVE core with our backend */
613 if (!xive_core_init(&xive_spapr_ops, tima, TM_QW1_OS, max_prio))
614 return false;
615
616 pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
617 return true;
618}