blob: 20621d36b2707f64688daa68247e08cd8a1e7d2c [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * Copyright (C) 2005 Li-Ta Lo <ollie@lanl.gov>
5 * Copyright (C) 2005 Tyan
6 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
7 * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com>
8 * Copyright (C) 2005-2007 Stefan Reinauer <stepan@openbios.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000018 */
19
Li-Ta Lo883b8792005-01-10 23:16:22 +000020#include <console/console.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <device/pci_ops.h>
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +000025#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000026#include <cbfs.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +000027
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020028/* Rmodules don't like weak symbols. */
29u32 __attribute__((weak)) map_oprom_vendev(u32 vendev) { return vendev; }
30
Uwe Hermannc1ee4292010-10-17 19:01:48 +000031struct rom_header *pci_rom_probe(struct device *dev)
Li-Ta Lo883b8792005-01-10 23:16:22 +000032{
Li-Ta Lo883b8792005-01-10 23:16:22 +000033 struct rom_header *rom_header;
34 struct pci_data *rom_data;
35
Myles Watsond27c08c2009-11-06 23:42:26 +000036 /* If it's in FLASH, then don't check device for ROM. */
Aaron Durbin899d13d2015-05-15 23:39:23 -050037 rom_header = cbfs_boot_map_optionrom(dev->vendor, dev->device);
Ronald G. Minnich308312c2009-04-06 20:38:34 +000038
Martin Rothe9dfdd92012-04-26 16:04:18 -060039 u32 vendev = (dev->vendor << 16) | dev->device;
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080040 u32 mapped_vendev = vendev;
41
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020042 mapped_vendev = map_oprom_vendev(vendev);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080043
44 if (!rom_header) {
45 if (vendev != mapped_vendev) {
Aaron Durbin899d13d2015-05-15 23:39:23 -050046 rom_header = cbfs_boot_map_optionrom(
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080047 mapped_vendev >> 16,
Aaron Durbin899d13d2015-05-15 23:39:23 -050048 mapped_vendev & 0xffff);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080049 }
50 }
51
Myles Watsond27c08c2009-11-06 23:42:26 +000052 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000053 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
54 dev_path(dev), rom_header);
Patrick Rudolph647e34d2016-02-11 08:36:50 +010055 } else if (!IS_ENABLED(CONFIG_ON_DEVICE_ROM_LOAD)) {
56 printk(BIOS_DEBUG, "PCI Option ROM loading disabled "
57 "for %s\n", dev_path(dev));
58 return NULL;
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +000059 } else {
Stefan Reinauer83fa1692015-06-18 22:01:07 -070060 uintptr_t rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000061
arch import user (historical)23053642005-07-06 16:49:59 +000062 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000063
64 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Stefan Reinauer1d888a92011-04-21 20:24:43 +000065#if CONFIG_BOARD_EMULATION_QEMU_X86
Myles Watsonae3e9982009-11-17 15:20:22 +000066 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
67 rom_address = 0xc0000;
68 else
Uwe Hermanne4870472010-11-04 23:23:47 +000069#endif
Myles Watsonae3e9982009-11-17 15:20:22 +000070 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000071 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000072 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000073 pci_write_config32(dev, PCI_ROM_ADDRESS,
74 rom_address|PCI_ROM_ADDRESS_ENABLE);
75 }
76
Stefan Reinauerafaa2572011-10-06 16:47:51 -070077 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000078 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000079 rom_header = (struct rom_header *)rom_address;
arch import user (historical)23053642005-07-06 16:49:59 +000080 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000081
Uwe Hermanne4870472010-11-04 23:23:47 +000082 printk(BIOS_SPEW, "PCI expansion ROM, signature 0x%04x, "
83 "INIT size 0x%04x, data ptr 0x%04x\n",
84 le32_to_cpu(rom_header->signature),
85 rom_header->size * 512, le32_to_cpu(rom_header->data));
86
Li-Ta Lo883b8792005-01-10 23:16:22 +000087 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Uwe Hermanne4870472010-11-04 23:23:47 +000088 printk(BIOS_ERR, "Incorrect expansion ROM header "
89 "signature %04x\n", le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +000090 return NULL;
91 }
92
Myles Watsond27c08c2009-11-06 23:42:26 +000093 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
94
Uwe Hermanne4870472010-11-04 23:23:47 +000095 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
96 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080097 /* If the device id is mapped, a mismatch is expected */
98 if ((dev->vendor != rom_data->vendor
99 || dev->device != rom_data->device)
100 && (vendev == mapped_vendev)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000101 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, "
102 "device ID %04x\n", rom_data->vendor, rom_data->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000103 return NULL;
104 }
105
Uwe Hermanne4870472010-11-04 23:23:47 +0000106 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, "
107 "Code Type %02x\n", rom_data->class_hi, rom_data->class_lo,
108 rom_data->type);
109
Yinghai Lu54ab3112005-01-18 03:10:46 +0000110 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000111 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000112 (rom_data->class_hi << 8) | rom_data->class_lo,
113 dev->class);
114 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000115 }
116
117 return rom_header;
118}
119
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000120static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000121
Uwe Hermanne4870472010-11-04 23:23:47 +0000122struct rom_header *pci_rom_load(struct device *dev,
123 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000124{
125 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000126 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000127 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000128
arch import user (historical)23053642005-07-06 16:49:59 +0000129 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000130 /* Get next image. */
131 rom_header = (struct rom_header *)((void *) rom_header
132 + image_size);
133
134 rom_data = (struct pci_data *)((void *) rom_header
135 + le32_to_cpu(rom_header->data));
136
137 image_size = le32_to_cpu(rom_data->ilen) * 512;
138 } while ((rom_data->type != 0) && (rom_data->indicator != 0)); // make sure we got x86 version
arch import user (historical)23053642005-07-06 16:49:59 +0000139
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000140 if (rom_data->type != 0)
141 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000142
143 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000144
Uwe Hermanne4870472010-11-04 23:23:47 +0000145 /*
146 * We check to see if the device thinks it is a VGA device not
147 * whether the ROM image is for a VGA device because some
148 * devices have a mismatch between the hardware and the ROM.
149 */
Mark Marshall2fa7c2e2009-11-05 08:10:12 +0000150 if (PCI_CLASS_DISPLAY_VGA == (dev->class >> 8)) {
Patrick Georgie1667822012-05-05 15:29:32 +0200151#if !CONFIG_MULTIPLE_VGA_ADAPTERS
Uwe Hermanne4870472010-11-04 23:23:47 +0000152 extern device_t vga_pri; /* Primary VGA device (device.c). */
153 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000154#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000155 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000156 printk(BIOS_DEBUG, "Copying VGA ROM Image from %p to "
157 "0x%x, 0x%x bytes\n", rom_header,
158 PCI_VGA_RAM_IMAGE_START, rom_size);
159 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
160 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000161 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000162 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000163 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000164
Uwe Hermanne4870472010-11-04 23:23:47 +0000165 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x "
166 "bytes\n", rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000167
168 memcpy(pci_ram_image_start, rom_header, rom_size);
169 pci_ram_image_start += rom_size;
170 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000171}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200172
173/* ACPI */
174#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
175static unsigned long
176pci_rom_acpi_fill_vfct(struct device *device,
177 struct acpi_vfct *vfct_struct,
178 unsigned long current)
179{
180 struct acpi_vfct_image_hdr *header = &vfct_struct->image_hdr;
181 struct rom_header *rom;
182
183 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
184
185 rom = pci_rom_probe(device);
186 if (!rom) {
187 printk(BIOS_ERR, "pci_rom_acpi_fill_vfct failed\n");
188 return current;
189 }
190
191 header->DeviceID = device->device;
192 header->VendorID = device->vendor;
193 header->PCIBus = device->bus->secondary;
194 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
195 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
196 header->ImageLength = rom->size * 512;
197 memcpy((void *)&header->VbiosContent, rom, header->ImageLength);
198
199 current += header->ImageLength;
200 return current;
201}
202
203unsigned long
204pci_rom_write_acpi_tables(struct device *device,
205 unsigned long current,
206 struct acpi_rsdp *rsdp)
207{
208 struct acpi_vfct *vfct;
209 struct rom_header *rom;
210
211 /* Only handle VGA devices */
212 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
213 return current;
214
215 /* Only handle enabled devices */
216 if (!device->enabled)
217 return current;
218
219 /* Probe for option rom */
220 rom = pci_rom_probe(device);
221 if (!rom)
222 return current;
223
224 /* AMD/ATI uses VFCT */
225 if (device->vendor == PCI_VENDOR_ID_ATI) {
226 current = ALIGN(current, 8);
227 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
228 vfct = (struct acpi_vfct *)current;
229 acpi_create_vfct(device, vfct, pci_rom_acpi_fill_vfct);
230 current += vfct->header.length;
231 acpi_add_table(rsdp, vfct);
232 }
233
234 return current;
235}
236#endif