cpufeature.c 2.28 KB
Newer Older
Palmer Dabbelt's avatar
Palmer Dabbelt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copied from arch/arm64/kernel/cpufeature.c
 *
 * Copyright (C) 2015 ARM Ltd.
 * Copyright (C) 2017 SiFive
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/of.h>
#include <asm/processor.h>
#include <asm/hwcap.h>

unsigned long elf_hwcap __read_mostly;
25 26 27
#ifdef CONFIG_FPU
bool has_fpu __read_mostly;
#endif
Palmer Dabbelt's avatar
Palmer Dabbelt committed
28 29 30

void riscv_fill_hwcap(void)
{
31
	struct device_node *node;
Palmer Dabbelt's avatar
Palmer Dabbelt committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	const char *isa;
	size_t i;
	static unsigned long isa2hwcap[256] = {0};

	isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
	isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
	isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
	isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
	isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
	isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;

	elf_hwcap = 0;

	/*
	 * We don't support running Linux on hertergenous ISA systems.  For
47
	 * now, we just check the ISA of the first "okay" processor.
Palmer Dabbelt's avatar
Palmer Dabbelt committed
48
	 */
49
	for_each_of_cpu_node(node) {
50 51
		if (riscv_of_processor_hartid(node) >= 0)
			break;
52
	}
Palmer Dabbelt's avatar
Palmer Dabbelt committed
53
	if (!node) {
54
		pr_warn("Unable to find \"cpu\" devicetree entry\n");
Palmer Dabbelt's avatar
Palmer Dabbelt committed
55 56 57 58
		return;
	}

	if (of_property_read_string(node, "riscv,isa", &isa)) {
59
		pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
60
		of_node_put(node);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
61 62
		return;
	}
63
	of_node_put(node);
Palmer Dabbelt's avatar
Palmer Dabbelt committed
64 65 66 67

	for (i = 0; i < strlen(isa); ++i)
		elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])];

68 69 70
	/* We don't support systems with F but without D, so mask those out
	 * here. */
	if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
71
		pr_info("This kernel does not support systems with F but not D\n");
72 73 74
		elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
	}

75
	pr_info("elf_hwcap is 0x%lx\n", elf_hwcap);
76 77 78 79 80

#ifdef CONFIG_FPU
	if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
		has_fpu = true;
#endif
Palmer Dabbelt's avatar
Palmer Dabbelt committed
81
}