Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2, or (at your option) |
| 9 | * any later verion of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 17 | #include <arch/acpi.h> |
| 18 | #include <types.h> |
| 19 | #include <string.h> |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 20 | #include <cbfs.h> |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 21 | #include <device/device.h> |
| 22 | #include <device/pci.h> |
| 23 | #include <device/pci_ids.h> |
| 24 | #include <device/pci_ops.h> |
Patrick Rudolph | bac2303 | 2017-06-30 15:18:23 +0200 | [diff] [blame] | 25 | #include <console/console.h> |
| 26 | #include <cbmem.h> |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 27 | #include "intel_bios.h" |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 28 | #include "opregion.h" |
| 29 | |
Patrick Georgi | 4a3956d | 2018-05-03 19:15:13 +0200 | [diff] [blame] | 30 | __weak |
| 31 | const char *mainboard_vbt_filename(void) |
| 32 | { |
| 33 | return "vbt.bin"; |
| 34 | } |
| 35 | |
| 36 | static char vbt_data[8 * KiB]; |
| 37 | static int vbt_data_used; |
| 38 | |
| 39 | void *locate_vbt(size_t *vbt_size) |
| 40 | { |
| 41 | uint32_t vbtsig = 0; |
| 42 | |
| 43 | if (vbt_data_used == 1) |
| 44 | return (void *)vbt_data; |
| 45 | |
| 46 | const char *filename = mainboard_vbt_filename(); |
| 47 | |
| 48 | size_t file_size = cbfs_boot_load_file(filename, |
| 49 | vbt_data, sizeof(vbt_data), CBFS_TYPE_RAW); |
| 50 | |
| 51 | if (file_size == 0) |
| 52 | return NULL; |
| 53 | |
| 54 | if (vbt_size) |
| 55 | *vbt_size = file_size; |
| 56 | |
| 57 | memcpy(&vbtsig, vbt_data, sizeof(vbtsig)); |
| 58 | if (vbtsig != VBT_SIGNATURE) { |
| 59 | printk(BIOS_ERR, "Missing/invalid signature in VBT data file!\n"); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | printk(BIOS_INFO, "Found a VBT of %zu bytes after decompression\n", |
| 64 | file_size); |
| 65 | vbt_data_used = 1; |
| 66 | |
| 67 | return (void *)vbt_data; |
| 68 | } |
| 69 | |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 70 | /* Write ASLS PCI register and prepare SWSCI register. */ |
| 71 | void intel_gma_opregion_register(uintptr_t opregion) |
| 72 | { |
Elyes HAOUAS | 263076c | 2018-05-02 21:54:59 +0200 | [diff] [blame] | 73 | struct device *igd; |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 74 | u16 reg16; |
Matt DeVillier | 681ef51 | 2018-02-11 01:17:01 -0600 | [diff] [blame] | 75 | u16 sci_reg; |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 76 | |
| 77 | igd = dev_find_slot(0, PCI_DEVFN(0x2, 0)); |
| 78 | if (!igd || !igd->enabled) |
| 79 | return; |
| 80 | |
| 81 | /* |
| 82 | * Intel BIOS Specification |
| 83 | * Chapter 5.3.7 "Initialize Hardware State" |
| 84 | */ |
| 85 | pci_write_config32(igd, ASLS, opregion); |
| 86 | |
| 87 | /* |
Matt DeVillier | 681ef51 | 2018-02-11 01:17:01 -0600 | [diff] [blame] | 88 | * Atom-based platforms use a combined SMI/SCI register, |
| 89 | * whereas non-Atom platforms use a separate SCI register. |
| 90 | */ |
| 91 | if (IS_ENABLED(CONFIG_INTEL_GMA_SWSMISCI)) |
| 92 | sci_reg = SWSMISCI; |
| 93 | else |
| 94 | sci_reg = SWSCI; |
| 95 | |
| 96 | /* |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 97 | * Intel's Windows driver relies on this: |
| 98 | * Intel BIOS Specification |
| 99 | * Chapter 5.4 "ASL Software SCI Handler" |
| 100 | */ |
Matt DeVillier | 681ef51 | 2018-02-11 01:17:01 -0600 | [diff] [blame] | 101 | reg16 = pci_read_config16(igd, sci_reg); |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 102 | reg16 &= ~GSSCIE; |
| 103 | reg16 |= SMISCISEL; |
Matt DeVillier | 681ef51 | 2018-02-11 01:17:01 -0600 | [diff] [blame] | 104 | pci_write_config16(igd, sci_reg, reg16); |
Patrick Rudolph | fa47042 | 2017-06-20 17:49:53 +0200 | [diff] [blame] | 105 | } |
Patrick Rudolph | bac2303 | 2017-06-30 15:18:23 +0200 | [diff] [blame] | 106 | |
| 107 | /* Restore ASLS register on S3 resume and prepare SWSCI. */ |
| 108 | void intel_gma_restore_opregion(void) |
| 109 | { |
| 110 | if (acpi_is_wakeup_s3()) { |
| 111 | const void *const gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS); |
| 112 | uintptr_t aslb; |
| 113 | |
| 114 | if (gnvs && (aslb = gma_get_gnvs_aslb(gnvs))) |
| 115 | intel_gma_opregion_register(aslb); |
| 116 | else |
| 117 | printk(BIOS_ERR, "Error: GNVS or ASLB not set.\n"); |
| 118 | } |
| 119 | } |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 120 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 121 | static enum cb_err vbt_validate(struct region_device *rdev) |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 122 | { |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 123 | uint32_t sig; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 124 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 125 | if (rdev_readat(rdev, &sig, 0, sizeof(sig)) != sizeof(sig)) |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 126 | return CB_ERR; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 127 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 128 | if (sig != VBT_SIGNATURE) |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 129 | return CB_ERR; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 130 | |
| 131 | return CB_SUCCESS; |
| 132 | } |
| 133 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 134 | static enum cb_err locate_vbt_vbios(const u8 *vbios, struct region_device *rdev) |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 135 | { |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 136 | const optionrom_header_t *oprom; |
| 137 | const optionrom_pcir_t *pcir; |
| 138 | struct region_device rd; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 139 | enum cb_err ret; |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 140 | u8 opromsize; |
| 141 | size_t offset; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 142 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 143 | // FIXME: caller should supply a region_device instead of vbios pointer |
| 144 | if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios, |
| 145 | sizeof(*oprom))) |
| 146 | return CB_ERR; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 147 | |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 148 | if (rdev_readat(&rd, &opromsize, offsetof(optionrom_header_t, size), |
| 149 | sizeof(opromsize)) != sizeof(opromsize) || !opromsize) |
| 150 | return CB_ERR; |
| 151 | |
| 152 | if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios, |
| 153 | opromsize * 512)) |
| 154 | return CB_ERR; |
| 155 | |
| 156 | oprom = rdev_mmap(&rd, 0, sizeof(*oprom)); |
| 157 | if (!oprom) |
| 158 | return CB_ERR; |
| 159 | |
| 160 | if (!oprom->pcir_offset || !oprom->vbt_offset) { |
| 161 | rdev_munmap(&rd, (void *)oprom); |
| 162 | return CB_ERR; |
| 163 | } |
| 164 | |
| 165 | pcir = rdev_mmap(&rd, oprom->pcir_offset, sizeof(*pcir)); |
| 166 | if (pcir == NULL) { |
| 167 | rdev_munmap(&rd, (void *)oprom); |
| 168 | return CB_ERR; |
| 169 | } |
| 170 | |
| 171 | printk(BIOS_DEBUG, "GMA: locate_vbt_vbios: %x %x %x %x %x\n", |
| 172 | oprom->signature, pcir->vendor, pcir->classcode[0], |
| 173 | pcir->classcode[1], pcir->classcode[2]); |
| 174 | |
| 175 | /* Make sure we got an Intel VGA option rom */ |
| 176 | if ((oprom->signature != OPROM_SIGNATURE) || |
| 177 | (pcir->vendor != PCI_VENDOR_ID_INTEL) || |
| 178 | (pcir->signature != 0x52494350) || |
| 179 | (pcir->classcode[0] != 0x00) || |
| 180 | (pcir->classcode[1] != 0x00) || |
| 181 | (pcir->classcode[2] != 0x03)) { |
| 182 | rdev_munmap(&rd, (void *)oprom); |
| 183 | rdev_munmap(&rd, (void *)pcir); |
| 184 | return CB_ERR; |
| 185 | } |
| 186 | |
| 187 | rdev_munmap(&rd, (void *)pcir); |
| 188 | |
| 189 | /* Search for $VBT as some VBIOS are broken... */ |
| 190 | offset = oprom->vbt_offset; |
| 191 | do { |
| 192 | ret = rdev_chain(rdev, &rd, offset, |
| 193 | (opromsize * 512) - offset); |
| 194 | offset++; |
| 195 | } while (ret == CB_SUCCESS && vbt_validate(rdev) != CB_SUCCESS); |
| 196 | |
| 197 | offset--; |
| 198 | |
| 199 | if (ret == CB_SUCCESS && offset != oprom->vbt_offset) |
| 200 | printk(BIOS_WARNING, "GMA: Buggy VBIOS found\n"); |
| 201 | else if (ret != CB_SUCCESS) |
| 202 | printk(BIOS_ERR, "GMA: Broken VBIOS found\n"); |
| 203 | |
| 204 | rdev_munmap(&rd, (void *)oprom); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static enum cb_err locate_vbt_cbfs(struct region_device *rdev) |
| 209 | { |
Patrick Georgi | 4a3956d | 2018-05-03 19:15:13 +0200 | [diff] [blame] | 210 | size_t vbt_data_size; |
| 211 | void *vbt = locate_vbt(&vbt_data_size); |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 212 | |
Patrick Georgi | 4a3956d | 2018-05-03 19:15:13 +0200 | [diff] [blame] | 213 | if (vbt == NULL) |
| 214 | return CB_ERR; |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 215 | |
Patrick Georgi | 4a3956d | 2018-05-03 19:15:13 +0200 | [diff] [blame] | 216 | if (rdev_chain(rdev, &addrspace_32bit.rdev, (uintptr_t)vbt, |
| 217 | vbt_data_size)) |
| 218 | return CB_ERR; |
| 219 | |
| 220 | printk(BIOS_INFO, "GMA: Found VBT in CBFS\n"); |
| 221 | |
| 222 | return CB_SUCCESS; |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static enum cb_err locate_vbt_vbios_cbfs(struct region_device *rdev) |
| 226 | { |
| 227 | const u8 *oprom = |
| 228 | (const u8 *)pci_rom_probe(dev_find_slot(0, PCI_DEVFN(0x2, 0))); |
| 229 | if (oprom == NULL) |
| 230 | return CB_ERR; |
| 231 | |
| 232 | printk(BIOS_INFO, "GMA: Found VBIOS in CBFS\n"); |
| 233 | |
| 234 | return locate_vbt_vbios(oprom, rdev); |
| 235 | } |
| 236 | |
| 237 | /* Initialize IGD OpRegion, called from ACPI code and OS drivers */ |
| 238 | enum cb_err |
| 239 | intel_gma_init_igd_opregion(igd_opregion_t *opregion) |
| 240 | { |
| 241 | struct region_device rdev; |
| 242 | optionrom_vbt_t *vbt = NULL; |
| 243 | optionrom_vbt_t *ext_vbt; |
| 244 | bool found = false; |
| 245 | |
| 246 | /* Search for vbt.bin in CBFS. */ |
| 247 | if (locate_vbt_cbfs(&rdev) == CB_SUCCESS && |
| 248 | vbt_validate(&rdev) == CB_SUCCESS) { |
| 249 | found = true; |
| 250 | printk(BIOS_INFO, "GMA: Found valid VBT in CBFS\n"); |
| 251 | } |
| 252 | /* Search for pci8086,XXXX.rom in CBFS. */ |
| 253 | else if (locate_vbt_vbios_cbfs(&rdev) == CB_SUCCESS && |
| 254 | vbt_validate(&rdev) == CB_SUCCESS) { |
| 255 | found = true; |
| 256 | printk(BIOS_INFO, "GMA: Found valid VBT in VBIOS\n"); |
| 257 | } |
| 258 | /* |
| 259 | * Try to locate Intel VBIOS at 0xc0000. It might have been placed by |
| 260 | * Native Graphics Init as fake Option ROM or when coreboot did run the |
| 261 | * VBIOS on legacy platforms. |
| 262 | * TODO: Place generated fake VBT in CBMEM and get rid of this. |
| 263 | */ |
| 264 | else if (locate_vbt_vbios((u8 *)0xc0000, &rdev) == CB_SUCCESS && |
| 265 | vbt_validate(&rdev) == CB_SUCCESS) { |
| 266 | found = true; |
| 267 | printk(BIOS_INFO, "GMA: Found valid VBT in legacy area\n"); |
| 268 | } |
| 269 | |
| 270 | if (!found) { |
| 271 | printk(BIOS_ERR, "GMA: VBT couldn't be found\n"); |
| 272 | return CB_ERR; |
| 273 | } |
| 274 | |
| 275 | vbt = rdev_mmap_full(&rdev); |
| 276 | if (!vbt) { |
| 277 | printk(BIOS_ERR, "GMA: Error mapping VBT\n"); |
| 278 | return CB_ERR; |
| 279 | } |
| 280 | |
| 281 | if (vbt->hdr_vbt_size > region_device_sz(&rdev)) { |
| 282 | printk(BIOS_ERR, "GMA: Error mapped only a partial VBT\n"); |
| 283 | rdev_munmap(&rdev, vbt); |
| 284 | return CB_ERR; |
| 285 | } |
| 286 | |
| 287 | memset(opregion, 0, sizeof(igd_opregion_t)); |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 288 | |
| 289 | memcpy(&opregion->header.signature, IGD_OPREGION_SIGNATURE, |
| 290 | sizeof(opregion->header.signature)); |
Matt DeVillier | 53e4195 | 2017-06-27 13:07:43 -0500 | [diff] [blame] | 291 | memcpy(opregion->header.vbios_version, vbt->coreblock_biosbuild, |
| 292 | ARRAY_SIZE(vbt->coreblock_biosbuild)); |
| 293 | /* Extended VBT support */ |
| 294 | if (vbt->hdr_vbt_size > sizeof(opregion->vbt.gvd1)) { |
| 295 | ext_vbt = cbmem_add(CBMEM_ID_EXT_VBT, vbt->hdr_vbt_size); |
| 296 | |
| 297 | if (ext_vbt == NULL) { |
| 298 | printk(BIOS_ERR, |
| 299 | "GMA: Unable to add Ext VBT to cbmem!\n"); |
| 300 | rdev_munmap(&rdev, vbt); |
| 301 | return CB_ERR; |
| 302 | } |
| 303 | |
| 304 | memcpy(ext_vbt, vbt, vbt->hdr_vbt_size); |
| 305 | opregion->mailbox3.rvda = (uintptr_t)ext_vbt; |
| 306 | opregion->mailbox3.rvds = vbt->hdr_vbt_size; |
| 307 | } else { |
| 308 | /* Raw VBT size which can fit in gvd1 */ |
| 309 | memcpy(opregion->vbt.gvd1, vbt, vbt->hdr_vbt_size); |
| 310 | } |
| 311 | |
| 312 | rdev_munmap(&rdev, vbt); |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 313 | |
| 314 | /* 8kb */ |
| 315 | opregion->header.size = sizeof(igd_opregion_t) / 1024; |
Matt DeVillier | fd0a891 | 2017-10-19 22:44:18 -0500 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Left-shift version field to accomodate Intel Windows driver quirk |
| 319 | * when not using a VBIOS. |
| 320 | * Required for Legacy boot + NGI, UEFI + NGI, and UEFI + GOP driver. |
| 321 | * |
| 322 | * Tested on: (platform, GPU, windows driver version) |
| 323 | * samsung/stumpy (SNB, GT2, 9.17.10.4459) |
| 324 | * google/link (IVB, GT2, 15.33.4653) |
| 325 | * google/wolf (HSW, GT1, 15.40.36.4703) |
| 326 | * google/panther (HSW, GT2, 15.40.36.4703) |
| 327 | * google/rikku (BDW, GT1, 15.40.36.4703) |
| 328 | * google/lulu (BDW, GT2, 15.40.36.4703) |
| 329 | * google/chell (SKL-Y, GT2, 15.45.21.4821) |
| 330 | * google/sentry (SKL-U, GT1, 15.45.21.4821) |
| 331 | * purism/librem13v2 (SKL-U, GT2, 15.45.21.4821) |
| 332 | * |
| 333 | * No adverse effects when using VBIOS or booting Linux. |
| 334 | */ |
| 335 | opregion->header.version = IGD_OPREGION_VERSION << 24; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 336 | |
| 337 | // FIXME We just assume we're mobile for now |
| 338 | opregion->header.mailboxes = MAILBOXES_MOBILE; |
| 339 | |
| 340 | // TODO Initialize Mailbox 1 |
Patrick Georgi | 0f68b23 | 2018-01-25 18:23:15 +0100 | [diff] [blame] | 341 | opregion->mailbox1.clid = 1; |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 342 | |
| 343 | // TODO Initialize Mailbox 3 |
| 344 | opregion->mailbox3.bclp = IGD_BACKLIGHT_BRIGHTNESS; |
| 345 | opregion->mailbox3.pfit = IGD_FIELD_VALID | IGD_PFIT_STRETCH; |
| 346 | opregion->mailbox3.pcft = 0; // should be (IMON << 1) & 0x3e |
| 347 | opregion->mailbox3.cblv = IGD_FIELD_VALID | IGD_INITIAL_BRIGHTNESS; |
| 348 | opregion->mailbox3.bclm[0] = IGD_WORD_FIELD_VALID + 0x0000; |
| 349 | opregion->mailbox3.bclm[1] = IGD_WORD_FIELD_VALID + 0x0a19; |
| 350 | opregion->mailbox3.bclm[2] = IGD_WORD_FIELD_VALID + 0x1433; |
| 351 | opregion->mailbox3.bclm[3] = IGD_WORD_FIELD_VALID + 0x1e4c; |
| 352 | opregion->mailbox3.bclm[4] = IGD_WORD_FIELD_VALID + 0x2866; |
| 353 | opregion->mailbox3.bclm[5] = IGD_WORD_FIELD_VALID + 0x327f; |
| 354 | opregion->mailbox3.bclm[6] = IGD_WORD_FIELD_VALID + 0x3c99; |
| 355 | opregion->mailbox3.bclm[7] = IGD_WORD_FIELD_VALID + 0x46b2; |
| 356 | opregion->mailbox3.bclm[8] = IGD_WORD_FIELD_VALID + 0x50cc; |
| 357 | opregion->mailbox3.bclm[9] = IGD_WORD_FIELD_VALID + 0x5ae5; |
| 358 | opregion->mailbox3.bclm[10] = IGD_WORD_FIELD_VALID + 0x64ff; |
| 359 | |
Matt DeVillier | ebe08e0 | 2017-07-14 13:28:42 -0500 | [diff] [blame] | 360 | /* Write ASLS PCI register and prepare SWSCI register. */ |
| 361 | intel_gma_opregion_register((uintptr_t)opregion); |
| 362 | |
| 363 | return CB_SUCCESS; |
| 364 | } |