seabios: pciinit: factor out pci bar region allocation logic.
[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
14 #define PCI_ROM_SLOT 6
15 #define PCI_NUM_REGIONS 7
16
17 static u32 pci_bios_io_addr;
18 static u32 pci_bios_mem_addr;
19 /* host irqs corresponding to PCI irqs A-D */
20 static u8 pci_irqs[4] = {
21     10, 10, 11, 11
22 };
23
24 static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
25 {
26     u32 ofs, old_addr;
27
28     if (region_num == PCI_ROM_SLOT) {
29         ofs = PCI_ROM_ADDRESS;
30     } else {
31         ofs = PCI_BASE_ADDRESS_0 + region_num * 4;
32     }
33
34     old_addr = pci_config_readl(bdf, ofs);
35
36     pci_config_writel(bdf, ofs, addr);
37     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
38 }
39
40 static void pci_bios_allocate_region(u16 bdf, int region_num)
41 {
42     u32 *paddr;
43     int ofs;
44     if (region_num == PCI_ROM_SLOT)
45         ofs = PCI_ROM_ADDRESS;
46     else
47         ofs = PCI_BASE_ADDRESS_0 + region_num * 4;
48
49     u32 old = pci_config_readl(bdf, ofs);
50     u32 mask;
51     if (region_num == PCI_ROM_SLOT) {
52         mask = PCI_ROM_ADDRESS_MASK;
53         pci_config_writel(bdf, ofs, mask);
54     } else {
55         if (old & PCI_BASE_ADDRESS_SPACE_IO)
56             mask = PCI_BASE_ADDRESS_IO_MASK;
57         else
58             mask = PCI_BASE_ADDRESS_MEM_MASK;
59         pci_config_writel(bdf, ofs, ~0);
60     }
61     u32 val = pci_config_readl(bdf, ofs);
62     pci_config_writel(bdf, ofs, old);
63
64     if (val != 0) {
65         u32 size = (~(val & mask)) + 1;
66         if (val & PCI_BASE_ADDRESS_SPACE_IO)
67             paddr = &pci_bios_io_addr;
68         else
69             paddr = &pci_bios_mem_addr;
70         *paddr = ALIGN(*paddr, size);
71         pci_set_io_region_addr(bdf, region_num, *paddr);
72         *paddr += size;
73     }
74 }
75
76 static void pci_bios_allocate_regions(u16 bdf)
77 {
78     int i;
79     for (i = 0; i < PCI_NUM_REGIONS; i++) {
80         pci_bios_allocate_region(bdf, i);
81     }
82 }
83
84 /* return the global irq number corresponding to a given device irq
85    pin. We could also use the bus number to have a more precise
86    mapping. */
87 static int pci_slot_get_pirq(u16 bdf, int irq_num)
88 {
89     int slot_addend = pci_bdf_to_dev(bdf) - 1;
90     return (irq_num + slot_addend) & 3;
91 }
92
93 static void pci_bios_init_bridges(u16 bdf)
94 {
95     u16 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
96     u16 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
97
98     if (vendor_id == PCI_VENDOR_ID_INTEL
99         && (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
100             || device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
101         int i, irq;
102         u8 elcr[2];
103
104         /* PIIX3/PIIX4 PCI to ISA bridge */
105
106         elcr[0] = 0x00;
107         elcr[1] = 0x00;
108         for (i = 0; i < 4; i++) {
109             irq = pci_irqs[i];
110             /* set to trigger level */
111             elcr[irq >> 3] |= (1 << (irq & 7));
112             /* activate irq remapping in PIIX */
113             pci_config_writeb(bdf, 0x60 + i, irq);
114         }
115         outb(elcr[0], 0x4d0);
116         outb(elcr[1], 0x4d1);
117         dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
118                 elcr[0], elcr[1]);
119     }
120 }
121
122 static void pci_bios_init_device(u16 bdf)
123 {
124     int class;
125     int pin, pic_irq, vendor_id, device_id;
126
127     class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
128     vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
129     device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
130     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
131             , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
132     switch (class) {
133     case PCI_CLASS_STORAGE_IDE:
134         if (vendor_id == PCI_VENDOR_ID_INTEL
135             && (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
136                 || device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
137             /* PIIX3/PIIX4 IDE */
138             pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
139             pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
140             pci_bios_allocate_regions(bdf);
141         } else {
142             /* IDE: we map it as in ISA mode */
143             pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
144             pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
145             pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
146             pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
147         }
148         break;
149     case PCI_CLASS_SYSTEM_PIC:
150         /* PIC */
151         if (vendor_id == PCI_VENDOR_ID_IBM) {
152             /* IBM */
153             if (device_id == 0x0046 || device_id == 0xFFFF) {
154                 /* MPIC & MPIC2 */
155                 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
156             }
157         }
158         break;
159     case 0xff00:
160         if (vendor_id == PCI_VENDOR_ID_APPLE &&
161             (device_id == 0x0017 || device_id == 0x0022)) {
162             /* macio bridge */
163             pci_set_io_region_addr(bdf, 0, 0x80800000);
164         }
165         break;
166     default:
167         /* default memory mappings */
168         pci_bios_allocate_regions(bdf);
169         break;
170     }
171
172     /* enable memory mappings */
173     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
174
175     /* map the interrupt */
176     pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
177     if (pin != 0) {
178         pin = pci_slot_get_pirq(bdf, pin - 1);
179         pic_irq = pci_irqs[pin];
180         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
181     }
182
183     if (vendor_id == PCI_VENDOR_ID_INTEL
184         && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
185         /* PIIX4 Power Management device (for ACPI) */
186
187         // acpi sci is hardwired to 9
188         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
189
190         pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
191         pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
192         pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
193         pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
194     }
195 }
196
197 void
198 pci_setup(void)
199 {
200     if (CONFIG_COREBOOT)
201         // Already done by coreboot.
202         return;
203
204     dprintf(3, "pci setup\n");
205
206     pci_bios_io_addr = 0xc000;
207     pci_bios_mem_addr = BUILD_PCIMEM_START;
208
209     int bdf, max;
210     foreachpci(bdf, max) {
211         pci_bios_init_bridges(bdf);
212     }
213     foreachpci(bdf, max) {
214         pci_bios_init_device(bdf);
215     }
216 }