2 * Emulated CXL Switch Upstream Port
4 * Copyright (c) 2022 Huawei Technologies.
6 * Based on xio3130_upstream.c
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "qemu/osdep.h"
13 #include "hw/qdev-properties.h"
14 #include "hw/pci/msi.h"
15 #include "hw/pci/pcie.h"
16 #include "hw/pci/pcie_port.h"
18 #define CXL_UPSTREAM_PORT_MSI_NR_VECTOR 2
20 #define CXL_UPSTREAM_PORT_MSI_OFFSET 0x70
21 #define CXL_UPSTREAM_PORT_PCIE_CAP_OFFSET 0x90
22 #define CXL_UPSTREAM_PORT_AER_OFFSET 0x100
23 #define CXL_UPSTREAM_PORT_DVSEC_OFFSET \
24 (CXL_UPSTREAM_PORT_AER_OFFSET + PCI_ERR_SIZEOF)
26 typedef struct CXLUpstreamPort
{
31 CXLComponentState cxl_cstate
;
35 CXLComponentState
*cxl_usp_to_cstate(CXLUpstreamPort
*usp
)
37 return &usp
->cxl_cstate
;
40 static void cxl_usp_dvsec_write_config(PCIDevice
*dev
, uint32_t addr
,
41 uint32_t val
, int len
)
43 CXLUpstreamPort
*usp
= CXL_USP(dev
);
45 if (range_contains(&usp
->cxl_cstate
.dvsecs
[EXTENSIONS_PORT_DVSEC
], addr
)) {
46 uint8_t *reg
= &dev
->config
[addr
];
47 addr
-= usp
->cxl_cstate
.dvsecs
[EXTENSIONS_PORT_DVSEC
].lob
;
48 if (addr
== PORT_CONTROL_OFFSET
) {
49 if (pci_get_word(reg
) & PORT_CONTROL_UNMASK_SBR
) {
51 qemu_log_mask(LOG_UNIMP
, "SBR mask control is not supported\n");
53 if (pci_get_word(reg
) & PORT_CONTROL_ALT_MEMID_EN
) {
54 /* Alt Memory & ID Space Enable */
55 qemu_log_mask(LOG_UNIMP
,
56 "Alt Memory & ID space is not supported\n");
62 static void cxl_usp_write_config(PCIDevice
*d
, uint32_t address
,
63 uint32_t val
, int len
)
65 CXLUpstreamPort
*usp
= CXL_USP(d
);
67 pcie_doe_write_config(&usp
->doe_cdat
, address
, val
, len
);
68 pci_bridge_write_config(d
, address
, val
, len
);
69 pcie_cap_flr_write_config(d
, address
, val
, len
);
70 pcie_aer_write_config(d
, address
, val
, len
);
72 cxl_usp_dvsec_write_config(d
, address
, val
, len
);
75 static uint32_t cxl_usp_read_config(PCIDevice
*d
, uint32_t address
, int len
)
77 CXLUpstreamPort
*usp
= CXL_USP(d
);
80 if (pcie_doe_read_config(&usp
->doe_cdat
, address
, len
, &val
)) {
84 return pci_default_read_config(d
, address
, len
);
87 static void latch_registers(CXLUpstreamPort
*usp
)
89 uint32_t *reg_state
= usp
->cxl_cstate
.crb
.cache_mem_registers
;
90 uint32_t *write_msk
= usp
->cxl_cstate
.crb
.cache_mem_regs_write_mask
;
92 cxl_component_register_init_common(reg_state
, write_msk
,
94 ARRAY_FIELD_DP32(reg_state
, CXL_HDM_DECODER_CAPABILITY
, TARGET_COUNT
, 8);
97 static void cxl_usp_reset(DeviceState
*qdev
)
99 PCIDevice
*d
= PCI_DEVICE(qdev
);
100 CXLUpstreamPort
*usp
= CXL_USP(qdev
);
102 pci_bridge_reset(qdev
);
103 pcie_cap_deverr_reset(d
);
104 latch_registers(usp
);
107 static void build_dvsecs(CXLComponentState
*cxl
)
111 dvsec
= (uint8_t *)&(CXLDVSECPortExtensions
){
112 .status
= 0x1, /* Port Power Management Init Complete */
114 cxl_component_create_dvsec(cxl
, CXL2_UPSTREAM_PORT
,
115 EXTENSIONS_PORT_DVSEC_LENGTH
,
116 EXTENSIONS_PORT_DVSEC
,
117 EXTENSIONS_PORT_DVSEC_REVID
, dvsec
);
118 dvsec
= (uint8_t *)&(CXLDVSECPortFlexBus
){
119 .cap
= 0x27, /* Cache, IO, Mem, non-MLD */
120 .ctrl
= 0x27, /* Cache, IO, Mem */
121 .status
= 0x26, /* same */
122 .rcvd_mod_ts_data_phase1
= 0xef, /* WTF? */
124 cxl_component_create_dvsec(cxl
, CXL2_UPSTREAM_PORT
,
125 PCIE_FLEXBUS_PORT_DVSEC_LENGTH_2_0
,
126 PCIE_FLEXBUS_PORT_DVSEC
,
127 PCIE_FLEXBUS_PORT_DVSEC_REVID_2_0
, dvsec
);
129 dvsec
= (uint8_t *)&(CXLDVSECRegisterLocator
){
131 .reg0_base_lo
= RBI_COMPONENT_REG
| CXL_COMPONENT_REG_BAR_IDX
,
134 cxl_component_create_dvsec(cxl
, CXL2_UPSTREAM_PORT
,
135 REG_LOC_DVSEC_LENGTH
, REG_LOC_DVSEC
,
136 REG_LOC_DVSEC_REVID
, dvsec
);
139 static bool cxl_doe_cdat_rsp(DOECap
*doe_cap
)
141 CDATObject
*cdat
= &CXL_USP(doe_cap
->pdev
)->cxl_cstate
.cdat
;
145 CDATReq
*req
= pcie_doe_get_write_mbox_ptr(doe_cap
);
148 cxl_doe_cdat_update(&CXL_USP(doe_cap
->pdev
)->cxl_cstate
, &error_fatal
);
149 assert(cdat
->entry_len
);
151 /* Discard if request length mismatched */
152 if (pcie_doe_get_obj_len(req
) <
153 DIV_ROUND_UP(sizeof(CDATReq
), sizeof(uint32_t))) {
157 ent
= req
->entry_handle
;
158 base
= cdat
->entry
[ent
].base
;
159 len
= cdat
->entry
[ent
].length
;
163 .vendor_id
= CXL_VENDOR_ID
,
164 .data_obj_type
= CXL_DOE_TABLE_ACCESS
,
166 .length
= DIV_ROUND_UP((sizeof(rsp
) + len
), sizeof(uint32_t)),
168 .rsp_code
= CXL_DOE_TAB_RSP
,
169 .table_type
= CXL_DOE_TAB_TYPE_CDAT
,
170 .entry_handle
= (ent
< cdat
->entry_len
- 1) ?
171 ent
+ 1 : CXL_DOE_TAB_ENT_MAX
,
174 memcpy(doe_cap
->read_mbox
, &rsp
, sizeof(rsp
));
175 memcpy(doe_cap
->read_mbox
+ DIV_ROUND_UP(sizeof(rsp
), sizeof(uint32_t)),
178 doe_cap
->read_mbox_len
+= rsp
.header
.length
;
183 static DOEProtocol doe_cdat_prot
[] = {
184 { CXL_VENDOR_ID
, CXL_DOE_TABLE_ACCESS
, cxl_doe_cdat_rsp
},
189 CXL_USP_CDAT_SSLBIS_LAT
,
190 CXL_USP_CDAT_SSLBIS_BW
,
191 CXL_USP_CDAT_NUM_ENTRIES
194 static int build_cdat_table(CDATSubHeader
***cdat_table
, void *priv
)
196 g_autofree CDATSslbis
*sslbis_latency
= NULL
;
197 g_autofree CDATSslbis
*sslbis_bandwidth
= NULL
;
198 CXLUpstreamPort
*us
= CXL_USP(priv
);
199 PCIBus
*bus
= &PCI_BRIDGE(us
)->sec_bus
;
200 int devfn
, sslbis_size
, i
;
202 uint16_t port_ids
[256];
204 for (devfn
= 0; devfn
< ARRAY_SIZE(bus
->devices
); devfn
++) {
205 PCIDevice
*d
= bus
->devices
[devfn
];
208 if (!d
|| !pci_is_express(d
) || !d
->exp
.exp_cap
) {
213 * Whilst the PCI express spec doesn't allow anything other than
214 * downstream ports on this bus, let us be a little paranoid
216 if (!object_dynamic_cast(OBJECT(d
), TYPE_PCIE_PORT
)) {
221 port_ids
[count
] = port
->port
;
225 /* May not yet have any ports - try again later */
230 sslbis_size
= sizeof(CDATSslbis
) + sizeof(*sslbis_latency
->sslbe
) * count
;
231 sslbis_latency
= g_malloc(sslbis_size
);
232 if (!sslbis_latency
) {
235 *sslbis_latency
= (CDATSslbis
) {
238 .type
= CDAT_TYPE_SSLBIS
,
239 .length
= sslbis_size
,
241 .data_type
= HMATLB_DATA_TYPE_ACCESS_LATENCY
,
242 .entry_base_unit
= 10000,
246 for (i
= 0; i
< count
; i
++) {
247 sslbis_latency
->sslbe
[i
] = (CDATSslbe
) {
248 .port_x_id
= CDAT_PORT_ID_USP
,
249 .port_y_id
= port_ids
[i
],
250 .latency_bandwidth
= 15, /* 150ns */
254 sslbis_bandwidth
= g_malloc(sslbis_size
);
255 if (!sslbis_bandwidth
) {
258 *sslbis_bandwidth
= (CDATSslbis
) {
261 .type
= CDAT_TYPE_SSLBIS
,
262 .length
= sslbis_size
,
264 .data_type
= HMATLB_DATA_TYPE_ACCESS_BANDWIDTH
,
265 .entry_base_unit
= 1000,
269 for (i
= 0; i
< count
; i
++) {
270 sslbis_bandwidth
->sslbe
[i
] = (CDATSslbe
) {
271 .port_x_id
= CDAT_PORT_ID_USP
,
272 .port_y_id
= port_ids
[i
],
273 .latency_bandwidth
= 16, /* 16 GB/s */
277 *cdat_table
= g_malloc0(sizeof(*cdat_table
) * CXL_USP_CDAT_NUM_ENTRIES
);
282 /* Header always at start of structure */
283 (*cdat_table
)[CXL_USP_CDAT_SSLBIS_LAT
] = g_steal_pointer(&sslbis_latency
);
284 (*cdat_table
)[CXL_USP_CDAT_SSLBIS_BW
] = g_steal_pointer(&sslbis_bandwidth
);
286 return CXL_USP_CDAT_NUM_ENTRIES
;
289 static void free_default_cdat_table(CDATSubHeader
**cdat_table
, int num
,
294 for (i
= 0; i
< num
; i
++) {
295 g_free(cdat_table
[i
]);
300 static void cxl_usp_realize(PCIDevice
*d
, Error
**errp
)
302 PCIEPort
*p
= PCIE_PORT(d
);
303 CXLUpstreamPort
*usp
= CXL_USP(d
);
304 CXLComponentState
*cxl_cstate
= &usp
->cxl_cstate
;
305 ComponentRegisters
*cregs
= &cxl_cstate
->crb
;
306 MemoryRegion
*component_bar
= &cregs
->component_registers
;
309 pci_bridge_initfn(d
, TYPE_PCIE_BUS
);
310 pcie_port_init_reg(d
);
312 rc
= msi_init(d
, CXL_UPSTREAM_PORT_MSI_OFFSET
,
313 CXL_UPSTREAM_PORT_MSI_NR_VECTOR
, true, true, errp
);
315 assert(rc
== -ENOTSUP
);
319 rc
= pcie_cap_init(d
, CXL_UPSTREAM_PORT_PCIE_CAP_OFFSET
,
320 PCI_EXP_TYPE_UPSTREAM
, p
->port
, errp
);
325 pcie_cap_flr_init(d
);
326 pcie_cap_deverr_init(d
);
327 rc
= pcie_aer_init(d
, PCI_ERR_VER
, CXL_UPSTREAM_PORT_AER_OFFSET
,
328 PCI_ERR_SIZEOF
, errp
);
333 cxl_cstate
->dvsec_offset
= CXL_UPSTREAM_PORT_DVSEC_OFFSET
;
334 cxl_cstate
->pdev
= d
;
335 build_dvsecs(cxl_cstate
);
336 cxl_component_register_block_init(OBJECT(d
), cxl_cstate
, TYPE_CXL_USP
);
337 pci_register_bar(d
, CXL_COMPONENT_REG_BAR_IDX
,
338 PCI_BASE_ADDRESS_SPACE_MEMORY
|
339 PCI_BASE_ADDRESS_MEM_TYPE_64
,
342 pcie_doe_init(d
, &usp
->doe_cdat
, cxl_cstate
->dvsec_offset
, doe_cdat_prot
,
345 cxl_cstate
->cdat
.build_cdat_table
= build_cdat_table
;
346 cxl_cstate
->cdat
.free_cdat_table
= free_default_cdat_table
;
347 cxl_cstate
->cdat
.private = d
;
348 cxl_doe_cdat_init(cxl_cstate
, errp
);
357 pci_bridge_exitfn(d
);
360 static void cxl_usp_exitfn(PCIDevice
*d
)
365 pci_bridge_exitfn(d
);
368 static Property cxl_upstream_props
[] = {
369 DEFINE_PROP_STRING("cdat", CXLUpstreamPort
, cxl_cstate
.cdat
.filename
),
370 DEFINE_PROP_END_OF_LIST()
373 static void cxl_upstream_class_init(ObjectClass
*oc
, void *data
)
375 DeviceClass
*dc
= DEVICE_CLASS(oc
);
376 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(oc
);
378 k
->config_write
= cxl_usp_write_config
;
379 k
->config_read
= cxl_usp_read_config
;
380 k
->realize
= cxl_usp_realize
;
381 k
->exit
= cxl_usp_exitfn
;
382 k
->vendor_id
= 0x19e5; /* Huawei */
383 k
->device_id
= 0xa128; /* Emulated CXL Switch Upstream Port */
385 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
386 dc
->desc
= "CXL Switch Upstream Port";
387 dc
->reset
= cxl_usp_reset
;
388 device_class_set_props(dc
, cxl_upstream_props
);
391 static const TypeInfo cxl_usp_info
= {
392 .name
= TYPE_CXL_USP
,
393 .parent
= TYPE_PCIE_PORT
,
394 .instance_size
= sizeof(CXLUpstreamPort
),
395 .class_init
= cxl_upstream_class_init
,
396 .interfaces
= (InterfaceInfo
[]) {
397 { INTERFACE_PCIE_DEVICE
},
398 { INTERFACE_CXL_DEVICE
},
403 static void cxl_usp_register_type(void)
405 type_register_static(&cxl_usp_info
);
408 type_init(cxl_usp_register_type
);