blob: 72e8cf6445f1bb383eb92aa4dbf35548777c8e0d [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin04654a22015-03-17 11:43:44 -05002#ifndef PROGRAM_LOADING_H
3#define PROGRAM_LOADING_H
Aaron Durbinbdf913a2014-02-24 14:56:34 -06004
Ting Shen05532262019-01-28 17:22:22 +08005#include <bootmem.h>
Aaron Durbin7e7a4df2015-12-08 14:34:35 -06006#include <commonlib/region.h>
Aaron Durbinbdf913a2014-02-24 14:56:34 -06007#include <stdint.h>
8#include <stddef.h>
9
Aaron Durbin6e76fff2015-03-20 09:42:05 -050010enum {
11 /* Last segment of program. Can be used to take different actions for
12 * cache maintenance of a program load. */
13 SEG_FINAL = 1 << 0,
14};
Ionela Voinescu00903e52015-01-09 13:14:20 +000015
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060016enum prog_type {
Julius Werner55b30812018-05-21 18:30:17 +000017 PROG_UNKNOWN,
Julius Werner99f46832018-05-16 14:14:04 -070018 PROG_BOOTBLOCK,
Julius Werner55b30812018-05-21 18:30:17 +000019 PROG_VERSTAGE,
20 PROG_ROMSTAGE,
21 PROG_RAMSTAGE,
22 PROG_REFCODE,
23 PROG_PAYLOAD,
24 PROG_BL31,
25 PROG_BL32,
Philipp Deppenwiese01797b12018-11-08 10:39:39 +010026 PROG_POSTCAR,
Patrick Rudolph4c3da702019-07-07 13:10:56 +020027 PROG_OPENSBI,
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060028};
29
Aaron Durbin096f4572016-03-31 13:49:00 -050030/*
31 * prog_segment_loaded() is called for each segment of a program loaded. The
32 * SEG_FINAL flag will be set on the last segment loaded. The following two
33 * functions, platform_segment_loaded() and arch_segment_loaded(), are called
34 * in that order within prog_segment_loaded(). In short, rely on
35 * prog_segment_loaded() to perform the proper dispatch sequence.
36 */
37void prog_segment_loaded(uintptr_t start, size_t size, int flags);
38void platform_segment_loaded(uintptr_t start, size_t size, int flags);
Aaron Durbin6e76fff2015-03-20 09:42:05 -050039void arch_segment_loaded(uintptr_t start, size_t size, int flags);
Ionela Voinescu00903e52015-01-09 13:14:20 +000040
Aaron Durbin3948e532015-03-20 13:00:20 -050041/* Representation of a program. */
42struct prog {
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060043 /* The region_device is the source of program content to load. After
44 * loading program it represents the memory region of the stages and
45 * payload. For architectures that use a bounce buffer
Aaron Durbinac12c66c2015-05-20 12:08:55 -050046 * then it would represent the bounce buffer. */
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060047 enum prog_type type;
Patrick Rudolph71327fb2018-05-03 10:35:26 +020048 uint32_t cbfs_type;
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060049 const char *name;
50 struct region_device rdev;
Aaron Durbin3948e532015-03-20 13:00:20 -050051 /* Entry to program with optional argument. It's up to the architecture
52 * to decide if argument is passed. */
53 void (*entry)(void *);
54 void *arg;
55};
56
Aaron Durbinac12c66c2015-05-20 12:08:55 -050057#define PROG_INIT(type_, name_) \
58 { \
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060059 .type = (type_), \
60 .name = (name_), \
Aaron Durbinac12c66c2015-05-20 12:08:55 -050061 }
62
63static inline const char *prog_name(const struct prog *prog)
64{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060065 return prog->name;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050066}
67
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060068static inline enum prog_type prog_type(const struct prog *prog)
Aaron Durbinac12c66c2015-05-20 12:08:55 -050069{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060070 return prog->type;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050071}
72
Patrick Rudolph71327fb2018-05-03 10:35:26 +020073static inline uint32_t prog_cbfs_type(const struct prog *prog)
74{
75 return prog->cbfs_type;
76}
77
Aaron Durbinac12c66c2015-05-20 12:08:55 -050078static inline struct region_device *prog_rdev(struct prog *prog)
79{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060080 return &prog->rdev;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050081}
82
Aaron Durbin6a452ef2015-05-19 16:25:20 -050083/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050084static inline size_t prog_size(const struct prog *prog)
85{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060086 return region_device_sz(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050087}
88
Aaron Durbin6a452ef2015-05-19 16:25:20 -050089/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050090static inline void *prog_start(const struct prog *prog)
91{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060092 return rdev_mmap_full(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050093}
94
95static inline void *prog_entry(const struct prog *prog)
96{
97 return prog->entry;
98}
99
100static inline void *prog_entry_arg(const struct prog *prog)
101{
102 return prog->arg;
103}
104
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500105/* region_device representing the 32-bit flat address space. */
106extern const struct mem_region_device addrspace_32bit;
107
108static inline void prog_memory_init(struct prog *prog, uintptr_t ptr,
109 size_t size)
110{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600111 rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size);
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500112}
113
Aaron Durbin3948e532015-03-20 13:00:20 -0500114static inline void prog_set_area(struct prog *prog, void *start, size_t size)
115{
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500116 prog_memory_init(prog, (uintptr_t)start, size);
Aaron Durbin3948e532015-03-20 13:00:20 -0500117}
118
119static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
120{
121 prog->entry = e;
122 prog->arg = arg;
123}
124
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200125static inline void prog_set_arg(struct prog *prog, void *arg)
126{
127 prog->arg = arg;
128}
129
Aaron Durbin899d13d2015-05-15 23:39:23 -0500130/* Locate the identified program to run. Return 0 on success. < 0 on error. */
Aaron Durbin6d720f32015-12-08 17:00:23 -0600131int prog_locate(struct prog *prog);
Frans Hendriksfc580342019-06-14 14:36:37 +0200132/* The prog_locate_hook() is called prior to CBFS traversal. The hook can be
133 * used to implement policy that allows or prohibits further progress through
134 * prog_locate(). The type and name field within struct prog are the only valid
135 * fields. A 0 return value allows further progress while a non-zero return
136 * value prohibits further progress */
137int prog_locate_hook(struct prog *prog);
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500138
Aaron Durbinb3847e62015-03-20 15:55:08 -0500139/* Run the program described by prog. */
140void prog_run(struct prog *prog);
141/* Per architecture implementation running a program. */
142void arch_prog_run(struct prog *prog);
143/* Platform (SoC/chipset) specific overrides for running a program. This is
144 * called prior to calling the arch_prog_run. Thus, if there is anything
145 * special that needs to be done by the platform similar to the architecture
146 * code it needs to that as well. */
147void platform_prog_run(struct prog *prog);
148
Aaron Durbince9efe02015-03-20 16:37:12 -0500149struct prog_loader_ops {
150 const char *name;
Aaron Durbin5e8286b2015-04-28 15:59:12 -0500151 /* Determine if the loader is the active one. If so returns 1 else 0
152 * or < 0 on error. */
153 int (*is_loader_active)(struct prog *prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500154 /* Returns < 0 on error or 0 on success. This function locates
155 * the rdev representing the file data associated with the passed in
156 * prog. */
157 int (*locate)(struct prog *prog);
Aaron Durbince9efe02015-03-20 16:37:12 -0500158};
159
Aaron Durbind1b0e872015-03-17 13:17:06 -0500160/************************
161 * ROMSTAGE LOADING *
162 ************************/
163
164/* Run romstage from bootblock. */
165void run_romstage(void);
Aaron Durbin04654a22015-03-17 11:43:44 -0500166
Kyösti Mälkkib8d575c2019-12-16 16:00:49 +0200167/* Runtime selector for CBFS_PREFIX of romstage. */
168int legacy_romstage_selector(struct prog *romstage);
169
Aaron Durbin04654a22015-03-17 11:43:44 -0500170/************************
171 * RAMSTAGE LOADING *
172 ************************/
173
Aaron Durbince9efe02015-03-20 16:37:12 -0500174/* Run ramstage from romstage. */
175void run_ramstage(void);
176
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300177/* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path,
178 * if ramstage overwrites low memory. */
179void backup_ramstage_section(uintptr_t base, size_t size);
180
Aaron Durbin04654a22015-03-17 11:43:44 -0500181/***********************
182 * PAYLOAD LOADING *
183 ***********************/
184
Kyösti Mälkkia48433d2018-06-07 06:31:43 +0300185int payload_arch_usable_ram_quirk(uint64_t start, uint64_t size);
186
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500187/* Load payload into memory in preparation to run. */
188void payload_load(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600189
190/* Run the loaded payload. */
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500191void payload_run(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600192
Simon Glass7ae73fc2016-08-27 12:18:38 -0600193/*
Ronald G. Minnichc3085542018-10-24 15:46:51 -0700194 * selfload() and selfload_check() load payloads into memory.
195 * selfload() does not check the payload to see if it targets memory.
196 * Call selfload_check() to check that the payload targets usable memory.
197 * If it does not, the load will fail and this function
198 * will return false. On successful payload loading these functions return true.
Simon Glass7ae73fc2016-08-27 12:18:38 -0600199 *
200 * Defined in src/lib/selfboot.c
201 */
Ting Shen05532262019-01-28 17:22:22 +0800202bool selfload_check(struct prog *payload, enum bootmem_type dest_type);
Ronald G. Minnichc3085542018-10-24 15:46:51 -0700203bool selfload(struct prog *payload);
Aaron Durbin04654a22015-03-17 11:43:44 -0500204
205#endif /* PROGRAM_LOADING_H */