90b8d2f0f961068092d15a348c4477d4a3c7ecec
[seabios.git] / src / mptable.c
1 // MPTable generation (on emulators)
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "memmap.h" // bios_table_cur_addr
10 #include "config.h" // CONFIG_*
11
12 static void putb(u8 **pp, int val)
13 {
14     u8 *q;
15     q = *pp;
16     *q++ = val;
17     *pp = q;
18 }
19
20 static void putstr(u8 **pp, const char *str)
21 {
22     u8 *q;
23     q = *pp;
24     while (*str)
25         *q++ = *str++;
26     *pp = q;
27 }
28
29 static void putle16(u8 **pp, int val)
30 {
31     u8 *q;
32     q = *pp;
33     *q++ = val;
34     *q++ = val >> 8;
35     *pp = q;
36 }
37
38 static void putle32(u8 **pp, int val)
39 {
40     u8 *q;
41     q = *pp;
42     *q++ = val;
43     *q++ = val >> 8;
44     *q++ = val >> 16;
45     *q++ = val >> 24;
46     *pp = q;
47 }
48
49 void
50 mptable_init(void)
51 {
52     u8 *mp_config_table, *q, *float_pointer_struct;
53     int ioapic_id, i, len;
54     int mp_config_table_size;
55
56     int smp_cpus = smp_probe();
57     if (CONFIG_QEMU && smp_cpus <= 1)
58         return;
59
60     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
61     mp_config_table = (u8 *)bios_table_cur_addr;
62     q = mp_config_table;
63     putstr(&q, "PCMP"); /* "PCMP signature */
64     putle16(&q, 0); /* table length (patched later) */
65     putb(&q, 4); /* spec rev */
66     putb(&q, 0); /* checksum (patched later) */
67     if (CONFIG_QEMU)
68         putstr(&q, "QEMUCPU "); /* OEM id */
69     else
70         putstr(&q, "BOCHSCPU");
71     putstr(&q, "0.1         "); /* vendor id */
72     putle32(&q, 0); /* OEM table ptr */
73     putle16(&q, 0); /* OEM table size */
74     putle16(&q, smp_cpus + 18); /* entry count */
75     putle32(&q, 0xfee00000); /* local APIC addr */
76     putle16(&q, 0); /* ext table length */
77     putb(&q, 0); /* ext table checksum */
78     putb(&q, 0); /* reserved */
79
80     for(i = 0; i < smp_cpus; i++) {
81         putb(&q, 0); /* entry type = processor */
82         putb(&q, i); /* APIC id */
83         putb(&q, 0x11); /* local APIC version number */
84         if (i == 0)
85             putb(&q, 3); /* cpu flags: enabled, bootstrap cpu */
86         else
87             putb(&q, 1); /* cpu flags: enabled */
88         putb(&q, 0); /* cpu signature */
89         putb(&q, 6);
90         putb(&q, 0);
91         putb(&q, 0);
92         putle16(&q, 0x201); /* feature flags */
93         putle16(&q, 0);
94
95         putle16(&q, 0); /* reserved */
96         putle16(&q, 0);
97         putle16(&q, 0);
98         putle16(&q, 0);
99     }
100
101     /* isa bus */
102     putb(&q, 1); /* entry type = bus */
103     putb(&q, 0); /* bus ID */
104     putstr(&q, "ISA   ");
105
106     /* ioapic */
107     ioapic_id = smp_cpus;
108     putb(&q, 2); /* entry type = I/O APIC */
109     putb(&q, ioapic_id); /* apic ID */
110     putb(&q, 0x11); /* I/O APIC version number */
111     putb(&q, 1); /* enable */
112     putle32(&q, 0xfec00000); /* I/O APIC addr */
113
114     /* irqs */
115     for(i = 0; i < 16; i++) {
116         putb(&q, 3); /* entry type = I/O interrupt */
117         putb(&q, 0); /* interrupt type = vectored interrupt */
118         putb(&q, 0); /* flags: po=0, el=0 */
119         putb(&q, 0);
120         putb(&q, 0); /* source bus ID = ISA */
121         putb(&q, i); /* source bus IRQ */
122         putb(&q, ioapic_id); /* dest I/O APIC ID */
123         putb(&q, i); /* dest I/O APIC interrupt in */
124     }
125     /* patch length */
126     len = q - mp_config_table;
127     mp_config_table[4] = len;
128     mp_config_table[5] = len >> 8;
129
130     mp_config_table[7] = -checksum(mp_config_table, q - mp_config_table);
131
132     mp_config_table_size = q - mp_config_table;
133
134     bios_table_cur_addr += mp_config_table_size;
135
136     /* floating pointer structure */
137     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
138     float_pointer_struct = (u8 *)bios_table_cur_addr;
139     q = float_pointer_struct;
140     putstr(&q, "_MP_");
141     /* pointer to MP config table */
142     putle32(&q, (unsigned long)mp_config_table);
143
144     putb(&q, 1); /* length in 16 byte units */
145     putb(&q, 4); /* MP spec revision */
146     putb(&q, 0); /* checksum (patched later) */
147     putb(&q, 0); /* MP feature byte 1 */
148
149     putb(&q, 0);
150     putb(&q, 0);
151     putb(&q, 0);
152     putb(&q, 0);
153     float_pointer_struct[10] = -checksum(float_pointer_struct
154                                          , q - float_pointer_struct);
155     bios_table_cur_addr += (q - float_pointer_struct);
156     dprintf(1, "MP table addr=0x%08lx MPC table addr=0x%08lx size=0x%x\n",
157             (unsigned long)float_pointer_struct,
158             (unsigned long)mp_config_table,
159             mp_config_table_size);
160 }