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