Received: by 10.223.164.202 with SMTP id h10csp1606040wrb; Mon, 27 Nov 2017 05:08:57 -0800 (PST) X-Google-Smtp-Source: AGs4zMZfMD7n6iB+kWvm+s2jKetBY7mSV7xuXqOJsYYEFGEQjcH7RJzpJkH+jEW5K71E1KlA9j47 X-Received: by 10.84.174.67 with SMTP id q61mr39184818plb.184.1511788137450; Mon, 27 Nov 2017 05:08:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511788137; cv=none; d=google.com; s=arc-20160816; b=QMPJVVpUrWBE5+oI81FGRMEMc8ByC18EjF54qzvNKlwghSucSaE7eUMA2bhZ4sBkjN CTJQkOrJf3uHRhwaS1x6ncsD8rd3YgjbKRMB8eSHDu2JPP32ZSfRAVjkv7HiRE8EgRUi 4IefMqaFwsk+RLJBy7DmZbmJQ5KW/lhMXJklC0MqqPtjZ1f0aUa/1Jzoeq6O3UCD66nW 3OL4qbsHu5G10zXTDyj1aVmF+qfNEcgF8Dgq0B8urnZURTyer9VUooxkrd0D4xFvOVt5 LlWV6QagBnAm60tO7dtAXOvGCVL4sFbSMKUZQcwiRmjr7bXKiU2rdtchDZic4aI72sEh YEbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=PUTRcDhE8Y8Rabye8lwaOxWOgOObbsR7rZO86u8I7ts=; b=Qo6IqyMB/f36iniju+3MPmFHOrRLbSEgEsZqumkohIHZsMHRyHPkLGjCriEj/2OZmN TkS0/v2HpB9PPXixrxJhkiIKQRTC0zNe2t1FB3m7/2KSLdK3zGqPjs1UmiUWqrOP5rXv HlF5W2wpX/PqSZvHcCJmhL4r8P11x/xMcopVBhC/IBLk61TOrHVvlc/3vVIfMIa3Tl6a f61qmvfcfWwG597HlpgLr8ShzpCJ3j3BksDHBLMn+nRmpWMMIfQ0hKCrXtDFhHKvGVlp 9b75JrLWThok6yZxNQjWC7NAn4NxN52ASM8XQTpQ0Z9kQ33CjqP6nDlQYNQCVdnzzYxX DDDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MdQ69qkp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e10si19693458pgp.680.2017.11.27.05.08.44; Mon, 27 Nov 2017 05:08:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MdQ69qkp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752694AbdK0NHe (ORCPT + 77 others); Mon, 27 Nov 2017 08:07:34 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:37149 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752379AbdK0Mx5 (ORCPT ); Mon, 27 Nov 2017 07:53:57 -0500 Received: by mail-pg0-f67.google.com with SMTP id m4so8711381pgc.4; Mon, 27 Nov 2017 04:53:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=PUTRcDhE8Y8Rabye8lwaOxWOgOObbsR7rZO86u8I7ts=; b=MdQ69qkpfRPdn0nPLSNnjze4iaX+5MRHO0vwBcw/p2Uc7DwvkSBh8ELeEFaYOq+iXS HcqU2CToItlG2PweRlKrViDNb9uTrGCie3oOfaSdMRb1iRizeJovMGJoui0YmiZmgXIy 1uiRUs2k5BEcn16VNDifM91RKf36XPRGlSy/RxERk6Cux2YcRbiZvCSg5VWYVRI+UhLi AAF3iabupwHF63v86WewxHsfAxjhhDp8kR4Fa5xv074jrAlDLw2wKvQupDdjEpkJUvkD 6Iye16phQ60+SwxlFQrhCriLwFSG4Gh9h3B6qrRHzFwey3fPWeCGqdBbhHhDrv5Bhn5M OdpA== 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:in-reply-to:references; bh=PUTRcDhE8Y8Rabye8lwaOxWOgOObbsR7rZO86u8I7ts=; b=GTWkexS49r7WgC575DKXNKinJbGTfQtFhrItENEOCqBDo0r3qDtlT4d18LuJQpmJsP WDwj2mYcb3XcLoyiGtX0ZveNOTtRPjbkT8c2YS2WlS+jS3ua2kyxONMQ9spg1NGPzTjC OmgiH7e5AdbRE3Pe83VtCBHREQV5noOcRVYhowOHykQH4zJ/0ubudvb+U9KJt1m0qj9r dLPzs9qorjtYBnmukuOUJcnuDD3oeu6OxXiqadJZ8sKJu3wipnC7JTTfKjajEA0k/67u PckrNijaPM5p6vZ6o2QDo7rCqVVI42bYNSWhBsve1Q4dYqqRVndxLPYbkkQCZviTNszn lasw== X-Gm-Message-State: AJaThX7sP4nEOJH9vZ4rx1l7qIZimh4nHlfulyrMIaPBqhoW1cbVMkCU IesV9S3Ze1sxYqS9+okBKs8= X-Received: by 10.99.127.14 with SMTP id a14mr38026527pgd.315.1511787236545; Mon, 27 Nov 2017 04:53:56 -0800 (PST) Received: from app09.andestech.com ([118.163.51.199]) by smtp.gmail.com with ESMTPSA id w64sm55225459pfj.62.2017.11.27.04.53.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Nov 2017 04:53:55 -0800 (PST) From: Greentime Hu To: greentime@andestech.com, linux-kernel@vger.kernel.org, arnd@arndb.de, linux-arch@vger.kernel.org, tglx@linutronix.de, jason@lakedaemon.net, marc.zyngier@arm.com, robh+dt@kernel.org, netdev@vger.kernel.org, deanbo422@gmail.com, devicetree@vger.kernel.org, viro@zeniv.linux.org.uk, dhowells@redhat.com, will.deacon@arm.com, daniel.lezcano@linaro.org, linux-serial@vger.kernel.org Cc: green.hu@gmail.com, Vincent Chen Subject: [PATCH v2 10/35] nds32: Atomic operations Date: Mon, 27 Nov 2017 20:27:57 +0800 Message-Id: <56a24fae3cec68f80c41eebd98fcceb9b137ac64.1511785528.git.green.hu@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greentime Hu This patch includes the atomic and futex operations. Many atomic operations use the load-lock word(llw) and store-condition word(scw) operations. Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu --- arch/nds32/include/asm/futex.h | 116 +++++++++++++++++++++++ arch/nds32/include/asm/spinlock.h | 184 +++++++++++++++++++++++++++++++++++++ 2 files changed, 300 insertions(+) create mode 100644 arch/nds32/include/asm/futex.h create mode 100644 arch/nds32/include/asm/spinlock.h diff --git a/arch/nds32/include/asm/futex.h b/arch/nds32/include/asm/futex.h new file mode 100644 index 0000000..5aa107c --- /dev/null +++ b/arch/nds32/include/asm/futex.h @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2005-2017 Andes Technology Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __NDS32_FUTEX_H__ +#define __NDS32_FUTEX_H__ + +#include +#include +#include + +#define __futex_atomic_ex_table(err_reg) \ + " .pushsection __ex_table,\"a\"\n" \ + " .align 3\n" \ + " .long 1b, 4f\n" \ + " .long 2b, 4f\n" \ + " .popsection\n" \ + " .pushsection .fixup,\"ax\"\n" \ + "4: move %0, " err_reg "\n" \ + " j 3b\n" \ + " .popsection" + +#define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \ + smp_mb(); \ + asm volatile( \ + " movi $ta, #0\n" \ + "1: llw %1, [%2+$ta]\n" \ + " " insn "\n" \ + "2: scw %0, [%2+$ta]\n" \ + " beqz %0, 1b\n" \ + " movi %0, #0\n" \ + "3:\n" \ + __futex_atomic_ex_table("%4") \ + : "=&r" (ret), "=&r" (oldval) \ + : "r" (uaddr), "r" (oparg), "i" (-EFAULT) \ + : "cc", "memory") +static inline int +futex_atomic_cmpxchg_inatomic(u32 * uval, u32 __user * uaddr, + u32 oldval, u32 newval) +{ + int ret = 0; + u32 val, tmp, flags; + + if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) + return -EFAULT; + + smp_mb(); + asm volatile (" movi $ta, #0\n" + "1: llw %1, [%6 + $ta]\n" + " sub %3, %1, %4\n" + " cmovz %2, %5, %3\n" + " cmovn %2, %1, %3\n" + "2: scw %2, [%6 + $ta]\n" + " beqz %2, 1b\n" + "3:\n " __futex_atomic_ex_table("%7") + :"+&r"(ret), "=&r"(val), "=&r"(tmp), "=&r"(flags) + :"r"(oldval), "r"(newval), "r"(uaddr), "i"(-EFAULT) + :"$ta", "memory"); + smp_mb(); + + *uval = val; + return ret; +} + +static inline int +arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr) +{ + int oldval = 0, ret; + + + pagefault_disable(); + switch (op) { + case FUTEX_OP_SET: + __futex_atomic_op("move %0, %3", ret, oldval, tmp, uaddr, + oparg); + break; + case FUTEX_OP_ADD: + __futex_atomic_op("add %0, %1, %3", ret, oldval, tmp, uaddr, + oparg); + break; + case FUTEX_OP_OR: + __futex_atomic_op("or %0, %1, %3", ret, oldval, tmp, uaddr, + oparg); + break; + case FUTEX_OP_ANDN: + __futex_atomic_op("and %0, %1, %3", ret, oldval, tmp, uaddr, + ~oparg); + break; + case FUTEX_OP_XOR: + __futex_atomic_op("xor %0, %1, %3", ret, oldval, tmp, uaddr, + oparg); + break; + default: + ret = -ENOSYS; + } + + pagefault_enable(); + + if (!ret) + *oval = oldval; + + return ret; +} +#endif /* __NDS32_FUTEX_H__ */ diff --git a/arch/nds32/include/asm/spinlock.h b/arch/nds32/include/asm/spinlock.h new file mode 100644 index 0000000..ca70437 --- /dev/null +++ b/arch/nds32/include/asm/spinlock.h @@ -0,0 +1,184 @@ +/* + * Copyright (C) 2005-2017 Andes Technology Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ASM_SPINLOCK_H +#define __ASM_SPINLOCK_H + +#include + +#define arch_spin_is_locked(x) ((x)->lock != 0) + +#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock) + +static inline void arch_spin_lock(arch_spinlock_t * lock) +{ + unsigned long tmp; + + asm volatile( + "1:\n" + "llw %0, [%1]\n" + "bnez %0, 1b\n" + "movi %0, #0x1\n" + "scw %0, [%1]\n" + "beqz %0, 1b\n" + :"=&r"(tmp) + :"r"(&lock->lock) + :"memory"); +} + +static inline int arch_spin_trylock(arch_spinlock_t * lock) +{ + unsigned long ret, tmp; + + asm volatile( + "1:\n" + "llw %0, [%2]\n" + "movi %1, #0x1\n" + "scw %1, [%2]\n" + "beqz %1, 1b\n" + :"=&r"(ret), "=&r"(tmp) + :"r"(&lock->lock) + :"memory"); + + return ret == 0; +} + +static inline void arch_spin_unlock(arch_spinlock_t * lock) +{ + asm volatile( + "xor $r15, $r15, $r15\n" + "swi $r15, [%0]\n" + : + :"r"(&lock->lock) + :"memory"); +} + +static inline void arch_write_lock(arch_rwlock_t * rw) +{ + unsigned long tmp; + + asm volatile( + "1:\n" + "llw %0, [%1]\n" + "bnez %0, 1b\n" + "sethi %0, 0x80000\n" + "scw %0, [%1]\n" + "beqz %0, 1b\n" + :"=&r"(tmp) + :"r"(&rw->lock) + :"memory"); +} + +static inline void arch_write_unlock(arch_rwlock_t * rw) +{ + asm volatile( + "xor $r15, $r15, $r15\n" + "swi $r15, [%0]\n" + : + :"r"(&rw->lock) + :"memory","$r15"); +} + +#define arch_write_can_lock(x) ((x)->lock == 0) +static inline void arch_read_lock(arch_rwlock_t * rw) +{ + int tmp; + + asm volatile( + "1:\n" + "llw %0, [%1]\n" + "bltz %0, 1b\n" + "addi %0, %0, #1\n" + "scw %0, [%1]\n" + "beqz %0, 1b\n" + :"=&r"(tmp) + :"r"(&rw->lock) + :"memory"); +} + +static inline void arch_read_unlock(arch_rwlock_t * rw) +{ + unsigned long tmp; + + asm volatile( + "1:\n" + "llw %0, [%1]\n" + "addi %0, %0, #-1\n" + "scw %0, [%1]\n" + "beqz %0, 1b\n" + :"=&r"(tmp) + :"r"(&rw->lock) + :"memory"); +} + +static inline int arch_read_trylock(arch_rwlock_t * rw) +{ + unsigned long ret, tmp; + + asm volatile( + "movi %0, #0x0\n" + "1:\n" + "llw %1, [%2]\n" + "bltz %1, 2f\n" + "addi %1, %1, #1\n" + "scw %1, [%2]\n" + "beqz %1, 1b\n" + "movi %0, #0x1\n" + "j 3f\n" + "2:\n" + "scw %1, [%2]\n" + "3:\n" + :"=&r"(ret), "=&r"(tmp) + :"r"(&rw->lock) + :"memory"); + + return ret; +} + +static inline int arch_write_trylock(arch_rwlock_t * rw) +{ + unsigned long ret, tmp; + + asm volatile( + "movi %0, #0x0\n" + "1:\n" + "llw %1, [%2]\n" + "bnez %1, 2f\n" + "sethi %1, 0x80000\n" + "scw %1, [%2]\n" + "beqz %1, 1b\n" + "movi %0, #0x1\n" + "j 3f\n" + "2:\n" + "scw %1, [%2]\n" + "3:\n" + :"=&r"(ret), "=&r"(tmp) + :"r"(&rw->lock) + :"memory"); + + return ret; +} + +#define arch_read_lock_flags(lock, flags) arch_read_lock(lock) +#define arch_write_lock_flags(lock, flags) arch_write_lock(lock) + +#define arch_read_can_lock(x) ((x)->lock < 0x80000000) + +#define arch_spin_relax(lock) cpu_relax() +#define arch_read_relax(lock) cpu_relax() +#define arch_write_relax(lock) cpu_relax() + +#endif /* __ASM_SPINLOCK_H */ -- 1.7.9.5 From 1585521566125393805@xxx Thu Nov 30 19:46:35 +0000 2017 X-GM-THRID: 1585508715375164061 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread