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