From patchwork Mon Oct 8 18:25:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 10631243 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 8B7CD174A for ; Mon, 8 Oct 2018 18:26:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7E7BA2994A for ; Mon, 8 Oct 2018 18:26:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7261D2995F; Mon, 8 Oct 2018 18:26:54 +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 96DAE2994A for ; Mon, 8 Oct 2018 18:26:53 +0000 (UTC) Received: from localhost ([::1]:47775 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9aF2-0005ZI-Uw for patchwork-qemu-devel@patchwork.kernel.org; Mon, 08 Oct 2018 14:26:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34850) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9aDl-0004YF-Hv for qemu-devel@nongnu.org; Mon, 08 Oct 2018 14:25:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9aDh-0007Y4-PE for qemu-devel@nongnu.org; Mon, 08 Oct 2018 14:25:33 -0400 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:55221) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9aDb-0007KW-Ur for qemu-devel@nongnu.org; Mon, 08 Oct 2018 14:25:27 -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=1539023123; x=1570559123; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=W/deeygCXVRhhYqsVHzVijG1inxixBTuwmHcTaYkloU=; b=qgWCHepZlSKrTpKxQ2sRy0UdrYviZZSTtym2Sl7ti321plZv2f63+a6b qFDrAKl0f9cp93XaozS+Xd4j9TvBUiZO9VqEoU9Lr/pJOcHUH6neNjSZ9 QEMbuHRSJbINrsaPWp9QuPiKtaK2LHEsDGYKaUs0Ykt4DWZVO+qj2c/gd UJ9+liRJTL3N8aD+TwrL+DlkKQKniy7W/Sm+Pdj+p7fAG7p5Aa1pOgNKv kp7SiWMZCJtJbYmAAm3ojv3Tq40bOn4ktaox+DTkX21D1K/NYyIZ0VSNy rJNCyBWiYTdiseM/Qx5HclYAPI3YgG7BIdtfl/9RqKutVqevUvdZyNQIw g==; X-IronPort-AV: E=Sophos;i="5.54,357,1534780800"; d="scan'208";a="92573583" Received: from mail-co1nam05lp0088.outbound.protection.outlook.com (HELO NAM05-CO1-obe.outbound.protection.outlook.com) ([216.32.181.88]) by ob1.hgst.iphmx.com with ESMTP; 09 Oct 2018 02:25:18 +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=QIktwMJJpqvBmRPNYoZelsSC0M5o4ezrU+aeRRD6bnw=; b=l7pG7j3wDJOWNDUiEgVg8jftCFTPanv7ENWF4MFhA33iw2gaM7pDlcRN11d+fw9/zMxkEw46+3cQcuMNUDPyp7OZDbngqNTzv+w405W3fmyy74H00hgf2Ax8tHITVC81DsZt25wk2O8Aek+jFNwjVHbtGzYeDxoWH4cju/59ByU= Received: from MWHPR04MB0401.namprd04.prod.outlook.com (10.173.48.18) by MWHPR04MB0320.namprd04.prod.outlook.com (10.169.202.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1207.23; Mon, 8 Oct 2018 18:25:17 +0000 Received: from MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::f94c:102e:8dce:7c02]) by MWHPR04MB0401.namprd04.prod.outlook.com ([fe80::f94c:102e:8dce:7c02%6]) with mapi id 15.20.1207.024; Mon, 8 Oct 2018 18:25:17 +0000 From: Alistair Francis To: "qemu-devel@nongnu.org" , "mjc@sifive.com" Thread-Topic: [PATCH v1 1/5] RISC-V: Allow setting and clearing multiple irqs Thread-Index: AQHUXzRDr2yivg1SZU+oCISdAR9dBA== Date: Mon, 8 Oct 2018 18:25:17 +0000 Message-ID: <15533cf04f1eaa150c7b3839c39d3d81978c14e6.1539023064.git.alistair.francis@wdc.com> References: In-Reply-To: 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: BYAPR07CA0062.namprd07.prod.outlook.com (2603:10b6:a03:60::39) To MWHPR04MB0401.namprd04.prod.outlook.com (2603:10b6:300:70::18) 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; MWHPR04MB0320; 6:xUlDXWwLbR/Nz2pQeo8FcNnM3R7tY71FLBQdNnc1UaGyTHkcf2YiSl+1Tw65jtAYO+qyzu8U4LyVoEj0HuwszoEbGdCMhAc05vQaFJm6DbwkRKXkgRGXHhmOqYh4BBkVVOXYkuFtunM6bXBPn4nxlezGQFCpFodUlOH62RHCc4gUpnA3b6vSOlWpVeiGmgopOHaJ0bWHJcqA3odsqCCgoRfLwpwnXqfrs5kMIEpqAZX1iw3jBOjtTnaK+eYTheyF/l01bSBXp/IKpd18y4LC0dIPtuDQmPvgNRVh9ZA/dIhMhLrtMYhrsfwyRa5V5JHoMFohEU5ay193gp9CVBbDErGENbPTzAS6ZDyqkWmBBoxrptyB72OxQhGEUsKGn1wlpjYf25yZb1fVbIFQimVIEDIoxargWxU4tsu5XTw0hB0pKnPgt+n6pFypbEKPrYA/sGTt1xSnC7k+mA1fmlnC3A==; 5:BDRHMiou/24IqPsYV9ZlzCXnb5Ch65/QbEP9wR9djjCArdWmNie5Np4+oCagR+6LqVMOqhJ0PPh3ZVCLI7PbKkp9L6Rgvw5cRUCJV61J+EJWPtDcE54a7IMG86Zbb3yMoPtw5q8/7RFAlcdE5KrKI4JNT2Uc7Fv1TTKv316+bnU=; 7:TwLRaWJgVS1gZlpFV+zBv5sUoX4hHXYdxad7uLlMW0XHs18S2PKwwkSumZ3ClZwra6cxDYQcvtPoARlkmuF2+abqJWznlNHpSTyQyZKEbJoVGR4DOUdrA+Y8zlrHB/G+xS+r8JUzkNCg+U1CB115A8rVhYQZHbnTN6RVpPgBJQpP7lZuGsyLXkYW7g0611SR3rPSK0uYdMZ81970BfVGnlIZCWJNtea+nJLUjH+Xs4JFG3ntXYFMkCtbwduJ5UJl x-ms-office365-filtering-correlation-id: 2d7ddc9a-7f0c-4e43-aeef-08d62d4b65ad 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:MWHPR04MB0320; x-ms-traffictypediagnostic: MWHPR04MB0320: 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)(20161123560045)(20161123562045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(201708071742011)(7699051); SRVR:MWHPR04MB0320; BCL:0; PCL:0; RULEID:; SRVR:MWHPR04MB0320; x-forefront-prvs: 081904387B x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(396003)(136003)(376002)(346002)(366004)(39860400002)(199004)(189003)(106356001)(186003)(102836004)(26005)(71200400001)(5660300001)(71190400001)(68736007)(6116002)(81156014)(118296001)(2906002)(50226002)(25786009)(305945005)(3846002)(7736002)(8676002)(478600001)(39060400002)(105586002)(8936002)(81166006)(2501003)(14454004)(6436002)(54906003)(110136005)(86362001)(6486002)(316002)(14444005)(53936002)(99286004)(386003)(52116002)(6506007)(76176011)(446003)(6512007)(44832011)(476003)(36756003)(5250100002)(11346002)(486006)(72206003)(4326008)(66066001)(97736004)(2616005)(256004)(2900100001); DIR:OUT; SFP:1102; SCL:1; SRVR:MWHPR04MB0320; H:MWHPR04MB0401.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-microsoft-antispam-message-info: JbN8caKeflr3GsGvcVIqf+1A/KiGEc2GlUUYJUgjd1A/qr1CftTWYj59nilmx76p9LFjRKTGVcrjeBW3pFfxz1KTW4dMHOSKLcTKkKvz1b3r9CCpMZaZmRawp6OZ1OCRR8nDzBYzRFoHNgPViO6IpR8EFMMLyibk1No0lsKOY6Y2PWSAMHSzZZMbLdq7j+eSIr5/OiQcdm1KWLMoA6expXIr7MbvYcSBvL04ykvb7Ld24qszdg/m3ihULI64MhmbqS9D7IZ1XY6Fs3bBC5a2DsOQBWWccCx3Yt/JFVMMkgMR7aGqrnRlGUmhTOfibOsIJK7yjjoSvUriQfpjR8p2x6oWHUuYjqDB2Y5e8/IuiiY= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2d7ddc9a-7f0c-4e43-aeef-08d62d4b65ad X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Oct 2018 18:25:17.5846 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR04MB0320 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 216.71.153.144 Subject: [Qemu-devel] [PATCH v1 1/5] 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: "alistair23@gmail.com" , 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 --- 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)