Commit missing files from last commit.
[seabios.git] / src / pci.c
1 /*
2  * pci.c
3  * 
4  * Copyright (C) 2008  Nguyen Anh Quynh <aquynh@gmail.com>
5  * Copyright (C) 2002  MandrakeSoft S.A.
6  * 
7  * This file may be distributed under the terms of the GNU GPLv3 license.
8  */
9
10 #include "acpi.h"
11 #include "hardware.h"
12 #include "ioport.h"
13 #include "pci.h"
14 #include "smm.h"
15 #include "types.h"
16 #include "util.h"
17
18 u32 pm_io_base, smb_io_base;
19 int pm_sci_int;
20 PCIDevice i440_pcidev;
21
22 static u32 pci_bios_io_addr  = 0xC000;
23 static u32 pci_bios_mem_addr = 0xF0000000;
24 static u32 pci_bios_bigmem_addr;
25
26 /* host irqs corresponding to PCI irqs A-D */
27 static u8 pci_irqs[4] = { 11, 9, 11, 9 };
28
29
30 static void
31 pci_set_io_region_addr(PCIDevice *d, int region_num, u32 addr)
32 {
33     u16 cmd;
34     u32 ofs, old_addr;
35
36     if (region_num == PCI_ROM_SLOT)
37         ofs = 0x30;
38     else
39         ofs = 0x10 + region_num * 4;
40
41     old_addr = pci_config_readl(d, ofs);
42
43     pci_config_writel(d, ofs, addr);
44     BX_INFO("region %d: 0x%08x\n", region_num, addr);
45
46     /* enable memory mappings */
47     cmd = pci_config_readw(d, PCI_COMMAND);
48     if (region_num == PCI_ROM_SLOT)
49         cmd |= 2;
50     else if (old_addr & PCI_ADDRESS_SPACE_IO)
51         cmd |= 1;
52     else
53         cmd |= 2;
54     pci_config_writew(d, PCI_COMMAND, cmd);
55 }
56
57 /* return the global irq number corresponding to a given device irq
58    pin. We could also use the bus number to have a more precise
59    mapping. */
60 static int
61 pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
62 {
63     int slot_addend;
64
65     slot_addend = (pci_dev->devfn >> 3) - 1;
66
67     return (irq_num + slot_addend) & 3;
68 }
69
70 static int
71 find_bios_table_area(void)
72 {
73     unsigned long addr;
74
75     for (addr = 0xf0000; addr < 0x100000; addr += 16) {
76         if (*(u32 *)addr == 0xaafb4442) {
77             bios_table_cur_addr = addr + 8;
78             bios_table_end_addr = bios_table_cur_addr + *(u32 *)(addr + 4);
79             BX_INFO("bios_table_addr: 0x%08lx end=0x%08lx\n",
80                     bios_table_cur_addr, bios_table_end_addr);
81
82             return 0;
83         }
84     }
85
86     return -1;
87 }
88
89 static void
90 bios_shadow_init(PCIDevice *d)
91 {
92     int v;
93
94     if (find_bios_table_area() < 0)
95         return;
96
97     /* remap the BIOS to shadow RAM an keep it read/write while we
98        are writing tables */
99     v = pci_config_readb(d, 0x59);
100     v &= 0xcf;
101     pci_config_writeb(d, 0x59, v);
102     memcpy((void *)BIOS_TMP_STORAGE, (void *)0x000f0000, 0x10000);
103     v |= 0x30;
104     pci_config_writeb(d, 0x59, v);
105     memcpy((void *)0x000f0000, (void *)BIOS_TMP_STORAGE, 0x10000);
106
107     i440_pcidev = *d;
108 }
109
110 void bios_lock_shadow_ram(void)
111 {
112     PCIDevice *d = &i440_pcidev;
113     int v;
114
115     wbinvd();
116     v = pci_config_readb(d, 0x59);
117     v = (v & 0x0f) | (0x10);
118     pci_config_writeb(d, 0x59, v);
119 }
120
121 static void pci_bios_init_bridges(PCIDevice *d)
122 {
123     u16 vendor_id, device_id;
124
125     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
126     device_id = pci_config_readw(d, PCI_DEVICE_ID);
127
128     if (vendor_id == PCI_VENDOR_ID_INTEL && device_id == PCI_DEVICE_ID_INTEL_82371SB_0) {
129         int i, irq;
130         u8 elcr[2];
131
132         /* PIIX3 bridge */
133         elcr[0] = 0x00;
134         elcr[1] = 0x00;
135         for (i = 0; i < 4; i++) {
136             irq = pci_irqs[i];
137             /* set to trigger level */
138             elcr[irq >> 3] |= (1 << (irq & 7));
139             /* activate irq remapping in PIIX */
140             pci_config_writeb(d, 0x60 + i, irq);
141         }
142
143         outb(elcr[0], 0x4d0);
144         outb(elcr[1], 0x4d1);
145         BX_INFO("PIIX3 init: elcr=%02x %02x\n", elcr[0], elcr[1]);
146     }
147         else if (vendor_id == PCI_VENDOR_ID_INTEL && device_id == PCI_DEVICE_ID_INTEL_82441) {
148         /* i440 PCI bridge */
149         bios_shadow_init(d);
150     }
151 }
152
153 static void
154 pci_bios_init_device(PCIDevice *d)
155 {
156     int class;
157     u32 *paddr;
158     int i, pin, pic_irq, vendor_id, device_id;
159
160     class = pci_config_readw(d, PCI_CLASS_DEVICE);
161     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
162     device_id = pci_config_readw(d, PCI_DEVICE_ID);
163     BX_INFO("PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n",
164             d->bus, d->devfn, vendor_id, device_id);
165     switch(class) {
166     case 0x0101:
167         if (vendor_id == PCI_VENDOR_ID_INTEL && device_id == PCI_DEVICE_ID_INTEL_82371SB_1) {
168             /* PIIX3 IDE */
169             pci_config_writew(d, 0x40, 0x8000); // enable IDE0
170             pci_config_writew(d, 0x42, 0x8000); // enable IDE1
171             goto default_map;
172         } else {
173             /* IDE: we map it as in ISA mode */
174             pci_set_io_region_addr(d, 0, 0x1f0);
175             pci_set_io_region_addr(d, 1, 0x3f4);
176             pci_set_io_region_addr(d, 2, 0x170);
177             pci_set_io_region_addr(d, 3, 0x374);
178         }
179         break;
180     case 0x0300:
181         if (vendor_id != 0x1234)
182             goto default_map;
183         /* VGA: map frame buffer to default Bochs VBE address */
184         pci_set_io_region_addr(d, 0, 0xE0000000);
185         break;
186     case 0x0800:
187         /* PIC */
188         if (vendor_id == PCI_VENDOR_ID_IBM) {
189             /* IBM */
190             if (device_id == 0x0046 || device_id == 0xFFFF) {
191                 /* MPIC & MPIC2 */
192                 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
193             }
194         }
195         break;
196     case 0xff00:
197         if (vendor_id == PCI_VENDOR_ID_APPLE &&
198             (device_id == 0x0017 || device_id == 0x0022)) {
199             /* macio bridge */
200             pci_set_io_region_addr(d, 0, 0x80800000);
201         }
202         break;
203     default:
204     default_map:
205         /* default memory mappings */
206         for (i = 0; i < PCI_NUM_REGIONS; i++) {
207             int ofs;
208             u32 val, size ;
209
210             if (i == PCI_ROM_SLOT)
211                 ofs = 0x30;
212             else
213                 ofs = 0x10 + i * 4;
214             pci_config_writel(d, ofs, 0xffffffff);
215             val = pci_config_readl(d, ofs);
216             if (val != 0) {
217                 size = (~(val & ~0xf)) + 1;
218                 if (val & PCI_ADDRESS_SPACE_IO)
219                     paddr = &pci_bios_io_addr;
220                 else if (size >= 0x04000000)
221                     paddr = &pci_bios_bigmem_addr;
222                 else
223                     paddr = &pci_bios_mem_addr;
224                 *paddr = (*paddr + size - 1) & ~(size - 1);
225                 pci_set_io_region_addr(d, i, *paddr);
226                 *paddr += size;
227             }
228         }
229         break;
230     }
231
232     /* map the interrupt */
233     pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
234     if (pin != 0) {
235         pin = pci_slot_get_pirq(d, pin - 1);
236         pic_irq = pci_irqs[pin];
237         pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
238     }
239
240     if (vendor_id == PCI_VENDOR_ID_INTEL && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
241         /* PIIX4 Power Management device (for ACPI) */
242         pm_io_base = PM_IO_BASE;
243         pci_config_writel(d, 0x40, pm_io_base | 1);
244         pci_config_writeb(d, 0x80, 0x01); /* enable PM io space */
245         smb_io_base = SMB_IO_BASE;
246         pci_config_writel(d, 0x90, smb_io_base | 1);
247         pci_config_writeb(d, 0xd2, 0x09); /* enable SMBus io space */
248         pm_sci_int = pci_config_readb(d, PCI_INTERRUPT_LINE);
249 #ifdef CONFIG_SMM
250         smm_init(d);
251 #endif
252         acpi_enabled = 1;
253     }
254 }
255
256 static void
257 pci_for_each_device(void (*init_func)(PCIDevice *d))
258 {
259     PCIDevice d1, *d = &d1;
260     int bus, devfn;
261     u16 vendor_id, device_id;
262
263     for (bus = 0; bus < 1; bus++) {
264         for (devfn = 0; devfn < 256; devfn++) {
265             d->bus = bus;
266             d->devfn = devfn;
267             vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
268             device_id = pci_config_readw(d, PCI_DEVICE_ID);
269             if (vendor_id != 0xffff || device_id != 0xffff)
270                 init_func(d);
271         }
272     }
273 }
274
275 void
276 pci_bios_init(void)
277 {
278     pci_bios_bigmem_addr = ram_size;
279
280     if (pci_bios_bigmem_addr < 0x90000000)
281         pci_bios_bigmem_addr = 0x90000000;
282
283     pci_for_each_device(pci_bios_init_bridges);
284
285     pci_for_each_device(pci_bios_init_device);
286 }