pcie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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-common.h"
  21. #include "hw/pci/pci_bridge.h"
  22. #include "hw/pci/pcie.h"
  23. #include "hw/pci/msix.h"
  24. #include "hw/pci/msi.h"
  25. #include "hw/pci/pci_bus.h"
  26. #include "hw/pci/pcie_regs.h"
  27. #include "qemu/range.h"
  28. #include "qapi/qmp/qerror.h"
  29. //#define DEBUG_PCIE
  30. #ifdef DEBUG_PCIE
  31. # define PCIE_DPRINTF(fmt, ...) \
  32. fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
  33. #else
  34. # define PCIE_DPRINTF(fmt, ...) do {} while (0)
  35. #endif
  36. #define PCIE_DEV_PRINTF(dev, fmt, ...) \
  37. PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
  38. /***************************************************************************
  39. * pci express capability helper functions
  40. */
  41. int pcie_cap_init(PCIDevice *dev, uint8_t offset, uint8_t type, uint8_t port)
  42. {
  43. int pos;
  44. uint8_t *exp_cap;
  45. assert(pci_is_express(dev));
  46. pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
  47. PCI_EXP_VER2_SIZEOF);
  48. if (pos < 0) {
  49. return pos;
  50. }
  51. dev->exp.exp_cap = pos;
  52. exp_cap = dev->config + pos;
  53. /* capability register
  54. interrupt message number defaults to 0 */
  55. pci_set_word(exp_cap + PCI_EXP_FLAGS,
  56. ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
  57. PCI_EXP_FLAGS_VER2);
  58. /* device capability register
  59. * table 7-12:
  60. * roll based error reporting bit must be set by all
  61. * Functions conforming to the ECN, PCI Express Base
  62. * Specification, Revision 1.1., or subsequent PCI Express Base
  63. * Specification revisions.
  64. */
  65. pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
  66. pci_set_long(exp_cap + PCI_EXP_LNKCAP,
  67. (port << PCI_EXP_LNKCAP_PN_SHIFT) |
  68. PCI_EXP_LNKCAP_ASPMS_0S |
  69. PCI_EXP_LNK_MLW_1 |
  70. PCI_EXP_LNK_LS_25);
  71. pci_set_word(exp_cap + PCI_EXP_LNKSTA,
  72. PCI_EXP_LNK_MLW_1 | PCI_EXP_LNK_LS_25);
  73. pci_set_long(exp_cap + PCI_EXP_DEVCAP2,
  74. PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP);
  75. pci_set_word(dev->wmask + pos, PCI_EXP_DEVCTL2_EETLPPB);
  76. return pos;
  77. }
  78. int pcie_endpoint_cap_init(PCIDevice *dev, uint8_t offset)
  79. {
  80. uint8_t type = PCI_EXP_TYPE_ENDPOINT;
  81. /*
  82. * Windows guests will report Code 10, device cannot start, if
  83. * a regular Endpoint type is exposed on a root complex. These
  84. * should instead be Root Complex Integrated Endpoints.
  85. */
  86. if (pci_bus_is_express(dev->bus) && pci_bus_is_root(dev->bus)) {
  87. type = PCI_EXP_TYPE_RC_END;
  88. }
  89. return pcie_cap_init(dev, offset, type, 0);
  90. }
  91. void pcie_cap_exit(PCIDevice *dev)
  92. {
  93. pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF);
  94. }
  95. uint8_t pcie_cap_get_type(const PCIDevice *dev)
  96. {
  97. uint32_t pos = dev->exp.exp_cap;
  98. assert(pos > 0);
  99. return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
  100. PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT;
  101. }
  102. /* MSI/MSI-X */
  103. /* pci express interrupt message number */
  104. /* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
  105. void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
  106. {
  107. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  108. assert(vector < 32);
  109. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ);
  110. pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS,
  111. vector << PCI_EXP_FLAGS_IRQ_SHIFT);
  112. }
  113. uint8_t pcie_cap_flags_get_vector(PCIDevice *dev)
  114. {
  115. return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
  116. PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT;
  117. }
  118. void pcie_cap_deverr_init(PCIDevice *dev)
  119. {
  120. uint32_t pos = dev->exp.exp_cap;
  121. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP,
  122. PCI_EXP_DEVCAP_RBER);
  123. pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL,
  124. PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
  125. PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
  126. pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA,
  127. PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED |
  128. PCI_EXP_DEVSTA_URD | PCI_EXP_DEVSTA_URD);
  129. }
  130. void pcie_cap_deverr_reset(PCIDevice *dev)
  131. {
  132. uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
  133. pci_long_test_and_clear_mask(devctl,
  134. PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
  135. PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
  136. }
  137. static void hotplug_event_update_event_status(PCIDevice *dev)
  138. {
  139. uint32_t pos = dev->exp.exp_cap;
  140. uint8_t *exp_cap = dev->config + pos;
  141. uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
  142. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  143. dev->exp.hpev_notified = (sltctl & PCI_EXP_SLTCTL_HPIE) &&
  144. (sltsta & sltctl & PCI_EXP_HP_EV_SUPPORTED);
  145. }
  146. static void hotplug_event_notify(PCIDevice *dev)
  147. {
  148. bool prev = dev->exp.hpev_notified;
  149. hotplug_event_update_event_status(dev);
  150. if (prev == dev->exp.hpev_notified) {
  151. return;
  152. }
  153. /* Note: the logic above does not take into account whether interrupts
  154. * are masked. The result is that interrupt will be sent when it is
  155. * subsequently unmasked. This appears to be legal: Section 6.7.3.4:
  156. * The Port may optionally send an MSI when there are hot-plug events that
  157. * occur while interrupt generation is disabled, and interrupt generation is
  158. * subsequently enabled. */
  159. if (msix_enabled(dev)) {
  160. msix_notify(dev, pcie_cap_flags_get_vector(dev));
  161. } else if (msi_enabled(dev)) {
  162. msi_notify(dev, pcie_cap_flags_get_vector(dev));
  163. } else {
  164. pci_set_irq(dev, dev->exp.hpev_notified);
  165. }
  166. }
  167. static void hotplug_event_clear(PCIDevice *dev)
  168. {
  169. hotplug_event_update_event_status(dev);
  170. if (!msix_enabled(dev) && !msi_enabled(dev) && !dev->exp.hpev_notified) {
  171. pci_irq_deassert(dev);
  172. }
  173. }
  174. /*
  175. * A PCI Express Hot-Plug Event has occurred, so update slot status register
  176. * and notify OS of the event if necessary.
  177. *
  178. * 6.7.3 PCI Express Hot-Plug Events
  179. * 6.7.3.4 Software Notification of Hot-Plug Events
  180. */
  181. static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
  182. {
  183. /* Minor optimization: if nothing changed - no event is needed. */
  184. if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
  185. PCI_EXP_SLTSTA, event)) {
  186. return;
  187. }
  188. hotplug_event_notify(dev);
  189. }
  190. static void pcie_cap_slot_hotplug_common(PCIDevice *hotplug_dev,
  191. DeviceState *dev,
  192. uint8_t **exp_cap, Error **errp)
  193. {
  194. *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
  195. uint16_t sltsta = pci_get_word(*exp_cap + PCI_EXP_SLTSTA);
  196. PCIE_DEV_PRINTF(PCI_DEVICE(dev), "hotplug state: %d\n", state);
  197. if (sltsta & PCI_EXP_SLTSTA_EIS) {
  198. /* the slot is electromechanically locked.
  199. * This error is propagated up to qdev and then to HMP/QMP.
  200. */
  201. error_setg_errno(errp, -EBUSY, "slot is electromechanically locked");
  202. }
  203. }
  204. void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  205. Error **errp)
  206. {
  207. uint8_t *exp_cap;
  208. PCIDevice *pci_dev = PCI_DEVICE(dev);
  209. pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
  210. /* Don't send event when device is enabled during qemu machine creation:
  211. * it is present on boot, no hotplug event is necessary. We do send an
  212. * event when the device is disabled later. */
  213. if (!dev->hotplugged) {
  214. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  215. PCI_EXP_SLTSTA_PDS);
  216. return;
  217. }
  218. /* TODO: multifunction hot-plug.
  219. * Right now, only a device of function = 0 is allowed to be
  220. * hot plugged/unplugged.
  221. */
  222. assert(PCI_FUNC(pci_dev->devfn) == 0);
  223. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  224. PCI_EXP_SLTSTA_PDS);
  225. pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
  226. }
  227. void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  228. Error **errp)
  229. {
  230. uint8_t *exp_cap;
  231. pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
  232. object_unparent(OBJECT(dev));
  233. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  234. PCI_EXP_SLTSTA_PDS);
  235. pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
  236. }
  237. /* pci express slot for pci express root/downstream port
  238. PCI express capability slot registers */
  239. void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
  240. {
  241. uint32_t pos = dev->exp.exp_cap;
  242. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
  243. PCI_EXP_FLAGS_SLOT);
  244. pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
  245. ~PCI_EXP_SLTCAP_PSN);
  246. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  247. (slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
  248. PCI_EXP_SLTCAP_EIP |
  249. PCI_EXP_SLTCAP_HPS |
  250. PCI_EXP_SLTCAP_HPC |
  251. PCI_EXP_SLTCAP_PIP |
  252. PCI_EXP_SLTCAP_AIP |
  253. PCI_EXP_SLTCAP_ABP);
  254. pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
  255. PCI_EXP_SLTCTL_PIC |
  256. PCI_EXP_SLTCTL_AIC);
  257. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
  258. PCI_EXP_SLTCTL_PIC_OFF |
  259. PCI_EXP_SLTCTL_AIC_OFF);
  260. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  261. PCI_EXP_SLTCTL_PIC |
  262. PCI_EXP_SLTCTL_AIC |
  263. PCI_EXP_SLTCTL_HPIE |
  264. PCI_EXP_SLTCTL_CCIE |
  265. PCI_EXP_SLTCTL_PDCE |
  266. PCI_EXP_SLTCTL_ABPE);
  267. /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
  268. * make the bit writable here in order to detect 1b is written.
  269. * pcie_cap_slot_write_config() test-and-clear the bit, so
  270. * this bit always returns 0 to the guest.
  271. */
  272. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  273. PCI_EXP_SLTCTL_EIC);
  274. pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
  275. PCI_EXP_HP_EV_SUPPORTED);
  276. dev->exp.hpev_notified = false;
  277. qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev))),
  278. DEVICE(dev), NULL);
  279. }
  280. void pcie_cap_slot_reset(PCIDevice *dev)
  281. {
  282. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  283. PCIE_DEV_PRINTF(dev, "reset\n");
  284. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  285. PCI_EXP_SLTCTL_EIC |
  286. PCI_EXP_SLTCTL_PIC |
  287. PCI_EXP_SLTCTL_AIC |
  288. PCI_EXP_SLTCTL_HPIE |
  289. PCI_EXP_SLTCTL_CCIE |
  290. PCI_EXP_SLTCTL_PDCE |
  291. PCI_EXP_SLTCTL_ABPE);
  292. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  293. PCI_EXP_SLTCTL_PIC_OFF |
  294. PCI_EXP_SLTCTL_AIC_OFF);
  295. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  296. PCI_EXP_SLTSTA_EIS |/* on reset,
  297. the lock is released */
  298. PCI_EXP_SLTSTA_CC |
  299. PCI_EXP_SLTSTA_PDC |
  300. PCI_EXP_SLTSTA_ABP);
  301. hotplug_event_update_event_status(dev);
  302. }
  303. void pcie_cap_slot_write_config(PCIDevice *dev,
  304. uint32_t addr, uint32_t val, int len)
  305. {
  306. uint32_t pos = dev->exp.exp_cap;
  307. uint8_t *exp_cap = dev->config + pos;
  308. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  309. if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
  310. hotplug_event_clear(dev);
  311. }
  312. if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
  313. return;
  314. }
  315. if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  316. PCI_EXP_SLTCTL_EIC)) {
  317. sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
  318. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  319. PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
  320. "sltsta -> 0x%02"PRIx16"\n",
  321. sltsta);
  322. }
  323. hotplug_event_notify(dev);
  324. /*
  325. * 6.7.3.2 Command Completed Events
  326. *
  327. * Software issues a command to a hot-plug capable Downstream Port by
  328. * issuing a write transaction that targets any portion of the Port’s Slot
  329. * Control register. A single write to the Slot Control register is
  330. * considered to be a single command, even if the write affects more than
  331. * one field in the Slot Control register. In response to this transaction,
  332. * the Port must carry out the requested actions and then set the
  333. * associated status field for the command completed event. */
  334. /* Real hardware might take a while to complete requested command because
  335. * physical movement would be involved like locking the electromechanical
  336. * lock. However in our case, command is completed instantaneously above,
  337. * so send a command completion event right now.
  338. */
  339. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
  340. }
  341. int pcie_cap_slot_post_load(void *opaque, int version_id)
  342. {
  343. PCIDevice *dev = opaque;
  344. hotplug_event_update_event_status(dev);
  345. return 0;
  346. }
  347. void pcie_cap_slot_push_attention_button(PCIDevice *dev)
  348. {
  349. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
  350. }
  351. /* root control/capabilities/status. PME isn't emulated for now */
  352. void pcie_cap_root_init(PCIDevice *dev)
  353. {
  354. pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
  355. PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  356. PCI_EXP_RTCTL_SEFEE);
  357. }
  358. void pcie_cap_root_reset(PCIDevice *dev)
  359. {
  360. pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
  361. }
  362. /* function level reset(FLR) */
  363. void pcie_cap_flr_init(PCIDevice *dev)
  364. {
  365. pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
  366. PCI_EXP_DEVCAP_FLR);
  367. /* Although reading BCR_FLR returns always 0,
  368. * the bit is made writable here in order to detect the 1b is written
  369. * pcie_cap_flr_write_config() test-and-clear the bit, so
  370. * this bit always returns 0 to the guest.
  371. */
  372. pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
  373. PCI_EXP_DEVCTL_BCR_FLR);
  374. }
  375. void pcie_cap_flr_write_config(PCIDevice *dev,
  376. uint32_t addr, uint32_t val, int len)
  377. {
  378. uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
  379. if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
  380. /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
  381. so the handler can detect FLR by looking at this bit. */
  382. pci_device_reset(dev);
  383. pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
  384. }
  385. }
  386. /* Alternative Routing-ID Interpretation (ARI) */
  387. /* ari forwarding support for down stream port */
  388. void pcie_cap_ari_init(PCIDevice *dev)
  389. {
  390. uint32_t pos = dev->exp.exp_cap;
  391. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
  392. PCI_EXP_DEVCAP2_ARI);
  393. pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
  394. PCI_EXP_DEVCTL2_ARI);
  395. }
  396. void pcie_cap_ari_reset(PCIDevice *dev)
  397. {
  398. uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
  399. pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
  400. }
  401. bool pcie_cap_is_ari_enabled(const PCIDevice *dev)
  402. {
  403. if (!pci_is_express(dev)) {
  404. return false;
  405. }
  406. if (!dev->exp.exp_cap) {
  407. return false;
  408. }
  409. return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
  410. PCI_EXP_DEVCTL2_ARI;
  411. }
  412. /**************************************************************************
  413. * pci express extended capability allocation functions
  414. * uint16_t ext_cap_id (16 bit)
  415. * uint8_t cap_ver (4 bit)
  416. * uint16_t cap_offset (12 bit)
  417. * uint16_t ext_cap_size
  418. */
  419. static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
  420. uint16_t *prev_p)
  421. {
  422. uint16_t prev = 0;
  423. uint16_t next;
  424. uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
  425. if (!header) {
  426. /* no extended capability */
  427. next = 0;
  428. goto out;
  429. }
  430. for (next = PCI_CONFIG_SPACE_SIZE; next;
  431. prev = next, next = PCI_EXT_CAP_NEXT(header)) {
  432. assert(next >= PCI_CONFIG_SPACE_SIZE);
  433. assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
  434. header = pci_get_long(dev->config + next);
  435. if (PCI_EXT_CAP_ID(header) == cap_id) {
  436. break;
  437. }
  438. }
  439. out:
  440. if (prev_p) {
  441. *prev_p = prev;
  442. }
  443. return next;
  444. }
  445. uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
  446. {
  447. return pcie_find_capability_list(dev, cap_id, NULL);
  448. }
  449. static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
  450. {
  451. uint32_t header = pci_get_long(dev->config + pos);
  452. assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
  453. header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
  454. ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
  455. pci_set_long(dev->config + pos, header);
  456. }
  457. /*
  458. * caller must supply valid (offset, size) * such that the range shouldn't
  459. * overlap with other capability or other registers.
  460. * This function doesn't check it.
  461. */
  462. void pcie_add_capability(PCIDevice *dev,
  463. uint16_t cap_id, uint8_t cap_ver,
  464. uint16_t offset, uint16_t size)
  465. {
  466. uint32_t header;
  467. uint16_t next;
  468. assert(offset >= PCI_CONFIG_SPACE_SIZE);
  469. assert(offset < offset + size);
  470. assert(offset + size < PCIE_CONFIG_SPACE_SIZE);
  471. assert(size >= 8);
  472. assert(pci_is_express(dev));
  473. if (offset == PCI_CONFIG_SPACE_SIZE) {
  474. header = pci_get_long(dev->config + offset);
  475. next = PCI_EXT_CAP_NEXT(header);
  476. } else {
  477. uint16_t prev;
  478. /* 0 is reserved cap id. use internally to find the last capability
  479. in the linked list */
  480. next = pcie_find_capability_list(dev, 0, &prev);
  481. assert(prev >= PCI_CONFIG_SPACE_SIZE);
  482. assert(next == 0);
  483. pcie_ext_cap_set_next(dev, prev, offset);
  484. }
  485. pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next));
  486. /* Make capability read-only by default */
  487. memset(dev->wmask + offset, 0, size);
  488. memset(dev->w1cmask + offset, 0, size);
  489. /* Check capability by default */
  490. memset(dev->cmask + offset, 0xFF, size);
  491. }
  492. /**************************************************************************
  493. * pci express extended capability helper functions
  494. */
  495. /* ARI */
  496. void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
  497. {
  498. pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
  499. offset, PCI_ARI_SIZEOF);
  500. pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn));
  501. }