]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/intr_machdep.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / intr_machdep.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 /*
33  * Machine dependent interrupt code for amd64.  For amd64, we have to
34  * deal with different PICs.  Thus, we use the passed in vector to lookup
35  * an interrupt source associated with that vector.  The interrupt source
36  * describes which PIC the source belongs to and includes methods to handle
37  * that source.
38  */
39
40 #include "opt_atpic.h"
41 #include "opt_ddb.h"
42
43 #include <sys/param.h>
44 #include <sys/bus.h>
45 #include <sys/interrupt.h>
46 #include <sys/ktr.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/smp.h>
52 #include <sys/syslog.h>
53 #include <sys/systm.h>
54 #include <sys/sx.h>
55 #include <machine/clock.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/smp.h>
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61
62 #ifndef DEV_ATPIC
63 #include <machine/segments.h>
64 #include <machine/frame.h>
65 #include <dev/ic/i8259.h>
66 #include <amd64/isa/icu.h>
67 #include <amd64/isa/isa.h>
68 #endif
69
70 #define MAX_STRAY_LOG   5
71
72 typedef void (*mask_fn)(void *);
73
74 static int intrcnt_index;
75 static struct intsrc *interrupt_sources[NUM_IO_INTS];
76 static struct sx intr_table_lock;
77 static struct mtx intrcnt_lock;
78 static STAILQ_HEAD(, pic) pics;
79
80 #ifdef SMP
81 static int assign_cpu;
82
83 static void     intr_assign_next_cpu(struct intsrc *isrc);
84 #endif
85
86 static int      intr_assign_cpu(void *arg, u_char cpu);
87 static void     intr_disable_src(void *arg);
88 static void     intr_init(void *__dummy);
89 static int      intr_pic_registered(struct pic *pic);
90 static void     intrcnt_setname(const char *name, int index);
91 static void     intrcnt_updatename(struct intsrc *is);
92 static void     intrcnt_register(struct intsrc *is);
93
94 static int
95 intr_pic_registered(struct pic *pic)
96 {
97         struct pic *p;
98
99         STAILQ_FOREACH(p, &pics, pics) {
100                 if (p == pic)
101                         return (1);
102         }
103         return (0);
104 }
105
106 /*
107  * Register a new interrupt controller (PIC).  This is to support suspend
108  * and resume where we suspend/resume controllers rather than individual
109  * sources.  This also allows controllers with no active sources (such as
110  * 8259As in a system using the APICs) to participate in suspend and resume.
111  */
112 int
113 intr_register_pic(struct pic *pic)
114 {
115         int error;
116
117         sx_xlock(&intr_table_lock);
118         if (intr_pic_registered(pic))
119                 error = EBUSY;
120         else {
121                 STAILQ_INSERT_TAIL(&pics, pic, pics);
122                 error = 0;
123         }
124         sx_xunlock(&intr_table_lock);
125         return (error);
126 }
127
128 /*
129  * Register a new interrupt source with the global interrupt system.
130  * The global interrupts need to be disabled when this function is
131  * called.
132  */
133 int
134 intr_register_source(struct intsrc *isrc)
135 {
136         int error, vector;
137
138         KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
139         vector = isrc->is_pic->pic_vector(isrc);
140         if (interrupt_sources[vector] != NULL)
141                 return (EEXIST);
142         error = intr_event_create(&isrc->is_event, isrc, 0, vector,
143             intr_disable_src, (mask_fn)isrc->is_pic->pic_enable_source,
144             (mask_fn)isrc->is_pic->pic_eoi_source, intr_assign_cpu, "irq%d:",
145             vector);
146         if (error)
147                 return (error);
148         sx_xlock(&intr_table_lock);
149         if (interrupt_sources[vector] != NULL) {
150                 sx_xunlock(&intr_table_lock);
151                 intr_event_destroy(isrc->is_event);
152                 return (EEXIST);
153         }
154         intrcnt_register(isrc);
155         interrupt_sources[vector] = isrc;
156         isrc->is_handlers = 0;
157         sx_xunlock(&intr_table_lock);
158         return (0);
159 }
160
161 struct intsrc *
162 intr_lookup_source(int vector)
163 {
164
165         return (interrupt_sources[vector]);
166 }
167
168 int
169 intr_add_handler(const char *name, int vector, driver_filter_t filter, 
170     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)    
171 {
172         struct intsrc *isrc;
173         int error;
174
175         isrc = intr_lookup_source(vector);
176         if (isrc == NULL)
177                 return (EINVAL);
178         error = intr_event_add_handler(isrc->is_event, name, filter, handler,
179             arg, intr_priority(flags), flags, cookiep);
180         if (error == 0) {
181                 sx_xlock(&intr_table_lock);
182                 intrcnt_updatename(isrc);
183                 isrc->is_handlers++;
184                 if (isrc->is_handlers == 1) {
185 #ifdef SMP
186                         if (assign_cpu)
187                                 intr_assign_next_cpu(isrc);
188 #endif
189                         isrc->is_pic->pic_enable_intr(isrc);
190                         isrc->is_pic->pic_enable_source(isrc);
191                 }
192                 sx_xunlock(&intr_table_lock);
193         }
194         return (error);
195 }
196
197 int
198 intr_remove_handler(void *cookie)
199 {
200         struct intsrc *isrc;
201         int error;
202
203         isrc = intr_handler_source(cookie);
204         error = intr_event_remove_handler(cookie);
205         if (error == 0) {
206                 sx_xlock(&intr_table_lock);
207                 isrc->is_handlers--;
208                 if (isrc->is_handlers == 0) {
209                         isrc->is_pic->pic_disable_source(isrc, PIC_NO_EOI);
210                         isrc->is_pic->pic_disable_intr(isrc);
211                 }
212                 intrcnt_updatename(isrc);
213                 sx_xunlock(&intr_table_lock);
214         }
215         return (error);
216 }
217
218 int
219 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
220 {
221         struct intsrc *isrc;
222
223         isrc = intr_lookup_source(vector);
224         if (isrc == NULL)
225                 return (EINVAL);
226         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
227 }
228
229 static void
230 intr_disable_src(void *arg)
231 {
232         struct intsrc *isrc;
233
234         isrc = arg;
235         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
236 }
237
238 void
239 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
240 {
241         struct intr_event *ie;
242         struct thread *td;
243         int vector;
244
245         td = curthread;
246
247         /*
248          * We count software interrupts when we process them.  The
249          * code here follows previous practice, but there's an
250          * argument for counting hardware interrupts when they're
251          * processed too.
252          */
253         (*isrc->is_count)++;
254         PCPU_INC(cnt.v_intr);
255
256         ie = isrc->is_event;
257
258         /*
259          * XXX: We assume that IRQ 0 is only used for the ISA timer
260          * device (clk).
261          */
262         vector = isrc->is_pic->pic_vector(isrc);
263         if (vector == 0)
264                 clkintr_pending = 1;
265
266         /*
267          * For stray interrupts, mask and EOI the source, bump the
268          * stray count, and log the condition.
269          */
270         if (intr_event_handle(ie, frame) != 0) {
271                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
272                 (*isrc->is_straycount)++;
273                 if (*isrc->is_straycount < MAX_STRAY_LOG)
274                         log(LOG_ERR, "stray irq%d\n", vector);
275                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
276                         log(LOG_CRIT,
277                             "too many stray irq %d's: not logging anymore\n",
278                             vector);
279         }
280 }
281
282 void
283 intr_resume(void)
284 {
285         struct pic *pic;
286
287 #ifndef DEV_ATPIC
288         atpic_reset();
289 #endif
290         sx_xlock(&intr_table_lock);
291         STAILQ_FOREACH(pic, &pics, pics) {
292                 if (pic->pic_resume != NULL)
293                         pic->pic_resume(pic);
294         }
295         sx_xunlock(&intr_table_lock);
296 }
297
298 void
299 intr_suspend(void)
300 {
301         struct pic *pic;
302
303         sx_xlock(&intr_table_lock);
304         STAILQ_FOREACH(pic, &pics, pics) {
305                 if (pic->pic_suspend != NULL)
306                         pic->pic_suspend(pic);
307         }
308         sx_xunlock(&intr_table_lock);
309 }
310
311 static int
312 intr_assign_cpu(void *arg, u_char cpu)
313 {
314 #ifdef SMP
315         struct intsrc *isrc;    
316
317         /*
318          * Don't do anything during early boot.  We will pick up the
319          * assignment once the APs are started.
320          */
321         if (assign_cpu && cpu != NOCPU) {
322                 isrc = arg;
323                 sx_xlock(&intr_table_lock);
324                 isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[cpu]);
325                 sx_xunlock(&intr_table_lock);
326         }
327         return (0);
328 #else
329         return (EOPNOTSUPP);
330 #endif
331 }
332
333 static void
334 intrcnt_setname(const char *name, int index)
335 {
336
337         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
338             MAXCOMLEN, name);
339 }
340
341 static void
342 intrcnt_updatename(struct intsrc *is)
343 {
344
345         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
346 }
347
348 static void
349 intrcnt_register(struct intsrc *is)
350 {
351         char straystr[MAXCOMLEN + 1];
352
353         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
354         mtx_lock_spin(&intrcnt_lock);
355         is->is_index = intrcnt_index;
356         intrcnt_index += 2;
357         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
358             is->is_pic->pic_vector(is));
359         intrcnt_updatename(is);
360         is->is_count = &intrcnt[is->is_index];
361         intrcnt_setname(straystr, is->is_index + 1);
362         is->is_straycount = &intrcnt[is->is_index + 1];
363         mtx_unlock_spin(&intrcnt_lock);
364 }
365
366 void
367 intrcnt_add(const char *name, u_long **countp)
368 {
369
370         mtx_lock_spin(&intrcnt_lock);
371         *countp = &intrcnt[intrcnt_index];
372         intrcnt_setname(name, intrcnt_index);
373         intrcnt_index++;
374         mtx_unlock_spin(&intrcnt_lock);
375 }
376
377 static void
378 intr_init(void *dummy __unused)
379 {
380
381         intrcnt_setname("???", 0);
382         intrcnt_index = 1;
383         STAILQ_INIT(&pics);
384         sx_init(&intr_table_lock, "intr sources");
385         mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
386 }
387 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
388
389 #ifndef DEV_ATPIC
390 /* Initialize the two 8259A's to a known-good shutdown state. */
391 void
392 atpic_reset(void)
393 {
394
395         outb(IO_ICU1, ICW1_RESET | ICW1_IC4);
396         outb(IO_ICU1 + ICU_IMR_OFFSET, IDT_IO_INTS);
397         outb(IO_ICU1 + ICU_IMR_OFFSET, 1 << 2);
398         outb(IO_ICU1 + ICU_IMR_OFFSET, ICW4_8086);
399         outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff);
400         outb(IO_ICU1, OCW3_SEL | OCW3_RR);
401
402         outb(IO_ICU2, ICW1_RESET | ICW1_IC4);
403         outb(IO_ICU2 + ICU_IMR_OFFSET, IDT_IO_INTS + 8);
404         outb(IO_ICU2 + ICU_IMR_OFFSET, 2);
405         outb(IO_ICU2 + ICU_IMR_OFFSET, ICW4_8086);
406         outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff);
407         outb(IO_ICU2, OCW3_SEL | OCW3_RR);
408 }
409 #endif
410
411 #ifdef DDB
412 /*
413  * Dump data about interrupt handlers
414  */
415 DB_SHOW_COMMAND(irqs, db_show_irqs)
416 {
417         struct intsrc **isrc;
418         int i, verbose;
419
420         if (strcmp(modif, "v") == 0)
421                 verbose = 1;
422         else
423                 verbose = 0;
424         isrc = interrupt_sources;
425         for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
426                 if (*isrc != NULL)
427                         db_dump_intr_event((*isrc)->is_event, verbose);
428 }
429 #endif
430
431 #ifdef SMP
432 /*
433  * Support for balancing interrupt sources across CPUs.  For now we just
434  * allocate CPUs round-robin.
435  */
436
437 /* The BSP is always a valid target. */
438 static cpumask_t intr_cpus = (1 << 0);
439 static int current_cpu;
440
441 static void
442 intr_assign_next_cpu(struct intsrc *isrc)
443 {
444
445         /*
446          * Assign this source to a local APIC in a round-robin fashion.
447          */
448         isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[current_cpu]);
449         do {
450                 current_cpu++;
451                 if (current_cpu > mp_maxid)
452                         current_cpu = 0;
453         } while (!(intr_cpus & (1 << current_cpu)));
454 }
455
456 /* Attempt to bind the specified IRQ to the specified CPU. */
457 int
458 intr_bind(u_int vector, u_char cpu)
459 {
460         struct intsrc *isrc;
461
462         isrc = intr_lookup_source(vector);
463         if (isrc == NULL)
464                 return (EINVAL);
465         return (intr_event_bind(isrc->is_event, cpu));
466 }
467
468 /*
469  * Add a CPU to our mask of valid CPUs that can be destinations of
470  * interrupts.
471  */
472 void
473 intr_add_cpu(u_int cpu)
474 {
475
476         if (cpu >= MAXCPU)
477                 panic("%s: Invalid CPU ID", __func__);
478         if (bootverbose)
479                 printf("INTR: Adding local APIC %d as a target\n",
480                     cpu_apic_ids[cpu]);
481
482         intr_cpus |= (1 << cpu);
483 }
484
485 /*
486  * Distribute all the interrupt sources among the available CPUs once the
487  * AP's have been launched.
488  */
489 static void
490 intr_shuffle_irqs(void *arg __unused)
491 {
492         struct intsrc *isrc;
493         int i;
494
495         /* Don't bother on UP. */
496         if (mp_ncpus == 1)
497                 return;
498
499         /* Round-robin assign a CPU to each enabled source. */
500         sx_xlock(&intr_table_lock);
501         assign_cpu = 1;
502         for (i = 0; i < NUM_IO_INTS; i++) {
503                 isrc = interrupt_sources[i];
504                 if (isrc != NULL && isrc->is_handlers > 0) {
505                         /*
506                          * If this event is already bound to a CPU,
507                          * then assign the source to that CPU instead
508                          * of picking one via round-robin.
509                          */
510                         if (isrc->is_event->ie_cpu != NOCPU)
511                                 isrc->is_pic->pic_assign_cpu(isrc,
512                                     cpu_apic_ids[isrc->is_event->ie_cpu]);
513                         else
514                                 intr_assign_next_cpu(isrc);
515                 }
516         }
517         sx_xunlock(&intr_table_lock);
518 }
519 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,
520     NULL);
521 #endif