1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <commonlib/endian.h>
5 #include <device/device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
13 #include <acpi/acpigen.h>
15 /* Rmodules don't like weak symbols. */
16 void __weak
map_oprom_vendev_rev(u32
*vendev
, u8
*rev
) { return; }
17 u32 __weak
map_oprom_vendev(u32 vendev
) { return vendev
; }
19 void vga_oprom_preload(void)
21 /* The CONFIG_VGA_BIOS_ID symbol is only defined when VGA_BIOS is selected */
23 const char name
[] = "pci" CONFIG_VGA_BIOS_ID
".rom";
25 if (!CONFIG(CBFS_PRELOAD
))
28 printk(BIOS_DEBUG
, "Preloading VGA ROM %s\n", name
);
34 static void *cbfs_boot_map_optionrom(uint16_t vendor
, uint16_t device
)
36 char name
[17] = "pciXXXX,XXXX.rom";
38 snprintf(name
, sizeof(name
), "pci%04hx,%04hx.rom", vendor
, device
);
40 return cbfs_map(name
, NULL
);
43 static void *cbfs_boot_map_optionrom_revision(uint16_t vendor
, uint16_t device
, uint8_t rev
)
45 char name
[20] = "pciXXXX,XXXX,XX.rom";
47 snprintf(name
, sizeof(name
), "pci%04hx,%04hx,%02hhx.rom", vendor
, device
, rev
);
49 return cbfs_map(name
, NULL
);
52 struct rom_header
*pci_rom_probe(const struct device
*dev
)
54 struct rom_header
*rom_header
= NULL
;
55 struct pci_data
*rom_data
;
56 u8 rev
= pci_read_config8(dev
, PCI_REVISION_ID
);
58 u32 vendev
= (dev
->vendor
<< 16) | dev
->device
;
59 u32 mapped_vendev
= vendev
;
61 /* If the ROM is in flash, then don't check the PCI device for it. */
62 if (CONFIG(CHECK_REV_IN_OPROM_NAME
)) {
63 rom_header
= cbfs_boot_map_optionrom_revision(dev
->vendor
, dev
->device
, rev
);
64 map_oprom_vendev_rev(&mapped_vendev
, &mapped_rev
);
66 rom_header
= cbfs_boot_map_optionrom(dev
->vendor
, dev
->device
);
67 mapped_vendev
= map_oprom_vendev(vendev
);
71 if (CONFIG(CHECK_REV_IN_OPROM_NAME
) &&
72 (vendev
!= mapped_vendev
|| rev
!= mapped_rev
)) {
73 rom_header
= cbfs_boot_map_optionrom_revision(
75 mapped_vendev
& 0xffff, mapped_rev
);
76 } else if (vendev
!= mapped_vendev
) {
77 rom_header
= cbfs_boot_map_optionrom(
79 mapped_vendev
& 0xffff);
84 printk(BIOS_DEBUG
, "In CBFS, ROM address for %s = %p\n",
85 dev_path(dev
), rom_header
);
86 } else if (CONFIG(ON_DEVICE_ROM_LOAD
)) {
87 uintptr_t rom_address
;
89 rom_address
= pci_read_config32(dev
, PCI_ROM_ADDRESS
);
91 if (rom_address
== 0x00000000 || rom_address
== 0xffffffff) {
92 if (CONFIG(CPU_QEMU_X86
) && (dev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
)
93 rom_address
= 0xc0000;
97 /* Enable expansion ROM address decoding. */
98 pci_write_config32(dev
, PCI_ROM_ADDRESS
,
99 rom_address
|PCI_ROM_ADDRESS_ENABLE
);
102 rom_address
&= PCI_ROM_ADDRESS_MASK
;
104 printk(BIOS_DEBUG
, "Option ROM address for %s = %lx\n",
105 dev_path(dev
), (unsigned long)rom_address
);
106 rom_header
= (struct rom_header
*)rom_address
;
108 printk(BIOS_DEBUG
, "PCI Option ROM loading disabled for %s\n",
114 "PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
115 le32_to_cpu(rom_header
->signature
),
116 rom_header
->size
* 512, le32_to_cpu(rom_header
->data
));
118 if (le32_to_cpu(rom_header
->signature
) != PCI_ROM_HDR
) {
119 printk(BIOS_ERR
, "Incorrect expansion ROM header signature %04x\n",
120 le32_to_cpu(rom_header
->signature
));
124 rom_data
= (((void *)rom_header
) + le32_to_cpu(rom_header
->data
));
126 printk(BIOS_SPEW
, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
127 rom_data
->vendor
, rom_data
->device
);
128 /* 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
)) {
132 printk(BIOS_ERR
, "ID mismatch: vendor ID %04x, device ID %04x\n",
133 dev
->vendor
, dev
->device
);
137 printk(BIOS_SPEW
, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
138 rom_data
->class_hi
, rom_data
->class_lo
,
141 if (dev
->class != ((rom_data
->class_hi
<< 8) | rom_data
->class_lo
)) {
142 printk(BIOS_DEBUG
, "Class Code mismatch ROM %08x, dev %08x\n",
143 (rom_data
->class_hi
<< 8) | rom_data
->class_lo
,
151 static void *pci_ram_image_start
= (void *)PCI_RAM_IMAGE_START
;
153 struct rom_header
*pci_rom_load(struct device
*dev
,
154 struct rom_header
*rom_header
)
156 struct pci_data
* rom_data
;
157 unsigned int rom_size
;
158 unsigned int image_size
=0;
161 /* Get next image. */
162 rom_header
= (struct rom_header
*)((void *) rom_header
165 rom_data
= (struct pci_data
*)((void *) rom_header
166 + le32_to_cpu(rom_header
->data
));
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
171 if (rom_data
->type
!= 0)
174 rom_size
= rom_header
->size
* 512;
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.
181 if ((dev
->class >> 8) == PCI_CLASS_DISPLAY_VGA
) {
182 #if !CONFIG(MULTIPLE_VGA_ADAPTERS)
183 extern struct device
*vga_pri
; /* Primary VGA device (device.c). */
184 if (dev
!= vga_pri
) return NULL
; /* Only one VGA supported. */
186 if ((void *)PCI_VGA_RAM_IMAGE_START
!= rom_header
) {
188 "Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
189 rom_header
, PCI_VGA_RAM_IMAGE_START
, rom_size
);
190 memcpy((void *)PCI_VGA_RAM_IMAGE_START
, rom_header
,
193 return (struct rom_header
*) (PCI_VGA_RAM_IMAGE_START
);
196 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
);
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
);
205 #if CONFIG(HAVE_ACPI_TABLES)
207 /* VBIOS may be modified after oprom init so use the copy if present. */
208 static struct rom_header
*check_initialized(const struct device
*dev
)
210 struct rom_header
*run_rom
;
211 struct pci_data
*rom_data
;
213 if (!CONFIG(VGA_ROM_RUN
) && !CONFIG(RUN_FSP_GOP
))
216 run_rom
= (struct rom_header
*)(uintptr_t)PCI_VGA_RAM_IMAGE_START
;
217 if (read_le16(&run_rom
->signature
) != PCI_ROM_HDR
)
220 rom_data
= (struct pci_data
*)((u8
*)run_rom
221 + read_le16(&run_rom
->data
));
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
)
232 pci_rom_acpi_fill_vfct(const struct device
*device
, acpi_vfct_t
*vfct_struct
,
233 unsigned long current
)
235 acpi_vfct_image_hdr_t
*header
= &vfct_struct
->image_hdr
;
236 struct rom_header
*rom
;
238 rom
= check_initialized(device
);
240 rom
= pci_rom_probe(device
);
242 printk(BIOS_ERR
, "%s failed\n", __func__
);
246 printk(BIOS_DEBUG
, " Copying %sVBIOS image from %p\n",
247 rom
== (struct rom_header
*)
248 (uintptr_t)PCI_VGA_RAM_IMAGE_START
?
252 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
);
260 vfct_struct
->VBIOSImageOffset
= (size_t)header
- (size_t)vfct_struct
;
262 current
+= header
->ImageLength
;
267 pci_rom_write_acpi_tables(const struct device
*device
, unsigned long current
,
268 struct acpi_rsdp
*rsdp
)
270 /* Only handle VGA devices */
271 if ((device
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
274 /* Only handle enabled devices */
275 if (!device
->enabled
)
278 /* AMD/ATI uses VFCT */
279 if (device
->vendor
== PCI_VID_ATI
) {
282 current
= ALIGN_UP(current
, 8);
283 vfct
= (acpi_vfct_t
*)current
;
284 acpi_create_vfct(device
, vfct
, pci_rom_acpi_fill_vfct
);
285 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
);
295 void pci_rom_ssdt(const struct device
*device
)
299 /* Only handle display devices */
300 if ((device
->class >> 16) != PCI_BASE_CLASS_DISPLAY
)
303 /* Only handle enabled devices */
304 if (!device
->enabled
)
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",
315 const char *scope
= acpi_device_path(device
);
317 printk(BIOS_ERR
, "%s: Missing ACPI scope\n", dev_path(device
));
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
));
328 const size_t cbrom_length
= rom
->size
* 512;
330 printk(BIOS_ERR
, "%s: ROM has zero length!\n",
335 void *cbrom
= cbmem_add(CBMEM_ID_ROM0
+ ngfx
, cbrom_length
);
337 printk(BIOS_ERR
, "%s: Failed to allocate CBMEM.\n",
341 /* Increment CBMEM id for next device */
344 memcpy(cbrom
, rom
, cbrom_length
);
346 /* write _ROM method */
347 acpigen_write_scope(scope
);
348 acpigen_write_rom(cbrom
, cbrom_length
);
349 acpigen_pop_len(); /* pop scope */