blob: bff721548b343d36192ff613c862af4e2488bea2 [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>
Duncan Laurie51c83732020-06-09 11:20:29 -07005#include <getopt.h>
Werner Zehac14a402019-07-11 12:47:19 +02006/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
7#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +02008#include <commonlib/helpers.h>
Duncan Laurie47b7b342020-05-15 15:39:08 -07009#include <stdint.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000010#include "sconfig.h"
11#include "sconfig.tab.h"
12
Patrick Georgi7fc9e292010-07-15 15:59:07 +000013extern int linenum;
14
Furquan Shaikh27efc502018-06-22 09:19:15 -070015/*
16 * Maintains list of all the unique chip structures for the board.
17 * This is shared across base and override device trees since we need to
18 * generate headers for all chips added by both the trees.
19 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070020static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070021
Kyösti Mälkki472d9022011-12-05 20:33:55 +020022typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020023 UNSLASH,
24 SPLIT_1ST,
25 TO_LOWER,
26 TO_UPPER,
27} translate_t;
28
Furquan Shaikh93198262018-06-03 04:22:17 -070029/*
30 * Mainboard is assumed to have a root device whose bus is the parent of all the
31 * devices that are added by parsing the devicetree file. This device has a
32 * mainboard chip instance associated with it.
33 *
34 *
35 *
36 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070037 * | Root device | | Mainboard |
38 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070039 * | | | chip_instance | (mainboard_instance)|
40 * | +------------------------+ | |
41 * | | +----------------------+
42 * | | bus |
43 * | parent v |
44 * | +-------------------+ |
45 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070046 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070047 * | | |
48 * +-------------------+ |
49 * | |
50 * | children | chip
51 * v |
52 * X |
53 * (new devices will |
54 * be added here as |
55 * children) |
56 * |
57 * |
58 * |
59 * +-------+----------+
60 * | |
61 * | Mainboard chip +----------->X (new chips will be
62 * | (mainboard_chip) | added here)
63 * | |
64 * +------------------+
65 *
66 *
67 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070068
69/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070070static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070071
Duncan Lauriee335c2e2020-07-29 16:28:43 -070072/* Root device of chipset tree. */
73static struct device chipset_root_dev;
74
Furquan Shaikh39ac7972018-06-21 18:44:32 -070075/* Root device of override tree (if applicable). */
76static struct device override_root_dev;
77
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070078static struct chip_instance mainboard_instance;
79
Furquan Shaikhde39fc72018-06-11 04:26:45 -070080static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070081 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070082 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070083};
84
Furquan Shaikhde39fc72018-06-11 04:26:45 -070085static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070086 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070087 .chip_instance = &mainboard_instance,
88 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070089 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070090 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070091 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070092};
93
Duncan Lauriee335c2e2020-07-29 16:28:43 -070094static struct bus chipset_root_bus = {
95 .id = 0,
96 .dev = &chipset_root_dev,
97};
98
99static struct device chipset_root_dev = {
100 .name = "chipset_root",
101 .chip_instance = &mainboard_instance,
102 .path = " .type = DEVICE_PATH_ROOT ",
103 .parent = &chipset_root_bus,
104 .enabled = 1,
105 .bus = &chipset_root_bus,
106};
107
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700108static struct bus override_root_bus = {
109 .id = 0,
110 .dev = &override_root_dev,
111};
112
113static struct device override_root_dev = {
114 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700115 /*
116 * Override tree root device points to the same mainboard chip instance
117 * as the base tree root device. It should not cause any side-effects
118 * since the mainboard chip instance pointer in override tree will just
119 * be ignored.
120 */
121 .chip_instance = &mainboard_instance,
122 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700123 .parent = &override_root_bus,
124 .enabled = 1,
125 .bus = &override_root_bus,
126};
127
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700128static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000129 .name = "mainboard",
130 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700131 .instance = &mainboard_instance,
132};
133
134static struct chip_instance mainboard_instance = {
135 .id = 0,
136 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000137};
138
Furquan Shaikh93198262018-06-03 04:22:17 -0700139/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700140struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000141
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700142struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700143 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700144 struct queue_entry *next;
145 struct queue_entry *prev;
146};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700147
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700148#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700149
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700150static void *s_alloc(const char *f, size_t s)
151{
152 void *data = calloc(1, s);
153 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530154 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700155 exit(1);
156 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700157 return data;
158}
159
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700160static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700161{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700162 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700163
164 e->data = data;
165 e->next = e->prev = e;
166 return e;
167}
168
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700169static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700170{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700171 struct queue_entry *tmp = new_queue_entry(data);
172 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700173
174 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700175 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700176 return;
177 }
178
179 q->prev->next = tmp;
180 tmp->prev = q->prev;
181 q->prev = tmp;
182 tmp->next = q;
183}
184
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700185static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700186{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700187 struct queue_entry *q = *q_head;
188 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700189 void *data;
190
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700191 if (!q)
192 return NULL;
193
194 tmp = q->prev;
195
Furquan Shaikh79e84122018-05-30 15:09:09 -0700196 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700197 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700198 else {
199 tmp->prev->next = q;
200 q->prev = tmp->prev;
201 }
202
203 data = tmp->data;
204 free(tmp);
205
206 return data;
207}
208
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700209static void *dequeue_head(struct queue_entry **q_head)
210{
211 struct queue_entry *q = *q_head;
212 struct queue_entry *tmp = q;
213 void *data;
214
215 if (!q)
216 return NULL;
217
218 if (q->next == q)
219 *q_head = NULL;
220 else {
221 q->next->prev = q->prev;
222 q->prev->next = q->next;
223 *q_head = q->next;
224 }
225
226 data = tmp->data;
227 free(tmp);
228
229 return data;
230}
231
232static void *peek_queue_head(struct queue_entry *q_head)
233{
234 if (!q_head)
235 return NULL;
236
237 return q_head->data;
238}
239
240static struct queue_entry *chip_q_head;
241
242void chip_enqueue_tail(void *data)
243{
244 enqueue_tail(&chip_q_head, data);
245}
246
247void *chip_dequeue_tail(void)
248{
249 return dequeue_tail(&chip_q_head);
250}
251
Martin Rothbec07532016-08-05 18:32:18 -0600252int yywrap(void)
253{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000254 return 1;
255}
256
Martin Rothbec07532016-08-05 18:32:18 -0600257void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000258{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000259 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600260 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000261 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000262}
263
Martin Rothbec07532016-08-05 18:32:18 -0600264char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200265{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200266 char *b, *c;
267 b = c = strdup(str);
268 while (c && *c) {
269 if ((mode == SPLIT_1ST) && (*c == '/')) {
270 *c = 0;
271 break;
272 }
Martin Rothbec07532016-08-05 18:32:18 -0600273 if (*c == '/')
274 *c = '_';
275 if (*c == '-')
276 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200277 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200278 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200279 if (mode == TO_LOWER)
280 *c = tolower(*c);
281 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200282 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200283 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200284}
285
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700286static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600287{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700288 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700289
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700290 while (h->next) {
291 int result = strcmp(path, h->next->name);
292 if (result == 0)
293 return h->next;
294
295 if (result < 0)
296 break;
297
298 h = h->next;
299 }
300
301 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
302 new_chip->next = h->next;
303 h->next = new_chip;
304
Patrick Georgi114e7b22010-05-05 11:19:50 +0000305 new_chip->chiph_exists = 1;
306 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700307 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000308
309 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700310 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700311 sprintf(chip_h, "src/%s", path);
312 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100313 /* root_complex gets away without a separate directory, but
314 * exists on on pretty much all AMD chipsets.
315 */
316 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300317 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
318 path);
319 exit(1);
320 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700321 }
322
Martin Roth824255e2016-08-05 17:40:39 -0600323 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200324
Martin Roth824255e2016-08-05 17:40:39 -0600325 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600326 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000327
Patrick Georgi1f688802014-08-03 15:51:19 +0200328 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700329
Patrick Georgi114e7b22010-05-05 11:19:50 +0000330 return new_chip;
331}
332
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700333struct chip_instance *new_chip_instance(char *path)
334{
335 struct chip *chip = get_chip(path);
336 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
337
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700338 instance->chip = chip;
339 instance->next = chip->instance;
340 chip->instance = instance;
341
342 return instance;
343}
344
Duncan Laurie47b7b342020-05-15 15:39:08 -0700345/* List of fw_config fields added during parsing. */
346static struct fw_config_field *fw_config_fields;
347
348static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
349 const char *name)
350{
351 struct fw_config_option *option = field->options;
352
353 while (option && option->name) {
354 if (!strcmp(option->name, name))
355 return option;
356 option = option->next;
357 }
358 return NULL;
359}
360
361static struct fw_config_field *find_fw_config_field(const char *name)
362{
363 struct fw_config_field *field = fw_config_fields;
364
365 while (field && field->name) {
366 if (!strcmp(field->name, name))
367 return field;
368 field = field->next;
369 }
370 return NULL;
371}
372
373struct fw_config_field *get_fw_config_field(const char *name)
374{
375 struct fw_config_field *field = find_fw_config_field(name);
376
377 /* Fail if the field does not exist, new fields must be added with a mask. */
378 if (!field) {
379 printf("ERROR: fw_config field not found: %s\n", name);
380 exit(1);
381 }
382 return field;
383}
384
385static void append_fw_config_field(struct fw_config_field *add)
386{
387 struct fw_config_field *field = fw_config_fields;
388
389 if (!fw_config_fields) {
390 fw_config_fields = add;
391 } else {
392 while (field && field->next)
393 field = field->next;
394 field->next = add;
395 }
396}
397
398struct fw_config_field *new_fw_config_field(const char *name,
399 unsigned int start_bit, unsigned int end_bit)
400{
401 struct fw_config_field *field = find_fw_config_field(name);
402
403 /* Check that field is within 32bits. */
404 if (start_bit > end_bit || end_bit > 31) {
405 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
406 start_bit, end_bit);
407 exit(1);
408 }
409
410 /* Don't allow re-defining a field, only adding new fields. */
411 if (field) {
412 printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n",
413 name, start_bit, end_bit, field->start_bit, field->end_bit);
414 exit(1);
415 }
416
417 /* Check for overlap with an existing field. */
418 field = fw_config_fields;
419 while (field) {
420 /* Check if the mask overlaps. */
421 if (start_bit <= field->end_bit && end_bit >= field->start_bit) {
422 printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n",
423 name, start_bit, end_bit,
424 field->name, field->start_bit, field->end_bit);
425 exit(1);
426 }
427 field = field->next;
428 }
429
430 field = S_ALLOC(sizeof(*field));
431 field->name = name;
432 field->start_bit = start_bit;
433 field->end_bit = end_bit;
434 append_fw_config_field(field);
435
436 return field;
437}
438
439static void append_fw_config_option_to_field(struct fw_config_field *field,
440 struct fw_config_option *add)
441{
442 struct fw_config_option *option = field->options;
443
444 if (!option) {
445 field->options = add;
446 } else {
447 while (option && option->next)
448 option = option->next;
449 option->next = add;
450 }
451}
452
453void add_fw_config_option(struct fw_config_field *field, const char *name, unsigned int value)
454{
455 struct fw_config_option *option;
456 uint32_t field_max_value;
457
458 /* Check that option value fits within field mask. */
459 field_max_value = (1 << (1 + field->end_bit - field->start_bit)) - 1;
460 if (value > field_max_value) {
461 printf("ERROR: fw_config option %s:%s value %u larger than field max %u\n",
462 field->name, name, value, field_max_value);
463 exit(1);
464 }
465
466 /* Check for existing option with this name or value. */
467 option = field->options;
468 while (option) {
469 if (!strcmp(option->name, name)) {
470 printf("ERROR: fw_config option name %s:%s already exists\n",
471 field->name, name);
472 exit(1);
473 }
474 /* Compare values. */
475 if (value == option->value) {
476 printf("ERROR: fw_config option %s:%s[%u] redefined as %s\n",
477 field->name, option->name, value, name);
478 exit(1);
479 }
480 option = option->next;
481 }
482
483 option = S_ALLOC(sizeof(*option));
484 option->name = name;
485 option->value = value;
486
487 /* Add option to the current field. */
488 append_fw_config_option_to_field(field, option);
489}
490
491static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
492{
493 struct fw_config_probe *probe = dev->probe;
494
495 if (!probe) {
496 dev->probe = add;
497 } else {
498 while (probe && probe->next)
499 probe = probe->next;
500 probe->next = add;
501 }
502}
503
504void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
505{
506 struct fw_config_probe *probe;
507
508 probe = bus->dev->probe;
509 while (probe) {
510 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
511 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
512 exit(1);
513 }
514 probe = probe->next;
515 }
516
517 probe = S_ALLOC(sizeof(*probe));
518 probe->field = field;
519 probe->option = option;
520
521 append_fw_config_probe_to_dev(bus->dev, probe);
522}
523
524static void emit_fw_config(FILE *fil)
525{
526 struct fw_config_field *field = fw_config_fields;
527
528 if (!field)
529 return;
530
531 fprintf(fil, "\n/* firmware configuration */\n");
532 fprintf(fil, "#include <fw_config.h>\n");
533
534 while (field) {
535 struct fw_config_option *option = field->options;
536 uint32_t mask;
537
538 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
539 field->name, field->name);
540
541 /* Compute mask from start and end bit. */
542 mask = ((1 << (1 + field->end_bit - field->start_bit)) - 1);
543 mask <<= field->start_bit;
544
545 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%08x\n",
546 field->name, mask);
547
548 while (option) {
549 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
550 field->name, option->name, option->name);
551 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%08x\n",
552 field->name, option->name, option->value << field->start_bit);
553
554 option = option->next;
555 }
556
557 field = field->next;
558 }
559
560 fprintf(fil, "\n");
561}
562
563static int emit_fw_config_probe(FILE *fil, struct device *dev)
564{
565 struct fw_config_probe *probe = dev->probe;
566
567 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
568
569 while (probe) {
570 /* Find matching field. */
571 struct fw_config_field *field;
572 struct fw_config_option *option;
573 uint32_t mask, value;
574
575 field = find_fw_config_field(probe->field);
576 if (!field) {
577 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
578 return -1;
579 }
580 option = find_fw_config_option(field, probe->option);
581 if (!option) {
582 printf("ERROR: fw_config_probe field %s option %s not found\n",
583 probe->field, probe->option);
584 return -1;
585 }
586
587 /* Fill out the probe structure with values from emit_fw_config(). */
588 fprintf(fil, "\t{\n");
589 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
590 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
591 probe->field, probe->option);
592 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
593 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
594 probe->field, probe->option);
595 fprintf(fil, "\t},\n");
596
597 probe = probe->next;
598 }
599
600 /* Add empty entry to mark end of list. */
601 fprintf(fil, "\t{ }\n};\n");
602 return 0;
603}
604
Furquan Shaikh93198262018-06-03 04:22:17 -0700605/*
606 * Allocate a new bus for the provided device.
607 * - If this is the first bus being allocated under this device, then its id
608 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
609 * - If this is not the first bus under this device, then its id is set to 1
610 * plus the id of last bus and newly allocated bus is added to the list of
611 * buses under the device. last_bus is updated to point to the newly
612 * allocated bus.
613 */
614static void alloc_bus(struct device *dev)
615{
616 struct bus *bus = S_ALLOC(sizeof(*bus));
617
618 bus->dev = dev;
619
620 if (dev->last_bus == NULL) {
621 bus->id = 0;
622 dev->bus = bus;
623 } else {
624 bus->id = dev->last_bus->id + 1;
625 dev->last_bus->next_bus = bus;
626 }
627
628 dev->last_bus = bus;
629}
630
631/*
632 * Allocate a new device under the given parent. This function allocates a new
633 * device structure under the provided parent bus and allocates a bus structure
634 * under the newly allocated device.
635 */
636static struct device *alloc_dev(struct bus *parent)
637{
638 struct device *dev = S_ALLOC(sizeof(*dev));
639
Furquan Shaikh93198262018-06-03 04:22:17 -0700640 dev->parent = parent;
641 dev->subsystem_vendor = -1;
642 dev->subsystem_device = -1;
643
644 alloc_bus(dev);
645
646 return dev;
647}
648
649/*
650 * This function scans the children of given bus to see if any device matches
651 * the new device that is requested.
652 *
653 * Returns pointer to the node if found, else NULL.
654 */
655static struct device *get_dev(struct bus *parent, int path_a, int path_b,
656 int bustype, struct chip_instance *chip_instance)
657{
658 struct device *child = parent->children;
659
660 while (child) {
661 if ((child->path_a == path_a) && (child->path_b == path_b) &&
662 (child->bustype == bustype) &&
663 (child->chip_instance == chip_instance))
664 return child;
665
666 child = child->sibling;
667 }
668
669 return NULL;
670}
671
Furquan Shaikh27efc502018-06-22 09:19:15 -0700672/*
673 * Add given node as child of the provided parent. If this is the first child of
674 * the parent, update parent->children pointer as well.
675 */
676static void set_new_child(struct bus *parent, struct device *child)
677{
678 struct device *c = parent->children;
679 if (c) {
680 while (c->sibling)
681 c = c->sibling;
682 c->sibling = child;
683 } else
684 parent->children = child;
685
686 child->sibling = NULL;
687 child->parent = parent;
688}
689
Nico Huber8e1ea522020-06-03 10:20:07 -0700690static const struct device *find_alias(const struct device *const parent,
691 const char *const alias)
692{
693 if (parent->alias && !strcmp(parent->alias, alias))
694 return parent;
695
696 const struct bus *bus;
697 for (bus = parent->bus; bus; bus = bus->next_bus) {
698 const struct device *child;
699 for (child = bus->children; child; child = child->sibling) {
700 const struct device *const ret = find_alias(child, alias);
701 if (ret)
702 return ret;
703 }
704 }
705
706 return NULL;
707}
708
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700709static struct device *new_device_with_path(struct bus *parent,
710 struct chip_instance *chip_instance,
711 const int bustype, int path_a, int path_b,
712 char *alias, int status)
Martin Rothbec07532016-08-05 18:32:18 -0600713{
Furquan Shaikh93198262018-06-03 04:22:17 -0700714 struct device *new_d;
715
Furquan Shaikh93198262018-06-03 04:22:17 -0700716 /* If device is found under parent, no need to allocate new device. */
717 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
718 if (new_d) {
719 alloc_bus(new_d);
720 return new_d;
721 }
722
723 new_d = alloc_dev(parent);
724
725 new_d->bustype = bustype;
726
727 new_d->path_a = path_a;
728 new_d->path_b = path_b;
Nico Huber8e1ea522020-06-03 10:20:07 -0700729 new_d->alias = alias;
Furquan Shaikh93198262018-06-03 04:22:17 -0700730
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800731 new_d->enabled = status & 0x01;
732 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000733 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700734 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000735
Furquan Shaikh27efc502018-06-22 09:19:15 -0700736 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000737
Furquan Shaikha9b64292018-05-31 07:52:00 -0700738 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200739 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000740 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200741 break;
742
743 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000744 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200745 break;
746
747 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700748 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200749 break;
750
751 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000752 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200753 break;
754
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800755 case CPU_CLUSTER:
756 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200757 break;
758
Aaron Durbinffda804b2014-09-03 12:40:15 -0500759 case CPU:
760 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
761 break;
762
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800763 case DOMAIN:
764 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200765 break;
766
767 case IOAPIC:
768 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
769 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700770
771 case GENERIC:
772 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
773 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800774
775 case SPI:
776 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
777 break;
778
Duncan Lauriebae9f852018-05-07 14:18:13 -0700779 case USB:
780 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
781 break;
782
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800783 case MMIO:
784 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
785 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600786
787 case ESPI:
788 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
789 break;
790
791 case LPC:
792 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
793 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000794 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800795
Patrick Georgi114e7b22010-05-05 11:19:50 +0000796 return new_d;
797}
798
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700799struct device *new_device_reference(struct bus *parent,
800 struct chip_instance *chip_instance,
801 const char *reference, int status)
802{
803 const struct device *dev = find_alias(&base_root_dev, reference);
804
805 if (!dev) {
806 printf("ERROR: Unable to find device reference %s\n", reference);
807 exit(1);
808 }
809
810 return new_device_with_path(parent, chip_instance, dev->bustype, dev->path_a,
811 dev->path_b, NULL, status);
812}
813
814struct device *new_device_raw(struct bus *parent,
815 struct chip_instance *chip_instance,
816 const int bustype, const char *devnum,
817 char *alias, int status)
818{
819 char *tmp;
820 int path_a;
821 int path_b = 0;
822
823 /* Check for alias name conflicts. */
824 if (alias && find_alias(root_parent->dev, alias)) {
825 printf("ERROR: Alias already exists: %s\n", alias);
826 exit(1);
827 }
828
829 path_a = strtol(devnum, &tmp, 16);
830 if (*tmp == '.') {
831 tmp++;
832 path_b = strtol(tmp, NULL, 16);
833 }
834
835 return new_device_with_path(parent, chip_instance, bustype, path_a, path_b, alias,
836 status);
837}
838
Furquan Shaikh27efc502018-06-22 09:19:15 -0700839static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600840{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700841 struct resource *r = S_ALLOC(sizeof(struct resource));
842
Patrick Georgi114e7b22010-05-05 11:19:50 +0000843 r->type = type;
844 r->index = index;
845 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000846 if (dev->res) {
847 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600848 while (head->next)
849 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000850 head->next = r;
851 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000852 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000853 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000854}
855
Furquan Shaikh27efc502018-06-22 09:19:15 -0700856void add_resource(struct bus *bus, int type, int index, int base)
857{
858 new_resource(bus->dev, type, index, base);
859}
860
Nico Huberd09459f2020-05-26 22:13:09 +0200861static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600862{
Nico Huberd09459f2020-05-26 22:13:09 +0200863 struct reg *const r = S_ALLOC(sizeof(struct reg));
864 struct reg *prev = NULL;
865 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700866
Patrick Georgi114e7b22010-05-05 11:19:50 +0000867 r->key = name;
868 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200869
870 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
871 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000872 if (sort == 0) {
873 printf("ERROR: duplicate 'register' key.\n");
874 exit(1);
875 }
Nico Huberd09459f2020-05-26 22:13:09 +0200876 if (sort < 0)
877 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000878 }
Nico Huberd09459f2020-05-26 22:13:09 +0200879 r->next = cur;
880 if (prev)
881 prev->next = r;
882 else
883 *head = r;
884}
885
886void add_register(struct chip_instance *chip_instance, char *name, char *val)
887{
888 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000889}
890
Nico Huber8e1ea522020-06-03 10:20:07 -0700891void add_reference(struct chip_instance *const chip_instance,
892 char *const name, char *const alias)
893{
894 add_reg(&chip_instance->ref, name, alias);
895}
896
897static void set_reference(struct chip_instance *const chip_instance,
898 char *const name, char *const alias)
899{
900 const struct device *const dev = find_alias(&base_root_dev, alias);
901 if (!dev) {
902 printf("ERROR: Cannot find device alias '%s'.\n", alias);
903 exit(1);
904 }
905
906 char *const ref_name = S_ALLOC(strlen(dev->name) + 2);
907 sprintf(ref_name, "&%s", dev->name);
908 add_register(chip_instance, name, ref_name);
909}
910
911static void update_references(FILE *file, FILE *head, struct device *dev,
912 struct device *next)
913{
914 struct reg *ref;
915
916 for (ref = dev->chip_instance->ref; ref; ref = ref->next)
917 set_reference(dev->chip_instance, ref->key, ref->value);
918}
919
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200920void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
921 char *data_width)
922{
923 struct device *dev = bus->dev;
924
925 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
926 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
927 exit(1);
928 }
929
930 dev->smbios_slot_type = type;
931 dev->smbios_slot_length = length;
932 dev->smbios_slot_data_width = data_width;
933 dev->smbios_slot_designation = designation;
934}
935
Furquan Shaikh93198262018-06-03 04:22:17 -0700936void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600937 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000938{
Furquan Shaikh93198262018-06-03 04:22:17 -0700939 struct device *dev = bus->dev;
940
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800941 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000942 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
943 exit(1);
944 }
945
946 dev->subsystem_vendor = vendor;
947 dev->subsystem_device = device;
948 dev->inherit_subsystem = inherit;
949}
950
Furquan Shaikh93198262018-06-03 04:22:17 -0700951void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600952 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200953{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200954 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700955 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200956
Martin Rothbec07532016-08-05 18:32:18 -0600957 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
958 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200959 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
960 exit(1);
961 }
962
963 srcpin = _srcpin[3] - 'A';
964
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800965 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200966 printf("ERROR: ioapic config only allowed for PCI devices\n");
967 exit(1);
968 }
969
970 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200971 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200972 exit(1);
973 }
974 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
975 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
976}
977
Furquan Shaikh93198262018-06-03 04:22:17 -0700978static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600979{
Furquan Shaikh93198262018-06-03 04:22:17 -0700980 struct bus *bus = dev->bus;
981
982 while (bus) {
983 if (bus->children)
984 return 1;
985 bus = bus->next_bus;
986 }
987
988 return 0;
989}
990
Nico Huber17e9bcb2019-09-20 12:05:51 +0200991static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700992{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700993 static int dev_id;
994
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700995 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200996 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700997 ptr->name);
998 return;
999 }
1000
Furquan Shaikh4ebe9532020-05-02 15:34:42 -07001001 char *name = S_ALLOC(10);
1002 sprintf(name, "_dev%d", dev_id++);
1003 ptr->name = name;
1004
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001005 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001006 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001007 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001008 ptr->name);
1009 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001010 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -06001011 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -07001012
Furquan Shaikh93198262018-06-03 04:22:17 -07001013 if (next)
1014 return;
1015
1016 fprintf(fil,
1017 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
1018 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001019}
1020
Furquan Shaikh93198262018-06-03 04:22:17 -07001021static void emit_resources(FILE *fil, struct device *ptr)
1022{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001023 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -07001024 return;
1025
1026 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001027 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001028 struct resource *r = ptr->res;
1029 while (r) {
1030 fprintf(fil,
1031 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
1032 if (r->type == IRQ)
1033 fprintf(fil, "IRQ");
1034 if (r->type == DRQ)
1035 fprintf(fil, "DRQ");
1036 if (r->type == IO)
1037 fprintf(fil, "IO");
1038 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
1039 r->base);
1040 if (r->next)
1041 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
1042 i++);
1043 else
1044 fprintf(fil, ".next=NULL },\n");
1045 r = r->next;
1046 }
1047
1048 fprintf(fil, "\t };\n");
1049}
1050
1051static void emit_bus(FILE *fil, struct bus *bus)
1052{
1053 fprintf(fil, "\t\t[%d] = {\n", bus->id);
1054 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
1055 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
1056 if (bus->children)
1057 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
1058
1059 if (bus->next_bus)
1060 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
1061 bus->id + 1);
1062 else
1063 fprintf(fil, "\t\t\t.next = NULL,\n");
1064 fprintf(fil, "\t\t},\n");
1065}
1066
1067static void emit_dev_links(FILE *fil, struct device *ptr)
1068{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001069 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001070 ptr->name);
1071
1072 struct bus *bus = ptr->bus;
1073
1074 while (bus) {
1075 emit_bus(fil, bus);
1076 bus = bus->next_bus;
1077 }
1078
1079 fprintf(fil, "\t};\n");
1080}
1081
Nico Huber17e9bcb2019-09-20 12:05:51 +02001082static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +02001083{
1084 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001085 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -07001086 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -07001087
Furquan Shaikhbbade242020-05-02 16:05:29 -07001088 /*
1089 * If the chip instance of device has base_chip_instance pointer set, then follow that
1090 * to update the chip instance for current device.
1091 */
1092 if (chip_ins->base_chip_instance)
1093 chip_ins = chip_ins->base_chip_instance;
1094
Duncan Laurie47b7b342020-05-15 15:39:08 -07001095 /* Emit probe structures. */
1096 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
1097 fclose(head);
1098 fclose(fil);
1099 exit(1);
1100 }
1101
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001102 if (ptr == &base_root_dev)
1103 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1104 else
1105 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1106
Furquan Shaikh93198262018-06-03 04:22:17 -07001107 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001108
1109 /*
1110 * ops field is set to default_dev_ops_root only for the root
1111 * device. For all other devices, it is set by the driver at runtime.
1112 */
1113 if (ptr == &base_root_dev)
1114 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1115 else
1116 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001117 fprintf(fil, "#endif\n");
1118 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1119 ptr->parent->id);
1120 fprintf(fil, "\t.path = {");
1121 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1122 fprintf(fil, "},\n");
1123 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001124 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001125 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001126 fprintf(fil, "\t.on_mainboard = 1,\n");
1127 if (ptr->subsystem_vendor > 0)
1128 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1129 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001130
Furquan Shaikh93198262018-06-03 04:22:17 -07001131 if (ptr->subsystem_device > 0)
1132 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1133 ptr->subsystem_device);
1134
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001135 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001136 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001137 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001138 }
1139 if (has_children)
1140 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1141 ptr->name);
1142 else
1143 fprintf(fil, "\t.link_list = NULL,\n");
1144 if (ptr->sibling)
1145 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001146 else
1147 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001148 fprintf(fil, "#if !DEVTREE_EARLY\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001149 if (ptr->probe)
1150 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Kyösti Mälkki1557a672019-06-30 10:51:31 +03001151 for (pin = 0; pin < 4; pin++) {
1152 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
1153 fprintf(fil,
1154 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
1155 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
1156
1157 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
1158 fprintf(fil,
1159 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
1160 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
1161 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001162 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1163 chip_ins->chip->name_underscore);
1164 if (chip_ins == &mainboard_instance)
1165 fprintf(fil, "\t.name = mainboard_name,\n");
1166 fprintf(fil, "#endif\n");
1167 if (chip_ins->chip->chiph_exists)
1168 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1169 chip_ins->chip->name_underscore, chip_ins->id);
1170 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001171 fprintf(fil, "\t.next=&%s,\n", next->name);
1172 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1173 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1174 fprintf(fil, "#if !DEVTREE_EARLY\n");
1175 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1176 }
1177 /* SMBIOS types start at 1, if zero it hasn't been set */
1178 if (ptr->smbios_slot_type)
1179 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1180 ptr->smbios_slot_type);
1181 if (ptr->smbios_slot_data_width)
1182 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1183 ptr->smbios_slot_data_width);
1184 if (ptr->smbios_slot_designation)
1185 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1186 ptr->smbios_slot_designation);
1187 if (ptr->smbios_slot_length)
1188 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1189 ptr->smbios_slot_length);
1190 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1191 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1192 fprintf(fil, "#endif\n");
1193 fprintf(fil, "#endif\n");
1194 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001195 fprintf(fil, "};\n");
1196
1197 emit_resources(fil, ptr);
1198
1199 if (has_children)
1200 emit_dev_links(fil, ptr);
1201}
1202
Nico Huber17e9bcb2019-09-20 12:05:51 +02001203static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001204{
1205 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001206 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
1207 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
1208 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001209 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
1210 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001211 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001212
Nico Huber17e9bcb2019-09-20 12:05:51 +02001213 if (ptr->bustype == PNP) {
1214 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
1215 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001216 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
1217 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001218 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001219}
1220
Furquan Shaikh93198262018-06-03 04:22:17 -07001221static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1222 struct device *d)
1223{
1224 while (d) {
1225 enqueue_tail(bfs_q_head, d);
1226 d = d->sibling;
1227 }
1228}
1229
1230static void add_children_to_queue(struct queue_entry **bfs_q_head,
1231 struct device *d)
1232{
1233 struct bus *bus = d->bus;
1234
1235 while (bus) {
1236 if (bus->children)
1237 add_siblings_to_queue(bfs_q_head, bus->children);
1238 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001239 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001240}
1241
Nico Huber17e9bcb2019-09-20 12:05:51 +02001242static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1243 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001244 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001245{
Furquan Shaikh93198262018-06-03 04:22:17 -07001246 struct queue_entry *bfs_q_head = NULL;
1247
1248 enqueue_tail(&bfs_q_head, ptr);
1249
1250 while ((ptr = dequeue_head(&bfs_q_head))) {
1251 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001252 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001253 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001254}
1255
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001256static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001257{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001258 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001259
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001260 while (chip) {
1261 if (chip->chiph_exists)
1262 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1263 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001264 }
1265 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1266 fprintf(fil,
1267 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001268
1269 chip = tmp;
1270 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001271 /* A lot of cpus do not define chip_operations at all, and the ones
1272 that do only initialise .name. */
1273 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1274 fprintf(fil,
1275 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1276 chip->name_underscore);
1277 } else {
1278 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1279 chip->name_underscore);
1280 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001281 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001282 }
1283 fprintf(fil, "#endif\n");
1284}
1285
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001286static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1287{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001288 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001289 instance->chip->name_underscore,
1290 instance->chip->name_underscore,
1291 instance->id);
1292
1293 if (instance->reg) {
1294 fprintf(fil, "\n");
1295 struct reg *r = instance->reg;
1296 while (r) {
1297 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1298 r = r->next;
1299 }
1300 }
1301 fprintf(fil, "};\n\n");
1302}
1303
Nico Huber8e1ea522020-06-03 10:20:07 -07001304static void emit_chip_configs(FILE *fil)
Furquan Shaikh79e84122018-05-30 15:09:09 -07001305{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001306 struct chip *chip = chip_header.next;
1307 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001308 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001309
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001310 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001311 if (!chip->chiph_exists)
1312 continue;
1313
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001314 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001315 instance = chip->instance;
1316 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001317 /*
1318 * Emit this chip instance only if there is no forwarding pointer to the
1319 * base tree chip instance.
1320 */
1321 if (instance->base_chip_instance == NULL) {
1322 instance->id = chip_id++;
1323 emit_chip_instance(fil, instance);
1324 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001325 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001326 }
1327 }
1328}
1329
Nico Huber17e9bcb2019-09-20 12:05:51 +02001330static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001331 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001332{
1333 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001334
1335 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1336 /* user already gave us a subsystem vendor/device */
1337 return;
1338 }
1339
Furquan Shaikh93198262018-06-03 04:22:17 -07001340 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001341
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001342 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001343 continue;
1344
1345 if (p->inherit_subsystem) {
1346 dev->subsystem_vendor = p->subsystem_vendor;
1347 dev->subsystem_device = p->subsystem_device;
1348 break;
1349 }
1350 }
1351}
1352
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001353static void usage(void)
1354{
Duncan Laurie51c83732020-06-09 11:20:29 -07001355 printf("usage: sconfig <options>\n");
1356 printf(" -c | --output_c : Path to output static.c file (required)\n");
1357 printf(" -r | --output_h : Path to header static.h file (required)\n");
1358 printf(" -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n");
1359 printf(" -o | --override_devtree : Path to override devicetree file (optional)\n");
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001360 printf(" -p | --chipset_devtree : Path to chipset/SOC devicetree file (optional)\n");
Martin Rothbec07532016-08-05 18:32:18 -06001361 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001362}
1363
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001364static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001365{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001366 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001367 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001368 perror(NULL);
1369 exit(1);
1370 }
1371
Patrick Georgi114e7b22010-05-05 11:19:50 +00001372 yyrestart(filec);
1373
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001374 root_parent = parent;
1375 linenum = 0;
1376
Patrick Georgi114e7b22010-05-05 11:19:50 +00001377 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001378
Patrick Georgi114e7b22010-05-05 11:19:50 +00001379 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001380}
1381
Furquan Shaikh27efc502018-06-22 09:19:15 -07001382/*
1383 * Match device nodes from base and override tree to see if they are the same
1384 * node.
1385 */
1386static int device_match(struct device *a, struct device *b)
1387{
1388 return ((a->path_a == b->path_a) &&
1389 (a->path_b == b->path_b) &&
1390 (a->bustype == b->bustype) &&
1391 (a->chip_instance->chip ==
1392 b->chip_instance->chip));
1393}
1394
1395/*
Bill XIEc61d4152019-11-21 18:16:12 +08001396 * Match resource nodes from base and override tree to see if they are the same
1397 * node.
1398 */
1399static int res_match(struct resource *a, struct resource *b)
1400{
1401 return ((a->type == b->type) &&
1402 (a->index == b->index));
1403}
1404
1405/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001406 * Add resource to device. If resource is already present, then update its base
1407 * and index. If not, then add a new resource to the device.
1408 */
1409static void update_resource(struct device *dev, struct resource *res)
1410{
1411 struct resource *base_res = dev->res;
1412
1413 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001414 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001415 base_res->base = res->base;
1416 return;
1417 }
1418 base_res = base_res->next;
1419 }
1420
1421 new_resource(dev, res->type, res->index, res->base);
1422}
1423
1424/*
1425 * Add register to chip instance. If register is already present, then update
1426 * its value. If not, then add a new register to the chip instance.
1427 */
Nico Huber8e1ea522020-06-03 10:20:07 -07001428static void update_register(struct reg **const head, struct reg *reg)
Furquan Shaikh27efc502018-06-22 09:19:15 -07001429{
Nico Huber8e1ea522020-06-03 10:20:07 -07001430 struct reg *base_reg = *head;
Furquan Shaikh27efc502018-06-22 09:19:15 -07001431
1432 while (base_reg) {
1433 if (!strcmp(base_reg->key, reg->key)) {
1434 base_reg->value = reg->value;
1435 return;
1436 }
1437 base_reg = base_reg->next;
1438 }
1439
Nico Huber8e1ea522020-06-03 10:20:07 -07001440 add_reg(head, reg->key, reg->value);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001441}
1442
1443static void override_devicetree(struct bus *base_parent,
1444 struct bus *override_parent);
1445
1446/*
1447 * Update the base device properties using the properties of override device. In
1448 * addition to that, call override_devicetree for all the buses under the
1449 * override device.
1450 *
1451 * Override Rules:
1452 * +--------------------+--------------------------------------------+
1453 * | | |
1454 * |struct device member| Rule |
1455 * | | |
1456 * +-----------------------------------------------------------------+
1457 * | | |
1458 * | id | Unchanged. This is used to generate device |
1459 * | | structure name in static.c. So, no need to |
1460 * | | override. |
1461 * | | |
1462 * +-----------------------------------------------------------------+
1463 * | | |
1464 * | enabled | Copy enabled state from override device. |
1465 * | | This allows variants to override device |
1466 * | | state. |
1467 * | | |
1468 * +-----------------------------------------------------------------+
1469 * | | |
1470 * | subsystem_vendor | Copy from override device only if any one |
1471 * | subsystem_device | of the ids is non-zero. |
1472 * | | |
1473 * +-----------------------------------------------------------------+
1474 * | | |
1475 * | inherit_subsystem | Copy from override device only if it is |
1476 * | | non-zero. This allows variant to only |
1477 * | | enable inherit flag for a device. |
1478 * | | |
1479 * +-----------------------------------------------------------------+
1480 * | | |
1481 * | path | Unchanged since these are same for both |
1482 * | path_a | base and override device (Used for |
1483 * | path_b | matching devices). |
1484 * | | |
1485 * +-----------------------------------------------------------------+
1486 * | | |
1487 * | bustype | Unchanged since this is same for both base |
1488 * | | and override device (User for matching |
1489 * | | devices). |
1490 * | | |
1491 * +-----------------------------------------------------------------+
1492 * | | |
1493 * | pci_irq_info | Unchanged. |
1494 * | | |
1495 * +-----------------------------------------------------------------+
1496 * | | |
1497 * | parent | Unchanged. This is meaningful only within |
1498 * | sibling | the parse tree, hence not being copied. |
1499 * | | |
1500 * +-----------------------------------------------------------------+
1501 * | | |
1502 * | res | Each resource that is present in override |
1503 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001504 * | | 1. If resource of same type and index is |
1505 * | | present in base device, then base of |
1506 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001507 * | | 2. If not, then a new resource is allocated|
1508 * | | under the base device using type, index |
1509 * | | and base from override res. |
1510 * | | |
1511 * +-----------------------------------------------------------------+
1512 * | | |
Nico Huber8e1ea522020-06-03 10:20:07 -07001513 * | ref | Each reference that is present in override |
1514 * | | device is copied over to base device with |
1515 * | | the same rules as registers. |
1516 * | | |
1517 * +-----------------------------------------------------------------+
1518 * | | |
1519 * | alias | Base device alias is copied to override. |
1520 * | | Override devices cannot change/remove an |
1521 * | | existing alias, but they can add an alias |
1522 * | | if one does not exist. |
1523 * | | |
1524 * +-----------------------------------------------------------------+
1525 * | | |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001526 * | chip_instance | Each register of chip_instance is copied |
1527 * | | over from override device to base device: |
1528 * | | 1. If register with same key is present in |
1529 * | | base device, then value of the register |
1530 * | | is copied. |
1531 * | | 2. If not, then a new register is allocated|
1532 * | | under the base chip_instance using key |
1533 * | | and value from override register. |
1534 * | | |
1535 * +-----------------------------------------------------------------+
1536 * | | |
1537 * | bus | Recursively call override_devicetree on |
1538 * | last_bus | each bus of override device. It is assumed |
1539 * | | that bus with id X under base device |
1540 * | | to bus with id X under override device. If |
1541 * | | override device has more buses than base |
1542 * | | device, then new buses are allocated under |
1543 * | | base device. |
1544 * | | |
1545 * +-----------------------------------------------------------------+
1546 */
1547static void update_device(struct device *base_dev, struct device *override_dev)
1548{
1549 /*
1550 * Copy the enabled state of override device to base device. This allows
1551 * override tree to enable or disable a particular device.
1552 */
1553 base_dev->enabled = override_dev->enabled;
1554
1555 /*
1556 * Copy subsystem vendor and device ids from override device to base
1557 * device only if the ids are non-zero in override device. Else, honor
1558 * the values in base device.
1559 */
1560 if (override_dev->subsystem_vendor ||
1561 override_dev->subsystem_device) {
1562 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1563 base_dev->subsystem_device = override_dev->subsystem_device;
1564 }
1565
1566 /*
1567 * Copy value of inherity_subsystem from override device to base device
1568 * only if it is non-zero in override device. This allows override
1569 * tree to only enable inhert flag for a device.
1570 */
1571 if (override_dev->inherit_subsystem)
1572 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1573
1574 /*
1575 * Copy resources of override device to base device.
1576 * 1. If resource is already present in base device, then index and base
1577 * of the resource will be copied over.
1578 * 2. If resource is not already present in base device, a new resource
1579 * will be allocated.
1580 */
1581 struct resource *res = override_dev->res;
1582 while (res) {
1583 update_resource(base_dev, res);
1584 res = res->next;
1585 }
1586
1587 /*
1588 * Copy registers of override chip instance to base chip instance.
1589 * 1. If register key is already present in base chip instance, then
1590 * value for the register is copied over.
1591 * 2. If register key is not already present in base chip instance, then
1592 * a new register will be allocated.
1593 */
1594 struct reg *reg = override_dev->chip_instance->reg;
1595 while (reg) {
Nico Huber8e1ea522020-06-03 10:20:07 -07001596 update_register(&base_dev->chip_instance->reg, reg);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001597 reg = reg->next;
1598 }
1599
Nico Huber8e1ea522020-06-03 10:20:07 -07001600 /* Copy references just as with registers. */
1601 reg = override_dev->chip_instance->ref;
1602 while (reg) {
1603 update_register(&base_dev->chip_instance->ref, reg);
1604 reg = reg->next;
1605 }
1606
1607 /* Check for alias name conflicts. */
1608 if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) {
1609 printf("ERROR: alias already exists: %s\n", override_dev->alias);
1610 exit(1);
1611 }
1612
1613 /*
1614 * Copy alias from base device.
1615 *
1616 * Override devices cannot change/remove an existing alias,
1617 * but they can add an alias to a device if one does not exist yet.
1618 */
1619 if (base_dev->alias)
1620 override_dev->alias = base_dev->alias;
1621 else
1622 base_dev->alias = override_dev->alias;
1623
Furquan Shaikh27efc502018-06-22 09:19:15 -07001624 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001625 * Use probe list from override device in place of base device, in order
1626 * to allow an override to remove a probe from the base device.
1627 */
1628 base_dev->probe = override_dev->probe;
1629
1630 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001631 * Update base_chip_instance member in chip instance of override tree to forward it to
1632 * the chip instance in base tree.
1633 */
1634 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1635
1636 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001637 * Now that the device properties are all copied over, look at each bus
1638 * of the override device and run override_devicetree in a recursive
1639 * manner. The assumption here is that first bus of override device
1640 * corresponds to first bus of base device and so on. If base device has
1641 * lesser buses than override tree, then new buses are allocated for it.
1642 */
1643 struct bus *override_bus = override_dev->bus;
1644 struct bus *base_bus = base_dev->bus;
1645
1646 while (override_bus) {
1647
1648 /*
1649 * If we have more buses in override tree device, then allocate
1650 * a new bus for the base tree device as well.
1651 */
1652 if (!base_bus) {
1653 alloc_bus(base_dev);
1654 base_bus = base_dev->last_bus;
1655 }
1656
1657 override_devicetree(base_dev->bus, override_dev->bus);
1658
1659 override_bus = override_bus->next_bus;
1660 base_bus = base_bus->next_bus;
1661 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001662}
1663
1664/*
1665 * Perform copy of device and properties from override parent to base parent.
1666 * This function walks through the override tree in a depth-first manner
1667 * performing following actions:
1668 * 1. If matching device is found in base tree, then copy the properties of
1669 * override device to base tree device. Call override_devicetree recursively on
1670 * the bus of override device.
1671 * 2. If matching device is not found in base tree, then set override tree
1672 * device as new child of base_parent and update the chip pointers in override
1673 * device subtree to ensure the nodes do not point to override tree chip
1674 * instance.
1675 */
1676static void override_devicetree(struct bus *base_parent,
1677 struct bus *override_parent)
1678{
1679 struct device *base_child;
1680 struct device *override_child = override_parent->children;
1681 struct device *next_child;
1682
1683 while (override_child) {
1684
1685 /* Look for a matching device in base tree. */
1686 for (base_child = base_parent->children;
1687 base_child; base_child = base_child->sibling) {
1688 if (device_match(base_child, override_child))
1689 break;
1690 }
1691
1692 next_child = override_child->sibling;
1693
1694 /*
1695 * If matching device is found, copy properties of
1696 * override_child to base_child.
1697 */
1698 if (base_child)
1699 update_device(base_child, override_child);
1700 else {
1701 /*
1702 * If matching device is not found, set override_child
1703 * as a new child of base_parent.
1704 */
1705 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001706 }
1707
1708 override_child = next_child;
1709 }
1710}
1711
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001712static void parse_override_devicetree(const char *file, struct device *dev)
1713{
1714 parse_devicetree(file, dev->bus);
1715
1716 if (!dev_has_children(dev)) {
1717 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1718 exit(1);
1719 }
1720
1721 override_devicetree(&base_root_bus, dev->bus);
1722}
1723
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001724int main(int argc, char **argv)
1725{
Duncan Laurie51c83732020-06-09 11:20:29 -07001726 static const struct option long_options[] = {
1727 { "mainboard_devtree", 1, NULL, 'm' },
1728 { "override_devtree", 1, NULL, 'o' },
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001729 { "chipset_devtree", 1, NULL, 'p' },
Duncan Laurie51c83732020-06-09 11:20:29 -07001730 { "output_c", 1, NULL, 'c' },
1731 { "output_h", 1, NULL, 'r' },
1732 { "help", 1, NULL, 'h' },
1733 { }
1734 };
1735 const char *override_devtree = NULL;
1736 const char *base_devtree = NULL;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001737 const char *chipset_devtree = NULL;
Duncan Laurie51c83732020-06-09 11:20:29 -07001738 const char *outputc = NULL;
1739 const char *outputh = NULL;
1740 int opt, option_index;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001741
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001742 while ((opt = getopt_long(argc, argv, "m:o:p:c:r:h", long_options,
Duncan Laurie51c83732020-06-09 11:20:29 -07001743 &option_index)) != EOF) {
1744 switch (opt) {
1745 case 'm':
1746 base_devtree = strdup(optarg);
1747 break;
1748 case 'o':
1749 override_devtree = strdup(optarg);
1750 break;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001751 case 'p':
1752 chipset_devtree = strdup(optarg);
1753 break;
Duncan Laurie51c83732020-06-09 11:20:29 -07001754 case 'c':
1755 outputc = strdup(optarg);
1756 break;
1757 case 'r':
1758 outputh = strdup(optarg);
1759 break;
1760 case 'h':
1761 default:
1762 usage();
1763 }
1764 }
1765
1766 if (!base_devtree || !outputc || !outputh)
1767 usage();
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001768
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001769 if (chipset_devtree) {
1770 /* Use the chipset devicetree as the base, then override
1771 with the mainboard "base" devicetree. */
1772 parse_devicetree(chipset_devtree, &base_root_bus);
1773 parse_override_devicetree(base_devtree, &chipset_root_dev);
1774 } else {
1775 parse_devicetree(base_devtree, &base_root_bus);
1776 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001777
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001778 if (override_devtree)
1779 parse_override_devicetree(override_devtree, &override_root_dev);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001780
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001781 FILE *autogen = fopen(outputc, "w");
1782 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001783 fprintf(stderr, "Could not open file '%s' for writing: ",
1784 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001785 perror(NULL);
1786 exit(1);
1787 }
1788
Nico Huber17e9bcb2019-09-20 12:05:51 +02001789 FILE *autohead = fopen(outputh, "w");
1790 if (!autohead) {
1791 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1792 perror(NULL);
1793 fclose(autogen);
1794 exit(1);
1795 }
1796 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1797 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1798 fprintf(autohead, "#include <device/device.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001799 emit_fw_config(autohead);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001800
Nico Huber57bbcb32020-05-26 22:39:47 +02001801 fprintf(autogen, "#include <device/device.h>\n");
1802 fprintf(autogen, "#include <device/pci.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001803 fprintf(autogen, "#include <static.h>\n");
Nico Huber8e1ea522020-06-03 10:20:07 -07001804 emit_chip_headers(autogen, chip_header.next);
1805 fprintf(autogen, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
Furquan Shaikh79e84122018-05-30 15:09:09 -07001806
Nico Huber17e9bcb2019-09-20 12:05:51 +02001807 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001808 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001809 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Nico Huber8e1ea522020-06-03 10:20:07 -07001810 walk_device_tree(autogen, autohead, &base_root_dev, update_references);
1811 fprintf(autogen, "\n/* chip configs */\n");
1812 emit_chip_configs(autogen);
Furquan Shaikh93198262018-06-03 04:22:17 -07001813 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001814 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001815
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001816 /* Expose static devicenames to global namespace. */
1817 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001818 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001819
Nico Huber17e9bcb2019-09-20 12:05:51 +02001820 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1821 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001822 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001823
Patrick Georgi114e7b22010-05-05 11:19:50 +00001824 return 0;
1825}