Merge tag 'audit-pr-20190305' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoor...
[sfrench/cifs-2.6.git] / arch / riscv / kernel / cpufeature.c
1 /*
2  * Copied from arch/arm64/kernel/cpufeature.c
3  *
4  * Copyright (C) 2015 ARM Ltd.
5  * Copyright (C) 2017 SiFive
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/of.h>
21 #include <asm/processor.h>
22 #include <asm/hwcap.h>
23
24 unsigned long elf_hwcap __read_mostly;
25 #ifdef CONFIG_FPU
26 bool has_fpu __read_mostly;
27 #endif
28
29 void riscv_fill_hwcap(void)
30 {
31         struct device_node *node = NULL;
32         const char *isa;
33         size_t i;
34         static unsigned long isa2hwcap[256] = {0};
35
36         isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
37         isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
38         isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
39         isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
40         isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
41         isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
42
43         elf_hwcap = 0;
44
45         /*
46          * We don't support running Linux on hertergenous ISA systems.  For
47          * now, we just check the ISA of the first "okay" processor.
48          */
49         while ((node = of_find_node_by_type(node, "cpu")))
50                 if (riscv_of_processor_hartid(node) >= 0)
51                         break;
52         if (!node) {
53                 pr_warning("Unable to find \"cpu\" devicetree entry");
54                 return;
55         }
56
57         if (of_property_read_string(node, "riscv,isa", &isa)) {
58                 pr_warning("Unable to find \"riscv,isa\" devicetree entry");
59                 of_node_put(node);
60                 return;
61         }
62         of_node_put(node);
63
64         for (i = 0; i < strlen(isa); ++i)
65                 elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])];
66
67         /* We don't support systems with F but without D, so mask those out
68          * here. */
69         if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
70                 pr_info("This kernel does not support systems with F but not D");
71                 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
72         }
73
74         pr_info("elf_hwcap is 0x%lx", elf_hwcap);
75
76 #ifdef CONFIG_FPU
77         if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
78                 has_fpu = true;
79 #endif
80 }