blob: cca9afd69434e95c06f7350815862ef1163145d2 [file] [log] [blame]
Aaron Durbin716738a2013-05-10 00:33:32 -05001/*
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 Reinauerfd4f4132013-06-19 12:25:44 -070024#include <arch/early_variables.h>
Aaron Durbin716738a2013-05-10 00:33:32 -050025
26typedef void (* const car_migration_func_t)(void);
27
28extern car_migration_func_t _car_migrate_start;
Aaron Durbin716738a2013-05-10 00:33:32 -050029
30extern char _car_data_start[];
31extern char _car_data_end[];
32
33/*
34 * The car_migrated global variable determines if the cache-as-ram space has
Martin Roth4c3ab732013-07-08 16:23:54 -060035 * been migrated to real RAM. It does this by assuming the following things:
Aaron Durbin716738a2013-05-10 00:33:32 -050036 * 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 */
42static int car_migrated CAR_GLOBAL;
43
44
45void *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älkkie4554252014-12-31 18:34:59 +020076/*
77 * Update a CAR_GLOBAL variable var, originally pointing to CAR region,
78 * with the address in migrated CAR region in DRAM.
79 */
80void *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älkki87acccc2014-12-19 09:19:29 +0200108static void do_car_migrate_variables(void)
Aaron Durbin716738a2013-05-10 00:33:32 -0500109{
110 void *migrated_base;
Aaron Durbin716738a2013-05-10 00:33:32 -0500111 size_t car_data_size = &_car_data_end[0] - &_car_data_start[0];
112
Aaron Durbin7f5897a2013-06-21 18:02:26 +0300113 /* Check if already migrated. */
114 if (car_migrated)
115 return;
116
Aaron Durbin716738a2013-05-10 00:33:32 -0500117 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älkki91fac612014-12-31 20:55:19 +0200128}
Aaron Durbin716738a2013-05-10 00:33:32 -0500129
Kyösti Mälkki91fac612014-12-31 20:55:19 +0200130static void do_car_migrate_hooks(void)
131{
132 car_migration_func_t *migrate_func;
Aaron Durbin716738a2013-05-10 00:33:32 -0500133 /* Call all the migration functions. */
134 migrate_func = &_car_migrate_start;
Edward O'Callaghand777d862014-02-20 05:10:09 +1100135 while (*migrate_func != NULL) {
Aaron Durbin716738a2013-05-10 00:33:32 -0500136 (*migrate_func)();
137 migrate_func++;
138 }
139}
Kyösti Mälkki87acccc2014-12-19 09:19:29 +0200140
141void car_migrate_variables(void)
142{
143 if (!IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE))
144 do_car_migrate_variables();
Kyösti Mälkki91fac612014-12-31 20:55:19 +0200145
146 if (!IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE))
147 do_car_migrate_hooks();
Kyösti Mälkki87acccc2014-12-19 09:19:29 +0200148}