summaryrefslogtreecommitdiffstats
path: root/src/arch/arm64/include/bits/bitops.h
diff options
context:
space:
mode:
authorMichael Brown2016-05-08 01:20:20 +0200
committerMichael Brown2016-05-08 01:20:20 +0200
commit17c6f322eef5e0a2250a89b140486cf07598d2fa (patch)
treec67442d1e56245b42f9e7b98f9fa0e7b9cc24b1d /src/arch/arm64/include/bits/bitops.h
parent[arm] Split out 32-bit-specific code to arch/arm32 (diff)
downloadipxe-17c6f322eef5e0a2250a89b140486cf07598d2fa.tar.gz
ipxe-17c6f322eef5e0a2250a89b140486cf07598d2fa.tar.xz
ipxe-17c6f322eef5e0a2250a89b140486cf07598d2fa.zip
[arm] Add support for 64-bit ARM (Aarch64)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/arch/arm64/include/bits/bitops.h')
-rw-r--r--src/arch/arm64/include/bits/bitops.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/arch/arm64/include/bits/bitops.h b/src/arch/arm64/include/bits/bitops.h
new file mode 100644
index 000000000..4350f622a
--- /dev/null
+++ b/src/arch/arm64/include/bits/bitops.h
@@ -0,0 +1,100 @@
+#ifndef _BITS_BITOPS_H
+#define _BITS_BITOPS_H
+
+/** @file
+ *
+ * ARM bit operations
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <stdint.h>
+
+/**
+ * Test and set bit atomically
+ *
+ * @v bit Bit to set
+ * @v bits Bit field
+ * @ret old Old value of bit (zero or non-zero)
+ */
+static inline __attribute__ (( always_inline )) int
+test_and_set_bit ( unsigned int bit, volatile void *bits ) {
+ unsigned int index = ( bit / 64 );
+ unsigned int offset = ( bit % 64 );
+ volatile uint64_t *qword = ( ( ( volatile uint64_t * ) bits ) + index );
+ uint64_t mask = ( 1UL << offset );
+ uint64_t old;
+ uint64_t new;
+ uint32_t flag;
+
+ __asm__ __volatile__ ( "\n1:\n\t"
+ "ldxr %0, %3\n\t"
+ "orr %1, %0, %4\n\t"
+ "stxr %w2, %1, %3\n\t"
+ "tst %w2, %w2\n\t"
+ "bne 1b\n\t"
+ : "=&r" ( old ), "=&r" ( new ), "=&r" ( flag ),
+ "+Q" ( *qword )
+ : "r" ( mask )
+ : "cc" );
+
+ return ( !! ( old & mask ) );
+}
+
+/**
+ * Test and clear bit atomically
+ *
+ * @v bit Bit to set
+ * @v bits Bit field
+ * @ret old Old value of bit (zero or non-zero)
+ */
+static inline __attribute__ (( always_inline )) int
+test_and_clear_bit ( unsigned int bit, volatile void *bits ) {
+ unsigned int index = ( bit / 64 );
+ unsigned int offset = ( bit % 64 );
+ volatile uint64_t *qword = ( ( ( volatile uint64_t * ) bits ) + index );
+ uint64_t mask = ( 1UL << offset );
+ uint64_t old;
+ uint64_t new;
+ uint32_t flag;
+
+ __asm__ __volatile__ ( "\n1:\n\t"
+ "ldxr %0, %3\n\t"
+ "bic %1, %0, %4\n\t"
+ "stxr %w2, %1, %3\n\t"
+ "tst %w2, %w2\n\t"
+ "bne 1b\n\t"
+ : "=&r" ( old ), "=&r" ( new ), "=&r" ( flag ),
+ "+Q" ( *qword )
+ : "r" ( mask )
+ : "cc" );
+
+ return ( !! ( old & mask ) );
+}
+
+/**
+ * Set bit atomically
+ *
+ * @v bit Bit to set
+ * @v bits Bit field
+ */
+static inline __attribute__ (( always_inline )) void
+set_bit ( unsigned int bit, volatile void *bits ) {
+
+ test_and_set_bit ( bit, bits );
+}
+
+/**
+ * Clear bit atomically
+ *
+ * @v bit Bit to set
+ * @v bits Bit field
+ */
+static inline __attribute__ (( always_inline )) void
+clear_bit ( unsigned int bit, volatile void *bits ) {
+
+ test_and_clear_bit ( bit, bits );
+}
+
+#endif /* _BITS_BITOPS_H */