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> |
| 20 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 21 | /* Stage cache uses cbmem. */ |
Aaron Durbin | 54546c9 | 2015-08-05 00:52:13 -0500 | [diff] [blame] | 22 | void stage_cache_add(int stage_id, const struct prog *stage) |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 23 | { |
| 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); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame^] | 32 | meta->arg = (uintptr_t)prog_entry_arg(stage); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 33 | |
| 34 | c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, prog_size(stage)); |
| 35 | if (c == NULL) |
| 36 | return; |
| 37 | |
| 38 | memcpy(c, prog_start(stage), prog_size(stage)); |
| 39 | } |
| 40 | |
| 41 | void stage_cache_load_stage(int stage_id, struct prog *stage) |
| 42 | { |
| 43 | struct stage_cache *meta; |
| 44 | const struct cbmem_entry *e; |
| 45 | void *c; |
| 46 | size_t size; |
| 47 | void *load_addr; |
| 48 | |
| 49 | prog_set_entry(stage, NULL, NULL); |
| 50 | |
| 51 | meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id); |
| 52 | if (meta == NULL) |
| 53 | return; |
| 54 | |
| 55 | e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id); |
| 56 | |
| 57 | if (e == NULL) |
| 58 | return; |
| 59 | |
| 60 | c = cbmem_entry_start(e); |
| 61 | size = cbmem_entry_size(e); |
| 62 | load_addr = (void *)(uintptr_t)meta->load_addr; |
| 63 | |
| 64 | memcpy(load_addr, c, size); |
| 65 | |
| 66 | prog_set_area(stage, load_addr, size); |
Kyösti Mälkki | d87e4b3 | 2017-09-05 22:43:05 +0300 | [diff] [blame^] | 67 | prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, |
| 68 | (void *)(uintptr_t)meta->arg); |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 69 | } |