Debian Codegraph @

src/soc/amd/common/block/graphics/graphics.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <acpi/acpi_device.h>
4 #include <acpi/acpigen.h>
5 #include <amdblocks/graphics.h>
6 #include <amdblocks/vbios_cache.h>
7 #include <amdblocks/vbt.h>
8 #include <boot/coreboot_tables.h>
9 #include <bootmode.h>
10 #include <bootstate.h>
11 #include <console/console.h>
12 #include <device/device.h>
13 #include <device/pci.h>
14 #include <fmap.h>
15 #include <security/vboot/vbios_cache_hash_tpm.h>
16 #include <timestamp.h>
17
18 static bool vbios_loaded_from_cache = false;
19 static uint8_t vbios_data[VBIOS_CACHE_FMAP_SIZE];
20
21 #define ATIF_FUNCTION_VERIFY_INTERFACE 0x0
22 struct atif_verify_interface_output {
23 uint16_t size; /* Size of this object, including size field */
24 uint16_t version;
25 uint32_t supported_notifications;
26 uint32_t supported_functions; /* Bit n set if function n+1 supported. */
27 };
28
29 #define ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS 0x10
30 # define ATIF_QBTC_REQUEST_LCD1 0
31 /* error codes */
32 # define ATIF_QBTC_ERROR_CODE_SUCCESS 0
33 # define ATIF_QBTC_ERROR_CODE_FAILURE 1
34 # define ATIF_QBTC_ERROR_CODE_DEVICE_NOT_SUPPORTED 2
35 struct atif_brightness_input {
36 uint16_t size;
37 /* ATIF doc indicates this field is a word, but the kernel drivers uses a byte. */
38 uint8_t requested_display;
39 };
40 struct atif_brightness_output {
41 uint16_t size; /* Size of this object, including size field. */
42 uint16_t flags; /* Currently all reserved. */
43 uint8_t error_code;
44 /* default brightness fields currently ignored by Linux driver. */
45 uint8_t default_brightness_ac; /* Percentage brightness when connected to AC. */
46 uint8_t default_brightness_dc; /* Percentage brightness when connected to DC. */
47 /* The following 2 fields are the only ones honored by Linux driver currently. */
48 uint8_t min_input_signal_level; /* 0-255 corresponding to 0% */
49 uint8_t max_input_signal_level; /* 0-255 corresponding to 100% */
50 /* Array of data points consisting of:
51 * { uint8_t luminance_level; (percent)
52 * uint8_t input_signal_level; (0-255 in value) }
53 * Linux ignores these fields so no support currently. */
54 uint8_t count_data_points; /* Count of data points. */
55 };
56
57 static void generate_atif(const struct device *dev)
58 {
59 struct atif_verify_interface_output verify_output = {
60 .size = sizeof(verify_output),
61 .version = 1,
62 .supported_functions =
63 BIT(ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS - 1),
64 };
65 struct atif_brightness_output brightness_error = {
66 .size = sizeof(brightness_error),
67 .error_code = ATIF_QBTC_ERROR_CODE_DEVICE_NOT_SUPPORTED,
68 };
69 struct atif_brightness_output brightness_out = {
70 .size = sizeof(brightness_out),
71 .error_code = ATIF_QBTC_ERROR_CODE_SUCCESS,
72 .min_input_signal_level = 0,
73 .max_input_signal_level = 255,
74 };
75
76 /* Scope (\_SB.PCI0.PBRA.IGFX) */
77 acpigen_write_scope(acpi_device_path(dev));
78 /* Method (ATIF, 2, NotSerialized) */
79 acpigen_write_method("ATIF", 2);
80 /* ToInteger (Arg0, Local0) */
81 acpigen_write_to_integer(ARG0_OP, LOCAL0_OP);
82
83 /* If ((Local0 == Zero)) */
84 acpigen_write_if_lequal_op_int(LOCAL0_OP, ATIF_FUNCTION_VERIFY_INTERFACE);
85 /* Return (Buffer (0x0C) { ... } */
86 acpigen_write_return_byte_buffer((uint8_t *)(void *)&verify_output,
87 sizeof(verify_output));
88
89 /* ElseIf ((Local0 == 0x10)) */
90 acpigen_write_else();
91 acpigen_write_if_lequal_op_int(LOCAL0_OP,
92 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS);
93 /* CreateByteField (Arg1, 0x02, DISP) */
94 acpigen_write_create_byte_field(ARG1_OP,
95 offsetof(struct atif_brightness_input, requested_display), "DISP");
96 /* ToInteger (DISP, Local1) */
97 acpigen_write_to_integer_from_namestring("DISP", LOCAL1_OP);
98 /* If ((Local1 == Zero)) */
99 acpigen_write_if_lequal_op_int(LOCAL1_OP, ATIF_QBTC_REQUEST_LCD1);
100 /* Return (Buffer (0x0A) { ... } */
101 acpigen_write_return_byte_buffer((uint8_t *)(void *)&brightness_out,
102 sizeof(brightness_out));
103 /* Else */
104 acpigen_write_else();
105 /* Return (Buffer (0x0A) */
106 acpigen_write_return_byte_buffer((uint8_t *)(void *)&brightness_error,
107 sizeof(brightness_error));
108 acpigen_pop_len(); /* else */
109
110 acpigen_pop_len(); /* if (LEqual(Local0, 0x10) */
111 acpigen_pop_len(); /* else */
112
113 acpigen_pop_len(); /* Method */
114 acpigen_pop_len(); /* Scope */
115 }
116
117 static void generate_acp(const struct device *dev)
118 {
119 /* Scope (\_SB.PCI0.IGFX) */
120 acpigen_write_scope(acpi_device_path(dev));
121 acpigen_write_device("ACP");
122
123 acpigen_write_name_string("_HID", "BOOT0003");
124
125 acpigen_pop_len(); /* Device */
126 acpigen_pop_len(); /* Scope */
127 }
128
129 static void graphics_fill_ssdt(const struct device *dev)
130 {
131 acpi_device_write_pci_dev(dev);
132
133 /* Use the VFCT copy when using GOP */
134 if (!CONFIG(RUN_FSP_GOP))
135 pci_rom_ssdt(dev);
136
137 if (CONFIG(SOC_AMD_COMMON_BLOCK_GRAPHICS_ATIF))
138 generate_atif(dev);
139
140 if (CONFIG(SOC_AMD_COMMON_BLOCK_GRAPHICS_ACP))
141 generate_acp(dev);
142 }
143
144 static const char *graphics_acpi_name(const struct device *dev)
145 {
146 return "IGFX";
147 }
148
149 void *vbt_get(void)
150 {
151 if (!CONFIG(RUN_FSP_GOP))
152 return NULL;
153
154 if (CONFIG(SOC_AMD_COMMON_BLOCK_GRAPHICS_NO_VGA))
155 return (void *)(uintptr_t)PCI_RAM_IMAGE_START;
156
157 return (void *)(uintptr_t)PCI_VGA_RAM_IMAGE_START;
158 }
159
160 static void graphics_set_resources(struct device *const dev)
161 {
162 struct rom_header *rom, *ram;
163
164 pci_dev_set_resources(dev);
165
166 if (!CONFIG(RUN_FSP_GOP))
167 return;
168
169 /* Load the VBIOS before FSP AFTER_PCI_ENUM notify is called. */
170 timestamp_add_now(TS_OPROM_INITIALIZE);
171 if (CONFIG(USE_SELECTIVE_GOP_INIT) && vbios_cache_is_valid() &&
172 !display_init_required()) {
173 vbios_load_from_cache();
174 timestamp_add_now(TS_OPROM_COPY_END);
175 return;
176 }
177
178 /*
179 * VBIOS cache was not used, so load it from CBFS and let FSP GOP
180 * initialize the ATOMBIOS tables.
181 */
182 rom = pci_rom_probe(dev);
183 if (rom == NULL) {
184 printk(BIOS_ERR, "%s: Unable to find ROM for %s\n",
185 __func__, dev_path(dev));
186 timestamp_add_now(TS_OPROM_COPY_END);
187 return;
188 }
189
190 ram = pci_rom_load(dev, rom);
191 if (ram == NULL) {
192 printk(BIOS_ERR, "%s: Unable to load ROM for %s\n",
193 __func__, dev_path(dev));
194 }
195
196 pci_rom_free(rom);
197
198 timestamp_add_now(TS_OPROM_COPY_END);
199 }
200
201 static void graphics_dev_init(struct device *const dev)
202 {
203 if (CONFIG(RUN_FSP_GOP))
204 fsp_graphics_init(dev);
205
206 /* Initialize PCI device, load/execute BIOS Option ROM */
207 pci_dev_init(dev);
208 }
209
210 static void read_vbios_cache_from_fmap(void *unused)
211 {
212 struct region_device rw_vbios_cache;
213 int32_t region_size;
214
215 if (!CONFIG(SOC_AMD_GFX_CACHE_VBIOS_IN_FMAP))
216 return;
217
218 if (fmap_locate_area_as_rdev(VBIOS_CACHE_FMAP_NAME, &rw_vbios_cache)) {
219 printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, VBIOS_CACHE_FMAP_NAME);
220 return;
221 }
222
223 region_size = region_device_sz(&rw_vbios_cache);
224
225 if (region_size != VBIOS_CACHE_FMAP_SIZE) {
226 printk(BIOS_ERR, "%s: %s FMAP size mismatch for VBIOS cache (%d vs %d).\n",
227 __func__, VBIOS_CACHE_FMAP_NAME, VBIOS_CACHE_FMAP_SIZE, region_size);
228 return;
229 }
230
231 /* Read cached VBIOS data into buffer */
232 if (rdev_readat(&rw_vbios_cache, &vbios_data, 0, VBIOS_CACHE_FMAP_SIZE) != VBIOS_CACHE_FMAP_SIZE) {
233 printk(BIOS_ERR, "Failed to read vbios data from flash; rdev_readat() failed.\n");
234 return;
235 }
236
237 printk(BIOS_SPEW, "VBIOS cache successfully read from FMAP.\n");
238 }
239
240 static void write_vbios_cache_to_fmap(void *unused)
241 {
242 if (!CONFIG(SOC_AMD_GFX_CACHE_VBIOS_IN_FMAP))
243 return;
244
245 /* Don't save if VBIOS loaded from cache / data unchanged */
246 if (vbios_loaded_from_cache == true) {
247 printk(BIOS_SPEW, "VBIOS data loaded from cache; not saving\n");
248 return;
249 }
250
251 struct region_device rw_vbios_cache;
252
253 if (fmap_locate_area_as_rdev_rw(VBIOS_CACHE_FMAP_NAME, &rw_vbios_cache)) {
254 printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, VBIOS_CACHE_FMAP_NAME);
255 return;
256 }
257
258 /* copy from PCI_VGA_RAM_IMAGE_START to rdev */
259 if (rdev_writeat(&rw_vbios_cache, vbt_get(), 0,
260 VBIOS_CACHE_FMAP_SIZE) != VBIOS_CACHE_FMAP_SIZE)
261 printk(BIOS_ERR, "Failed to save vbios data to flash; rdev_writeat() failed.\n");
262
263 /* copy modified vbios data to buffer before hashing */
264 memcpy(vbios_data, vbt_get(), VBIOS_CACHE_FMAP_SIZE);
265
266 /* save data hash to TPM NVRAM for validation on subsequent boots */
267 vbios_cache_update_hash(vbios_data, VBIOS_CACHE_FMAP_SIZE);
268
269 printk(BIOS_SPEW, "VBIOS cache successfully written to FMAP.\n");
270 }
271
272 /*
273 * Loads cached VBIOS data into legacy oprom location.
274 *
275 * Assumes user has called vbios_cache_is_valid() and checked for success
276 */
277 void vbios_load_from_cache(void)
278 {
279 /* copy cached vbios data from buffer to address used by FSP */
280 memcpy(vbt_get(), vbios_data, VBIOS_CACHE_FMAP_SIZE);
281
282 /* mark cache as used so we know not to write it later */
283 vbios_loaded_from_cache = true;
284 }
285
286 /*
287 * Return true if VBIOS cache data is valid
288 *
289 * Compare first 2 bytes of data with known signature
290 * and hash of data with hash stored in TPM NVRAM
291 */
292 bool vbios_cache_is_valid(void)
293 {
294 bool sig_valid = vbios_data[0] == 0x55 && vbios_data[1] == 0xaa;
295 return sig_valid && vbios_cache_verify_hash(vbios_data, VBIOS_CACHE_FMAP_SIZE) == CB_SUCCESS;
296 }
297
298 BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, read_vbios_cache_from_fmap, NULL);
299 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME_CHECK, BS_ON_ENTRY, write_vbios_cache_to_fmap, NULL);
300
301 const struct device_operations amd_graphics_ops = {
302 .read_resources = pci_dev_read_resources,
303 .set_resources = graphics_set_resources,
304 .enable_resources = pci_dev_enable_resources,
305 .init = graphics_dev_init,
306 .scan_bus = scan_static_bus,
307 .ops_pci = &pci_dev_ops_pci,
308 .write_acpi_tables = pci_rom_write_acpi_tables,
309 .acpi_fill_ssdt = graphics_fill_ssdt,
310 .acpi_name = graphics_acpi_name,
311 };