14a583303SJonathan Cameron /*
24a583303SJonathan Cameron * SPDX-License-Identifier: GPL-2.0-or-later
34a583303SJonathan Cameron *
44a583303SJonathan Cameron * Emulation of a CXL Switch Mailbox CCI PCIe function.
54a583303SJonathan Cameron *
64a583303SJonathan Cameron * Copyright (c) 2023 Huawei Technologies.
74a583303SJonathan Cameron *
84a583303SJonathan Cameron * From www.computeexpresslink.org
94a583303SJonathan Cameron * Compute Express Link (CXL) Specification revision 3.0 Version 1.0
104a583303SJonathan Cameron */
114a583303SJonathan Cameron #include "qemu/osdep.h"
124a583303SJonathan Cameron #include "hw/pci/pci.h"
134a583303SJonathan Cameron #include "hw/pci-bridge/cxl_upstream_port.h"
144a583303SJonathan Cameron #include "qapi/error.h"
154a583303SJonathan Cameron #include "qemu/log.h"
164a583303SJonathan Cameron #include "qemu/module.h"
174a583303SJonathan Cameron #include "hw/qdev-properties.h"
184a583303SJonathan Cameron #include "hw/cxl/cxl.h"
194a583303SJonathan Cameron
cswmbcci_reset(DeviceState * dev)204a583303SJonathan Cameron static void cswmbcci_reset(DeviceState *dev)
214a583303SJonathan Cameron {
224a583303SJonathan Cameron CSWMBCCIDev *cswmb = CXL_SWITCH_MAILBOX_CCI(dev);
234a583303SJonathan Cameron cxl_device_register_init_swcci(cswmb);
244a583303SJonathan Cameron }
254a583303SJonathan Cameron
cswbcci_realize(PCIDevice * pci_dev,Error ** errp)264a583303SJonathan Cameron static void cswbcci_realize(PCIDevice *pci_dev, Error **errp)
274a583303SJonathan Cameron {
284a583303SJonathan Cameron CSWMBCCIDev *cswmb = CXL_SWITCH_MAILBOX_CCI(pci_dev);
294a583303SJonathan Cameron CXLComponentState *cxl_cstate = &cswmb->cxl_cstate;
304a583303SJonathan Cameron CXLDeviceState *cxl_dstate = &cswmb->cxl_dstate;
314a583303SJonathan Cameron CXLDVSECRegisterLocator *regloc_dvsec;
324a583303SJonathan Cameron CXLUpstreamPort *usp;
334a583303SJonathan Cameron
344a583303SJonathan Cameron if (!cswmb->target) {
354a583303SJonathan Cameron error_setg(errp, "Target not set");
364a583303SJonathan Cameron return;
374a583303SJonathan Cameron }
384a583303SJonathan Cameron usp = CXL_USP(cswmb->target);
394a583303SJonathan Cameron
404a583303SJonathan Cameron pcie_endpoint_cap_init(pci_dev, 0x80);
414a583303SJonathan Cameron cxl_cstate->dvsec_offset = 0x100;
424a583303SJonathan Cameron cxl_cstate->pdev = pci_dev;
434a583303SJonathan Cameron cswmb->cci = &usp->swcci;
444a583303SJonathan Cameron cxl_device_register_block_init(OBJECT(pci_dev), cxl_dstate, cswmb->cci);
454a583303SJonathan Cameron pci_register_bar(pci_dev, 0,
464a583303SJonathan Cameron PCI_BASE_ADDRESS_SPACE_MEMORY |
474a583303SJonathan Cameron PCI_BASE_ADDRESS_MEM_TYPE_64,
484a583303SJonathan Cameron &cxl_dstate->device_registers);
494a583303SJonathan Cameron regloc_dvsec = &(CXLDVSECRegisterLocator) {
504a583303SJonathan Cameron .rsvd = 0,
514a583303SJonathan Cameron .reg0_base_lo = RBI_CXL_DEVICE_REG | 0,
524a583303SJonathan Cameron .reg0_base_hi = 0,
534a583303SJonathan Cameron };
544a583303SJonathan Cameron cxl_component_create_dvsec(cxl_cstate, CXL3_SWITCH_MAILBOX_CCI,
554a583303SJonathan Cameron REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
564a583303SJonathan Cameron REG_LOC_DVSEC_REVID, (uint8_t *)regloc_dvsec);
574a583303SJonathan Cameron
584a583303SJonathan Cameron cxl_initialize_mailbox_swcci(cswmb->cci, DEVICE(pci_dev),
594a583303SJonathan Cameron DEVICE(cswmb->target),
604a583303SJonathan Cameron CXL_MAILBOX_MAX_PAYLOAD_SIZE);
614a583303SJonathan Cameron }
624a583303SJonathan Cameron
cswmbcci_exit(PCIDevice * pci_dev)634a583303SJonathan Cameron static void cswmbcci_exit(PCIDevice *pci_dev)
644a583303SJonathan Cameron {
654a583303SJonathan Cameron /* Nothing to do here yet */
664a583303SJonathan Cameron }
674a583303SJonathan Cameron
684a583303SJonathan Cameron static Property cxl_switch_cci_props[] = {
694a583303SJonathan Cameron DEFINE_PROP_LINK("target", CSWMBCCIDev,
704a583303SJonathan Cameron target, TYPE_CXL_USP, PCIDevice *),
714a583303SJonathan Cameron DEFINE_PROP_END_OF_LIST(),
724a583303SJonathan Cameron };
734a583303SJonathan Cameron
cswmbcci_class_init(ObjectClass * oc,void * data)744a583303SJonathan Cameron static void cswmbcci_class_init(ObjectClass *oc, void *data)
754a583303SJonathan Cameron {
764a583303SJonathan Cameron DeviceClass *dc = DEVICE_CLASS(oc);
774a583303SJonathan Cameron PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
784a583303SJonathan Cameron
794a583303SJonathan Cameron pc->realize = cswbcci_realize;
804a583303SJonathan Cameron pc->exit = cswmbcci_exit;
814a583303SJonathan Cameron /* Serial bus, CXL Switch CCI */
824a583303SJonathan Cameron pc->class_id = 0x0c0b;
834a583303SJonathan Cameron /*
844a583303SJonathan Cameron * Huawei Technologies
854a583303SJonathan Cameron * CXL Switch Mailbox CCI - DID assigned for emulation only.
864a583303SJonathan Cameron * No real hardware will ever use this ID.
874a583303SJonathan Cameron */
884a583303SJonathan Cameron pc->vendor_id = 0x19e5;
894a583303SJonathan Cameron pc->device_id = 0xa123;
904a583303SJonathan Cameron pc->revision = 0;
914a583303SJonathan Cameron dc->desc = "CXL Switch Mailbox CCI";
92*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, cswmbcci_reset);
934a583303SJonathan Cameron device_class_set_props(dc, cxl_switch_cci_props);
944a583303SJonathan Cameron }
954a583303SJonathan Cameron
964a583303SJonathan Cameron static const TypeInfo cswmbcci_info = {
974a583303SJonathan Cameron .name = TYPE_CXL_SWITCH_MAILBOX_CCI,
984a583303SJonathan Cameron .parent = TYPE_PCI_DEVICE,
994a583303SJonathan Cameron .class_init = cswmbcci_class_init,
1004a583303SJonathan Cameron .instance_size = sizeof(CSWMBCCIDev),
1014a583303SJonathan Cameron .interfaces = (InterfaceInfo[]) {
1024a583303SJonathan Cameron { INTERFACE_PCIE_DEVICE },
1034a583303SJonathan Cameron { }
1044a583303SJonathan Cameron },
1054a583303SJonathan Cameron };
1064a583303SJonathan Cameron
cxl_switch_mailbox_cci_register(void)1074a583303SJonathan Cameron static void cxl_switch_mailbox_cci_register(void)
1084a583303SJonathan Cameron {
1094a583303SJonathan Cameron type_register_static(&cswmbcci_info);
1104a583303SJonathan Cameron }
1114a583303SJonathan Cameron type_init(cxl_switch_mailbox_cci_register);
112