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