]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/intr_machdep.c
Add definition for Intel Speculative Store Bypass Disable MSR bits
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / intr_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 /*
32  * Machine dependent interrupt code for x86.  For x86, we have to
33  * deal with different PICs.  Thus, we use the passed in vector to lookup
34  * an interrupt source associated with that vector.  The interrupt source
35  * describes which PIC the source belongs to and includes methods to handle
36  * that source.
37  */
38
39 #include "opt_atpic.h"
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/interrupt.h>
45 #include <sys/ktr.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/queue.h>
51 #include <sys/sbuf.h>
52 #include <sys/smp.h>
53 #include <sys/sx.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/systm.h>
57 #include <sys/taskqueue.h>
58 #include <sys/vmmeter.h>
59 #include <machine/clock.h>
60 #include <machine/intr_machdep.h>
61 #include <machine/smp.h>
62 #ifdef DDB
63 #include <ddb/ddb.h>
64 #endif
65
66 #ifndef DEV_ATPIC
67 #include <machine/segments.h>
68 #include <machine/frame.h>
69 #include <dev/ic/i8259.h>
70 #include <x86/isa/icu.h>
71 #include <isa/isareg.h>
72 #endif
73
74 #include <vm/vm.h>
75
76 #define MAX_STRAY_LOG   5
77
78 typedef void (*mask_fn)(void *);
79
80 static int intrcnt_index;
81 static struct intsrc *interrupt_sources[NUM_IO_INTS];
82 #ifdef SMP
83 static struct intsrc *interrupt_sorted[NUM_IO_INTS];
84 CTASSERT(sizeof(interrupt_sources) == sizeof(interrupt_sorted));
85 static int intrbalance;
86 SYSCTL_INT(_hw, OID_AUTO, intrbalance, CTLFLAG_RW, &intrbalance, 0,
87     "Interrupt auto-balance interval (seconds).  Zero disables.");
88 static struct timeout_task intrbalance_task;
89 #endif
90 static struct sx intrsrc_lock;
91 static struct mtx intrpic_lock;
92 static struct mtx intrcnt_lock;
93 static TAILQ_HEAD(pics_head, pic) pics;
94
95 #if defined(SMP) && !defined(EARLY_AP_STARTUP)
96 static int assign_cpu;
97 #endif
98
99 u_long intrcnt[INTRCNT_COUNT];
100 char intrnames[INTRCNT_COUNT * (MAXCOMLEN + 1)];
101 size_t sintrcnt = sizeof(intrcnt);
102 size_t sintrnames = sizeof(intrnames);
103
104 static int      intr_assign_cpu(void *arg, int cpu);
105 static void     intr_disable_src(void *arg);
106 static void     intr_init(void *__dummy);
107 static int      intr_pic_registered(struct pic *pic);
108 static void     intrcnt_setname(const char *name, int index);
109 static void     intrcnt_updatename(struct intsrc *is);
110 static void     intrcnt_register(struct intsrc *is);
111
112 static int
113 intr_pic_registered(struct pic *pic)
114 {
115         struct pic *p;
116
117         TAILQ_FOREACH(p, &pics, pics) {
118                 if (p == pic)
119                         return (1);
120         }
121         return (0);
122 }
123
124 /*
125  * Register a new interrupt controller (PIC).  This is to support suspend
126  * and resume where we suspend/resume controllers rather than individual
127  * sources.  This also allows controllers with no active sources (such as
128  * 8259As in a system using the APICs) to participate in suspend and resume.
129  */
130 int
131 intr_register_pic(struct pic *pic)
132 {
133         int error;
134
135         mtx_lock(&intrpic_lock);
136         if (intr_pic_registered(pic))
137                 error = EBUSY;
138         else {
139                 TAILQ_INSERT_TAIL(&pics, pic, pics);
140                 error = 0;
141         }
142         mtx_unlock(&intrpic_lock);
143         return (error);
144 }
145
146 /*
147  * Register a new interrupt source with the global interrupt system.
148  * The global interrupts need to be disabled when this function is
149  * called.
150  */
151 int
152 intr_register_source(struct intsrc *isrc)
153 {
154         int error, vector;
155
156         KASSERT(intr_pic_registered(isrc->is_pic), ("unregistered PIC"));
157         vector = isrc->is_pic->pic_vector(isrc);
158         if (interrupt_sources[vector] != NULL)
159                 return (EEXIST);
160         error = intr_event_create(&isrc->is_event, isrc, 0, vector,
161             intr_disable_src, (mask_fn)isrc->is_pic->pic_enable_source,
162             (mask_fn)isrc->is_pic->pic_eoi_source, intr_assign_cpu, "irq%d:",
163             vector);
164         if (error)
165                 return (error);
166         sx_xlock(&intrsrc_lock);
167         if (interrupt_sources[vector] != NULL) {
168                 sx_xunlock(&intrsrc_lock);
169                 intr_event_destroy(isrc->is_event);
170                 return (EEXIST);
171         }
172         intrcnt_register(isrc);
173         interrupt_sources[vector] = isrc;
174         isrc->is_handlers = 0;
175         sx_xunlock(&intrsrc_lock);
176         return (0);
177 }
178
179 struct intsrc *
180 intr_lookup_source(int vector)
181 {
182
183         if (vector < 0 || vector >= nitems(interrupt_sources))
184                 return (NULL);
185         return (interrupt_sources[vector]);
186 }
187
188 int
189 intr_add_handler(const char *name, int vector, driver_filter_t filter,
190     driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep,
191     int domain)
192 {
193         struct intsrc *isrc;
194         int error;
195
196         isrc = intr_lookup_source(vector);
197         if (isrc == NULL)
198                 return (EINVAL);
199         error = intr_event_add_handler(isrc->is_event, name, filter, handler,
200             arg, intr_priority(flags), flags, cookiep);
201         if (error == 0) {
202                 sx_xlock(&intrsrc_lock);
203                 intrcnt_updatename(isrc);
204                 isrc->is_handlers++;
205                 if (isrc->is_handlers == 1) {
206                         isrc->is_domain = domain;
207                         isrc->is_pic->pic_enable_intr(isrc);
208                         isrc->is_pic->pic_enable_source(isrc);
209                 }
210                 sx_xunlock(&intrsrc_lock);
211         }
212         return (error);
213 }
214
215 int
216 intr_remove_handler(void *cookie)
217 {
218         struct intsrc *isrc;
219         int error;
220
221         isrc = intr_handler_source(cookie);
222         error = intr_event_remove_handler(cookie);
223         if (error == 0) {
224                 sx_xlock(&intrsrc_lock);
225                 isrc->is_handlers--;
226                 if (isrc->is_handlers == 0) {
227                         isrc->is_pic->pic_disable_source(isrc, PIC_NO_EOI);
228                         isrc->is_pic->pic_disable_intr(isrc);
229                 }
230                 intrcnt_updatename(isrc);
231                 sx_xunlock(&intrsrc_lock);
232         }
233         return (error);
234 }
235
236 int
237 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
238 {
239         struct intsrc *isrc;
240
241         isrc = intr_lookup_source(vector);
242         if (isrc == NULL)
243                 return (EINVAL);
244         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
245 }
246
247 static void
248 intr_disable_src(void *arg)
249 {
250         struct intsrc *isrc;
251
252         isrc = arg;
253         isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
254 }
255
256 void
257 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
258 {
259         struct intr_event *ie;
260         int vector;
261
262         /*
263          * We count software interrupts when we process them.  The
264          * code here follows previous practice, but there's an
265          * argument for counting hardware interrupts when they're
266          * processed too.
267          */
268         (*isrc->is_count)++;
269         VM_CNT_INC(v_intr);
270
271         ie = isrc->is_event;
272
273         /*
274          * XXX: We assume that IRQ 0 is only used for the ISA timer
275          * device (clk).
276          */
277         vector = isrc->is_pic->pic_vector(isrc);
278         if (vector == 0)
279                 clkintr_pending = 1;
280
281         /*
282          * For stray interrupts, mask and EOI the source, bump the
283          * stray count, and log the condition.
284          */
285         if (intr_event_handle(ie, frame) != 0) {
286                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
287                 (*isrc->is_straycount)++;
288                 if (*isrc->is_straycount < MAX_STRAY_LOG)
289                         log(LOG_ERR, "stray irq%d\n", vector);
290                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
291                         log(LOG_CRIT,
292                             "too many stray irq %d's: not logging anymore\n",
293                             vector);
294         }
295 }
296
297 void
298 intr_resume(bool suspend_cancelled)
299 {
300         struct pic *pic;
301
302 #ifndef DEV_ATPIC
303         atpic_reset();
304 #endif
305         mtx_lock(&intrpic_lock);
306         TAILQ_FOREACH(pic, &pics, pics) {
307                 if (pic->pic_resume != NULL)
308                         pic->pic_resume(pic, suspend_cancelled);
309         }
310         mtx_unlock(&intrpic_lock);
311 }
312
313 void
314 intr_suspend(void)
315 {
316         struct pic *pic;
317
318         mtx_lock(&intrpic_lock);
319         TAILQ_FOREACH_REVERSE(pic, &pics, pics_head, pics) {
320                 if (pic->pic_suspend != NULL)
321                         pic->pic_suspend(pic);
322         }
323         mtx_unlock(&intrpic_lock);
324 }
325
326 static int
327 intr_assign_cpu(void *arg, int cpu)
328 {
329 #ifdef SMP
330         struct intsrc *isrc;
331         int error;
332
333 #ifdef EARLY_AP_STARTUP
334         MPASS(mp_ncpus == 1 || smp_started);
335
336         /* Nothing to do if there is only a single CPU. */
337         if (mp_ncpus > 1 && cpu != NOCPU) {
338 #else
339         /*
340          * Don't do anything during early boot.  We will pick up the
341          * assignment once the APs are started.
342          */
343         if (assign_cpu && cpu != NOCPU) {
344 #endif
345                 isrc = arg;
346                 sx_xlock(&intrsrc_lock);
347                 error = isrc->is_pic->pic_assign_cpu(isrc, cpu_apic_ids[cpu]);
348                 if (error == 0)
349                         isrc->is_cpu = cpu;
350                 sx_xunlock(&intrsrc_lock);
351         } else
352                 error = 0;
353         return (error);
354 #else
355         return (EOPNOTSUPP);
356 #endif
357 }
358
359 static void
360 intrcnt_setname(const char *name, int index)
361 {
362
363         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
364             MAXCOMLEN, name);
365 }
366
367 static void
368 intrcnt_updatename(struct intsrc *is)
369 {
370
371         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
372 }
373
374 static void
375 intrcnt_register(struct intsrc *is)
376 {
377         char straystr[MAXCOMLEN + 1];
378
379         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
380         mtx_lock_spin(&intrcnt_lock);
381         is->is_index = intrcnt_index;
382         intrcnt_index += 2;
383         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
384             is->is_pic->pic_vector(is));
385         intrcnt_updatename(is);
386         is->is_count = &intrcnt[is->is_index];
387         intrcnt_setname(straystr, is->is_index + 1);
388         is->is_straycount = &intrcnt[is->is_index + 1];
389         mtx_unlock_spin(&intrcnt_lock);
390 }
391
392 void
393 intrcnt_add(const char *name, u_long **countp)
394 {
395
396         mtx_lock_spin(&intrcnt_lock);
397         *countp = &intrcnt[intrcnt_index];
398         intrcnt_setname(name, intrcnt_index);
399         intrcnt_index++;
400         mtx_unlock_spin(&intrcnt_lock);
401 }
402
403 static void
404 intr_init(void *dummy __unused)
405 {
406
407         intrcnt_setname("???", 0);
408         intrcnt_index = 1;
409         TAILQ_INIT(&pics);
410         mtx_init(&intrpic_lock, "intrpic", NULL, MTX_DEF);
411         sx_init(&intrsrc_lock, "intrsrc");
412         mtx_init(&intrcnt_lock, "intrcnt", NULL, MTX_SPIN);
413 }
414 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
415
416 static void
417 intr_init_final(void *dummy __unused)
418 {
419
420         /*
421          * Enable interrupts on the BSP after all of the interrupt
422          * controllers are initialized.  Device interrupts are still
423          * disabled in the interrupt controllers until interrupt
424          * handlers are registered.  Interrupts are enabled on each AP
425          * after their first context switch.
426          */
427         enable_intr();
428 }
429 SYSINIT(intr_init_final, SI_SUB_INTR, SI_ORDER_ANY, intr_init_final, NULL);
430
431 #ifndef DEV_ATPIC
432 /* Initialize the two 8259A's to a known-good shutdown state. */
433 void
434 atpic_reset(void)
435 {
436
437         outb(IO_ICU1, ICW1_RESET | ICW1_IC4);
438         outb(IO_ICU1 + ICU_IMR_OFFSET, IDT_IO_INTS);
439         outb(IO_ICU1 + ICU_IMR_OFFSET, IRQ_MASK(ICU_SLAVEID));
440         outb(IO_ICU1 + ICU_IMR_OFFSET, MASTER_MODE);
441         outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff);
442         outb(IO_ICU1, OCW3_SEL | OCW3_RR);
443
444         outb(IO_ICU2, ICW1_RESET | ICW1_IC4);
445         outb(IO_ICU2 + ICU_IMR_OFFSET, IDT_IO_INTS + 8);
446         outb(IO_ICU2 + ICU_IMR_OFFSET, ICU_SLAVEID);
447         outb(IO_ICU2 + ICU_IMR_OFFSET, SLAVE_MODE);
448         outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff);
449         outb(IO_ICU2, OCW3_SEL | OCW3_RR);
450 }
451 #endif
452
453 /* Add a description to an active interrupt handler. */
454 int
455 intr_describe(u_int vector, void *ih, const char *descr)
456 {
457         struct intsrc *isrc;
458         int error;
459
460         isrc = intr_lookup_source(vector);
461         if (isrc == NULL)
462                 return (EINVAL);
463         error = intr_event_describe_handler(isrc->is_event, ih, descr);
464         if (error)
465                 return (error);
466         intrcnt_updatename(isrc);
467         return (0);
468 }
469
470 void
471 intr_reprogram(void)
472 {
473         struct intsrc *is;
474         int v;
475
476         sx_xlock(&intrsrc_lock);
477         for (v = 0; v < NUM_IO_INTS; v++) {
478                 is = interrupt_sources[v];
479                 if (is == NULL)
480                         continue;
481                 if (is->is_pic->pic_reprogram_pin != NULL)
482                         is->is_pic->pic_reprogram_pin(is);
483         }
484         sx_xunlock(&intrsrc_lock);
485 }
486
487 #ifdef DDB
488 /*
489  * Dump data about interrupt handlers
490  */
491 DB_SHOW_COMMAND(irqs, db_show_irqs)
492 {
493         struct intsrc **isrc;
494         int i, verbose;
495
496         if (strcmp(modif, "v") == 0)
497                 verbose = 1;
498         else
499                 verbose = 0;
500         isrc = interrupt_sources;
501         for (i = 0; i < NUM_IO_INTS && !db_pager_quit; i++, isrc++)
502                 if (*isrc != NULL)
503                         db_dump_intr_event((*isrc)->is_event, verbose);
504 }
505 #endif
506
507 #ifdef SMP
508 /*
509  * Support for balancing interrupt sources across CPUs.  For now we just
510  * allocate CPUs round-robin.
511  */
512
513 cpuset_t intr_cpus = CPUSET_T_INITIALIZER(0x1);
514 static int current_cpu[MAXMEMDOM];
515
516 static void
517 intr_init_cpus(void)
518 {
519         int i;
520
521         for (i = 0; i < vm_ndomains; i++) {
522                 current_cpu[i] = 0;
523                 if (!CPU_ISSET(current_cpu[i], &intr_cpus) ||
524                     !CPU_ISSET(current_cpu[i], &cpuset_domain[i]))
525                         intr_next_cpu(i);
526         }
527 }
528
529 /*
530  * Return the CPU that the next interrupt source should use.  For now
531  * this just returns the next local APIC according to round-robin.
532  */
533 u_int
534 intr_next_cpu(int domain)
535 {
536         u_int apic_id;
537
538 #ifdef EARLY_AP_STARTUP
539         MPASS(mp_ncpus == 1 || smp_started);
540         if (mp_ncpus == 1)
541                 return (PCPU_GET(apic_id));
542 #else
543         /* Leave all interrupts on the BSP during boot. */
544         if (!assign_cpu)
545                 return (PCPU_GET(apic_id));
546 #endif
547
548         mtx_lock_spin(&icu_lock);
549         apic_id = cpu_apic_ids[current_cpu[domain]];
550         do {
551                 current_cpu[domain]++;
552                 if (current_cpu[domain] > mp_maxid)
553                         current_cpu[domain] = 0;
554         } while (!CPU_ISSET(current_cpu[domain], &intr_cpus) ||
555             !CPU_ISSET(current_cpu[domain], &cpuset_domain[domain]));
556         mtx_unlock_spin(&icu_lock);
557         return (apic_id);
558 }
559
560 /* Attempt to bind the specified IRQ to the specified CPU. */
561 int
562 intr_bind(u_int vector, u_char cpu)
563 {
564         struct intsrc *isrc;
565
566         isrc = intr_lookup_source(vector);
567         if (isrc == NULL)
568                 return (EINVAL);
569         return (intr_event_bind(isrc->is_event, cpu));
570 }
571
572 /*
573  * Add a CPU to our mask of valid CPUs that can be destinations of
574  * interrupts.
575  */
576 void
577 intr_add_cpu(u_int cpu)
578 {
579
580         if (cpu >= MAXCPU)
581                 panic("%s: Invalid CPU ID", __func__);
582         if (bootverbose)
583                 printf("INTR: Adding local APIC %d as a target\n",
584                     cpu_apic_ids[cpu]);
585
586         CPU_SET(cpu, &intr_cpus);
587 }
588
589 #ifdef EARLY_AP_STARTUP
590 static void
591 intr_smp_startup(void *arg __unused)
592 {
593
594         intr_init_cpus();
595         return;
596 }
597 SYSINIT(intr_smp_startup, SI_SUB_SMP, SI_ORDER_SECOND, intr_smp_startup,
598     NULL);
599
600 #else
601 /*
602  * Distribute all the interrupt sources among the available CPUs once the
603  * AP's have been launched.
604  */
605 static void
606 intr_shuffle_irqs(void *arg __unused)
607 {
608         struct intsrc *isrc;
609         u_int cpu;
610         int i;
611
612         intr_init_cpus();
613         /* Don't bother on UP. */
614         if (mp_ncpus == 1)
615                 return;
616
617         /* Round-robin assign a CPU to each enabled source. */
618         sx_xlock(&intrsrc_lock);
619         assign_cpu = 1;
620         for (i = 0; i < NUM_IO_INTS; i++) {
621                 isrc = interrupt_sources[i];
622                 if (isrc != NULL && isrc->is_handlers > 0) {
623                         /*
624                          * If this event is already bound to a CPU,
625                          * then assign the source to that CPU instead
626                          * of picking one via round-robin.  Note that
627                          * this is careful to only advance the
628                          * round-robin if the CPU assignment succeeds.
629                          */
630                         cpu = isrc->is_event->ie_cpu;
631                         if (cpu == NOCPU)
632                                 cpu = current_cpu[isrc->is_domain];
633                         if (isrc->is_pic->pic_assign_cpu(isrc,
634                             cpu_apic_ids[cpu]) == 0) {
635                                 isrc->is_cpu = cpu;
636                                 if (isrc->is_event->ie_cpu == NOCPU)
637                                         intr_next_cpu(isrc->is_domain);
638                         }
639                 }
640         }
641         sx_xunlock(&intrsrc_lock);
642 }
643 SYSINIT(intr_shuffle_irqs, SI_SUB_SMP, SI_ORDER_SECOND, intr_shuffle_irqs,
644     NULL);
645 #endif
646
647 /*
648  * TODO: Export this information in a non-MD fashion, integrate with vmstat -i.
649  */
650 static int
651 sysctl_hw_intrs(SYSCTL_HANDLER_ARGS)
652 {
653         struct sbuf sbuf;
654         struct intsrc *isrc;
655         int error;
656         int i;
657
658         error = sysctl_wire_old_buffer(req, 0);
659         if (error != 0)
660                 return (error);
661
662         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
663         sx_slock(&intrsrc_lock);
664         for (i = 0; i < NUM_IO_INTS; i++) {
665                 isrc = interrupt_sources[i];
666                 if (isrc == NULL)
667                         continue;
668                 sbuf_printf(&sbuf, "%s:%d @cpu%d(domain%d): %ld\n",
669                     isrc->is_event->ie_fullname,
670                     isrc->is_index,
671                     isrc->is_cpu,
672                     isrc->is_domain,
673                     *isrc->is_count);
674         }
675
676         sx_sunlock(&intrsrc_lock);
677         error = sbuf_finish(&sbuf);
678         sbuf_delete(&sbuf);
679         return (error);
680 }
681 SYSCTL_PROC(_hw, OID_AUTO, intrs, CTLTYPE_STRING | CTLFLAG_RW,
682     0, 0, sysctl_hw_intrs, "A", "interrupt:number @cpu: count");
683
684 /*
685  * Compare two, possibly NULL, entries in the interrupt source array
686  * by load.
687  */
688 static int
689 intrcmp(const void *one, const void *two)
690 {
691         const struct intsrc *i1, *i2;
692
693         i1 = *(const struct intsrc * const *)one;
694         i2 = *(const struct intsrc * const *)two;
695         if (i1 != NULL && i2 != NULL)
696                 return (*i1->is_count - *i2->is_count);
697         if (i1 != NULL)
698                 return (1);
699         if (i2 != NULL)
700                 return (-1);
701         return (0);
702 }
703
704 /*
705  * Balance IRQs across available CPUs according to load.
706  */
707 static void
708 intr_balance(void *dummy __unused, int pending __unused)
709 {
710         struct intsrc *isrc;
711         int interval;
712         u_int cpu;
713         int i;
714
715         interval = intrbalance;
716         if (interval == 0)
717                 goto out;
718
719         /*
720          * Sort interrupts according to count.
721          */
722         sx_xlock(&intrsrc_lock);
723         memcpy(interrupt_sorted, interrupt_sources, sizeof(interrupt_sorted));
724         qsort(interrupt_sorted, NUM_IO_INTS, sizeof(interrupt_sorted[0]),
725             intrcmp);
726
727         /*
728          * Restart the scan from the same location to avoid moving in the
729          * common case.
730          */
731         intr_init_cpus();
732
733         /*
734          * Assign round-robin from most loaded to least.
735          */
736         for (i = NUM_IO_INTS - 1; i >= 0; i--) {
737                 isrc = interrupt_sorted[i];
738                 if (isrc == NULL  || isrc->is_event->ie_cpu != NOCPU)
739                         continue;
740                 cpu = current_cpu[isrc->is_domain];
741                 intr_next_cpu(isrc->is_domain);
742                 if (isrc->is_cpu != cpu &&
743                     isrc->is_pic->pic_assign_cpu(isrc,
744                     cpu_apic_ids[cpu]) == 0)
745                         isrc->is_cpu = cpu;
746         }
747         sx_xunlock(&intrsrc_lock);
748 out:
749         taskqueue_enqueue_timeout(taskqueue_thread, &intrbalance_task,
750             interval ? hz * interval : hz * 60);
751
752 }
753
754 static void
755 intr_balance_init(void *dummy __unused)
756 {
757
758         TIMEOUT_TASK_INIT(taskqueue_thread, &intrbalance_task, 0, intr_balance,
759             NULL);
760         taskqueue_enqueue_timeout(taskqueue_thread, &intrbalance_task, hz);
761 }
762 SYSINIT(intr_balance_init, SI_SUB_SMP, SI_ORDER_ANY, intr_balance_init, NULL);
763
764 #else
765 /*
766  * Always route interrupts to the current processor in the UP case.
767  */
768 u_int
769 intr_next_cpu(int domain)
770 {
771
772         return (PCPU_GET(apic_id));
773 }
774 #endif