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