Bring a bit more apic infrastructure up.
[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         if (apics == NULL)
30                 return ncleft;
31
32         printd("APIC lapic paddr %#.8llux, flags %#.8ux\n",
33                 apics->lapicpa, apics->pcat);
34         np = 0;
35         printd("apics->st %p\n", apics->st);
36         for(st = apics->st; st != NULL; st = st->next){
37                 already = "";
38                 switch(st->type){
39                 case ASlapic:
40                         printd("ASlapic %d\n", st->lapic.id);
41                         /* this table is supposed to have all of them if it exists */
42                         if(st->lapic.id > MaxAPICNO)
43                                 break;
44                         apic = xlapic + st->lapic.id;
45                         bp = (np++ == 0);
46                         if(apic->useable){
47                                 already = "(mp)";
48                         }
49                         else if(ncleft != 0){
50                                 ncleft--;
51                                 printk("apicinit(%d, %p, %d);\n", st->lapic.id, apics->lapicpa, bp);
52                                 apicinit(st->lapic.id, apics->lapicpa, bp);
53                         } else
54                                 already = "(off)";
55
56                         printd("apic proc %d/%d apicid %d %s\n", np-1, apic->machno, st->lapic.id, already);
57                         break;
58                 case ASioapic:
59                         printd("ASioapic %d\n", st->ioapic.id);
60                         if(st->ioapic.id > Napic)
61                                 break;
62                         apic = xioapic + st->ioapic.id;
63                         if(apic->useable){
64                                 apic->ibase = st->ioapic.ibase; /* gnarly */
65                                 already = "(mp)";
66                                 goto pr1;
67                         }
68                         printk("ioapicinit(%d, %p, %p);\n", st->lapic.id, apics->lapicpa, st->ioapic.addr);
69                         ioapicinit(st->ioapic.id, st->ioapic.ibase, st->ioapic.addr);
70                 pr1:
71                         printd("ioapic %d ", st->ioapic.id);
72                         printd("addr %p base %d %s\n", apic->paddr, apic->ibase, already);
73                         break;
74                 }
75         }
76         return ncleft;
77 }