blob: 730721dc17685df882bef73c733793e97005da44 [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
Simon Glassddb39b22019-08-24 14:10:32 -06006#include <handoff.h>
Simon Glass6980b6b2019-11-14 12:57:45 -07007#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glassddb39b22019-08-24 14:10:32 -06009#include <asm/fsp/fsp_support.h>
10#include <asm/e820.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glassddb39b22019-08-24 14:10:32 -060012#include <asm/mrccache.h>
Simon Glass8ccadee2019-12-06 21:42:12 -070013#include <asm/mtrr.h>
Simon Glassddb39b22019-08-24 14:10:32 -060014#include <asm/post.h>
Simon Glass154dc3e2020-09-22 12:45:40 -060015#include <dm/ofnode.h>
Simon Glassddb39b22019-08-24 14:10:32 -060016
17DECLARE_GLOBAL_DATA_PTR;
18
19int fsp_scan_for_ram_size(void)
20{
21 phys_size_t ram_size = 0;
22 const struct hob_header *hdr;
23 struct hob_res_desc *res_desc;
24
25 hdr = gd->arch.hob_list;
26 while (!end_of_hob(hdr)) {
27 if (hdr->type == HOB_TYPE_RES_DESC) {
28 res_desc = (struct hob_res_desc *)hdr;
29 if (res_desc->type == RES_SYS_MEM ||
30 res_desc->type == RES_MEM_RESERVED)
31 ram_size += res_desc->len;
32 }
33 hdr = get_next_hob(hdr);
34 }
35
36 gd->ram_size = ram_size;
37 post_code(POST_DRAM);
38
39 return 0;
40};
41
42int dram_init_banksize(void)
43{
Simon Glass7b9e0d42020-11-04 09:57:43 -070044 efi_guid_t fsp = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
Simon Glass75545f72019-12-06 21:42:11 -070045 const struct hob_header *hdr;
46 struct hob_res_desc *res_desc;
Simon Glass7b9e0d42020-11-04 09:57:43 -070047 phys_addr_t mtrr_top;
Simon Glass75545f72019-12-06 21:42:11 -070048 phys_addr_t low_end;
49 uint bank;
Bin Meng5ba83722021-08-02 17:45:21 +080050 bool update_mtrr;
51
52 /*
53 * For FSP1, the system memory and reserved memory used by FSP are
54 * already programmed in the MTRR by FSP. Also it is observed that
55 * FSP on Intel Queensbay platform reports the TSEG memory range
56 * that has the same RES_MEM_RESERVED resource type whose address
57 * is programmed by FSP to be near the top of 4 GiB space, which is
58 * not what we want for DRAM.
59 *
60 * However it seems FSP2's behavior is different. We need to add the
61 * DRAM range in MTRR otherwise the boot process goes very slowly,
Simon Glassbcdc0e22023-02-05 15:35:48 -070062 * which was observed on Chromebook Coral with FSP2.
Bin Meng5ba83722021-08-02 17:45:21 +080063 */
64 update_mtrr = CONFIG_IS_ENABLED(FSP_VERSION2);
Simon Glass75545f72019-12-06 21:42:11 -070065
Simon Glassd89c4a32020-04-26 09:12:53 -060066 if (!ll_boot_init()) {
67 gd->bd->bi_dram[0].start = 0;
68 gd->bd->bi_dram[0].size = gd->ram_size;
69
Bin Meng5ba83722021-08-02 17:45:21 +080070 if (update_mtrr)
71 mtrr_add_request(MTRR_TYPE_WRBACK, 0, gd->ram_size);
Simon Glassd89c4a32020-04-26 09:12:53 -060072 return 0;
73 }
74
Simon Glass7b9e0d42020-11-04 09:57:43 -070075 low_end = 0; /* top of low memory usable by U-Boot */
76 mtrr_top = 0; /* top of low memory (even if reserved) */
Simon Glass75545f72019-12-06 21:42:11 -070077 for (bank = 1, hdr = gd->arch.hob_list;
78 bank < CONFIG_NR_DRAM_BANKS && !end_of_hob(hdr);
79 hdr = get_next_hob(hdr)) {
80 if (hdr->type != HOB_TYPE_RES_DESC)
81 continue;
82 res_desc = (struct hob_res_desc *)hdr;
Simon Glass7b9e0d42020-11-04 09:57:43 -070083 if (!guidcmp(&res_desc->owner, &fsp))
84 low_end = res_desc->phys_start;
Simon Glass75545f72019-12-06 21:42:11 -070085 if (res_desc->type != RES_SYS_MEM &&
86 res_desc->type != RES_MEM_RESERVED)
87 continue;
88 if (res_desc->phys_start < (1ULL << 32)) {
Simon Glass7b9e0d42020-11-04 09:57:43 -070089 mtrr_top = max(mtrr_top,
90 res_desc->phys_start + res_desc->len);
91 } else {
92 gd->bd->bi_dram[bank].start = res_desc->phys_start;
93 gd->bd->bi_dram[bank].size = res_desc->len;
Bin Meng5ba83722021-08-02 17:45:21 +080094 if (update_mtrr)
95 mtrr_add_request(MTRR_TYPE_WRBACK,
96 res_desc->phys_start,
97 res_desc->len);
Simon Glass7b9e0d42020-11-04 09:57:43 -070098 log_debug("ram %llx %llx\n",
99 gd->bd->bi_dram[bank].start,
100 gd->bd->bi_dram[bank].size);
Simon Glass75545f72019-12-06 21:42:11 -0700101 }
Simon Glass75545f72019-12-06 21:42:11 -0700102 }
103
104 /* Add the memory below 4GB */
Simon Glassddb39b22019-08-24 14:10:32 -0600105 gd->bd->bi_dram[0].start = 0;
Simon Glass75545f72019-12-06 21:42:11 -0700106 gd->bd->bi_dram[0].size = low_end;
Simon Glassddb39b22019-08-24 14:10:32 -0600107
Simon Glass7b9e0d42020-11-04 09:57:43 -0700108 /*
109 * Set up an MTRR to the top of low, reserved memory. This is necessary
110 * for graphics to run at full speed in U-Boot.
111 */
Bin Meng5ba83722021-08-02 17:45:21 +0800112 if (update_mtrr)
113 mtrr_add_request(MTRR_TYPE_WRBACK, 0, mtrr_top);
Simon Glass8ccadee2019-12-06 21:42:12 -0700114
Simon Glassddb39b22019-08-24 14:10:32 -0600115 return 0;
116}
117
118unsigned int install_e820_map(unsigned int max_entries,
119 struct e820_entry *entries)
120{
121 unsigned int num_entries = 0;
122 const struct hob_header *hdr;
123 struct hob_res_desc *res_desc;
Simon Glass154dc3e2020-09-22 12:45:40 -0600124 const fdt64_t *prop;
125 int size;
Simon Glassddb39b22019-08-24 14:10:32 -0600126
127 hdr = gd->arch.hob_list;
128
129 while (!end_of_hob(hdr)) {
130 if (hdr->type == HOB_TYPE_RES_DESC) {
131 res_desc = (struct hob_res_desc *)hdr;
132 entries[num_entries].addr = res_desc->phys_start;
133 entries[num_entries].size = res_desc->len;
134
135 if (res_desc->type == RES_SYS_MEM)
136 entries[num_entries].type = E820_RAM;
137 else if (res_desc->type == RES_MEM_RESERVED)
138 entries[num_entries].type = E820_RESERVED;
139
140 num_entries++;
141 }
142 hdr = get_next_hob(hdr);
143 }
144
145 /* Mark PCIe ECAM address range as reserved */
146 entries[num_entries].addr = CONFIG_PCIE_ECAM_BASE;
147 entries[num_entries].size = CONFIG_PCIE_ECAM_SIZE;
148 entries[num_entries].type = E820_RESERVED;
149 num_entries++;
150
Simon Glasse6ad2022020-07-09 18:43:16 -0600151 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
152 ulong stack_size;
153
154 stack_size = CONFIG_IS_ENABLED(HAVE_ACPI_RESUME,
Heinrich Schuchardt99186b32020-07-29 12:31:17 +0200155 (CONFIG_STACK_SIZE_RESUME), (0));
Simon Glasse6ad2022020-07-09 18:43:16 -0600156 /*
157 * Everything between U-Boot's stack and ram top needs to be
158 * reserved in order for ACPI S3 resume to work.
159 */
160 entries[num_entries].addr = gd->start_addr_sp - stack_size;
161 entries[num_entries].size = gd->ram_top - gd->start_addr_sp +
162 stack_size;
163 entries[num_entries].type = E820_RESERVED;
164 num_entries++;
165 }
Simon Glassddb39b22019-08-24 14:10:32 -0600166
Simon Glass154dc3e2020-09-22 12:45:40 -0600167 prop = ofnode_read_chosen_prop("e820-entries", &size);
168 if (prop) {
169 int count = size / (sizeof(u64) * 3);
170 int i;
171
172 if (num_entries + count >= max_entries)
173 return -ENOSPC;
174 for (i = 0; i < count; i++, num_entries++, prop += 3) {
175 entries[num_entries].addr = fdt64_to_cpu(prop[0]);
176 entries[num_entries].size = fdt64_to_cpu(prop[1]);
177 entries[num_entries].type = fdt64_to_cpu(prop[2]);
178 }
179 }
180
Simon Glassddb39b22019-08-24 14:10:32 -0600181 return num_entries;
182}
Simon Glass25628082019-09-25 08:11:41 -0600183
184#if CONFIG_IS_ENABLED(HANDOFF) && IS_ENABLED(CONFIG_USE_HOB)
185int handoff_arch_save(struct spl_handoff *ho)
186{
Simon Glass7b9e0d42020-11-04 09:57:43 -0700187 ho->arch.usable_ram_top = gd->bd->bi_dram[0].size;
Simon Glass25628082019-09-25 08:11:41 -0600188 ho->arch.hob_list = gd->arch.hob_list;
189
190 return 0;
191}
192#endif