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