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