Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arch/riscv/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ config RISCV
select HAS_IOPORT if MMU
select HAVE_ALIGNED_STRUCT_PAGE
select HAVE_ARCH_AUDITSYSCALL
select HAVE_ARCH_BITREVERSE if RISCV_ISA_ZBKB
select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP
select HAVE_ARCH_HUGE_VMAP if MMU && 64BIT
select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
Expand Down Expand Up @@ -207,6 +208,7 @@ config RISCV
select LOCK_MM_AND_FIND_VMA
select MMU_GATHER_RCU_TABLE_FREE if SMP && MMU
select MODULES_USE_ELF_RELA if MODULES
select NEED_BYTE_REVERSE_TABLE
select OF
select OF_EARLY_FLATTREE
select OF_IRQ
Expand Down
51 changes: 51 additions & 0 deletions arch/riscv/include/asm/bitrev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_BITREV_H
#define __ASM_BITREV_H

#include <linux/types.h>
#include <asm/cpufeature-macros.h>
#include <asm/hwcap.h>
#include <asm-generic/bitops/__bitrev.h>

static __always_inline __attribute_const__ u32 __arch_bitrev32(u32 x)
{
unsigned long result;

if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZBKB))
return generic___bitrev32(x);

asm volatile(
".option push\n"
".option arch,+zbkb\n"
"rev8 %0, %1\n"
"brev8 %0, %0\n"
".option pop"
: "=r" (result) : "r" ((long)x)
);

return result >> (__riscv_xlen - 32);
}

static __always_inline __attribute_const__ u16 __arch_bitrev16(u16 x)
{
return __arch_bitrev32(x) >> 16;
}

static __always_inline __attribute_const__ u8 __arch_bitrev8(u8 x)
{
unsigned long result;

if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZBKB))
return generic___bitrev8(x);

asm volatile(
".option push\n"
".option arch,+zbkb\n"
"brev8 %0, %1\n"
".option pop"
: "=r" (result) : "r" ((long)x)
);

return result;
}
#endif
25 changes: 25 additions & 0 deletions include/asm-generic/bitops/__bitrev.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_GENERIC_BITOPS___BITREV_H_
#define _ASM_GENERIC_BITOPS___BITREV_H_

#ifdef CONFIG_NEED_BYTE_REVERSE_TABLE
#include <asm/types.h>

extern u8 const byte_rev_table[256];
static __always_inline __attribute_const__ u8 generic___bitrev8(u8 byte)
{
return byte_rev_table[byte];
}

static __always_inline __attribute_const__ u16 generic___bitrev16(u16 x)
{
return (generic___bitrev8(x & 0xff) << 8) | generic___bitrev8(x >> 8);
}

static __always_inline __attribute_const__ u32 generic___bitrev32(u32 x)
{
return (generic___bitrev16(x & 0xffff) << 16) | generic___bitrev16(x >> 16);
}
#endif /* CONFIG_NEED_BYTE_REVERSE_TABLE */

#endif /* _ASM_GENERIC_BITOPS___BITREV_H_ */
20 changes: 4 additions & 16 deletions include/linux/bitrev.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@
#define __bitrev8 __arch_bitrev8

#else
extern u8 const byte_rev_table[256];
static inline u8 __bitrev8(u8 byte)
{
return byte_rev_table[byte];
}

static inline u16 __bitrev16(u16 x)
{
return (__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8);
}

static inline u32 __bitrev32(u32 x)
{
return (__bitrev16(x & 0xffff) << 16) | __bitrev16(x >> 16);
}

#include <asm-generic/bitops/__bitrev.h>
#define __bitrev32 generic___bitrev32
#define __bitrev16 generic___bitrev16
#define __bitrev8 generic___bitrev8
#endif /* CONFIG_HAVE_ARCH_BITREVERSE */

#define __bitrev8x4(x) (__bitrev32(swab32(x)))
Expand Down
4 changes: 4 additions & 0 deletions lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ config HAVE_ARCH_BITREVERSE
This option enables the use of hardware bit-reversal instructions on
architectures which support such operations.

config NEED_BYTE_REVERSE_TABLE
bool
default y if !HAVE_ARCH_BITREVERSE

config ARCH_HAS_STRNCPY_FROM_USER
bool

Expand Down
4 changes: 2 additions & 2 deletions lib/bitrev.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only
#ifndef CONFIG_HAVE_ARCH_BITREVERSE
#ifdef CONFIG_NEED_BYTE_REVERSE_TABLE
#include <linux/types.h>
#include <linux/module.h>
#include <linux/bitrev.h>
Expand Down Expand Up @@ -44,4 +44,4 @@ const u8 byte_rev_table[256] = {
};
EXPORT_SYMBOL_GPL(byte_rev_table);

#endif /* CONFIG_HAVE_ARCH_BITREVERSE */
#endif /* CONFIG_NEED_BYTE_REVERSE_TABLE */
Loading