blob: 3fc99db72682445aa08e0f9fd823aa1c1dacadc4 [file] [log] [blame]
Aaron Durbinbd74a4b2015-03-06 23:17:33 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 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 Durbinbd74a4b2015-03-06 23:17:33 -060014 */
15
16#include <arch/early_variables.h>
17#include <cbmem.h>
18#include <stage_cache.h>
19#include <string.h>
20
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060021/* Stage cache uses cbmem. */
Aaron Durbin54546c92015-08-05 00:52:13 -050022void stage_cache_add(int stage_id, const struct prog *stage)
Aaron Durbinbd74a4b2015-03-06 23:17:33 -060023{
24 struct stage_cache *meta;
25 void *c;
26
27 meta = cbmem_add(CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta));
28 if (meta == NULL)
29 return;
30 meta->load_addr = (uintptr_t)prog_start(stage);
31 meta->entry_addr = (uintptr_t)prog_entry(stage);
32
33 c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, prog_size(stage));
34 if (c == NULL)
35 return;
36
37 memcpy(c, prog_start(stage), prog_size(stage));
38}
39
40void stage_cache_load_stage(int stage_id, struct prog *stage)
41{
42 struct stage_cache *meta;
43 const struct cbmem_entry *e;
44 void *c;
45 size_t size;
46 void *load_addr;
47
48 prog_set_entry(stage, NULL, NULL);
49
50 meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id);
51 if (meta == NULL)
52 return;
53
54 e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id);
55
56 if (e == NULL)
57 return;
58
59 c = cbmem_entry_start(e);
60 size = cbmem_entry_size(e);
61 load_addr = (void *)(uintptr_t)meta->load_addr;
62
63 memcpy(load_addr, c, size);
64
65 prog_set_area(stage, load_addr, size);
66 prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, NULL);
67}