cacheinfo.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MIPS cacheinfo support
  4. */
  5. #include <linux/cacheinfo.h>
  6. /* Populates leaf and increments to next leaf */
  7. #define populate_cache(cache, leaf, c_level, c_type) \
  8. do { \
  9. leaf->type = c_type; \
  10. leaf->level = c_level; \
  11. leaf->coherency_line_size = c->cache.linesz; \
  12. leaf->number_of_sets = c->cache.sets; \
  13. leaf->ways_of_associativity = c->cache.ways; \
  14. leaf->size = c->cache.linesz * c->cache.sets * \
  15. c->cache.ways; \
  16. leaf++; \
  17. } while (0)
  18. int init_cache_level(unsigned int cpu)
  19. {
  20. struct cpuinfo_mips *c = &current_cpu_data;
  21. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  22. int levels = 0, leaves = 0;
  23. /*
  24. * If Dcache is not set, we assume the cache structures
  25. * are not properly initialized.
  26. */
  27. if (c->dcache.waysize)
  28. levels += 1;
  29. else
  30. return -ENOENT;
  31. leaves += (c->icache.waysize) ? 2 : 1;
  32. if (c->vcache.waysize) {
  33. levels++;
  34. leaves++;
  35. }
  36. if (c->scache.waysize) {
  37. levels++;
  38. leaves++;
  39. }
  40. if (c->tcache.waysize) {
  41. levels++;
  42. leaves++;
  43. }
  44. this_cpu_ci->num_levels = levels;
  45. this_cpu_ci->num_leaves = leaves;
  46. return 0;
  47. }
  48. static void fill_cpumask_siblings(int cpu, cpumask_t *cpu_map)
  49. {
  50. int cpu1;
  51. for_each_possible_cpu(cpu1)
  52. if (cpus_are_siblings(cpu, cpu1))
  53. cpumask_set_cpu(cpu1, cpu_map);
  54. }
  55. static void fill_cpumask_cluster(int cpu, cpumask_t *cpu_map)
  56. {
  57. int cpu1;
  58. int cluster = cpu_cluster(&cpu_data[cpu]);
  59. for_each_possible_cpu(cpu1)
  60. if (cpu_cluster(&cpu_data[cpu1]) == cluster)
  61. cpumask_set_cpu(cpu1, cpu_map);
  62. }
  63. int populate_cache_leaves(unsigned int cpu)
  64. {
  65. struct cpuinfo_mips *c = &current_cpu_data;
  66. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  67. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  68. int level = 1;
  69. if (c->icache.waysize) {
  70. /* I/D caches are per core */
  71. fill_cpumask_siblings(cpu, &this_leaf->shared_cpu_map);
  72. populate_cache(dcache, this_leaf, level, CACHE_TYPE_DATA);
  73. fill_cpumask_siblings(cpu, &this_leaf->shared_cpu_map);
  74. populate_cache(icache, this_leaf, level, CACHE_TYPE_INST);
  75. level++;
  76. } else {
  77. populate_cache(dcache, this_leaf, level, CACHE_TYPE_UNIFIED);
  78. level++;
  79. }
  80. if (c->vcache.waysize) {
  81. /* Vcache is per core as well */
  82. fill_cpumask_siblings(cpu, &this_leaf->shared_cpu_map);
  83. populate_cache(vcache, this_leaf, level, CACHE_TYPE_UNIFIED);
  84. level++;
  85. }
  86. if (c->scache.waysize) {
  87. /* Scache is per cluster */
  88. fill_cpumask_cluster(cpu, &this_leaf->shared_cpu_map);
  89. populate_cache(scache, this_leaf, level, CACHE_TYPE_UNIFIED);
  90. level++;
  91. }
  92. if (c->tcache.waysize)
  93. populate_cache(tcache, this_leaf, level, CACHE_TYPE_UNIFIED);
  94. this_cpu_ci->cpu_map_populated = true;
  95. return 0;
  96. }