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-common.h"
22 #include "hw/pci/pci_bridge.h"
23 #include "hw/pci/pcie.h"
24 #include "hw/pci/msix.h"
25 #include "hw/pci/msi.h"
26 #include "hw/pci/pci_bus.h"
27 #include "hw/pci/pcie_regs.h"
28 #include "qemu/range.h"
32 # define PCIE_DPRINTF(fmt, ...) \
33 fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
35 # define PCIE_DPRINTF(fmt, ...) do {} while (0)
37 #define PCIE_DEV_PRINTF(dev, fmt, ...) \
38 PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
41 /***************************************************************************
42 * pci express capability helper functions
44 int pcie_cap_init(PCIDevice *dev, uint8_t offset, uint8_t type, uint8_t port)
49 assert(pci_is_express(dev));
52 * Mangle type to convert Endpoints to Root Complex Integrated Endpoints.
53 * Windows will report Code 10 (device cannot start) for regular Endpoints
54 * on the Root Complex.
56 if (pci_bus_is_express(dev->bus) && pci_bus_is_root(dev->bus)) {
58 case PCI_EXP_TYPE_ENDPOINT:
59 type = PCI_EXP_TYPE_RC_END;
64 pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
69 dev->exp.exp_cap = pos;
70 exp_cap = dev->config + pos;
72 /* capability register
73 interrupt message number defaults to 0 */
74 pci_set_word(exp_cap + PCI_EXP_FLAGS,
75 ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
78 /* device capability register
80 * roll based error reporting bit must be set by all
81 * Functions conforming to the ECN, PCI Express Base
82 * Specification, Revision 1.1., or subsequent PCI Express Base
83 * Specification revisions.
85 pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
87 pci_set_long(exp_cap + PCI_EXP_LNKCAP,
88 (port << PCI_EXP_LNKCAP_PN_SHIFT) |
89 PCI_EXP_LNKCAP_ASPMS_0S |
93 pci_set_word(exp_cap + PCI_EXP_LNKSTA,
94 PCI_EXP_LNK_MLW_1 | PCI_EXP_LNK_LS_25);
96 pci_set_long(exp_cap + PCI_EXP_DEVCAP2,
97 PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP);
99 pci_set_word(dev->wmask + pos, PCI_EXP_DEVCTL2_EETLPPB);
103 void pcie_cap_exit(PCIDevice *dev)
105 pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF);
108 uint8_t pcie_cap_get_type(const PCIDevice *dev)
110 uint32_t pos = dev->exp.exp_cap;
112 return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
113 PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT;
117 /* pci express interrupt message number */
118 /* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
119 void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
121 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
123 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ);
124 pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS,
125 vector << PCI_EXP_FLAGS_IRQ_SHIFT);
128 uint8_t pcie_cap_flags_get_vector(PCIDevice *dev)
130 return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
131 PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT;
134 void pcie_cap_deverr_init(PCIDevice *dev)
136 uint32_t pos = dev->exp.exp_cap;
137 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP,
138 PCI_EXP_DEVCAP_RBER);
139 pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL,
140 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
141 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
142 pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA,
143 PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED |
144 PCI_EXP_DEVSTA_URD | PCI_EXP_DEVSTA_URD);
147 void pcie_cap_deverr_reset(PCIDevice *dev)
149 uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
150 pci_long_test_and_clear_mask(devctl,
151 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
152 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
155 static void hotplug_event_update_event_status(PCIDevice *dev)
157 uint32_t pos = dev->exp.exp_cap;
158 uint8_t *exp_cap = dev->config + pos;
159 uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
160 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
162 dev->exp.hpev_notified = (sltctl & PCI_EXP_SLTCTL_HPIE) &&
163 (sltsta & sltctl & PCI_EXP_HP_EV_SUPPORTED);
166 static void hotplug_event_notify(PCIDevice *dev)
168 bool prev = dev->exp.hpev_notified;
170 hotplug_event_update_event_status(dev);
172 if (prev == dev->exp.hpev_notified) {
176 /* Note: the logic above does not take into account whether interrupts
177 * are masked. The result is that interrupt will be sent when it is
178 * subsequently unmasked. This appears to be legal: Section 6.7.3.4:
179 * The Port may optionally send an MSI when there are hot-plug events that
180 * occur while interrupt generation is disabled, and interrupt generation is
181 * subsequently enabled. */
182 if (msix_enabled(dev)) {
183 msix_notify(dev, pcie_cap_flags_get_vector(dev));
184 } else if (msi_enabled(dev)) {
185 msi_notify(dev, pcie_cap_flags_get_vector(dev));
187 qemu_set_irq(dev->irq[dev->exp.hpev_intx], dev->exp.hpev_notified);
191 static void hotplug_event_clear(PCIDevice *dev)
193 hotplug_event_update_event_status(dev);
194 if (!msix_enabled(dev) && !msi_enabled(dev) && !dev->exp.hpev_notified) {
195 qemu_set_irq(dev->irq[dev->exp.hpev_intx], 0);
200 * A PCI Express Hot-Plug Event has occurred, so update slot status register
201 * and notify OS of the event if necessary.
203 * 6.7.3 PCI Express Hot-Plug Events
204 * 6.7.3.4 Software Notification of Hot-Plug Events
206 static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
208 /* Minor optimization: if nothing changed - no event is needed. */
209 if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
210 PCI_EXP_SLTSTA, event)) {
213 hotplug_event_notify(dev);
216 static int pcie_cap_slot_hotplug(DeviceState *qdev,
217 PCIDevice *pci_dev, PCIHotplugState state)
219 PCIDevice *d = PCI_DEVICE(qdev);
220 uint8_t *exp_cap = d->config + d->exp.exp_cap;
221 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
223 /* Don't send event when device is enabled during qemu machine creation:
224 * it is present on boot, no hotplug event is necessary. We do send an
225 * event when the device is disabled later. */
226 if (state == PCI_COLDPLUG_ENABLED) {
227 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
232 PCIE_DEV_PRINTF(pci_dev, "hotplug state: %d\n", state);
233 if (sltsta & PCI_EXP_SLTSTA_EIS) {
234 /* the slot is electromechanically locked.
235 * This error is propagated up to qdev and then to HMP/QMP.
240 /* TODO: multifunction hot-plug.
241 * Right now, only a device of function = 0 is allowed to be
242 * hot plugged/unplugged.
244 assert(PCI_FUNC(pci_dev->devfn) == 0);
246 if (state == PCI_HOTPLUG_ENABLED) {
247 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
249 pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
251 qdev_free(&pci_dev->qdev);
252 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
254 pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
259 /* pci express slot for pci express root/downstream port
260 PCI express capability slot registers */
261 void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
263 uint32_t pos = dev->exp.exp_cap;
265 pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
268 pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
269 ~PCI_EXP_SLTCAP_PSN);
270 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
271 (slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
279 pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
282 pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
283 PCI_EXP_SLTCTL_PIC_OFF |
284 PCI_EXP_SLTCTL_AIC_OFF);
285 pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
288 PCI_EXP_SLTCTL_HPIE |
289 PCI_EXP_SLTCTL_CCIE |
290 PCI_EXP_SLTCTL_PDCE |
291 PCI_EXP_SLTCTL_ABPE);
292 /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
293 * make the bit writable here in order to detect 1b is written.
294 * pcie_cap_slot_write_config() test-and-clear the bit, so
295 * this bit always returns 0 to the guest.
297 pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
300 pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
301 PCI_EXP_HP_EV_SUPPORTED);
303 dev->exp.hpev_notified = false;
305 pci_bus_hotplug(pci_bridge_get_sec_bus(DO_UPCAST(PCIBridge, dev, dev)),
306 pcie_cap_slot_hotplug, &dev->qdev);
309 void pcie_cap_slot_reset(PCIDevice *dev)
311 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
313 PCIE_DEV_PRINTF(dev, "reset\n");
315 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
319 PCI_EXP_SLTCTL_HPIE |
320 PCI_EXP_SLTCTL_CCIE |
321 PCI_EXP_SLTCTL_PDCE |
322 PCI_EXP_SLTCTL_ABPE);
323 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
324 PCI_EXP_SLTCTL_PIC_OFF |
325 PCI_EXP_SLTCTL_AIC_OFF);
327 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
328 PCI_EXP_SLTSTA_EIS |/* on reset,
329 the lock is released */
334 hotplug_event_update_event_status(dev);
337 void pcie_cap_slot_write_config(PCIDevice *dev,
338 uint32_t addr, uint32_t val, int len)
340 uint32_t pos = dev->exp.exp_cap;
341 uint8_t *exp_cap = dev->config + pos;
342 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
344 if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
345 hotplug_event_clear(dev);
348 if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
352 if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
353 PCI_EXP_SLTCTL_EIC)) {
354 sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
355 pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
356 PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
357 "sltsta -> 0x%02"PRIx16"\n",
361 hotplug_event_notify(dev);
364 * 6.7.3.2 Command Completed Events
366 * Software issues a command to a hot-plug capable Downstream Port by
367 * issuing a write transaction that targets any portion of the Port’s Slot
368 * Control register. A single write to the Slot Control register is
369 * considered to be a single command, even if the write affects more than
370 * one field in the Slot Control register. In response to this transaction,
371 * the Port must carry out the requested actions and then set the
372 * associated status field for the command completed event. */
374 /* Real hardware might take a while to complete requested command because
375 * physical movement would be involved like locking the electromechanical
376 * lock. However in our case, command is completed instantaneously above,
377 * so send a command completion event right now.
379 pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
382 int pcie_cap_slot_post_load(void *opaque, int version_id)
384 PCIDevice *dev = opaque;
385 hotplug_event_update_event_status(dev);
389 void pcie_cap_slot_push_attention_button(PCIDevice *dev)
391 pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
394 /* root control/capabilities/status. PME isn't emulated for now */
395 void pcie_cap_root_init(PCIDevice *dev)
397 pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
398 PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
399 PCI_EXP_RTCTL_SEFEE);
402 void pcie_cap_root_reset(PCIDevice *dev)
404 pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
407 /* function level reset(FLR) */
408 void pcie_cap_flr_init(PCIDevice *dev)
410 pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
413 /* Although reading BCR_FLR returns always 0,
414 * the bit is made writable here in order to detect the 1b is written
415 * pcie_cap_flr_write_config() test-and-clear the bit, so
416 * this bit always returns 0 to the guest.
418 pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
419 PCI_EXP_DEVCTL_BCR_FLR);
422 void pcie_cap_flr_write_config(PCIDevice *dev,
423 uint32_t addr, uint32_t val, int len)
425 uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
426 if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
427 /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
428 so the handler can detect FLR by looking at this bit. */
429 pci_device_reset(dev);
430 pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
434 /* Alternative Routing-ID Interpretation (ARI) */
435 /* ari forwarding support for down stream port */
436 void pcie_cap_ari_init(PCIDevice *dev)
438 uint32_t pos = dev->exp.exp_cap;
439 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
440 PCI_EXP_DEVCAP2_ARI);
441 pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
442 PCI_EXP_DEVCTL2_ARI);
445 void pcie_cap_ari_reset(PCIDevice *dev)
447 uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
448 pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
451 bool pcie_cap_is_ari_enabled(const PCIDevice *dev)
453 if (!pci_is_express(dev)) {
456 if (!dev->exp.exp_cap) {
460 return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
464 /**************************************************************************
465 * pci express extended capability allocation functions
466 * uint16_t ext_cap_id (16 bit)
467 * uint8_t cap_ver (4 bit)
468 * uint16_t cap_offset (12 bit)
469 * uint16_t ext_cap_size
472 static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
477 uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
480 /* no extended capability */
484 for (next = PCI_CONFIG_SPACE_SIZE; next;
485 prev = next, next = PCI_EXT_CAP_NEXT(header)) {
487 assert(next >= PCI_CONFIG_SPACE_SIZE);
488 assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
490 header = pci_get_long(dev->config + next);
491 if (PCI_EXT_CAP_ID(header) == cap_id) {
503 uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
505 return pcie_find_capability_list(dev, cap_id, NULL);
508 static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
510 uint32_t header = pci_get_long(dev->config + pos);
511 assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
512 header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
513 ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
514 pci_set_long(dev->config + pos, header);
518 * caller must supply valid (offset, size) * such that the range shouldn't
519 * overlap with other capability or other registers.
520 * This function doesn't check it.
522 void pcie_add_capability(PCIDevice *dev,
523 uint16_t cap_id, uint8_t cap_ver,
524 uint16_t offset, uint16_t size)
529 assert(offset >= PCI_CONFIG_SPACE_SIZE);
530 assert(offset < offset + size);
531 assert(offset + size < PCIE_CONFIG_SPACE_SIZE);
533 assert(pci_is_express(dev));
535 if (offset == PCI_CONFIG_SPACE_SIZE) {
536 header = pci_get_long(dev->config + offset);
537 next = PCI_EXT_CAP_NEXT(header);
541 /* 0 is reserved cap id. use internally to find the last capability
542 in the linked list */
543 next = pcie_find_capability_list(dev, 0, &prev);
545 assert(prev >= PCI_CONFIG_SPACE_SIZE);
547 pcie_ext_cap_set_next(dev, prev, offset);
549 pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next));
551 /* Make capability read-only by default */
552 memset(dev->wmask + offset, 0, size);
553 memset(dev->w1cmask + offset, 0, size);
554 /* Check capability by default */
555 memset(dev->cmask + offset, 0xFF, size);
558 /**************************************************************************
559 * pci express extended capability helper functions
563 void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
565 pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
566 offset, PCI_ARI_SIZEOF);
567 pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn));