6e368d3f663120ad64ce7c408b872fae4ca99ae2
[platform/kernel/linux-starfive.git] / arch / riscv / include / asm / hwcap.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copied from arch/arm64/include/asm/hwcap.h
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  * Copyright (C) 2017 SiFive
7  */
8 #ifndef _ASM_RISCV_HWCAP_H
9 #define _ASM_RISCV_HWCAP_H
10
11 #include <asm/alternative-macros.h>
12 #include <asm/errno.h>
13 #include <linux/bits.h>
14 #include <uapi/asm/hwcap.h>
15
16 #define RISCV_ISA_EXT_a         ('a' - 'a')
17 #define RISCV_ISA_EXT_c         ('c' - 'a')
18 #define RISCV_ISA_EXT_d         ('d' - 'a')
19 #define RISCV_ISA_EXT_f         ('f' - 'a')
20 #define RISCV_ISA_EXT_h         ('h' - 'a')
21 #define RISCV_ISA_EXT_i         ('i' - 'a')
22 #define RISCV_ISA_EXT_m         ('m' - 'a')
23 #define RISCV_ISA_EXT_s         ('s' - 'a')
24 #define RISCV_ISA_EXT_u         ('u' - 'a')
25
26 /*
27  * Increse this to higher value as kernel support more ISA extensions.
28  */
29 #define RISCV_ISA_EXT_MAX       64
30 #define RISCV_ISA_EXT_NAME_LEN_MAX 32
31
32 /* The base ID for multi-letter ISA extensions */
33 #define RISCV_ISA_EXT_BASE 26
34
35 /*
36  * These macros represent the logical ID for each multi-letter RISC-V ISA extension.
37  * The logical ID should start from RISCV_ISA_EXT_BASE and must not exceed
38  * RISCV_ISA_EXT_MAX. 0-25 range is reserved for single letter
39  * extensions while all the multi-letter extensions should define the next
40  * available logical extension id.
41  * Entries are sorted alphabetically.
42  */
43 #define RISCV_ISA_EXT_SSCOFPMF         26
44 #define RISCV_ISA_EXT_SSTC             27
45 #define RISCV_ISA_EXT_SVINVAL          28
46 #define RISCV_ISA_EXT_SVNAPOT          29
47 #define RISCV_ISA_EXT_SVPBMT           30
48 #define RISCV_ISA_EXT_ZBB              31
49 #define RISCV_ISA_EXT_ZICBOM           32
50 #define RISCV_ISA_EXT_ZIHINTPAUSE      33
51
52 #ifndef __ASSEMBLY__
53
54 #include <linux/jump_label.h>
55
56 /*
57  * This yields a mask that user programs can use to figure out what
58  * instruction set this cpu supports.
59  */
60 #define ELF_HWCAP               (elf_hwcap)
61
62 enum {
63         CAP_HWCAP = 1,
64 };
65
66 extern unsigned long elf_hwcap;
67
68 struct riscv_isa_ext_data {
69         /* Name of the extension displayed to userspace via /proc/cpuinfo */
70         char uprop[RISCV_ISA_EXT_NAME_LEN_MAX];
71         /* The logical ISA extension ID */
72         unsigned int isa_ext_id;
73 };
74
75 static __always_inline bool
76 riscv_has_extension_likely(const unsigned long ext)
77 {
78         compiletime_assert(ext < RISCV_ISA_EXT_MAX,
79                            "ext must be < RISCV_ISA_EXT_MAX");
80
81         asm_volatile_goto(
82         ALTERNATIVE("j  %l[l_no]", "nop", 0, %[ext], 1)
83         :
84         : [ext] "i" (ext)
85         :
86         : l_no);
87
88         return true;
89 l_no:
90         return false;
91 }
92
93 static __always_inline bool
94 riscv_has_extension_unlikely(const unsigned long ext)
95 {
96         compiletime_assert(ext < RISCV_ISA_EXT_MAX,
97                            "ext must be < RISCV_ISA_EXT_MAX");
98
99         asm_volatile_goto(
100         ALTERNATIVE("nop", "j   %l[l_yes]", 0, %[ext], 1)
101         :
102         : [ext] "i" (ext)
103         :
104         : l_yes);
105
106         return false;
107 l_yes:
108         return true;
109 }
110
111 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap);
112
113 #define riscv_isa_extension_mask(ext) BIT_MASK(RISCV_ISA_EXT_##ext)
114
115 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit);
116 #define riscv_isa_extension_available(isa_bitmap, ext)  \
117         __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_##ext)
118
119 #endif
120
121 #endif /* _ASM_RISCV_HWCAP_H */