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