Use pci->header_type in pci_bar() to avoid unnecessary pci_config_readb.
[seabios.git] / src / pciinit.c
1 // Initialize PCI devices (on emulators)
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "pci.h" // pci_config_readl
10 #include "biosvar.h" // GET_EBDA
11 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
12 #include "pci_regs.h" // PCI_COMMAND
13 #include "xen.h" // usingXen
14
15 #define PCI_IO_INDEX_SHIFT 2
16 #define PCI_MEM_INDEX_SHIFT 12
17
18 #define PCI_BRIDGE_IO_MIN      0x1000
19 #define PCI_BRIDGE_MEM_MIN   0x100000
20
21 enum pci_region_type {
22     PCI_REGION_TYPE_IO,
23     PCI_REGION_TYPE_MEM,
24     PCI_REGION_TYPE_PREFMEM,
25     PCI_REGION_TYPE_COUNT,
26 };
27
28 static const char *region_type_name[] = {
29     [ PCI_REGION_TYPE_IO ]      = "io",
30     [ PCI_REGION_TYPE_MEM ]     = "mem",
31     [ PCI_REGION_TYPE_PREFMEM ] = "prefmem",
32 };
33
34 static struct pci_bus {
35     struct {
36         /* pci region stats */
37         u32 count[32 - PCI_MEM_INDEX_SHIFT];
38         u32 sum, max;
39         /* seconday bus region sizes */
40         u32 size;
41         /* pci region assignments */
42         u32 bases[32 - PCI_MEM_INDEX_SHIFT];
43         u32 base;
44     } r[PCI_REGION_TYPE_COUNT];
45 } *busses;
46 static int busses_count;
47
48 static int pci_size_to_index(u32 size, enum pci_region_type type)
49 {
50     int index = __fls(size);
51     int shift = (type == PCI_REGION_TYPE_IO) ?
52         PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
53
54     if (index < shift)
55         index = shift;
56     index -= shift;
57     return index;
58 }
59
60 static u32 pci_index_to_size(int index, enum pci_region_type type)
61 {
62     int shift = (type == PCI_REGION_TYPE_IO) ?
63         PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
64
65     return 0x1 << (index + shift);
66 }
67
68 static enum pci_region_type pci_addr_to_type(u32 addr)
69 {
70     if (addr & PCI_BASE_ADDRESS_SPACE_IO)
71         return PCI_REGION_TYPE_IO;
72     if (addr & PCI_BASE_ADDRESS_MEM_PREFETCH)
73         return PCI_REGION_TYPE_PREFMEM;
74     return PCI_REGION_TYPE_MEM;
75 }
76
77 static u32 pci_bar(struct pci_device *pci, int region_num)
78 {
79     if (region_num != PCI_ROM_SLOT) {
80         return PCI_BASE_ADDRESS_0 + region_num * 4;
81     }
82
83 #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
84     u8 type = pci->header_type & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
85     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
86 }
87
88 static void
89 pci_set_io_region_addr(struct pci_device *pci, int region_num, u32 addr)
90 {
91     pci_config_writel(pci->bdf, pci_bar(pci, region_num), addr);
92 }
93
94
95 /****************************************************************
96  * Misc. device init
97  ****************************************************************/
98
99 /* host irqs corresponding to PCI irqs A-D */
100 const u8 pci_irqs[4] = {
101     10, 10, 11, 11
102 };
103
104 /* return the global irq number corresponding to a given device irq
105    pin. We could also use the bus number to have a more precise
106    mapping. */
107 static int pci_slot_get_pirq(u16 bdf, int irq_num)
108 {
109     int slot_addend = pci_bdf_to_dev(bdf) - 1;
110     return (irq_num + slot_addend) & 3;
111 }
112
113 /* PIIX3/PIIX4 PCI to ISA bridge */
114 static void piix_isa_bridge_init(struct pci_device *pci, void *arg)
115 {
116     int i, irq;
117     u8 elcr[2];
118
119     elcr[0] = 0x00;
120     elcr[1] = 0x00;
121     for (i = 0; i < 4; i++) {
122         irq = pci_irqs[i];
123         /* set to trigger level */
124         elcr[irq >> 3] |= (1 << (irq & 7));
125         /* activate irq remapping in PIIX */
126         pci_config_writeb(pci->bdf, 0x60 + i, irq);
127     }
128     outb(elcr[0], 0x4d0);
129     outb(elcr[1], 0x4d1);
130     dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n", elcr[0], elcr[1]);
131 }
132
133 static const struct pci_device_id pci_isa_bridge_tbl[] = {
134     /* PIIX3/PIIX4 PCI to ISA bridge */
135     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
136                piix_isa_bridge_init),
137     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
138                piix_isa_bridge_init),
139
140     PCI_DEVICE_END
141 };
142
143 static void storage_ide_init(struct pci_device *pci, void *arg)
144 {
145     /* IDE: we map it as in ISA mode */
146     pci_set_io_region_addr(pci, 0, PORT_ATA1_CMD_BASE);
147     pci_set_io_region_addr(pci, 1, PORT_ATA1_CTRL_BASE);
148     pci_set_io_region_addr(pci, 2, PORT_ATA2_CMD_BASE);
149     pci_set_io_region_addr(pci, 3, PORT_ATA2_CTRL_BASE);
150 }
151
152 /* PIIX3/PIIX4 IDE */
153 static void piix_ide_init(struct pci_device *pci, void *arg)
154 {
155     u16 bdf = pci->bdf;
156     pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
157     pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
158 }
159
160 static void pic_ibm_init(struct pci_device *pci, void *arg)
161 {
162     /* PIC, IBM, MPIC & MPIC2 */
163     pci_set_io_region_addr(pci, 0, 0x80800000 + 0x00040000);
164 }
165
166 static void apple_macio_init(struct pci_device *pci, void *arg)
167 {
168     /* macio bridge */
169     pci_set_io_region_addr(pci, 0, 0x80800000);
170 }
171
172 static const struct pci_device_id pci_class_tbl[] = {
173     /* STORAGE IDE */
174     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
175                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
176     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
177                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
178     PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
179                      storage_ide_init),
180
181     /* PIC, IBM, MIPC & MPIC2 */
182     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
183                      pic_ibm_init),
184     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
185                      pic_ibm_init),
186
187     /* 0xff00 */
188     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_init),
189     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_init),
190
191     PCI_DEVICE_END,
192 };
193
194 /* PIIX4 Power Management device (for ACPI) */
195 static void piix4_pm_init(struct pci_device *pci, void *arg)
196 {
197     u16 bdf = pci->bdf;
198     // acpi sci is hardwired to 9
199     pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
200
201     pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
202     pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
203     pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
204     pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
205 }
206
207 static const struct pci_device_id pci_device_tbl[] = {
208     /* PIIX4 Power Management device (for ACPI) */
209     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
210                piix4_pm_init),
211
212     PCI_DEVICE_END,
213 };
214
215 static void pci_bios_init_device(struct pci_device *pci)
216 {
217     u16 bdf = pci->bdf;
218     int pin, pic_irq;
219
220     dprintf(1, "PCI: init bdf=%02x:%02x.%x id=%04x:%04x\n"
221             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
222             , pci->vendor, pci->device);
223     pci_init_device(pci_class_tbl, pci, NULL);
224
225     /* enable memory mappings */
226     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
227
228     /* map the interrupt */
229     pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
230     if (pin != 0) {
231         pin = pci_slot_get_pirq(bdf, pin - 1);
232         pic_irq = pci_irqs[pin];
233         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
234     }
235
236     pci_init_device(pci_device_tbl, pci, NULL);
237 }
238
239 static void pci_bios_init_device_in_bus(int bus)
240 {
241     struct pci_device *pci;
242     foreachpci(pci) {
243         u8 pci_bus = pci_bdf_to_bus(pci->bdf);
244         if (pci_bus < bus)
245             continue;
246         if (pci_bus > bus)
247             break;
248         pci_bios_init_device(pci);
249     }
250 }
251
252
253 /****************************************************************
254  * Bus initialization
255  ****************************************************************/
256
257 static void
258 pci_bios_init_bus_rec(int bus, u8 *pci_bus)
259 {
260     int bdf;
261     u16 class;
262
263     dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
264
265     /* prevent accidental access to unintended devices */
266     foreachbdf(bdf, bus) {
267         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
268         if (class == PCI_CLASS_BRIDGE_PCI) {
269             pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255);
270             pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0);
271         }
272     }
273
274     foreachbdf(bdf, bus) {
275         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
276         if (class != PCI_CLASS_BRIDGE_PCI) {
277             continue;
278         }
279         dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
280
281         u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
282         if (pribus != bus) {
283             dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
284             pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
285         } else {
286             dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
287         }
288
289         u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
290         (*pci_bus)++;
291         if (*pci_bus != secbus) {
292             dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
293                     secbus, *pci_bus);
294             secbus = *pci_bus;
295             pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
296         } else {
297             dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
298         }
299
300         /* set to max for access to all subordinate buses.
301            later set it to accurate value */
302         u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS);
303         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255);
304
305         pci_bios_init_bus_rec(secbus, pci_bus);
306
307         if (subbus != *pci_bus) {
308             dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
309                     subbus, *pci_bus);
310             subbus = *pci_bus;
311         } else {
312             dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
313         }
314         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
315     }
316 }
317
318 static void
319 pci_bios_init_bus(void)
320 {
321     u8 pci_bus = 0;
322     pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
323     busses_count = pci_bus + 1;
324 }
325
326
327 /****************************************************************
328  * Bus sizing
329  ****************************************************************/
330
331 static u32 pci_size_roundup(u32 size)
332 {
333     int index = __fls(size-1)+1;
334     return 0x1 << index;
335 }
336
337 static void
338 pci_bios_get_bar(struct pci_device *pci, int bar, u32 *val, u32 *size)
339 {
340     u32 ofs = pci_bar(pci, bar);
341     u16 bdf = pci->bdf;
342     u32 old = pci_config_readl(bdf, ofs);
343     u32 mask;
344
345     if (bar == PCI_ROM_SLOT) {
346         mask = PCI_ROM_ADDRESS_MASK;
347         pci_config_writel(bdf, ofs, mask);
348     } else {
349         if (old & PCI_BASE_ADDRESS_SPACE_IO)
350             mask = PCI_BASE_ADDRESS_IO_MASK;
351         else
352             mask = PCI_BASE_ADDRESS_MEM_MASK;
353         pci_config_writel(bdf, ofs, ~0);
354     }
355     *val = pci_config_readl(bdf, ofs);
356     pci_config_writel(bdf, ofs, old);
357     *size = (~(*val & mask)) + 1;
358 }
359
360 static void pci_bios_bus_reserve(struct pci_bus *bus, int type, u32 size)
361 {
362     u32 index;
363
364     index = pci_size_to_index(size, type);
365     size = pci_index_to_size(index, type);
366     bus->r[type].count[index]++;
367     bus->r[type].sum += size;
368     if (bus->r[type].max < size)
369         bus->r[type].max = size;
370 }
371
372 static void pci_bios_check_device_in_bus(int bus);
373
374 static void pci_bios_check_device(struct pci_bus *bus, struct pci_device *dev)
375 {
376     if (dev->class == PCI_CLASS_BRIDGE_PCI) {
377         if (dev->secondary_bus >= busses_count) {
378             /* should never trigger */
379             dprintf(1, "PCI: bus count too small (%d), skipping bus #%d\n",
380                     busses_count, dev->secondary_bus);
381             return;
382         }
383         struct pci_bus *s = busses + dev->secondary_bus;
384         pci_bios_check_device_in_bus(dev->secondary_bus);
385         int type;
386         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
387             u32 limit = (type == PCI_REGION_TYPE_IO) ?
388                 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
389             s->r[type].size = s->r[type].sum;
390             if (s->r[type].size < limit)
391                 s->r[type].size = limit;
392             s->r[type].size = pci_size_roundup(s->r[type].size);
393             pci_bios_bus_reserve(bus, type, s->r[type].size);
394         }
395         dprintf(1, "PCI: secondary bus %d sizes: io %x, mem %x, prefmem %x\n",
396                 dev->secondary_bus,
397                 s->r[PCI_REGION_TYPE_IO].size,
398                 s->r[PCI_REGION_TYPE_MEM].size,
399                 s->r[PCI_REGION_TYPE_PREFMEM].size);
400         return;
401     }
402
403     int i;
404     for (i = 0; i < PCI_NUM_REGIONS; i++) {
405         u32 val, size;
406         pci_bios_get_bar(dev, i, &val, &size);
407         if (val == 0) {
408             continue;
409         }
410         pci_bios_bus_reserve(bus, pci_addr_to_type(val), size);
411         dev->bars[i].addr = val;
412         dev->bars[i].size = size;
413         dev->bars[i].is64 = (!(val & PCI_BASE_ADDRESS_SPACE_IO) &&
414             (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64);
415
416         if (dev->bars[i].is64) {
417             i++;
418         }
419     }
420 }
421
422 static void pci_bios_check_device_in_bus(int bus)
423 {
424     struct pci_device *pci;
425
426     dprintf(1, "PCI: check devices bus %d\n", bus);
427     foreachpci(pci) {
428         if (pci_bdf_to_bus(pci->bdf) != bus)
429             continue;
430         pci_bios_check_device(&busses[bus], pci);
431     }
432 }
433
434 #define ROOT_BASE(top, sum, max) ALIGN_DOWN((top)-(sum),(max) ?: 1)
435
436 static int pci_bios_init_root_regions(u32 start, u32 end)
437 {
438     struct pci_bus *bus = &busses[0];
439
440     bus->r[PCI_REGION_TYPE_IO].base = 0xc000;
441
442     if (bus->r[PCI_REGION_TYPE_MEM].sum < bus->r[PCI_REGION_TYPE_PREFMEM].sum) {
443         bus->r[PCI_REGION_TYPE_MEM].base =
444             ROOT_BASE(end,
445                       bus->r[PCI_REGION_TYPE_MEM].sum,
446                       bus->r[PCI_REGION_TYPE_MEM].max);
447         bus->r[PCI_REGION_TYPE_PREFMEM].base =
448             ROOT_BASE(bus->r[PCI_REGION_TYPE_MEM].base,
449                       bus->r[PCI_REGION_TYPE_PREFMEM].sum,
450                       bus->r[PCI_REGION_TYPE_PREFMEM].max);
451         if (bus->r[PCI_REGION_TYPE_PREFMEM].base >= start) {
452             return 0;
453         }
454     } else {
455         bus->r[PCI_REGION_TYPE_PREFMEM].base =
456             ROOT_BASE(end,
457                       bus->r[PCI_REGION_TYPE_PREFMEM].sum,
458                       bus->r[PCI_REGION_TYPE_PREFMEM].max);
459         bus->r[PCI_REGION_TYPE_MEM].base =
460             ROOT_BASE(bus->r[PCI_REGION_TYPE_PREFMEM].base,
461                       bus->r[PCI_REGION_TYPE_MEM].sum,
462                       bus->r[PCI_REGION_TYPE_MEM].max);
463         if (bus->r[PCI_REGION_TYPE_MEM].base >= start) {
464             return 0;
465         }
466     }
467     return -1;
468 }
469
470
471 /****************************************************************
472  * BAR assignment
473  ****************************************************************/
474
475 static void pci_bios_init_bus_bases(struct pci_bus *bus)
476 {
477     u32 base, newbase, size;
478     int type, i;
479
480     for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
481         dprintf(1, "  type %s max %x sum %x base %x\n", region_type_name[type],
482                 bus->r[type].max, bus->r[type].sum, bus->r[type].base);
483         base = bus->r[type].base;
484         for (i = ARRAY_SIZE(bus->r[type].count)-1; i >= 0; i--) {
485             size = pci_index_to_size(i, type);
486             if (!bus->r[type].count[i])
487                 continue;
488             newbase = base + size * bus->r[type].count[i];
489             dprintf(1, "    size %8x: %d bar(s), %8x -> %8x\n",
490                     size, bus->r[type].count[i], base, newbase - 1);
491             bus->r[type].bases[i] = base;
492             base = newbase;
493         }
494     }
495 }
496
497 static u32 pci_bios_bus_get_addr(struct pci_bus *bus, int type, u32 size)
498 {
499     u32 index, addr;
500
501     index = pci_size_to_index(size, type);
502     addr = bus->r[type].bases[index];
503     bus->r[type].bases[index] += pci_index_to_size(index, type);
504     return addr;
505 }
506
507 #define PCI_IO_SHIFT            8
508 #define PCI_MEMORY_SHIFT        16
509 #define PCI_PREF_MEMORY_SHIFT   16
510
511 static void pci_bios_map_device_in_bus(int bus);
512
513 static void pci_bios_map_device(struct pci_bus *bus, struct pci_device *dev)
514 {
515     if (dev->class == PCI_CLASS_BRIDGE_PCI) {
516         if (dev->secondary_bus >= busses_count) {
517             return;
518         }
519         struct pci_bus *s = busses + dev->secondary_bus;
520         u32 base, limit;
521
522         int type;
523         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
524             s->r[type].base = pci_bios_bus_get_addr(bus, type, s->r[type].size);
525         }
526         dprintf(1, "PCI: init bases bus %d (secondary)\n", dev->secondary_bus);
527         pci_bios_init_bus_bases(s);
528
529         base = s->r[PCI_REGION_TYPE_IO].base;
530         limit = base + s->r[PCI_REGION_TYPE_IO].size - 1;
531         u16 bdf = dev->bdf;
532         pci_config_writeb(bdf, PCI_IO_BASE, base >> PCI_IO_SHIFT);
533         pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
534         pci_config_writeb(bdf, PCI_IO_LIMIT, limit >> PCI_IO_SHIFT);
535         pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
536
537         base = s->r[PCI_REGION_TYPE_MEM].base;
538         limit = base + s->r[PCI_REGION_TYPE_MEM].size - 1;
539         pci_config_writew(bdf, PCI_MEMORY_BASE, base >> PCI_MEMORY_SHIFT);
540         pci_config_writew(bdf, PCI_MEMORY_LIMIT, limit >> PCI_MEMORY_SHIFT);
541
542         base = s->r[PCI_REGION_TYPE_PREFMEM].base;
543         limit = base + s->r[PCI_REGION_TYPE_PREFMEM].size - 1;
544         pci_config_writew(bdf, PCI_PREF_MEMORY_BASE, base >> PCI_PREF_MEMORY_SHIFT);
545         pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT, limit >> PCI_PREF_MEMORY_SHIFT);
546         pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
547         pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
548
549         pci_bios_map_device_in_bus(dev->secondary_bus);
550         return;
551     }
552
553     int i;
554     for (i = 0; i < PCI_NUM_REGIONS; i++) {
555         u32 addr;
556         if (dev->bars[i].addr == 0) {
557             continue;
558         }
559
560         addr = pci_bios_bus_get_addr(bus, pci_addr_to_type(dev->bars[i].addr),
561                                      dev->bars[i].size);
562         dprintf(1, "  bar %d, addr %x, size %x [%s]\n",
563                 i, addr, dev->bars[i].size,
564                 region_type_name[pci_addr_to_type(dev->bars[i].addr)]);
565         pci_set_io_region_addr(dev, i, addr);
566
567         if (dev->bars[i].is64) {
568             i++;
569         }
570     }
571 }
572
573 static void pci_bios_map_device_in_bus(int bus)
574 {
575     struct pci_device *pci;
576
577     foreachpci(pci) {
578         u16 bdf = pci->bdf;
579         if (pci_bdf_to_bus(bdf) != bus)
580             continue;
581         dprintf(1, "PCI: map device bdf=%02x:%02x.%x\n"
582                 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
583         pci_bios_map_device(&busses[bus], pci);
584     }
585 }
586
587
588 /****************************************************************
589  * Main setup code
590  ****************************************************************/
591
592 void
593 pci_setup(void)
594 {
595     if (CONFIG_COREBOOT || usingXen()) {
596         // PCI setup already done by coreboot or Xen - just do probe.
597         pci_probe_devices();
598         return;
599     }
600
601     dprintf(3, "pci setup\n");
602
603     u32 start = BUILD_PCIMEM_START;
604     u32 end   = BUILD_PCIMEM_END;
605
606     dprintf(1, "=== PCI bus & bridge init ===\n");
607     if (pci_probe_host() != 0) {
608         return;
609     }
610     pci_bios_init_bus();
611
612     dprintf(1, "=== PCI device probing ===\n");
613     pci_probe_devices();
614
615     dprintf(1, "=== PCI new allocation pass #1 ===\n");
616     busses = malloc_tmp(sizeof(*busses) * busses_count);
617     memset(busses, 0, sizeof(*busses) * busses_count);
618     pci_bios_check_device_in_bus(0 /* host bus */);
619     if (pci_bios_init_root_regions(start, end) != 0) {
620         panic("PCI: out of address space\n");
621     }
622
623     dprintf(1, "=== PCI new allocation pass #2 ===\n");
624     dprintf(1, "PCI: init bases bus 0 (primary)\n");
625     pci_bios_init_bus_bases(&busses[0]);
626     pci_bios_map_device_in_bus(0 /* host bus */);
627
628     pci_bios_init_device_in_bus(0 /* host bus */);
629
630     struct pci_device *pci;
631     foreachpci(pci) {
632         pci_init_device(pci_isa_bridge_tbl, pci, NULL);
633     }
634
635     free(busses);
636     busses_count = 0;
637 }