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