pcie_port.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * pcie_port.c
  3. *
  4. * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
  5. * VA Linux Systems Japan K.K.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/pci/pcie_port.h"
  22. #include "hw/qdev-properties.h"
  23. #include "qemu/module.h"
  24. #include "hw/hotplug.h"
  25. void pcie_port_init_reg(PCIDevice *d)
  26. {
  27. /* Unlike pci bridge,
  28. 66MHz and fast back to back don't apply to pci express port. */
  29. pci_set_word(d->config + PCI_STATUS, 0);
  30. pci_set_word(d->config + PCI_SEC_STATUS, 0);
  31. /*
  32. * Unlike conventional pci bridge, for some bits the spec states:
  33. * Does not apply to PCI Express and must be hardwired to 0.
  34. */
  35. pci_word_test_and_clear_mask(d->wmask + PCI_BRIDGE_CONTROL,
  36. PCI_BRIDGE_CTL_MASTER_ABORT |
  37. PCI_BRIDGE_CTL_FAST_BACK |
  38. PCI_BRIDGE_CTL_DISCARD |
  39. PCI_BRIDGE_CTL_SEC_DISCARD |
  40. PCI_BRIDGE_CTL_DISCARD_STATUS |
  41. PCI_BRIDGE_CTL_DISCARD_SERR);
  42. }
  43. /**************************************************************************
  44. * (chassis number, pcie physical slot number) -> pcie slot conversion
  45. */
  46. struct PCIEChassis {
  47. uint8_t number;
  48. QLIST_HEAD(, PCIESlot) slots;
  49. QLIST_ENTRY(PCIEChassis) next;
  50. };
  51. static QLIST_HEAD(, PCIEChassis) chassis = QLIST_HEAD_INITIALIZER(chassis);
  52. static struct PCIEChassis *pcie_chassis_find(uint8_t chassis_number)
  53. {
  54. struct PCIEChassis *c;
  55. QLIST_FOREACH(c, &chassis, next) {
  56. if (c->number == chassis_number) {
  57. break;
  58. }
  59. }
  60. return c;
  61. }
  62. void pcie_chassis_create(uint8_t chassis_number)
  63. {
  64. struct PCIEChassis *c;
  65. c = pcie_chassis_find(chassis_number);
  66. if (c) {
  67. return;
  68. }
  69. c = g_malloc0(sizeof(*c));
  70. c->number = chassis_number;
  71. QLIST_INIT(&c->slots);
  72. QLIST_INSERT_HEAD(&chassis, c, next);
  73. }
  74. static PCIESlot *pcie_chassis_find_slot_with_chassis(struct PCIEChassis *c,
  75. uint8_t slot)
  76. {
  77. PCIESlot *s;
  78. QLIST_FOREACH(s, &c->slots, next) {
  79. if (s->slot == slot) {
  80. break;
  81. }
  82. }
  83. return s;
  84. }
  85. PCIESlot *pcie_chassis_find_slot(uint8_t chassis_number, uint16_t slot)
  86. {
  87. struct PCIEChassis *c;
  88. c = pcie_chassis_find(chassis_number);
  89. if (!c) {
  90. return NULL;
  91. }
  92. return pcie_chassis_find_slot_with_chassis(c, slot);
  93. }
  94. int pcie_chassis_add_slot(struct PCIESlot *slot)
  95. {
  96. struct PCIEChassis *c;
  97. c = pcie_chassis_find(slot->chassis);
  98. if (!c) {
  99. return -ENODEV;
  100. }
  101. if (pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
  102. return -EBUSY;
  103. }
  104. QLIST_INSERT_HEAD(&c->slots, slot, next);
  105. return 0;
  106. }
  107. void pcie_chassis_del_slot(PCIESlot *s)
  108. {
  109. QLIST_REMOVE(s, next);
  110. }
  111. static Property pcie_port_props[] = {
  112. DEFINE_PROP_UINT8("port", PCIEPort, port, 0),
  113. DEFINE_PROP_UINT16("aer_log_max", PCIEPort,
  114. parent_obj.parent_obj.exp.aer_log.log_max,
  115. PCIE_AER_LOG_MAX_DEFAULT),
  116. DEFINE_PROP_END_OF_LIST()
  117. };
  118. static void pcie_port_class_init(ObjectClass *oc, void *data)
  119. {
  120. DeviceClass *dc = DEVICE_CLASS(oc);
  121. dc->props = pcie_port_props;
  122. }
  123. static const TypeInfo pcie_port_type_info = {
  124. .name = TYPE_PCIE_PORT,
  125. .parent = TYPE_PCI_BRIDGE,
  126. .instance_size = sizeof(PCIEPort),
  127. .abstract = true,
  128. .class_init = pcie_port_class_init,
  129. };
  130. static Property pcie_slot_props[] = {
  131. DEFINE_PROP_UINT8("chassis", PCIESlot, chassis, 0),
  132. DEFINE_PROP_UINT16("slot", PCIESlot, slot, 0),
  133. DEFINE_PROP_END_OF_LIST()
  134. };
  135. static void pcie_slot_class_init(ObjectClass *oc, void *data)
  136. {
  137. DeviceClass *dc = DEVICE_CLASS(oc);
  138. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  139. dc->props = pcie_slot_props;
  140. hc->pre_plug = pcie_cap_slot_pre_plug_cb;
  141. hc->plug = pcie_cap_slot_plug_cb;
  142. hc->unplug = pcie_cap_slot_unplug_cb;
  143. hc->unplug_request = pcie_cap_slot_unplug_request_cb;
  144. }
  145. static const TypeInfo pcie_slot_type_info = {
  146. .name = TYPE_PCIE_SLOT,
  147. .parent = TYPE_PCIE_PORT,
  148. .instance_size = sizeof(PCIESlot),
  149. .abstract = true,
  150. .class_init = pcie_slot_class_init,
  151. .interfaces = (InterfaceInfo[]) {
  152. { TYPE_HOTPLUG_HANDLER },
  153. { }
  154. }
  155. };
  156. static void pcie_port_register_types(void)
  157. {
  158. type_register_static(&pcie_port_type_info);
  159. type_register_static(&pcie_slot_type_info);
  160. }
  161. type_init(pcie_port_register_types)