2
0

pcihp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * QEMU<->ACPI BIOS PCI hotplug interface
  3. *
  4. * QEMU supports PCI hotplug via ACPI. This module
  5. * implements the interface between QEMU and the ACPI BIOS.
  6. * Interface specification - see docs/specs/acpi_pci_hotplug.txt
  7. *
  8. * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
  9. * Copyright (c) 2006 Fabrice Bellard
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License version 2 as published by the Free Software Foundation.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  22. *
  23. * Contributions after 2012-01-13 are licensed under the terms of the
  24. * GNU GPL, version 2 or (at your option) any later version.
  25. */
  26. #include "hw/acpi/pcihp.h"
  27. #include "hw/hw.h"
  28. #include "hw/i386/pc.h"
  29. #include "hw/pci/pci.h"
  30. #include "hw/acpi/acpi.h"
  31. #include "sysemu/sysemu.h"
  32. #include "qemu/range.h"
  33. #include "exec/ioport.h"
  34. #include "exec/address-spaces.h"
  35. #include "hw/pci/pci_bus.h"
  36. #include "qom/qom-qobject.h"
  37. #include "qapi/qmp/qint.h"
  38. //#define DEBUG
  39. #ifdef DEBUG
  40. # define ACPI_PCIHP_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  41. #else
  42. # define ACPI_PCIHP_DPRINTF(format, ...) do { } while (0)
  43. #endif
  44. #define ACPI_PCI_HOTPLUG_STATUS 2
  45. #define ACPI_PCIHP_ADDR 0xae00
  46. #define ACPI_PCIHP_SIZE 0x0014
  47. #define ACPI_PCIHP_LEGACY_SIZE 0x000f
  48. #define PCI_UP_BASE 0x0000
  49. #define PCI_DOWN_BASE 0x0004
  50. #define PCI_EJ_BASE 0x0008
  51. #define PCI_RMV_BASE 0x000c
  52. #define PCI_SEL_BASE 0x0010
  53. typedef struct AcpiPciHpFind {
  54. int bsel;
  55. PCIBus *bus;
  56. } AcpiPciHpFind;
  57. static int acpi_pcihp_get_bsel(PCIBus *bus)
  58. {
  59. QObject *o = object_property_get_qobject(OBJECT(bus),
  60. ACPI_PCIHP_PROP_BSEL, NULL);
  61. int64_t bsel = -1;
  62. if (o) {
  63. bsel = qint_get_int(qobject_to_qint(o));
  64. }
  65. if (bsel < 0) {
  66. return -1;
  67. }
  68. return bsel;
  69. }
  70. static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
  71. {
  72. AcpiPciHpFind *find = opaque;
  73. if (find->bsel == acpi_pcihp_get_bsel(bus)) {
  74. find->bus = bus;
  75. }
  76. }
  77. static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
  78. {
  79. AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
  80. if (bsel < 0) {
  81. return NULL;
  82. }
  83. pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
  84. /* Make bsel 0 eject root bus if bsel property is not set,
  85. * for compatibility with non acpi setups.
  86. * TODO: really needed?
  87. */
  88. if (!bsel && !find.bus) {
  89. find.bus = s->root;
  90. }
  91. return find.bus;
  92. }
  93. static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
  94. {
  95. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  96. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  97. /*
  98. * ACPI doesn't allow hotplug of bridge devices. Don't allow
  99. * hot-unplug of bridge devices unless they were added by hotplug
  100. * (and so, not described by acpi).
  101. */
  102. return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
  103. }
  104. static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
  105. {
  106. BusChild *kid, *next;
  107. int slot = ffs(slots) - 1;
  108. PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
  109. if (!bus) {
  110. return;
  111. }
  112. /* Mark request as complete */
  113. s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
  114. s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
  115. QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
  116. DeviceState *qdev = kid->child;
  117. PCIDevice *dev = PCI_DEVICE(qdev);
  118. if (PCI_SLOT(dev->devfn) == slot) {
  119. if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
  120. object_unparent(OBJECT(qdev));
  121. }
  122. }
  123. }
  124. }
  125. static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
  126. {
  127. BusChild *kid, *next;
  128. PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
  129. /* Execute any pending removes during reset */
  130. while (s->acpi_pcihp_pci_status[bsel].down) {
  131. acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
  132. }
  133. s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
  134. if (!bus) {
  135. return;
  136. }
  137. QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
  138. DeviceState *qdev = kid->child;
  139. PCIDevice *pdev = PCI_DEVICE(qdev);
  140. int slot = PCI_SLOT(pdev->devfn);
  141. if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
  142. s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
  143. }
  144. }
  145. }
  146. static void acpi_pcihp_update(AcpiPciHpState *s)
  147. {
  148. int i;
  149. for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
  150. acpi_pcihp_update_hotplug_bus(s, i);
  151. }
  152. }
  153. void acpi_pcihp_reset(AcpiPciHpState *s)
  154. {
  155. acpi_pcihp_update(s);
  156. }
  157. void acpi_pcihp_device_plug_cb(ACPIREGS *ar, qemu_irq irq, AcpiPciHpState *s,
  158. DeviceState *dev, Error **errp)
  159. {
  160. PCIDevice *pdev = PCI_DEVICE(dev);
  161. int slot = PCI_SLOT(pdev->devfn);
  162. int bsel = acpi_pcihp_get_bsel(pdev->bus);
  163. if (bsel < 0) {
  164. error_setg(errp, "Unsupported bus. Bus doesn't have property '"
  165. ACPI_PCIHP_PROP_BSEL "' set");
  166. return;
  167. }
  168. /* Don't send event when device is enabled during qemu machine creation:
  169. * it is present on boot, no hotplug event is necessary. We do send an
  170. * event when the device is disabled later. */
  171. if (!dev->hotplugged) {
  172. return;
  173. }
  174. s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
  175. ar->gpe.sts[0] |= ACPI_PCI_HOTPLUG_STATUS;
  176. acpi_update_sci(ar, irq);
  177. }
  178. void acpi_pcihp_device_unplug_cb(ACPIREGS *ar, qemu_irq irq, AcpiPciHpState *s,
  179. DeviceState *dev, Error **errp)
  180. {
  181. PCIDevice *pdev = PCI_DEVICE(dev);
  182. int slot = PCI_SLOT(pdev->devfn);
  183. int bsel = acpi_pcihp_get_bsel(pdev->bus);
  184. if (bsel < 0) {
  185. error_setg(errp, "Unsupported bus. Bus doesn't have property '"
  186. ACPI_PCIHP_PROP_BSEL "' set");
  187. return;
  188. }
  189. s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
  190. ar->gpe.sts[0] |= ACPI_PCI_HOTPLUG_STATUS;
  191. acpi_update_sci(ar, irq);
  192. }
  193. static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
  194. {
  195. AcpiPciHpState *s = opaque;
  196. uint32_t val = 0;
  197. int bsel = s->hotplug_select;
  198. if (bsel < 0 || bsel > ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  199. return 0;
  200. }
  201. switch (addr) {
  202. case PCI_UP_BASE:
  203. val = s->acpi_pcihp_pci_status[bsel].up;
  204. if (!s->legacy_piix) {
  205. s->acpi_pcihp_pci_status[bsel].up = 0;
  206. }
  207. ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
  208. break;
  209. case PCI_DOWN_BASE:
  210. val = s->acpi_pcihp_pci_status[bsel].down;
  211. ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
  212. break;
  213. case PCI_EJ_BASE:
  214. /* No feature defined yet */
  215. ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
  216. break;
  217. case PCI_RMV_BASE:
  218. val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
  219. ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
  220. break;
  221. case PCI_SEL_BASE:
  222. val = s->hotplug_select;
  223. ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
  224. default:
  225. break;
  226. }
  227. return val;
  228. }
  229. static void pci_write(void *opaque, hwaddr addr, uint64_t data,
  230. unsigned int size)
  231. {
  232. AcpiPciHpState *s = opaque;
  233. switch (addr) {
  234. case PCI_EJ_BASE:
  235. if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  236. break;
  237. }
  238. acpi_pcihp_eject_slot(s, s->hotplug_select, data);
  239. ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
  240. addr, data);
  241. break;
  242. case PCI_SEL_BASE:
  243. s->hotplug_select = data;
  244. ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
  245. addr, data);
  246. default:
  247. break;
  248. }
  249. }
  250. static const MemoryRegionOps acpi_pcihp_io_ops = {
  251. .read = pci_read,
  252. .write = pci_write,
  253. .endianness = DEVICE_LITTLE_ENDIAN,
  254. .valid = {
  255. .min_access_size = 4,
  256. .max_access_size = 4,
  257. },
  258. };
  259. void acpi_pcihp_init(AcpiPciHpState *s, PCIBus *root_bus,
  260. MemoryRegion *address_space_io, bool bridges_enabled)
  261. {
  262. uint16_t io_size = ACPI_PCIHP_SIZE;
  263. s->root= root_bus;
  264. s->legacy_piix = !bridges_enabled;
  265. if (s->legacy_piix) {
  266. unsigned *bus_bsel = g_malloc(sizeof *bus_bsel);
  267. io_size = ACPI_PCIHP_LEGACY_SIZE;
  268. *bus_bsel = ACPI_PCIHP_BSEL_DEFAULT;
  269. object_property_add_uint32_ptr(OBJECT(root_bus), ACPI_PCIHP_PROP_BSEL,
  270. bus_bsel, NULL);
  271. }
  272. memory_region_init_io(&s->io, NULL, &acpi_pcihp_io_ops, s,
  273. "acpi-pci-hotplug", io_size);
  274. memory_region_add_subregion(address_space_io, ACPI_PCIHP_ADDR, &s->io);
  275. }
  276. const VMStateDescription vmstate_acpi_pcihp_pci_status = {
  277. .name = "acpi_pcihp_pci_status",
  278. .version_id = 1,
  279. .minimum_version_id = 1,
  280. .minimum_version_id_old = 1,
  281. .fields = (VMStateField []) {
  282. VMSTATE_UINT32(up, AcpiPciHpPciStatus),
  283. VMSTATE_UINT32(down, AcpiPciHpPciStatus),
  284. VMSTATE_END_OF_LIST()
  285. }
  286. };