x86: add GDT and IDT
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
f10e3f31d7
commit
e5b55a6ba5
25 changed files with 1211 additions and 13 deletions
|
|
@ -1,2 +1,4 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += acpi/
|
||||
obj-y += tty/
|
||||
obj-y += ahci.o
|
||||
|
|
|
|||
2
drivers/acpi/Makefile
Normal file
2
drivers/acpi/Makefile
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += acpi.o
|
||||
73
drivers/acpi/acpi.c
Normal file
73
drivers/acpi/acpi.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/requests.h>
|
||||
|
||||
#include <drivers/acpi.h>
|
||||
|
||||
#include <lib/string.h>
|
||||
|
||||
struct rsdp {
|
||||
/* REV 0 */
|
||||
char signature[8];
|
||||
u8 checksum;
|
||||
char oem_id[6];
|
||||
u8 revision;
|
||||
u32 rsdt_address;
|
||||
|
||||
/* REV: 2 */
|
||||
u32 length;
|
||||
u64 xsdt_address;
|
||||
u8 extended_checksum;
|
||||
u8 reserved[3];
|
||||
} PACKED;
|
||||
|
||||
struct rsdt *rsdt = NULL;
|
||||
struct xsdt *xsdt = NULL;
|
||||
|
||||
void *find_table(const char *signature)
|
||||
{
|
||||
u32 entries = (xsdt->header.length - sizeof(struct acpi_header)) / 8;
|
||||
for (u32 i = 0; i < entries; i++) {
|
||||
struct acpi_header *h;
|
||||
if (xsdt)
|
||||
h = (struct acpi_header *)TO_VIRT((xsdt->tables[i]));
|
||||
else
|
||||
h = (struct acpi_header *)TO_VIRT((rsdt->tables[i]));
|
||||
if (memcmp(h->signature, signature, 4) == 0)
|
||||
return h;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void acpi_load_pci()
|
||||
{
|
||||
struct acpi_mcfg *mcfg = find_table("MCFG");
|
||||
|
||||
u32 n_entries = (mcfg->header.length - sizeof(struct acpi_header) - 8) / sizeof(struct acpi_mcfg_entry);
|
||||
|
||||
for (u32 i = 0; i < n_entries; i++) {
|
||||
struct acpi_mcfg_entry *entry = &mcfg->entries[i];
|
||||
u64 pci_virt_base = TO_VIRT(entry->base_address);
|
||||
|
||||
kdebug("MCFG entry: base=%p, start_bus=%d,end_bus=%d,seg_group=%d\n", pci_virt_base, entry->start_bus_number,
|
||||
entry->end_bus_number, entry->pci_segment_group);
|
||||
}
|
||||
}
|
||||
|
||||
void acpi_init()
|
||||
{
|
||||
struct rsdp *rsdp;
|
||||
rsdp = kern_rsdp_res->address;
|
||||
if (kern_rsdp_res->revision == 3) {
|
||||
vaddr_t p = (paddr_t)rsdp | hhdm_start;
|
||||
rsdp = (void *)p;
|
||||
}
|
||||
|
||||
rsdt = (struct rsdt *)(rsdp->rsdt_address | hhdm_start);
|
||||
if (rsdp->revision == 2)
|
||||
xsdt = (struct xsdt *)(rsdp->xsdt_address | hhdm_start);
|
||||
|
||||
acpi_load_pci();
|
||||
}
|
||||
215
drivers/ahci.c
Normal file
215
drivers/ahci.c
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <lib/string.h>
|
||||
#include <lib/stdio.h>
|
||||
|
||||
#include <drivers/ahci.h>
|
||||
|
||||
static uint8_t sata_device_count = 0;
|
||||
static struct sata_device sata_devices[32] = { 0 };
|
||||
|
||||
static paddr_t ahci_pbase = 0;
|
||||
static vaddr_t ahci_vbase = 0;
|
||||
|
||||
static int ahci_port_type(hbaport_t *port)
|
||||
{
|
||||
uint32_t ssts = port->ssts;
|
||||
uint8_t det = ssts & 0x0F;
|
||||
|
||||
if (det != HBA_PORT_DET_PRESENT)
|
||||
return AHCI_DEV_NULL;
|
||||
|
||||
if (port->clb == 0)
|
||||
return AHCI_DEV_NULL;
|
||||
|
||||
switch (port->sig) {
|
||||
case SATA_SIG_ATAPI:
|
||||
return AHCI_DEV_SATAPI;
|
||||
case SATA_SIG_SEMB:
|
||||
return AHCI_DEV_SEMB;
|
||||
case SATA_SIG_PM:
|
||||
return AHCI_DEV_PM;
|
||||
default:
|
||||
return AHCI_DEV_SATA;
|
||||
}
|
||||
}
|
||||
|
||||
static void halt_cmd(hbaport_t *port)
|
||||
{
|
||||
port->cmd &= ~HBA_PORT_CMD_ST;
|
||||
port->cmd &= ~HBA_PORT_CMD_FRE;
|
||||
|
||||
while (port->cmd & (HBA_PORT_CMD_FR | HBA_PORT_CMD_CR))
|
||||
;
|
||||
}
|
||||
|
||||
static void start_cmd(hbaport_t *port)
|
||||
{
|
||||
while (port->cmd & HBA_PORT_CMD_CR)
|
||||
;
|
||||
|
||||
port->cmd |= HBA_PORT_CMD_FRE;
|
||||
port->cmd |= HBA_PORT_CMD_ST;
|
||||
}
|
||||
|
||||
static void ahci_rebase(hbaport_t *port, int n)
|
||||
{
|
||||
halt_cmd(port);
|
||||
|
||||
port->clb = (ahci_pbase) + (n << 10);
|
||||
memset((void *)(port->clb | hhdm_start), 0, 1024);
|
||||
|
||||
port->fb = ahci_pbase + (32 << 10) + (n << 8);
|
||||
memset((void *)(port->fb | hhdm_start), 0, 256);
|
||||
|
||||
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
cmdheader[i].prdt_len = 8;
|
||||
cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8);
|
||||
|
||||
void *ctba = (void *)((uintptr_t)cmdheader[i].cmd_table_base | hhdm_start);
|
||||
memset(ctba, 0, 256);
|
||||
}
|
||||
|
||||
start_cmd(port);
|
||||
}
|
||||
|
||||
static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
||||
{
|
||||
uint32_t pi = abar->pi;
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if (pi & 1) {
|
||||
uint32_t type = ahci_port_type(&abar->ports[i]);
|
||||
if (type == AHCI_DEV_SATA) {
|
||||
struct sata_device *sdev = &sata_devices[sata_device_count];
|
||||
sdev->port = i;
|
||||
sdev->controller = dev;
|
||||
sdev->abar = abar;
|
||||
|
||||
/* char *name = kzalloc(16, ALLOC_KERN);
|
||||
name[15] = 0;
|
||||
|
||||
snprintf(name, "ahci%d", 15, sata_device_count);*/
|
||||
|
||||
size_t sector_count = ((size_t)abar->ports[i].ssts) << 32 | abar->ports[i].ssts;
|
||||
sdev->sector_count = sector_count;
|
||||
|
||||
sdev->sector_size = 512;
|
||||
|
||||
/* bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size);
|
||||
block_gpt_init(bdev);*/
|
||||
|
||||
ahci_rebase(&abar->ports[i], i);
|
||||
sata_device_count++;
|
||||
kprintf(LOG_INFO "SATA drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_SATAPI) {
|
||||
kprintf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_SEMB) {
|
||||
kprintf(LOG_INFO "SEMB drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_PM) {
|
||||
kprintf(LOG_INFO "PM drive found at port %d\n", i);
|
||||
}
|
||||
}
|
||||
pi >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int cmdslot(hbaport_t *port)
|
||||
{
|
||||
uint32_t slots = (port->sact | port->ci);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if ((slots & 1))
|
||||
slots >>= 1;
|
||||
else
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int 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];
|
||||
|
||||
/* clear pending interrupt */
|
||||
port->is = (uint32_t)-1;
|
||||
|
||||
int spin = 0;
|
||||
int slot = cmdslot(port);
|
||||
if (slot == -1)
|
||||
return false;
|
||||
|
||||
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
|
||||
cmdheader += slot;
|
||||
cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
|
||||
cmdheader->write = 0;
|
||||
cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1;
|
||||
|
||||
struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start);
|
||||
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
|
||||
|
||||
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
|
||||
cmdfis->fis_type = FIS_REG_H2D;
|
||||
cmdfis->cmd_mode = 1;
|
||||
cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX;
|
||||
|
||||
cmdfis->lba0 = (uint8_t)lba;
|
||||
cmdfis->lba1 = (uint8_t)(lba >> 8);
|
||||
cmdfis->lba2 = (uint8_t)(lba >> 16);
|
||||
cmdfis->device = 1 << 6; /* LBA mode */
|
||||
cmdfis->lba3 = (uint8_t)(lba >> 24);
|
||||
cmdfis->lba4 = (uint8_t)(lba >> 32);
|
||||
cmdfis->lba5 = (uint8_t)(lba >> 40);
|
||||
|
||||
cmdfis->count_low = count & 0xff;
|
||||
cmdfis->count_high = (count >> 8) & 0xff;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < cmdheader->prdt_len; i++) {
|
||||
cmdtbl->prdt_entry[i].data_base = buf;
|
||||
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
|
||||
cmdtbl->prdt_entry[i].interrupt = 1;
|
||||
buf += 8 * 1024;
|
||||
count -= 16;
|
||||
}
|
||||
|
||||
/* wait until port is free */
|
||||
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) {
|
||||
spin++;
|
||||
}
|
||||
|
||||
if (spin == 1000000) {
|
||||
kprintf(LOG_ERROR "AHCI: Port is hung\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* issue command */
|
||||
port->ci = 1 << slot;
|
||||
|
||||
/* wait for completion */
|
||||
while (1) {
|
||||
if (port->is & HBA_PORT_IS_TFES) {
|
||||
kprintf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((port->ci & (1 << slot)) == 0)
|
||||
break;
|
||||
/* ELSE IF: reschedule */
|
||||
}
|
||||
|
||||
/* check again */
|
||||
if (port->is & HBA_PORT_IS_TFES) {
|
||||
kprintf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ahci_init()
|
||||
{
|
||||
}
|
||||
1
drivers/pci/pci.c
Normal file
1
drivers/pci/pci.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
Loading…
Reference in a new issue