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>
This commit is contained in:
Arthur Heymans 2024-01-25 22:26:07 +01:00
parent e4e26560ee
commit 3e99ba02a4
4 changed files with 40 additions and 17 deletions

View File

@ -125,6 +125,41 @@ struct device *alloc_dev(struct bus *parent, struct device_path *path)
return dev; 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. * See if a device structure already exists and if not allocate it.
* *

View File

@ -171,6 +171,7 @@ extern struct bus *free_links;
/* Generic device interface functions */ /* Generic device interface functions */
struct device *alloc_dev(struct bus *parent, struct device_path *path); struct device *alloc_dev(struct bus *parent, struct device_path *path);
struct bus *alloc_bus(struct device *parent);
void dev_initialize_chips(void); void dev_initialize_chips(void);
void dev_enumerate(void); void dev_enumerate(void);
void dev_configure(void); void dev_configure(void);
@ -229,10 +230,9 @@ void mp_init_cpus(DEVTREE_CONST struct bus *cpu_bus);
static inline void mp_cpu_bus_init(struct device *dev) static inline void mp_cpu_bus_init(struct device *dev)
{ {
/* Make sure the cpu cluster has a downstream bus for LAPICs to be allocated. */ /* Make sure the cpu cluster has a downstream bus for LAPICs to be allocated. */
if (!dev->link_list) struct bus *bus = alloc_bus(dev);
add_more_links(dev, 1);
mp_init_cpus(dev->link_list); mp_init_cpus(bus);
} }
/* Debug functions */ /* Debug functions */

View File

@ -72,14 +72,7 @@ void iio_pci_domain_scan_bus(struct device *dev)
if (!sr) if (!sr)
return; return;
if (!dev->link_list) { struct bus *bus = alloc_bus(dev);
dev->link_list = calloc(1, sizeof(struct bus));
if (!dev->link_list)
die("%s: out of memory.\n", __func__);
}
struct bus *bus = dev->link_list;
bus->dev = dev;
bus->secondary = sr->BusBase; bus->secondary = sr->BusBase;
bus->subordinate = sr->BusBase; bus->subordinate = sr->BusBase;
bus->max_subordinate = sr->BusLimit; bus->max_subordinate = sr->BusLimit;

View File

@ -45,12 +45,7 @@ static void create_ioat_domain(const union xeon_domain_path dp, struct bus *cons
domain->ops = &ioat_domain_ops; domain->ops = &ioat_domain_ops;
domain->link_list = calloc(1, sizeof(struct bus)); struct bus *const bus = alloc_bus(domain);
if (!domain->link_list)
die("%s: out of memory.\n", __func__);
struct bus *const bus = domain->link_list;
bus->dev = domain;
bus->secondary = bus_base; bus->secondary = bus_base;
bus->subordinate = bus->secondary; bus->subordinate = bus->secondary;
bus->max_subordinate = bus_limit; bus->max_subordinate = bus_limit;