From patchwork Fri Jan 11 18:06:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 10760453 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 895571869 for ; Fri, 11 Jan 2019 18:08:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7A56E2A23E for ; Fri, 11 Jan 2019 18:08:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6EC142A246; Fri, 11 Jan 2019 18:08:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DFDCD2A244 for ; Fri, 11 Jan 2019 18:08:01 +0000 (UTC) Received: from localhost ([127.0.0.1]:36457 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi1Dt-0000uj-8d for patchwork-qemu-devel@patchwork.kernel.org; Fri, 11 Jan 2019 13:08:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi1Cg-0008QD-BJ for qemu-devel@nongnu.org; Fri, 11 Jan 2019 13:06:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gi1Ce-0002e4-DK for qemu-devel@nongnu.org; Fri, 11 Jan 2019 13:06:46 -0500 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]:38627) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gi1Ce-0002bJ-76 for qemu-devel@nongnu.org; Fri, 11 Jan 2019 13:06:44 -0500 Received: by mail-pg1-x534.google.com with SMTP id g189so6625702pgc.5 for ; Fri, 11 Jan 2019 10:06:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=subject:date:message-id:in-reply-to:references:cc:from:to; bh=25DrlostMl53bKZZKWDtpDrtjnQ/9J8kol06XrENVUA=; b=hXusvXtq8eI5cLoqHOUwSfozR4P6qIcz6uMqf6r9w+1/+LGKHqVv5llfg+64KVE9Nf sIr7kjnsW7Pqm9TiecMDYNZ1Y+gUnBNLqjWqESjyx+w5PCgmlgGrHsfJwlohgOlyJFPE miibHtotlH/rElZ/Pm+Dngoh2Qu90fApwbEkc1kZ/YlxW3Btz6blL6fHYBAJMicqPaPK 7EfURZ7Bi3wAOyRA8cwf51QjcVBct3PFNApb00DIINm4osfmsZWGBi4ymqRa0EUAQ5cq u+XElfRsOjlSuQCQDDm7aZ5GDa1G4RC03Ac16aNjvgmwuCjXO1buAyjg7szHXmLzDqeO OfQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:date:message-id:in-reply-to:references :cc:from:to; bh=25DrlostMl53bKZZKWDtpDrtjnQ/9J8kol06XrENVUA=; b=Xioe1NGjczab0TS9xL43dmjvB+m2iFHjEzieman6N5vnneYy/GzOyH9kOL70b7GuOH R+Ckk4kv6YtSKwSXOA+h00yq5135j9zXe9MqCkH6QtBKS6YoQb19Y97yeoWM074SXbxf Y7yoqfxHrSzxGUxaaOwlJcbreUKabL7CDZdCX+Hm2hRrrdmaYEVqnb+MM/wmybd5H2Lm 86DA01g5gGF3jU7R+d+2XASmTj8UOxjswk+wJocqKrcpsFCBawJP0Mirq1tjMEreL5Xm PAH9jUoHETc1M+rWAxQKXkts0cg5pj+JQUyC7kjkwo76xtk97SPgN3xsMAzs6ttPIsuQ SQJA== X-Gm-Message-State: AJcUukckM5/N1RQjqTbJcxcAZkz5fV2IPXAxKkFVX0I2giV4gB9Rh+1q R8DaTUcFBJwstmaj8p+ObD5UPQ== X-Google-Smtp-Source: ALg8bN75vci3as4IuxqnjJgLyDZRUp02hgZpsy48cu5q3EL7XXIw8KEhodCBfQAQyXecpG5EiEzfwQ== X-Received: by 2002:a63:c0f:: with SMTP id b15mr14397689pgl.314.1547230000969; Fri, 11 Jan 2019 10:06:40 -0800 (PST) Received: from localhost ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id t67sm155709907pfd.90.2019.01.11.10.06.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Jan 2019 10:06:39 -0800 (PST) Date: Fri, 11 Jan 2019 10:06:28 -0800 Message-Id: <20190111180630.6433-3-palmer@sifive.com> X-Mailer: git-send-email 2.18.1 In-Reply-To: <20190111180630.6433-1-palmer@sifive.com> References: <20190111180630.6433-1-palmer@sifive.com> From: Palmer Dabbelt To: Peter Maydell X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::534 Subject: [Qemu-devel] [PULL 2/4] RISC-V: Implement atomic mip/sip CSR updates X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alistair Francis , Michael Clark , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Palmer Dabbelt Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Michael Clark Use the new CSR read/modify/write interface to implement atomic updates to mip/sip. Signed-off-by: Michael Clark Signed-off-by: Alistair Francis Reviewed-by: Richard Henderson Signed-off-by: Palmer Dabbelt --- target/riscv/csr.c | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index b61b0ef37971..44ea8b7cb6e8 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -487,25 +487,31 @@ static int write_mbadaddr(CPURISCVState *env, int csrno, target_ulong val) return 0; } -static int read_mip(CPURISCVState *env, int csrno, target_ulong *val) -{ - *val = atomic_read(&env->mip); - return 0; -} - -static int write_mip(CPURISCVState *env, int csrno, target_ulong val) +static int rmw_mip(CPURISCVState *env, int csrno, target_ulong *ret_value, + target_ulong new_value, target_ulong write_mask) { RISCVCPU *cpu = riscv_env_get_cpu(env); + target_ulong mask = write_mask & delegable_ints; + uint32_t old_mip; + + /* We can't allow the supervisor to control SEIP as this would allow the + * supervisor to clear a pending external interrupt which will result in + * lost a interrupt in the case a PLIC is attached. The SEIP bit must be + * hardware controlled when a PLIC is attached. This should be an option + * for CPUs with software-delegated Supervisor External Interrupts. */ + mask &= ~MIP_SEIP; + + if (mask) { + qemu_mutex_lock_iothread(); + old_mip = riscv_cpu_update_mip(cpu, mask, (new_value & mask)); + qemu_mutex_unlock_iothread(); + } else { + old_mip = atomic_read(&env->mip); + } - /* - * csrs, csrc on mip.SEIP is not decomposable into separate read and - * write steps, so a different implementation is needed - */ - - qemu_mutex_lock_iothread(); - riscv_cpu_update_mip(cpu, MIP_SSIP | MIP_STIP, - (val & (MIP_SSIP | MIP_STIP))); - qemu_mutex_unlock_iothread(); + if (ret_value) { + *ret_value = old_mip; + } return 0; } @@ -623,17 +629,11 @@ static int write_sbadaddr(CPURISCVState *env, int csrno, target_ulong val) return 0; } -static int read_sip(CPURISCVState *env, int csrno, target_ulong *val) -{ - *val = atomic_read(&env->mip) & env->mideleg; - return 0; -} - -static int write_sip(CPURISCVState *env, int csrno, target_ulong val) +static int rmw_sip(CPURISCVState *env, int csrno, target_ulong *ret_value, + target_ulong new_value, target_ulong write_mask) { - target_ulong newval = (atomic_read(&env->mip) & ~env->mideleg) - | (val & env->mideleg); - return write_mip(env, CSR_MIP, newval); + return rmw_mip(env, CSR_MSTATUS, ret_value, new_value, + write_mask & env->mideleg); } /* Supervisor Protection and Translation */ @@ -812,7 +812,7 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MEPC] = { read_mepc, write_mepc }, [CSR_MCAUSE] = { read_mcause, write_mcause }, [CSR_MBADADDR] = { read_mbadaddr, write_mbadaddr }, - [CSR_MIP] = { read_mip, write_mip }, + [CSR_MIP] = { NULL, NULL, rmw_mip }, /* Supervisor Trap Setup */ [CSR_SSTATUS] = { read_sstatus, write_sstatus }, @@ -825,7 +825,7 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_SEPC] = { read_sepc, write_sepc }, [CSR_SCAUSE] = { read_scause, write_scause }, [CSR_SBADADDR] = { read_sbadaddr, write_sbadaddr }, - [CSR_SIP] = { read_sip, write_sip }, + [CSR_SIP] = { NULL, NULL, rmw_sip }, /* Supervisor Protection and Translation */ [CSR_SATP] = { read_satp, write_satp },