blob: 416e2e941824d36a00a0146445515900f4be8d24 [file] [log] [blame]
Aaron Durbinbdf913a2014-02-24 14:56:34 -06001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin04654a22015-03-17 11:43:44 -05004 * Copyright 2015 Google Inc.
Ionela Voinescu00903e52015-01-09 13:14:20 +00005 * Copyright (C) 2014 Imagination Technologies
Aaron Durbinbdf913a2014-02-24 14:56:34 -06006 *
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 Durbinbdf913a2014-02-24 14:56:34 -060015 */
Aaron Durbin04654a22015-03-17 11:43:44 -050016#ifndef PROGRAM_LOADING_H
17#define PROGRAM_LOADING_H
Aaron Durbinbdf913a2014-02-24 14:56:34 -060018
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060019#include <commonlib/region.h>
Aaron Durbinbdf913a2014-02-24 14:56:34 -060020#include <stdint.h>
21#include <stddef.h>
22
Aaron Durbin6e76fff2015-03-20 09:42:05 -050023enum {
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 Voinescu00903e52015-01-09 13:14:20 +000028
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060029enum 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 Durbin096f4572016-03-31 13:49:00 -050040/*
41 * prog_segment_loaded() is called for each segment of a program loaded. The
42 * SEG_FINAL flag will be set on the last segment loaded. The following two
43 * functions, platform_segment_loaded() and arch_segment_loaded(), are called
44 * in that order within prog_segment_loaded(). In short, rely on
45 * prog_segment_loaded() to perform the proper dispatch sequence.
46 */
47void prog_segment_loaded(uintptr_t start, size_t size, int flags);
48void platform_segment_loaded(uintptr_t start, size_t size, int flags);
Aaron Durbin6e76fff2015-03-20 09:42:05 -050049void arch_segment_loaded(uintptr_t start, size_t size, int flags);
Ionela Voinescu00903e52015-01-09 13:14:20 +000050
Vladimir Serbinenkof0d39c42016-02-19 16:44:22 +010051/* Return true if arch supports bounce buffer. */
52int arch_supports_bounce_buffer(void);
53
Aaron Durbin3948e532015-03-20 13:00:20 -050054/* Representation of a program. */
55struct prog {
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060056 /* The region_device is the source of program content to load. After
57 * loading program it represents the memory region of the stages and
58 * payload. For architectures that use a bounce buffer
Aaron Durbinac12c66c2015-05-20 12:08:55 -050059 * then it would represent the bounce buffer. */
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060060 enum prog_type type;
61 const char *name;
62 struct region_device rdev;
Aaron Durbin3948e532015-03-20 13:00:20 -050063 /* Entry to program with optional argument. It's up to the architecture
64 * to decide if argument is passed. */
65 void (*entry)(void *);
66 void *arg;
67};
68
Aaron Durbinac12c66c2015-05-20 12:08:55 -050069#define PROG_INIT(type_, name_) \
70 { \
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060071 .type = (type_), \
72 .name = (name_), \
Aaron Durbinac12c66c2015-05-20 12:08:55 -050073 }
74
75static inline const char *prog_name(const struct prog *prog)
76{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060077 return prog->name;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050078}
79
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060080static inline enum prog_type prog_type(const struct prog *prog)
Aaron Durbinac12c66c2015-05-20 12:08:55 -050081{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060082 return prog->type;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050083}
84
85static inline struct region_device *prog_rdev(struct prog *prog)
86{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060087 return &prog->rdev;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050088}
89
Aaron Durbin6a452ef2015-05-19 16:25:20 -050090/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050091static inline size_t prog_size(const struct prog *prog)
92{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060093 return region_device_sz(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050094}
95
Aaron Durbin6a452ef2015-05-19 16:25:20 -050096/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050097static inline void *prog_start(const struct prog *prog)
98{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060099 return rdev_mmap_full(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -0500100}
101
102static inline void *prog_entry(const struct prog *prog)
103{
104 return prog->entry;
105}
106
107static inline void *prog_entry_arg(const struct prog *prog)
108{
109 return prog->arg;
110}
111
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500112/* region_device representing the 32-bit flat address space. */
113extern const struct mem_region_device addrspace_32bit;
114
115static inline void prog_memory_init(struct prog *prog, uintptr_t ptr,
116 size_t size)
117{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600118 rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size);
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500119}
120
Aaron Durbin3948e532015-03-20 13:00:20 -0500121static inline void prog_set_area(struct prog *prog, void *start, size_t size)
122{
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500123 prog_memory_init(prog, (uintptr_t)start, size);
Aaron Durbin3948e532015-03-20 13:00:20 -0500124}
125
126static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
127{
128 prog->entry = e;
129 prog->arg = arg;
130}
131
Aaron Durbin899d13d2015-05-15 23:39:23 -0500132/* Locate the identified program to run. Return 0 on success. < 0 on error. */
Aaron Durbin6d720f32015-12-08 17:00:23 -0600133int prog_locate(struct prog *prog);
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500134
Aaron Durbinb3847e62015-03-20 15:55:08 -0500135/* Run the program described by prog. */
136void prog_run(struct prog *prog);
137/* Per architecture implementation running a program. */
138void arch_prog_run(struct prog *prog);
139/* Platform (SoC/chipset) specific overrides for running a program. This is
140 * called prior to calling the arch_prog_run. Thus, if there is anything
141 * special that needs to be done by the platform similar to the architecture
142 * code it needs to that as well. */
143void platform_prog_run(struct prog *prog);
144
Aaron Durbince9efe02015-03-20 16:37:12 -0500145struct prog_loader_ops {
146 const char *name;
Aaron Durbin5e8286b2015-04-28 15:59:12 -0500147 /* Determine if the loader is the active one. If so returns 1 else 0
148 * or < 0 on error. */
149 int (*is_loader_active)(struct prog *prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500150 /* Returns < 0 on error or 0 on success. This function locates
151 * the rdev representing the file data associated with the passed in
152 * prog. */
153 int (*locate)(struct prog *prog);
Aaron Durbince9efe02015-03-20 16:37:12 -0500154};
155
Aaron Durbind1b0e872015-03-17 13:17:06 -0500156/************************
157 * ROMSTAGE LOADING *
158 ************************/
159
160/* Run romstage from bootblock. */
161void run_romstage(void);
Aaron Durbin04654a22015-03-17 11:43:44 -0500162
163/************************
164 * RAMSTAGE LOADING *
165 ************************/
166
Aaron Durbince9efe02015-03-20 16:37:12 -0500167/* Run ramstage from romstage. */
168void run_ramstage(void);
169
Kyösti Mälkkie5c00a52016-06-27 14:50:27 +0300170/* Determine where stack for ramstage loader is located. */
171enum { ROMSTAGE_STACK_CBMEM, ROMSTAGE_STACK_LOW_MEM };
172uintptr_t romstage_ram_stack_base(size_t size, int src);
173uintptr_t romstage_ram_stack_top(void);
174uintptr_t romstage_ram_stack_bottom(void);
175
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300176/* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path,
177 * if ramstage overwrites low memory. */
178void backup_ramstage_section(uintptr_t base, size_t size);
179
Aaron Durbin04654a22015-03-17 11:43:44 -0500180/***********************
181 * PAYLOAD LOADING *
182 ***********************/
183
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500184/* Load payload into memory in preparation to run. */
185void payload_load(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600186
187/* Run the loaded payload. */
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500188void payload_run(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600189
Aaron Durbinc34713d2014-02-25 20:36:56 -0600190/* Mirror the payload to be loaded. */
Aaron Durbince9efe02015-03-20 16:37:12 -0500191void mirror_payload(struct prog *payload);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600192
Simon Glass7ae73fc2016-08-27 12:18:38 -0600193/*
194 * Set check_regions to true to check that the payload targets usable memory.
195 * With this flag set, if it does not, the load will fail and this function
196 * will return NULL.
197 *
198 * Defined in src/lib/selfboot.c
199 */
200void *selfload(struct prog *payload, bool check_regions);
Aaron Durbin04654a22015-03-17 11:43:44 -0500201
202#endif /* PROGRAM_LOADING_H */