blob: 663895c2ceaafd613f9bffb368a29b9049937d49 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Uwe Hermannb80dbf02007-04-22 19:08:13 +00002
Li-Ta Lo883b8792005-01-10 23:16:22 +00003#include <console/console.h>
Marshall Dawson6f978cf2017-04-19 18:31:07 -06004#include <commonlib/endian.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +00005#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +00009#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000010#include <cbfs.h>
Patrick Rudolph00c0cd22017-06-06 19:30:55 +020011#include <cbmem.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070012#include <acpi/acpigen.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +000013
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020014/* Rmodules don't like weak symbols. */
Martin Rothdafcc7a2020-02-05 16:46:30 -070015void __weak map_oprom_vendev_rev(u32 *vendev, u8 *rev) { return; }
Aaron Durbin64031672018-04-21 14:45:32 -060016u32 __weak map_oprom_vendev(u32 vendev) { return vendev; }
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020017
Furquan Shaikh0f007d82020-04-24 06:41:18 -070018struct rom_header *pci_rom_probe(const struct device *dev)
Li-Ta Lo883b8792005-01-10 23:16:22 +000019{
Martin Rotha616a4b2020-01-21 09:28:40 -070020 struct rom_header *rom_header = NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +000021 struct pci_data *rom_data;
Martin Rothdafcc7a2020-02-05 16:46:30 -070022 u8 rev = pci_read_config8(dev, PCI_REVISION_ID);
23 u8 mapped_rev = rev;
24 u32 vendev = (dev->vendor << 16) | dev->device;
25 u32 mapped_vendev = vendev;
Li-Ta Lo883b8792005-01-10 23:16:22 +000026
Martin Rothdafcc7a2020-02-05 16:46:30 -070027 /* If the ROM is in flash, then don't check the PCI device for it. */
Martin Rotha616a4b2020-01-21 09:28:40 -070028 if (CONFIG(CHECK_REV_IN_OPROM_NAME)) {
Martin Rotha616a4b2020-01-21 09:28:40 -070029 rom_header = cbfs_boot_map_optionrom_revision(dev->vendor, dev->device, rev);
Martin Rothdafcc7a2020-02-05 16:46:30 -070030 map_oprom_vendev_rev(&mapped_vendev, &mapped_rev);
31 } else {
32 rom_header = cbfs_boot_map_optionrom(dev->vendor, dev->device);
33 mapped_vendev = map_oprom_vendev(vendev);
Martin Rotha616a4b2020-01-21 09:28:40 -070034 }
35
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080036 if (!rom_header) {
Martin Rothdafcc7a2020-02-05 16:46:30 -070037 if (CONFIG(CHECK_REV_IN_OPROM_NAME) &&
38 (vendev != mapped_vendev || rev != mapped_rev)) {
39 rom_header = cbfs_boot_map_optionrom_revision(
40 mapped_vendev >> 16,
41 mapped_vendev & 0xffff, mapped_rev);
42 } else if (vendev != mapped_vendev) {
Aaron Durbin899d13d2015-05-15 23:39:23 -050043 rom_header = cbfs_boot_map_optionrom(
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080044 mapped_vendev >> 16,
Aaron Durbin899d13d2015-05-15 23:39:23 -050045 mapped_vendev & 0xffff);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080046 }
47 }
48
Myles Watsond27c08c2009-11-06 23:42:26 +000049 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000050 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
51 dev_path(dev), rom_header);
Julius Wernercd49cce2019-03-05 16:53:33 -080052 } else if (!CONFIG(ON_DEVICE_ROM_LOAD)) {
Elyes HAOUASc32ccb72019-06-14 23:27:26 +020053 printk(BIOS_DEBUG, "PCI Option ROM loading disabled for %s\n",
54 dev_path(dev));
55 return NULL;
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +000056 } else {
Stefan Reinauer83fa1692015-06-18 22:01:07 -070057 uintptr_t rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000058
arch import user (historical)23053642005-07-06 16:49:59 +000059 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000060
61 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Angel Ponsd6152302020-03-01 14:46:38 +010062#if CONFIG(CPU_QEMU_X86)
Myles Watsonae3e9982009-11-17 15:20:22 +000063 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
64 rom_address = 0xc0000;
65 else
Uwe Hermanne4870472010-11-04 23:23:47 +000066#endif
Myles Watsonae3e9982009-11-17 15:20:22 +000067 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000068 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000069 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000070 pci_write_config32(dev, PCI_ROM_ADDRESS,
71 rom_address|PCI_ROM_ADDRESS_ENABLE);
72 }
73
Kyösti Mälkki8c9be432019-06-29 22:34:07 +030074 rom_address &= PCI_ROM_ADDRESS_MASK;
75
Stefan Reinauerafaa2572011-10-06 16:47:51 -070076 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000077 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000078 rom_header = (struct rom_header *)rom_address;
arch import user (historical)23053642005-07-06 16:49:59 +000079 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000080
Uwe Hermanne4870472010-11-04 23:23:47 +000081 printk(BIOS_SPEW, "PCI expansion ROM, signature 0x%04x, "
82 "INIT size 0x%04x, data ptr 0x%04x\n",
83 le32_to_cpu(rom_header->signature),
84 rom_header->size * 512, le32_to_cpu(rom_header->data));
85
Li-Ta Lo883b8792005-01-10 23:16:22 +000086 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Uwe Hermanne4870472010-11-04 23:23:47 +000087 printk(BIOS_ERR, "Incorrect expansion ROM header "
88 "signature %04x\n", le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +000089 return NULL;
90 }
91
Myles Watsond27c08c2009-11-06 23:42:26 +000092 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
93
Uwe Hermanne4870472010-11-04 23:23:47 +000094 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
95 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080096 /* If the device id is mapped, a mismatch is expected */
97 if ((dev->vendor != rom_data->vendor
98 || dev->device != rom_data->device)
99 && (vendev == mapped_vendev)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000100 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, "
Martin Rothb6e2afb2020-01-21 08:57:00 -0700101 "device ID %04x\n", dev->vendor, dev->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000102 return NULL;
103 }
104
Uwe Hermanne4870472010-11-04 23:23:47 +0000105 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, "
106 "Code Type %02x\n", rom_data->class_hi, rom_data->class_lo,
107 rom_data->type);
108
Yinghai Lu54ab3112005-01-18 03:10:46 +0000109 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000110 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000111 (rom_data->class_hi << 8) | rom_data->class_lo,
112 dev->class);
113 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000114 }
115
116 return rom_header;
117}
118
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000119static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000120
Uwe Hermanne4870472010-11-04 23:23:47 +0000121struct rom_header *pci_rom_load(struct device *dev,
122 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000123{
124 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000125 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000126 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000127
arch import user (historical)23053642005-07-06 16:49:59 +0000128 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000129 /* Get next image. */
130 rom_header = (struct rom_header *)((void *) rom_header
131 + image_size);
132
133 rom_data = (struct pci_data *)((void *) rom_header
134 + le32_to_cpu(rom_header->data));
135
136 image_size = le32_to_cpu(rom_data->ilen) * 512;
137 } 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 +0000138
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000139 if (rom_data->type != 0)
140 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000141
142 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000143
Uwe Hermanne4870472010-11-04 23:23:47 +0000144 /*
145 * We check to see if the device thinks it is a VGA device not
146 * whether the ROM image is for a VGA device because some
147 * devices have a mismatch between the hardware and the ROM.
148 */
Elyes HAOUAS0f8b8d92019-01-03 10:23:28 +0100149 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800150#if !CONFIG(MULTIPLE_VGA_ADAPTERS)
Elyes HAOUASe3480662018-05-06 20:32:23 +0200151 extern struct device *vga_pri; /* Primary VGA device (device.c). */
Uwe Hermanne4870472010-11-04 23:23:47 +0000152 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000153#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000154 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000155 printk(BIOS_DEBUG, "Copying VGA ROM Image from %p to "
156 "0x%x, 0x%x bytes\n", rom_header,
157 PCI_VGA_RAM_IMAGE_START, rom_size);
158 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
159 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000160 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000161 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000162 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000163
Uwe Hermanne4870472010-11-04 23:23:47 +0000164 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x "
165 "bytes\n", rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000166
167 memcpy(pci_ram_image_start, rom_header, rom_size);
168 pci_ram_image_start += rom_size;
169 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000170}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200171
172/* ACPI */
Julius Wernercd49cce2019-03-05 16:53:33 -0800173#if CONFIG(HAVE_ACPI_TABLES)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600174
175/* VBIOS may be modified after oprom init so use the copy if present. */
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700176static struct rom_header *check_initialized(const struct device *dev)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600177{
178 struct rom_header *run_rom;
179 struct pci_data *rom_data;
180
Nikolai Vyssotskib649d6a2021-03-11 19:15:03 -0600181 if (!CONFIG(VGA_ROM_RUN) && !CONFIG(RUN_FSP_GOP))
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600182 return NULL;
183
184 run_rom = (struct rom_header *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
185 if (read_le16(&run_rom->signature) != PCI_ROM_HDR)
186 return NULL;
187
188 rom_data = (struct pci_data *)((u8 *)run_rom
Jacob Garber7cfe68d2019-07-16 13:14:09 -0600189 + read_le16(&run_rom->data));
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600190
191 if (read_le32(&rom_data->signature) == PCI_DATA_HDR
192 && read_le16(&rom_data->device) == dev->device
193 && read_le16(&rom_data->vendor) == dev->vendor)
194 return run_rom;
195 else
196 return NULL;
197}
198
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200199static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700200pci_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200201 unsigned long current)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200202{
Himanshu Sahdevafd05052019-10-21 11:23:20 +0530203 acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200204 struct rom_header *rom;
205
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600206 rom = check_initialized(device);
207 if (!rom)
208 rom = pci_rom_probe(device);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200209 if (!rom) {
Elyes HAOUAS0c1d6602021-01-16 17:29:29 +0100210 printk(BIOS_ERR, "%s failed\n", __func__);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200211 return current;
212 }
213
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600214 printk(BIOS_DEBUG, " Copying %sVBIOS image from %p\n",
215 rom == (struct rom_header *)
216 (uintptr_t)PCI_VGA_RAM_IMAGE_START ?
217 "initialized " : "",
218 rom);
219
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200220 header->DeviceID = device->device;
221 header->VendorID = device->vendor;
222 header->PCIBus = device->bus->secondary;
223 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
224 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
225 header->ImageLength = rom->size * 512;
226 memcpy((void *)&header->VbiosContent, rom, header->ImageLength);
227
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300228 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
229
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200230 current += header->ImageLength;
231 return current;
232}
233
234unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700235pci_rom_write_acpi_tables(const struct device *device, unsigned long current,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200236 struct acpi_rsdp *rsdp)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200237{
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200238 /* Only handle VGA devices */
239 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
240 return current;
241
242 /* Only handle enabled devices */
243 if (!device->enabled)
244 return current;
245
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200246 /* AMD/ATI uses VFCT */
247 if (device->vendor == PCI_VENDOR_ID_ATI) {
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530248 acpi_vfct_t *vfct;
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300249
Felix Heldd5a11ed2019-06-20 14:35:44 +0200250 current = ALIGN_UP(current, 8);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530251 vfct = (acpi_vfct_t *)current;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200252 acpi_create_vfct(device, vfct, pci_rom_acpi_fill_vfct);
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300253 if (vfct->header.length) {
254 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
255 current += vfct->header.length;
256 acpi_add_table(rsdp, vfct);
257 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200258 }
259
260 return current;
261}
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200262
Furquan Shaikh7536a392020-04-24 21:59:21 -0700263void pci_rom_ssdt(const struct device *device)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200264{
265 static size_t ngfx;
266
Benjamin Doron90341c12020-08-04 06:24:03 +0000267 /* Only handle display devices */
268 if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200269 return;
270
271 /* Only handle enabled devices */
272 if (!device->enabled)
273 return;
274
275 /* Probe for option rom */
276 const struct rom_header *rom = pci_rom_probe(device);
277 if (!rom || !rom->size) {
278 printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n",
279 dev_path(device));
280 return;
281 }
282
283 const char *scope = acpi_device_path(device);
284 if (!scope) {
285 printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device));
286 return;
287 }
288
289 /* Supports up to four devices. */
290 if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) {
291 printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device));
292 return;
293 }
294
295 /* Prepare memory */
296 const size_t cbrom_length = rom->size * 512;
297 if (!cbrom_length) {
298 printk(BIOS_ERR, "%s: ROM has zero length!\n",
299 dev_path(device));
300 return;
301 }
302
303 void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length);
304 if (!cbrom) {
305 printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n",
306 dev_path(device));
307 return;
308 }
309 /* Increment CBMEM id for next device */
310 ngfx++;
311
312 memcpy(cbrom, rom, cbrom_length);
313
314 /* write _ROM method */
315 acpigen_write_scope(scope);
316 acpigen_write_rom(cbrom, cbrom_length);
317 acpigen_pop_len(); /* pop scope */
318}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200319#endif