Skip to content

Commit 1302e45

Browse files
wipawelbjoernd
authored andcommitted
pci: add pci config space read/write helpers
Add legacy (IO ports based) access mechanism to PCI configuration space. The helpers allow to read/write 8-bit, 16-bit and 32-bit values. Separate helper pci_cfg_set_addr() is used for address selection. Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
1 parent 1c794c6 commit 1302e45

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

include/arch/x86/pci_cfg.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright © 2021 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
#ifndef KTF_PCI_CFG_H
26+
#define KTF_PCI_CFG_H
27+
28+
#include <ktf.h>
29+
#include <lib.h>
30+
31+
#define PCI_IO_PORT_ADDRESS 0xcf8
32+
#define PCI_IO_PORT_DATA 0xcfc
33+
34+
union pci_cfg_addr {
35+
struct {
36+
uint32_t zero : 2, reg : 6, fn : 3, dev : 5, bus : 8, rsvd : 7, ecsm : 1;
37+
} __packed;
38+
uint32_t val;
39+
};
40+
typedef union pci_cfg_addr pci_cfg_addr_t;
41+
42+
union pci_cfg_value {
43+
uint8_t byte[4];
44+
uint16_t word[2];
45+
uint16_t dword;
46+
};
47+
typedef union pci_cfg_value pci_cfg_value_t;
48+
49+
/* Static declarations */
50+
51+
static inline void pci_cfg_set_addr(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) {
52+
pci_cfg_addr_t addr;
53+
54+
addr.val = 0;
55+
addr.reg = reg;
56+
addr.fn = func;
57+
addr.dev = dev;
58+
addr.bus = bus;
59+
60+
outd(PCI_IO_PORT_ADDRESS, addr.val);
61+
}
62+
63+
static inline uint8_t pci_cfg_read8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) {
64+
pci_cfg_set_addr(bus, dev, func, reg);
65+
return inb(PCI_IO_PORT_DATA);
66+
}
67+
68+
static inline void pci_cfg_write8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg,
69+
uint8_t value) {
70+
pci_cfg_set_addr(bus, dev, func, reg);
71+
outb(PCI_IO_PORT_DATA, value);
72+
}
73+
74+
static inline uint16_t pci_cfg_read16(uint8_t bus, uint8_t dev, uint8_t func,
75+
uint8_t reg) {
76+
pci_cfg_set_addr(bus, dev, func, reg);
77+
return inw(PCI_IO_PORT_DATA);
78+
}
79+
80+
static inline void pci_cfg_write16(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg,
81+
uint16_t value) {
82+
pci_cfg_set_addr(bus, dev, func, reg);
83+
outw(PCI_IO_PORT_DATA, value);
84+
}
85+
86+
static inline uint32_t pci_cfg_read(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) {
87+
pci_cfg_set_addr(bus, dev, func, reg);
88+
return ind(PCI_IO_PORT_DATA);
89+
}
90+
91+
static inline void pci_cfg_write(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg,
92+
uint32_t value) {
93+
pci_cfg_set_addr(bus, dev, func, reg);
94+
outd(PCI_IO_PORT_DATA, value);
95+
}
96+
97+
/* External declarations */
98+
99+
#endif /* KTF_PCI_CFG_H */

0 commit comments

Comments
 (0)