]>
Commit | Line | Data |
---|---|---|
69b91039 FB |
1 | /* |
2 | * QEMU PCI bus manager | |
3 | * | |
4 | * Copyright (c) 2004 Fabrice Bellard | |
5fafdf24 | 5 | * |
69b91039 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
c759b24f MT |
24 | #include "hw/hw.h" |
25 | #include "hw/pci/pci.h" | |
26 | #include "hw/pci/pci_bridge.h" | |
06aac7bd | 27 | #include "hw/pci/pci_bus.h" |
568f0690 | 28 | #include "hw/pci/pci_host.h" |
83c9089e | 29 | #include "monitor/monitor.h" |
1422e32d | 30 | #include "net/net.h" |
9c17d615 | 31 | #include "sysemu/sysemu.h" |
c759b24f | 32 | #include "hw/loader.h" |
1de7afc9 | 33 | #include "qemu/range.h" |
79627472 | 34 | #include "qmp-commands.h" |
c759b24f MT |
35 | #include "hw/pci/msi.h" |
36 | #include "hw/pci/msix.h" | |
022c62cb | 37 | #include "exec/address-spaces.h" |
69b91039 FB |
38 | |
39 | //#define DEBUG_PCI | |
d8d2e079 | 40 | #ifdef DEBUG_PCI |
2e49d64a | 41 | # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
d8d2e079 IY |
42 | #else |
43 | # define PCI_DPRINTF(format, ...) do { } while (0) | |
44 | #endif | |
69b91039 | 45 | |
10c4c98a | 46 | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
4f43c1ff | 47 | static char *pcibus_get_dev_path(DeviceState *dev); |
5e0259e7 | 48 | static char *pcibus_get_fw_dev_path(DeviceState *dev); |
9bb33586 | 49 | static int pcibus_reset(BusState *qbus); |
5c397242 | 50 | static void pci_bus_finalize(Object *obj); |
10c4c98a | 51 | |
3cb75a7c PB |
52 | static Property pci_props[] = { |
53 | DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), | |
54 | DEFINE_PROP_STRING("romfile", PCIDevice, romfile), | |
55 | DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), | |
56 | DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present, | |
57 | QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false), | |
58 | DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present, | |
59 | QEMU_PCI_CAP_SERR_BITNR, true), | |
60 | DEFINE_PROP_END_OF_LIST() | |
61 | }; | |
62 | ||
0d936928 AL |
63 | static void pci_bus_class_init(ObjectClass *klass, void *data) |
64 | { | |
65 | BusClass *k = BUS_CLASS(klass); | |
66 | ||
67 | k->print_dev = pcibus_dev_print; | |
68 | k->get_dev_path = pcibus_get_dev_path; | |
69 | k->get_fw_dev_path = pcibus_get_fw_dev_path; | |
70 | k->reset = pcibus_reset; | |
71 | } | |
72 | ||
73 | static const TypeInfo pci_bus_info = { | |
74 | .name = TYPE_PCI_BUS, | |
75 | .parent = TYPE_BUS, | |
76 | .instance_size = sizeof(PCIBus), | |
5c397242 | 77 | .instance_finalize = pci_bus_finalize, |
0d936928 | 78 | .class_init = pci_bus_class_init, |
30468f78 | 79 | }; |
69b91039 | 80 | |
3a861c46 AW |
81 | static const TypeInfo pcie_bus_info = { |
82 | .name = TYPE_PCIE_BUS, | |
83 | .parent = TYPE_PCI_BUS, | |
84 | }; | |
85 | ||
d662210a | 86 | static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num); |
1941d19c | 87 | static void pci_update_mappings(PCIDevice *d); |
d98f08f5 | 88 | static void pci_irq_handler(void *opaque, int irq_num, int level); |
ab85ceb1 | 89 | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom); |
230741dc | 90 | static void pci_del_option_rom(PCIDevice *pdev); |
1941d19c | 91 | |
d350d97d AL |
92 | static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET; |
93 | static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU; | |
e822a52a | 94 | |
7588e2b0 | 95 | static QLIST_HEAD(, PCIHostState) pci_host_bridges; |
30468f78 | 96 | |
2d1e9f96 JQ |
97 | static const VMStateDescription vmstate_pcibus = { |
98 | .name = "PCIBUS", | |
99 | .version_id = 1, | |
100 | .minimum_version_id = 1, | |
101 | .minimum_version_id_old = 1, | |
102 | .fields = (VMStateField []) { | |
103 | VMSTATE_INT32_EQUAL(nirq, PCIBus), | |
c7bde572 | 104 | VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t), |
2d1e9f96 | 105 | VMSTATE_END_OF_LIST() |
52fc1d83 | 106 | } |
2d1e9f96 | 107 | }; |
b3b11697 | 108 | static int pci_bar(PCIDevice *d, int reg) |
5330de09 | 109 | { |
b3b11697 IY |
110 | uint8_t type; |
111 | ||
112 | if (reg != PCI_ROM_SLOT) | |
113 | return PCI_BASE_ADDRESS_0 + reg * 4; | |
114 | ||
115 | type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; | |
116 | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS; | |
5330de09 MT |
117 | } |
118 | ||
d036bb21 MT |
119 | static inline int pci_irq_state(PCIDevice *d, int irq_num) |
120 | { | |
121 | return (d->irq_state >> irq_num) & 0x1; | |
122 | } | |
123 | ||
124 | static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) | |
125 | { | |
126 | d->irq_state &= ~(0x1 << irq_num); | |
127 | d->irq_state |= level << irq_num; | |
128 | } | |
129 | ||
130 | static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) | |
131 | { | |
132 | PCIBus *bus; | |
133 | for (;;) { | |
134 | bus = pci_dev->bus; | |
135 | irq_num = bus->map_irq(pci_dev, irq_num); | |
136 | if (bus->set_irq) | |
137 | break; | |
138 | pci_dev = bus->parent_dev; | |
139 | } | |
140 | bus->irq_count[irq_num] += change; | |
141 | bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0); | |
142 | } | |
143 | ||
9ddf8437 IY |
144 | int pci_bus_get_irq_level(PCIBus *bus, int irq_num) |
145 | { | |
146 | assert(irq_num >= 0); | |
147 | assert(irq_num < bus->nirq); | |
148 | return !!bus->irq_count[irq_num]; | |
149 | } | |
150 | ||
f9bf77dd MT |
151 | /* Update interrupt status bit in config space on interrupt |
152 | * state change. */ | |
153 | static void pci_update_irq_status(PCIDevice *dev) | |
154 | { | |
155 | if (dev->irq_state) { | |
156 | dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; | |
157 | } else { | |
158 | dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; | |
159 | } | |
160 | } | |
161 | ||
4c92325b IY |
162 | void pci_device_deassert_intx(PCIDevice *dev) |
163 | { | |
164 | int i; | |
165 | for (i = 0; i < PCI_NUM_PINS; ++i) { | |
d98f08f5 | 166 | pci_irq_handler(dev, i, 0); |
4c92325b IY |
167 | } |
168 | } | |
169 | ||
0ead87c8 IY |
170 | /* |
171 | * This function is called on #RST and FLR. | |
172 | * FLR if PCI_EXP_DEVCTL_BCR_FLR is set | |
173 | */ | |
174 | void pci_device_reset(PCIDevice *dev) | |
5330de09 | 175 | { |
c0b1905b | 176 | int r; |
6fc4925b AL |
177 | |
178 | qdev_reset_all(&dev->qdev); | |
c0b1905b | 179 | |
d036bb21 | 180 | dev->irq_state = 0; |
f9bf77dd | 181 | pci_update_irq_status(dev); |
4c92325b | 182 | pci_device_deassert_intx(dev); |
ebabb67a | 183 | /* Clear all writable bits */ |
99443c21 | 184 | pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, |
f9aebe2e MT |
185 | pci_get_word(dev->wmask + PCI_COMMAND) | |
186 | pci_get_word(dev->w1cmask + PCI_COMMAND)); | |
89d437df IY |
187 | pci_word_test_and_clear_mask(dev->config + PCI_STATUS, |
188 | pci_get_word(dev->wmask + PCI_STATUS) | | |
189 | pci_get_word(dev->w1cmask + PCI_STATUS)); | |
c0b1905b MT |
190 | dev->config[PCI_CACHE_LINE_SIZE] = 0x0; |
191 | dev->config[PCI_INTERRUPT_LINE] = 0x0; | |
192 | for (r = 0; r < PCI_NUM_REGIONS; ++r) { | |
71ebd6dc IY |
193 | PCIIORegion *region = &dev->io_regions[r]; |
194 | if (!region->size) { | |
c0b1905b MT |
195 | continue; |
196 | } | |
71ebd6dc IY |
197 | |
198 | if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) && | |
199 | region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { | |
200 | pci_set_quad(dev->config + pci_bar(dev, r), region->type); | |
201 | } else { | |
202 | pci_set_long(dev->config + pci_bar(dev, r), region->type); | |
203 | } | |
c0b1905b MT |
204 | } |
205 | pci_update_mappings(dev); | |
cbd2d434 JK |
206 | |
207 | msi_reset(dev); | |
208 | msix_reset(dev); | |
5330de09 MT |
209 | } |
210 | ||
9bb33586 IY |
211 | /* |
212 | * Trigger pci bus reset under a given bus. | |
213 | * To be called on RST# assert. | |
214 | */ | |
81e3e75b | 215 | static int pcibus_reset(BusState *qbus) |
6eaa6847 | 216 | { |
81e3e75b | 217 | PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus); |
6eaa6847 GN |
218 | int i; |
219 | ||
220 | for (i = 0; i < bus->nirq; i++) { | |
221 | bus->irq_count[i] = 0; | |
222 | } | |
5330de09 MT |
223 | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
224 | if (bus->devices[i]) { | |
225 | pci_device_reset(bus->devices[i]); | |
226 | } | |
6eaa6847 | 227 | } |
9bb33586 IY |
228 | |
229 | /* topology traverse is done by pci_bus_reset(). | |
230 | Tell qbus/qdev walker not to traverse the tree */ | |
231 | return 1; | |
232 | } | |
233 | ||
7588e2b0 | 234 | static void pci_host_bus_register(PCIBus *bus, DeviceState *parent) |
e822a52a | 235 | { |
7588e2b0 DG |
236 | PCIHostState *host_bridge = PCI_HOST_BRIDGE(parent); |
237 | ||
238 | QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next); | |
e822a52a IY |
239 | } |
240 | ||
1ef7a2a2 | 241 | PCIBus *pci_find_primary_bus(void) |
e822a52a | 242 | { |
9bc47305 | 243 | PCIBus *primary_bus = NULL; |
7588e2b0 | 244 | PCIHostState *host; |
e822a52a | 245 | |
7588e2b0 | 246 | QLIST_FOREACH(host, &pci_host_bridges, next) { |
9bc47305 DG |
247 | if (primary_bus) { |
248 | /* We have multiple root buses, refuse to select a primary */ | |
249 | return NULL; | |
e822a52a | 250 | } |
9bc47305 | 251 | primary_bus = host->bus; |
e822a52a IY |
252 | } |
253 | ||
9bc47305 | 254 | return primary_bus; |
e822a52a IY |
255 | } |
256 | ||
c473d18d | 257 | PCIBus *pci_device_root_bus(const PCIDevice *d) |
e075e788 | 258 | { |
c473d18d | 259 | PCIBus *bus = d->bus; |
e075e788 | 260 | |
e075e788 IY |
261 | while ((d = bus->parent_dev) != NULL) { |
262 | bus = d->bus; | |
263 | } | |
264 | ||
c473d18d DG |
265 | return bus; |
266 | } | |
267 | ||
568f0690 | 268 | const char *pci_root_bus_path(PCIDevice *dev) |
c473d18d | 269 | { |
568f0690 DG |
270 | PCIBus *rootbus = pci_device_root_bus(dev); |
271 | PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent); | |
272 | PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge); | |
c473d18d | 273 | |
568f0690 DG |
274 | assert(!rootbus->parent_dev); |
275 | assert(host_bridge->bus == rootbus); | |
276 | ||
277 | if (hc->root_bus_path) { | |
278 | return (*hc->root_bus_path)(host_bridge, rootbus); | |
e075e788 IY |
279 | } |
280 | ||
568f0690 | 281 | return rootbus->qbus.name; |
e075e788 IY |
282 | } |
283 | ||
4fec6404 | 284 | static void pci_bus_init(PCIBus *bus, DeviceState *parent, |
1e39101c | 285 | const char *name, |
aee97b84 AK |
286 | MemoryRegion *address_space_mem, |
287 | MemoryRegion *address_space_io, | |
1e39101c | 288 | uint8_t devfn_min) |
30468f78 | 289 | { |
6fa84913 | 290 | assert(PCI_FUNC(devfn_min) == 0); |
502a5395 | 291 | bus->devfn_min = devfn_min; |
5968eca3 AK |
292 | bus->address_space_mem = address_space_mem; |
293 | bus->address_space_io = address_space_io; | |
e822a52a IY |
294 | |
295 | /* host bridge */ | |
296 | QLIST_INIT(&bus->child); | |
2b8cc89a | 297 | |
7588e2b0 | 298 | pci_host_bus_register(bus, parent); |
e822a52a | 299 | |
0be71e32 | 300 | vmstate_register(NULL, -1, &vmstate_pcibus, bus); |
21eea4b3 GH |
301 | } |
302 | ||
8c0bf9e2 AW |
303 | bool pci_bus_is_express(PCIBus *bus) |
304 | { | |
305 | return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS); | |
306 | } | |
307 | ||
0889464a AW |
308 | bool pci_bus_is_root(PCIBus *bus) |
309 | { | |
310 | return !bus->parent_dev; | |
311 | } | |
312 | ||
dd301ca6 | 313 | void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, |
4fec6404 PB |
314 | const char *name, |
315 | MemoryRegion *address_space_mem, | |
316 | MemoryRegion *address_space_io, | |
60a0e443 | 317 | uint8_t devfn_min, const char *typename) |
4fec6404 | 318 | { |
fb17dfe0 | 319 | qbus_create_inplace(bus, bus_size, typename, parent, name); |
4fec6404 PB |
320 | pci_bus_init(bus, parent, name, address_space_mem, |
321 | address_space_io, devfn_min); | |
322 | } | |
323 | ||
1e39101c | 324 | PCIBus *pci_bus_new(DeviceState *parent, const char *name, |
aee97b84 AK |
325 | MemoryRegion *address_space_mem, |
326 | MemoryRegion *address_space_io, | |
60a0e443 | 327 | uint8_t devfn_min, const char *typename) |
21eea4b3 GH |
328 | { |
329 | PCIBus *bus; | |
330 | ||
60a0e443 | 331 | bus = PCI_BUS(qbus_create(typename, parent, name)); |
4fec6404 PB |
332 | pci_bus_init(bus, parent, name, address_space_mem, |
333 | address_space_io, devfn_min); | |
21eea4b3 GH |
334 | return bus; |
335 | } | |
336 | ||
337 | void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, | |
338 | void *irq_opaque, int nirq) | |
339 | { | |
340 | bus->set_irq = set_irq; | |
341 | bus->map_irq = map_irq; | |
342 | bus->irq_opaque = irq_opaque; | |
343 | bus->nirq = nirq; | |
7267c094 | 344 | bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0])); |
21eea4b3 GH |
345 | } |
346 | ||
87c30546 | 347 | void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev) |
ee995ffb GH |
348 | { |
349 | bus->qbus.allow_hotplug = 1; | |
350 | bus->hotplug = hotplug; | |
87c30546 | 351 | bus->hotplug_qdev = qdev; |
ee995ffb GH |
352 | } |
353 | ||
21eea4b3 GH |
354 | PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
355 | pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, | |
1e39101c | 356 | void *irq_opaque, |
aee97b84 AK |
357 | MemoryRegion *address_space_mem, |
358 | MemoryRegion *address_space_io, | |
60a0e443 | 359 | uint8_t devfn_min, int nirq, const char *typename) |
21eea4b3 GH |
360 | { |
361 | PCIBus *bus; | |
362 | ||
aee97b84 | 363 | bus = pci_bus_new(parent, name, address_space_mem, |
60a0e443 | 364 | address_space_io, devfn_min, typename); |
21eea4b3 | 365 | pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
30468f78 FB |
366 | return bus; |
367 | } | |
69b91039 | 368 | |
502a5395 PB |
369 | int pci_bus_num(PCIBus *s) |
370 | { | |
0889464a | 371 | if (pci_bus_is_root(s)) |
e94ff650 IY |
372 | return 0; /* pci host bridge */ |
373 | return s->parent_dev->config[PCI_SECONDARY_BUS]; | |
502a5395 PB |
374 | } |
375 | ||
5c397242 BD |
376 | static void pci_bus_finalize(Object *obj) |
377 | { | |
378 | PCIBus *bus = PCI_BUS(obj); | |
379 | vmstate_unregister(NULL, &vmstate_pcibus, bus); | |
380 | } | |
381 | ||
73534f2f | 382 | static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
30ca2aab | 383 | { |
73534f2f | 384 | PCIDevice *s = container_of(pv, PCIDevice, config); |
e78e9ae4 | 385 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s); |
a9f49946 | 386 | uint8_t *config; |
52fc1d83 AZ |
387 | int i; |
388 | ||
a9f49946 | 389 | assert(size == pci_config_size(s)); |
7267c094 | 390 | config = g_malloc(size); |
a9f49946 IY |
391 | |
392 | qemu_get_buffer(f, config, size); | |
393 | for (i = 0; i < size; ++i) { | |
f9aebe2e MT |
394 | if ((config[i] ^ s->config[i]) & |
395 | s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) { | |
7267c094 | 396 | g_free(config); |
bd4b65ee | 397 | return -EINVAL; |
a9f49946 IY |
398 | } |
399 | } | |
400 | memcpy(s->config, config, size); | |
bd4b65ee | 401 | |
1941d19c | 402 | pci_update_mappings(s); |
e78e9ae4 | 403 | if (pc->is_bridge) { |
f055e96b | 404 | PCIBridge *b = PCI_BRIDGE(s); |
e78e9ae4 DK |
405 | pci_bridge_update_mappings(b); |
406 | } | |
52fc1d83 | 407 | |
4ea375bf GH |
408 | memory_region_set_enabled(&s->bus_master_enable_region, |
409 | pci_get_word(s->config + PCI_COMMAND) | |
410 | & PCI_COMMAND_MASTER); | |
411 | ||
7267c094 | 412 | g_free(config); |
30ca2aab FB |
413 | return 0; |
414 | } | |
415 | ||
73534f2f | 416 | /* just put buffer */ |
84e2e3eb | 417 | static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
73534f2f | 418 | { |
dbe73d7f | 419 | const uint8_t **v = pv; |
a9f49946 | 420 | assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
dbe73d7f | 421 | qemu_put_buffer(f, *v, size); |
73534f2f JQ |
422 | } |
423 | ||
424 | static VMStateInfo vmstate_info_pci_config = { | |
425 | .name = "pci config", | |
426 | .get = get_pci_config_device, | |
427 | .put = put_pci_config_device, | |
428 | }; | |
429 | ||
d036bb21 MT |
430 | static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
431 | { | |
c3f8f611 | 432 | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
d036bb21 MT |
433 | uint32_t irq_state[PCI_NUM_PINS]; |
434 | int i; | |
435 | for (i = 0; i < PCI_NUM_PINS; ++i) { | |
436 | irq_state[i] = qemu_get_be32(f); | |
437 | if (irq_state[i] != 0x1 && irq_state[i] != 0) { | |
438 | fprintf(stderr, "irq state %d: must be 0 or 1.\n", | |
439 | irq_state[i]); | |
440 | return -EINVAL; | |
441 | } | |
442 | } | |
443 | ||
444 | for (i = 0; i < PCI_NUM_PINS; ++i) { | |
445 | pci_set_irq_state(s, i, irq_state[i]); | |
446 | } | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size) | |
452 | { | |
453 | int i; | |
c3f8f611 | 454 | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
d036bb21 MT |
455 | |
456 | for (i = 0; i < PCI_NUM_PINS; ++i) { | |
457 | qemu_put_be32(f, pci_irq_state(s, i)); | |
458 | } | |
459 | } | |
460 | ||
461 | static VMStateInfo vmstate_info_pci_irq_state = { | |
462 | .name = "pci irq state", | |
463 | .get = get_pci_irq_state, | |
464 | .put = put_pci_irq_state, | |
465 | }; | |
466 | ||
73534f2f JQ |
467 | const VMStateDescription vmstate_pci_device = { |
468 | .name = "PCIDevice", | |
469 | .version_id = 2, | |
470 | .minimum_version_id = 1, | |
471 | .minimum_version_id_old = 1, | |
472 | .fields = (VMStateField []) { | |
473 | VMSTATE_INT32_LE(version_id, PCIDevice), | |
a9f49946 IY |
474 | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0, |
475 | vmstate_info_pci_config, | |
476 | PCI_CONFIG_SPACE_SIZE), | |
d036bb21 MT |
477 | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2, |
478 | vmstate_info_pci_irq_state, | |
479 | PCI_NUM_PINS * sizeof(int32_t)), | |
a9f49946 IY |
480 | VMSTATE_END_OF_LIST() |
481 | } | |
482 | }; | |
483 | ||
484 | const VMStateDescription vmstate_pcie_device = { | |
1de53459 | 485 | .name = "PCIEDevice", |
a9f49946 IY |
486 | .version_id = 2, |
487 | .minimum_version_id = 1, | |
488 | .minimum_version_id_old = 1, | |
489 | .fields = (VMStateField []) { | |
490 | VMSTATE_INT32_LE(version_id, PCIDevice), | |
491 | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0, | |
492 | vmstate_info_pci_config, | |
493 | PCIE_CONFIG_SPACE_SIZE), | |
d036bb21 MT |
494 | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2, |
495 | vmstate_info_pci_irq_state, | |
496 | PCI_NUM_PINS * sizeof(int32_t)), | |
73534f2f JQ |
497 | VMSTATE_END_OF_LIST() |
498 | } | |
499 | }; | |
500 | ||
a9f49946 IY |
501 | static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
502 | { | |
503 | return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device; | |
504 | } | |
505 | ||
73534f2f JQ |
506 | void pci_device_save(PCIDevice *s, QEMUFile *f) |
507 | { | |
f9bf77dd MT |
508 | /* Clear interrupt status bit: it is implicit |
509 | * in irq_state which we are saving. | |
510 | * This makes us compatible with old devices | |
511 | * which never set or clear this bit. */ | |
512 | s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; | |
a9f49946 | 513 | vmstate_save_state(f, pci_get_vmstate(s), s); |
f9bf77dd MT |
514 | /* Restore the interrupt status bit. */ |
515 | pci_update_irq_status(s); | |
73534f2f JQ |
516 | } |
517 | ||
518 | int pci_device_load(PCIDevice *s, QEMUFile *f) | |
519 | { | |
f9bf77dd MT |
520 | int ret; |
521 | ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id); | |
522 | /* Restore the interrupt status bit. */ | |
523 | pci_update_irq_status(s); | |
524 | return ret; | |
73534f2f JQ |
525 | } |
526 | ||
5e434f4e | 527 | static void pci_set_default_subsystem_id(PCIDevice *pci_dev) |
d350d97d | 528 | { |
5e434f4e IY |
529 | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
530 | pci_default_sub_vendor_id); | |
531 | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, | |
532 | pci_default_sub_device_id); | |
d350d97d AL |
533 | } |
534 | ||
880345c4 | 535 | /* |
43c945f1 IY |
536 | * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL |
537 | * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error | |
880345c4 | 538 | */ |
6ac363b5 | 539 | int pci_parse_devaddr(const char *addr, int *domp, int *busp, |
43c945f1 | 540 | unsigned int *slotp, unsigned int *funcp) |
880345c4 AL |
541 | { |
542 | const char *p; | |
543 | char *e; | |
544 | unsigned long val; | |
545 | unsigned long dom = 0, bus = 0; | |
43c945f1 IY |
546 | unsigned int slot = 0; |
547 | unsigned int func = 0; | |
880345c4 AL |
548 | |
549 | p = addr; | |
550 | val = strtoul(p, &e, 16); | |
551 | if (e == p) | |
552 | return -1; | |
553 | if (*e == ':') { | |
554 | bus = val; | |
555 | p = e + 1; | |
556 | val = strtoul(p, &e, 16); | |
557 | if (e == p) | |
558 | return -1; | |
559 | if (*e == ':') { | |
560 | dom = bus; | |
561 | bus = val; | |
562 | p = e + 1; | |
563 | val = strtoul(p, &e, 16); | |
564 | if (e == p) | |
565 | return -1; | |
566 | } | |
567 | } | |
568 | ||
880345c4 AL |
569 | slot = val; |
570 | ||
43c945f1 IY |
571 | if (funcp != NULL) { |
572 | if (*e != '.') | |
573 | return -1; | |
574 | ||
575 | p = e + 1; | |
576 | val = strtoul(p, &e, 16); | |
577 | if (e == p) | |
578 | return -1; | |
579 | ||
580 | func = val; | |
581 | } | |
582 | ||
583 | /* if funcp == NULL func is 0 */ | |
584 | if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) | |
585 | return -1; | |
586 | ||
880345c4 AL |
587 | if (*e) |
588 | return -1; | |
589 | ||
880345c4 AL |
590 | *domp = dom; |
591 | *busp = bus; | |
592 | *slotp = slot; | |
43c945f1 IY |
593 | if (funcp != NULL) |
594 | *funcp = func; | |
880345c4 AL |
595 | return 0; |
596 | } | |
597 | ||
85c6e4fa | 598 | PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root, const char *devaddr) |
5607c388 MA |
599 | { |
600 | int dom, bus; | |
601 | unsigned slot; | |
602 | ||
85c6e4fa DG |
603 | assert(!root->parent_dev); |
604 | ||
1ef7a2a2 DG |
605 | if (!root) { |
606 | fprintf(stderr, "No primary PCI bus\n"); | |
607 | return NULL; | |
608 | } | |
609 | ||
5607c388 MA |
610 | if (!devaddr) { |
611 | *devfnp = -1; | |
1ef7a2a2 | 612 | return pci_find_bus_nr(root, 0); |
5607c388 MA |
613 | } |
614 | ||
43c945f1 | 615 | if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) { |
5607c388 MA |
616 | return NULL; |
617 | } | |
618 | ||
1ef7a2a2 DG |
619 | if (dom != 0) { |
620 | fprintf(stderr, "No support for non-zero PCI domains\n"); | |
621 | return NULL; | |
622 | } | |
623 | ||
6ff534b6 | 624 | *devfnp = PCI_DEVFN(slot, 0); |
1ef7a2a2 | 625 | return pci_find_bus_nr(root, bus); |
5607c388 MA |
626 | } |
627 | ||
bd4b65ee MT |
628 | static void pci_init_cmask(PCIDevice *dev) |
629 | { | |
630 | pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff); | |
631 | pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff); | |
632 | dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; | |
633 | dev->cmask[PCI_REVISION_ID] = 0xff; | |
634 | dev->cmask[PCI_CLASS_PROG] = 0xff; | |
635 | pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff); | |
636 | dev->cmask[PCI_HEADER_TYPE] = 0xff; | |
637 | dev->cmask[PCI_CAPABILITY_LIST] = 0xff; | |
638 | } | |
639 | ||
b7ee1603 MT |
640 | static void pci_init_wmask(PCIDevice *dev) |
641 | { | |
a9f49946 IY |
642 | int config_size = pci_config_size(dev); |
643 | ||
b7ee1603 MT |
644 | dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff; |
645 | dev->wmask[PCI_INTERRUPT_LINE] = 0xff; | |
67a51b48 | 646 | pci_set_word(dev->wmask + PCI_COMMAND, |
a7b15a5c MT |
647 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
648 | PCI_COMMAND_INTX_DISABLE); | |
b1aeb926 IY |
649 | if (dev->cap_present & QEMU_PCI_CAP_SERR) { |
650 | pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR); | |
651 | } | |
3e21ffc9 IY |
652 | |
653 | memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff, | |
654 | config_size - PCI_CONFIG_HEADER_SIZE); | |
b7ee1603 MT |
655 | } |
656 | ||
89d437df IY |
657 | static void pci_init_w1cmask(PCIDevice *dev) |
658 | { | |
659 | /* | |
f6bdfcc9 | 660 | * Note: It's okay to set w1cmask even for readonly bits as |
89d437df IY |
661 | * long as their value is hardwired to 0. |
662 | */ | |
663 | pci_set_word(dev->w1cmask + PCI_STATUS, | |
664 | PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT | | |
665 | PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT | | |
666 | PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY); | |
667 | } | |
668 | ||
d5f27e88 | 669 | static void pci_init_mask_bridge(PCIDevice *d) |
fb231628 IY |
670 | { |
671 | /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and | |
672 | PCI_SEC_LETENCY_TIMER */ | |
673 | memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); | |
674 | ||
675 | /* base and limit */ | |
676 | d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff; | |
677 | d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff; | |
678 | pci_set_word(d->wmask + PCI_MEMORY_BASE, | |
679 | PCI_MEMORY_RANGE_MASK & 0xffff); | |
680 | pci_set_word(d->wmask + PCI_MEMORY_LIMIT, | |
681 | PCI_MEMORY_RANGE_MASK & 0xffff); | |
682 | pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, | |
683 | PCI_PREF_RANGE_MASK & 0xffff); | |
684 | pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, | |
685 | PCI_PREF_RANGE_MASK & 0xffff); | |
686 | ||
687 | /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */ | |
688 | memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); | |
689 | ||
d5f27e88 | 690 | /* Supported memory and i/o types */ |
68917102 MT |
691 | d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16; |
692 | d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16; | |
d5f27e88 MT |
693 | pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE, |
694 | PCI_PREF_RANGE_TYPE_64); | |
695 | pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT, | |
696 | PCI_PREF_RANGE_TYPE_64); | |
697 | ||
45eb768c MT |
698 | /* |
699 | * TODO: Bridges default to 10-bit VGA decoding but we currently only | |
700 | * implement 16-bit decoding (no alias support). | |
701 | */ | |
f6bdfcc9 MT |
702 | pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, |
703 | PCI_BRIDGE_CTL_PARITY | | |
704 | PCI_BRIDGE_CTL_SERR | | |
705 | PCI_BRIDGE_CTL_ISA | | |
706 | PCI_BRIDGE_CTL_VGA | | |
707 | PCI_BRIDGE_CTL_VGA_16BIT | | |
708 | PCI_BRIDGE_CTL_MASTER_ABORT | | |
709 | PCI_BRIDGE_CTL_BUS_RESET | | |
710 | PCI_BRIDGE_CTL_FAST_BACK | | |
711 | PCI_BRIDGE_CTL_DISCARD | | |
712 | PCI_BRIDGE_CTL_SEC_DISCARD | | |
f6bdfcc9 MT |
713 | PCI_BRIDGE_CTL_DISCARD_SERR); |
714 | /* Below does not do anything as we never set this bit, put here for | |
715 | * completeness. */ | |
716 | pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL, | |
717 | PCI_BRIDGE_CTL_DISCARD_STATUS); | |
d5f27e88 | 718 | d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK; |
15ab7a75 | 719 | d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK; |
d5f27e88 MT |
720 | pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE, |
721 | PCI_PREF_RANGE_TYPE_MASK); | |
15ab7a75 MT |
722 | pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT, |
723 | PCI_PREF_RANGE_TYPE_MASK); | |
fb231628 IY |
724 | } |
725 | ||
6eab3de1 IY |
726 | static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) |
727 | { | |
728 | uint8_t slot = PCI_SLOT(dev->devfn); | |
729 | uint8_t func; | |
730 | ||
731 | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { | |
732 | dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; | |
733 | } | |
734 | ||
735 | /* | |
b0cd712c | 736 | * multifunction bit is interpreted in two ways as follows. |
6eab3de1 IY |
737 | * - all functions must set the bit to 1. |
738 | * Example: Intel X53 | |
739 | * - function 0 must set the bit, but the rest function (> 0) | |
740 | * is allowed to leave the bit to 0. | |
741 | * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10, | |
742 | * | |
743 | * So OS (at least Linux) checks the bit of only function 0, | |
744 | * and doesn't see the bit of function > 0. | |
745 | * | |
746 | * The below check allows both interpretation. | |
747 | */ | |
748 | if (PCI_FUNC(dev->devfn)) { | |
749 | PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)]; | |
750 | if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) { | |
751 | /* function 0 should set multifunction bit */ | |
752 | error_report("PCI: single function device can't be populated " | |
753 | "in function %x.%x", slot, PCI_FUNC(dev->devfn)); | |
754 | return -1; | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
759 | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { | |
760 | return 0; | |
761 | } | |
762 | /* function 0 indicates single function, so function > 0 must be NULL */ | |
763 | for (func = 1; func < PCI_FUNC_MAX; ++func) { | |
764 | if (bus->devices[PCI_DEVFN(slot, func)]) { | |
765 | error_report("PCI: %x.0 indicates single function, " | |
766 | "but %x.%x is already populated.", | |
767 | slot, slot, func); | |
768 | return -1; | |
769 | } | |
770 | } | |
771 | return 0; | |
772 | } | |
773 | ||
a9f49946 IY |
774 | static void pci_config_alloc(PCIDevice *pci_dev) |
775 | { | |
776 | int config_size = pci_config_size(pci_dev); | |
777 | ||
7267c094 AL |
778 | pci_dev->config = g_malloc0(config_size); |
779 | pci_dev->cmask = g_malloc0(config_size); | |
780 | pci_dev->wmask = g_malloc0(config_size); | |
781 | pci_dev->w1cmask = g_malloc0(config_size); | |
782 | pci_dev->used = g_malloc0(config_size); | |
a9f49946 IY |
783 | } |
784 | ||
785 | static void pci_config_free(PCIDevice *pci_dev) | |
786 | { | |
7267c094 AL |
787 | g_free(pci_dev->config); |
788 | g_free(pci_dev->cmask); | |
789 | g_free(pci_dev->wmask); | |
790 | g_free(pci_dev->w1cmask); | |
791 | g_free(pci_dev->used); | |
a9f49946 IY |
792 | } |
793 | ||
69b91039 | 794 | /* -1 for devfn means auto assign */ |
6b1b92d3 | 795 | static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus, |
40021f08 | 796 | const char *name, int devfn) |
69b91039 | 797 | { |
40021f08 AL |
798 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); |
799 | PCIConfigReadFunc *config_read = pc->config_read; | |
800 | PCIConfigWriteFunc *config_write = pc->config_write; | |
e00387d5 | 801 | AddressSpace *dma_as; |
113f89df | 802 | |
69b91039 | 803 | if (devfn < 0) { |
b47b0706 | 804 | for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices); |
6fa84913 | 805 | devfn += PCI_FUNC_MAX) { |
30468f78 | 806 | if (!bus->devices[devfn]) |
69b91039 FB |
807 | goto found; |
808 | } | |
3709c1b7 | 809 | error_report("PCI: no slot/function available for %s, all in use", name); |
09e3acc6 | 810 | return NULL; |
69b91039 | 811 | found: ; |
07b7d053 | 812 | } else if (bus->devices[devfn]) { |
3709c1b7 DB |
813 | error_report("PCI: slot %d function %d not available for %s, in use by %s", |
814 | PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name); | |
09e3acc6 | 815 | return NULL; |
69b91039 | 816 | } |
e00387d5 | 817 | |
30468f78 | 818 | pci_dev->bus = bus; |
9eda7d37 | 819 | dma_as = pci_device_iommu_address_space(pci_dev); |
24addbc7 | 820 | |
40c5dce9 PB |
821 | memory_region_init_alias(&pci_dev->bus_master_enable_region, |
822 | OBJECT(pci_dev), "bus master", | |
e00387d5 AK |
823 | dma_as->root, 0, memory_region_size(dma_as->root)); |
824 | memory_region_set_enabled(&pci_dev->bus_master_enable_region, false); | |
7dca8043 AK |
825 | address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region, |
826 | name); | |
e00387d5 | 827 | |
69b91039 FB |
828 | pci_dev->devfn = devfn; |
829 | pstrcpy(pci_dev->name, sizeof(pci_dev->name), name); | |
d036bb21 | 830 | pci_dev->irq_state = 0; |
a9f49946 | 831 | pci_config_alloc(pci_dev); |
fb231628 | 832 | |
40021f08 AL |
833 | pci_config_set_vendor_id(pci_dev->config, pc->vendor_id); |
834 | pci_config_set_device_id(pci_dev->config, pc->device_id); | |
835 | pci_config_set_revision(pci_dev->config, pc->revision); | |
836 | pci_config_set_class(pci_dev->config, pc->class_id); | |
113f89df | 837 | |
40021f08 AL |
838 | if (!pc->is_bridge) { |
839 | if (pc->subsystem_vendor_id || pc->subsystem_id) { | |
113f89df | 840 | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
40021f08 | 841 | pc->subsystem_vendor_id); |
113f89df | 842 | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
40021f08 | 843 | pc->subsystem_id); |
113f89df IY |
844 | } else { |
845 | pci_set_default_subsystem_id(pci_dev); | |
846 | } | |
847 | } else { | |
848 | /* subsystem_vendor_id/subsystem_id are only for header type 0 */ | |
40021f08 AL |
849 | assert(!pc->subsystem_vendor_id); |
850 | assert(!pc->subsystem_id); | |
fb231628 | 851 | } |
bd4b65ee | 852 | pci_init_cmask(pci_dev); |
b7ee1603 | 853 | pci_init_wmask(pci_dev); |
89d437df | 854 | pci_init_w1cmask(pci_dev); |
40021f08 | 855 | if (pc->is_bridge) { |
d5f27e88 | 856 | pci_init_mask_bridge(pci_dev); |
fb231628 | 857 | } |
6eab3de1 IY |
858 | if (pci_init_multifunction(bus, pci_dev)) { |
859 | pci_config_free(pci_dev); | |
860 | return NULL; | |
861 | } | |
0ac32c83 FB |
862 | |
863 | if (!config_read) | |
864 | config_read = pci_default_read_config; | |
865 | if (!config_write) | |
866 | config_write = pci_default_write_config; | |
69b91039 FB |
867 | pci_dev->config_read = config_read; |
868 | pci_dev->config_write = config_write; | |
30468f78 | 869 | bus->devices[devfn] = pci_dev; |
f16c4abf | 870 | pci_dev->version_id = 2; /* Current pci device vmstate version */ |
69b91039 FB |
871 | return pci_dev; |
872 | } | |
873 | ||
925fe64a AW |
874 | static void do_pci_unregister_device(PCIDevice *pci_dev) |
875 | { | |
925fe64a AW |
876 | pci_dev->bus->devices[pci_dev->devfn] = NULL; |
877 | pci_config_free(pci_dev); | |
817dcc53 | 878 | |
e00387d5 AK |
879 | address_space_destroy(&pci_dev->bus_master_as); |
880 | memory_region_destroy(&pci_dev->bus_master_enable_region); | |
925fe64a AW |
881 | } |
882 | ||
5851e08c AL |
883 | static void pci_unregister_io_regions(PCIDevice *pci_dev) |
884 | { | |
885 | PCIIORegion *r; | |
886 | int i; | |
887 | ||
888 | for(i = 0; i < PCI_NUM_REGIONS; i++) { | |
889 | r = &pci_dev->io_regions[i]; | |
182f9c8a | 890 | if (!r->size || r->addr == PCI_BAR_UNMAPPED) |
5851e08c | 891 | continue; |
03952339 | 892 | memory_region_del_subregion(r->address_space, r->memory); |
5851e08c | 893 | } |
e01fd687 AW |
894 | |
895 | pci_unregister_vga(pci_dev); | |
5851e08c AL |
896 | } |
897 | ||
a36a344d | 898 | static int pci_unregister_device(DeviceState *dev) |
5851e08c | 899 | { |
40021f08 AL |
900 | PCIDevice *pci_dev = PCI_DEVICE(dev); |
901 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); | |
5851e08c AL |
902 | |
903 | pci_unregister_io_regions(pci_dev); | |
230741dc | 904 | pci_del_option_rom(pci_dev); |
7cf1b0fd | 905 | |
f90c2bcd AW |
906 | if (pc->exit) { |
907 | pc->exit(pci_dev); | |
908 | } | |
5851e08c | 909 | |
925fe64a | 910 | do_pci_unregister_device(pci_dev); |
5851e08c AL |
911 | return 0; |
912 | } | |
913 | ||
e824b2cc AK |
914 | void pci_register_bar(PCIDevice *pci_dev, int region_num, |
915 | uint8_t type, MemoryRegion *memory) | |
69b91039 FB |
916 | { |
917 | PCIIORegion *r; | |
d7ce493a | 918 | uint32_t addr; |
5a9ff381 | 919 | uint64_t wmask; |
cfc0be25 | 920 | pcibus_t size = memory_region_size(memory); |
a4c20c6a | 921 | |
2bbb9c2f IY |
922 | assert(region_num >= 0); |
923 | assert(region_num < PCI_NUM_REGIONS); | |
a4c20c6a AL |
924 | if (size & (size-1)) { |
925 | fprintf(stderr, "ERROR: PCI region size must be pow2 " | |
89e8b13c | 926 | "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
a4c20c6a AL |
927 | exit(1); |
928 | } | |
929 | ||
69b91039 | 930 | r = &pci_dev->io_regions[region_num]; |
182f9c8a | 931 | r->addr = PCI_BAR_UNMAPPED; |
69b91039 FB |
932 | r->size = size; |
933 | r->type = type; | |
79ff8cb0 | 934 | r->memory = NULL; |
b7ee1603 MT |
935 | |
936 | wmask = ~(size - 1); | |
b3b11697 | 937 | addr = pci_bar(pci_dev, region_num); |
d7ce493a | 938 | if (region_num == PCI_ROM_SLOT) { |
ebabb67a | 939 | /* ROM enable bit is writable */ |
5330de09 | 940 | wmask |= PCI_ROM_ADDRESS_ENABLE; |
d7ce493a | 941 | } |
b0ff8eb2 | 942 | pci_set_long(pci_dev->config + addr, type); |
14421258 IY |
943 | if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) && |
944 | r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { | |
945 | pci_set_quad(pci_dev->wmask + addr, wmask); | |
946 | pci_set_quad(pci_dev->cmask + addr, ~0ULL); | |
947 | } else { | |
948 | pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff); | |
949 | pci_set_long(pci_dev->cmask + addr, 0xffffffff); | |
950 | } | |
79ff8cb0 | 951 | pci_dev->io_regions[region_num].memory = memory; |
5968eca3 | 952 | pci_dev->io_regions[region_num].address_space |
cfc0be25 | 953 | = type & PCI_BASE_ADDRESS_SPACE_IO |
5968eca3 AK |
954 | ? pci_dev->bus->address_space_io |
955 | : pci_dev->bus->address_space_mem; | |
79ff8cb0 AK |
956 | } |
957 | ||
e01fd687 AW |
958 | static void pci_update_vga(PCIDevice *pci_dev) |
959 | { | |
960 | uint16_t cmd; | |
961 | ||
962 | if (!pci_dev->has_vga) { | |
963 | return; | |
964 | } | |
965 | ||
966 | cmd = pci_get_word(pci_dev->config + PCI_COMMAND); | |
967 | ||
968 | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM], | |
969 | cmd & PCI_COMMAND_MEMORY); | |
970 | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO], | |
971 | cmd & PCI_COMMAND_IO); | |
972 | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI], | |
973 | cmd & PCI_COMMAND_IO); | |
974 | } | |
975 | ||
976 | void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem, | |
977 | MemoryRegion *io_lo, MemoryRegion *io_hi) | |
978 | { | |
979 | assert(!pci_dev->has_vga); | |
980 | ||
981 | assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE); | |
982 | pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem; | |
983 | memory_region_add_subregion_overlap(pci_dev->bus->address_space_mem, | |
984 | QEMU_PCI_VGA_MEM_BASE, mem, 1); | |
985 | ||
986 | assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE); | |
987 | pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo; | |
988 | memory_region_add_subregion_overlap(pci_dev->bus->address_space_io, | |
989 | QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1); | |
990 | ||
991 | assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE); | |
992 | pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi; | |
993 | memory_region_add_subregion_overlap(pci_dev->bus->address_space_io, | |
994 | QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1); | |
995 | pci_dev->has_vga = true; | |
996 | ||
997 | pci_update_vga(pci_dev); | |
998 | } | |
999 | ||
1000 | void pci_unregister_vga(PCIDevice *pci_dev) | |
1001 | { | |
1002 | if (!pci_dev->has_vga) { | |
1003 | return; | |
1004 | } | |
1005 | ||
1006 | memory_region_del_subregion(pci_dev->bus->address_space_mem, | |
1007 | pci_dev->vga_regions[QEMU_PCI_VGA_MEM]); | |
1008 | memory_region_del_subregion(pci_dev->bus->address_space_io, | |
1009 | pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]); | |
1010 | memory_region_del_subregion(pci_dev->bus->address_space_io, | |
1011 | pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]); | |
1012 | pci_dev->has_vga = false; | |
1013 | } | |
1014 | ||
16a96f28 AK |
1015 | pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) |
1016 | { | |
1017 | return pci_dev->io_regions[region_num].addr; | |
1018 | } | |
1019 | ||
876a350d MT |
1020 | static pcibus_t pci_bar_address(PCIDevice *d, |
1021 | int reg, uint8_t type, pcibus_t size) | |
1022 | { | |
1023 | pcibus_t new_addr, last_addr; | |
1024 | int bar = pci_bar(d, reg); | |
1025 | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); | |
1026 | ||
1027 | if (type & PCI_BASE_ADDRESS_SPACE_IO) { | |
1028 | if (!(cmd & PCI_COMMAND_IO)) { | |
1029 | return PCI_BAR_UNMAPPED; | |
1030 | } | |
1031 | new_addr = pci_get_long(d->config + bar) & ~(size - 1); | |
1032 | last_addr = new_addr + size - 1; | |
9f1a029a HP |
1033 | /* Check if 32 bit BAR wraps around explicitly. |
1034 | * TODO: make priorities correct and remove this work around. | |
1035 | */ | |
1036 | if (last_addr <= new_addr || new_addr == 0 || last_addr >= UINT32_MAX) { | |
876a350d MT |
1037 | return PCI_BAR_UNMAPPED; |
1038 | } | |
1039 | return new_addr; | |
1040 | } | |
1041 | ||
1042 | if (!(cmd & PCI_COMMAND_MEMORY)) { | |
1043 | return PCI_BAR_UNMAPPED; | |
1044 | } | |
1045 | if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) { | |
1046 | new_addr = pci_get_quad(d->config + bar); | |
1047 | } else { | |
1048 | new_addr = pci_get_long(d->config + bar); | |
1049 | } | |
1050 | /* the ROM slot has a specific enable bit */ | |
1051 | if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) { | |
1052 | return PCI_BAR_UNMAPPED; | |
1053 | } | |
1054 | new_addr &= ~(size - 1); | |
1055 | last_addr = new_addr + size - 1; | |
1056 | /* NOTE: we do not support wrapping */ | |
1057 | /* XXX: as we cannot support really dynamic | |
1058 | mappings, we handle specific values as invalid | |
1059 | mappings. */ | |
1060 | if (last_addr <= new_addr || new_addr == 0 || | |
1061 | last_addr == PCI_BAR_UNMAPPED) { | |
1062 | return PCI_BAR_UNMAPPED; | |
1063 | } | |
1064 | ||
1065 | /* Now pcibus_t is 64bit. | |
1066 | * Check if 32 bit BAR wraps around explicitly. | |
1067 | * Without this, PC ide doesn't work well. | |
1068 | * TODO: remove this work around. | |
1069 | */ | |
1070 | if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) { | |
1071 | return PCI_BAR_UNMAPPED; | |
1072 | } | |
1073 | ||
1074 | /* | |
1075 | * OS is allowed to set BAR beyond its addressable | |
1076 | * bits. For example, 32 bit OS can set 64bit bar | |
1077 | * to >4G. Check it. TODO: we might need to support | |
1078 | * it in the future for e.g. PAE. | |
1079 | */ | |
a8170e5e | 1080 | if (last_addr >= HWADDR_MAX) { |
876a350d MT |
1081 | return PCI_BAR_UNMAPPED; |
1082 | } | |
1083 | ||
1084 | return new_addr; | |
1085 | } | |
1086 | ||
0ac32c83 FB |
1087 | static void pci_update_mappings(PCIDevice *d) |
1088 | { | |
1089 | PCIIORegion *r; | |
876a350d | 1090 | int i; |
7df32ca0 | 1091 | pcibus_t new_addr; |
3b46e624 | 1092 | |
8a8696a3 | 1093 | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
0ac32c83 | 1094 | r = &d->io_regions[i]; |
a9688570 IY |
1095 | |
1096 | /* this region isn't registered */ | |
ec503442 | 1097 | if (!r->size) |
a9688570 IY |
1098 | continue; |
1099 | ||
876a350d | 1100 | new_addr = pci_bar_address(d, i, r->type, r->size); |
a9688570 IY |
1101 | |
1102 | /* This bar isn't changed */ | |
7df32ca0 | 1103 | if (new_addr == r->addr) |
a9688570 IY |
1104 | continue; |
1105 | ||
1106 | /* now do the real mapping */ | |
1107 | if (r->addr != PCI_BAR_UNMAPPED) { | |
03952339 | 1108 | memory_region_del_subregion(r->address_space, r->memory); |
0ac32c83 | 1109 | } |
a9688570 IY |
1110 | r->addr = new_addr; |
1111 | if (r->addr != PCI_BAR_UNMAPPED) { | |
8b881e77 AK |
1112 | memory_region_add_subregion_overlap(r->address_space, |
1113 | r->addr, r->memory, 1); | |
a9688570 | 1114 | } |
0ac32c83 | 1115 | } |
e01fd687 AW |
1116 | |
1117 | pci_update_vga(d); | |
0ac32c83 FB |
1118 | } |
1119 | ||
a7b15a5c MT |
1120 | static inline int pci_irq_disabled(PCIDevice *d) |
1121 | { | |
1122 | return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE; | |
1123 | } | |
1124 | ||
1125 | /* Called after interrupt disabled field update in config space, | |
1126 | * assert/deassert interrupts if necessary. | |
1127 | * Gets original interrupt disable bit value (before update). */ | |
1128 | static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) | |
1129 | { | |
1130 | int i, disabled = pci_irq_disabled(d); | |
1131 | if (disabled == was_irq_disabled) | |
1132 | return; | |
1133 | for (i = 0; i < PCI_NUM_PINS; ++i) { | |
1134 | int state = pci_irq_state(d, i); | |
1135 | pci_change_irq_level(d, i, disabled ? -state : state); | |
1136 | } | |
1137 | } | |
1138 | ||
5fafdf24 | 1139 | uint32_t pci_default_read_config(PCIDevice *d, |
0ac32c83 | 1140 | uint32_t address, int len) |
69b91039 | 1141 | { |
5029fe12 | 1142 | uint32_t val = 0; |
42e4126b | 1143 | |
5029fe12 IY |
1144 | memcpy(&val, d->config + address, len); |
1145 | return le32_to_cpu(val); | |
0ac32c83 FB |
1146 | } |
1147 | ||
b7ee1603 | 1148 | void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
0ac32c83 | 1149 | { |
a7b15a5c | 1150 | int i, was_irq_disabled = pci_irq_disabled(d); |
0ac32c83 | 1151 | |
42e4126b | 1152 | for (i = 0; i < l; val >>= 8, ++i) { |
91011d4f | 1153 | uint8_t wmask = d->wmask[addr + i]; |
92ba5f51 IY |
1154 | uint8_t w1cmask = d->w1cmask[addr + i]; |
1155 | assert(!(wmask & w1cmask)); | |
91011d4f | 1156 | d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
92ba5f51 | 1157 | d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */ |
0ac32c83 | 1158 | } |
260c0cd3 | 1159 | if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
edb00035 IY |
1160 | ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) || |
1161 | ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) || | |
260c0cd3 | 1162 | range_covers_byte(addr, l, PCI_COMMAND)) |
0ac32c83 | 1163 | pci_update_mappings(d); |
a7b15a5c | 1164 | |
1c380f94 | 1165 | if (range_covers_byte(addr, l, PCI_COMMAND)) { |
a7b15a5c | 1166 | pci_update_irq_disabled(d, was_irq_disabled); |
1c380f94 AK |
1167 | memory_region_set_enabled(&d->bus_master_enable_region, |
1168 | pci_get_word(d->config + PCI_COMMAND) | |
1169 | & PCI_COMMAND_MASTER); | |
1170 | } | |
95d65800 JK |
1171 | |
1172 | msi_write_config(d, addr, val, l); | |
1173 | msix_write_config(d, addr, val, l); | |
69b91039 FB |
1174 | } |
1175 | ||
502a5395 PB |
1176 | /***********************************************************/ |
1177 | /* generic PCI irq support */ | |
30468f78 | 1178 | |
502a5395 | 1179 | /* 0 <= irq_num <= 3. level must be 0 or 1 */ |
d98f08f5 | 1180 | static void pci_irq_handler(void *opaque, int irq_num, int level) |
69b91039 | 1181 | { |
a60380a5 | 1182 | PCIDevice *pci_dev = opaque; |
80b3ada7 | 1183 | int change; |
3b46e624 | 1184 | |
d036bb21 | 1185 | change = level - pci_irq_state(pci_dev, irq_num); |
80b3ada7 PB |
1186 | if (!change) |
1187 | return; | |
d2b59317 | 1188 | |
d036bb21 | 1189 | pci_set_irq_state(pci_dev, irq_num, level); |
f9bf77dd | 1190 | pci_update_irq_status(pci_dev); |
a7b15a5c MT |
1191 | if (pci_irq_disabled(pci_dev)) |
1192 | return; | |
d036bb21 | 1193 | pci_change_irq_level(pci_dev, irq_num, change); |
69b91039 FB |
1194 | } |
1195 | ||
d98f08f5 MA |
1196 | static inline int pci_intx(PCIDevice *pci_dev) |
1197 | { | |
1198 | return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1; | |
1199 | } | |
1200 | ||
1201 | qemu_irq pci_allocate_irq(PCIDevice *pci_dev) | |
1202 | { | |
1203 | int intx = pci_intx(pci_dev); | |
1204 | ||
1205 | return qemu_allocate_irq(pci_irq_handler, pci_dev, intx); | |
1206 | } | |
1207 | ||
1208 | void pci_set_irq(PCIDevice *pci_dev, int level) | |
1209 | { | |
1210 | int intx = pci_intx(pci_dev); | |
1211 | pci_irq_handler(pci_dev, intx, level); | |
1212 | } | |
1213 | ||
3afa9bb4 MT |
1214 | /* Special hooks used by device assignment */ |
1215 | void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq) | |
1216 | { | |
0889464a | 1217 | assert(pci_bus_is_root(bus)); |
3afa9bb4 MT |
1218 | bus->route_intx_to_irq = route_intx_to_irq; |
1219 | } | |
1220 | ||
1221 | PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin) | |
1222 | { | |
1223 | PCIBus *bus; | |
1224 | ||
1225 | do { | |
1226 | bus = dev->bus; | |
1227 | pin = bus->map_irq(dev, pin); | |
1228 | dev = bus->parent_dev; | |
1229 | } while (dev); | |
05c0621e AW |
1230 | |
1231 | if (!bus->route_intx_to_irq) { | |
312fd5f2 | 1232 | error_report("PCI: Bug - unimplemented PCI INTx routing (%s)", |
05c0621e AW |
1233 | object_get_typename(OBJECT(bus->qbus.parent))); |
1234 | return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 }; | |
1235 | } | |
1236 | ||
3afa9bb4 | 1237 | return bus->route_intx_to_irq(bus->irq_opaque, pin); |
0ae16251 JK |
1238 | } |
1239 | ||
d6e65d54 AW |
1240 | bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new) |
1241 | { | |
1242 | return old->mode != new->mode || old->irq != new->irq; | |
1243 | } | |
1244 | ||
0ae16251 JK |
1245 | void pci_bus_fire_intx_routing_notifier(PCIBus *bus) |
1246 | { | |
1247 | PCIDevice *dev; | |
1248 | PCIBus *sec; | |
1249 | int i; | |
1250 | ||
1251 | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { | |
1252 | dev = bus->devices[i]; | |
1253 | if (dev && dev->intx_routing_notifier) { | |
1254 | dev->intx_routing_notifier(dev); | |
1255 | } | |
e5368f0d AW |
1256 | } |
1257 | ||
1258 | QLIST_FOREACH(sec, &bus->child, sibling) { | |
1259 | pci_bus_fire_intx_routing_notifier(sec); | |
0ae16251 JK |
1260 | } |
1261 | } | |
1262 | ||
1263 | void pci_device_set_intx_routing_notifier(PCIDevice *dev, | |
1264 | PCIINTxRoutingNotifier notifier) | |
1265 | { | |
1266 | dev->intx_routing_notifier = notifier; | |
69b91039 FB |
1267 | } |
1268 | ||
91e56159 IY |
1269 | /* |
1270 | * PCI-to-PCI bridge specification | |
1271 | * 9.1: Interrupt routing. Table 9-1 | |
1272 | * | |
1273 | * the PCI Express Base Specification, Revision 2.1 | |
1274 | * 2.2.8.1: INTx interrutp signaling - Rules | |
1275 | * the Implementation Note | |
1276 | * Table 2-20 | |
1277 | */ | |
1278 | /* | |
1279 | * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD | |
1280 | * 0-origin unlike PCI interrupt pin register. | |
1281 | */ | |
1282 | int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin) | |
1283 | { | |
1284 | return (pin + PCI_SLOT(pci_dev->devfn)) % PCI_NUM_PINS; | |
1285 | } | |
1286 | ||
502a5395 PB |
1287 | /***********************************************************/ |
1288 | /* monitor info on PCI */ | |
0ac32c83 | 1289 | |
6650ee6d PB |
1290 | typedef struct { |
1291 | uint16_t class; | |
1292 | const char *desc; | |
5e0259e7 GN |
1293 | const char *fw_name; |
1294 | uint16_t fw_ign_bits; | |
6650ee6d PB |
1295 | } pci_class_desc; |
1296 | ||
09bc878a | 1297 | static const pci_class_desc pci_class_descriptions[] = |
6650ee6d | 1298 | { |
5e0259e7 GN |
1299 | { 0x0001, "VGA controller", "display"}, |
1300 | { 0x0100, "SCSI controller", "scsi"}, | |
1301 | { 0x0101, "IDE controller", "ide"}, | |
1302 | { 0x0102, "Floppy controller", "fdc"}, | |
1303 | { 0x0103, "IPI controller", "ipi"}, | |
1304 | { 0x0104, "RAID controller", "raid"}, | |
dcb5b19a TS |
1305 | { 0x0106, "SATA controller"}, |
1306 | { 0x0107, "SAS controller"}, | |
1307 | { 0x0180, "Storage controller"}, | |
5e0259e7 GN |
1308 | { 0x0200, "Ethernet controller", "ethernet"}, |
1309 | { 0x0201, "Token Ring controller", "token-ring"}, | |
1310 | { 0x0202, "FDDI controller", "fddi"}, | |
1311 | { 0x0203, "ATM controller", "atm"}, | |
dcb5b19a | 1312 | { 0x0280, "Network controller"}, |
5e0259e7 | 1313 | { 0x0300, "VGA controller", "display", 0x00ff}, |
dcb5b19a TS |
1314 | { 0x0301, "XGA controller"}, |
1315 | { 0x0302, "3D controller"}, | |
1316 | { 0x0380, "Display controller"}, | |
5e0259e7 GN |
1317 | { 0x0400, "Video controller", "video"}, |
1318 | { 0x0401, "Audio controller", "sound"}, | |
dcb5b19a | 1319 | { 0x0402, "Phone"}, |
602ef4d9 | 1320 | { 0x0403, "Audio controller", "sound"}, |
dcb5b19a | 1321 | { 0x0480, "Multimedia controller"}, |
5e0259e7 GN |
1322 | { 0x0500, "RAM controller", "memory"}, |
1323 | { 0x0501, "Flash controller", "flash"}, | |
dcb5b19a | 1324 | { 0x0580, "Memory controller"}, |
5e0259e7 GN |
1325 | { 0x0600, "Host bridge", "host"}, |
1326 | { 0x0601, "ISA bridge", "isa"}, | |
1327 | { 0x0602, "EISA bridge", "eisa"}, | |
1328 | { 0x0603, "MC bridge", "mca"}, | |
4c41425d | 1329 | { 0x0604, "PCI bridge", "pci-bridge"}, |
5e0259e7 GN |
1330 | { 0x0605, "PCMCIA bridge", "pcmcia"}, |
1331 | { 0x0606, "NUBUS bridge", "nubus"}, | |
1332 | { 0x0607, "CARDBUS bridge", "cardbus"}, | |
dcb5b19a TS |
1333 | { 0x0608, "RACEWAY bridge"}, |
1334 | { 0x0680, "Bridge"}, | |
5e0259e7 GN |
1335 | { 0x0700, "Serial port", "serial"}, |
1336 | { 0x0701, "Parallel port", "parallel"}, | |
1337 | { 0x0800, "Interrupt controller", "interrupt-controller"}, | |
1338 | { 0x0801, "DMA controller", "dma-controller"}, | |
1339 | { 0x0802, "Timer", "timer"}, | |
1340 | { 0x0803, "RTC", "rtc"}, | |
1341 | { 0x0900, "Keyboard", "keyboard"}, | |
1342 | { 0x0901, "Pen", "pen"}, | |
1343 | { 0x0902, "Mouse", "mouse"}, | |
1344 | { 0x0A00, "Dock station", "dock", 0x00ff}, | |
1345 | { 0x0B00, "i386 cpu", "cpu", 0x00ff}, | |
1346 | { 0x0c00, "Fireware contorller", "fireware"}, | |
1347 | { 0x0c01, "Access bus controller", "access-bus"}, | |
1348 | { 0x0c02, "SSA controller", "ssa"}, | |
1349 | { 0x0c03, "USB controller", "usb"}, | |
1350 | { 0x0c04, "Fibre channel controller", "fibre-channel"}, | |
f7748569 | 1351 | { 0x0c05, "SMBus"}, |
6650ee6d PB |
1352 | { 0, NULL} |
1353 | }; | |
1354 | ||
163c8a59 | 1355 | static void pci_for_each_device_under_bus(PCIBus *bus, |
7aa8cbb9 AP |
1356 | void (*fn)(PCIBus *b, PCIDevice *d, |
1357 | void *opaque), | |
1358 | void *opaque) | |
30468f78 | 1359 | { |
163c8a59 LC |
1360 | PCIDevice *d; |
1361 | int devfn; | |
30468f78 | 1362 | |
163c8a59 LC |
1363 | for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1364 | d = bus->devices[devfn]; | |
1365 | if (d) { | |
7aa8cbb9 | 1366 | fn(bus, d, opaque); |
163c8a59 LC |
1367 | } |
1368 | } | |
1369 | } | |
1370 | ||
1371 | void pci_for_each_device(PCIBus *bus, int bus_num, | |
7aa8cbb9 AP |
1372 | void (*fn)(PCIBus *b, PCIDevice *d, void *opaque), |
1373 | void *opaque) | |
163c8a59 | 1374 | { |
d662210a | 1375 | bus = pci_find_bus_nr(bus, bus_num); |
163c8a59 LC |
1376 | |
1377 | if (bus) { | |
7aa8cbb9 | 1378 | pci_for_each_device_under_bus(bus, fn, opaque); |
163c8a59 LC |
1379 | } |
1380 | } | |
1381 | ||
79627472 | 1382 | static const pci_class_desc *get_class_desc(int class) |
163c8a59 | 1383 | { |
79627472 | 1384 | const pci_class_desc *desc; |
163c8a59 | 1385 | |
79627472 LC |
1386 | desc = pci_class_descriptions; |
1387 | while (desc->desc && class != desc->class) { | |
1388 | desc++; | |
30468f78 | 1389 | } |
b4dccd8d | 1390 | |
79627472 LC |
1391 | return desc; |
1392 | } | |
14421258 | 1393 | |
79627472 | 1394 | static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num); |
163c8a59 | 1395 | |
79627472 LC |
1396 | static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev) |
1397 | { | |
1398 | PciMemoryRegionList *head = NULL, *cur_item = NULL; | |
1399 | int i; | |
163c8a59 | 1400 | |
79627472 LC |
1401 | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1402 | const PCIIORegion *r = &dev->io_regions[i]; | |
1403 | PciMemoryRegionList *region; | |
1404 | ||
1405 | if (!r->size) { | |
1406 | continue; | |
502a5395 | 1407 | } |
163c8a59 | 1408 | |
79627472 LC |
1409 | region = g_malloc0(sizeof(*region)); |
1410 | region->value = g_malloc0(sizeof(*region->value)); | |
163c8a59 | 1411 | |
79627472 LC |
1412 | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) { |
1413 | region->value->type = g_strdup("io"); | |
1414 | } else { | |
1415 | region->value->type = g_strdup("memory"); | |
1416 | region->value->has_prefetch = true; | |
1417 | region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH); | |
1418 | region->value->has_mem_type_64 = true; | |
1419 | region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64); | |
d5e4acf7 | 1420 | } |
163c8a59 | 1421 | |
79627472 LC |
1422 | region->value->bar = i; |
1423 | region->value->address = r->addr; | |
1424 | region->value->size = r->size; | |
163c8a59 | 1425 | |
79627472 LC |
1426 | /* XXX: waiting for the qapi to support GSList */ |
1427 | if (!cur_item) { | |
1428 | head = cur_item = region; | |
1429 | } else { | |
1430 | cur_item->next = region; | |
1431 | cur_item = region; | |
163c8a59 | 1432 | } |
80b3ada7 | 1433 | } |
384d8876 | 1434 | |
79627472 | 1435 | return head; |
163c8a59 LC |
1436 | } |
1437 | ||
79627472 LC |
1438 | static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus, |
1439 | int bus_num) | |
163c8a59 | 1440 | { |
79627472 | 1441 | PciBridgeInfo *info; |
163c8a59 | 1442 | |
79627472 | 1443 | info = g_malloc0(sizeof(*info)); |
163c8a59 | 1444 | |
79627472 LC |
1445 | info->bus.number = dev->config[PCI_PRIMARY_BUS]; |
1446 | info->bus.secondary = dev->config[PCI_SECONDARY_BUS]; | |
1447 | info->bus.subordinate = dev->config[PCI_SUBORDINATE_BUS]; | |
163c8a59 | 1448 | |
79627472 LC |
1449 | info->bus.io_range = g_malloc0(sizeof(*info->bus.io_range)); |
1450 | info->bus.io_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO); | |
1451 | info->bus.io_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO); | |
163c8a59 | 1452 | |
79627472 LC |
1453 | info->bus.memory_range = g_malloc0(sizeof(*info->bus.memory_range)); |
1454 | info->bus.memory_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); | |
1455 | info->bus.memory_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); | |
163c8a59 | 1456 | |
79627472 LC |
1457 | info->bus.prefetchable_range = g_malloc0(sizeof(*info->bus.prefetchable_range)); |
1458 | info->bus.prefetchable_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); | |
1459 | info->bus.prefetchable_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); | |
163c8a59 | 1460 | |
79627472 | 1461 | if (dev->config[PCI_SECONDARY_BUS] != 0) { |
d662210a | 1462 | PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]); |
79627472 LC |
1463 | if (child_bus) { |
1464 | info->has_devices = true; | |
1465 | info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]); | |
1466 | } | |
163c8a59 LC |
1467 | } |
1468 | ||
79627472 | 1469 | return info; |
163c8a59 LC |
1470 | } |
1471 | ||
79627472 LC |
1472 | static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus, |
1473 | int bus_num) | |
163c8a59 | 1474 | { |
79627472 LC |
1475 | const pci_class_desc *desc; |
1476 | PciDeviceInfo *info; | |
b5937f29 | 1477 | uint8_t type; |
79627472 | 1478 | int class; |
163c8a59 | 1479 | |
79627472 LC |
1480 | info = g_malloc0(sizeof(*info)); |
1481 | info->bus = bus_num; | |
1482 | info->slot = PCI_SLOT(dev->devfn); | |
1483 | info->function = PCI_FUNC(dev->devfn); | |
1484 | ||
1485 | class = pci_get_word(dev->config + PCI_CLASS_DEVICE); | |
6f88009e | 1486 | info->class_info.q_class = class; |
79627472 LC |
1487 | desc = get_class_desc(class); |
1488 | if (desc->desc) { | |
1489 | info->class_info.has_desc = true; | |
1490 | info->class_info.desc = g_strdup(desc->desc); | |
1491 | } | |
1492 | ||
1493 | info->id.vendor = pci_get_word(dev->config + PCI_VENDOR_ID); | |
1494 | info->id.device = pci_get_word(dev->config + PCI_DEVICE_ID); | |
1495 | info->regions = qmp_query_pci_regions(dev); | |
1496 | info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : ""); | |
163c8a59 LC |
1497 | |
1498 | if (dev->config[PCI_INTERRUPT_PIN] != 0) { | |
79627472 LC |
1499 | info->has_irq = true; |
1500 | info->irq = dev->config[PCI_INTERRUPT_LINE]; | |
163c8a59 LC |
1501 | } |
1502 | ||
b5937f29 IY |
1503 | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
1504 | if (type == PCI_HEADER_TYPE_BRIDGE) { | |
79627472 LC |
1505 | info->has_pci_bridge = true; |
1506 | info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num); | |
163c8a59 LC |
1507 | } |
1508 | ||
79627472 | 1509 | return info; |
163c8a59 LC |
1510 | } |
1511 | ||
79627472 | 1512 | static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num) |
384d8876 | 1513 | { |
79627472 | 1514 | PciDeviceInfoList *info, *head = NULL, *cur_item = NULL; |
163c8a59 | 1515 | PCIDevice *dev; |
79627472 | 1516 | int devfn; |
163c8a59 LC |
1517 | |
1518 | for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { | |
1519 | dev = bus->devices[devfn]; | |
1520 | if (dev) { | |
79627472 LC |
1521 | info = g_malloc0(sizeof(*info)); |
1522 | info->value = qmp_query_pci_device(dev, bus, bus_num); | |
1523 | ||
1524 | /* XXX: waiting for the qapi to support GSList */ | |
1525 | if (!cur_item) { | |
1526 | head = cur_item = info; | |
1527 | } else { | |
1528 | cur_item->next = info; | |
1529 | cur_item = info; | |
1530 | } | |
163c8a59 | 1531 | } |
1074df4f | 1532 | } |
163c8a59 | 1533 | |
79627472 | 1534 | return head; |
1074df4f IY |
1535 | } |
1536 | ||
79627472 | 1537 | static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num) |
1074df4f | 1538 | { |
79627472 LC |
1539 | PciInfo *info = NULL; |
1540 | ||
d662210a | 1541 | bus = pci_find_bus_nr(bus, bus_num); |
502a5395 | 1542 | if (bus) { |
79627472 LC |
1543 | info = g_malloc0(sizeof(*info)); |
1544 | info->bus = bus_num; | |
1545 | info->devices = qmp_query_pci_devices(bus, bus_num); | |
f2aa58c6 | 1546 | } |
163c8a59 | 1547 | |
79627472 | 1548 | return info; |
f2aa58c6 FB |
1549 | } |
1550 | ||
79627472 | 1551 | PciInfoList *qmp_query_pci(Error **errp) |
f2aa58c6 | 1552 | { |
79627472 | 1553 | PciInfoList *info, *head = NULL, *cur_item = NULL; |
7588e2b0 | 1554 | PCIHostState *host_bridge; |
163c8a59 | 1555 | |
7588e2b0 | 1556 | QLIST_FOREACH(host_bridge, &pci_host_bridges, next) { |
79627472 | 1557 | info = g_malloc0(sizeof(*info)); |
7588e2b0 | 1558 | info->value = qmp_query_pci_bus(host_bridge->bus, 0); |
79627472 LC |
1559 | |
1560 | /* XXX: waiting for the qapi to support GSList */ | |
1561 | if (!cur_item) { | |
1562 | head = cur_item = info; | |
1563 | } else { | |
1564 | cur_item->next = info; | |
1565 | cur_item = info; | |
163c8a59 | 1566 | } |
e822a52a | 1567 | } |
163c8a59 | 1568 | |
79627472 | 1569 | return head; |
77d4bc34 | 1570 | } |
a41b2ff2 | 1571 | |
cb457d76 AL |
1572 | static const char * const pci_nic_models[] = { |
1573 | "ne2k_pci", | |
1574 | "i82551", | |
1575 | "i82557b", | |
1576 | "i82559er", | |
1577 | "rtl8139", | |
1578 | "e1000", | |
1579 | "pcnet", | |
1580 | "virtio", | |
1581 | NULL | |
1582 | }; | |
1583 | ||
9d07d757 PB |
1584 | static const char * const pci_nic_names[] = { |
1585 | "ne2k_pci", | |
1586 | "i82551", | |
1587 | "i82557b", | |
1588 | "i82559er", | |
1589 | "rtl8139", | |
1590 | "e1000", | |
1591 | "pcnet", | |
53c25cea | 1592 | "virtio-net-pci", |
cb457d76 AL |
1593 | NULL |
1594 | }; | |
1595 | ||
a41b2ff2 | 1596 | /* Initialize a PCI NIC. */ |
33e66b86 | 1597 | /* FIXME callers should check for failure, but don't */ |
29b358f9 DG |
1598 | PCIDevice *pci_nic_init(NICInfo *nd, PCIBus *rootbus, |
1599 | const char *default_model, | |
5607c388 | 1600 | const char *default_devaddr) |
a41b2ff2 | 1601 | { |
5607c388 | 1602 | const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
07caea31 MA |
1603 | PCIBus *bus; |
1604 | int devfn; | |
5607c388 | 1605 | PCIDevice *pci_dev; |
9d07d757 | 1606 | DeviceState *dev; |
cb457d76 AL |
1607 | int i; |
1608 | ||
07caea31 MA |
1609 | i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1610 | if (i < 0) | |
1611 | return NULL; | |
1612 | ||
29b358f9 | 1613 | bus = pci_get_bus_devfn(&devfn, rootbus, devaddr); |
07caea31 | 1614 | if (!bus) { |
1ecda02b MA |
1615 | error_report("Invalid PCI device address %s for device %s", |
1616 | devaddr, pci_nic_names[i]); | |
07caea31 MA |
1617 | return NULL; |
1618 | } | |
1619 | ||
499cf102 | 1620 | pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
9ee05825 | 1621 | dev = &pci_dev->qdev; |
1cc33683 | 1622 | qdev_set_nic_properties(dev, nd); |
07caea31 MA |
1623 | if (qdev_init(dev) < 0) |
1624 | return NULL; | |
9ee05825 | 1625 | return pci_dev; |
a41b2ff2 PB |
1626 | } |
1627 | ||
29b358f9 DG |
1628 | PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, |
1629 | const char *default_model, | |
07caea31 MA |
1630 | const char *default_devaddr) |
1631 | { | |
1632 | PCIDevice *res; | |
1633 | ||
1634 | if (qemu_show_nic_models(nd->model, pci_nic_models)) | |
1635 | exit(0); | |
1636 | ||
29b358f9 | 1637 | res = pci_nic_init(nd, rootbus, default_model, default_devaddr); |
07caea31 MA |
1638 | if (!res) |
1639 | exit(1); | |
1640 | return res; | |
1641 | } | |
1642 | ||
129d42fb AJ |
1643 | PCIDevice *pci_vga_init(PCIBus *bus) |
1644 | { | |
1645 | switch (vga_interface_type) { | |
1646 | case VGA_CIRRUS: | |
1647 | return pci_create_simple(bus, -1, "cirrus-vga"); | |
1648 | case VGA_QXL: | |
1649 | return pci_create_simple(bus, -1, "qxl-vga"); | |
1650 | case VGA_STD: | |
1651 | return pci_create_simple(bus, -1, "VGA"); | |
1652 | case VGA_VMWARE: | |
1653 | return pci_create_simple(bus, -1, "vmware-svga"); | |
1654 | case VGA_NONE: | |
1655 | default: /* Other non-PCI types. Checking for unsupported types is already | |
1656 | done in vl.c. */ | |
1657 | return NULL; | |
1658 | } | |
1659 | } | |
1660 | ||
929176c3 MT |
1661 | /* Whether a given bus number is in range of the secondary |
1662 | * bus of the given bridge device. */ | |
1663 | static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num) | |
1664 | { | |
1665 | return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) & | |
1666 | PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ && | |
1667 | dev->config[PCI_SECONDARY_BUS] < bus_num && | |
1668 | bus_num <= dev->config[PCI_SUBORDINATE_BUS]; | |
1669 | } | |
1670 | ||
d662210a | 1671 | static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num) |
3ae80618 | 1672 | { |
470e6363 | 1673 | PCIBus *sec; |
3ae80618 | 1674 | |
470e6363 | 1675 | if (!bus) { |
e822a52a | 1676 | return NULL; |
470e6363 | 1677 | } |
3ae80618 | 1678 | |
e822a52a IY |
1679 | if (pci_bus_num(bus) == bus_num) { |
1680 | return bus; | |
1681 | } | |
1682 | ||
929176c3 | 1683 | /* Consider all bus numbers in range for the host pci bridge. */ |
0889464a | 1684 | if (!pci_bus_is_root(bus) && |
929176c3 MT |
1685 | !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) { |
1686 | return NULL; | |
1687 | } | |
1688 | ||
e822a52a | 1689 | /* try child bus */ |
929176c3 MT |
1690 | for (; bus; bus = sec) { |
1691 | QLIST_FOREACH(sec, &bus->child, sibling) { | |
0889464a | 1692 | assert(!pci_bus_is_root(sec)); |
929176c3 MT |
1693 | if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) { |
1694 | return sec; | |
1695 | } | |
1696 | if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) { | |
1697 | break; | |
c021f8e6 | 1698 | } |
e822a52a IY |
1699 | } |
1700 | } | |
1701 | ||
1702 | return NULL; | |
3ae80618 AL |
1703 | } |
1704 | ||
5256d8bf | 1705 | PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) |
3ae80618 | 1706 | { |
d662210a | 1707 | bus = pci_find_bus_nr(bus, bus_num); |
3ae80618 AL |
1708 | |
1709 | if (!bus) | |
1710 | return NULL; | |
1711 | ||
5256d8bf | 1712 | return bus->devices[devfn]; |
3ae80618 AL |
1713 | } |
1714 | ||
d307af79 | 1715 | static int pci_qdev_init(DeviceState *qdev) |
6b1b92d3 PB |
1716 | { |
1717 | PCIDevice *pci_dev = (PCIDevice *)qdev; | |
40021f08 | 1718 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); |
6b1b92d3 | 1719 | PCIBus *bus; |
113f89df | 1720 | int rc; |
ab85ceb1 | 1721 | bool is_default_rom; |
6b1b92d3 | 1722 | |
a9f49946 | 1723 | /* initialize cap_present for pci_is_express() and pci_config_size() */ |
40021f08 | 1724 | if (pc->is_express) { |
a9f49946 IY |
1725 | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1726 | } | |
1727 | ||
fef7fbc9 | 1728 | bus = PCI_BUS(qdev_get_parent_bus(qdev)); |
6e008585 AL |
1729 | pci_dev = do_pci_register_device(pci_dev, bus, |
1730 | object_get_typename(OBJECT(qdev)), | |
1731 | pci_dev->devfn); | |
09e3acc6 GH |
1732 | if (pci_dev == NULL) |
1733 | return -1; | |
40021f08 | 1734 | if (qdev->hotplugged && pc->no_hotplug) { |
f79f2bfc | 1735 | qerror_report(QERR_DEVICE_NO_HOTPLUG, object_get_typename(OBJECT(pci_dev))); |
180c22e1 GH |
1736 | do_pci_unregister_device(pci_dev); |
1737 | return -1; | |
1738 | } | |
40021f08 AL |
1739 | if (pc->init) { |
1740 | rc = pc->init(pci_dev); | |
c2afc922 IY |
1741 | if (rc != 0) { |
1742 | do_pci_unregister_device(pci_dev); | |
1743 | return rc; | |
1744 | } | |
925fe64a | 1745 | } |
8c52c8f3 GH |
1746 | |
1747 | /* rom loading */ | |
ab85ceb1 | 1748 | is_default_rom = false; |
40021f08 AL |
1749 | if (pci_dev->romfile == NULL && pc->romfile != NULL) { |
1750 | pci_dev->romfile = g_strdup(pc->romfile); | |
ab85ceb1 SW |
1751 | is_default_rom = true; |
1752 | } | |
1753 | pci_add_option_rom(pci_dev, is_default_rom); | |
8c52c8f3 | 1754 | |
5beb8ad5 | 1755 | if (bus->hotplug) { |
e927d487 MT |
1756 | /* Let buses differentiate between hotplug and when device is |
1757 | * enabled during qemu machine creation. */ | |
1758 | rc = bus->hotplug(bus->hotplug_qdev, pci_dev, | |
1759 | qdev->hotplugged ? PCI_HOTPLUG_ENABLED: | |
1760 | PCI_COLDPLUG_ENABLED); | |
a213ff63 IY |
1761 | if (rc != 0) { |
1762 | int r = pci_unregister_device(&pci_dev->qdev); | |
1763 | assert(!r); | |
1764 | return rc; | |
1765 | } | |
1766 | } | |
ee995ffb GH |
1767 | return 0; |
1768 | } | |
1769 | ||
1770 | static int pci_unplug_device(DeviceState *qdev) | |
1771 | { | |
40021f08 AL |
1772 | PCIDevice *dev = PCI_DEVICE(qdev); |
1773 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); | |
ee995ffb | 1774 | |
40021f08 | 1775 | if (pc->no_hotplug) { |
f79f2bfc | 1776 | qerror_report(QERR_DEVICE_NO_HOTPLUG, object_get_typename(OBJECT(dev))); |
180c22e1 GH |
1777 | return -1; |
1778 | } | |
e927d487 MT |
1779 | return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, |
1780 | PCI_HOTPLUG_DISABLED); | |
6b1b92d3 PB |
1781 | } |
1782 | ||
49823868 IY |
1783 | PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction, |
1784 | const char *name) | |
6b1b92d3 PB |
1785 | { |
1786 | DeviceState *dev; | |
1787 | ||
02e2da45 | 1788 | dev = qdev_create(&bus->qbus, name); |
09f1bbcd | 1789 | qdev_prop_set_int32(dev, "addr", devfn); |
49823868 | 1790 | qdev_prop_set_bit(dev, "multifunction", multifunction); |
40021f08 | 1791 | return PCI_DEVICE(dev); |
71077c1c | 1792 | } |
6b1b92d3 | 1793 | |
49823868 IY |
1794 | PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn, |
1795 | bool multifunction, | |
1796 | const char *name) | |
71077c1c | 1797 | { |
49823868 | 1798 | PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name); |
e23a1b33 | 1799 | qdev_init_nofail(&dev->qdev); |
71077c1c | 1800 | return dev; |
6b1b92d3 | 1801 | } |
6f4cbd39 | 1802 | |
49823868 IY |
1803 | PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1804 | { | |
1805 | return pci_create_multifunction(bus, devfn, false, name); | |
1806 | } | |
1807 | ||
1808 | PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) | |
1809 | { | |
1810 | return pci_create_simple_multifunction(bus, devfn, false, name); | |
1811 | } | |
1812 | ||
b56d701f | 1813 | static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size) |
6f4cbd39 MT |
1814 | { |
1815 | int offset = PCI_CONFIG_HEADER_SIZE; | |
1816 | int i; | |
b56d701f | 1817 | for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) { |
6f4cbd39 MT |
1818 | if (pdev->used[i]) |
1819 | offset = i + 1; | |
1820 | else if (i - offset + 1 == size) | |
1821 | return offset; | |
b56d701f | 1822 | } |
6f4cbd39 MT |
1823 | return 0; |
1824 | } | |
1825 | ||
1826 | static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id, | |
1827 | uint8_t *prev_p) | |
1828 | { | |
1829 | uint8_t next, prev; | |
1830 | ||
1831 | if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST)) | |
1832 | return 0; | |
1833 | ||
1834 | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]); | |
1835 | prev = next + PCI_CAP_LIST_NEXT) | |
1836 | if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id) | |
1837 | break; | |
1838 | ||
1839 | if (prev_p) | |
1840 | *prev_p = prev; | |
1841 | return next; | |
1842 | } | |
1843 | ||
c9abe111 JK |
1844 | static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset) |
1845 | { | |
1846 | uint8_t next, prev, found = 0; | |
1847 | ||
1848 | if (!(pdev->used[offset])) { | |
1849 | return 0; | |
1850 | } | |
1851 | ||
1852 | assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST); | |
1853 | ||
1854 | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]); | |
1855 | prev = next + PCI_CAP_LIST_NEXT) { | |
1856 | if (next <= offset && next > found) { | |
1857 | found = next; | |
1858 | } | |
1859 | } | |
1860 | return found; | |
1861 | } | |
1862 | ||
ab85ceb1 SW |
1863 | /* Patch the PCI vendor and device ids in a PCI rom image if necessary. |
1864 | This is needed for an option rom which is used for more than one device. */ | |
1865 | static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size) | |
1866 | { | |
1867 | uint16_t vendor_id; | |
1868 | uint16_t device_id; | |
1869 | uint16_t rom_vendor_id; | |
1870 | uint16_t rom_device_id; | |
1871 | uint16_t rom_magic; | |
1872 | uint16_t pcir_offset; | |
1873 | uint8_t checksum; | |
1874 | ||
1875 | /* Words in rom data are little endian (like in PCI configuration), | |
1876 | so they can be read / written with pci_get_word / pci_set_word. */ | |
1877 | ||
1878 | /* Only a valid rom will be patched. */ | |
1879 | rom_magic = pci_get_word(ptr); | |
1880 | if (rom_magic != 0xaa55) { | |
1881 | PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic); | |
1882 | return; | |
1883 | } | |
1884 | pcir_offset = pci_get_word(ptr + 0x18); | |
1885 | if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) { | |
1886 | PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset); | |
1887 | return; | |
1888 | } | |
1889 | ||
1890 | vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID); | |
1891 | device_id = pci_get_word(pdev->config + PCI_DEVICE_ID); | |
1892 | rom_vendor_id = pci_get_word(ptr + pcir_offset + 4); | |
1893 | rom_device_id = pci_get_word(ptr + pcir_offset + 6); | |
1894 | ||
1895 | PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile, | |
1896 | vendor_id, device_id, rom_vendor_id, rom_device_id); | |
1897 | ||
1898 | checksum = ptr[6]; | |
1899 | ||
1900 | if (vendor_id != rom_vendor_id) { | |
1901 | /* Patch vendor id and checksum (at offset 6 for etherboot roms). */ | |
1902 | checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8); | |
1903 | checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8); | |
1904 | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); | |
1905 | ptr[6] = checksum; | |
1906 | pci_set_word(ptr + pcir_offset + 4, vendor_id); | |
1907 | } | |
1908 | ||
1909 | if (device_id != rom_device_id) { | |
1910 | /* Patch device id and checksum (at offset 6 for etherboot roms). */ | |
1911 | checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8); | |
1912 | checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8); | |
1913 | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); | |
1914 | ptr[6] = checksum; | |
1915 | pci_set_word(ptr + pcir_offset + 6, device_id); | |
1916 | } | |
1917 | } | |
1918 | ||
c2039bd0 | 1919 | /* Add an option rom for the device */ |
ab85ceb1 | 1920 | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom) |
c2039bd0 AL |
1921 | { |
1922 | int size; | |
1923 | char *path; | |
1924 | void *ptr; | |
1724f049 | 1925 | char name[32]; |
4be9f0d1 | 1926 | const VMStateDescription *vmsd; |
c2039bd0 | 1927 | |
8c52c8f3 GH |
1928 | if (!pdev->romfile) |
1929 | return 0; | |
1930 | if (strlen(pdev->romfile) == 0) | |
1931 | return 0; | |
1932 | ||
88169ddf GH |
1933 | if (!pdev->rom_bar) { |
1934 | /* | |
1935 | * Load rom via fw_cfg instead of creating a rom bar, | |
1936 | * for 0.11 compatibility. | |
1937 | */ | |
1938 | int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE); | |
1939 | if (class == 0x0300) { | |
1940 | rom_add_vga(pdev->romfile); | |
1941 | } else { | |
2e55e842 | 1942 | rom_add_option(pdev->romfile, -1); |
88169ddf GH |
1943 | } |
1944 | return 0; | |
1945 | } | |
1946 | ||
8c52c8f3 | 1947 | path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); |
c2039bd0 | 1948 | if (path == NULL) { |
7267c094 | 1949 | path = g_strdup(pdev->romfile); |
c2039bd0 AL |
1950 | } |
1951 | ||
1952 | size = get_image_size(path); | |
8c52c8f3 | 1953 | if (size < 0) { |
1ecda02b | 1954 | error_report("%s: failed to find romfile \"%s\"", |
8c7f3dd0 SH |
1955 | __func__, pdev->romfile); |
1956 | g_free(path); | |
1957 | return -1; | |
1958 | } else if (size == 0) { | |
1959 | error_report("%s: ignoring empty romfile \"%s\"", | |
1960 | __func__, pdev->romfile); | |
7267c094 | 1961 | g_free(path); |
8c52c8f3 GH |
1962 | return -1; |
1963 | } | |
c2039bd0 AL |
1964 | if (size & (size - 1)) { |
1965 | size = 1 << qemu_fls(size); | |
1966 | } | |
1967 | ||
4be9f0d1 AL |
1968 | vmsd = qdev_get_vmsd(DEVICE(pdev)); |
1969 | ||
1970 | if (vmsd) { | |
1971 | snprintf(name, sizeof(name), "%s.rom", vmsd->name); | |
1972 | } else { | |
f79f2bfc | 1973 | snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev))); |
4be9f0d1 | 1974 | } |
14caaf7f | 1975 | pdev->has_rom = true; |
40c5dce9 | 1976 | memory_region_init_ram(&pdev->rom, OBJECT(pdev), name, size); |
c5705a77 | 1977 | vmstate_register_ram(&pdev->rom, &pdev->qdev); |
14caaf7f | 1978 | ptr = memory_region_get_ram_ptr(&pdev->rom); |
c2039bd0 | 1979 | load_image(path, ptr); |
7267c094 | 1980 | g_free(path); |
c2039bd0 | 1981 | |
ab85ceb1 SW |
1982 | if (is_default_rom) { |
1983 | /* Only the default rom images will be patched (if needed). */ | |
1984 | pci_patch_ids(pdev, ptr, size); | |
1985 | } | |
1986 | ||
e824b2cc | 1987 | pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom); |
c2039bd0 AL |
1988 | |
1989 | return 0; | |
1990 | } | |
1991 | ||
230741dc AW |
1992 | static void pci_del_option_rom(PCIDevice *pdev) |
1993 | { | |
14caaf7f | 1994 | if (!pdev->has_rom) |
230741dc AW |
1995 | return; |
1996 | ||
c5705a77 | 1997 | vmstate_unregister_ram(&pdev->rom, &pdev->qdev); |
14caaf7f AK |
1998 | memory_region_destroy(&pdev->rom); |
1999 | pdev->has_rom = false; | |
230741dc AW |
2000 | } |
2001 | ||
ca77089d IY |
2002 | /* |
2003 | * if !offset | |
2004 | * Reserve space and add capability to the linked list in pci config space | |
2005 | * | |
2006 | * if offset = 0, | |
2007 | * Find and reserve space and add capability to the linked list | |
2008 | * in pci config space */ | |
2009 | int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, | |
2010 | uint8_t offset, uint8_t size) | |
6f4cbd39 | 2011 | { |
ca77089d | 2012 | uint8_t *config; |
c9abe111 JK |
2013 | int i, overlapping_cap; |
2014 | ||
ca77089d IY |
2015 | if (!offset) { |
2016 | offset = pci_find_space(pdev, size); | |
2017 | if (!offset) { | |
2018 | return -ENOSPC; | |
2019 | } | |
c9abe111 JK |
2020 | } else { |
2021 | /* Verify that capabilities don't overlap. Note: device assignment | |
2022 | * depends on this check to verify that the device is not broken. | |
2023 | * Should never trigger for emulated devices, but it's helpful | |
2024 | * for debugging these. */ | |
2025 | for (i = offset; i < offset + size; i++) { | |
2026 | overlapping_cap = pci_find_capability_at_offset(pdev, i); | |
2027 | if (overlapping_cap) { | |
568f0690 | 2028 | fprintf(stderr, "ERROR: %s:%02x:%02x.%x " |
c9abe111 JK |
2029 | "Attempt to add PCI capability %x at offset " |
2030 | "%x overlaps existing capability %x at offset %x\n", | |
568f0690 | 2031 | pci_root_bus_path(pdev), pci_bus_num(pdev->bus), |
c9abe111 JK |
2032 | PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), |
2033 | cap_id, offset, overlapping_cap, i); | |
2034 | return -EINVAL; | |
2035 | } | |
2036 | } | |
ca77089d IY |
2037 | } |
2038 | ||
2039 | config = pdev->config + offset; | |
6f4cbd39 MT |
2040 | config[PCI_CAP_LIST_ID] = cap_id; |
2041 | config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; | |
2042 | pdev->config[PCI_CAPABILITY_LIST] = offset; | |
2043 | pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; | |
e26631b7 | 2044 | memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4)); |
6f4cbd39 MT |
2045 | /* Make capability read-only by default */ |
2046 | memset(pdev->wmask + offset, 0, size); | |
bd4b65ee MT |
2047 | /* Check capability by default */ |
2048 | memset(pdev->cmask + offset, 0xFF, size); | |
6f4cbd39 MT |
2049 | return offset; |
2050 | } | |
2051 | ||
2052 | /* Unlink capability from the pci config space. */ | |
2053 | void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size) | |
2054 | { | |
2055 | uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); | |
2056 | if (!offset) | |
2057 | return; | |
2058 | pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; | |
ebabb67a | 2059 | /* Make capability writable again */ |
6f4cbd39 | 2060 | memset(pdev->wmask + offset, 0xff, size); |
1a4f5971 | 2061 | memset(pdev->w1cmask + offset, 0, size); |
bd4b65ee MT |
2062 | /* Clear cmask as device-specific registers can't be checked */ |
2063 | memset(pdev->cmask + offset, 0, size); | |
e26631b7 | 2064 | memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4)); |
6f4cbd39 MT |
2065 | |
2066 | if (!pdev->config[PCI_CAPABILITY_LIST]) | |
2067 | pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; | |
2068 | } | |
2069 | ||
6f4cbd39 MT |
2070 | uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
2071 | { | |
2072 | return pci_find_capability_list(pdev, cap_id, NULL); | |
2073 | } | |
10c4c98a GH |
2074 | |
2075 | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) | |
2076 | { | |
2077 | PCIDevice *d = (PCIDevice *)dev; | |
2078 | const pci_class_desc *desc; | |
2079 | char ctxt[64]; | |
2080 | PCIIORegion *r; | |
2081 | int i, class; | |
2082 | ||
b0ff8eb2 | 2083 | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
10c4c98a GH |
2084 | desc = pci_class_descriptions; |
2085 | while (desc->desc && class != desc->class) | |
2086 | desc++; | |
2087 | if (desc->desc) { | |
2088 | snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); | |
2089 | } else { | |
2090 | snprintf(ctxt, sizeof(ctxt), "Class %04x", class); | |
2091 | } | |
2092 | ||
2093 | monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, " | |
2094 | "pci id %04x:%04x (sub %04x:%04x)\n", | |
7f5feab4 | 2095 | indent, "", ctxt, pci_bus_num(d->bus), |
e822a52a | 2096 | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
b0ff8eb2 IY |
2097 | pci_get_word(d->config + PCI_VENDOR_ID), |
2098 | pci_get_word(d->config + PCI_DEVICE_ID), | |
2099 | pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), | |
2100 | pci_get_word(d->config + PCI_SUBSYSTEM_ID)); | |
10c4c98a GH |
2101 | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
2102 | r = &d->io_regions[i]; | |
2103 | if (!r->size) | |
2104 | continue; | |
89e8b13c IY |
2105 | monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS |
2106 | " [0x%"FMT_PCIBUS"]\n", | |
2107 | indent, "", | |
0392a017 | 2108 | i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
10c4c98a GH |
2109 | r->addr, r->addr + r->size - 1); |
2110 | } | |
2111 | } | |
03587182 | 2112 | |
5e0259e7 GN |
2113 | static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len) |
2114 | { | |
2115 | PCIDevice *d = (PCIDevice *)dev; | |
2116 | const char *name = NULL; | |
2117 | const pci_class_desc *desc = pci_class_descriptions; | |
2118 | int class = pci_get_word(d->config + PCI_CLASS_DEVICE); | |
2119 | ||
2120 | while (desc->desc && | |
2121 | (class & ~desc->fw_ign_bits) != | |
2122 | (desc->class & ~desc->fw_ign_bits)) { | |
2123 | desc++; | |
2124 | } | |
2125 | ||
2126 | if (desc->desc) { | |
2127 | name = desc->fw_name; | |
2128 | } | |
2129 | ||
2130 | if (name) { | |
2131 | pstrcpy(buf, len, name); | |
2132 | } else { | |
2133 | snprintf(buf, len, "pci%04x,%04x", | |
2134 | pci_get_word(d->config + PCI_VENDOR_ID), | |
2135 | pci_get_word(d->config + PCI_DEVICE_ID)); | |
2136 | } | |
2137 | ||
2138 | return buf; | |
2139 | } | |
2140 | ||
2141 | static char *pcibus_get_fw_dev_path(DeviceState *dev) | |
2142 | { | |
2143 | PCIDevice *d = (PCIDevice *)dev; | |
2144 | char path[50], name[33]; | |
2145 | int off; | |
2146 | ||
2147 | off = snprintf(path, sizeof(path), "%s@%x", | |
2148 | pci_dev_fw_name(dev, name, sizeof name), | |
2149 | PCI_SLOT(d->devfn)); | |
2150 | if (PCI_FUNC(d->devfn)) | |
2151 | snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn)); | |
a5cf8262 | 2152 | return g_strdup(path); |
5e0259e7 GN |
2153 | } |
2154 | ||
4f43c1ff AW |
2155 | static char *pcibus_get_dev_path(DeviceState *dev) |
2156 | { | |
a6a7005d MT |
2157 | PCIDevice *d = container_of(dev, PCIDevice, qdev); |
2158 | PCIDevice *t; | |
2159 | int slot_depth; | |
2160 | /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function. | |
2161 | * 00 is added here to make this format compatible with | |
2162 | * domain:Bus:Slot.Func for systems without nested PCI bridges. | |
2163 | * Slot.Function list specifies the slot and function numbers for all | |
2164 | * devices on the path from root to the specific device. */ | |
568f0690 DG |
2165 | const char *root_bus_path; |
2166 | int root_bus_len; | |
2991181a | 2167 | char slot[] = ":SS.F"; |
2991181a | 2168 | int slot_len = sizeof slot - 1 /* For '\0' */; |
a6a7005d MT |
2169 | int path_len; |
2170 | char *path, *p; | |
2991181a | 2171 | int s; |
a6a7005d | 2172 | |
568f0690 DG |
2173 | root_bus_path = pci_root_bus_path(d); |
2174 | root_bus_len = strlen(root_bus_path); | |
2175 | ||
a6a7005d MT |
2176 | /* Calculate # of slots on path between device and root. */; |
2177 | slot_depth = 0; | |
2178 | for (t = d; t; t = t->bus->parent_dev) { | |
2179 | ++slot_depth; | |
2180 | } | |
2181 | ||
568f0690 | 2182 | path_len = root_bus_len + slot_len * slot_depth; |
a6a7005d MT |
2183 | |
2184 | /* Allocate memory, fill in the terminating null byte. */ | |
7267c094 | 2185 | path = g_malloc(path_len + 1 /* For '\0' */); |
a6a7005d MT |
2186 | path[path_len] = '\0'; |
2187 | ||
568f0690 | 2188 | memcpy(path, root_bus_path, root_bus_len); |
a6a7005d MT |
2189 | |
2190 | /* Fill in slot numbers. We walk up from device to root, so need to print | |
2191 | * them in the reverse order, last to first. */ | |
2192 | p = path + path_len; | |
2193 | for (t = d; t; t = t->bus->parent_dev) { | |
2194 | p -= slot_len; | |
2991181a | 2195 | s = snprintf(slot, sizeof slot, ":%02x.%x", |
4c900518 | 2196 | PCI_SLOT(t->devfn), PCI_FUNC(t->devfn)); |
2991181a MT |
2197 | assert(s == slot_len); |
2198 | memcpy(p, slot, slot_len); | |
a6a7005d MT |
2199 | } |
2200 | ||
2201 | return path; | |
4f43c1ff AW |
2202 | } |
2203 | ||
f3006dd1 IY |
2204 | static int pci_qdev_find_recursive(PCIBus *bus, |
2205 | const char *id, PCIDevice **pdev) | |
2206 | { | |
2207 | DeviceState *qdev = qdev_find_recursive(&bus->qbus, id); | |
2208 | if (!qdev) { | |
2209 | return -ENODEV; | |
2210 | } | |
2211 | ||
2212 | /* roughly check if given qdev is pci device */ | |
4be9f0d1 | 2213 | if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) { |
40021f08 | 2214 | *pdev = PCI_DEVICE(qdev); |
f3006dd1 IY |
2215 | return 0; |
2216 | } | |
2217 | return -EINVAL; | |
2218 | } | |
2219 | ||
2220 | int pci_qdev_find_device(const char *id, PCIDevice **pdev) | |
2221 | { | |
7588e2b0 | 2222 | PCIHostState *host_bridge; |
f3006dd1 IY |
2223 | int rc = -ENODEV; |
2224 | ||
7588e2b0 DG |
2225 | QLIST_FOREACH(host_bridge, &pci_host_bridges, next) { |
2226 | int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev); | |
f3006dd1 IY |
2227 | if (!tmp) { |
2228 | rc = 0; | |
2229 | break; | |
2230 | } | |
2231 | if (tmp != -ENODEV) { | |
2232 | rc = tmp; | |
2233 | } | |
2234 | } | |
2235 | ||
2236 | return rc; | |
2237 | } | |
f5e6fed8 AK |
2238 | |
2239 | MemoryRegion *pci_address_space(PCIDevice *dev) | |
2240 | { | |
2241 | return dev->bus->address_space_mem; | |
2242 | } | |
e11d6439 RH |
2243 | |
2244 | MemoryRegion *pci_address_space_io(PCIDevice *dev) | |
2245 | { | |
2246 | return dev->bus->address_space_io; | |
2247 | } | |
40021f08 | 2248 | |
39bffca2 AL |
2249 | static void pci_device_class_init(ObjectClass *klass, void *data) |
2250 | { | |
2251 | DeviceClass *k = DEVICE_CLASS(klass); | |
2252 | k->init = pci_qdev_init; | |
2253 | k->unplug = pci_unplug_device; | |
2254 | k->exit = pci_unregister_device; | |
0d936928 | 2255 | k->bus_type = TYPE_PCI_BUS; |
bce54474 | 2256 | k->props = pci_props; |
39bffca2 AL |
2257 | } |
2258 | ||
9eda7d37 AK |
2259 | AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) |
2260 | { | |
2261 | PCIBus *bus = PCI_BUS(dev->bus); | |
2262 | ||
2263 | if (bus->iommu_fn) { | |
2264 | return bus->iommu_fn(bus, bus->iommu_opaque, dev->devfn); | |
2265 | } | |
2266 | ||
2267 | if (bus->parent_dev) { | |
2268 | /** We are ignoring the bus master DMA bit of the bridge | |
2269 | * as it would complicate things such as VFIO for no good reason */ | |
2270 | return pci_device_iommu_address_space(bus->parent_dev); | |
2271 | } | |
2272 | ||
2273 | return &address_space_memory; | |
2274 | } | |
2275 | ||
e00387d5 | 2276 | void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque) |
5fa45de5 | 2277 | { |
e00387d5 AK |
2278 | bus->iommu_fn = fn; |
2279 | bus->iommu_opaque = opaque; | |
5fa45de5 DG |
2280 | } |
2281 | ||
43864069 MT |
2282 | static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque) |
2283 | { | |
2284 | Range *range = opaque; | |
2285 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); | |
2286 | uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND); | |
77d6f4ea | 2287 | int i; |
43864069 MT |
2288 | |
2289 | if (!(cmd & PCI_COMMAND_MEMORY)) { | |
2290 | return; | |
2291 | } | |
2292 | ||
2293 | if (pc->is_bridge) { | |
2294 | pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); | |
2295 | pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); | |
2296 | ||
2297 | base = MAX(base, 0x1ULL << 32); | |
2298 | ||
2299 | if (limit >= base) { | |
2300 | Range pref_range; | |
2301 | pref_range.begin = base; | |
2302 | pref_range.end = limit + 1; | |
2303 | range_extend(range, &pref_range); | |
2304 | } | |
2305 | } | |
77d6f4ea MT |
2306 | for (i = 0; i < PCI_NUM_REGIONS; ++i) { |
2307 | PCIIORegion *r = &dev->io_regions[i]; | |
43864069 MT |
2308 | Range region_range; |
2309 | ||
77d6f4ea MT |
2310 | if (!r->size || |
2311 | (r->type & PCI_BASE_ADDRESS_SPACE_IO) || | |
2312 | !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) { | |
2313 | continue; | |
2314 | } | |
2315 | region_range.begin = pci_bar_address(dev, i, r->type, r->size); | |
2316 | region_range.end = region_range.begin + r->size; | |
2317 | ||
2318 | if (region_range.begin == PCI_BAR_UNMAPPED) { | |
43864069 MT |
2319 | continue; |
2320 | } | |
43864069 MT |
2321 | |
2322 | region_range.begin = MAX(region_range.begin, 0x1ULL << 32); | |
2323 | ||
2324 | if (region_range.end - 1 >= region_range.begin) { | |
2325 | range_extend(range, ®ion_range); | |
2326 | } | |
2327 | } | |
2328 | } | |
2329 | ||
2330 | void pci_bus_get_w64_range(PCIBus *bus, Range *range) | |
2331 | { | |
2332 | range->begin = range->end = 0; | |
2333 | pci_for_each_device_under_bus(bus, pci_dev_get_w64, range); | |
2334 | } | |
2335 | ||
8c43a6f0 | 2336 | static const TypeInfo pci_device_type_info = { |
40021f08 AL |
2337 | .name = TYPE_PCI_DEVICE, |
2338 | .parent = TYPE_DEVICE, | |
2339 | .instance_size = sizeof(PCIDevice), | |
2340 | .abstract = true, | |
2341 | .class_size = sizeof(PCIDeviceClass), | |
39bffca2 | 2342 | .class_init = pci_device_class_init, |
40021f08 AL |
2343 | }; |
2344 | ||
83f7d43a | 2345 | static void pci_register_types(void) |
40021f08 | 2346 | { |
0d936928 | 2347 | type_register_static(&pci_bus_info); |
3a861c46 | 2348 | type_register_static(&pcie_bus_info); |
40021f08 AL |
2349 | type_register_static(&pci_device_type_info); |
2350 | } | |
2351 | ||
83f7d43a | 2352 | type_init(pci_register_types) |