blob: fb598282dae274810427045c04635fa3d54078e0 [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
Nico Huberc0fc38e2022-08-06 19:02:59 +0200151/* Global list of all `struct device_operations` identifiers to declare. */
152static struct identifier *device_operations;
153
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700154#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700155
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700156static void *s_alloc(const char *f, size_t s)
157{
158 void *data = calloc(1, s);
159 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530160 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700161 exit(1);
162 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700163 return data;
164}
165
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700166static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700167{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700168 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700169
170 e->data = data;
171 e->next = e->prev = e;
172 return e;
173}
174
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700175static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700176{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700177 struct queue_entry *tmp = new_queue_entry(data);
178 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179
180 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700181 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700182 return;
183 }
184
185 q->prev->next = tmp;
186 tmp->prev = q->prev;
187 q->prev = tmp;
188 tmp->next = q;
189}
190
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700191static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700192{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700193 struct queue_entry *q = *q_head;
194 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700195 void *data;
196
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700197 if (!q)
198 return NULL;
199
200 tmp = q->prev;
201
Furquan Shaikh79e84122018-05-30 15:09:09 -0700202 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700203 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700204 else {
205 tmp->prev->next = q;
206 q->prev = tmp->prev;
207 }
208
209 data = tmp->data;
210 free(tmp);
211
212 return data;
213}
214
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700215static void *dequeue_head(struct queue_entry **q_head)
216{
217 struct queue_entry *q = *q_head;
218 struct queue_entry *tmp = q;
219 void *data;
220
221 if (!q)
222 return NULL;
223
224 if (q->next == q)
225 *q_head = NULL;
226 else {
227 q->next->prev = q->prev;
228 q->prev->next = q->next;
229 *q_head = q->next;
230 }
231
232 data = tmp->data;
233 free(tmp);
234
235 return data;
236}
237
238static void *peek_queue_head(struct queue_entry *q_head)
239{
240 if (!q_head)
241 return NULL;
242
243 return q_head->data;
244}
245
246static struct queue_entry *chip_q_head;
247
248void chip_enqueue_tail(void *data)
249{
250 enqueue_tail(&chip_q_head, data);
251}
252
253void *chip_dequeue_tail(void)
254{
255 return dequeue_tail(&chip_q_head);
256}
257
Martin Rothbec07532016-08-05 18:32:18 -0600258int yywrap(void)
259{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000260 return 1;
261}
262
Martin Rothbec07532016-08-05 18:32:18 -0600263void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000264{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000265 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600266 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000267 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000268}
269
Martin Rothbec07532016-08-05 18:32:18 -0600270char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200271{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200272 char *b, *c;
273 b = c = strdup(str);
274 while (c && *c) {
275 if ((mode == SPLIT_1ST) && (*c == '/')) {
276 *c = 0;
277 break;
278 }
Martin Rothbec07532016-08-05 18:32:18 -0600279 if (*c == '/')
280 *c = '_';
281 if (*c == '-')
282 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200283 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200284 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200285 if (mode == TO_LOWER)
286 *c = tolower(*c);
287 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200288 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200289 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200290}
291
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700292static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600293{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700294 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700295
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700296 while (h->next) {
297 int result = strcmp(path, h->next->name);
298 if (result == 0)
299 return h->next;
300
301 if (result < 0)
302 break;
303
304 h = h->next;
305 }
306
307 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
308 new_chip->next = h->next;
309 h->next = new_chip;
310
Patrick Georgi114e7b22010-05-05 11:19:50 +0000311 new_chip->chiph_exists = 1;
312 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700313 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000314
315 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700316 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700317 sprintf(chip_h, "src/%s", path);
318 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100319 /* root_complex gets away without a separate directory, but
Alexander Goncharov893c3ae82023-02-04 15:20:37 +0400320 * exists on pretty much all AMD chipsets.
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100321 */
322 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300323 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
324 path);
325 exit(1);
326 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700327 }
328
Martin Roth824255e2016-08-05 17:40:39 -0600329 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200330
Martin Roth824255e2016-08-05 17:40:39 -0600331 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600332 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000333
Patrick Georgi1f688802014-08-03 15:51:19 +0200334 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700335
Patrick Georgi114e7b22010-05-05 11:19:50 +0000336 return new_chip;
337}
338
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700339struct chip_instance *new_chip_instance(char *path)
340{
341 struct chip *chip = get_chip(path);
342 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
343
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700344 instance->chip = chip;
345 instance->next = chip->instance;
346 chip->instance = instance;
347
348 return instance;
349}
350
Duncan Laurie47b7b342020-05-15 15:39:08 -0700351/* List of fw_config fields added during parsing. */
352static struct fw_config_field *fw_config_fields;
353
354static struct fw_config_option *find_fw_config_option(struct fw_config_field *field,
355 const char *name)
356{
357 struct fw_config_option *option = field->options;
358
359 while (option && option->name) {
360 if (!strcmp(option->name, name))
361 return option;
362 option = option->next;
363 }
364 return NULL;
365}
366
367static struct fw_config_field *find_fw_config_field(const char *name)
368{
369 struct fw_config_field *field = fw_config_fields;
370
371 while (field && field->name) {
372 if (!strcmp(field->name, name))
373 return field;
374 field = field->next;
375 }
376 return NULL;
377}
378
379struct fw_config_field *get_fw_config_field(const char *name)
380{
381 struct fw_config_field *field = find_fw_config_field(name);
382
383 /* Fail if the field does not exist, new fields must be added with a mask. */
384 if (!field) {
385 printf("ERROR: fw_config field not found: %s\n", name);
386 exit(1);
387 }
388 return field;
389}
390
391static void append_fw_config_field(struct fw_config_field *add)
392{
393 struct fw_config_field *field = fw_config_fields;
394
395 if (!fw_config_fields) {
396 fw_config_fields = add;
397 } else {
398 while (field && field->next)
399 field = field->next;
400 field->next = add;
401 }
402}
403
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600404void append_fw_config_bits(struct fw_config_field_bits **bits,
405 unsigned int start_bit, unsigned int end_bit)
406{
407 struct fw_config_field_bits *new_bits = S_ALLOC(sizeof(*new_bits));
408 new_bits->start_bit = start_bit;
409 new_bits->end_bit = end_bit;
410 new_bits->next = NULL;
411
412 if (*bits == NULL) {
413 *bits = new_bits;
414 return;
415 }
416
417 struct fw_config_field_bits *tmp = *bits;
418 while (tmp->next)
419 tmp = tmp->next;
420
421 tmp->next = new_bits;
422}
423
424int fw_config_masks_overlap(struct fw_config_field *existing,
425 unsigned int start_bit, unsigned int end_bit)
426{
427 struct fw_config_field_bits *bits = existing->bits;
428 while (bits) {
429 if (start_bit <= bits->end_bit && end_bit >= bits->start_bit) {
430 printf("ERROR: fw_config field [%u-%u] overlaps %s[%u-%u]\n",
431 start_bit, end_bit,
432 existing->name, bits->start_bit, bits->end_bit);
433 return 1;
434 }
435 bits = bits->next;
436 }
437
438 return 0;
439}
440
441struct fw_config_field *new_fw_config_field(const char *name, struct fw_config_field_bits *bits)
Duncan Laurie47b7b342020-05-15 15:39:08 -0700442{
443 struct fw_config_field *field = find_fw_config_field(name);
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600444 struct fw_config_field_bits *tmp;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700445
446 /* Don't allow re-defining a field, only adding new fields. */
447 if (field) {
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600448 printf("ERROR: fw_config field %s already exists\n", name);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700449 exit(1);
450 }
451
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600452 /* Check that each field is within 64 bits. */
453 tmp = bits;
454 while (tmp) {
455 if (tmp->start_bit > tmp->end_bit || tmp->end_bit > 63) {
Tim Wawrzynczak54c36622021-05-04 10:08:10 -0600456 printf("ERROR: fw_config field %s has invalid range %u-%u\n", name,
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600457 tmp->start_bit, tmp->end_bit);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700458 exit(1);
459 }
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600460
461 /* Check for overlap with an existing field. */
462 struct fw_config_field *existing = fw_config_fields;
463 while (existing) {
464 if (fw_config_masks_overlap(existing, tmp->start_bit, tmp->end_bit))
465 exit(1);
466 existing = existing->next;
467 }
468
469 tmp = tmp->next;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700470 }
471
472 field = S_ALLOC(sizeof(*field));
473 field->name = name;
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600474 field->bits = bits;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700475 append_fw_config_field(field);
476
477 return field;
478}
479
480static void append_fw_config_option_to_field(struct fw_config_field *field,
481 struct fw_config_option *add)
482{
483 struct fw_config_option *option = field->options;
484
485 if (!option) {
486 field->options = add;
487 } else {
488 while (option && option->next)
489 option = option->next;
490 option->next = add;
491 }
492}
493
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600494static uint64_t calc_max_field_value(const struct fw_config_field *field)
495{
496 unsigned int bit_count = 0;
497
498 const struct fw_config_field_bits *bits = field->bits;
499 while (bits) {
500 bit_count += 1 + bits->end_bit - bits->start_bit;
501 bits = bits->next;
502 };
503
504 return (1ull << bit_count) - 1ull;
505}
506
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600507void add_fw_config_option(struct fw_config_field *field, const char *name, uint64_t value)
Duncan Laurie47b7b342020-05-15 15:39:08 -0700508{
509 struct fw_config_option *option;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700510
511 /* Check that option value fits within field mask. */
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600512 uint64_t field_max_value = calc_max_field_value(field);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700513 if (value > field_max_value) {
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600514 printf("ERROR: fw_config option %s:%s value %" PRIx64 " larger than field max %"
515 PRIx64 "\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700516 field->name, name, value, field_max_value);
517 exit(1);
518 }
519
520 /* Check for existing option with this name or value. */
521 option = field->options;
522 while (option) {
523 if (!strcmp(option->name, name)) {
524 printf("ERROR: fw_config option name %s:%s already exists\n",
525 field->name, name);
526 exit(1);
527 }
528 /* Compare values. */
529 if (value == option->value) {
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600530 printf("ERROR: fw_config option %s:%s[%" PRIx64 "] redefined as %s\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700531 field->name, option->name, value, name);
532 exit(1);
533 }
534 option = option->next;
535 }
536
537 option = S_ALLOC(sizeof(*option));
538 option->name = name;
539 option->value = value;
540
541 /* Add option to the current field. */
542 append_fw_config_option_to_field(field, option);
543}
544
545static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add)
546{
547 struct fw_config_probe *probe = dev->probe;
548
549 if (!probe) {
550 dev->probe = add;
551 } else {
552 while (probe && probe->next)
553 probe = probe->next;
554 probe->next = add;
555 }
556}
557
Furquan Shaikhb9c22e02021-08-23 23:23:23 -0700558static int check_probe_exists(struct fw_config_probe *probe, const char *field,
559 const char *option)
560{
561 while (probe) {
562 if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) {
563 return 1;
564 }
565 probe = probe->next;
566 }
567
568 return 0;
569}
570
Duncan Laurie47b7b342020-05-15 15:39:08 -0700571void add_fw_config_probe(struct bus *bus, const char *field, const char *option)
572{
573 struct fw_config_probe *probe;
574
Furquan Shaikhb9c22e02021-08-23 23:23:23 -0700575 if (check_probe_exists(bus->dev->probe, field, option)) {
576 printf("ERROR: fw_config probe %s:%s already exists\n", field, option);
577 exit(1);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700578 }
579
580 probe = S_ALLOC(sizeof(*probe));
581 probe->field = field;
582 probe->option = option;
583
584 append_fw_config_probe_to_dev(bus->dev, probe);
585}
586
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600587static uint64_t compute_fw_config_mask(const struct fw_config_field_bits *bits)
588{
589 uint64_t mask = 0;
590
591 while (bits) {
592 /* Compute mask from start and end bit. */
593 uint64_t tmp = ((1ull << (1ull + bits->end_bit - bits->start_bit)) - 1ull);
594 tmp <<= bits->start_bit;
595 mask |= tmp;
596 bits = bits->next;
597 }
598
599 return mask;
600}
601
602static unsigned int bits_width(const struct fw_config_field_bits *bits)
603{
604 return 1 + bits->end_bit - bits->start_bit;
605}
606
607static uint64_t calc_option_value(const struct fw_config_field *field,
608 const struct fw_config_option *option)
609{
610 uint64_t value = 0;
611 uint64_t original = option->value;
612
613 struct fw_config_field_bits *bits = field->bits;
614 while (bits) {
615 const unsigned int width = bits_width(bits);
616 const uint64_t orig_mask = (1ull << width) - 1ull;
617 const uint64_t orig = (original & orig_mask);
618 value |= (orig << bits->start_bit);
619
620 original >>= width;
621 bits = bits->next;
622 }
623
624 return value;
625}
626
Duncan Laurie47b7b342020-05-15 15:39:08 -0700627static void emit_fw_config(FILE *fil)
628{
629 struct fw_config_field *field = fw_config_fields;
630
631 if (!field)
632 return;
633
Duncan Laurie47b7b342020-05-15 15:39:08 -0700634 while (field) {
635 struct fw_config_option *option = field->options;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600636 uint64_t mask;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700637
638 fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n",
639 field->name, field->name);
640
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600641 mask = compute_fw_config_mask(field->bits);
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600642 fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%" PRIx64 "\n",
Duncan Laurie47b7b342020-05-15 15:39:08 -0700643 field->name, mask);
644
645 while (option) {
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600646 const uint64_t value = calc_option_value(field, option);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700647 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n",
648 field->name, option->name, option->name);
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600649 fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%"
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -0600650 PRIx64 "\n", field->name, option->name, value);
Duncan Laurie47b7b342020-05-15 15:39:08 -0700651 option = option->next;
652 }
653
654 field = field->next;
655 }
656
657 fprintf(fil, "\n");
658}
659
660static int emit_fw_config_probe(FILE *fil, struct device *dev)
661{
662 struct fw_config_probe *probe = dev->probe;
663
664 fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name);
665
666 while (probe) {
667 /* Find matching field. */
668 struct fw_config_field *field;
669 struct fw_config_option *option;
Tim Wawrzynczak24b4af62020-10-01 15:41:31 -0600670 uint64_t mask, value;
Duncan Laurie47b7b342020-05-15 15:39:08 -0700671
672 field = find_fw_config_field(probe->field);
673 if (!field) {
674 printf("ERROR: fw_config_probe field %s not found\n", probe->field);
675 return -1;
676 }
677 option = find_fw_config_option(field, probe->option);
678 if (!option) {
679 printf("ERROR: fw_config_probe field %s option %s not found\n",
680 probe->field, probe->option);
681 return -1;
682 }
683
684 /* Fill out the probe structure with values from emit_fw_config(). */
685 fprintf(fil, "\t{\n");
686 fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field);
687 fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n",
688 probe->field, probe->option);
689 fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field);
690 fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n",
691 probe->field, probe->option);
692 fprintf(fil, "\t},\n");
693
694 probe = probe->next;
695 }
696
697 /* Add empty entry to mark end of list. */
698 fprintf(fil, "\t{ }\n};\n");
699 return 0;
700}
701
Nico Huberc0fc38e2022-08-06 19:02:59 +0200702/* Enqueue identifier to list with head `*it`, if not already present. */
703void add_identifier(struct identifier **it, const char *id)
704{
705 for (; *it != NULL; it = &(*it)->next) {
706 if (!strcmp((*it)->id, id))
707 return;
708 }
709
710 *it = S_ALLOC(sizeof(**it));
711 (*it)->id = id;
712}
713
714void add_device_ops(struct bus *bus, char *ops_id)
715{
716 if (bus->dev->ops_id) {
717 printf("ERROR: Device operations may only be specified once,\n"
718 " found '%s', '%s'.\n", bus->dev->ops_id, ops_id);
719 exit(1);
720 }
721
722 add_identifier(&device_operations, ops_id);
723 bus->dev->ops_id = ops_id;
724}
725
Furquan Shaikh93198262018-06-03 04:22:17 -0700726/*
727 * Allocate a new bus for the provided device.
728 * - If this is the first bus being allocated under this device, then its id
729 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
730 * - If this is not the first bus under this device, then its id is set to 1
731 * plus the id of last bus and newly allocated bus is added to the list of
732 * buses under the device. last_bus is updated to point to the newly
733 * allocated bus.
734 */
735static void alloc_bus(struct device *dev)
736{
737 struct bus *bus = S_ALLOC(sizeof(*bus));
738
739 bus->dev = dev;
740
741 if (dev->last_bus == NULL) {
742 bus->id = 0;
743 dev->bus = bus;
744 } else {
745 bus->id = dev->last_bus->id + 1;
746 dev->last_bus->next_bus = bus;
747 }
748
749 dev->last_bus = bus;
750}
751
752/*
753 * Allocate a new device under the given parent. This function allocates a new
754 * device structure under the provided parent bus and allocates a bus structure
755 * under the newly allocated device.
756 */
757static struct device *alloc_dev(struct bus *parent)
758{
759 struct device *dev = S_ALLOC(sizeof(*dev));
760
Furquan Shaikh93198262018-06-03 04:22:17 -0700761 dev->parent = parent;
762 dev->subsystem_vendor = -1;
763 dev->subsystem_device = -1;
764
765 alloc_bus(dev);
766
767 return dev;
768}
769
770/*
771 * This function scans the children of given bus to see if any device matches
772 * the new device that is requested.
773 *
774 * Returns pointer to the node if found, else NULL.
775 */
776static struct device *get_dev(struct bus *parent, int path_a, int path_b,
777 int bustype, struct chip_instance *chip_instance)
778{
779 struct device *child = parent->children;
780
781 while (child) {
782 if ((child->path_a == path_a) && (child->path_b == path_b) &&
783 (child->bustype == bustype) &&
784 (child->chip_instance == chip_instance))
785 return child;
786
787 child = child->sibling;
788 }
789
790 return NULL;
791}
792
Furquan Shaikh27efc502018-06-22 09:19:15 -0700793/*
794 * Add given node as child of the provided parent. If this is the first child of
795 * the parent, update parent->children pointer as well.
796 */
797static void set_new_child(struct bus *parent, struct device *child)
798{
799 struct device *c = parent->children;
800 if (c) {
801 while (c->sibling)
802 c = c->sibling;
803 c->sibling = child;
804 } else
805 parent->children = child;
806
807 child->sibling = NULL;
808 child->parent = parent;
809}
810
Nico Huber8e1ea522020-06-03 10:20:07 -0700811static const struct device *find_alias(const struct device *const parent,
812 const char *const alias)
813{
814 if (parent->alias && !strcmp(parent->alias, alias))
815 return parent;
816
817 const struct bus *bus;
818 for (bus = parent->bus; bus; bus = bus->next_bus) {
819 const struct device *child;
820 for (child = bus->children; child; child = child->sibling) {
821 const struct device *const ret = find_alias(child, alias);
822 if (ret)
823 return ret;
824 }
825 }
826
827 return NULL;
828}
829
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700830static struct device *new_device_with_path(struct bus *parent,
831 struct chip_instance *chip_instance,
832 const int bustype, int path_a, int path_b,
833 char *alias, int status)
Martin Rothbec07532016-08-05 18:32:18 -0600834{
Furquan Shaikh93198262018-06-03 04:22:17 -0700835 struct device *new_d;
836
Furquan Shaikh93198262018-06-03 04:22:17 -0700837 /* If device is found under parent, no need to allocate new device. */
838 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
839 if (new_d) {
840 alloc_bus(new_d);
841 return new_d;
842 }
843
844 new_d = alloc_dev(parent);
845
846 new_d->bustype = bustype;
847
848 new_d->path_a = path_a;
849 new_d->path_b = path_b;
Nico Huber8e1ea522020-06-03 10:20:07 -0700850 new_d->alias = alias;
Furquan Shaikh93198262018-06-03 04:22:17 -0700851
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800852 new_d->enabled = status & 0x01;
853 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000854 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700855 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000856
Furquan Shaikh27efc502018-06-22 09:19:15 -0700857 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000858
Furquan Shaikha9b64292018-05-31 07:52:00 -0700859 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200860 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000861 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200862 break;
863
864 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000865 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200866 break;
867
868 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700869 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200870 break;
871
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800872 case CPU_CLUSTER:
873 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200874 break;
875
Aaron Durbinffda804b2014-09-03 12:40:15 -0500876 case CPU:
877 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
878 break;
879
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800880 case DOMAIN:
881 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200882 break;
883
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700884 case GENERIC:
885 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
886 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800887
888 case SPI:
889 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
890 break;
891
Duncan Lauriebae9f852018-05-07 14:18:13 -0700892 case USB:
893 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
894 break;
895
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800896 case MMIO:
897 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
898 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600899
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100900 case GPIO:
901 new_d->path = ".type=DEVICE_PATH_GPIO,{.gpio={ .id = 0x%x }}";
902 break;
Mario Scheithauer67f63e72022-11-02 15:57:10 +0100903
904 case MDIO:
905 new_d->path = ".type=DEVICE_PATH_MDIO,{.mdio={ .addr = 0x%x }}";
906 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000907 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800908
Patrick Georgi114e7b22010-05-05 11:19:50 +0000909 return new_d;
910}
911
Duncan Lauriee335c2e2020-07-29 16:28:43 -0700912struct device *new_device_reference(struct bus *parent,
913 struct chip_instance *chip_instance,
914 const char *reference, int status)
915{
916 const struct device *dev = find_alias(&base_root_dev, reference);
917
918 if (!dev) {
919 printf("ERROR: Unable to find device reference %s\n", reference);
920 exit(1);
921 }
922
923 return new_device_with_path(parent, chip_instance, dev->bustype, dev->path_a,
924 dev->path_b, NULL, status);
925}
926
927struct device *new_device_raw(struct bus *parent,
928 struct chip_instance *chip_instance,
929 const int bustype, const char *devnum,
930 char *alias, int status)
931{
932 char *tmp;
933 int path_a;
934 int path_b = 0;
935
936 /* Check for alias name conflicts. */
937 if (alias && find_alias(root_parent->dev, alias)) {
938 printf("ERROR: Alias already exists: %s\n", alias);
939 exit(1);
940 }
941
942 path_a = strtol(devnum, &tmp, 16);
943 if (*tmp == '.') {
944 tmp++;
945 path_b = strtol(tmp, NULL, 16);
946 }
947
948 return new_device_with_path(parent, chip_instance, bustype, path_a, path_b, alias,
949 status);
950}
951
Furquan Shaikh27efc502018-06-22 09:19:15 -0700952static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600953{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700954 struct resource *r = S_ALLOC(sizeof(struct resource));
955
Patrick Georgi114e7b22010-05-05 11:19:50 +0000956 r->type = type;
957 r->index = index;
958 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000959 if (dev->res) {
960 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600961 while (head->next)
962 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000963 head->next = r;
964 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000965 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000966 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000967}
968
Furquan Shaikh27efc502018-06-22 09:19:15 -0700969void add_resource(struct bus *bus, int type, int index, int base)
970{
971 new_resource(bus->dev, type, index, base);
972}
973
Nico Huberd09459f2020-05-26 22:13:09 +0200974static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600975{
Nico Huberd09459f2020-05-26 22:13:09 +0200976 struct reg *const r = S_ALLOC(sizeof(struct reg));
977 struct reg *prev = NULL;
978 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700979
Patrick Georgi114e7b22010-05-05 11:19:50 +0000980 r->key = name;
981 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200982
983 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
984 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000985 if (sort == 0) {
Patrick Georgi51933122020-11-02 18:08:14 +0100986 printf("ERROR: duplicate 'register' key '%s'.\n", r->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000987 exit(1);
988 }
Nico Huberd09459f2020-05-26 22:13:09 +0200989 if (sort < 0)
990 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000991 }
Nico Huberd09459f2020-05-26 22:13:09 +0200992 r->next = cur;
993 if (prev)
994 prev->next = r;
995 else
996 *head = r;
997}
998
999void add_register(struct chip_instance *chip_instance, char *name, char *val)
1000{
1001 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001002}
1003
Nico Huber8e1ea522020-06-03 10:20:07 -07001004void add_reference(struct chip_instance *const chip_instance,
1005 char *const name, char *const alias)
1006{
1007 add_reg(&chip_instance->ref, name, alias);
1008}
1009
1010static void set_reference(struct chip_instance *const chip_instance,
1011 char *const name, char *const alias)
1012{
1013 const struct device *const dev = find_alias(&base_root_dev, alias);
1014 if (!dev) {
1015 printf("ERROR: Cannot find device alias '%s'.\n", alias);
1016 exit(1);
1017 }
1018
1019 char *const ref_name = S_ALLOC(strlen(dev->name) + 2);
1020 sprintf(ref_name, "&%s", dev->name);
1021 add_register(chip_instance, name, ref_name);
1022}
1023
1024static void update_references(FILE *file, FILE *head, struct device *dev,
1025 struct device *next)
1026{
1027 struct reg *ref;
1028
1029 for (ref = dev->chip_instance->ref; ref; ref = ref->next)
1030 set_reference(dev->chip_instance, ref->key, ref->value);
1031}
1032
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001033void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
1034 char *data_width)
1035{
1036 struct device *dev = bus->dev;
1037
1038 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
1039 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
1040 exit(1);
1041 }
1042
1043 dev->smbios_slot_type = type;
1044 dev->smbios_slot_length = length;
1045 dev->smbios_slot_data_width = data_width;
1046 dev->smbios_slot_designation = designation;
1047}
1048
Angel Pons437da712021-09-03 16:51:40 +02001049void add_smbios_dev_info(struct bus *bus, long instance_id, const char *refdes)
1050{
1051 struct device *dev = bus->dev;
1052
1053 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
1054 printf("ERROR: 'dev_info' only allowed for PCI devices\n");
1055 exit(1);
1056 }
1057
1058 if (instance_id < 0 || instance_id > UINT8_MAX) {
1059 printf("ERROR: SMBIOS dev info instance ID '%ld' out of range\n", instance_id);
1060 exit(1);
1061 }
1062
1063 dev->smbios_instance_id_valid = 1;
1064 dev->smbios_instance_id = (unsigned int)instance_id;
1065 dev->smbios_refdes = refdes;
1066}
1067
Furquan Shaikh93198262018-06-03 04:22:17 -07001068void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -06001069 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +00001070{
Furquan Shaikh93198262018-06-03 04:22:17 -07001071 struct device *dev = bus->dev;
1072
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001073 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001074 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
1075 exit(1);
1076 }
1077
1078 dev->subsystem_vendor = vendor;
1079 dev->subsystem_device = device;
1080 dev->inherit_subsystem = inherit;
1081}
1082
Furquan Shaikh93198262018-06-03 04:22:17 -07001083static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -06001084{
Furquan Shaikh93198262018-06-03 04:22:17 -07001085 struct bus *bus = dev->bus;
1086
1087 while (bus) {
1088 if (bus->children)
1089 return 1;
1090 bus = bus->next_bus;
1091 }
1092
1093 return 0;
1094}
1095
Nico Huber17e9bcb2019-09-20 12:05:51 +02001096static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -07001097{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -07001098 static int dev_id;
1099
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001100 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001101 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001102 ptr->name);
1103 return;
1104 }
1105
Furquan Shaikh0df32c82021-09-16 16:05:56 -07001106 char *name;
1107
1108 if (ptr->alias) {
1109 name = S_ALLOC(6 + strlen(ptr->alias));
1110 sprintf(name, "_dev_%s", ptr->alias);
1111 } else {
1112 name = S_ALLOC(11);
1113 sprintf(name, "_dev_%d", dev_id++);
1114 }
1115
Furquan Shaikh4ebe9532020-05-02 15:34:42 -07001116 ptr->name = name;
1117
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001118 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001119 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001120 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001121 ptr->name);
1122 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001123 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -06001124 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -07001125
Furquan Shaikh93198262018-06-03 04:22:17 -07001126 if (next)
1127 return;
1128
1129 fprintf(fil,
1130 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
1131 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001132}
1133
Angel Ponsd56d2a82021-09-03 16:55:25 +02001134static void emit_smbios_data(FILE *fil, struct device *ptr)
1135{
1136 fprintf(fil, "#if !DEVTREE_EARLY\n");
1137 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
1138
1139 /* SMBIOS types start at 1, if zero it hasn't been set */
1140 if (ptr->smbios_slot_type)
1141 fprintf(fil, "\t.smbios_slot_type = %s,\n",
1142 ptr->smbios_slot_type);
1143 if (ptr->smbios_slot_data_width)
1144 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
1145 ptr->smbios_slot_data_width);
1146 if (ptr->smbios_slot_designation)
1147 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
1148 ptr->smbios_slot_designation);
1149 if (ptr->smbios_slot_length)
1150 fprintf(fil, "\t.smbios_slot_length = %s,\n",
1151 ptr->smbios_slot_length);
1152
Angel Pons437da712021-09-03 16:51:40 +02001153 /* Fill in SMBIOS type41 fields */
1154 if (ptr->smbios_instance_id_valid) {
1155 fprintf(fil, "\t.smbios_instance_id_valid = true,\n");
1156 fprintf(fil, "\t.smbios_instance_id = %u,\n", ptr->smbios_instance_id);
1157 if (ptr->smbios_refdes)
1158 fprintf(fil, "\t.smbios_refdes = \"%s\",\n", ptr->smbios_refdes);
1159 }
1160
Angel Ponsd56d2a82021-09-03 16:55:25 +02001161 fprintf(fil, "#endif\n");
1162 fprintf(fil, "#endif\n");
1163}
1164
Furquan Shaikh93198262018-06-03 04:22:17 -07001165static void emit_resources(FILE *fil, struct device *ptr)
1166{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001167 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -07001168 return;
1169
1170 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001171 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001172 struct resource *r = ptr->res;
1173 while (r) {
1174 fprintf(fil,
1175 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
1176 if (r->type == IRQ)
1177 fprintf(fil, "IRQ");
1178 if (r->type == DRQ)
1179 fprintf(fil, "DRQ");
1180 if (r->type == IO)
1181 fprintf(fil, "IO");
1182 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
1183 r->base);
1184 if (r->next)
1185 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
1186 i++);
1187 else
1188 fprintf(fil, ".next=NULL },\n");
1189 r = r->next;
1190 }
1191
1192 fprintf(fil, "\t };\n");
1193}
1194
1195static void emit_bus(FILE *fil, struct bus *bus)
1196{
1197 fprintf(fil, "\t\t[%d] = {\n", bus->id);
1198 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
1199 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
1200 if (bus->children)
1201 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
1202
1203 if (bus->next_bus)
1204 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
1205 bus->id + 1);
1206 else
1207 fprintf(fil, "\t\t\t.next = NULL,\n");
1208 fprintf(fil, "\t\t},\n");
1209}
1210
1211static void emit_dev_links(FILE *fil, struct device *ptr)
1212{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001213 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -07001214 ptr->name);
1215
1216 struct bus *bus = ptr->bus;
1217
1218 while (bus) {
1219 emit_bus(fil, bus);
1220 bus = bus->next_bus;
1221 }
1222
1223 fprintf(fil, "\t};\n");
1224}
1225
Furquan Shaikhfceca922021-01-06 22:36:59 -08001226static struct chip_instance *get_chip_instance(const struct device *dev)
Sven Schnelle0fa50a12012-06-21 22:19:48 +02001227{
Furquan Shaikhfceca922021-01-06 22:36:59 -08001228 struct chip_instance *chip_ins = dev->chip_instance;
Furquan Shaikhbbade242020-05-02 16:05:29 -07001229 /*
1230 * If the chip instance of device has base_chip_instance pointer set, then follow that
1231 * to update the chip instance for current device.
1232 */
1233 if (chip_ins->base_chip_instance)
1234 chip_ins = chip_ins->base_chip_instance;
1235
Furquan Shaikhfceca922021-01-06 22:36:59 -08001236 return chip_ins;
1237}
1238
1239static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
1240{
Furquan Shaikhfceca922021-01-06 22:36:59 -08001241 struct chip_instance *chip_ins = get_chip_instance(ptr);
1242 int has_children = dev_has_children(ptr);
1243
Duncan Laurie47b7b342020-05-15 15:39:08 -07001244 /* Emit probe structures. */
1245 if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) {
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001246 if (head)
1247 fclose(head);
Duncan Laurie47b7b342020-05-15 15:39:08 -07001248 fclose(fil);
1249 exit(1);
1250 }
1251
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001252 if (ptr == &base_root_dev)
1253 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
1254 else
1255 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
1256
Furquan Shaikh93198262018-06-03 04:22:17 -07001257 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001258
1259 /*
Nico Huberc0fc38e2022-08-06 19:02:59 +02001260 * ops field can be set in the devicetree. If unspecified, it is set
1261 * to default_dev_ops_root only for the root device, other devices
1262 * get it set by the driver at runtime.
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001263 */
Nico Huberc0fc38e2022-08-06 19:02:59 +02001264 if (ptr->ops_id)
1265 fprintf(fil, "\t.ops = &%s,\n", ptr->ops_id);
1266 else if (ptr == &base_root_dev)
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -07001267 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
1268 else
1269 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001270 fprintf(fil, "#endif\n");
1271 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
1272 ptr->parent->id);
1273 fprintf(fil, "\t.path = {");
1274 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
1275 fprintf(fil, "},\n");
1276 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +08001277 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +00001278 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -07001279 fprintf(fil, "\t.on_mainboard = 1,\n");
1280 if (ptr->subsystem_vendor > 0)
1281 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
1282 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +00001283
Furquan Shaikh93198262018-06-03 04:22:17 -07001284 if (ptr->subsystem_device > 0)
1285 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
1286 ptr->subsystem_device);
1287
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -07001288 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -07001289 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -06001290 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -07001291 }
1292 if (has_children)
1293 fprintf(fil, "\t.link_list = &%s_links[0],\n",
1294 ptr->name);
1295 else
1296 fprintf(fil, "\t.link_list = NULL,\n");
1297 if (ptr->sibling)
1298 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -06001299 else
1300 fprintf(fil, "\t.sibling = NULL,\n");
Duncan Laurie47b7b342020-05-15 15:39:08 -07001301 if (ptr->probe)
1302 fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name);
Furquan Shaikhe59ad2e2021-05-22 07:36:58 -07001303 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh93198262018-06-03 04:22:17 -07001304 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
1305 chip_ins->chip->name_underscore);
1306 if (chip_ins == &mainboard_instance)
1307 fprintf(fil, "\t.name = mainboard_name,\n");
1308 fprintf(fil, "#endif\n");
1309 if (chip_ins->chip->chiph_exists)
1310 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
1311 chip_ins->chip->name_underscore, chip_ins->id);
1312 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +02001313 fprintf(fil, "\t.next=&%s,\n", next->name);
Angel Ponsd56d2a82021-09-03 16:55:25 +02001314
1315 emit_smbios_data(fil, ptr);
1316
Furquan Shaikh93198262018-06-03 04:22:17 -07001317 fprintf(fil, "};\n");
1318
1319 emit_resources(fil, ptr);
1320
1321 if (has_children)
1322 emit_dev_links(fil, ptr);
1323}
1324
Nico Huber17e9bcb2019-09-20 12:05:51 +02001325static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001326{
Furquan Shaikhfceca922021-01-06 22:36:59 -08001327 struct chip_instance *chip_ins = get_chip_instance(ptr);
1328
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001329 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +02001330 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
Arthur Heymans166de992023-07-13 11:32:30 +02001331 fprintf(head, "extern DEVTREE_CONST struct device *const __pci_%d_%02x_%d;\n",
1332 ptr->parent->dev->path_a, ptr->path_a, ptr->path_b);
1333 fprintf(fil, "DEVTREE_CONST struct device *const __pci_%d_%02x_%d = &%s;\n",
1334 ptr->parent->dev->path_a, ptr->path_a, ptr->path_b, ptr->name);
Furquan Shaikhfceca922021-01-06 22:36:59 -08001335
1336 if (chip_ins->chip->chiph_exists) {
Arthur Heymans166de992023-07-13 11:32:30 +02001337 fprintf(head, "extern DEVTREE_CONST void *const __pci_%d_%02x_%d_config;\n",
1338 ptr->parent->dev->path_a, ptr->path_a, ptr->path_b);
1339 fprintf(fil, "DEVTREE_CONST void *const __pci_%d_%02x_%d_config = &%s_info_%d;\n",
1340 ptr->parent->dev->path_a, ptr->path_a, ptr->path_b,
1341 chip_ins->chip->name_underscore, chip_ins->id);
Furquan Shaikhfceca922021-01-06 22:36:59 -08001342 }
Nico Huber17e9bcb2019-09-20 12:05:51 +02001343 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001344
Nico Huber17e9bcb2019-09-20 12:05:51 +02001345 if (ptr->bustype == PNP) {
Furquan Shaikh708f25e2021-01-08 09:54:54 -08001346 fprintf(head, "extern DEVTREE_CONST struct device *const __pnp_%04x_%02x;\n",
Nico Huber17e9bcb2019-09-20 12:05:51 +02001347 ptr->path_a, ptr->path_b);
Furquan Shaikh708f25e2021-01-08 09:54:54 -08001348 fprintf(fil, "DEVTREE_CONST struct device *const __pnp_%04x_%02x = &%s;\n",
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001349 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001350 }
Furquan Shaikh0df32c82021-09-16 16:05:56 -07001351
1352 if (ptr->alias) {
1353 fprintf(head, "extern DEVTREE_CONST struct device *const %s_ptr;\n", ptr->name);
1354 fprintf(fil, "DEVTREE_CONST struct device *const %s_ptr = &%s;\n",
1355 ptr->name, ptr->name);
1356 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001357}
1358
Furquan Shaikh93198262018-06-03 04:22:17 -07001359static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
1360 struct device *d)
1361{
1362 while (d) {
1363 enqueue_tail(bfs_q_head, d);
1364 d = d->sibling;
1365 }
1366}
1367
1368static void add_children_to_queue(struct queue_entry **bfs_q_head,
1369 struct device *d)
1370{
1371 struct bus *bus = d->bus;
1372
1373 while (bus) {
1374 if (bus->children)
1375 add_siblings_to_queue(bfs_q_head, bus->children);
1376 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +00001377 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001378}
1379
Nico Huber17e9bcb2019-09-20 12:05:51 +02001380static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
1381 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -07001382 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -06001383{
Furquan Shaikh93198262018-06-03 04:22:17 -07001384 struct queue_entry *bfs_q_head = NULL;
1385
1386 enqueue_tail(&bfs_q_head, ptr);
1387
1388 while ((ptr = dequeue_head(&bfs_q_head))) {
1389 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +02001390 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -07001391 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00001392}
1393
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001394static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001395{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001396 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001397
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001398 while (chip) {
1399 if (chip->chiph_exists)
1400 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
1401 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001402 }
1403 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
1404 fprintf(fil,
1405 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001406
1407 chip = tmp;
1408 while (chip) {
Kyösti Mälkkib2a10f82019-08-17 06:28:40 +03001409 /* A lot of cpus do not define chip_operations at all, and the ones
1410 that do only initialise .name. */
1411 if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) {
1412 fprintf(fil,
1413 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
1414 chip->name_underscore);
1415 } else {
1416 fprintf(fil, "extern struct chip_operations %s_ops;\n",
1417 chip->name_underscore);
1418 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001419 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -07001420 }
1421 fprintf(fil, "#endif\n");
1422}
1423
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001424static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
1425{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +02001426 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001427 instance->chip->name_underscore,
1428 instance->chip->name_underscore,
1429 instance->id);
1430
1431 if (instance->reg) {
1432 fprintf(fil, "\n");
1433 struct reg *r = instance->reg;
1434 while (r) {
1435 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
1436 r = r->next;
1437 }
1438 }
1439 fprintf(fil, "};\n\n");
1440}
1441
Nico Huber8e1ea522020-06-03 10:20:07 -07001442static void emit_chip_configs(FILE *fil)
Furquan Shaikh79e84122018-05-30 15:09:09 -07001443{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001444 struct chip *chip = chip_header.next;
1445 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001446 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001447
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001448 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -07001449 if (!chip->chiph_exists)
1450 continue;
1451
Furquan Shaikh9f681d22020-05-02 15:51:02 -07001452 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001453 instance = chip->instance;
1454 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -07001455 /*
1456 * Emit this chip instance only if there is no forwarding pointer to the
1457 * base tree chip instance.
1458 */
1459 if (instance->base_chip_instance == NULL) {
1460 instance->id = chip_id++;
1461 emit_chip_instance(fil, instance);
1462 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -07001463 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -07001464 }
1465 }
1466}
1467
Nico Huberc0fc38e2022-08-06 19:02:59 +02001468static void emit_identifiers(FILE *fil, const char *decl, const struct identifier *it)
1469{
1470 for (; it != NULL; it = it->next)
1471 fprintf(fil, "extern %s %s;\n", decl, it->id);
1472}
1473
Nico Huber17e9bcb2019-09-20 12:05:51 +02001474static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001475 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001476{
1477 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001478
1479 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1480 /* user already gave us a subsystem vendor/device */
1481 return;
1482 }
1483
Furquan Shaikh93198262018-06-03 04:22:17 -07001484 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001485
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001486 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001487 continue;
1488
1489 if (p->inherit_subsystem) {
1490 dev->subsystem_vendor = p->subsystem_vendor;
1491 dev->subsystem_device = p->subsystem_device;
1492 break;
1493 }
1494 }
1495}
1496
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001497static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001498{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001499 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001500 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001501 perror(NULL);
1502 exit(1);
1503 }
1504
Patrick Georgi114e7b22010-05-05 11:19:50 +00001505 yyrestart(filec);
1506
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001507 root_parent = parent;
1508 linenum = 0;
1509
Patrick Georgi114e7b22010-05-05 11:19:50 +00001510 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001511
Patrick Georgi114e7b22010-05-05 11:19:50 +00001512 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001513}
1514
Furquan Shaikhb9c22e02021-08-23 23:23:23 -07001515static int device_probe_count(struct fw_config_probe *probe)
1516{
1517 int count = 0;
1518 while (probe) {
1519 probe = probe->next;
1520 count++;
1521 }
1522
1523 return count;
1524}
1525
1526/*
1527 * When overriding devices, use the following rules:
1528 * 1. If probe count matches and:
1529 * a. Entire probe list matches for both devices -> Same device, override.
1530 * b. No probe entries match -> Different devices, do not override.
1531 * c. Partial list matches -> Bad device tree entries, fail build.
1532 *
1533 * 2. If probe counts do not match and:
1534 * a. No probe entries match -> Different devices, do not override.
1535 * b. Partial list matches -> Bad device tree entries, fail build.
1536 */
1537static int device_probes_match(struct device *a, struct device *b)
1538{
1539 struct fw_config_probe *a_probe = a->probe;
1540 struct fw_config_probe *b_probe = b->probe;
1541 int a_probe_count = device_probe_count(a_probe);
1542 int b_probe_count = device_probe_count(b_probe);
1543 int match_count = 0;
1544
1545 while (a_probe) {
1546 if (check_probe_exists(b_probe, a_probe->field, a_probe->option))
1547 match_count++;
1548 a_probe = a_probe->next;
1549 }
1550
1551 if ((a_probe_count == b_probe_count) && (a_probe_count == match_count))
1552 return 1;
1553
1554 if (match_count) {
1555 printf("ERROR: devices with overlapping probes: ");
1556 printf(a->path, a->path_a, a->path_b);
1557 printf(b->path, b->path_a, b->path_b);
1558 printf("\n");
1559 exit(1);
1560 }
1561
1562 return 0;
1563}
1564
Furquan Shaikh27efc502018-06-22 09:19:15 -07001565/*
1566 * Match device nodes from base and override tree to see if they are the same
1567 * node.
1568 */
1569static int device_match(struct device *a, struct device *b)
1570{
1571 return ((a->path_a == b->path_a) &&
1572 (a->path_b == b->path_b) &&
1573 (a->bustype == b->bustype) &&
1574 (a->chip_instance->chip ==
1575 b->chip_instance->chip));
1576}
1577
1578/*
Bill XIEc61d4152019-11-21 18:16:12 +08001579 * Match resource nodes from base and override tree to see if they are the same
1580 * node.
1581 */
1582static int res_match(struct resource *a, struct resource *b)
1583{
1584 return ((a->type == b->type) &&
1585 (a->index == b->index));
1586}
1587
1588/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001589 * Add resource to device. If resource is already present, then update its base
1590 * and index. If not, then add a new resource to the device.
1591 */
1592static void update_resource(struct device *dev, struct resource *res)
1593{
1594 struct resource *base_res = dev->res;
1595
1596 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001597 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001598 base_res->base = res->base;
1599 return;
1600 }
1601 base_res = base_res->next;
1602 }
1603
1604 new_resource(dev, res->type, res->index, res->base);
1605}
1606
1607/*
1608 * Add register to chip instance. If register is already present, then update
1609 * its value. If not, then add a new register to the chip instance.
1610 */
Nico Huber8e1ea522020-06-03 10:20:07 -07001611static void update_register(struct reg **const head, struct reg *reg)
Furquan Shaikh27efc502018-06-22 09:19:15 -07001612{
Nico Huber8e1ea522020-06-03 10:20:07 -07001613 struct reg *base_reg = *head;
Furquan Shaikh27efc502018-06-22 09:19:15 -07001614
1615 while (base_reg) {
1616 if (!strcmp(base_reg->key, reg->key)) {
1617 base_reg->value = reg->value;
1618 return;
1619 }
1620 base_reg = base_reg->next;
1621 }
1622
Nico Huber8e1ea522020-06-03 10:20:07 -07001623 add_reg(head, reg->key, reg->value);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001624}
1625
1626static void override_devicetree(struct bus *base_parent,
1627 struct bus *override_parent);
1628
1629/*
1630 * Update the base device properties using the properties of override device. In
1631 * addition to that, call override_devicetree for all the buses under the
1632 * override device.
1633 *
1634 * Override Rules:
1635 * +--------------------+--------------------------------------------+
1636 * | | |
1637 * |struct device member| Rule |
1638 * | | |
1639 * +-----------------------------------------------------------------+
1640 * | | |
1641 * | id | Unchanged. This is used to generate device |
1642 * | | structure name in static.c. So, no need to |
1643 * | | override. |
1644 * | | |
1645 * +-----------------------------------------------------------------+
1646 * | | |
1647 * | enabled | Copy enabled state from override device. |
1648 * | | This allows variants to override device |
1649 * | | state. |
1650 * | | |
1651 * +-----------------------------------------------------------------+
1652 * | | |
1653 * | subsystem_vendor | Copy from override device only if any one |
1654 * | subsystem_device | of the ids is non-zero. |
1655 * | | |
1656 * +-----------------------------------------------------------------+
1657 * | | |
1658 * | inherit_subsystem | Copy from override device only if it is |
1659 * | | non-zero. This allows variant to only |
1660 * | | enable inherit flag for a device. |
1661 * | | |
1662 * +-----------------------------------------------------------------+
1663 * | | |
1664 * | path | Unchanged since these are same for both |
1665 * | path_a | base and override device (Used for |
1666 * | path_b | matching devices). |
1667 * | | |
1668 * +-----------------------------------------------------------------+
1669 * | | |
1670 * | bustype | Unchanged since this is same for both base |
1671 * | | and override device (User for matching |
1672 * | | devices). |
1673 * | | |
1674 * +-----------------------------------------------------------------+
1675 * | | |
1676 * | pci_irq_info | Unchanged. |
1677 * | | |
1678 * +-----------------------------------------------------------------+
1679 * | | |
1680 * | parent | Unchanged. This is meaningful only within |
1681 * | sibling | the parse tree, hence not being copied. |
1682 * | | |
1683 * +-----------------------------------------------------------------+
1684 * | | |
1685 * | res | Each resource that is present in override |
1686 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001687 * | | 1. If resource of same type and index is |
1688 * | | present in base device, then base of |
1689 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001690 * | | 2. If not, then a new resource is allocated|
1691 * | | under the base device using type, index |
1692 * | | and base from override res. |
1693 * | | |
1694 * +-----------------------------------------------------------------+
1695 * | | |
Nico Huber8e1ea522020-06-03 10:20:07 -07001696 * | ref | Each reference that is present in override |
1697 * | | device is copied over to base device with |
1698 * | | the same rules as registers. |
1699 * | | |
1700 * +-----------------------------------------------------------------+
1701 * | | |
1702 * | alias | Base device alias is copied to override. |
1703 * | | Override devices cannot change/remove an |
1704 * | | existing alias, but they can add an alias |
1705 * | | if one does not exist. |
1706 * | | |
1707 * +-----------------------------------------------------------------+
1708 * | | |
Frans Hendriks3a7db272021-01-20 07:40:05 +01001709 * | smbios_slot info | Copy SMBIOS slot information from override.|
1710 * | | This allows variants to override PCI(e) |
1711 * | | slot information in SMBIOS tables. |
1712 * | | |
1713 * +-----------------------------------------------------------------+
1714 * | | |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001715 * | chip_instance | Each register of chip_instance is copied |
1716 * | | over from override device to base device: |
1717 * | | 1. If register with same key is present in |
1718 * | | base device, then value of the register |
1719 * | | is copied. |
1720 * | | 2. If not, then a new register is allocated|
1721 * | | under the base chip_instance using key |
1722 * | | and value from override register. |
1723 * | | |
1724 * +-----------------------------------------------------------------+
1725 * | | |
1726 * | bus | Recursively call override_devicetree on |
1727 * | last_bus | each bus of override device. It is assumed |
1728 * | | that bus with id X under base device |
1729 * | | to bus with id X under override device. If |
1730 * | | override device has more buses than base |
1731 * | | device, then new buses are allocated under |
1732 * | | base device. |
1733 * | | |
1734 * +-----------------------------------------------------------------+
1735 */
1736static void update_device(struct device *base_dev, struct device *override_dev)
1737{
1738 /*
1739 * Copy the enabled state of override device to base device. This allows
1740 * override tree to enable or disable a particular device.
1741 */
1742 base_dev->enabled = override_dev->enabled;
1743
1744 /*
Duncan Laurie7f6a4842020-10-28 14:22:34 -07001745 * Copy the hidden state of override device to base device. This allows
1746 * override tree to hide or unhide a particular device.
1747 */
1748 base_dev->hidden = override_dev->hidden;
1749
1750 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001751 * Copy subsystem vendor and device ids from override device to base
1752 * device only if the ids are non-zero in override device. Else, honor
1753 * the values in base device.
1754 */
1755 if (override_dev->subsystem_vendor ||
1756 override_dev->subsystem_device) {
1757 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1758 base_dev->subsystem_device = override_dev->subsystem_device;
1759 }
1760
1761 /*
1762 * Copy value of inherity_subsystem from override device to base device
1763 * only if it is non-zero in override device. This allows override
1764 * tree to only enable inhert flag for a device.
1765 */
1766 if (override_dev->inherit_subsystem)
1767 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1768
1769 /*
1770 * Copy resources of override device to base device.
1771 * 1. If resource is already present in base device, then index and base
1772 * of the resource will be copied over.
1773 * 2. If resource is not already present in base device, a new resource
1774 * will be allocated.
1775 */
1776 struct resource *res = override_dev->res;
1777 while (res) {
1778 update_resource(base_dev, res);
1779 res = res->next;
1780 }
1781
1782 /*
1783 * Copy registers of override chip instance to base chip instance.
1784 * 1. If register key is already present in base chip instance, then
1785 * value for the register is copied over.
1786 * 2. If register key is not already present in base chip instance, then
1787 * a new register will be allocated.
1788 */
1789 struct reg *reg = override_dev->chip_instance->reg;
1790 while (reg) {
Nico Huber8e1ea522020-06-03 10:20:07 -07001791 update_register(&base_dev->chip_instance->reg, reg);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001792 reg = reg->next;
1793 }
1794
Nico Huber8e1ea522020-06-03 10:20:07 -07001795 /* Copy references just as with registers. */
1796 reg = override_dev->chip_instance->ref;
1797 while (reg) {
1798 update_register(&base_dev->chip_instance->ref, reg);
1799 reg = reg->next;
1800 }
1801
1802 /* Check for alias name conflicts. */
1803 if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) {
1804 printf("ERROR: alias already exists: %s\n", override_dev->alias);
1805 exit(1);
1806 }
1807
1808 /*
1809 * Copy alias from base device.
1810 *
1811 * Override devices cannot change/remove an existing alias,
1812 * but they can add an alias to a device if one does not exist yet.
1813 */
1814 if (base_dev->alias)
1815 override_dev->alias = base_dev->alias;
1816 else
1817 base_dev->alias = override_dev->alias;
1818
Furquan Shaikh27efc502018-06-22 09:19:15 -07001819 /*
Duncan Laurie47b7b342020-05-15 15:39:08 -07001820 * Use probe list from override device in place of base device, in order
1821 * to allow an override to remove a probe from the base device.
1822 */
1823 base_dev->probe = override_dev->probe;
1824
Frans Hendriks3a7db272021-01-20 07:40:05 +01001825 /* Copy SMBIOS slot information from base device */
1826 base_dev->smbios_slot_type = override_dev->smbios_slot_type;
1827 base_dev->smbios_slot_length = override_dev->smbios_slot_length;
1828 base_dev->smbios_slot_data_width = override_dev->smbios_slot_data_width;
1829 base_dev->smbios_slot_designation = override_dev->smbios_slot_designation;
1830
Duncan Laurie47b7b342020-05-15 15:39:08 -07001831 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001832 * Update base_chip_instance member in chip instance of override tree to forward it to
1833 * the chip instance in base tree.
1834 */
Furquan Shaikh9d1bf812021-02-17 19:07:18 -08001835 override_dev->chip_instance->base_chip_instance = get_chip_instance(base_dev);
Furquan Shaikhbbade242020-05-02 16:05:29 -07001836
Nico Huberc0fc38e2022-08-06 19:02:59 +02001837 /* Allow to override the ops of a device */
1838 if (override_dev->ops_id)
1839 base_dev->ops_id = override_dev->ops_id;
1840
Furquan Shaikhbbade242020-05-02 16:05:29 -07001841 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001842 * Now that the device properties are all copied over, look at each bus
1843 * of the override device and run override_devicetree in a recursive
1844 * manner. The assumption here is that first bus of override device
1845 * corresponds to first bus of base device and so on. If base device has
1846 * lesser buses than override tree, then new buses are allocated for it.
1847 */
1848 struct bus *override_bus = override_dev->bus;
1849 struct bus *base_bus = base_dev->bus;
1850
1851 while (override_bus) {
1852
1853 /*
1854 * If we have more buses in override tree device, then allocate
1855 * a new bus for the base tree device as well.
1856 */
1857 if (!base_bus) {
1858 alloc_bus(base_dev);
1859 base_bus = base_dev->last_bus;
1860 }
1861
1862 override_devicetree(base_dev->bus, override_dev->bus);
1863
1864 override_bus = override_bus->next_bus;
1865 base_bus = base_bus->next_bus;
1866 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001867}
1868
1869/*
1870 * Perform copy of device and properties from override parent to base parent.
1871 * This function walks through the override tree in a depth-first manner
1872 * performing following actions:
1873 * 1. If matching device is found in base tree, then copy the properties of
1874 * override device to base tree device. Call override_devicetree recursively on
1875 * the bus of override device.
1876 * 2. If matching device is not found in base tree, then set override tree
1877 * device as new child of base_parent and update the chip pointers in override
1878 * device subtree to ensure the nodes do not point to override tree chip
1879 * instance.
1880 */
1881static void override_devicetree(struct bus *base_parent,
1882 struct bus *override_parent)
1883{
1884 struct device *base_child;
1885 struct device *override_child = override_parent->children;
1886 struct device *next_child;
1887
1888 while (override_child) {
1889
1890 /* Look for a matching device in base tree. */
1891 for (base_child = base_parent->children;
1892 base_child; base_child = base_child->sibling) {
Furquan Shaikhb9c22e02021-08-23 23:23:23 -07001893 if (!device_match(base_child, override_child))
1894 continue;
1895 /* If base device has no probe statement, nothing else to compare. */
1896 if (base_child->probe == NULL)
1897 break;
1898 /*
1899 * If base device has probe statements, ensure that all probe conditions
1900 * match for base and override device.
1901 */
1902 if (device_probes_match(base_child, override_child))
Furquan Shaikh27efc502018-06-22 09:19:15 -07001903 break;
1904 }
1905
1906 next_child = override_child->sibling;
1907
1908 /*
1909 * If matching device is found, copy properties of
1910 * override_child to base_child.
1911 */
1912 if (base_child)
1913 update_device(base_child, override_child);
1914 else {
1915 /*
1916 * If matching device is not found, set override_child
1917 * as a new child of base_parent.
1918 */
1919 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001920 }
1921
1922 override_child = next_child;
1923 }
1924}
1925
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07001926static void parse_override_devicetree(const char *file, struct device *dev)
1927{
1928 parse_devicetree(file, dev->bus);
1929
1930 if (!dev_has_children(dev)) {
1931 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1932 exit(1);
1933 }
1934
1935 override_devicetree(&base_root_bus, dev->bus);
1936}
1937
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001938static void generate_outputh(FILE *f, const char *fw_conf_header, const char *device_header)
1939{
1940 fprintf(f, "#ifndef __STATIC_DEVICE_TREE_H\n");
1941 fprintf(f, "#define __STATIC_DEVICE_TREE_H\n\n");
1942
1943 fprintf(f, "#include <%s>\n", fw_conf_header);
1944 fprintf(f, "#include <%s>\n\n", device_header);
1945
1946 fprintf(f, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1947}
1948
1949static void generate_outputc(FILE *f, const char *static_header)
1950{
Tim Wawrzynczak5cd979e2021-09-14 14:09:20 -06001951 fprintf(f, "#include <boot/coreboot_tables.h>\n");
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001952 fprintf(f, "#include <device/device.h>\n");
1953 fprintf(f, "#include <device/pci.h>\n");
1954 fprintf(f, "#include <fw_config.h>\n");
Kyösti Mälkkia9dd3c32022-12-15 22:12:10 +02001955 fprintf(f, "#include <identity.h>\n");
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001956 fprintf(f, "#include <%s>\n", static_header);
1957 emit_chip_headers(f, chip_header.next);
Nico Huberc0fc38e2022-08-06 19:02:59 +02001958 emit_identifiers(f, "struct device_operations", device_operations);
Bill XIEac1362502022-07-08 16:53:21 +08001959 fprintf(f, "\n#define STORAGE static __maybe_unused DEVTREE_CONST\n\n");
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06001960
1961 walk_device_tree(NULL, NULL, &base_root_dev, inherit_subsystem_ids);
1962 fprintf(f, "\n/* pass 0 */\n");
1963 walk_device_tree(f, NULL, &base_root_dev, pass0);
1964 walk_device_tree(NULL, NULL, &base_root_dev, update_references);
1965 fprintf(f, "\n/* chip configs */\n");
1966 emit_chip_configs(f);
1967 fprintf(f, "\n/* pass 1 */\n");
1968 walk_device_tree(f, NULL, &base_root_dev, pass1);
1969}
1970
1971static void generate_outputd(FILE *gen, FILE *dev)
1972{
1973 fprintf(dev, "#ifndef __STATIC_DEVICES_H\n");
1974 fprintf(dev, "#define __STATIC_DEVICES_H\n\n");
1975 fprintf(dev, "#include <device/device.h>\n\n");
1976 fprintf(dev, "/* expose_device_names */\n");
1977 walk_device_tree(gen, dev, &base_root_dev, expose_device_names);
1978 fprintf(dev, "\n#endif /* __STATIC_DEVICE_NAMES_H */\n");
1979}
1980
1981static void generate_outputf(FILE *f)
1982{
1983 fprintf(f, "#ifndef __STATIC_FW_CONFIG_H\n");
1984 fprintf(f, "#define __STATIC_FW_CONFIG_H\n\n");
1985 emit_fw_config(f);
1986 fprintf(f, "\n#endif /* __STATIC_FW_CONFIG_H */\n");
1987}
1988
Jakub Czapiga00d71ff2022-12-08 14:33:02 +01001989static void usage(const char *name)
1990{
1991 printf("Usage: %s <options>\n", name);
1992 printf("Options:\n"
1993 " -c | --output_c : Path to output static.c file (required)\n"
1994 " -r | --output_h : Path to header static.h file (required)\n"
1995 " -d | --output_d : Path to header static_devices.h file (required)\n"
1996 " -f | --output_f : Path to header static_fw_config.h file (required)\n"
1997 " -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n"
1998 " -o | --override_devtree : Path to override devicetree file (optional)\n"
1999 " -p | --chipset_devtree : Path to chipset/SOC devicetree file (optional)\n");
2000
2001 exit(1);
2002}
2003
Furquan Shaikhde39fc72018-06-11 04:26:45 -07002004int main(int argc, char **argv)
2005{
Duncan Laurie51c83732020-06-09 11:20:29 -07002006 static const struct option long_options[] = {
Jakub Czapiga00d71ff2022-12-08 14:33:02 +01002007 { "mainboard_devtree", required_argument, NULL, 'm' },
2008 { "override_devtree", required_argument, NULL, 'o' },
2009 { "chipset_devtree", required_argument, NULL, 'p' },
2010 { "output_c", required_argument, NULL, 'c' },
2011 { "output_h", required_argument, NULL, 'r' },
2012 { "output_d", required_argument, NULL, 'd' },
2013 { "output_f", required_argument, NULL, 'f' },
2014 { "help", no_argument, NULL, 'h' },
Duncan Laurie51c83732020-06-09 11:20:29 -07002015 { }
2016 };
2017 const char *override_devtree = NULL;
2018 const char *base_devtree = NULL;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07002019 const char *chipset_devtree = NULL;
Duncan Laurie51c83732020-06-09 11:20:29 -07002020 const char *outputc = NULL;
2021 const char *outputh = NULL;
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002022 const char *outputd = NULL;
2023 const char *outputf = NULL;
Duncan Laurie51c83732020-06-09 11:20:29 -07002024 int opt, option_index;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07002025
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002026 while ((opt = getopt_long(argc, argv, "m:o:p:c:r:d:f:h", long_options,
Duncan Laurie51c83732020-06-09 11:20:29 -07002027 &option_index)) != EOF) {
2028 switch (opt) {
2029 case 'm':
2030 base_devtree = strdup(optarg);
2031 break;
2032 case 'o':
2033 override_devtree = strdup(optarg);
2034 break;
Duncan Lauriee335c2e2020-07-29 16:28:43 -07002035 case 'p':
2036 chipset_devtree = strdup(optarg);
2037 break;
Duncan Laurie51c83732020-06-09 11:20:29 -07002038 case 'c':
2039 outputc = strdup(optarg);
2040 break;
2041 case 'r':
2042 outputh = strdup(optarg);
2043 break;
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002044 case 'd':
2045 outputd = strdup(optarg);
2046 break;
2047 case 'f':
2048 outputf = strdup(optarg);
2049 break;
Duncan Laurie51c83732020-06-09 11:20:29 -07002050 case 'h':
2051 default:
Jakub Czapiga00d71ff2022-12-08 14:33:02 +01002052 usage(argv[0]);
Duncan Laurie51c83732020-06-09 11:20:29 -07002053 }
2054 }
2055
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002056 if (!base_devtree || !outputc || !outputh || !outputd || !outputf)
Jakub Czapiga00d71ff2022-12-08 14:33:02 +01002057 usage(argv[0]);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07002058
Duncan Lauriee335c2e2020-07-29 16:28:43 -07002059 if (chipset_devtree) {
2060 /* Use the chipset devicetree as the base, then override
2061 with the mainboard "base" devicetree. */
2062 parse_devicetree(chipset_devtree, &base_root_bus);
2063 parse_override_devicetree(base_devtree, &chipset_root_dev);
2064 } else {
2065 parse_devicetree(base_devtree, &base_root_bus);
2066 }
Patrick Georgi114e7b22010-05-05 11:19:50 +00002067
Duncan Lauriecbd0bd82020-06-09 11:24:10 -07002068 if (override_devtree)
2069 parse_override_devicetree(override_devtree, &override_root_dev);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07002070
Tim Wawrzynczak13e240c2021-04-28 14:03:07 -06002071
Kyösti Mälkki472d9022011-12-05 20:33:55 +02002072 FILE *autogen = fopen(outputc, "w");
2073 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06002074 fprintf(stderr, "Could not open file '%s' for writing: ",
2075 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00002076 perror(NULL);
2077 exit(1);
2078 }
2079
Nico Huber17e9bcb2019-09-20 12:05:51 +02002080 FILE *autohead = fopen(outputh, "w");
2081 if (!autohead) {
2082 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
2083 perror(NULL);
2084 fclose(autogen);
2085 exit(1);
2086 }
Nico Huber17e9bcb2019-09-20 12:05:51 +02002087
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002088 FILE *autodev = fopen(outputd, "w");
2089 if (!autodev) {
2090 fprintf(stderr, "Could not open file '%s' for writing: ", outputd);
2091 perror(NULL);
2092 fclose(autogen);
2093 fclose(autohead);
2094 exit(1);
2095 }
Furquan Shaikh79e84122018-05-30 15:09:09 -07002096
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002097 FILE *autofwconf = fopen(outputf, "w");
2098 if (!autofwconf) {
2099 fprintf(stderr, "Could not open file '%s' for writing: ", outputf);
2100 perror(NULL);
2101 fclose(autogen);
2102 fclose(autohead);
2103 fclose(autodev);
2104 exit(1);
2105 }
Sven Schnelle270a9082011-03-01 19:58:15 +00002106
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002107 char *f = strdup(outputf);
2108 assert(f);
2109 char *d = strdup(outputd);
2110 assert(d);
2111 char *h = strdup(outputh);
2112 assert(h);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02002113
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002114 const char *fw_conf_header = basename(f);
2115 const char *device_header = basename(d);
2116 const char *static_header = basename(h);
2117
2118 generate_outputh(autohead, fw_conf_header, device_header);
2119 generate_outputc(autogen, static_header);
2120 generate_outputd(autogen, autodev);
2121 generate_outputf(autofwconf);
2122
Nico Huber17e9bcb2019-09-20 12:05:51 +02002123 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02002124 fclose(autogen);
Tim Wawrzynczakba4a4902020-09-23 15:36:30 -06002125 fclose(autodev);
2126 fclose(autofwconf);
2127 free(f);
2128 free(d);
2129 free(h);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00002130
Patrick Georgi114e7b22010-05-05 11:19:50 +00002131 return 0;
2132}