Device IRQs WIP

This commit is contained in:
2026-03-12 19:23:47 +01:00
parent 04b7355a3d
commit 19793e9126
29 changed files with 420 additions and 187 deletions

View File

@@ -1,13 +1,18 @@
#include <amd64/io.h>
#include <amd64/intr_defs.h>
#include <amd64/apic.h>
#include <device/device.h>
#include <device/idedrv.h>
#include <device/partitions.h>
#include <devices.h>
#include <libk/std.h>
#include <libk/list.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <status.h>
#include <irq/irq.h>
#define IDE_REG_DATA 0x00
#define IDE_REG_ERROR 0x01
@@ -78,6 +83,75 @@ static void ide_delay (uint16_t ctrl) {
}
#pragma clang optimize on
static void ide_flush (struct idedrv* idedrv) {
if (idedrv->lba48)
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH48);
else
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH28);
}
static void ide_irq (void* arg, void* regs, struct reschedule_ctx* rctx) {
struct idedrv* idedrv = arg;
spin_lock (&idedrv->device->lock);
struct list_node_link* node = idedrv->requests;
if (node == NULL) {
(void)inb (idedrv->io + IDE_REG_STATUS);
spin_unlock (&idedrv->device->lock);
return;
}
struct idedrv_request* req = list_entry (node, struct idedrv_request, requests_link);
struct list_node_link* sqlist_node = req->sq.proc_list;
struct proc_sq_entry* sq_entry = list_entry (sqlist_node, struct proc_sq_entry, sq_link);
struct proc* resumed_proc = sq_entry->proc;
uint8_t status = inb (idedrv->io + IDE_REG_STATUS);
if ((status & (IDE_ERR | IDE_DRQ))) {
list_remove (idedrv->requests, &req->requests_link);
spin_unlock (&idedrv->device->lock);
proc_sq_resume (resumed_proc, sq_entry, rctx);
free (req);
return;
}
spin_lock (&resumed_proc->lock);
if (req->rw == 1) {
if ((status & IDE_DRQ)) {
uint16_t* p = req->outbuffer + (req->sector_done_count * (idedrv->sector_size / 2));
insw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
req->sector_done_count++;
}
} else if (req->rw == 2) {
req->sector_done_count++;
if (req->sector_done_count < req->sector_count) {
uint16_t* p = req->outbuffer + (req->sector_done_count * (idedrv->sector_size / 2));
outsw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
}
}
if (req->sector_done_count >= req->sector_count) {
if (req->rw == 2)
ide_flush (idedrv);
list_remove (idedrv->requests, &req->requests_link);
spin_unlock (&resumed_proc->lock);
spin_unlock (&idedrv->device->lock);
free (req);
proc_sq_resume (resumed_proc, sq_entry, rctx);
return;
}
spin_unlock (&resumed_proc->lock);
spin_unlock (&idedrv->device->lock);
}
void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* probe) {
probe->flags = 0;
probe->sector_count = 0;
@@ -141,7 +215,10 @@ void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* pro
probe->sector_size = 512;
}
static void ide_prepare (struct idedrv* idedrv, size_t sector, uint16_t sector_count) {
static void ide_prepare (struct idedrv* idedrv, size_t sector, uint16_t sector_count, bool clear) {
if (clear)
outb (idedrv->ctrl, 0x00);
if (idedrv->lba48) {
outb (idedrv->io + IDE_REG_DRIVE, 0x40 | (idedrv->devno << 4));
ide_delay (idedrv->ctrl);
@@ -167,7 +244,9 @@ static void ide_prepare (struct idedrv* idedrv, size_t sector, uint16_t sector_c
}
}
bool idedrv_init (struct device* device, void* arg) {
bool idedrv_init (struct device* device, void* arg, struct proc* proc, struct reschedule_ctx* rctx) {
(void)proc, (void)rctx;
struct idedrv_init* init = arg;
struct idedrv* idedrv = malloc (sizeof (*idedrv));
@@ -175,23 +254,41 @@ bool idedrv_init (struct device* device, void* arg) {
if (idedrv == NULL)
return false;
idedrv->device = device;
idedrv->lba48 = init->lba48;
idedrv->sector_count = init->sector_count;
idedrv->sector_size = init->sector_size;
idedrv->io = init->io;
idedrv->ctrl = init->ctrl;
idedrv->devno = init->devno;
idedrv->primscnd = init->primscnd;
device->udata = idedrv;
device_probe_partitions (device);
if (idedrv->primscnd == 1) {
ioapic_route_irq (IDE_DRIVE_PRIM, 14, 0, thiscpu->lapic_id);
irq_attach (&ide_irq, idedrv, IDE_DRIVE_PRIM);
} else if (idedrv->primscnd == 2) {
ioapic_route_irq (IDE_DRIVE_SCND, 15, 0, thiscpu->lapic_id);
irq_attach (&ide_irq, idedrv, IDE_DRIVE_SCND);
}
return true;
}
void idedrv_fini (struct device* device) {
void idedrv_fini (struct device* device, struct proc* proc, struct reschedule_ctx* rctx) {
struct idedrv* idedrv = device->udata;
struct list_node_link* req_link, *tmp_req_link;
list_foreach (idedrv->requests, req_link, tmp_req_link) {
struct idedrv_request* req = list_entry (req_link, struct idedrv_request, requests_link);
list_remove (idedrv->requests, &req->requests_link);
struct proc_sq_entry* sq_entry = list_entry (req->sq.proc_list, struct proc_sq_entry, sq_link);
proc_sq_resume (proc, sq_entry, rctx);
free (req);
}
irq_detach (idedrv->primscnd == 1 ? IDE_DRIVE_PRIM : IDE_DRIVE_SCND);
free (idedrv);
}
@@ -211,20 +308,47 @@ int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx
if (sector + sector_count > idedrv->sector_count)
return -ST_OOB_ERROR;
ide_prepare (idedrv, sector, sector_count);
spin_lock (&proc->lock);
bool is_kproc = (proc->flags & PROC_KPROC) != 0;
spin_unlock (&proc->lock);
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
outb (idedrv->io + IDE_REG_CMD, cmd);
/* /1* polling *1/ */
/* if (is_kproc) { */
ide_prepare (idedrv, sector, sector_count, false);
for (uint16_t s = 0; s < sector_count; s++) {
if (!ide_wait (idedrv->io, 100000, true, true))
return -ST_XDRV_READ_ERROR;
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
outb (idedrv->io + IDE_REG_CMD, cmd);
insw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
idedrv->sector_size / 2);
}
for (uint16_t s = 0; s < sector_count; s++) {
if (!ide_wait (idedrv->io, 100000, true, true))
return -ST_XDRV_READ_ERROR;
return ST_OK;
insw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)), (idedrv->sector_size / 2));
}
return ST_OK;
/* } else { /1* IRQ *1/ */
/* struct idedrv_request* req = malloc (sizeof (*req)); */
/* if (req == NULL) */
/* return -ST_OOM_ERROR; */
/* memset (req, 0, sizeof (*req)); */
/* req->outbuffer = buffer; */
/* req->sector_count = sector_count; */
/* req->sector_done_count = 0; */
/* req->rw = 1; */
/* list_append (idedrv->requests, &req->requests_link); */
/* proc_sq_suspend (proc, &req->sq, NULL, rctx); */
/* ide_prepare (idedrv, sector, sector_count, true); */
/* uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28; */
/* outb (idedrv->io + IDE_REG_CMD, cmd); */
/* return ST_OK; */
/* } */
}
int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
@@ -242,25 +366,58 @@ int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ct
if (sector + sector_count > idedrv->sector_count)
return -ST_OOB_ERROR;
spin_lock (&proc->lock);
bool is_kproc = (proc->flags & PROC_KPROC) != 0;
spin_unlock (&proc->lock);
ide_prepare (idedrv, sector, sector_count);
/* /1* polling *1/ */
/* if (is_kproc) { */
ide_prepare (idedrv, sector, sector_count, false);
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
outb (idedrv->io + IDE_REG_CMD, cmd);
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
outb (idedrv->io + IDE_REG_CMD, cmd);
for (uint16_t s = 0; s < sector_count; s++) {
if (!ide_wait (idedrv->io, 100000, true, true))
return -ST_XDRV_WRITE_ERROR;
for (uint16_t s = 0; s < sector_count; s++) {
if (!ide_wait (idedrv->io, 100000, true, true))
return -ST_XDRV_WRITE_ERROR;
outsw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
idedrv->sector_size / 2);
}
outsw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)), (idedrv->sector_size / 2));
}
cmd = idedrv->lba48 ? IDE_CMD_FLUSH48 : IDE_CMD_FLUSH28;
outb (idedrv->io + IDE_REG_CMD, cmd);
ide_wait (idedrv->io, 100000, false, true);
return ST_OK;
/* } else { /1* IRQ *1/ */
/* struct idedrv_request* req = malloc (sizeof (*req)); */
return ST_OK;
/* if (req == NULL) */
/* return -ST_OOM_ERROR; */
/* memset (req, 0, sizeof (*req)); */
/* req->outbuffer = buffer; */
/* req->sector_count = sector_count; */
/* req->sector_done_count = 0; */
/* req->rw = 2; */
/* list_append (idedrv->requests, &req->requests_link); */
/* proc_sq_suspend (proc, &req->sq, NULL, rctx); */
/* ide_prepare (idedrv, sector, sector_count, true); */
/* uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28; */
/* outb (idedrv->io + IDE_REG_CMD, cmd); */
/* if (!ide_wait (idedrv->io, 100000, true, true)) { */
/* list_remove (idedrv->requests, &req->requests_link); */
/* struct proc_sq_entry* sq_entry = list_entry (req->sq.proc_list, struct proc_sq_entry, sq_link); */
/* proc_sq_resume (proc, sq_entry, rctx); */
/* free (req); */
/* return -ST_XDRV_WRITE_ERROR; */
/* } */
/* outsw (idedrv->io + IDE_REG_DATA, buffer, idedrv->sector_size / 2); */
/* return ST_OK; */
/* } */
}
int idedrv_get_device_type (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,