aboutsummaryrefslogtreecommitdiff
path: root/src/device/device.c
diff options
context:
space:
mode:
authorArthur Heymans <arthur@aheymans.xyz>2024-01-25 22:26:07 +0100
committerArthur Heymans <arthur@aheymans.xyz>2024-01-29 15:04:18 +0000
commit3e99ba02a40cb1035c9ed22552cfc22aaf6bda7d (patch)
treeb2a03e9da5ac03153aedc8bb43586739d53be6af /src/device/device.c
parente4e26560ee70d4215cbfb5f24cbbc9283177c4d9 (diff)
device: Add a helper function to add a downstream bus
Adding downstream busses at runtime is a common pattern so add a helper function. Signed-off-by: Arthur Heymans <arthur@aheymans.xyz> Change-Id: Ic898189b92997b93304fcbf47c73e2bb5ec09023 Reviewed-on: https://review.coreboot.org/c/coreboot/+/80210 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de> Reviewed-by: Felix Held <felix-coreboot@felixheld.de>
Diffstat (limited to 'src/device/device.c')
-rw-r--r--src/device/device.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/device/device.c b/src/device/device.c
index 811b70937c..d877e14eab 100644
--- a/src/device/device.c
+++ b/src/device/device.c
@@ -125,6 +125,41 @@ struct device *alloc_dev(struct bus *parent, struct device_path *path)
return dev;
}
+DECLARE_SPIN_LOCK(bus_lock)
+
+/**
+ * Allocate a new bus structure
+ *
+ * Allocate a new downstream bus structure below a device and attach it
+ * to the device tree if the device doesn't already have a downstream bus.
+ *
+ * @param parent Parent device the to be created bus should be attached to.
+ * @return Pointer to the newly created bus structure or the existing bus.
+ *
+ */
+static struct bus *__alloc_bus(struct device *parent)
+{
+ if (parent->link_list)
+ return parent->link_list;
+
+ struct bus *bus = calloc(1, sizeof(struct bus));
+ if (!bus)
+ die("Couldn't allocate downstream bus!\n");
+ parent->link_list = bus;
+ bus->dev = parent;
+
+ return bus;
+}
+
+struct bus *alloc_bus(struct device *parent)
+{
+ struct bus *bus;
+ spin_lock(&bus_lock);
+ bus = __alloc_bus(parent);
+ spin_unlock(&bus_lock);
+ return bus;
+}
+
/**
* See if a device structure already exists and if not allocate it.
*