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