blob: 365edc41a6f34772e77265abe2fc4036a6e3d660 [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>
Patrick Georgi114e7b22010-05-05 11:19:50 +000018#include "sconfig.h"
19#include "sconfig.tab.h"
20
Patrick Georgi7fc9e292010-07-15 15:59:07 +000021extern int linenum;
22
Furquan Shaikh27efc502018-06-22 09:19:15 -070023/*
24 * Maintains list of all the unique chip structures for the board.
25 * This is shared across base and override device trees since we need to
26 * generate headers for all chips added by both the trees.
27 */
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070028static struct chip chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -070029
30/*
31 * This is intentionally shared between chip and device structure ids because it
32 * is easier to track the order of parsing for chip and device.
33 */
34static int count = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +000035
Kyösti Mälkki472d9022011-12-05 20:33:55 +020036typedef enum {
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020037 UNSLASH,
38 SPLIT_1ST,
39 TO_LOWER,
40 TO_UPPER,
41} translate_t;
42
Furquan Shaikh93198262018-06-03 04:22:17 -070043/*
44 * Mainboard is assumed to have a root device whose bus is the parent of all the
45 * devices that are added by parsing the devicetree file. This device has a
46 * mainboard chip instance associated with it.
47 *
48 *
49 *
50 * +------------------------+ +----------------------+
Furquan Shaikhde39fc72018-06-11 04:26:45 -070051 * | Root device | | Mainboard |
52 * +---------+ (base_root_dev) +--------------->+ instance +
Furquan Shaikh93198262018-06-03 04:22:17 -070053 * | | | chip_instance | (mainboard_instance)|
54 * | +------------------------+ | |
55 * | | +----------------------+
56 * | | bus |
57 * | parent v |
58 * | +-------------------+ |
59 * | | Root bus | |
Furquan Shaikhde39fc72018-06-11 04:26:45 -070060 * +----------->+ (base_root_bus) | |
Furquan Shaikh93198262018-06-03 04:22:17 -070061 * | | |
62 * +-------------------+ |
63 * | |
64 * | children | chip
65 * v |
66 * X |
67 * (new devices will |
68 * be added here as |
69 * children) |
70 * |
71 * |
72 * |
73 * +-------+----------+
74 * | |
75 * | Mainboard chip +----------->X (new chips will be
76 * | (mainboard_chip) | added here)
77 * | |
78 * +------------------+
79 *
80 *
81 */
Furquan Shaikh39ac7972018-06-21 18:44:32 -070082
83/* Root device of primary tree. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -070084static struct device base_root_dev;
Furquan Shaikh39ac7972018-06-21 18:44:32 -070085
86/* Root device of override tree (if applicable). */
87static struct device override_root_dev;
88
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -070089static struct chip_instance mainboard_instance;
90
Furquan Shaikhde39fc72018-06-11 04:26:45 -070091static struct bus base_root_bus = {
Furquan Shaikh93198262018-06-03 04:22:17 -070092 .id = 0,
Furquan Shaikhde39fc72018-06-11 04:26:45 -070093 .dev = &base_root_dev,
Furquan Shaikh93198262018-06-03 04:22:17 -070094};
95
Furquan Shaikhde39fc72018-06-11 04:26:45 -070096static struct device base_root_dev = {
Furquan Shaikh93198262018-06-03 04:22:17 -070097 .name = "dev_root",
98 .id = 0,
99 .chip_instance = &mainboard_instance,
100 .path = " .type = DEVICE_PATH_ROOT ",
101 .ops = "&default_dev_ops_root",
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700102 .parent = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -0700103 .enabled = 1,
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700104 .bus = &base_root_bus,
Furquan Shaikh93198262018-06-03 04:22:17 -0700105};
106
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700107static struct bus override_root_bus = {
108 .id = 0,
109 .dev = &override_root_dev,
110};
111
112static struct device override_root_dev = {
113 .name = "override_root",
114 .id = 0,
115 /*
116 * Override tree root device points to the same mainboard chip instance
117 * as the base tree root device. It should not cause any side-effects
118 * since the mainboard chip instance pointer in override tree will just
119 * be ignored.
120 */
121 .chip_instance = &mainboard_instance,
122 .path = " .type = DEVICE_PATH_ROOT ",
123 .ops = "&default_dev_ops_root",
124 .parent = &override_root_bus,
125 .enabled = 1,
126 .bus = &override_root_bus,
127};
128
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700129static struct chip mainboard_chip = {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000130 .name = "mainboard",
131 .name_underscore = "mainboard",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700132 .instance = &mainboard_instance,
133};
134
135static struct chip_instance mainboard_instance = {
136 .id = 0,
137 .chip = &mainboard_chip,
Furquan Shaikh27efc502018-06-22 09:19:15 -0700138 .ref_count = 2,
Patrick Georgi114e7b22010-05-05 11:19:50 +0000139};
140
Furquan Shaikh93198262018-06-03 04:22:17 -0700141/* This is the parent of all devices added by parsing the devicetree file. */
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700142struct bus *root_parent;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000143
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700144struct queue_entry {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700145 void *data;
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700146 struct queue_entry *next;
147 struct queue_entry *prev;
148};
Furquan Shaikh79e84122018-05-30 15:09:09 -0700149
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700150#define S_ALLOC(_s) s_alloc(__func__, _s)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700151
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700152static void *s_alloc(const char *f, size_t s)
153{
154 void *data = calloc(1, s);
155 if (!data) {
Maulik V Vaghela82e8c692018-06-08 10:32:08 +0530156 fprintf(stderr, "%s: Failed to alloc mem!\n", f);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700157 exit(1);
158 }
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700159 return data;
160}
161
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700162static struct queue_entry *new_queue_entry(void *data)
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700163{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700164 struct queue_entry *e = S_ALLOC(sizeof(*e));
Furquan Shaikh79e84122018-05-30 15:09:09 -0700165
166 e->data = data;
167 e->next = e->prev = e;
168 return e;
169}
170
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700171static void enqueue_tail(struct queue_entry **q_head, void *data)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700172{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700173 struct queue_entry *tmp = new_queue_entry(data);
174 struct queue_entry *q = *q_head;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700175
176 if (!q) {
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700177 *q_head = tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700178 return;
179 }
180
181 q->prev->next = tmp;
182 tmp->prev = q->prev;
183 q->prev = tmp;
184 tmp->next = q;
185}
186
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700187static void *dequeue_tail(struct queue_entry **q_head)
Furquan Shaikh79e84122018-05-30 15:09:09 -0700188{
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700189 struct queue_entry *q = *q_head;
190 struct queue_entry *tmp;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700191 void *data;
192
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700193 if (!q)
194 return NULL;
195
196 tmp = q->prev;
197
Furquan Shaikh79e84122018-05-30 15:09:09 -0700198 if (tmp == q)
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700199 *q_head = NULL;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700200 else {
201 tmp->prev->next = q;
202 q->prev = tmp->prev;
203 }
204
205 data = tmp->data;
206 free(tmp);
207
208 return data;
209}
210
Furquan Shaikh7398ded2018-06-03 10:42:49 -0700211static void *dequeue_head(struct queue_entry **q_head)
212{
213 struct queue_entry *q = *q_head;
214 struct queue_entry *tmp = q;
215 void *data;
216
217 if (!q)
218 return NULL;
219
220 if (q->next == q)
221 *q_head = NULL;
222 else {
223 q->next->prev = q->prev;
224 q->prev->next = q->next;
225 *q_head = q->next;
226 }
227
228 data = tmp->data;
229 free(tmp);
230
231 return data;
232}
233
234static void *peek_queue_head(struct queue_entry *q_head)
235{
236 if (!q_head)
237 return NULL;
238
239 return q_head->data;
240}
241
242static struct queue_entry *chip_q_head;
243
244void chip_enqueue_tail(void *data)
245{
246 enqueue_tail(&chip_q_head, data);
247}
248
249void *chip_dequeue_tail(void)
250{
251 return dequeue_tail(&chip_q_head);
252}
253
Martin Rothbec07532016-08-05 18:32:18 -0600254int yywrap(void)
255{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000256 return 1;
257}
258
Martin Rothbec07532016-08-05 18:32:18 -0600259void yyerror(char const *str)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000260{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000261 extern char *yytext;
Martin Rothbec07532016-08-05 18:32:18 -0600262 fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000263 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000264}
265
Martin Rothbec07532016-08-05 18:32:18 -0600266char *translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200267{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200268 char *b, *c;
269 b = c = strdup(str);
270 while (c && *c) {
271 if ((mode == SPLIT_1ST) && (*c == '/')) {
272 *c = 0;
273 break;
274 }
Martin Rothbec07532016-08-05 18:32:18 -0600275 if (*c == '/')
276 *c = '_';
277 if (*c == '-')
278 *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200279 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200280 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200281 if (mode == TO_LOWER)
282 *c = tolower(*c);
283 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200284 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200285 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200286}
287
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700288static struct chip *get_chip(char *path)
Martin Rothbec07532016-08-05 18:32:18 -0600289{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700290 struct chip *h = &chip_header;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700291
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700292 while (h->next) {
293 int result = strcmp(path, h->next->name);
294 if (result == 0)
295 return h->next;
296
297 if (result < 0)
298 break;
299
300 h = h->next;
301 }
302
303 struct chip *new_chip = S_ALLOC(sizeof(struct chip));
304 new_chip->next = h->next;
305 h->next = new_chip;
306
Patrick Georgi114e7b22010-05-05 11:19:50 +0000307 new_chip->chiph_exists = 1;
308 new_chip->name = path;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700309 new_chip->name_underscore = translate_name(path, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000310
311 struct stat st;
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700312 char *chip_h = S_ALLOC(strlen(path) + 18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700313 sprintf(chip_h, "src/%s", path);
314 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100315 /* root_complex gets away without a separate directory, but
316 * exists on on pretty much all AMD chipsets.
317 */
318 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300319 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
320 path);
321 exit(1);
322 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700323 }
324
Martin Roth824255e2016-08-05 17:40:39 -0600325 sprintf(chip_h, "src/%s/chip.h", path);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200326
Martin Roth824255e2016-08-05 17:40:39 -0600327 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
Martin Rothbec07532016-08-05 18:32:18 -0600328 new_chip->chiph_exists = 0;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000329
Patrick Georgi1f688802014-08-03 15:51:19 +0200330 free(chip_h);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700331
Patrick Georgi114e7b22010-05-05 11:19:50 +0000332 return new_chip;
333}
334
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700335struct chip_instance *new_chip_instance(char *path)
336{
337 struct chip *chip = get_chip(path);
338 struct chip_instance *instance = S_ALLOC(sizeof(*instance));
339
340 instance->id = ++count;
341 instance->chip = chip;
342 instance->next = chip->instance;
343 chip->instance = instance;
344
345 return instance;
346}
347
Furquan Shaikh27efc502018-06-22 09:19:15 -0700348static void delete_chip_instance(struct chip_instance *ins)
349{
350
351 if (ins->ref_count == 0) {
352 printf("ERROR: ref count for chip instance is zero!!\n");
353 exit(1);
354 }
355
356 if (--ins->ref_count)
357 return;
358
359 struct chip *c = ins->chip;
360
361 /* Get pointer to first instance of the chip. */
362 struct chip_instance *i = c->instance;
363
364 /*
365 * If chip instance to be deleted is the first instance, then update
366 * instance pointer of the chip as well.
367 */
368 if (i == ins) {
369 c->instance = ins->next;
370 free(ins);
371 return;
372 }
373
374 /*
375 * Loop through the instances list of the chip to find and remove the
376 * given instance.
377 */
378 while (1) {
379 if (i == NULL) {
380 printf("ERROR: chip instance not found!\n");
381 exit(1);
382 }
383
384 if (i->next != ins) {
385 i = i->next;
386 continue;
387 }
388
389 i->next = ins->next;
390 break;
391 }
392
393 free(ins);
394}
395
Furquan Shaikh93198262018-06-03 04:22:17 -0700396/*
397 * Allocate a new bus for the provided device.
398 * - If this is the first bus being allocated under this device, then its id
399 * is set to 0 and bus and last_bus are pointed to the newly allocated bus.
400 * - If this is not the first bus under this device, then its id is set to 1
401 * plus the id of last bus and newly allocated bus is added to the list of
402 * buses under the device. last_bus is updated to point to the newly
403 * allocated bus.
404 */
405static void alloc_bus(struct device *dev)
406{
407 struct bus *bus = S_ALLOC(sizeof(*bus));
408
409 bus->dev = dev;
410
411 if (dev->last_bus == NULL) {
412 bus->id = 0;
413 dev->bus = bus;
414 } else {
415 bus->id = dev->last_bus->id + 1;
416 dev->last_bus->next_bus = bus;
417 }
418
419 dev->last_bus = bus;
420}
421
422/*
423 * Allocate a new device under the given parent. This function allocates a new
424 * device structure under the provided parent bus and allocates a bus structure
425 * under the newly allocated device.
426 */
427static struct device *alloc_dev(struct bus *parent)
428{
429 struct device *dev = S_ALLOC(sizeof(*dev));
430
431 dev->id = ++count;
432 dev->parent = parent;
433 dev->subsystem_vendor = -1;
434 dev->subsystem_device = -1;
435
436 alloc_bus(dev);
437
438 return dev;
439}
440
441/*
442 * This function scans the children of given bus to see if any device matches
443 * the new device that is requested.
444 *
445 * Returns pointer to the node if found, else NULL.
446 */
447static struct device *get_dev(struct bus *parent, int path_a, int path_b,
448 int bustype, struct chip_instance *chip_instance)
449{
450 struct device *child = parent->children;
451
452 while (child) {
453 if ((child->path_a == path_a) && (child->path_b == path_b) &&
454 (child->bustype == bustype) &&
455 (child->chip_instance == chip_instance))
456 return child;
457
458 child = child->sibling;
459 }
460
461 return NULL;
462}
463
Furquan Shaikh27efc502018-06-22 09:19:15 -0700464/*
465 * Add given node as child of the provided parent. If this is the first child of
466 * the parent, update parent->children pointer as well.
467 */
468static void set_new_child(struct bus *parent, struct device *child)
469{
470 struct device *c = parent->children;
471 if (c) {
472 while (c->sibling)
473 c = c->sibling;
474 c->sibling = child;
475 } else
476 parent->children = child;
477
478 child->sibling = NULL;
479 child->parent = parent;
480}
481
Furquan Shaikh93198262018-06-03 04:22:17 -0700482struct device *new_device(struct bus *parent,
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700483 struct chip_instance *chip_instance,
Furquan Shaikha9b64292018-05-31 07:52:00 -0700484 const int bustype, const char *devnum,
Furquan Shaikh79e84122018-05-30 15:09:09 -0700485 int enabled)
Martin Rothbec07532016-08-05 18:32:18 -0600486{
Patrick Georgi114e7b22010-05-05 11:19:50 +0000487 char *tmp;
Furquan Shaikh93198262018-06-03 04:22:17 -0700488 int path_a;
489 int path_b = 0;
490 struct device *new_d;
491
492 path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000493 if (*tmp == '.') {
494 tmp++;
Furquan Shaikh93198262018-06-03 04:22:17 -0700495 path_b = strtol(tmp, NULL, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000496 }
497
Furquan Shaikh93198262018-06-03 04:22:17 -0700498 /* If device is found under parent, no need to allocate new device. */
499 new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
500 if (new_d) {
501 alloc_bus(new_d);
502 return new_d;
503 }
504
505 new_d = alloc_dev(parent);
506
507 new_d->bustype = bustype;
508
509 new_d->path_a = path_a;
510 new_d->path_b = path_b;
511
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700512 char *name = S_ALLOC(10);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000513 sprintf(name, "_dev%d", new_d->id);
514 new_d->name = name;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700515
Patrick Georgi114e7b22010-05-05 11:19:50 +0000516 new_d->enabled = enabled;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700517 new_d->chip_instance = chip_instance;
Furquan Shaikh27efc502018-06-22 09:19:15 -0700518 chip_instance->ref_count++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000519
Furquan Shaikh27efc502018-06-22 09:19:15 -0700520 set_new_child(parent, new_d);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000521
Furquan Shaikha9b64292018-05-31 07:52:00 -0700522 switch (bustype) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200523 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000524 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200525 break;
526
527 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000528 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200529 break;
530
531 case I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700532 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200533 break;
534
535 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000536 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200537 break;
538
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800539 case CPU_CLUSTER:
540 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200541 break;
542
Aaron Durbinffda804b2014-09-03 12:40:15 -0500543 case CPU:
544 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
545 break;
546
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800547 case DOMAIN:
548 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200549 break;
550
551 case IOAPIC:
552 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
553 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700554
555 case GENERIC:
556 new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}";
557 break;
Furquan Shaikhe6700292017-02-11 00:50:38 -0800558
559 case SPI:
560 new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}";
561 break;
562
Duncan Lauriebae9f852018-05-07 14:18:13 -0700563 case USB:
564 new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}";
565 break;
566
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800567 case MMIO:
568 new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}";
569 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000570 }
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800571
Patrick Georgi114e7b22010-05-05 11:19:50 +0000572 return new_d;
573}
574
Furquan Shaikh27efc502018-06-22 09:19:15 -0700575static void new_resource(struct device *dev, int type, int index, int base)
Martin Rothbec07532016-08-05 18:32:18 -0600576{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700577 struct resource *r = S_ALLOC(sizeof(struct resource));
578
Patrick Georgi114e7b22010-05-05 11:19:50 +0000579 r->type = type;
580 r->index = index;
581 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000582 if (dev->res) {
583 struct resource *head = dev->res;
Martin Rothbec07532016-08-05 18:32:18 -0600584 while (head->next)
585 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000586 head->next = r;
587 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000588 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000589 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000590}
591
Furquan Shaikh27efc502018-06-22 09:19:15 -0700592void add_resource(struct bus *bus, int type, int index, int base)
593{
594 new_resource(bus->dev, type, index, base);
595}
596
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700597void add_register(struct chip_instance *chip_instance, char *name, char *val)
Martin Rothbec07532016-08-05 18:32:18 -0600598{
Furquan Shaikh369e1f02018-05-31 09:48:51 -0700599 struct reg *r = S_ALLOC(sizeof(struct reg));
600
Patrick Georgi114e7b22010-05-05 11:19:50 +0000601 r->key = name;
602 r->value = val;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700603 if (chip_instance->reg) {
604 struct reg *head = chip_instance->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000605 // sorting to be equal to sconfig's behaviour
606 int sort = strcmp(r->key, head->key);
607 if (sort == 0) {
608 printf("ERROR: duplicate 'register' key.\n");
609 exit(1);
610 }
Martin Rothbec07532016-08-05 18:32:18 -0600611 if (sort < 0) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000612 r->next = head;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700613 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000614 } else {
Martin Rothbec07532016-08-05 18:32:18 -0600615 while ((head->next)
616 && (strcmp(head->next->key, r->key) < 0))
617 head = head->next;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000618 r->next = head->next;
619 head->next = r;
620 }
621 } else {
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700622 chip_instance->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000623 }
624}
625
Furquan Shaikh93198262018-06-03 04:22:17 -0700626void add_pci_subsystem_ids(struct bus *bus, int vendor, int device,
Martin Rothbec07532016-08-05 18:32:18 -0600627 int inherit)
Sven Schnelle270a9082011-03-01 19:58:15 +0000628{
Furquan Shaikh93198262018-06-03 04:22:17 -0700629 struct device *dev = bus->dev;
630
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800631 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000632 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
633 exit(1);
634 }
635
636 dev->subsystem_vendor = vendor;
637 dev->subsystem_device = device;
638 dev->inherit_subsystem = inherit;
639}
640
Furquan Shaikh93198262018-06-03 04:22:17 -0700641void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin,
Martin Rothbec07532016-08-05 18:32:18 -0600642 int irqpin)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200643{
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200644 int srcpin;
Furquan Shaikh93198262018-06-03 04:22:17 -0700645 struct device *dev = bus->dev;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200646
Martin Rothbec07532016-08-05 18:32:18 -0600647 if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) ||
648 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200649 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
650 exit(1);
651 }
652
653 srcpin = _srcpin[3] - 'A';
654
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800655 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200656 printf("ERROR: ioapic config only allowed for PCI devices\n");
657 exit(1);
658 }
659
660 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200661 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200662 exit(1);
663 }
664 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
665 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
666}
667
Furquan Shaikh93198262018-06-03 04:22:17 -0700668static int dev_has_children(struct device *dev)
Martin Rothbec07532016-08-05 18:32:18 -0600669{
Furquan Shaikh93198262018-06-03 04:22:17 -0700670 struct bus *bus = dev->bus;
671
672 while (bus) {
673 if (bus->children)
674 return 1;
675 bus = bus->next_bus;
676 }
677
678 return 0;
679}
680
681static void pass0(FILE *fil, struct device *ptr, struct device *next)
682{
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700683 if (ptr == &base_root_dev) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700684 fprintf(fil, "DEVTREE_CONST struct bus %s_links[];\n",
685 ptr->name);
686 return;
687 }
688
689 fprintf(fil, "DEVTREE_CONST static struct device %s;\n", ptr->name);
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700690 if (ptr->res)
Furquan Shaikh93198262018-06-03 04:22:17 -0700691 fprintf(fil, "DEVTREE_CONST struct resource %s_res[];\n",
692 ptr->name);
693 if (dev_has_children(ptr))
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500694 fprintf(fil, "DEVTREE_CONST struct bus %s_links[];\n",
Martin Rothbec07532016-08-05 18:32:18 -0600695 ptr->name);
Stefan Reinauer57879c92012-07-31 16:47:25 -0700696
Furquan Shaikh93198262018-06-03 04:22:17 -0700697 if (next)
698 return;
699
700 fprintf(fil,
701 "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n",
702 ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000703}
704
Furquan Shaikh93198262018-06-03 04:22:17 -0700705static void emit_resources(FILE *fil, struct device *ptr)
706{
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700707 if (ptr->res == NULL)
Furquan Shaikh93198262018-06-03 04:22:17 -0700708 return;
709
710 int i = 1;
711 fprintf(fil, "DEVTREE_CONST struct resource %s_res[] = {\n", ptr->name);
712 struct resource *r = ptr->res;
713 while (r) {
714 fprintf(fil,
715 "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
716 if (r->type == IRQ)
717 fprintf(fil, "IRQ");
718 if (r->type == DRQ)
719 fprintf(fil, "DRQ");
720 if (r->type == IO)
721 fprintf(fil, "IO");
722 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index,
723 r->base);
724 if (r->next)
725 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name,
726 i++);
727 else
728 fprintf(fil, ".next=NULL },\n");
729 r = r->next;
730 }
731
732 fprintf(fil, "\t };\n");
733}
734
735static void emit_bus(FILE *fil, struct bus *bus)
736{
737 fprintf(fil, "\t\t[%d] = {\n", bus->id);
738 fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
739 fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
740 if (bus->children)
741 fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
742
743 if (bus->next_bus)
744 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
745 bus->id + 1);
746 else
747 fprintf(fil, "\t\t\t.next = NULL,\n");
748 fprintf(fil, "\t\t},\n");
749}
750
751static void emit_dev_links(FILE *fil, struct device *ptr)
752{
753 fprintf(fil, "DEVTREE_CONST struct bus %s_links[] = {\n",
754 ptr->name);
755
756 struct bus *bus = ptr->bus;
757
758 while (bus) {
759 emit_bus(fil, bus);
760 bus = bus->next_bus;
761 }
762
763 fprintf(fil, "\t};\n");
764}
765
766static void pass1(FILE *fil, struct device *ptr, struct device *next)
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200767{
768 int pin;
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700769 struct chip_instance *chip_ins = ptr->chip_instance;
Furquan Shaikh93198262018-06-03 04:22:17 -0700770 int has_children = dev_has_children(ptr);
Furquan Shaikh79e84122018-05-30 15:09:09 -0700771
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700772 if (ptr != &base_root_dev)
Furquan Shaikh93198262018-06-03 04:22:17 -0700773 fprintf(fil, "static ");
774 fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name);
775 fprintf(fil, "#if !DEVTREE_EARLY\n");
776 fprintf(fil, "\t.ops = %s,\n", (ptr->ops) ? (ptr->ops) : "0");
777 fprintf(fil, "#endif\n");
778 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
779 ptr->parent->id);
780 fprintf(fil, "\t.path = {");
781 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
782 fprintf(fil, "},\n");
783 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
784 fprintf(fil, "\t.on_mainboard = 1,\n");
785 if (ptr->subsystem_vendor > 0)
786 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n",
787 ptr->subsystem_vendor);
Sven Schnelle270a9082011-03-01 19:58:15 +0000788
Furquan Shaikh93198262018-06-03 04:22:17 -0700789 for (pin = 0; pin < 4; pin++) {
790 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
Martin Rothbec07532016-08-05 18:32:18 -0600791 fprintf(fil,
Furquan Shaikh93198262018-06-03 04:22:17 -0700792 "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n",
793 pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
794
795 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
796 fprintf(fil,
797 "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n",
798 pin, ptr->pci_irq_info[pin].ioapic_dst_id);
Myles Watsonc25cc112010-05-21 14:33:48 +0000799 }
Furquan Shaikh93198262018-06-03 04:22:17 -0700800
801 if (ptr->subsystem_device > 0)
802 fprintf(fil, "\t.subsystem_device = 0x%04x,\n",
803 ptr->subsystem_device);
804
Furquan Shaikh4ca3a8a2018-06-07 23:36:45 -0700805 if (ptr->res) {
Furquan Shaikh93198262018-06-03 04:22:17 -0700806 fprintf(fil, "\t.resource_list = &%s_res[0],\n",
Martin Rothbec07532016-08-05 18:32:18 -0600807 ptr->name);
Furquan Shaikh93198262018-06-03 04:22:17 -0700808 }
809 if (has_children)
810 fprintf(fil, "\t.link_list = &%s_links[0],\n",
811 ptr->name);
812 else
813 fprintf(fil, "\t.link_list = NULL,\n");
814 if (ptr->sibling)
815 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
816 fprintf(fil, "#if !DEVTREE_EARLY\n");
817 fprintf(fil, "\t.chip_ops = &%s_ops,\n",
818 chip_ins->chip->name_underscore);
819 if (chip_ins == &mainboard_instance)
820 fprintf(fil, "\t.name = mainboard_name,\n");
821 fprintf(fil, "#endif\n");
822 if (chip_ins->chip->chiph_exists)
823 fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
824 chip_ins->chip->name_underscore, chip_ins->id);
825 if (next)
826 fprintf(fil, "\t.next=&%s\n", next->name);
827 fprintf(fil, "};\n");
828
829 emit_resources(fil, ptr);
830
831 if (has_children)
832 emit_dev_links(fil, ptr);
833}
834
835static void add_siblings_to_queue(struct queue_entry **bfs_q_head,
836 struct device *d)
837{
838 while (d) {
839 enqueue_tail(bfs_q_head, d);
840 d = d->sibling;
841 }
842}
843
844static void add_children_to_queue(struct queue_entry **bfs_q_head,
845 struct device *d)
846{
847 struct bus *bus = d->bus;
848
849 while (bus) {
850 if (bus->children)
851 add_siblings_to_queue(bfs_q_head, bus->children);
852 bus = bus->next_bus;
Myles Watson894a3472010-06-09 22:41:35 +0000853 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000854}
855
Furquan Shaikh79e84122018-05-30 15:09:09 -0700856static void walk_device_tree(FILE *fil, struct device *ptr,
Furquan Shaikh93198262018-06-03 04:22:17 -0700857 void (*func)(FILE *, struct device *,
858 struct device *))
Martin Rothbec07532016-08-05 18:32:18 -0600859{
Furquan Shaikh93198262018-06-03 04:22:17 -0700860 struct queue_entry *bfs_q_head = NULL;
861
862 enqueue_tail(&bfs_q_head, ptr);
863
864 while ((ptr = dequeue_head(&bfs_q_head))) {
865 add_children_to_queue(&bfs_q_head, ptr);
866 func(fil, ptr, peek_queue_head(bfs_q_head));
867 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000868}
869
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700870static void emit_chip_headers(FILE *fil, struct chip *chip)
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700871{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700872 struct chip *tmp = chip;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700873
874 fprintf(fil, "#include <device/device.h>\n");
875 fprintf(fil, "#include <device/pci.h>\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700876
877 while (chip) {
878 if (chip->chiph_exists)
879 fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
880 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700881 }
882 fprintf(fil, "\n#if !DEVTREE_EARLY\n");
883 fprintf(fil,
884 "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700885
886 chip = tmp;
887 while (chip) {
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700888 fprintf(fil,
889 "__attribute__((weak)) struct chip_operations %s_ops = {};\n",
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700890 chip->name_underscore);
891 chip = chip->next;
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700892 }
893 fprintf(fil, "#endif\n");
894}
895
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700896static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
897{
898 fprintf(fil, "DEVTREE_CONST struct %s_config %s_info_%d = {",
899 instance->chip->name_underscore,
900 instance->chip->name_underscore,
901 instance->id);
902
903 if (instance->reg) {
904 fprintf(fil, "\n");
905 struct reg *r = instance->reg;
906 while (r) {
907 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
908 r = r->next;
909 }
910 }
911 fprintf(fil, "};\n\n");
912}
913
Furquan Shaikh79e84122018-05-30 15:09:09 -0700914static void emit_chips(FILE *fil)
915{
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700916 struct chip *chip = chip_header.next;
917 struct chip_instance *instance;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700918
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700919 emit_chip_headers(fil, chip);
Furquan Shaikha0cc5a62018-05-30 23:46:16 -0700920
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700921 for (; chip; chip = chip->next) {
Furquan Shaikh79e84122018-05-30 15:09:09 -0700922 if (!chip->chiph_exists)
923 continue;
924
Furquan Shaikhc56ae2f2018-05-31 10:33:16 -0700925 instance = chip->instance;
926 while (instance) {
927 emit_chip_instance(fil, instance);
928 instance = instance->next;
Furquan Shaikh79e84122018-05-30 15:09:09 -0700929 }
930 }
931}
932
Furquan Shaikh93198262018-06-03 04:22:17 -0700933static void inherit_subsystem_ids(FILE *file, struct device *dev,
934 struct device *next)
Sven Schnelle270a9082011-03-01 19:58:15 +0000935{
936 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000937
938 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
939 /* user already gave us a subsystem vendor/device */
940 return;
941 }
942
Furquan Shaikh93198262018-06-03 04:22:17 -0700943 for (p = dev; p && p->parent->dev != p; p = p->parent->dev) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000944
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800945 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000946 continue;
947
948 if (p->inherit_subsystem) {
949 dev->subsystem_vendor = p->subsystem_vendor;
950 dev->subsystem_device = p->subsystem_device;
951 break;
952 }
953 }
954}
955
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200956static void usage(void)
957{
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700958 printf("usage: sconfig devicetree_file output_file [override_devicetree_file]\n");
Martin Rothbec07532016-08-05 18:32:18 -0600959 exit(1);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200960}
961
Martin Roth32051702015-11-24 12:34:16 -0700962enum {
Martin Rothc9c27bb2016-08-05 18:15:06 -0600963 DEVICEFILE_ARG = 1,
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700964 OUTPUTFILE_ARG,
965 OVERRIDE_DEVICEFILE_ARG,
Martin Rothbec07532016-08-05 18:32:18 -0600966};
Martin Roth32051702015-11-24 12:34:16 -0700967
Furquan Shaikh39ac7972018-06-21 18:44:32 -0700968#define MANDATORY_ARG_COUNT 3
969#define OPTIONAL_ARG_COUNT 1
970#define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200971
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700972static void parse_devicetree(const char *file, struct bus *parent)
Martin Rothbec07532016-08-05 18:32:18 -0600973{
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700974 FILE *filec = fopen(file, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000975 if (!filec) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000976 perror(NULL);
977 exit(1);
978 }
979
Patrick Georgi114e7b22010-05-05 11:19:50 +0000980 yyrestart(filec);
981
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700982 root_parent = parent;
983 linenum = 0;
984
Patrick Georgi114e7b22010-05-05 11:19:50 +0000985 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000986
Patrick Georgi114e7b22010-05-05 11:19:50 +0000987 fclose(filec);
Furquan Shaikhde39fc72018-06-11 04:26:45 -0700988}
989
Furquan Shaikh27efc502018-06-22 09:19:15 -0700990/*
991 * Match device nodes from base and override tree to see if they are the same
992 * node.
993 */
994static int device_match(struct device *a, struct device *b)
995{
996 return ((a->path_a == b->path_a) &&
997 (a->path_b == b->path_b) &&
998 (a->bustype == b->bustype) &&
999 (a->chip_instance->chip ==
1000 b->chip_instance->chip));
1001}
1002
1003/*
1004 * Walk through the override subtree in breadth-first manner starting at node to
1005 * see if chip_instance pointer of the node is same as chip_instance pointer of
1006 * override parent that is passed into the function. If yes, then update the
1007 * chip_instance pointer of the node to chip_instance pointer of the base
1008 * parent.
1009 */
1010static void update_chip_pointers(struct device *node,
1011 struct chip_instance *base_parent_ci,
1012 struct chip_instance *override_parent_ci)
1013{
1014 struct queue_entry *q_head = NULL;
1015
1016 enqueue_tail(&q_head, node);
1017
1018 while ((node = dequeue_head(&q_head))) {
1019 if (node->chip_instance != override_parent_ci)
1020 continue;
1021 node->chip_instance = base_parent_ci;
1022 add_children_to_queue(&q_head, node);
1023 }
1024}
1025
1026/*
1027 * Add resource to device. If resource is already present, then update its base
1028 * and index. If not, then add a new resource to the device.
1029 */
1030static void update_resource(struct device *dev, struct resource *res)
1031{
1032 struct resource *base_res = dev->res;
1033
1034 while (base_res) {
1035 if (base_res->type == res->type) {
1036 base_res->index = res->index;
1037 base_res->base = res->base;
1038 return;
1039 }
1040 base_res = base_res->next;
1041 }
1042
1043 new_resource(dev, res->type, res->index, res->base);
1044}
1045
1046/*
1047 * Add register to chip instance. If register is already present, then update
1048 * its value. If not, then add a new register to the chip instance.
1049 */
1050static void update_register(struct chip_instance *c, struct reg *reg)
1051{
1052 struct reg *base_reg = c->reg;
1053
1054 while (base_reg) {
1055 if (!strcmp(base_reg->key, reg->key)) {
1056 base_reg->value = reg->value;
1057 return;
1058 }
1059 base_reg = base_reg->next;
1060 }
1061
1062 add_register(c, reg->key, reg->value);
1063}
1064
1065static void override_devicetree(struct bus *base_parent,
1066 struct bus *override_parent);
1067
1068/*
1069 * Update the base device properties using the properties of override device. In
1070 * addition to that, call override_devicetree for all the buses under the
1071 * override device.
1072 *
1073 * Override Rules:
1074 * +--------------------+--------------------------------------------+
1075 * | | |
1076 * |struct device member| Rule |
1077 * | | |
1078 * +-----------------------------------------------------------------+
1079 * | | |
1080 * | id | Unchanged. This is used to generate device |
1081 * | | structure name in static.c. So, no need to |
1082 * | | override. |
1083 * | | |
1084 * +-----------------------------------------------------------------+
1085 * | | |
1086 * | enabled | Copy enabled state from override device. |
1087 * | | This allows variants to override device |
1088 * | | state. |
1089 * | | |
1090 * +-----------------------------------------------------------------+
1091 * | | |
1092 * | subsystem_vendor | Copy from override device only if any one |
1093 * | subsystem_device | of the ids is non-zero. |
1094 * | | |
1095 * +-----------------------------------------------------------------+
1096 * | | |
1097 * | inherit_subsystem | Copy from override device only if it is |
1098 * | | non-zero. This allows variant to only |
1099 * | | enable inherit flag for a device. |
1100 * | | |
1101 * +-----------------------------------------------------------------+
1102 * | | |
1103 * | path | Unchanged since these are same for both |
1104 * | path_a | base and override device (Used for |
1105 * | path_b | matching devices). |
1106 * | | |
1107 * +-----------------------------------------------------------------+
1108 * | | |
1109 * | bustype | Unchanged since this is same for both base |
1110 * | | and override device (User for matching |
1111 * | | devices). |
1112 * | | |
1113 * +-----------------------------------------------------------------+
1114 * | | |
1115 * | pci_irq_info | Unchanged. |
1116 * | | |
1117 * +-----------------------------------------------------------------+
1118 * | | |
1119 * | parent | Unchanged. This is meaningful only within |
1120 * | sibling | the parse tree, hence not being copied. |
1121 * | | |
1122 * +-----------------------------------------------------------------+
1123 * | | |
1124 * | res | Each resource that is present in override |
1125 * | | device is copied over to base device: |
1126 * | | 1. If resource of same type is present in |
1127 * | | base device, then index and base of the |
1128 * | | resource is copied. |
1129 * | | 2. If not, then a new resource is allocated|
1130 * | | under the base device using type, index |
1131 * | | and base from override res. |
1132 * | | |
1133 * +-----------------------------------------------------------------+
1134 * | | |
1135 * | chip_instance | Each register of chip_instance is copied |
1136 * | | over from override device to base device: |
1137 * | | 1. If register with same key is present in |
1138 * | | base device, then value of the register |
1139 * | | is copied. |
1140 * | | 2. If not, then a new register is allocated|
1141 * | | under the base chip_instance using key |
1142 * | | and value from override register. |
1143 * | | |
1144 * +-----------------------------------------------------------------+
1145 * | | |
1146 * | bus | Recursively call override_devicetree on |
1147 * | last_bus | each bus of override device. It is assumed |
1148 * | | that bus with id X under base device |
1149 * | | to bus with id X under override device. If |
1150 * | | override device has more buses than base |
1151 * | | device, then new buses are allocated under |
1152 * | | base device. |
1153 * | | |
1154 * +-----------------------------------------------------------------+
1155 */
1156static void update_device(struct device *base_dev, struct device *override_dev)
1157{
1158 /*
1159 * Copy the enabled state of override device to base device. This allows
1160 * override tree to enable or disable a particular device.
1161 */
1162 base_dev->enabled = override_dev->enabled;
1163
1164 /*
1165 * Copy subsystem vendor and device ids from override device to base
1166 * device only if the ids are non-zero in override device. Else, honor
1167 * the values in base device.
1168 */
1169 if (override_dev->subsystem_vendor ||
1170 override_dev->subsystem_device) {
1171 base_dev->subsystem_vendor = override_dev->subsystem_vendor;
1172 base_dev->subsystem_device = override_dev->subsystem_device;
1173 }
1174
1175 /*
1176 * Copy value of inherity_subsystem from override device to base device
1177 * only if it is non-zero in override device. This allows override
1178 * tree to only enable inhert flag for a device.
1179 */
1180 if (override_dev->inherit_subsystem)
1181 base_dev->inherit_subsystem = override_dev->inherit_subsystem;
1182
1183 /*
1184 * Copy resources of override device to base device.
1185 * 1. If resource is already present in base device, then index and base
1186 * of the resource will be copied over.
1187 * 2. If resource is not already present in base device, a new resource
1188 * will be allocated.
1189 */
1190 struct resource *res = override_dev->res;
1191 while (res) {
1192 update_resource(base_dev, res);
1193 res = res->next;
1194 }
1195
1196 /*
1197 * Copy registers of override chip instance to base chip instance.
1198 * 1. If register key is already present in base chip instance, then
1199 * value for the register is copied over.
1200 * 2. If register key is not already present in base chip instance, then
1201 * a new register will be allocated.
1202 */
1203 struct reg *reg = override_dev->chip_instance->reg;
1204 while (reg) {
1205 update_register(base_dev->chip_instance, reg);
1206 reg = reg->next;
1207 }
1208
1209 /*
1210 * Now that the device properties are all copied over, look at each bus
1211 * of the override device and run override_devicetree in a recursive
1212 * manner. The assumption here is that first bus of override device
1213 * corresponds to first bus of base device and so on. If base device has
1214 * lesser buses than override tree, then new buses are allocated for it.
1215 */
1216 struct bus *override_bus = override_dev->bus;
1217 struct bus *base_bus = base_dev->bus;
1218
1219 while (override_bus) {
1220
1221 /*
1222 * If we have more buses in override tree device, then allocate
1223 * a new bus for the base tree device as well.
1224 */
1225 if (!base_bus) {
1226 alloc_bus(base_dev);
1227 base_bus = base_dev->last_bus;
1228 }
1229
1230 override_devicetree(base_dev->bus, override_dev->bus);
1231
1232 override_bus = override_bus->next_bus;
1233 base_bus = base_bus->next_bus;
1234 }
1235
1236 delete_chip_instance(override_dev->chip_instance);
1237 override_dev->chip_instance = NULL;
1238}
1239
1240/*
1241 * Perform copy of device and properties from override parent to base parent.
1242 * This function walks through the override tree in a depth-first manner
1243 * performing following actions:
1244 * 1. If matching device is found in base tree, then copy the properties of
1245 * override device to base tree device. Call override_devicetree recursively on
1246 * the bus of override device.
1247 * 2. If matching device is not found in base tree, then set override tree
1248 * device as new child of base_parent and update the chip pointers in override
1249 * device subtree to ensure the nodes do not point to override tree chip
1250 * instance.
1251 */
1252static void override_devicetree(struct bus *base_parent,
1253 struct bus *override_parent)
1254{
1255 struct device *base_child;
1256 struct device *override_child = override_parent->children;
1257 struct device *next_child;
1258
1259 while (override_child) {
1260
1261 /* Look for a matching device in base tree. */
1262 for (base_child = base_parent->children;
1263 base_child; base_child = base_child->sibling) {
1264 if (device_match(base_child, override_child))
1265 break;
1266 }
1267
1268 next_child = override_child->sibling;
1269
1270 /*
1271 * If matching device is found, copy properties of
1272 * override_child to base_child.
1273 */
1274 if (base_child)
1275 update_device(base_child, override_child);
1276 else {
1277 /*
1278 * If matching device is not found, set override_child
1279 * as a new child of base_parent.
1280 */
1281 set_new_child(base_parent, override_child);
1282 /*
1283 * Ensure all nodes in override tree pointing to
1284 * override parent chip_instance now point to base
1285 * parent chip_instance.
1286 */
1287 update_chip_pointers(override_child,
1288 base_parent->dev->chip_instance,
1289 override_parent->dev->chip_instance);
1290 }
1291
1292 override_child = next_child;
1293 }
1294}
1295
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001296int main(int argc, char **argv)
1297{
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001298 if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT))
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001299 usage();
1300
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001301 const char *base_devtree = argv[DEVICEFILE_ARG];
1302 const char *outputc = argv[OUTPUTFILE_ARG];
1303 const char *override_devtree;
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001304
1305 parse_devicetree(base_devtree, &base_root_bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +00001306
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001307 if (argc == TOTAL_ARG_COUNT) {
1308 override_devtree = argv[OVERRIDE_DEVICEFILE_ARG];
1309 parse_devicetree(override_devtree, &override_root_bus);
Furquan Shaikh27efc502018-06-22 09:19:15 -07001310
1311 override_devicetree(&base_root_bus, &override_root_bus);
Furquan Shaikh39ac7972018-06-21 18:44:32 -07001312 }
1313
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001314 FILE *autogen = fopen(outputc, "w");
1315 if (!autogen) {
Martin Rothbec07532016-08-05 18:32:18 -06001316 fprintf(stderr, "Could not open file '%s' for writing: ",
1317 outputc);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001318 perror(NULL);
1319 exit(1);
1320 }
1321
Furquan Shaikh79e84122018-05-30 15:09:09 -07001322 emit_chips(autogen);
1323
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001324 walk_device_tree(autogen, &base_root_dev, inherit_subsystem_ids);
Martin Roth824255e2016-08-05 17:40:39 -06001325 fprintf(autogen, "\n/* pass 0 */\n");
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001326 walk_device_tree(autogen, &base_root_dev, pass0);
Furquan Shaikh93198262018-06-03 04:22:17 -07001327 fprintf(autogen, "\n/* pass 1 */\n");
Furquan Shaikhde39fc72018-06-11 04:26:45 -07001328 walk_device_tree(autogen, &base_root_dev, pass1);
Sven Schnelle270a9082011-03-01 19:58:15 +00001329
Kyösti Mälkki472d9022011-12-05 20:33:55 +02001330 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +00001331
Patrick Georgi114e7b22010-05-05 11:19:50 +00001332 return 0;
1333}