PCI device locking and synchronization
[akaros.git] / kern / arch / x86 / mpacpi.c
1 /* This file is part of the UCB release of Plan 9. It is subject to the license
2  * terms in the LICENSE file found in the top-level directory of this
3  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
4  * part of the UCB release of Plan 9, including this file, may be copied,
5  * modified, propagated, or distributed except according to the terms contained
6  * in the LICENSE file. */
7
8 #include <vfs.h>
9 #include <kfs.h>
10 #include <slab.h>
11 #include <kmalloc.h>
12 #include <kref.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <assert.h>
16 #include <error.h>
17 #include <cpio.h>
18 #include <pmap.h>
19 #include <smp.h>
20 #include <ip.h>
21 #include <acpi.h>
22 #include <arch/ioapic.h>
23 #include <arch/coreid.h>
24
25 extern struct Madt *apics;
26
27 int mpacpi(int ncleft)
28 {
29         char *already;
30         int np, bp;
31         struct apic *apic;
32         struct Apicst *st;
33
34         /* If we don't have an mpisabusno yet, it's because the MP tables failed to
35          * parse.  So we'll just take the last one available.  I think we're
36          * supposed to parse the ACPI shit with the AML to figure out the buses and
37          * find a clear one, but fuck that.  Note this busno is just for our own
38          * RDT/Rbus bookkeeping. */
39         if (mpisabusno == -1)
40                 mpisabusno = Nbus - 1;
41
42         if (apics == NULL)
43                 return ncleft;
44
45         printd("APIC lapic paddr %#.8llux, flags %#.8ux\n",
46                    apics->lapicpa, apics->pcat);
47         np = 0;
48         printd("apics->st %p\n", apics->st);
49         for (st = apics->st; st != NULL; st = st->next) {
50                 already = "";
51                 switch (st->type) {
52                         case ASlapic:
53                                 printd("ASlapic %d\n", st->lapic.id);
54                                 /* this table is supposed to have all of them if it exists */
55                                 if (st->lapic.id > MaxAPICNO)
56                                         break;
57                                 apic = xlapic + st->lapic.id;
58                                 bp = (np++ == 0);
59                                 if (apic->useable) {
60                                         already = "(mp)";
61                                 } else if (ncleft != 0) {
62                                         ncleft--;
63                                         apicinit(st->lapic.id, apics->lapicpa, bp);
64                                 } else
65                                         already = "(off)";
66
67                                 printd("apic proc %d/%d apicid %d %s\n", np - 1, apic->machno,
68                                            st->lapic.id, already);
69                                 break;
70                         case ASioapic:
71                                 printd("ASioapic %d\n", st->ioapic.id);
72                                 if (st->ioapic.id > Napic)
73                                         break;
74                                 apic = xioapic + st->ioapic.id;
75                                 if (apic->useable) {
76                                         apic->ibase = st->ioapic.ibase; /* gnarly */
77                                         already = "(mp)";
78                                         goto pr1;
79                                 }
80                                 ioapicinit(st->ioapic.id, st->ioapic.ibase, st->ioapic.addr);
81 pr1:
82                                 printd("ioapic %d ", st->ioapic.id);
83                                 printd("addr %p base %d %s\n", apic->paddr, apic->ibase,
84                                            already);
85                                 break;
86                 }
87         }
88         return ncleft;
89 }