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 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 19 | #include <commonlib/region.h> |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
| 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 | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 29 | enum prog_type { |
| 30 | PROG_UNKNOWN, |
| 31 | PROG_VERSTAGE, |
| 32 | PROG_ROMSTAGE, |
| 33 | PROG_RAMSTAGE, |
| 34 | PROG_REFCODE, |
| 35 | PROG_PAYLOAD, |
| 36 | PROG_BL31, |
| 37 | PROG_BL32, |
| 38 | }; |
| 39 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 40 | /* 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] | 41 | * set on the last segment loaded. */ |
| 42 | void arch_segment_loaded(uintptr_t start, size_t size, int flags); |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 43 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 44 | /* Representation of a program. */ |
| 45 | struct prog { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 46 | /* The region_device is the source of program content to load. After |
| 47 | * loading program it represents the memory region of the stages and |
| 48 | * payload. For architectures that use a bounce buffer |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 49 | * then it would represent the bounce buffer. */ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 50 | enum prog_type type; |
| 51 | const char *name; |
| 52 | struct region_device rdev; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 53 | /* Entry to program with optional argument. It's up to the architecture |
| 54 | * to decide if argument is passed. */ |
| 55 | void (*entry)(void *); |
| 56 | void *arg; |
| 57 | }; |
| 58 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 59 | #define PROG_INIT(type_, name_) \ |
| 60 | { \ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 61 | .type = (type_), \ |
| 62 | .name = (name_), \ |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline const char *prog_name(const struct prog *prog) |
| 66 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 67 | return prog->name; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 70 | static inline enum prog_type prog_type(const struct prog *prog) |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 71 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 72 | return prog->type; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline struct region_device *prog_rdev(struct prog *prog) |
| 76 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 77 | return &prog->rdev; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 80 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 81 | static inline size_t prog_size(const struct prog *prog) |
| 82 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 83 | return region_device_sz(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 86 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 87 | static inline void *prog_start(const struct prog *prog) |
| 88 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 89 | return rdev_mmap_full(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline void *prog_entry(const struct prog *prog) |
| 93 | { |
| 94 | return prog->entry; |
| 95 | } |
| 96 | |
| 97 | static inline void *prog_entry_arg(const struct prog *prog) |
| 98 | { |
| 99 | return prog->arg; |
| 100 | } |
| 101 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 102 | /* region_device representing the 32-bit flat address space. */ |
| 103 | extern const struct mem_region_device addrspace_32bit; |
| 104 | |
| 105 | static inline void prog_memory_init(struct prog *prog, uintptr_t ptr, |
| 106 | size_t size) |
| 107 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame^] | 108 | rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 111 | static inline void prog_set_area(struct prog *prog, void *start, size_t size) |
| 112 | { |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 113 | prog_memory_init(prog, (uintptr_t)start, size); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static inline void prog_set_entry(struct prog *prog, void *e, void *arg) |
| 117 | { |
| 118 | prog->entry = e; |
| 119 | prog->arg = arg; |
| 120 | } |
| 121 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 122 | /* 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] | 123 | int prog_locate(struct prog *prog); |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 124 | |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 125 | /* Run the program described by prog. */ |
| 126 | void prog_run(struct prog *prog); |
| 127 | /* Per architecture implementation running a program. */ |
| 128 | void arch_prog_run(struct prog *prog); |
| 129 | /* Platform (SoC/chipset) specific overrides for running a program. This is |
| 130 | * called prior to calling the arch_prog_run. Thus, if there is anything |
| 131 | * special that needs to be done by the platform similar to the architecture |
| 132 | * code it needs to that as well. */ |
| 133 | void platform_prog_run(struct prog *prog); |
| 134 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 135 | struct prog_loader_ops { |
| 136 | const char *name; |
Aaron Durbin | 5e8286b | 2015-04-28 15:59:12 -0500 | [diff] [blame] | 137 | /* Determine if the loader is the active one. If so returns 1 else 0 |
| 138 | * or < 0 on error. */ |
| 139 | int (*is_loader_active)(struct prog *prog); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 140 | /* Returns < 0 on error or 0 on success. This function locates |
| 141 | * the rdev representing the file data associated with the passed in |
| 142 | * prog. */ |
| 143 | int (*locate)(struct prog *prog); |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 144 | }; |
| 145 | |
Aaron Durbin | d1b0e87 | 2015-03-17 13:17:06 -0500 | [diff] [blame] | 146 | /************************ |
| 147 | * ROMSTAGE LOADING * |
| 148 | ************************/ |
| 149 | |
| 150 | /* Run romstage from bootblock. */ |
| 151 | void run_romstage(void); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 152 | |
| 153 | /************************ |
| 154 | * RAMSTAGE LOADING * |
| 155 | ************************/ |
| 156 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 157 | /* Run ramstage from romstage. */ |
| 158 | void run_ramstage(void); |
| 159 | |
Aaron Durbin | bd74a4b | 2015-03-06 23:17:33 -0600 | [diff] [blame] | 160 | /* Called when the stage cache couldn't load ramstage on resume. */ |
| 161 | void ramstage_cache_invalid(void); |
Aaron Durbin | fcfdff8 | 2015-03-20 10:58:41 -0500 | [diff] [blame] | 162 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 163 | /*********************** |
| 164 | * PAYLOAD LOADING * |
| 165 | ***********************/ |
| 166 | |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 167 | /* Load payload into memory in preparation to run. */ |
| 168 | void payload_load(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 169 | |
| 170 | /* Run the loaded payload. */ |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 171 | void payload_run(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 172 | |
Aaron Durbin | c34713d | 2014-02-25 20:36:56 -0600 | [diff] [blame] | 173 | /* Mirror the payload to be loaded. */ |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 174 | void mirror_payload(struct prog *payload); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 175 | |
| 176 | /* Defined in src/lib/selfboot.c */ |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 177 | void *selfload(struct prog *payload); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 178 | |
| 179 | #endif /* PROGRAM_LOADING_H */ |