blob: 9e030fe8392435cbd55155048d31ff3ce68db245 [file] [log] [blame]
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04001// MPTable generation (on emulators)
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04007
8#include "util.h" // dprintf
9#include "memmap.h" // bios_table_cur_addr
Kevin O'Connor9521e262008-07-04 13:04:29 -040010#include "config.h" // CONFIG_*
Kevin O'Connor35746442009-02-27 20:54:51 -050011#include "mptable.h" // MPTABLE_SIGNATURE
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040012
13void
14mptable_init(void)
15{
Kevin O'Connord3817022008-07-12 12:46:07 -040016 if (! CONFIG_MPTABLE)
17 return;
18
Kevin O'Connor7b49cd92008-11-08 10:35:26 -050019 dprintf(3, "init MPTable\n");
20
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040021 int smp_cpus = smp_probe();
Kevin O'Connor6cb8ba92008-08-17 11:03:24 -040022 if (smp_cpus <= 1)
23 // Building an mptable on uniprocessor machines confuses some OSes.
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040024 return;
25
Kevin O'Connor35746442009-02-27 20:54:51 -050026 u32 start = ALIGN(bios_table_cur_addr, 16);
27 int length = (sizeof(struct mptable_floating_s)
28 + sizeof(struct mptable_config_s)
29 + sizeof(struct mpt_cpu) * smp_cpus
30 + sizeof(struct mpt_bus)
31 + sizeof(struct mpt_ioapic)
32 + sizeof(struct mpt_intsrc) * 16);
33 if (start + length > bios_table_end_addr) {
34 dprintf(1, "No room for MPTABLE!\n");
35 return;
36 }
Kevin O'Connora69a5592009-02-28 11:46:40 -050037 bios_table_cur_addr = start + length;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040038
Kevin O'Connor35746442009-02-27 20:54:51 -050039 /* floating pointer structure */
40 struct mptable_floating_s *floating = (void*)start;
41 memset(floating, 0, sizeof(*floating));
42 struct mptable_config_s *config = (void*)&floating[1];
43 floating->signature = MPTABLE_SIGNATURE;
44 floating->physaddr = (u32)config;
45 floating->length = 1;
46 floating->spec_rev = 4;
47 floating->checksum = -checksum(floating, sizeof(*floating));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040048
Kevin O'Connor35746442009-02-27 20:54:51 -050049 // Config structure.
50 memset(config, 0, sizeof(*config));
51 config->signature = MPCONFIG_SIGNATURE;
52 config->length = length - sizeof(*floating);
53 config->spec = 4;
54 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
55 memcpy(config->productid, "0.1 ", sizeof(config->productid));
56 config->entrycount = smp_cpus + 2 + 16;
57 config->lapic = BUILD_APIC_ADDR;
58
59 // CPU definitions.
60 struct mpt_cpu *cpus = (void*)&config[1];
61 int i;
62 for (i = 0; i < smp_cpus; i++) {
63 struct mpt_cpu *cpu = &cpus[i];
64 memset(cpu, 0, sizeof(*cpu));
65 cpu->type = MPT_TYPE_CPU;
66 cpu->apicid = i;
67 cpu->apicver = 0x11;
68 /* cpu flags: enabled, bootstrap cpu */
69 cpu->cpuflag = (i == 0 ? 3 : 1);
70 cpu->cpufeature = 0x600;
71 cpu->featureflag = 0x201;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040072 }
73
74 /* isa bus */
Kevin O'Connor35746442009-02-27 20:54:51 -050075 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
76 memset(bus, 0, sizeof(*bus));
77 bus->type = MPT_TYPE_BUS;
78 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040079
80 /* ioapic */
Kevin O'Connor35746442009-02-27 20:54:51 -050081 u8 ioapic_id = smp_cpus;
82 struct mpt_ioapic *ioapic = (void*)&bus[1];
83 memset(ioapic, 0, sizeof(*ioapic));
84 ioapic->type = MPT_TYPE_IOAPIC;
85 ioapic->apicid = ioapic_id;
86 ioapic->apicver = 0x11;
87 ioapic->flags = 1; // enable
88 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040089
90 /* irqs */
Kevin O'Connor35746442009-02-27 20:54:51 -050091 struct mpt_intsrc *intsrcs = (void *)&ioapic[1];
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040092 for(i = 0; i < 16; i++) {
Kevin O'Connor35746442009-02-27 20:54:51 -050093 struct mpt_intsrc *isrc = &intsrcs[i];
94 memset(isrc, 0, sizeof(*isrc));
95 isrc->type = MPT_TYPE_INTSRC;
96 isrc->srcbusirq = i;
97 isrc->dstapic = ioapic_id;
98 isrc->dstirq = i;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040099 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400100
Kevin O'Connor35746442009-02-27 20:54:51 -0500101 // Set checksum.
102 config->checksum = -checksum(config, config->length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400103
Kevin O'Connor35746442009-02-27 20:54:51 -0500104 dprintf(1, "MP table addr=0x%x MPC table addr=0x%x size=0x%x\n",
105 (u32)floating, (u32)config, length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400106}