]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/intr_machdep.c
Import tzdata 2018a
[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->event != 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 u_int
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         return (p->base);
363 }
364
365 u_int
366 powerpc_get_irq(uint32_t node, u_int pin)
367 {
368         int idx;
369
370         if (node == 0)
371                 return (pin);
372
373         mtx_lock(&intr_table_lock);
374         for (idx = 0; idx < npics; idx++) {
375                 if (piclist[idx].node == node) {
376                         mtx_unlock(&intr_table_lock);
377                         return (piclist[idx].base + pin);
378                 }
379         }
380
381         /*
382          * XXX we should never encounter an unregistered PIC, but that
383          * can only be done when we properly support bus enumeration
384          * using multiple passes. Until then, fake an entry and give it
385          * some adhoc maximum number of IRQs and IPIs.
386          */
387         piclist[idx].dev = NULL;
388         piclist[idx].node = node;
389         piclist[idx].irqs = 124;
390         piclist[idx].ipis = 4;
391         piclist[idx].base = nirqs;
392         nirqs += (1 << 25);
393         npics++;
394
395         KASSERT(npics < MAX_PICS,
396             ("Number of PICs exceeds maximum (%d)", MAX_PICS));
397
398         mtx_unlock(&intr_table_lock);
399
400         return (piclist[idx].base + pin);
401 }
402
403 int
404 powerpc_enable_intr(void)
405 {
406         struct powerpc_intr *i;
407         int error, vector;
408 #ifdef SMP
409         int n;
410 #endif
411
412         if (npics == 0)
413                 panic("no PIC detected\n");
414
415         if (root_pic == NULL)
416                 root_pic = piclist[0].dev;
417
418 #ifdef SMP
419         /* Install an IPI handler. */
420         if (mp_ncpus > 1) {
421                 for (n = 0; n < npics; n++) {
422                         if (piclist[n].dev != root_pic)
423                                 continue;
424
425                         KASSERT(piclist[n].ipis != 0,
426                             ("%s: SMP root PIC does not supply any IPIs",
427                             __func__));
428                         error = powerpc_setup_intr("IPI",
429                             MAP_IRQ(piclist[n].node, piclist[n].irqs),
430                             powerpc_ipi_handler, NULL, NULL,
431                             INTR_TYPE_MISC | INTR_EXCL, &ipi_cookie);
432                         if (error) {
433                                 printf("unable to setup IPI handler\n");
434                                 return (error);
435                         }
436
437                         /*
438                          * Some subterfuge: disable late EOI and mark this
439                          * as an IPI to the dispatch layer.
440                          */
441                         i = intr_lookup(MAP_IRQ(piclist[n].node,
442                             piclist[n].irqs));
443                         i->event->ie_post_filter = NULL;
444                         i->ipi = 1;
445                 }
446         }
447 #endif
448
449         for (vector = 0; vector < nvectors; vector++) {
450                 i = powerpc_intrs[vector];
451                 if (i == NULL)
452                         continue;
453
454                 error = powerpc_map_irq(i);
455                 if (error)
456                         continue;
457
458                 if (i->trig == INTR_TRIGGER_INVALID)
459                         PIC_TRANSLATE_CODE(i->pic, i->intline, i->fwcode,
460                             &i->trig, &i->pol);
461                 if (i->trig != INTR_TRIGGER_CONFORM ||
462                     i->pol != INTR_POLARITY_CONFORM)
463                         PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
464
465                 if (i->event != NULL)
466                         PIC_ENABLE(i->pic, i->intline, vector);
467         }
468
469         return (0);
470 }
471
472 int
473 powerpc_setup_intr(const char *name, u_int irq, driver_filter_t filter,
474     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
475 {
476         struct powerpc_intr *i;
477         int error, enable = 0;
478
479         i = intr_lookup(irq);
480         if (i == NULL)
481                 return (ENOMEM);
482
483         if (i->event == NULL) {
484                 error = intr_event_create(&i->event, (void *)i, 0, irq,
485                     powerpc_intr_pre_ithread, powerpc_intr_post_ithread,
486                     powerpc_intr_eoi, powerpc_assign_intr_cpu, "irq%u:", irq);
487                 if (error)
488                         return (error);
489
490                 enable = 1;
491         }
492
493         error = intr_event_add_handler(i->event, name, filter, handler, arg,
494             intr_priority(flags), flags, cookiep);
495
496         mtx_lock(&intr_table_lock);
497         intrcnt_setname(i->event->ie_fullname, i->cntindex);
498         mtx_unlock(&intr_table_lock);
499
500         if (!cold) {
501                 error = powerpc_map_irq(i);
502
503                 if (!error) {
504                         if (i->trig == INTR_TRIGGER_INVALID)
505                                 PIC_TRANSLATE_CODE(i->pic, i->intline,
506                                     i->fwcode, &i->trig, &i->pol);
507         
508                         if (i->trig != INTR_TRIGGER_CONFORM ||
509                             i->pol != INTR_POLARITY_CONFORM)
510                                 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
511
512                         if (i->pic == root_pic)
513                                 PIC_BIND(i->pic, i->intline, i->cpu);
514
515                         if (enable)
516                                 PIC_ENABLE(i->pic, i->intline, i->vector);
517                 }
518         }
519         return (error);
520 }
521
522 int
523 powerpc_teardown_intr(void *cookie)
524 {
525
526         return (intr_event_remove_handler(cookie));
527 }
528
529 #ifdef SMP
530 int
531 powerpc_bind_intr(u_int irq, u_char cpu)
532 {
533         struct powerpc_intr *i;
534
535         i = intr_lookup(irq);
536         if (i == NULL)
537                 return (ENOMEM);
538
539         return (intr_event_bind(i->event, cpu));
540 }
541 #endif
542
543 int
544 powerpc_fw_config_intr(int irq, int sense_code)
545 {
546         struct powerpc_intr *i;
547
548         i = intr_lookup(irq);
549         if (i == NULL)
550                 return (ENOMEM);
551
552         i->trig = INTR_TRIGGER_INVALID;
553         i->pol = INTR_POLARITY_CONFORM;
554         i->fwcode = sense_code;
555
556         if (!cold && i->pic != NULL) {
557                 PIC_TRANSLATE_CODE(i->pic, i->intline, i->fwcode, &i->trig,
558                     &i->pol);
559                 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
560         }
561
562         return (0);
563 }
564
565 int
566 powerpc_config_intr(int irq, enum intr_trigger trig, enum intr_polarity pol)
567 {
568         struct powerpc_intr *i;
569
570         i = intr_lookup(irq);
571         if (i == NULL)
572                 return (ENOMEM);
573
574         i->trig = trig;
575         i->pol = pol;
576
577         if (!cold && i->pic != NULL)
578                 PIC_CONFIG(i->pic, i->intline, trig, pol);
579
580         return (0);
581 }
582
583 void
584 powerpc_dispatch_intr(u_int vector, struct trapframe *tf)
585 {
586         struct powerpc_intr *i;
587         struct intr_event *ie;
588
589         i = powerpc_intrs[vector];
590         if (i == NULL)
591                 goto stray;
592
593         (*i->cntp)++;
594
595         ie = i->event;
596         KASSERT(ie != NULL, ("%s: interrupt without an event", __func__));
597
598         /*
599          * IPIs are magical and need to be EOI'ed before filtering.
600          * This prevents races in IPI handling.
601          */
602         if (i->ipi)
603                 PIC_EOI(i->pic, i->intline);
604
605         if (intr_event_handle(ie, tf) != 0) {
606                 goto stray;
607         }
608         return;
609
610 stray:
611         stray_count++;
612         if (stray_count <= MAX_STRAY_LOG) {
613                 printf("stray irq %d\n", i ? i->irq : -1);
614                 if (stray_count >= MAX_STRAY_LOG) {
615                         printf("got %d stray interrupts, not logging anymore\n",
616                             MAX_STRAY_LOG);
617                 }
618         }
619         if (i != NULL)
620                 PIC_MASK(i->pic, i->intline);
621 }
622
623 void
624 powerpc_intr_mask(u_int irq)
625 {
626         struct powerpc_intr *i;
627
628         i = intr_lookup(irq);
629         if (i == NULL || i->pic == NULL)
630                 return;
631
632         PIC_MASK(i->pic, i->intline);
633 }
634
635 void
636 powerpc_intr_unmask(u_int irq)
637 {
638         struct powerpc_intr *i;
639
640         i = intr_lookup(irq);
641         if (i == NULL || i->pic == NULL)
642                 return;
643
644         PIC_UNMASK(i->pic, i->intline);
645 }