2
0

pcie.c 35 KB

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