cacheinfo.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARM64 cacheinfo support
  4. *
  5. * Copyright (C) 2015 ARM Ltd.
  6. * All Rights Reserved
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/cacheinfo.h>
  10. #include <linux/of.h>
  11. #define MAX_CACHE_LEVEL 7 /* Max 7 level supported */
  12. int cache_line_size(void)
  13. {
  14. if (coherency_max_size != 0)
  15. return coherency_max_size;
  16. return cache_line_size_of_cpu();
  17. }
  18. EXPORT_SYMBOL_GPL(cache_line_size);
  19. static inline enum cache_type get_cache_type(int level)
  20. {
  21. u64 clidr;
  22. if (level > MAX_CACHE_LEVEL)
  23. return CACHE_TYPE_NOCACHE;
  24. clidr = read_sysreg(clidr_el1);
  25. return CLIDR_CTYPE(clidr, level);
  26. }
  27. static void ci_leaf_init(struct cacheinfo *this_leaf,
  28. enum cache_type type, unsigned int level)
  29. {
  30. this_leaf->level = level;
  31. this_leaf->type = type;
  32. }
  33. static void detect_cache_level(unsigned int *level_p, unsigned int *leaves_p)
  34. {
  35. unsigned int ctype, level, leaves;
  36. for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
  37. ctype = get_cache_type(level);
  38. if (ctype == CACHE_TYPE_NOCACHE) {
  39. level--;
  40. break;
  41. }
  42. /* Separate instruction and data caches */
  43. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  44. }
  45. *level_p = level;
  46. *leaves_p = leaves;
  47. }
  48. int early_cache_level(unsigned int cpu)
  49. {
  50. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  51. detect_cache_level(&this_cpu_ci->num_levels, &this_cpu_ci->num_leaves);
  52. return 0;
  53. }
  54. int init_cache_level(unsigned int cpu)
  55. {
  56. unsigned int level, leaves;
  57. int fw_level, ret;
  58. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  59. detect_cache_level(&level, &leaves);
  60. if (acpi_disabled) {
  61. fw_level = of_find_last_cache_level(cpu);
  62. } else {
  63. ret = acpi_get_cache_info(cpu, &fw_level, NULL);
  64. if (ret < 0)
  65. fw_level = 0;
  66. }
  67. if (level < fw_level) {
  68. /*
  69. * some external caches not specified in CLIDR_EL1
  70. * the information may be available in the device tree
  71. * only unified external caches are considered here
  72. */
  73. leaves += (fw_level - level);
  74. level = fw_level;
  75. }
  76. this_cpu_ci->num_levels = level;
  77. this_cpu_ci->num_leaves = leaves;
  78. return 0;
  79. }
  80. int populate_cache_leaves(unsigned int cpu)
  81. {
  82. unsigned int level, idx;
  83. enum cache_type type;
  84. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  85. struct cacheinfo *infos = this_cpu_ci->info_list;
  86. for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
  87. idx < this_cpu_ci->num_leaves; level++) {
  88. type = get_cache_type(level);
  89. if (type == CACHE_TYPE_SEPARATE) {
  90. if (idx + 1 >= this_cpu_ci->num_leaves)
  91. break;
  92. ci_leaf_init(&infos[idx++], CACHE_TYPE_DATA, level);
  93. ci_leaf_init(&infos[idx++], CACHE_TYPE_INST, level);
  94. } else {
  95. ci_leaf_init(&infos[idx++], type, level);
  96. }
  97. }
  98. return 0;
  99. }