zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 18 | */ |
| 19 | |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 20 | #include <console/console.h> |
| 21 | #include <cpu/x86/msr.h> |
| 22 | #include <cpu/x86/mtrr.h> |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 23 | #include <cpu/amd/car.h> |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 24 | #include <cpu/amd/mtrr.h> |
| 25 | #include <cpu/x86/cache.h> |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 26 | #include <cbmem.h> |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 27 | #include <string.h> |
Kyösti Mälkki | 920d17c | 2015-05-26 00:10:52 +0300 | [diff] [blame] | 28 | #include <halt.h> |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 29 | #include "s3_resume.h" |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 30 | |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 31 | static void *backup_resume(void) |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 32 | { |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 33 | void *resume_backup_memory; |
| 34 | |
Kyösti Mälkki | 920d17c | 2015-05-26 00:10:52 +0300 | [diff] [blame] | 35 | printk(BIOS_DEBUG, "Find resume memory location\n"); |
| 36 | |
| 37 | if (cbmem_recovery(1)) { |
| 38 | printk(BIOS_EMERG, "Unable to recover CBMEM\n"); |
| 39 | halt(); |
| 40 | } |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 41 | |
| 42 | resume_backup_memory = cbmem_find(CBMEM_ID_RESUME); |
Kyösti Mälkki | 920d17c | 2015-05-26 00:10:52 +0300 | [diff] [blame] | 43 | if (resume_backup_memory == NULL) { |
| 44 | printk(BIOS_EMERG, "No storage for low-memory backup\n"); |
| 45 | halt(); |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return resume_backup_memory; |
| 49 | } |
| 50 | |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 51 | static void move_stack_high_mem(void) |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 52 | { |
Kyösti Mälkki | f728408 | 2015-05-26 10:12:45 +0300 | [diff] [blame] | 53 | void *high_stack = cbmem_find(CBMEM_ID_ROMSTAGE_RAM_STACK); |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 54 | |
Kyösti Mälkki | f728408 | 2015-05-26 10:12:45 +0300 | [diff] [blame] | 55 | /* TODO: Make the switch with empty stack instead. */ |
| 56 | memcpy(high_stack, (void *)BSP_STACK_BASE_ADDR, HIGH_ROMSTAGE_STACK_SIZE); |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 57 | |
Kyösti Mälkki | f728408 | 2015-05-26 10:12:45 +0300 | [diff] [blame] | 58 | /* TODO: We only switch stack on BSP. */ |
Stefan Reinauer | 67b9430 | 2015-06-18 01:14:01 -0700 | [diff] [blame^] | 59 | #ifdef __x86_64__ |
| 60 | __asm__ |
| 61 | volatile ("add %0, %%rsp; add %0, %%rbp; invd"::"g" |
| 62 | (high_stack - BSP_STACK_BASE_ADDR) |
| 63 | :); |
| 64 | #else |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 65 | __asm__ |
| 66 | volatile ("add %0, %%esp; add %0, %%ebp; invd"::"g" |
| 67 | (high_stack - BSP_STACK_BASE_ADDR) |
| 68 | :); |
Stefan Reinauer | 67b9430 | 2015-06-18 01:14:01 -0700 | [diff] [blame^] | 69 | #endif |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 70 | } |
| 71 | |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 72 | static void set_resume_cache(void) |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 73 | { |
| 74 | msr_t msr; |
| 75 | |
| 76 | /* disable fixed mtrr for now, it will be enabled by mtrr restore */ |
| 77 | msr = rdmsr(SYSCFG_MSR); |
| 78 | msr.lo &= ~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrFixDramModEn); |
| 79 | wrmsr(SYSCFG_MSR, msr); |
| 80 | |
| 81 | /* Enable caching for 0 - coreboot ram using variable mtrr */ |
| 82 | msr.lo = 0 | MTRR_TYPE_WRBACK; |
| 83 | msr.hi = 0; |
| 84 | wrmsr(MTRRphysBase_MSR(0), msr); |
| 85 | msr.lo = ~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid; |
| 86 | msr.hi = (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1; |
| 87 | wrmsr(MTRRphysMask_MSR(0), msr); |
| 88 | |
| 89 | /* Set the default memory type and disable fixed and enable variable MTRRs */ |
| 90 | msr.hi = 0; |
| 91 | msr.lo = (1 << 11); |
| 92 | wrmsr(MTRRdefType_MSR, msr); |
| 93 | |
| 94 | enable_cache(); |
| 95 | } |
| 96 | |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 97 | void prepare_for_resume(void) |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 98 | { |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 99 | void *resume_backup_memory = backup_resume(); |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 100 | |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 101 | post_code(0x62); |
| 102 | printk(BIOS_DEBUG, "Move CAR stack.\n"); |
| 103 | move_stack_high_mem(); |
Kyösti Mälkki | 23b4f0c | 2014-06-18 09:55:26 +0300 | [diff] [blame] | 104 | |
| 105 | post_code(0x63); |
| 106 | disable_cache_as_ram(); |
| 107 | printk(BIOS_DEBUG, "CAR disabled.\n"); |
| 108 | set_resume_cache(); |
| 109 | |
| 110 | /* |
| 111 | * Copy the system memory that is in the ramstage area to the |
| 112 | * reserved area. |
| 113 | */ |
| 114 | if (resume_backup_memory) |
| 115 | memcpy(resume_backup_memory, (void *)(CONFIG_RAMBASE), HIGH_MEMORY_SAVE); |
| 116 | |
| 117 | printk(BIOS_DEBUG, "System memory saved. OK to load ramstage.\n"); |
zbao | f722373 | 2012-04-13 13:42:15 +0800 | [diff] [blame] | 118 | } |