From patchwork Thu Feb 22 22:22:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774924 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp77178wrb; Thu, 22 Feb 2024 14:26:52 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCX3s8ojn90jVHFz+FGhR/ZJwLRkZ2U2AepnhPp+GmDPuzFcC8vTVR4kaXrLXF6xL5yTlQHAbNH+RHRWpq1qjsCU X-Google-Smtp-Source: AGHT+IE0tWv+aU5eOzq8JSg02Xkk3sHxecRQzkX8h+vGcvTSKtAxdjDSN2Wb01OawaqWTkKZ/SmQ X-Received: by 2002:a05:6870:5247:b0:21e:8dc7:342e with SMTP id o7-20020a056870524700b0021e8dc7342emr306181oai.25.1708640812604; Thu, 22 Feb 2024 14:26:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640812; cv=none; d=google.com; s=arc-20160816; b=HXupmXcbTGL0Xm8t8qmyhFOOQ/lxs5SOBREzMnNXUTQYaE668gjeStNT01eqcA/sbF XyFzu8K1Fe9cSutnojFbT8o/oc3pcm5sqLFWurlrm7wts/F/5Ujiq5Li9ljRNnkDj8UP E7gdyt2OFF8ukFw/V10RZ0hk5+J+ycmrSra4i6krvqgjpjuM9dzkAFmULc5bR0ykZUSd bsSarSh9GNXPlD+pb8cakyraOfvx2Kg2SfYGi2kj0pN0XFSHTEnmvkIY5ZCdx9wEN2/l TjbN4eRCXQ61mVs91mtOOD7eIIMPkYI6BFqY0pgDRSdRDa57el50cOuQR4q/m9Pzg0D0 x02A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2/xMTkt4H9928R+Sycr3uemIyeVCofAaI2nXM9aL6oM=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=VkMN9MFhqT539OtzV1wnDS1aheGmIsd9gt4pR1gAtISt3l9PQKLsH34bO8R1jnaRKo KyeFSC+0UWT1EMQp2LLZG1ZxidX9Z1VZ+NfRgML4mvTV7l6xqgzKQK7eJyuFNOYa55mZ cEmXH2sVG5d/Ariej/K9fksyqwrA8i129/m2dJ94RpOvuFs3+iya69paeAtwiCnnhNHD nWDdtsMr+iA5n8imyu6VT5yL2WiFUDk8RCJUZzS6QFQFpZO+wvvFGUu628jztuiIu0sF Cr5BbMj4VufkuAoc979Rgalm2n6lKZL3FW/KtjnWBCPR0lwzd+SqWBGbpsaBnLXIRBzw hCuQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SWiQRL+M; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id o3-20020ac87c43000000b0042e5e4cc248si498254qtv.180.2024.02.22.14.26.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:26:52 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SWiQRL+M; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUR-0002al-Rp; Thu, 22 Feb 2024 17:24:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUP-0002Zm-NY for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:25 -0500 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUM-0003A9-Kq for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:25 -0500 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1d71cb97937so3301255ad.3 for ; Thu, 22 Feb 2024 14:24:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640661; x=1709245461; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2/xMTkt4H9928R+Sycr3uemIyeVCofAaI2nXM9aL6oM=; b=SWiQRL+MH7LpXQvovLfXbcNVvSegRk3NaEUChjuIBq1VvDZtYWKfNjmUSqcfF02ziO NEd+Ay2d1mj7BfbpBGl4evgbk2wTfvyuxwA/32W/N9w81MeLUShTMf738QRwfJtu7NTO 1TImSDK0CSWDf6KrqwBqn34QgUIdGnS2hI16AtQaYJwSMY0pQQ8k0wvyU4vlPAh+5DCS VqGv152z8j69sBHY8KYysg8bP71NOOfPvC70oj4sKg6vYcPjuA/4BgRQ2EUQCaPYF3+R cjKJWVT5b40j51Zb+53+5wMoQBRH5buJDZMNBi3TVniCeikr/oRTxvGsy+6A+QgZ5uU2 0JRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640661; x=1709245461; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2/xMTkt4H9928R+Sycr3uemIyeVCofAaI2nXM9aL6oM=; b=YccS8axVVXx716P4orXVjtAyuiUx4hOSdQbd4JViFJDhDHEDE8te0rYVAMw/D4EDO7 27qhyG4b9P+ydwuwLmqn5o2LL3ojeQM3rmamqtfdGCDuWy656abbrNx1vqusD3QLnLHa Og/gWrSYzbD8esxnE+JRT3PGdIM5LUPkhd/5g/1B7wCoBXhsOf4IbTnyAOJSsO53+Dla 4cQwbiEyQFO1n0UUgSGE/CTcW3bijs55Ca8qv16bIdq/mY9IJDe92tlBd6GQskJ4gUL3 xl/5lO2lEmYTSXsAZXfUguPYhBoODsD3K6LBizXQopZH2V+psX+85m7cRR1Z04s1t5TJ KucA== X-Gm-Message-State: AOJu0YxHNqgksBpOl8YIpnuiKpvP7BAF9CkdvE+3O8izz5aC3rYpE6PG Q14tav+lC78oN4gES4J0MsMQQUGYaqO67NvGWnTTFbeHEpq5xyGg3Gqq5Usf5SwDq2qUBN23xT+ CBuU= X-Received: by 2002:a17:902:c402:b0:1db:f941:c9a with SMTP id k2-20020a170902c40200b001dbf9410c9amr234719plk.45.1708640660462; Thu, 22 Feb 2024 14:24:20 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:19 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 1/6] hw/misc/ivshmem: Add ivshmem-flat device Date: Thu, 22 Feb 2024 22:22:13 +0000 Message-Id: <20240222222218.2261956-2-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Add a new device, ivshmem-flat, which is similar to the ivshmem PCI but does not require a PCI bus. It's meant to be used on machines like those with Cortex-M MCUs, which usually lack a PCI/PCIe bus, e.g. lm3s6965evb and mps2-an385. The device currently only supports the sysbus bus. The new device, just like the ivshmem PCI device, supports both peer notification via hardware interrupts and shared memory. The device shared memory size can be set using the 'shmem-size' option and it defaults to 4 MiB, which is the default size of shmem allocated by the ivshmem server. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1134 Message-ID: <20231127052024.435743-2-gustavo.romero@linaro.org> Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Gustavo Romero --- docs/system/devices/ivshmem-flat.rst | 33 ++ hw/misc/Kconfig | 5 + hw/misc/ivshmem-flat.c | 463 +++++++++++++++++++++++++++ hw/misc/meson.build | 2 + hw/misc/trace-events | 17 + include/hw/misc/ivshmem-flat.h | 85 +++++ 6 files changed, 605 insertions(+) create mode 100644 docs/system/devices/ivshmem-flat.rst create mode 100644 hw/misc/ivshmem-flat.c create mode 100644 include/hw/misc/ivshmem-flat.h diff --git a/docs/system/devices/ivshmem-flat.rst b/docs/system/devices/ivshmem-flat.rst new file mode 100644 index 0000000000..1f97052804 --- /dev/null +++ b/docs/system/devices/ivshmem-flat.rst @@ -0,0 +1,33 @@ +Inter-VM Shared Memory Flat Device +---------------------------------- + +The ivshmem-flat device is meant to be used on machines that lack a PCI bus, +making them unsuitable for the use of the traditional ivshmem device modeled as +a PCI device. Machines like those with a Cortex-M MCU are good candidates to use +the ivshmem-flat device. Also, since the flat version maps the control and +status registers directly to the memory, it requires a quite tiny "device +driver" to interact with other VMs, which is useful in some RTOSes, like +Zephyr, which usually run on constrained resource targets. + +Similar to the ivshmem device, the ivshmem-flat device supports both peer +notification via HW interrupts and Inter-VM shared memory. This allows the +device to be used together with the traditional ivshmem, enabling communication +between, for instance, an aarch64 VM (using the traditional ivshmem device and +running Linux), and an arm VM (using the ivshmem-flat device and running Zephyr +instead). + +The ivshmem-flat device does not support the use of a ``memdev`` option (see +ivshmem.rst for more details). It relies on the ivshmem server to create and +distribute the proper shared memory file descriptor and the eventfd(s) to notify +(interrupt) the peers. Therefore, to use this device, it is always necessary to +have an ivshmem server up and running for proper device creation. + +Although the ivshmem-flat supports both peer notification (interrupts) and +shared memory, the interrupt mechanism is optional. If no input IRQ is +specified for the device it is disabled, preventing the VM from notifying or +being notified by other VMs (a warning will be displayed to the user to inform +the IRQ mechanism is disabled). The shared memory region is always present. + +The MMRs (INTRMASK, INTRSTATUS, IVPOSITION, and DOORBELL registers) offsets at +the MMR region, and their functions, follow the ivshmem spec, so they work +exactly as in the ivshmem PCI device (see ./specs/ivshmem-spec.txt). diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index 4fc6b29b43..a643cfac3a 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -68,6 +68,11 @@ config IVSHMEM_DEVICE default y if PCI_DEVICES depends on PCI && LINUX && IVSHMEM && MSI_NONBROKEN +config IVSHMEM_FLAT_DEVICE + bool + default y + depends on LINUX && IVSHMEM + config ECCMEMCTL bool select ECC diff --git a/hw/misc/ivshmem-flat.c b/hw/misc/ivshmem-flat.c new file mode 100644 index 0000000000..833ee2fefb --- /dev/null +++ b/hw/misc/ivshmem-flat.c @@ -0,0 +1,463 @@ +/* + * Inter-VM Shared Memory Flat Device + * + * SPDX-FileCopyrightText: 2023 Linaro Ltd. + * SPDX-FileContributor: Gustavo Romero + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "qemu/error-report.h" +#include "qemu/module.h" +#include "qapi/error.h" +#include "hw/irq.h" +#include "hw/qdev-properties-system.h" +#include "hw/sysbus.h" +#include "chardev/char-fe.h" +#include "exec/address-spaces.h" +#include "trace.h" + +#include "hw/misc/ivshmem-flat.h" + +static int64_t ivshmem_flat_recv_msg(IvshmemFTState *s, int *pfd) +{ + int64_t msg; + int n, ret; + + n = 0; + do { + ret = qemu_chr_fe_read_all(&s->server_chr, (uint8_t *)&msg + n, + sizeof(msg) - n); + if (ret < 0) { + if (ret == -EINTR) { + continue; + } + exit(1); + } + n += ret; + } while (n < sizeof(msg)); + + if (pfd) { + *pfd = qemu_chr_fe_get_msgfd(&s->server_chr); + } + return le64_to_cpu(msg); +} + +static void ivshmem_flat_irq_handler(void *opaque) +{ + VectorInfo *vi = opaque; + EventNotifier *e = &vi->event_notifier; + uint16_t vector_id; + const VectorInfo (*v)[64]; + + assert(e->initialized); + + vector_id = vi->id; + + /* + * The vector info struct is passed to the handler via the 'opaque' pointer. + * This struct pointer allows the retrieval of the vector ID and its + * associated event notifier. However, for triggering an interrupt using + * qemu_set_irq, it's necessary to also have a pointer to the device state, + * i.e., a pointer to the IvshmemFTState struct. Since the vector info + * struct is contained within the IvshmemFTState struct, its pointer can be + * used to obtain the pointer to IvshmemFTState through simple pointer math. + */ + v = (void *)(vi - vector_id); /* v = &IvshmemPeer->vector[0] */ + IvshmemPeer *own_peer = container_of(v, IvshmemPeer, vector); + IvshmemFTState *s = container_of(own_peer, IvshmemFTState, own); + + /* Clear event */ + if (!event_notifier_test_and_clear(e)) { + return; + } + + trace_ivshmem_flat_irq_handler(vector_id); + + /* + * Toggle device's output line, which is connected to interrupt controller, + * generating an interrupt request to the CPU. + */ + qemu_irq_pulse(s->irq); +} + +static IvshmemPeer *ivshmem_flat_find_peer(IvshmemFTState *s, uint16_t peer_id) +{ + IvshmemPeer *peer; + + /* Own ID */ + if (s->own.id == peer_id) { + return &s->own; + } + + /* Peer ID */ + QTAILQ_FOREACH(peer, &s->peer, next) { + if (peer->id == peer_id) { + return peer; + } + } + + return NULL; +} + +static IvshmemPeer *ivshmem_flat_add_peer(IvshmemFTState *s, uint16_t peer_id) +{ + IvshmemPeer *new_peer; + + new_peer = g_malloc0(sizeof(*new_peer)); + new_peer->id = peer_id; + new_peer->vector_counter = 0; + + QTAILQ_INSERT_TAIL(&s->peer, new_peer, next); + + trace_ivshmem_flat_new_peer(peer_id); + + return new_peer; +} + +static void ivshmem_flat_remove_peer(IvshmemFTState *s, uint16_t peer_id) +{ + IvshmemPeer *peer; + + peer = ivshmem_flat_find_peer(s, peer_id); + assert(peer); + + QTAILQ_REMOVE(&s->peer, peer, next); + for (int n = 0; n < peer->vector_counter; n++) { + int efd; + efd = event_notifier_get_fd(&(peer->vector[n].event_notifier)); + close(efd); + } + + g_free(peer); +} + +static void ivshmem_flat_add_vector(IvshmemFTState *s, IvshmemPeer *peer, + int vector_fd) +{ + if (peer->vector_counter >= IVSHMEM_MAX_VECTOR_NUM) { + trace_ivshmem_flat_add_vector_failure(peer->vector_counter, + vector_fd, peer->id); + close(vector_fd); + + return; + } + + trace_ivshmem_flat_add_vector_success(peer->vector_counter, + vector_fd, peer->id); + + /* + * Set vector ID and its associated eventfd notifier and add them to the + * peer. + */ + peer->vector[peer->vector_counter].id = peer->vector_counter; + g_unix_set_fd_nonblocking(vector_fd, true, NULL); + event_notifier_init_fd(&peer->vector[peer->vector_counter].event_notifier, + vector_fd); + + /* + * If it's the device's own ID, register also the handler for the eventfd + * so the device can be notified by the other peers. + */ + if (peer == &s->own) { + qemu_set_fd_handler(vector_fd, ivshmem_flat_irq_handler, NULL, + &peer->vector); + } + + peer->vector_counter++; +} + +static void ivshmem_flat_process_msg(IvshmemFTState *s, uint64_t msg, int fd) +{ + uint16_t peer_id; + IvshmemPeer *peer; + + peer_id = msg & 0xFFFF; + peer = ivshmem_flat_find_peer(s, peer_id); + + if (!peer) { + peer = ivshmem_flat_add_peer(s, peer_id); + } + + if (fd >= 0) { + ivshmem_flat_add_vector(s, peer, fd); + } else { /* fd == -1, which is received when peers disconnect. */ + ivshmem_flat_remove_peer(s, peer_id); + } +} + +static int ivshmem_flat_can_receive_data(void *opaque) +{ + IvshmemFTState *s = opaque; + + assert(s->msg_buffered_bytes < sizeof(s->msg_buf)); + return sizeof(s->msg_buf) - s->msg_buffered_bytes; +} + +static void ivshmem_flat_read_msg(void *opaque, const uint8_t *buf, int size) +{ + IvshmemFTState *s = opaque; + int fd; + int64_t msg; + + assert(size >= 0 && s->msg_buffered_bytes + size <= sizeof(s->msg_buf)); + memcpy((unsigned char *)&s->msg_buf + s->msg_buffered_bytes, buf, size); + s->msg_buffered_bytes += size; + if (s->msg_buffered_bytes < sizeof(s->msg_buf)) { + return; + } + msg = le64_to_cpu(s->msg_buf); + s->msg_buffered_bytes = 0; + + fd = qemu_chr_fe_get_msgfd(&s->server_chr); + + ivshmem_flat_process_msg(s, msg, fd); +} + +static uint64_t ivshmem_flat_iomem_read(void *opaque, + hwaddr offset, unsigned size) +{ + IvshmemFTState *s = opaque; + uint32_t ret; + + trace_ivshmem_flat_read_mmr(offset); + + switch (offset) { + case INTMASK: + ret = 0; /* Ignore read since all bits are reserved in rev 1. */ + break; + case INTSTATUS: + ret = 0; /* Ignore read since all bits are reserved in rev 1. */ + break; + case IVPOSITION: + ret = s->own.id; + break; + case DOORBELL: + trace_ivshmem_flat_read_mmr_doorbell(); /* DOORBELL is write-only */ + ret = 0; + break; + default: + /* Should never reach out here due to iomem map range being exact */ + trace_ivshmem_flat_read_write_mmr_invalid(offset); + ret = 0; + } + + return ret; +} + +static int ivshmem_flat_interrupt_peer(IvshmemFTState *s, + uint16_t peer_id, uint16_t vector_id) +{ + IvshmemPeer *peer; + + peer = ivshmem_flat_find_peer(s, peer_id); + if (!peer) { + trace_ivshmem_flat_interrupt_invalid_peer(peer_id); + return 1; + } + + event_notifier_set(&(peer->vector[vector_id].event_notifier)); + + return 0; +} + +static void ivshmem_flat_iomem_write(void *opaque, hwaddr offset, + uint64_t value, unsigned size) +{ + IvshmemFTState *s = opaque; + uint16_t peer_id = (value >> 16) & 0xFFFF; + uint16_t vector_id = value & 0xFFFF; + + trace_ivshmem_flat_write_mmr(offset); + + switch (offset) { + case INTMASK: + break; + case INTSTATUS: + break; + case IVPOSITION: + break; + case DOORBELL: + trace_ivshmem_flat_interrupt_peer(peer_id, vector_id); + ivshmem_flat_interrupt_peer(s, peer_id, vector_id); + break; + default: + /* Should never reach out here due to iomem map range being exact. */ + trace_ivshmem_flat_read_write_mmr_invalid(offset); + break; + } + + return; +} + +static const MemoryRegionOps ivshmem_flat_ops = { + .read = ivshmem_flat_iomem_read, + .write = ivshmem_flat_iomem_write, + .endianness = DEVICE_LITTLE_ENDIAN, + .impl = { /* Read/write aligned at 32 bits. */ + .min_access_size = 4, + .max_access_size = 4, + }, +}; + +static void ivshmem_flat_instance_init(Object *obj) +{ + SysBusDevice *sbd = SYS_BUS_DEVICE(obj); + IvshmemFTState *s = IVSHMEM_FLAT(obj); + + /* + * Init mem region for 4 MMRs (ivshmem_registers), + * 32 bits each => 16 bytes (0x10). + */ + memory_region_init_io(&s->iomem, obj, &ivshmem_flat_ops, s, + "ivshmem-mmio", 0x10); + sysbus_init_mmio(sbd, &s->iomem); + + /* + * Create one output IRQ that will be connect to the + * machine's interrupt controller. + */ + sysbus_init_irq(sbd, &s->irq); + + QTAILQ_INIT(&s->peer); +} + +static bool ivshmem_flat_connect_server(DeviceState *dev, Error **errp) +{ + IvshmemFTState *s = IVSHMEM_FLAT(dev); + SysBusDevice *sbd = SYS_BUS_DEVICE(dev); + int64_t protocol_version, msg; + int shmem_fd; + uint16_t peer_id; + struct stat fdstat; + + /* Check ivshmem server connection. */ + if (!qemu_chr_fe_backend_connected(&s->server_chr)) { + error_setg(errp, "ivshmem server socket not specified or incorret." + " Can't create device."); + return false; + } + + /* + * Message sequence from server on new connection: + * _____________________________________ + * |STEP| uint64_t msg | int fd | + * ------------------------------------- + * + * 0 PROTOCOL -1 \ + * 1 OWN PEER ID -1 |-- Header/Greeting + * 2 -1 shmem fd / + * + * 3 PEER IDx Other peer's Vector 0 eventfd + * 4 PEER IDx Other peer's Vector 1 eventfd + * . . + * . . + * . . + * N PEER IDy Other peer's Vector 0 eventfd + * N+1 PEER IDy Other peer's Vector 1 eventfd + * . . + * . . + * . . + * + * ivshmem_flat_recv_msg() calls return 'msg' and 'fd'. + * + * See ./docs/specs/ivshmem-spec.txt for details on the protocol. + */ + + /* Step 0 */ + protocol_version = ivshmem_flat_recv_msg(s, NULL); + + /* Step 1 */ + msg = ivshmem_flat_recv_msg(s, NULL); + peer_id = 0xFFFF & msg; + s->own.id = peer_id; + s->own.vector_counter = 0; + + trace_ivshmem_flat_proto_ver_own_id(protocol_version, s->own.id); + + /* Step 2 */ + msg = ivshmem_flat_recv_msg(s, &shmem_fd); + /* Map shmem fd and MMRs into memory regions. */ + if (msg != -1 || shmem_fd < 0) { + error_setg(errp, "Could not receive valid shmem fd." + " Can't create device!"); + return false; + } + + if (fstat(shmem_fd, &fdstat) != 0) { + error_setg(errp, "Could not determine shmem fd size." + " Can't create device!"); + return false; + } + trace_ivshmem_flat_shmem_size(shmem_fd, fdstat.st_size); + + /* + * Shmem size provided by the ivshmem server must be equal to + * device's shmem size. + */ + if (fdstat.st_size != s->shmem_size) { + error_setg(errp, "Can't map shmem fd: shmem size different" + " from device size!"); + return false; + } + + /* + * Beyond step 2 ivshmem_process_msg, called by ivshmem_flat_read_msg + * handler -- when data is available on the server socket -- will handle + * the additional messages that will be generated by the server as peers + * connect or disconnect. + */ + qemu_chr_fe_set_handlers(&s->server_chr, ivshmem_flat_can_receive_data, + ivshmem_flat_read_msg, NULL, NULL, s, NULL, true); + + memory_region_init_ram_from_fd(&s->shmem, OBJECT(s), + "ivshmem-shmem", s->shmem_size, + RAM_SHARED, shmem_fd, 0, NULL); + sysbus_init_mmio(sbd, &s->shmem); + + return true; +} + +static void ivshmem_flat_realize(DeviceState *dev, Error **errp) +{ + if (!ivshmem_flat_connect_server(dev, errp)) { + return; + } +} + +static Property ivshmem_flat_props[] = { + DEFINE_PROP_CHR("chardev", IvshmemFTState, server_chr), + DEFINE_PROP_UINT32("shmem-size", IvshmemFTState, shmem_size, 4 * MiB), + DEFINE_PROP_END_OF_LIST(), +}; + +static void ivshmem_flat_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + dc->hotpluggable = true; + dc->realize = ivshmem_flat_realize; + + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + device_class_set_props(dc, ivshmem_flat_props); + + /* Reason: Must be wired up in code (sysbus MRs and IRQ) */ + dc->user_creatable = false; +} + +static const TypeInfo ivshmem_flat_info = { + .name = TYPE_IVSHMEM_FLAT, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(IvshmemFTState), + .instance_init = ivshmem_flat_instance_init, + .class_init = ivshmem_flat_class_init, +}; + +static void ivshmem_flat_register_types(void) +{ + type_register_static(&ivshmem_flat_info); +} + +type_init(ivshmem_flat_register_types); diff --git a/hw/misc/meson.build b/hw/misc/meson.build index e4ef1da5a5..84dff09f5d 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -38,7 +38,9 @@ system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) subdir('macio') +# ivshmem devices system_ss.add(when: 'CONFIG_IVSHMEM_DEVICE', if_true: files('ivshmem.c')) +system_ss.add(when: 'CONFIG_IVSHMEM_FLAT_DEVICE', if_true: files('ivshmem-flat.c')) system_ss.add(when: 'CONFIG_ALLWINNER_SRAMC', if_true: files('allwinner-sramc.c')) system_ss.add(when: 'CONFIG_ALLWINNER_A10_CCM', if_true: files('allwinner-a10-ccm.c')) diff --git a/hw/misc/trace-events b/hw/misc/trace-events index 5f5bc92222..cba03743dd 100644 --- a/hw/misc/trace-events +++ b/hw/misc/trace-events @@ -341,3 +341,20 @@ djmemc_write(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRI # iosb.c iosb_read(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRIx64" size=%u" iosb_write(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRIx64" size=%u" + +# ivshmem-flat.c +ivshmem_flat_irq_handler(uint16_t vector_id) "Caught interrupt request: vector %d" +ivshmem_flat_new_peer(uint16_t peer_id) "New peer ID: %d" +ivshmem_flat_add_vector_failure(uint16_t vector_id, uint32_t vector_fd, uint16_t peer_id) "Failed to add vector %u (fd = %u) to peer ID %u, maximum number of vectors reached" +ivshmem_flat_add_vector_success(uint16_t vector_id, uint32_t vector_fd, uint16_t peer_id) "Successful addition of vector %u (fd = %u) to peer ID %u" +ivshmem_flat_irq_resolved(const char *irq_qompath) "IRQ QOM path '%s' correctly resolved" +ivshmem_flat_proto_ver_own_id(uint64_t proto_ver, uint16_t peer_id) "Protocol Version = 0x%"PRIx64", Own Peer ID = %u" +ivshmem_flat_shmem_size(int fd, uint64_t size) "Shmem fd (%d) total size is %"PRIu64" byte(s)" +ivshmem_flat_shmem_map(uint64_t addr) "Mapping shmem @ 0x%"PRIx64 +ivshmem_flat_mmio_map(uint64_t addr) "Mapping MMRs @ 0x%"PRIx64 +ivshmem_flat_read_mmr(uint64_t addr_offset) "Read access at offset %"PRIu64 +ivshmem_flat_read_mmr_doorbell(void) "DOORBELL register is write-only!" +ivshmem_flat_read_write_mmr_invalid(uint64_t addr_offset) "No ivshmem register mapped at offset %"PRIu64 +ivshmem_flat_interrupt_invalid_peer(uint16_t peer_id) "Can't interrupt non-existing peer %u" +ivshmem_flat_write_mmr(uint64_t addr_offset) "Write access at offset %"PRIu64 +ivshmem_flat_interrupt_peer(uint16_t peer_id, uint16_t vector_id) "Interrupting peer ID %u, vector %u..." diff --git a/include/hw/misc/ivshmem-flat.h b/include/hw/misc/ivshmem-flat.h new file mode 100644 index 0000000000..97ca0ddce6 --- /dev/null +++ b/include/hw/misc/ivshmem-flat.h @@ -0,0 +1,85 @@ +/* + * Inter-VM Shared Memory Flat Device + * + * SPDX-FileCopyrightText: 2023 Linaro Ltd. + * SPDX-FileContributor: Gustavo Romero + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#ifndef IVSHMEM_FLAT_H +#define IVSHMEM_FLAT_H + +#include "qemu/queue.h" +#include "qemu/event_notifier.h" +#include "chardev/char-fe.h" +#include "exec/memory.h" +#include "qom/object.h" +#include "hw/sysbus.h" + +#define IVSHMEM_MAX_VECTOR_NUM 64 + +/* + * QEMU interface: + * + QOM property "chardev" is the character device id of the ivshmem server + * socket + * + QOM property "shmem-size" sets the size of the RAM region shared between + * the device and the ivshmem server + * + sysbus MMIO region 0: device I/O mapped registers + * + sysbus MMIO region 1: shared memory with ivshmem server + * + sysbus IRQ 0: single output interrupt + */ + +#define TYPE_IVSHMEM_FLAT "ivshmem-flat" +typedef struct IvshmemFTState IvshmemFTState; + +DECLARE_INSTANCE_CHECKER(IvshmemFTState, IVSHMEM_FLAT, TYPE_IVSHMEM_FLAT) + +/* Ivshmem registers. See ./docs/specs/ivshmem-spec.txt for details. */ +enum ivshmem_registers { + INTMASK = 0, + INTSTATUS = 4, + IVPOSITION = 8, + DOORBELL = 12, +}; + +typedef struct VectorInfo { + EventNotifier event_notifier; + uint16_t id; +} VectorInfo; + +typedef struct IvshmemPeer { + QTAILQ_ENTRY(IvshmemPeer) next; + VectorInfo vector[IVSHMEM_MAX_VECTOR_NUM]; + int vector_counter; + uint16_t id; +} IvshmemPeer; + +struct IvshmemFTState { + SysBusDevice parent_obj; + + uint64_t msg_buf; + int msg_buffered_bytes; + + QTAILQ_HEAD(, IvshmemPeer) peer; + IvshmemPeer own; + + CharBackend server_chr; + + /* IRQ */ + qemu_irq irq; + + /* I/O registers */ + MemoryRegion iomem; + uint32_t intmask; + uint32_t intstatus; + uint32_t ivposition; + uint32_t doorbell; + + /* Shared memory */ + MemoryRegion shmem; + int shmem_fd; + uint32_t shmem_size; +}; + +#endif /* IVSHMEM_FLAT_H */ From patchwork Thu Feb 22 22:22:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774919 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp76851wrb; Thu, 22 Feb 2024 14:25:48 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVIfswBTXrAVQVriNEkSPDWZvmxRuJKRYBhqgptXZXQF5FrcS2S0ntEx14nJDtU3Xrrz2x4kcVDwmspzNsm5SXA X-Google-Smtp-Source: AGHT+IEv2dzydlI27tuBWMINrZv0LNRamArYl2GrolXKOxn8m0LCxzoA6owIbloPumWkH1XXUDqC X-Received: by 2002:a05:6808:13d1:b0:3bf:f1e9:2199 with SMTP id d17-20020a05680813d100b003bff1e92199mr283662oiw.46.1708640748365; Thu, 22 Feb 2024 14:25:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640748; cv=none; d=google.com; s=arc-20160816; b=G0oS+eNbdBzdq4bgfV5QXtxs5jRf82DXuIC4jQsnpVWLUNBIKehJSEmd/uo37wg60P w1Kro87528s+88d3EqQqQPQ5i2DA0TilzTD/6jc85ErzVLDkfSbCCmcYo7rFSV/Mvks4 w2OzrWGFSxJnnBBoZuZjn1JU7FtjV0aUmdIZ2UsY4rhaJFRNKyOSIo4wy8a3QU61whuE B/eeYY6b8xkAe5uqRChuE1uwqBLrcSFN9WiGFbAmFTmF8xGgXG1dUxMdoKf/aMKYrtko 6+XxubhO7eshe37+xFU2vC5cWWk9onm/zQ48w0VN/OHjfWy3b1MCnDAvyX06LFORN3b5 x8Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=98kHt0liMmpLoahi1BP6lbVmFWWdBBy/kfZQvJ/NECc=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=WmGTi8mftuoWm1QDoXCimzzuKt9NdTqWvKNnehSTnTPnDfHXGb2aqlSATctCp6Zhkb i0vF++TIeSl8gFJsBhux+IYBmmFqCNF3uvYbsZPMg8+az2Wh6wvBl08qP0dB7ZFA7eCw Gb8wWVwUylJXy3NevBc2GIx1gwS0LPo0IXLP44GZ/WahkJBFaLA1QnVxGgG9Zyg0I61A 8rZbOGZuxRhFx6Kb3/BUvGybWoWQIsONKQnAHZbIXfAWGI12BU7EPwXeoQoLd8DYllIg dlS31Uzra0grP9BGQ99PDCktI/9lG/v48qiz14euA/Uz0du+a89YRJTxsmsd4toR2uU5 nh3g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=my8dX9q9; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d25-20020ac85459000000b0042c765ddf7bsi937978qtq.801.2024.02.22.14.25.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:25:48 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=my8dX9q9; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUS-0002b4-GU; Thu, 22 Feb 2024 17:24:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUR-0002aS-Im for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:27 -0500 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUP-0003AW-Hy for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:27 -0500 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1dbae7b8ff2so2218115ad.3 for ; Thu, 22 Feb 2024 14:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640664; x=1709245464; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=98kHt0liMmpLoahi1BP6lbVmFWWdBBy/kfZQvJ/NECc=; b=my8dX9q9FTguT3d6StMjB/KLnwV1Zfu6pQlTyg2ygl4WISFFD5N1nxeULewkqpNU/R Z1ms9cLBiM3FoHPjD3tUfZEqhkt0jux/6669W6E0sfVYMDakE64eiQYhDmBFyAcb3J/W d+NZPRtsHpdZr7rl2/4j9EY+TaV7IOHbsPlRSylJ3H5fPdYF7AOcfSL37TZuilx1uSnq pe7cX1rJR0KD5Eh2fvIZ6W2upjNy6+e55lbEBAYzEMgSB2eFXfmEuTFPqwjKAYteSOBK 6X06E+qQ8EfyY6NF0GfZPxKiFpZ9Mo3wJC/ClFcH1QfqmOukEu31Zgi2x+DzIqqxs90c UYZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640664; x=1709245464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=98kHt0liMmpLoahi1BP6lbVmFWWdBBy/kfZQvJ/NECc=; b=MC67IRymZCpn+HnnD4ADOwHaqbCIr5wlISW7wHDXCq3ssdZL3uerbX94RqmxY1fh7X WhLz9kRadUY+z9zBipRKf8B08aJdO0172Y+/9IVL6PtXlXxcnmiIBmI1RbR6H1ixwrEw 23wRkw/XDoQ5Zzv2Ab5QpdCE2MAmLraWrVcjWIIz8y1qIZvsbwngfVz0fUm+pwXlHfRZ LkxKtjBLnQFl8umS5zseP8gBPeqtaoRT5TcICudG0W+EKnrpUWc3/xkeL1LLiMULTFo3 3lXhey0Jhclo/b44viDzVQsu3vnJ73BON9d93WC9FmrMOLJMG1PXrmjywhshiT41ozrn Gfpw== X-Gm-Message-State: AOJu0YwugSZ9Pzckus4YB0PoMAlBOLQhWCt4JHyINilAEElXAMy2XNC+ liIzANSO8z9WVEEFRBPaQM611zgZvJcuvCwPtp9ejm/mpsZKP/bUcQO1P7ioTpUQpRXvv/8I+5r ntMU= X-Received: by 2002:a17:902:e810:b0:1dc:f0e:4e with SMTP id u16-20020a170902e81000b001dc0f0e004emr246883plg.12.1708640663674; Thu, 22 Feb 2024 14:24:23 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:23 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 2/6] hw/misc/ivshmem-flat: Allow device to wire itself on sysbus Date: Thu, 22 Feb 2024 22:22:14 +0000 Message-Id: <20240222222218.2261956-3-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org This commit enables the ivshmem-flat device to wire itself on sysbus. It maps the device's Memory-Mapped Registers (MMRs) and shared memory (shmem) into the VM's memory layout and also allows connection to an input IRQ so that the device can trigger an interrupt for notification. Three device options are introduced to control how this is done: x-bus-address-iomem, x-bus-address-shmem, and x-irq-qompath. The following is an example on how to create the ivshmem-flat device on a Stellaris machine: $ qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -chardev socket,path=/tmp/ivshmem_socket,id=ivshmem_flat -device ivshmem-flat,chardev=ivshmem_flat,x-irq-qompath='/machine/soc/v7m/nvic/unnamed-gpio-in[0]',x-bus-address-iomem=0x400FF000,x-bus-address-shmem=0x40100000 -kernel zephyr_kernel.elf The IRQ QOM path option for the target machine can be determined by creating the VM without the ivshmem-flat device, going to the QEMU console and listing the QOM nodes with 'info qom-tree'. In the Stellaris example above the input IRQ is in the machine's NVIC Interrupt Controller. If 'x-irq-qompath' is not provided the device won't be able to be interrupted by other VMs (peers) and only the shared memory (shmem) feature will be supported. The MMRs for status and control (notification) are mapped to the MMIO region at 'x-bus-address-iomem', whilst the shared memory region start is mapped at address specified by 'x-bus-address-shmem'. Message-ID: <20231127052024.435743-2-gustavo.romero@linaro.org> Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Gustavo Romero --- docs/system/devices/ivshmem-flat.rst | 57 +++++++++++++++++++++ hw/core/sysbus-fdt.c | 2 + hw/misc/ivshmem-flat.c | 74 ++++++++++++++++++++++++++-- include/hw/misc/ivshmem-flat.h | 9 ++++ 4 files changed, 139 insertions(+), 3 deletions(-) diff --git a/docs/system/devices/ivshmem-flat.rst b/docs/system/devices/ivshmem-flat.rst index 1f97052804..ddc3477f52 100644 --- a/docs/system/devices/ivshmem-flat.rst +++ b/docs/system/devices/ivshmem-flat.rst @@ -31,3 +31,60 @@ the IRQ mechanism is disabled). The shared memory region is always present. The MMRs (INTRMASK, INTRSTATUS, IVPOSITION, and DOORBELL registers) offsets at the MMR region, and their functions, follow the ivshmem spec, so they work exactly as in the ivshmem PCI device (see ./specs/ivshmem-spec.txt). + + +Device Options +-------------- + +The required options to create an ivshmem-flat device are: (a) the UNIX +socket where the ivshmem server is listening, usually ``/tmp/ivshmem_socket``; +(b) the address where to map the MMRs (``x-bus-address-iomem=``) in the VM +memory layout; and (c) the address where to map the shared memory in the VM +memory layout (``x-bus-address-shmem=``). Both (a) and (b) depend on the VM +being used, as the MMRs and shmem must be mapped to a region not previously +occupied in the VM. + +Example: + +.. parsed-literal:: + + |qemu-system-arm| -chardev socket,path=/tmp/ivshmem_socket,id=ivshmem_flat -device ivshmem-flat,chardev=ivshmem_flat,x-irq-qompath='/machine/soc/v7m/nvic/unnamed-gpio-in[0]',x-bus-address-iomem=0x400FF000,x-bus-address-shmem=0x40100000 + +The other option, ``x-irq-qompath=``, is not required if the user doesn't want +the device supporting notifications. + +``x-irq-qompath``. Used to inform the device which IRQ input line it can attach +to enable the notification mechanism (IRQ). The ivshmem-flat device currently +only supports notification via vector 0. Notifications via other vectors are +ignored. (optional) + +Two examples for different machines follow. + +Stellaris machine (``- machine lm3s6965evb``): + +:: + + x-irq-qompath=/machine/soc/v7m/nvic/unnamed-gpio-in[0] + +Arm mps2-an385 machine (``-machine mps2-an385``): + +:: + + x-irq-qompath=/machine/armv7m/nvic/unnamed-gpio-in[0] + +The available IRQ input lines on a given VM that the ivshmem-flat device can be +attached to can be found from the QEMU monitor (Ctrl-a + c) with: + +(qemu) info qom-tree + +``x-bus-address-iomem``. Allows changing the address where the MMRs are mapped +into the VM memory layout. (required) + + ``x-bus-address-shmem``. Allows changing the address where the shared memory +region is mapped into the VM memory layout. (required) + +``shmem-size``. Allows changing the size (in bytes) of shared memory region. +Default is 4 MiB, which is the same default used by the ivshmem server, so +usually it's not necessary to change it. The size must match the size of the +shared memory reserverd and informed by the ivshmem server, otherwise device +creation fails. (optional) diff --git a/hw/core/sysbus-fdt.c b/hw/core/sysbus-fdt.c index eebcd28f9a..40d7356cae 100644 --- a/hw/core/sysbus-fdt.c +++ b/hw/core/sysbus-fdt.c @@ -31,6 +31,7 @@ #include "qemu/error-report.h" #include "sysemu/device_tree.h" #include "sysemu/tpm.h" +#include "hw/misc/ivshmem-flat.h" #include "hw/platform-bus.h" #include "hw/vfio/vfio-platform.h" #include "hw/vfio/vfio-calxeda-xgmac.h" @@ -495,6 +496,7 @@ static const BindingEntry bindings[] = { TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node), #endif TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node), + TYPE_BINDING(TYPE_IVSHMEM_FLAT, no_fdt_node), TYPE_BINDING("", NULL), /* last element */ }; diff --git a/hw/misc/ivshmem-flat.c b/hw/misc/ivshmem-flat.c index 833ee2fefb..972281ca57 100644 --- a/hw/misc/ivshmem-flat.c +++ b/hw/misc/ivshmem-flat.c @@ -420,16 +420,86 @@ static bool ivshmem_flat_connect_server(DeviceState *dev, Error **errp) return true; } +static bool ivshmem_flat_sysbus_wire(DeviceState *dev, Error **errp) +{ + IvshmemFTState *s = IVSHMEM_FLAT(dev); + SysBusDevice *sbd = SYS_BUS_DEVICE(dev); + + if (s->x_sysbus_mmio_addr != UINT64_MAX) { + trace_ivshmem_flat_mmio_map(s->x_sysbus_mmio_addr); + sysbus_mmio_map(sbd, 0, s->x_sysbus_mmio_addr); + } else { + error_setg(errp, "No address for iomem (MMRs) specified. Can't create" + " ivshmem-flat device. Use 'x-bus-address-iomem'" + " option."); + return false; + } + + if (s->x_sysbus_shmem_addr != UINT64_MAX) { + trace_ivshmem_flat_shmem_map(s->x_sysbus_shmem_addr); + sysbus_mmio_map(sbd, 1, s->x_sysbus_shmem_addr); + } else { + error_setg(errp, "No address for shmem specified. Can't create" + " ivshmem-flat device. Use 'x-bus-address-shmem'" + " option."); + return false; + } + + /* Check for input IRQ line, if it's provided, connect it. */ + if (s->x_sysbus_irq_qompath) { + Object *oirq; + bool ambiguous = false; + qemu_irq irq; + + oirq = object_resolve_path_type(s->x_sysbus_irq_qompath, TYPE_IRQ, + &ambiguous); + if (ambiguous) { + error_setg(errp, "Specified IRQ is ambiguous. Can't create" + " ivshmem-flat device."); + return false; + } + + if (!oirq) { + error_setg(errp, "Can't resolve IRQ QOM path."); + return false; + } + irq = (qemu_irq)oirq; + trace_ivshmem_flat_irq_resolved(s->x_sysbus_irq_qompath); + + /* + * Connect device out irq line to interrupt controller input irq line. + */ + sysbus_connect_irq(sbd, 0, irq); + } else { + /* + * If input IRQ is not provided, warn user the device won't be able + * to trigger any interrupts. + */ + warn_report("Input IRQ not specified, device won't be able" + " to handle IRQs!"); + } + + return true; +} + static void ivshmem_flat_realize(DeviceState *dev, Error **errp) { if (!ivshmem_flat_connect_server(dev, errp)) { return; } + if (!ivshmem_flat_sysbus_wire(dev, errp)) { + return; + } } static Property ivshmem_flat_props[] = { DEFINE_PROP_CHR("chardev", IvshmemFTState, server_chr), DEFINE_PROP_UINT32("shmem-size", IvshmemFTState, shmem_size, 4 * MiB), + DEFINE_PROP_STRING("x-irq-qompath", IvshmemFTState, x_sysbus_irq_qompath), + DEFINE_PROP_UINT64("x-bus-address-iomem", IvshmemFTState, + x_sysbus_mmio_addr, UINT64_MAX), + DEFINE_PROP_UINT64("x-bus-address-shmem", IvshmemFTState, + x_sysbus_shmem_addr, UINT64_MAX), DEFINE_PROP_END_OF_LIST(), }; @@ -438,13 +508,11 @@ static void ivshmem_flat_class_init(ObjectClass *klass, void *data) DeviceClass *dc = DEVICE_CLASS(klass); dc->hotpluggable = true; + dc->user_creatable = true; dc->realize = ivshmem_flat_realize; set_bit(DEVICE_CATEGORY_MISC, dc->categories); device_class_set_props(dc, ivshmem_flat_props); - - /* Reason: Must be wired up in code (sysbus MRs and IRQ) */ - dc->user_creatable = false; } static const TypeInfo ivshmem_flat_info = { diff --git a/include/hw/misc/ivshmem-flat.h b/include/hw/misc/ivshmem-flat.h index 97ca0ddce6..d5b6d99ae4 100644 --- a/include/hw/misc/ivshmem-flat.h +++ b/include/hw/misc/ivshmem-flat.h @@ -25,6 +25,12 @@ * socket * + QOM property "shmem-size" sets the size of the RAM region shared between * the device and the ivshmem server + * + QOM property "x-bus-address-iomem" is the base address of the I/O region + * on the main system bus + * + QOM property "x-bus-address-shmem" is the base address of the shared RAM + * region on the main system bus + * + QOM property "x-irq-qompath" is the QOM path of the interrupt being + * notified * + sysbus MMIO region 0: device I/O mapped registers * + sysbus MMIO region 1: shared memory with ivshmem server * + sysbus IRQ 0: single output interrupt @@ -68,9 +74,11 @@ struct IvshmemFTState { /* IRQ */ qemu_irq irq; + char *x_sysbus_irq_qompath; /* I/O registers */ MemoryRegion iomem; + uint64_t x_sysbus_mmio_addr; uint32_t intmask; uint32_t intstatus; uint32_t ivposition; @@ -80,6 +88,7 @@ struct IvshmemFTState { MemoryRegion shmem; int shmem_fd; uint32_t shmem_size; + uint64_t x_sysbus_shmem_addr; }; #endif /* IVSHMEM_FLAT_H */ From patchwork Thu Feb 22 22:22:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774922 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp77015wrb; Thu, 22 Feb 2024 14:26:19 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCViFz5TrCVNBPjgP1mso4tkbt5KvS4ZFbascydZE08cr6WsY/oc6tpJXrG1IYie8y41s2yixRbqmknIqTYuAp8D X-Google-Smtp-Source: AGHT+IFE5ZeLEU8k4CXnYCDRA8NhYXmBCmJL+2VUlT11IyqCRt7ejCNYoaelT7rIJuShWsUYGjXT X-Received: by 2002:a0c:f310:0:b0:68d:1347:77dc with SMTP id j16-20020a0cf310000000b0068d134777dcmr405469qvl.65.1708640779731; Thu, 22 Feb 2024 14:26:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640779; cv=none; d=google.com; s=arc-20160816; b=UXs6wTtACeUBDgS0kvu8691MucAVvoPbFe3BBqFjDfvEk1g98dQDvt1vAwLEf3pHYo D27cnBqTZfuyPxI0atmrJ5COYJ+uAL0xM/TBrAbIUNf1ift8ZvfdwVYK5mTeQ5UEAiX0 RvGEm1t2OrWCMJF+WdU4FUWRGJhPmCNvOQoEh0cR1yv9kBuWQ05HwD9Epy45pJih5Ufo iBiD+rL3wHpmaRIqyM2BSum2qoTWu1qxi0Xihd0JpD6gnmAPsezhD1iJhgEUAsUtR/Nh crCONGcJxwz6fRoNDQuZ9tFvzGta7lyE30Yq1zo7FSjqZd09um6kI0VQlgAGI5tuEmpq ixdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Q9UcYHsblrSFfrXQA46NbgNay0fShjw8qDCj70T3uHM=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=TtY2B9Pnquxd18Je7oF2bGfFc23VcgDCmi56QeNHvtI96JKxQaiic0U7FumdBBjuE0 Ye/l/zyiSQR2/36dUbuFofUN4Dh4fbAI/gM1vFXGlCbBx+TQ3tu5LxoPgciOraiYmo/9 jzvxq8DKgWvNPECWAoRnFToa3vlk/FeIeRAmkVO9bSx9lEEtAeW3yaklwipu9ZqTaxUU MuTW8AgYemnl99XufBuip+NOEecI51J8BxDjwXoHzaC4j9i5WEtHZWxt4TYwvYWNrwsd 4GDqCWwCbn24uUNYxZzZJ/y0EKxhXwcwjnBk+jIv8u5nE5tyngqux6mx6sB7geVic/YM Sjww==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iv50Vm8Z; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a4-20020ad45c44000000b0068f34f52833si15221080qva.107.2024.02.22.14.26.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:26:19 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iv50Vm8Z; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUW-0002ds-3u; Thu, 22 Feb 2024 17:24:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUV-0002bl-0s for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:31 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUS-0003BC-CX for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:29 -0500 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1dc29f1956cso1288065ad.0 for ; Thu, 22 Feb 2024 14:24:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640667; x=1709245467; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q9UcYHsblrSFfrXQA46NbgNay0fShjw8qDCj70T3uHM=; b=iv50Vm8ZwkMM6CAkwpkBEwGpswYsH81/7OMPUZfuChJ7fobHzKjwTPCvD042+PfJKI HY/8o7hZEZkM+GtB3TEu+nQYdhapXT5je6M5IMj/yMhkVzjjyB4NoSpt0sN0h0SrsBfh 0kcRaoerOb4IDlYIIrtuLs4KHo5DwJ6fxnoLpRHh8rDlrurM1equg7qsvT3bC+va6Vnz PNtd/VxQLs0CLr65k76+leW2AMoHMUp6PDJRf4QZp3Bo88DbQG+JADx0T3cMGCmwO1/E fW/l7sNB5zmi+WJ1Hq6lWGf5ztFSI2eqaMiDB7D4L1Odfd1LG8QUD+DR+nN7LbfslpD8 RazQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640667; x=1709245467; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q9UcYHsblrSFfrXQA46NbgNay0fShjw8qDCj70T3uHM=; b=F1W16F+FcvMcoqB67lHwHQnpiMwNywlifQ9wvkkeAo+e+X0J93RilpH5imLxwU287I yXtFjuGhZADIuwsVEaYjT0VbRSq2DgNB2LeYtuyPgxtc2HG+j5dF2xU62M2pZUxymExw /37B1HhsuBJmTo83AF6wzgTvgkRJ0wvjPeDjwu3eWAkiAP+Oh7qGhA3ZZOPvyb2jyim6 ochxZ0IQGTScenFhAurqKIz+/1KLpEefz4+m8k55FIuGUITPHt2vAEF+T/DiaR2erHAz WERN6p9xHNU9EdikcAELGlPDGzx2iEo4fcXrSE/h/CnI90u+X0dhsDUFJg1Ymrq302JF WXxw== X-Gm-Message-State: AOJu0YzzA48njuV1sgiPUhZ2QYqiWAHxyiiI0LbJn9pFYlWzMYcdZ1ax ANntkYOZGzlbqBdw0NrSNPT2phHX9qCHDo6giVfoa0LWjCjZZ4lMywP7x+mMIz875OCEnvEtIT/ 44G0= X-Received: by 2002:a17:902:7895:b0:1dc:139:8488 with SMTP id q21-20020a170902789500b001dc01398488mr199012pll.5.1708640666890; Thu, 22 Feb 2024 14:24:26 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:26 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 3/6] hw/arm: Allow some machines to use the ivshmem-flat device Date: Thu, 22 Feb 2024 22:22:15 +0000 Message-Id: <20240222222218.2261956-4-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x635.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Allow Arm machine lm3s6965evb and the mps2 ones, like the mps2-an385, to use the ivshmem-flat device. Message-ID: <20231127052024.435743-2-gustavo.romero@linaro.org> Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Gustavo Romero --- hw/arm/mps2.c | 3 +++ hw/arm/stellaris.c | 3 +++ hw/arm/virt.c | 2 ++ 3 files changed, 8 insertions(+) diff --git a/hw/arm/mps2.c b/hw/arm/mps2.c index 50919ee46d..fe158dfbc0 100644 --- a/hw/arm/mps2.c +++ b/hw/arm/mps2.c @@ -42,6 +42,7 @@ #include "hw/timer/cmsdk-apb-dualtimer.h" #include "hw/misc/mps2-scc.h" #include "hw/misc/mps2-fpgaio.h" +#include "hw/misc/ivshmem-flat.h" #include "hw/ssi/pl022.h" #include "hw/i2c/arm_sbcon_i2c.h" #include "hw/net/lan9118.h" @@ -472,6 +473,8 @@ static void mps2_class_init(ObjectClass *oc, void *data) mc->max_cpus = 1; mc->default_ram_size = 16 * MiB; mc->default_ram_id = "mps.ram"; + + machine_class_allow_dynamic_sysbus_dev(mc, TYPE_IVSHMEM_FLAT); } static void mps2_an385_class_init(ObjectClass *oc, void *data) diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c index a2f998bf9e..e25858f232 100644 --- a/hw/arm/stellaris.c +++ b/hw/arm/stellaris.c @@ -28,6 +28,7 @@ #include "hw/watchdog/cmsdk-apb-watchdog.h" #include "migration/vmstate.h" #include "hw/misc/unimp.h" +#include "hw/misc/ivshmem-flat.h" #include "hw/timer/stellaris-gptm.h" #include "hw/qdev-clock.h" #include "qom/object.h" @@ -1404,6 +1405,8 @@ static void lm3s6965evb_class_init(ObjectClass *oc, void *data) mc->init = lm3s6965evb_init; mc->ignore_memory_transaction_failures = true; mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3"); + + machine_class_allow_dynamic_sysbus_dev(mc, TYPE_IVSHMEM_FLAT); } static const TypeInfo lm3s6965evb_type = { diff --git a/hw/arm/virt.c b/hw/arm/virt.c index 0af1943697..6c0917f3b2 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -84,6 +84,7 @@ #include "hw/virtio/virtio-iommu.h" #include "hw/char/pl011.h" #include "qemu/guest-random.h" +#include "hw/misc/ivshmem-flat.h" #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \ static void virt_##major##_##minor##_class_init(ObjectClass *oc, \ @@ -2973,6 +2974,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data) machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE); machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE); machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM); + machine_class_allow_dynamic_sysbus_dev(mc, TYPE_IVSHMEM_FLAT); #ifdef CONFIG_TPM machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS); #endif From patchwork Thu Feb 22 22:22:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774921 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp76917wrb; Thu, 22 Feb 2024 14:25:59 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVlABfsCZPV/z9W0HKrSS6m6/qlq9esIDIeJhSjDEzOiheGEH2HW8LwNAn40i/ua1WNtBt9vBTxGmdXh3DwoJ8a X-Google-Smtp-Source: AGHT+IFe9MFAAcqM2xXWEeLsntVP/ofADJ2uaPnhFpAtFjSqgoxmD2VQBkc9uHLMZe8w5wAfJ0+p X-Received: by 2002:ac8:58d6:0:b0:42d:c864:4fd with SMTP id u22-20020ac858d6000000b0042dc86404fdmr628058qta.6.1708640759429; Thu, 22 Feb 2024 14:25:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640759; cv=none; d=google.com; s=arc-20160816; b=vmZZoR/b563gYEIeF5YzYF3ZI31EJWjGzoXeNwhFzZWjSdw4flGZ0v0veWeZo8Gm6q sgRZ9Wlv5Q9PDJPO1UHd1EBXTg0uqaLolFp1SRiudeU0l8tctCoV6QPQT5no6pYKk6xj FzK2EqbJaG7DvTiAHnVAB5NsDGwPH8Wt+qrMMKAa1ZSWzgBJ+faObI7Klb3HdZzTDVFp K6sy5s57udeFOQIn4P81GPaqHUqt8miuU/4rvOIU+dWqV1RMXbUT5TCogTFr81oLYz7H ibFZDVZHk46ggmdCcsFar/WKklFi0NA8ATpj4JnROhGwgTRBIBOvkQLxmErm44QCyvvL PRBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aV4SgQZuDXWBRvw2n8ljUcfJ5DI/n/OkG0enno6mss4=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=tFGdL5B9DoTRYHiEXiFHAQ7bXAYvXjJl76AdJMI4nkQoouUed2ud7I+ETzI65+gdTZ 384692i2tWAfzWKSplkM22S58O1brFw3v3YR//5CYKm0A4jc+wvDjzKfHaV3wGSoyU6Z h/wK6EXV8UHe7KqOcu1zjP9tiSrPdW1NP89nNWLVQeewBTeoO8cW11L2nwHnPPopYWC0 Qcci76GYNBYIYBoA31Wi9qEJ2MLKiBqKVdxXHbWM3Glj6ttqF8NvzJUOgJpz/u11lSe4 BF7GE+nBBnAqtFeKvUBiHO1o49faVQFsPUFYtSemjNXKPo/X3rJPZ5IQIfkGbs1Vd6/T wvVg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YuJf5YE6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j20-20020ac874d4000000b0042dbe72f60fsi12825863qtr.233.2024.02.22.14.25.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:25:59 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YuJf5YE6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUY-0002eZ-7L; Thu, 22 Feb 2024 17:24:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUX-0002eG-7A for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:33 -0500 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUV-0003Cw-Pu for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:32 -0500 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1dc0e5b223eso1292305ad.1 for ; Thu, 22 Feb 2024 14:24:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640670; x=1709245470; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aV4SgQZuDXWBRvw2n8ljUcfJ5DI/n/OkG0enno6mss4=; b=YuJf5YE6DIYvSljaoegdnCIXW0X7IU9/mZs1OJBGrjFI31bUPjLpEcLiJ/HFmpQPrt HRV27u8AdEtDCyQHVp6GR012goVXuvVoL0AH4wrHhwaAygGU9Xrh/dfjiCwSVr4EAMxc 14CkHjK7ltwW4Vi9JIsIwvx3invjgE0Uq+nshwBqigcdOmBnZt7hXuWGHBXOXz9svgWy Js0GXPtYQg6l2YZrlPZ705T1Hk++ssZDz4ZJ11L+TkhdDAdXChI0kWQPVBrdqW/iToea Ly+WME7WCcfzDudGDvEYpNq3W9m/pRGVpWxbwA8oJ5G2TyOzX8kkfqollOtI2MiC87pP sRjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640670; x=1709245470; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aV4SgQZuDXWBRvw2n8ljUcfJ5DI/n/OkG0enno6mss4=; b=dNrj/oi5hrNFN3VK5JPOGV5ikA1fN5Dbnbf0+PXvuSTyEj2O+ixMiDC7AJY4LUcGul 913zUmg9BMnJN/ECh107sqfTsmsgO/Mk1+32IG+cbITEkEX47orbdp/6mSf0l8Z3toLk cMupiWRBQS5agt22DngrznvXgQk1i83tKyPp0c3dx8rSgnZBZHKV0i53hQd4+mv0K7Cf u2BBVKIOK5jao5A8CcnYAygnhfWa3h5UqnuszDZYMqMztoDF3i9r+80uA/bsEtBoJX+Z dxucUV1U4kzap7z+n6wKErCVuxhxKy7UV8bQcTIfza/SvSgOqI+DOF4mC3B/kpMSh7bp 3VnQ== X-Gm-Message-State: AOJu0YzCbC547uGdrE/a6OpfuzdF5M+kZ5B2uJreQuWUrKGX/WlY850W OVxVxF5fO3W8Dcsf2/NrSg6Z2Dt6JON4WjiGhDWsJq8pefwcFS2IXkw1RkzhFgfCfKo42Scbj4d rHeo= X-Received: by 2002:a17:902:e74f:b0:1dc:19e:3d9d with SMTP id p15-20020a170902e74f00b001dc019e3d9dmr246112plf.30.1708640669975; Thu, 22 Feb 2024 14:24:29 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:29 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 4/6] hw/misc/ivshmem: Rename ivshmem to ivshmem-pci Date: Thu, 22 Feb 2024 22:22:16 +0000 Message-Id: <20240222222218.2261956-5-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Because now there is also an MMIO ivshmem device (ivshmem-flat.c), and ivshmem.c is a PCI specific implementation, rename it to ivshmem-pci.c. Reviewed-by: Philippe Mathieu-Daudé Message-ID: <20231127052024.435743-5-gustavo.romero@linaro.org> Signed-off-by: Gustavo Romero --- hw/misc/{ivshmem.c => ivshmem-pci.c} | 0 hw/misc/meson.build | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename hw/misc/{ivshmem.c => ivshmem-pci.c} (100%) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem-pci.c similarity index 100% rename from hw/misc/ivshmem.c rename to hw/misc/ivshmem-pci.c diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 84dff09f5d..4a9369082b 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -39,7 +39,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) subdir('macio') # ivshmem devices -system_ss.add(when: 'CONFIG_IVSHMEM_DEVICE', if_true: files('ivshmem.c')) +system_ss.add(when: 'CONFIG_IVSHMEM_DEVICE', if_true: files('ivshmem-pci.c')) system_ss.add(when: 'CONFIG_IVSHMEM_FLAT_DEVICE', if_true: files('ivshmem-flat.c')) system_ss.add(when: 'CONFIG_ALLWINNER_SRAMC', if_true: files('allwinner-sramc.c')) From patchwork Thu Feb 22 22:22:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774923 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp77060wrb; Thu, 22 Feb 2024 14:26:29 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWIHcVUc2K7BjVg9S//iYsQjTeCpVMw6CAcSAfUGf/R4rGlHC83OYx57BYHN5pKVGYx/NFqf4pm5wr/axxvafWZ X-Google-Smtp-Source: AGHT+IHXURkdEwaiQyNoToQ5NUx9B7MwnOPBNJN8mwN4EQQtxWqJJ6b57+ngt4+N2JTJF0J0uZX+ X-Received: by 2002:a05:6102:ccb:b0:471:ac66:61ce with SMTP id g11-20020a0561020ccb00b00471ac6661cemr485826vst.0.1708640789293; Thu, 22 Feb 2024 14:26:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640789; cv=none; d=google.com; s=arc-20160816; b=S1cxP87Guaq7q8/+2fdh+OmsyWO+nZIbvNEN+DOBjpxioM0J5I8yQjgT4qzvmKU+Wz s+vhtzZRD/k306w5BS2U18yoJT1JOKAMTN7hIMnGsgHw+QlnDsGi5LS4weNV3Swo+Y6n wfDpG5pn2KKg7qiYHgj9eEkidmQHVle51JW2GV6VrJFeEWZ+j3PSB36HOH4m5VrBB4np j7OfLZh+6j9aRmtGoRo+Mu4MNhOewh8a5wOMmkQZ/C+6b9UstETfrso+o/Wj3E1eho7P muAyo2wxWjjI4a0/VFnuvCVpRSXaIMMXYlMrAsAK4vRwpASBcXoWNtwbuJgbixrOX/e0 qGSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HX02PeSmvlZ6JSpGgvLOAz55jZu9nn1Y4vVQAH0ZIkk=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=WIKyEObBO47leHGKktngQmrfI0yAllYs6iN8liht1Ji/NBU20wTZfZ5zq86j5YC07N ryjXGge3t/+I2qLqsJSq/UvfM/ACis9wqNTRhw1uEdBwwwSOdtK4PRxT9en65ODiCeDq SIWl1kOsTP/lIl1G4yO54iwMs1Y3suTdKHzxAsfQAty4UxtDBC2oEoravtCygvlOBOwL qOHX+zzEAQKyr7nZUeCYDMffI5g+DJvU48Pq4CbXtEwzUZXc3opNlH2WBnoPpD/uM7c8 QqNpRUvSr8+eV4Sdj65a/2mTPbBRjYyZ4uFDEcJkuh+c0YDQyQ/NeS/WAlZahLiEm3rw jZcA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QiN2jtmp; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id c13-20020ac8660d000000b0042e2aa110easi2535971qtp.691.2024.02.22.14.26.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:26:29 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QiN2jtmp; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUd-0002fp-0X; Thu, 22 Feb 2024 17:24:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUb-0002fJ-8c for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:37 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUZ-0003DS-0c for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:37 -0500 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d95d67ff45so1013025ad.2 for ; Thu, 22 Feb 2024 14:24:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640673; x=1709245473; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HX02PeSmvlZ6JSpGgvLOAz55jZu9nn1Y4vVQAH0ZIkk=; b=QiN2jtmpebe+p0wmeE17sgGw81zznSNbeixmUzuHQ1g/cgRWEjUBGDOQqvb3Fl28UG HeB0DbA0BM15Rk7ATMMi0Bo98f3N1VNoV77nyySi2FN9QP/4B5ikSD8gwKbhLAJO4LYd HBjaV9D1QWzERZZ7oEW9g0il5mFgKcdap3/Z+mTOLfeiqHAfVA8osFXVNwrk4N+aZVOd lshj4H9QtacGl2RFHzF6xBtp0kgTq9yh8FIH1josEzsrYqV855SCvgLrdw1Uk1V9XeY/ +dPBN40GtdifzMlTc3bKsmJvSuMdA9OltOweb/yaf722J5Cso/py/nORHlqmwUs6VKVS hlhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640673; x=1709245473; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HX02PeSmvlZ6JSpGgvLOAz55jZu9nn1Y4vVQAH0ZIkk=; b=vJBn3/SR2d7RUU0NlEXiOB2oY34aIYfVzDWBi2J9z4uuFo3RrR3B5/1umMg7ck2XM+ mbeMZBPfOlswdTbkwZfbwJ3rjF1eacpFVSmtjBOyFf2AlJbqcX1Zvdk/W12prrtHL3Mq Yt03ZgtxfLF9OyTow5Bre8n5r7jJ390LjjxMr885oW2lQlfrxWgf3z6bCyzwAsaJtHXE m6tEbMJ5JP//qgvyNNwmXajjcbxrReqE/yVlsSySk1Xc0EzsEX+WXE1bs+scV1YZDFRD ydQXyWsh8BcK0TflPjUzQUjJEWLs0Jw4jcOxi2UgkJmVzrOCCAd/mVw3V9iamy1ym1ni 85/Q== X-Gm-Message-State: AOJu0YzXQRXrm7lEdgukzeZf/aE6NMSooKozDwcrZRAsGxdzmO7gVgYL r+DWO6VM0tBKGzvjblkC3Oq5LzXDPhWriJ5nCS7S1Deq0esBnNpjLv+aF65AVYd1vPTYAvX+LtN r3jw= X-Received: by 2002:a17:902:b20b:b0:1d7:4353:aba5 with SMTP id t11-20020a170902b20b00b001d74353aba5mr120331plr.58.1708640673193; Thu, 22 Feb 2024 14:24:33 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:32 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 5/6] tests/qtest: Reorganize common code in ivshmem-test Date: Thu, 22 Feb 2024 22:22:17 +0000 Message-Id: <20240222222218.2261956-6-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org This commit reorganizes the ivshmem-test qtest by moving common structs, functions, and code that can be utilized by other ivshmem qtests into two new files: ivshmem-utils.h and ivshmem-utils.c. Enum Reg, struct ServerThread, and mktempshm() have been relocated to these new files. Two new functions have been introduced to handle the ivshmem server start/stop: test_ivshmem_server_{start,stop}. To accommodate the new way for starting/stopping the ivshmem server, struct ServerThread now includes two new members: 'server', previously present but not a member of any struct; and 'status', a new member of a new type, ServerStartStatus, used to track and handle service termination properly. Additionally, a new function, mktempsocket(), has been added to help create a unix socket filename, similar to what mktempshm() does for the creation of a shm file. Finally, the ivshmem-test qtest has been adapted to use the new ivhsmem utils. Adjustments in that sense have also been made to meson.build; also 'rt' have been removed as a lib dependency for ivhsmem-test.c. Two lines unrelated to these changes have had their line indentation also fixed in meson.build. Message-ID: <20231127052024.435743-3-gustavo.romero@linaro.org> Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Gustavo Romero Acked-by: Thomas Huth --- tests/qtest/ivshmem-test.c | 113 ++------------------------ tests/qtest/ivshmem-utils.c | 156 ++++++++++++++++++++++++++++++++++++ tests/qtest/ivshmem-utils.h | 56 +++++++++++++ tests/qtest/meson.build | 6 +- 4 files changed, 222 insertions(+), 109 deletions(-) create mode 100644 tests/qtest/ivshmem-utils.c create mode 100644 tests/qtest/ivshmem-utils.h diff --git a/tests/qtest/ivshmem-test.c b/tests/qtest/ivshmem-test.c index 9bf8e78df6..5ce43e2f76 100644 --- a/tests/qtest/ivshmem-test.c +++ b/tests/qtest/ivshmem-test.c @@ -3,17 +3,17 @@ * * Copyright (c) 2014 SUSE LINUX Products GmbH * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2023 Linaro Ltd. * * 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 -#include "contrib/ivshmem-server/ivshmem-server.h" +#include "ivshmem-utils.h" #include "libqos/libqos-pc.h" #include "libqos/libqos-spapr.h" -#include "libqtest.h" + +static ServerThread thread; #define TMPSHMSIZE (1 << 20) static char *tmpshm; @@ -45,13 +45,6 @@ typedef struct _IVState { QPCIDevice *dev; } IVState; -enum Reg { - INTRMASK = 0, - INTRSTATUS = 4, - IVPOSITION = 8, - DOORBELL = 12, -}; - static const char* reg2str(enum Reg reg) { switch (reg) { case INTRMASK: @@ -241,54 +234,6 @@ static void test_ivshmem_pair(void) g_free(data); } -typedef struct ServerThread { - GThread *thread; - IvshmemServer *server; - int pipe[2]; /* to handle quit */ -} ServerThread; - -static void *server_thread(void *data) -{ - ServerThread *t = data; - IvshmemServer *server = t->server; - - while (true) { - fd_set fds; - int maxfd, ret; - - FD_ZERO(&fds); - FD_SET(t->pipe[0], &fds); - maxfd = t->pipe[0] + 1; - - ivshmem_server_get_fds(server, &fds, &maxfd); - - ret = select(maxfd, &fds, NULL, NULL, NULL); - - if (ret < 0) { - if (errno == EINTR) { - continue; - } - - g_critical("select error: %s\n", strerror(errno)); - break; - } - if (ret == 0) { - continue; - } - - if (FD_ISSET(t->pipe[0], &fds)) { - break; - } - - if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) { - g_critical("ivshmem_server_handle_fds() failed\n"); - break; - } - } - - return NULL; -} - static void setup_vm_with_server(IVState *s, int nvectors) { char *cmd; @@ -304,27 +249,12 @@ static void setup_vm_with_server(IVState *s, int nvectors) static void test_ivshmem_server(void) { - g_autoptr(GError) err = NULL; IVState state1, state2, *s1, *s2; - ServerThread thread; - IvshmemServer server; int ret, vm1, vm2; int nvectors = 2; guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; - ret = ivshmem_server_init(&server, tmpserver, tmpshm, true, - TMPSHMSIZE, nvectors, - g_test_verbose()); - g_assert_cmpint(ret, ==, 0); - - ret = ivshmem_server_start(&server); - g_assert_cmpint(ret, ==, 0); - - thread.server = &server; - g_unix_open_pipe(thread.pipe, FD_CLOEXEC, &err); - g_assert_no_error(err); - thread.thread = g_thread_new("ivshmem-server", server_thread, &thread); - g_assert(thread.thread != NULL); + test_ivshmem_server_start(&thread, tmpserver, tmpshm, nvectors); setup_vm_with_server(&state1, nvectors); s1 = &state1; @@ -367,15 +297,7 @@ static void test_ivshmem_server(void) cleanup_vm(s2); cleanup_vm(s1); - if (qemu_write_full(thread.pipe[1], "q", 1) != 1) { - g_error("qemu_write_full: %s", g_strerror(errno)); - } - - g_thread_join(thread.thread); - - ivshmem_server_close(&server); - close(thread.pipe[1]); - close(thread.pipe[0]); + test_ivshmem_server_stop(&thread); } static void test_ivshmem_hotplug_q35(void) @@ -454,31 +376,10 @@ static void cleanup(void) static void abrt_handler(void *data) { + test_ivshmem_server_stop(&thread); cleanup(); } -static gchar *mktempshm(int size, int *fd) -{ - while (true) { - gchar *name; - - name = g_strdup_printf("/qtest-%u-%u", getpid(), g_test_rand_int()); - *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL, - S_IRWXU|S_IRWXG|S_IRWXO); - if (*fd > 0) { - g_assert(ftruncate(*fd, size) == 0); - return name; - } - - g_free(name); - - if (errno != EEXIST) { - perror("shm_open"); - return NULL; - } - } -} - int main(int argc, char **argv) { int ret, fd; diff --git a/tests/qtest/ivshmem-utils.c b/tests/qtest/ivshmem-utils.c new file mode 100644 index 0000000000..c2fc3463dd --- /dev/null +++ b/tests/qtest/ivshmem-utils.c @@ -0,0 +1,156 @@ +/* + * Common utilities for testing ivshmem devices + * + * SPDX-FileCopyrightText: 2012 SUSE LINUX Products GmbH + * SPDX-FileCopyrightText: 2021 Red Hat, Inc. + * SPDX-FileCopyrightText: 2023 Linaro Ltd. + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#include "ivshmem-utils.h" + +gchar *mktempshm(int size, int *fd) +{ + while (true) { + /* Relative path to the shm filesystem, e.g. '/dev/shm'. */ + gchar *shm_rel_path; + + shm_rel_path = g_strdup_printf("/ivshmem_qtest-%u-%u", getpid(), + g_test_rand_int()); + *fd = shm_open(shm_rel_path, O_CREAT | O_RDWR | O_EXCL, + S_IRWXU | S_IRWXG | S_IRWXO); + if (*fd > 0) { + g_assert(ftruncate(*fd, size) == 0); + return shm_rel_path; + } + + g_free(shm_rel_path); + + if (errno != EEXIST) { + perror("shm_open"); + return NULL; + } + } +} + +gchar *mktempsocket(void) +{ + gchar *server_socket_path; + + server_socket_path = g_strdup_printf("%s/ivshmem_socket_qtest-%u-%u", + g_get_tmp_dir(), getpid(), + g_test_rand_int()); + return server_socket_path; +} + +static void *server_thread(void *data) +{ + ServerThread *t = data; + IvshmemServer *server = &t->server; + + while (true) { + fd_set fds; + int maxfd, ret; + + FD_ZERO(&fds); + FD_SET(t->pipe[0], &fds); + maxfd = t->pipe[0] + 1; + + ivshmem_server_get_fds(server, &fds, &maxfd); + + ret = select(maxfd, &fds, NULL, NULL, NULL); + + if (ret < 0) { + if (errno == EINTR) { + continue; + } + + g_critical("select error: %s\n", strerror(errno)); + break; + } + if (ret == 0) { + continue; + } + + if (FD_ISSET(t->pipe[0], &fds)) { + break; + } + + if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) { + g_critical("ivshmem_server_handle_fds() failed\n"); + break; + } + } + + return NULL; +} + +void test_ivshmem_server_start(ServerThread *thread, + const char *server_socket_path, + const char *shm_rel_path, unsigned num_vectors) +{ + g_autoptr(GError) err = NULL; + int ret; + struct stat shm_st; + char *shm_path; + + g_assert(thread != NULL); + g_assert(server_socket_path != NULL); + g_assert_cmpint(num_vectors, >, 0); + g_assert(shm_rel_path != NULL); + + /* + * Find out shm size. shm_open() deals with relative paths but stat() needs + * the full path to the shm file. + */ + shm_path = g_strdup_printf("/dev/shm%s", shm_rel_path); + ret = stat(shm_path, &shm_st); + g_assert_cmpint(ret, ==, 0); + g_assert_cmpint(shm_st.st_size, >, 0); + + ret = ivshmem_server_init(&thread->server, server_socket_path, shm_rel_path, + true, shm_st.st_size, num_vectors, g_test_verbose()); + g_assert_cmpint(ret, ==, 0); + ret = ivshmem_server_start(&thread->server); + g_assert_cmpint(ret, ==, 0); + thread->status = SERVER; + + g_unix_open_pipe(thread->pipe, FD_CLOEXEC, &err); + g_assert_no_error(err); + thread->status |= PIPE; + + thread->thread = g_thread_new("ivshmem-server", server_thread, thread); + g_assert(thread->thread != NULL); + thread->status |= THREAD; +} + +void test_ivshmem_server_stop(ServerThread *thread) +{ + /* + * This function can be called any time on a test error/abort (e.g., it can + * be called from the abort handler), including from the + * test_ivshmem_server_start(). Therefore, the start steps (server started, + * pipe created, and thread created) are tracked when the server starts and + * then checked below accordingly for proper termination. + */ + + if (thread->status & THREAD) { + /* Ask to exit from thread. */ + if (qemu_write_full(thread->pipe[1], "q", 1) != 1) { + g_error("qemu_write_full: %s", g_strerror(errno)); + } + + /* Wait thread to exit. */ + g_thread_join(thread->thread); + } + + if (thread->status & PIPE) { + close(thread->pipe[1]); + close(thread->pipe[0]); + } + + if (thread->status & SERVER) { + ivshmem_server_close(&thread->server); + } +} diff --git a/tests/qtest/ivshmem-utils.h b/tests/qtest/ivshmem-utils.h new file mode 100644 index 0000000000..c43661caac --- /dev/null +++ b/tests/qtest/ivshmem-utils.h @@ -0,0 +1,56 @@ +/* + * Common utilities for testing ivshmem devices + * + * SPDX-FileCopyrightText: 2012 SUSE LINUX Products GmbH + * SPDX-FileCopyrightText: 2021 Red Hat, Inc. + * SPDX-FileCopyrightText: 2023 Linaro Ltd. + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#ifndef QTEST_IVSHMEM_UTILS_H +#define QTEST_IVSHMEM_UTILS_H + +#include "qemu/osdep.h" +#include +#include "contrib/ivshmem-server/ivshmem-server.h" +#include "libqtest.h" + +enum Reg { + INTRMASK = 0, + INTRSTATUS = 4, + IVPOSITION = 8, + DOORBELL = 12, +}; + +enum ServerStartStatus { + SERVER = 1, /* Ivshmem server started */ + THREAD = 2, /* Thread for monitoring fds created */ + PIPE = 4, /* Pipe created */ +}; + +typedef struct ServerThread { + GThread *thread; + IvshmemServer server; + /* + * Pipe is used to communicate with the thread, asking it to terminate on + * receiving 'q'. + */ + int pipe[2]; + /* + * Server statuses are used to keep track of thread/server/pipe start since + * test_ivshmem_server_stop can be called at any time on a test error, + * even from test_ivshmem_server_start itself, therefore, they are used for + * proper service termination. + */ + enum ServerStartStatus status; +} ServerThread; + +gchar *mktempshm(int size, int *fd); +gchar *mktempsocket(void); +void test_ivshmem_server_start(ServerThread *thread, + const char *server_socket_path, + const char *shm_rel_path, unsigned num_vectors); +void test_ivshmem_server_stop(ServerThread *thread); + +#endif /* QTEST_IVSHMEM_UTILS_H */ diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 2b89e8634b..bc6457220c 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -59,9 +59,9 @@ qtests_i386 = \ (config_all_devices.has_key('CONFIG_PVPANIC_ISA') ? ['pvpanic-test'] : []) + \ (config_all_devices.has_key('CONFIG_PVPANIC_PCI') ? ['pvpanic-pci-test'] : []) + \ (config_all_devices.has_key('CONFIG_HDA') ? ['intel-hda-test'] : []) + \ - (config_all_devices.has_key('CONFIG_I82801B11') ? ['i82801b11-test'] : []) + \ + (config_all_devices.has_key('CONFIG_I82801B11') ? ['i82801b11-test'] : []) + \ (config_all_devices.has_key('CONFIG_IOH3420') ? ['ioh3420-test'] : []) + \ - (config_all_devices.has_key('CONFIG_LPC_ICH9') ? ['lpc-ich9-test'] : []) + \ + (config_all_devices.has_key('CONFIG_LPC_ICH9') ? ['lpc-ich9-test'] : []) + \ (config_all_devices.has_key('CONFIG_USB_UHCI') ? ['usb-hcd-uhci-test'] : []) + \ (config_all_devices.has_key('CONFIG_USB_UHCI') and \ config_all_devices.has_key('CONFIG_USB_EHCI') ? ['usb-hcd-ehci-test'] : []) + \ @@ -319,7 +319,7 @@ qtests = { 'cdrom-test': files('boot-sector.c'), 'dbus-vmstate-test': files('migration-helpers.c') + dbus_vmstate1, 'erst-test': files('erst-test.c'), - 'ivshmem-test': [rt, '../../contrib/ivshmem-server/ivshmem-server.c'], + 'ivshmem-test': ['ivshmem-utils.c', '../../contrib/ivshmem-server/ivshmem-server.c'], 'migration-test': migration_files, 'pxe-test': files('boot-sector.c'), 'qos-test': [chardev, io, qos_test_ss.apply({}).sources()], From patchwork Thu Feb 22 22:22:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 774920 Delivered-To: patch@linaro.org Received: by 2002:adf:a356:0:b0:33b:4db1:f5b3 with SMTP id d22csp76853wrb; Thu, 22 Feb 2024 14:25:48 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXDdP4FAYlUoBSGc104SpANOH/kToLR3gRkNrLTIqH3N6LS6ASOdLTEk7Fg5krYFoKfWW+8ADN9kgHKuf4VDsL5 X-Google-Smtp-Source: AGHT+IFtWLsfoxrweBBRf1GbFrOk6/dCAavTJqb4q3oHxul3DZwtZ8OoCFYxLzaO8n/gPUCealfl X-Received: by 2002:a05:620a:1a94:b0:785:c16e:181c with SMTP id bl20-20020a05620a1a9400b00785c16e181cmr787237qkb.10.1708640748710; Thu, 22 Feb 2024 14:25:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708640748; cv=none; d=google.com; s=arc-20160816; b=sCmykt3/V5tKRg6tUO9gRawuBuHmHfiauteKsTuuYrvpgBAhMEUzj31RZXU/51IQoH YSH6x80MPhKRV/IrthrYdsUkGMDrGgDW4PTXqXmvk3AoBOpoLGMX7LZegXc6rvmM5W1X RhIgAPjHEW60jNOVcbzzCGv2bRsNKP1DUUfaTYydvmV6Xp6UqHD92dwj8pvveUya/JgP eBFPRuvBU/bglPTIVii7wGG0aUHQs86eKHOEsu9vxSqILv1mlnpJXBUWpgK7XXQHK+hP +fo+rrkjViwxLrtEMCAaTLdBy5gpgYae4TluFZNeCJydYtQg3KGM6x31EdRwlYt/PyHY V9Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gEU9egN9+/NfXTjj920HZmiXCFvNCQE5eANcITvsGZ4=; fh=FwJ41ag6ReWcl5sLmX4YL9yVV2DrcCZ6yOxRDQfQdJE=; b=ct4kz1CZHnbWIGWvPiS/+rTYsoBanJ2wG1qXHjpSanQ1flZ4uS0yxqfScrWnVs/I4B vqakz4uf7yUC835d/fND43EwwY0KoCnyEhudxmCbj2Nc9eeKlZceqEIvBxmM73ndkdhC WXkVMKmdiArLTvpZGcGEWJTtflWIKnsZV1peKXSWi30/f5+DEGrYxg0ygTSY3+I8H/e/ IJwW0fff/f1XFIYsF/ie0VR+qnzsIkgG19hmaS9+TlnFlZBBD7RYFajuYruQ9xtaUfVm QOV33kKM9Ie6Z3ab/BLQrmoEJFT0aplz2GYI709Fnw3juytIaIj4vhgVXwfPY8LmSyka Qh+A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GFIJ4+XW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id vw22-20020a05620a565600b00787aca2fd12si751911qkn.581.2024.02.22.14.25.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Feb 2024 14:25:48 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GFIJ4+XW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdHUh-0002ht-0f; Thu, 22 Feb 2024 17:24:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rdHUe-0002go-S2 for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:40 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdHUc-0003Dy-Hg for qemu-devel@nongnu.org; Thu, 22 Feb 2024 17:24:40 -0500 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1dc3b4b9b62so2717545ad.1 for ; Thu, 22 Feb 2024 14:24:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708640677; x=1709245477; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gEU9egN9+/NfXTjj920HZmiXCFvNCQE5eANcITvsGZ4=; b=GFIJ4+XWUfjW25jAznwolT4AKarz/4gK4BLV6FokPfabpBX6wcvHWhlKxjoIlMZh2Z O+qcpvlRikRa4XN81JJqa3h57N0pxZorQapD+MJY5Az+meAQ/dHhV4skwBZxthDOxJc4 9twkHF+9VvAwxNEd+G94VxUxABSlLlTxt+/t7h0S2ocMPKExy5OkxEjHXWbYMfPwl8fe gtkiRMYRaL38W7YpxXppi8mMxcuEAHnjx6U55ZJy2Er3IoV4PupusHdy5Afpdi3ADulm E5HWAnrZ5kkiiB1KL4/EWQnh0jcy/j0+4DTODa3eSCkDZIKk03gdMssxKg+SLkq3DqqB BtHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708640677; x=1709245477; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gEU9egN9+/NfXTjj920HZmiXCFvNCQE5eANcITvsGZ4=; b=Qi29ikw9ma3j+h/TFZehvKnCROzNzRmNFHodeGnaRXQdyAjkhU0sZ8pKq1of+J8vTR SZcwbmVnIRyE5x7bfUkbu2Qp6xkNly0Su5m2LioB31M+dDUXwQb44vQhryjwUpkUzLTr 6wACkM/RYzx2cpE235IMR9/0I5naeMZRo5vaoxzGWAggfYqv14kjv3Wo+w7zj/7oz1fz /jFNDCuNTzVpu+c12+zItC3vsrLlYOQ2MlomEetLW83yWtZ9U5MjNg+mNddPhiXU9wVD zYkbrW9lft9d50plvFcO8uTjBBb7Rz/IayaZA89MrVjr2X6XGkmfXJUVrbUvsrQZCEEU tjDg== X-Gm-Message-State: AOJu0Yxfcm816v4clUU25d6SA8OUBVc/rPWsxhC6RDhivJssgmRgsbZn VzPGRjQ9pvKK60VPKZlfopOk5jdadSOc0g9uSOH93A9U38TFRW1hhOUrcEoN3NrqUTvGPle6TPG OCps= X-Received: by 2002:a17:902:e890:b0:1db:e514:1cfd with SMTP id w16-20020a170902e89000b001dbe5141cfdmr518768plg.21.1708640676599; Thu, 22 Feb 2024 14:24:36 -0800 (PST) Received: from amd.. ([2804:7f0:b400:285a:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id ji7-20020a170903324700b001d7057c2fbasm10457210plb.100.2024.02.22.14.24.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 14:24:35 -0800 (PST) From: Gustavo Romero To: qemu-devel@nongnu.org, philmd@linaro.org Cc: thuth@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, alex.bennee@linaro.org, pbonzini@redhat.com, anton.kochkov@proton.me, richard.henderson@linaro.org, peter.maydell@linaro.org, gustavo.romero@linaro.org Subject: [PATCH 6/6] tests/qtest: Add ivshmem-flat test Date: Thu, 22 Feb 2024 22:22:18 +0000 Message-Id: <20240222222218.2261956-7-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222222218.2261956-1-gustavo.romero@linaro.org> References: <20240222222218.2261956-1-gustavo.romero@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=gustavo.romero@linaro.org; helo=mail-pl1-x62e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org This commit adds a qtest for the ivshmem-flat device to test memory sharing, IRQ triggering, and the memory mapped registers in the device. Based-on: https://lists.gnu.org/archive/html/qemu-devel/2023-11/msg03176.html Message-ID: <20231127052024.435743-4-gustavo.romero@linaro.org> Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Gustavo Romero Acked-by: Thomas Huth --- tests/qtest/ivshmem-flat-test.c | 338 ++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 2 files changed, 340 insertions(+) create mode 100644 tests/qtest/ivshmem-flat-test.c diff --git a/tests/qtest/ivshmem-flat-test.c b/tests/qtest/ivshmem-flat-test.c new file mode 100644 index 0000000000..b6f59bba54 --- /dev/null +++ b/tests/qtest/ivshmem-flat-test.c @@ -0,0 +1,338 @@ +/* + * Inter-VM Shared Memory Flat Device qtests + * + * SPDX-FileCopyrightText: 2023 Linaro Ltd. + * SPDX-FileContributor: Gustavo Romero + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ + +#include "ivshmem-utils.h" + +#define IVSHMEM_FLAT_MMR_ADDR 0x400FF000 +#define IVSHMEM_FLAT_SHM_ADDR 0x40100000 +#define SHM_SIZE 131072 /* 128k */ + +static ServerThread thread; + +uint32_t *shm_ptr; +char *shm_rel_path; +char *server_socket_path; + +static void cleanup(void) +{ + if (shm_ptr) { + munmap(shm_ptr, SHM_SIZE); + shm_ptr = NULL; + } + + if (shm_rel_path) { + shm_unlink(shm_rel_path); + shm_rel_path = NULL; + } + + if (server_socket_path) { + unlink(server_socket_path); + server_socket_path = NULL; + } +} + +static void abort_handler(void *data) +{ + test_ivshmem_server_stop(&thread); + cleanup(); +} + +/* + * Check if exactly 1 positive pulse (low->high->low) on 'irq' qtest IRQ line + * happens. N.B.: 'irq' must be intercepted using qtest_irq_intercept_* before + * this function can be used on it. It returns 0 when pulse is detected, + * otherwise 1. + */ +static int test_ivshmem_flat_irq_positive_pulse(QTestState *qts, int irq) +{ + uint64_t num_raises = 0; + uint64_t num_lows = 0; + int attempts = 0; + + while (attempts < 5) { + num_raises = qtest_get_irq_raised_counter(qts, 0); + if (num_raises) { + num_lows = qtest_get_irq_lowered_counter(qts, 0); + /* Check for exactly 1 raise and 1 low IRQ event */ + if (num_raises == num_lows && num_lows == 1) { + return 0; /* Pulse detected */ + } + } + + g_usleep(10000); + attempts++; + } + + g_message("%s: Timeout expired", __func__); + return 1; +} + +static inline uint32_t read_reg(QTestState *qts, enum Reg reg) +{ + uint32_t v; + + qtest_memread(qts, IVSHMEM_FLAT_MMR_ADDR + reg, &v, sizeof(v)); + + return v; +} + +static inline void write_reg(QTestState *qts, enum Reg reg, uint32_t v) +{ + qtest_memwrite(qts, IVSHMEM_FLAT_MMR_ADDR + reg, &v, sizeof(v)); +} + +/* + * Setup a test VM with ivshmem-flat device attached, IRQ properly set, and + * connected to the ivshmem-server. + */ +static QTestState *setup_vm(void) +{ + QTestState *qts; + const char *cmd_line; + + /* + * x-bus-address-{iomem,shmem} are just random addresses that don't conflict + * with any other address in the lm3s6965evb machine. shmem-size used is + * much smaller than the ivshmem server default (4 MiB) to save memory + * resources when testing. + */ + cmd_line = g_strdup_printf("-machine lm3s6965evb " + "-chardev socket,path=%s,id=ivshm " + "-device ivshmem-flat,chardev=ivshm," + "x-irq-qompath='/machine/soc/v7m/nvic/unnamed-gpio-in[0]'," + "x-bus-address-iomem=%#x," + "x-bus-address-shmem=%#x," + "shmem-size=%d", + server_socket_path, + IVSHMEM_FLAT_MMR_ADDR, + IVSHMEM_FLAT_SHM_ADDR, + SHM_SIZE); + + qts = qtest_init(cmd_line); + + return qts; +} + +static void test_ivshmem_flat_irq(void) +{ + QTestState *vm_state; + uint16_t own_id; + + vm_state = setup_vm(); + + qtest_irq_intercept_out_named(vm_state, + "/machine/peripheral-anon/device[0]", + "sysbus-irq"); + + /* IVPOSTION has the device's own ID distributed by the ivshmem-server. */ + own_id = read_reg(vm_state, IVPOSITION); + + /* Make device notify itself. */ + write_reg(vm_state, DOORBELL, (own_id << 16) | 0 /* vector 0 */); + + /* + * Check intercepted device's IRQ output line. 'sysbus-irq' was associated + * to qtest IRQ 0 when intercepted and after self notification qtest IRQ 0 + * must be toggled by the device. The test fails if no toggling is detected. + */ + g_assert(test_ivshmem_flat_irq_positive_pulse(vm_state, + 0 /* qtest IRQ */) == 0); + + qtest_quit(vm_state); +} + +static void test_ivshmem_flat_shm_write(void) +{ + QTestState *vm_state; + int num_elements, i; + uint32_t *data; + + vm_state = setup_vm(); + + /* Prepare test data with random values. */ + data = g_malloc(SHM_SIZE); + num_elements = SHM_SIZE / sizeof(*data); + for (i = 0; i < num_elements; i++) { + data[i] = g_test_rand_int(); + } + + /* + * Write test data to VM address IVSHMEM_FLAT_SHM_ADDR, where the shared + * memory region is located. + */ + qtest_memwrite(vm_state, IVSHMEM_FLAT_SHM_ADDR, data, SHM_SIZE); + + /* + * Since the shared memory fd is mmapped into this test process VMA at + * shm_ptr, every byte written by the VM in its shared memory region should + * also be available in the test process via shm_ptr. Thus, data in shm_ptr + * is compared back against the original test data. + */ + for (i = 0; i < num_elements; i++) { + g_assert_cmpint(shm_ptr[i], ==, data[i]); + } + + qtest_quit(vm_state); +} + +static void test_ivshmem_flat_shm_read(void) +{ + QTestState *vm_state; + int num_elements, i; + uint32_t *data; + uint32_t v; + + vm_state = setup_vm(); + + /* Prepare test data with random values. */ + data = g_malloc(SHM_SIZE); + num_elements = SHM_SIZE / sizeof(*data); + for (i = 0; i < num_elements; i++) { + data[i] = g_test_rand_int(); + } + + /* + * Copy test data to the shared memory region so it can be read from the VM + * (IVSHMEM_FLAT_SHM_ADDR location). + */ + memcpy(shm_ptr, data, SHM_SIZE); + + /* Check data */ + for (i = 0; i < num_elements; i++) { + qtest_memread(vm_state, IVSHMEM_FLAT_SHM_ADDR + i * sizeof(v), &v, + sizeof(v)); + g_assert_cmpint(v, ==, data[i]); + } + + qtest_quit(vm_state); +} + +static void test_ivshmem_flat_shm_pair(void) +{ + QTestState *vm0_state, *vm1_state; + uint16_t vm0_peer_id, vm1_peer_id; + int num_elements, i; + uint32_t *data; + uint32_t v; + + vm0_state = setup_vm(); + vm1_state = setup_vm(); + + /* Get peer ID for the VM so it can be used for one notify each other. */ + vm0_peer_id = read_reg(vm0_state, IVPOSITION); + vm1_peer_id = read_reg(vm1_state, IVPOSITION); + + /* Observe vm1 IRQ output line first. */ + qtest_irq_intercept_out_named(vm1_state, + "/machine/peripheral-anon/device[0]", + "sysbus-irq"); + + /* Notify (interrupt) VM1 from VM0. */ + write_reg(vm0_state, DOORBELL, (vm1_peer_id << 16) | 0 /* vector 0 */); + + /* Check if VM1 IRQ output line is toggled after notification from VM0. */ + g_assert(test_ivshmem_flat_irq_positive_pulse(vm1_state, + 0 /* qtest IRQ */) == 0); + + /* Secondly, observe VM0 IRQ output line first. */ + qtest_irq_intercept_out_named(vm0_state, + "/machine/peripheral-anon/device[0]", + "sysbus-irq"); + + /* ... and do the opposite: notify (interrupt) VM0 from VM1. */ + write_reg(vm1_state, DOORBELL, (vm0_peer_id << 16) | 0 /* vector 0 */); + + /* Check if VM0 IRQ output line is toggled after notification from VM0. */ + g_assert(test_ivshmem_flat_irq_positive_pulse(vm0_state, + 0 /* qtest IRQ */) == 0); + + /* Prepare test data with random values. */ + data = g_malloc(SHM_SIZE); + num_elements = SHM_SIZE / sizeof(*data); + for (i = 0; i < num_elements; i++) { + data[i] = g_test_rand_int(); + } + + /* Write test data on VM0. */ + qtest_memwrite(vm0_state, IVSHMEM_FLAT_SHM_ADDR, data, SHM_SIZE); + + /* Check test data on VM1. */ + for (i = 0; i < num_elements; i++) { + qtest_memread(vm1_state, IVSHMEM_FLAT_SHM_ADDR + i * sizeof(v), &v, + sizeof(v)); + g_assert_cmpint(v, ==, data[i]); + } + + /* Prepare new test data with random values. */ + for (i = 0; i < num_elements; i++) { + data[i] = g_test_rand_int(); + } + + /* Write test data on VM1. */ + qtest_memwrite(vm1_state, IVSHMEM_FLAT_SHM_ADDR, data, SHM_SIZE); + + /* Check test data on VM0. */ + for (i = 0; i < num_elements; i++) { + qtest_memread(vm0_state, IVSHMEM_FLAT_SHM_ADDR + i * sizeof(v), &v, + sizeof(v)); + g_assert_cmpint(v, ==, data[i]); + } + + qtest_quit(vm0_state); + qtest_quit(vm1_state); +} + +int main(int argc, char *argv[]) +{ + int shm_fd, r; + + g_test_init(&argc, &argv, NULL); + + if (!qtest_has_machine("lm3s6965evb")) { + g_test_skip("Machine Stellaris (lm3s6965evb) not found, " + "skipping ivshmem-flat device test."); + return 0; + } + + /* If test fails, stop server, cleanup socket and shm files. */ + qtest_add_abrt_handler(abort_handler, NULL); + + shm_rel_path = mktempshm(SHM_SIZE, &shm_fd); + g_assert(shm_rel_path); + + /* + * Map shm to this test's VMA so it's possible to read/write from/to it. For + * VMs with the ivhsmem-flat device attached, this region will also be + * mapped in their own memory layout, at IVSHMEM_FLAT_SHM_ADDR (default). + */ + shm_ptr = mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + g_assert(shm_ptr != MAP_FAILED); + + server_socket_path = mktempsocket(); + /* It never fails, so no assert(). */ + + /* + * Currently, ivshmem-flat device only supports notification via 1 vector, + * i.e. vector 0. + */ + test_ivshmem_server_start(&thread, server_socket_path, shm_rel_path, 1); + + /* Register tests. */ + qtest_add_func("/ivshmem-flat/irq", test_ivshmem_flat_irq); + qtest_add_func("/ivshmem-flat/shm-write", test_ivshmem_flat_shm_write); + qtest_add_func("/ivshmem-flat/shm-read", test_ivshmem_flat_shm_read); + qtest_add_func("/ivshmem-flat/pair", test_ivshmem_flat_shm_pair); + + r = g_test_run(); + + test_ivshmem_server_stop(&thread); + cleanup(); + + return r; +} diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index bc6457220c..c0468bc6e0 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -205,6 +205,7 @@ qtests_stm32l4x5 = \ 'stm32l4x5_syscfg-test'] qtests_arm = \ + (config_all_devices.has_key('CONFIG_IVSHMEM_FLAT_DEVICE') ? ['ivshmem-flat-test'] : []) + \ (config_all_devices.has_key('CONFIG_MPS2') ? ['sse-timer-test'] : []) + \ (config_all_devices.has_key('CONFIG_CMSDK_APB_DUALTIMER') ? ['cmsdk-apb-dualtimer-test'] : []) + \ (config_all_devices.has_key('CONFIG_CMSDK_APB_TIMER') ? ['cmsdk-apb-timer-test'] : []) + \ @@ -320,6 +321,7 @@ qtests = { 'dbus-vmstate-test': files('migration-helpers.c') + dbus_vmstate1, 'erst-test': files('erst-test.c'), 'ivshmem-test': ['ivshmem-utils.c', '../../contrib/ivshmem-server/ivshmem-server.c'], + 'ivshmem-flat-test': ['ivshmem-utils.c', '../../contrib/ivshmem-server/ivshmem-server.c'], 'migration-test': migration_files, 'pxe-test': files('boot-sector.c'), 'qos-test': [chardev, io, qos_test_ss.apply({}).sources()],