pcie.c 39 KB

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