util/ renames
[coreboot.git] / src / arch / i386 / boot / linuxbios_table.c
1 #include <console/console.h>
2 #include <ip_checksum.h>
3 #include <boot/linuxbios_tables.h>
4 #include "linuxbios_table.h"
5 #include <string.h>
6 #include <version.h>
7 #include <device/device.h>
8 #include <stdlib.h>
9
10 struct 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
31 struct 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
38 struct 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
45 struct lb_record *lb_next_record(struct lb_record *rec)
46 {
47         rec = (void *)(((char *)rec) + rec->size);      
48         return rec;
49 }
50
51 struct 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
66 struct 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
77 struct 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
101 struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
102 {
103         struct lb_record *rec;
104         struct cmos_checksum *cmos_checksum;
105         rec = lb_new_record(header);
106         cmos_checksum = (struct cmos_checksum *)rec;
107         cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
108
109         cmos_checksum->size = (sizeof(*cmos_checksum));
110
111         cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
112         cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
113         cmos_checksum->location = LB_CKS_LOC * 8;
114         cmos_checksum->type = CHECKSUM_PCBIOS;
115         
116         return cmos_checksum;
117 }
118
119 void lb_strings(struct lb_header *header)
120 {
121         static const struct {
122                 uint32_t tag;
123                 const char *string;
124         } strings[] = {
125                 { LB_TAG_VERSION,        coreboot_version,        },
126                 { LB_TAG_EXTRA_VERSION,  coreboot_extra_version,  },
127                 { LB_TAG_BUILD,          coreboot_build,          },
128                 { LB_TAG_COMPILE_TIME,   coreboot_compile_time,   },
129                 { LB_TAG_COMPILE_BY,     coreboot_compile_by,     },
130                 { LB_TAG_COMPILE_HOST,   coreboot_compile_host,   },
131                 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
132                 { LB_TAG_COMPILER,       coreboot_compiler,       },
133                 { LB_TAG_LINKER,         coreboot_linker,         },
134                 { LB_TAG_ASSEMBLER,      coreboot_assembler,      },
135         };
136         unsigned int i;
137         for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
138                 struct lb_string *rec;
139                 size_t len;
140                 rec = (struct lb_string *)lb_new_record(header);
141                 len = strlen(strings[i].string);
142                 rec->tag = strings[i].tag;
143                 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
144                 memcpy(rec->string, strings[i].string, len+1);
145         }
146
147 }
148
149 void lb_memory_range(struct lb_memory *mem,
150         uint32_t type, uint64_t start, uint64_t size)
151 {
152         int entries;
153         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
154         mem->map[entries].start = pack_lb64(start);
155         mem->map[entries].size = pack_lb64(size);
156         mem->map[entries].type = type;
157         mem->size += sizeof(mem->map[0]);
158 }
159
160 static void lb_reserve_table_memory(struct lb_header *head)
161 {
162         struct lb_record *last_rec;
163         struct lb_memory *mem;
164         uint64_t start;
165         uint64_t end;
166         int i, entries;
167
168         last_rec = lb_last_record(head);
169         mem = get_lb_mem();
170         start = (unsigned long)head;
171         end = (unsigned long)last_rec;
172         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
173         /* Resize the right two memory areas so this table is in
174          * a reserved area of memory.  Everything has been carefully
175          * setup so that is all we need to do.
176          */
177         for(i = 0; i < entries; i++ ) {
178                 uint64_t map_start = unpack_lb64(mem->map[i].start);
179                 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
180                 /* Does this area need to be expanded? */
181                 if (map_end == start) {
182                         mem->map[i].size = pack_lb64(end - map_start);
183                 }
184                 /* Does this area need to be contracted? */
185                 else if (map_start == start) {
186                         mem->map[i].start = pack_lb64(end);
187                         mem->map[i].size = pack_lb64(map_end - end);
188                 }
189         }
190 }
191
192 unsigned long lb_table_fini(struct lb_header *head)
193 {
194         struct lb_record *rec, *first_rec;
195         rec = lb_last_record(head);
196         if (head->table_entries) {
197                 head->table_bytes += rec->size;
198         }
199         lb_reserve_table_memory(head);
200         first_rec = lb_first_record(head);
201         head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
202         head->header_checksum = 0;
203         head->header_checksum = compute_ip_checksum(head, sizeof(*head));
204         printk_debug("Wrote coreboot table at: %p - %p  checksum %lx\n",
205                 head, rec, head->table_checksum);
206         return (unsigned long)rec;
207 }
208
209 static void lb_cleanup_memory_ranges(struct lb_memory *mem)
210 {
211         int entries;
212         int i, j;
213         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
214         
215         /* Sort the lb memory ranges */
216         for(i = 0; i < entries; i++) {
217                 uint64_t entry_start = unpack_lb64(mem->map[i].start);
218                 for(j = i; j < entries; j++) {
219                         uint64_t temp_start = unpack_lb64(mem->map[j].start);
220                         if (temp_start < entry_start) {
221                                 struct lb_memory_range tmp;
222                                 tmp = mem->map[i];
223                                 mem->map[i] = mem->map[j];
224                                 mem->map[j] = tmp;
225                         }
226                 }
227         }
228
229         /* Merge adjacent entries */
230         for(i = 0; (i + 1) < entries; i++) {
231                 uint64_t start, end, nstart, nend;
232                 if (mem->map[i].type != mem->map[i + 1].type) {
233                         continue;
234                 }
235                 start  = unpack_lb64(mem->map[i].start);
236                 end    = start + unpack_lb64(mem->map[i].size);
237                 nstart = unpack_lb64(mem->map[i + 1].start);
238                 nend   = nstart + unpack_lb64(mem->map[i + 1].size);
239                 if ((start <= nstart) && (end > nstart)) {
240                         if (start > nstart) {
241                                 start = nstart;
242                         }
243                         if (end < nend) {
244                                 end = nend;
245                         }
246                         /* Record the new region size */
247                         mem->map[i].start = pack_lb64(start);
248                         mem->map[i].size  = pack_lb64(end - start);
249
250                         /* Delete the entry I have merged with */
251                         memmove(&mem->map[i + 1], &mem->map[i + 2], 
252                                 ((entries - i - 2) * sizeof(mem->map[0])));
253                         mem->size -= sizeof(mem->map[0]);
254                         entries -= 1;
255                         /* See if I can merge with the next entry as well */
256                         i -= 1; 
257                 }
258         }
259 }
260
261 static void lb_remove_memory_range(struct lb_memory *mem, 
262         uint64_t start, uint64_t size)
263 {
264         uint64_t end;
265         int entries;
266         int i;
267
268         end = start + size;
269         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
270
271         /* Remove a reserved area from the memory map */
272         for(i = 0; i < entries; i++) {
273                 uint64_t map_start = unpack_lb64(mem->map[i].start);
274                 uint64_t map_end   = map_start + unpack_lb64(mem->map[i].size);
275                 if ((start <= map_start) && (end >= map_end)) {
276                         /* Remove the completely covered range */
277                         memmove(&mem->map[i], &mem->map[i + 1], 
278                                 ((entries - i - 1) * sizeof(mem->map[0])));
279                         mem->size -= sizeof(mem->map[0]);
280                         entries -= 1;
281                         /* Since the index will disappear revisit what will appear here */
282                         i -= 1; 
283                 }
284                 else if ((start > map_start) && (end < map_end)) {
285                         /* Split the memory range */
286                         memmove(&mem->map[i + 1], &mem->map[i], 
287                                 ((entries - i) * sizeof(mem->map[0])));
288                         mem->size += sizeof(mem->map[0]);
289                         entries += 1;
290                         /* Update the first map entry */
291                         mem->map[i].size = pack_lb64(start - map_start);
292                         /* Update the second map entry */
293                         mem->map[i + 1].start = pack_lb64(end);
294                         mem->map[i + 1].size  = pack_lb64(map_end - end);
295                         /* Don't bother with this map entry again */
296                         i += 1;
297                 }
298                 else if ((start <= map_start) && (end > map_start)) {
299                         /* Shrink the start of the memory range */
300                         mem->map[i].start = pack_lb64(end);
301                         mem->map[i].size  = pack_lb64(map_end - end);
302                 }
303                 else if ((start < map_end) && (start > map_start)) {
304                         /* Shrink the end of the memory range */
305                         mem->map[i].size = pack_lb64(start - map_start);
306                 }
307         }
308 }
309
310 static void lb_add_memory_range(struct lb_memory *mem,
311         uint32_t type, uint64_t start, uint64_t size)
312 {
313         lb_remove_memory_range(mem, start, size);
314         lb_memory_range(mem, type, start, size);
315         lb_cleanup_memory_ranges(mem);
316 }
317
318 /* Routines to extract part so the coreboot table or 
319  * information from the coreboot table after we have written it.
320  * Currently get_lb_mem relies on a global we can change the
321  * implementaiton.
322  */
323 static struct lb_memory *mem_ranges = 0;
324 struct lb_memory *get_lb_mem(void)
325 {
326         return mem_ranges;
327 }
328
329 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
330 {
331         struct lb_memory *mem = gp;
332         lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
333 }
334
335 static struct lb_memory *build_lb_mem(struct lb_header *head)
336 {
337         struct lb_memory *mem;
338
339         /* Record where the lb memory ranges will live */
340         mem = lb_memory(head);
341         mem_ranges = mem;
342
343         /* Build the raw table of memory */
344         search_global_resources(
345                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
346                 build_lb_mem_range, mem);
347         lb_cleanup_memory_ranges(mem);
348         return mem;
349 }
350
351 unsigned long write_coreboot_table( 
352         unsigned long low_table_start, unsigned long low_table_end, 
353         unsigned long rom_table_start, unsigned long rom_table_end)
354 {
355         unsigned long table_size;
356         struct lb_header *head;
357         struct lb_memory *mem;
358
359         if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
360                 /* We need to put lbtable on  to [0xf0000,0x100000) */
361                 head = lb_table_init(rom_table_end);
362                 rom_table_end = (unsigned long)head;
363         } else {
364                 head = lb_table_init(low_table_end);
365                 low_table_end = (unsigned long)head;
366         }
367  
368         printk_debug("Adjust low_table_end from 0x%08x to ", low_table_end);
369         low_table_end += 0xfff; // 4K aligned
370         low_table_end &= ~0xfff;
371         printk_debug("0x%08x \n", low_table_end);
372
373         /* The Linux kernel assumes this region is reserved */
374         printk_debug("Adjust rom_table_end from 0x%08x to ", rom_table_end);
375         rom_table_end += 0xffff; // 64K align
376         rom_table_end &= ~0xffff;
377         printk_debug("0x%08x \n", rom_table_end);
378
379 #if (HAVE_OPTION_TABLE == 1) 
380         {
381                 struct lb_record *rec_dest, *rec_src;
382                 /* Write the option config table... */
383                 rec_dest = lb_new_record(head);
384                 rec_src = (struct lb_record *)(void *)&option_table;
385                 memcpy(rec_dest,  rec_src, rec_src->size);
386                 /* Create cmos checksum entry in coreboot table */
387                 lb_cmos_checksum(head);
388         }
389 #endif
390         /* Record where RAM is located */
391         mem = build_lb_mem(head);
392         
393         /* Record the mptable and the the lb_table (This will be adjusted later) */
394         lb_add_memory_range(mem, LB_MEM_TABLE, 
395                 low_table_start, low_table_end - low_table_start);
396
397         /* Record the pirq table, acpi tables, and maybe the mptable */
398         table_size=rom_table_end-rom_table_start;
399         lb_add_memory_range(mem, LB_MEM_TABLE, 
400                 rom_table_start, table_size<0x10000?0x10000:table_size);
401
402         /* Note:
403          * I assume that there is always memory at immediately after
404          * the low_table_end.  This means that after I setup the coreboot table.
405          * I can trivially fixup the reserved memory ranges to hold the correct
406          * size of the coreboot table.
407          */
408
409         /* Record our motheboard */
410         lb_mainboard(head);
411         /* Record our various random string information */
412         lb_strings(head);
413
414         /* Remember where my valid memory ranges are */
415         return lb_table_fini(head);
416         
417 }