| 1 |
|
| 2 |
|
| 3 |
#include <acpi/acpi.h> |
| 4 |
#include <types.h> |
| 5 |
#include <string.h> |
| 6 |
#include <cbfs.h> |
| 7 |
#include <device/device.h> |
| 8 |
#include <device/pci.h> |
| 9 |
#include <device/pci_ids.h> |
| 10 |
#include <device/pci_ops.h> |
| 11 |
#include <console/console.h> |
| 12 |
#include <cbmem.h> |
| 13 |
#include "intel_bios.h" |
| 14 |
#include "opregion.h" |
| 15 |
|
| 16 |
__weak |
| 17 |
const char *mainboard_vbt_filename(void) |
| 18 |
{ |
| 19 |
return "vbt.bin"; |
| 20 |
} |
| 21 |
|
| 22 |
void *locate_vbt(size_t *vbt_size) |
| 23 |
{ |
| 24 |
static void *data; |
| 25 |
static size_t size; |
| 26 |
|
| 27 |
if (data) |
| 28 |
goto out; |
| 29 |
|
| 30 |
data = cbfs_map(mainboard_vbt_filename(), &size); |
| 31 |
if (!data || size == 0) { |
| 32 |
printk(BIOS_ERR, "Could not find or load %s CBFS file\n", |
| 33 |
mainboard_vbt_filename()); |
| 34 |
goto err; |
| 35 |
} |
| 36 |
|
| 37 |
if (*(uint32_t *)data == VBT_SIGNATURE) { |
| 38 |
printk(BIOS_INFO, "Found a VBT of %zu bytes\n", size); |
| 39 |
goto out; |
| 40 |
} |
| 41 |
|
| 42 |
printk(BIOS_ERR, "Missing/invalid signature in VBT data file!\n"); |
| 43 |
|
| 44 |
err: |
| 45 |
if (data) { |
| 46 |
cbfs_unmap(data); |
| 47 |
data = NULL; |
| 48 |
} |
| 49 |
size = 0; |
| 50 |
|
| 51 |
out: |
| 52 |
if (vbt_size) |
| 53 |
*vbt_size = size; |
| 54 |
return data; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
static void intel_gma_opregion_register(uintptr_t opregion) |
| 59 |
{ |
| 60 |
struct device *igd; |
| 61 |
u16 reg16; |
| 62 |
u16 sci_reg; |
| 63 |
|
| 64 |
igd = pcidev_on_root(0x2, 0); |
| 65 |
if (!igd || !igd->enabled) |
| 66 |
return; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pci_write_config32(igd, ASLS, opregion); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if (CONFIG(INTEL_GMA_SWSMISCI)) |
| 79 |
sci_reg = SWSMISCI; |
| 80 |
else |
| 81 |
sci_reg = SWSCI; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
reg16 = pci_read_config16(igd, sci_reg); |
| 89 |
reg16 &= ~GSSCIE; |
| 90 |
reg16 |= SMISCISEL; |
| 91 |
pci_write_config16(igd, sci_reg, reg16); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static enum cb_err intel_gma_restore_opregion(void) |
| 96 |
{ |
| 97 |
const igd_opregion_t *const opregion = cbmem_find(CBMEM_ID_IGD_OPREGION); |
| 98 |
if (!opregion) { |
| 99 |
printk(BIOS_ERR, "GMA: Failed to find IGD OpRegion.\n"); |
| 100 |
return CB_ERR; |
| 101 |
} |
| 102 |
|
| 103 |
intel_gma_opregion_register((uintptr_t)opregion); |
| 104 |
return CB_SUCCESS; |
| 105 |
} |
| 106 |
|
| 107 |
static enum cb_err vbt_validate(struct region_device *rdev) |
| 108 |
{ |
| 109 |
uint32_t sig; |
| 110 |
|
| 111 |
if (rdev_readat(rdev, &sig, 0, sizeof(sig)) != sizeof(sig)) |
| 112 |
return CB_ERR; |
| 113 |
|
| 114 |
if (sig != VBT_SIGNATURE) |
| 115 |
return CB_ERR; |
| 116 |
|
| 117 |
return CB_SUCCESS; |
| 118 |
} |
| 119 |
|
| 120 |
static enum cb_err locate_vbt_vbios(const u8 *vbios, struct region_device *rdev) |
| 121 |
{ |
| 122 |
const optionrom_header_t *oprom; |
| 123 |
const optionrom_pcir_t *pcir; |
| 124 |
struct region_device rd; |
| 125 |
enum cb_err ret; |
| 126 |
u8 opromsize; |
| 127 |
size_t offset; |
| 128 |
|
| 129 |
|
| 130 |
if (rdev_chain_mem(&rd, vbios, sizeof(*oprom))) |
| 131 |
return CB_ERR; |
| 132 |
|
| 133 |
if (rdev_readat(&rd, &opromsize, offsetof(optionrom_header_t, size), |
| 134 |
sizeof(opromsize)) != sizeof(opromsize) || !opromsize) |
| 135 |
return CB_ERR; |
| 136 |
|
| 137 |
if (rdev_chain_mem(&rd, vbios, opromsize * 512)) |
| 138 |
return CB_ERR; |
| 139 |
|
| 140 |
oprom = rdev_mmap(&rd, 0, sizeof(*oprom)); |
| 141 |
if (!oprom) |
| 142 |
return CB_ERR; |
| 143 |
|
| 144 |
if (!oprom->pcir_offset || !oprom->vbt_offset) { |
| 145 |
rdev_munmap(&rd, (void *)oprom); |
| 146 |
return CB_ERR; |
| 147 |
} |
| 148 |
|
| 149 |
pcir = rdev_mmap(&rd, oprom->pcir_offset, sizeof(*pcir)); |
| 150 |
if (pcir == NULL) { |
| 151 |
rdev_munmap(&rd, (void *)oprom); |
| 152 |
return CB_ERR; |
| 153 |
} |
| 154 |
|
| 155 |
printk(BIOS_DEBUG, "GMA: %s: %x %x %x %x %x\n", __func__, |
| 156 |
oprom->signature, pcir->vendor, pcir->classcode[0], |
| 157 |
pcir->classcode[1], pcir->classcode[2]); |
| 158 |
|
| 159 |
|
| 160 |
if ((oprom->signature != OPROM_SIGNATURE) || |
| 161 |
(pcir->vendor != PCI_VID_INTEL) || |
| 162 |
(pcir->signature != 0x52494350) || |
| 163 |
(pcir->classcode[0] != 0x00) || |
| 164 |
(pcir->classcode[1] != 0x00) || |
| 165 |
(pcir->classcode[2] != 0x03)) { |
| 166 |
rdev_munmap(&rd, (void *)oprom); |
| 167 |
rdev_munmap(&rd, (void *)pcir); |
| 168 |
return CB_ERR; |
| 169 |
} |
| 170 |
|
| 171 |
rdev_munmap(&rd, (void *)pcir); |
| 172 |
|
| 173 |
|
| 174 |
offset = oprom->vbt_offset; |
| 175 |
do { |
| 176 |
ret = rdev_chain(rdev, &rd, offset, |
| 177 |
(opromsize * 512) - offset); |
| 178 |
offset++; |
| 179 |
} while (ret == CB_SUCCESS && vbt_validate(rdev) != CB_SUCCESS); |
| 180 |
|
| 181 |
offset--; |
| 182 |
|
| 183 |
if (ret == CB_SUCCESS && offset != oprom->vbt_offset) |
| 184 |
printk(BIOS_WARNING, "GMA: Buggy VBIOS found\n"); |
| 185 |
else if (ret != CB_SUCCESS) |
| 186 |
printk(BIOS_ERR, "GMA: Broken VBIOS found\n"); |
| 187 |
|
| 188 |
rdev_munmap(&rd, (void *)oprom); |
| 189 |
return ret; |
| 190 |
} |
| 191 |
|
| 192 |
static enum cb_err locate_vbt_cbfs(struct region_device *rdev) |
| 193 |
{ |
| 194 |
size_t vbt_data_size; |
| 195 |
void *vbt = locate_vbt(&vbt_data_size); |
| 196 |
|
| 197 |
if (vbt == NULL) |
| 198 |
return CB_ERR; |
| 199 |
|
| 200 |
if (rdev_chain_mem(rdev, vbt, vbt_data_size)) |
| 201 |
return CB_ERR; |
| 202 |
|
| 203 |
printk(BIOS_INFO, "GMA: Found VBT in CBFS\n"); |
| 204 |
|
| 205 |
return CB_SUCCESS; |
| 206 |
} |
| 207 |
|
| 208 |
static enum cb_err locate_vbt_vbios_cbfs(struct region_device *rdev) |
| 209 |
{ |
| 210 |
const u8 *oprom = |
| 211 |
(const u8 *)pci_rom_probe(pcidev_on_root(0x2, 0)); |
| 212 |
if (oprom == NULL) |
| 213 |
return CB_ERR; |
| 214 |
|
| 215 |
printk(BIOS_INFO, "GMA: Found VBIOS in CBFS\n"); |
| 216 |
|
| 217 |
return locate_vbt_vbios(oprom, rdev); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static enum cb_err find_vbt_location(struct region_device *rdev) |
| 228 |
{ |
| 229 |
|
| 230 |
if (locate_vbt_cbfs(rdev) == CB_SUCCESS && |
| 231 |
vbt_validate(rdev) == CB_SUCCESS) { |
| 232 |
printk(BIOS_INFO, "GMA: Found valid VBT in CBFS\n"); |
| 233 |
return CB_SUCCESS; |
| 234 |
} |
| 235 |
|
| 236 |
else if (locate_vbt_vbios_cbfs(rdev) == CB_SUCCESS && |
| 237 |
vbt_validate(rdev) == CB_SUCCESS) { |
| 238 |
printk(BIOS_INFO, "GMA: Found valid VBT in VBIOS\n"); |
| 239 |
return CB_SUCCESS; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
else if (locate_vbt_vbios((u8 *)0xc0000, rdev) == CB_SUCCESS && |
| 248 |
vbt_validate(rdev) == CB_SUCCESS) { |
| 249 |
printk(BIOS_INFO, "GMA: Found valid VBT in legacy area\n"); |
| 250 |
return CB_SUCCESS; |
| 251 |
} |
| 252 |
|
| 253 |
printk(BIOS_ERR, "GMA: VBT couldn't be found\n"); |
| 254 |
return CB_ERR; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
static struct opregion_version opregion_get_version(void) |
| 259 |
{ |
| 260 |
if (CONFIG(INTEL_GMA_OPREGION_2_1)) |
| 261 |
return (struct opregion_version) { .major = 2, .minor = 1 }; |
| 262 |
|
| 263 |
return (struct opregion_version) { .major = 2, .minor = 0 }; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static inline bool is_ext_vbt_required(igd_opregion_t *opregion, optionrom_vbt_t *vbt) |
| 272 |
{ |
| 273 |
return (vbt->hdr_vbt_size > sizeof(opregion->vbt.gvd1)); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
static inline bool uses_relative_vbt_addr(opregion_header_t *header) |
| 278 |
{ |
| 279 |
if (header->opver.major > 2) |
| 280 |
return true; |
| 281 |
|
| 282 |
return header->opver.major >= 2 && header->opver.minor >= 1; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
static void opregion_add_ext_vbt(igd_opregion_t *opregion, uint8_t *ext_vbt, |
| 290 |
optionrom_vbt_t *vbt, size_t ext_vbt_size) |
| 291 |
{ |
| 292 |
opregion_header_t *header = &opregion->header; |
| 293 |
|
| 294 |
memcpy(ext_vbt, vbt, vbt->hdr_vbt_size); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
if (uses_relative_vbt_addr(header)) |
| 300 |
opregion->mailbox3.rvda = sizeof(*opregion); |
| 301 |
else |
| 302 |
opregion->mailbox3.rvda = (uintptr_t)ext_vbt; |
| 303 |
|
| 304 |
opregion->mailbox3.rvds = ext_vbt_size; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
enum cb_err intel_gma_init_igd_opregion(void) |
| 309 |
{ |
| 310 |
igd_opregion_t *opregion; |
| 311 |
struct region_device rdev; |
| 312 |
optionrom_vbt_t *vbt = NULL; |
| 313 |
size_t opregion_size = sizeof(igd_opregion_t); |
| 314 |
size_t ext_vbt_size; |
| 315 |
|
| 316 |
if (acpi_is_wakeup_s3()) |
| 317 |
return intel_gma_restore_opregion(); |
| 318 |
|
| 319 |
if (find_vbt_location(&rdev) != CB_SUCCESS) |
| 320 |
return CB_ERR; |
| 321 |
|
| 322 |
vbt = rdev_mmap_full(&rdev); |
| 323 |
if (!vbt) { |
| 324 |
printk(BIOS_ERR, "GMA: Error mapping VBT\n"); |
| 325 |
return CB_ERR; |
| 326 |
} |
| 327 |
|
| 328 |
if (vbt->hdr_vbt_size > region_device_sz(&rdev)) { |
| 329 |
printk(BIOS_ERR, "GMA: Error mapped only a partial VBT\n"); |
| 330 |
rdev_munmap(&rdev, vbt); |
| 331 |
return CB_ERR; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
ext_vbt_size = ALIGN_UP(vbt->hdr_vbt_size, 512); |
| 337 |
opregion_size += ext_vbt_size; |
| 338 |
|
| 339 |
opregion = cbmem_add(CBMEM_ID_IGD_OPREGION, opregion_size); |
| 340 |
if (!opregion) { |
| 341 |
printk(BIOS_ERR, "GMA: Failed to add IGD OpRegion to CBMEM.\n"); |
| 342 |
return CB_ERR; |
| 343 |
} |
| 344 |
|
| 345 |
memset(opregion, 0, opregion_size); |
| 346 |
|
| 347 |
memcpy(&opregion->header.signature, IGD_OPREGION_SIGNATURE, |
| 348 |
sizeof(opregion->header.signature)); |
| 349 |
memcpy(opregion->header.vbios_version, vbt->coreblock_biosbuild, |
| 350 |
ARRAY_SIZE(vbt->coreblock_biosbuild)); |
| 351 |
|
| 352 |
|
| 353 |
opregion->header.opver = opregion_get_version(); |
| 354 |
|
| 355 |
|
| 356 |
if (is_ext_vbt_required(opregion, vbt)) { |
| 357 |
|
| 358 |
uint8_t *ext_vbt = (uint8_t *)opregion + sizeof(*opregion); |
| 359 |
opregion_add_ext_vbt(opregion, ext_vbt, vbt, ext_vbt_size); |
| 360 |
} else { |
| 361 |
|
| 362 |
memcpy(opregion->vbt.gvd1, vbt, vbt->hdr_vbt_size); |
| 363 |
} |
| 364 |
|
| 365 |
rdev_munmap(&rdev, vbt); |
| 366 |
|
| 367 |
|
| 368 |
opregion->header.size = sizeof(igd_opregion_t) / 1024; |
| 369 |
|
| 370 |
|
| 371 |
opregion->header.mailboxes = IGD_MAILBOXES; |
| 372 |
|
| 373 |
|
| 374 |
opregion->mailbox1.clid = 1; |
| 375 |
|
| 376 |
|
| 377 |
opregion->mailbox3.bclp = IGD_BACKLIGHT_BRIGHTNESS; |
| 378 |
opregion->mailbox3.pfit = IGD_FIELD_VALID | IGD_PFIT_STRETCH; |
| 379 |
opregion->mailbox3.pcft = 0; |
| 380 |
opregion->mailbox3.cblv = IGD_FIELD_VALID | IGD_INITIAL_BRIGHTNESS; |
| 381 |
opregion->mailbox3.bclm[0] = IGD_WORD_FIELD_VALID + 0x0000; |
| 382 |
opregion->mailbox3.bclm[1] = IGD_WORD_FIELD_VALID + 0x0a19; |
| 383 |
opregion->mailbox3.bclm[2] = IGD_WORD_FIELD_VALID + 0x1433; |
| 384 |
opregion->mailbox3.bclm[3] = IGD_WORD_FIELD_VALID + 0x1e4c; |
| 385 |
opregion->mailbox3.bclm[4] = IGD_WORD_FIELD_VALID + 0x2866; |
| 386 |
opregion->mailbox3.bclm[5] = IGD_WORD_FIELD_VALID + 0x327f; |
| 387 |
opregion->mailbox3.bclm[6] = IGD_WORD_FIELD_VALID + 0x3c99; |
| 388 |
opregion->mailbox3.bclm[7] = IGD_WORD_FIELD_VALID + 0x46b2; |
| 389 |
opregion->mailbox3.bclm[8] = IGD_WORD_FIELD_VALID + 0x50cc; |
| 390 |
opregion->mailbox3.bclm[9] = IGD_WORD_FIELD_VALID + 0x5ae5; |
| 391 |
opregion->mailbox3.bclm[10] = IGD_WORD_FIELD_VALID + 0x64ff; |
| 392 |
|
| 393 |
|
| 394 |
intel_gma_opregion_register((uintptr_t)opregion); |
| 395 |
|
| 396 |
return CB_SUCCESS; |
| 397 |
} |