WKern
Loading...
Searching...
No Matches
scconfig.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18#define PCI_CONFIG_ADDRESS 0xCF8
19#define PCI_CONFIG_DATA 0xCFC
20#define VIRTIO_REG(offset) (iob + (offset))
21
22#include <global.h>
23#include <io/kio.h>
24
29
31
48u32 PciConfigRead(u8 bus, u8 slot, u8 func, u8 offset) {
49 u32 addr =
50 (1U << 31) | (bus << 16) | (slot << 11) | (func << 8) | (offset & 0xFC);
52 return Inl(PCI_CONFIG_DATA);
53}
54
60 for (u8 bus = 0; bus < 255; bus++) {
61 for (u8 slot = 0; slot < 32; slot++) {
62 for (u8 func = 0; func < 8; func++) {
63 u16 vendor = PciConfigRead(bus, slot, func, 0x00) & 0xFFFF;
64 if (vendor == 0xFFFF) {
65 continue;
66 }
67 u16 dev = (PciConfigRead(bus, slot, func, 0x00) >> 16) & 0xFFFF;
68 bar0 = PciConfigRead(bus, slot, func, 0x10);
69 u32 iob = bar0 & ~0xF;
70 u32 revid = (PciConfigRead(bus, slot, func, 0x08) >> 0) & 0xFF;
71
72 if (vendor == 0x1AF4 && dev == 0x1000 && revid == 0x00) {
73 virtio_bus = bus;
74 virtio_slot = slot;
75 virtio_func = func;
76 Kprintf("VirtIO at %x:%x.%x BAR0=%x IO=%x Rev=%x\n", bus,
77 slot, func, bar0, iob, revid);
78
79 // 1. Reset device
80 Outb(VIRTIO_REG(0x12), 0x00);
81
82 // 2. Set ACKNOWLEDGE and DRIVER status bits
83 Outb(VIRTIO_REG(0x12), 0x01); // AcKnowledge
84 Outb(VIRTIO_REG(0x12), 0x03); // AcKnowledge | Driver
85
86 // 3. Read device features
87 u32 features = Inl(VIRTIO_REG(0x00));
88 // Optionally masK features here
89
90 // 4. Write accepted features
91 Outl(VIRTIO_REG(0x04), features); // For now, accept all
92
93 // 5. Set DRIVER_OK bit
94 Outb(VIRTIO_REG(0x12),
95 0x07); // AcKnowledge | Driver | Features OK
96 return iob;
97 }
98 }
99 }
100 }
101 return (u32)(uptr)NULL;
102}
void Outb(u16 port, u8 val)
Write a byte to the specified I/O port.
Definition asm.c:30
u32 Inl(u16 port)
Read a 32-bit value from the specified I/O port.
Definition asm.c:84
void Outl(u16 port, u32 val)
Write a 32-bit value to the specified I/O port.
Definition asm.c:74
#define NULL
Definition global.h:39
u32 iob
Definition main.c:36
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
unsigned long uptr
Long Unsigned Int.
Definition nums.h:34
unsigned char u8
8-Bit Unsigned Int
Definition nums.h:32
u8 virtio_slot
Definition scconfig.c:27
u8 virtio_bus
Definition scconfig.c:26
u32 bar0
Definition scconfig.c:30
u8 virtio_irq
Definition scconfig.c:25
u8 virtio_func
Definition scconfig.c:28
#define VIRTIO_REG(offset)
Definition scconfig.c:20
u32 FindVirtionetDev()
Search for VirtNet Device.
Definition scconfig.c:59
#define PCI_CONFIG_ADDRESS
Definition scconfig.c:18
u32 PciConfigRead(u8 bus, u8 slot, u8 func, u8 offset)
Reads a 32-bit value from PCI configuration space.
Definition scconfig.c:48
#define PCI_CONFIG_DATA
Definition scconfig.c:19