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