cacheinfo.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_CACHEINFO_H
  3. #define _LINUX_CACHEINFO_H
  4. #include <linux/bitops.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/smp.h>
  7. struct device_node;
  8. struct attribute;
  9. enum cache_type {
  10. CACHE_TYPE_NOCACHE = 0,
  11. CACHE_TYPE_INST = BIT(0),
  12. CACHE_TYPE_DATA = BIT(1),
  13. CACHE_TYPE_SEPARATE = CACHE_TYPE_INST | CACHE_TYPE_DATA,
  14. CACHE_TYPE_UNIFIED = BIT(2),
  15. };
  16. extern unsigned int coherency_max_size;
  17. /**
  18. * struct cacheinfo - represent a cache leaf node
  19. * @id: This cache's id. It is unique among caches with the same (type, level).
  20. * @type: type of the cache - data, inst or unified
  21. * @level: represents the hierarchy in the multi-level cache
  22. * @coherency_line_size: size of each cache line usually representing
  23. * the minimum amount of data that gets transferred from memory
  24. * @number_of_sets: total number of sets, a set is a collection of cache
  25. * lines sharing the same index
  26. * @ways_of_associativity: number of ways in which a particular memory
  27. * block can be placed in the cache
  28. * @physical_line_partition: number of physical cache lines sharing the
  29. * same cachetag
  30. * @size: Total size of the cache
  31. * @shared_cpu_map: logical cpumask representing all the cpus sharing
  32. * this cache node
  33. * @attributes: bitfield representing various cache attributes
  34. * @fw_token: Unique value used to determine if different cacheinfo
  35. * structures represent a single hardware cache instance.
  36. * @disable_sysfs: indicates whether this node is visible to the user via
  37. * sysfs or not
  38. * @priv: pointer to any private data structure specific to particular
  39. * cache design
  40. *
  41. * While @of_node, @disable_sysfs and @priv are used for internal book
  42. * keeping, the remaining members form the core properties of the cache
  43. */
  44. struct cacheinfo {
  45. unsigned int id;
  46. enum cache_type type;
  47. unsigned int level;
  48. unsigned int coherency_line_size;
  49. unsigned int number_of_sets;
  50. unsigned int ways_of_associativity;
  51. unsigned int physical_line_partition;
  52. unsigned int size;
  53. cpumask_t shared_cpu_map;
  54. unsigned int attributes;
  55. #define CACHE_WRITE_THROUGH BIT(0)
  56. #define CACHE_WRITE_BACK BIT(1)
  57. #define CACHE_WRITE_POLICY_MASK \
  58. (CACHE_WRITE_THROUGH | CACHE_WRITE_BACK)
  59. #define CACHE_READ_ALLOCATE BIT(2)
  60. #define CACHE_WRITE_ALLOCATE BIT(3)
  61. #define CACHE_ALLOCATE_POLICY_MASK \
  62. (CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE)
  63. #define CACHE_ID BIT(4)
  64. void *fw_token;
  65. bool disable_sysfs;
  66. void *priv;
  67. };
  68. struct cpu_cacheinfo {
  69. struct cacheinfo *info_list;
  70. unsigned int num_levels;
  71. unsigned int num_leaves;
  72. bool cpu_map_populated;
  73. };
  74. struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu);
  75. int init_cache_level(unsigned int cpu);
  76. int populate_cache_leaves(unsigned int cpu);
  77. int cache_setup_acpi(unsigned int cpu);
  78. bool last_level_cache_is_valid(unsigned int cpu);
  79. bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y);
  80. int detect_cache_attributes(unsigned int cpu);
  81. #ifndef CONFIG_ACPI_PPTT
  82. /*
  83. * acpi_find_last_cache_level is only called on ACPI enabled
  84. * platforms using the PPTT for topology. This means that if
  85. * the platform supports other firmware configuration methods
  86. * we need to stub out the call when ACPI is disabled.
  87. * ACPI enabled platforms not using PPTT won't be making calls
  88. * to this function so we need not worry about them.
  89. */
  90. static inline int acpi_find_last_cache_level(unsigned int cpu)
  91. {
  92. return 0;
  93. }
  94. #else
  95. int acpi_find_last_cache_level(unsigned int cpu);
  96. #endif
  97. const struct attribute_group *cache_get_priv_group(struct cacheinfo *this_leaf);
  98. /*
  99. * Get the id of the cache associated with @cpu at level @level.
  100. * cpuhp lock must be held.
  101. */
  102. static inline int get_cpu_cacheinfo_id(int cpu, int level)
  103. {
  104. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  105. int i;
  106. for (i = 0; i < ci->num_leaves; i++) {
  107. if (ci->info_list[i].level == level) {
  108. if (ci->info_list[i].attributes & CACHE_ID)
  109. return ci->info_list[i].id;
  110. return -1;
  111. }
  112. }
  113. return -1;
  114. }
  115. #endif /* _LINUX_CACHEINFO_H */