xen: use uint64_t instead of target_ulong in cpu_ioreq_move
[qemu/ar7.git] / hw / acpi_piix4.c
blob29f0f76c35cd0febede65fb492add484d72816f0
1 /*
2 * ACPI implementation
4 * Copyright (c) 2006 Fabrice Bellard
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.
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.
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/>
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"
28 //#define DEBUG
30 #ifdef DEBUG
31 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
32 #else
33 # define PIIX4_DPRINTF(format, ...) do { } while (0)
34 #endif
36 #define ACPI_DBG_IO_ADDR 0xb044
38 #define GPE_BASE 0xafe0
39 #define GPE_LEN 4
40 #define PCI_BASE 0xae00
41 #define PCI_EJ_BASE 0xae08
42 #define PCI_RMV_BASE 0xae0c
44 #define PIIX4_PCI_HOTPLUG_STATUS 2
46 struct pci_status {
47 uint32_t up;
48 uint32_t down;
51 typedef struct PIIX4PMState {
52 PCIDevice dev;
53 IORange ioport;
54 ACPIPM1EVT pm1a;
55 ACPIPM1CNT pm1_cnt;
57 APMState apm;
59 ACPIPMTimer tmr;
61 PMSMBus smb;
62 uint32_t smb_io_base;
64 qemu_irq irq;
65 qemu_irq smi_irq;
66 int kvm_enabled;
67 Notifier machine_ready;
69 /* for pci hotplug */
70 ACPIGPE gpe;
71 struct pci_status pci0_status;
72 uint32_t pci0_hotplug_enable;
73 } PIIX4PMState;
75 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
77 #define ACPI_ENABLE 0xf1
78 #define ACPI_DISABLE 0xf0
80 static void pm_update_sci(PIIX4PMState *s)
82 int sci_level, pmsts;
84 pmsts = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
85 sci_level = (((pmsts & s->pm1a.en) &
86 (ACPI_BITMASK_RT_CLOCK_ENABLE |
87 ACPI_BITMASK_POWER_BUTTON_ENABLE |
88 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
89 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
90 (((s->gpe.sts[0] & s->gpe.en[0]) & PIIX4_PCI_HOTPLUG_STATUS) != 0);
92 qemu_set_irq(s->irq, sci_level);
93 /* schedule a timer interruption if needed */
94 acpi_pm_tmr_update(&s->tmr, (s->pm1a.en & ACPI_BITMASK_TIMER_ENABLE) &&
95 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
98 static void pm_tmr_timer(ACPIPMTimer *tmr)
100 PIIX4PMState *s = container_of(tmr, PIIX4PMState, tmr);
101 pm_update_sci(s);
104 static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
105 uint64_t val)
107 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
109 if (width != 2) {
110 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
111 (unsigned)addr, width, (unsigned)val);
114 switch(addr) {
115 case 0x00:
116 acpi_pm1_evt_write_sts(&s->pm1a, &s->tmr, val);
117 pm_update_sci(s);
118 break;
119 case 0x02:
120 s->pm1a.en = val;
121 pm_update_sci(s);
122 break;
123 case 0x04:
124 acpi_pm1_cnt_write(&s->pm1a, &s->pm1_cnt, val);
125 break;
126 default:
127 break;
129 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
130 (unsigned int)val);
133 static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
134 uint64_t *data)
136 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
137 uint32_t val;
139 switch(addr) {
140 case 0x00:
141 val = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
142 break;
143 case 0x02:
144 val = s->pm1a.en;
145 break;
146 case 0x04:
147 val = s->pm1_cnt.cnt;
148 break;
149 case 0x08:
150 val = acpi_pm_tmr_get(&s->tmr);
151 break;
152 default:
153 val = 0;
154 break;
156 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
157 *data = val;
160 static const IORangeOps pm_iorange_ops = {
161 .read = pm_ioport_read,
162 .write = pm_ioport_write,
165 static void apm_ctrl_changed(uint32_t val, void *arg)
167 PIIX4PMState *s = arg;
169 /* ACPI specs 3.0, 4.7.2.5 */
170 acpi_pm1_cnt_update(&s->pm1_cnt, val == ACPI_ENABLE, val == ACPI_DISABLE);
172 if (s->dev.config[0x5b] & (1 << 1)) {
173 if (s->smi_irq) {
174 qemu_irq_raise(s->smi_irq);
179 static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
181 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
184 static void pm_io_space_update(PIIX4PMState *s)
186 uint32_t pm_io_base;
188 if (s->dev.config[0x80] & 1) {
189 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
190 pm_io_base &= 0xffc0;
192 /* XXX: need to improve memory and ioport allocation */
193 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
194 iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
195 ioport_register(&s->ioport);
199 static void pm_write_config(PCIDevice *d,
200 uint32_t address, uint32_t val, int len)
202 pci_default_write_config(d, address, val, len);
203 if (range_covers_byte(address, len, 0x80))
204 pm_io_space_update((PIIX4PMState *)d);
207 static int vmstate_acpi_post_load(void *opaque, int version_id)
209 PIIX4PMState *s = opaque;
211 pm_io_space_update(s);
212 return 0;
215 #define VMSTATE_GPE_ARRAY(_field, _state) \
217 .name = (stringify(_field)), \
218 .version_id = 0, \
219 .num = GPE_LEN, \
220 .info = &vmstate_info_uint16, \
221 .size = sizeof(uint16_t), \
222 .flags = VMS_ARRAY | VMS_POINTER, \
223 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
226 static const VMStateDescription vmstate_gpe = {
227 .name = "gpe",
228 .version_id = 1,
229 .minimum_version_id = 1,
230 .minimum_version_id_old = 1,
231 .fields = (VMStateField []) {
232 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
233 VMSTATE_GPE_ARRAY(en, ACPIGPE),
234 VMSTATE_END_OF_LIST()
238 static const VMStateDescription vmstate_pci_status = {
239 .name = "pci_status",
240 .version_id = 1,
241 .minimum_version_id = 1,
242 .minimum_version_id_old = 1,
243 .fields = (VMStateField []) {
244 VMSTATE_UINT32(up, struct pci_status),
245 VMSTATE_UINT32(down, struct pci_status),
246 VMSTATE_END_OF_LIST()
250 static const VMStateDescription vmstate_acpi = {
251 .name = "piix4_pm",
252 .version_id = 2,
253 .minimum_version_id = 1,
254 .minimum_version_id_old = 1,
255 .post_load = vmstate_acpi_post_load,
256 .fields = (VMStateField []) {
257 VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
258 VMSTATE_UINT16(pm1a.sts, PIIX4PMState),
259 VMSTATE_UINT16(pm1a.en, PIIX4PMState),
260 VMSTATE_UINT16(pm1_cnt.cnt, PIIX4PMState),
261 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
262 VMSTATE_TIMER(tmr.timer, PIIX4PMState),
263 VMSTATE_INT64(tmr.overflow_time, PIIX4PMState),
264 VMSTATE_STRUCT(gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
265 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
266 struct pci_status),
267 VMSTATE_END_OF_LIST()
271 static void piix4_update_hotplug(PIIX4PMState *s)
273 PCIDevice *dev = &s->dev;
274 BusState *bus = qdev_get_parent_bus(&dev->qdev);
275 DeviceState *qdev, *next;
277 s->pci0_hotplug_enable = ~0;
279 QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
280 PCIDeviceInfo *info = container_of(qdev->info, PCIDeviceInfo, qdev);
281 PCIDevice *pdev = DO_UPCAST(PCIDevice, qdev, qdev);
282 int slot = PCI_SLOT(pdev->devfn);
284 if (info->no_hotplug) {
285 s->pci0_hotplug_enable &= ~(1 << slot);
290 static void piix4_reset(void *opaque)
292 PIIX4PMState *s = opaque;
293 uint8_t *pci_conf = s->dev.config;
295 pci_conf[0x58] = 0;
296 pci_conf[0x59] = 0;
297 pci_conf[0x5a] = 0;
298 pci_conf[0x5b] = 0;
300 if (s->kvm_enabled) {
301 /* Mark SMM as already inited (until KVM supports SMM). */
302 pci_conf[0x5B] = 0x02;
304 piix4_update_hotplug(s);
307 static void piix4_powerdown(void *opaque, int irq, int power_failing)
309 PIIX4PMState *s = opaque;
310 ACPIPM1EVT *pm1a = s? &s->pm1a: NULL;
311 ACPIPMTimer *tmr = s? &s->tmr: NULL;
313 acpi_pm1_evt_power_down(pm1a, tmr);
316 static void piix4_pm_machine_ready(Notifier *n, void *opaque)
318 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
319 uint8_t *pci_conf;
321 pci_conf = s->dev.config;
322 pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
323 pci_conf[0x63] = 0x60;
324 pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
325 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
329 static int piix4_pm_initfn(PCIDevice *dev)
331 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
332 uint8_t *pci_conf;
334 pci_conf = s->dev.config;
335 pci_conf[0x06] = 0x80;
336 pci_conf[0x07] = 0x02;
337 pci_conf[0x09] = 0x00;
338 pci_conf[0x3d] = 0x01; // interrupt pin 1
340 pci_conf[0x40] = 0x01; /* PM io base read only bit */
342 /* APM */
343 apm_init(&s->apm, apm_ctrl_changed, s);
345 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
347 if (s->kvm_enabled) {
348 /* Mark SMM as already inited to prevent SMM from running. KVM does not
349 * support SMM mode. */
350 pci_conf[0x5B] = 0x02;
353 /* XXX: which specification is used ? The i82731AB has different
354 mappings */
355 pci_conf[0x90] = s->smb_io_base | 1;
356 pci_conf[0x91] = s->smb_io_base >> 8;
357 pci_conf[0xd2] = 0x09;
358 register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
359 register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
361 acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);
362 acpi_gpe_init(&s->gpe, GPE_LEN);
364 qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
366 pm_smbus_init(&s->dev.qdev, &s->smb);
367 s->machine_ready.notify = piix4_pm_machine_ready;
368 qemu_add_machine_init_done_notifier(&s->machine_ready);
369 qemu_register_reset(piix4_reset, s);
370 piix4_acpi_system_hot_add_init(dev->bus, s);
372 return 0;
375 i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
376 qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
377 int kvm_enabled)
379 PCIDevice *dev;
380 PIIX4PMState *s;
382 dev = pci_create(bus, devfn, "PIIX4_PM");
383 qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
385 s = DO_UPCAST(PIIX4PMState, dev, dev);
386 s->irq = sci_irq;
387 acpi_pm1_cnt_init(&s->pm1_cnt, cmos_s3);
388 s->smi_irq = smi_irq;
389 s->kvm_enabled = kvm_enabled;
391 qdev_init_nofail(&dev->qdev);
393 return s->smb.smbus;
396 static PCIDeviceInfo piix4_pm_info = {
397 .qdev.name = "PIIX4_PM",
398 .qdev.desc = "PM",
399 .qdev.size = sizeof(PIIX4PMState),
400 .qdev.vmsd = &vmstate_acpi,
401 .qdev.no_user = 1,
402 .no_hotplug = 1,
403 .init = piix4_pm_initfn,
404 .config_write = pm_write_config,
405 .vendor_id = PCI_VENDOR_ID_INTEL,
406 .device_id = PCI_DEVICE_ID_INTEL_82371AB_3,
407 .revision = 0x03,
408 .class_id = PCI_CLASS_BRIDGE_OTHER,
409 .qdev.props = (Property[]) {
410 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
411 DEFINE_PROP_END_OF_LIST(),
415 static void piix4_pm_register(void)
417 pci_qdev_register(&piix4_pm_info);
420 device_init(piix4_pm_register);
422 static uint32_t gpe_readb(void *opaque, uint32_t addr)
424 PIIX4PMState *s = opaque;
425 uint32_t val = acpi_gpe_ioport_readb(&s->gpe, addr);
427 PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
428 return val;
431 static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
433 PIIX4PMState *s = opaque;
435 acpi_gpe_ioport_writeb(&s->gpe, addr, val);
436 pm_update_sci(s);
438 PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
441 static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
443 uint32_t val = 0;
444 struct pci_status *g = opaque;
445 switch (addr) {
446 case PCI_BASE:
447 val = g->up;
448 break;
449 case PCI_BASE + 4:
450 val = g->down;
451 break;
452 default:
453 break;
456 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr, val);
457 return val;
460 static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
462 struct pci_status *g = opaque;
463 switch (addr) {
464 case PCI_BASE:
465 g->up = val;
466 break;
467 case PCI_BASE + 4:
468 g->down = val;
469 break;
472 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr, val);
475 static uint32_t pciej_read(void *opaque, uint32_t addr)
477 PIIX4_DPRINTF("pciej read %x\n", addr);
478 return 0;
481 static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
483 BusState *bus = opaque;
484 DeviceState *qdev, *next;
485 PCIDevice *dev;
486 PCIDeviceInfo *info;
487 int slot = ffs(val) - 1;
489 QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
490 dev = DO_UPCAST(PCIDevice, qdev, qdev);
491 info = container_of(qdev->info, PCIDeviceInfo, qdev);
492 if (PCI_SLOT(dev->devfn) == slot && !info->no_hotplug) {
493 qdev_free(qdev);
498 PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
501 static uint32_t pcirmv_read(void *opaque, uint32_t addr)
503 PIIX4PMState *s = opaque;
505 return s->pci0_hotplug_enable;
508 static void pcirmv_write(void *opaque, uint32_t addr, uint32_t val)
510 return;
513 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
514 PCIHotplugState state);
516 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
518 struct pci_status *pci0_status = &s->pci0_status;
520 register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
521 register_ioport_read(GPE_BASE, GPE_LEN, 1, gpe_readb, s);
522 acpi_gpe_blk(&s->gpe, GPE_BASE);
524 register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, pci0_status);
525 register_ioport_read(PCI_BASE, 8, 4, pcihotplug_read, pci0_status);
527 register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
528 register_ioport_read(PCI_EJ_BASE, 4, 4, pciej_read, bus);
530 register_ioport_write(PCI_RMV_BASE, 4, 4, pcirmv_write, s);
531 register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
533 pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
536 static void enable_device(PIIX4PMState *s, int slot)
538 s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
539 s->pci0_status.up |= (1 << slot);
542 static void disable_device(PIIX4PMState *s, int slot)
544 s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
545 s->pci0_status.down |= (1 << slot);
548 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
549 PCIHotplugState state)
551 int slot = PCI_SLOT(dev->devfn);
552 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
553 DO_UPCAST(PCIDevice, qdev, qdev));
555 /* Don't send event when device is enabled during qemu machine creation:
556 * it is present on boot, no hotplug event is necessary. We do send an
557 * event when the device is disabled later. */
558 if (state == PCI_COLDPLUG_ENABLED) {
559 return 0;
562 s->pci0_status.up = 0;
563 s->pci0_status.down = 0;
564 if (state == PCI_HOTPLUG_ENABLED) {
565 enable_device(s, slot);
566 } else {
567 disable_device(s, slot);
570 pm_update_sci(s);
572 return 0;