blob: 951ad6d219ccae4a034a2e69e369df9cc4c140f1 [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,
Furquan Shaikh27efc502018-06-22 09:19:15 -0700131 .ref_count = 2,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000132};
133
Furquan Shaikh93198262018-06-03 04:22:17 -0700134/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700135struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000136
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700137struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700138 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700139 struct queue_entry *next;
140 struct queue_entry *prev;
141};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700142
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700143#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700144
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700145static void *s_alloc(const char *f, size_t s)
146{
147 void *data = calloc(1, s);
148 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530149 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700150 exit(1);
151 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700152 return data;
153}
154
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700155static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700156{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700157 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700158
159 e->data = data;
160 e->next = e->prev = e;
161 return e;
162}
163
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700164static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700165{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700166 struct queue_entry *tmp = new_queue_entry(data);
167 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700168
169 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700170 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700171 return;
172 }
173
174 q->prev->next = tmp;
175 tmp->prev = q->prev;
176 q->prev = tmp;
177 tmp->next = q;
178}
179
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700180static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700181{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700182 struct queue_entry *q = *q_head;
183 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700184 void *data;
185
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700186 if (!q)
187 return NULL;
188
189 tmp = q->prev;
190
Furquan Shaikh79e84122018-05-30 15:09:09 -0700191 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700192 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700193 else {
194 tmp->prev->next = q;
195 q->prev = tmp->prev;
196 }
197
198 data = tmp->data;
199 free(tmp);
200
201 return data;
202}
203
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700204static void *dequeue_head(struct queue_entry **q_head)
205{
206 struct queue_entry *q = *q_head;
207 struct queue_entry *tmp = q;
208 void *data;
209
210 if (!q)
211 return NULL;
212
213 if (q->next == q)
214 *q_head = NULL;
215 else {
216 q->next->prev = q->prev;
217 q->prev->next = q->next;
218 *q_head = q->next;
219 }
220
221 data = tmp->data;
222 free(tmp);
223
224 return data;
225}
226
227static void *peek_queue_head(struct queue_entry *q_head)
228{
229 if (!q_head)
230 return NULL;
231
232 return q_head->data;
233}
234
235static struct queue_entry *chip_q_head;
236
237void chip_enqueue_tail(void *data)
238{
239 enqueue_tail(&chip_q_head, data);
240}
241
242void *chip_dequeue_tail(void)
243{
244 return dequeue_tail(&chip_q_head);
245}
246
Martin Rothbec07532016-08-05 18:32:18 -0600247int yywrap(void)
248{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000249 return 1;
250}
251
Martin Rothbec07532016-08-05 18:32:18 -0600252void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000253{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000254 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600255 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000256 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000257}
258
Martin Rothbec07532016-08-05 18:32:18 -0600259char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200260{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200261 char *b, *c;
262 b = c = strdup(str);
263 while (c && *c) {
264 if ((mode == SPLIT_1ST) && (*c == '/')) {
265 *c = 0;
266 break;
267 }
Martin Rothbec07532016-08-05 18:32:18 -0600268 if (*c == '/')
269 *c = '_';
270 if (*c == '-')
271 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200272 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200273 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200274 if (mode == TO_LOWER)
275 *c = tolower(*c);
276 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200277 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200278 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200279}
280
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700281static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600282{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700283 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700284
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700285 while (h->next) {
286 int result = strcmp(path, h->next->name);
287 if (result == 0)
288 return h->next;
289
290 if (result < 0)
291 break;
292
293 h = h->next;
294 }
295
296 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
297 new_chip->next = h->next;
298 h->next = new_chip;
299
Patrick Georgi114e7b22010-05-05 11:19:50 +0000300 new_chip->chiph_exists = 1;
301 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700302 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000303
304 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700305 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700306 sprintf(chip_h, "src/%s", path);
307 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100308 /* root_complex gets away without a separate directory, but
309 * exists on on pretty much all AMD chipsets.
310 */
311 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300312 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
313 path);
314 exit(1);
315 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700316 }
317
Martin Roth824255e2016-08-05 17:40:39 -0600318 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200319
Martin Roth824255e2016-08-05 17:40:39 -0600320 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600321 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000322
Patrick Georgi1f688802014-08-03 15:51:19 +0200323 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700324
Patrick Georgi114e7b22010-05-05 11:19:50 +0000325 return new_chip;
326}
327
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700328struct chip_instance *new_chip_instance(char *path)
329{
330 struct chip *chip = get_chip(path);
331 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
332
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700333 instance->chip = chip;
334 instance->next = chip->instance;
335 chip->instance = instance;
336
337 return instance;
338}
339
Furquan Shaikh27efc502018-06-22 09:19:15 -0700340static void delete_chip_instance(struct chip_instance *ins)
341{
342
343 if (ins->ref_count == 0) {
344 printf("ERROR: ref count for chip instance is zero!!\n");
345 exit(1);
346 }
347
348 if (--ins->ref_count)
349 return;
350
351 struct chip *c = ins->chip;
352
353 /* Get pointer to first instance of the chip. */
354 struct chip_instance *i = c->instance;
355
356 /*
357 * If chip instance to be deleted is the first instance, then update
358 * instance pointer of the chip as well.
359 */
360 if (i == ins) {
361 c->instance = ins->next;
362 free(ins);
363 return;
364 }
365
366 /*
367 * Loop through the instances list of the chip to find and remove the
368 * given instance.
369 */
370 while (1) {
371 if (i == NULL) {
372 printf("ERROR: chip instance not found!\n");
373 exit(1);
374 }
375
376 if (i->next != ins) {
377 i = i->next;
378 continue;
379 }
380
381 i->next = ins->next;
382 break;
383 }
384
385 free(ins);
386}
387
Furquan Shaikh93198262018-06-03 04:22:17 -0700388/*
389 * Allocate a new bus for the provided device.
390 * - If this is the first bus being allocated under this device, then its id
391 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
392 * - If this is not the first bus under this device, then its id is set to 1
393 * plus the id of last bus and newly allocated bus is added to the list of
394 * buses under the device. last_bus is updated to point to the newly
395 * allocated bus.
396 */
397static void alloc_bus(struct device *dev)
398{
399 struct bus *bus = S_ALLOC(sizeof(*bus));
400
401 bus->dev = dev;
402
403 if (dev->last_bus == NULL) {
404 bus->id = 0;
405 dev->bus = bus;
406 } else {
407 bus->id = dev->last_bus->id + 1;
408 dev->last_bus->next_bus = bus;
409 }
410
411 dev->last_bus = bus;
412}
413
414/*
415 * Allocate a new device under the given parent. This function allocates a new
416 * device structure under the provided parent bus and allocates a bus structure
417 * under the newly allocated device.
418 */
419static struct device *alloc_dev(struct bus *parent)
420{
421 struct device *dev = S_ALLOC(sizeof(*dev));
422
Furquan Shaikh93198262018-06-03 04:22:17 -0700423 dev->parent = parent;
424 dev->subsystem_vendor = -1;
425 dev->subsystem_device = -1;
426
427 alloc_bus(dev);
428
429 return dev;
430}
431
432/*
433 * This function scans the children of given bus to see if any device matches
434 * the new device that is requested.
435 *
436 * Returns pointer to the node if found, else NULL.
437 */
438static struct device *get_dev(struct bus *parent, int path_a, int path_b,
439 int bustype, struct chip_instance *chip_instance)
440{
441 struct device *child = parent->children;
442
443 while (child) {
444 if ((child->path_a == path_a) && (child->path_b == path_b) &&
445 (child->bustype == bustype) &&
446 (child->chip_instance == chip_instance))
447 return child;
448
449 child = child->sibling;
450 }
451
452 return NULL;
453}
454
Furquan Shaikh27efc502018-06-22 09:19:15 -0700455/*
456 * Add given node as child of the provided parent. If this is the first child of
457 * the parent, update parent->children pointer as well.
458 */
459static void set_new_child(struct bus *parent, struct device *child)
460{
461 struct device *c = parent->children;
462 if (c) {
463 while (c->sibling)
464 c = c->sibling;
465 c->sibling = child;
466 } else
467 parent->children = child;
468
469 child->sibling = NULL;
470 child->parent = parent;
471}
472
Furquan Shaikh93198262018-06-03 04:22:17 -0700473struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700474 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700475 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800476 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600477{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000478 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700479 int path_a;
480 int path_b = 0;
481 struct device *new_d;
482
483 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000484 if (*tmp == '.') {
485 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700486 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000487 }
488
Furquan Shaikh93198262018-06-03 04:22:17 -0700489 /* If device is found under parent, no need to allocate new device. */
490 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
491 if (new_d) {
492 alloc_bus(new_d);
493 return new_d;
494 }
495
496 new_d = alloc_dev(parent);
497
498 new_d->bustype = bustype;
499
500 new_d->path_a = path_a;
501 new_d->path_b = path_b;
502
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800503 new_d->enabled = status & 0x01;
504 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000505 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700506 new_d->chip_instance = chip_instance;
Furquan Shaikh27efc502018-06-22 09:19:15 -0700507 chip_instance->ref_count++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000508
Furquan Shaikh27efc502018-06-22 09:19:15 -0700509 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000510
Furquan Shaikha9b64292018-05-31 07:52:00 -0700511 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200512 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000513 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200514 break;
515
516 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000517 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200518 break;
519
520 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700521 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200522 break;
523
524 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000525 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200526 break;
527
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800528 case CPU_CLUSTER:
529 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200530 break;
531
Aaron Durbinffda804b2014-09-03 12:40:15 -0500532 case CPU:
533 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
534 break;
535
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800536 case DOMAIN:
537 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200538 break;
539
540 case IOAPIC:
541 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
542 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700543
544 case GENERIC:
545 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
546 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800547
548 case SPI:
549 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
550 break;
551
Duncan Lauriebae9f852018-05-07 14:18:13 -0700552 case USB:
553 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
554 break;
555
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800556 case MMIO:
557 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
558 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000559 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800560
Patrick Georgi114e7b22010-05-05 11:19:50 +0000561 return new_d;
562}
563
Furquan Shaikh27efc502018-06-22 09:19:15 -0700564static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600565{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700566 struct resource *r = S_ALLOC(sizeof(struct resource));
567
Patrick Georgi114e7b22010-05-05 11:19:50 +0000568 r->type = type;
569 r->index = index;
570 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000571 if (dev->res) {
572 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600573 while (head->next)
574 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000575 head->next = r;
576 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000577 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000578 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000579}
580
Furquan Shaikh27efc502018-06-22 09:19:15 -0700581void add_resource(struct bus *bus, int type, int index, int base)
582{
583 new_resource(bus->dev, type, index, base);
584}
585
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700586void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600587{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700588 struct reg *r = S_ALLOC(sizeof(struct reg));
589
Patrick Georgi114e7b22010-05-05 11:19:50 +0000590 r->key = name;
591 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700592 if (chip_instance->reg) {
593 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000594 // sorting to be equal to sconfig's behaviour
595 int sort = strcmp(r->key, head->key);
596 if (sort == 0) {
597 printf("ERROR: duplicate 'register' key.\n");
598 exit(1);
599 }
Martin Rothbec07532016-08-05 18:32:18 -0600600 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000601 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700602 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000603 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600604 while ((head->next)
605 && (strcmp(head->next->key, r->key) < 0))
606 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000607 r->next = head->next;
608 head->next = r;
609 }
610 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700611 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000612 }
613}
614
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200615void add_slot_desc(struct bus *bus, char *type, char *length, char *designation,
616 char *data_width)
617{
618 struct device *dev = bus->dev;
619
620 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
621 printf("ERROR: 'slot_type' only allowed for PCI devices\n");
622 exit(1);
623 }
624
625 dev->smbios_slot_type = type;
626 dev->smbios_slot_length = length;
627 dev->smbios_slot_data_width = data_width;
628 dev->smbios_slot_designation = designation;
629}
630
Furquan Shaikh93198262018-06-03 04:22:17 -0700631void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600632 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000633{
Furquan Shaikh93198262018-06-03 04:22:17 -0700634 struct device *dev = bus->dev;
635
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800636 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000637 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
638 exit(1);
639 }
640
641 dev->subsystem_vendor = vendor;
642 dev->subsystem_device = device;
643 dev->inherit_subsystem = inherit;
644}
645
Furquan Shaikh93198262018-06-03 04:22:17 -0700646void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600647 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200648{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200649 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700650 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200651
Martin Rothbec07532016-08-05 18:32:18 -0600652 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
653 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200654 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
655 exit(1);
656 }
657
658 srcpin = _srcpin[3] - 'A';
659
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800660 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200661 printf("ERROR: ioapic config only allowed for PCI devices\n");
662 exit(1);
663 }
664
665 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200666 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200667 exit(1);
668 }
669 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
670 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
671}
672
Furquan Shaikh93198262018-06-03 04:22:17 -0700673static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600674{
Furquan Shaikh93198262018-06-03 04:22:17 -0700675 struct bus *bus = dev->bus;
676
677 while (bus) {
678 if (bus->children)
679 return 1;
680 bus = bus->next_bus;
681 }
682
683 return 0;
684}
685
Nico Huber17e9bcb2019-09-20 12:05:51 +0200686static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Furquan Shaikh93198262018-06-03 04:22:17 -0700687{
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700688 static int dev_id;
689
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700690 if (ptr == &base_root_dev) {
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200691 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700692 ptr->name);
693 return;
694 }
695
Furquan Shaikh4ebe9532020-05-02 15:34:42 -0700696 char *name = S_ALLOC(10);
697 sprintf(name, "_dev%d", dev_id++);
698 ptr->name = name;
699
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200700 fprintf(fil, "STORAGE struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700701 if (ptr->res)
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200702 fprintf(fil, "STORAGE struct resource %s_res[];\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700703 ptr->name);
704 if (dev_has_children(ptr))
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200705 fprintf(fil, "STORAGE struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600706 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700707
Furquan Shaikh93198262018-06-03 04:22:17 -0700708 if (next)
709 return;
710
711 fprintf(fil,
712 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
713 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000714}
715
Furquan Shaikh93198262018-06-03 04:22:17 -0700716static void emit_resources(FILE *fil, struct device *ptr)
717{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700718 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700719 return;
720
721 int i = 1;
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200722 fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700723 struct resource *r = ptr->res;
724 while (r) {
725 fprintf(fil,
726 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
727 if (r->type == IRQ)
728 fprintf(fil, "IRQ");
729 if (r->type == DRQ)
730 fprintf(fil, "DRQ");
731 if (r->type == IO)
732 fprintf(fil, "IO");
733 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
734 r->base);
735 if (r->next)
736 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
737 i++);
738 else
739 fprintf(fil, ".next=NULL },\n");
740 r = r->next;
741 }
742
743 fprintf(fil, "\t };\n");
744}
745
746static void emit_bus(FILE *fil, struct bus *bus)
747{
748 fprintf(fil, "\t\t[%d] = {\n", bus->id);
749 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
750 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
751 if (bus->children)
752 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
753
754 if (bus->next_bus)
755 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
756 bus->id + 1);
757 else
758 fprintf(fil, "\t\t\t.next = NULL,\n");
759 fprintf(fil, "\t\t},\n");
760}
761
762static void emit_dev_links(FILE *fil, struct device *ptr)
763{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200764 fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
Furquan Shaikh93198262018-06-03 04:22:17 -0700765 ptr->name);
766
767 struct bus *bus = ptr->bus;
768
769 while (bus) {
770 emit_bus(fil, bus);
771 bus = bus->next_bus;
772 }
773
774 fprintf(fil, "\t};\n");
775}
776
Nico Huber17e9bcb2019-09-20 12:05:51 +0200777static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200778{
779 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700780 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700781 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700782
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200783 if (ptr == &base_root_dev)
784 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
785 else
786 fprintf(fil, "STORAGE struct device %s = {\n", ptr->name);
787
Furquan Shaikh93198262018-06-03 04:22:17 -0700788 fprintf(fil, "#if !DEVTREE_EARLY\n");
Furquan Shaikh50ddc0b2018-06-23 00:59:31 -0700789
790 /*
791 * ops field is set to default_dev_ops_root only for the root
792 * device. For all other devices, it is set by the driver at runtime.
793 */
794 if (ptr == &base_root_dev)
795 fprintf(fil, "\t.ops = &default_dev_ops_root,\n");
796 else
797 fprintf(fil, "\t.ops = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700798 fprintf(fil, "#endif\n");
799 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
800 ptr->parent->id);
801 fprintf(fil, "\t.path = {");
802 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
803 fprintf(fil, "},\n");
804 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800805 fprintf(fil, "\t.hidden = %d,\n", ptr->hidden);
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000806 fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory);
Furquan Shaikh93198262018-06-03 04:22:17 -0700807 fprintf(fil, "\t.on_mainboard = 1,\n");
808 if (ptr->subsystem_vendor > 0)
809 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
810 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000811
Furquan Shaikh93198262018-06-03 04:22:17 -0700812 if (ptr->subsystem_device > 0)
813 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
814 ptr->subsystem_device);
815
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700816 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700817 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600818 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700819 }
820 if (has_children)
821 fprintf(fil, "\t.link_list = &%s_links[0],\n",
822 ptr->name);
823 else
824 fprintf(fil, "\t.link_list = NULL,\n");
825 if (ptr->sibling)
826 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Aaron Durbind47afe92020-03-26 12:29:35 -0600827 else
828 fprintf(fil, "\t.sibling = NULL,\n");
Furquan Shaikh93198262018-06-03 04:22:17 -0700829 fprintf(fil, "#if !DEVTREE_EARLY\n");
Kyösti Mälkki1557a672019-06-30 10:51:31 +0300830 for (pin = 0; pin < 4; pin++) {
831 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
832 fprintf(fil,
833 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
834 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
835
836 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
837 fprintf(fil,
838 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
839 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
840 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700841 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
842 chip_ins->chip->name_underscore);
843 if (chip_ins == &mainboard_instance)
844 fprintf(fil, "\t.name = mainboard_name,\n");
845 fprintf(fil, "#endif\n");
846 if (chip_ins->chip->chiph_exists)
847 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
848 chip_ins->chip->name_underscore, chip_ins->id);
849 if (next)
Patrick Rudolphac24d3c2019-04-12 14:42:17 +0200850 fprintf(fil, "\t.next=&%s,\n", next->name);
851 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
852 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
853 fprintf(fil, "#if !DEVTREE_EARLY\n");
854 fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n");
855 }
856 /* SMBIOS types start at 1, if zero it hasn't been set */
857 if (ptr->smbios_slot_type)
858 fprintf(fil, "\t.smbios_slot_type = %s,\n",
859 ptr->smbios_slot_type);
860 if (ptr->smbios_slot_data_width)
861 fprintf(fil, "\t.smbios_slot_data_width = %s,\n",
862 ptr->smbios_slot_data_width);
863 if (ptr->smbios_slot_designation)
864 fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n",
865 ptr->smbios_slot_designation);
866 if (ptr->smbios_slot_length)
867 fprintf(fil, "\t.smbios_slot_length = %s,\n",
868 ptr->smbios_slot_length);
869 if (ptr->smbios_slot_type || ptr->smbios_slot_data_width ||
870 ptr->smbios_slot_designation || ptr->smbios_slot_length) {
871 fprintf(fil, "#endif\n");
872 fprintf(fil, "#endif\n");
873 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700874 fprintf(fil, "};\n");
875
876 emit_resources(fil, ptr);
877
878 if (has_children)
879 emit_dev_links(fil, ptr);
880}
881
Nico Huber17e9bcb2019-09-20 12:05:51 +0200882static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200883{
884 /* Only devices on root bus here. */
Nico Huber17e9bcb2019-09-20 12:05:51 +0200885 if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) {
886 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n",
887 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200888 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n",
889 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200890 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200891
Nico Huber17e9bcb2019-09-20 12:05:51 +0200892 if (ptr->bustype == PNP) {
893 fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n",
894 ptr->path_a, ptr->path_b);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200895 fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n",
896 ptr->path_a, ptr->path_b, ptr->name);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200897 }
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +0200898}
899
Furquan Shaikh93198262018-06-03 04:22:17 -0700900static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
901 struct device *d)
902{
903 while (d) {
904 enqueue_tail(bfs_q_head, d);
905 d = d->sibling;
906 }
907}
908
909static void add_children_to_queue(struct queue_entry **bfs_q_head,
910 struct device *d)
911{
912 struct bus *bus = d->bus;
913
914 while (bus) {
915 if (bus->children)
916 add_siblings_to_queue(bfs_q_head, bus->children);
917 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000918 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000919}
920
Nico Huber17e9bcb2019-09-20 12:05:51 +0200921static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
922 void (*func)(FILE *, FILE *, struct device *,
Furquan Shaikh93198262018-06-03 04:22:17 -0700923 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600924{
Furquan Shaikh93198262018-06-03 04:22:17 -0700925 struct queue_entry *bfs_q_head = NULL;
926
927 enqueue_tail(&bfs_q_head, ptr);
928
929 while ((ptr = dequeue_head(&bfs_q_head))) {
930 add_children_to_queue(&bfs_q_head, ptr);
Nico Huber17e9bcb2019-09-20 12:05:51 +0200931 func(fil, head, ptr, peek_queue_head(bfs_q_head));
Furquan Shaikh93198262018-06-03 04:22:17 -0700932 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000933}
934
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700935static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700936{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700937 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700938
939 fprintf(fil, "#include <device/device.h>\n");
940 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700941
942 while (chip) {
943 if (chip->chiph_exists)
944 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
945 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700946 }
947 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
948 fprintf(fil,
949 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700950
951 chip = tmp;
952 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700953 fprintf(fil,
954 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700955 chip->name_underscore);
956 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700957 }
958 fprintf(fil, "#endif\n");
959}
960
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700961static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
962{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200963 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700964 instance->chip->name_underscore,
965 instance->chip->name_underscore,
966 instance->id);
967
968 if (instance->reg) {
969 fprintf(fil, "\n");
970 struct reg *r = instance->reg;
971 while (r) {
972 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
973 r = r->next;
974 }
975 }
976 fprintf(fil, "};\n\n");
977}
978
Furquan Shaikh79e84122018-05-30 15:09:09 -0700979static void emit_chips(FILE *fil)
980{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700981 struct chip *chip = chip_header.next;
982 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700983 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700984
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700985 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700986
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200987 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
988
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700989 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700990 if (!chip->chiph_exists)
991 continue;
992
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700993 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700994 instance = chip->instance;
995 while (instance) {
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700996 instance->id = chip_id++;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700997 emit_chip_instance(fil, instance);
998 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700999 }
1000 }
1001}
1002
Nico Huber17e9bcb2019-09-20 12:05:51 +02001003static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -07001004 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +00001005{
1006 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +00001007
1008 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
1009 /* user already gave us a subsystem vendor/device */
1010 return;
1011 }
1012
Furquan Shaikh93198262018-06-03 04:22:17 -07001013 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +00001014
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001015 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +00001016 continue;
1017
1018 if (p->inherit_subsystem) {
1019 dev->subsystem_vendor = p->subsystem_vendor;
1020 dev->subsystem_device = p->subsystem_device;
1021 break;
1022 }
1023 }
1024}
1025
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001026static void usage(void)
1027{
Nico Huber17e9bcb2019-09-20 12:05:51 +02001028 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -06001029 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001030}
1031
Martin Roth32051702015-11-24 12:34:16 -07001032enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -06001033 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001034 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +02001035 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001036 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -06001037};
Martin Roth32051702015-11-24 12:34:16 -07001038
Nico Huber17e9bcb2019-09-20 12:05:51 +02001039#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001040#define OPTIONAL_ARG_COUNT 1
1041#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001042
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001043static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001044{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001045 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001046 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001047 perror(NULL);
1048 exit(1);
1049 }
1050
Patrick Georgi114e7b22010-05-05 11:19:50 +00001051 yyrestart(filec);
1052
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001053 root_parent = parent;
1054 linenum = 0;
1055
Patrick Georgi114e7b22010-05-05 11:19:50 +00001056 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001057
Patrick Georgi114e7b22010-05-05 11:19:50 +00001058 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001059}
1060
Furquan Shaikh27efc502018-06-22 09:19:15 -07001061/*
1062 * Match device nodes from base and override tree to see if they are the same
1063 * node.
1064 */
1065static int device_match(struct device *a, struct device *b)
1066{
1067 return ((a->path_a == b->path_a) &&
1068 (a->path_b == b->path_b) &&
1069 (a->bustype == b->bustype) &&
1070 (a->chip_instance->chip ==
1071 b->chip_instance->chip));
1072}
1073
1074/*
Bill XIEc61d4152019-11-21 18:16:12 +08001075 * Match resource nodes from base and override tree to see if they are the same
1076 * node.
1077 */
1078static int res_match(struct resource *a, struct resource *b)
1079{
1080 return ((a->type == b->type) &&
1081 (a->index == b->index));
1082}
1083
1084/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001085 * Walk through the override subtree in breadth-first manner starting at node to
1086 * see if chip_instance pointer of the node is same as chip_instance pointer of
1087 * override parent that is passed into the function. If yes, then update the
1088 * chip_instance pointer of the node to chip_instance pointer of the base
1089 * parent.
1090 */
1091static void update_chip_pointers(struct device *node,
1092 struct chip_instance *base_parent_ci,
1093 struct chip_instance *override_parent_ci)
1094{
1095 struct queue_entry *q_head = NULL;
1096
1097 enqueue_tail(&q_head, node);
1098
1099 while ((node = dequeue_head(&q_head))) {
1100 if (node->chip_instance != override_parent_ci)
1101 continue;
1102 node->chip_instance = base_parent_ci;
1103 add_children_to_queue(&q_head, node);
1104 }
1105}
1106
1107/*
1108 * Add resource to device. If resource is already present, then update its base
1109 * and index. If not, then add a new resource to the device.
1110 */
1111static void update_resource(struct device *dev, struct resource *res)
1112{
1113 struct resource *base_res = dev->res;
1114
1115 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001116 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001117 base_res->base = res->base;
1118 return;
1119 }
1120 base_res = base_res->next;
1121 }
1122
1123 new_resource(dev, res->type, res->index, res->base);
1124}
1125
1126/*
1127 * Add register to chip instance. If register is already present, then update
1128 * its value. If not, then add a new register to the chip instance.
1129 */
1130static void update_register(struct chip_instance *c, struct reg *reg)
1131{
1132 struct reg *base_reg = c->reg;
1133
1134 while (base_reg) {
1135 if (!strcmp(base_reg->key, reg->key)) {
1136 base_reg->value = reg->value;
1137 return;
1138 }
1139 base_reg = base_reg->next;
1140 }
1141
1142 add_register(c, reg->key, reg->value);
1143}
1144
1145static void override_devicetree(struct bus *base_parent,
1146 struct bus *override_parent);
1147
1148/*
1149 * Update the base device properties using the properties of override device. In
1150 * addition to that, call override_devicetree for all the buses under the
1151 * override device.
1152 *
1153 * Override Rules:
1154 * +--------------------+--------------------------------------------+
1155 * | | |
1156 * |struct device member| Rule |
1157 * | | |
1158 * +-----------------------------------------------------------------+
1159 * | | |
1160 * | id | Unchanged. This is used to generate device |
1161 * | | structure name in static.c. So, no need to |
1162 * | | override. |
1163 * | | |
1164 * +-----------------------------------------------------------------+
1165 * | | |
1166 * | enabled | Copy enabled state from override device. |
1167 * | | This allows variants to override device |
1168 * | | state. |
1169 * | | |
1170 * +-----------------------------------------------------------------+
1171 * | | |
1172 * | subsystem_vendor | Copy from override device only if any one |
1173 * | subsystem_device | of the ids is non-zero. |
1174 * | | |
1175 * +-----------------------------------------------------------------+
1176 * | | |
1177 * | inherit_subsystem | Copy from override device only if it is |
1178 * | | non-zero. This allows variant to only |
1179 * | | enable inherit flag for a device. |
1180 * | | |
1181 * +-----------------------------------------------------------------+
1182 * | | |
1183 * | path | Unchanged since these are same for both |
1184 * | path_a | base and override device (Used for |
1185 * | path_b | matching devices). |
1186 * | | |
1187 * +-----------------------------------------------------------------+
1188 * | | |
1189 * | bustype | Unchanged since this is same for both base |
1190 * | | and override device (User for matching |
1191 * | | devices). |
1192 * | | |
1193 * +-----------------------------------------------------------------+
1194 * | | |
1195 * | pci_irq_info | Unchanged. |
1196 * | | |
1197 * +-----------------------------------------------------------------+
1198 * | | |
1199 * | parent | Unchanged. This is meaningful only within |
1200 * | sibling | the parse tree, hence not being copied. |
1201 * | | |
1202 * +-----------------------------------------------------------------+
1203 * | | |
1204 * | res | Each resource that is present in override |
1205 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001206 * | | 1. If resource of same type and index is |
1207 * | | present in base device, then base of |
1208 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001209 * | | 2. If not, then a new resource is allocated|
1210 * | | under the base device using type, index |
1211 * | | and base from override res. |
1212 * | | |
1213 * +-----------------------------------------------------------------+
1214 * | | |
1215 * | chip_instance | Each register of chip_instance is copied |
1216 * | | over from override device to base device: |
1217 * | | 1. If register with same key is present in |
1218 * | | base device, then value of the register |
1219 * | | is copied. |
1220 * | | 2. If not, then a new register is allocated|
1221 * | | under the base chip_instance using key |
1222 * | | and value from override register. |
1223 * | | |
1224 * +-----------------------------------------------------------------+
1225 * | | |
1226 * | bus | Recursively call override_devicetree on |
1227 * | last_bus | each bus of override device. It is assumed |
1228 * | | that bus with id X under base device |
1229 * | | to bus with id X under override device. If |
1230 * | | override device has more buses than base |
1231 * | | device, then new buses are allocated under |
1232 * | | base device. |
1233 * | | |
1234 * +-----------------------------------------------------------------+
1235 */
1236static void update_device(struct device *base_dev, struct device *override_dev)
1237{
1238 /*
1239 * Copy the enabled state of override device to base device. This allows
1240 * override tree to enable or disable a particular device.
1241 */
1242 base_dev->enabled = override_dev->enabled;
1243
1244 /*
1245 * Copy subsystem vendor and device ids from override device to base
1246 * device only if the ids are non-zero in override device. Else, honor
1247 * the values in base device.
1248 */
1249 if (override_dev->subsystem_vendor ||
1250 override_dev->subsystem_device) {
1251 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1252 base_dev->subsystem_device = override_dev->subsystem_device;
1253 }
1254
1255 /*
1256 * Copy value of inherity_subsystem from override device to base device
1257 * only if it is non-zero in override device. This allows override
1258 * tree to only enable inhert flag for a device.
1259 */
1260 if (override_dev->inherit_subsystem)
1261 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1262
1263 /*
1264 * Copy resources of override device to base device.
1265 * 1. If resource is already present in base device, then index and base
1266 * of the resource will be copied over.
1267 * 2. If resource is not already present in base device, a new resource
1268 * will be allocated.
1269 */
1270 struct resource *res = override_dev->res;
1271 while (res) {
1272 update_resource(base_dev, res);
1273 res = res->next;
1274 }
1275
1276 /*
1277 * Copy registers of override chip instance to base chip instance.
1278 * 1. If register key is already present in base chip instance, then
1279 * value for the register is copied over.
1280 * 2. If register key is not already present in base chip instance, then
1281 * a new register will be allocated.
1282 */
1283 struct reg *reg = override_dev->chip_instance->reg;
1284 while (reg) {
1285 update_register(base_dev->chip_instance, reg);
1286 reg = reg->next;
1287 }
1288
1289 /*
1290 * Now that the device properties are all copied over, look at each bus
1291 * of the override device and run override_devicetree in a recursive
1292 * manner. The assumption here is that first bus of override device
1293 * corresponds to first bus of base device and so on. If base device has
1294 * lesser buses than override tree, then new buses are allocated for it.
1295 */
1296 struct bus *override_bus = override_dev->bus;
1297 struct bus *base_bus = base_dev->bus;
1298
1299 while (override_bus) {
1300
1301 /*
1302 * If we have more buses in override tree device, then allocate
1303 * a new bus for the base tree device as well.
1304 */
1305 if (!base_bus) {
1306 alloc_bus(base_dev);
1307 base_bus = base_dev->last_bus;
1308 }
1309
1310 override_devicetree(base_dev->bus, override_dev->bus);
1311
1312 override_bus = override_bus->next_bus;
1313 base_bus = base_bus->next_bus;
1314 }
1315
1316 delete_chip_instance(override_dev->chip_instance);
1317 override_dev->chip_instance = NULL;
1318}
1319
1320/*
1321 * Perform copy of device and properties from override parent to base parent.
1322 * This function walks through the override tree in a depth-first manner
1323 * performing following actions:
1324 * 1. If matching device is found in base tree, then copy the properties of
1325 * override device to base tree device. Call override_devicetree recursively on
1326 * the bus of override device.
1327 * 2. If matching device is not found in base tree, then set override tree
1328 * device as new child of base_parent and update the chip pointers in override
1329 * device subtree to ensure the nodes do not point to override tree chip
1330 * instance.
1331 */
1332static void override_devicetree(struct bus *base_parent,
1333 struct bus *override_parent)
1334{
1335 struct device *base_child;
1336 struct device *override_child = override_parent->children;
1337 struct device *next_child;
1338
1339 while (override_child) {
1340
1341 /* Look for a matching device in base tree. */
1342 for (base_child = base_parent->children;
1343 base_child; base_child = base_child->sibling) {
1344 if (device_match(base_child, override_child))
1345 break;
1346 }
1347
1348 next_child = override_child->sibling;
1349
1350 /*
1351 * If matching device is found, copy properties of
1352 * override_child to base_child.
1353 */
1354 if (base_child)
1355 update_device(base_child, override_child);
1356 else {
1357 /*
1358 * If matching device is not found, set override_child
1359 * as a new child of base_parent.
1360 */
1361 set_new_child(base_parent, override_child);
1362 /*
1363 * Ensure all nodes in override tree pointing to
1364 * override parent chip_instance now point to base
1365 * parent chip_instance.
1366 */
1367 update_chip_pointers(override_child,
1368 base_parent->dev->chip_instance,
1369 override_parent->dev->chip_instance);
1370 }
1371
1372 override_child = next_child;
1373 }
1374}
1375
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001376int main(int argc, char **argv)
1377{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001378 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001379 usage();
1380
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001381 const char *base_devtree = argv[DEVICEFILE_ARG];
1382 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001383 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001384 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001385
1386 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001387
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001388 if (argc == TOTAL_ARG_COUNT) {
1389 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1390 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001391
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001392 if (!dev_has_children(&override_root_dev)) {
1393 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1394 exit(1);
1395 }
1396
Furquan Shaikh27efc502018-06-22 09:19:15 -07001397 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001398 }
1399
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001400 FILE *autogen = fopen(outputc, "w");
1401 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001402 fprintf(stderr, "Could not open file '%s' for writing: ",
1403 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001404 perror(NULL);
1405 exit(1);
1406 }
1407
Nico Huber17e9bcb2019-09-20 12:05:51 +02001408 FILE *autohead = fopen(outputh, "w");
1409 if (!autohead) {
1410 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1411 perror(NULL);
1412 fclose(autogen);
1413 exit(1);
1414 }
1415 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1416 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1417 fprintf(autohead, "#include <device/device.h>\n\n");
1418
Furquan Shaikh79e84122018-05-30 15:09:09 -07001419 emit_chips(autogen);
1420
Nico Huber17e9bcb2019-09-20 12:05:51 +02001421 walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001422 fprintf(autogen, "\n/* pass 0 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001423 walk_device_tree(autogen, autohead, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001424 fprintf(autogen, "\n/* pass 1 */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001425 walk_device_tree(autogen, autohead, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001426
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001427 /* Expose static devicenames to global namespace. */
1428 fprintf(autogen, "\n/* expose_device_names */\n");
Nico Huber17e9bcb2019-09-20 12:05:51 +02001429 walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names);
Kyösti Mälkki5e2a2cd2019-03-15 17:44:23 +02001430
Nico Huber17e9bcb2019-09-20 12:05:51 +02001431 fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n");
1432 fclose(autohead);
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001433 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001434
Patrick Georgi114e7b22010-05-05 11:19:50 +00001435 return 0;
1436}