aboutsummaryrefslogtreecommitdiff
path: root/src/mptable.c
blob: 47aee24bb227b0ca542465dcae5758416a7f4563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// MPTable generation (on emulators)
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2006 Fabrice Bellard
//
// This file may be distributed under the terms of the GNU LGPLv3 license.

#include "util.h" // dprintf
#include "memmap.h" // bios_table_cur_addr
#include "config.h" // CONFIG_*
#include "mptable.h" // MPTABLE_SIGNATURE

void
mptable_init(void)
{
    if (! CONFIG_MPTABLE)
        return;

    dprintf(3, "init MPTable\n");

    int smp_cpus = CountCPUs;
    if (smp_cpus <= 1)
        // Building an mptable on uniprocessor machines confuses some OSes.
        return;

    u32 start = ALIGN(bios_table_cur_addr, 16);
    int length = (sizeof(struct mptable_floating_s)
                  + sizeof(struct mptable_config_s)
                  + sizeof(struct mpt_cpu) * smp_cpus
                  + sizeof(struct mpt_bus)
                  + sizeof(struct mpt_ioapic)
                  + sizeof(struct mpt_intsrc) * 16);
    if (start + length > bios_table_end_addr) {
        dprintf(1, "No room for MPTABLE!\n");
        return;
    }
    bios_table_cur_addr = start + length;

    /* floating pointer structure */
    struct mptable_floating_s *floating = (void*)start;
    memset(floating, 0, sizeof(*floating));
    struct mptable_config_s *config = (void*)&floating[1];
    floating->signature = MPTABLE_SIGNATURE;
    floating->physaddr = (u32)config;
    floating->length = 1;
    floating->spec_rev = 4;
    floating->checksum = -checksum(floating, sizeof(*floating));

    // Config structure.
    memset(config, 0, sizeof(*config));
    config->signature = MPCONFIG_SIGNATURE;
    config->length = length - sizeof(*floating);
    config->spec = 4;
    memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
    memcpy(config->productid, "0.1         ", sizeof(config->productid));
    config->entrycount = smp_cpus + 2 + 16;
    config->lapic = BUILD_APIC_ADDR;

    // CPU definitions.
    u32 cpuid_signature, ebx, ecx, cpuid_features;
    cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
    struct mpt_cpu *cpus = (void*)&config[1];
    int i;
    for (i = 0; i < smp_cpus; i++) {
        struct mpt_cpu *cpu = &cpus[i];
        memset(cpu, 0, sizeof(*cpu));
        cpu->type = MPT_TYPE_CPU;
        cpu->apicid = i;
        cpu->apicver = 0x11;
        /* cpu flags: enabled, bootstrap cpu */
        cpu->cpuflag = (i == 0 ? 3 : 1);
        if (cpuid_signature) {
            cpu->cpusignature = cpuid_signature;
            cpu->featureflag = cpuid_features;
        } else {
            cpu->cpusignature = 0x600;
            cpu->featureflag = 0x201;
        }
    }

    /* isa bus */
    struct mpt_bus *bus = (void*)&cpus[smp_cpus];
    memset(bus, 0, sizeof(*bus));
    bus->type = MPT_TYPE_BUS;
    memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));

    /* ioapic */
    u8 ioapic_id = smp_cpus;
    struct mpt_ioapic *ioapic = (void*)&bus[1];
    memset(ioapic, 0, sizeof(*ioapic));
    ioapic->type = MPT_TYPE_IOAPIC;
    ioapic->apicid = ioapic_id;
    ioapic->apicver = 0x11;
    ioapic->flags = 1; // enable
    ioapic->apicaddr = BUILD_IOAPIC_ADDR;

    /* irqs */
    struct mpt_intsrc *intsrcs = (void *)&ioapic[1];
    for(i = 0; i < 16; i++) {
        struct mpt_intsrc *isrc = &intsrcs[i];
        memset(isrc, 0, sizeof(*isrc));
        isrc->type = MPT_TYPE_INTSRC;
        isrc->srcbusirq = i;
        isrc->dstapic = ioapic_id;
        isrc->dstirq = i;
    }

    // Set checksum.
    config->checksum = -checksum(config, config->length);

    dprintf(1, "MP table addr=0x%x MPC table addr=0x%x size=0x%x\n",
            (u32)floating, (u32)config, length);
}