Selaa lähdekoodia

9.808u-mods: working ide driver, and fixed a bug of do_irq

rain 5 vuotta sitten
vanhempi
sitoutus
90d52d8588

+ 1 - 1
LINES

@@ -40,4 +40,4 @@
 2021-8-18 9.106: 9106 lines, 59 files, rain
 2021-8-19 9.085: 9085 lines, 59 files, rain
 2021-8-21 9.389: 9389 lines, 59 files, rain
-
+2021-8-22 9.808: 9808 lines, 63 files, rain

+ 2 - 2
Makefile

@@ -3,8 +3,8 @@ BOOT_SRCS = boot/boot.asm boot/loader.asm
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c kernel/rlib/fifo.c 
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o kernel/rlib/fifo.o 
-MODULE_SRCS = modules/ahci.c 
-MODULE_OBJS = modules/ahci.o 
+MODULE_SRCS = modules/ahci.c modules/ide.c 
+MODULE_OBJS = modules/ahci.o modules/ide.o 
 KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o kernel/tty.o kernel/console.o $(RLIB_OBJS) $(MODULE_OBJS) 
 KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c kernel/tty.c kernel/console.c $(RLIB_SRCS) $(MODULE_SRCS) 
 

+ 5 - 0
README.md

@@ -160,3 +160,8 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 
 9.389u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working............
+
+9.808u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;now ide driver is working, and we fixed a bug about the irq: 
+&nbsp;&nbsp;&nbsp;&nbsp;that the slave-irq's EOI not send. 
+

+ 2 - 0
include/arch/io.h

@@ -30,6 +30,7 @@
 
 
 typedef __uint16_t port_t;
+typedef port_t __io_t;
 
 
 #define inb(port) ({ \
@@ -51,6 +52,7 @@ typedef __uint16_t port_t;
 })
 
 #define insw(port, buf, c) __asm__ volatile ("cld;rep;insw;"::"d" (port), "D" (buf), "c" (c))
+#define outsw(port, buf, c) __asm__ volatile ("cld;rep;outsw;" :: "d" (port), "D" (buf), "c" (c))
 
 
 #define outb(value, port) \

+ 1 - 1
include/arch/x86/int.h

@@ -70,8 +70,8 @@ void divide_error(void);
 #define PIC1_ICW1 0x00a0
 #define PIC1_OCW2 0x00a0
 
-
 #define PIC1_IMR  0x00a1
+
 #define PIC1_ICW2 0x00a1
 #define PIC1_ICW3 0x00a1
 #define PIC1_ICW4 0x00a1

+ 63 - 0
include/kernel/block.h

@@ -0,0 +1,63 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#ifndef _BLOCK_H
+#define _BLOCK_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <kernel/sched.h>
+
+
+struct req_node {
+	struct blkdev *dev;
+
+	char *buffer;
+	struct task *waiting;
+};
+
+
+struct blkdev {
+	__uint8_t pci_class;
+	__uint8_t pci_subclass;
+
+#define DEVMAJOR_MM 	0
+#define DEVMAJOR_HD 	1
+#define DEVMAJOR_UNK 	2
+
+	__uint8_t dev;
+
+#define BLKNIL 		0
+#define BLKREAD 	1
+#define BLKWRITE 	2
+
+	int (* send_cmd) (int cmd, struct req_node *node);
+};
+
+
+#endif
+

+ 2 - 2
include/kernel/init.h

@@ -34,14 +34,14 @@
 
 /* fastest, but no syslog and schk */
 
-// #define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
+#define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
 
 /* standard */
 
 // #define DEBUG_LEVEL		DEV_DEBUG_LEVEL
 
 /* slowest: schk will display function info in this mode */
-#define DEBUG_LEVEL		TEST_DEBUG_LEVEL
+// #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
 /* how many we should put on the screen (functions name) */
 #define TRACE_SYSLOG_NR 	8

+ 16 - 1
include/kernel/int.h

@@ -57,7 +57,22 @@ void machine(void);
 void simd(void);
 void virtualization(void);
 
-
+void irq0x20_interrupt(void);
+void irq0x21_interrupt(void);
+void irq0x22_interrupt(void);
+void irq0x23_interrupt(void);
+void irq0x24_interrupt(void);
+void irq0x25_interrupt(void);
+void irq0x26_interrupt(void);
+void irq0x27_interrupt(void);
+void irq0x28_interrupt(void);
+void irq0x29_interrupt(void);
+void irq0x2a_interrupt(void);
+void irq0x2b_interrupt(void);
+void irq0x2c_interrupt(void);
+void irq0x2d_interrupt(void);
+void irq0x2e_interrupt(void);
+void irq0x2f_interrupt(void);
 
 void register_irq(int, void *, const void *);
 void unregister_irq(int);

+ 2 - 0
include/kernel/pci.h

@@ -213,6 +213,8 @@ struct pci_vendor_table {
 #define CLASS_FPU 	0x40
 #define CLASS_NOCLASS 	0xff
 
+#define PCI_IO_MASK 	(~1ul)
+
 
 #endif
 

+ 3 - 1
include/kernel/pci_vendors.h

@@ -43,10 +43,12 @@
 #define foo(fn) extern errno_t fn (struct pci_header0 *)
 
 foo(ahci_init);
+foo(ide_init);
 
 
 struct pci_vendor_table pci_vendors[] = {
-	{CLASS_DISK, 0x06, ahci_init}, 
+	{CLASS_DISK, 0x06, ahci_init},
+	{CLASS_DISK, 0x01, ide_init}, 
 	{0xff, 0xff, NULL}
 };
 

+ 2 - 23
include/modules/ahci.h

@@ -27,6 +27,8 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
+#include <modules/ata.h>
+
 
 /* i tried to keep this as simple as i can... */
 
@@ -185,29 +187,6 @@ struct fis_h2d {
 
 #define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
 
-#define ATACMD_ID 	0xec
-
-#define ATACMD_RD 	0x20
-#define ATACMD_RD48 	0x24
-
-#define ATACMD_WR 	0x30
-#define ATACMD_WR48 	0x34
-
-#define ATASTAT_READY 	0x40
-#define ATASTAT_ERR 	0x01
-#define ATASTAT_BUSY 	0x80
-
-#define ATACTL_INTDIS 	0x02
-#define ATACTL_RESET 	0x04
-
-#define ATAERR_OTHER 	0x01
-#define ATAERR_TRACK0	0x02
-#define ATAERR_ABORT 	0x04
-#define ATAERR_NOTFOUND	0x10
-#define ATAERR_DATAERR 	0x40
-#define ATAERR_BADSECT 	0x80
-
-
 extern volatile struct ahci_hba_mem *hba;
 
 int ahci_rw_sect(volatile struct ahci_hba_port *, int, char *, __uint64_t);

+ 53 - 0
include/modules/ata.h

@@ -0,0 +1,53 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+
+#define ATACMD_ID 	0xec
+
+#define ATACMD_RD 	0x20
+#define ATACMD_RD48 	0x24
+
+#define ATACMD_WR 	0x30
+#define ATACMD_WR48 	0x34
+
+#define ATASTAT_READY 	0x40
+#define ATASTAT_REQ 	0x08
+#define ATASTAT_ERR 	0x01
+#define ATASTAT_BUSY 	0x80
+
+#define ATACTL_INTDIS 	0x02
+#define ATACTL_RESET 	0x04
+
+#define ATAERR_OTHER 	0x01
+#define ATAERR_TRACK0	0x02
+#define ATAERR_ABORT 	0x04
+#define ATAERR_NOTFOUND	0x10
+#define ATAERR_DATAERR 	0x40
+#define ATAERR_BADSECT 	0x80
+

+ 63 - 0
include/modules/ide.h

@@ -0,0 +1,63 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <modules/ata.h>
+
+#define ide_info(fmt, ...) printk(yellow, "ide: " fmt, ##__VA_ARGS__);
+
+/* 0: PIO mode */
+#define PROGIF_HD0MODE 		0x01
+
+/* 1: can change the pio mode to pci mode */
+#define PROGIF_HD0CHANGABLE  	0x02
+
+#define PROGIF_HD1MODE 		0x04
+#define PROGIF_HD1CHANGABLE 	0x08
+
+#define PROGIF_DMA 		0x80
+
+#define PORT_DATA 		0
+#define PORT_ERR 		1
+#define PORT_NSECTORS 		2
+#define PORT_ADDR0		3
+#define PORT_ADDR1 		4
+#define PORT_ADDR2 		5
+#define PORT_DEV 		6
+#define PORT_STAT 		7
+
+void do_ide_intr(void);
+void do_ide_id(void);
+void do_ide_read(void);
+
+int ide_cmdout(int, int, char *, int, __uint64_t);
+
+
+#define wait_disk_not_busy(port) 	while (inb(port + PORT_STAT) & ATASTAT_BUSY);
+#define wait_disk_ready(port) 		while (!(inb(port + PORT_STAT) & ATASTAT_READY));
+

+ 0 - 3
init/main.c

@@ -93,9 +93,6 @@ int init(void) {
 
 	printk(white, "total %d devices. \n", pci_lspci());
 
-	trace_irq();
-	trace_syslog();
-
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
 	if (bootarg->bootdev & 0x80) 

+ 0 - 4
kernel/int.c

@@ -39,10 +39,6 @@
 #include <arch/sys.h>
 
 
-build_irq(0x20);
-build_irq(0x21);
-
-
 int int_init(void) {
 	schk_init();
 

+ 27 - 1
kernel/irq.c

@@ -37,6 +37,24 @@
 #include <arch/sys.h>
 
 
+build_irq(0x20);
+build_irq(0x21);
+build_irq(0x22);
+build_irq(0x23);
+build_irq(0x24);
+build_irq(0x25);
+build_irq(0x26);
+build_irq(0x27);
+build_irq(0x28);
+build_irq(0x29);
+build_irq(0x2a);
+build_irq(0x2b);
+build_irq(0x2c);
+build_irq(0x2d);
+build_irq(0x2e);
+build_irq(0x2f);
+
+
 struct irq_entry irq_table[MAX_IRQ_NR];
 
 
@@ -104,7 +122,15 @@ void do_irq(const __uint64_t *rsp, __uint64_t nr) {
 	else 
 		printk(red, "unknow irq %x\n", nr - 0x20);
 
-	outb(0x20, 0x20);
+	if (nr > 0x27) {
+		/* slave-irq ok */
+		outb(0x62, PIC0_OCW2);
+		/* this.irqnr ok */
+		outb(0x60 - 0x28 + nr, PIC1_OCW2);
+	}
+
+	else 
+		outb(0x60 - 0x20 + nr, PIC0_OCW2);
 
 	schk_leave();
 

+ 1 - 1
modules/Makefile

@@ -1,4 +1,4 @@
-OBJS = ahci.o 
+OBJS = ahci.o ide.o  
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 all: $(OBJS)

+ 0 - 14
modules/ahci.c

@@ -41,12 +41,6 @@
 
 
 volatile struct ahci_hba_mem *hba;
-static char buffer[512] = {0};
-
-
-build_irq(0x2a);
-build_irq(0x2e);
-build_irq(0x2f);
 
 
 errno_t ahci_init(struct pci_header0 *hdr) {
@@ -78,8 +72,6 @@ errno_t ahci_init(struct pci_header0 *hdr) {
 
 	hba->global_ctl = 0x80000002;
 
-	mfence();
-
 	ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
 
 	int i, nrdevs = 0;
@@ -111,13 +103,9 @@ errno_t ahci_init(struct pci_header0 *hdr) {
 	hba->ports[0].cmd = hba->ports[0].cmd | 0x10000003;
 	hba->ports[0].sata_ctl = 0x04;
 
-	mfence();
-
 	ahci_info("SATA error: %x. \n", hba->ports[0].sata_error);
 	ahci_info("interrupt status: %x\n", hba->ports[0].int_status);
 
-	ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 2);
-
 	schk_leave();
 
 	return 0;
@@ -209,8 +197,6 @@ int ahci_rw_sect(volatile struct ahci_hba_port *port, int atacmd, char *buffer,
 
 	/* interrupt enable */
 	port->sata_ctl = 0x00;
-	
-	mfence();
 
 	/* wait it complete */
 

+ 235 - 0
modules/ide.c

@@ -0,0 +1,235 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/console.h>
+#include <kernel/pci.h>
+#include <kernel/mm.h>
+
+#include <kernel/int.h>
+#include <kernel/panic.h>
+#include <kernel/schk.h>
+
+#include <kernel/block.h>
+#include <arch/x86/paging.h>
+#include <arch/io.h>
+
+#include <lib/string.h>
+#include <modules/ide.h>
+
+
+void (* ide_intr) (void) = NULL;
+
+#define HD0 	1
+#define HD1 	2
+
+int intr_dev = 0;
+
+__io_t hda_base, hda_ctl;
+__io_t hdb_base, hdb_ctl;
+
+static char buffer[512] = {0};
+
+__uint64_t hda_nsectors, hdb_nsectors;
+
+
+errno_t ide_init(struct pci_header0 *hdr) {
+	schk_init();
+
+	__io_t hd0_base, hd0_ctl;
+	__io_t hd1_base, hd1_ctl;
+
+	ide_info("PCI BAR0: \t%q\n", hdr->bar0);
+	ide_info("PCI BAR1: \t%q\n", hdr->bar1);
+	ide_info("PCI BAR2: \t%q\n", hdr->bar2);
+	ide_info("PCI BAR3: \t%q\n", hdr->bar3);
+	ide_info("PCI BAR4: \t%q\n", hdr->bar4);
+	ide_info("PCI BAR5: \t%q\n", hdr->bar5);
+
+	ide_info("irq nr: \t%b\n", hdr->irqnr);
+	ide_info("program interface: %q\n", hdr->hdr.prog_if);
+
+	if (hdr->hdr.prog_if & PROGIF_HD0MODE) {
+		hd0_base = hdr->bar0 & PCI_IO_MASK;
+		hd0_ctl = hdr->bar1 & PCI_IO_MASK;
+	}
+
+	else {
+		hd0_base = 0x1f0;
+		hd0_ctl = 0x3f6;
+	}
+
+	if (hdr->hdr.prog_if & PROGIF_HD1MODE) {
+		hd1_base = hdr->bar2 & PCI_IO_MASK;
+		hd1_ctl = hdr->bar3 & PCI_IO_MASK;
+	}
+
+	else {
+		hd1_base = 0x170;
+		hd1_ctl = 0x376;
+	}
+
+	hda_base = hd0_base;
+	hda_ctl = hd0_ctl;
+
+	hdb_base = hd1_base;
+	hdb_ctl = hd1_ctl;
+
+	ide_info("hda port: %w  hda control port: %w\n", hd0_base, hd0_ctl);
+	ide_info("hdb port: %w  hdb control port: %w\n", hd1_base, hd1_ctl);
+
+	register_irq(0x0e, irq0x2e_interrupt, do_ide_intr);
+	register_irq(0x0f, irq0x2f_interrupt, do_ide_intr);
+
+	ide_intr = do_ide_id;
+	intr_dev = HD0;
+
+	outb(0x00, hd0_ctl);
+	
+	outb(0x00, hd0_base + PORT_ERR);
+	outb(0x00, hd0_base + PORT_NSECTORS);
+
+	outb(0x00, hd0_base + PORT_ADDR0);
+	outb(0x00, hd0_base + PORT_ADDR1);
+	outb(0x00, hd0_base + PORT_ADDR2);
+
+	outb(0x00, hd0_base + PORT_DEV);
+	outb(ATACMD_ID, hd0_base + PORT_STAT);
+
+	while (ide_intr) hlt();
+
+	ide_info("ide_init: id completed. \n");
+
+	ide_intr = do_ide_id;
+	intr_dev = HD1;
+
+	outb(0x00, hd1_ctl);
+
+	outb(0x00, hd1_base + PORT_ERR);
+	outb(0x00, hd1_base + PORT_NSECTORS);
+
+	outb(0x00, hd1_base + PORT_ADDR0);
+	outb(0x00, hd1_base + PORT_ADDR1);
+	outb(0x00, hd1_base + PORT_ADDR2);
+
+	outb(0x00, hd1_base + PORT_DEV);
+	outb(ATACMD_ID, hd1_base + PORT_STAT);
+
+	while (ide_intr) hlt();
+
+	ide_info("ide_init: id completed. \n");
+
+	memset(buffer, 0x12, 512);
+
+	ide_cmdout(HD0, BLKWRITE, buffer, 1, 0x00000000);
+
+	schk_leave();
+
+	return 0;
+}
+
+
+void do_ide_intr(void) {
+	if (!ide_intr) {
+		ide_info("unexcepted ide interrupt. \n");
+	}
+
+	else {
+		ide_intr();
+		ide_intr = NULL;
+	}
+
+	return;
+}
+
+
+void do_ide_id(void) {
+	__io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
+	__uint16_t buffer[256] = {0};
+
+	insw(dev + PORT_DATA, buffer, 256);
+
+	ide_info("sectors count: %d. \n", *(__uint64_t *) (buffer + 100));
+
+	return;
+}
+
+
+void do_ide_read(void) {
+	__io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
+
+	insw(dev + PORT_DATA, &buffer, 256);
+
+	ide_info("read completed. \n");
+
+	return;
+}
+
+
+void do_ide_write(void) {
+	ide_info("write completed. \n");
+
+	return;
+}
+
+
+int ide_cmdout(int dev, int cmd, char *buffer, int nsect, __uint64_t lba) {
+	if (nsect > 255) 
+		panic("ide_cmdout: bad nsect. ");
+
+	if (cmd != BLKREAD && cmd != BLKWRITE) 
+		panic("ide_cmdout: bad cmd. ");
+
+	intr_dev = dev;
+	ide_intr = cmd == BLKREAD ? do_ide_read : do_ide_write;
+
+	__io_t port = dev == HD0 ? hda_base : hdb_base;
+	__io_t ctl = dev == HD0 ? hda_ctl : hdb_ctl;
+
+	outb(0x00, ctl);
+
+	wait_disk_not_busy(port);
+
+	outb(0xe0, port + PORT_DEV);
+	outb(0x00, port + PORT_ERR);
+
+	outb(nsect, port + PORT_NSECTORS);
+	outb(lba & 0xff, port + PORT_ADDR0);
+	outb((lba >> 8) & 0xff, port + PORT_ADDR1);
+	outb((lba >> 16) & 0xff, port + PORT_ADDR2);
+
+	wait_disk_ready(port);
+
+	outb(cmd == BLKREAD ? ATACMD_RD : ATACMD_WR, port + PORT_STAT);
+
+	if (cmd == BLKWRITE) 
+		outsw(port + PORT_DATA, buffer, 256);
+
+	return 0;
+}
+