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