forked from TrenchBoot/secure-kernel-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pci.c
213 lines (181 loc) · 5.19 KB
/
pci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* These bits are from Linux. Copyrights, where present, come from the
* files the definitions came from. Code in this module is from
* arch/x86/pci/direct.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <defs.h>
#include <types.h>
#include <errno-base.h>
#include <boot.h>
#include <pci.h>
int (*pci_read)(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len, u32 *value);
int (*pci_write)(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len, u32 value);
/*
* Functions for accessing PCI base (first 256 bytes) and extended
* (4096 bytes per PCI function) configuration space with type 1
* accesses.
*/
#define PCI_CONF1_ADDRESS(bus, devfn, reg) \
(0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
| (devfn << 8) | (reg & 0xFC))
static int pci_conf1_read(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len,
u32 *value)
{
if ( seg || (bus > 255) || (devfn > 255) || (reg > 4095) )
{
*value = -1;
return -EINVAL;
}
outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
switch ( len )
{
case 1:
*value = inb(0xCFC + (reg & 3));
break;
case 2:
*value = inw(0xCFC + (reg & 2));
break;
case 3:
*value = inw(0xCFC);
break;
case 4:
*value = inl(0xCFC);
break;
}
return 0;
}
static int pci_conf1_write(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len,
u32 value)
{
if ( seg || (bus > 255) || (devfn > 255) || (reg > 4095) )
return -EINVAL;
outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
switch ( len )
{
case 1:
outb((u8)value, 0xCFC + (reg & 3));
break;
case 2:
outw((u16)value, 0xCFC + (reg & 2));
break;
case 3:
outw((u16)value, 0xCFC);
break;
case 4:
outl((u32)value, 0xCFC);
break;
}
return 0;
}
u32 mmio_base_addr;
#define PCI_MMIO_ADDRESS(bus, devfn, reg) \
_p(mmio_base_addr | (bus << 20ULL) | (devfn << 12ULL) | reg)
static int pci_mmio_read(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len, u32 *value)
{
if ( seg || (bus > 255) || (devfn > 255) || (reg > 4095) )
{
*value = -1;
return -EINVAL;
}
void *addr = PCI_MMIO_ADDRESS(bus, devfn, reg);
switch ( len )
{
case 1:
*value = ioread8(addr);
break;
case 2:
*value = ioread16((void *)((size_t)addr & ~1UL));
break;
case 3:
*value = ioread16((void *)((size_t)addr & ~3UL));
break;
case 4:
*value = ioread32((void *)((size_t)addr & ~3UL));
break;
}
return 0;
}
static int pci_mmio_write(unsigned int seg, unsigned int bus,
unsigned int devfn, int reg, int len, u32 value)
{
if ( seg || (bus > 255) || (devfn > 255) || (reg > 4095) )
return -EINVAL;
void *addr = PCI_MMIO_ADDRESS(bus, devfn, reg);
switch ( len )
{
case 1:
iowrite8((u8)value, addr);
break;
case 2:
iowrite16((u16)value, (void *)((size_t)addr & ~1UL));
break;
case 3:
iowrite16((u16)value, (void *)((size_t)addr & ~3UL));
break;
case 4:
iowrite32((u32)value, (void *)((size_t)addr & ~3UL));
break;
}
return 0;
}
u32 pci_locate(unsigned int bus, unsigned int devfn)
{
u32 pci_cap_ptr;
u32 next;
/* Read capabilities pointer */
pci_read(0, bus,
devfn,
PCI_CAPABILITY_LIST,
4, &pci_cap_ptr);
if ( INVALID_CAP(pci_cap_ptr) )
return 0;
pci_cap_ptr &= 0xFF;
while ( pci_cap_ptr != 0 )
{
pci_read(0, bus,
devfn,
pci_cap_ptr,
4, &next);
if ( PCI_CAP_ID(next) == PCI_CAPABILITIES_POINTER_ID_DEV )
break;
pci_cap_ptr = PCI_CAP_PTR(next);
}
if ( INVALID_CAP(pci_cap_ptr) )
return 0;
return pci_cap_ptr;
}
void pci_init(void)
{
u32 eax, edx;
asm volatile("rdmsr" : "=a"(eax), "=d"(edx) : "c"(0xc0010058));
if ( eax & 1 ) /* MMIO configuration space is enabled */
{
mmio_base_addr = (eax & 0xfff00000);
pci_read = &pci_mmio_read;
pci_write = &pci_mmio_write;
}
else
{
pci_read = &pci_conf1_read;
pci_write = &pci_conf1_write;
}
}