Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame^] | 1 | /* sconfig, coreboot device tree compiler */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 2 | /* |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; version 2 of the License. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Werner Zeh | ac14a40 | 2019-07-11 12:47:19 +0200 | [diff] [blame] | 14 | /* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/ |
| 15 | #include <sys/stat.h> |
Kyösti Mälkki | e29a6ac | 2019-03-15 17:05:19 +0200 | [diff] [blame] | 16 | #include <commonlib/helpers.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 17 | #include "sconfig.h" |
| 18 | #include "sconfig.tab.h" |
| 19 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 20 | extern int linenum; |
| 21 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Maintains list of all the unique chip structures for the board. |
| 24 | * This is shared across base and override device trees since we need to |
| 25 | * generate headers for all chips added by both the trees. |
| 26 | */ |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 27 | static struct chip chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 28 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 29 | typedef enum { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 30 | UNSLASH, |
| 31 | SPLIT_1ST, |
| 32 | TO_LOWER, |
| 33 | TO_UPPER, |
| 34 | } translate_t; |
| 35 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Mainboard is assumed to have a root device whose bus is the parent of all the |
| 38 | * devices that are added by parsing the devicetree file. This device has a |
| 39 | * mainboard chip instance associated with it. |
| 40 | * |
| 41 | * |
| 42 | * |
| 43 | * +------------------------+ +----------------------+ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 44 | * | Root device | | Mainboard | |
| 45 | * +---------+ (base_root_dev) +--------------->+ instance + |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 46 | * | | | chip_instance | (mainboard_instance)| |
| 47 | * | +------------------------+ | | |
| 48 | * | | +----------------------+ |
| 49 | * | | bus | |
| 50 | * | parent v | |
| 51 | * | +-------------------+ | |
| 52 | * | | Root bus | | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 53 | * +----------->+ (base_root_bus) | | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 54 | * | | | |
| 55 | * +-------------------+ | |
| 56 | * | | |
| 57 | * | children | chip |
| 58 | * v | |
| 59 | * X | |
| 60 | * (new devices will | |
| 61 | * be added here as | |
| 62 | * children) | |
| 63 | * | |
| 64 | * | |
| 65 | * | |
| 66 | * +-------+----------+ |
| 67 | * | | |
| 68 | * | Mainboard chip +----------->X (new chips will be |
| 69 | * | (mainboard_chip) | added here) |
| 70 | * | | |
| 71 | * +------------------+ |
| 72 | * |
| 73 | * |
| 74 | */ |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 75 | |
| 76 | /* Root device of primary tree. */ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 77 | static struct device base_root_dev; |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 78 | |
| 79 | /* Root device of override tree (if applicable). */ |
| 80 | static struct device override_root_dev; |
| 81 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 82 | static struct chip_instance mainboard_instance; |
| 83 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 84 | static struct bus base_root_bus = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 85 | .id = 0, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 86 | .dev = &base_root_dev, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 89 | static struct device base_root_dev = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 90 | .name = "dev_root", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 91 | .chip_instance = &mainboard_instance, |
| 92 | .path = " .type = DEVICE_PATH_ROOT ", |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 93 | .parent = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 94 | .enabled = 1, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 95 | .bus = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 98 | static struct bus override_root_bus = { |
| 99 | .id = 0, |
| 100 | .dev = &override_root_dev, |
| 101 | }; |
| 102 | |
| 103 | static struct device override_root_dev = { |
| 104 | .name = "override_root", |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Override tree root device points to the same mainboard chip instance |
| 107 | * as the base tree root device. It should not cause any side-effects |
| 108 | * since the mainboard chip instance pointer in override tree will just |
| 109 | * be ignored. |
| 110 | */ |
| 111 | .chip_instance = &mainboard_instance, |
| 112 | .path = " .type = DEVICE_PATH_ROOT ", |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 113 | .parent = &override_root_bus, |
| 114 | .enabled = 1, |
| 115 | .bus = &override_root_bus, |
| 116 | }; |
| 117 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 118 | static struct chip mainboard_chip = { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 119 | .name = "mainboard", |
| 120 | .name_underscore = "mainboard", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 121 | .instance = &mainboard_instance, |
| 122 | }; |
| 123 | |
| 124 | static struct chip_instance mainboard_instance = { |
| 125 | .id = 0, |
| 126 | .chip = &mainboard_chip, |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 129 | /* 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] | 130 | struct bus *root_parent; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 131 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 132 | struct queue_entry { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 133 | void *data; |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 134 | struct queue_entry *next; |
| 135 | struct queue_entry *prev; |
| 136 | }; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 137 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 138 | #define S_ALLOC(_s) s_alloc(__func__, _s) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 139 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 140 | static void *s_alloc(const char *f, size_t s) |
| 141 | { |
| 142 | void *data = calloc(1, s); |
| 143 | if (!data) { |
Maulik V Vaghela | 82e8c69 | 2018-06-08 10:32:08 +0530 | [diff] [blame] | 144 | fprintf(stderr, "%s: Failed to alloc mem!\n", f); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 145 | exit(1); |
| 146 | } |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 147 | return data; |
| 148 | } |
| 149 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 150 | static struct queue_entry *new_queue_entry(void *data) |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 151 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 152 | struct queue_entry *e = S_ALLOC(sizeof(*e)); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 153 | |
| 154 | e->data = data; |
| 155 | e->next = e->prev = e; |
| 156 | return e; |
| 157 | } |
| 158 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 159 | static void enqueue_tail(struct queue_entry **q_head, void *data) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 160 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 161 | struct queue_entry *tmp = new_queue_entry(data); |
| 162 | struct queue_entry *q = *q_head; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 163 | |
| 164 | if (!q) { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 165 | *q_head = tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | q->prev->next = tmp; |
| 170 | tmp->prev = q->prev; |
| 171 | q->prev = tmp; |
| 172 | tmp->next = q; |
| 173 | } |
| 174 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 175 | static void *dequeue_tail(struct queue_entry **q_head) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 176 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 177 | struct queue_entry *q = *q_head; |
| 178 | struct queue_entry *tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 179 | void *data; |
| 180 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 181 | if (!q) |
| 182 | return NULL; |
| 183 | |
| 184 | tmp = q->prev; |
| 185 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 186 | if (tmp == q) |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 187 | *q_head = NULL; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 188 | else { |
| 189 | tmp->prev->next = q; |
| 190 | q->prev = tmp->prev; |
| 191 | } |
| 192 | |
| 193 | data = tmp->data; |
| 194 | free(tmp); |
| 195 | |
| 196 | return data; |
| 197 | } |
| 198 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 199 | static void *dequeue_head(struct queue_entry **q_head) |
| 200 | { |
| 201 | struct queue_entry *q = *q_head; |
| 202 | struct queue_entry *tmp = q; |
| 203 | void *data; |
| 204 | |
| 205 | if (!q) |
| 206 | return NULL; |
| 207 | |
| 208 | if (q->next == q) |
| 209 | *q_head = NULL; |
| 210 | else { |
| 211 | q->next->prev = q->prev; |
| 212 | q->prev->next = q->next; |
| 213 | *q_head = q->next; |
| 214 | } |
| 215 | |
| 216 | data = tmp->data; |
| 217 | free(tmp); |
| 218 | |
| 219 | return data; |
| 220 | } |
| 221 | |
| 222 | static void *peek_queue_head(struct queue_entry *q_head) |
| 223 | { |
| 224 | if (!q_head) |
| 225 | return NULL; |
| 226 | |
| 227 | return q_head->data; |
| 228 | } |
| 229 | |
| 230 | static struct queue_entry *chip_q_head; |
| 231 | |
| 232 | void chip_enqueue_tail(void *data) |
| 233 | { |
| 234 | enqueue_tail(&chip_q_head, data); |
| 235 | } |
| 236 | |
| 237 | void *chip_dequeue_tail(void) |
| 238 | { |
| 239 | return dequeue_tail(&chip_q_head); |
| 240 | } |
| 241 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 242 | int yywrap(void) |
| 243 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 244 | return 1; |
| 245 | } |
| 246 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 247 | void yyerror(char const *str) |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 248 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 249 | extern char *yytext; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 250 | fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 251 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 254 | char *translate_name(const char *str, translate_t mode) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 255 | { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 256 | char *b, *c; |
| 257 | b = c = strdup(str); |
| 258 | while (c && *c) { |
| 259 | if ((mode == SPLIT_1ST) && (*c == '/')) { |
| 260 | *c = 0; |
| 261 | break; |
| 262 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 263 | if (*c == '/') |
| 264 | *c = '_'; |
| 265 | if (*c == '-') |
| 266 | *c = '_'; |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 267 | if (mode == TO_UPPER) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 268 | *c = toupper(*c); |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 269 | if (mode == TO_LOWER) |
| 270 | *c = tolower(*c); |
| 271 | c++; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 272 | } |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 273 | return b; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 276 | static struct chip *get_chip(char *path) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 277 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 278 | struct chip *h = &chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 279 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 280 | while (h->next) { |
| 281 | int result = strcmp(path, h->next->name); |
| 282 | if (result == 0) |
| 283 | return h->next; |
| 284 | |
| 285 | if (result < 0) |
| 286 | break; |
| 287 | |
| 288 | h = h->next; |
| 289 | } |
| 290 | |
| 291 | struct chip *new_chip = S_ALLOC(sizeof(struct chip)); |
| 292 | new_chip->next = h->next; |
| 293 | h->next = new_chip; |
| 294 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 295 | new_chip->chiph_exists = 1; |
| 296 | new_chip->name = path; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 297 | new_chip->name_underscore = translate_name(path, UNSLASH); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 298 | |
| 299 | struct stat st; |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 300 | char *chip_h = S_ALLOC(strlen(path) + 18); |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 301 | sprintf(chip_h, "src/%s", path); |
| 302 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) { |
Patrick Georgi | 92bcaa2 | 2015-11-13 09:39:22 +0100 | [diff] [blame] | 303 | /* root_complex gets away without a separate directory, but |
| 304 | * exists on on pretty much all AMD chipsets. |
| 305 | */ |
| 306 | if (!strstr(path, "/root_complex")) { |
Kyösti Mälkki | 6aeb4a2 | 2013-06-11 17:00:11 +0300 | [diff] [blame] | 307 | fprintf(stderr, "ERROR: Chip component %s does not exist.\n", |
| 308 | path); |
| 309 | exit(1); |
| 310 | } |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 313 | sprintf(chip_h, "src/%s/chip.h", path); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 314 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 315 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 316 | new_chip->chiph_exists = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 317 | |
Patrick Georgi | 1f68880 | 2014-08-03 15:51:19 +0200 | [diff] [blame] | 318 | free(chip_h); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 319 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 320 | return new_chip; |
| 321 | } |
| 322 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 323 | struct chip_instance *new_chip_instance(char *path) |
| 324 | { |
| 325 | struct chip *chip = get_chip(path); |
| 326 | struct chip_instance *instance = S_ALLOC(sizeof(*instance)); |
| 327 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 328 | instance->chip = chip; |
| 329 | instance->next = chip->instance; |
| 330 | chip->instance = instance; |
| 331 | |
| 332 | return instance; |
| 333 | } |
| 334 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 335 | /* |
| 336 | * Allocate a new bus for the provided device. |
| 337 | * - If this is the first bus being allocated under this device, then its id |
| 338 | * is set to 0 and bus and last_bus are pointed to the newly allocated bus. |
| 339 | * - If this is not the first bus under this device, then its id is set to 1 |
| 340 | * plus the id of last bus and newly allocated bus is added to the list of |
| 341 | * buses under the device. last_bus is updated to point to the newly |
| 342 | * allocated bus. |
| 343 | */ |
| 344 | static void alloc_bus(struct device *dev) |
| 345 | { |
| 346 | struct bus *bus = S_ALLOC(sizeof(*bus)); |
| 347 | |
| 348 | bus->dev = dev; |
| 349 | |
| 350 | if (dev->last_bus == NULL) { |
| 351 | bus->id = 0; |
| 352 | dev->bus = bus; |
| 353 | } else { |
| 354 | bus->id = dev->last_bus->id + 1; |
| 355 | dev->last_bus->next_bus = bus; |
| 356 | } |
| 357 | |
| 358 | dev->last_bus = bus; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Allocate a new device under the given parent. This function allocates a new |
| 363 | * device structure under the provided parent bus and allocates a bus structure |
| 364 | * under the newly allocated device. |
| 365 | */ |
| 366 | static struct device *alloc_dev(struct bus *parent) |
| 367 | { |
| 368 | struct device *dev = S_ALLOC(sizeof(*dev)); |
| 369 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 370 | dev->parent = parent; |
| 371 | dev->subsystem_vendor = -1; |
| 372 | dev->subsystem_device = -1; |
| 373 | |
| 374 | alloc_bus(dev); |
| 375 | |
| 376 | return dev; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * This function scans the children of given bus to see if any device matches |
| 381 | * the new device that is requested. |
| 382 | * |
| 383 | * Returns pointer to the node if found, else NULL. |
| 384 | */ |
| 385 | static struct device *get_dev(struct bus *parent, int path_a, int path_b, |
| 386 | int bustype, struct chip_instance *chip_instance) |
| 387 | { |
| 388 | struct device *child = parent->children; |
| 389 | |
| 390 | while (child) { |
| 391 | if ((child->path_a == path_a) && (child->path_b == path_b) && |
| 392 | (child->bustype == bustype) && |
| 393 | (child->chip_instance == chip_instance)) |
| 394 | return child; |
| 395 | |
| 396 | child = child->sibling; |
| 397 | } |
| 398 | |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 402 | /* |
| 403 | * Add given node as child of the provided parent. If this is the first child of |
| 404 | * the parent, update parent->children pointer as well. |
| 405 | */ |
| 406 | static void set_new_child(struct bus *parent, struct device *child) |
| 407 | { |
| 408 | struct device *c = parent->children; |
| 409 | if (c) { |
| 410 | while (c->sibling) |
| 411 | c = c->sibling; |
| 412 | c->sibling = child; |
| 413 | } else |
| 414 | parent->children = child; |
| 415 | |
| 416 | child->sibling = NULL; |
| 417 | child->parent = parent; |
| 418 | } |
| 419 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 420 | struct device *new_device(struct bus *parent, |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 421 | struct chip_instance *chip_instance, |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 422 | const int bustype, const char *devnum, |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 423 | int status) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 424 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 425 | char *tmp; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 426 | int path_a; |
| 427 | int path_b = 0; |
| 428 | struct device *new_d; |
| 429 | |
| 430 | path_a = strtol(devnum, &tmp, 16); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 431 | if (*tmp == '.') { |
| 432 | tmp++; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 433 | path_b = strtol(tmp, NULL, 16); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 436 | /* If device is found under parent, no need to allocate new device. */ |
| 437 | new_d = get_dev(parent, path_a, path_b, bustype, chip_instance); |
| 438 | if (new_d) { |
| 439 | alloc_bus(new_d); |
| 440 | return new_d; |
| 441 | } |
| 442 | |
| 443 | new_d = alloc_dev(parent); |
| 444 | |
| 445 | new_d->bustype = bustype; |
| 446 | |
| 447 | new_d->path_a = path_a; |
| 448 | new_d->path_b = path_b; |
| 449 | |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 450 | new_d->enabled = status & 0x01; |
| 451 | new_d->hidden = (status >> 1) & 0x01; |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 452 | new_d->mandatory = (status >> 2) & 0x01; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 453 | new_d->chip_instance = chip_instance; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 454 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 455 | set_new_child(parent, new_d); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 456 | |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 457 | switch (bustype) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 458 | case PCI: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 459 | 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] | 460 | break; |
| 461 | |
| 462 | case PNP: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 463 | 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] | 464 | break; |
| 465 | |
| 466 | case I2C: |
Duncan Laurie | b7ce5fe | 2016-05-07 19:49:37 -0700 | [diff] [blame] | 467 | 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] | 468 | break; |
| 469 | |
| 470 | case APIC: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 471 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 472 | break; |
| 473 | |
Stefan Reinauer | 0aa37c4 | 2013-02-12 15:20:54 -0800 | [diff] [blame] | 474 | case CPU_CLUSTER: |
| 475 | 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] | 476 | break; |
| 477 | |
Aaron Durbin | ffda804b | 2014-09-03 12:40:15 -0500 | [diff] [blame] | 478 | case CPU: |
| 479 | new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}"; |
| 480 | break; |
| 481 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 482 | case DOMAIN: |
| 483 | new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 484 | break; |
| 485 | |
| 486 | case IOAPIC: |
| 487 | new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}"; |
| 488 | break; |
Duncan Laurie | 4650f5b | 2016-05-07 20:01:34 -0700 | [diff] [blame] | 489 | |
| 490 | case GENERIC: |
| 491 | new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}"; |
| 492 | break; |
Furquan Shaikh | e670029 | 2017-02-11 00:50:38 -0800 | [diff] [blame] | 493 | |
| 494 | case SPI: |
| 495 | new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}"; |
| 496 | break; |
| 497 | |
Duncan Laurie | bae9f85 | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 498 | case USB: |
| 499 | new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}"; |
| 500 | break; |
| 501 | |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 502 | case MMIO: |
| 503 | new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}"; |
| 504 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 505 | } |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 506 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 507 | return new_d; |
| 508 | } |
| 509 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 510 | 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] | 511 | { |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 512 | struct resource *r = S_ALLOC(sizeof(struct resource)); |
| 513 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 514 | r->type = type; |
| 515 | r->index = index; |
| 516 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 517 | if (dev->res) { |
| 518 | struct resource *head = dev->res; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 519 | while (head->next) |
| 520 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 521 | head->next = r; |
| 522 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 523 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 524 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 527 | void add_resource(struct bus *bus, int type, int index, int base) |
| 528 | { |
| 529 | new_resource(bus->dev, type, index, base); |
| 530 | } |
| 531 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 532 | void add_register(struct chip_instance *chip_instance, char *name, char *val) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 533 | { |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 534 | struct reg *r = S_ALLOC(sizeof(struct reg)); |
| 535 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 536 | r->key = name; |
| 537 | r->value = val; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 538 | if (chip_instance->reg) { |
| 539 | struct reg *head = chip_instance->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 540 | // sorting to be equal to sconfig's behaviour |
| 541 | int sort = strcmp(r->key, head->key); |
| 542 | if (sort == 0) { |
| 543 | printf("ERROR: duplicate 'register' key.\n"); |
| 544 | exit(1); |
| 545 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 546 | if (sort < 0) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 547 | r->next = head; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 548 | chip_instance->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 549 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 550 | while ((head->next) |
| 551 | && (strcmp(head->next->key, r->key) < 0)) |
| 552 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 553 | r->next = head->next; |
| 554 | head->next = r; |
| 555 | } |
| 556 | } else { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 557 | chip_instance->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 561 | void add_slot_desc(struct bus *bus, char *type, char *length, char *designation, |
| 562 | char *data_width) |
| 563 | { |
| 564 | struct device *dev = bus->dev; |
| 565 | |
| 566 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
| 567 | printf("ERROR: 'slot_type' only allowed for PCI devices\n"); |
| 568 | exit(1); |
| 569 | } |
| 570 | |
| 571 | dev->smbios_slot_type = type; |
| 572 | dev->smbios_slot_length = length; |
| 573 | dev->smbios_slot_data_width = data_width; |
| 574 | dev->smbios_slot_designation = designation; |
| 575 | } |
| 576 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 577 | void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 578 | int inherit) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 579 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 580 | struct device *dev = bus->dev; |
| 581 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 582 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 583 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 584 | exit(1); |
| 585 | } |
| 586 | |
| 587 | dev->subsystem_vendor = vendor; |
| 588 | dev->subsystem_device = device; |
| 589 | dev->inherit_subsystem = inherit; |
| 590 | } |
| 591 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 592 | void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 593 | int irqpin) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 594 | { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 595 | int srcpin; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 596 | struct device *dev = bus->dev; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 597 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 598 | if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) || |
| 599 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 600 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 601 | exit(1); |
| 602 | } |
| 603 | |
| 604 | srcpin = _srcpin[3] - 'A'; |
| 605 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 606 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 607 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 608 | exit(1); |
| 609 | } |
| 610 | |
| 611 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 612 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 613 | exit(1); |
| 614 | } |
| 615 | dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin; |
| 616 | dev->pci_irq_info[srcpin].ioapic_dst_id = apicid; |
| 617 | } |
| 618 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 619 | static int dev_has_children(struct device *dev) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 620 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 621 | struct bus *bus = dev->bus; |
| 622 | |
| 623 | while (bus) { |
| 624 | if (bus->children) |
| 625 | return 1; |
| 626 | bus = bus->next_bus; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 632 | static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 633 | { |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 634 | static int dev_id; |
| 635 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 636 | if (ptr == &base_root_dev) { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 637 | fprintf(fil, "STORAGE struct bus %s_links[];\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 638 | ptr->name); |
| 639 | return; |
| 640 | } |
| 641 | |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 642 | char *name = S_ALLOC(10); |
| 643 | sprintf(name, "_dev%d", dev_id++); |
| 644 | ptr->name = name; |
| 645 | |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 646 | fprintf(fil, "STORAGE struct device %s;\n", ptr->name); |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 647 | if (ptr->res) |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 648 | fprintf(fil, "STORAGE struct resource %s_res[];\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 649 | ptr->name); |
| 650 | if (dev_has_children(ptr)) |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 651 | fprintf(fil, "STORAGE struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 652 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 653 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 654 | if (next) |
| 655 | return; |
| 656 | |
| 657 | fprintf(fil, |
| 658 | "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n", |
| 659 | ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 662 | static void emit_resources(FILE *fil, struct device *ptr) |
| 663 | { |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 664 | if (ptr->res == NULL) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 665 | return; |
| 666 | |
| 667 | int i = 1; |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 668 | fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 669 | struct resource *r = ptr->res; |
| 670 | while (r) { |
| 671 | fprintf(fil, |
| 672 | "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 673 | if (r->type == IRQ) |
| 674 | fprintf(fil, "IRQ"); |
| 675 | if (r->type == DRQ) |
| 676 | fprintf(fil, "DRQ"); |
| 677 | if (r->type == IO) |
| 678 | fprintf(fil, "IO"); |
| 679 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, |
| 680 | r->base); |
| 681 | if (r->next) |
| 682 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, |
| 683 | i++); |
| 684 | else |
| 685 | fprintf(fil, ".next=NULL },\n"); |
| 686 | r = r->next; |
| 687 | } |
| 688 | |
| 689 | fprintf(fil, "\t };\n"); |
| 690 | } |
| 691 | |
| 692 | static void emit_bus(FILE *fil, struct bus *bus) |
| 693 | { |
| 694 | fprintf(fil, "\t\t[%d] = {\n", bus->id); |
| 695 | fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id); |
| 696 | fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name); |
| 697 | if (bus->children) |
| 698 | fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name); |
| 699 | |
| 700 | if (bus->next_bus) |
| 701 | fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name, |
| 702 | bus->id + 1); |
| 703 | else |
| 704 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 705 | fprintf(fil, "\t\t},\n"); |
| 706 | } |
| 707 | |
| 708 | static void emit_dev_links(FILE *fil, struct device *ptr) |
| 709 | { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 710 | fprintf(fil, "STORAGE struct bus %s_links[] = {\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 711 | ptr->name); |
| 712 | |
| 713 | struct bus *bus = ptr->bus; |
| 714 | |
| 715 | while (bus) { |
| 716 | emit_bus(fil, bus); |
| 717 | bus = bus->next_bus; |
| 718 | } |
| 719 | |
| 720 | fprintf(fil, "\t};\n"); |
| 721 | } |
| 722 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 723 | static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 724 | { |
| 725 | int pin; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 726 | struct chip_instance *chip_ins = ptr->chip_instance; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 727 | int has_children = dev_has_children(ptr); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 728 | |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 729 | /* |
| 730 | * If the chip instance of device has base_chip_instance pointer set, then follow that |
| 731 | * to update the chip instance for current device. |
| 732 | */ |
| 733 | if (chip_ins->base_chip_instance) |
| 734 | chip_ins = chip_ins->base_chip_instance; |
| 735 | |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 736 | if (ptr == &base_root_dev) |
| 737 | fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name); |
| 738 | else |
| 739 | fprintf(fil, "STORAGE struct device %s = {\n", ptr->name); |
| 740 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 741 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Furquan Shaikh | 50ddc0b | 2018-06-23 00:59:31 -0700 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | * ops field is set to default_dev_ops_root only for the root |
| 745 | * device. For all other devices, it is set by the driver at runtime. |
| 746 | */ |
| 747 | if (ptr == &base_root_dev) |
| 748 | fprintf(fil, "\t.ops = &default_dev_ops_root,\n"); |
| 749 | else |
| 750 | fprintf(fil, "\t.ops = NULL,\n"); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 751 | fprintf(fil, "#endif\n"); |
| 752 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name, |
| 753 | ptr->parent->id); |
| 754 | fprintf(fil, "\t.path = {"); |
| 755 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 756 | fprintf(fil, "},\n"); |
| 757 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 758 | fprintf(fil, "\t.hidden = %d,\n", ptr->hidden); |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 759 | fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 760 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
| 761 | if (ptr->subsystem_vendor > 0) |
| 762 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", |
| 763 | ptr->subsystem_vendor); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 764 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 765 | if (ptr->subsystem_device > 0) |
| 766 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 767 | ptr->subsystem_device); |
| 768 | |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 769 | if (ptr->res) { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 770 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 771 | ptr->name); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 772 | } |
| 773 | if (has_children) |
| 774 | fprintf(fil, "\t.link_list = &%s_links[0],\n", |
| 775 | ptr->name); |
| 776 | else |
| 777 | fprintf(fil, "\t.link_list = NULL,\n"); |
| 778 | if (ptr->sibling) |
| 779 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
Aaron Durbin | d47afe9 | 2020-03-26 12:29:35 -0600 | [diff] [blame] | 780 | else |
| 781 | fprintf(fil, "\t.sibling = NULL,\n"); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 782 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Kyösti Mälkki | 1557a67 | 2019-06-30 10:51:31 +0300 | [diff] [blame] | 783 | for (pin = 0; pin < 4; pin++) { |
| 784 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
| 785 | fprintf(fil, |
| 786 | "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", |
| 787 | pin, ptr->pci_irq_info[pin].ioapic_irq_pin); |
| 788 | |
| 789 | if (ptr->pci_irq_info[pin].ioapic_dst_id > 0) |
| 790 | fprintf(fil, |
| 791 | "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", |
| 792 | pin, ptr->pci_irq_info[pin].ioapic_dst_id); |
| 793 | } |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 794 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", |
| 795 | chip_ins->chip->name_underscore); |
| 796 | if (chip_ins == &mainboard_instance) |
| 797 | fprintf(fil, "\t.name = mainboard_name,\n"); |
| 798 | fprintf(fil, "#endif\n"); |
| 799 | if (chip_ins->chip->chiph_exists) |
| 800 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", |
| 801 | chip_ins->chip->name_underscore, chip_ins->id); |
| 802 | if (next) |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 803 | fprintf(fil, "\t.next=&%s,\n", next->name); |
| 804 | if (ptr->smbios_slot_type || ptr->smbios_slot_data_width || |
| 805 | ptr->smbios_slot_designation || ptr->smbios_slot_length) { |
| 806 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
| 807 | fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n"); |
| 808 | } |
| 809 | /* SMBIOS types start at 1, if zero it hasn't been set */ |
| 810 | if (ptr->smbios_slot_type) |
| 811 | fprintf(fil, "\t.smbios_slot_type = %s,\n", |
| 812 | ptr->smbios_slot_type); |
| 813 | if (ptr->smbios_slot_data_width) |
| 814 | fprintf(fil, "\t.smbios_slot_data_width = %s,\n", |
| 815 | ptr->smbios_slot_data_width); |
| 816 | if (ptr->smbios_slot_designation) |
| 817 | fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n", |
| 818 | ptr->smbios_slot_designation); |
| 819 | if (ptr->smbios_slot_length) |
| 820 | fprintf(fil, "\t.smbios_slot_length = %s,\n", |
| 821 | ptr->smbios_slot_length); |
| 822 | if (ptr->smbios_slot_type || ptr->smbios_slot_data_width || |
| 823 | ptr->smbios_slot_designation || ptr->smbios_slot_length) { |
| 824 | fprintf(fil, "#endif\n"); |
| 825 | fprintf(fil, "#endif\n"); |
| 826 | } |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 827 | fprintf(fil, "};\n"); |
| 828 | |
| 829 | emit_resources(fil, ptr); |
| 830 | |
| 831 | if (has_children) |
| 832 | emit_dev_links(fil, ptr); |
| 833 | } |
| 834 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 835 | static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 836 | { |
| 837 | /* Only devices on root bus here. */ |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 838 | if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) { |
| 839 | fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d;\n", |
| 840 | ptr->path_a, ptr->path_b); |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 841 | fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pci_0_%02x_%d = &%s;\n", |
| 842 | ptr->path_a, ptr->path_b, ptr->name); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 843 | } |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 844 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 845 | if (ptr->bustype == PNP) { |
| 846 | fprintf(head, "extern DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x;\n", |
| 847 | ptr->path_a, ptr->path_b); |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 848 | fprintf(fil, "DEVTREE_CONST struct device *DEVTREE_CONST __pnp_%04x_%02x = &%s;\n", |
| 849 | ptr->path_a, ptr->path_b, ptr->name); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 850 | } |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 853 | static void add_siblings_to_queue(struct queue_entry **bfs_q_head, |
| 854 | struct device *d) |
| 855 | { |
| 856 | while (d) { |
| 857 | enqueue_tail(bfs_q_head, d); |
| 858 | d = d->sibling; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | static void add_children_to_queue(struct queue_entry **bfs_q_head, |
| 863 | struct device *d) |
| 864 | { |
| 865 | struct bus *bus = d->bus; |
| 866 | |
| 867 | while (bus) { |
| 868 | if (bus->children) |
| 869 | add_siblings_to_queue(bfs_q_head, bus->children); |
| 870 | bus = bus->next_bus; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 871 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 874 | static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr, |
| 875 | void (*func)(FILE *, FILE *, struct device *, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 876 | struct device *)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 877 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 878 | struct queue_entry *bfs_q_head = NULL; |
| 879 | |
| 880 | enqueue_tail(&bfs_q_head, ptr); |
| 881 | |
| 882 | while ((ptr = dequeue_head(&bfs_q_head))) { |
| 883 | add_children_to_queue(&bfs_q_head, ptr); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 884 | func(fil, head, ptr, peek_queue_head(bfs_q_head)); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 885 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 888 | static void emit_chip_headers(FILE *fil, struct chip *chip) |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 889 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 890 | struct chip *tmp = chip; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 891 | |
| 892 | fprintf(fil, "#include <device/device.h>\n"); |
| 893 | fprintf(fil, "#include <device/pci.h>\n"); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 894 | |
| 895 | while (chip) { |
| 896 | if (chip->chiph_exists) |
| 897 | fprintf(fil, "#include \"%s/chip.h\"\n", chip->name); |
| 898 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 899 | } |
| 900 | fprintf(fil, "\n#if !DEVTREE_EARLY\n"); |
| 901 | fprintf(fil, |
| 902 | "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 903 | |
| 904 | chip = tmp; |
| 905 | while (chip) { |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 906 | fprintf(fil, |
| 907 | "__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 908 | chip->name_underscore); |
| 909 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 910 | } |
| 911 | fprintf(fil, "#endif\n"); |
| 912 | } |
| 913 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 914 | static void emit_chip_instance(FILE *fil, struct chip_instance *instance) |
| 915 | { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 916 | fprintf(fil, "STORAGE struct %s_config %s_info_%d = {", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 917 | instance->chip->name_underscore, |
| 918 | instance->chip->name_underscore, |
| 919 | instance->id); |
| 920 | |
| 921 | if (instance->reg) { |
| 922 | fprintf(fil, "\n"); |
| 923 | struct reg *r = instance->reg; |
| 924 | while (r) { |
| 925 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 926 | r = r->next; |
| 927 | } |
| 928 | } |
| 929 | fprintf(fil, "};\n\n"); |
| 930 | } |
| 931 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 932 | static void emit_chips(FILE *fil) |
| 933 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 934 | struct chip *chip = chip_header.next; |
| 935 | struct chip_instance *instance; |
Furquan Shaikh | 9f681d2 | 2020-05-02 15:51:02 -0700 | [diff] [blame] | 936 | int chip_id; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 937 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 938 | emit_chip_headers(fil, chip); |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 939 | |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 940 | fprintf(fil, "\n#define STORAGE static __unused DEVTREE_CONST\n\n"); |
| 941 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 942 | for (; chip; chip = chip->next) { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 943 | if (!chip->chiph_exists) |
| 944 | continue; |
| 945 | |
Furquan Shaikh | 9f681d2 | 2020-05-02 15:51:02 -0700 | [diff] [blame] | 946 | chip_id = 1; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 947 | instance = chip->instance; |
| 948 | while (instance) { |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Emit this chip instance only if there is no forwarding pointer to the |
| 951 | * base tree chip instance. |
| 952 | */ |
| 953 | if (instance->base_chip_instance == NULL) { |
| 954 | instance->id = chip_id++; |
| 955 | emit_chip_instance(fil, instance); |
| 956 | } |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 957 | instance = instance->next; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 962 | static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 963 | struct device *next) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 964 | { |
| 965 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 966 | |
| 967 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 968 | /* user already gave us a subsystem vendor/device */ |
| 969 | return; |
| 970 | } |
| 971 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 972 | for (p = dev; p && p->parent->dev != p; p = p->parent->dev) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 973 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 974 | if (p->bustype != PCI && p->bustype != DOMAIN) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 975 | continue; |
| 976 | |
| 977 | if (p->inherit_subsystem) { |
| 978 | dev->subsystem_vendor = p->subsystem_vendor; |
| 979 | dev->subsystem_device = p->subsystem_device; |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 985 | static void usage(void) |
| 986 | { |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 987 | printf("usage: sconfig devicetree_file output_file header_file [override_devicetree_file]\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 988 | exit(1); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 991 | enum { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 992 | DEVICEFILE_ARG = 1, |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 993 | OUTPUTFILE_ARG, |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 994 | HEADERFILE_ARG, |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 995 | OVERRIDE_DEVICEFILE_ARG, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 996 | }; |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 997 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 998 | #define MANDATORY_ARG_COUNT 4 |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 999 | #define OPTIONAL_ARG_COUNT 1 |
| 1000 | #define TOTAL_ARG_COUNT (MANDATORY_ARG_COUNT + OPTIONAL_ARG_COUNT) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1001 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1002 | static void parse_devicetree(const char *file, struct bus *parent) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1003 | { |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1004 | FILE *filec = fopen(file, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1005 | if (!filec) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1006 | perror(NULL); |
| 1007 | exit(1); |
| 1008 | } |
| 1009 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1010 | yyrestart(filec); |
| 1011 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1012 | root_parent = parent; |
| 1013 | linenum = 0; |
| 1014 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1015 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1016 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1017 | fclose(filec); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * Match device nodes from base and override tree to see if they are the same |
| 1022 | * node. |
| 1023 | */ |
| 1024 | static int device_match(struct device *a, struct device *b) |
| 1025 | { |
| 1026 | return ((a->path_a == b->path_a) && |
| 1027 | (a->path_b == b->path_b) && |
| 1028 | (a->bustype == b->bustype) && |
| 1029 | (a->chip_instance->chip == |
| 1030 | b->chip_instance->chip)); |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1034 | * Match resource nodes from base and override tree to see if they are the same |
| 1035 | * node. |
| 1036 | */ |
| 1037 | static int res_match(struct resource *a, struct resource *b) |
| 1038 | { |
| 1039 | return ((a->type == b->type) && |
| 1040 | (a->index == b->index)); |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1044 | * Add resource to device. If resource is already present, then update its base |
| 1045 | * and index. If not, then add a new resource to the device. |
| 1046 | */ |
| 1047 | static void update_resource(struct device *dev, struct resource *res) |
| 1048 | { |
| 1049 | struct resource *base_res = dev->res; |
| 1050 | |
| 1051 | while (base_res) { |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1052 | if (res_match(base_res, res)) { |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1053 | base_res->base = res->base; |
| 1054 | return; |
| 1055 | } |
| 1056 | base_res = base_res->next; |
| 1057 | } |
| 1058 | |
| 1059 | new_resource(dev, res->type, res->index, res->base); |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Add register to chip instance. If register is already present, then update |
| 1064 | * its value. If not, then add a new register to the chip instance. |
| 1065 | */ |
| 1066 | static void update_register(struct chip_instance *c, struct reg *reg) |
| 1067 | { |
| 1068 | struct reg *base_reg = c->reg; |
| 1069 | |
| 1070 | while (base_reg) { |
| 1071 | if (!strcmp(base_reg->key, reg->key)) { |
| 1072 | base_reg->value = reg->value; |
| 1073 | return; |
| 1074 | } |
| 1075 | base_reg = base_reg->next; |
| 1076 | } |
| 1077 | |
| 1078 | add_register(c, reg->key, reg->value); |
| 1079 | } |
| 1080 | |
| 1081 | static void override_devicetree(struct bus *base_parent, |
| 1082 | struct bus *override_parent); |
| 1083 | |
| 1084 | /* |
| 1085 | * Update the base device properties using the properties of override device. In |
| 1086 | * addition to that, call override_devicetree for all the buses under the |
| 1087 | * override device. |
| 1088 | * |
| 1089 | * Override Rules: |
| 1090 | * +--------------------+--------------------------------------------+ |
| 1091 | * | | | |
| 1092 | * |struct device member| Rule | |
| 1093 | * | | | |
| 1094 | * +-----------------------------------------------------------------+ |
| 1095 | * | | | |
| 1096 | * | id | Unchanged. This is used to generate device | |
| 1097 | * | | structure name in static.c. So, no need to | |
| 1098 | * | | override. | |
| 1099 | * | | | |
| 1100 | * +-----------------------------------------------------------------+ |
| 1101 | * | | | |
| 1102 | * | enabled | Copy enabled state from override device. | |
| 1103 | * | | This allows variants to override device | |
| 1104 | * | | state. | |
| 1105 | * | | | |
| 1106 | * +-----------------------------------------------------------------+ |
| 1107 | * | | | |
| 1108 | * | subsystem_vendor | Copy from override device only if any one | |
| 1109 | * | subsystem_device | of the ids is non-zero. | |
| 1110 | * | | | |
| 1111 | * +-----------------------------------------------------------------+ |
| 1112 | * | | | |
| 1113 | * | inherit_subsystem | Copy from override device only if it is | |
| 1114 | * | | non-zero. This allows variant to only | |
| 1115 | * | | enable inherit flag for a device. | |
| 1116 | * | | | |
| 1117 | * +-----------------------------------------------------------------+ |
| 1118 | * | | | |
| 1119 | * | path | Unchanged since these are same for both | |
| 1120 | * | path_a | base and override device (Used for | |
| 1121 | * | path_b | matching devices). | |
| 1122 | * | | | |
| 1123 | * +-----------------------------------------------------------------+ |
| 1124 | * | | | |
| 1125 | * | bustype | Unchanged since this is same for both base | |
| 1126 | * | | and override device (User for matching | |
| 1127 | * | | devices). | |
| 1128 | * | | | |
| 1129 | * +-----------------------------------------------------------------+ |
| 1130 | * | | | |
| 1131 | * | pci_irq_info | Unchanged. | |
| 1132 | * | | | |
| 1133 | * +-----------------------------------------------------------------+ |
| 1134 | * | | | |
| 1135 | * | parent | Unchanged. This is meaningful only within | |
| 1136 | * | sibling | the parse tree, hence not being copied. | |
| 1137 | * | | | |
| 1138 | * +-----------------------------------------------------------------+ |
| 1139 | * | | | |
| 1140 | * | res | Each resource that is present in override | |
| 1141 | * | | device is copied over to base device: | |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1142 | * | | 1. If resource of same type and index is | |
| 1143 | * | | present in base device, then base of | |
| 1144 | * | | the resource is copied. | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1145 | * | | 2. If not, then a new resource is allocated| |
| 1146 | * | | under the base device using type, index | |
| 1147 | * | | and base from override res. | |
| 1148 | * | | | |
| 1149 | * +-----------------------------------------------------------------+ |
| 1150 | * | | | |
| 1151 | * | chip_instance | Each register of chip_instance is copied | |
| 1152 | * | | over from override device to base device: | |
| 1153 | * | | 1. If register with same key is present in | |
| 1154 | * | | base device, then value of the register | |
| 1155 | * | | is copied. | |
| 1156 | * | | 2. If not, then a new register is allocated| |
| 1157 | * | | under the base chip_instance using key | |
| 1158 | * | | and value from override register. | |
| 1159 | * | | | |
| 1160 | * +-----------------------------------------------------------------+ |
| 1161 | * | | | |
| 1162 | * | bus | Recursively call override_devicetree on | |
| 1163 | * | last_bus | each bus of override device. It is assumed | |
| 1164 | * | | that bus with id X under base device | |
| 1165 | * | | to bus with id X under override device. If | |
| 1166 | * | | override device has more buses than base | |
| 1167 | * | | device, then new buses are allocated under | |
| 1168 | * | | base device. | |
| 1169 | * | | | |
| 1170 | * +-----------------------------------------------------------------+ |
| 1171 | */ |
| 1172 | static void update_device(struct device *base_dev, struct device *override_dev) |
| 1173 | { |
| 1174 | /* |
| 1175 | * Copy the enabled state of override device to base device. This allows |
| 1176 | * override tree to enable or disable a particular device. |
| 1177 | */ |
| 1178 | base_dev->enabled = override_dev->enabled; |
| 1179 | |
| 1180 | /* |
| 1181 | * Copy subsystem vendor and device ids from override device to base |
| 1182 | * device only if the ids are non-zero in override device. Else, honor |
| 1183 | * the values in base device. |
| 1184 | */ |
| 1185 | if (override_dev->subsystem_vendor || |
| 1186 | override_dev->subsystem_device) { |
| 1187 | base_dev->subsystem_vendor = override_dev->subsystem_vendor; |
| 1188 | base_dev->subsystem_device = override_dev->subsystem_device; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * Copy value of inherity_subsystem from override device to base device |
| 1193 | * only if it is non-zero in override device. This allows override |
| 1194 | * tree to only enable inhert flag for a device. |
| 1195 | */ |
| 1196 | if (override_dev->inherit_subsystem) |
| 1197 | base_dev->inherit_subsystem = override_dev->inherit_subsystem; |
| 1198 | |
| 1199 | /* |
| 1200 | * Copy resources of override device to base device. |
| 1201 | * 1. If resource is already present in base device, then index and base |
| 1202 | * of the resource will be copied over. |
| 1203 | * 2. If resource is not already present in base device, a new resource |
| 1204 | * will be allocated. |
| 1205 | */ |
| 1206 | struct resource *res = override_dev->res; |
| 1207 | while (res) { |
| 1208 | update_resource(base_dev, res); |
| 1209 | res = res->next; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Copy registers of override chip instance to base chip instance. |
| 1214 | * 1. If register key is already present in base chip instance, then |
| 1215 | * value for the register is copied over. |
| 1216 | * 2. If register key is not already present in base chip instance, then |
| 1217 | * a new register will be allocated. |
| 1218 | */ |
| 1219 | struct reg *reg = override_dev->chip_instance->reg; |
| 1220 | while (reg) { |
| 1221 | update_register(base_dev->chip_instance, reg); |
| 1222 | reg = reg->next; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 1226 | * Update base_chip_instance member in chip instance of override tree to forward it to |
| 1227 | * the chip instance in base tree. |
| 1228 | */ |
| 1229 | override_dev->chip_instance->base_chip_instance = base_dev->chip_instance; |
| 1230 | |
| 1231 | /* |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1232 | * Now that the device properties are all copied over, look at each bus |
| 1233 | * of the override device and run override_devicetree in a recursive |
| 1234 | * manner. The assumption here is that first bus of override device |
| 1235 | * corresponds to first bus of base device and so on. If base device has |
| 1236 | * lesser buses than override tree, then new buses are allocated for it. |
| 1237 | */ |
| 1238 | struct bus *override_bus = override_dev->bus; |
| 1239 | struct bus *base_bus = base_dev->bus; |
| 1240 | |
| 1241 | while (override_bus) { |
| 1242 | |
| 1243 | /* |
| 1244 | * If we have more buses in override tree device, then allocate |
| 1245 | * a new bus for the base tree device as well. |
| 1246 | */ |
| 1247 | if (!base_bus) { |
| 1248 | alloc_bus(base_dev); |
| 1249 | base_bus = base_dev->last_bus; |
| 1250 | } |
| 1251 | |
| 1252 | override_devicetree(base_dev->bus, override_dev->bus); |
| 1253 | |
| 1254 | override_bus = override_bus->next_bus; |
| 1255 | base_bus = base_bus->next_bus; |
| 1256 | } |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Perform copy of device and properties from override parent to base parent. |
| 1261 | * This function walks through the override tree in a depth-first manner |
| 1262 | * performing following actions: |
| 1263 | * 1. If matching device is found in base tree, then copy the properties of |
| 1264 | * override device to base tree device. Call override_devicetree recursively on |
| 1265 | * the bus of override device. |
| 1266 | * 2. If matching device is not found in base tree, then set override tree |
| 1267 | * device as new child of base_parent and update the chip pointers in override |
| 1268 | * device subtree to ensure the nodes do not point to override tree chip |
| 1269 | * instance. |
| 1270 | */ |
| 1271 | static void override_devicetree(struct bus *base_parent, |
| 1272 | struct bus *override_parent) |
| 1273 | { |
| 1274 | struct device *base_child; |
| 1275 | struct device *override_child = override_parent->children; |
| 1276 | struct device *next_child; |
| 1277 | |
| 1278 | while (override_child) { |
| 1279 | |
| 1280 | /* Look for a matching device in base tree. */ |
| 1281 | for (base_child = base_parent->children; |
| 1282 | base_child; base_child = base_child->sibling) { |
| 1283 | if (device_match(base_child, override_child)) |
| 1284 | break; |
| 1285 | } |
| 1286 | |
| 1287 | next_child = override_child->sibling; |
| 1288 | |
| 1289 | /* |
| 1290 | * If matching device is found, copy properties of |
| 1291 | * override_child to base_child. |
| 1292 | */ |
| 1293 | if (base_child) |
| 1294 | update_device(base_child, override_child); |
| 1295 | else { |
| 1296 | /* |
| 1297 | * If matching device is not found, set override_child |
| 1298 | * as a new child of base_parent. |
| 1299 | */ |
| 1300 | set_new_child(base_parent, override_child); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | override_child = next_child; |
| 1304 | } |
| 1305 | } |
| 1306 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1307 | int main(int argc, char **argv) |
| 1308 | { |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1309 | if ((argc < MANDATORY_ARG_COUNT) || (argc > TOTAL_ARG_COUNT)) |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1310 | usage(); |
| 1311 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1312 | const char *base_devtree = argv[DEVICEFILE_ARG]; |
| 1313 | const char *outputc = argv[OUTPUTFILE_ARG]; |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1314 | const char *outputh = argv[HEADERFILE_ARG]; |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1315 | const char *override_devtree; |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1316 | |
| 1317 | parse_devicetree(base_devtree, &base_root_bus); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1318 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1319 | if (argc == TOTAL_ARG_COUNT) { |
| 1320 | override_devtree = argv[OVERRIDE_DEVICEFILE_ARG]; |
| 1321 | parse_devicetree(override_devtree, &override_root_bus); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1322 | |
Furquan Shaikh | 4d99b27 | 2019-04-11 15:13:25 -0700 | [diff] [blame] | 1323 | if (!dev_has_children(&override_root_dev)) { |
| 1324 | fprintf(stderr, "ERROR: Override tree needs at least one device!\n"); |
| 1325 | exit(1); |
| 1326 | } |
| 1327 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1328 | override_devicetree(&base_root_bus, &override_root_bus); |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1331 | FILE *autogen = fopen(outputc, "w"); |
| 1332 | if (!autogen) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1333 | fprintf(stderr, "Could not open file '%s' for writing: ", |
| 1334 | outputc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1335 | perror(NULL); |
| 1336 | exit(1); |
| 1337 | } |
| 1338 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1339 | FILE *autohead = fopen(outputh, "w"); |
| 1340 | if (!autohead) { |
| 1341 | fprintf(stderr, "Could not open file '%s' for writing: ", outputh); |
| 1342 | perror(NULL); |
| 1343 | fclose(autogen); |
| 1344 | exit(1); |
| 1345 | } |
| 1346 | fprintf(autohead, "#ifndef __STATIC_DEVICE_TREE_H\n"); |
| 1347 | fprintf(autohead, "#define __STATIC_DEVICE_TREE_H\n\n"); |
| 1348 | fprintf(autohead, "#include <device/device.h>\n\n"); |
| 1349 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1350 | emit_chips(autogen); |
| 1351 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1352 | walk_device_tree(autogen, autohead, &base_root_dev, inherit_subsystem_ids); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 1353 | fprintf(autogen, "\n/* pass 0 */\n"); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1354 | walk_device_tree(autogen, autohead, &base_root_dev, pass0); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1355 | fprintf(autogen, "\n/* pass 1 */\n"); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1356 | walk_device_tree(autogen, autohead, &base_root_dev, pass1); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1357 | |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1358 | /* Expose static devicenames to global namespace. */ |
| 1359 | fprintf(autogen, "\n/* expose_device_names */\n"); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1360 | walk_device_tree(autogen, autohead, &base_root_dev, expose_device_names); |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1361 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1362 | fprintf(autohead, "\n#endif /* __STATIC_DEVICE_TREE_H */\n"); |
| 1363 | fclose(autohead); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1364 | fclose(autogen); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1365 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1366 | return 0; |
| 1367 | } |