1
// Initialize PCI devices (on emulators)
3
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4
// Copyright (C) 2006 Fabrice Bellard
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
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
14
#define PCI_ROM_SLOT 6
15
#define PCI_NUM_REGIONS 7
17
static void pci_bios_init_device_in_bus(int bus);
19
static u32 pci_bios_io_addr;
20
static u32 pci_bios_mem_addr;
21
static u32 pci_bios_prefmem_addr;
22
/* host irqs corresponding to PCI irqs A-D */
23
static u8 pci_irqs[4] = {
27
static u32 pci_bar(u16 bdf, int region_num)
29
if (region_num != PCI_ROM_SLOT) {
30
return PCI_BASE_ADDRESS_0 + region_num * 4;
33
#define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
34
u8 type = pci_config_readb(bdf, PCI_HEADER_TYPE);
35
type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
36
return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
39
static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
43
ofs = pci_bar(bdf, region_num);
45
old_addr = pci_config_readl(bdf, ofs);
47
pci_config_writel(bdf, ofs, addr);
48
dprintf(1, "region %d: 0x%08x\n", region_num, addr);
56
static int pci_bios_allocate_region(u16 bdf, int region_num)
59
u32 ofs = pci_bar(bdf, region_num);
61
u32 old = pci_config_readl(bdf, ofs);
63
if (region_num == PCI_ROM_SLOT) {
64
mask = PCI_ROM_ADDRESS_MASK;
65
pci_config_writel(bdf, ofs, mask);
67
if (old & PCI_BASE_ADDRESS_SPACE_IO)
68
mask = PCI_BASE_ADDRESS_IO_MASK;
70
mask = PCI_BASE_ADDRESS_MEM_MASK;
71
pci_config_writel(bdf, ofs, ~0);
73
u32 val = pci_config_readl(bdf, ofs);
74
pci_config_writel(bdf, ofs, old);
76
u32 size = (~(val & mask)) + 1;
78
if (val & PCI_BASE_ADDRESS_SPACE_IO) {
79
paddr = &pci_bios_io_addr;
80
if (ALIGN(*paddr, size) + size >= 64 * 1024) {
82
"io region of (bdf 0x%x bar %d) can't be mapped.\n",
86
} else if ((val & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
87
/* keep behaviour on bus = 0 */
88
pci_bdf_to_bus(bdf) != 0 &&
89
/* If pci_bios_prefmem_addr == 0, keep old behaviour */
90
pci_bios_prefmem_addr != 0) {
91
paddr = &pci_bios_prefmem_addr;
92
if (ALIGN(*paddr, size) + size >= BUILD_PCIPREFMEM_END) {
94
"prefmem region of (bdf 0x%x bar %d) can't be mapped. "
95
"decrease BUILD_PCIMEM_SIZE and recompile. size %x\n",
96
bdf, region_num, BUILD_PCIPREFMEM_SIZE);
100
paddr = &pci_bios_mem_addr;
101
if (ALIGN(*paddr, size) + size >= BUILD_PCIMEM_END) {
103
"mem region of (bdf 0x%x bar %d) can't be mapped. "
104
"increase BUILD_PCIMEM_SIZE and recompile. size %x\n",
105
bdf, region_num, BUILD_PCIMEM_SIZE);
110
*paddr = ALIGN(*paddr, size);
111
pci_set_io_region_addr(bdf, region_num, *paddr);
116
int is_64bit = !(val & PCI_BASE_ADDRESS_SPACE_IO) &&
117
(val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64;
120
pci_config_writel(bdf, ofs + 4, 0);
122
pci_config_writel(bdf, ofs + 4, ~0);
128
static void pci_bios_allocate_regions(u16 bdf)
131
for (i = 0; i < PCI_NUM_REGIONS; i++) {
132
int is_64bit = pci_bios_allocate_region(bdf, i);
139
/* return the global irq number corresponding to a given device irq
140
pin. We could also use the bus number to have a more precise
142
static int pci_slot_get_pirq(u16 bdf, int irq_num)
144
int slot_addend = pci_bdf_to_dev(bdf) - 1;
145
return (irq_num + slot_addend) & 3;
148
static void pci_bios_init_bridges(u16 bdf)
150
u16 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
151
u16 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
153
if (vendor_id == PCI_VENDOR_ID_INTEL
154
&& (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
155
|| device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
159
/* PIIX3/PIIX4 PCI to ISA bridge */
163
for (i = 0; i < 4; i++) {
165
/* set to trigger level */
166
elcr[irq >> 3] |= (1 << (irq & 7));
167
/* activate irq remapping in PIIX */
168
pci_config_writeb(bdf, 0x60 + i, irq);
170
outb(elcr[0], 0x4d0);
171
outb(elcr[1], 0x4d1);
172
dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
177
#define PCI_IO_ALIGN 4096
178
#define PCI_IO_SHIFT 8
179
#define PCI_MEMORY_ALIGN (1UL << 20)
180
#define PCI_MEMORY_SHIFT 16
181
#define PCI_PREF_MEMORY_ALIGN (1UL << 20)
182
#define PCI_PREF_MEMORY_SHIFT 16
184
static void pci_bios_init_device_bridge(u16 bdf)
186
pci_bios_allocate_region(bdf, 0);
187
pci_bios_allocate_region(bdf, 1);
188
pci_bios_allocate_region(bdf, PCI_ROM_SLOT);
190
u32 io_old = pci_bios_io_addr;
191
u32 mem_old = pci_bios_mem_addr;
192
u32 prefmem_old = pci_bios_prefmem_addr;
194
/* IO BASE is assumed to be 16 bit */
195
pci_bios_io_addr = ALIGN(pci_bios_io_addr, PCI_IO_ALIGN);
196
pci_bios_mem_addr = ALIGN(pci_bios_mem_addr, PCI_MEMORY_ALIGN);
197
pci_bios_prefmem_addr =
198
ALIGN(pci_bios_prefmem_addr, PCI_PREF_MEMORY_ALIGN);
200
u32 io_base = pci_bios_io_addr;
201
u32 mem_base = pci_bios_mem_addr;
202
u32 prefmem_base = pci_bios_prefmem_addr;
204
u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
206
pci_bios_init_device_in_bus(secbus);
209
pci_bios_io_addr = ALIGN(pci_bios_io_addr, PCI_IO_ALIGN);
210
pci_bios_mem_addr = ALIGN(pci_bios_mem_addr, PCI_MEMORY_ALIGN);
211
pci_bios_prefmem_addr =
212
ALIGN(pci_bios_prefmem_addr, PCI_PREF_MEMORY_ALIGN);
214
u32 io_end = pci_bios_io_addr;
215
if (io_end == io_base) {
216
pci_bios_io_addr = io_old;
220
pci_config_writeb(bdf, PCI_IO_BASE, io_base >> PCI_IO_SHIFT);
221
pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
222
pci_config_writeb(bdf, PCI_IO_LIMIT, (io_end - 1) >> PCI_IO_SHIFT);
223
pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
225
u32 mem_end = pci_bios_mem_addr;
226
if (mem_end == mem_base) {
227
pci_bios_mem_addr = mem_old;
228
mem_base = 0xffffffff;
231
pci_config_writew(bdf, PCI_MEMORY_BASE, mem_base >> PCI_MEMORY_SHIFT);
232
pci_config_writew(bdf, PCI_MEMORY_LIMIT, (mem_end -1) >> PCI_MEMORY_SHIFT);
234
u32 prefmem_end = pci_bios_prefmem_addr;
235
if (prefmem_end == prefmem_base) {
236
pci_bios_prefmem_addr = prefmem_old;
237
prefmem_base = 0xffffffff;
240
pci_config_writew(bdf, PCI_PREF_MEMORY_BASE,
241
prefmem_base >> PCI_PREF_MEMORY_SHIFT);
242
pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT,
243
(prefmem_end - 1) >> PCI_PREF_MEMORY_SHIFT);
244
pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
245
pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
247
dprintf(1, "PCI: br io = [0x%x, 0x%x)\n", io_base, io_end);
248
dprintf(1, "PCI: br mem = [0x%x, 0x%x)\n", mem_base, mem_end);
249
dprintf(1, "PCI: br pref = [0x%x, 0x%x)\n", prefmem_base, prefmem_end);
251
u16 cmd = pci_config_readw(bdf, PCI_COMMAND);
252
cmd &= ~PCI_COMMAND_IO;
253
if (io_end > io_base) {
254
cmd |= PCI_COMMAND_IO;
256
cmd &= ~PCI_COMMAND_MEMORY;
257
if (mem_end > mem_base || prefmem_end > prefmem_base) {
258
cmd |= PCI_COMMAND_MEMORY;
260
cmd |= PCI_COMMAND_MASTER;
261
pci_config_writew(bdf, PCI_COMMAND, cmd);
263
pci_config_maskw(bdf, PCI_BRIDGE_CONTROL, 0, PCI_BRIDGE_CTL_SERR);
266
static void pci_bios_init_device(u16 bdf)
269
int pin, pic_irq, vendor_id, device_id;
271
class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
272
vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
273
device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
274
dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
275
, pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
277
case PCI_CLASS_STORAGE_IDE:
278
if (vendor_id == PCI_VENDOR_ID_INTEL
279
&& (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
280
|| device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
281
/* PIIX3/PIIX4 IDE */
282
pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
283
pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
284
pci_bios_allocate_regions(bdf);
286
/* IDE: we map it as in ISA mode */
287
pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
288
pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
289
pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
290
pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
293
case PCI_CLASS_SYSTEM_PIC:
295
if (vendor_id == PCI_VENDOR_ID_IBM) {
297
if (device_id == 0x0046 || device_id == 0xFFFF) {
299
pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
304
if (vendor_id == PCI_VENDOR_ID_APPLE &&
305
(device_id == 0x0017 || device_id == 0x0022)) {
307
pci_set_io_region_addr(bdf, 0, 0x80800000);
310
case PCI_CLASS_BRIDGE_PCI:
311
pci_bios_init_device_bridge(bdf);
314
/* default memory mappings */
315
pci_bios_allocate_regions(bdf);
319
/* enable memory mappings */
320
pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
322
/* map the interrupt */
323
pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
325
pin = pci_slot_get_pirq(bdf, pin - 1);
326
pic_irq = pci_irqs[pin];
327
pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
330
if (vendor_id == PCI_VENDOR_ID_INTEL
331
&& device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
332
/* PIIX4 Power Management device (for ACPI) */
334
// acpi sci is hardwired to 9
335
pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
337
pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
338
pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
339
pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
340
pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
344
static void pci_bios_init_device_in_bus(int bus)
347
foreachpci_in_bus(bdf, max, bus) {
348
pci_bios_init_device(bdf);
353
pci_bios_init_bus_rec(int bus, u8 *pci_bus)
358
dprintf(1, "PCI: %s bus = 0x%x\n", __func__, bus);
360
/* prevent accidental access to unintended devices */
361
foreachpci_in_bus(bdf, max, bus) {
362
class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
363
if (class == PCI_CLASS_BRIDGE_PCI) {
364
pci_config_writeb(bdf, PCI_SECONDARY_BUS, 255);
365
pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 0);
369
foreachpci_in_bus(bdf, max, bus) {
370
class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
371
if (class != PCI_CLASS_BRIDGE_PCI) {
374
dprintf(1, "PCI: %s bdf = 0x%x\n", __func__, bdf);
376
u8 pribus = pci_config_readb(bdf, PCI_PRIMARY_BUS);
378
dprintf(1, "PCI: primary bus = 0x%x -> 0x%x\n", pribus, bus);
379
pci_config_writeb(bdf, PCI_PRIMARY_BUS, bus);
381
dprintf(1, "PCI: primary bus = 0x%x\n", pribus);
384
u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
386
if (*pci_bus != secbus) {
387
dprintf(1, "PCI: secondary bus = 0x%x -> 0x%x\n",
390
pci_config_writeb(bdf, PCI_SECONDARY_BUS, secbus);
392
dprintf(1, "PCI: secondary bus = 0x%x\n", secbus);
395
/* set to max for access to all subordinate buses.
396
later set it to accurate value */
397
u8 subbus = pci_config_readb(bdf, PCI_SUBORDINATE_BUS);
398
pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, 255);
400
pci_bios_init_bus_rec(secbus, pci_bus);
402
if (subbus != *pci_bus) {
403
dprintf(1, "PCI: subordinate bus = 0x%x -> 0x%x\n",
407
dprintf(1, "PCI: subordinate bus = 0x%x\n", subbus);
409
pci_config_writeb(bdf, PCI_SUBORDINATE_BUS, subbus);
414
pci_bios_init_bus(void)
417
pci_bios_init_bus_rec(0 /* host bus */, &pci_bus);
424
// Already done by coreboot.
427
dprintf(3, "pci setup\n");
429
pci_bios_io_addr = 0xc000;
430
pci_bios_mem_addr = BUILD_PCIMEM_START;
431
pci_bios_prefmem_addr = BUILD_PCIPREFMEM_START;
436
foreachpci(bdf, max) {
437
pci_bios_init_bridges(bdf);
439
pci_bios_init_device_in_bus(0 /* host bus */);