xref: /qemu/hw/pci-bridge/cxl_downstream.c (revision 18cef1c6a5a37710a2e5876fed2445849f31e321)
1*18cef1c6SJonathan Cameron /*
2*18cef1c6SJonathan Cameron  * Emulated CXL Switch Downstream Port
3*18cef1c6SJonathan Cameron  *
4*18cef1c6SJonathan Cameron  * Copyright (c) 2022 Huawei Technologies.
5*18cef1c6SJonathan Cameron  *
6*18cef1c6SJonathan Cameron  * Based on xio3130_downstream.c
7*18cef1c6SJonathan Cameron  *
8*18cef1c6SJonathan Cameron  * SPDX-License-Identifier: GPL-2.0-or-later
9*18cef1c6SJonathan Cameron  */
10*18cef1c6SJonathan Cameron 
11*18cef1c6SJonathan Cameron #include "qemu/osdep.h"
12*18cef1c6SJonathan Cameron #include "qemu/log.h"
13*18cef1c6SJonathan Cameron #include "hw/pci/msi.h"
14*18cef1c6SJonathan Cameron #include "hw/pci/pcie.h"
15*18cef1c6SJonathan Cameron #include "hw/pci/pcie_port.h"
16*18cef1c6SJonathan Cameron #include "qapi/error.h"
17*18cef1c6SJonathan Cameron 
18*18cef1c6SJonathan Cameron typedef struct CXLDownStreamPort {
19*18cef1c6SJonathan Cameron     /*< private >*/
20*18cef1c6SJonathan Cameron     PCIESlot parent_obj;
21*18cef1c6SJonathan Cameron 
22*18cef1c6SJonathan Cameron     /*< public >*/
23*18cef1c6SJonathan Cameron     CXLComponentState cxl_cstate;
24*18cef1c6SJonathan Cameron } CXLDownstreamPort;
25*18cef1c6SJonathan Cameron 
26*18cef1c6SJonathan Cameron #define TYPE_CXL_DSP "cxl-downstream"
27*18cef1c6SJonathan Cameron DECLARE_INSTANCE_CHECKER(CXLDownstreamPort, CXL_DSP, TYPE_CXL_DSP)
28*18cef1c6SJonathan Cameron 
29*18cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_MSI_OFFSET 0x70
30*18cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_MSI_NR_VECTOR 1
31*18cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_EXP_OFFSET 0x90
32*18cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_AER_OFFSET 0x100
33*18cef1c6SJonathan Cameron #define CXL_DOWNSTREAM_PORT_DVSEC_OFFSET        \
34*18cef1c6SJonathan Cameron     (CXL_DOWNSTREAM_PORT_AER_OFFSET + PCI_ERR_SIZEOF)
35*18cef1c6SJonathan Cameron 
36*18cef1c6SJonathan Cameron static void latch_registers(CXLDownstreamPort *dsp)
37*18cef1c6SJonathan Cameron {
38*18cef1c6SJonathan Cameron     uint32_t *reg_state = dsp->cxl_cstate.crb.cache_mem_registers;
39*18cef1c6SJonathan Cameron     uint32_t *write_msk = dsp->cxl_cstate.crb.cache_mem_regs_write_mask;
40*18cef1c6SJonathan Cameron 
41*18cef1c6SJonathan Cameron     cxl_component_register_init_common(reg_state, write_msk,
42*18cef1c6SJonathan Cameron                                        CXL2_DOWNSTREAM_PORT);
43*18cef1c6SJonathan Cameron }
44*18cef1c6SJonathan Cameron 
45*18cef1c6SJonathan Cameron /* TODO: Look at sharing this code acorss all CXL port types */
46*18cef1c6SJonathan Cameron static void cxl_dsp_dvsec_write_config(PCIDevice *dev, uint32_t addr,
47*18cef1c6SJonathan Cameron                                       uint32_t val, int len)
48*18cef1c6SJonathan Cameron {
49*18cef1c6SJonathan Cameron     CXLDownstreamPort *dsp = CXL_DSP(dev);
50*18cef1c6SJonathan Cameron     CXLComponentState *cxl_cstate = &dsp->cxl_cstate;
51*18cef1c6SJonathan Cameron 
52*18cef1c6SJonathan Cameron     if (range_contains(&cxl_cstate->dvsecs[EXTENSIONS_PORT_DVSEC], addr)) {
53*18cef1c6SJonathan Cameron         uint8_t *reg = &dev->config[addr];
54*18cef1c6SJonathan Cameron         addr -= cxl_cstate->dvsecs[EXTENSIONS_PORT_DVSEC].lob;
55*18cef1c6SJonathan Cameron         if (addr == PORT_CONTROL_OFFSET) {
56*18cef1c6SJonathan Cameron             if (pci_get_word(reg) & PORT_CONTROL_UNMASK_SBR) {
57*18cef1c6SJonathan Cameron                 /* unmask SBR */
58*18cef1c6SJonathan Cameron                 qemu_log_mask(LOG_UNIMP, "SBR mask control is not supported\n");
59*18cef1c6SJonathan Cameron             }
60*18cef1c6SJonathan Cameron             if (pci_get_word(reg) & PORT_CONTROL_ALT_MEMID_EN) {
61*18cef1c6SJonathan Cameron                 /* Alt Memory & ID Space Enable */
62*18cef1c6SJonathan Cameron                 qemu_log_mask(LOG_UNIMP,
63*18cef1c6SJonathan Cameron                               "Alt Memory & ID space is not supported\n");
64*18cef1c6SJonathan Cameron 
65*18cef1c6SJonathan Cameron             }
66*18cef1c6SJonathan Cameron         }
67*18cef1c6SJonathan Cameron     }
68*18cef1c6SJonathan Cameron }
69*18cef1c6SJonathan Cameron 
70*18cef1c6SJonathan Cameron static void cxl_dsp_config_write(PCIDevice *d, uint32_t address,
71*18cef1c6SJonathan Cameron                                  uint32_t val, int len)
72*18cef1c6SJonathan Cameron {
73*18cef1c6SJonathan Cameron     uint16_t slt_ctl, slt_sta;
74*18cef1c6SJonathan Cameron 
75*18cef1c6SJonathan Cameron     pcie_cap_slot_get(d, &slt_ctl, &slt_sta);
76*18cef1c6SJonathan Cameron     pci_bridge_write_config(d, address, val, len);
77*18cef1c6SJonathan Cameron     pcie_cap_flr_write_config(d, address, val, len);
78*18cef1c6SJonathan Cameron     pcie_cap_slot_write_config(d, slt_ctl, slt_sta, address, val, len);
79*18cef1c6SJonathan Cameron     pcie_aer_write_config(d, address, val, len);
80*18cef1c6SJonathan Cameron 
81*18cef1c6SJonathan Cameron     cxl_dsp_dvsec_write_config(d, address, val, len);
82*18cef1c6SJonathan Cameron }
83*18cef1c6SJonathan Cameron 
84*18cef1c6SJonathan Cameron static void cxl_dsp_reset(DeviceState *qdev)
85*18cef1c6SJonathan Cameron {
86*18cef1c6SJonathan Cameron     PCIDevice *d = PCI_DEVICE(qdev);
87*18cef1c6SJonathan Cameron     CXLDownstreamPort *dsp = CXL_DSP(qdev);
88*18cef1c6SJonathan Cameron 
89*18cef1c6SJonathan Cameron     pcie_cap_deverr_reset(d);
90*18cef1c6SJonathan Cameron     pcie_cap_slot_reset(d);
91*18cef1c6SJonathan Cameron     pcie_cap_arifwd_reset(d);
92*18cef1c6SJonathan Cameron     pci_bridge_reset(qdev);
93*18cef1c6SJonathan Cameron 
94*18cef1c6SJonathan Cameron     latch_registers(dsp);
95*18cef1c6SJonathan Cameron }
96*18cef1c6SJonathan Cameron 
97*18cef1c6SJonathan Cameron static void build_dvsecs(CXLComponentState *cxl)
98*18cef1c6SJonathan Cameron {
99*18cef1c6SJonathan Cameron     uint8_t *dvsec;
100*18cef1c6SJonathan Cameron 
101*18cef1c6SJonathan Cameron     dvsec = (uint8_t *)&(CXLDVSECPortExtensions){ 0 };
102*18cef1c6SJonathan Cameron     cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
103*18cef1c6SJonathan Cameron                                EXTENSIONS_PORT_DVSEC_LENGTH,
104*18cef1c6SJonathan Cameron                                EXTENSIONS_PORT_DVSEC,
105*18cef1c6SJonathan Cameron                                EXTENSIONS_PORT_DVSEC_REVID, dvsec);
106*18cef1c6SJonathan Cameron 
107*18cef1c6SJonathan Cameron     dvsec = (uint8_t *)&(CXLDVSECPortFlexBus){
108*18cef1c6SJonathan Cameron         .cap                     = 0x27, /* Cache, IO, Mem, non-MLD */
109*18cef1c6SJonathan Cameron         .ctrl                    = 0x02, /* IO always enabled */
110*18cef1c6SJonathan Cameron         .status                  = 0x26, /* same */
111*18cef1c6SJonathan Cameron         .rcvd_mod_ts_data_phase1 = 0xef, /* WTF? */
112*18cef1c6SJonathan Cameron     };
113*18cef1c6SJonathan Cameron     cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
114*18cef1c6SJonathan Cameron                                PCIE_FLEXBUS_PORT_DVSEC_LENGTH_2_0,
115*18cef1c6SJonathan Cameron                                PCIE_FLEXBUS_PORT_DVSEC,
116*18cef1c6SJonathan Cameron                                PCIE_FLEXBUS_PORT_DVSEC_REVID_2_0, dvsec);
117*18cef1c6SJonathan Cameron 
118*18cef1c6SJonathan Cameron     dvsec = (uint8_t *)&(CXLDVSECPortGPF){
119*18cef1c6SJonathan Cameron         .rsvd        = 0,
120*18cef1c6SJonathan Cameron         .phase1_ctrl = 1, /* 1μs timeout */
121*18cef1c6SJonathan Cameron         .phase2_ctrl = 1, /* 1μs timeout */
122*18cef1c6SJonathan Cameron     };
123*18cef1c6SJonathan Cameron     cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
124*18cef1c6SJonathan Cameron                                GPF_PORT_DVSEC_LENGTH, GPF_PORT_DVSEC,
125*18cef1c6SJonathan Cameron                                GPF_PORT_DVSEC_REVID, dvsec);
126*18cef1c6SJonathan Cameron 
127*18cef1c6SJonathan Cameron     dvsec = (uint8_t *)&(CXLDVSECRegisterLocator){
128*18cef1c6SJonathan Cameron         .rsvd         = 0,
129*18cef1c6SJonathan Cameron         .reg0_base_lo = RBI_COMPONENT_REG | CXL_COMPONENT_REG_BAR_IDX,
130*18cef1c6SJonathan Cameron         .reg0_base_hi = 0,
131*18cef1c6SJonathan Cameron     };
132*18cef1c6SJonathan Cameron     cxl_component_create_dvsec(cxl, CXL2_DOWNSTREAM_PORT,
133*18cef1c6SJonathan Cameron                                REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
134*18cef1c6SJonathan Cameron                                REG_LOC_DVSEC_REVID, dvsec);
135*18cef1c6SJonathan Cameron }
136*18cef1c6SJonathan Cameron 
137*18cef1c6SJonathan Cameron static void cxl_dsp_realize(PCIDevice *d, Error **errp)
138*18cef1c6SJonathan Cameron {
139*18cef1c6SJonathan Cameron     PCIEPort *p = PCIE_PORT(d);
140*18cef1c6SJonathan Cameron     PCIESlot *s = PCIE_SLOT(d);
141*18cef1c6SJonathan Cameron     CXLDownstreamPort *dsp = CXL_DSP(d);
142*18cef1c6SJonathan Cameron     CXLComponentState *cxl_cstate = &dsp->cxl_cstate;
143*18cef1c6SJonathan Cameron     ComponentRegisters *cregs = &cxl_cstate->crb;
144*18cef1c6SJonathan Cameron     MemoryRegion *component_bar = &cregs->component_registers;
145*18cef1c6SJonathan Cameron     int rc;
146*18cef1c6SJonathan Cameron 
147*18cef1c6SJonathan Cameron     pci_bridge_initfn(d, TYPE_PCIE_BUS);
148*18cef1c6SJonathan Cameron     pcie_port_init_reg(d);
149*18cef1c6SJonathan Cameron 
150*18cef1c6SJonathan Cameron     rc = msi_init(d, CXL_DOWNSTREAM_PORT_MSI_OFFSET,
151*18cef1c6SJonathan Cameron                   CXL_DOWNSTREAM_PORT_MSI_NR_VECTOR,
152*18cef1c6SJonathan Cameron                   true, true, errp);
153*18cef1c6SJonathan Cameron     if (rc) {
154*18cef1c6SJonathan Cameron         assert(rc == -ENOTSUP);
155*18cef1c6SJonathan Cameron         goto err_bridge;
156*18cef1c6SJonathan Cameron     }
157*18cef1c6SJonathan Cameron 
158*18cef1c6SJonathan Cameron     rc = pcie_cap_init(d, CXL_DOWNSTREAM_PORT_EXP_OFFSET,
159*18cef1c6SJonathan Cameron                        PCI_EXP_TYPE_DOWNSTREAM, p->port,
160*18cef1c6SJonathan Cameron                        errp);
161*18cef1c6SJonathan Cameron     if (rc < 0) {
162*18cef1c6SJonathan Cameron         goto err_msi;
163*18cef1c6SJonathan Cameron     }
164*18cef1c6SJonathan Cameron 
165*18cef1c6SJonathan Cameron     pcie_cap_flr_init(d);
166*18cef1c6SJonathan Cameron     pcie_cap_deverr_init(d);
167*18cef1c6SJonathan Cameron     pcie_cap_slot_init(d, s);
168*18cef1c6SJonathan Cameron     pcie_cap_arifwd_init(d);
169*18cef1c6SJonathan Cameron 
170*18cef1c6SJonathan Cameron     pcie_chassis_create(s->chassis);
171*18cef1c6SJonathan Cameron     rc = pcie_chassis_add_slot(s);
172*18cef1c6SJonathan Cameron     if (rc < 0) {
173*18cef1c6SJonathan Cameron         error_setg(errp, "Can't add chassis slot, error %d", rc);
174*18cef1c6SJonathan Cameron         goto err_pcie_cap;
175*18cef1c6SJonathan Cameron     }
176*18cef1c6SJonathan Cameron 
177*18cef1c6SJonathan Cameron     rc = pcie_aer_init(d, PCI_ERR_VER, CXL_DOWNSTREAM_PORT_AER_OFFSET,
178*18cef1c6SJonathan Cameron                        PCI_ERR_SIZEOF, errp);
179*18cef1c6SJonathan Cameron     if (rc < 0) {
180*18cef1c6SJonathan Cameron         goto err_chassis;
181*18cef1c6SJonathan Cameron     }
182*18cef1c6SJonathan Cameron 
183*18cef1c6SJonathan Cameron     cxl_cstate->dvsec_offset = CXL_DOWNSTREAM_PORT_DVSEC_OFFSET;
184*18cef1c6SJonathan Cameron     cxl_cstate->pdev = d;
185*18cef1c6SJonathan Cameron     build_dvsecs(cxl_cstate);
186*18cef1c6SJonathan Cameron     cxl_component_register_block_init(OBJECT(d), cxl_cstate, TYPE_CXL_DSP);
187*18cef1c6SJonathan Cameron     pci_register_bar(d, CXL_COMPONENT_REG_BAR_IDX,
188*18cef1c6SJonathan Cameron                      PCI_BASE_ADDRESS_SPACE_MEMORY |
189*18cef1c6SJonathan Cameron                          PCI_BASE_ADDRESS_MEM_TYPE_64,
190*18cef1c6SJonathan Cameron                      component_bar);
191*18cef1c6SJonathan Cameron 
192*18cef1c6SJonathan Cameron     return;
193*18cef1c6SJonathan Cameron 
194*18cef1c6SJonathan Cameron  err_chassis:
195*18cef1c6SJonathan Cameron     pcie_chassis_del_slot(s);
196*18cef1c6SJonathan Cameron  err_pcie_cap:
197*18cef1c6SJonathan Cameron     pcie_cap_exit(d);
198*18cef1c6SJonathan Cameron  err_msi:
199*18cef1c6SJonathan Cameron     msi_uninit(d);
200*18cef1c6SJonathan Cameron  err_bridge:
201*18cef1c6SJonathan Cameron     pci_bridge_exitfn(d);
202*18cef1c6SJonathan Cameron }
203*18cef1c6SJonathan Cameron 
204*18cef1c6SJonathan Cameron static void cxl_dsp_exitfn(PCIDevice *d)
205*18cef1c6SJonathan Cameron {
206*18cef1c6SJonathan Cameron     PCIESlot *s = PCIE_SLOT(d);
207*18cef1c6SJonathan Cameron 
208*18cef1c6SJonathan Cameron     pcie_aer_exit(d);
209*18cef1c6SJonathan Cameron     pcie_chassis_del_slot(s);
210*18cef1c6SJonathan Cameron     pcie_cap_exit(d);
211*18cef1c6SJonathan Cameron     msi_uninit(d);
212*18cef1c6SJonathan Cameron     pci_bridge_exitfn(d);
213*18cef1c6SJonathan Cameron }
214*18cef1c6SJonathan Cameron 
215*18cef1c6SJonathan Cameron static void cxl_dsp_class_init(ObjectClass *oc, void *data)
216*18cef1c6SJonathan Cameron {
217*18cef1c6SJonathan Cameron     DeviceClass *dc = DEVICE_CLASS(oc);
218*18cef1c6SJonathan Cameron     PCIDeviceClass *k = PCI_DEVICE_CLASS(oc);
219*18cef1c6SJonathan Cameron 
220*18cef1c6SJonathan Cameron     k->is_bridge = true;
221*18cef1c6SJonathan Cameron     k->config_write = cxl_dsp_config_write;
222*18cef1c6SJonathan Cameron     k->realize = cxl_dsp_realize;
223*18cef1c6SJonathan Cameron     k->exit = cxl_dsp_exitfn;
224*18cef1c6SJonathan Cameron     k->vendor_id = 0x19e5; /* Huawei */
225*18cef1c6SJonathan Cameron     k->device_id = 0xa129; /* Emulated CXL Switch Downstream Port */
226*18cef1c6SJonathan Cameron     k->revision = 0;
227*18cef1c6SJonathan Cameron     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
228*18cef1c6SJonathan Cameron     dc->desc = "CXL Switch Downstream Port";
229*18cef1c6SJonathan Cameron     dc->reset = cxl_dsp_reset;
230*18cef1c6SJonathan Cameron }
231*18cef1c6SJonathan Cameron 
232*18cef1c6SJonathan Cameron static const TypeInfo cxl_dsp_info = {
233*18cef1c6SJonathan Cameron     .name = TYPE_CXL_DSP,
234*18cef1c6SJonathan Cameron     .instance_size = sizeof(CXLDownstreamPort),
235*18cef1c6SJonathan Cameron     .parent = TYPE_PCIE_SLOT,
236*18cef1c6SJonathan Cameron     .class_init = cxl_dsp_class_init,
237*18cef1c6SJonathan Cameron     .interfaces = (InterfaceInfo[]) {
238*18cef1c6SJonathan Cameron         { INTERFACE_PCIE_DEVICE },
239*18cef1c6SJonathan Cameron         { INTERFACE_CXL_DEVICE },
240*18cef1c6SJonathan Cameron         { }
241*18cef1c6SJonathan Cameron     },
242*18cef1c6SJonathan Cameron };
243*18cef1c6SJonathan Cameron 
244*18cef1c6SJonathan Cameron static void cxl_dsp_register_type(void)
245*18cef1c6SJonathan Cameron {
246*18cef1c6SJonathan Cameron     type_register_static(&cxl_dsp_info);
247*18cef1c6SJonathan Cameron }
248*18cef1c6SJonathan Cameron 
249*18cef1c6SJonathan Cameron type_init(cxl_dsp_register_type);
250