acpi_piix4.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. #include "hw.h"
  19. #include "pc.h"
  20. #include "apm.h"
  21. #include "pm_smbus.h"
  22. #include "pci.h"
  23. #include "acpi.h"
  24. #include "sysemu.h"
  25. #include "range.h"
  26. #include "ioport.h"
  27. //#define DEBUG
  28. #ifdef DEBUG
  29. # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  30. #else
  31. # define PIIX4_DPRINTF(format, ...) do { } while (0)
  32. #endif
  33. #define ACPI_DBG_IO_ADDR 0xb044
  34. #define GPE_BASE 0xafe0
  35. #define GPE_LEN 4
  36. #define PCI_BASE 0xae00
  37. #define PCI_EJ_BASE 0xae08
  38. #define PCI_RMV_BASE 0xae0c
  39. #define PIIX4_PCI_HOTPLUG_STATUS 2
  40. struct pci_status {
  41. uint32_t up;
  42. uint32_t down;
  43. };
  44. typedef struct PIIX4PMState {
  45. PCIDevice dev;
  46. IORange ioport;
  47. ACPIPM1EVT pm1a;
  48. ACPIPM1CNT pm1_cnt;
  49. APMState apm;
  50. ACPIPMTimer tmr;
  51. PMSMBus smb;
  52. uint32_t smb_io_base;
  53. qemu_irq irq;
  54. qemu_irq smi_irq;
  55. int kvm_enabled;
  56. Notifier machine_ready;
  57. /* for pci hotplug */
  58. ACPIGPE gpe;
  59. struct pci_status pci0_status;
  60. uint32_t pci0_hotplug_enable;
  61. } PIIX4PMState;
  62. static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
  63. #define ACPI_ENABLE 0xf1
  64. #define ACPI_DISABLE 0xf0
  65. static void pm_update_sci(PIIX4PMState *s)
  66. {
  67. int sci_level, pmsts;
  68. pmsts = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
  69. sci_level = (((pmsts & s->pm1a.en) &
  70. (ACPI_BITMASK_RT_CLOCK_ENABLE |
  71. ACPI_BITMASK_POWER_BUTTON_ENABLE |
  72. ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
  73. ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
  74. (((s->gpe.sts[0] & s->gpe.en[0]) & PIIX4_PCI_HOTPLUG_STATUS) != 0);
  75. qemu_set_irq(s->irq, sci_level);
  76. /* schedule a timer interruption if needed */
  77. acpi_pm_tmr_update(&s->tmr, (s->pm1a.en & ACPI_BITMASK_TIMER_ENABLE) &&
  78. !(pmsts & ACPI_BITMASK_TIMER_STATUS));
  79. }
  80. static void pm_tmr_timer(ACPIPMTimer *tmr)
  81. {
  82. PIIX4PMState *s = container_of(tmr, PIIX4PMState, tmr);
  83. pm_update_sci(s);
  84. }
  85. static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
  86. uint64_t val)
  87. {
  88. PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
  89. if (width != 2) {
  90. PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
  91. (unsigned)addr, width, (unsigned)val);
  92. }
  93. switch(addr) {
  94. case 0x00:
  95. acpi_pm1_evt_write_sts(&s->pm1a, &s->tmr, val);
  96. pm_update_sci(s);
  97. break;
  98. case 0x02:
  99. s->pm1a.en = val;
  100. pm_update_sci(s);
  101. break;
  102. case 0x04:
  103. acpi_pm1_cnt_write(&s->pm1a, &s->pm1_cnt, val);
  104. break;
  105. default:
  106. break;
  107. }
  108. PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
  109. (unsigned int)val);
  110. }
  111. static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
  112. uint64_t *data)
  113. {
  114. PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
  115. uint32_t val;
  116. switch(addr) {
  117. case 0x00:
  118. val = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
  119. break;
  120. case 0x02:
  121. val = s->pm1a.en;
  122. break;
  123. case 0x04:
  124. val = s->pm1_cnt.cnt;
  125. break;
  126. case 0x08:
  127. val = acpi_pm_tmr_get(&s->tmr);
  128. break;
  129. default:
  130. val = 0;
  131. break;
  132. }
  133. PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
  134. *data = val;
  135. }
  136. static const IORangeOps pm_iorange_ops = {
  137. .read = pm_ioport_read,
  138. .write = pm_ioport_write,
  139. };
  140. static void apm_ctrl_changed(uint32_t val, void *arg)
  141. {
  142. PIIX4PMState *s = arg;
  143. /* ACPI specs 3.0, 4.7.2.5 */
  144. acpi_pm1_cnt_update(&s->pm1_cnt, val == ACPI_ENABLE, val == ACPI_DISABLE);
  145. if (s->dev.config[0x5b] & (1 << 1)) {
  146. if (s->smi_irq) {
  147. qemu_irq_raise(s->smi_irq);
  148. }
  149. }
  150. }
  151. static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
  152. {
  153. PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
  154. }
  155. static void pm_io_space_update(PIIX4PMState *s)
  156. {
  157. uint32_t pm_io_base;
  158. if (s->dev.config[0x80] & 1) {
  159. pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
  160. pm_io_base &= 0xffc0;
  161. /* XXX: need to improve memory and ioport allocation */
  162. PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
  163. iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
  164. ioport_register(&s->ioport);
  165. }
  166. }
  167. static void pm_write_config(PCIDevice *d,
  168. uint32_t address, uint32_t val, int len)
  169. {
  170. pci_default_write_config(d, address, val, len);
  171. if (range_covers_byte(address, len, 0x80))
  172. pm_io_space_update((PIIX4PMState *)d);
  173. }
  174. static int vmstate_acpi_post_load(void *opaque, int version_id)
  175. {
  176. PIIX4PMState *s = opaque;
  177. pm_io_space_update(s);
  178. return 0;
  179. }
  180. #define VMSTATE_GPE_ARRAY(_field, _state) \
  181. { \
  182. .name = (stringify(_field)), \
  183. .version_id = 0, \
  184. .num = GPE_LEN, \
  185. .info = &vmstate_info_uint16, \
  186. .size = sizeof(uint16_t), \
  187. .flags = VMS_ARRAY | VMS_POINTER, \
  188. .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
  189. }
  190. static const VMStateDescription vmstate_gpe = {
  191. .name = "gpe",
  192. .version_id = 1,
  193. .minimum_version_id = 1,
  194. .minimum_version_id_old = 1,
  195. .fields = (VMStateField []) {
  196. VMSTATE_GPE_ARRAY(sts, ACPIGPE),
  197. VMSTATE_GPE_ARRAY(en, ACPIGPE),
  198. VMSTATE_END_OF_LIST()
  199. }
  200. };
  201. static const VMStateDescription vmstate_pci_status = {
  202. .name = "pci_status",
  203. .version_id = 1,
  204. .minimum_version_id = 1,
  205. .minimum_version_id_old = 1,
  206. .fields = (VMStateField []) {
  207. VMSTATE_UINT32(up, struct pci_status),
  208. VMSTATE_UINT32(down, struct pci_status),
  209. VMSTATE_END_OF_LIST()
  210. }
  211. };
  212. static const VMStateDescription vmstate_acpi = {
  213. .name = "piix4_pm",
  214. .version_id = 2,
  215. .minimum_version_id = 1,
  216. .minimum_version_id_old = 1,
  217. .post_load = vmstate_acpi_post_load,
  218. .fields = (VMStateField []) {
  219. VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
  220. VMSTATE_UINT16(pm1a.sts, PIIX4PMState),
  221. VMSTATE_UINT16(pm1a.en, PIIX4PMState),
  222. VMSTATE_UINT16(pm1_cnt.cnt, PIIX4PMState),
  223. VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
  224. VMSTATE_TIMER(tmr.timer, PIIX4PMState),
  225. VMSTATE_INT64(tmr.overflow_time, PIIX4PMState),
  226. VMSTATE_STRUCT(gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
  227. VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
  228. struct pci_status),
  229. VMSTATE_END_OF_LIST()
  230. }
  231. };
  232. static void piix4_update_hotplug(PIIX4PMState *s)
  233. {
  234. PCIDevice *dev = &s->dev;
  235. BusState *bus = qdev_get_parent_bus(&dev->qdev);
  236. DeviceState *qdev, *next;
  237. s->pci0_hotplug_enable = ~0;
  238. QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
  239. PCIDeviceInfo *info = container_of(qdev->info, PCIDeviceInfo, qdev);
  240. PCIDevice *pdev = DO_UPCAST(PCIDevice, qdev, qdev);
  241. int slot = PCI_SLOT(pdev->devfn);
  242. if (info->no_hotplug) {
  243. s->pci0_hotplug_enable &= ~(1 << slot);
  244. }
  245. }
  246. }
  247. static void piix4_reset(void *opaque)
  248. {
  249. PIIX4PMState *s = opaque;
  250. uint8_t *pci_conf = s->dev.config;
  251. pci_conf[0x58] = 0;
  252. pci_conf[0x59] = 0;
  253. pci_conf[0x5a] = 0;
  254. pci_conf[0x5b] = 0;
  255. if (s->kvm_enabled) {
  256. /* Mark SMM as already inited (until KVM supports SMM). */
  257. pci_conf[0x5B] = 0x02;
  258. }
  259. piix4_update_hotplug(s);
  260. }
  261. static void piix4_powerdown(void *opaque, int irq, int power_failing)
  262. {
  263. PIIX4PMState *s = opaque;
  264. ACPIPM1EVT *pm1a = s? &s->pm1a: NULL;
  265. ACPIPMTimer *tmr = s? &s->tmr: NULL;
  266. acpi_pm1_evt_power_down(pm1a, tmr);
  267. }
  268. static void piix4_pm_machine_ready(Notifier *n, void *opaque)
  269. {
  270. PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
  271. uint8_t *pci_conf;
  272. pci_conf = s->dev.config;
  273. pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
  274. pci_conf[0x63] = 0x60;
  275. pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
  276. (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
  277. }
  278. static int piix4_pm_initfn(PCIDevice *dev)
  279. {
  280. PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
  281. uint8_t *pci_conf;
  282. pci_conf = s->dev.config;
  283. pci_conf[0x06] = 0x80;
  284. pci_conf[0x07] = 0x02;
  285. pci_conf[0x09] = 0x00;
  286. pci_conf[0x3d] = 0x01; // interrupt pin 1
  287. pci_conf[0x40] = 0x01; /* PM io base read only bit */
  288. /* APM */
  289. apm_init(&s->apm, apm_ctrl_changed, s);
  290. register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
  291. if (s->kvm_enabled) {
  292. /* Mark SMM as already inited to prevent SMM from running. KVM does not
  293. * support SMM mode. */
  294. pci_conf[0x5B] = 0x02;
  295. }
  296. /* XXX: which specification is used ? The i82731AB has different
  297. mappings */
  298. pci_conf[0x90] = s->smb_io_base | 1;
  299. pci_conf[0x91] = s->smb_io_base >> 8;
  300. pci_conf[0xd2] = 0x09;
  301. register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
  302. register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
  303. acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);
  304. acpi_gpe_init(&s->gpe, GPE_LEN);
  305. qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
  306. pm_smbus_init(&s->dev.qdev, &s->smb);
  307. s->machine_ready.notify = piix4_pm_machine_ready;
  308. qemu_add_machine_init_done_notifier(&s->machine_ready);
  309. qemu_register_reset(piix4_reset, s);
  310. piix4_acpi_system_hot_add_init(dev->bus, s);
  311. return 0;
  312. }
  313. i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
  314. qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
  315. int kvm_enabled)
  316. {
  317. PCIDevice *dev;
  318. PIIX4PMState *s;
  319. dev = pci_create(bus, devfn, "PIIX4_PM");
  320. qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
  321. s = DO_UPCAST(PIIX4PMState, dev, dev);
  322. s->irq = sci_irq;
  323. acpi_pm1_cnt_init(&s->pm1_cnt, cmos_s3);
  324. s->smi_irq = smi_irq;
  325. s->kvm_enabled = kvm_enabled;
  326. qdev_init_nofail(&dev->qdev);
  327. return s->smb.smbus;
  328. }
  329. static PCIDeviceInfo piix4_pm_info = {
  330. .qdev.name = "PIIX4_PM",
  331. .qdev.desc = "PM",
  332. .qdev.size = sizeof(PIIX4PMState),
  333. .qdev.vmsd = &vmstate_acpi,
  334. .qdev.no_user = 1,
  335. .no_hotplug = 1,
  336. .init = piix4_pm_initfn,
  337. .config_write = pm_write_config,
  338. .vendor_id = PCI_VENDOR_ID_INTEL,
  339. .device_id = PCI_DEVICE_ID_INTEL_82371AB_3,
  340. .revision = 0x03,
  341. .class_id = PCI_CLASS_BRIDGE_OTHER,
  342. .qdev.props = (Property[]) {
  343. DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
  344. DEFINE_PROP_END_OF_LIST(),
  345. }
  346. };
  347. static void piix4_pm_register(void)
  348. {
  349. pci_qdev_register(&piix4_pm_info);
  350. }
  351. device_init(piix4_pm_register);
  352. static uint32_t gpe_readb(void *opaque, uint32_t addr)
  353. {
  354. PIIX4PMState *s = opaque;
  355. uint32_t val = acpi_gpe_ioport_readb(&s->gpe, addr);
  356. PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
  357. return val;
  358. }
  359. static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
  360. {
  361. PIIX4PMState *s = opaque;
  362. acpi_gpe_ioport_writeb(&s->gpe, addr, val);
  363. pm_update_sci(s);
  364. PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
  365. }
  366. static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
  367. {
  368. uint32_t val = 0;
  369. struct pci_status *g = opaque;
  370. switch (addr) {
  371. case PCI_BASE:
  372. val = g->up;
  373. break;
  374. case PCI_BASE + 4:
  375. val = g->down;
  376. break;
  377. default:
  378. break;
  379. }
  380. PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr, val);
  381. return val;
  382. }
  383. static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
  384. {
  385. struct pci_status *g = opaque;
  386. switch (addr) {
  387. case PCI_BASE:
  388. g->up = val;
  389. break;
  390. case PCI_BASE + 4:
  391. g->down = val;
  392. break;
  393. }
  394. PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr, val);
  395. }
  396. static uint32_t pciej_read(void *opaque, uint32_t addr)
  397. {
  398. PIIX4_DPRINTF("pciej read %x\n", addr);
  399. return 0;
  400. }
  401. static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
  402. {
  403. BusState *bus = opaque;
  404. DeviceState *qdev, *next;
  405. PCIDevice *dev;
  406. PCIDeviceInfo *info;
  407. int slot = ffs(val) - 1;
  408. QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
  409. dev = DO_UPCAST(PCIDevice, qdev, qdev);
  410. info = container_of(qdev->info, PCIDeviceInfo, qdev);
  411. if (PCI_SLOT(dev->devfn) == slot && !info->no_hotplug) {
  412. qdev_free(qdev);
  413. }
  414. }
  415. PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
  416. }
  417. static uint32_t pcirmv_read(void *opaque, uint32_t addr)
  418. {
  419. PIIX4PMState *s = opaque;
  420. return s->pci0_hotplug_enable;
  421. }
  422. static void pcirmv_write(void *opaque, uint32_t addr, uint32_t val)
  423. {
  424. return;
  425. }
  426. static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
  427. PCIHotplugState state);
  428. static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
  429. {
  430. struct pci_status *pci0_status = &s->pci0_status;
  431. register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
  432. register_ioport_read(GPE_BASE, GPE_LEN, 1, gpe_readb, s);
  433. acpi_gpe_blk(&s->gpe, GPE_BASE);
  434. register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, pci0_status);
  435. register_ioport_read(PCI_BASE, 8, 4, pcihotplug_read, pci0_status);
  436. register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
  437. register_ioport_read(PCI_EJ_BASE, 4, 4, pciej_read, bus);
  438. register_ioport_write(PCI_RMV_BASE, 4, 4, pcirmv_write, s);
  439. register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
  440. pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
  441. }
  442. static void enable_device(PIIX4PMState *s, int slot)
  443. {
  444. s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
  445. s->pci0_status.up |= (1 << slot);
  446. }
  447. static void disable_device(PIIX4PMState *s, int slot)
  448. {
  449. s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
  450. s->pci0_status.down |= (1 << slot);
  451. }
  452. static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
  453. PCIHotplugState state)
  454. {
  455. int slot = PCI_SLOT(dev->devfn);
  456. PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
  457. DO_UPCAST(PCIDevice, qdev, qdev));
  458. /* Don't send event when device is enabled during qemu machine creation:
  459. * it is present on boot, no hotplug event is necessary. We do send an
  460. * event when the device is disabled later. */
  461. if (state == PCI_COLDPLUG_ENABLED) {
  462. return 0;
  463. }
  464. s->pci0_status.up = 0;
  465. s->pci0_status.down = 0;
  466. if (state == PCI_HOTPLUG_ENABLED) {
  467. enable_device(s, slot);
  468. } else {
  469. disable_device(s, slot);
  470. }
  471. pm_update_sci(s);
  472. return 0;
  473. }