Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sconfig, coreboot device tree compiler |
| 3 | * |
| 4 | * Copyright (C) 2010 coresystems GmbH |
Stefan Reinauer | 2e78aa5 | 2016-05-07 01:11:14 -0700 | [diff] [blame] | 5 | * written by Patrick Georgi <patrick@georgi-clan.de> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 6 | * |
| 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 18 | #include "sconfig.h" |
| 19 | #include "sconfig.tab.h" |
| 20 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 21 | extern int linenum; |
| 22 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 23 | /* |
| 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 Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 28 | static struct chip chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 29 | |
| 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 | */ |
| 34 | static int count = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 35 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 36 | typedef enum { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 37 | UNSLASH, |
| 38 | SPLIT_1ST, |
| 39 | TO_LOWER, |
| 40 | TO_UPPER, |
| 41 | } translate_t; |
| 42 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 43 | /* |
| 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 Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 51 | * | Root device | | Mainboard | |
| 52 | * +---------+ (base_root_dev) +--------------->+ instance + |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 53 | * | | | chip_instance | (mainboard_instance)| |
| 54 | * | +------------------------+ | | |
| 55 | * | | +----------------------+ |
| 56 | * | | bus | |
| 57 | * | parent v | |
| 58 | * | +-------------------+ | |
| 59 | * | | Root bus | | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 60 | * +----------->+ (base_root_bus) | | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 61 | * | | | |
| 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 Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 82 | |
| 83 | /* Root device of primary tree. */ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 84 | static struct device base_root_dev; |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 85 | |
| 86 | /* Root device of override tree (if applicable). */ |
| 87 | static struct device override_root_dev; |
| 88 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 89 | static struct chip_instance mainboard_instance; |
| 90 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 91 | static struct bus base_root_bus = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 92 | .id = 0, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 93 | .dev = &base_root_dev, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 96 | static struct device base_root_dev = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 97 | .name = "dev_root", |
| 98 | .id = 0, |
| 99 | .chip_instance = &mainboard_instance, |
| 100 | .path = " .type = DEVICE_PATH_ROOT ", |
| 101 | .ops = "&default_dev_ops_root", |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 102 | .parent = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 103 | .enabled = 1, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 104 | .bus = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 107 | static struct bus override_root_bus = { |
| 108 | .id = 0, |
| 109 | .dev = &override_root_dev, |
| 110 | }; |
| 111 | |
| 112 | static 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 Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 129 | static struct chip mainboard_chip = { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 130 | .name = "mainboard", |
| 131 | .name_underscore = "mainboard", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 132 | .instance = &mainboard_instance, |
| 133 | }; |
| 134 | |
| 135 | static struct chip_instance mainboard_instance = { |
| 136 | .id = 0, |
| 137 | .chip = &mainboard_chip, |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 138 | .ref_count = 2, |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 141 | /* This is the parent of all devices added by parsing the devicetree file. */ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 142 | struct bus *root_parent; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 143 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 144 | struct queue_entry { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 145 | void *data; |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 146 | struct queue_entry *next; |
| 147 | struct queue_entry *prev; |
| 148 | }; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 149 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 150 | #define S_ALLOC(_s) s_alloc(__func__, _s) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 151 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 152 | static void *s_alloc(const char *f, size_t s) |
| 153 | { |
| 154 | void *data = calloc(1, s); |
| 155 | if (!data) { |
Maulik V Vaghela | 82e8c69 | 2018-06-08 10:32:08 +0530 | [diff] [blame] | 156 | fprintf(stderr, "%s: Failed to alloc mem!\n", f); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 157 | exit(1); |
| 158 | } |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 159 | return data; |
| 160 | } |
| 161 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 162 | static struct queue_entry *new_queue_entry(void *data) |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 163 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 164 | struct queue_entry *e = S_ALLOC(sizeof(*e)); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 165 | |
| 166 | e->data = data; |
| 167 | e->next = e->prev = e; |
| 168 | return e; |
| 169 | } |
| 170 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 171 | static void enqueue_tail(struct queue_entry **q_head, void *data) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 172 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 173 | struct queue_entry *tmp = new_queue_entry(data); |
| 174 | struct queue_entry *q = *q_head; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 175 | |
| 176 | if (!q) { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 177 | *q_head = tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | q->prev->next = tmp; |
| 182 | tmp->prev = q->prev; |
| 183 | q->prev = tmp; |
| 184 | tmp->next = q; |
| 185 | } |
| 186 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 187 | static void *dequeue_tail(struct queue_entry **q_head) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 188 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 189 | struct queue_entry *q = *q_head; |
| 190 | struct queue_entry *tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 191 | void *data; |
| 192 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 193 | if (!q) |
| 194 | return NULL; |
| 195 | |
| 196 | tmp = q->prev; |
| 197 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 198 | if (tmp == q) |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 199 | *q_head = NULL; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 200 | 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 Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 211 | static 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 | |
| 234 | static 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 | |
| 242 | static struct queue_entry *chip_q_head; |
| 243 | |
| 244 | void chip_enqueue_tail(void *data) |
| 245 | { |
| 246 | enqueue_tail(&chip_q_head, data); |
| 247 | } |
| 248 | |
| 249 | void *chip_dequeue_tail(void) |
| 250 | { |
| 251 | return dequeue_tail(&chip_q_head); |
| 252 | } |
| 253 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 254 | int yywrap(void) |
| 255 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 256 | return 1; |
| 257 | } |
| 258 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 259 | void yyerror(char const *str) |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 260 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 261 | extern char *yytext; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 262 | fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 263 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 266 | char *translate_name(const char *str, translate_t mode) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 267 | { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 268 | 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 Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 275 | if (*c == '/') |
| 276 | *c = '_'; |
| 277 | if (*c == '-') |
| 278 | *c = '_'; |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 279 | if (mode == TO_UPPER) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 280 | *c = toupper(*c); |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 281 | if (mode == TO_LOWER) |
| 282 | *c = tolower(*c); |
| 283 | c++; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 284 | } |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 285 | return b; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 288 | static struct chip *get_chip(char *path) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 289 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 290 | struct chip *h = &chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 291 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 292 | 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 307 | new_chip->chiph_exists = 1; |
| 308 | new_chip->name = path; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 309 | new_chip->name_underscore = translate_name(path, UNSLASH); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 310 | |
| 311 | struct stat st; |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 312 | char *chip_h = S_ALLOC(strlen(path) + 18); |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 313 | sprintf(chip_h, "src/%s", path); |
| 314 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) { |
Patrick Georgi | 92bcaa2 | 2015-11-13 09:39:22 +0100 | [diff] [blame] | 315 | /* 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älkki | 6aeb4a2 | 2013-06-11 17:00:11 +0300 | [diff] [blame] | 319 | fprintf(stderr, "ERROR: Chip component %s does not exist.\n", |
| 320 | path); |
| 321 | exit(1); |
| 322 | } |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 325 | sprintf(chip_h, "src/%s/chip.h", path); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 326 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 327 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 328 | new_chip->chiph_exists = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 329 | |
Patrick Georgi | 1f68880 | 2014-08-03 15:51:19 +0200 | [diff] [blame] | 330 | free(chip_h); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 331 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 332 | return new_chip; |
| 333 | } |
| 334 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 335 | struct 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 Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 348 | static 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 Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 396 | /* |
| 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 | */ |
| 405 | static 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 | */ |
| 427 | static 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 | */ |
| 447 | static 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 Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 464 | /* |
| 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 | */ |
| 468 | static 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 Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 482 | struct device *new_device(struct bus *parent, |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 483 | struct chip_instance *chip_instance, |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 484 | const int bustype, const char *devnum, |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 485 | int enabled) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 486 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 487 | char *tmp; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 488 | int path_a; |
| 489 | int path_b = 0; |
| 490 | struct device *new_d; |
| 491 | |
| 492 | path_a = strtol(devnum, &tmp, 16); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 493 | if (*tmp == '.') { |
| 494 | tmp++; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 495 | path_b = strtol(tmp, NULL, 16); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 498 | /* 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 Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 512 | char *name = S_ALLOC(10); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 513 | sprintf(name, "_dev%d", new_d->id); |
| 514 | new_d->name = name; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 515 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 516 | new_d->enabled = enabled; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 517 | new_d->chip_instance = chip_instance; |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 518 | chip_instance->ref_count++; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 519 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 520 | set_new_child(parent, new_d); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 521 | |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 522 | switch (bustype) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 523 | case PCI: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 524 | new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 525 | break; |
| 526 | |
| 527 | case PNP: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 528 | new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | case I2C: |
Duncan Laurie | b7ce5fe | 2016-05-07 19:49:37 -0700 | [diff] [blame] | 532 | new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 533 | break; |
| 534 | |
| 535 | case APIC: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 536 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 537 | break; |
| 538 | |
Stefan Reinauer | 0aa37c4 | 2013-02-12 15:20:54 -0800 | [diff] [blame] | 539 | case CPU_CLUSTER: |
| 540 | new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 541 | break; |
| 542 | |
Aaron Durbin | ffda804b | 2014-09-03 12:40:15 -0500 | [diff] [blame] | 543 | case CPU: |
| 544 | new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}"; |
| 545 | break; |
| 546 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 547 | case DOMAIN: |
| 548 | new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 549 | break; |
| 550 | |
| 551 | case IOAPIC: |
| 552 | new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}"; |
| 553 | break; |
Duncan Laurie | 4650f5b | 2016-05-07 20:01:34 -0700 | [diff] [blame] | 554 | |
| 555 | case GENERIC: |
| 556 | new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}"; |
| 557 | break; |
Furquan Shaikh | e670029 | 2017-02-11 00:50:38 -0800 | [diff] [blame] | 558 | |
| 559 | case SPI: |
| 560 | new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}"; |
| 561 | break; |
| 562 | |
Duncan Laurie | bae9f85 | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 563 | case USB: |
| 564 | new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}"; |
| 565 | break; |
| 566 | |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 567 | case MMIO: |
| 568 | new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}"; |
| 569 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 570 | } |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 571 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 572 | return new_d; |
| 573 | } |
| 574 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 575 | static void new_resource(struct device *dev, int type, int index, int base) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 576 | { |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 577 | struct resource *r = S_ALLOC(sizeof(struct resource)); |
| 578 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 579 | r->type = type; |
| 580 | r->index = index; |
| 581 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 582 | if (dev->res) { |
| 583 | struct resource *head = dev->res; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 584 | while (head->next) |
| 585 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 586 | head->next = r; |
| 587 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 588 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 589 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 592 | void add_resource(struct bus *bus, int type, int index, int base) |
| 593 | { |
| 594 | new_resource(bus->dev, type, index, base); |
| 595 | } |
| 596 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 597 | void add_register(struct chip_instance *chip_instance, char *name, char *val) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 598 | { |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 599 | struct reg *r = S_ALLOC(sizeof(struct reg)); |
| 600 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 601 | r->key = name; |
| 602 | r->value = val; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 603 | if (chip_instance->reg) { |
| 604 | struct reg *head = chip_instance->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 605 | // 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 Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 611 | if (sort < 0) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 612 | r->next = head; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 613 | chip_instance->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 614 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 615 | while ((head->next) |
| 616 | && (strcmp(head->next->key, r->key) < 0)) |
| 617 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 618 | r->next = head->next; |
| 619 | head->next = r; |
| 620 | } |
| 621 | } else { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 622 | chip_instance->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 626 | void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 627 | int inherit) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 628 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 629 | struct device *dev = bus->dev; |
| 630 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 631 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 632 | 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 Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 641 | void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 642 | int irqpin) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 643 | { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 644 | int srcpin; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 645 | struct device *dev = bus->dev; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 646 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 647 | if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) || |
| 648 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 649 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 650 | exit(1); |
| 651 | } |
| 652 | |
| 653 | srcpin = _srcpin[3] - 'A'; |
| 654 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 655 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 656 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 657 | exit(1); |
| 658 | } |
| 659 | |
| 660 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 661 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 662 | 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 Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 668 | static int dev_has_children(struct device *dev) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 669 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 670 | 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 | |
| 681 | static void pass0(FILE *fil, struct device *ptr, struct device *next) |
| 682 | { |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 683 | if (ptr == &base_root_dev) { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 684 | 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 Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 690 | if (ptr->res) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 691 | fprintf(fil, "DEVTREE_CONST struct resource %s_res[];\n", |
| 692 | ptr->name); |
| 693 | if (dev_has_children(ptr)) |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 694 | fprintf(fil, "DEVTREE_CONST struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 695 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 696 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 697 | if (next) |
| 698 | return; |
| 699 | |
| 700 | fprintf(fil, |
| 701 | "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n", |
| 702 | ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 705 | static void emit_resources(FILE *fil, struct device *ptr) |
| 706 | { |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 707 | if (ptr->res == NULL) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 708 | 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 | |
| 735 | static 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 | |
| 751 | static 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 | |
| 766 | static void pass1(FILE *fil, struct device *ptr, struct device *next) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 767 | { |
| 768 | int pin; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 769 | struct chip_instance *chip_ins = ptr->chip_instance; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 770 | int has_children = dev_has_children(ptr); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 771 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 772 | if (ptr != &base_root_dev) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 773 | 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 Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 788 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 789 | for (pin = 0; pin < 4; pin++) { |
| 790 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 791 | fprintf(fil, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 792 | "\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 Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 799 | } |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 800 | |
| 801 | if (ptr->subsystem_device > 0) |
| 802 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 803 | ptr->subsystem_device); |
| 804 | |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 805 | if (ptr->res) { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 806 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 807 | ptr->name); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 808 | } |
| 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 | |
| 835 | static 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 | |
| 844 | static 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 Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 853 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 856 | static void walk_device_tree(FILE *fil, struct device *ptr, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 857 | void (*func)(FILE *, struct device *, |
| 858 | struct device *)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 859 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 860 | 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 870 | static void emit_chip_headers(FILE *fil, struct chip *chip) |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 871 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 872 | struct chip *tmp = chip; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 873 | |
| 874 | fprintf(fil, "#include <device/device.h>\n"); |
| 875 | fprintf(fil, "#include <device/pci.h>\n"); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 876 | |
| 877 | while (chip) { |
| 878 | if (chip->chiph_exists) |
| 879 | fprintf(fil, "#include \"%s/chip.h\"\n", chip->name); |
| 880 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 881 | } |
| 882 | fprintf(fil, "\n#if !DEVTREE_EARLY\n"); |
| 883 | fprintf(fil, |
| 884 | "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 885 | |
| 886 | chip = tmp; |
| 887 | while (chip) { |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 888 | fprintf(fil, |
| 889 | "__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 890 | chip->name_underscore); |
| 891 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 892 | } |
| 893 | fprintf(fil, "#endif\n"); |
| 894 | } |
| 895 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 896 | static 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 Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 914 | static void emit_chips(FILE *fil) |
| 915 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 916 | struct chip *chip = chip_header.next; |
| 917 | struct chip_instance *instance; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 918 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 919 | emit_chip_headers(fil, chip); |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 920 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 921 | for (; chip; chip = chip->next) { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 922 | if (!chip->chiph_exists) |
| 923 | continue; |
| 924 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 925 | instance = chip->instance; |
| 926 | while (instance) { |
| 927 | emit_chip_instance(fil, instance); |
| 928 | instance = instance->next; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 933 | static void inherit_subsystem_ids(FILE *file, struct device *dev, |
| 934 | struct device *next) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 935 | { |
| 936 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 937 | |
| 938 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 939 | /* user already gave us a subsystem vendor/device */ |
| 940 | return; |
| 941 | } |
| 942 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 943 | for (p = dev; p && p->parent->dev != p; p = p->parent->dev) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 944 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 945 | if (p->bustype != PCI && p->bustype != DOMAIN) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 946 | 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älkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 956 | static void usage(void) |
| 957 | { |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 958 | printf("usage: sconfig devicetree_file output_file [override_devicetree_file]\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 959 | exit(1); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 962 | enum { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 963 | DEVICEFILE_ARG = 1, |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 964 | OUTPUTFILE_ARG, |
| 965 | OVERRIDE_DEVICEFILE_ARG, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 966 | }; |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 967 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 968 | #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älkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 971 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 972 | static void parse_devicetree(const char *file, struct bus *parent) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 973 | { |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 974 | FILE *filec = fopen(file, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 975 | if (!filec) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 976 | perror(NULL); |
| 977 | exit(1); |
| 978 | } |
| 979 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 980 | yyrestart(filec); |
| 981 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 982 | root_parent = parent; |
| 983 | linenum = 0; |
| 984 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 985 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 986 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 987 | fclose(filec); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 990 | /* |
| 991 | * Match device nodes from base and override tree to see if they are the same |
| 992 | * node. |
| 993 | */ |
| 994 | static 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 | */ |
| 1010 | static 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 | */ |
| 1030 | static 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 | */ |
| 1050 | static 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 | |
| 1065 | static 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 | */ |
| 1156 | static 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 | */ |
| 1252 | static 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 Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1296 | int main(int argc, char **argv) |
| 1297 | { |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1298 | if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT)) |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1299 | usage(); |
| 1300 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1301 | const char *base_devtree = argv[DEVICEFILE_ARG]; |
| 1302 | const char *outputc = argv[OUTPUTFILE_ARG]; |
| 1303 | const char *override_devtree; |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1304 | |
| 1305 | parse_devicetree(base_devtree, &base_root_bus); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1306 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1307 | if (argc == TOTAL_ARG_COUNT) { |
| 1308 | override_devtree = argv[OVERRIDE_DEVICEFILE_ARG]; |
| 1309 | parse_devicetree(override_devtree, &override_root_bus); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame^] | 1310 | |
| 1311 | override_devicetree(&base_root_bus, &override_root_bus); |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1314 | FILE *autogen = fopen(outputc, "w"); |
| 1315 | if (!autogen) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1316 | fprintf(stderr, "Could not open file '%s' for writing: ", |
| 1317 | outputc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1318 | perror(NULL); |
| 1319 | exit(1); |
| 1320 | } |
| 1321 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1322 | emit_chips(autogen); |
| 1323 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1324 | walk_device_tree(autogen, &base_root_dev, inherit_subsystem_ids); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 1325 | fprintf(autogen, "\n/* pass 0 */\n"); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1326 | walk_device_tree(autogen, &base_root_dev, pass0); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1327 | fprintf(autogen, "\n/* pass 1 */\n"); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1328 | walk_device_tree(autogen, &base_root_dev, pass1); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1329 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1330 | fclose(autogen); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1331 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |