Damien Roth | bedbd67 | 2016-01-18 13:59:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, |
| 5 | * Raptor Engineering |
Patrick Georgi | 5b2a2d0 | 2018-09-26 20:46:04 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012 Google LLC |
Timothy Pearson | 1c4508e | 2015-09-05 17:50:29 -0500 | [diff] [blame] | 7 | * 2005.6 by yhlu |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 8 | * 2006.3 yhlu add copy data from CAR to ram |
Damien Roth | bedbd67 | 2016-01-18 13:59:26 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 18 | */ |
Patrick Georgi | 3d5bb23 | 2010-05-09 21:15:13 +0000 | [diff] [blame] | 19 | #include <string.h> |
Damien Zammit | 75a3d1f | 2016-11-28 00:29:10 +1100 | [diff] [blame] | 20 | #include <console/console.h> |
Stefan Reinauer | aa987b2 | 2010-04-09 13:31:07 +0000 | [diff] [blame] | 21 | #include <arch/stages.h> |
Aaron Durbin | 0dc7354 | 2015-07-16 16:07:02 -0500 | [diff] [blame] | 22 | #include <arch/early_variables.h> |
Patrick Georgi | c928a29 | 2010-05-14 11:02:56 +0000 | [diff] [blame] | 23 | #include <cpu/x86/mtrr.h> |
Kyösti Mälkki | 88a67f0 | 2013-12-12 12:27:53 +0200 | [diff] [blame] | 24 | #include <cpu/amd/mtrr.h> |
Kyösti Mälkki | 2458f42 | 2014-04-22 16:46:31 +0300 | [diff] [blame] | 25 | #include <cpu/amd/car.h> |
Damien Zammit | 75a3d1f | 2016-11-28 00:29:10 +1100 | [diff] [blame] | 26 | #include <cpu/amd/msr.h> |
Kyösti Mälkki | 207880c | 2013-12-10 09:03:17 +0200 | [diff] [blame] | 27 | #include <arch/acpi.h> |
Timothy Pearson | 2003844 | 2015-09-05 18:46:24 -0500 | [diff] [blame] | 28 | #include <romstage_handoff.h> |
Kyösti Mälkki | b98391c | 2017-07-13 13:14:16 +0300 | [diff] [blame] | 29 | |
Stefan Reinauer | 806e146 | 2005-12-01 10:54:44 +0000 | [diff] [blame] | 30 | #include "cpu/amd/car/disable_cache_as_ram.c" |
| 31 | |
Kyösti Mälkki | b98391c | 2017-07-13 13:14:16 +0300 | [diff] [blame] | 32 | // For set_sysinfo_in_ram() |
Elyes HAOUAS | 65bb543 | 2018-07-03 14:59:50 +0200 | [diff] [blame] | 33 | #include <northbridge/amd/amdfam10/raminit.h> |
Kyösti Mälkki | b98391c | 2017-07-13 13:14:16 +0300 | [diff] [blame] | 34 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 35 | #if CONFIG_RAMTOP <= 0x100000 |
| 36 | #error "You need to set CONFIG_RAMTOP greater than 1M" |
| 37 | #endif |
| 38 | |
Timothy Pearson | d657446 | 2015-10-24 14:09:49 -0500 | [diff] [blame] | 39 | #if IS_ENABLED(CONFIG_DEBUG_CAR) |
| 40 | #define print_car_debug(format, arg...) printk(BIOS_DEBUG, "%s: " format, __func__, ##arg) |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 41 | #else |
Timothy Pearson | d657446 | 2015-10-24 14:09:49 -0500 | [diff] [blame] | 42 | #define print_car_debug(format, arg...) |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 45 | static size_t backup_size(void) |
Ronald G. Minnich | fb0a64b | 2005-11-23 21:01:08 +0000 | [diff] [blame] | 46 | { |
Aaron Durbin | 0dc7354 | 2015-07-16 16:07:02 -0500 | [diff] [blame] | 47 | size_t car_size = car_data_size(); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 48 | return ALIGN(car_size + 1024, 1024); |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 49 | } |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 50 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 51 | static void memcpy_(void *d, const void *s, size_t len) |
| 52 | { |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 53 | print_car_debug(" Copy [%08x-%08x] to [%08x - %08x] ...", |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 54 | (uint32_t) s, (uint32_t) (s + len - 1), |
| 55 | (uint32_t) d, (uint32_t) (d + len - 1)); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 56 | memcpy(d, s, len); |
| 57 | } |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 58 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 59 | static void memset_(void *d, int val, size_t len) |
| 60 | { |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 61 | print_car_debug(" Fill [%08x-%08x] ...", |
| 62 | (uint32_t) d, (uint32_t) (d + len - 1)); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 63 | memset(d, val, len); |
| 64 | } |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 65 | |
Timothy Pearson | 1c4508e | 2015-09-05 17:50:29 -0500 | [diff] [blame] | 66 | static int memcmp_(void *d, const void *s, size_t len) |
| 67 | { |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 68 | print_car_debug(" Compare [%08x-%08x] with [%08x - %08x] ...", |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 69 | (uint32_t) s, (uint32_t) (s + len - 1), |
| 70 | (uint32_t) d, (uint32_t) (d + len - 1)); |
Timothy Pearson | 1c4508e | 2015-09-05 17:50:29 -0500 | [diff] [blame] | 71 | return memcmp(d, s, len); |
| 72 | } |
| 73 | |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 74 | static void prepare_romstage_ramstack(int s3resume) |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 75 | { |
| 76 | size_t backup_top = backup_size(); |
| 77 | print_car_debug("Prepare CAR migration and stack regions..."); |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 78 | |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 79 | if (s3resume) { |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 80 | void *resume_backup_memory = |
| 81 | acpi_backup_container(CONFIG_RAMBASE, HIGH_MEMORY_SAVE); |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 82 | if (resume_backup_memory) |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 83 | memcpy_(resume_backup_memory |
| 84 | + HIGH_MEMORY_SAVE - backup_top, |
| 85 | (void *)(CONFIG_RAMTOP - backup_top), |
| 86 | backup_top); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 87 | } |
| 88 | memset_((void *)(CONFIG_RAMTOP - backup_top), 0, backup_top); |
| 89 | |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 90 | print_car_debug(" Done\n"); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 93 | static void prepare_ramstage_region(int s3resume) |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 94 | { |
| 95 | size_t backup_top = backup_size(); |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 96 | print_car_debug("Prepare ramstage memory region..."); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 97 | |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 98 | if (s3resume) { |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 99 | void *resume_backup_memory = |
| 100 | acpi_backup_container(CONFIG_RAMBASE, HIGH_MEMORY_SAVE); |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 101 | if (resume_backup_memory) |
| 102 | memcpy_(resume_backup_memory, (void *) CONFIG_RAMBASE, |
| 103 | HIGH_MEMORY_SAVE - backup_top); |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 106 | print_car_debug(" Done\n"); |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 107 | } |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 108 | |
Jonathan A. Kollasch | 20d9fb4 | 2015-11-24 10:15:22 -0600 | [diff] [blame] | 109 | /* Disable Erratum 343 Workaround, see RevGuide for Fam10h, Pub#41322 Rev 3.33 |
| 110 | * and RevGuide for Fam12h, Pub#44739 Rev 3.10 |
| 111 | */ |
Ronald G. Minnich | fb0a64b | 2005-11-23 21:01:08 +0000 | [diff] [blame] | 112 | |
Marco Schmidt | c263b44 | 2009-06-06 11:21:52 +0000 | [diff] [blame] | 113 | static void vErrata343(void) |
| 114 | { |
Timothy Pearson | 730a043 | 2015-10-16 13:51:51 -0500 | [diff] [blame] | 115 | msr_t msr; |
| 116 | unsigned int uiMask = 0xFFFFFFF7; |
Marco Schmidt | c263b44 | 2009-06-06 11:21:52 +0000 | [diff] [blame] | 117 | |
Timothy Pearson | 730a043 | 2015-10-16 13:51:51 -0500 | [diff] [blame] | 118 | msr = rdmsr(BU_CFG2_MSR); |
| 119 | msr.hi &= uiMask; // IcDisSpecTlbWr (bit 35) = 0 |
| 120 | wrmsr(BU_CFG2_MSR, msr); |
Marco Schmidt | c263b44 | 2009-06-06 11:21:52 +0000 | [diff] [blame] | 121 | } |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 122 | |
Elyes HAOUAS | 6c9737b | 2018-07-08 12:30:02 +0200 | [diff] [blame] | 123 | asmlinkage void *post_cache_as_ram(void) |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 124 | { |
Timothy Pearson | 730a043 | 2015-10-16 13:51:51 -0500 | [diff] [blame] | 125 | uint32_t family = amd_fam1x_cpu_family(); |
Kyösti Mälkki | 916b331 | 2017-09-04 23:43:30 +0300 | [diff] [blame] | 126 | int s3resume = 0; |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 127 | |
Timothy Pearson | 1f78099 | 2015-06-01 23:58:59 -0500 | [diff] [blame] | 128 | /* Verify that the BSP didn't overrun the lower stack |
| 129 | * boundary during romstage execution |
| 130 | */ |
| 131 | volatile uint32_t *lower_stack_boundary; |
Evelyn Huang | acd02b5 | 2017-05-30 15:35:22 -0600 | [diff] [blame] | 132 | lower_stack_boundary = |
| 133 | (void *)((CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE) |
| 134 | - CONFIG_DCACHE_BSP_STACK_SIZE); |
Timothy Pearson | 1f78099 | 2015-06-01 23:58:59 -0500 | [diff] [blame] | 135 | if ((*lower_stack_boundary) != 0xdeadbeef) |
| 136 | printk(BIOS_WARNING, "BSP overran lower stack boundary. Undefined behaviour may result!\n"); |
| 137 | |
Kyösti Mälkki | e0e1e64 | 2018-06-03 06:53:30 +0300 | [diff] [blame] | 138 | s3resume = acpi_is_wakeup_s3(); |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 139 | |
| 140 | prepare_romstage_ramstack(s3resume); |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 141 | |
Aaron Durbin | 77e1399 | 2016-11-29 17:43:04 -0600 | [diff] [blame] | 142 | romstage_handoff_init(s3resume); |
Kyösti Mälkki | d113190 | 2016-06-26 15:44:05 +0300 | [diff] [blame] | 143 | |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 144 | /* from here don't store more data in CAR */ |
Jonathan A. Kollasch | 20d9fb4 | 2015-11-24 10:15:22 -0600 | [diff] [blame] | 145 | if (family >= 0x1f && family <= 0x3f) { |
| 146 | /* Family 10h and 12h, 11h until shown otherwise */ |
Timothy Pearson | 730a043 | 2015-10-16 13:51:51 -0500 | [diff] [blame] | 147 | vErrata343(); |
| 148 | } |
Marco Schmidt | c263b44 | 2009-06-06 11:21:52 +0000 | [diff] [blame] | 149 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 150 | size_t car_size = car_data_size(); |
| 151 | void *migrated_car = (void *)(CONFIG_RAMTOP - car_size); |
| 152 | |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 153 | print_car_debug("Copying data from cache to RAM..."); |
Andrey Petrov | dd56de9 | 2016-02-25 17:22:17 -0800 | [diff] [blame] | 154 | memcpy_(migrated_car, _car_relocatable_data_start, car_size); |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 155 | print_car_debug(" Done\n"); |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 156 | |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 157 | print_car_debug("Verifying data integrity in RAM..."); |
Andrey Petrov | dd56de9 | 2016-02-25 17:22:17 -0800 | [diff] [blame] | 158 | if (memcmp_(migrated_car, _car_relocatable_data_start, car_size) == 0) |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 159 | print_car_debug(" Done\n"); |
Timothy Pearson | 1c4508e | 2015-09-05 17:50:29 -0500 | [diff] [blame] | 160 | else |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 161 | print_car_debug(" FAILED\n"); |
Timothy Pearson | 1c4508e | 2015-09-05 17:50:29 -0500 | [diff] [blame] | 162 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 163 | /* New stack grows right below migrated_car. */ |
Paul Menzel | 42b6265 | 2015-11-07 09:15:06 +0100 | [diff] [blame] | 164 | print_car_debug("Switching to use RAM as stack..."); |
Kyösti Mälkki | 1438245 | 2017-07-13 14:11:52 +0300 | [diff] [blame] | 165 | return migrated_car; |
Vladimir Serbinenko | a6c29fe | 2013-11-26 17:49:29 +0100 | [diff] [blame] | 166 | } |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 167 | |
Kyösti Mälkki | 1438245 | 2017-07-13 14:11:52 +0300 | [diff] [blame] | 168 | asmlinkage void cache_as_ram_new_stack(void) |
Vladimir Serbinenko | a6c29fe | 2013-11-26 17:49:29 +0100 | [diff] [blame] | 169 | { |
Elyes HAOUAS | 585d1a0 | 2016-07-28 19:15:34 +0200 | [diff] [blame] | 170 | print_car_debug("Disabling cache as RAM now\n"); |
Kyösti Mälkki | b98391c | 2017-07-13 13:14:16 +0300 | [diff] [blame] | 171 | disable_cache_as_ram_real(0); // inline |
Ronald G. Minnich | fb0a64b | 2005-11-23 21:01:08 +0000 | [diff] [blame] | 172 | |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 173 | disable_cache(); |
Kyösti Mälkki | 65cc526 | 2016-06-19 20:38:41 +0300 | [diff] [blame] | 174 | /* Enable cached access to RAM in the range 0M to CACHE_TMP_RAMTOP */ |
| 175 | set_var_mtrr(0, 0x00000000, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK); |
Rudolf Marek | bcaea14 | 2010-11-22 22:00:52 +0000 | [diff] [blame] | 176 | enable_cache(); |
| 177 | |
Kyösti Mälkki | c30bfca | 2016-06-28 07:38:03 +0300 | [diff] [blame] | 178 | prepare_ramstage_region(acpi_is_wakeup_s3()); |
Stefan Reinauer | 806e146 | 2005-12-01 10:54:44 +0000 | [diff] [blame] | 179 | |
Myles Watson | f326e3a | 2010-03-11 22:12:10 +0000 | [diff] [blame] | 180 | set_sysinfo_in_ram(1); // So other core0 could start to train mem |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 181 | |
Furquan Shaikh | 20f25dd | 2014-04-22 10:41:05 -0700 | [diff] [blame] | 182 | /*copy and execute ramstage */ |
Stefan Reinauer | 648d166 | 2013-05-06 18:05:39 -0700 | [diff] [blame] | 183 | copy_and_run(); |
Myles Watson | f326e3a | 2010-03-11 22:12:10 +0000 | [diff] [blame] | 184 | /* We will not return */ |
Ronald G. Minnich | fb0a64b | 2005-11-23 21:01:08 +0000 | [diff] [blame] | 185 | |
Kyösti Mälkki | abc083e | 2013-12-29 12:07:54 +0200 | [diff] [blame] | 186 | print_car_debug("should not be here -\n"); |
Ronald G. Minnich | fb0a64b | 2005-11-23 21:01:08 +0000 | [diff] [blame] | 187 | } |