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