blob: b0c32f677aff955e4c48c46dc97ed6c66ff2fca2 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* sconfig, coreboot device tree compiler */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi114e7b22010-05-05 11:19:50 +00003
Patrick Georgi2dbfcb72012-05-30 16:26:30 +02004#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +02005/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
6#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +02007#include <commonlib/helpers.h>
Duncan Laurie47b7b342020-05-15 15:39:08 -07008#include <stdint.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +00009#include "sconfig.h"
10#include "sconfig.tab.h"
11
Patrick Georgi7fc9e292010-07-15 15:59:07 +000012extern int linenum;
13
Furquan Shaikh27efc502018-06-22 09:19:15 -070014/*
15 * Maintains list of all the unique chip structures for the board.
16 * This is shared across base and override device trees since we need to
17 * generate headers for all chips added by both the trees.
18 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070019static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070020
Kyösti Mälkki472d9022011-12-05 20:33:55 +020021typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020022 UNSLASH,
23 SPLIT_1ST,
24 TO_LOWER,
25 TO_UPPER,
26} translate_t;
27
Furquan Shaikh93198262018-06-03 04:22:17 -070028/*
29 * Mainboard is assumed to have a root device whose bus is the parent of all the
30 * devices that are added by parsing the devicetree file. This device has a
31 * mainboard chip instance associated with it.
32 *
33 *
34 *
35 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070036 * | Root device | | Mainboard |
37 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070038 * | | | chip_instance | (mainboard_instance)|
39 * | +------------------------+ | |
40 * | | +----------------------+
41 * | | bus |
42 * | parent v |
43 * | +-------------------+ |
44 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070045 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070046 * | | |
47 * +-------------------+ |
48 * | |
49 * | children | chip
50 * v |
51 * X |
52 * (new devices will |
53 * be added here as |
54 * children) |
55 * |
56 * |
57 * |
58 * +-------+----------+
59 * | |
60 * | Mainboard chip +----------->X (new chips will be
61 * | (mainboard_chip) | added here)
62 * | |
63 * +------------------+
64 *
65 *
66 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070067
68/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070069static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070070
71/* Root device of override tree (if applicable). */
72static struct device override_root_dev;
73
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070074static struct chip_instance mainboard_instance;
75
Furquan Shaikhde39fc72018-06-11 04:26:45 -070076static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070077 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070078 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070079};
80
Furquan Shaikhde39fc72018-06-11 04:26:45 -070081static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070082 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070083 .chip_instance = &mainboard_instance,
84 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070085 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070086 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070087 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070088};
89
Furquan Shaikh39ac7972018-06-21 18:44:32 -070090static struct bus override_root_bus = {
91 .id = 0,
92 .dev = &override_root_dev,
93};
94
95static struct device override_root_dev = {
96 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -070097 /*
98 * Override tree root device points to the same mainboard chip instance
99 * as the base tree root device. It should not cause any side-effects
100 * since the mainboard chip instance pointer in override tree will just
101 * be ignored.
102 */
103 .chip_instance = &mainboard_instance,
104 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700105 .parent = &override_root_bus,
106 .enabled = 1,
107 .bus = &override_root_bus,
108};
109
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700110static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000111 .name = "mainboard",
112 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700113 .instance = &mainboard_instance,
114};
115
116static struct chip_instance mainboard_instance = {
117 .id = 0,
118 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000119};
120
Furquan Shaikh93198262018-06-03 04:22:17 -0700121/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700122struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000123
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700124struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700125 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700126 struct queue_entry *next;
127 struct queue_entry *prev;
128};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700129
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700130#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700131
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700132static void *s_alloc(const char *f, size_t s)
133{
134 void *data = calloc(1, s);
135 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530136 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700137 exit(1);
138 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700139 return data;
140}
141
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700142static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700143{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700144 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700145
146 e->data = data;
147 e->next = e->prev = e;
148 return e;
149}
150
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700151static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700152{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700153 struct queue_entry *tmp = new_queue_entry(data);
154 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700155
156 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700157 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700158 return;
159 }
160
161 q->prev->next = tmp;
162 tmp->prev = q->prev;
163 q->prev = tmp;
164 tmp->next = q;
165}
166
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700167static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700168{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700169 struct queue_entry *q = *q_head;
170 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700171 void *data;
172
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700173 if (!q)
174 return NULL;
175
176 tmp = q->prev;
177
Furquan Shaikh79e84122018-05-30 15:09:09 -0700178 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700179 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700180 else {
181 tmp->prev->next = q;
182 q->prev = tmp->prev;
183 }
184
185 data = tmp->data;
186 free(tmp);
187
188 return data;
189}
190
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700191static void *dequeue_head(struct queue_entry **q_head)
192{
193 struct queue_entry *q = *q_head;
194 struct queue_entry *tmp = q;
195 void *data;
196
197 if (!q)
198 return NULL;
199
200 if (q->next == q)
201 *q_head = NULL;
202 else {
203 q->next->prev = q->prev;
204 q->prev->next = q->next;
205 *q_head = q->next;
206 }
207
208 data = tmp->data;
209 free(tmp);
210
211 return data;
212}
213
214static void *peek_queue_head(struct queue_entry *q_head)
215{
216 if (!q_head)
217 return NULL;
218
219 return q_head->data;
220}
221
222static struct queue_entry *chip_q_head;
223
224void chip_enqueue_tail(void *data)
225{
226 enqueue_tail(&chip_q_head, data);
227}
228
229void *chip_dequeue_tail(void)
230{
231 return dequeue_tail(&chip_q_head);
232}
233
Martin Rothbec07532016-08-05 18:32:18 -0600234int yywrap(void)
235{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000236 return 1;
237}
238
Martin Rothbec07532016-08-05 18:32:18 -0600239void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000240{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000241 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600242 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000243 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000244}
245
Martin Rothbec07532016-08-05 18:32:18 -0600246char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200247{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200248 char *b, *c;
249 b = c = strdup(str);
250 while (c && *c) {
251 if ((mode == SPLIT_1ST) && (*c == '/')) {
252 *c = 0;
253 break;
254 }
Martin Rothbec07532016-08-05 18:32:18 -0600255 if (*c == '/')
256 *c = '_';
257 if (*c == '-')
258 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200259 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200260 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200261 if (mode == TO_LOWER)
262 *c = tolower(*c);
263 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200264 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200265 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200266}
267
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700268static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600269{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700270 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700271
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700272 while (h->next) {
273 int result = strcmp(path, h->next->name);
274 if (result == 0)
275 return h->next;
276
277 if (result < 0)
278 break;
279
280 h = h->next;
281 }
282
283 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
284 new_chip->next = h->next;
285 h->next = new_chip;
286
Patrick Georgi114e7b22010-05-05 11:19:50 +0000287 new_chip->chiph_exists = 1;
288 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700289 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000290
291 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700292 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700293 sprintf(chip_h, "src/%s", path);
294 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100295 /* root_complex gets away without a separate directory, but
296 * exists on on pretty much all AMD chipsets.
297 */
298 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300299 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
300 path);
301 exit(1);
302 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700303 }
304
Martin Roth824255e2016-08-05 17:40:39 -0600305 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200306
Martin Roth824255e2016-08-05 17:40:39 -0600307 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600308 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000309
Patrick Georgi1f688802014-08-03 15:51:19 +0200310 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700311
Patrick Georgi114e7b22010-05-05 11:19:50 +0000312 return new_chip;
313}
314
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700315struct chip_instance *new_chip_instance(char *path)
316{
317 struct chip *chip = get_chip(path);
318 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
319
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700320 instance->chip = chip;
321 instance->next = chip->instance;
322 chip->instance = instance;
323
324 return instance;
325}
326
Duncan Laurie47b7b342020-05-15 15:39:08 -0700327/* List of fw_config fields added during parsing. */
328static struct fw_config_field *fw_config_fields;
329
330static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
331 const char *name)
332{
333 struct fw_config_option *option = field->options;
334
335 while (option && option->name) {
336 if (!strcmp(option->name, name))
337 return option;
338 option = option->next;
339 }
340 return NULL;
341}
342
343static struct fw_config_field *find_fw_config_field(const char *name)
344{
345 struct fw_config_field *field = fw_config_fields;
346
347 while (field && field->name) {
348 if (!strcmp(field->name, name))
349 return field;
350 field = field->next;
351 }
352 return NULL;
353}
354
355struct fw_config_field *get_fw_config_field(const char *name)
356{
357 struct fw_config_field *field = find_fw_config_field(name);
358
359 /* Fail if the field does not exist, new fields must be added with a mask. */
360 if (!field) {
361 printf("ERROR: fw_config field not found: %s\n", name);
362 exit(1);
363 }
364 return field;
365}
366
367static void append_fw_config_field(struct fw_config_field *add)
368{
369 struct fw_config_field *field = fw_config_fields;
370
371 if (!fw_config_fields) {
372 fw_config_fields = add;
373 } else {
374 while (field && field->next)
375 field = field->next;
376 field->next = add;
377 }
378}
379
380struct fw_config_field *new_fw_config_field(const char *name,
381 unsigned int start_bit, unsigned int end_bit)
382{
383 struct fw_config_field *field = find_fw_config_field(name);
384
385 /* Check that field is within 32bits. */
386 if (start_bit > end_bit || end_bit > 31) {
387 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
388 start_bit, end_bit);
389 exit(1);
390 }
391
392 /* Don't allow re-defining a field, only adding new fields. */
393 if (field) {
394 printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n",
395 name, start_bit, end_bit, field->start_bit, field->end_bit);
396 exit(1);
397 }
398
399 /* Check for overlap with an existing field. */
400 field = fw_config_fields;
401 while (field) {
402 /* Check if the mask overlaps. */
403 if (start_bit <= field->end_bit && end_bit >= field->start_bit) {
404 printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n",
405 name, start_bit, end_bit,
406 field->name, field->start_bit, field->end_bit);
407 exit(1);
408 }
409 field = field->next;
410 }
411
412 field = S_ALLOC(sizeof(*field));
413 field->name = name;
414 field->start_bit = start_bit;
415 field->end_bit = end_bit;
416 append_fw_config_field(field);
417
418 return field;
419}
420
421static void append_fw_config_option_to_field(struct fw_config_field *field,
422 struct fw_config_option *add)
423{
424 struct fw_config_option *option = field->options;
425
426 if (!option) {
427 field->options = add;
428 } else {
429 while (option && option->next)
430 option = option->next;
431 option->next = add;
432 }
433}
434
435void add_fw_config_option(struct fw_config_field *field, const char *name, unsigned int value)
436{
437 struct fw_config_option *option;
438 uint32_t field_max_value;
439
440 /* Check that option value fits within field mask. */
441 field_max_value = (1 << (1 + field->end_bit - field->start_bit)) - 1;
442 if (value > field_max_value) {
443 printf("ERROR: fw_config option %s:%s value %u larger than field max %u\n",
444 field->name, name, value, field_max_value);
445 exit(1);
446 }
447
448 /* Check for existing option with this name or value. */
449 option = field->options;
450 while (option) {
451 if (!strcmp(option->name, name)) {
452 printf("ERROR: fw_config option name %s:%s already exists\n",
453 field->name, name);
454 exit(1);
455 }
456 /* Compare values. */
457 if (value == option->value) {
458 printf("ERROR: fw_config option %s:%s[%u] redefined as %s\n",
459 field->name, option->name, value, name);
460 exit(1);
461 }
462 option = option->next;
463 }
464
465 option = S_ALLOC(sizeof(*option));
466 option->name = name;
467 option->value = value;
468
469 /* Add option to the current field. */
470 append_fw_config_option_to_field(field, option);
471}
472
473static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
474{
475 struct fw_config_probe *probe = dev->probe;
476
477 if (!probe) {
478 dev->probe = add;
479 } else {
480 while (probe && probe->next)
481 probe = probe->next;
482 probe->next = add;
483 }
484}
485
486void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
487{
488 struct fw_config_probe *probe;
489
490 probe = bus->dev->probe;
491 while (probe) {
492 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
493 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
494 exit(1);
495 }
496 probe = probe->next;
497 }
498
499 probe = S_ALLOC(sizeof(*probe));
500 probe->field = field;
501 probe->option = option;
502
503 append_fw_config_probe_to_dev(bus->dev, probe);
504}
505
506static void emit_fw_config(FILE *fil)
507{
508 struct fw_config_field *field = fw_config_fields;
509
510 if (!field)
511 return;
512
513 fprintf(fil, "\n/* firmware configuration */\n");
514 fprintf(fil, "#include <fw_config.h>\n");
515
516 while (field) {
517 struct fw_config_option *option = field->options;
518 uint32_t mask;
519
520 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
521 field->name, field->name);
522
523 /* Compute mask from start and end bit. */
524 mask = ((1 << (1 + field->end_bit - field->start_bit)) - 1);
525 mask <<= field->start_bit;
526
527 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%08x\n",
528 field->name, mask);
529
530 while (option) {
531 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
532 field->name, option->name, option->name);
533 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%08x\n",
534 field->name, option->name, option->value << field->start_bit);
535
536 option = option->next;
537 }
538
539 field = field->next;
540 }
541
542 fprintf(fil, "\n");
543}
544
545static int emit_fw_config_probe(FILE *fil, struct device *dev)
546{
547 struct fw_config_probe *probe = dev->probe;
548
549 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
550
551 while (probe) {
552 /* Find matching field. */
553 struct fw_config_field *field;
554 struct fw_config_option *option;
555 uint32_t mask, value;
556
557 field = find_fw_config_field(probe->field);
558 if (!field) {
559 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
560 return -1;
561 }
562 option = find_fw_config_option(field, probe->option);
563 if (!option) {
564 printf("ERROR: fw_config_probe field %s option %s not found\n",
565 probe->field, probe->option);
566 return -1;
567 }
568
569 /* Fill out the probe structure with values from emit_fw_config(). */
570 fprintf(fil, "\t{\n");
571 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
572 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
573 probe->field, probe->option);
574 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
575 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
576 probe->field, probe->option);
577 fprintf(fil, "\t},\n");
578
579 probe = probe->next;
580 }
581
582 /* Add empty entry to mark end of list. */
583 fprintf(fil, "\t{ }\n};\n");
584 return 0;
585}
586
Furquan Shaikh93198262018-06-03 04:22:17 -0700587/*
588 * Allocate a new bus for the provided device.
589 * - If this is the first bus being allocated under this device, then its id
590 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
591 * - If this is not the first bus under this device, then its id is set to 1
592 * plus the id of last bus and newly allocated bus is added to the list of
593 * buses under the device. last_bus is updated to point to the newly
594 * allocated bus.
595 */
596static void alloc_bus(struct device *dev)
597{
598 struct bus *bus = S_ALLOC(sizeof(*bus));
599
600 bus->dev = dev;
601
602 if (dev->last_bus == NULL) {
603 bus->id = 0;
604 dev->bus = bus;
605 } else {
606 bus->id = dev->last_bus->id + 1;
607 dev->last_bus->next_bus = bus;
608 }
609
610 dev->last_bus = bus;
611}
612
613/*
614 * Allocate a new device under the given parent. This function allocates a new
615 * device structure under the provided parent bus and allocates a bus structure
616 * under the newly allocated device.
617 */
618static struct device *alloc_dev(struct bus *parent)
619{
620 struct device *dev = S_ALLOC(sizeof(*dev));
621
Furquan Shaikh93198262018-06-03 04:22:17 -0700622 dev->parent = parent;
623 dev->subsystem_vendor = -1;
624 dev->subsystem_device = -1;
625
626 alloc_bus(dev);
627
628 return dev;
629}
630
631/*
632 * This function scans the children of given bus to see if any device matches
633 * the new device that is requested.
634 *
635 * Returns pointer to the node if found, else NULL.
636 */
637static struct device *get_dev(struct bus *parent, int path_a, int path_b,
638 int bustype, struct chip_instance *chip_instance)
639{
640 struct device *child = parent->children;
641
642 while (child) {
643 if ((child->path_a == path_a) && (child->path_b == path_b) &&
644 (child->bustype == bustype) &&
645 (child->chip_instance == chip_instance))
646 return child;
647
648 child = child->sibling;
649 }
650
651 return NULL;
652}
653
Furquan Shaikh27efc502018-06-22 09:19:15 -0700654/*
655 * Add given node as child of the provided parent. If this is the first child of
656 * the parent, update parent->children pointer as well.
657 */
658static void set_new_child(struct bus *parent, struct device *child)
659{
660 struct device *c = parent->children;
661 if (c) {
662 while (c->sibling)
663 c = c->sibling;
664 c->sibling = child;
665 } else
666 parent->children = child;
667
668 child->sibling = NULL;
669 child->parent = parent;
670}
671
Furquan Shaikh93198262018-06-03 04:22:17 -0700672struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700673 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700674 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800675 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600676{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000677 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700678 int path_a;
679 int path_b = 0;
680 struct device *new_d;
681
682 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000683 if (*tmp == '.') {
684 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700685 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000686 }
687
Furquan Shaikh93198262018-06-03 04:22:17 -0700688 /* If device is found under parent, no need to allocate new device. */
689 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
690 if (new_d) {
691 alloc_bus(new_d);
692 return new_d;
693 }
694
695 new_d = alloc_dev(parent);
696
697 new_d->bustype = bustype;
698
699 new_d->path_a = path_a;
700 new_d->path_b = path_b;
701
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800702 new_d->enabled = status & 0x01;
703 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000704 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700705 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000706
Furquan Shaikh27efc502018-06-22 09:19:15 -0700707 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000708
Furquan Shaikha9b64292018-05-31 07:52:00 -0700709 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200710 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000711 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200712 break;
713
714 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000715 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200716 break;
717
718 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700719 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200720 break;
721
722 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000723 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200724 break;
725
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800726 case CPU_CLUSTER:
727 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200728 break;
729
Aaron Durbinffda804b2014-09-03 12:40:15 -0500730 case CPU:
731 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
732 break;
733
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800734 case DOMAIN:
735 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200736 break;
737
738 case IOAPIC:
739 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
740 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700741
742 case GENERIC:
743 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
744 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800745
746 case SPI:
747 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
748 break;
749
Duncan Lauriebae9f852018-05-07 14:18:13 -0700750 case USB:
751 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
752 break;
753
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800754 case MMIO:
755 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
756 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600757
758 case ESPI:
759 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
760 break;
761
762 case LPC:
763 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
764 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000765 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800766
Patrick Georgi114e7b22010-05-05 11:19:50 +0000767 return new_d;
768}
769
Furquan Shaikh27efc502018-06-22 09:19:15 -0700770static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600771{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700772 struct resource *r = S_ALLOC(sizeof(struct resource));
773
Patrick Georgi114e7b22010-05-05 11:19:50 +0000774 r->type = type;
775 r->index = index;
776 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000777 if (dev->res) {
778 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600779 while (head->next)
780 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000781 head->next = r;
782 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000783 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000784 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000785}
786
Furquan Shaikh27efc502018-06-22 09:19:15 -0700787void add_resource(struct bus *bus, int type, int index, int base)
788{
789 new_resource(bus->dev, type, index, base);
790}
791
Nico Huberd09459f2020-05-26 22:13:09 +0200792static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600793{
Nico Huberd09459f2020-05-26 22:13:09 +0200794 struct reg *const r = S_ALLOC(sizeof(struct reg));
795 struct reg *prev = NULL;
796 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700797
Patrick Georgi114e7b22010-05-05 11:19:50 +0000798 r->key = name;
799 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200800
801 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
802 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000803 if (sort == 0) {
804 printf("ERROR: duplicate 'register' key.\n");
805 exit(1);
806 }
Nico Huberd09459f2020-05-26 22:13:09 +0200807 if (sort < 0)
808 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000809 }
Nico Huberd09459f2020-05-26 22:13:09 +0200810 r->next = cur;
811 if (prev)
812 prev->next = r;
813 else
814 *head = r;
815}
816
817void add_register(struct chip_instance *chip_instance, char *name, char *val)
818{
819 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000820}
821
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200822void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
823 char *data_width)
824{
825 struct device *dev = bus->dev;
826
827 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
828 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
829 exit(1);
830 }
831
832 dev->smbios_slot_type = type;
833 dev->smbios_slot_length = length;
834 dev->smbios_slot_data_width = data_width;
835 dev->smbios_slot_designation = designation;
836}
837
Furquan Shaikh93198262018-06-03 04:22:17 -0700838void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600839 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000840{
Furquan Shaikh93198262018-06-03 04:22:17 -0700841 struct device *dev = bus->dev;
842
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800843 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000844 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
845 exit(1);
846 }
847
848 dev->subsystem_vendor = vendor;
849 dev->subsystem_device = device;
850 dev->inherit_subsystem = inherit;
851}
852
Furquan Shaikh93198262018-06-03 04:22:17 -0700853void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600854 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200855{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200856 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700857 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200858
Martin Rothbec07532016-08-05 18:32:18 -0600859 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
860 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200861 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
862 exit(1);
863 }
864
865 srcpin = _srcpin[3] - 'A';
866
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800867 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200868 printf("ERROR: ioapic config only allowed for PCI devices\n");
869 exit(1);
870 }
871
872 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200873 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200874 exit(1);
875 }
876 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
877 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
878}
879
Furquan Shaikh93198262018-06-03 04:22:17 -0700880static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600881{
Furquan Shaikh93198262018-06-03 04:22:17 -0700882 struct bus *bus = dev->bus;
883
884 while (bus) {
885 if (bus->children)
886 return 1;
887 bus = bus->next_bus;
888 }
889
890 return 0;
891}
892
Nico Huber17e9bcb2019-09-20 12:05:51 +0200893static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700894{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700895 static int dev_id;
896
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700897 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200898 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700899 ptr->name);
900 return;
901 }
902
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700903 char *name = S_ALLOC(10);
904 sprintf(name, "_dev%d", dev_id++);
905 ptr->name = name;
906
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200907 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700908 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200909 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700910 ptr->name);
911 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200912 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600913 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700914
Furquan Shaikh93198262018-06-03 04:22:17 -0700915 if (next)
916 return;
917
918 fprintf(fil,
919 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
920 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000921}
922
Furquan Shaikh93198262018-06-03 04:22:17 -0700923static void emit_resources(FILE *fil, struct device *ptr)
924{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700925 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700926 return;
927
928 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200929 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700930 struct resource *r = ptr->res;
931 while (r) {
932 fprintf(fil,
933 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
934 if (r->type == IRQ)
935 fprintf(fil, "IRQ");
936 if (r->type == DRQ)
937 fprintf(fil, "DRQ");
938 if (r->type == IO)
939 fprintf(fil, "IO");
940 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
941 r->base);
942 if (r->next)
943 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
944 i++);
945 else
946 fprintf(fil, ".next=NULL },\n");
947 r = r->next;
948 }
949
950 fprintf(fil, "\t };\n");
951}
952
953static void emit_bus(FILE *fil, struct bus *bus)
954{
955 fprintf(fil, "\t\t[%d] = {\n", bus->id);
956 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
957 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
958 if (bus->children)
959 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
960
961 if (bus->next_bus)
962 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
963 bus->id + 1);
964 else
965 fprintf(fil, "\t\t\t.next = NULL,\n");
966 fprintf(fil, "\t\t},\n");
967}
968
969static void emit_dev_links(FILE *fil, struct device *ptr)
970{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200971 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700972 ptr->name);
973
974 struct bus *bus = ptr->bus;
975
976 while (bus) {
977 emit_bus(fil, bus);
978 bus = bus->next_bus;
979 }
980
981 fprintf(fil, "\t};\n");
982}
983
Nico Huber17e9bcb2019-09-20 12:05:51 +0200984static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200985{
986 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700987 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700988 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700989
Furquan Shaikhbbade242020-05-02 16:05:29 -0700990 /*
991 * If the chip instance of device has base_chip_instance pointer set, then follow that
992 * to update the chip instance for current device.
993 */
994 if (chip_ins->base_chip_instance)
995 chip_ins = chip_ins->base_chip_instance;
996
Duncan Laurie47b7b342020-05-15 15:39:08 -0700997 /* Emit probe structures. */
998 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
999 fclose(head);
1000 fclose(fil);
1001 exit(1);
1002 }
1003
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001004 if (ptr == &base_root_dev)
1005 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1006 else
1007 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1008
Furquan Shaikh93198262018-06-03 04:22:17 -07001009 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001010
1011 /*
1012 * ops field is set to default_dev_ops_root only for the root
1013 * device. For all other devices, it is set by the driver at runtime.
1014 */
1015 if (ptr == &base_root_dev)
1016 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1017 else
1018 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001019 fprintf(fil, "#endif\n");
1020 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1021 ptr->parent->id);
1022 fprintf(fil, "\t.path = {");
1023 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1024 fprintf(fil, "},\n");
1025 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001026 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001027 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001028 fprintf(fil, "\t.on_mainboard = 1,\n");
1029 if (ptr->subsystem_vendor > 0)
1030 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1031 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001032
Furquan Shaikh93198262018-06-03 04:22:17 -07001033 if (ptr->subsystem_device > 0)
1034 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1035 ptr->subsystem_device);
1036
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001037 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001038 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001039 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001040 }
1041 if (has_children)
1042 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1043 ptr->name);
1044 else
1045 fprintf(fil, "\t.link_list = NULL,\n");
1046 if (ptr->sibling)
1047 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001048 else
1049 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001050 fprintf(fil, "#if !DEVTREE_EARLY\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001051 if (ptr->probe)
1052 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Kyösti Mälkki1557a672019-06-30 10:51:31 +03001053 for (pin = 0; pin < 4; pin++) {
1054 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
1055 fprintf(fil,
1056 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
1057 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
1058
1059 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
1060 fprintf(fil,
1061 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
1062 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
1063 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001064 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1065 chip_ins->chip->name_underscore);
1066 if (chip_ins == &mainboard_instance)
1067 fprintf(fil, "\t.name = mainboard_name,\n");
1068 fprintf(fil, "#endif\n");
1069 if (chip_ins->chip->chiph_exists)
1070 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1071 chip_ins->chip->name_underscore, chip_ins->id);
1072 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001073 fprintf(fil, "\t.next=&%s,\n", next->name);
1074 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1075 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1076 fprintf(fil, "#if !DEVTREE_EARLY\n");
1077 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1078 }
1079 /* SMBIOS types start at 1, if zero it hasn't been set */
1080 if (ptr->smbios_slot_type)
1081 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1082 ptr->smbios_slot_type);
1083 if (ptr->smbios_slot_data_width)
1084 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1085 ptr->smbios_slot_data_width);
1086 if (ptr->smbios_slot_designation)
1087 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1088 ptr->smbios_slot_designation);
1089 if (ptr->smbios_slot_length)
1090 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1091 ptr->smbios_slot_length);
1092 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1093 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1094 fprintf(fil, "#endif\n");
1095 fprintf(fil, "#endif\n");
1096 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001097 fprintf(fil, "};\n");
1098
1099 emit_resources(fil, ptr);
1100
1101 if (has_children)
1102 emit_dev_links(fil, ptr);
1103}
1104
Nico Huber17e9bcb2019-09-20 12:05:51 +02001105static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001106{
1107 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001108 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
1109 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
1110 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001111 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
1112 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001113 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001114
Nico Huber17e9bcb2019-09-20 12:05:51 +02001115 if (ptr->bustype == PNP) {
1116 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
1117 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001118 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
1119 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001120 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001121}
1122
Furquan Shaikh93198262018-06-03 04:22:17 -07001123static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1124 struct device *d)
1125{
1126 while (d) {
1127 enqueue_tail(bfs_q_head, d);
1128 d = d->sibling;
1129 }
1130}
1131
1132static void add_children_to_queue(struct queue_entry **bfs_q_head,
1133 struct device *d)
1134{
1135 struct bus *bus = d->bus;
1136
1137 while (bus) {
1138 if (bus->children)
1139 add_siblings_to_queue(bfs_q_head, bus->children);
1140 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001141 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001142}
1143
Nico Huber17e9bcb2019-09-20 12:05:51 +02001144static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1145 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001146 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001147{
Furquan Shaikh93198262018-06-03 04:22:17 -07001148 struct queue_entry *bfs_q_head = NULL;
1149
1150 enqueue_tail(&bfs_q_head, ptr);
1151
1152 while ((ptr = dequeue_head(&bfs_q_head))) {
1153 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001154 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001155 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001156}
1157
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001158static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001159{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001160 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001161
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001162 while (chip) {
1163 if (chip->chiph_exists)
1164 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1165 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001166 }
1167 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1168 fprintf(fil,
1169 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001170
1171 chip = tmp;
1172 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001173 /* A lot of cpus do not define chip_operations at all, and the ones
1174 that do only initialise .name. */
1175 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1176 fprintf(fil,
1177 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1178 chip->name_underscore);
1179 } else {
1180 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1181 chip->name_underscore);
1182 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001183 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001184 }
1185 fprintf(fil, "#endif\n");
1186}
1187
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001188static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1189{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001190 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001191 instance->chip->name_underscore,
1192 instance->chip->name_underscore,
1193 instance->id);
1194
1195 if (instance->reg) {
1196 fprintf(fil, "\n");
1197 struct reg *r = instance->reg;
1198 while (r) {
1199 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1200 r = r->next;
1201 }
1202 }
1203 fprintf(fil, "};\n\n");
1204}
1205
Furquan Shaikh79e84122018-05-30 15:09:09 -07001206static void emit_chips(FILE *fil)
1207{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001208 struct chip *chip = chip_header.next;
1209 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001210 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001211
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001212 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001213
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001214 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
1215
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001216 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001217 if (!chip->chiph_exists)
1218 continue;
1219
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001220 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001221 instance = chip->instance;
1222 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001223 /*
1224 * Emit this chip instance only if there is no forwarding pointer to the
1225 * base tree chip instance.
1226 */
1227 if (instance->base_chip_instance == NULL) {
1228 instance->id = chip_id++;
1229 emit_chip_instance(fil, instance);
1230 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001231 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001232 }
1233 }
1234}
1235
Nico Huber17e9bcb2019-09-20 12:05:51 +02001236static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001237 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001238{
1239 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001240
1241 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1242 /* user already gave us a subsystem vendor/device */
1243 return;
1244 }
1245
Furquan Shaikh93198262018-06-03 04:22:17 -07001246 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001247
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001248 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001249 continue;
1250
1251 if (p->inherit_subsystem) {
1252 dev->subsystem_vendor = p->subsystem_vendor;
1253 dev->subsystem_device = p->subsystem_device;
1254 break;
1255 }
1256 }
1257}
1258
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001259static void usage(void)
1260{
Nico Huber17e9bcb2019-09-20 12:05:51 +02001261 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -06001262 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001263}
1264
Martin Roth32051702015-11-24 12:34:16 -07001265enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -06001266 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001267 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +02001268 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001269 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -06001270};
Martin Roth32051702015-11-24 12:34:16 -07001271
Nico Huber17e9bcb2019-09-20 12:05:51 +02001272#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001273#define OPTIONAL_ARG_COUNT 1
1274#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001275
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001276static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001277{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001278 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001279 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001280 perror(NULL);
1281 exit(1);
1282 }
1283
Patrick Georgi114e7b22010-05-05 11:19:50 +00001284 yyrestart(filec);
1285
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001286 root_parent = parent;
1287 linenum = 0;
1288
Patrick Georgi114e7b22010-05-05 11:19:50 +00001289 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001290
Patrick Georgi114e7b22010-05-05 11:19:50 +00001291 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001292}
1293
Furquan Shaikh27efc502018-06-22 09:19:15 -07001294/*
1295 * Match device nodes from base and override tree to see if they are the same
1296 * node.
1297 */
1298static int device_match(struct device *a, struct device *b)
1299{
1300 return ((a->path_a == b->path_a) &&
1301 (a->path_b == b->path_b) &&
1302 (a->bustype == b->bustype) &&
1303 (a->chip_instance->chip ==
1304 b->chip_instance->chip));
1305}
1306
1307/*
Bill XIEc61d4152019-11-21 18:16:12 +08001308 * Match resource nodes from base and override tree to see if they are the same
1309 * node.
1310 */
1311static int res_match(struct resource *a, struct resource *b)
1312{
1313 return ((a->type == b->type) &&
1314 (a->index == b->index));
1315}
1316
1317/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001318 * Add resource to device. If resource is already present, then update its base
1319 * and index. If not, then add a new resource to the device.
1320 */
1321static void update_resource(struct device *dev, struct resource *res)
1322{
1323 struct resource *base_res = dev->res;
1324
1325 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001326 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001327 base_res->base = res->base;
1328 return;
1329 }
1330 base_res = base_res->next;
1331 }
1332
1333 new_resource(dev, res->type, res->index, res->base);
1334}
1335
1336/*
1337 * Add register to chip instance. If register is already present, then update
1338 * its value. If not, then add a new register to the chip instance.
1339 */
1340static void update_register(struct chip_instance *c, struct reg *reg)
1341{
1342 struct reg *base_reg = c->reg;
1343
1344 while (base_reg) {
1345 if (!strcmp(base_reg->key, reg->key)) {
1346 base_reg->value = reg->value;
1347 return;
1348 }
1349 base_reg = base_reg->next;
1350 }
1351
1352 add_register(c, reg->key, reg->value);
1353}
1354
1355static void override_devicetree(struct bus *base_parent,
1356 struct bus *override_parent);
1357
1358/*
1359 * Update the base device properties using the properties of override device. In
1360 * addition to that, call override_devicetree for all the buses under the
1361 * override device.
1362 *
1363 * Override Rules:
1364 * +--------------------+--------------------------------------------+
1365 * | | |
1366 * |struct device member| Rule |
1367 * | | |
1368 * +-----------------------------------------------------------------+
1369 * | | |
1370 * | id | Unchanged. This is used to generate device |
1371 * | | structure name in static.c. So, no need to |
1372 * | | override. |
1373 * | | |
1374 * +-----------------------------------------------------------------+
1375 * | | |
1376 * | enabled | Copy enabled state from override device. |
1377 * | | This allows variants to override device |
1378 * | | state. |
1379 * | | |
1380 * +-----------------------------------------------------------------+
1381 * | | |
1382 * | subsystem_vendor | Copy from override device only if any one |
1383 * | subsystem_device | of the ids is non-zero. |
1384 * | | |
1385 * +-----------------------------------------------------------------+
1386 * | | |
1387 * | inherit_subsystem | Copy from override device only if it is |
1388 * | | non-zero. This allows variant to only |
1389 * | | enable inherit flag for a device. |
1390 * | | |
1391 * +-----------------------------------------------------------------+
1392 * | | |
1393 * | path | Unchanged since these are same for both |
1394 * | path_a | base and override device (Used for |
1395 * | path_b | matching devices). |
1396 * | | |
1397 * +-----------------------------------------------------------------+
1398 * | | |
1399 * | bustype | Unchanged since this is same for both base |
1400 * | | and override device (User for matching |
1401 * | | devices). |
1402 * | | |
1403 * +-----------------------------------------------------------------+
1404 * | | |
1405 * | pci_irq_info | Unchanged. |
1406 * | | |
1407 * +-----------------------------------------------------------------+
1408 * | | |
1409 * | parent | Unchanged. This is meaningful only within |
1410 * | sibling | the parse tree, hence not being copied. |
1411 * | | |
1412 * +-----------------------------------------------------------------+
1413 * | | |
1414 * | res | Each resource that is present in override |
1415 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001416 * | | 1. If resource of same type and index is |
1417 * | | present in base device, then base of |
1418 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001419 * | | 2. If not, then a new resource is allocated|
1420 * | | under the base device using type, index |
1421 * | | and base from override res. |
1422 * | | |
1423 * +-----------------------------------------------------------------+
1424 * | | |
1425 * | chip_instance | Each register of chip_instance is copied |
1426 * | | over from override device to base device: |
1427 * | | 1. If register with same key is present in |
1428 * | | base device, then value of the register |
1429 * | | is copied. |
1430 * | | 2. If not, then a new register is allocated|
1431 * | | under the base chip_instance using key |
1432 * | | and value from override register. |
1433 * | | |
1434 * +-----------------------------------------------------------------+
1435 * | | |
1436 * | bus | Recursively call override_devicetree on |
1437 * | last_bus | each bus of override device. It is assumed |
1438 * | | that bus with id X under base device |
1439 * | | to bus with id X under override device. If |
1440 * | | override device has more buses than base |
1441 * | | device, then new buses are allocated under |
1442 * | | base device. |
1443 * | | |
1444 * +-----------------------------------------------------------------+
1445 */
1446static void update_device(struct device *base_dev, struct device *override_dev)
1447{
1448 /*
1449 * Copy the enabled state of override device to base device. This allows
1450 * override tree to enable or disable a particular device.
1451 */
1452 base_dev->enabled = override_dev->enabled;
1453
1454 /*
1455 * Copy subsystem vendor and device ids from override device to base
1456 * device only if the ids are non-zero in override device. Else, honor
1457 * the values in base device.
1458 */
1459 if (override_dev->subsystem_vendor ||
1460 override_dev->subsystem_device) {
1461 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1462 base_dev->subsystem_device = override_dev->subsystem_device;
1463 }
1464
1465 /*
1466 * Copy value of inherity_subsystem from override device to base device
1467 * only if it is non-zero in override device. This allows override
1468 * tree to only enable inhert flag for a device.
1469 */
1470 if (override_dev->inherit_subsystem)
1471 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1472
1473 /*
1474 * Copy resources of override device to base device.
1475 * 1. If resource is already present in base device, then index and base
1476 * of the resource will be copied over.
1477 * 2. If resource is not already present in base device, a new resource
1478 * will be allocated.
1479 */
1480 struct resource *res = override_dev->res;
1481 while (res) {
1482 update_resource(base_dev, res);
1483 res = res->next;
1484 }
1485
1486 /*
1487 * Copy registers of override chip instance to base chip instance.
1488 * 1. If register key is already present in base chip instance, then
1489 * value for the register is copied over.
1490 * 2. If register key is not already present in base chip instance, then
1491 * a new register will be allocated.
1492 */
1493 struct reg *reg = override_dev->chip_instance->reg;
1494 while (reg) {
1495 update_register(base_dev->chip_instance, reg);
1496 reg = reg->next;
1497 }
1498
1499 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001500 * Use probe list from override device in place of base device, in order
1501 * to allow an override to remove a probe from the base device.
1502 */
1503 base_dev->probe = override_dev->probe;
1504
1505 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001506 * Update base_chip_instance member in chip instance of override tree to forward it to
1507 * the chip instance in base tree.
1508 */
1509 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1510
1511 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001512 * Now that the device properties are all copied over, look at each bus
1513 * of the override device and run override_devicetree in a recursive
1514 * manner. The assumption here is that first bus of override device
1515 * corresponds to first bus of base device and so on. If base device has
1516 * lesser buses than override tree, then new buses are allocated for it.
1517 */
1518 struct bus *override_bus = override_dev->bus;
1519 struct bus *base_bus = base_dev->bus;
1520
1521 while (override_bus) {
1522
1523 /*
1524 * If we have more buses in override tree device, then allocate
1525 * a new bus for the base tree device as well.
1526 */
1527 if (!base_bus) {
1528 alloc_bus(base_dev);
1529 base_bus = base_dev->last_bus;
1530 }
1531
1532 override_devicetree(base_dev->bus, override_dev->bus);
1533
1534 override_bus = override_bus->next_bus;
1535 base_bus = base_bus->next_bus;
1536 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001537}
1538
1539/*
1540 * Perform copy of device and properties from override parent to base parent.
1541 * This function walks through the override tree in a depth-first manner
1542 * performing following actions:
1543 * 1. If matching device is found in base tree, then copy the properties of
1544 * override device to base tree device. Call override_devicetree recursively on
1545 * the bus of override device.
1546 * 2. If matching device is not found in base tree, then set override tree
1547 * device as new child of base_parent and update the chip pointers in override
1548 * device subtree to ensure the nodes do not point to override tree chip
1549 * instance.
1550 */
1551static void override_devicetree(struct bus *base_parent,
1552 struct bus *override_parent)
1553{
1554 struct device *base_child;
1555 struct device *override_child = override_parent->children;
1556 struct device *next_child;
1557
1558 while (override_child) {
1559
1560 /* Look for a matching device in base tree. */
1561 for (base_child = base_parent->children;
1562 base_child; base_child = base_child->sibling) {
1563 if (device_match(base_child, override_child))
1564 break;
1565 }
1566
1567 next_child = override_child->sibling;
1568
1569 /*
1570 * If matching device is found, copy properties of
1571 * override_child to base_child.
1572 */
1573 if (base_child)
1574 update_device(base_child, override_child);
1575 else {
1576 /*
1577 * If matching device is not found, set override_child
1578 * as a new child of base_parent.
1579 */
1580 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001581 }
1582
1583 override_child = next_child;
1584 }
1585}
1586
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001587int main(int argc, char **argv)
1588{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001589 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001590 usage();
1591
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001592 const char *base_devtree = argv[DEVICEFILE_ARG];
1593 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001594 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001595 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001596
1597 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001598
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001599 if (argc == TOTAL_ARG_COUNT) {
1600 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1601 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001602
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001603 if (!dev_has_children(&override_root_dev)) {
1604 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1605 exit(1);
1606 }
1607
Furquan Shaikh27efc502018-06-22 09:19:15 -07001608 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001609 }
1610
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001611 FILE *autogen = fopen(outputc, "w");
1612 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001613 fprintf(stderr, "Could not open file '%s' for writing: ",
1614 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001615 perror(NULL);
1616 exit(1);
1617 }
1618
Nico Huber17e9bcb2019-09-20 12:05:51 +02001619 FILE *autohead = fopen(outputh, "w");
1620 if (!autohead) {
1621 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1622 perror(NULL);
1623 fclose(autogen);
1624 exit(1);
1625 }
1626 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1627 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1628 fprintf(autohead, "#include <device/device.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001629 emit_fw_config(autohead);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001630
Nico Huber57bbcb32020-05-26 22:39:47 +02001631 fprintf(autogen, "#include <device/device.h>\n");
1632 fprintf(autogen, "#include <device/pci.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001633 fprintf(autogen, "#include <static.h>\n");
Nico Huber57bbcb32020-05-26 22:39:47 +02001634
Furquan Shaikh79e84122018-05-30 15:09:09 -07001635 emit_chips(autogen);
1636
Nico Huber17e9bcb2019-09-20 12:05:51 +02001637 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001638 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001639 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001640 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001641 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001642
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001643 /* Expose static devicenames to global namespace. */
1644 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001645 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001646
Nico Huber17e9bcb2019-09-20 12:05:51 +02001647 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1648 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001649 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001650
Patrick Georgi114e7b22010-05-05 11:19:50 +00001651 return 0;
1652}