| 1 |
|
| 2 |
|
| 3 |
#include <acpi/acpigen.h> |
| 4 |
#include <bootmode.h> |
| 5 |
#include <cbfs.h> |
| 6 |
#include <cbmem.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <commonlib/endian.h> |
| 9 |
#include <delay.h> |
| 10 |
#include <device/device.h> |
| 11 |
#include <device/pci.h> |
| 12 |
#include <device/pci_ids.h> |
| 13 |
#include <device/pci_ops.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
#include <timestamp.h> |
| 17 |
|
| 18 |
|
| 19 |
u32 __weak map_oprom_vendev(u32 vendev) { return vendev; } |
| 20 |
|
| 21 |
void vga_oprom_preload(void) |
| 22 |
{ |
| 23 |
|
| 24 |
#if CONFIG(VGA_BIOS) |
| 25 |
const char name[] = "pci" CONFIG_VGA_BIOS_ID ".rom"; |
| 26 |
|
| 27 |
if (!CONFIG(CBFS_PRELOAD)) |
| 28 |
return; |
| 29 |
|
| 30 |
printk(BIOS_DEBUG, "Preloading VGA ROM %s\n", name); |
| 31 |
|
| 32 |
cbfs_preload(name); |
| 33 |
#endif |
| 34 |
} |
| 35 |
|
| 36 |
static void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device) |
| 37 |
{ |
| 38 |
char name[17] = "pciXXXX,XXXX.rom"; |
| 39 |
|
| 40 |
snprintf(name, sizeof(name), "pci%04hx,%04hx.rom", vendor, device); |
| 41 |
|
| 42 |
return cbfs_map(name, NULL); |
| 43 |
} |
| 44 |
|
| 45 |
struct rom_header *pci_rom_probe(const struct device *dev) |
| 46 |
{ |
| 47 |
struct rom_header *rom_header = NULL; |
| 48 |
struct pci_data *rom_data; |
| 49 |
u32 vendev = (dev->vendor << 16) | dev->device; |
| 50 |
u32 mapped_vendev = vendev; |
| 51 |
|
| 52 |
|
| 53 |
mapped_vendev = map_oprom_vendev(vendev); |
| 54 |
rom_header = cbfs_boot_map_optionrom(mapped_vendev >> 16, mapped_vendev & 0xffff); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if (!rom_header && vendev != mapped_vendev) { |
| 59 |
rom_header = cbfs_boot_map_optionrom(vendev >> 16, vendev & 0xffff); |
| 60 |
if (rom_header) { |
| 61 |
printk(BIOS_NOTICE, "VGA_BIOS_ID should be the remapped PCI ID " |
| 62 |
"%04hx,%04hx in the VBIOS file\n", |
| 63 |
mapped_vendev >> 16, mapped_vendev & 0xffff); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (rom_header) { |
| 68 |
printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n", |
| 69 |
dev_path(dev), rom_header); |
| 70 |
} else if (CONFIG(ON_DEVICE_ROM_LOAD)) { |
| 71 |
uintptr_t rom_address; |
| 72 |
|
| 73 |
rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS); |
| 74 |
|
| 75 |
if (rom_address == 0x00000000 || rom_address == 0xffffffff) { |
| 76 |
if (CONFIG(CPU_QEMU_X86) && (dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) |
| 77 |
rom_address = 0xc0000; |
| 78 |
else |
| 79 |
return NULL; |
| 80 |
} else { |
| 81 |
|
| 82 |
pci_write_config32(dev, PCI_ROM_ADDRESS, |
| 83 |
rom_address|PCI_ROM_ADDRESS_ENABLE); |
| 84 |
} |
| 85 |
|
| 86 |
rom_address &= PCI_ROM_ADDRESS_MASK; |
| 87 |
|
| 88 |
printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n", |
| 89 |
dev_path(dev), (unsigned long)rom_address); |
| 90 |
rom_header = (struct rom_header *)rom_address; |
| 91 |
} else { |
| 92 |
printk(BIOS_DEBUG, "PCI Option ROM loading disabled for %s\n", |
| 93 |
dev_path(dev)); |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
printk(BIOS_SPEW, |
| 98 |
"PCI expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n", |
| 99 |
le16_to_cpu(rom_header->signature), |
| 100 |
rom_header->size * 512, le16_to_cpu(rom_header->data)); |
| 101 |
|
| 102 |
if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) { |
| 103 |
printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x\n", |
| 104 |
le16_to_cpu(rom_header->signature)); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data)); |
| 109 |
|
| 110 |
printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n", |
| 111 |
rom_data->vendor, rom_data->device); |
| 112 |
|
| 113 |
if ((dev->vendor != rom_data->vendor |
| 114 |
|| dev->device != rom_data->device) |
| 115 |
&& (vendev == mapped_vendev)) { |
| 116 |
printk(BIOS_ERR, "ID mismatch: vendor ID %04x, device ID %04x\n", |
| 117 |
dev->vendor, dev->device); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, Code Type %02x\n", |
| 122 |
rom_data->class_hi, rom_data->class_lo, |
| 123 |
rom_data->type); |
| 124 |
|
| 125 |
if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) { |
| 126 |
printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n", |
| 127 |
(rom_data->class_hi << 8) | rom_data->class_lo, |
| 128 |
dev->class); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
return rom_header; |
| 133 |
} |
| 134 |
|
| 135 |
static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START; |
| 136 |
|
| 137 |
struct rom_header *pci_rom_load(struct device *dev, |
| 138 |
struct rom_header *rom_header) |
| 139 |
{ |
| 140 |
struct pci_data * rom_data; |
| 141 |
unsigned int rom_size; |
| 142 |
unsigned int image_size=0; |
| 143 |
|
| 144 |
do { |
| 145 |
|
| 146 |
rom_header = (struct rom_header *)((void *)rom_header |
| 147 |
+ image_size); |
| 148 |
|
| 149 |
rom_data = (struct pci_data *)((void *)rom_header |
| 150 |
+ le16_to_cpu(rom_header->data)); |
| 151 |
|
| 152 |
image_size = le16_to_cpu(rom_data->ilen) * 512; |
| 153 |
} while ((rom_data->type != 0) && (rom_data->indicator != 0)); |
| 154 |
|
| 155 |
if (rom_data->type != 0) |
| 156 |
return NULL; |
| 157 |
|
| 158 |
rom_size = rom_header->size * 512; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { |
| 166 |
extern struct device *vga_pri; |
| 167 |
if (dev != vga_pri) return NULL; |
| 168 |
if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) { |
| 169 |
printk(BIOS_DEBUG, |
| 170 |
"Copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n", |
| 171 |
rom_header, PCI_VGA_RAM_IMAGE_START, rom_size); |
| 172 |
memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header, |
| 173 |
rom_size); |
| 174 |
} |
| 175 |
|
| 176 |
dev->pci_vga_option_rom = (struct rom_header *)(PCI_VGA_RAM_IMAGE_START); |
| 177 |
return (struct rom_header *)(PCI_VGA_RAM_IMAGE_START); |
| 178 |
} |
| 179 |
|
| 180 |
printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x bytes\n", |
| 181 |
rom_header, pci_ram_image_start, rom_size); |
| 182 |
|
| 183 |
dev->pci_vga_option_rom = (struct rom_header *)pci_ram_image_start; |
| 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); |
| 187 |
} |
| 188 |
|
| 189 |
static int should_run_oprom(struct device *dev, struct rom_header *rom) |
| 190 |
{ |
| 191 |
static int should_run = -1; |
| 192 |
|
| 193 |
if (dev->upstream->segment_group) { |
| 194 |
printk(BIOS_ERR, "Only option ROMs of devices in first PCI segment group can " |
| 195 |
"be run.\n"); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
if (CONFIG(VENDORCODE_ELTAN_VBOOT)) |
| 200 |
if (rom != NULL) |
| 201 |
if (!verified_boot_should_run_oprom(rom)) |
| 202 |
return 0; |
| 203 |
|
| 204 |
if (should_run >= 0) |
| 205 |
return should_run; |
| 206 |
|
| 207 |
if (CONFIG(ALWAYS_RUN_OPROM)) { |
| 208 |
should_run = 1; |
| 209 |
return should_run; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
should_run = display_init_required(); |
| 216 |
|
| 217 |
if (!should_run) |
| 218 |
printk(BIOS_DEBUG, "Not running VGA Option ROM\n"); |
| 219 |
return should_run; |
| 220 |
} |
| 221 |
|
| 222 |
static int should_load_oprom(struct device *dev) |
| 223 |
{ |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if (!CONFIG(S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() && |
| 228 |
((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)) |
| 229 |
return 0; |
| 230 |
if (CONFIG(ALWAYS_LOAD_OPROM)) |
| 231 |
return 1; |
| 232 |
if (should_run_oprom(dev, NULL)) |
| 233 |
return 1; |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static void oprom_pre_graphics_stall(void) |
| 239 |
{ |
| 240 |
if (CONFIG_PRE_GRAPHICS_DELAY_MS) |
| 241 |
mdelay(CONFIG_PRE_GRAPHICS_DELAY_MS); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
void pci_rom_run(struct device *dev) |
| 246 |
{ |
| 247 |
struct rom_header *rom, *ram; |
| 248 |
|
| 249 |
|
| 250 |
if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)) |
| 251 |
return; |
| 252 |
|
| 253 |
if (!should_load_oprom(dev)) |
| 254 |
return; |
| 255 |
timestamp_add_now(TS_OPROM_INITIALIZE); |
| 256 |
|
| 257 |
rom = pci_rom_probe(dev); |
| 258 |
if (rom == NULL) |
| 259 |
return; |
| 260 |
|
| 261 |
ram = pci_rom_load(dev, rom); |
| 262 |
if (ram == NULL) |
| 263 |
return; |
| 264 |
timestamp_add_now(TS_OPROM_COPY_END); |
| 265 |
|
| 266 |
if (!should_run_oprom(dev, rom)) |
| 267 |
return; |
| 268 |
|
| 269 |
|
| 270 |
oprom_pre_graphics_stall(); |
| 271 |
|
| 272 |
run_bios(dev, (unsigned long)ram); |
| 273 |
|
| 274 |
gfx_set_init_done(1); |
| 275 |
printk(BIOS_DEBUG, "VGA Option ROM was run\n"); |
| 276 |
timestamp_add_now(TS_OPROM_END); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 281 |
|
| 282 |
static unsigned long |
| 283 |
ati_rom_acpi_fill_vfct(const struct device *device, acpi_vfct_t *vfct_struct, |
| 284 |
unsigned long current) |
| 285 |
{ |
| 286 |
acpi_vfct_image_hdr_t *header = &vfct_struct->image_hdr; |
| 287 |
struct rom_header *rom; |
| 288 |
|
| 289 |
|
| 290 |
rom = device->pci_vga_option_rom; |
| 291 |
if (!rom) |
| 292 |
rom = pci_rom_probe(device); |
| 293 |
if (!rom) { |
| 294 |
printk(BIOS_ERR, "%s failed\n", __func__); |
| 295 |
return current; |
| 296 |
} |
| 297 |
if (device->upstream->segment_group) { |
| 298 |
printk(BIOS_ERR, "VFCT only supports GPU in first PCI segment group.\n"); |
| 299 |
return current; |
| 300 |
} |
| 301 |
|
| 302 |
printk(BIOS_DEBUG, " Copying VBIOS image from %p\n", rom); |
| 303 |
|
| 304 |
header->DeviceID = device->device; |
| 305 |
header->VendorID = device->vendor; |
| 306 |
header->PCIBus = device->upstream->secondary; |
| 307 |
header->PCIFunction = PCI_FUNC(device->path.pci.devfn); |
| 308 |
header->PCIDevice = PCI_SLOT(device->path.pci.devfn); |
| 309 |
header->ImageLength = rom->size * 512; |
| 310 |
memcpy((void *)header->VbiosContent, rom, header->ImageLength); |
| 311 |
|
| 312 |
vfct_struct->VBIOSImageOffset = (size_t)header - (size_t)vfct_struct; |
| 313 |
|
| 314 |
current += header->ImageLength; |
| 315 |
return current; |
| 316 |
} |
| 317 |
|
| 318 |
unsigned long |
| 319 |
pci_rom_write_acpi_tables(const struct device *device, unsigned long current, |
| 320 |
struct acpi_rsdp *rsdp) |
| 321 |
{ |
| 322 |
|
| 323 |
if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY) |
| 324 |
return current; |
| 325 |
|
| 326 |
|
| 327 |
if (!device->enabled) |
| 328 |
return current; |
| 329 |
|
| 330 |
|
| 331 |
if (device->vendor == PCI_VID_ATI) { |
| 332 |
acpi_vfct_t *vfct; |
| 333 |
|
| 334 |
current = ALIGN_UP(current, 8); |
| 335 |
vfct = (acpi_vfct_t *)current; |
| 336 |
acpi_create_vfct(device, vfct, ati_rom_acpi_fill_vfct); |
| 337 |
if (vfct->header.length) { |
| 338 |
printk(BIOS_DEBUG, "ACPI: * VFCT at %lx\n", current); |
| 339 |
current += vfct->header.length; |
| 340 |
acpi_add_table(rsdp, vfct); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return current; |
| 345 |
} |
| 346 |
|
| 347 |
void pci_rom_ssdt(const struct device *device) |
| 348 |
{ |
| 349 |
const struct rom_header *rom; |
| 350 |
static size_t ngfx; |
| 351 |
|
| 352 |
|
| 353 |
if ((device->class >> 16) != PCI_BASE_CLASS_DISPLAY) |
| 354 |
return; |
| 355 |
|
| 356 |
|
| 357 |
if (!device->enabled) |
| 358 |
return; |
| 359 |
|
| 360 |
|
| 361 |
rom = device->pci_vga_option_rom; |
| 362 |
if (!rom) { |
| 363 |
|
| 364 |
rom = pci_rom_probe(device); |
| 365 |
if (!rom || !rom->size) { |
| 366 |
printk(BIOS_WARNING, "%s: Missing PCI Option ROM\n", |
| 367 |
dev_path(device)); |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
if ((CBMEM_ID_ROM0 + ngfx) > CBMEM_ID_ROM3) { |
| 373 |
printk(BIOS_ERR, "%s: Out of CBMEM IDs.\n", dev_path(device)); |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
const size_t cbrom_length = rom->size * 512; |
| 379 |
if (!cbrom_length) { |
| 380 |
printk(BIOS_ERR, "%s: ROM has zero length!\n", |
| 381 |
dev_path(device)); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
void *cbrom = cbmem_add(CBMEM_ID_ROM0 + ngfx, cbrom_length); |
| 386 |
if (!cbrom) { |
| 387 |
printk(BIOS_ERR, "%s: Failed to allocate CBMEM.\n", |
| 388 |
dev_path(device)); |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
ngfx++; |
| 393 |
|
| 394 |
memcpy(cbrom, rom, cbrom_length); |
| 395 |
|
| 396 |
rom = cbrom; |
| 397 |
} |
| 398 |
|
| 399 |
const char *scope = acpi_device_path(device); |
| 400 |
if (!scope) { |
| 401 |
printk(BIOS_ERR, "%s: Missing ACPI scope\n", dev_path(device)); |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
acpigen_write_scope(scope); |
| 407 |
acpigen_write_rom((void *)rom, rom->size * 512); |
| 408 |
acpigen_pop_len(); |
| 409 |
} |
| 410 |
#endif |