2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin Köhne <c.koehne@beckhoff.com>
9 #include <sys/endian.h>
11 #include <machine/vmm.h>
18 #include "acpi_device.h"
21 #include "qemu_fwcfg.h"
23 #define QEMU_FWCFG_ACPI_DEVICE_NAME "FWCF"
24 #define QEMU_FWCFG_ACPI_HARDWARE_ID "QEMU0002"
26 #define QEMU_FWCFG_SELECTOR_PORT_NUMBER 0x510
27 #define QEMU_FWCFG_SELECTOR_PORT_SIZE 1
28 #define QEMU_FWCFG_SELECTOR_PORT_FLAGS IOPORT_F_INOUT
29 #define QEMU_FWCFG_DATA_PORT_NUMBER 0x511
30 #define QEMU_FWCFG_DATA_PORT_SIZE 1
31 #define QEMU_FWCFG_DATA_PORT_FLAGS \
32 IOPORT_F_INOUT /* QEMU v2.4+ ignores writes */
34 #define QEMU_FWCFG_ARCHITECTURE_MASK 0x0001
35 #define QEMU_FWCFG_INDEX_MASK 0x3FFF
37 #define QEMU_FWCFG_SELECT_READ 0
38 #define QEMU_FWCFG_SELECT_WRITE 1
40 #define QEMU_FWCFG_ARCHITECTURE_GENERIC 0
41 #define QEMU_FWCFG_ARCHITECTURE_SPECIFIC 1
43 #define QEMU_FWCFG_INDEX_SIGNATURE 0x00
44 #define QEMU_FWCFG_INDEX_ID 0x01
45 #define QEMU_FWCFG_INDEX_FILE_DIR 0x19
47 #define QEMU_FWCFG_FIRST_FILE_INDEX 0x20
49 #define QEMU_FWCFG_MIN_FILES 10
53 union qemu_fwcfg_selector {
56 uint16_t writeable : 1;
57 uint16_t architecture : 1;
62 struct qemu_fwcfg_signature {
66 struct qemu_fwcfg_id {
67 uint32_t interface : 1; /* always set */
69 uint32_t reserved : 30;
72 struct qemu_fwcfg_file {
76 uint8_t name[QEMU_FWCFG_MAX_NAME];
79 struct qemu_fwcfg_directory {
81 struct qemu_fwcfg_file files[0];
86 struct qemu_fwcfg_softc {
87 struct acpi_device *acpi_dev;
90 union qemu_fwcfg_selector selector;
91 struct qemu_fwcfg_item items[QEMU_FWCFG_MAX_ARCHS]
92 [QEMU_FWCFG_MAX_ENTRIES];
93 struct qemu_fwcfg_directory *directory;
96 static struct qemu_fwcfg_softc fwcfg_sc;
99 qemu_fwcfg_selector_port_handler(struct vmctx *const ctx __unused, const int in,
100 const int port __unused, const int bytes, uint32_t *const eax,
101 void *const arg __unused)
103 if (bytes != sizeof(uint16_t)) {
104 warnx("%s: invalid size (%d) of IO port access", __func__,
110 *eax = htole16(fwcfg_sc.selector.bits);
114 fwcfg_sc.data_offset = 0;
115 fwcfg_sc.selector.bits = le16toh(*eax);
121 qemu_fwcfg_data_port_handler(struct vmctx *const ctx __unused, const int in,
122 const int port __unused, const int bytes, uint32_t *const eax,
123 void *const arg __unused)
125 if (bytes != sizeof(uint8_t)) {
126 warnx("%s: invalid size (%d) of IO port access", __func__,
132 warnx("%s: Writes to qemu fwcfg data port aren't allowed",
138 struct qemu_fwcfg_item *const item =
139 &fwcfg_sc.items[fwcfg_sc.selector.architecture]
140 [fwcfg_sc.selector.index];
141 if (item->data == NULL) {
143 "%s: qemu fwcfg item doesn't exist (architecture %s index 0x%x)",
145 fwcfg_sc.selector.architecture ? "specific" : "generic",
146 fwcfg_sc.selector.index);
149 } else if (fwcfg_sc.data_offset >= item->size) {
151 "%s: qemu fwcfg item read exceeds size (architecture %s index 0x%x size 0x%x offset 0x%x)",
153 fwcfg_sc.selector.architecture ? "specific" : "generic",
154 fwcfg_sc.selector.index, item->size, fwcfg_sc.data_offset);
159 /* return item data */
160 *eax = item->data[fwcfg_sc.data_offset];
161 fwcfg_sc.data_offset++;
167 qemu_fwcfg_add_item(const uint16_t architecture, const uint16_t index,
168 const uint32_t size, void *const data)
170 /* truncate architecture and index to their desired size */
171 const uint16_t arch = architecture & QEMU_FWCFG_ARCHITECTURE_MASK;
172 const uint16_t idx = index & QEMU_FWCFG_INDEX_MASK;
174 /* get pointer to item specified by selector */
175 struct qemu_fwcfg_item *const fwcfg_item = &fwcfg_sc.items[arch][idx];
177 /* check if item is already used */
178 if (fwcfg_item->data != NULL) {
179 warnx("%s: qemu fwcfg item exists (architecture %s index 0x%x)",
180 __func__, arch ? "specific" : "generic", idx);
184 /* save data of the item */
185 fwcfg_item->size = size;
186 fwcfg_item->data = data;
192 qemu_fwcfg_add_item_file_dir(void)
194 const size_t size = sizeof(struct qemu_fwcfg_directory) +
195 QEMU_FWCFG_MIN_FILES * sizeof(struct qemu_fwcfg_file);
196 struct qemu_fwcfg_directory *const fwcfg_directory = calloc(1, size);
197 if (fwcfg_directory == NULL) {
201 fwcfg_sc.directory = fwcfg_directory;
203 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
204 QEMU_FWCFG_INDEX_FILE_DIR, sizeof(struct qemu_fwcfg_directory),
205 (uint8_t *)fwcfg_sc.directory));
209 qemu_fwcfg_add_item_id(void)
211 struct qemu_fwcfg_id *const fwcfg_id = calloc(1,
212 sizeof(struct qemu_fwcfg_id));
213 if (fwcfg_id == NULL) {
217 fwcfg_id->interface = 1;
220 uint32_t *const le_fwcfg_id_ptr = (uint32_t *)fwcfg_id;
221 *le_fwcfg_id_ptr = htole32(*le_fwcfg_id_ptr);
223 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
224 QEMU_FWCFG_INDEX_ID, sizeof(struct qemu_fwcfg_id),
225 (uint8_t *)fwcfg_id));
229 qemu_fwcfg_add_item_signature(void)
231 struct qemu_fwcfg_signature *const fwcfg_signature = calloc(1,
232 sizeof(struct qemu_fwcfg_signature));
233 if (fwcfg_signature == NULL) {
237 fwcfg_signature->signature[0] = 'Q';
238 fwcfg_signature->signature[1] = 'E';
239 fwcfg_signature->signature[2] = 'M';
240 fwcfg_signature->signature[3] = 'U';
242 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
243 QEMU_FWCFG_INDEX_SIGNATURE, sizeof(struct qemu_fwcfg_signature),
244 (uint8_t *)fwcfg_signature));
248 qemu_fwcfg_register_port(const char *const name, const int port, const int size,
249 const int flags, const inout_func_t handler)
251 struct inout_port iop;
253 bzero(&iop, sizeof(iop));
258 iop.handler = handler;
260 return (register_inout(&iop));
264 qemu_fwcfg_add_file(const char *name, const uint32_t size, void *const data)
266 if (strlen(name) >= QEMU_FWCFG_MAX_NAME)
270 * QEMU specifies count as big endian.
271 * Convert it to host endian to work with it.
273 const uint32_t count = be32toh(fwcfg_sc.directory->be_count) + 1;
275 /* add file to items list */
276 const uint32_t index = QEMU_FWCFG_FIRST_FILE_INDEX + count - 1;
277 const int error = qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
284 * files should be sorted alphabetical, get index for new file
287 for (file_index = 0; file_index < count - 1; ++file_index) {
288 if (strcmp(name, fwcfg_sc.directory->files[file_index].name) <
293 if (count > QEMU_FWCFG_MIN_FILES) {
294 /* alloc new file directory */
295 const uint64_t new_size = sizeof(struct qemu_fwcfg_directory) +
296 count * sizeof(struct qemu_fwcfg_file);
297 struct qemu_fwcfg_directory *const new_directory = calloc(1,
299 if (new_directory == NULL) {
301 "%s: Unable to allocate a new qemu fwcfg files directory (count %d)",
306 /* copy files below file_index to new directory */
307 memcpy(new_directory->files, fwcfg_sc.directory->files,
308 file_index * sizeof(struct qemu_fwcfg_file));
310 /* copy files above file_index to directory */
311 memcpy(&new_directory->files[file_index + 1],
312 &fwcfg_sc.directory->files[file_index],
313 (count - file_index) * sizeof(struct qemu_fwcfg_file));
315 /* free old directory */
316 free(fwcfg_sc.directory);
318 /* set directory pointer to new directory */
319 fwcfg_sc.directory = new_directory;
321 /* adjust directory pointer */
322 fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].data =
323 (uint8_t *)fwcfg_sc.directory;
325 /* shift files behind file_index */
326 for (uint32_t i = QEMU_FWCFG_MIN_FILES - 1; i > file_index;
328 memcpy(&fwcfg_sc.directory->files[i],
329 &fwcfg_sc.directory->files[i - 1],
330 sizeof(struct qemu_fwcfg_file));
335 * QEMU specifies count, size and index as big endian.
336 * Save these values in big endian to simplify guest reads of these
339 fwcfg_sc.directory->be_count = htobe32(count);
340 fwcfg_sc.directory->files[file_index].be_size = htobe32(size);
341 fwcfg_sc.directory->files[file_index].be_selector = htobe16(index);
342 strcpy(fwcfg_sc.directory->files[file_index].name, name);
344 /* set new size for the fwcfg_file_directory */
345 fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].size =
346 sizeof(struct qemu_fwcfg_directory) +
347 count * sizeof(struct qemu_fwcfg_file);
353 qemu_fwcfg_init(struct vmctx *const ctx)
358 * Bhyve supports fwctl (bhyve) and fwcfg (qemu) as firmware interfaces.
359 * Both are using the same ports. So, it's not possible to provide both
360 * interfaces at the same time to the guest. Therefore, only create acpi
361 * tables and register io ports for fwcfg, if it's used.
363 if (strcmp(lpc_fwcfg(), "qemu") == 0) {
364 error = acpi_device_create(&fwcfg_sc.acpi_dev, ctx,
365 QEMU_FWCFG_ACPI_DEVICE_NAME, QEMU_FWCFG_ACPI_HARDWARE_ID);
367 warnx("%s: failed to create ACPI device for QEMU FwCfg",
372 error = acpi_device_add_res_fixed_ioport(fwcfg_sc.acpi_dev,
373 QEMU_FWCFG_SELECTOR_PORT_NUMBER, 2);
375 warnx("%s: failed to add fixed IO port for QEMU FwCfg",
380 /* add handlers for fwcfg ports */
381 if ((error = qemu_fwcfg_register_port("qemu_fwcfg_selector",
382 QEMU_FWCFG_SELECTOR_PORT_NUMBER,
383 QEMU_FWCFG_SELECTOR_PORT_SIZE,
384 QEMU_FWCFG_SELECTOR_PORT_FLAGS,
385 qemu_fwcfg_selector_port_handler)) != 0) {
387 "%s: Unable to register qemu fwcfg selector port 0x%x",
388 __func__, QEMU_FWCFG_SELECTOR_PORT_NUMBER);
391 if ((error = qemu_fwcfg_register_port("qemu_fwcfg_data",
392 QEMU_FWCFG_DATA_PORT_NUMBER, QEMU_FWCFG_DATA_PORT_SIZE,
393 QEMU_FWCFG_DATA_PORT_FLAGS,
394 qemu_fwcfg_data_port_handler)) != 0) {
396 "%s: Unable to register qemu fwcfg data port 0x%x",
397 __func__, QEMU_FWCFG_DATA_PORT_NUMBER);
402 /* add common fwcfg items */
403 if ((error = qemu_fwcfg_add_item_signature()) != 0) {
404 warnx("%s: Unable to add signature item", __func__);
407 if ((error = qemu_fwcfg_add_item_id()) != 0) {
408 warnx("%s: Unable to add id item", __func__);
411 if ((error = qemu_fwcfg_add_item_file_dir()) != 0) {
412 warnx("%s: Unable to add file_dir item", __func__);
418 acpi_device_destroy(fwcfg_sc.acpi_dev);