cacheinfo.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2017 SiFive
  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/cacheinfo.h>
  14. #include <linux/cpu.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. static void ci_leaf_init(struct cacheinfo *this_leaf,
  18. struct device_node *node,
  19. enum cache_type type, unsigned int level)
  20. {
  21. this_leaf->level = level;
  22. this_leaf->type = type;
  23. }
  24. static int __init_cache_level(unsigned int cpu)
  25. {
  26. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  27. struct device_node *np = of_cpu_device_node_get(cpu);
  28. int levels = 0, leaves = 0, level;
  29. if (of_property_read_bool(np, "cache-size"))
  30. ++leaves;
  31. if (of_property_read_bool(np, "i-cache-size"))
  32. ++leaves;
  33. if (of_property_read_bool(np, "d-cache-size"))
  34. ++leaves;
  35. if (leaves > 0)
  36. levels = 1;
  37. while ((np = of_find_next_cache_node(np))) {
  38. if (!of_device_is_compatible(np, "cache"))
  39. break;
  40. if (of_property_read_u32(np, "cache-level", &level))
  41. break;
  42. if (level <= levels)
  43. break;
  44. if (of_property_read_bool(np, "cache-size"))
  45. ++leaves;
  46. if (of_property_read_bool(np, "i-cache-size"))
  47. ++leaves;
  48. if (of_property_read_bool(np, "d-cache-size"))
  49. ++leaves;
  50. levels = level;
  51. }
  52. this_cpu_ci->num_levels = levels;
  53. this_cpu_ci->num_leaves = leaves;
  54. return 0;
  55. }
  56. static int __populate_cache_leaves(unsigned int cpu)
  57. {
  58. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  59. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  60. struct device_node *np = of_cpu_device_node_get(cpu);
  61. int levels = 1, level = 1;
  62. if (of_property_read_bool(np, "cache-size"))
  63. ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
  64. if (of_property_read_bool(np, "i-cache-size"))
  65. ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
  66. if (of_property_read_bool(np, "d-cache-size"))
  67. ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
  68. while ((np = of_find_next_cache_node(np))) {
  69. if (!of_device_is_compatible(np, "cache"))
  70. break;
  71. if (of_property_read_u32(np, "cache-level", &level))
  72. break;
  73. if (level <= levels)
  74. break;
  75. if (of_property_read_bool(np, "cache-size"))
  76. ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
  77. if (of_property_read_bool(np, "i-cache-size"))
  78. ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
  79. if (of_property_read_bool(np, "d-cache-size"))
  80. ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
  81. levels = level;
  82. }
  83. return 0;
  84. }
  85. DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
  86. DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)