blob: fe7c1cfdf029f86ecf8ad5d173b82681f5aa4e5b [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) {
Raul E Rangel69cb69f2021-07-16 15:04:18 -060081 if (CONFIG(CPU_QEMU_X86) && (dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
Myles Watsonae3e9982009-11-17 15:20:22 +000082 rom_address = 0xc0000;
83 else
Myles Watsonae3e9982009-11-17 15:20:22 +000084 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000085 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000086 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000087 pci_write_config32(dev, PCI_ROM_ADDRESS,
88 rom_address|PCI_ROM_ADDRESS_ENABLE);
89 }
90
Kyösti Mälkki8c9be432019-06-29 22:34:07 +030091 rom_address &= PCI_ROM_ADDRESS_MASK;
92
Stefan Reinauerafaa2572011-10-06 16:47:51 -070093 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000094 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000095 rom_header = (struct rom_header *)rom_address;
arch import user (historical)23053642005-07-06 16:49:59 +000096 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000097
Angel Ponsd19cc112021-07-04 11:41:31 +020098 printk(BIOS_SPEW,
99 "PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000100 le32_to_cpu(rom_header->signature),
101 rom_header->size * 512, le32_to_cpu(rom_header->data));
102
Li-Ta Lo883b8792005-01-10 23:16:22 +0000103 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200104 printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x\n",
105 le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +0000106 return NULL;
107 }
108
Myles Watsond27c08c2009-11-06 23:42:26 +0000109 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
110
Uwe Hermanne4870472010-11-04 23:23:47 +0000111 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
112 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -0800113 /* If the device id is mapped, a mismatch is expected */
114 if ((dev->vendor != rom_data->vendor
115 || dev->device != rom_data->device)
116 && (vendev == mapped_vendev)) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200117 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, device ID %04x\n",
118 dev->vendor, dev->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000119 return NULL;
120 }
121
Angel Ponsd19cc112021-07-04 11:41:31 +0200122 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
123 rom_data->class_hi, rom_data->class_lo,
Uwe Hermanne4870472010-11-04 23:23:47 +0000124 rom_data->type);
125
Yinghai Lu54ab3112005-01-18 03:10:46 +0000126 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000127 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000128 (rom_data->class_hi << 8) | rom_data->class_lo,
129 dev->class);
130 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000131 }
132
133 return rom_header;
134}
135
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000136static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000137
Uwe Hermanne4870472010-11-04 23:23:47 +0000138struct rom_header *pci_rom_load(struct device *dev,
139 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000140{
141 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000142 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000143 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000144
arch import user (historical)23053642005-07-06 16:49:59 +0000145 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000146 /* Get next image. */
147 rom_header = (struct rom_header *)((void *) rom_header
148 + image_size);
149
150 rom_data = (struct pci_data *)((void *) rom_header
151 + le32_to_cpu(rom_header->data));
152
153 image_size = le32_to_cpu(rom_data->ilen) * 512;
154 } 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 +0000155
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000156 if (rom_data->type != 0)
157 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000158
159 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000160
Uwe Hermanne4870472010-11-04 23:23:47 +0000161 /*
162 * We check to see if the device thinks it is a VGA device not
163 * whether the ROM image is for a VGA device because some
164 * devices have a mismatch between the hardware and the ROM.
165 */
Elyes HAOUAS0f8b8d92019-01-03 10:23:28 +0100166 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800167#if !CONFIG(MULTIPLE_VGA_ADAPTERS)
Elyes HAOUASe3480662018-05-06 20:32:23 +0200168 extern struct device *vga_pri; /* Primary VGA device (device.c). */
Uwe Hermanne4870472010-11-04 23:23:47 +0000169 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000170#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000171 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200172 printk(BIOS_DEBUG,
173 "Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
174 rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
Uwe Hermanne4870472010-11-04 23:23:47 +0000175 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
176 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000177 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000178 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000179 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000180
Angel Ponsd19cc112021-07-04 11:41:31 +0200181 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x bytes\n",
182 rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000183
184 memcpy(pci_ram_image_start, rom_header, rom_size);
185 pci_ram_image_start += rom_size;
186 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000187}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200188
189/* ACPI */
Julius Wernercd49cce2019-03-05 16:53:33 -0800190#if CONFIG(HAVE_ACPI_TABLES)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600191
192/* VBIOS may be modified after oprom init so use the copy if present. */
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700193static struct rom_header *check_initialized(const struct device *dev)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600194{
195 struct rom_header *run_rom;
196 struct pci_data *rom_data;
197
Nikolai Vyssotskib649d6a2021-03-11 19:15:03 -0600198 if (!CONFIG(VGA_ROM_RUN) && !CONFIG(RUN_FSP_GOP))
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600199 return NULL;
200
201 run_rom = (struct rom_header *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
202 if (read_le16(&run_rom->signature) != PCI_ROM_HDR)
203 return NULL;
204
205 rom_data = (struct pci_data *)((u8 *)run_rom
Jacob Garber7cfe68d2019-07-16 13:14:09 -0600206 + read_le16(&run_rom->data));
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600207
208 if (read_le32(&rom_data->signature) == PCI_DATA_HDR
209 && read_le16(&rom_data->device) == dev->device
210 && read_le16(&rom_data->vendor) == dev->vendor)
211 return run_rom;
212 else
213 return NULL;
214}
215
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200216static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700217pci_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200218 unsigned long current)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200219{
Himanshu Sahdevafd05052019-10-21 11:23:20 +0530220 acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200221 struct rom_header *rom;
222
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600223 rom = check_initialized(device);
224 if (!rom)
225 rom = pci_rom_probe(device);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200226 if (!rom) {
Elyes HAOUAS0c1d6602021-01-16 17:29:29 +0100227 printk(BIOS_ERR, "%s failed\n", __func__);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200228 return current;
229 }
230
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600231 printk(BIOS_DEBUG, " Copying %sVBIOS image from %p\n",
232 rom == (struct rom_header *)
233 (uintptr_t)PCI_VGA_RAM_IMAGE_START ?
234 "initialized " : "",
235 rom);
236
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200237 header->DeviceID = device->device;
238 header->VendorID = device->vendor;
239 header->PCIBus = device->bus->secondary;
240 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
241 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
242 header->ImageLength = rom->size * 512;
243 memcpy((void *)&header->VbiosContent, rom, header->ImageLength);
244
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300245 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
246
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200247 current += header->ImageLength;
248 return current;
249}
250
251unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700252pci_rom_write_acpi_tables(const struct device *device, unsigned long current,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200253 struct acpi_rsdp *rsdp)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200254{
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200255 /* Only handle VGA devices */
256 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
257 return current;
258
259 /* Only handle enabled devices */
260 if (!device->enabled)
261 return current;
262
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200263 /* AMD/ATI uses VFCT */
264 if (device->vendor == PCI_VENDOR_ID_ATI) {
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530265 acpi_vfct_t *vfct;
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300266
Felix Heldd5a11ed2019-06-20 14:35:44 +0200267 current = ALIGN_UP(current, 8);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530268 vfct = (acpi_vfct_t *)current;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200269 acpi_create_vfct(device, vfct, pci_rom_acpi_fill_vfct);
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300270 if (vfct->header.length) {
271 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
272 current += vfct->header.length;
273 acpi_add_table(rsdp, vfct);
274 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200275 }
276
277 return current;
278}
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200279
Furquan Shaikh7536a392020-04-24 21:59:21 -0700280void pci_rom_ssdt(const struct device *device)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200281{
282 static size_t ngfx;
283
Benjamin Doron90341c12020-08-04 06:24:03 +0000284 /* Only handle display devices */
285 if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200286 return;
287
288 /* Only handle enabled devices */
289 if (!device->enabled)
290 return;
291
292 /* Probe for option rom */
293 const struct rom_header *rom = pci_rom_probe(device);
294 if (!rom || !rom->size) {
295 printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n",
296 dev_path(device));
297 return;
298 }
299
300 const char *scope = acpi_device_path(device);
301 if (!scope) {
302 printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device));
303 return;
304 }
305
306 /* Supports up to four devices. */
307 if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) {
308 printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device));
309 return;
310 }
311
312 /* Prepare memory */
313 const size_t cbrom_length = rom->size * 512;
314 if (!cbrom_length) {
315 printk(BIOS_ERR, "%s: ROM has zero length!\n",
316 dev_path(device));
317 return;
318 }
319
320 void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length);
321 if (!cbrom) {
322 printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n",
323 dev_path(device));
324 return;
325 }
326 /* Increment CBMEM id for next device */
327 ngfx++;
328
329 memcpy(cbrom, rom, cbrom_length);
330
331 /* write _ROM method */
332 acpigen_write_scope(scope);
333 acpigen_write_rom(cbrom, cbrom_length);
334 acpigen_pop_len(); /* pop scope */
335}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200336#endif