1 // Initialize PCI devices (on emulators)
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
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"
15 #define PCI_ROM_SLOT 6
16 #define PCI_NUM_REGIONS 7
18 static void pci_bios_init_device_in_bus(int bus);
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] = {
28 static u32 pci_bar(u16 bdf, int region_num)
30 if (region_num != PCI_ROM_SLOT) {
31 return PCI_BASE_ADDRESS_0 + region_num * 4;
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;
40 static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
44 ofs = pci_bar(bdf, region_num);
46 pci_config_writel(bdf, ofs, addr);
47 dprintf(1, "region %d: 0x%08x\n", region_num, addr);
55 static int pci_bios_allocate_region(u16 bdf, int region_num)
58 u32 ofs = pci_bar(bdf, region_num);
60 u32 old = pci_config_readl(bdf, ofs);
62 if (region_num == PCI_ROM_SLOT) {
63 mask = PCI_ROM_ADDRESS_MASK;
64 pci_config_writel(bdf, ofs, mask);
66 if (old & PCI_BASE_ADDRESS_SPACE_IO)
67 mask = PCI_BASE_ADDRESS_IO_MASK;
69 mask = PCI_BASE_ADDRESS_MEM_MASK;
70 pci_config_writel(bdf, ofs, ~0);
72 u32 val = pci_config_readl(bdf, ofs);
73 pci_config_writel(bdf, ofs, old);
75 u32 size = (~(val & mask)) + 1;
77 if (val & PCI_BASE_ADDRESS_SPACE_IO) {
78 paddr = &pci_bios_io_addr;
79 if (ALIGN(*paddr, size) + size >= 64 * 1024) {
81 "io region of (bdf 0x%x bar %d) can't be mapped.\n",
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_bios_prefmem_addr != 0) {
90 paddr = &pci_bios_prefmem_addr;
91 if (ALIGN(*paddr, size) + size >= BUILD_PCIPREFMEM_END) {
93 "prefmem region of (bdf 0x%x bar %d) can't be mapped. "
94 "decrease BUILD_PCIMEM_SIZE and recompile. size %x\n",
95 bdf, region_num, BUILD_PCIPREFMEM_SIZE);
99 paddr = &pci_bios_mem_addr;
100 if (ALIGN(*paddr, size) + size >= BUILD_PCIMEM_END) {
102 "mem region of (bdf 0x%x bar %d) can't be mapped. "
103 "increase BUILD_PCIMEM_SIZE and recompile. size %x\n",
104 bdf, region_num, BUILD_PCIMEM_SIZE);
109 *paddr = ALIGN(*paddr, size);
110 pci_set_io_region_addr(bdf, region_num, *paddr);
115 int is_64bit = !(val & PCI_BASE_ADDRESS_SPACE_IO) &&
116 (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64;
117 if (is_64bit && size > 0) {
118 pci_config_writel(bdf, ofs + 4, 0);
123 void pci_bios_allocate_regions(u16 bdf, void *arg)
126 for (i = 0; i < PCI_NUM_REGIONS; i++) {
127 int is_64bit = pci_bios_allocate_region(bdf, i);
134 /* return the global irq number corresponding to a given device irq
135 pin. We could also use the bus number to have a more precise
137 static int pci_slot_get_pirq(u16 bdf, int irq_num)
139 int slot_addend = pci_bdf_to_dev(bdf) - 1;
140 return (irq_num + slot_addend) & 3;
143 static const struct pci_device_id pci_isa_bridge_tbl[] = {
144 /* PIIX3/PIIX4 PCI to ISA bridge */
145 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0,
146 piix_isa_bridge_init),
147 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0,
148 piix_isa_bridge_init),
153 #define PCI_IO_ALIGN 4096
154 #define PCI_IO_SHIFT 8
155 #define PCI_MEMORY_ALIGN (1UL << 20)
156 #define PCI_MEMORY_SHIFT 16
157 #define PCI_PREF_MEMORY_ALIGN (1UL << 20)
158 #define PCI_PREF_MEMORY_SHIFT 16
160 static void pci_bios_init_device_bridge(u16 bdf, void *arg)
162 pci_bios_allocate_region(bdf, 0);
163 pci_bios_allocate_region(bdf, 1);
164 pci_bios_allocate_region(bdf, PCI_ROM_SLOT);
166 u32 io_old = pci_bios_io_addr;
167 u32 mem_old = pci_bios_mem_addr;
168 u32 prefmem_old = pci_bios_prefmem_addr;
170 /* IO BASE is assumed to be 16 bit */
171 pci_bios_io_addr = ALIGN(pci_bios_io_addr, PCI_IO_ALIGN);
172 pci_bios_mem_addr = ALIGN(pci_bios_mem_addr, PCI_MEMORY_ALIGN);
173 pci_bios_prefmem_addr =
174 ALIGN(pci_bios_prefmem_addr, PCI_PREF_MEMORY_ALIGN);
176 u32 io_base = pci_bios_io_addr;
177 u32 mem_base = pci_bios_mem_addr;
178 u32 prefmem_base = pci_bios_prefmem_addr;
180 u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
182 pci_bios_init_device_in_bus(secbus);
185 pci_bios_io_addr = ALIGN(pci_bios_io_addr, PCI_IO_ALIGN);
186 pci_bios_mem_addr = ALIGN(pci_bios_mem_addr, PCI_MEMORY_ALIGN);
187 pci_bios_prefmem_addr =
188 ALIGN(pci_bios_prefmem_addr, PCI_PREF_MEMORY_ALIGN);
190 u32 io_end = pci_bios_io_addr;
191 if (io_end == io_base) {
192 pci_bios_io_addr = io_old;
196 pci_config_writeb(bdf, PCI_IO_BASE, io_base >> PCI_IO_SHIFT);
197 pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
198 pci_config_writeb(bdf, PCI_IO_LIMIT, (io_end - 1) >> PCI_IO_SHIFT);
199 pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
201 u32 mem_end = pci_bios_mem_addr;
202 if (mem_end == mem_base) {
203 pci_bios_mem_addr = mem_old;
204 mem_base = 0xffffffff;
207 pci_config_writew(bdf, PCI_MEMORY_BASE, mem_base >> PCI_MEMORY_SHIFT);
208 pci_config_writew(bdf, PCI_MEMORY_LIMIT, (mem_end -1) >> PCI_MEMORY_SHIFT);
210 u32 prefmem_end = pci_bios_prefmem_addr;
211 if (prefmem_end == prefmem_base) {
212 pci_bios_prefmem_addr = prefmem_old;
213 prefmem_base = 0xffffffff;
216 pci_config_writew(bdf, PCI_PREF_MEMORY_BASE,
217 prefmem_base >> PCI_PREF_MEMORY_SHIFT);
218 pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT,
219 (prefmem_end - 1) >> PCI_PREF_MEMORY_SHIFT);
220 pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
221 pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
223 dprintf(1, "PCI: br io = [0x%x, 0x%x)\n", io_base, io_end);
224 dprintf(1, "PCI: br mem = [0x%x, 0x%x)\n", mem_base, mem_end);
225 dprintf(1, "PCI: br pref = [0x%x, 0x%x)\n", prefmem_base, prefmem_end);
227 u16 cmd = pci_config_readw(bdf, PCI_COMMAND);
228 cmd &= ~PCI_COMMAND_IO;
229 if (io_end > io_base) {
230 cmd |= PCI_COMMAND_IO;
232 cmd &= ~PCI_COMMAND_MEMORY;
233 if (mem_end > mem_base || prefmem_end > prefmem_base) {
234 cmd |= PCI_COMMAND_MEMORY;
236 cmd |= PCI_COMMAND_MASTER;
237 pci_config_writew(bdf, PCI_COMMAND, cmd);
239 pci_config_maskw(bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_SERR);
242 static void storage_ide_init(u16 bdf, void *arg)
244 /* IDE: we map it as in ISA mode */
245 pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
246 pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
247 pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
248 pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
251 static void pic_ibm_init(u16 bdf, void *arg)
253 /* PIC, IBM, MPIC & MPIC2 */
254 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
257 static void apple_macio_init(u16 bdf, void *arg)
260 pci_set_io_region_addr(bdf, 0, 0x80800000);
263 static const struct pci_device_id pci_class_tbl[] = {
265 PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1,
266 PCI_CLASS_STORAGE_IDE, piix_ide_init),
267 PCI_DEVICE_CLASS(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB,
268 PCI_CLASS_STORAGE_IDE, piix_ide_init),
269 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
272 /* PIC, IBM, MIPC & MPIC2 */
273 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
275 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
279 PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0017, 0xff00, apple_macio_init),
280 PCI_DEVICE_CLASS(PCI_VENDOR_ID_APPLE, 0x0022, 0xff00, apple_macio_init),
283 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_BRIDGE_PCI,
284 pci_bios_init_device_bridge),
287 PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID, pci_bios_allocate_regions),
292 static const struct pci_device_id pci_device_tbl[] = {
293 /* PIIX4 Power Management device (for ACPI) */
294 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
300 static void pci_bios_init_device(u16 bdf)
302 int pin, pic_irq, vendor_id, device_id;
304 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
305 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
306 dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
307 , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
308 pci_init_device(pci_class_tbl, bdf, NULL);
310 /* enable memory mappings */
311 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
313 /* map the interrupt */
314 pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
316 pin = pci_slot_get_pirq(bdf, pin - 1);
317 pic_irq = pci_irqs[pin];
318 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
321 pci_init_device(pci_device_tbl, bdf, NULL);
324 static void pci_bios_init_device_in_bus(int bus)
327 foreachpci_in_bus(bdf, max, bus) {
328 pci_bios_init_device(bdf);
333 pci_bios_init_bus_rec(int bus, u8 *pci_bus)
338 dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
340 /* prevent accidental access to unintended devices */
341 foreachpci_in_bus(bdf, max, bus) {
342 class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
343 if (class == PCI_CLASS_BRIDGE_PCI) {
344 pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255);
345 pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0);
349 foreachpci_in_bus(bdf, max, bus) {
350 class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
351 if (class != PCI_CLASS_BRIDGE_PCI) {
354 dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
356 u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
358 dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
359 pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
361 dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
364 u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
366 if (*pci_bus != secbus) {
367 dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
370 pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
372 dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
375 /* set to max for access to all subordinate buses.
376 later set it to accurate value */
377 u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS);
378 pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255);
380 pci_bios_init_bus_rec(secbus, pci_bus);
382 if (subbus != *pci_bus) {
383 dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
387 dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
389 pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
394 pci_bios_init_bus(void)
397 pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
404 // Already done by coreboot.
407 dprintf(3, "pci setup\n");
409 pci_bios_io_addr = 0xc000;
410 pci_bios_mem_addr = BUILD_PCIMEM_START;
411 pci_bios_prefmem_addr = BUILD_PCIPREFMEM_START;
416 foreachpci(bdf, max) {
417 pci_init_device(pci_isa_bridge_tbl, bdf, NULL);
419 pci_bios_init_device_in_bus(0 /* host bus */);