cb3b4e3d0138c90f85028fb7ff2a5e05057fddcb
[akaros.git] / kern / arch / x86 / mpacpi.c
1 #define DEBUG
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15 #include <acpi.h>
16 #include <arch/ioapic.h>
17 #include <arch/coreid.h>
18
19 extern  struct Madt     *apics;
20
21 int
22 mpacpi(int ncleft)
23 {
24         char *already;
25         int np, bp;
26         struct apic *apic;
27         struct Apicst *st;
28
29         printk("mpacpi ncleft %d\n", ncleft);
30         if (apics == NULL)
31                 return ncleft;
32
33         printd("APIC lapic paddr %#.8llux, flags %#.8ux\n",
34                 apics->lapicpa, apics->pcat);
35         np = 0;
36         printd("apics->st %p\n", apics->st);
37         for(st = apics->st; st != NULL; st = st->next){
38                 already = "";
39                 switch(st->type){
40                 case ASlapic:
41                         printd("ASlapic %d\n", st->lapic.id);
42                         /* this table is supposed to have all of them if it exists */
43                         if(st->lapic.id > MaxAPICNO)
44                                 break;
45                         apic = xlapic + st->lapic.id;
46                         bp = (np++ == 0);
47                         if(apic->useable){
48                                 already = "(mp)";
49                         }
50                         else if(ncleft != 0){
51                                 ncleft--;
52                                 printk("apicinit(%d, %p, %d);\n", st->lapic.id, apics->lapicpa, bp);
53                                 apicinit(st->lapic.id, apics->lapicpa, bp);
54                         } else
55                                 already = "(off)";
56
57                         printd("apic proc %d/%d apicid %d %s\n", np-1, apic->machno, st->lapic.id, already);
58 monitor(NULL);
59                         break;
60                 case ASioapic:
61                         printd("ASioapic %d\n", st->ioapic.id);
62                         if(st->ioapic.id > Napic)
63                                 break;
64                         apic = xioapic + st->ioapic.id;
65                         if(apic->useable){
66                                 apic->ibase = st->ioapic.ibase; /* gnarly */
67                                 already = "(mp)";
68                                 goto pr1;
69                         }
70                         printk("ioapicinit(%d, %p, %p);\n", st->lapic.id, apics->lapicpa, st->ioapic.addr);
71                         ioapicinit(st->ioapic.id, st->ioapic.ibase, st->ioapic.addr);
72 monitor(NULL);
73                 pr1:
74                         printd("ioapic %d ", st->ioapic.id);
75                         printd("addr %p base %d %s\n", apic->paddr, apic->ibase, already);
76                         break;
77                 }
78         }
79         return ncleft;
80 }