From nobody Tue Nov 26 20:28:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1705418241; cv=none; d=zohomail.com; s=zohoarc; b=EAzSILvY0vruI4Tb32XlcVQ4GPYbAuuLBPfNfMHxrYigRcRIsVpzNKUJxma/e4w39O0OYZ5uY5Yro1p5L2aRvdsUH/w0wGK/7soz2M9McQ+87JKXeIxXLkBElkGoWDONN9izHq9FTR70LFmV7CJJX8VpXzwD1U7i0ZIG2dWWCZY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705418241; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=tLJo5v2421ryPOioIx5dkKcmsBZ1hWcqvO+bf2sNHwo=; b=mVrJ6dkPmgou6FrOMF/W3vMz4TNEeWfkYxUYwnOiyG/RR9WLsVYTEcJOv3PSBL5cLfZXejzza/pU7aVPksZNZbQUX2O8Ea4g7evbPcUsV5tYdxjD6OqagDaFY3Ff58+HEBWFb/Xc/ON0JOztns+kuWGzmWQkzrAiV7OBspmEZp0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1705418241793597.3618111116184; Tue, 16 Jan 2024 07:17:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rPl7K-0006g2-B2; Tue, 16 Jan 2024 10:12:42 -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 1rPl7H-0006dh-1A for qemu-devel@nongnu.org; Tue, 16 Jan 2024 10:12:39 -0500 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rPl7D-0005qT-Ua for qemu-devel@nongnu.org; Tue, 16 Jan 2024 10:12:38 -0500 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3376ead25e1so7847424f8f.3 for ; Tue, 16 Jan 2024 07:12:35 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id m14-20020adff38e000000b003379b549a00sm10091357wro.10.2024.01.16.07.12.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 07:12:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1705417954; x=1706022754; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=tLJo5v2421ryPOioIx5dkKcmsBZ1hWcqvO+bf2sNHwo=; b=CaXsMpeowoDkBugnUgWSapg4djHbT+Dzl7gWdt56okO/Bw7q6rY7eC2t3ZYLEuTXAZ 4VMho9GA6ozuP8j2JLW5X/Zp8379R5Yv3f0Bv3FPnAiBDWYvQaSnM+iO65ZBwB3WWU9o ZYzlE5UWeRhW18yz8MuBr50I4FqNH0rGmYoZYYV1NUM+CW+CmJ4H7j9QjrvwVvCFVnsi O31Bl185pOdCx5kZC3gZ6QCVw7C1FgZHEU/mgobtzaoNEbccFHEPz4vbQnnDwcuEQV37 q4cH6SuASzScl5phdtbpH4375wA7aTsaX3G68UFVtp21CkhITrRU9YjeCeHDy6SX0Owc HPtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705417954; x=1706022754; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tLJo5v2421ryPOioIx5dkKcmsBZ1hWcqvO+bf2sNHwo=; b=r/9IThdsWkX2LLmY6tF/3urW5q5IngZzdMfzsoQBDb2VAHEq9N5ykplk1doxC4PUhE d3l4RmwBVi77yXpLfy7oXMwxKI6Y41MCcr5hXo/awXTxr2ygbGkmiYcfXITrqP6MoItw 4iNUOdi3L81Q0nj5EmMOaruRLMMSp26DvyJkqJZLzIki6PKX1Ad0TDfQu2G4heiN0GTF RmmEKc88Qr2CG1DDEHwrHtTE4lMEOMkw/VBHNDJdqWU3OEHHU7QDU3tdZijoufebomWY edNzw22CXGmpgsQrRvLOUJjXCqZJe9uWWr1vuOftA4k1pgDVyAAPs5zh4kvjpMW+bUoK sdcw== X-Gm-Message-State: AOJu0YzGG+cM9vqyopUmjmOeEjWw2kPz24VJaNmQo5osupRW3a2gmxAl Yz7Z38Axto4v6lR9EfmHBRyfo027BBDxxixUK1uP8yRvD0Y= X-Google-Smtp-Source: AGHT+IEUAvVDk5LasLUxE5ccl2CCxkBtrvo9NoCmcHkT8d9BH0EKrpgqOKut2j6e92dke29paLmi9A== X-Received: by 2002:a05:6000:1882:b0:337:a21e:7731 with SMTP id a2-20020a056000188200b00337a21e7731mr3047264wri.22.1705417954286; Tue, 16 Jan 2024 07:12:34 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 12/21] hw/misc: Add qtest for NPCM7xx PCI Mailbox Date: Tue, 16 Jan 2024 15:12:19 +0000 Message-Id: <20240116151228.2430754-13-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240116151228.2430754-1-peter.maydell@linaro.org> References: <20240116151228.2430754-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::434; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x434.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=ham 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1705418244033100003 Content-Type: text/plain; charset="utf-8" From: Hao Wu This patches adds a qtest for NPCM7XX PCI Mailbox module. It sends read and write requests to the module, and verifies that the module contains the correct data after the requests. Change-Id: I2e1dbaecf8be9ec7eab55cb54f7fdeb0715b8275 Signed-off-by: Hao Wu Signed-off-by: Nabih Estefan Reviewed-by: Tyrone Ting Message-id: 20240110234232.4116804-4-nabihestefan@google.com [PMM: avoid in_port_t type as it is not present on Windows] Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- tests/qtest/npcm7xx_pci_mbox-test.c | 238 ++++++++++++++++++++++++++++ tests/qtest/meson.build | 1 + 2 files changed, 239 insertions(+) create mode 100644 tests/qtest/npcm7xx_pci_mbox-test.c diff --git a/tests/qtest/npcm7xx_pci_mbox-test.c b/tests/qtest/npcm7xx_pci_= mbox-test.c new file mode 100644 index 00000000000..341642e6371 --- /dev/null +++ b/tests/qtest/npcm7xx_pci_mbox-test.c @@ -0,0 +1,238 @@ +/* + * QTests for Nuvoton NPCM7xx PCI Mailbox Modules. + * + * Copyright 2021 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qnum.h" +#include "libqtest-single.h" + +#define PCI_MBOX_BA 0xf0848000 +#define PCI_MBOX_IRQ 8 + +/* register offset */ +#define PCI_MBOX_STAT 0x00 +#define PCI_MBOX_CTL 0x04 +#define PCI_MBOX_CMD 0x08 + +#define CODE_OK 0x00 +#define CODE_INVALID_OP 0xa0 +#define CODE_INVALID_SIZE 0xa1 +#define CODE_ERROR 0xff + +#define OP_READ 0x01 +#define OP_WRITE 0x02 +#define OP_INVALID 0x41 + + +static int sock; +static int fd; + +/* + * Create a local TCP socket with any port, then save off the port we got. + */ +static int open_socket(void) +{ + struct sockaddr_in myaddr; + socklen_t addrlen; + + myaddr.sin_family =3D AF_INET; + myaddr.sin_addr.s_addr =3D htonl(INADDR_LOOPBACK); + myaddr.sin_port =3D 0; + sock =3D socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + g_assert(sock !=3D -1); + g_assert(bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) !=3D = -1); + addrlen =3D sizeof(myaddr); + g_assert(getsockname(sock, (struct sockaddr *) &myaddr , &addrlen) != =3D -1); + g_assert(listen(sock, 1) !=3D -1); + return ntohs(myaddr.sin_port); +} + +static void setup_fd(void) +{ + fd_set readfds; + + FD_ZERO(&readfds); + FD_SET(sock, &readfds); + g_assert(select(sock + 1, &readfds, NULL, NULL, NULL) =3D=3D 1); + + fd =3D accept(sock, NULL, 0); + g_assert(fd >=3D 0); +} + +static uint8_t read_response(uint8_t *buf, size_t len) +{ + uint8_t code; + ssize_t ret =3D read(fd, &code, 1); + + if (ret =3D=3D -1) { + return CODE_ERROR; + } + if (code !=3D CODE_OK) { + return code; + } + g_test_message("response code: %x", code); + if (len > 0) { + ret =3D read(fd, buf, len); + if (ret < len) { + return CODE_ERROR; + } + } + return CODE_OK; +} + +static void receive_data(uint64_t offset, uint8_t *buf, size_t len) +{ + uint8_t op =3D OP_READ; + uint8_t code; + ssize_t rv; + + while (len > 0) { + uint8_t size; + + if (len >=3D 8) { + size =3D 8; + } else if (len >=3D 4) { + size =3D 4; + } else if (len >=3D 2) { + size =3D 2; + } else { + size =3D 1; + } + + g_test_message("receiving %u bytes", size); + /* Write op */ + rv =3D write(fd, &op, 1); + g_assert_cmpint(rv, =3D=3D, 1); + /* Write offset */ + rv =3D write(fd, (uint8_t *)&offset, sizeof(uint64_t)); + g_assert_cmpint(rv, =3D=3D, sizeof(uint64_t)); + /* Write size */ + g_assert_cmpint(write(fd, &size, 1), =3D=3D, 1); + + /* Read data and Expect response */ + code =3D read_response(buf, size); + g_assert_cmphex(code, =3D=3D, CODE_OK); + + buf +=3D size; + offset +=3D size; + len -=3D size; + } +} + +static void send_data(uint64_t offset, const uint8_t *buf, size_t len) +{ + uint8_t op =3D OP_WRITE; + uint8_t code; + ssize_t rv; + + while (len > 0) { + uint8_t size; + + if (len >=3D 8) { + size =3D 8; + } else if (len >=3D 4) { + size =3D 4; + } else if (len >=3D 2) { + size =3D 2; + } else { + size =3D 1; + } + + g_test_message("sending %u bytes", size); + /* Write op */ + rv =3D write(fd, &op, 1); + g_assert_cmpint(rv, =3D=3D, 1); + /* Write offset */ + rv =3D write(fd, (uint8_t *)&offset, sizeof(uint64_t)); + g_assert_cmpint(rv, =3D=3D, sizeof(uint64_t)); + /* Write size */ + g_assert_cmpint(write(fd, &size, 1), =3D=3D, 1); + /* Write data */ + g_assert_cmpint(write(fd, buf, size), =3D=3D, size); + + /* Expect response */ + code =3D read_response(NULL, 0); + g_assert_cmphex(code, =3D=3D, CODE_OK); + + buf +=3D size; + offset +=3D size; + len -=3D size; + } +} + +static void test_invalid_op(void) +{ + uint8_t op =3D OP_INVALID; + uint8_t code; + uint8_t buf[1]; + + g_assert_cmpint(write(fd, &op, 1), =3D=3D, 1); + code =3D read_response(buf, 1); + g_assert_cmphex(code, =3D=3D, CODE_INVALID_OP); +} + +/* Send data via chardev and read them in guest. */ +static void test_guest_read(void) +{ + const char *data =3D "Hello World!"; + uint64_t offset =3D 0xa0; + char buf[100]; + size_t len =3D strlen(data); + + send_data(offset, (uint8_t *)data, len); + memread(PCI_MBOX_BA + offset, buf, len); + g_assert_cmpint(strncmp(data, buf, len), =3D=3D, 0); +} + +/* Write data in guest and read out via chardev. */ +static void test_guest_write(void) +{ + const char *data =3D "Hello World!"; + uint64_t offset =3D 0xa0; + char buf[100]; + size_t len =3D strlen(data); + + memwrite(PCI_MBOX_BA + offset, data, len); + receive_data(offset, (uint8_t *)buf, len); + g_assert_cmpint(strncmp(data, buf, len), =3D=3D, 0); +} + +int main(int argc, char **argv) +{ + int ret; + int port; + + g_test_init(&argc, &argv, NULL); + port =3D open_socket(); + g_test_message("port=3D%d", port); + global_qtest =3D qtest_initf("-machine npcm750-evb " + "-chardev socket,id=3Dnpcm7xx-pcimbox-chr,host=3Dlocalhost," + "port=3D%d,reconnect=3D10 " + "-global driver=3Dnpcm7xx-pci-mbox,property=3Dchardev," + "value=3Dnpcm7xx-pcimbox-chr", + port); + setup_fd(); + qtest_irq_intercept_in(global_qtest, "/machine/soc/a9mpcore/gic"); + + qtest_add_func("/npcm7xx_pci_mbox/invalid_op", test_invalid_op); + qtest_add_func("/npcm7xx_pci_mbox/read", test_guest_read); + qtest_add_func("/npcm7xx_pci_mbox/write", test_guest_write); + ret =3D g_test_run(); + qtest_quit(global_qtest); + + return ret; +} diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 5e4bc3da85e..464eb4c6845 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -184,6 +184,7 @@ qtests_sparc64 =3D \ qtests_npcm7xx =3D \ ['npcm7xx_adc-test', 'npcm7xx_gpio-test', + 'npcm7xx_pci_mbox-test', 'npcm7xx_pwm-test', 'npcm7xx_rng-test', 'npcm7xx_sdhci-test', --=20 2.34.1