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