blob: d6da68883181092e28f55d14101d657f21ed7bf3 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Aaron Durbinbdf913a2014-02-24 14:56:34 -060019 */
Aaron Durbin04654a22015-03-17 11:43:44 -050020#ifndef PROGRAM_LOADING_H
21#define PROGRAM_LOADING_H
Aaron Durbinbdf913a2014-02-24 14:56:34 -060022
23#include <stdint.h>
24#include <stddef.h>
Aaron Durbinac12c66c2015-05-20 12:08:55 -050025#include <assets.h>
Aaron Durbinbdf913a2014-02-24 14:56:34 -060026
Aaron Durbin6e76fff2015-03-20 09:42:05 -050027enum {
28 /* Last segment of program. Can be used to take different actions for
29 * cache maintenance of a program load. */
30 SEG_FINAL = 1 << 0,
31};
Ionela Voinescu00903e52015-01-09 13:14:20 +000032
Aaron Durbin3948e532015-03-20 13:00:20 -050033/* Called for each segment of a program loaded. The SEG_FINAL flag will be
Aaron Durbin6e76fff2015-03-20 09:42:05 -050034 * set on the last segment loaded. */
35void arch_segment_loaded(uintptr_t start, size_t size, int flags);
Ionela Voinescu00903e52015-01-09 13:14:20 +000036
Aaron Durbin3948e532015-03-20 13:00:20 -050037/* Representation of a program. */
38struct prog {
Aaron Durbinac12c66c2015-05-20 12:08:55 -050039 /* The region_device within the asset is the source of program content
40 * to load. After loading program it represents the memory region of
41 * the stages and payload. For architectures that use a bounce buffer
42 * then it would represent the bounce buffer. */
43 struct asset asset;
Aaron Durbin3948e532015-03-20 13:00:20 -050044 /* Entry to program with optional argument. It's up to the architecture
45 * to decide if argument is passed. */
46 void (*entry)(void *);
47 void *arg;
48};
49
Aaron Durbinac12c66c2015-05-20 12:08:55 -050050#define PROG_INIT(type_, name_) \
51 { \
52 .asset = ASSET_INIT(type_, name_), \
53 }
54
55static inline const char *prog_name(const struct prog *prog)
56{
57 return asset_name(&prog->asset);
58}
59
60static inline enum asset_type prog_type(const struct prog *prog)
61{
62 return asset_type(&prog->asset);
63}
64
65static inline struct region_device *prog_rdev(struct prog *prog)
66{
67 return asset_rdev(&prog->asset);
68}
69
Aaron Durbin6a452ef2015-05-19 16:25:20 -050070/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050071static inline size_t prog_size(const struct prog *prog)
72{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050073 return asset_size(&prog->asset);
Aaron Durbin3948e532015-03-20 13:00:20 -050074}
75
Aaron Durbin6a452ef2015-05-19 16:25:20 -050076/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050077static inline void *prog_start(const struct prog *prog)
78{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050079 return asset_mmap(&prog->asset);
Aaron Durbin3948e532015-03-20 13:00:20 -050080}
81
82static inline void *prog_entry(const struct prog *prog)
83{
84 return prog->entry;
85}
86
87static inline void *prog_entry_arg(const struct prog *prog)
88{
89 return prog->arg;
90}
91
Aaron Durbin6a452ef2015-05-19 16:25:20 -050092/* region_device representing the 32-bit flat address space. */
93extern const struct mem_region_device addrspace_32bit;
94
95static inline void prog_memory_init(struct prog *prog, uintptr_t ptr,
96 size_t size)
97{
Aaron Durbinac12c66c2015-05-20 12:08:55 -050098 rdev_chain(&prog->asset.rdev, &addrspace_32bit.rdev, ptr, size);
Aaron Durbin6a452ef2015-05-19 16:25:20 -050099}
100
Aaron Durbin3948e532015-03-20 13:00:20 -0500101static inline void prog_set_area(struct prog *prog, void *start, size_t size)
102{
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500103 prog_memory_init(prog, (uintptr_t)start, size);
Aaron Durbin3948e532015-03-20 13:00:20 -0500104}
105
106static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
107{
108 prog->entry = e;
109 prog->arg = arg;
110}
111
Aaron Durbin899d13d2015-05-15 23:39:23 -0500112/* Locate the identified program to run. Return 0 on success. < 0 on error. */
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500113static inline int prog_locate(struct prog *prog)
114{
115 return asset_locate(&prog->asset);
116}
117
Aaron Durbinb3847e62015-03-20 15:55:08 -0500118/* Run the program described by prog. */
119void prog_run(struct prog *prog);
120/* Per architecture implementation running a program. */
121void arch_prog_run(struct prog *prog);
122/* Platform (SoC/chipset) specific overrides for running a program. This is
123 * called prior to calling the arch_prog_run. Thus, if there is anything
124 * special that needs to be done by the platform similar to the architecture
125 * code it needs to that as well. */
126void platform_prog_run(struct prog *prog);
127
Aaron Durbince9efe02015-03-20 16:37:12 -0500128struct prog_loader_ops {
129 const char *name;
Aaron Durbin5e8286b2015-04-28 15:59:12 -0500130 /* Determine if the loader is the active one. If so returns 1 else 0
131 * or < 0 on error. */
132 int (*is_loader_active)(struct prog *prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500133 /* Returns < 0 on error or 0 on success. This function locates
134 * the rdev representing the file data associated with the passed in
135 * prog. */
136 int (*locate)(struct prog *prog);
Aaron Durbince9efe02015-03-20 16:37:12 -0500137};
138
Aaron Durbind1b0e872015-03-17 13:17:06 -0500139/************************
140 * ROMSTAGE LOADING *
141 ************************/
142
143/* Run romstage from bootblock. */
144void run_romstage(void);
Aaron Durbin04654a22015-03-17 11:43:44 -0500145
146/************************
147 * RAMSTAGE LOADING *
148 ************************/
149
Aaron Durbince9efe02015-03-20 16:37:12 -0500150/* Run ramstage from romstage. */
151void run_ramstage(void);
152
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600153/* Called when the stage cache couldn't load ramstage on resume. */
154void ramstage_cache_invalid(void);
Aaron Durbinfcfdff82015-03-20 10:58:41 -0500155
Aaron Durbin04654a22015-03-17 11:43:44 -0500156/***********************
157 * PAYLOAD LOADING *
158 ***********************/
159
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500160/* Load payload into memory in preparation to run. */
161void payload_load(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600162
163/* Run the loaded payload. */
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500164void payload_run(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600165
Aaron Durbinc34713d2014-02-25 20:36:56 -0600166/* Mirror the payload to be loaded. */
Aaron Durbince9efe02015-03-20 16:37:12 -0500167void mirror_payload(struct prog *payload);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600168
169/* Defined in src/lib/selfboot.c */
Aaron Durbince9efe02015-03-20 16:37:12 -0500170void *selfload(struct prog *payload);
Aaron Durbin04654a22015-03-17 11:43:44 -0500171
172#endif /* PROGRAM_LOADING_H */