root / hw / pcie.c @ 5afb9869
History | View | Annotate | Download (18.8 kB)
1 |
/*
|
---|---|
2 |
* pcie.c
|
3 |
*
|
4 |
* Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
|
5 |
* VA Linux Systems Japan K.K.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or modify
|
8 |
* it under the terms of the GNU General Public License as published by
|
9 |
* the Free Software Foundation; either version 2 of the License, or
|
10 |
* (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License along
|
18 |
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
|
21 |
#include "sysemu.h" |
22 |
#include "pci_bridge.h" |
23 |
#include "pcie.h" |
24 |
#include "msix.h" |
25 |
#include "msi.h" |
26 |
#include "pci_internals.h" |
27 |
#include "pcie_regs.h" |
28 |
#include "range.h" |
29 |
|
30 |
//#define DEBUG_PCIE
|
31 |
#ifdef DEBUG_PCIE
|
32 |
# define PCIE_DPRINTF(fmt, ...) \
|
33 |
fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__) |
34 |
#else
|
35 |
# define PCIE_DPRINTF(fmt, ...) do {} while (0) |
36 |
#endif
|
37 |
#define PCIE_DEV_PRINTF(dev, fmt, ...) \
|
38 |
PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__) |
39 |
|
40 |
|
41 |
/***************************************************************************
|
42 |
* pci express capability helper functions
|
43 |
*/
|
44 |
int pcie_cap_init(PCIDevice *dev, uint8_t offset, uint8_t type, uint8_t port)
|
45 |
{ |
46 |
int pos;
|
47 |
uint8_t *exp_cap; |
48 |
|
49 |
assert(pci_is_express(dev)); |
50 |
|
51 |
pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset, |
52 |
PCI_EXP_VER2_SIZEOF); |
53 |
if (pos < 0) { |
54 |
return pos;
|
55 |
} |
56 |
dev->exp.exp_cap = pos; |
57 |
exp_cap = dev->config + pos; |
58 |
|
59 |
/* capability register
|
60 |
interrupt message number defaults to 0 */
|
61 |
pci_set_word(exp_cap + PCI_EXP_FLAGS, |
62 |
((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) | |
63 |
PCI_EXP_FLAGS_VER2); |
64 |
|
65 |
/* device capability register
|
66 |
* table 7-12:
|
67 |
* roll based error reporting bit must be set by all
|
68 |
* Functions conforming to the ECN, PCI Express Base
|
69 |
* Specification, Revision 1.1., or subsequent PCI Express Base
|
70 |
* Specification revisions.
|
71 |
*/
|
72 |
pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER); |
73 |
|
74 |
pci_set_long(exp_cap + PCI_EXP_LNKCAP, |
75 |
(port << PCI_EXP_LNKCAP_PN_SHIFT) | |
76 |
PCI_EXP_LNKCAP_ASPMS_0S | |
77 |
PCI_EXP_LNK_MLW_1 | |
78 |
PCI_EXP_LNK_LS_25); |
79 |
|
80 |
pci_set_word(exp_cap + PCI_EXP_LNKSTA, |
81 |
PCI_EXP_LNK_MLW_1 | PCI_EXP_LNK_LS_25); |
82 |
|
83 |
pci_set_long(exp_cap + PCI_EXP_DEVCAP2, |
84 |
PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP); |
85 |
|
86 |
pci_set_word(dev->wmask + pos, PCI_EXP_DEVCTL2_EETLPPB); |
87 |
return pos;
|
88 |
} |
89 |
|
90 |
void pcie_cap_exit(PCIDevice *dev)
|
91 |
{ |
92 |
pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF); |
93 |
} |
94 |
|
95 |
uint8_t pcie_cap_get_type(const PCIDevice *dev)
|
96 |
{ |
97 |
uint32_t pos = dev->exp.exp_cap; |
98 |
assert(pos > 0);
|
99 |
return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
|
100 |
PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT; |
101 |
} |
102 |
|
103 |
/* MSI/MSI-X */
|
104 |
/* pci express interrupt message number */
|
105 |
/* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
|
106 |
void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
|
107 |
{ |
108 |
uint8_t *exp_cap = dev->config + dev->exp.exp_cap; |
109 |
assert(vector < 32);
|
110 |
pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ); |
111 |
pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS, |
112 |
vector << PCI_EXP_FLAGS_IRQ_SHIFT); |
113 |
} |
114 |
|
115 |
uint8_t pcie_cap_flags_get_vector(PCIDevice *dev) |
116 |
{ |
117 |
return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
|
118 |
PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT; |
119 |
} |
120 |
|
121 |
void pcie_cap_deverr_init(PCIDevice *dev)
|
122 |
{ |
123 |
uint32_t pos = dev->exp.exp_cap; |
124 |
pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP, |
125 |
PCI_EXP_DEVCAP_RBER); |
126 |
pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL, |
127 |
PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | |
128 |
PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE); |
129 |
pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA, |
130 |
PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED | |
131 |
PCI_EXP_DEVSTA_URD | PCI_EXP_DEVSTA_URD); |
132 |
} |
133 |
|
134 |
void pcie_cap_deverr_reset(PCIDevice *dev)
|
135 |
{ |
136 |
uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL; |
137 |
pci_long_test_and_clear_mask(devctl, |
138 |
PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | |
139 |
PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE); |
140 |
} |
141 |
|
142 |
/*
|
143 |
* A PCI Express Hot-Plug Event has occured, so update slot status register
|
144 |
* and notify OS of the event if necessary.
|
145 |
*
|
146 |
* 6.7.3 PCI Express Hot-Plug Events
|
147 |
* 6.7.3.4 Software Notification of Hot-Plug Events
|
148 |
*/
|
149 |
static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event) |
150 |
{ |
151 |
uint8_t *exp_cap = dev->config + dev->exp.exp_cap; |
152 |
uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL); |
153 |
uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA); |
154 |
|
155 |
PCIE_DEV_PRINTF(dev, |
156 |
"sltctl: 0x%02"PRIx16" sltsta: 0x%02"PRIx16" event: %x\n", |
157 |
sltctl, sltsta, event); |
158 |
|
159 |
if (pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA, event)) {
|
160 |
return;
|
161 |
} |
162 |
sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA); |
163 |
PCIE_DEV_PRINTF(dev, "sltsta -> %02"PRIx16"\n", sltsta); |
164 |
|
165 |
if ((sltctl & PCI_EXP_SLTCTL_HPIE) &&
|
166 |
(sltctl & event & PCI_EXP_HP_EV_SUPPORTED)) { |
167 |
if (pci_msi_enabled(dev)) {
|
168 |
pci_msi_notify(dev, pcie_cap_flags_get_vector(dev)); |
169 |
} else {
|
170 |
qemu_set_irq(dev->irq[dev->exp.hpev_intx], 1);
|
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
static int pcie_cap_slot_hotplug(DeviceState *qdev, |
176 |
PCIDevice *pci_dev, int state)
|
177 |
{ |
178 |
PCIDevice *d = DO_UPCAST(PCIDevice, qdev, qdev); |
179 |
uint8_t *exp_cap = d->config + d->exp.exp_cap; |
180 |
uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA); |
181 |
|
182 |
if (!pci_dev->qdev.hotplugged) {
|
183 |
assert(state); /* this case only happens at machine creation. */
|
184 |
pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA, |
185 |
PCI_EXP_SLTSTA_PDS); |
186 |
return 0; |
187 |
} |
188 |
|
189 |
PCIE_DEV_PRINTF(pci_dev, "hotplug state: %d\n", state);
|
190 |
if (sltsta & PCI_EXP_SLTSTA_EIS) {
|
191 |
/* the slot is electromechanically locked.
|
192 |
* This error is propagated up to qdev and then to HMP/QMP.
|
193 |
*/
|
194 |
return -EBUSY;
|
195 |
} |
196 |
|
197 |
/* TODO: multifunction hot-plug.
|
198 |
* Right now, only a device of function = 0 is allowed to be
|
199 |
* hot plugged/unplugged.
|
200 |
*/
|
201 |
assert(PCI_FUNC(pci_dev->devfn) == 0);
|
202 |
|
203 |
if (state) {
|
204 |
pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA, |
205 |
PCI_EXP_SLTSTA_PDS); |
206 |
pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC); |
207 |
} else {
|
208 |
qdev_free(&pci_dev->qdev); |
209 |
pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA, |
210 |
PCI_EXP_SLTSTA_PDS); |
211 |
pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC); |
212 |
} |
213 |
return 0; |
214 |
} |
215 |
|
216 |
/* pci express slot for pci express root/downstream port
|
217 |
PCI express capability slot registers */
|
218 |
void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
|
219 |
{ |
220 |
uint32_t pos = dev->exp.exp_cap; |
221 |
|
222 |
pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS, |
223 |
PCI_EXP_FLAGS_SLOT); |
224 |
|
225 |
pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP, |
226 |
~PCI_EXP_SLTCAP_PSN); |
227 |
pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP, |
228 |
(slot << PCI_EXP_SLTCAP_PSN_SHIFT) | |
229 |
PCI_EXP_SLTCAP_EIP | |
230 |
PCI_EXP_SLTCAP_HPS | |
231 |
PCI_EXP_SLTCAP_HPC | |
232 |
PCI_EXP_SLTCAP_PIP | |
233 |
PCI_EXP_SLTCAP_AIP | |
234 |
PCI_EXP_SLTCAP_ABP); |
235 |
|
236 |
pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL, |
237 |
PCI_EXP_SLTCTL_PIC | |
238 |
PCI_EXP_SLTCTL_AIC); |
239 |
pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL, |
240 |
PCI_EXP_SLTCTL_PIC_OFF | |
241 |
PCI_EXP_SLTCTL_AIC_OFF); |
242 |
pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL, |
243 |
PCI_EXP_SLTCTL_PIC | |
244 |
PCI_EXP_SLTCTL_AIC | |
245 |
PCI_EXP_SLTCTL_HPIE | |
246 |
PCI_EXP_SLTCTL_CCIE | |
247 |
PCI_EXP_SLTCTL_PDCE | |
248 |
PCI_EXP_SLTCTL_ABPE); |
249 |
/* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
|
250 |
* make the bit writable here in order to detect 1b is written.
|
251 |
* pcie_cap_slot_write_config() test-and-clear the bit, so
|
252 |
* this bit always returns 0 to the guest.
|
253 |
*/
|
254 |
pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL, |
255 |
PCI_EXP_SLTCTL_EIC); |
256 |
|
257 |
pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA, |
258 |
PCI_EXP_HP_EV_SUPPORTED); |
259 |
|
260 |
pci_bus_hotplug(pci_bridge_get_sec_bus(DO_UPCAST(PCIBridge, dev, dev)), |
261 |
pcie_cap_slot_hotplug, &dev->qdev); |
262 |
} |
263 |
|
264 |
void pcie_cap_slot_reset(PCIDevice *dev)
|
265 |
{ |
266 |
uint8_t *exp_cap = dev->config + dev->exp.exp_cap; |
267 |
|
268 |
PCIE_DEV_PRINTF(dev, "reset\n");
|
269 |
|
270 |
pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL, |
271 |
PCI_EXP_SLTCTL_EIC | |
272 |
PCI_EXP_SLTCTL_PIC | |
273 |
PCI_EXP_SLTCTL_AIC | |
274 |
PCI_EXP_SLTCTL_HPIE | |
275 |
PCI_EXP_SLTCTL_CCIE | |
276 |
PCI_EXP_SLTCTL_PDCE | |
277 |
PCI_EXP_SLTCTL_ABPE); |
278 |
pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL, |
279 |
PCI_EXP_SLTCTL_PIC_OFF | |
280 |
PCI_EXP_SLTCTL_AIC_OFF); |
281 |
|
282 |
pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA, |
283 |
PCI_EXP_SLTSTA_EIS |/* on reset,
|
284 |
the lock is released */
|
285 |
PCI_EXP_SLTSTA_CC | |
286 |
PCI_EXP_SLTSTA_PDC | |
287 |
PCI_EXP_SLTSTA_ABP); |
288 |
} |
289 |
|
290 |
void pcie_cap_slot_write_config(PCIDevice *dev,
|
291 |
uint32_t addr, uint32_t val, int len,
|
292 |
uint16_t sltctl_prev) |
293 |
{ |
294 |
uint32_t pos = dev->exp.exp_cap; |
295 |
uint8_t *exp_cap = dev->config + pos; |
296 |
uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL); |
297 |
uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA); |
298 |
|
299 |
PCIE_DEV_PRINTF(dev, |
300 |
"addr: 0x%"PRIx32" val: 0x%"PRIx32" len: %d\n" |
301 |
"\tsltctl_prev: 0x%02"PRIx16" sltctl: 0x%02"PRIx16 |
302 |
" sltsta: 0x%02"PRIx16"\n", |
303 |
addr, val, len, sltctl_prev, sltctl, sltsta); |
304 |
|
305 |
/* SLTCTL */
|
306 |
if (ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) { |
307 |
PCIE_DEV_PRINTF(dev, "sltctl: 0x%02"PRIx16" -> 0x%02"PRIx16"\n", |
308 |
sltctl_prev, sltctl); |
309 |
if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
|
310 |
PCI_EXP_SLTCTL_EIC)) { |
311 |
sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
|
312 |
pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta); |
313 |
PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
|
314 |
"sltsta -> 0x%02"PRIx16"\n", |
315 |
sltsta); |
316 |
} |
317 |
|
318 |
/*
|
319 |
* The events control bits might be enabled or disabled,
|
320 |
* Check if the software notificastion condition is satisfied
|
321 |
* or disatisfied.
|
322 |
*
|
323 |
* 6.7.3.4 Software Notification of Hot-plug events
|
324 |
*/
|
325 |
if (pci_msi_enabled(dev)) {
|
326 |
bool msi_trigger =
|
327 |
(sltctl & PCI_EXP_SLTCTL_HPIE) && |
328 |
((sltctl_prev ^ sltctl) & sltctl & /* stlctl: 0 -> 1 */
|
329 |
sltsta & PCI_EXP_HP_EV_SUPPORTED); |
330 |
if (msi_trigger) {
|
331 |
pci_msi_notify(dev, pcie_cap_flags_get_vector(dev)); |
332 |
} |
333 |
} else {
|
334 |
int int_level =
|
335 |
(sltctl & PCI_EXP_SLTCTL_HPIE) && |
336 |
(sltctl & sltsta & PCI_EXP_HP_EV_SUPPORTED); |
337 |
qemu_set_irq(dev->irq[dev->exp.hpev_intx], int_level); |
338 |
} |
339 |
|
340 |
if (!((sltctl_prev ^ sltctl) & PCI_EXP_SLTCTL_SUPPORTED)) {
|
341 |
PCIE_DEV_PRINTF(dev, |
342 |
"sprious command completion slctl "
|
343 |
"0x%"PRIx16" -> 0x%"PRIx16"\n", |
344 |
sltctl_prev, sltctl); |
345 |
} |
346 |
|
347 |
/* command completion.
|
348 |
* Real hardware might take a while to complete
|
349 |
* requested command because physical movement would be involved
|
350 |
* like locking the electromechanical lock.
|
351 |
* However in our case, command is completed instantaneously above,
|
352 |
* so send a command completion event right now.
|
353 |
*
|
354 |
* 6.7.3.2 Command Completed Events
|
355 |
*/
|
356 |
/* set command completed bit */
|
357 |
pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI); |
358 |
} |
359 |
} |
360 |
|
361 |
void pcie_cap_slot_push_attention_button(PCIDevice *dev)
|
362 |
{ |
363 |
pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP); |
364 |
} |
365 |
|
366 |
/* root control/capabilities/status. PME isn't emulated for now */
|
367 |
void pcie_cap_root_init(PCIDevice *dev)
|
368 |
{ |
369 |
pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL, |
370 |
PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE | |
371 |
PCI_EXP_RTCTL_SEFEE); |
372 |
} |
373 |
|
374 |
void pcie_cap_root_reset(PCIDevice *dev)
|
375 |
{ |
376 |
pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
|
377 |
} |
378 |
|
379 |
/*
|
380 |
* TODO: implement FLR:
|
381 |
* Right now sets the bit which indicates FLR is supported.
|
382 |
*/
|
383 |
/* function level reset(FLR) */
|
384 |
void pcie_cap_flr_init(PCIDevice *dev)
|
385 |
{ |
386 |
pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP, |
387 |
PCI_EXP_DEVCAP_FLR); |
388 |
|
389 |
/* Although reading BCR_FLR returns always 0,
|
390 |
* the bit is made writable here in order to detect the 1b is written
|
391 |
* pcie_cap_flr_write_config() test-and-clear the bit, so
|
392 |
* this bit always returns 0 to the guest.
|
393 |
*/
|
394 |
pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL, |
395 |
PCI_EXP_DEVCTL_BCR_FLR); |
396 |
} |
397 |
|
398 |
void pcie_cap_flr_write_config(PCIDevice *dev,
|
399 |
uint32_t addr, uint32_t val, int len)
|
400 |
{ |
401 |
uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL; |
402 |
if (pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR)) {
|
403 |
/* TODO: implement FLR */
|
404 |
} |
405 |
} |
406 |
|
407 |
/* Alternative Routing-ID Interpretation (ARI) */
|
408 |
/* ari forwarding support for down stream port */
|
409 |
void pcie_cap_ari_init(PCIDevice *dev)
|
410 |
{ |
411 |
uint32_t pos = dev->exp.exp_cap; |
412 |
pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2, |
413 |
PCI_EXP_DEVCAP2_ARI); |
414 |
pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2, |
415 |
PCI_EXP_DEVCTL2_ARI); |
416 |
} |
417 |
|
418 |
void pcie_cap_ari_reset(PCIDevice *dev)
|
419 |
{ |
420 |
uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2; |
421 |
pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI); |
422 |
} |
423 |
|
424 |
bool pcie_cap_is_ari_enabled(const PCIDevice *dev) |
425 |
{ |
426 |
if (!pci_is_express(dev)) {
|
427 |
return false; |
428 |
} |
429 |
if (!dev->exp.exp_cap) {
|
430 |
return false; |
431 |
} |
432 |
|
433 |
return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
|
434 |
PCI_EXP_DEVCTL2_ARI; |
435 |
} |
436 |
|
437 |
/**************************************************************************
|
438 |
* pci express extended capability allocation functions
|
439 |
* uint16_t ext_cap_id (16 bit)
|
440 |
* uint8_t cap_ver (4 bit)
|
441 |
* uint16_t cap_offset (12 bit)
|
442 |
* uint16_t ext_cap_size
|
443 |
*/
|
444 |
|
445 |
static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
|
446 |
uint16_t *prev_p) |
447 |
{ |
448 |
uint16_t prev = 0;
|
449 |
uint16_t next; |
450 |
uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE); |
451 |
|
452 |
if (!header) {
|
453 |
/* no extended capability */
|
454 |
next = 0;
|
455 |
goto out;
|
456 |
} |
457 |
for (next = PCI_CONFIG_SPACE_SIZE; next;
|
458 |
prev = next, next = PCI_EXT_CAP_NEXT(header)) { |
459 |
|
460 |
assert(next >= PCI_CONFIG_SPACE_SIZE); |
461 |
assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
|
462 |
|
463 |
header = pci_get_long(dev->config + next); |
464 |
if (PCI_EXT_CAP_ID(header) == cap_id) {
|
465 |
break;
|
466 |
} |
467 |
} |
468 |
|
469 |
out:
|
470 |
if (prev_p) {
|
471 |
*prev_p = prev; |
472 |
} |
473 |
return next;
|
474 |
} |
475 |
|
476 |
uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id) |
477 |
{ |
478 |
return pcie_find_capability_list(dev, cap_id, NULL); |
479 |
} |
480 |
|
481 |
static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next) |
482 |
{ |
483 |
uint16_t header = pci_get_long(dev->config + pos); |
484 |
assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
|
485 |
header = (header & ~PCI_EXT_CAP_NEXT_MASK) | |
486 |
((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK); |
487 |
pci_set_long(dev->config + pos, header); |
488 |
} |
489 |
|
490 |
/*
|
491 |
* caller must supply valid (offset, size) * such that the range shouldn't
|
492 |
* overlap with other capability or other registers.
|
493 |
* This function doesn't check it.
|
494 |
*/
|
495 |
void pcie_add_capability(PCIDevice *dev,
|
496 |
uint16_t cap_id, uint8_t cap_ver, |
497 |
uint16_t offset, uint16_t size) |
498 |
{ |
499 |
uint32_t header; |
500 |
uint16_t next; |
501 |
|
502 |
assert(offset >= PCI_CONFIG_SPACE_SIZE); |
503 |
assert(offset < offset + size); |
504 |
assert(offset + size < PCIE_CONFIG_SPACE_SIZE); |
505 |
assert(size >= 8);
|
506 |
assert(pci_is_express(dev)); |
507 |
|
508 |
if (offset == PCI_CONFIG_SPACE_SIZE) {
|
509 |
header = pci_get_long(dev->config + offset); |
510 |
next = PCI_EXT_CAP_NEXT(header); |
511 |
} else {
|
512 |
uint16_t prev; |
513 |
|
514 |
/* 0 is reserved cap id. use internally to find the last capability
|
515 |
in the linked list */
|
516 |
next = pcie_find_capability_list(dev, 0, &prev);
|
517 |
|
518 |
assert(prev >= PCI_CONFIG_SPACE_SIZE); |
519 |
assert(next == 0);
|
520 |
pcie_ext_cap_set_next(dev, prev, offset); |
521 |
} |
522 |
pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next)); |
523 |
|
524 |
/* Make capability read-only by default */
|
525 |
memset(dev->wmask + offset, 0, size);
|
526 |
memset(dev->w1cmask + offset, 0, size);
|
527 |
/* Check capability by default */
|
528 |
memset(dev->cmask + offset, 0xFF, size);
|
529 |
} |
530 |
|
531 |
/**************************************************************************
|
532 |
* pci express extended capability helper functions
|
533 |
*/
|
534 |
|
535 |
/* ARI */
|
536 |
void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
|
537 |
{ |
538 |
pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER, |
539 |
offset, PCI_ARI_SIZEOF); |
540 |
pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn)); |
541 |
} |