blob: b8dafd1bdd0606b01271ba18e1724c11a1b3a49e [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>
Raul E Rangela736f482021-07-16 14:03:21 -06009#include <stdio.h>
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +000010#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000011#include <cbfs.h>
Patrick Rudolph00c0cd22017-06-06 19:30:55 +020012#include <cbmem.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070013#include <acpi/acpigen.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +000014
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020015/* Rmodules don't like weak symbols. */
Martin Rothdafcc7a2020-02-05 16:46:30 -070016void __weak map_oprom_vendev_rev(u32 *vendev, u8 *rev) { return; }
Aaron Durbin64031672018-04-21 14:45:32 -060017u32 __weak map_oprom_vendev(u32 vendev) { return vendev; }
Kyösti Mälkki1bdd3212014-12-26 13:29:09 +020018
Raul E Rangela3811fe2021-11-01 13:40:14 -060019void vga_oprom_preload(void)
20{
21/* The CONFIG_VGA_BIOS_ID symbol is only defined when VGA_BIOS is selected */
22#if CONFIG(VGA_BIOS)
23 const char name[] = "pci" CONFIG_VGA_BIOS_ID ".rom";
24
25 if (!CONFIG(CBFS_PRELOAD))
26 return;
27
28 printk(BIOS_DEBUG, "Preloading VGA ROM %s\n", name);
29
30 cbfs_preload(name);
31#endif
32}
33
Raul E Rangela736f482021-07-16 14:03:21 -060034static void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
35{
36 char name[17] = "pciXXXX,XXXX.rom";
37
38 snprintf(name, sizeof(name), "pci%04hx,%04hx.rom", vendor, device);
39
40 return cbfs_map(name, NULL);
41}
42
43static void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
44{
45 char name[20] = "pciXXXX,XXXX,XX.rom";
46
47 snprintf(name, sizeof(name), "pci%04hx,%04hx,%02hhx.rom", vendor, device, rev);
48
49 return cbfs_map(name, NULL);
50}
51
Furquan Shaikh0f007d82020-04-24 06:41:18 -070052struct rom_header *pci_rom_probe(const struct device *dev)
Li-Ta Lo883b8792005-01-10 23:16:22 +000053{
Martin Rotha616a4b2020-01-21 09:28:40 -070054 struct rom_header *rom_header = NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +000055 struct pci_data *rom_data;
Martin Rothdafcc7a2020-02-05 16:46:30 -070056 u8 rev = pci_read_config8(dev, PCI_REVISION_ID);
57 u8 mapped_rev = rev;
58 u32 vendev = (dev->vendor << 16) | dev->device;
59 u32 mapped_vendev = vendev;
Li-Ta Lo883b8792005-01-10 23:16:22 +000060
Martin Rothdafcc7a2020-02-05 16:46:30 -070061 /* If the ROM is in flash, then don't check the PCI device for it. */
Martin Rotha616a4b2020-01-21 09:28:40 -070062 if (CONFIG(CHECK_REV_IN_OPROM_NAME)) {
Martin Rotha616a4b2020-01-21 09:28:40 -070063 rom_header = cbfs_boot_map_optionrom_revision(dev->vendor, dev->device, rev);
Martin Rothdafcc7a2020-02-05 16:46:30 -070064 map_oprom_vendev_rev(&mapped_vendev, &mapped_rev);
65 } else {
66 rom_header = cbfs_boot_map_optionrom(dev->vendor, dev->device);
67 mapped_vendev = map_oprom_vendev(vendev);
Martin Rotha616a4b2020-01-21 09:28:40 -070068 }
69
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080070 if (!rom_header) {
Martin Rothdafcc7a2020-02-05 16:46:30 -070071 if (CONFIG(CHECK_REV_IN_OPROM_NAME) &&
72 (vendev != mapped_vendev || rev != mapped_rev)) {
73 rom_header = cbfs_boot_map_optionrom_revision(
74 mapped_vendev >> 16,
75 mapped_vendev & 0xffff, mapped_rev);
76 } else if (vendev != mapped_vendev) {
Aaron Durbin899d13d2015-05-15 23:39:23 -050077 rom_header = cbfs_boot_map_optionrom(
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080078 mapped_vendev >> 16,
Aaron Durbin899d13d2015-05-15 23:39:23 -050079 mapped_vendev & 0xffff);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080080 }
81 }
82
Myles Watsond27c08c2009-11-06 23:42:26 +000083 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000084 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
85 dev_path(dev), rom_header);
Raul E Rangelad5307e2021-07-16 15:06:56 -060086 } else if (CONFIG(ON_DEVICE_ROM_LOAD)) {
Stefan Reinauer83fa1692015-06-18 22:01:07 -070087 uintptr_t rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000088
arch import user (historical)23053642005-07-06 16:49:59 +000089 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000090
91 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Raul E Rangel69cb69f2021-07-16 15:04:18 -060092 if (CONFIG(CPU_QEMU_X86) && (dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
Myles Watsonae3e9982009-11-17 15:20:22 +000093 rom_address = 0xc0000;
94 else
Myles Watsonae3e9982009-11-17 15:20:22 +000095 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000096 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000097 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000098 pci_write_config32(dev, PCI_ROM_ADDRESS,
99 rom_address|PCI_ROM_ADDRESS_ENABLE);
100 }
101
Kyösti Mälkki8c9be432019-06-29 22:34:07 +0300102 rom_address &= PCI_ROM_ADDRESS_MASK;
103
Stefan Reinauerafaa2572011-10-06 16:47:51 -0700104 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000105 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +0000106 rom_header = (struct rom_header *)rom_address;
Raul E Rangelad5307e2021-07-16 15:06:56 -0600107 } else {
108 printk(BIOS_DEBUG, "PCI Option ROM loading disabled for %s\n",
109 dev_path(dev));
110 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000111 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000112
Angel Ponsd19cc112021-07-04 11:41:31 +0200113 printk(BIOS_SPEW,
114 "PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000115 le32_to_cpu(rom_header->signature),
116 rom_header->size * 512, le32_to_cpu(rom_header->data));
117
Li-Ta Lo883b8792005-01-10 23:16:22 +0000118 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200119 printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x\n",
120 le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +0000121 return NULL;
122 }
123
Myles Watsond27c08c2009-11-06 23:42:26 +0000124 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
125
Uwe Hermanne4870472010-11-04 23:23:47 +0000126 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
127 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -0800128 /* If the device id is mapped, a mismatch is expected */
129 if ((dev->vendor != rom_data->vendor
130 || dev->device != rom_data->device)
131 && (vendev == mapped_vendev)) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200132 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, device ID %04x\n",
133 dev->vendor, dev->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000134 return NULL;
135 }
136
Angel Ponsd19cc112021-07-04 11:41:31 +0200137 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
138 rom_data->class_hi, rom_data->class_lo,
Uwe Hermanne4870472010-11-04 23:23:47 +0000139 rom_data->type);
140
Yinghai Lu54ab3112005-01-18 03:10:46 +0000141 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000142 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000143 (rom_data->class_hi << 8) | rom_data->class_lo,
144 dev->class);
145 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000146 }
147
148 return rom_header;
149}
150
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000151static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000152
Uwe Hermanne4870472010-11-04 23:23:47 +0000153struct rom_header *pci_rom_load(struct device *dev,
154 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000155{
156 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000157 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000158 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000159
arch import user (historical)23053642005-07-06 16:49:59 +0000160 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000161 /* Get next image. */
162 rom_header = (struct rom_header *)((void *) rom_header
163 + image_size);
164
165 rom_data = (struct pci_data *)((void *) rom_header
166 + le32_to_cpu(rom_header->data));
167
168 image_size = le32_to_cpu(rom_data->ilen) * 512;
169 } 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 +0000170
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000171 if (rom_data->type != 0)
172 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000173
174 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000175
Uwe Hermanne4870472010-11-04 23:23:47 +0000176 /*
177 * We check to see if the device thinks it is a VGA device not
178 * whether the ROM image is for a VGA device because some
179 * devices have a mismatch between the hardware and the ROM.
180 */
Elyes HAOUAS0f8b8d92019-01-03 10:23:28 +0100181 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800182#if !CONFIG(MULTIPLE_VGA_ADAPTERS)
Elyes HAOUASe3480662018-05-06 20:32:23 +0200183 extern struct device *vga_pri; /* Primary VGA device (device.c). */
Uwe Hermanne4870472010-11-04 23:23:47 +0000184 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000185#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000186 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200187 printk(BIOS_DEBUG,
188 "Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
189 rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
Uwe Hermanne4870472010-11-04 23:23:47 +0000190 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
191 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000192 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000193 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000194 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000195
Angel Ponsd19cc112021-07-04 11:41:31 +0200196 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x bytes\n",
197 rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000198
199 memcpy(pci_ram_image_start, rom_header, rom_size);
200 pci_ram_image_start += rom_size;
201 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000202}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200203
204/* ACPI */
Julius Wernercd49cce2019-03-05 16:53:33 -0800205#if CONFIG(HAVE_ACPI_TABLES)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600206
207/* VBIOS may be modified after oprom init so use the copy if present. */
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700208static struct rom_header *check_initialized(const struct device *dev)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600209{
210 struct rom_header *run_rom;
211 struct pci_data *rom_data;
212
Nikolai Vyssotskib649d6a2021-03-11 19:15:03 -0600213 if (!CONFIG(VGA_ROM_RUN) && !CONFIG(RUN_FSP_GOP))
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600214 return NULL;
215
216 run_rom = (struct rom_header *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
217 if (read_le16(&run_rom->signature) != PCI_ROM_HDR)
218 return NULL;
219
220 rom_data = (struct pci_data *)((u8 *)run_rom
Jacob Garber7cfe68d2019-07-16 13:14:09 -0600221 + read_le16(&run_rom->data));
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600222
223 if (read_le32(&rom_data->signature) == PCI_DATA_HDR
224 && read_le16(&rom_data->device) == dev->device
225 && read_le16(&rom_data->vendor) == dev->vendor)
226 return run_rom;
227 else
228 return NULL;
229}
230
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200231static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700232pci_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200233 unsigned long current)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200234{
Himanshu Sahdevafd05052019-10-21 11:23:20 +0530235 acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200236 struct rom_header *rom;
237
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600238 rom = check_initialized(device);
239 if (!rom)
240 rom = pci_rom_probe(device);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200241 if (!rom) {
Elyes HAOUAS0c1d6602021-01-16 17:29:29 +0100242 printk(BIOS_ERR, "%s failed\n", __func__);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200243 return current;
244 }
245
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600246 printk(BIOS_DEBUG, " Copying %sVBIOS image from %p\n",
247 rom == (struct rom_header *)
248 (uintptr_t)PCI_VGA_RAM_IMAGE_START ?
249 "initialized " : "",
250 rom);
251
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200252 header->DeviceID = device->device;
253 header->VendorID = device->vendor;
254 header->PCIBus = device->bus->secondary;
255 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
256 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
257 header->ImageLength = rom->size * 512;
258 memcpy((void *)&header->VbiosContent, rom, header->ImageLength);
259
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300260 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
261
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200262 current += header->ImageLength;
263 return current;
264}
265
266unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700267pci_rom_write_acpi_tables(const struct device *device, unsigned long current,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200268 struct acpi_rsdp *rsdp)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200269{
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200270 /* Only handle VGA devices */
271 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
272 return current;
273
274 /* Only handle enabled devices */
275 if (!device->enabled)
276 return current;
277
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200278 /* AMD/ATI uses VFCT */
Felix Singer43b7f412022-03-07 04:34:52 +0100279 if (device->vendor == PCI_VID_ATI) {
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530280 acpi_vfct_t *vfct;
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300281
Felix Heldd5a11ed2019-06-20 14:35:44 +0200282 current = ALIGN_UP(current, 8);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530283 vfct = (acpi_vfct_t *)current;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200284 acpi_create_vfct(device, vfct, pci_rom_acpi_fill_vfct);
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300285 if (vfct->header.length) {
286 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
287 current += vfct->header.length;
288 acpi_add_table(rsdp, vfct);
289 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200290 }
291
292 return current;
293}
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200294
Furquan Shaikh7536a392020-04-24 21:59:21 -0700295void pci_rom_ssdt(const struct device *device)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200296{
297 static size_t ngfx;
298
Benjamin Doron90341c12020-08-04 06:24:03 +0000299 /* Only handle display devices */
300 if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200301 return;
302
303 /* Only handle enabled devices */
304 if (!device->enabled)
305 return;
306
307 /* Probe for option rom */
308 const struct rom_header *rom = pci_rom_probe(device);
309 if (!rom || !rom->size) {
310 printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n",
311 dev_path(device));
312 return;
313 }
314
315 const char *scope = acpi_device_path(device);
316 if (!scope) {
317 printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device));
318 return;
319 }
320
321 /* Supports up to four devices. */
322 if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) {
323 printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device));
324 return;
325 }
326
327 /* Prepare memory */
328 const size_t cbrom_length = rom->size * 512;
329 if (!cbrom_length) {
330 printk(BIOS_ERR, "%s: ROM has zero length!\n",
331 dev_path(device));
332 return;
333 }
334
335 void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length);
336 if (!cbrom) {
337 printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n",
338 dev_path(device));
339 return;
340 }
341 /* Increment CBMEM id for next device */
342 ngfx++;
343
344 memcpy(cbrom, rom, cbrom_length);
345
346 /* write _ROM method */
347 acpigen_write_scope(scope);
348 acpigen_write_rom(cbrom, cbrom_length);
349 acpigen_pop_len(); /* pop scope */
350}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200351#endif