From ced09b0adbfce30e1923ead11c893abc4c829a0d Mon Sep 17 00:00:00 2001
From: Wenlong Zhang <zhangwenlong@loongson.cn>
Date: Wed, 19 Jan 2022 06:26:16 +0000
Subject: [PATCH] Add support for the LOONGARCH architecture

Signed-off-by: Wenlong Zhang <zhangwenlong@loongson.cn>
---
 include/Makefile.am              |  2 ++
 include/urcu/arch.h              |  5 ++++
 include/urcu/arch/loongarch.h    | 50 ++++++++++++++++++++++++++++++++
 include/urcu/uatomic.h           |  2 ++
 include/urcu/uatomic/loongarch.h | 44 ++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+)
 create mode 100644 include/urcu/arch/loongarch.h
 create mode 100644 include/urcu/uatomic/loongarch.h

diff --git a/include/Makefile.am b/include/Makefile.am
index b55bcf0..91b9161 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -16,6 +16,7 @@ nobase_include_HEADERS = \
 	urcu/arch/sparc64.h \
 	urcu/arch/tile.h \
 	urcu/arch/x86.h \
+	urcu/arch/loongarch.h \
 	urcu/assert.h \
 	urcu/call-rcu.h \
 	urcu/cds.h \
@@ -77,6 +78,7 @@ nobase_include_HEADERS = \
 	urcu/uatomic/sparc64.h \
 	urcu/uatomic/tile.h \
 	urcu/uatomic/x86.h \
+	urcu/uatomic/loongarch.h \
 	urcu/urcu-bp.h \
 	urcu/urcu-futex.h \
 	urcu/urcu.h \
diff --git a/include/urcu/arch.h b/include/urcu/arch.h
index d3914da..393c6b3 100644
--- a/include/urcu/arch.h
+++ b/include/urcu/arch.h
@@ -167,6 +167,11 @@
 #define URCU_ARCH_RISCV 1
 #include <urcu/arch/riscv.h>
 
+#elif defined(__loongarch__) || (_loongarch64)
+
+#define URCU_ARCH_LOONGARCH 1
+#include <urcu/arch/loongarch.h>
+
 #else
 #error "Cannot build: unrecognized architecture, see <urcu/arch.h>."
 #endif
diff --git a/include/urcu/arch/loongarch.h b/include/urcu/arch/loongarch.h
new file mode 100644
index 0000000..865bbb5
--- /dev/null
+++ b/include/urcu/arch/loongarch.h
@@ -0,0 +1,50 @@
+#ifndef _URCU_ARCH_LOONGARCH_H
+#define _URCU_ARCH_LOONGARCH_H
+
+/*
+ * arch/loongarch.h: definitions for the LOONGARCH architecture
+ *
+ * Copyright (c) 2022 Loongson Technology Corporation Limited
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <urcu/compiler.h>
+#include <urcu/config.h>
+#include <urcu/syscall-compat.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+#include <sys/time.h>
+
+/*
+ * On Linux, define the membarrier system call number if not yet available in
+ * the system headers. loongarch64 implements asm-generic/unistd.h system call
+ * numbers.
+ */
+#if (defined(__linux__) && !defined(__NR_membarrier))
+#define __NR_membarrier		283
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#include <urcu/arch/generic.h>
+
+#endif /* _URCU_ARCH_LOONGARCH_H */
diff --git a/include/urcu/uatomic.h b/include/urcu/uatomic.h
index 2fb5fd4..e1ff44d 100644
--- a/include/urcu/uatomic.h
+++ b/include/urcu/uatomic.h
@@ -51,6 +51,8 @@
 #include <urcu/uatomic/m68k.h>
 #elif defined(URCU_ARCH_RISCV)
 #include <urcu/uatomic/riscv.h>
+#elif defined(URCU_ARCH_LOONGARCH)
+#include <urcu/uatomic/loongarch.h>
 #else
 #error "Cannot build: unrecognized architecture, see <urcu/arch.h>."
 #endif
diff --git a/include/urcu/uatomic/loongarch.h b/include/urcu/uatomic/loongarch.h
new file mode 100644
index 0000000..e0fb2a6
--- /dev/null
+++ b/include/urcu/uatomic/loongarch.h
@@ -0,0 +1,44 @@
+/*
+ * Atomic exchange operations for the LOONGARCH architecture. Let GCC do it.
+ *
+ * Copyright (c) 2022 Loongson Technology Corporation Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef _URCU_ARCH_UATOMIC_LOONGARCH_H
+#define _URCU_ARCH_UATOMIC_LOONGARCH_H
+
+#include <urcu/compiler.h>
+#include <urcu/system.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define UATOMIC_HAS_ATOMIC_BYTE
+#define UATOMIC_HAS_ATOMIC_SHORT
+
+#ifdef __cplusplus
+}
+#endif
+
+#include <urcu/uatomic/generic.h>
+
+#endif /* _URCU_ARCH_UATOMIC_LOONGARCH_H */
-- 
2.27.0

