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