x86_64: init local apic on boot, or legacy pic/pit as fallback
This commit is contained in:
@@ -3,5 +3,8 @@
|
||||
|
||||
kern_status_t acpi_init(void)
|
||||
{
|
||||
apic_init();
|
||||
smp_init();
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
109
arch/x86_64/acpi/apic.c
Normal file
109
arch/x86_64/acpi/apic.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <socks/printk.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/cpu.h>
|
||||
#include <arch/acpi.h>
|
||||
#include <arch/msr.h>
|
||||
#include <arch/ports.h>
|
||||
#include <arch/pit.h>
|
||||
|
||||
#define PIC1_DATA 0x21
|
||||
#define PIC2_DATA 0xA1
|
||||
|
||||
#define IA32_APIC_BASE_MSR 0x1B
|
||||
#define IA32_APIC_BASE_MSR_BSP 0x100
|
||||
#define IA32_APIC_BASE_MSR_ENABLE 0x800
|
||||
|
||||
static uint32_t *lapic_base;
|
||||
|
||||
extern int check_apic(void);
|
||||
|
||||
static uintptr_t apic_get_base(void)
|
||||
{
|
||||
return rdmsr(IA32_APIC_BASE_MSR);
|
||||
}
|
||||
|
||||
static void apic_set_base(uintptr_t addr)
|
||||
{
|
||||
wrmsr(IA32_APIC_BASE_MSR, addr);
|
||||
}
|
||||
|
||||
static void disable_8259(void)
|
||||
{
|
||||
/* mask all interrupts on PICs 1 and 2 */
|
||||
outportb(PIC1_DATA, 0xFF);
|
||||
outportb(PIC2_DATA, 0xFF);
|
||||
}
|
||||
|
||||
static uint32_t local_apic_read(uint32_t reg)
|
||||
{
|
||||
return READ_ONCE(lapic_base[reg >> 4]);
|
||||
}
|
||||
|
||||
static void local_apic_write(uint32_t reg, uint32_t val)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void *find_lapic(struct acpi_madt *madt)
|
||||
{
|
||||
phys_addr_t local_apic = madt->m_lapic_ptr;
|
||||
|
||||
unsigned char *p = (unsigned char *)madt + sizeof *madt;
|
||||
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
|
||||
|
||||
while (p < madt_end) {
|
||||
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
|
||||
struct lapic_override_record *lapic;
|
||||
|
||||
switch (rec->r_type) {
|
||||
case ACPI_MADT_LAPIC_OVERRIDE:
|
||||
lapic = (struct lapic_override_record *)(rec + 1);
|
||||
return vm_phys_to_virt(lapic->l_lapic_ptr);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
return vm_phys_to_virt(local_apic);
|
||||
}
|
||||
|
||||
|
||||
kern_status_t local_apic_enable(void)
|
||||
{
|
||||
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
|
||||
if (!madt) {
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
apic_set_base(apic_get_base());
|
||||
lapic_base = find_lapic(madt);
|
||||
|
||||
local_apic_write(0xF0, local_apic_read(0xF0) | 0x100);
|
||||
|
||||
printk("acpi: enabled local APIC on core %u", this_cpu());
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static void configure_legacy_pic(void)
|
||||
{
|
||||
printk("acpi: APIC unavailable, using 8259 PIC");
|
||||
pit_init(1000);
|
||||
}
|
||||
|
||||
kern_status_t apic_init(void)
|
||||
{
|
||||
if (check_apic() == 0) {
|
||||
configure_legacy_pic();
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
if (local_apic_enable() != KERN_OK) {
|
||||
configure_legacy_pic();
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
disable_8259();
|
||||
return KERN_OK;
|
||||
}
|
||||
18
arch/x86_64/acpi/apic_ctrl.S
Normal file
18
arch/x86_64/acpi/apic_ctrl.S
Normal file
@@ -0,0 +1,18 @@
|
||||
.global check_apic
|
||||
.type check_apic, @function
|
||||
check_apic:
|
||||
push %rbp
|
||||
mov %rsp, %rbp
|
||||
|
||||
push %rbx
|
||||
mov $1, %rax
|
||||
cpuid
|
||||
|
||||
andl $0x200, %edx
|
||||
shr $9, %edx
|
||||
|
||||
mov %rdx, %rax
|
||||
|
||||
pop %rbx
|
||||
pop %rbp
|
||||
ret
|
||||
@@ -9,7 +9,7 @@ static struct acpi_rsdp_10 *search_for_rsdp(void)
|
||||
{
|
||||
uint64_t *start = vm_phys_to_virt(0x80000);
|
||||
uint64_t *end = vm_phys_to_virt(0xFFFFF);
|
||||
|
||||
|
||||
for (uint64_t *i = start; i < end; i++) {
|
||||
if (*i == ACPI_SIG_RSDP) {
|
||||
return (struct acpi_rsdp_10 *)i;
|
||||
@@ -24,7 +24,6 @@ static int init_rsdt(void)
|
||||
struct acpi_rsdp_10 *x = search_for_rsdp();
|
||||
|
||||
if (!x) {
|
||||
printk("acpi: not available");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -33,25 +32,23 @@ static int init_rsdt(void)
|
||||
for (unsigned int i = 0; i < sizeof *x; i++) {
|
||||
chksum += b[i];
|
||||
}
|
||||
|
||||
|
||||
if (chksum & 0xFF) {
|
||||
printk("acpi: not available");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x->r_revision == 2) {
|
||||
chksum = 0;
|
||||
|
||||
|
||||
for (unsigned int i = sizeof *x; i < sizeof(struct acpi_rsdp_20); i++) {
|
||||
chksum += b[i];
|
||||
}
|
||||
|
||||
if (chksum & 0xFF) {
|
||||
printk("acpi: not available");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
memcpy(&rsdp.rsdp_20, x, sizeof rsdp.rsdp_20);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,27 +3,6 @@
|
||||
#include <socks/vm.h>
|
||||
#include <socks/cpu.h>
|
||||
|
||||
#define AP_TRAMPOLINE_PADDR 0x8000
|
||||
#define LAPIC_IPI_STATUS_REG 0x0280
|
||||
#define LAPIC_IPI_DEST_REG 0x0310
|
||||
#define LAPIC_IPI_ICR_REG 0x0300
|
||||
|
||||
struct acpi_madt_record {
|
||||
uint8_t r_type;
|
||||
uint8_t r_length;
|
||||
} __packed;
|
||||
|
||||
struct lapic_record {
|
||||
uint8_t l_cpu_id;
|
||||
uint8_t l_apic_id;
|
||||
uint32_t l_flags;
|
||||
} __packed;
|
||||
|
||||
struct lapic_override_record {
|
||||
uint16_t l_reserved;
|
||||
uint64_t l_lapic_ptr;
|
||||
} __packed;
|
||||
|
||||
extern uint8_t acpi_bsp_lapic_id(void);
|
||||
extern char ap_trampoline[];
|
||||
|
||||
@@ -61,31 +40,6 @@ static int __used init_ap(struct acpi_madt_record *rec, void *bsp_lapic, uint8_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *__used find_lapic(struct acpi_madt *madt)
|
||||
{
|
||||
phys_addr_t local_apic = madt->m_lapic_ptr;
|
||||
|
||||
unsigned char *p = (unsigned char *)madt + sizeof *madt;
|
||||
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
|
||||
|
||||
while (p < madt_end) {
|
||||
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
|
||||
struct lapic_override_record *lapic;
|
||||
|
||||
switch (rec->r_type) {
|
||||
case ACPI_MADT_LAPIC_OVERRIDE:
|
||||
lapic = (struct lapic_override_record *)(rec + 1);
|
||||
return vm_phys_to_virt(lapic->l_lapic_ptr);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
return vm_phys_to_virt(local_apic);
|
||||
}
|
||||
|
||||
/*
|
||||
kern_status_t acpi_smp_init(void)
|
||||
{
|
||||
@@ -125,10 +79,20 @@ kern_status_t acpi_smp_init(void)
|
||||
}
|
||||
*/
|
||||
|
||||
static void no_smp_config(void)
|
||||
{
|
||||
cpu_set_available(0);
|
||||
cpu_set_online(0);
|
||||
|
||||
ml_cpu_block *self = ml_this_cpu();
|
||||
self->c_cpu_id = 0;
|
||||
}
|
||||
|
||||
kern_status_t acpi_scan_cpu_topology(void)
|
||||
{
|
||||
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
|
||||
if (!madt) {
|
||||
no_smp_config();
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
@@ -171,3 +135,8 @@ kern_status_t acpi_scan_cpu_topology(void)
|
||||
printk("acpi: found %u logical cores", nr_processors);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t smp_init(void)
|
||||
{
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user