]> Git Repo - qemu.git/blame - hw/pci.c
pci: factor out pci_for_each_device().
[qemu.git] / hw / pci.c
CommitLineData
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 */
87ecb68b
PB
24#include "hw.h"
25#include "pci.h"
a9f49946 26#include "pci_host.h"
376253ec 27#include "monitor.h"
87ecb68b 28#include "net.h"
880345c4 29#include "sysemu.h"
69b91039
FB
30
31//#define DEBUG_PCI
d8d2e079 32#ifdef DEBUG_PCI
2e49d64a 33# define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
d8d2e079
IY
34#else
35# define PCI_DPRINTF(format, ...) do { } while (0)
36#endif
69b91039 37
30468f78 38struct PCIBus {
02e2da45 39 BusState qbus;
30468f78 40 int devfn_min;
502a5395 41 pci_set_irq_fn set_irq;
d2b59317 42 pci_map_irq_fn map_irq;
ee995ffb 43 pci_hotplug_fn hotplug;
30468f78 44 uint32_t config_reg; /* XXX: suppress */
5d4e84c8 45 void *irq_opaque;
30468f78 46 PCIDevice *devices[256];
80b3ada7 47 PCIDevice *parent_dev;
e822a52a
IY
48
49 QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
50 QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
51
d2b59317
PB
52 /* The bus IRQ state is the logical OR of the connected devices.
53 Keep a count of the number of devices with raised IRQs. */
52fc1d83 54 int nirq;
10c4c98a
GH
55 int *irq_count;
56};
57
58static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
59
60static struct BusInfo pci_bus_info = {
61 .name = "PCI",
62 .size = sizeof(PCIBus),
63 .print_dev = pcibus_dev_print,
ee6847d1 64 .props = (Property[]) {
54586bd1
GH
65 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
66 DEFINE_PROP_END_OF_LIST()
ee6847d1 67 }
30468f78 68};
69b91039 69
1941d19c 70static void pci_update_mappings(PCIDevice *d);
d537cf6c 71static void pci_set_irq(void *opaque, int irq_num, int level);
1941d19c 72
c227f099 73target_phys_addr_t pci_mem_base;
d350d97d
AL
74static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
75static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
e822a52a
IY
76
77struct PCIHostBus {
78 int domain;
79 struct PCIBus *bus;
80 QLIST_ENTRY(PCIHostBus) next;
81};
82static QLIST_HEAD(, PCIHostBus) host_buses;
30468f78 83
2d1e9f96
JQ
84static const VMStateDescription vmstate_pcibus = {
85 .name = "PCIBUS",
86 .version_id = 1,
87 .minimum_version_id = 1,
88 .minimum_version_id_old = 1,
89 .fields = (VMStateField []) {
90 VMSTATE_INT32_EQUAL(nirq, PCIBus),
c7bde572 91 VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
2d1e9f96 92 VMSTATE_END_OF_LIST()
52fc1d83 93 }
2d1e9f96 94};
52fc1d83 95
b3b11697 96static int pci_bar(PCIDevice *d, int reg)
5330de09 97{
b3b11697
IY
98 uint8_t type;
99
100 if (reg != PCI_ROM_SLOT)
101 return PCI_BASE_ADDRESS_0 + reg * 4;
102
103 type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
104 return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
5330de09
MT
105}
106
107static void pci_device_reset(PCIDevice *dev)
108{
c0b1905b
MT
109 int r;
110
5330de09 111 memset(dev->irq_state, 0, sizeof dev->irq_state);
c0b1905b
MT
112 dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
113 PCI_COMMAND_MASTER);
114 dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
115 dev->config[PCI_INTERRUPT_LINE] = 0x0;
116 for (r = 0; r < PCI_NUM_REGIONS; ++r) {
117 if (!dev->io_regions[r].size) {
118 continue;
119 }
b3b11697 120 pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
c0b1905b
MT
121 }
122 pci_update_mappings(dev);
5330de09
MT
123}
124
6eaa6847
GN
125static void pci_bus_reset(void *opaque)
126{
a60380a5 127 PCIBus *bus = opaque;
6eaa6847
GN
128 int i;
129
130 for (i = 0; i < bus->nirq; i++) {
131 bus->irq_count[i] = 0;
132 }
5330de09
MT
133 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
134 if (bus->devices[i]) {
135 pci_device_reset(bus->devices[i]);
136 }
6eaa6847
GN
137 }
138}
139
e822a52a
IY
140static void pci_host_bus_register(int domain, PCIBus *bus)
141{
142 struct PCIHostBus *host;
143 host = qemu_mallocz(sizeof(*host));
144 host->domain = domain;
145 host->bus = bus;
146 QLIST_INSERT_HEAD(&host_buses, host, next);
147}
148
149PCIBus *pci_find_host_bus(int domain)
150{
151 struct PCIHostBus *host;
152
153 QLIST_FOREACH(host, &host_buses, next) {
154 if (host->domain == domain) {
155 return host->bus;
156 }
157 }
158
159 return NULL;
160}
161
21eea4b3
GH
162void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
163 const char *name, int devfn_min)
30468f78 164{
52fc1d83
AZ
165 static int nbus = 0;
166
21eea4b3 167 qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
502a5395 168 bus->devfn_min = devfn_min;
e822a52a
IY
169
170 /* host bridge */
171 QLIST_INIT(&bus->child);
172 pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
173
2d1e9f96 174 vmstate_register(nbus++, &vmstate_pcibus, bus);
a08d4367 175 qemu_register_reset(pci_bus_reset, bus);
21eea4b3
GH
176}
177
178PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
179{
180 PCIBus *bus;
181
182 bus = qemu_mallocz(sizeof(*bus));
183 bus->qbus.qdev_allocated = 1;
184 pci_bus_new_inplace(bus, parent, name, devfn_min);
185 return bus;
186}
187
188void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
189 void *irq_opaque, int nirq)
190{
191 bus->set_irq = set_irq;
192 bus->map_irq = map_irq;
193 bus->irq_opaque = irq_opaque;
194 bus->nirq = nirq;
195 bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
196}
197
ee995ffb
GH
198void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
199{
200 bus->qbus.allow_hotplug = 1;
201 bus->hotplug = hotplug;
202}
203
21eea4b3
GH
204PCIBus *pci_register_bus(DeviceState *parent, const char *name,
205 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
206 void *irq_opaque, int devfn_min, int nirq)
207{
208 PCIBus *bus;
209
210 bus = pci_bus_new(parent, name, devfn_min);
211 pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
30468f78
FB
212 return bus;
213}
69b91039 214
e822a52a
IY
215static void pci_register_secondary_bus(PCIBus *parent,
216 PCIBus *bus,
03587182
GH
217 PCIDevice *dev,
218 pci_map_irq_fn map_irq,
219 const char *name)
80b3ada7 220{
03587182 221 qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
80b3ada7
PB
222 bus->map_irq = map_irq;
223 bus->parent_dev = dev;
e822a52a
IY
224
225 QLIST_INIT(&bus->child);
226 QLIST_INSERT_HEAD(&parent->child, bus, sibling);
227}
228
229static void pci_unregister_secondary_bus(PCIBus *bus)
230{
231 assert(QLIST_EMPTY(&bus->child));
232 QLIST_REMOVE(bus, sibling);
80b3ada7
PB
233}
234
502a5395
PB
235int pci_bus_num(PCIBus *s)
236{
e94ff650
IY
237 if (!s->parent_dev)
238 return 0; /* pci host bridge */
239 return s->parent_dev->config[PCI_SECONDARY_BUS];
502a5395
PB
240}
241
e822a52a
IY
242static uint8_t pci_sub_bus(PCIBus *s)
243{
244 if (!s->parent_dev)
245 return 255; /* pci host bridge */
246 return s->parent_dev->config[PCI_SUBORDINATE_BUS];
247}
248
73534f2f 249static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
30ca2aab 250{
73534f2f 251 PCIDevice *s = container_of(pv, PCIDevice, config);
a9f49946 252 uint8_t *config;
52fc1d83
AZ
253 int i;
254
a9f49946
IY
255 assert(size == pci_config_size(s));
256 config = qemu_malloc(size);
257
258 qemu_get_buffer(f, config, size);
259 for (i = 0; i < size; ++i) {
260 if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
261 qemu_free(config);
bd4b65ee 262 return -EINVAL;
a9f49946
IY
263 }
264 }
265 memcpy(s->config, config, size);
bd4b65ee 266
1941d19c 267 pci_update_mappings(s);
52fc1d83 268
a9f49946 269 qemu_free(config);
30ca2aab
FB
270 return 0;
271}
272
73534f2f 273/* just put buffer */
84e2e3eb 274static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
73534f2f
JQ
275{
276 const uint8_t *v = pv;
a9f49946 277 assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
73534f2f
JQ
278 qemu_put_buffer(f, v, size);
279}
280
281static VMStateInfo vmstate_info_pci_config = {
282 .name = "pci config",
283 .get = get_pci_config_device,
284 .put = put_pci_config_device,
285};
286
287const VMStateDescription vmstate_pci_device = {
288 .name = "PCIDevice",
289 .version_id = 2,
290 .minimum_version_id = 1,
291 .minimum_version_id_old = 1,
292 .fields = (VMStateField []) {
293 VMSTATE_INT32_LE(version_id, PCIDevice),
a9f49946
IY
294 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
295 vmstate_info_pci_config,
296 PCI_CONFIG_SPACE_SIZE),
297 VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
298 VMSTATE_END_OF_LIST()
299 }
300};
301
302const VMStateDescription vmstate_pcie_device = {
303 .name = "PCIDevice",
304 .version_id = 2,
305 .minimum_version_id = 1,
306 .minimum_version_id_old = 1,
307 .fields = (VMStateField []) {
308 VMSTATE_INT32_LE(version_id, PCIDevice),
309 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
310 vmstate_info_pci_config,
311 PCIE_CONFIG_SPACE_SIZE),
e369cad7 312 VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
73534f2f
JQ
313 VMSTATE_END_OF_LIST()
314 }
315};
316
a9f49946
IY
317static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
318{
319 return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
320}
321
73534f2f
JQ
322void pci_device_save(PCIDevice *s, QEMUFile *f)
323{
a9f49946 324 vmstate_save_state(f, pci_get_vmstate(s), s);
73534f2f
JQ
325}
326
327int pci_device_load(PCIDevice *s, QEMUFile *f)
328{
a9f49946 329 return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
73534f2f
JQ
330}
331
d350d97d
AL
332static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
333{
334 uint16_t *id;
335
336 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
337 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
338 id[1] = cpu_to_le16(pci_default_sub_device_id);
339 return 0;
340}
341
880345c4
AL
342/*
343 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
344 */
345static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
346{
347 const char *p;
348 char *e;
349 unsigned long val;
350 unsigned long dom = 0, bus = 0;
351 unsigned slot = 0;
352
353 p = addr;
354 val = strtoul(p, &e, 16);
355 if (e == p)
356 return -1;
357 if (*e == ':') {
358 bus = val;
359 p = e + 1;
360 val = strtoul(p, &e, 16);
361 if (e == p)
362 return -1;
363 if (*e == ':') {
364 dom = bus;
365 bus = val;
366 p = e + 1;
367 val = strtoul(p, &e, 16);
368 if (e == p)
369 return -1;
370 }
371 }
372
373 if (dom > 0xffff || bus > 0xff || val > 0x1f)
374 return -1;
375
376 slot = val;
377
378 if (*e)
379 return -1;
380
381 /* Note: QEMU doesn't implement domains other than 0 */
e822a52a 382 if (!pci_find_bus(pci_find_host_bus(dom), bus))
880345c4
AL
383 return -1;
384
385 *domp = dom;
386 *busp = bus;
387 *slotp = slot;
388 return 0;
389}
390
e9283f8b
JK
391int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
392 unsigned *slotp)
880345c4 393{
e9283f8b
JK
394 /* strip legacy tag */
395 if (!strncmp(addr, "pci_addr=", 9)) {
396 addr += 9;
397 }
398 if (pci_parse_devaddr(addr, domp, busp, slotp)) {
399 monitor_printf(mon, "Invalid pci address\n");
880345c4 400 return -1;
e9283f8b
JK
401 }
402 return 0;
880345c4
AL
403}
404
49bd1458 405PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
5607c388
MA
406{
407 int dom, bus;
408 unsigned slot;
409
410 if (!devaddr) {
411 *devfnp = -1;
e822a52a 412 return pci_find_bus(pci_find_host_bus(0), 0);
5607c388
MA
413 }
414
415 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
416 return NULL;
417 }
418
419 *devfnp = slot << 3;
e822a52a 420 return pci_find_bus(pci_find_host_bus(0), bus);
5607c388
MA
421}
422
bd4b65ee
MT
423static void pci_init_cmask(PCIDevice *dev)
424{
425 pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
426 pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
427 dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
428 dev->cmask[PCI_REVISION_ID] = 0xff;
429 dev->cmask[PCI_CLASS_PROG] = 0xff;
430 pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
431 dev->cmask[PCI_HEADER_TYPE] = 0xff;
432 dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
433}
434
b7ee1603
MT
435static void pci_init_wmask(PCIDevice *dev)
436{
437 int i;
a9f49946
IY
438 int config_size = pci_config_size(dev);
439
b7ee1603
MT
440 dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
441 dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
67a51b48
IY
442 pci_set_word(dev->wmask + PCI_COMMAND,
443 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
a9f49946 444 for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
b7ee1603
MT
445 dev->wmask[i] = 0xff;
446}
447
fb231628
IY
448static void pci_init_wmask_bridge(PCIDevice *d)
449{
450 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
451 PCI_SEC_LETENCY_TIMER */
452 memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
453
454 /* base and limit */
455 d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
456 d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
457 pci_set_word(d->wmask + PCI_MEMORY_BASE,
458 PCI_MEMORY_RANGE_MASK & 0xffff);
459 pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
460 PCI_MEMORY_RANGE_MASK & 0xffff);
461 pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
462 PCI_PREF_RANGE_MASK & 0xffff);
463 pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
464 PCI_PREF_RANGE_MASK & 0xffff);
465
466 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
467 memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
468
469 pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
470}
471
a9f49946
IY
472static void pci_config_alloc(PCIDevice *pci_dev)
473{
474 int config_size = pci_config_size(pci_dev);
475
476 pci_dev->config = qemu_mallocz(config_size);
477 pci_dev->cmask = qemu_mallocz(config_size);
478 pci_dev->wmask = qemu_mallocz(config_size);
479 pci_dev->used = qemu_mallocz(config_size);
480}
481
482static void pci_config_free(PCIDevice *pci_dev)
483{
484 qemu_free(pci_dev->config);
485 qemu_free(pci_dev->cmask);
486 qemu_free(pci_dev->wmask);
487 qemu_free(pci_dev->used);
488}
489
69b91039 490/* -1 for devfn means auto assign */
6b1b92d3
PB
491static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
492 const char *name, int devfn,
493 PCIConfigReadFunc *config_read,
fb231628
IY
494 PCIConfigWriteFunc *config_write,
495 uint8_t header_type)
69b91039 496{
69b91039 497 if (devfn < 0) {
30468f78
FB
498 for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
499 if (!bus->devices[devfn])
69b91039
FB
500 goto found;
501 }
502 return NULL;
503 found: ;
07b7d053
MA
504 } else if (bus->devices[devfn]) {
505 return NULL;
69b91039 506 }
30468f78 507 pci_dev->bus = bus;
69b91039
FB
508 pci_dev->devfn = devfn;
509 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
d2b59317 510 memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
a9f49946 511 pci_config_alloc(pci_dev);
fb231628
IY
512
513 header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
514 if (header_type == PCI_HEADER_TYPE_NORMAL) {
515 pci_set_default_subsystem_id(pci_dev);
516 }
bd4b65ee 517 pci_init_cmask(pci_dev);
b7ee1603 518 pci_init_wmask(pci_dev);
fb231628
IY
519 if (header_type == PCI_HEADER_TYPE_BRIDGE) {
520 pci_init_wmask_bridge(pci_dev);
521 }
0ac32c83
FB
522
523 if (!config_read)
524 config_read = pci_default_read_config;
525 if (!config_write)
526 config_write = pci_default_write_config;
69b91039
FB
527 pci_dev->config_read = config_read;
528 pci_dev->config_write = config_write;
30468f78 529 bus->devices[devfn] = pci_dev;
e369cad7 530 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
f16c4abf 531 pci_dev->version_id = 2; /* Current pci device vmstate version */
69b91039
FB
532 return pci_dev;
533}
534
6b1b92d3
PB
535PCIDevice *pci_register_device(PCIBus *bus, const char *name,
536 int instance_size, int devfn,
537 PCIConfigReadFunc *config_read,
538 PCIConfigWriteFunc *config_write)
539{
540 PCIDevice *pci_dev;
541
542 pci_dev = qemu_mallocz(instance_size);
543 pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
fb231628
IY
544 config_read, config_write,
545 PCI_HEADER_TYPE_NORMAL);
6b1b92d3
PB
546 return pci_dev;
547}
c227f099 548static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
5851e08c
AL
549{
550 return addr + pci_mem_base;
551}
552
553static void pci_unregister_io_regions(PCIDevice *pci_dev)
554{
555 PCIIORegion *r;
556 int i;
557
558 for(i = 0; i < PCI_NUM_REGIONS; i++) {
559 r = &pci_dev->io_regions[i];
182f9c8a 560 if (!r->size || r->addr == PCI_BAR_UNMAPPED)
5851e08c 561 continue;
0392a017 562 if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
5851e08c
AL
563 isa_unassign_ioport(r->addr, r->size);
564 } else {
565 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
566 r->size,
567 IO_MEM_UNASSIGNED);
568 }
569 }
570}
571
a36a344d 572static int pci_unregister_device(DeviceState *dev)
5851e08c 573{
a36a344d 574 PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
e3936fa5 575 PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
5851e08c
AL
576 int ret = 0;
577
e3936fa5
GH
578 if (info->exit)
579 ret = info->exit(pci_dev);
5851e08c
AL
580 if (ret)
581 return ret;
582
583 pci_unregister_io_regions(pci_dev);
584
585 qemu_free_irqs(pci_dev->irq);
5851e08c 586 pci_dev->bus->devices[pci_dev->devfn] = NULL;
a9f49946 587 pci_config_free(pci_dev);
5851e08c
AL
588 return 0;
589}
590
28c2c264 591void pci_register_bar(PCIDevice *pci_dev, int region_num,
6e355d90 592 pcibus_t size, int type,
69b91039
FB
593 PCIMapIORegionFunc *map_func)
594{
595 PCIIORegion *r;
d7ce493a 596 uint32_t addr;
6e355d90 597 pcibus_t wmask;
69b91039 598
8a8696a3 599 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
69b91039 600 return;
a4c20c6a
AL
601
602 if (size & (size-1)) {
603 fprintf(stderr, "ERROR: PCI region size must be pow2 "
89e8b13c 604 "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
a4c20c6a
AL
605 exit(1);
606 }
607
69b91039 608 r = &pci_dev->io_regions[region_num];
182f9c8a 609 r->addr = PCI_BAR_UNMAPPED;
69b91039
FB
610 r->size = size;
611 r->type = type;
612 r->map_func = map_func;
b7ee1603
MT
613
614 wmask = ~(size - 1);
b3b11697 615 addr = pci_bar(pci_dev, region_num);
d7ce493a 616 if (region_num == PCI_ROM_SLOT) {
b7ee1603 617 /* ROM enable bit is writeable */
5330de09 618 wmask |= PCI_ROM_ADDRESS_ENABLE;
d7ce493a 619 }
b0ff8eb2 620 pci_set_long(pci_dev->config + addr, type);
14421258
IY
621 if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
622 r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
623 pci_set_quad(pci_dev->wmask + addr, wmask);
624 pci_set_quad(pci_dev->cmask + addr, ~0ULL);
625 } else {
626 pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
627 pci_set_long(pci_dev->cmask + addr, 0xffffffff);
628 }
69b91039
FB
629}
630
0ac32c83
FB
631static void pci_update_mappings(PCIDevice *d)
632{
633 PCIIORegion *r;
634 int cmd, i;
6e355d90 635 pcibus_t last_addr, new_addr;
3b46e624 636
b0ff8eb2 637 cmd = pci_get_word(d->config + PCI_COMMAND);
8a8696a3 638 for(i = 0; i < PCI_NUM_REGIONS; i++) {
0ac32c83 639 r = &d->io_regions[i];
a9688570
IY
640
641 /* this region isn't registered */
642 if (r->size == 0)
643 continue;
644
645 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
646 if (cmd & PCI_COMMAND_IO) {
647 new_addr = pci_get_long(d->config + pci_bar(d, i));
648 new_addr = new_addr & ~(r->size - 1);
649 last_addr = new_addr + r->size - 1;
650 /* NOTE: we have only 64K ioports on PC */
651 if (last_addr <= new_addr || new_addr == 0 ||
652 last_addr >= 0x10000) {
182f9c8a 653 new_addr = PCI_BAR_UNMAPPED;
0ac32c83
FB
654 }
655 } else {
a9688570
IY
656 new_addr = PCI_BAR_UNMAPPED;
657 }
658 } else {
659 if (cmd & PCI_COMMAND_MEMORY) {
660 if (r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
661 new_addr = pci_get_quad(d->config + pci_bar(d, i));
0ac32c83 662 } else {
a9688570
IY
663 new_addr = pci_get_long(d->config + pci_bar(d, i));
664 }
665 /* the ROM slot has a specific enable bit */
666 if (i == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE))
667 goto no_mem_map;
668 new_addr = new_addr & ~(r->size - 1);
669 last_addr = new_addr + r->size - 1;
670 /* NOTE: we do not support wrapping */
671 /* XXX: as we cannot support really dynamic
672 mappings, we handle specific values as invalid
673 mappings. */
674 if (last_addr <= new_addr || new_addr == 0 ||
675 last_addr == PCI_BAR_UNMAPPED ||
676
677 /* Now pcibus_t is 64bit.
678 * Check if 32 bit BAR wrap around explicitly.
679 * Without this, PC ide doesn't work well.
680 * TODO: remove this work around.
681 */
682 (!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) &&
683 last_addr >= UINT32_MAX) ||
684
685 /*
686 * OS is allowed to set BAR beyond its addressable
687 * bits. For example, 32 bit OS can set 64bit bar
688 * to >4G. Check it.
689 */
690 last_addr >= TARGET_PHYS_ADDR_MAX) {
182f9c8a 691 new_addr = PCI_BAR_UNMAPPED;
0ac32c83 692 }
a9688570
IY
693 } else {
694 no_mem_map:
695 new_addr = PCI_BAR_UNMAPPED;
0ac32c83 696 }
a9688570
IY
697 }
698
699 /* This bar isn't changed */
700 if (new_addr == r->addr)
701 continue;
702
703 /* now do the real mapping */
704 if (r->addr != PCI_BAR_UNMAPPED) {
705 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
706 int class;
707 /* NOTE: specific hack for IDE in PC case:
708 only one byte must be mapped. */
709 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
710 if (class == 0x0101 && r->size == 4) {
711 isa_unassign_ioport(r->addr + 2, 1);
712 } else {
713 isa_unassign_ioport(r->addr, r->size);
0ac32c83 714 }
a9688570
IY
715 } else {
716 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
717 r->size,
718 IO_MEM_UNASSIGNED);
719 qemu_unregister_coalesced_mmio(r->addr, r->size);
0ac32c83
FB
720 }
721 }
a9688570
IY
722 r->addr = new_addr;
723 if (r->addr != PCI_BAR_UNMAPPED) {
724 r->map_func(d, i, r->addr, r->size, r->type);
725 }
0ac32c83
FB
726 }
727}
728
5fafdf24 729uint32_t pci_default_read_config(PCIDevice *d,
0ac32c83 730 uint32_t address, int len)
69b91039 731{
5029fe12
IY
732 uint32_t val = 0;
733 assert(len == 1 || len == 2 || len == 4);
a9f49946 734 len = MIN(len, pci_config_size(d) - address);
5029fe12
IY
735 memcpy(&val, d->config + address, len);
736 return le32_to_cpu(val);
0ac32c83
FB
737}
738
b7ee1603 739void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
0ac32c83 740{
b7ee1603 741 int i;
a9f49946 742 uint32_t config_size = pci_config_size(d);
0ac32c83 743
a9f49946 744 for(i = 0; i < l && addr < config_size; val >>= 8, ++i, ++addr) {
b7ee1603
MT
745 uint8_t wmask = d->wmask[addr];
746 d->config[addr] = (d->config[addr] & ~wmask) | (val & wmask);
0ac32c83 747 }
260c0cd3 748 if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
edb00035
IY
749 ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
750 ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
260c0cd3 751 range_covers_byte(addr, l, PCI_COMMAND))
0ac32c83 752 pci_update_mappings(d);
69b91039
FB
753}
754
502a5395
PB
755/***********************************************************/
756/* generic PCI irq support */
30468f78 757
502a5395 758/* 0 <= irq_num <= 3. level must be 0 or 1 */
d537cf6c 759static void pci_set_irq(void *opaque, int irq_num, int level)
69b91039 760{
a60380a5 761 PCIDevice *pci_dev = opaque;
80b3ada7
PB
762 PCIBus *bus;
763 int change;
3b46e624 764
80b3ada7
PB
765 change = level - pci_dev->irq_state[irq_num];
766 if (!change)
767 return;
d2b59317 768
d2b59317 769 pci_dev->irq_state[irq_num] = level;
5e966ce6
PB
770 for (;;) {
771 bus = pci_dev->bus;
80b3ada7 772 irq_num = bus->map_irq(pci_dev, irq_num);
5e966ce6
PB
773 if (bus->set_irq)
774 break;
80b3ada7 775 pci_dev = bus->parent_dev;
80b3ada7
PB
776 }
777 bus->irq_count[irq_num] += change;
d2b59317 778 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
69b91039
FB
779}
780
502a5395
PB
781/***********************************************************/
782/* monitor info on PCI */
0ac32c83 783
6650ee6d
PB
784typedef struct {
785 uint16_t class;
786 const char *desc;
787} pci_class_desc;
788
09bc878a 789static const pci_class_desc pci_class_descriptions[] =
6650ee6d 790{
4ca9c76f 791 { 0x0100, "SCSI controller"},
6650ee6d 792 { 0x0101, "IDE controller"},
dcb5b19a
TS
793 { 0x0102, "Floppy controller"},
794 { 0x0103, "IPI controller"},
795 { 0x0104, "RAID controller"},
796 { 0x0106, "SATA controller"},
797 { 0x0107, "SAS controller"},
798 { 0x0180, "Storage controller"},
6650ee6d 799 { 0x0200, "Ethernet controller"},
dcb5b19a
TS
800 { 0x0201, "Token Ring controller"},
801 { 0x0202, "FDDI controller"},
802 { 0x0203, "ATM controller"},
803 { 0x0280, "Network controller"},
6650ee6d 804 { 0x0300, "VGA controller"},
dcb5b19a
TS
805 { 0x0301, "XGA controller"},
806 { 0x0302, "3D controller"},
807 { 0x0380, "Display controller"},
808 { 0x0400, "Video controller"},
809 { 0x0401, "Audio controller"},
810 { 0x0402, "Phone"},
811 { 0x0480, "Multimedia controller"},
812 { 0x0500, "RAM controller"},
813 { 0x0501, "Flash controller"},
814 { 0x0580, "Memory controller"},
6650ee6d
PB
815 { 0x0600, "Host bridge"},
816 { 0x0601, "ISA bridge"},
dcb5b19a
TS
817 { 0x0602, "EISA bridge"},
818 { 0x0603, "MC bridge"},
6650ee6d 819 { 0x0604, "PCI bridge"},
dcb5b19a
TS
820 { 0x0605, "PCMCIA bridge"},
821 { 0x0606, "NUBUS bridge"},
822 { 0x0607, "CARDBUS bridge"},
823 { 0x0608, "RACEWAY bridge"},
824 { 0x0680, "Bridge"},
6650ee6d
PB
825 { 0x0c03, "USB controller"},
826 { 0, NULL}
827};
828
e822a52a 829static void pci_info_device(PCIBus *bus, PCIDevice *d)
30468f78 830{
376253ec 831 Monitor *mon = cur_mon;
502a5395
PB
832 int i, class;
833 PCIIORegion *r;
09bc878a 834 const pci_class_desc *desc;
30468f78 835
376253ec 836 monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
e94ff650
IY
837 pci_bus_num(d->bus),
838 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
b0ff8eb2 839 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
376253ec 840 monitor_printf(mon, " ");
6650ee6d
PB
841 desc = pci_class_descriptions;
842 while (desc->desc && class != desc->class)
843 desc++;
844 if (desc->desc) {
376253ec 845 monitor_printf(mon, "%s", desc->desc);
6650ee6d 846 } else {
376253ec 847 monitor_printf(mon, "Class %04x", class);
72cc6cfe 848 }
376253ec 849 monitor_printf(mon, ": PCI device %04x:%04x\n",
b0ff8eb2
IY
850 pci_get_word(d->config + PCI_VENDOR_ID),
851 pci_get_word(d->config + PCI_DEVICE_ID));
30468f78 852
502a5395 853 if (d->config[PCI_INTERRUPT_PIN] != 0) {
376253ec
AL
854 monitor_printf(mon, " IRQ %d.\n",
855 d->config[PCI_INTERRUPT_LINE]);
30468f78 856 }
80b3ada7 857 if (class == 0x0604) {
376253ec 858 monitor_printf(mon, " BUS %d.\n", d->config[0x19]);
80b3ada7 859 }
502a5395
PB
860 for(i = 0;i < PCI_NUM_REGIONS; i++) {
861 r = &d->io_regions[i];
862 if (r->size != 0) {
376253ec 863 monitor_printf(mon, " BAR%d: ", i);
0392a017 864 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
89e8b13c
IY
865 monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
866 " [0x%04"FMT_PCIBUS"].\n",
376253ec 867 r->addr, r->addr + r->size - 1);
502a5395 868 } else {
14421258
IY
869 const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
870 "64 bit" : "32 bit";
871 const char *prefetch =
872 r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
873 " prefetchable" : "";
874
875 monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
89e8b13c 876 " [0x%08"FMT_PCIBUS"].\n",
14421258 877 type, prefetch,
376253ec 878 r->addr, r->addr + r->size - 1);
502a5395
PB
879 }
880 }
77d4bc34 881 }
8ad12514 882 monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
80b3ada7 883 if (class == 0x0604 && d->config[0x19] != 0) {
e822a52a 884 pci_for_each_device(bus, d->config[0x19], pci_info_device);
80b3ada7 885 }
384d8876
FB
886}
887
1074df4f
IY
888static void pci_for_each_device_under_bus(PCIBus *bus,
889 void (*fn)(PCIBus *b, PCIDevice *d))
384d8876 890{
384d8876 891 PCIDevice *d;
502a5395 892 int devfn;
3b46e624 893
1074df4f
IY
894 for(devfn = 0; devfn < 256; devfn++) {
895 d = bus->devices[devfn];
896 if (d)
897 fn(bus, d);
898 }
899}
900
901void pci_for_each_device(PCIBus *bus, int bus_num,
902 void (*fn)(PCIBus *b, PCIDevice *d))
903{
e822a52a 904 bus = pci_find_bus(bus, bus_num);
1074df4f 905
502a5395 906 if (bus) {
1074df4f 907 pci_for_each_device_under_bus(bus, fn);
f2aa58c6 908 }
f2aa58c6
FB
909}
910
376253ec 911void pci_info(Monitor *mon)
f2aa58c6 912{
e822a52a
IY
913 struct PCIHostBus *host;
914 QLIST_FOREACH(host, &host_buses, next) {
915 pci_for_each_device(host->bus, 0, pci_info_device);
916 }
77d4bc34 917}
a41b2ff2 918
cb457d76
AL
919static const char * const pci_nic_models[] = {
920 "ne2k_pci",
921 "i82551",
922 "i82557b",
923 "i82559er",
924 "rtl8139",
925 "e1000",
926 "pcnet",
927 "virtio",
928 NULL
929};
930
9d07d757
PB
931static const char * const pci_nic_names[] = {
932 "ne2k_pci",
933 "i82551",
934 "i82557b",
935 "i82559er",
936 "rtl8139",
937 "e1000",
938 "pcnet",
53c25cea 939 "virtio-net-pci",
cb457d76
AL
940 NULL
941};
942
a41b2ff2 943/* Initialize a PCI NIC. */
33e66b86 944/* FIXME callers should check for failure, but don't */
5607c388
MA
945PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
946 const char *default_devaddr)
a41b2ff2 947{
5607c388 948 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
07caea31
MA
949 PCIBus *bus;
950 int devfn;
5607c388 951 PCIDevice *pci_dev;
9d07d757 952 DeviceState *dev;
cb457d76
AL
953 int i;
954
07caea31
MA
955 i = qemu_find_nic_model(nd, pci_nic_models, default_model);
956 if (i < 0)
957 return NULL;
958
959 bus = pci_get_bus_devfn(&devfn, devaddr);
960 if (!bus) {
961 qemu_error("Invalid PCI device address %s for device %s\n",
962 devaddr, pci_nic_names[i]);
963 return NULL;
964 }
965
499cf102 966 pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
9ee05825 967 dev = &pci_dev->qdev;
dea7b3b9
MM
968 if (nd->name)
969 dev->id = qemu_strdup(nd->name);
1cc33683 970 qdev_set_nic_properties(dev, nd);
07caea31
MA
971 if (qdev_init(dev) < 0)
972 return NULL;
9ee05825 973 return pci_dev;
a41b2ff2
PB
974}
975
07caea31
MA
976PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
977 const char *default_devaddr)
978{
979 PCIDevice *res;
980
981 if (qemu_show_nic_models(nd->model, pci_nic_models))
982 exit(0);
983
984 res = pci_nic_init(nd, default_model, default_devaddr);
985 if (!res)
986 exit(1);
987 return res;
988}
989
80b3ada7
PB
990typedef struct {
991 PCIDevice dev;
03587182
GH
992 PCIBus bus;
993 uint32_t vid;
994 uint32_t did;
80b3ada7
PB
995} PCIBridge;
996
9596ebb7 997static void pci_bridge_write_config(PCIDevice *d,
80b3ada7
PB
998 uint32_t address, uint32_t val, int len)
999{
80b3ada7
PB
1000 pci_default_write_config(d, address, val, len);
1001}
1002
e822a52a 1003PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
3ae80618 1004{
e822a52a 1005 PCIBus *sec;
3ae80618 1006
e822a52a
IY
1007 if (!bus)
1008 return NULL;
3ae80618 1009
e822a52a
IY
1010 if (pci_bus_num(bus) == bus_num) {
1011 return bus;
1012 }
1013
1014 /* try child bus */
1015 QLIST_FOREACH(sec, &bus->child, sibling) {
1016 if (pci_bus_num(sec) <= bus_num && bus_num <= pci_sub_bus(sec)) {
1017 return pci_find_bus(sec, bus_num);
1018 }
1019 }
1020
1021 return NULL;
3ae80618
AL
1022}
1023
e822a52a 1024PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
3ae80618 1025{
e822a52a 1026 bus = pci_find_bus(bus, bus_num);
3ae80618
AL
1027
1028 if (!bus)
1029 return NULL;
1030
1031 return bus->devices[PCI_DEVFN(slot, function)];
1032}
1033
03587182 1034static int pci_bridge_initfn(PCIDevice *dev)
80b3ada7 1035{
03587182 1036 PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
480b9f24 1037
03587182
GH
1038 pci_config_set_vendor_id(s->dev.config, s->vid);
1039 pci_config_set_device_id(s->dev.config, s->did);
480b9f24 1040
74c01823
IY
1041 /* TODO: intial value
1042 * command register:
1043 * According to PCI bridge spec, after reset
1044 * bus master bit is off
1045 * memory space enable bit is off
1046 * According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
1047 * the reset value should be zero unless the boot pin is tied high
1048 * (which is tru) and thus it should be PCI_COMMAND_MEMORY.
1049 *
1050 * For now, don't touch the value.
1051 * Later command register will be set to zero and apb_pci.c will
1052 * override the value.
1053 * Same for latency timer, and multi function bit of header type.
1054 */
1055 pci_set_word(dev->config + PCI_COMMAND,
1056 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1057
1058 pci_set_word(dev->config + PCI_STATUS,
1059 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1060 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1061 dev->config[PCI_LATENCY_TIMER] = 0x10;
1062 dev->config[PCI_HEADER_TYPE] =
1063 PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE;
1064 pci_set_word(dev->config + PCI_SEC_STATUS,
1065 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
03587182
GH
1066 return 0;
1067}
80b3ada7 1068
e822a52a
IY
1069static int pci_bridge_exitfn(PCIDevice *pci_dev)
1070{
1071 PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1072 PCIBus *bus = &s->bus;
1073 pci_unregister_secondary_bus(bus);
1074 return 0;
1075}
1076
03587182
GH
1077PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1078 pci_map_irq_fn map_irq, const char *name)
1079{
1080 PCIDevice *dev;
1081 PCIBridge *s;
1082
499cf102 1083 dev = pci_create(bus, devfn, "pci-bridge");
03587182
GH
1084 qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1085 qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
e23a1b33 1086 qdev_init_nofail(&dev->qdev);
03587182
GH
1087
1088 s = DO_UPCAST(PCIBridge, dev, dev);
e822a52a 1089 pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
03587182 1090 return &s->bus;
80b3ada7 1091}
6b1b92d3 1092
81a322d4 1093static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
6b1b92d3
PB
1094{
1095 PCIDevice *pci_dev = (PCIDevice *)qdev;
02e2da45 1096 PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
6b1b92d3 1097 PCIBus *bus;
ee995ffb 1098 int devfn, rc;
6b1b92d3 1099
a9f49946
IY
1100 /* initialize cap_present for pci_is_express() and pci_config_size() */
1101 if (info->is_express) {
1102 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1103 }
1104
02e2da45 1105 bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
ee6847d1 1106 devfn = pci_dev->devfn;
16eaedf2 1107 pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
fb231628
IY
1108 info->config_read, info->config_write,
1109 info->header_type);
6b1b92d3 1110 assert(pci_dev);
ee995ffb
GH
1111 rc = info->init(pci_dev);
1112 if (rc != 0)
1113 return rc;
1114 if (qdev->hotplugged)
1115 bus->hotplug(pci_dev, 1);
1116 return 0;
1117}
1118
1119static int pci_unplug_device(DeviceState *qdev)
1120{
1121 PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1122
1123 dev->bus->hotplug(dev, 0);
1124 return 0;
6b1b92d3
PB
1125}
1126
0aab0d3a 1127void pci_qdev_register(PCIDeviceInfo *info)
6b1b92d3 1128{
02e2da45 1129 info->qdev.init = pci_qdev_init;
ee995ffb 1130 info->qdev.unplug = pci_unplug_device;
a36a344d 1131 info->qdev.exit = pci_unregister_device;
10c4c98a 1132 info->qdev.bus_info = &pci_bus_info;
074f2fff 1133 qdev_register(&info->qdev);
6b1b92d3
PB
1134}
1135
0aab0d3a
GH
1136void pci_qdev_register_many(PCIDeviceInfo *info)
1137{
1138 while (info->qdev.name) {
1139 pci_qdev_register(info);
1140 info++;
1141 }
1142}
1143
499cf102 1144PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
6b1b92d3
PB
1145{
1146 DeviceState *dev;
1147
02e2da45 1148 dev = qdev_create(&bus->qbus, name);
a6307b08 1149 qdev_prop_set_uint32(dev, "addr", devfn);
71077c1c
GH
1150 return DO_UPCAST(PCIDevice, qdev, dev);
1151}
6b1b92d3 1152
71077c1c
GH
1153PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1154{
499cf102 1155 PCIDevice *dev = pci_create(bus, devfn, name);
e23a1b33 1156 qdev_init_nofail(&dev->qdev);
71077c1c 1157 return dev;
6b1b92d3 1158}
6f4cbd39
MT
1159
1160static int pci_find_space(PCIDevice *pdev, uint8_t size)
1161{
a9f49946 1162 int config_size = pci_config_size(pdev);
6f4cbd39
MT
1163 int offset = PCI_CONFIG_HEADER_SIZE;
1164 int i;
a9f49946 1165 for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
6f4cbd39
MT
1166 if (pdev->used[i])
1167 offset = i + 1;
1168 else if (i - offset + 1 == size)
1169 return offset;
1170 return 0;
1171}
1172
1173static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1174 uint8_t *prev_p)
1175{
1176 uint8_t next, prev;
1177
1178 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1179 return 0;
1180
1181 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1182 prev = next + PCI_CAP_LIST_NEXT)
1183 if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1184 break;
1185
1186 if (prev_p)
1187 *prev_p = prev;
1188 return next;
1189}
1190
1191/* Reserve space and add capability to the linked list in pci config space */
1192int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1193{
1194 uint8_t offset = pci_find_space(pdev, size);
1195 uint8_t *config = pdev->config + offset;
1196 if (!offset)
1197 return -ENOSPC;
1198 config[PCI_CAP_LIST_ID] = cap_id;
1199 config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1200 pdev->config[PCI_CAPABILITY_LIST] = offset;
1201 pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1202 memset(pdev->used + offset, 0xFF, size);
1203 /* Make capability read-only by default */
1204 memset(pdev->wmask + offset, 0, size);
bd4b65ee
MT
1205 /* Check capability by default */
1206 memset(pdev->cmask + offset, 0xFF, size);
6f4cbd39
MT
1207 return offset;
1208}
1209
1210/* Unlink capability from the pci config space. */
1211void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1212{
1213 uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1214 if (!offset)
1215 return;
1216 pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1217 /* Make capability writeable again */
1218 memset(pdev->wmask + offset, 0xff, size);
bd4b65ee
MT
1219 /* Clear cmask as device-specific registers can't be checked */
1220 memset(pdev->cmask + offset, 0, size);
6f4cbd39
MT
1221 memset(pdev->used + offset, 0, size);
1222
1223 if (!pdev->config[PCI_CAPABILITY_LIST])
1224 pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1225}
1226
1227/* Reserve space for capability at a known offset (to call after load). */
1228void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1229{
1230 memset(pdev->used + offset, 0xff, size);
1231}
1232
1233uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1234{
1235 return pci_find_capability_list(pdev, cap_id, NULL);
1236}
10c4c98a
GH
1237
1238static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1239{
1240 PCIDevice *d = (PCIDevice *)dev;
1241 const pci_class_desc *desc;
1242 char ctxt[64];
1243 PCIIORegion *r;
1244 int i, class;
1245
b0ff8eb2 1246 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
10c4c98a
GH
1247 desc = pci_class_descriptions;
1248 while (desc->desc && class != desc->class)
1249 desc++;
1250 if (desc->desc) {
1251 snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1252 } else {
1253 snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1254 }
1255
1256 monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1257 "pci id %04x:%04x (sub %04x:%04x)\n",
1258 indent, "", ctxt,
e822a52a
IY
1259 d->config[PCI_SECONDARY_BUS],
1260 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
b0ff8eb2
IY
1261 pci_get_word(d->config + PCI_VENDOR_ID),
1262 pci_get_word(d->config + PCI_DEVICE_ID),
1263 pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1264 pci_get_word(d->config + PCI_SUBSYSTEM_ID));
10c4c98a
GH
1265 for (i = 0; i < PCI_NUM_REGIONS; i++) {
1266 r = &d->io_regions[i];
1267 if (!r->size)
1268 continue;
89e8b13c
IY
1269 monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1270 " [0x%"FMT_PCIBUS"]\n",
1271 indent, "",
0392a017 1272 i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
10c4c98a
GH
1273 r->addr, r->addr + r->size - 1);
1274 }
1275}
03587182
GH
1276
1277static PCIDeviceInfo bridge_info = {
1278 .qdev.name = "pci-bridge",
1279 .qdev.size = sizeof(PCIBridge),
1280 .init = pci_bridge_initfn,
e822a52a 1281 .exit = pci_bridge_exitfn,
03587182
GH
1282 .config_write = pci_bridge_write_config,
1283 .qdev.props = (Property[]) {
1284 DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1285 DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1286 DEFINE_PROP_END_OF_LIST(),
1287 }
1288};
1289
1290static void pci_register_devices(void)
1291{
1292 pci_qdev_register(&bridge_info);
1293}
1294
1295device_init(pci_register_devices)
This page took 0.519983 seconds and 4 git commands to generate.