1 /* Copyright 2013-2014 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __SKIBOOT_H
18 #define __SKIBOOT_H
19 
20 #include <compiler.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <bitutils.h>
29 #include <types.h>
30 
31 #include <ccan/container_of/container_of.h>
32 #include <ccan/list/list.h>
33 #include <ccan/short_types/short_types.h>
34 #include <ccan/build_assert/build_assert.h>
35 #include <ccan/array_size/array_size.h>
36 #include <ccan/endian/endian.h>
37 #include <ccan/str/str.h>
38 
39 #include <libflash/blocklevel.h>
40 
41 #include <mem-map.h>
42 #include <op-panel.h>
43 #include <platform.h>
44 
45 /* Special ELF sections */
46 #define __force_data		__section(".force.data")
47 
48 struct mem_region;
49 extern struct mem_region *mem_region_next(struct mem_region *region);
50 
51 /* Misc linker script symbols */
52 extern char _start[];
53 extern char _stext[];
54 extern char _etext[];
55 extern char __sym_map_end[];
56 extern char _romem_end[];
57 
58 #ifndef __TESTING__
59 /* Readonly section start and end. */
60 extern char __rodata_start[], __rodata_end[];
61 
is_rodata(const void * p)62 static inline bool is_rodata(const void *p)
63 {
64 	return ((const char *)p >= __rodata_start && (const char *)p < __rodata_end);
65 }
66 #else
is_rodata(const void * p)67 static inline bool is_rodata(const void *p)
68 {
69 	return false;
70 }
71 #endif
72 
73 /* Console logging
74  * Update console_get_level() if you add here
75  */
76 #define PR_EMERG	0
77 #define PR_ALERT	1
78 #define PR_CRIT		2
79 #define PR_ERR		3
80 #define PR_WARNING	4
81 #define PR_NOTICE	5
82 #define PR_PRINTF	PR_NOTICE
83 #define PR_INFO		6
84 #define PR_DEBUG	7
85 #define PR_TRACE	8
86 #define PR_INSANE	9
87 
88 #ifndef pr_fmt
89 #define pr_fmt(fmt) fmt
90 #endif
91 
92 void _prlog(int log_level, const char* fmt, ...) __attribute__((format (printf, 2, 3)));
93 #define prlog(l, f, ...) do { _prlog(l, pr_fmt(f), ##__VA_ARGS__); } while(0)
94 #define prerror(fmt...)	do { prlog(PR_ERR, fmt); } while(0)
95 #define prlog_once(arg, ...)	 		\
96 ({						\
97 	static bool __prlog_once = false;	\
98 	if (!__prlog_once) {			\
99 		__prlog_once = true;		\
100 		prlog(arg, ##__VA_ARGS__);	\
101 	}					\
102 })
103 
104 /* Location codes  -- at most 80 chars with null termination */
105 #define LOC_CODE_SIZE	80
106 
107 /* Processor generation */
108 enum proc_gen {
109 	proc_gen_unknown,
110 	proc_gen_p8,
111 	proc_gen_p9,
112 };
113 extern enum proc_gen proc_gen;
114 
115 extern unsigned int pcie_max_link_speed;
116 
117 /* Convert a 4-bit number to a hex char */
118 extern char __attrconst tohex(uint8_t nibble);
119 
120 #ifndef __TEST__
121 /* Bit position of the most significant 1-bit (LSB=0, MSB=63) */
ilog2(unsigned long val)122 static inline int ilog2(unsigned long val)
123 {
124 	int left_zeros;
125 
126 	asm volatile ("cntlzd %0,%1" : "=r" (left_zeros) : "r" (val));
127 
128 	return 63 - left_zeros;
129 }
130 
is_pow2(unsigned long val)131 static inline bool is_pow2(unsigned long val)
132 {
133 	return val == (1ul << ilog2(val));
134 }
135 #endif
136 
137 #define lo32(x)	((x) & 0xffffffff)
138 #define hi32(x)	(((x) >> 32) & 0xffffffff)
139 
140 /* WARNING: _a *MUST* be a power of two */
141 #define ALIGN_UP(_v, _a)	(((_v) + (_a) - 1) & ~((_a) - 1))
142 #define ALIGN_DOWN(_v, _a)	((_v) & ~((_a) - 1))
143 
144 /* TCE alignment */
145 #define TCE_SHIFT	12
146 #define TCE_PSIZE	(1ul << 12)
147 #define TCE_MASK	(TCE_PSIZE - 1)
148 
149 /* Not the greatest variants but will do for now ... */
150 #define MIN(a, b)	((a) < (b) ? (a) : (b))
151 #define MAX(a, b)	((a) > (b) ? (a) : (b))
152 
153 /* Clean the stray high bit which the FSP inserts: we only have 52 bits real */
cleanup_addr(u64 addr)154 static inline u64 cleanup_addr(u64 addr)
155 {
156 	return addr & ((1ULL << 52) - 1);
157 }
158 
159 /* Start the kernel */
160 extern void start_kernel(uint64_t entry, void* fdt,
161 			 uint64_t mem_top) __noreturn;
162 extern void start_kernel32(uint64_t entry, void* fdt,
163 			   uint64_t mem_top) __noreturn;
164 extern void start_kernel_secondary(uint64_t entry) __noreturn;
165 
166 /* Get description of machine from HDAT and create device-tree */
167 extern int parse_hdat(bool is_opal);
168 
169 struct dt_node;
170 
171 /* Add /cpus/features node for boot environment that passes an fdt */
172 extern void dt_add_cpufeatures(struct dt_node *root);
173 
174 /* Root of device tree. */
175 extern struct dt_node *dt_root;
176 
177 /* Full skiboot version number (possibly includes gitid). */
178 extern const char version[];
179 
180 /* Debug support */
181 extern char __sym_map_start[];
182 extern char __sym_map_end[];
183 extern size_t snprintf_symbol(char *buf, size_t len, uint64_t addr);
184 
185 /* Direct controls */
186 extern void direct_controls_init(void);
187 extern int64_t opal_signal_system_reset(int cpu_nr);
188 
189 /* Fast reboot support */
190 extern void disable_fast_reboot(const char *reason);
191 extern void add_fast_reboot_dt_entries(void);
192 extern void fast_reboot(void);
193 extern void __noreturn __secondary_cpu_entry(void);
194 extern void __noreturn load_and_boot_kernel(bool is_reboot);
195 extern void cleanup_local_tlb(void);
196 extern void cleanup_global_tlb(void);
197 extern void init_shared_sprs(void);
198 extern void init_replicated_sprs(void);
199 extern bool start_preload_kernel(void);
200 extern void copy_exception_vectors(void);
201 extern void copy_sreset_vector(void);
202 extern void copy_sreset_vector_fast_reboot(void);
203 
204 /* Various probe routines, to replace with an initcall system */
205 extern void probe_phb3(void);
206 extern void probe_phb4(void);
207 extern int preload_capp_ucode(void);
208 extern void preload_io_vpd(void);
209 extern void probe_npu(void);
210 extern void probe_npu2(void);
211 extern void uart_init(void);
212 extern void mbox_init(void);
213 extern void early_uart_init(void);
214 extern void homer_init(void);
215 extern void slw_init(void);
216 extern void add_cpu_idle_state_properties(void);
217 extern void lpc_rtc_init(void);
218 
219 /* flash support */
220 struct flash_chip;
221 extern int flash_register(struct blocklevel_device *bl);
222 extern int flash_start_preload_resource(enum resource_id id, uint32_t subid,
223 					void *buf, size_t *len);
224 extern int flash_resource_loaded(enum resource_id id, uint32_t idx);
225 extern bool flash_reserve(void);
226 extern void flash_release(void);
227 #define FLASH_SUBPART_ALIGNMENT 0x1000
228 #define FLASH_SUBPART_HEADER_SIZE FLASH_SUBPART_ALIGNMENT
229 extern int flash_subpart_info(void *part_header, uint32_t header_len,
230 			      uint32_t part_size, uint32_t *part_actual,
231 			      uint32_t subid, uint32_t *offset,
232 			      uint32_t *size);
233 extern void flash_fw_version_preload(void);
234 extern void flash_dt_add_fw_version(void);
235 extern const char *flash_map_resource_name(enum resource_id id);
236 
237 /*
238  * Decompression routines
239  *
240  * The below structure members are needed for the xz library routines,
241  *   src: Source address (The compressed binary)
242  *   src_size: Source size
243  *   dst: Destination address (The memory area where the `src` will be
244  *        decompressed)
245  *   dst_size: Destination size
246  */
247 struct xz_decompress {
248 	void *dst;
249 	void *src;
250 	size_t dst_size;
251 	size_t src_size;
252 	/* The status of the decompress process:
253 	     - OPAL_PARTIAL: if the job is in progress
254 	     - OPAL_SUCCESS: if the job is successful
255 	     - OPAL_NO_MEM: memory allocation failure
256 	     - OPAL_PARAMETER: If any of the above (src, dst..) are invalid or
257 	     if xz decompress fails. In which case the caller should check the
258 	     xz_error for failure reason.
259 	 */
260 	int status;
261 	int xz_error;
262 	/* The decompression job, this will be freed if the caller uses
263 	 * `wait_xz_decompression` function, in any other case its the
264 	 * responsibility of caller to free the allocation job.  */
265 	struct cpu_job *job;
266 };
267 
268 extern void xz_start_decompress(struct xz_decompress *);
269 extern void wait_xz_decompress(struct xz_decompress *);
270 
271 /* NVRAM support */
272 extern void nvram_init(void);
273 extern void nvram_read_complete(bool success);
274 
275 /* UART stuff */
276 enum {
277 	UART_CONSOLE_OPAL,
278 	UART_CONSOLE_OS
279 };
280 extern void uart_set_console_policy(int policy);
281 extern bool uart_enabled(void);
282 
283 /* PRD */
284 extern void prd_psi_interrupt(uint32_t proc);
285 extern void prd_tmgt_interrupt(uint32_t proc);
286 extern void prd_occ_reset(uint32_t proc);
287 extern void prd_sbe_passthrough(uint32_t proc);
288 extern void prd_init(void);
289 extern void prd_register_reserved_memory(void);
290 extern void prd_fsp_occ_reset(uint32_t proc);
291 extern void prd_fsp_occ_load_start(u32 proc);
292 extern void prd_fw_resp_fsp_response(int status);
293 extern int  prd_hbrt_fsp_msg_notify(void *data, u32 dsize);
294 
295 /* Flatten device-tree */
296 extern void *create_dtb(const struct dt_node *root, bool exclusive);
297 
298 /* Track failure in Wakup engine */
299 enum wakeup_engine_states {
300 	WAKEUP_ENGINE_NOT_PRESENT,
301 	WAKEUP_ENGINE_PRESENT,
302 	WAKEUP_ENGINE_FAILED
303 };
304 extern enum wakeup_engine_states wakeup_engine_state;
305 extern bool has_deep_states;
306 extern void nx_p9_rng_late_init(void);
307 extern void xive_late_init(void);
308 
309 
310 
311 /* SLW reinit function for switching core settings */
312 extern int64_t slw_reinit(uint64_t flags);
313 
314 /* Patch SPR in SLW image */
315 extern int64_t opal_slw_set_reg(uint64_t cpu_pir, uint64_t sprn, uint64_t val);
316 
317 extern void fast_sleep_exit(void);
318 
319 /* Fallback fake RTC */
320 extern void fake_rtc_init(void);
321 
322 /* Exceptions */
323 struct stack_frame;
324 extern void exception_entry(struct stack_frame *stack);
325 extern void exception_entry_pm_sreset(void);
326 extern void exception_entry_pm_mce(void);
327 
328 /* Assembly in head.S */
329 extern void disable_machine_check(void);
330 extern void enable_machine_check(void);
331 extern unsigned int enter_p8_pm_state(bool winkle);
332 extern unsigned int enter_p9_pm_state(uint64_t psscr);
333 extern void enter_p9_pm_lite_state(uint64_t psscr);
334 extern uint32_t reset_patch_start;
335 extern uint32_t reset_patch_end;
336 extern uint32_t reset_fast_reboot_patch_start;
337 extern uint32_t reset_fast_reboot_patch_end;
338 
339 /* Fallback fake NVRAM */
340 extern int fake_nvram_info(uint32_t *total_size);
341 extern int fake_nvram_start_read(void *dst, uint32_t src, uint32_t len);
342 extern int fake_nvram_write(uint32_t offset, void *src, uint32_t size);
343 
344 #endif /* __SKIBOOT_H */
345