From c0fc38eed8f407d71f714f4d6fe2af0c3501ece4 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Sat, 6 Aug 2022 19:02:59 +0200 Subject: sconfig: Allow to specify device operations Currently we only have runtime mechanisms to assign device operations to a node in our devicetree (with one exception: the root device). The most common method is to map PCI IDs to the device operations with a `struct pci_driver`. Another accustomed way is to let a chip driver assign them. For very common drivers, e.g. those in soc/intel/common/blocks/, the PCI ID lists grew very large and are incredibly error-prone. Often, IDs are missing and sometimes IDs are added almost mechanically without checking the code for compatibility. Maintaining these lists in a central place also reduces flexibility. Now, for onboard devices it is actually unnecessary to assign the device operations at runtime. We already know exactly what operations should be assigned. And since we are using chipset devicetrees, we have a perfect place to put that information. This patch adds a simple mechanism to `sconfig`. It allows us to speci- fy operations per device, e.g. device pci 00.0 alias system_agent on ops system_agent_ops end The operations are given as a C identifier. In this example, we simply assume that a global `struct device_operations system_agent_ops` exists. Change-Id: I2833d2f2450fde3206c33393f58b86fd4280b566 Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/c/coreboot/+/66483 Tested-by: build bot (Jenkins) Reviewed-by: Tim Wawrzynczak Reviewed-by: Arthur Heymans Reviewed-by: Felix Held --- util/sconfig/main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'util/sconfig/main.c') diff --git a/util/sconfig/main.c b/util/sconfig/main.c index 6648254d0a..0833d388e5 100644 --- a/util/sconfig/main.c +++ b/util/sconfig/main.c @@ -148,6 +148,9 @@ struct queue_entry { struct queue_entry *prev; }; +/* Global list of all `struct device_operations` identifiers to declare. */ +static struct identifier *device_operations; + #define S_ALLOC(_s) s_alloc(__func__, _s) static void *s_alloc(const char *f, size_t s) @@ -696,6 +699,30 @@ static int emit_fw_config_probe(FILE *fil, struct device *dev) return 0; } +/* Enqueue identifier to list with head `*it`, if not already present. */ +void add_identifier(struct identifier **it, const char *id) +{ + for (; *it != NULL; it = &(*it)->next) { + if (!strcmp((*it)->id, id)) + return; + } + + *it = S_ALLOC(sizeof(**it)); + (*it)->id = id; +} + +void add_device_ops(struct bus *bus, char *ops_id) +{ + if (bus->dev->ops_id) { + printf("ERROR: Device operations may only be specified once,\n" + " found '%s', '%s'.\n", bus->dev->ops_id, ops_id); + exit(1); + } + + add_identifier(&device_operations, ops_id); + 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 @@ -1262,10 +1289,13 @@ static void pass1(FILE *fil, FILE *head, struct device *ptr, struct device *next fprintf(fil, "#if !DEVTREE_EARLY\n"); /* - * ops field is set to default_dev_ops_root only for the root - * device. For all other devices, it is set by the driver at runtime. + * ops field can be set in the devicetree. If unspecified, it is set + * to default_dev_ops_root only for the root device, other devices + * get it set by the driver at runtime. */ - if (ptr == &base_root_dev) + if (ptr->ops_id) + fprintf(fil, "\t.ops = &%s,\n", ptr->ops_id); + else if (ptr == &base_root_dev) fprintf(fil, "\t.ops = &default_dev_ops_root,\n"); else fprintf(fil, "\t.ops = NULL,\n"); @@ -1477,6 +1507,12 @@ static void emit_chip_configs(FILE *fil) } } +static void emit_identifiers(FILE *fil, const char *decl, const struct identifier *it) +{ + for (; it != NULL; it = it->next) + fprintf(fil, "extern %s %s;\n", decl, it->id); +} + static void inherit_subsystem_ids(FILE *file, FILE *head, struct device *dev, struct device *next) { @@ -1854,6 +1890,10 @@ static void update_device(struct device *base_dev, struct device *override_dev) */ override_dev->chip_instance->base_chip_instance = get_chip_instance(base_dev); + /* Allow to override the ops of a device */ + if (override_dev->ops_id) + base_dev->ops_id = override_dev->ops_id; + /* * Now that the device properties are all copied over, look at each bus * of the override device and run override_devicetree in a recursive @@ -1970,6 +2010,7 @@ static void generate_outputc(FILE *f, const char *static_header) fprintf(f, "#include \n"); fprintf(f, "#include <%s>\n", static_header); emit_chip_headers(f, chip_header.next); + emit_identifiers(f, "struct device_operations", device_operations); fprintf(f, "\n#define STORAGE static __maybe_unused DEVTREE_CONST\n\n"); walk_device_tree(NULL, NULL, &base_root_dev, inherit_subsystem_ids); -- cgit v1.2.3