blob: 186eedba433e5a9c9d38c78885a41766ba52d033 [file] [log] [blame]
Patrick Georgi114e7b22010-05-05 11:19:50 +00001/*
2 * sconfig, coreboot device tree compiler
3 *
4 * Copyright (C) 2010 coresystems GmbH
Stefan Reinauer2e78aa52016-05-07 01:11:14 -07005 * written by Patrick Georgi <patrick@georgi-clan.de>
Patrick Georgi114e7b22010-05-05 11:19:50 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Patrick Georgi114e7b22010-05-05 11:19:50 +000015 */
16
Patrick Georgi2dbfcb72012-05-30 16:26:30 +020017#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +020018/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
19#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +020020#include <commonlib/helpers.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000021#include "sconfig.h"
22#include "sconfig.tab.h"
23
Patrick Georgi7fc9e292010-07-15 15:59:07 +000024extern int linenum;
25
Furquan Shaikh27efc502018-06-22 09:19:15 -070026/*
27 * Maintains list of all the unique chip structures for the board.
28 * This is shared across base and override device trees since we need to
29 * generate headers for all chips added by both the trees.
30 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070031static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070032
Kyösti Mälkki472d9022011-12-05 20:33:55 +020033typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020034 UNSLASH,
35 SPLIT_1ST,
36 TO_LOWER,
37 TO_UPPER,
38} translate_t;
39
Furquan Shaikh93198262018-06-03 04:22:17 -070040/*
41 * Mainboard is assumed to have a root device whose bus is the parent of all the
42 * devices that are added by parsing the devicetree file. This device has a
43 * mainboard chip instance associated with it.
44 *
45 *
46 *
47 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070048 * | Root device | | Mainboard |
49 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070050 * | | | chip_instance | (mainboard_instance)|
51 * | +------------------------+ | |
52 * | | +----------------------+
53 * | | bus |
54 * | parent v |
55 * | +-------------------+ |
56 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070057 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070058 * | | |
59 * +-------------------+ |
60 * | |
61 * | children | chip
62 * v |
63 * X |
64 * (new devices will |
65 * be added here as |
66 * children) |
67 * |
68 * |
69 * |
70 * +-------+----------+
71 * | |
72 * | Mainboard chip +----------->X (new chips will be
73 * | (mainboard_chip) | added here)
74 * | |
75 * +------------------+
76 *
77 *
78 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070079
80/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070081static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070082
83/* Root device of override tree (if applicable). */
84static struct device override_root_dev;
85
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070086static struct chip_instance mainboard_instance;
87
Furquan Shaikhde39fc72018-06-11 04:26:45 -070088static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070089 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070090 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070091};
92
Furquan Shaikhde39fc72018-06-11 04:26:45 -070093static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070094 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070095 .chip_instance = &mainboard_instance,
96 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070097 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070098 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070099 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -0700100};
101
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700102static struct bus override_root_bus = {
103 .id = 0,
104 .dev = &override_root_dev,
105};
106
107static struct device override_root_dev = {
108 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700109 /*
110 * Override tree root device points to the same mainboard chip instance
111 * as the base tree root device. It should not cause any side-effects
112 * since the mainboard chip instance pointer in override tree will just
113 * be ignored.
114 */
115 .chip_instance = &mainboard_instance,
116 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700117 .parent = &override_root_bus,
118 .enabled = 1,
119 .bus = &override_root_bus,
120};
121
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700122static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000123 .name = "mainboard",
124 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700125 .instance = &mainboard_instance,
126};
127
128static struct chip_instance mainboard_instance = {
129 .id = 0,
130 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000131};
132
Furquan Shaikh93198262018-06-03 04:22:17 -0700133/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700134struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000135
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700136struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700137 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700138 struct queue_entry *next;
139 struct queue_entry *prev;
140};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700141
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700142#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700143
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700144static void *s_alloc(const char *f, size_t s)
145{
146 void *data = calloc(1, s);
147 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530148 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700149 exit(1);
150 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700151 return data;
152}
153
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700154static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700155{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700156 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700157
158 e->data = data;
159 e->next = e->prev = e;
160 return e;
161}
162
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700163static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700164{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700165 struct queue_entry *tmp = new_queue_entry(data);
166 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700167
168 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700169 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700170 return;
171 }
172
173 q->prev->next = tmp;
174 tmp->prev = q->prev;
175 q->prev = tmp;
176 tmp->next = q;
177}
178
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700179static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700180{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700181 struct queue_entry *q = *q_head;
182 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700183 void *data;
184
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700185 if (!q)
186 return NULL;
187
188 tmp = q->prev;
189
Furquan Shaikh79e84122018-05-30 15:09:09 -0700190 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700191 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700192 else {
193 tmp->prev->next = q;
194 q->prev = tmp->prev;
195 }
196
197 data = tmp->data;
198 free(tmp);
199
200 return data;
201}
202
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700203static void *dequeue_head(struct queue_entry **q_head)
204{
205 struct queue_entry *q = *q_head;
206 struct queue_entry *tmp = q;
207 void *data;
208
209 if (!q)
210 return NULL;
211
212 if (q->next == q)
213 *q_head = NULL;
214 else {
215 q->next->prev = q->prev;
216 q->prev->next = q->next;
217 *q_head = q->next;
218 }
219
220 data = tmp->data;
221 free(tmp);
222
223 return data;
224}
225
226static void *peek_queue_head(struct queue_entry *q_head)
227{
228 if (!q_head)
229 return NULL;
230
231 return q_head->data;
232}
233
234static struct queue_entry *chip_q_head;
235
236void chip_enqueue_tail(void *data)
237{
238 enqueue_tail(&chip_q_head, data);
239}
240
241void *chip_dequeue_tail(void)
242{
243 return dequeue_tail(&chip_q_head);
244}
245
Martin Rothbec07532016-08-05 18:32:18 -0600246int yywrap(void)
247{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000248 return 1;
249}
250
Martin Rothbec07532016-08-05 18:32:18 -0600251void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000252{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000253 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600254 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000255 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000256}
257
Martin Rothbec07532016-08-05 18:32:18 -0600258char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200259{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200260 char *b, *c;
261 b = c = strdup(str);
262 while (c && *c) {
263 if ((mode == SPLIT_1ST) && (*c == '/')) {
264 *c = 0;
265 break;
266 }
Martin Rothbec07532016-08-05 18:32:18 -0600267 if (*c == '/')
268 *c = '_';
269 if (*c == '-')
270 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200271 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200272 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200273 if (mode == TO_LOWER)
274 *c = tolower(*c);
275 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200276 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200277 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200278}
279
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700280static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600281{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700282 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700283
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700284 while (h->next) {
285 int result = strcmp(path, h->next->name);
286 if (result == 0)
287 return h->next;
288
289 if (result < 0)
290 break;
291
292 h = h->next;
293 }
294
295 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
296 new_chip->next = h->next;
297 h->next = new_chip;
298
Patrick Georgi114e7b22010-05-05 11:19:50 +0000299 new_chip->chiph_exists = 1;
300 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700301 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000302
303 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700304 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700305 sprintf(chip_h, "src/%s", path);
306 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100307 /* root_complex gets away without a separate directory, but
308 * exists on on pretty much all AMD chipsets.
309 */
310 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300311 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
312 path);
313 exit(1);
314 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700315 }
316
Martin Roth824255e2016-08-05 17:40:39 -0600317 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200318
Martin Roth824255e2016-08-05 17:40:39 -0600319 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600320 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000321
Patrick Georgi1f688802014-08-03 15:51:19 +0200322 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700323
Patrick Georgi114e7b22010-05-05 11:19:50 +0000324 return new_chip;
325}
326
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700327struct chip_instance *new_chip_instance(char *path)
328{
329 struct chip *chip = get_chip(path);
330 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
331
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700332 instance->chip = chip;
333 instance->next = chip->instance;
334 chip->instance = instance;
335
336 return instance;
337}
338
Furquan Shaikh93198262018-06-03 04:22:17 -0700339/*
340 * Allocate a new bus for the provided device.
341 * - If this is the first bus being allocated under this device, then its id
342 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
343 * - If this is not the first bus under this device, then its id is set to 1
344 * plus the id of last bus and newly allocated bus is added to the list of
345 * buses under the device. last_bus is updated to point to the newly
346 * allocated bus.
347 */
348static void alloc_bus(struct device *dev)
349{
350 struct bus *bus = S_ALLOC(sizeof(*bus));
351
352 bus->dev = dev;
353
354 if (dev->last_bus == NULL) {
355 bus->id = 0;
356 dev->bus = bus;
357 } else {
358 bus->id = dev->last_bus->id + 1;
359 dev->last_bus->next_bus = bus;
360 }
361
362 dev->last_bus = bus;
363}
364
365/*
366 * Allocate a new device under the given parent. This function allocates a new
367 * device structure under the provided parent bus and allocates a bus structure
368 * under the newly allocated device.
369 */
370static struct device *alloc_dev(struct bus *parent)
371{
372 struct device *dev = S_ALLOC(sizeof(*dev));
373
Furquan Shaikh93198262018-06-03 04:22:17 -0700374 dev->parent = parent;
375 dev->subsystem_vendor = -1;
376 dev->subsystem_device = -1;
377
378 alloc_bus(dev);
379
380 return dev;
381}
382
383/*
384 * This function scans the children of given bus to see if any device matches
385 * the new device that is requested.
386 *
387 * Returns pointer to the node if found, else NULL.
388 */
389static struct device *get_dev(struct bus *parent, int path_a, int path_b,
390 int bustype, struct chip_instance *chip_instance)
391{
392 struct device *child = parent->children;
393
394 while (child) {
395 if ((child->path_a == path_a) && (child->path_b == path_b) &&
396 (child->bustype == bustype) &&
397 (child->chip_instance == chip_instance))
398 return child;
399
400 child = child->sibling;
401 }
402
403 return NULL;
404}
405
Furquan Shaikh27efc502018-06-22 09:19:15 -0700406/*
407 * Add given node as child of the provided parent. If this is the first child of
408 * the parent, update parent->children pointer as well.
409 */
410static void set_new_child(struct bus *parent, struct device *child)
411{
412 struct device *c = parent->children;
413 if (c) {
414 while (c->sibling)
415 c = c->sibling;
416 c->sibling = child;
417 } else
418 parent->children = child;
419
420 child->sibling = NULL;
421 child->parent = parent;
422}
423
Furquan Shaikh93198262018-06-03 04:22:17 -0700424struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700425 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700426 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800427 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600428{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000429 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700430 int path_a;
431 int path_b = 0;
432 struct device *new_d;
433
434 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000435 if (*tmp == '.') {
436 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700437 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000438 }
439
Furquan Shaikh93198262018-06-03 04:22:17 -0700440 /* If device is found under parent, no need to allocate new device. */
441 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
442 if (new_d) {
443 alloc_bus(new_d);
444 return new_d;
445 }
446
447 new_d = alloc_dev(parent);
448
449 new_d->bustype = bustype;
450
451 new_d->path_a = path_a;
452 new_d->path_b = path_b;
453
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800454 new_d->enabled = status & 0x01;
455 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000456 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700457 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000458
Furquan Shaikh27efc502018-06-22 09:19:15 -0700459 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000460
Furquan Shaikha9b64292018-05-31 07:52:00 -0700461 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200462 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000463 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200464 break;
465
466 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000467 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200468 break;
469
470 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700471 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200472 break;
473
474 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000475 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200476 break;
477
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800478 case CPU_CLUSTER:
479 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200480 break;
481
Aaron Durbinffda804b2014-09-03 12:40:15 -0500482 case CPU:
483 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
484 break;
485
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800486 case DOMAIN:
487 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200488 break;
489
490 case IOAPIC:
491 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
492 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700493
494 case GENERIC:
495 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
496 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800497
498 case SPI:
499 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
500 break;
501
Duncan Lauriebae9f852018-05-07 14:18:13 -0700502 case USB:
503 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
504 break;
505
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800506 case MMIO:
507 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
508 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000509 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800510
Patrick Georgi114e7b22010-05-05 11:19:50 +0000511 return new_d;
512}
513
Furquan Shaikh27efc502018-06-22 09:19:15 -0700514static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600515{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700516 struct resource *r = S_ALLOC(sizeof(struct resource));
517
Patrick Georgi114e7b22010-05-05 11:19:50 +0000518 r->type = type;
519 r->index = index;
520 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000521 if (dev->res) {
522 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600523 while (head->next)
524 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000525 head->next = r;
526 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000527 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000528 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000529}
530
Furquan Shaikh27efc502018-06-22 09:19:15 -0700531void add_resource(struct bus *bus, int type, int index, int base)
532{
533 new_resource(bus->dev, type, index, base);
534}
535
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700536void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600537{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700538 struct reg *r = S_ALLOC(sizeof(struct reg));
539
Patrick Georgi114e7b22010-05-05 11:19:50 +0000540 r->key = name;
541 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700542 if (chip_instance->reg) {
543 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000544 // sorting to be equal to sconfig's behaviour
545 int sort = strcmp(r->key, head->key);
546 if (sort == 0) {
547 printf("ERROR: duplicate 'register' key.\n");
548 exit(1);
549 }
Martin Rothbec07532016-08-05 18:32:18 -0600550 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000551 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700552 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000553 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600554 while ((head->next)
555 && (strcmp(head->next->key, r->key) < 0))
556 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000557 r->next = head->next;
558 head->next = r;
559 }
560 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700561 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000562 }
563}
564
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200565void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
566 char *data_width)
567{
568 struct device *dev = bus->dev;
569
570 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
571 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
572 exit(1);
573 }
574
575 dev->smbios_slot_type = type;
576 dev->smbios_slot_length = length;
577 dev->smbios_slot_data_width = data_width;
578 dev->smbios_slot_designation = designation;
579}
580
Furquan Shaikh93198262018-06-03 04:22:17 -0700581void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600582 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000583{
Furquan Shaikh93198262018-06-03 04:22:17 -0700584 struct device *dev = bus->dev;
585
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800586 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000587 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
588 exit(1);
589 }
590
591 dev->subsystem_vendor = vendor;
592 dev->subsystem_device = device;
593 dev->inherit_subsystem = inherit;
594}
595
Furquan Shaikh93198262018-06-03 04:22:17 -0700596void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600597 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200598{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200599 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700600 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200601
Martin Rothbec07532016-08-05 18:32:18 -0600602 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
603 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200604 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
605 exit(1);
606 }
607
608 srcpin = _srcpin[3] - 'A';
609
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800610 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200611 printf("ERROR: ioapic config only allowed for PCI devices\n");
612 exit(1);
613 }
614
615 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200616 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200617 exit(1);
618 }
619 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
620 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
621}
622
Furquan Shaikh93198262018-06-03 04:22:17 -0700623static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600624{
Furquan Shaikh93198262018-06-03 04:22:17 -0700625 struct bus *bus = dev->bus;
626
627 while (bus) {
628 if (bus->children)
629 return 1;
630 bus = bus->next_bus;
631 }
632
633 return 0;
634}
635
Nico Huber17e9bcb2019-09-20 12:05:51 +0200636static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700637{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700638 static int dev_id;
639
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700640 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200641 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700642 ptr->name);
643 return;
644 }
645
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700646 char *name = S_ALLOC(10);
647 sprintf(name, "_dev%d", dev_id++);
648 ptr->name = name;
649
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200650 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700651 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200652 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700653 ptr->name);
654 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200655 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600656 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700657
Furquan Shaikh93198262018-06-03 04:22:17 -0700658 if (next)
659 return;
660
661 fprintf(fil,
662 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
663 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000664}
665
Furquan Shaikh93198262018-06-03 04:22:17 -0700666static void emit_resources(FILE *fil, struct device *ptr)
667{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700668 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700669 return;
670
671 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200672 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700673 struct resource *r = ptr->res;
674 while (r) {
675 fprintf(fil,
676 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
677 if (r->type == IRQ)
678 fprintf(fil, "IRQ");
679 if (r->type == DRQ)
680 fprintf(fil, "DRQ");
681 if (r->type == IO)
682 fprintf(fil, "IO");
683 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
684 r->base);
685 if (r->next)
686 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
687 i++);
688 else
689 fprintf(fil, ".next=NULL },\n");
690 r = r->next;
691 }
692
693 fprintf(fil, "\t };\n");
694}
695
696static void emit_bus(FILE *fil, struct bus *bus)
697{
698 fprintf(fil, "\t\t[%d] = {\n", bus->id);
699 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
700 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
701 if (bus->children)
702 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
703
704 if (bus->next_bus)
705 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
706 bus->id + 1);
707 else
708 fprintf(fil, "\t\t\t.next = NULL,\n");
709 fprintf(fil, "\t\t},\n");
710}
711
712static void emit_dev_links(FILE *fil, struct device *ptr)
713{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200714 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700715 ptr->name);
716
717 struct bus *bus = ptr->bus;
718
719 while (bus) {
720 emit_bus(fil, bus);
721 bus = bus->next_bus;
722 }
723
724 fprintf(fil, "\t};\n");
725}
726
Nico Huber17e9bcb2019-09-20 12:05:51 +0200727static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200728{
729 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700730 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700731 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700732
Furquan Shaikhbbade242020-05-02 16:05:29 -0700733 /*
734 * If the chip instance of device has base_chip_instance pointer set, then follow that
735 * to update the chip instance for current device.
736 */
737 if (chip_ins->base_chip_instance)
738 chip_ins = chip_ins->base_chip_instance;
739
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200740 if (ptr == &base_root_dev)
741 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
742 else
743 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
744
Furquan Shaikh93198262018-06-03 04:22:17 -0700745 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -0700746
747 /*
748 * ops field is set to default_dev_ops_root only for the root
749 * device. For all other devices, it is set by the driver at runtime.
750 */
751 if (ptr == &base_root_dev)
752 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
753 else
754 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700755 fprintf(fil, "#endif\n");
756 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
757 ptr->parent->id);
758 fprintf(fil, "\t.path = {");
759 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
760 fprintf(fil, "},\n");
761 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800762 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000763 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -0700764 fprintf(fil, "\t.on_mainboard = 1,\n");
765 if (ptr->subsystem_vendor > 0)
766 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
767 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000768
Furquan Shaikh93198262018-06-03 04:22:17 -0700769 if (ptr->subsystem_device > 0)
770 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
771 ptr->subsystem_device);
772
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700773 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700774 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600775 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700776 }
777 if (has_children)
778 fprintf(fil, "\t.link_list = &%s_links[0],\n",
779 ptr->name);
780 else
781 fprintf(fil, "\t.link_list = NULL,\n");
782 if (ptr->sibling)
783 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -0600784 else
785 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700786 fprintf(fil, "#if !DEVTREE_EARLY\n");
Kyösti Mälkki1557a672019-06-30 10:51:31 +0300787 for (pin = 0; pin < 4; pin++) {
788 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
789 fprintf(fil,
790 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
791 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
792
793 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
794 fprintf(fil,
795 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
796 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
797 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700798 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
799 chip_ins->chip->name_underscore);
800 if (chip_ins == &mainboard_instance)
801 fprintf(fil, "\t.name = mainboard_name,\n");
802 fprintf(fil, "#endif\n");
803 if (chip_ins->chip->chiph_exists)
804 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
805 chip_ins->chip->name_underscore, chip_ins->id);
806 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200807 fprintf(fil, "\t.next=&%s,\n", next->name);
808 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
809 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
810 fprintf(fil, "#if !DEVTREE_EARLY\n");
811 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
812 }
813 /* SMBIOS types start at 1, if zero it hasn't been set */
814 if (ptr->smbios_slot_type)
815 fprintf(fil, "\t.smbios_slot_type = %s,\n",
816 ptr->smbios_slot_type);
817 if (ptr->smbios_slot_data_width)
818 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
819 ptr->smbios_slot_data_width);
820 if (ptr->smbios_slot_designation)
821 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
822 ptr->smbios_slot_designation);
823 if (ptr->smbios_slot_length)
824 fprintf(fil, "\t.smbios_slot_length = %s,\n",
825 ptr->smbios_slot_length);
826 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
827 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
828 fprintf(fil, "#endif\n");
829 fprintf(fil, "#endif\n");
830 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700831 fprintf(fil, "};\n");
832
833 emit_resources(fil, ptr);
834
835 if (has_children)
836 emit_dev_links(fil, ptr);
837}
838
Nico Huber17e9bcb2019-09-20 12:05:51 +0200839static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200840{
841 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +0200842 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
843 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
844 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200845 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
846 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200847 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200848
Nico Huber17e9bcb2019-09-20 12:05:51 +0200849 if (ptr->bustype == PNP) {
850 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
851 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200852 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
853 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200854 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200855}
856
Furquan Shaikh93198262018-06-03 04:22:17 -0700857static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
858 struct device *d)
859{
860 while (d) {
861 enqueue_tail(bfs_q_head, d);
862 d = d->sibling;
863 }
864}
865
866static void add_children_to_queue(struct queue_entry **bfs_q_head,
867 struct device *d)
868{
869 struct bus *bus = d->bus;
870
871 while (bus) {
872 if (bus->children)
873 add_siblings_to_queue(bfs_q_head, bus->children);
874 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000875 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000876}
877
Nico Huber17e9bcb2019-09-20 12:05:51 +0200878static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
879 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -0700880 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600881{
Furquan Shaikh93198262018-06-03 04:22:17 -0700882 struct queue_entry *bfs_q_head = NULL;
883
884 enqueue_tail(&bfs_q_head, ptr);
885
886 while ((ptr = dequeue_head(&bfs_q_head))) {
887 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200888 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -0700889 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000890}
891
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700892static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700893{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700894 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700895
896 fprintf(fil, "#include <device/device.h>\n");
897 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700898
899 while (chip) {
900 if (chip->chiph_exists)
901 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
902 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700903 }
904 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
905 fprintf(fil,
906 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700907
908 chip = tmp;
909 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700910 fprintf(fil,
911 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700912 chip->name_underscore);
913 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700914 }
915 fprintf(fil, "#endif\n");
916}
917
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700918static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
919{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200920 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700921 instance->chip->name_underscore,
922 instance->chip->name_underscore,
923 instance->id);
924
925 if (instance->reg) {
926 fprintf(fil, "\n");
927 struct reg *r = instance->reg;
928 while (r) {
929 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
930 r = r->next;
931 }
932 }
933 fprintf(fil, "};\n\n");
934}
935
Furquan Shaikh79e84122018-05-30 15:09:09 -0700936static void emit_chips(FILE *fil)
937{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700938 struct chip *chip = chip_header.next;
939 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700940 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700941
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700942 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700943
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200944 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
945
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700946 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700947 if (!chip->chiph_exists)
948 continue;
949
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700950 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700951 instance = chip->instance;
952 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -0700953 /*
954 * Emit this chip instance only if there is no forwarding pointer to the
955 * base tree chip instance.
956 */
957 if (instance->base_chip_instance == NULL) {
958 instance->id = chip_id++;
959 emit_chip_instance(fil, instance);
960 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700961 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700962 }
963 }
964}
965
Nico Huber17e9bcb2019-09-20 12:05:51 +0200966static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -0700967 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +0000968{
969 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000970
971 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
972 /* user already gave us a subsystem vendor/device */
973 return;
974 }
975
Furquan Shaikh93198262018-06-03 04:22:17 -0700976 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000977
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800978 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000979 continue;
980
981 if (p->inherit_subsystem) {
982 dev->subsystem_vendor = p->subsystem_vendor;
983 dev->subsystem_device = p->subsystem_device;
984 break;
985 }
986 }
987}
988
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200989static void usage(void)
990{
Nico Huber17e9bcb2019-09-20 12:05:51 +0200991 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -0600992 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200993}
994
Martin Roth32051702015-11-24 12:34:16 -0700995enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -0600996 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700997 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +0200998 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700999 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -06001000};
Martin Roth32051702015-11-24 12:34:16 -07001001
Nico Huber17e9bcb2019-09-20 12:05:51 +02001002#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001003#define OPTIONAL_ARG_COUNT 1
1004#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001005
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001006static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001007{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001008 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001009 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001010 perror(NULL);
1011 exit(1);
1012 }
1013
Patrick Georgi114e7b22010-05-05 11:19:50 +00001014 yyrestart(filec);
1015
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001016 root_parent = parent;
1017 linenum = 0;
1018
Patrick Georgi114e7b22010-05-05 11:19:50 +00001019 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001020
Patrick Georgi114e7b22010-05-05 11:19:50 +00001021 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001022}
1023
Furquan Shaikh27efc502018-06-22 09:19:15 -07001024/*
1025 * Match device nodes from base and override tree to see if they are the same
1026 * node.
1027 */
1028static int device_match(struct device *a, struct device *b)
1029{
1030 return ((a->path_a == b->path_a) &&
1031 (a->path_b == b->path_b) &&
1032 (a->bustype == b->bustype) &&
1033 (a->chip_instance->chip ==
1034 b->chip_instance->chip));
1035}
1036
1037/*
Bill XIEc61d4152019-11-21 18:16:12 +08001038 * Match resource nodes from base and override tree to see if they are the same
1039 * node.
1040 */
1041static int res_match(struct resource *a, struct resource *b)
1042{
1043 return ((a->type == b->type) &&
1044 (a->index == b->index));
1045}
1046
1047/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001048 * Add resource to device. If resource is already present, then update its base
1049 * and index. If not, then add a new resource to the device.
1050 */
1051static void update_resource(struct device *dev, struct resource *res)
1052{
1053 struct resource *base_res = dev->res;
1054
1055 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001056 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001057 base_res->base = res->base;
1058 return;
1059 }
1060 base_res = base_res->next;
1061 }
1062
1063 new_resource(dev, res->type, res->index, res->base);
1064}
1065
1066/*
1067 * Add register to chip instance. If register is already present, then update
1068 * its value. If not, then add a new register to the chip instance.
1069 */
1070static void update_register(struct chip_instance *c, struct reg *reg)
1071{
1072 struct reg *base_reg = c->reg;
1073
1074 while (base_reg) {
1075 if (!strcmp(base_reg->key, reg->key)) {
1076 base_reg->value = reg->value;
1077 return;
1078 }
1079 base_reg = base_reg->next;
1080 }
1081
1082 add_register(c, reg->key, reg->value);
1083}
1084
1085static void override_devicetree(struct bus *base_parent,
1086 struct bus *override_parent);
1087
1088/*
1089 * Update the base device properties using the properties of override device. In
1090 * addition to that, call override_devicetree for all the buses under the
1091 * override device.
1092 *
1093 * Override Rules:
1094 * +--------------------+--------------------------------------------+
1095 * | | |
1096 * |struct device member| Rule |
1097 * | | |
1098 * +-----------------------------------------------------------------+
1099 * | | |
1100 * | id | Unchanged. This is used to generate device |
1101 * | | structure name in static.c. So, no need to |
1102 * | | override. |
1103 * | | |
1104 * +-----------------------------------------------------------------+
1105 * | | |
1106 * | enabled | Copy enabled state from override device. |
1107 * | | This allows variants to override device |
1108 * | | state. |
1109 * | | |
1110 * +-----------------------------------------------------------------+
1111 * | | |
1112 * | subsystem_vendor | Copy from override device only if any one |
1113 * | subsystem_device | of the ids is non-zero. |
1114 * | | |
1115 * +-----------------------------------------------------------------+
1116 * | | |
1117 * | inherit_subsystem | Copy from override device only if it is |
1118 * | | non-zero. This allows variant to only |
1119 * | | enable inherit flag for a device. |
1120 * | | |
1121 * +-----------------------------------------------------------------+
1122 * | | |
1123 * | path | Unchanged since these are same for both |
1124 * | path_a | base and override device (Used for |
1125 * | path_b | matching devices). |
1126 * | | |
1127 * +-----------------------------------------------------------------+
1128 * | | |
1129 * | bustype | Unchanged since this is same for both base |
1130 * | | and override device (User for matching |
1131 * | | devices). |
1132 * | | |
1133 * +-----------------------------------------------------------------+
1134 * | | |
1135 * | pci_irq_info | Unchanged. |
1136 * | | |
1137 * +-----------------------------------------------------------------+
1138 * | | |
1139 * | parent | Unchanged. This is meaningful only within |
1140 * | sibling | the parse tree, hence not being copied. |
1141 * | | |
1142 * +-----------------------------------------------------------------+
1143 * | | |
1144 * | res | Each resource that is present in override |
1145 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001146 * | | 1. If resource of same type and index is |
1147 * | | present in base device, then base of |
1148 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001149 * | | 2. If not, then a new resource is allocated|
1150 * | | under the base device using type, index |
1151 * | | and base from override res. |
1152 * | | |
1153 * +-----------------------------------------------------------------+
1154 * | | |
1155 * | chip_instance | Each register of chip_instance is copied |
1156 * | | over from override device to base device: |
1157 * | | 1. If register with same key is present in |
1158 * | | base device, then value of the register |
1159 * | | is copied. |
1160 * | | 2. If not, then a new register is allocated|
1161 * | | under the base chip_instance using key |
1162 * | | and value from override register. |
1163 * | | |
1164 * +-----------------------------------------------------------------+
1165 * | | |
1166 * | bus | Recursively call override_devicetree on |
1167 * | last_bus | each bus of override device. It is assumed |
1168 * | | that bus with id X under base device |
1169 * | | to bus with id X under override device. If |
1170 * | | override device has more buses than base |
1171 * | | device, then new buses are allocated under |
1172 * | | base device. |
1173 * | | |
1174 * +-----------------------------------------------------------------+
1175 */
1176static void update_device(struct device *base_dev, struct device *override_dev)
1177{
1178 /*
1179 * Copy the enabled state of override device to base device. This allows
1180 * override tree to enable or disable a particular device.
1181 */
1182 base_dev->enabled = override_dev->enabled;
1183
1184 /*
1185 * Copy subsystem vendor and device ids from override device to base
1186 * device only if the ids are non-zero in override device. Else, honor
1187 * the values in base device.
1188 */
1189 if (override_dev->subsystem_vendor ||
1190 override_dev->subsystem_device) {
1191 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1192 base_dev->subsystem_device = override_dev->subsystem_device;
1193 }
1194
1195 /*
1196 * Copy value of inherity_subsystem from override device to base device
1197 * only if it is non-zero in override device. This allows override
1198 * tree to only enable inhert flag for a device.
1199 */
1200 if (override_dev->inherit_subsystem)
1201 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1202
1203 /*
1204 * Copy resources of override device to base device.
1205 * 1. If resource is already present in base device, then index and base
1206 * of the resource will be copied over.
1207 * 2. If resource is not already present in base device, a new resource
1208 * will be allocated.
1209 */
1210 struct resource *res = override_dev->res;
1211 while (res) {
1212 update_resource(base_dev, res);
1213 res = res->next;
1214 }
1215
1216 /*
1217 * Copy registers of override chip instance to base chip instance.
1218 * 1. If register key is already present in base chip instance, then
1219 * value for the register is copied over.
1220 * 2. If register key is not already present in base chip instance, then
1221 * a new register will be allocated.
1222 */
1223 struct reg *reg = override_dev->chip_instance->reg;
1224 while (reg) {
1225 update_register(base_dev->chip_instance, reg);
1226 reg = reg->next;
1227 }
1228
1229 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001230 * Update base_chip_instance member in chip instance of override tree to forward it to
1231 * the chip instance in base tree.
1232 */
1233 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1234
1235 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001236 * Now that the device properties are all copied over, look at each bus
1237 * of the override device and run override_devicetree in a recursive
1238 * manner. The assumption here is that first bus of override device
1239 * corresponds to first bus of base device and so on. If base device has
1240 * lesser buses than override tree, then new buses are allocated for it.
1241 */
1242 struct bus *override_bus = override_dev->bus;
1243 struct bus *base_bus = base_dev->bus;
1244
1245 while (override_bus) {
1246
1247 /*
1248 * If we have more buses in override tree device, then allocate
1249 * a new bus for the base tree device as well.
1250 */
1251 if (!base_bus) {
1252 alloc_bus(base_dev);
1253 base_bus = base_dev->last_bus;
1254 }
1255
1256 override_devicetree(base_dev->bus, override_dev->bus);
1257
1258 override_bus = override_bus->next_bus;
1259 base_bus = base_bus->next_bus;
1260 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001261}
1262
1263/*
1264 * Perform copy of device and properties from override parent to base parent.
1265 * This function walks through the override tree in a depth-first manner
1266 * performing following actions:
1267 * 1. If matching device is found in base tree, then copy the properties of
1268 * override device to base tree device. Call override_devicetree recursively on
1269 * the bus of override device.
1270 * 2. If matching device is not found in base tree, then set override tree
1271 * device as new child of base_parent and update the chip pointers in override
1272 * device subtree to ensure the nodes do not point to override tree chip
1273 * instance.
1274 */
1275static void override_devicetree(struct bus *base_parent,
1276 struct bus *override_parent)
1277{
1278 struct device *base_child;
1279 struct device *override_child = override_parent->children;
1280 struct device *next_child;
1281
1282 while (override_child) {
1283
1284 /* Look for a matching device in base tree. */
1285 for (base_child = base_parent->children;
1286 base_child; base_child = base_child->sibling) {
1287 if (device_match(base_child, override_child))
1288 break;
1289 }
1290
1291 next_child = override_child->sibling;
1292
1293 /*
1294 * If matching device is found, copy properties of
1295 * override_child to base_child.
1296 */
1297 if (base_child)
1298 update_device(base_child, override_child);
1299 else {
1300 /*
1301 * If matching device is not found, set override_child
1302 * as a new child of base_parent.
1303 */
1304 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001305 }
1306
1307 override_child = next_child;
1308 }
1309}
1310
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001311int main(int argc, char **argv)
1312{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001313 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001314 usage();
1315
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001316 const char *base_devtree = argv[DEVICEFILE_ARG];
1317 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001318 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001319 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001320
1321 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001322
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001323 if (argc == TOTAL_ARG_COUNT) {
1324 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1325 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001326
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001327 if (!dev_has_children(&override_root_dev)) {
1328 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1329 exit(1);
1330 }
1331
Furquan Shaikh27efc502018-06-22 09:19:15 -07001332 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001333 }
1334
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001335 FILE *autogen = fopen(outputc, "w");
1336 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001337 fprintf(stderr, "Could not open file '%s' for writing: ",
1338 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001339 perror(NULL);
1340 exit(1);
1341 }
1342
Nico Huber17e9bcb2019-09-20 12:05:51 +02001343 FILE *autohead = fopen(outputh, "w");
1344 if (!autohead) {
1345 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1346 perror(NULL);
1347 fclose(autogen);
1348 exit(1);
1349 }
1350 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1351 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1352 fprintf(autohead, "#include <device/device.h>\n\n");
1353
Furquan Shaikh79e84122018-05-30 15:09:09 -07001354 emit_chips(autogen);
1355
Nico Huber17e9bcb2019-09-20 12:05:51 +02001356 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001357 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001358 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001359 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001360 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001361
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001362 /* Expose static devicenames to global namespace. */
1363 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001364 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001365
Nico Huber17e9bcb2019-09-20 12:05:51 +02001366 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1367 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001368 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001369
Patrick Georgi114e7b22010-05-05 11:19:50 +00001370 return 0;
1371}