pci_bridge_dev.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Standard PCI Bridge Device
  3. *
  4. * Copyright (c) 2011 Red Hat Inc. Author: Michael S. Tsirkin <mst@redhat.com>
  5. *
  6. * http://www.pcisig.com/specifications/conventional/pci_to_pci_bridge_architecture/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "hw/pci/pci_bridge.h"
  22. #include "hw/pci/pci_ids.h"
  23. #include "hw/pci/msi.h"
  24. #include "hw/pci/shpc.h"
  25. #include "hw/pci/slotid_cap.h"
  26. #include "exec/memory.h"
  27. #include "hw/pci/pci_bus.h"
  28. #include "hw/hotplug.h"
  29. #define TYPE_PCI_BRIDGE_DEV "pci-bridge"
  30. #define PCI_BRIDGE_DEV(obj) \
  31. OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)
  32. struct PCIBridgeDev {
  33. /*< private >*/
  34. PCIBridge parent_obj;
  35. /*< public >*/
  36. MemoryRegion bar;
  37. uint8_t chassis_nr;
  38. #define PCI_BRIDGE_DEV_F_MSI_REQ 0
  39. uint32_t flags;
  40. };
  41. typedef struct PCIBridgeDev PCIBridgeDev;
  42. static int pci_bridge_dev_initfn(PCIDevice *dev)
  43. {
  44. PCIBridge *br = PCI_BRIDGE(dev);
  45. PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
  46. int err;
  47. err = pci_bridge_initfn(dev, TYPE_PCI_BUS);
  48. if (err) {
  49. goto bridge_error;
  50. }
  51. dev->config[PCI_INTERRUPT_PIN] = 0x1;
  52. memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar", shpc_bar_size(dev));
  53. err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0);
  54. if (err) {
  55. goto shpc_error;
  56. }
  57. err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0);
  58. if (err) {
  59. goto slotid_error;
  60. }
  61. if ((bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_MSI_REQ)) &&
  62. msi_supported) {
  63. err = msi_init(dev, 0, 1, true, true);
  64. if (err < 0) {
  65. goto msi_error;
  66. }
  67. }
  68. /* TODO: spec recommends using 64 bit prefetcheable BAR.
  69. * Check whether that works well. */
  70. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
  71. PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
  72. return 0;
  73. msi_error:
  74. slotid_cap_cleanup(dev);
  75. slotid_error:
  76. shpc_cleanup(dev, &bridge_dev->bar);
  77. shpc_error:
  78. memory_region_destroy(&bridge_dev->bar);
  79. pci_bridge_exitfn(dev);
  80. bridge_error:
  81. return err;
  82. }
  83. static void pci_bridge_dev_exitfn(PCIDevice *dev)
  84. {
  85. PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
  86. if (msi_present(dev)) {
  87. msi_uninit(dev);
  88. }
  89. slotid_cap_cleanup(dev);
  90. shpc_cleanup(dev, &bridge_dev->bar);
  91. memory_region_destroy(&bridge_dev->bar);
  92. pci_bridge_exitfn(dev);
  93. }
  94. static void pci_bridge_dev_write_config(PCIDevice *d,
  95. uint32_t address, uint32_t val, int len)
  96. {
  97. pci_bridge_write_config(d, address, val, len);
  98. if (msi_present(d)) {
  99. msi_write_config(d, address, val, len);
  100. }
  101. shpc_cap_write_config(d, address, val, len);
  102. }
  103. static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
  104. {
  105. PCIDevice *dev = PCI_DEVICE(qdev);
  106. pci_bridge_reset(qdev);
  107. shpc_reset(dev);
  108. }
  109. static Property pci_bridge_dev_properties[] = {
  110. /* Note: 0 is not a legal chassis number. */
  111. DEFINE_PROP_UINT8("chassis_nr", PCIBridgeDev, chassis_nr, 0),
  112. DEFINE_PROP_BIT("msi", PCIBridgeDev, flags, PCI_BRIDGE_DEV_F_MSI_REQ, true),
  113. DEFINE_PROP_END_OF_LIST(),
  114. };
  115. static const VMStateDescription pci_bridge_dev_vmstate = {
  116. .name = "pci_bridge",
  117. .fields = (VMStateField[]) {
  118. VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
  119. SHPC_VMSTATE(shpc, PCIDevice),
  120. VMSTATE_END_OF_LIST()
  121. }
  122. };
  123. static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
  124. {
  125. DeviceClass *dc = DEVICE_CLASS(klass);
  126. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  127. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  128. k->init = pci_bridge_dev_initfn;
  129. k->exit = pci_bridge_dev_exitfn;
  130. k->config_write = pci_bridge_dev_write_config;
  131. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  132. k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
  133. k->class_id = PCI_CLASS_BRIDGE_PCI;
  134. k->is_bridge = 1,
  135. dc->desc = "Standard PCI Bridge";
  136. dc->reset = qdev_pci_bridge_dev_reset;
  137. dc->props = pci_bridge_dev_properties;
  138. dc->vmsd = &pci_bridge_dev_vmstate;
  139. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  140. hc->plug = shpc_device_hotplug_cb;
  141. hc->unplug = shpc_device_hot_unplug_cb;
  142. }
  143. static const TypeInfo pci_bridge_dev_info = {
  144. .name = TYPE_PCI_BRIDGE_DEV,
  145. .parent = TYPE_PCI_BRIDGE,
  146. .instance_size = sizeof(PCIBridgeDev),
  147. .class_init = pci_bridge_dev_class_init,
  148. .interfaces = (InterfaceInfo[]) {
  149. { TYPE_HOTPLUG_HANDLER },
  150. { }
  151. }
  152. };
  153. static void pci_bridge_dev_register(void)
  154. {
  155. type_register_static(&pci_bridge_dev_info);
  156. }
  157. type_init(pci_bridge_dev_register);