From 87146087ca263dd9a9191b26bb1cf0ea86e28892 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 4 May 2023 21:47:48 -0400 Subject: [PATCH] begin ahci driver --- dev/ahci.c | 26 +++++ dev/pci.c | 37 ++++++- include/dev/ahci.h | 226 ++++++++++++++++++++++++++++++++++++++++ include/dev/pci.h | 90 ++++++++++++++++ include/kernel/common.h | 1 + include/kernel/irq.h | 11 ++ init/irq.c | 140 +++++++++++++++++++++++++ 7 files changed, 529 insertions(+), 2 deletions(-) create mode 100644 dev/ahci.c create mode 100644 include/dev/ahci.h create mode 100644 include/kernel/irq.h create mode 100644 init/irq.c diff --git a/dev/ahci.c b/dev/ahci.c new file mode 100644 index 0000000..697ce03 --- /dev/null +++ b/dev/ahci.c @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +#include +#include + +static hbamem_t *abar = NULL; + +void ahci_init() +{ + struct pci_device *dev = pci_find_device(0x01, 0x06); + if (dev == NULL) { + printf(LOG_ERROR "Failed to find AHCI device\n"); + return; + } + + abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start); + + if (!(abar->ghc & (1 << 31))) { + printf(LOG_ERROR "AHCI controller not enabled\n"); + return; + } + + printf(LOG_SUCCESS "AHCI controller ready\n"); +} diff --git a/dev/pci.c b/dev/pci.c index 18f7667..af1ab6b 100644 --- a/dev/pci.c +++ b/dev/pci.c @@ -42,10 +42,43 @@ static void pci_scan_bus(uint8_t bus) pci_devices[pci_device_count].class = class; pci_devices[pci_device_count].subclass = subclass; + pci_devices[pci_device_count].bar0 = pci_config_read_word(bus, slot, 0, 0x10); + pci_devices[pci_device_count].bar0 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x12) << 16); + + pci_devices[pci_device_count].bar1 = pci_config_read_word(bus, slot, 0, 0x14); + pci_devices[pci_device_count].bar1 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x16) << 16); + + uint8_t header_type = pci_config_read_word(bus, slot, 0, 0xE) >> 16; + pci_devices[pci_device_count].header_type = header_type; + + if (header_type == 0) { + pci_devices[pci_device_count].bar2 = pci_config_read_word(bus, slot, 0, 0x18); + pci_devices[pci_device_count].bar2 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x1A) << 16); + + pci_devices[pci_device_count].bar3 = pci_config_read_word(bus, slot, 0, 0x1C); + pci_devices[pci_device_count].bar3 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x1E) << 16); + + pci_devices[pci_device_count].bar4 = pci_config_read_word(bus, slot, 0, 0x20); + pci_devices[pci_device_count].bar4 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x22) << 16); + + pci_devices[pci_device_count].bar5 = pci_config_read_word(bus, slot, 0, 0x24); + pci_devices[pci_device_count].bar5 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x26) << 16); + } + pci_device_count++; } } +struct pci_device *pci_find_device(uint8_t class, uint8_t subclass) +{ + for (int i = 0; i < pci_device_count; i++) { + if (pci_devices[i].class == class && pci_devices[i].subclass == subclass) + return &pci_devices[i]; + } + + return NULL; +} + void pci_init() { pci_devices = kzalloc(sizeof(struct pci_device) * MAX_PCI_DEVICES); @@ -58,9 +91,9 @@ void pci_init() /* Print PCI devices */ for (int i = 0; i < pci_device_count; i++) { struct pci_device *dev = &pci_devices[i]; - const char fmt[] = LOG_DEBUG "PCI device BUS[%d:%d] CLASS[%d:%d] vendor_id=%xh device_id=%xh\n"; + const char fmt[] = LOG_DEBUG "PCI device BUS[%d:%d] CLASS[%d:%d] vendor_id=%xh device_id=%xh, header_type=%d\n"; - printf(fmt, dev->bus, dev->slot, dev->class, dev->subclass, dev->vendor_id, dev->device_id); + printf(fmt, dev->bus, dev->slot, dev->class, dev->subclass, dev->vendor_id, dev->device_id, dev->header_type); } #endif /* KDEBUG */ } diff --git a/include/dev/ahci.h b/include/dev/ahci.h new file mode 100644 index 0000000..99d53d4 --- /dev/null +++ b/include/dev/ahci.h @@ -0,0 +1,226 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _AHCI_H_ +#define _AHCI_H_ + +#include +#include + +/* Frame information structure (FIS) */ +#define FIS_REG_H2D 0x27 +#define FIS_REG_D2H 0x34 +#define FIS_DMA_ACT 0x39 +#define FIS_DMA_SETUP 0x41 +#define FIS_DATA 0x46 +#define FIS_BIST 0x58 +#define FIS_PIO_SETUP 0x5F +#define FIS_DEV_BITS 0xA1 + +#define ATA_CMD_IDENT 0xEC + +struct fis_reg_h2d { + /* 0x00 */ + uint8_t fis_type; + + uint8_t pmult : 4; + uint8_t reserved : 3; + uint8_t cmd_mode : 1; /* high: command, low: control */ + + uint8_t command; + uint8_t feature_low; + + /* 0x04 */ + uint8_t lba0; + uint8_t lba1; + uint8_t lba2; + uint8_t device; + + /* 0x08 */ + uint8_t lba3; + uint8_t lba4; + uint8_t lba5; + uint8_t feature_high; + + /* 0x0C */ + uint8_t count_low; + uint8_t count_high; + uint8_t icc; + uint8_t control; + + /* 0x10 */ + uint32_t reserved2; +} PACKED; + +struct fis_reg_d2h { + /* 0x00 */ + uint8_t fis_type; + + uint8_t pmult : 4; + uint8_t reserved : 2; + uint8_t intr : 1; + uint8_t reserved2 : 1; + + uint8_t status; + uint8_t error; + + /* 0x04 */ + uint8_t lba0; + uint8_t lba1; + uint8_t lba2; + uint8_t device; + + /* 0x08 */ + uint8_t lba3; + uint8_t lba4; + uint8_t lba5; + uint8_t feature_high; + + /* 0x0C */ + uint8_t count_low; + uint8_t count_high; + uint8_t icc; + uint8_t control; + + /* 0x10 */ + uint32_t reserved3; +} PACKED; + +struct fis_data { + /* 0x00 */ + uint8_t fis_type; + + uint8_t pmult : 4; + uint8_t reserved : 4; + + uint8_t reserved2[2]; + + /* 0x04 */ + uint32_t data[1]; /* Variable length */ +} PACKED; + +struct fis_dma_setup { + /* 0x00 */ + uint8_t fis_type; + + uint8_t pmult : 4; + uint8_t reserved : 1; + uint8_t data_dir : 1; + uint8_t intr : 1; + uint8_t auto_act : 1; + + uint16_t reserved2; + + /* 0x04 */ + uint64_t dma_buffer_id; + + /* 0x0C */ + uint32_t reserved3; + + /* 0x10 */ + uint32_t dma_buffer_offset; + + /* 0x14 */ + uint32_t transfer_count; + + /* 0x18 */ + uint32_t reserved4; +} PACKED; + +struct fis_pio_setup { + /* 0x00 */ + uint8_t fis_type; + + uint8_t pmult : 4; + uint8_t reserved : 1; + uint8_t data_dir : 1; + uint8_t intr : 1; + uint8_t reserved2 : 1; + + uint8_t status; + uint8_t error; + + /* 0x04 */ + uint8_t lba0; + uint8_t lba1; + uint8_t lba2; + uint8_t device; + + /* 0x08 */ + uint8_t lba3; + uint8_t lba4; + uint8_t lba5; + uint8_t reserved3; + + /* 0x0C */ + uint8_t count_low; + uint8_t count_high; + uint8_t reserved4; + uint8_t e_status; + + /* 0x10 */ + uint16_t transfer_count; + uint16_t reserved5; +} PACKED; + +typedef volatile struct { + /* 0x00 */ + uint32_t clb; + uint32_t clbu; + uint32_t fb; + uint32_t fbu; + + /* 0x10 */ + uint32_t is; + uint32_t ie; + uint32_t cmd; + uint32_t reserved; + + /* 0x20 */ + uint32_t tfd; + uint32_t sig; + uint32_t ssts; + uint32_t sctl; + + /* 0x30 */ + uint32_t serr; + uint32_t sact; + uint32_t ci; + uint32_t sntf; + + /* 0x40 */ + uint32_t fbs; + + uint32_t reserved1[11]; + uint32_t vendor[4]; +} PACKED hbaport_t; + +typedef volatile struct { + /* 0x00 */ + uint32_t cap; + uint32_t ghc; + uint32_t is; + uint32_t pi; + uint32_t vs; + + /* 0x14 */ + uint32_t ccc_ctl; + uint32_t ccc_ports; + uint32_t em_loc; + uint32_t em_ctl; + uint32_t cap2; + + /* 0x28 */ + uint32_t bohc; + + /* 0x2C */ + uint8_t reserved[0x74]; + + /* 0xA0 */ + char vendor[0x60]; + + /* 0x100 */ + hbaport_t ports[32]; +} PACKED hbamem_t; + +void ahci_init(); + +#endif /* _AHCI_H_ */ diff --git a/include/dev/pci.h b/include/dev/pci.h index 978c6a3..b182d94 100644 --- a/include/dev/pci.h +++ b/include/dev/pci.h @@ -17,9 +17,99 @@ struct pci_device { uint16_t vendor_id; uint16_t device_id; + uint8_t header_type; + + paddr32_t bar0; + paddr32_t bar1; + paddr32_t bar2; + paddr32_t bar3; + paddr32_t bar4; + paddr32_t bar5; }; +struct pci_header_base { + uint16_t vendor_id; + uint16_t device_id; + + uint16_t command; + uint16_t status; + + uint8_t revision_id; + uint8_t prog_if; + uint8_t subclass; + uint8_t class; + + uint8_t cache_line_size; + uint8_t latency_timer; + uint8_t header_type; + uint8_t bist; +} PACKED; + +struct pci_header_type_0 { + struct pci_header_base base; + + paddr32_t bar0; + paddr32_t bar1; + paddr32_t bar2; + paddr32_t bar3; + paddr32_t bar4; + paddr32_t bar5; + + uint32_t cardbus_cis_pointer; + + uint16_t subsystem_vendor_id; + uint16_t subsystem_id; + + uint32_t expansion_rom_base_address; + + uint8_t capabilities_pointer; + uint8_t reserved[7]; + + uint8_t interrupt_line; + uint8_t interrupt_pin; + uint8_t min_grant; + uint8_t max_latency; +} PACKED; + +struct pci_header_type_1 { + struct pci_header_base base; + + paddr32_t bar0; + paddr32_t bar1; + + uint8_t primary_bus_number; + uint8_t secondary_bus_number; + uint8_t subordinate_bus_number; + uint8_t secondary_latency_timer; + + uint8_t io_base; + uint8_t io_limit; + uint16_t secondary_status; + + uint16_t memory_base; + uint16_t memory_limit; + + uint16_t prefetchable_memory_base; + uint16_t prefetchable_memory_limit; + + uint32_t prefetchable_base_upper; + uint32_t prefetchable_limit_upper; + + uint16_t io_base_upper; + uint16_t io_limit_upper; + + uint8_t capabilities_pointer; + uint8_t reserved[3]; + + uint32_t expansion_rom_base_address; + + uint8_t interrupt_line; + uint8_t interrupt_pin; + uint16_t bridge_control; +} PACKED; + void pci_init(); uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); +struct pci_device *pci_find_device(uint8_t class, uint8_t subclass); #endif /* _PCI_H_ */ diff --git a/include/kernel/common.h b/include/kernel/common.h index 2506e88..1010456 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -24,5 +24,6 @@ void yield(); typedef uintptr_t paddr_t; +typedef uint32_t paddr32_t; #endif /* _COMMON_H_ */ diff --git a/include/kernel/irq.h b/include/kernel/irq.h new file mode 100644 index 0000000..c066dd3 --- /dev/null +++ b/include/kernel/irq.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _IRQ_H_ +#define _IRQ_H_ + +#include + +int irq_map(uint8_t irq, uint8_t ist, void *func); +void irq_unmap(uint8_t irq); +int irq_highest_free(); + +#endif /* _IRQ_H_ */ diff --git a/init/irq.c b/init/irq.c new file mode 100644 index 0000000..40bb119 --- /dev/null +++ b/init/irq.c @@ -0,0 +1,140 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include +#include +#include + +#define exception(_i) _exception_##_i +#define exception_decl(_j) void exception(_j)() + +static bool irq_mapped[16] = { false }; + +exception_decl(0x00); +exception_decl(0x01); +exception_decl(0x02); +exception_decl(0x03); +exception_decl(0x04); +exception_decl(0x05); +exception_decl(0x06); +exception_decl(0x07); +exception_decl(0x08); +exception_decl(0x09); +exception_decl(0x0A); +exception_decl(0x0B); +exception_decl(0x0C); +exception_decl(0x0D); +exception_decl(0x0E); +exception_decl(0x0F); +exception_decl(0x10); +exception_decl(0x11); +exception_decl(0x12); +exception_decl(0x13); +exception_decl(0x14); +exception_decl(0x15); +exception_decl(0x16); +exception_decl(0x17); +exception_decl(0x18); +exception_decl(0x19); +exception_decl(0x1A); +exception_decl(0x1B); +exception_decl(0x1C); +exception_decl(0x1D); +exception_decl(0x1E); +exception_decl(0x1F); + +void idt_load(); /* descriptor.S */ +void isr_sched(); /* isr.S */ + +void sched_test() +{ + printf("a"); +} + +void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, void *func) +{ + uintptr_t f = (uintptr_t)(func); + struct irqdesc *irqdesc = (__idt + gate); + irqdesc->offset_low = f & 0xFFFF; + irqdesc->offset_mid = ((f >> 0x10) & 0xFFFF); + irqdesc->offset_high = ((f >> 0x20) & 0xFFFFFFFF); + + irqdesc->segment = segment; + irqdesc->ist = ist; + irqdesc->attrs = type; +} + +int irq_map(uint8_t irq, uint8_t ist, void *func) +{ + if (irq_mapped[irq]) + return 1; + + irq_mapped[irq] = true; + idt_insert(irq + 0x20, GATE_INTR, ist, GDT_SEGMENT_CODE_RING0, func); + pic_mask(irq, 0); + + return 0; +} + +void irq_unmap(uint8_t irq) +{ + irq_mapped[irq] = false; + idt_insert(irq + 0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, NULL); + pic_mask(irq, 1); +} + +int irq_highest_free() +{ + for (int i = 0; i < 16; i++) { + if (!irq_mapped[i]) + return i; + } + + return -1; +} + +void idt_init() +{ + idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00)); + idt_insert(0x01, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x01)); + idt_insert(0x02, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x02)); + idt_insert(0x03, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x03)); + idt_insert(0x04, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x04)); + idt_insert(0x05, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x05)); + idt_insert(0x06, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x06)); + idt_insert(0x07, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x07)); + idt_insert(0x08, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x08)); + idt_insert(0x09, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x09)); + idt_insert(0x0A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0A)); + idt_insert(0x0B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0B)); + idt_insert(0x0C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0C)); + idt_insert(0x0D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0D)); + idt_insert(0x0E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0E)); + idt_insert(0x0F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0F)); + idt_insert(0x10, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x10)); + idt_insert(0x11, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x11)); + idt_insert(0x12, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x12)); + idt_insert(0x13, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x13)); + idt_insert(0x14, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x14)); + idt_insert(0x15, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x15)); + idt_insert(0x16, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x16)); + idt_insert(0x17, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x17)); + idt_insert(0x18, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x18)); + idt_insert(0x19, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x19)); + idt_insert(0x1A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1A)); + idt_insert(0x1B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1B)); + idt_insert(0x1C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1C)); + idt_insert(0x1D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1D)); + idt_insert(0x1E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1E)); + idt_insert(0x1F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1F)); + + irq_map(4, 0, isr_serial_input); + + __idtr.size = (IDT_SIZE - 1); + __idtr.offset = (uint64_t)__idt; + idt_load(); + + printf(LOG_SUCCESS "IDT successfully loaded\n"); +}