blob: b48f9926720c69443a5fe2a24ecddaaa4f1e793c [file] [log] [blame]
Patrick Georgi114e7b22010-05-05 11:19:50 +00001/*
2 * sconfig, coreboot device tree compiler
3 *
4 * Copyright (C) 2010 coresystems GmbH
Stefan Reinauer2e78aa52016-05-07 01:11:14 -07005 * written by Patrick Georgi <patrick@georgi-clan.de>
Patrick Georgi114e7b22010-05-05 11:19:50 +00006 *
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.
Patrick Georgi114e7b22010-05-05 11:19:50 +000015 */
16
Patrick Georgi2dbfcb72012-05-30 16:26:30 +020017#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +020018/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
19#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +020020#include <commonlib/helpers.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000021#include "sconfig.h"
22#include "sconfig.tab.h"
23
Patrick Georgi7fc9e292010-07-15 15:59:07 +000024extern int linenum;
25
Furquan Shaikh27efc502018-06-22 09:19:15 -070026/*
27 * Maintains list of all the unique chip structures for the board.
28 * This is shared across base and override device trees since we need to
29 * generate headers for all chips added by both the trees.
30 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070031static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070032
33/*
34 * This is intentionally shared between chip and device structure ids because it
35 * is easier to track the order of parsing for chip and device.
36 */
37static int count = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +000038
Kyösti Mälkki472d9022011-12-05 20:33:55 +020039typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020040 UNSLASH,
41 SPLIT_1ST,
42 TO_LOWER,
43 TO_UPPER,
44} translate_t;
45
Furquan Shaikh93198262018-06-03 04:22:17 -070046/*
47 * Mainboard is assumed to have a root device whose bus is the parent of all the
48 * devices that are added by parsing the devicetree file. This device has a
49 * mainboard chip instance associated with it.
50 *
51 *
52 *
53 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070054 * | Root device | | Mainboard |
55 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070056 * | | | chip_instance | (mainboard_instance)|
57 * | +------------------------+ | |
58 * | | +----------------------+
59 * | | bus |
60 * | parent v |
61 * | +-------------------+ |
62 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070063 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070064 * | | |
65 * +-------------------+ |
66 * | |
67 * | children | chip
68 * v |
69 * X |
70 * (new devices will |
71 * be added here as |
72 * children) |
73 * |
74 * |
75 * |
76 * +-------+----------+
77 * | |
78 * | Mainboard chip +----------->X (new chips will be
79 * | (mainboard_chip) | added here)
80 * | |
81 * +------------------+
82 *
83 *
84 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070085
86/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070087static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070088
89/* Root device of override tree (if applicable). */
90static struct device override_root_dev;
91
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070092static struct chip_instance mainboard_instance;
93
Furquan Shaikhde39fc72018-06-11 04:26:45 -070094static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070095 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070096 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070097};
98
Furquan Shaikhde39fc72018-06-11 04:26:45 -070099static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -0700100 .name = "dev_root",
101 .id = 0,
102 .chip_instance = &mainboard_instance,
103 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700104 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -0700105 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700106 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -0700107};
108
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700109static struct bus override_root_bus = {
110 .id = 0,
111 .dev = &override_root_dev,
112};
113
114static struct device override_root_dev = {
115 .name = "override_root",
116 .id = 0,
117 /*
118 * Override tree root device points to the same mainboard chip instance
119 * as the base tree root device. It should not cause any side-effects
120 * since the mainboard chip instance pointer in override tree will just
121 * be ignored.
122 */
123 .chip_instance = &mainboard_instance,
124 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700125 .parent = &override_root_bus,
126 .enabled = 1,
127 .bus = &override_root_bus,
128};
129
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700130static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000131 .name = "mainboard",
132 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700133 .instance = &mainboard_instance,
134};
135
136static struct chip_instance mainboard_instance = {
137 .id = 0,
138 .chip = &mainboard_chip,
Furquan Shaikh27efc502018-06-22 09:19:15 -0700139 .ref_count = 2,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000140};
141
Furquan Shaikh93198262018-06-03 04:22:17 -0700142/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700143struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000144
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700145struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700146 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700147 struct queue_entry *next;
148 struct queue_entry *prev;
149};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700150
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700151#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700152
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700153static void *s_alloc(const char *f, size_t s)
154{
155 void *data = calloc(1, s);
156 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530157 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700158 exit(1);
159 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700160 return data;
161}
162
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700163static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700164{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700165 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700166
167 e->data = data;
168 e->next = e->prev = e;
169 return e;
170}
171
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700172static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700173{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700174 struct queue_entry *tmp = new_queue_entry(data);
175 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700176
177 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700178 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179 return;
180 }
181
182 q->prev->next = tmp;
183 tmp->prev = q->prev;
184 q->prev = tmp;
185 tmp->next = q;
186}
187
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700188static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700189{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700190 struct queue_entry *q = *q_head;
191 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700192 void *data;
193
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700194 if (!q)
195 return NULL;
196
197 tmp = q->prev;
198
Furquan Shaikh79e84122018-05-30 15:09:09 -0700199 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700200 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700201 else {
202 tmp->prev->next = q;
203 q->prev = tmp->prev;
204 }
205
206 data = tmp->data;
207 free(tmp);
208
209 return data;
210}
211
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700212static void *dequeue_head(struct queue_entry **q_head)
213{
214 struct queue_entry *q = *q_head;
215 struct queue_entry *tmp = q;
216 void *data;
217
218 if (!q)
219 return NULL;
220
221 if (q->next == q)
222 *q_head = NULL;
223 else {
224 q->next->prev = q->prev;
225 q->prev->next = q->next;
226 *q_head = q->next;
227 }
228
229 data = tmp->data;
230 free(tmp);
231
232 return data;
233}
234
235static void *peek_queue_head(struct queue_entry *q_head)
236{
237 if (!q_head)
238 return NULL;
239
240 return q_head->data;
241}
242
243static struct queue_entry *chip_q_head;
244
245void chip_enqueue_tail(void *data)
246{
247 enqueue_tail(&chip_q_head, data);
248}
249
250void *chip_dequeue_tail(void)
251{
252 return dequeue_tail(&chip_q_head);
253}
254
Martin Rothbec07532016-08-05 18:32:18 -0600255int yywrap(void)
256{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000257 return 1;
258}
259
Martin Rothbec07532016-08-05 18:32:18 -0600260void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000261{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000262 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600263 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000264 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000265}
266
Martin Rothbec07532016-08-05 18:32:18 -0600267char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200268{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200269 char *b, *c;
270 b = c = strdup(str);
271 while (c && *c) {
272 if ((mode == SPLIT_1ST) && (*c == '/')) {
273 *c = 0;
274 break;
275 }
Martin Rothbec07532016-08-05 18:32:18 -0600276 if (*c == '/')
277 *c = '_';
278 if (*c == '-')
279 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200280 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200281 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200282 if (mode == TO_LOWER)
283 *c = tolower(*c);
284 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200285 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200286 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200287}
288
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700289static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600290{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700291 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700292
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700293 while (h->next) {
294 int result = strcmp(path, h->next->name);
295 if (result == 0)
296 return h->next;
297
298 if (result < 0)
299 break;
300
301 h = h->next;
302 }
303
304 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
305 new_chip->next = h->next;
306 h->next = new_chip;
307
Patrick Georgi114e7b22010-05-05 11:19:50 +0000308 new_chip->chiph_exists = 1;
309 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700310 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000311
312 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700313 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700314 sprintf(chip_h, "src/%s", path);
315 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100316 /* root_complex gets away without a separate directory, but
317 * exists on on pretty much all AMD chipsets.
318 */
319 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300320 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
321 path);
322 exit(1);
323 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700324 }
325
Martin Roth824255e2016-08-05 17:40:39 -0600326 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200327
Martin Roth824255e2016-08-05 17:40:39 -0600328 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600329 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000330
Patrick Georgi1f688802014-08-03 15:51:19 +0200331 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700332
Patrick Georgi114e7b22010-05-05 11:19:50 +0000333 return new_chip;
334}
335
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700336struct chip_instance *new_chip_instance(char *path)
337{
338 struct chip *chip = get_chip(path);
339 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
340
341 instance->id = ++count;
342 instance->chip = chip;
343 instance->next = chip->instance;
344 chip->instance = instance;
345
346 return instance;
347}
348
Furquan Shaikh27efc502018-06-22 09:19:15 -0700349static void delete_chip_instance(struct chip_instance *ins)
350{
351
352 if (ins->ref_count == 0) {
353 printf("ERROR: ref count for chip instance is zero!!\n");
354 exit(1);
355 }
356
357 if (--ins->ref_count)
358 return;
359
360 struct chip *c = ins->chip;
361
362 /* Get pointer to first instance of the chip. */
363 struct chip_instance *i = c->instance;
364
365 /*
366 * If chip instance to be deleted is the first instance, then update
367 * instance pointer of the chip as well.
368 */
369 if (i == ins) {
370 c->instance = ins->next;
371 free(ins);
372 return;
373 }
374
375 /*
376 * Loop through the instances list of the chip to find and remove the
377 * given instance.
378 */
379 while (1) {
380 if (i == NULL) {
381 printf("ERROR: chip instance not found!\n");
382 exit(1);
383 }
384
385 if (i->next != ins) {
386 i = i->next;
387 continue;
388 }
389
390 i->next = ins->next;
391 break;
392 }
393
394 free(ins);
395}
396
Furquan Shaikh93198262018-06-03 04:22:17 -0700397/*
398 * Allocate a new bus for the provided device.
399 * - If this is the first bus being allocated under this device, then its id
400 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
401 * - If this is not the first bus under this device, then its id is set to 1
402 * plus the id of last bus and newly allocated bus is added to the list of
403 * buses under the device. last_bus is updated to point to the newly
404 * allocated bus.
405 */
406static void alloc_bus(struct device *dev)
407{
408 struct bus *bus = S_ALLOC(sizeof(*bus));
409
410 bus->dev = dev;
411
412 if (dev->last_bus == NULL) {
413 bus->id = 0;
414 dev->bus = bus;
415 } else {
416 bus->id = dev->last_bus->id + 1;
417 dev->last_bus->next_bus = bus;
418 }
419
420 dev->last_bus = bus;
421}
422
423/*
424 * Allocate a new device under the given parent. This function allocates a new
425 * device structure under the provided parent bus and allocates a bus structure
426 * under the newly allocated device.
427 */
428static struct device *alloc_dev(struct bus *parent)
429{
430 struct device *dev = S_ALLOC(sizeof(*dev));
431
432 dev->id = ++count;
433 dev->parent = parent;
434 dev->subsystem_vendor = -1;
435 dev->subsystem_device = -1;
436
437 alloc_bus(dev);
438
439 return dev;
440}
441
442/*
443 * This function scans the children of given bus to see if any device matches
444 * the new device that is requested.
445 *
446 * Returns pointer to the node if found, else NULL.
447 */
448static struct device *get_dev(struct bus *parent, int path_a, int path_b,
449 int bustype, struct chip_instance *chip_instance)
450{
451 struct device *child = parent->children;
452
453 while (child) {
454 if ((child->path_a == path_a) && (child->path_b == path_b) &&
455 (child->bustype == bustype) &&
456 (child->chip_instance == chip_instance))
457 return child;
458
459 child = child->sibling;
460 }
461
462 return NULL;
463}
464
Furquan Shaikh27efc502018-06-22 09:19:15 -0700465/*
466 * Add given node as child of the provided parent. If this is the first child of
467 * the parent, update parent->children pointer as well.
468 */
469static void set_new_child(struct bus *parent, struct device *child)
470{
471 struct device *c = parent->children;
472 if (c) {
473 while (c->sibling)
474 c = c->sibling;
475 c->sibling = child;
476 } else
477 parent->children = child;
478
479 child->sibling = NULL;
480 child->parent = parent;
481}
482
Furquan Shaikh93198262018-06-03 04:22:17 -0700483struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700484 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700485 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800486 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600487{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000488 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700489 int path_a;
490 int path_b = 0;
491 struct device *new_d;
492
493 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000494 if (*tmp == '.') {
495 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700496 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000497 }
498
Furquan Shaikh93198262018-06-03 04:22:17 -0700499 /* If device is found under parent, no need to allocate new device. */
500 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
501 if (new_d) {
502 alloc_bus(new_d);
503 return new_d;
504 }
505
506 new_d = alloc_dev(parent);
507
508 new_d->bustype = bustype;
509
510 new_d->path_a = path_a;
511 new_d->path_b = path_b;
512
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700513 char *name = S_ALLOC(10);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000514 sprintf(name, "_dev%d", new_d->id);
515 new_d->name = name;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700516
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800517 new_d->enabled = status & 0x01;
518 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000519 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700520 new_d->chip_instance = chip_instance;
Furquan Shaikh27efc502018-06-22 09:19:15 -0700521 chip_instance->ref_count++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000522
Furquan Shaikh27efc502018-06-22 09:19:15 -0700523 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000524
Furquan Shaikha9b64292018-05-31 07:52:00 -0700525 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200526 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000527 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200528 break;
529
530 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000531 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200532 break;
533
534 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700535 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200536 break;
537
538 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000539 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200540 break;
541
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800542 case CPU_CLUSTER:
543 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200544 break;
545
Aaron Durbinffda804b2014-09-03 12:40:15 -0500546 case CPU:
547 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
548 break;
549
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800550 case DOMAIN:
551 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200552 break;
553
554 case IOAPIC:
555 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
556 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700557
558 case GENERIC:
559 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
560 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800561
562 case SPI:
563 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
564 break;
565
Duncan Lauriebae9f852018-05-07 14:18:13 -0700566 case USB:
567 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
568 break;
569
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800570 case MMIO:
571 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
572 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000573 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800574
Patrick Georgi114e7b22010-05-05 11:19:50 +0000575 return new_d;
576}
577
Furquan Shaikh27efc502018-06-22 09:19:15 -0700578static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600579{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700580 struct resource *r = S_ALLOC(sizeof(struct resource));
581
Patrick Georgi114e7b22010-05-05 11:19:50 +0000582 r->type = type;
583 r->index = index;
584 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000585 if (dev->res) {
586 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600587 while (head->next)
588 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000589 head->next = r;
590 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000591 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000592 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000593}
594
Furquan Shaikh27efc502018-06-22 09:19:15 -0700595void add_resource(struct bus *bus, int type, int index, int base)
596{
597 new_resource(bus->dev, type, index, base);
598}
599
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700600void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600601{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700602 struct reg *r = S_ALLOC(sizeof(struct reg));
603
Patrick Georgi114e7b22010-05-05 11:19:50 +0000604 r->key = name;
605 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700606 if (chip_instance->reg) {
607 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000608 // sorting to be equal to sconfig's behaviour
609 int sort = strcmp(r->key, head->key);
610 if (sort == 0) {
611 printf("ERROR: duplicate 'register' key.\n");
612 exit(1);
613 }
Martin Rothbec07532016-08-05 18:32:18 -0600614 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000615 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700616 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000617 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600618 while ((head->next)
619 && (strcmp(head->next->key, r->key) < 0))
620 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000621 r->next = head->next;
622 head->next = r;
623 }
624 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700625 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000626 }
627}
628
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200629void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
630 char *data_width)
631{
632 struct device *dev = bus->dev;
633
634 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
635 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
636 exit(1);
637 }
638
639 dev->smbios_slot_type = type;
640 dev->smbios_slot_length = length;
641 dev->smbios_slot_data_width = data_width;
642 dev->smbios_slot_designation = designation;
643}
644
Furquan Shaikh93198262018-06-03 04:22:17 -0700645void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600646 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000647{
Furquan Shaikh93198262018-06-03 04:22:17 -0700648 struct device *dev = bus->dev;
649
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800650 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000651 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
652 exit(1);
653 }
654
655 dev->subsystem_vendor = vendor;
656 dev->subsystem_device = device;
657 dev->inherit_subsystem = inherit;
658}
659
Furquan Shaikh93198262018-06-03 04:22:17 -0700660void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600661 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200662{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200663 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700664 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200665
Martin Rothbec07532016-08-05 18:32:18 -0600666 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
667 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200668 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
669 exit(1);
670 }
671
672 srcpin = _srcpin[3] - 'A';
673
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800674 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200675 printf("ERROR: ioapic config only allowed for PCI devices\n");
676 exit(1);
677 }
678
679 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200680 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200681 exit(1);
682 }
683 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
684 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
685}
686
Furquan Shaikh93198262018-06-03 04:22:17 -0700687static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600688{
Furquan Shaikh93198262018-06-03 04:22:17 -0700689 struct bus *bus = dev->bus;
690
691 while (bus) {
692 if (bus->children)
693 return 1;
694 bus = bus->next_bus;
695 }
696
697 return 0;
698}
699
Nico Huber17e9bcb2019-09-20 12:05:51 +0200700static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700701{
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700702 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200703 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700704 ptr->name);
705 return;
706 }
707
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200708 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700709 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200710 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700711 ptr->name);
712 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200713 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600714 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700715
Furquan Shaikh93198262018-06-03 04:22:17 -0700716 if (next)
717 return;
718
719 fprintf(fil,
720 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
721 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000722}
723
Furquan Shaikh93198262018-06-03 04:22:17 -0700724static void emit_resources(FILE *fil, struct device *ptr)
725{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700726 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700727 return;
728
729 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200730 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700731 struct resource *r = ptr->res;
732 while (r) {
733 fprintf(fil,
734 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
735 if (r->type == IRQ)
736 fprintf(fil, "IRQ");
737 if (r->type == DRQ)
738 fprintf(fil, "DRQ");
739 if (r->type == IO)
740 fprintf(fil, "IO");
741 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
742 r->base);
743 if (r->next)
744 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
745 i++);
746 else
747 fprintf(fil, ".next=NULL },\n");
748 r = r->next;
749 }
750
751 fprintf(fil, "\t };\n");
752}
753
754static void emit_bus(FILE *fil, struct bus *bus)
755{
756 fprintf(fil, "\t\t[%d] = {\n", bus->id);
757 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
758 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
759 if (bus->children)
760 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
761
762 if (bus->next_bus)
763 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
764 bus->id + 1);
765 else
766 fprintf(fil, "\t\t\t.next = NULL,\n");
767 fprintf(fil, "\t\t},\n");
768}
769
770static void emit_dev_links(FILE *fil, struct device *ptr)
771{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200772 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700773 ptr->name);
774
775 struct bus *bus = ptr->bus;
776
777 while (bus) {
778 emit_bus(fil, bus);
779 bus = bus->next_bus;
780 }
781
782 fprintf(fil, "\t};\n");
783}
784
Nico Huber17e9bcb2019-09-20 12:05:51 +0200785static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200786{
787 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700788 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700789 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700790
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200791 if (ptr == &base_root_dev)
792 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
793 else
794 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
795
Furquan Shaikh93198262018-06-03 04:22:17 -0700796 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -0700797
798 /*
799 * ops field is set to default_dev_ops_root only for the root
800 * device. For all other devices, it is set by the driver at runtime.
801 */
802 if (ptr == &base_root_dev)
803 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
804 else
805 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700806 fprintf(fil, "#endif\n");
807 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
808 ptr->parent->id);
809 fprintf(fil, "\t.path = {");
810 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
811 fprintf(fil, "},\n");
812 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800813 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000814 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -0700815 fprintf(fil, "\t.on_mainboard = 1,\n");
816 if (ptr->subsystem_vendor > 0)
817 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
818 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000819
Furquan Shaikh93198262018-06-03 04:22:17 -0700820 if (ptr->subsystem_device > 0)
821 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
822 ptr->subsystem_device);
823
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700824 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700825 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600826 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700827 }
828 if (has_children)
829 fprintf(fil, "\t.link_list = &%s_links[0],\n",
830 ptr->name);
831 else
832 fprintf(fil, "\t.link_list = NULL,\n");
833 if (ptr->sibling)
834 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -0600835 else
836 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700837 fprintf(fil, "#if !DEVTREE_EARLY\n");
Kyösti Mälkki1557a672019-06-30 10:51:31 +0300838 for (pin = 0; pin < 4; pin++) {
839 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
840 fprintf(fil,
841 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
842 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
843
844 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
845 fprintf(fil,
846 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
847 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
848 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700849 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
850 chip_ins->chip->name_underscore);
851 if (chip_ins == &mainboard_instance)
852 fprintf(fil, "\t.name = mainboard_name,\n");
853 fprintf(fil, "#endif\n");
854 if (chip_ins->chip->chiph_exists)
855 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
856 chip_ins->chip->name_underscore, chip_ins->id);
857 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200858 fprintf(fil, "\t.next=&%s,\n", next->name);
859 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
860 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
861 fprintf(fil, "#if !DEVTREE_EARLY\n");
862 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
863 }
864 /* SMBIOS types start at 1, if zero it hasn't been set */
865 if (ptr->smbios_slot_type)
866 fprintf(fil, "\t.smbios_slot_type = %s,\n",
867 ptr->smbios_slot_type);
868 if (ptr->smbios_slot_data_width)
869 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
870 ptr->smbios_slot_data_width);
871 if (ptr->smbios_slot_designation)
872 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
873 ptr->smbios_slot_designation);
874 if (ptr->smbios_slot_length)
875 fprintf(fil, "\t.smbios_slot_length = %s,\n",
876 ptr->smbios_slot_length);
877 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
878 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
879 fprintf(fil, "#endif\n");
880 fprintf(fil, "#endif\n");
881 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700882 fprintf(fil, "};\n");
883
884 emit_resources(fil, ptr);
885
886 if (has_children)
887 emit_dev_links(fil, ptr);
888}
889
Nico Huber17e9bcb2019-09-20 12:05:51 +0200890static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200891{
892 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +0200893 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
894 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
895 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200896 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
897 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200898 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200899
Nico Huber17e9bcb2019-09-20 12:05:51 +0200900 if (ptr->bustype == PNP) {
901 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
902 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200903 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
904 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200905 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200906}
907
Furquan Shaikh93198262018-06-03 04:22:17 -0700908static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
909 struct device *d)
910{
911 while (d) {
912 enqueue_tail(bfs_q_head, d);
913 d = d->sibling;
914 }
915}
916
917static void add_children_to_queue(struct queue_entry **bfs_q_head,
918 struct device *d)
919{
920 struct bus *bus = d->bus;
921
922 while (bus) {
923 if (bus->children)
924 add_siblings_to_queue(bfs_q_head, bus->children);
925 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000926 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000927}
928
Nico Huber17e9bcb2019-09-20 12:05:51 +0200929static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
930 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -0700931 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600932{
Furquan Shaikh93198262018-06-03 04:22:17 -0700933 struct queue_entry *bfs_q_head = NULL;
934
935 enqueue_tail(&bfs_q_head, ptr);
936
937 while ((ptr = dequeue_head(&bfs_q_head))) {
938 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200939 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -0700940 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000941}
942
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700943static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700944{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700945 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700946
947 fprintf(fil, "#include <device/device.h>\n");
948 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700949
950 while (chip) {
951 if (chip->chiph_exists)
952 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
953 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700954 }
955 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
956 fprintf(fil,
957 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700958
959 chip = tmp;
960 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700961 fprintf(fil,
962 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700963 chip->name_underscore);
964 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700965 }
966 fprintf(fil, "#endif\n");
967}
968
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700969static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
970{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200971 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700972 instance->chip->name_underscore,
973 instance->chip->name_underscore,
974 instance->id);
975
976 if (instance->reg) {
977 fprintf(fil, "\n");
978 struct reg *r = instance->reg;
979 while (r) {
980 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
981 r = r->next;
982 }
983 }
984 fprintf(fil, "};\n\n");
985}
986
Furquan Shaikh79e84122018-05-30 15:09:09 -0700987static void emit_chips(FILE *fil)
988{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700989 struct chip *chip = chip_header.next;
990 struct chip_instance *instance;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700991
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700992 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700993
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200994 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
995
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700996 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700997 if (!chip->chiph_exists)
998 continue;
999
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001000 instance = chip->instance;
1001 while (instance) {
1002 emit_chip_instance(fil, instance);
1003 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001004 }
1005 }
1006}
1007
Nico Huber17e9bcb2019-09-20 12:05:51 +02001008static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001009 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001010{
1011 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001012
1013 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1014 /* user already gave us a subsystem vendor/device */
1015 return;
1016 }
1017
Furquan Shaikh93198262018-06-03 04:22:17 -07001018 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001019
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001020 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001021 continue;
1022
1023 if (p->inherit_subsystem) {
1024 dev->subsystem_vendor = p->subsystem_vendor;
1025 dev->subsystem_device = p->subsystem_device;
1026 break;
1027 }
1028 }
1029}
1030
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001031static void usage(void)
1032{
Nico Huber17e9bcb2019-09-20 12:05:51 +02001033 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -06001034 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001035}
1036
Martin Roth32051702015-11-24 12:34:16 -07001037enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -06001038 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001039 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +02001040 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001041 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -06001042};
Martin Roth32051702015-11-24 12:34:16 -07001043
Nico Huber17e9bcb2019-09-20 12:05:51 +02001044#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001045#define OPTIONAL_ARG_COUNT 1
1046#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001047
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001048static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001049{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001050 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001051 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001052 perror(NULL);
1053 exit(1);
1054 }
1055
Patrick Georgi114e7b22010-05-05 11:19:50 +00001056 yyrestart(filec);
1057
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001058 root_parent = parent;
1059 linenum = 0;
1060
Patrick Georgi114e7b22010-05-05 11:19:50 +00001061 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001062
Patrick Georgi114e7b22010-05-05 11:19:50 +00001063 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001064}
1065
Furquan Shaikh27efc502018-06-22 09:19:15 -07001066/*
1067 * Match device nodes from base and override tree to see if they are the same
1068 * node.
1069 */
1070static int device_match(struct device *a, struct device *b)
1071{
1072 return ((a->path_a == b->path_a) &&
1073 (a->path_b == b->path_b) &&
1074 (a->bustype == b->bustype) &&
1075 (a->chip_instance->chip ==
1076 b->chip_instance->chip));
1077}
1078
1079/*
Bill XIEc61d4152019-11-21 18:16:12 +08001080 * Match resource nodes from base and override tree to see if they are the same
1081 * node.
1082 */
1083static int res_match(struct resource *a, struct resource *b)
1084{
1085 return ((a->type == b->type) &&
1086 (a->index == b->index));
1087}
1088
1089/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001090 * Walk through the override subtree in breadth-first manner starting at node to
1091 * see if chip_instance pointer of the node is same as chip_instance pointer of
1092 * override parent that is passed into the function. If yes, then update the
1093 * chip_instance pointer of the node to chip_instance pointer of the base
1094 * parent.
1095 */
1096static void update_chip_pointers(struct device *node,
1097 struct chip_instance *base_parent_ci,
1098 struct chip_instance *override_parent_ci)
1099{
1100 struct queue_entry *q_head = NULL;
1101
1102 enqueue_tail(&q_head, node);
1103
1104 while ((node = dequeue_head(&q_head))) {
1105 if (node->chip_instance != override_parent_ci)
1106 continue;
1107 node->chip_instance = base_parent_ci;
1108 add_children_to_queue(&q_head, node);
1109 }
1110}
1111
1112/*
1113 * Add resource to device. If resource is already present, then update its base
1114 * and index. If not, then add a new resource to the device.
1115 */
1116static void update_resource(struct device *dev, struct resource *res)
1117{
1118 struct resource *base_res = dev->res;
1119
1120 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001121 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001122 base_res->base = res->base;
1123 return;
1124 }
1125 base_res = base_res->next;
1126 }
1127
1128 new_resource(dev, res->type, res->index, res->base);
1129}
1130
1131/*
1132 * Add register to chip instance. If register is already present, then update
1133 * its value. If not, then add a new register to the chip instance.
1134 */
1135static void update_register(struct chip_instance *c, struct reg *reg)
1136{
1137 struct reg *base_reg = c->reg;
1138
1139 while (base_reg) {
1140 if (!strcmp(base_reg->key, reg->key)) {
1141 base_reg->value = reg->value;
1142 return;
1143 }
1144 base_reg = base_reg->next;
1145 }
1146
1147 add_register(c, reg->key, reg->value);
1148}
1149
1150static void override_devicetree(struct bus *base_parent,
1151 struct bus *override_parent);
1152
1153/*
1154 * Update the base device properties using the properties of override device. In
1155 * addition to that, call override_devicetree for all the buses under the
1156 * override device.
1157 *
1158 * Override Rules:
1159 * +--------------------+--------------------------------------------+
1160 * | | |
1161 * |struct device member| Rule |
1162 * | | |
1163 * +-----------------------------------------------------------------+
1164 * | | |
1165 * | id | Unchanged. This is used to generate device |
1166 * | | structure name in static.c. So, no need to |
1167 * | | override. |
1168 * | | |
1169 * +-----------------------------------------------------------------+
1170 * | | |
1171 * | enabled | Copy enabled state from override device. |
1172 * | | This allows variants to override device |
1173 * | | state. |
1174 * | | |
1175 * +-----------------------------------------------------------------+
1176 * | | |
1177 * | subsystem_vendor | Copy from override device only if any one |
1178 * | subsystem_device | of the ids is non-zero. |
1179 * | | |
1180 * +-----------------------------------------------------------------+
1181 * | | |
1182 * | inherit_subsystem | Copy from override device only if it is |
1183 * | | non-zero. This allows variant to only |
1184 * | | enable inherit flag for a device. |
1185 * | | |
1186 * +-----------------------------------------------------------------+
1187 * | | |
1188 * | path | Unchanged since these are same for both |
1189 * | path_a | base and override device (Used for |
1190 * | path_b | matching devices). |
1191 * | | |
1192 * +-----------------------------------------------------------------+
1193 * | | |
1194 * | bustype | Unchanged since this is same for both base |
1195 * | | and override device (User for matching |
1196 * | | devices). |
1197 * | | |
1198 * +-----------------------------------------------------------------+
1199 * | | |
1200 * | pci_irq_info | Unchanged. |
1201 * | | |
1202 * +-----------------------------------------------------------------+
1203 * | | |
1204 * | parent | Unchanged. This is meaningful only within |
1205 * | sibling | the parse tree, hence not being copied. |
1206 * | | |
1207 * +-----------------------------------------------------------------+
1208 * | | |
1209 * | res | Each resource that is present in override |
1210 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001211 * | | 1. If resource of same type and index is |
1212 * | | present in base device, then base of |
1213 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001214 * | | 2. If not, then a new resource is allocated|
1215 * | | under the base device using type, index |
1216 * | | and base from override res. |
1217 * | | |
1218 * +-----------------------------------------------------------------+
1219 * | | |
1220 * | chip_instance | Each register of chip_instance is copied |
1221 * | | over from override device to base device: |
1222 * | | 1. If register with same key is present in |
1223 * | | base device, then value of the register |
1224 * | | is copied. |
1225 * | | 2. If not, then a new register is allocated|
1226 * | | under the base chip_instance using key |
1227 * | | and value from override register. |
1228 * | | |
1229 * +-----------------------------------------------------------------+
1230 * | | |
1231 * | bus | Recursively call override_devicetree on |
1232 * | last_bus | each bus of override device. It is assumed |
1233 * | | that bus with id X under base device |
1234 * | | to bus with id X under override device. If |
1235 * | | override device has more buses than base |
1236 * | | device, then new buses are allocated under |
1237 * | | base device. |
1238 * | | |
1239 * +-----------------------------------------------------------------+
1240 */
1241static void update_device(struct device *base_dev, struct device *override_dev)
1242{
1243 /*
1244 * Copy the enabled state of override device to base device. This allows
1245 * override tree to enable or disable a particular device.
1246 */
1247 base_dev->enabled = override_dev->enabled;
1248
1249 /*
1250 * Copy subsystem vendor and device ids from override device to base
1251 * device only if the ids are non-zero in override device. Else, honor
1252 * the values in base device.
1253 */
1254 if (override_dev->subsystem_vendor ||
1255 override_dev->subsystem_device) {
1256 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1257 base_dev->subsystem_device = override_dev->subsystem_device;
1258 }
1259
1260 /*
1261 * Copy value of inherity_subsystem from override device to base device
1262 * only if it is non-zero in override device. This allows override
1263 * tree to only enable inhert flag for a device.
1264 */
1265 if (override_dev->inherit_subsystem)
1266 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1267
1268 /*
1269 * Copy resources of override device to base device.
1270 * 1. If resource is already present in base device, then index and base
1271 * of the resource will be copied over.
1272 * 2. If resource is not already present in base device, a new resource
1273 * will be allocated.
1274 */
1275 struct resource *res = override_dev->res;
1276 while (res) {
1277 update_resource(base_dev, res);
1278 res = res->next;
1279 }
1280
1281 /*
1282 * Copy registers of override chip instance to base chip instance.
1283 * 1. If register key is already present in base chip instance, then
1284 * value for the register is copied over.
1285 * 2. If register key is not already present in base chip instance, then
1286 * a new register will be allocated.
1287 */
1288 struct reg *reg = override_dev->chip_instance->reg;
1289 while (reg) {
1290 update_register(base_dev->chip_instance, reg);
1291 reg = reg->next;
1292 }
1293
1294 /*
1295 * Now that the device properties are all copied over, look at each bus
1296 * of the override device and run override_devicetree in a recursive
1297 * manner. The assumption here is that first bus of override device
1298 * corresponds to first bus of base device and so on. If base device has
1299 * lesser buses than override tree, then new buses are allocated for it.
1300 */
1301 struct bus *override_bus = override_dev->bus;
1302 struct bus *base_bus = base_dev->bus;
1303
1304 while (override_bus) {
1305
1306 /*
1307 * If we have more buses in override tree device, then allocate
1308 * a new bus for the base tree device as well.
1309 */
1310 if (!base_bus) {
1311 alloc_bus(base_dev);
1312 base_bus = base_dev->last_bus;
1313 }
1314
1315 override_devicetree(base_dev->bus, override_dev->bus);
1316
1317 override_bus = override_bus->next_bus;
1318 base_bus = base_bus->next_bus;
1319 }
1320
1321 delete_chip_instance(override_dev->chip_instance);
1322 override_dev->chip_instance = NULL;
1323}
1324
1325/*
1326 * Perform copy of device and properties from override parent to base parent.
1327 * This function walks through the override tree in a depth-first manner
1328 * performing following actions:
1329 * 1. If matching device is found in base tree, then copy the properties of
1330 * override device to base tree device. Call override_devicetree recursively on
1331 * the bus of override device.
1332 * 2. If matching device is not found in base tree, then set override tree
1333 * device as new child of base_parent and update the chip pointers in override
1334 * device subtree to ensure the nodes do not point to override tree chip
1335 * instance.
1336 */
1337static void override_devicetree(struct bus *base_parent,
1338 struct bus *override_parent)
1339{
1340 struct device *base_child;
1341 struct device *override_child = override_parent->children;
1342 struct device *next_child;
1343
1344 while (override_child) {
1345
1346 /* Look for a matching device in base tree. */
1347 for (base_child = base_parent->children;
1348 base_child; base_child = base_child->sibling) {
1349 if (device_match(base_child, override_child))
1350 break;
1351 }
1352
1353 next_child = override_child->sibling;
1354
1355 /*
1356 * If matching device is found, copy properties of
1357 * override_child to base_child.
1358 */
1359 if (base_child)
1360 update_device(base_child, override_child);
1361 else {
1362 /*
1363 * If matching device is not found, set override_child
1364 * as a new child of base_parent.
1365 */
1366 set_new_child(base_parent, override_child);
1367 /*
1368 * Ensure all nodes in override tree pointing to
1369 * override parent chip_instance now point to base
1370 * parent chip_instance.
1371 */
1372 update_chip_pointers(override_child,
1373 base_parent->dev->chip_instance,
1374 override_parent->dev->chip_instance);
1375 }
1376
1377 override_child = next_child;
1378 }
1379}
1380
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001381int main(int argc, char **argv)
1382{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001383 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001384 usage();
1385
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001386 const char *base_devtree = argv[DEVICEFILE_ARG];
1387 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001388 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001389 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001390
1391 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001392
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001393 if (argc == TOTAL_ARG_COUNT) {
1394 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1395 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001396
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001397 if (!dev_has_children(&override_root_dev)) {
1398 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1399 exit(1);
1400 }
1401
Furquan Shaikh27efc502018-06-22 09:19:15 -07001402 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001403 }
1404
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001405 FILE *autogen = fopen(outputc, "w");
1406 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001407 fprintf(stderr, "Could not open file '%s' for writing: ",
1408 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001409 perror(NULL);
1410 exit(1);
1411 }
1412
Nico Huber17e9bcb2019-09-20 12:05:51 +02001413 FILE *autohead = fopen(outputh, "w");
1414 if (!autohead) {
1415 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1416 perror(NULL);
1417 fclose(autogen);
1418 exit(1);
1419 }
1420 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1421 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1422 fprintf(autohead, "#include <device/device.h>\n\n");
1423
Furquan Shaikh79e84122018-05-30 15:09:09 -07001424 emit_chips(autogen);
1425
Nico Huber17e9bcb2019-09-20 12:05:51 +02001426 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001427 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001428 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001429 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001430 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001431
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001432 /* Expose static devicenames to global namespace. */
1433 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001434 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001435
Nico Huber17e9bcb2019-09-20 12:05:51 +02001436 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1437 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001438 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001439
Patrick Georgi114e7b22010-05-05 11:19:50 +00001440 return 0;
1441}