From: John Tsiombikas Date: Sun, 6 May 2018 15:04:25 +0000 (+0300) Subject: init_pci X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=bootcensus;a=commitdiff_plain;h=c5698ff9776ac76cd2243525177c35b0d12855e0 init_pci --- diff --git a/src/int86.h b/src/int86.h index d6fcb7d..2545925 100644 --- a/src/int86.h +++ b/src/int86.h @@ -27,6 +27,19 @@ struct int86regs { uint16_t es, ds, fs, gs; } __attribute__ ((packed)); +#define FLAGS_CARRY 0x0001 +#define FLAGS_PARITY 0x0004 +#define FLAGS_AUXC 0x0010 +#define FLAGS_ZERO 0x0040 +#define FLAGS_SIGN 0x0080 +#define FLAGS_TRAP 0x0100 +#define FLAGS_INTR 0x0200 +#define FLAGS_DIR 0x0400 +#define FLAGS_OVF 0x0800 +#define FLAGS_SIOPL(x) (((uint16_t)(x) & 3) << 12) +#define FLAGS_GIOPL(x) (((x) >> 12) & 3) +#define FLAGS_NTASK 0x4000 + void int86(int inum, struct int86regs *regs); #endif /* INT86_H_ */ diff --git a/src/kmain.c b/src/kmain.c index 339d3e0..2404033 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -25,6 +25,7 @@ along with this program. If not, see . #include "timer.h" #include "contty.h" #include "video.h" +#include "pci.h" #include "vbetest.h" @@ -40,6 +41,8 @@ void pcboot_main(void) init_mem(); + init_pci(); + /* initialize the timer */ init_timer(); diff --git a/src/pci.c b/src/pci.c new file mode 100644 index 0000000..b5ab9fd --- /dev/null +++ b/src/pci.c @@ -0,0 +1,82 @@ +/* +pcboot - bootable PC demo/game kernel +Copyright (C) 2018 John Tsiombikas + +This program 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) any later version. + +This program 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 this program. If not, see . +*/ +#include +#include +#include +#include "pci.h" +#include "int86.h" +#include "asmops.h" +#include "panic.h" + +#define CONFIG_ADDR_PORT 0xcf8 +#define CONFIG_DATA_PORT 0xcfc + +#define ADDR_ENABLE 0x80000000 +#define ADDR_BUSID(x) (((uint32_t)(x) & 0xff) << 16) +#define ADDR_DEVID(x) (((uint32_t)(x) & 0x1f) << 11) +#define ADDR_FUNC(x) (((uint32_t)(x) & 3) << 8) + +/* signature returned in edx by the PCI BIOS present function: FOURCC "PCI " */ +#define PCI_SIG 0x20494350 + +static uint16_t cfg_read32_acc1(int bus, int dev, int func, int reg); +static uint16_t cfg_read32_acc2(int bus, int dev, int func, int reg); + +static uint16_t (*cfg_read32)(int, int, int, int); + +void init_pci(void) +{ + struct int86regs regs; + + memset(®s, 0, sizeof regs); + regs.eax = 0xb101; + int86(0x1a, ®s); + + /* PCI BIOS present if CF=0, AH=0, and EDX has the "PCI " sig FOURCC */ + if((regs.flags & FLAGS_CARRY) || (regs.eax & 0xff00) || regs.edx != PCI_SIG) { + printf("No PCI BIOS present\n"); + return; + } + + printf("PCI BIOS v%x.%x found\n", (regs.ebx & 0xff00) >> 8, regs.ebx & 0xff); + if(regs.eax & 1) { + cfg_read32 = cfg_read32_acc1; + } else { + if(!(regs.eax & 2)) { + printf("Failed to find supported PCI access mechanism\n"); + return; + } + printf("PCI access mechanism #1 unsupported, falling back to mechanism #2\n"); + cfg_read32 = cfg_read32_acc2; + } +} + +static uint16_t cfg_read32_acc1(int bus, int dev, int func, int reg) +{ + uint32_t addr = ADDR_ENABLE | ADDR_BUSID(bus) | ADDR_DEVID(dev) | + ADDR_FUNC(func) | reg; + + outl(addr, CONFIG_ADDR_PORT); + return inl(CONFIG_DATA_PORT); +} + +static uint16_t cfg_read32_acc2(int bus, int dev, int func, int reg) +{ + panic("BUG: PCI access mechanism #2 not implemented yet!"); + return 0; +} diff --git a/src/pci.h b/src/pci.h new file mode 100644 index 0000000..1ce01bc --- /dev/null +++ b/src/pci.h @@ -0,0 +1,23 @@ +/* +pcboot - bootable PC demo/game kernel +Copyright (C) 2018 John Tsiombikas + +This program 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) any later version. + +This program 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 this program. If not, see . +*/ +#ifndef PCI_H_ +#define PCI_H_ + +void init_pci(void); + +#endif /* PCI_H_ */