From nobody Sun May 19 05:00:12 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1595599076; cv=none; d=zohomail.com; s=zohoarc; b=UlRCIZADCdkwSpwVjUb4nh93kJCPVaGFvhmubChhfeQ/8BoQAYkQst+6pOrTAzQZHblL+M9FjJqxME4/wRm5Qv2IHc0JjLhl6UWp/IUCuKGrjWzV/6SSJQ/Xawt+CJRhRn1ndpjOSTu9VyoP+TO0odIMMbz/1WiyttxEmTQDS1U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595599076; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject; bh=FJWgZR9dxkHebEXa3CI+aE3coVvsa0sr7XhGjd0lqG8=; b=HyFuql7y2G8fkrSW6IFJ4cGbfDqGYdBR2jIMQqEkxE8jiNg2xaKyUWYaC247q8sipsTnmEAZulP6b+1CCPywA6XqeZMsT2ev/e4+eg18rYQq/AvdAkfP5/aomJix/IcrQOKVbn8AsJJBX5BQL+prQrhqGX83U8Xw00GJuXPFAcc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1595599076946306.6347353109037; Fri, 24 Jul 2020 06:57:56 -0700 (PDT) Received: from localhost ([::1]:59292 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jyyCw-0001Sb-PG for importer@patchew.org; Fri, 24 Jul 2020 09:57:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34224) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jyuMq-0004zW-7e for qemu-devel@nongnu.org; Fri, 24 Jul 2020 05:51:52 -0400 Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:51042) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jyuMm-0002q3-QT for qemu-devel@nongnu.org; Fri, 24 Jul 2020 05:51:51 -0400 Received: by mail-pj1-x1042.google.com with SMTP id k71so4944949pje.0 for ; Fri, 24 Jul 2020 02:51:48 -0700 (PDT) Received: from localhost.localdomain (111-243-186-54.dynamic-ip.hinet.net. [111.243.186.54]) by smtp.gmail.com with ESMTPSA id x23sm5581636pfn.4.2020.07.24.02.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 02:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FJWgZR9dxkHebEXa3CI+aE3coVvsa0sr7XhGjd0lqG8=; b=YZUw61qayAokcJsE3AMM/hMWoavlVPaGmeLIuqjSJSPunhVV6NioyDI+1kv4Q+zRgn rBgxl5bs05wJgcJxNaPRvbFKXm3Fn9Z+zW2oPNqFIorLyodPkfIkn4uaN0r6QsYiZ4d/ AgfPBdh6nIWzix6e+Bzvx8uDqdJEynHkqZgZO+esWMagS7GcqEHVDKj5JcxttO5mgdJq nKKVlFz2z7BswYqeGPw+d2m+t6V9cIgfc1bgEqtQtZAF9JPMpC4YpalQ4V5DEOEg12kn 6YhkiuGkXRtdkR/xW/kHntEzSkNlBsRkD1/Xv+cjd/zmtSKMH94LNLA90mAr46fxrK6t KShg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FJWgZR9dxkHebEXa3CI+aE3coVvsa0sr7XhGjd0lqG8=; b=A8A1BedKTXPvydqYsuGztqsfRK7A4zUJGqJnrRdXlK17gHhr2krvMZ30tSek43sWz3 wiSBTd3SkBPh81Ub4lblqjry/ScdVC+rettFPOSpPrvU+V26uadRcT/sztLsrWlrhHTv GSC15GXFLWyXUiaydscYI1KG9+eyawirdVoPBxLaCUTR2ZAo9qN7hqrgh5dE65dtL3tm xRzC0ugdwsQ62k3CtEZFIAsP65eAukAWq4+G/8ec5US5R3nuYt23noO+GvUpVDu9wniA hD9N0LcWf6kYHce57H4Xv8yFbmXQzuXXi+QUJa/AdQZ/LHvgpg4W98QgwDjNr7BYiMR4 hRkA== X-Gm-Message-State: AOAM531O8MsITsG8lc0psfioSNWnq8tD5DsIrg4GS5hQokmlPFdjqE1v nl/9Hu1zJhuG5Jh5joUV4c7WgQ== X-Google-Smtp-Source: ABdhPJz7z4Sw8Kr4ukujIIaIJVnom86cl9IgivN6BqVNHcSmnTcvr4FN03OsHxVYE7rDVWYacZnRbQ== X-Received: by 2002:a17:90a:14a5:: with SMTP id k34mr4821300pja.37.1595584307320; Fri, 24 Jul 2020 02:51:47 -0700 (PDT) From: Green Wan To: Subject: [RFC PATCH 1/2] hw/riscv: sifive_u: Add file-backed OTP. softmmu/vl: add otp-file to boot option Date: Fri, 24 Jul 2020 17:51:11 +0800 Message-Id: <20200724095112.2615-2-green.wan@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200724095112.2615-1-green.wan@sifive.com> References: <20200724095112.2615-1-green.wan@sifive.com> 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=2607:f8b0:4864:20::1042; envelope-from=green.wan@sifive.com; helo=mail-pj1-x1042.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, URIBL_BLOCKED=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 24 Jul 2020 09:54:21 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-riscv@nongnu.org, Sagar Karandikar , Bastian Koppelmann , qemu-devel@nongnu.org, Green Wan , Alistair Francis , Paolo Bonzini , Palmer Dabbelt , bmeng.cn@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a file-backed implementation for OTP of sifive_u machine. Use '-boot otp-file=3Dxxx' to enable it. Do file open, mmap and close for every OTP read/write in case keep the update-to-date snapshot of OTP. Signed-off-by: Green Wan --- hw/riscv/sifive_u_otp.c | 88 ++++++++++++++++++++++++++++++++- include/hw/riscv/sifive_u_otp.h | 2 + qemu-options.hx | 3 +- softmmu/vl.c | 6 ++- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/hw/riscv/sifive_u_otp.c b/hw/riscv/sifive_u_otp.c index f6ecbaa2ca..26e1965821 100644 --- a/hw/riscv/sifive_u_otp.c +++ b/hw/riscv/sifive_u_otp.c @@ -24,6 +24,72 @@ #include "qemu/log.h" #include "qemu/module.h" #include "hw/riscv/sifive_u_otp.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define TRACE_PREFIX "FU540_OTP: " +#define SIFIVE_FU540_OTP_SIZE (SIFIVE_U_OTP_NUM_FUSES * 4) + +static int otp_backed_fd; +static unsigned int *otp_mmap; + +static void sifive_u_otp_backed_load(const char *filename); +static uint64_t sifive_u_otp_backed_read(uint32_t fuseidx); +static void sifive_u_otp_backed_write(uint32_t fuseidx, + uint32_t paio, + uint32_t pdin); +static void sifive_u_otp_backed_unload(void); + +void sifive_u_otp_backed_load(const char *filename) +{ + if (otp_backed_fd < 0) { + + otp_backed_fd =3D open(filename, O_RDWR); + + if (otp_backed_fd < 0) + qemu_log_mask(LOG_TRACE, + TRACE_PREFIX "Warning: can't open otp file\n"); + else { + + otp_mmap =3D (unsigned int *)mmap(0, + SIFIVE_FU540_OTP_SIZE, + PROT_READ | PROT_WRITE | PROT_= EXEC, + MAP_FILE | MAP_SHARED, + otp_backed_fd, + 0); + + if (otp_mmap =3D=3D MAP_FAILED) + qemu_log_mask(LOG_TRACE, + TRACE_PREFIX "Warning: can't mmap otp file\n= "); + } + } + +} + +uint64_t sifive_u_otp_backed_read(uint32_t fuseidx) +{ + return (uint64_t)(otp_mmap[fuseidx]); +} + +void sifive_u_otp_backed_write(uint32_t fuseidx, uint32_t paio, uint32_t p= din) +{ + otp_mmap[fuseidx] &=3D ~(pdin << paio); + otp_mmap[fuseidx] |=3D (pdin << paio); +} + + +void sifive_u_otp_backed_unload(void) +{ + munmap(otp_mmap, SIFIVE_FU540_OTP_SIZE); + close(otp_backed_fd); + otp_backed_fd =3D -1; +} =20 static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int = size) { @@ -46,7 +112,17 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr = addr, unsigned int size) if ((s->pce & SIFIVE_U_OTP_PCE_EN) && (s->pdstb & SIFIVE_U_OTP_PDSTB_EN) && (s->ptrim & SIFIVE_U_OTP_PTRIM_EN)) { - return s->fuse[s->pa & SIFIVE_U_OTP_PA_MASK]; + + if (otp_file) { + uint64_t val; + + sifive_u_otp_backed_load(otp_file); + val =3D sifive_u_otp_backed_read(s->pa); + sifive_u_otp_backed_unload(); + + return val; + } else + return s->fuse[s->pa & SIFIVE_U_OTP_PA_MASK]; } else { return 0xff; } @@ -123,6 +199,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr ad= dr, s->ptrim =3D val32; break; case SIFIVE_U_OTP_PWE: + if (otp_file) { + sifive_u_otp_backed_load(otp_file); + sifive_u_otp_backed_write(s->pa, s->paio, s->pdin); + sifive_u_otp_backed_unload(); + } + s->pwe =3D val32; break; default: @@ -165,6 +247,10 @@ static void sifive_u_otp_reset(DeviceState *dev) /* Make a valid content of serial number */ s->fuse[SIFIVE_U_OTP_SERIAL_ADDR] =3D s->serial; s->fuse[SIFIVE_U_OTP_SERIAL_ADDR + 1] =3D ~(s->serial); + + /* Initialize file mmap and descriptor. */ + otp_mmap =3D NULL; + otp_backed_fd =3D -1; } =20 static void sifive_u_otp_class_init(ObjectClass *klass, void *data) diff --git a/include/hw/riscv/sifive_u_otp.h b/include/hw/riscv/sifive_u_ot= p.h index 639297564a..1342bd7342 100644 --- a/include/hw/riscv/sifive_u_otp.h +++ b/include/hw/riscv/sifive_u_otp.h @@ -52,6 +52,8 @@ #define SIFIVE_U_OTP(obj) \ OBJECT_CHECK(SiFiveUOTPState, (obj), TYPE_SIFIVE_U_OTP) =20 +extern const char *otp_file; + typedef struct SiFiveUOTPState { /*< private >*/ SysBusDevice parent_obj; diff --git a/qemu-options.hx b/qemu-options.hx index 708583b4ce..eb9a54f4ed 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -415,10 +415,11 @@ ERST =20 DEF("boot", HAS_ARG, QEMU_OPTION_boot, "-boot [order=3Ddrives][,once=3Ddrives][,menu=3Don|off]\n" - " [,splash=3Dsp_name][,splash-time=3Dsp_time][,reboot-timeout=3Dr= b_time][,strict=3Don|off]\n" + " [,splash=3Dsp_name][,splash-time=3Dsp_time][,reboot-timeout=3Dr= b_time][,strict=3Don|off][,otp-file=3Dotp_file]\n" " 'drives': floppy (a), hard disk (c), CD-ROM (d), netw= ork (n)\n" " 'sp_name': the file's name that would be passed to bi= os as logo picture, if menu=3Don\n" " 'sp_time': the period that splash picture last if men= u=3Don, unit is ms\n" + " 'otp_file': the file name backed OTP\n" " 'rb_timeout': the timeout before guest reboot when bo= ot failed, unit is ms\n", QEMU_ARCH_ALL) SRST diff --git a/softmmu/vl.c b/softmmu/vl.c index f476ef89ed..58e0b2fc0a 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -21,7 +21,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN * THE SOFTWARE. */ - #include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/units.h" @@ -161,6 +160,7 @@ unsigned int nb_prom_envs =3D 0; const char *prom_envs[MAX_PROM_ENVS]; int boot_menu; bool boot_strict; +const char *otp_file; uint8_t *boot_splash_filedata; int only_migratable; /* turn it off unless user states otherwise */ bool wakeup_suspend_enabled; @@ -308,6 +308,9 @@ static QemuOptsList qemu_boot_opts =3D { }, { .name =3D "strict", .type =3D QEMU_OPT_BOOL, + }, { + .name =3D "otp-file", + .type =3D QEMU_OPT_STRING, }, { /*End of list */ } }, @@ -4215,6 +4218,7 @@ void qemu_init(int argc, char **argv, char **envp) =20 boot_menu =3D qemu_opt_get_bool(opts, "menu", boot_menu); boot_strict =3D qemu_opt_get_bool(opts, "strict", false); + otp_file =3D qemu_opt_get(opts, "otp-file"); } =20 if (!boot_order) { --=20 2.17.1 From nobody Sun May 19 05:00:12 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1595598975; cv=none; d=zohomail.com; s=zohoarc; b=fteS6QnN7MZU0hizXcVUf6QW4E5cg6BfMedi+1fhu+r1P3s1R4IdVMEAgzbMJh3e5JzKjLyn57MRksBLnhD0uTrZisdlaE/54V9OA9gZIKV2wMtIGeV0Fgkd5be5X/VGX2bjiXOEYAr6209Q0hC2cA3MJPd8laxhyM6tAfrmKa4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595598975; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject; bh=RtXwts8UAKZ/GyfPJVf+JplfZufhV8mc3N6j35EEXAE=; b=jRLOQjNT+UNbbW5QDQd2RQ4OB7p3JLO35cxIzDTdzHO7hgwYrC23HbTqt07Urumajc4jvBfMahXS5uU0k/ll7eOalR2AZHfw/Y6cBwp9SI9HoQQAuNGC4a1uKztd5mKY2GQIB/o8yWnA2dA2q3zTrPBCxPgfQCDOuRZROS8rGJc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1595598975078259.1825536784637; Fri, 24 Jul 2020 06:56:15 -0700 (PDT) Received: from localhost ([::1]:51492 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jyyBJ-0006cU-5i for importer@patchew.org; Fri, 24 Jul 2020 09:56:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34256) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jyuMr-00051X-Ci for qemu-devel@nongnu.org; Fri, 24 Jul 2020 05:51:53 -0400 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:33182) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jyuMp-0002qW-07 for qemu-devel@nongnu.org; Fri, 24 Jul 2020 05:51:53 -0400 Received: by mail-pl1-x641.google.com with SMTP id 72so4167677ple.0 for ; Fri, 24 Jul 2020 02:51:50 -0700 (PDT) Received: from localhost.localdomain (111-243-186-54.dynamic-ip.hinet.net. [111.243.186.54]) by smtp.gmail.com with ESMTPSA id x23sm5581636pfn.4.2020.07.24.02.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 02:51:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RtXwts8UAKZ/GyfPJVf+JplfZufhV8mc3N6j35EEXAE=; b=BHe5duqpDrCmFoSkfz3ZlkAUyWMf6iQ2M98LUet0vrR2zvcZiLh3lHt2YnyiNDfVvK c/apETkTwXUIIL37iKWqelOGkgHYoqMFdpZKVLLMrniVJ8VpaAeZc+GhMN0/CvIp3pDX a31arhl/f4qMENfga3AjggTGrgigcYXap+uuHKKTHpuZ+iNfJEjQKKz6zyXJlYVU1U9f HzzWygqFBxeoSNh8b5uOgE9PSbeko0MS4MdCgdKY8Ek5JmL3N9JxQgJXK5ZnAFrVWQF/ dIQwiak1SKkzEczo08/aFOIt3XQ5kX1IEQgI46zl2WkRlS59caCHmiNxwI+SLwiBO3vN r37w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RtXwts8UAKZ/GyfPJVf+JplfZufhV8mc3N6j35EEXAE=; b=izKypeXZKLUGnYyLqjZSIIEmGmHpSiTsSwcKkWyFkVTSAaeFbErKX3dylBLFrKVQpy Sht0TCIljXoXXuDU5iEWYNrhx1dAbTCMLdyX7YjCLRYRY2UOX/78awJ+ZlIqmSOEw5Yj nVGOifluDqpHLPj2DuA48lqrFzNMvo42N5pUu7H2KEb4IACW0tmq5Pyv1cjKjE1m50Hr WmqDEomL+6bdm5SoTfswK/SboGaRNhtPZHvUFaZiYDUQOGenhdfvvGN7wiz9VYEaEGrC EYjHSI5c8E2WouJsjiQg4QiHY012NQw8w9LnuVmz+ShY8+HFYiGhYxOOYtduYMeeI9sE qfqw== X-Gm-Message-State: AOAM5310Y93qNSTpLZ/vapugLsx55m1x1lYdr3iTt3XkVlD7HP/KqWfZ qKaJLbwturl4VTKqOiIcQkja5g== X-Google-Smtp-Source: ABdhPJxsKjg3RDtbq9NBEFjjd8OLruNmjXg6/BikQNUKP/f1FHOO+DHNNm5S/Zdpi7te8VXdhFsT5w== X-Received: by 2002:a17:90a:dd44:: with SMTP id u4mr4285690pjv.203.1595584309656; Fri, 24 Jul 2020 02:51:49 -0700 (PDT) From: Green Wan To: Subject: [RFC PATCH 2/2] hw/riscv: sifive_u: Add write-once protection. Date: Fri, 24 Jul 2020 17:51:12 +0800 Message-Id: <20200724095112.2615-3-green.wan@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200724095112.2615-1-green.wan@sifive.com> References: <20200724095112.2615-1-green.wan@sifive.com> 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=2607:f8b0:4864:20::641; envelope-from=green.wan@sifive.com; helo=mail-pl1-x641.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, URIBL_BLOCKED=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 24 Jul 2020 09:54:21 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-riscv@nongnu.org, Sagar Karandikar , Bastian Koppelmann , qemu-devel@nongnu.org, Green Wan , Alistair Francis , Paolo Bonzini , Palmer Dabbelt , bmeng.cn@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add array to store the 'written' status for all bit of OTP to block the write operation to the same bit. Ignore the control register offset from 0x0 to 0x38 of OTP memory mapping. Signed-off-by: Green Wan --- hw/riscv/sifive_u_otp.c | 34 ++++++++++++++++++++++++++++++--- include/hw/riscv/sifive_u_otp.h | 1 + 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/hw/riscv/sifive_u_otp.c b/hw/riscv/sifive_u_otp.c index 26e1965821..e0f85dee22 100644 --- a/hw/riscv/sifive_u_otp.c +++ b/hw/riscv/sifive_u_otp.c @@ -36,6 +36,12 @@ #define TRACE_PREFIX "FU540_OTP: " #define SIFIVE_FU540_OTP_SIZE (SIFIVE_U_OTP_NUM_FUSES * 4) =20 +#define SET_WRITTEN_BIT(map, idx, bit) \ + (map[idx] |=3D (0x1 << bit)) + +#define GET_WRITTEN_BIT(map, idx, bit) \ + ((map[idx] >> bit) & 0x1) + static int otp_backed_fd; static unsigned int *otp_mmap; =20 @@ -199,6 +205,18 @@ static void sifive_u_otp_write(void *opaque, hwaddr ad= dr, s->ptrim =3D val32; break; case SIFIVE_U_OTP_PWE: + /* Keep written state for data only and PWE is enabled. Ignore PAS= =3D1 */ + if ((s->pa > SIFIVE_U_OTP_PWE) && (val32 & 0x1) && !s->pas) { + if (GET_WRITTEN_BIT(s->fuse_wo, s->pa, s->paio)) { + qemu_log_mask(LOG_GUEST_ERROR, + TRACE_PREFIX "Error: write idx<%u>, bit<%u>\= n", + s->pa, s->paio); + break; + } else { + SET_WRITTEN_BIT(s->fuse_wo, s->pa, s->paio); + } + } + if (otp_file) { sifive_u_otp_backed_load(otp_file); sifive_u_otp_backed_write(s->pa, s->paio, s->pdin); @@ -244,9 +262,19 @@ static void sifive_u_otp_reset(DeviceState *dev) /* Initialize all fuses' initial value to 0xFFs */ memset(s->fuse, 0xff, sizeof(s->fuse)); =20 - /* Make a valid content of serial number */ - s->fuse[SIFIVE_U_OTP_SERIAL_ADDR] =3D s->serial; - s->fuse[SIFIVE_U_OTP_SERIAL_ADDR + 1] =3D ~(s->serial); + /* Initialize write-once map */ + memset(s->fuse_wo, 0x00, sizeof(s->fuse_wo)); + + /* if otp file is used, not over write these value. */ + if (!otp_file) { + /* Make a valid content of serial number */ + s->fuse[SIFIVE_U_OTP_SERIAL_ADDR] =3D s->serial; + s->fuse[SIFIVE_U_OTP_SERIAL_ADDR + 1] =3D ~(s->serial); + + /* set status to 'written' */ + s->fuse_wo[SIFIVE_U_OTP_SERIAL_ADDR] =3D 0xffff; + s->fuse_wo[SIFIVE_U_OTP_SERIAL_ADDR + 1] =3D 0xffff; + } =20 /* Initialize file mmap and descriptor. */ otp_mmap =3D NULL; diff --git a/include/hw/riscv/sifive_u_otp.h b/include/hw/riscv/sifive_u_ot= p.h index 1342bd7342..9c9c57f39e 100644 --- a/include/hw/riscv/sifive_u_otp.h +++ b/include/hw/riscv/sifive_u_otp.h @@ -77,6 +77,7 @@ typedef struct SiFiveUOTPState { uint32_t fuse[SIFIVE_U_OTP_NUM_FUSES]; /* config */ uint32_t serial; + uint32_t fuse_wo[SIFIVE_U_OTP_NUM_FUSES]; } SiFiveUOTPState; =20 #endif /* HW_SIFIVE_U_OTP_H */ --=20 2.17.1