blob: 566f35101d26b24fdab9e277311073caa1dbe6df [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfa470422017-06-20 17:49:53 +02002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi.h>
Matt DeVillierebe08e02017-07-14 13:28:42 -05004#include <types.h>
5#include <string.h>
Matt DeVillier53e41952017-06-27 13:07:43 -05006#include <cbfs.h>
Patrick Rudolphfa470422017-06-20 17:49:53 +02007#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
10#include <device/pci_ops.h>
Patrick Rudolphbac23032017-06-30 15:18:23 +020011#include <console/console.h>
12#include <cbmem.h>
Matt DeVillierebe08e02017-07-14 13:28:42 -050013#include "intel_bios.h"
Patrick Rudolphfa470422017-06-20 17:49:53 +020014#include "opregion.h"
15
Patrick Georgi4a3956d2018-05-03 19:15:13 +020016__weak
17const char *mainboard_vbt_filename(void)
18{
19 return "vbt.bin";
20}
21
22static char vbt_data[8 * KiB];
Aaron Durbin44f80652018-05-11 11:43:52 -060023static size_t vbt_data_sz;
Patrick Georgi4a3956d2018-05-03 19:15:13 +020024
25void *locate_vbt(size_t *vbt_size)
26{
27 uint32_t vbtsig = 0;
28
Aaron Durbin44f80652018-05-11 11:43:52 -060029 if (vbt_data_sz != 0) {
30 if (vbt_size)
31 *vbt_size = vbt_data_sz;
Patrick Georgi4a3956d2018-05-03 19:15:13 +020032 return (void *)vbt_data;
Aaron Durbin44f80652018-05-11 11:43:52 -060033 }
Patrick Georgi4a3956d2018-05-03 19:15:13 +020034
35 const char *filename = mainboard_vbt_filename();
36
37 size_t file_size = cbfs_boot_load_file(filename,
38 vbt_data, sizeof(vbt_data), CBFS_TYPE_RAW);
39
40 if (file_size == 0)
41 return NULL;
42
43 if (vbt_size)
44 *vbt_size = file_size;
45
46 memcpy(&vbtsig, vbt_data, sizeof(vbtsig));
47 if (vbtsig != VBT_SIGNATURE) {
48 printk(BIOS_ERR, "Missing/invalid signature in VBT data file!\n");
49 return NULL;
50 }
51
52 printk(BIOS_INFO, "Found a VBT of %zu bytes after decompression\n",
53 file_size);
Aaron Durbin44f80652018-05-11 11:43:52 -060054 vbt_data_sz = file_size;
Patrick Georgi4a3956d2018-05-03 19:15:13 +020055
56 return (void *)vbt_data;
57}
58
Patrick Rudolphfa470422017-06-20 17:49:53 +020059/* Write ASLS PCI register and prepare SWSCI register. */
60void intel_gma_opregion_register(uintptr_t opregion)
61{
Elyes HAOUAS263076c2018-05-02 21:54:59 +020062 struct device *igd;
Patrick Rudolphfa470422017-06-20 17:49:53 +020063 u16 reg16;
Matt DeVillier681ef512018-02-11 01:17:01 -060064 u16 sci_reg;
Patrick Rudolphfa470422017-06-20 17:49:53 +020065
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030066 igd = pcidev_on_root(0x2, 0);
Patrick Rudolphfa470422017-06-20 17:49:53 +020067 if (!igd || !igd->enabled)
68 return;
69
70 /*
71 * Intel BIOS Specification
72 * Chapter 5.3.7 "Initialize Hardware State"
73 */
74 pci_write_config32(igd, ASLS, opregion);
75
76 /*
Matt DeVillier681ef512018-02-11 01:17:01 -060077 * Atom-based platforms use a combined SMI/SCI register,
78 * whereas non-Atom platforms use a separate SCI register.
79 */
Julius Wernercd49cce2019-03-05 16:53:33 -080080 if (CONFIG(INTEL_GMA_SWSMISCI))
Matt DeVillier681ef512018-02-11 01:17:01 -060081 sci_reg = SWSMISCI;
82 else
83 sci_reg = SWSCI;
84
85 /*
Patrick Rudolphfa470422017-06-20 17:49:53 +020086 * Intel's Windows driver relies on this:
87 * Intel BIOS Specification
88 * Chapter 5.4 "ASL Software SCI Handler"
89 */
Matt DeVillier681ef512018-02-11 01:17:01 -060090 reg16 = pci_read_config16(igd, sci_reg);
Patrick Rudolphfa470422017-06-20 17:49:53 +020091 reg16 &= ~GSSCIE;
92 reg16 |= SMISCISEL;
Matt DeVillier681ef512018-02-11 01:17:01 -060093 pci_write_config16(igd, sci_reg, reg16);
Patrick Rudolphfa470422017-06-20 17:49:53 +020094}
Patrick Rudolphbac23032017-06-30 15:18:23 +020095
96/* Restore ASLS register on S3 resume and prepare SWSCI. */
97void intel_gma_restore_opregion(void)
98{
99 if (acpi_is_wakeup_s3()) {
100 const void *const gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
101 uintptr_t aslb;
102
103 if (gnvs && (aslb = gma_get_gnvs_aslb(gnvs)))
104 intel_gma_opregion_register(aslb);
105 else
106 printk(BIOS_ERR, "Error: GNVS or ASLB not set.\n");
107 }
108}
Matt DeVillierebe08e02017-07-14 13:28:42 -0500109
Matt DeVillier53e41952017-06-27 13:07:43 -0500110static enum cb_err vbt_validate(struct region_device *rdev)
Matt DeVillierebe08e02017-07-14 13:28:42 -0500111{
Matt DeVillier53e41952017-06-27 13:07:43 -0500112 uint32_t sig;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500113
Matt DeVillier53e41952017-06-27 13:07:43 -0500114 if (rdev_readat(rdev, &sig, 0, sizeof(sig)) != sizeof(sig))
Matt DeVillierebe08e02017-07-14 13:28:42 -0500115 return CB_ERR;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500116
Matt DeVillier53e41952017-06-27 13:07:43 -0500117 if (sig != VBT_SIGNATURE)
Matt DeVillierebe08e02017-07-14 13:28:42 -0500118 return CB_ERR;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500119
120 return CB_SUCCESS;
121}
122
Matt DeVillier53e41952017-06-27 13:07:43 -0500123static enum cb_err locate_vbt_vbios(const u8 *vbios, struct region_device *rdev)
Matt DeVillierebe08e02017-07-14 13:28:42 -0500124{
Matt DeVillier53e41952017-06-27 13:07:43 -0500125 const optionrom_header_t *oprom;
126 const optionrom_pcir_t *pcir;
127 struct region_device rd;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500128 enum cb_err ret;
Matt DeVillier53e41952017-06-27 13:07:43 -0500129 u8 opromsize;
130 size_t offset;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500131
Matt DeVillier53e41952017-06-27 13:07:43 -0500132 // FIXME: caller should supply a region_device instead of vbios pointer
133 if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios,
134 sizeof(*oprom)))
135 return CB_ERR;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500136
Matt DeVillier53e41952017-06-27 13:07:43 -0500137 if (rdev_readat(&rd, &opromsize, offsetof(optionrom_header_t, size),
138 sizeof(opromsize)) != sizeof(opromsize) || !opromsize)
139 return CB_ERR;
140
141 if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios,
142 opromsize * 512))
143 return CB_ERR;
144
145 oprom = rdev_mmap(&rd, 0, sizeof(*oprom));
146 if (!oprom)
147 return CB_ERR;
148
149 if (!oprom->pcir_offset || !oprom->vbt_offset) {
150 rdev_munmap(&rd, (void *)oprom);
151 return CB_ERR;
152 }
153
154 pcir = rdev_mmap(&rd, oprom->pcir_offset, sizeof(*pcir));
155 if (pcir == NULL) {
156 rdev_munmap(&rd, (void *)oprom);
157 return CB_ERR;
158 }
159
160 printk(BIOS_DEBUG, "GMA: locate_vbt_vbios: %x %x %x %x %x\n",
161 oprom->signature, pcir->vendor, pcir->classcode[0],
162 pcir->classcode[1], pcir->classcode[2]);
163
164 /* Make sure we got an Intel VGA option rom */
165 if ((oprom->signature != OPROM_SIGNATURE) ||
166 (pcir->vendor != PCI_VENDOR_ID_INTEL) ||
167 (pcir->signature != 0x52494350) ||
168 (pcir->classcode[0] != 0x00) ||
169 (pcir->classcode[1] != 0x00) ||
170 (pcir->classcode[2] != 0x03)) {
171 rdev_munmap(&rd, (void *)oprom);
172 rdev_munmap(&rd, (void *)pcir);
173 return CB_ERR;
174 }
175
176 rdev_munmap(&rd, (void *)pcir);
177
178 /* Search for $VBT as some VBIOS are broken... */
179 offset = oprom->vbt_offset;
180 do {
181 ret = rdev_chain(rdev, &rd, offset,
182 (opromsize * 512) - offset);
183 offset++;
184 } while (ret == CB_SUCCESS && vbt_validate(rdev) != CB_SUCCESS);
185
186 offset--;
187
188 if (ret == CB_SUCCESS && offset != oprom->vbt_offset)
189 printk(BIOS_WARNING, "GMA: Buggy VBIOS found\n");
190 else if (ret != CB_SUCCESS)
191 printk(BIOS_ERR, "GMA: Broken VBIOS found\n");
192
193 rdev_munmap(&rd, (void *)oprom);
194 return ret;
195}
196
197static enum cb_err locate_vbt_cbfs(struct region_device *rdev)
198{
Patrick Georgi4a3956d2018-05-03 19:15:13 +0200199 size_t vbt_data_size;
200 void *vbt = locate_vbt(&vbt_data_size);
Matt DeVillier53e41952017-06-27 13:07:43 -0500201
Patrick Georgi4a3956d2018-05-03 19:15:13 +0200202 if (vbt == NULL)
203 return CB_ERR;
Matt DeVillier53e41952017-06-27 13:07:43 -0500204
Patrick Georgi4a3956d2018-05-03 19:15:13 +0200205 if (rdev_chain(rdev, &addrspace_32bit.rdev, (uintptr_t)vbt,
206 vbt_data_size))
207 return CB_ERR;
208
209 printk(BIOS_INFO, "GMA: Found VBT in CBFS\n");
210
211 return CB_SUCCESS;
Matt DeVillier53e41952017-06-27 13:07:43 -0500212}
213
214static enum cb_err locate_vbt_vbios_cbfs(struct region_device *rdev)
215{
216 const u8 *oprom =
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300217 (const u8 *)pci_rom_probe(pcidev_on_root(0x2, 0));
Matt DeVillier53e41952017-06-27 13:07:43 -0500218 if (oprom == NULL)
219 return CB_ERR;
220
221 printk(BIOS_INFO, "GMA: Found VBIOS in CBFS\n");
222
223 return locate_vbt_vbios(oprom, rdev);
224}
225
226/* Initialize IGD OpRegion, called from ACPI code and OS drivers */
227enum cb_err
228intel_gma_init_igd_opregion(igd_opregion_t *opregion)
229{
230 struct region_device rdev;
231 optionrom_vbt_t *vbt = NULL;
232 optionrom_vbt_t *ext_vbt;
233 bool found = false;
234
235 /* Search for vbt.bin in CBFS. */
236 if (locate_vbt_cbfs(&rdev) == CB_SUCCESS &&
237 vbt_validate(&rdev) == CB_SUCCESS) {
238 found = true;
239 printk(BIOS_INFO, "GMA: Found valid VBT in CBFS\n");
240 }
241 /* Search for pci8086,XXXX.rom in CBFS. */
242 else if (locate_vbt_vbios_cbfs(&rdev) == CB_SUCCESS &&
243 vbt_validate(&rdev) == CB_SUCCESS) {
244 found = true;
245 printk(BIOS_INFO, "GMA: Found valid VBT in VBIOS\n");
246 }
247 /*
248 * Try to locate Intel VBIOS at 0xc0000. It might have been placed by
249 * Native Graphics Init as fake Option ROM or when coreboot did run the
250 * VBIOS on legacy platforms.
251 * TODO: Place generated fake VBT in CBMEM and get rid of this.
252 */
253 else if (locate_vbt_vbios((u8 *)0xc0000, &rdev) == CB_SUCCESS &&
254 vbt_validate(&rdev) == CB_SUCCESS) {
255 found = true;
256 printk(BIOS_INFO, "GMA: Found valid VBT in legacy area\n");
257 }
258
259 if (!found) {
260 printk(BIOS_ERR, "GMA: VBT couldn't be found\n");
261 return CB_ERR;
262 }
263
264 vbt = rdev_mmap_full(&rdev);
265 if (!vbt) {
266 printk(BIOS_ERR, "GMA: Error mapping VBT\n");
267 return CB_ERR;
268 }
269
270 if (vbt->hdr_vbt_size > region_device_sz(&rdev)) {
271 printk(BIOS_ERR, "GMA: Error mapped only a partial VBT\n");
272 rdev_munmap(&rdev, vbt);
273 return CB_ERR;
274 }
275
276 memset(opregion, 0, sizeof(igd_opregion_t));
Matt DeVillierebe08e02017-07-14 13:28:42 -0500277
278 memcpy(&opregion->header.signature, IGD_OPREGION_SIGNATURE,
279 sizeof(opregion->header.signature));
Matt DeVillier53e41952017-06-27 13:07:43 -0500280 memcpy(opregion->header.vbios_version, vbt->coreblock_biosbuild,
281 ARRAY_SIZE(vbt->coreblock_biosbuild));
282 /* Extended VBT support */
283 if (vbt->hdr_vbt_size > sizeof(opregion->vbt.gvd1)) {
284 ext_vbt = cbmem_add(CBMEM_ID_EXT_VBT, vbt->hdr_vbt_size);
285
286 if (ext_vbt == NULL) {
287 printk(BIOS_ERR,
288 "GMA: Unable to add Ext VBT to cbmem!\n");
289 rdev_munmap(&rdev, vbt);
290 return CB_ERR;
291 }
292
293 memcpy(ext_vbt, vbt, vbt->hdr_vbt_size);
294 opregion->mailbox3.rvda = (uintptr_t)ext_vbt;
295 opregion->mailbox3.rvds = vbt->hdr_vbt_size;
296 } else {
297 /* Raw VBT size which can fit in gvd1 */
298 memcpy(opregion->vbt.gvd1, vbt, vbt->hdr_vbt_size);
299 }
300
301 rdev_munmap(&rdev, vbt);
Matt DeVillierebe08e02017-07-14 13:28:42 -0500302
303 /* 8kb */
304 opregion->header.size = sizeof(igd_opregion_t) / 1024;
Matt DeVillierfd0a8912017-10-19 22:44:18 -0500305
306 /*
Elyes HAOUAS18958382018-08-07 12:23:16 +0200307 * Left-shift version field to accommodate Intel Windows driver quirk
Matt DeVillierfd0a8912017-10-19 22:44:18 -0500308 * when not using a VBIOS.
309 * Required for Legacy boot + NGI, UEFI + NGI, and UEFI + GOP driver.
310 *
311 * Tested on: (platform, GPU, windows driver version)
312 * samsung/stumpy (SNB, GT2, 9.17.10.4459)
313 * google/link (IVB, GT2, 15.33.4653)
314 * google/wolf (HSW, GT1, 15.40.36.4703)
315 * google/panther (HSW, GT2, 15.40.36.4703)
316 * google/rikku (BDW, GT1, 15.40.36.4703)
317 * google/lulu (BDW, GT2, 15.40.36.4703)
318 * google/chell (SKL-Y, GT2, 15.45.21.4821)
319 * google/sentry (SKL-U, GT1, 15.45.21.4821)
320 * purism/librem13v2 (SKL-U, GT2, 15.45.21.4821)
321 *
322 * No adverse effects when using VBIOS or booting Linux.
323 */
324 opregion->header.version = IGD_OPREGION_VERSION << 24;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500325
326 // FIXME We just assume we're mobile for now
327 opregion->header.mailboxes = MAILBOXES_MOBILE;
328
329 // TODO Initialize Mailbox 1
Patrick Georgi0f68b232018-01-25 18:23:15 +0100330 opregion->mailbox1.clid = 1;
Matt DeVillierebe08e02017-07-14 13:28:42 -0500331
332 // TODO Initialize Mailbox 3
333 opregion->mailbox3.bclp = IGD_BACKLIGHT_BRIGHTNESS;
334 opregion->mailbox3.pfit = IGD_FIELD_VALID | IGD_PFIT_STRETCH;
335 opregion->mailbox3.pcft = 0; // should be (IMON << 1) & 0x3e
336 opregion->mailbox3.cblv = IGD_FIELD_VALID | IGD_INITIAL_BRIGHTNESS;
337 opregion->mailbox3.bclm[0] = IGD_WORD_FIELD_VALID + 0x0000;
338 opregion->mailbox3.bclm[1] = IGD_WORD_FIELD_VALID + 0x0a19;
339 opregion->mailbox3.bclm[2] = IGD_WORD_FIELD_VALID + 0x1433;
340 opregion->mailbox3.bclm[3] = IGD_WORD_FIELD_VALID + 0x1e4c;
341 opregion->mailbox3.bclm[4] = IGD_WORD_FIELD_VALID + 0x2866;
342 opregion->mailbox3.bclm[5] = IGD_WORD_FIELD_VALID + 0x327f;
343 opregion->mailbox3.bclm[6] = IGD_WORD_FIELD_VALID + 0x3c99;
344 opregion->mailbox3.bclm[7] = IGD_WORD_FIELD_VALID + 0x46b2;
345 opregion->mailbox3.bclm[8] = IGD_WORD_FIELD_VALID + 0x50cc;
346 opregion->mailbox3.bclm[9] = IGD_WORD_FIELD_VALID + 0x5ae5;
347 opregion->mailbox3.bclm[10] = IGD_WORD_FIELD_VALID + 0x64ff;
348
Matt DeVillierebe08e02017-07-14 13:28:42 -0500349 /* Write ASLS PCI register and prepare SWSCI register. */
350 intel_gma_opregion_register((uintptr_t)opregion);
351
352 return CB_SUCCESS;
353}