]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sparc64/intr_machdep.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / sparc64 / intr_machdep.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*-
33  * Copyright (c) 2001 Jake Burkholder.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  *      from: @(#)isa.c 7.2 (Berkeley) 5/13/91
58  *      form: src/sys/i386/isa/intr_machdep.c,v 1.57 2001/07/20
59  */
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/bus.h>
67 #include <sys/errno.h>
68 #include <sys/interrupt.h>
69 #include <sys/kernel.h>
70 #include <sys/ktr.h>
71 #include <sys/lock.h>
72 #include <sys/mutex.h>
73 #include <sys/pcpu.h>
74 #include <sys/proc.h>
75 #include <sys/smp.h>
76 #include <sys/sx.h>
77
78 #include <machine/frame.h>
79 #include <machine/intr_machdep.h>
80
81 #define MAX_STRAY_LOG   5
82
83 CTASSERT((1 << IV_SHIFT) == sizeof(struct intr_vector));
84
85 ih_func_t *intr_handlers[PIL_MAX];
86 uint16_t pil_countp[PIL_MAX];
87
88 struct intr_vector intr_vectors[IV_MAX];
89 uint16_t intr_countp[IV_MAX];
90 static u_long intr_stray_count[IV_MAX];
91
92 static const char *const pil_names[] = {
93         "stray",
94         "low",          /* PIL_LOW */
95         "ithrd",        /* PIL_ITHREAD */
96         "rndzvs",       /* PIL_RENDEZVOUS */
97         "ast",          /* PIL_AST */
98         "stop",         /* PIL_STOP */
99         "preempt",      /* PIL_PREEMPT */
100         "stray", "stray", "stray", "stray", "stray",
101         "filter",       /* PIL_FILTER */
102         "fast",         /* PIL_FAST */
103         "tick",         /* PIL_TICK */
104 };
105
106 /* protect the intr_vectors table */
107 static struct sx intr_table_lock;
108 /* protect intrcnt_index */
109 static struct mtx intrcnt_lock;
110
111 #ifdef SMP
112 static int assign_cpu;
113
114 static void intr_assign_next_cpu(struct intr_vector *iv);
115 static void intr_shuffle_irqs(void *arg __unused);
116 #endif
117
118 static int intr_assign_cpu(void *arg, u_char cpu);
119 static void intr_execute_handlers(void *);
120 static void intr_stray_level(struct trapframe *);
121 static void intr_stray_vector(void *);
122 static int intrcnt_setname(const char *, int);
123 static void intrcnt_updatename(int, const char *, int);
124
125 static void
126 intrcnt_updatename(int vec, const char *name, int ispil)
127 {
128         static int intrcnt_index, stray_pil_index, stray_vec_index;
129         int name_index;
130
131         mtx_lock_spin(&intrcnt_lock);
132         if (intrnames[0] == '\0') {
133                 /* for bitbucket */
134                 if (bootverbose)
135                         printf("initalizing intr_countp\n");
136                 intrcnt_setname("???", intrcnt_index++);
137
138                 stray_vec_index = intrcnt_index++;
139                 intrcnt_setname("stray", stray_vec_index);
140                 for (name_index = 0; name_index < IV_MAX; name_index++)
141                         intr_countp[name_index] = stray_vec_index;
142
143                 stray_pil_index = intrcnt_index++;
144                 intrcnt_setname("pil", stray_pil_index);
145                 for (name_index = 0; name_index < PIL_MAX; name_index++)
146                         pil_countp[name_index] = stray_pil_index;
147         }
148
149         if (name == NULL)
150                 name = "???";
151
152         if (!ispil && intr_countp[vec] != stray_vec_index)
153                 name_index = intr_countp[vec];
154         else if (ispil && pil_countp[vec] != stray_pil_index)
155                 name_index = pil_countp[vec];
156         else
157                 name_index = intrcnt_index++;
158
159         if (intrcnt_setname(name, name_index))
160                 name_index = 0;
161
162         if (!ispil)
163                 intr_countp[vec] = name_index;
164         else
165                 pil_countp[vec] = name_index;
166         mtx_unlock_spin(&intrcnt_lock);
167 }
168
169 static int
170 intrcnt_setname(const char *name, int index)
171 {
172
173         if (intrnames + (MAXCOMLEN + 1) * index >= eintrnames)
174                 return (E2BIG);
175         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
176             MAXCOMLEN, name);
177         return (0);
178 }
179
180 void
181 intr_setup(int pri, ih_func_t *ihf, int vec, iv_func_t *ivf, void *iva)
182 {
183         char pilname[MAXCOMLEN + 1];
184         register_t s;
185
186         s = intr_disable();
187         if (vec != -1) {
188                 intr_vectors[vec].iv_func = ivf;
189                 intr_vectors[vec].iv_arg = iva;
190                 intr_vectors[vec].iv_pri = pri;
191                 intr_vectors[vec].iv_vec = vec;
192         }
193         intr_handlers[pri] = ihf;
194         intr_restore(s);
195         snprintf(pilname, MAXCOMLEN + 1, "pil%d: %s", pri, pil_names[pri]);
196         intrcnt_updatename(pri, pilname, 1);
197 }
198
199 static void
200 intr_stray_level(struct trapframe *tf)
201 {
202
203         printf("stray level interrupt %ld\n", tf->tf_level);
204 }
205
206 static void
207 intr_stray_vector(void *cookie)
208 {
209         struct intr_vector *iv;
210
211         iv = cookie;
212         if (intr_stray_count[iv->iv_vec] < MAX_STRAY_LOG) {
213                 printf("stray vector interrupt %d\n", iv->iv_vec);
214                 intr_stray_count[iv->iv_vec]++;
215                 if (intr_stray_count[iv->iv_vec] >= MAX_STRAY_LOG)
216                         printf("got %d stray interrupt %d's: not logging "
217                             "anymore\n", MAX_STRAY_LOG, iv->iv_vec);
218         }
219 }
220
221 void
222 intr_init1()
223 {
224         int i;
225
226         /* Mark all interrupts as being stray. */
227         for (i = 0; i < PIL_MAX; i++)
228                 intr_handlers[i] = intr_stray_level;
229         for (i = 0; i < IV_MAX; i++) {
230                 intr_vectors[i].iv_func = intr_stray_vector;
231                 intr_vectors[i].iv_arg = &intr_vectors[i];
232                 intr_vectors[i].iv_pri = PIL_LOW;
233                 intr_vectors[i].iv_vec = i;
234                 intr_vectors[i].iv_refcnt = 0;
235         }
236         intr_handlers[PIL_LOW] = intr_fast;
237 }
238
239 void
240 intr_init2()
241 {
242
243         sx_init(&intr_table_lock, "intr sources");
244         mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
245 }
246
247 static int
248 intr_assign_cpu(void *arg, u_char cpu)
249 {
250 #ifdef SMP
251         struct pcpu *pc;
252         struct intr_vector *iv;
253
254         /*
255          * Don't do anything during early boot.  We will pick up the
256          * assignment once the APs are started.
257          */
258         if (assign_cpu && cpu != NOCPU) {
259                 pc = pcpu_find(cpu);
260                 if (pc == NULL)
261                         return (EINVAL);
262                 iv = arg;
263                 sx_xlock(&intr_table_lock);
264                 iv->iv_mid = pc->pc_mid;
265                 iv->iv_ic->ic_assign(iv);
266                 sx_xunlock(&intr_table_lock);
267         }
268         return (0);
269 #else
270         return (EOPNOTSUPP);
271 #endif
272 }
273
274 static void
275 intr_execute_handlers(void *cookie)
276 {
277         struct intr_vector *iv;
278 #ifndef INTR_FILTER
279         struct intr_event *ie;
280         struct intr_handler *ih;
281         int error, thread, ret;
282 #endif
283
284         iv = cookie;
285 #ifndef INTR_FILTER
286         ie = iv->iv_event;
287         if (iv->iv_ic == NULL || ie == NULL) {
288                 intr_stray_vector(iv);
289                 return;
290         }
291
292         /* Execute fast interrupt handlers directly. */
293         ret = 0;
294         thread = 0;
295         critical_enter();
296         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
297                 if (ih->ih_filter == NULL) {
298                         thread = 1;
299                         continue;
300                 }
301                 MPASS(ih->ih_filter != NULL && ih->ih_argument != NULL);
302                 CTR3(KTR_INTR, "%s: executing handler %p(%p)", __func__,
303                     ih->ih_filter, ih->ih_argument);
304                 ret = ih->ih_filter(ih->ih_argument);
305                 /*
306                  * Wrapper handler special case: see
307                  * i386/intr_machdep.c::intr_execute_handlers()
308                  */
309                 if (!thread) {
310                         if (ret == FILTER_SCHEDULE_THREAD)
311                                 thread = 1;
312                 }
313         }
314         if (!thread)
315                 iv->iv_ic->ic_clear(iv);
316
317         /* Schedule a heavyweight interrupt process. */
318         if (thread)
319                 error = intr_event_schedule_thread(ie);
320         else if (TAILQ_EMPTY(&ie->ie_handlers))
321                 error = EINVAL;
322         else
323                 error = 0;
324         critical_exit();
325         if (error == EINVAL)
326 #else
327         if (iv->iv_ic == NULL || intr_event_handle(iv->iv_event, NULL) != 0)
328 #endif
329                 intr_stray_vector(iv);
330 }
331
332 int
333 intr_controller_register(int vec, const struct intr_controller *ic,
334     void *icarg)
335 {
336         struct intr_event *ie;
337         struct intr_vector *iv;
338         int error;
339
340         if (vec < 0 || vec >= IV_MAX)
341                 return (EINVAL);
342         sx_xlock(&intr_table_lock);
343         iv = &intr_vectors[vec];
344         ie = iv->iv_event;
345         sx_xunlock(&intr_table_lock);
346         if (ie != NULL)
347                 return (EEXIST);
348         error = intr_event_create(&ie, iv, 0, ic->ic_clear,
349 #ifdef INTR_FILTER
350             ic->ic_clear, NULL, intr_assign_cpu, "vec%d:", vec);
351 #else
352             intr_assign_cpu, "vec%d:", vec);
353 #endif
354         if (error != 0)
355                 return (error);
356         sx_xlock(&intr_table_lock);
357         if (iv->iv_event != NULL) {
358                 sx_xunlock(&intr_table_lock);
359                 intr_event_destroy(ie);
360                 return (EEXIST);
361         }
362         iv->iv_ic = ic;
363         iv->iv_icarg = icarg;
364         iv->iv_event = ie;
365         iv->iv_mid = PCPU_GET(mid);
366         sx_xunlock(&intr_table_lock);
367         return (0);
368 }
369
370 int
371 inthand_add(const char *name, int vec, driver_filter_t *filt,
372     driver_intr_t *handler, void *arg, int flags, void **cookiep)
373 {
374         const struct intr_controller *ic;
375         struct intr_event *ie;
376         struct intr_handler *ih;
377         struct intr_vector *iv;
378         int error, filter;
379
380         if (vec < 0 || vec >= IV_MAX)
381                 return (EINVAL);
382         /*
383          * INTR_FAST filters/handlers are special purpose only, allowing
384          * them to be shared just would complicate things unnecessarily.
385          */
386         if ((flags & INTR_FAST) != 0 && (flags & INTR_EXCL) == 0)
387                 return (EINVAL);
388         sx_xlock(&intr_table_lock);
389         iv = &intr_vectors[vec];
390         ic = iv->iv_ic;
391         ie = iv->iv_event;
392         sx_xunlock(&intr_table_lock);
393         if (ic == NULL || ie == NULL)
394                 return (EINVAL);
395         error = intr_event_add_handler(ie, name, filt, handler, arg,
396             intr_priority(flags), flags, cookiep);
397         if (error != 0)
398                 return (error);
399         sx_xlock(&intr_table_lock);
400         /* Disable the interrupt while we fiddle with it. */
401         ic->ic_disable(iv);
402         iv->iv_refcnt++;
403         if (iv->iv_refcnt == 1)
404                 intr_setup((flags & INTR_FAST) != 0 ? PIL_FAST :
405                     filt != NULL ? PIL_FILTER : PIL_ITHREAD, intr_fast,
406                     vec, intr_execute_handlers, iv);
407         else if (filt != NULL) {
408                 /*
409                  * Check if we need to upgrade from PIL_ITHREAD to PIL_FILTER.
410                  * Given that apart from the on-board SCCs and UARTs shared
411                  * interrupts are rather uncommon on sparc64 this sould be
412                  * pretty rare in practice.
413                  */
414                 filter = 0;
415                 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
416                         if (ih->ih_filter != NULL && ih->ih_filter != filt) {
417                                 filter = 1;
418                                 break;
419                         }
420                 }
421                 if (filter == 0)
422                         intr_setup(PIL_FILTER, intr_fast, vec,
423                             intr_execute_handlers, iv);
424         }
425         intr_stray_count[vec] = 0;
426         intrcnt_updatename(vec, ie->ie_fullname, 0);
427 #ifdef SMP
428         if (assign_cpu)
429                 intr_assign_next_cpu(iv);
430 #endif
431         ic->ic_enable(iv);
432         /* Ensure the interrupt is cleared, it might have triggered before. */
433         ic->ic_clear(iv);
434         sx_xunlock(&intr_table_lock);
435         return (0);
436 }
437
438 int
439 inthand_remove(int vec, void *cookie)
440 {
441         struct intr_vector *iv;
442         int error;
443
444         if (vec < 0 || vec >= IV_MAX)
445                 return (EINVAL);
446         error = intr_event_remove_handler(cookie);
447         if (error == 0) {
448                 /*
449                  * XXX: maybe this should be done regardless of whether
450                  * intr_event_remove_handler() succeeded?
451                  */
452                 sx_xlock(&intr_table_lock);
453                 iv = &intr_vectors[vec];
454                 iv->iv_refcnt--;
455                 if (iv->iv_refcnt == 0) {
456                         /*
457                          * Don't disable the interrupt for now, so that
458                          * stray interrupts get detected...
459                          */
460                         intr_setup(PIL_LOW, intr_fast, vec,
461                             intr_stray_vector, iv);
462                 }
463                 sx_xunlock(&intr_table_lock);
464         }
465         return (error);
466 }
467
468 #ifdef SMP
469 /*
470  * Support for balancing interrupt sources across CPUs.  For now we just
471  * allocate CPUs round-robin.
472  */
473
474 /* The BSP is always a valid target. */
475 static cpumask_t intr_cpus = (1 << 0);
476 static int current_cpu;
477
478 static void
479 intr_assign_next_cpu(struct intr_vector *iv)
480 {
481         struct pcpu *pc;
482
483         sx_assert(&intr_table_lock, SA_XLOCKED);
484
485         /*
486          * Assign this source to a CPU in a round-robin fashion.
487          */
488         pc = pcpu_find(current_cpu);
489         if (pc == NULL)
490                 return;
491         iv->iv_mid = pc->pc_mid;
492         iv->iv_ic->ic_assign(iv);
493         do {
494                 current_cpu++;
495                 if (current_cpu > mp_maxid)
496                         current_cpu = 0;
497         } while (!(intr_cpus & (1 << current_cpu)));
498 }
499
500 /* Attempt to bind the specified IRQ to the specified CPU. */
501 int
502 intr_bind(int vec, u_char cpu)
503 {
504         struct intr_vector *iv;
505
506         if (vec < 0 || vec >= IV_MAX)
507                 return (EINVAL);
508         iv = &intr_vectors[vec];
509         if (iv == NULL)
510                 return (EINVAL);
511         return (intr_event_bind(iv->iv_event, cpu));
512 }
513
514 /*
515  * Add a CPU to our mask of valid CPUs that can be destinations of
516  * interrupts.
517  */
518 void
519 intr_add_cpu(u_int cpu)
520 {
521
522         if (cpu >= MAXCPU)
523                 panic("%s: Invalid CPU ID", __func__);
524         if (bootverbose)
525                 printf("INTR: Adding CPU %d as a target\n", cpu);
526
527         intr_cpus |= (1 << cpu);
528 }
529
530 /*
531  * Distribute all the interrupt sources among the available CPUs once the
532  * APs have been launched.
533  */
534 static void
535 intr_shuffle_irqs(void *arg __unused)
536 {
537         struct pcpu *pc;
538         struct intr_vector *iv;
539         int i;
540
541         /* Don't bother on UP. */
542         if (mp_ncpus == 1)
543                 return;
544
545         sx_xlock(&intr_table_lock);
546         assign_cpu = 1;
547         for (i = 0; i < IV_MAX; i++) {
548                 iv = &intr_vectors[i];
549                 if (iv != NULL && iv->iv_refcnt > 0) {
550                         /*
551                          * If this event is already bound to a CPU,
552                          * then assign the source to that CPU instead
553                          * of picking one via round-robin.
554                          */
555                         if (iv->iv_event->ie_cpu != NOCPU &&
556                             (pc = pcpu_find(iv->iv_event->ie_cpu)) != NULL) {
557                                 iv->iv_mid = pc->pc_mid;
558                                 iv->iv_ic->ic_assign(iv);
559                         } else
560                                 intr_assign_next_cpu(iv);
561                 }
562         }
563         sx_xunlock(&intr_table_lock);
564 }
565 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,
566     NULL);
567 #endif