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