Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 1 | // 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 Glass | 67c4e9f | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 10 | #include <asm/fsp/fsp_support.h> |
| 11 | #include <asm/e820.h> |
| 12 | #include <asm/mrccache.h> |
Simon Glass | 4279110 | 2019-12-06 21:42:12 -0700 | [diff] [blame] | 13 | #include <asm/mtrr.h> |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 14 | #include <asm/post.h> |
Simon Glass | 70c202c | 2020-09-22 12:45:40 -0600 | [diff] [blame] | 15 | #include <dm/ofnode.h> |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | int 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 | |
| 42 | int dram_init_banksize(void) |
| 43 | { |
Simon Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 44 | efi_guid_t fsp = FSP_HOB_RESOURCE_OWNER_FSP_GUID; |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 45 | const struct hob_header *hdr; |
| 46 | struct hob_res_desc *res_desc; |
Simon Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 47 | phys_addr_t mtrr_top; |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 48 | phys_addr_t low_end; |
| 49 | uint bank; |
| 50 | |
Simon Glass | c793dbd | 2020-04-26 09:12:53 -0600 | [diff] [blame] | 51 | 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 Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 59 | low_end = 0; /* top of low memory usable by U-Boot */ |
| 60 | mtrr_top = 0; /* top of low memory (even if reserved) */ |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 61 | 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 Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 67 | if (!guidcmp(&res_desc->owner, &fsp)) |
| 68 | low_end = res_desc->phys_start; |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 69 | 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 Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 73 | 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 Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 83 | } |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Add the memory below 4GB */ |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 87 | gd->bd->bi_dram[0].start = 0; |
Simon Glass | ea4e97a | 2019-12-06 21:42:11 -0700 | [diff] [blame] | 88 | gd->bd->bi_dram[0].size = low_end; |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 89 | |
Simon Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 90 | /* |
| 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 Glass | 4279110 | 2019-12-06 21:42:12 -0700 | [diff] [blame] | 95 | |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | unsigned 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 Glass | 70c202c | 2020-09-22 12:45:40 -0600 | [diff] [blame] | 105 | const fdt64_t *prop; |
| 106 | int size; |
Simon Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 107 | |
| 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 Glass | ef5f5f6 | 2020-07-09 18:43:16 -0600 | [diff] [blame] | 132 | if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) { |
| 133 | ulong stack_size; |
| 134 | |
| 135 | stack_size = CONFIG_IS_ENABLED(HAVE_ACPI_RESUME, |
Heinrich Schuchardt | 4f0c4be | 2020-07-29 12:31:17 +0200 | [diff] [blame] | 136 | (CONFIG_STACK_SIZE_RESUME), (0)); |
Simon Glass | ef5f5f6 | 2020-07-09 18:43:16 -0600 | [diff] [blame] | 137 | /* |
| 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 Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 147 | |
Simon Glass | 70c202c | 2020-09-22 12:45:40 -0600 | [diff] [blame] | 148 | 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 Glass | e9de4a7 | 2019-08-24 14:10:32 -0600 | [diff] [blame] | 162 | return num_entries; |
| 163 | } |
Simon Glass | c3863ea | 2019-09-25 08:11:41 -0600 | [diff] [blame] | 164 | |
| 165 | #if CONFIG_IS_ENABLED(HANDOFF) && IS_ENABLED(CONFIG_USE_HOB) |
| 166 | int handoff_arch_save(struct spl_handoff *ho) |
| 167 | { |
Simon Glass | d46c093 | 2020-11-04 09:57:43 -0700 | [diff] [blame] | 168 | ho->arch.usable_ram_top = gd->bd->bi_dram[0].size; |
Simon Glass | c3863ea | 2019-09-25 08:11:41 -0600 | [diff] [blame] | 169 | ho->arch.hob_list = gd->arch.hob_list; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | #endif |