9314698b4515ec5bc9f4867640472a2015d485ec
[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 static struct pci_region pci_bios_io_region;
22 static struct pci_region pci_bios_mem_region;
23 static struct pci_region pci_bios_prefmem_region;
24
25 enum pci_region_type {
26     PCI_REGION_TYPE_IO,
27     PCI_REGION_TYPE_MEM,
28     PCI_REGION_TYPE_PREFMEM,
29     PCI_REGION_TYPE_COUNT,
30 };
31
32 static const char *region_type_name[] = {
33     [ PCI_REGION_TYPE_IO ]      = "io",
34     [ PCI_REGION_TYPE_MEM ]     = "mem",
35     [ PCI_REGION_TYPE_PREFMEM ] = "prefmem",
36 };
37
38 static struct pci_bus {
39     struct {
40         /* pci region stats */
41         u32 count[32 - PCI_MEM_INDEX_SHIFT];
42         u32 sum, max;
43         /* seconday bus region sizes */
44         u32 size;
45         /* pci region assignments */
46         u32 bases[32 - PCI_MEM_INDEX_SHIFT];
47         u32 base;
48     } r[PCI_REGION_TYPE_COUNT];
49 } *busses;
50 static int busses_count;
51
52 static void pci_bios_init_device_in_bus(int bus);
53 static void pci_bios_check_device_in_bus(int bus);
54 static void pci_bios_init_bus_bases(struct pci_bus *bus);
55 static void pci_bios_map_device_in_bus(int bus);
56
57 static int pci_size_to_index(u32 size, enum pci_region_type type)
58 {
59     int index = __fls(size);
60     int shift = (type == PCI_REGION_TYPE_IO) ?
61         PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
62
63     if (index < shift)
64         index = shift;
65     index -= shift;
66     return index;
67 }
68
69 static u32 pci_index_to_size(int index, enum pci_region_type type)
70 {
71     int shift = (type == PCI_REGION_TYPE_IO) ?
72         PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
73
74     return 0x1 << (index + shift);
75 }
76
77 static enum pci_region_type pci_addr_to_type(u32 addr)
78 {
79     if (addr & PCI_BASE_ADDRESS_SPACE_IO)
80         return PCI_REGION_TYPE_IO;
81     if (addr & PCI_BASE_ADDRESS_MEM_PREFETCH)
82         return PCI_REGION_TYPE_PREFMEM;
83     return PCI_REGION_TYPE_MEM;
84 }
85
86 static u32 pci_size_roundup(u32 size)
87 {
88     int index = __fls(size-1)+1;
89     return 0x1 << index;
90 }
91
92 /* host irqs corresponding to PCI irqs A-D */
93 const u8 pci_irqs[4] = {
94     10, 10, 11, 11
95 };
96
97 static u32 pci_bar(u16 bdf, int region_num)
98 {
99     if (region_num != PCI_ROM_SLOT) {
100         return PCI_BASE_ADDRESS_0 + region_num * 4;
101     }
102
103 #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
104     u8 type = pci_config_readb(bdf, PCI_HEADER_TYPE);
105     type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
106     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
107 }
108
109 static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
110 {
111     u32 ofs;
112
113     ofs = pci_bar(bdf, region_num);
114
115     pci_config_writel(bdf, ofs, addr);
116     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
117 }
118
119 /*
120  * return value
121  *      0:     32bit BAR
122  *      non 0: 64bit BAR
123  */
124 static int pci_bios_allocate_region(u16 bdf, int region_num)
125 {
126     struct pci_region *r;
127     u32 ofs = pci_bar(bdf, region_num);
128
129     u32 old = pci_config_readl(bdf, ofs);
130     u32 mask;
131     if (region_num == PCI_ROM_SLOT) {
132         mask = PCI_ROM_ADDRESS_MASK;
133         pci_config_writel(bdf, ofs, mask);
134     } else {
135         if (old & PCI_BASE_ADDRESS_SPACE_IO)
136             mask = PCI_BASE_ADDRESS_IO_MASK;
137         else
138             mask = PCI_BASE_ADDRESS_MEM_MASK;
139         pci_config_writel(bdf, ofs, ~0);
140     }
141     u32 val = pci_config_readl(bdf, ofs);
142     pci_config_writel(bdf, ofs, old);
143
144     u32 size = (~(val & mask)) + 1;
145     if (val != 0) {
146         const char *type;
147         const char *msg;
148         if (val & PCI_BASE_ADDRESS_SPACE_IO) {
149             r = &pci_bios_io_region;
150             type = "io";
151             msg = "";
152         } else if ((val & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
153                    /* keep behaviour on bus = 0 */
154                    pci_bdf_to_bus(bdf) != 0 &&
155                    /* If pci_bios_prefmem_addr == 0, keep old behaviour */
156                    pci_region_addr(&pci_bios_prefmem_region) != 0) {
157             r = &pci_bios_prefmem_region;
158             type = "prefmem";
159             msg = "decrease BUILD_PCIMEM_SIZE and recompile. size %x";
160         } else {
161             r = &pci_bios_mem_region;
162             type = "mem";
163             msg = "increase BUILD_PCIMEM_SIZE and recompile.";
164         }
165         u32 addr = pci_region_alloc(r, size);
166         if (addr > 0) {
167             pci_set_io_region_addr(bdf, region_num, addr);
168         } else {
169             size = 0;
170             dprintf(1,
171                     "%s region of (bdf 0x%x bar %d) can't be mapped. "
172                     "%s size %x\n",
173                     type, bdf, region_num, msg, pci_region_size(r));
174         }
175     }
176
177     int is_64bit = !(val & PCI_BASE_ADDRESS_SPACE_IO) &&
178         (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64;
179     if (is_64bit && size > 0) {
180         pci_config_writel(bdf, ofs + 4, 0);
181     }
182     return is_64bit;
183 }
184
185 static void pci_bios_allocate_regions(struct pci_device *pci, void *arg)
186 {
187     int i;
188     for (i = 0; i < PCI_NUM_REGIONS; i++) {
189         int is_64bit = pci_bios_allocate_region(pci->bdf, i);
190         if (is_64bit){
191             i++;
192         }
193     }
194 }
195
196 /* return the global irq number corresponding to a given device irq
197    pin. We could also use the bus number to have a more precise
198    mapping. */
199 static int pci_slot_get_pirq(u16 bdf, int irq_num)
200 {
201     int slot_addend = pci_bdf_to_dev(bdf) - 1;
202     return (irq_num + slot_addend) & 3;
203 }
204
205 /* PIIX3/PIIX4 PCI to ISA bridge */
206 static void piix_isa_bridge_init(struct pci_device *pci, void *arg)
207 {
208     int i, irq;
209     u8 elcr[2];
210
211     elcr[0] = 0x00;
212     elcr[1] = 0x00;
213     for (i = 0; i < 4; i++) {
214         irq = pci_irqs[i];
215         /* set to trigger level */
216         elcr[irq >> 3] |= (1 << (irq & 7));
217         /* activate irq remapping in PIIX */
218         pci_config_writeb(pci->bdf, 0x60 + i, irq);
219     }
220     outb(elcr[0], 0x4d0);
221     outb(elcr[1], 0x4d1);
222     dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n", elcr[0], elcr[1]);
223 }
224
225 static const struct pci_device_id pci_isa_bridge_tbl[] = {
226     /* PIIX3/PIIX4 PCI to ISA bridge */
227     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
228                piix_isa_bridge_init),
229     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
230                piix_isa_bridge_init),
231
232     PCI_DEVICE_END
233 };
234
235 #define PCI_IO_ALIGN            4096
236 #define PCI_IO_SHIFT            8
237 #define PCI_MEMORY_ALIGN        (1UL << 20)
238 #define PCI_MEMORY_SHIFT        16
239 #define PCI_PREF_MEMORY_ALIGN   (1UL << 20)
240 #define PCI_PREF_MEMORY_SHIFT   16
241
242 static void pci_bios_init_device_bridge(struct pci_device *pci, void *arg)
243 {
244     u16 bdf = pci->bdf;
245     pci_bios_allocate_region(bdf, 0);
246     pci_bios_allocate_region(bdf, 1);
247     pci_bios_allocate_region(bdf, PCI_ROM_SLOT);
248
249     u32 io_old = pci_region_addr(&pci_bios_io_region);
250     u32 mem_old = pci_region_addr(&pci_bios_mem_region);
251     u32 prefmem_old = pci_region_addr(&pci_bios_prefmem_region);
252
253     /* IO BASE is assumed to be 16 bit */
254     if (pci_region_align(&pci_bios_io_region, PCI_IO_ALIGN) == 0) {
255         pci_region_disable(&pci_bios_io_region);
256     }
257     if (pci_region_align(&pci_bios_mem_region, PCI_MEMORY_ALIGN) == 0) {
258         pci_region_disable(&pci_bios_mem_region);
259     }
260     if (pci_region_align(&pci_bios_prefmem_region,
261                          PCI_PREF_MEMORY_ALIGN) == 0) {
262         pci_region_disable(&pci_bios_prefmem_region);
263     }
264
265     u32 io_base = pci_region_addr(&pci_bios_io_region);
266     u32 mem_base = pci_region_addr(&pci_bios_mem_region);
267     u32 prefmem_base = pci_region_addr(&pci_bios_prefmem_region);
268
269     u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
270     if (secbus > 0) {
271         pci_bios_init_device_in_bus(secbus);
272     }
273
274     u32 io_end = pci_region_align(&pci_bios_io_region, PCI_IO_ALIGN);
275     if (io_end == 0) {
276         pci_region_revert(&pci_bios_io_region, io_old);
277         io_base = 0xffff;
278         io_end = 1;
279     }
280     pci_config_writeb(bdf, PCI_IO_BASE, io_base >> PCI_IO_SHIFT);
281     pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
282     pci_config_writeb(bdf, PCI_IO_LIMIT, (io_end - 1) >> PCI_IO_SHIFT);
283     pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
284
285     u32 mem_end = pci_region_align(&pci_bios_mem_region, PCI_MEMORY_ALIGN);
286     if (mem_end == 0) {
287         pci_region_revert(&pci_bios_mem_region, mem_old);
288         mem_base = 0xffffffff;
289         mem_end = 1;
290     }
291     pci_config_writew(bdf, PCI_MEMORY_BASE, mem_base >> PCI_MEMORY_SHIFT);
292     pci_config_writew(bdf, PCI_MEMORY_LIMIT, (mem_end -1) >> PCI_MEMORY_SHIFT);
293
294     u32 prefmem_end = pci_region_align(&pci_bios_prefmem_region,
295                                        PCI_PREF_MEMORY_ALIGN);
296     if (prefmem_end == 0) {
297         pci_region_revert(&pci_bios_prefmem_region, prefmem_old);
298         prefmem_base = 0xffffffff;
299         prefmem_end = 1;
300     }
301     pci_config_writew(bdf, PCI_PREF_MEMORY_BASE,
302                       prefmem_base >> PCI_PREF_MEMORY_SHIFT);
303     pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT,
304                       (prefmem_end - 1) >> PCI_PREF_MEMORY_SHIFT);
305     pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
306     pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
307
308     dprintf(1, "PCI: br io   = [0x%x, 0x%x)\n", io_base, io_end);
309     dprintf(1, "PCI: br mem  = [0x%x, 0x%x)\n", mem_base, mem_end);
310     dprintf(1, "PCI: br pref = [0x%x, 0x%x)\n", prefmem_base, prefmem_end);
311
312     u16 cmd = pci_config_readw(bdf, PCI_COMMAND);
313     cmd &= ~PCI_COMMAND_IO;
314     if (io_end > io_base) {
315         cmd |= PCI_COMMAND_IO;
316     }
317     cmd &= ~PCI_COMMAND_MEMORY;
318     if (mem_end > mem_base || prefmem_end > prefmem_base) {
319         cmd |= PCI_COMMAND_MEMORY;
320     }
321     cmd |= PCI_COMMAND_MASTER;
322     pci_config_writew(bdf, PCI_COMMAND, cmd);
323
324     pci_config_maskw(bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_SERR);
325 }
326
327 static void storage_ide_init(struct pci_device *pci, void *arg)
328 {
329     u16 bdf = pci->bdf;
330     /* IDE: we map it as in ISA mode */
331     pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
332     pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
333     pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
334     pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
335 }
336
337 /* PIIX3/PIIX4 IDE */
338 static void piix_ide_init(struct pci_device *pci, void *arg)
339 {
340     u16 bdf = pci->bdf;
341     pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
342     pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
343     pci_bios_allocate_regions(pci, NULL);
344 }
345
346 static void pic_ibm_init(struct pci_device *pci, void *arg)
347 {
348     /* PIC, IBM, MPIC & MPIC2 */
349     pci_set_io_region_addr(pci->bdf, 0, 0x80800000 + 0x00040000);
350 }
351
352 static void apple_macio_init(struct pci_device *pci, void *arg)
353 {
354     /* macio bridge */
355     pci_set_io_region_addr(pci->bdf, 0, 0x80800000);
356 }
357
358 static const struct pci_device_id pci_class_tbl[] = {
359     /* STORAGE IDE */
360     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
361                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
362     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
363                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
364     PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
365                      storage_ide_init),
366
367     /* PIC, IBM, MIPC & MPIC2 */
368     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
369                      pic_ibm_init),
370     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
371                      pic_ibm_init),
372
373     /* 0xff00 */
374     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_init),
375     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_init),
376
377     /* PCI bridge */
378     PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_BRIDGE_PCI,
379                      pci_bios_init_device_bridge),
380
381     /* default */
382     PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID, pci_bios_allocate_regions),
383
384     PCI_DEVICE_END,
385 };
386
387 /* PIIX4 Power Management device (for ACPI) */
388 static void piix4_pm_init(struct pci_device *pci, void *arg)
389 {
390     u16 bdf = pci->bdf;
391     // acpi sci is hardwired to 9
392     pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
393
394     pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
395     pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
396     pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
397     pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
398 }
399
400 static const struct pci_device_id pci_device_tbl[] = {
401     /* PIIX4 Power Management device (for ACPI) */
402     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
403                piix4_pm_init),
404
405     PCI_DEVICE_END,
406 };
407
408 static void pci_bios_init_device(struct pci_device *pci)
409 {
410     u16 bdf = pci->bdf;
411     int pin, pic_irq;
412
413     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
414             , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf)
415             , pci->vendor, pci->device);
416     pci_init_device(pci_class_tbl, pci, NULL);
417
418     /* enable memory mappings */
419     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
420
421     /* map the interrupt */
422     pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
423     if (pin != 0) {
424         pin = pci_slot_get_pirq(bdf, pin - 1);
425         pic_irq = pci_irqs[pin];
426         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
427     }
428
429     pci_init_device(pci_device_tbl, pci, NULL);
430 }
431
432 static void pci_bios_init_device_in_bus(int bus)
433 {
434     struct pci_device *pci;
435     foreachpci(pci) {
436         u8 pci_bus = pci_bdf_to_bus(pci->bdf);
437         if (pci_bus < bus)
438             continue;
439         if (pci_bus > bus)
440             break;
441         pci_bios_init_device(pci);
442     }
443 }
444
445 static void
446 pci_bios_init_bus_rec(int bus, u8 *pci_bus)
447 {
448     int bdf;
449     u16 class;
450
451     dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
452
453     /* prevent accidental access to unintended devices */
454     foreachbdf(bdf, bus) {
455         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
456         if (class == PCI_CLASS_BRIDGE_PCI) {
457             pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255);
458             pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0);
459         }
460     }
461
462     foreachbdf(bdf, bus) {
463         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
464         if (class != PCI_CLASS_BRIDGE_PCI) {
465             continue;
466         }
467         dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
468
469         u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
470         if (pribus != bus) {
471             dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
472             pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
473         } else {
474             dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
475         }
476
477         u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
478         (*pci_bus)++;
479         if (*pci_bus != secbus) {
480             dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
481                     secbus, *pci_bus);
482             secbus = *pci_bus;
483             pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
484         } else {
485             dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
486         }
487
488         /* set to max for access to all subordinate buses.
489            later set it to accurate value */
490         u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS);
491         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255);
492
493         pci_bios_init_bus_rec(secbus, pci_bus);
494
495         if (subbus != *pci_bus) {
496             dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
497                     subbus, *pci_bus);
498             subbus = *pci_bus;
499         } else {
500             dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
501         }
502         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
503     }
504 }
505
506 static void
507 pci_bios_init_bus(void)
508 {
509     u8 pci_bus = 0;
510     pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
511     busses_count = pci_bus + 1;
512 }
513
514 static void pci_bios_bus_get_bar(struct pci_bus *bus, int bdf, int bar,
515                                  u32 *val, u32 *size)
516 {
517     u32 ofs = pci_bar(bdf, bar);
518     u32 old = pci_config_readl(bdf, ofs);
519     u32 mask;
520
521     if (bar == PCI_ROM_SLOT) {
522         mask = PCI_ROM_ADDRESS_MASK;
523         pci_config_writel(bdf, ofs, mask);
524     } else {
525         if (old & PCI_BASE_ADDRESS_SPACE_IO)
526             mask = PCI_BASE_ADDRESS_IO_MASK;
527         else
528             mask = PCI_BASE_ADDRESS_MEM_MASK;
529         pci_config_writel(bdf, ofs, ~0);
530     }
531     *val = pci_config_readl(bdf, ofs);
532     pci_config_writel(bdf, ofs, old);
533     *size = (~(*val & mask)) + 1;
534 }
535
536 static void pci_bios_bus_reserve(struct pci_bus *bus, int type, u32 size)
537 {
538     u32 index;
539
540     index = pci_size_to_index(size, type);
541     size = pci_index_to_size(index, type);
542     bus->r[type].count[index]++;
543     bus->r[type].sum += size;
544     if (bus->r[type].max < size)
545         bus->r[type].max = size;
546 }
547
548 static u32 pci_bios_bus_get_addr(struct pci_bus *bus, int type, u32 size)
549 {
550     u32 index, addr;
551
552     index = pci_size_to_index(size, type);
553     addr = bus->r[type].bases[index];
554     bus->r[type].bases[index] += pci_index_to_size(index, type);
555     return addr;
556 }
557
558 static void pci_bios_check_device(struct pci_bus *bus, struct pci_device *dev)
559 {
560     u16 bdf = dev->bdf;
561     u32 limit;
562     int i,type;
563
564     if (dev->class == PCI_CLASS_BRIDGE_PCI) {
565         if (dev->secondary_bus >= busses_count) {
566             /* should never trigger */
567             dprintf(1, "PCI: bus count too small (%d), skipping bus #%d\n",
568                     busses_count, dev->secondary_bus);
569             return;
570         }
571         struct pci_bus *s = busses + dev->secondary_bus;
572         pci_bios_check_device_in_bus(dev->secondary_bus);
573         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
574             limit = (type == PCI_REGION_TYPE_IO) ?
575                 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
576             s->r[type].size = s->r[type].sum;
577             if (s->r[type].size < limit)
578                 s->r[type].size = limit;
579             s->r[type].size = pci_size_roundup(s->r[type].size);
580             pci_bios_bus_reserve(bus, type, s->r[type].size);
581         }
582         dprintf(1, "PCI: secondary bus %d sizes: io %x, mem %x, prefmem %x\n",
583                 dev->secondary_bus,
584                 s->r[PCI_REGION_TYPE_IO].size,
585                 s->r[PCI_REGION_TYPE_MEM].size,
586                 s->r[PCI_REGION_TYPE_PREFMEM].size);
587         return;
588     }
589
590     for (i = 0; i < PCI_NUM_REGIONS; i++) {
591         u32 val, size;
592         pci_bios_bus_get_bar(bus, bdf, i, &val, &size);
593         if (val == 0) {
594             continue;
595         }
596         pci_bios_bus_reserve(bus, pci_addr_to_type(val), size);
597         dev->bars[i].addr = val;
598         dev->bars[i].size = size;
599         dev->bars[i].is64 = (!(val & PCI_BASE_ADDRESS_SPACE_IO) &&
600             (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64);
601
602         if (dev->bars[i].is64) {
603             i++;
604         }
605     }
606 }
607
608 static void pci_bios_map_device(struct pci_bus *bus, struct pci_device *dev)
609 {
610     int type, i;
611
612     if (dev->class == PCI_CLASS_BRIDGE_PCI) {
613         if (dev->secondary_bus >= busses_count) {
614             return;
615         }
616         struct pci_bus *s = busses + dev->secondary_bus;
617
618         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
619             s->r[type].base = pci_bios_bus_get_addr(bus, type, s->r[type].size);
620         }
621         dprintf(1, "PCI: init bases bus %d (secondary)\n", dev->secondary_bus);
622         pci_bios_init_bus_bases(s);
623         /* TODO: commit assignments */
624
625         pci_bios_map_device_in_bus(dev->secondary_bus);
626         return;
627     }
628
629     for (i = 0; i < PCI_NUM_REGIONS; i++) {
630         u32 addr;
631         if (dev->bars[i].addr == 0) {
632             continue;
633         }
634
635         addr = pci_bios_bus_get_addr(bus, pci_addr_to_type(dev->bars[i].addr),
636                                      dev->bars[i].size);
637         dprintf(1, "  bar %d, addr %x, size %x [%s]\n",
638                 i, addr, dev->bars[i].size,
639                 dev->bars[i].addr & PCI_BASE_ADDRESS_SPACE_IO ? "io" : "mem");
640         /* TODO: commit assignments */
641
642         if (dev->bars[i].is64) {
643             i++;
644         }
645     }
646 }
647
648 static void pci_bios_check_device_in_bus(int bus)
649 {
650     struct pci_device *pci;
651
652     dprintf(1, "PCI: check devices bus %d\n", bus);
653     foreachpci(pci) {
654         if (pci_bdf_to_bus(pci->bdf) != bus)
655             continue;
656         pci_bios_check_device(&busses[bus], pci);
657     }
658 }
659
660 static void pci_bios_map_device_in_bus(int bus)
661 {
662     struct pci_device *pci;
663
664     foreachpci(pci) {
665         if (pci_bdf_to_bus(pci->bdf) != bus)
666             continue;
667         dprintf(1, "PCI: map device bus %d, bfd 0x%x\n", bus, pci->bdf);
668         pci_bios_map_device(&busses[bus], pci);
669     }
670 }
671
672 static void pci_bios_init_bus_bases(struct pci_bus *bus)
673 {
674     u32 base, newbase, size;
675     int type, i;
676
677     for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
678         dprintf(1, "  type %s max %x sum %x base %x\n", region_type_name[type],
679                 bus->r[type].max, bus->r[type].sum, bus->r[type].base);
680         base = bus->r[type].base;
681         for (i = ARRAY_SIZE(bus->r[type].count)-1; i >= 0; i--) {
682             size = pci_index_to_size(i, type);
683             if (!bus->r[type].count[i])
684                 continue;
685             newbase = base + size * bus->r[type].count[i];
686             dprintf(1, "    size %8x: %d bar(s), %8x -> %8x\n",
687                     size, bus->r[type].count[i], base, newbase - 1);
688             bus->r[type].bases[i] = base;
689             base = newbase;
690         }
691     }
692 }
693
694 #define ROOT_BASE(top, sum, align) ALIGN_DOWN((top)-(sum),(align))
695
696 static int pci_bios_init_root_regions(u32 start, u32 end)
697 {
698     struct pci_bus *bus = &busses[0];
699
700     bus->r[PCI_REGION_TYPE_IO].base = 0xc000;
701
702     if (bus->r[PCI_REGION_TYPE_MEM].sum < bus->r[PCI_REGION_TYPE_PREFMEM].sum) {
703         bus->r[PCI_REGION_TYPE_MEM].base =
704             ROOT_BASE(end,
705                       bus->r[PCI_REGION_TYPE_MEM].sum,
706                       bus->r[PCI_REGION_TYPE_MEM].max);
707         bus->r[PCI_REGION_TYPE_PREFMEM].base =
708             ROOT_BASE(bus->r[PCI_REGION_TYPE_MEM].base,
709                       bus->r[PCI_REGION_TYPE_PREFMEM].sum,
710                       bus->r[PCI_REGION_TYPE_PREFMEM].max);
711         if (bus->r[PCI_REGION_TYPE_PREFMEM].base >= start) {
712             return 0;
713         }
714     } else {
715         bus->r[PCI_REGION_TYPE_PREFMEM].base =
716             ROOT_BASE(end,
717                       bus->r[PCI_REGION_TYPE_PREFMEM].sum,
718                       bus->r[PCI_REGION_TYPE_PREFMEM].max);
719         bus->r[PCI_REGION_TYPE_MEM].base =
720             ROOT_BASE(bus->r[PCI_REGION_TYPE_PREFMEM].base,
721                       bus->r[PCI_REGION_TYPE_MEM].sum,
722                       bus->r[PCI_REGION_TYPE_MEM].max);
723         if (bus->r[PCI_REGION_TYPE_MEM].base >= start) {
724             return 0;
725         }
726     }
727     return -1;
728 }
729
730 void
731 pci_setup(void)
732 {
733     if (CONFIG_COREBOOT || usingXen()) {
734         // PCI setup already done by coreboot or Xen - just do probe.
735         pci_probe();
736         return;
737     }
738
739     dprintf(3, "pci setup\n");
740
741     u32 start = BUILD_PCIMEM_START;
742     u32 end   = BUILD_IOAPIC_ADDR;
743
744     pci_region_init(&pci_bios_io_region, 0xc000, 64 * 1024 - 1);
745     pci_region_init(&pci_bios_mem_region,
746                     BUILD_PCIMEM_START, BUILD_PCIMEM_END - 1);
747     pci_region_init(&pci_bios_prefmem_region,
748                     BUILD_PCIPREFMEM_START, BUILD_PCIPREFMEM_END - 1);
749
750     dprintf(1, "=== PCI bus & bridge init ===\n");
751     pci_bios_init_bus();
752
753     dprintf(1, "=== PCI device probing ===\n");
754     pci_probe();
755
756     dprintf(1, "=== PCI new allocation pass #1 ===\n");
757     busses = malloc_tmp(sizeof(*busses) * busses_count);
758     memset(busses, 0, sizeof(*busses) * busses_count);
759     pci_bios_check_device_in_bus(0 /* host bus */);
760     if (pci_bios_init_root_regions(start, end) != 0) {
761         dprintf(1, "PCI: out of address space\n");
762         /* Hmm, what do now? */
763     }
764
765     dprintf(1, "=== PCI new allocation pass #2 ===\n");
766     dprintf(1, "PCI: init bases bus 0 (primary)\n");
767     pci_bios_init_bus_bases(&busses[0]);
768     pci_bios_map_device_in_bus(0 /* host bus */);
769
770     dprintf(1, "=== PCI old allocation pass ===\n");
771     struct pci_device *pci;
772     foreachpci(pci) {
773         pci_init_device(pci_isa_bridge_tbl, pci, NULL);
774     }
775     pci_bios_init_device_in_bus(0 /* host bus */);
776
777     free(busses);
778     busses_count = 0;
779 }