0bd9b72467f970ece9abf4d859618f61cdfc1f26
[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 "dev-i440fx.h"
14 #include "xen.h" // usingXen
15
16 #define PCI_ROM_SLOT 6
17 #define PCI_NUM_REGIONS 7
18
19 static void pci_bios_init_device_in_bus(int bus);
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 /* host irqs corresponding to PCI irqs A-D */
26 const u8 pci_irqs[4] = {
27     10, 10, 11, 11
28 };
29
30 static u32 pci_bar(u16 bdf, int region_num)
31 {
32     if (region_num != PCI_ROM_SLOT) {
33         return PCI_BASE_ADDRESS_0 + region_num * 4;
34     }
35
36 #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
37     u8 type = pci_config_readb(bdf, PCI_HEADER_TYPE);
38     type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
39     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
40 }
41
42 static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
43 {
44     u32 ofs;
45
46     ofs = pci_bar(bdf, region_num);
47
48     pci_config_writel(bdf, ofs, addr);
49     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
50 }
51
52 /*
53  * return value
54  *      0:     32bit BAR
55  *      non 0: 64bit BAR
56  */
57 static int pci_bios_allocate_region(u16 bdf, int region_num)
58 {
59     struct pci_region *r;
60     u32 ofs = pci_bar(bdf, region_num);
61
62     u32 old = pci_config_readl(bdf, ofs);
63     u32 mask;
64     if (region_num == PCI_ROM_SLOT) {
65         mask = PCI_ROM_ADDRESS_MASK;
66         pci_config_writel(bdf, ofs, mask);
67     } else {
68         if (old & PCI_BASE_ADDRESS_SPACE_IO)
69             mask = PCI_BASE_ADDRESS_IO_MASK;
70         else
71             mask = PCI_BASE_ADDRESS_MEM_MASK;
72         pci_config_writel(bdf, ofs, ~0);
73     }
74     u32 val = pci_config_readl(bdf, ofs);
75     pci_config_writel(bdf, ofs, old);
76
77     u32 size = (~(val & mask)) + 1;
78     if (val != 0) {
79         const char *type;
80         const char *msg;
81         if (val & PCI_BASE_ADDRESS_SPACE_IO) {
82             r = &pci_bios_io_region;
83             type = "io";
84             msg = "";
85         } else if ((val & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
86                    /* keep behaviour on bus = 0 */
87                    pci_bdf_to_bus(bdf) != 0 &&
88                    /* If pci_bios_prefmem_addr == 0, keep old behaviour */
89                    pci_region_addr(&pci_bios_prefmem_region) != 0) {
90             r = &pci_bios_prefmem_region;
91             type = "prefmem";
92             msg = "decrease BUILD_PCIMEM_SIZE and recompile. size %x";
93         } else {
94             r = &pci_bios_mem_region;
95             type = "mem";
96             msg = "increase BUILD_PCIMEM_SIZE and recompile.";
97         }
98         u32 addr = pci_region_alloc(r, size);
99         if (addr > 0) {
100             pci_set_io_region_addr(bdf, region_num, addr);
101         } else {
102             size = 0;
103             dprintf(1,
104                     "%s region of (bdf 0x%x bar %d) can't be mapped. "
105                     "%s size %x\n",
106                     type, bdf, region_num, msg, pci_region_size(r));
107         }
108     }
109
110     int is_64bit = !(val & PCI_BASE_ADDRESS_SPACE_IO) &&
111         (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64;
112     if (is_64bit && size > 0) {
113         pci_config_writel(bdf, ofs + 4, 0);
114     }
115     return is_64bit;
116 }
117
118 void pci_bios_allocate_regions(u16 bdf, void *arg)
119 {
120     int i;
121     for (i = 0; i < PCI_NUM_REGIONS; i++) {
122         int is_64bit = pci_bios_allocate_region(bdf, i);
123         if (is_64bit){
124             i++;
125         }
126     }
127 }
128
129 /* return the global irq number corresponding to a given device irq
130    pin. We could also use the bus number to have a more precise
131    mapping. */
132 static int pci_slot_get_pirq(u16 bdf, int irq_num)
133 {
134     int slot_addend = pci_bdf_to_dev(bdf) - 1;
135     return (irq_num + slot_addend) & 3;
136 }
137
138 static const struct pci_device_id pci_isa_bridge_tbl[] = {
139     /* PIIX3/PIIX4 PCI to ISA bridge */
140     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
141                piix_isa_bridge_init),
142     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
143                piix_isa_bridge_init),
144
145     PCI_DEVICE_END
146 };
147
148 #define PCI_IO_ALIGN            4096
149 #define PCI_IO_SHIFT            8
150 #define PCI_MEMORY_ALIGN        (1UL << 20)
151 #define PCI_MEMORY_SHIFT        16
152 #define PCI_PREF_MEMORY_ALIGN   (1UL << 20)
153 #define PCI_PREF_MEMORY_SHIFT   16
154
155 static void pci_bios_init_device_bridge(u16 bdf, void *arg)
156 {
157     pci_bios_allocate_region(bdf, 0);
158     pci_bios_allocate_region(bdf, 1);
159     pci_bios_allocate_region(bdf, PCI_ROM_SLOT);
160
161     u32 io_old = pci_region_addr(&pci_bios_io_region);
162     u32 mem_old = pci_region_addr(&pci_bios_mem_region);
163     u32 prefmem_old = pci_region_addr(&pci_bios_prefmem_region);
164
165     /* IO BASE is assumed to be 16 bit */
166     if (pci_region_align(&pci_bios_io_region, PCI_IO_ALIGN) == 0) {
167         pci_region_disable(&pci_bios_io_region);
168     }
169     if (pci_region_align(&pci_bios_mem_region, PCI_MEMORY_ALIGN) == 0) {
170         pci_region_disable(&pci_bios_mem_region);
171     }
172     if (pci_region_align(&pci_bios_prefmem_region,
173                          PCI_PREF_MEMORY_ALIGN) == 0) {
174         pci_region_disable(&pci_bios_prefmem_region);
175     }
176
177     u32 io_base = pci_region_addr(&pci_bios_io_region);
178     u32 mem_base = pci_region_addr(&pci_bios_mem_region);
179     u32 prefmem_base = pci_region_addr(&pci_bios_prefmem_region);
180
181     u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
182     if (secbus > 0) {
183         pci_bios_init_device_in_bus(secbus);
184     }
185
186     u32 io_end = pci_region_align(&pci_bios_io_region, PCI_IO_ALIGN);
187     if (io_end == 0) {
188         pci_region_revert(&pci_bios_io_region, io_old);
189         io_base = 0xffff;
190         io_end = 1;
191     }
192     pci_config_writeb(bdf, PCI_IO_BASE, io_base >> PCI_IO_SHIFT);
193     pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
194     pci_config_writeb(bdf, PCI_IO_LIMIT, (io_end - 1) >> PCI_IO_SHIFT);
195     pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
196
197     u32 mem_end = pci_region_align(&pci_bios_mem_region, PCI_MEMORY_ALIGN);
198     if (mem_end == 0) {
199         pci_region_revert(&pci_bios_mem_region, mem_old);
200         mem_base = 0xffffffff;
201         mem_end = 1;
202     }
203     pci_config_writew(bdf, PCI_MEMORY_BASE, mem_base >> PCI_MEMORY_SHIFT);
204     pci_config_writew(bdf, PCI_MEMORY_LIMIT, (mem_end -1) >> PCI_MEMORY_SHIFT);
205
206     u32 prefmem_end = pci_region_align(&pci_bios_prefmem_region,
207                                        PCI_PREF_MEMORY_ALIGN);
208     if (prefmem_end == 0) {
209         pci_region_revert(&pci_bios_prefmem_region, prefmem_old);
210         prefmem_base = 0xffffffff;
211         prefmem_end = 1;
212     }
213     pci_config_writew(bdf, PCI_PREF_MEMORY_BASE,
214                       prefmem_base >> PCI_PREF_MEMORY_SHIFT);
215     pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT,
216                       (prefmem_end - 1) >> PCI_PREF_MEMORY_SHIFT);
217     pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
218     pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
219
220     dprintf(1, "PCI: br io   = [0x%x, 0x%x)\n", io_base, io_end);
221     dprintf(1, "PCI: br mem  = [0x%x, 0x%x)\n", mem_base, mem_end);
222     dprintf(1, "PCI: br pref = [0x%x, 0x%x)\n", prefmem_base, prefmem_end);
223
224     u16 cmd = pci_config_readw(bdf, PCI_COMMAND);
225     cmd &= ~PCI_COMMAND_IO;
226     if (io_end > io_base) {
227         cmd |= PCI_COMMAND_IO;
228     }
229     cmd &= ~PCI_COMMAND_MEMORY;
230     if (mem_end > mem_base || prefmem_end > prefmem_base) {
231         cmd |= PCI_COMMAND_MEMORY;
232     }
233     cmd |= PCI_COMMAND_MASTER;
234     pci_config_writew(bdf, PCI_COMMAND, cmd);
235
236     pci_config_maskw(bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_SERR);
237 }
238
239 static void storage_ide_init(u16 bdf, void *arg)
240 {
241     /* IDE: we map it as in ISA mode */
242     pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
243     pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
244     pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
245     pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
246 }
247
248 static void pic_ibm_init(u16 bdf, void *arg)
249 {
250     /* PIC, IBM, MPIC & MPIC2 */
251     pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
252 }
253
254 static void apple_macio_init(u16 bdf, void *arg)
255 {
256     /* macio bridge */
257     pci_set_io_region_addr(bdf, 0, 0x80800000);
258 }
259
260 static const struct pci_device_id pci_class_tbl[] = {
261     /* STORAGE IDE */
262     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
263                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
264     PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
265                      PCI_CLASS_STORAGE_IDE, piix_ide_init),
266     PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
267                      storage_ide_init),
268
269     /* PIC, IBM, MIPC & MPIC2 */
270     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
271                      pic_ibm_init),
272     PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
273                      pic_ibm_init),
274
275     /* 0xff00 */
276     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_init),
277     PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_init),
278
279     /* PCI bridge */
280     PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_BRIDGE_PCI,
281                      pci_bios_init_device_bridge),
282
283     /* default */
284     PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID, pci_bios_allocate_regions),
285
286     PCI_DEVICE_END,
287 };
288
289 static const struct pci_device_id pci_device_tbl[] = {
290     /* PIIX4 Power Management device (for ACPI) */
291     PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
292                piix4_pm_init),
293
294     PCI_DEVICE_END,
295 };
296
297 static void pci_bios_init_device(u16 bdf)
298 {
299     int pin, pic_irq, vendor_id, device_id;
300
301     vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
302     device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
303     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
304             , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
305     pci_init_device(pci_class_tbl, bdf, NULL);
306
307     /* enable memory mappings */
308     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
309
310     /* map the interrupt */
311     pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
312     if (pin != 0) {
313         pin = pci_slot_get_pirq(bdf, pin - 1);
314         pic_irq = pci_irqs[pin];
315         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
316     }
317
318     pci_init_device(pci_device_tbl, bdf, NULL);
319 }
320
321 static void pci_bios_init_device_in_bus(int bus)
322 {
323     int bdf, max;
324     foreachpci_in_bus(bdf, max, bus) {
325         pci_bios_init_device(bdf);
326     }
327 }
328
329 static void
330 pci_bios_init_bus_rec(int bus, u8 *pci_bus)
331 {
332     int bdf, max;
333     u16 class;
334
335     dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
336
337     /* prevent accidental access to unintended devices */
338     foreachpci_in_bus(bdf, max, bus) {
339         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
340         if (class == PCI_CLASS_BRIDGE_PCI) {
341             pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255);
342             pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0);
343         }
344     }
345
346     foreachpci_in_bus(bdf, max, bus) {
347         class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
348         if (class != PCI_CLASS_BRIDGE_PCI) {
349             continue;
350         }
351         dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
352
353         u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
354         if (pribus != bus) {
355             dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
356             pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
357         } else {
358             dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
359         }
360
361         u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
362         (*pci_bus)++;
363         if (*pci_bus != secbus) {
364             dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
365                     secbus, *pci_bus);
366             secbus = *pci_bus;
367             pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
368         } else {
369             dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
370         }
371
372         /* set to max for access to all subordinate buses.
373            later set it to accurate value */
374         u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS);
375         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255);
376
377         pci_bios_init_bus_rec(secbus, pci_bus);
378
379         if (subbus != *pci_bus) {
380             dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
381                     subbus, *pci_bus);
382             subbus = *pci_bus;
383         } else {
384             dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
385         }
386         pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
387     }
388 }
389
390 static void
391 pci_bios_init_bus(void)
392 {
393     u8 pci_bus = 0;
394     pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
395 }
396
397 void
398 pci_setup(void)
399 {
400     if (CONFIG_COREBOOT || usingXen())
401         // Already done by coreboot or Xen.
402         return;
403
404     dprintf(3, "pci setup\n");
405
406     pci_region_init(&pci_bios_io_region, 0xc000, 64 * 1024 - 1);
407     pci_region_init(&pci_bios_mem_region,
408                     BUILD_PCIMEM_START, BUILD_PCIMEM_END - 1);
409     pci_region_init(&pci_bios_prefmem_region,
410                     BUILD_PCIPREFMEM_START, BUILD_PCIPREFMEM_END - 1);
411
412     pci_bios_init_bus();
413
414     int bdf, max;
415     foreachpci(bdf, max) {
416         pci_init_device(pci_isa_bridge_tbl, bdf, NULL);
417     }
418     pci_bios_init_device_in_bus(0 /* host bus */);
419 }