From patchwork Thu Oct 11 20:31:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 10637401 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 DB2DA112B for ; Thu, 11 Oct 2018 20:36:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C73752C12B for ; Thu, 11 Oct 2018 20:36:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BB6892C12E; Thu, 11 Oct 2018 20:36:40 +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.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 D4A802C0BE for ; Thu, 11 Oct 2018 20:36:39 +0000 (UTC) Received: from localhost ([::1]:37006 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gAhhH-0001ez-4v for patchwork-qemu-devel@patchwork.kernel.org; Thu, 11 Oct 2018 16:36:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39674) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gAhcs-0006jY-Bt for qemu-devel@nongnu.org; Thu, 11 Oct 2018 16:32:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gAhcn-0006h1-9l for qemu-devel@nongnu.org; Thu, 11 Oct 2018 16:32:06 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:29185) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gAhcm-0006dE-H8 for qemu-devel@nongnu.org; Thu, 11 Oct 2018 16:32:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1539289920; x=1570825920; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=tmuJh4dPjfMR4X5JCi0mqUEVAYrAQXsdcptMyYwe8Yo=; b=L0pz1jPCFQSmha9O4QQ8wqiiNA9fjcizPYPfLEcoHGZj5DW72bUsASph fPvSerUY1BrhnzvE30Tffqqm4JrkiarU897rDxoYODuC6wNCBjeZggrrp 3kKJCE0CQaRMCqKk6EhZ9FvSn+Ng9lJGbBA3/u7KL2TZ83ptStRlmx5KG v2YJ5laluwjYt6eyFL89Kc8zFWHwSDSczMxx0rOa96DO9qA79YAsq5tlG I6Eoeh3P5Qnxj+g2YPYw1xU1j2s6D64Yw6ntFHeH39aN2t77bgl3YCTdh hYw0YZv0c3jNsvc6bwNSxd1Fo8XGpd66JkaC6DqIGqP63ift8B30L9uOJ g==; X-IronPort-AV: E=Sophos;i="5.54,369,1534780800"; d="scan'208";a="91781676" Received: from mail-bl2nam02lp0088.outbound.protection.outlook.com (HELO NAM02-BL2-obe.outbound.protection.outlook.com) ([207.46.163.88]) by ob1.hgst.iphmx.com with ESMTP; 12 Oct 2018 04:31:59 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector1-wdc-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=WYf7kfZIsitKxFSVjtO2wLk1sUWD+eEJfYZKo+9nmec=; b=T4rOqagHI9WDTH2Kh65ePp3T3z3/tf/cWh5uIiqwj0SWoGSVLc9v646nb4x+fo9ANF/qog0+QcdNJaNC7xw7iB8MwTkl4ATiHOx+D3oizQyhgt+6Rj03La6BgkA4quv7aT5F7ZL1ZQmmVP0j0pKDyueMCTh7Rji3oCkfxgmqwCM= Received: from CY4PR04MB0392.namprd04.prod.outlook.com (10.173.188.145) by CY4PR04MB1224.namprd04.prod.outlook.com (10.173.193.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1207.26; Thu, 11 Oct 2018 20:31:57 +0000 Received: from CY4PR04MB0392.namprd04.prod.outlook.com ([fe80::ddab:bdc2:6bc7:6d22]) by CY4PR04MB0392.namprd04.prod.outlook.com ([fe80::ddab:bdc2:6bc7:6d22%6]) with mapi id 15.20.1228.020; Thu, 11 Oct 2018 20:31:57 +0000 From: Alistair Francis To: "peter.maydell@linaro.org" Thread-Topic: [PULL 06/10] RISC-V: Allow setting and clearing multiple irqs Thread-Index: AQHUYaF0gZE+NDNYZkCMZ1cslkKjqw== Date: Thu, 11 Oct 2018 20:31:56 +0000 Message-ID: <20181011203115.18666-7-alistair.francis@wdc.com> References: <20181011203115.18666-1-alistair.francis@wdc.com> In-Reply-To: <20181011203115.18666-1-alistair.francis@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.17.1 x-clientproxiedby: SG2PR01CA0126.apcprd01.prod.exchangelabs.com (2603:1096:4:40::30) To CY4PR04MB0392.namprd04.prod.outlook.com (2603:10b6:903:b1::17) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alistair.Francis@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [199.255.44.250] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; CY4PR04MB1224; 6:cmTQ6+4DzTQjQ8fQMoLtvHWZ9iO7mg1hmguj3vEqlLuNPi7ifFkF9jHHjrZDgbQhmbndWquQ+KBW/mM8fTDdax0pDw8mDNHUDq38SRb/06E0cwSdntArke+pR5DSGixTimBAVb1GtqjTYaut/5RO2b1jx9+Rkg/aBMtJMiFwFQhuaA6LIxwV5UpHZj+q5cH7979Cu7nAc42hSEprSfK5Lbk4WTXYfmWme4TkBD3YH6G0ossACh0oUYfdn2HUs/9MuYsCmxu0GMjme1xa0WHE2aShaHGneeOjnDZO6rtQNdckZxFhzQMfnf5oNLfmTEitLYQuAbwAi2pBaZV441JuuI7NZOTUPwV8xcR2tAKBmQILsgE7t5o1srs2ZgnN16IL2IsGTAmadr4DwfK+ek2CoRB2gH9Bk9++WguPaoYKd0EoFpvS9RXhNSvoJiC8KCssm+QXbu/tdqVHQ/t7IbrU8Q==; 5:ypdPhxW08bTKdwNqvNPscd8l2cSalmy+QsFr/nC8s9aFK0oiQXuj+zDcH2nE+Vm9Vb6cstumzEWXHK350jR5c/cz2CVOCePWvP2Grji7Kv9ZmU7j+ebAGHeaqxdKSjmwChVvlP+NvDDuzTNuwkIYSt1CN6V2pW85nRB1oeUIu5k=; 7:MNuGC8n7kDsP3Om+IBuVFyKRW2ufNcpr8FONSVz3bHgW7z4OmJ/WM8qaJ5UE6OZL/2/Mml3CGtRItn8oigOJbJut7cV6gwRBvicVL7ySqX4m2o0wuB7w+KGhSU6uqxBozX1ZA8mG9TUiHgdcWrcMpQk3dZpuU/ehTZLrlbxeHw5Zwyjn3/mDthzxeIV8J7eq1CesDPJuJn3rENJ7PqTyhvAcxTLuq/15PGhcTh/nswtsyNJ0os5f5dxjrT+pAkWJ x-ms-office365-filtering-correlation-id: e2bf52dd-2747-4d21-f3bb-08d62fb89679 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(4618075)(2017052603328)(7153060)(7193020); SRVR:CY4PR04MB1224; x-ms-traffictypediagnostic: CY4PR04MB1224: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(3231355)(944501410)(52105095)(3002001)(10201501046)(93006095)(93001095)(6055026)(149066)(150057)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123564045)(20161123558120)(20161123562045)(201708071742011)(7699051); SRVR:CY4PR04MB1224; BCL:0; PCL:0; RULEID:; SRVR:CY4PR04MB1224; x-forefront-prvs: 08220FA8D6 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(396003)(346002)(376002)(39860400002)(366004)(136003)(199004)(189003)(81156014)(8936002)(68736007)(36756003)(6506007)(86362001)(14444005)(7736002)(6916009)(8676002)(105586002)(76176011)(2906002)(256004)(81166006)(50226002)(54906003)(5660300001)(5640700003)(6486002)(1076002)(4326008)(52116002)(486006)(11346002)(2900100001)(102836004)(2501003)(99286004)(2616005)(6436002)(478600001)(72206003)(44832011)(476003)(186003)(97736004)(2351001)(25786009)(71190400001)(53936002)(3846002)(14454004)(386003)(106356001)(316002)(6512007)(446003)(305945005)(71200400001)(26005)(66066001)(6116002)(5250100002); DIR:OUT; SFP:1102; SCL:1; SRVR:CY4PR04MB1224; H:CY4PR04MB0392.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-microsoft-antispam-message-info: OaROawiOmkfxN01EImnIbX0epgYEFF0BT6MXBoytFHuNEWJ2oOAmmCSBwkTBbxy4M1EKArUkZAgYMmmBfPsoYWkIitER3ZORDSr3tfjSlLIANZQjQi7Lh4mNfL8okgY12pUZnfT8xJ8Ki36FZZjZBt3zZZUYhsOei9fJ3m+D8enqerl7jP6hOVFhONDd5Gpjjqy5xGUJq7CH6IA68AWd4mbt4R4sQxJSAuSJiTBlnNAxNa8kUt2P4W3r2Fjwg590b7NQnTbn5W/SpRXiQDuS8onmbx5oIqDX9cqUr/lX/gMP0go9qTfJ5SKfyhoM6pzicmOmY+R1SqboDJn5g0RqMZ71UfXbaLVIG1v3SsvXJv8= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: e2bf52dd-2747-4d21-f3bb-08d62fb89679 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Oct 2018 20:31:56.9370 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR04MB1224 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 216.71.154.42 Subject: [Qemu-devel] [PULL 06/10] RISC-V: Allow setting and clearing multiple irqs 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 , "qemu-devel@nongnu.org" , Michael Clark , Alistair Francis 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 Change the API of riscv_set_local_interrupt to take a write mask and value to allow setting and clearing of multiple local interrupts atomically in a single call. Rename the new function to riscv_cpu_update_mip. Cc: Sagar Karandikar Cc: Bastian Koppelmann Cc: Palmer Dabbelt Cc: Alistair Francis Signed-off-by: Michael Clark Reviewed-by: Alistair Francis Reviewed-by: Palmer Dabbelt Signed-off-by: Alistair Francis --- hw/riscv/sifive_clint.c | 8 ++++---- hw/riscv/sifive_plic.c | 4 ++-- target/riscv/cpu.h | 22 +++++++++++++--------- target/riscv/op_helper.c | 24 +++++++++++++++--------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/hw/riscv/sifive_clint.c b/hw/riscv/sifive_clint.c index 7cc606e065..0d2fd52487 100644 --- a/hw/riscv/sifive_clint.c +++ b/hw/riscv/sifive_clint.c @@ -47,12 +47,12 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value) if (cpu->env.timecmp <= rtc_r) { /* if we're setting an MTIMECMP value in the "past", immediately raise the timer interrupt */ - riscv_set_local_interrupt(cpu, MIP_MTIP, 1); + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1)); return; } /* otherwise, set up the future timer interrupt */ - riscv_set_local_interrupt(cpu, MIP_MTIP, 0); + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(0)); diff = cpu->env.timecmp - rtc_r; /* back to ns (note args switched in muldiv64) */ next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + @@ -67,7 +67,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value) static void sifive_clint_timer_cb(void *opaque) { RISCVCPU *cpu = opaque; - riscv_set_local_interrupt(cpu, MIP_MTIP, 1); + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1)); } /* CPU wants to read rtc or timecmp register */ @@ -132,7 +132,7 @@ static void sifive_clint_write(void *opaque, hwaddr addr, uint64_t value, if (!env) { error_report("clint: invalid timecmp hartid: %zu", hartid); } else if ((addr & 0x3) == 0) { - riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_MSIP, value != 0); + riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MSIP, BOOL_TO_MASK(value)); } else { error_report("clint: invalid sip write: %08x", (uint32_t)addr); } diff --git a/hw/riscv/sifive_plic.c b/hw/riscv/sifive_plic.c index f635e6ff67..9cf9a1f986 100644 --- a/hw/riscv/sifive_plic.c +++ b/hw/riscv/sifive_plic.c @@ -142,10 +142,10 @@ static void sifive_plic_update(SiFivePLICState *plic) int level = sifive_plic_irqs_pending(plic, addrid); switch (mode) { case PLICMode_M: - riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_MEIP, level); + riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MEIP, BOOL_TO_MASK(level)); break; case PLICMode_S: - riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_SEIP, level); + riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_SEIP, BOOL_TO_MASK(level)); break; default: break; diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index d4f36295f0..4ee09b9cff 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -126,13 +126,18 @@ struct CPURISCVState { target_ulong mhartid; target_ulong mstatus; + /* * CAUTION! Unlike the rest of this struct, mip is accessed asynchonously - * by I/O threads and other vCPUs, so hold the iothread mutex before - * operating on it. CPU_INTERRUPT_HARD should be in effect iff this is - * non-zero. Use riscv_cpu_set_local_interrupt. + * by I/O threads. It should be read with atomic_read. It should be updated + * using riscv_cpu_update_mip with the iothread mutex held. The iothread + * mutex must be held because mip must be consistent with the CPU inturrept + * state. riscv_cpu_update_mip calls cpu_interrupt or cpu_reset_interrupt + * wuth the invariant that CPU_INTERRUPT_HARD is set iff mip is non-zero. + * mip is 32-bits to allow atomic_read on 32-bit hosts. */ - uint32_t mip; /* allow atomic_read for >= 32-bit hosts */ + uint32_t mip; + target_ulong mie; target_ulong mideleg; @@ -247,7 +252,6 @@ void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, uintptr_t retaddr); int riscv_cpu_handle_mmu_fault(CPUState *cpu, vaddr address, int size, int rw, int mmu_idx); - char *riscv_isa_string(RISCVCPU *cpu); void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf); @@ -255,6 +259,10 @@ void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf); #define cpu_list riscv_cpu_list #define cpu_mmu_index riscv_cpu_mmu_index +#ifndef CONFIG_USER_ONLY +uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value); +#define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */ +#endif void riscv_set_mode(CPURISCVState *env, target_ulong newpriv); void riscv_translate_init(void); @@ -285,10 +293,6 @@ void csr_write_helper(CPURISCVState *env, target_ulong val_to_write, target_ulong csrno); target_ulong csr_read_helper(CPURISCVState *env, target_ulong csrno); -#ifndef CONFIG_USER_ONLY -void riscv_set_local_interrupt(RISCVCPU *cpu, target_ulong mask, int value); -#endif - #include "exec/cpu-all.h" #endif /* RISCV_CPU_H */ diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index aec7558e1b..d0883d329b 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -171,10 +171,8 @@ void csr_write_helper(CPURISCVState *env, target_ulong val_to_write, */ qemu_mutex_lock_iothread(); RISCVCPU *cpu = riscv_env_get_cpu(env); - riscv_set_local_interrupt(cpu, MIP_SSIP, - (val_to_write & MIP_SSIP) != 0); - riscv_set_local_interrupt(cpu, MIP_STIP, - (val_to_write & MIP_STIP) != 0); + riscv_cpu_update_mip(cpu, MIP_SSIP | MIP_STIP, + (val_to_write & (MIP_SSIP | MIP_STIP))); /* * csrs, csrc on mip.SEIP is not decomposable into separate read and * write steps, so a different implementation is needed @@ -657,16 +655,24 @@ target_ulong helper_csrrc(CPURISCVState *env, target_ulong src, #ifndef CONFIG_USER_ONLY /* iothread_mutex must be held */ -void riscv_set_local_interrupt(RISCVCPU *cpu, target_ulong mask, int value) +uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) { - target_ulong old_mip = cpu->env.mip; - cpu->env.mip = (old_mip & ~mask) | (value ? mask : 0); + CPURISCVState *env = &cpu->env; + uint32_t old, new, cmp = atomic_read(&env->mip); - if (cpu->env.mip && !old_mip) { + do { + old = cmp; + new = (old & ~mask) | (value & mask); + cmp = atomic_cmpxchg(&env->mip, old, new); + } while (old != cmp); + + if (new && !old) { cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD); - } else if (!cpu->env.mip && old_mip) { + } else if (!new && old) { cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_HARD); } + + return old; } void riscv_set_mode(CPURISCVState *env, target_ulong newpriv)