The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pci.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file pci.h
5 * @brief PCI bus enumeration through the legacy configuration ports.
6 *
7 * pci_init reads the configuration space of every function on every bus
8 * (ports 0xCF8 and 0xCFC) and keeps a table of what it finds: the ids, the
9 * class, the BARs with their sizes, the interrupt line and pin, and where
10 * the MSI and MSI-X capabilities are. drivers find their device by class or
11 * by vendor and device id.
12 */
13
14#include "common/stdint.h"
15
16#define PCI_MAX_DEVICES 64
17#define PCI_BAR_COUNT 6
18
19// classes and subclasses the kernel looks for
20#define PCI_CLASS_STORAGE 0x01
21#define PCI_SUBCLASS_SATA 0x06 // with prog_if 0x01: AHCI
22#define PCI_SUBCLASS_NVME 0x08
23#define PCI_CLASS_MULTIMEDIA 0x04
24#define PCI_SUBCLASS_AC97 0x01 // multimedia audio controller
25#define PCI_SUBCLASS_HDA 0x03 // high definition audio
26#define PCI_CLASS_BRIDGE 0x06
27#define PCI_CLASS_SERIAL 0x0C
28#define PCI_SUBCLASS_USB 0x03 // with prog_if 0x30: xHCI
29
30// configuration space offsets
31#define PCI_COMMAND 0x04
32#define PCI_COMMAND_IO 0x0001
33#define PCI_COMMAND_MEMORY 0x0002
34#define PCI_COMMAND_BUS_MASTER 0x0004
35#define PCI_COMMAND_INTX_OFF 0x0400
36
37typedef struct
38{
39 uint64_t base; // the address (I/O port or memory), 0 if the BAR is not used
40 uint64_t size; // bytes the BAR decodes
41 bool is_io; // an I/O port range (else memory)
42 bool is_64; // a 64-bit memory BAR (it uses the next BAR too)
44} pci_bar_t;
45
46typedef struct
47{
57 uint8_t header_type; // bits 0-6: 0 = device, 1 = PCI bridge
58 uint8_t irq_line; // the legacy IRQ the firmware routed (0xFF: none)
59 uint8_t irq_pin; // 1 = INTA ... 4 = INTD, 0 = none
60 uint8_t msi_offset; // the MSI capability in config space, 0 if none
61 uint8_t msix_offset; // the MSI-X capability in config space, 0 if none
64
65/**
66 * enumerate the PCI bus. call once, before the drivers that use PCI.
67 * @return the number of functions found.
68 */
69uint32_t pci_init(void);
70
71/**
72 * the number of functions pci_init found.
73 */
75
76/**
77 * a function by its index in the table.
78 * @return the function, or NULL if index is out of range.
79 */
81
82/**
83 * the nth function of a class, subclass and programming interface.
84 * @param prog_if the programming interface, or -1 for any.
85 * @param nth 0 for the first match.
86 * @return the function, or NULL if there is no such match.
87 */
88pci_device_t *pci_find_class(uint8_t class_code, uint8_t subclass, int prog_if, uint32_t nth);
89
90/**
91 * read or write the configuration space of a function (offsets are
92 * aligned to the width).
93 */
94uint32_t pci_read32(const pci_device_t *dev, uint8_t offset);
95uint16_t pci_read16(const pci_device_t *dev, uint8_t offset);
96uint8_t pci_read8(const pci_device_t *dev, uint8_t offset);
97void pci_write32(const pci_device_t *dev, uint8_t offset, uint32_t value);
98void pci_write16(const pci_device_t *dev, uint8_t offset, uint16_t value);
99
100/**
101 * turn on memory decoding and bus mastering (DMA) for a function.
102 */
103void pci_enable_bus_master(const pci_device_t *dev);
104
105/**
106 * a short name for a class, for /proc/pci (for example "SATA controller").
107 */
108const char *pci_class_name(const pci_device_t *dev);
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
unsigned short uint16_t
Definition stdint.h:13
pci_device_t * pci_find_class(uint8_t class_code, uint8_t subclass, int prog_if, uint32_t nth)
the nth function of a class, subclass and programming interface.
Definition pci.c:261
void pci_write32(const pci_device_t *dev, uint8_t offset, uint32_t value)
Definition pci.c:84
void pci_enable_bus_master(const pci_device_t *dev)
turn on memory decoding and bus mastering (DMA) for a function.
Definition pci.c:98
uint16_t pci_read16(const pci_device_t *dev, uint8_t offset)
Definition pci.c:74
pci_device_t * pci_device_at(uint32_t index)
a function by its index in the table.
Definition pci.c:256
uint32_t pci_init(void)
enumerate the PCI bus.
Definition pci.c:218
uint32_t pci_device_count(void)
the number of functions pci_init found.
Definition pci.c:251
uint32_t pci_read32(const pci_device_t *dev, uint8_t offset)
read or write the configuration space of a function (offsets are aligned to the width).
Definition pci.c:69
void pci_write16(const pci_device_t *dev, uint8_t offset, uint16_t value)
Definition pci.c:89
uint8_t pci_read8(const pci_device_t *dev, uint8_t offset)
Definition pci.c:79
const char * pci_class_name(const pci_device_t *dev)
a short name for a class, for /proc/pci (for example "SATA controller").
Definition pci.c:279
#define PCI_BAR_COUNT
Definition pci.h:17
Definition pci.h:38
bool prefetchable
Definition pci.h:43
uint64_t size
Definition pci.h:40
uint64_t base
Definition pci.h:39
bool is_64
Definition pci.h:42
bool is_io
Definition pci.h:41
Definition pci.h:47
uint8_t revision
Definition pci.h:56
uint8_t irq_pin
Definition pci.h:59
uint8_t msi_offset
Definition pci.h:60
uint8_t device
Definition pci.h:49
uint8_t msix_offset
Definition pci.h:61
uint8_t function
Definition pci.h:50
uint16_t vendor_id
Definition pci.h:51
uint8_t irq_line
Definition pci.h:58
uint8_t header_type
Definition pci.h:57
uint16_t device_id
Definition pci.h:52
uint8_t class_code
Definition pci.h:53
pci_bar_t bars[PCI_BAR_COUNT]
Definition pci.h:62
uint8_t prog_if
Definition pci.h:55
uint8_t subclass
Definition pci.h:54
uint8_t bus
Definition pci.h:48