| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <ctype.h> |
| 6 |
#include <getopt.h> |
| 7 |
#include <inttypes.h> |
| 8 |
#include <libgen.h> |
| 9 |
|
| 10 |
#include <sys/stat.h> |
| 11 |
#include <commonlib/helpers.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include "sconfig.h" |
| 14 |
#include "sconfig.tab.h" |
| 15 |
|
| 16 |
extern int linenum; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static struct chip chip_header; |
| 24 |
|
| 25 |
typedef enum { |
| 26 |
UNSLASH, |
| 27 |
SPLIT_1ST, |
| 28 |
TO_LOWER, |
| 29 |
TO_UPPER, |
| 30 |
} translate_t; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static struct device base_root_dev; |
| 74 |
|
| 75 |
|
| 76 |
static struct device chipset_root_dev; |
| 77 |
|
| 78 |
|
| 79 |
static struct device override_root_dev; |
| 80 |
|
| 81 |
static struct chip_instance mainboard_instance; |
| 82 |
|
| 83 |
static struct bus base_root_bus = { |
| 84 |
.dev = &base_root_dev, |
| 85 |
}; |
| 86 |
|
| 87 |
static struct device base_root_dev = { |
| 88 |
.name = "dev_root", |
| 89 |
.chip_instance = &mainboard_instance, |
| 90 |
.path = " .type = DEVICE_PATH_ROOT ", |
| 91 |
.parent = &base_root_bus, |
| 92 |
.enabled = 1, |
| 93 |
.bus = &base_root_bus, |
| 94 |
}; |
| 95 |
|
| 96 |
static struct bus chipset_root_bus = { |
| 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 |
|
| 109 |
static struct bus override_root_bus = { |
| 110 |
.dev = &override_root_dev, |
| 111 |
}; |
| 112 |
|
| 113 |
static struct device override_root_dev = { |
| 114 |
.name = "override_root", |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
.chip_instance = &mainboard_instance, |
| 122 |
.path = " .type = DEVICE_PATH_ROOT ", |
| 123 |
.parent = &override_root_bus, |
| 124 |
.enabled = 1, |
| 125 |
.bus = &override_root_bus, |
| 126 |
}; |
| 127 |
|
| 128 |
static struct chip mainboard_chip = { |
| 129 |
.name = "mainboard", |
| 130 |
.name_underscore = "mainboard", |
| 131 |
.instance = &mainboard_instance, |
| 132 |
}; |
| 133 |
|
| 134 |
static struct chip_instance mainboard_instance = { |
| 135 |
.id = 0, |
| 136 |
.chip = &mainboard_chip, |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
struct bus *root_parent; |
| 141 |
|
| 142 |
struct queue_entry { |
| 143 |
void *data; |
| 144 |
struct queue_entry *next; |
| 145 |
struct queue_entry *prev; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
static struct identifier *device_operations; |
| 150 |
|
| 151 |
#define S_ALLOC(_s) s_alloc(__func__, _s) |
| 152 |
|
| 153 |
static void *s_alloc(const char *f, size_t s) |
| 154 |
{ |
| 155 |
void *data = calloc(1, s); |
| 156 |
if (!data) { |
| 157 |
fprintf(stderr, "%s: Failed to alloc mem!\n", f); |
| 158 |
exit(1); |
| 159 |
} |
| 160 |
return data; |
| 161 |
} |
| 162 |
|
| 163 |
static struct queue_entry *new_queue_entry(void *data) |
| 164 |
{ |
| 165 |
struct queue_entry *e = S_ALLOC(sizeof(*e)); |
| 166 |
|
| 167 |
e->data = data; |
| 168 |
e->next = e->prev = e; |
| 169 |
return e; |
| 170 |
} |
| 171 |
|
| 172 |
static void enqueue_tail(struct queue_entry **q_head, void *data) |
| 173 |
{ |
| 174 |
struct queue_entry *tmp = new_queue_entry(data); |
| 175 |
struct queue_entry *q = *q_head; |
| 176 |
|
| 177 |
if (!q) { |
| 178 |
*q_head = tmp; |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
q->prev->next = tmp; |
| 183 |
tmp->prev = q->prev; |
| 184 |
q->prev = tmp; |
| 185 |
tmp->next = q; |
| 186 |
} |
| 187 |
|
| 188 |
static void *dequeue_tail(struct queue_entry **q_head) |
| 189 |
{ |
| 190 |
struct queue_entry *q = *q_head; |
| 191 |
struct queue_entry *tmp; |
| 192 |
void *data; |
| 193 |
|
| 194 |
if (!q) |
| 195 |
return NULL; |
| 196 |
|
| 197 |
tmp = q->prev; |
| 198 |
|
| 199 |
if (tmp == q) |
| 200 |
*q_head = NULL; |
| 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 |
|
| 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 |
|
| 255 |
int yywrap(void) |
| 256 |
{ |
| 257 |
return 1; |
| 258 |
} |
| 259 |
|
| 260 |
void yyerror(char const *str) |
| 261 |
{ |
| 262 |
extern char *yytext; |
| 263 |
fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
| 264 |
exit(1); |
| 265 |
} |
| 266 |
|
| 267 |
char *translate_name(const char *str, translate_t mode) |
| 268 |
{ |
| 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 |
} |
| 276 |
if (*c == '/') |
| 277 |
*c = '_'; |
| 278 |
if (*c == '-') |
| 279 |
*c = '_'; |
| 280 |
if (mode == TO_UPPER) |
| 281 |
*c = toupper(*c); |
| 282 |
if (mode == TO_LOWER) |
| 283 |
*c = tolower(*c); |
| 284 |
c++; |
| 285 |
} |
| 286 |
return b; |
| 287 |
} |
| 288 |
|
| 289 |
static struct chip *get_chip(char *path) |
| 290 |
{ |
| 291 |
struct chip *h = &chip_header; |
| 292 |
|
| 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 |
|
| 308 |
new_chip->chiph_exists = 1; |
| 309 |
new_chip->name = path; |
| 310 |
new_chip->name_underscore = translate_name(path, UNSLASH); |
| 311 |
|
| 312 |
struct stat st; |
| 313 |
char *chip_h = S_ALLOC(strlen(path) + 18); |
| 314 |
sprintf(chip_h, "src/%s", path); |
| 315 |
if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (!strstr(path, "/root_complex")) { |
| 320 |
fprintf(stderr, "ERROR: Chip component %s does not exist.\n", |
| 321 |
path); |
| 322 |
exit(1); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
sprintf(chip_h, "src/%s/chip.h", path); |
| 327 |
|
| 328 |
if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
| 329 |
new_chip->chiph_exists = 0; |
| 330 |
|
| 331 |
free(chip_h); |
| 332 |
|
| 333 |
return new_chip; |
| 334 |
} |
| 335 |
|
| 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 |
|
| 341 |
instance->chip = chip; |
| 342 |
instance->next = chip->instance; |
| 343 |
chip->instance = instance; |
| 344 |
|
| 345 |
return instance; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 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 |
|
| 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 |
|
| 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) |
| 439 |
{ |
| 440 |
struct fw_config_field *field = find_fw_config_field(name); |
| 441 |
struct fw_config_field_bits *tmp; |
| 442 |
|
| 443 |
|
| 444 |
if (field) { |
| 445 |
printf("ERROR: fw_config field %s already exists\n", name); |
| 446 |
exit(1); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
tmp = bits; |
| 451 |
while (tmp) { |
| 452 |
if (tmp->start_bit > tmp->end_bit || tmp->end_bit > 63) { |
| 453 |
printf("ERROR: fw_config field %s has invalid range %u-%u\n", name, |
| 454 |
tmp->start_bit, tmp->end_bit); |
| 455 |
exit(1); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 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; |
| 467 |
} |
| 468 |
|
| 469 |
field = S_ALLOC(sizeof(*field)); |
| 470 |
field->name = name; |
| 471 |
field->bits = bits; |
| 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 |
|
| 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 |
|
| 504 |
void add_fw_config_option(struct fw_config_field *field, const char *name, uint64_t value) |
| 505 |
{ |
| 506 |
struct fw_config_option *option; |
| 507 |
|
| 508 |
|
| 509 |
uint64_t field_max_value = calc_max_field_value(field); |
| 510 |
if (value > field_max_value) { |
| 511 |
printf("ERROR: fw_config option %s:%s value %" PRIx64 " larger than field max %" |
| 512 |
PRIx64 "\n", |
| 513 |
field->name, name, value, field_max_value); |
| 514 |
exit(1); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 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 |
|
| 526 |
if (value == option->value) { |
| 527 |
printf("ERROR: fw_config option %s:%s[%" PRIx64 "] redefined as %s\n", |
| 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 |
|
| 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 |
|
| 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 |
|
| 568 |
void add_fw_config_probe(struct bus *bus, const char *field, const char *option) |
| 569 |
{ |
| 570 |
struct fw_config_probe *probe; |
| 571 |
|
| 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); |
| 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 |
|
| 584 |
void probe_unprovisioned_fw_config(struct bus *bus) |
| 585 |
{ |
| 586 |
bus->dev->enable_on_unprovisioned_fw_config = true; |
| 587 |
} |
| 588 |
|
| 589 |
static uint64_t compute_fw_config_mask(const struct fw_config_field_bits *bits) |
| 590 |
{ |
| 591 |
uint64_t mask = 0; |
| 592 |
|
| 593 |
while (bits) { |
| 594 |
|
| 595 |
uint64_t tmp = ((1ull << (1ull + bits->end_bit - bits->start_bit)) - 1ull); |
| 596 |
tmp <<= bits->start_bit; |
| 597 |
mask |= tmp; |
| 598 |
bits = bits->next; |
| 599 |
} |
| 600 |
|
| 601 |
return mask; |
| 602 |
} |
| 603 |
|
| 604 |
static unsigned int bits_width(const struct fw_config_field_bits *bits) |
| 605 |
{ |
| 606 |
return 1 + bits->end_bit - bits->start_bit; |
| 607 |
} |
| 608 |
|
| 609 |
static uint64_t calc_option_value(const struct fw_config_field *field, |
| 610 |
const struct fw_config_option *option) |
| 611 |
{ |
| 612 |
uint64_t value = 0; |
| 613 |
uint64_t original = option->value; |
| 614 |
|
| 615 |
struct fw_config_field_bits *bits = field->bits; |
| 616 |
while (bits) { |
| 617 |
const unsigned int width = bits_width(bits); |
| 618 |
const uint64_t orig_mask = (1ull << width) - 1ull; |
| 619 |
const uint64_t orig = (original & orig_mask); |
| 620 |
value |= (orig << bits->start_bit); |
| 621 |
|
| 622 |
original >>= width; |
| 623 |
bits = bits->next; |
| 624 |
} |
| 625 |
|
| 626 |
return value; |
| 627 |
} |
| 628 |
|
| 629 |
static void emit_fw_config(FILE *fil) |
| 630 |
{ |
| 631 |
struct fw_config_field *field = fw_config_fields; |
| 632 |
|
| 633 |
if (!field) |
| 634 |
return; |
| 635 |
|
| 636 |
while (field) { |
| 637 |
struct fw_config_option *option = field->options; |
| 638 |
uint64_t mask; |
| 639 |
|
| 640 |
fprintf(fil, "#define FW_CONFIG_FIELD_%s_NAME \"%s\"\n", |
| 641 |
field->name, field->name); |
| 642 |
|
| 643 |
mask = compute_fw_config_mask(field->bits); |
| 644 |
fprintf(fil, "#define FW_CONFIG_FIELD_%s_MASK 0x%" PRIx64 "\n", |
| 645 |
field->name, mask); |
| 646 |
|
| 647 |
while (option) { |
| 648 |
const uint64_t value = calc_option_value(field, option); |
| 649 |
fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_NAME \"%s\"\n", |
| 650 |
field->name, option->name, option->name); |
| 651 |
fprintf(fil, "#define FW_CONFIG_FIELD_%s_OPTION_%s_VALUE 0x%" |
| 652 |
PRIx64 "\n", field->name, option->name, value); |
| 653 |
option = option->next; |
| 654 |
} |
| 655 |
|
| 656 |
field = field->next; |
| 657 |
} |
| 658 |
|
| 659 |
fprintf(fil, "\n"); |
| 660 |
} |
| 661 |
|
| 662 |
static int emit_fw_config_probe(FILE *fil, struct device *dev) |
| 663 |
{ |
| 664 |
struct fw_config_probe *probe = dev->probe; |
| 665 |
|
| 666 |
fprintf(fil, "STORAGE struct fw_config %s_probe_list[] = {\n", dev->name); |
| 667 |
|
| 668 |
while (probe) { |
| 669 |
|
| 670 |
struct fw_config_field *field; |
| 671 |
struct fw_config_option *option; |
| 672 |
uint64_t mask, value; |
| 673 |
|
| 674 |
field = find_fw_config_field(probe->field); |
| 675 |
if (!field) { |
| 676 |
printf("ERROR: fw_config_probe field %s not found\n", probe->field); |
| 677 |
return -1; |
| 678 |
} |
| 679 |
option = find_fw_config_option(field, probe->option); |
| 680 |
if (!option) { |
| 681 |
printf("ERROR: fw_config_probe field %s option %s not found\n", |
| 682 |
probe->field, probe->option); |
| 683 |
return -1; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
fprintf(fil, "\t{\n"); |
| 688 |
fprintf(fil, "\t\t.field_name = FW_CONFIG_FIELD_%s_NAME,\n", probe->field); |
| 689 |
fprintf(fil, "\t\t.option_name = FW_CONFIG_FIELD_%s_OPTION_%s_NAME,\n", |
| 690 |
probe->field, probe->option); |
| 691 |
fprintf(fil, "\t\t.mask = FW_CONFIG_FIELD_%s_MASK,\n", probe->field); |
| 692 |
fprintf(fil, "\t\t.value = FW_CONFIG_FIELD_%s_OPTION_%s_VALUE,\n", |
| 693 |
probe->field, probe->option); |
| 694 |
fprintf(fil, "\t},\n"); |
| 695 |
|
| 696 |
probe = probe->next; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
fprintf(fil, "\t{ }\n};\n"); |
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
void add_identifier(struct identifier **it, const char *id) |
| 706 |
{ |
| 707 |
for (; *it != NULL; it = &(*it)->next) { |
| 708 |
if (!strcmp((*it)->id, id)) |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
*it = S_ALLOC(sizeof(**it)); |
| 713 |
(*it)->id = id; |
| 714 |
} |
| 715 |
|
| 716 |
void add_device_ops(struct bus *bus, char *ops_id) |
| 717 |
{ |
| 718 |
if (bus->dev->ops_id) { |
| 719 |
printf("ERROR: Device operations may only be specified once,\n" |
| 720 |
" found '%s', '%s'.\n", bus->dev->ops_id, ops_id); |
| 721 |
exit(1); |
| 722 |
} |
| 723 |
|
| 724 |
add_identifier(&device_operations, ops_id); |
| 725 |
bus->dev->ops_id = ops_id; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
static void alloc_bus(struct device *dev) |
| 730 |
{ |
| 731 |
struct bus *bus = S_ALLOC(sizeof(*bus)); |
| 732 |
|
| 733 |
bus->dev = dev; |
| 734 |
dev->bus = bus; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
static struct device *alloc_dev(struct bus *parent) |
| 743 |
{ |
| 744 |
struct device *dev = S_ALLOC(sizeof(*dev)); |
| 745 |
|
| 746 |
dev->parent = parent; |
| 747 |
dev->subsystem_vendor = -1; |
| 748 |
dev->subsystem_device = -1; |
| 749 |
|
| 750 |
alloc_bus(dev); |
| 751 |
|
| 752 |
return dev; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
static struct device *get_dev(struct bus *parent, int path_a, int path_b, |
| 762 |
int bustype, struct chip_instance *chip_instance) |
| 763 |
{ |
| 764 |
struct device *child = parent->children; |
| 765 |
|
| 766 |
while (child) { |
| 767 |
if ((child->path_a == path_a) && (child->path_b == path_b) && |
| 768 |
(child->bustype == bustype) && |
| 769 |
(child->chip_instance == chip_instance)) |
| 770 |
return child; |
| 771 |
|
| 772 |
child = child->sibling; |
| 773 |
} |
| 774 |
|
| 775 |
return NULL; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static void set_new_child(struct bus *parent, struct device *child) |
| 783 |
{ |
| 784 |
struct device *c = parent->children; |
| 785 |
if (c) { |
| 786 |
while (c->sibling) |
| 787 |
c = c->sibling; |
| 788 |
c->sibling = child; |
| 789 |
} else |
| 790 |
parent->children = child; |
| 791 |
|
| 792 |
child->sibling = NULL; |
| 793 |
child->parent = parent; |
| 794 |
} |
| 795 |
|
| 796 |
static const struct device *find_alias(const struct device *const parent, |
| 797 |
const char *const alias) |
| 798 |
{ |
| 799 |
if (parent->alias && !strcmp(parent->alias, alias)) |
| 800 |
return parent; |
| 801 |
|
| 802 |
const struct bus *bus = parent->bus; |
| 803 |
if (!bus) |
| 804 |
return NULL; |
| 805 |
|
| 806 |
const struct device *child; |
| 807 |
for (child = bus->children; child; child = child->sibling) { |
| 808 |
const struct device *const ret = find_alias(child, alias); |
| 809 |
if (ret) |
| 810 |
return ret; |
| 811 |
} |
| 812 |
|
| 813 |
return NULL; |
| 814 |
} |
| 815 |
|
| 816 |
static struct device *new_device_with_path(struct bus *parent, |
| 817 |
struct chip_instance *chip_instance, |
| 818 |
const int bustype, int path_a, int path_b, |
| 819 |
char *alias, int status) |
| 820 |
{ |
| 821 |
struct device *new_d; |
| 822 |
|
| 823 |
|
| 824 |
new_d = get_dev(parent, path_a, path_b, bustype, chip_instance); |
| 825 |
if (new_d) { |
| 826 |
printf("ERROR: Duplicate device! %s\n", new_d->name); |
| 827 |
exit(1); |
| 828 |
} |
| 829 |
|
| 830 |
new_d = alloc_dev(parent); |
| 831 |
|
| 832 |
new_d->bustype = bustype; |
| 833 |
|
| 834 |
new_d->path_a = path_a; |
| 835 |
new_d->path_b = path_b; |
| 836 |
new_d->alias = alias; |
| 837 |
|
| 838 |
new_d->enabled = status & 0x01; |
| 839 |
new_d->hidden = (status >> 1) & 0x01; |
| 840 |
new_d->mandatory = (status >> 2) & 0x01; |
| 841 |
new_d->chip_instance = chip_instance; |
| 842 |
|
| 843 |
set_new_child(parent, new_d); |
| 844 |
|
| 845 |
switch (bustype) { |
| 846 |
case PCI: |
| 847 |
new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}"; |
| 848 |
break; |
| 849 |
|
| 850 |
case PNP: |
| 851 |
new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}"; |
| 852 |
break; |
| 853 |
|
| 854 |
case I2C: |
| 855 |
new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}"; |
| 856 |
break; |
| 857 |
|
| 858 |
case CPU_CLUSTER: |
| 859 |
new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}"; |
| 860 |
break; |
| 861 |
|
| 862 |
case CPU: |
| 863 |
new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}"; |
| 864 |
break; |
| 865 |
|
| 866 |
case DOMAIN: |
| 867 |
new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain_id = 0x%x }}"; |
| 868 |
break; |
| 869 |
|
| 870 |
case GENERIC: |
| 871 |
new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}"; |
| 872 |
break; |
| 873 |
|
| 874 |
case SPI: |
| 875 |
new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}"; |
| 876 |
break; |
| 877 |
|
| 878 |
case USB: |
| 879 |
new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}"; |
| 880 |
break; |
| 881 |
|
| 882 |
case MMIO: |
| 883 |
new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}"; |
| 884 |
break; |
| 885 |
|
| 886 |
case GPIO: |
| 887 |
new_d->path = ".type=DEVICE_PATH_GPIO,{.gpio={ .id = 0x%x }}"; |
| 888 |
break; |
| 889 |
|
| 890 |
case MDIO: |
| 891 |
new_d->path = ".type=DEVICE_PATH_MDIO,{.mdio={ .addr = 0x%x }}"; |
| 892 |
break; |
| 893 |
} |
| 894 |
|
| 895 |
return new_d; |
| 896 |
} |
| 897 |
|
| 898 |
struct device *new_device_reference(struct bus *parent, |
| 899 |
struct chip_instance *chip_instance, |
| 900 |
const char *reference, int status) |
| 901 |
{ |
| 902 |
const struct device *dev = find_alias(&base_root_dev, reference); |
| 903 |
|
| 904 |
if (!dev) { |
| 905 |
printf("ERROR: Unable to find device reference %s\n", reference); |
| 906 |
exit(1); |
| 907 |
} |
| 908 |
|
| 909 |
return new_device_with_path(parent, chip_instance, dev->bustype, dev->path_a, |
| 910 |
dev->path_b, NULL, status); |
| 911 |
} |
| 912 |
|
| 913 |
struct device *new_device_raw(struct bus *parent, |
| 914 |
struct chip_instance *chip_instance, |
| 915 |
const int bustype, const char *devnum, |
| 916 |
char *alias, int status) |
| 917 |
{ |
| 918 |
char *tmp; |
| 919 |
int path_a; |
| 920 |
int path_b = 0; |
| 921 |
|
| 922 |
|
| 923 |
if (alias && find_alias(root_parent->dev, alias)) { |
| 924 |
printf("ERROR: Alias already exists: %s\n", alias); |
| 925 |
exit(1); |
| 926 |
} |
| 927 |
|
| 928 |
path_a = strtol(devnum, &tmp, 16); |
| 929 |
if (*tmp == '.') { |
| 930 |
tmp++; |
| 931 |
path_b = strtol(tmp, NULL, 16); |
| 932 |
} |
| 933 |
|
| 934 |
return new_device_with_path(parent, chip_instance, bustype, path_a, path_b, alias, |
| 935 |
status); |
| 936 |
} |
| 937 |
|
| 938 |
static void new_resource(struct device *dev, int type, int index, int base) |
| 939 |
{ |
| 940 |
struct resource *r = S_ALLOC(sizeof(struct resource)); |
| 941 |
|
| 942 |
r->type = type; |
| 943 |
r->index = index; |
| 944 |
r->base = base; |
| 945 |
if (dev->res) { |
| 946 |
struct resource *head = dev->res; |
| 947 |
while (head->next) |
| 948 |
head = head->next; |
| 949 |
head->next = r; |
| 950 |
} else { |
| 951 |
dev->res = r; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
void add_resource(struct bus *bus, int type, int index, int base) |
| 956 |
{ |
| 957 |
new_resource(bus->dev, type, index, base); |
| 958 |
} |
| 959 |
|
| 960 |
static void add_reg(struct reg **const head, char *const name, char *const val) |
| 961 |
{ |
| 962 |
struct reg *const r = S_ALLOC(sizeof(struct reg)); |
| 963 |
struct reg *prev = NULL; |
| 964 |
struct reg *cur; |
| 965 |
|
| 966 |
r->key = name; |
| 967 |
r->value = val; |
| 968 |
|
| 969 |
for (cur = *head; cur != NULL; prev = cur, cur = cur->next) { |
| 970 |
const int sort = strcmp(r->key, cur->key); |
| 971 |
if (sort == 0) { |
| 972 |
printf("ERROR: duplicate 'register' key '%s'.\n", r->key); |
| 973 |
exit(1); |
| 974 |
} |
| 975 |
if (sort < 0) |
| 976 |
break; |
| 977 |
} |
| 978 |
r->next = cur; |
| 979 |
if (prev) |
| 980 |
prev->next = r; |
| 981 |
else |
| 982 |
*head = r; |
| 983 |
} |
| 984 |
|
| 985 |
void add_register(struct chip_instance *chip_instance, char *name, char *val) |
| 986 |
{ |
| 987 |
add_reg(&chip_instance->reg, name, val); |
| 988 |
} |
| 989 |
|
| 990 |
void add_reference(struct chip_instance *const chip_instance, |
| 991 |
char *const name, char *const alias) |
| 992 |
{ |
| 993 |
add_reg(&chip_instance->ref, name, alias); |
| 994 |
} |
| 995 |
|
| 996 |
static void set_reference(struct chip_instance *const chip_instance, |
| 997 |
char *const name, char *const alias) |
| 998 |
{ |
| 999 |
const struct device *const dev = find_alias(&base_root_dev, alias); |
| 1000 |
if (!dev) { |
| 1001 |
printf("ERROR: Cannot find device alias '%s'.\n", alias); |
| 1002 |
exit(1); |
| 1003 |
} |
| 1004 |
|
| 1005 |
char *const ref_name = S_ALLOC(strlen(dev->name) + 2); |
| 1006 |
sprintf(ref_name, "&%s", dev->name); |
| 1007 |
add_register(chip_instance, name, ref_name); |
| 1008 |
} |
| 1009 |
|
| 1010 |
static void update_references(FILE *file, FILE *head, struct device *dev, |
| 1011 |
struct device *next) |
| 1012 |
{ |
| 1013 |
struct reg *ref; |
| 1014 |
|
| 1015 |
for (ref = dev->chip_instance->ref; ref; ref = ref->next) |
| 1016 |
set_reference(dev->chip_instance, ref->key, ref->value); |
| 1017 |
} |
| 1018 |
|
| 1019 |
void add_slot_desc(struct bus *bus, char *type, char *length, char *designation, |
| 1020 |
char *data_width) |
| 1021 |
{ |
| 1022 |
struct device *dev = bus->dev; |
| 1023 |
|
| 1024 |
if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
| 1025 |
printf("ERROR: 'slot_type' only allowed for PCI devices\n"); |
| 1026 |
exit(1); |
| 1027 |
} |
| 1028 |
|
| 1029 |
dev->smbios_slot_type = type; |
| 1030 |
dev->smbios_slot_length = length; |
| 1031 |
dev->smbios_slot_data_width = data_width; |
| 1032 |
dev->smbios_slot_designation = designation; |
| 1033 |
} |
| 1034 |
|
| 1035 |
void add_smbios_dev_info(struct bus *bus, long instance_id, const char *refdes) |
| 1036 |
{ |
| 1037 |
struct device *dev = bus->dev; |
| 1038 |
|
| 1039 |
if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
| 1040 |
printf("ERROR: 'dev_info' only allowed for PCI devices\n"); |
| 1041 |
exit(1); |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (instance_id < 0 || instance_id > UINT8_MAX) { |
| 1045 |
printf("ERROR: SMBIOS dev info instance ID '%ld' out of range\n", instance_id); |
| 1046 |
exit(1); |
| 1047 |
} |
| 1048 |
|
| 1049 |
dev->smbios_instance_id_valid = 1; |
| 1050 |
dev->smbios_instance_id = (unsigned int)instance_id; |
| 1051 |
dev->smbios_refdes = refdes; |
| 1052 |
} |
| 1053 |
|
| 1054 |
void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
| 1055 |
int inherit) |
| 1056 |
{ |
| 1057 |
struct device *dev = bus->dev; |
| 1058 |
|
| 1059 |
if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
| 1060 |
printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 1061 |
exit(1); |
| 1062 |
} |
| 1063 |
|
| 1064 |
dev->subsystem_vendor = vendor; |
| 1065 |
dev->subsystem_device = device; |
| 1066 |
dev->inherit_subsystem = inherit; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static int dev_has_children(struct device *dev) |
| 1070 |
{ |
| 1071 |
struct bus *bus = dev->bus; |
| 1072 |
|
| 1073 |
if (bus && bus->children) |
| 1074 |
return 1; |
| 1075 |
|
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static void pass0(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
| 1080 |
{ |
| 1081 |
static int dev_id; |
| 1082 |
|
| 1083 |
if (ptr == &base_root_dev) { |
| 1084 |
fprintf(fil, "STORAGE struct bus %s_bus;\n", |
| 1085 |
ptr->name); |
| 1086 |
return; |
| 1087 |
} |
| 1088 |
|
| 1089 |
char *name; |
| 1090 |
|
| 1091 |
if (ptr->alias) { |
| 1092 |
name = S_ALLOC(6 + strlen(ptr->alias)); |
| 1093 |
sprintf(name, "_dev_%s", ptr->alias); |
| 1094 |
} else { |
| 1095 |
name = S_ALLOC(11); |
| 1096 |
sprintf(name, "_dev_%d", dev_id++); |
| 1097 |
} |
| 1098 |
|
| 1099 |
ptr->name = name; |
| 1100 |
|
| 1101 |
fprintf(fil, "STORAGE struct device %s;\n", ptr->name); |
| 1102 |
if (ptr->res) |
| 1103 |
fprintf(fil, "STORAGE struct resource %s_res[];\n", |
| 1104 |
ptr->name); |
| 1105 |
if (dev_has_children(ptr)) |
| 1106 |
fprintf(fil, "STORAGE struct bus %s_bus;\n", |
| 1107 |
ptr->name); |
| 1108 |
|
| 1109 |
if (next) |
| 1110 |
return; |
| 1111 |
|
| 1112 |
fprintf(fil, |
| 1113 |
"DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n", |
| 1114 |
ptr->name); |
| 1115 |
} |
| 1116 |
|
| 1117 |
static void emit_smbios_data(FILE *fil, struct device *ptr) |
| 1118 |
{ |
| 1119 |
fprintf(fil, "#if !DEVTREE_EARLY\n"); |
| 1120 |
fprintf(fil, "#if CONFIG(GENERATE_SMBIOS_TABLES)\n"); |
| 1121 |
|
| 1122 |
|
| 1123 |
if (ptr->smbios_slot_type) |
| 1124 |
fprintf(fil, "\t.smbios_slot_type = %s,\n", |
| 1125 |
ptr->smbios_slot_type); |
| 1126 |
if (ptr->smbios_slot_data_width) |
| 1127 |
fprintf(fil, "\t.smbios_slot_data_width = %s,\n", |
| 1128 |
ptr->smbios_slot_data_width); |
| 1129 |
if (ptr->smbios_slot_designation) |
| 1130 |
fprintf(fil, "\t.smbios_slot_designation = \"%s\",\n", |
| 1131 |
ptr->smbios_slot_designation); |
| 1132 |
if (ptr->smbios_slot_length) |
| 1133 |
fprintf(fil, "\t.smbios_slot_length = %s,\n", |
| 1134 |
ptr->smbios_slot_length); |
| 1135 |
|
| 1136 |
|
| 1137 |
if (ptr->smbios_instance_id_valid) { |
| 1138 |
fprintf(fil, "\t.smbios_instance_id_valid = true,\n"); |
| 1139 |
fprintf(fil, "\t.smbios_instance_id = %u,\n", ptr->smbios_instance_id); |
| 1140 |
if (ptr->smbios_refdes) |
| 1141 |
fprintf(fil, "\t.smbios_refdes = \"%s\",\n", ptr->smbios_refdes); |
| 1142 |
} |
| 1143 |
|
| 1144 |
fprintf(fil, "#endif\n"); |
| 1145 |
fprintf(fil, "#endif\n"); |
| 1146 |
} |
| 1147 |
|
| 1148 |
static void emit_resources(FILE *fil, struct device *ptr) |
| 1149 |
{ |
| 1150 |
if (ptr->res == NULL) |
| 1151 |
return; |
| 1152 |
|
| 1153 |
int i = 1; |
| 1154 |
fprintf(fil, "STORAGE struct resource %s_res[] = {\n", ptr->name); |
| 1155 |
struct resource *r = ptr->res; |
| 1156 |
while (r) { |
| 1157 |
fprintf(fil, |
| 1158 |
"\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 1159 |
if (r->type == IRQ) |
| 1160 |
fprintf(fil, "IRQ"); |
| 1161 |
if (r->type == DRQ) |
| 1162 |
fprintf(fil, "DRQ"); |
| 1163 |
if (r->type == IO) |
| 1164 |
fprintf(fil, "IO"); |
| 1165 |
fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, |
| 1166 |
r->base); |
| 1167 |
if (r->next) |
| 1168 |
fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, |
| 1169 |
i++); |
| 1170 |
else |
| 1171 |
fprintf(fil, ".next=NULL },\n"); |
| 1172 |
r = r->next; |
| 1173 |
} |
| 1174 |
|
| 1175 |
fprintf(fil, "\t };\n"); |
| 1176 |
} |
| 1177 |
|
| 1178 |
static void emit_dev_bus(FILE *fil, struct device *ptr) |
| 1179 |
{ |
| 1180 |
fprintf(fil, "STORAGE struct bus %s_bus = {\n", |
| 1181 |
ptr->name); |
| 1182 |
|
| 1183 |
assert(ptr->bus && ptr->bus->children); |
| 1184 |
struct bus *bus = ptr->bus; |
| 1185 |
|
| 1186 |
fprintf(fil, "\t.dev = &%s,\n", bus->dev->name); |
| 1187 |
fprintf(fil, "\t.children = &%s,\n", bus->children->name); |
| 1188 |
|
| 1189 |
fprintf(fil, "};\n"); |
| 1190 |
} |
| 1191 |
|
| 1192 |
static struct chip_instance *get_chip_instance(const struct device *dev) |
| 1193 |
{ |
| 1194 |
struct chip_instance *chip_ins = dev->chip_instance; |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
if (chip_ins->base_chip_instance) |
| 1200 |
chip_ins = chip_ins->base_chip_instance; |
| 1201 |
|
| 1202 |
return chip_ins; |
| 1203 |
} |
| 1204 |
|
| 1205 |
static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
| 1206 |
{ |
| 1207 |
struct chip_instance *chip_ins = get_chip_instance(ptr); |
| 1208 |
int has_children = dev_has_children(ptr); |
| 1209 |
|
| 1210 |
|
| 1211 |
if (ptr->probe && (emit_fw_config_probe(fil, ptr) < 0)) { |
| 1212 |
if (head) |
| 1213 |
fclose(head); |
| 1214 |
fclose(fil); |
| 1215 |
exit(1); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (ptr == &base_root_dev) |
| 1219 |
fprintf(fil, "DEVTREE_CONST struct device %s = {\n", ptr->name); |
| 1220 |
else |
| 1221 |
fprintf(fil, "STORAGE struct device %s = {\n", ptr->name); |
| 1222 |
|
| 1223 |
fprintf(fil, "#if !DEVTREE_EARLY\n"); |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
if (ptr->ops_id) |
| 1231 |
fprintf(fil, "\t.ops = &%s,\n", ptr->ops_id); |
| 1232 |
else if (ptr == &base_root_dev) |
| 1233 |
fprintf(fil, "\t.ops = &default_dev_ops_root,\n"); |
| 1234 |
else |
| 1235 |
fprintf(fil, "\t.ops = NULL,\n"); |
| 1236 |
fprintf(fil, "#endif\n"); |
| 1237 |
fprintf(fil, "\t.upstream = &%s_bus,\n", ptr->parent->dev->name); |
| 1238 |
fprintf(fil, "\t.path = {"); |
| 1239 |
fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 1240 |
fprintf(fil, "},\n"); |
| 1241 |
fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
| 1242 |
fprintf(fil, "\t.hidden = %d,\n", ptr->hidden); |
| 1243 |
fprintf(fil, "\t.mandatory = %d,\n", ptr->mandatory); |
| 1244 |
fprintf(fil, "\t.on_mainboard = 1,\n"); |
| 1245 |
if (ptr->subsystem_vendor > 0) |
| 1246 |
fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", |
| 1247 |
ptr->subsystem_vendor); |
| 1248 |
|
| 1249 |
if (ptr->subsystem_device > 0) |
| 1250 |
fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 1251 |
ptr->subsystem_device); |
| 1252 |
|
| 1253 |
if (ptr->res) { |
| 1254 |
fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
| 1255 |
ptr->name); |
| 1256 |
} |
| 1257 |
if (has_children) |
| 1258 |
fprintf(fil, "\t.downstream = &%s_bus,\n", |
| 1259 |
ptr->name); |
| 1260 |
else |
| 1261 |
fprintf(fil, "\t.downstream = NULL,\n"); |
| 1262 |
if (ptr->sibling) |
| 1263 |
fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
| 1264 |
else |
| 1265 |
fprintf(fil, "\t.sibling = NULL,\n"); |
| 1266 |
if (ptr->probe) |
| 1267 |
fprintf(fil, "\t.probe_list = %s_probe_list,\n", ptr->name); |
| 1268 |
fprintf(fil, "\t.enable_on_unprovisioned_fw_config = %d,\n", |
| 1269 |
ptr->enable_on_unprovisioned_fw_config); |
| 1270 |
fprintf(fil, "#if !DEVTREE_EARLY\n"); |
| 1271 |
fprintf(fil, "\t.chip_ops = &%s_ops,\n", |
| 1272 |
chip_ins->chip->name_underscore); |
| 1273 |
if (chip_ins == &mainboard_instance) |
| 1274 |
fprintf(fil, "\t.name = mainboard_name,\n"); |
| 1275 |
fprintf(fil, "#endif\n"); |
| 1276 |
if (chip_ins->chip->chiph_exists) |
| 1277 |
fprintf(fil, "\t.chip_info = &%s_info_%d,\n", |
| 1278 |
chip_ins->chip->name_underscore, chip_ins->id); |
| 1279 |
if (next) |
| 1280 |
fprintf(fil, "\t.next=&%s,\n", next->name); |
| 1281 |
|
| 1282 |
emit_smbios_data(fil, ptr); |
| 1283 |
|
| 1284 |
fprintf(fil, "};\n"); |
| 1285 |
|
| 1286 |
emit_resources(fil, ptr); |
| 1287 |
|
| 1288 |
if (has_children) |
| 1289 |
emit_dev_bus(fil, ptr); |
| 1290 |
} |
| 1291 |
|
| 1292 |
static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next) |
| 1293 |
{ |
| 1294 |
struct chip_instance *chip_ins = get_chip_instance(ptr); |
| 1295 |
|
| 1296 |
|
| 1297 |
if (ptr->bustype == PCI && ptr->parent->dev->bustype == DOMAIN) { |
| 1298 |
if (ptr->alias) { |
| 1299 |
fprintf(head, "static const pci_devfn_t _sdev_%s = PCI_DEV(%d, %d, %d);\n", |
| 1300 |
ptr->alias, ptr->parent->dev->path_a, ptr->path_a, ptr->path_b); |
| 1301 |
} |
| 1302 |
fprintf(head, "extern DEVTREE_CONST struct device *const __pci_%d_%02x_%d;\n", |
| 1303 |
ptr->parent->dev->path_a, ptr->path_a, ptr->path_b); |
| 1304 |
fprintf(fil, "DEVTREE_CONST struct device *const __pci_%d_%02x_%d = &%s;\n", |
| 1305 |
ptr->parent->dev->path_a, ptr->path_a, ptr->path_b, ptr->name); |
| 1306 |
|
| 1307 |
if (chip_ins->chip->chiph_exists) { |
| 1308 |
fprintf(head, "extern DEVTREE_CONST void *const __pci_%d_%02x_%d_config;\n", |
| 1309 |
ptr->parent->dev->path_a, ptr->path_a, ptr->path_b); |
| 1310 |
fprintf(fil, "DEVTREE_CONST void *const __pci_%d_%02x_%d_config = &%s_info_%d;\n", |
| 1311 |
ptr->parent->dev->path_a, ptr->path_a, ptr->path_b, |
| 1312 |
chip_ins->chip->name_underscore, chip_ins->id); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
if (ptr->bustype == PNP) { |
| 1317 |
if (ptr->alias) { |
| 1318 |
fprintf(head, "static const pnp_devfn_t _sdev_%s = PNP_DEV(0x%02x, 0x%04x);\n", |
| 1319 |
ptr->alias, ptr->path_a, ptr->path_b); |
| 1320 |
} |
| 1321 |
fprintf(head, "extern DEVTREE_CONST struct device *const __pnp_%04x_%02x;\n", |
| 1322 |
ptr->path_a, ptr->path_b); |
| 1323 |
fprintf(fil, "DEVTREE_CONST struct device *const __pnp_%04x_%02x = &%s;\n", |
| 1324 |
ptr->path_a, ptr->path_b, ptr->name); |
| 1325 |
} |
| 1326 |
|
| 1327 |
if (ptr->alias) { |
| 1328 |
fprintf(head, "extern DEVTREE_CONST struct device *const %s_ptr;\n", ptr->name); |
| 1329 |
fprintf(fil, "DEVTREE_CONST struct device *const %s_ptr = &%s;\n", |
| 1330 |
ptr->name, ptr->name); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
static void add_siblings_to_queue(struct queue_entry **bfs_q_head, |
| 1335 |
struct device *d) |
| 1336 |
{ |
| 1337 |
while (d) { |
| 1338 |
enqueue_tail(bfs_q_head, d); |
| 1339 |
d = d->sibling; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
static void add_children_to_queue(struct queue_entry **bfs_q_head, |
| 1344 |
struct device *d) |
| 1345 |
{ |
| 1346 |
struct bus *bus = d->bus; |
| 1347 |
|
| 1348 |
if (dev_has_children(d)) |
| 1349 |
add_siblings_to_queue(bfs_q_head, bus->children); |
| 1350 |
} |
| 1351 |
|
| 1352 |
static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr, |
| 1353 |
void (*func)(FILE *, FILE *, struct device *, |
| 1354 |
struct device *)) |
| 1355 |
{ |
| 1356 |
struct queue_entry *bfs_q_head = NULL; |
| 1357 |
|
| 1358 |
enqueue_tail(&bfs_q_head, ptr); |
| 1359 |
|
| 1360 |
while ((ptr = dequeue_head(&bfs_q_head))) { |
| 1361 |
add_children_to_queue(&bfs_q_head, ptr); |
| 1362 |
func(fil, head, ptr, peek_queue_head(bfs_q_head)); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
static void emit_chip_headers(FILE *fil, struct chip *chip) |
| 1367 |
{ |
| 1368 |
struct chip *tmp = chip; |
| 1369 |
|
| 1370 |
while (chip) { |
| 1371 |
if (chip->chiph_exists) |
| 1372 |
fprintf(fil, "#include \"%s/chip.h\"\n", chip->name); |
| 1373 |
chip = chip->next; |
| 1374 |
} |
| 1375 |
fprintf(fil, "\n#if !DEVTREE_EARLY\n"); |
| 1376 |
fprintf(fil, |
| 1377 |
"__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
| 1378 |
|
| 1379 |
chip = tmp; |
| 1380 |
while (chip) { |
| 1381 |
|
| 1382 |
|
| 1383 |
if (strstr(chip->name_underscore, "cpu_") == chip->name_underscore) { |
| 1384 |
fprintf(fil, |
| 1385 |
"__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
| 1386 |
chip->name_underscore); |
| 1387 |
} else { |
| 1388 |
fprintf(fil, "extern struct chip_operations %s_ops;\n", |
| 1389 |
chip->name_underscore); |
| 1390 |
} |
| 1391 |
chip = chip->next; |
| 1392 |
} |
| 1393 |
fprintf(fil, "#endif\n"); |
| 1394 |
} |
| 1395 |
|
| 1396 |
static void emit_chip_instance(FILE *fil, struct chip_instance *instance) |
| 1397 |
{ |
| 1398 |
fprintf(fil, "STORAGE struct %s_config %s_info_%d = {", |
| 1399 |
instance->chip->name_underscore, |
| 1400 |
instance->chip->name_underscore, |
| 1401 |
instance->id); |
| 1402 |
|
| 1403 |
if (instance->reg) { |
| 1404 |
fprintf(fil, "\n"); |
| 1405 |
struct reg *r = instance->reg; |
| 1406 |
while (r) { |
| 1407 |
fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 1408 |
r = r->next; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
fprintf(fil, "};\n\n"); |
| 1412 |
} |
| 1413 |
|
| 1414 |
static void emit_chip_configs(FILE *fil) |
| 1415 |
{ |
| 1416 |
struct chip *chip = chip_header.next; |
| 1417 |
struct chip_instance *instance; |
| 1418 |
int chip_id; |
| 1419 |
|
| 1420 |
for (; chip; chip = chip->next) { |
| 1421 |
if (!chip->chiph_exists) |
| 1422 |
continue; |
| 1423 |
|
| 1424 |
chip_id = 1; |
| 1425 |
instance = chip->instance; |
| 1426 |
while (instance) { |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
if (instance->base_chip_instance == NULL) { |
| 1432 |
instance->id = chip_id++; |
| 1433 |
emit_chip_instance(fil, instance); |
| 1434 |
} |
| 1435 |
instance = instance->next; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
static void emit_identifiers(FILE *fil, const char *decl, const struct identifier *it) |
| 1441 |
{ |
| 1442 |
for (; it != NULL; it = it->next) |
| 1443 |
fprintf(fil, "extern %s %s;\n", decl, it->id); |
| 1444 |
} |
| 1445 |
|
| 1446 |
static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev, |
| 1447 |
struct device *next) |
| 1448 |
{ |
| 1449 |
struct device *p; |
| 1450 |
|
| 1451 |
if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 1452 |
|
| 1453 |
return; |
| 1454 |
} |
| 1455 |
|
| 1456 |
for (p = dev; p && p->parent->dev != p; p = p->parent->dev) { |
| 1457 |
|
| 1458 |
if (p->bustype != PCI && p->bustype != DOMAIN) |
| 1459 |
continue; |
| 1460 |
|
| 1461 |
if (p->inherit_subsystem) { |
| 1462 |
dev->subsystem_vendor = p->subsystem_vendor; |
| 1463 |
dev->subsystem_device = p->subsystem_device; |
| 1464 |
break; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
static void parse_devicetree(const char *file, struct bus *parent) |
| 1470 |
{ |
| 1471 |
FILE *filec = fopen(file, "r"); |
| 1472 |
if (!filec) { |
| 1473 |
perror(NULL); |
| 1474 |
exit(1); |
| 1475 |
} |
| 1476 |
|
| 1477 |
yyrestart(filec); |
| 1478 |
|
| 1479 |
root_parent = parent; |
| 1480 |
linenum = 0; |
| 1481 |
|
| 1482 |
yyparse(); |
| 1483 |
|
| 1484 |
fclose(filec); |
| 1485 |
} |
| 1486 |
|
| 1487 |
static int device_probe_count(struct fw_config_probe *probe) |
| 1488 |
{ |
| 1489 |
int count = 0; |
| 1490 |
while (probe) { |
| 1491 |
probe = probe->next; |
| 1492 |
count++; |
| 1493 |
} |
| 1494 |
|
| 1495 |
return count; |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
static int device_probes_match(struct device *a, struct device *b) |
| 1510 |
{ |
| 1511 |
struct fw_config_probe *a_probe = a->probe; |
| 1512 |
struct fw_config_probe *b_probe = b->probe; |
| 1513 |
int a_probe_count = device_probe_count(a_probe); |
| 1514 |
int b_probe_count = device_probe_count(b_probe); |
| 1515 |
int match_count = 0; |
| 1516 |
|
| 1517 |
while (a_probe) { |
| 1518 |
if (check_probe_exists(b_probe, a_probe->field, a_probe->option)) |
| 1519 |
match_count++; |
| 1520 |
a_probe = a_probe->next; |
| 1521 |
} |
| 1522 |
|
| 1523 |
if ((a_probe_count == b_probe_count) && (a_probe_count == match_count)) |
| 1524 |
return 1; |
| 1525 |
|
| 1526 |
if (match_count) { |
| 1527 |
printf("ERROR: devices with overlapping probes: "); |
| 1528 |
printf(a->path, a->path_a, a->path_b); |
| 1529 |
printf(b->path, b->path_a, b->path_b); |
| 1530 |
printf("\n"); |
| 1531 |
exit(1); |
| 1532 |
} |
| 1533 |
|
| 1534 |
return 0; |
| 1535 |
} |
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
static int device_match(struct device *a, struct device *b) |
| 1542 |
{ |
| 1543 |
return ((a->path_a == b->path_a) && |
| 1544 |
(a->path_b == b->path_b) && |
| 1545 |
(a->bustype == b->bustype) && |
| 1546 |
(a->chip_instance->chip == |
| 1547 |
b->chip_instance->chip)); |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
static int res_match(struct resource *a, struct resource *b) |
| 1555 |
{ |
| 1556 |
return ((a->type == b->type) && |
| 1557 |
(a->index == b->index)); |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
static void update_resource(struct device *dev, struct resource *res) |
| 1565 |
{ |
| 1566 |
struct resource *base_res = dev->res; |
| 1567 |
|
| 1568 |
while (base_res) { |
| 1569 |
if (res_match(base_res, res)) { |
| 1570 |
base_res->base = res->base; |
| 1571 |
return; |
| 1572 |
} |
| 1573 |
base_res = base_res->next; |
| 1574 |
} |
| 1575 |
|
| 1576 |
new_resource(dev, res->type, res->index, res->base); |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
static void update_register(struct reg **const head, struct reg *reg) |
| 1584 |
{ |
| 1585 |
struct reg *base_reg = *head; |
| 1586 |
|
| 1587 |
while (base_reg) { |
| 1588 |
if (!strcmp(base_reg->key, reg->key)) { |
| 1589 |
base_reg->value = reg->value; |
| 1590 |
return; |
| 1591 |
} |
| 1592 |
base_reg = base_reg->next; |
| 1593 |
} |
| 1594 |
|
| 1595 |
add_reg(head, reg->key, reg->value); |
| 1596 |
} |
| 1597 |
|
| 1598 |
static void override_devicetree(struct bus *base_parent, |
| 1599 |
struct bus *override_parent); |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
static void update_device(struct device *base_dev, struct device *override_dev) |
| 1706 |
{ |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
base_dev->enabled = override_dev->enabled; |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
base_dev->hidden = override_dev->hidden; |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
if (override_dev->subsystem_vendor || |
| 1725 |
override_dev->subsystem_device) { |
| 1726 |
base_dev->subsystem_vendor = override_dev->subsystem_vendor; |
| 1727 |
base_dev->subsystem_device = override_dev->subsystem_device; |
| 1728 |
} |
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
if (override_dev->inherit_subsystem) |
| 1736 |
base_dev->inherit_subsystem = override_dev->inherit_subsystem; |
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
struct resource *res = override_dev->res; |
| 1746 |
while (res) { |
| 1747 |
update_resource(base_dev, res); |
| 1748 |
res = res->next; |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
struct reg *reg = override_dev->chip_instance->reg; |
| 1759 |
while (reg) { |
| 1760 |
update_register(&base_dev->chip_instance->reg, reg); |
| 1761 |
reg = reg->next; |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
reg = override_dev->chip_instance->ref; |
| 1766 |
while (reg) { |
| 1767 |
update_register(&base_dev->chip_instance->ref, reg); |
| 1768 |
reg = reg->next; |
| 1769 |
} |
| 1770 |
|
| 1771 |
|
| 1772 |
if (override_dev->alias && find_alias(&base_root_dev, override_dev->alias)) { |
| 1773 |
printf("ERROR: alias already exists: %s\n", override_dev->alias); |
| 1774 |
exit(1); |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
if (base_dev->alias) |
| 1784 |
override_dev->alias = base_dev->alias; |
| 1785 |
else |
| 1786 |
base_dev->alias = override_dev->alias; |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
base_dev->probe = override_dev->probe; |
| 1793 |
base_dev->enable_on_unprovisioned_fw_config = |
| 1794 |
override_dev->enable_on_unprovisioned_fw_config; |
| 1795 |
|
| 1796 |
|
| 1797 |
base_dev->smbios_slot_type = override_dev->smbios_slot_type; |
| 1798 |
base_dev->smbios_slot_length = override_dev->smbios_slot_length; |
| 1799 |
base_dev->smbios_slot_data_width = override_dev->smbios_slot_data_width; |
| 1800 |
base_dev->smbios_slot_designation = override_dev->smbios_slot_designation; |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
override_dev->chip_instance->base_chip_instance = get_chip_instance(base_dev); |
| 1807 |
|
| 1808 |
|
| 1809 |
if (override_dev->ops_id) |
| 1810 |
base_dev->ops_id = override_dev->ops_id; |
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
struct bus *override_bus = override_dev->bus; |
| 1819 |
struct bus *base_bus = base_dev->bus; |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
if (!base_bus) |
| 1826 |
alloc_bus(base_dev); |
| 1827 |
|
| 1828 |
override_devicetree(base_dev->bus, override_dev->bus); |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
static void override_devicetree(struct bus *base_parent, |
| 1844 |
struct bus *override_parent) |
| 1845 |
{ |
| 1846 |
struct device *base_child; |
| 1847 |
struct device *override_child = override_parent->children; |
| 1848 |
struct device *next_child; |
| 1849 |
|
| 1850 |
while (override_child) { |
| 1851 |
|
| 1852 |
|
| 1853 |
for (base_child = base_parent->children; |
| 1854 |
base_child; base_child = base_child->sibling) { |
| 1855 |
if (!device_match(base_child, override_child)) |
| 1856 |
continue; |
| 1857 |
|
| 1858 |
if (base_child->probe == NULL) |
| 1859 |
break; |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
if (device_probes_match(base_child, override_child)) |
| 1865 |
break; |
| 1866 |
} |
| 1867 |
|
| 1868 |
next_child = override_child->sibling; |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
if (base_child) |
| 1875 |
update_device(base_child, override_child); |
| 1876 |
else { |
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
set_new_child(base_parent, override_child); |
| 1882 |
} |
| 1883 |
|
| 1884 |
override_child = next_child; |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
static void parse_override_devicetree(const char *file, struct device *dev) |
| 1889 |
{ |
| 1890 |
parse_devicetree(file, dev->bus); |
| 1891 |
|
| 1892 |
if (!dev_has_children(dev)) { |
| 1893 |
fprintf(stderr, "ERROR: Override tree needs at least one device!\n"); |
| 1894 |
exit(1); |
| 1895 |
} |
| 1896 |
|
| 1897 |
override_devicetree(&base_root_bus, dev->bus); |
| 1898 |
} |
| 1899 |
|
| 1900 |
static void generate_outputh(FILE *f, const char *fw_conf_header, const char *device_header) |
| 1901 |
{ |
| 1902 |
fprintf(f, "#ifndef __STATIC_DEVICE_TREE_H\n"); |
| 1903 |
fprintf(f, "#define __STATIC_DEVICE_TREE_H\n\n"); |
| 1904 |
|
| 1905 |
fprintf(f, "#include <%s>\n", fw_conf_header); |
| 1906 |
fprintf(f, "#include <%s>\n\n", device_header); |
| 1907 |
|
| 1908 |
fprintf(f, "/* Returns pointer to config structure of root device (B:D:F = 0:00:0) */\n"); |
| 1909 |
fprintf(f, "#define config_of_soc() __pci_0_00_0_config\n\n"); |
| 1910 |
|
| 1911 |
fprintf(f, "/* Macro to generate `struct device *` name that points to a device with the given alias. */\n"); |
| 1912 |
fprintf(f, "#define DEV_PTR(_alias) \t_dev_##_alias##_ptr\n\n"); |
| 1913 |
|
| 1914 |
fprintf(f, "/* Macro to generate weak `struct device *` definition that points to a device with the given\n"); |
| 1915 |
fprintf(f, " alias. */\n"); |
| 1916 |
fprintf(f, "#define WEAK_DEV_PTR(_alias)\t\t\t\\\n"); |
| 1917 |
fprintf(f, "\t__weak DEVTREE_CONST struct device *const DEV_PTR(_alias)\n"); |
| 1918 |
|
| 1919 |
fprintf(f, "\n#endif /* __STATIC_DEVICE_TREE_H */\n"); |
| 1920 |
} |
| 1921 |
|
| 1922 |
static void generate_outputc(FILE *f, const char *static_header) |
| 1923 |
{ |
| 1924 |
fprintf(f, "#include <boot/coreboot_tables.h>\n"); |
| 1925 |
fprintf(f, "#include <device/device.h>\n"); |
| 1926 |
fprintf(f, "#include <device/pci.h>\n"); |
| 1927 |
fprintf(f, "#include <fw_config.h>\n"); |
| 1928 |
fprintf(f, "#include <identity.h>\n"); |
| 1929 |
fprintf(f, "#include <%s>\n", static_header); |
| 1930 |
emit_chip_headers(f, chip_header.next); |
| 1931 |
emit_identifiers(f, "struct device_operations", device_operations); |
| 1932 |
fprintf(f, "\n#define STORAGE static __maybe_unused DEVTREE_CONST\n\n"); |
| 1933 |
|
| 1934 |
walk_device_tree(NULL, NULL, &base_root_dev, inherit_subsystem_ids); |
| 1935 |
fprintf(f, "\n/* pass 0 */\n"); |
| 1936 |
walk_device_tree(f, NULL, &base_root_dev, pass0); |
| 1937 |
walk_device_tree(NULL, NULL, &base_root_dev, update_references); |
| 1938 |
fprintf(f, "\n/* chip configs */\n"); |
| 1939 |
emit_chip_configs(f); |
| 1940 |
fprintf(f, "\n/* pass 1 */\n"); |
| 1941 |
walk_device_tree(f, NULL, &base_root_dev, pass1); |
| 1942 |
} |
| 1943 |
|
| 1944 |
static void generate_outputd(FILE *gen, FILE *dev) |
| 1945 |
{ |
| 1946 |
fprintf(dev, "#ifndef __STATIC_DEVICES_H\n"); |
| 1947 |
fprintf(dev, "#define __STATIC_DEVICES_H\n\n"); |
| 1948 |
fprintf(dev, "#include <device/pci_type.h>\n"); |
| 1949 |
fprintf(dev, "#include <device/pnp_type.h>\n"); |
| 1950 |
fprintf(dev, "#include <device/device.h>\n\n"); |
| 1951 |
fprintf(dev, "/* expose_device_names */\n"); |
| 1952 |
walk_device_tree(gen, dev, &base_root_dev, expose_device_names); |
| 1953 |
fprintf(dev, "\n#endif /* __STATIC_DEVICE_NAMES_H */\n"); |
| 1954 |
} |
| 1955 |
|
| 1956 |
static void generate_outputf(FILE *f) |
| 1957 |
{ |
| 1958 |
fprintf(f, "#ifndef __STATIC_FW_CONFIG_H\n"); |
| 1959 |
fprintf(f, "#define __STATIC_FW_CONFIG_H\n\n"); |
| 1960 |
emit_fw_config(f); |
| 1961 |
fprintf(f, "\n#endif /* __STATIC_FW_CONFIG_H */\n"); |
| 1962 |
} |
| 1963 |
|
| 1964 |
static void usage(void) |
| 1965 |
{ |
| 1966 |
printf("Usage: sconfig <options>\n"); |
| 1967 |
printf(" -c | --output_c : Path to output static.c file (required)\n"); |
| 1968 |
printf(" -r | --output_h : Path to header static.h file (required)\n"); |
| 1969 |
printf(" -d | --output_d : Path to header static_devices.h file (required)\n"); |
| 1970 |
printf(" -f | --output_f : Path to header static_fw_config.h file (required)\n"); |
| 1971 |
printf(" -m | --mainboard_devtree : Path to mainboard devicetree file (required)\n"); |
| 1972 |
printf(" -o | --override_devtree : Path to override devicetree file (optional)\n"); |
| 1973 |
printf(" -p | --chipset_devtree : Path to chipset/SOC devicetree file (optional)\n"); |
| 1974 |
|
| 1975 |
exit(1); |
| 1976 |
} |
| 1977 |
|
| 1978 |
int main(int argc, char **argv) |
| 1979 |
{ |
| 1980 |
static const struct option long_options[] = { |
| 1981 |
{ "mainboard_devtree", required_argument, NULL, 'm' }, |
| 1982 |
{ "override_devtree", required_argument, NULL, 'o' }, |
| 1983 |
{ "chipset_devtree", required_argument, NULL, 'p' }, |
| 1984 |
{ "output_c", required_argument, NULL, 'c' }, |
| 1985 |
{ "output_h", required_argument, NULL, 'r' }, |
| 1986 |
{ "output_d", required_argument, NULL, 'd' }, |
| 1987 |
{ "output_f", required_argument, NULL, 'f' }, |
| 1988 |
{ "help", no_argument, NULL, 'h' }, |
| 1989 |
{ } |
| 1990 |
}; |
| 1991 |
const char *override_devtree = NULL; |
| 1992 |
const char *base_devtree = NULL; |
| 1993 |
const char *chipset_devtree = NULL; |
| 1994 |
const char *outputc = NULL; |
| 1995 |
const char *outputh = NULL; |
| 1996 |
const char *outputd = NULL; |
| 1997 |
const char *outputf = NULL; |
| 1998 |
int opt, option_index; |
| 1999 |
|
| 2000 |
while ((opt = getopt_long(argc, argv, "m:o:p:c:r:d:f:h", long_options, |
| 2001 |
&option_index)) != EOF) { |
| 2002 |
switch (opt) { |
| 2003 |
case 'm': |
| 2004 |
base_devtree = optarg; |
| 2005 |
break; |
| 2006 |
case 'o': |
| 2007 |
override_devtree = optarg; |
| 2008 |
break; |
| 2009 |
case 'p': |
| 2010 |
chipset_devtree = optarg; |
| 2011 |
break; |
| 2012 |
case 'c': |
| 2013 |
outputc = optarg; |
| 2014 |
break; |
| 2015 |
case 'r': |
| 2016 |
outputh = optarg; |
| 2017 |
break; |
| 2018 |
case 'd': |
| 2019 |
outputd = optarg; |
| 2020 |
break; |
| 2021 |
case 'f': |
| 2022 |
outputf = optarg; |
| 2023 |
break; |
| 2024 |
case 'h': |
| 2025 |
default: |
| 2026 |
usage(); |
| 2027 |
} |
| 2028 |
} |
| 2029 |
|
| 2030 |
if (!base_devtree || !outputc || !outputh || !outputd || !outputf) |
| 2031 |
usage(); |
| 2032 |
|
| 2033 |
if (chipset_devtree) { |
| 2034 |
|
| 2035 |
|
| 2036 |
parse_devicetree(chipset_devtree, &base_root_bus); |
| 2037 |
parse_override_devicetree(base_devtree, &chipset_root_dev); |
| 2038 |
} else { |
| 2039 |
parse_devicetree(base_devtree, &base_root_bus); |
| 2040 |
} |
| 2041 |
|
| 2042 |
if (override_devtree) |
| 2043 |
parse_override_devicetree(override_devtree, &override_root_dev); |
| 2044 |
|
| 2045 |
|
| 2046 |
FILE *autogen = fopen(outputc, "w"); |
| 2047 |
if (!autogen) { |
| 2048 |
fprintf(stderr, "Could not open file '%s' for writing: ", |
| 2049 |
outputc); |
| 2050 |
perror(NULL); |
| 2051 |
exit(1); |
| 2052 |
} |
| 2053 |
|
| 2054 |
FILE *autohead = fopen(outputh, "w"); |
| 2055 |
if (!autohead) { |
| 2056 |
fprintf(stderr, "Could not open file '%s' for writing: ", outputh); |
| 2057 |
perror(NULL); |
| 2058 |
fclose(autogen); |
| 2059 |
exit(1); |
| 2060 |
} |
| 2061 |
|
| 2062 |
FILE *autodev = fopen(outputd, "w"); |
| 2063 |
if (!autodev) { |
| 2064 |
fprintf(stderr, "Could not open file '%s' for writing: ", outputd); |
| 2065 |
perror(NULL); |
| 2066 |
fclose(autogen); |
| 2067 |
fclose(autohead); |
| 2068 |
exit(1); |
| 2069 |
} |
| 2070 |
|
| 2071 |
FILE *autofwconf = fopen(outputf, "w"); |
| 2072 |
if (!autofwconf) { |
| 2073 |
fprintf(stderr, "Could not open file '%s' for writing: ", outputf); |
| 2074 |
perror(NULL); |
| 2075 |
fclose(autogen); |
| 2076 |
fclose(autohead); |
| 2077 |
fclose(autodev); |
| 2078 |
exit(1); |
| 2079 |
} |
| 2080 |
|
| 2081 |
char *f = strdup(outputf); |
| 2082 |
assert(f); |
| 2083 |
char *d = strdup(outputd); |
| 2084 |
assert(d); |
| 2085 |
char *h = strdup(outputh); |
| 2086 |
assert(h); |
| 2087 |
|
| 2088 |
const char *fw_conf_header = basename(f); |
| 2089 |
const char *device_header = basename(d); |
| 2090 |
const char *static_header = basename(h); |
| 2091 |
|
| 2092 |
generate_outputh(autohead, fw_conf_header, device_header); |
| 2093 |
generate_outputc(autogen, static_header); |
| 2094 |
generate_outputd(autogen, autodev); |
| 2095 |
generate_outputf(autofwconf); |
| 2096 |
|
| 2097 |
fclose(autohead); |
| 2098 |
fclose(autogen); |
| 2099 |
fclose(autodev); |
| 2100 |
fclose(autofwconf); |
| 2101 |
free(f); |
| 2102 |
free(d); |
| 2103 |
free(h); |
| 2104 |
|
| 2105 |
return 0; |
| 2106 |
} |