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