cache.c 4.2 KB

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