cxl_upstream.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Emulated CXL Switch Upstream Port
  3. *
  4. * Copyright (c) 2022 Huawei Technologies.
  5. *
  6. * Based on xio3130_upstream.c
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/log.h"
  12. #include "hw/qdev-properties.h"
  13. #include "hw/pci/msi.h"
  14. #include "hw/pci/pcie.h"
  15. #include "hw/pci/pcie_port.h"
  16. #define CXL_UPSTREAM_PORT_MSI_NR_VECTOR 2
  17. #define CXL_UPSTREAM_PORT_MSI_OFFSET 0x70
  18. #define CXL_UPSTREAM_PORT_PCIE_CAP_OFFSET 0x90
  19. #define CXL_UPSTREAM_PORT_AER_OFFSET 0x100
  20. #define CXL_UPSTREAM_PORT_DVSEC_OFFSET \
  21. (CXL_UPSTREAM_PORT_AER_OFFSET + PCI_ERR_SIZEOF)
  22. typedef struct CXLUpstreamPort {
  23. /*< private >*/
  24. PCIEPort parent_obj;
  25. /*< public >*/
  26. CXLComponentState cxl_cstate;
  27. DOECap doe_cdat;
  28. } CXLUpstreamPort;
  29. CXLComponentState *cxl_usp_to_cstate(CXLUpstreamPort *usp)
  30. {
  31. return &usp->cxl_cstate;
  32. }
  33. static void cxl_usp_dvsec_write_config(PCIDevice *dev, uint32_t addr,
  34. uint32_t val, int len)
  35. {
  36. CXLUpstreamPort *usp = CXL_USP(dev);
  37. if (range_contains(&usp->cxl_cstate.dvsecs[EXTENSIONS_PORT_DVSEC], addr)) {
  38. uint8_t *reg = &dev->config[addr];
  39. addr -= usp->cxl_cstate.dvsecs[EXTENSIONS_PORT_DVSEC].lob;
  40. if (addr == PORT_CONTROL_OFFSET) {
  41. if (pci_get_word(reg) & PORT_CONTROL_UNMASK_SBR) {
  42. /* unmask SBR */
  43. qemu_log_mask(LOG_UNIMP, "SBR mask control is not supported\n");
  44. }
  45. if (pci_get_word(reg) & PORT_CONTROL_ALT_MEMID_EN) {
  46. /* Alt Memory & ID Space Enable */
  47. qemu_log_mask(LOG_UNIMP,
  48. "Alt Memory & ID space is not supported\n");
  49. }
  50. }
  51. }
  52. }
  53. static void cxl_usp_write_config(PCIDevice *d, uint32_t address,
  54. uint32_t val, int len)
  55. {
  56. CXLUpstreamPort *usp = CXL_USP(d);
  57. pcie_doe_write_config(&usp->doe_cdat, address, val, len);
  58. pci_bridge_write_config(d, address, val, len);
  59. pcie_cap_flr_write_config(d, address, val, len);
  60. pcie_aer_write_config(d, address, val, len);
  61. cxl_usp_dvsec_write_config(d, address, val, len);
  62. }
  63. static uint32_t cxl_usp_read_config(PCIDevice *d, uint32_t address, int len)
  64. {
  65. CXLUpstreamPort *usp = CXL_USP(d);
  66. uint32_t val;
  67. if (pcie_doe_read_config(&usp->doe_cdat, address, len, &val)) {
  68. return val;
  69. }
  70. return pci_default_read_config(d, address, len);
  71. }
  72. static void latch_registers(CXLUpstreamPort *usp)
  73. {
  74. uint32_t *reg_state = usp->cxl_cstate.crb.cache_mem_registers;
  75. uint32_t *write_msk = usp->cxl_cstate.crb.cache_mem_regs_write_mask;
  76. cxl_component_register_init_common(reg_state, write_msk,
  77. CXL2_UPSTREAM_PORT);
  78. ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, TARGET_COUNT, 8);
  79. }
  80. static void cxl_usp_reset(DeviceState *qdev)
  81. {
  82. PCIDevice *d = PCI_DEVICE(qdev);
  83. CXLUpstreamPort *usp = CXL_USP(qdev);
  84. pci_bridge_reset(qdev);
  85. pcie_cap_deverr_reset(d);
  86. latch_registers(usp);
  87. }
  88. static void build_dvsecs(CXLComponentState *cxl)
  89. {
  90. uint8_t *dvsec;
  91. dvsec = (uint8_t *)&(CXLDVSECPortExtensions){
  92. .status = 0x1, /* Port Power Management Init Complete */
  93. };
  94. cxl_component_create_dvsec(cxl, CXL2_UPSTREAM_PORT,
  95. EXTENSIONS_PORT_DVSEC_LENGTH,
  96. EXTENSIONS_PORT_DVSEC,
  97. EXTENSIONS_PORT_DVSEC_REVID, dvsec);
  98. dvsec = (uint8_t *)&(CXLDVSECPortFlexBus){
  99. .cap = 0x27, /* Cache, IO, Mem, non-MLD */
  100. .ctrl = 0x27, /* Cache, IO, Mem */
  101. .status = 0x26, /* same */
  102. .rcvd_mod_ts_data_phase1 = 0xef, /* WTF? */
  103. };
  104. cxl_component_create_dvsec(cxl, CXL2_UPSTREAM_PORT,
  105. PCIE_FLEXBUS_PORT_DVSEC_LENGTH_2_0,
  106. PCIE_FLEXBUS_PORT_DVSEC,
  107. PCIE_FLEXBUS_PORT_DVSEC_REVID_2_0, dvsec);
  108. dvsec = (uint8_t *)&(CXLDVSECRegisterLocator){
  109. .rsvd = 0,
  110. .reg0_base_lo = RBI_COMPONENT_REG | CXL_COMPONENT_REG_BAR_IDX,
  111. .reg0_base_hi = 0,
  112. };
  113. cxl_component_create_dvsec(cxl, CXL2_UPSTREAM_PORT,
  114. REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
  115. REG_LOC_DVSEC_REVID, dvsec);
  116. }
  117. static bool cxl_doe_cdat_rsp(DOECap *doe_cap)
  118. {
  119. CDATObject *cdat = &CXL_USP(doe_cap->pdev)->cxl_cstate.cdat;
  120. uint16_t ent;
  121. void *base;
  122. uint32_t len;
  123. CDATReq *req = pcie_doe_get_write_mbox_ptr(doe_cap);
  124. CDATRsp rsp;
  125. cxl_doe_cdat_update(&CXL_USP(doe_cap->pdev)->cxl_cstate, &error_fatal);
  126. assert(cdat->entry_len);
  127. /* Discard if request length mismatched */
  128. if (pcie_doe_get_obj_len(req) <
  129. DIV_ROUND_UP(sizeof(CDATReq), sizeof(uint32_t))) {
  130. return false;
  131. }
  132. ent = req->entry_handle;
  133. base = cdat->entry[ent].base;
  134. len = cdat->entry[ent].length;
  135. rsp = (CDATRsp) {
  136. .header = {
  137. .vendor_id = CXL_VENDOR_ID,
  138. .data_obj_type = CXL_DOE_TABLE_ACCESS,
  139. .reserved = 0x0,
  140. .length = DIV_ROUND_UP((sizeof(rsp) + len), sizeof(uint32_t)),
  141. },
  142. .rsp_code = CXL_DOE_TAB_RSP,
  143. .table_type = CXL_DOE_TAB_TYPE_CDAT,
  144. .entry_handle = (ent < cdat->entry_len - 1) ?
  145. ent + 1 : CXL_DOE_TAB_ENT_MAX,
  146. };
  147. memcpy(doe_cap->read_mbox, &rsp, sizeof(rsp));
  148. memcpy(doe_cap->read_mbox + DIV_ROUND_UP(sizeof(rsp), sizeof(uint32_t)),
  149. base, len);
  150. doe_cap->read_mbox_len += rsp.header.length;
  151. return true;
  152. }
  153. static DOEProtocol doe_cdat_prot[] = {
  154. { CXL_VENDOR_ID, CXL_DOE_TABLE_ACCESS, cxl_doe_cdat_rsp },
  155. { }
  156. };
  157. enum {
  158. CXL_USP_CDAT_SSLBIS_LAT,
  159. CXL_USP_CDAT_SSLBIS_BW,
  160. CXL_USP_CDAT_NUM_ENTRIES
  161. };
  162. static int build_cdat_table(CDATSubHeader ***cdat_table, void *priv)
  163. {
  164. g_autofree CDATSslbis *sslbis_latency = NULL;
  165. g_autofree CDATSslbis *sslbis_bandwidth = NULL;
  166. CXLUpstreamPort *us = CXL_USP(priv);
  167. PCIBus *bus = &PCI_BRIDGE(us)->sec_bus;
  168. int devfn, sslbis_size, i;
  169. int count = 0;
  170. uint16_t port_ids[256];
  171. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  172. PCIDevice *d = bus->devices[devfn];
  173. PCIEPort *port;
  174. if (!d || !pci_is_express(d) || !d->exp.exp_cap) {
  175. continue;
  176. }
  177. /*
  178. * Whilst the PCI express spec doesn't allow anything other than
  179. * downstream ports on this bus, let us be a little paranoid
  180. */
  181. if (!object_dynamic_cast(OBJECT(d), TYPE_PCIE_PORT)) {
  182. continue;
  183. }
  184. port = PCIE_PORT(d);
  185. port_ids[count] = port->port;
  186. count++;
  187. }
  188. /* May not yet have any ports - try again later */
  189. if (count == 0) {
  190. return 0;
  191. }
  192. sslbis_size = sizeof(CDATSslbis) + sizeof(*sslbis_latency->sslbe) * count;
  193. sslbis_latency = g_malloc(sslbis_size);
  194. if (!sslbis_latency) {
  195. return -ENOMEM;
  196. }
  197. *sslbis_latency = (CDATSslbis) {
  198. .sslbis_header = {
  199. .header = {
  200. .type = CDAT_TYPE_SSLBIS,
  201. .length = sslbis_size,
  202. },
  203. .data_type = HMATLB_DATA_TYPE_ACCESS_LATENCY,
  204. .entry_base_unit = 10000,
  205. },
  206. };
  207. for (i = 0; i < count; i++) {
  208. sslbis_latency->sslbe[i] = (CDATSslbe) {
  209. .port_x_id = CDAT_PORT_ID_USP,
  210. .port_y_id = port_ids[i],
  211. .latency_bandwidth = 15, /* 150ns */
  212. };
  213. }
  214. sslbis_bandwidth = g_malloc(sslbis_size);
  215. if (!sslbis_bandwidth) {
  216. return 0;
  217. }
  218. *sslbis_bandwidth = (CDATSslbis) {
  219. .sslbis_header = {
  220. .header = {
  221. .type = CDAT_TYPE_SSLBIS,
  222. .length = sslbis_size,
  223. },
  224. .data_type = HMATLB_DATA_TYPE_ACCESS_BANDWIDTH,
  225. .entry_base_unit = 1000,
  226. },
  227. };
  228. for (i = 0; i < count; i++) {
  229. sslbis_bandwidth->sslbe[i] = (CDATSslbe) {
  230. .port_x_id = CDAT_PORT_ID_USP,
  231. .port_y_id = port_ids[i],
  232. .latency_bandwidth = 16, /* 16 GB/s */
  233. };
  234. }
  235. *cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
  236. if (!*cdat_table) {
  237. return -ENOMEM;
  238. }
  239. /* Header always at start of structure */
  240. (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT] = g_steal_pointer(&sslbis_latency);
  241. (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW] = g_steal_pointer(&sslbis_bandwidth);
  242. return CXL_USP_CDAT_NUM_ENTRIES;
  243. }
  244. static void free_default_cdat_table(CDATSubHeader **cdat_table, int num,
  245. void *priv)
  246. {
  247. int i;
  248. for (i = 0; i < num; i++) {
  249. g_free(cdat_table[i]);
  250. }
  251. g_free(cdat_table);
  252. }
  253. static void cxl_usp_realize(PCIDevice *d, Error **errp)
  254. {
  255. PCIEPort *p = PCIE_PORT(d);
  256. CXLUpstreamPort *usp = CXL_USP(d);
  257. CXLComponentState *cxl_cstate = &usp->cxl_cstate;
  258. ComponentRegisters *cregs = &cxl_cstate->crb;
  259. MemoryRegion *component_bar = &cregs->component_registers;
  260. int rc;
  261. pci_bridge_initfn(d, TYPE_PCIE_BUS);
  262. pcie_port_init_reg(d);
  263. rc = msi_init(d, CXL_UPSTREAM_PORT_MSI_OFFSET,
  264. CXL_UPSTREAM_PORT_MSI_NR_VECTOR, true, true, errp);
  265. if (rc) {
  266. assert(rc == -ENOTSUP);
  267. goto err_bridge;
  268. }
  269. rc = pcie_cap_init(d, CXL_UPSTREAM_PORT_PCIE_CAP_OFFSET,
  270. PCI_EXP_TYPE_UPSTREAM, p->port, errp);
  271. if (rc < 0) {
  272. goto err_msi;
  273. }
  274. pcie_cap_flr_init(d);
  275. pcie_cap_deverr_init(d);
  276. rc = pcie_aer_init(d, PCI_ERR_VER, CXL_UPSTREAM_PORT_AER_OFFSET,
  277. PCI_ERR_SIZEOF, errp);
  278. if (rc) {
  279. goto err_cap;
  280. }
  281. cxl_cstate->dvsec_offset = CXL_UPSTREAM_PORT_DVSEC_OFFSET;
  282. cxl_cstate->pdev = d;
  283. build_dvsecs(cxl_cstate);
  284. cxl_component_register_block_init(OBJECT(d), cxl_cstate, TYPE_CXL_USP);
  285. pci_register_bar(d, CXL_COMPONENT_REG_BAR_IDX,
  286. PCI_BASE_ADDRESS_SPACE_MEMORY |
  287. PCI_BASE_ADDRESS_MEM_TYPE_64,
  288. component_bar);
  289. pcie_doe_init(d, &usp->doe_cdat, cxl_cstate->dvsec_offset, doe_cdat_prot,
  290. true, 1);
  291. cxl_cstate->cdat.build_cdat_table = build_cdat_table;
  292. cxl_cstate->cdat.free_cdat_table = free_default_cdat_table;
  293. cxl_cstate->cdat.private = d;
  294. cxl_doe_cdat_init(cxl_cstate, errp);
  295. return;
  296. err_cap:
  297. pcie_cap_exit(d);
  298. err_msi:
  299. msi_uninit(d);
  300. err_bridge:
  301. pci_bridge_exitfn(d);
  302. }
  303. static void cxl_usp_exitfn(PCIDevice *d)
  304. {
  305. pcie_aer_exit(d);
  306. pcie_cap_exit(d);
  307. msi_uninit(d);
  308. pci_bridge_exitfn(d);
  309. }
  310. static Property cxl_upstream_props[] = {
  311. DEFINE_PROP_STRING("cdat", CXLUpstreamPort, cxl_cstate.cdat.filename),
  312. DEFINE_PROP_END_OF_LIST()
  313. };
  314. static void cxl_upstream_class_init(ObjectClass *oc, void *data)
  315. {
  316. DeviceClass *dc = DEVICE_CLASS(oc);
  317. PCIDeviceClass *k = PCI_DEVICE_CLASS(oc);
  318. k->config_write = cxl_usp_write_config;
  319. k->config_read = cxl_usp_read_config;
  320. k->realize = cxl_usp_realize;
  321. k->exit = cxl_usp_exitfn;
  322. k->vendor_id = 0x19e5; /* Huawei */
  323. k->device_id = 0xa128; /* Emulated CXL Switch Upstream Port */
  324. k->revision = 0;
  325. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  326. dc->desc = "CXL Switch Upstream Port";
  327. dc->reset = cxl_usp_reset;
  328. device_class_set_props(dc, cxl_upstream_props);
  329. }
  330. static const TypeInfo cxl_usp_info = {
  331. .name = TYPE_CXL_USP,
  332. .parent = TYPE_PCIE_PORT,
  333. .instance_size = sizeof(CXLUpstreamPort),
  334. .class_init = cxl_upstream_class_init,
  335. .interfaces = (InterfaceInfo[]) {
  336. { INTERFACE_PCIE_DEVICE },
  337. { INTERFACE_CXL_DEVICE },
  338. { }
  339. },
  340. };
  341. static void cxl_usp_register_type(void)
  342. {
  343. type_register_static(&cxl_usp_info);
  344. }
  345. type_init(cxl_usp_register_type);