cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Extract CPU cache information and expose them via sysfs.
  4. *
  5. * Copyright IBM Corp. 2012
  6. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  7. */
  8. #include <linux/seq_file.h>
  9. #include <linux/cpu.h>
  10. #include <linux/cacheinfo.h>
  11. #include <asm/facility.h>
  12. enum {
  13. CACHE_SCOPE_NOTEXISTS,
  14. CACHE_SCOPE_PRIVATE,
  15. CACHE_SCOPE_SHARED,
  16. CACHE_SCOPE_RESERVED,
  17. };
  18. enum {
  19. CTYPE_SEPARATE,
  20. CTYPE_DATA,
  21. CTYPE_INSTRUCTION,
  22. CTYPE_UNIFIED,
  23. };
  24. enum {
  25. EXTRACT_TOPOLOGY,
  26. EXTRACT_LINE_SIZE,
  27. EXTRACT_SIZE,
  28. EXTRACT_ASSOCIATIVITY,
  29. };
  30. enum {
  31. CACHE_TI_UNIFIED = 0,
  32. CACHE_TI_DATA = 0,
  33. CACHE_TI_INSTRUCTION,
  34. };
  35. struct cache_info {
  36. unsigned char : 4;
  37. unsigned char scope : 2;
  38. unsigned char type : 2;
  39. };
  40. #define CACHE_MAX_LEVEL 8
  41. union cache_topology {
  42. struct cache_info ci[CACHE_MAX_LEVEL];
  43. unsigned long long raw;
  44. };
  45. static const char * const cache_type_string[] = {
  46. "",
  47. "Instruction",
  48. "Data",
  49. "",
  50. "Unified",
  51. };
  52. static const enum cache_type cache_type_map[] = {
  53. [CTYPE_SEPARATE] = CACHE_TYPE_SEPARATE,
  54. [CTYPE_DATA] = CACHE_TYPE_DATA,
  55. [CTYPE_INSTRUCTION] = CACHE_TYPE_INST,
  56. [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
  57. };
  58. void show_cacheinfo(struct seq_file *m)
  59. {
  60. struct cpu_cacheinfo *this_cpu_ci;
  61. struct cacheinfo *cache;
  62. int idx;
  63. if (!test_facility(34))
  64. return;
  65. this_cpu_ci = get_cpu_cacheinfo(cpumask_any(cpu_online_mask));
  66. for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
  67. cache = this_cpu_ci->info_list + idx;
  68. seq_printf(m, "cache%-11d: ", idx);
  69. seq_printf(m, "level=%d ", cache->level);
  70. seq_printf(m, "type=%s ", cache_type_string[cache->type]);
  71. seq_printf(m, "scope=%s ",
  72. cache->disable_sysfs ? "Shared" : "Private");
  73. seq_printf(m, "size=%dK ", cache->size >> 10);
  74. seq_printf(m, "line_size=%u ", cache->coherency_line_size);
  75. seq_printf(m, "associativity=%d", cache->ways_of_associativity);
  76. seq_puts(m, "\n");
  77. }
  78. }
  79. static inline enum cache_type get_cache_type(struct cache_info *ci, int level)
  80. {
  81. if (level >= CACHE_MAX_LEVEL)
  82. return CACHE_TYPE_NOCACHE;
  83. ci += level;
  84. if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE)
  85. return CACHE_TYPE_NOCACHE;
  86. return cache_type_map[ci->type];
  87. }
  88. static inline unsigned long ecag(int ai, int li, int ti)
  89. {
  90. return __ecag(ECAG_CACHE_ATTRIBUTE, ai << 4 | li << 1 | ti);
  91. }
  92. static void ci_leaf_init(struct cacheinfo *this_leaf, int private,
  93. enum cache_type type, unsigned int level, int cpu)
  94. {
  95. int ti, num_sets;
  96. if (type == CACHE_TYPE_INST)
  97. ti = CACHE_TI_INSTRUCTION;
  98. else
  99. ti = CACHE_TI_UNIFIED;
  100. this_leaf->level = level + 1;
  101. this_leaf->type = type;
  102. this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
  103. this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
  104. this_leaf->size = ecag(EXTRACT_SIZE, level, ti);
  105. num_sets = this_leaf->size / this_leaf->coherency_line_size;
  106. num_sets /= this_leaf->ways_of_associativity;
  107. this_leaf->number_of_sets = num_sets;
  108. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  109. if (!private)
  110. this_leaf->disable_sysfs = true;
  111. }
  112. int init_cache_level(unsigned int cpu)
  113. {
  114. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  115. unsigned int level = 0, leaves = 0;
  116. union cache_topology ct;
  117. enum cache_type ctype;
  118. if (!test_facility(34))
  119. return -EOPNOTSUPP;
  120. if (!this_cpu_ci)
  121. return -EINVAL;
  122. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  123. do {
  124. ctype = get_cache_type(&ct.ci[0], level);
  125. if (ctype == CACHE_TYPE_NOCACHE)
  126. break;
  127. /* Separate instruction and data caches */
  128. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  129. } while (++level < CACHE_MAX_LEVEL);
  130. this_cpu_ci->num_levels = level;
  131. this_cpu_ci->num_leaves = leaves;
  132. return 0;
  133. }
  134. int populate_cache_leaves(unsigned int cpu)
  135. {
  136. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  137. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  138. unsigned int level, idx, pvt;
  139. union cache_topology ct;
  140. enum cache_type ctype;
  141. if (!test_facility(34))
  142. return -EOPNOTSUPP;
  143. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  144. for (idx = 0, level = 0; level < this_cpu_ci->num_levels &&
  145. idx < this_cpu_ci->num_leaves; idx++, level++) {
  146. if (!this_leaf)
  147. return -EINVAL;
  148. pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0;
  149. ctype = get_cache_type(&ct.ci[0], level);
  150. if (ctype == CACHE_TYPE_SEPARATE) {
  151. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu);
  152. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu);
  153. } else {
  154. ci_leaf_init(this_leaf++, pvt, ctype, level, cpu);
  155. }
  156. }
  157. return 0;
  158. }