cacheinfo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/bitops.h>
  20. #include <linux/cacheinfo.h>
  21. #include <linux/cpu.h>
  22. #include <linux/compiler.h>
  23. #include <linux/of.h>
  24. #include <asm/cachetype.h>
  25. #include <asm/processor.h>
  26. #define MAX_CACHE_LEVEL 7 /* Max 7 level supported */
  27. /* Ctypen, bits[3(n - 1) + 2 : 3(n - 1)], for n = 1 to 7 */
  28. #define CLIDR_CTYPE_SHIFT(level) (3 * (level - 1))
  29. #define CLIDR_CTYPE_MASK(level) (7 << CLIDR_CTYPE_SHIFT(level))
  30. #define CLIDR_CTYPE(clidr, level) \
  31. (((clidr) & CLIDR_CTYPE_MASK(level)) >> CLIDR_CTYPE_SHIFT(level))
  32. static inline enum cache_type get_cache_type(int level)
  33. {
  34. u64 clidr;
  35. if (level > MAX_CACHE_LEVEL)
  36. return CACHE_TYPE_NOCACHE;
  37. asm volatile ("mrs %x0, clidr_el1" : "=r" (clidr));
  38. return CLIDR_CTYPE(clidr, level);
  39. }
  40. /*
  41. * Cache Size Selection Register(CSSELR) selects which Cache Size ID
  42. * Register(CCSIDR) is accessible by specifying the required cache
  43. * level and the cache type. We need to ensure that no one else changes
  44. * CSSELR by calling this in non-preemtible context
  45. */
  46. u64 __attribute_const__ cache_get_ccsidr(u64 csselr)
  47. {
  48. u64 ccsidr;
  49. WARN_ON(preemptible());
  50. /* Put value into CSSELR */
  51. asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
  52. isb();
  53. /* Read result out of CCSIDR */
  54. asm volatile("mrs %x0, ccsidr_el1" : "=r" (ccsidr));
  55. return ccsidr;
  56. }
  57. static void ci_leaf_init(struct cacheinfo *this_leaf,
  58. enum cache_type type, unsigned int level)
  59. {
  60. bool is_icache = type & CACHE_TYPE_INST;
  61. u64 tmp = cache_get_ccsidr((level - 1) << 1 | is_icache);
  62. this_leaf->level = level;
  63. this_leaf->type = type;
  64. this_leaf->coherency_line_size = CACHE_LINESIZE(tmp);
  65. this_leaf->number_of_sets = CACHE_NUMSETS(tmp);
  66. this_leaf->ways_of_associativity = CACHE_ASSOCIATIVITY(tmp);
  67. this_leaf->size = this_leaf->number_of_sets *
  68. this_leaf->coherency_line_size * this_leaf->ways_of_associativity;
  69. this_leaf->attributes =
  70. ((tmp & CCSIDR_EL1_WRITE_THROUGH) ? CACHE_WRITE_THROUGH : 0) |
  71. ((tmp & CCSIDR_EL1_WRITE_BACK) ? CACHE_WRITE_BACK : 0) |
  72. ((tmp & CCSIDR_EL1_READ_ALLOCATE) ? CACHE_READ_ALLOCATE : 0) |
  73. ((tmp & CCSIDR_EL1_WRITE_ALLOCATE) ? CACHE_WRITE_ALLOCATE : 0);
  74. }
  75. static int __init_cache_level(unsigned int cpu)
  76. {
  77. unsigned int ctype, level, leaves;
  78. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  79. for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
  80. ctype = get_cache_type(level);
  81. if (ctype == CACHE_TYPE_NOCACHE) {
  82. level--;
  83. break;
  84. }
  85. /* Separate instruction and data caches */
  86. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  87. }
  88. this_cpu_ci->num_levels = level;
  89. this_cpu_ci->num_leaves = leaves;
  90. return 0;
  91. }
  92. static int __populate_cache_leaves(unsigned int cpu)
  93. {
  94. unsigned int level, idx;
  95. enum cache_type type;
  96. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  97. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  98. for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
  99. idx < this_cpu_ci->num_leaves; idx++, level++) {
  100. type = get_cache_type(level);
  101. if (type == CACHE_TYPE_SEPARATE) {
  102. ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
  103. ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
  104. } else {
  105. ci_leaf_init(this_leaf++, type, level);
  106. }
  107. }
  108. return 0;
  109. }
  110. DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
  111. DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)