Check for NULL before calling device_match()
[coreboot.git] / util / sconfig / main.c
1 /*
2  * sconfig, coreboot device tree compiler
3  *
4  * Copyright (C) 2010 coresystems GmbH
5  *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19  */
20
21 #include "sconfig.h"
22 #include "sconfig.tab.h"
23
24 struct device *head, *lastdev;
25
26 struct header headers;
27
28 static int devcount = 0;
29
30 static struct device root;
31 static struct device mainboard = {
32         .name = "mainboard",
33         .name_underscore = "mainboard",
34         .id = 0,
35         .chip = &mainboard,
36         .type = chip,
37         .chiph_exists = 1,
38         .children = &root
39 };
40
41 static struct device root = {
42         .name = "dev_root",
43         .name_underscore = "dev_root",
44         .id = 0,
45         .chip = &mainboard,
46         .type = device,
47         .path = " .type = DEVICE_PATH_ROOT ",
48         .ops = "&default_dev_ops_root",
49         .parent = &root,
50         .bus = &root,
51         .enabled = 1
52 };
53
54 static struct device *new_dev(struct device *parent, struct device *bus) {
55         struct device *dev = malloc(sizeof(struct device));
56         memset(dev, 0, sizeof(struct device));
57         dev->id = ++devcount;
58         dev->parent = parent;
59         dev->bus = bus;
60         head->next = dev;
61         head = dev;
62         return dev;
63 }
64
65 static int device_match(struct device *a, struct device *b) {
66         if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
67                 return 1;
68         return 0;
69 }
70
71 void fold_in(struct device *parent) {
72         struct device *child = parent->children;
73         struct device *latest = 0;
74         while (child != latest) {
75                 if (child->children) {
76                         if (!latest) latest = child->children;
77                         parent->latestchild->next_sibling = child->children;
78                         parent->latestchild = child->latestchild;
79                 }
80                 child = child->next_sibling;
81         }
82 }
83
84 int yywrap(void) {
85         return 1;
86 }
87
88 void yyerror (char const *str)
89 {
90         fprintf (stderr, "%s\n", str);
91 }
92
93 void postprocess_devtree(void) {
94         root.next_sibling = root.children;
95         root.next_sibling->next_sibling = root.next_sibling->children;
96
97         struct device *dev = &root;
98         while (dev) {
99                 /* skip "chip" elements in children chain */
100                 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
101                 /* skip "chip" elements and functions of the same device in sibling chain */
102                 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
103                 /* If end of chain, and parent is a chip, move on */
104                 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
105                 /* skip chips */
106                 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
107                 /* skip duplicate function elements in nextdev chain */
108                 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
109                 dev = dev->next_sibling;
110         }
111 }
112
113 struct device *new_chip(struct device *parent, struct device *bus, char *path) {
114         struct device *new_chip = new_dev(parent, bus);
115         new_chip->chiph_exists = 1;
116         new_chip->name = path;
117         new_chip->name_underscore = strdup(new_chip->name);
118         char *c;
119         for (c = new_chip->name_underscore; *c; c++) {
120                 if (*c == '/') *c = '_';
121                 if (*c == '-') *c = '_';
122         }
123         new_chip->type = chip;
124         new_chip->chip = new_chip;
125
126         struct stat st;
127         char *chip_h = malloc(strlen(path)+12);
128         sprintf(chip_h, "src/%s/chip.h", path);
129         if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
130                 new_chip->chiph_exists = 0;
131
132         if (parent->latestchild) {
133                 parent->latestchild->next_sibling = new_chip;
134                 parent->latestchild->sibling = new_chip;
135         }
136         parent->latestchild = new_chip;
137         if (!parent->children)
138                 parent->children = new_chip;
139         return new_chip;
140 }
141
142 void add_header(struct device *dev) {
143         if (dev->chiph_exists) {
144                 int include_exists = 0;
145                 struct header *h = &headers;
146                 while (h->next) {
147                         int result = strcmp(dev->name, h->next->name);
148                         if (result == 0) {
149                                 include_exists = 1;
150                                 break;
151                         }
152                         if (result < 0) break;
153                         h = h->next;
154                 }
155                 if (!include_exists) {
156                         struct header *tmp = h->next;
157                         h->next = malloc(sizeof(struct header));
158                         memset(h->next, 0, sizeof(struct header));
159                         h->next->name = dev->name;
160                         h->next->next = tmp;
161                 }
162         }
163 }
164
165 struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
166         struct device *new_d = new_dev(parent, busdev);
167         new_d->bustype = bus;
168
169         char *tmp;
170         new_d->path_a = strtol(strdup(devnum), &tmp, 16);
171         if (*tmp == '.') {
172                 tmp++;
173                 new_d->path_b = strtol(tmp, NULL, 16);
174         }
175
176         char *name = malloc(10);
177         sprintf(name, "_dev%d", new_d->id);
178         new_d->name = name;
179         new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
180         new_d->type = device;
181         new_d->enabled = enabled;
182         new_d->chip = new_d->parent->chip;
183
184         if (parent->latestchild) {
185                 parent->latestchild->next_sibling = new_d;
186                 parent->latestchild->sibling = new_d;
187         }
188         parent->latestchild = new_d;
189         if (!parent->children)
190                 parent->children = new_d;
191
192         lastdev->nextdev = new_d;
193         lastdev = new_d;
194         if (bus == PCI) {
195                 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
196         }
197         if (bus == PNP) {
198                 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
199         }
200         if (bus == I2C) {
201                 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
202         }
203         if (bus == APIC) {
204                 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
205         }
206         if (bus == APIC_CLUSTER) {
207                 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
208         }
209         if (bus == PCI_DOMAIN) {
210                 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
211         }
212         return new_d;
213 }
214
215 void alias_siblings(struct device *d) {
216         while (d) {
217                 int link = 0;
218                 struct device *cmp = d->next_sibling;
219                 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
220                         if (cmp->type==device && !cmp->used) {
221                                 if (device_match(d, cmp)) {
222                                         d->multidev = 1;
223
224                                         cmp->aliased_name = malloc(12);
225                                         sprintf(cmp->aliased_name, "_dev%d", cmp->id);
226                                         cmp->id = d->id;
227                                         cmp->name = d->name;
228                                         cmp->used = 1;
229                                         cmp->link = ++link;
230                                 }
231                         }
232                         cmp = cmp->next_sibling;
233                 }
234                 d = d->next_sibling;
235         }
236 }
237
238 void add_resource(struct device *dev, int type, int index, int base) {
239         struct resource *r = malloc(sizeof(struct resource));
240         memset (r, 0, sizeof(struct resource));
241         r->type = type;
242         r->index = index;
243         r->base = base;
244         if (dev->res) {
245                 struct resource *head = dev->res;
246                 while (head->next) head = head->next;
247                 head->next = r;
248         } else {
249                 dev->res = r;
250         }
251         dev->rescnt++;
252 }
253
254 void add_register(struct device *dev, char *name, char *val) {
255         struct reg *r = malloc(sizeof(struct reg));
256         memset (r, 0, sizeof(struct reg));
257         r->key = name;
258         r->value = val;
259         if (dev->reg) {
260                 struct reg *head = dev->reg;
261                 // sorting to be equal to sconfig's behaviour
262                 int sort = strcmp(r->key, head->key);
263                 if (sort == 0) {
264                         printf("ERROR: duplicate 'register' key.\n");
265                         exit(1);
266                 }
267                 if (sort<0) {
268                         r->next = head;
269                         dev->reg = r;
270                 } else {
271                         while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
272                         r->next = head->next;
273                         head->next = r;
274                 }
275         } else {
276                 dev->reg = r;
277         }
278 }
279
280 static void pass0(FILE *fil, struct device *ptr) {
281         if (ptr->type == device && ptr->id == 0)
282                 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
283         if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
284                 fprintf(fil, "struct device %s;\n", ptr->name);
285                 if (ptr->rescnt > 0)
286                         fprintf(fil, "struct resource %s_res[];\n", ptr->name);
287                 if (ptr->children || ptr->multidev)
288                         fprintf(fil, "struct bus %s_links[];\n", ptr->name);
289         }
290         if ((ptr->type == device) && (ptr->id != 0) && ptr->used)
291                 fprintf(fil, "struct device %s;\n", ptr->aliased_name);
292 }
293
294 static void pass1(FILE *fil, struct device *ptr) {
295         if (!ptr->used && (ptr->type == device)) {
296                 fprintf(fil, "struct device %s = {\n", ptr->name);
297                 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
298                 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
299                 fprintf(fil, "\t.path = {");
300                 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
301                 fprintf(fil, "},\n");
302                 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
303                 fprintf(fil, "\t.on_mainboard = 1,\n");
304                 if (ptr->rescnt > 0) {
305                         fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
306                 }
307                 int link = 0;
308                 if (ptr->children || ptr->multidev)
309                         fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
310                 else
311                         fprintf(fil, "\t.link_list = NULL,\n", ptr->name);
312                 if (ptr->sibling)
313                         fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
314                 if (ptr->chip->chiph_exists) {
315                         fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
316                         fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
317                 }
318                 if (ptr->nextdev)
319                         fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
320                 fprintf(fil, "};\n");
321         }
322         if (ptr->rescnt > 0) {
323                 int i=1;
324                 fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
325                 struct resource *r = ptr->res;
326                 while (r) {
327                         fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
328                         if (r->type == IRQ) fprintf(fil, "IRQ");
329                         if (r->type == DRQ) fprintf(fil, "DRQ");
330                         if (r->type == IO) fprintf(fil, "IO");
331                         fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
332                         if (r->next)
333                                 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
334                         else
335                                 fprintf(fil, ".next=NULL },\n");
336                         r = r->next;
337                 }
338                 fprintf(fil, "\t };\n");
339         }
340         if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
341                 fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
342                 if (ptr->multidev) {
343                         struct device *d = ptr;
344                         while (d) {
345                                 if (device_match(d, ptr)) {
346                                         fprintf(fil, "\t\t[%d] = {\n", d->link);
347                                         fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
348                                         fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
349                                         if (d->children)
350                                                 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
351                                         if (d->next_sibling && device_match(d->next_sibling, ptr))
352                                                 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
353                                         else
354                                                 fprintf(fil, "\t\t\t.next = NULL,\n");
355                                         fprintf(fil, "\t\t},\n");
356                                 }
357                                 d = d->next_sibling;
358                         }
359                 } else {
360                         if (ptr->children) {
361                                 fprintf(fil, "\t\t[0] = {\n");
362                                 fprintf(fil, "\t\t\t.link_num = 0,\n");
363                                 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
364                                 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
365                                 fprintf(fil, "\t\t\t.next = NULL,\n");
366                                 fprintf(fil, "\t\t},\n");
367                         }
368                 }
369                 fprintf(fil, "\t};\n");
370         }
371         if ((ptr->type == chip) && (ptr->chiph_exists)) {
372                 if (ptr->reg) {
373                         fprintf(fil, "struct %s_config %s_info_%d\t= {\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
374                         struct reg *r = ptr->reg;
375                         while (r) {
376                                 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
377                                 r = r->next;
378                         }
379                         fprintf(fil, "};\n\n");
380                 } else {
381                         fprintf(fil, "struct %s_config %s_info_%d;\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
382                 }
383         }
384 }
385
386 static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
387         do {
388                 func(fil, ptr);
389                 ptr = ptr->next_sibling;
390         } while (ptr);
391 }
392
393 int main(int argc, char** argv) {
394         if (argc != 3) {
395                 printf("usage: sconfig vendor/mainboard outputdir\n");
396                 return 1;
397         }
398         char *mainboard=argv[1];
399         char *outputdir=argv[2];
400         char *devtree=malloc(strlen(mainboard)+30);
401         char *outputc=malloc(strlen(outputdir)+10);
402         sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
403         sprintf(outputc, "%s/static.c", outputdir);
404
405         headers.next = malloc(sizeof(struct header));
406         headers.next->name = malloc(strlen(mainboard)+12);
407         headers.next->next = 0;
408         sprintf(headers.next->name, "mainboard/%s", mainboard);
409
410         FILE *filec = fopen(devtree, "r");
411         yyrestart(filec);
412
413         FILE *staticc = fopen(outputc, "w");
414
415         lastdev = head = &root;
416         yyparse();
417         fclose(filec);
418
419         if ((head->type == chip) && (!head->chiph_exists)) {
420                 struct device *tmp = head;
421                 head = &root;
422                 while (head->next != tmp) head = head->next;
423         }
424
425         fprintf(staticc, "#include <device/device.h>\n");
426         fprintf(staticc, "#include <device/pci.h>\n");
427         struct header *h = &headers;
428         while (h->next) {
429                 h = h->next;
430                 fprintf(staticc, "#include \"%s/chip.h\"\n", h->name);
431         }
432         fprintf(staticc, "\n/* pass 0 */\n");
433         walk_device_tree(staticc, &root, pass0, NULL);
434         fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device **last_dev_p = &%s.next;\n", lastdev->name);
435         walk_device_tree(staticc, &root, pass1, NULL);
436
437         fclose(staticc);
438         return 0;
439 }