]>
Commit | Line | Data |
---|---|---|
db4728e6 MT |
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 | ||
b6a0aa05 | 27 | #include "qemu/osdep.h" |
db4728e6 MT |
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" | |
db4728e6 MT |
35 | #include "exec/address-spaces.h" |
36 | #include "hw/pci/pci_bus.h" | |
da34e65c | 37 | #include "qapi/error.h" |
db4728e6 | 38 | #include "qom/qom-qobject.h" |
db4728e6 MT |
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 | ||
e358edc8 IM |
48 | #define ACPI_PCIHP_ADDR 0xae00 |
49 | #define ACPI_PCIHP_SIZE 0x0014 | |
a7b613cf IM |
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 | |
db4728e6 MT |
55 | |
56 | typedef struct AcpiPciHpFind { | |
57 | int bsel; | |
58 | PCIBus *bus; | |
59 | } AcpiPciHpFind; | |
60 | ||
61 | static int acpi_pcihp_get_bsel(PCIBus *bus) | |
62 | { | |
7c38ecd0 | 63 | Error *local_err = NULL; |
c03d83d5 MAL |
64 | uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, |
65 | &local_err); | |
7c38ecd0 | 66 | |
c03d83d5 | 67 | if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
7c38ecd0 KB |
68 | if (local_err) { |
69 | error_free(local_err); | |
70 | } | |
db4728e6 | 71 | return -1; |
7c38ecd0 KB |
72 | } else { |
73 | return bsel; | |
db4728e6 | 74 | } |
db4728e6 MT |
75 | } |
76 | ||
ab938ae4 AP |
77 | /* Assign BSEL property to all buses. In the future, this can be changed |
78 | * to only assign to buses that support hotplug. | |
79 | */ | |
80 | static 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 | ||
96 | static 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 | ||
db4728e6 MT |
114 | static 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 | ||
122 | static 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 | ||
142 | static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) | |
143 | { | |
144 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); | |
2897ae02 | 145 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
db4728e6 MT |
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 | */ | |
2897ae02 | 151 | return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; |
db4728e6 MT |
152 | } |
153 | ||
154 | static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) | |
155 | { | |
156 | BusChild *kid, *next; | |
786a4ea8 | 157 | int slot = ctz32(slots); |
db4728e6 MT |
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); | |
5a2223ca | 166 | s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); |
db4728e6 MT |
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) { | |
5a2223ca | 172 | if (!acpi_pcihp_pc_no_hotplug(s, dev)) { |
db4728e6 MT |
173 | object_unparent(OBJECT(qdev)); |
174 | } | |
175 | } | |
176 | } | |
db4728e6 MT |
177 | } |
178 | ||
179 | static 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; | |
db4728e6 MT |
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 | } | |
db4728e6 MT |
202 | } |
203 | } | |
204 | ||
205 | static 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 | ||
214 | void acpi_pcihp_reset(AcpiPciHpState *s) | |
215 | { | |
ab938ae4 | 216 | acpi_set_pci_info(); |
db4728e6 MT |
217 | acpi_pcihp_update(s); |
218 | } | |
219 | ||
0058c082 | 220 | void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, |
c24d5e0b | 221 | DeviceState *dev, Error **errp) |
db4728e6 | 222 | { |
c24d5e0b IM |
223 | PCIDevice *pdev = PCI_DEVICE(dev); |
224 | int slot = PCI_SLOT(pdev->devfn); | |
fd56e061 | 225 | int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); |
db4728e6 | 226 | if (bsel < 0) { |
c24d5e0b IM |
227 | error_setg(errp, "Unsupported bus. Bus doesn't have property '" |
228 | ACPI_PCIHP_PROP_BSEL "' set"); | |
229 | return; | |
db4728e6 MT |
230 | } |
231 | ||
232 | /* Don't send event when device is enabled during qemu machine creation: | |
233 | * it is present on boot, no hotplug event is necessary. We do send an | |
234 | * event when the device is disabled later. */ | |
c24d5e0b IM |
235 | if (!dev->hotplugged) { |
236 | return; | |
db4728e6 MT |
237 | } |
238 | ||
c24d5e0b | 239 | s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); |
0058c082 | 240 | acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); |
c24d5e0b IM |
241 | } |
242 | ||
0058c082 | 243 | void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, |
c24d5e0b IM |
244 | DeviceState *dev, Error **errp) |
245 | { | |
246 | PCIDevice *pdev = PCI_DEVICE(dev); | |
247 | int slot = PCI_SLOT(pdev->devfn); | |
fd56e061 | 248 | int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); |
c24d5e0b IM |
249 | if (bsel < 0) { |
250 | error_setg(errp, "Unsupported bus. Bus doesn't have property '" | |
251 | ACPI_PCIHP_PROP_BSEL "' set"); | |
252 | return; | |
db4728e6 MT |
253 | } |
254 | ||
c24d5e0b | 255 | s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); |
0058c082 | 256 | acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); |
db4728e6 MT |
257 | } |
258 | ||
259 | static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) | |
260 | { | |
261 | AcpiPciHpState *s = opaque; | |
262 | uint32_t val = 0; | |
263 | int bsel = s->hotplug_select; | |
264 | ||
fa365d7c | 265 | if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
db4728e6 MT |
266 | return 0; |
267 | } | |
268 | ||
269 | switch (addr) { | |
a7b613cf | 270 | case PCI_UP_BASE: |
5a2223ca | 271 | val = s->acpi_pcihp_pci_status[bsel].up; |
99d09dd3 IM |
272 | if (!s->legacy_piix) { |
273 | s->acpi_pcihp_pci_status[bsel].up = 0; | |
274 | } | |
db4728e6 MT |
275 | ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val); |
276 | break; | |
a7b613cf | 277 | case PCI_DOWN_BASE: |
db4728e6 MT |
278 | val = s->acpi_pcihp_pci_status[bsel].down; |
279 | ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val); | |
280 | break; | |
a7b613cf | 281 | case PCI_EJ_BASE: |
db4728e6 MT |
282 | /* No feature defined yet */ |
283 | ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val); | |
284 | break; | |
a7b613cf | 285 | case PCI_RMV_BASE: |
db4728e6 MT |
286 | val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; |
287 | ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val); | |
288 | break; | |
a7b613cf | 289 | case PCI_SEL_BASE: |
db4728e6 MT |
290 | val = s->hotplug_select; |
291 | ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val); | |
292 | default: | |
293 | break; | |
294 | } | |
295 | ||
296 | return val; | |
297 | } | |
298 | ||
299 | static void pci_write(void *opaque, hwaddr addr, uint64_t data, | |
300 | unsigned int size) | |
301 | { | |
302 | AcpiPciHpState *s = opaque; | |
303 | switch (addr) { | |
a7b613cf | 304 | case PCI_EJ_BASE: |
db4728e6 MT |
305 | if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
306 | break; | |
307 | } | |
308 | acpi_pcihp_eject_slot(s, s->hotplug_select, data); | |
309 | ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n", | |
310 | addr, data); | |
311 | break; | |
a7b613cf | 312 | case PCI_SEL_BASE: |
f5855994 | 313 | s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; |
db4728e6 MT |
314 | ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n", |
315 | addr, data); | |
316 | default: | |
317 | break; | |
318 | } | |
319 | } | |
320 | ||
321 | static const MemoryRegionOps acpi_pcihp_io_ops = { | |
322 | .read = pci_read, | |
323 | .write = pci_write, | |
324 | .endianness = DEVICE_LITTLE_ENDIAN, | |
325 | .valid = { | |
326 | .min_access_size = 4, | |
327 | .max_access_size = 4, | |
328 | }, | |
329 | }; | |
330 | ||
78c2d872 | 331 | void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, |
99d09dd3 | 332 | MemoryRegion *address_space_io, bool bridges_enabled) |
db4728e6 | 333 | { |
78c2d872 IM |
334 | s->io_len = ACPI_PCIHP_SIZE; |
335 | s->io_base = ACPI_PCIHP_ADDR; | |
e358edc8 | 336 | |
db4728e6 | 337 | s->root= root_bus; |
99d09dd3 | 338 | s->legacy_piix = !bridges_enabled; |
e358edc8 | 339 | |
78c2d872 IM |
340 | memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, |
341 | "acpi-pci-hotplug", s->io_len); | |
342 | memory_region_add_subregion(address_space_io, s->io_base, &s->io); | |
343 | ||
344 | object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, | |
345 | &error_abort); | |
346 | object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, | |
347 | &error_abort); | |
db4728e6 MT |
348 | } |
349 | ||
350 | const VMStateDescription vmstate_acpi_pcihp_pci_status = { | |
351 | .name = "acpi_pcihp_pci_status", | |
352 | .version_id = 1, | |
353 | .minimum_version_id = 1, | |
d49805ae | 354 | .fields = (VMStateField[]) { |
db4728e6 MT |
355 | VMSTATE_UINT32(up, AcpiPciHpPciStatus), |
356 | VMSTATE_UINT32(down, AcpiPciHpPciStatus), | |
357 | VMSTATE_END_OF_LIST() | |
358 | } | |
359 | }; |