cxl_root_port.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * CXL 2.0 Root Port Implementation
  3. *
  4. * Copyright(C) 2020 Intel Corporation.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/log.h"
  21. #include "qemu/range.h"
  22. #include "hw/pci/pci_bridge.h"
  23. #include "hw/pci/pcie_port.h"
  24. #include "hw/pci/msi.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/qdev-properties-system.h"
  27. #include "hw/sysbus.h"
  28. #include "qapi/error.h"
  29. #include "hw/cxl/cxl.h"
  30. #define CXL_ROOT_PORT_DID 0x7075
  31. #define CXL_RP_MSI_OFFSET 0x60
  32. #define CXL_RP_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_MASKBIT
  33. #define CXL_RP_MSI_NR_VECTOR 2
  34. /* Copied from the gen root port which we derive */
  35. #define GEN_PCIE_ROOT_PORT_AER_OFFSET 0x100
  36. #define GEN_PCIE_ROOT_PORT_ACS_OFFSET \
  37. (GEN_PCIE_ROOT_PORT_AER_OFFSET + PCI_ERR_SIZEOF)
  38. #define CXL_ROOT_PORT_DVSEC_OFFSET \
  39. (GEN_PCIE_ROOT_PORT_ACS_OFFSET + PCI_ACS_SIZEOF)
  40. typedef struct CXLRootPort {
  41. /*< private >*/
  42. PCIESlot parent_obj;
  43. CXLComponentState cxl_cstate;
  44. PCIResReserve res_reserve;
  45. } CXLRootPort;
  46. #define TYPE_CXL_ROOT_PORT "cxl-rp"
  47. DECLARE_INSTANCE_CHECKER(CXLRootPort, CXL_ROOT_PORT, TYPE_CXL_ROOT_PORT)
  48. /*
  49. * If two MSI vector are allocated, Advanced Error Interrupt Message Number
  50. * is 1. otherwise 0.
  51. * 17.12.5.10 RPERRSTS, 32:27 bit Advanced Error Interrupt Message Number.
  52. */
  53. static uint8_t cxl_rp_aer_vector(const PCIDevice *d)
  54. {
  55. switch (msi_nr_vectors_allocated(d)) {
  56. case 1:
  57. return 0;
  58. case 2:
  59. return 1;
  60. case 4:
  61. case 8:
  62. case 16:
  63. case 32:
  64. default:
  65. break;
  66. }
  67. abort();
  68. return 0;
  69. }
  70. static int cxl_rp_interrupts_init(PCIDevice *d, Error **errp)
  71. {
  72. int rc;
  73. rc = msi_init(d, CXL_RP_MSI_OFFSET, CXL_RP_MSI_NR_VECTOR,
  74. CXL_RP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
  75. CXL_RP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT,
  76. errp);
  77. if (rc < 0) {
  78. assert(rc == -ENOTSUP);
  79. }
  80. return rc;
  81. }
  82. static void cxl_rp_interrupts_uninit(PCIDevice *d)
  83. {
  84. msi_uninit(d);
  85. }
  86. static void latch_registers(CXLRootPort *crp)
  87. {
  88. uint32_t *reg_state = crp->cxl_cstate.crb.cache_mem_registers;
  89. uint32_t *write_msk = crp->cxl_cstate.crb.cache_mem_regs_write_mask;
  90. cxl_component_register_init_common(reg_state, write_msk, CXL2_ROOT_PORT);
  91. }
  92. static void build_dvsecs(CXLComponentState *cxl)
  93. {
  94. uint8_t *dvsec;
  95. dvsec = (uint8_t *)&(CXLDVSECPortExt){ 0 };
  96. cxl_component_create_dvsec(cxl, CXL2_ROOT_PORT,
  97. EXTENSIONS_PORT_DVSEC_LENGTH,
  98. EXTENSIONS_PORT_DVSEC,
  99. EXTENSIONS_PORT_DVSEC_REVID, dvsec);
  100. dvsec = (uint8_t *)&(CXLDVSECPortGPF){
  101. .rsvd = 0,
  102. .phase1_ctrl = 1, /* 1μs timeout */
  103. .phase2_ctrl = 1, /* 1μs timeout */
  104. };
  105. cxl_component_create_dvsec(cxl, CXL2_ROOT_PORT,
  106. GPF_PORT_DVSEC_LENGTH, GPF_PORT_DVSEC,
  107. GPF_PORT_DVSEC_REVID, dvsec);
  108. dvsec = (uint8_t *)&(CXLDVSECPortFlexBus){
  109. .cap = 0x26, /* IO, Mem, non-MLD */
  110. .ctrl = 0x2,
  111. .status = 0x26, /* same */
  112. .rcvd_mod_ts_data_phase1 = 0xef,
  113. };
  114. cxl_component_create_dvsec(cxl, CXL2_ROOT_PORT,
  115. PCIE_CXL3_FLEXBUS_PORT_DVSEC_LENGTH,
  116. PCIE_FLEXBUS_PORT_DVSEC,
  117. PCIE_CXL3_FLEXBUS_PORT_DVSEC_REVID, dvsec);
  118. dvsec = (uint8_t *)&(CXLDVSECRegisterLocator){
  119. .rsvd = 0,
  120. .reg0_base_lo = RBI_COMPONENT_REG | CXL_COMPONENT_REG_BAR_IDX,
  121. .reg0_base_hi = 0,
  122. };
  123. cxl_component_create_dvsec(cxl, CXL2_ROOT_PORT,
  124. REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
  125. REG_LOC_DVSEC_REVID, dvsec);
  126. }
  127. static void cxl_rp_realize(DeviceState *dev, Error **errp)
  128. {
  129. PCIDevice *pci_dev = PCI_DEVICE(dev);
  130. PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(dev);
  131. CXLRootPort *crp = CXL_ROOT_PORT(dev);
  132. CXLComponentState *cxl_cstate = &crp->cxl_cstate;
  133. ComponentRegisters *cregs = &cxl_cstate->crb;
  134. MemoryRegion *component_bar = &cregs->component_registers;
  135. Error *local_err = NULL;
  136. rpc->parent_realize(dev, &local_err);
  137. if (local_err) {
  138. error_propagate(errp, local_err);
  139. return;
  140. }
  141. int rc =
  142. pci_bridge_qemu_reserve_cap_init(pci_dev, 0, crp->res_reserve, errp);
  143. if (rc < 0) {
  144. rpc->parent_class.exit(pci_dev);
  145. return;
  146. }
  147. if (!crp->res_reserve.io || crp->res_reserve.io == -1) {
  148. pci_word_test_and_clear_mask(pci_dev->wmask + PCI_COMMAND,
  149. PCI_COMMAND_IO);
  150. pci_dev->wmask[PCI_IO_BASE] = 0;
  151. pci_dev->wmask[PCI_IO_LIMIT] = 0;
  152. }
  153. cxl_cstate->dvsec_offset = CXL_ROOT_PORT_DVSEC_OFFSET;
  154. cxl_cstate->pdev = pci_dev;
  155. build_dvsecs(cxl_cstate);
  156. cxl_component_register_block_init(OBJECT(pci_dev), cxl_cstate,
  157. TYPE_CXL_ROOT_PORT);
  158. pci_register_bar(pci_dev, CXL_COMPONENT_REG_BAR_IDX,
  159. PCI_BASE_ADDRESS_SPACE_MEMORY |
  160. PCI_BASE_ADDRESS_MEM_TYPE_64,
  161. component_bar);
  162. }
  163. static void cxl_rp_reset_hold(Object *obj, ResetType type)
  164. {
  165. PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(obj);
  166. CXLRootPort *crp = CXL_ROOT_PORT(obj);
  167. if (rpc->parent_phases.hold) {
  168. rpc->parent_phases.hold(obj, type);
  169. }
  170. latch_registers(crp);
  171. }
  172. static const Property gen_rp_props[] = {
  173. DEFINE_PROP_UINT32("bus-reserve", CXLRootPort, res_reserve.bus, -1),
  174. DEFINE_PROP_SIZE("io-reserve", CXLRootPort, res_reserve.io, -1),
  175. DEFINE_PROP_SIZE("mem-reserve", CXLRootPort, res_reserve.mem_non_pref, -1),
  176. DEFINE_PROP_SIZE("pref32-reserve", CXLRootPort, res_reserve.mem_pref_32,
  177. -1),
  178. DEFINE_PROP_SIZE("pref64-reserve", CXLRootPort, res_reserve.mem_pref_64,
  179. -1),
  180. DEFINE_PROP_PCIE_LINK_SPEED("x-speed", PCIESlot,
  181. speed, PCIE_LINK_SPEED_64),
  182. DEFINE_PROP_PCIE_LINK_WIDTH("x-width", PCIESlot,
  183. width, PCIE_LINK_WIDTH_32),
  184. };
  185. static void cxl_rp_dvsec_write_config(PCIDevice *dev, uint32_t addr,
  186. uint32_t val, int len)
  187. {
  188. CXLRootPort *crp = CXL_ROOT_PORT(dev);
  189. if (range_contains(&crp->cxl_cstate.dvsecs[EXTENSIONS_PORT_DVSEC], addr)) {
  190. uint8_t *reg = &dev->config[addr];
  191. addr -= crp->cxl_cstate.dvsecs[EXTENSIONS_PORT_DVSEC].lob;
  192. if (addr == PORT_CONTROL_OFFSET) {
  193. if (pci_get_word(reg) & PORT_CONTROL_UNMASK_SBR) {
  194. /* unmask SBR */
  195. qemu_log_mask(LOG_UNIMP, "SBR mask control is not supported\n");
  196. }
  197. if (pci_get_word(reg) & PORT_CONTROL_ALT_MEMID_EN) {
  198. /* Alt Memory & ID Space Enable */
  199. qemu_log_mask(LOG_UNIMP,
  200. "Alt Memory & ID space is not supported\n");
  201. }
  202. }
  203. }
  204. }
  205. static void cxl_rp_aer_vector_update(PCIDevice *d)
  206. {
  207. PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d);
  208. if (rpc->aer_vector) {
  209. pcie_aer_root_set_vector(d, rpc->aer_vector(d));
  210. }
  211. }
  212. static void cxl_rp_write_config(PCIDevice *d, uint32_t address, uint32_t val,
  213. int len)
  214. {
  215. uint16_t slt_ctl, slt_sta;
  216. uint32_t root_cmd =
  217. pci_get_long(d->config + d->exp.aer_cap + PCI_ERR_ROOT_COMMAND);
  218. pcie_cap_slot_get(d, &slt_ctl, &slt_sta);
  219. pci_bridge_write_config(d, address, val, len);
  220. cxl_rp_aer_vector_update(d);
  221. pcie_cap_flr_write_config(d, address, val, len);
  222. pcie_cap_slot_write_config(d, slt_ctl, slt_sta, address, val, len);
  223. pcie_aer_write_config(d, address, val, len);
  224. pcie_aer_root_write_config(d, address, val, len, root_cmd);
  225. cxl_rp_dvsec_write_config(d, address, val, len);
  226. }
  227. static void cxl_root_port_class_init(ObjectClass *oc, void *data)
  228. {
  229. DeviceClass *dc = DEVICE_CLASS(oc);
  230. PCIDeviceClass *k = PCI_DEVICE_CLASS(oc);
  231. ResettableClass *rc = RESETTABLE_CLASS(oc);
  232. PCIERootPortClass *rpc = PCIE_ROOT_PORT_CLASS(oc);
  233. k->vendor_id = PCI_VENDOR_ID_INTEL;
  234. k->device_id = CXL_ROOT_PORT_DID;
  235. dc->desc = "CXL Root Port";
  236. k->revision = 0;
  237. device_class_set_props(dc, gen_rp_props);
  238. k->config_write = cxl_rp_write_config;
  239. device_class_set_parent_realize(dc, cxl_rp_realize, &rpc->parent_realize);
  240. resettable_class_set_parent_phases(rc, NULL, cxl_rp_reset_hold, NULL,
  241. &rpc->parent_phases);
  242. rpc->aer_offset = GEN_PCIE_ROOT_PORT_AER_OFFSET;
  243. rpc->acs_offset = GEN_PCIE_ROOT_PORT_ACS_OFFSET;
  244. rpc->aer_vector = cxl_rp_aer_vector;
  245. rpc->interrupts_init = cxl_rp_interrupts_init;
  246. rpc->interrupts_uninit = cxl_rp_interrupts_uninit;
  247. dc->hotpluggable = false;
  248. }
  249. static const TypeInfo cxl_root_port_info = {
  250. .name = TYPE_CXL_ROOT_PORT,
  251. .parent = TYPE_PCIE_ROOT_PORT,
  252. .instance_size = sizeof(CXLRootPort),
  253. .class_init = cxl_root_port_class_init,
  254. .interfaces = (InterfaceInfo[]) {
  255. { INTERFACE_CXL_DEVICE },
  256. { }
  257. },
  258. };
  259. static void cxl_register(void)
  260. {
  261. type_register_static(&cxl_root_port_info);
  262. }
  263. type_init(cxl_register);