#include "sconfig.h"
#include "sconfig.tab.h"
+extern int linenum;
+
struct device *head, *lastdev;
struct header headers;
.enabled = 1
};
-static struct device *new_dev() {
+static struct device *new_dev(struct device *parent, struct device *bus) {
struct device *dev = malloc(sizeof(struct device));
memset(dev, 0, sizeof(struct device));
dev->id = ++devcount;
- dev->parent = cur_parent;
- dev->bus = cur_bus;
+ dev->parent = parent;
+ dev->bus = bus;
+ dev->subsystem_vendor = -1;
+ dev->subsystem_device = -1;
head->next = dev;
head = dev;
return dev;
void yyerror (char const *str)
{
- fprintf (stderr, "%s\n", str);
+ extern char *yytext;
+ fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
+ exit(1);
}
void postprocess_devtree(void) {
}
}
-struct device *new_chip(char *path) {
- struct device *new_chip = new_dev();
+struct device *new_chip(struct device *parent, struct device *bus, char *path) {
+ struct device *new_chip = new_dev(parent, bus);
new_chip->chiph_exists = 1;
new_chip->name = path;
new_chip->name_underscore = strdup(new_chip->name);
struct stat st;
char *chip_h = malloc(strlen(path)+12);
+ sprintf(chip_h, "src/%s", path);
+ if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
+ fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
+ path);
+ exit(1);
+ }
+
sprintf(chip_h, "src/%s/chip.h", path);
if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
new_chip->chiph_exists = 0;
- if (cur_parent->latestchild) {
- cur_parent->latestchild->next_sibling = new_chip;
- cur_parent->latestchild->sibling = new_chip;
+ if (parent->latestchild) {
+ parent->latestchild->next_sibling = new_chip;
+ parent->latestchild->sibling = new_chip;
}
- cur_parent->latestchild = new_chip;
- if (!cur_parent->children)
- cur_parent->children = new_chip;
+ parent->latestchild = new_chip;
+ if (!parent->children)
+ parent->children = new_chip;
return new_chip;
}
}
}
-struct device *new_device(const int bus, const char *devnum, int enabled) {
- struct device *new_d = new_dev();
+struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
+ struct device *new_d = new_dev(parent, busdev);
new_d->bustype = bus;
char *tmp;
new_d->enabled = enabled;
new_d->chip = new_d->parent->chip;
- if (cur_parent->latestchild) {
- cur_parent->latestchild->next_sibling = new_d;
- cur_parent->latestchild->sibling = new_d;
+ if (parent->latestchild) {
+ parent->latestchild->next_sibling = new_d;
+ parent->latestchild->sibling = new_d;
}
- cur_parent->latestchild = new_d;
- if (!cur_parent->children)
- cur_parent->children = new_d;
+ parent->latestchild = new_d;
+ if (!parent->children)
+ parent->children = new_d;
lastdev->nextdev = new_d;
lastdev = new_d;
if (device_match(d, cmp)) {
d->multidev = 1;
- cmp->aliased_name = malloc(12);
- sprintf(cmp->aliased_name, "_dev%d", cmp->id);
cmp->id = d->id;
cmp->name = d->name;
cmp->used = 1;
}
}
-void add_resource(int type, int index, int base) {
+void add_resource(struct device *dev, int type, int index, int base) {
struct resource *r = malloc(sizeof(struct resource));
memset (r, 0, sizeof(struct resource));
r->type = type;
r->index = index;
r->base = base;
- if (cur_parent->res) {
- struct resource *head = cur_parent->res;
+ if (dev->res) {
+ struct resource *head = dev->res;
while (head->next) head = head->next;
head->next = r;
} else {
- cur_parent->res = r;
+ dev->res = r;
}
- cur_parent->rescnt++;
+ dev->rescnt++;
}
-void add_register(char *name, char *val) {
+void add_register(struct device *dev, char *name, char *val) {
struct reg *r = malloc(sizeof(struct reg));
memset (r, 0, sizeof(struct reg));
r->key = name;
r->value = val;
- if (cur_parent->reg) {
- struct reg *head = cur_parent->reg;
+ if (dev->reg) {
+ struct reg *head = dev->reg;
// sorting to be equal to sconfig's behaviour
int sort = strcmp(r->key, head->key);
if (sort == 0) {
}
if (sort<0) {
r->next = head;
- cur_parent->reg = r;
+ dev->reg = r;
} else {
while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
r->next = head->next;
head->next = r;
}
} else {
- cur_parent->reg = r;
+ dev->reg = r;
+ }
+}
+
+void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
+{
+ if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
+ printf("ERROR: 'subsystem' only allowed for PCI devices\n");
+ exit(1);
}
+
+ dev->subsystem_vendor = vendor;
+ dev->subsystem_device = device;
+ dev->inherit_subsystem = inherit;
}
static void pass0(FILE *fil, struct device *ptr) {
- if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used))
- fprintf(fil, "struct device %s;\n", ptr->name);
- if ((ptr->type == device) && (ptr->id != 0) && ptr->used)
- fprintf(fil, "struct device %s;\n", ptr->aliased_name);
+ if (ptr->type == device && ptr->id == 0)
+ fprintf(fil, "struct bus %s_links[];\n", ptr->name);
+ if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
+ fprintf(fil, "static struct device %s;\n", ptr->name);
+ if (ptr->rescnt > 0)
+ fprintf(fil, "struct resource %s_res[];\n", ptr->name);
+ if (ptr->children || ptr->multidev)
+ fprintf(fil, "struct bus %s_links[];\n", ptr->name);
+ }
}
static void pass1(FILE *fil, struct device *ptr) {
if (!ptr->used && (ptr->type == device)) {
+ if (ptr->id != 0)
+ fprintf(fil, "static ", ptr->name);
fprintf(fil, "struct device %s = {\n", ptr->name);
fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
- fprintf(fil, "\t.bus = &%s.link[%d],\n", ptr->bus->name, ptr->bus->link);
+ fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
fprintf(fil, "\t.path = {");
fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
fprintf(fil, "},\n");
fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
fprintf(fil, "\t.on_mainboard = 1,\n");
+ if (ptr->subsystem_vendor > 0)
+ fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
+
+ if (ptr->subsystem_device > 0)
+ fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
+
if (ptr->rescnt > 0) {
- fprintf(fil, "\t.resources = %d,\n", ptr->rescnt);
- fprintf(fil, "\t.resource = {\n");
- struct resource *r = ptr->res;
- while (r) {
- fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
- if (r->type == IRQ) fprintf(fil, "IRQ");
- if (r->type == DRQ) fprintf(fil, "DRQ");
- if (r->type == IO) fprintf(fil, "IO");
- fprintf(fil, ", .index=0x%x, .base=0x%x},\n", r->index, r->base);
- r = r->next;
- }
- fprintf(fil, "\t },\n");
+ fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
}
int link = 0;
- fprintf(fil, "\t.link = {\n");
+ if (ptr->children || ptr->multidev)
+ fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
+ else
+ fprintf(fil, "\t.link_list = NULL,\n", ptr->name);
+ if (ptr->sibling)
+ fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
+ if (ptr->chip->chiph_exists) {
+ fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
+ fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
+ }
+ if (ptr->nextdev)
+ fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
+ fprintf(fil, "};\n");
+ }
+ if (ptr->rescnt > 0) {
+ int i=1;
+ fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
+ struct resource *r = ptr->res;
+ while (r) {
+ fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
+ if (r->type == IRQ) fprintf(fil, "IRQ");
+ if (r->type == DRQ) fprintf(fil, "DRQ");
+ if (r->type == IO) fprintf(fil, "IO");
+ fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
+ if (r->next)
+ fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
+ else
+ fprintf(fil, ".next=NULL },\n");
+ r = r->next;
+ }
+ fprintf(fil, "\t };\n");
+ }
+ if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
+ fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
if (ptr->multidev) {
struct device *d = ptr;
while (d) {
if (device_match(d, ptr)) {
fprintf(fil, "\t\t[%d] = {\n", d->link);
- fprintf(fil, "\t\t\t.link = %d,\n", d->link);
+ fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
if (d->children)
fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
+ if (d->next_sibling && device_match(d->next_sibling, ptr))
+ fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
+ else
+ fprintf(fil, "\t\t\t.next = NULL,\n");
fprintf(fil, "\t\t},\n");
- link++;
}
d = d->next_sibling;
}
} else {
if (ptr->children) {
fprintf(fil, "\t\t[0] = {\n");
- fprintf(fil, "\t\t\t.link = 0,\n");
+ fprintf(fil, "\t\t\t.link_num = 0,\n");
fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
+ fprintf(fil, "\t\t\t.next = NULL,\n");
fprintf(fil, "\t\t},\n");
- link++;
}
}
- fprintf(fil, "\t},\n");
- fprintf(fil, "\t.links = %d,\n", link);
- if (ptr->sibling)
- fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
- if (ptr->chip->chiph_exists) {
- fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
- fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
- }
- if (ptr->nextdev)
- fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
- fprintf(fil, "};\n");
+ fprintf(fil, "\t};\n");
}
if ((ptr->type == chip) && (ptr->chiph_exists)) {
if (ptr->reg) {
} while (ptr);
}
+static void inherit_subsystem_ids(FILE *file, struct device *dev)
+{
+ struct device *p;
+
+ if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
+ /* user already gave us a subsystem vendor/device */
+ return;
+ }
+
+ for(p = dev; p && p != p->parent; p = p->parent) {
+
+ if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
+ continue;
+
+ if (p->inherit_subsystem) {
+ dev->subsystem_vendor = p->subsystem_vendor;
+ dev->subsystem_device = p->subsystem_device;
+ break;
+ }
+ }
+}
+
int main(int argc, char** argv) {
if (argc != 3) {
printf("usage: sconfig vendor/mainboard outputdir\n");
sprintf(headers.next->name, "mainboard/%s", mainboard);
FILE *filec = fopen(devtree, "r");
+ if (!filec) {
+ fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
+ perror(NULL);
+ exit(1);
+ }
+
yyrestart(filec);
- FILE *staticc = fopen(outputc, "w");
+ lastdev = head = &root;
- cur_bus = cur_parent = lastdev = head = &root;
yyparse();
+
fclose(filec);
if ((head->type == chip) && (!head->chiph_exists)) {
while (head->next != tmp) head = head->next;
}
+ FILE *staticc = fopen(outputc, "w");
+ if (!staticc) {
+ fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
+ perror(NULL);
+ exit(1);
+ }
+
fprintf(staticc, "#include <device/device.h>\n");
fprintf(staticc, "#include <device/pci.h>\n");
struct header *h = &headers;
h = h->next;
fprintf(staticc, "#include \"%s/chip.h\"\n", h->name);
}
+
+ walk_device_tree(staticc, &root, inherit_subsystem_ids, NULL);
+
fprintf(staticc, "\n/* pass 0 */\n");
walk_device_tree(staticc, &root, pass0, NULL);
- fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device **last_dev_p = &%s.next;\n", lastdev->name);
+ fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device *last_dev = &%s;\n", lastdev->name);
walk_device_tree(staticc, &root, pass1, NULL);
fclose(staticc);
+
return 0;
}