The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
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
37
typedef
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)
43
bool
prefetchable
;
44
}
pci_bar_t
;
45
46
typedef
struct
47
{
48
uint8_t
bus
;
49
uint8_t
device
;
50
uint8_t
function
;
51
uint16_t
vendor_id
;
52
uint16_t
device_id
;
53
uint8_t
class_code
;
54
uint8_t
subclass
;
55
uint8_t
prog_if
;
56
uint8_t
revision
;
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
62
pci_bar_t
bars
[
PCI_BAR_COUNT
];
63
}
pci_device_t
;
64
65
/**
66
* enumerate the PCI bus. call once, before the drivers that use PCI.
67
* @return the number of functions found.
68
*/
69
uint32_t
pci_init
(
void
);
70
71
/**
72
* the number of functions pci_init found.
73
*/
74
uint32_t
pci_device_count
(
void
);
75
76
/**
77
* a function by its index in the table.
78
* @return the function, or NULL if index is out of range.
79
*/
80
pci_device_t
*
pci_device_at
(
uint32_t
index);
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
*/
88
pci_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
*/
94
uint32_t
pci_read32
(
const
pci_device_t
*dev,
uint8_t
offset);
95
uint16_t
pci_read16
(
const
pci_device_t
*dev,
uint8_t
offset);
96
uint8_t
pci_read8
(
const
pci_device_t
*dev,
uint8_t
offset);
97
void
pci_write32
(
const
pci_device_t
*dev,
uint8_t
offset,
uint32_t
value);
98
void
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
*/
103
void
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
*/
108
const
char
*
pci_class_name
(
const
pci_device_t
*dev);
stdint.h
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint16_t
unsigned short uint16_t
Definition
stdint.h:13
pci_find_class
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
pci_write32
void pci_write32(const pci_device_t *dev, uint8_t offset, uint32_t value)
Definition
pci.c:84
pci_enable_bus_master
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
pci_read16
uint16_t pci_read16(const pci_device_t *dev, uint8_t offset)
Definition
pci.c:74
pci_device_at
pci_device_t * pci_device_at(uint32_t index)
a function by its index in the table.
Definition
pci.c:256
pci_init
uint32_t pci_init(void)
enumerate the PCI bus.
Definition
pci.c:218
pci_device_count
uint32_t pci_device_count(void)
the number of functions pci_init found.
Definition
pci.c:251
pci_read32
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
pci_write16
void pci_write16(const pci_device_t *dev, uint8_t offset, uint16_t value)
Definition
pci.c:89
pci_read8
uint8_t pci_read8(const pci_device_t *dev, uint8_t offset)
Definition
pci.c:79
pci_class_name
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
PCI_BAR_COUNT
#define PCI_BAR_COUNT
Definition
pci.h:17
pci_bar_t
Definition
pci.h:38
pci_bar_t::prefetchable
bool prefetchable
Definition
pci.h:43
pci_bar_t::size
uint64_t size
Definition
pci.h:40
pci_bar_t::base
uint64_t base
Definition
pci.h:39
pci_bar_t::is_64
bool is_64
Definition
pci.h:42
pci_bar_t::is_io
bool is_io
Definition
pci.h:41
pci_device_t
Definition
pci.h:47
pci_device_t::revision
uint8_t revision
Definition
pci.h:56
pci_device_t::irq_pin
uint8_t irq_pin
Definition
pci.h:59
pci_device_t::msi_offset
uint8_t msi_offset
Definition
pci.h:60
pci_device_t::device
uint8_t device
Definition
pci.h:49
pci_device_t::msix_offset
uint8_t msix_offset
Definition
pci.h:61
pci_device_t::function
uint8_t function
Definition
pci.h:50
pci_device_t::vendor_id
uint16_t vendor_id
Definition
pci.h:51
pci_device_t::irq_line
uint8_t irq_line
Definition
pci.h:58
pci_device_t::header_type
uint8_t header_type
Definition
pci.h:57
pci_device_t::device_id
uint16_t device_id
Definition
pci.h:52
pci_device_t::class_code
uint8_t class_code
Definition
pci.h:53
pci_device_t::bars
pci_bar_t bars[PCI_BAR_COUNT]
Definition
pci.h:62
pci_device_t::prog_if
uint8_t prog_if
Definition
pci.h:55
pci_device_t::subclass
uint8_t subclass
Definition
pci.h:54
pci_device_t::bus
uint8_t bus
Definition
pci.h:48
src
drivers
pci
pci.h
Generated by
1.18.0