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