sizeram removal/conversion.
[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 void 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         };
118         unsigned int i;
119         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
131 void lb_memory_range(struct lb_memory *mem,
132         uint32_t type, uint64_t start, uint64_t size)
133 {
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
142 static 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
174 unsigned 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
191 static 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
241 static 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
290 static 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 }
297
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  */
303 static struct lb_memory *mem_ranges = 0;
304 struct lb_memory *get_lb_mem(void)
305 {
306         return mem_ranges;
307 }
308
309 static struct lb_memory *build_lb_mem(struct lb_header *head)
310 {
311         struct lb_memory *mem;
312         struct device *dev;
313
314         /* Record where the lb memory ranges will live */
315         mem = lb_memory(head);
316         mem_ranges = mem;
317
318         /* Build the raw table of memory */
319         for(dev = all_devices; dev; dev = dev->next) {
320                 struct resource *res, *last;
321                 last = &dev->resource[dev->resources];
322                 for(res = &dev->resource[0]; res < last; res++) {
323                         if (!(res->flags & IORESOURCE_MEM) ||
324                                 !(res->flags & IORESOURCE_CACHEABLE)) {
325                                 continue;
326                         }
327                         lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
328                 }
329         }
330         lb_cleanup_memory_ranges(mem);
331         return mem;
332 }
333
334 unsigned long write_linuxbios_table( 
335         unsigned long low_table_start, unsigned long low_table_end,
336         unsigned long rom_table_startk, unsigned long rom_table_endk)
337 {
338         unsigned long table_size;
339         struct lb_header *head;
340         struct lb_memory *mem;
341
342         head = lb_table_init(low_table_end);
343         low_table_end = (unsigned long)head;
344         if (HAVE_OPTION_TABLE == 1) {
345                 struct lb_record *rec_dest, *rec_src;
346                 /* Write the option config table... */
347                 rec_dest = lb_new_record(head);
348                 rec_src = (struct lb_record *)&option_table;
349                 memcpy(rec_dest,  rec_src, rec_src->size);
350         }
351         /* Record where RAM is located */
352         mem = build_lb_mem(head);
353         
354         /* Find the current mptable size */
355         table_size = (low_table_end - low_table_start);
356
357         /* Record the mptable and the the lb_table (This will be adjusted later) */
358         lb_add_memory_range(mem, LB_MEM_TABLE, 
359                 low_table_start, table_size);
360
361         /* Record the pirq table */
362         lb_add_memory_range(mem, LB_MEM_TABLE, 
363                 rom_table_startk << 10, (rom_table_endk - rom_table_startk) << 10);
364
365         /* Note:
366          * I assume that there is always memory at immediately after
367          * the low_table_end.  This means that after I setup the linuxbios table.
368          * I can trivially fixup the reserved memory ranges to hold the correct
369          * size of the linuxbios table.
370          */
371
372         /* Record our motheboard */
373         lb_mainboard(head);
374         /* Record our various random string information */
375         lb_strings(head);
376
377         low_table_end = lb_table_fini(head);
378
379         /* Remember where my valid memory ranges are */
380         return low_table_end;
381 }