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