pcie.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. static bool pcie_sltctl_powered_off(uint16_t sltctl)
  40. {
  41. return (sltctl & PCI_EXP_SLTCTL_PCC) == PCI_EXP_SLTCTL_PWR_OFF
  42. && (sltctl & PCI_EXP_SLTCTL_PIC) == PCI_EXP_SLTCTL_PWR_IND_OFF;
  43. }
  44. /***************************************************************************
  45. * pci express capability helper functions
  46. */
  47. static void
  48. pcie_cap_v1_fill(PCIDevice *dev, uint8_t port, uint8_t type, uint8_t version)
  49. {
  50. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  51. uint8_t *cmask = dev->cmask + dev->exp.exp_cap;
  52. /* capability register
  53. interrupt message number defaults to 0 */
  54. pci_set_word(exp_cap + PCI_EXP_FLAGS,
  55. ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
  56. version);
  57. /* device capability register
  58. * table 7-12:
  59. * roll based error reporting bit must be set by all
  60. * Functions conforming to the ECN, PCI Express Base
  61. * Specification, Revision 1.1., or subsequent PCI Express Base
  62. * Specification revisions.
  63. */
  64. pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
  65. pci_set_long(exp_cap + PCI_EXP_LNKCAP,
  66. (port << PCI_EXP_LNKCAP_PN_SHIFT) |
  67. PCI_EXP_LNKCAP_ASPMS_0S |
  68. QEMU_PCI_EXP_LNKCAP_MLW(QEMU_PCI_EXP_LNK_X1) |
  69. QEMU_PCI_EXP_LNKCAP_MLS(QEMU_PCI_EXP_LNK_2_5GT));
  70. pci_set_word(exp_cap + PCI_EXP_LNKSTA,
  71. QEMU_PCI_EXP_LNKSTA_NLW(QEMU_PCI_EXP_LNK_X1) |
  72. QEMU_PCI_EXP_LNKSTA_CLS(QEMU_PCI_EXP_LNK_2_5GT));
  73. /* We changed link status bits over time, and changing them across
  74. * migrations is generally fine as hardware changes them too.
  75. * Let's not bother checking.
  76. */
  77. pci_set_word(cmask + PCI_EXP_LNKSTA, 0);
  78. }
  79. static void pcie_cap_fill_slot_lnk(PCIDevice *dev)
  80. {
  81. PCIESlot *s = (PCIESlot *)object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT);
  82. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  83. /* Skip anything that isn't a PCIESlot */
  84. if (!s) {
  85. return;
  86. }
  87. /* Clear and fill LNKCAP from what was configured above */
  88. pci_long_test_and_clear_mask(exp_cap + PCI_EXP_LNKCAP,
  89. PCI_EXP_LNKCAP_MLW | PCI_EXP_LNKCAP_SLS);
  90. pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
  91. QEMU_PCI_EXP_LNKCAP_MLW(s->width) |
  92. QEMU_PCI_EXP_LNKCAP_MLS(s->speed));
  93. /*
  94. * Link bandwidth notification is required for all root ports and
  95. * downstream ports supporting links wider than x1 or multiple link
  96. * speeds.
  97. */
  98. if (s->width > QEMU_PCI_EXP_LNK_X1 ||
  99. s->speed > QEMU_PCI_EXP_LNK_2_5GT) {
  100. pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
  101. PCI_EXP_LNKCAP_LBNC);
  102. }
  103. if (s->speed > QEMU_PCI_EXP_LNK_2_5GT) {
  104. /*
  105. * Hot-plug capable downstream ports and downstream ports supporting
  106. * link speeds greater than 5GT/s must hardwire PCI_EXP_LNKCAP_DLLLARC
  107. * to 1b. PCI_EXP_LNKCAP_DLLLARC implies PCI_EXP_LNKSTA_DLLLA, which
  108. * we also hardwire to 1b here. 2.5GT/s hot-plug slots should also
  109. * technically implement this, but it's not done here for compatibility.
  110. */
  111. pci_long_test_and_set_mask(exp_cap + PCI_EXP_LNKCAP,
  112. PCI_EXP_LNKCAP_DLLLARC);
  113. /* the PCI_EXP_LNKSTA_DLLLA will be set in the hotplug function */
  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 if (pci_intx(dev) != -1) {
  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) && pci_intx(dev) != -1 &&
  316. !dev->exp.hpev_notified) {
  317. pci_irq_deassert(dev);
  318. }
  319. }
  320. void pcie_cap_slot_enable_power(PCIDevice *dev)
  321. {
  322. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  323. uint32_t sltcap = pci_get_long(exp_cap + PCI_EXP_SLTCAP);
  324. if (sltcap & PCI_EXP_SLTCAP_PCP) {
  325. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  326. PCI_EXP_SLTCTL_PCC);
  327. }
  328. }
  329. static void pcie_set_power_device(PCIBus *bus, PCIDevice *dev, void *opaque)
  330. {
  331. bool *power = opaque;
  332. pci_set_power(dev, *power);
  333. }
  334. static void pcie_cap_update_power(PCIDevice *hotplug_dev)
  335. {
  336. uint8_t *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
  337. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(hotplug_dev));
  338. uint32_t sltcap = pci_get_long(exp_cap + PCI_EXP_SLTCAP);
  339. uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
  340. bool power = true;
  341. if (sltcap & PCI_EXP_SLTCAP_PCP) {
  342. power = (sltctl & PCI_EXP_SLTCTL_PCC) == PCI_EXP_SLTCTL_PWR_ON;
  343. /* Don't we need to check also (sltctl & PCI_EXP_SLTCTL_PIC) ? */
  344. }
  345. pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
  346. pcie_set_power_device, &power);
  347. }
  348. /*
  349. * A PCI Express Hot-Plug Event has occurred, so update slot status register
  350. * and notify OS of the event if necessary.
  351. *
  352. * 6.7.3 PCI Express Hot-Plug Events
  353. * 6.7.3.4 Software Notification of Hot-Plug Events
  354. */
  355. static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
  356. {
  357. /* Minor optimization: if nothing changed - no event is needed. */
  358. if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
  359. PCI_EXP_SLTSTA, event) == event) {
  360. return;
  361. }
  362. hotplug_event_notify(dev);
  363. }
  364. static void pcie_cap_slot_plug_common(PCIDevice *hotplug_dev, DeviceState *dev,
  365. Error **errp)
  366. {
  367. uint8_t *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
  368. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  369. PCIE_DEV_PRINTF(PCI_DEVICE(dev), "hotplug state: 0x%x\n", sltsta);
  370. if (sltsta & PCI_EXP_SLTSTA_EIS) {
  371. /* the slot is electromechanically locked.
  372. * This error is propagated up to qdev and then to HMP/QMP.
  373. */
  374. error_setg_errno(errp, EBUSY, "slot is electromechanically locked");
  375. }
  376. }
  377. void pcie_cap_slot_pre_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  378. Error **errp)
  379. {
  380. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  381. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  382. uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
  383. /* Check if hot-plug is disabled on the slot */
  384. if (dev->hotplugged && (sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
  385. error_setg(errp, "Hot-plug failed: unsupported by the port device '%s'",
  386. DEVICE(hotplug_pdev)->id);
  387. return;
  388. }
  389. pcie_cap_slot_plug_common(PCI_DEVICE(hotplug_dev), dev, errp);
  390. }
  391. void pcie_cap_slot_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  392. Error **errp)
  393. {
  394. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  395. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  396. PCIDevice *pci_dev = PCI_DEVICE(dev);
  397. uint32_t lnkcap = pci_get_long(exp_cap + PCI_EXP_LNKCAP);
  398. if (pci_is_vf(pci_dev)) {
  399. /* Virtual function cannot be physically disconnected */
  400. return;
  401. }
  402. /* Don't send event when device is enabled during qemu machine creation:
  403. * it is present on boot, no hotplug event is necessary. We do send an
  404. * event when the device is disabled later. */
  405. if (!dev->hotplugged) {
  406. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  407. PCI_EXP_SLTSTA_PDS);
  408. if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
  409. (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
  410. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
  411. PCI_EXP_LNKSTA_DLLLA);
  412. }
  413. pcie_cap_update_power(hotplug_pdev);
  414. return;
  415. }
  416. /* To enable multifunction hot-plug, we just ensure the function
  417. * 0 added last. When function 0 is added, we set the sltsta and
  418. * inform OS via event notification.
  419. */
  420. if (pci_get_function_0(pci_dev)) {
  421. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  422. PCI_EXP_SLTSTA_PDS);
  423. if (pci_dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
  424. (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
  425. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA,
  426. PCI_EXP_LNKSTA_DLLLA);
  427. }
  428. pcie_cap_slot_event(hotplug_pdev,
  429. PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
  430. pcie_cap_update_power(hotplug_pdev);
  431. }
  432. }
  433. void pcie_cap_slot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  434. Error **errp)
  435. {
  436. qdev_unrealize(dev);
  437. }
  438. static void pcie_unplug_device(PCIBus *bus, PCIDevice *dev, void *opaque)
  439. {
  440. HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(DEVICE(dev));
  441. if (dev->partially_hotplugged) {
  442. dev->qdev.pending_deleted_event = false;
  443. return;
  444. }
  445. hotplug_handler_unplug(hotplug_ctrl, DEVICE(dev), &error_abort);
  446. object_unparent(OBJECT(dev));
  447. }
  448. static void pcie_cap_slot_do_unplug(PCIDevice *dev)
  449. {
  450. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
  451. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  452. uint32_t lnkcap = pci_get_long(exp_cap + PCI_EXP_LNKCAP);
  453. pci_for_each_device_under_bus(sec_bus, pcie_unplug_device, NULL);
  454. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  455. PCI_EXP_SLTSTA_PDS);
  456. if (dev->cap_present & QEMU_PCIE_LNKSTA_DLLLA ||
  457. (lnkcap & PCI_EXP_LNKCAP_DLLLARC)) {
  458. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
  459. PCI_EXP_LNKSTA_DLLLA);
  460. }
  461. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  462. PCI_EXP_SLTSTA_PDC);
  463. }
  464. void pcie_cap_slot_unplug_request_cb(HotplugHandler *hotplug_dev,
  465. DeviceState *dev, Error **errp)
  466. {
  467. Error *local_err = NULL;
  468. PCIDevice *pci_dev = PCI_DEVICE(dev);
  469. PCIBus *bus = pci_get_bus(pci_dev);
  470. PCIDevice *hotplug_pdev = PCI_DEVICE(hotplug_dev);
  471. uint8_t *exp_cap = hotplug_pdev->config + hotplug_pdev->exp.exp_cap;
  472. uint32_t sltcap = pci_get_word(exp_cap + PCI_EXP_SLTCAP);
  473. uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
  474. /* Check if hot-unplug is disabled on the slot */
  475. if ((sltcap & PCI_EXP_SLTCAP_HPC) == 0) {
  476. error_setg(errp, "Hot-unplug failed: "
  477. "unsupported by the port device '%s'",
  478. DEVICE(hotplug_pdev)->id);
  479. return;
  480. }
  481. pcie_cap_slot_plug_common(hotplug_pdev, dev, &local_err);
  482. if (local_err) {
  483. error_propagate(errp, local_err);
  484. return;
  485. }
  486. if ((sltctl & PCI_EXP_SLTCTL_PIC) == PCI_EXP_SLTCTL_PWR_IND_BLINK) {
  487. error_setg(errp, "Hot-unplug failed: "
  488. "guest is busy (power indicator blinking)");
  489. return;
  490. }
  491. dev->pending_deleted_event = true;
  492. dev->pending_deleted_expires_ms =
  493. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 5000; /* 5 secs */
  494. /* In case user cancel the operation of multi-function hot-add,
  495. * remove the function that is unexposed to guest individually,
  496. * without interaction with guest.
  497. */
  498. if (pci_dev->devfn &&
  499. !bus->devices[0]) {
  500. pcie_unplug_device(bus, pci_dev, NULL);
  501. return;
  502. }
  503. if (pcie_sltctl_powered_off(sltctl)) {
  504. /* slot is powered off -> unplug without round-trip to the guest */
  505. pcie_cap_slot_do_unplug(hotplug_pdev);
  506. hotplug_event_notify(hotplug_pdev);
  507. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  508. PCI_EXP_SLTSTA_ABP);
  509. return;
  510. }
  511. pcie_cap_slot_push_attention_button(hotplug_pdev);
  512. }
  513. /* pci express slot for pci express root/downstream port
  514. PCI express capability slot registers */
  515. void pcie_cap_slot_init(PCIDevice *dev, PCIESlot *s)
  516. {
  517. uint32_t pos = dev->exp.exp_cap;
  518. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
  519. PCI_EXP_FLAGS_SLOT);
  520. pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
  521. ~PCI_EXP_SLTCAP_PSN);
  522. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  523. (s->slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
  524. PCI_EXP_SLTCAP_EIP |
  525. PCI_EXP_SLTCAP_PIP |
  526. PCI_EXP_SLTCAP_AIP |
  527. PCI_EXP_SLTCAP_ABP);
  528. /*
  529. * Expose native hot-plug on all bridges if hot-plug is enabled on the slot.
  530. * (unless broken 6.1 ABI is enforced for compat reasons)
  531. */
  532. if (s->hotplug &&
  533. (!s->hide_native_hotplug_cap || DEVICE(dev)->hotplugged)) {
  534. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  535. PCI_EXP_SLTCAP_HPS |
  536. PCI_EXP_SLTCAP_HPC);
  537. }
  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. pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
  547. PCI_EXP_SLTCTL_PIC |
  548. PCI_EXP_SLTCTL_AIC);
  549. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
  550. PCI_EXP_SLTCTL_PWR_IND_OFF |
  551. PCI_EXP_SLTCTL_ATTN_IND_OFF);
  552. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  553. PCI_EXP_SLTCTL_PIC |
  554. PCI_EXP_SLTCTL_AIC |
  555. PCI_EXP_SLTCTL_HPIE |
  556. PCI_EXP_SLTCTL_CCIE |
  557. PCI_EXP_SLTCTL_PDCE |
  558. PCI_EXP_SLTCTL_ABPE);
  559. /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
  560. * make the bit writable here in order to detect 1b is written.
  561. * pcie_cap_slot_write_config() test-and-clear the bit, so
  562. * this bit always returns 0 to the guest.
  563. */
  564. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  565. PCI_EXP_SLTCTL_EIC);
  566. pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
  567. PCI_EXP_HP_EV_SUPPORTED);
  568. dev->exp.hpev_notified = false;
  569. qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev))),
  570. OBJECT(dev));
  571. }
  572. void pcie_cap_slot_reset(PCIDevice *dev)
  573. {
  574. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  575. uint8_t port_type = pcie_cap_get_type(dev);
  576. assert(port_type == PCI_EXP_TYPE_DOWNSTREAM ||
  577. port_type == PCI_EXP_TYPE_ROOT_PORT);
  578. PCIE_DEV_PRINTF(dev, "reset\n");
  579. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  580. PCI_EXP_SLTCTL_EIC |
  581. PCI_EXP_SLTCTL_PIC |
  582. PCI_EXP_SLTCTL_AIC |
  583. PCI_EXP_SLTCTL_HPIE |
  584. PCI_EXP_SLTCTL_CCIE |
  585. PCI_EXP_SLTCTL_PDCE |
  586. PCI_EXP_SLTCTL_ABPE);
  587. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  588. PCI_EXP_SLTCTL_PWR_IND_OFF |
  589. PCI_EXP_SLTCTL_ATTN_IND_OFF);
  590. if (dev->cap_present & QEMU_PCIE_SLTCAP_PCP) {
  591. /* Downstream ports enforce device number 0. */
  592. bool populated = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
  593. uint16_t pic;
  594. if (populated) {
  595. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  596. PCI_EXP_SLTCTL_PCC);
  597. } else {
  598. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  599. PCI_EXP_SLTCTL_PCC);
  600. }
  601. pic = populated ?
  602. PCI_EXP_SLTCTL_PWR_IND_ON : PCI_EXP_SLTCTL_PWR_IND_OFF;
  603. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL, pic);
  604. }
  605. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  606. PCI_EXP_SLTSTA_EIS |/* on reset,
  607. the lock is released */
  608. PCI_EXP_SLTSTA_CC |
  609. PCI_EXP_SLTSTA_PDC |
  610. PCI_EXP_SLTSTA_ABP);
  611. pcie_cap_update_power(dev);
  612. hotplug_event_update_event_status(dev);
  613. }
  614. void pcie_cap_slot_get(PCIDevice *dev, uint16_t *slt_ctl, uint16_t *slt_sta)
  615. {
  616. uint32_t pos = dev->exp.exp_cap;
  617. uint8_t *exp_cap = dev->config + pos;
  618. *slt_ctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
  619. *slt_sta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  620. }
  621. void pcie_cap_slot_write_config(PCIDevice *dev,
  622. uint16_t old_slt_ctl, uint16_t old_slt_sta,
  623. uint32_t addr, uint32_t val, int len)
  624. {
  625. uint32_t pos = dev->exp.exp_cap;
  626. uint8_t *exp_cap = dev->config + pos;
  627. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  628. if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
  629. /*
  630. * Guests tend to clears all bits during init.
  631. * If they clear bits that weren't set this is racy and will lose events:
  632. * not a big problem for manual button presses, but a problem for us.
  633. * As a work-around, detect this and revert status to what it was
  634. * before the write.
  635. *
  636. * Note: in theory this can be detected as a duplicate button press
  637. * which cancels the previous press. Does not seem to happen in
  638. * practice as guests seem to only have this bug during init.
  639. */
  640. #define PCIE_SLOT_EVENTS (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD | \
  641. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC | \
  642. PCI_EXP_SLTSTA_CC)
  643. if (val & ~old_slt_sta & PCIE_SLOT_EVENTS) {
  644. sltsta = (sltsta & ~PCIE_SLOT_EVENTS) | (old_slt_sta & PCIE_SLOT_EVENTS);
  645. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  646. }
  647. hotplug_event_clear(dev);
  648. }
  649. if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
  650. return;
  651. }
  652. if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  653. PCI_EXP_SLTCTL_EIC)) {
  654. sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
  655. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  656. PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
  657. "sltsta -> 0x%02"PRIx16"\n",
  658. sltsta);
  659. }
  660. /*
  661. * If the slot is populated, power indicator is off and power
  662. * controller is off, it is safe to detach the devices.
  663. *
  664. * Note: don't detach if condition was already true:
  665. * this is a work around for guests that overwrite
  666. * control of powered off slots before powering them on.
  667. */
  668. if ((sltsta & PCI_EXP_SLTSTA_PDS) && pcie_sltctl_powered_off(val) &&
  669. !pcie_sltctl_powered_off(old_slt_ctl))
  670. {
  671. pcie_cap_slot_do_unplug(dev);
  672. }
  673. pcie_cap_update_power(dev);
  674. hotplug_event_notify(dev);
  675. /*
  676. * 6.7.3.2 Command Completed Events
  677. *
  678. * Software issues a command to a hot-plug capable Downstream Port by
  679. * issuing a write transaction that targets any portion of the Port’s Slot
  680. * Control register. A single write to the Slot Control register is
  681. * considered to be a single command, even if the write affects more than
  682. * one field in the Slot Control register. In response to this transaction,
  683. * the Port must carry out the requested actions and then set the
  684. * associated status field for the command completed event. */
  685. /* Real hardware might take a while to complete requested command because
  686. * physical movement would be involved like locking the electromechanical
  687. * lock. However in our case, command is completed instantaneously above,
  688. * so send a command completion event right now.
  689. */
  690. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
  691. }
  692. int pcie_cap_slot_post_load(void *opaque, int version_id)
  693. {
  694. PCIDevice *dev = opaque;
  695. hotplug_event_update_event_status(dev);
  696. pcie_cap_update_power(dev);
  697. return 0;
  698. }
  699. void pcie_cap_slot_push_attention_button(PCIDevice *dev)
  700. {
  701. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
  702. }
  703. /* root control/capabilities/status. PME isn't emulated for now */
  704. void pcie_cap_root_init(PCIDevice *dev)
  705. {
  706. pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
  707. PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  708. PCI_EXP_RTCTL_SEFEE);
  709. }
  710. void pcie_cap_root_reset(PCIDevice *dev)
  711. {
  712. pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
  713. }
  714. /* function level reset(FLR) */
  715. void pcie_cap_flr_init(PCIDevice *dev)
  716. {
  717. pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
  718. PCI_EXP_DEVCAP_FLR);
  719. /* Although reading BCR_FLR returns always 0,
  720. * the bit is made writable here in order to detect the 1b is written
  721. * pcie_cap_flr_write_config() test-and-clear the bit, so
  722. * this bit always returns 0 to the guest.
  723. */
  724. pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
  725. PCI_EXP_DEVCTL_BCR_FLR);
  726. }
  727. void pcie_cap_flr_write_config(PCIDevice *dev,
  728. uint32_t addr, uint32_t val, int len)
  729. {
  730. uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
  731. if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
  732. /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
  733. so the handler can detect FLR by looking at this bit. */
  734. pci_device_reset(dev);
  735. pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
  736. }
  737. }
  738. /* Alternative Routing-ID Interpretation (ARI)
  739. * forwarding support for root and downstream ports
  740. */
  741. void pcie_cap_arifwd_init(PCIDevice *dev)
  742. {
  743. uint32_t pos = dev->exp.exp_cap;
  744. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
  745. PCI_EXP_DEVCAP2_ARI);
  746. pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
  747. PCI_EXP_DEVCTL2_ARI);
  748. }
  749. void pcie_cap_arifwd_reset(PCIDevice *dev)
  750. {
  751. uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
  752. pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
  753. }
  754. bool pcie_cap_is_arifwd_enabled(const PCIDevice *dev)
  755. {
  756. if (!pci_is_express(dev)) {
  757. return false;
  758. }
  759. if (!dev->exp.exp_cap) {
  760. return false;
  761. }
  762. return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
  763. PCI_EXP_DEVCTL2_ARI;
  764. }
  765. /**************************************************************************
  766. * pci express extended capability list management functions
  767. * uint16_t ext_cap_id (16 bit)
  768. * uint8_t cap_ver (4 bit)
  769. * uint16_t cap_offset (12 bit)
  770. * uint16_t ext_cap_size
  771. */
  772. /* Passing a cap_id value > 0xffff will return 0 and put end of list in prev */
  773. static uint16_t pcie_find_capability_list(PCIDevice *dev, uint32_t cap_id,
  774. uint16_t *prev_p)
  775. {
  776. uint16_t prev = 0;
  777. uint16_t next;
  778. uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
  779. if (!header) {
  780. /* no extended capability */
  781. next = 0;
  782. goto out;
  783. }
  784. for (next = PCI_CONFIG_SPACE_SIZE; next;
  785. prev = next, next = PCI_EXT_CAP_NEXT(header)) {
  786. assert(next >= PCI_CONFIG_SPACE_SIZE);
  787. assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
  788. header = pci_get_long(dev->config + next);
  789. if (PCI_EXT_CAP_ID(header) == cap_id) {
  790. break;
  791. }
  792. }
  793. out:
  794. if (prev_p) {
  795. *prev_p = prev;
  796. }
  797. return next;
  798. }
  799. uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
  800. {
  801. return pcie_find_capability_list(dev, cap_id, NULL);
  802. }
  803. static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
  804. {
  805. uint32_t header = pci_get_long(dev->config + pos);
  806. assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
  807. header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
  808. ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
  809. pci_set_long(dev->config + pos, header);
  810. }
  811. /*
  812. * Caller must supply valid (offset, size) such that the range wouldn't
  813. * overlap with other capability or other registers.
  814. * This function doesn't check it.
  815. */
  816. void pcie_add_capability(PCIDevice *dev,
  817. uint16_t cap_id, uint8_t cap_ver,
  818. uint16_t offset, uint16_t size)
  819. {
  820. assert(offset >= PCI_CONFIG_SPACE_SIZE);
  821. assert(offset < (uint16_t)(offset + size));
  822. assert((uint16_t)(offset + size) <= PCIE_CONFIG_SPACE_SIZE);
  823. assert(size >= 8);
  824. assert(pci_is_express(dev));
  825. if (offset != PCI_CONFIG_SPACE_SIZE) {
  826. uint16_t prev;
  827. /*
  828. * 0xffffffff is not a valid cap id (it's a 16 bit field). use
  829. * internally to find the last capability in the linked list.
  830. */
  831. pcie_find_capability_list(dev, 0xffffffff, &prev);
  832. assert(prev >= PCI_CONFIG_SPACE_SIZE);
  833. pcie_ext_cap_set_next(dev, prev, offset);
  834. }
  835. pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, 0));
  836. /* Make capability read-only by default */
  837. memset(dev->wmask + offset, 0, size);
  838. memset(dev->w1cmask + offset, 0, size);
  839. /* Check capability by default */
  840. memset(dev->cmask + offset, 0xFF, size);
  841. }
  842. /*
  843. * Sync the PCIe Link Status negotiated speed and width of a bridge with the
  844. * downstream device. If downstream device is not present, re-write with the
  845. * Link Capability fields. If downstream device reports invalid width or
  846. * speed, replace with minimum values (LnkSta fields are RsvdZ on VFs but such
  847. * values interfere with PCIe native hotplug detecting new devices). Limit
  848. * width and speed to bridge capabilities for compatibility. Use config_read
  849. * to access the downstream device since it could be an assigned device with
  850. * volatile link information.
  851. */
  852. void pcie_sync_bridge_lnk(PCIDevice *bridge_dev)
  853. {
  854. PCIBridge *br = PCI_BRIDGE(bridge_dev);
  855. PCIBus *bus = pci_bridge_get_sec_bus(br);
  856. PCIDevice *target = bus->devices[0];
  857. uint8_t *exp_cap = bridge_dev->config + bridge_dev->exp.exp_cap;
  858. uint16_t lnksta, lnkcap = pci_get_word(exp_cap + PCI_EXP_LNKCAP);
  859. if (!target || !target->exp.exp_cap) {
  860. lnksta = lnkcap;
  861. } else {
  862. lnksta = target->config_read(target,
  863. target->exp.exp_cap + PCI_EXP_LNKSTA,
  864. sizeof(lnksta));
  865. if ((lnksta & PCI_EXP_LNKSTA_NLW) > (lnkcap & PCI_EXP_LNKCAP_MLW)) {
  866. lnksta &= ~PCI_EXP_LNKSTA_NLW;
  867. lnksta |= lnkcap & PCI_EXP_LNKCAP_MLW;
  868. } else if (!(lnksta & PCI_EXP_LNKSTA_NLW)) {
  869. lnksta |= QEMU_PCI_EXP_LNKSTA_NLW(QEMU_PCI_EXP_LNK_X1);
  870. }
  871. if ((lnksta & PCI_EXP_LNKSTA_CLS) > (lnkcap & PCI_EXP_LNKCAP_SLS)) {
  872. lnksta &= ~PCI_EXP_LNKSTA_CLS;
  873. lnksta |= lnkcap & PCI_EXP_LNKCAP_SLS;
  874. } else if (!(lnksta & PCI_EXP_LNKSTA_CLS)) {
  875. lnksta |= QEMU_PCI_EXP_LNKSTA_CLS(QEMU_PCI_EXP_LNK_2_5GT);
  876. }
  877. }
  878. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_LNKSTA,
  879. PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW);
  880. pci_word_test_and_set_mask(exp_cap + PCI_EXP_LNKSTA, lnksta &
  881. (PCI_EXP_LNKSTA_CLS | PCI_EXP_LNKSTA_NLW));
  882. }
  883. /**************************************************************************
  884. * pci express extended capability helper functions
  885. */
  886. /* ARI */
  887. void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
  888. {
  889. pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
  890. offset, PCI_ARI_SIZEOF);
  891. pci_set_long(dev->config + offset + PCI_ARI_CAP, (nextfn & 0xff) << 8);
  892. }
  893. void pcie_dev_ser_num_init(PCIDevice *dev, uint16_t offset, uint64_t ser_num)
  894. {
  895. static const int pci_dsn_ver = 1;
  896. static const int pci_dsn_cap = 4;
  897. pcie_add_capability(dev, PCI_EXT_CAP_ID_DSN, pci_dsn_ver, offset,
  898. PCI_EXT_CAP_DSN_SIZEOF);
  899. pci_set_quad(dev->config + offset + pci_dsn_cap, ser_num);
  900. }
  901. void pcie_ats_init(PCIDevice *dev, uint16_t offset, bool aligned)
  902. {
  903. pcie_add_capability(dev, PCI_EXT_CAP_ID_ATS, 0x1,
  904. offset, PCI_EXT_CAP_ATS_SIZEOF);
  905. dev->exp.ats_cap = offset;
  906. /* Invalidate Queue Depth 0 */
  907. if (aligned) {
  908. pci_set_word(dev->config + offset + PCI_ATS_CAP,
  909. PCI_ATS_CAP_PAGE_ALIGNED);
  910. }
  911. /* STU 0, Disabled by default */
  912. pci_set_word(dev->config + offset + PCI_ATS_CTRL, 0);
  913. pci_set_word(dev->wmask + dev->exp.ats_cap + PCI_ATS_CTRL, 0x800f);
  914. }
  915. /* ACS (Access Control Services) */
  916. void pcie_acs_init(PCIDevice *dev, uint16_t offset)
  917. {
  918. bool is_downstream = pci_is_express_downstream_port(dev);
  919. uint16_t cap_bits = 0;
  920. /* For endpoints, only multifunction devs may have an ACS capability: */
  921. assert(is_downstream ||
  922. (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) ||
  923. PCI_FUNC(dev->devfn));
  924. pcie_add_capability(dev, PCI_EXT_CAP_ID_ACS, PCI_ACS_VER, offset,
  925. PCI_ACS_SIZEOF);
  926. dev->exp.acs_cap = offset;
  927. if (is_downstream) {
  928. /*
  929. * Downstream ports must implement SV, TB, RR, CR, UF, and DT (with
  930. * caveats on the latter four that we ignore for simplicity).
  931. * Endpoints may also implement a subset of ACS capabilities,
  932. * but these are optional if the endpoint does not support
  933. * peer-to-peer between functions and thus omitted here.
  934. */
  935. cap_bits = PCI_ACS_SV | PCI_ACS_TB | PCI_ACS_RR |
  936. PCI_ACS_CR | PCI_ACS_UF | PCI_ACS_DT;
  937. }
  938. pci_set_word(dev->config + offset + PCI_ACS_CAP, cap_bits);
  939. pci_set_word(dev->wmask + offset + PCI_ACS_CTRL, cap_bits);
  940. }
  941. void pcie_acs_reset(PCIDevice *dev)
  942. {
  943. if (dev->exp.acs_cap) {
  944. pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
  945. }
  946. }