pcie.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. //#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. static void hotplug_event_clear(PCIDevice *dev)
  154. {
  155. hotplug_event_update_event_status(dev);
  156. if (!msix_enabled(dev) && !msi_enabled(dev) && !dev->exp.hpev_notified) {
  157. qemu_set_irq(dev->irq[dev->exp.hpev_intx], 0);
  158. }
  159. }
  160. /*
  161. * A PCI Express Hot-Plug Event has occurred, so update slot status register
  162. * and notify OS of the event if necessary.
  163. *
  164. * 6.7.3 PCI Express Hot-Plug Events
  165. * 6.7.3.4 Software Notification of Hot-Plug Events
  166. */
  167. static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
  168. {
  169. /* Minor optimization: if nothing changed - no event is needed. */
  170. if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
  171. PCI_EXP_SLTSTA, event)) {
  172. return;
  173. }
  174. hotplug_event_notify(dev);
  175. }
  176. static int pcie_cap_slot_hotplug(DeviceState *qdev,
  177. PCIDevice *pci_dev, PCIHotplugState state)
  178. {
  179. PCIDevice *d = PCI_DEVICE(qdev);
  180. uint8_t *exp_cap = d->config + d->exp.exp_cap;
  181. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  182. /* Don't send event when device is enabled during qemu machine creation:
  183. * it is present on boot, no hotplug event is necessary. We do send an
  184. * event when the device is disabled later. */
  185. if (state == PCI_COLDPLUG_ENABLED) {
  186. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  187. PCI_EXP_SLTSTA_PDS);
  188. return 0;
  189. }
  190. PCIE_DEV_PRINTF(pci_dev, "hotplug state: %d\n", state);
  191. if (sltsta & PCI_EXP_SLTSTA_EIS) {
  192. /* the slot is electromechanically locked.
  193. * This error is propagated up to qdev and then to HMP/QMP.
  194. */
  195. return -EBUSY;
  196. }
  197. /* TODO: multifunction hot-plug.
  198. * Right now, only a device of function = 0 is allowed to be
  199. * hot plugged/unplugged.
  200. */
  201. assert(PCI_FUNC(pci_dev->devfn) == 0);
  202. if (state == PCI_HOTPLUG_ENABLED) {
  203. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
  204. PCI_EXP_SLTSTA_PDS);
  205. pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
  206. } else {
  207. qdev_free(&pci_dev->qdev);
  208. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  209. PCI_EXP_SLTSTA_PDS);
  210. pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
  211. }
  212. return 0;
  213. }
  214. /* pci express slot for pci express root/downstream port
  215. PCI express capability slot registers */
  216. void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
  217. {
  218. uint32_t pos = dev->exp.exp_cap;
  219. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
  220. PCI_EXP_FLAGS_SLOT);
  221. pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
  222. ~PCI_EXP_SLTCAP_PSN);
  223. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
  224. (slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
  225. PCI_EXP_SLTCAP_EIP |
  226. PCI_EXP_SLTCAP_HPS |
  227. PCI_EXP_SLTCAP_HPC |
  228. PCI_EXP_SLTCAP_PIP |
  229. PCI_EXP_SLTCAP_AIP |
  230. PCI_EXP_SLTCAP_ABP);
  231. pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
  232. PCI_EXP_SLTCTL_PIC |
  233. PCI_EXP_SLTCTL_AIC);
  234. pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
  235. PCI_EXP_SLTCTL_PIC_OFF |
  236. PCI_EXP_SLTCTL_AIC_OFF);
  237. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  238. PCI_EXP_SLTCTL_PIC |
  239. PCI_EXP_SLTCTL_AIC |
  240. PCI_EXP_SLTCTL_HPIE |
  241. PCI_EXP_SLTCTL_CCIE |
  242. PCI_EXP_SLTCTL_PDCE |
  243. PCI_EXP_SLTCTL_ABPE);
  244. /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
  245. * make the bit writable here in order to detect 1b is written.
  246. * pcie_cap_slot_write_config() test-and-clear the bit, so
  247. * this bit always returns 0 to the guest.
  248. */
  249. pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
  250. PCI_EXP_SLTCTL_EIC);
  251. pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
  252. PCI_EXP_HP_EV_SUPPORTED);
  253. dev->exp.hpev_notified = false;
  254. pci_bus_hotplug(pci_bridge_get_sec_bus(DO_UPCAST(PCIBridge, dev, dev)),
  255. pcie_cap_slot_hotplug, &dev->qdev);
  256. }
  257. void pcie_cap_slot_reset(PCIDevice *dev)
  258. {
  259. uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
  260. PCIE_DEV_PRINTF(dev, "reset\n");
  261. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  262. PCI_EXP_SLTCTL_EIC |
  263. PCI_EXP_SLTCTL_PIC |
  264. PCI_EXP_SLTCTL_AIC |
  265. PCI_EXP_SLTCTL_HPIE |
  266. PCI_EXP_SLTCTL_CCIE |
  267. PCI_EXP_SLTCTL_PDCE |
  268. PCI_EXP_SLTCTL_ABPE);
  269. pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
  270. PCI_EXP_SLTCTL_PIC_OFF |
  271. PCI_EXP_SLTCTL_AIC_OFF);
  272. pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
  273. PCI_EXP_SLTSTA_EIS |/* on reset,
  274. the lock is released */
  275. PCI_EXP_SLTSTA_CC |
  276. PCI_EXP_SLTSTA_PDC |
  277. PCI_EXP_SLTSTA_ABP);
  278. hotplug_event_update_event_status(dev);
  279. }
  280. void pcie_cap_slot_write_config(PCIDevice *dev,
  281. uint32_t addr, uint32_t val, int len)
  282. {
  283. uint32_t pos = dev->exp.exp_cap;
  284. uint8_t *exp_cap = dev->config + pos;
  285. uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
  286. if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
  287. hotplug_event_clear(dev);
  288. }
  289. if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
  290. return;
  291. }
  292. if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
  293. PCI_EXP_SLTCTL_EIC)) {
  294. sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
  295. pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
  296. PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
  297. "sltsta -> 0x%02"PRIx16"\n",
  298. sltsta);
  299. }
  300. hotplug_event_notify(dev);
  301. /*
  302. * 6.7.3.2 Command Completed Events
  303. *
  304. * Software issues a command to a hot-plug capable Downstream Port by
  305. * issuing a write transaction that targets any portion of the Port’s Slot
  306. * Control register. A single write to the Slot Control register is
  307. * considered to be a single command, even if the write affects more than
  308. * one field in the Slot Control register. In response to this transaction,
  309. * the Port must carry out the requested actions and then set the
  310. * associated status field for the command completed event. */
  311. /* Real hardware might take a while to complete requested command because
  312. * physical movement would be involved like locking the electromechanical
  313. * lock. However in our case, command is completed instantaneously above,
  314. * so send a command completion event right now.
  315. */
  316. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
  317. }
  318. int pcie_cap_slot_post_load(void *opaque, int version_id)
  319. {
  320. PCIDevice *dev = opaque;
  321. hotplug_event_update_event_status(dev);
  322. return 0;
  323. }
  324. void pcie_cap_slot_push_attention_button(PCIDevice *dev)
  325. {
  326. pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
  327. }
  328. /* root control/capabilities/status. PME isn't emulated for now */
  329. void pcie_cap_root_init(PCIDevice *dev)
  330. {
  331. pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
  332. PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
  333. PCI_EXP_RTCTL_SEFEE);
  334. }
  335. void pcie_cap_root_reset(PCIDevice *dev)
  336. {
  337. pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
  338. }
  339. /* function level reset(FLR) */
  340. void pcie_cap_flr_init(PCIDevice *dev)
  341. {
  342. pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
  343. PCI_EXP_DEVCAP_FLR);
  344. /* Although reading BCR_FLR returns always 0,
  345. * the bit is made writable here in order to detect the 1b is written
  346. * pcie_cap_flr_write_config() test-and-clear the bit, so
  347. * this bit always returns 0 to the guest.
  348. */
  349. pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
  350. PCI_EXP_DEVCTL_BCR_FLR);
  351. }
  352. void pcie_cap_flr_write_config(PCIDevice *dev,
  353. uint32_t addr, uint32_t val, int len)
  354. {
  355. uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
  356. if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
  357. /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
  358. so the handler can detect FLR by looking at this bit. */
  359. pci_device_reset(dev);
  360. pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
  361. }
  362. }
  363. /* Alternative Routing-ID Interpretation (ARI) */
  364. /* ari forwarding support for down stream port */
  365. void pcie_cap_ari_init(PCIDevice *dev)
  366. {
  367. uint32_t pos = dev->exp.exp_cap;
  368. pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
  369. PCI_EXP_DEVCAP2_ARI);
  370. pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
  371. PCI_EXP_DEVCTL2_ARI);
  372. }
  373. void pcie_cap_ari_reset(PCIDevice *dev)
  374. {
  375. uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
  376. pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
  377. }
  378. bool pcie_cap_is_ari_enabled(const PCIDevice *dev)
  379. {
  380. if (!pci_is_express(dev)) {
  381. return false;
  382. }
  383. if (!dev->exp.exp_cap) {
  384. return false;
  385. }
  386. return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
  387. PCI_EXP_DEVCTL2_ARI;
  388. }
  389. /**************************************************************************
  390. * pci express extended capability allocation functions
  391. * uint16_t ext_cap_id (16 bit)
  392. * uint8_t cap_ver (4 bit)
  393. * uint16_t cap_offset (12 bit)
  394. * uint16_t ext_cap_size
  395. */
  396. static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
  397. uint16_t *prev_p)
  398. {
  399. uint16_t prev = 0;
  400. uint16_t next;
  401. uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
  402. if (!header) {
  403. /* no extended capability */
  404. next = 0;
  405. goto out;
  406. }
  407. for (next = PCI_CONFIG_SPACE_SIZE; next;
  408. prev = next, next = PCI_EXT_CAP_NEXT(header)) {
  409. assert(next >= PCI_CONFIG_SPACE_SIZE);
  410. assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
  411. header = pci_get_long(dev->config + next);
  412. if (PCI_EXT_CAP_ID(header) == cap_id) {
  413. break;
  414. }
  415. }
  416. out:
  417. if (prev_p) {
  418. *prev_p = prev;
  419. }
  420. return next;
  421. }
  422. uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
  423. {
  424. return pcie_find_capability_list(dev, cap_id, NULL);
  425. }
  426. static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
  427. {
  428. uint32_t header = pci_get_long(dev->config + pos);
  429. assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
  430. header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
  431. ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
  432. pci_set_long(dev->config + pos, header);
  433. }
  434. /*
  435. * caller must supply valid (offset, size) * such that the range shouldn't
  436. * overlap with other capability or other registers.
  437. * This function doesn't check it.
  438. */
  439. void pcie_add_capability(PCIDevice *dev,
  440. uint16_t cap_id, uint8_t cap_ver,
  441. uint16_t offset, uint16_t size)
  442. {
  443. uint32_t header;
  444. uint16_t next;
  445. assert(offset >= PCI_CONFIG_SPACE_SIZE);
  446. assert(offset < offset + size);
  447. assert(offset + size < PCIE_CONFIG_SPACE_SIZE);
  448. assert(size >= 8);
  449. assert(pci_is_express(dev));
  450. if (offset == PCI_CONFIG_SPACE_SIZE) {
  451. header = pci_get_long(dev->config + offset);
  452. next = PCI_EXT_CAP_NEXT(header);
  453. } else {
  454. uint16_t prev;
  455. /* 0 is reserved cap id. use internally to find the last capability
  456. in the linked list */
  457. next = pcie_find_capability_list(dev, 0, &prev);
  458. assert(prev >= PCI_CONFIG_SPACE_SIZE);
  459. assert(next == 0);
  460. pcie_ext_cap_set_next(dev, prev, offset);
  461. }
  462. pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next));
  463. /* Make capability read-only by default */
  464. memset(dev->wmask + offset, 0, size);
  465. memset(dev->w1cmask + offset, 0, size);
  466. /* Check capability by default */
  467. memset(dev->cmask + offset, 0xFF, size);
  468. }
  469. /**************************************************************************
  470. * pci express extended capability helper functions
  471. */
  472. /* ARI */
  473. void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
  474. {
  475. pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
  476. offset, PCI_ARI_SIZEOF);
  477. pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn));
  478. }