blob: dbb266b34663e38f71dfccce1625d3baf956f203 [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
72/* Root device of override tree (if applicable). */
73static struct device override_root_dev;
74
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070075static struct chip_instance mainboard_instance;
76
Furquan Shaikhde39fc72018-06-11 04:26:45 -070077static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070078 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070079 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070080};
81
Furquan Shaikhde39fc72018-06-11 04:26:45 -070082static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070083 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070084 .chip_instance = &mainboard_instance,
85 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070086 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070087 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070088 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070089};
90
Furquan Shaikh39ac7972018-06-21 18:44:32 -070091static struct bus override_root_bus = {
92 .id = 0,
93 .dev = &override_root_dev,
94};
95
96static struct device override_root_dev = {
97 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -070098 /*
99 * Override tree root device points to the same mainboard chip instance
100 * as the base tree root device. It should not cause any side-effects
101 * since the mainboard chip instance pointer in override tree will just
102 * be ignored.
103 */
104 .chip_instance = &mainboard_instance,
105 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700106 .parent = &override_root_bus,
107 .enabled = 1,
108 .bus = &override_root_bus,
109};
110
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700111static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000112 .name = "mainboard",
113 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700114 .instance = &mainboard_instance,
115};
116
117static struct chip_instance mainboard_instance = {
118 .id = 0,
119 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000120};
121
Furquan Shaikh93198262018-06-03 04:22:17 -0700122/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700123struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000124
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700125struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700126 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700127 struct queue_entry *next;
128 struct queue_entry *prev;
129};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700130
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700131#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700132
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700133static void *s_alloc(const char *f, size_t s)
134{
135 void *data = calloc(1, s);
136 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530137 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700138 exit(1);
139 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700140 return data;
141}
142
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700143static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700144{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700145 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700146
147 e->data = data;
148 e->next = e->prev = e;
149 return e;
150}
151
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700152static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700153{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700154 struct queue_entry *tmp = new_queue_entry(data);
155 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700156
157 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700158 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700159 return;
160 }
161
162 q->prev->next = tmp;
163 tmp->prev = q->prev;
164 q->prev = tmp;
165 tmp->next = q;
166}
167
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700168static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700169{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700170 struct queue_entry *q = *q_head;
171 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700172 void *data;
173
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700174 if (!q)
175 return NULL;
176
177 tmp = q->prev;
178
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700180 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700181 else {
182 tmp->prev->next = q;
183 q->prev = tmp->prev;
184 }
185
186 data = tmp->data;
187 free(tmp);
188
189 return data;
190}
191
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700192static void *dequeue_head(struct queue_entry **q_head)
193{
194 struct queue_entry *q = *q_head;
195 struct queue_entry *tmp = q;
196 void *data;
197
198 if (!q)
199 return NULL;
200
201 if (q->next == q)
202 *q_head = NULL;
203 else {
204 q->next->prev = q->prev;
205 q->prev->next = q->next;
206 *q_head = q->next;
207 }
208
209 data = tmp->data;
210 free(tmp);
211
212 return data;
213}
214
215static void *peek_queue_head(struct queue_entry *q_head)
216{
217 if (!q_head)
218 return NULL;
219
220 return q_head->data;
221}
222
223static struct queue_entry *chip_q_head;
224
225void chip_enqueue_tail(void *data)
226{
227 enqueue_tail(&chip_q_head, data);
228}
229
230void *chip_dequeue_tail(void)
231{
232 return dequeue_tail(&chip_q_head);
233}
234
Martin Rothbec07532016-08-05 18:32:18 -0600235int yywrap(void)
236{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000237 return 1;
238}
239
Martin Rothbec07532016-08-05 18:32:18 -0600240void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000241{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000242 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600243 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000244 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000245}
246
Martin Rothbec07532016-08-05 18:32:18 -0600247char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200248{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200249 char *b, *c;
250 b = c = strdup(str);
251 while (c && *c) {
252 if ((mode == SPLIT_1ST) && (*c == '/')) {
253 *c = 0;
254 break;
255 }
Martin Rothbec07532016-08-05 18:32:18 -0600256 if (*c == '/')
257 *c = '_';
258 if (*c == '-')
259 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200260 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200261 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200262 if (mode == TO_LOWER)
263 *c = tolower(*c);
264 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200265 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200266 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200267}
268
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700269static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600270{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700271 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700272
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700273 while (h->next) {
274 int result = strcmp(path, h->next->name);
275 if (result == 0)
276 return h->next;
277
278 if (result < 0)
279 break;
280
281 h = h->next;
282 }
283
284 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
285 new_chip->next = h->next;
286 h->next = new_chip;
287
Patrick Georgi114e7b22010-05-05 11:19:50 +0000288 new_chip->chiph_exists = 1;
289 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700290 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000291
292 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700293 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700294 sprintf(chip_h, "src/%s", path);
295 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100296 /* root_complex gets away without a separate directory, but
297 * exists on on pretty much all AMD chipsets.
298 */
299 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300300 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
301 path);
302 exit(1);
303 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700304 }
305
Martin Roth824255e2016-08-05 17:40:39 -0600306 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200307
Martin Roth824255e2016-08-05 17:40:39 -0600308 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600309 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000310
Patrick Georgi1f688802014-08-03 15:51:19 +0200311 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700312
Patrick Georgi114e7b22010-05-05 11:19:50 +0000313 return new_chip;
314}
315
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700316struct chip_instance *new_chip_instance(char *path)
317{
318 struct chip *chip = get_chip(path);
319 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
320
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700321 instance->chip = chip;
322 instance->next = chip->instance;
323 chip->instance = instance;
324
325 return instance;
326}
327
Duncan Laurie47b7b342020-05-15 15:39:08 -0700328/* List of fw_config fields added during parsing. */
329static struct fw_config_field *fw_config_fields;
330
331static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
332 const char *name)
333{
334 struct fw_config_option *option = field->options;
335
336 while (option && option->name) {
337 if (!strcmp(option->name, name))
338 return option;
339 option = option->next;
340 }
341 return NULL;
342}
343
344static struct fw_config_field *find_fw_config_field(const char *name)
345{
346 struct fw_config_field *field = fw_config_fields;
347
348 while (field && field->name) {
349 if (!strcmp(field->name, name))
350 return field;
351 field = field->next;
352 }
353 return NULL;
354}
355
356struct fw_config_field *get_fw_config_field(const char *name)
357{
358 struct fw_config_field *field = find_fw_config_field(name);
359
360 /* Fail if the field does not exist, new fields must be added with a mask. */
361 if (!field) {
362 printf("ERROR: fw_config field not found: %s\n", name);
363 exit(1);
364 }
365 return field;
366}
367
368static void append_fw_config_field(struct fw_config_field *add)
369{
370 struct fw_config_field *field = fw_config_fields;
371
372 if (!fw_config_fields) {
373 fw_config_fields = add;
374 } else {
375 while (field && field->next)
376 field = field->next;
377 field->next = add;
378 }
379}
380
381struct fw_config_field *new_fw_config_field(const char *name,
382 unsigned int start_bit, unsigned int end_bit)
383{
384 struct fw_config_field *field = find_fw_config_field(name);
385
386 /* Check that field is within 32bits. */
387 if (start_bit > end_bit || end_bit > 31) {
388 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
389 start_bit, end_bit);
390 exit(1);
391 }
392
393 /* Don't allow re-defining a field, only adding new fields. */
394 if (field) {
395 printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n",
396 name, start_bit, end_bit, field->start_bit, field->end_bit);
397 exit(1);
398 }
399
400 /* Check for overlap with an existing field. */
401 field = fw_config_fields;
402 while (field) {
403 /* Check if the mask overlaps. */
404 if (start_bit <= field->end_bit && end_bit >= field->start_bit) {
405 printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n",
406 name, start_bit, end_bit,
407 field->name, field->start_bit, field->end_bit);
408 exit(1);
409 }
410 field = field->next;
411 }
412
413 field = S_ALLOC(sizeof(*field));
414 field->name = name;
415 field->start_bit = start_bit;
416 field->end_bit = end_bit;
417 append_fw_config_field(field);
418
419 return field;
420}
421
422static void append_fw_config_option_to_field(struct fw_config_field *field,
423 struct fw_config_option *add)
424{
425 struct fw_config_option *option = field->options;
426
427 if (!option) {
428 field->options = add;
429 } else {
430 while (option && option->next)
431 option = option->next;
432 option->next = add;
433 }
434}
435
436void add_fw_config_option(struct fw_config_field *field, const char *name, unsigned int value)
437{
438 struct fw_config_option *option;
439 uint32_t field_max_value;
440
441 /* Check that option value fits within field mask. */
442 field_max_value = (1 << (1 + field->end_bit - field->start_bit)) - 1;
443 if (value > field_max_value) {
444 printf("ERROR: fw_config option %s:%s value %u larger than field max %u\n",
445 field->name, name, value, field_max_value);
446 exit(1);
447 }
448
449 /* Check for existing option with this name or value. */
450 option = field->options;
451 while (option) {
452 if (!strcmp(option->name, name)) {
453 printf("ERROR: fw_config option name %s:%s already exists\n",
454 field->name, name);
455 exit(1);
456 }
457 /* Compare values. */
458 if (value == option->value) {
459 printf("ERROR: fw_config option %s:%s[%u] redefined as %s\n",
460 field->name, option->name, value, name);
461 exit(1);
462 }
463 option = option->next;
464 }
465
466 option = S_ALLOC(sizeof(*option));
467 option->name = name;
468 option->value = value;
469
470 /* Add option to the current field. */
471 append_fw_config_option_to_field(field, option);
472}
473
474static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
475{
476 struct fw_config_probe *probe = dev->probe;
477
478 if (!probe) {
479 dev->probe = add;
480 } else {
481 while (probe && probe->next)
482 probe = probe->next;
483 probe->next = add;
484 }
485}
486
487void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
488{
489 struct fw_config_probe *probe;
490
491 probe = bus->dev->probe;
492 while (probe) {
493 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
494 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
495 exit(1);
496 }
497 probe = probe->next;
498 }
499
500 probe = S_ALLOC(sizeof(*probe));
501 probe->field = field;
502 probe->option = option;
503
504 append_fw_config_probe_to_dev(bus->dev, probe);
505}
506
507static void emit_fw_config(FILE *fil)
508{
509 struct fw_config_field *field = fw_config_fields;
510
511 if (!field)
512 return;
513
514 fprintf(fil, "\n/* firmware configuration */\n");
515 fprintf(fil, "#include <fw_config.h>\n");
516
517 while (field) {
518 struct fw_config_option *option = field->options;
519 uint32_t mask;
520
521 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
522 field->name, field->name);
523
524 /* Compute mask from start and end bit. */
525 mask = ((1 << (1 + field->end_bit - field->start_bit)) - 1);
526 mask <<= field->start_bit;
527
528 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%08x\n",
529 field->name, mask);
530
531 while (option) {
532 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
533 field->name, option->name, option->name);
534 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%08x\n",
535 field->name, option->name, option->value << field->start_bit);
536
537 option = option->next;
538 }
539
540 field = field->next;
541 }
542
543 fprintf(fil, "\n");
544}
545
546static int emit_fw_config_probe(FILE *fil, struct device *dev)
547{
548 struct fw_config_probe *probe = dev->probe;
549
550 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
551
552 while (probe) {
553 /* Find matching field. */
554 struct fw_config_field *field;
555 struct fw_config_option *option;
556 uint32_t mask, value;
557
558 field = find_fw_config_field(probe->field);
559 if (!field) {
560 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
561 return -1;
562 }
563 option = find_fw_config_option(field, probe->option);
564 if (!option) {
565 printf("ERROR: fw_config_probe field %s option %s not found\n",
566 probe->field, probe->option);
567 return -1;
568 }
569
570 /* Fill out the probe structure with values from emit_fw_config(). */
571 fprintf(fil, "\t{\n");
572 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
573 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
574 probe->field, probe->option);
575 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
576 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
577 probe->field, probe->option);
578 fprintf(fil, "\t},\n");
579
580 probe = probe->next;
581 }
582
583 /* Add empty entry to mark end of list. */
584 fprintf(fil, "\t{ }\n};\n");
585 return 0;
586}
587
Furquan Shaikh93198262018-06-03 04:22:17 -0700588/*
589 * Allocate a new bus for the provided device.
590 * - If this is the first bus being allocated under this device, then its id
591 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
592 * - If this is not the first bus under this device, then its id is set to 1
593 * plus the id of last bus and newly allocated bus is added to the list of
594 * buses under the device. last_bus is updated to point to the newly
595 * allocated bus.
596 */
597static void alloc_bus(struct device *dev)
598{
599 struct bus *bus = S_ALLOC(sizeof(*bus));
600
601 bus->dev = dev;
602
603 if (dev->last_bus == NULL) {
604 bus->id = 0;
605 dev->bus = bus;
606 } else {
607 bus->id = dev->last_bus->id + 1;
608 dev->last_bus->next_bus = bus;
609 }
610
611 dev->last_bus = bus;
612}
613
614/*
615 * Allocate a new device under the given parent. This function allocates a new
616 * device structure under the provided parent bus and allocates a bus structure
617 * under the newly allocated device.
618 */
619static struct device *alloc_dev(struct bus *parent)
620{
621 struct device *dev = S_ALLOC(sizeof(*dev));
622
Furquan Shaikh93198262018-06-03 04:22:17 -0700623 dev->parent = parent;
624 dev->subsystem_vendor = -1;
625 dev->subsystem_device = -1;
626
627 alloc_bus(dev);
628
629 return dev;
630}
631
632/*
633 * This function scans the children of given bus to see if any device matches
634 * the new device that is requested.
635 *
636 * Returns pointer to the node if found, else NULL.
637 */
638static struct device *get_dev(struct bus *parent, int path_a, int path_b,
639 int bustype, struct chip_instance *chip_instance)
640{
641 struct device *child = parent->children;
642
643 while (child) {
644 if ((child->path_a == path_a) && (child->path_b == path_b) &&
645 (child->bustype == bustype) &&
646 (child->chip_instance == chip_instance))
647 return child;
648
649 child = child->sibling;
650 }
651
652 return NULL;
653}
654
Furquan Shaikh27efc502018-06-22 09:19:15 -0700655/*
656 * Add given node as child of the provided parent. If this is the first child of
657 * the parent, update parent->children pointer as well.
658 */
659static void set_new_child(struct bus *parent, struct device *child)
660{
661 struct device *c = parent->children;
662 if (c) {
663 while (c->sibling)
664 c = c->sibling;
665 c->sibling = child;
666 } else
667 parent->children = child;
668
669 child->sibling = NULL;
670 child->parent = parent;
671}
672
Nico Huber8e1ea522020-06-03 10:20:07 -0700673static const struct device *find_alias(const struct device *const parent,
674 const char *const alias)
675{
676 if (parent->alias && !strcmp(parent->alias, alias))
677 return parent;
678
679 const struct bus *bus;
680 for (bus = parent->bus; bus; bus = bus->next_bus) {
681 const struct device *child;
682 for (child = bus->children; child; child = child->sibling) {
683 const struct device *const ret = find_alias(child, alias);
684 if (ret)
685 return ret;
686 }
687 }
688
689 return NULL;
690}
691
Furquan Shaikh93198262018-06-03 04:22:17 -0700692struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700693 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700694 const int bustype, const char *devnum,
Nico Huber8e1ea522020-06-03 10:20:07 -0700695 char *alias, int status)
Martin Rothbec07532016-08-05 18:32:18 -0600696{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000697 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700698 int path_a;
699 int path_b = 0;
700 struct device *new_d;
701
Nico Huber8e1ea522020-06-03 10:20:07 -0700702 /* Check for alias name conflicts. */
703 if (alias && find_alias(&base_root_dev, alias)) {
704 printf("ERROR: Alias already exists: %s\n", alias);
705 exit(1);
706 }
707
Furquan Shaikh93198262018-06-03 04:22:17 -0700708 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000709 if (*tmp == '.') {
710 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700711 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000712 }
713
Furquan Shaikh93198262018-06-03 04:22:17 -0700714 /* If device is found under parent, no need to allocate new device. */
715 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
716 if (new_d) {
717 alloc_bus(new_d);
718 return new_d;
719 }
720
721 new_d = alloc_dev(parent);
722
723 new_d->bustype = bustype;
724
725 new_d->path_a = path_a;
726 new_d->path_b = path_b;
Nico Huber8e1ea522020-06-03 10:20:07 -0700727 new_d->alias = alias;
Furquan Shaikh93198262018-06-03 04:22:17 -0700728
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800729 new_d->enabled = status & 0x01;
730 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000731 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700732 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000733
Furquan Shaikh27efc502018-06-22 09:19:15 -0700734 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000735
Furquan Shaikha9b64292018-05-31 07:52:00 -0700736 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200737 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000738 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200739 break;
740
741 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000742 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200743 break;
744
745 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700746 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200747 break;
748
749 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000750 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200751 break;
752
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800753 case CPU_CLUSTER:
754 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200755 break;
756
Aaron Durbinffda804b2014-09-03 12:40:15 -0500757 case CPU:
758 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
759 break;
760
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800761 case DOMAIN:
762 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200763 break;
764
765 case IOAPIC:
766 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
767 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700768
769 case GENERIC:
770 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
771 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800772
773 case SPI:
774 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
775 break;
776
Duncan Lauriebae9f852018-05-07 14:18:13 -0700777 case USB:
778 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
779 break;
780
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800781 case MMIO:
782 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
783 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600784
785 case ESPI:
786 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
787 break;
788
789 case LPC:
790 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
791 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000792 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800793
Patrick Georgi114e7b22010-05-05 11:19:50 +0000794 return new_d;
795}
796
Furquan Shaikh27efc502018-06-22 09:19:15 -0700797static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600798{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700799 struct resource *r = S_ALLOC(sizeof(struct resource));
800
Patrick Georgi114e7b22010-05-05 11:19:50 +0000801 r->type = type;
802 r->index = index;
803 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000804 if (dev->res) {
805 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600806 while (head->next)
807 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000808 head->next = r;
809 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000810 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000811 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000812}
813
Furquan Shaikh27efc502018-06-22 09:19:15 -0700814void add_resource(struct bus *bus, int type, int index, int base)
815{
816 new_resource(bus->dev, type, index, base);
817}
818
Nico Huberd09459f2020-05-26 22:13:09 +0200819static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600820{
Nico Huberd09459f2020-05-26 22:13:09 +0200821 struct reg *const r = S_ALLOC(sizeof(struct reg));
822 struct reg *prev = NULL;
823 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700824
Patrick Georgi114e7b22010-05-05 11:19:50 +0000825 r->key = name;
826 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200827
828 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
829 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000830 if (sort == 0) {
831 printf("ERROR: duplicate 'register' key.\n");
832 exit(1);
833 }
Nico Huberd09459f2020-05-26 22:13:09 +0200834 if (sort < 0)
835 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000836 }
Nico Huberd09459f2020-05-26 22:13:09 +0200837 r->next = cur;
838 if (prev)
839 prev->next = r;
840 else
841 *head = r;
842}
843
844void add_register(struct chip_instance *chip_instance, char *name, char *val)
845{
846 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000847}
848
Nico Huber8e1ea522020-06-03 10:20:07 -0700849void add_reference(struct chip_instance *const chip_instance,
850 char *const name, char *const alias)
851{
852 add_reg(&chip_instance->ref, name, alias);
853}
854
855static void set_reference(struct chip_instance *const chip_instance,
856 char *const name, char *const alias)
857{
858 const struct device *const dev = find_alias(&base_root_dev, alias);
859 if (!dev) {
860 printf("ERROR: Cannot find device alias '%s'.\n", alias);
861 exit(1);
862 }
863
864 char *const ref_name = S_ALLOC(strlen(dev->name) + 2);
865 sprintf(ref_name, "&%s", dev->name);
866 add_register(chip_instance, name, ref_name);
867}
868
869static void update_references(FILE *file, FILE *head, struct device *dev,
870 struct device *next)
871{
872 struct reg *ref;
873
874 for (ref = dev->chip_instance->ref; ref; ref = ref->next)
875 set_reference(dev->chip_instance, ref->key, ref->value);
876}
877
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200878void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
879 char *data_width)
880{
881 struct device *dev = bus->dev;
882
883 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
884 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
885 exit(1);
886 }
887
888 dev->smbios_slot_type = type;
889 dev->smbios_slot_length = length;
890 dev->smbios_slot_data_width = data_width;
891 dev->smbios_slot_designation = designation;
892}
893
Furquan Shaikh93198262018-06-03 04:22:17 -0700894void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600895 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000896{
Furquan Shaikh93198262018-06-03 04:22:17 -0700897 struct device *dev = bus->dev;
898
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800899 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000900 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
901 exit(1);
902 }
903
904 dev->subsystem_vendor = vendor;
905 dev->subsystem_device = device;
906 dev->inherit_subsystem = inherit;
907}
908
Furquan Shaikh93198262018-06-03 04:22:17 -0700909void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600910 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200911{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200912 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700913 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200914
Martin Rothbec07532016-08-05 18:32:18 -0600915 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
916 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200917 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
918 exit(1);
919 }
920
921 srcpin = _srcpin[3] - 'A';
922
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800923 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200924 printf("ERROR: ioapic config only allowed for PCI devices\n");
925 exit(1);
926 }
927
928 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200929 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200930 exit(1);
931 }
932 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
933 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
934}
935
Furquan Shaikh93198262018-06-03 04:22:17 -0700936static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600937{
Furquan Shaikh93198262018-06-03 04:22:17 -0700938 struct bus *bus = dev->bus;
939
940 while (bus) {
941 if (bus->children)
942 return 1;
943 bus = bus->next_bus;
944 }
945
946 return 0;
947}
948
Nico Huber17e9bcb2019-09-20 12:05:51 +0200949static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700950{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700951 static int dev_id;
952
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700953 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200954 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700955 ptr->name);
956 return;
957 }
958
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700959 char *name = S_ALLOC(10);
960 sprintf(name, "_dev%d", dev_id++);
961 ptr->name = name;
962
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200963 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700964 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200965 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700966 ptr->name);
967 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200968 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600969 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700970
Furquan Shaikh93198262018-06-03 04:22:17 -0700971 if (next)
972 return;
973
974 fprintf(fil,
975 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
976 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000977}
978
Furquan Shaikh93198262018-06-03 04:22:17 -0700979static void emit_resources(FILE *fil, struct device *ptr)
980{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700981 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700982 return;
983
984 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200985 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700986 struct resource *r = ptr->res;
987 while (r) {
988 fprintf(fil,
989 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
990 if (r->type == IRQ)
991 fprintf(fil, "IRQ");
992 if (r->type == DRQ)
993 fprintf(fil, "DRQ");
994 if (r->type == IO)
995 fprintf(fil, "IO");
996 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
997 r->base);
998 if (r->next)
999 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
1000 i++);
1001 else
1002 fprintf(fil, ".next=NULL },\n");
1003 r = r->next;
1004 }
1005
1006 fprintf(fil, "\t };\n");
1007}
1008
1009static void emit_bus(FILE *fil, struct bus *bus)
1010{
1011 fprintf(fil, "\t\t[%d] = {\n", bus->id);
1012 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
1013 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
1014 if (bus->children)
1015 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
1016
1017 if (bus->next_bus)
1018 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
1019 bus->id + 1);
1020 else
1021 fprintf(fil, "\t\t\t.next = NULL,\n");
1022 fprintf(fil, "\t\t},\n");
1023}
1024
1025static void emit_dev_links(FILE *fil, struct device *ptr)
1026{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001027 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001028 ptr->name);
1029
1030 struct bus *bus = ptr->bus;
1031
1032 while (bus) {
1033 emit_bus(fil, bus);
1034 bus = bus->next_bus;
1035 }
1036
1037 fprintf(fil, "\t};\n");
1038}
1039
Nico Huber17e9bcb2019-09-20 12:05:51 +02001040static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +02001041{
1042 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001043 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -07001044 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -07001045
Furquan Shaikhbbade242020-05-02 16:05:29 -07001046 /*
1047 * If the chip instance of device has base_chip_instance pointer set, then follow that
1048 * to update the chip instance for current device.
1049 */
1050 if (chip_ins->base_chip_instance)
1051 chip_ins = chip_ins->base_chip_instance;
1052
Duncan Laurie47b7b342020-05-15 15:39:08 -07001053 /* Emit probe structures. */
1054 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
1055 fclose(head);
1056 fclose(fil);
1057 exit(1);
1058 }
1059
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001060 if (ptr == &base_root_dev)
1061 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1062 else
1063 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1064
Furquan Shaikh93198262018-06-03 04:22:17 -07001065 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001066
1067 /*
1068 * ops field is set to default_dev_ops_root only for the root
1069 * device. For all other devices, it is set by the driver at runtime.
1070 */
1071 if (ptr == &base_root_dev)
1072 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1073 else
1074 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001075 fprintf(fil, "#endif\n");
1076 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1077 ptr->parent->id);
1078 fprintf(fil, "\t.path = {");
1079 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1080 fprintf(fil, "},\n");
1081 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001082 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001083 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001084 fprintf(fil, "\t.on_mainboard = 1,\n");
1085 if (ptr->subsystem_vendor > 0)
1086 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1087 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001088
Furquan Shaikh93198262018-06-03 04:22:17 -07001089 if (ptr->subsystem_device > 0)
1090 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1091 ptr->subsystem_device);
1092
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001093 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001094 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001095 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001096 }
1097 if (has_children)
1098 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1099 ptr->name);
1100 else
1101 fprintf(fil, "\t.link_list = NULL,\n");
1102 if (ptr->sibling)
1103 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001104 else
1105 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001106 fprintf(fil, "#if !DEVTREE_EARLY\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001107 if (ptr->probe)
1108 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Kyösti Mälkki1557a672019-06-30 10:51:31 +03001109 for (pin = 0; pin < 4; pin++) {
1110 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
1111 fprintf(fil,
1112 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
1113 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
1114
1115 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
1116 fprintf(fil,
1117 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
1118 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
1119 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001120 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1121 chip_ins->chip->name_underscore);
1122 if (chip_ins == &mainboard_instance)
1123 fprintf(fil, "\t.name = mainboard_name,\n");
1124 fprintf(fil, "#endif\n");
1125 if (chip_ins->chip->chiph_exists)
1126 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1127 chip_ins->chip->name_underscore, chip_ins->id);
1128 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001129 fprintf(fil, "\t.next=&%s,\n", next->name);
1130 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1131 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1132 fprintf(fil, "#if !DEVTREE_EARLY\n");
1133 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1134 }
1135 /* SMBIOS types start at 1, if zero it hasn't been set */
1136 if (ptr->smbios_slot_type)
1137 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1138 ptr->smbios_slot_type);
1139 if (ptr->smbios_slot_data_width)
1140 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1141 ptr->smbios_slot_data_width);
1142 if (ptr->smbios_slot_designation)
1143 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1144 ptr->smbios_slot_designation);
1145 if (ptr->smbios_slot_length)
1146 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1147 ptr->smbios_slot_length);
1148 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1149 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1150 fprintf(fil, "#endif\n");
1151 fprintf(fil, "#endif\n");
1152 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001153 fprintf(fil, "};\n");
1154
1155 emit_resources(fil, ptr);
1156
1157 if (has_children)
1158 emit_dev_links(fil, ptr);
1159}
1160
Nico Huber17e9bcb2019-09-20 12:05:51 +02001161static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001162{
1163 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001164 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
1165 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
1166 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001167 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
1168 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001169 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001170
Nico Huber17e9bcb2019-09-20 12:05:51 +02001171 if (ptr->bustype == PNP) {
1172 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
1173 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001174 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
1175 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001176 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001177}
1178
Furquan Shaikh93198262018-06-03 04:22:17 -07001179static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1180 struct device *d)
1181{
1182 while (d) {
1183 enqueue_tail(bfs_q_head, d);
1184 d = d->sibling;
1185 }
1186}
1187
1188static void add_children_to_queue(struct queue_entry **bfs_q_head,
1189 struct device *d)
1190{
1191 struct bus *bus = d->bus;
1192
1193 while (bus) {
1194 if (bus->children)
1195 add_siblings_to_queue(bfs_q_head, bus->children);
1196 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001197 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001198}
1199
Nico Huber17e9bcb2019-09-20 12:05:51 +02001200static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1201 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001202 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001203{
Furquan Shaikh93198262018-06-03 04:22:17 -07001204 struct queue_entry *bfs_q_head = NULL;
1205
1206 enqueue_tail(&bfs_q_head, ptr);
1207
1208 while ((ptr = dequeue_head(&bfs_q_head))) {
1209 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001210 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001211 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001212}
1213
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001214static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001215{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001216 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001217
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001218 while (chip) {
1219 if (chip->chiph_exists)
1220 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1221 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001222 }
1223 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1224 fprintf(fil,
1225 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001226
1227 chip = tmp;
1228 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001229 /* A lot of cpus do not define chip_operations at all, and the ones
1230 that do only initialise .name. */
1231 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1232 fprintf(fil,
1233 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1234 chip->name_underscore);
1235 } else {
1236 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1237 chip->name_underscore);
1238 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001239 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001240 }
1241 fprintf(fil, "#endif\n");
1242}
1243
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001244static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1245{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001246 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001247 instance->chip->name_underscore,
1248 instance->chip->name_underscore,
1249 instance->id);
1250
1251 if (instance->reg) {
1252 fprintf(fil, "\n");
1253 struct reg *r = instance->reg;
1254 while (r) {
1255 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1256 r = r->next;
1257 }
1258 }
1259 fprintf(fil, "};\n\n");
1260}
1261
Nico Huber8e1ea522020-06-03 10:20:07 -07001262static void emit_chip_configs(FILE *fil)
Furquan Shaikh79e84122018-05-30 15:09:09 -07001263{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001264 struct chip *chip = chip_header.next;
1265 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001266 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001267
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001268 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001269 if (!chip->chiph_exists)
1270 continue;
1271
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001272 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001273 instance = chip->instance;
1274 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001275 /*
1276 * Emit this chip instance only if there is no forwarding pointer to the
1277 * base tree chip instance.
1278 */
1279 if (instance->base_chip_instance == NULL) {
1280 instance->id = chip_id++;
1281 emit_chip_instance(fil, instance);
1282 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001283 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001284 }
1285 }
1286}
1287
Nico Huber17e9bcb2019-09-20 12:05:51 +02001288static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001289 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001290{
1291 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001292
1293 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1294 /* user already gave us a subsystem vendor/device */
1295 return;
1296 }
1297
Furquan Shaikh93198262018-06-03 04:22:17 -07001298 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001299
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001300 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001301 continue;
1302
1303 if (p->inherit_subsystem) {
1304 dev->subsystem_vendor = p->subsystem_vendor;
1305 dev->subsystem_device = p->subsystem_device;
1306 break;
1307 }
1308 }
1309}
1310
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001311static void usage(void)
1312{
Duncan Laurie51c83732020-06-09 11:20:29 -07001313 printf("usage: sconfig <options>\n");
1314 printf(" -c | --output_c : Path to output static.c file (required)\n");
1315 printf(" -r | --output_h : Path to header static.h file (required)\n");
1316 printf(" -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n");
1317 printf(" -o | --override_devtree : Path to override devicetree file (optional)\n");
Martin Rothbec07532016-08-05 18:32:18 -06001318 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001319}
1320
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001321static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001322{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001323 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001324 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001325 perror(NULL);
1326 exit(1);
1327 }
1328
Patrick Georgi114e7b22010-05-05 11:19:50 +00001329 yyrestart(filec);
1330
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001331 root_parent = parent;
1332 linenum = 0;
1333
Patrick Georgi114e7b22010-05-05 11:19:50 +00001334 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001335
Patrick Georgi114e7b22010-05-05 11:19:50 +00001336 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001337}
1338
Furquan Shaikh27efc502018-06-22 09:19:15 -07001339/*
1340 * Match device nodes from base and override tree to see if they are the same
1341 * node.
1342 */
1343static int device_match(struct device *a, struct device *b)
1344{
1345 return ((a->path_a == b->path_a) &&
1346 (a->path_b == b->path_b) &&
1347 (a->bustype == b->bustype) &&
1348 (a->chip_instance->chip ==
1349 b->chip_instance->chip));
1350}
1351
1352/*
Bill XIEc61d4152019-11-21 18:16:12 +08001353 * Match resource nodes from base and override tree to see if they are the same
1354 * node.
1355 */
1356static int res_match(struct resource *a, struct resource *b)
1357{
1358 return ((a->type == b->type) &&
1359 (a->index == b->index));
1360}
1361
1362/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001363 * Add resource to device. If resource is already present, then update its base
1364 * and index. If not, then add a new resource to the device.
1365 */
1366static void update_resource(struct device *dev, struct resource *res)
1367{
1368 struct resource *base_res = dev->res;
1369
1370 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001371 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001372 base_res->base = res->base;
1373 return;
1374 }
1375 base_res = base_res->next;
1376 }
1377
1378 new_resource(dev, res->type, res->index, res->base);
1379}
1380
1381/*
1382 * Add register to chip instance. If register is already present, then update
1383 * its value. If not, then add a new register to the chip instance.
1384 */
Nico Huber8e1ea522020-06-03 10:20:07 -07001385static void update_register(struct reg **const head, struct reg *reg)
Furquan Shaikh27efc502018-06-22 09:19:15 -07001386{
Nico Huber8e1ea522020-06-03 10:20:07 -07001387 struct reg *base_reg = *head;
Furquan Shaikh27efc502018-06-22 09:19:15 -07001388
1389 while (base_reg) {
1390 if (!strcmp(base_reg->key, reg->key)) {
1391 base_reg->value = reg->value;
1392 return;
1393 }
1394 base_reg = base_reg->next;
1395 }
1396
Nico Huber8e1ea522020-06-03 10:20:07 -07001397 add_reg(head, reg->key, reg->value);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001398}
1399
1400static void override_devicetree(struct bus *base_parent,
1401 struct bus *override_parent);
1402
1403/*
1404 * Update the base device properties using the properties of override device. In
1405 * addition to that, call override_devicetree for all the buses under the
1406 * override device.
1407 *
1408 * Override Rules:
1409 * +--------------------+--------------------------------------------+
1410 * | | |
1411 * |struct device member| Rule |
1412 * | | |
1413 * +-----------------------------------------------------------------+
1414 * | | |
1415 * | id | Unchanged. This is used to generate device |
1416 * | | structure name in static.c. So, no need to |
1417 * | | override. |
1418 * | | |
1419 * +-----------------------------------------------------------------+
1420 * | | |
1421 * | enabled | Copy enabled state from override device. |
1422 * | | This allows variants to override device |
1423 * | | state. |
1424 * | | |
1425 * +-----------------------------------------------------------------+
1426 * | | |
1427 * | subsystem_vendor | Copy from override device only if any one |
1428 * | subsystem_device | of the ids is non-zero. |
1429 * | | |
1430 * +-----------------------------------------------------------------+
1431 * | | |
1432 * | inherit_subsystem | Copy from override device only if it is |
1433 * | | non-zero. This allows variant to only |
1434 * | | enable inherit flag for a device. |
1435 * | | |
1436 * +-----------------------------------------------------------------+
1437 * | | |
1438 * | path | Unchanged since these are same for both |
1439 * | path_a | base and override device (Used for |
1440 * | path_b | matching devices). |
1441 * | | |
1442 * +-----------------------------------------------------------------+
1443 * | | |
1444 * | bustype | Unchanged since this is same for both base |
1445 * | | and override device (User for matching |
1446 * | | devices). |
1447 * | | |
1448 * +-----------------------------------------------------------------+
1449 * | | |
1450 * | pci_irq_info | Unchanged. |
1451 * | | |
1452 * +-----------------------------------------------------------------+
1453 * | | |
1454 * | parent | Unchanged. This is meaningful only within |
1455 * | sibling | the parse tree, hence not being copied. |
1456 * | | |
1457 * +-----------------------------------------------------------------+
1458 * | | |
1459 * | res | Each resource that is present in override |
1460 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001461 * | | 1. If resource of same type and index is |
1462 * | | present in base device, then base of |
1463 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001464 * | | 2. If not, then a new resource is allocated|
1465 * | | under the base device using type, index |
1466 * | | and base from override res. |
1467 * | | |
1468 * +-----------------------------------------------------------------+
1469 * | | |
Nico Huber8e1ea522020-06-03 10:20:07 -07001470 * | ref | Each reference that is present in override |
1471 * | | device is copied over to base device with |
1472 * | | the same rules as registers. |
1473 * | | |
1474 * +-----------------------------------------------------------------+
1475 * | | |
1476 * | alias | Base device alias is copied to override. |
1477 * | | Override devices cannot change/remove an |
1478 * | | existing alias, but they can add an alias |
1479 * | | if one does not exist. |
1480 * | | |
1481 * +-----------------------------------------------------------------+
1482 * | | |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001483 * | chip_instance | Each register of chip_instance is copied |
1484 * | | over from override device to base device: |
1485 * | | 1. If register with same key is present in |
1486 * | | base device, then value of the register |
1487 * | | is copied. |
1488 * | | 2. If not, then a new register is allocated|
1489 * | | under the base chip_instance using key |
1490 * | | and value from override register. |
1491 * | | |
1492 * +-----------------------------------------------------------------+
1493 * | | |
1494 * | bus | Recursively call override_devicetree on |
1495 * | last_bus | each bus of override device. It is assumed |
1496 * | | that bus with id X under base device |
1497 * | | to bus with id X under override device. If |
1498 * | | override device has more buses than base |
1499 * | | device, then new buses are allocated under |
1500 * | | base device. |
1501 * | | |
1502 * +-----------------------------------------------------------------+
1503 */
1504static void update_device(struct device *base_dev, struct device *override_dev)
1505{
1506 /*
1507 * Copy the enabled state of override device to base device. This allows
1508 * override tree to enable or disable a particular device.
1509 */
1510 base_dev->enabled = override_dev->enabled;
1511
1512 /*
1513 * Copy subsystem vendor and device ids from override device to base
1514 * device only if the ids are non-zero in override device. Else, honor
1515 * the values in base device.
1516 */
1517 if (override_dev->subsystem_vendor ||
1518 override_dev->subsystem_device) {
1519 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1520 base_dev->subsystem_device = override_dev->subsystem_device;
1521 }
1522
1523 /*
1524 * Copy value of inherity_subsystem from override device to base device
1525 * only if it is non-zero in override device. This allows override
1526 * tree to only enable inhert flag for a device.
1527 */
1528 if (override_dev->inherit_subsystem)
1529 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1530
1531 /*
1532 * Copy resources of override device to base device.
1533 * 1. If resource is already present in base device, then index and base
1534 * of the resource will be copied over.
1535 * 2. If resource is not already present in base device, a new resource
1536 * will be allocated.
1537 */
1538 struct resource *res = override_dev->res;
1539 while (res) {
1540 update_resource(base_dev, res);
1541 res = res->next;
1542 }
1543
1544 /*
1545 * Copy registers of override chip instance to base chip instance.
1546 * 1. If register key is already present in base chip instance, then
1547 * value for the register is copied over.
1548 * 2. If register key is not already present in base chip instance, then
1549 * a new register will be allocated.
1550 */
1551 struct reg *reg = override_dev->chip_instance->reg;
1552 while (reg) {
Nico Huber8e1ea522020-06-03 10:20:07 -07001553 update_register(&base_dev->chip_instance->reg, reg);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001554 reg = reg->next;
1555 }
1556
Nico Huber8e1ea522020-06-03 10:20:07 -07001557 /* Copy references just as with registers. */
1558 reg = override_dev->chip_instance->ref;
1559 while (reg) {
1560 update_register(&base_dev->chip_instance->ref, reg);
1561 reg = reg->next;
1562 }
1563
1564 /* Check for alias name conflicts. */
1565 if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) {
1566 printf("ERROR: alias already exists: %s\n", override_dev->alias);
1567 exit(1);
1568 }
1569
1570 /*
1571 * Copy alias from base device.
1572 *
1573 * Override devices cannot change/remove an existing alias,
1574 * but they can add an alias to a device if one does not exist yet.
1575 */
1576 if (base_dev->alias)
1577 override_dev->alias = base_dev->alias;
1578 else
1579 base_dev->alias = override_dev->alias;
1580
Furquan Shaikh27efc502018-06-22 09:19:15 -07001581 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001582 * Use probe list from override device in place of base device, in order
1583 * to allow an override to remove a probe from the base device.
1584 */
1585 base_dev->probe = override_dev->probe;
1586
1587 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001588 * Update base_chip_instance member in chip instance of override tree to forward it to
1589 * the chip instance in base tree.
1590 */
1591 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1592
1593 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001594 * Now that the device properties are all copied over, look at each bus
1595 * of the override device and run override_devicetree in a recursive
1596 * manner. The assumption here is that first bus of override device
1597 * corresponds to first bus of base device and so on. If base device has
1598 * lesser buses than override tree, then new buses are allocated for it.
1599 */
1600 struct bus *override_bus = override_dev->bus;
1601 struct bus *base_bus = base_dev->bus;
1602
1603 while (override_bus) {
1604
1605 /*
1606 * If we have more buses in override tree device, then allocate
1607 * a new bus for the base tree device as well.
1608 */
1609 if (!base_bus) {
1610 alloc_bus(base_dev);
1611 base_bus = base_dev->last_bus;
1612 }
1613
1614 override_devicetree(base_dev->bus, override_dev->bus);
1615
1616 override_bus = override_bus->next_bus;
1617 base_bus = base_bus->next_bus;
1618 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001619}
1620
1621/*
1622 * Perform copy of device and properties from override parent to base parent.
1623 * This function walks through the override tree in a depth-first manner
1624 * performing following actions:
1625 * 1. If matching device is found in base tree, then copy the properties of
1626 * override device to base tree device. Call override_devicetree recursively on
1627 * the bus of override device.
1628 * 2. If matching device is not found in base tree, then set override tree
1629 * device as new child of base_parent and update the chip pointers in override
1630 * device subtree to ensure the nodes do not point to override tree chip
1631 * instance.
1632 */
1633static void override_devicetree(struct bus *base_parent,
1634 struct bus *override_parent)
1635{
1636 struct device *base_child;
1637 struct device *override_child = override_parent->children;
1638 struct device *next_child;
1639
1640 while (override_child) {
1641
1642 /* Look for a matching device in base tree. */
1643 for (base_child = base_parent->children;
1644 base_child; base_child = base_child->sibling) {
1645 if (device_match(base_child, override_child))
1646 break;
1647 }
1648
1649 next_child = override_child->sibling;
1650
1651 /*
1652 * If matching device is found, copy properties of
1653 * override_child to base_child.
1654 */
1655 if (base_child)
1656 update_device(base_child, override_child);
1657 else {
1658 /*
1659 * If matching device is not found, set override_child
1660 * as a new child of base_parent.
1661 */
1662 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001663 }
1664
1665 override_child = next_child;
1666 }
1667}
1668
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001669static void parse_override_devicetree(const char *file, struct device *dev)
1670{
1671 parse_devicetree(file, dev->bus);
1672
1673 if (!dev_has_children(dev)) {
1674 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1675 exit(1);
1676 }
1677
1678 override_devicetree(&base_root_bus, dev->bus);
1679}
1680
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001681int main(int argc, char **argv)
1682{
Duncan Laurie51c83732020-06-09 11:20:29 -07001683 static const struct option long_options[] = {
1684 { "mainboard_devtree", 1, NULL, 'm' },
1685 { "override_devtree", 1, NULL, 'o' },
1686 { "output_c", 1, NULL, 'c' },
1687 { "output_h", 1, NULL, 'r' },
1688 { "help", 1, NULL, 'h' },
1689 { }
1690 };
1691 const char *override_devtree = NULL;
1692 const char *base_devtree = NULL;
1693 const char *outputc = NULL;
1694 const char *outputh = NULL;
1695 int opt, option_index;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001696
Duncan Laurie51c83732020-06-09 11:20:29 -07001697 while ((opt = getopt_long(argc, argv, "m:o:c:r:h", long_options,
1698 &option_index)) != EOF) {
1699 switch (opt) {
1700 case 'm':
1701 base_devtree = strdup(optarg);
1702 break;
1703 case 'o':
1704 override_devtree = strdup(optarg);
1705 break;
1706 case 'c':
1707 outputc = strdup(optarg);
1708 break;
1709 case 'r':
1710 outputh = strdup(optarg);
1711 break;
1712 case 'h':
1713 default:
1714 usage();
1715 }
1716 }
1717
1718 if (!base_devtree || !outputc || !outputh)
1719 usage();
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001720
1721 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001722
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001723 if (override_devtree)
1724 parse_override_devicetree(override_devtree, &override_root_dev);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001725
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001726 FILE *autogen = fopen(outputc, "w");
1727 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001728 fprintf(stderr, "Could not open file '%s' for writing: ",
1729 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001730 perror(NULL);
1731 exit(1);
1732 }
1733
Nico Huber17e9bcb2019-09-20 12:05:51 +02001734 FILE *autohead = fopen(outputh, "w");
1735 if (!autohead) {
1736 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1737 perror(NULL);
1738 fclose(autogen);
1739 exit(1);
1740 }
1741 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1742 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1743 fprintf(autohead, "#include <device/device.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001744 emit_fw_config(autohead);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001745
Nico Huber57bbcb32020-05-26 22:39:47 +02001746 fprintf(autogen, "#include <device/device.h>\n");
1747 fprintf(autogen, "#include <device/pci.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001748 fprintf(autogen, "#include <static.h>\n");
Nico Huber8e1ea522020-06-03 10:20:07 -07001749 emit_chip_headers(autogen, chip_header.next);
1750 fprintf(autogen, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
Furquan Shaikh79e84122018-05-30 15:09:09 -07001751
Nico Huber17e9bcb2019-09-20 12:05:51 +02001752 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001753 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001754 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Nico Huber8e1ea522020-06-03 10:20:07 -07001755 walk_device_tree(autogen, autohead, &base_root_dev, update_references);
1756 fprintf(autogen, "\n/* chip configs */\n");
1757 emit_chip_configs(autogen);
Furquan Shaikh93198262018-06-03 04:22:17 -07001758 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001759 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001760
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001761 /* Expose static devicenames to global namespace. */
1762 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001763 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001764
Nico Huber17e9bcb2019-09-20 12:05:51 +02001765 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1766 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001767 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001768
Patrick Georgi114e7b22010-05-05 11:19:50 +00001769 return 0;
1770}