1 /* 2 * Copyright (c) 2011 The Chromium OS Authors. 3 * (C) Copyright 2010,2011 4 * Graeme Russ, <graeme.russ@gmail.com> 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 #include <common.h> 10 #include <malloc.h> 11 #include <asm/e820.h> 12 #include <asm/u-boot-x86.h> 13 #include <asm/global_data.h> 14 #include <asm/init_helpers.h> 15 #include <asm/processor.h> 16 #include <asm/sections.h> 17 #include <asm/zimage.h> 18 #include <asm/arch/sysinfo.h> 19 #include <asm/arch/tables.h> 20 21 DECLARE_GLOBAL_DATA_PTR; 22 23 unsigned install_e820_map(unsigned max_entries, struct e820entry *entries) 24 { 25 unsigned num_entries; 26 int i; 27 28 num_entries = min((unsigned)lib_sysinfo.n_memranges, max_entries); 29 if (num_entries < lib_sysinfo.n_memranges) { 30 printf("Warning: Limiting e820 map to %d entries.\n", 31 num_entries); 32 } 33 for (i = 0; i < num_entries; i++) { 34 struct memrange *memrange = &lib_sysinfo.memrange[i]; 35 36 entries[i].addr = memrange->base; 37 entries[i].size = memrange->size; 38 39 /* 40 * coreboot has some extensions (type 6 & 16) to the E820 types. 41 * When we detect this, mark it as E820_RESERVED. 42 */ 43 if (memrange->type == CB_MEM_VENDOR_RSVD || 44 memrange->type == CB_MEM_TABLE) 45 entries[i].type = E820_RESERVED; 46 else 47 entries[i].type = memrange->type; 48 } 49 50 return num_entries; 51 } 52 53 /* 54 * This function looks for the highest region of memory lower than 4GB which 55 * has enough space for U-Boot where U-Boot is aligned on a page boundary. It 56 * overrides the default implementation found elsewhere which simply picks the 57 * end of ram, wherever that may be. The location of the stack, the relocation 58 * address, and how far U-Boot is moved by relocation are set in the global 59 * data structure. 60 */ 61 ulong board_get_usable_ram_top(ulong total_size) 62 { 63 uintptr_t dest_addr = 0; 64 int i; 65 66 for (i = 0; i < lib_sysinfo.n_memranges; i++) { 67 struct memrange *memrange = &lib_sysinfo.memrange[i]; 68 /* Force U-Boot to relocate to a page aligned address. */ 69 uint64_t start = roundup(memrange->base, 1 << 12); 70 uint64_t end = memrange->base + memrange->size; 71 72 /* Ignore non-memory regions. */ 73 if (memrange->type != CB_MEM_RAM) 74 continue; 75 76 /* Filter memory over 4GB. */ 77 if (end > 0xffffffffULL) 78 end = 0x100000000ULL; 79 /* Skip this region if it's too small. */ 80 if (end - start < total_size) 81 continue; 82 83 /* Use this address if it's the largest so far. */ 84 if (end > dest_addr) 85 dest_addr = end; 86 } 87 88 /* If no suitable area was found, return an error. */ 89 if (!dest_addr) 90 panic("No available memory found for relocation"); 91 92 return (ulong)dest_addr; 93 } 94 95 int dram_init(void) 96 { 97 int i; 98 phys_size_t ram_size = 0; 99 100 for (i = 0; i < lib_sysinfo.n_memranges; i++) { 101 struct memrange *memrange = &lib_sysinfo.memrange[i]; 102 unsigned long long end = memrange->base + memrange->size; 103 104 if (memrange->type == CB_MEM_RAM && end > ram_size && 105 memrange->base < (1ULL << 32)) 106 ram_size = end; 107 } 108 gd->ram_size = ram_size; 109 if (ram_size == 0) 110 return -1; 111 112 return calculate_relocation_address(); 113 } 114 115 void dram_init_banksize(void) 116 { 117 int i, j; 118 119 if (CONFIG_NR_DRAM_BANKS) { 120 for (i = 0, j = 0; i < lib_sysinfo.n_memranges; i++) { 121 struct memrange *memrange = &lib_sysinfo.memrange[i]; 122 123 if (memrange->type == CB_MEM_RAM && 124 memrange->base < (1ULL << 32)) { 125 gd->bd->bi_dram[j].start = memrange->base; 126 gd->bd->bi_dram[j].size = memrange->size; 127 j++; 128 if (j >= CONFIG_NR_DRAM_BANKS) 129 break; 130 } 131 } 132 } 133 } 134