blob: 594b5e55a9b667e14bef77a8f70aeb70cdae5a49 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00002#include <ip_checksum.h>
3#include <boot/linuxbios_tables.h>
4#include "linuxbios_table.h"
5#include <string.h>
6#include <version.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +00007#include <device/device.h>
8#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009
Eric Biederman8ca8d762003-04-22 19:02:15 +000010struct lb_header *lb_table_init(unsigned long addr)
11{
12 struct lb_header *header;
13
14 /* 16 byte align the address */
15 addr += 15;
16 addr &= ~15;
17
18 header = (void *)addr;
19 header->signature[0] = 'L';
20 header->signature[1] = 'B';
21 header->signature[2] = 'I';
22 header->signature[3] = 'O';
23 header->header_bytes = sizeof(*header);
24 header->header_checksum = 0;
25 header->table_bytes = 0;
26 header->table_checksum = 0;
27 header->table_entries = 0;
28 return header;
29}
30
31struct lb_record *lb_first_record(struct lb_header *header)
32{
33 struct lb_record *rec;
34 rec = (void *)(((char *)header) + sizeof(*header));
35 return rec;
36}
37
38struct lb_record *lb_last_record(struct lb_header *header)
39{
40 struct lb_record *rec;
41 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
42 return rec;
43}
44
45struct lb_record *lb_next_record(struct lb_record *rec)
46{
47 rec = (void *)(((char *)rec) + rec->size);
48 return rec;
49}
50
51struct lb_record *lb_new_record(struct lb_header *header)
52{
53 struct lb_record *rec;
54 rec = lb_last_record(header);
55 if (header->table_entries) {
56 header->table_bytes += rec->size;
57 }
58 rec = lb_last_record(header);
59 header->table_entries++;
60 rec->tag = LB_TAG_UNUSED;
61 rec->size = sizeof(*rec);
62 return rec;
63}
64
65
66struct lb_memory *lb_memory(struct lb_header *header)
67{
68 struct lb_record *rec;
69 struct lb_memory *mem;
70 rec = lb_new_record(header);
71 mem = (struct lb_memory *)rec;
72 mem->tag = LB_TAG_MEMORY;
73 mem->size = sizeof(*mem);
74 return mem;
75}
76
77struct lb_mainboard *lb_mainboard(struct lb_header *header)
78{
79 struct lb_record *rec;
80 struct lb_mainboard *mainboard;
81 rec = lb_new_record(header);
82 mainboard = (struct lb_mainboard *)rec;
83 mainboard->tag = LB_TAG_MAINBOARD;
84
85 mainboard->size = (sizeof(*mainboard) +
86 strlen(mainboard_vendor) + 1 +
87 strlen(mainboard_part_number) + 1 +
88 3) & ~3;
89
90 mainboard->vendor_idx = 0;
91 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
92
93 memcpy(mainboard->strings + mainboard->vendor_idx,
94 mainboard_vendor, strlen(mainboard_vendor) + 1);
95 memcpy(mainboard->strings + mainboard->part_number_idx,
96 mainboard_part_number, strlen(mainboard_part_number) + 1);
97
98 return mainboard;
99}
100
101void lb_strings(struct lb_header *header)
102{
103 static const struct {
104 uint32_t tag;
105 const uint8_t *string;
106 } strings[] = {
107 { LB_TAG_VERSION, linuxbios_version, },
108 { LB_TAG_EXTRA_VERSION, linuxbios_extra_version, },
109 { LB_TAG_BUILD, linuxbios_build, },
110 { LB_TAG_COMPILE_TIME, linuxbios_compile_time, },
111 { LB_TAG_COMPILE_BY, linuxbios_compile_by, },
112 { LB_TAG_COMPILE_HOST, linuxbios_compile_host, },
113 { LB_TAG_COMPILE_DOMAIN, linuxbios_compile_domain, },
114 { LB_TAG_COMPILER, linuxbios_compiler, },
115 { LB_TAG_LINKER, linuxbios_linker, },
116 { LB_TAG_ASSEMBLER, linuxbios_assembler, },
117 };
Eric Biederman52685572003-05-19 19:16:21 +0000118 unsigned int i;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
120 struct lb_string *rec;
121 size_t len;
122 rec = (struct lb_string *)lb_new_record(header);
123 len = strlen(strings[i].string);
124 rec->tag = strings[i].tag;
125 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
126 memcpy(rec->string, strings[i].string, len+1);
127 }
128
129}
130
Eric Biederman8ca8d762003-04-22 19:02:15 +0000131void lb_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000132 uint32_t type, uint64_t start, uint64_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000133{
134 int entries;
135 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
136 mem->map[entries].start = start;
137 mem->map[entries].size = size;
138 mem->map[entries].type = type;
139 mem->size += sizeof(mem->map[0]);
140}
141
Eric Biederman8ca8d762003-04-22 19:02:15 +0000142static void lb_reserve_table_memory(struct lb_header *head)
143{
144 struct lb_record *last_rec;
145 struct lb_memory *mem;
146 uint64_t start;
147 uint64_t end;
148 int i, entries;
149
150 last_rec = lb_last_record(head);
151 mem = get_lb_mem();
152 start = (unsigned long)head;
153 end = (unsigned long)last_rec;
154 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
155 /* Resize the right two memory areas so this table is in
156 * a reserved area of memory. Everything has been carefully
157 * setup so that is all we need to do.
158 */
159 for(i = 0; i < entries; i++ ) {
160 uint64_t map_start = mem->map[i].start;
161 uint64_t map_end = map_start + mem->map[i].size;
162 /* Does this area need to be expanded? */
163 if (map_end == start) {
164 mem->map[i].size = end - map_start;
165 }
166 /* Does this area need to be contracted? */
167 else if (map_start == start) {
168 mem->map[i].start = end;
169 mem->map[i].size = map_end - end;
170 }
171 }
172}
173
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174unsigned long lb_table_fini(struct lb_header *head)
175{
176 struct lb_record *rec, *first_rec;
177 rec = lb_last_record(head);
178 if (head->table_entries) {
179 head->table_bytes += rec->size;
180 }
181 lb_reserve_table_memory(head);
182 first_rec = lb_first_record(head);
183 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
184 head->header_checksum = 0;
185 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
186 printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
187 head, rec, head->table_checksum);
188 return (unsigned long)rec;
189}
190
Eric Biederman6e53f502004-10-27 08:53:57 +0000191static void lb_cleanup_memory_ranges(struct lb_memory *mem)
192{
193 int entries;
194 int i, j;
195 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
196
197 /* Sort the lb memory ranges */
198 for(i = 0; i < entries; i++) {
199 for(j = i; j < entries; j++) {
200 if (mem->map[j].start < mem->map[i].start) {
201 struct lb_memory_range tmp;
202 tmp = mem->map[i];
203 mem->map[i] = mem->map[j];
204 mem->map[j] = tmp;
205 }
206 }
207 }
208
209 /* Merge adjacent entries */
210 for(i = 0; (i + 1) < entries; i++) {
211 uint64_t start, end, nstart, nend;
212 if (mem->map[i].type != mem->map[i + 1].type) {
213 continue;
214 }
215 start = mem->map[i].start;
216 end = start + mem->map[i].size;
217 nstart = mem->map[i + 1].start;
218 nend = nstart + mem->map[i + 1].size;
219 if ((start <= nstart) && (end > nstart)) {
220 if (start > nstart) {
221 start = nstart;
222 }
223 if (end < nend) {
224 end = nend;
225 }
226 /* Record the new region size */
227 mem->map[i].start = start;
228 mem->map[i].size = end - start;
229
230 /* Delete the entry I have merged with */
231 memmove(&mem->map[i + 1], &mem->map[i + 2],
232 ((entries - i - 2) * sizeof(mem->map[0])));
233 mem->size -= sizeof(mem->map[0]);
234 entries -= 1;
235 /* See if I can merge with the next entry as well */
236 i -= 1;
237 }
238 }
239}
240
241static void lb_remove_memory_range(struct lb_memory *mem,
242 uint64_t start, uint64_t size)
243{
244 uint64_t end;
245 int entries;
246 int i;
247
248 end = start + size;
249 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
250
251 /* Remove a reserved area from the memory map */
252 for(i = 0; i < entries; i++) {
253 uint64_t map_start = mem->map[i].start;
254 uint64_t map_end = map_start + mem->map[i].size;
255 if ((start <= map_start) && (end >= map_end)) {
256 /* Remove the completely covered range */
257 memmove(&mem->map[i], &mem->map[i + 1],
258 ((entries - i - 1) * sizeof(mem->map[0])));
259 mem->size -= sizeof(mem->map[0]);
260 entries -= 1;
261 /* Since the index will disappear revisit what will appear here */
262 i -= 1;
263 }
264 else if ((start > map_start) && (end < map_end)) {
265 /* Split the memory range */
266 memmove(&mem->map[i + 1], &mem->map[i],
267 ((entries - i) * sizeof(mem->map[0])));
268 mem->size += sizeof(mem->map[0]);
269 entries += 1;
270 /* Update the first map entry */
271 mem->map[i].size = start - map_start;
272 /* Update the second map entry */
273 mem->map[i + 1].start = end;
274 mem->map[i + 1].size = map_end - end;
275 /* Don't bother with this map entry again */
276 i += 1;
277 }
278 else if ((start <= map_start) && (end > map_start)) {
279 /* Shrink the start of the memory range */
280 mem->map[i].start = end;
281 mem->map[i].size = map_end - end;
282 }
283 else if ((start < map_end) && (start > map_start)) {
284 /* Shrink the end of the memory range */
285 mem->map[i].size = start - map_start;
286 }
287 }
288}
289
290static void lb_add_memory_range(struct lb_memory *mem,
291 uint32_t type, uint64_t start, uint64_t size)
292{
293 lb_remove_memory_range(mem, start, size);
294 lb_memory_range(mem, type, start, size);
295 lb_cleanup_memory_ranges(mem);
296}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000297
298/* Routines to extract part so the linuxBIOS table or
299 * information from the linuxBIOS table after we have written it.
300 * Currently get_lb_mem relies on a global we can change the
301 * implementaiton.
302 */
303static struct lb_memory *mem_ranges = 0;
304struct lb_memory *get_lb_mem(void)
305{
306 return mem_ranges;
307}
308
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000309static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
310{
311 struct lb_memory *mem = gp;
312 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
313}
314
Eric Biederman6e53f502004-10-27 08:53:57 +0000315static struct lb_memory *build_lb_mem(struct lb_header *head)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000316{
Eric Biederman6e53f502004-10-27 08:53:57 +0000317 struct lb_memory *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000318 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000319
Eric Biederman6e53f502004-10-27 08:53:57 +0000320 /* Record where the lb memory ranges will live */
321 mem = lb_memory(head);
322 mem_ranges = mem;
323
324 /* Build the raw table of memory */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000325 search_global_resources(
326 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
327 build_lb_mem_range, mem);
Eric Biederman6e53f502004-10-27 08:53:57 +0000328 lb_cleanup_memory_ranges(mem);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000329 return mem;
330}
331
Eric Biederman8ca8d762003-04-22 19:02:15 +0000332unsigned long write_linuxbios_table(
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333 unsigned long low_table_start, unsigned long low_table_end,
334 unsigned long rom_table_startk, unsigned long rom_table_endk)
335{
336 unsigned long table_size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000337 struct lb_header *head;
338 struct lb_memory *mem;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000339
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340 head = lb_table_init(low_table_end);
341 low_table_end = (unsigned long)head;
Eric Biederman6e53f502004-10-27 08:53:57 +0000342 if (HAVE_OPTION_TABLE == 1) {
343 struct lb_record *rec_dest, *rec_src;
344 /* Write the option config table... */
345 rec_dest = lb_new_record(head);
346 rec_src = (struct lb_record *)&option_table;
347 memcpy(rec_dest, rec_src, rec_src->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000348 }
Eric Biederman6e53f502004-10-27 08:53:57 +0000349 /* Record where RAM is located */
350 mem = build_lb_mem(head);
351
352 /* Find the current mptable size */
353 table_size = (low_table_end - low_table_start);
354
355 /* Record the mptable and the the lb_table (This will be adjusted later) */
356 lb_add_memory_range(mem, LB_MEM_TABLE,
357 low_table_start, table_size);
358
359 /* Record the pirq table */
360 lb_add_memory_range(mem, LB_MEM_TABLE,
361 rom_table_startk << 10, (rom_table_endk - rom_table_startk) << 10);
362
363 /* Note:
364 * I assume that there is always memory at immediately after
365 * the low_table_end. This means that after I setup the linuxbios table.
366 * I can trivially fixup the reserved memory ranges to hold the correct
367 * size of the linuxbios table.
368 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000369
370 /* Record our motheboard */
371 lb_mainboard(head);
372 /* Record our various random string information */
373 lb_strings(head);
374
375 low_table_end = lb_table_fini(head);
376
377 /* Remember where my valid memory ranges are */
378 return low_table_end;
379}