acpi_piix4.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * ACPI implementation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License version 2 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  17. *
  18. * Contributions after 2012-01-13 are licensed under the terms of the
  19. * GNU GPL, version 2 or (at your option) any later version.
  20. */
  21. #include "hw.h"
  22. #include "pc.h"
  23. #include "apm.h"
  24. #include "pm_smbus.h"
  25. #include "pci/pci.h"
  26. #include "acpi.h"
  27. #include "sysemu/sysemu.h"
  28. #include "qemu/range.h"
  29. #include "exec/ioport.h"
  30. #include "fw_cfg.h"
  31. #include "exec/address-spaces.h"
  32. //#define DEBUG
  33. #ifdef DEBUG
  34. # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  35. #else
  36. # define PIIX4_DPRINTF(format, ...) do { } while (0)
  37. #endif
  38. #define GPE_BASE 0xafe0
  39. #define GPE_LEN 4
  40. #define PCI_HOTPLUG_ADDR 0xae00
  41. #define PCI_HOTPLUG_SIZE 0x000f
  42. #define PCI_UP_BASE 0xae00
  43. #define PCI_DOWN_BASE 0xae04
  44. #define PCI_EJ_BASE 0xae08
  45. #define PCI_RMV_BASE 0xae0c
  46. #define PIIX4_PCI_HOTPLUG_STATUS 2
  47. struct pci_status {
  48. uint32_t up; /* deprecated, maintained for migration compatibility */
  49. uint32_t down;
  50. };
  51. typedef struct PIIX4PMState {
  52. PCIDevice dev;
  53. MemoryRegion io;
  54. MemoryRegion io_gpe;
  55. MemoryRegion io_pci;
  56. ACPIREGS ar;
  57. APMState apm;
  58. PMSMBus smb;
  59. uint32_t smb_io_base;
  60. qemu_irq irq;
  61. qemu_irq smi_irq;
  62. int kvm_enabled;
  63. Notifier machine_ready;
  64. Notifier powerdown_notifier;
  65. /* for pci hotplug */
  66. struct pci_status pci0_status;
  67. uint32_t pci0_hotplug_enable;
  68. uint32_t pci0_slot_device_present;
  69. uint8_t disable_s3;
  70. uint8_t disable_s4;
  71. uint8_t s4_val;
  72. } PIIX4PMState;
  73. static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
  74. PCIBus *bus, PIIX4PMState *s);
  75. #define ACPI_ENABLE 0xf1
  76. #define ACPI_DISABLE 0xf0
  77. static void pm_update_sci(PIIX4PMState *s)
  78. {
  79. int sci_level, pmsts;
  80. pmsts = acpi_pm1_evt_get_sts(&s->ar);
  81. sci_level = (((pmsts & s->ar.pm1.evt.en) &
  82. (ACPI_BITMASK_RT_CLOCK_ENABLE |
  83. ACPI_BITMASK_POWER_BUTTON_ENABLE |
  84. ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
  85. ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
  86. (((s->ar.gpe.sts[0] & s->ar.gpe.en[0])
  87. & PIIX4_PCI_HOTPLUG_STATUS) != 0);
  88. qemu_set_irq(s->irq, sci_level);
  89. /* schedule a timer interruption if needed */
  90. acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
  91. !(pmsts & ACPI_BITMASK_TIMER_STATUS));
  92. }
  93. static void pm_tmr_timer(ACPIREGS *ar)
  94. {
  95. PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
  96. pm_update_sci(s);
  97. }
  98. static void apm_ctrl_changed(uint32_t val, void *arg)
  99. {
  100. PIIX4PMState *s = arg;
  101. /* ACPI specs 3.0, 4.7.2.5 */
  102. acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
  103. if (s->dev.config[0x5b] & (1 << 1)) {
  104. if (s->smi_irq) {
  105. qemu_irq_raise(s->smi_irq);
  106. }
  107. }
  108. }
  109. static void pm_io_space_update(PIIX4PMState *s)
  110. {
  111. uint32_t pm_io_base;
  112. pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
  113. pm_io_base &= 0xffc0;
  114. memory_region_transaction_begin();
  115. memory_region_set_enabled(&s->io, s->dev.config[0x80] & 1);
  116. memory_region_set_address(&s->io, pm_io_base);
  117. memory_region_transaction_commit();
  118. }
  119. static void smbus_io_space_update(PIIX4PMState *s)
  120. {
  121. s->smb_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x90));
  122. s->smb_io_base &= 0xffc0;
  123. memory_region_transaction_begin();
  124. memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & 1);
  125. memory_region_set_address(&s->smb.io, s->smb_io_base);
  126. memory_region_transaction_commit();
  127. }
  128. static void pm_write_config(PCIDevice *d,
  129. uint32_t address, uint32_t val, int len)
  130. {
  131. pci_default_write_config(d, address, val, len);
  132. if (range_covers_byte(address, len, 0x80) ||
  133. ranges_overlap(address, len, 0x40, 4)) {
  134. pm_io_space_update((PIIX4PMState *)d);
  135. }
  136. if (range_covers_byte(address, len, 0xd2) ||
  137. ranges_overlap(address, len, 0x90, 4)) {
  138. smbus_io_space_update((PIIX4PMState *)d);
  139. }
  140. }
  141. static void vmstate_pci_status_pre_save(void *opaque)
  142. {
  143. struct pci_status *pci0_status = opaque;
  144. PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
  145. /* We no longer track up, so build a safe value for migrating
  146. * to a version that still does... of course these might get lost
  147. * by an old buggy implementation, but we try. */
  148. pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
  149. }
  150. static int vmstate_acpi_post_load(void *opaque, int version_id)
  151. {
  152. PIIX4PMState *s = opaque;
  153. pm_io_space_update(s);
  154. return 0;
  155. }
  156. #define VMSTATE_GPE_ARRAY(_field, _state) \
  157. { \
  158. .name = (stringify(_field)), \
  159. .version_id = 0, \
  160. .info = &vmstate_info_uint16, \
  161. .size = sizeof(uint16_t), \
  162. .flags = VMS_SINGLE | VMS_POINTER, \
  163. .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
  164. }
  165. static const VMStateDescription vmstate_gpe = {
  166. .name = "gpe",
  167. .version_id = 1,
  168. .minimum_version_id = 1,
  169. .minimum_version_id_old = 1,
  170. .fields = (VMStateField []) {
  171. VMSTATE_GPE_ARRAY(sts, ACPIGPE),
  172. VMSTATE_GPE_ARRAY(en, ACPIGPE),
  173. VMSTATE_END_OF_LIST()
  174. }
  175. };
  176. static const VMStateDescription vmstate_pci_status = {
  177. .name = "pci_status",
  178. .version_id = 1,
  179. .minimum_version_id = 1,
  180. .minimum_version_id_old = 1,
  181. .pre_save = vmstate_pci_status_pre_save,
  182. .fields = (VMStateField []) {
  183. VMSTATE_UINT32(up, struct pci_status),
  184. VMSTATE_UINT32(down, struct pci_status),
  185. VMSTATE_END_OF_LIST()
  186. }
  187. };
  188. static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
  189. {
  190. PIIX4PMState *s = opaque;
  191. int ret, i;
  192. uint16_t temp;
  193. ret = pci_device_load(&s->dev, f);
  194. if (ret < 0) {
  195. return ret;
  196. }
  197. qemu_get_be16s(f, &s->ar.pm1.evt.sts);
  198. qemu_get_be16s(f, &s->ar.pm1.evt.en);
  199. qemu_get_be16s(f, &s->ar.pm1.cnt.cnt);
  200. ret = vmstate_load_state(f, &vmstate_apm, &s->apm, 1);
  201. if (ret) {
  202. return ret;
  203. }
  204. qemu_get_timer(f, s->ar.tmr.timer);
  205. qemu_get_sbe64s(f, &s->ar.tmr.overflow_time);
  206. qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts);
  207. for (i = 0; i < 3; i++) {
  208. qemu_get_be16s(f, &temp);
  209. }
  210. qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en);
  211. for (i = 0; i < 3; i++) {
  212. qemu_get_be16s(f, &temp);
  213. }
  214. ret = vmstate_load_state(f, &vmstate_pci_status, &s->pci0_status, 1);
  215. return ret;
  216. }
  217. /* qemu-kvm 1.2 uses version 3 but advertised as 2
  218. * To support incoming qemu-kvm 1.2 migration, change version_id
  219. * and minimum_version_id to 2 below (which breaks migration from
  220. * qemu 1.2).
  221. *
  222. */
  223. static const VMStateDescription vmstate_acpi = {
  224. .name = "piix4_pm",
  225. .version_id = 3,
  226. .minimum_version_id = 3,
  227. .minimum_version_id_old = 1,
  228. .load_state_old = acpi_load_old,
  229. .post_load = vmstate_acpi_post_load,
  230. .fields = (VMStateField []) {
  231. VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
  232. VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
  233. VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
  234. VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
  235. VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
  236. VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
  237. VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
  238. VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
  239. VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
  240. struct pci_status),
  241. VMSTATE_END_OF_LIST()
  242. }
  243. };
  244. static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
  245. {
  246. BusChild *kid, *next;
  247. BusState *bus = qdev_get_parent_bus(&s->dev.qdev);
  248. int slot = ffs(slots) - 1;
  249. bool slot_free = true;
  250. /* Mark request as complete */
  251. s->pci0_status.down &= ~(1U << slot);
  252. QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
  253. DeviceState *qdev = kid->child;
  254. PCIDevice *dev = PCI_DEVICE(qdev);
  255. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  256. if (PCI_SLOT(dev->devfn) == slot) {
  257. if (pc->no_hotplug) {
  258. slot_free = false;
  259. } else {
  260. qdev_free(qdev);
  261. }
  262. }
  263. }
  264. if (slot_free) {
  265. s->pci0_slot_device_present &= ~(1U << slot);
  266. }
  267. }
  268. static void piix4_update_hotplug(PIIX4PMState *s)
  269. {
  270. PCIDevice *dev = &s->dev;
  271. BusState *bus = qdev_get_parent_bus(&dev->qdev);
  272. BusChild *kid, *next;
  273. /* Execute any pending removes during reset */
  274. while (s->pci0_status.down) {
  275. acpi_piix_eject_slot(s, s->pci0_status.down);
  276. }
  277. s->pci0_hotplug_enable = ~0;
  278. s->pci0_slot_device_present = 0;
  279. QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
  280. DeviceState *qdev = kid->child;
  281. PCIDevice *pdev = PCI_DEVICE(qdev);
  282. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
  283. int slot = PCI_SLOT(pdev->devfn);
  284. if (pc->no_hotplug) {
  285. s->pci0_hotplug_enable &= ~(1U << slot);
  286. }
  287. s->pci0_slot_device_present |= (1U << slot);
  288. }
  289. }
  290. static void piix4_reset(void *opaque)
  291. {
  292. PIIX4PMState *s = opaque;
  293. uint8_t *pci_conf = s->dev.config;
  294. pci_conf[0x58] = 0;
  295. pci_conf[0x59] = 0;
  296. pci_conf[0x5a] = 0;
  297. pci_conf[0x5b] = 0;
  298. pci_conf[0x40] = 0x01; /* PM io base read only bit */
  299. pci_conf[0x80] = 0;
  300. if (s->kvm_enabled) {
  301. /* Mark SMM as already inited (until KVM supports SMM). */
  302. pci_conf[0x5B] = 0x02;
  303. }
  304. piix4_update_hotplug(s);
  305. }
  306. static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
  307. {
  308. PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
  309. assert(s != NULL);
  310. acpi_pm1_evt_power_down(&s->ar);
  311. }
  312. static void piix4_pm_machine_ready(Notifier *n, void *opaque)
  313. {
  314. PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
  315. uint8_t *pci_conf;
  316. pci_conf = s->dev.config;
  317. pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
  318. pci_conf[0x63] = 0x60;
  319. pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
  320. (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
  321. }
  322. static int piix4_pm_initfn(PCIDevice *dev)
  323. {
  324. PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
  325. uint8_t *pci_conf;
  326. pci_conf = s->dev.config;
  327. pci_conf[0x06] = 0x80;
  328. pci_conf[0x07] = 0x02;
  329. pci_conf[0x09] = 0x00;
  330. pci_conf[0x3d] = 0x01; // interrupt pin 1
  331. /* APM */
  332. apm_init(dev, &s->apm, apm_ctrl_changed, s);
  333. if (s->kvm_enabled) {
  334. /* Mark SMM as already inited to prevent SMM from running. KVM does not
  335. * support SMM mode. */
  336. pci_conf[0x5B] = 0x02;
  337. }
  338. /* XXX: which specification is used ? The i82731AB has different
  339. mappings */
  340. pci_conf[0x90] = s->smb_io_base | 1;
  341. pci_conf[0x91] = s->smb_io_base >> 8;
  342. pci_conf[0xd2] = 0x09;
  343. pm_smbus_init(&s->dev.qdev, &s->smb);
  344. memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1);
  345. memory_region_add_subregion(pci_address_space_io(dev),
  346. s->smb_io_base, &s->smb.io);
  347. memory_region_init(&s->io, "piix4-pm", 64);
  348. memory_region_set_enabled(&s->io, false);
  349. memory_region_add_subregion(pci_address_space_io(dev),
  350. 0, &s->io);
  351. acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
  352. acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
  353. acpi_pm1_cnt_init(&s->ar, &s->io, s->s4_val);
  354. acpi_gpe_init(&s->ar, GPE_LEN);
  355. s->powerdown_notifier.notify = piix4_pm_powerdown_req;
  356. qemu_register_powerdown_notifier(&s->powerdown_notifier);
  357. s->machine_ready.notify = piix4_pm_machine_ready;
  358. qemu_add_machine_init_done_notifier(&s->machine_ready);
  359. qemu_register_reset(piix4_reset, s);
  360. piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s);
  361. return 0;
  362. }
  363. i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
  364. qemu_irq sci_irq, qemu_irq smi_irq,
  365. int kvm_enabled, void *fw_cfg)
  366. {
  367. PCIDevice *dev;
  368. PIIX4PMState *s;
  369. dev = pci_create(bus, devfn, "PIIX4_PM");
  370. qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
  371. s = DO_UPCAST(PIIX4PMState, dev, dev);
  372. s->irq = sci_irq;
  373. s->smi_irq = smi_irq;
  374. s->kvm_enabled = kvm_enabled;
  375. qdev_init_nofail(&dev->qdev);
  376. if (fw_cfg) {
  377. uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
  378. suspend[3] = 1 | ((!s->disable_s3) << 7);
  379. suspend[4] = s->s4_val | ((!s->disable_s4) << 7);
  380. fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6);
  381. }
  382. return s->smb.smbus;
  383. }
  384. static Property piix4_pm_properties[] = {
  385. DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
  386. DEFINE_PROP_UINT8("disable_s3", PIIX4PMState, disable_s3, 0),
  387. DEFINE_PROP_UINT8("disable_s4", PIIX4PMState, disable_s4, 0),
  388. DEFINE_PROP_UINT8("s4_val", PIIX4PMState, s4_val, 2),
  389. DEFINE_PROP_END_OF_LIST(),
  390. };
  391. static void piix4_pm_class_init(ObjectClass *klass, void *data)
  392. {
  393. DeviceClass *dc = DEVICE_CLASS(klass);
  394. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  395. k->no_hotplug = 1;
  396. k->init = piix4_pm_initfn;
  397. k->config_write = pm_write_config;
  398. k->vendor_id = PCI_VENDOR_ID_INTEL;
  399. k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
  400. k->revision = 0x03;
  401. k->class_id = PCI_CLASS_BRIDGE_OTHER;
  402. dc->desc = "PM";
  403. dc->no_user = 1;
  404. dc->vmsd = &vmstate_acpi;
  405. dc->props = piix4_pm_properties;
  406. }
  407. static const TypeInfo piix4_pm_info = {
  408. .name = "PIIX4_PM",
  409. .parent = TYPE_PCI_DEVICE,
  410. .instance_size = sizeof(PIIX4PMState),
  411. .class_init = piix4_pm_class_init,
  412. };
  413. static void piix4_pm_register_types(void)
  414. {
  415. type_register_static(&piix4_pm_info);
  416. }
  417. type_init(piix4_pm_register_types)
  418. static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width)
  419. {
  420. PIIX4PMState *s = opaque;
  421. uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
  422. PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
  423. return val;
  424. }
  425. static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val,
  426. unsigned width)
  427. {
  428. PIIX4PMState *s = opaque;
  429. acpi_gpe_ioport_writeb(&s->ar, addr, val);
  430. pm_update_sci(s);
  431. PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
  432. }
  433. static const MemoryRegionOps piix4_gpe_ops = {
  434. .read = gpe_readb,
  435. .write = gpe_writeb,
  436. .valid.min_access_size = 1,
  437. .valid.max_access_size = 4,
  438. .impl.min_access_size = 1,
  439. .impl.max_access_size = 1,
  440. .endianness = DEVICE_LITTLE_ENDIAN,
  441. };
  442. static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
  443. {
  444. PIIX4PMState *s = opaque;
  445. uint32_t val = 0;
  446. switch (addr) {
  447. case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
  448. /* Manufacture an "up" value to cause a device check on any hotplug
  449. * slot with a device. Extra device checks are harmless. */
  450. val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
  451. PIIX4_DPRINTF("pci_up_read %x\n", val);
  452. break;
  453. case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
  454. val = s->pci0_status.down;
  455. PIIX4_DPRINTF("pci_down_read %x\n", val);
  456. break;
  457. case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
  458. /* No feature defined yet */
  459. PIIX4_DPRINTF("pci_features_read %x\n", val);
  460. break;
  461. case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
  462. val = s->pci0_hotplug_enable;
  463. break;
  464. default:
  465. break;
  466. }
  467. return val;
  468. }
  469. static void pci_write(void *opaque, hwaddr addr, uint64_t data,
  470. unsigned int size)
  471. {
  472. switch (addr) {
  473. case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
  474. acpi_piix_eject_slot(opaque, (uint32_t)data);
  475. PIIX4_DPRINTF("pciej write %" HWADDR_PRIx " <== % " PRIu64 "\n",
  476. addr, data);
  477. break;
  478. default:
  479. break;
  480. }
  481. }
  482. static const MemoryRegionOps piix4_pci_ops = {
  483. .read = pci_read,
  484. .write = pci_write,
  485. .endianness = DEVICE_LITTLE_ENDIAN,
  486. .valid = {
  487. .min_access_size = 4,
  488. .max_access_size = 4,
  489. },
  490. };
  491. static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
  492. PCIHotplugState state);
  493. static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
  494. PCIBus *bus, PIIX4PMState *s)
  495. {
  496. memory_region_init_io(&s->io_gpe, &piix4_gpe_ops, s, "apci-gpe0",
  497. GPE_LEN);
  498. memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
  499. memory_region_init_io(&s->io_pci, &piix4_pci_ops, s, "apci-pci-hotplug",
  500. PCI_HOTPLUG_SIZE);
  501. memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR,
  502. &s->io_pci);
  503. pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
  504. }
  505. static void enable_device(PIIX4PMState *s, int slot)
  506. {
  507. s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
  508. s->pci0_slot_device_present |= (1U << slot);
  509. }
  510. static void disable_device(PIIX4PMState *s, int slot)
  511. {
  512. s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
  513. s->pci0_status.down |= (1U << slot);
  514. }
  515. static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
  516. PCIHotplugState state)
  517. {
  518. int slot = PCI_SLOT(dev->devfn);
  519. PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
  520. PCI_DEVICE(qdev));
  521. /* Don't send event when device is enabled during qemu machine creation:
  522. * it is present on boot, no hotplug event is necessary. We do send an
  523. * event when the device is disabled later. */
  524. if (state == PCI_COLDPLUG_ENABLED) {
  525. s->pci0_slot_device_present |= (1U << slot);
  526. return 0;
  527. }
  528. if (state == PCI_HOTPLUG_ENABLED) {
  529. enable_device(s, slot);
  530. } else {
  531. disable_device(s, slot);
  532. }
  533. pm_update_sci(s);
  534. return 0;
  535. }