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