From patchwork Mon May 26 18:05:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 892624 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e53:0:b0:3a3:61c9:c5d4 with SMTP id j19csp1333282wrz; Mon, 26 May 2025 11:07:24 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVeoJwY+/lwb2PWs57mHRADTNv/nxU+Nt4ToDMTuh+P0YvngJWc8IASTQ4XgshTcds3RoEOLg==@linaro.org X-Google-Smtp-Source: AGHT+IHpHBoiGIc2dAPd4qOI1RF7w3rjfAFNrbHou3NXy+DrzLc0fBYEX87gb5LejQrnIu2xfYnE X-Received: by 2002:a05:620a:c53:b0:7ca:cd71:2bf8 with SMTP id af79cd13be357-7ceecc902edmr1795329985a.54.1748282834230; Mon, 26 May 2025 11:07:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1748282834; cv=none; d=google.com; s=arc-20240605; b=E4xtyrY3CyBAcofTDuPdtW4oJkrwU+ByS+m8S5tuPBIZ2S0+pjrB9KJNszDwjT4vYk HOMGbK9BNicFiRQUnfhpr1Ts/3BHODQwYJX2eDwyZzYw9U7vQhe9bqfAk3ssAMtBZg/Z 5SN6oJDvtbPYuuQmihtLF8QbSMMXjwb9zlA3vdKkV2ChBRJg083k5P8bdCgz2K26+Vfs c5J2djMpvkFSXZg0bBEsgy8vol8Zs6UrzDMdhmmCl6wfsHuNWBiIggpOlVd/LcWvgCUC ek0syDids2/SfhxIMznxRH2tFceYfaqkINqJes9GbmikqqkXYzuUlskBwbdz2owMef/h NZzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=PK/3BMMw+rPIcEuLbLBqAtKj1UfFQt8uO08FuIUDDYU=; fh=sNbyXvGZ3H/PhxvVZ5gltDMeFC820XEETh9JUbyAxFY=; b=ErO+34VMpZ81OQoIJZVKy7OU2/lTa6i4wFwQCfDQFZBIuGarGQKwP4NoqB4WZqgt5l rqAEnThFdbmUo7O+TepdBzW9zI/XT8uA1+xyk35lMUK+wXWHMAU0egDkFv8TczdBlXHr tKgsW5cmbbYo5ydIkyzVsxwsIJLp19KfRo77gakYZYSy+rIwIzCq2UP92TaDlr1JVlfq 890uMoEQheed+2eki0WKm9bzGAxSrojBG/Fr8mMuOLfiX1HX++QveqC7Z5LWVaMcpHZb FQAiYs3K/vE4jC0DQ36k+uvG25rf0NzgdGvv6c/MUk2xAcJCQrbWAMHGdN8ur4IXN9BW MqZA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WX5uySL2; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7cd4688925fsi2400677085a.327.2025.05.26.11.07.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 26 May 2025 11:07:14 -0700 (PDT) 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=WX5uySL2; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJcDd-0003H1-9F; Mon, 26 May 2025 14:06:38 -0400 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 1uJcDF-00036e-Eg for qemu-devel@nongnu.org; Mon, 26 May 2025 14:06:14 -0400 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJcDD-0004pq-I6 for qemu-devel@nongnu.org; Mon, 26 May 2025 14:06:13 -0400 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-43cfba466b2so33095665e9.3 for ; Mon, 26 May 2025 11:06:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1748282770; x=1748887570; 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=PK/3BMMw+rPIcEuLbLBqAtKj1UfFQt8uO08FuIUDDYU=; b=WX5uySL2yP1hyPrlTDYUz2KpXgw21GQ4jBsaaxabLmSsZOvAFzqNZvL9AemyKnuiIg rLrR39zpaFma5+pq93atf6qZPOTc4G/5hSm2LqW9timAoZ2WBPafNz12w20/i4QGJeg1 LWLNyPBnFfPtHoM6h+vo2GsMLDffqho3Qj5Tbe79tIeK9PWB/BIw0dK40YDirhs4WBOu DiicblP6t0h+1DxGyWOVBr7TTPU9gZFVohO2Huv5sqrgCn9KHDcYIxGd0rX8/9AVBPAV Ldc+ioinmZ4IMrA5XXHX7fLbWGebH3+wY6O9M9kPJEJ4VK5Z2i3uI8pvouPicEHOaO3V NJ0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748282770; x=1748887570; 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=PK/3BMMw+rPIcEuLbLBqAtKj1UfFQt8uO08FuIUDDYU=; b=aBZWL1GkGr3VD/4IbrMCCIn1iVv57ev7RQ9HlmekAgk9cse7FW4fw4x3GXC7H2X+xt C/Ys7UxBgS1HmAqt3shZLYJMn8XphIphWSnYFEyeyoQdJxo2XRliyvcHfUaUYSUg27CW k9sAYQJnluRfaExSOlJVMBkwX1NC0LhpxZXQJTRgVNdAYSHNCbcLF0TDN6sNmee7ijEt +6c3B8YlP8WHIWHi1UCVxI6tcSqKhohnHuSTDnErqEujYOQ0IzaOlYJ7BCiAYfnXzHlB uaFPeWF0EcttzMy56e1ISfHbwN09cZ5N4hoFJcCGLB/OHZV6WBmGB92CP3q5g2uEUabA N2FA== X-Gm-Message-State: AOJu0Yy17OSIbQq1UXB/7zqH0Y6W76c50DPho4HelDSeWgqMFSGHnx6x Eej2sr0m4MM8g8qfmpNXX6Y6CopjLLHOm1f7rnKKaUoxFqi1ncdEWje4shi5Vkl1XRXgqGpAVYM UzgHwdxQ= X-Gm-Gg: ASbGnctdJhd0zv67JQ+2WPkWFnLRo8jwTVEEFF+Fy3EqhF0Rj9JL+O7iSbKoFOFISf5 XyrnIxaX+/9zDGa51KIZjbXLXBCMcyjFhmqw5KsXkCa7sNNwWTYeylDhFdFn09EY4WOAUO90CiZ xnJwadgYiLGuOBOs+U2eRM6W7WulZ61h5R73IHGOF/6cy8obt+EfjBRYjC0aYJXSmdMhpGeFBMM z5xD1DCG5mAuQKMjyiSArxkoVcjBfuiOTsnuX89wCAG5irPyaDIey1fIPIvhhYxXp5QchMmkfFN 23jY6MfHcbszcLDTUjbS/AQiWeD5KI4xYjlNy46Risms5Xz2Ygy0aM1YHXLcgI8jFgEQcHSKIeL XvBPWe93zHAA1QDeIWkjk5aJs1RGGZWc= X-Received: by 2002:a05:600c:4fd6:b0:43d:b85:1831 with SMTP id 5b1f17b1804b1-44c91bce747mr94158155e9.0.1748282769815; Mon, 26 May 2025 11:06:09 -0700 (PDT) Received: from localhost.localdomain (88-187-86-199.subs.proxad.net. [88.187.86.199]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a4dc1172ecsm2684499f8f.48.2025.05.26.11.06.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 26 May 2025 11:06:09 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: John Snow , Gerd Hoffmann , Jiaxun Yang , Laurent Vivier , qemu-block@nongnu.org, Fabiano Rosas , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [RFC PATCH 2/2] hw/ide/ahci: Delay a bit before completing reset Date: Mon, 26 May 2025 20:05:58 +0200 Message-ID: <20250526180558.65613-3-philmd@linaro.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250526180558.65613-1-philmd@linaro.org> References: <20250526180558.65613-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::329; envelope-from=philmd@linaro.org; helo=mail-wm1-x329.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 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 Give few milliseconds to (emulated) hardware to complete its reset sequence. The intent is to have this model better match hardware, reducing firmware bugs "works in QEMU but not in real world" such https://github.com/FlyGoat/csmwrap/issues/14. Reported-by: Jiaxun Yang Signed-off-by: Philippe Mathieu-Daudé --- include/hw/ide/ahci.h | 1 + hw/ide/ahci.c | 39 +++++++++++++++++++++++++++++++++++++-- tests/qtest/ahci-test.c | 4 ++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/include/hw/ide/ahci.h b/include/hw/ide/ahci.h index cd07b87811b..82617835195 100644 --- a/include/hw/ide/ahci.h +++ b/include/hw/ide/ahci.h @@ -46,6 +46,7 @@ typedef struct AHCIState { uint32_t ports; qemu_irq irq; AddressSpace *as; + QEMUTimer *reset_timer; } AHCIState; diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index 7e586c7a0a4..cf1c5d3f3db 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -28,6 +28,7 @@ #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/main-loop.h" +#include "qemu/timer.h" #include "system/block-backend.h" #include "system/dma.h" #include "ahci-internal.h" @@ -47,6 +48,7 @@ static bool ahci_map_fis_address(AHCIDevice *ad); static void ahci_unmap_clb_address(AHCIDevice *ad); static void ahci_unmap_fis_address(AHCIDevice *ad); static void ahci_reset_delayed(AHCIState *s, bool immediate); +static void ahci_reset_complete(void *opaque); static const char *AHCIHostReg_lookup[AHCI_HOST_REG__COUNT] = { [AHCI_HOST_REG_CAP] = "CAP", @@ -459,7 +461,7 @@ static void ahci_mem_write(void *opaque, hwaddr addr, break; case AHCI_HOST_REG_CTL: /* R/W */ if (val & HOST_CTL_RESET) { - ahci_reset(s); + ahci_reset_delayed(s, true); } else { s->control_regs.ghc = (val & 0x3) | HOST_CTL_AHCI_EN; ahci_check_irq(s); @@ -1591,6 +1593,7 @@ void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as) s->as = as; assert(s->ports > 0); s->dev = g_new0(AHCIDevice, s->ports); + s->reset_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, ahci_reset_complete, s); ahci_reg_init(s); irqs = qemu_allocate_irqs(ahci_irq_set, s, s->ports); for (i = 0; i < s->ports; i++) { @@ -1622,8 +1625,12 @@ void ahci_uninit(AHCIState *s) } g_free(s->dev); + + timer_free(s->reset_timer); } +#define AHCI_RESET_DELAY_MS 69 /* Arbitrary value less than 500 ms */ + static void ahci_reset_complete(void *opaque) { AHCIState *s = opaque; @@ -1667,7 +1674,11 @@ static void ahci_reset_delayed(AHCIState *s, bool immediate) } if (immediate) { + timer_del(s->reset_timer); ahci_reset_complete(s); + } else if (!timer_pending(s->reset_timer)) { + timer_mod(s->reset_timer, + qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + AHCI_RESET_DELAY_MS); } } @@ -1676,6 +1687,24 @@ void ahci_reset(AHCIState *s) ahci_reset_delayed(s, false); } +static bool ahci_timer_needed(void *opaque) +{ + AHCIState *s = opaque; + + return timer_pending(s->reset_timer); +} + +static const VMStateDescription vmstate_ahci_timer = { + .name = "ahci/reset_timer", + .version_id = 1, + .minimum_version_id = 1, + .needed = ahci_timer_needed, + .fields = (const VMStateField[]) { + VMSTATE_TIMER_PTR(reset_timer, AHCIState), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_ncq_tfs = { .name = "ncq state", .version_id = 1, @@ -1734,7 +1763,9 @@ static int ahci_state_post_load(void *opaque, int version_id) ad = &s->dev[i]; pr = &ad->port_regs; - if (!(pr->cmd & PORT_CMD_START) && (pr->cmd & PORT_CMD_LIST_ON)) { + if (timer_pending(s->reset_timer)) { + /* Reset in progress */ + } else if (!(pr->cmd & PORT_CMD_START) && (pr->cmd & PORT_CMD_LIST_ON)) { error_report("AHCI: DMA engine should be off, but status bit " "indicates it is still running."); return -1; @@ -1823,6 +1854,10 @@ const VMStateDescription vmstate_ahci = { VMSTATE_UINT32_EQUAL(ports, AHCIState, NULL), VMSTATE_END_OF_LIST() }, + .subsections = (const VMStateDescription * const []) { + &vmstate_ahci_timer, + NULL + } }; void ahci_ide_create_devs(AHCIState *ahci, DriveInfo **hd) diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c index e8aabfc13f5..4389aaa7f70 100644 --- a/tests/qtest/ahci-test.c +++ b/tests/qtest/ahci-test.c @@ -40,6 +40,8 @@ #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024) #define TEST_IMAGE_SIZE_MB_SMALL 64 +#define AHCI_RESET_TIMEOUT_NS (500 * 1000 * 1000) + /*** Globals ***/ static char *tmp_path; static char *debug_path; @@ -483,6 +485,8 @@ static void ahci_test_hba_spec(AHCIQState *ahci) g_assert(ahci != NULL); + qtest_clock_step(ahci->parent->qts, AHCI_RESET_TIMEOUT_NS); + /* * Note that the AHCI spec does expect the BIOS to set up a few things: * CAP.SSS - Support for staggered spin-up (t/f)