]> git.proxmox.com Git - mirror_qemu.git/blame - hw/pci/pcie.c
pcie: factor out pcie_cap_slot_unplug()
[mirror_qemu.git] / hw / pci / pcie.c
CommitLineData
0428527c
IY
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
97d5408f 21#include "qemu/osdep.h"
da34e65c 22#include "qapi/error.h"
c759b24f
MT
23#include "hw/pci/pci_bridge.h"
24#include "hw/pci/pcie.h"
25#include "hw/pci/msix.h"
26#include "hw/pci/msi.h"
06aac7bd 27#include "hw/pci/pci_bus.h"
c759b24f 28#include "hw/pci/pcie_regs.h"
3d67447f 29#include "hw/pci/pcie_port.h"
1de7afc9 30#include "qemu/range.h"
0428527c
IY
31
32//#define DEBUG_PCIE
33#ifdef DEBUG_PCIE
34# define PCIE_DPRINTF(fmt, ...) \
35 fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
36#else
37# define PCIE_DPRINTF(fmt, ...) do {} while (0)
38#endif
39#define PCIE_DEV_PRINTF(dev, fmt, ...) \
40 PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
41
42
43/***************************************************************************
44 * pci express capability helper functions
45 */
0428527c 46
6383292a 47static void
6b449540 48pcie_cap_v1_fill(PCIDevice *dev, uint8_t port, uint8_t type, uint8_t version)
6383292a 49{
6b449540
MT
50 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
51 uint8_t *cmask = dev->cmask + dev->exp.exp_cap;
52
0428527c 53 /* capability register
6383292a 54 interrupt message number defaults to 0 */
0428527c
IY
55 pci_set_word(exp_cap + PCI_EXP_FLAGS,
56 ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
6383292a 57 version);
0428527c
IY
58
59 /* device capability register
60 * table 7-12:
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.
65 */
66 pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
67
68 pci_set_long(exp_cap + PCI_EXP_LNKCAP,
69 (port << PCI_EXP_LNKCAP_PN_SHIFT) |
70 PCI_EXP_LNKCAP_ASPMS_0S |
d96a0ac7
AW
71 QEMU_PCI_EXP_LNKCAP_MLW(QEMU_PCI_EXP_LNK_X1) |
72 QEMU_PCI_EXP_LNKCAP_MLS(QEMU_PCI_EXP_LNK_2_5GT));
0428527c
IY
73
74 pci_set_word(exp_cap + PCI_EXP_LNKSTA,
d96a0ac7
AW
75 QEMU_PCI_EXP_LNKSTA_NLW(QEMU_PCI_EXP_LNK_X1) |
76 QEMU_PCI_EXP_LNKSTA_CLS(QEMU_PCI_EXP_LNK_2_5GT));
6b449540 77
6b449540
MT
78 /* We changed link status bits over time, and changing them across
79 * migrations is generally fine as hardware changes them too.
80 * Let's not bother checking.
81 */
82 pci_set_word(cmask + PCI_EXP_LNKSTA, 0);
6383292a
DF
83}
84
3d67447f
AW
85static void pcie_cap_fill_slot_lnk(PCIDevice *dev)
86{
87 PCIESlot *s = (PCIESlot *)object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT);
88 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
89
90 /* Skip anything that isn't a PCIESlot */
91 if (!s) {
92 return;
93 }
94
95 /* Clear and fill LNKCAP from what was configured above */
96 pci_long_test_and_clear_mask(exp_cap + PCI_EXP_LNKCAP,
97 PCI_EXP_LNKCAP_MLW | PCI_EXP_LNKCAP_SLS);
98 pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
99 QEMU_PCI_EXP_LNKCAP_MLW(s->width) |
100 QEMU_PCI_EXP_LNKCAP_MLS(s->speed));
101
102 /*
103 * Link bandwidth notification is required for all root ports and
104 * downstream ports supporting links wider than x1 or multiple link
105 * speeds.
106 */
107 if (s->width > QEMU_PCI_EXP_LNK_X1 ||
108 s->speed > QEMU_PCI_EXP_LNK_2_5GT) {
109 pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
110 PCI_EXP_LNKCAP_LBNC);
111 }
112
113 if (s->speed > QEMU_PCI_EXP_LNK_2_5GT) {
114 /*
115 * Hot-plug capable downstream ports and downstream ports supporting
116 * link speeds greater than 5GT/s must hardwire PCI_EXP_LNKCAP_DLLLARC
117 * to 1b. PCI_EXP_LNKCAP_DLLLARC implies PCI_EXP_LNKSTA_DLLLA, which
118 * we also hardwire to 1b here. 2.5GT/s hot-plug slots should also
119 * technically implement this, but it's not done here for compatibility.
120 */
121 pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
122 PCI_EXP_LNKCAP_DLLLARC);
df72184e 123 /* the PCI_EXP_LNKSTA_DLLLA will be set in the hotplug function */
3d67447f
AW
124
125 /*
126 * Target Link Speed defaults to the highest link speed supported by
127 * the component. 2.5GT/s devices are permitted to hardwire to zero.
128 */
129 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKCTL2,
130 PCI_EXP_LNKCTL2_TLS);
131 pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKCTL2,
132 QEMU_PCI_EXP_LNKCAP_MLS(s->speed) &
133 PCI_EXP_LNKCTL2_TLS);
134 }
135
136 /*
137 * 2.5 & 5.0GT/s can be fully described by LNKCAP, but 8.0GT/s is
138 * actually a reference to the highest bit supported in this register.
139 * We assume the device supports all link speeds.
140 */
141 if (s->speed > QEMU_PCI_EXP_LNK_5GT) {
142 pci_long_test_and_clear_mask(exp_cap + PCI_EXP_LNKCAP2, ~0U);
143 pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP2,
144 PCI_EXP_LNKCAP2_SLS_2_5GB |
145 PCI_EXP_LNKCAP2_SLS_5_0GB |
146 PCI_EXP_LNKCAP2_SLS_8_0GB);
147 if (s->speed > QEMU_PCI_EXP_LNK_8GT) {
148 pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP2,
149 PCI_EXP_LNKCAP2_SLS_16_0GB);
150 }
151 }
152}
153
f8cd1b02
MZ
154int pcie_cap_init(PCIDevice *dev, uint8_t offset,
155 uint8_t type, uint8_t port,
156 Error **errp)
6383292a
DF
157{
158 /* PCIe cap v2 init */
159 int pos;
160 uint8_t *exp_cap;
161
162 assert(pci_is_express(dev));
163
9a7c2a59 164 pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
f8cd1b02 165 PCI_EXP_VER2_SIZEOF, errp);
6383292a
DF
166 if (pos < 0) {
167 return pos;
168 }
169 dev->exp.exp_cap = pos;
170 exp_cap = dev->config + pos;
171
172 /* Filling values common with v1 */
6b449540 173 pcie_cap_v1_fill(dev, port, type, PCI_EXP_FLAGS_VER2);
0428527c 174
3d67447f
AW
175 /* Fill link speed and width options */
176 pcie_cap_fill_slot_lnk(dev);
177
6383292a 178 /* Filling v2 specific values */
0428527c
IY
179 pci_set_long(exp_cap + PCI_EXP_DEVCAP2,
180 PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP);
181
30b04f87 182 pci_set_word(dev->wmask + pos + PCI_EXP_DEVCTL2, PCI_EXP_DEVCTL2_EETLPPB);
f03d8ea3
MA
183
184 if (dev->cap_present & QEMU_PCIE_EXTCAP_INIT) {
185 /* read-only to behave like a 'NULL' Extended Capability Header */
186 pci_set_long(dev->wmask + PCI_CONFIG_SPACE_SIZE, 0);
187 }
188
0428527c
IY
189 return pos;
190}
191
6383292a
DF
192int pcie_cap_v1_init(PCIDevice *dev, uint8_t offset, uint8_t type,
193 uint8_t port)
194{
195 /* PCIe cap v1 init */
196 int pos;
9a7c2a59 197 Error *local_err = NULL;
6383292a
DF
198
199 assert(pci_is_express(dev));
200
9a7c2a59
MZ
201 pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
202 PCI_EXP_VER1_SIZEOF, &local_err);
6383292a 203 if (pos < 0) {
9a7c2a59 204 error_report_err(local_err);
6383292a
DF
205 return pos;
206 }
207 dev->exp.exp_cap = pos;
6383292a 208
6b449540 209 pcie_cap_v1_fill(dev, port, type, PCI_EXP_FLAGS_VER1);
6383292a
DF
210
211 return pos;
212}
213
214static int
215pcie_endpoint_cap_common_init(PCIDevice *dev, uint8_t offset, uint8_t cap_size)
6214e73c
AW
216{
217 uint8_t type = PCI_EXP_TYPE_ENDPOINT;
f8cd1b02
MZ
218 Error *local_err = NULL;
219 int ret;
6214e73c
AW
220
221 /*
222 * Windows guests will report Code 10, device cannot start, if
223 * a regular Endpoint type is exposed on a root complex. These
224 * should instead be Root Complex Integrated Endpoints.
225 */
fd56e061
DG
226 if (pci_bus_is_express(pci_get_bus(dev))
227 && pci_bus_is_root(pci_get_bus(dev))) {
6214e73c
AW
228 type = PCI_EXP_TYPE_RC_END;
229 }
230
f8cd1b02
MZ
231 if (cap_size == PCI_EXP_VER1_SIZEOF) {
232 return pcie_cap_v1_init(dev, offset, type, 0);
233 } else {
234 ret = pcie_cap_init(dev, offset, type, 0, &local_err);
235
236 if (ret < 0) {
237 error_report_err(local_err);
238 }
239
240 return ret;
241 }
6383292a
DF
242}
243
244int pcie_endpoint_cap_init(PCIDevice *dev, uint8_t offset)
245{
246 return pcie_endpoint_cap_common_init(dev, offset, PCI_EXP_VER2_SIZEOF);
247}
248
249int pcie_endpoint_cap_v1_init(PCIDevice *dev, uint8_t offset)
250{
251 return pcie_endpoint_cap_common_init(dev, offset, PCI_EXP_VER1_SIZEOF);
6214e73c
AW
252}
253
0428527c
IY
254void pcie_cap_exit(PCIDevice *dev)
255{
256 pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF);
257}
258
6383292a
DF
259void pcie_cap_v1_exit(PCIDevice *dev)
260{
261 pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER1_SIZEOF);
262}
263
0428527c
IY
264uint8_t pcie_cap_get_type(const PCIDevice *dev)
265{
266 uint32_t pos = dev->exp.exp_cap;
267 assert(pos > 0);
268 return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
269 PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT;
270}
271
272/* MSI/MSI-X */
273/* pci express interrupt message number */
274/* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
275void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
276{
277 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
278 assert(vector < 32);
279 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ);
280 pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS,
281 vector << PCI_EXP_FLAGS_IRQ_SHIFT);
282}
283
284uint8_t pcie_cap_flags_get_vector(PCIDevice *dev)
285{
286 return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
287 PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT;
288}
289
290void pcie_cap_deverr_init(PCIDevice *dev)
291{
292 uint32_t pos = dev->exp.exp_cap;
293 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP,
294 PCI_EXP_DEVCAP_RBER);
295 pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL,
296 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
297 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
298 pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA,
299 PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED |
8e815eee 300 PCI_EXP_DEVSTA_FED | PCI_EXP_DEVSTA_URD);
0428527c
IY
301}
302
303void pcie_cap_deverr_reset(PCIDevice *dev)
304{
305 uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
306 pci_long_test_and_clear_mask(devctl,
307 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
308 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
309}
310
d584f1b9
MA
311void pcie_cap_lnkctl_init(PCIDevice *dev)
312{
313 uint32_t pos = dev->exp.exp_cap;
314 pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_LNKCTL,
315 PCI_EXP_LNKCTL_CCC | PCI_EXP_LNKCTL_ES);
316}
317
318void pcie_cap_lnkctl_reset(PCIDevice *dev)
319{
320 uint8_t *lnkctl = dev->config + dev->exp.exp_cap + PCI_EXP_LNKCTL;
321 pci_long_test_and_clear_mask(lnkctl,
322 PCI_EXP_LNKCTL_CCC | PCI_EXP_LNKCTL_ES);
323}
324
6bde6aaa
MT
325static void hotplug_event_update_event_status(PCIDevice *dev)
326{
327 uint32_t pos = dev->exp.exp_cap;
328 uint8_t *exp_cap = dev->config + pos;
329 uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
330 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
331
332 dev->exp.hpev_notified = (sltctl & PCI_EXP_SLTCTL_HPIE) &&
333 (sltsta & sltctl & PCI_EXP_HP_EV_SUPPORTED);
334}
335
336static void hotplug_event_notify(PCIDevice *dev)
337{
338 bool prev = dev->exp.hpev_notified;
339
340 hotplug_event_update_event_status(dev);
341
342 if (prev == dev->exp.hpev_notified) {
343 return;
344 }
345
346 /* Note: the logic above does not take into account whether interrupts
347 * are masked. The result is that interrupt will be sent when it is
348 * subsequently unmasked. This appears to be legal: Section 6.7.3.4:
349 * The Port may optionally send an MSI when there are hot-plug events that
350 * occur while interrupt generation is disabled, and interrupt generation is
351 * subsequently enabled. */
4a9dd665
MT
352 if (msix_enabled(dev)) {
353 msix_notify(dev, pcie_cap_flags_get_vector(dev));
354 } else if (msi_enabled(dev)) {
355 msi_notify(dev, pcie_cap_flags_get_vector(dev));
356 } else {
5a03e708 357 pci_set_irq(dev, dev->exp.hpev_notified);
6bde6aaa
MT
358 }
359}
360
1553d4f1
IY
361static void hotplug_event_clear(PCIDevice *dev)
362{
363 hotplug_event_update_event_status(dev);
364 if (!msix_enabled(dev) && !msi_enabled(dev) && !dev->exp.hpev_notified) {
5a03e708 365 pci_irq_deassert(dev);
1553d4f1
IY
366 }
367}
368
d5daff7d
GH
369static void pcie_set_power_device(PCIBus *bus, PCIDevice *dev, void *opaque)
370{
371 bool *power = opaque;
372
373 pci_set_power(dev, *power);
374}
375
376static void pcie_cap_update_power(PCIDevice *hotplug_dev)
377{
378 uint8_t *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
379 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(hotplug_dev));
380 uint32_t sltcap = pci_get_long(exp_cap + PCI_EXP_SLTCAP);
381 uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
382 bool power = true;
383
384 if (sltcap & PCI_EXP_SLTCAP_PCP) {
385 power = (sltctl & PCI_EXP_SLTCTL_PCC) == PCI_EXP_SLTCTL_PWR_ON;
386 }
387
388 pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
389 pcie_set_power_device, &power);
390}
391
0428527c 392/*
a1c7273b 393 * A PCI Express Hot-Plug Event has occurred, so update slot status register
0428527c
IY
394 * and notify OS of the event if necessary.
395 *
396 * 6.7.3 PCI Express Hot-Plug Events
397 * 6.7.3.4 Software Notification of Hot-Plug Events
398 */
399static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
400{
6bde6aaa
MT
401 /* Minor optimization: if nothing changed - no event is needed. */
402 if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
861dc735 403 PCI_EXP_SLTSTA, event) == event) {
0428527c
IY
404 return;
405 }
6bde6aaa 406 hotplug_event_notify(dev);
0428527c
IY
407}
408
5571727a 409static void pcie_cap_slot_plug_common(PCIDevice *hotplug_dev, DeviceState *dev,
b9731850 410 Error **errp)
0428527c 411{
b9731850
DH
412 uint8_t *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
413 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
0428527c 414
e4bcd27c 415 PCIE_DEV_PRINTF(PCI_DEVICE(dev), "hotplug state: 0x%x\n", sltsta);
0428527c
IY
416 if (sltsta & PCI_EXP_SLTSTA_EIS) {
417 /* the slot is electromechanically locked.
418 * This error is propagated up to qdev and then to HMP/QMP.
419 */
6c150fbd 420 error_setg_errno(errp, EBUSY, "slot is electromechanically locked");
0428527c 421 }
a66e657e
IM
422}
423
b9731850
DH
424void pcie_cap_slot_pre_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
425 Error **errp)
426{
0dabc0f6
JS
427 PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
428 uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
429 uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
430
431 /* Check if hot-plug is disabled on the slot */
432 if (dev->hotplugged && (sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
433 error_setg(errp, "Hot-plug failed: unsupported by the port device '%s'",
434 DEVICE(hotplug_pdev)->id);
435 return;
436 }
437
b9731850
DH
438 pcie_cap_slot_plug_common(PCI_DEVICE(hotplug_dev), dev, errp);
439}
440
5571727a
DH
441void pcie_cap_slot_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
442 Error **errp)
a66e657e 443{
b9731850
DH
444 PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
445 uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
6e1f0a55 446 PCIDevice *pci_dev = PCI_DEVICE(dev);
df72184e 447 uint32_t lnkcap = pci_get_long(exp_cap + PCI_EXP_LNKCAP);
a66e657e 448
a66e657e
IM
449 /* Don't send event when device is enabled during qemu machine creation:
450 * it is present on boot, no hotplug event is necessary. We do send an
451 * event when the device is disabled later. */
452 if (!dev->hotplugged) {
0428527c
IY
453 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
454 PCI_EXP_SLTSTA_PDS);
df72184e
LV
455 if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
456 (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
2f2b18f6
ZX
457 pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
458 PCI_EXP_LNKSTA_DLLLA);
459 }
d5daff7d 460 pcie_cap_update_power(hotplug_pdev);
a66e657e 461 return;
0428527c 462 }
a66e657e 463
3f1e1478
C
464 /* To enable multifunction hot-plug, we just ensure the function
465 * 0 added last. When function 0 is added, we set the sltsta and
466 * inform OS via event notification.
6e1f0a55 467 */
3f1e1478
C
468 if (pci_get_function_0(pci_dev)) {
469 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
470 PCI_EXP_SLTSTA_PDS);
df72184e
LV
471 if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
472 (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
2f2b18f6
ZX
473 pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
474 PCI_EXP_LNKSTA_DLLLA);
475 }
6a1e0733 476 pcie_cap_slot_event(hotplug_pdev,
3f1e1478 477 PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
d5daff7d 478 pcie_cap_update_power(hotplug_pdev);
3f1e1478 479 }
a66e657e
IM
480}
481
a1952d01
DH
482void pcie_cap_slot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
483 Error **errp)
0d1c7d88 484{
981c3dcd 485 qdev_unrealize(dev);
0d1c7d88
C
486}
487
a1952d01
DH
488static void pcie_unplug_device(PCIBus *bus, PCIDevice *dev, void *opaque)
489{
490 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(DEVICE(dev));
491
a99c4da9 492 if (dev->partially_hotplugged) {
c000a9bd 493 dev->qdev.pending_deleted_event = false;
a99c4da9
JF
494 return;
495 }
a1952d01 496 hotplug_handler_unplug(hotplug_ctrl, DEVICE(dev), &error_abort);
07578b0a 497 object_unparent(OBJECT(dev));
a1952d01
DH
498}
499
44242d4d
GH
500static void pcie_cap_slot_do_unplug(PCIDevice *dev)
501{
502 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
503 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
504 uint32_t lnkcap = pci_get_long(exp_cap + PCI_EXP_LNKCAP);
505
506 pci_for_each_device_under_bus(sec_bus, pcie_unplug_device, NULL);
507
508 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
509 PCI_EXP_SLTSTA_PDS);
510 if (dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
511 (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
512 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
513 PCI_EXP_LNKSTA_DLLLA);
514 }
515 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
516 PCI_EXP_SLTSTA_PDC);
517}
518
5571727a
DH
519void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
520 DeviceState *dev, Error **errp)
a66e657e 521{
b9731850 522 Error *local_err = NULL;
0d1c7d88 523 PCIDevice *pci_dev = PCI_DEVICE(dev);
fd56e061 524 PCIBus *bus = pci_get_bus(pci_dev);
0501e1aa
JS
525 PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
526 uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
527 uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
81124b3c 528 uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
0501e1aa
JS
529
530 /* Check if hot-unplug is disabled on the slot */
531 if ((sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
532 error_setg(errp, "Hot-unplug failed: "
533 "unsupported by the port device '%s'",
534 DEVICE(hotplug_pdev)->id);
535 return;
536 }
a66e657e 537
6a1e0733 538 pcie_cap_slot_plug_common(hotplug_pdev, dev, &local_err);
b9731850
DH
539 if (local_err) {
540 error_propagate(errp, local_err);
541 return;
542 }
a66e657e 543
81124b3c
GH
544 if ((sltctl & PCI_EXP_SLTCTL_PIC) == PCI_EXP_SLTCTL_PWR_IND_BLINK) {
545 error_setg(errp, "Hot-unplug failed: "
546 "guest is busy (power indicator blinking)");
547 return;
548 }
549
c000a9bd
JF
550 dev->pending_deleted_event = true;
551
0d1c7d88
C
552 /* In case user cancel the operation of multi-function hot-add,
553 * remove the function that is unexposed to guest individually,
554 * without interaction with guest.
555 */
556 if (pci_dev->devfn &&
557 !bus->devices[0]) {
558 pcie_unplug_device(bus, pci_dev, NULL);
559
560 return;
561 }
562
6a1e0733 563 pcie_cap_slot_push_attention_button(hotplug_pdev);
0428527c
IY
564}
565
566/* pci express slot for pci express root/downstream port
567 PCI express capability slot registers */
530a0963 568void pcie_cap_slot_init(PCIDevice *dev, PCIESlot *s)
0428527c
IY
569{
570 uint32_t pos = dev->exp.exp_cap;
571
572 pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
573 PCI_EXP_FLAGS_SLOT);
574
575 pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
576 ~PCI_EXP_SLTCAP_PSN);
577 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
530a0963 578 (s->slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
0428527c 579 PCI_EXP_SLTCAP_EIP |
0428527c
IY
580 PCI_EXP_SLTCAP_PIP |
581 PCI_EXP_SLTCAP_AIP |
582 PCI_EXP_SLTCAP_ABP);
3f3cbbb2
JS
583
584 /*
585 * Enable native hot-plug on all hot-plugged bridges unless
586 * hot-plug is disabled on the slot.
587 */
588 if (s->hotplug &&
589 (s->native_hotplug || DEVICE(dev)->hotplugged)) {
530a0963
JS
590 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
591 PCI_EXP_SLTCAP_HPS |
592 PCI_EXP_SLTCAP_HPC);
593 }
0428527c 594
f23b6bdc
MA
595 if (dev->cap_present & QEMU_PCIE_SLTCAP_PCP) {
596 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
597 PCI_EXP_SLTCAP_PCP);
598 pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
599 PCI_EXP_SLTCTL_PCC);
600 pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
601 PCI_EXP_SLTCTL_PCC);
602 }
603
0428527c
IY
604 pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
605 PCI_EXP_SLTCTL_PIC |
606 PCI_EXP_SLTCTL_AIC);
607 pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
608 PCI_EXP_SLTCTL_PIC_OFF |
609 PCI_EXP_SLTCTL_AIC_OFF);
610 pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
611 PCI_EXP_SLTCTL_PIC |
612 PCI_EXP_SLTCTL_AIC |
613 PCI_EXP_SLTCTL_HPIE |
614 PCI_EXP_SLTCTL_CCIE |
615 PCI_EXP_SLTCTL_PDCE |
616 PCI_EXP_SLTCTL_ABPE);
617 /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
618 * make the bit writable here in order to detect 1b is written.
619 * pcie_cap_slot_write_config() test-and-clear the bit, so
620 * this bit always returns 0 to the guest.
621 */
622 pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
623 PCI_EXP_SLTCTL_EIC);
624
625 pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
626 PCI_EXP_HP_EV_SUPPORTED);
627
6bde6aaa
MT
628 dev->exp.hpev_notified = false;
629
a66e657e 630 qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev))),
9bc6bfdf 631 OBJECT(dev));
0428527c
IY
632}
633
634void pcie_cap_slot_reset(PCIDevice *dev)
635{
636 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
f23b6bdc
MA
637 uint8_t port_type = pcie_cap_get_type(dev);
638
639 assert(port_type == PCI_EXP_TYPE_DOWNSTREAM ||
640 port_type == PCI_EXP_TYPE_ROOT_PORT);
0428527c
IY
641
642 PCIE_DEV_PRINTF(dev, "reset\n");
643
644 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
645 PCI_EXP_SLTCTL_EIC |
646 PCI_EXP_SLTCTL_PIC |
647 PCI_EXP_SLTCTL_AIC |
648 PCI_EXP_SLTCTL_HPIE |
649 PCI_EXP_SLTCTL_CCIE |
650 PCI_EXP_SLTCTL_PDCE |
651 PCI_EXP_SLTCTL_ABPE);
652 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
0428527c
IY
653 PCI_EXP_SLTCTL_AIC_OFF);
654
f23b6bdc 655 if (dev->cap_present & QEMU_PCIE_SLTCAP_PCP) {
f23b6bdc 656 /* Downstream ports enforce device number 0. */
20de98af
MT
657 bool populated = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
658 uint16_t pic;
f23b6bdc
MA
659
660 if (populated) {
661 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
662 PCI_EXP_SLTCTL_PCC);
663 } else {
664 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
665 PCI_EXP_SLTCTL_PCC);
666 }
667
668 pic = populated ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
669 pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL, pic);
20de98af 670 }
f23b6bdc 671
0428527c
IY
672 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
673 PCI_EXP_SLTSTA_EIS |/* on reset,
674 the lock is released */
675 PCI_EXP_SLTSTA_CC |
676 PCI_EXP_SLTSTA_PDC |
677 PCI_EXP_SLTSTA_ABP);
6bde6aaa 678
d5daff7d 679 pcie_cap_update_power(dev);
804b2071 680 hotplug_event_update_event_status(dev);
0428527c
IY
681}
682
2841ab43
MT
683void pcie_cap_slot_get(PCIDevice *dev, uint16_t *slt_ctl, uint16_t *slt_sta)
684{
685 uint32_t pos = dev->exp.exp_cap;
686 uint8_t *exp_cap = dev->config + pos;
687 *slt_ctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
688 *slt_sta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
689}
690
d85d65cc
MT
691void pcie_cap_slot_write_config(PCIDevice *dev,
692 uint16_t old_slt_ctl, uint16_t old_slt_sta,
6bde6aaa 693 uint32_t addr, uint32_t val, int len)
0428527c
IY
694{
695 uint32_t pos = dev->exp.exp_cap;
696 uint8_t *exp_cap = dev->config + pos;
0428527c
IY
697 uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
698
1553d4f1 699 if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
110c477c
MT
700 /*
701 * Guests tend to clears all bits during init.
702 * If they clear bits that weren't set this is racy and will lose events:
703 * not a big problem for manual button presses, but a problem for us.
704 * As a work-around, detect this and revert status to what it was
705 * before the write.
706 *
707 * Note: in theory this can be detected as a duplicate button press
708 * which cancels the previous press. Does not seem to happen in
709 * practice as guests seem to only have this bug during init.
710 */
711#define PCIE_SLOT_EVENTS (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD | \
712 PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC | \
713 PCI_EXP_SLTSTA_CC)
714
d85d65cc
MT
715 if (val & ~old_slt_sta & PCIE_SLOT_EVENTS) {
716 sltsta = (sltsta & ~PCIE_SLOT_EVENTS) | (old_slt_sta & PCIE_SLOT_EVENTS);
110c477c
MT
717 pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
718 }
1553d4f1
IY
719 hotplug_event_clear(dev);
720 }
721
ac0cdda3
MT
722 if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
723 return;
724 }
725
ac0cdda3
MT
726 if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
727 PCI_EXP_SLTCTL_EIC)) {
728 sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
729 pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
730 PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
731 "sltsta -> 0x%02"PRIx16"\n",
732 sltsta);
733 }
0428527c 734
554f802d 735 /*
d85d65cc 736 * If the slot is populated, power indicator is off and power
554f802d 737 * controller is off, it is safe to detach the devices.
d85d65cc
MT
738 *
739 * Note: don't detach if condition was already true:
740 * this is a work around for guests that overwrite
741 * control of powered off slots before powering them on.
554f802d
MA
742 */
743 if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
2841ab43 744 (val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF &&
d85d65cc
MT
745 (!(old_slt_ctl & PCI_EXP_SLTCTL_PCC) ||
746 (old_slt_ctl & PCI_EXP_SLTCTL_PIC_OFF) != PCI_EXP_SLTCTL_PIC_OFF)) {
44242d4d 747 pcie_cap_slot_do_unplug(dev);
554f802d 748 }
d5daff7d 749 pcie_cap_update_power(dev);
554f802d 750
6bde6aaa 751 hotplug_event_notify(dev);
ac0cdda3
MT
752
753 /*
754 * 6.7.3.2 Command Completed Events
755 *
756 * Software issues a command to a hot-plug capable Downstream Port by
757 * issuing a write transaction that targets any portion of the Port’s Slot
758 * Control register. A single write to the Slot Control register is
759 * considered to be a single command, even if the write affects more than
760 * one field in the Slot Control register. In response to this transaction,
761 * the Port must carry out the requested actions and then set the
762 * associated status field for the command completed event. */
763
764 /* Real hardware might take a while to complete requested command because
765 * physical movement would be involved like locking the electromechanical
766 * lock. However in our case, command is completed instantaneously above,
767 * so send a command completion event right now.
768 */
769 pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
0428527c
IY
770}
771
6bde6aaa
MT
772int pcie_cap_slot_post_load(void *opaque, int version_id)
773{
774 PCIDevice *dev = opaque;
775 hotplug_event_update_event_status(dev);
d5daff7d 776 pcie_cap_update_power(dev);
6bde6aaa
MT
777 return 0;
778}
779
0428527c
IY
780void pcie_cap_slot_push_attention_button(PCIDevice *dev)
781{
782 pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
783}
784
785/* root control/capabilities/status. PME isn't emulated for now */
786void pcie_cap_root_init(PCIDevice *dev)
787{
788 pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
789 PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
790 PCI_EXP_RTCTL_SEFEE);
791}
792
793void pcie_cap_root_reset(PCIDevice *dev)
794{
795 pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
796}
797
0428527c
IY
798/* function level reset(FLR) */
799void pcie_cap_flr_init(PCIDevice *dev)
800{
801 pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
802 PCI_EXP_DEVCAP_FLR);
803
804 /* Although reading BCR_FLR returns always 0,
805 * the bit is made writable here in order to detect the 1b is written
806 * pcie_cap_flr_write_config() test-and-clear the bit, so
807 * this bit always returns 0 to the guest.
808 */
809 pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
810 PCI_EXP_DEVCTL_BCR_FLR);
811}
812
813void pcie_cap_flr_write_config(PCIDevice *dev,
814 uint32_t addr, uint32_t val, int len)
815{
816 uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
0ead87c8
IY
817 if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
818 /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
819 so the handler can detect FLR by looking at this bit. */
820 pci_device_reset(dev);
821 pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
0428527c
IY
822 }
823}
824
821be9db 825/* Alternative Routing-ID Interpretation (ARI)
187de915 826 * forwarding support for root and downstream ports
821be9db
KO
827 */
828void pcie_cap_arifwd_init(PCIDevice *dev)
0428527c
IY
829{
830 uint32_t pos = dev->exp.exp_cap;
831 pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
832 PCI_EXP_DEVCAP2_ARI);
833 pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
834 PCI_EXP_DEVCTL2_ARI);
835}
836
821be9db 837void pcie_cap_arifwd_reset(PCIDevice *dev)
0428527c
IY
838{
839 uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
840 pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
841}
842
821be9db 843bool pcie_cap_is_arifwd_enabled(const PCIDevice *dev)
0428527c
IY
844{
845 if (!pci_is_express(dev)) {
846 return false;
847 }
848 if (!dev->exp.exp_cap) {
849 return false;
850 }
851
852 return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
853 PCI_EXP_DEVCTL2_ARI;
854}
855
856/**************************************************************************
4d5e17a5 857 * pci express extended capability list management functions
0428527c
IY
858 * uint16_t ext_cap_id (16 bit)
859 * uint8_t cap_ver (4 bit)
860 * uint16_t cap_offset (12 bit)
861 * uint16_t ext_cap_size
862 */
863
4bb571d8
MT
864/* Passing a cap_id value > 0xffff will return 0 and put end of list in prev */
865static uint16_t pcie_find_capability_list(PCIDevice *dev, uint32_t cap_id,
0428527c
IY
866 uint16_t *prev_p)
867{
868 uint16_t prev = 0;
869 uint16_t next;
870 uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
871
872 if (!header) {
873 /* no extended capability */
874 next = 0;
875 goto out;
876 }
877 for (next = PCI_CONFIG_SPACE_SIZE; next;
878 prev = next, next = PCI_EXT_CAP_NEXT(header)) {
879
880 assert(next >= PCI_CONFIG_SPACE_SIZE);
881 assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
882
883 header = pci_get_long(dev->config + next);
884 if (PCI_EXT_CAP_ID(header) == cap_id) {
885 break;
886 }
887 }
888
889out:
890 if (prev_p) {
891 *prev_p = prev;
892 }
893 return next;
894}
895
896uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
897{
898 return pcie_find_capability_list(dev, cap_id, NULL);
899}
900
901static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
902{
812d2594 903 uint32_t header = pci_get_long(dev->config + pos);
0428527c
IY
904 assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
905 header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
906 ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
907 pci_set_long(dev->config + pos, header);
908}
909
910/*
d62d1eb6 911 * Caller must supply valid (offset, size) such that the range wouldn't
0428527c
IY
912 * overlap with other capability or other registers.
913 * This function doesn't check it.
914 */
915void pcie_add_capability(PCIDevice *dev,
916 uint16_t cap_id, uint8_t cap_ver,
917 uint16_t offset, uint16_t size)
918{
0428527c
IY
919 assert(offset >= PCI_CONFIG_SPACE_SIZE);
920 assert(offset < offset + size);
79095ef7 921 assert(offset + size <= PCIE_CONFIG_SPACE_SIZE);
0428527c
IY
922 assert(size >= 8);
923 assert(pci_is_express(dev));
924
d4e9b75a 925 if (offset != PCI_CONFIG_SPACE_SIZE) {
0428527c
IY
926 uint16_t prev;
927
4bb571d8
MT
928 /*
929 * 0xffffffff is not a valid cap id (it's a 16 bit field). use
930 * internally to find the last capability in the linked list.
931 */
d4e9b75a 932 pcie_find_capability_list(dev, 0xffffffff, &prev);
0428527c 933 assert(prev >= PCI_CONFIG_SPACE_SIZE);
0428527c
IY
934 pcie_ext_cap_set_next(dev, prev, offset);
935 }
d4e9b75a 936 pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, 0));
0428527c
IY
937
938 /* Make capability read-only by default */
939 memset(dev->wmask + offset, 0, size);
940 memset(dev->w1cmask + offset, 0, size);
941 /* Check capability by default */
942 memset(dev->cmask + offset, 0xFF, size);
943}
944
727b4866
AW
945/*
946 * Sync the PCIe Link Status negotiated speed and width of a bridge with the
947 * downstream device. If downstream device is not present, re-write with the
88c86919
AW
948 * Link Capability fields. If downstream device reports invalid width or
949 * speed, replace with minimum values (LnkSta fields are RsvdZ on VFs but such
950 * values interfere with PCIe native hotplug detecting new devices). Limit
951 * width and speed to bridge capabilities for compatibility. Use config_read
952 * to access the downstream device since it could be an assigned device with
953 * volatile link information.
727b4866
AW
954 */
955void pcie_sync_bridge_lnk(PCIDevice *bridge_dev)
956{
957 PCIBridge *br = PCI_BRIDGE(bridge_dev);
958 PCIBus *bus = pci_bridge_get_sec_bus(br);
959 PCIDevice *target = bus->devices[0];
960 uint8_t *exp_cap = bridge_dev->config + bridge_dev->exp.exp_cap;
961 uint16_t lnksta, lnkcap = pci_get_word(exp_cap + PCI_EXP_LNKCAP);
962
963 if (!target || !target->exp.exp_cap) {
964 lnksta = lnkcap;
965 } else {
966 lnksta = target->config_read(target,
967 target->exp.exp_cap + PCI_EXP_LNKSTA,
968 sizeof(lnksta));
969
970 if ((lnksta & PCI_EXP_LNKSTA_NLW) > (lnkcap & PCI_EXP_LNKCAP_MLW)) {
971 lnksta &= ~PCI_EXP_LNKSTA_NLW;
972 lnksta |= lnkcap & PCI_EXP_LNKCAP_MLW;
88c86919
AW
973 } else if (!(lnksta & PCI_EXP_LNKSTA_NLW)) {
974 lnksta |= QEMU_PCI_EXP_LNKSTA_NLW(QEMU_PCI_EXP_LNK_X1);
727b4866
AW
975 }
976
977 if ((lnksta & PCI_EXP_LNKSTA_CLS) > (lnkcap & PCI_EXP_LNKCAP_SLS)) {
978 lnksta &= ~PCI_EXP_LNKSTA_CLS;
979 lnksta |= lnkcap & PCI_EXP_LNKCAP_SLS;
88c86919
AW
980 } else if (!(lnksta & PCI_EXP_LNKSTA_CLS)) {
981 lnksta |= QEMU_PCI_EXP_LNKSTA_CLS(QEMU_PCI_EXP_LNK_2_5GT);
727b4866
AW
982 }
983 }
984
985 pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
986 PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW);
987 pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA, lnksta &
988 (PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW));
989}
990
0428527c
IY
991/**************************************************************************
992 * pci express extended capability helper functions
993 */
994
995/* ARI */
996void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
997{
998 pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
999 offset, PCI_ARI_SIZEOF);
ec70b46b 1000 pci_set_long(dev->config + offset + PCI_ARI_CAP, (nextfn & 0xff) << 8);
0428527c 1001}
b56b9285
DF
1002
1003void pcie_dev_ser_num_init(PCIDevice *dev, uint16_t offset, uint64_t ser_num)
1004{
1005 static const int pci_dsn_ver = 1;
1006 static const int pci_dsn_cap = 4;
1007
1008 pcie_add_capability(dev, PCI_EXT_CAP_ID_DSN, pci_dsn_ver, offset,
1009 PCI_EXT_CAP_DSN_SIZEOF);
1010 pci_set_quad(dev->config + offset + pci_dsn_cap, ser_num);
1011}
615c4ed2 1012
d83f46d1 1013void pcie_ats_init(PCIDevice *dev, uint16_t offset, bool aligned)
615c4ed2
JW
1014{
1015 pcie_add_capability(dev, PCI_EXT_CAP_ID_ATS, 0x1,
1016 offset, PCI_EXT_CAP_ATS_SIZEOF);
1017
1018 dev->exp.ats_cap = offset;
1019
d83f46d1
JW
1020 /* Invalidate Queue Depth 0 */
1021 if (aligned) {
1022 pci_set_word(dev->config + offset + PCI_ATS_CAP,
1023 PCI_ATS_CAP_PAGE_ALIGNED);
1024 }
615c4ed2
JW
1025 /* STU 0, Disabled by default */
1026 pci_set_word(dev->config + offset + PCI_ATS_CTRL, 0);
1027
1028 pci_set_word(dev->wmask + dev->exp.ats_cap + PCI_ATS_CTRL, 0x800f);
1029}
db891a9b
KO
1030
1031/* ACS (Access Control Services) */
1032void pcie_acs_init(PCIDevice *dev, uint16_t offset)
1033{
1034 bool is_downstream = pci_is_express_downstream_port(dev);
1035 uint16_t cap_bits = 0;
1036
1037 /* For endpoints, only multifunction devs may have an ACS capability: */
1038 assert(is_downstream ||
1039 (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) ||
1040 PCI_FUNC(dev->devfn));
1041
1042 pcie_add_capability(dev, PCI_EXT_CAP_ID_ACS, PCI_ACS_VER, offset,
1043 PCI_ACS_SIZEOF);
1044 dev->exp.acs_cap = offset;
1045
1046 if (is_downstream) {
1047 /*
1048 * Downstream ports must implement SV, TB, RR, CR, UF, and DT (with
1049 * caveats on the latter four that we ignore for simplicity).
1050 * Endpoints may also implement a subset of ACS capabilities,
1051 * but these are optional if the endpoint does not support
1052 * peer-to-peer between functions and thus omitted here.
1053 */
1054 cap_bits = PCI_ACS_SV | PCI_ACS_TB | PCI_ACS_RR |
1055 PCI_ACS_CR | PCI_ACS_UF | PCI_ACS_DT;
1056 }
1057
1058 pci_set_word(dev->config + offset + PCI_ACS_CAP, cap_bits);
1059 pci_set_word(dev->wmask + offset + PCI_ACS_CTRL, cap_bits);
1060}
1061
1062void pcie_acs_reset(PCIDevice *dev)
1063{
1064 if (dev->exp.acs_cap) {
1065 pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
1066 }
1067}