switch-mailbox-cci.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. *
  4. * Emulation of a CXL Switch Mailbox CCI PCIe function.
  5. *
  6. * Copyright (c) 2023 Huawei Technologies.
  7. *
  8. * From www.computeexpresslink.org
  9. * Compute Express Link (CXL) Specification revision 3.0 Version 1.0
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/pci/pci.h"
  13. #include "hw/pci-bridge/cxl_upstream_port.h"
  14. #include "qapi/error.h"
  15. #include "qemu/log.h"
  16. #include "qemu/module.h"
  17. #include "hw/qdev-properties.h"
  18. #include "hw/cxl/cxl.h"
  19. #define CXL_SWCCI_MSIX_MBOX 3
  20. static void cswmbcci_reset(DeviceState *dev)
  21. {
  22. CSWMBCCIDev *cswmb = CXL_SWITCH_MAILBOX_CCI(dev);
  23. cxl_device_register_init_swcci(cswmb, CXL_SWCCI_MSIX_MBOX);
  24. }
  25. static void cswbcci_realize(PCIDevice *pci_dev, Error **errp)
  26. {
  27. CSWMBCCIDev *cswmb = CXL_SWITCH_MAILBOX_CCI(pci_dev);
  28. CXLComponentState *cxl_cstate = &cswmb->cxl_cstate;
  29. CXLDeviceState *cxl_dstate = &cswmb->cxl_dstate;
  30. CXLDVSECRegisterLocator *regloc_dvsec;
  31. CXLUpstreamPort *usp;
  32. if (!cswmb->target) {
  33. error_setg(errp, "Target not set");
  34. return;
  35. }
  36. usp = CXL_USP(cswmb->target);
  37. pcie_endpoint_cap_init(pci_dev, 0x80);
  38. cxl_cstate->dvsec_offset = 0x100;
  39. cxl_cstate->pdev = pci_dev;
  40. cswmb->cci = &usp->swcci;
  41. cxl_device_register_block_init(OBJECT(pci_dev), cxl_dstate, cswmb->cci);
  42. pci_register_bar(pci_dev, 0,
  43. PCI_BASE_ADDRESS_SPACE_MEMORY |
  44. PCI_BASE_ADDRESS_MEM_TYPE_64,
  45. &cxl_dstate->device_registers);
  46. regloc_dvsec = &(CXLDVSECRegisterLocator) {
  47. .rsvd = 0,
  48. .reg0_base_lo = RBI_CXL_DEVICE_REG | 0,
  49. .reg0_base_hi = 0,
  50. };
  51. cxl_component_create_dvsec(cxl_cstate, CXL3_SWITCH_MAILBOX_CCI,
  52. REG_LOC_DVSEC_LENGTH, REG_LOC_DVSEC,
  53. REG_LOC_DVSEC_REVID, (uint8_t *)regloc_dvsec);
  54. cxl_initialize_mailbox_swcci(cswmb->cci, DEVICE(pci_dev),
  55. DEVICE(cswmb->target),
  56. CXL_MAILBOX_MAX_PAYLOAD_SIZE);
  57. }
  58. static void cswmbcci_exit(PCIDevice *pci_dev)
  59. {
  60. /* Nothing to do here yet */
  61. }
  62. static const Property cxl_switch_cci_props[] = {
  63. DEFINE_PROP_LINK("target", CSWMBCCIDev,
  64. target, TYPE_CXL_USP, PCIDevice *),
  65. };
  66. static void cswmbcci_class_init(ObjectClass *oc, void *data)
  67. {
  68. DeviceClass *dc = DEVICE_CLASS(oc);
  69. PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
  70. pc->realize = cswbcci_realize;
  71. pc->exit = cswmbcci_exit;
  72. /* Serial bus, CXL Switch CCI */
  73. pc->class_id = 0x0c0b;
  74. /*
  75. * Huawei Technologies
  76. * CXL Switch Mailbox CCI - DID assigned for emulation only.
  77. * No real hardware will ever use this ID.
  78. */
  79. pc->vendor_id = 0x19e5;
  80. pc->device_id = 0xa123;
  81. pc->revision = 0;
  82. dc->desc = "CXL Switch Mailbox CCI";
  83. device_class_set_legacy_reset(dc, cswmbcci_reset);
  84. device_class_set_props(dc, cxl_switch_cci_props);
  85. }
  86. static const TypeInfo cswmbcci_info = {
  87. .name = TYPE_CXL_SWITCH_MAILBOX_CCI,
  88. .parent = TYPE_PCI_DEVICE,
  89. .class_init = cswmbcci_class_init,
  90. .instance_size = sizeof(CSWMBCCIDev),
  91. .interfaces = (InterfaceInfo[]) {
  92. { INTERFACE_PCIE_DEVICE },
  93. { }
  94. },
  95. };
  96. static void cxl_switch_mailbox_cci_register(void)
  97. {
  98. type_register_static(&cswmbcci_info);
  99. }
  100. type_init(cxl_switch_mailbox_cci_register);