MP Implementation 2/4: Implement a poor-man's IPI messaging subsystem,
[dragonfly.git] / sys / platform / pc32 / i386 / mpapic.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
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. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
26  * $DragonFly: src/sys/platform/pc32/i386/Attic/mpapic.c,v 1.5 2003/07/08 06:27:26 dillon Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <machine/smptests.h>   /** TEST_TEST1, GRAB_LOPRIO */
32 #include <machine/globaldata.h>
33 #include <machine/smp.h>
34 #include <machine/mpapic.h>
35 #include <machine/segments.h>
36 #include <sys/thread2.h>
37
38 #include <i386/isa/intr_machdep.h>      /* Xspuriousint() */
39
40 /* EISA Edge/Level trigger control registers */
41 #define ELCR0   0x4d0                   /* eisa irq 0-7 */
42 #define ELCR1   0x4d1                   /* eisa irq 8-15 */
43
44 /*
45  * pointers to pmapped apic hardware.
46  */
47
48 #if defined(APIC_IO)
49 volatile ioapic_t       **ioapic;
50 #endif  /* APIC_IO */
51
52 /*
53  * Enable APIC, configure interrupts.
54  */
55 void
56 apic_initialize(void)
57 {
58         u_int   temp;
59
60         /* setup LVT1 as ExtINT */
61         temp = lapic.lvt_lint0;
62         temp &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM);
63         if (mycpu->gd_cpuid == 0)
64                 temp |= 0x00000700;     /* process ExtInts */
65         else
66                 temp |= 0x00010700;     /* mask ExtInts */
67         lapic.lvt_lint0 = temp;
68
69         /* setup LVT2 as NMI, masked till later... */
70         temp = lapic.lvt_lint1;
71         temp &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM);
72         temp |= 0x00010400;             /* masked, edge trigger, active hi */
73         lapic.lvt_lint1 = temp;
74
75         /* set the Task Priority Register as needed */
76         temp = lapic.tpr;
77         temp &= ~APIC_TPR_PRIO;         /* clear priority field */
78
79         /*
80          * Leave the BSP and TPR 0 during boot so it gets all the interrupts,
81          * set APs at TPR 0xF0 at boot so they get no ints.
82          */
83 #if 0
84         if (mycpu->gd_cpuid != 0)
85                 temp |= TPR_IPI_ONLY;   /* disable INTs on this cpu */
86 #endif
87         lapic.tpr = temp;
88
89         /* enable the local APIC */
90         temp = lapic.svr;
91         temp |= APIC_SVR_SWEN;          /* software enable APIC */
92         temp &= ~APIC_SVR_FOCUS;        /* enable 'focus processor' */
93
94         /* set the 'spurious INT' vector */
95         if ((XSPURIOUSINT_OFFSET & APIC_SVR_VEC_FIX) != APIC_SVR_VEC_FIX)
96                 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
97         temp &= ~APIC_SVR_VEC_PROG;     /* clear (programmable) vector field */
98         temp |= (XSPURIOUSINT_OFFSET & APIC_SVR_VEC_PROG);
99
100 #if defined(TEST_TEST1)
101         if (cpuid == GUARD_CPU) {
102                 temp &= ~APIC_SVR_SWEN; /* software DISABLE APIC */
103         }
104 #endif  /** TEST_TEST1 */
105
106         lapic.svr = temp;
107
108         if (bootverbose)
109                 apic_dump("apic_initialize()");
110 }
111
112
113 /*
114  * dump contents of local APIC registers
115  */
116 void
117 apic_dump(char* str)
118 {
119         printf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
120         printf("     lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
121                 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
122 }
123
124
125 #if defined(APIC_IO)
126
127 /*
128  * IO APIC code,
129  */
130
131 #define IOAPIC_ISA_INTS         16
132 #define REDIRCNT_IOAPIC(A) \
133             ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
134
135 static int trigger __P((int apic, int pin, u_int32_t * flags));
136 static void polarity __P((int apic, int pin, u_int32_t * flags, int level));
137
138 #define DEFAULT_FLAGS           \
139         ((u_int32_t)            \
140          (IOART_INTMSET |       \
141           IOART_DESTPHY |       \
142           IOART_DELLOPRI))
143
144 #define DEFAULT_ISA_FLAGS       \
145         ((u_int32_t)            \
146          (IOART_INTMSET |       \
147           IOART_TRGREDG |       \
148           IOART_INTAHI |        \
149           IOART_DESTPHY |       \
150           IOART_DELLOPRI))
151
152 void
153 io_apic_set_id(int apic, int id)
154 {
155         u_int32_t ux;
156         
157         ux = io_apic_read(apic, IOAPIC_ID);     /* get current contents */
158         if (((ux & APIC_ID_MASK) >> 24) != id) {
159                 printf("Changing APIC ID for IO APIC #%d"
160                        " from %d to %d on chip\n",
161                        apic, ((ux & APIC_ID_MASK) >> 24), id);
162                 ux &= ~APIC_ID_MASK;    /* clear the ID field */
163                 ux |= (id << 24);
164                 io_apic_write(apic, IOAPIC_ID, ux);     /* write new value */
165                 ux = io_apic_read(apic, IOAPIC_ID);     /* re-read && test */
166                 if (((ux & APIC_ID_MASK) >> 24) != id)
167                         panic("can't control IO APIC #%d ID, reg: 0x%08x",
168                               apic, ux);
169         }
170 }
171
172
173 int
174 io_apic_get_id(int apic)
175 {
176   return (io_apic_read(apic, IOAPIC_ID) & APIC_ID_MASK) >> 24;
177 }
178   
179
180
181 /*
182  * Setup the IO APIC.
183  */
184
185 extern int      apic_pin_trigger;       /* 'opaque' */
186
187 void
188 io_apic_setup_intpin(int apic, int pin)
189 {
190         int bus, bustype, irq;
191         u_char          select;         /* the select register is 8 bits */
192         u_int32_t       flags;          /* the window register is 32 bits */
193         u_int32_t       target;         /* the window register is 32 bits */
194         u_int32_t       vector;         /* the window register is 32 bits */
195         int             level;
196
197         target = IOART_DEST;
198
199         select = pin * 2 + IOAPIC_REDTBL0;      /* register */
200         /* 
201          * Always disable interrupts, and by default map
202          * pin X to IRQX because the disable doesn't stick
203          * and the uninitialize vector will get translated 
204          * into a panic.
205          *
206          * This is correct for IRQs 1 and 3-15.  In the other cases, 
207          * any robust driver will handle the spurious interrupt, and 
208          * the effective NOP beats a panic.
209          *
210          * A dedicated "bogus interrupt" entry in the IDT would
211          * be a nicer hack, although some one should find out 
212          * why some systems are generating interrupts when they
213          * shouldn't and stop the carnage.
214          */
215         vector = NRSVIDT + pin;                 /* IDT vec */
216         imen_lock();
217         io_apic_write(apic, select,
218                       (io_apic_read(apic, select) & ~IOART_INTMASK 
219                        & ~0xff)|IOART_INTMSET|vector);
220         imen_unlock();
221         
222         /* we only deal with vectored INTs here */
223         if (apic_int_type(apic, pin) != 0)
224                 return;
225         
226         irq = apic_irq(apic, pin);
227         if (irq < 0)
228                 return;
229         
230         /* determine the bus type for this pin */
231         bus = apic_src_bus_id(apic, pin);
232         if (bus == -1)
233                 return;
234         bustype = apic_bus_type(bus);
235         
236         if ((bustype == ISA) &&
237             (pin < IOAPIC_ISA_INTS) && 
238             (irq == pin) &&
239             (apic_polarity(apic, pin) == 0x1) &&
240             (apic_trigger(apic, pin) == 0x3)) {
241                 /* 
242                  * A broken BIOS might describe some ISA 
243                  * interrupts as active-high level-triggered.
244                  * Use default ISA flags for those interrupts.
245                  */
246                 flags = DEFAULT_ISA_FLAGS;
247         } else {
248                 /* 
249                  * Program polarity and trigger mode according to 
250                  * interrupt entry.
251                  */
252                 flags = DEFAULT_FLAGS;
253                 level = trigger(apic, pin, &flags);
254                 if (level == 1)
255                         apic_pin_trigger |= (1 << irq);
256                 polarity(apic, pin, &flags, level);
257         }
258         
259         /* program the appropriate registers */
260         if (apic != 0 || pin != irq)
261                 printf("IOAPIC #%d intpin %d -> irq %d\n",
262                        apic, pin, irq);
263         vector = NRSVIDT + irq;                 /* IDT vec */
264         imen_lock();
265         io_apic_write(apic, select, flags | vector);
266         io_apic_write(apic, select + 1, target);
267         imen_unlock();
268 }
269
270 int
271 io_apic_setup(int apic)
272 {
273         int             maxpin;
274         int             pin;
275
276         if (apic == 0)
277                 apic_pin_trigger = 0;   /* default to edge-triggered */
278
279         maxpin = REDIRCNT_IOAPIC(apic);         /* pins in APIC */
280         printf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
281         
282         for (pin = 0; pin < maxpin; ++pin) {
283                 io_apic_setup_intpin(apic, pin);
284         }
285
286         /* return GOOD status */
287         return 0;
288 }
289 #undef DEFAULT_ISA_FLAGS
290 #undef DEFAULT_FLAGS
291
292
293 #define DEFAULT_EXTINT_FLAGS    \
294         ((u_int32_t)            \
295          (IOART_INTMSET |       \
296           IOART_TRGREDG |       \
297           IOART_INTAHI |        \
298           IOART_DESTPHY |       \
299           IOART_DELLOPRI))
300
301 /*
302  * Setup the source of External INTerrupts.
303  */
304 int
305 ext_int_setup(int apic, int intr)
306 {
307         u_char  select;         /* the select register is 8 bits */
308         u_int32_t flags;        /* the window register is 32 bits */
309         u_int32_t target;       /* the window register is 32 bits */
310         u_int32_t vector;       /* the window register is 32 bits */
311
312         if (apic_int_type(apic, intr) != 3)
313                 return -1;
314
315         target = IOART_DEST;
316         select = IOAPIC_REDTBL0 + (2 * intr);
317         vector = NRSVIDT + intr;
318         flags = DEFAULT_EXTINT_FLAGS;
319
320         io_apic_write(apic, select, flags | vector);
321         io_apic_write(apic, select + 1, target);
322
323         return 0;
324 }
325 #undef DEFAULT_EXTINT_FLAGS
326
327
328 /*
329  * Set the trigger level for an IO APIC pin.
330  */
331 static int
332 trigger(int apic, int pin, u_int32_t * flags)
333 {
334         int     id;
335         int     eirq;
336         int     level;
337         static int intcontrol = -1;
338
339         switch (apic_trigger(apic, pin)) {
340
341         case 0x00:
342                 break;
343
344         case 0x01:
345                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG */
346                 return 0;
347
348         case 0x03:
349                 *flags |= IOART_TRGRLVL;
350                 return 1;
351
352         case -1:
353         default:
354                 goto bad;
355         }
356
357         if ((id = apic_src_bus_id(apic, pin)) == -1)
358                 goto bad;
359
360         switch (apic_bus_type(id)) {
361         case ISA:
362                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG; */
363                 return 0;
364
365         case EISA:
366                 eirq = apic_src_bus_irq(apic, pin);
367
368                 if (eirq < 0 || eirq > 15) {
369                         printf("EISA IRQ %d?!?!\n", eirq);
370                         goto bad;
371                 }
372
373                 if (intcontrol == -1) {
374                         intcontrol = inb(ELCR1) << 8;
375                         intcontrol |= inb(ELCR0);
376                         printf("EISA INTCONTROL = %08x\n", intcontrol);
377                 }
378
379                 /* Use ELCR settings to determine level or edge mode */
380                 level = (intcontrol >> eirq) & 1;
381
382                 /*
383                  * Note that on older Neptune chipset based systems, any
384                  * pci interrupts often show up here and in the ELCR as well
385                  * as level sensitive interrupts attributed to the EISA bus.
386                  */
387
388                 if (level)
389                         *flags |= IOART_TRGRLVL;
390                 else
391                         *flags &= ~IOART_TRGRLVL;
392
393                 return level;
394
395         case PCI:
396                 *flags |= IOART_TRGRLVL;
397                 return 1;
398
399         case -1:
400         default:
401                 goto bad;
402         }
403
404 bad:
405         panic("bad APIC IO INT flags");
406 }
407
408
409 /*
410  * Set the polarity value for an IO APIC pin.
411  */
412 static void
413 polarity(int apic, int pin, u_int32_t * flags, int level)
414 {
415         int     id;
416
417         switch (apic_polarity(apic, pin)) {
418
419         case 0x00:
420                 break;
421
422         case 0x01:
423                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
424                 return;
425
426         case 0x03:
427                 *flags |= IOART_INTALO;
428                 return;
429
430         case -1:
431         default:
432                 goto bad;
433         }
434
435         if ((id = apic_src_bus_id(apic, pin)) == -1)
436                 goto bad;
437
438         switch (apic_bus_type(id)) {
439         case ISA:
440                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
441                 return;
442
443         case EISA:
444                 /* polarity converter always gives active high */
445                 *flags &= ~IOART_INTALO;
446                 return;
447
448         case PCI:
449                 *flags |= IOART_INTALO;
450                 return;
451
452         case -1:
453         default:
454                 goto bad;
455         }
456
457 bad:
458         panic("bad APIC IO INT flags");
459 }
460
461
462 /*
463  * Print contents of apic_imen.
464  */
465 extern  u_int apic_imen;                /* keep apic_imen 'opaque' */
466 void
467 imen_dump(void)
468 {
469         int x;
470
471         printf("SMP: enabled INTs: ");
472         for (x = 0; x < 24; ++x)
473                 if ((apic_imen & (1 << x)) == 0)
474                         printf("%d, ", x);
475         printf("apic_imen: 0x%08x\n", apic_imen);
476 }
477
478
479 /*
480  * Inter Processor Interrupt functions.
481  */
482
483
484 /*
485  * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
486  *
487  *  destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
488  *  vector is any valid SYSTEM INT vector
489  *  delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
490  *
491  * A backlog of requests can create a deadlock between cpus.  To avoid this
492  * we have to be able to accept IPIs at the same time we are trying to send
493  * them.  The critical section prevents us from attempting to send additional
494  * IPIs reentrantly, but also prevents IPIQ processing so we have to call
495  * lwkt_process_ipiq() manually.  It's rather messy and expensive for this
496  * to occur but fortunately it does not happen too often.
497  */
498 int
499 apic_ipi(int dest_type, int vector, int delivery_mode)
500 {
501         u_long  icr_lo;
502
503         crit_enter();
504         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
505             unsigned int eflags = read_eflags();
506             cpu_enable_intr();
507             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
508                 lwkt_process_ipiq();
509             }
510             write_eflags(eflags);
511         }
512
513         icr_lo = (lapic.icr_lo & APIC_RESV2_MASK) | dest_type | 
514                 delivery_mode | vector;
515         lapic.icr_lo = icr_lo;
516         crit_exit();
517         return 0;
518 }
519
520 static int
521 apic_ipi_singledest(int cpu, int vector, int delivery_mode)
522 {
523         u_long  icr_lo;
524         u_long  icr_hi;
525
526         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
527             unsigned int eflags = read_eflags();
528             cpu_enable_intr();
529             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
530                 lwkt_process_ipiq();
531             }
532             write_eflags(eflags);
533         }
534         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
535         icr_hi |= (CPU_TO_ID(cpu) << 24);
536         lapic.icr_hi = icr_hi;
537
538         /* build IRC_LOW */
539         icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
540             | APIC_DEST_DESTFLD | delivery_mode | vector;
541
542         /* write APIC ICR */
543         lapic.icr_lo = icr_lo;
544         return 0;
545 }
546
547 /*
548  * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
549  *
550  * target is a bitmask of destination cpus.  Vector is any
551  * valid system INT vector.  Delivery mode may be either
552  * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
553  */
554 int
555 selected_apic_ipi(u_int target, int vector, int delivery_mode)
556 {
557         int status = 0;
558
559         crit_enter();
560         while (target) {
561                 int n = bsfl(target);
562                 target &= ~(1 << n);
563                 if (apic_ipi_singledest(n, vector, delivery_mode) < 0)
564                         status |= 1 << n;
565         }
566         crit_exit();
567         return(status);
568 }
569
570 #endif  /* APIC_IO */
571
572 /*
573  * Timer code, in development...
574  *  - suggested by rgrimes@gndrsh.aac.dev.com
575  */
576
577 /** XXX FIXME: temp hack till we can determin bus clock */
578 #ifndef BUS_CLOCK
579 #define BUS_CLOCK       66000000
580 #define bus_clock()     66000000
581 #endif
582
583 #if defined(READY)
584 int acquire_apic_timer __P((void));
585 int release_apic_timer __P((void));
586
587 /*
588  * Acquire the APIC timer for exclusive use.
589  */
590 int
591 acquire_apic_timer(void)
592 {
593 #if 1
594         return 0;
595 #else
596         /** XXX FIXME: make this really do something */
597         panic("APIC timer in use when attempting to aquire");
598 #endif
599 }
600
601
602 /*
603  * Return the APIC timer.
604  */
605 int
606 release_apic_timer(void)
607 {
608 #if 1
609         return 0;
610 #else
611         /** XXX FIXME: make this really do something */
612         panic("APIC timer was already released");
613 #endif
614 }
615 #endif  /* READY */
616
617
618 /*
619  * Load a 'downcount time' in uSeconds.
620  */
621 void
622 set_apic_timer(int value)
623 {
624         u_long  lvtt;
625         long    ticks_per_microsec;
626
627         /*
628          * Calculate divisor and count from value:
629          * 
630          *  timeBase == CPU bus clock divisor == [1,2,4,8,16,32,64,128]
631          *  value == time in uS
632          */
633         lapic.dcr_timer = APIC_TDCR_1;
634         ticks_per_microsec = bus_clock() / 1000000;
635
636         /* configure timer as one-shot */
637         lvtt = lapic.lvt_timer;
638         lvtt &= ~(APIC_LVTT_VECTOR | APIC_LVTT_DS | APIC_LVTT_M | APIC_LVTT_TM);
639         lvtt |= APIC_LVTT_M;                    /* no INT, one-shot */
640         lapic.lvt_timer = lvtt;
641
642         /* */
643         lapic.icr_timer = value * ticks_per_microsec;
644 }
645
646
647 /*
648  * Read remaining time in timer.
649  */
650 int
651 read_apic_timer(void)
652 {
653 #if 0
654         /** XXX FIXME: we need to return the actual remaining time,
655          *         for now we just return the remaining count.
656          */
657 #else
658         return lapic.ccr_timer;
659 #endif
660 }
661
662
663 /*
664  * Spin-style delay, set delay time in uS, spin till it drains.
665  */
666 void
667 u_sleep(int count)
668 {
669         set_apic_timer(count);
670         while (read_apic_timer())
671                  /* spin */ ;
672 }