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 old_addr = pci_config_readl(bdf, ofs);
48 pci_config_writel(bdf, ofs, addr);
49 dprintf(1, "region %d: 0x%08x\n", region_num, addr);
57 static int pci_bios_allocate_region(u16 bdf, int region_num)
60 u32 ofs = pci_bar(bdf, region_num);
62 u32 old = pci_config_readl(bdf, ofs);
64 if (region_num == PCI_ROM_SLOT) {
65 mask = PCI_ROM_ADDRESS_MASK;
66 pci_config_writel(bdf, ofs, mask);
68 if (old & PCI_BASE_ADDRESS_SPACE_IO)
69 mask = PCI_BASE_ADDRESS_IO_MASK;
71 mask = PCI_BASE_ADDRESS_MEM_MASK;
72 pci_config_writel(bdf, ofs, ~0);
74 u32 val = pci_config_readl(bdf, ofs);
75 pci_config_writel(bdf, ofs, old);
77 u32 size = (~(val & mask)) + 1;
79 if (val & PCI_BASE_ADDRESS_SPACE_IO) {
80 paddr = &pci_bios_io_addr;
81 if (ALIGN(*paddr, size) + size >= 64 * 1024) {
83 "io region of (bdf 0x%x bar %d) can't be mapped.\n",
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) {
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);
101 paddr = &pci_bios_mem_addr;
102 if (ALIGN(*paddr, size) + size >= BUILD_PCIMEM_END) {
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);
111 *paddr = ALIGN(*paddr, size);
112 pci_set_io_region_addr(bdf, region_num, *paddr);
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);
125 void pci_bios_allocate_regions(u16 bdf, void *arg)
128 for (i = 0; i < PCI_NUM_REGIONS; i++) {
129 int is_64bit = pci_bios_allocate_region(bdf, i);
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
139 static int pci_slot_get_pirq(u16 bdf, int irq_num)
141 int slot_addend = pci_bdf_to_dev(bdf) - 1;
142 return (irq_num + slot_addend) & 3;
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),
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
162 static void pci_bios_init_device_bridge(u16 bdf, void *arg)
164 pci_bios_allocate_region(bdf, 0);
165 pci_bios_allocate_region(bdf, 1);
166 pci_bios_allocate_region(bdf, PCI_ROM_SLOT);
168 u32 io_old = pci_bios_io_addr;
169 u32 mem_old = pci_bios_mem_addr;
170 u32 prefmem_old = pci_bios_prefmem_addr;
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);
178 u32 io_base = pci_bios_io_addr;
179 u32 mem_base = pci_bios_mem_addr;
180 u32 prefmem_base = pci_bios_prefmem_addr;
182 u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
184 pci_bios_init_device_in_bus(secbus);
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);
192 u32 io_end = pci_bios_io_addr;
193 if (io_end == io_base) {
194 pci_bios_io_addr = io_old;
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);
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;
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);
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;
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);
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);
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;
234 cmd &= ~PCI_COMMAND_MEMORY;
235 if (mem_end > mem_base || prefmem_end > prefmem_base) {
236 cmd |= PCI_COMMAND_MEMORY;
238 cmd |= PCI_COMMAND_MASTER;
239 pci_config_writew(bdf, PCI_COMMAND, cmd);
241 pci_config_maskw(bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_SERR);
244 static void storage_ide_init(u16 bdf, void *arg)
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);
253 static void pic_ibm_init(u16 bdf, void *arg)
255 /* PIC, IBM, MPIC & MPIC2 */
256 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
259 static void apple_macio_init(u16 bdf, void *arg)
262 pci_set_io_region_addr(bdf, 0, 0x80800000);
265 static const struct pci_device_id pci_class_tbl[] = {
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,
274 /* PIC, IBM, MIPC & MPIC2 */
275 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0x0046, PCI_CLASS_SYSTEM_PIC,
277 PCI_DEVICE_CLASS(PCI_VENDOR_ID_IBM, 0xFFFF, PCI_CLASS_SYSTEM_PIC,
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),
285 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_BRIDGE_PCI,
286 pci_bios_init_device_bridge),
289 PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID, pci_bios_allocate_regions),
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,
302 static void pci_bios_init_device(u16 bdf)
304 int pin, pic_irq, vendor_id, device_id;
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);
312 /* enable memory mappings */
313 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
315 /* map the interrupt */
316 pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
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);
323 pci_init_device(pci_device_tbl, bdf, NULL);
326 static void pci_bios_init_device_in_bus(int bus)
329 foreachpci_in_bus(bdf, max, bus) {
330 pci_bios_init_device(bdf);
335 pci_bios_init_bus_rec(int bus, u8 *pci_bus)
340 dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
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);
351 foreachpci_in_bus(bdf, max, bus) {
352 class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
353 if (class != PCI_CLASS_BRIDGE_PCI) {
356 dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
358 u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
360 dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
361 pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
363 dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
366 u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
368 if (*pci_bus != secbus) {
369 dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
372 pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
374 dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
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);
382 pci_bios_init_bus_rec(secbus, pci_bus);
384 if (subbus != *pci_bus) {
385 dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
389 dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
391 pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
396 pci_bios_init_bus(void)
399 pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
406 // Already done by coreboot.
409 dprintf(3, "pci setup\n");
411 pci_bios_io_addr = 0xc000;
412 pci_bios_mem_addr = BUILD_PCIMEM_START;
413 pci_bios_prefmem_addr = BUILD_PCIPREFMEM_START;
418 foreachpci(bdf, max) {
419 pci_init_device(pci_isa_bridge_tbl, bdf, NULL);
421 pci_bios_init_device_in_bus(0 /* host bus */);