blob: 106b5f9c8bc2c099354bc83f62be0b4ac9fe8ad5 [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 Rangela736f482021-07-16 14:03:21 -060019static void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
20{
21 char name[17] = "pciXXXX,XXXX.rom";
22
23 snprintf(name, sizeof(name), "pci%04hx,%04hx.rom", vendor, device);
24
25 return cbfs_map(name, NULL);
26}
27
28static void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t rev)
29{
30 char name[20] = "pciXXXX,XXXX,XX.rom";
31
32 snprintf(name, sizeof(name), "pci%04hx,%04hx,%02hhx.rom", vendor, device, rev);
33
34 return cbfs_map(name, NULL);
35}
36
Furquan Shaikh0f007d82020-04-24 06:41:18 -070037struct rom_header *pci_rom_probe(const struct device *dev)
Li-Ta Lo883b8792005-01-10 23:16:22 +000038{
Martin Rotha616a4b2020-01-21 09:28:40 -070039 struct rom_header *rom_header = NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +000040 struct pci_data *rom_data;
Martin Rothdafcc7a2020-02-05 16:46:30 -070041 u8 rev = pci_read_config8(dev, PCI_REVISION_ID);
42 u8 mapped_rev = rev;
43 u32 vendev = (dev->vendor << 16) | dev->device;
44 u32 mapped_vendev = vendev;
Li-Ta Lo883b8792005-01-10 23:16:22 +000045
Martin Rothdafcc7a2020-02-05 16:46:30 -070046 /* If the ROM is in flash, then don't check the PCI device for it. */
Martin Rotha616a4b2020-01-21 09:28:40 -070047 if (CONFIG(CHECK_REV_IN_OPROM_NAME)) {
Martin Rotha616a4b2020-01-21 09:28:40 -070048 rom_header = cbfs_boot_map_optionrom_revision(dev->vendor, dev->device, rev);
Martin Rothdafcc7a2020-02-05 16:46:30 -070049 map_oprom_vendev_rev(&mapped_vendev, &mapped_rev);
50 } else {
51 rom_header = cbfs_boot_map_optionrom(dev->vendor, dev->device);
52 mapped_vendev = map_oprom_vendev(vendev);
Martin Rotha616a4b2020-01-21 09:28:40 -070053 }
54
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080055 if (!rom_header) {
Martin Rothdafcc7a2020-02-05 16:46:30 -070056 if (CONFIG(CHECK_REV_IN_OPROM_NAME) &&
57 (vendev != mapped_vendev || rev != mapped_rev)) {
58 rom_header = cbfs_boot_map_optionrom_revision(
59 mapped_vendev >> 16,
60 mapped_vendev & 0xffff, mapped_rev);
61 } else if (vendev != mapped_vendev) {
Aaron Durbin899d13d2015-05-15 23:39:23 -050062 rom_header = cbfs_boot_map_optionrom(
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080063 mapped_vendev >> 16,
Aaron Durbin899d13d2015-05-15 23:39:23 -050064 mapped_vendev & 0xffff);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080065 }
66 }
67
Myles Watsond27c08c2009-11-06 23:42:26 +000068 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000069 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
70 dev_path(dev), rom_header);
Julius Wernercd49cce2019-03-05 16:53:33 -080071 } else if (!CONFIG(ON_DEVICE_ROM_LOAD)) {
Elyes HAOUASc32ccb72019-06-14 23:27:26 +020072 printk(BIOS_DEBUG, "PCI Option ROM loading disabled for %s\n",
73 dev_path(dev));
74 return NULL;
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +000075 } else {
Stefan Reinauer83fa1692015-06-18 22:01:07 -070076 uintptr_t rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000077
arch import user (historical)23053642005-07-06 16:49:59 +000078 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000079
80 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Angel Ponsd6152302020-03-01 14:46:38 +010081#if CONFIG(CPU_QEMU_X86)
Myles Watsonae3e9982009-11-17 15:20:22 +000082 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
83 rom_address = 0xc0000;
84 else
Uwe Hermanne4870472010-11-04 23:23:47 +000085#endif
Myles Watsonae3e9982009-11-17 15:20:22 +000086 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000087 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000088 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000089 pci_write_config32(dev, PCI_ROM_ADDRESS,
90 rom_address|PCI_ROM_ADDRESS_ENABLE);
91 }
92
Kyösti Mälkki8c9be432019-06-29 22:34:07 +030093 rom_address &= PCI_ROM_ADDRESS_MASK;
94
Stefan Reinauerafaa2572011-10-06 16:47:51 -070095 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000096 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000097 rom_header = (struct rom_header *)rom_address;
arch import user (historical)23053642005-07-06 16:49:59 +000098 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000099
Angel Ponsd19cc112021-07-04 11:41:31 +0200100 printk(BIOS_SPEW,
101 "PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000102 le32_to_cpu(rom_header->signature),
103 rom_header->size * 512, le32_to_cpu(rom_header->data));
104
Li-Ta Lo883b8792005-01-10 23:16:22 +0000105 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200106 printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x\n",
107 le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +0000108 return NULL;
109 }
110
Myles Watsond27c08c2009-11-06 23:42:26 +0000111 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
112
Uwe Hermanne4870472010-11-04 23:23:47 +0000113 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
114 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -0800115 /* If the device id is mapped, a mismatch is expected */
116 if ((dev->vendor != rom_data->vendor
117 || dev->device != rom_data->device)
118 && (vendev == mapped_vendev)) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200119 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, device ID %04x\n",
120 dev->vendor, dev->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000121 return NULL;
122 }
123
Angel Ponsd19cc112021-07-04 11:41:31 +0200124 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
125 rom_data->class_hi, rom_data->class_lo,
Uwe Hermanne4870472010-11-04 23:23:47 +0000126 rom_data->type);
127
Yinghai Lu54ab3112005-01-18 03:10:46 +0000128 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000129 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000130 (rom_data->class_hi << 8) | rom_data->class_lo,
131 dev->class);
132 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000133 }
134
135 return rom_header;
136}
137
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000138static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000139
Uwe Hermanne4870472010-11-04 23:23:47 +0000140struct rom_header *pci_rom_load(struct device *dev,
141 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000142{
143 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000144 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000145 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000146
arch import user (historical)23053642005-07-06 16:49:59 +0000147 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000148 /* Get next image. */
149 rom_header = (struct rom_header *)((void *) rom_header
150 + image_size);
151
152 rom_data = (struct pci_data *)((void *) rom_header
153 + le32_to_cpu(rom_header->data));
154
155 image_size = le32_to_cpu(rom_data->ilen) * 512;
156 } 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 +0000157
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000158 if (rom_data->type != 0)
159 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000160
161 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000162
Uwe Hermanne4870472010-11-04 23:23:47 +0000163 /*
164 * We check to see if the device thinks it is a VGA device not
165 * whether the ROM image is for a VGA device because some
166 * devices have a mismatch between the hardware and the ROM.
167 */
Elyes HAOUAS0f8b8d92019-01-03 10:23:28 +0100168 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800169#if !CONFIG(MULTIPLE_VGA_ADAPTERS)
Elyes HAOUASe3480662018-05-06 20:32:23 +0200170 extern struct device *vga_pri; /* Primary VGA device (device.c). */
Uwe Hermanne4870472010-11-04 23:23:47 +0000171 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000172#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000173 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200174 printk(BIOS_DEBUG,
175 "Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
176 rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
Uwe Hermanne4870472010-11-04 23:23:47 +0000177 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
178 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000179 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000180 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000181 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000182
Angel Ponsd19cc112021-07-04 11:41:31 +0200183 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x bytes\n",
184 rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000185
186 memcpy(pci_ram_image_start, rom_header, rom_size);
187 pci_ram_image_start += rom_size;
188 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000189}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200190
191/* ACPI */
Julius Wernercd49cce2019-03-05 16:53:33 -0800192#if CONFIG(HAVE_ACPI_TABLES)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600193
194/* VBIOS may be modified after oprom init so use the copy if present. */
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700195static struct rom_header *check_initialized(const struct device *dev)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600196{
197 struct rom_header *run_rom;
198 struct pci_data *rom_data;
199
Nikolai Vyssotskib649d6a2021-03-11 19:15:03 -0600200 if (!CONFIG(VGA_ROM_RUN) && !CONFIG(RUN_FSP_GOP))
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600201 return NULL;
202
203 run_rom = (struct rom_header *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
204 if (read_le16(&run_rom->signature) != PCI_ROM_HDR)
205 return NULL;
206
207 rom_data = (struct pci_data *)((u8 *)run_rom
Jacob Garber7cfe68d2019-07-16 13:14:09 -0600208 + read_le16(&run_rom->data));
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600209
210 if (read_le32(&rom_data->signature) == PCI_DATA_HDR
211 && read_le16(&rom_data->device) == dev->device
212 && read_le16(&rom_data->vendor) == dev->vendor)
213 return run_rom;
214 else
215 return NULL;
216}
217
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200218static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700219pci_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200220 unsigned long current)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200221{
Himanshu Sahdevafd05052019-10-21 11:23:20 +0530222 acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200223 struct rom_header *rom;
224
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600225 rom = check_initialized(device);
226 if (!rom)
227 rom = pci_rom_probe(device);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200228 if (!rom) {
Elyes HAOUAS0c1d6602021-01-16 17:29:29 +0100229 printk(BIOS_ERR, "%s failed\n", __func__);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200230 return current;
231 }
232
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600233 printk(BIOS_DEBUG, " Copying %sVBIOS image from %p\n",
234 rom == (struct rom_header *)
235 (uintptr_t)PCI_VGA_RAM_IMAGE_START ?
236 "initialized " : "",
237 rom);
238
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200239 header->DeviceID = device->device;
240 header->VendorID = device->vendor;
241 header->PCIBus = device->bus->secondary;
242 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
243 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
244 header->ImageLength = rom->size * 512;
245 memcpy((void *)&header->VbiosContent, rom, header->ImageLength);
246
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300247 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
248
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200249 current += header->ImageLength;
250 return current;
251}
252
253unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700254pci_rom_write_acpi_tables(const struct device *device, unsigned long current,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200255 struct acpi_rsdp *rsdp)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200256{
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200257 /* Only handle VGA devices */
258 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
259 return current;
260
261 /* Only handle enabled devices */
262 if (!device->enabled)
263 return current;
264
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200265 /* AMD/ATI uses VFCT */
266 if (device->vendor == PCI_VENDOR_ID_ATI) {
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530267 acpi_vfct_t *vfct;
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300268
Felix Heldd5a11ed2019-06-20 14:35:44 +0200269 current = ALIGN_UP(current, 8);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530270 vfct = (acpi_vfct_t *)current;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200271 acpi_create_vfct(device, vfct, pci_rom_acpi_fill_vfct);
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300272 if (vfct->header.length) {
273 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
274 current += vfct->header.length;
275 acpi_add_table(rsdp, vfct);
276 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200277 }
278
279 return current;
280}
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200281
Furquan Shaikh7536a392020-04-24 21:59:21 -0700282void pci_rom_ssdt(const struct device *device)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200283{
284 static size_t ngfx;
285
Benjamin Doron90341c12020-08-04 06:24:03 +0000286 /* Only handle display devices */
287 if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200288 return;
289
290 /* Only handle enabled devices */
291 if (!device->enabled)
292 return;
293
294 /* Probe for option rom */
295 const struct rom_header *rom = pci_rom_probe(device);
296 if (!rom || !rom->size) {
297 printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n",
298 dev_path(device));
299 return;
300 }
301
302 const char *scope = acpi_device_path(device);
303 if (!scope) {
304 printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device));
305 return;
306 }
307
308 /* Supports up to four devices. */
309 if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) {
310 printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device));
311 return;
312 }
313
314 /* Prepare memory */
315 const size_t cbrom_length = rom->size * 512;
316 if (!cbrom_length) {
317 printk(BIOS_ERR, "%s: ROM has zero length!\n",
318 dev_path(device));
319 return;
320 }
321
322 void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length);
323 if (!cbrom) {
324 printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n",
325 dev_path(device));
326 return;
327 }
328 /* Increment CBMEM id for next device */
329 ngfx++;
330
331 memcpy(cbrom, rom, cbrom_length);
332
333 /* write _ROM method */
334 acpigen_write_scope(scope);
335 acpigen_write_rom(cbrom, cbrom_length);
336 acpigen_pop_len(); /* pop scope */
337}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200338#endif