qemu: initialize hot add system / acpi gpe (Marcelo Tosatti)
[sniper_test.git] / hw / pci.c
blobcd07273c832f95f6a4329919af772d95884727fc
1 /*
2 * QEMU PCI bus manager
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw.h"
25 #include "pci.h"
26 #include "console.h"
27 #include "net.h"
28 #include "virtio-net.h"
30 //#define DEBUG_PCI
32 struct PCIBus {
33 int bus_num;
34 int devfn_min;
35 pci_set_irq_fn set_irq;
36 pci_map_irq_fn map_irq;
37 uint32_t config_reg; /* XXX: suppress */
38 /* low level pic */
39 SetIRQFunc *low_set_irq;
40 qemu_irq *irq_opaque;
41 PCIDevice *devices[256];
42 PCIDevice *parent_dev;
43 PCIBus *next;
44 /* The bus IRQ state is the logical OR of the connected devices.
45 Keep a count of the number of devices with raised IRQs. */
46 int nirq;
47 int irq_count[];
50 static void pci_update_mappings(PCIDevice *d);
51 static void pci_set_irq(void *opaque, int irq_num, int level);
53 target_phys_addr_t pci_mem_base;
54 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
55 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
56 static int pci_irq_index;
57 static PCIBus *first_bus;
59 static void pcibus_save(QEMUFile *f, void *opaque)
61 PCIBus *bus = (PCIBus *)opaque;
62 int i;
64 qemu_put_be32(f, bus->nirq);
65 for (i = 0; i < bus->nirq; i++)
66 qemu_put_be32(f, bus->irq_count[i]);
69 static int pcibus_load(QEMUFile *f, void *opaque, int version_id)
71 PCIBus *bus = (PCIBus *)opaque;
72 int i, nirq;
74 if (version_id != 1)
75 return -EINVAL;
77 nirq = qemu_get_be32(f);
78 if (bus->nirq != nirq) {
79 fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
80 nirq, bus->nirq);
81 return -EINVAL;
84 for (i = 0; i < nirq; i++)
85 bus->irq_count[i] = qemu_get_be32(f);
87 return 0;
90 PCIBus *pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
91 qemu_irq *pic, int devfn_min, int nirq)
93 PCIBus *bus;
94 static int nbus = 0;
96 bus = qemu_mallocz(sizeof(PCIBus) + (nirq * sizeof(int)));
97 bus->set_irq = set_irq;
98 bus->map_irq = map_irq;
99 bus->irq_opaque = pic;
100 bus->devfn_min = devfn_min;
101 bus->nirq = nirq;
102 first_bus = bus;
103 register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
104 return bus;
107 static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
109 PCIBus *bus;
110 bus = qemu_mallocz(sizeof(PCIBus));
111 bus->map_irq = map_irq;
112 bus->parent_dev = dev;
113 bus->next = dev->bus->next;
114 dev->bus->next = bus;
115 return bus;
118 int pci_bus_num(PCIBus *s)
120 return s->bus_num;
123 void pci_device_save(PCIDevice *s, QEMUFile *f)
125 int i;
127 qemu_put_be32(f, 2); /* PCI device version */
128 qemu_put_buffer(f, s->config, 256);
129 for (i = 0; i < 4; i++)
130 qemu_put_be32(f, s->irq_state[i]);
133 int pci_device_load(PCIDevice *s, QEMUFile *f)
135 uint32_t version_id;
136 int i;
138 version_id = qemu_get_be32(f);
139 if (version_id > 2)
140 return -EINVAL;
141 qemu_get_buffer(f, s->config, 256);
142 pci_update_mappings(s);
144 if (version_id >= 2)
145 for (i = 0; i < 4; i ++)
146 s->irq_state[i] = qemu_get_be32(f);
148 return 0;
151 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
153 uint16_t *id;
155 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
156 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
157 id[1] = cpu_to_le16(pci_default_sub_device_id);
158 return 0;
161 /* -1 for devfn means auto assign */
162 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
163 int instance_size, int devfn,
164 PCIConfigReadFunc *config_read,
165 PCIConfigWriteFunc *config_write)
167 PCIDevice *pci_dev;
169 if (pci_irq_index >= PCI_DEVICES_MAX)
170 return NULL;
172 if (devfn < 0) {
173 for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
174 if (!bus->devices[devfn])
175 goto found;
177 return NULL;
178 found: ;
180 pci_dev = qemu_mallocz(instance_size);
181 pci_dev->bus = bus;
182 pci_dev->devfn = devfn;
183 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
184 memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
185 pci_set_default_subsystem_id(pci_dev);
187 if (!config_read)
188 config_read = pci_default_read_config;
189 if (!config_write)
190 config_write = pci_default_write_config;
191 pci_dev->config_read = config_read;
192 pci_dev->config_write = config_write;
193 pci_dev->irq_index = pci_irq_index++;
194 bus->devices[devfn] = pci_dev;
195 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
196 return pci_dev;
199 static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
201 return addr + pci_mem_base;
204 static void pci_unregister_io_regions(PCIDevice *pci_dev)
206 PCIIORegion *r;
207 int i;
209 for(i = 0; i < PCI_NUM_REGIONS; i++) {
210 r = &pci_dev->io_regions[i];
211 if (!r->size || r->addr == -1)
212 continue;
213 if (r->type == PCI_ADDRESS_SPACE_IO) {
214 isa_unassign_ioport(r->addr, r->size);
215 } else {
216 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
217 r->size,
218 IO_MEM_UNASSIGNED);
223 int pci_unregister_device(PCIDevice *pci_dev)
225 int ret = 0;
227 if (pci_dev->unregister)
228 ret = pci_dev->unregister(pci_dev);
229 if (ret)
230 return ret;
232 pci_unregister_io_regions(pci_dev);
234 qemu_free_irqs(pci_dev->irq);
235 pci_irq_index--;
236 pci_dev->bus->devices[pci_dev->devfn] = NULL;
237 qemu_free(pci_dev);
238 return 0;
241 void pci_register_io_region(PCIDevice *pci_dev, int region_num,
242 uint32_t size, int type,
243 PCIMapIORegionFunc *map_func)
245 PCIIORegion *r;
246 uint32_t addr;
248 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
249 return;
251 if (size & (size-1)) {
252 fprintf(stderr, "ERROR: PCI region size must be pow2 "
253 "type=0x%x, size=0x%x\n", type, size);
254 exit(1);
257 r = &pci_dev->io_regions[region_num];
258 r->addr = -1;
259 r->size = size;
260 r->type = type;
261 r->map_func = map_func;
262 if (region_num == PCI_ROM_SLOT) {
263 addr = 0x30;
264 } else {
265 addr = 0x10 + region_num * 4;
267 *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
270 static void pci_update_mappings(PCIDevice *d)
272 PCIIORegion *r;
273 int cmd, i;
274 uint32_t last_addr, new_addr, config_ofs;
276 cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
277 for(i = 0; i < PCI_NUM_REGIONS; i++) {
278 r = &d->io_regions[i];
279 if (i == PCI_ROM_SLOT) {
280 config_ofs = 0x30;
281 } else {
282 config_ofs = 0x10 + i * 4;
284 if (r->size != 0) {
285 if (r->type & PCI_ADDRESS_SPACE_IO) {
286 if (cmd & PCI_COMMAND_IO) {
287 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
288 config_ofs));
289 new_addr = new_addr & ~(r->size - 1);
290 last_addr = new_addr + r->size - 1;
291 /* NOTE: we have only 64K ioports on PC */
292 if (last_addr <= new_addr || new_addr == 0 ||
293 last_addr >= 0x10000) {
294 new_addr = -1;
296 } else {
297 new_addr = -1;
299 } else {
300 if (cmd & PCI_COMMAND_MEMORY) {
301 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
302 config_ofs));
303 /* the ROM slot has a specific enable bit */
304 if (i == PCI_ROM_SLOT && !(new_addr & 1))
305 goto no_mem_map;
306 new_addr = new_addr & ~(r->size - 1);
307 last_addr = new_addr + r->size - 1;
308 /* NOTE: we do not support wrapping */
309 /* XXX: as we cannot support really dynamic
310 mappings, we handle specific values as invalid
311 mappings. */
312 if (last_addr <= new_addr || new_addr == 0 ||
313 last_addr == -1) {
314 new_addr = -1;
316 } else {
317 no_mem_map:
318 new_addr = -1;
321 /* now do the real mapping */
322 if (new_addr != r->addr) {
323 if (r->addr != -1) {
324 if (r->type & PCI_ADDRESS_SPACE_IO) {
325 int class;
326 /* NOTE: specific hack for IDE in PC case:
327 only one byte must be mapped. */
328 class = d->config[0x0a] | (d->config[0x0b] << 8);
329 if (class == 0x0101 && r->size == 4) {
330 isa_unassign_ioport(r->addr + 2, 1);
331 } else {
332 isa_unassign_ioport(r->addr, r->size);
334 } else {
335 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
336 r->size,
337 IO_MEM_UNASSIGNED);
338 qemu_unregister_coalesced_mmio(r->addr, r->size);
341 r->addr = new_addr;
342 if (r->addr != -1) {
343 r->map_func(d, i, r->addr, r->size, r->type);
350 uint32_t pci_default_read_config(PCIDevice *d,
351 uint32_t address, int len)
353 uint32_t val;
355 switch(len) {
356 default:
357 case 4:
358 if (address <= 0xfc) {
359 val = le32_to_cpu(*(uint32_t *)(d->config + address));
360 break;
362 /* fall through */
363 case 2:
364 if (address <= 0xfe) {
365 val = le16_to_cpu(*(uint16_t *)(d->config + address));
366 break;
368 /* fall through */
369 case 1:
370 val = d->config[address];
371 break;
373 return val;
376 void pci_default_write_config(PCIDevice *d,
377 uint32_t address, uint32_t val, int len)
379 int can_write, i;
380 uint32_t end, addr;
382 if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
383 (address >= 0x30 && address < 0x34))) {
384 PCIIORegion *r;
385 int reg;
387 if ( address >= 0x30 ) {
388 reg = PCI_ROM_SLOT;
389 }else{
390 reg = (address - 0x10) >> 2;
392 r = &d->io_regions[reg];
393 if (r->size == 0)
394 goto default_config;
395 /* compute the stored value */
396 if (reg == PCI_ROM_SLOT) {
397 /* keep ROM enable bit */
398 val &= (~(r->size - 1)) | 1;
399 } else {
400 val &= ~(r->size - 1);
401 val |= r->type;
403 *(uint32_t *)(d->config + address) = cpu_to_le32(val);
404 pci_update_mappings(d);
405 return;
407 default_config:
408 /* not efficient, but simple */
409 addr = address;
410 for(i = 0; i < len; i++) {
411 /* default read/write accesses */
412 switch(d->config[0x0e]) {
413 case 0x00:
414 case 0x80:
415 switch(addr) {
416 case 0x00:
417 case 0x01:
418 case 0x02:
419 case 0x03:
420 case 0x08:
421 case 0x09:
422 case 0x0a:
423 case 0x0b:
424 case 0x0e:
425 case 0x10 ... 0x27: /* base */
426 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
427 case 0x30 ... 0x33: /* rom */
428 case 0x3d:
429 can_write = 0;
430 break;
431 default:
432 can_write = 1;
433 break;
435 break;
436 default:
437 case 0x01:
438 switch(addr) {
439 case 0x00:
440 case 0x01:
441 case 0x02:
442 case 0x03:
443 case 0x08:
444 case 0x09:
445 case 0x0a:
446 case 0x0b:
447 case 0x0e:
448 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
449 case 0x38 ... 0x3b: /* rom */
450 case 0x3d:
451 can_write = 0;
452 break;
453 default:
454 can_write = 1;
455 break;
457 break;
459 if (can_write) {
460 /* Mask out writes to reserved bits in registers */
461 switch (addr) {
462 case 0x05:
463 val &= ~PCI_COMMAND_RESERVED_MASK_HI;
464 break;
465 case 0x06:
466 val &= ~PCI_STATUS_RESERVED_MASK_LO;
467 break;
468 case 0x07:
469 val &= ~PCI_STATUS_RESERVED_MASK_HI;
470 break;
472 d->config[addr] = val;
474 if (++addr > 0xff)
475 break;
476 val >>= 8;
479 end = address + len;
480 if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
481 /* if the command register is modified, we must modify the mappings */
482 pci_update_mappings(d);
486 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
488 PCIBus *s = opaque;
489 PCIDevice *pci_dev;
490 int config_addr, bus_num;
492 #if defined(DEBUG_PCI) && 0
493 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
494 addr, val, len);
495 #endif
496 bus_num = (addr >> 16) & 0xff;
497 while (s && s->bus_num != bus_num)
498 s = s->next;
499 if (!s)
500 return;
501 pci_dev = s->devices[(addr >> 8) & 0xff];
502 if (!pci_dev)
503 return;
504 config_addr = addr & 0xff;
505 #if defined(DEBUG_PCI)
506 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
507 pci_dev->name, config_addr, val, len);
508 #endif
509 pci_dev->config_write(pci_dev, config_addr, val, len);
512 uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
514 PCIBus *s = opaque;
515 PCIDevice *pci_dev;
516 int config_addr, bus_num;
517 uint32_t val;
519 bus_num = (addr >> 16) & 0xff;
520 while (s && s->bus_num != bus_num)
521 s= s->next;
522 if (!s)
523 goto fail;
524 pci_dev = s->devices[(addr >> 8) & 0xff];
525 if (!pci_dev) {
526 fail:
527 switch(len) {
528 case 1:
529 val = 0xff;
530 break;
531 case 2:
532 val = 0xffff;
533 break;
534 default:
535 case 4:
536 val = 0xffffffff;
537 break;
539 goto the_end;
541 config_addr = addr & 0xff;
542 val = pci_dev->config_read(pci_dev, config_addr, len);
543 #if defined(DEBUG_PCI)
544 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
545 pci_dev->name, config_addr, val, len);
546 #endif
547 the_end:
548 #if defined(DEBUG_PCI) && 0
549 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
550 addr, val, len);
551 #endif
552 return val;
555 /***********************************************************/
556 /* generic PCI irq support */
558 /* 0 <= irq_num <= 3. level must be 0 or 1 */
559 static void pci_set_irq(void *opaque, int irq_num, int level)
561 PCIDevice *pci_dev = (PCIDevice *)opaque;
562 PCIBus *bus;
563 int change;
565 change = level - pci_dev->irq_state[irq_num];
566 if (!change)
567 return;
569 pci_dev->irq_state[irq_num] = level;
570 for (;;) {
571 bus = pci_dev->bus;
572 irq_num = bus->map_irq(pci_dev, irq_num);
573 if (bus->set_irq)
574 break;
575 pci_dev = bus->parent_dev;
577 bus->irq_count[irq_num] += change;
578 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
581 /***********************************************************/
582 /* monitor info on PCI */
584 typedef struct {
585 uint16_t class;
586 const char *desc;
587 } pci_class_desc;
589 static const pci_class_desc pci_class_descriptions[] =
591 { 0x0100, "SCSI controller"},
592 { 0x0101, "IDE controller"},
593 { 0x0102, "Floppy controller"},
594 { 0x0103, "IPI controller"},
595 { 0x0104, "RAID controller"},
596 { 0x0106, "SATA controller"},
597 { 0x0107, "SAS controller"},
598 { 0x0180, "Storage controller"},
599 { 0x0200, "Ethernet controller"},
600 { 0x0201, "Token Ring controller"},
601 { 0x0202, "FDDI controller"},
602 { 0x0203, "ATM controller"},
603 { 0x0280, "Network controller"},
604 { 0x0300, "VGA controller"},
605 { 0x0301, "XGA controller"},
606 { 0x0302, "3D controller"},
607 { 0x0380, "Display controller"},
608 { 0x0400, "Video controller"},
609 { 0x0401, "Audio controller"},
610 { 0x0402, "Phone"},
611 { 0x0480, "Multimedia controller"},
612 { 0x0500, "RAM controller"},
613 { 0x0501, "Flash controller"},
614 { 0x0580, "Memory controller"},
615 { 0x0600, "Host bridge"},
616 { 0x0601, "ISA bridge"},
617 { 0x0602, "EISA bridge"},
618 { 0x0603, "MC bridge"},
619 { 0x0604, "PCI bridge"},
620 { 0x0605, "PCMCIA bridge"},
621 { 0x0606, "NUBUS bridge"},
622 { 0x0607, "CARDBUS bridge"},
623 { 0x0608, "RACEWAY bridge"},
624 { 0x0680, "Bridge"},
625 { 0x0c03, "USB controller"},
626 { 0, NULL}
629 static void pci_info_device(PCIDevice *d)
631 int i, class;
632 PCIIORegion *r;
633 const pci_class_desc *desc;
635 term_printf(" Bus %2d, device %3d, function %d:\n",
636 d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
637 class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
638 term_printf(" ");
639 desc = pci_class_descriptions;
640 while (desc->desc && class != desc->class)
641 desc++;
642 if (desc->desc) {
643 term_printf("%s", desc->desc);
644 } else {
645 term_printf("Class %04x", class);
647 term_printf(": PCI device %04x:%04x\n",
648 le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
649 le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
651 if (d->config[PCI_INTERRUPT_PIN] != 0) {
652 term_printf(" IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]);
654 if (class == 0x0604) {
655 term_printf(" BUS %d.\n", d->config[0x19]);
657 for(i = 0;i < PCI_NUM_REGIONS; i++) {
658 r = &d->io_regions[i];
659 if (r->size != 0) {
660 term_printf(" BAR%d: ", i);
661 if (r->type & PCI_ADDRESS_SPACE_IO) {
662 term_printf("I/O at 0x%04x [0x%04x].\n",
663 r->addr, r->addr + r->size - 1);
664 } else {
665 term_printf("32 bit memory at 0x%08x [0x%08x].\n",
666 r->addr, r->addr + r->size - 1);
670 if (class == 0x0604 && d->config[0x19] != 0) {
671 pci_for_each_device(d->config[0x19], pci_info_device);
675 void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
677 PCIBus *bus = first_bus;
678 PCIDevice *d;
679 int devfn;
681 while (bus && bus->bus_num != bus_num)
682 bus = bus->next;
683 if (bus) {
684 for(devfn = 0; devfn < 256; devfn++) {
685 d = bus->devices[devfn];
686 if (d)
687 fn(d);
692 void pci_info(void)
694 pci_for_each_device(0, pci_info_device);
697 static const char * const pci_nic_models[] = {
698 "ne2k_pci",
699 "i82551",
700 "i82557b",
701 "i82559er",
702 "rtl8139",
703 "e1000",
704 "pcnet",
705 "virtio",
706 NULL
709 typedef PCIDevice *(*PCINICInitFn)(PCIBus *, NICInfo *, int);
711 static PCINICInitFn pci_nic_init_fns[] = {
712 pci_ne2000_init,
713 pci_i82551_init,
714 pci_i82557b_init,
715 pci_i82559er_init,
716 pci_rtl8139_init,
717 pci_e1000_init,
718 pci_pcnet_init,
719 virtio_net_init,
720 NULL
723 /* Initialize a PCI NIC. */
724 PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
725 const char *default_model)
727 PCIDevice *pci_dev;
728 int i;
730 qemu_check_nic_model_list(nd, pci_nic_models, default_model);
732 for (i = 0; pci_nic_models[i]; i++)
733 if (strcmp(nd->model, pci_nic_models[i]) == 0) {
734 pci_dev = pci_nic_init_fns[i](bus, nd, devfn);
735 if (pci_dev)
736 nd->private = pci_dev;
737 return pci_dev;
740 return NULL;
743 typedef struct {
744 PCIDevice dev;
745 PCIBus *bus;
746 } PCIBridge;
748 static void pci_bridge_write_config(PCIDevice *d,
749 uint32_t address, uint32_t val, int len)
751 PCIBridge *s = (PCIBridge *)d;
753 if (address == 0x19 || (address == 0x18 && len > 1)) {
754 if (address == 0x19)
755 s->bus->bus_num = val & 0xff;
756 else
757 s->bus->bus_num = (val >> 8) & 0xff;
758 #if defined(DEBUG_PCI)
759 printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
760 #endif
762 pci_default_write_config(d, address, val, len);
765 PCIBus *pci_find_bus(int bus_num)
767 PCIBus *bus = first_bus;
769 while (bus && bus->bus_num != bus_num)
770 bus = bus->next;
772 return bus;
775 PCIDevice *pci_find_device(int bus_num, int slot, int function)
777 PCIBus *bus = pci_find_bus(bus_num);
779 if (!bus)
780 return NULL;
782 return bus->devices[PCI_DEVFN(slot, function)];
785 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
786 pci_map_irq_fn map_irq, const char *name)
788 PCIBridge *s;
789 s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
790 devfn, NULL, pci_bridge_write_config);
792 pci_config_set_vendor_id(s->dev.config, vid);
793 pci_config_set_device_id(s->dev.config, did);
795 s->dev.config[0x04] = 0x06; // command = bus master, pci mem
796 s->dev.config[0x05] = 0x00;
797 s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
798 s->dev.config[0x07] = 0x00; // status = fast devsel
799 s->dev.config[0x08] = 0x00; // revision
800 s->dev.config[0x09] = 0x00; // programming i/f
801 pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
802 s->dev.config[0x0D] = 0x10; // latency_timer
803 s->dev.config[0x0E] = 0x81; // header_type
804 s->dev.config[0x1E] = 0xa0; // secondary status
806 s->bus = pci_register_secondary_bus(&s->dev, map_irq);
807 return s->bus;