pcie_port.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. int pcie_chassis_add_slot(struct PCIESlot *slot)
  86. {
  87. struct PCIEChassis *c;
  88. c = pcie_chassis_find(slot->chassis);
  89. if (!c) {
  90. return -ENODEV;
  91. }
  92. if (pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
  93. return -EBUSY;
  94. }
  95. QLIST_INSERT_HEAD(&c->slots, slot, next);
  96. return 0;
  97. }
  98. void pcie_chassis_del_slot(PCIESlot *s)
  99. {
  100. QLIST_REMOVE(s, next);
  101. }
  102. static Property pcie_port_props[] = {
  103. DEFINE_PROP_UINT8("port", PCIEPort, port, 0),
  104. DEFINE_PROP_UINT16("aer_log_max", PCIEPort,
  105. parent_obj.parent_obj.exp.aer_log.log_max,
  106. PCIE_AER_LOG_MAX_DEFAULT),
  107. DEFINE_PROP_END_OF_LIST()
  108. };
  109. static void pcie_port_class_init(ObjectClass *oc, void *data)
  110. {
  111. DeviceClass *dc = DEVICE_CLASS(oc);
  112. device_class_set_props(dc, pcie_port_props);
  113. }
  114. PCIDevice *pcie_find_port_by_pn(PCIBus *bus, uint8_t pn)
  115. {
  116. int devfn;
  117. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  118. PCIDevice *d = bus->devices[devfn];
  119. PCIEPort *port;
  120. if (!d || !pci_is_express(d) || !d->exp.exp_cap) {
  121. continue;
  122. }
  123. if (!object_dynamic_cast(OBJECT(d), TYPE_PCIE_PORT)) {
  124. continue;
  125. }
  126. port = PCIE_PORT(d);
  127. if (port->port == pn) {
  128. return d;
  129. }
  130. }
  131. return NULL;
  132. }
  133. /* Find first port in devfn number order */
  134. PCIDevice *pcie_find_port_first(PCIBus *bus)
  135. {
  136. int devfn;
  137. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  138. PCIDevice *d = bus->devices[devfn];
  139. if (!d || !pci_is_express(d) || !d->exp.exp_cap) {
  140. continue;
  141. }
  142. if (object_dynamic_cast(OBJECT(d), TYPE_PCIE_PORT)) {
  143. return d;
  144. }
  145. }
  146. return NULL;
  147. }
  148. int pcie_count_ds_ports(PCIBus *bus)
  149. {
  150. int dsp_count = 0;
  151. int devfn;
  152. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  153. PCIDevice *d = bus->devices[devfn];
  154. if (!d || !pci_is_express(d) || !d->exp.exp_cap) {
  155. continue;
  156. }
  157. if (object_dynamic_cast(OBJECT(d), TYPE_PCIE_PORT)) {
  158. dsp_count++;
  159. }
  160. }
  161. return dsp_count;
  162. }
  163. static bool pcie_slot_is_hotpluggbale_bus(HotplugHandler *plug_handler,
  164. BusState *bus)
  165. {
  166. PCIESlot *s = PCIE_SLOT(bus->parent);
  167. return s->hotplug;
  168. }
  169. static const TypeInfo pcie_port_type_info = {
  170. .name = TYPE_PCIE_PORT,
  171. .parent = TYPE_PCI_BRIDGE,
  172. .instance_size = sizeof(PCIEPort),
  173. .abstract = true,
  174. .class_init = pcie_port_class_init,
  175. };
  176. static Property pcie_slot_props[] = {
  177. DEFINE_PROP_UINT8("chassis", PCIESlot, chassis, 0),
  178. DEFINE_PROP_UINT16("slot", PCIESlot, slot, 0),
  179. DEFINE_PROP_BOOL("hotplug", PCIESlot, hotplug, true),
  180. DEFINE_PROP_BOOL("x-do-not-expose-native-hotplug-cap", PCIESlot,
  181. hide_native_hotplug_cap, false),
  182. DEFINE_PROP_END_OF_LIST()
  183. };
  184. static void pcie_slot_class_init(ObjectClass *oc, void *data)
  185. {
  186. DeviceClass *dc = DEVICE_CLASS(oc);
  187. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  188. device_class_set_props(dc, pcie_slot_props);
  189. hc->pre_plug = pcie_cap_slot_pre_plug_cb;
  190. hc->plug = pcie_cap_slot_plug_cb;
  191. hc->unplug = pcie_cap_slot_unplug_cb;
  192. hc->unplug_request = pcie_cap_slot_unplug_request_cb;
  193. hc->is_hotpluggable_bus = pcie_slot_is_hotpluggbale_bus;
  194. }
  195. static const TypeInfo pcie_slot_type_info = {
  196. .name = TYPE_PCIE_SLOT,
  197. .parent = TYPE_PCIE_PORT,
  198. .instance_size = sizeof(PCIESlot),
  199. .abstract = true,
  200. .class_init = pcie_slot_class_init,
  201. .interfaces = (InterfaceInfo[]) {
  202. { TYPE_HOTPLUG_HANDLER },
  203. { }
  204. }
  205. };
  206. static void pcie_port_register_types(void)
  207. {
  208. type_register_static(&pcie_port_type_info);
  209. type_register_static(&pcie_slot_type_info);
  210. }
  211. type_init(pcie_port_register_types)