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