cacheinfo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * ARM64 cacheinfo support
  3. *
  4. * Copyright (C) 2015 ARM Ltd.
  5. * All Rights Reserved
  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 "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of 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/acpi.h>
  20. #include <linux/cacheinfo.h>
  21. #include <linux/of.h>
  22. #define MAX_CACHE_LEVEL 7 /* Max 7 level supported */
  23. /* Ctypen, bits[3(n - 1) + 2 : 3(n - 1)], for n = 1 to 7 */
  24. #define CLIDR_CTYPE_SHIFT(level) (3 * (level - 1))
  25. #define CLIDR_CTYPE_MASK(level) (7 << CLIDR_CTYPE_SHIFT(level))
  26. #define CLIDR_CTYPE(clidr, level) \
  27. (((clidr) & CLIDR_CTYPE_MASK(level)) >> CLIDR_CTYPE_SHIFT(level))
  28. static inline enum cache_type get_cache_type(int level)
  29. {
  30. u64 clidr;
  31. if (level > MAX_CACHE_LEVEL)
  32. return CACHE_TYPE_NOCACHE;
  33. clidr = read_sysreg(clidr_el1);
  34. return CLIDR_CTYPE(clidr, level);
  35. }
  36. static void ci_leaf_init(struct cacheinfo *this_leaf,
  37. enum cache_type type, unsigned int level)
  38. {
  39. this_leaf->level = level;
  40. this_leaf->type = type;
  41. }
  42. static int __init_cache_level(unsigned int cpu)
  43. {
  44. unsigned int ctype, level, leaves, fw_level;
  45. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  46. for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
  47. ctype = get_cache_type(level);
  48. if (ctype == CACHE_TYPE_NOCACHE) {
  49. level--;
  50. break;
  51. }
  52. /* Separate instruction and data caches */
  53. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  54. }
  55. if (acpi_disabled)
  56. fw_level = of_find_last_cache_level(cpu);
  57. else
  58. fw_level = acpi_find_last_cache_level(cpu);
  59. if (level < fw_level) {
  60. /*
  61. * some external caches not specified in CLIDR_EL1
  62. * the information may be available in the device tree
  63. * only unified external caches are considered here
  64. */
  65. leaves += (fw_level - level);
  66. level = fw_level;
  67. }
  68. this_cpu_ci->num_levels = level;
  69. this_cpu_ci->num_leaves = leaves;
  70. return 0;
  71. }
  72. static int __populate_cache_leaves(unsigned int cpu)
  73. {
  74. unsigned int level, idx;
  75. enum cache_type type;
  76. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  77. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  78. for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
  79. idx < this_cpu_ci->num_leaves; idx++, level++) {
  80. type = get_cache_type(level);
  81. if (type == CACHE_TYPE_SEPARATE) {
  82. ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
  83. ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
  84. } else {
  85. ci_leaf_init(this_leaf++, type, level);
  86. }
  87. }
  88. return 0;
  89. }
  90. DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
  91. DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)