blob: a76497d4e01bf345737123d7fb0ad765979dfc80 [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>
12#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 Glass75545f72019-12-06 21:42:11 -070044 const struct hob_header *hdr;
45 struct hob_res_desc *res_desc;
46 phys_addr_t low_end;
47 uint bank;
48
Simon Glassd89c4a32020-04-26 09:12:53 -060049 if (!ll_boot_init()) {
50 gd->bd->bi_dram[0].start = 0;
51 gd->bd->bi_dram[0].size = gd->ram_size;
52
53 mtrr_add_request(MTRR_TYPE_WRBACK, 0, gd->ram_size);
54 return 0;
55 }
56
Simon Glass75545f72019-12-06 21:42:11 -070057 low_end = 0;
58 for (bank = 1, hdr = gd->arch.hob_list;
59 bank < CONFIG_NR_DRAM_BANKS && !end_of_hob(hdr);
60 hdr = get_next_hob(hdr)) {
61 if (hdr->type != HOB_TYPE_RES_DESC)
62 continue;
63 res_desc = (struct hob_res_desc *)hdr;
64 if (res_desc->type != RES_SYS_MEM &&
65 res_desc->type != RES_MEM_RESERVED)
66 continue;
67 if (res_desc->phys_start < (1ULL << 32)) {
68 low_end = max(low_end,
69 res_desc->phys_start + res_desc->len);
70 continue;
71 }
72
73 gd->bd->bi_dram[bank].start = res_desc->phys_start;
74 gd->bd->bi_dram[bank].size = res_desc->len;
Simon Glass8ccadee2019-12-06 21:42:12 -070075 mtrr_add_request(MTRR_TYPE_WRBACK, res_desc->phys_start,
76 res_desc->len);
Simon Glass75545f72019-12-06 21:42:11 -070077 log_debug("ram %llx %llx\n", gd->bd->bi_dram[bank].start,
78 gd->bd->bi_dram[bank].size);
79 }
80
81 /* Add the memory below 4GB */
Simon Glassddb39b22019-08-24 14:10:32 -060082 gd->bd->bi_dram[0].start = 0;
Simon Glass75545f72019-12-06 21:42:11 -070083 gd->bd->bi_dram[0].size = low_end;
Simon Glassddb39b22019-08-24 14:10:32 -060084
Simon Glass8ccadee2019-12-06 21:42:12 -070085 mtrr_add_request(MTRR_TYPE_WRBACK, 0, low_end);
86
Simon Glassddb39b22019-08-24 14:10:32 -060087 return 0;
88}
89
90unsigned int install_e820_map(unsigned int max_entries,
91 struct e820_entry *entries)
92{
93 unsigned int num_entries = 0;
94 const struct hob_header *hdr;
95 struct hob_res_desc *res_desc;
Simon Glass154dc3e2020-09-22 12:45:40 -060096 const fdt64_t *prop;
97 int size;
Simon Glassddb39b22019-08-24 14:10:32 -060098
99 hdr = gd->arch.hob_list;
100
101 while (!end_of_hob(hdr)) {
102 if (hdr->type == HOB_TYPE_RES_DESC) {
103 res_desc = (struct hob_res_desc *)hdr;
104 entries[num_entries].addr = res_desc->phys_start;
105 entries[num_entries].size = res_desc->len;
106
107 if (res_desc->type == RES_SYS_MEM)
108 entries[num_entries].type = E820_RAM;
109 else if (res_desc->type == RES_MEM_RESERVED)
110 entries[num_entries].type = E820_RESERVED;
111
112 num_entries++;
113 }
114 hdr = get_next_hob(hdr);
115 }
116
117 /* Mark PCIe ECAM address range as reserved */
118 entries[num_entries].addr = CONFIG_PCIE_ECAM_BASE;
119 entries[num_entries].size = CONFIG_PCIE_ECAM_SIZE;
120 entries[num_entries].type = E820_RESERVED;
121 num_entries++;
122
Simon Glasse6ad2022020-07-09 18:43:16 -0600123 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
124 ulong stack_size;
125
126 stack_size = CONFIG_IS_ENABLED(HAVE_ACPI_RESUME,
Heinrich Schuchardt99186b32020-07-29 12:31:17 +0200127 (CONFIG_STACK_SIZE_RESUME), (0));
Simon Glasse6ad2022020-07-09 18:43:16 -0600128 /*
129 * Everything between U-Boot's stack and ram top needs to be
130 * reserved in order for ACPI S3 resume to work.
131 */
132 entries[num_entries].addr = gd->start_addr_sp - stack_size;
133 entries[num_entries].size = gd->ram_top - gd->start_addr_sp +
134 stack_size;
135 entries[num_entries].type = E820_RESERVED;
136 num_entries++;
137 }
Simon Glassddb39b22019-08-24 14:10:32 -0600138
Simon Glass154dc3e2020-09-22 12:45:40 -0600139 prop = ofnode_read_chosen_prop("e820-entries", &size);
140 if (prop) {
141 int count = size / (sizeof(u64) * 3);
142 int i;
143
144 if (num_entries + count >= max_entries)
145 return -ENOSPC;
146 for (i = 0; i < count; i++, num_entries++, prop += 3) {
147 entries[num_entries].addr = fdt64_to_cpu(prop[0]);
148 entries[num_entries].size = fdt64_to_cpu(prop[1]);
149 entries[num_entries].type = fdt64_to_cpu(prop[2]);
150 }
151 }
152
Simon Glassddb39b22019-08-24 14:10:32 -0600153 return num_entries;
154}
Simon Glass25628082019-09-25 08:11:41 -0600155
156#if CONFIG_IS_ENABLED(HANDOFF) && IS_ENABLED(CONFIG_USE_HOB)
157int handoff_arch_save(struct spl_handoff *ho)
158{
159 ho->arch.usable_ram_top = fsp_get_usable_lowmem_top(gd->arch.hob_list);
160 ho->arch.hob_list = gd->arch.hob_list;
161
162 return 0;
163}
164#endif