cacheinfo.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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->of_node = node;
  22. this_leaf->level = level;
  23. this_leaf->type = type;
  24. /* not a sector cache */
  25. this_leaf->physical_line_partition = 1;
  26. /* TODO: Add to DTS */
  27. this_leaf->attributes =
  28. CACHE_WRITE_BACK
  29. | CACHE_READ_ALLOCATE
  30. | CACHE_WRITE_ALLOCATE;
  31. }
  32. static int __init_cache_level(unsigned int cpu)
  33. {
  34. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  35. struct device_node *np = of_cpu_device_node_get(cpu);
  36. int levels = 0, leaves = 0, level;
  37. if (of_property_read_bool(np, "cache-size"))
  38. ++leaves;
  39. if (of_property_read_bool(np, "i-cache-size"))
  40. ++leaves;
  41. if (of_property_read_bool(np, "d-cache-size"))
  42. ++leaves;
  43. if (leaves > 0)
  44. levels = 1;
  45. while ((np = of_find_next_cache_node(np))) {
  46. if (!of_device_is_compatible(np, "cache"))
  47. break;
  48. if (of_property_read_u32(np, "cache-level", &level))
  49. break;
  50. if (level <= levels)
  51. break;
  52. if (of_property_read_bool(np, "cache-size"))
  53. ++leaves;
  54. if (of_property_read_bool(np, "i-cache-size"))
  55. ++leaves;
  56. if (of_property_read_bool(np, "d-cache-size"))
  57. ++leaves;
  58. levels = level;
  59. }
  60. this_cpu_ci->num_levels = levels;
  61. this_cpu_ci->num_leaves = leaves;
  62. return 0;
  63. }
  64. static int __populate_cache_leaves(unsigned int cpu)
  65. {
  66. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  67. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  68. struct device_node *np = of_cpu_device_node_get(cpu);
  69. int levels = 1, level = 1;
  70. if (of_property_read_bool(np, "cache-size"))
  71. ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
  72. if (of_property_read_bool(np, "i-cache-size"))
  73. ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
  74. if (of_property_read_bool(np, "d-cache-size"))
  75. ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
  76. while ((np = of_find_next_cache_node(np))) {
  77. if (!of_device_is_compatible(np, "cache"))
  78. break;
  79. if (of_property_read_u32(np, "cache-level", &level))
  80. break;
  81. if (level <= levels)
  82. break;
  83. if (of_property_read_bool(np, "cache-size"))
  84. ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
  85. if (of_property_read_bool(np, "i-cache-size"))
  86. ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
  87. if (of_property_read_bool(np, "d-cache-size"))
  88. ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
  89. levels = level;
  90. }
  91. return 0;
  92. }
  93. DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
  94. DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)