]> Git Repo - qemu.git/blame_incremental - hw/acpi/pcihp.c
pci/pcihp: perform check for bus capability in pre_plug handler
[qemu.git] / hw / acpi / pcihp.c
... / ...
CommitLineData
1/*
2 * QEMU<->ACPI BIOS PCI hotplug interface
3 *
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
7 *
8 * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin ([email protected])
9 * Copyright (c) 2006 Fabrice Bellard
10 *
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.
14 *
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.
19 *
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/>
22 *
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.
25 */
26
27#include "qemu/osdep.h"
28#include "hw/acpi/pcihp.h"
29
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/address-spaces.h"
36#include "hw/pci/pci_bus.h"
37#include "qapi/error.h"
38#include "qom/qom-qobject.h"
39
40//#define DEBUG
41
42#ifdef DEBUG
43# define ACPI_PCIHP_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
44#else
45# define ACPI_PCIHP_DPRINTF(format, ...) do { } while (0)
46#endif
47
48#define ACPI_PCIHP_ADDR 0xae00
49#define ACPI_PCIHP_SIZE 0x0014
50#define PCI_UP_BASE 0x0000
51#define PCI_DOWN_BASE 0x0004
52#define PCI_EJ_BASE 0x0008
53#define PCI_RMV_BASE 0x000c
54#define PCI_SEL_BASE 0x0010
55
56typedef struct AcpiPciHpFind {
57 int bsel;
58 PCIBus *bus;
59} AcpiPciHpFind;
60
61static int acpi_pcihp_get_bsel(PCIBus *bus)
62{
63 Error *local_err = NULL;
64 uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
65 &local_err);
66
67 if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
68 if (local_err) {
69 error_free(local_err);
70 }
71 return -1;
72 } else {
73 return bsel;
74 }
75}
76
77/* Assign BSEL property to all buses. In the future, this can be changed
78 * to only assign to buses that support hotplug.
79 */
80static void *acpi_set_bsel(PCIBus *bus, void *opaque)
81{
82 unsigned *bsel_alloc = opaque;
83 unsigned *bus_bsel;
84
85 if (qbus_is_hotpluggable(BUS(bus))) {
86 bus_bsel = g_malloc(sizeof *bus_bsel);
87
88 *bus_bsel = (*bsel_alloc)++;
89 object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
90 bus_bsel, &error_abort);
91 }
92
93 return bsel_alloc;
94}
95
96static void acpi_set_pci_info(void)
97{
98 static bool bsel_is_set;
99 PCIBus *bus;
100 unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
101
102 if (bsel_is_set) {
103 return;
104 }
105 bsel_is_set = true;
106
107 bus = find_i440fx(); /* TODO: Q35 support */
108 if (bus) {
109 /* Scan all PCI buses. Set property to enable acpi based hotplug. */
110 pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
111 }
112}
113
114static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
115{
116 AcpiPciHpFind *find = opaque;
117 if (find->bsel == acpi_pcihp_get_bsel(bus)) {
118 find->bus = bus;
119 }
120}
121
122static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
123{
124 AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
125
126 if (bsel < 0) {
127 return NULL;
128 }
129
130 pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
131
132 /* Make bsel 0 eject root bus if bsel property is not set,
133 * for compatibility with non acpi setups.
134 * TODO: really needed?
135 */
136 if (!bsel && !find.bus) {
137 find.bus = s->root;
138 }
139 return find.bus;
140}
141
142static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
143{
144 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
145 DeviceClass *dc = DEVICE_GET_CLASS(dev);
146 /*
147 * ACPI doesn't allow hotplug of bridge devices. Don't allow
148 * hot-unplug of bridge devices unless they were added by hotplug
149 * (and so, not described by acpi).
150 */
151 return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
152}
153
154static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
155{
156 BusChild *kid, *next;
157 int slot = ctz32(slots);
158 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
159
160 if (!bus) {
161 return;
162 }
163
164 /* Mark request as complete */
165 s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
166 s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
167
168 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
169 DeviceState *qdev = kid->child;
170 PCIDevice *dev = PCI_DEVICE(qdev);
171 if (PCI_SLOT(dev->devfn) == slot) {
172 if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
173 object_unparent(OBJECT(qdev));
174 }
175 }
176 }
177}
178
179static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
180{
181 BusChild *kid, *next;
182 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
183
184 /* Execute any pending removes during reset */
185 while (s->acpi_pcihp_pci_status[bsel].down) {
186 acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
187 }
188
189 s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
190
191 if (!bus) {
192 return;
193 }
194 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
195 DeviceState *qdev = kid->child;
196 PCIDevice *pdev = PCI_DEVICE(qdev);
197 int slot = PCI_SLOT(pdev->devfn);
198
199 if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
200 s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
201 }
202 }
203}
204
205static void acpi_pcihp_update(AcpiPciHpState *s)
206{
207 int i;
208
209 for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
210 acpi_pcihp_update_hotplug_bus(s, i);
211 }
212}
213
214void acpi_pcihp_reset(AcpiPciHpState *s)
215{
216 acpi_set_pci_info();
217 acpi_pcihp_update(s);
218}
219
220void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
221 DeviceState *dev, Error **errp)
222{
223 /* Only hotplugged devices need the hotplug capability. */
224 if (dev->hotplugged &&
225 acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) {
226 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
227 ACPI_PCIHP_PROP_BSEL "' set");
228 return;
229 }
230}
231
232void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
233 DeviceState *dev, Error **errp)
234{
235 PCIDevice *pdev = PCI_DEVICE(dev);
236 int slot = PCI_SLOT(pdev->devfn);
237 int bsel;
238
239 /* Don't send event when device is enabled during qemu machine creation:
240 * it is present on boot, no hotplug event is necessary. We do send an
241 * event when the device is disabled later. */
242 if (!dev->hotplugged) {
243 return;
244 }
245
246 bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
247 g_assert(bsel >= 0);
248 s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
249 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
250}
251
252void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
253 DeviceState *dev, Error **errp)
254{
255 PCIDevice *pdev = PCI_DEVICE(dev);
256 int slot = PCI_SLOT(pdev->devfn);
257 int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
258 if (bsel < 0) {
259 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
260 ACPI_PCIHP_PROP_BSEL "' set");
261 return;
262 }
263
264 s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
265 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
266}
267
268static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
269{
270 AcpiPciHpState *s = opaque;
271 uint32_t val = 0;
272 int bsel = s->hotplug_select;
273
274 if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
275 return 0;
276 }
277
278 switch (addr) {
279 case PCI_UP_BASE:
280 val = s->acpi_pcihp_pci_status[bsel].up;
281 if (!s->legacy_piix) {
282 s->acpi_pcihp_pci_status[bsel].up = 0;
283 }
284 ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
285 break;
286 case PCI_DOWN_BASE:
287 val = s->acpi_pcihp_pci_status[bsel].down;
288 ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
289 break;
290 case PCI_EJ_BASE:
291 /* No feature defined yet */
292 ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
293 break;
294 case PCI_RMV_BASE:
295 val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
296 ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
297 break;
298 case PCI_SEL_BASE:
299 val = s->hotplug_select;
300 ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
301 default:
302 break;
303 }
304
305 return val;
306}
307
308static void pci_write(void *opaque, hwaddr addr, uint64_t data,
309 unsigned int size)
310{
311 AcpiPciHpState *s = opaque;
312 switch (addr) {
313 case PCI_EJ_BASE:
314 if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
315 break;
316 }
317 acpi_pcihp_eject_slot(s, s->hotplug_select, data);
318 ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
319 addr, data);
320 break;
321 case PCI_SEL_BASE:
322 s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
323 ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
324 addr, data);
325 default:
326 break;
327 }
328}
329
330static const MemoryRegionOps acpi_pcihp_io_ops = {
331 .read = pci_read,
332 .write = pci_write,
333 .endianness = DEVICE_LITTLE_ENDIAN,
334 .valid = {
335 .min_access_size = 4,
336 .max_access_size = 4,
337 },
338};
339
340void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
341 MemoryRegion *address_space_io, bool bridges_enabled)
342{
343 s->io_len = ACPI_PCIHP_SIZE;
344 s->io_base = ACPI_PCIHP_ADDR;
345
346 s->root= root_bus;
347 s->legacy_piix = !bridges_enabled;
348
349 memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
350 "acpi-pci-hotplug", s->io_len);
351 memory_region_add_subregion(address_space_io, s->io_base, &s->io);
352
353 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
354 &error_abort);
355 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
356 &error_abort);
357}
358
359const VMStateDescription vmstate_acpi_pcihp_pci_status = {
360 .name = "acpi_pcihp_pci_status",
361 .version_id = 1,
362 .minimum_version_id = 1,
363 .fields = (VMStateField[]) {
364 VMSTATE_UINT32(up, AcpiPciHpPciStatus),
365 VMSTATE_UINT32(down, AcpiPciHpPciStatus),
366 VMSTATE_END_OF_LIST()
367 }
368};
This page took 0.022396 seconds and 4 git commands to generate.