blob: 1e212ab21686b3953b3592ba3d0c4c37b164d021 [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 Rothdafcc7a2020-02-05 16:46:30 -070063 map_oprom_vendev_rev(&mapped_vendev, &mapped_rev);
Felix Held42f03962023-03-08 15:38:01 +010064 rom_header = cbfs_boot_map_optionrom_revision(mapped_vendev >> 16,
65 mapped_vendev & 0xffff,
66 mapped_rev);
Martin Rothdafcc7a2020-02-05 16:46:30 -070067 } else {
Martin Rothdafcc7a2020-02-05 16:46:30 -070068 mapped_vendev = map_oprom_vendev(vendev);
Felix Held42f03962023-03-08 15:38:01 +010069 rom_header = cbfs_boot_map_optionrom(mapped_vendev >> 16,
70 mapped_vendev & 0xffff);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080071 }
72
Myles Watsond27c08c2009-11-06 23:42:26 +000073 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000074 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
75 dev_path(dev), rom_header);
Raul E Rangelad5307e2021-07-16 15:06:56 -060076 } else if (CONFIG(ON_DEVICE_ROM_LOAD)) {
Stefan Reinauer83fa1692015-06-18 22:01:07 -070077 uintptr_t rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000078
arch import user (historical)23053642005-07-06 16:49:59 +000079 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000080
81 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Raul E Rangel69cb69f2021-07-16 15:04:18 -060082 if (CONFIG(CPU_QEMU_X86) && (dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
Myles Watsonae3e9982009-11-17 15:20:22 +000083 rom_address = 0xc0000;
84 else
Myles Watsonae3e9982009-11-17 15:20:22 +000085 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000086 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000087 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000088 pci_write_config32(dev, PCI_ROM_ADDRESS,
89 rom_address|PCI_ROM_ADDRESS_ENABLE);
90 }
91
Kyösti Mälkki8c9be432019-06-29 22:34:07 +030092 rom_address &= PCI_ROM_ADDRESS_MASK;
93
Stefan Reinauerafaa2572011-10-06 16:47:51 -070094 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000095 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000096 rom_header = (struct rom_header *)rom_address;
Raul E Rangelad5307e2021-07-16 15:06:56 -060097 } else {
98 printk(BIOS_DEBUG, "PCI Option ROM loading disabled for %s\n",
99 dev_path(dev));
100 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000101 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000102
Angel Ponsd19cc112021-07-04 11:41:31 +0200103 printk(BIOS_SPEW,
104 "PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000105 le32_to_cpu(rom_header->signature),
106 rom_header->size * 512, le32_to_cpu(rom_header->data));
107
Li-Ta Lo883b8792005-01-10 23:16:22 +0000108 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200109 printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x\n",
110 le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +0000111 return NULL;
112 }
113
Myles Watsond27c08c2009-11-06 23:42:26 +0000114 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
115
Uwe Hermanne4870472010-11-04 23:23:47 +0000116 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
117 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -0800118 /* If the device id is mapped, a mismatch is expected */
119 if ((dev->vendor != rom_data->vendor
120 || dev->device != rom_data->device)
121 && (vendev == mapped_vendev)) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200122 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, device ID %04x\n",
123 dev->vendor, dev->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000124 return NULL;
125 }
126
Angel Ponsd19cc112021-07-04 11:41:31 +0200127 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
128 rom_data->class_hi, rom_data->class_lo,
Uwe Hermanne4870472010-11-04 23:23:47 +0000129 rom_data->type);
130
Yinghai Lu54ab3112005-01-18 03:10:46 +0000131 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000132 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000133 (rom_data->class_hi << 8) | rom_data->class_lo,
134 dev->class);
135 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000136 }
137
138 return rom_header;
139}
140
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000141static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000142
Uwe Hermanne4870472010-11-04 23:23:47 +0000143struct rom_header *pci_rom_load(struct device *dev,
144 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000145{
146 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000147 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000148 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000149
arch import user (historical)23053642005-07-06 16:49:59 +0000150 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000151 /* Get next image. */
Elyes Haouasd369c662022-11-18 15:06:21 +0100152 rom_header = (struct rom_header *)((void *)rom_header
Uwe Hermanne4870472010-11-04 23:23:47 +0000153 + image_size);
154
Elyes Haouasd369c662022-11-18 15:06:21 +0100155 rom_data = (struct pci_data *)((void *)rom_header
Uwe Hermanne4870472010-11-04 23:23:47 +0000156 + le32_to_cpu(rom_header->data));
157
158 image_size = le32_to_cpu(rom_data->ilen) * 512;
159 } 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 +0000160
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000161 if (rom_data->type != 0)
162 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000163
164 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000165
Uwe Hermanne4870472010-11-04 23:23:47 +0000166 /*
167 * We check to see if the device thinks it is a VGA device not
168 * whether the ROM image is for a VGA device because some
169 * devices have a mismatch between the hardware and the ROM.
170 */
Elyes HAOUAS0f8b8d92019-01-03 10:23:28 +0100171 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
Elyes HAOUASe3480662018-05-06 20:32:23 +0200172 extern struct device *vga_pri; /* Primary VGA device (device.c). */
Uwe Hermanne4870472010-11-04 23:23:47 +0000173 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000174 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Angel Ponsd19cc112021-07-04 11:41:31 +0200175 printk(BIOS_DEBUG,
176 "Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
177 rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
Uwe Hermanne4870472010-11-04 23:23:47 +0000178 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
179 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000180 }
Elyes Haouasd369c662022-11-18 15:06:21 +0100181 return (struct rom_header *)(PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000182 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000183
Angel Ponsd19cc112021-07-04 11:41:31 +0200184 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x bytes\n",
185 rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000186
187 memcpy(pci_ram_image_start, rom_header, rom_size);
188 pci_ram_image_start += rom_size;
Elyes Haouasd369c662022-11-18 15:06:21 +0100189 return (struct rom_header *)(pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000190}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200191
192/* ACPI */
Julius Wernercd49cce2019-03-05 16:53:33 -0800193#if CONFIG(HAVE_ACPI_TABLES)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600194
195/* VBIOS may be modified after oprom init so use the copy if present. */
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700196static struct rom_header *check_initialized(const struct device *dev)
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600197{
198 struct rom_header *run_rom;
199 struct pci_data *rom_data;
200
Nikolai Vyssotskib649d6a2021-03-11 19:15:03 -0600201 if (!CONFIG(VGA_ROM_RUN) && !CONFIG(RUN_FSP_GOP))
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600202 return NULL;
203
204 run_rom = (struct rom_header *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
205 if (read_le16(&run_rom->signature) != PCI_ROM_HDR)
206 return NULL;
207
208 rom_data = (struct pci_data *)((u8 *)run_rom
Jacob Garber7cfe68d2019-07-16 13:14:09 -0600209 + read_le16(&run_rom->data));
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600210
211 if (read_le32(&rom_data->signature) == PCI_DATA_HDR
212 && read_le16(&rom_data->device) == dev->device
213 && read_le16(&rom_data->vendor) == dev->vendor)
214 return run_rom;
215 else
216 return NULL;
217}
218
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200219static unsigned long
Matt DeVillier0cd2a502023-09-04 08:32:39 -0500220ati_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200221 unsigned long current)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200222{
Himanshu Sahdevafd05052019-10-21 11:23:20 +0530223 acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200224 struct rom_header *rom;
225
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600226 rom = check_initialized(device);
227 if (!rom)
228 rom = pci_rom_probe(device);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200229 if (!rom) {
Elyes HAOUAS0c1d6602021-01-16 17:29:29 +0100230 printk(BIOS_ERR, "%s failed\n", __func__);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200231 return current;
232 }
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200233 if (device->upstream->segment_group) {
Felix Held3b5b66d2024-01-11 22:26:18 +0100234 printk(BIOS_ERR, "VFCT only supports GPU in first PCI segment group.\n");
235 return current;
236 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200237
Marshall Dawson6f978cf2017-04-19 18:31:07 -0600238 printk(BIOS_DEBUG, " Copying %sVBIOS image from %p\n",
239 rom == (struct rom_header *)
240 (uintptr_t)PCI_VGA_RAM_IMAGE_START ?
241 "initialized " : "",
242 rom);
243
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200244 header->DeviceID = device->device;
245 header->VendorID = device->vendor;
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200246 header->PCIBus = device->upstream->secondary;
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200247 header->PCIFunction = PCI_FUNC(device->path.pci.devfn);
248 header->PCIDevice = PCI_SLOT(device->path.pci.devfn);
249 header->ImageLength = rom->size * 512;
Matt DeVillier7c04d0e2023-09-03 12:51:58 -0500250 memcpy((void *)header->VbiosContent, rom, header->ImageLength);
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200251
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300252 vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct;
253
Matt DeVillier7c04d0e2023-09-03 12:51:58 -0500254 /* Calculate and set checksum for VBIOS data if FSP GOP driver used,
255 Since GOP driver modifies ATOMBIOS tables at end of VBIOS */
256 if (CONFIG(RUN_FSP_GOP)) {
257 /* Clear existing checksum before recalculating */
258 header->VbiosContent[VFCT_VBIOS_CHECKSUM_OFFSET] = 0;
259 header->VbiosContent[VFCT_VBIOS_CHECKSUM_OFFSET] =
260 acpi_checksum(header->VbiosContent, header->ImageLength);
261 }
262
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200263 current += header->ImageLength;
264 return current;
265}
266
267unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700268pci_rom_write_acpi_tables(const struct device *device, unsigned long current,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200269 struct acpi_rsdp *rsdp)
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200270{
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200271 /* Only handle VGA devices */
272 if ((device->class >> 8) != PCI_CLASS_DISPLAY_VGA)
273 return current;
274
275 /* Only handle enabled devices */
276 if (!device->enabled)
277 return current;
278
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200279 /* AMD/ATI uses VFCT */
Felix Singer43b7f412022-03-07 04:34:52 +0100280 if (device->vendor == PCI_VID_ATI) {
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530281 acpi_vfct_t *vfct;
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300282
Felix Heldd5a11ed2019-06-20 14:35:44 +0200283 current = ALIGN_UP(current, 8);
Himanshu Sahdev7f1da072019-10-21 15:27:19 +0530284 vfct = (acpi_vfct_t *)current;
Matt DeVillier0cd2a502023-09-04 08:32:39 -0500285 acpi_create_vfct(device, vfct, ati_rom_acpi_fill_vfct);
Kyösti Mälkkifb493792019-06-30 06:29:52 +0300286 if (vfct->header.length) {
287 printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current);
288 current += vfct->header.length;
289 acpi_add_table(rsdp, vfct);
290 }
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200291 }
292
293 return current;
294}
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200295
Furquan Shaikh7536a392020-04-24 21:59:21 -0700296void pci_rom_ssdt(const struct device *device)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200297{
298 static size_t ngfx;
299
Benjamin Doron90341c12020-08-04 06:24:03 +0000300 /* Only handle display devices */
301 if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY)
Patrick Rudolph00c0cd22017-06-06 19:30:55 +0200302 return;
303
304 /* Only handle enabled devices */
305 if (!device->enabled)
306 return;
307
308 /* Probe for option rom */
309 const struct rom_header *rom = pci_rom_probe(device);
310 if (!rom || !rom->size) {
311 printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n",
312 dev_path(device));
313 return;
314 }
315
316 const char *scope = acpi_device_path(device);
317 if (!scope) {
318 printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device));
319 return;
320 }
321
322 /* Supports up to four devices. */
323 if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) {
324 printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device));
325 return;
326 }
327
328 /* Prepare memory */
329 const size_t cbrom_length = rom->size * 512;
330 if (!cbrom_length) {
331 printk(BIOS_ERR, "%s: ROM has zero length!\n",
332 dev_path(device));
333 return;
334 }
335
336 void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length);
337 if (!cbrom) {
338 printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n",
339 dev_path(device));
340 return;
341 }
342 /* Increment CBMEM id for next device */
343 ngfx++;
344
345 memcpy(cbrom, rom, cbrom_length);
346
347 /* write _ROM method */
348 acpigen_write_scope(scope);
349 acpigen_write_rom(cbrom, cbrom_length);
350 acpigen_pop_len(); /* pop scope */
351}
Patrick Rudolpha5c2ac62016-03-31 20:04:23 +0200352#endif