blob: cc889a688d8adf3529d040c17f68ed703526bc70 [file] [log] [blame]
Simon Glassddb39b22019-08-24 14:10:32 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
4 */
5
6#include <common.h>
7#include <handoff.h>
Simon Glass6980b6b2019-11-14 12:57:45 -07008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassddb39b22019-08-24 14:10:32 -060010#include <asm/fsp/fsp_support.h>
11#include <asm/e820.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glassddb39b22019-08-24 14:10:32 -060013#include <asm/mrccache.h>
Simon Glass8ccadee2019-12-06 21:42:12 -070014#include <asm/mtrr.h>
Simon Glassddb39b22019-08-24 14:10:32 -060015#include <asm/post.h>
Simon Glass154dc3e2020-09-22 12:45:40 -060016#include <dm/ofnode.h>
Simon Glassddb39b22019-08-24 14:10:32 -060017
18DECLARE_GLOBAL_DATA_PTR;
19
20int fsp_scan_for_ram_size(void)
21{
22 phys_size_t ram_size = 0;
23 const struct hob_header *hdr;
24 struct hob_res_desc *res_desc;
25
26 hdr = gd->arch.hob_list;
27 while (!end_of_hob(hdr)) {
28 if (hdr->type == HOB_TYPE_RES_DESC) {
29 res_desc = (struct hob_res_desc *)hdr;
30 if (res_desc->type == RES_SYS_MEM ||
31 res_desc->type == RES_MEM_RESERVED)
32 ram_size += res_desc->len;
33 }
34 hdr = get_next_hob(hdr);
35 }
36
37 gd->ram_size = ram_size;
38 post_code(POST_DRAM);
39
40 return 0;
41};
42
43int dram_init_banksize(void)
44{
Simon Glass7b9e0d42020-11-04 09:57:43 -070045 efi_guid_t fsp = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
Simon Glass75545f72019-12-06 21:42:11 -070046 const struct hob_header *hdr;
47 struct hob_res_desc *res_desc;
Simon Glass7b9e0d42020-11-04 09:57:43 -070048 phys_addr_t mtrr_top;
Simon Glass75545f72019-12-06 21:42:11 -070049 phys_addr_t low_end;
50 uint bank;
Bin Meng5ba83722021-08-02 17:45:21 +080051 bool update_mtrr;
52
53 /*
54 * For FSP1, the system memory and reserved memory used by FSP are
55 * already programmed in the MTRR by FSP. Also it is observed that
56 * FSP on Intel Queensbay platform reports the TSEG memory range
57 * that has the same RES_MEM_RESERVED resource type whose address
58 * is programmed by FSP to be near the top of 4 GiB space, which is
59 * not what we want for DRAM.
60 *
61 * However it seems FSP2's behavior is different. We need to add the
62 * DRAM range in MTRR otherwise the boot process goes very slowly,
Simon Glassbcdc0e22023-02-05 15:35:48 -070063 * which was observed on Chromebook Coral with FSP2.
Bin Meng5ba83722021-08-02 17:45:21 +080064 */
65 update_mtrr = CONFIG_IS_ENABLED(FSP_VERSION2);
Simon Glass75545f72019-12-06 21:42:11 -070066
Simon Glassd89c4a32020-04-26 09:12:53 -060067 if (!ll_boot_init()) {
68 gd->bd->bi_dram[0].start = 0;
69 gd->bd->bi_dram[0].size = gd->ram_size;
70
Bin Meng5ba83722021-08-02 17:45:21 +080071 if (update_mtrr)
72 mtrr_add_request(MTRR_TYPE_WRBACK, 0, gd->ram_size);
Simon Glassd89c4a32020-04-26 09:12:53 -060073 return 0;
74 }
75
Simon Glass7b9e0d42020-11-04 09:57:43 -070076 low_end = 0; /* top of low memory usable by U-Boot */
77 mtrr_top = 0; /* top of low memory (even if reserved) */
Simon Glass75545f72019-12-06 21:42:11 -070078 for (bank = 1, hdr = gd->arch.hob_list;
79 bank < CONFIG_NR_DRAM_BANKS && !end_of_hob(hdr);
80 hdr = get_next_hob(hdr)) {
81 if (hdr->type != HOB_TYPE_RES_DESC)
82 continue;
83 res_desc = (struct hob_res_desc *)hdr;
Simon Glass7b9e0d42020-11-04 09:57:43 -070084 if (!guidcmp(&res_desc->owner, &fsp))
85 low_end = res_desc->phys_start;
Simon Glass75545f72019-12-06 21:42:11 -070086 if (res_desc->type != RES_SYS_MEM &&
87 res_desc->type != RES_MEM_RESERVED)
88 continue;
89 if (res_desc->phys_start < (1ULL << 32)) {
Simon Glass7b9e0d42020-11-04 09:57:43 -070090 mtrr_top = max(mtrr_top,
91 res_desc->phys_start + res_desc->len);
92 } else {
93 gd->bd->bi_dram[bank].start = res_desc->phys_start;
94 gd->bd->bi_dram[bank].size = res_desc->len;
Bin Meng5ba83722021-08-02 17:45:21 +080095 if (update_mtrr)
96 mtrr_add_request(MTRR_TYPE_WRBACK,
97 res_desc->phys_start,
98 res_desc->len);
Simon Glass7b9e0d42020-11-04 09:57:43 -070099 log_debug("ram %llx %llx\n",
100 gd->bd->bi_dram[bank].start,
101 gd->bd->bi_dram[bank].size);
Simon Glass75545f72019-12-06 21:42:11 -0700102 }
Simon Glass75545f72019-12-06 21:42:11 -0700103 }
104
105 /* Add the memory below 4GB */
Simon Glassddb39b22019-08-24 14:10:32 -0600106 gd->bd->bi_dram[0].start = 0;
Simon Glass75545f72019-12-06 21:42:11 -0700107 gd->bd->bi_dram[0].size = low_end;
Simon Glassddb39b22019-08-24 14:10:32 -0600108
Simon Glass7b9e0d42020-11-04 09:57:43 -0700109 /*
110 * Set up an MTRR to the top of low, reserved memory. This is necessary
111 * for graphics to run at full speed in U-Boot.
112 */
Bin Meng5ba83722021-08-02 17:45:21 +0800113 if (update_mtrr)
114 mtrr_add_request(MTRR_TYPE_WRBACK, 0, mtrr_top);
Simon Glass8ccadee2019-12-06 21:42:12 -0700115
Simon Glassddb39b22019-08-24 14:10:32 -0600116 return 0;
117}
118
119unsigned int install_e820_map(unsigned int max_entries,
120 struct e820_entry *entries)
121{
122 unsigned int num_entries = 0;
123 const struct hob_header *hdr;
124 struct hob_res_desc *res_desc;
Simon Glass154dc3e2020-09-22 12:45:40 -0600125 const fdt64_t *prop;
126 int size;
Simon Glassddb39b22019-08-24 14:10:32 -0600127
128 hdr = gd->arch.hob_list;
129
130 while (!end_of_hob(hdr)) {
131 if (hdr->type == HOB_TYPE_RES_DESC) {
132 res_desc = (struct hob_res_desc *)hdr;
133 entries[num_entries].addr = res_desc->phys_start;
134 entries[num_entries].size = res_desc->len;
135
136 if (res_desc->type == RES_SYS_MEM)
137 entries[num_entries].type = E820_RAM;
138 else if (res_desc->type == RES_MEM_RESERVED)
139 entries[num_entries].type = E820_RESERVED;
140
141 num_entries++;
142 }
143 hdr = get_next_hob(hdr);
144 }
145
146 /* Mark PCIe ECAM address range as reserved */
147 entries[num_entries].addr = CONFIG_PCIE_ECAM_BASE;
148 entries[num_entries].size = CONFIG_PCIE_ECAM_SIZE;
149 entries[num_entries].type = E820_RESERVED;
150 num_entries++;
151
Simon Glasse6ad2022020-07-09 18:43:16 -0600152 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
153 ulong stack_size;
154
155 stack_size = CONFIG_IS_ENABLED(HAVE_ACPI_RESUME,
Heinrich Schuchardt99186b32020-07-29 12:31:17 +0200156 (CONFIG_STACK_SIZE_RESUME), (0));
Simon Glasse6ad2022020-07-09 18:43:16 -0600157 /*
158 * Everything between U-Boot's stack and ram top needs to be
159 * reserved in order for ACPI S3 resume to work.
160 */
161 entries[num_entries].addr = gd->start_addr_sp - stack_size;
162 entries[num_entries].size = gd->ram_top - gd->start_addr_sp +
163 stack_size;
164 entries[num_entries].type = E820_RESERVED;
165 num_entries++;
166 }
Simon Glassddb39b22019-08-24 14:10:32 -0600167
Simon Glass154dc3e2020-09-22 12:45:40 -0600168 prop = ofnode_read_chosen_prop("e820-entries", &size);
169 if (prop) {
170 int count = size / (sizeof(u64) * 3);
171 int i;
172
173 if (num_entries + count >= max_entries)
174 return -ENOSPC;
175 for (i = 0; i < count; i++, num_entries++, prop += 3) {
176 entries[num_entries].addr = fdt64_to_cpu(prop[0]);
177 entries[num_entries].size = fdt64_to_cpu(prop[1]);
178 entries[num_entries].type = fdt64_to_cpu(prop[2]);
179 }
180 }
181
Simon Glassddb39b22019-08-24 14:10:32 -0600182 return num_entries;
183}
Simon Glass25628082019-09-25 08:11:41 -0600184
185#if CONFIG_IS_ENABLED(HANDOFF) && IS_ENABLED(CONFIG_USE_HOB)
186int handoff_arch_save(struct spl_handoff *ho)
187{
Simon Glass7b9e0d42020-11-04 09:57:43 -0700188 ho->arch.usable_ram_top = gd->bd->bi_dram[0].size;
Simon Glass25628082019-09-25 08:11:41 -0600189 ho->arch.hob_list = gd->arch.hob_list;
190
191 return 0;
192}
193#endif