118cef1c6SJonathan Cameron /*
218cef1c6SJonathan Cameron * Emulated CXL Switch Downstream Port
318cef1c6SJonathan Cameron *
418cef1c6SJonathan Cameron * Copyright (c) 2022 Huawei Technologies.
518cef1c6SJonathan Cameron *
618cef1c6SJonathan Cameron * Based on xio3130_downstream.c
718cef1c6SJonathan Cameron *
818cef1c6SJonathan Cameron * SPDX-License-Identifier: GPL-2.0-or-later
918cef1c6SJonathan Cameron */
1018cef1c6SJonathan Cameron
1118cef1c6SJonathan Cameron #include "qemu/osdep.h"
1218cef1c6SJonathan Cameron #include "qemu/log.h"
1318cef1c6SJonathan Cameron #include "hw/pci/msi.h"
1418cef1c6SJonathan Cameron #include "hw/pci/pcie.h"
1518cef1c6SJonathan Cameron #include "hw/pci/pcie_port.h"
16*845f94deSJonathan Cameron #include "hw/qdev-properties.h"
17*845f94deSJonathan Cameron #include "hw/qdev-properties-system.h"
183314efd2SJonathan Cameron #include "hw/cxl/cxl.h"
1918cef1c6SJonathan Cameron #include "qapi/error.h"
2018cef1c6SJonathan Cameron
219518d8bcSJonathan Cameron typedef struct CXLDownstreamPort {
2218cef1c6SJonathan Cameron /*< private >*/
2318cef1c6SJonathan Cameron PCIESlot parent_obj;
2418cef1c6SJonathan Cameron
2518cef1c6SJonathan Cameron /*< public >*/
2618cef1c6SJonathan Cameron CXLComponentState cxl_cstate;
2718cef1c6SJonathan Cameron } CXLDownstreamPort;
2818cef1c6SJonathan Cameron
2918cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_MSI_OFFSET 0x70
3018cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_MSI_NR_VECTOR 1
3118cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_EXP_OFFSET 0x90
3218cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_AER_OFFSET 0x100
3318cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_DVSEC_OFFSET \
3418cef1c6SJonathan Cameron (CXL_DOWNSTREAM_PORT_AER_OFFSET + PCI_ERR_SIZEOF)
3518cef1c6SJonathan Cameron
latch_registers(CXLDownstreamPort * dsp)3618cef1c6SJonathan Cameron static void latch_registers(CXLDownstreamPort *dsp)
3718cef1c6SJonathan Cameron {
3818cef1c6SJonathan Cameron uint32_t *reg_state = dsp->cxl_cstate.crb.cache_mem_registers;
3918cef1c6SJonathan Cameron uint32_t *write_msk = dsp->cxl_cstate.crb.cache_mem_regs_write_mask;
4018cef1c6SJonathan Cameron
4118cef1c6SJonathan Cameron cxl_component_register_init_common(reg_state, write_msk,
4218cef1c6SJonathan Cameron CXL2_DOWNSTREAM_PORT);
4318cef1c6SJonathan Cameron }
4418cef1c6SJonathan Cameron
45f1c0cff8SMichael Tokarev /* TODO: Look at sharing this code across all CXL port types */
cxl_dsp_dvsec_write_config(PCIDevice * dev,uint32_t addr,uint32_t val,int len)4618cef1c6SJonathan Cameron static void cxl_dsp_dvsec_write_config(PCIDevice *dev, uint32_t addr,
4718cef1c6SJonathan Cameron uint32_t val, int len)
4818cef1c6SJonathan Cameron {
4918cef1c6SJonathan Cameron CXLDownstreamPort *dsp = CXL_DSP(dev);
5018cef1c6SJonathan Cameron CXLComponentState *cxl_cstate = &dsp->cxl_cstate;
5118cef1c6SJonathan Cameron
5218cef1c6SJonathan Cameron if (range_contains(&cxl_cstate->dvsecs[EXTENSIONS_PORT_DVSEC], addr)) {
5318cef1c6SJonathan Cameron uint8_t *reg = &dev->config[addr];
5418cef1c6SJonathan Cameron addr -= cxl_cstate->dvsecs[EXTENSIONS_PORT_DVSEC].lob;
5518cef1c6SJonathan Cameron if (addr == PORT_CONTROL_OFFSET) {
5618cef1c6SJonathan Cameron if (pci_get_word(reg) & PORT_CONTROL_UNMASK_SBR) {
5718cef1c6SJonathan Cameron /* unmask SBR */
5818cef1c6SJonathan Cameron qemu_log_mask(LOG_UNIMP, "SBR mask control is not supported\n");
5918cef1c6SJonathan Cameron }
6018cef1c6SJonathan Cameron if (pci_get_word(reg) & PORT_CONTROL_ALT_MEMID_EN) {
6118cef1c6SJonathan Cameron /* Alt Memory & ID Space Enable */
6218cef1c6SJonathan Cameron qemu_log_mask(LOG_UNIMP,
6318cef1c6SJonathan Cameron "Alt Memory & ID space is not supported\n");
6418cef1c6SJonathan Cameron
6518cef1c6SJonathan Cameron }
6618cef1c6SJonathan Cameron }
6718cef1c6SJonathan Cameron }
6818cef1c6SJonathan Cameron }
6918cef1c6SJonathan Cameron
cxl_dsp_config_write(PCIDevice * d,uint32_t address,uint32_t val,int len)7018cef1c6SJonathan Cameron static void cxl_dsp_config_write(PCIDevice *d, uint32_t address,
7118cef1c6SJonathan Cameron uint32_t val, int len)
7218cef1c6SJonathan Cameron {
7318cef1c6SJonathan Cameron uint16_t slt_ctl, slt_sta;
7418cef1c6SJonathan Cameron
7518cef1c6SJonathan Cameron pcie_cap_slot_get(d, &slt_ctl, &slt_sta);
7618cef1c6SJonathan Cameron pci_bridge_write_config(d, address, val, len);
7718cef1c6SJonathan Cameron pcie_cap_flr_write_config(d, address, val, len);
7818cef1c6SJonathan Cameron pcie_cap_slot_write_config(d, slt_ctl, slt_sta, address, val, len);
7918cef1c6SJonathan Cameron pcie_aer_write_config(d, address, val, len);
8018cef1c6SJonathan Cameron
8118cef1c6SJonathan Cameron cxl_dsp_dvsec_write_config(d, address, val, len);
8218cef1c6SJonathan Cameron }
8318cef1c6SJonathan Cameron
cxl_dsp_reset(DeviceState * qdev)8418cef1c6SJonathan Cameron static void cxl_dsp_reset(DeviceState *qdev)
8518cef1c6SJonathan Cameron {
8618cef1c6SJonathan Cameron PCIDevice *d = PCI_DEVICE(qdev);
8718cef1c6SJonathan Cameron CXLDownstreamPort *dsp = CXL_DSP(qdev);
8818cef1c6SJonathan Cameron
8918cef1c6SJonathan Cameron pcie_cap_deverr_reset(d);
9018cef1c6SJonathan Cameron pcie_cap_slot_reset(d);
9118cef1c6SJonathan Cameron pcie_cap_arifwd_reset(d);
9218cef1c6SJonathan Cameron pci_bridge_reset(qdev);
9318cef1c6SJonathan Cameron
9418cef1c6SJonathan Cameron latch_registers(dsp);
9518cef1c6SJonathan Cameron }
9618cef1c6SJonathan Cameron
build_dvsecs(CXLComponentState * cxl)9718cef1c6SJonathan Cameron static void build_dvsecs(CXLComponentState *cxl)
9818cef1c6SJonathan Cameron {
9918cef1c6SJonathan Cameron uint8_t *dvsec;
10018cef1c6SJonathan Cameron
101b34ae3c9SJonathan Cameron dvsec = (uint8_t *)&(CXLDVSECPortExt){ 0 };
10218cef1c6SJonathan Cameron cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
10318cef1c6SJonathan Cameron EXTENSIONS_PORT_DVSEC_LENGTH,
10418cef1c6SJonathan Cameron EXTENSIONS_PORT_DVSEC,
10518cef1c6SJonathan Cameron EXTENSIONS_PORT_DVSEC_REVID, dvsec);
10618cef1c6SJonathan Cameron
10718cef1c6SJonathan Cameron dvsec = (uint8_t *)&(CXLDVSECPortFlexBus){
10818cef1c6SJonathan Cameron .cap = 0x27, /* Cache, IO, Mem, non-MLD */
10918cef1c6SJonathan Cameron .ctrl = 0x02, /* IO always enabled */
11018cef1c6SJonathan Cameron .status = 0x26, /* same */
11118cef1c6SJonathan Cameron .rcvd_mod_ts_data_phase1 = 0xef, /* WTF? */
11218cef1c6SJonathan Cameron };
11318cef1c6SJonathan Cameron cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
1148700ee15SJonathan Cameron PCIE_CXL3_FLEXBUS_PORT_DVSEC_LENGTH,
11518cef1c6SJonathan Cameron PCIE_FLEXBUS_PORT_DVSEC,
1168700ee15SJonathan Cameron PCIE_CXL3_FLEXBUS_PORT_DVSEC_REVID, dvsec);
11718cef1c6SJonathan Cameron
11818cef1c6SJonathan Cameron dvsec = (uint8_t *)&(CXLDVSECPortGPF){
11918cef1c6SJonathan Cameron .rsvd = 0,
12018cef1c6SJonathan Cameron .phase1_ctrl = 1, /* 1μs timeout */
12118cef1c6SJonathan Cameron .phase2_ctrl = 1, /* 1μs timeout */
12218cef1c6SJonathan Cameron };
12318cef1c6SJonathan Cameron cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
12418cef1c6SJonathan Cameron GPF_PORT_DVSEC_LENGTH, GPF_PORT_DVSEC,
12518cef1c6SJonathan Cameron GPF_PORT_DVSEC_REVID, dvsec);
12618cef1c6SJonathan Cameron
12718cef1c6SJonathan Cameron dvsec = (uint8_t *)&(CXLDVSECRegisterLocator){
12818cef1c6SJonathan Cameron .rsvd = 0,
12918cef1c6SJonathan Cameron .reg0_base_lo = RBI_COMPONENT_REG | CXL_COMPONENT_REG_BAR_IDX,
13018cef1c6SJonathan Cameron .reg0_base_hi = 0,
13118cef1c6SJonathan Cameron };
13218cef1c6SJonathan Cameron cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
13318cef1c6SJonathan Cameron REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
13418cef1c6SJonathan Cameron REG_LOC_DVSEC_REVID, dvsec);
13518cef1c6SJonathan Cameron }
13618cef1c6SJonathan Cameron
cxl_dsp_realize(PCIDevice * d,Error ** errp)13718cef1c6SJonathan Cameron static void cxl_dsp_realize(PCIDevice *d, Error **errp)
13818cef1c6SJonathan Cameron {
13918cef1c6SJonathan Cameron PCIEPort *p = PCIE_PORT(d);
14018cef1c6SJonathan Cameron PCIESlot *s = PCIE_SLOT(d);
14118cef1c6SJonathan Cameron CXLDownstreamPort *dsp = CXL_DSP(d);
14218cef1c6SJonathan Cameron CXLComponentState *cxl_cstate = &dsp->cxl_cstate;
14318cef1c6SJonathan Cameron ComponentRegisters *cregs = &cxl_cstate->crb;
14418cef1c6SJonathan Cameron MemoryRegion *component_bar = &cregs->component_registers;
14518cef1c6SJonathan Cameron int rc;
14618cef1c6SJonathan Cameron
14718cef1c6SJonathan Cameron pci_bridge_initfn(d, TYPE_PCIE_BUS);
14818cef1c6SJonathan Cameron pcie_port_init_reg(d);
14918cef1c6SJonathan Cameron
15018cef1c6SJonathan Cameron rc = msi_init(d, CXL_DOWNSTREAM_PORT_MSI_OFFSET,
15118cef1c6SJonathan Cameron CXL_DOWNSTREAM_PORT_MSI_NR_VECTOR,
15218cef1c6SJonathan Cameron true, true, errp);
15318cef1c6SJonathan Cameron if (rc) {
15418cef1c6SJonathan Cameron assert(rc == -ENOTSUP);
15518cef1c6SJonathan Cameron goto err_bridge;
15618cef1c6SJonathan Cameron }
15718cef1c6SJonathan Cameron
15818cef1c6SJonathan Cameron rc = pcie_cap_init(d, CXL_DOWNSTREAM_PORT_EXP_OFFSET,
15918cef1c6SJonathan Cameron PCI_EXP_TYPE_DOWNSTREAM, p->port,
16018cef1c6SJonathan Cameron errp);
16118cef1c6SJonathan Cameron if (rc < 0) {
16218cef1c6SJonathan Cameron goto err_msi;
16318cef1c6SJonathan Cameron }
16418cef1c6SJonathan Cameron
16518cef1c6SJonathan Cameron pcie_cap_flr_init(d);
16618cef1c6SJonathan Cameron pcie_cap_deverr_init(d);
16718cef1c6SJonathan Cameron pcie_cap_slot_init(d, s);
16818cef1c6SJonathan Cameron pcie_cap_arifwd_init(d);
16918cef1c6SJonathan Cameron
17018cef1c6SJonathan Cameron pcie_chassis_create(s->chassis);
17118cef1c6SJonathan Cameron rc = pcie_chassis_add_slot(s);
17218cef1c6SJonathan Cameron if (rc < 0) {
17318cef1c6SJonathan Cameron error_setg(errp, "Can't add chassis slot, error %d", rc);
17418cef1c6SJonathan Cameron goto err_pcie_cap;
17518cef1c6SJonathan Cameron }
17618cef1c6SJonathan Cameron
17718cef1c6SJonathan Cameron rc = pcie_aer_init(d, PCI_ERR_VER, CXL_DOWNSTREAM_PORT_AER_OFFSET,
17818cef1c6SJonathan Cameron PCI_ERR_SIZEOF, errp);
17918cef1c6SJonathan Cameron if (rc < 0) {
18018cef1c6SJonathan Cameron goto err_chassis;
18118cef1c6SJonathan Cameron }
18218cef1c6SJonathan Cameron
18318cef1c6SJonathan Cameron cxl_cstate->dvsec_offset = CXL_DOWNSTREAM_PORT_DVSEC_OFFSET;
18418cef1c6SJonathan Cameron cxl_cstate->pdev = d;
18518cef1c6SJonathan Cameron build_dvsecs(cxl_cstate);
18618cef1c6SJonathan Cameron cxl_component_register_block_init(OBJECT(d), cxl_cstate, TYPE_CXL_DSP);
18718cef1c6SJonathan Cameron pci_register_bar(d, CXL_COMPONENT_REG_BAR_IDX,
18818cef1c6SJonathan Cameron PCI_BASE_ADDRESS_SPACE_MEMORY |
18918cef1c6SJonathan Cameron PCI_BASE_ADDRESS_MEM_TYPE_64,
19018cef1c6SJonathan Cameron component_bar);
19118cef1c6SJonathan Cameron
19218cef1c6SJonathan Cameron return;
19318cef1c6SJonathan Cameron
19418cef1c6SJonathan Cameron err_chassis:
19518cef1c6SJonathan Cameron pcie_chassis_del_slot(s);
19618cef1c6SJonathan Cameron err_pcie_cap:
19718cef1c6SJonathan Cameron pcie_cap_exit(d);
19818cef1c6SJonathan Cameron err_msi:
19918cef1c6SJonathan Cameron msi_uninit(d);
20018cef1c6SJonathan Cameron err_bridge:
20118cef1c6SJonathan Cameron pci_bridge_exitfn(d);
20218cef1c6SJonathan Cameron }
20318cef1c6SJonathan Cameron
cxl_dsp_exitfn(PCIDevice * d)20418cef1c6SJonathan Cameron static void cxl_dsp_exitfn(PCIDevice *d)
20518cef1c6SJonathan Cameron {
20618cef1c6SJonathan Cameron PCIESlot *s = PCIE_SLOT(d);
20718cef1c6SJonathan Cameron
20818cef1c6SJonathan Cameron pcie_aer_exit(d);
20918cef1c6SJonathan Cameron pcie_chassis_del_slot(s);
21018cef1c6SJonathan Cameron pcie_cap_exit(d);
21118cef1c6SJonathan Cameron msi_uninit(d);
21218cef1c6SJonathan Cameron pci_bridge_exitfn(d);
21318cef1c6SJonathan Cameron }
21418cef1c6SJonathan Cameron
215*845f94deSJonathan Cameron static Property cxl_dsp_props[] = {
216*845f94deSJonathan Cameron DEFINE_PROP_PCIE_LINK_SPEED("x-speed", PCIESlot,
217*845f94deSJonathan Cameron speed, PCIE_LINK_SPEED_64),
218*845f94deSJonathan Cameron DEFINE_PROP_PCIE_LINK_WIDTH("x-width", PCIESlot,
219*845f94deSJonathan Cameron width, PCIE_LINK_WIDTH_16),
220*845f94deSJonathan Cameron DEFINE_PROP_END_OF_LIST()
221*845f94deSJonathan Cameron };
222314f5033SJonathan Cameron
cxl_dsp_class_init(ObjectClass * oc,void * data)22318cef1c6SJonathan Cameron static void cxl_dsp_class_init(ObjectClass *oc, void *data)
22418cef1c6SJonathan Cameron {
22518cef1c6SJonathan Cameron DeviceClass *dc = DEVICE_CLASS(oc);
22618cef1c6SJonathan Cameron PCIDeviceClass *k = PCI_DEVICE_CLASS(oc);
22718cef1c6SJonathan Cameron
228*845f94deSJonathan Cameron device_class_set_props(dc, cxl_dsp_props);
22918cef1c6SJonathan Cameron k->config_write = cxl_dsp_config_write;
23018cef1c6SJonathan Cameron k->realize = cxl_dsp_realize;
23118cef1c6SJonathan Cameron k->exit = cxl_dsp_exitfn;
23218cef1c6SJonathan Cameron k->vendor_id = 0x19e5; /* Huawei */
23318cef1c6SJonathan Cameron k->device_id = 0xa129; /* Emulated CXL Switch Downstream Port */
23418cef1c6SJonathan Cameron k->revision = 0;
23518cef1c6SJonathan Cameron set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
23618cef1c6SJonathan Cameron dc->desc = "CXL Switch Downstream Port";
237e3d08143SPeter Maydell device_class_set_legacy_reset(dc, cxl_dsp_reset);
23818cef1c6SJonathan Cameron }
23918cef1c6SJonathan Cameron
24018cef1c6SJonathan Cameron static const TypeInfo cxl_dsp_info = {
24118cef1c6SJonathan Cameron .name = TYPE_CXL_DSP,
24218cef1c6SJonathan Cameron .instance_size = sizeof(CXLDownstreamPort),
24318cef1c6SJonathan Cameron .parent = TYPE_PCIE_SLOT,
24418cef1c6SJonathan Cameron .class_init = cxl_dsp_class_init,
24518cef1c6SJonathan Cameron .interfaces = (InterfaceInfo[]) {
24618cef1c6SJonathan Cameron { INTERFACE_PCIE_DEVICE },
24718cef1c6SJonathan Cameron { INTERFACE_CXL_DEVICE },
24818cef1c6SJonathan Cameron { }
24918cef1c6SJonathan Cameron },
25018cef1c6SJonathan Cameron };
25118cef1c6SJonathan Cameron
cxl_dsp_register_type(void)25218cef1c6SJonathan Cameron static void cxl_dsp_register_type(void)
25318cef1c6SJonathan Cameron {
25418cef1c6SJonathan Cameron type_register_static(&cxl_dsp_info);
25518cef1c6SJonathan Cameron }
25618cef1c6SJonathan Cameron
25718cef1c6SJonathan Cameron type_init(cxl_dsp_register_type);
258