blob: 6676baea9047130152c901cda7396dc864130efd [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* sconfig, coreboot device tree compiler */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi114e7b22010-05-05 11:19:50 +00003
Patrick Georgi2dbfcb72012-05-30 16:26:30 +02004#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +02005/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
6#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +02007#include <commonlib/helpers.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +00008#include "sconfig.h"
9#include "sconfig.tab.h"
10
Patrick Georgi7fc9e292010-07-15 15:59:07 +000011extern int linenum;
12
Furquan Shaikh27efc502018-06-22 09:19:15 -070013/*
14 * Maintains list of all the unique chip structures for the board.
15 * This is shared across base and override device trees since we need to
16 * generate headers for all chips added by both the trees.
17 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070018static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070019
Kyösti Mälkki472d9022011-12-05 20:33:55 +020020typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020021 UNSLASH,
22 SPLIT_1ST,
23 TO_LOWER,
24 TO_UPPER,
25} translate_t;
26
Furquan Shaikh93198262018-06-03 04:22:17 -070027/*
28 * Mainboard is assumed to have a root device whose bus is the parent of all the
29 * devices that are added by parsing the devicetree file. This device has a
30 * mainboard chip instance associated with it.
31 *
32 *
33 *
34 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070035 * | Root device | | Mainboard |
36 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070037 * | | | chip_instance | (mainboard_instance)|
38 * | +------------------------+ | |
39 * | | +----------------------+
40 * | | bus |
41 * | parent v |
42 * | +-------------------+ |
43 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070044 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070045 * | | |
46 * +-------------------+ |
47 * | |
48 * | children | chip
49 * v |
50 * X |
51 * (new devices will |
52 * be added here as |
53 * children) |
54 * |
55 * |
56 * |
57 * +-------+----------+
58 * | |
59 * | Mainboard chip +----------->X (new chips will be
60 * | (mainboard_chip) | added here)
61 * | |
62 * +------------------+
63 *
64 *
65 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070066
67/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070068static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070069
70/* Root device of override tree (if applicable). */
71static struct device override_root_dev;
72
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070073static struct chip_instance mainboard_instance;
74
Furquan Shaikhde39fc72018-06-11 04:26:45 -070075static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070076 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070077 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070078};
79
Furquan Shaikhde39fc72018-06-11 04:26:45 -070080static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070081 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070082 .chip_instance = &mainboard_instance,
83 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070084 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070085 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070086 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070087};
88
Furquan Shaikh39ac7972018-06-21 18:44:32 -070089static struct bus override_root_bus = {
90 .id = 0,
91 .dev = &override_root_dev,
92};
93
94static struct device override_root_dev = {
95 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -070096 /*
97 * Override tree root device points to the same mainboard chip instance
98 * as the base tree root device. It should not cause any side-effects
99 * since the mainboard chip instance pointer in override tree will just
100 * be ignored.
101 */
102 .chip_instance = &mainboard_instance,
103 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700104 .parent = &override_root_bus,
105 .enabled = 1,
106 .bus = &override_root_bus,
107};
108
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700109static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000110 .name = "mainboard",
111 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700112 .instance = &mainboard_instance,
113};
114
115static struct chip_instance mainboard_instance = {
116 .id = 0,
117 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000118};
119
Furquan Shaikh93198262018-06-03 04:22:17 -0700120/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700121struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000122
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700123struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700124 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700125 struct queue_entry *next;
126 struct queue_entry *prev;
127};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700128
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700129#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700130
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700131static void *s_alloc(const char *f, size_t s)
132{
133 void *data = calloc(1, s);
134 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530135 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700136 exit(1);
137 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700138 return data;
139}
140
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700141static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700142{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700143 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700144
145 e->data = data;
146 e->next = e->prev = e;
147 return e;
148}
149
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700150static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700151{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700152 struct queue_entry *tmp = new_queue_entry(data);
153 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700154
155 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700156 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700157 return;
158 }
159
160 q->prev->next = tmp;
161 tmp->prev = q->prev;
162 q->prev = tmp;
163 tmp->next = q;
164}
165
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700166static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700167{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700168 struct queue_entry *q = *q_head;
169 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700170 void *data;
171
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700172 if (!q)
173 return NULL;
174
175 tmp = q->prev;
176
Furquan Shaikh79e84122018-05-30 15:09:09 -0700177 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700178 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179 else {
180 tmp->prev->next = q;
181 q->prev = tmp->prev;
182 }
183
184 data = tmp->data;
185 free(tmp);
186
187 return data;
188}
189
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700190static void *dequeue_head(struct queue_entry **q_head)
191{
192 struct queue_entry *q = *q_head;
193 struct queue_entry *tmp = q;
194 void *data;
195
196 if (!q)
197 return NULL;
198
199 if (q->next == q)
200 *q_head = NULL;
201 else {
202 q->next->prev = q->prev;
203 q->prev->next = q->next;
204 *q_head = q->next;
205 }
206
207 data = tmp->data;
208 free(tmp);
209
210 return data;
211}
212
213static void *peek_queue_head(struct queue_entry *q_head)
214{
215 if (!q_head)
216 return NULL;
217
218 return q_head->data;
219}
220
221static struct queue_entry *chip_q_head;
222
223void chip_enqueue_tail(void *data)
224{
225 enqueue_tail(&chip_q_head, data);
226}
227
228void *chip_dequeue_tail(void)
229{
230 return dequeue_tail(&chip_q_head);
231}
232
Martin Rothbec07532016-08-05 18:32:18 -0600233int yywrap(void)
234{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000235 return 1;
236}
237
Martin Rothbec07532016-08-05 18:32:18 -0600238void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000239{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000240 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600241 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000242 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000243}
244
Martin Rothbec07532016-08-05 18:32:18 -0600245char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200246{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200247 char *b, *c;
248 b = c = strdup(str);
249 while (c && *c) {
250 if ((mode == SPLIT_1ST) && (*c == '/')) {
251 *c = 0;
252 break;
253 }
Martin Rothbec07532016-08-05 18:32:18 -0600254 if (*c == '/')
255 *c = '_';
256 if (*c == '-')
257 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200258 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200259 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200260 if (mode == TO_LOWER)
261 *c = tolower(*c);
262 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200263 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200264 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200265}
266
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700267static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600268{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700269 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700270
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700271 while (h->next) {
272 int result = strcmp(path, h->next->name);
273 if (result == 0)
274 return h->next;
275
276 if (result < 0)
277 break;
278
279 h = h->next;
280 }
281
282 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
283 new_chip->next = h->next;
284 h->next = new_chip;
285
Patrick Georgi114e7b22010-05-05 11:19:50 +0000286 new_chip->chiph_exists = 1;
287 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700288 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000289
290 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700291 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700292 sprintf(chip_h, "src/%s", path);
293 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100294 /* root_complex gets away without a separate directory, but
295 * exists on on pretty much all AMD chipsets.
296 */
297 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300298 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
299 path);
300 exit(1);
301 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700302 }
303
Martin Roth824255e2016-08-05 17:40:39 -0600304 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200305
Martin Roth824255e2016-08-05 17:40:39 -0600306 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600307 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000308
Patrick Georgi1f688802014-08-03 15:51:19 +0200309 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700310
Patrick Georgi114e7b22010-05-05 11:19:50 +0000311 return new_chip;
312}
313
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700314struct chip_instance *new_chip_instance(char *path)
315{
316 struct chip *chip = get_chip(path);
317 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
318
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700319 instance->chip = chip;
320 instance->next = chip->instance;
321 chip->instance = instance;
322
323 return instance;
324}
325
Furquan Shaikh93198262018-06-03 04:22:17 -0700326/*
327 * Allocate a new bus for the provided device.
328 * - If this is the first bus being allocated under this device, then its id
329 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
330 * - If this is not the first bus under this device, then its id is set to 1
331 * plus the id of last bus and newly allocated bus is added to the list of
332 * buses under the device. last_bus is updated to point to the newly
333 * allocated bus.
334 */
335static void alloc_bus(struct device *dev)
336{
337 struct bus *bus = S_ALLOC(sizeof(*bus));
338
339 bus->dev = dev;
340
341 if (dev->last_bus == NULL) {
342 bus->id = 0;
343 dev->bus = bus;
344 } else {
345 bus->id = dev->last_bus->id + 1;
346 dev->last_bus->next_bus = bus;
347 }
348
349 dev->last_bus = bus;
350}
351
352/*
353 * Allocate a new device under the given parent. This function allocates a new
354 * device structure under the provided parent bus and allocates a bus structure
355 * under the newly allocated device.
356 */
357static struct device *alloc_dev(struct bus *parent)
358{
359 struct device *dev = S_ALLOC(sizeof(*dev));
360
Furquan Shaikh93198262018-06-03 04:22:17 -0700361 dev->parent = parent;
362 dev->subsystem_vendor = -1;
363 dev->subsystem_device = -1;
364
365 alloc_bus(dev);
366
367 return dev;
368}
369
370/*
371 * This function scans the children of given bus to see if any device matches
372 * the new device that is requested.
373 *
374 * Returns pointer to the node if found, else NULL.
375 */
376static struct device *get_dev(struct bus *parent, int path_a, int path_b,
377 int bustype, struct chip_instance *chip_instance)
378{
379 struct device *child = parent->children;
380
381 while (child) {
382 if ((child->path_a == path_a) && (child->path_b == path_b) &&
383 (child->bustype == bustype) &&
384 (child->chip_instance == chip_instance))
385 return child;
386
387 child = child->sibling;
388 }
389
390 return NULL;
391}
392
Furquan Shaikh27efc502018-06-22 09:19:15 -0700393/*
394 * Add given node as child of the provided parent. If this is the first child of
395 * the parent, update parent->children pointer as well.
396 */
397static void set_new_child(struct bus *parent, struct device *child)
398{
399 struct device *c = parent->children;
400 if (c) {
401 while (c->sibling)
402 c = c->sibling;
403 c->sibling = child;
404 } else
405 parent->children = child;
406
407 child->sibling = NULL;
408 child->parent = parent;
409}
410
Furquan Shaikh93198262018-06-03 04:22:17 -0700411struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700412 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700413 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800414 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600415{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000416 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700417 int path_a;
418 int path_b = 0;
419 struct device *new_d;
420
421 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000422 if (*tmp == '.') {
423 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700424 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000425 }
426
Furquan Shaikh93198262018-06-03 04:22:17 -0700427 /* If device is found under parent, no need to allocate new device. */
428 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
429 if (new_d) {
430 alloc_bus(new_d);
431 return new_d;
432 }
433
434 new_d = alloc_dev(parent);
435
436 new_d->bustype = bustype;
437
438 new_d->path_a = path_a;
439 new_d->path_b = path_b;
440
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800441 new_d->enabled = status & 0x01;
442 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000443 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700444 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000445
Furquan Shaikh27efc502018-06-22 09:19:15 -0700446 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000447
Furquan Shaikha9b64292018-05-31 07:52:00 -0700448 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200449 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000450 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200451 break;
452
453 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000454 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200455 break;
456
457 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700458 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200459 break;
460
461 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000462 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200463 break;
464
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800465 case CPU_CLUSTER:
466 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200467 break;
468
Aaron Durbinffda804b2014-09-03 12:40:15 -0500469 case CPU:
470 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
471 break;
472
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800473 case DOMAIN:
474 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200475 break;
476
477 case IOAPIC:
478 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
479 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700480
481 case GENERIC:
482 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
483 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800484
485 case SPI:
486 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
487 break;
488
Duncan Lauriebae9f852018-05-07 14:18:13 -0700489 case USB:
490 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
491 break;
492
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800493 case MMIO:
494 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
495 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600496
497 case ESPI:
498 new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}";
499 break;
500
501 case LPC:
502 new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}";
503 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000504 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800505
Patrick Georgi114e7b22010-05-05 11:19:50 +0000506 return new_d;
507}
508
Furquan Shaikh27efc502018-06-22 09:19:15 -0700509static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600510{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700511 struct resource *r = S_ALLOC(sizeof(struct resource));
512
Patrick Georgi114e7b22010-05-05 11:19:50 +0000513 r->type = type;
514 r->index = index;
515 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000516 if (dev->res) {
517 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600518 while (head->next)
519 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000520 head->next = r;
521 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000522 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000523 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000524}
525
Furquan Shaikh27efc502018-06-22 09:19:15 -0700526void add_resource(struct bus *bus, int type, int index, int base)
527{
528 new_resource(bus->dev, type, index, base);
529}
530
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700531void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600532{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700533 struct reg *r = S_ALLOC(sizeof(struct reg));
534
Patrick Georgi114e7b22010-05-05 11:19:50 +0000535 r->key = name;
536 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700537 if (chip_instance->reg) {
538 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000539 // sorting to be equal to sconfig's behaviour
540 int sort = strcmp(r->key, head->key);
541 if (sort == 0) {
542 printf("ERROR: duplicate 'register' key.\n");
543 exit(1);
544 }
Martin Rothbec07532016-08-05 18:32:18 -0600545 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000546 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700547 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000548 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600549 while ((head->next)
550 && (strcmp(head->next->key, r->key) < 0))
551 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000552 r->next = head->next;
553 head->next = r;
554 }
555 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700556 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000557 }
558}
559
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200560void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
561 char *data_width)
562{
563 struct device *dev = bus->dev;
564
565 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
566 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
567 exit(1);
568 }
569
570 dev->smbios_slot_type = type;
571 dev->smbios_slot_length = length;
572 dev->smbios_slot_data_width = data_width;
573 dev->smbios_slot_designation = designation;
574}
575
Furquan Shaikh93198262018-06-03 04:22:17 -0700576void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600577 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000578{
Furquan Shaikh93198262018-06-03 04:22:17 -0700579 struct device *dev = bus->dev;
580
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800581 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000582 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
583 exit(1);
584 }
585
586 dev->subsystem_vendor = vendor;
587 dev->subsystem_device = device;
588 dev->inherit_subsystem = inherit;
589}
590
Furquan Shaikh93198262018-06-03 04:22:17 -0700591void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600592 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200593{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200594 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700595 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200596
Martin Rothbec07532016-08-05 18:32:18 -0600597 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
598 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200599 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
600 exit(1);
601 }
602
603 srcpin = _srcpin[3] - 'A';
604
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800605 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200606 printf("ERROR: ioapic config only allowed for PCI devices\n");
607 exit(1);
608 }
609
610 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200611 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200612 exit(1);
613 }
614 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
615 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
616}
617
Furquan Shaikh93198262018-06-03 04:22:17 -0700618static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600619{
Furquan Shaikh93198262018-06-03 04:22:17 -0700620 struct bus *bus = dev->bus;
621
622 while (bus) {
623 if (bus->children)
624 return 1;
625 bus = bus->next_bus;
626 }
627
628 return 0;
629}
630
Nico Huber17e9bcb2019-09-20 12:05:51 +0200631static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700632{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700633 static int dev_id;
634
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700635 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200636 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700637 ptr->name);
638 return;
639 }
640
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700641 char *name = S_ALLOC(10);
642 sprintf(name, "_dev%d", dev_id++);
643 ptr->name = name;
644
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200645 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700646 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200647 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700648 ptr->name);
649 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200650 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600651 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700652
Furquan Shaikh93198262018-06-03 04:22:17 -0700653 if (next)
654 return;
655
656 fprintf(fil,
657 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
658 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000659}
660
Furquan Shaikh93198262018-06-03 04:22:17 -0700661static void emit_resources(FILE *fil, struct device *ptr)
662{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700663 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700664 return;
665
666 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200667 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700668 struct resource *r = ptr->res;
669 while (r) {
670 fprintf(fil,
671 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
672 if (r->type == IRQ)
673 fprintf(fil, "IRQ");
674 if (r->type == DRQ)
675 fprintf(fil, "DRQ");
676 if (r->type == IO)
677 fprintf(fil, "IO");
678 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
679 r->base);
680 if (r->next)
681 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
682 i++);
683 else
684 fprintf(fil, ".next=NULL },\n");
685 r = r->next;
686 }
687
688 fprintf(fil, "\t };\n");
689}
690
691static void emit_bus(FILE *fil, struct bus *bus)
692{
693 fprintf(fil, "\t\t[%d] = {\n", bus->id);
694 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
695 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
696 if (bus->children)
697 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
698
699 if (bus->next_bus)
700 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
701 bus->id + 1);
702 else
703 fprintf(fil, "\t\t\t.next = NULL,\n");
704 fprintf(fil, "\t\t},\n");
705}
706
707static void emit_dev_links(FILE *fil, struct device *ptr)
708{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200709 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700710 ptr->name);
711
712 struct bus *bus = ptr->bus;
713
714 while (bus) {
715 emit_bus(fil, bus);
716 bus = bus->next_bus;
717 }
718
719 fprintf(fil, "\t};\n");
720}
721
Nico Huber17e9bcb2019-09-20 12:05:51 +0200722static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200723{
724 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700725 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700726 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700727
Furquan Shaikhbbade242020-05-02 16:05:29 -0700728 /*
729 * If the chip instance of device has base_chip_instance pointer set, then follow that
730 * to update the chip instance for current device.
731 */
732 if (chip_ins->base_chip_instance)
733 chip_ins = chip_ins->base_chip_instance;
734
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200735 if (ptr == &base_root_dev)
736 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
737 else
738 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
739
Furquan Shaikh93198262018-06-03 04:22:17 -0700740 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -0700741
742 /*
743 * ops field is set to default_dev_ops_root only for the root
744 * device. For all other devices, it is set by the driver at runtime.
745 */
746 if (ptr == &base_root_dev)
747 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
748 else
749 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700750 fprintf(fil, "#endif\n");
751 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
752 ptr->parent->id);
753 fprintf(fil, "\t.path = {");
754 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
755 fprintf(fil, "},\n");
756 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800757 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000758 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -0700759 fprintf(fil, "\t.on_mainboard = 1,\n");
760 if (ptr->subsystem_vendor > 0)
761 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
762 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000763
Furquan Shaikh93198262018-06-03 04:22:17 -0700764 if (ptr->subsystem_device > 0)
765 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
766 ptr->subsystem_device);
767
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700768 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700769 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600770 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700771 }
772 if (has_children)
773 fprintf(fil, "\t.link_list = &%s_links[0],\n",
774 ptr->name);
775 else
776 fprintf(fil, "\t.link_list = NULL,\n");
777 if (ptr->sibling)
778 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -0600779 else
780 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700781 fprintf(fil, "#if !DEVTREE_EARLY\n");
Kyösti Mälkki1557a672019-06-30 10:51:31 +0300782 for (pin = 0; pin < 4; pin++) {
783 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
784 fprintf(fil,
785 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
786 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
787
788 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
789 fprintf(fil,
790 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
791 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
792 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700793 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
794 chip_ins->chip->name_underscore);
795 if (chip_ins == &mainboard_instance)
796 fprintf(fil, "\t.name = mainboard_name,\n");
797 fprintf(fil, "#endif\n");
798 if (chip_ins->chip->chiph_exists)
799 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
800 chip_ins->chip->name_underscore, chip_ins->id);
801 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200802 fprintf(fil, "\t.next=&%s,\n", next->name);
803 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
804 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
805 fprintf(fil, "#if !DEVTREE_EARLY\n");
806 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
807 }
808 /* SMBIOS types start at 1, if zero it hasn't been set */
809 if (ptr->smbios_slot_type)
810 fprintf(fil, "\t.smbios_slot_type = %s,\n",
811 ptr->smbios_slot_type);
812 if (ptr->smbios_slot_data_width)
813 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
814 ptr->smbios_slot_data_width);
815 if (ptr->smbios_slot_designation)
816 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
817 ptr->smbios_slot_designation);
818 if (ptr->smbios_slot_length)
819 fprintf(fil, "\t.smbios_slot_length = %s,\n",
820 ptr->smbios_slot_length);
821 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
822 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
823 fprintf(fil, "#endif\n");
824 fprintf(fil, "#endif\n");
825 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700826 fprintf(fil, "};\n");
827
828 emit_resources(fil, ptr);
829
830 if (has_children)
831 emit_dev_links(fil, ptr);
832}
833
Nico Huber17e9bcb2019-09-20 12:05:51 +0200834static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200835{
836 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +0200837 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
838 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
839 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200840 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
841 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200842 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200843
Nico Huber17e9bcb2019-09-20 12:05:51 +0200844 if (ptr->bustype == PNP) {
845 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
846 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200847 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
848 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200849 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200850}
851
Furquan Shaikh93198262018-06-03 04:22:17 -0700852static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
853 struct device *d)
854{
855 while (d) {
856 enqueue_tail(bfs_q_head, d);
857 d = d->sibling;
858 }
859}
860
861static void add_children_to_queue(struct queue_entry **bfs_q_head,
862 struct device *d)
863{
864 struct bus *bus = d->bus;
865
866 while (bus) {
867 if (bus->children)
868 add_siblings_to_queue(bfs_q_head, bus->children);
869 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000870 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000871}
872
Nico Huber17e9bcb2019-09-20 12:05:51 +0200873static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
874 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -0700875 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600876{
Furquan Shaikh93198262018-06-03 04:22:17 -0700877 struct queue_entry *bfs_q_head = NULL;
878
879 enqueue_tail(&bfs_q_head, ptr);
880
881 while ((ptr = dequeue_head(&bfs_q_head))) {
882 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200883 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -0700884 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000885}
886
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700887static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700888{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700889 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700890
891 fprintf(fil, "#include <device/device.h>\n");
892 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700893
894 while (chip) {
895 if (chip->chiph_exists)
896 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
897 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700898 }
899 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
900 fprintf(fil,
901 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700902
903 chip = tmp;
904 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700905 fprintf(fil,
906 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700907 chip->name_underscore);
908 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700909 }
910 fprintf(fil, "#endif\n");
911}
912
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700913static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
914{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200915 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700916 instance->chip->name_underscore,
917 instance->chip->name_underscore,
918 instance->id);
919
920 if (instance->reg) {
921 fprintf(fil, "\n");
922 struct reg *r = instance->reg;
923 while (r) {
924 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
925 r = r->next;
926 }
927 }
928 fprintf(fil, "};\n\n");
929}
930
Furquan Shaikh79e84122018-05-30 15:09:09 -0700931static void emit_chips(FILE *fil)
932{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700933 struct chip *chip = chip_header.next;
934 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700935 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700936
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700937 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700938
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200939 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
940
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700941 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700942 if (!chip->chiph_exists)
943 continue;
944
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700945 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700946 instance = chip->instance;
947 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -0700948 /*
949 * Emit this chip instance only if there is no forwarding pointer to the
950 * base tree chip instance.
951 */
952 if (instance->base_chip_instance == NULL) {
953 instance->id = chip_id++;
954 emit_chip_instance(fil, instance);
955 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700956 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700957 }
958 }
959}
960
Nico Huber17e9bcb2019-09-20 12:05:51 +0200961static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -0700962 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +0000963{
964 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000965
966 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
967 /* user already gave us a subsystem vendor/device */
968 return;
969 }
970
Furquan Shaikh93198262018-06-03 04:22:17 -0700971 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000972
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800973 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000974 continue;
975
976 if (p->inherit_subsystem) {
977 dev->subsystem_vendor = p->subsystem_vendor;
978 dev->subsystem_device = p->subsystem_device;
979 break;
980 }
981 }
982}
983
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200984static void usage(void)
985{
Nico Huber17e9bcb2019-09-20 12:05:51 +0200986 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -0600987 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200988}
989
Martin Roth32051702015-11-24 12:34:16 -0700990enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -0600991 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700992 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +0200993 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700994 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -0600995};
Martin Roth32051702015-11-24 12:34:16 -0700996
Nico Huber17e9bcb2019-09-20 12:05:51 +0200997#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700998#define OPTIONAL_ARG_COUNT 1
999#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001000
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001001static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001002{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001003 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001004 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001005 perror(NULL);
1006 exit(1);
1007 }
1008
Patrick Georgi114e7b22010-05-05 11:19:50 +00001009 yyrestart(filec);
1010
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001011 root_parent = parent;
1012 linenum = 0;
1013
Patrick Georgi114e7b22010-05-05 11:19:50 +00001014 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001015
Patrick Georgi114e7b22010-05-05 11:19:50 +00001016 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001017}
1018
Furquan Shaikh27efc502018-06-22 09:19:15 -07001019/*
1020 * Match device nodes from base and override tree to see if they are the same
1021 * node.
1022 */
1023static int device_match(struct device *a, struct device *b)
1024{
1025 return ((a->path_a == b->path_a) &&
1026 (a->path_b == b->path_b) &&
1027 (a->bustype == b->bustype) &&
1028 (a->chip_instance->chip ==
1029 b->chip_instance->chip));
1030}
1031
1032/*
Bill XIEc61d4152019-11-21 18:16:12 +08001033 * Match resource nodes from base and override tree to see if they are the same
1034 * node.
1035 */
1036static int res_match(struct resource *a, struct resource *b)
1037{
1038 return ((a->type == b->type) &&
1039 (a->index == b->index));
1040}
1041
1042/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001043 * Add resource to device. If resource is already present, then update its base
1044 * and index. If not, then add a new resource to the device.
1045 */
1046static void update_resource(struct device *dev, struct resource *res)
1047{
1048 struct resource *base_res = dev->res;
1049
1050 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001051 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001052 base_res->base = res->base;
1053 return;
1054 }
1055 base_res = base_res->next;
1056 }
1057
1058 new_resource(dev, res->type, res->index, res->base);
1059}
1060
1061/*
1062 * Add register to chip instance. If register is already present, then update
1063 * its value. If not, then add a new register to the chip instance.
1064 */
1065static void update_register(struct chip_instance *c, struct reg *reg)
1066{
1067 struct reg *base_reg = c->reg;
1068
1069 while (base_reg) {
1070 if (!strcmp(base_reg->key, reg->key)) {
1071 base_reg->value = reg->value;
1072 return;
1073 }
1074 base_reg = base_reg->next;
1075 }
1076
1077 add_register(c, reg->key, reg->value);
1078}
1079
1080static void override_devicetree(struct bus *base_parent,
1081 struct bus *override_parent);
1082
1083/*
1084 * Update the base device properties using the properties of override device. In
1085 * addition to that, call override_devicetree for all the buses under the
1086 * override device.
1087 *
1088 * Override Rules:
1089 * +--------------------+--------------------------------------------+
1090 * | | |
1091 * |struct device member| Rule |
1092 * | | |
1093 * +-----------------------------------------------------------------+
1094 * | | |
1095 * | id | Unchanged. This is used to generate device |
1096 * | | structure name in static.c. So, no need to |
1097 * | | override. |
1098 * | | |
1099 * +-----------------------------------------------------------------+
1100 * | | |
1101 * | enabled | Copy enabled state from override device. |
1102 * | | This allows variants to override device |
1103 * | | state. |
1104 * | | |
1105 * +-----------------------------------------------------------------+
1106 * | | |
1107 * | subsystem_vendor | Copy from override device only if any one |
1108 * | subsystem_device | of the ids is non-zero. |
1109 * | | |
1110 * +-----------------------------------------------------------------+
1111 * | | |
1112 * | inherit_subsystem | Copy from override device only if it is |
1113 * | | non-zero. This allows variant to only |
1114 * | | enable inherit flag for a device. |
1115 * | | |
1116 * +-----------------------------------------------------------------+
1117 * | | |
1118 * | path | Unchanged since these are same for both |
1119 * | path_a | base and override device (Used for |
1120 * | path_b | matching devices). |
1121 * | | |
1122 * +-----------------------------------------------------------------+
1123 * | | |
1124 * | bustype | Unchanged since this is same for both base |
1125 * | | and override device (User for matching |
1126 * | | devices). |
1127 * | | |
1128 * +-----------------------------------------------------------------+
1129 * | | |
1130 * | pci_irq_info | Unchanged. |
1131 * | | |
1132 * +-----------------------------------------------------------------+
1133 * | | |
1134 * | parent | Unchanged. This is meaningful only within |
1135 * | sibling | the parse tree, hence not being copied. |
1136 * | | |
1137 * +-----------------------------------------------------------------+
1138 * | | |
1139 * | res | Each resource that is present in override |
1140 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001141 * | | 1. If resource of same type and index is |
1142 * | | present in base device, then base of |
1143 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001144 * | | 2. If not, then a new resource is allocated|
1145 * | | under the base device using type, index |
1146 * | | and base from override res. |
1147 * | | |
1148 * +-----------------------------------------------------------------+
1149 * | | |
1150 * | chip_instance | Each register of chip_instance is copied |
1151 * | | over from override device to base device: |
1152 * | | 1. If register with same key is present in |
1153 * | | base device, then value of the register |
1154 * | | is copied. |
1155 * | | 2. If not, then a new register is allocated|
1156 * | | under the base chip_instance using key |
1157 * | | and value from override register. |
1158 * | | |
1159 * +-----------------------------------------------------------------+
1160 * | | |
1161 * | bus | Recursively call override_devicetree on |
1162 * | last_bus | each bus of override device. It is assumed |
1163 * | | that bus with id X under base device |
1164 * | | to bus with id X under override device. If |
1165 * | | override device has more buses than base |
1166 * | | device, then new buses are allocated under |
1167 * | | base device. |
1168 * | | |
1169 * +-----------------------------------------------------------------+
1170 */
1171static void update_device(struct device *base_dev, struct device *override_dev)
1172{
1173 /*
1174 * Copy the enabled state of override device to base device. This allows
1175 * override tree to enable or disable a particular device.
1176 */
1177 base_dev->enabled = override_dev->enabled;
1178
1179 /*
1180 * Copy subsystem vendor and device ids from override device to base
1181 * device only if the ids are non-zero in override device. Else, honor
1182 * the values in base device.
1183 */
1184 if (override_dev->subsystem_vendor ||
1185 override_dev->subsystem_device) {
1186 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1187 base_dev->subsystem_device = override_dev->subsystem_device;
1188 }
1189
1190 /*
1191 * Copy value of inherity_subsystem from override device to base device
1192 * only if it is non-zero in override device. This allows override
1193 * tree to only enable inhert flag for a device.
1194 */
1195 if (override_dev->inherit_subsystem)
1196 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1197
1198 /*
1199 * Copy resources of override device to base device.
1200 * 1. If resource is already present in base device, then index and base
1201 * of the resource will be copied over.
1202 * 2. If resource is not already present in base device, a new resource
1203 * will be allocated.
1204 */
1205 struct resource *res = override_dev->res;
1206 while (res) {
1207 update_resource(base_dev, res);
1208 res = res->next;
1209 }
1210
1211 /*
1212 * Copy registers of override chip instance to base chip instance.
1213 * 1. If register key is already present in base chip instance, then
1214 * value for the register is copied over.
1215 * 2. If register key is not already present in base chip instance, then
1216 * a new register will be allocated.
1217 */
1218 struct reg *reg = override_dev->chip_instance->reg;
1219 while (reg) {
1220 update_register(base_dev->chip_instance, reg);
1221 reg = reg->next;
1222 }
1223
1224 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001225 * Update base_chip_instance member in chip instance of override tree to forward it to
1226 * the chip instance in base tree.
1227 */
1228 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1229
1230 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001231 * Now that the device properties are all copied over, look at each bus
1232 * of the override device and run override_devicetree in a recursive
1233 * manner. The assumption here is that first bus of override device
1234 * corresponds to first bus of base device and so on. If base device has
1235 * lesser buses than override tree, then new buses are allocated for it.
1236 */
1237 struct bus *override_bus = override_dev->bus;
1238 struct bus *base_bus = base_dev->bus;
1239
1240 while (override_bus) {
1241
1242 /*
1243 * If we have more buses in override tree device, then allocate
1244 * a new bus for the base tree device as well.
1245 */
1246 if (!base_bus) {
1247 alloc_bus(base_dev);
1248 base_bus = base_dev->last_bus;
1249 }
1250
1251 override_devicetree(base_dev->bus, override_dev->bus);
1252
1253 override_bus = override_bus->next_bus;
1254 base_bus = base_bus->next_bus;
1255 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001256}
1257
1258/*
1259 * Perform copy of device and properties from override parent to base parent.
1260 * This function walks through the override tree in a depth-first manner
1261 * performing following actions:
1262 * 1. If matching device is found in base tree, then copy the properties of
1263 * override device to base tree device. Call override_devicetree recursively on
1264 * the bus of override device.
1265 * 2. If matching device is not found in base tree, then set override tree
1266 * device as new child of base_parent and update the chip pointers in override
1267 * device subtree to ensure the nodes do not point to override tree chip
1268 * instance.
1269 */
1270static void override_devicetree(struct bus *base_parent,
1271 struct bus *override_parent)
1272{
1273 struct device *base_child;
1274 struct device *override_child = override_parent->children;
1275 struct device *next_child;
1276
1277 while (override_child) {
1278
1279 /* Look for a matching device in base tree. */
1280 for (base_child = base_parent->children;
1281 base_child; base_child = base_child->sibling) {
1282 if (device_match(base_child, override_child))
1283 break;
1284 }
1285
1286 next_child = override_child->sibling;
1287
1288 /*
1289 * If matching device is found, copy properties of
1290 * override_child to base_child.
1291 */
1292 if (base_child)
1293 update_device(base_child, override_child);
1294 else {
1295 /*
1296 * If matching device is not found, set override_child
1297 * as a new child of base_parent.
1298 */
1299 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001300 }
1301
1302 override_child = next_child;
1303 }
1304}
1305
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001306int main(int argc, char **argv)
1307{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001308 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001309 usage();
1310
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001311 const char *base_devtree = argv[DEVICEFILE_ARG];
1312 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001313 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001314 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001315
1316 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001317
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001318 if (argc == TOTAL_ARG_COUNT) {
1319 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1320 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001321
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001322 if (!dev_has_children(&override_root_dev)) {
1323 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1324 exit(1);
1325 }
1326
Furquan Shaikh27efc502018-06-22 09:19:15 -07001327 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001328 }
1329
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001330 FILE *autogen = fopen(outputc, "w");
1331 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001332 fprintf(stderr, "Could not open file '%s' for writing: ",
1333 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001334 perror(NULL);
1335 exit(1);
1336 }
1337
Nico Huber17e9bcb2019-09-20 12:05:51 +02001338 FILE *autohead = fopen(outputh, "w");
1339 if (!autohead) {
1340 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1341 perror(NULL);
1342 fclose(autogen);
1343 exit(1);
1344 }
1345 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1346 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1347 fprintf(autohead, "#include <device/device.h>\n\n");
1348
Furquan Shaikh79e84122018-05-30 15:09:09 -07001349 emit_chips(autogen);
1350
Nico Huber17e9bcb2019-09-20 12:05:51 +02001351 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001352 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001353 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001354 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001355 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001356
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001357 /* Expose static devicenames to global namespace. */
1358 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001359 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001360
Nico Huber17e9bcb2019-09-20 12:05:51 +02001361 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1362 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001363 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001364
Patrick Georgi114e7b22010-05-05 11:19:50 +00001365 return 0;
1366}