cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Extract CPU cache information and expose them via sysfs.
  3. *
  4. * Copyright IBM Corp. 2012
  5. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  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. if (!test_facility(34))
  63. return;
  64. this_cpu_ci = get_cpu_cacheinfo(cpumask_any(cpu_online_mask));
  65. for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
  66. cache = this_cpu_ci->info_list + idx;
  67. seq_printf(m, "cache%-11d: ", idx);
  68. seq_printf(m, "level=%d ", cache->level);
  69. seq_printf(m, "type=%s ", cache_type_string[cache->type]);
  70. seq_printf(m, "scope=%s ",
  71. cache->disable_sysfs ? "Shared" : "Private");
  72. seq_printf(m, "size=%dK ", cache->size >> 10);
  73. seq_printf(m, "line_size=%u ", cache->coherency_line_size);
  74. seq_printf(m, "associativity=%d", cache->ways_of_associativity);
  75. seq_puts(m, "\n");
  76. }
  77. }
  78. static inline enum cache_type get_cache_type(struct cache_info *ci, int level)
  79. {
  80. if (level >= CACHE_MAX_LEVEL)
  81. return CACHE_TYPE_NOCACHE;
  82. ci += level;
  83. if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE)
  84. return CACHE_TYPE_NOCACHE;
  85. return cache_type_map[ci->type];
  86. }
  87. static inline unsigned long ecag(int ai, int li, int ti)
  88. {
  89. return __ecag(ECAG_CACHE_ATTRIBUTE, ai << 4 | li << 1 | ti);
  90. }
  91. static void ci_leaf_init(struct cacheinfo *this_leaf, int private,
  92. enum cache_type type, unsigned int level, int cpu)
  93. {
  94. int ti, num_sets;
  95. if (type == CACHE_TYPE_INST)
  96. ti = CACHE_TI_INSTRUCTION;
  97. else
  98. ti = CACHE_TI_UNIFIED;
  99. this_leaf->level = level + 1;
  100. this_leaf->type = type;
  101. this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
  102. this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
  103. this_leaf->size = ecag(EXTRACT_SIZE, level, ti);
  104. num_sets = this_leaf->size / this_leaf->coherency_line_size;
  105. num_sets /= this_leaf->ways_of_associativity;
  106. this_leaf->number_of_sets = num_sets;
  107. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  108. if (!private)
  109. this_leaf->disable_sysfs = true;
  110. }
  111. int init_cache_level(unsigned int cpu)
  112. {
  113. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  114. unsigned int level = 0, leaves = 0;
  115. union cache_topology ct;
  116. enum cache_type ctype;
  117. if (!test_facility(34))
  118. return -EOPNOTSUPP;
  119. if (!this_cpu_ci)
  120. return -EINVAL;
  121. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  122. do {
  123. ctype = get_cache_type(&ct.ci[0], level);
  124. if (ctype == CACHE_TYPE_NOCACHE)
  125. break;
  126. /* Separate instruction and data caches */
  127. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  128. } while (++level < CACHE_MAX_LEVEL);
  129. this_cpu_ci->num_levels = level;
  130. this_cpu_ci->num_leaves = leaves;
  131. return 0;
  132. }
  133. int populate_cache_leaves(unsigned int cpu)
  134. {
  135. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  136. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  137. unsigned int level, idx, pvt;
  138. union cache_topology ct;
  139. enum cache_type ctype;
  140. if (!test_facility(34))
  141. return -EOPNOTSUPP;
  142. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  143. for (idx = 0, level = 0; level < this_cpu_ci->num_levels &&
  144. idx < this_cpu_ci->num_leaves; idx++, level++) {
  145. if (!this_leaf)
  146. return -EINVAL;
  147. pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0;
  148. ctype = get_cache_type(&ct.ci[0], level);
  149. if (ctype == CACHE_TYPE_SEPARATE) {
  150. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu);
  151. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu);
  152. } else {
  153. ci_leaf_init(this_leaf++, pvt, ctype, level, cpu);
  154. }
  155. }
  156. return 0;
  157. }