Statistics
| Branch: | Revision:

root / hw / acpi_piix4.c @ 5e22c276

History | View | Annotate | Download (18.6 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 6b620ca3 Paolo Bonzini
 *
18 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
19 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
20 93d89f63 Isaku Yamahata
 */
21 93d89f63 Isaku Yamahata
#include "hw.h"
22 93d89f63 Isaku Yamahata
#include "pc.h"
23 93d89f63 Isaku Yamahata
#include "apm.h"
24 93d89f63 Isaku Yamahata
#include "pm_smbus.h"
25 a2cb15b0 Michael S. Tsirkin
#include "pci/pci.h"
26 93d89f63 Isaku Yamahata
#include "acpi.h"
27 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
28 1de7afc9 Paolo Bonzini
#include "qemu/range.h"
29 022c62cb Paolo Bonzini
#include "exec/ioport.h"
30 459ae5ea Gleb Natapov
#include "fw_cfg.h"
31 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
32 93d89f63 Isaku Yamahata
33 93d89f63 Isaku Yamahata
//#define DEBUG
34 93d89f63 Isaku Yamahata
35 50d8ff8b Isaku Yamahata
#ifdef DEBUG
36 50d8ff8b Isaku Yamahata
# define PIIX4_DPRINTF(format, ...)     printf(format, ## __VA_ARGS__)
37 50d8ff8b Isaku Yamahata
#else
38 50d8ff8b Isaku Yamahata
# define PIIX4_DPRINTF(format, ...)     do { } while (0)
39 50d8ff8b Isaku Yamahata
#endif
40 50d8ff8b Isaku Yamahata
41 ac404095 Isaku Yamahata
#define GPE_BASE 0xafe0
42 23910d3f Isaku Yamahata
#define GPE_LEN 4
43 c177684c Gerd Hoffmann
44 c177684c Gerd Hoffmann
#define PCI_HOTPLUG_ADDR 0xae00
45 c177684c Gerd Hoffmann
#define PCI_HOTPLUG_SIZE 0x000f
46 ba737541 Alex Williamson
#define PCI_UP_BASE 0xae00
47 ba737541 Alex Williamson
#define PCI_DOWN_BASE 0xae04
48 ac404095 Isaku Yamahata
#define PCI_EJ_BASE 0xae08
49 668643b0 Marcelo Tosatti
#define PCI_RMV_BASE 0xae0c
50 ac404095 Isaku Yamahata
51 4441a287 Gleb Natapov
#define PIIX4_PCI_HOTPLUG_STATUS 2
52 4441a287 Gleb Natapov
53 ac404095 Isaku Yamahata
struct pci_status {
54 7faa8075 Alex Williamson
    uint32_t up; /* deprecated, maintained for migration compatibility */
55 ac404095 Isaku Yamahata
    uint32_t down;
56 ac404095 Isaku Yamahata
};
57 ac404095 Isaku Yamahata
58 93d89f63 Isaku Yamahata
typedef struct PIIX4PMState {
59 93d89f63 Isaku Yamahata
    PCIDevice dev;
60 56e5b2a1 Gerd Hoffmann
61 af11110b Gerd Hoffmann
    MemoryRegion io;
62 b65b93f2 Gerd Hoffmann
    MemoryRegion io_gpe;
63 c177684c Gerd Hoffmann
    MemoryRegion io_pci;
64 355bf2e5 Gerd Hoffmann
    ACPIREGS ar;
65 93d89f63 Isaku Yamahata
66 93d89f63 Isaku Yamahata
    APMState apm;
67 93d89f63 Isaku Yamahata
68 93d89f63 Isaku Yamahata
    PMSMBus smb;
69 e8ec0571 Isaku Yamahata
    uint32_t smb_io_base;
70 93d89f63 Isaku Yamahata
71 93d89f63 Isaku Yamahata
    qemu_irq irq;
72 93d89f63 Isaku Yamahata
    qemu_irq smi_irq;
73 93d89f63 Isaku Yamahata
    int kvm_enabled;
74 6141dbfe Paolo Bonzini
    Notifier machine_ready;
75 d010f91c Igor Mammedov
    Notifier powerdown_notifier;
76 ac404095 Isaku Yamahata
77 ac404095 Isaku Yamahata
    /* for pci hotplug */
78 ac404095 Isaku Yamahata
    struct pci_status pci0_status;
79 668643b0 Marcelo Tosatti
    uint32_t pci0_hotplug_enable;
80 7faa8075 Alex Williamson
    uint32_t pci0_slot_device_present;
81 459ae5ea Gleb Natapov
82 459ae5ea Gleb Natapov
    uint8_t disable_s3;
83 459ae5ea Gleb Natapov
    uint8_t disable_s4;
84 459ae5ea Gleb Natapov
    uint8_t s4_val;
85 93d89f63 Isaku Yamahata
} PIIX4PMState;
86 93d89f63 Isaku Yamahata
87 56e5b2a1 Gerd Hoffmann
static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
88 56e5b2a1 Gerd Hoffmann
                                           PCIBus *bus, PIIX4PMState *s);
89 ac404095 Isaku Yamahata
90 93d89f63 Isaku Yamahata
#define ACPI_ENABLE 0xf1
91 93d89f63 Isaku Yamahata
#define ACPI_DISABLE 0xf0
92 93d89f63 Isaku Yamahata
93 93d89f63 Isaku Yamahata
static void pm_update_sci(PIIX4PMState *s)
94 93d89f63 Isaku Yamahata
{
95 93d89f63 Isaku Yamahata
    int sci_level, pmsts;
96 93d89f63 Isaku Yamahata
97 2886be1b Gerd Hoffmann
    pmsts = acpi_pm1_evt_get_sts(&s->ar);
98 355bf2e5 Gerd Hoffmann
    sci_level = (((pmsts & s->ar.pm1.evt.en) &
99 93d89f63 Isaku Yamahata
                  (ACPI_BITMASK_RT_CLOCK_ENABLE |
100 93d89f63 Isaku Yamahata
                   ACPI_BITMASK_POWER_BUTTON_ENABLE |
101 93d89f63 Isaku Yamahata
                   ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
102 633aa0ac Gleb Natapov
                   ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
103 355bf2e5 Gerd Hoffmann
        (((s->ar.gpe.sts[0] & s->ar.gpe.en[0])
104 355bf2e5 Gerd Hoffmann
          & PIIX4_PCI_HOTPLUG_STATUS) != 0);
105 633aa0ac Gleb Natapov
106 93d89f63 Isaku Yamahata
    qemu_set_irq(s->irq, sci_level);
107 93d89f63 Isaku Yamahata
    /* schedule a timer interruption if needed */
108 355bf2e5 Gerd Hoffmann
    acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
109 a54d41a8 Isaku Yamahata
                       !(pmsts & ACPI_BITMASK_TIMER_STATUS));
110 93d89f63 Isaku Yamahata
}
111 93d89f63 Isaku Yamahata
112 355bf2e5 Gerd Hoffmann
static void pm_tmr_timer(ACPIREGS *ar)
113 93d89f63 Isaku Yamahata
{
114 355bf2e5 Gerd Hoffmann
    PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
115 93d89f63 Isaku Yamahata
    pm_update_sci(s);
116 93d89f63 Isaku Yamahata
}
117 93d89f63 Isaku Yamahata
118 93d89f63 Isaku Yamahata
static void apm_ctrl_changed(uint32_t val, void *arg)
119 93d89f63 Isaku Yamahata
{
120 93d89f63 Isaku Yamahata
    PIIX4PMState *s = arg;
121 93d89f63 Isaku Yamahata
122 93d89f63 Isaku Yamahata
    /* ACPI specs 3.0, 4.7.2.5 */
123 355bf2e5 Gerd Hoffmann
    acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
124 93d89f63 Isaku Yamahata
125 93d89f63 Isaku Yamahata
    if (s->dev.config[0x5b] & (1 << 1)) {
126 93d89f63 Isaku Yamahata
        if (s->smi_irq) {
127 93d89f63 Isaku Yamahata
            qemu_irq_raise(s->smi_irq);
128 93d89f63 Isaku Yamahata
        }
129 93d89f63 Isaku Yamahata
    }
130 93d89f63 Isaku Yamahata
}
131 93d89f63 Isaku Yamahata
132 93d89f63 Isaku Yamahata
static void pm_io_space_update(PIIX4PMState *s)
133 93d89f63 Isaku Yamahata
{
134 93d89f63 Isaku Yamahata
    uint32_t pm_io_base;
135 93d89f63 Isaku Yamahata
136 af11110b Gerd Hoffmann
    pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
137 af11110b Gerd Hoffmann
    pm_io_base &= 0xffc0;
138 93d89f63 Isaku Yamahata
139 af11110b Gerd Hoffmann
    memory_region_transaction_begin();
140 af11110b Gerd Hoffmann
    memory_region_set_enabled(&s->io, s->dev.config[0x80] & 1);
141 af11110b Gerd Hoffmann
    memory_region_set_address(&s->io, pm_io_base);
142 af11110b Gerd Hoffmann
    memory_region_transaction_commit();
143 93d89f63 Isaku Yamahata
}
144 93d89f63 Isaku Yamahata
145 24fe083d Gerd Hoffmann
static void smbus_io_space_update(PIIX4PMState *s)
146 24fe083d Gerd Hoffmann
{
147 24fe083d Gerd Hoffmann
    s->smb_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x90));
148 24fe083d Gerd Hoffmann
    s->smb_io_base &= 0xffc0;
149 24fe083d Gerd Hoffmann
150 24fe083d Gerd Hoffmann
    memory_region_transaction_begin();
151 24fe083d Gerd Hoffmann
    memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & 1);
152 24fe083d Gerd Hoffmann
    memory_region_set_address(&s->smb.io, s->smb_io_base);
153 24fe083d Gerd Hoffmann
    memory_region_transaction_commit();
154 93d89f63 Isaku Yamahata
}
155 93d89f63 Isaku Yamahata
156 93d89f63 Isaku Yamahata
static void pm_write_config(PCIDevice *d,
157 93d89f63 Isaku Yamahata
                            uint32_t address, uint32_t val, int len)
158 93d89f63 Isaku Yamahata
{
159 93d89f63 Isaku Yamahata
    pci_default_write_config(d, address, val, len);
160 24fe083d Gerd Hoffmann
    if (range_covers_byte(address, len, 0x80) ||
161 24fe083d Gerd Hoffmann
        ranges_overlap(address, len, 0x40, 4)) {
162 93d89f63 Isaku Yamahata
        pm_io_space_update((PIIX4PMState *)d);
163 24fe083d Gerd Hoffmann
    }
164 24fe083d Gerd Hoffmann
    if (range_covers_byte(address, len, 0xd2) ||
165 24fe083d Gerd Hoffmann
        ranges_overlap(address, len, 0x90, 4)) {
166 24fe083d Gerd Hoffmann
        smbus_io_space_update((PIIX4PMState *)d);
167 24fe083d Gerd Hoffmann
    }
168 93d89f63 Isaku Yamahata
}
169 93d89f63 Isaku Yamahata
170 7faa8075 Alex Williamson
static void vmstate_pci_status_pre_save(void *opaque)
171 7faa8075 Alex Williamson
{
172 7faa8075 Alex Williamson
    struct pci_status *pci0_status = opaque;
173 7faa8075 Alex Williamson
    PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
174 7faa8075 Alex Williamson
175 7faa8075 Alex Williamson
    /* We no longer track up, so build a safe value for migrating
176 7faa8075 Alex Williamson
     * to a version that still does... of course these might get lost
177 7faa8075 Alex Williamson
     * by an old buggy implementation, but we try. */
178 7faa8075 Alex Williamson
    pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
179 7faa8075 Alex Williamson
}
180 7faa8075 Alex Williamson
181 93d89f63 Isaku Yamahata
static int vmstate_acpi_post_load(void *opaque, int version_id)
182 93d89f63 Isaku Yamahata
{
183 93d89f63 Isaku Yamahata
    PIIX4PMState *s = opaque;
184 93d89f63 Isaku Yamahata
185 93d89f63 Isaku Yamahata
    pm_io_space_update(s);
186 93d89f63 Isaku Yamahata
    return 0;
187 93d89f63 Isaku Yamahata
}
188 93d89f63 Isaku Yamahata
189 23910d3f Isaku Yamahata
#define VMSTATE_GPE_ARRAY(_field, _state)                            \
190 23910d3f Isaku Yamahata
 {                                                                   \
191 23910d3f Isaku Yamahata
     .name       = (stringify(_field)),                              \
192 23910d3f Isaku Yamahata
     .version_id = 0,                                                \
193 23910d3f Isaku Yamahata
     .info       = &vmstate_info_uint16,                             \
194 23910d3f Isaku Yamahata
     .size       = sizeof(uint16_t),                                 \
195 b0b873a0 Marcelo Tosatti
     .flags      = VMS_SINGLE | VMS_POINTER,                         \
196 23910d3f Isaku Yamahata
     .offset     = vmstate_offset_pointer(_state, _field, uint8_t),  \
197 23910d3f Isaku Yamahata
 }
198 23910d3f Isaku Yamahata
199 4cf3e6f3 Alex Williamson
static const VMStateDescription vmstate_gpe = {
200 4cf3e6f3 Alex Williamson
    .name = "gpe",
201 4cf3e6f3 Alex Williamson
    .version_id = 1,
202 4cf3e6f3 Alex Williamson
    .minimum_version_id = 1,
203 4cf3e6f3 Alex Williamson
    .minimum_version_id_old = 1,
204 4cf3e6f3 Alex Williamson
    .fields      = (VMStateField []) {
205 23910d3f Isaku Yamahata
        VMSTATE_GPE_ARRAY(sts, ACPIGPE),
206 23910d3f Isaku Yamahata
        VMSTATE_GPE_ARRAY(en, ACPIGPE),
207 4cf3e6f3 Alex Williamson
        VMSTATE_END_OF_LIST()
208 4cf3e6f3 Alex Williamson
    }
209 4cf3e6f3 Alex Williamson
};
210 4cf3e6f3 Alex Williamson
211 4cf3e6f3 Alex Williamson
static const VMStateDescription vmstate_pci_status = {
212 4cf3e6f3 Alex Williamson
    .name = "pci_status",
213 4cf3e6f3 Alex Williamson
    .version_id = 1,
214 4cf3e6f3 Alex Williamson
    .minimum_version_id = 1,
215 4cf3e6f3 Alex Williamson
    .minimum_version_id_old = 1,
216 7faa8075 Alex Williamson
    .pre_save = vmstate_pci_status_pre_save,
217 4cf3e6f3 Alex Williamson
    .fields      = (VMStateField []) {
218 4cf3e6f3 Alex Williamson
        VMSTATE_UINT32(up, struct pci_status),
219 4cf3e6f3 Alex Williamson
        VMSTATE_UINT32(down, struct pci_status),
220 4cf3e6f3 Alex Williamson
        VMSTATE_END_OF_LIST()
221 4cf3e6f3 Alex Williamson
    }
222 4cf3e6f3 Alex Williamson
};
223 4cf3e6f3 Alex Williamson
224 b0b873a0 Marcelo Tosatti
static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
225 b0b873a0 Marcelo Tosatti
{
226 b0b873a0 Marcelo Tosatti
    PIIX4PMState *s = opaque;
227 b0b873a0 Marcelo Tosatti
    int ret, i;
228 b0b873a0 Marcelo Tosatti
    uint16_t temp;
229 b0b873a0 Marcelo Tosatti
230 b0b873a0 Marcelo Tosatti
    ret = pci_device_load(&s->dev, f);
231 b0b873a0 Marcelo Tosatti
    if (ret < 0) {
232 b0b873a0 Marcelo Tosatti
        return ret;
233 b0b873a0 Marcelo Tosatti
    }
234 b0b873a0 Marcelo Tosatti
    qemu_get_be16s(f, &s->ar.pm1.evt.sts);
235 b0b873a0 Marcelo Tosatti
    qemu_get_be16s(f, &s->ar.pm1.evt.en);
236 b0b873a0 Marcelo Tosatti
    qemu_get_be16s(f, &s->ar.pm1.cnt.cnt);
237 b0b873a0 Marcelo Tosatti
238 b0b873a0 Marcelo Tosatti
    ret = vmstate_load_state(f, &vmstate_apm, opaque, 1);
239 b0b873a0 Marcelo Tosatti
    if (ret) {
240 b0b873a0 Marcelo Tosatti
        return ret;
241 b0b873a0 Marcelo Tosatti
    }
242 b0b873a0 Marcelo Tosatti
243 b0b873a0 Marcelo Tosatti
    qemu_get_timer(f, s->ar.tmr.timer);
244 b0b873a0 Marcelo Tosatti
    qemu_get_sbe64s(f, &s->ar.tmr.overflow_time);
245 b0b873a0 Marcelo Tosatti
246 b0b873a0 Marcelo Tosatti
    qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts);
247 b0b873a0 Marcelo Tosatti
    for (i = 0; i < 3; i++) {
248 b0b873a0 Marcelo Tosatti
        qemu_get_be16s(f, &temp);
249 b0b873a0 Marcelo Tosatti
    }
250 b0b873a0 Marcelo Tosatti
251 b0b873a0 Marcelo Tosatti
    qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en);
252 b0b873a0 Marcelo Tosatti
    for (i = 0; i < 3; i++) {
253 b0b873a0 Marcelo Tosatti
        qemu_get_be16s(f, &temp);
254 b0b873a0 Marcelo Tosatti
    }
255 b0b873a0 Marcelo Tosatti
256 b0b873a0 Marcelo Tosatti
    ret = vmstate_load_state(f, &vmstate_pci_status, opaque, 1);
257 b0b873a0 Marcelo Tosatti
    return ret;
258 b0b873a0 Marcelo Tosatti
}
259 b0b873a0 Marcelo Tosatti
260 b0b873a0 Marcelo Tosatti
/* qemu-kvm 1.2 uses version 3 but advertised as 2
261 b0b873a0 Marcelo Tosatti
 * To support incoming qemu-kvm 1.2 migration, change version_id
262 b0b873a0 Marcelo Tosatti
 * and minimum_version_id to 2 below (which breaks migration from
263 b0b873a0 Marcelo Tosatti
 * qemu 1.2).
264 b0b873a0 Marcelo Tosatti
 *
265 b0b873a0 Marcelo Tosatti
 */
266 93d89f63 Isaku Yamahata
static const VMStateDescription vmstate_acpi = {
267 93d89f63 Isaku Yamahata
    .name = "piix4_pm",
268 b0b873a0 Marcelo Tosatti
    .version_id = 3,
269 b0b873a0 Marcelo Tosatti
    .minimum_version_id = 3,
270 93d89f63 Isaku Yamahata
    .minimum_version_id_old = 1,
271 b0b873a0 Marcelo Tosatti
    .load_state_old = acpi_load_old,
272 93d89f63 Isaku Yamahata
    .post_load = vmstate_acpi_post_load,
273 93d89f63 Isaku Yamahata
    .fields      = (VMStateField []) {
274 93d89f63 Isaku Yamahata
        VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
275 355bf2e5 Gerd Hoffmann
        VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
276 355bf2e5 Gerd Hoffmann
        VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
277 355bf2e5 Gerd Hoffmann
        VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
278 93d89f63 Isaku Yamahata
        VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
279 355bf2e5 Gerd Hoffmann
        VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
280 355bf2e5 Gerd Hoffmann
        VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
281 355bf2e5 Gerd Hoffmann
        VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
282 4cf3e6f3 Alex Williamson
        VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
283 4cf3e6f3 Alex Williamson
                       struct pci_status),
284 93d89f63 Isaku Yamahata
        VMSTATE_END_OF_LIST()
285 93d89f63 Isaku Yamahata
    }
286 93d89f63 Isaku Yamahata
};
287 93d89f63 Isaku Yamahata
288 7faa8075 Alex Williamson
static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
289 7faa8075 Alex Williamson
{
290 0866aca1 Anthony Liguori
    BusChild *kid, *next;
291 7faa8075 Alex Williamson
    BusState *bus = qdev_get_parent_bus(&s->dev.qdev);
292 7faa8075 Alex Williamson
    int slot = ffs(slots) - 1;
293 54bfa546 Michael S. Tsirkin
    bool slot_free = true;
294 7faa8075 Alex Williamson
295 7faa8075 Alex Williamson
    /* Mark request as complete */
296 7faa8075 Alex Williamson
    s->pci0_status.down &= ~(1U << slot);
297 7faa8075 Alex Williamson
298 0866aca1 Anthony Liguori
    QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
299 0866aca1 Anthony Liguori
        DeviceState *qdev = kid->child;
300 7faa8075 Alex Williamson
        PCIDevice *dev = PCI_DEVICE(qdev);
301 7faa8075 Alex Williamson
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
302 54bfa546 Michael S. Tsirkin
        if (PCI_SLOT(dev->devfn) == slot) {
303 54bfa546 Michael S. Tsirkin
            if (pc->no_hotplug) {
304 54bfa546 Michael S. Tsirkin
                slot_free = false;
305 54bfa546 Michael S. Tsirkin
            } else {
306 54bfa546 Michael S. Tsirkin
                qdev_free(qdev);
307 54bfa546 Michael S. Tsirkin
            }
308 7faa8075 Alex Williamson
        }
309 7faa8075 Alex Williamson
    }
310 54bfa546 Michael S. Tsirkin
    if (slot_free) {
311 54bfa546 Michael S. Tsirkin
        s->pci0_slot_device_present &= ~(1U << slot);
312 54bfa546 Michael S. Tsirkin
    }
313 7faa8075 Alex Williamson
}
314 7faa8075 Alex Williamson
315 668643b0 Marcelo Tosatti
static void piix4_update_hotplug(PIIX4PMState *s)
316 668643b0 Marcelo Tosatti
{
317 668643b0 Marcelo Tosatti
    PCIDevice *dev = &s->dev;
318 668643b0 Marcelo Tosatti
    BusState *bus = qdev_get_parent_bus(&dev->qdev);
319 0866aca1 Anthony Liguori
    BusChild *kid, *next;
320 668643b0 Marcelo Tosatti
321 7faa8075 Alex Williamson
    /* Execute any pending removes during reset */
322 7faa8075 Alex Williamson
    while (s->pci0_status.down) {
323 7faa8075 Alex Williamson
        acpi_piix_eject_slot(s, s->pci0_status.down);
324 7faa8075 Alex Williamson
    }
325 7faa8075 Alex Williamson
326 668643b0 Marcelo Tosatti
    s->pci0_hotplug_enable = ~0;
327 7faa8075 Alex Williamson
    s->pci0_slot_device_present = 0;
328 668643b0 Marcelo Tosatti
329 0866aca1 Anthony Liguori
    QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
330 0866aca1 Anthony Liguori
        DeviceState *qdev = kid->child;
331 40021f08 Anthony Liguori
        PCIDevice *pdev = PCI_DEVICE(qdev);
332 40021f08 Anthony Liguori
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
333 668643b0 Marcelo Tosatti
        int slot = PCI_SLOT(pdev->devfn);
334 668643b0 Marcelo Tosatti
335 40021f08 Anthony Liguori
        if (pc->no_hotplug) {
336 7faa8075 Alex Williamson
            s->pci0_hotplug_enable &= ~(1U << slot);
337 668643b0 Marcelo Tosatti
        }
338 7faa8075 Alex Williamson
339 7faa8075 Alex Williamson
        s->pci0_slot_device_present |= (1U << slot);
340 668643b0 Marcelo Tosatti
    }
341 668643b0 Marcelo Tosatti
}
342 668643b0 Marcelo Tosatti
343 93d89f63 Isaku Yamahata
static void piix4_reset(void *opaque)
344 93d89f63 Isaku Yamahata
{
345 93d89f63 Isaku Yamahata
    PIIX4PMState *s = opaque;
346 93d89f63 Isaku Yamahata
    uint8_t *pci_conf = s->dev.config;
347 93d89f63 Isaku Yamahata
348 93d89f63 Isaku Yamahata
    pci_conf[0x58] = 0;
349 93d89f63 Isaku Yamahata
    pci_conf[0x59] = 0;
350 93d89f63 Isaku Yamahata
    pci_conf[0x5a] = 0;
351 93d89f63 Isaku Yamahata
    pci_conf[0x5b] = 0;
352 93d89f63 Isaku Yamahata
353 4d09d37c Gleb Natapov
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
354 4d09d37c Gleb Natapov
    pci_conf[0x80] = 0;
355 4d09d37c Gleb Natapov
356 93d89f63 Isaku Yamahata
    if (s->kvm_enabled) {
357 93d89f63 Isaku Yamahata
        /* Mark SMM as already inited (until KVM supports SMM). */
358 93d89f63 Isaku Yamahata
        pci_conf[0x5B] = 0x02;
359 93d89f63 Isaku Yamahata
    }
360 668643b0 Marcelo Tosatti
    piix4_update_hotplug(s);
361 93d89f63 Isaku Yamahata
}
362 93d89f63 Isaku Yamahata
363 d010f91c Igor Mammedov
static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
364 93d89f63 Isaku Yamahata
{
365 d010f91c Igor Mammedov
    PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
366 93d89f63 Isaku Yamahata
367 355bf2e5 Gerd Hoffmann
    assert(s != NULL);
368 355bf2e5 Gerd Hoffmann
    acpi_pm1_evt_power_down(&s->ar);
369 93d89f63 Isaku Yamahata
}
370 93d89f63 Isaku Yamahata
371 9e8dd451 Jan Kiszka
static void piix4_pm_machine_ready(Notifier *n, void *opaque)
372 6141dbfe Paolo Bonzini
{
373 6141dbfe Paolo Bonzini
    PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
374 6141dbfe Paolo Bonzini
    uint8_t *pci_conf;
375 6141dbfe Paolo Bonzini
376 6141dbfe Paolo Bonzini
    pci_conf = s->dev.config;
377 6141dbfe Paolo Bonzini
    pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
378 6141dbfe Paolo Bonzini
    pci_conf[0x63] = 0x60;
379 6141dbfe Paolo Bonzini
    pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
380 6141dbfe Paolo Bonzini
        (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
381 6141dbfe Paolo Bonzini
382 6141dbfe Paolo Bonzini
}
383 6141dbfe Paolo Bonzini
384 e8ec0571 Isaku Yamahata
static int piix4_pm_initfn(PCIDevice *dev)
385 93d89f63 Isaku Yamahata
{
386 e8ec0571 Isaku Yamahata
    PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
387 93d89f63 Isaku Yamahata
    uint8_t *pci_conf;
388 93d89f63 Isaku Yamahata
389 93d89f63 Isaku Yamahata
    pci_conf = s->dev.config;
390 93d89f63 Isaku Yamahata
    pci_conf[0x06] = 0x80;
391 93d89f63 Isaku Yamahata
    pci_conf[0x07] = 0x02;
392 93d89f63 Isaku Yamahata
    pci_conf[0x09] = 0x00;
393 93d89f63 Isaku Yamahata
    pci_conf[0x3d] = 0x01; // interrupt pin 1
394 93d89f63 Isaku Yamahata
395 93d89f63 Isaku Yamahata
    /* APM */
396 42d8a3cf Julien Grall
    apm_init(dev, &s->apm, apm_ctrl_changed, s);
397 93d89f63 Isaku Yamahata
398 93d89f63 Isaku Yamahata
    if (s->kvm_enabled) {
399 93d89f63 Isaku Yamahata
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
400 93d89f63 Isaku Yamahata
         * support SMM mode. */
401 93d89f63 Isaku Yamahata
        pci_conf[0x5B] = 0x02;
402 93d89f63 Isaku Yamahata
    }
403 93d89f63 Isaku Yamahata
404 93d89f63 Isaku Yamahata
    /* XXX: which specification is used ? The i82731AB has different
405 93d89f63 Isaku Yamahata
       mappings */
406 e8ec0571 Isaku Yamahata
    pci_conf[0x90] = s->smb_io_base | 1;
407 e8ec0571 Isaku Yamahata
    pci_conf[0x91] = s->smb_io_base >> 8;
408 93d89f63 Isaku Yamahata
    pci_conf[0xd2] = 0x09;
409 798512e5 Gerd Hoffmann
    pm_smbus_init(&s->dev.qdev, &s->smb);
410 24fe083d Gerd Hoffmann
    memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1);
411 56e5b2a1 Gerd Hoffmann
    memory_region_add_subregion(pci_address_space_io(dev),
412 56e5b2a1 Gerd Hoffmann
                                s->smb_io_base, &s->smb.io);
413 93d89f63 Isaku Yamahata
414 ca5d64b4 Gerd Hoffmann
    memory_region_init(&s->io, "piix4-pm", 64);
415 af11110b Gerd Hoffmann
    memory_region_set_enabled(&s->io, false);
416 56e5b2a1 Gerd Hoffmann
    memory_region_add_subregion(pci_address_space_io(dev),
417 56e5b2a1 Gerd Hoffmann
                                0, &s->io);
418 93d89f63 Isaku Yamahata
419 77d58b1e Gerd Hoffmann
    acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
420 b5a7c024 Gerd Hoffmann
    acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
421 afafe4bb Gerd Hoffmann
    acpi_pm1_cnt_init(&s->ar, &s->io);
422 355bf2e5 Gerd Hoffmann
    acpi_gpe_init(&s->ar, GPE_LEN);
423 93d89f63 Isaku Yamahata
424 d010f91c Igor Mammedov
    s->powerdown_notifier.notify = piix4_pm_powerdown_req;
425 d010f91c Igor Mammedov
    qemu_register_powerdown_notifier(&s->powerdown_notifier);
426 93d89f63 Isaku Yamahata
427 6141dbfe Paolo Bonzini
    s->machine_ready.notify = piix4_pm_machine_ready;
428 6141dbfe Paolo Bonzini
    qemu_add_machine_init_done_notifier(&s->machine_ready);
429 e8ec0571 Isaku Yamahata
    qemu_register_reset(piix4_reset, s);
430 56e5b2a1 Gerd Hoffmann
431 56e5b2a1 Gerd Hoffmann
    piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s);
432 e8ec0571 Isaku Yamahata
433 e8ec0571 Isaku Yamahata
    return 0;
434 e8ec0571 Isaku Yamahata
}
435 e8ec0571 Isaku Yamahata
436 e8ec0571 Isaku Yamahata
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
437 da98c8eb Gerd Hoffmann
                       qemu_irq sci_irq, qemu_irq smi_irq,
438 459ae5ea Gleb Natapov
                       int kvm_enabled, void *fw_cfg)
439 e8ec0571 Isaku Yamahata
{
440 e8ec0571 Isaku Yamahata
    PCIDevice *dev;
441 e8ec0571 Isaku Yamahata
    PIIX4PMState *s;
442 e8ec0571 Isaku Yamahata
443 e8ec0571 Isaku Yamahata
    dev = pci_create(bus, devfn, "PIIX4_PM");
444 e8ec0571 Isaku Yamahata
    qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
445 93d89f63 Isaku Yamahata
446 e8ec0571 Isaku Yamahata
    s = DO_UPCAST(PIIX4PMState, dev, dev);
447 93d89f63 Isaku Yamahata
    s->irq = sci_irq;
448 93d89f63 Isaku Yamahata
    s->smi_irq = smi_irq;
449 e8ec0571 Isaku Yamahata
    s->kvm_enabled = kvm_enabled;
450 e8ec0571 Isaku Yamahata
451 e8ec0571 Isaku Yamahata
    qdev_init_nofail(&dev->qdev);
452 93d89f63 Isaku Yamahata
453 459ae5ea Gleb Natapov
    if (fw_cfg) {
454 459ae5ea Gleb Natapov
        uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
455 459ae5ea Gleb Natapov
        suspend[3] = 1 | ((!s->disable_s3) << 7);
456 459ae5ea Gleb Natapov
        suspend[4] = s->s4_val | ((!s->disable_s4) << 7);
457 459ae5ea Gleb Natapov
458 459ae5ea Gleb Natapov
        fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6);
459 459ae5ea Gleb Natapov
    }
460 459ae5ea Gleb Natapov
461 93d89f63 Isaku Yamahata
    return s->smb.smbus;
462 93d89f63 Isaku Yamahata
}
463 93d89f63 Isaku Yamahata
464 40021f08 Anthony Liguori
static Property piix4_pm_properties[] = {
465 40021f08 Anthony Liguori
    DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
466 459ae5ea Gleb Natapov
    DEFINE_PROP_UINT8("disable_s3", PIIX4PMState, disable_s3, 0),
467 459ae5ea Gleb Natapov
    DEFINE_PROP_UINT8("disable_s4", PIIX4PMState, disable_s4, 0),
468 459ae5ea Gleb Natapov
    DEFINE_PROP_UINT8("s4_val", PIIX4PMState, s4_val, 2),
469 40021f08 Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
470 40021f08 Anthony Liguori
};
471 40021f08 Anthony Liguori
472 40021f08 Anthony Liguori
static void piix4_pm_class_init(ObjectClass *klass, void *data)
473 40021f08 Anthony Liguori
{
474 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
475 40021f08 Anthony Liguori
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
476 40021f08 Anthony Liguori
477 40021f08 Anthony Liguori
    k->no_hotplug = 1;
478 40021f08 Anthony Liguori
    k->init = piix4_pm_initfn;
479 40021f08 Anthony Liguori
    k->config_write = pm_write_config;
480 40021f08 Anthony Liguori
    k->vendor_id = PCI_VENDOR_ID_INTEL;
481 40021f08 Anthony Liguori
    k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
482 40021f08 Anthony Liguori
    k->revision = 0x03;
483 40021f08 Anthony Liguori
    k->class_id = PCI_CLASS_BRIDGE_OTHER;
484 39bffca2 Anthony Liguori
    dc->desc = "PM";
485 39bffca2 Anthony Liguori
    dc->no_user = 1;
486 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_acpi;
487 39bffca2 Anthony Liguori
    dc->props = piix4_pm_properties;
488 40021f08 Anthony Liguori
}
489 40021f08 Anthony Liguori
490 39bffca2 Anthony Liguori
static TypeInfo piix4_pm_info = {
491 39bffca2 Anthony Liguori
    .name          = "PIIX4_PM",
492 39bffca2 Anthony Liguori
    .parent        = TYPE_PCI_DEVICE,
493 39bffca2 Anthony Liguori
    .instance_size = sizeof(PIIX4PMState),
494 39bffca2 Anthony Liguori
    .class_init    = piix4_pm_class_init,
495 e8ec0571 Isaku Yamahata
};
496 e8ec0571 Isaku Yamahata
497 83f7d43a Andreas Fรคrber
static void piix4_pm_register_types(void)
498 e8ec0571 Isaku Yamahata
{
499 39bffca2 Anthony Liguori
    type_register_static(&piix4_pm_info);
500 e8ec0571 Isaku Yamahata
}
501 e8ec0571 Isaku Yamahata
502 83f7d43a Andreas Fรคrber
type_init(piix4_pm_register_types)
503 e8ec0571 Isaku Yamahata
504 b65b93f2 Gerd Hoffmann
static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width)
505 93d89f63 Isaku Yamahata
{
506 633aa0ac Gleb Natapov
    PIIX4PMState *s = opaque;
507 355bf2e5 Gerd Hoffmann
    uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
508 93d89f63 Isaku Yamahata
509 50d8ff8b Isaku Yamahata
    PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
510 93d89f63 Isaku Yamahata
    return val;
511 93d89f63 Isaku Yamahata
}
512 93d89f63 Isaku Yamahata
513 b65b93f2 Gerd Hoffmann
static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val,
514 b65b93f2 Gerd Hoffmann
                       unsigned width)
515 93d89f63 Isaku Yamahata
{
516 633aa0ac Gleb Natapov
    PIIX4PMState *s = opaque;
517 633aa0ac Gleb Natapov
518 355bf2e5 Gerd Hoffmann
    acpi_gpe_ioport_writeb(&s->ar, addr, val);
519 633aa0ac Gleb Natapov
    pm_update_sci(s);
520 93d89f63 Isaku Yamahata
521 50d8ff8b Isaku Yamahata
    PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
522 93d89f63 Isaku Yamahata
}
523 93d89f63 Isaku Yamahata
524 b65b93f2 Gerd Hoffmann
static const MemoryRegionOps piix4_gpe_ops = {
525 b65b93f2 Gerd Hoffmann
    .read = gpe_readb,
526 b65b93f2 Gerd Hoffmann
    .write = gpe_writeb,
527 b65b93f2 Gerd Hoffmann
    .valid.min_access_size = 1,
528 b65b93f2 Gerd Hoffmann
    .valid.max_access_size = 4,
529 b65b93f2 Gerd Hoffmann
    .impl.min_access_size = 1,
530 b65b93f2 Gerd Hoffmann
    .impl.max_access_size = 1,
531 b65b93f2 Gerd Hoffmann
    .endianness = DEVICE_LITTLE_ENDIAN,
532 b65b93f2 Gerd Hoffmann
};
533 b65b93f2 Gerd Hoffmann
534 ba737541 Alex Williamson
static uint32_t pci_up_read(void *opaque, uint32_t addr)
535 93d89f63 Isaku Yamahata
{
536 ba737541 Alex Williamson
    PIIX4PMState *s = opaque;
537 7faa8075 Alex Williamson
    uint32_t val;
538 7faa8075 Alex Williamson
539 7faa8075 Alex Williamson
    /* Manufacture an "up" value to cause a device check on any hotplug
540 7faa8075 Alex Williamson
     * slot with a device.  Extra device checks are harmless. */
541 7faa8075 Alex Williamson
    val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
542 93d89f63 Isaku Yamahata
543 ba737541 Alex Williamson
    PIIX4_DPRINTF("pci_up_read %x\n", val);
544 93d89f63 Isaku Yamahata
    return val;
545 93d89f63 Isaku Yamahata
}
546 93d89f63 Isaku Yamahata
547 ba737541 Alex Williamson
static uint32_t pci_down_read(void *opaque, uint32_t addr)
548 93d89f63 Isaku Yamahata
{
549 ba737541 Alex Williamson
    PIIX4PMState *s = opaque;
550 ba737541 Alex Williamson
    uint32_t val = s->pci0_status.down;
551 ba737541 Alex Williamson
552 ba737541 Alex Williamson
    PIIX4_DPRINTF("pci_down_read %x\n", val);
553 ba737541 Alex Williamson
    return val;
554 93d89f63 Isaku Yamahata
}
555 93d89f63 Isaku Yamahata
556 9290f364 Alex Williamson
static uint32_t pci_features_read(void *opaque, uint32_t addr)
557 93d89f63 Isaku Yamahata
{
558 9290f364 Alex Williamson
    /* No feature defined yet */
559 9290f364 Alex Williamson
    PIIX4_DPRINTF("pci_features_read %x\n", 0);
560 93d89f63 Isaku Yamahata
    return 0;
561 93d89f63 Isaku Yamahata
}
562 93d89f63 Isaku Yamahata
563 93d89f63 Isaku Yamahata
static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
564 93d89f63 Isaku Yamahata
{
565 7faa8075 Alex Williamson
    acpi_piix_eject_slot(opaque, val);
566 93d89f63 Isaku Yamahata
567 50d8ff8b Isaku Yamahata
    PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
568 93d89f63 Isaku Yamahata
}
569 93d89f63 Isaku Yamahata
570 668643b0 Marcelo Tosatti
static uint32_t pcirmv_read(void *opaque, uint32_t addr)
571 668643b0 Marcelo Tosatti
{
572 668643b0 Marcelo Tosatti
    PIIX4PMState *s = opaque;
573 668643b0 Marcelo Tosatti
574 668643b0 Marcelo Tosatti
    return s->pci0_hotplug_enable;
575 668643b0 Marcelo Tosatti
}
576 668643b0 Marcelo Tosatti
577 c177684c Gerd Hoffmann
static const MemoryRegionOps piix4_pci_ops = {
578 c177684c Gerd Hoffmann
    .old_portio = (MemoryRegionPortio[]) {
579 c177684c Gerd Hoffmann
        {
580 c177684c Gerd Hoffmann
            .offset = PCI_UP_BASE - PCI_HOTPLUG_ADDR,   .len = 4, .size = 4,
581 c177684c Gerd Hoffmann
            .read = pci_up_read,
582 c177684c Gerd Hoffmann
        },{
583 c177684c Gerd Hoffmann
            .offset = PCI_DOWN_BASE - PCI_HOTPLUG_ADDR, .len = 4, .size = 4,
584 c177684c Gerd Hoffmann
            .read = pci_down_read,
585 c177684c Gerd Hoffmann
        },{
586 c177684c Gerd Hoffmann
            .offset = PCI_EJ_BASE - PCI_HOTPLUG_ADDR,   .len = 4, .size = 4,
587 c177684c Gerd Hoffmann
            .read = pci_features_read,
588 c177684c Gerd Hoffmann
            .write = pciej_write,
589 c177684c Gerd Hoffmann
        },{
590 c177684c Gerd Hoffmann
            .offset = PCI_RMV_BASE - PCI_HOTPLUG_ADDR,  .len = 4, .size = 4,
591 c177684c Gerd Hoffmann
            .read = pcirmv_read,
592 c177684c Gerd Hoffmann
        },
593 c177684c Gerd Hoffmann
        PORTIO_END_OF_LIST()
594 c177684c Gerd Hoffmann
    },
595 c177684c Gerd Hoffmann
    .endianness = DEVICE_LITTLE_ENDIAN,
596 c177684c Gerd Hoffmann
};
597 c177684c Gerd Hoffmann
598 4cff0a59 Michael S. Tsirkin
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
599 4cff0a59 Michael S. Tsirkin
                                PCIHotplugState state);
600 93d89f63 Isaku Yamahata
601 56e5b2a1 Gerd Hoffmann
static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
602 56e5b2a1 Gerd Hoffmann
                                           PCIBus *bus, PIIX4PMState *s)
603 93d89f63 Isaku Yamahata
{
604 b65b93f2 Gerd Hoffmann
    memory_region_init_io(&s->io_gpe, &piix4_gpe_ops, s, "apci-gpe0",
605 b65b93f2 Gerd Hoffmann
                          GPE_LEN);
606 56e5b2a1 Gerd Hoffmann
    memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
607 ac404095 Isaku Yamahata
608 c177684c Gerd Hoffmann
    memory_region_init_io(&s->io_pci, &piix4_pci_ops, s, "apci-pci-hotplug",
609 c177684c Gerd Hoffmann
                          PCI_HOTPLUG_SIZE);
610 56e5b2a1 Gerd Hoffmann
    memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR,
611 c177684c Gerd Hoffmann
                                &s->io_pci);
612 ac404095 Isaku Yamahata
    pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
613 93d89f63 Isaku Yamahata
}
614 93d89f63 Isaku Yamahata
615 ac404095 Isaku Yamahata
static void enable_device(PIIX4PMState *s, int slot)
616 93d89f63 Isaku Yamahata
{
617 355bf2e5 Gerd Hoffmann
    s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
618 7faa8075 Alex Williamson
    s->pci0_slot_device_present |= (1U << slot);
619 93d89f63 Isaku Yamahata
}
620 93d89f63 Isaku Yamahata
621 ac404095 Isaku Yamahata
static void disable_device(PIIX4PMState *s, int slot)
622 93d89f63 Isaku Yamahata
{
623 355bf2e5 Gerd Hoffmann
    s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
624 7faa8075 Alex Williamson
    s->pci0_status.down |= (1U << slot);
625 93d89f63 Isaku Yamahata
}
626 93d89f63 Isaku Yamahata
627 4cff0a59 Michael S. Tsirkin
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
628 4cff0a59 Michael S. Tsirkin
                                PCIHotplugState state)
629 93d89f63 Isaku Yamahata
{
630 93d89f63 Isaku Yamahata
    int slot = PCI_SLOT(dev->devfn);
631 ac404095 Isaku Yamahata
    PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
632 40021f08 Anthony Liguori
                                PCI_DEVICE(qdev));
633 93d89f63 Isaku Yamahata
634 4cff0a59 Michael S. Tsirkin
    /* Don't send event when device is enabled during qemu machine creation:
635 4cff0a59 Michael S. Tsirkin
     * it is present on boot, no hotplug event is necessary. We do send an
636 4cff0a59 Michael S. Tsirkin
     * event when the device is disabled later. */
637 4cff0a59 Michael S. Tsirkin
    if (state == PCI_COLDPLUG_ENABLED) {
638 7faa8075 Alex Williamson
        s->pci0_slot_device_present |= (1U << slot);
639 5beb8ad5 Isaku Yamahata
        return 0;
640 4cff0a59 Michael S. Tsirkin
    }
641 5beb8ad5 Isaku Yamahata
642 4cff0a59 Michael S. Tsirkin
    if (state == PCI_HOTPLUG_ENABLED) {
643 ac404095 Isaku Yamahata
        enable_device(s, slot);
644 ac404095 Isaku Yamahata
    } else {
645 ac404095 Isaku Yamahata
        disable_device(s, slot);
646 ac404095 Isaku Yamahata
    }
647 633aa0ac Gleb Natapov
648 633aa0ac Gleb Natapov
    pm_update_sci(s);
649 633aa0ac Gleb Natapov
650 93d89f63 Isaku Yamahata
    return 0;
651 93d89f63 Isaku Yamahata
}