blob: f1bd7cc1278b8d38861bbded823e21e504fe8499 [file] [log] [blame]
Aaron Durbin49048022014-02-18 21:55:02 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003-2004 Eric Biederman
5 * Copyright (C) 2005-2010 coresystems GmbH
6 * Copyright (C) 2014 Google Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <console/console.h>
23#include <bootmem.h>
24#include <cbmem.h>
25#include <device/resource.h>
26#include <stdlib.h>
27
28static struct memranges bootmem;
29
30void bootmem_init(void)
31{
32 const unsigned long cacheable = IORESOURCE_CACHEABLE;
33 const unsigned long reserved = IORESOURCE_RESERVE;
34 struct memranges *bm = &bootmem;
35
36 /*
37 * Fill the memory map out. The order of operations is important in
38 * that each overlapping range will take over the next. Therefore,
39 * add cacheable resources as RAM then add the reserved resources.
40 */
41 memranges_init(bm, cacheable, cacheable, LB_MEM_RAM);
42 memranges_add_resources(bm, reserved, reserved, LB_MEM_RESERVED);
43
44 /* Add memory used by CBMEM. */
45 cbmem_add_bootmem();
46}
47
48void bootmem_add_range(uint64_t start, uint64_t size, uint32_t type)
49{
50 memranges_insert(&bootmem, start, size, type);
51}
52
53void bootmem_write_memory_table(struct lb_memory *mem)
54{
55 const struct range_entry *r;
56 struct lb_memory_range *lb_r;
57
58 lb_r = &mem->map[0];
59
60 bootmem_dump_ranges();
61
62 memranges_each_entry(r, &bootmem) {
63 lb_r->start = pack_lb64(range_entry_base(r));
64 lb_r->size = pack_lb64(range_entry_size(r));
65 lb_r->type = range_entry_tag(r);
66
67 lb_r++;
68 mem->size += sizeof(struct lb_memory_range);
69 }
70}
71
72struct range_strings {
73 unsigned long tag;
74 const char *str;
75};
76
77static const struct range_strings type_strings[] = {
78 { LB_MEM_RAM, "RAM" },
79 { LB_MEM_RESERVED, "RESERVED" },
80 { LB_MEM_ACPI, "ACPI" },
81 { LB_MEM_NVS, "NVS" },
82 { LB_MEM_UNUSABLE, "UNUSABLE" },
83 { LB_MEM_VENDOR_RSVD, "VENDOR RESERVED" },
84 { LB_MEM_TABLE, "CONFIGURATION TABLES" },
85};
86
87static const char *bootmem_range_string(unsigned long tag)
88{
89 int i;
90
91 for (i = 0; i < ARRAY_SIZE(type_strings); i++) {
92 if (type_strings[i].tag == tag)
93 return type_strings[i].str;
94 }
95
96 return "UNKNOWN!";
97}
98
99void bootmem_dump_ranges(void)
100{
101 int i;
102 const struct range_entry *r;
103
104 i = 0;
105 memranges_each_entry(r, &bootmem) {
106 printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n",
107 i, range_entry_base(r), range_entry_end(r) - 1,
108 bootmem_range_string(range_entry_tag(r)));
109 i++;
110 }
111}
112
113int bootmem_region_targets_usable_ram(uint64_t start, uint64_t size)
114{
115 const struct range_entry *r;
116 uint64_t end = start + size;
117
118 memranges_each_entry(r, &bootmem) {
119 /* All further bootmem entries are beyond this range. */
120 if (end <= range_entry_base(r))
121 break;
122
123 if (start >= range_entry_base(r) && end <= range_entry_end(r)) {
124 if (range_entry_tag(r) == LB_MEM_RAM)
125 return 1;
126 }
127 }
128 return 0;
129}
130
131void *bootmem_allocate_buffer(size_t size)
132{
133 const struct range_entry *r;
134 const struct range_entry *region;
135 /* All allocated buffers fall below the 32-bit boundary. */
136 const resource_t max_addr = 1ULL << 32;
137 resource_t begin;
138 resource_t end;
139
140 /* 4KiB alignment. */
141 size = ALIGN(size, 4096);
142 region = NULL;
143 memranges_each_entry(r, &bootmem) {
144 if (range_entry_size(r) < size)
145 continue;
146
147 if (range_entry_tag(r) != LB_MEM_RAM)
148 continue;
149
150 if (range_entry_base(r) >= max_addr)
151 continue;
152
153 end = range_entry_end(r);
154 if (end > max_addr)
155 end = max_addr;
156
157 if ((end - range_entry_base(r)) < size)
158 continue;
159
160 region = r;
161 }
162
163 if (region == NULL)
164 return NULL;
165
166 /* region now points to the highest usable region for the given size. */
167 begin = range_entry_base(region);
168 end = range_entry_end(region);
169 if (end > max_addr)
170 end = max_addr;
171 begin = end - size;
172
173 /* Mark buffer as unusuable for future buffer use. */
174 bootmem_add_range(begin, size, LB_MEM_UNUSABLE);
175
176 return (void *)(uintptr_t)begin;
177}