| 1 |
|
| 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; |
| 24 |
uint16_t version; |
| 25 |
uint32_t supported_notifications; |
| 26 |
uint32_t supported_functions; |
| 27 |
}; |
| 28 |
|
| 29 |
#define ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS 0x10 |
| 30 |
# define ATIF_QBTC_REQUEST_LCD1 0 |
| 31 |
|
| 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 |
|
| 38 |
uint8_t requested_display; |
| 39 |
}; |
| 40 |
struct atif_brightness_output { |
| 41 |
uint16_t size; |
| 42 |
uint16_t flags; |
| 43 |
uint8_t error_code; |
| 44 |
|
| 45 |
uint8_t default_brightness_ac; |
| 46 |
uint8_t default_brightness_dc; |
| 47 |
|
| 48 |
uint8_t min_input_signal_level; |
| 49 |
uint8_t max_input_signal_level; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
uint8_t count_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 |
|
| 77 |
acpigen_write_scope(acpi_device_path(dev)); |
| 78 |
|
| 79 |
acpigen_write_method("ATIF", 2); |
| 80 |
|
| 81 |
acpigen_write_to_integer(ARG0_OP, LOCAL0_OP); |
| 82 |
|
| 83 |
|
| 84 |
acpigen_write_if_lequal_op_int(LOCAL0_OP, ATIF_FUNCTION_VERIFY_INTERFACE); |
| 85 |
|
| 86 |
acpigen_write_return_byte_buffer((uint8_t *)(void *)&verify_output, |
| 87 |
sizeof(verify_output)); |
| 88 |
|
| 89 |
|
| 90 |
acpigen_write_else(); |
| 91 |
acpigen_write_if_lequal_op_int(LOCAL0_OP, |
| 92 |
ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS); |
| 93 |
|
| 94 |
acpigen_write_create_byte_field(ARG1_OP, |
| 95 |
offsetof(struct atif_brightness_input, requested_display), "DISP"); |
| 96 |
|
| 97 |
acpigen_write_to_integer_from_namestring("DISP", LOCAL1_OP); |
| 98 |
|
| 99 |
acpigen_write_if_lequal_op_int(LOCAL1_OP, ATIF_QBTC_REQUEST_LCD1); |
| 100 |
|
| 101 |
acpigen_write_return_byte_buffer((uint8_t *)(void *)&brightness_out, |
| 102 |
sizeof(brightness_out)); |
| 103 |
|
| 104 |
acpigen_write_else(); |
| 105 |
|
| 106 |
acpigen_write_return_byte_buffer((uint8_t *)(void *)&brightness_error, |
| 107 |
sizeof(brightness_error)); |
| 108 |
acpigen_pop_len(); |
| 109 |
|
| 110 |
acpigen_pop_len(); |
| 111 |
acpigen_pop_len(); |
| 112 |
|
| 113 |
acpigen_pop_len(); |
| 114 |
acpigen_pop_len(); |
| 115 |
} |
| 116 |
|
| 117 |
static void generate_acp(const struct device *dev) |
| 118 |
{ |
| 119 |
|
| 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(); |
| 126 |
acpigen_pop_len(); |
| 127 |
} |
| 128 |
|
| 129 |
static void graphics_fill_ssdt(const struct device *dev) |
| 130 |
{ |
| 131 |
acpi_device_write_pci_dev(dev); |
| 132 |
|
| 133 |
|
| 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 |
|
| 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 |
|
| 180 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 264 |
memcpy(vbios_data, vbt_get(), VBIOS_CACHE_FMAP_SIZE); |
| 265 |
|
| 266 |
|
| 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 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
void vbios_load_from_cache(void) |
| 278 |
{ |
| 279 |
|
| 280 |
memcpy(vbt_get(), vbios_data, VBIOS_CACHE_FMAP_SIZE); |
| 281 |
|
| 282 |
|
| 283 |
vbios_loaded_from_cache = true; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 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 |
}; |