blob: 3ffd40ce7478fd527e844b802cec5722e4e0cae0 [file] [log] [blame]
Simon Glasse9de4a72019-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 Glass67c4e9f2019-11-14 12:57:45 -07008#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glasse9de4a72019-08-24 14:10:32 -060010#include <asm/fsp/fsp_support.h>
11#include <asm/e820.h>
12#include <asm/mrccache.h>
Simon Glass42791102019-12-06 21:42:12 -070013#include <asm/mtrr.h>
Simon Glasse9de4a72019-08-24 14:10:32 -060014#include <asm/post.h>
Simon Glass70c202c2020-09-22 12:45:40 -060015#include <dm/ofnode.h>
Simon Glasse9de4a72019-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 Glassd46c0932020-11-04 09:57:43 -070044 efi_guid_t fsp = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
Simon Glassea4e97a2019-12-06 21:42:11 -070045 const struct hob_header *hdr;
46 struct hob_res_desc *res_desc;
Simon Glassd46c0932020-11-04 09:57:43 -070047 phys_addr_t mtrr_top;
Simon Glassea4e97a2019-12-06 21:42:11 -070048 phys_addr_t low_end;
49 uint bank;
50
Simon Glassc793dbd2020-04-26 09:12:53 -060051 if (!ll_boot_init()) {
52 gd->bd->bi_dram[0].start = 0;
53 gd->bd->bi_dram[0].size = gd->ram_size;
54
55 mtrr_add_request(MTRR_TYPE_WRBACK, 0, gd->ram_size);
56 return 0;
57 }
58
Simon Glassd46c0932020-11-04 09:57:43 -070059 low_end = 0; /* top of low memory usable by U-Boot */
60 mtrr_top = 0; /* top of low memory (even if reserved) */
Simon Glassea4e97a2019-12-06 21:42:11 -070061 for (bank = 1, hdr = gd->arch.hob_list;
62 bank < CONFIG_NR_DRAM_BANKS && !end_of_hob(hdr);
63 hdr = get_next_hob(hdr)) {
64 if (hdr->type != HOB_TYPE_RES_DESC)
65 continue;
66 res_desc = (struct hob_res_desc *)hdr;
Simon Glassd46c0932020-11-04 09:57:43 -070067 if (!guidcmp(&res_desc->owner, &fsp))
68 low_end = res_desc->phys_start;
Simon Glassea4e97a2019-12-06 21:42:11 -070069 if (res_desc->type != RES_SYS_MEM &&
70 res_desc->type != RES_MEM_RESERVED)
71 continue;
72 if (res_desc->phys_start < (1ULL << 32)) {
Simon Glassd46c0932020-11-04 09:57:43 -070073 mtrr_top = max(mtrr_top,
74 res_desc->phys_start + res_desc->len);
75 } else {
76 gd->bd->bi_dram[bank].start = res_desc->phys_start;
77 gd->bd->bi_dram[bank].size = res_desc->len;
78 mtrr_add_request(MTRR_TYPE_WRBACK, res_desc->phys_start,
79 res_desc->len);
80 log_debug("ram %llx %llx\n",
81 gd->bd->bi_dram[bank].start,
82 gd->bd->bi_dram[bank].size);
Simon Glassea4e97a2019-12-06 21:42:11 -070083 }
Simon Glassea4e97a2019-12-06 21:42:11 -070084 }
85
86 /* Add the memory below 4GB */
Simon Glasse9de4a72019-08-24 14:10:32 -060087 gd->bd->bi_dram[0].start = 0;
Simon Glassea4e97a2019-12-06 21:42:11 -070088 gd->bd->bi_dram[0].size = low_end;
Simon Glasse9de4a72019-08-24 14:10:32 -060089
Simon Glassd46c0932020-11-04 09:57:43 -070090 /*
91 * Set up an MTRR to the top of low, reserved memory. This is necessary
92 * for graphics to run at full speed in U-Boot.
93 */
94 mtrr_add_request(MTRR_TYPE_WRBACK, 0, mtrr_top);
Simon Glass42791102019-12-06 21:42:12 -070095
Simon Glasse9de4a72019-08-24 14:10:32 -060096 return 0;
97}
98
99unsigned int install_e820_map(unsigned int max_entries,
100 struct e820_entry *entries)
101{
102 unsigned int num_entries = 0;
103 const struct hob_header *hdr;
104 struct hob_res_desc *res_desc;
Simon Glass70c202c2020-09-22 12:45:40 -0600105 const fdt64_t *prop;
106 int size;
Simon Glasse9de4a72019-08-24 14:10:32 -0600107
108 hdr = gd->arch.hob_list;
109
110 while (!end_of_hob(hdr)) {
111 if (hdr->type == HOB_TYPE_RES_DESC) {
112 res_desc = (struct hob_res_desc *)hdr;
113 entries[num_entries].addr = res_desc->phys_start;
114 entries[num_entries].size = res_desc->len;
115
116 if (res_desc->type == RES_SYS_MEM)
117 entries[num_entries].type = E820_RAM;
118 else if (res_desc->type == RES_MEM_RESERVED)
119 entries[num_entries].type = E820_RESERVED;
120
121 num_entries++;
122 }
123 hdr = get_next_hob(hdr);
124 }
125
126 /* Mark PCIe ECAM address range as reserved */
127 entries[num_entries].addr = CONFIG_PCIE_ECAM_BASE;
128 entries[num_entries].size = CONFIG_PCIE_ECAM_SIZE;
129 entries[num_entries].type = E820_RESERVED;
130 num_entries++;
131
Simon Glassef5f5f62020-07-09 18:43:16 -0600132 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
133 ulong stack_size;
134
135 stack_size = CONFIG_IS_ENABLED(HAVE_ACPI_RESUME,
Heinrich Schuchardt4f0c4be2020-07-29 12:31:17 +0200136 (CONFIG_STACK_SIZE_RESUME), (0));
Simon Glassef5f5f62020-07-09 18:43:16 -0600137 /*
138 * Everything between U-Boot's stack and ram top needs to be
139 * reserved in order for ACPI S3 resume to work.
140 */
141 entries[num_entries].addr = gd->start_addr_sp - stack_size;
142 entries[num_entries].size = gd->ram_top - gd->start_addr_sp +
143 stack_size;
144 entries[num_entries].type = E820_RESERVED;
145 num_entries++;
146 }
Simon Glasse9de4a72019-08-24 14:10:32 -0600147
Simon Glass70c202c2020-09-22 12:45:40 -0600148 prop = ofnode_read_chosen_prop("e820-entries", &size);
149 if (prop) {
150 int count = size / (sizeof(u64) * 3);
151 int i;
152
153 if (num_entries + count >= max_entries)
154 return -ENOSPC;
155 for (i = 0; i < count; i++, num_entries++, prop += 3) {
156 entries[num_entries].addr = fdt64_to_cpu(prop[0]);
157 entries[num_entries].size = fdt64_to_cpu(prop[1]);
158 entries[num_entries].type = fdt64_to_cpu(prop[2]);
159 }
160 }
161
Simon Glasse9de4a72019-08-24 14:10:32 -0600162 return num_entries;
163}
Simon Glassc3863ea2019-09-25 08:11:41 -0600164
165#if CONFIG_IS_ENABLED(HANDOFF) && IS_ENABLED(CONFIG_USE_HOB)
166int handoff_arch_save(struct spl_handoff *ho)
167{
Simon Glassd46c0932020-11-04 09:57:43 -0700168 ho->arch.usable_ram_top = gd->bd->bi_dram[0].size;
Simon Glassc3863ea2019-09-25 08:11:41 -0600169 ho->arch.hob_list = gd->arch.hob_list;
170
171 return 0;
172}
173#endif