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 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <sys/types.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 11 | struct resource; |
| 12 | struct resource { |
| 13 | int type; |
| 14 | int index; |
| 15 | int base; |
| 16 | struct resource *next; |
| 17 | }; |
| 18 | |
| 19 | struct reg; |
| 20 | struct reg { |
| 21 | char *key; |
| 22 | char *value; |
| 23 | struct reg *next; |
| 24 | }; |
| 25 | |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 26 | struct pci_irq_info { |
| 27 | int ioapic_irq_pin; |
| 28 | int ioapic_dst_id; |
| 29 | }; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 30 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 31 | struct chip; |
| 32 | struct chip_instance { |
Furquan Shaikh | 4ebe953 | 2020-05-02 15:34:42 -0700 | [diff] [blame] | 33 | /* Monotonically increasing ID for each chip instance. */ |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 34 | int id; |
| 35 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 36 | /* Pointer to registers for this chip. */ |
| 37 | struct reg *reg; |
| 38 | |
| 39 | /* Pointer to chip of which this is instance. */ |
| 40 | struct chip *chip; |
| 41 | |
| 42 | /* Pointer to next instance of the same chip. */ |
| 43 | struct chip_instance *next; |
Furquan Shaikh | 27efc50 | 2018-06-22 09:19:15 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 46 | * Pointer to corresponding chip instance in base devicetree. |
| 47 | * a) If the chip instance belongs to the base devicetree, then this pointer is set to |
| 48 | * NULL. |
| 49 | * b) If the chip instance belongs to override tree, then this pointer is set to its |
| 50 | * corresponding chip instance in base devicetree (if it exists), else to NULL. |
| 51 | * |
| 52 | * This is useful when generating chip instances and chip_ops for a device to determine |
| 53 | * 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] | 54 | */ |
Furquan Shaikh | bbade24 | 2020-05-02 16:05:29 -0700 | [diff] [blame] | 55 | struct chip_instance *base_chip_instance; |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct chip { |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 59 | /* Indicates if chip header exists for this chip. */ |
| 60 | int chiph_exists; |
| 61 | |
| 62 | /* Name of current chip. */ |
| 63 | char *name; |
| 64 | |
| 65 | /* Name of current chip normalized to _. */ |
| 66 | char *name_underscore; |
| 67 | |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 68 | /* Pointer to first instance of this chip. */ |
| 69 | struct chip_instance *instance; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 70 | |
| 71 | /* Pointer to next chip. */ |
| 72 | struct chip *next; |
| 73 | }; |
| 74 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 75 | struct device; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 76 | struct bus { |
| 77 | /* Instance/ID of the bus under the device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 78 | int id; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 79 | |
| 80 | /* Pointer to device to which this bus belongs. */ |
| 81 | struct device *dev; |
| 82 | |
| 83 | /* Pointer to list of children. */ |
| 84 | struct device *children; |
| 85 | |
| 86 | /* Pointer to next bus for the device. */ |
| 87 | struct bus *next_bus; |
| 88 | }; |
| 89 | |
| 90 | struct device { |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 91 | /* Indicates device status (enabled / hidden or not). */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 92 | int enabled; |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 93 | int hidden; |
Ronald G. Minnich | 466ca2c | 2019-10-22 02:02:24 +0000 | [diff] [blame] | 94 | /* non-zero if the device should be included in all cases */ |
| 95 | int mandatory; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 96 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 97 | /* Subsystem IDs for the device. */ |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 98 | int subsystem_vendor; |
| 99 | int subsystem_device; |
| 100 | int inherit_subsystem; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 101 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 102 | /* Name of this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 103 | char *name; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 104 | |
| 105 | /* Path of this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 106 | char *path; |
| 107 | int path_a; |
| 108 | int path_b; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 109 | |
| 110 | /* Type of bus that exists under this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 111 | int bustype; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 112 | |
| 113 | /* PCI IRQ info. */ |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 114 | struct pci_irq_info pci_irq_info[4]; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 115 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 116 | /* Pointer to bus of parent on which this device resides. */ |
| 117 | struct bus *parent; |
| 118 | |
| 119 | /* Pointer to next child under the same parent. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 120 | struct device *sibling; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 121 | |
| 122 | /* Pointer to resources for this device. */ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 123 | struct resource *res; |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 124 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 125 | /* Pointer to chip instance for this device. */ |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 126 | struct chip_instance *chip_instance; |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 127 | |
| 128 | /* Pointer to list of buses under this device. */ |
| 129 | struct bus *bus; |
| 130 | /* Pointer to last bus under this device. */ |
| 131 | struct bus *last_bus; |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 132 | |
| 133 | /* SMBIOS slot type */ |
| 134 | char *smbios_slot_type; |
| 135 | |
| 136 | /* SMBIOS slot data width */ |
| 137 | char *smbios_slot_data_width; |
| 138 | |
| 139 | /* SMBIOS slot description for reference designation */ |
| 140 | char *smbios_slot_designation; |
| 141 | |
| 142 | /* SMBIOS slot length */ |
| 143 | char *smbios_slot_length; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 146 | extern struct bus *root_parent; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 147 | |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 148 | struct device *new_device(struct bus *parent, |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 149 | struct chip_instance *chip_instance, |
Furquan Shaikh | a9b6429 | 2018-05-31 07:52:00 -0700 | [diff] [blame] | 150 | const int bustype, const char *devnum, |
Hung-Te Lin | 936dbe1 | 2018-09-10 10:51:26 +0800 | [diff] [blame] | 151 | int status); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 152 | |
| 153 | void add_resource(struct bus *bus, int type, int index, int base); |
| 154 | |
| 155 | void add_pci_subsystem_ids(struct bus *bus, int vendor, int device, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 156 | int inherit); |
Furquan Shaikh | 9319826 | 2018-06-03 04:22:17 -0700 | [diff] [blame] | 157 | |
| 158 | void add_ioapic_info(struct bus *bus, int apicid, const char *_srcpin, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 159 | int irqpin); |
Stefan Reinauer | 2e78aa5 | 2016-05-07 01:11:14 -0700 | [diff] [blame] | 160 | |
Patrick Rudolph | ac24d3c | 2019-04-12 14:42:17 +0200 | [diff] [blame] | 161 | void add_slot_desc(struct bus *bus, char *type, char *length, char *designation, |
| 162 | char *data_width); |
| 163 | |
Stefan Reinauer | 2e78aa5 | 2016-05-07 01:11:14 -0700 | [diff] [blame] | 164 | void yyrestart(FILE *input_file); |
Furquan Shaikh | 79e8412 | 2018-05-30 15:09:09 -0700 | [diff] [blame] | 165 | |
| 166 | /* Add chip data to tail of queue. */ |
| 167 | void chip_enqueue_tail(void *data); |
| 168 | |
| 169 | /* Retrieve chip data from tail of queue. */ |
| 170 | void *chip_dequeue_tail(void); |
Furquan Shaikh | c56ae2f | 2018-05-31 10:33:16 -0700 | [diff] [blame] | 171 | |
| 172 | struct chip_instance *new_chip_instance(char *path); |
| 173 | void add_register(struct chip_instance *chip, char *name, char *val); |