Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 1 | /* |
| 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 Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <arch/early_variables.h> |
| 17 | #include <cbmem.h> |
| 18 | #include <stage_cache.h> |
| 19 | #include <string.h> |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 20 | #include <console/console.h> |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 21 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 22 | /* Stage cache uses cbmem. */ |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 23 | void stage_cache_add(int stage_id, const struct prog *stage) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 24 | { |
| 25 | struct stage_cache *meta; |
| 26 | void *c; |
| 27 | |
| 28 | meta = cbmem_add(CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta)); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 29 | if (meta == NULL) { |
| 30 | printk(BIOS_ERR, "Error: Can't add %x metadata to cbmem\n", |
| 31 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 32 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 33 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 34 | meta->load_addr = (uintptr_t)prog_start(stage); |
| 35 | meta->entry_addr = (uintptr_t)prog_entry(stage); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 36 | meta->arg = (uintptr_t)prog_entry_arg(stage); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 37 | |
| 38 | c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, prog_size(stage)); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 39 | if (c == NULL) { |
| 40 | printk(BIOS_ERR, "Error: Can't add stage_cache %x to cbmem\n", |
| 41 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 42 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 43 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 44 | |
| 45 | memcpy(c, prog_start(stage), prog_size(stage)); |
| 46 | } |
| 47 | |
Marshall Dawson | 8d6e0e0 | 2018-01-30 15:33:23 -0700 | [diff] [blame^] | 48 | void stage_cache_add_raw(int stage_id, const void *base, const size_t size) |
| 49 | { |
| 50 | void *c; |
| 51 | |
| 52 | c = cbmem_add(CBMEM_ID_STAGEx_RAW + stage_id, size); |
| 53 | if (c == NULL) { |
| 54 | printk(BIOS_DEBUG, "Error: Can't add %x raw data to cbmem\n", |
| 55 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | memcpy(c, base, size); |
| 60 | } |
| 61 | |
| 62 | void stage_cache_get_raw(int stage_id, void **base, size_t *size) |
| 63 | { |
| 64 | const struct cbmem_entry *e; |
| 65 | |
| 66 | e = cbmem_entry_find(CBMEM_ID_STAGEx_RAW + stage_id); |
| 67 | if (e == NULL) { |
| 68 | printk(BIOS_ERR, "Error: Can't find raw %x data in cbmem\n", |
| 69 | CBMEM_ID_STAGEx_RAW + stage_id); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | *base = cbmem_entry_start(e); |
| 74 | *size = cbmem_entry_size(e); |
| 75 | } |
| 76 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 77 | void stage_cache_load_stage(int stage_id, struct prog *stage) |
| 78 | { |
| 79 | struct stage_cache *meta; |
| 80 | const struct cbmem_entry *e; |
| 81 | void *c; |
| 82 | size_t size; |
| 83 | void *load_addr; |
| 84 | |
| 85 | prog_set_entry(stage, NULL, NULL); |
| 86 | |
| 87 | meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id); |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 88 | if (meta == NULL) { |
| 89 | printk(BIOS_ERR, "Error: Can't find %x metadata in cbmem\n", |
| 90 | CBMEM_ID_STAGEx_META + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 91 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 92 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 93 | |
| 94 | e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id); |
| 95 | |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 96 | if (e == NULL) { |
| 97 | printk(BIOS_ERR, "Error: Can't find stage_cache %x in cbmem\n", |
| 98 | CBMEM_ID_STAGEx_CACHE + stage_id); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 99 | return; |
Marshall Dawson | 0876caa | 2018-01-30 15:54:52 -0700 | [diff] [blame] | 100 | } |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 101 | |
| 102 | c = cbmem_entry_start(e); |
| 103 | size = cbmem_entry_size(e); |
| 104 | load_addr = (void *)(uintptr_t)meta->load_addr; |
| 105 | |
| 106 | memcpy(load_addr, c, size); |
| 107 | |
| 108 | prog_set_area(stage, load_addr, size); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame] | 109 | prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, |
| 110 | (void *)(uintptr_t)meta->arg); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 111 | } |