Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google, 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. |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <string.h> |
| 17 | #include <stddef.h> |
| 18 | #include <console/console.h> |
| 19 | #include <cbmem.h> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 20 | #include <arch/early_variables.h> |
Ben Gardner | e597e63 | 2015-11-23 20:47:59 -0600 | [diff] [blame] | 21 | #include <symbols.h> |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 22 | |
Marc Jones | 7868797 | 2015-04-22 23:16:31 -0600 | [diff] [blame] | 23 | #if IS_ENABLED(CONFIG_PLATFORM_USES_FSP1_0) |
| 24 | #include <drivers/intel/fsp1_0/fsp_util.h> |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 25 | #endif |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 26 | typedef void (* const car_migration_func_t)(void); |
| 27 | |
| 28 | extern car_migration_func_t _car_migrate_start; |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 29 | |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 30 | /* |
| 31 | * The car_migrated global variable determines if the cache-as-ram space has |
Martin Roth | 4c3ab73 | 2013-07-08 16:23:54 -0600 | [diff] [blame] | 32 | * been migrated to real RAM. It does this by assuming the following things: |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 33 | * 1. cache-as-ram space is zero'd out once it is set up. |
| 34 | * 2. Either the cache-as-ram space is memory-backed after getting torn down |
| 35 | * or the space returns 0xff's for each byte read. |
| 36 | * Based on these 2 attributes there is the ability to tell when the |
| 37 | * cache-as-ram region has been migrated. |
| 38 | */ |
| 39 | static int car_migrated CAR_GLOBAL; |
| 40 | |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 41 | /** @brief returns pointer to a CAR variable, before or after migration. |
| 42 | * |
| 43 | * @param var pointer to the CAR variable |
| 44 | */ |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 45 | void *car_get_var_ptr(void *var) |
| 46 | { |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 47 | char *migrated_base = NULL; |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 48 | int offset; |
Andrey Petrov | dd56de9 | 2016-02-25 17:22:17 -0800 | [diff] [blame] | 49 | void * _car_start = _car_relocatable_data_start; |
| 50 | void * _car_end = _car_relocatable_data_end; |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 51 | |
| 52 | /* If the cache-as-ram has not been migrated return the pointer |
| 53 | * passed in. */ |
| 54 | if (!car_migrated) |
| 55 | return var; |
| 56 | |
| 57 | if (var < _car_start || var >= _car_end) { |
| 58 | printk(BIOS_ERR, |
| 59 | "Requesting CAR variable outside of CAR region: %p\n", |
| 60 | var); |
| 61 | return var; |
| 62 | } |
| 63 | |
Marc Jones | 7868797 | 2015-04-22 23:16:31 -0600 | [diff] [blame] | 64 | #if IS_ENABLED(CONFIG_PLATFORM_USES_FSP1_0) |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 65 | migrated_base=(char *)find_saved_temp_mem( |
| 66 | *(void **)CBMEM_FSP_HOB_PTR); |
Ben Gardner | e597e63 | 2015-11-23 20:47:59 -0600 | [diff] [blame] | 67 | /* FSP 1.0 migrates the entire DCACHE RAM */ |
| 68 | offset = (char *)var - (char *)CONFIG_DCACHE_RAM_BASE; |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 69 | #else |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 70 | migrated_base = cbmem_find(CBMEM_ID_CAR_GLOBALS); |
Ben Gardner | e597e63 | 2015-11-23 20:47:59 -0600 | [diff] [blame] | 71 | offset = (char *)var - (char *)_car_start; |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 72 | #endif |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 73 | |
Martin Roth | 582b2ae | 2015-01-11 14:29:29 -0700 | [diff] [blame] | 74 | if (migrated_base == NULL) |
| 75 | die( "CAR: Could not find migration base!\n"); |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 76 | |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 77 | return &migrated_base[offset]; |
| 78 | } |
| 79 | |
Kyösti Mälkki | e455425 | 2014-12-31 18:34:59 +0200 | [diff] [blame] | 80 | /* |
| 81 | * Update a CAR_GLOBAL variable var, originally pointing to CAR region, |
| 82 | * with the address in migrated CAR region in DRAM. |
| 83 | */ |
| 84 | void *car_sync_var_ptr(void *var) |
| 85 | { |
| 86 | void ** mig_var = car_get_var_ptr(var); |
Andrey Petrov | dd56de9 | 2016-02-25 17:22:17 -0800 | [diff] [blame] | 87 | void * _car_start = _car_relocatable_data_start; |
| 88 | void * _car_end = _car_relocatable_data_end; |
Kyösti Mälkki | e455425 | 2014-12-31 18:34:59 +0200 | [diff] [blame] | 89 | |
| 90 | /* Not moved or migrated yet. */ |
| 91 | if (mig_var == var) |
| 92 | return mig_var; |
| 93 | |
Ben Gardner | e597e63 | 2015-11-23 20:47:59 -0600 | [diff] [blame] | 94 | /* |
| 95 | * Migrate the cbmem console pointer for FSP 1.0 platforms. Otherwise, |
| 96 | * keep console buffer in CAR until cbmemc_reinit() moves it. |
| 97 | */ |
| 98 | if (*mig_var == _preram_cbmem_console) { |
| 99 | if (IS_ENABLED(CONFIG_PLATFORM_USES_FSP1_0)) |
| 100 | *mig_var += (char *)mig_var - (char *)var; |
| 101 | return mig_var; |
| 102 | } |
| 103 | |
Kyösti Mälkki | e455425 | 2014-12-31 18:34:59 +0200 | [diff] [blame] | 104 | /* It's already pointing outside car.global_data. */ |
| 105 | if (*mig_var < _car_start || *mig_var > _car_end) |
| 106 | return mig_var; |
| 107 | |
Kyösti Mälkki | e455425 | 2014-12-31 18:34:59 +0200 | [diff] [blame] | 108 | /* Move the pointer by the same amount the variable storing it was |
| 109 | * moved by. |
| 110 | */ |
| 111 | *mig_var += (char *)mig_var - (char *)var; |
| 112 | |
| 113 | return mig_var; |
| 114 | } |
| 115 | |
Kyösti Mälkki | 87acccc | 2014-12-19 09:19:29 +0200 | [diff] [blame] | 116 | static void do_car_migrate_variables(void) |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 117 | { |
| 118 | void *migrated_base; |
Aaron Durbin | 0dc7354 | 2015-07-16 16:07:02 -0500 | [diff] [blame] | 119 | size_t car_size = car_data_size(); |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 120 | |
Aaron Durbin | 7f5897a | 2013-06-21 18:02:26 +0300 | [diff] [blame] | 121 | /* Check if already migrated. */ |
| 122 | if (car_migrated) |
| 123 | return; |
| 124 | |
Aaron Durbin | 0dc7354 | 2015-07-16 16:07:02 -0500 | [diff] [blame] | 125 | migrated_base = cbmem_add(CBMEM_ID_CAR_GLOBALS, car_size); |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 126 | |
| 127 | if (migrated_base == NULL) { |
| 128 | printk(BIOS_ERR, "Could not migrate CAR data!\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
Andrey Petrov | dd56de9 | 2016-02-25 17:22:17 -0800 | [diff] [blame] | 132 | memcpy(migrated_base, _car_relocatable_data_start, car_size); |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 133 | |
| 134 | /* Mark that the data has been moved. */ |
| 135 | car_migrated = ~0; |
Kyösti Mälkki | 91fac61 | 2014-12-31 20:55:19 +0200 | [diff] [blame] | 136 | } |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 137 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 138 | static void car_migrate_variables(int is_recovery) |
Kyösti Mälkki | 91fac61 | 2014-12-31 20:55:19 +0200 | [diff] [blame] | 139 | { |
Martin Roth | 9346d50 | 2015-07-15 18:32:43 -0600 | [diff] [blame] | 140 | if (!IS_ENABLED(PLATFORM_USES_FSP1_0)) |
Kyösti Mälkki | 87acccc | 2014-12-19 09:19:29 +0200 | [diff] [blame] | 141 | do_car_migrate_variables(); |
| 142 | } |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 143 | ROMSTAGE_CBMEM_INIT_HOOK(car_migrate_variables) |