From patchwork Wed Apr 25 23:45:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Clark X-Patchwork-Id: 10364433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 20C106038F for ; Thu, 26 Apr 2018 00:15:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0DBE52900B for ; Thu, 26 Apr 2018 00:15:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 004F429011; Thu, 26 Apr 2018 00:15:00 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4384A2900B for ; Thu, 26 Apr 2018 00:15:00 +0000 (UTC) Received: from localhost ([::1]:39694 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fBUYt-0001bz-Cu for patchwork-qemu-devel@patchwork.kernel.org; Wed, 25 Apr 2018 20:14:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46818) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fBU9d-0005lo-Fy for qemu-devel@nongnu.org; Wed, 25 Apr 2018 19:48:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fBU9b-0005VR-7m for qemu-devel@nongnu.org; Wed, 25 Apr 2018 19:48:53 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:38906) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fBU9a-0005V1-Vn for qemu-devel@nongnu.org; Wed, 25 Apr 2018 19:48:51 -0400 Received: by mail-pf0-x241.google.com with SMTP id o76so11876622pfi.5 for ; Wed, 25 Apr 2018 16:48:50 -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=hPM1woFJd5NIoT+l/RkjBfkcuVLy5QunyU17I2RYujQ=; b=b2LEWCh6y8xLypc6DrYzF4Q2yO8ia38/4/CXfL7ph634ZBlQ66RsL6Bs9JknwQ+8gE I/Bu5Qom7OI8EBNtohmSsB8JV+Gl9KQ7qfWfkIvOnUXT7YVj/z/xJWXx5Vk3L8mXpzGK PIMfM6hG/v5H2NgqMcmFXXhYvbM8UjtrVKb/On3bYs7S/UhuV2RvfZ+HBgX/YtLM8h3t 1vKVB42/zXy/fvwhul5mKkFH96dKEdzIKw/gonnXwuWPp884rkR1L9W8GgAaagQP6cj6 d0TCFRpwSF8gVLTFKea9op/jQKYAdn2HWWVpo/REFNcnOMhYlIdcwNIc2IK1T/tXpH9i sxLQ== 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=hPM1woFJd5NIoT+l/RkjBfkcuVLy5QunyU17I2RYujQ=; b=DH16C6WYgXxFwxp0mTSZVn2pu37RFguweNDmCMgfAX3sho0AUZAKZss2xK1Uz+HEJD 1Sq4oFses4SCfBeUfRlvV+1He0Z27gbpb10VtalwgaDEis+W5G1G7RI10ee6m9nkcyrZ P4ES7Fy8ecH0QSVoZoQSUe5EZSI1XBIRRhbdP+aOCaJM8cHaPRALEOnDNnuX9E20MFiQ Xpbzkbmp9Feica1HtAvEBA1v47xvBK5diWbjuWO78m2matvynjBEBPgXz6rrkpaer+4u GOjiLtq7XAhLWH4YNQXlxaDvsMLLwDiXpU6bxIkABwx280tMkArslz2hldc0mwz/DIUw aWVA== X-Gm-Message-State: ALQs6tCl6LRLGt5hHcoHEUCN1C6rh6ayi4pkkMYskO+SstRe6wuLaWPh zCuem+Jku4Sdl1BHnWKXtB32ylei33w= X-Google-Smtp-Source: AIpwx4/jhUClaNp/fPJMWa7bvmQsvz+Qd+oZ74zJt/k1h7Kb4FQuaNJcNNOQGDmXKq//30dgnGVONA== X-Received: by 10.99.96.202 with SMTP id u193mr20391325pgb.197.1524700129938; Wed, 25 Apr 2018 16:48:49 -0700 (PDT) Received: from localhost.localdomain (122-58-167-38-fibre.bb.spark.co.nz. [122.58.167.38]) by smtp.gmail.com with ESMTPSA id e10sm29577549pfn.67.2018.04.25.16.48.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 25 Apr 2018 16:48:49 -0700 (PDT) From: Michael Clark To: qemu-devel@nongnu.org Date: Thu, 26 Apr 2018 11:45:31 +1200 Message-Id: <1524699938-6764-29-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1524699938-6764-1-git-send-email-mjc@sifive.com> References: <1524699938-6764-1-git-send-email-mjc@sifive.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH v8 28/35] 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: Sagar Karandikar , Bastian Koppelmann , Palmer Dabbelt , Michael Clark , Alistair Francis , patches@groups.riscv.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Use the new CSR read/modify/write interface to implement atomic updates to mip/sip. Cc: Sagar Karandikar Cc: Bastian Koppelmann Cc: Palmer Dabbelt Cc: Alistair Francis Signed-off-by: Michael Clark Reviewed-by: Alistair Francis --- 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 a424867..c704545 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -491,25 +491,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; } @@ -627,17 +633,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 */ @@ -819,7 +819,7 @@ static const riscv_csr_operations csr_ops[0xfff] = { [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 }, @@ -832,7 +832,7 @@ static const riscv_csr_operations csr_ops[0xfff] = { [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 },