Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* sconfig, coreboot device tree compiler */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 3 | |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 4 | #include <stdint.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/types.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 12 | struct resource; |
| 13 | struct resource { |
| 14 | int type; |
| 15 | int index; |
| 16 | int base; |
| 17 | struct resource *next; |
| 18 | }; |
| 19 | |
| 20 | struct reg; |
| 21 | struct reg { |
| 22 | char *key; |
| 23 | char *value; |
| 24 | struct reg *next; |
| 25 | }; |
| 26 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 27 | struct fw_config_option; |
| 28 | struct fw_config_option { |
| 29 | const char *name; |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 30 | uint64_t value; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 31 | struct fw_config_option *next; |
| 32 | }; |
Tim Wawrzynczak | 13e240c | 2021-04-28 14:03:07 -0600 | [diff] [blame] | 33 | |
| 34 | struct fw_config_field_bits; |
| 35 | struct fw_config_field_bits { |
| 36 | unsigned int start_bit; |
| 37 | unsigned int end_bit; |
| 38 | struct fw_config_field_bits *next; |
| 39 | }; |
| 40 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 41 | struct fw_config_field; |
| 42 | struct fw_config_field { |
| 43 | const char *name; |
Tim Wawrzynczak | 13e240c | 2021-04-28 14:03:07 -0600 | [diff] [blame] | 44 | struct fw_config_field_bits *bits; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 45 | struct fw_config_field *next; |
| 46 | struct fw_config_option *options; |
| 47 | }; |
| 48 | struct fw_config_probe; |
| 49 | struct fw_config_probe { |
| 50 | const char *field; |
| 51 | const char *option; |
| 52 | struct fw_config_probe *next; |
| 53 | }; |
| 54 | |
Nico Huber | c0fc38e | 2022-08-06 19:02:59 +0200 | [diff] [blame] | 55 | struct identifier { |
| 56 | const char *id; |
| 57 | struct identifier *next; |
| 58 | }; |
| 59 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 60 | struct chip; |
| 61 | struct chip_instance { |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 62 | /* Monotonically increasing ID for each chip instance. */ |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 63 | int id; |
| 64 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 65 | /* Pointer to registers for this chip. */ |
| 66 | struct reg *reg; |
| 67 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 68 | /* Pointer to references for this chip. */ |
| 69 | struct reg *ref; |
| 70 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 71 | /* Pointer to chip of which this is instance. */ |
| 72 | struct chip *chip; |
| 73 | |
| 74 | /* Pointer to next instance of the same chip. */ |
| 75 | struct chip_instance *next; |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 78 | * Pointer to corresponding chip instance in base devicetree. |
| 79 | * a) If the chip instance belongs to the base devicetree, then this pointer is set to |
| 80 | * NULL. |
| 81 | * b) If the chip instance belongs to override tree, then this pointer is set to its |
| 82 | * corresponding chip instance in base devicetree (if it exists), else to NULL. |
| 83 | * |
| 84 | * This is useful when generating chip instances and chip_ops for a device to determine |
| 85 | * if this is the instance to emit or if there is a base chip instance to use instead. |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 86 | */ |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 87 | struct chip_instance *base_chip_instance; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | struct chip { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 91 | /* Indicates if chip header exists for this chip. */ |
| 92 | int chiph_exists; |
| 93 | |
| 94 | /* Name of current chip. */ |
| 95 | char *name; |
| 96 | |
| 97 | /* Name of current chip normalized to _. */ |
| 98 | char *name_underscore; |
| 99 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 100 | /* Pointer to first instance of this chip. */ |
| 101 | struct chip_instance *instance; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 102 | |
| 103 | /* Pointer to next chip. */ |
| 104 | struct chip *next; |
| 105 | }; |
| 106 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 107 | struct device; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 108 | struct bus { |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 109 | /* Pointer to device to which this bus belongs. */ |
| 110 | struct device *dev; |
| 111 | |
| 112 | /* Pointer to list of children. */ |
| 113 | struct device *children; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct device { |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 117 | /* Indicates device status (enabled / hidden or not). */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 118 | int enabled; |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 119 | int hidden; |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 120 | /* non-zero if the device should be included in all cases */ |
| 121 | int mandatory; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 122 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 123 | /* Subsystem IDs for the device. */ |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 124 | int subsystem_vendor; |
| 125 | int subsystem_device; |
| 126 | int inherit_subsystem; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 127 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 128 | /* Name of this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 129 | char *name; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 130 | |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 131 | /* Alias of this device (for internal references) */ |
| 132 | char *alias; |
| 133 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 134 | /* Path of this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 135 | char *path; |
| 136 | int path_a; |
| 137 | int path_b; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 138 | |
| 139 | /* Type of bus that exists under this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 140 | int bustype; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 141 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 142 | /* Pointer to bus of parent on which this device resides. */ |
| 143 | struct bus *parent; |
| 144 | |
| 145 | /* Pointer to next child under the same parent. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 146 | struct device *sibling; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 147 | |
| 148 | /* Pointer to resources for this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 149 | struct resource *res; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 150 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 151 | /* Pointer to chip instance for this device. */ |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 152 | struct chip_instance *chip_instance; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 153 | |
Arthur Heymans | 80c79a5 | 2023-08-24 15:12:19 +0200 | [diff] [blame] | 154 | /* Pointer to the bus under this device. */ |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 155 | struct bus *bus; |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 156 | |
Nico Huber | c0fc38e | 2022-08-06 19:02:59 +0200 | [diff] [blame] | 157 | /* Global identifier of the ops for this device. */ |
| 158 | char *ops_id; |
| 159 | |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 160 | /* SMBIOS slot type */ |
| 161 | char *smbios_slot_type; |
| 162 | |
| 163 | /* SMBIOS slot data width */ |
| 164 | char *smbios_slot_data_width; |
| 165 | |
| 166 | /* SMBIOS slot description for reference designation */ |
| 167 | char *smbios_slot_designation; |
| 168 | |
| 169 | /* SMBIOS slot length */ |
| 170 | char *smbios_slot_length; |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 171 | |
Angel Pons | 437da71 | 2021-09-03 16:51:40 +0200 | [diff] [blame] | 172 | /* SMBIOS type41 fields */ |
| 173 | int smbios_instance_id_valid; |
| 174 | unsigned int smbios_instance_id; |
| 175 | const char *smbios_refdes; |
| 176 | |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 177 | /* List of field+option to probe. */ |
| 178 | struct fw_config_probe *probe; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 181 | extern struct bus *root_parent; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 182 | |
Duncan Laurie | e335c2e | 2020-07-29 16:28:43 -0700 | [diff] [blame] | 183 | struct device *new_device_raw(struct bus *parent, |
| 184 | struct chip_instance *chip_instance, |
| 185 | const int bustype, const char *devnum, |
| 186 | char *alias, int status); |
| 187 | |
| 188 | struct device *new_device_reference(struct bus *parent, |
| 189 | struct chip_instance *chip_instance, |
| 190 | const char *reference, int status); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 191 | |
| 192 | void add_resource(struct bus *bus, int type, int index, int base); |
| 193 | |
| 194 | void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 195 | int inherit); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 196 | |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 197 | void add_slot_desc(struct bus *bus, char *type, char *length, char *designation, |
| 198 | char *data_width); |
| 199 | |
Angel Pons | 437da71 | 2021-09-03 16:51:40 +0200 | [diff] [blame] | 200 | void add_smbios_dev_info(struct bus *bus, long instance_id, const char *refdes); |
| 201 | |
Stefan Reinauer | 2e78aa5 | 2016-05-07 01:11:14 -0700 | [diff] [blame] | 202 | void yyrestart(FILE *input_file); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 203 | |
| 204 | /* Add chip data to tail of queue. */ |
| 205 | void chip_enqueue_tail(void *data); |
| 206 | |
| 207 | /* Retrieve chip data from tail of queue. */ |
| 208 | void *chip_dequeue_tail(void); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 209 | |
| 210 | struct chip_instance *new_chip_instance(char *path); |
| 211 | void add_register(struct chip_instance *chip, char *name, char *val); |
Nico Huber | 8e1ea52 | 2020-06-03 10:20:07 -0700 | [diff] [blame] | 212 | void add_reference(struct chip_instance *chip, char *name, char *alias); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 213 | |
| 214 | struct fw_config_field *get_fw_config_field(const char *name); |
| 215 | |
Tim Wawrzynczak | 13e240c | 2021-04-28 14:03:07 -0600 | [diff] [blame] | 216 | void add_fw_config_field_bits(struct fw_config_field *field, |
| 217 | unsigned int start_bit, unsigned int end_bit); |
| 218 | |
| 219 | struct fw_config_field *new_fw_config_field(const char *name, struct fw_config_field_bits *bits); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 220 | |
| 221 | void add_fw_config_option(struct fw_config_field *field, const char *name, |
Tim Wawrzynczak | 24b4af6 | 2020-10-01 15:41:31 -0600 | [diff] [blame] | 222 | uint64_t value); |
Duncan Laurie | 47b7b34 | 2020-05-15 15:39:08 -0700 | [diff] [blame] | 223 | |
| 224 | void add_fw_config_probe(struct bus *bus, const char *field, const char *option); |
Tim Wawrzynczak | 13e240c | 2021-04-28 14:03:07 -0600 | [diff] [blame] | 225 | |
| 226 | void append_fw_config_bits(struct fw_config_field_bits **bits, |
| 227 | unsigned int start_bit, unsigned int end_bit); |
Nico Huber | c0fc38e | 2022-08-06 19:02:59 +0200 | [diff] [blame] | 228 | |
| 229 | void add_device_ops(struct bus *, char *ops_id); |