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