blob: f71dcb79e589b2922de40eee21dadd87c1585b92 [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 Durbin3948e532015-03-20 13:00:20 -050040/* Called for each segment of a program loaded. The SEG_FINAL flag will be
Aaron Durbin6e76fff2015-03-20 09:42:05 -050041 * set on the last segment loaded. */
42void arch_segment_loaded(uintptr_t start, size_t size, int flags);
Ionela Voinescu00903e52015-01-09 13:14:20 +000043
Aaron Durbin3948e532015-03-20 13:00:20 -050044/* Representation of a program. */
45struct prog {
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060046 /* 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 Durbinac12c66c2015-05-20 12:08:55 -050049 * then it would represent the bounce buffer. */
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060050 enum prog_type type;
51 const char *name;
52 struct region_device rdev;
Aaron Durbin3948e532015-03-20 13:00:20 -050053 /* 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 Durbinac12c66c2015-05-20 12:08:55 -050059#define PROG_INIT(type_, name_) \
60 { \
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060061 .type = (type_), \
62 .name = (name_), \
Aaron Durbinac12c66c2015-05-20 12:08:55 -050063 }
64
65static inline const char *prog_name(const struct prog *prog)
66{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060067 return prog->name;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050068}
69
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060070static inline enum prog_type prog_type(const struct prog *prog)
Aaron Durbinac12c66c2015-05-20 12:08:55 -050071{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060072 return prog->type;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050073}
74
75static inline struct region_device *prog_rdev(struct prog *prog)
76{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060077 return &prog->rdev;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050078}
79
Aaron Durbin6a452ef2015-05-19 16:25:20 -050080/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050081static inline size_t prog_size(const struct prog *prog)
82{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060083 return region_device_sz(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050084}
85
Aaron Durbin6a452ef2015-05-19 16:25:20 -050086/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050087static inline void *prog_start(const struct prog *prog)
88{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060089 return rdev_mmap_full(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050090}
91
92static inline void *prog_entry(const struct prog *prog)
93{
94 return prog->entry;
95}
96
97static inline void *prog_entry_arg(const struct prog *prog)
98{
99 return prog->arg;
100}
101
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500102/* region_device representing the 32-bit flat address space. */
103extern const struct mem_region_device addrspace_32bit;
104
105static inline void prog_memory_init(struct prog *prog, uintptr_t ptr,
106 size_t size)
107{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600108 rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size);
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500109}
110
Aaron Durbin3948e532015-03-20 13:00:20 -0500111static inline void prog_set_area(struct prog *prog, void *start, size_t size)
112{
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500113 prog_memory_init(prog, (uintptr_t)start, size);
Aaron Durbin3948e532015-03-20 13:00:20 -0500114}
115
116static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
117{
118 prog->entry = e;
119 prog->arg = arg;
120}
121
Aaron Durbin899d13d2015-05-15 23:39:23 -0500122/* Locate the identified program to run. Return 0 on success. < 0 on error. */
Aaron Durbin6d720f32015-12-08 17:00:23 -0600123int prog_locate(struct prog *prog);
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500124
Aaron Durbinb3847e62015-03-20 15:55:08 -0500125/* Run the program described by prog. */
126void prog_run(struct prog *prog);
127/* Per architecture implementation running a program. */
128void 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. */
133void platform_prog_run(struct prog *prog);
134
Aaron Durbince9efe02015-03-20 16:37:12 -0500135struct prog_loader_ops {
136 const char *name;
Aaron Durbin5e8286b2015-04-28 15:59:12 -0500137 /* 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 Durbin899d13d2015-05-15 23:39:23 -0500140 /* 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 Durbince9efe02015-03-20 16:37:12 -0500144};
145
Aaron Durbind1b0e872015-03-17 13:17:06 -0500146/************************
147 * ROMSTAGE LOADING *
148 ************************/
149
150/* Run romstage from bootblock. */
151void run_romstage(void);
Aaron Durbin04654a22015-03-17 11:43:44 -0500152
153/************************
154 * RAMSTAGE LOADING *
155 ************************/
156
Aaron Durbince9efe02015-03-20 16:37:12 -0500157/* Run ramstage from romstage. */
158void run_ramstage(void);
159
Aaron Durbinbd74a4b2015-03-06 23:17:33 -0600160/* Called when the stage cache couldn't load ramstage on resume. */
161void ramstage_cache_invalid(void);
Aaron Durbinfcfdff82015-03-20 10:58:41 -0500162
Aaron Durbin04654a22015-03-17 11:43:44 -0500163/***********************
164 * PAYLOAD LOADING *
165 ***********************/
166
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500167/* Load payload into memory in preparation to run. */
168void payload_load(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600169
170/* Run the loaded payload. */
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500171void payload_run(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600172
Aaron Durbinc34713d2014-02-25 20:36:56 -0600173/* Mirror the payload to be loaded. */
Aaron Durbince9efe02015-03-20 16:37:12 -0500174void mirror_payload(struct prog *payload);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600175
176/* Defined in src/lib/selfboot.c */
Aaron Durbince9efe02015-03-20 16:37:12 -0500177void *selfload(struct prog *payload);
Aaron Durbin04654a22015-03-17 11:43:44 -0500178
179#endif /* PROGRAM_LOADING_H */