xref: /openbmc/u-boot/arch/x86/cpu/broadwell/refcode.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Read a coreboot rmodule and execute it.
4  * The rmodule_header struct is from coreboot.
5  *
6  * Copyright (c) 2016 Google, Inc
7  */
8 
9 #include <common.h>
10 #include <errno.h>
11 #include <asm/arch/pei_data.h>
12 
13 #define RMODULE_MAGIC		0xf8fe
14 #define RMODULE_VERSION_1	1
15 
16 /*
17  * All fields with '_offset' in the name are byte offsets into the flat blob.
18  * The linker and the linker script takes are of assigning the values.
19  */
20 struct rmodule_header {
21 	uint16_t magic;
22 	uint8_t version;
23 	uint8_t type;
24 	/* The payload represents the program's loadable code and data */
25 	uint32_t payload_begin_offset;
26 	uint32_t payload_end_offset;
27 	/* Begin and of relocation information about the program module */
28 	uint32_t relocations_begin_offset;
29 	uint32_t relocations_end_offset;
30 	/*
31 	 * The starting address of the linked program. This address is vital
32 	 * for determining relocation offsets as the relocation info and other
33 	 * symbols (bss, entry point) need this value as a basis to calculate
34 	 * the offsets.
35 	 */
36 	uint32_t module_link_start_address;
37 	/*
38 	 * The module_program_size is the size of memory used while running
39 	 * the program. The program is assumed to consume a contiguous amount
40 	 * of memory
41 	 */
42 	uint32_t module_program_size;
43 	/* This is program's execution entry point */
44 	uint32_t module_entry_point;
45 	/*
46 	 * Optional parameter structure that can be used to pass data into
47 	 * the module
48 	 */
49 	uint32_t parameters_begin;
50 	uint32_t parameters_end;
51 	/* BSS section information so the loader can clear the bss */
52 	uint32_t bss_begin;
53 	uint32_t bss_end;
54 	/* Add some room for growth */
55 	uint32_t padding[4];
56 } __packed;
57 
58 /**
59  * cpu_run_reference_code() - Run the platform reference code
60  *
61  * Some platforms require a binary blob to be executed once SDRAM is
62  * available. This is used to set up various platform features, such as the
63  * platform controller hub (PCH). This function should be implemented by the
64  * CPU-specific code.
65  *
66  * @return 0 on success, -ve on failure
67  */
68 static int cpu_run_reference_code(void)
69 {
70 	struct pei_data _pei_data __aligned(8);
71 	struct pei_data *pei_data = &_pei_data;
72 	asmlinkage int (*func)(void *);
73 	struct rmodule_header *hdr;
74 	char *src, *dest;
75 	int ret, dummy;
76 	int size;
77 
78 	hdr = (struct rmodule_header *)CONFIG_X86_REFCODE_ADDR;
79 	debug("Extracting code from rmodule at %p\n", hdr);
80 	if (hdr->magic != RMODULE_MAGIC) {
81 		debug("Invalid rmodule magic\n");
82 		return -EINVAL;
83 	}
84 	if (hdr->module_link_start_address != 0) {
85 		debug("Link start address must be 0\n");
86 		return -EPERM;
87 	}
88 	if (hdr->module_entry_point != 0) {
89 		debug("Entry point must be 0\n");
90 		return -EPERM;
91 	}
92 
93 	memset(pei_data, '\0', sizeof(struct pei_data));
94 	broadwell_fill_pei_data(pei_data);
95 	mainboard_fill_pei_data(pei_data);
96 	pei_data->saved_data = (void *)&dummy;
97 
98 	src = (char *)hdr + hdr->payload_begin_offset;
99 	dest = (char *)CONFIG_X86_REFCODE_RUN_ADDR;
100 
101 	size = hdr->payload_end_offset - hdr->payload_begin_offset;
102 	debug("Copying refcode from %p to %p, size %x\n", src, dest, size);
103 	memcpy(dest, src, size);
104 
105 	size = hdr->bss_end - hdr->bss_begin;
106 	debug("Zeroing BSS at %p, size %x\n", dest + hdr->bss_begin, size);
107 	memset(dest + hdr->bss_begin, '\0', size);
108 
109 	func = (asmlinkage int (*)(void *))dest;
110 	debug("Running reference code at %p\n", func);
111 #ifdef DEBUG
112 	print_buffer(CONFIG_X86_REFCODE_RUN_ADDR, (void *)func, 1, 0x40, 0);
113 #endif
114 	ret = func(pei_data);
115 	if (ret != 0) {
116 		debug("Reference code returned %d\n", ret);
117 		return -EL2HLT;
118 	}
119 	debug("Refereence code completed\n");
120 
121 	return 0;
122 }
123 
124 int arch_early_init_r(void)
125 {
126 	return cpu_run_reference_code();
127 }
128