[coreboot-gerrit] Change in coreboot[master]: util/sconfig: Get rid of struct header

Furquan Shaikh (Code Review) gerrit at coreboot.org
Thu May 31 19:39:27 CEST 2018


Furquan Shaikh has uploaded this change for review. ( https://review.coreboot.org/26739


Change subject: util/sconfig: Get rid of struct header
......................................................................

util/sconfig: Get rid of struct header

Now that chips and devices are treated differently in sconfig, this
change gets rid of struct header and add_header function which were
responsible for maintaining list of headers that need to be added to
static.c.

Instead, struct chip is re-factored into struct chip and
struct chip_instance, where chip is a list of unique chips required by
the mainboard whereas chip_instance is an instance of the chip. One
chip can have multiple instances dependending upon the devices in the
system. Also, struct device is updated to hold a pointer to chip
instance instead of the chip structure. This unique list of chips is
then used to add appropriate headers to static.c

BUG=b:80081934
TEST=Verified using abuild that all boards compile successfully.

Change-Id: I6fccdf7c361b4f55a831195adcda9b21932755aa
Signed-off-by: Furquan Shaikh <furquan at google.com>
---
M util/sconfig/main.c
M util/sconfig/sconfig.h
M util/sconfig/sconfig.tab.c_shipped
M util/sconfig/sconfig.y
4 files changed, 135 insertions(+), 111 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/39/26739/1

diff --git a/util/sconfig/main.c b/util/sconfig/main.c
index 8287cce..32d7aac 100644
--- a/util/sconfig/main.c
+++ b/util/sconfig/main.c
@@ -22,7 +22,7 @@
 
 struct device *head, *lastdev;
 
-static struct chip *chip_head;
+static struct chip chip_header;
 
 /*
  * This is intentionally shared between chip and device structure ids because it
@@ -38,15 +38,24 @@
 } translate_t;
 
 static struct device root;
-static struct chip mainboard = {
+
+static struct chip_instance mainboard_instance;
+
+static struct chip mainboard_chip = {
 	.name = "mainboard",
 	.name_underscore = "mainboard",
+	.instance = &mainboard_instance,
+};
+
+static struct chip_instance mainboard_instance = {
+	.id = 0,
+	.chip = &mainboard_chip,
 };
 
 static struct device root = {
 	.name = "dev_root",
 	.id = 0,
-	.chip = &mainboard,
+	.chip_instance = &mainboard_instance,
 	.path = " .type = DEVICE_PATH_ROOT ",
 	.ops = "&default_dev_ops_root",
 	.parent = &root,
@@ -205,14 +214,28 @@
 	return b;
 }
 
-struct chip *new_chip(char *path)
+static struct chip *get_chip(char *path)
 {
-	struct chip *new_chip = S_ALLOC(sizeof(*new_chip));
+	struct chip *h = &chip_header;
 
-	new_chip->id = ++count;
+	while (h->next) {
+		int result = strcmp(path, h->next->name);
+		if (result == 0)
+			return h->next;
+
+		if (result < 0)
+			break;
+
+		h = h->next;
+	}
+
+	struct chip *new_chip = S_ALLOC(sizeof(struct chip));
+	new_chip->next = h->next;
+	h->next = new_chip;
+
 	new_chip->chiph_exists = 1;
 	new_chip->name = path;
-	new_chip->name_underscore = translate_name(new_chip->name, UNSLASH);
+	new_chip->name_underscore = translate_name(path, UNSLASH);
 
 	struct stat st;
 	char *chip_h = S_ALLOC(strlen(path) + 18);
@@ -235,13 +258,24 @@
 
 	free(chip_h);
 
-	new_chip->next = chip_head;
-	chip_head = new_chip;
-
 	return new_chip;
 }
 
-struct device *new_device(struct device *parent, struct chip *chip,
+struct chip_instance *new_chip_instance(char *path)
+{
+	struct chip *chip = get_chip(path);
+	struct chip_instance *instance = S_ALLOC(sizeof(*instance));
+
+	instance->id = ++count;
+	instance->chip = chip;
+	instance->next = chip->instance;
+	chip->instance = instance;
+
+	return instance;
+}
+
+struct device *new_device(struct device *parent,
+			  struct chip_instance *chip_instance,
 			  const int bustype, const char *devnum,
 			  int enabled)
 {
@@ -260,7 +294,7 @@
 	new_d->name = name;
 
 	new_d->enabled = enabled;
-	new_d->chip = chip;
+	new_d->chip_instance = chip_instance;
 
 	if (parent->latestchild) {
 		parent->latestchild->next_sibling = new_d;
@@ -367,14 +401,14 @@
 	dev->rescnt++;
 }
 
-void add_register(struct chip *chip, char *name, char *val)
+void add_register(struct chip_instance *chip_instance, char *name, char *val)
 {
 	struct reg *r = S_ALLOC(sizeof(struct reg));
 
 	r->key = name;
 	r->value = val;
-	if (chip->reg) {
-		struct reg *head = chip->reg;
+	if (chip_instance->reg) {
+		struct reg *head = chip_instance->reg;
 		// sorting to be equal to sconfig's behaviour
 		int sort = strcmp(r->key, head->key);
 		if (sort == 0) {
@@ -383,7 +417,7 @@
 		}
 		if (sort < 0) {
 			r->next = head;
-			chip->reg = r;
+			chip_instance->reg = r;
 		} else {
 			while ((head->next)
 			       && (strcmp(head->next->key, r->key) < 0))
@@ -392,7 +426,7 @@
 			head->next = r;
 		}
 	} else {
-		chip->reg = r;
+		chip_instance->reg = r;
 	}
 }
 
@@ -458,6 +492,7 @@
 static void pass1(FILE * fil, struct device *ptr)
 {
 	int pin;
+	struct chip_instance *chip_ins = ptr->chip_instance;
 
 	if (!ptr->used) {
 		if (ptr->id != 0)
@@ -504,13 +539,13 @@
 		if (ptr->sibling)
 			fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
 		fprintf(fil, "#if !DEVTREE_EARLY\n");
-		fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
-		if (ptr->chip == &mainboard)
+		fprintf(fil, "\t.chip_ops = &%s_ops,\n", chip_ins->chip->name_underscore);
+		if (chip_ins == &mainboard_instance)
 			fprintf(fil, "\t.name = mainboard_name,\n");
 		fprintf(fil, "#endif\n");
-		if (ptr->chip->chiph_exists)
+		if (chip_ins->chip->chiph_exists)
 			fprintf(fil, "\t.chip_info = &%s_info_%d,\n",
-				ptr->chip->name_underscore, ptr->chip->id);
+				chip_ins->chip->name_underscore, chip_ins->id);
 		if (ptr->nextdev)
 			fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
 		fprintf(fil, "};\n");
@@ -594,89 +629,70 @@
 	} while (ptr);
 }
 
-static void add_header(struct chip *chip, struct header *h)
+static void emit_chip_headers(FILE *fil)
 {
-	int include_exists = 0;
-
-	while (h->next) {
-		int result = strcmp(chip->name, h->next->name);
-		if (result == 0) {
-			include_exists = 1;
-			break;
-		}
-		if (result < 0)
-			break;
-		h = h->next;
-	}
-
-	if (!include_exists) {
-		struct header *tmp = h->next;
-		h->next = S_ALLOC(sizeof(struct header));
-
-		h->next->chiph_exists = chip->chiph_exists;
-		h->next->name = chip->name;
-		h->next->next = tmp;
-	}
-}
-
-static void emit_headers(FILE *fil)
-{
-	struct header *h;
-	struct chip *chip;
-	struct header headers = {};
-
-	for (chip = chip_head; chip; chip = chip->next)
-		add_header(chip, &headers);
+	struct chip *chip = chip_header.next;
 
 	fprintf(fil, "#include <device/device.h>\n");
 	fprintf(fil, "#include <device/pci.h>\n");
-	h = &headers;
-	while (h->next) {
-		h = h->next;
-		if (h->chiph_exists)
-			fprintf(fil, "#include \"%s/chip.h\"\n", h->name);
+
+	while (chip) {
+		if (chip->chiph_exists)
+			fprintf(fil, "#include \"%s/chip.h\"\n", chip->name);
+		chip = chip->next;
 	}
 	fprintf(fil, "\n#if !DEVTREE_EARLY\n");
 	fprintf(fil,
 		"__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
-	h = &headers;
-	while (h->next) {
-		h = h->next;
-		char *name_underscore = translate_name(h->name, UNSLASH);
+
+	chip = chip_header.next;
+	while (chip) {
 		fprintf(fil,
 			"__attribute__((weak)) struct chip_operations %s_ops = {};\n",
-			name_underscore);
-		free(name_underscore);
+			chip->name_underscore);
+		chip = chip->next;
 	}
 	fprintf(fil, "#endif\n");
 }
 
+static void emit_chip_instance(FILE *fil, struct chip_instance *instance)
+{
+	if (instance->reg) {
+		fprintf(fil,
+			"DEVTREE_CONST struct %s_config %s_info_%d = {\n",
+			instance->chip->name_underscore,
+			instance->chip->name_underscore,
+			instance->id);
+		struct reg *r = instance->reg;
+		while (r) {
+			fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
+			r = r->next;
+		}
+		fprintf(fil, "};\n\n");
+	} else {
+		fprintf(fil,
+			"DEVTREE_CONST struct %s_config %s_info_%d = { };\n",
+			instance->chip->name_underscore,
+			instance->chip->name_underscore,
+			instance->id);
+	}
+}
+
 static void emit_chips(FILE *fil)
 {
-	struct chip *chip;
+	struct chip *chip = chip_header.next;
+	struct chip_instance *instance;
 
-	emit_headers(fil);
+	emit_chip_headers(fil);
 
-	for (chip = chip_head; chip; chip = chip->next) {
+	for (; chip; chip = chip->next) {
 		if (!chip->chiph_exists)
 			continue;
 
-		if (chip->reg) {
-			fprintf(fil,
-				"DEVTREE_CONST struct %s_config %s_info_%d = {\n",
-				chip->name_underscore, chip->name_underscore,
-				chip->id);
-			struct reg *r = chip->reg;
-			while (r) {
-				fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
-				r = r->next;
-			}
-			fprintf(fil, "};\n\n");
-		} else {
-			fprintf(fil,
-				"DEVTREE_CONST struct %s_config %s_info_%d = { };\n",
-				chip->name_underscore, chip->name_underscore,
-				chip->id);
+		instance = chip->instance;
+		while (instance) {
+			emit_chip_instance(fil, instance);
+			instance = instance->next;
 		}
 	}
 }
diff --git a/util/sconfig/sconfig.h b/util/sconfig/sconfig.h
index b9df91e..33176ae 100644
--- a/util/sconfig/sconfig.h
+++ b/util/sconfig/sconfig.h
@@ -42,13 +42,25 @@
 	int ioapic_dst_id;
 };
 
-struct chip {
+struct chip;
+struct chip_instance {
 	/*
 	 * Monotonically increasing ID for each newly allocated
 	 * node(chip/device).
 	 */
 	int id;
 
+	/* Pointer to registers for this chip. */
+	struct reg *reg;
+
+	/* Pointer to chip of which this is instance. */
+	struct chip *chip;
+
+	/* Pointer to next instance of the same chip. */
+	struct chip_instance *next;
+};
+
+struct chip {
 	/* Indicates if chip header exists for this chip. */
 	int chiph_exists;
 
@@ -58,8 +70,8 @@
 	/* Name of current chip normalized to _. */
 	char *name_underscore;
 
-	/* Pointer to registers for this chip. */
-	struct reg *reg;
+	/* Pointer to first instance of this chip. */
+	struct chip_instance *instance;
 
 	/* Pointer to next chip. */
 	struct chip *next;
@@ -93,28 +105,21 @@
 	struct device *sibling;
 	struct resource *res;
 
-	struct chip *chip;
+	struct chip_instance *chip_instance;
 };
 
 extern struct device *head;
 
-struct header;
-struct header {
-	char *name;
-	int chiph_exists;
-	struct header *next;
-};
-
 void fold_in(struct device *parent);
 
 void postprocess_devtree(void);
-struct chip *new_chip(char *path);
-struct device *new_device(struct device *parent, struct chip *chip,
+
+struct device *new_device(struct device *parent,
+			  struct chip_instance *chip_instance,
 			  const int bustype, const char *devnum,
 			  int enabled);
 void alias_siblings(struct device *d);
 void add_resource(struct device *dev, int type, int index, int base);
-void add_register(struct chip *chip, char *name, char *val);
 void add_pci_subsystem_ids(struct device *dev, int vendor, int device,
 			   int inherit);
 void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin,
@@ -124,3 +129,6 @@
 
 void enqueue(void *data);
 void *dequeue(void);
+
+struct chip_instance *new_chip_instance(char *path);
+void add_register(struct chip_instance *chip, char *name, char *val);
diff --git a/util/sconfig/sconfig.tab.c_shipped b/util/sconfig/sconfig.tab.c_shipped
index 17bb31f..e774c2d 100644
--- a/util/sconfig/sconfig.tab.c_shipped
+++ b/util/sconfig/sconfig.tab.c_shipped
@@ -86,7 +86,7 @@
 void yyerror(const char *s);
 
 static struct device *cur_parent;
-static struct chip *cur_chip;
+static struct chip *cur_chip_instance;
 
 
 
@@ -1300,9 +1300,9 @@
   case 14:
 
     {
-	(yyval.chip) = new_chip((yyvsp[0].string));
-	enqueue(cur_chip);
-	cur_chip = (yyval.chip);
+	(yyval.chip) = new_chip_instance((yyvsp[0].string));
+	enqueue(cur_chip_instance);
+	cur_chip_instance = (yyval.chip);
 }
 
     break;
@@ -1310,7 +1310,7 @@
   case 15:
 
     {
-	cur_chip = dequeue();
+	cur_chip_instance = dequeue();
 }
 
     break;
@@ -1318,7 +1318,7 @@
   case 16:
 
     {
-	(yyval.device) = new_device(cur_parent, cur_chip, (yyvsp[-2].number), (yyvsp[-1].string), (yyvsp[0].number));
+	(yyval.device) = new_device(cur_parent, cur_chip_instance, (yyvsp[-2].number), (yyvsp[-1].string), (yyvsp[0].number));
 	cur_parent = (yyval.device);
 }
 
@@ -1342,7 +1342,7 @@
 
   case 19:
 
-    { add_register(cur_chip, (yyvsp[-2].string), (yyvsp[0].string)); }
+    { add_register(cur_chip_instance, (yyvsp[-2].string), (yyvsp[0].string)); }
 
     break;
 
diff --git a/util/sconfig/sconfig.y b/util/sconfig/sconfig.y
index 01ce92d..a1bcdee 100755
--- a/util/sconfig/sconfig.y
+++ b/util/sconfig/sconfig.y
@@ -21,7 +21,7 @@
 void yyerror(const char *s);
 
 static struct device *cur_parent;
-static struct chip *cur_chip;
+static struct chip *cur_chip_instance;
 
 %}
 %union {
@@ -40,16 +40,16 @@
 devicechildren: devicechildren device | devicechildren chip | devicechildren resource | devicechildren subsystemid | devicechildren ioapic_irq | /* empty */ ;
 
 chip: CHIP STRING /* == path */ {
-	$<chip>$ = new_chip($<string>2);
-	enqueue(cur_chip);
-	cur_chip = $<chip>$;
+	$<chip>$ = new_chip_instance($<string>2);
+	enqueue(cur_chip_instance);
+	cur_chip_instance = $<chip>$;
 }
 	chipchildren END {
-	cur_chip = dequeue();
+	cur_chip_instance = dequeue();
 };
 
 device: DEVICE BUS NUMBER /* == devnum */ BOOL {
-	$<device>$ = new_device(cur_parent, cur_chip, $<number>2, $<string>3, $<number>4);
+	$<device>$ = new_device(cur_parent, cur_chip_instance, $<number>2, $<string>3, $<number>4);
 	cur_parent = $<device>$;
 }
 	devicechildren END {
@@ -62,7 +62,7 @@
 	{ add_resource(cur_parent, $<number>1, strtol($<string>2, NULL, 0), strtol($<string>4, NULL, 0)); } ;
 
 registers: REGISTER STRING /* == regname */ EQUALS STRING /* == regval */
-	{ add_register(cur_chip, $<string>2, $<string>4); } ;
+	{ add_register(cur_chip_instance, $<string>2, $<string>4); } ;
 
 subsystemid: SUBSYSTEMID NUMBER NUMBER
 	{ add_pci_subsystem_ids(cur_parent, strtol($<string>2, NULL, 16), strtol($<string>3, NULL, 16), 0); };

-- 
To view, visit https://review.coreboot.org/26739
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6fccdf7c361b4f55a831195adcda9b21932755aa
Gerrit-Change-Number: 26739
Gerrit-PatchSet: 1
Gerrit-Owner: Furquan Shaikh <furquan at google.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20180531/2b6abf07/attachment-0001.html>


More information about the coreboot-gerrit mailing list