commit 0433f83eaeaea8676624893f8e8457f0045a30bd
parent 1c1e4402427b9ef63374ab677c52e7b9b4fee9c4
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 17 Apr 2024 09:05:28 +0900
divide machine independent functions
Diffstat:
6 files changed, 120 insertions(+), 115 deletions(-)
diff --git a/sys/include/pci.h b/sys/include/pci.h
@@ -0,0 +1,23 @@
+// #include <libc.h>
+
+typedef struct PCIDev {
+ uint16 vendor_id;
+ uint16 device_id;
+ uint8 base_class;
+ uint8 sub_class;
+ uint8 prog;
+} PCIDev;
+#define MaxPCIDev 128
+
+extern PCIDev pci_dev[MaxPCIDev];
+extern int num_pci_dev;
+
+// Pci_config_read16 reads pci header.
+// The least significant bit of offset is ignored.
+uint16 pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset);
+uint32 pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset);
+uint16 pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func);
+int pci_scan_all_bus(void);
+int pci_scan_bus(uint8 bus);
+int pci_scan_dev(uint8 bus, uint8 dev);
+int pci_scan_func(uint8 bus, uint8 dev, uint8 func);
diff --git a/sys/src/Makefile b/sys/src/Makefile
@@ -10,7 +10,6 @@ BOOTCFLAGS = -I ../include \
CC = gcc # TODO: find portable compiler flags.
LD = ld
CFLAGS = -I ../include -fpic -mno-red-zone -Wall -g \
- -D$(ARCH) \
-ffreestanding -fno-builtin -nostdlib # -nostdinc
LDFLAGS = -L../lib -static
@@ -29,18 +28,20 @@ boot/boot.efi: boot/boot.dll
boot/boot.dll: boot/boot.c boot/utils.c
$(BOOTCC) $(BOOTCFLAGS) -o $@ boot/boot.c boot/utils.c
-kernel/main.elf: kernel/$(ARCH)/start.o kernel/main.o kernel/memmap.ld \
+kernel/main.elf: kernel/memmap.ld \
+ kernel/$(ARCH)/start.o kernel/main.o kernel/alloc.o kernel/console.o \
+ kernel/draw.o kernel/pci.o kernel/$(ARCH)/pci.o kernel/$(ARCH)/machine.o \
../lib/libc.a \
kernel/alloc.o kernel/console.o kernel/draw.o \
kernel/$(ARCH)/machine.o kernel/$(ARCH)/pci.o
$(LD) $(LDFLAGS) -e kernel_start -T kernel/memmap.ld -o $@ \
kernel/$(ARCH)/start.o kernel/main.o \
- kernel/alloc.o kernel/console.o kernel/draw.o kernel/$(ARCH)/pci.o \
- kernel/$(ARCH)/machine.o \
+ kernel/alloc.o kernel/console.o kernel/draw.o kernel/pci.o \
+ kernel/$(ARCH)/pci.o kernel/$(ARCH)/machine.o \
-lc
kernel/$(ARCH)/start.o: kernel/$(ARCH)/start.s
$(CC) $(CFLAGS) -c -o $@ kernel/$(ARCH)/start.s
-kernel/main.o: kernel/main.c ../include/uefi.h ../include/libc.h ../include/draw.h ../include/console.h kernel/$(ARCH)/pci.h
+kernel/main.o: kernel/main.c ../include/uefi.h ../include/libc.h ../include/draw.h ../include/console.h ../include/pci.h
$(CC) $(CFLAGS) -c -o $@ kernel/main.c
kernel/alloc.o: kernel/alloc.c ../include/libc.h
$(CC) $(CFLAGS) -c -o $@ kernel/alloc.c
@@ -48,7 +49,9 @@ kernel/console.o: kernel/console.c ../include/uefi.h ../include/libc.h ../includ
$(CC) $(CFLAGS) -c -o $@ kernel/console.c
kernel/draw.o: kernel/draw.c ../include/uefi.h ../include/libc.h ../include/draw.h ../include/console.h
$(CC) $(CFLAGS) -c -o $@ kernel/draw.c
-kernel/$(ARCH)/pci.o: kernel/$(ARCH)/pci.c kernel/$(ARCH)/pci.h ../include/libc.h
+kernel/pci.o: kernel/pci.c ../include/libc.h ../include/pci.h
+ $(CC) $(CFLAGS) -c -o $@ kernel/pci.c
+kernel/$(ARCH)/pci.o: kernel/$(ARCH)/pci.c kernel/$(ARCH)/pci.h ../include/libc.h ../include/pci.h
$(CC) $(CFLAGS) -c -o $@ kernel/$(ARCH)/pci.c
kernel/$(ARCH)/machine.o: kernel/$(ARCH)/machine.s
$(CC) $(CFLAGS) -c -o $@ kernel/$(ARCH)/machine.s
diff --git a/sys/src/kernel/amd64/pci.c b/sys/src/kernel/amd64/pci.c
@@ -1,13 +1,13 @@
#include <libc.h>
-#include "pci.h"
+#include <pci.h>
#include <uefi.h>
#include <draw.h>
#include <console.h>
+#include "pci.h"
+
const uint16 pci_config_address = 0xcf8;
const uint16 pci_config_data = 0xcfc;
-PCIDev pci_dev[MaxPCIDev];
-int num_pci_dev;
uint16
pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset)
@@ -34,82 +34,3 @@ pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset)
io_out32(pci_config_address, addr);
return io_in32(pci_config_data);
}
-
-uint16
-pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func)
-{
- return pci_config_read16(bus, dev, func, 0x0);
-}
-
-int
-pci_scan_all_bus(void)
-{
- int func;
- num_pci_dev = 0;
- for (func = 0; func < 8; func++) {
- if (pci_config_read_vendor_id(0, 0, func) == 0xffff) {
- continue;
- }
- if (pci_scan_bus(func) < 0) {
- return -1;
- }
- }
- return 0;
-}
-
-int
-pci_scan_bus(uint8 bus)
-{
- int dev;
- for (dev = 0; dev < 32; dev++) {
- if (pci_config_read_vendor_id(bus, dev, 0) == 0xffff) {
- continue;
- }
- if (pci_scan_dev(bus, dev) < 0) {
- return -1;
- }
- }
- return 0;
-}
-
-int
-pci_scan_dev(uint8 bus, uint8 dev)
-{
- int func;
- if (pci_config_read_vendor_id(bus, dev, 0) == 0xffff) {
- return pci_scan_func(bus, dev, 0);
- }
- for (func = 1; func < 8; func++) {
- if (pci_config_read_vendor_id(bus, dev, func) == 0xffff) {
- continue;
- }
- if (pci_scan_func(bus, dev, func) < 0) {
- return -1;
- }
- }
- return 0;
-}
-
-int
-pci_scan_func(uint8 bus, uint8 dev, uint8 func)
-{
- uint8 sub_bus;
- if (num_pci_dev >= MaxPCIDev) {
- return -1;
- }
- pci_dev[num_pci_dev].vendor_id = pci_config_read16(bus, dev, func, 0x0);
- pci_dev[num_pci_dev].device_id = pci_config_read16(bus, dev, func, 0x2);
- pci_dev[num_pci_dev].base_class = pci_config_read16(bus, dev, func, 0xa) >> 8;
- pci_dev[num_pci_dev].sub_class = pci_config_read16(bus, dev, func, 0xa);
- pci_dev[num_pci_dev].prog = pci_config_read16(bus, dev, func, 0x8) >> 8;
- num_pci_dev++;
-
- // pci-pci bridge
- if (pci_dev[num_pci_dev-1].base_class == 0x06 &&
- pci_dev[num_pci_dev-1].sub_class == 0x04) {
- sub_bus = pci_config_read16(bus, dev, func, 0x18) >> 8;
- return pci_scan_bus(sub_bus);
- }
-
- return 0;
-}
-\ No newline at end of file
diff --git a/sys/src/kernel/amd64/pci.h b/sys/src/kernel/amd64/pci.h
@@ -1,28 +1,7 @@
// #include <libc.h>
-typedef struct PCIDev {
- uint16 vendor_id;
- uint16 device_id;
- uint8 base_class;
- uint8 sub_class;
- uint8 prog;
-} PCIDev;
-#define MaxPCIDev 128
-
extern const uint16 pci_config_address;
extern const uint16 pci_config_data;
-extern PCIDev pci_dev[MaxPCIDev];
-extern int num_pci_dev;
void io_out32(uint16 addr, uint32 data);
uint32 io_in32(uint16 addr);
-
-// Pci_config_read16 reads pci header.
-// The least significant bit of offset is ignored.
-uint16 pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset);
-uint32 pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset);
-uint16 pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func);
-int pci_scan_all_bus(void);
-int pci_scan_bus(uint8 bus);
-int pci_scan_dev(uint8 bus, uint8 dev);
-int pci_scan_func(uint8 bus, uint8 dev, uint8 func);
-\ No newline at end of file
diff --git a/sys/src/kernel/main.c b/sys/src/kernel/main.c
@@ -2,10 +2,7 @@
#include <libc.h>
#include <draw.h>
#include <console.h>
-
-#ifdef amd64
-#include "amd64/pci.h"
-#endif
+#include <pci.h>
error err;
uint8 kernel_main_stack[1024];
diff --git a/sys/src/kernel/pci.c b/sys/src/kernel/pci.c
@@ -0,0 +1,84 @@
+#include <libc.h>
+#include <pci.h>
+
+PCIDev pci_dev[MaxPCIDev];
+int num_pci_dev;
+
+uint16
+pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func)
+{
+ return pci_config_read16(bus, dev, func, 0x0);
+}
+
+int
+pci_scan_all_bus(void)
+{
+ int func;
+ num_pci_dev = 0;
+ for (func = 0; func < 8; func++) {
+ if (pci_config_read_vendor_id(0, 0, func) == 0xffff) {
+ continue;
+ }
+ if (pci_scan_bus(func) < 0) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int
+pci_scan_bus(uint8 bus)
+{
+ int dev;
+ for (dev = 0; dev < 32; dev++) {
+ if (pci_config_read_vendor_id(bus, dev, 0) == 0xffff) {
+ continue;
+ }
+ if (pci_scan_dev(bus, dev) < 0) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int
+pci_scan_dev(uint8 bus, uint8 dev)
+{
+ int func;
+ if (pci_config_read_vendor_id(bus, dev, 0) == 0xffff) {
+ return pci_scan_func(bus, dev, 0);
+ }
+ for (func = 1; func < 8; func++) {
+ if (pci_config_read_vendor_id(bus, dev, func) == 0xffff) {
+ continue;
+ }
+ if (pci_scan_func(bus, dev, func) < 0) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int
+pci_scan_func(uint8 bus, uint8 dev, uint8 func)
+{
+ uint8 sub_bus;
+ if (num_pci_dev >= MaxPCIDev) {
+ return -1;
+ }
+ pci_dev[num_pci_dev].vendor_id = pci_config_read16(bus, dev, func, 0x0);
+ pci_dev[num_pci_dev].device_id = pci_config_read16(bus, dev, func, 0x2);
+ pci_dev[num_pci_dev].base_class = pci_config_read16(bus, dev, func, 0xa) >> 8;
+ pci_dev[num_pci_dev].sub_class = pci_config_read16(bus, dev, func, 0xa);
+ pci_dev[num_pci_dev].prog = pci_config_read16(bus, dev, func, 0x8) >> 8;
+ num_pci_dev++;
+
+ // pci-pci bridge
+ if (pci_dev[num_pci_dev-1].base_class == 0x06 &&
+ pci_dev[num_pci_dev-1].sub_class == 0x04) {
+ sub_bus = pci_config_read16(bus, dev, func, 0x18) >> 8;
+ return pci_scan_bus(sub_bus);
+ }
+
+ return 0;
+}