pci_bridge_dev.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "qemu/osdep.h"
  22. #include "qapi/error.h"
  23. #include "qemu/module.h"
  24. #include "hw/pci/pci_bridge.h"
  25. #include "hw/pci/pci_ids.h"
  26. #include "hw/pci/msi.h"
  27. #include "hw/pci/shpc.h"
  28. #include "hw/pci/slotid_cap.h"
  29. #include "hw/qdev-properties.h"
  30. #include "exec/memory.h"
  31. #include "hw/pci/pci_bus.h"
  32. #include "hw/hotplug.h"
  33. #include "qom/object.h"
  34. #define TYPE_PCI_BRIDGE_DEV "pci-bridge"
  35. #define TYPE_PCI_BRIDGE_SEAT_DEV "pci-bridge-seat"
  36. OBJECT_DECLARE_SIMPLE_TYPE(PCIBridgeDev, PCI_BRIDGE_DEV)
  37. struct PCIBridgeDev {
  38. /*< private >*/
  39. PCIBridge parent_obj;
  40. /*< public >*/
  41. MemoryRegion bar;
  42. uint8_t chassis_nr;
  43. #define PCI_BRIDGE_DEV_F_SHPC_REQ 0
  44. uint32_t flags;
  45. OnOffAuto msi;
  46. /* additional resources to reserve */
  47. PCIResReserve res_reserve;
  48. };
  49. static void pci_bridge_dev_realize(PCIDevice *dev, Error **errp)
  50. {
  51. PCIBridge *br = PCI_BRIDGE(dev);
  52. PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
  53. int err;
  54. Error *local_err = NULL;
  55. pci_bridge_initfn(dev, TYPE_PCI_BUS);
  56. if (bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_SHPC_REQ)) {
  57. dev->config[PCI_INTERRUPT_PIN] = 0x1;
  58. memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar",
  59. shpc_bar_size(dev));
  60. err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0, errp);
  61. if (err) {
  62. goto shpc_error;
  63. }
  64. } else {
  65. /* MSI is not applicable without SHPC */
  66. bridge_dev->msi = ON_OFF_AUTO_OFF;
  67. }
  68. err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0, errp);
  69. if (err) {
  70. goto slotid_error;
  71. }
  72. if (bridge_dev->msi != ON_OFF_AUTO_OFF) {
  73. /* it means SHPC exists, because MSI is needed by SHPC */
  74. err = msi_init(dev, 0, 1, true, true, &local_err);
  75. /* Any error other than -ENOTSUP(board's MSI support is broken)
  76. * is a programming error */
  77. assert(!err || err == -ENOTSUP);
  78. if (err && bridge_dev->msi == ON_OFF_AUTO_ON) {
  79. /* Can't satisfy user's explicit msi=on request, fail */
  80. error_append_hint(&local_err, "You have to use msi=auto (default) "
  81. "or msi=off with this machine type.\n");
  82. error_propagate(errp, local_err);
  83. goto msi_error;
  84. }
  85. assert(!local_err || bridge_dev->msi == ON_OFF_AUTO_AUTO);
  86. /* With msi=auto, we fall back to MSI off silently */
  87. error_free(local_err);
  88. }
  89. err = pci_bridge_qemu_reserve_cap_init(dev, 0,
  90. bridge_dev->res_reserve, errp);
  91. if (err) {
  92. goto cap_error;
  93. }
  94. if (shpc_present(dev)) {
  95. /* TODO: spec recommends using 64 bit prefetcheable BAR.
  96. * Check whether that works well. */
  97. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
  98. PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
  99. }
  100. return;
  101. cap_error:
  102. msi_uninit(dev);
  103. msi_error:
  104. slotid_cap_cleanup(dev);
  105. slotid_error:
  106. if (shpc_present(dev)) {
  107. shpc_cleanup(dev, &bridge_dev->bar);
  108. }
  109. shpc_error:
  110. pci_bridge_exitfn(dev);
  111. }
  112. static void pci_bridge_dev_exitfn(PCIDevice *dev)
  113. {
  114. PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
  115. pci_del_capability(dev, PCI_CAP_ID_VNDR, sizeof(PCIBridgeQemuCap));
  116. if (msi_present(dev)) {
  117. msi_uninit(dev);
  118. }
  119. slotid_cap_cleanup(dev);
  120. if (shpc_present(dev)) {
  121. shpc_cleanup(dev, &bridge_dev->bar);
  122. }
  123. pci_bridge_exitfn(dev);
  124. }
  125. static void pci_bridge_dev_instance_finalize(Object *obj)
  126. {
  127. /* this function is idempotent and handles (PCIDevice.shpc == NULL) */
  128. shpc_free(PCI_DEVICE(obj));
  129. }
  130. static void pci_bridge_dev_write_config(PCIDevice *d,
  131. uint32_t address, uint32_t val, int len)
  132. {
  133. pci_bridge_write_config(d, address, val, len);
  134. if (msi_present(d)) {
  135. msi_write_config(d, address, val, len);
  136. }
  137. if (shpc_present(d)) {
  138. shpc_cap_write_config(d, address, val, len);
  139. }
  140. }
  141. static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
  142. {
  143. PCIDevice *dev = PCI_DEVICE(qdev);
  144. pci_bridge_reset(qdev);
  145. if (shpc_present(dev)) {
  146. shpc_reset(dev);
  147. }
  148. }
  149. static const Property pci_bridge_dev_properties[] = {
  150. /* Note: 0 is not a legal chassis number. */
  151. DEFINE_PROP_UINT8(PCI_BRIDGE_DEV_PROP_CHASSIS_NR, PCIBridgeDev, chassis_nr,
  152. 0),
  153. DEFINE_PROP_ON_OFF_AUTO(PCI_BRIDGE_DEV_PROP_MSI, PCIBridgeDev, msi,
  154. ON_OFF_AUTO_AUTO),
  155. DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_SHPC, PCIBridgeDev, flags,
  156. PCI_BRIDGE_DEV_F_SHPC_REQ, true),
  157. DEFINE_PROP_UINT32("bus-reserve", PCIBridgeDev,
  158. res_reserve.bus, -1),
  159. DEFINE_PROP_SIZE("io-reserve", PCIBridgeDev,
  160. res_reserve.io, -1),
  161. DEFINE_PROP_SIZE("mem-reserve", PCIBridgeDev,
  162. res_reserve.mem_non_pref, -1),
  163. DEFINE_PROP_SIZE("pref32-reserve", PCIBridgeDev,
  164. res_reserve.mem_pref_32, -1),
  165. DEFINE_PROP_SIZE("pref64-reserve", PCIBridgeDev,
  166. res_reserve.mem_pref_64, -1),
  167. };
  168. static bool pci_device_shpc_present(void *opaque, int version_id)
  169. {
  170. PCIDevice *dev = opaque;
  171. return shpc_present(dev);
  172. }
  173. static const VMStateDescription pci_bridge_dev_vmstate = {
  174. .name = "pci_bridge",
  175. .priority = MIG_PRI_PCI_BUS,
  176. .fields = (const VMStateField[]) {
  177. VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
  178. SHPC_VMSTATE(shpc, PCIDevice, pci_device_shpc_present),
  179. VMSTATE_END_OF_LIST()
  180. }
  181. };
  182. void pci_bridge_dev_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  183. Error **errp)
  184. {
  185. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  186. if (!shpc_present(pci_hotplug_dev)) {
  187. error_setg(errp, "standard hotplug controller has been disabled for "
  188. "this %s", object_get_typename(OBJECT(hotplug_dev)));
  189. return;
  190. }
  191. shpc_device_plug_cb(hotplug_dev, dev, errp);
  192. }
  193. void pci_bridge_dev_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
  194. Error **errp)
  195. {
  196. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  197. g_assert(shpc_present(pci_hotplug_dev));
  198. shpc_device_unplug_cb(hotplug_dev, dev, errp);
  199. }
  200. void pci_bridge_dev_unplug_request_cb(HotplugHandler *hotplug_dev,
  201. DeviceState *dev, Error **errp)
  202. {
  203. PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
  204. if (!shpc_present(pci_hotplug_dev)) {
  205. error_setg(errp, "standard hotplug controller has been disabled for "
  206. "this %s", object_get_typename(OBJECT(hotplug_dev)));
  207. return;
  208. }
  209. shpc_device_unplug_request_cb(hotplug_dev, dev, errp);
  210. }
  211. static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
  212. {
  213. DeviceClass *dc = DEVICE_CLASS(klass);
  214. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  215. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  216. k->realize = pci_bridge_dev_realize;
  217. k->exit = pci_bridge_dev_exitfn;
  218. k->config_write = pci_bridge_dev_write_config;
  219. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  220. k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
  221. k->class_id = PCI_CLASS_BRIDGE_PCI;
  222. dc->desc = "Standard PCI Bridge";
  223. device_class_set_legacy_reset(dc, qdev_pci_bridge_dev_reset);
  224. device_class_set_props(dc, pci_bridge_dev_properties);
  225. dc->vmsd = &pci_bridge_dev_vmstate;
  226. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  227. hc->plug = pci_bridge_dev_plug_cb;
  228. hc->unplug = pci_bridge_dev_unplug_cb;
  229. hc->unplug_request = pci_bridge_dev_unplug_request_cb;
  230. }
  231. static const TypeInfo pci_bridge_dev_info = {
  232. .name = TYPE_PCI_BRIDGE_DEV,
  233. .parent = TYPE_PCI_BRIDGE,
  234. .instance_size = sizeof(PCIBridgeDev),
  235. .class_init = pci_bridge_dev_class_init,
  236. .instance_finalize = pci_bridge_dev_instance_finalize,
  237. .interfaces = (InterfaceInfo[]) {
  238. { TYPE_HOTPLUG_HANDLER },
  239. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  240. { }
  241. }
  242. };
  243. /*
  244. * Multiseat bridge. Same as the standard pci bridge, only with a
  245. * different pci id, so we can match it easily in the guest for
  246. * automagic multiseat configuration. See docs/multiseat.txt for more.
  247. */
  248. static void pci_bridge_dev_seat_class_init(ObjectClass *klass, void *data)
  249. {
  250. DeviceClass *dc = DEVICE_CLASS(klass);
  251. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  252. k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT;
  253. dc->desc = "Standard PCI Bridge (multiseat)";
  254. }
  255. static const TypeInfo pci_bridge_dev_seat_info = {
  256. .name = TYPE_PCI_BRIDGE_SEAT_DEV,
  257. .parent = TYPE_PCI_BRIDGE_DEV,
  258. .instance_size = sizeof(PCIBridgeDev),
  259. .class_init = pci_bridge_dev_seat_class_init,
  260. };
  261. static void pci_bridge_dev_register(void)
  262. {
  263. type_register_static(&pci_bridge_dev_info);
  264. type_register_static(&pci_bridge_dev_seat_info);
  265. }
  266. type_init(pci_bridge_dev_register);