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