cpu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2012 Regents of the University of California
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/of.h>
  16. /* Return -1 if not a valid hart */
  17. int riscv_of_processor_hart(struct device_node *node)
  18. {
  19. const char *isa, *status;
  20. u32 hart;
  21. if (!of_device_is_compatible(node, "riscv")) {
  22. pr_warn("Found incompatible CPU\n");
  23. return -(ENODEV);
  24. }
  25. if (of_property_read_u32(node, "reg", &hart)) {
  26. pr_warn("Found CPU without hart ID\n");
  27. return -(ENODEV);
  28. }
  29. if (hart >= NR_CPUS) {
  30. pr_info("Found hart ID %d, which is above NR_CPUs. Disabling this hart\n", hart);
  31. return -(ENODEV);
  32. }
  33. if (of_property_read_string(node, "status", &status)) {
  34. pr_warn("CPU with hartid=%d has no \"status\" property\n", hart);
  35. return -(ENODEV);
  36. }
  37. if (strcmp(status, "okay")) {
  38. pr_info("CPU with hartid=%d has a non-okay status of \"%s\"\n", hart, status);
  39. return -(ENODEV);
  40. }
  41. if (of_property_read_string(node, "riscv,isa", &isa)) {
  42. pr_warn("CPU with hartid=%d has no \"riscv,isa\" property\n", hart);
  43. return -(ENODEV);
  44. }
  45. if (isa[0] != 'r' || isa[1] != 'v') {
  46. pr_warn("CPU with hartid=%d has an invalid ISA of \"%s\"\n", hart, isa);
  47. return -(ENODEV);
  48. }
  49. return hart;
  50. }
  51. #ifdef CONFIG_PROC_FS
  52. static void *c_start(struct seq_file *m, loff_t *pos)
  53. {
  54. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  55. if ((*pos) < nr_cpu_ids)
  56. return (void *)(uintptr_t)(1 + *pos);
  57. return NULL;
  58. }
  59. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  60. {
  61. (*pos)++;
  62. return c_start(m, pos);
  63. }
  64. static void c_stop(struct seq_file *m, void *v)
  65. {
  66. }
  67. static int c_show(struct seq_file *m, void *v)
  68. {
  69. unsigned long hart_id = (unsigned long)v - 1;
  70. struct device_node *node = of_get_cpu_node(hart_id, NULL);
  71. const char *compat, *isa, *mmu;
  72. seq_printf(m, "hart\t: %lu\n", hart_id);
  73. if (!of_property_read_string(node, "riscv,isa", &isa)
  74. && isa[0] == 'r'
  75. && isa[1] == 'v')
  76. seq_printf(m, "isa\t: %s\n", isa);
  77. if (!of_property_read_string(node, "mmu-type", &mmu)
  78. && !strncmp(mmu, "riscv,", 6))
  79. seq_printf(m, "mmu\t: %s\n", mmu+6);
  80. if (!of_property_read_string(node, "compatible", &compat)
  81. && strcmp(compat, "riscv"))
  82. seq_printf(m, "uarch\t: %s\n", compat);
  83. seq_puts(m, "\n");
  84. return 0;
  85. }
  86. const struct seq_operations cpuinfo_op = {
  87. .start = c_start,
  88. .next = c_next,
  89. .stop = c_stop,
  90. .show = c_show
  91. };
  92. #endif /* CONFIG_PROC_FS */