110 lines
2.9 KiB
C
110 lines
2.9 KiB
C
#ifndef PCI_PCI_H_
|
|
#define PCI_PCI_H_
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include "kprintf.h"
|
|
|
|
typedef union {
|
|
uint32_t bits;
|
|
struct {
|
|
uint32_t alwayszero: 2;
|
|
uint32_t fieldnum: 6;
|
|
uint32_t fnnum: 3;
|
|
uint32_t devnum: 5;
|
|
uint32_t busnum: 8;
|
|
uint32_t resv: 7;
|
|
uint32_t enable: 1;
|
|
};
|
|
} PciDev;
|
|
|
|
typedef struct {
|
|
uint16_t iobase;
|
|
} PciBarIo;
|
|
|
|
typedef struct {
|
|
uint64_t addr;
|
|
} PciBarMem;
|
|
|
|
typedef struct {
|
|
union {
|
|
PciBarIo io;
|
|
PciBarMem mem;
|
|
} x;
|
|
uint64_t size;
|
|
uint32_t flags;
|
|
} PciBar;
|
|
|
|
#define PCI_CFG_ADDR 0xCF8
|
|
#define PCI_CFG_DATA 0xCFC
|
|
|
|
#define PCI_VENDORID 0x00
|
|
#define PCI_DEVICEID 0x02
|
|
#define PCI_CMD 0x04
|
|
#define PCI_STATUS 0x06
|
|
#define PCI_REVID 0x08
|
|
#define PCI_PROGIF 0x09
|
|
#define PCI_SUBCLASS 0x0A
|
|
#define PCI_CLASS 0x0B
|
|
#define PCI_CACHELINESZ 0x0C
|
|
#define PCI_LTNCY_TIMER 0x0D
|
|
#define PCI_HDRTYPE 0x0E
|
|
#define PCI_BIST 0x0F
|
|
#define PCI_BAR0 0x10
|
|
#define PCI_BAR1 0x14
|
|
#define PCI_BAR2 0x18
|
|
#define PCI_BAR3 0x1C
|
|
#define PCI_BAR4 0x20
|
|
#define PCI_BAR5 0x24
|
|
#define PCI_INTRLINE 0x3C
|
|
#define PCI_SCNDRY_BUS 0x09
|
|
|
|
#define PCI_HDR_DEV 0
|
|
#define PCI_HDR_BRIDGE 1
|
|
#define PCI_HDR_CARDBUS 2
|
|
|
|
#define PCI_DEV_PER_BUS 32
|
|
#define PCI_FN_PER_DEV 32
|
|
|
|
#define PCI_BAR_IO 0x01
|
|
#define PCI_BAR_MEM32 0x02
|
|
#define PCI_BAR_MEM64 0x04
|
|
#define PCI_BAR_PREFETCH 0x08
|
|
|
|
uint32_t pci_read32(PciDev dev, uint32_t field);
|
|
uint16_t pci_read16(PciDev dev, uint32_t field);
|
|
uint8_t pci_read8(PciDev dev, uint32_t field);
|
|
void pci_write32(PciDev dev, uint32_t field, uint32_t v);
|
|
void pci_write16(PciDev dev, uint32_t field, uint16_t v);
|
|
void pci_write8(PciDev dev, uint32_t field, uint8_t v);
|
|
void pci_readbar(PciDev dev, uint32_t bar, uint32_t *addr, uint32_t *mask);
|
|
void pci_getbar(PciDev dev, PciBar *bar, uint32_t barid);
|
|
uint32_t pci_devtype(PciDev dev);
|
|
uint32_t pci_scndrybus(PciDev dev);
|
|
uint32_t pci_isend(PciDev dev);
|
|
PciDev pci_scanfn(uint16_t vendorid, uint16_t deviceid, uint32_t bus,
|
|
uint32_t device, uint32_t fn, int devtype);
|
|
PciDev pci_scanbus(uint16_t vendorid, uint16_t deviceid,
|
|
uint32_t bus, int devtype);
|
|
PciDev pci_scandev(uint16_t vendorid, uint16_t deviceid,
|
|
uint32_t bus, uint32_t device, int devtype);
|
|
PciDev pci_getdev(uint16_t vendorid, uint16_t deviceid, int devtype);
|
|
|
|
void pci_init(void);
|
|
|
|
#define PCI_INIT_ARRAY_MAX 0x100
|
|
|
|
typedef void (*PciInitFn)(void);
|
|
extern PciInitFn PCI_INIT_ARRAY[PCI_INIT_ARRAY_MAX];
|
|
|
|
#define PCI_LOG_BAR(tag_, bar_, num) \
|
|
LOG("pci/"tag_, "BAR%d: type=%s flags=0x%08x,size=0x%016x,resource=0x%016x\n", \
|
|
num, \
|
|
(bar_).flags & PCI_BAR_IO ? "I/O" : ((bar_).flags & PCI_BAR_MEM32 ? "MEM32" : "MEM64"), \
|
|
(bar_).flags, \
|
|
(bar_).size, \
|
|
(bar_).flags & PCI_BAR_IO ? (bar_).x.io.iobase : (bar_).x.mem.addr \
|
|
);
|
|
|
|
#endif // PCI_PCI_H_
|