Added tables.c
[coreboot.git] / src / arch / ppc / boot / linuxbios_table.c
1 #include <mem.h>
2 #include <ip_checksum.h>
3 #include <boot/linuxbios_tables.h>
4 #include "linuxbios_table.h"
5 #include <console/console.h>
6 #include <string.h>
7 #include <version.h>
8
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         int i;
119         for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
120                 struct lb_record *rec;
121                 struct lb_string *str;
122                 size_t len;
123                 rec = lb_new_record(header);
124                 str = (struct lb_string *)rec;
125                 len = strlen(strings[i].string);
126                 str->tag = strings[i].tag;
127                 str->size = (sizeof(*rec) + len + 1 + 3) & ~3;
128                 memcpy(str->string, strings[i].string, len+1);
129         }
130
131 }
132
133 /* Some version of gcc have problems with 64 bit types so
134  * take an unsigned long instead of a uint64_t for now.
135  */
136 void lb_memory_range(struct lb_memory *mem,
137         uint32_t type, unsigned long start, unsigned long size)
138 {
139         int entries;
140         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
141         mem->map[entries].start = start;
142         mem->map[entries].size = size;
143         mem->map[entries].type = type;
144         mem->size += sizeof(mem->map[0]);
145 }
146
147 static void lb_memory_rangek(struct lb_memory *mem,
148         uint32_t type, unsigned long startk, unsigned long endk)
149 {
150         int entries;
151         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
152         mem->map[entries].start = startk;
153         mem->map[entries].start <<= 10;
154         mem->map[entries].size = endk - startk;
155         mem->map[entries].size <<= 10;
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 = mem->map[i].start;
179                 uint64_t map_end = map_start + mem->map[i].size;
180                 /* Does this area need to be expanded? */
181                 if (map_end == start) {
182                         mem->map[i].size = end - map_start;
183                 }
184                 /* Does this area need to be contracted? */
185                 else if (map_start == start) {
186                         mem->map[i].start = end;
187                         mem->map[i].size = map_end - end;
188                 }
189         }
190 }
191
192
193 unsigned long lb_table_fini(struct lb_header *head)
194 {
195         struct lb_record *rec, *first_rec;
196         rec = lb_last_record(head);
197         if (head->table_entries) {
198                 head->table_bytes += rec->size;
199         }
200         lb_reserve_table_memory(head);
201         first_rec = lb_first_record(head);
202         head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
203         head->header_checksum = 0;
204         head->header_checksum = compute_ip_checksum(head, sizeof(*head));
205         printk_debug("Wrote linuxbios table at: %p - %p  checksum %lx\n",
206                 head, rec, head->table_checksum);
207         return (unsigned long)rec;
208 }
209
210
211 /* Routines to extract part so the linuxBIOS table or 
212  * information from the linuxBIOS table after we have written it.
213  * Currently get_lb_mem relies on a global we can change the
214  * implementaiton.
215  */
216 static struct lb_memory *mem_ranges = 0;
217 struct lb_memory *get_lb_mem(void)
218 {
219         return mem_ranges;
220 }
221
222 unsigned long write_linuxbios_table( 
223         unsigned long *processor_map, 
224         struct mem_range *ram,
225         unsigned long low_table_start, unsigned long low_table_end,
226         unsigned long rom_table_startk, unsigned long rom_table_endk)
227 {
228         unsigned long table_size;
229         struct mem_range *ramp;
230         struct lb_header *head;
231         struct lb_memory *mem;
232 #if HAVE_OPTION_TABLE == 1
233         struct lb_record *rec_dest, *rec_src;
234 #endif  
235
236         head = lb_table_init(low_table_end);
237         low_table_end = (unsigned long)head;
238 #if HAVE_OPTION_TABLE == 1
239         /* Write the option config table... */
240         rec_dest = lb_new_record(head);
241         rec_src = (struct lb_record *)&option_table;
242         memcpy(rec_dest,  rec_src, rec_src->size);
243 #endif  
244         mem = lb_memory(head);
245         mem_ranges = mem;
246         /* I assume there is always ram at address 0 */
247         /* Reserve our tables in low memory */
248         table_size = (low_table_end - low_table_start);
249         lb_memory_range(mem, LB_MEM_TABLE, 0, table_size);
250         lb_memory_range(mem, LB_MEM_RAM, table_size, (ram[0].sizek << 10) - table_size);
251         /* Reserving pci memory mapped  space will keep the kernel from booting seeing
252          * any pci resources.
253          */
254         for(ramp = &ram[1]; ramp->sizek; ramp++) {
255                 unsigned long startk, endk;
256                 startk = ramp->basek;
257                 endk = startk + ramp->sizek;
258                 if ((startk < rom_table_startk) && (endk > rom_table_startk)) {
259                         lb_memory_rangek(mem, LB_MEM_RAM, startk, rom_table_startk);
260                         startk = rom_table_startk;
261                 }
262                 if ((startk == rom_table_startk) && (endk > startk)) {
263                         unsigned long tend;
264                         tend = rom_table_endk;
265                         if (tend > endk) {
266                                 tend = endk;
267                         }
268                         lb_memory_rangek(mem, LB_MEM_TABLE, rom_table_startk, tend);
269                         startk = tend;
270                 }
271                 if (endk > startk) {
272                         lb_memory_rangek(mem, LB_MEM_RAM, startk, endk);
273                 }
274         }
275
276         /* Record our motheboard */
277         lb_mainboard(head);
278         /* Record our various random string information */
279         lb_strings(head);
280
281         low_table_end = lb_table_fini(head);
282
283         /* Remember where my valid memory ranges are */
284         return low_table_end;
285 }