All of lore.kernel.org
 help / color / mirror / Atom feed
From: Octavian Purdila <tavip@google.com>
To: qemu-devel@nongnu.org
Cc: qemu-arm@nongnu.org, stefanst@google.com, pbonzini@redhat.com,
	 alex.bennee@linaro.org, thuth@redhat.com,
	peter.maydell@linaro.org,  marcandre.lureau@redhat.com,
	alistair@alistair23.me, berrange@redhat.com,  philmd@linaro.org,
	jsnow@redhat.com, crosa@redhat.com, bleal@redhat.com
Subject: [RFC PATCH 09/23] test/unit: add flexcomm unit test
Date: Mon,  5 Aug 2024 13:17:04 -0700	[thread overview]
Message-ID: <20240805201719.2345596-10-tavip@google.com> (raw)
In-Reply-To: <20240805201719.2345596-1-tavip@google.com>

Add flexcomm function selection unit tests.

Signed-off-by: Octavian Purdila <tavip@google.com>
---
 tests/unit/meson.build     |   8 +-
 tests/unit/test-flexcomm.c | 215 +++++++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+), 1 deletion(-)
 create mode 100644 tests/unit/test-flexcomm.c

diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 397f2503f8..4ccb15404d 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -141,7 +141,13 @@ if have_system
     'test-bufferiszero': [],
     'test-smp-parse': [qom, meson.project_source_root() / 'hw/core/machine-smp.c'],
     'test-vmstate': [migration, io],
-    'test-yank': ['socket-helpers.c', qom, io, chardev]
+    'test-yank': ['socket-helpers.c', qom, io, chardev],
+    'test-flexcomm': [
+      hwcore,
+      meson.project_source_root() / 'hw/core/gpio.c',
+      meson.project_source_root() / 'tests/unit/sysbus-mock.c',
+      meson.project_source_root() / 'hw/misc/flexcomm.c',
+     ],
   }
   if config_host_data.get('CONFIG_INOTIFY1')
     tests += {'test-util-filemonitor': []}
diff --git a/tests/unit/test-flexcomm.c b/tests/unit/test-flexcomm.c
new file mode 100644
index 0000000000..f4efe2ac52
--- /dev/null
+++ b/tests/unit/test-flexcomm.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2024 Google LLC
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qemu/main-loop.h"
+#include "exec/memory.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+
+#include "hw/misc/flexcomm.h"
+#include "sysbus-mock.h"
+#include "reg-utils.h"
+
+#define MAX_MSG_STACK 2
+
+typedef struct {
+    DeviceState *dev;
+    char *msg[MAX_MSG_STACK];
+    int msg_count;
+} TestFixture;
+
+#define SELECT_MSG(f, selected) "f[%d]select(%d)", f, selected
+#define REG_READ_MSG(f, addr, size) "f[%d]reg_read(%x, %d)", f, \
+        (uint32_t)addr, size
+#define REG_WRITE_MSG(f, addr, data, size) "f[%d]reg_write(%x, %x, %d)", \
+        f, (uint32_t)addr, (uint32_t)data, size
+
+#define FLEXCOMM_BASE 0x40106000UL
+
+
+static void f_ops_select(void *opaque, FlexcommState *s, int f,
+                         bool selected)
+{
+    TestFixture *tf = (TestFixture *)opaque;
+
+    tf->msg[tf->msg_count++] = g_strdup_printf(SELECT_MSG(f, selected));
+}
+
+static MemTxResult f_ops_reg_read(void *opaque, FlexcommState *s, int f,
+                                  hwaddr addr, uint64_t *data, unsigned size)
+{
+    TestFixture *tf = (TestFixture *)opaque;
+
+    tf->msg[tf->msg_count++] = g_strdup_printf(REG_READ_MSG(f, addr, size));
+    return MEMTX_OK;
+}
+
+static MemTxResult f_ops_reg_write(void *opaque, FlexcommState *s, int f,
+                                   hwaddr addr, uint64_t data, unsigned size)
+{
+    TestFixture *tf = (TestFixture *)opaque;
+
+    tf->msg[tf->msg_count++] = g_strdup_printf(REG_WRITE_MSG(f, addr, data,
+                                                             size));
+    return MEMTX_OK;
+}
+
+static void assert_msg(TestFixture *f, const char *fmt, ...)
+    __attribute__((format(printf, 2, 3)));
+
+static void assert_msg(TestFixture *f, const char *fmt, ...)
+{
+    va_list ap;
+    char *msg;
+
+    va_start(ap, fmt);
+    msg = g_strdup_vprintf(fmt, ap);
+    va_end(ap);
+
+    g_assert_cmpstr(msg, ==, f->msg[--f->msg_count]);
+}
+
+static const FlexcommFunctionOps f_ops = {
+    .select = f_ops_select,
+    .reg_read = f_ops_reg_read,
+    .reg_write = f_ops_reg_write,
+};
+
+/*
+ * Test fixture initialization.
+ */
+static void set_up(TestFixture *f, gconstpointer data)
+{
+    int i;
+
+    f->dev = qdev_new(TYPE_FLEXCOMM);
+    g_assert(f->dev);
+
+    if (data != NULL) {
+        qdev_prop_set_int32(DEVICE(f->dev), "functions", (uintptr_t)data);
+    }
+
+    qdev_realize_and_unref(f->dev, NULL, NULL);
+    sysbus_mmio_map(SYS_BUS_DEVICE(f->dev), 0, FLEXCOMM_BASE);
+
+    for (i = 0; i < FLEXCOMM_FUNCTIONS; i++) {
+        /* replace functions ops */
+        flexcomm_unregister_ops(i);
+        assert(flexcomm_register_ops(i, f, &f_ops, NULL));
+        assert(!flexcomm_register_ops(i, f, &f_ops, NULL));
+    }
+
+    device_cold_reset(f->dev);
+}
+
+static void tear_down(TestFixture *f, gconstpointer user_data)
+{
+    qdev_unrealize(f->dev);
+    g_free(f->dev);
+}
+
+static void select_test(TestFixture *f, gconstpointer user_data)
+{
+    uint32_t tmp = 0;
+    struct {
+        int persel;
+        int func;
+    } persel_func_map[] = {
+        { FLEXCOMM_PERSEL_USART, FLEXCOMM_FUNC_USART },
+        { FLEXCOMM_PERSEL_SPI, FLEXCOMM_FUNC_SPI },
+        { FLEXCOMM_PERSEL_I2C, FLEXCOMM_FUNC_I2C },
+        { FLEXCOMM_PERSEL_I2S_TX, FLEXCOMM_FUNC_I2S },
+        { FLEXCOMM_PERSEL_I2S_RX, FLEXCOMM_FUNC_I2S },
+    };
+    int i;
+
+    /* test that no function is selected */
+    g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, PERSEL) == 0);
+
+    /* no register access until a function is selected  */
+    g_assert(reg32_addr_read_raw(f->dev, FLEXCOMM_BASE, &tmp, 4)
+             == MEMTX_ERROR);
+    g_assert(reg32_addr_write_raw(f->dev, FLEXCOMM_BASE, tmp, 4)
+             == MEMTX_ERROR);
+
+    /* test that we can select all functions (including I2S RX) */
+    for (i = 0; i < ARRAY_SIZE(persel_func_map); i++) {
+        int persel = persel_func_map[i].persel;
+        int func = persel_func_map[i].func;
+
+        REG32_WRITE(f->dev, FLEXCOMM, PSELID, persel);
+        g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, PERSEL) == persel);
+        g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PID, ID) == persel);
+
+        /* check that current function was selected */
+        assert_msg(f, SELECT_MSG(func, 1));
+
+        /* check that previous function was de-selected */
+        if (i > 0) {
+            int prev_func = persel_func_map[i - 1].func;
+
+            assert_msg(f, SELECT_MSG(prev_func, 0));
+        }
+
+        /* test that we can access function registers */
+        reg32_addr_write_raw(f->dev, FLEXCOMM_BASE + 0x100, 0xabcd, 4);
+        assert_msg(f, REG_WRITE_MSG(func, 0x100, 0xabcd, 4));
+
+        reg32_addr_read_raw(f->dev, FLEXCOMM_BASE + 0x100, &tmp, 4);
+        assert_msg(f, REG_READ_MSG(func, 0x100, 4));
+    }
+
+    /* try to select something invalid */
+    REG32_WRITE(f->dev, FLEXCOMM, PSELID, 7);
+    /* check for no function selected */
+    g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, PERSEL) == 0);
+    g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PID, ID) == 0);
+    /* check that previous function was deselected */
+    assert_msg(f, SELECT_MSG(FLEXCOMM_FUNC_I2S, 0));
+
+    /* now select and lock USART */
+    tmp = FLEXCOMM_PSELID_LOCK_Msk | FLEXCOMM_PERSEL_USART;
+    REG32_WRITE(f->dev, FLEXCOMM, PSELID, tmp);
+    tmp = REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, PERSEL);
+    g_assert(tmp == FLEXCOMM_PERSEL_USART);
+    g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, LOCK) == 1);
+    tmp = REG32_READ_FIELD(f->dev, FLEXCOMM, PID, ID);
+    g_assert(tmp == FLEXCOMM_PERSEL_USART);
+    assert_msg(f, SELECT_MSG(FLEXCOMM_FUNC_USART, 1));
+
+    /* try to change the selection to spi */
+    REG32_WRITE(f->dev, FLEXCOMM, PSELID, FLEXCOMM_PERSEL_SPI);
+    /* it should still be locked USART */
+    tmp = REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, PERSEL);
+    g_assert(tmp == FLEXCOMM_PERSEL_USART);
+    g_assert(REG32_READ_FIELD(f->dev, FLEXCOMM, PSELID, LOCK) == 1);
+    tmp = REG32_READ_FIELD(f->dev, FLEXCOMM, PID, ID);
+    g_assert(tmp == FLEXCOMM_PERSEL_USART);
+}
+
+/* mock-up */
+const PropertyInfo qdev_prop_chr;
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    /* Initialize object types. */
+    sysbus_mock_init();
+    module_call_init(MODULE_INIT_QOM);
+
+    g_test_add("/flexcomm/select_test", TestFixture,
+               (gconstpointer)FLEXCOMM_FULL, set_up, select_test,
+               tear_down);
+
+    return g_test_run();
+}
-- 
2.46.0.rc2.264.g509ed76dc8-goog

  parent reply	other threads:[~2024-08-05 20:17 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05 20:16 [RFC PATCH 00/23] NXP i.MX RT595, ARM SVD and device model unit tests Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 01/23] fifo32: add peek function Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 02/23] tests/unit: add fifo test Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 03/23] scripts: add script to generate C header files from SVD XML files Octavian Purdila
2024-08-08 21:56   ` John Snow
2024-08-08 22:30     ` Octavian Purdila
2024-08-08 23:06       ` John Snow
2024-08-09  9:30       ` Philippe Mathieu-Daudé
2024-08-09  9:42         ` Paolo Bonzini
2024-08-09  9:59           ` Daniel P. Berrangé
2024-08-13  8:32             ` Philippe Mathieu-Daudé
2024-08-09  6:34     ` Paolo Bonzini
2024-08-09 19:28       ` Octavian Purdila
2024-08-12 15:27   ` Peter Maydell
2024-08-12 17:56     ` Octavian Purdila
2024-08-12 22:43       ` Richard Henderson
2024-08-13 15:47         ` Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 04/23] hw/arm: add SVD file for NXP i.MX RT595 Octavian Purdila
2024-08-06 14:06   ` Alex Bennée
2024-08-06 20:31     ` Octavian Purdila
2024-08-07 11:24       ` Philippe Mathieu-Daudé
2024-08-07 16:36         ` Octavian Purdila
2024-08-09  9:13       ` Daniel P. Berrangé
2024-08-09 22:40         ` Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 05/23] hw: add register access utility functions Octavian Purdila
2024-08-12 15:32   ` Peter Maydell
2024-08-12 21:14     ` Octavian Purdila
2024-08-12 22:35       ` Richard Henderson
2024-08-13  8:28       ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 06/23] hw/misc: add basic flexcomm device model Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 07/23] tests/unit: add system bus mock Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 08/23] test/unit: add register access macros and functions Octavian Purdila
2024-08-05 20:17 ` Octavian Purdila [this message]
2024-08-05 20:17 ` [RFC PATCH 10/23] hw/char: add support for flexcomm usart Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 11/23] test/unit: add flexcomm usart unit test Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 12/23] hw/i2c: add support for flexcomm i2c Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 13/23] test/unit: add i2c-tester Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 14/23] test/unit: add unit tests for flexcomm i2c Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 15/23] hw/ssi: add support for flexcomm spi Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 16/23] test/unit: add spi-tester Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 17/23] test/unit: add unit tests for flexcomm spi Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 18/23] hw/misc: add support for RT500's clock controller Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 19/23] test/unit: add unit tests " Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 20/23] hw/ssi: add support for flexspi Octavian Purdila
2024-08-08  5:11   ` Philippe Mathieu-Daudé
2024-08-08 21:31     ` Octavian Purdila
2024-08-09  8:54       ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 21/23] hw/misc: add support for RT500 reset controller Octavian Purdila
2024-08-08  5:00   ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 22/23] hw/arm: add basic support for the RT500 SoC Octavian Purdila
2024-08-06 14:51   ` Philippe Mathieu-Daudé
2024-08-07 23:57     ` Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 23/23] hw/arm: add RT595-EVK board Octavian Purdila
2024-08-12 16:10 ` [RFC PATCH 00/23] NXP i.MX RT595, ARM SVD and device model unit tests Peter Maydell
2024-08-12 16:22   ` Daniel P. Berrangé
2024-08-12 18:39     ` Octavian Purdila

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=20240805201719.2345596-10-tavip@google.com \
    --to=tavip@google.com \
    --cc=alex.bennee@linaro.org \
    --cc=alistair@alistair23.me \
    --cc=berrange@redhat.com \
    --cc=bleal@redhat.com \
    --cc=crosa@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanst@google.com \
    --cc=thuth@redhat.com \
    /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.