4 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu-common.h"
24 #include "hw/pci/pci_bridge.h"
25 #include "hw/pci/pcie.h"
26 #include "hw/pci/msix.h"
27 #include "hw/pci/msi.h"
28 #include "hw/pci/pci_bus.h"
29 #include "hw/pci/pcie_regs.h"
30 #include "qemu/range.h"
34 # define PCIE_DPRINTF(fmt, ...) \
35 fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
37 # define PCIE_DPRINTF(fmt, ...) do {} while (0)
39 #define PCIE_DEV_PRINTF(dev, fmt, ...) \
40 PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
43 /***************************************************************************
44 * pci express capability helper functions
48 pcie_cap_v1_fill(PCIDevice
*dev
, uint8_t port
, uint8_t type
, uint8_t version
)
50 uint8_t *exp_cap
= dev
->config
+ dev
->exp
.exp_cap
;
51 uint8_t *cmask
= dev
->cmask
+ dev
->exp
.exp_cap
;
53 /* capability register
54 interrupt message number defaults to 0 */
55 pci_set_word(exp_cap
+ PCI_EXP_FLAGS
,
56 ((type
<< PCI_EXP_FLAGS_TYPE_SHIFT
) & PCI_EXP_FLAGS_TYPE
) |
59 /* device capability register
61 * roll based error reporting bit must be set by all
62 * Functions conforming to the ECN, PCI Express Base
63 * Specification, Revision 1.1., or subsequent PCI Express Base
64 * Specification revisions.
66 pci_set_long(exp_cap
+ PCI_EXP_DEVCAP
, PCI_EXP_DEVCAP_RBER
);
68 pci_set_long(exp_cap
+ PCI_EXP_LNKCAP
,
69 (port
<< PCI_EXP_LNKCAP_PN_SHIFT
) |
70 PCI_EXP_LNKCAP_ASPMS_0S
|
74 pci_set_word(exp_cap
+ PCI_EXP_LNKSTA
,
75 PCI_EXP_LNK_MLW_1
| PCI_EXP_LNK_LS_25
);
77 if (dev
->cap_present
& QEMU_PCIE_LNKSTA_DLLLA
) {
78 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_LNKSTA
,
79 PCI_EXP_LNKSTA_DLLLA
);
82 /* We changed link status bits over time, and changing them across
83 * migrations is generally fine as hardware changes them too.
84 * Let's not bother checking.
86 pci_set_word(cmask
+ PCI_EXP_LNKSTA
, 0);
89 int pcie_cap_init(PCIDevice
*dev
, uint8_t offset
,
90 uint8_t type
, uint8_t port
,
93 /* PCIe cap v2 init */
97 assert(pci_is_express(dev
));
99 pos
= pci_add_capability(dev
, PCI_CAP_ID_EXP
, offset
,
100 PCI_EXP_VER2_SIZEOF
, errp
);
104 dev
->exp
.exp_cap
= pos
;
105 exp_cap
= dev
->config
+ pos
;
107 /* Filling values common with v1 */
108 pcie_cap_v1_fill(dev
, port
, type
, PCI_EXP_FLAGS_VER2
);
110 /* Filling v2 specific values */
111 pci_set_long(exp_cap
+ PCI_EXP_DEVCAP2
,
112 PCI_EXP_DEVCAP2_EFF
| PCI_EXP_DEVCAP2_EETLPP
);
114 pci_set_word(dev
->wmask
+ pos
+ PCI_EXP_DEVCTL2
, PCI_EXP_DEVCTL2_EETLPPB
);
116 if (dev
->cap_present
& QEMU_PCIE_EXTCAP_INIT
) {
117 /* read-only to behave like a 'NULL' Extended Capability Header */
118 pci_set_long(dev
->wmask
+ PCI_CONFIG_SPACE_SIZE
, 0);
124 int pcie_cap_v1_init(PCIDevice
*dev
, uint8_t offset
, uint8_t type
,
127 /* PCIe cap v1 init */
129 Error
*local_err
= NULL
;
131 assert(pci_is_express(dev
));
133 pos
= pci_add_capability(dev
, PCI_CAP_ID_EXP
, offset
,
134 PCI_EXP_VER1_SIZEOF
, &local_err
);
136 error_report_err(local_err
);
139 dev
->exp
.exp_cap
= pos
;
141 pcie_cap_v1_fill(dev
, port
, type
, PCI_EXP_FLAGS_VER1
);
147 pcie_endpoint_cap_common_init(PCIDevice
*dev
, uint8_t offset
, uint8_t cap_size
)
149 uint8_t type
= PCI_EXP_TYPE_ENDPOINT
;
150 Error
*local_err
= NULL
;
154 * Windows guests will report Code 10, device cannot start, if
155 * a regular Endpoint type is exposed on a root complex. These
156 * should instead be Root Complex Integrated Endpoints.
158 if (pci_bus_is_express(dev
->bus
) && pci_bus_is_root(dev
->bus
)) {
159 type
= PCI_EXP_TYPE_RC_END
;
162 if (cap_size
== PCI_EXP_VER1_SIZEOF
) {
163 return pcie_cap_v1_init(dev
, offset
, type
, 0);
165 ret
= pcie_cap_init(dev
, offset
, type
, 0, &local_err
);
168 error_report_err(local_err
);
175 int pcie_endpoint_cap_init(PCIDevice
*dev
, uint8_t offset
)
177 return pcie_endpoint_cap_common_init(dev
, offset
, PCI_EXP_VER2_SIZEOF
);
180 int pcie_endpoint_cap_v1_init(PCIDevice
*dev
, uint8_t offset
)
182 return pcie_endpoint_cap_common_init(dev
, offset
, PCI_EXP_VER1_SIZEOF
);
185 void pcie_cap_exit(PCIDevice
*dev
)
187 pci_del_capability(dev
, PCI_CAP_ID_EXP
, PCI_EXP_VER2_SIZEOF
);
190 void pcie_cap_v1_exit(PCIDevice
*dev
)
192 pci_del_capability(dev
, PCI_CAP_ID_EXP
, PCI_EXP_VER1_SIZEOF
);
195 uint8_t pcie_cap_get_type(const PCIDevice
*dev
)
197 uint32_t pos
= dev
->exp
.exp_cap
;
199 return (pci_get_word(dev
->config
+ pos
+ PCI_EXP_FLAGS
) &
200 PCI_EXP_FLAGS_TYPE
) >> PCI_EXP_FLAGS_TYPE_SHIFT
;
204 /* pci express interrupt message number */
205 /* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
206 void pcie_cap_flags_set_vector(PCIDevice
*dev
, uint8_t vector
)
208 uint8_t *exp_cap
= dev
->config
+ dev
->exp
.exp_cap
;
210 pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_FLAGS
, PCI_EXP_FLAGS_IRQ
);
211 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_FLAGS
,
212 vector
<< PCI_EXP_FLAGS_IRQ_SHIFT
);
215 uint8_t pcie_cap_flags_get_vector(PCIDevice
*dev
)
217 return (pci_get_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_FLAGS
) &
218 PCI_EXP_FLAGS_IRQ
) >> PCI_EXP_FLAGS_IRQ_SHIFT
;
221 void pcie_cap_deverr_init(PCIDevice
*dev
)
223 uint32_t pos
= dev
->exp
.exp_cap
;
224 pci_long_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_DEVCAP
,
225 PCI_EXP_DEVCAP_RBER
);
226 pci_long_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_DEVCTL
,
227 PCI_EXP_DEVCTL_CERE
| PCI_EXP_DEVCTL_NFERE
|
228 PCI_EXP_DEVCTL_FERE
| PCI_EXP_DEVCTL_URRE
);
229 pci_long_test_and_set_mask(dev
->w1cmask
+ pos
+ PCI_EXP_DEVSTA
,
230 PCI_EXP_DEVSTA_CED
| PCI_EXP_DEVSTA_NFED
|
231 PCI_EXP_DEVSTA_FED
| PCI_EXP_DEVSTA_URD
);
234 void pcie_cap_deverr_reset(PCIDevice
*dev
)
236 uint8_t *devctl
= dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL
;
237 pci_long_test_and_clear_mask(devctl
,
238 PCI_EXP_DEVCTL_CERE
| PCI_EXP_DEVCTL_NFERE
|
239 PCI_EXP_DEVCTL_FERE
| PCI_EXP_DEVCTL_URRE
);
242 void pcie_cap_lnkctl_init(PCIDevice
*dev
)
244 uint32_t pos
= dev
->exp
.exp_cap
;
245 pci_long_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_LNKCTL
,
246 PCI_EXP_LNKCTL_CCC
| PCI_EXP_LNKCTL_ES
);
249 void pcie_cap_lnkctl_reset(PCIDevice
*dev
)
251 uint8_t *lnkctl
= dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_LNKCTL
;
252 pci_long_test_and_clear_mask(lnkctl
,
253 PCI_EXP_LNKCTL_CCC
| PCI_EXP_LNKCTL_ES
);
256 static void hotplug_event_update_event_status(PCIDevice
*dev
)
258 uint32_t pos
= dev
->exp
.exp_cap
;
259 uint8_t *exp_cap
= dev
->config
+ pos
;
260 uint16_t sltctl
= pci_get_word(exp_cap
+ PCI_EXP_SLTCTL
);
261 uint16_t sltsta
= pci_get_word(exp_cap
+ PCI_EXP_SLTSTA
);
263 dev
->exp
.hpev_notified
= (sltctl
& PCI_EXP_SLTCTL_HPIE
) &&
264 (sltsta
& sltctl
& PCI_EXP_HP_EV_SUPPORTED
);
267 static void hotplug_event_notify(PCIDevice
*dev
)
269 bool prev
= dev
->exp
.hpev_notified
;
271 hotplug_event_update_event_status(dev
);
273 if (prev
== dev
->exp
.hpev_notified
) {
277 /* Note: the logic above does not take into account whether interrupts
278 * are masked. The result is that interrupt will be sent when it is
279 * subsequently unmasked. This appears to be legal: Section 6.7.3.4:
280 * The Port may optionally send an MSI when there are hot-plug events that
281 * occur while interrupt generation is disabled, and interrupt generation is
282 * subsequently enabled. */
283 if (msix_enabled(dev
)) {
284 msix_notify(dev
, pcie_cap_flags_get_vector(dev
));
285 } else if (msi_enabled(dev
)) {
286 msi_notify(dev
, pcie_cap_flags_get_vector(dev
));
288 pci_set_irq(dev
, dev
->exp
.hpev_notified
);
292 static void hotplug_event_clear(PCIDevice
*dev
)
294 hotplug_event_update_event_status(dev
);
295 if (!msix_enabled(dev
) && !msi_enabled(dev
) && !dev
->exp
.hpev_notified
) {
296 pci_irq_deassert(dev
);
301 * A PCI Express Hot-Plug Event has occurred, so update slot status register
302 * and notify OS of the event if necessary.
304 * 6.7.3 PCI Express Hot-Plug Events
305 * 6.7.3.4 Software Notification of Hot-Plug Events
307 static void pcie_cap_slot_event(PCIDevice
*dev
, PCIExpressHotPlugEvent event
)
309 /* Minor optimization: if nothing changed - no event is needed. */
310 if (pci_word_test_and_set_mask(dev
->config
+ dev
->exp
.exp_cap
+
311 PCI_EXP_SLTSTA
, event
)) {
314 hotplug_event_notify(dev
);
317 static void pcie_cap_slot_hotplug_common(PCIDevice
*hotplug_dev
,
319 uint8_t **exp_cap
, Error
**errp
)
321 *exp_cap
= hotplug_dev
->config
+ hotplug_dev
->exp
.exp_cap
;
322 uint16_t sltsta
= pci_get_word(*exp_cap
+ PCI_EXP_SLTSTA
);
324 PCIE_DEV_PRINTF(PCI_DEVICE(dev
), "hotplug state: 0x%x\n", sltsta
);
325 if (sltsta
& PCI_EXP_SLTSTA_EIS
) {
326 /* the slot is electromechanically locked.
327 * This error is propagated up to qdev and then to HMP/QMP.
329 error_setg_errno(errp
, EBUSY
, "slot is electromechanically locked");
333 void pcie_cap_slot_hotplug_cb(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
337 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
339 pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev
), dev
, &exp_cap
, errp
);
341 /* Don't send event when device is enabled during qemu machine creation:
342 * it is present on boot, no hotplug event is necessary. We do send an
343 * event when the device is disabled later. */
344 if (!dev
->hotplugged
) {
345 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTSTA
,
350 /* To enable multifunction hot-plug, we just ensure the function
351 * 0 added last. When function 0 is added, we set the sltsta and
352 * inform OS via event notification.
354 if (pci_get_function_0(pci_dev
)) {
355 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTSTA
,
357 pcie_cap_slot_event(PCI_DEVICE(hotplug_dev
),
358 PCI_EXP_HP_EV_PDC
| PCI_EXP_HP_EV_ABP
);
362 static void pcie_unplug_device(PCIBus
*bus
, PCIDevice
*dev
, void *opaque
)
364 object_unparent(OBJECT(dev
));
367 void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler
*hotplug_dev
,
368 DeviceState
*dev
, Error
**errp
)
371 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
372 PCIBus
*bus
= pci_dev
->bus
;
374 pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev
), dev
, &exp_cap
, errp
);
376 /* In case user cancel the operation of multi-function hot-add,
377 * remove the function that is unexposed to guest individually,
378 * without interaction with guest.
380 if (pci_dev
->devfn
&&
382 pcie_unplug_device(bus
, pci_dev
, NULL
);
387 pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev
));
390 /* pci express slot for pci express root/downstream port
391 PCI express capability slot registers */
392 void pcie_cap_slot_init(PCIDevice
*dev
, uint16_t slot
)
394 uint32_t pos
= dev
->exp
.exp_cap
;
396 pci_word_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_FLAGS
,
399 pci_long_test_and_clear_mask(dev
->config
+ pos
+ PCI_EXP_SLTCAP
,
400 ~PCI_EXP_SLTCAP_PSN
);
401 pci_long_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_SLTCAP
,
402 (slot
<< PCI_EXP_SLTCAP_PSN_SHIFT
) |
410 if (dev
->cap_present
& QEMU_PCIE_SLTCAP_PCP
) {
411 pci_long_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_SLTCAP
,
413 pci_word_test_and_clear_mask(dev
->config
+ pos
+ PCI_EXP_SLTCTL
,
415 pci_word_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_SLTCTL
,
419 pci_word_test_and_clear_mask(dev
->config
+ pos
+ PCI_EXP_SLTCTL
,
422 pci_word_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_SLTCTL
,
423 PCI_EXP_SLTCTL_PIC_OFF
|
424 PCI_EXP_SLTCTL_AIC_OFF
);
425 pci_word_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_SLTCTL
,
428 PCI_EXP_SLTCTL_HPIE
|
429 PCI_EXP_SLTCTL_CCIE
|
430 PCI_EXP_SLTCTL_PDCE
|
431 PCI_EXP_SLTCTL_ABPE
);
432 /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
433 * make the bit writable here in order to detect 1b is written.
434 * pcie_cap_slot_write_config() test-and-clear the bit, so
435 * this bit always returns 0 to the guest.
437 pci_word_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_SLTCTL
,
440 pci_word_test_and_set_mask(dev
->w1cmask
+ pos
+ PCI_EXP_SLTSTA
,
441 PCI_EXP_HP_EV_SUPPORTED
);
443 dev
->exp
.hpev_notified
= false;
445 qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev
))),
449 void pcie_cap_slot_reset(PCIDevice
*dev
)
451 uint8_t *exp_cap
= dev
->config
+ dev
->exp
.exp_cap
;
452 uint8_t port_type
= pcie_cap_get_type(dev
);
454 assert(port_type
== PCI_EXP_TYPE_DOWNSTREAM
||
455 port_type
== PCI_EXP_TYPE_ROOT_PORT
);
457 PCIE_DEV_PRINTF(dev
, "reset\n");
459 pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_SLTCTL
,
463 PCI_EXP_SLTCTL_HPIE
|
464 PCI_EXP_SLTCTL_CCIE
|
465 PCI_EXP_SLTCTL_PDCE
|
466 PCI_EXP_SLTCTL_ABPE
);
467 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTCTL
,
468 PCI_EXP_SLTCTL_AIC_OFF
);
470 if (dev
->cap_present
& QEMU_PCIE_SLTCAP_PCP
) {
471 /* Downstream ports enforce device number 0. */
472 bool populated
= pci_bridge_get_sec_bus(PCI_BRIDGE(dev
))->devices
[0];
476 pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_SLTCTL
,
479 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTCTL
,
483 pic
= populated
? PCI_EXP_SLTCTL_PIC_ON
: PCI_EXP_SLTCTL_PIC_OFF
;
484 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTCTL
, pic
);
487 pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_SLTSTA
,
488 PCI_EXP_SLTSTA_EIS
|/* on reset,
489 the lock is released */
494 hotplug_event_update_event_status(dev
);
497 void pcie_cap_slot_write_config(PCIDevice
*dev
,
498 uint32_t addr
, uint32_t val
, int len
)
500 uint32_t pos
= dev
->exp
.exp_cap
;
501 uint8_t *exp_cap
= dev
->config
+ pos
;
502 uint16_t sltsta
= pci_get_word(exp_cap
+ PCI_EXP_SLTSTA
);
504 if (ranges_overlap(addr
, len
, pos
+ PCI_EXP_SLTSTA
, 2)) {
505 hotplug_event_clear(dev
);
508 if (!ranges_overlap(addr
, len
, pos
+ PCI_EXP_SLTCTL
, 2)) {
512 if (pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_SLTCTL
,
513 PCI_EXP_SLTCTL_EIC
)) {
514 sltsta
^= PCI_EXP_SLTSTA_EIS
; /* toggle PCI_EXP_SLTSTA_EIS bit */
515 pci_set_word(exp_cap
+ PCI_EXP_SLTSTA
, sltsta
);
516 PCIE_DEV_PRINTF(dev
, "PCI_EXP_SLTCTL_EIC: "
517 "sltsta -> 0x%02"PRIx16
"\n",
522 * If the slot is polulated, power indicator is off and power
523 * controller is off, it is safe to detach the devices.
525 if ((sltsta
& PCI_EXP_SLTSTA_PDS
) && (val
& PCI_EXP_SLTCTL_PCC
) &&
526 ((val
& PCI_EXP_SLTCTL_PIC_OFF
) == PCI_EXP_SLTCTL_PIC_OFF
)) {
527 PCIBus
*sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(dev
));
528 pci_for_each_device(sec_bus
, pci_bus_num(sec_bus
),
529 pcie_unplug_device
, NULL
);
531 pci_word_test_and_clear_mask(exp_cap
+ PCI_EXP_SLTSTA
,
533 pci_word_test_and_set_mask(exp_cap
+ PCI_EXP_SLTSTA
,
537 hotplug_event_notify(dev
);
540 * 6.7.3.2 Command Completed Events
542 * Software issues a command to a hot-plug capable Downstream Port by
543 * issuing a write transaction that targets any portion of the Port’s Slot
544 * Control register. A single write to the Slot Control register is
545 * considered to be a single command, even if the write affects more than
546 * one field in the Slot Control register. In response to this transaction,
547 * the Port must carry out the requested actions and then set the
548 * associated status field for the command completed event. */
550 /* Real hardware might take a while to complete requested command because
551 * physical movement would be involved like locking the electromechanical
552 * lock. However in our case, command is completed instantaneously above,
553 * so send a command completion event right now.
555 pcie_cap_slot_event(dev
, PCI_EXP_HP_EV_CCI
);
558 int pcie_cap_slot_post_load(void *opaque
, int version_id
)
560 PCIDevice
*dev
= opaque
;
561 hotplug_event_update_event_status(dev
);
565 void pcie_cap_slot_push_attention_button(PCIDevice
*dev
)
567 pcie_cap_slot_event(dev
, PCI_EXP_HP_EV_ABP
);
570 /* root control/capabilities/status. PME isn't emulated for now */
571 void pcie_cap_root_init(PCIDevice
*dev
)
573 pci_set_word(dev
->wmask
+ dev
->exp
.exp_cap
+ PCI_EXP_RTCTL
,
574 PCI_EXP_RTCTL_SECEE
| PCI_EXP_RTCTL_SENFEE
|
575 PCI_EXP_RTCTL_SEFEE
);
578 void pcie_cap_root_reset(PCIDevice
*dev
)
580 pci_set_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_RTCTL
, 0);
583 /* function level reset(FLR) */
584 void pcie_cap_flr_init(PCIDevice
*dev
)
586 pci_long_test_and_set_mask(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCAP
,
589 /* Although reading BCR_FLR returns always 0,
590 * the bit is made writable here in order to detect the 1b is written
591 * pcie_cap_flr_write_config() test-and-clear the bit, so
592 * this bit always returns 0 to the guest.
594 pci_word_test_and_set_mask(dev
->wmask
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL
,
595 PCI_EXP_DEVCTL_BCR_FLR
);
598 void pcie_cap_flr_write_config(PCIDevice
*dev
,
599 uint32_t addr
, uint32_t val
, int len
)
601 uint8_t *devctl
= dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL
;
602 if (pci_get_word(devctl
) & PCI_EXP_DEVCTL_BCR_FLR
) {
603 /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
604 so the handler can detect FLR by looking at this bit. */
605 pci_device_reset(dev
);
606 pci_word_test_and_clear_mask(devctl
, PCI_EXP_DEVCTL_BCR_FLR
);
610 /* Alternative Routing-ID Interpretation (ARI)
611 * forwarding support for root and downstream ports
613 void pcie_cap_arifwd_init(PCIDevice
*dev
)
615 uint32_t pos
= dev
->exp
.exp_cap
;
616 pci_long_test_and_set_mask(dev
->config
+ pos
+ PCI_EXP_DEVCAP2
,
617 PCI_EXP_DEVCAP2_ARI
);
618 pci_long_test_and_set_mask(dev
->wmask
+ pos
+ PCI_EXP_DEVCTL2
,
619 PCI_EXP_DEVCTL2_ARI
);
622 void pcie_cap_arifwd_reset(PCIDevice
*dev
)
624 uint8_t *devctl2
= dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL2
;
625 pci_long_test_and_clear_mask(devctl2
, PCI_EXP_DEVCTL2_ARI
);
628 bool pcie_cap_is_arifwd_enabled(const PCIDevice
*dev
)
630 if (!pci_is_express(dev
)) {
633 if (!dev
->exp
.exp_cap
) {
637 return pci_get_long(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL2
) &
641 /**************************************************************************
642 * pci express extended capability list management functions
643 * uint16_t ext_cap_id (16 bit)
644 * uint8_t cap_ver (4 bit)
645 * uint16_t cap_offset (12 bit)
646 * uint16_t ext_cap_size
649 /* Passing a cap_id value > 0xffff will return 0 and put end of list in prev */
650 static uint16_t pcie_find_capability_list(PCIDevice
*dev
, uint32_t cap_id
,
655 uint32_t header
= pci_get_long(dev
->config
+ PCI_CONFIG_SPACE_SIZE
);
658 /* no extended capability */
662 for (next
= PCI_CONFIG_SPACE_SIZE
; next
;
663 prev
= next
, next
= PCI_EXT_CAP_NEXT(header
)) {
665 assert(next
>= PCI_CONFIG_SPACE_SIZE
);
666 assert(next
<= PCIE_CONFIG_SPACE_SIZE
- 8);
668 header
= pci_get_long(dev
->config
+ next
);
669 if (PCI_EXT_CAP_ID(header
) == cap_id
) {
681 uint16_t pcie_find_capability(PCIDevice
*dev
, uint16_t cap_id
)
683 return pcie_find_capability_list(dev
, cap_id
, NULL
);
686 static void pcie_ext_cap_set_next(PCIDevice
*dev
, uint16_t pos
, uint16_t next
)
688 uint32_t header
= pci_get_long(dev
->config
+ pos
);
689 assert(!(next
& (PCI_EXT_CAP_ALIGN
- 1)));
690 header
= (header
& ~PCI_EXT_CAP_NEXT_MASK
) |
691 ((next
<< PCI_EXT_CAP_NEXT_SHIFT
) & PCI_EXT_CAP_NEXT_MASK
);
692 pci_set_long(dev
->config
+ pos
, header
);
696 * Caller must supply valid (offset, size) such that the range wouldn't
697 * overlap with other capability or other registers.
698 * This function doesn't check it.
700 void pcie_add_capability(PCIDevice
*dev
,
701 uint16_t cap_id
, uint8_t cap_ver
,
702 uint16_t offset
, uint16_t size
)
704 assert(offset
>= PCI_CONFIG_SPACE_SIZE
);
705 assert(offset
< offset
+ size
);
706 assert(offset
+ size
<= PCIE_CONFIG_SPACE_SIZE
);
708 assert(pci_is_express(dev
));
710 if (offset
!= PCI_CONFIG_SPACE_SIZE
) {
714 * 0xffffffff is not a valid cap id (it's a 16 bit field). use
715 * internally to find the last capability in the linked list.
717 pcie_find_capability_list(dev
, 0xffffffff, &prev
);
718 assert(prev
>= PCI_CONFIG_SPACE_SIZE
);
719 pcie_ext_cap_set_next(dev
, prev
, offset
);
721 pci_set_long(dev
->config
+ offset
, PCI_EXT_CAP(cap_id
, cap_ver
, 0));
723 /* Make capability read-only by default */
724 memset(dev
->wmask
+ offset
, 0, size
);
725 memset(dev
->w1cmask
+ offset
, 0, size
);
726 /* Check capability by default */
727 memset(dev
->cmask
+ offset
, 0xFF, size
);
730 /**************************************************************************
731 * pci express extended capability helper functions
735 void pcie_ari_init(PCIDevice
*dev
, uint16_t offset
, uint16_t nextfn
)
737 pcie_add_capability(dev
, PCI_EXT_CAP_ID_ARI
, PCI_ARI_VER
,
738 offset
, PCI_ARI_SIZEOF
);
739 pci_set_long(dev
->config
+ offset
+ PCI_ARI_CAP
, (nextfn
& 0xff) << 8);
742 void pcie_dev_ser_num_init(PCIDevice
*dev
, uint16_t offset
, uint64_t ser_num
)
744 static const int pci_dsn_ver
= 1;
745 static const int pci_dsn_cap
= 4;
747 pcie_add_capability(dev
, PCI_EXT_CAP_ID_DSN
, pci_dsn_ver
, offset
,
748 PCI_EXT_CAP_DSN_SIZEOF
);
749 pci_set_quad(dev
->config
+ offset
+ pci_dsn_cap
, ser_num
);
752 void pcie_ats_init(PCIDevice
*dev
, uint16_t offset
)
754 pcie_add_capability(dev
, PCI_EXT_CAP_ID_ATS
, 0x1,
755 offset
, PCI_EXT_CAP_ATS_SIZEOF
);
757 dev
->exp
.ats_cap
= offset
;
759 /* Invalidate Queue Depth 0, Page Aligned Request 0 */
760 pci_set_word(dev
->config
+ offset
+ PCI_ATS_CAP
, 0);
761 /* STU 0, Disabled by default */
762 pci_set_word(dev
->config
+ offset
+ PCI_ATS_CTRL
, 0);
764 pci_set_word(dev
->wmask
+ dev
->exp
.ats_cap
+ PCI_ATS_CTRL
, 0x800f);