memory: Introduce DEVICE_HOST_ENDIAN for ram device
[qemu/ar7.git] / hw / acpi / pcihp.c
blob2b0f3e1bfb414d2fd0aa3a2395104ea76172957d
1 /*
2 * QEMU<->ACPI BIOS PCI hotplug interface
4 * QEMU supports PCI hotplug via ACPI. This module
5 * implements the interface between QEMU and the ACPI BIOS.
6 * Interface specification - see docs/specs/acpi_pci_hotplug.txt
8 * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
9 * Copyright (c) 2006 Fabrice Bellard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License version 2 as published by the Free Software Foundation.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>
23 * Contributions after 2012-01-13 are licensed under the terms of the
24 * GNU GPL, version 2 or (at your option) any later version.
27 #include "qemu/osdep.h"
28 #include "hw/acpi/pcihp.h"
30 #include "hw/hw.h"
31 #include "hw/i386/pc.h"
32 #include "hw/pci/pci.h"
33 #include "hw/acpi/acpi.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/ioport.h"
36 #include "exec/address-spaces.h"
37 #include "hw/pci/pci_bus.h"
38 #include "qapi/error.h"
39 #include "qom/qom-qobject.h"
40 #include "qapi/qmp/qint.h"
42 //#define DEBUG
44 #ifdef DEBUG
45 # define ACPI_PCIHP_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
46 #else
47 # define ACPI_PCIHP_DPRINTF(format, ...) do { } while (0)
48 #endif
50 #define ACPI_PCIHP_ADDR 0xae00
51 #define ACPI_PCIHP_SIZE 0x0014
52 #define PCI_UP_BASE 0x0000
53 #define PCI_DOWN_BASE 0x0004
54 #define PCI_EJ_BASE 0x0008
55 #define PCI_RMV_BASE 0x000c
56 #define PCI_SEL_BASE 0x0010
58 typedef struct AcpiPciHpFind {
59 int bsel;
60 PCIBus *bus;
61 } AcpiPciHpFind;
63 static int acpi_pcihp_get_bsel(PCIBus *bus)
65 Error *local_err = NULL;
66 int64_t bsel = object_property_get_int(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
67 &local_err);
69 if (local_err || bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
70 if (local_err) {
71 error_free(local_err);
73 return -1;
74 } else {
75 return bsel;
79 static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
81 AcpiPciHpFind *find = opaque;
82 if (find->bsel == acpi_pcihp_get_bsel(bus)) {
83 find->bus = bus;
87 static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
89 AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
91 if (bsel < 0) {
92 return NULL;
95 pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
97 /* Make bsel 0 eject root bus if bsel property is not set,
98 * for compatibility with non acpi setups.
99 * TODO: really needed?
101 if (!bsel && !find.bus) {
102 find.bus = s->root;
104 return find.bus;
107 static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
109 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
110 DeviceClass *dc = DEVICE_GET_CLASS(dev);
112 * ACPI doesn't allow hotplug of bridge devices. Don't allow
113 * hot-unplug of bridge devices unless they were added by hotplug
114 * (and so, not described by acpi).
116 return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
119 static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
121 BusChild *kid, *next;
122 int slot = ctz32(slots);
123 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
125 if (!bus) {
126 return;
129 /* Mark request as complete */
130 s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
131 s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
133 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
134 DeviceState *qdev = kid->child;
135 PCIDevice *dev = PCI_DEVICE(qdev);
136 if (PCI_SLOT(dev->devfn) == slot) {
137 if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
138 object_unparent(OBJECT(qdev));
144 static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
146 BusChild *kid, *next;
147 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
149 /* Execute any pending removes during reset */
150 while (s->acpi_pcihp_pci_status[bsel].down) {
151 acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
154 s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
156 if (!bus) {
157 return;
159 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
160 DeviceState *qdev = kid->child;
161 PCIDevice *pdev = PCI_DEVICE(qdev);
162 int slot = PCI_SLOT(pdev->devfn);
164 if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
165 s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
170 static void acpi_pcihp_update(AcpiPciHpState *s)
172 int i;
174 for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
175 acpi_pcihp_update_hotplug_bus(s, i);
179 void acpi_pcihp_reset(AcpiPciHpState *s)
181 acpi_pcihp_update(s);
184 void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
185 DeviceState *dev, Error **errp)
187 PCIDevice *pdev = PCI_DEVICE(dev);
188 int slot = PCI_SLOT(pdev->devfn);
189 int bsel = acpi_pcihp_get_bsel(pdev->bus);
190 if (bsel < 0) {
191 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
192 ACPI_PCIHP_PROP_BSEL "' set");
193 return;
196 /* Don't send event when device is enabled during qemu machine creation:
197 * it is present on boot, no hotplug event is necessary. We do send an
198 * event when the device is disabled later. */
199 if (!dev->hotplugged) {
200 return;
203 s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
204 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
207 void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
208 DeviceState *dev, Error **errp)
210 PCIDevice *pdev = PCI_DEVICE(dev);
211 int slot = PCI_SLOT(pdev->devfn);
212 int bsel = acpi_pcihp_get_bsel(pdev->bus);
213 if (bsel < 0) {
214 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
215 ACPI_PCIHP_PROP_BSEL "' set");
216 return;
219 s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
220 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
223 static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
225 AcpiPciHpState *s = opaque;
226 uint32_t val = 0;
227 int bsel = s->hotplug_select;
229 if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
230 return 0;
233 switch (addr) {
234 case PCI_UP_BASE:
235 val = s->acpi_pcihp_pci_status[bsel].up;
236 if (!s->legacy_piix) {
237 s->acpi_pcihp_pci_status[bsel].up = 0;
239 ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
240 break;
241 case PCI_DOWN_BASE:
242 val = s->acpi_pcihp_pci_status[bsel].down;
243 ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
244 break;
245 case PCI_EJ_BASE:
246 /* No feature defined yet */
247 ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
248 break;
249 case PCI_RMV_BASE:
250 val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
251 ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
252 break;
253 case PCI_SEL_BASE:
254 val = s->hotplug_select;
255 ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
256 default:
257 break;
260 return val;
263 static void pci_write(void *opaque, hwaddr addr, uint64_t data,
264 unsigned int size)
266 AcpiPciHpState *s = opaque;
267 switch (addr) {
268 case PCI_EJ_BASE:
269 if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
270 break;
272 acpi_pcihp_eject_slot(s, s->hotplug_select, data);
273 ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
274 addr, data);
275 break;
276 case PCI_SEL_BASE:
277 s->hotplug_select = data;
278 ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
279 addr, data);
280 default:
281 break;
285 static const MemoryRegionOps acpi_pcihp_io_ops = {
286 .read = pci_read,
287 .write = pci_write,
288 .endianness = DEVICE_LITTLE_ENDIAN,
289 .valid = {
290 .min_access_size = 4,
291 .max_access_size = 4,
295 void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
296 MemoryRegion *address_space_io, bool bridges_enabled)
298 s->io_len = ACPI_PCIHP_SIZE;
299 s->io_base = ACPI_PCIHP_ADDR;
301 s->root= root_bus;
302 s->legacy_piix = !bridges_enabled;
304 memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
305 "acpi-pci-hotplug", s->io_len);
306 memory_region_add_subregion(address_space_io, s->io_base, &s->io);
308 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
309 &error_abort);
310 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
311 &error_abort);
314 const VMStateDescription vmstate_acpi_pcihp_pci_status = {
315 .name = "acpi_pcihp_pci_status",
316 .version_id = 1,
317 .minimum_version_id = 1,
318 .fields = (VMStateField[]) {
319 VMSTATE_UINT32(up, AcpiPciHpPciStatus),
320 VMSTATE_UINT32(down, AcpiPciHpPciStatus),
321 VMSTATE_END_OF_LIST()