cache.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. get_online_cpus();
  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. put_online_cpus();
  79. }
  80. static inline enum cache_type get_cache_type(struct cache_info *ci, int level)
  81. {
  82. if (level >= CACHE_MAX_LEVEL)
  83. return CACHE_TYPE_NOCACHE;
  84. ci += level;
  85. if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE)
  86. return CACHE_TYPE_NOCACHE;
  87. return cache_type_map[ci->type];
  88. }
  89. static inline unsigned long ecag(int ai, int li, int ti)
  90. {
  91. unsigned long cmd, val;
  92. cmd = ai << 4 | li << 1 | ti;
  93. asm volatile(".insn rsy,0xeb000000004c,%0,0,0(%1)" /* ecag */
  94. : "=d" (val) : "a" (cmd));
  95. return val;
  96. }
  97. static void ci_leaf_init(struct cacheinfo *this_leaf, int private,
  98. enum cache_type type, unsigned int level, int cpu)
  99. {
  100. int ti, num_sets;
  101. if (type == CACHE_TYPE_INST)
  102. ti = CACHE_TI_INSTRUCTION;
  103. else
  104. ti = CACHE_TI_UNIFIED;
  105. this_leaf->level = level + 1;
  106. this_leaf->type = type;
  107. this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
  108. this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
  109. this_leaf->size = ecag(EXTRACT_SIZE, level, ti);
  110. num_sets = this_leaf->size / this_leaf->coherency_line_size;
  111. num_sets /= this_leaf->ways_of_associativity;
  112. this_leaf->number_of_sets = num_sets;
  113. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  114. if (!private)
  115. this_leaf->disable_sysfs = true;
  116. }
  117. int init_cache_level(unsigned int cpu)
  118. {
  119. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  120. unsigned int level = 0, leaves = 0;
  121. union cache_topology ct;
  122. enum cache_type ctype;
  123. if (!this_cpu_ci)
  124. return -EINVAL;
  125. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  126. do {
  127. ctype = get_cache_type(&ct.ci[0], level);
  128. if (ctype == CACHE_TYPE_NOCACHE)
  129. break;
  130. /* Separate instruction and data caches */
  131. leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
  132. } while (++level < CACHE_MAX_LEVEL);
  133. this_cpu_ci->num_levels = level;
  134. this_cpu_ci->num_leaves = leaves;
  135. return 0;
  136. }
  137. int populate_cache_leaves(unsigned int cpu)
  138. {
  139. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  140. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  141. unsigned int level, idx, pvt;
  142. union cache_topology ct;
  143. enum cache_type ctype;
  144. if (!test_facility(34))
  145. return -EOPNOTSUPP;
  146. ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
  147. for (idx = 0, level = 0; level < this_cpu_ci->num_levels &&
  148. idx < this_cpu_ci->num_leaves; idx++, level++) {
  149. if (!this_leaf)
  150. return -EINVAL;
  151. pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0;
  152. ctype = get_cache_type(&ct.ci[0], level);
  153. if (ctype == CACHE_TYPE_SEPARATE) {
  154. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu);
  155. ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu);
  156. } else {
  157. ci_leaf_init(this_leaf++, pvt, ctype, level, cpu);
  158. }
  159. }
  160. return 0;
  161. }