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