pcie.c 19 KB

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