x86_64: move ACPI driver to kext
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
#include <arch/acpi.h>
|
||||
#include <socks/printk.h>
|
||||
#include <socks/device.h>
|
||||
#include <socks/libc/stdio.h>
|
||||
|
||||
static kern_status_t acpi_bus_scan(struct device *dev)
|
||||
{
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static struct bus_device_ops acpi_ops = {
|
||||
.scan = acpi_bus_scan,
|
||||
};
|
||||
|
||||
kern_status_t acpi_init(void)
|
||||
{
|
||||
apic_init();
|
||||
smp_init();
|
||||
|
||||
struct bus_device *acpi_dev = bus_device_create();
|
||||
struct device *base = bus_device_base(acpi_dev);
|
||||
acpi_dev->b_ops = &acpi_ops;
|
||||
snprintf(base->dev_name, sizeof base->dev_name, "acpi");
|
||||
|
||||
device_register(base, system_driver(), root_device());
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
.global acpi_ap_lapic_id
|
||||
.type acpi_ap_lapic_id, @function
|
||||
|
||||
acpi_ap_lapic_id:
|
||||
push %rbp
|
||||
mov %rsp, %rbp
|
||||
|
||||
push %rbx
|
||||
|
||||
mov $0x00000001, %eax
|
||||
cpuid
|
||||
shrq $0x18, %rbx
|
||||
|
||||
mov %rbx, %rax
|
||||
|
||||
pop %rbx
|
||||
pop %rbp
|
||||
ret
|
||||
|
||||
|
||||
.extern __ap_stack_top
|
||||
.extern __bsp_done
|
||||
.extern __this_ap_ok
|
||||
.extern __all_ap_ok
|
||||
|
||||
.extern ap_startup
|
||||
.type ap_startup, @function
|
||||
|
||||
.global ap_trampoline
|
||||
|
||||
# this code will be relocated to 0x8000, sets up environment for calling a C function
|
||||
.code16
|
||||
.align 0x100
|
||||
ap_trampoline:
|
||||
cli
|
||||
cld
|
||||
ljmp $0, $0x8080
|
||||
.align 16
|
||||
_L8010_GDT_table:
|
||||
.long 0, 0
|
||||
.long 0x0000FFFF, 0x00CF9A00 # flat code
|
||||
.long 0x0000FFFF, 0x008F9200 # flat data
|
||||
.long 0x00000068, 0x00CF8900 # tss
|
||||
_L8030_GDT_value:
|
||||
.word _L8030_GDT_value - _L8010_GDT_table - 1
|
||||
.long 0x8010
|
||||
.long 0, 0
|
||||
.align 64
|
||||
_L8040_GDT64_table:
|
||||
.long 0, 0
|
||||
.long 0x0000ffff, 0x002f9a00 # kernel code
|
||||
.long 0x0000ffff, 0x002f9200 # kernel data
|
||||
_L8058_GDT64_value:
|
||||
.word _L8058_GDT64_value - _L8040_GDT64_table - 1
|
||||
.quad 0x8040
|
||||
|
||||
.align 64
|
||||
_L8080:
|
||||
xorw %ax, %ax
|
||||
movw %ax, %ds
|
||||
lgdtl 0x8030
|
||||
movl %cr0, %eax
|
||||
orl $1, %eax
|
||||
movl %eax, %cr0
|
||||
ljmp $8, $0x80a0
|
||||
.align 32
|
||||
.code32
|
||||
_L80A0:
|
||||
movw $16, %ax
|
||||
movw %ax, %ds
|
||||
movw %ax, %ss
|
||||
# get our Local APIC ID
|
||||
mov $1, %eax
|
||||
cpuid
|
||||
shrl $24, %ebx
|
||||
movl %ebx, %edi
|
||||
|
||||
movl %cr4, %eax
|
||||
orl $0x20, %eax
|
||||
movl %eax, %cr4
|
||||
|
||||
movl $0x8ff8, %eax
|
||||
movl (%eax), %eax
|
||||
mov %eax, %cr3
|
||||
|
||||
movl $0xC0000080, %ecx
|
||||
rdmsr
|
||||
# enable long mode, syscall/sysret, and NX protection
|
||||
orl $0x00000901, %eax
|
||||
wrmsr
|
||||
|
||||
movl $0x8058, %eax
|
||||
lgdt (%eax)
|
||||
|
||||
movl %cr0, %ecx
|
||||
orl $0x8000002a, %ecx
|
||||
movl %ecx, %cr0
|
||||
|
||||
ljmpl $0x08, $0x8100
|
||||
|
||||
.code64
|
||||
.align 64
|
||||
_L8100:
|
||||
mov (__ap_stack_top), %rsp
|
||||
movabsq $(ap_trampoline_exit), %rax
|
||||
callq *%rax
|
||||
hlt
|
||||
@@ -1,230 +0,0 @@
|
||||
#include <socks/printk.h>
|
||||
#include <socks/clock.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/cpu.h>
|
||||
#include <arch/acpi/io_apic.hpp>
|
||||
#include <arch/acpi/local_apic.hpp>
|
||||
#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 int apic_enabled = 0;
|
||||
static unsigned int bsp_id = (unsigned int)-1;
|
||||
|
||||
using namespace arch::acpi;
|
||||
|
||||
static struct queue io_apics;
|
||||
|
||||
extern "C" {
|
||||
/* defined in apic_ctrl.S */
|
||||
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);
|
||||
}
|
||||
|
||||
kern_status_t local_apic_enable(struct acpi_madt *madt)
|
||||
{
|
||||
apic_set_base(apic_get_base());
|
||||
local_apic::find(madt, local_apic::get());
|
||||
|
||||
local_apic::get().write(0xF0, 0x1FF);
|
||||
local_apic::get().ack();
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static io_apic *get_ioapic_for_irq(unsigned int vec)
|
||||
{
|
||||
queue_foreach (io_apic, ioapic, &io_apics, io_entry) {
|
||||
if (vec >= ioapic->io_first_irq && vec < ioapic->io_first_irq + ioapic->io_nr_irq) {
|
||||
return ioapic;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void configure_legacy_pic(void)
|
||||
{
|
||||
printk("acpi: APIC unavailable");
|
||||
pit_start(HZ);
|
||||
}
|
||||
|
||||
static void ioapic_init(uintptr_t base, unsigned int int_base)
|
||||
{
|
||||
uint32_t *base_vaddr = (uint32_t *)vm_phys_to_virt(base);
|
||||
io_apic *apic = kmalloc_object(io_apic, VM_NORMAL, base_vaddr, int_base);
|
||||
|
||||
//printk("acpi: I/O APIC at 0x%llx; base=%u, irqs=%u", base, int_base, apic->io_nr_irq);
|
||||
|
||||
for (unsigned int i = 0; i < apic->io_nr_irq; i++) {
|
||||
apic->map_irq(i, 32 + int_base + i);
|
||||
}
|
||||
|
||||
queue_push_back(&io_apics, &apic->io_entry);
|
||||
}
|
||||
|
||||
static int lapic_clock_irq(void)
|
||||
{
|
||||
if (this_cpu() == bsp_id) {
|
||||
clock_advance(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct irq_hook lapic_clock_irq_hook = {
|
||||
{},
|
||||
lapic_clock_irq,
|
||||
};
|
||||
|
||||
void local_apic_config_timer(bool bsp)
|
||||
{
|
||||
local_apic& lapic = local_apic::get();
|
||||
|
||||
lapic.write(local_apic::TIMER_DIV, 0x3);
|
||||
lapic.write(local_apic::TIMER_INITCOUNT, (uint32_t)-1);
|
||||
clock_wait(10);
|
||||
lapic.write(local_apic::LVT_TIMER, APIC_LVT_INT_MASKED);
|
||||
|
||||
if (bsp) {
|
||||
/* mask IRQ0 to block interrupts from the PIT. */
|
||||
io_apic *irq0_apic = get_ioapic_for_irq(0);
|
||||
irq0_apic->mask_irq(0);
|
||||
}
|
||||
|
||||
uint32_t total_ticks = 0xFFFFFFFF - lapic.read(local_apic::TIMER_CURCOUNT);
|
||||
|
||||
/* convert to LAPIC ticks per kernel clock tick */
|
||||
total_ticks /= 10;
|
||||
|
||||
lapic.write(local_apic::LVT_TIMER, IRQ0 | APIC_LVT_TIMER_MODE_PERIODIC);
|
||||
lapic.write(local_apic::TIMER_DIV, 0x3);
|
||||
lapic.write(local_apic::TIMER_INITCOUNT, total_ticks);
|
||||
}
|
||||
|
||||
static void parse_legacy_irq_override(struct acpi_madt *madt)
|
||||
{
|
||||
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 acpi_madt_irqsrc_override*irq;
|
||||
io_apic *handler = nullptr;
|
||||
|
||||
switch (rec->r_type) {
|
||||
case ACPI_MADT_IRQSRC_OVERRIDE:
|
||||
irq = (struct acpi_madt_irqsrc_override *)(rec + 1);
|
||||
handler = get_ioapic_for_irq(irq->irq_srcvec);
|
||||
handler->map_irq(irq->irq_srcvec, 32 + irq->irq_destvec);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
}
|
||||
|
||||
static void init_all_ioapic(struct acpi_madt *madt)
|
||||
{
|
||||
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 acpi_madt_ioapic *ioapic;
|
||||
|
||||
switch (rec->r_type) {
|
||||
case ACPI_MADT_IOAPIC:
|
||||
ioapic = (struct acpi_madt_ioapic *)(rec + 1);
|
||||
ioapic_init(ioapic->io_address, ioapic->io_intbase);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
parse_legacy_irq_override(madt);
|
||||
}
|
||||
|
||||
kern_status_t ap_apic_init(void)
|
||||
{
|
||||
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
|
||||
local_apic_enable(madt);
|
||||
|
||||
//irq_enable();
|
||||
local_apic_config_timer(false);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
/* only the bootstrap processor should call apic_init()
|
||||
all other APs should call ap_apic_init() instead */
|
||||
kern_status_t apic_init(void)
|
||||
{
|
||||
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
|
||||
|
||||
if (check_apic() == 0 || !madt) {
|
||||
configure_legacy_pic();
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
bsp_id = this_cpu();
|
||||
init_all_ioapic(madt);
|
||||
|
||||
local_apic_enable(madt);
|
||||
disable_8259();
|
||||
|
||||
apic_enabled = 1;
|
||||
irq_enable();
|
||||
|
||||
pit_start(HZ);
|
||||
|
||||
local_apic_config_timer(true);
|
||||
pit_stop();
|
||||
|
||||
hook_irq(IRQ0, &lapic_clock_irq_hook);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
void irq_ack(unsigned int vec)
|
||||
{
|
||||
if (apic_enabled) {
|
||||
local_apic::get().ack();
|
||||
} else {
|
||||
if (vec >= 40) {
|
||||
outportb(0xA0, 0x20);
|
||||
}
|
||||
|
||||
outportb(0x20, 0x20);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
.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
|
||||
@@ -1,91 +0,0 @@
|
||||
#include <socks/compiler.h>
|
||||
#include <socks/printk.h>
|
||||
#include <arch/acpi/io_apic.hpp>
|
||||
|
||||
#define IOAPICID 0x00
|
||||
#define IOAPICVER 0x01
|
||||
#define IOAPICARB 0x02
|
||||
#define IOAPICREDTBL(n) (0x10 + 2 * n)
|
||||
|
||||
namespace arch::acpi {
|
||||
io_apic::io_apic(uint32_t *base, unsigned int first_irq) : io_base(base), io_first_irq(first_irq)
|
||||
{
|
||||
io_nr_irq = ((read(IOAPICVER) >> 16) + 1) & 0xFF;
|
||||
}
|
||||
|
||||
uint32_t io_apic::read(uint32_t reg)
|
||||
{
|
||||
write_once(io_base, reg & 0xFF);
|
||||
return read_once(io_base + 4);
|
||||
}
|
||||
|
||||
void io_apic::write(uint32_t reg, uint32_t val)
|
||||
{
|
||||
write_once(io_base, reg & 0xFF);
|
||||
write_once(io_base + 4, val);
|
||||
}
|
||||
|
||||
kern_status_t io_apic::read_irq(unsigned int index, irq_entry &entry)
|
||||
{
|
||||
if (index >= io_nr_irq) {
|
||||
return KERN_NO_ENTRY;
|
||||
}
|
||||
|
||||
uint32_t *entry_data = (uint32_t *)&entry;
|
||||
entry_data[0] = read(IOAPICREDTBL(index));
|
||||
entry_data[1] = read(IOAPICREDTBL(index) + 1);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
void io_apic::write_irq(unsigned int index, const irq_entry &entry)
|
||||
{
|
||||
if (index >= io_nr_irq) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t *entry_data = (const uint32_t *)&entry;
|
||||
write(IOAPICREDTBL(index), entry_data[0]);
|
||||
write(IOAPICREDTBL(index) + 1, entry_data[1]);
|
||||
}
|
||||
|
||||
void io_apic::map_irq(unsigned int src, unsigned int dest, unsigned int dest_cpu)
|
||||
{
|
||||
io_apic::irq_entry irq{};
|
||||
irq.irq_vec = dest;
|
||||
irq.irq_dest = dest_cpu;
|
||||
irq.irq_delivery = 0;
|
||||
irq.irq_destmode = 0;
|
||||
irq.irq_polarity = 0;
|
||||
irq.irq_triggermode = 0;
|
||||
irq.irq_mask = 0;
|
||||
|
||||
write_irq(src, irq);
|
||||
}
|
||||
|
||||
void io_apic::mask_irq(unsigned int vec)
|
||||
{
|
||||
irq_entry irq;
|
||||
if (read_irq(vec, irq) != KERN_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
irq.irq_mask = 1;
|
||||
write_irq(vec, irq);
|
||||
}
|
||||
|
||||
void io_apic::unmask_irq(unsigned int vec, unsigned int dest_cpu)
|
||||
{
|
||||
irq_entry irq;
|
||||
if (read_irq(vec, irq) != KERN_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
irq.irq_mask = 0;
|
||||
if (dest_cpu != (unsigned int)-1) {
|
||||
irq.irq_dest = dest_cpu;
|
||||
}
|
||||
|
||||
write_irq(vec, irq);
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
#include <arch/acpi/local_apic.hpp>
|
||||
#include <arch/acpi.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/compiler.h>
|
||||
|
||||
#define LAPIC_REG_EOI 0xB0
|
||||
|
||||
namespace arch::acpi {
|
||||
static local_apic lapic;
|
||||
|
||||
local_apic::local_apic(uint32_t *base) : base_(base)
|
||||
{
|
||||
}
|
||||
|
||||
kern_status_t local_apic::find(struct acpi_madt *madt, local_apic& out)
|
||||
{
|
||||
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);
|
||||
out.base_ = (uint32_t *)vm_phys_to_virt(lapic->l_lapic_ptr);
|
||||
return KERN_OK;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
out.base_ = (uint32_t *)vm_phys_to_virt(local_apic);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
uint32_t local_apic::read(uint32_t reg)
|
||||
{
|
||||
return read_once(base_ + (reg >> 2));
|
||||
}
|
||||
|
||||
void local_apic::write(uint32_t reg, uint32_t val)
|
||||
{
|
||||
write_once(base_ + (reg >> 2), val);
|
||||
}
|
||||
|
||||
void local_apic::ack()
|
||||
{
|
||||
write(LAPIC_REG_EOI, 0);
|
||||
}
|
||||
|
||||
void local_apic::send_ipi(unsigned int dest, unsigned int data)
|
||||
{
|
||||
uint32_t dest_val = (read(IPI_DEST) & 0x00FFFFFF) | (dest << 24);
|
||||
write(IPI_STATUS, 0);
|
||||
write(IPI_DEST, dest_val);
|
||||
|
||||
uint32_t icr_val = (read(IPI_ICR) & 0xFFF00000) | data;
|
||||
write(IPI_ICR, icr_val);
|
||||
|
||||
do {
|
||||
__asm__ __volatile__("pause" : : : "memory");
|
||||
} while (read(IPI_ICR) & (1 << 12));
|
||||
}
|
||||
|
||||
local_apic& local_apic::get(void)
|
||||
{
|
||||
return lapic;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
#include <arch/acpi.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/printk.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static struct acpi_rsdp rsdp;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int init_rsdt(void)
|
||||
{
|
||||
struct acpi_rsdp_10 *x = search_for_rsdp();
|
||||
|
||||
if (!x) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t *b = (uint8_t *)x;
|
||||
size_t chksum = 0;
|
||||
for (unsigned int i = 0; i < sizeof *x; i++) {
|
||||
chksum += b[i];
|
||||
}
|
||||
|
||||
if (chksum & 0xFF) {
|
||||
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) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(&rsdp.rsdp_20, x, sizeof rsdp.rsdp_20);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct acpi_sdt *find_via_rsdt(uint32_t sig)
|
||||
{
|
||||
struct acpi_rsdt *rsdt = vm_phys_to_virt(rsdp.rsdp_10.r_rsdt_ptr);
|
||||
unsigned long nr_tables = (rsdt->r_header.s_length - sizeof(struct acpi_sdt)) / sizeof(uint32_t);
|
||||
|
||||
for (unsigned long i = 0; i < nr_tables; i++) {
|
||||
struct acpi_sdt *table = vm_phys_to_virt(rsdt->r_tables[i]);
|
||||
if (*(uint32_t *)table == sig) {
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct acpi_sdt *find_via_xsdt(uint32_t sig)
|
||||
{
|
||||
struct acpi_xsdt *xsdt = vm_phys_to_virt(rsdp.rsdp_20.r_xsdt_ptr);
|
||||
unsigned long nr_tables = (xsdt->x_header.s_length - sizeof(struct acpi_sdt)) / sizeof(uint64_t);
|
||||
|
||||
for (unsigned long i = 0; i < nr_tables; i++) {
|
||||
struct acpi_sdt *table = vm_phys_to_virt(xsdt->x_tables[i]);
|
||||
if (*(uint32_t *)table == sig) {
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct acpi_sdt *acpi_find_sdt(uint32_t sig)
|
||||
{
|
||||
if (!rsdp.rsdp_10.r_sig[0]) {
|
||||
if (init_rsdt() != 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (rsdp.rsdp_10.r_revision == 0) {
|
||||
return find_via_rsdt(sig);
|
||||
} else {
|
||||
return find_via_xsdt(sig);
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
#include <arch/acpi.h>
|
||||
#include <arch/acpi/local_apic.hpp>
|
||||
#include <socks/printk.h>
|
||||
#include <socks/pmap.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/cpu.h>
|
||||
|
||||
using namespace arch::acpi;
|
||||
|
||||
extern "C" uint8_t acpi_ap_lapic_id(void);
|
||||
extern "C" char ap_trampoline[];
|
||||
|
||||
volatile struct vm_page *__ap_stack_page = NULL;
|
||||
volatile uintptr_t __ap_stack_top = 0;
|
||||
volatile uint8_t __this_ap_ok = 0;
|
||||
volatile uint8_t __all_ap_ok = 0;
|
||||
|
||||
extern "C" void ap_trampoline_exit(void)
|
||||
{
|
||||
struct vm_page *stack_page = (struct vm_page *)__ap_stack_page;
|
||||
__this_ap_ok = 1;
|
||||
|
||||
ml_cpu_block *this_cpu = (ml_cpu_block *)kmalloc(sizeof *this_cpu, VM_NORMAL);
|
||||
memset(this_cpu, 0x00, sizeof *this_cpu);
|
||||
ml_cpu_block_init(this_cpu);
|
||||
ml_cpu_block_use(this_cpu);
|
||||
|
||||
unsigned int cpu_id = acpi_ap_lapic_id();
|
||||
this_cpu->c_cpu_id = cpu_id;
|
||||
|
||||
struct cpu_data *self = get_this_cpu();
|
||||
memset(self, 0x0, sizeof *self);
|
||||
self->c_flags = CPU_ONLINE;
|
||||
self->c_id = cpu_id;
|
||||
this_cpu->c_data = self;
|
||||
|
||||
struct thread *this_thread = create_idle_thread();
|
||||
this_thread->tr_id = cpu_id;
|
||||
this_thread->tr_kstack = (struct vm_page *)stack_page;
|
||||
self->c_rq.rq_idle = self->c_rq.rq_cur = this_thread;
|
||||
|
||||
put_cpu(self);
|
||||
|
||||
ap_apic_init();
|
||||
|
||||
cpu_set_online(cpu_id);
|
||||
irq_enable();
|
||||
|
||||
idle();
|
||||
}
|
||||
|
||||
static int init_ap(struct acpi_madt_record *rec, local_apic& lapic, uint8_t bsp_id)
|
||||
{
|
||||
struct lapic_record *ap_lapic = (struct lapic_record *)(rec + 1);
|
||||
if (!(ap_lapic->l_flags & 0x1) && !(ap_lapic->l_flags & 0x2)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ap_lapic->l_apic_id == bsp_id) {
|
||||
//printk("acpi: core %u online [BSP]", ap_lapic->l_apic_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
__this_ap_ok = 0;
|
||||
__ap_stack_page = vm_page_alloc(VM_PAGE_4K, VM_NORMAL);
|
||||
__ap_stack_top = (uintptr_t)vm_page_get_vaddr((struct vm_page *)__ap_stack_page) + VM_PAGE_SIZE;
|
||||
|
||||
lapic.send_ipi(ap_lapic->l_apic_id, 0xC500);
|
||||
lapic.send_ipi(ap_lapic->l_apic_id, 0x8500);
|
||||
milli_sleep(10); // wait 10 msec
|
||||
lapic.send_ipi(ap_lapic->l_apic_id, 0x4600 | (AP_TRAMPOLINE_PADDR >> VM_PAGE_SHIFT));
|
||||
|
||||
do {
|
||||
ml_cpu_relax();
|
||||
} while (__this_ap_ok == 0);
|
||||
|
||||
//printk("acpi: core %u online [AP]", ap_lapic->l_apic_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
kern_status_t bring_all_ap_online(void)
|
||||
{
|
||||
struct acpi_madt *madt = (struct acpi_madt *)acpi_find_sdt(ACPI_SIG_MADT);
|
||||
if (!madt) {
|
||||
return KERN_UNSUPPORTED;
|
||||
}
|
||||
|
||||
uint8_t bsp_id = acpi_ap_lapic_id();
|
||||
|
||||
void *ap_trampoline_dest = vm_phys_to_virt(AP_TRAMPOLINE_PADDR);
|
||||
memcpy(ap_trampoline_dest, ap_trampoline, VM_PAGE_SIZE);
|
||||
|
||||
pmap_t kernel_pmap = get_kernel_pmap();
|
||||
uint32_t *pmap_ptr = (uint32_t *)vm_phys_to_virt(0x8ff8);
|
||||
*pmap_ptr = kernel_pmap;
|
||||
|
||||
pmap_add(kernel_pmap, (void *)0x8000, 8, (enum vm_prot)(VM_PROT_READ | VM_PROT_EXEC), PMAP_NORMAL);
|
||||
|
||||
unsigned char *p = (unsigned char *)madt + sizeof *madt;
|
||||
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
|
||||
|
||||
unsigned int nr_processors = 0;
|
||||
|
||||
while (p < madt_end) {
|
||||
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
|
||||
|
||||
switch (rec->r_type) {
|
||||
case ACPI_MADT_LAPIC:
|
||||
if (init_ap(rec, local_apic::get(), bsp_id) == 0) {
|
||||
nr_processors++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
while (cpu_nr_available() != cpu_nr_online()) {
|
||||
ml_cpu_relax();
|
||||
}
|
||||
|
||||
printk("acpi: %u APs online", cpu_nr_online());
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
uint8_t bsp_id = acpi_ap_lapic_id();
|
||||
|
||||
//void *bsp_lapic = find_lapic(madt);
|
||||
|
||||
unsigned char *p = (unsigned char *)madt + sizeof *madt;
|
||||
unsigned char *madt_end = (unsigned char *)madt + madt->m_base.s_length;
|
||||
|
||||
unsigned int nr_processors = 0;
|
||||
|
||||
while (p < madt_end) {
|
||||
struct acpi_madt_record *rec = (struct acpi_madt_record *)p;
|
||||
|
||||
if (rec->r_type != ACPI_MADT_LAPIC) {
|
||||
p += rec->r_length;
|
||||
continue;
|
||||
}
|
||||
|
||||
struct lapic_record *lapic = (struct lapic_record *)(rec + 1);
|
||||
|
||||
if (!(lapic->l_flags & 0x1) && !(lapic->l_flags & 0x2)) {
|
||||
/* processor cannot be used */
|
||||
continue;
|
||||
}
|
||||
|
||||
cpu_set_available(lapic->l_apic_id);
|
||||
nr_processors++;
|
||||
|
||||
p += rec->r_length;
|
||||
}
|
||||
|
||||
cpu_set_online(bsp_id);
|
||||
|
||||
/* store the BSP ID in the current cpu block */
|
||||
ml_cpu_block *self = ml_this_cpu();
|
||||
self->c_cpu_id = bsp_id;
|
||||
|
||||
printk("acpi: found %u logical cores", nr_processors);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t smp_init(void)
|
||||
{
|
||||
return bring_all_ap_online();
|
||||
}
|
||||
Reference in New Issue
Block a user