Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 4 | * Copyright 2015 Google Inc. |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 5 | * Copyright (C) 2014 Imagination Technologies |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 15 | */ |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 16 | #ifndef PROGRAM_LOADING_H |
| 17 | #define PROGRAM_LOADING_H |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <stddef.h> |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 21 | #include <assets.h> |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 22 | |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 23 | enum { |
| 24 | /* Last segment of program. Can be used to take different actions for |
| 25 | * cache maintenance of a program load. */ |
| 26 | SEG_FINAL = 1 << 0, |
| 27 | }; |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 28 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 29 | /* Called for each segment of a program loaded. The SEG_FINAL flag will be |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 30 | * set on the last segment loaded. */ |
| 31 | void arch_segment_loaded(uintptr_t start, size_t size, int flags); |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 32 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 33 | /* Representation of a program. */ |
| 34 | struct prog { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 35 | /* The region_device within the asset is the source of program content |
| 36 | * to load. After loading program it represents the memory region of |
| 37 | * the stages and payload. For architectures that use a bounce buffer |
| 38 | * then it would represent the bounce buffer. */ |
| 39 | struct asset asset; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 40 | /* Entry to program with optional argument. It's up to the architecture |
| 41 | * to decide if argument is passed. */ |
| 42 | void (*entry)(void *); |
| 43 | void *arg; |
| 44 | }; |
| 45 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 46 | #define PROG_INIT(type_, name_) \ |
| 47 | { \ |
| 48 | .asset = ASSET_INIT(type_, name_), \ |
| 49 | } |
| 50 | |
| 51 | static inline const char *prog_name(const struct prog *prog) |
| 52 | { |
| 53 | return asset_name(&prog->asset); |
| 54 | } |
| 55 | |
| 56 | static inline enum asset_type prog_type(const struct prog *prog) |
| 57 | { |
| 58 | return asset_type(&prog->asset); |
| 59 | } |
| 60 | |
| 61 | static inline struct region_device *prog_rdev(struct prog *prog) |
| 62 | { |
| 63 | return asset_rdev(&prog->asset); |
| 64 | } |
| 65 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 66 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 67 | static inline size_t prog_size(const struct prog *prog) |
| 68 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 69 | return asset_size(&prog->asset); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 72 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 73 | static inline void *prog_start(const struct prog *prog) |
| 74 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 75 | return asset_mmap(&prog->asset); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static inline void *prog_entry(const struct prog *prog) |
| 79 | { |
| 80 | return prog->entry; |
| 81 | } |
| 82 | |
| 83 | static inline void *prog_entry_arg(const struct prog *prog) |
| 84 | { |
| 85 | return prog->arg; |
| 86 | } |
| 87 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 88 | /* region_device representing the 32-bit flat address space. */ |
| 89 | extern const struct mem_region_device addrspace_32bit; |
| 90 | |
| 91 | static inline void prog_memory_init(struct prog *prog, uintptr_t ptr, |
| 92 | size_t size) |
| 93 | { |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 94 | rdev_chain(&prog->asset.rdev, &addrspace_32bit.rdev, ptr, size); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 97 | static inline void prog_set_area(struct prog *prog, void *start, size_t size) |
| 98 | { |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 99 | prog_memory_init(prog, (uintptr_t)start, size); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static inline void prog_set_entry(struct prog *prog, void *e, void *arg) |
| 103 | { |
| 104 | prog->entry = e; |
| 105 | prog->arg = arg; |
| 106 | } |
| 107 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 108 | /* Locate the identified program to run. Return 0 on success. < 0 on error. */ |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame^] | 109 | int prog_locate(struct prog *prog); |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 110 | |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 111 | /* Run the program described by prog. */ |
| 112 | void prog_run(struct prog *prog); |
| 113 | /* Per architecture implementation running a program. */ |
| 114 | void arch_prog_run(struct prog *prog); |
| 115 | /* Platform (SoC/chipset) specific overrides for running a program. This is |
| 116 | * called prior to calling the arch_prog_run. Thus, if there is anything |
| 117 | * special that needs to be done by the platform similar to the architecture |
| 118 | * code it needs to that as well. */ |
| 119 | void platform_prog_run(struct prog *prog); |
| 120 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 121 | struct prog_loader_ops { |
| 122 | const char *name; |
Aaron Durbin | 5e8286b | 2015-04-28 15:59:12 -0500 | [diff] [blame] | 123 | /* Determine if the loader is the active one. If so returns 1 else 0 |
| 124 | * or < 0 on error. */ |
| 125 | int (*is_loader_active)(struct prog *prog); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 126 | /* Returns < 0 on error or 0 on success. This function locates |
| 127 | * the rdev representing the file data associated with the passed in |
| 128 | * prog. */ |
| 129 | int (*locate)(struct prog *prog); |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
Aaron Durbin | d1b0e87 | 2015-03-17 13:17:06 -0500 | [diff] [blame] | 132 | /************************ |
| 133 | * ROMSTAGE LOADING * |
| 134 | ************************/ |
| 135 | |
| 136 | /* Run romstage from bootblock. */ |
| 137 | void run_romstage(void); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 138 | |
| 139 | /************************ |
| 140 | * RAMSTAGE LOADING * |
| 141 | ************************/ |
| 142 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 143 | /* Run ramstage from romstage. */ |
| 144 | void run_ramstage(void); |
| 145 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 146 | /* Called when the stage cache couldn't load ramstage on resume. */ |
| 147 | void ramstage_cache_invalid(void); |
Aaron Durbin | fcfdff8 | 2015-03-20 10:58:41 -0500 | [diff] [blame] | 148 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 149 | /*********************** |
| 150 | * PAYLOAD LOADING * |
| 151 | ***********************/ |
| 152 | |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 153 | /* Load payload into memory in preparation to run. */ |
| 154 | void payload_load(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 155 | |
| 156 | /* Run the loaded payload. */ |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 157 | void payload_run(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 158 | |
Aaron Durbin | c34713d | 2014-02-25 20:36:56 -0600 | [diff] [blame] | 159 | /* Mirror the payload to be loaded. */ |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 160 | void mirror_payload(struct prog *payload); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 161 | |
| 162 | /* Defined in src/lib/selfboot.c */ |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 163 | void *selfload(struct prog *payload); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 164 | |
| 165 | #endif /* PROGRAM_LOADING_H */ |