Extract 'struct bregs' out of biosvar.h; clean up header includes.
[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 GPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "pci.h" // PCIDevice
10 #include "biosvar.h" // GET_EBDA
11
12 #define PCI_ADDRESS_SPACE_MEM           0x00
13 #define PCI_ADDRESS_SPACE_IO            0x01
14 #define PCI_ADDRESS_SPACE_MEM_PREFETCH  0x08
15
16 #define PCI_ROM_SLOT 6
17 #define PCI_NUM_REGIONS 7
18
19 #define PCI_DEVICES_MAX 64
20
21 static u32 pci_bios_io_addr;
22 static u32 pci_bios_mem_addr;
23 static u32 pci_bios_bigmem_addr;
24 /* host irqs corresponding to PCI irqs A-D */
25 static u8 pci_irqs[4] = { 11, 9, 11, 9 };
26
27 static void pci_set_io_region_addr(PCIDevice d, int region_num, u32 addr)
28 {
29     u16 cmd;
30     u32 ofs, old_addr;
31
32     if ( region_num == PCI_ROM_SLOT ) {
33         ofs = 0x30;
34     }else{
35         ofs = 0x10 + region_num * 4;
36     }
37
38     old_addr = pci_config_readl(d, ofs);
39
40     pci_config_writel(d, ofs, addr);
41     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
42
43     /* enable memory mappings */
44     cmd = pci_config_readw(d, PCI_COMMAND);
45     if ( region_num == PCI_ROM_SLOT )
46         cmd |= 2;
47     else if (old_addr & PCI_ADDRESS_SPACE_IO)
48         cmd |= 1;
49     else
50         cmd |= 2;
51     pci_config_writew(d, PCI_COMMAND, cmd);
52 }
53
54 /* return the global irq number corresponding to a given device irq
55    pin. We could also use the bus number to have a more precise
56    mapping. */
57 static int pci_slot_get_pirq(PCIDevice pci_dev, int irq_num)
58 {
59     int slot_addend;
60     slot_addend = (pci_dev.devfn >> 3) - 1;
61     return (irq_num + slot_addend) & 3;
62 }
63
64 static void pci_bios_init_bridges(PCIDevice d)
65 {
66     u16 vendor_id, device_id;
67
68     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
69     device_id = pci_config_readw(d, PCI_DEVICE_ID);
70
71     if (vendor_id == 0x8086 && device_id == 0x7000) {
72         int i, irq;
73         u8 elcr[2];
74
75         /* PIIX3 bridge */
76
77         elcr[0] = 0x00;
78         elcr[1] = 0x00;
79         for(i = 0; i < 4; i++) {
80             irq = pci_irqs[i];
81             /* set to trigger level */
82             elcr[irq >> 3] |= (1 << (irq & 7));
83             /* activate irq remapping in PIIX */
84             pci_config_writeb(d, 0x60 + i, irq);
85         }
86         outb(elcr[0], 0x4d0);
87         outb(elcr[1], 0x4d1);
88         dprintf(1, "PIIX3 init: elcr=%02x %02x\n",
89                 elcr[0], elcr[1]);
90     }
91 }
92
93 static void pci_bios_init_device(PCIDevice d)
94 {
95     int class;
96     u32 *paddr;
97     int i, pin, pic_irq, vendor_id, device_id;
98
99     class = pci_config_readw(d, PCI_CLASS_DEVICE);
100     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
101     device_id = pci_config_readw(d, PCI_DEVICE_ID);
102     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n",
103             d.bus, d.devfn, vendor_id, device_id);
104     switch(class) {
105     case 0x0101:
106         if (vendor_id == 0x8086 && device_id == 0x7010) {
107             /* PIIX3 IDE */
108             pci_config_writew(d, 0x40, 0x8000); // enable IDE0
109             pci_config_writew(d, 0x42, 0x8000); // enable IDE1
110             goto default_map;
111         } else {
112             /* IDE: we map it as in ISA mode */
113             pci_set_io_region_addr(d, 0, 0x1f0);
114             pci_set_io_region_addr(d, 1, 0x3f4);
115             pci_set_io_region_addr(d, 2, 0x170);
116             pci_set_io_region_addr(d, 3, 0x374);
117         }
118         break;
119     case 0x0300:
120         if (vendor_id != 0x1234)
121             goto default_map;
122         /* VGA: map frame buffer to default Bochs VBE address */
123         pci_set_io_region_addr(d, 0, 0xE0000000);
124         break;
125     case 0x0800:
126         /* PIC */
127         if (vendor_id == 0x1014) {
128             /* IBM */
129             if (device_id == 0x0046 || device_id == 0xFFFF) {
130                 /* MPIC & MPIC2 */
131                 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
132             }
133         }
134         break;
135     case 0xff00:
136         if (vendor_id == 0x0106b &&
137             (device_id == 0x0017 || device_id == 0x0022)) {
138             /* macio bridge */
139             pci_set_io_region_addr(d, 0, 0x80800000);
140         }
141         break;
142     default:
143     default_map:
144         /* default memory mappings */
145         for(i = 0; i < PCI_NUM_REGIONS; i++) {
146             int ofs;
147             u32 val, size ;
148
149             if (i == PCI_ROM_SLOT)
150                 ofs = 0x30;
151             else
152                 ofs = 0x10 + i * 4;
153             pci_config_writel(d, ofs, 0xffffffff);
154             val = pci_config_readl(d, ofs);
155             if (val != 0) {
156                 size = (~(val & ~0xf)) + 1;
157                 if (val & PCI_ADDRESS_SPACE_IO)
158                     paddr = &pci_bios_io_addr;
159                 else if (size >= 0x04000000)
160                     paddr = &pci_bios_bigmem_addr;
161                 else
162                     paddr = &pci_bios_mem_addr;
163                 *paddr = (*paddr + size - 1) & ~(size - 1);
164                 pci_set_io_region_addr(d, i, *paddr);
165                 *paddr += size;
166             }
167         }
168         break;
169     }
170
171     /* map the interrupt */
172     pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
173     if (pin != 0) {
174         pin = pci_slot_get_pirq(d, pin - 1);
175         pic_irq = pci_irqs[pin];
176         pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
177     }
178
179     if (vendor_id == 0x8086 && device_id == 0x7113) {
180         /* PIIX4 Power Management device (for ACPI) */
181         u32 pm_io_base = BUILD_PM_IO_BASE;
182         pci_config_writel(d, 0x40, pm_io_base | 1);
183         pci_config_writeb(d, 0x80, 0x01); /* enable PM io space */
184         u32 smb_io_base = BUILD_SMB_IO_BASE;
185         pci_config_writel(d, 0x90, smb_io_base | 1);
186         pci_config_writeb(d, 0xd2, 0x09); /* enable SMBus io space */
187     }
188 }
189
190 static void pci_for_each_device(void (*init_func)(PCIDevice d))
191 {
192     int bus, devfn;
193     u16 vendor_id, device_id;
194
195     for(bus = 0; bus < 1; bus++) {
196         for(devfn = 0; devfn < 256; devfn++) {
197             PCIDevice d = pci_bd(bus, devfn);
198             vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
199             device_id = pci_config_readw(d, PCI_DEVICE_ID);
200             if (vendor_id != 0xffff || device_id != 0xffff) {
201                 init_func(d);
202             }
203         }
204     }
205 }
206
207 void
208 pci_bios_setup(void)
209 {
210     if (CONFIG_COREBOOT)
211         // Already done by coreboot.
212         return;
213
214     pci_bios_io_addr = 0xc000;
215     pci_bios_mem_addr = 0xf0000000;
216     pci_bios_bigmem_addr = GET_EBDA(ram_size);
217     if (pci_bios_bigmem_addr < 0x90000000)
218         pci_bios_bigmem_addr = 0x90000000;
219
220     pci_for_each_device(pci_bios_init_bridges);
221
222     pci_for_each_device(pci_bios_init_device);
223 }