blob: 47aee24bb227b0ca542465dcae5758416a7f4563 [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'Connore97ca7b2009-06-21 09:10:28 -040021 int smp_cpus = CountCPUs;
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.
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040060 u32 cpuid_signature, ebx, ecx, cpuid_features;
61 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
Kevin O'Connor35746442009-02-27 20:54:51 -050062 struct mpt_cpu *cpus = (void*)&config[1];
63 int i;
64 for (i = 0; i < smp_cpus; i++) {
65 struct mpt_cpu *cpu = &cpus[i];
66 memset(cpu, 0, sizeof(*cpu));
67 cpu->type = MPT_TYPE_CPU;
68 cpu->apicid = i;
69 cpu->apicver = 0x11;
70 /* cpu flags: enabled, bootstrap cpu */
71 cpu->cpuflag = (i == 0 ? 3 : 1);
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040072 if (cpuid_signature) {
73 cpu->cpusignature = cpuid_signature;
74 cpu->featureflag = cpuid_features;
75 } else {
76 cpu->cpusignature = 0x600;
77 cpu->featureflag = 0x201;
78 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040079 }
80
81 /* isa bus */
Kevin O'Connor35746442009-02-27 20:54:51 -050082 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
83 memset(bus, 0, sizeof(*bus));
84 bus->type = MPT_TYPE_BUS;
85 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040086
87 /* ioapic */
Kevin O'Connor35746442009-02-27 20:54:51 -050088 u8 ioapic_id = smp_cpus;
89 struct mpt_ioapic *ioapic = (void*)&bus[1];
90 memset(ioapic, 0, sizeof(*ioapic));
91 ioapic->type = MPT_TYPE_IOAPIC;
92 ioapic->apicid = ioapic_id;
93 ioapic->apicver = 0x11;
94 ioapic->flags = 1; // enable
95 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040096
97 /* irqs */
Kevin O'Connor35746442009-02-27 20:54:51 -050098 struct mpt_intsrc *intsrcs = (void *)&ioapic[1];
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040099 for(i = 0; i < 16; i++) {
Kevin O'Connor35746442009-02-27 20:54:51 -0500100 struct mpt_intsrc *isrc = &intsrcs[i];
101 memset(isrc, 0, sizeof(*isrc));
102 isrc->type = MPT_TYPE_INTSRC;
103 isrc->srcbusirq = i;
104 isrc->dstapic = ioapic_id;
105 isrc->dstirq = i;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400106 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400107
Kevin O'Connor35746442009-02-27 20:54:51 -0500108 // Set checksum.
109 config->checksum = -checksum(config, config->length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400110
Kevin O'Connor35746442009-02-27 20:54:51 -0500111 dprintf(1, "MP table addr=0x%x MPC table addr=0x%x size=0x%x\n",
112 (u32)floating, (u32)config, length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400113}