blob: 815bfc7e5550e1858b5b7072d45a6e6b31faf7c8 [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
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06004#include <assert.h>
Patrick Georgi2dbfcb72012-05-30 16:26:30 +02005#include <ctype.h>
Duncan Laurie51c83732020-06-09 11:20:29 -07006#include <getopt.h>
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -06007#include <inttypes.h>
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06008#include <libgen.h>
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -06009/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
Werner Zehac14a402019-07-11 12:47:19 +020010#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +020011#include <commonlib/helpers.h>
Duncan Laurie47b7b342020-05-15 15:39:08 -070012#include <stdint.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000013#include "sconfig.h"
14#include "sconfig.tab.h"
15
Patrick Georgi7fc9e292010-07-15 15:59:07 +000016extern int linenum;
17
Furquan Shaikh27efc502018-06-22 09:19:15 -070018/*
19 * Maintains list of all the unique chip structures for the board.
20 * This is shared across base and override device trees since we need to
21 * generate headers for all chips added by both the trees.
22 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070023static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070024
Kyösti Mälkki472d9022011-12-05 20:33:55 +020025typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020026 UNSLASH,
27 SPLIT_1ST,
28 TO_LOWER,
29 TO_UPPER,
30} translate_t;
31
Furquan Shaikh93198262018-06-03 04:22:17 -070032/*
33 * Mainboard is assumed to have a root device whose bus is the parent of all the
34 * devices that are added by parsing the devicetree file. This device has a
35 * mainboard chip instance associated with it.
36 *
37 *
38 *
39 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070040 * | Root device | | Mainboard |
41 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070042 * | | | chip_instance | (mainboard_instance)|
43 * | +------------------------+ | |
44 * | | +----------------------+
45 * | | bus |
46 * | parent v |
47 * | +-------------------+ |
48 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070049 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070050 * | | |
51 * +-------------------+ |
52 * | |
53 * | children | chip
54 * v |
55 * X |
56 * (new devices will |
57 * be added here as |
58 * children) |
59 * |
60 * |
61 * |
62 * +-------+----------+
63 * | |
64 * | Mainboard chip +----------->X (new chips will be
65 * | (mainboard_chip) | added here)
66 * | |
67 * +------------------+
68 *
69 *
70 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070071
72/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070073static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070074
Duncan Lauriee335c2e2020-07-29 16:28:43 -070075/* Root device of chipset tree. */
76static struct device chipset_root_dev;
77
Furquan Shaikh39ac7972018-06-21 18:44:32 -070078/* Root device of override tree (if applicable). */
79static struct device override_root_dev;
80
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070081static struct chip_instance mainboard_instance;
82
Furquan Shaikhde39fc72018-06-11 04:26:45 -070083static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070084 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070085 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070086};
87
Furquan Shaikhde39fc72018-06-11 04:26:45 -070088static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070089 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070090 .chip_instance = &mainboard_instance,
91 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070092 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070093 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070094 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070095};
96
Duncan Lauriee335c2e2020-07-29 16:28:43 -070097static struct bus chipset_root_bus = {
98 .id = 0,
99 .dev = &chipset_root_dev,
100};
101
102static struct device chipset_root_dev = {
103 .name = "chipset_root",
104 .chip_instance = &mainboard_instance,
105 .path = " .type = DEVICE_PATH_ROOT ",
106 .parent = &chipset_root_bus,
107 .enabled = 1,
108 .bus = &chipset_root_bus,
109};
110
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700111static struct bus override_root_bus = {
112 .id = 0,
113 .dev = &override_root_dev,
114};
115
116static struct device override_root_dev = {
117 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700118 /*
119 * Override tree root device points to the same mainboard chip instance
120 * as the base tree root device. It should not cause any side-effects
121 * since the mainboard chip instance pointer in override tree will just
122 * be ignored.
123 */
124 .chip_instance = &mainboard_instance,
125 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700126 .parent = &override_root_bus,
127 .enabled = 1,
128 .bus = &override_root_bus,
129};
130
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700131static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000132 .name = "mainboard",
133 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700134 .instance = &mainboard_instance,
135};
136
137static struct chip_instance mainboard_instance = {
138 .id = 0,
139 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000140};
141
Furquan Shaikh93198262018-06-03 04:22:17 -0700142/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700143struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000144
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700145struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700146 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700147 struct queue_entry *next;
148 struct queue_entry *prev;
149};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700150
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700151#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700152
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700153static void *s_alloc(const char *f, size_t s)
154{
155 void *data = calloc(1, s);
156 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530157 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700158 exit(1);
159 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700160 return data;
161}
162
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700163static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700164{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700165 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700166
167 e->data = data;
168 e->next = e->prev = e;
169 return e;
170}
171
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700172static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700173{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700174 struct queue_entry *tmp = new_queue_entry(data);
175 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700176
177 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700178 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179 return;
180 }
181
182 q->prev->next = tmp;
183 tmp->prev = q->prev;
184 q->prev = tmp;
185 tmp->next = q;
186}
187
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700188static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700189{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700190 struct queue_entry *q = *q_head;
191 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700192 void *data;
193
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700194 if (!q)
195 return NULL;
196
197 tmp = q->prev;
198
Furquan Shaikh79e84122018-05-30 15:09:09 -0700199 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700200 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700201 else {
202 tmp->prev->next = q;
203 q->prev = tmp->prev;
204 }
205
206 data = tmp->data;
207 free(tmp);
208
209 return data;
210}
211
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700212static void *dequeue_head(struct queue_entry **q_head)
213{
214 struct queue_entry *q = *q_head;
215 struct queue_entry *tmp = q;
216 void *data;
217
218 if (!q)
219 return NULL;
220
221 if (q->next == q)
222 *q_head = NULL;
223 else {
224 q->next->prev = q->prev;
225 q->prev->next = q->next;
226 *q_head = q->next;
227 }
228
229 data = tmp->data;
230 free(tmp);
231
232 return data;
233}
234
235static void *peek_queue_head(struct queue_entry *q_head)
236{
237 if (!q_head)
238 return NULL;
239
240 return q_head->data;
241}
242
243static struct queue_entry *chip_q_head;
244
245void chip_enqueue_tail(void *data)
246{
247 enqueue_tail(&chip_q_head, data);
248}
249
250void *chip_dequeue_tail(void)
251{
252 return dequeue_tail(&chip_q_head);
253}
254
Martin Rothbec07532016-08-05 18:32:18 -0600255int yywrap(void)
256{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000257 return 1;
258}
259
Martin Rothbec07532016-08-05 18:32:18 -0600260void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000261{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000262 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600263 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000264 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000265}
266
Martin Rothbec07532016-08-05 18:32:18 -0600267char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200268{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200269 char *b, *c;
270 b = c = strdup(str);
271 while (c && *c) {
272 if ((mode == SPLIT_1ST) && (*c == '/')) {
273 *c = 0;
274 break;
275 }
Martin Rothbec07532016-08-05 18:32:18 -0600276 if (*c == '/')
277 *c = '_';
278 if (*c == '-')
279 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200280 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200281 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200282 if (mode == TO_LOWER)
283 *c = tolower(*c);
284 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200285 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200286 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200287}
288
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700289static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600290{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700291 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700292
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700293 while (h->next) {
294 int result = strcmp(path, h->next->name);
295 if (result == 0)
296 return h->next;
297
298 if (result < 0)
299 break;
300
301 h = h->next;
302 }
303
304 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
305 new_chip->next = h->next;
306 h->next = new_chip;
307
Patrick Georgi114e7b22010-05-05 11:19:50 +0000308 new_chip->chiph_exists = 1;
309 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700310 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000311
312 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700313 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700314 sprintf(chip_h, "src/%s", path);
315 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100316 /* root_complex gets away without a separate directory, but
317 * exists on on pretty much all AMD chipsets.
318 */
319 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300320 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
321 path);
322 exit(1);
323 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700324 }
325
Martin Roth824255e2016-08-05 17:40:39 -0600326 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200327
Martin Roth824255e2016-08-05 17:40:39 -0600328 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600329 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000330
Patrick Georgi1f688802014-08-03 15:51:19 +0200331 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700332
Patrick Georgi114e7b22010-05-05 11:19:50 +0000333 return new_chip;
334}
335
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700336struct chip_instance *new_chip_instance(char *path)
337{
338 struct chip *chip = get_chip(path);
339 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
340
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700341 instance->chip = chip;
342 instance->next = chip->instance;
343 chip->instance = instance;
344
345 return instance;
346}
347
Duncan Laurie47b7b342020-05-15 15:39:08 -0700348/* List of fw_config fields added during parsing. */
349static struct fw_config_field *fw_config_fields;
350
351static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
352 const char *name)
353{
354 struct fw_config_option *option = field->options;
355
356 while (option && option->name) {
357 if (!strcmp(option->name, name))
358 return option;
359 option = option->next;
360 }
361 return NULL;
362}
363
364static struct fw_config_field *find_fw_config_field(const char *name)
365{
366 struct fw_config_field *field = fw_config_fields;
367
368 while (field && field->name) {
369 if (!strcmp(field->name, name))
370 return field;
371 field = field->next;
372 }
373 return NULL;
374}
375
376struct fw_config_field *get_fw_config_field(const char *name)
377{
378 struct fw_config_field *field = find_fw_config_field(name);
379
380 /* Fail if the field does not exist, new fields must be added with a mask. */
381 if (!field) {
382 printf("ERROR: fw_config field not found: %s\n", name);
383 exit(1);
384 }
385 return field;
386}
387
388static void append_fw_config_field(struct fw_config_field *add)
389{
390 struct fw_config_field *field = fw_config_fields;
391
392 if (!fw_config_fields) {
393 fw_config_fields = add;
394 } else {
395 while (field && field->next)
396 field = field->next;
397 field->next = add;
398 }
399}
400
401struct fw_config_field *new_fw_config_field(const char *name,
402 unsigned int start_bit, unsigned int end_bit)
403{
404 struct fw_config_field *field = find_fw_config_field(name);
405
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600406 /* Check that field is within 64 bits. */
407 if (start_bit > end_bit || end_bit > 63) {
Duncan Laurie47b7b342020-05-15 15:39:08 -0700408 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
409 start_bit, end_bit);
410 exit(1);
411 }
412
413 /* Don't allow re-defining a field, only adding new fields. */
414 if (field) {
415 printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n",
416 name, start_bit, end_bit, field->start_bit, field->end_bit);
417 exit(1);
418 }
419
420 /* Check for overlap with an existing field. */
421 field = fw_config_fields;
422 while (field) {
423 /* Check if the mask overlaps. */
424 if (start_bit <= field->end_bit && end_bit >= field->start_bit) {
425 printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n",
426 name, start_bit, end_bit,
427 field->name, field->start_bit, field->end_bit);
428 exit(1);
429 }
430 field = field->next;
431 }
432
433 field = S_ALLOC(sizeof(*field));
434 field->name = name;
435 field->start_bit = start_bit;
436 field->end_bit = end_bit;
437 append_fw_config_field(field);
438
439 return field;
440}
441
442static void append_fw_config_option_to_field(struct fw_config_field *field,
443 struct fw_config_option *add)
444{
445 struct fw_config_option *option = field->options;
446
447 if (!option) {
448 field->options = add;
449 } else {
450 while (option && option->next)
451 option = option->next;
452 option->next = add;
453 }
454}
455
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600456void add_fw_config_option(struct fw_config_field *field, const char *name, uint64_t value)
Duncan Laurie47b7b342020-05-15 15:39:08 -0700457{
458 struct fw_config_option *option;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600459 uint64_t field_max_value;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700460
461 /* Check that option value fits within field mask. */
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600462 field_max_value = (1ull << (1ull + field->end_bit - field->start_bit)) - 1ull;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700463 if (value > field_max_value) {
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600464 printf("ERROR: fw_config option %s:%s value %" PRIx64 " larger than field max %"
465 PRIx64 "\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700466 field->name, name, value, field_max_value);
467 exit(1);
468 }
469
470 /* Check for existing option with this name or value. */
471 option = field->options;
472 while (option) {
473 if (!strcmp(option->name, name)) {
474 printf("ERROR: fw_config option name %s:%s already exists\n",
475 field->name, name);
476 exit(1);
477 }
478 /* Compare values. */
479 if (value == option->value) {
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600480 printf("ERROR: fw_config option %s:%s[%" PRIx64 "] redefined as %s\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700481 field->name, option->name, value, name);
482 exit(1);
483 }
484 option = option->next;
485 }
486
487 option = S_ALLOC(sizeof(*option));
488 option->name = name;
489 option->value = value;
490
491 /* Add option to the current field. */
492 append_fw_config_option_to_field(field, option);
493}
494
495static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
496{
497 struct fw_config_probe *probe = dev->probe;
498
499 if (!probe) {
500 dev->probe = add;
501 } else {
502 while (probe && probe->next)
503 probe = probe->next;
504 probe->next = add;
505 }
506}
507
508void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
509{
510 struct fw_config_probe *probe;
511
512 probe = bus->dev->probe;
513 while (probe) {
514 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
515 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
516 exit(1);
517 }
518 probe = probe->next;
519 }
520
521 probe = S_ALLOC(sizeof(*probe));
522 probe->field = field;
523 probe->option = option;
524
525 append_fw_config_probe_to_dev(bus->dev, probe);
526}
527
528static void emit_fw_config(FILE *fil)
529{
530 struct fw_config_field *field = fw_config_fields;
531
532 if (!field)
533 return;
534
Duncan Laurie47b7b342020-05-15 15:39:08 -0700535 while (field) {
536 struct fw_config_option *option = field->options;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600537 uint64_t mask;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700538
539 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
540 field->name, field->name);
541
542 /* Compute mask from start and end bit. */
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600543 mask = ((1ull << (1ull + field->end_bit - field->start_bit)) - 1ull);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700544 mask <<= field->start_bit;
545
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600546 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%" PRIx64 "\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700547 field->name, mask);
548
549 while (option) {
550 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
551 field->name, option->name, option->name);
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600552 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%"
553 PRIx64 "\n", field->name, option->name,
554 option->value << field->start_bit);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700555
556 option = option->next;
557 }
558
559 field = field->next;
560 }
561
562 fprintf(fil, "\n");
563}
564
565static int emit_fw_config_probe(FILE *fil, struct device *dev)
566{
567 struct fw_config_probe *probe = dev->probe;
568
569 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
570
571 while (probe) {
572 /* Find matching field. */
573 struct fw_config_field *field;
574 struct fw_config_option *option;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600575 uint64_t mask, value;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700576
577 field = find_fw_config_field(probe->field);
578 if (!field) {
579 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
580 return -1;
581 }
582 option = find_fw_config_option(field, probe->option);
583 if (!option) {
584 printf("ERROR: fw_config_probe field %s option %s not found\n",
585 probe->field, probe->option);
586 return -1;
587 }
588
589 /* Fill out the probe structure with values from emit_fw_config(). */
590 fprintf(fil, "\t{\n");
591 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
592 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
593 probe->field, probe->option);
594 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
595 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
596 probe->field, probe->option);
597 fprintf(fil, "\t},\n");
598
599 probe = probe->next;
600 }
601
602 /* Add empty entry to mark end of list. */
603 fprintf(fil, "\t{ }\n};\n");
604 return 0;
605}
606
Furquan Shaikh93198262018-06-03 04:22:17 -0700607/*
608 * Allocate a new bus for the provided device.
609 * - If this is the first bus being allocated under this device, then its id
610 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
611 * - If this is not the first bus under this device, then its id is set to 1
612 * plus the id of last bus and newly allocated bus is added to the list of
613 * buses under the device. last_bus is updated to point to the newly
614 * allocated bus.
615 */
616static void alloc_bus(struct device *dev)
617{
618 struct bus *bus = S_ALLOC(sizeof(*bus));
619
620 bus->dev = dev;
621
622 if (dev->last_bus == NULL) {
623 bus->id = 0;
624 dev->bus = bus;
625 } else {
626 bus->id = dev->last_bus->id + 1;
627 dev->last_bus->next_bus = bus;
628 }
629
630 dev->last_bus = bus;
631}
632
633/*
634 * Allocate a new device under the given parent. This function allocates a new
635 * device structure under the provided parent bus and allocates a bus structure
636 * under the newly allocated device.
637 */
638static struct device *alloc_dev(struct bus *parent)
639{
640 struct device *dev = S_ALLOC(sizeof(*dev));
641
Furquan Shaikh93198262018-06-03 04:22:17 -0700642 dev->parent = parent;
643 dev->subsystem_vendor = -1;
644 dev->subsystem_device = -1;
645
646 alloc_bus(dev);
647
648 return dev;
649}
650
651/*
652 * This function scans the children of given bus to see if any device matches
653 * the new device that is requested.
654 *
655 * Returns pointer to the node if found, else NULL.
656 */
657static struct device *get_dev(struct bus *parent, int path_a, int path_b,
658 int bustype, struct chip_instance *chip_instance)
659{
660 struct device *child = parent->children;
661
662 while (child) {
663 if ((child->path_a == path_a) && (child->path_b == path_b) &&
664 (child->bustype == bustype) &&
665 (child->chip_instance == chip_instance))
666 return child;
667
668 child = child->sibling;
669 }
670
671 return NULL;
672}
673
Furquan Shaikh27efc502018-06-22 09:19:15 -0700674/*
675 * Add given node as child of the provided parent. If this is the first child of
676 * the parent, update parent->children pointer as well.
677 */
678static void set_new_child(struct bus *parent, struct device *child)
679{
680 struct device *c = parent->children;
681 if (c) {
682 while (c->sibling)
683 c = c->sibling;
684 c->sibling = child;
685 } else
686 parent->children = child;
687
688 child->sibling = NULL;
689 child->parent = parent;
690}
691
Nico Huber8e1ea522020-06-03 10:20:07 -0700692static const struct device *find_alias(const struct device *const parent,
693 const char *const alias)
694{
695 if (parent->alias && !strcmp(parent->alias, alias))
696 return parent;
697
698 const struct bus *bus;
699 for (bus = parent->bus; bus; bus = bus->next_bus) {
700 const struct device *child;
701 for (child = bus->children; child; child = child->sibling) {
702 const struct device *const ret = find_alias(child, alias);
703 if (ret)
704 return ret;
705 }
706 }
707
708 return NULL;
709}
710
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700711static struct device *new_device_with_path(struct bus *parent,
712 struct chip_instance *chip_instance,
713 const int bustype, int path_a, int path_b,
714 char *alias, int status)
Martin Rothbec07532016-08-05 18:32:18 -0600715{
Furquan Shaikh93198262018-06-03 04:22:17 -0700716 struct device *new_d;
717
Furquan Shaikh93198262018-06-03 04:22:17 -0700718 /* If device is found under parent, no need to allocate new device. */
719 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
720 if (new_d) {
721 alloc_bus(new_d);
722 return new_d;
723 }
724
725 new_d = alloc_dev(parent);
726
727 new_d->bustype = bustype;
728
729 new_d->path_a = path_a;
730 new_d->path_b = path_b;
Nico Huber8e1ea522020-06-03 10:20:07 -0700731 new_d->alias = alias;
Furquan Shaikh93198262018-06-03 04:22:17 -0700732
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800733 new_d->enabled = status & 0x01;
734 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000735 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700736 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000737
Furquan Shaikh27efc502018-06-22 09:19:15 -0700738 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000739
Furquan Shaikha9b64292018-05-31 07:52:00 -0700740 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200741 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000742 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200743 break;
744
745 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000746 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200747 break;
748
749 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700750 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200751 break;
752
753 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000754 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200755 break;
756
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800757 case CPU_CLUSTER:
758 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200759 break;
760
Aaron Durbinffda804b2014-09-03 12:40:15 -0500761 case CPU:
762 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
763 break;
764
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800765 case DOMAIN:
766 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200767 break;
768
769 case IOAPIC:
770 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
771 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700772
773 case GENERIC:
774 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
775 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800776
777 case SPI:
778 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
779 break;
780
Duncan Lauriebae9f852018-05-07 14:18:13 -0700781 case USB:
782 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
783 break;
784
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800785 case MMIO:
786 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
787 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600788
789 case ESPI:
790 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
791 break;
792
793 case LPC:
794 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
795 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000796 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800797
Patrick Georgi114e7b22010-05-05 11:19:50 +0000798 return new_d;
799}
800
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700801struct device *new_device_reference(struct bus *parent,
802 struct chip_instance *chip_instance,
803 const char *reference, int status)
804{
805 const struct device *dev = find_alias(&base_root_dev, reference);
806
807 if (!dev) {
808 printf("ERROR: Unable to find device reference %s\n", reference);
809 exit(1);
810 }
811
812 return new_device_with_path(parent, chip_instance, dev->bustype, dev->path_a,
813 dev->path_b, NULL, status);
814}
815
816struct device *new_device_raw(struct bus *parent,
817 struct chip_instance *chip_instance,
818 const int bustype, const char *devnum,
819 char *alias, int status)
820{
821 char *tmp;
822 int path_a;
823 int path_b = 0;
824
825 /* Check for alias name conflicts. */
826 if (alias && find_alias(root_parent->dev, alias)) {
827 printf("ERROR: Alias already exists: %s\n", alias);
828 exit(1);
829 }
830
831 path_a = strtol(devnum, &tmp, 16);
832 if (*tmp == '.') {
833 tmp++;
834 path_b = strtol(tmp, NULL, 16);
835 }
836
837 return new_device_with_path(parent, chip_instance, bustype, path_a, path_b, alias,
838 status);
839}
840
Furquan Shaikh27efc502018-06-22 09:19:15 -0700841static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600842{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700843 struct resource *r = S_ALLOC(sizeof(struct resource));
844
Patrick Georgi114e7b22010-05-05 11:19:50 +0000845 r->type = type;
846 r->index = index;
847 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000848 if (dev->res) {
849 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600850 while (head->next)
851 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000852 head->next = r;
853 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000854 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000855 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000856}
857
Furquan Shaikh27efc502018-06-22 09:19:15 -0700858void add_resource(struct bus *bus, int type, int index, int base)
859{
860 new_resource(bus->dev, type, index, base);
861}
862
Nico Huberd09459f2020-05-26 22:13:09 +0200863static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600864{
Nico Huberd09459f2020-05-26 22:13:09 +0200865 struct reg *const r = S_ALLOC(sizeof(struct reg));
866 struct reg *prev = NULL;
867 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700868
Patrick Georgi114e7b22010-05-05 11:19:50 +0000869 r->key = name;
870 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200871
872 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
873 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000874 if (sort == 0) {
Patrick Georgi51933122020-11-02 18:08:14 +0100875 printf("ERROR: duplicate 'register' key '%s'.\n", r->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000876 exit(1);
877 }
Nico Huberd09459f2020-05-26 22:13:09 +0200878 if (sort < 0)
879 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000880 }
Nico Huberd09459f2020-05-26 22:13:09 +0200881 r->next = cur;
882 if (prev)
883 prev->next = r;
884 else
885 *head = r;
886}
887
888void add_register(struct chip_instance *chip_instance, char *name, char *val)
889{
890 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000891}
892
Nico Huber8e1ea522020-06-03 10:20:07 -0700893void add_reference(struct chip_instance *const chip_instance,
894 char *const name, char *const alias)
895{
896 add_reg(&chip_instance->ref, name, alias);
897}
898
899static void set_reference(struct chip_instance *const chip_instance,
900 char *const name, char *const alias)
901{
902 const struct device *const dev = find_alias(&base_root_dev, alias);
903 if (!dev) {
904 printf("ERROR: Cannot find device alias '%s'.\n", alias);
905 exit(1);
906 }
907
908 char *const ref_name = S_ALLOC(strlen(dev->name) + 2);
909 sprintf(ref_name, "&%s", dev->name);
910 add_register(chip_instance, name, ref_name);
911}
912
913static void update_references(FILE *file, FILE *head, struct device *dev,
914 struct device *next)
915{
916 struct reg *ref;
917
918 for (ref = dev->chip_instance->ref; ref; ref = ref->next)
919 set_reference(dev->chip_instance, ref->key, ref->value);
920}
921
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200922void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
923 char *data_width)
924{
925 struct device *dev = bus->dev;
926
927 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
928 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
929 exit(1);
930 }
931
932 dev->smbios_slot_type = type;
933 dev->smbios_slot_length = length;
934 dev->smbios_slot_data_width = data_width;
935 dev->smbios_slot_designation = designation;
936}
937
Furquan Shaikh93198262018-06-03 04:22:17 -0700938void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600939 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000940{
Furquan Shaikh93198262018-06-03 04:22:17 -0700941 struct device *dev = bus->dev;
942
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800943 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000944 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
945 exit(1);
946 }
947
948 dev->subsystem_vendor = vendor;
949 dev->subsystem_device = device;
950 dev->inherit_subsystem = inherit;
951}
952
Furquan Shaikh93198262018-06-03 04:22:17 -0700953void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600954 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200955{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200956 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700957 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200958
Martin Rothbec07532016-08-05 18:32:18 -0600959 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
960 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200961 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
962 exit(1);
963 }
964
965 srcpin = _srcpin[3] - 'A';
966
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800967 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200968 printf("ERROR: ioapic config only allowed for PCI devices\n");
969 exit(1);
970 }
971
972 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200973 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200974 exit(1);
975 }
976 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
977 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
978}
979
Furquan Shaikh93198262018-06-03 04:22:17 -0700980static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600981{
Furquan Shaikh93198262018-06-03 04:22:17 -0700982 struct bus *bus = dev->bus;
983
984 while (bus) {
985 if (bus->children)
986 return 1;
987 bus = bus->next_bus;
988 }
989
990 return 0;
991}
992
Nico Huber17e9bcb2019-09-20 12:05:51 +0200993static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700994{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700995 static int dev_id;
996
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700997 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200998 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700999 ptr->name);
1000 return;
1001 }
1002
Furquan Shaikh4ebe9532020-05-02 15:34:42 -07001003 char *name = S_ALLOC(10);
1004 sprintf(name, "_dev%d", dev_id++);
1005 ptr->name = name;
1006
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001007 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001008 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001009 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001010 ptr->name);
1011 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001012 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -06001013 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -07001014
Furquan Shaikh93198262018-06-03 04:22:17 -07001015 if (next)
1016 return;
1017
1018 fprintf(fil,
1019 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
1020 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001021}
1022
Furquan Shaikh93198262018-06-03 04:22:17 -07001023static void emit_resources(FILE *fil, struct device *ptr)
1024{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001025 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -07001026 return;
1027
1028 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001029 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001030 struct resource *r = ptr->res;
1031 while (r) {
1032 fprintf(fil,
1033 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
1034 if (r->type == IRQ)
1035 fprintf(fil, "IRQ");
1036 if (r->type == DRQ)
1037 fprintf(fil, "DRQ");
1038 if (r->type == IO)
1039 fprintf(fil, "IO");
1040 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
1041 r->base);
1042 if (r->next)
1043 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
1044 i++);
1045 else
1046 fprintf(fil, ".next=NULL },\n");
1047 r = r->next;
1048 }
1049
1050 fprintf(fil, "\t };\n");
1051}
1052
1053static void emit_bus(FILE *fil, struct bus *bus)
1054{
1055 fprintf(fil, "\t\t[%d] = {\n", bus->id);
1056 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
1057 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
1058 if (bus->children)
1059 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
1060
1061 if (bus->next_bus)
1062 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
1063 bus->id + 1);
1064 else
1065 fprintf(fil, "\t\t\t.next = NULL,\n");
1066 fprintf(fil, "\t\t},\n");
1067}
1068
1069static void emit_dev_links(FILE *fil, struct device *ptr)
1070{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001071 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001072 ptr->name);
1073
1074 struct bus *bus = ptr->bus;
1075
1076 while (bus) {
1077 emit_bus(fil, bus);
1078 bus = bus->next_bus;
1079 }
1080
1081 fprintf(fil, "\t};\n");
1082}
1083
Nico Huber17e9bcb2019-09-20 12:05:51 +02001084static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +02001085{
1086 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001087 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -07001088 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -07001089
Furquan Shaikhbbade242020-05-02 16:05:29 -07001090 /*
1091 * If the chip instance of device has base_chip_instance pointer set, then follow that
1092 * to update the chip instance for current device.
1093 */
1094 if (chip_ins->base_chip_instance)
1095 chip_ins = chip_ins->base_chip_instance;
1096
Duncan Laurie47b7b342020-05-15 15:39:08 -07001097 /* Emit probe structures. */
1098 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001099 if (head)
1100 fclose(head);
Duncan Laurie47b7b342020-05-15 15:39:08 -07001101 fclose(fil);
1102 exit(1);
1103 }
1104
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001105 if (ptr == &base_root_dev)
1106 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1107 else
1108 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1109
Furquan Shaikh93198262018-06-03 04:22:17 -07001110 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001111
1112 /*
1113 * ops field is set to default_dev_ops_root only for the root
1114 * device. For all other devices, it is set by the driver at runtime.
1115 */
1116 if (ptr == &base_root_dev)
1117 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1118 else
1119 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001120 fprintf(fil, "#endif\n");
1121 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1122 ptr->parent->id);
1123 fprintf(fil, "\t.path = {");
1124 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1125 fprintf(fil, "},\n");
1126 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001127 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001128 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001129 fprintf(fil, "\t.on_mainboard = 1,\n");
1130 if (ptr->subsystem_vendor > 0)
1131 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1132 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001133
Furquan Shaikh93198262018-06-03 04:22:17 -07001134 if (ptr->subsystem_device > 0)
1135 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1136 ptr->subsystem_device);
1137
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001138 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001139 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001140 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001141 }
1142 if (has_children)
1143 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1144 ptr->name);
1145 else
1146 fprintf(fil, "\t.link_list = NULL,\n");
1147 if (ptr->sibling)
1148 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001149 else
1150 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001151 fprintf(fil, "#if !DEVTREE_EARLY\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001152 if (ptr->probe)
1153 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Kyösti Mälkki1557a672019-06-30 10:51:31 +03001154 for (pin = 0; pin < 4; pin++) {
1155 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
1156 fprintf(fil,
1157 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
1158 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
1159
1160 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
1161 fprintf(fil,
1162 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
1163 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
1164 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001165 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1166 chip_ins->chip->name_underscore);
1167 if (chip_ins == &mainboard_instance)
1168 fprintf(fil, "\t.name = mainboard_name,\n");
1169 fprintf(fil, "#endif\n");
1170 if (chip_ins->chip->chiph_exists)
1171 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1172 chip_ins->chip->name_underscore, chip_ins->id);
1173 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001174 fprintf(fil, "\t.next=&%s,\n", next->name);
1175 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1176 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1177 fprintf(fil, "#if !DEVTREE_EARLY\n");
1178 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1179 }
1180 /* SMBIOS types start at 1, if zero it hasn't been set */
1181 if (ptr->smbios_slot_type)
1182 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1183 ptr->smbios_slot_type);
1184 if (ptr->smbios_slot_data_width)
1185 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1186 ptr->smbios_slot_data_width);
1187 if (ptr->smbios_slot_designation)
1188 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1189 ptr->smbios_slot_designation);
1190 if (ptr->smbios_slot_length)
1191 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1192 ptr->smbios_slot_length);
1193 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
1194 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
1195 fprintf(fil, "#endif\n");
1196 fprintf(fil, "#endif\n");
1197 }
Furquan Shaikh93198262018-06-03 04:22:17 -07001198 fprintf(fil, "};\n");
1199
1200 emit_resources(fil, ptr);
1201
1202 if (has_children)
1203 emit_dev_links(fil, ptr);
1204}
1205
Nico Huber17e9bcb2019-09-20 12:05:51 +02001206static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001207{
1208 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001209 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
1210 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
1211 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001212 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
1213 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001214 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001215
Nico Huber17e9bcb2019-09-20 12:05:51 +02001216 if (ptr->bustype == PNP) {
1217 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
1218 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001219 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
1220 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001221 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001222}
1223
Furquan Shaikh93198262018-06-03 04:22:17 -07001224static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1225 struct device *d)
1226{
1227 while (d) {
1228 enqueue_tail(bfs_q_head, d);
1229 d = d->sibling;
1230 }
1231}
1232
1233static void add_children_to_queue(struct queue_entry **bfs_q_head,
1234 struct device *d)
1235{
1236 struct bus *bus = d->bus;
1237
1238 while (bus) {
1239 if (bus->children)
1240 add_siblings_to_queue(bfs_q_head, bus->children);
1241 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001242 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001243}
1244
Nico Huber17e9bcb2019-09-20 12:05:51 +02001245static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1246 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001247 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001248{
Furquan Shaikh93198262018-06-03 04:22:17 -07001249 struct queue_entry *bfs_q_head = NULL;
1250
1251 enqueue_tail(&bfs_q_head, ptr);
1252
1253 while ((ptr = dequeue_head(&bfs_q_head))) {
1254 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001255 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001256 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001257}
1258
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001259static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001260{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001261 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001262
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001263 while (chip) {
1264 if (chip->chiph_exists)
1265 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1266 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001267 }
1268 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1269 fprintf(fil,
1270 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001271
1272 chip = tmp;
1273 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001274 /* A lot of cpus do not define chip_operations at all, and the ones
1275 that do only initialise .name. */
1276 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1277 fprintf(fil,
1278 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1279 chip->name_underscore);
1280 } else {
1281 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1282 chip->name_underscore);
1283 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001284 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001285 }
1286 fprintf(fil, "#endif\n");
1287}
1288
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001289static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1290{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001291 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001292 instance->chip->name_underscore,
1293 instance->chip->name_underscore,
1294 instance->id);
1295
1296 if (instance->reg) {
1297 fprintf(fil, "\n");
1298 struct reg *r = instance->reg;
1299 while (r) {
1300 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1301 r = r->next;
1302 }
1303 }
1304 fprintf(fil, "};\n\n");
1305}
1306
Nico Huber8e1ea522020-06-03 10:20:07 -07001307static void emit_chip_configs(FILE *fil)
Furquan Shaikh79e84122018-05-30 15:09:09 -07001308{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001309 struct chip *chip = chip_header.next;
1310 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001311 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001312
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001313 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001314 if (!chip->chiph_exists)
1315 continue;
1316
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001317 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001318 instance = chip->instance;
1319 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001320 /*
1321 * Emit this chip instance only if there is no forwarding pointer to the
1322 * base tree chip instance.
1323 */
1324 if (instance->base_chip_instance == NULL) {
1325 instance->id = chip_id++;
1326 emit_chip_instance(fil, instance);
1327 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001328 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001329 }
1330 }
1331}
1332
Nico Huber17e9bcb2019-09-20 12:05:51 +02001333static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001334 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001335{
1336 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001337
1338 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1339 /* user already gave us a subsystem vendor/device */
1340 return;
1341 }
1342
Furquan Shaikh93198262018-06-03 04:22:17 -07001343 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001344
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001345 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001346 continue;
1347
1348 if (p->inherit_subsystem) {
1349 dev->subsystem_vendor = p->subsystem_vendor;
1350 dev->subsystem_device = p->subsystem_device;
1351 break;
1352 }
1353 }
1354}
1355
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001356static void usage(void)
1357{
Duncan Laurie51c83732020-06-09 11:20:29 -07001358 printf("usage: sconfig <options>\n");
1359 printf(" -c | --output_c : Path to output static.c file (required)\n");
1360 printf(" -r | --output_h : Path to header static.h file (required)\n");
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001361 printf(" -d | --output_d : Path to header static_devices.h file (required)\n");
1362 printf(" -f | --output_f : Path to header static_fw_config.h file (required)\n");
Duncan Laurie51c83732020-06-09 11:20:29 -07001363 printf(" -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n");
1364 printf(" -o | --override_devtree : Path to override devicetree file (optional)\n");
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001365 printf(" -p | --chipset_devtree : Path to chipset/SOC devicetree file (optional)\n");
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001366
Martin Rothbec07532016-08-05 18:32:18 -06001367 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001368}
1369
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001370static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001371{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001372 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001373 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001374 perror(NULL);
1375 exit(1);
1376 }
1377
Patrick Georgi114e7b22010-05-05 11:19:50 +00001378 yyrestart(filec);
1379
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001380 root_parent = parent;
1381 linenum = 0;
1382
Patrick Georgi114e7b22010-05-05 11:19:50 +00001383 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001384
Patrick Georgi114e7b22010-05-05 11:19:50 +00001385 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001386}
1387
Furquan Shaikh27efc502018-06-22 09:19:15 -07001388/*
1389 * Match device nodes from base and override tree to see if they are the same
1390 * node.
1391 */
1392static int device_match(struct device *a, struct device *b)
1393{
1394 return ((a->path_a == b->path_a) &&
1395 (a->path_b == b->path_b) &&
1396 (a->bustype == b->bustype) &&
1397 (a->chip_instance->chip ==
1398 b->chip_instance->chip));
1399}
1400
1401/*
Bill XIEc61d4152019-11-21 18:16:12 +08001402 * Match resource nodes from base and override tree to see if they are the same
1403 * node.
1404 */
1405static int res_match(struct resource *a, struct resource *b)
1406{
1407 return ((a->type == b->type) &&
1408 (a->index == b->index));
1409}
1410
1411/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001412 * Add resource to device. If resource is already present, then update its base
1413 * and index. If not, then add a new resource to the device.
1414 */
1415static void update_resource(struct device *dev, struct resource *res)
1416{
1417 struct resource *base_res = dev->res;
1418
1419 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001420 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001421 base_res->base = res->base;
1422 return;
1423 }
1424 base_res = base_res->next;
1425 }
1426
1427 new_resource(dev, res->type, res->index, res->base);
1428}
1429
1430/*
1431 * Add register to chip instance. If register is already present, then update
1432 * its value. If not, then add a new register to the chip instance.
1433 */
Nico Huber8e1ea522020-06-03 10:20:07 -07001434static void update_register(struct reg **const head, struct reg *reg)
Furquan Shaikh27efc502018-06-22 09:19:15 -07001435{
Nico Huber8e1ea522020-06-03 10:20:07 -07001436 struct reg *base_reg = *head;
Furquan Shaikh27efc502018-06-22 09:19:15 -07001437
1438 while (base_reg) {
1439 if (!strcmp(base_reg->key, reg->key)) {
1440 base_reg->value = reg->value;
1441 return;
1442 }
1443 base_reg = base_reg->next;
1444 }
1445
Nico Huber8e1ea522020-06-03 10:20:07 -07001446 add_reg(head, reg->key, reg->value);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001447}
1448
1449static void override_devicetree(struct bus *base_parent,
1450 struct bus *override_parent);
1451
1452/*
1453 * Update the base device properties using the properties of override device. In
1454 * addition to that, call override_devicetree for all the buses under the
1455 * override device.
1456 *
1457 * Override Rules:
1458 * +--------------------+--------------------------------------------+
1459 * | | |
1460 * |struct device member| Rule |
1461 * | | |
1462 * +-----------------------------------------------------------------+
1463 * | | |
1464 * | id | Unchanged. This is used to generate device |
1465 * | | structure name in static.c. So, no need to |
1466 * | | override. |
1467 * | | |
1468 * +-----------------------------------------------------------------+
1469 * | | |
1470 * | enabled | Copy enabled state from override device. |
1471 * | | This allows variants to override device |
1472 * | | state. |
1473 * | | |
1474 * +-----------------------------------------------------------------+
1475 * | | |
1476 * | subsystem_vendor | Copy from override device only if any one |
1477 * | subsystem_device | of the ids is non-zero. |
1478 * | | |
1479 * +-----------------------------------------------------------------+
1480 * | | |
1481 * | inherit_subsystem | Copy from override device only if it is |
1482 * | | non-zero. This allows variant to only |
1483 * | | enable inherit flag for a device. |
1484 * | | |
1485 * +-----------------------------------------------------------------+
1486 * | | |
1487 * | path | Unchanged since these are same for both |
1488 * | path_a | base and override device (Used for |
1489 * | path_b | matching devices). |
1490 * | | |
1491 * +-----------------------------------------------------------------+
1492 * | | |
1493 * | bustype | Unchanged since this is same for both base |
1494 * | | and override device (User for matching |
1495 * | | devices). |
1496 * | | |
1497 * +-----------------------------------------------------------------+
1498 * | | |
1499 * | pci_irq_info | Unchanged. |
1500 * | | |
1501 * +-----------------------------------------------------------------+
1502 * | | |
1503 * | parent | Unchanged. This is meaningful only within |
1504 * | sibling | the parse tree, hence not being copied. |
1505 * | | |
1506 * +-----------------------------------------------------------------+
1507 * | | |
1508 * | res | Each resource that is present in override |
1509 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001510 * | | 1. If resource of same type and index is |
1511 * | | present in base device, then base of |
1512 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001513 * | | 2. If not, then a new resource is allocated|
1514 * | | under the base device using type, index |
1515 * | | and base from override res. |
1516 * | | |
1517 * +-----------------------------------------------------------------+
1518 * | | |
Nico Huber8e1ea522020-06-03 10:20:07 -07001519 * | ref | Each reference that is present in override |
1520 * | | device is copied over to base device with |
1521 * | | the same rules as registers. |
1522 * | | |
1523 * +-----------------------------------------------------------------+
1524 * | | |
1525 * | alias | Base device alias is copied to override. |
1526 * | | Override devices cannot change/remove an |
1527 * | | existing alias, but they can add an alias |
1528 * | | if one does not exist. |
1529 * | | |
1530 * +-----------------------------------------------------------------+
1531 * | | |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001532 * | chip_instance | Each register of chip_instance is copied |
1533 * | | over from override device to base device: |
1534 * | | 1. If register with same key is present in |
1535 * | | base device, then value of the register |
1536 * | | is copied. |
1537 * | | 2. If not, then a new register is allocated|
1538 * | | under the base chip_instance using key |
1539 * | | and value from override register. |
1540 * | | |
1541 * +-----------------------------------------------------------------+
1542 * | | |
1543 * | bus | Recursively call override_devicetree on |
1544 * | last_bus | each bus of override device. It is assumed |
1545 * | | that bus with id X under base device |
1546 * | | to bus with id X under override device. If |
1547 * | | override device has more buses than base |
1548 * | | device, then new buses are allocated under |
1549 * | | base device. |
1550 * | | |
1551 * +-----------------------------------------------------------------+
1552 */
1553static void update_device(struct device *base_dev, struct device *override_dev)
1554{
1555 /*
1556 * Copy the enabled state of override device to base device. This allows
1557 * override tree to enable or disable a particular device.
1558 */
1559 base_dev->enabled = override_dev->enabled;
1560
1561 /*
Duncan Laurie7f6a4842020-10-28 14:22:34 -07001562 * Copy the hidden state of override device to base device. This allows
1563 * override tree to hide or unhide a particular device.
1564 */
1565 base_dev->hidden = override_dev->hidden;
1566
1567 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001568 * Copy subsystem vendor and device ids from override device to base
1569 * device only if the ids are non-zero in override device. Else, honor
1570 * the values in base device.
1571 */
1572 if (override_dev->subsystem_vendor ||
1573 override_dev->subsystem_device) {
1574 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1575 base_dev->subsystem_device = override_dev->subsystem_device;
1576 }
1577
1578 /*
1579 * Copy value of inherity_subsystem from override device to base device
1580 * only if it is non-zero in override device. This allows override
1581 * tree to only enable inhert flag for a device.
1582 */
1583 if (override_dev->inherit_subsystem)
1584 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1585
1586 /*
1587 * Copy resources of override device to base device.
1588 * 1. If resource is already present in base device, then index and base
1589 * of the resource will be copied over.
1590 * 2. If resource is not already present in base device, a new resource
1591 * will be allocated.
1592 */
1593 struct resource *res = override_dev->res;
1594 while (res) {
1595 update_resource(base_dev, res);
1596 res = res->next;
1597 }
1598
1599 /*
1600 * Copy registers of override chip instance to base chip instance.
1601 * 1. If register key is already present in base chip instance, then
1602 * value for the register is copied over.
1603 * 2. If register key is not already present in base chip instance, then
1604 * a new register will be allocated.
1605 */
1606 struct reg *reg = override_dev->chip_instance->reg;
1607 while (reg) {
Nico Huber8e1ea522020-06-03 10:20:07 -07001608 update_register(&base_dev->chip_instance->reg, reg);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001609 reg = reg->next;
1610 }
1611
Nico Huber8e1ea522020-06-03 10:20:07 -07001612 /* Copy references just as with registers. */
1613 reg = override_dev->chip_instance->ref;
1614 while (reg) {
1615 update_register(&base_dev->chip_instance->ref, reg);
1616 reg = reg->next;
1617 }
1618
1619 /* Check for alias name conflicts. */
1620 if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) {
1621 printf("ERROR: alias already exists: %s\n", override_dev->alias);
1622 exit(1);
1623 }
1624
1625 /*
1626 * Copy alias from base device.
1627 *
1628 * Override devices cannot change/remove an existing alias,
1629 * but they can add an alias to a device if one does not exist yet.
1630 */
1631 if (base_dev->alias)
1632 override_dev->alias = base_dev->alias;
1633 else
1634 base_dev->alias = override_dev->alias;
1635
Furquan Shaikh27efc502018-06-22 09:19:15 -07001636 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001637 * Use probe list from override device in place of base device, in order
1638 * to allow an override to remove a probe from the base device.
1639 */
1640 base_dev->probe = override_dev->probe;
1641
1642 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001643 * Update base_chip_instance member in chip instance of override tree to forward it to
1644 * the chip instance in base tree.
1645 */
1646 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1647
1648 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001649 * Now that the device properties are all copied over, look at each bus
1650 * of the override device and run override_devicetree in a recursive
1651 * manner. The assumption here is that first bus of override device
1652 * corresponds to first bus of base device and so on. If base device has
1653 * lesser buses than override tree, then new buses are allocated for it.
1654 */
1655 struct bus *override_bus = override_dev->bus;
1656 struct bus *base_bus = base_dev->bus;
1657
1658 while (override_bus) {
1659
1660 /*
1661 * If we have more buses in override tree device, then allocate
1662 * a new bus for the base tree device as well.
1663 */
1664 if (!base_bus) {
1665 alloc_bus(base_dev);
1666 base_bus = base_dev->last_bus;
1667 }
1668
1669 override_devicetree(base_dev->bus, override_dev->bus);
1670
1671 override_bus = override_bus->next_bus;
1672 base_bus = base_bus->next_bus;
1673 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001674}
1675
1676/*
1677 * Perform copy of device and properties from override parent to base parent.
1678 * This function walks through the override tree in a depth-first manner
1679 * performing following actions:
1680 * 1. If matching device is found in base tree, then copy the properties of
1681 * override device to base tree device. Call override_devicetree recursively on
1682 * the bus of override device.
1683 * 2. If matching device is not found in base tree, then set override tree
1684 * device as new child of base_parent and update the chip pointers in override
1685 * device subtree to ensure the nodes do not point to override tree chip
1686 * instance.
1687 */
1688static void override_devicetree(struct bus *base_parent,
1689 struct bus *override_parent)
1690{
1691 struct device *base_child;
1692 struct device *override_child = override_parent->children;
1693 struct device *next_child;
1694
1695 while (override_child) {
1696
1697 /* Look for a matching device in base tree. */
1698 for (base_child = base_parent->children;
1699 base_child; base_child = base_child->sibling) {
1700 if (device_match(base_child, override_child))
1701 break;
1702 }
1703
1704 next_child = override_child->sibling;
1705
1706 /*
1707 * If matching device is found, copy properties of
1708 * override_child to base_child.
1709 */
1710 if (base_child)
1711 update_device(base_child, override_child);
1712 else {
1713 /*
1714 * If matching device is not found, set override_child
1715 * as a new child of base_parent.
1716 */
1717 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001718 }
1719
1720 override_child = next_child;
1721 }
1722}
1723
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001724static void parse_override_devicetree(const char *file, struct device *dev)
1725{
1726 parse_devicetree(file, dev->bus);
1727
1728 if (!dev_has_children(dev)) {
1729 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1730 exit(1);
1731 }
1732
1733 override_devicetree(&base_root_bus, dev->bus);
1734}
1735
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001736static void generate_outputh(FILE *f, const char *fw_conf_header, const char *device_header)
1737{
1738 fprintf(f, "#ifndef __STATIC_DEVICE_TREE_H\n");
1739 fprintf(f, "#define __STATIC_DEVICE_TREE_H\n\n");
1740
1741 fprintf(f, "#include <%s>\n", fw_conf_header);
1742 fprintf(f, "#include <%s>\n\n", device_header);
1743
1744 fprintf(f, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1745}
1746
1747static void generate_outputc(FILE *f, const char *static_header)
1748{
1749 fprintf(f, "#include <device/device.h>\n");
1750 fprintf(f, "#include <device/pci.h>\n");
1751 fprintf(f, "#include <fw_config.h>\n");
1752 fprintf(f, "#include <%s>\n", static_header);
1753 emit_chip_headers(f, chip_header.next);
1754 fprintf(f, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
1755
1756 walk_device_tree(NULL, NULL, &base_root_dev, inherit_subsystem_ids);
1757 fprintf(f, "\n/* pass 0 */\n");
1758 walk_device_tree(f, NULL, &base_root_dev, pass0);
1759 walk_device_tree(NULL, NULL, &base_root_dev, update_references);
1760 fprintf(f, "\n/* chip configs */\n");
1761 emit_chip_configs(f);
1762 fprintf(f, "\n/* pass 1 */\n");
1763 walk_device_tree(f, NULL, &base_root_dev, pass1);
1764}
1765
1766static void generate_outputd(FILE *gen, FILE *dev)
1767{
1768 fprintf(dev, "#ifndef __STATIC_DEVICES_H\n");
1769 fprintf(dev, "#define __STATIC_DEVICES_H\n\n");
1770 fprintf(dev, "#include <device/device.h>\n\n");
1771 fprintf(dev, "/* expose_device_names */\n");
1772 walk_device_tree(gen, dev, &base_root_dev, expose_device_names);
1773 fprintf(dev, "\n#endif /* __STATIC_DEVICE_NAMES_H */\n");
1774}
1775
1776static void generate_outputf(FILE *f)
1777{
1778 fprintf(f, "#ifndef __STATIC_FW_CONFIG_H\n");
1779 fprintf(f, "#define __STATIC_FW_CONFIG_H\n\n");
1780 emit_fw_config(f);
1781 fprintf(f, "\n#endif /* __STATIC_FW_CONFIG_H */\n");
1782}
1783
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001784int main(int argc, char **argv)
1785{
Duncan Laurie51c83732020-06-09 11:20:29 -07001786 static const struct option long_options[] = {
1787 { "mainboard_devtree", 1, NULL, 'm' },
1788 { "override_devtree", 1, NULL, 'o' },
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001789 { "chipset_devtree", 1, NULL, 'p' },
Duncan Laurie51c83732020-06-09 11:20:29 -07001790 { "output_c", 1, NULL, 'c' },
1791 { "output_h", 1, NULL, 'r' },
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001792 { "output_d", 1, NULL, 'd' },
1793 { "output_f", 1, NULL, 'f' },
Duncan Laurie51c83732020-06-09 11:20:29 -07001794 { "help", 1, NULL, 'h' },
1795 { }
1796 };
1797 const char *override_devtree = NULL;
1798 const char *base_devtree = NULL;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001799 const char *chipset_devtree = NULL;
Duncan Laurie51c83732020-06-09 11:20:29 -07001800 const char *outputc = NULL;
1801 const char *outputh = NULL;
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001802 const char *outputd = NULL;
1803 const char *outputf = NULL;
Duncan Laurie51c83732020-06-09 11:20:29 -07001804 int opt, option_index;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001805
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001806 while ((opt = getopt_long(argc, argv, "m:o:p:c:r:d:f:h", long_options,
Duncan Laurie51c83732020-06-09 11:20:29 -07001807 &option_index)) != EOF) {
1808 switch (opt) {
1809 case 'm':
1810 base_devtree = strdup(optarg);
1811 break;
1812 case 'o':
1813 override_devtree = strdup(optarg);
1814 break;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001815 case 'p':
1816 chipset_devtree = strdup(optarg);
1817 break;
Duncan Laurie51c83732020-06-09 11:20:29 -07001818 case 'c':
1819 outputc = strdup(optarg);
1820 break;
1821 case 'r':
1822 outputh = strdup(optarg);
1823 break;
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001824 case 'd':
1825 outputd = strdup(optarg);
1826 break;
1827 case 'f':
1828 outputf = strdup(optarg);
1829 break;
Duncan Laurie51c83732020-06-09 11:20:29 -07001830 case 'h':
1831 default:
1832 usage();
1833 }
1834 }
1835
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001836 if (!base_devtree || !outputc || !outputh || !outputd || !outputf)
Duncan Laurie51c83732020-06-09 11:20:29 -07001837 usage();
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001838
Duncan Lauriee335c2e2020-07-29 16:28:43 -07001839 if (chipset_devtree) {
1840 /* Use the chipset devicetree as the base, then override
1841 with the mainboard "base" devicetree. */
1842 parse_devicetree(chipset_devtree, &base_root_bus);
1843 parse_override_devicetree(base_devtree, &chipset_root_dev);
1844 } else {
1845 parse_devicetree(base_devtree, &base_root_bus);
1846 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001847
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001848 if (override_devtree)
1849 parse_override_devicetree(override_devtree, &override_root_dev);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001850
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001851 FILE *autogen = fopen(outputc, "w");
1852 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001853 fprintf(stderr, "Could not open file '%s' for writing: ",
1854 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001855 perror(NULL);
1856 exit(1);
1857 }
1858
Nico Huber17e9bcb2019-09-20 12:05:51 +02001859 FILE *autohead = fopen(outputh, "w");
1860 if (!autohead) {
1861 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1862 perror(NULL);
1863 fclose(autogen);
1864 exit(1);
1865 }
Nico Huber17e9bcb2019-09-20 12:05:51 +02001866
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001867 FILE *autodev = fopen(outputd, "w");
1868 if (!autodev) {
1869 fprintf(stderr, "Could not open file '%s' for writing: ", outputd);
1870 perror(NULL);
1871 fclose(autogen);
1872 fclose(autohead);
1873 exit(1);
1874 }
Furquan Shaikh79e84122018-05-30 15:09:09 -07001875
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001876 FILE *autofwconf = fopen(outputf, "w");
1877 if (!autofwconf) {
1878 fprintf(stderr, "Could not open file '%s' for writing: ", outputf);
1879 perror(NULL);
1880 fclose(autogen);
1881 fclose(autohead);
1882 fclose(autodev);
1883 exit(1);
1884 }
Sven Schnelle270a9082011-03-01 19:58:15 +00001885
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001886 char *f = strdup(outputf);
1887 assert(f);
1888 char *d = strdup(outputd);
1889 assert(d);
1890 char *h = strdup(outputh);
1891 assert(h);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001892
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001893 const char *fw_conf_header = basename(f);
1894 const char *device_header = basename(d);
1895 const char *static_header = basename(h);
1896
1897 generate_outputh(autohead, fw_conf_header, device_header);
1898 generate_outputc(autogen, static_header);
1899 generate_outputd(autogen, autodev);
1900 generate_outputf(autofwconf);
1901
Nico Huber17e9bcb2019-09-20 12:05:51 +02001902 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001903 fclose(autogen);
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001904 fclose(autodev);
1905 fclose(autofwconf);
1906 free(f);
1907 free(d);
1908 free(h);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001909
Patrick Georgi114e7b22010-05-05 11:19:50 +00001910 return 0;
1911}