pcihp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.1 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 "qemu/osdep.h"
  27. #include "hw/acpi/pcihp.h"
  28. #include "hw/pci-host/i440fx.h"
  29. #include "hw/pci/pci.h"
  30. #include "hw/pci/pci_bridge.h"
  31. #include "hw/pci/pci_host.h"
  32. #include "hw/pci/pcie_port.h"
  33. #include "hw/pci-bridge/xio3130_downstream.h"
  34. #include "hw/i386/acpi-build.h"
  35. #include "hw/acpi/acpi.h"
  36. #include "hw/pci/pci_bus.h"
  37. #include "migration/vmstate.h"
  38. #include "qapi/error.h"
  39. #include "qom/qom-qobject.h"
  40. #include "trace.h"
  41. #define ACPI_PCIHP_SIZE 0x0018
  42. #define PCI_UP_BASE 0x0000
  43. #define PCI_DOWN_BASE 0x0004
  44. #define PCI_EJ_BASE 0x0008
  45. #define PCI_RMV_BASE 0x000c
  46. #define PCI_SEL_BASE 0x0010
  47. #define PCI_AIDX_BASE 0x0014
  48. typedef struct AcpiPciHpFind {
  49. int bsel;
  50. PCIBus *bus;
  51. } AcpiPciHpFind;
  52. static int acpi_pcihp_get_bsel(PCIBus *bus)
  53. {
  54. Error *local_err = NULL;
  55. uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
  56. &local_err);
  57. if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  58. if (local_err) {
  59. error_free(local_err);
  60. }
  61. return -1;
  62. } else {
  63. return bsel;
  64. }
  65. }
  66. typedef struct {
  67. unsigned bsel_alloc;
  68. bool has_bridge_hotplug;
  69. } BSELInfo;
  70. /* Assign BSEL property only to buses that support hotplug. */
  71. static void *acpi_set_bsel(PCIBus *bus, void *opaque)
  72. {
  73. BSELInfo *info = opaque;
  74. unsigned *bus_bsel;
  75. DeviceState *br = bus->qbus.parent;
  76. bool is_bridge = IS_PCI_BRIDGE(br);
  77. /* hotplugged bridges can't be described in ACPI ignore them */
  78. if (qbus_is_hotpluggable(BUS(bus))) {
  79. if (!is_bridge || (!br->hotplugged && info->has_bridge_hotplug)) {
  80. bus_bsel = g_malloc(sizeof *bus_bsel);
  81. *bus_bsel = info->bsel_alloc++;
  82. object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
  83. bus_bsel, OBJ_PROP_FLAG_READ);
  84. }
  85. }
  86. return info;
  87. }
  88. static void acpi_set_pci_info(bool has_bridge_hotplug)
  89. {
  90. static bool bsel_is_set;
  91. Object *host = acpi_get_i386_pci_host();
  92. PCIBus *bus;
  93. BSELInfo info = { .bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT,
  94. .has_bridge_hotplug = has_bridge_hotplug };
  95. if (bsel_is_set) {
  96. return;
  97. }
  98. bsel_is_set = true;
  99. if (!host) {
  100. return;
  101. }
  102. bus = PCI_HOST_BRIDGE(host)->bus;
  103. if (bus) {
  104. /* Scan all PCI buses. Set property to enable acpi based hotplug. */
  105. pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &info);
  106. }
  107. }
  108. static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
  109. {
  110. AcpiPciHpFind *find = opaque;
  111. if (find->bsel == acpi_pcihp_get_bsel(bus)) {
  112. find->bus = bus;
  113. }
  114. }
  115. static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
  116. {
  117. AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
  118. if (bsel < 0) {
  119. return NULL;
  120. }
  121. pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
  122. /* Make bsel 0 eject root bus if bsel property is not set,
  123. * for compatibility with non acpi setups.
  124. * TODO: really needed?
  125. */
  126. if (!bsel && !find.bus) {
  127. find.bus = s->root;
  128. }
  129. /*
  130. * Check if find.bus is actually hotpluggable. If bsel is set to
  131. * NULL for example on the root bus in order to make it
  132. * non-hotpluggable, find.bus will match the root bus when bsel
  133. * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
  134. * bus is not hotpluggable however, we should not select the bus.
  135. * Instead, we should set find.bus to NULL in that case. In the check
  136. * below, we generalize this case for all buses, not just the root bus.
  137. * The callers of this function check for a null return value and
  138. * handle them appropriately.
  139. */
  140. if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
  141. find.bus = NULL;
  142. }
  143. return find.bus;
  144. }
  145. static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
  146. {
  147. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  148. /*
  149. * ACPI doesn't allow hotplug of bridge devices. Don't allow
  150. * hot-unplug of bridge devices unless they were added by hotplug
  151. * (and so, not described by acpi).
  152. *
  153. * Don't allow hot-unplug of SR-IOV Virtual Functions, as they
  154. * will be removed implicitly, when Physical Function is unplugged.
  155. */
  156. return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable ||
  157. pci_is_vf(dev);
  158. }
  159. static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
  160. {
  161. HotplugHandler *hotplug_ctrl;
  162. BusChild *kid, *next;
  163. int slot = ctz32(slots);
  164. PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
  165. trace_acpi_pci_eject_slot(bsel, slot);
  166. if (!bus || slot > 31) {
  167. return;
  168. }
  169. /* Mark request as complete */
  170. s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
  171. s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
  172. QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
  173. DeviceState *qdev = kid->child;
  174. PCIDevice *dev = PCI_DEVICE(qdev);
  175. if (PCI_SLOT(dev->devfn) == slot) {
  176. if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
  177. /*
  178. * partially_hotplugged is used by virtio-net failover:
  179. * failover has asked the guest OS to unplug the device
  180. * but we need to keep some references to the device
  181. * to be able to plug it back in case of failure so
  182. * we don't execute hotplug_handler_unplug().
  183. */
  184. if (dev->partially_hotplugged) {
  185. /*
  186. * pending_deleted_event is set to true when
  187. * virtio-net failover asks to unplug the device,
  188. * and set to false here when the operation is done
  189. * This is used by the migration loop to detect the
  190. * end of the operation and really start the migration.
  191. */
  192. qdev->pending_deleted_event = false;
  193. } else {
  194. hotplug_ctrl = qdev_get_hotplug_handler(qdev);
  195. hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
  196. object_unparent(OBJECT(qdev));
  197. }
  198. }
  199. }
  200. }
  201. }
  202. static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
  203. {
  204. BusChild *kid, *next;
  205. PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
  206. /* Execute any pending removes during reset */
  207. while (s->acpi_pcihp_pci_status[bsel].down) {
  208. acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
  209. }
  210. s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
  211. if (!bus) {
  212. return;
  213. }
  214. QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
  215. DeviceState *qdev = kid->child;
  216. PCIDevice *pdev = PCI_DEVICE(qdev);
  217. int slot = PCI_SLOT(pdev->devfn);
  218. if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
  219. s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
  220. }
  221. }
  222. }
  223. static void acpi_pcihp_update(AcpiPciHpState *s)
  224. {
  225. int i;
  226. for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
  227. acpi_pcihp_update_hotplug_bus(s, i);
  228. }
  229. }
  230. void acpi_pcihp_reset(AcpiPciHpState *s)
  231. {
  232. acpi_set_pci_info(s->use_acpi_hotplug_bridge);
  233. acpi_pcihp_update(s);
  234. }
  235. void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  236. DeviceState *dev, Error **errp)
  237. {
  238. PCIDevice *pdev = PCI_DEVICE(dev);
  239. /* Only hotplugged devices need the hotplug capability. */
  240. if (dev->hotplugged &&
  241. acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
  242. error_setg(errp, "Unsupported bus. Bus doesn't have property '"
  243. ACPI_PCIHP_PROP_BSEL "' set");
  244. return;
  245. }
  246. }
  247. void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
  248. DeviceState *dev, Error **errp)
  249. {
  250. PCIDevice *pdev = PCI_DEVICE(dev);
  251. int slot = PCI_SLOT(pdev->devfn);
  252. PCIDevice *bridge;
  253. PCIBus *bus;
  254. int bsel;
  255. /* Don't send event when device is enabled during qemu machine creation:
  256. * it is present on boot, no hotplug event is necessary. We do send an
  257. * event when the device is disabled later. */
  258. if (!dev->hotplugged) {
  259. /*
  260. * Overwrite the default hotplug handler with the ACPI PCI one
  261. * for cold plugged bridges only.
  262. */
  263. if (s->use_acpi_hotplug_bridge &&
  264. object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  265. PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
  266. qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
  267. /* We don't have to overwrite any other hotplug handler yet */
  268. assert(QLIST_EMPTY(&sec->child));
  269. }
  270. return;
  271. }
  272. bus = pci_get_bus(pdev);
  273. bridge = pci_bridge_get_device(bus);
  274. if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) ||
  275. object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) {
  276. pcie_cap_slot_enable_power(bridge);
  277. }
  278. bsel = acpi_pcihp_get_bsel(bus);
  279. g_assert(bsel >= 0);
  280. s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
  281. acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
  282. }
  283. void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
  284. DeviceState *dev, Error **errp)
  285. {
  286. PCIDevice *pdev = PCI_DEVICE(dev);
  287. trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
  288. acpi_pcihp_get_bsel(pci_get_bus(pdev)));
  289. qdev_unrealize(dev);
  290. }
  291. void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  292. AcpiPciHpState *s, DeviceState *dev,
  293. Error **errp)
  294. {
  295. PCIDevice *pdev = PCI_DEVICE(dev);
  296. int slot = PCI_SLOT(pdev->devfn);
  297. int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
  298. trace_acpi_pci_unplug_request(bsel, slot);
  299. if (bsel < 0) {
  300. error_setg(errp, "Unsupported bus. Bus doesn't have property '"
  301. ACPI_PCIHP_PROP_BSEL "' set");
  302. return;
  303. }
  304. /*
  305. * pending_deleted_event is used by virtio-net failover to detect the
  306. * end of the unplug operation, the flag is set to false in
  307. * acpi_pcihp_eject_slot() when the operation is completed.
  308. */
  309. pdev->qdev.pending_deleted_event = true;
  310. /* if unplug was requested before OSPM is initialized,
  311. * linux kernel will clear GPE0.sts[] bits during boot, which effectively
  312. * hides unplug event. And than followup qmp_device_del() calls remain
  313. * blocked by above flag permanently.
  314. * Unblock qmp_device_del() by setting expire limit, so user can
  315. * repeat unplug request later when OSPM has been booted.
  316. */
  317. pdev->qdev.pending_deleted_expires_ms =
  318. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); /* 1 msec */
  319. s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
  320. acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
  321. }
  322. bool acpi_pcihp_is_hotpluggbale_bus(AcpiPciHpState *s, BusState *bus)
  323. {
  324. Object *o = OBJECT(bus->parent);
  325. if (s->use_acpi_hotplug_bridge &&
  326. object_dynamic_cast(o, TYPE_PCI_BRIDGE)) {
  327. if (object_dynamic_cast(o, TYPE_PCIE_SLOT) && !PCIE_SLOT(o)->hotplug) {
  328. return false;
  329. }
  330. return true;
  331. }
  332. if (s->use_acpi_root_pci_hotplug) {
  333. return true;
  334. }
  335. return false;
  336. }
  337. static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
  338. {
  339. AcpiPciHpState *s = opaque;
  340. uint32_t val = 0;
  341. int bsel = s->hotplug_select;
  342. if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  343. return 0;
  344. }
  345. switch (addr) {
  346. case PCI_UP_BASE:
  347. val = s->acpi_pcihp_pci_status[bsel].up;
  348. if (s->use_acpi_hotplug_bridge) {
  349. s->acpi_pcihp_pci_status[bsel].up = 0;
  350. }
  351. trace_acpi_pci_up_read(val);
  352. break;
  353. case PCI_DOWN_BASE:
  354. val = s->acpi_pcihp_pci_status[bsel].down;
  355. trace_acpi_pci_down_read(val);
  356. break;
  357. case PCI_EJ_BASE:
  358. trace_acpi_pci_features_read(val);
  359. break;
  360. case PCI_RMV_BASE:
  361. val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
  362. trace_acpi_pci_rmv_read(val);
  363. break;
  364. case PCI_SEL_BASE:
  365. val = s->hotplug_select;
  366. trace_acpi_pci_sel_read(val);
  367. break;
  368. case PCI_AIDX_BASE:
  369. val = s->acpi_index;
  370. s->acpi_index = 0;
  371. trace_acpi_pci_acpi_index_read(val);
  372. break;
  373. default:
  374. break;
  375. }
  376. return val;
  377. }
  378. static void pci_write(void *opaque, hwaddr addr, uint64_t data,
  379. unsigned int size)
  380. {
  381. int slot;
  382. PCIBus *bus;
  383. BusChild *kid, *next;
  384. AcpiPciHpState *s = opaque;
  385. s->acpi_index = 0;
  386. switch (addr) {
  387. case PCI_AIDX_BASE:
  388. /*
  389. * fetch acpi-index for specified slot so that follow up read from
  390. * PCI_AIDX_BASE can return it to guest
  391. */
  392. slot = ctz32(data);
  393. if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  394. break;
  395. }
  396. bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
  397. if (!bus) {
  398. break;
  399. }
  400. QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
  401. Object *o = OBJECT(kid->child);
  402. PCIDevice *dev = PCI_DEVICE(o);
  403. if (PCI_SLOT(dev->devfn) == slot) {
  404. s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
  405. break;
  406. }
  407. }
  408. trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
  409. break;
  410. case PCI_EJ_BASE:
  411. if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
  412. break;
  413. }
  414. acpi_pcihp_eject_slot(s, s->hotplug_select, data);
  415. trace_acpi_pci_ej_write(addr, data);
  416. break;
  417. case PCI_SEL_BASE:
  418. s->hotplug_select = s->use_acpi_hotplug_bridge ? data :
  419. ACPI_PCIHP_BSEL_DEFAULT;
  420. trace_acpi_pci_sel_write(addr, data);
  421. default:
  422. break;
  423. }
  424. }
  425. static const MemoryRegionOps acpi_pcihp_io_ops = {
  426. .read = pci_read,
  427. .write = pci_write,
  428. .endianness = DEVICE_LITTLE_ENDIAN,
  429. .valid = {
  430. .min_access_size = 4,
  431. .max_access_size = 4,
  432. },
  433. };
  434. void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
  435. MemoryRegion *address_space_io,
  436. uint16_t io_base)
  437. {
  438. s->io_len = ACPI_PCIHP_SIZE;
  439. s->io_base = io_base;
  440. s->root = root_bus;
  441. memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
  442. "acpi-pci-hotplug", s->io_len);
  443. memory_region_add_subregion(address_space_io, s->io_base, &s->io);
  444. object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
  445. OBJ_PROP_FLAG_READ);
  446. object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
  447. OBJ_PROP_FLAG_READ);
  448. }
  449. const VMStateDescription vmstate_acpi_pcihp_pci_status = {
  450. .name = "acpi_pcihp_pci_status",
  451. .version_id = 1,
  452. .minimum_version_id = 1,
  453. .fields = (VMStateField[]) {
  454. VMSTATE_UINT32(up, AcpiPciHpPciStatus),
  455. VMSTATE_UINT32(down, AcpiPciHpPciStatus),
  456. VMSTATE_END_OF_LIST()
  457. }
  458. };