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