2
0

pci_bridge_dev.c 9.8 KB

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