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