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