blob: 0d8f7f86af250397de2f2fc539a54b054c4a96d2 [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
Nico Huberd09459f2020-05-26 22:13:09 +0200531static void add_reg(struct reg **const head, char *const name, char *const val)
Martin Rothbec07532016-08-05 18:32:18 -0600532{
Nico Huberd09459f2020-05-26 22:13:09 +0200533 struct reg *const r = S_ALLOC(sizeof(struct reg));
534 struct reg *prev = NULL;
535 struct reg *cur;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700536
Patrick Georgi114e7b22010-05-05 11:19:50 +0000537 r->key = name;
538 r->value = val;
Nico Huberd09459f2020-05-26 22:13:09 +0200539
540 for (cur = *head; cur != NULL; prev = cur, cur = cur->next) {
541 const int sort = strcmp(r->key, cur->key);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000542 if (sort == 0) {
543 printf("ERROR: duplicate 'register' key.\n");
544 exit(1);
545 }
Nico Huberd09459f2020-05-26 22:13:09 +0200546 if (sort < 0)
547 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000548 }
Nico Huberd09459f2020-05-26 22:13:09 +0200549 r->next = cur;
550 if (prev)
551 prev->next = r;
552 else
553 *head = r;
554}
555
556void add_register(struct chip_instance *chip_instance, char *name, char *val)
557{
558 add_reg(&chip_instance->reg, name, val);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000559}
560
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200561void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
562 char *data_width)
563{
564 struct device *dev = bus->dev;
565
566 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
567 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
568 exit(1);
569 }
570
571 dev->smbios_slot_type = type;
572 dev->smbios_slot_length = length;
573 dev->smbios_slot_data_width = data_width;
574 dev->smbios_slot_designation = designation;
575}
576
Furquan Shaikh93198262018-06-03 04:22:17 -0700577void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600578 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000579{
Furquan Shaikh93198262018-06-03 04:22:17 -0700580 struct device *dev = bus->dev;
581
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800582 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000583 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
584 exit(1);
585 }
586
587 dev->subsystem_vendor = vendor;
588 dev->subsystem_device = device;
589 dev->inherit_subsystem = inherit;
590}
591
Furquan Shaikh93198262018-06-03 04:22:17 -0700592void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600593 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200594{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200595 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700596 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200597
Martin Rothbec07532016-08-05 18:32:18 -0600598 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
599 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200600 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
601 exit(1);
602 }
603
604 srcpin = _srcpin[3] - 'A';
605
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800606 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200607 printf("ERROR: ioapic config only allowed for PCI devices\n");
608 exit(1);
609 }
610
611 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200612 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200613 exit(1);
614 }
615 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
616 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
617}
618
Furquan Shaikh93198262018-06-03 04:22:17 -0700619static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600620{
Furquan Shaikh93198262018-06-03 04:22:17 -0700621 struct bus *bus = dev->bus;
622
623 while (bus) {
624 if (bus->children)
625 return 1;
626 bus = bus->next_bus;
627 }
628
629 return 0;
630}
631
Nico Huber17e9bcb2019-09-20 12:05:51 +0200632static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700633{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700634 static int dev_id;
635
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700636 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200637 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700638 ptr->name);
639 return;
640 }
641
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700642 char *name = S_ALLOC(10);
643 sprintf(name, "_dev%d", dev_id++);
644 ptr->name = name;
645
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200646 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700647 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200648 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700649 ptr->name);
650 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200651 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600652 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700653
Furquan Shaikh93198262018-06-03 04:22:17 -0700654 if (next)
655 return;
656
657 fprintf(fil,
658 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
659 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000660}
661
Furquan Shaikh93198262018-06-03 04:22:17 -0700662static void emit_resources(FILE *fil, struct device *ptr)
663{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700664 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700665 return;
666
667 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200668 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700669 struct resource *r = ptr->res;
670 while (r) {
671 fprintf(fil,
672 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
673 if (r->type == IRQ)
674 fprintf(fil, "IRQ");
675 if (r->type == DRQ)
676 fprintf(fil, "DRQ");
677 if (r->type == IO)
678 fprintf(fil, "IO");
679 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
680 r->base);
681 if (r->next)
682 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
683 i++);
684 else
685 fprintf(fil, ".next=NULL },\n");
686 r = r->next;
687 }
688
689 fprintf(fil, "\t };\n");
690}
691
692static void emit_bus(FILE *fil, struct bus *bus)
693{
694 fprintf(fil, "\t\t[%d] = {\n", bus->id);
695 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
696 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
697 if (bus->children)
698 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
699
700 if (bus->next_bus)
701 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
702 bus->id + 1);
703 else
704 fprintf(fil, "\t\t\t.next = NULL,\n");
705 fprintf(fil, "\t\t},\n");
706}
707
708static void emit_dev_links(FILE *fil, struct device *ptr)
709{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200710 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700711 ptr->name);
712
713 struct bus *bus = ptr->bus;
714
715 while (bus) {
716 emit_bus(fil, bus);
717 bus = bus->next_bus;
718 }
719
720 fprintf(fil, "\t};\n");
721}
722
Nico Huber17e9bcb2019-09-20 12:05:51 +0200723static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200724{
725 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700726 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700727 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700728
Furquan Shaikhbbade242020-05-02 16:05:29 -0700729 /*
730 * If the chip instance of device has base_chip_instance pointer set, then follow that
731 * to update the chip instance for current device.
732 */
733 if (chip_ins->base_chip_instance)
734 chip_ins = chip_ins->base_chip_instance;
735
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200736 if (ptr == &base_root_dev)
737 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
738 else
739 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
740
Furquan Shaikh93198262018-06-03 04:22:17 -0700741 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -0700742
743 /*
744 * ops field is set to default_dev_ops_root only for the root
745 * device. For all other devices, it is set by the driver at runtime.
746 */
747 if (ptr == &base_root_dev)
748 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
749 else
750 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700751 fprintf(fil, "#endif\n");
752 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
753 ptr->parent->id);
754 fprintf(fil, "\t.path = {");
755 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
756 fprintf(fil, "},\n");
757 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800758 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000759 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -0700760 fprintf(fil, "\t.on_mainboard = 1,\n");
761 if (ptr->subsystem_vendor > 0)
762 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
763 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000764
Furquan Shaikh93198262018-06-03 04:22:17 -0700765 if (ptr->subsystem_device > 0)
766 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
767 ptr->subsystem_device);
768
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700769 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700770 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600771 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700772 }
773 if (has_children)
774 fprintf(fil, "\t.link_list = &%s_links[0],\n",
775 ptr->name);
776 else
777 fprintf(fil, "\t.link_list = NULL,\n");
778 if (ptr->sibling)
779 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -0600780 else
781 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700782 fprintf(fil, "#if !DEVTREE_EARLY\n");
Kyösti Mälkki1557a672019-06-30 10:51:31 +0300783 for (pin = 0; pin < 4; pin++) {
784 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
785 fprintf(fil,
786 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
787 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
788
789 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
790 fprintf(fil,
791 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
792 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
793 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700794 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
795 chip_ins->chip->name_underscore);
796 if (chip_ins == &mainboard_instance)
797 fprintf(fil, "\t.name = mainboard_name,\n");
798 fprintf(fil, "#endif\n");
799 if (chip_ins->chip->chiph_exists)
800 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
801 chip_ins->chip->name_underscore, chip_ins->id);
802 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200803 fprintf(fil, "\t.next=&%s,\n", next->name);
804 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
805 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
806 fprintf(fil, "#if !DEVTREE_EARLY\n");
807 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
808 }
809 /* SMBIOS types start at 1, if zero it hasn't been set */
810 if (ptr->smbios_slot_type)
811 fprintf(fil, "\t.smbios_slot_type = %s,\n",
812 ptr->smbios_slot_type);
813 if (ptr->smbios_slot_data_width)
814 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
815 ptr->smbios_slot_data_width);
816 if (ptr->smbios_slot_designation)
817 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
818 ptr->smbios_slot_designation);
819 if (ptr->smbios_slot_length)
820 fprintf(fil, "\t.smbios_slot_length = %s,\n",
821 ptr->smbios_slot_length);
822 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
823 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
824 fprintf(fil, "#endif\n");
825 fprintf(fil, "#endif\n");
826 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700827 fprintf(fil, "};\n");
828
829 emit_resources(fil, ptr);
830
831 if (has_children)
832 emit_dev_links(fil, ptr);
833}
834
Nico Huber17e9bcb2019-09-20 12:05:51 +0200835static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200836{
837 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +0200838 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
839 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
840 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200841 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
842 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200843 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200844
Nico Huber17e9bcb2019-09-20 12:05:51 +0200845 if (ptr->bustype == PNP) {
846 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
847 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200848 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
849 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200850 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200851}
852
Furquan Shaikh93198262018-06-03 04:22:17 -0700853static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
854 struct device *d)
855{
856 while (d) {
857 enqueue_tail(bfs_q_head, d);
858 d = d->sibling;
859 }
860}
861
862static void add_children_to_queue(struct queue_entry **bfs_q_head,
863 struct device *d)
864{
865 struct bus *bus = d->bus;
866
867 while (bus) {
868 if (bus->children)
869 add_siblings_to_queue(bfs_q_head, bus->children);
870 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000871 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000872}
873
Nico Huber17e9bcb2019-09-20 12:05:51 +0200874static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
875 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -0700876 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600877{
Furquan Shaikh93198262018-06-03 04:22:17 -0700878 struct queue_entry *bfs_q_head = NULL;
879
880 enqueue_tail(&bfs_q_head, ptr);
881
882 while ((ptr = dequeue_head(&bfs_q_head))) {
883 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200884 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -0700885 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000886}
887
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700888static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700889{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700890 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700891
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700892 while (chip) {
893 if (chip->chiph_exists)
894 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
895 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700896 }
897 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
898 fprintf(fil,
899 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700900
901 chip = tmp;
902 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700903 fprintf(fil,
904 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700905 chip->name_underscore);
906 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700907 }
908 fprintf(fil, "#endif\n");
909}
910
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700911static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
912{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200913 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700914 instance->chip->name_underscore,
915 instance->chip->name_underscore,
916 instance->id);
917
918 if (instance->reg) {
919 fprintf(fil, "\n");
920 struct reg *r = instance->reg;
921 while (r) {
922 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
923 r = r->next;
924 }
925 }
926 fprintf(fil, "};\n\n");
927}
928
Furquan Shaikh79e84122018-05-30 15:09:09 -0700929static void emit_chips(FILE *fil)
930{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700931 struct chip *chip = chip_header.next;
932 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700933 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700934
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700935 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700936
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200937 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
938
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700939 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700940 if (!chip->chiph_exists)
941 continue;
942
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700943 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700944 instance = chip->instance;
945 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -0700946 /*
947 * Emit this chip instance only if there is no forwarding pointer to the
948 * base tree chip instance.
949 */
950 if (instance->base_chip_instance == NULL) {
951 instance->id = chip_id++;
952 emit_chip_instance(fil, instance);
953 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700954 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700955 }
956 }
957}
958
Nico Huber17e9bcb2019-09-20 12:05:51 +0200959static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -0700960 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +0000961{
962 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000963
964 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
965 /* user already gave us a subsystem vendor/device */
966 return;
967 }
968
Furquan Shaikh93198262018-06-03 04:22:17 -0700969 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000970
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800971 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000972 continue;
973
974 if (p->inherit_subsystem) {
975 dev->subsystem_vendor = p->subsystem_vendor;
976 dev->subsystem_device = p->subsystem_device;
977 break;
978 }
979 }
980}
981
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200982static void usage(void)
983{
Nico Huber17e9bcb2019-09-20 12:05:51 +0200984 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -0600985 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200986}
987
Martin Roth32051702015-11-24 12:34:16 -0700988enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -0600989 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700990 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +0200991 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700992 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -0600993};
Martin Roth32051702015-11-24 12:34:16 -0700994
Nico Huber17e9bcb2019-09-20 12:05:51 +0200995#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700996#define OPTIONAL_ARG_COUNT 1
997#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200998
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700999static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001000{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001001 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001002 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001003 perror(NULL);
1004 exit(1);
1005 }
1006
Patrick Georgi114e7b22010-05-05 11:19:50 +00001007 yyrestart(filec);
1008
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001009 root_parent = parent;
1010 linenum = 0;
1011
Patrick Georgi114e7b22010-05-05 11:19:50 +00001012 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001013
Patrick Georgi114e7b22010-05-05 11:19:50 +00001014 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001015}
1016
Furquan Shaikh27efc502018-06-22 09:19:15 -07001017/*
1018 * Match device nodes from base and override tree to see if they are the same
1019 * node.
1020 */
1021static int device_match(struct device *a, struct device *b)
1022{
1023 return ((a->path_a == b->path_a) &&
1024 (a->path_b == b->path_b) &&
1025 (a->bustype == b->bustype) &&
1026 (a->chip_instance->chip ==
1027 b->chip_instance->chip));
1028}
1029
1030/*
Bill XIEc61d4152019-11-21 18:16:12 +08001031 * Match resource nodes from base and override tree to see if they are the same
1032 * node.
1033 */
1034static int res_match(struct resource *a, struct resource *b)
1035{
1036 return ((a->type == b->type) &&
1037 (a->index == b->index));
1038}
1039
1040/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001041 * Add resource to device. If resource is already present, then update its base
1042 * and index. If not, then add a new resource to the device.
1043 */
1044static void update_resource(struct device *dev, struct resource *res)
1045{
1046 struct resource *base_res = dev->res;
1047
1048 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001049 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001050 base_res->base = res->base;
1051 return;
1052 }
1053 base_res = base_res->next;
1054 }
1055
1056 new_resource(dev, res->type, res->index, res->base);
1057}
1058
1059/*
1060 * Add register to chip instance. If register is already present, then update
1061 * its value. If not, then add a new register to the chip instance.
1062 */
1063static void update_register(struct chip_instance *c, struct reg *reg)
1064{
1065 struct reg *base_reg = c->reg;
1066
1067 while (base_reg) {
1068 if (!strcmp(base_reg->key, reg->key)) {
1069 base_reg->value = reg->value;
1070 return;
1071 }
1072 base_reg = base_reg->next;
1073 }
1074
1075 add_register(c, reg->key, reg->value);
1076}
1077
1078static void override_devicetree(struct bus *base_parent,
1079 struct bus *override_parent);
1080
1081/*
1082 * Update the base device properties using the properties of override device. In
1083 * addition to that, call override_devicetree for all the buses under the
1084 * override device.
1085 *
1086 * Override Rules:
1087 * +--------------------+--------------------------------------------+
1088 * | | |
1089 * |struct device member| Rule |
1090 * | | |
1091 * +-----------------------------------------------------------------+
1092 * | | |
1093 * | id | Unchanged. This is used to generate device |
1094 * | | structure name in static.c. So, no need to |
1095 * | | override. |
1096 * | | |
1097 * +-----------------------------------------------------------------+
1098 * | | |
1099 * | enabled | Copy enabled state from override device. |
1100 * | | This allows variants to override device |
1101 * | | state. |
1102 * | | |
1103 * +-----------------------------------------------------------------+
1104 * | | |
1105 * | subsystem_vendor | Copy from override device only if any one |
1106 * | subsystem_device | of the ids is non-zero. |
1107 * | | |
1108 * +-----------------------------------------------------------------+
1109 * | | |
1110 * | inherit_subsystem | Copy from override device only if it is |
1111 * | | non-zero. This allows variant to only |
1112 * | | enable inherit flag for a device. |
1113 * | | |
1114 * +-----------------------------------------------------------------+
1115 * | | |
1116 * | path | Unchanged since these are same for both |
1117 * | path_a | base and override device (Used for |
1118 * | path_b | matching devices). |
1119 * | | |
1120 * +-----------------------------------------------------------------+
1121 * | | |
1122 * | bustype | Unchanged since this is same for both base |
1123 * | | and override device (User for matching |
1124 * | | devices). |
1125 * | | |
1126 * +-----------------------------------------------------------------+
1127 * | | |
1128 * | pci_irq_info | Unchanged. |
1129 * | | |
1130 * +-----------------------------------------------------------------+
1131 * | | |
1132 * | parent | Unchanged. This is meaningful only within |
1133 * | sibling | the parse tree, hence not being copied. |
1134 * | | |
1135 * +-----------------------------------------------------------------+
1136 * | | |
1137 * | res | Each resource that is present in override |
1138 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001139 * | | 1. If resource of same type and index is |
1140 * | | present in base device, then base of |
1141 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001142 * | | 2. If not, then a new resource is allocated|
1143 * | | under the base device using type, index |
1144 * | | and base from override res. |
1145 * | | |
1146 * +-----------------------------------------------------------------+
1147 * | | |
1148 * | chip_instance | Each register of chip_instance is copied |
1149 * | | over from override device to base device: |
1150 * | | 1. If register with same key is present in |
1151 * | | base device, then value of the register |
1152 * | | is copied. |
1153 * | | 2. If not, then a new register is allocated|
1154 * | | under the base chip_instance using key |
1155 * | | and value from override register. |
1156 * | | |
1157 * +-----------------------------------------------------------------+
1158 * | | |
1159 * | bus | Recursively call override_devicetree on |
1160 * | last_bus | each bus of override device. It is assumed |
1161 * | | that bus with id X under base device |
1162 * | | to bus with id X under override device. If |
1163 * | | override device has more buses than base |
1164 * | | device, then new buses are allocated under |
1165 * | | base device. |
1166 * | | |
1167 * +-----------------------------------------------------------------+
1168 */
1169static void update_device(struct device *base_dev, struct device *override_dev)
1170{
1171 /*
1172 * Copy the enabled state of override device to base device. This allows
1173 * override tree to enable or disable a particular device.
1174 */
1175 base_dev->enabled = override_dev->enabled;
1176
1177 /*
1178 * Copy subsystem vendor and device ids from override device to base
1179 * device only if the ids are non-zero in override device. Else, honor
1180 * the values in base device.
1181 */
1182 if (override_dev->subsystem_vendor ||
1183 override_dev->subsystem_device) {
1184 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1185 base_dev->subsystem_device = override_dev->subsystem_device;
1186 }
1187
1188 /*
1189 * Copy value of inherity_subsystem from override device to base device
1190 * only if it is non-zero in override device. This allows override
1191 * tree to only enable inhert flag for a device.
1192 */
1193 if (override_dev->inherit_subsystem)
1194 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1195
1196 /*
1197 * Copy resources of override device to base device.
1198 * 1. If resource is already present in base device, then index and base
1199 * of the resource will be copied over.
1200 * 2. If resource is not already present in base device, a new resource
1201 * will be allocated.
1202 */
1203 struct resource *res = override_dev->res;
1204 while (res) {
1205 update_resource(base_dev, res);
1206 res = res->next;
1207 }
1208
1209 /*
1210 * Copy registers of override chip instance to base chip instance.
1211 * 1. If register key is already present in base chip instance, then
1212 * value for the register is copied over.
1213 * 2. If register key is not already present in base chip instance, then
1214 * a new register will be allocated.
1215 */
1216 struct reg *reg = override_dev->chip_instance->reg;
1217 while (reg) {
1218 update_register(base_dev->chip_instance, reg);
1219 reg = reg->next;
1220 }
1221
1222 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001223 * Update base_chip_instance member in chip instance of override tree to forward it to
1224 * the chip instance in base tree.
1225 */
1226 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1227
1228 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001229 * Now that the device properties are all copied over, look at each bus
1230 * of the override device and run override_devicetree in a recursive
1231 * manner. The assumption here is that first bus of override device
1232 * corresponds to first bus of base device and so on. If base device has
1233 * lesser buses than override tree, then new buses are allocated for it.
1234 */
1235 struct bus *override_bus = override_dev->bus;
1236 struct bus *base_bus = base_dev->bus;
1237
1238 while (override_bus) {
1239
1240 /*
1241 * If we have more buses in override tree device, then allocate
1242 * a new bus for the base tree device as well.
1243 */
1244 if (!base_bus) {
1245 alloc_bus(base_dev);
1246 base_bus = base_dev->last_bus;
1247 }
1248
1249 override_devicetree(base_dev->bus, override_dev->bus);
1250
1251 override_bus = override_bus->next_bus;
1252 base_bus = base_bus->next_bus;
1253 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001254}
1255
1256/*
1257 * Perform copy of device and properties from override parent to base parent.
1258 * This function walks through the override tree in a depth-first manner
1259 * performing following actions:
1260 * 1. If matching device is found in base tree, then copy the properties of
1261 * override device to base tree device. Call override_devicetree recursively on
1262 * the bus of override device.
1263 * 2. If matching device is not found in base tree, then set override tree
1264 * device as new child of base_parent and update the chip pointers in override
1265 * device subtree to ensure the nodes do not point to override tree chip
1266 * instance.
1267 */
1268static void override_devicetree(struct bus *base_parent,
1269 struct bus *override_parent)
1270{
1271 struct device *base_child;
1272 struct device *override_child = override_parent->children;
1273 struct device *next_child;
1274
1275 while (override_child) {
1276
1277 /* Look for a matching device in base tree. */
1278 for (base_child = base_parent->children;
1279 base_child; base_child = base_child->sibling) {
1280 if (device_match(base_child, override_child))
1281 break;
1282 }
1283
1284 next_child = override_child->sibling;
1285
1286 /*
1287 * If matching device is found, copy properties of
1288 * override_child to base_child.
1289 */
1290 if (base_child)
1291 update_device(base_child, override_child);
1292 else {
1293 /*
1294 * If matching device is not found, set override_child
1295 * as a new child of base_parent.
1296 */
1297 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001298 }
1299
1300 override_child = next_child;
1301 }
1302}
1303
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001304int main(int argc, char **argv)
1305{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001306 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001307 usage();
1308
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001309 const char *base_devtree = argv[DEVICEFILE_ARG];
1310 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001311 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001312 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001313
1314 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001315
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001316 if (argc == TOTAL_ARG_COUNT) {
1317 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1318 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001319
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001320 if (!dev_has_children(&override_root_dev)) {
1321 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1322 exit(1);
1323 }
1324
Furquan Shaikh27efc502018-06-22 09:19:15 -07001325 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001326 }
1327
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001328 FILE *autogen = fopen(outputc, "w");
1329 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001330 fprintf(stderr, "Could not open file '%s' for writing: ",
1331 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001332 perror(NULL);
1333 exit(1);
1334 }
1335
Nico Huber17e9bcb2019-09-20 12:05:51 +02001336 FILE *autohead = fopen(outputh, "w");
1337 if (!autohead) {
1338 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1339 perror(NULL);
1340 fclose(autogen);
1341 exit(1);
1342 }
1343 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1344 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1345 fprintf(autohead, "#include <device/device.h>\n\n");
1346
Nico Huber57bbcb32020-05-26 22:39:47 +02001347 fprintf(autogen, "#include <device/device.h>\n");
1348 fprintf(autogen, "#include <device/pci.h>\n\n");
1349
Furquan Shaikh79e84122018-05-30 15:09:09 -07001350 emit_chips(autogen);
1351
Nico Huber17e9bcb2019-09-20 12:05:51 +02001352 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001353 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001354 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001355 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001356 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001357
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001358 /* Expose static devicenames to global namespace. */
1359 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001360 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001361
Nico Huber17e9bcb2019-09-20 12:05:51 +02001362 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1363 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001364 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001365
Patrick Georgi114e7b22010-05-05 11:19:50 +00001366 return 0;
1367}