blob: 414addff49cb2c4469b4b8403ad49b7b0f8b165f [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 Biedermanec01aa92004-12-10 20:50:43 +000010static inline uint64_t unpack_lb64(struct lb_uint64 value)
11{
12 uint64_t result;
13 result = value.hi;
14 result = (result << 32) + value.lo;
15 return result;
16}
17
18static inline struct lb_uint64 pack_lb64(uint64_t value)
19{
20 struct lb_uint64 result;
21 result.lo = (value >> 0) & 0xffffffff;
22 result.hi = (value >> 32) & 0xffffffff;
23 return result;
24}
25
Eric Biederman8ca8d762003-04-22 19:02:15 +000026struct lb_header *lb_table_init(unsigned long addr)
27{
28 struct lb_header *header;
29
30 /* 16 byte align the address */
31 addr += 15;
32 addr &= ~15;
33
34 header = (void *)addr;
35 header->signature[0] = 'L';
36 header->signature[1] = 'B';
37 header->signature[2] = 'I';
38 header->signature[3] = 'O';
39 header->header_bytes = sizeof(*header);
40 header->header_checksum = 0;
41 header->table_bytes = 0;
42 header->table_checksum = 0;
43 header->table_entries = 0;
44 return header;
45}
46
47struct lb_record *lb_first_record(struct lb_header *header)
48{
49 struct lb_record *rec;
50 rec = (void *)(((char *)header) + sizeof(*header));
51 return rec;
52}
53
54struct lb_record *lb_last_record(struct lb_header *header)
55{
56 struct lb_record *rec;
57 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
58 return rec;
59}
60
61struct lb_record *lb_next_record(struct lb_record *rec)
62{
63 rec = (void *)(((char *)rec) + rec->size);
64 return rec;
65}
66
67struct lb_record *lb_new_record(struct lb_header *header)
68{
69 struct lb_record *rec;
70 rec = lb_last_record(header);
71 if (header->table_entries) {
72 header->table_bytes += rec->size;
73 }
74 rec = lb_last_record(header);
75 header->table_entries++;
76 rec->tag = LB_TAG_UNUSED;
77 rec->size = sizeof(*rec);
78 return rec;
79}
80
81
82struct lb_memory *lb_memory(struct lb_header *header)
83{
84 struct lb_record *rec;
85 struct lb_memory *mem;
86 rec = lb_new_record(header);
87 mem = (struct lb_memory *)rec;
88 mem->tag = LB_TAG_MEMORY;
89 mem->size = sizeof(*mem);
90 return mem;
91}
92
93struct lb_mainboard *lb_mainboard(struct lb_header *header)
94{
95 struct lb_record *rec;
96 struct lb_mainboard *mainboard;
97 rec = lb_new_record(header);
98 mainboard = (struct lb_mainboard *)rec;
99 mainboard->tag = LB_TAG_MAINBOARD;
100
101 mainboard->size = (sizeof(*mainboard) +
102 strlen(mainboard_vendor) + 1 +
103 strlen(mainboard_part_number) + 1 +
104 3) & ~3;
105
106 mainboard->vendor_idx = 0;
107 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
108
109 memcpy(mainboard->strings + mainboard->vendor_idx,
110 mainboard_vendor, strlen(mainboard_vendor) + 1);
111 memcpy(mainboard->strings + mainboard->part_number_idx,
112 mainboard_part_number, strlen(mainboard_part_number) + 1);
113
114 return mainboard;
115}
116
117void lb_strings(struct lb_header *header)
118{
119 static const struct {
120 uint32_t tag;
121 const uint8_t *string;
122 } strings[] = {
123 { LB_TAG_VERSION, linuxbios_version, },
124 { LB_TAG_EXTRA_VERSION, linuxbios_extra_version, },
125 { LB_TAG_BUILD, linuxbios_build, },
126 { LB_TAG_COMPILE_TIME, linuxbios_compile_time, },
127 { LB_TAG_COMPILE_BY, linuxbios_compile_by, },
128 { LB_TAG_COMPILE_HOST, linuxbios_compile_host, },
129 { LB_TAG_COMPILE_DOMAIN, linuxbios_compile_domain, },
130 { LB_TAG_COMPILER, linuxbios_compiler, },
131 { LB_TAG_LINKER, linuxbios_linker, },
132 { LB_TAG_ASSEMBLER, linuxbios_assembler, },
133 };
Eric Biederman52685572003-05-19 19:16:21 +0000134 unsigned int i;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
136 struct lb_string *rec;
137 size_t len;
138 rec = (struct lb_string *)lb_new_record(header);
139 len = strlen(strings[i].string);
140 rec->tag = strings[i].tag;
141 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
142 memcpy(rec->string, strings[i].string, len+1);
143 }
144
145}
146
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147void lb_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000148 uint32_t type, uint64_t start, uint64_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149{
150 int entries;
151 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Eric Biedermanec01aa92004-12-10 20:50:43 +0000152 mem->map[entries].start = pack_lb64(start);
153 mem->map[entries].size = pack_lb64(size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154 mem->map[entries].type = type;
155 mem->size += sizeof(mem->map[0]);
156}
157
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158static void lb_reserve_table_memory(struct lb_header *head)
159{
160 struct lb_record *last_rec;
161 struct lb_memory *mem;
162 uint64_t start;
163 uint64_t end;
164 int i, entries;
165
166 last_rec = lb_last_record(head);
167 mem = get_lb_mem();
168 start = (unsigned long)head;
169 end = (unsigned long)last_rec;
170 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
171 /* Resize the right two memory areas so this table is in
172 * a reserved area of memory. Everything has been carefully
173 * setup so that is all we need to do.
174 */
175 for(i = 0; i < entries; i++ ) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000176 uint64_t map_start = unpack_lb64(mem->map[i].start);
177 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178 /* Does this area need to be expanded? */
179 if (map_end == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000180 mem->map[i].size = pack_lb64(end - map_start);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181 }
182 /* Does this area need to be contracted? */
183 else if (map_start == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000184 mem->map[i].start = pack_lb64(end);
185 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186 }
187 }
188}
189
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190unsigned long lb_table_fini(struct lb_header *head)
191{
192 struct lb_record *rec, *first_rec;
193 rec = lb_last_record(head);
194 if (head->table_entries) {
195 head->table_bytes += rec->size;
196 }
197 lb_reserve_table_memory(head);
198 first_rec = lb_first_record(head);
199 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
200 head->header_checksum = 0;
201 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
202 printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
203 head, rec, head->table_checksum);
204 return (unsigned long)rec;
205}
206
Eric Biederman6e53f502004-10-27 08:53:57 +0000207static void lb_cleanup_memory_ranges(struct lb_memory *mem)
208{
209 int entries;
210 int i, j;
211 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
212
213 /* Sort the lb memory ranges */
214 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000215 uint64_t entry_start = unpack_lb64(mem->map[i].start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000216 for(j = i; j < entries; j++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000217 uint64_t temp_start = unpack_lb64(mem->map[j].start);
218 if (temp_start < entry_start) {
Eric Biederman6e53f502004-10-27 08:53:57 +0000219 struct lb_memory_range tmp;
220 tmp = mem->map[i];
221 mem->map[i] = mem->map[j];
222 mem->map[j] = tmp;
223 }
224 }
225 }
226
227 /* Merge adjacent entries */
228 for(i = 0; (i + 1) < entries; i++) {
229 uint64_t start, end, nstart, nend;
230 if (mem->map[i].type != mem->map[i + 1].type) {
231 continue;
232 }
Eric Biedermanec01aa92004-12-10 20:50:43 +0000233 start = unpack_lb64(mem->map[i].start);
234 end = start + unpack_lb64(mem->map[i].size);
235 nstart = unpack_lb64(mem->map[i + 1].start);
236 nend = nstart + unpack_lb64(mem->map[i + 1].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000237 if ((start <= nstart) && (end > nstart)) {
238 if (start > nstart) {
239 start = nstart;
240 }
241 if (end < nend) {
242 end = nend;
243 }
244 /* Record the new region size */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000245 mem->map[i].start = pack_lb64(start);
246 mem->map[i].size = pack_lb64(end - start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000247
248 /* Delete the entry I have merged with */
249 memmove(&mem->map[i + 1], &mem->map[i + 2],
250 ((entries - i - 2) * sizeof(mem->map[0])));
251 mem->size -= sizeof(mem->map[0]);
252 entries -= 1;
253 /* See if I can merge with the next entry as well */
254 i -= 1;
255 }
256 }
257}
258
259static void lb_remove_memory_range(struct lb_memory *mem,
260 uint64_t start, uint64_t size)
261{
262 uint64_t end;
263 int entries;
264 int i;
265
266 end = start + size;
267 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
268
269 /* Remove a reserved area from the memory map */
270 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000271 uint64_t map_start = unpack_lb64(mem->map[i].start);
272 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000273 if ((start <= map_start) && (end >= map_end)) {
274 /* Remove the completely covered range */
275 memmove(&mem->map[i], &mem->map[i + 1],
276 ((entries - i - 1) * sizeof(mem->map[0])));
277 mem->size -= sizeof(mem->map[0]);
278 entries -= 1;
279 /* Since the index will disappear revisit what will appear here */
280 i -= 1;
281 }
282 else if ((start > map_start) && (end < map_end)) {
283 /* Split the memory range */
284 memmove(&mem->map[i + 1], &mem->map[i],
285 ((entries - i) * sizeof(mem->map[0])));
286 mem->size += sizeof(mem->map[0]);
287 entries += 1;
288 /* Update the first map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000289 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000290 /* Update the second map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000291 mem->map[i + 1].start = pack_lb64(end);
292 mem->map[i + 1].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000293 /* Don't bother with this map entry again */
294 i += 1;
295 }
296 else if ((start <= map_start) && (end > map_start)) {
297 /* Shrink the start of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000298 mem->map[i].start = pack_lb64(end);
299 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000300 }
301 else if ((start < map_end) && (start > map_start)) {
302 /* Shrink the end of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000303 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000304 }
305 }
306}
307
308static void lb_add_memory_range(struct lb_memory *mem,
309 uint32_t type, uint64_t start, uint64_t size)
310{
311 lb_remove_memory_range(mem, start, size);
312 lb_memory_range(mem, type, start, size);
313 lb_cleanup_memory_ranges(mem);
314}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000315
316/* Routines to extract part so the linuxBIOS table or
317 * information from the linuxBIOS table after we have written it.
318 * Currently get_lb_mem relies on a global we can change the
319 * implementaiton.
320 */
321static struct lb_memory *mem_ranges = 0;
322struct lb_memory *get_lb_mem(void)
323{
324 return mem_ranges;
325}
326
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000327static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
328{
329 struct lb_memory *mem = gp;
330 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
331}
332
Eric Biederman6e53f502004-10-27 08:53:57 +0000333static struct lb_memory *build_lb_mem(struct lb_header *head)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000334{
Eric Biederman6e53f502004-10-27 08:53:57 +0000335 struct lb_memory *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000336
Eric Biederman6e53f502004-10-27 08:53:57 +0000337 /* Record where the lb memory ranges will live */
338 mem = lb_memory(head);
339 mem_ranges = mem;
340
341 /* Build the raw table of memory */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000342 search_global_resources(
343 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
344 build_lb_mem_range, mem);
Eric Biederman6e53f502004-10-27 08:53:57 +0000345 lb_cleanup_memory_ranges(mem);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000346 return mem;
347}
348
Eric Biederman8ca8d762003-04-22 19:02:15 +0000349unsigned long write_linuxbios_table(
Eric Biederman8ca8d762003-04-22 19:02:15 +0000350 unsigned long low_table_start, unsigned long low_table_end,
351 unsigned long rom_table_startk, unsigned long rom_table_endk)
352{
353 unsigned long table_size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000354 struct lb_header *head;
355 struct lb_memory *mem;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000356
Eric Biederman8ca8d762003-04-22 19:02:15 +0000357 head = lb_table_init(low_table_end);
358 low_table_end = (unsigned long)head;
Eric Biederman6e53f502004-10-27 08:53:57 +0000359 if (HAVE_OPTION_TABLE == 1) {
360 struct lb_record *rec_dest, *rec_src;
361 /* Write the option config table... */
362 rec_dest = lb_new_record(head);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000363 rec_src = (struct lb_record *)(void *)&option_table;
Eric Biederman6e53f502004-10-27 08:53:57 +0000364 memcpy(rec_dest, rec_src, rec_src->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000365 }
Eric Biederman6e53f502004-10-27 08:53:57 +0000366 /* Record where RAM is located */
367 mem = build_lb_mem(head);
368
369 /* Find the current mptable size */
370 table_size = (low_table_end - low_table_start);
371
372 /* Record the mptable and the the lb_table (This will be adjusted later) */
373 lb_add_memory_range(mem, LB_MEM_TABLE,
374 low_table_start, table_size);
375
376 /* Record the pirq table */
377 lb_add_memory_range(mem, LB_MEM_TABLE,
378 rom_table_startk << 10, (rom_table_endk - rom_table_startk) << 10);
379
380 /* Note:
381 * I assume that there is always memory at immediately after
382 * the low_table_end. This means that after I setup the linuxbios table.
383 * I can trivially fixup the reserved memory ranges to hold the correct
384 * size of the linuxbios table.
385 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000386
387 /* Record our motheboard */
388 lb_mainboard(head);
389 /* Record our various random string information */
390 lb_strings(head);
391
392 low_table_end = lb_table_fini(head);
393
394 /* Remember where my valid memory ranges are */
395 return low_table_end;
396}