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 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 4 | #include <assert.h> |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 5 | #include <ctype.h> |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 6 | #include <getopt.h> |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 7 | #include <inttypes.h> |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 8 | #include <libgen.h> |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 9 | /* stat.h needs to be included before commonlib/helpers.h to avoid errors.*/ |
Werner Zeh | ac14a40 | 2019-07-11 12:47:19 +0200 | [diff] [blame] | 10 | #include <sys/stat.h> |
Kyösti Mälkki | e29a6ac | 2019-03-15 17:05:19 +0200 | [diff] [blame] | 11 | #include <commonlib/helpers.h> |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 12 | #include <stdint.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 13 | #include "sconfig.h" |
| 14 | #include "sconfig.tab.h" |
| 15 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 16 | extern int linenum; |
| 17 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Maintains list of all the unique chip structures for the board. |
| 20 | * This is shared across base and override device trees since we need to |
| 21 | * generate headers for all chips added by both the trees. |
| 22 | */ |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 23 | static struct chip chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 24 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 25 | typedef enum { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 26 | UNSLASH, |
| 27 | SPLIT_1ST, |
| 28 | TO_LOWER, |
| 29 | TO_UPPER, |
| 30 | } translate_t; |
| 31 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Mainboard is assumed to have a root device whose bus is the parent of all the |
| 34 | * devices that are added by parsing the devicetree file. This device has a |
| 35 | * mainboard chip instance associated with it. |
| 36 | * |
| 37 | * |
| 38 | * |
| 39 | * +------------------------+ +----------------------+ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 40 | * | Root device | | Mainboard | |
| 41 | * +---------+ (base_root_dev) +--------------->+ instance + |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 42 | * | | | chip_instance | (mainboard_instance)| |
| 43 | * | +------------------------+ | | |
| 44 | * | | +----------------------+ |
| 45 | * | | bus | |
| 46 | * | parent v | |
| 47 | * | +-------------------+ | |
| 48 | * | | Root bus | | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 49 | * +----------->+ (base_root_bus) | | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 50 | * | | | |
| 51 | * +-------------------+ | |
| 52 | * | | |
| 53 | * | children | chip |
| 54 | * v | |
| 55 | * X | |
| 56 | * (new devices will | |
| 57 | * be added here as | |
| 58 | * children) | |
| 59 | * | |
| 60 | * | |
| 61 | * | |
| 62 | * +-------+----------+ |
| 63 | * | | |
| 64 | * | Mainboard chip +----------->X (new chips will be |
| 65 | * | (mainboard_chip) | added here) |
| 66 | * | | |
| 67 | * +------------------+ |
| 68 | * |
| 69 | * |
| 70 | */ |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 71 | |
| 72 | /* Root device of primary tree. */ |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 73 | static struct device base_root_dev; |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 74 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 75 | /* Root device of chipset tree. */ |
| 76 | static struct device chipset_root_dev; |
| 77 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 78 | /* Root device of override tree (if applicable). */ |
| 79 | static struct device override_root_dev; |
| 80 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 81 | static struct chip_instance mainboard_instance; |
| 82 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 83 | static struct bus base_root_bus = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 84 | .id = 0, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 85 | .dev = &base_root_dev, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 88 | static struct device base_root_dev = { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 89 | .name = "dev_root", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 90 | .chip_instance = &mainboard_instance, |
| 91 | .path = " .type = DEVICE_PATH_ROOT ", |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 92 | .parent = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 93 | .enabled = 1, |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 94 | .bus = &base_root_bus, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 97 | static struct bus chipset_root_bus = { |
| 98 | .id = 0, |
| 99 | .dev = &chipset_root_dev, |
| 100 | }; |
| 101 | |
| 102 | static struct device chipset_root_dev = { |
| 103 | .name = "chipset_root", |
| 104 | .chip_instance = &mainboard_instance, |
| 105 | .path = " .type = DEVICE_PATH_ROOT ", |
| 106 | .parent = &chipset_root_bus, |
| 107 | .enabled = 1, |
| 108 | .bus = &chipset_root_bus, |
| 109 | }; |
| 110 | |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 111 | static struct bus override_root_bus = { |
| 112 | .id = 0, |
| 113 | .dev = &override_root_dev, |
| 114 | }; |
| 115 | |
| 116 | static struct device override_root_dev = { |
| 117 | .name = "override_root", |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Override tree root device points to the same mainboard chip instance |
| 120 | * as the base tree root device. It should not cause any side-effects |
| 121 | * since the mainboard chip instance pointer in override tree will just |
| 122 | * be ignored. |
| 123 | */ |
| 124 | .chip_instance = &mainboard_instance, |
| 125 | .path = " .type = DEVICE_PATH_ROOT ", |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 126 | .parent = &override_root_bus, |
| 127 | .enabled = 1, |
| 128 | .bus = &override_root_bus, |
| 129 | }; |
| 130 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 131 | static struct chip mainboard_chip = { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 132 | .name = "mainboard", |
| 133 | .name_underscore = "mainboard", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 134 | .instance = &mainboard_instance, |
| 135 | }; |
| 136 | |
| 137 | static struct chip_instance mainboard_instance = { |
| 138 | .id = 0, |
| 139 | .chip = &mainboard_chip, |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 142 | /* 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] | 143 | struct bus *root_parent; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 144 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 145 | struct queue_entry { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 146 | void *data; |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 147 | struct queue_entry *next; |
| 148 | struct queue_entry *prev; |
| 149 | }; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 150 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 151 | #define S_ALLOC(_s) s_alloc(__func__, _s) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 152 | |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 153 | static void *s_alloc(const char *f, size_t s) |
| 154 | { |
| 155 | void *data = calloc(1, s); |
| 156 | if (!data) { |
Maulik V Vaghela | 82e8c69 | 2018-06-08 10:32:08 +0530 | [diff] [blame] | 157 | fprintf(stderr, "%s: Failed to alloc mem!\n", f); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 158 | exit(1); |
| 159 | } |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 160 | return data; |
| 161 | } |
| 162 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 163 | static struct queue_entry *new_queue_entry(void *data) |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 164 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 165 | struct queue_entry *e = S_ALLOC(sizeof(*e)); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 166 | |
| 167 | e->data = data; |
| 168 | e->next = e->prev = e; |
| 169 | return e; |
| 170 | } |
| 171 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 172 | static void enqueue_tail(struct queue_entry **q_head, void *data) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 173 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 174 | struct queue_entry *tmp = new_queue_entry(data); |
| 175 | struct queue_entry *q = *q_head; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 176 | |
| 177 | if (!q) { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 178 | *q_head = tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | |
| 182 | q->prev->next = tmp; |
| 183 | tmp->prev = q->prev; |
| 184 | q->prev = tmp; |
| 185 | tmp->next = q; |
| 186 | } |
| 187 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 188 | static void *dequeue_tail(struct queue_entry **q_head) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 189 | { |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 190 | struct queue_entry *q = *q_head; |
| 191 | struct queue_entry *tmp; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 192 | void *data; |
| 193 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 194 | if (!q) |
| 195 | return NULL; |
| 196 | |
| 197 | tmp = q->prev; |
| 198 | |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 199 | if (tmp == q) |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 200 | *q_head = NULL; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 201 | else { |
| 202 | tmp->prev->next = q; |
| 203 | q->prev = tmp->prev; |
| 204 | } |
| 205 | |
| 206 | data = tmp->data; |
| 207 | free(tmp); |
| 208 | |
| 209 | return data; |
| 210 | } |
| 211 | |
Furquan Shaikh | 7398ded | 2018-06-03 10:42:49 -0700 | [diff] [blame] | 212 | static void *dequeue_head(struct queue_entry **q_head) |
| 213 | { |
| 214 | struct queue_entry *q = *q_head; |
| 215 | struct queue_entry *tmp = q; |
| 216 | void *data; |
| 217 | |
| 218 | if (!q) |
| 219 | return NULL; |
| 220 | |
| 221 | if (q->next == q) |
| 222 | *q_head = NULL; |
| 223 | else { |
| 224 | q->next->prev = q->prev; |
| 225 | q->prev->next = q->next; |
| 226 | *q_head = q->next; |
| 227 | } |
| 228 | |
| 229 | data = tmp->data; |
| 230 | free(tmp); |
| 231 | |
| 232 | return data; |
| 233 | } |
| 234 | |
| 235 | static void *peek_queue_head(struct queue_entry *q_head) |
| 236 | { |
| 237 | if (!q_head) |
| 238 | return NULL; |
| 239 | |
| 240 | return q_head->data; |
| 241 | } |
| 242 | |
| 243 | static struct queue_entry *chip_q_head; |
| 244 | |
| 245 | void chip_enqueue_tail(void *data) |
| 246 | { |
| 247 | enqueue_tail(&chip_q_head, data); |
| 248 | } |
| 249 | |
| 250 | void *chip_dequeue_tail(void) |
| 251 | { |
| 252 | return dequeue_tail(&chip_q_head); |
| 253 | } |
| 254 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 255 | int yywrap(void) |
| 256 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 257 | return 1; |
| 258 | } |
| 259 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 260 | void yyerror(char const *str) |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 261 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 262 | extern char *yytext; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 263 | fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 264 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 267 | char *translate_name(const char *str, translate_t mode) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 268 | { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 269 | char *b, *c; |
| 270 | b = c = strdup(str); |
| 271 | while (c && *c) { |
| 272 | if ((mode == SPLIT_1ST) && (*c == '/')) { |
| 273 | *c = 0; |
| 274 | break; |
| 275 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 276 | if (*c == '/') |
| 277 | *c = '_'; |
| 278 | if (*c == '-') |
| 279 | *c = '_'; |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 280 | if (mode == TO_UPPER) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 281 | *c = toupper(*c); |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 282 | if (mode == TO_LOWER) |
| 283 | *c = tolower(*c); |
| 284 | c++; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 285 | } |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 286 | return b; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 289 | static struct chip *get_chip(char *path) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 290 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 291 | struct chip *h = &chip_header; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 292 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 293 | while (h->next) { |
| 294 | int result = strcmp(path, h->next->name); |
| 295 | if (result == 0) |
| 296 | return h->next; |
| 297 | |
| 298 | if (result < 0) |
| 299 | break; |
| 300 | |
| 301 | h = h->next; |
| 302 | } |
| 303 | |
| 304 | struct chip *new_chip = S_ALLOC(sizeof(struct chip)); |
| 305 | new_chip->next = h->next; |
| 306 | h->next = new_chip; |
| 307 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 308 | new_chip->chiph_exists = 1; |
| 309 | new_chip->name = path; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 310 | new_chip->name_underscore = translate_name(path, UNSLASH); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 311 | |
| 312 | struct stat st; |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 313 | char *chip_h = S_ALLOC(strlen(path) + 18); |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 314 | sprintf(chip_h, "src/%s", path); |
| 315 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) { |
Patrick Georgi | 92bcaa2 | 2015-11-13 09:39:22 +0100 | [diff] [blame] | 316 | /* root_complex gets away without a separate directory, but |
| 317 | * exists on on pretty much all AMD chipsets. |
| 318 | */ |
| 319 | if (!strstr(path, "/root_complex")) { |
Kyösti Mälkki | 6aeb4a2 | 2013-06-11 17:00:11 +0300 | [diff] [blame] | 320 | fprintf(stderr, "ERROR: Chip component %s does not exist.\n", |
| 321 | path); |
| 322 | exit(1); |
| 323 | } |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 326 | sprintf(chip_h, "src/%s/chip.h", path); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 327 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 328 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 329 | new_chip->chiph_exists = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 330 | |
Patrick Georgi | 1f68880 | 2014-08-03 15:51:19 +0200 | [diff] [blame] | 331 | free(chip_h); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 332 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 333 | return new_chip; |
| 334 | } |
| 335 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 336 | struct chip_instance *new_chip_instance(char *path) |
| 337 | { |
| 338 | struct chip *chip = get_chip(path); |
| 339 | struct chip_instance *instance = S_ALLOC(sizeof(*instance)); |
| 340 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 341 | instance->chip = chip; |
| 342 | instance->next = chip->instance; |
| 343 | chip->instance = instance; |
| 344 | |
| 345 | return instance; |
| 346 | } |
| 347 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 348 | /* List of fw_config fields added during parsing. */ |
| 349 | static struct fw_config_field *fw_config_fields; |
| 350 | |
| 351 | static struct fw_config_option *find_fw_config_option(struct fw_config_field *field, |
| 352 | const char *name) |
| 353 | { |
| 354 | struct fw_config_option *option = field->options; |
| 355 | |
| 356 | while (option && option->name) { |
| 357 | if (!strcmp(option->name, name)) |
| 358 | return option; |
| 359 | option = option->next; |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | static struct fw_config_field *find_fw_config_field(const char *name) |
| 365 | { |
| 366 | struct fw_config_field *field = fw_config_fields; |
| 367 | |
| 368 | while (field && field->name) { |
| 369 | if (!strcmp(field->name, name)) |
| 370 | return field; |
| 371 | field = field->next; |
| 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | struct fw_config_field *get_fw_config_field(const char *name) |
| 377 | { |
| 378 | struct fw_config_field *field = find_fw_config_field(name); |
| 379 | |
| 380 | /* Fail if the field does not exist, new fields must be added with a mask. */ |
| 381 | if (!field) { |
| 382 | printf("ERROR: fw_config field not found: %s\n", name); |
| 383 | exit(1); |
| 384 | } |
| 385 | return field; |
| 386 | } |
| 387 | |
| 388 | static void append_fw_config_field(struct fw_config_field *add) |
| 389 | { |
| 390 | struct fw_config_field *field = fw_config_fields; |
| 391 | |
| 392 | if (!fw_config_fields) { |
| 393 | fw_config_fields = add; |
| 394 | } else { |
| 395 | while (field && field->next) |
| 396 | field = field->next; |
| 397 | field->next = add; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | struct fw_config_field *new_fw_config_field(const char *name, |
| 402 | unsigned int start_bit, unsigned int end_bit) |
| 403 | { |
| 404 | struct fw_config_field *field = find_fw_config_field(name); |
| 405 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 406 | /* Check that field is within 64 bits. */ |
| 407 | if (start_bit > end_bit || end_bit > 63) { |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 408 | printf("ERROR: fw_config field %s has invalid range %u-%u\n", name, |
| 409 | start_bit, end_bit); |
| 410 | exit(1); |
| 411 | } |
| 412 | |
| 413 | /* Don't allow re-defining a field, only adding new fields. */ |
| 414 | if (field) { |
| 415 | printf("ERROR: fw_config field %s[%u-%u] already exists with range %u-%u\n", |
| 416 | name, start_bit, end_bit, field->start_bit, field->end_bit); |
| 417 | exit(1); |
| 418 | } |
| 419 | |
| 420 | /* Check for overlap with an existing field. */ |
| 421 | field = fw_config_fields; |
| 422 | while (field) { |
| 423 | /* Check if the mask overlaps. */ |
| 424 | if (start_bit <= field->end_bit && end_bit >= field->start_bit) { |
| 425 | printf("ERROR: fw_config field %s[%u-%u] overlaps %s[%u-%u]\n", |
| 426 | name, start_bit, end_bit, |
| 427 | field->name, field->start_bit, field->end_bit); |
| 428 | exit(1); |
| 429 | } |
| 430 | field = field->next; |
| 431 | } |
| 432 | |
| 433 | field = S_ALLOC(sizeof(*field)); |
| 434 | field->name = name; |
| 435 | field->start_bit = start_bit; |
| 436 | field->end_bit = end_bit; |
| 437 | append_fw_config_field(field); |
| 438 | |
| 439 | return field; |
| 440 | } |
| 441 | |
| 442 | static void append_fw_config_option_to_field(struct fw_config_field *field, |
| 443 | struct fw_config_option *add) |
| 444 | { |
| 445 | struct fw_config_option *option = field->options; |
| 446 | |
| 447 | if (!option) { |
| 448 | field->options = add; |
| 449 | } else { |
| 450 | while (option && option->next) |
| 451 | option = option->next; |
| 452 | option->next = add; |
| 453 | } |
| 454 | } |
| 455 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 456 | void add_fw_config_option(struct fw_config_field *field, const char *name, uint64_t value) |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 457 | { |
| 458 | struct fw_config_option *option; |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 459 | uint64_t field_max_value; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 460 | |
| 461 | /* Check that option value fits within field mask. */ |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 462 | field_max_value = (1ull << (1ull + field->end_bit - field->start_bit)) - 1ull; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 463 | if (value > field_max_value) { |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 464 | printf("ERROR: fw_config option %s:%s value %" PRIx64 " larger than field max %" |
| 465 | PRIx64 "\n", |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 466 | field->name, name, value, field_max_value); |
| 467 | exit(1); |
| 468 | } |
| 469 | |
| 470 | /* Check for existing option with this name or value. */ |
| 471 | option = field->options; |
| 472 | while (option) { |
| 473 | if (!strcmp(option->name, name)) { |
| 474 | printf("ERROR: fw_config option name %s:%s already exists\n", |
| 475 | field->name, name); |
| 476 | exit(1); |
| 477 | } |
| 478 | /* Compare values. */ |
| 479 | if (value == option->value) { |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 480 | printf("ERROR: fw_config option %s:%s[%" PRIx64 "] redefined as %s\n", |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 481 | field->name, option->name, value, name); |
| 482 | exit(1); |
| 483 | } |
| 484 | option = option->next; |
| 485 | } |
| 486 | |
| 487 | option = S_ALLOC(sizeof(*option)); |
| 488 | option->name = name; |
| 489 | option->value = value; |
| 490 | |
| 491 | /* Add option to the current field. */ |
| 492 | append_fw_config_option_to_field(field, option); |
| 493 | } |
| 494 | |
| 495 | static void append_fw_config_probe_to_dev(struct device *dev, struct fw_config_probe *add) |
| 496 | { |
| 497 | struct fw_config_probe *probe = dev->probe; |
| 498 | |
| 499 | if (!probe) { |
| 500 | dev->probe = add; |
| 501 | } else { |
| 502 | while (probe && probe->next) |
| 503 | probe = probe->next; |
| 504 | probe->next = add; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void add_fw_config_probe(struct bus *bus, const char *field, const char *option) |
| 509 | { |
| 510 | struct fw_config_probe *probe; |
| 511 | |
| 512 | probe = bus->dev->probe; |
| 513 | while (probe) { |
| 514 | if (!strcmp(probe->field, field) && !strcmp(probe->option, option)) { |
| 515 | printf("ERROR: fw_config probe %s:%s already exists\n", field, option); |
| 516 | exit(1); |
| 517 | } |
| 518 | probe = probe->next; |
| 519 | } |
| 520 | |
| 521 | probe = S_ALLOC(sizeof(*probe)); |
| 522 | probe->field = field; |
| 523 | probe->option = option; |
| 524 | |
| 525 | append_fw_config_probe_to_dev(bus->dev, probe); |
| 526 | } |
| 527 | |
| 528 | static void emit_fw_config(FILE *fil) |
| 529 | { |
| 530 | struct fw_config_field *field = fw_config_fields; |
| 531 | |
| 532 | if (!field) |
| 533 | return; |
| 534 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 535 | while (field) { |
| 536 | struct fw_config_option *option = field->options; |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 537 | uint64_t mask; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 538 | |
| 539 | fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n", |
| 540 | field->name, field->name); |
| 541 | |
| 542 | /* Compute mask from start and end bit. */ |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 543 | mask = ((1ull << (1ull + field->end_bit - field->start_bit)) - 1ull); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 544 | mask <<= field->start_bit; |
| 545 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 546 | fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%" PRIx64 "\n", |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 547 | field->name, mask); |
| 548 | |
| 549 | while (option) { |
| 550 | fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n", |
| 551 | field->name, option->name, option->name); |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 552 | fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%" |
| 553 | PRIx64 "\n", field->name, option->name, |
| 554 | option->value << field->start_bit); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 555 | |
| 556 | option = option->next; |
| 557 | } |
| 558 | |
| 559 | field = field->next; |
| 560 | } |
| 561 | |
| 562 | fprintf(fil, "\n"); |
| 563 | } |
| 564 | |
| 565 | static int emit_fw_config_probe(FILE *fil, struct device *dev) |
| 566 | { |
| 567 | struct fw_config_probe *probe = dev->probe; |
| 568 | |
| 569 | fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name); |
| 570 | |
| 571 | while (probe) { |
| 572 | /* Find matching field. */ |
| 573 | struct fw_config_field *field; |
| 574 | struct fw_config_option *option; |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 575 | uint64_t mask, value; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 576 | |
| 577 | field = find_fw_config_field(probe->field); |
| 578 | if (!field) { |
| 579 | printf("ERROR: fw_config_probe field %s not found\n", probe->field); |
| 580 | return -1; |
| 581 | } |
| 582 | option = find_fw_config_option(field, probe->option); |
| 583 | if (!option) { |
| 584 | printf("ERROR: fw_config_probe field %s option %s not found\n", |
| 585 | probe->field, probe->option); |
| 586 | return -1; |
| 587 | } |
| 588 | |
| 589 | /* Fill out the probe structure with values from emit_fw_config(). */ |
| 590 | fprintf(fil, "\t{\n"); |
| 591 | fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field); |
| 592 | fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n", |
| 593 | probe->field, probe->option); |
| 594 | fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field); |
| 595 | fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n", |
| 596 | probe->field, probe->option); |
| 597 | fprintf(fil, "\t},\n"); |
| 598 | |
| 599 | probe = probe->next; |
| 600 | } |
| 601 | |
| 602 | /* Add empty entry to mark end of list. */ |
| 603 | fprintf(fil, "\t{ }\n};\n"); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 607 | /* |
| 608 | * Allocate a new bus for the provided device. |
| 609 | * - If this is the first bus being allocated under this device, then its id |
| 610 | * is set to 0 and bus and last_bus are pointed to the newly allocated bus. |
| 611 | * - If this is not the first bus under this device, then its id is set to 1 |
| 612 | * plus the id of last bus and newly allocated bus is added to the list of |
| 613 | * buses under the device. last_bus is updated to point to the newly |
| 614 | * allocated bus. |
| 615 | */ |
| 616 | static void alloc_bus(struct device *dev) |
| 617 | { |
| 618 | struct bus *bus = S_ALLOC(sizeof(*bus)); |
| 619 | |
| 620 | bus->dev = dev; |
| 621 | |
| 622 | if (dev->last_bus == NULL) { |
| 623 | bus->id = 0; |
| 624 | dev->bus = bus; |
| 625 | } else { |
| 626 | bus->id = dev->last_bus->id + 1; |
| 627 | dev->last_bus->next_bus = bus; |
| 628 | } |
| 629 | |
| 630 | dev->last_bus = bus; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Allocate a new device under the given parent. This function allocates a new |
| 635 | * device structure under the provided parent bus and allocates a bus structure |
| 636 | * under the newly allocated device. |
| 637 | */ |
| 638 | static struct device *alloc_dev(struct bus *parent) |
| 639 | { |
| 640 | struct device *dev = S_ALLOC(sizeof(*dev)); |
| 641 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 642 | dev->parent = parent; |
| 643 | dev->subsystem_vendor = -1; |
| 644 | dev->subsystem_device = -1; |
| 645 | |
| 646 | alloc_bus(dev); |
| 647 | |
| 648 | return dev; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * This function scans the children of given bus to see if any device matches |
| 653 | * the new device that is requested. |
| 654 | * |
| 655 | * Returns pointer to the node if found, else NULL. |
| 656 | */ |
| 657 | static struct device *get_dev(struct bus *parent, int path_a, int path_b, |
| 658 | int bustype, struct chip_instance *chip_instance) |
| 659 | { |
| 660 | struct device *child = parent->children; |
| 661 | |
| 662 | while (child) { |
| 663 | if ((child->path_a == path_a) && (child->path_b == path_b) && |
| 664 | (child->bustype == bustype) && |
| 665 | (child->chip_instance == chip_instance)) |
| 666 | return child; |
| 667 | |
| 668 | child = child->sibling; |
| 669 | } |
| 670 | |
| 671 | return NULL; |
| 672 | } |
| 673 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Add given node as child of the provided parent. If this is the first child of |
| 676 | * the parent, update parent->children pointer as well. |
| 677 | */ |
| 678 | static void set_new_child(struct bus *parent, struct device *child) |
| 679 | { |
| 680 | struct device *c = parent->children; |
| 681 | if (c) { |
| 682 | while (c->sibling) |
| 683 | c = c->sibling; |
| 684 | c->sibling = child; |
| 685 | } else |
| 686 | parent->children = child; |
| 687 | |
| 688 | child->sibling = NULL; |
| 689 | child->parent = parent; |
| 690 | } |
| 691 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 692 | static const struct device *find_alias(const struct device *const parent, |
| 693 | const char *const alias) |
| 694 | { |
| 695 | if (parent->alias && !strcmp(parent->alias, alias)) |
| 696 | return parent; |
| 697 | |
| 698 | const struct bus *bus; |
| 699 | for (bus = parent->bus; bus; bus = bus->next_bus) { |
| 700 | const struct device *child; |
| 701 | for (child = bus->children; child; child = child->sibling) { |
| 702 | const struct device *const ret = find_alias(child, alias); |
| 703 | if (ret) |
| 704 | return ret; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return NULL; |
| 709 | } |
| 710 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 711 | static struct device *new_device_with_path(struct bus *parent, |
| 712 | struct chip_instance *chip_instance, |
| 713 | const int bustype, int path_a, int path_b, |
| 714 | char *alias, int status) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 715 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 716 | struct device *new_d; |
| 717 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 718 | /* If device is found under parent, no need to allocate new device. */ |
| 719 | new_d = get_dev(parent, path_a, path_b, bustype, chip_instance); |
| 720 | if (new_d) { |
| 721 | alloc_bus(new_d); |
| 722 | return new_d; |
| 723 | } |
| 724 | |
| 725 | new_d = alloc_dev(parent); |
| 726 | |
| 727 | new_d->bustype = bustype; |
| 728 | |
| 729 | new_d->path_a = path_a; |
| 730 | new_d->path_b = path_b; |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 731 | new_d->alias = alias; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 732 | |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 733 | new_d->enabled = status & 0x01; |
| 734 | new_d->hidden = (status >> 1) & 0x01; |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 735 | new_d->mandatory = (status >> 2) & 0x01; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 736 | new_d->chip_instance = chip_instance; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 737 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 738 | set_new_child(parent, new_d); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 739 | |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 740 | switch (bustype) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 741 | case PCI: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 742 | 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] | 743 | break; |
| 744 | |
| 745 | case PNP: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 746 | 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] | 747 | break; |
| 748 | |
| 749 | case I2C: |
Duncan Laurie | b7ce5fe | 2016-05-07 19:49:37 -0700 | [diff] [blame] | 750 | 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] | 751 | break; |
| 752 | |
| 753 | case APIC: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 754 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 755 | break; |
| 756 | |
Stefan Reinauer | 0aa37c4 | 2013-02-12 15:20:54 -0800 | [diff] [blame] | 757 | case CPU_CLUSTER: |
| 758 | 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] | 759 | break; |
| 760 | |
Aaron Durbin | ffda804b | 2014-09-03 12:40:15 -0500 | [diff] [blame] | 761 | case CPU: |
| 762 | new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}"; |
| 763 | break; |
| 764 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 765 | case DOMAIN: |
| 766 | new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | case IOAPIC: |
| 770 | new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}"; |
| 771 | break; |
Duncan Laurie | 4650f5b | 2016-05-07 20:01:34 -0700 | [diff] [blame] | 772 | |
| 773 | case GENERIC: |
| 774 | new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}"; |
| 775 | break; |
Furquan Shaikh | e670029 | 2017-02-11 00:50:38 -0800 | [diff] [blame] | 776 | |
| 777 | case SPI: |
| 778 | new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}"; |
| 779 | break; |
| 780 | |
Duncan Laurie | bae9f85 | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 781 | case USB: |
| 782 | new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}"; |
| 783 | break; |
| 784 | |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 785 | case MMIO: |
| 786 | new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}"; |
| 787 | break; |
Raul E Rangel | 3f3f53c | 2020-05-06 11:47:04 -0600 | [diff] [blame] | 788 | |
| 789 | case ESPI: |
| 790 | new_d->path = ".type=DEVICE_PATH_ESPI,{.espi={ .addr = 0x%x }}"; |
| 791 | break; |
| 792 | |
| 793 | case LPC: |
| 794 | new_d->path = ".type=DEVICE_PATH_LPC,{.lpc={ .addr = 0x%x }}"; |
| 795 | break; |
Michael Niewöhner | dbb667a | 2020-12-11 21:26:02 +0100 | [diff] [blame] | 796 | |
| 797 | case GPIO: |
| 798 | new_d->path = ".type=DEVICE_PATH_GPIO,{.gpio={ .id = 0x%x }}"; |
| 799 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 800 | } |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 801 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 802 | return new_d; |
| 803 | } |
| 804 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 805 | struct device *new_device_reference(struct bus *parent, |
| 806 | struct chip_instance *chip_instance, |
| 807 | const char *reference, int status) |
| 808 | { |
| 809 | const struct device *dev = find_alias(&base_root_dev, reference); |
| 810 | |
| 811 | if (!dev) { |
| 812 | printf("ERROR: Unable to find device reference %s\n", reference); |
| 813 | exit(1); |
| 814 | } |
| 815 | |
| 816 | return new_device_with_path(parent, chip_instance, dev->bustype, dev->path_a, |
| 817 | dev->path_b, NULL, status); |
| 818 | } |
| 819 | |
| 820 | struct device *new_device_raw(struct bus *parent, |
| 821 | struct chip_instance *chip_instance, |
| 822 | const int bustype, const char *devnum, |
| 823 | char *alias, int status) |
| 824 | { |
| 825 | char *tmp; |
| 826 | int path_a; |
| 827 | int path_b = 0; |
| 828 | |
| 829 | /* Check for alias name conflicts. */ |
| 830 | if (alias && find_alias(root_parent->dev, alias)) { |
| 831 | printf("ERROR: Alias already exists: %s\n", alias); |
| 832 | exit(1); |
| 833 | } |
| 834 | |
| 835 | path_a = strtol(devnum, &tmp, 16); |
| 836 | if (*tmp == '.') { |
| 837 | tmp++; |
| 838 | path_b = strtol(tmp, NULL, 16); |
| 839 | } |
| 840 | |
| 841 | return new_device_with_path(parent, chip_instance, bustype, path_a, path_b, alias, |
| 842 | status); |
| 843 | } |
| 844 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 845 | 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] | 846 | { |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 847 | struct resource *r = S_ALLOC(sizeof(struct resource)); |
| 848 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 849 | r->type = type; |
| 850 | r->index = index; |
| 851 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 852 | if (dev->res) { |
| 853 | struct resource *head = dev->res; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 854 | while (head->next) |
| 855 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 856 | head->next = r; |
| 857 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 858 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 859 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 862 | void add_resource(struct bus *bus, int type, int index, int base) |
| 863 | { |
| 864 | new_resource(bus->dev, type, index, base); |
| 865 | } |
| 866 | |
Nico Huber | d09459f | 2020-05-26 22:13:09 +0200 | [diff] [blame] | 867 | static void add_reg(struct reg **const head, char *const name, char *const val) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 868 | { |
Nico Huber | d09459f | 2020-05-26 22:13:09 +0200 | [diff] [blame] | 869 | struct reg *const r = S_ALLOC(sizeof(struct reg)); |
| 870 | struct reg *prev = NULL; |
| 871 | struct reg *cur; |
Furquan Shaikh | 369e1f0 | 2018-05-31 09:48:51 -0700 | [diff] [blame] | 872 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 873 | r->key = name; |
| 874 | r->value = val; |
Nico Huber | d09459f | 2020-05-26 22:13:09 +0200 | [diff] [blame] | 875 | |
| 876 | for (cur = *head; cur != NULL; prev = cur, cur = cur->next) { |
| 877 | const int sort = strcmp(r->key, cur->key); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 878 | if (sort == 0) { |
Patrick Georgi | 5193312 | 2020-11-02 18:08:14 +0100 | [diff] [blame] | 879 | printf("ERROR: duplicate 'register' key '%s'.\n", r->key); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 880 | exit(1); |
| 881 | } |
Nico Huber | d09459f | 2020-05-26 22:13:09 +0200 | [diff] [blame] | 882 | if (sort < 0) |
| 883 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 884 | } |
Nico Huber | d09459f | 2020-05-26 22:13:09 +0200 | [diff] [blame] | 885 | r->next = cur; |
| 886 | if (prev) |
| 887 | prev->next = r; |
| 888 | else |
| 889 | *head = r; |
| 890 | } |
| 891 | |
| 892 | void add_register(struct chip_instance *chip_instance, char *name, char *val) |
| 893 | { |
| 894 | add_reg(&chip_instance->reg, name, val); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 897 | void add_reference(struct chip_instance *const chip_instance, |
| 898 | char *const name, char *const alias) |
| 899 | { |
| 900 | add_reg(&chip_instance->ref, name, alias); |
| 901 | } |
| 902 | |
| 903 | static void set_reference(struct chip_instance *const chip_instance, |
| 904 | char *const name, char *const alias) |
| 905 | { |
| 906 | const struct device *const dev = find_alias(&base_root_dev, alias); |
| 907 | if (!dev) { |
| 908 | printf("ERROR: Cannot find device alias '%s'.\n", alias); |
| 909 | exit(1); |
| 910 | } |
| 911 | |
| 912 | char *const ref_name = S_ALLOC(strlen(dev->name) + 2); |
| 913 | sprintf(ref_name, "&%s", dev->name); |
| 914 | add_register(chip_instance, name, ref_name); |
| 915 | } |
| 916 | |
| 917 | static void update_references(FILE *file, FILE *head, struct device *dev, |
| 918 | struct device *next) |
| 919 | { |
| 920 | struct reg *ref; |
| 921 | |
| 922 | for (ref = dev->chip_instance->ref; ref; ref = ref->next) |
| 923 | set_reference(dev->chip_instance, ref->key, ref->value); |
| 924 | } |
| 925 | |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 926 | void add_slot_desc(struct bus *bus, char *type, char *length, char *designation, |
| 927 | char *data_width) |
| 928 | { |
| 929 | struct device *dev = bus->dev; |
| 930 | |
| 931 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
| 932 | printf("ERROR: 'slot_type' only allowed for PCI devices\n"); |
| 933 | exit(1); |
| 934 | } |
| 935 | |
| 936 | dev->smbios_slot_type = type; |
| 937 | dev->smbios_slot_length = length; |
| 938 | dev->smbios_slot_data_width = data_width; |
| 939 | dev->smbios_slot_designation = designation; |
| 940 | } |
| 941 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 942 | void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 943 | int inherit) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 944 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 945 | struct device *dev = bus->dev; |
| 946 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 947 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 948 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 949 | exit(1); |
| 950 | } |
| 951 | |
| 952 | dev->subsystem_vendor = vendor; |
| 953 | dev->subsystem_device = device; |
| 954 | dev->inherit_subsystem = inherit; |
| 955 | } |
| 956 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 957 | void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 958 | int irqpin) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 959 | { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 960 | int srcpin; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 961 | struct device *dev = bus->dev; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 962 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 963 | if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) || |
| 964 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 965 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 966 | exit(1); |
| 967 | } |
| 968 | |
| 969 | srcpin = _srcpin[3] - 'A'; |
| 970 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 971 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 972 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 973 | exit(1); |
| 974 | } |
| 975 | |
| 976 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 977 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 978 | exit(1); |
| 979 | } |
| 980 | dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin; |
| 981 | dev->pci_irq_info[srcpin].ioapic_dst_id = apicid; |
| 982 | } |
| 983 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 984 | static int dev_has_children(struct device *dev) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 985 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 986 | struct bus *bus = dev->bus; |
| 987 | |
| 988 | while (bus) { |
| 989 | if (bus->children) |
| 990 | return 1; |
| 991 | bus = bus->next_bus; |
| 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 997 | 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] | 998 | { |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 999 | static int dev_id; |
| 1000 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1001 | if (ptr == &base_root_dev) { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1002 | fprintf(fil, "STORAGE struct bus %s_links[];\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1003 | ptr->name); |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 1007 | char *name = S_ALLOC(10); |
| 1008 | sprintf(name, "_dev%d", dev_id++); |
| 1009 | ptr->name = name; |
| 1010 | |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1011 | fprintf(fil, "STORAGE struct device %s;\n", ptr->name); |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 1012 | if (ptr->res) |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1013 | fprintf(fil, "STORAGE struct resource %s_res[];\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1014 | ptr->name); |
| 1015 | if (dev_has_children(ptr)) |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1016 | fprintf(fil, "STORAGE struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1017 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 1018 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1019 | if (next) |
| 1020 | return; |
| 1021 | |
| 1022 | fprintf(fil, |
| 1023 | "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n", |
| 1024 | ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1027 | static void emit_resources(FILE *fil, struct device *ptr) |
| 1028 | { |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 1029 | if (ptr->res == NULL) |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1030 | return; |
| 1031 | |
| 1032 | int i = 1; |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1033 | fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1034 | struct resource *r = ptr->res; |
| 1035 | while (r) { |
| 1036 | fprintf(fil, |
| 1037 | "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 1038 | if (r->type == IRQ) |
| 1039 | fprintf(fil, "IRQ"); |
| 1040 | if (r->type == DRQ) |
| 1041 | fprintf(fil, "DRQ"); |
| 1042 | if (r->type == IO) |
| 1043 | fprintf(fil, "IO"); |
| 1044 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, |
| 1045 | r->base); |
| 1046 | if (r->next) |
| 1047 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, |
| 1048 | i++); |
| 1049 | else |
| 1050 | fprintf(fil, ".next=NULL },\n"); |
| 1051 | r = r->next; |
| 1052 | } |
| 1053 | |
| 1054 | fprintf(fil, "\t };\n"); |
| 1055 | } |
| 1056 | |
| 1057 | static void emit_bus(FILE *fil, struct bus *bus) |
| 1058 | { |
| 1059 | fprintf(fil, "\t\t[%d] = {\n", bus->id); |
| 1060 | fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id); |
| 1061 | fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name); |
| 1062 | if (bus->children) |
| 1063 | fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name); |
| 1064 | |
| 1065 | if (bus->next_bus) |
| 1066 | fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name, |
| 1067 | bus->id + 1); |
| 1068 | else |
| 1069 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 1070 | fprintf(fil, "\t\t},\n"); |
| 1071 | } |
| 1072 | |
| 1073 | static void emit_dev_links(FILE *fil, struct device *ptr) |
| 1074 | { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1075 | fprintf(fil, "STORAGE struct bus %s_links[] = {\n", |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1076 | ptr->name); |
| 1077 | |
| 1078 | struct bus *bus = ptr->bus; |
| 1079 | |
| 1080 | while (bus) { |
| 1081 | emit_bus(fil, bus); |
| 1082 | bus = bus->next_bus; |
| 1083 | } |
| 1084 | |
| 1085 | fprintf(fil, "\t};\n"); |
| 1086 | } |
| 1087 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1088 | 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] | 1089 | { |
| 1090 | int pin; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1091 | struct chip_instance *chip_ins = ptr->chip_instance; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1092 | int has_children = dev_has_children(ptr); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1093 | |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * If the chip instance of device has base_chip_instance pointer set, then follow that |
| 1096 | * to update the chip instance for current device. |
| 1097 | */ |
| 1098 | if (chip_ins->base_chip_instance) |
| 1099 | chip_ins = chip_ins->base_chip_instance; |
| 1100 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 1101 | /* Emit probe structures. */ |
| 1102 | if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) { |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1103 | if (head) |
| 1104 | fclose(head); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 1105 | fclose(fil); |
| 1106 | exit(1); |
| 1107 | } |
| 1108 | |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1109 | if (ptr == &base_root_dev) |
| 1110 | fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name); |
| 1111 | else |
| 1112 | fprintf(fil, "STORAGE struct device %s = {\n", ptr->name); |
| 1113 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1114 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Furquan Shaikh | 50ddc0b | 2018-06-23 00:59:31 -0700 | [diff] [blame] | 1115 | |
| 1116 | /* |
| 1117 | * ops field is set to default_dev_ops_root only for the root |
| 1118 | * device. For all other devices, it is set by the driver at runtime. |
| 1119 | */ |
| 1120 | if (ptr == &base_root_dev) |
| 1121 | fprintf(fil, "\t.ops = &default_dev_ops_root,\n"); |
| 1122 | else |
| 1123 | fprintf(fil, "\t.ops = NULL,\n"); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1124 | fprintf(fil, "#endif\n"); |
| 1125 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name, |
| 1126 | ptr->parent->id); |
| 1127 | fprintf(fil, "\t.path = {"); |
| 1128 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 1129 | fprintf(fil, "},\n"); |
| 1130 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 1131 | fprintf(fil, "\t.hidden = %d,\n", ptr->hidden); |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 1132 | fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1133 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
| 1134 | if (ptr->subsystem_vendor > 0) |
| 1135 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", |
| 1136 | ptr->subsystem_vendor); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1137 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1138 | if (ptr->subsystem_device > 0) |
| 1139 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 1140 | ptr->subsystem_device); |
| 1141 | |
Furquan Shaikh | 4ca3a8a | 2018-06-07 23:36:45 -0700 | [diff] [blame] | 1142 | if (ptr->res) { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1143 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1144 | ptr->name); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1145 | } |
| 1146 | if (has_children) |
| 1147 | fprintf(fil, "\t.link_list = &%s_links[0],\n", |
| 1148 | ptr->name); |
| 1149 | else |
| 1150 | fprintf(fil, "\t.link_list = NULL,\n"); |
| 1151 | if (ptr->sibling) |
| 1152 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
Aaron Durbin | d47afe9 | 2020-03-26 12:29:35 -0600 | [diff] [blame] | 1153 | else |
| 1154 | fprintf(fil, "\t.sibling = NULL,\n"); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1155 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 1156 | if (ptr->probe) |
| 1157 | fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name); |
Kyösti Mälkki | 1557a67 | 2019-06-30 10:51:31 +0300 | [diff] [blame] | 1158 | for (pin = 0; pin < 4; pin++) { |
| 1159 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
| 1160 | fprintf(fil, |
| 1161 | "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", |
| 1162 | pin, ptr->pci_irq_info[pin].ioapic_irq_pin); |
| 1163 | |
| 1164 | if (ptr->pci_irq_info[pin].ioapic_dst_id > 0) |
| 1165 | fprintf(fil, |
| 1166 | "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", |
| 1167 | pin, ptr->pci_irq_info[pin].ioapic_dst_id); |
| 1168 | } |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1169 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", |
| 1170 | chip_ins->chip->name_underscore); |
| 1171 | if (chip_ins == &mainboard_instance) |
| 1172 | fprintf(fil, "\t.name = mainboard_name,\n"); |
| 1173 | fprintf(fil, "#endif\n"); |
| 1174 | if (chip_ins->chip->chiph_exists) |
| 1175 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", |
| 1176 | chip_ins->chip->name_underscore, chip_ins->id); |
| 1177 | if (next) |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 1178 | fprintf(fil, "\t.next=&%s,\n", next->name); |
| 1179 | if (ptr->smbios_slot_type || ptr->smbios_slot_data_width || |
| 1180 | ptr->smbios_slot_designation || ptr->smbios_slot_length) { |
| 1181 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
| 1182 | fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n"); |
| 1183 | } |
| 1184 | /* SMBIOS types start at 1, if zero it hasn't been set */ |
| 1185 | if (ptr->smbios_slot_type) |
| 1186 | fprintf(fil, "\t.smbios_slot_type = %s,\n", |
| 1187 | ptr->smbios_slot_type); |
| 1188 | if (ptr->smbios_slot_data_width) |
| 1189 | fprintf(fil, "\t.smbios_slot_data_width = %s,\n", |
| 1190 | ptr->smbios_slot_data_width); |
| 1191 | if (ptr->smbios_slot_designation) |
| 1192 | fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n", |
| 1193 | ptr->smbios_slot_designation); |
| 1194 | if (ptr->smbios_slot_length) |
| 1195 | fprintf(fil, "\t.smbios_slot_length = %s,\n", |
| 1196 | ptr->smbios_slot_length); |
| 1197 | if (ptr->smbios_slot_type || ptr->smbios_slot_data_width || |
| 1198 | ptr->smbios_slot_designation || ptr->smbios_slot_length) { |
| 1199 | fprintf(fil, "#endif\n"); |
| 1200 | fprintf(fil, "#endif\n"); |
| 1201 | } |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1202 | fprintf(fil, "};\n"); |
| 1203 | |
| 1204 | emit_resources(fil, ptr); |
| 1205 | |
| 1206 | if (has_children) |
| 1207 | emit_dev_links(fil, ptr); |
| 1208 | } |
| 1209 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1210 | 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] | 1211 | { |
| 1212 | /* Only devices on root bus here. */ |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1213 | if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) { |
Furquan Shaikh | 708f25e | 2021-01-08 09:54:54 -0800 | [diff] [blame] | 1214 | fprintf(head, "extern DEVTREE_CONST struct device *const __pci_0_%02x_%d;\n", |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1215 | ptr->path_a, ptr->path_b); |
Furquan Shaikh | 708f25e | 2021-01-08 09:54:54 -0800 | [diff] [blame] | 1216 | fprintf(fil, "DEVTREE_CONST struct device *const __pci_0_%02x_%d = &%s;\n", |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1217 | ptr->path_a, ptr->path_b, ptr->name); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1218 | } |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1219 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1220 | if (ptr->bustype == PNP) { |
Furquan Shaikh | 708f25e | 2021-01-08 09:54:54 -0800 | [diff] [blame] | 1221 | fprintf(head, "extern DEVTREE_CONST struct device *const __pnp_%04x_%02x;\n", |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1222 | ptr->path_a, ptr->path_b); |
Furquan Shaikh | 708f25e | 2021-01-08 09:54:54 -0800 | [diff] [blame] | 1223 | fprintf(fil, "DEVTREE_CONST struct device *const __pnp_%04x_%02x = &%s;\n", |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1224 | ptr->path_a, ptr->path_b, ptr->name); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1225 | } |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1228 | static void add_siblings_to_queue(struct queue_entry **bfs_q_head, |
| 1229 | struct device *d) |
| 1230 | { |
| 1231 | while (d) { |
| 1232 | enqueue_tail(bfs_q_head, d); |
| 1233 | d = d->sibling; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static void add_children_to_queue(struct queue_entry **bfs_q_head, |
| 1238 | struct device *d) |
| 1239 | { |
| 1240 | struct bus *bus = d->bus; |
| 1241 | |
| 1242 | while (bus) { |
| 1243 | if (bus->children) |
| 1244 | add_siblings_to_queue(bfs_q_head, bus->children); |
| 1245 | bus = bus->next_bus; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1246 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1249 | static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr, |
| 1250 | void (*func)(FILE *, FILE *, struct device *, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1251 | struct device *)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1252 | { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1253 | struct queue_entry *bfs_q_head = NULL; |
| 1254 | |
| 1255 | enqueue_tail(&bfs_q_head, ptr); |
| 1256 | |
| 1257 | while ((ptr = dequeue_head(&bfs_q_head))) { |
| 1258 | add_children_to_queue(&bfs_q_head, ptr); |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1259 | func(fil, head, ptr, peek_queue_head(bfs_q_head)); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1260 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1263 | static void emit_chip_headers(FILE *fil, struct chip *chip) |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 1264 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1265 | struct chip *tmp = chip; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 1266 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1267 | while (chip) { |
| 1268 | if (chip->chiph_exists) |
| 1269 | fprintf(fil, "#include \"%s/chip.h\"\n", chip->name); |
| 1270 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 1271 | } |
| 1272 | fprintf(fil, "\n#if !DEVTREE_EARLY\n"); |
| 1273 | fprintf(fil, |
| 1274 | "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1275 | |
| 1276 | chip = tmp; |
| 1277 | while (chip) { |
Kyösti Mälkki | b2a10f8 | 2019-08-17 06:28:40 +0300 | [diff] [blame] | 1278 | /* A lot of cpus do not define chip_operations at all, and the ones |
| 1279 | that do only initialise .name. */ |
| 1280 | if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) { |
| 1281 | fprintf(fil, |
| 1282 | "__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
| 1283 | chip->name_underscore); |
| 1284 | } else { |
| 1285 | fprintf(fil, "extern struct chip_operations %s_ops;\n", |
| 1286 | chip->name_underscore); |
| 1287 | } |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1288 | chip = chip->next; |
Furquan Shaikh | a0cc5a6 | 2018-05-30 23:46:16 -0700 | [diff] [blame] | 1289 | } |
| 1290 | fprintf(fil, "#endif\n"); |
| 1291 | } |
| 1292 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1293 | static void emit_chip_instance(FILE *fil, struct chip_instance *instance) |
| 1294 | { |
Kyösti Mälkki | 5ccce7c | 2019-03-15 10:04:11 +0200 | [diff] [blame] | 1295 | fprintf(fil, "STORAGE struct %s_config %s_info_%d = {", |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1296 | instance->chip->name_underscore, |
| 1297 | instance->chip->name_underscore, |
| 1298 | instance->id); |
| 1299 | |
| 1300 | if (instance->reg) { |
| 1301 | fprintf(fil, "\n"); |
| 1302 | struct reg *r = instance->reg; |
| 1303 | while (r) { |
| 1304 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 1305 | r = r->next; |
| 1306 | } |
| 1307 | } |
| 1308 | fprintf(fil, "};\n\n"); |
| 1309 | } |
| 1310 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1311 | static void emit_chip_configs(FILE *fil) |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1312 | { |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1313 | struct chip *chip = chip_header.next; |
| 1314 | struct chip_instance *instance; |
Furquan Shaikh | 9f681d2 | 2020-05-02 15:51:02 -0700 | [diff] [blame] | 1315 | int chip_id; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1316 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1317 | for (; chip; chip = chip->next) { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1318 | if (!chip->chiph_exists) |
| 1319 | continue; |
| 1320 | |
Furquan Shaikh | 9f681d2 | 2020-05-02 15:51:02 -0700 | [diff] [blame] | 1321 | chip_id = 1; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1322 | instance = chip->instance; |
| 1323 | while (instance) { |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 1324 | /* |
| 1325 | * Emit this chip instance only if there is no forwarding pointer to the |
| 1326 | * base tree chip instance. |
| 1327 | */ |
| 1328 | if (instance->base_chip_instance == NULL) { |
| 1329 | instance->id = chip_id++; |
| 1330 | emit_chip_instance(fil, instance); |
| 1331 | } |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 1332 | instance = instance->next; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1337 | static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev, |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1338 | struct device *next) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1339 | { |
| 1340 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 1343 | /* user already gave us a subsystem vendor/device */ |
| 1344 | return; |
| 1345 | } |
| 1346 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 1347 | for (p = dev; p && p->parent->dev != p; p = p->parent->dev) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1348 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1349 | if (p->bustype != PCI && p->bustype != DOMAIN) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1350 | continue; |
| 1351 | |
| 1352 | if (p->inherit_subsystem) { |
| 1353 | dev->subsystem_vendor = p->subsystem_vendor; |
| 1354 | dev->subsystem_device = p->subsystem_device; |
| 1355 | break; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1360 | static void usage(void) |
| 1361 | { |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1362 | printf("usage: sconfig <options>\n"); |
| 1363 | printf(" -c | --output_c : Path to output static.c file (required)\n"); |
| 1364 | printf(" -r | --output_h : Path to header static.h file (required)\n"); |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1365 | printf(" -d | --output_d : Path to header static_devices.h file (required)\n"); |
| 1366 | printf(" -f | --output_f : Path to header static_fw_config.h file (required)\n"); |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1367 | printf(" -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n"); |
| 1368 | printf(" -o | --override_devtree : Path to override devicetree file (optional)\n"); |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 1369 | printf(" -p | --chipset_devtree : Path to chipset/SOC devicetree file (optional)\n"); |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1370 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1371 | exit(1); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1374 | static void parse_devicetree(const char *file, struct bus *parent) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1375 | { |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1376 | FILE *filec = fopen(file, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1377 | if (!filec) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1378 | perror(NULL); |
| 1379 | exit(1); |
| 1380 | } |
| 1381 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1382 | yyrestart(filec); |
| 1383 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1384 | root_parent = parent; |
| 1385 | linenum = 0; |
| 1386 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1387 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1388 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1389 | fclose(filec); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * Match device nodes from base and override tree to see if they are the same |
| 1394 | * node. |
| 1395 | */ |
| 1396 | static int device_match(struct device *a, struct device *b) |
| 1397 | { |
| 1398 | return ((a->path_a == b->path_a) && |
| 1399 | (a->path_b == b->path_b) && |
| 1400 | (a->bustype == b->bustype) && |
| 1401 | (a->chip_instance->chip == |
| 1402 | b->chip_instance->chip)); |
| 1403 | } |
| 1404 | |
| 1405 | /* |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1406 | * Match resource nodes from base and override tree to see if they are the same |
| 1407 | * node. |
| 1408 | */ |
| 1409 | static int res_match(struct resource *a, struct resource *b) |
| 1410 | { |
| 1411 | return ((a->type == b->type) && |
| 1412 | (a->index == b->index)); |
| 1413 | } |
| 1414 | |
| 1415 | /* |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1416 | * Add resource to device. If resource is already present, then update its base |
| 1417 | * and index. If not, then add a new resource to the device. |
| 1418 | */ |
| 1419 | static void update_resource(struct device *dev, struct resource *res) |
| 1420 | { |
| 1421 | struct resource *base_res = dev->res; |
| 1422 | |
| 1423 | while (base_res) { |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1424 | if (res_match(base_res, res)) { |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1425 | base_res->base = res->base; |
| 1426 | return; |
| 1427 | } |
| 1428 | base_res = base_res->next; |
| 1429 | } |
| 1430 | |
| 1431 | new_resource(dev, res->type, res->index, res->base); |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * Add register to chip instance. If register is already present, then update |
| 1436 | * its value. If not, then add a new register to the chip instance. |
| 1437 | */ |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1438 | static void update_register(struct reg **const head, struct reg *reg) |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1439 | { |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1440 | struct reg *base_reg = *head; |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1441 | |
| 1442 | while (base_reg) { |
| 1443 | if (!strcmp(base_reg->key, reg->key)) { |
| 1444 | base_reg->value = reg->value; |
| 1445 | return; |
| 1446 | } |
| 1447 | base_reg = base_reg->next; |
| 1448 | } |
| 1449 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1450 | add_reg(head, reg->key, reg->value); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static void override_devicetree(struct bus *base_parent, |
| 1454 | struct bus *override_parent); |
| 1455 | |
| 1456 | /* |
| 1457 | * Update the base device properties using the properties of override device. In |
| 1458 | * addition to that, call override_devicetree for all the buses under the |
| 1459 | * override device. |
| 1460 | * |
| 1461 | * Override Rules: |
| 1462 | * +--------------------+--------------------------------------------+ |
| 1463 | * | | | |
| 1464 | * |struct device member| Rule | |
| 1465 | * | | | |
| 1466 | * +-----------------------------------------------------------------+ |
| 1467 | * | | | |
| 1468 | * | id | Unchanged. This is used to generate device | |
| 1469 | * | | structure name in static.c. So, no need to | |
| 1470 | * | | override. | |
| 1471 | * | | | |
| 1472 | * +-----------------------------------------------------------------+ |
| 1473 | * | | | |
| 1474 | * | enabled | Copy enabled state from override device. | |
| 1475 | * | | This allows variants to override device | |
| 1476 | * | | state. | |
| 1477 | * | | | |
| 1478 | * +-----------------------------------------------------------------+ |
| 1479 | * | | | |
| 1480 | * | subsystem_vendor | Copy from override device only if any one | |
| 1481 | * | subsystem_device | of the ids is non-zero. | |
| 1482 | * | | | |
| 1483 | * +-----------------------------------------------------------------+ |
| 1484 | * | | | |
| 1485 | * | inherit_subsystem | Copy from override device only if it is | |
| 1486 | * | | non-zero. This allows variant to only | |
| 1487 | * | | enable inherit flag for a device. | |
| 1488 | * | | | |
| 1489 | * +-----------------------------------------------------------------+ |
| 1490 | * | | | |
| 1491 | * | path | Unchanged since these are same for both | |
| 1492 | * | path_a | base and override device (Used for | |
| 1493 | * | path_b | matching devices). | |
| 1494 | * | | | |
| 1495 | * +-----------------------------------------------------------------+ |
| 1496 | * | | | |
| 1497 | * | bustype | Unchanged since this is same for both base | |
| 1498 | * | | and override device (User for matching | |
| 1499 | * | | devices). | |
| 1500 | * | | | |
| 1501 | * +-----------------------------------------------------------------+ |
| 1502 | * | | | |
| 1503 | * | pci_irq_info | Unchanged. | |
| 1504 | * | | | |
| 1505 | * +-----------------------------------------------------------------+ |
| 1506 | * | | | |
| 1507 | * | parent | Unchanged. This is meaningful only within | |
| 1508 | * | sibling | the parse tree, hence not being copied. | |
| 1509 | * | | | |
| 1510 | * +-----------------------------------------------------------------+ |
| 1511 | * | | | |
| 1512 | * | res | Each resource that is present in override | |
| 1513 | * | | device is copied over to base device: | |
Bill XIE | c61d415 | 2019-11-21 18:16:12 +0800 | [diff] [blame] | 1514 | * | | 1. If resource of same type and index is | |
| 1515 | * | | present in base device, then base of | |
| 1516 | * | | the resource is copied. | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1517 | * | | 2. If not, then a new resource is allocated| |
| 1518 | * | | under the base device using type, index | |
| 1519 | * | | and base from override res. | |
| 1520 | * | | | |
| 1521 | * +-----------------------------------------------------------------+ |
| 1522 | * | | | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1523 | * | ref | Each reference that is present in override | |
| 1524 | * | | device is copied over to base device with | |
| 1525 | * | | the same rules as registers. | |
| 1526 | * | | | |
| 1527 | * +-----------------------------------------------------------------+ |
| 1528 | * | | | |
| 1529 | * | alias | Base device alias is copied to override. | |
| 1530 | * | | Override devices cannot change/remove an | |
| 1531 | * | | existing alias, but they can add an alias | |
| 1532 | * | | if one does not exist. | |
| 1533 | * | | | |
| 1534 | * +-----------------------------------------------------------------+ |
| 1535 | * | | | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1536 | * | chip_instance | Each register of chip_instance is copied | |
| 1537 | * | | over from override device to base device: | |
| 1538 | * | | 1. If register with same key is present in | |
| 1539 | * | | base device, then value of the register | |
| 1540 | * | | is copied. | |
| 1541 | * | | 2. If not, then a new register is allocated| |
| 1542 | * | | under the base chip_instance using key | |
| 1543 | * | | and value from override register. | |
| 1544 | * | | | |
| 1545 | * +-----------------------------------------------------------------+ |
| 1546 | * | | | |
| 1547 | * | bus | Recursively call override_devicetree on | |
| 1548 | * | last_bus | each bus of override device. It is assumed | |
| 1549 | * | | that bus with id X under base device | |
| 1550 | * | | to bus with id X under override device. If | |
| 1551 | * | | override device has more buses than base | |
| 1552 | * | | device, then new buses are allocated under | |
| 1553 | * | | base device. | |
| 1554 | * | | | |
| 1555 | * +-----------------------------------------------------------------+ |
| 1556 | */ |
| 1557 | static void update_device(struct device *base_dev, struct device *override_dev) |
| 1558 | { |
| 1559 | /* |
| 1560 | * Copy the enabled state of override device to base device. This allows |
| 1561 | * override tree to enable or disable a particular device. |
| 1562 | */ |
| 1563 | base_dev->enabled = override_dev->enabled; |
| 1564 | |
| 1565 | /* |
Duncan Laurie | 7f6a484 | 2020-10-28 14:22:34 -0700 | [diff] [blame] | 1566 | * Copy the hidden state of override device to base device. This allows |
| 1567 | * override tree to hide or unhide a particular device. |
| 1568 | */ |
| 1569 | base_dev->hidden = override_dev->hidden; |
| 1570 | |
| 1571 | /* |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1572 | * Copy subsystem vendor and device ids from override device to base |
| 1573 | * device only if the ids are non-zero in override device. Else, honor |
| 1574 | * the values in base device. |
| 1575 | */ |
| 1576 | if (override_dev->subsystem_vendor || |
| 1577 | override_dev->subsystem_device) { |
| 1578 | base_dev->subsystem_vendor = override_dev->subsystem_vendor; |
| 1579 | base_dev->subsystem_device = override_dev->subsystem_device; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Copy value of inherity_subsystem from override device to base device |
| 1584 | * only if it is non-zero in override device. This allows override |
| 1585 | * tree to only enable inhert flag for a device. |
| 1586 | */ |
| 1587 | if (override_dev->inherit_subsystem) |
| 1588 | base_dev->inherit_subsystem = override_dev->inherit_subsystem; |
| 1589 | |
| 1590 | /* |
| 1591 | * Copy resources of override device to base device. |
| 1592 | * 1. If resource is already present in base device, then index and base |
| 1593 | * of the resource will be copied over. |
| 1594 | * 2. If resource is not already present in base device, a new resource |
| 1595 | * will be allocated. |
| 1596 | */ |
| 1597 | struct resource *res = override_dev->res; |
| 1598 | while (res) { |
| 1599 | update_resource(base_dev, res); |
| 1600 | res = res->next; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * Copy registers of override chip instance to base chip instance. |
| 1605 | * 1. If register key is already present in base chip instance, then |
| 1606 | * value for the register is copied over. |
| 1607 | * 2. If register key is not already present in base chip instance, then |
| 1608 | * a new register will be allocated. |
| 1609 | */ |
| 1610 | struct reg *reg = override_dev->chip_instance->reg; |
| 1611 | while (reg) { |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1612 | update_register(&base_dev->chip_instance->reg, reg); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1613 | reg = reg->next; |
| 1614 | } |
| 1615 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 1616 | /* Copy references just as with registers. */ |
| 1617 | reg = override_dev->chip_instance->ref; |
| 1618 | while (reg) { |
| 1619 | update_register(&base_dev->chip_instance->ref, reg); |
| 1620 | reg = reg->next; |
| 1621 | } |
| 1622 | |
| 1623 | /* Check for alias name conflicts. */ |
| 1624 | if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) { |
| 1625 | printf("ERROR: alias already exists: %s\n", override_dev->alias); |
| 1626 | exit(1); |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Copy alias from base device. |
| 1631 | * |
| 1632 | * Override devices cannot change/remove an existing alias, |
| 1633 | * but they can add an alias to a device if one does not exist yet. |
| 1634 | */ |
| 1635 | if (base_dev->alias) |
| 1636 | override_dev->alias = base_dev->alias; |
| 1637 | else |
| 1638 | base_dev->alias = override_dev->alias; |
| 1639 | |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1640 | /* |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 1641 | * Use probe list from override device in place of base device, in order |
| 1642 | * to allow an override to remove a probe from the base device. |
| 1643 | */ |
| 1644 | base_dev->probe = override_dev->probe; |
| 1645 | |
| 1646 | /* |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 1647 | * Update base_chip_instance member in chip instance of override tree to forward it to |
| 1648 | * the chip instance in base tree. |
| 1649 | */ |
| 1650 | override_dev->chip_instance->base_chip_instance = base_dev->chip_instance; |
| 1651 | |
| 1652 | /* |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1653 | * Now that the device properties are all copied over, look at each bus |
| 1654 | * of the override device and run override_devicetree in a recursive |
| 1655 | * manner. The assumption here is that first bus of override device |
| 1656 | * corresponds to first bus of base device and so on. If base device has |
| 1657 | * lesser buses than override tree, then new buses are allocated for it. |
| 1658 | */ |
| 1659 | struct bus *override_bus = override_dev->bus; |
| 1660 | struct bus *base_bus = base_dev->bus; |
| 1661 | |
| 1662 | while (override_bus) { |
| 1663 | |
| 1664 | /* |
| 1665 | * If we have more buses in override tree device, then allocate |
| 1666 | * a new bus for the base tree device as well. |
| 1667 | */ |
| 1668 | if (!base_bus) { |
| 1669 | alloc_bus(base_dev); |
| 1670 | base_bus = base_dev->last_bus; |
| 1671 | } |
| 1672 | |
| 1673 | override_devicetree(base_dev->bus, override_dev->bus); |
| 1674 | |
| 1675 | override_bus = override_bus->next_bus; |
| 1676 | base_bus = base_bus->next_bus; |
| 1677 | } |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * Perform copy of device and properties from override parent to base parent. |
| 1682 | * This function walks through the override tree in a depth-first manner |
| 1683 | * performing following actions: |
| 1684 | * 1. If matching device is found in base tree, then copy the properties of |
| 1685 | * override device to base tree device. Call override_devicetree recursively on |
| 1686 | * the bus of override device. |
| 1687 | * 2. If matching device is not found in base tree, then set override tree |
| 1688 | * device as new child of base_parent and update the chip pointers in override |
| 1689 | * device subtree to ensure the nodes do not point to override tree chip |
| 1690 | * instance. |
| 1691 | */ |
| 1692 | static void override_devicetree(struct bus *base_parent, |
| 1693 | struct bus *override_parent) |
| 1694 | { |
| 1695 | struct device *base_child; |
| 1696 | struct device *override_child = override_parent->children; |
| 1697 | struct device *next_child; |
| 1698 | |
| 1699 | while (override_child) { |
| 1700 | |
| 1701 | /* Look for a matching device in base tree. */ |
| 1702 | for (base_child = base_parent->children; |
| 1703 | base_child; base_child = base_child->sibling) { |
| 1704 | if (device_match(base_child, override_child)) |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | next_child = override_child->sibling; |
| 1709 | |
| 1710 | /* |
| 1711 | * If matching device is found, copy properties of |
| 1712 | * override_child to base_child. |
| 1713 | */ |
| 1714 | if (base_child) |
| 1715 | update_device(base_child, override_child); |
| 1716 | else { |
| 1717 | /* |
| 1718 | * If matching device is not found, set override_child |
| 1719 | * as a new child of base_parent. |
| 1720 | */ |
| 1721 | set_new_child(base_parent, override_child); |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | override_child = next_child; |
| 1725 | } |
| 1726 | } |
| 1727 | |
Duncan Laurie | cbd0bd8 | 2020-06-09 11:24:10 -0700 | [diff] [blame] | 1728 | static void parse_override_devicetree(const char *file, struct device *dev) |
| 1729 | { |
| 1730 | parse_devicetree(file, dev->bus); |
| 1731 | |
| 1732 | if (!dev_has_children(dev)) { |
| 1733 | fprintf(stderr, "ERROR: Override tree needs at least one device!\n"); |
| 1734 | exit(1); |
| 1735 | } |
| 1736 | |
| 1737 | override_devicetree(&base_root_bus, dev->bus); |
| 1738 | } |
| 1739 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1740 | static void generate_outputh(FILE *f, const char *fw_conf_header, const char *device_header) |
| 1741 | { |
| 1742 | fprintf(f, "#ifndef __STATIC_DEVICE_TREE_H\n"); |
| 1743 | fprintf(f, "#define __STATIC_DEVICE_TREE_H\n\n"); |
| 1744 | |
| 1745 | fprintf(f, "#include <%s>\n", fw_conf_header); |
| 1746 | fprintf(f, "#include <%s>\n\n", device_header); |
| 1747 | |
| 1748 | fprintf(f, "\n#endif /* __STATIC_DEVICE_TREE_H */\n"); |
| 1749 | } |
| 1750 | |
| 1751 | static void generate_outputc(FILE *f, const char *static_header) |
| 1752 | { |
| 1753 | fprintf(f, "#include <device/device.h>\n"); |
| 1754 | fprintf(f, "#include <device/pci.h>\n"); |
| 1755 | fprintf(f, "#include <fw_config.h>\n"); |
| 1756 | fprintf(f, "#include <%s>\n", static_header); |
| 1757 | emit_chip_headers(f, chip_header.next); |
| 1758 | fprintf(f, "\n#define STORAGE static __unused DEVTREE_CONST\n\n"); |
| 1759 | |
| 1760 | walk_device_tree(NULL, NULL, &base_root_dev, inherit_subsystem_ids); |
| 1761 | fprintf(f, "\n/* pass 0 */\n"); |
| 1762 | walk_device_tree(f, NULL, &base_root_dev, pass0); |
| 1763 | walk_device_tree(NULL, NULL, &base_root_dev, update_references); |
| 1764 | fprintf(f, "\n/* chip configs */\n"); |
| 1765 | emit_chip_configs(f); |
| 1766 | fprintf(f, "\n/* pass 1 */\n"); |
| 1767 | walk_device_tree(f, NULL, &base_root_dev, pass1); |
| 1768 | } |
| 1769 | |
| 1770 | static void generate_outputd(FILE *gen, FILE *dev) |
| 1771 | { |
| 1772 | fprintf(dev, "#ifndef __STATIC_DEVICES_H\n"); |
| 1773 | fprintf(dev, "#define __STATIC_DEVICES_H\n\n"); |
| 1774 | fprintf(dev, "#include <device/device.h>\n\n"); |
| 1775 | fprintf(dev, "/* expose_device_names */\n"); |
| 1776 | walk_device_tree(gen, dev, &base_root_dev, expose_device_names); |
| 1777 | fprintf(dev, "\n#endif /* __STATIC_DEVICE_NAMES_H */\n"); |
| 1778 | } |
| 1779 | |
| 1780 | static void generate_outputf(FILE *f) |
| 1781 | { |
| 1782 | fprintf(f, "#ifndef __STATIC_FW_CONFIG_H\n"); |
| 1783 | fprintf(f, "#define __STATIC_FW_CONFIG_H\n\n"); |
| 1784 | emit_fw_config(f); |
| 1785 | fprintf(f, "\n#endif /* __STATIC_FW_CONFIG_H */\n"); |
| 1786 | } |
| 1787 | |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1788 | int main(int argc, char **argv) |
| 1789 | { |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1790 | static const struct option long_options[] = { |
| 1791 | { "mainboard_devtree", 1, NULL, 'm' }, |
| 1792 | { "override_devtree", 1, NULL, 'o' }, |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 1793 | { "chipset_devtree", 1, NULL, 'p' }, |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1794 | { "output_c", 1, NULL, 'c' }, |
| 1795 | { "output_h", 1, NULL, 'r' }, |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1796 | { "output_d", 1, NULL, 'd' }, |
| 1797 | { "output_f", 1, NULL, 'f' }, |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1798 | { "help", 1, NULL, 'h' }, |
| 1799 | { } |
| 1800 | }; |
| 1801 | const char *override_devtree = NULL; |
| 1802 | const char *base_devtree = NULL; |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 1803 | const char *chipset_devtree = NULL; |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1804 | const char *outputc = NULL; |
| 1805 | const char *outputh = NULL; |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1806 | const char *outputd = NULL; |
| 1807 | const char *outputf = NULL; |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1808 | int opt, option_index; |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1809 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1810 | while ((opt = getopt_long(argc, argv, "m:o:p:c:r:d:f:h", long_options, |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1811 | &option_index)) != EOF) { |
| 1812 | switch (opt) { |
| 1813 | case 'm': |
| 1814 | base_devtree = strdup(optarg); |
| 1815 | break; |
| 1816 | case 'o': |
| 1817 | override_devtree = strdup(optarg); |
| 1818 | break; |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 1819 | case 'p': |
| 1820 | chipset_devtree = strdup(optarg); |
| 1821 | break; |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1822 | case 'c': |
| 1823 | outputc = strdup(optarg); |
| 1824 | break; |
| 1825 | case 'r': |
| 1826 | outputh = strdup(optarg); |
| 1827 | break; |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1828 | case 'd': |
| 1829 | outputd = strdup(optarg); |
| 1830 | break; |
| 1831 | case 'f': |
| 1832 | outputf = strdup(optarg); |
| 1833 | break; |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1834 | case 'h': |
| 1835 | default: |
| 1836 | usage(); |
| 1837 | } |
| 1838 | } |
| 1839 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1840 | if (!base_devtree || !outputc || !outputh || !outputd || !outputf) |
Duncan Laurie | 51c8373 | 2020-06-09 11:20:29 -0700 | [diff] [blame] | 1841 | usage(); |
Furquan Shaikh | de39fc7 | 2018-06-11 04:26:45 -0700 | [diff] [blame] | 1842 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 1843 | if (chipset_devtree) { |
| 1844 | /* Use the chipset devicetree as the base, then override |
| 1845 | with the mainboard "base" devicetree. */ |
| 1846 | parse_devicetree(chipset_devtree, &base_root_bus); |
| 1847 | parse_override_devicetree(base_devtree, &chipset_root_dev); |
| 1848 | } else { |
| 1849 | parse_devicetree(base_devtree, &base_root_bus); |
| 1850 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1851 | |
Duncan Laurie | cbd0bd8 | 2020-06-09 11:24:10 -0700 | [diff] [blame] | 1852 | if (override_devtree) |
| 1853 | parse_override_devicetree(override_devtree, &override_root_dev); |
Furquan Shaikh | 39ac797 | 2018-06-21 18:44:32 -0700 | [diff] [blame] | 1854 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1855 | FILE *autogen = fopen(outputc, "w"); |
| 1856 | if (!autogen) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 1857 | fprintf(stderr, "Could not open file '%s' for writing: ", |
| 1858 | outputc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1859 | perror(NULL); |
| 1860 | exit(1); |
| 1861 | } |
| 1862 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1863 | FILE *autohead = fopen(outputh, "w"); |
| 1864 | if (!autohead) { |
| 1865 | fprintf(stderr, "Could not open file '%s' for writing: ", outputh); |
| 1866 | perror(NULL); |
| 1867 | fclose(autogen); |
| 1868 | exit(1); |
| 1869 | } |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1870 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1871 | FILE *autodev = fopen(outputd, "w"); |
| 1872 | if (!autodev) { |
| 1873 | fprintf(stderr, "Could not open file '%s' for writing: ", outputd); |
| 1874 | perror(NULL); |
| 1875 | fclose(autogen); |
| 1876 | fclose(autohead); |
| 1877 | exit(1); |
| 1878 | } |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 1879 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1880 | FILE *autofwconf = fopen(outputf, "w"); |
| 1881 | if (!autofwconf) { |
| 1882 | fprintf(stderr, "Could not open file '%s' for writing: ", outputf); |
| 1883 | perror(NULL); |
| 1884 | fclose(autogen); |
| 1885 | fclose(autohead); |
| 1886 | fclose(autodev); |
| 1887 | exit(1); |
| 1888 | } |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 1889 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1890 | char *f = strdup(outputf); |
| 1891 | assert(f); |
| 1892 | char *d = strdup(outputd); |
| 1893 | assert(d); |
| 1894 | char *h = strdup(outputh); |
| 1895 | assert(h); |
Kyösti Mälkki | 5e2a2cd | 2019-03-15 17:44:23 +0200 | [diff] [blame] | 1896 | |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1897 | const char *fw_conf_header = basename(f); |
| 1898 | const char *device_header = basename(d); |
| 1899 | const char *static_header = basename(h); |
| 1900 | |
| 1901 | generate_outputh(autohead, fw_conf_header, device_header); |
| 1902 | generate_outputc(autogen, static_header); |
| 1903 | generate_outputd(autogen, autodev); |
| 1904 | generate_outputf(autofwconf); |
| 1905 | |
Nico Huber | 17e9bcb | 2019-09-20 12:05:51 +0200 | [diff] [blame] | 1906 | fclose(autohead); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 1907 | fclose(autogen); |
Tim Wawrzynczak | ba4a490 | 2020-09-23 15:36:30 -0600 | [diff] [blame] | 1908 | fclose(autodev); |
| 1909 | fclose(autofwconf); |
| 1910 | free(f); |
| 1911 | free(d); |
| 1912 | free(h); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 1913 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1914 | return 0; |
| 1915 | } |