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