commit a3e1af3faae1380dce3556ea1578bf5207b38f41
parent 1cefe2bbda34606fb4fb05e5a9ce1aae661b1e20
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 11 Apr 2024 09:20:19 +0900
scan pci devices
Diffstat:
4 files changed, 38 insertions(+), 22 deletions(-)
diff --git a/sys/src/Makefile b/sys/src/Makefile
@@ -7,9 +7,10 @@ BOOTCFLAGS = -I ../include \
-fno-builtin -g -mabi=sysv \
-mno-stack-arg-probe -Wall -e EfiMain
-CC = gcc #TODO: tcc doesn't work. I think it is a problem with build flags.
+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
@@ -39,7 +40,7 @@ kernel/main.elf: kernel/$(ARCH)/start.o kernel/main.o kernel/memmap.ld \
-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/main.o: kernel/main.c ../include/uefi.h ../include/libc.h ../include/draw.h ../include/console.h kernel/$(ARCH)/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
diff --git a/sys/src/kernel/amd64/pci.c b/sys/src/kernel/amd64/pci.c
@@ -1,6 +1,9 @@
#include <libc.h>
#include "pci.h"
+const uint16 pci_config_address = 0xcf8;
+const uint16 pci_config_data = 0xcfc;
+
uint16
pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset)
{
@@ -14,6 +17,19 @@ pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset)
return (io_in32(pci_config_data) >> ((offset&2)*8)) & 0xffff;
}
+uint32
+pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset)
+{
+ uint32 addr;
+ addr = 1 << 31 | // enable bit.
+ (uint32) bus << 16 |
+ (uint32) (dev&0x1f) << 11 |
+ (uint32) (func&3) << 8 |
+ (offset&0xfc); // the least 2 bits should be 0.
+ io_out32(pci_config_address, addr);
+ return io_in32(pci_config_data);
+}
+
uint16
pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func)
{
diff --git a/sys/src/kernel/amd64/pci.h b/sys/src/kernel/amd64/pci.h
@@ -1,10 +1,13 @@
// #include <libc.h>
-const uint16 pci_config_address = 0xcf8;
-const uint16 pci_config_data = 0xcfc;
+extern const uint16 pci_config_address;
+extern const uint16 pci_config_data;
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);
diff --git a/sys/src/kernel/main.c b/sys/src/kernel/main.c
@@ -3,6 +3,10 @@
#include <draw.h>
#include <console.h>
+#ifdef amd64
+#include "amd64/pci.h"
+#endif
+
error err;
uint8 kernel_main_stack[1024];
RGBA32 m_img[16*16]; // initialized at the bottom of this file.
@@ -26,7 +30,7 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
root_window.children = root_children;
Window win0;
- make_window(&root_window, &win0, (Point){150, 100}, (Point){400, 300}, 0xff, 0xb9d08bff);
+ make_window(&root_window, &win0, (Point){150, 100}, (Point){400, 500}, 0xff, 0xb9d08bff);
Console con0 = {
.win = &win0,
.font = &asciifont,
@@ -47,23 +51,15 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
draw_mouse(&root_window, &m);
cons_printf(&con0, "hello world!\n");
cons_printf(&con0, "pixel at {%d, %d}: %x\n", p.x, p.y, get_pixel(&root_window, p));
- cons_printf(&con0, "pci bus 0\n");
- cons_printf(&con0, "%x\t%x\n",
- (uint16) pci_config_read16(0, 0, 0, 2),
- (uint16) pci_config_read16(0, 0, 0, 0));
- cons_printf(&con0, "%x\t%x\n",
- (uint16) pci_config_read16(0, 0, 0, 6),
- (uint16) pci_config_read16(0, 0, 0, 4));
- cons_printf(&con0, "%x\t%x\t%x\t%x\n",
- (uint8) pci_config_read16(0, 0, 0, 11),
- (uint8) pci_config_read16(0, 0, 0, 10),
- (uint8) pci_config_read16(0, 0, 0, 9),
- (uint8) pci_config_read16(0, 0, 0, 8));
- cons_printf(&con0, "%x\t%x\t%x\t%x\n",
- (uint8) pci_config_read16(0, 0, 0, 15),
- (uint8) pci_config_read16(0, 0, 0, 14),
- (uint8) pci_config_read16(0, 0, 0, 13),
- (uint8) pci_config_read16(0, 0, 0, 12));
+ for (int dev = 0; dev < 32; dev++) {
+ uint16 vendor_id, device_id;
+ vendor_id = pci_config_read_vendor_id(0, dev, 0);
+ if (vendor_id == 0xffff) {
+ continue;
+ }
+ device_id = pci_config_read16(0, dev, 0, 2);
+ cons_printf(&con0, "dev %d: %x, %x\n", dev, vendor_id, device_id);
+ }
halt:
for(;;) { __asm__("hlt"); }