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