cpufeature.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <linux/of.h>
  20. #include <asm/processor.h>
  21. #include <asm/hwcap.h>
  22. unsigned long elf_hwcap __read_mostly;
  23. #ifdef CONFIG_FPU
  24. bool has_fpu __read_mostly;
  25. #endif
  26. void riscv_fill_hwcap(void)
  27. {
  28. struct device_node *node = NULL;
  29. const char *isa;
  30. size_t i;
  31. static unsigned long isa2hwcap[256] = {0};
  32. isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
  33. isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
  34. isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
  35. isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
  36. isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
  37. isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
  38. elf_hwcap = 0;
  39. /*
  40. * We don't support running Linux on hertergenous ISA systems. For
  41. * now, we just check the ISA of the first "okay" processor.
  42. */
  43. while ((node = of_find_node_by_type(node, "cpu")))
  44. if (riscv_of_processor_hartid(node) >= 0)
  45. break;
  46. if (!node) {
  47. pr_warning("Unable to find \"cpu\" devicetree entry");
  48. return;
  49. }
  50. if (of_property_read_string(node, "riscv,isa", &isa)) {
  51. pr_warning("Unable to find \"riscv,isa\" devicetree entry");
  52. return;
  53. }
  54. for (i = 0; i < strlen(isa); ++i)
  55. elf_hwcap |= isa2hwcap[(unsigned char)(isa[i])];
  56. /* We don't support systems with F but without D, so mask those out
  57. * here. */
  58. if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
  59. pr_info("This kernel does not support systems with F but not D");
  60. elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
  61. }
  62. pr_info("elf_hwcap is 0x%lx", elf_hwcap);
  63. #ifdef CONFIG_FPU
  64. if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
  65. has_fpu = true;
  66. #endif
  67. }