Merge branch 'vendor/LESS'
[dragonfly.git] / sys / platform / pc64 / apic / mpapic.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the developer may NOT be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
27  * $DragonFly: src/sys/platform/pc64/apic/mpapic.c,v 1.1 2008/08/29 17:07:12 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <machine/globaldata.h>
33 #include <machine/smp.h>
34 #include <machine_base/apic/mpapic.h>
35 #include <machine/segments.h>
36 #include <sys/thread2.h>
37
38 #include <machine_base/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 volatile ioapic_t       **ioapic;
49
50 void    lapic_timer_fixup(void);
51
52 /*
53  * Enable APIC, configure interrupts.
54  */
55 void
56 apic_initialize(void)
57 {
58         u_int   temp;
59
60         /*
61          * setup LVT1 as ExtINT on the BSP.  This is theoretically an
62          * aggregate interrupt input from the 8259.  The INTA cycle
63          * will be routed to the external controller (the 8259) which
64          * is expected to supply the vector.
65          *
66          * Must be setup edge triggered, active high.
67          *
68          * Disable LVT1 on the APs.  It doesn't matter what delivery
69          * mode we use because we leave it masked.
70          */
71         temp = lapic.lvt_lint0;
72         temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK | 
73                   APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
74         if (mycpu->gd_cpuid == 0)
75                 temp |= APIC_LVT_DM_EXTINT;
76         else
77                 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
78         lapic.lvt_lint0 = temp;
79
80         /*
81          * setup LVT2 as NMI, masked till later.  Edge trigger, active high.
82          */
83         temp = lapic.lvt_lint1;
84         temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK | 
85                   APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
86         temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
87         lapic.lvt_lint1 = temp;
88
89         /*
90          * Mask the apic error interrupt, apic performance counter
91          * interrupt, and the apic timer interrupt.
92          */
93         lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
94         lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
95         lapic.lvt_timer = lapic.lvt_timer | APIC_LVT_MASKED;
96
97         /*
98          * Set the Task Priority Register as needed.   At the moment allow
99          * interrupts on all cpus (the APs will remain CLId until they are
100          * ready to deal).  We could disable all but IPIs by setting
101          * temp |= TPR_IPI_ONLY for cpu != 0.
102          */
103         temp = lapic.tpr;
104         temp &= ~APIC_TPR_PRIO;         /* clear priority field */
105 #ifndef APIC_IO
106         /*
107          * If we are NOT running the IO APICs, the LAPIC will only be used
108          * for IPIs.  Set the TPR to prevent any unintentional interrupts.
109          */
110         temp |= TPR_IPI_ONLY;
111 #endif
112
113         lapic.tpr = temp;
114
115         /* 
116          * enable the local APIC 
117          */
118         temp = lapic.svr;
119         temp |= APIC_SVR_ENABLE;        /* enable the APIC */
120         temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
121
122         /*
123          * Set the spurious interrupt vector.  The low 4 bits of the vector
124          * must be 1111.
125          */
126         if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
127                 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
128         temp &= ~APIC_SVR_VECTOR;
129         temp |= XSPURIOUSINT_OFFSET;
130
131         lapic.svr = temp;
132
133         /*
134          * Pump out a few EOIs to clean out interrupts that got through
135          * before we were able to set the TPR.
136          */
137         lapic.eoi = 0;
138         lapic.eoi = 0;
139         lapic.eoi = 0;
140
141         if (bootverbose)
142                 apic_dump("apic_initialize()");
143 }
144
145 void
146 lapic_timer_fixup(void)
147 {
148         /* TODO */
149 }
150
151 /*
152  * dump contents of local APIC registers
153  */
154 void
155 apic_dump(char* str)
156 {
157         kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
158         kprintf("     lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
159                 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
160 }
161
162
163 #if defined(APIC_IO)
164
165 /*
166  * IO APIC code,
167  */
168
169 #define IOAPIC_ISA_INTS         16
170 #define REDIRCNT_IOAPIC(A) \
171             ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
172
173 static int trigger (int apic, int pin, u_int32_t * flags);
174 static void polarity (int apic, int pin, u_int32_t * flags, int level);
175
176 #define DEFAULT_FLAGS           \
177         ((u_int32_t)            \
178          (IOART_INTMSET |       \
179           IOART_DESTPHY |       \
180           IOART_DELLOPRI))
181
182 #define DEFAULT_ISA_FLAGS       \
183         ((u_int32_t)            \
184          (IOART_INTMSET |       \
185           IOART_TRGREDG |       \
186           IOART_INTAHI |        \
187           IOART_DESTPHY |       \
188           IOART_DELLOPRI))
189
190 void
191 io_apic_set_id(int apic, int id)
192 {
193         u_int32_t ux;
194         
195         ux = io_apic_read(apic, IOAPIC_ID);     /* get current contents */
196         if (((ux & APIC_ID_MASK) >> 24) != id) {
197                 kprintf("Changing APIC ID for IO APIC #%d"
198                        " from %d to %d on chip\n",
199                        apic, ((ux & APIC_ID_MASK) >> 24), id);
200                 ux &= ~APIC_ID_MASK;    /* clear the ID field */
201                 ux |= (id << 24);
202                 io_apic_write(apic, IOAPIC_ID, ux);     /* write new value */
203                 ux = io_apic_read(apic, IOAPIC_ID);     /* re-read && test */
204                 if (((ux & APIC_ID_MASK) >> 24) != id)
205                         panic("can't control IO APIC #%d ID, reg: 0x%08x",
206                               apic, ux);
207         }
208 }
209
210
211 int
212 io_apic_get_id(int apic)
213 {
214   return (io_apic_read(apic, IOAPIC_ID) & APIC_ID_MASK) >> 24;
215 }
216   
217
218
219 /*
220  * Setup the IO APIC.
221  */
222
223 extern int      apic_pin_trigger;       /* 'opaque' */
224
225 void
226 io_apic_setup_intpin(int apic, int pin)
227 {
228         int bus, bustype, irq;
229         u_char          select;         /* the select register is 8 bits */
230         u_int32_t       flags;          /* the window register is 32 bits */
231         u_int32_t       target;         /* the window register is 32 bits */
232         u_int32_t       vector;         /* the window register is 32 bits */
233         int             level;
234
235         select = pin * 2 + IOAPIC_REDTBL0;      /* register */
236
237         /*
238          * Always clear an IO APIC pin before [re]programming it.  This is
239          * particularly important if the pin is set up for a level interrupt
240          * as the IOART_REM_IRR bit might be set.   When we reprogram the
241          * vector any EOI from pending ints on this pin could be lost and
242          * IRR might never get reset.
243          *
244          * To fix this problem, clear the vector and make sure it is 
245          * programmed as an edge interrupt.  This should theoretically
246          * clear IRR so we can later, safely program it as a level 
247          * interrupt.
248          */
249         imen_lock();
250
251         flags = io_apic_read(apic, select) & IOART_RESV;
252         flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
253         flags |= IOART_DESTPHY | IOART_DELFIXED;
254
255         target = io_apic_read(apic, select + 1) & IOART_HI_DEST_RESV;
256         target |= 0;    /* fixed mode cpu mask of 0 - don't deliver anywhere */
257
258         vector = 0;
259
260         io_apic_write(apic, select, flags | vector);
261         io_apic_write(apic, select + 1, target);
262
263         imen_unlock();
264
265         /*
266          * We only deal with vectored interrupts here.  ? documentation is
267          * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
268          * vs ExTINT, etc.
269          *
270          * This test also catches unconfigured pins.
271          */
272         if (apic_int_type(apic, pin) != 0)
273                 return;
274
275         /*
276          * Leave the pin unprogrammed if it does not correspond to
277          * an IRQ.
278          */
279         irq = apic_irq(apic, pin);
280         if (irq < 0)
281                 return;
282         
283         /* determine the bus type for this pin */
284         bus = apic_src_bus_id(apic, pin);
285         if (bus < 0)
286                 return;
287         bustype = apic_bus_type(bus);
288         
289         if ((bustype == ISA) &&
290             (pin < IOAPIC_ISA_INTS) && 
291             (irq == pin) &&
292             (apic_polarity(apic, pin) == 0x1) &&
293             (apic_trigger(apic, pin) == 0x3)) {
294                 /* 
295                  * A broken BIOS might describe some ISA 
296                  * interrupts as active-high level-triggered.
297                  * Use default ISA flags for those interrupts.
298                  */
299                 flags = DEFAULT_ISA_FLAGS;
300         } else {
301                 /* 
302                  * Program polarity and trigger mode according to 
303                  * interrupt entry.
304                  */
305                 flags = DEFAULT_FLAGS;
306                 level = trigger(apic, pin, &flags);
307                 if (level == 1)
308                         apic_pin_trigger |= (1 << irq);
309                 polarity(apic, pin, &flags, level);
310         }
311         
312         if (bootverbose) {
313                 kprintf("IOAPIC #%d intpin %d -> irq %d\n",
314                        apic, pin, irq);
315         }
316
317         /*
318          * Program the appropriate registers.  This routing may be 
319          * overridden when an interrupt handler for a device is
320          * actually added (see register_int(), which calls through
321          * the MACHINTR ABI to set up an interrupt handler/vector).
322          *
323          * The order in which we must program the two registers for
324          * safety is unclear! XXX
325          */
326         imen_lock();
327
328         vector = IDT_OFFSET + irq;                      /* IDT vec */
329         target = io_apic_read(apic, select + 1) & IOART_HI_DEST_RESV;
330         target |= IOART_HI_DEST_BROADCAST;
331         flags |= io_apic_read(apic, select) & IOART_RESV;
332         io_apic_write(apic, select, flags | vector);
333         io_apic_write(apic, select + 1, target);
334
335         imen_unlock();
336 }
337
338 int
339 io_apic_setup(int apic)
340 {
341         int             maxpin;
342         int             pin;
343
344         if (apic == 0)
345                 apic_pin_trigger = 0;   /* default to edge-triggered */
346
347         maxpin = REDIRCNT_IOAPIC(apic);         /* pins in APIC */
348         kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
349         
350         for (pin = 0; pin < maxpin; ++pin) {
351                 io_apic_setup_intpin(apic, pin);
352         }
353         while (pin < 32) {
354                 if (apic_int_type(apic, pin) >= 0) {
355                         kprintf("Warning: IOAPIC #%d pin %d does not exist,"
356                                 " cannot program!\n", apic, pin);
357                 }
358                 ++pin;
359         }
360
361         /* return GOOD status */
362         return 0;
363 }
364 #undef DEFAULT_ISA_FLAGS
365 #undef DEFAULT_FLAGS
366
367
368 #define DEFAULT_EXTINT_FLAGS    \
369         ((u_int32_t)            \
370          (IOART_INTMSET |       \
371           IOART_TRGREDG |       \
372           IOART_INTAHI |        \
373           IOART_DESTPHY |       \
374           IOART_DELLOPRI))
375
376 /*
377  * Setup the source of External INTerrupts.
378  */
379 int
380 ext_int_setup(int apic, int intr)
381 {
382         u_char  select;         /* the select register is 8 bits */
383         u_int32_t flags;        /* the window register is 32 bits */
384         u_int32_t target;       /* the window register is 32 bits */
385         u_int32_t vector;       /* the window register is 32 bits */
386
387         if (apic_int_type(apic, intr) != 3)
388                 return -1;
389
390         target = IOART_HI_DEST_BROADCAST;
391         select = IOAPIC_REDTBL0 + (2 * intr);
392         vector = IDT_OFFSET + intr;
393         flags = DEFAULT_EXTINT_FLAGS;
394
395         io_apic_write(apic, select, flags | vector);
396         io_apic_write(apic, select + 1, target);
397
398         return 0;
399 }
400 #undef DEFAULT_EXTINT_FLAGS
401
402
403 /*
404  * Set the trigger level for an IO APIC pin.
405  */
406 static int
407 trigger(int apic, int pin, u_int32_t * flags)
408 {
409         int     id;
410         int     eirq;
411         int     level;
412         static int intcontrol = -1;
413
414         switch (apic_trigger(apic, pin)) {
415
416         case 0x00:
417                 break;
418
419         case 0x01:
420                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG */
421                 return 0;
422
423         case 0x03:
424                 *flags |= IOART_TRGRLVL;
425                 return 1;
426
427         case -1:
428         default:
429                 goto bad;
430         }
431
432         if ((id = apic_src_bus_id(apic, pin)) == -1)
433                 goto bad;
434
435         switch (apic_bus_type(id)) {
436         case ISA:
437                 *flags &= ~IOART_TRGRLVL;       /* *flags |= IOART_TRGREDG; */
438                 return 0;
439
440         case EISA:
441                 eirq = apic_src_bus_irq(apic, pin);
442
443                 if (eirq < 0 || eirq > 15) {
444                         kprintf("EISA IRQ %d?!?!\n", eirq);
445                         goto bad;
446                 }
447
448                 if (intcontrol == -1) {
449                         intcontrol = inb(ELCR1) << 8;
450                         intcontrol |= inb(ELCR0);
451                         kprintf("EISA INTCONTROL = %08x\n", intcontrol);
452                 }
453
454                 /* Use ELCR settings to determine level or edge mode */
455                 level = (intcontrol >> eirq) & 1;
456
457                 /*
458                  * Note that on older Neptune chipset based systems, any
459                  * pci interrupts often show up here and in the ELCR as well
460                  * as level sensitive interrupts attributed to the EISA bus.
461                  */
462
463                 if (level)
464                         *flags |= IOART_TRGRLVL;
465                 else
466                         *flags &= ~IOART_TRGRLVL;
467
468                 return level;
469
470         case PCI:
471                 *flags |= IOART_TRGRLVL;
472                 return 1;
473
474         case -1:
475         default:
476                 goto bad;
477         }
478
479 bad:
480         panic("bad APIC IO INT flags");
481 }
482
483
484 /*
485  * Set the polarity value for an IO APIC pin.
486  */
487 static void
488 polarity(int apic, int pin, u_int32_t * flags, int level)
489 {
490         int     id;
491
492         switch (apic_polarity(apic, pin)) {
493
494         case 0x00:
495                 break;
496
497         case 0x01:
498                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
499                 return;
500
501         case 0x03:
502                 *flags |= IOART_INTALO;
503                 return;
504
505         case -1:
506         default:
507                 goto bad;
508         }
509
510         if ((id = apic_src_bus_id(apic, pin)) == -1)
511                 goto bad;
512
513         switch (apic_bus_type(id)) {
514         case ISA:
515                 *flags &= ~IOART_INTALO;        /* *flags |= IOART_INTAHI */
516                 return;
517
518         case EISA:
519                 /* polarity converter always gives active high */
520                 *flags &= ~IOART_INTALO;
521                 return;
522
523         case PCI:
524                 *flags |= IOART_INTALO;
525                 return;
526
527         case -1:
528         default:
529                 goto bad;
530         }
531
532 bad:
533         panic("bad APIC IO INT flags");
534 }
535
536
537 /*
538  * Print contents of apic_imen.
539  */
540 extern  u_int apic_imen;                /* keep apic_imen 'opaque' */
541 void
542 imen_dump(void)
543 {
544         int x;
545
546         kprintf("SMP: enabled INTs: ");
547         for (x = 0; x < 24; ++x)
548                 if ((apic_imen & (1 << x)) == 0)
549                         kprintf("%d, ", x);
550         kprintf("apic_imen: 0x%08x\n", apic_imen);
551 }
552
553
554 /*
555  * Inter Processor Interrupt functions.
556  */
557
558 #endif  /* APIC_IO */
559
560 /*
561  * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
562  *
563  *  destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
564  *  vector is any valid SYSTEM INT vector
565  *  delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
566  *
567  * A backlog of requests can create a deadlock between cpus.  To avoid this
568  * we have to be able to accept IPIs at the same time we are trying to send
569  * them.  The critical section prevents us from attempting to send additional
570  * IPIs reentrantly, but also prevents IPIQ processing so we have to call
571  * lwkt_process_ipiq() manually.  It's rather messy and expensive for this
572  * to occur but fortunately it does not happen too often.
573  */
574 int
575 apic_ipi(int dest_type, int vector, int delivery_mode)
576 {
577         u_long  icr_lo;
578
579         crit_enter();
580         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
581             unsigned int eflags = read_eflags();
582             cpu_enable_intr();
583             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
584                 lwkt_process_ipiq();
585             }
586             write_eflags(eflags);
587         }
588
589         icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type | 
590                 delivery_mode | vector;
591         lapic.icr_lo = icr_lo;
592         crit_exit();
593         return 0;
594 }
595
596 void
597 single_apic_ipi(int cpu, int vector, int delivery_mode)
598 {
599         u_long  icr_lo;
600         u_long  icr_hi;
601
602         crit_enter();
603         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
604             unsigned int eflags = read_eflags();
605             cpu_enable_intr();
606             while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
607                 lwkt_process_ipiq();
608             }
609             write_eflags(eflags);
610         }
611         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
612         icr_hi |= (CPU_TO_ID(cpu) << 24);
613         lapic.icr_hi = icr_hi;
614
615         /* build ICR_LOW */
616         icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
617             | APIC_DEST_DESTFLD | delivery_mode | vector;
618
619         /* write APIC ICR */
620         lapic.icr_lo = icr_lo;
621         crit_exit();
622 }
623
624 #if 0   
625
626 /*
627  * Returns 0 if the apic is busy, 1 if we were able to queue the request.
628  *
629  * NOT WORKING YET!  The code as-is may end up not queueing an IPI at all
630  * to the target, and the scheduler does not 'poll' for IPI messages.
631  */
632 int
633 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
634 {
635         u_long  icr_lo;
636         u_long  icr_hi;
637
638         crit_enter();
639         if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
640             crit_exit();
641             return(0);
642         }
643         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
644         icr_hi |= (CPU_TO_ID(cpu) << 24);
645         lapic.icr_hi = icr_hi;
646
647         /* build IRC_LOW */
648         icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
649             | APIC_DEST_DESTFLD | delivery_mode | vector;
650
651         /* write APIC ICR */
652         lapic.icr_lo = icr_lo;
653         crit_exit();
654         return(1);
655 }
656
657 #endif
658
659 /*
660  * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
661  *
662  * target is a bitmask of destination cpus.  Vector is any
663  * valid system INT vector.  Delivery mode may be either
664  * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
665  */
666 void
667 selected_apic_ipi(u_int target, int vector, int delivery_mode)
668 {
669         crit_enter();
670         while (target) {
671                 int n = bsfl(target);
672                 target &= ~(1 << n);
673                 single_apic_ipi(n, vector, delivery_mode);
674         }
675         crit_exit();
676 }
677
678 /*
679  * Timer code, in development...
680  *  - suggested by rgrimes@gndrsh.aac.dev.com
681  */
682
683 /** XXX FIXME: temp hack till we can determin bus clock */
684 #ifndef BUS_CLOCK
685 #define BUS_CLOCK       66000000
686 #define bus_clock()     66000000
687 #endif
688
689 #if defined(READY)
690 int acquire_apic_timer (void);
691 int release_apic_timer (void);
692
693 /*
694  * Acquire the APIC timer for exclusive use.
695  */
696 int
697 acquire_apic_timer(void)
698 {
699 #if 1
700         return 0;
701 #else
702         /** XXX FIXME: make this really do something */
703         panic("APIC timer in use when attempting to acquire");
704 #endif
705 }
706
707
708 /*
709  * Return the APIC timer.
710  */
711 int
712 release_apic_timer(void)
713 {
714 #if 1
715         return 0;
716 #else
717         /** XXX FIXME: make this really do something */
718         panic("APIC timer was already released");
719 #endif
720 }
721 #endif  /* READY */
722
723
724 /*
725  * Load a 'downcount time' in uSeconds.
726  */
727 void
728 set_apic_timer(int value)
729 {
730         u_long  lvtt;
731         long    ticks_per_microsec;
732
733         /*
734          * Calculate divisor and count from value:
735          * 
736          *  timeBase == CPU bus clock divisor == [1,2,4,8,16,32,64,128]
737          *  value == time in uS
738          */
739         lapic.dcr_timer = APIC_TDCR_1;
740         ticks_per_microsec = bus_clock() / 1000000;
741
742         /* configure timer as one-shot */
743         lvtt = lapic.lvt_timer;
744         lvtt &= ~(APIC_LVTT_VECTOR | APIC_LVTT_DS);
745         lvtt &= ~(APIC_LVTT_PERIODIC);
746         lvtt |= APIC_LVTT_MASKED;               /* no INT, one-shot */
747         lapic.lvt_timer = lvtt;
748
749         /* */
750         lapic.icr_timer = value * ticks_per_microsec;
751 }
752
753
754 /*
755  * Read remaining time in timer.
756  */
757 int
758 read_apic_timer(void)
759 {
760 #if 0
761         /** XXX FIXME: we need to return the actual remaining time,
762          *         for now we just return the remaining count.
763          */
764 #else
765         return lapic.ccr_timer;
766 #endif
767 }
768
769
770 /*
771  * Spin-style delay, set delay time in uS, spin till it drains.
772  */
773 void
774 u_sleep(int count)
775 {
776         set_apic_timer(count);
777         while (read_apic_timer())
778                  /* spin */ ;
779 }