Kyösti Mälkki (kyosti.malkki@gmail.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/15255
-gerrit
commit a8bf8d583feb801724edfa9dcdc33ba9493e38d9 Author: Kyösti Mälkki kyosti.malkki@gmail.com Date: Tue Jun 28 07:38:46 2016 +0300
ACPI S3: Remove HIGH_MEMORY_SAVE where possible
Use actual requirement of ramstage size for S3 resume backup in CBMEM, unless ACPI_HUGE_LOWMEM_BACKUP is selected.
Change-Id: Ide7ce013f3727c2928cdb00fbcc7e7e84e859ff1 Signed-off-by: Kyösti Mälkki kyosti.malkki@gmail.com --- src/Kconfig | 4 + src/arch/x86/acpi_s3.c | 126 +++++++++++++++++++++++++----- src/arch/x86/include/arch/acpi.h | 8 +- src/cpu/amd/car/post_cache_as_ram.c | 7 +- src/cpu/amd/family_10h-family_15h/Kconfig | 1 + src/cpu/amd/model_fxx/Kconfig | 1 + src/include/cbfs.h | 3 + src/include/program_loading.h | 4 + src/lib/cbfs.c | 12 +++ src/lib/prog_loaders.c | 14 +++- 10 files changed, 151 insertions(+), 29 deletions(-)
diff --git a/src/Kconfig b/src/Kconfig index 7043851..b4c3cbe 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -516,6 +516,10 @@ config HAVE_ACPI_RESUME bool default n
+config ACPI_HUGE_LOWMEM_BACKUP + bool + default n + config RESUME_PATH_SAME_AS_BOOT bool default y if ARCH_X86 diff --git a/src/arch/x86/acpi_s3.c b/src/arch/x86/acpi_s3.c index bb52313..9d56c82 100644 --- a/src/arch/x86/acpi_s3.c +++ b/src/arch/x86/acpi_s3.c @@ -80,32 +80,114 @@ void acpi_fail_wakeup(void) } #endif /* ENV_RAMSTAGE */
-void acpi_prepare_for_resume(void) + +struct resume_backup { + u32 magic; + u32 base; + u32 size; + u8 buf[0]; +}; + +#define RESUME_BACKUP_MAGIC 0xCAFEBABE + +void backup_ramstage_section(u32 base, u32 size) { - if (!HIGH_MEMORY_SAVE) + struct resume_backup *backup_mem = cbmem_find(CBMEM_ID_RESUME); + if (backup_mem == NULL) + return; + + if (backup_mem->magic == RESUME_BACKUP_MAGIC) return;
+ size = ALIGN_UP(size, 4); + if (!size) { + base = backup_mem->base; + size = backup_mem->size; + } + + if (IS_ENABLED(CONFIG_ACPI_HUGE_LOWMEM_BACKUP)) { + base = CONFIG_RAMBASE; + size = HIGH_MEMORY_SAVE; + } + + /* FIXME: Sanity check CBMEM. */ + /* Back up the OS-controlled memory where ramstage will be loaded. */ - void *src = (void *)CONFIG_RAMBASE; - void *dest = cbmem_find(CBMEM_ID_RESUME); - if (dest != NULL) - memcpy(dest, src, HIGH_MEMORY_SAVE); + memcpy(&backup_mem->buf, (void*)base, size); + backup_mem->base = base; + backup_mem->size = size; + backup_mem->magic = RESUME_BACKUP_MAGIC; }
+/* Make backup of low-memory region, relying on the base and size + * of the ramstage that was loaded before entry to ACPI S3. + * + * DEPRECATED + */ +void acpi_prepare_for_resume(void) +{ + backup_ramstage_section(0, 0); +} + +void *acpi_backup_container(void) +{ + struct resume_backup *backup_mem = cbmem_find(CBMEM_ID_RESUME); + if (backup_mem == NULL) + return NULL; + + backup_mem->magic = 0; + return (void*)&backup_mem->buf; +} + +void acpi_backup_validate(u32 base, u32 size) +{ + struct resume_backup *backup_mem = cbmem_find(CBMEM_ID_RESUME); + if (backup_mem == NULL) + return; + + backup_mem->base = base; + backup_mem->size = size; + backup_mem->magic = RESUME_BACKUP_MAGIC; +} + +extern unsigned char _program, _eprogram; + void acpi_prepare_resume_backup(void) { + size_t size = (u32)(&_eprogram - &_program); + u32 base = (u32) &_program; + struct resume_backup *backup_mem; + + if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) + return; + if (!acpi_s3_resume_allowed()) return;
/* Let's prepare the ACPI S3 Resume area now already, so we can rely on - * it being there during reboot time. We don't need the pointer, nor - * the result right now. If it fails, ACPI resume will be disabled. + * it being there during reboot time. If this fails, ACPI resume will + * be disabled. We assume that ramstage does not change while in suspend, + * so base and size of the currently running ramstage will match the one + * we will load on resume path. */ if (IS_ENABLED(CONFIG_LATE_CBMEM_INIT)) romstage_ram_stack_maybe_low(0);
- if (HIGH_MEMORY_SAVE) - cbmem_add(CBMEM_ID_RESUME, HIGH_MEMORY_SAVE); + if (IS_ENABLED(CONFIG_ACPI_HUGE_LOWMEM_BACKUP)) { + base = CONFIG_RAMBASE; + size = HIGH_MEMORY_SAVE; + } + + size = ALIGN_UP(size, 4); + + size_t reserve = ALIGN_UP(sizeof(*backup_mem) + size, 1024); + backup_mem = cbmem_add(CBMEM_ID_RESUME, reserve); + if (backup_mem == NULL) + return; + + backup_mem->magic = 0; + backup_mem->base = base; + backup_mem->size = size; }
#define WAKEUP_BASE 0x600 @@ -118,21 +200,24 @@ extern unsigned int __wakeup_size;
static void acpi_jump_to_wakeup(void *vector) { - uintptr_t acpi_backup_memory = 0; + struct resume_backup *backup_mem; + u32 source = 0, target = 0, size = 0;
if (!acpi_s3_resume_allowed()) { printk(BIOS_WARNING, "ACPI: S3 resume not allowed.\n"); return; }
- if (HIGH_MEMORY_SAVE) { - acpi_backup_memory = (uintptr_t)cbmem_find(CBMEM_ID_RESUME); - - if (!acpi_backup_memory) { - printk(BIOS_WARNING, "ACPI: Backup memory missing. " - "No S3 resume.\n"); - return; - } + backup_mem = cbmem_find(CBMEM_ID_RESUME); + if (backup_mem && backup_mem->magic == RESUME_BACKUP_MAGIC) { + backup_mem->magic = 0; + source = (u32) &backup_mem->buf; + target = backup_mem->base; + size = backup_mem->size; + } else if (!IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) { + printk(BIOS_WARNING, "ACPI: Backup memory missing. " + "No S3 resume.\n"); + return; }
/* Copy wakeup trampoline in place. */ @@ -140,8 +225,7 @@ static void acpi_jump_to_wakeup(void *vector)
timestamp_add_now(TS_ACPI_WAKE_JUMP);
- acpi_do_wakeup((uintptr_t)vector, acpi_backup_memory, CONFIG_RAMBASE, - HIGH_MEMORY_SAVE); + acpi_do_wakeup((uintptr_t)vector, source, target, size); }
void __attribute__((weak)) mainboard_suspend_resume(void) diff --git a/src/arch/x86/include/arch/acpi.h b/src/arch/x86/include/arch/acpi.h index 7434000..6bb1a8c 100644 --- a/src/arch/x86/include/arch/acpi.h +++ b/src/arch/x86/include/arch/acpi.h @@ -24,12 +24,7 @@ #ifndef __ASM_ACPI_H #define __ASM_ACPI_H
-#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) && \ - ! IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE) #define HIGH_MEMORY_SAVE (CONFIG_RAMTOP - CONFIG_RAMBASE) -#else -#define HIGH_MEMORY_SAVE 0 -#endif
#ifndef __ASSEMBLER__ #include <stdint.h> @@ -623,6 +618,9 @@ static inline int acpi_s3_resume_allowed(void) return IS_ENABLED(CONFIG_HAVE_ACPI_RESUME); }
+void *acpi_backup_container(void); +void acpi_backup_validate(u32 base, u32 size); + #if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME) extern int acpi_slp_type;
diff --git a/src/cpu/amd/car/post_cache_as_ram.c b/src/cpu/amd/car/post_cache_as_ram.c index 15b2642..ce1f930 100644 --- a/src/cpu/amd/car/post_cache_as_ram.c +++ b/src/cpu/amd/car/post_cache_as_ram.c @@ -69,7 +69,7 @@ static void prepare_romstage_ramstack(int s3resume) print_car_debug("Prepare CAR migration and stack regions...");
if (s3resume) { - void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME); + void *resume_backup_memory = acpi_backup_container(); if (resume_backup_memory) memcpy_(resume_backup_memory + HIGH_MEMORY_SAVE - backup_top, (void *)(CONFIG_RAMTOP - backup_top), backup_top); @@ -85,7 +85,7 @@ static void prepare_ramstage_region(int s3resume) print_car_debug("Prepare ramstage memory region...");
if (s3resume) { - void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME); + void *resume_backup_memory = acpi_backup_container(); if (resume_backup_memory) memcpy_(resume_backup_memory, (void *) CONFIG_RAMBASE, HIGH_MEMORY_SAVE - backup_top); @@ -102,6 +102,9 @@ static void prepare_ramstage_region(int s3resume) #endif
print_car_debug(" Done\n"); + + if (s3resume) + acpi_backup_validate(CONFIG_RAMBASE, HIGH_MEMORY_SAVE); }
/* Disable Erratum 343 Workaround, see RevGuide for Fam10h, Pub#41322 Rev 3.33 diff --git a/src/cpu/amd/family_10h-family_15h/Kconfig b/src/cpu/amd/family_10h-family_15h/Kconfig index fda7fc8..30c7dee 100644 --- a/src/cpu/amd/family_10h-family_15h/Kconfig +++ b/src/cpu/amd/family_10h-family_15h/Kconfig @@ -10,6 +10,7 @@ config CPU_AMD_MODEL_10XXX select TSC_SYNC_LFENCE select UDELAY_LAPIC select HAVE_MONOTONIC_TIMER + select ACPI_HUGE_LOWMEM_BACKUP select SUPPORT_CPU_UCODE_IN_CBFS select CPU_MICROCODE_MULTIPLE_FILES if !CPU_MICROCODE_CBFS_NONE
diff --git a/src/cpu/amd/model_fxx/Kconfig b/src/cpu/amd/model_fxx/Kconfig index c8bae33..3eb5b94 100644 --- a/src/cpu/amd/model_fxx/Kconfig +++ b/src/cpu/amd/model_fxx/Kconfig @@ -9,6 +9,7 @@ config CPU_AMD_MODEL_FXX select SSE2 select TSC_SYNC_LFENCE select UDELAY_LAPIC + select ACPI_HUGE_LOWMEM_BACKUP select SUPPORT_CPU_UCODE_IN_CBFS
if CPU_AMD_MODEL_FXX diff --git a/src/include/cbfs.h b/src/include/cbfs.h index 2d19218..6a9e054 100644 --- a/src/include/cbfs.h +++ b/src/include/cbfs.h @@ -43,6 +43,9 @@ void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size); size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, size_t in_size, void *buffer, size_t buffer_size, uint32_t compression);
+/* Fill base and size of the memory pstage will occupy when loaded. */ +void cbfs_prog_stage_section(struct prog *pstage, uint32_t *base, uint32_t *size); + /* Load stage into memory filling in prog. Return 0 on success. < 0 on error. */ int cbfs_prog_stage_load(struct prog *prog);
diff --git a/src/include/program_loading.h b/src/include/program_loading.h index 929d60c..269f812 100644 --- a/src/include/program_loading.h +++ b/src/include/program_loading.h @@ -175,6 +175,10 @@ unsigned long romstage_ram_stack_maybe_low(int no_cbmem); unsigned long romstage_ram_stack(void); extern const unsigned long romstage_ram_stack_size;
+/* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path, + * if ramstage overwrites low memory. */ +void backup_ramstage_section(u32 base, u32 size); + /*********************** * PAYLOAD LOADING * ***********************/ diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c index aa652c2..c00e6aa 100644 --- a/src/lib/cbfs.c +++ b/src/lib/cbfs.c @@ -166,6 +166,18 @@ void *cbfs_boot_load_stage_by_name(const char *name) return prog_entry(&stage); }
+void cbfs_prog_stage_section(struct prog *pstage, uint32_t *base, uint32_t *size) +{ + struct cbfs_stage stage; + const struct region_device *fh = prog_rdev(pstage); + + if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) + return; + + *base = stage.load; + *size = stage.memlen; +} + int cbfs_prog_stage_load(struct prog *pstage) { struct cbfs_stage stage; diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c index ecbc679..d659abe 100644 --- a/src/lib/prog_loaders.c +++ b/src/lib/prog_loaders.c @@ -101,6 +101,18 @@ static int load_relocatable_ramstage(struct prog *ramstage) return rmodule_stage_load(&rmod_ram); }
+static int load_nonrelocatable_ramstage(struct prog *ramstage) +{ + if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) { + uint32_t base = 0, size = 0; + cbfs_prog_stage_section(ramstage, &base, &size); + if (size) + backup_ramstage_section(base, size); + } + + return cbfs_prog_stage_load(ramstage); +} + void run_ramstage(void) { struct prog ramstage = @@ -126,7 +138,7 @@ void run_ramstage(void) if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) { if (load_relocatable_ramstage(&ramstage)) goto fail; - } else if (cbfs_prog_stage_load(&ramstage)) + } else if (load_nonrelocatable_ramstage(&ramstage)) goto fail;
stage_cache_add(STAGE_RAMSTAGE, &ramstage);