device/device.h: Drop multiple links

Multiple links are unused throughout the tree and make the code more
confusing as an iteration over all busses is needed to get downstream
devices. This also not done consistently e.g. the allocator does not
care about multiple links on busses. A better way of dealing multiple
links below a device is to feature dummy devices with each their
respective bus.

This drops the sconfig capability to declare the same device multiple
times which was previously used to declare multiple links.

Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Change-Id: Iab6fe269faef46ae77ed1ea425440cf5c7dbd49b
Reviewed-on: https://review.coreboot.org/c/coreboot/+/78328
Reviewed-by: Felix Held <felix-coreboot@felixheld.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Jincheng Li <jincheng.li@intel.com>
Reviewed-by: Lean Sheng Tan <sheng.tan@9elements.com>
diff --git a/util/sconfig/main.c b/util/sconfig/main.c
index dd26e07..533c9fb 100644
--- a/util/sconfig/main.c
+++ b/util/sconfig/main.c
@@ -81,7 +81,6 @@
 static struct chip_instance mainboard_instance;
 
 static struct bus base_root_bus = {
-	.id = 0,
 	.dev = &base_root_dev,
 };
 
@@ -95,7 +94,6 @@
 };
 
 static struct bus chipset_root_bus = {
-	.id = 0,
 	.dev = &chipset_root_dev,
 };
 
@@ -109,7 +107,6 @@
 };
 
 static struct bus override_root_bus = {
-	.id = 0,
 	.dev = &override_root_dev,
 };
 
@@ -723,30 +720,13 @@
 	bus->dev->ops_id = ops_id;
 }
 
-/*
- * Allocate a new bus for the provided device.
- *   - If this is the first bus being allocated under this device, then its id
- *     is set to 0 and bus and last_bus are pointed to the newly allocated bus.
- *   - If this is not the first bus under this device, then its id is set to 1
- *     plus the id of last bus and newly allocated bus is added to the list of
- *     buses under the device. last_bus is updated to point to the newly
- *     allocated bus.
- */
+/* Allocate a new bus for the provided device. */
 static void alloc_bus(struct device *dev)
 {
 	struct bus *bus = S_ALLOC(sizeof(*bus));
 
 	bus->dev = dev;
-
-	if (dev->last_bus == NULL)  {
-		bus->id = 0;
-		dev->bus = bus;
-	} else {
-		bus->id = dev->last_bus->id + 1;
-		dev->last_bus->next_bus = bus;
-	}
-
-	dev->last_bus = bus;
+	dev->bus = bus;
 }
 
 /*
@@ -814,14 +794,15 @@
 	if (parent->alias && !strcmp(parent->alias, alias))
 		return parent;
 
-	const struct bus *bus;
-	for (bus = parent->bus; bus; bus = bus->next_bus) {
-		const struct device *child;
-		for (child = bus->children; child; child = child->sibling) {
-			const struct device *const ret = find_alias(child, alias);
-			if (ret)
-				return ret;
-		}
+	const struct bus *bus = parent->bus;
+	if (!bus)
+		return NULL;
+
+	const struct device *child;
+	for (child = bus->children; child; child = child->sibling) {
+		const struct device *const ret = find_alias(child, alias);
+		if (ret)
+			return ret;
 	}
 
 	return NULL;
@@ -834,11 +815,11 @@
 {
 	struct device *new_d;
 
-	/* If device is found under parent, no need to allocate new device. */
+	/* We don't allow duplicate devices in devicetree. */
 	new_d = get_dev(parent, path_a, path_b, bustype, chip_instance);
 	if (new_d) {
-		alloc_bus(new_d);
-		return new_d;
+		printf("ERROR: Duplicate device! %s\n", new_d->name);
+		exit(1);
 	}
 
 	new_d = alloc_dev(parent);
@@ -1084,11 +1065,8 @@
 {
 	struct bus *bus = dev->bus;
 
-	while (bus) {
-		if (bus->children)
-			return 1;
-		bus = bus->next_bus;
-	}
+	if (bus && bus->children)
+		return 1;
 
 	return 0;
 }
@@ -1098,7 +1076,7 @@
 	static int dev_id;
 
 	if (ptr == &base_root_dev) {
-		fprintf(fil, "STORAGE struct bus %s_links[];\n",
+		fprintf(fil, "STORAGE struct bus %s_bus;\n",
 			ptr->name);
 		return;
 	}
@@ -1120,7 +1098,7 @@
 		fprintf(fil, "STORAGE struct resource %s_res[];\n",
 			ptr->name);
 	if (dev_has_children(ptr))
-		fprintf(fil, "STORAGE struct bus %s_links[];\n",
+		fprintf(fil, "STORAGE struct bus %s_bus;\n",
 			ptr->name);
 
 	if (next)
@@ -1192,35 +1170,18 @@
 	fprintf(fil, "\t };\n");
 }
 
-static void emit_bus(FILE *fil, struct bus *bus)
+static void emit_dev_bus(FILE *fil, struct device *ptr)
 {
-	fprintf(fil, "\t\t[%d] = {\n", bus->id);
-	fprintf(fil, "\t\t\t.link_num = %d,\n", bus->id);
-	fprintf(fil, "\t\t\t.dev = &%s,\n", bus->dev->name);
-	if (bus->children)
-		fprintf(fil, "\t\t\t.children = &%s,\n", bus->children->name);
-
-	if (bus->next_bus)
-		fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", bus->dev->name,
-			bus->id + 1);
-	else
-		fprintf(fil, "\t\t\t.next = NULL,\n");
-	fprintf(fil, "\t\t},\n");
-}
-
-static void emit_dev_links(FILE *fil, struct device *ptr)
-{
-	fprintf(fil, "STORAGE struct bus %s_links[] = {\n",
+	fprintf(fil, "STORAGE struct bus %s_bus = {\n",
 		ptr->name);
 
+	assert(ptr->bus && ptr->bus->children);
 	struct bus *bus = ptr->bus;
 
-	while (bus) {
-		emit_bus(fil, bus);
-		bus = bus->next_bus;
-	}
+	fprintf(fil, "\t.dev = &%s,\n", bus->dev->name);
+	fprintf(fil, "\t.children = &%s,\n", bus->children->name);
 
-	fprintf(fil, "\t};\n");
+	fprintf(fil, "};\n");
 }
 
 static struct chip_instance *get_chip_instance(const struct device *dev)
@@ -1268,8 +1229,7 @@
 	else
 		fprintf(fil, "\t.ops = NULL,\n");
 	fprintf(fil, "#endif\n");
-	fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->parent->dev->name,
-		ptr->parent->id);
+	fprintf(fil, "\t.bus = &%s_bus,\n", ptr->parent->dev->name);
 	fprintf(fil, "\t.path = {");
 	fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
 	fprintf(fil, "},\n");
@@ -1290,7 +1250,7 @@
 			ptr->name);
 	}
 	if (has_children)
-		fprintf(fil, "\t.link_list = &%s_links[0],\n",
+		fprintf(fil, "\t.link_list = &%s_bus,\n",
 			ptr->name);
 	else
 		fprintf(fil, "\t.link_list = NULL,\n");
@@ -1319,7 +1279,7 @@
 	emit_resources(fil, ptr);
 
 	if (has_children)
-		emit_dev_links(fil, ptr);
+		emit_dev_bus(fil, ptr);
 }
 
 static void expose_device_names(FILE *fil, FILE *head, struct device *ptr, struct device *next)
@@ -1370,11 +1330,8 @@
 {
 	struct bus *bus = d->bus;
 
-	while (bus) {
-		if (bus->children)
-			add_siblings_to_queue(bfs_q_head, bus->children);
-		bus = bus->next_bus;
-	}
+	if (dev_has_children(d))
+		add_siblings_to_queue(bfs_q_head, bus->children);
 }
 
 static void walk_device_tree(FILE *fil, FILE *head, struct device *ptr,
@@ -1724,12 +1681,9 @@
  * +-----------------------------------------------------------------+
  * |                    |                                            |
  * | bus                | Recursively call override_devicetree on    |
- * | last_bus           | each bus of override device. It is assumed |
+ * |                    | each bus of override device. It is assumed |
  * |                    | that bus with id X under base device       |
- * |                    | to bus with id X under override device. If |
- * |                    | override device has more buses than base   |
- * |                    | device, then new buses are allocated under |
- * |                    | base device.                               |
+ * |                    | to bus with id X under override device.    |
  * |                    |                                            |
  * +-----------------------------------------------------------------+
  */
@@ -1841,29 +1795,20 @@
 	/*
 	 * Now that the device properties are all copied over, look at each bus
 	 * of the override device and run override_devicetree in a recursive
-	 * manner. The assumption here is that first bus of override device
-	 * corresponds to first bus of base device and so on. If base device has
-	 * lesser buses than override tree, then new buses are allocated for it.
+	 * manner. If base device has no bus but the override tree has, then a new
+	 * bus is allocated for it.
 	 */
 	struct bus *override_bus = override_dev->bus;
 	struct bus *base_bus = base_dev->bus;
 
-	while (override_bus) {
+	/*
+	 * If we have more buses in override tree device, then allocate
+	 * a new bus for the base tree device as well.
+	 */
+	if (!base_bus)
+		alloc_bus(base_dev);
 
-		/*
-		 * If we have more buses in override tree device, then allocate
-		 * a new bus for the base tree device as well.
-		 */
-		if (!base_bus) {
-			alloc_bus(base_dev);
-			base_bus = base_dev->last_bus;
-		}
-
-		override_devicetree(base_dev->bus, override_dev->bus);
-
-		override_bus = override_bus->next_bus;
-		base_bus = base_bus->next_bus;
-	}
+	override_devicetree(base_dev->bus, override_dev->bus);
 }
 
 /*
diff --git a/util/sconfig/sconfig.h b/util/sconfig/sconfig.h
index cb8f7b3..bb9a98f 100644
--- a/util/sconfig/sconfig.h
+++ b/util/sconfig/sconfig.h
@@ -106,17 +106,11 @@
 
 struct device;
 struct bus {
-	/* Instance/ID of the bus under the device. */
-	int id;
-
 	/* Pointer to device to which this bus belongs. */
 	struct device *dev;
 
 	/* Pointer to list of children. */
 	struct device *children;
-
-	/* Pointer to next bus for the device. */
-	struct bus *next_bus;
 };
 
 struct device {
@@ -157,10 +151,8 @@
 	/* Pointer to chip instance for this device. */
 	struct chip_instance *chip_instance;
 
-	/* Pointer to list of buses under this device. */
+	/* Pointer to the bus under this device. */
 	struct bus *bus;
-	/* Pointer to last bus under this device. */
-	struct bus *last_bus;
 
 	/* Global identifier of the ops for this device. */
 	char *ops_id;
diff --git a/util/sconfig/sconfig.tab.c_shipped b/util/sconfig/sconfig.tab.c_shipped
index cb60fb5..5b10922 100644
--- a/util/sconfig/sconfig.tab.c_shipped
+++ b/util/sconfig/sconfig.tab.c_shipped
@@ -1238,7 +1238,7 @@
   case 26: /* @2: %empty  */
                                                        {
 	(yyval.dev) = new_device_raw(cur_parent, cur_chip_instance, (yyvsp[-3].number), (yyvsp[-2].string), (yyvsp[-1].string), (yyvsp[0].number));
-	cur_parent = (yyval.dev)->last_bus;
+	cur_parent = (yyval.dev)->bus;
 }
     break;
 
@@ -1251,7 +1251,7 @@
   case 28: /* @3: %empty  */
                                        {
 	(yyval.dev) = new_device_reference(cur_parent, cur_chip_instance, (yyvsp[-1].string), (yyvsp[0].number));
-	cur_parent = (yyval.dev)->last_bus;
+	cur_parent = (yyval.dev)->bus;
 }
     break;
 
diff --git a/util/sconfig/sconfig.y b/util/sconfig/sconfig.y
index 17ab9a5..8ec103c 100644
--- a/util/sconfig/sconfig.y
+++ b/util/sconfig/sconfig.y
@@ -44,7 +44,7 @@
 
 device: DEVICE BUS NUMBER /* == devnum */ alias status {
 	$<dev>$ = new_device_raw(cur_parent, cur_chip_instance, $<number>2, $<string>3, $<string>4, $<number>5);
-	cur_parent = $<dev>$->last_bus;
+	cur_parent = $<dev>$->bus;
 }
 	devicechildren END {
 	cur_parent = $<dev>6->parent;
@@ -52,7 +52,7 @@
 
 device: DEVICE REFERENCE STRING status {
 	$<dev>$ = new_device_reference(cur_parent, cur_chip_instance, $<string>3, $<number>4);
-	cur_parent = $<dev>$->last_bus;
+	cur_parent = $<dev>$->bus;
 }
 	devicechildren END {
 	cur_parent = $<dev>5->parent;