diff --git a/dev/ahci.c b/dev/ahci.c index 20be31d..871f29d 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include "stdio.h" #include +#include #include +#include #include #include @@ -15,6 +17,7 @@ static paddr_t ahci_pbase = 0; static void ahci_irq_handler() { + printf(LOG_ERROR "AHCI INTERRUPT\n"); } static int ahci_port_type(hbaport_t *port) @@ -110,14 +113,16 @@ static int cmdslot(hbaport_t *port) { uint32_t slots = (port->sact | port->ci); for (int i = 0; i < 32; i++) { - if ((slots & 1) == 0) + if ((slots & 1)) + slots >>= 1; + else return i; - slots >>= 1; } + return -1; } -bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf) +bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) { hbaport_t *port = &dev->abar->ports[dev->port]; @@ -154,7 +159,7 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis); cmdfis->fis_type = FIS_REG_H2D; cmdfis->cmd_mode = 1; - cmdfis->command = ATA_CMD_READ_DMA_EX; + cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX; cmdfis->lba0 = (uint8_t)lba; cmdfis->lba1 = (uint8_t)(lba >> 8); @@ -177,8 +182,10 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad return false; } + /* issue command */ port->ci = 1 << slot; + /* wait for completion */ while (1) { if ((port->ci & (1 << slot)) == 0) break; @@ -188,7 +195,8 @@ bool ahci_read_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pad } } - if(port->is & HBA_PORT_IS_TFES) { + /* check again */ + if (port->is & HBA_PORT_IS_TFES) { printf(LOG_ERROR "AHCI: Read disk error\n"); return false; } @@ -201,18 +209,35 @@ void ahci_init() struct pci_device *dev = pci_find_device(0x01, 0x06); /* check interrupt number */ if (dev == NULL) { - printf(LOG_ERROR "Failed to find AHCI device\n"); + printf(LOG_WARN "No SATA controllers found\n"); return; } abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start); if (!(abar->ghc & (1 << 31))) { - printf(LOG_ERROR "AHCI controller not enabled\n"); + printf(LOG_WARN "AHCI controller not enabled\n"); return; } + /* enable AHCI interrupts */ + abar->ghc |= AHCI_GHC_IE; + abar->ghc |= AHCI_GHC_AE; + + size_t ahci_zone_size = 0x100000; + ahci_pbase = (paddr_t)buddy_alloc(ahci_zone_size) & (~hhdm_start); /* 1 MiB */ ahci_probe_ports(dev, abar); - ahci_pbase = (paddr_t)buddy_alloc(0x100000); /* 1 MiB */ + + /* map ahci zone with cache disabled (pcd=1) */ + struct page attrs; + attrs.val = 1; + attrs.rw = 1; + attrs.pcd = 1; + kmap(ahci_pbase, ahci_pbase | hhdm_start, ahci_zone_size, attrs.val); + + if (sata_device_count == 0) { + printf(LOG_WARN "No SATA devices found\n"); + return; + } /* setup device irq number */ uint16_t irq = irq_highest_free(); @@ -222,7 +247,13 @@ void ahci_init() pci_config_write_long(dev->bus, dev->slot, dev->func, 0x3C, tmp); irq_map(irq, ahci_irq_handler); - tmp = pci_config_read_long(dev->bus, dev->slot, dev->func, 0x3C); + char *buf = kmalloc(512); + ahci_access_sectors(&sata_devices[1], 0, 1, (paddr_t)buf & (~hhdm_start), false); + buf[511] = 0; + printf("AHCI: %s\n", buf); + + memcpy(buf, "Hello, World!", 13); + ahci_access_sectors(&sata_devices[1], 0, 1, (paddr_t)buf & (~hhdm_start), true); printf(LOG_SUCCESS "AHCI controller ready\n"); } diff --git a/include/dev/ahci.h b/include/dev/ahci.h index b79c792..27b15cc 100644 --- a/include/dev/ahci.h +++ b/include/dev/ahci.h @@ -19,6 +19,7 @@ #define ATA_DEV_DRQ 0x08 #define ATA_CMD_READ_DMA_EX 0x25 +#define ATA_CMD_WRITE_DMA_EX 0x35 #define ATA_CMD_IDENT 0xEC #define SATA_SIG_ATA 0x00000101 @@ -32,6 +33,10 @@ #define AHCI_DEV_PM 3 #define AHCI_DEV_SATAPI 4 +#define AHCI_GHC_ENABLE 0x1 +#define AHCI_GHC_IE 0x2 +#define AHCI_GHC_AE 0x80000000 + #define HBA_PORT_IPM_ACTIVE 0x1 #define HBA_PORT_DET_PRESENT 0x3 diff --git a/include/kernel/mem.h b/include/kernel/mem.h index f41d53c..b8f9729 100644 --- a/include/kernel/mem.h +++ b/include/kernel/mem.h @@ -80,6 +80,7 @@ extern size_t num_regions; extern size_t max_regions; extern struct page *pml4; +extern paddr_t kcr3; uint64_t cr3_read(); void cr3_write(uint64_t cr3); diff --git a/init/irq.c b/init/irq.c index 1c0b741..08363e2 100644 --- a/init/irq.c +++ b/init/irq.c @@ -104,7 +104,7 @@ void irq_unmap(uint8_t irq) int irq_highest_free() { - for (int i = 15; i >= 0; i--) { + for (int i = 14; i >= 0; i--) { if (!irq_mapped[i]) return i; } diff --git a/init/main.c b/init/main.c index 38f2403..c02a549 100644 --- a/init/main.c +++ b/init/main.c @@ -13,6 +13,7 @@ #include #include +#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) #include #define KSTACK_SIZE 0x4000 @@ -20,6 +21,12 @@ void stack_init(uintptr_t rsp, uintptr_t rbp); struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 }; +struct limine_kernel_file_request module_req = { .id = LIMINE_KERNEL_FILE_REQUEST, .revision = 0 }; + +static char *kcmdline; +static char *kpath; +static struct limine_uuid kroot_disk; +static struct limine_uuid kroot_part; uintptr_t hhdm_start; uintptr_t data_end; @@ -43,7 +50,6 @@ static inline void _pic_init() while (!console_ready()) ; - iowait(); } void kmain(void) @@ -63,6 +69,24 @@ void kmain(void) mem_early_init(kmem, one_gb); kmalloc_init(); + struct limine_kernel_file_response *resp = module_req.response; + + struct limine_file *kfile = (struct limine_file *)((paddr_t)resp->kernel_file | hhdm_start); + + kcmdline = kzalloc(strlen(kfile->cmdline) + 1); + strcpy(kcmdline, kfile->cmdline); + + kpath = kzalloc(strlen(kfile->path) + 1); + strcpy(kpath, kfile->path); + + kroot_disk = kfile->gpt_disk_uuid; + kroot_part = kfile->gpt_part_uuid; + +#ifdef KDEBUG + printf(LOG_DEBUG "Kernel file: path='%s' cmdline='%s'\n", kpath, kcmdline); + printf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b); +#endif /* KDEBUG */ + console_init(); acpi_init(); diff --git a/mem/buddy.c b/mem/buddy.c index 8458e54..f7c4178 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -21,6 +21,8 @@ struct page *pml4; struct mem_region *mem_regions; size_t num_regions; +paddr_t kcr3 = 0; + static void *(*alloc_page)(void); static struct slab *kmap_slab; @@ -431,6 +433,7 @@ void mem_early_init(char *mem, size_t len) pml4_paddr = (uintptr_t)pml4; pml4_paddr &= (~hhdm_start); cr3.pml4_addr = pml4_paddr >> 12; + kcr3 = cr3.val; cr3_write(cr3.val); /* done */