From patchwork Mon Jan 8 12:53:41 2024 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: 760763 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp867784wrz; Mon, 8 Jan 2024 04:54:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IEFCQthQ9ZMkdO3xjc5GeffYJ/CyacEYWYUNl4ieAfb2dEdCTIi3g7rjDW35fiiANFOcgkT X-Received: by 2002:a05:622a:14ca:b0:428:3bee:ea2d with SMTP id u10-20020a05622a14ca00b004283beeea2dmr5379453qtx.96.1704718481097; Mon, 08 Jan 2024 04:54:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704718481; cv=none; d=google.com; s=arc-20160816; b=aMYeNL8JHHUTKUUxG3z/FEEy2AjH7gek3tlzndH3T5P6zcReGx/5Fs0rhy7StPUcAa kheAFe3DonTDVKoLfg/wJGh1o6Ajn81DwabrkHbJstte+TgcxEWRdmX60/+XGkTlzKQC DjSTcumVpR/drR6paxjB+2BkAJrly6J260PCDJ+91DcdpmG3WcNv2+rWI3i+jbMK2O8i BSwSWWnw7ajmWGLRqxJuluIxWsPopH+0dfIajjFlWfozcE9gtVyXPeR9MhbLIWreKiGW JlrG46JkiikeBPo8qrgLRJmYi/XWF0gs00Kj5CBb6ZFSby4GisBDX6P+FmVmVicdNLh7 Q8TQ== 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=ghVWFLUr6713RIktIzJDLwjlFUwvU8dITI3EJerUt6E=; fh=ot/AHU6TwUyk96ZCKy1yVPJbrRHdtPsmBCo02SyURJs=; b=pNMfg/N0zUpMuM6O3+MSHVr9xYE3t4hgbK70KSaGeh/mTbNgcnY8SncMYpN0YzCmw3 HmYkA33EvRi8RHHSvV7X0icfmD+plhQzPezsg94+CLVsucavwp96nLOZ3yL8A5UTgXt6 gH9Nn7deAY0gY033UzUQT1iU3UicLqtHNHIJ+JxeCh+sGokjSiWEyQD8Qi+n5inNd+r/ n8t72yhTu5mJ51Zbbo2LskVvreRWtgGFFMiZoIjLJHKlRlEyNKRzIesHFnjvCSDWcWSp oKzJk5sS/7S4MD4EtA02pnWRX07G2aEaLzLvl/yzs3193GNiHlKN+3fNKdHlTORs62gk 8Siw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cE7lqjDI; 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 o15-20020a05622a044f00b0042753fdb817si7785504qtx.449.2024.01.08.04.54.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Jan 2024 04:54:41 -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=cE7lqjDI; 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 1rMp8d-0002tw-Ca; Mon, 08 Jan 2024 07:53:55 -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 1rMp8c-0002tR-2J for qemu-devel@nongnu.org; Mon, 08 Jan 2024 07:53:54 -0500 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rMp8a-0006H4-Ef for qemu-devel@nongnu.org; Mon, 08 Jan 2024 07:53:53 -0500 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-40e43e48a16so13298135e9.2 for ; Mon, 08 Jan 2024 04:53:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704718431; x=1705323231; 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=ghVWFLUr6713RIktIzJDLwjlFUwvU8dITI3EJerUt6E=; b=cE7lqjDIiL85o7NwwIR0GAfIWwo31IejvK/kRERHRmSjrzksbF+toE2N+a4j370YKg Qdd+tOKyfFe7rcTYNXnk97z2S2oyX5nL+akdA+Z6Rw1AgyqBhBGeLc8//anhHjdhi+WY jW7v4R93E0Y+4ReyGen5N9gpnfF9LAJzPN6jhuvGtkxi3pS6F9dtKJwoshfvniEj31QX IXITi/byUXRWi8M5yDhNEuYQnJP4qSmmQPt2nGwgUo7wf6YkQU86n6ECnxylFZ459Q9a iEVChatIC3RuNKLpHYJNQ+BiPGzQ16BBdZP2uLhFNs+hLmDzhHnGkO2m8Kb2ntMFCbRF gGSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704718431; x=1705323231; 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=ghVWFLUr6713RIktIzJDLwjlFUwvU8dITI3EJerUt6E=; b=LjT4MZufjp0o4q7Kq2ZY/fxXW8be6ykS1POskv/ZA2f0dRSUQNFtnR3aerynp7Dcx0 9lLpGplrICnzDDZRRRFZs226fEgi1dUrdsLkN+GowLq0hMk3nvcQwtSq55neW6xS2EhE mUSk7KdMVhDy3GcSnBzY5uUQT4RoNHQfyWfMgtobBw93IVR7weRIUGMHYOiCEszrf4VW ic+phndbklzxlycM/8h30b7w4Kk1SzDsByxwdHO3Sc2TzjgmmehI1qPPzt1JM2SDLTQ6 yBL/mSrZ88cOXCgaAxwHYx8XuAy2kii4Lag+H3O6+QfENAan+0Pd5Y6xBV+1nBe2t+Yb egMg== X-Gm-Message-State: AOJu0YyNawnIQqx8Jbp2cyGtjzoY5Zy0c0IxgEpFHO6tYmd1nUH83dpv ySjZBMQpgImx8S6tWgJyxfwJBnwFPjGOsRfbJIikd9HjNFg= X-Received: by 2002:a7b:c4d1:0:b0:40e:4aed:ad27 with SMTP id g17-20020a7bc4d1000000b0040e4aedad27mr125789wmk.144.1704718430674; Mon, 08 Jan 2024 04:53:50 -0800 (PST) Received: from localhost.localdomain ([176.176.175.62]) by smtp.gmail.com with ESMTPSA id l42-20020a05600c1d2a00b004030e8ff964sm11004600wms.34.2024.01.08.04.53.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jan 2024 04:53:50 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: Gerd Hoffmann , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v2 1/2] hw/block/pflash_cfi01: Use the LD/ST API in pflash_data_read/write Date: Mon, 8 Jan 2024 13:53:41 +0100 Message-ID: <20240108125342.48298-2-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240108125342.48298-1-philmd@linaro.org> References: <20240108125342.48298-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=philmd@linaro.org; helo=mail-wm1-x32e.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 Similarly to commit c3d25271b2 ("hw/block/pflash_cfi02: Use the ldst API in pflash_write") for CFI type 2, use the LD/ST API for CFI type 1. Signed-off-by: Philippe Mathieu-Daudé --- hw/block/pflash_cfi01.c | 63 ++++++----------------------------------- 1 file changed, 8 insertions(+), 55 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 3e2dc08bd7..ce63ba43b6 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -225,34 +225,10 @@ static uint32_t pflash_data_read(PFlashCFI01 *pfl, hwaddr offset, uint32_t ret; p = pfl->storage; - switch (width) { - case 1: - ret = p[offset]; - break; - case 2: - if (be) { - ret = p[offset] << 8; - ret |= p[offset + 1]; - } else { - ret = p[offset]; - ret |= p[offset + 1] << 8; - } - break; - case 4: - if (be) { - ret = p[offset] << 24; - ret |= p[offset + 1] << 16; - ret |= p[offset + 2] << 8; - ret |= p[offset + 3]; - } else { - ret = p[offset]; - ret |= p[offset + 1] << 8; - ret |= p[offset + 2] << 16; - ret |= p[offset + 3] << 24; - } - break; - default: - abort(); + if (be) { + ret = ldn_be_p(p, width); + } else { + ret = ldn_le_p(p, width); } trace_pflash_data_read(pfl->name, offset, width, ret); return ret; @@ -406,34 +382,11 @@ static inline void pflash_data_write(PFlashCFI01 *pfl, hwaddr offset, uint8_t *p = pfl->storage; trace_pflash_data_write(pfl->name, offset, width, value, pfl->counter); - switch (width) { - case 1: - p[offset] = value; - break; - case 2: - if (be) { - p[offset] = value >> 8; - p[offset + 1] = value; - } else { - p[offset] = value; - p[offset + 1] = value >> 8; - } - break; - case 4: - if (be) { - p[offset] = value >> 24; - p[offset + 1] = value >> 16; - p[offset + 2] = value >> 8; - p[offset + 3] = value; - } else { - p[offset] = value; - p[offset + 1] = value >> 8; - p[offset + 2] = value >> 16; - p[offset + 3] = value >> 24; - } - break; + if (be) { + stn_be_p(p, width, value); + } else { + stn_le_p(p, width, value); } - } static void pflash_write(PFlashCFI01 *pfl, hwaddr offset, From patchwork Mon Jan 8 12:53:42 2024 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: 760762 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp867704wrz; Mon, 8 Jan 2024 04:54:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJnk4wdfL2wMHr0v6sGgbVfRJXe2OeuEHbku6t4OPrk+xZWJRXW3RZlQLlNlXRksK17gHo X-Received: by 2002:a05:6214:e41:b0:67f:89c6:27bb with SMTP id o1-20020a0562140e4100b0067f89c627bbmr4648372qvc.79.1704718464110; Mon, 08 Jan 2024 04:54:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704718464; cv=none; d=google.com; s=arc-20160816; b=PRDsdNuxhAVDOW6s08tFq1fh6va0Zp0sUxYGLArEbuGrel0vHdXec7maOLG0hFXIw/ yplcBS2oIepxFIjt68S/1FB1KkJ8gbLkgn/fThRaUk9Hs0Ft4R4XyxMP1W3XtLo/ZLpX Sr5tpPEs8sy9I5KjZ3kBI30bOUHZetBKYRpeabi7B70UPOZz1lBZAb005tlJnPPA5pwj FKVy+K5Lp1sprvaga3Zbh4OFKZWAB78FnY+RDD2qEcndGk4kRrvYwoG/6r4HgcYxNPsO Lj+KBjRsjA5O42JaOSFmLsR+KblgofF6TNOeuujRjKmzuPKxFm/eToO6hapghRBU1zEn 5YzQ== 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=GmyBpmtz8oXX8KnZsDsq1mJ6m0zHjrv/X6q6YSk63xs=; fh=ot/AHU6TwUyk96ZCKy1yVPJbrRHdtPsmBCo02SyURJs=; b=B+v1crInK7j9h14LWcZMVr0CJesZXNa//ZKst+AL5RBxXvvQrHoT84WQD+9HmSsq+A qpi+Zjy4CjeGR+zzMKHy2aIpeLffbYstPhFnDtP5bIuUR5PA0P2f8A0AWVAeNCVKG6Nl UcLvsbI7Rosgxs0HgJRqKd3+Kf/O204hhBGJHcCWdMfFbOe/dw+2KdbGzEQoerCuUmpr /aXfTjkuVeKaLbhQimrbN1pYitXjo3M46+eJYDVE+JQsliwVRAo0C7TBV1TXjVMvJySs gW9/6BrDU/uaD6wQKiNtzpKypdFqssmD5/we41Bf/NJLPuSGWMBMIpS8QQ+hgriCzanl bLAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GmcYILCj; 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 g4-20020a0cf084000000b0067f94768e7bsi7590951qvk.246.2024.01.08.04.54.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Jan 2024 04:54:24 -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=GmcYILCj; 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 1rMp8p-00031t-RC; Mon, 08 Jan 2024 07:54:08 -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 1rMp8j-00030e-B0 for qemu-devel@nongnu.org; Mon, 08 Jan 2024 07:54:01 -0500 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rMp8f-0006It-O1 for qemu-devel@nongnu.org; Mon, 08 Jan 2024 07:54:00 -0500 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40d8902da73so16449555e9.2 for ; Mon, 08 Jan 2024 04:53:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704718436; x=1705323236; 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=GmyBpmtz8oXX8KnZsDsq1mJ6m0zHjrv/X6q6YSk63xs=; b=GmcYILCjtScSciAIBMyJIbwhY9OCXElf2fYsgbaT61SOFV24m5rwd8WIUjVTfI6lKI pKjR8/bJ055ZfDz8HXSCnelkQkIRz6hA4+Vyscq1bULMxPMY4w7waDcxLDtJHaq6En7E aav1l6eMLpE5e06SnQIotKKttbdK1A8z/fFT0wjdxCz9/lnWbb3qWesGOR8Ng4H6GE4W 8SKLG5OCTcNHFSRfnrzsTxxifwECDDAb1Ts9YZaMvlrrVP7V+RkYrCCYMUPHQRhuD55Y Cb9F5Y1MreWb5fv+gJ0myGeNrPJrIHMoHLcnt7dMGAH4szsGz7FSiUOJXU1jVOGRScjo nC8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704718436; x=1705323236; 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=GmyBpmtz8oXX8KnZsDsq1mJ6m0zHjrv/X6q6YSk63xs=; b=j0MPZRUlIbXrm7pL837XBpP1cjYJJg2xVePU1LminByaxDBLL9fc6fvH4oZQxfsCV5 ZElupYSOA8VGU8zE1eVCd1mHMPuNI6ZobgCwhbLDaL8gXoE6wEeHMDGye5jgd5tidQGc i3tu/knSsPI1WXSArXeUDOzprI82N8b63dJjsXZ/0ILMwQ3ahUuJ2yYDajMPmGzLbLu5 zhiChAHl89nuRl1okKi/sbRngWaGNAEdKfg88j3gXT3a+3x6pDKbJ5r90w8QQ2zS4V6C AqJsliZswzj8Z0yr8sAMeZXly3K02MDLZXhHjhHnjgvpPkOkFpBo4fkwu9W8XNn3uFoj GLVw== X-Gm-Message-State: AOJu0YwMW/C8V1z76Rl57qE8+rLzbUitmmmEY5HIY64xqoLWXPnWdcx3 YO7WgjSShOlXAB7+0H+wEh65xaVNjNXz8w== X-Received: by 2002:a05:600c:5493:b0:40e:4aef:26bf with SMTP id iv19-20020a05600c549300b0040e4aef26bfmr123659wmb.30.1704718435880; Mon, 08 Jan 2024 04:53:55 -0800 (PST) Received: from localhost.localdomain ([176.176.175.62]) by smtp.gmail.com with ESMTPSA id k7-20020a5d6d47000000b0033725783839sm7750457wri.110.2024.01.08.04.53.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jan 2024 04:53:55 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: Gerd Hoffmann , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v2 2/2] hw/pflash: implement update buffer for block writes Date: Mon, 8 Jan 2024 13:53:42 +0100 Message-ID: <20240108125342.48298-3-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240108125342.48298-1-philmd@linaro.org> References: <20240108125342.48298-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=philmd@linaro.org; helo=mail-wm1-x334.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 From: Gerd Hoffmann Add an update buffer where all block updates are staged. Flush or discard updates properly, so we should never see half-completed block writes in pflash storage. Drop a bunch of FIXME comments ;) Signed-off-by: Gerd Hoffmann Message-ID: <20240105135855.268064-3-kraxel@redhat.com> Signed-off-by: Philippe Mathieu-Daudé --- hw/block/pflash_cfi01.c | 106 ++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 26 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index ce63ba43b6..0120462648 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -80,16 +80,39 @@ struct PFlashCFI01 { uint16_t ident3; uint8_t cfi_table[0x52]; uint64_t counter; - unsigned int writeblock_size; + uint32_t writeblock_size; MemoryRegion mem; char *name; void *storage; VMChangeStateEntry *vmstate; bool old_multiple_chip_handling; + + /* block update buffer */ + unsigned char *blk_bytes; + uint32_t blk_offset; }; static int pflash_post_load(void *opaque, int version_id); +static bool pflash_blk_write_state_needed(void *opaque) +{ + PFlashCFI01 *pfl = opaque; + + return (pfl->blk_offset != -1); +} + +static const VMStateDescription vmstate_pflash_blk_write = { + .name = "pflash_cfi01_blk_write", + .version_id = 1, + .minimum_version_id = 1, + .needed = pflash_blk_write_state_needed, + .fields = (const VMStateField[]) { + VMSTATE_VBUFFER_UINT32(blk_bytes, PFlashCFI01, 0, NULL, writeblock_size), + VMSTATE_UINT32(blk_offset, PFlashCFI01), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_pflash = { .name = "pflash_cfi01", .version_id = 1, @@ -101,6 +124,10 @@ static const VMStateDescription vmstate_pflash = { VMSTATE_UINT8(status, PFlashCFI01), VMSTATE_UINT64(counter, PFlashCFI01), VMSTATE_END_OF_LIST() + }, + .subsections = (const VMStateDescription * const []) { + &vmstate_pflash_blk_write, + NULL } }; @@ -376,12 +403,51 @@ static void pflash_update(PFlashCFI01 *pfl, int offset, } } +/* copy current flash content to block update buffer */ +static void pflash_blk_write_start(PFlashCFI01 *pfl, hwaddr offset) +{ + hwaddr mask = ~(pfl->writeblock_size - 1); + + pfl->blk_offset = offset & mask; + memcpy(pfl->blk_bytes, pfl->storage + pfl->blk_offset, + pfl->writeblock_size); +} + +/* commit block update buffer changes */ +static void pflash_blk_write_flush(PFlashCFI01 *pfl) +{ + g_assert(pfl->blk_offset != -1); + memcpy(pfl->storage + pfl->blk_offset, pfl->blk_bytes, + pfl->writeblock_size); + pflash_update(pfl, pfl->blk_offset, pfl->writeblock_size); + pfl->blk_offset = -1; +} + +/* discard block update buffer changes */ +static void pflash_blk_write_abort(PFlashCFI01 *pfl) +{ + pfl->blk_offset = -1; +} + static inline void pflash_data_write(PFlashCFI01 *pfl, hwaddr offset, uint32_t value, int width, int be) { - uint8_t *p = pfl->storage; + uint8_t *p; + + if (pfl->blk_offset != -1) { + /* block write: redirect writes to block update buffer */ + if ((offset < pfl->blk_offset) || + (offset + width > pfl->blk_offset + pfl->writeblock_size)) { + pfl->status |= 0x10; /* Programming error */ + return; + } + p = pfl->blk_bytes + (offset - pfl->blk_offset); + } else { + /* write directly to storage */ + trace_pflash_data_write(pfl->name, offset, width, value, pfl->counter); + p = pfl->storage + offset; + } - trace_pflash_data_write(pfl->name, offset, width, value, pfl->counter); if (be) { stn_be_p(p, width, value); } else { @@ -504,6 +570,7 @@ static void pflash_write(PFlashCFI01 *pfl, hwaddr offset, trace_pflash_write_block(pfl->name, value); pfl->counter = value; pfl->wcycle++; + pflash_blk_write_start(pfl, offset); break; case 0x60: if (cmd == 0xd0) { @@ -534,12 +601,7 @@ static void pflash_write(PFlashCFI01 *pfl, hwaddr offset, switch (pfl->cmd) { case 0xe8: /* Block write */ /* FIXME check @offset, @width */ - if (!pfl->ro) { - /* - * FIXME writing straight to memory is *wrong*. We - * should write to a buffer, and flush it to memory - * only on confirm command (see below). - */ + if (!pfl->ro && (pfl->blk_offset != -1)) { pflash_data_write(pfl, offset, value, width, be); } else { pfl->status |= 0x10; /* Programming error */ @@ -548,18 +610,8 @@ static void pflash_write(PFlashCFI01 *pfl, hwaddr offset, pfl->status |= 0x80; if (!pfl->counter) { - hwaddr mask = pfl->writeblock_size - 1; - mask = ~mask; - trace_pflash_write(pfl->name, "block write finished"); pfl->wcycle++; - if (!pfl->ro) { - /* Flush the entire write buffer onto backing storage. */ - /* FIXME premature! */ - pflash_update(pfl, offset & mask, pfl->writeblock_size); - } else { - pfl->status |= 0x10; /* Programming error */ - } } pfl->counter--; @@ -571,20 +623,17 @@ static void pflash_write(PFlashCFI01 *pfl, hwaddr offset, case 3: /* Confirm mode */ switch (pfl->cmd) { case 0xe8: /* Block write */ - if (cmd == 0xd0) { - /* FIXME this is where we should write out the buffer */ + if ((cmd == 0xd0) && !(pfl->status & 0x10)) { + pflash_blk_write_flush(pfl); pfl->wcycle = 0; pfl->status |= 0x80; } else { - qemu_log_mask(LOG_UNIMP, - "%s: Aborting write to buffer not implemented," - " the data is already written to storage!\n" - "Flash device reset into READ mode.\n", - __func__); + pflash_blk_write_abort(pfl); goto mode_read_array; } break; default: + pflash_blk_write_abort(pfl); goto error_flash; } break; @@ -818,6 +867,9 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) pfl->cmd = 0x00; pfl->status = 0x80; /* WSM ready */ pflash_cfi01_fill_cfi_table(pfl); + + pfl->blk_bytes = g_malloc(pfl->writeblock_size); + pfl->blk_offset = -1; } static void pflash_cfi01_system_reset(DeviceState *dev) @@ -837,6 +889,8 @@ static void pflash_cfi01_system_reset(DeviceState *dev) * This model deliberately ignores this delay. */ pfl->status = 0x80; + + pfl->blk_offset = -1; } static Property pflash_cfi01_properties[] = {