1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "hw/acpi/pcihp.h"
29
30#include "hw/pci-host/i440fx.h"
31#include "hw/pci/pci.h"
32#include "hw/pci/pci_bridge.h"
33#include "hw/pci/pci_host.h"
34#include "hw/pci/pcie_port.h"
35#include "hw/i386/acpi-build.h"
36#include "hw/acpi/acpi.h"
37#include "hw/pci/pci_bus.h"
38#include "migration/vmstate.h"
39#include "qapi/error.h"
40#include "qom/qom-qobject.h"
41#include "trace.h"
42
43#define ACPI_PCIHP_SIZE 0x0018
44#define PCI_UP_BASE 0x0000
45#define PCI_DOWN_BASE 0x0004
46#define PCI_EJ_BASE 0x0008
47#define PCI_RMV_BASE 0x000c
48#define PCI_SEL_BASE 0x0010
49#define PCI_AIDX_BASE 0x0014
50
51typedef struct AcpiPciHpFind {
52 int bsel;
53 PCIBus *bus;
54} AcpiPciHpFind;
55
56static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data)
57{
58 return a - b;
59}
60
61static GSequence *pci_acpi_index_list(void)
62{
63 static GSequence *used_acpi_index_list;
64
65 if (!used_acpi_index_list) {
66 used_acpi_index_list = g_sequence_new(NULL);
67 }
68 return used_acpi_index_list;
69}
70
71static int acpi_pcihp_get_bsel(PCIBus *bus)
72{
73 Error *local_err = NULL;
74 uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
75 &local_err);
76
77 if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
78 if (local_err) {
79 error_free(local_err);
80 }
81 return -1;
82 } else {
83 return bsel;
84 }
85}
86
87
88
89
90static void *acpi_set_bsel(PCIBus *bus, void *opaque)
91{
92 unsigned *bsel_alloc = opaque;
93 unsigned *bus_bsel;
94
95 if (qbus_is_hotpluggable(BUS(bus))) {
96 bus_bsel = g_malloc(sizeof *bus_bsel);
97
98 *bus_bsel = (*bsel_alloc)++;
99 object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
100 bus_bsel, OBJ_PROP_FLAG_READ);
101 }
102
103 return bsel_alloc;
104}
105
106static void acpi_set_pci_info(void)
107{
108 static bool bsel_is_set;
109 Object *host = acpi_get_i386_pci_host();
110 PCIBus *bus;
111 unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
112
113 if (bsel_is_set) {
114 return;
115 }
116 bsel_is_set = true;
117
118 if (!host) {
119 return;
120 }
121
122 bus = PCI_HOST_BRIDGE(host)->bus;
123 if (bus) {
124
125 pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
126 }
127}
128
129static void acpi_pcihp_disable_root_bus(void)
130{
131 static bool root_hp_disabled;
132 Object *host = acpi_get_i386_pci_host();
133 PCIBus *bus;
134
135 if (root_hp_disabled) {
136 return;
137 }
138
139 bus = PCI_HOST_BRIDGE(host)->bus;
140 if (bus) {
141
142 qbus_set_hotplug_handler(BUS(bus), NULL);
143 }
144 root_hp_disabled = true;
145 return;
146}
147
148static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
149{
150 AcpiPciHpFind *find = opaque;
151 if (find->bsel == acpi_pcihp_get_bsel(bus)) {
152 find->bus = bus;
153 }
154}
155
156static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
157{
158 AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
159
160 if (bsel < 0) {
161 return NULL;
162 }
163
164 pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
165
166
167
168
169
170 if (!bsel && !find.bus) {
171 find.bus = s->root;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
186 find.bus = NULL;
187 }
188 return find.bus;
189}
190
191static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
192{
193 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
194 DeviceClass *dc = DEVICE_GET_CLASS(dev);
195
196
197
198
199
200 return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
201}
202
203static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
204{
205 HotplugHandler *hotplug_ctrl;
206 BusChild *kid, *next;
207 int slot = ctz32(slots);
208 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
209
210 trace_acpi_pci_eject_slot(bsel, slot);
211
212 if (!bus || slot > 31) {
213 return;
214 }
215
216
217 s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
218 s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
219
220 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
221 DeviceState *qdev = kid->child;
222 PCIDevice *dev = PCI_DEVICE(qdev);
223 if (PCI_SLOT(dev->devfn) == slot) {
224 if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
225
226
227
228
229
230
231
232 if (dev->partially_hotplugged) {
233
234
235
236
237
238
239
240 qdev->pending_deleted_event = false;
241 } else {
242 hotplug_ctrl = qdev_get_hotplug_handler(qdev);
243 hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
244 object_unparent(OBJECT(qdev));
245 }
246 }
247 }
248 }
249}
250
251static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
252{
253 BusChild *kid, *next;
254 PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
255
256
257 while (s->acpi_pcihp_pci_status[bsel].down) {
258 acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
259 }
260
261 s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
262
263 if (!bus) {
264 return;
265 }
266 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
267 DeviceState *qdev = kid->child;
268 PCIDevice *pdev = PCI_DEVICE(qdev);
269 int slot = PCI_SLOT(pdev->devfn);
270
271 if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
272 s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
273 }
274 }
275}
276
277static void acpi_pcihp_update(AcpiPciHpState *s)
278{
279 int i;
280
281 for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
282 acpi_pcihp_update_hotplug_bus(s, i);
283 }
284}
285
286void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
287{
288 if (acpihp_root_off) {
289 acpi_pcihp_disable_root_bus();
290 }
291 acpi_set_pci_info();
292 acpi_pcihp_update(s);
293}
294
295#define ONBOARD_INDEX_MAX (16 * 1024 - 1)
296
297void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
298 DeviceState *dev, Error **errp)
299{
300 PCIDevice *pdev = PCI_DEVICE(dev);
301
302
303 if (dev->hotplugged &&
304 acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
305 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
306 ACPI_PCIHP_PROP_BSEL "' set");
307 return;
308 }
309
310
311
312
313
314
315 if (pdev->acpi_index > ONBOARD_INDEX_MAX) {
316 error_setg(errp, "acpi-index should be less or equal to %u",
317 ONBOARD_INDEX_MAX);
318 return;
319 }
320
321
322
323
324 if (pdev->acpi_index) {
325 GSequence *used_indexes = pci_acpi_index_list();
326
327 if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index),
328 g_cmp_uint32, NULL)) {
329 error_setg(errp, "a PCI device with acpi-index = %" PRIu32
330 " already exist", pdev->acpi_index);
331 return;
332 }
333 g_sequence_insert_sorted(used_indexes,
334 GINT_TO_POINTER(pdev->acpi_index),
335 g_cmp_uint32, NULL);
336 }
337}
338
339void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
340 DeviceState *dev, Error **errp)
341{
342 PCIDevice *pdev = PCI_DEVICE(dev);
343 int slot = PCI_SLOT(pdev->devfn);
344 int bsel;
345
346
347
348
349 if (!dev->hotplugged) {
350
351
352
353
354 if (!s->legacy_piix &&
355 object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
356 PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
357
358
359 if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT) &&
360 !PCIE_SLOT(pdev)->hotplug) {
361 qbus_set_hotplug_handler(BUS(sec), NULL);
362 return;
363 }
364
365 qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
366
367 assert(QLIST_EMPTY(&sec->child));
368 }
369
370 return;
371 }
372
373 bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
374 g_assert(bsel >= 0);
375 s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
376 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
377}
378
379void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
380 DeviceState *dev, Error **errp)
381{
382 PCIDevice *pdev = PCI_DEVICE(dev);
383
384 trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
385 acpi_pcihp_get_bsel(pci_get_bus(pdev)));
386
387
388
389
390 if (pdev->acpi_index) {
391 GSequence *used_indexes = pci_acpi_index_list();
392
393 g_sequence_remove(g_sequence_lookup(used_indexes,
394 GINT_TO_POINTER(pdev->acpi_index),
395 g_cmp_uint32, NULL));
396 }
397
398 qdev_unrealize(dev);
399}
400
401void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
402 AcpiPciHpState *s, DeviceState *dev,
403 Error **errp)
404{
405 PCIDevice *pdev = PCI_DEVICE(dev);
406 int slot = PCI_SLOT(pdev->devfn);
407 int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
408
409 trace_acpi_pci_unplug_request(bsel, slot);
410
411 if (bsel < 0) {
412 error_setg(errp, "Unsupported bus. Bus doesn't have property '"
413 ACPI_PCIHP_PROP_BSEL "' set");
414 return;
415 }
416
417
418
419
420
421
422 pdev->qdev.pending_deleted_event = true;
423 s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
424 acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
425}
426
427static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
428{
429 AcpiPciHpState *s = opaque;
430 uint32_t val = 0;
431 int bsel = s->hotplug_select;
432
433 if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
434 return 0;
435 }
436
437 switch (addr) {
438 case PCI_UP_BASE:
439 val = s->acpi_pcihp_pci_status[bsel].up;
440 if (!s->legacy_piix) {
441 s->acpi_pcihp_pci_status[bsel].up = 0;
442 }
443 trace_acpi_pci_up_read(val);
444 break;
445 case PCI_DOWN_BASE:
446 val = s->acpi_pcihp_pci_status[bsel].down;
447 trace_acpi_pci_down_read(val);
448 break;
449 case PCI_EJ_BASE:
450 trace_acpi_pci_features_read(val);
451 break;
452 case PCI_RMV_BASE:
453 val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
454 trace_acpi_pci_rmv_read(val);
455 break;
456 case PCI_SEL_BASE:
457 val = s->hotplug_select;
458 trace_acpi_pci_sel_read(val);
459 break;
460 case PCI_AIDX_BASE:
461 val = s->acpi_index;
462 s->acpi_index = 0;
463 trace_acpi_pci_acpi_index_read(val);
464 break;
465 default:
466 break;
467 }
468
469 return val;
470}
471
472static void pci_write(void *opaque, hwaddr addr, uint64_t data,
473 unsigned int size)
474{
475 int slot;
476 PCIBus *bus;
477 BusChild *kid, *next;
478 AcpiPciHpState *s = opaque;
479
480 s->acpi_index = 0;
481 switch (addr) {
482 case PCI_AIDX_BASE:
483
484
485
486
487 slot = ctz32(data);
488
489 if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
490 break;
491 }
492
493 bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
494 QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
495 Object *o = OBJECT(kid->child);
496 PCIDevice *dev = PCI_DEVICE(o);
497 if (PCI_SLOT(dev->devfn) == slot) {
498 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
499 break;
500 }
501 }
502 trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
503 break;
504 case PCI_EJ_BASE:
505 if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
506 break;
507 }
508 acpi_pcihp_eject_slot(s, s->hotplug_select, data);
509 trace_acpi_pci_ej_write(addr, data);
510 break;
511 case PCI_SEL_BASE:
512 s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
513 trace_acpi_pci_sel_write(addr, data);
514 default:
515 break;
516 }
517}
518
519static const MemoryRegionOps acpi_pcihp_io_ops = {
520 .read = pci_read,
521 .write = pci_write,
522 .endianness = DEVICE_LITTLE_ENDIAN,
523 .valid = {
524 .min_access_size = 4,
525 .max_access_size = 4,
526 },
527};
528
529void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
530 MemoryRegion *address_space_io, bool bridges_enabled,
531 uint16_t io_base)
532{
533 s->io_len = ACPI_PCIHP_SIZE;
534 s->io_base = io_base;
535
536 s->root = root_bus;
537 s->legacy_piix = !bridges_enabled;
538
539 memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
540 "acpi-pci-hotplug", s->io_len);
541 memory_region_add_subregion(address_space_io, s->io_base, &s->io);
542
543 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
544 OBJ_PROP_FLAG_READ);
545 object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
546 OBJ_PROP_FLAG_READ);
547}
548
549bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id)
550{
551 AcpiPciHpState *s = opaque;
552 return s->acpi_index;
553}
554
555const VMStateDescription vmstate_acpi_pcihp_pci_status = {
556 .name = "acpi_pcihp_pci_status",
557 .version_id = 1,
558 .minimum_version_id = 1,
559 .fields = (VMStateField[]) {
560 VMSTATE_UINT32(up, AcpiPciHpPciStatus),
561 VMSTATE_UINT32(down, AcpiPciHpPciStatus),
562 VMSTATE_END_OF_LIST()
563 }
564};
565