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