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