2
0

pci_bridge_dev.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "pci/pci_bridge.h"
  22. #include "pci/pci_ids.h"
  23. #include "pci/msi.h"
  24. #include "pci/shpc.h"
  25. #include "pci/slotid_cap.h"
  26. #include "exec/memory.h"
  27. #include "pci/pci_bus.h"
  28. struct PCIBridgeDev {
  29. PCIBridge bridge;
  30. MemoryRegion bar;
  31. uint8_t chassis_nr;
  32. #define PCI_BRIDGE_DEV_F_MSI_REQ 0
  33. uint32_t flags;
  34. };
  35. typedef struct PCIBridgeDev PCIBridgeDev;
  36. /* Mapping mandated by PCI-to-PCI Bridge architecture specification,
  37. * revision 1.2 */
  38. /* Table 9-1: Interrupt Binding for Devices Behind a Bridge */
  39. static int pci_bridge_dev_map_irq_fn(PCIDevice *dev, int irq_num)
  40. {
  41. return (irq_num + PCI_SLOT(dev->devfn)) % PCI_NUM_PINS;
  42. }
  43. static int pci_bridge_dev_initfn(PCIDevice *dev)
  44. {
  45. PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
  46. PCIBridgeDev *bridge_dev = DO_UPCAST(PCIBridgeDev, bridge, br);
  47. int err;
  48. pci_bridge_map_irq(br, NULL, pci_bridge_dev_map_irq_fn);
  49. err = pci_bridge_initfn(dev);
  50. if (err) {
  51. goto bridge_error;
  52. }
  53. memory_region_init(&bridge_dev->bar, "shpc-bar", shpc_bar_size(dev));
  54. err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0);
  55. if (err) {
  56. goto shpc_error;
  57. }
  58. err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0);
  59. if (err) {
  60. goto slotid_error;
  61. }
  62. if ((bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_MSI_REQ)) &&
  63. msi_supported) {
  64. err = msi_init(dev, 0, 1, true, true);
  65. if (err < 0) {
  66. goto msi_error;
  67. }
  68. }
  69. /* TODO: spec recommends using 64 bit prefetcheable BAR.
  70. * Check whether that works well. */
  71. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
  72. PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
  73. dev->config[PCI_INTERRUPT_PIN] = 0x1;
  74. return 0;
  75. msi_error:
  76. slotid_cap_cleanup(dev);
  77. slotid_error:
  78. shpc_cleanup(dev, &bridge_dev->bar);
  79. shpc_error:
  80. memory_region_destroy(&bridge_dev->bar);
  81. pci_bridge_exitfn(dev);
  82. bridge_error:
  83. return err;
  84. }
  85. static void pci_bridge_dev_exitfn(PCIDevice *dev)
  86. {
  87. PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
  88. PCIBridgeDev *bridge_dev = DO_UPCAST(PCIBridgeDev, bridge, br);
  89. if (msi_present(dev)) {
  90. msi_uninit(dev);
  91. }
  92. slotid_cap_cleanup(dev);
  93. shpc_cleanup(dev, &bridge_dev->bar);
  94. memory_region_destroy(&bridge_dev->bar);
  95. pci_bridge_exitfn(dev);
  96. }
  97. static void pci_bridge_dev_write_config(PCIDevice *d,
  98. uint32_t address, uint32_t val, int len)
  99. {
  100. pci_bridge_write_config(d, address, val, len);
  101. if (msi_present(d)) {
  102. msi_write_config(d, address, val, len);
  103. }
  104. shpc_cap_write_config(d, address, val, len);
  105. }
  106. static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
  107. {
  108. PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
  109. pci_bridge_reset(qdev);
  110. shpc_reset(dev);
  111. }
  112. static Property pci_bridge_dev_properties[] = {
  113. /* Note: 0 is not a legal chassis number. */
  114. DEFINE_PROP_UINT8("chassis_nr", PCIBridgeDev, chassis_nr, 0),
  115. DEFINE_PROP_BIT("msi", PCIBridgeDev, flags, PCI_BRIDGE_DEV_F_MSI_REQ, true),
  116. DEFINE_PROP_END_OF_LIST(),
  117. };
  118. static const VMStateDescription pci_bridge_dev_vmstate = {
  119. .name = "pci_bridge",
  120. .fields = (VMStateField[]) {
  121. VMSTATE_PCI_DEVICE(bridge.dev, PCIBridgeDev),
  122. SHPC_VMSTATE(bridge.dev.shpc, PCIBridgeDev),
  123. VMSTATE_END_OF_LIST()
  124. }
  125. };
  126. static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
  127. {
  128. DeviceClass *dc = DEVICE_CLASS(klass);
  129. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  130. k->init = pci_bridge_dev_initfn;
  131. k->exit = pci_bridge_dev_exitfn;
  132. k->config_write = pci_bridge_dev_write_config;
  133. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  134. k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
  135. k->class_id = PCI_CLASS_BRIDGE_PCI;
  136. k->is_bridge = 1,
  137. dc->desc = "Standard PCI Bridge";
  138. dc->reset = qdev_pci_bridge_dev_reset;
  139. dc->props = pci_bridge_dev_properties;
  140. dc->vmsd = &pci_bridge_dev_vmstate;
  141. }
  142. static const TypeInfo pci_bridge_dev_info = {
  143. .name = "pci-bridge",
  144. .parent = TYPE_PCI_DEVICE,
  145. .instance_size = sizeof(PCIBridgeDev),
  146. .class_init = pci_bridge_dev_class_init,
  147. };
  148. static void pci_bridge_dev_register(void)
  149. {
  150. type_register_static(&pci_bridge_dev_info);
  151. }
  152. type_init(pci_bridge_dev_register);