Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 22 Mar 2017 17:33:57 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r315719 - head/sys/compat/linuxkpi/common/include/asm
Message-ID:  <201703221733.v2MHXvUG023871@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Wed Mar 22 17:33:57 2017
New Revision: 315719
URL: https://svnweb.freebsd.org/changeset/base/315719

Log:
  Extend cmpxchg() to support 8- and 16-bit values, and add xchg().
  
  These are needed to support updated revisions of the DRM code.
  
  Reviewed by:	hselasky (previous version)
  MFC after:	2 weeks

Modified:
  head/sys/compat/linuxkpi/common/include/asm/atomic.h

Modified: head/sys/compat/linuxkpi/common/include/asm/atomic.h
==============================================================================
--- head/sys/compat/linuxkpi/common/include/asm/atomic.h	Wed Mar 22 17:29:04 2017	(r315718)
+++ head/sys/compat/linuxkpi/common/include/asm/atomic.h	Wed Mar 22 17:33:57 2017	(r315719)
@@ -28,11 +28,13 @@
  *
  * $FreeBSD$
  */
-#ifndef	_ASM_ATOMIC_H_
+
+#ifndef _ASM_ATOMIC_H_
 #define	_ASM_ATOMIC_H_
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
+
 #include <machine/atomic.h>
 
 #define	ATOMIC_INIT(x)	{ .counter = (x) }
@@ -158,31 +160,47 @@ atomic_cmpxchg(atomic_t *v, int old, int
 }
 
 #define	cmpxchg(ptr, old, new) ({				\
-	__typeof(*(ptr)) __ret = (old);				\
-	CTASSERT(sizeof(__ret) == 4 || sizeof(__ret) == 8);	\
-	for (;;) {						\
-		if (sizeof(__ret) == 4) {			\
-			if (atomic_cmpset_int((volatile int *)	\
-			    (ptr), (old), (new)))		\
-				break;				\
-			__ret = atomic_load_acq_int(		\
-			    (volatile int *)(ptr));		\
-			if (__ret != (old))			\
-				break;				\
-		} else {					\
-			if (atomic_cmpset_64(			\
-			    (volatile int64_t *)(ptr),		\
-			    (old), (new)))			\
-				break;				\
-			__ret = atomic_load_acq_64(		\
-			    (volatile int64_t *)(ptr));		\
-			if (__ret != (old))			\
-				break;				\
-		}						\
+	__typeof(*(ptr)) __ret;					\
+								\
+	CTASSERT(sizeof(__ret) == 1 || sizeof(__ret) == 2 ||	\
+	    sizeof(__ret) == 4 || sizeof(__ret) == 8);		\
+								\
+	__ret = (old);						\
+	switch (sizeof(__ret)) {				\
+	case 1:							\
+		while (!atomic_fcmpset_8((volatile int8_t *)(ptr), \
+		    (int8_t *)&__ret, (new)) && __ret == (old))	\
+			;					\
+		break;						\
+	case 2:							\
+		while (!atomic_fcmpset_16((volatile int16_t *)(ptr), \
+		    (int16_t *)&__ret, (new)) && __ret == (old)) \
+			;					\
+		break;						\
+	case 4:							\
+		while (!atomic_fcmpset_32((volatile int32_t *)(ptr), \
+		    (int32_t *)&__ret, (new)) && __ret == (old)) \
+			;					\
+		break;						\
+	case 8:							\
+		while (!atomic_fcmpset_64((volatile int64_t *)(ptr), \
+		    (int64_t *)&__ret, (new)) && __ret == (old)) \
+			;					\
+		break;						\
 	}							\
 	__ret;							\
 })
 
+#define	cmpxchg_relaxed	cmpxchg
+
+#define	xchg(ptr, v) ({						\
+	__typeof(*(ptr)) __ret;					\
+								\
+	__ret = *(ptr);						\
+	*(ptr) = v;						\
+	__ret;							\
+})
+
 #define	LINUX_ATOMIC_OP(op, c_op)				\
 static inline void atomic_##op(int i, atomic_t *v)		\
 {								\



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201703221733.v2MHXvUG023871>