Skip to content

Commit c5698ff

Browse files
committed
init_pci
1 parent 9c8fab2 commit c5698ff

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

src/int86.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ struct int86regs {
2727
uint16_t es, ds, fs, gs;
2828
} __attribute__ ((packed));
2929

30+
#define FLAGS_CARRY 0x0001
31+
#define FLAGS_PARITY 0x0004
32+
#define FLAGS_AUXC 0x0010
33+
#define FLAGS_ZERO 0x0040
34+
#define FLAGS_SIGN 0x0080
35+
#define FLAGS_TRAP 0x0100
36+
#define FLAGS_INTR 0x0200
37+
#define FLAGS_DIR 0x0400
38+
#define FLAGS_OVF 0x0800
39+
#define FLAGS_SIOPL(x) (((uint16_t)(x) & 3) << 12)
40+
#define FLAGS_GIOPL(x) (((x) >> 12) & 3)
41+
#define FLAGS_NTASK 0x4000
42+
3043
void int86(int inum, struct int86regs *regs);
3144

3245
#endif /* INT86_H_ */

src/kmain.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2525
#include "timer.h"
2626
#include "contty.h"
2727
#include "video.h"
28+
#include "pci.h"
2829
#include "vbetest.h"
2930

3031

@@ -40,6 +41,8 @@ void pcboot_main(void)
4041

4142
init_mem();
4243

44+
init_pci();
45+
4346
/* initialize the timer */
4447
init_timer();
4548

src/pci.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
pcboot - bootable PC demo/game kernel
3+
Copyright (C) 2018 John Tsiombikas <[email protected]>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY, without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
#include <stdio.h>
19+
#include <string.h>
20+
#include <inttypes.h>
21+
#include "pci.h"
22+
#include "int86.h"
23+
#include "asmops.h"
24+
#include "panic.h"
25+
26+
#define CONFIG_ADDR_PORT 0xcf8
27+
#define CONFIG_DATA_PORT 0xcfc
28+
29+
#define ADDR_ENABLE 0x80000000
30+
#define ADDR_BUSID(x) (((uint32_t)(x) & 0xff) << 16)
31+
#define ADDR_DEVID(x) (((uint32_t)(x) & 0x1f) << 11)
32+
#define ADDR_FUNC(x) (((uint32_t)(x) & 3) << 8)
33+
34+
/* signature returned in edx by the PCI BIOS present function: FOURCC "PCI " */
35+
#define PCI_SIG 0x20494350
36+
37+
static uint16_t cfg_read32_acc1(int bus, int dev, int func, int reg);
38+
static uint16_t cfg_read32_acc2(int bus, int dev, int func, int reg);
39+
40+
static uint16_t (*cfg_read32)(int, int, int, int);
41+
42+
void init_pci(void)
43+
{
44+
struct int86regs regs;
45+
46+
memset(&regs, 0, sizeof regs);
47+
regs.eax = 0xb101;
48+
int86(0x1a, &regs);
49+
50+
/* PCI BIOS present if CF=0, AH=0, and EDX has the "PCI " sig FOURCC */
51+
if((regs.flags & FLAGS_CARRY) || (regs.eax & 0xff00) || regs.edx != PCI_SIG) {
52+
printf("No PCI BIOS present\n");
53+
return;
54+
}
55+
56+
printf("PCI BIOS v%x.%x found\n", (regs.ebx & 0xff00) >> 8, regs.ebx & 0xff);
57+
if(regs.eax & 1) {
58+
cfg_read32 = cfg_read32_acc1;
59+
} else {
60+
if(!(regs.eax & 2)) {
61+
printf("Failed to find supported PCI access mechanism\n");
62+
return;
63+
}
64+
printf("PCI access mechanism #1 unsupported, falling back to mechanism #2\n");
65+
cfg_read32 = cfg_read32_acc2;
66+
}
67+
}
68+
69+
static uint16_t cfg_read32_acc1(int bus, int dev, int func, int reg)
70+
{
71+
uint32_t addr = ADDR_ENABLE | ADDR_BUSID(bus) | ADDR_DEVID(dev) |
72+
ADDR_FUNC(func) | reg;
73+
74+
outl(addr, CONFIG_ADDR_PORT);
75+
return inl(CONFIG_DATA_PORT);
76+
}
77+
78+
static uint16_t cfg_read32_acc2(int bus, int dev, int func, int reg)
79+
{
80+
panic("BUG: PCI access mechanism #2 not implemented yet!");
81+
return 0;
82+
}

src/pci.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
pcboot - bootable PC demo/game kernel
3+
Copyright (C) 2018 John Tsiombikas <[email protected]>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY, without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
#ifndef PCI_H_
19+
#define PCI_H_
20+
21+
void init_pci(void);
22+
23+
#endif /* PCI_H_ */

0 commit comments

Comments
 (0)