pcie.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  352. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  353. uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
  354. /* Check if hot-plug is disabled on the slot */
  355. if (dev->hotplugged && (sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
  356. error_setg(errp, "Hot-plug failed: unsupported by the port device '%s'",
  357. DEVICE(hotplug_pdev)->id);
  358. return;
  359. }
  360. pcie_cap_slot_plug_common(PCI_DEVICE(hotplug_dev), dev, errp);
  361. }
  362. void pcie_cap_slot_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  363. Error **errp)
  364. {
  365. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  366. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  367. PCIDevice *pci_dev = PCI_DEVICE(dev);
  368. /* Don't send event when device is enabled during qemu machine creation:
  369. * it is present on boot, no hotplug event is necessary. We do send an
  370. * event when the device is disabled later. */
  371. if (!dev->hotplugged) {
  372. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  373. PCI_EXP_SLTSTA_PDS);
  374. if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA) {
  375. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
  376. PCI_EXP_LNKSTA_DLLLA);
  377. }
  378. return;
  379. }
  380. /* To enable multifunction hot-plug, we just ensure the function
  381. * 0 added last. When function 0 is added, we set the sltsta and
  382. * inform OS via event notification.
  383. */
  384. if (pci_get_function_0(pci_dev)) {
  385. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  386. PCI_EXP_SLTSTA_PDS);
  387. if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA) {
  388. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
  389. PCI_EXP_LNKSTA_DLLLA);
  390. }
  391. pcie_cap_slot_event(hotplug_pdev,
  392. PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
  393. }
  394. }
  395. void pcie_cap_slot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  396. Error **errp)
  397. {
  398. qdev_unrealize(dev);
  399. }
  400. static void pcie_unplug_device(PCIBus *bus, PCIDevice *dev, void *opaque)
  401. {
  402. HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(DEVICE(dev));
  403. if (dev->partially_hotplugged) {
  404. dev->qdev.pending_deleted_event = false;
  405. return;
  406. }
  407. hotplug_handler_unplug(hotplug_ctrl, DEVICE(dev), &error_abort);
  408. object_unparent(OBJECT(dev));
  409. }
  410. void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
  411. DeviceState *dev, Error **errp)
  412. {
  413. Error *local_err = NULL;
  414. PCIDevice *pci_dev = PCI_DEVICE(dev);
  415. PCIBus *bus = pci_get_bus(pci_dev);
  416. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  417. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  418. uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
  419. /* Check if hot-unplug is disabled on the slot */
  420. if ((sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
  421. error_setg(errp, "Hot-unplug failed: "
  422. "unsupported by the port device '%s'",
  423. DEVICE(hotplug_pdev)->id);
  424. return;
  425. }
  426. pcie_cap_slot_plug_common(hotplug_pdev, dev, &local_err);
  427. if (local_err) {
  428. error_propagate(errp, local_err);
  429. return;
  430. }
  431. dev->pending_deleted_event = true;
  432. /* In case user cancel the operation of multi-function hot-add,
  433. * remove the function that is unexposed to guest individually,
  434. * without interaction with guest.
  435. */
  436. if (pci_dev->devfn &&
  437. !bus->devices[0]) {
  438. pcie_unplug_device(bus, pci_dev, NULL);
  439. return;
  440. }
  441. pcie_cap_slot_push_attention_button(hotplug_pdev);
  442. }
  443. /* pci express slot for pci express root/downstream port
  444. PCI express capability slot registers */
  445. void pcie_cap_slot_init(PCIDevice *dev, PCIESlot *s)
  446. {
  447. uint32_t pos = dev->exp.exp_cap;
  448. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
  449. PCI_EXP_FLAGS_SLOT);
  450. pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
  451. ~PCI_EXP_SLTCAP_PSN);
  452. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  453. (s->slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
  454. PCI_EXP_SLTCAP_EIP |
  455. PCI_EXP_SLTCAP_PIP |
  456. PCI_EXP_SLTCAP_AIP |
  457. PCI_EXP_SLTCAP_ABP);
  458. if (s->hotplug) {
  459. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  460. PCI_EXP_SLTCAP_HPS |
  461. PCI_EXP_SLTCAP_HPC);
  462. }
  463. if (dev->cap_present & QEMU_PCIE_SLTCAP_PCP) {
  464. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  465. PCI_EXP_SLTCAP_PCP);
  466. pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
  467. PCI_EXP_SLTCTL_PCC);
  468. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  469. PCI_EXP_SLTCTL_PCC);
  470. }
  471. pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
  472. PCI_EXP_SLTCTL_PIC |
  473. PCI_EXP_SLTCTL_AIC);
  474. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
  475. PCI_EXP_SLTCTL_PIC_OFF |
  476. PCI_EXP_SLTCTL_AIC_OFF);
  477. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  478. PCI_EXP_SLTCTL_PIC |
  479. PCI_EXP_SLTCTL_AIC |
  480. PCI_EXP_SLTCTL_HPIE |
  481. PCI_EXP_SLTCTL_CCIE |
  482. PCI_EXP_SLTCTL_PDCE |
  483. PCI_EXP_SLTCTL_ABPE);
  484. /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
  485. * make the bit writable here in order to detect 1b is written.
  486. * pcie_cap_slot_write_config() test-and-clear the bit, so
  487. * this bit always returns 0 to the guest.
  488. */
  489. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  490. PCI_EXP_SLTCTL_EIC);
  491. pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
  492. PCI_EXP_HP_EV_SUPPORTED);
  493. dev->exp.hpev_notified = false;
  494. qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev))),
  495. OBJECT(dev));
  496. }
  497. void pcie_cap_slot_reset(PCIDevice *dev)
  498. {
  499. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  500. uint8_t port_type = pcie_cap_get_type(dev);
  501. assert(port_type == PCI_EXP_TYPE_DOWNSTREAM ||
  502. port_type == PCI_EXP_TYPE_ROOT_PORT);
  503. PCIE_DEV_PRINTF(dev, "reset\n");
  504. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  505. PCI_EXP_SLTCTL_EIC |
  506. PCI_EXP_SLTCTL_PIC |
  507. PCI_EXP_SLTCTL_AIC |
  508. PCI_EXP_SLTCTL_HPIE |
  509. PCI_EXP_SLTCTL_CCIE |
  510. PCI_EXP_SLTCTL_PDCE |
  511. PCI_EXP_SLTCTL_ABPE);
  512. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  513. PCI_EXP_SLTCTL_AIC_OFF);
  514. if (dev->cap_present & QEMU_PCIE_SLTCAP_PCP) {
  515. /* Downstream ports enforce device number 0. */
  516. bool populated = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
  517. uint16_t pic;
  518. if (populated) {
  519. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  520. PCI_EXP_SLTCTL_PCC);
  521. } else {
  522. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  523. PCI_EXP_SLTCTL_PCC);
  524. }
  525. pic = populated ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
  526. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL, pic);
  527. }
  528. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  529. PCI_EXP_SLTSTA_EIS |/* on reset,
  530. the lock is released */
  531. PCI_EXP_SLTSTA_CC |
  532. PCI_EXP_SLTSTA_PDC |
  533. PCI_EXP_SLTSTA_ABP);
  534. hotplug_event_update_event_status(dev);
  535. }
  536. void pcie_cap_slot_get(PCIDevice *dev, uint16_t *slt_ctl, uint16_t *slt_sta)
  537. {
  538. uint32_t pos = dev->exp.exp_cap;
  539. uint8_t *exp_cap = dev->config + pos;
  540. *slt_ctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
  541. *slt_sta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  542. }
  543. void pcie_cap_slot_write_config(PCIDevice *dev,
  544. uint16_t old_slt_ctl, uint16_t old_slt_sta,
  545. uint32_t addr, uint32_t val, int len)
  546. {
  547. uint32_t pos = dev->exp.exp_cap;
  548. uint8_t *exp_cap = dev->config + pos;
  549. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  550. if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
  551. /*
  552. * Guests tend to clears all bits during init.
  553. * If they clear bits that weren't set this is racy and will lose events:
  554. * not a big problem for manual button presses, but a problem for us.
  555. * As a work-around, detect this and revert status to what it was
  556. * before the write.
  557. *
  558. * Note: in theory this can be detected as a duplicate button press
  559. * which cancels the previous press. Does not seem to happen in
  560. * practice as guests seem to only have this bug during init.
  561. */
  562. #define PCIE_SLOT_EVENTS (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD | \
  563. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC | \
  564. PCI_EXP_SLTSTA_CC)
  565. if (val & ~old_slt_sta & PCIE_SLOT_EVENTS) {
  566. sltsta = (sltsta & ~PCIE_SLOT_EVENTS) | (old_slt_sta & PCIE_SLOT_EVENTS);
  567. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  568. }
  569. hotplug_event_clear(dev);
  570. }
  571. if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
  572. return;
  573. }
  574. if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  575. PCI_EXP_SLTCTL_EIC)) {
  576. sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
  577. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  578. PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
  579. "sltsta -> 0x%02"PRIx16"\n",
  580. sltsta);
  581. }
  582. /*
  583. * If the slot is populated, power indicator is off and power
  584. * controller is off, it is safe to detach the devices.
  585. *
  586. * Note: don't detach if condition was already true:
  587. * this is a work around for guests that overwrite
  588. * control of powered off slots before powering them on.
  589. */
  590. if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
  591. (val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF &&
  592. (!(old_slt_ctl & PCI_EXP_SLTCTL_PCC) ||
  593. (old_slt_ctl & PCI_EXP_SLTCTL_PIC_OFF) != PCI_EXP_SLTCTL_PIC_OFF)) {
  594. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
  595. pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
  596. pcie_unplug_device, NULL);
  597. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  598. PCI_EXP_SLTSTA_PDS);
  599. if (dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA) {
  600. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
  601. PCI_EXP_LNKSTA_DLLLA);
  602. }
  603. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  604. PCI_EXP_SLTSTA_PDC);
  605. }
  606. hotplug_event_notify(dev);
  607. /*
  608. * 6.7.3.2 Command Completed Events
  609. *
  610. * Software issues a command to a hot-plug capable Downstream Port by
  611. * issuing a write transaction that targets any portion of the Port’s Slot
  612. * Control register. A single write to the Slot Control register is
  613. * considered to be a single command, even if the write affects more than
  614. * one field in the Slot Control register. In response to this transaction,
  615. * the Port must carry out the requested actions and then set the
  616. * associated status field for the command completed event. */
  617. /* Real hardware might take a while to complete requested command because
  618. * physical movement would be involved like locking the electromechanical
  619. * lock. However in our case, command is completed instantaneously above,
  620. * so send a command completion event right now.
  621. */
  622. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
  623. }
  624. int pcie_cap_slot_post_load(void *opaque, int version_id)
  625. {
  626. PCIDevice *dev = opaque;
  627. hotplug_event_update_event_status(dev);
  628. return 0;
  629. }
  630. void pcie_cap_slot_push_attention_button(PCIDevice *dev)
  631. {
  632. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
  633. }
  634. /* root control/capabilities/status. PME isn't emulated for now */
  635. void pcie_cap_root_init(PCIDevice *dev)
  636. {
  637. pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
  638. PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  639. PCI_EXP_RTCTL_SEFEE);
  640. }
  641. void pcie_cap_root_reset(PCIDevice *dev)
  642. {
  643. pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
  644. }
  645. /* function level reset(FLR) */
  646. void pcie_cap_flr_init(PCIDevice *dev)
  647. {
  648. pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
  649. PCI_EXP_DEVCAP_FLR);
  650. /* Although reading BCR_FLR returns always 0,
  651. * the bit is made writable here in order to detect the 1b is written
  652. * pcie_cap_flr_write_config() test-and-clear the bit, so
  653. * this bit always returns 0 to the guest.
  654. */
  655. pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
  656. PCI_EXP_DEVCTL_BCR_FLR);
  657. }
  658. void pcie_cap_flr_write_config(PCIDevice *dev,
  659. uint32_t addr, uint32_t val, int len)
  660. {
  661. uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
  662. if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
  663. /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
  664. so the handler can detect FLR by looking at this bit. */
  665. pci_device_reset(dev);
  666. pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
  667. }
  668. }
  669. /* Alternative Routing-ID Interpretation (ARI)
  670. * forwarding support for root and downstream ports
  671. */
  672. void pcie_cap_arifwd_init(PCIDevice *dev)
  673. {
  674. uint32_t pos = dev->exp.exp_cap;
  675. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
  676. PCI_EXP_DEVCAP2_ARI);
  677. pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
  678. PCI_EXP_DEVCTL2_ARI);
  679. }
  680. void pcie_cap_arifwd_reset(PCIDevice *dev)
  681. {
  682. uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
  683. pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
  684. }
  685. bool pcie_cap_is_arifwd_enabled(const PCIDevice *dev)
  686. {
  687. if (!pci_is_express(dev)) {
  688. return false;
  689. }
  690. if (!dev->exp.exp_cap) {
  691. return false;
  692. }
  693. return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
  694. PCI_EXP_DEVCTL2_ARI;
  695. }
  696. /**************************************************************************
  697. * pci express extended capability list management functions
  698. * uint16_t ext_cap_id (16 bit)
  699. * uint8_t cap_ver (4 bit)
  700. * uint16_t cap_offset (12 bit)
  701. * uint16_t ext_cap_size
  702. */
  703. /* Passing a cap_id value > 0xffff will return 0 and put end of list in prev */
  704. static uint16_t pcie_find_capability_list(PCIDevice *dev, uint32_t cap_id,
  705. uint16_t *prev_p)
  706. {
  707. uint16_t prev = 0;
  708. uint16_t next;
  709. uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
  710. if (!header) {
  711. /* no extended capability */
  712. next = 0;
  713. goto out;
  714. }
  715. for (next = PCI_CONFIG_SPACE_SIZE; next;
  716. prev = next, next = PCI_EXT_CAP_NEXT(header)) {
  717. assert(next >= PCI_CONFIG_SPACE_SIZE);
  718. assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
  719. header = pci_get_long(dev->config + next);
  720. if (PCI_EXT_CAP_ID(header) == cap_id) {
  721. break;
  722. }
  723. }
  724. out:
  725. if (prev_p) {
  726. *prev_p = prev;
  727. }
  728. return next;
  729. }
  730. uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
  731. {
  732. return pcie_find_capability_list(dev, cap_id, NULL);
  733. }
  734. static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
  735. {
  736. uint32_t header = pci_get_long(dev->config + pos);
  737. assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
  738. header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
  739. ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
  740. pci_set_long(dev->config + pos, header);
  741. }
  742. /*
  743. * Caller must supply valid (offset, size) such that the range wouldn't
  744. * overlap with other capability or other registers.
  745. * This function doesn't check it.
  746. */
  747. void pcie_add_capability(PCIDevice *dev,
  748. uint16_t cap_id, uint8_t cap_ver,
  749. uint16_t offset, uint16_t size)
  750. {
  751. assert(offset >= PCI_CONFIG_SPACE_SIZE);
  752. assert(offset < offset + size);
  753. assert(offset + size <= PCIE_CONFIG_SPACE_SIZE);
  754. assert(size >= 8);
  755. assert(pci_is_express(dev));
  756. if (offset != PCI_CONFIG_SPACE_SIZE) {
  757. uint16_t prev;
  758. /*
  759. * 0xffffffff is not a valid cap id (it's a 16 bit field). use
  760. * internally to find the last capability in the linked list.
  761. */
  762. pcie_find_capability_list(dev, 0xffffffff, &prev);
  763. assert(prev >= PCI_CONFIG_SPACE_SIZE);
  764. pcie_ext_cap_set_next(dev, prev, offset);
  765. }
  766. pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, 0));
  767. /* Make capability read-only by default */
  768. memset(dev->wmask + offset, 0, size);
  769. memset(dev->w1cmask + offset, 0, size);
  770. /* Check capability by default */
  771. memset(dev->cmask + offset, 0xFF, size);
  772. }
  773. /*
  774. * Sync the PCIe Link Status negotiated speed and width of a bridge with the
  775. * downstream device. If downstream device is not present, re-write with the
  776. * Link Capability fields. If downstream device reports invalid width or
  777. * speed, replace with minimum values (LnkSta fields are RsvdZ on VFs but such
  778. * values interfere with PCIe native hotplug detecting new devices). Limit
  779. * width and speed to bridge capabilities for compatibility. Use config_read
  780. * to access the downstream device since it could be an assigned device with
  781. * volatile link information.
  782. */
  783. void pcie_sync_bridge_lnk(PCIDevice *bridge_dev)
  784. {
  785. PCIBridge *br = PCI_BRIDGE(bridge_dev);
  786. PCIBus *bus = pci_bridge_get_sec_bus(br);
  787. PCIDevice *target = bus->devices[0];
  788. uint8_t *exp_cap = bridge_dev->config + bridge_dev->exp.exp_cap;
  789. uint16_t lnksta, lnkcap = pci_get_word(exp_cap + PCI_EXP_LNKCAP);
  790. if (!target || !target->exp.exp_cap) {
  791. lnksta = lnkcap;
  792. } else {
  793. lnksta = target->config_read(target,
  794. target->exp.exp_cap + PCI_EXP_LNKSTA,
  795. sizeof(lnksta));
  796. if ((lnksta & PCI_EXP_LNKSTA_NLW) > (lnkcap & PCI_EXP_LNKCAP_MLW)) {
  797. lnksta &= ~PCI_EXP_LNKSTA_NLW;
  798. lnksta |= lnkcap & PCI_EXP_LNKCAP_MLW;
  799. } else if (!(lnksta & PCI_EXP_LNKSTA_NLW)) {
  800. lnksta |= QEMU_PCI_EXP_LNKSTA_NLW(QEMU_PCI_EXP_LNK_X1);
  801. }
  802. if ((lnksta & PCI_EXP_LNKSTA_CLS) > (lnkcap & PCI_EXP_LNKCAP_SLS)) {
  803. lnksta &= ~PCI_EXP_LNKSTA_CLS;
  804. lnksta |= lnkcap & PCI_EXP_LNKCAP_SLS;
  805. } else if (!(lnksta & PCI_EXP_LNKSTA_CLS)) {
  806. lnksta |= QEMU_PCI_EXP_LNKSTA_CLS(QEMU_PCI_EXP_LNK_2_5GT);
  807. }
  808. }
  809. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
  810. PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW);
  811. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA, lnksta &
  812. (PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW));
  813. }
  814. /**************************************************************************
  815. * pci express extended capability helper functions
  816. */
  817. /* ARI */
  818. void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
  819. {
  820. pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
  821. offset, PCI_ARI_SIZEOF);
  822. pci_set_long(dev->config + offset + PCI_ARI_CAP, (nextfn & 0xff) << 8);
  823. }
  824. void pcie_dev_ser_num_init(PCIDevice *dev, uint16_t offset, uint64_t ser_num)
  825. {
  826. static const int pci_dsn_ver = 1;
  827. static const int pci_dsn_cap = 4;
  828. pcie_add_capability(dev, PCI_EXT_CAP_ID_DSN, pci_dsn_ver, offset,
  829. PCI_EXT_CAP_DSN_SIZEOF);
  830. pci_set_quad(dev->config + offset + pci_dsn_cap, ser_num);
  831. }
  832. void pcie_ats_init(PCIDevice *dev, uint16_t offset)
  833. {
  834. pcie_add_capability(dev, PCI_EXT_CAP_ID_ATS, 0x1,
  835. offset, PCI_EXT_CAP_ATS_SIZEOF);
  836. dev->exp.ats_cap = offset;
  837. /* Invalidate Queue Depth 0, Page Aligned Request 0 */
  838. pci_set_word(dev->config + offset + PCI_ATS_CAP, 0);
  839. /* STU 0, Disabled by default */
  840. pci_set_word(dev->config + offset + PCI_ATS_CTRL, 0);
  841. pci_set_word(dev->wmask + dev->exp.ats_cap + PCI_ATS_CTRL, 0x800f);
  842. }
  843. /* ACS (Access Control Services) */
  844. void pcie_acs_init(PCIDevice *dev, uint16_t offset)
  845. {
  846. bool is_downstream = pci_is_express_downstream_port(dev);
  847. uint16_t cap_bits = 0;
  848. /* For endpoints, only multifunction devs may have an ACS capability: */
  849. assert(is_downstream ||
  850. (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) ||
  851. PCI_FUNC(dev->devfn));
  852. pcie_add_capability(dev, PCI_EXT_CAP_ID_ACS, PCI_ACS_VER, offset,
  853. PCI_ACS_SIZEOF);
  854. dev->exp.acs_cap = offset;
  855. if (is_downstream) {
  856. /*
  857. * Downstream ports must implement SV, TB, RR, CR, UF, and DT (with
  858. * caveats on the latter four that we ignore for simplicity).
  859. * Endpoints may also implement a subset of ACS capabilities,
  860. * but these are optional if the endpoint does not support
  861. * peer-to-peer between functions and thus omitted here.
  862. */
  863. cap_bits = PCI_ACS_SV | PCI_ACS_TB | PCI_ACS_RR |
  864. PCI_ACS_CR | PCI_ACS_UF | PCI_ACS_DT;
  865. }
  866. pci_set_word(dev->config + offset + PCI_ACS_CAP, cap_bits);
  867. pci_set_word(dev->wmask + offset + PCI_ACS_CTRL, cap_bits);
  868. }
  869. void pcie_acs_reset(PCIDevice *dev)
  870. {
  871. if (dev->exp.acs_cap) {
  872. pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
  873. }
  874. }