]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/intr_machdep.c
Merge ^/head r325999 through r326131.
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / intr_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*-
35  * Copyright (c) 2002 Benno Rice.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *      from: @(#)isa.c 7.2 (Berkeley) 5/13/91
60  *      form: src/sys/i386/isa/intr_machdep.c,v 1.57 2001/07/20
61  *
62  * $FreeBSD$
63  */
64
65 #include "opt_isa.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/queue.h>
71 #include <sys/bus.h>
72 #include <sys/cpuset.h>
73 #include <sys/interrupt.h>
74 #include <sys/ktr.h>
75 #include <sys/lock.h>
76 #include <sys/malloc.h>
77 #include <sys/mutex.h>
78 #include <sys/pcpu.h>
79 #include <sys/smp.h>
80 #include <sys/syslog.h>
81 #include <sys/vmmeter.h>
82 #include <sys/proc.h>
83
84 #include <machine/frame.h>
85 #include <machine/intr_machdep.h>
86 #include <machine/md_var.h>
87 #include <machine/smp.h>
88 #include <machine/trap.h>
89
90 #include "pic_if.h"
91
92 #define MAX_STRAY_LOG   5
93
94 static MALLOC_DEFINE(M_INTR, "intr", "interrupt handler data");
95
96 struct powerpc_intr {
97         struct intr_event *event;
98         long    *cntp;
99         u_int   irq;
100         device_t pic;
101         u_int   intline;
102         u_int   vector;
103         u_int   cntindex;
104         cpuset_t cpu;
105         enum intr_trigger trig;
106         enum intr_polarity pol;
107         int     fwcode;
108         int     ipi;
109 };
110
111 struct pic {
112         device_t dev;
113         uint32_t node;
114         u_int   irqs;
115         u_int   ipis;
116         int     base;
117 };
118
119 static u_int intrcnt_index = 0;
120 static struct mtx intr_table_lock;
121 static struct powerpc_intr *powerpc_intrs[INTR_VECTORS];
122 static struct pic piclist[MAX_PICS];
123 static u_int nvectors;          /* Allocated vectors */
124 static u_int npics;             /* PICs registered */
125 #ifdef DEV_ISA
126 static u_int nirqs = 16;        /* Allocated IRQS (ISA pre-allocated). */
127 #else
128 static u_int nirqs = 0;         /* Allocated IRQs. */
129 #endif
130 static u_int stray_count;
131
132 u_long intrcnt[INTR_VECTORS];
133 char intrnames[INTR_VECTORS * MAXCOMLEN];
134 size_t sintrcnt = sizeof(intrcnt);
135 size_t sintrnames = sizeof(intrnames);
136
137 device_t root_pic;
138
139 #ifdef SMP
140 static void *ipi_cookie;
141 #endif
142
143 static void
144 intr_init(void *dummy __unused)
145 {
146
147         mtx_init(&intr_table_lock, "intr sources lock", NULL, MTX_DEF);
148 }
149 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
150
151 #ifdef SMP
152 static void
153 smp_intr_init(void *dummy __unused)
154 {
155         struct powerpc_intr *i;
156         int vector;
157
158         for (vector = 0; vector < nvectors; vector++) {
159                 i = powerpc_intrs[vector];
160                 if (i != NULL && i->pic == root_pic)
161                         PIC_BIND(i->pic, i->intline, i->cpu);
162         }
163 }
164 SYSINIT(smp_intr_init, SI_SUB_SMP, SI_ORDER_ANY, smp_intr_init, NULL);
165 #endif
166
167 static void
168 intrcnt_setname(const char *name, int index)
169 {
170
171         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
172             MAXCOMLEN, name);
173 }
174
175 void
176 intrcnt_add(const char *name, u_long **countp)
177 {
178         int idx;
179
180         idx = atomic_fetchadd_int(&intrcnt_index, 1);
181         *countp = &intrcnt[idx];
182         intrcnt_setname(name, idx);
183 }
184
185 static struct powerpc_intr *
186 intr_lookup(u_int irq)
187 {
188         char intrname[16];
189         struct powerpc_intr *i, *iscan;
190         int vector;
191
192         mtx_lock(&intr_table_lock);
193         for (vector = 0; vector < nvectors; vector++) {
194                 i = powerpc_intrs[vector];
195                 if (i != NULL && i->irq == irq) {
196                         mtx_unlock(&intr_table_lock);
197                         return (i);
198                 }
199         }
200
201         i = malloc(sizeof(*i), M_INTR, M_NOWAIT);
202         if (i == NULL) {
203                 mtx_unlock(&intr_table_lock);
204                 return (NULL);
205         }
206
207         i->event = NULL;
208         i->cntp = NULL;
209         i->trig = INTR_TRIGGER_CONFORM;
210         i->pol = INTR_POLARITY_CONFORM;
211         i->irq = irq;
212         i->pic = NULL;
213         i->vector = -1;
214         i->fwcode = 0;
215         i->ipi = 0;
216
217 #ifdef SMP
218         i->cpu = all_cpus;
219 #else
220         CPU_SETOF(0, &i->cpu);
221 #endif
222
223         for (vector = 0; vector < INTR_VECTORS && vector <= nvectors;
224             vector++) {
225                 iscan = powerpc_intrs[vector];
226                 if (iscan != NULL && iscan->irq == irq)
227                         break;
228                 if (iscan == NULL && i->vector == -1)
229                         i->vector = vector;
230                 iscan = NULL;
231         }
232
233         if (iscan == NULL && i->vector != -1) {
234                 powerpc_intrs[i->vector] = i;
235                 i->cntindex = atomic_fetchadd_int(&intrcnt_index, 1);
236                 i->cntp = &intrcnt[i->cntindex];
237                 sprintf(intrname, "irq%u:", i->irq);
238                 intrcnt_setname(intrname, i->cntindex);
239                 nvectors++;
240         }
241         mtx_unlock(&intr_table_lock);
242
243         if (iscan != NULL || i->vector == -1) {
244                 free(i, M_INTR);
245                 i = iscan;
246         }
247
248         return (i);
249 }
250
251 static int
252 powerpc_map_irq(struct powerpc_intr *i)
253 {
254         struct pic *p;
255         u_int cnt;
256         int idx;
257
258         for (idx = 0; idx < npics; idx++) {
259                 p = &piclist[idx];
260                 cnt = p->irqs + p->ipis;
261                 if (i->irq >= p->base && i->irq < p->base + cnt)
262                         break;
263         }
264         if (idx == npics)
265                 return (EINVAL);
266
267         i->intline = i->irq - p->base;
268         i->pic = p->dev;
269
270         /* Try a best guess if that failed */
271         if (i->pic == NULL)
272                 i->pic = root_pic;
273
274         return (0);
275 }
276
277 static void
278 powerpc_intr_eoi(void *arg)
279 {
280         struct powerpc_intr *i = arg;
281
282         PIC_EOI(i->pic, i->intline);
283 }
284
285 static void
286 powerpc_intr_pre_ithread(void *arg)
287 {
288         struct powerpc_intr *i = arg;
289
290         PIC_MASK(i->pic, i->intline);
291         PIC_EOI(i->pic, i->intline);
292 }
293
294 static void
295 powerpc_intr_post_ithread(void *arg)
296 {
297         struct powerpc_intr *i = arg;
298
299         PIC_UNMASK(i->pic, i->intline);
300 }
301
302 static int
303 powerpc_assign_intr_cpu(void *arg, int cpu)
304 {
305 #ifdef SMP
306         struct powerpc_intr *i = arg;
307
308         if (cpu == NOCPU)
309                 i->cpu = all_cpus;
310         else
311                 CPU_SETOF(cpu, &i->cpu);
312
313         if (!cold && i->pic != NULL && i->pic == root_pic)
314                 PIC_BIND(i->pic, i->intline, i->cpu);
315
316         return (0);
317 #else
318         return (EOPNOTSUPP);
319 #endif
320 }
321
322 void
323 powerpc_register_pic(device_t dev, uint32_t node, u_int irqs, u_int ipis,
324     u_int atpic)
325 {
326         struct pic *p;
327         u_int irq;
328         int idx;
329
330         mtx_lock(&intr_table_lock);
331
332         /* XXX see powerpc_get_irq(). */
333         for (idx = 0; idx < npics; idx++) {
334                 p = &piclist[idx];
335                 if (p->node != node)
336                         continue;
337                 if (node != 0 || p->dev == dev)
338                         break;
339         }
340         p = &piclist[idx];
341
342         p->dev = dev;
343         p->node = node;
344         p->irqs = irqs;
345         p->ipis = ipis;
346         if (idx == npics) {
347 #ifdef DEV_ISA
348                 p->base = (atpic) ? 0 : nirqs;
349 #else
350                 p->base = nirqs;
351 #endif
352                 irq = p->base + irqs + ipis;
353                 nirqs = MAX(nirqs, irq);
354                 npics++;
355         }
356
357         KASSERT(npics < MAX_PICS,
358             ("Number of PICs exceeds maximum (%d)", MAX_PICS));
359
360         mtx_unlock(&intr_table_lock);
361 }
362
363 u_int
364 powerpc_get_irq(uint32_t node, u_int pin)
365 {
366         int idx;
367
368         if (node == 0)
369                 return (pin);
370
371         mtx_lock(&intr_table_lock);
372         for (idx = 0; idx < npics; idx++) {
373                 if (piclist[idx].node == node) {
374                         mtx_unlock(&intr_table_lock);
375                         return (piclist[idx].base + pin);
376                 }
377         }
378
379         /*
380          * XXX we should never encounter an unregistered PIC, but that
381          * can only be done when we properly support bus enumeration
382          * using multiple passes. Until then, fake an entry and give it
383          * some adhoc maximum number of IRQs and IPIs.
384          */
385         piclist[idx].dev = NULL;
386         piclist[idx].node = node;
387         piclist[idx].irqs = 124;
388         piclist[idx].ipis = 4;
389         piclist[idx].base = nirqs;
390         nirqs += 128;
391         npics++;
392
393         KASSERT(npics < MAX_PICS,
394             ("Number of PICs exceeds maximum (%d)", MAX_PICS));
395
396         mtx_unlock(&intr_table_lock);
397
398         return (piclist[idx].base + pin);
399 }
400
401 int
402 powerpc_enable_intr(void)
403 {
404         struct powerpc_intr *i;
405         int error, vector;
406 #ifdef SMP
407         int n;
408 #endif
409
410         if (npics == 0)
411                 panic("no PIC detected\n");
412
413         if (root_pic == NULL)
414                 root_pic = piclist[0].dev;
415
416 #ifdef SMP
417         /* Install an IPI handler. */
418         if (mp_ncpus > 1) {
419                 for (n = 0; n < npics; n++) {
420                         if (piclist[n].dev != root_pic)
421                                 continue;
422
423                         KASSERT(piclist[n].ipis != 0,
424                             ("%s: SMP root PIC does not supply any IPIs",
425                             __func__));
426                         error = powerpc_setup_intr("IPI",
427                             MAP_IRQ(piclist[n].node, piclist[n].irqs),
428                             powerpc_ipi_handler, NULL, NULL,
429                             INTR_TYPE_MISC | INTR_EXCL, &ipi_cookie);
430                         if (error) {
431                                 printf("unable to setup IPI handler\n");
432                                 return (error);
433                         }
434
435                         /*
436                          * Some subterfuge: disable late EOI and mark this
437                          * as an IPI to the dispatch layer.
438                          */
439                         i = intr_lookup(MAP_IRQ(piclist[n].node,
440                             piclist[n].irqs));
441                         i->event->ie_post_filter = NULL;
442                         i->ipi = 1;
443                 }
444         }
445 #endif
446
447         for (vector = 0; vector < nvectors; vector++) {
448                 i = powerpc_intrs[vector];
449                 if (i == NULL)
450                         continue;
451
452                 error = powerpc_map_irq(i);
453                 if (error)
454                         continue;
455
456                 if (i->trig == INTR_TRIGGER_INVALID)
457                         PIC_TRANSLATE_CODE(i->pic, i->intline, i->fwcode,
458                             &i->trig, &i->pol);
459                 if (i->trig != INTR_TRIGGER_CONFORM ||
460                     i->pol != INTR_POLARITY_CONFORM)
461                         PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
462
463                 if (i->event != NULL)
464                         PIC_ENABLE(i->pic, i->intline, vector);
465         }
466
467         return (0);
468 }
469
470 int
471 powerpc_setup_intr(const char *name, u_int irq, driver_filter_t filter,
472     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
473 {
474         struct powerpc_intr *i;
475         int error, enable = 0;
476
477         i = intr_lookup(irq);
478         if (i == NULL)
479                 return (ENOMEM);
480
481         if (i->event == NULL) {
482                 error = intr_event_create(&i->event, (void *)i, 0, irq,
483                     powerpc_intr_pre_ithread, powerpc_intr_post_ithread,
484                     powerpc_intr_eoi, powerpc_assign_intr_cpu, "irq%u:", irq);
485                 if (error)
486                         return (error);
487
488                 enable = 1;
489         }
490
491         error = intr_event_add_handler(i->event, name, filter, handler, arg,
492             intr_priority(flags), flags, cookiep);
493
494         mtx_lock(&intr_table_lock);
495         intrcnt_setname(i->event->ie_fullname, i->cntindex);
496         mtx_unlock(&intr_table_lock);
497
498         if (!cold) {
499                 error = powerpc_map_irq(i);
500
501                 if (!error) {
502                         if (i->trig == INTR_TRIGGER_INVALID)
503                                 PIC_TRANSLATE_CODE(i->pic, i->intline,
504                                     i->fwcode, &i->trig, &i->pol);
505         
506                         if (i->trig != INTR_TRIGGER_CONFORM ||
507                             i->pol != INTR_POLARITY_CONFORM)
508                                 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
509
510                         if (i->pic == root_pic)
511                                 PIC_BIND(i->pic, i->intline, i->cpu);
512
513                         if (enable)
514                                 PIC_ENABLE(i->pic, i->intline, i->vector);
515                 }
516         }
517         return (error);
518 }
519
520 int
521 powerpc_teardown_intr(void *cookie)
522 {
523
524         return (intr_event_remove_handler(cookie));
525 }
526
527 #ifdef SMP
528 int
529 powerpc_bind_intr(u_int irq, u_char cpu)
530 {
531         struct powerpc_intr *i;
532
533         i = intr_lookup(irq);
534         if (i == NULL)
535                 return (ENOMEM);
536
537         return (intr_event_bind(i->event, cpu));
538 }
539 #endif
540
541 int
542 powerpc_fw_config_intr(int irq, int sense_code)
543 {
544         struct powerpc_intr *i;
545
546         i = intr_lookup(irq);
547         if (i == NULL)
548                 return (ENOMEM);
549
550         i->trig = INTR_TRIGGER_INVALID;
551         i->pol = INTR_POLARITY_CONFORM;
552         i->fwcode = sense_code;
553
554         if (!cold && i->pic != NULL) {
555                 PIC_TRANSLATE_CODE(i->pic, i->intline, i->fwcode, &i->trig,
556                     &i->pol);
557                 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
558         }
559
560         return (0);
561 }
562
563 int
564 powerpc_config_intr(int irq, enum intr_trigger trig, enum intr_polarity pol)
565 {
566         struct powerpc_intr *i;
567
568         i = intr_lookup(irq);
569         if (i == NULL)
570                 return (ENOMEM);
571
572         i->trig = trig;
573         i->pol = pol;
574
575         if (!cold && i->pic != NULL)
576                 PIC_CONFIG(i->pic, i->intline, trig, pol);
577
578         return (0);
579 }
580
581 void
582 powerpc_dispatch_intr(u_int vector, struct trapframe *tf)
583 {
584         struct powerpc_intr *i;
585         struct intr_event *ie;
586
587         i = powerpc_intrs[vector];
588         if (i == NULL)
589                 goto stray;
590
591         (*i->cntp)++;
592
593         ie = i->event;
594         KASSERT(ie != NULL, ("%s: interrupt without an event", __func__));
595
596         /*
597          * IPIs are magical and need to be EOI'ed before filtering.
598          * This prevents races in IPI handling.
599          */
600         if (i->ipi)
601                 PIC_EOI(i->pic, i->intline);
602
603         if (intr_event_handle(ie, tf) != 0) {
604                 goto stray;
605         }
606         return;
607
608 stray:
609         stray_count++;
610         if (stray_count <= MAX_STRAY_LOG) {
611                 printf("stray irq %d\n", i ? i->irq : -1);
612                 if (stray_count >= MAX_STRAY_LOG) {
613                         printf("got %d stray interrupts, not logging anymore\n",
614                             MAX_STRAY_LOG);
615                 }
616         }
617         if (i != NULL)
618                 PIC_MASK(i->pic, i->intline);
619 }
620
621 void
622 powerpc_intr_mask(u_int irq)
623 {
624         struct powerpc_intr *i;
625
626         i = intr_lookup(irq);
627         if (i == NULL || i->pic == NULL)
628                 return;
629
630         PIC_MASK(i->pic, i->intline);
631 }
632
633 void
634 powerpc_intr_unmask(u_int irq)
635 {
636         struct powerpc_intr *i;
637
638         i = intr_lookup(irq);
639         if (i == NULL || i->pic == NULL)
640                 return;
641
642         PIC_UNMASK(i->pic, i->intline);
643 }