blob: 170acadec7536479313c1618d276b827ed187a80 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* sconfig, coreboot device tree compiler */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi114e7b22010-05-05 11:19:50 +00003
Patrick Georgi2dbfcb72012-05-30 16:26:30 +02004#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +02005/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
6#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +02007#include <commonlib/helpers.h>
Duncan Laurie47b7b342020-05-15 15:39:08 -07008#include <stdint.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +00009#include "sconfig.h"
10#include "sconfig.tab.h"
11
Patrick Georgi7fc9e292010-07-15 15:59:07 +000012extern int linenum;
13
Furquan Shaikh27efc502018-06-22 09:19:15 -070014/*
15 * Maintains list of all the unique chip structures for the board.
16 * This is shared across base and override device trees since we need to
17 * generate headers for all chips added by both the trees.
18 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070019static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070020
Kyösti Mälkki472d9022011-12-05 20:33:55 +020021typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020022 UNSLASH,
23 SPLIT_1ST,
24 TO_LOWER,
25 TO_UPPER,
26} translate_t;
27
Furquan Shaikh93198262018-06-03 04:22:17 -070028/*
29 * Mainboard is assumed to have a root device whose bus is the parent of all the
30 * devices that are added by parsing the devicetree file. This device has a
31 * mainboard chip instance associated with it.
32 *
33 *
34 *
35 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070036 * | Root device | | Mainboard |
37 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070038 * | | | chip_instance | (mainboard_instance)|
39 * | +------------------------+ | |
40 * | | +----------------------+
41 * | | bus |
42 * | parent v |
43 * | +-------------------+ |
44 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070045 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070046 * | | |
47 * +-------------------+ |
48 * | |
49 * | children | chip
50 * v |
51 * X |
52 * (new devices will |
53 * be added here as |
54 * children) |
55 * |
56 * |
57 * |
58 * +-------+----------+
59 * | |
60 * | Mainboard chip +----------->X (new chips will be
61 * | (mainboard_chip) | added here)
62 * | |
63 * +------------------+
64 *
65 *
66 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070067
68/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070069static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070070
71/* Root device of override tree (if applicable). */
72static struct device override_root_dev;
73
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070074static struct chip_instance mainboard_instance;
75
Furquan Shaikhde39fc72018-06-11 04:26:45 -070076static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070077 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070078 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070079};
80
Furquan Shaikhde39fc72018-06-11 04:26:45 -070081static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070082 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070083 .chip_instance = &mainboard_instance,
84 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070085 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070086 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070087 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070088};
89
Furquan Shaikh39ac7972018-06-21 18:44:32 -070090static struct bus override_root_bus = {
91 .id = 0,
92 .dev = &override_root_dev,
93};
94
95static struct device override_root_dev = {
96 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -070097 /*
98 * Override tree root device points to the same mainboard chip instance
99 * as the base tree root device. It should not cause any side-effects
100 * since the mainboard chip instance pointer in override tree will just
101 * be ignored.
102 */
103 .chip_instance = &mainboard_instance,
104 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700105 .parent = &override_root_bus,
106 .enabled = 1,
107 .bus = &override_root_bus,
108};
109
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700110static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000111 .name = "mainboard",
112 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700113 .instance = &mainboard_instance,
114};
115
116static struct chip_instance mainboard_instance = {
117 .id = 0,
118 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000119};
120
Furquan Shaikh93198262018-06-03 04:22:17 -0700121/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700122struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000123
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700124struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700125 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700126 struct queue_entry *next;
127 struct queue_entry *prev;
128};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700129
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700130#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700131
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700132static void *s_alloc(const char *f, size_t s)
133{
134 void *data = calloc(1, s);
135 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530136 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700137 exit(1);
138 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700139 return data;
140}
141
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700142static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700143{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700144 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700145
146 e->data = data;
147 e->next = e->prev = e;
148 return e;
149}
150
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700151static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700152{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700153 struct queue_entry *tmp = new_queue_entry(data);
154 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700155
156 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700157 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700158 return;
159 }
160
161 q->prev->next = tmp;
162 tmp->prev = q->prev;
163 q->prev = tmp;
164 tmp->next = q;
165}
166
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700167static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700168{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700169 struct queue_entry *q = *q_head;
170 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700171 void *data;
172
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700173 if (!q)
174 return NULL;
175
176 tmp = q->prev;
177
Furquan Shaikh79e84122018-05-30 15:09:09 -0700178 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700179 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700180 else {
181 tmp->prev->next = q;
182 q->prev = tmp->prev;
183 }
184
185 data = tmp->data;
186 free(tmp);
187
188 return data;
189}
190
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700191static void *dequeue_head(struct queue_entry **q_head)
192{
193 struct queue_entry *q = *q_head;
194 struct queue_entry *tmp = q;
195 void *data;
196
197 if (!q)
198 return NULL;
199
200 if (q->next == q)
201 *q_head = NULL;
202 else {
203 q->next->prev = q->prev;
204 q->prev->next = q->next;
205 *q_head = q->next;
206 }
207
208 data = tmp->data;
209 free(tmp);
210
211 return data;
212}
213
214static void *peek_queue_head(struct queue_entry *q_head)
215{
216 if (!q_head)
217 return NULL;
218
219 return q_head->data;
220}
221
222static struct queue_entry *chip_q_head;
223
224void chip_enqueue_tail(void *data)
225{
226 enqueue_tail(&chip_q_head, data);
227}
228
229void *chip_dequeue_tail(void)
230{
231 return dequeue_tail(&chip_q_head);
232}
233
Martin Rothbec07532016-08-05 18:32:18 -0600234int yywrap(void)
235{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000236 return 1;
237}
238
Martin Rothbec07532016-08-05 18:32:18 -0600239void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000240{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000241 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600242 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000243 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000244}
245
Martin Rothbec07532016-08-05 18:32:18 -0600246char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200247{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200248 char *b, *c;
249 b = c = strdup(str);
250 while (c && *c) {
251 if ((mode == SPLIT_1ST) && (*c == '/')) {
252 *c = 0;
253 break;
254 }
Martin Rothbec07532016-08-05 18:32:18 -0600255 if (*c == '/')
256 *c = '_';
257 if (*c == '-')
258 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200259 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200260 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200261 if (mode == TO_LOWER)
262 *c = tolower(*c);
263 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200264 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200265 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200266}
267
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700268static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600269{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700270 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700271
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700272 while (h->next) {
273 int result = strcmp(path, h->next->name);
274 if (result == 0)
275 return h->next;
276
277 if (result < 0)
278 break;
279
280 h = h->next;
281 }
282
283 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
284 new_chip->next = h->next;
285 h->next = new_chip;
286
Patrick Georgi114e7b22010-05-05 11:19:50 +0000287 new_chip->chiph_exists = 1;
288 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700289 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000290
291 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700292 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700293 sprintf(chip_h, "src/%s", path);
294 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100295 /* root_complex gets away without a separate directory, but
296 * exists on on pretty much all AMD chipsets.
297 */
298 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300299 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
300 path);
301 exit(1);
302 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700303 }
304
Martin Roth824255e2016-08-05 17:40:39 -0600305 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200306
Martin Roth824255e2016-08-05 17:40:39 -0600307 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600308 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000309
Patrick Georgi1f688802014-08-03 15:51:19 +0200310 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700311
Patrick Georgi114e7b22010-05-05 11:19:50 +0000312 return new_chip;
313}
314
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700315struct chip_instance *new_chip_instance(char *path)
316{
317 struct chip *chip = get_chip(path);
318 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
319
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700320 instance->chip = chip;
321 instance->next = chip->instance;
322 chip->instance = instance;
323
324 return instance;
325}
326
Duncan Laurie47b7b342020-05-15 15:39:08 -0700327/* List of fw_config fields added during parsing. */
328static struct fw_config_field *fw_config_fields;
329
330static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
331 const char *name)
332{
333 struct fw_config_option *option = field->options;
334
335 while (option && option->name) {
336 if (!strcmp(option->name, name))
337 return option;
338 option = option->next;
339 }
340 return NULL;
341}
342
343static struct fw_config_field *find_fw_config_field(const char *name)
344{
345 struct fw_config_field *field = fw_config_fields;
346
347 while (field && field->name) {
348 if (!strcmp(field->name, name))
349 return field;
350 field = field->next;
351 }
352 return NULL;
353}
354
355struct fw_config_field *get_fw_config_field(const char *name)
356{
357 struct fw_config_field *field = find_fw_config_field(name);
358
359 /* Fail if the field does not exist, new fields must be added with a mask. */
360 if (!field) {
361 printf("ERROR: fw_config field not found: %s\n", name);
362 exit(1);
363 }
364 return field;
365}
366
367static void append_fw_config_field(struct fw_config_field *add)
368{
369 struct fw_config_field *field = fw_config_fields;
370
371 if (!fw_config_fields) {
372 fw_config_fields = add;
373 } else {
374 while (field && field->next)
375 field = field->next;
376 field->next = add;
377 }
378}
379
380struct fw_config_field *new_fw_config_field(const char *name,
381 unsigned int start_bit, unsigned int end_bit)
382{
383 struct fw_config_field *field = find_fw_config_field(name);
384
385 /* Check that field is within 32bits. */
386 if (start_bit > end_bit || end_bit > 31) {
387 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
388 start_bit, end_bit);
389 exit(1);
390 }
391
392 /* Don't allow re-defining a field, only adding new fields. */
393 if (field) {
394 printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n",
395 name, start_bit, end_bit, field->start_bit, field->end_bit);
396 exit(1);
397 }
398
399 /* Check for overlap with an existing field. */
400 field = fw_config_fields;
401 while (field) {
402 /* Check if the mask overlaps. */
403 if (start_bit <= field->end_bit && end_bit >= field->start_bit) {
404 printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n",
405 name, start_bit, end_bit,
406 field->name, field->start_bit, field->end_bit);
407 exit(1);
408 }
409 field = field->next;
410 }
411
412 field = S_ALLOC(sizeof(*field));
413 field->name = name;
414 field->start_bit = start_bit;
415 field->end_bit = end_bit;
416 append_fw_config_field(field);
417
418 return field;
419}
420
421static void append_fw_config_option_to_field(struct fw_config_field *field,
422 struct fw_config_option *add)
423{
424 struct fw_config_option *option = field->options;
425
426 if (!option) {
427 field->options = add;
428 } else {
429 while (option && option->next)
430 option = option->next;
431 option->next = add;
432 }
433}
434
435void add_fw_config_option(struct fw_config_field *field, const char *name, unsigned int value)
436{
437 struct fw_config_option *option;
438 uint32_t field_max_value;
439
440 /* Check that option value fits within field mask. */
441 field_max_value = (1 << (1 + field->end_bit - field->start_bit)) - 1;
442 if (value > field_max_value) {
443 printf("ERROR: fw_config option %s:%s value %u larger than field max %u\n",
444 field->name, name, value, field_max_value);
445 exit(1);
446 }
447
448 /* Check for existing option with this name or value. */
449 option = field->options;
450 while (option) {
451 if (!strcmp(option->name, name)) {
452 printf("ERROR: fw_config option name %s:%s already exists\n",
453 field->name, name);
454 exit(1);
455 }
456 /* Compare values. */
457 if (value == option->value) {
458 printf("ERROR: fw_config option %s:%s[%u] redefined as %s\n",
459 field->name, option->name, value, name);
460 exit(1);
461 }
462 option = option->next;
463 }
464
465 option = S_ALLOC(sizeof(*option));
466 option->name = name;
467 option->value = value;
468
469 /* Add option to the current field. */
470 append_fw_config_option_to_field(field, option);
471}
472
473static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
474{
475 struct fw_config_probe *probe = dev->probe;
476
477 if (!probe) {
478 dev->probe = add;
479 } else {
480 while (probe && probe->next)
481 probe = probe->next;
482 probe->next = add;
483 }
484}
485
486void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
487{
488 struct fw_config_probe *probe;
489
490 probe = bus->dev->probe;
491 while (probe) {
492 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
493 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
494 exit(1);
495 }
496 probe = probe->next;
497 }
498
499 probe = S_ALLOC(sizeof(*probe));
500 probe->field = field;
501 probe->option = option;
502
503 append_fw_config_probe_to_dev(bus->dev, probe);
504}
505
506static void emit_fw_config(FILE *fil)
507{
508 struct fw_config_field *field = fw_config_fields;
509
510 if (!field)
511 return;
512
513 fprintf(fil, "\n/* firmware configuration */\n");
514 fprintf(fil, "#include <fw_config.h>\n");
515
516 while (field) {
517 struct fw_config_option *option = field->options;
518 uint32_t mask;
519
520 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
521 field->name, field->name);
522
523 /* Compute mask from start and end bit. */
524 mask = ((1 << (1 + field->end_bit - field->start_bit)) - 1);
525 mask <<= field->start_bit;
526
527 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%08x\n",
528 field->name, mask);
529
530 while (option) {
531 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
532 field->name, option->name, option->name);
533 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%08x\n",
534 field->name, option->name, option->value << field->start_bit);
535
536 option = option->next;
537 }
538
539 field = field->next;
540 }
541
542 fprintf(fil, "\n");
543}
544
545static int emit_fw_config_probe(FILE *fil, struct device *dev)
546{
547 struct fw_config_probe *probe = dev->probe;
548
549 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
550
551 while (probe) {
552 /* Find matching field. */
553 struct fw_config_field *field;
554 struct fw_config_option *option;
555 uint32_t mask, value;
556
557 field = find_fw_config_field(probe->field);
558 if (!field) {
559 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
560 return -1;
561 }
562 option = find_fw_config_option(field, probe->option);
563 if (!option) {
564 printf("ERROR: fw_config_probe field %s option %s not found\n",
565 probe->field, probe->option);
566 return -1;
567 }
568
569 /* Fill out the probe structure with values from emit_fw_config(). */
570 fprintf(fil, "\t{\n");
571 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
572 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
573 probe->field, probe->option);
574 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
575 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
576 probe->field, probe->option);
577 fprintf(fil, "\t},\n");
578
579 probe = probe->next;
580 }
581
582 /* Add empty entry to mark end of list. */
583 fprintf(fil, "\t{ }\n};\n");
584 return 0;
585}
586
Furquan Shaikh93198262018-06-03 04:22:17 -0700587/*
588 * Allocate a new bus for the provided device.
589 * - If this is the first bus being allocated under this device, then its id
590 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
591 * - If this is not the first bus under this device, then its id is set to 1
592 * plus the id of last bus and newly allocated bus is added to the list of
593 * buses under the device. last_bus is updated to point to the newly
594 * allocated bus.
595 */
596static void alloc_bus(struct device *dev)
597{
598 struct bus *bus = S_ALLOC(sizeof(*bus));
599
600 bus->dev = dev;
601
602 if (dev->last_bus == NULL) {
603 bus->id = 0;
604 dev->bus = bus;
605 } else {
606 bus->id = dev->last_bus->id + 1;
607 dev->last_bus->next_bus = bus;
608 }
609
610 dev->last_bus = bus;
611}
612
613/*
614 * Allocate a new device under the given parent. This function allocates a new
615 * device structure under the provided parent bus and allocates a bus structure
616 * under the newly allocated device.
617 */
618static struct device *alloc_dev(struct bus *parent)
619{
620 struct device *dev = S_ALLOC(sizeof(*dev));
621
Furquan Shaikh93198262018-06-03 04:22:17 -0700622 dev->parent = parent;
623 dev->subsystem_vendor = -1;
624 dev->subsystem_device = -1;
625
626 alloc_bus(dev);
627
628 return dev;
629}
630
631/*
632 * This function scans the children of given bus to see if any device matches
633 * the new device that is requested.
634 *
635 * Returns pointer to the node if found, else NULL.
636 */
637static struct device *get_dev(struct bus *parent, int path_a, int path_b,
638 int bustype, struct chip_instance *chip_instance)
639{
640 struct device *child = parent->children;
641
642 while (child) {
643 if ((child->path_a == path_a) && (child->path_b == path_b) &&
644 (child->bustype == bustype) &&
645 (child->chip_instance == chip_instance))
646 return child;
647
648 child = child->sibling;
649 }
650
651 return NULL;
652}
653
Furquan Shaikh27efc502018-06-22 09:19:15 -0700654/*
655 * Add given node as child of the provided parent. If this is the first child of
656 * the parent, update parent->children pointer as well.
657 */
658static void set_new_child(struct bus *parent, struct device *child)
659{
660 struct device *c = parent->children;
661 if (c) {
662 while (c->sibling)
663 c = c->sibling;
664 c->sibling = child;
665 } else
666 parent->children = child;
667
668 child->sibling = NULL;
669 child->parent = parent;
670}
671
Nico Huber8e1ea522020-06-03 10:20:07 -0700672static const struct device *find_alias(const struct device *const parent,
673 const char *const alias)
674{
675 if (parent->alias && !strcmp(parent->alias, alias))
676 return parent;
677
678 const struct bus *bus;
679 for (bus = parent->bus; bus; bus = bus->next_bus) {
680 const struct device *child;
681 for (child = bus->children; child; child = child->sibling) {
682 const struct device *const ret = find_alias(child, alias);
683 if (ret)
684 return ret;
685 }
686 }
687
688 return NULL;
689}
690
Furquan Shaikh93198262018-06-03 04:22:17 -0700691struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700692 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700693 const int bustype, const char *devnum,
Nico Huber8e1ea522020-06-03 10:20:07 -0700694 char *alias, int status)
Martin Rothbec07532016-08-05 18:32:18 -0600695{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000696 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700697 int path_a;
698 int path_b = 0;
699 struct device *new_d;
700
Nico Huber8e1ea522020-06-03 10:20:07 -0700701 /* Check for alias name conflicts. */
702 if (alias && find_alias(&base_root_dev, alias)) {
703 printf("ERROR: Alias already exists: %s\n", alias);
704 exit(1);
705 }
706
Furquan Shaikh93198262018-06-03 04:22:17 -0700707 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000708 if (*tmp == '.') {
709 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700710 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000711 }
712
Furquan Shaikh93198262018-06-03 04:22:17 -0700713 /* If device is found under parent, no need to allocate new device. */
714 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
715 if (new_d) {
716 alloc_bus(new_d);
717 return new_d;
718 }
719
720 new_d = alloc_dev(parent);
721
722 new_d->bustype = bustype;
723
724 new_d->path_a = path_a;
725 new_d->path_b = path_b;
Nico Huber8e1ea522020-06-03 10:20:07 -0700726 new_d->alias = alias;
Furquan Shaikh93198262018-06-03 04:22:17 -0700727
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800728 new_d->enabled = status & 0x01;
729 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000730 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700731 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000732
Furquan Shaikh27efc502018-06-22 09:19:15 -0700733 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000734
Furquan Shaikha9b64292018-05-31 07:52:00 -0700735 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200736 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000737 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200738 break;
739
740 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000741 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200742 break;
743
744 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700745 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200746 break;
747
748 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000749 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200750 break;
751
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800752 case CPU_CLUSTER:
753 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200754 break;
755
Aaron Durbinffda804b2014-09-03 12:40:15 -0500756 case CPU:
757 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
758 break;
759
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800760 case DOMAIN:
761 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200762 break;
763
764 case IOAPIC:
765 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
766 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700767
768 case GENERIC:
769 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
770 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800771
772 case SPI:
773 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
774 break;
775
Duncan Lauriebae9f852018-05-07 14:18:13 -0700776 case USB:
777 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
778 break;
779
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800780 case MMIO:
781 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
782 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600783
784 case ESPI:
785 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
786 break;
787
788 case LPC:
789 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
790 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000791 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800792
Patrick Georgi114e7b22010-05-05 11:19:50 +0000793 return new_d;
794}
795
Furquan Shaikh27efc502018-06-22 09:19:15 -0700796static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600797{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700798 struct resource *r = S_ALLOC(sizeof(struct resource));
799
Patrick Georgi114e7b22010-05-05 11:19:50 +0000800 r->type = type;
801 r->index = index;
802 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000803 if (dev->res) {
804 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600805 while (head->next)
806 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000807 head->next = r;
808 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000809 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000810 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000811}
812
Furquan Shaikh27efc502018-06-22 09:19:15 -0700813void add_resource(struct bus *bus, int type, int index, int base)
814{
815 new_resource(bus->dev, type, index, base);
816}
817
Nico Huberd09459f2020-05-26 22:13:09 +0200818static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600819{
Nico Huberd09459f2020-05-26 22:13:09 +0200820 struct reg *const r = S_ALLOC(sizeof(struct reg));
821 struct reg *prev = NULL;
822 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700823
Patrick Georgi114e7b22010-05-05 11:19:50 +0000824 r->key = name;
825 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200826
827 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
828 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000829 if (sort == 0) {
830 printf("ERROR: duplicate 'register' key.\n");
831 exit(1);
832 }
Nico Huberd09459f2020-05-26 22:13:09 +0200833 if (sort < 0)
834 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000835 }
Nico Huberd09459f2020-05-26 22:13:09 +0200836 r->next = cur;
837 if (prev)
838 prev->next = r;
839 else
840 *head = r;
841}
842
843void add_register(struct chip_instance *chip_instance, char *name, char *val)
844{
845 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000846}
847
Nico Huber8e1ea522020-06-03 10:20:07 -0700848void add_reference(struct chip_instance *const chip_instance,
849 char *const name, char *const alias)
850{
851 add_reg(&chip_instance->ref, name, alias);
852}
853
854static void set_reference(struct chip_instance *const chip_instance,
855 char *const name, char *const alias)
856{
857 const struct device *const dev = find_alias(&base_root_dev, alias);
858 if (!dev) {
859 printf("ERROR: Cannot find device alias '%s'.\n", alias);
860 exit(1);
861 }
862
863 char *const ref_name = S_ALLOC(strlen(dev->name) + 2);
864 sprintf(ref_name, "&%s", dev->name);
865 add_register(chip_instance, name, ref_name);
866}
867
868static void update_references(FILE *file, FILE *head, struct device *dev,
869 struct device *next)
870{
871 struct reg *ref;
872
873 for (ref = dev->chip_instance->ref; ref; ref = ref->next)
874 set_reference(dev->chip_instance, ref->key, ref->value);
875}
876
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200877void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
878 char *data_width)
879{
880 struct device *dev = bus->dev;
881
882 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
883 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
884 exit(1);
885 }
886
887 dev->smbios_slot_type = type;
888 dev->smbios_slot_length = length;
889 dev->smbios_slot_data_width = data_width;
890 dev->smbios_slot_designation = designation;
891}
892
Furquan Shaikh93198262018-06-03 04:22:17 -0700893void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600894 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000895{
Furquan Shaikh93198262018-06-03 04:22:17 -0700896 struct device *dev = bus->dev;
897
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800898 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000899 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
900 exit(1);
901 }
902
903 dev->subsystem_vendor = vendor;
904 dev->subsystem_device = device;
905 dev->inherit_subsystem = inherit;
906}
907
Furquan Shaikh93198262018-06-03 04:22:17 -0700908void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600909 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200910{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200911 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700912 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200913
Martin Rothbec07532016-08-05 18:32:18 -0600914 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
915 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200916 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
917 exit(1);
918 }
919
920 srcpin = _srcpin[3] - 'A';
921
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800922 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200923 printf("ERROR: ioapic config only allowed for PCI devices\n");
924 exit(1);
925 }
926
927 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200928 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200929 exit(1);
930 }
931 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
932 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
933}
934
Furquan Shaikh93198262018-06-03 04:22:17 -0700935static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600936{
Furquan Shaikh93198262018-06-03 04:22:17 -0700937 struct bus *bus = dev->bus;
938
939 while (bus) {
940 if (bus->children)
941 return 1;
942 bus = bus->next_bus;
943 }
944
945 return 0;
946}
947
Nico Huber17e9bcb2019-09-20 12:05:51 +0200948static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700949{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700950 static int dev_id;
951
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700952 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200953 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700954 ptr->name);
955 return;
956 }
957
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700958 char *name = S_ALLOC(10);
959 sprintf(name, "_dev%d", dev_id++);
960 ptr->name = name;
961
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200962 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700963 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200964 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700965 ptr->name);
966 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200967 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600968 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700969
Furquan Shaikh93198262018-06-03 04:22:17 -0700970 if (next)
971 return;
972
973 fprintf(fil,
974 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
975 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000976}
977
Furquan Shaikh93198262018-06-03 04:22:17 -0700978static void emit_resources(FILE *fil, struct device *ptr)
979{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700980 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700981 return;
982
983 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200984 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700985 struct resource *r = ptr->res;
986 while (r) {
987 fprintf(fil,
988 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
989 if (r->type == IRQ)
990 fprintf(fil, "IRQ");
991 if (r->type == DRQ)
992 fprintf(fil, "DRQ");
993 if (r->type == IO)
994 fprintf(fil, "IO");
995 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
996 r->base);
997 if (r->next)
998 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
999 i++);
1000 else
1001 fprintf(fil, ".next=NULL },\n");
1002 r = r->next;
1003 }
1004
1005 fprintf(fil, "\t };\n");
1006}
1007
1008static void emit_bus(FILE *fil, struct bus *bus)
1009{
1010 fprintf(fil, "\t\t[%d] = {\n", bus->id);
1011 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
1012 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
1013 if (bus->children)
1014 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
1015
1016 if (bus->next_bus)
1017 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
1018 bus->id + 1);
1019 else
1020 fprintf(fil, "\t\t\t.next = NULL,\n");
1021 fprintf(fil, "\t\t},\n");
1022}
1023
1024static void emit_dev_links(FILE *fil, struct device *ptr)
1025{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001026 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001027 ptr->name);
1028
1029 struct bus *bus = ptr->bus;
1030
1031 while (bus) {
1032 emit_bus(fil, bus);
1033 bus = bus->next_bus;
1034 }
1035
1036 fprintf(fil, "\t};\n");
1037}
1038
Nico Huber17e9bcb2019-09-20 12:05:51 +02001039static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +02001040{
1041 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001042 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -07001043 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -07001044
Furquan Shaikhbbade242020-05-02 16:05:29 -07001045 /*
1046 * If the chip instance of device has base_chip_instance pointer set, then follow that
1047 * to update the chip instance for current device.
1048 */
1049 if (chip_ins->base_chip_instance)
1050 chip_ins = chip_ins->base_chip_instance;
1051
Duncan Laurie47b7b342020-05-15 15:39:08 -07001052 /* Emit probe structures. */
1053 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
1054 fclose(head);
1055 fclose(fil);
1056 exit(1);
1057 }
1058
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001059 if (ptr == &base_root_dev)
1060 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1061 else
1062 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1063
Furquan Shaikh93198262018-06-03 04:22:17 -07001064 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001065
1066 /*
1067 * ops field is set to default_dev_ops_root only for the root
1068 * device. For all other devices, it is set by the driver at runtime.
1069 */
1070 if (ptr == &base_root_dev)
1071 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1072 else
1073 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001074 fprintf(fil, "#endif\n");
1075 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1076 ptr->parent->id);
1077 fprintf(fil, "\t.path = {");
1078 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1079 fprintf(fil, "},\n");
1080 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001081 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001082 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001083 fprintf(fil, "\t.on_mainboard = 1,\n");
1084 if (ptr->subsystem_vendor > 0)
1085 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1086 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001087
Furquan Shaikh93198262018-06-03 04:22:17 -07001088 if (ptr->subsystem_device > 0)
1089 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1090 ptr->subsystem_device);
1091
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001092 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001093 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001094 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001095 }
1096 if (has_children)
1097 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1098 ptr->name);
1099 else
1100 fprintf(fil, "\t.link_list = NULL,\n");
1101 if (ptr->sibling)
1102 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001103 else
1104 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001105 fprintf(fil, "#if !DEVTREE_EARLY\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001106 if (ptr->probe)
1107 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Kyösti Mälkki1557a672019-06-30 10:51:31 +03001108 for (pin = 0; pin < 4; pin++) {
1109 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
1110 fprintf(fil,
1111 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
1112 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
1113
1114 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
1115 fprintf(fil,
1116 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
1117 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
1118 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001119 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1120 chip_ins->chip->name_underscore);
1121 if (chip_ins == &mainboard_instance)
1122 fprintf(fil, "\t.name = mainboard_name,\n");
1123 fprintf(fil, "#endif\n");
1124 if (chip_ins->chip->chiph_exists)
1125 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1126 chip_ins->chip->name_underscore, chip_ins->id);
1127 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001128 fprintf(fil, "\t.next=&%s,\n", next->name);
1129 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1130 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1131 fprintf(fil, "#if !DEVTREE_EARLY\n");
1132 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1133 }
1134 /* SMBIOS types start at 1, if zero it hasn't been set */
1135 if (ptr->smbios_slot_type)
1136 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1137 ptr->smbios_slot_type);
1138 if (ptr->smbios_slot_data_width)
1139 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1140 ptr->smbios_slot_data_width);
1141 if (ptr->smbios_slot_designation)
1142 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1143 ptr->smbios_slot_designation);
1144 if (ptr->smbios_slot_length)
1145 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1146 ptr->smbios_slot_length);
1147 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1148 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1149 fprintf(fil, "#endif\n");
1150 fprintf(fil, "#endif\n");
1151 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001152 fprintf(fil, "};\n");
1153
1154 emit_resources(fil, ptr);
1155
1156 if (has_children)
1157 emit_dev_links(fil, ptr);
1158}
1159
Nico Huber17e9bcb2019-09-20 12:05:51 +02001160static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001161{
1162 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001163 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
1164 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
1165 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001166 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
1167 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001168 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001169
Nico Huber17e9bcb2019-09-20 12:05:51 +02001170 if (ptr->bustype == PNP) {
1171 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
1172 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001173 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
1174 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001175 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001176}
1177
Furquan Shaikh93198262018-06-03 04:22:17 -07001178static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1179 struct device *d)
1180{
1181 while (d) {
1182 enqueue_tail(bfs_q_head, d);
1183 d = d->sibling;
1184 }
1185}
1186
1187static void add_children_to_queue(struct queue_entry **bfs_q_head,
1188 struct device *d)
1189{
1190 struct bus *bus = d->bus;
1191
1192 while (bus) {
1193 if (bus->children)
1194 add_siblings_to_queue(bfs_q_head, bus->children);
1195 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001196 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001197}
1198
Nico Huber17e9bcb2019-09-20 12:05:51 +02001199static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1200 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001201 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001202{
Furquan Shaikh93198262018-06-03 04:22:17 -07001203 struct queue_entry *bfs_q_head = NULL;
1204
1205 enqueue_tail(&bfs_q_head, ptr);
1206
1207 while ((ptr = dequeue_head(&bfs_q_head))) {
1208 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001209 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001210 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001211}
1212
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001213static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001214{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001215 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001216
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001217 while (chip) {
1218 if (chip->chiph_exists)
1219 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1220 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001221 }
1222 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1223 fprintf(fil,
1224 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001225
1226 chip = tmp;
1227 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001228 /* A lot of cpus do not define chip_operations at all, and the ones
1229 that do only initialise .name. */
1230 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1231 fprintf(fil,
1232 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1233 chip->name_underscore);
1234 } else {
1235 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1236 chip->name_underscore);
1237 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001238 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001239 }
1240 fprintf(fil, "#endif\n");
1241}
1242
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001243static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1244{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001245 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001246 instance->chip->name_underscore,
1247 instance->chip->name_underscore,
1248 instance->id);
1249
1250 if (instance->reg) {
1251 fprintf(fil, "\n");
1252 struct reg *r = instance->reg;
1253 while (r) {
1254 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1255 r = r->next;
1256 }
1257 }
1258 fprintf(fil, "};\n\n");
1259}
1260
Nico Huber8e1ea522020-06-03 10:20:07 -07001261static void emit_chip_configs(FILE *fil)
Furquan Shaikh79e84122018-05-30 15:09:09 -07001262{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001263 struct chip *chip = chip_header.next;
1264 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001265 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001266
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001267 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001268 if (!chip->chiph_exists)
1269 continue;
1270
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001271 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001272 instance = chip->instance;
1273 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001274 /*
1275 * Emit this chip instance only if there is no forwarding pointer to the
1276 * base tree chip instance.
1277 */
1278 if (instance->base_chip_instance == NULL) {
1279 instance->id = chip_id++;
1280 emit_chip_instance(fil, instance);
1281 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001282 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001283 }
1284 }
1285}
1286
Nico Huber17e9bcb2019-09-20 12:05:51 +02001287static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001288 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001289{
1290 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001291
1292 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1293 /* user already gave us a subsystem vendor/device */
1294 return;
1295 }
1296
Furquan Shaikh93198262018-06-03 04:22:17 -07001297 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001298
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001299 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001300 continue;
1301
1302 if (p->inherit_subsystem) {
1303 dev->subsystem_vendor = p->subsystem_vendor;
1304 dev->subsystem_device = p->subsystem_device;
1305 break;
1306 }
1307 }
1308}
1309
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001310static void usage(void)
1311{
Nico Huber17e9bcb2019-09-20 12:05:51 +02001312 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -06001313 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001314}
1315
Martin Roth32051702015-11-24 12:34:16 -07001316enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -06001317 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001318 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +02001319 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001320 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -06001321};
Martin Roth32051702015-11-24 12:34:16 -07001322
Nico Huber17e9bcb2019-09-20 12:05:51 +02001323#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001324#define OPTIONAL_ARG_COUNT 1
1325#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001326
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001327static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001328{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001329 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001330 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001331 perror(NULL);
1332 exit(1);
1333 }
1334
Patrick Georgi114e7b22010-05-05 11:19:50 +00001335 yyrestart(filec);
1336
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001337 root_parent = parent;
1338 linenum = 0;
1339
Patrick Georgi114e7b22010-05-05 11:19:50 +00001340 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001341
Patrick Georgi114e7b22010-05-05 11:19:50 +00001342 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001343}
1344
Furquan Shaikh27efc502018-06-22 09:19:15 -07001345/*
1346 * Match device nodes from base and override tree to see if they are the same
1347 * node.
1348 */
1349static int device_match(struct device *a, struct device *b)
1350{
1351 return ((a->path_a == b->path_a) &&
1352 (a->path_b == b->path_b) &&
1353 (a->bustype == b->bustype) &&
1354 (a->chip_instance->chip ==
1355 b->chip_instance->chip));
1356}
1357
1358/*
Bill XIEc61d4152019-11-21 18:16:12 +08001359 * Match resource nodes from base and override tree to see if they are the same
1360 * node.
1361 */
1362static int res_match(struct resource *a, struct resource *b)
1363{
1364 return ((a->type == b->type) &&
1365 (a->index == b->index));
1366}
1367
1368/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001369 * Add resource to device. If resource is already present, then update its base
1370 * and index. If not, then add a new resource to the device.
1371 */
1372static void update_resource(struct device *dev, struct resource *res)
1373{
1374 struct resource *base_res = dev->res;
1375
1376 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001377 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001378 base_res->base = res->base;
1379 return;
1380 }
1381 base_res = base_res->next;
1382 }
1383
1384 new_resource(dev, res->type, res->index, res->base);
1385}
1386
1387/*
1388 * Add register to chip instance. If register is already present, then update
1389 * its value. If not, then add a new register to the chip instance.
1390 */
Nico Huber8e1ea522020-06-03 10:20:07 -07001391static void update_register(struct reg **const head, struct reg *reg)
Furquan Shaikh27efc502018-06-22 09:19:15 -07001392{
Nico Huber8e1ea522020-06-03 10:20:07 -07001393 struct reg *base_reg = *head;
Furquan Shaikh27efc502018-06-22 09:19:15 -07001394
1395 while (base_reg) {
1396 if (!strcmp(base_reg->key, reg->key)) {
1397 base_reg->value = reg->value;
1398 return;
1399 }
1400 base_reg = base_reg->next;
1401 }
1402
Nico Huber8e1ea522020-06-03 10:20:07 -07001403 add_reg(head, reg->key, reg->value);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001404}
1405
1406static void override_devicetree(struct bus *base_parent,
1407 struct bus *override_parent);
1408
1409/*
1410 * Update the base device properties using the properties of override device. In
1411 * addition to that, call override_devicetree for all the buses under the
1412 * override device.
1413 *
1414 * Override Rules:
1415 * +--------------------+--------------------------------------------+
1416 * | | |
1417 * |struct device member| Rule |
1418 * | | |
1419 * +-----------------------------------------------------------------+
1420 * | | |
1421 * | id | Unchanged. This is used to generate device |
1422 * | | structure name in static.c. So, no need to |
1423 * | | override. |
1424 * | | |
1425 * +-----------------------------------------------------------------+
1426 * | | |
1427 * | enabled | Copy enabled state from override device. |
1428 * | | This allows variants to override device |
1429 * | | state. |
1430 * | | |
1431 * +-----------------------------------------------------------------+
1432 * | | |
1433 * | subsystem_vendor | Copy from override device only if any one |
1434 * | subsystem_device | of the ids is non-zero. |
1435 * | | |
1436 * +-----------------------------------------------------------------+
1437 * | | |
1438 * | inherit_subsystem | Copy from override device only if it is |
1439 * | | non-zero. This allows variant to only |
1440 * | | enable inherit flag for a device. |
1441 * | | |
1442 * +-----------------------------------------------------------------+
1443 * | | |
1444 * | path | Unchanged since these are same for both |
1445 * | path_a | base and override device (Used for |
1446 * | path_b | matching devices). |
1447 * | | |
1448 * +-----------------------------------------------------------------+
1449 * | | |
1450 * | bustype | Unchanged since this is same for both base |
1451 * | | and override device (User for matching |
1452 * | | devices). |
1453 * | | |
1454 * +-----------------------------------------------------------------+
1455 * | | |
1456 * | pci_irq_info | Unchanged. |
1457 * | | |
1458 * +-----------------------------------------------------------------+
1459 * | | |
1460 * | parent | Unchanged. This is meaningful only within |
1461 * | sibling | the parse tree, hence not being copied. |
1462 * | | |
1463 * +-----------------------------------------------------------------+
1464 * | | |
1465 * | res | Each resource that is present in override |
1466 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001467 * | | 1. If resource of same type and index is |
1468 * | | present in base device, then base of |
1469 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001470 * | | 2. If not, then a new resource is allocated|
1471 * | | under the base device using type, index |
1472 * | | and base from override res. |
1473 * | | |
1474 * +-----------------------------------------------------------------+
1475 * | | |
Nico Huber8e1ea522020-06-03 10:20:07 -07001476 * | ref | Each reference that is present in override |
1477 * | | device is copied over to base device with |
1478 * | | the same rules as registers. |
1479 * | | |
1480 * +-----------------------------------------------------------------+
1481 * | | |
1482 * | alias | Base device alias is copied to override. |
1483 * | | Override devices cannot change/remove an |
1484 * | | existing alias, but they can add an alias |
1485 * | | if one does not exist. |
1486 * | | |
1487 * +-----------------------------------------------------------------+
1488 * | | |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001489 * | chip_instance | Each register of chip_instance is copied |
1490 * | | over from override device to base device: |
1491 * | | 1. If register with same key is present in |
1492 * | | base device, then value of the register |
1493 * | | is copied. |
1494 * | | 2. If not, then a new register is allocated|
1495 * | | under the base chip_instance using key |
1496 * | | and value from override register. |
1497 * | | |
1498 * +-----------------------------------------------------------------+
1499 * | | |
1500 * | bus | Recursively call override_devicetree on |
1501 * | last_bus | each bus of override device. It is assumed |
1502 * | | that bus with id X under base device |
1503 * | | to bus with id X under override device. If |
1504 * | | override device has more buses than base |
1505 * | | device, then new buses are allocated under |
1506 * | | base device. |
1507 * | | |
1508 * +-----------------------------------------------------------------+
1509 */
1510static void update_device(struct device *base_dev, struct device *override_dev)
1511{
1512 /*
1513 * Copy the enabled state of override device to base device. This allows
1514 * override tree to enable or disable a particular device.
1515 */
1516 base_dev->enabled = override_dev->enabled;
1517
1518 /*
1519 * Copy subsystem vendor and device ids from override device to base
1520 * device only if the ids are non-zero in override device. Else, honor
1521 * the values in base device.
1522 */
1523 if (override_dev->subsystem_vendor ||
1524 override_dev->subsystem_device) {
1525 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1526 base_dev->subsystem_device = override_dev->subsystem_device;
1527 }
1528
1529 /*
1530 * Copy value of inherity_subsystem from override device to base device
1531 * only if it is non-zero in override device. This allows override
1532 * tree to only enable inhert flag for a device.
1533 */
1534 if (override_dev->inherit_subsystem)
1535 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1536
1537 /*
1538 * Copy resources of override device to base device.
1539 * 1. If resource is already present in base device, then index and base
1540 * of the resource will be copied over.
1541 * 2. If resource is not already present in base device, a new resource
1542 * will be allocated.
1543 */
1544 struct resource *res = override_dev->res;
1545 while (res) {
1546 update_resource(base_dev, res);
1547 res = res->next;
1548 }
1549
1550 /*
1551 * Copy registers of override chip instance to base chip instance.
1552 * 1. If register key is already present in base chip instance, then
1553 * value for the register is copied over.
1554 * 2. If register key is not already present in base chip instance, then
1555 * a new register will be allocated.
1556 */
1557 struct reg *reg = override_dev->chip_instance->reg;
1558 while (reg) {
Nico Huber8e1ea522020-06-03 10:20:07 -07001559 update_register(&base_dev->chip_instance->reg, reg);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001560 reg = reg->next;
1561 }
1562
Nico Huber8e1ea522020-06-03 10:20:07 -07001563 /* Copy references just as with registers. */
1564 reg = override_dev->chip_instance->ref;
1565 while (reg) {
1566 update_register(&base_dev->chip_instance->ref, reg);
1567 reg = reg->next;
1568 }
1569
1570 /* Check for alias name conflicts. */
1571 if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) {
1572 printf("ERROR: alias already exists: %s\n", override_dev->alias);
1573 exit(1);
1574 }
1575
1576 /*
1577 * Copy alias from base device.
1578 *
1579 * Override devices cannot change/remove an existing alias,
1580 * but they can add an alias to a device if one does not exist yet.
1581 */
1582 if (base_dev->alias)
1583 override_dev->alias = base_dev->alias;
1584 else
1585 base_dev->alias = override_dev->alias;
1586
Furquan Shaikh27efc502018-06-22 09:19:15 -07001587 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001588 * Use probe list from override device in place of base device, in order
1589 * to allow an override to remove a probe from the base device.
1590 */
1591 base_dev->probe = override_dev->probe;
1592
1593 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001594 * Update base_chip_instance member in chip instance of override tree to forward it to
1595 * the chip instance in base tree.
1596 */
1597 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1598
1599 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001600 * Now that the device properties are all copied over, look at each bus
1601 * of the override device and run override_devicetree in a recursive
1602 * manner. The assumption here is that first bus of override device
1603 * corresponds to first bus of base device and so on. If base device has
1604 * lesser buses than override tree, then new buses are allocated for it.
1605 */
1606 struct bus *override_bus = override_dev->bus;
1607 struct bus *base_bus = base_dev->bus;
1608
1609 while (override_bus) {
1610
1611 /*
1612 * If we have more buses in override tree device, then allocate
1613 * a new bus for the base tree device as well.
1614 */
1615 if (!base_bus) {
1616 alloc_bus(base_dev);
1617 base_bus = base_dev->last_bus;
1618 }
1619
1620 override_devicetree(base_dev->bus, override_dev->bus);
1621
1622 override_bus = override_bus->next_bus;
1623 base_bus = base_bus->next_bus;
1624 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001625}
1626
1627/*
1628 * Perform copy of device and properties from override parent to base parent.
1629 * This function walks through the override tree in a depth-first manner
1630 * performing following actions:
1631 * 1. If matching device is found in base tree, then copy the properties of
1632 * override device to base tree device. Call override_devicetree recursively on
1633 * the bus of override device.
1634 * 2. If matching device is not found in base tree, then set override tree
1635 * device as new child of base_parent and update the chip pointers in override
1636 * device subtree to ensure the nodes do not point to override tree chip
1637 * instance.
1638 */
1639static void override_devicetree(struct bus *base_parent,
1640 struct bus *override_parent)
1641{
1642 struct device *base_child;
1643 struct device *override_child = override_parent->children;
1644 struct device *next_child;
1645
1646 while (override_child) {
1647
1648 /* Look for a matching device in base tree. */
1649 for (base_child = base_parent->children;
1650 base_child; base_child = base_child->sibling) {
1651 if (device_match(base_child, override_child))
1652 break;
1653 }
1654
1655 next_child = override_child->sibling;
1656
1657 /*
1658 * If matching device is found, copy properties of
1659 * override_child to base_child.
1660 */
1661 if (base_child)
1662 update_device(base_child, override_child);
1663 else {
1664 /*
1665 * If matching device is not found, set override_child
1666 * as a new child of base_parent.
1667 */
1668 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001669 }
1670
1671 override_child = next_child;
1672 }
1673}
1674
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001675int main(int argc, char **argv)
1676{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001677 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001678 usage();
1679
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001680 const char *base_devtree = argv[DEVICEFILE_ARG];
1681 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001682 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001683 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001684
1685 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001686
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001687 if (argc == TOTAL_ARG_COUNT) {
1688 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1689 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001690
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001691 if (!dev_has_children(&override_root_dev)) {
1692 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1693 exit(1);
1694 }
1695
Furquan Shaikh27efc502018-06-22 09:19:15 -07001696 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001697 }
1698
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001699 FILE *autogen = fopen(outputc, "w");
1700 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001701 fprintf(stderr, "Could not open file '%s' for writing: ",
1702 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001703 perror(NULL);
1704 exit(1);
1705 }
1706
Nico Huber17e9bcb2019-09-20 12:05:51 +02001707 FILE *autohead = fopen(outputh, "w");
1708 if (!autohead) {
1709 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1710 perror(NULL);
1711 fclose(autogen);
1712 exit(1);
1713 }
1714 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1715 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1716 fprintf(autohead, "#include <device/device.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001717 emit_fw_config(autohead);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001718
Nico Huber57bbcb32020-05-26 22:39:47 +02001719 fprintf(autogen, "#include <device/device.h>\n");
1720 fprintf(autogen, "#include <device/pci.h>\n\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001721 fprintf(autogen, "#include <static.h>\n");
Nico Huber8e1ea522020-06-03 10:20:07 -07001722 emit_chip_headers(autogen, chip_header.next);
1723 fprintf(autogen, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
Furquan Shaikh79e84122018-05-30 15:09:09 -07001724
Nico Huber17e9bcb2019-09-20 12:05:51 +02001725 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001726 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001727 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Nico Huber8e1ea522020-06-03 10:20:07 -07001728 walk_device_tree(autogen, autohead, &base_root_dev, update_references);
1729 fprintf(autogen, "\n/* chip configs */\n");
1730 emit_chip_configs(autogen);
Furquan Shaikh93198262018-06-03 04:22:17 -07001731 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001732 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001733
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001734 /* Expose static devicenames to global namespace. */
1735 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001736 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001737
Nico Huber17e9bcb2019-09-20 12:05:51 +02001738 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1739 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001740 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001741
Patrick Georgi114e7b22010-05-05 11:19:50 +00001742 return 0;
1743}