pci: add simple pci driver
this driver only prints devices on the pci bus.
This commit is contained in:
parent
2c9e239c65
commit
605507a688
7 changed files with 113 additions and 32 deletions
40
dev/pci.c
Normal file
40
dev/pci.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/pio.h>
|
||||
#include <dev/pci.h>
|
||||
|
||||
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset)
|
||||
{
|
||||
uint32_t addr;
|
||||
uint32_t lbus = bus;
|
||||
uint32_t lslot = slot;
|
||||
uint32_t lfunc = func;
|
||||
uint16_t tmp = 0;
|
||||
|
||||
addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | ((uint32_t)0x80000000);
|
||||
|
||||
outl(PCIPM_CONFIG_ADDRESS, addr);
|
||||
tmp = (uint16_t)((inl(PCIPM_CONFIG_DATA) >> ((offset & 2) << 3)) & 0xFFFF);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
static void pci_print_bus(uint8_t bus)
|
||||
{
|
||||
for (uint8_t slot = 0; slot < 32; slot++) {
|
||||
uint16_t vendor_id = pci_config_read_word(bus, slot, 0, 0);
|
||||
if (vendor_id == 0xFFFF)
|
||||
continue;
|
||||
|
||||
uint8_t class = pci_config_read_word(bus, slot, 0, 0xA);
|
||||
uint8_t subclass = (pci_config_read_word(bus, slot, 0, 0xA) >> 8);
|
||||
uint8_t prog_if = (pci_config_read_word(bus, slot, 0, 0x8) >> 8);
|
||||
|
||||
printf(LOG_INFO "PCI device: vendor:%xh class:%xh subclass:%xh prof_if:%xh\n", vendor_id, class, subclass, prog_if);
|
||||
}
|
||||
}
|
||||
|
||||
void pci_init()
|
||||
{
|
||||
for (int bus = 0; bus < 256; bus++)
|
||||
pci_print_bus(bus);
|
||||
}
|
||||
13
include/dev/pci.h
Normal file
13
include/dev/pci.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PCI_H_
|
||||
#define _PCI_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define PCIPM_CONFIG_ADDRESS 0xCF8
|
||||
#define PCIPM_CONFIG_DATA 0xCFC
|
||||
|
||||
void pci_init();
|
||||
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||
|
||||
#endif /* _PCI_H_ */
|
||||
|
|
@ -4,21 +4,16 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
static inline void outb(uint16_t port, uint8_t val)
|
||||
{
|
||||
__asm__ volatile("outb %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
#define genio(suffix, type) \
|
||||
void out##suffix(uint16_t port, type value); \
|
||||
type in##suffix(uint16_t port);
|
||||
|
||||
static inline uint8_t inb(uint16_t port)
|
||||
{
|
||||
uint8_t ret;
|
||||
__asm__ volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
genio(b, uint8_t);
|
||||
genio(w, uint16_t);
|
||||
genio(l, uint32_t);
|
||||
|
||||
static inline void iowait()
|
||||
{
|
||||
outb(0x80, 0);
|
||||
}
|
||||
#undef genio
|
||||
|
||||
void iowait();
|
||||
|
||||
#endif /* _PIO_H_ */
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@
|
|||
#ifndef _STDIO_H_
|
||||
#define _STDIO_H_
|
||||
|
||||
#define COL_RED_BLACK "\e[31;40m"
|
||||
#define COL_YELLOW_BLACK "\e[33;40m"
|
||||
#define COL_GREEN_BLACK "\e[32;40m"
|
||||
#define COL_MAGENTA_BLACK "\e[35;40m"
|
||||
#define COL_RED_BLACK "\e[31m"
|
||||
#define COL_YELLOW_BLACK "\e[33m"
|
||||
#define COL_GREEN_BLACK "\e[32m"
|
||||
#define COL_MAGENTA_BLACK "\e[35m"
|
||||
|
||||
#define COL_RESET "\e[m"
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
#include <dev/pic.h>
|
||||
#include <dev/console.h>
|
||||
#include <dev/serial.h>
|
||||
#include <dev/pci.h>
|
||||
|
||||
#include <limine/limine.h>
|
||||
|
||||
|
|
@ -73,6 +74,8 @@ void kmain(void)
|
|||
|
||||
void kmain_post_stack_init()
|
||||
{
|
||||
pci_init();
|
||||
|
||||
printf("\n\n# ");
|
||||
yield();
|
||||
}
|
||||
|
|
|
|||
23
init/pio.c
Normal file
23
init/pio.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/pio.h>
|
||||
|
||||
#define genio(suffix, type) \
|
||||
inline void out##suffix(uint16_t port, type value) \
|
||||
{ \
|
||||
__asm__ volatile("out" #suffix " %0, %1" : : "a"(value), "Nd"(port)); \
|
||||
} \
|
||||
inline type in##suffix(uint16_t port) \
|
||||
{ \
|
||||
type ret; \
|
||||
__asm__ volatile("in" #suffix " %1, %0" : "=a"(ret) : "Nd"(port)); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
inline void iowait()
|
||||
{
|
||||
outb(0x80, 0);
|
||||
}
|
||||
|
||||
genio(b, uint8_t);
|
||||
genio(w, uint16_t);
|
||||
genio(l, uint32_t);
|
||||
35
lib/stdio.c
35
lib/stdio.c
|
|
@ -34,6 +34,7 @@ int printf(const char *restrict fmt, ...)
|
|||
va_start(args, fmt);
|
||||
|
||||
const char *convert = "0123456789ABCDEF";
|
||||
char buf[11] = { 0 };
|
||||
|
||||
while ((c = *fmt++)) {
|
||||
if (c == '%') {
|
||||
|
|
@ -52,19 +53,15 @@ int printf(const char *restrict fmt, ...)
|
|||
i = -i;
|
||||
}
|
||||
|
||||
char buf[11];
|
||||
n = 0;
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
do {
|
||||
buf[n] = convert[i % 10];
|
||||
n++;
|
||||
} while ((i /= 10));
|
||||
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf_out(buf[(n - 1) - j]);
|
||||
ret++;
|
||||
}
|
||||
break;
|
||||
goto printf_reverse;
|
||||
case 's':
|
||||
s = va_arg(args, char *);
|
||||
ret += printf_write(s);
|
||||
|
|
@ -76,17 +73,21 @@ int printf(const char *restrict fmt, ...)
|
|||
break;
|
||||
case 'x':
|
||||
i = va_arg(args, int);
|
||||
for (uint32_t j = 0; j < (2 * sizeof(i)); j++) {
|
||||
int sel = ((sizeof(i) * 2) - j - 1) * 4;
|
||||
int num = (i >> sel) & 0xF;
|
||||
printf_out(convert[num]);
|
||||
ret++;
|
||||
}
|
||||
n = 0;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
do {
|
||||
buf[n] = convert[i & 0xF];
|
||||
n++;
|
||||
} while ((i >>= 4));
|
||||
|
||||
goto printf_reverse;
|
||||
break;
|
||||
case 'X':
|
||||
l = va_arg(args, long);
|
||||
for (uint64_t j = 0; j < (2 * sizeof(l)); j++) {
|
||||
int sel = ((sizeof(l) * 2) - j - 1) * 4;
|
||||
for (uint64_t j = 0; j < (sizeof(l) << 1); j++) {
|
||||
int sel = ((sizeof(l) << 1) - j - 1) << 2;
|
||||
int num = (l >> sel) & 0xF;
|
||||
printf_out(convert[num]);
|
||||
ret++;
|
||||
|
|
@ -95,6 +96,12 @@ int printf(const char *restrict fmt, ...)
|
|||
default:
|
||||
printf_out(c);
|
||||
ret++;
|
||||
break;
|
||||
printf_reverse:
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf_out(buf[(n - 1) - j]);
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf_out(c);
|
||||
|
|
|
|||
Loading…
Reference in a new issue