1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2012 Regents of the University of California
4 *
5 * Derived from arch/x86/include/asm/word-at-a-time.h
6 */
7
8 #ifndef _ASM_RISCV_WORD_AT_A_TIME_H
9 #define _ASM_RISCV_WORD_AT_A_TIME_H
10
11
12 #include <asm/asm-extable.h>
13 #include <linux/kernel.h>
14
15 struct word_at_a_time {
16 const unsigned long one_bits, high_bits;
17 };
18
19 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
20
has_zero(unsigned long val,unsigned long * bits,const struct word_at_a_time * c)21 static inline unsigned long has_zero(unsigned long val,
22 unsigned long *bits, const struct word_at_a_time *c)
23 {
24 unsigned long mask = ((val - c->one_bits) & ~val) & c->high_bits;
25 *bits = mask;
26 return mask;
27 }
28
prep_zero_mask(unsigned long val,unsigned long bits,const struct word_at_a_time * c)29 static inline unsigned long prep_zero_mask(unsigned long val,
30 unsigned long bits, const struct word_at_a_time *c)
31 {
32 return bits;
33 }
34
create_zero_mask(unsigned long bits)35 static inline unsigned long create_zero_mask(unsigned long bits)
36 {
37 bits = (bits - 1) & ~bits;
38 return bits >> 7;
39 }
40
find_zero(unsigned long mask)41 static inline unsigned long find_zero(unsigned long mask)
42 {
43 return fls64(mask) >> 3;
44 }
45
46 /* The mask we created is directly usable as a bytemask */
47 #define zero_bytemask(mask) (mask)
48
49 #ifdef CONFIG_DCACHE_WORD_ACCESS
50
51 /*
52 * Load an unaligned word from kernel space.
53 *
54 * In the (very unlikely) case of the word being a page-crosser
55 * and the next page not being mapped, take the exception and
56 * return zeroes in the non-existing part.
57 */
load_unaligned_zeropad(const void * addr)58 static inline unsigned long load_unaligned_zeropad(const void *addr)
59 {
60 unsigned long ret;
61
62 /* Load word from unaligned pointer addr */
63 asm(
64 "1: " REG_L " %0, %2\n"
65 "2:\n"
66 _ASM_EXTABLE_LOAD_UNALIGNED_ZEROPAD(1b, 2b, %0, %1)
67 : "=&r" (ret)
68 : "r" (addr), "m" (*(unsigned long *)addr));
69
70 return ret;
71 }
72
73 #endif /* CONFIG_DCACHE_WORD_ACCESS */
74
75 #endif /* _ASM_RISCV_WORD_AT_A_TIME_H */
76