123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- #include "apic.h"
- #include <common/kprint.h>
- #include <common/printk.h>
- #include <common/cpu.h>
- #include <common/glib.h>
- #include <exception/gate.h>
- #include <driver/acpi/acpi.h>
- #include <exception/softirq.h>
- #include <process/process.h>
- #include <sched/sched.h>
- #pragma GCC push_options
- #pragma GCC optimize("O0")
- extern void (*interrupt_table[24])(void);
- bool flag_support_apic = false;
- bool flag_support_x2apic = false;
- uint local_apic_version;
- uint local_apic_max_LVT_entries;
- static struct acpi_Multiple_APIC_Description_Table_t *madt;
- static struct acpi_IO_APIC_Structure_t *io_apic_ICS;
- #define send_EOI() \
- do \
- { \
- __asm__ __volatile__("movq $0x00, %%rdx \n\t" \
- "movq $0x00, %%rax \n\t" \
- "movq $0x80b, %%rcx \n\t" \
- "wrmsr \n\t" :: \
- : "memory"); \
- } while (0)
- void apic_io_apic_init()
- {
- ul madt_addr;
- acpi_iter_SDT(acpi_get_MADT, &madt_addr);
- madt = (struct acpi_Multiple_APIC_Description_Table_t *)madt_addr;
-
-
-
- void *ent = (void *)(madt_addr) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
- struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
- while (header->length > 2)
- {
- header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
- if (header->type == 1)
- {
- struct acpi_IO_APIC_Structure_t *t = (struct acpi_IO_APIC_Structure_t *)ent;
-
- io_apic_ICS = t;
- break;
- }
- ent += header->length;
- }
-
- apic_ioapic_map.addr_phys = io_apic_ICS->IO_APIC_Address;
- apic_ioapic_map.virtual_index_addr = (unsigned char *)APIC_IO_APIC_VIRT_BASE_ADDR;
- apic_ioapic_map.virtual_data_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x10);
- apic_ioapic_map.virtual_EOI_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x40);
-
-
- mm_map_phys_addr((ul)apic_ioapic_map.virtual_index_addr, apic_ioapic_map.addr_phys, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
-
- *apic_ioapic_map.virtual_index_addr = 0x00;
- io_mfence();
- *apic_ioapic_map.virtual_data_addr = 0x0f000000;
- io_mfence();
-
- io_mfence();
-
- *apic_ioapic_map.virtual_index_addr = 0x01;
- io_mfence();
- kdebug("IO APIC Version=%d, Max Redirection Entries=%d", *apic_ioapic_map.virtual_data_addr & 0xff, (((*apic_ioapic_map.virtual_data_addr) >> 16) & 0xff) + 1);
-
- for (int i = 0x10; i < 0x40; i += 2)
- {
-
- apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1));
- }
-
-
-
- }
- void apic_init_ap_core_local_apic()
- {
- kinfo("Initializing AP-core's local apic...");
- uint eax, edx;
-
- __asm__ __volatile__("movq $0x1b, %%rcx \n\t"
- "rdmsr \n\t"
- "bts $10, %%rax \n\t"
- "bts $11, %%rax \n\t"
- "wrmsr \n\t"
- "movq $0x1b, %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)::"memory");
-
-
- if (eax & 0xc00)
- kinfo("xAPIC & x2APIC enabled!");
-
-
- __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
- "rdmsr \n\t"
- "bts $8, %%rax \n\t"
-
- "wrmsr \n\t"
- "movq $0x80f, %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)
- :
- : "memory");
- if (eax & 0x100)
- printk_color(RED, YELLOW, "SVR[8] enabled\n");
- if (edx & 0x1000)
- printk_color(RED, YELLOW, "SVR[12] enabled\n");
-
- __asm__ __volatile__("movq $0x802, %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)
- :
- : "memory");
- printk_color(RED, YELLOW, "x2APIC ID:%#010x\n", eax);
-
-
- __asm__ __volatile__(
-
- "movq $0x832, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x833, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x834, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x835, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x836, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x837, %%rcx \n\t"
- "wrmsr \n\t"
- :
- : "a"(0x10000), "d"(0x00)
- : "memory");
- kdebug("All LVT Masked");
- }
- void apic_local_apic_init()
- {
-
- mm_map_phys_addr(APIC_LOCAL_APIC_VIRT_BASE_ADDR, 0xfee00000UL, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
- uint a, b, c, d;
- cpu_cpuid(1, 0, &a, &b, &c, &d);
-
-
- if ((1 << 9) & d)
- {
- flag_support_apic = true;
- kdebug("This computer support APIC&xAPIC");
- }
- else
- {
- flag_support_apic = false;
- kerror("This computer does not support APIC&xAPIC");
- while (1)
- ;
- }
-
- if ((1 << 21) & c)
- {
- flag_support_x2apic = true;
- kdebug("This computer support x2APIC");
- }
- else
- {
- kerror("This computer does not support x2APIC");
- }
- uint eax, edx;
-
- __asm__ __volatile__("movq $0x1b, %%rcx \n\t"
- "rdmsr \n\t"
- "bts $10, %%rax \n\t"
- "bts $11, %%rax \n\t"
- "wrmsr \n\t"
- "movq $0x1b, %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)::"memory");
-
-
- if (eax & 0xc00)
- kinfo("xAPIC & x2APIC enabled!");
-
-
- __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
- "rdmsr \n\t"
- "bts $8, %%rax \n\t"
-
- "movq $0x80f, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x80f , %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)::"memory");
-
-
- if (eax & 0x100)
- kinfo("APIC Software Enabled.");
- if (eax & 0x1000)
- kinfo("EOI-Broadcast Suppression Enabled.");
-
-
-
-
-
-
-
-
-
- __asm__ __volatile__("movq $0x803, %%rcx \n\t"
- "rdmsr \n\t"
- : "=a"(eax), "=d"(edx)::"memory");
- local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
- local_apic_version = eax & 0xff;
- kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (eax >> 24) & 0x1);
- if ((eax & 0xff) < 0x10)
- {
- kdebug("82489DX discrete APIC");
- }
- else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
- kdebug("Integrated APIC.");
-
-
- __asm__ __volatile__(
-
- "movq $0x832, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x833, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x834, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x835, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x836, %%rcx \n\t"
- "wrmsr \n\t"
- "movq $0x837, %%rcx \n\t"
- "wrmsr \n\t"
- :
- : "a"(0x10000), "d"(0x00)
- : "memory");
-
- *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER) = APIC_LVT_INT_MASKED;
- io_mfence();
-
- kdebug("All LVT Masked");
-
- }
- void apic_init()
- {
-
- for (int i = 32; i <= 55; ++i)
- set_intr_gate(i, 0, interrupt_table[i - 32]);
-
- for (int i = 150; i < 160; ++i)
- set_intr_gate(i, 0, local_apic_interrupt_table[i - 150]);
-
-
- io_mfence();
- io_out8(0xa1, 0xff);
- io_mfence();
- io_out8(0x21, 0xff);
- io_mfence();
- kdebug("8259A Masked.");
-
- io_out8(0x22, 0x70);
- io_out8(0x23, 0x01);
- apic_local_apic_init();
- apic_io_apic_init();
-
- io_out32(0xcf8, 0x8000f8f0);
- uint32_t RCBA_phys = io_in32(0xcfc);
-
- if (RCBA_phys > 0xfec00000 && RCBA_phys < 0xfee00000)
- RCBA_vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + RCBA_phys;
- else
- {
- RCBA_vaddr = 0;
- kwarn("Cannot get RCBA address. RCBA_phys=%#010lx", RCBA_phys);
- }
- sti();
- }
- void do_IRQ(struct pt_regs *rsp, ul number)
- {
- if (number < 0x80 && number >= 32)
- {
-
- irq_desc_t *irq = &interrupt_desc[number - 32];
-
- if (irq != NULL && irq->handler != NULL)
- irq->handler(number, irq->parameter, rsp);
- else
- kwarn("Intr vector [%d] does not have a handler!");
-
- if (irq->controller != NULL && irq->controller->ack != NULL)
- irq->controller->ack(number);
- else
- {
-
- __asm__ __volatile__("movq $0x00, %%rdx \n\t"
- "movq $0x00, %%rax \n\t"
- "movq $0x80b, %%rcx \n\t"
- "wrmsr \n\t" ::
- : "memory");
- }
- }
- else if (number == 0x80)
- {
-
- do_syscall_int(rsp, 0);
- }
- else if (number >= 200)
- {
-
- apic_local_apic_edge_ack(number);
- {
- irq_desc_t *irq = &SMP_IPI_desc[number - 200];
- if (irq->handler != NULL)
- irq->handler(number, irq->parameter, rsp);
- }
- }
- else if (number >= 150 && number < 200)
- {
- irq_desc_t *irq = &local_apic_interrupt_desc[number - 150];
-
- if (irq != NULL && irq->handler != NULL)
- irq->handler(number, irq->parameter, rsp);
- else
- kwarn("Intr vector [%d] does not have a handler!");
-
- if (irq->controller != NULL && irq->controller->ack != NULL)
- irq->controller->ack(number);
- else
- {
-
- send_EOI();
- }
- }
- else
- {
- kwarn("do IRQ receive: %d", number);
- }
-
-
- do_softirq();
-
-
- if (current_pcb->preempt_count > 0)
- return;
- else if (current_pcb->preempt_count < 0)
- kBUG("current_pcb->preempt_count<0! pid=%d", current_pcb->pid);
-
- if (current_pcb->flags & PF_NEED_SCHED)
- {
- io_mfence();
- sched();
- }
- }
- ul apic_ioapic_read_rte(unsigned char index)
- {
-
- ul ret;
-
- *apic_ioapic_map.virtual_index_addr = index + 1;
- io_mfence();
- ret = *apic_ioapic_map.virtual_data_addr;
- ret <<= 32;
- io_mfence();
-
- *apic_ioapic_map.virtual_index_addr = index;
- io_mfence();
- ret |= *apic_ioapic_map.virtual_data_addr;
- io_mfence();
- return ret;
- }
- void apic_ioapic_write_rte(unsigned char index, ul value)
- {
-
- *apic_ioapic_map.virtual_index_addr = index;
- io_mfence();
- *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
- io_mfence();
-
- value >>= 32;
- io_mfence();
- *apic_ioapic_map.virtual_index_addr = index + 1;
- io_mfence();
- *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
- io_mfence();
- }
- void apic_ioapic_enable(ul irq_num)
- {
- ul index = 0x10 + ((irq_num - 32) << 1);
- ul value = apic_ioapic_read_rte(index);
- value &= (~0x10000UL);
- apic_ioapic_write_rte(index, value);
- }
- void apic_ioapic_disable(ul irq_num)
- {
- ul index = 0x10 + ((irq_num - 32) << 1);
- ul value = apic_ioapic_read_rte(index);
- value |= (0x10000UL);
- apic_ioapic_write_rte(index, value);
- }
- ul apic_ioapic_install(ul irq_num, void *arg)
- {
- struct apic_IO_APIC_RTE_entry *entry = (struct apic_IO_APIC_RTE_entry *)arg;
-
- apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), *(ul *)entry);
- return 0;
- }
- void apic_ioapic_uninstall(ul irq_num)
- {
-
- apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), 0x10000UL);
- }
- void apic_ioapic_level_ack(ul irq_num)
- {
-
-
- __asm__ __volatile__("movq $0x00, %%rdx \n\t"
- "movq $0x00, %%rax \n\t"
- "movq $0x80b, %%rcx \n\t"
- "wrmsr \n\t" ::
- : "memory");
- *apic_ioapic_map.virtual_EOI_addr = irq_num;
- }
- void apic_ioapic_edge_ack(ul irq_num)
- {
-
-
- __asm__ __volatile__("movq $0x00, %%rdx \n\t"
- "movq $0x00, %%rax \n\t"
- "movq $0x80b, %%rcx \n\t"
- "wrmsr \n\t" ::
- : "memory");
- }
- void apic_local_apic_edge_ack(ul irq_num)
- {
-
- __asm__ __volatile__("movq $0x00, %%rdx \n\t"
- "movq $0x00, %%rax \n\t"
- "movq $0x80b, %%rcx \n\t"
- "wrmsr \n\t" ::
- : "memory");
- }
- uint apic_get_ics(const uint type, ul ret_vaddr[], uint *total)
- {
- void *ent = (void *)(madt) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
- struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
- bool flag = false;
- uint cnt = 0;
- while (header->length > 2)
- {
- header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
- if (header->type == type)
- {
- ret_vaddr[cnt++] = (ul)ent;
- flag = true;
- }
- ent += header->length;
- }
- *total = cnt;
- if (!flag)
- return APIC_E_NOTFOUND;
- else
- return APIC_SUCCESS;
- }
- void apic_make_rte_entry(struct apic_IO_APIC_RTE_entry *entry, uint8_t vector, uint8_t deliver_mode, uint8_t dest_mode,
- uint8_t deliver_status, uint8_t polarity, uint8_t irr, uint8_t trigger, uint8_t mask, uint8_t dest_apicID)
- {
- entry->vector = vector;
- entry->deliver_mode = deliver_mode;
- entry->dest_mode = dest_mode;
- entry->deliver_status = deliver_status;
- entry->polarity = polarity;
- entry->remote_IRR = irr;
- entry->trigger_mode = trigger;
- entry->mask = mask;
- entry->reserved = 0;
- if (dest_mode == DEST_PHYSICAL)
- {
- entry->destination.physical.phy_dest = dest_apicID;
- entry->destination.physical.reserved1 = 0;
- entry->destination.physical.reserved2 = 0;
- }
- else
- {
- entry->destination.logical.logical_dest = dest_apicID;
- entry->destination.logical.reserved1 = 0;
- }
- }
- #pragma GCC pop_options
|