move mptable to 960k to 1M
[coreboot.git] / src / arch / ppc / 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 = pack_lb64(start);
137         mem->map[entries].size = pack_lb64(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 = unpack_lb64(mem->map[i].start);
161                 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
162                 /* Does this area need to be expanded? */
163                 if (map_end == start) {
164                         mem->map[i].size = pack_lb64(end - map_start);
165                 }
166                 /* Does this area need to be contracted? */
167                 else if (map_start == start) {
168                         mem->map[i].start = pack_lb64(end);
169                         mem->map[i].size = pack_lb64(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                 uint64_t entry_start = unpack_lb64(mem->map[i].start);
200                 for(j = i; j < entries; j++) {
201                         uint64_t temp_start = unpack_lb64(mem->map[j].start);
202                         if (temp_start < entry_start) {
203                                 struct lb_memory_range tmp;
204                                 tmp = mem->map[i];
205                                 mem->map[i] = mem->map[j];
206                                 mem->map[j] = tmp;
207                         }
208                 }
209         }
210
211         /* Merge adjacent entries */
212         for(i = 0; (i + 1) < entries; i++) {
213                 uint64_t start, end, nstart, nend;
214                 if (mem->map[i].type != mem->map[i + 1].type) {
215                         continue;
216                 }
217                 start  = unpack_lb64(mem->map[i].start);
218                 end    = start + unpack_lb64(mem->map[i].size);
219                 nstart = unpack_lb64(mem->map[i + 1].start);
220                 nend   = nstart + unpack_lb64(mem->map[i + 1].size);
221                 if ((start <= nstart) && (end > nstart)) {
222                         if (start > nstart) {
223                                 start = nstart;
224                         }
225                         if (end < nend) {
226                                 end = nend;
227                         }
228                         /* Record the new region size */
229                         mem->map[i].start = pack_lb64(start);
230                         mem->map[i].size  = pack_lb64(end - start);
231
232                         /* Delete the entry I have merged with */
233                         memmove(&mem->map[i + 1], &mem->map[i + 2], 
234                                 ((entries - i - 2) * sizeof(mem->map[0])));
235                         mem->size -= sizeof(mem->map[0]);
236                         entries -= 1;
237                         /* See if I can merge with the next entry as well */
238                         i -= 1; 
239                 }
240         }
241 }
242
243 static void lb_remove_memory_range(struct lb_memory *mem, 
244         uint64_t start, uint64_t size)
245 {
246         uint64_t end;
247         int entries;
248         int i;
249
250         end = start + size;
251         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
252
253         /* Remove a reserved area from the memory map */
254         for(i = 0; i < entries; i++) {
255                 uint64_t map_start = unpack_lb64(mem->map[i].start);
256                 uint64_t map_end   = map_start + unpack_lb64(mem->map[i].size);
257                 if ((start <= map_start) && (end >= map_end)) {
258                         /* Remove the completely covered range */
259                         memmove(&mem->map[i], &mem->map[i + 1], 
260                                 ((entries - i - 1) * sizeof(mem->map[0])));
261                         mem->size -= sizeof(mem->map[0]);
262                         entries -= 1;
263                         /* Since the index will disappear revisit what will appear here */
264                         i -= 1; 
265                 }
266                 else if ((start > map_start) && (end < map_end)) {
267                         /* Split the memory range */
268                         memmove(&mem->map[i + 1], &mem->map[i], 
269                                 ((entries - i) * sizeof(mem->map[0])));
270                         mem->size += sizeof(mem->map[0]);
271                         entries += 1;
272                         /* Update the first map entry */
273                         mem->map[i].size = pack_lb64(start - map_start);
274                         /* Update the second map entry */
275                         mem->map[i + 1].start = pack_lb64(end);
276                         mem->map[i + 1].size  = pack_lb64(map_end - end);
277                         /* Don't bother with this map entry again */
278                         i += 1;
279                 }
280                 else if ((start <= map_start) && (end > map_start)) {
281                         /* Shrink the start of the memory range */
282                         mem->map[i].start = pack_lb64(end);
283                         mem->map[i].size  = pack_lb64(map_end - end);
284                 }
285                 else if ((start < map_end) && (start > map_start)) {
286                         /* Shrink the end of the memory range */
287                         mem->map[i].size = pack_lb64(start - map_start);
288                 }
289         }
290 }
291
292 static void lb_add_memory_range(struct lb_memory *mem,
293         uint32_t type, uint64_t start, uint64_t size)
294 {
295         lb_remove_memory_range(mem, start, size);
296         lb_memory_range(mem, type, start, size);
297         lb_cleanup_memory_ranges(mem);
298 }
299
300 /* Routines to extract part so the linuxBIOS table or 
301  * information from the linuxBIOS table after we have written it.
302  * Currently get_lb_mem relies on a global we can change the
303  * implementaiton.
304  */
305 static struct lb_memory *mem_ranges = 0;
306 struct lb_memory *get_lb_mem(void)
307 {
308         return mem_ranges;
309 }
310
311 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
312 {
313         struct lb_memory *mem = gp;
314         lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
315 }
316
317 static struct lb_memory *build_lb_mem(struct lb_header *head)
318 {
319         struct lb_memory *mem;
320
321         /* Record where the lb memory ranges will live */
322         mem = lb_memory(head);
323         mem_ranges = mem;
324
325         /* Build the raw table of memory */
326         search_global_resources(
327                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
328                 build_lb_mem_range, mem);
329         lb_cleanup_memory_ranges(mem);
330         return mem;
331 }
332
333 unsigned long write_linuxbios_table( 
334         unsigned long low_table_start, unsigned long low_table_end,
335         unsigned long rom_table_start, unsigned long rom_table_end)
336 {
337         unsigned long table_size;
338         struct lb_header *head;
339         struct lb_memory *mem;
340
341         head = lb_table_init(low_table_end);
342         low_table_end = (unsigned long)head;
343         if (HAVE_OPTION_TABLE == 1) {
344                 struct lb_record *rec_dest, *rec_src;
345                 /* Write the option config table... */
346                 rec_dest = lb_new_record(head);
347                 rec_src = (struct lb_record *)(void *)&option_table;
348                 memcpy(rec_dest,  rec_src, rec_src->size);
349         }
350         /* Record where RAM is located */
351         mem = build_lb_mem(head);
352         
353         /* Find the current mptable size */
354         table_size = (low_table_end - low_table_start);
355
356         /* Record the mptable and the the lb_table (This will be adjusted later) */
357         lb_add_memory_range(mem, LB_MEM_TABLE, 
358                 low_table_start, table_size);
359
360         /* Record the pirq table */
361         lb_add_memory_range(mem, LB_MEM_TABLE, 
362                 rom_table_start, (rom_table_end - rom_table_start));
363
364         /* Note:
365          * I assume that there is always memory at immediately after
366          * the low_table_end.  This means that after I setup the linuxbios table.
367          * I can trivially fixup the reserved memory ranges to hold the correct
368          * size of the linuxbios table.
369          */
370
371         /* Record our motheboard */
372         lb_mainboard(head);
373         /* Record our various random string information */
374         lb_strings(head);
375
376         low_table_end = lb_table_fini(head);
377
378         /* Remember where my valid memory ranges are */
379         return low_table_end;
380 }