xref: /openbmc/qemu/include/hw/nvram/fw_cfg.h (revision 51b19950)
1 #ifndef FW_CFG_H
2 #define FW_CFG_H
3 
4 #include "exec/hwaddr.h"
5 #include "standard-headers/linux/qemu_fw_cfg.h"
6 #include "hw/sysbus.h"
7 #include "sysemu/dma.h"
8 
9 #define TYPE_FW_CFG     "fw_cfg"
10 #define TYPE_FW_CFG_IO  "fw_cfg_io"
11 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
12 
13 #define FW_CFG(obj)     OBJECT_CHECK(FWCfgState,    (obj), TYPE_FW_CFG)
14 #define FW_CFG_IO(obj)  OBJECT_CHECK(FWCfgIoState,  (obj), TYPE_FW_CFG_IO)
15 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
16 
17 typedef struct fw_cfg_file FWCfgFile;
18 
19 #define FW_CFG_ORDER_OVERRIDE_VGA    70
20 #define FW_CFG_ORDER_OVERRIDE_NIC    80
21 #define FW_CFG_ORDER_OVERRIDE_USER   100
22 #define FW_CFG_ORDER_OVERRIDE_DEVICE 110
23 
24 void fw_cfg_set_order_override(FWCfgState *fw_cfg, int order);
25 void fw_cfg_reset_order_override(FWCfgState *fw_cfg);
26 
27 typedef struct FWCfgFiles {
28     uint32_t  count;
29     FWCfgFile f[];
30 } FWCfgFiles;
31 
32 typedef struct fw_cfg_dma_access FWCfgDmaAccess;
33 
34 typedef void (*FWCfgCallback)(void *opaque);
35 typedef void (*FWCfgWriteCallback)(void *opaque, off_t start, size_t len);
36 
37 struct FWCfgState {
38     /*< private >*/
39     SysBusDevice parent_obj;
40     /*< public >*/
41 
42     uint16_t file_slots;
43     FWCfgEntry *entries[2];
44     int *entry_order;
45     FWCfgFiles *files;
46     uint16_t cur_entry;
47     uint32_t cur_offset;
48     Notifier machine_ready;
49 
50     int fw_cfg_order_override;
51 
52     bool dma_enabled;
53     dma_addr_t dma_addr;
54     AddressSpace *dma_as;
55     MemoryRegion dma_iomem;
56 
57     /* restore during migration */
58     bool acpi_mr_restore;
59     uint64_t table_mr_size;
60     uint64_t linker_mr_size;
61     uint64_t rsdp_mr_size;
62 };
63 
64 struct FWCfgIoState {
65     /*< private >*/
66     FWCfgState parent_obj;
67     /*< public >*/
68 
69     MemoryRegion comb_iomem;
70 };
71 
72 struct FWCfgMemState {
73     /*< private >*/
74     FWCfgState parent_obj;
75     /*< public >*/
76 
77     MemoryRegion ctl_iomem, data_iomem;
78     uint32_t data_width;
79     MemoryRegionOps wide_data_ops;
80 };
81 
82 /**
83  * fw_cfg_add_bytes:
84  * @s: fw_cfg device being modified
85  * @key: selector key value for new fw_cfg item
86  * @data: pointer to start of item data
87  * @len: size of item data
88  *
89  * Add a new fw_cfg item, available by selecting the given key, as a raw
90  * "blob" of the given size. The data referenced by the starting pointer
91  * is only linked, NOT copied, into the data structure of the fw_cfg device.
92  */
93 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
94 
95 /**
96  * fw_cfg_add_string:
97  * @s: fw_cfg device being modified
98  * @key: selector key value for new fw_cfg item
99  * @value: NUL-terminated ascii string
100  *
101  * Add a new fw_cfg item, available by selecting the given key. The item
102  * data will consist of a dynamically allocated copy of the provided string,
103  * including its NUL terminator.
104  */
105 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
106 
107 /**
108  * fw_cfg_modify_string:
109  * @s: fw_cfg device being modified
110  * @key: selector key value for new fw_cfg item
111  * @value: NUL-terminated ascii string
112  *
113  * Replace the fw_cfg item available by selecting the given key. The new
114  * data will consist of a dynamically allocated copy of the provided string,
115  * including its NUL terminator. The data being replaced, assumed to have
116  * been dynamically allocated during an earlier call to either
117  * fw_cfg_add_string() or fw_cfg_modify_string(), is freed before returning.
118  */
119 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value);
120 
121 /**
122  * fw_cfg_add_i16:
123  * @s: fw_cfg device being modified
124  * @key: selector key value for new fw_cfg item
125  * @value: 16-bit integer
126  *
127  * Add a new fw_cfg item, available by selecting the given key. The item
128  * data will consist of a dynamically allocated copy of the given 16-bit
129  * value, converted to little-endian representation.
130  */
131 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
132 
133 /**
134  * fw_cfg_modify_i16:
135  * @s: fw_cfg device being modified
136  * @key: selector key value for new fw_cfg item
137  * @value: 16-bit integer
138  *
139  * Replace the fw_cfg item available by selecting the given key. The new
140  * data will consist of a dynamically allocated copy of the given 16-bit
141  * value, converted to little-endian representation. The data being replaced,
142  * assumed to have been dynamically allocated during an earlier call to
143  * either fw_cfg_add_i16() or fw_cfg_modify_i16(), is freed before returning.
144  */
145 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
146 
147 /**
148  * fw_cfg_add_i32:
149  * @s: fw_cfg device being modified
150  * @key: selector key value for new fw_cfg item
151  * @value: 32-bit integer
152  *
153  * Add a new fw_cfg item, available by selecting the given key. The item
154  * data will consist of a dynamically allocated copy of the given 32-bit
155  * value, converted to little-endian representation.
156  */
157 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
158 
159 /**
160  * fw_cfg_modify_i32:
161  * @s: fw_cfg device being modified
162  * @key: selector key value for new fw_cfg item
163  * @value: 32-bit integer
164  *
165  * Replace the fw_cfg item available by selecting the given key. The new
166  * data will consist of a dynamically allocated copy of the given 32-bit
167  * value, converted to little-endian representation. The data being replaced,
168  * assumed to have been dynamically allocated during an earlier call to
169  * either fw_cfg_add_i32() or fw_cfg_modify_i32(), is freed before returning.
170  */
171 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value);
172 
173 /**
174  * fw_cfg_add_i64:
175  * @s: fw_cfg device being modified
176  * @key: selector key value for new fw_cfg item
177  * @value: 64-bit integer
178  *
179  * Add a new fw_cfg item, available by selecting the given key. The item
180  * data will consist of a dynamically allocated copy of the given 64-bit
181  * value, converted to little-endian representation.
182  */
183 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
184 
185 /**
186  * fw_cfg_modify_i64:
187  * @s: fw_cfg device being modified
188  * @key: selector key value for new fw_cfg item
189  * @value: 64-bit integer
190  *
191  * Replace the fw_cfg item available by selecting the given key. The new
192  * data will consist of a dynamically allocated copy of the given 64-bit
193  * value, converted to little-endian representation. The data being replaced,
194  * assumed to have been dynamically allocated during an earlier call to
195  * either fw_cfg_add_i64() or fw_cfg_modify_i64(), is freed before returning.
196  */
197 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value);
198 
199 /**
200  * fw_cfg_add_file:
201  * @s: fw_cfg device being modified
202  * @filename: name of new fw_cfg file item
203  * @data: pointer to start of item data
204  * @len: size of item data
205  *
206  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
207  * referenced by the starting pointer is only linked, NOT copied, into the
208  * data structure of the fw_cfg device.
209  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
210  * will be used; also, a new entry will be added to the file directory
211  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
212  * data size, and assigned selector key value.
213  */
214 void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
215                      size_t len);
216 
217 /**
218  * fw_cfg_add_file_callback:
219  * @s: fw_cfg device being modified
220  * @filename: name of new fw_cfg file item
221  * @select_cb: callback function when selecting
222  * @write_cb: callback function after a write
223  * @callback_opaque: argument to be passed into callback function
224  * @data: pointer to start of item data
225  * @len: size of item data
226  * @read_only: is file read only
227  *
228  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
229  * referenced by the starting pointer is only linked, NOT copied, into the
230  * data structure of the fw_cfg device.
231  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
232  * will be used; also, a new entry will be added to the file directory
233  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
234  * data size, and assigned selector key value.
235  * Additionally, set a callback function (and argument) to be called each
236  * time this item is selected (by having its selector key either written to
237  * the fw_cfg control register, or passed to QEMU in FWCfgDmaAccess.control
238  * with FW_CFG_DMA_CTL_SELECT).
239  */
240 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
241                               FWCfgCallback select_cb,
242                               FWCfgWriteCallback write_cb,
243                               void *callback_opaque,
244                               void *data, size_t len, bool read_only);
245 
246 /**
247  * fw_cfg_modify_file:
248  * @s: fw_cfg device being modified
249  * @filename: name of new fw_cfg file item
250  * @data: pointer to start of item data
251  * @len: size of item data
252  *
253  * Replace a NAMED fw_cfg item. If an existing item is found, its callback
254  * information will be cleared, and a pointer to its data will be returned
255  * to the caller, so that it may be freed if necessary. If an existing item
256  * is not found, this call defaults to fw_cfg_add_file(), and NULL is
257  * returned to the caller.
258  * In either case, the new item data is only linked, NOT copied, into the
259  * data structure of the fw_cfg device.
260  *
261  * Returns: pointer to old item's data, or NULL if old item does not exist.
262  */
263 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
264                          size_t len);
265 
266 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
267                                 AddressSpace *dma_as);
268 FWCfgState *fw_cfg_init_io(uint32_t iobase);
269 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr);
270 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
271                                  hwaddr data_addr, uint32_t data_width,
272                                  hwaddr dma_addr, AddressSpace *dma_as);
273 
274 FWCfgState *fw_cfg_find(void);
275 bool fw_cfg_dma_enabled(void *opaque);
276 
277 /**
278  * fw_cfg_arch_key_name:
279  *
280  * @key: The uint16 selector key.
281  *
282  * The key is architecture-specific (the FW_CFG_ARCH_LOCAL mask is expected
283  * to be set in the key).
284  *
285  * Returns: The stringified architecture-specific name if the selector
286  *          refers to a well-known numerically defined item, or NULL on
287  *          key lookup failure.
288  */
289 const char *fw_cfg_arch_key_name(uint16_t key);
290 
291 #endif
292