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. |
| 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 |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stddef.h> |
| 22 | #include <console/console.h> |
| 23 | #include <cbmem.h> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 24 | #include <arch/early_variables.h> |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 25 | |
| 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 | |
| 30 | extern char _car_data_start[]; |
| 31 | extern char _car_data_end[]; |
| 32 | |
| 33 | /* |
| 34 | * 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] | 35 | * 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] | 36 | * 1. cache-as-ram space is zero'd out once it is set up. |
| 37 | * 2. Either the cache-as-ram space is memory-backed after getting torn down |
| 38 | * or the space returns 0xff's for each byte read. |
| 39 | * Based on these 2 attributes there is the ability to tell when the |
| 40 | * cache-as-ram region has been migrated. |
| 41 | */ |
| 42 | static int car_migrated CAR_GLOBAL; |
| 43 | |
| 44 | |
| 45 | void *car_get_var_ptr(void *var) |
| 46 | { |
| 47 | char *migrated_base; |
| 48 | int offset; |
| 49 | void * _car_start = &_car_data_start; |
| 50 | void * _car_end = &_car_data_end; |
| 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 | |
| 64 | migrated_base = cbmem_find(CBMEM_ID_CAR_GLOBALS); |
| 65 | |
| 66 | if (migrated_base == NULL) { |
| 67 | printk(BIOS_ERR, "CAR: Could not find migration base!\n"); |
| 68 | return var; |
| 69 | } |
| 70 | |
| 71 | offset = (char *)var - (char *)_car_start; |
| 72 | |
| 73 | return &migrated_base[offset]; |
| 74 | } |
| 75 | |
Kyösti Mälkki | e455425 | 2014-12-31 18:34:59 +0200 | [diff] [blame] | 76 | /* |
| 77 | * Update a CAR_GLOBAL variable var, originally pointing to CAR region, |
| 78 | * with the address in migrated CAR region in DRAM. |
| 79 | */ |
| 80 | void *car_sync_var_ptr(void *var) |
| 81 | { |
| 82 | void ** mig_var = car_get_var_ptr(var); |
| 83 | void * _car_start = &_car_data_start; |
| 84 | void * _car_end = &_car_data_end; |
| 85 | |
| 86 | /* Not moved or migrated yet. */ |
| 87 | if (mig_var == var) |
| 88 | return mig_var; |
| 89 | |
| 90 | /* It's already pointing outside car.global_data. */ |
| 91 | if (*mig_var < _car_start || *mig_var > _car_end) |
| 92 | return mig_var; |
| 93 | |
| 94 | #if !IS_ENABLED(CONFIG_PLATFORM_USES_FSP) |
| 95 | /* Keep console buffer in CAR until cbmemc_reinit() moves it. */ |
| 96 | if (*mig_var == _car_end) |
| 97 | return mig_var; |
| 98 | #endif |
| 99 | |
| 100 | /* Move the pointer by the same amount the variable storing it was |
| 101 | * moved by. |
| 102 | */ |
| 103 | *mig_var += (char *)mig_var - (char *)var; |
| 104 | |
| 105 | return mig_var; |
| 106 | } |
| 107 | |
Kyösti Mälkki | 87acccc | 2014-12-19 09:19:29 +0200 | [diff] [blame] | 108 | static void do_car_migrate_variables(void) |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 109 | { |
| 110 | void *migrated_base; |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 111 | size_t car_data_size = &_car_data_end[0] - &_car_data_start[0]; |
| 112 | |
Aaron Durbin | 7f5897a | 2013-06-21 18:02:26 +0300 | [diff] [blame] | 113 | /* Check if already migrated. */ |
| 114 | if (car_migrated) |
| 115 | return; |
| 116 | |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 117 | migrated_base = cbmem_add(CBMEM_ID_CAR_GLOBALS, car_data_size); |
| 118 | |
| 119 | if (migrated_base == NULL) { |
| 120 | printk(BIOS_ERR, "Could not migrate CAR data!\n"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | memcpy(migrated_base, &_car_data_start[0], car_data_size); |
| 125 | |
| 126 | /* Mark that the data has been moved. */ |
| 127 | car_migrated = ~0; |
Kyösti Mälkki | 91fac61 | 2014-12-31 20:55:19 +0200 | [diff] [blame] | 128 | } |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 129 | |
Kyösti Mälkki | 91fac61 | 2014-12-31 20:55:19 +0200 | [diff] [blame] | 130 | static void do_car_migrate_hooks(void) |
| 131 | { |
| 132 | car_migration_func_t *migrate_func; |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 133 | /* Call all the migration functions. */ |
| 134 | migrate_func = &_car_migrate_start; |
Edward O'Callaghan | d777d86 | 2014-02-20 05:10:09 +1100 | [diff] [blame] | 135 | while (*migrate_func != NULL) { |
Aaron Durbin | 716738a | 2013-05-10 00:33:32 -0500 | [diff] [blame] | 136 | (*migrate_func)(); |
| 137 | migrate_func++; |
| 138 | } |
| 139 | } |
Kyösti Mälkki | 87acccc | 2014-12-19 09:19:29 +0200 | [diff] [blame] | 140 | |
| 141 | void car_migrate_variables(void) |
| 142 | { |
| 143 | if (!IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE)) |
| 144 | do_car_migrate_variables(); |
Kyösti Mälkki | 91fac61 | 2014-12-31 20:55:19 +0200 | [diff] [blame] | 145 | |
| 146 | if (!IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE)) |
| 147 | do_car_migrate_hooks(); |
Kyösti Mälkki | 87acccc | 2014-12-19 09:19:29 +0200 | [diff] [blame] | 148 | } |