From: Fabiano Rosas <farosas@suse.de>
To: "Ani Sinha" <anisinha@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>
Cc: berrange@redhat.com, armbru@redhat.com,
Ani Sinha <anisinha@redhat.com>,
qemu-devel@nongnu.org
Subject: Re: [PATCH v4 2/2] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files
Date: Thu, 09 Jan 2025 17:30:10 -0300 [thread overview]
Message-ID: <87ikqnenn1.fsf@suse.de> (raw)
In-Reply-To: <20250109074929.252339-3-anisinha@redhat.com>
Ani Sinha <anisinha@redhat.com> writes:
> At present, the libqos/fw_cfg.c library does not support the modern DMA
> interface which is required to write to the fw_cfg files. It only uses the IO
> interface. Implement read and write methods based on DMA. This will enable
> developers to write tests that writes to the fw_cfg file(s). The structure of
> the code is taken from edk2 fw_cfg implementation. It has been tested by
> writing a qtest that writes to a fw_cfg file. This test will be part of a
> future patch series.
What's the blocker for the rest of the series? It would be preferable to
merge it all together, rather than this going in first without any
users.
>
> Signed-off-by: Ani Sinha <anisinha@redhat.com>
> ---
> tests/qtest/libqos/fw_cfg.c | 138 ++++++++++++++++++++++++++++++++++++
> tests/qtest/libqos/fw_cfg.h | 6 +-
> 2 files changed, 143 insertions(+), 1 deletion(-)
>
> diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c
> index f1ed4898f7..b3ae97d32d 100644
> --- a/tests/qtest/libqos/fw_cfg.c
> +++ b/tests/qtest/libqos/fw_cfg.c
> @@ -14,6 +14,8 @@
>
> #include "qemu/osdep.h"
> #include "fw_cfg.h"
> +#include "malloc-pc.h"
> +#include "libqos-malloc.h"
> #include "../libqtest.h"
> #include "qemu/bswap.h"
> #include "hw/nvram/fw_cfg.h"
> @@ -60,6 +62,65 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
> qtest_writew(fw_cfg->qts, fw_cfg->base, key);
> }
>
> +static void
> +qfw_cfg_dma_transfer(QFWCFG *fw_cfg, QOSState *qs, void *address,
> + uint32_t length, uint32_t control)
> +{
> + FWCfgDmaAccess access;
> + uint32_t addr;
> + uint64_t guest_access_addr;
> + uint64_t gaddr;
> +
> + /* create a data buffer in guest memory */
> + gaddr = guest_alloc(&qs->alloc, length);
> + g_assert(gaddr);
Same here, none of these asserts are needed.
> +
> + if (control & FW_CFG_DMA_CTL_WRITE) {
> + qtest_bufwrite(fw_cfg->qts, gaddr, address, length);
> + }
> + access.address = cpu_to_be64(gaddr);
> + access.length = cpu_to_be32(length);
> + access.control = cpu_to_be32(control);
> +
> + /* now create a separate buffer in guest memory for 'access' */
> + guest_access_addr = guest_alloc(&qs->alloc, sizeof(access));
> + g_assert(guest_access_addr);
> + qtest_bufwrite(fw_cfg->qts, guest_access_addr, &access, sizeof(access));
> +
> + /* write lower 32 bits of address */
> + addr = cpu_to_be32((uint32_t)(uintptr_t)guest_access_addr);
> + qtest_outl(fw_cfg->qts, fw_cfg->base + 8, addr);
> +
> + /* write upper 32 bits of address */
> + addr = cpu_to_be32((uint32_t)(uintptr_t)(guest_access_addr >> 32));
> + qtest_outl(fw_cfg->qts, fw_cfg->base + 4, addr);
> +
> + g_assert(!(be32_to_cpu(access.control) & FW_CFG_DMA_CTL_ERROR));
> +
> + if (control & FW_CFG_DMA_CTL_READ) {
> + qtest_bufread(fw_cfg->qts, gaddr, address, length);
> + }
> +
> + guest_free(&qs->alloc, guest_access_addr);
> + guest_free(&qs->alloc, gaddr);
> +}
> +
> +static void
> +qfw_cfg_write_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
> + void *buf, uint32_t len)
> +{
> + qfw_cfg_select(fw_cfg, key);
> + qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_WRITE);
> +}
> +
> +static void
> +qfw_cfg_read_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
> + void *buf, uint32_t len)
> +{
> + qfw_cfg_select(fw_cfg, key);
> + qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_READ);
> +}
> +
> static bool
> find_pdir_entry(QFWCFG *fw_cfg, const char *filename,
> uint16_t *sel, uint32_t *size)
> @@ -123,6 +184,83 @@ size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
> return filesize;
> }
>
> +/*
> + * The caller need check the return value. When the return value is
> + * nonzero, it means that some bytes have been transferred.
> + *
> + * If the fw_cfg file in question is smaller than the allocated & passed-in
> + * buffer, then the first len bytes were read.
> + *
> + * If the fw_cfg file in question is larger than the passed-in
> + * buffer, then the return value explains how much was actually read.
> + *
> + * It is illegal to call this function if fw_cfg does not support DMA
> + * interface. The caller should ensure that DMA is supported before
> + * calling this function.
> + *
> + * Passed QOSState pointer qs must be initialized. qs->alloc must also be
> + * properly initialized.
> + */
> +size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
> + void *data, size_t buflen)
> +{
> + uint32_t len = 0;
> + uint16_t sel;
> + uint32_t id;
> +
> + g_assert(qs);
> + /* check if DMA is supported since we use DMA for read */
> + id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
> + g_assert(id & FW_CFG_VERSION_DMA);
> +
> + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
> + if (len > buflen) {
> + len = buflen;
> + }
> + qfw_cfg_read_entry(fw_cfg, qs, sel, data, len);
> + }
> +
> + return len;
> +}
> +
> +/*
> + * The caller need check the return value. When the return value is
> + * nonzero, it means that some bytes have been transferred.
> + *
> + * If the fw_cfg file in question is smaller than the allocated & passed-in
> + * buffer, then the buffer has been partially written.
> + *
> + * If the fw_cfg file in question is larger than the passed-in
> + * buffer, then the return value explains how much was actually written.
> + *
> + * It is illegal to call this function if fw_cfg does not support DMA
> + * interface. The caller should ensure that DMA is supported before
> + * calling this function.
> + *
> + * Passed QOSState pointer qs must be initialized. qs->alloc must also be
> + * properly initialized.
> + */
> +size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
> + void *data, size_t buflen)
> +{
> + uint32_t len = 0;
> + uint16_t sel;
> + uint32_t id;
> +
> + g_assert(qs);
> + /* write operation is only valid if DMA is supported */
> + id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
> + g_assert(id & FW_CFG_VERSION_DMA);
> +
> + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
> + if (len > buflen) {
> + len = buflen;
> + }
> + qfw_cfg_write_entry(fw_cfg, qs, sel, data, len);
> + }
> + return len;
> +}
> +
> static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
> {
> uint8_t *ptr = data;
> diff --git a/tests/qtest/libqos/fw_cfg.h b/tests/qtest/libqos/fw_cfg.h
> index b0456a15df..6d6ff09725 100644
> --- a/tests/qtest/libqos/fw_cfg.h
> +++ b/tests/qtest/libqos/fw_cfg.h
> @@ -14,6 +14,7 @@
> #define LIBQOS_FW_CFG_H
>
> #include "../libqtest.h"
> +#include "libqos.h"
>
> typedef struct QFWCFG QFWCFG;
>
> @@ -33,7 +34,10 @@ uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key);
> uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key);
> size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
> void *data, size_t buflen);
> -
> +size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
> + void *data, size_t buflen);
> +size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
> + void *data, size_t buflen);
> QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base);
> void mm_fw_cfg_uninit(QFWCFG *fw_cfg);
> QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base);
next prev parent reply other threads:[~2025-01-09 20:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-09 7:49 [PATCH v4 0/2] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Ani Sinha
2025-01-09 7:49 ` [PATCH v4 1/2] libqos/fw_cfg: refactor file directory iteraton to make it more reusable Ani Sinha
2025-01-09 20:01 ` Fabiano Rosas
2025-01-10 3:43 ` Ani Sinha
2025-01-09 7:49 ` [PATCH v4 2/2] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Ani Sinha
2025-01-09 20:30 ` Fabiano Rosas [this message]
2025-01-10 5:23 ` Ani Sinha
2025-01-10 10:12 ` Gerd Hoffmann
2025-01-10 6:43 ` Philippe Mathieu-Daudé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ikqnenn1.fsf@suse.de \
--to=farosas@suse.de \
--cc=anisinha@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=kraxel@redhat.com \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.