Add function acpi_find_cache_info() to find the
information of the caches found in a CPU hierarchy
represented in the PPTT.

Co-developed-by: Jonathan Cameron <jonathan.came...@huawei.com>
Signed-off-by: Jonathan Cameron <jonathan.came...@huawei.com>
Signed-off-by: Shiju Jose <shiju.j...@huawei.com>
---
 drivers/acpi/pptt.c       | 62 +++++++++++++++++++++++++++++++++++++++
 include/linux/cacheinfo.h | 12 ++++++++
 2 files changed, 74 insertions(+)

diff --git a/drivers/acpi/pptt.c b/drivers/acpi/pptt.c
index de1dd605d3ad..5f46e6257e6b 100644
--- a/drivers/acpi/pptt.c
+++ b/drivers/acpi/pptt.c
@@ -670,6 +670,68 @@ int acpi_find_last_cache_level(unsigned int cpu)
        return number_of_levels;
 }
 
+/**
+ * acpi_find_cache_info() - Find the information of CPU caches
+ * represented in the PPTT.
+ * @cpu: Kernel logical CPU number.
+ * @cacheinfo: array of struct acpi_cacheinfo.
+ * @size: dimension of the array.
+ *
+ * Given a logical CPU number, returns the info of caches
+ * represented in the PPTT.
+ * Errors caused by lack of a PPTT table, or otherwise, return 0
+ * indicating we didn't find any cache levels.
+ *
+ * Return: total number of caches found in the CPU hierarchy or error.
+ */
+int acpi_find_cache_info(unsigned int cpu, struct acpi_cacheinfo *cacheinfo,
+                        size_t size)
+{
+       u32 acpi_cpu_id;
+       acpi_status status;
+       struct acpi_table_header *table_hdr;
+       struct acpi_pptt_processor *cpu_node = NULL;
+       struct acpi_pptt_cache *found_cache;
+       int i, number_of_caches = 0;
+       int max_level, level = 1;
+       enum cache_type type[] = {
+               CACHE_TYPE_DATA,
+               CACHE_TYPE_INST,
+               CACHE_TYPE_UNIFIED,
+       };
+
+       if (!cacheinfo || !size)
+               return -ENOMEM;
+
+       status = acpi_get_table(ACPI_SIG_PPTT, 0, &table_hdr);
+       if (ACPI_FAILURE(status)) {
+               acpi_pptt_warn_missing();
+               return -ENOENT;
+       }
+
+       acpi_cpu_id = get_acpi_id_for_cpu(cpu);
+       max_level = acpi_find_cache_levels(table_hdr, acpi_cpu_id);
+       while (level <= max_level) {
+               for (i = 0; i < ARRAY_SIZE(type); i++) {
+                       found_cache = acpi_find_cache_node(table_hdr, 
acpi_cpu_id,
+                                                          type[i], level, 
&cpu_node);
+                       if (found_cache) {
+                               cacheinfo[number_of_caches].level = level;
+                               cacheinfo[number_of_caches].type = 
acpi_cache_type(type[i]);
+                               number_of_caches++;
+                               if (number_of_caches >= size) {
+                                       acpi_put_table(table_hdr);
+                                       return -ENOMEM;
+                               }
+                       }
+               }
+               level++;
+       }
+       acpi_put_table(table_hdr);
+
+       return number_of_caches;
+}
+
 /**
  * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
  * @cpu: Kernel logical CPU number
diff --git a/include/linux/cacheinfo.h b/include/linux/cacheinfo.h
index 4f72b47973c3..7d37945d2650 100644
--- a/include/linux/cacheinfo.h
+++ b/include/linux/cacheinfo.h
@@ -79,6 +79,11 @@ struct cpu_cacheinfo {
        bool cpu_map_populated;
 };
 
+struct acpi_cacheinfo {
+       u8 level;
+       u8 type;
+};
+
 /*
  * Helpers to make sure "func" is executed on the cpu whose cache
  * attributes are being detected
@@ -114,8 +119,15 @@ static inline int acpi_find_last_cache_level(unsigned int 
cpu)
 {
        return 0;
 }
+static inline int acpi_find_cache_info(unsigned int cpu, struct acpi_cacheinfo 
*cacheinfo,
+                                      size_t size)
+{
+       return 0;
+}
 #else
 int acpi_find_last_cache_level(unsigned int cpu);
+int acpi_find_cache_info(unsigned int cpu, struct acpi_cacheinfo *cacheinfo,
+                        size_t size);
 #endif
 
 const struct attribute_group *cache_get_priv_group(struct cacheinfo 
*this_leaf);
-- 
2.17.1

Reply via email to