blob: 036c59ce570374bd14d2dce64c40bb937cbb9407 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* sconfig, coreboot device tree compiler */
Patrick Georgi114e7b22010-05-05 11:19:50 +00002/*
Patrick Georgi114e7b22010-05-05 11:19:50 +00003 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
Patrick Georgi114e7b22010-05-05 11:19:50 +000011 */
12
Patrick Georgi2dbfcb72012-05-30 16:26:30 +020013#include <ctype.h>
Werner Zehac14a402019-07-11 12:47:19 +020014/* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/
15#include <sys/stat.h>
Kyösti Mälkkie29a6ac2019-03-15 17:05:19 +020016#include <commonlib/helpers.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000017#include "sconfig.h"
18#include "sconfig.tab.h"
19
Patrick Georgi7fc9e292010-07-15 15:59:07 +000020extern int linenum;
21
Furquan Shaikh27efc502018-06-22 09:19:15 -070022/*
23 * Maintains list of all the unique chip structures for the board.
24 * This is shared across base and override device trees since we need to
25 * generate headers for all chips added by both the trees.
26 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070027static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070028
Kyösti Mälkki472d9022011-12-05 20:33:55 +020029typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020030 UNSLASH,
31 SPLIT_1ST,
32 TO_LOWER,
33 TO_UPPER,
34} translate_t;
35
Furquan Shaikh93198262018-06-03 04:22:17 -070036/*
37 * Mainboard is assumed to have a root device whose bus is the parent of all the
38 * devices that are added by parsing the devicetree file. This device has a
39 * mainboard chip instance associated with it.
40 *
41 *
42 *
43 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070044 * | Root device | | Mainboard |
45 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070046 * | | | chip_instance | (mainboard_instance)|
47 * | +------------------------+ | |
48 * | | +----------------------+
49 * | | bus |
50 * | parent v |
51 * | +-------------------+ |
52 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070053 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070054 * | | |
55 * +-------------------+ |
56 * | |
57 * | children | chip
58 * v |
59 * X |
60 * (new devices will |
61 * be added here as |
62 * children) |
63 * |
64 * |
65 * |
66 * +-------+----------+
67 * | |
68 * | Mainboard chip +----------->X (new chips will be
69 * | (mainboard_chip) | added here)
70 * | |
71 * +------------------+
72 *
73 *
74 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070075
76/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070077static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070078
79/* Root device of override tree (if applicable). */
80static struct device override_root_dev;
81
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070082static struct chip_instance mainboard_instance;
83
Furquan Shaikhde39fc72018-06-11 04:26:45 -070084static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070085 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070086 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070087};
88
Furquan Shaikhde39fc72018-06-11 04:26:45 -070089static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070090 .name = "dev_root",
Furquan Shaikh93198262018-06-03 04:22:17 -070091 .chip_instance = &mainboard_instance,
92 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikhde39fc72018-06-11 04:26:45 -070093 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070094 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070095 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -070096};
97
Furquan Shaikh39ac7972018-06-21 18:44:32 -070098static struct bus override_root_bus = {
99 .id = 0,
100 .dev = &override_root_dev,
101};
102
103static struct device override_root_dev = {
104 .name = "override_root",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700105 /*
106 * Override tree root device points to the same mainboard chip instance
107 * as the base tree root device. It should not cause any side-effects
108 * since the mainboard chip instance pointer in override tree will just
109 * be ignored.
110 */
111 .chip_instance = &mainboard_instance,
112 .path = " .type = DEVICE_PATH_ROOT ",
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700113 .parent = &override_root_bus,
114 .enabled = 1,
115 .bus = &override_root_bus,
116};
117
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700118static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000119 .name = "mainboard",
120 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700121 .instance = &mainboard_instance,
122};
123
124static struct chip_instance mainboard_instance = {
125 .id = 0,
126 .chip = &mainboard_chip,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000127};
128
Furquan Shaikh93198262018-06-03 04:22:17 -0700129/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700130struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000131
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700132struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700133 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700134 struct queue_entry *next;
135 struct queue_entry *prev;
136};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700137
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700138#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700139
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700140static void *s_alloc(const char *f, size_t s)
141{
142 void *data = calloc(1, s);
143 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530144 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700145 exit(1);
146 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700147 return data;
148}
149
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700150static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700151{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700152 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700153
154 e->data = data;
155 e->next = e->prev = e;
156 return e;
157}
158
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700159static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700160{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700161 struct queue_entry *tmp = new_queue_entry(data);
162 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700163
164 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700165 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700166 return;
167 }
168
169 q->prev->next = tmp;
170 tmp->prev = q->prev;
171 q->prev = tmp;
172 tmp->next = q;
173}
174
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700175static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700176{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700177 struct queue_entry *q = *q_head;
178 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700179 void *data;
180
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700181 if (!q)
182 return NULL;
183
184 tmp = q->prev;
185
Furquan Shaikh79e84122018-05-30 15:09:09 -0700186 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700187 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700188 else {
189 tmp->prev->next = q;
190 q->prev = tmp->prev;
191 }
192
193 data = tmp->data;
194 free(tmp);
195
196 return data;
197}
198
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700199static void *dequeue_head(struct queue_entry **q_head)
200{
201 struct queue_entry *q = *q_head;
202 struct queue_entry *tmp = q;
203 void *data;
204
205 if (!q)
206 return NULL;
207
208 if (q->next == q)
209 *q_head = NULL;
210 else {
211 q->next->prev = q->prev;
212 q->prev->next = q->next;
213 *q_head = q->next;
214 }
215
216 data = tmp->data;
217 free(tmp);
218
219 return data;
220}
221
222static void *peek_queue_head(struct queue_entry *q_head)
223{
224 if (!q_head)
225 return NULL;
226
227 return q_head->data;
228}
229
230static struct queue_entry *chip_q_head;
231
232void chip_enqueue_tail(void *data)
233{
234 enqueue_tail(&chip_q_head, data);
235}
236
237void *chip_dequeue_tail(void)
238{
239 return dequeue_tail(&chip_q_head);
240}
241
Martin Rothbec07532016-08-05 18:32:18 -0600242int yywrap(void)
243{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000244 return 1;
245}
246
Martin Rothbec07532016-08-05 18:32:18 -0600247void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000248{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000249 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600250 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000251 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000252}
253
Martin Rothbec07532016-08-05 18:32:18 -0600254char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200255{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200256 char *b, *c;
257 b = c = strdup(str);
258 while (c && *c) {
259 if ((mode == SPLIT_1ST) && (*c == '/')) {
260 *c = 0;
261 break;
262 }
Martin Rothbec07532016-08-05 18:32:18 -0600263 if (*c == '/')
264 *c = '_';
265 if (*c == '-')
266 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200267 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200268 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200269 if (mode == TO_LOWER)
270 *c = tolower(*c);
271 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200272 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200273 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200274}
275
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700276static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600277{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700278 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700279
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700280 while (h->next) {
281 int result = strcmp(path, h->next->name);
282 if (result == 0)
283 return h->next;
284
285 if (result < 0)
286 break;
287
288 h = h->next;
289 }
290
291 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
292 new_chip->next = h->next;
293 h->next = new_chip;
294
Patrick Georgi114e7b22010-05-05 11:19:50 +0000295 new_chip->chiph_exists = 1;
296 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700297 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000298
299 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700300 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700301 sprintf(chip_h, "src/%s", path);
302 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100303 /* root_complex gets away without a separate directory, but
304 * exists on on pretty much all AMD chipsets.
305 */
306 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300307 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
308 path);
309 exit(1);
310 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700311 }
312
Martin Roth824255e2016-08-05 17:40:39 -0600313 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200314
Martin Roth824255e2016-08-05 17:40:39 -0600315 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600316 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000317
Patrick Georgi1f688802014-08-03 15:51:19 +0200318 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700319
Patrick Georgi114e7b22010-05-05 11:19:50 +0000320 return new_chip;
321}
322
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700323struct chip_instance *new_chip_instance(char *path)
324{
325 struct chip *chip = get_chip(path);
326 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
327
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700328 instance->chip = chip;
329 instance->next = chip->instance;
330 chip->instance = instance;
331
332 return instance;
333}
334
Furquan Shaikh93198262018-06-03 04:22:17 -0700335/*
336 * Allocate a new bus for the provided device.
337 * - If this is the first bus being allocated under this device, then its id
338 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
339 * - If this is not the first bus under this device, then its id is set to 1
340 * plus the id of last bus and newly allocated bus is added to the list of
341 * buses under the device. last_bus is updated to point to the newly
342 * allocated bus.
343 */
344static void alloc_bus(struct device *dev)
345{
346 struct bus *bus = S_ALLOC(sizeof(*bus));
347
348 bus->dev = dev;
349
350 if (dev->last_bus == NULL) {
351 bus->id = 0;
352 dev->bus = bus;
353 } else {
354 bus->id = dev->last_bus->id + 1;
355 dev->last_bus->next_bus = bus;
356 }
357
358 dev->last_bus = bus;
359}
360
361/*
362 * Allocate a new device under the given parent. This function allocates a new
363 * device structure under the provided parent bus and allocates a bus structure
364 * under the newly allocated device.
365 */
366static struct device *alloc_dev(struct bus *parent)
367{
368 struct device *dev = S_ALLOC(sizeof(*dev));
369
Furquan Shaikh93198262018-06-03 04:22:17 -0700370 dev->parent = parent;
371 dev->subsystem_vendor = -1;
372 dev->subsystem_device = -1;
373
374 alloc_bus(dev);
375
376 return dev;
377}
378
379/*
380 * This function scans the children of given bus to see if any device matches
381 * the new device that is requested.
382 *
383 * Returns pointer to the node if found, else NULL.
384 */
385static struct device *get_dev(struct bus *parent, int path_a, int path_b,
386 int bustype, struct chip_instance *chip_instance)
387{
388 struct device *child = parent->children;
389
390 while (child) {
391 if ((child->path_a == path_a) && (child->path_b == path_b) &&
392 (child->bustype == bustype) &&
393 (child->chip_instance == chip_instance))
394 return child;
395
396 child = child->sibling;
397 }
398
399 return NULL;
400}
401
Furquan Shaikh27efc502018-06-22 09:19:15 -0700402/*
403 * Add given node as child of the provided parent. If this is the first child of
404 * the parent, update parent->children pointer as well.
405 */
406static void set_new_child(struct bus *parent, struct device *child)
407{
408 struct device *c = parent->children;
409 if (c) {
410 while (c->sibling)
411 c = c->sibling;
412 c->sibling = child;
413 } else
414 parent->children = child;
415
416 child->sibling = NULL;
417 child->parent = parent;
418}
419
Furquan Shaikh93198262018-06-03 04:22:17 -0700420struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700421 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700422 const int bustype, const char *devnum,
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800423 int status)
Martin Rothbec07532016-08-05 18:32:18 -0600424{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000425 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700426 int path_a;
427 int path_b = 0;
428 struct device *new_d;
429
430 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000431 if (*tmp == '.') {
432 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700433 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000434 }
435
Furquan Shaikh93198262018-06-03 04:22:17 -0700436 /* If device is found under parent, no need to allocate new device. */
437 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
438 if (new_d) {
439 alloc_bus(new_d);
440 return new_d;
441 }
442
443 new_d = alloc_dev(parent);
444
445 new_d->bustype = bustype;
446
447 new_d->path_a = path_a;
448 new_d->path_b = path_b;
449
Hung-Te Lin936dbe12018-09-10 10:51:26 +0800450 new_d->enabled = status & 0x01;
451 new_d->hidden = (status >> 1) & 0x01;
Ronald G. Minnich466ca2c2019-10-22 02:02:24 +0000452 new_d->mandatory = (status >> 2) & 0x01;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700453 new_d->chip_instance = chip_instance;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000454
Furquan Shaikh27efc502018-06-22 09:19:15 -0700455 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000456
Furquan Shaikha9b64292018-05-31 07:52:00 -0700457 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200458 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000459 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200460 break;
461
462 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000463 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200464 break;
465
466 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700467 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200468 break;
469
470 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000471 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200472 break;
473
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800474 case CPU_CLUSTER:
475 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200476 break;
477
Aaron Durbinffda804b2014-09-03 12:40:15 -0500478 case CPU:
479 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
480 break;
481
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800482 case DOMAIN:
483 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200484 break;
485
486 case IOAPIC:
487 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
488 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700489
490 case GENERIC:
491 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
492 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800493
494 case SPI:
495 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
496 break;
497
Duncan Lauriebae9f852018-05-07 14:18:13 -0700498 case USB:
499 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
500 break;
501
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800502 case MMIO:
503 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
504 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000505 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800506
Patrick Georgi114e7b22010-05-05 11:19:50 +0000507 return new_d;
508}
509
Furquan Shaikh27efc502018-06-22 09:19:15 -0700510static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600511{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700512 struct resource *r = S_ALLOC(sizeof(struct resource));
513
Patrick Georgi114e7b22010-05-05 11:19:50 +0000514 r->type = type;
515 r->index = index;
516 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000517 if (dev->res) {
518 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600519 while (head->next)
520 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000521 head->next = r;
522 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000523 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000524 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000525}
526
Furquan Shaikh27efc502018-06-22 09:19:15 -0700527void add_resource(struct bus *bus, int type, int index, int base)
528{
529 new_resource(bus->dev, type, index, base);
530}
531
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700532void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600533{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700534 struct reg *r = S_ALLOC(sizeof(struct reg));
535
Patrick Georgi114e7b22010-05-05 11:19:50 +0000536 r->key = name;
537 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700538 if (chip_instance->reg) {
539 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000540 // sorting to be equal to sconfig's behaviour
541 int sort = strcmp(r->key, head->key);
542 if (sort == 0) {
543 printf("ERROR: duplicate 'register' key.\n");
544 exit(1);
545 }
Martin Rothbec07532016-08-05 18:32:18 -0600546 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000547 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700548 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000549 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600550 while ((head->next)
551 && (strcmp(head->next->key, r->key) < 0))
552 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000553 r->next = head->next;
554 head->next = r;
555 }
556 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700557 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000558 }
559}
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
892 fprintf(fil, "#include <device/device.h>\n");
893 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700894
895 while (chip) {
896 if (chip->chiph_exists)
897 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
898 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700899 }
900 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
901 fprintf(fil,
902 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700903
904 chip = tmp;
905 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700906 fprintf(fil,
907 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700908 chip->name_underscore);
909 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700910 }
911 fprintf(fil, "#endif\n");
912}
913
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700914static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
915{
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200916 fprintf(fil, "STORAGE struct %s_config %s_info_%d = {",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700917 instance->chip->name_underscore,
918 instance->chip->name_underscore,
919 instance->id);
920
921 if (instance->reg) {
922 fprintf(fil, "\n");
923 struct reg *r = instance->reg;
924 while (r) {
925 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
926 r = r->next;
927 }
928 }
929 fprintf(fil, "};\n\n");
930}
931
Furquan Shaikh79e84122018-05-30 15:09:09 -0700932static void emit_chips(FILE *fil)
933{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700934 struct chip *chip = chip_header.next;
935 struct chip_instance *instance;
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700936 int chip_id;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700937
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700938 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700939
Kyösti Mälkki5ccce7c2019-03-15 10:04:11 +0200940 fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
941
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700942 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700943 if (!chip->chiph_exists)
944 continue;
945
Furquan Shaikh9f681d22020-05-02 15:51:02 -0700946 chip_id = 1;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700947 instance = chip->instance;
948 while (instance) {
Furquan Shaikhbbade242020-05-02 16:05:29 -0700949 /*
950 * Emit this chip instance only if there is no forwarding pointer to the
951 * base tree chip instance.
952 */
953 if (instance->base_chip_instance == NULL) {
954 instance->id = chip_id++;
955 emit_chip_instance(fil, instance);
956 }
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700957 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700958 }
959 }
960}
961
Nico Huber17e9bcb2019-09-20 12:05:51 +0200962static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev,
Furquan Shaikh93198262018-06-03 04:22:17 -0700963 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +0000964{
965 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000966
967 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
968 /* user already gave us a subsystem vendor/device */
969 return;
970 }
971
Furquan Shaikh93198262018-06-03 04:22:17 -0700972 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000973
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800974 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000975 continue;
976
977 if (p->inherit_subsystem) {
978 dev->subsystem_vendor = p->subsystem_vendor;
979 dev->subsystem_device = p->subsystem_device;
980 break;
981 }
982 }
983}
984
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200985static void usage(void)
986{
Nico Huber17e9bcb2019-09-20 12:05:51 +0200987 printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -0600988 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200989}
990
Martin Roth32051702015-11-24 12:34:16 -0700991enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -0600992 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700993 OUTPUTFILE_ARG,
Nico Huber17e9bcb2019-09-20 12:05:51 +0200994 HEADERFILE_ARG,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700995 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -0600996};
Martin Roth32051702015-11-24 12:34:16 -0700997
Nico Huber17e9bcb2019-09-20 12:05:51 +0200998#define MANDATORY_ARG_COUNT 4
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700999#define OPTIONAL_ARG_COUNT 1
1000#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001001
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001002static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -06001003{
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001004 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001005 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001006 perror(NULL);
1007 exit(1);
1008 }
1009
Patrick Georgi114e7b22010-05-05 11:19:50 +00001010 yyrestart(filec);
1011
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001012 root_parent = parent;
1013 linenum = 0;
1014
Patrick Georgi114e7b22010-05-05 11:19:50 +00001015 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001016
Patrick Georgi114e7b22010-05-05 11:19:50 +00001017 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001018}
1019
Furquan Shaikh27efc502018-06-22 09:19:15 -07001020/*
1021 * Match device nodes from base and override tree to see if they are the same
1022 * node.
1023 */
1024static int device_match(struct device *a, struct device *b)
1025{
1026 return ((a->path_a == b->path_a) &&
1027 (a->path_b == b->path_b) &&
1028 (a->bustype == b->bustype) &&
1029 (a->chip_instance->chip ==
1030 b->chip_instance->chip));
1031}
1032
1033/*
Bill XIEc61d4152019-11-21 18:16:12 +08001034 * Match resource nodes from base and override tree to see if they are the same
1035 * node.
1036 */
1037static int res_match(struct resource *a, struct resource *b)
1038{
1039 return ((a->type == b->type) &&
1040 (a->index == b->index));
1041}
1042
1043/*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001044 * Add resource to device. If resource is already present, then update its base
1045 * and index. If not, then add a new resource to the device.
1046 */
1047static void update_resource(struct device *dev, struct resource *res)
1048{
1049 struct resource *base_res = dev->res;
1050
1051 while (base_res) {
Bill XIEc61d4152019-11-21 18:16:12 +08001052 if (res_match(base_res, res)) {
Furquan Shaikh27efc502018-06-22 09:19:15 -07001053 base_res->base = res->base;
1054 return;
1055 }
1056 base_res = base_res->next;
1057 }
1058
1059 new_resource(dev, res->type, res->index, res->base);
1060}
1061
1062/*
1063 * Add register to chip instance. If register is already present, then update
1064 * its value. If not, then add a new register to the chip instance.
1065 */
1066static void update_register(struct chip_instance *c, struct reg *reg)
1067{
1068 struct reg *base_reg = c->reg;
1069
1070 while (base_reg) {
1071 if (!strcmp(base_reg->key, reg->key)) {
1072 base_reg->value = reg->value;
1073 return;
1074 }
1075 base_reg = base_reg->next;
1076 }
1077
1078 add_register(c, reg->key, reg->value);
1079}
1080
1081static void override_devicetree(struct bus *base_parent,
1082 struct bus *override_parent);
1083
1084/*
1085 * Update the base device properties using the properties of override device. In
1086 * addition to that, call override_devicetree for all the buses under the
1087 * override device.
1088 *
1089 * Override Rules:
1090 * +--------------------+--------------------------------------------+
1091 * | | |
1092 * |struct device member| Rule |
1093 * | | |
1094 * +-----------------------------------------------------------------+
1095 * | | |
1096 * | id | Unchanged. This is used to generate device |
1097 * | | structure name in static.c. So, no need to |
1098 * | | override. |
1099 * | | |
1100 * +-----------------------------------------------------------------+
1101 * | | |
1102 * | enabled | Copy enabled state from override device. |
1103 * | | This allows variants to override device |
1104 * | | state. |
1105 * | | |
1106 * +-----------------------------------------------------------------+
1107 * | | |
1108 * | subsystem_vendor | Copy from override device only if any one |
1109 * | subsystem_device | of the ids is non-zero. |
1110 * | | |
1111 * +-----------------------------------------------------------------+
1112 * | | |
1113 * | inherit_subsystem | Copy from override device only if it is |
1114 * | | non-zero. This allows variant to only |
1115 * | | enable inherit flag for a device. |
1116 * | | |
1117 * +-----------------------------------------------------------------+
1118 * | | |
1119 * | path | Unchanged since these are same for both |
1120 * | path_a | base and override device (Used for |
1121 * | path_b | matching devices). |
1122 * | | |
1123 * +-----------------------------------------------------------------+
1124 * | | |
1125 * | bustype | Unchanged since this is same for both base |
1126 * | | and override device (User for matching |
1127 * | | devices). |
1128 * | | |
1129 * +-----------------------------------------------------------------+
1130 * | | |
1131 * | pci_irq_info | Unchanged. |
1132 * | | |
1133 * +-----------------------------------------------------------------+
1134 * | | |
1135 * | parent | Unchanged. This is meaningful only within |
1136 * | sibling | the parse tree, hence not being copied. |
1137 * | | |
1138 * +-----------------------------------------------------------------+
1139 * | | |
1140 * | res | Each resource that is present in override |
1141 * | | device is copied over to base device: |
Bill XIEc61d4152019-11-21 18:16:12 +08001142 * | | 1. If resource of same type and index is |
1143 * | | present in base device, then base of |
1144 * | | the resource is copied. |
Furquan Shaikh27efc502018-06-22 09:19:15 -07001145 * | | 2. If not, then a new resource is allocated|
1146 * | | under the base device using type, index |
1147 * | | and base from override res. |
1148 * | | |
1149 * +-----------------------------------------------------------------+
1150 * | | |
1151 * | chip_instance | Each register of chip_instance is copied |
1152 * | | over from override device to base device: |
1153 * | | 1. If register with same key is present in |
1154 * | | base device, then value of the register |
1155 * | | is copied. |
1156 * | | 2. If not, then a new register is allocated|
1157 * | | under the base chip_instance using key |
1158 * | | and value from override register. |
1159 * | | |
1160 * +-----------------------------------------------------------------+
1161 * | | |
1162 * | bus | Recursively call override_devicetree on |
1163 * | last_bus | each bus of override device. It is assumed |
1164 * | | that bus with id X under base device |
1165 * | | to bus with id X under override device. If |
1166 * | | override device has more buses than base |
1167 * | | device, then new buses are allocated under |
1168 * | | base device. |
1169 * | | |
1170 * +-----------------------------------------------------------------+
1171 */
1172static void update_device(struct device *base_dev, struct device *override_dev)
1173{
1174 /*
1175 * Copy the enabled state of override device to base device. This allows
1176 * override tree to enable or disable a particular device.
1177 */
1178 base_dev->enabled = override_dev->enabled;
1179
1180 /*
1181 * Copy subsystem vendor and device ids from override device to base
1182 * device only if the ids are non-zero in override device. Else, honor
1183 * the values in base device.
1184 */
1185 if (override_dev->subsystem_vendor ||
1186 override_dev->subsystem_device) {
1187 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1188 base_dev->subsystem_device = override_dev->subsystem_device;
1189 }
1190
1191 /*
1192 * Copy value of inherity_subsystem from override device to base device
1193 * only if it is non-zero in override device. This allows override
1194 * tree to only enable inhert flag for a device.
1195 */
1196 if (override_dev->inherit_subsystem)
1197 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1198
1199 /*
1200 * Copy resources of override device to base device.
1201 * 1. If resource is already present in base device, then index and base
1202 * of the resource will be copied over.
1203 * 2. If resource is not already present in base device, a new resource
1204 * will be allocated.
1205 */
1206 struct resource *res = override_dev->res;
1207 while (res) {
1208 update_resource(base_dev, res);
1209 res = res->next;
1210 }
1211
1212 /*
1213 * Copy registers of override chip instance to base chip instance.
1214 * 1. If register key is already present in base chip instance, then
1215 * value for the register is copied over.
1216 * 2. If register key is not already present in base chip instance, then
1217 * a new register will be allocated.
1218 */
1219 struct reg *reg = override_dev->chip_instance->reg;
1220 while (reg) {
1221 update_register(base_dev->chip_instance, reg);
1222 reg = reg->next;
1223 }
1224
1225 /*
Furquan Shaikhbbade242020-05-02 16:05:29 -07001226 * Update base_chip_instance member in chip instance of override tree to forward it to
1227 * the chip instance in base tree.
1228 */
1229 override_dev->chip_instance->base_chip_instance = base_dev->chip_instance;
1230
1231 /*
Furquan Shaikh27efc502018-06-22 09:19:15 -07001232 * Now that the device properties are all copied over, look at each bus
1233 * of the override device and run override_devicetree in a recursive
1234 * manner. The assumption here is that first bus of override device
1235 * corresponds to first bus of base device and so on. If base device has
1236 * lesser buses than override tree, then new buses are allocated for it.
1237 */
1238 struct bus *override_bus = override_dev->bus;
1239 struct bus *base_bus = base_dev->bus;
1240
1241 while (override_bus) {
1242
1243 /*
1244 * If we have more buses in override tree device, then allocate
1245 * a new bus for the base tree device as well.
1246 */
1247 if (!base_bus) {
1248 alloc_bus(base_dev);
1249 base_bus = base_dev->last_bus;
1250 }
1251
1252 override_devicetree(base_dev->bus, override_dev->bus);
1253
1254 override_bus = override_bus->next_bus;
1255 base_bus = base_bus->next_bus;
1256 }
Furquan Shaikh27efc502018-06-22 09:19:15 -07001257}
1258
1259/*
1260 * Perform copy of device and properties from override parent to base parent.
1261 * This function walks through the override tree in a depth-first manner
1262 * performing following actions:
1263 * 1. If matching device is found in base tree, then copy the properties of
1264 * override device to base tree device. Call override_devicetree recursively on
1265 * the bus of override device.
1266 * 2. If matching device is not found in base tree, then set override tree
1267 * device as new child of base_parent and update the chip pointers in override
1268 * device subtree to ensure the nodes do not point to override tree chip
1269 * instance.
1270 */
1271static void override_devicetree(struct bus *base_parent,
1272 struct bus *override_parent)
1273{
1274 struct device *base_child;
1275 struct device *override_child = override_parent->children;
1276 struct device *next_child;
1277
1278 while (override_child) {
1279
1280 /* Look for a matching device in base tree. */
1281 for (base_child = base_parent->children;
1282 base_child; base_child = base_child->sibling) {
1283 if (device_match(base_child, override_child))
1284 break;
1285 }
1286
1287 next_child = override_child->sibling;
1288
1289 /*
1290 * If matching device is found, copy properties of
1291 * override_child to base_child.
1292 */
1293 if (base_child)
1294 update_device(base_child, override_child);
1295 else {
1296 /*
1297 * If matching device is not found, set override_child
1298 * as a new child of base_parent.
1299 */
1300 set_new_child(base_parent, override_child);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001301 }
1302
1303 override_child = next_child;
1304 }
1305}
1306
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001307int main(int argc, char **argv)
1308{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001309 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001310 usage();
1311
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001312 const char *base_devtree = argv[DEVICEFILE_ARG];
1313 const char *outputc = argv[OUTPUTFILE_ARG];
Nico Huber17e9bcb2019-09-20 12:05:51 +02001314 const char *outputh = argv[HEADERFILE_ARG];
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001315 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001316
1317 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001318
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001319 if (argc == TOTAL_ARG_COUNT) {
1320 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1321 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001322
Furquan Shaikh4d99b272019-04-11 15:13:25 -07001323 if (!dev_has_children(&override_root_dev)) {
1324 fprintf(stderr, "ERROR: Override tree needs at least one device!\n");
1325 exit(1);
1326 }
1327
Furquan Shaikh27efc502018-06-22 09:19:15 -07001328 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001329 }
1330
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001331 FILE *autogen = fopen(outputc, "w");
1332 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001333 fprintf(stderr, "Could not open file '%s' for writing: ",
1334 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001335 perror(NULL);
1336 exit(1);
1337 }
1338
Nico Huber17e9bcb2019-09-20 12:05:51 +02001339 FILE *autohead = fopen(outputh, "w");
1340 if (!autohead) {
1341 fprintf(stderr, "Could not open file '%s' for writing: ", outputh);
1342 perror(NULL);
1343 fclose(autogen);
1344 exit(1);
1345 }
1346 fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n");
1347 fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n");
1348 fprintf(autohead, "#include <device/device.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}