xref: /openbmc/linux/drivers/acpi/pptt.c (revision 91d7b60a)
12bd00bcdSJeremy Linton // SPDX-License-Identifier: GPL-2.0
22bd00bcdSJeremy Linton /*
32bd00bcdSJeremy Linton  * pptt.c - parsing of Processor Properties Topology Table (PPTT)
42bd00bcdSJeremy Linton  *
52bd00bcdSJeremy Linton  * Copyright (C) 2018, ARM
62bd00bcdSJeremy Linton  *
72bd00bcdSJeremy Linton  * This file implements parsing of the Processor Properties Topology Table
82bd00bcdSJeremy Linton  * which is optionally used to describe the processor and cache topology.
92bd00bcdSJeremy Linton  * Due to the relative pointers used throughout the table, this doesn't
102bd00bcdSJeremy Linton  * leverage the existing subtable parsing in the kernel.
112bd00bcdSJeremy Linton  *
122bd00bcdSJeremy Linton  * The PPTT structure is an inverted tree, with each node potentially
132bd00bcdSJeremy Linton  * holding one or two inverted tree data structures describing
142bd00bcdSJeremy Linton  * the caches available at that level. Each cache structure optionally
152bd00bcdSJeremy Linton  * contains properties describing the cache at a given level which can be
162bd00bcdSJeremy Linton  * used to override hardware probed values.
172bd00bcdSJeremy Linton  */
182bd00bcdSJeremy Linton #define pr_fmt(fmt) "ACPI PPTT: " fmt
192bd00bcdSJeremy Linton 
202bd00bcdSJeremy Linton #include <linux/acpi.h>
212bd00bcdSJeremy Linton #include <linux/cacheinfo.h>
222bd00bcdSJeremy Linton #include <acpi/processor.h>
232bd00bcdSJeremy Linton 
fetch_pptt_subtable(struct acpi_table_header * table_hdr,u32 pptt_ref)242bd00bcdSJeremy Linton static struct acpi_subtable_header *fetch_pptt_subtable(struct acpi_table_header *table_hdr,
252bd00bcdSJeremy Linton 							u32 pptt_ref)
262bd00bcdSJeremy Linton {
272bd00bcdSJeremy Linton 	struct acpi_subtable_header *entry;
282bd00bcdSJeremy Linton 
292bd00bcdSJeremy Linton 	/* there isn't a subtable at reference 0 */
302bd00bcdSJeremy Linton 	if (pptt_ref < sizeof(struct acpi_subtable_header))
312bd00bcdSJeremy Linton 		return NULL;
322bd00bcdSJeremy Linton 
332bd00bcdSJeremy Linton 	if (pptt_ref + sizeof(struct acpi_subtable_header) > table_hdr->length)
342bd00bcdSJeremy Linton 		return NULL;
352bd00bcdSJeremy Linton 
362bd00bcdSJeremy Linton 	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, pptt_ref);
372bd00bcdSJeremy Linton 
382bd00bcdSJeremy Linton 	if (entry->length == 0)
392bd00bcdSJeremy Linton 		return NULL;
402bd00bcdSJeremy Linton 
412bd00bcdSJeremy Linton 	if (pptt_ref + entry->length > table_hdr->length)
422bd00bcdSJeremy Linton 		return NULL;
432bd00bcdSJeremy Linton 
442bd00bcdSJeremy Linton 	return entry;
452bd00bcdSJeremy Linton }
462bd00bcdSJeremy Linton 
fetch_pptt_node(struct acpi_table_header * table_hdr,u32 pptt_ref)472bd00bcdSJeremy Linton static struct acpi_pptt_processor *fetch_pptt_node(struct acpi_table_header *table_hdr,
482bd00bcdSJeremy Linton 						   u32 pptt_ref)
492bd00bcdSJeremy Linton {
502bd00bcdSJeremy Linton 	return (struct acpi_pptt_processor *)fetch_pptt_subtable(table_hdr, pptt_ref);
512bd00bcdSJeremy Linton }
522bd00bcdSJeremy Linton 
fetch_pptt_cache(struct acpi_table_header * table_hdr,u32 pptt_ref)532bd00bcdSJeremy Linton static struct acpi_pptt_cache *fetch_pptt_cache(struct acpi_table_header *table_hdr,
542bd00bcdSJeremy Linton 						u32 pptt_ref)
552bd00bcdSJeremy Linton {
562bd00bcdSJeremy Linton 	return (struct acpi_pptt_cache *)fetch_pptt_subtable(table_hdr, pptt_ref);
572bd00bcdSJeremy Linton }
582bd00bcdSJeremy Linton 
acpi_get_pptt_resource(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * node,int resource)592bd00bcdSJeremy Linton static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
602bd00bcdSJeremy Linton 							   struct acpi_pptt_processor *node,
612bd00bcdSJeremy Linton 							   int resource)
622bd00bcdSJeremy Linton {
632bd00bcdSJeremy Linton 	u32 *ref;
642bd00bcdSJeremy Linton 
652bd00bcdSJeremy Linton 	if (resource >= node->number_of_priv_resources)
662bd00bcdSJeremy Linton 		return NULL;
672bd00bcdSJeremy Linton 
682bd00bcdSJeremy Linton 	ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
692bd00bcdSJeremy Linton 	ref += resource;
702bd00bcdSJeremy Linton 
712bd00bcdSJeremy Linton 	return fetch_pptt_subtable(table_hdr, *ref);
722bd00bcdSJeremy Linton }
732bd00bcdSJeremy Linton 
acpi_pptt_match_type(int table_type,int type)742bd00bcdSJeremy Linton static inline bool acpi_pptt_match_type(int table_type, int type)
752bd00bcdSJeremy Linton {
762bd00bcdSJeremy Linton 	return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
772bd00bcdSJeremy Linton 		table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
782bd00bcdSJeremy Linton }
792bd00bcdSJeremy Linton 
802bd00bcdSJeremy Linton /**
812bd00bcdSJeremy Linton  * acpi_pptt_walk_cache() - Attempt to find the requested acpi_pptt_cache
822bd00bcdSJeremy Linton  * @table_hdr: Pointer to the head of the PPTT table
832bd00bcdSJeremy Linton  * @local_level: passed res reflects this cache level
84bd500361SPierre Gondois  * @split_levels: Number of split cache levels (data/instruction).
852bd00bcdSJeremy Linton  * @res: cache resource in the PPTT we want to walk
862bd00bcdSJeremy Linton  * @found: returns a pointer to the requested level if found
872bd00bcdSJeremy Linton  * @level: the requested cache level
882bd00bcdSJeremy Linton  * @type: the requested cache type
892bd00bcdSJeremy Linton  *
902bd00bcdSJeremy Linton  * Attempt to find a given cache level, while counting the max number
912bd00bcdSJeremy Linton  * of cache levels for the cache node.
922bd00bcdSJeremy Linton  *
932bd00bcdSJeremy Linton  * Given a pptt resource, verify that it is a cache node, then walk
942bd00bcdSJeremy Linton  * down each level of caches, counting how many levels are found
952bd00bcdSJeremy Linton  * as well as checking the cache type (icache, dcache, unified). If a
962bd00bcdSJeremy Linton  * level & type match, then we set found, and continue the search.
972bd00bcdSJeremy Linton  * Once the entire cache branch has been walked return its max
982bd00bcdSJeremy Linton  * depth.
992bd00bcdSJeremy Linton  *
1002bd00bcdSJeremy Linton  * Return: The cache structure and the level we terminated with.
1012bd00bcdSJeremy Linton  */
acpi_pptt_walk_cache(struct acpi_table_header * table_hdr,unsigned int local_level,unsigned int * split_levels,struct acpi_subtable_header * res,struct acpi_pptt_cache ** found,unsigned int level,int type)102643956e6STian Tao static unsigned int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr,
103643956e6STian Tao 					 unsigned int local_level,
104bd500361SPierre Gondois 					 unsigned int *split_levels,
1052bd00bcdSJeremy Linton 					 struct acpi_subtable_header *res,
1062bd00bcdSJeremy Linton 					 struct acpi_pptt_cache **found,
107643956e6STian Tao 					 unsigned int level, int type)
1082bd00bcdSJeremy Linton {
1092bd00bcdSJeremy Linton 	struct acpi_pptt_cache *cache;
1102bd00bcdSJeremy Linton 
1112bd00bcdSJeremy Linton 	if (res->type != ACPI_PPTT_TYPE_CACHE)
1122bd00bcdSJeremy Linton 		return 0;
1132bd00bcdSJeremy Linton 
1142bd00bcdSJeremy Linton 	cache = (struct acpi_pptt_cache *) res;
1152bd00bcdSJeremy Linton 	while (cache) {
1162bd00bcdSJeremy Linton 		local_level++;
1172bd00bcdSJeremy Linton 
118bd500361SPierre Gondois 		if (!(cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)) {
119bd500361SPierre Gondois 			cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
120bd500361SPierre Gondois 			continue;
121bd500361SPierre Gondois 		}
122bd500361SPierre Gondois 
123bd500361SPierre Gondois 		if (split_levels &&
124bd500361SPierre Gondois 		    (acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_DATA) ||
125bd500361SPierre Gondois 		     acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_INSTR)))
126bd500361SPierre Gondois 			*split_levels = local_level;
127bd500361SPierre Gondois 
1282bd00bcdSJeremy Linton 		if (local_level == level &&
1292bd00bcdSJeremy Linton 		    acpi_pptt_match_type(cache->attributes, type)) {
1302bd00bcdSJeremy Linton 			if (*found != NULL && cache != *found)
1312bd00bcdSJeremy Linton 				pr_warn("Found duplicate cache level/type unable to determine uniqueness\n");
1322bd00bcdSJeremy Linton 
133643956e6STian Tao 			pr_debug("Found cache @ level %u\n", level);
1342bd00bcdSJeremy Linton 			*found = cache;
1352bd00bcdSJeremy Linton 			/*
1362bd00bcdSJeremy Linton 			 * continue looking at this node's resource list
1372bd00bcdSJeremy Linton 			 * to verify that we don't find a duplicate
1382bd00bcdSJeremy Linton 			 * cache node.
1392bd00bcdSJeremy Linton 			 */
1402bd00bcdSJeremy Linton 		}
1412bd00bcdSJeremy Linton 		cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
1422bd00bcdSJeremy Linton 	}
1432bd00bcdSJeremy Linton 	return local_level;
1442bd00bcdSJeremy Linton }
1452bd00bcdSJeremy Linton 
146643956e6STian Tao static struct acpi_pptt_cache *
acpi_find_cache_level(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu_node,unsigned int * starting_level,unsigned int * split_levels,unsigned int level,int type)147643956e6STian Tao acpi_find_cache_level(struct acpi_table_header *table_hdr,
1482bd00bcdSJeremy Linton 		      struct acpi_pptt_processor *cpu_node,
149bd500361SPierre Gondois 		      unsigned int *starting_level, unsigned int *split_levels,
150bd500361SPierre Gondois 		      unsigned int level, int type)
1512bd00bcdSJeremy Linton {
1522bd00bcdSJeremy Linton 	struct acpi_subtable_header *res;
153643956e6STian Tao 	unsigned int number_of_levels = *starting_level;
1542bd00bcdSJeremy Linton 	int resource = 0;
1552bd00bcdSJeremy Linton 	struct acpi_pptt_cache *ret = NULL;
156643956e6STian Tao 	unsigned int local_level;
1572bd00bcdSJeremy Linton 
1582bd00bcdSJeremy Linton 	/* walk down from processor node */
1592bd00bcdSJeremy Linton 	while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
1602bd00bcdSJeremy Linton 		resource++;
1612bd00bcdSJeremy Linton 
1622bd00bcdSJeremy Linton 		local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
163bd500361SPierre Gondois 						   split_levels, res, &ret,
164bd500361SPierre Gondois 						   level, type);
1652bd00bcdSJeremy Linton 		/*
1662bd00bcdSJeremy Linton 		 * we are looking for the max depth. Since its potentially
1672bd00bcdSJeremy Linton 		 * possible for a given node to have resources with differing
1682bd00bcdSJeremy Linton 		 * depths verify that the depth we have found is the largest.
1692bd00bcdSJeremy Linton 		 */
1702bd00bcdSJeremy Linton 		if (number_of_levels < local_level)
1712bd00bcdSJeremy Linton 			number_of_levels = local_level;
1722bd00bcdSJeremy Linton 	}
1732bd00bcdSJeremy Linton 	if (number_of_levels > *starting_level)
1742bd00bcdSJeremy Linton 		*starting_level = number_of_levels;
1752bd00bcdSJeremy Linton 
1762bd00bcdSJeremy Linton 	return ret;
1772bd00bcdSJeremy Linton }
1782bd00bcdSJeremy Linton 
1792bd00bcdSJeremy Linton /**
180bd500361SPierre Gondois  * acpi_count_levels() - Given a PPTT table, and a CPU node, count the cache
181bd500361SPierre Gondois  * levels and split cache levels (data/instruction).
1822bd00bcdSJeremy Linton  * @table_hdr: Pointer to the head of the PPTT table
1832bd00bcdSJeremy Linton  * @cpu_node: processor node we wish to count caches for
184bd500361SPierre Gondois  * @levels: Number of levels if success.
185bd500361SPierre Gondois  * @split_levels:	Number of split cache levels (data/instruction) if
186bd500361SPierre Gondois  *			success. Can by NULL.
1872bd00bcdSJeremy Linton  *
1882bd00bcdSJeremy Linton  * Given a processor node containing a processing unit, walk into it and count
1892bd00bcdSJeremy Linton  * how many levels exist solely for it, and then walk up each level until we hit
1902bd00bcdSJeremy Linton  * the root node (ignore the package level because it may be possible to have
191bd500361SPierre Gondois  * caches that exist across packages). Count the number of cache levels and
192bd500361SPierre Gondois  * split cache levels (data/instruction) that exist at each level on the way
193bd500361SPierre Gondois  * up.
1942bd00bcdSJeremy Linton  */
acpi_count_levels(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu_node,unsigned int * levels,unsigned int * split_levels)195bd500361SPierre Gondois static void acpi_count_levels(struct acpi_table_header *table_hdr,
196bd500361SPierre Gondois 			      struct acpi_pptt_processor *cpu_node,
197bd500361SPierre Gondois 			      unsigned int *levels, unsigned int *split_levels)
1982bd00bcdSJeremy Linton {
1992bd00bcdSJeremy Linton 	do {
200bd500361SPierre Gondois 		acpi_find_cache_level(table_hdr, cpu_node, levels, split_levels, 0, 0);
2012bd00bcdSJeremy Linton 		cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
2022bd00bcdSJeremy Linton 	} while (cpu_node);
2032bd00bcdSJeremy Linton }
2042bd00bcdSJeremy Linton 
2052bd00bcdSJeremy Linton /**
2062bd00bcdSJeremy Linton  * acpi_pptt_leaf_node() - Given a processor node, determine if its a leaf
2072bd00bcdSJeremy Linton  * @table_hdr: Pointer to the head of the PPTT table
2082bd00bcdSJeremy Linton  * @node: passed node is checked to see if its a leaf
2092bd00bcdSJeremy Linton  *
2102bd00bcdSJeremy Linton  * Determine if the *node parameter is a leaf node by iterating the
2112bd00bcdSJeremy Linton  * PPTT table, looking for nodes which reference it.
2122bd00bcdSJeremy Linton  *
2132bd00bcdSJeremy Linton  * Return: 0 if we find a node referencing the passed node (or table error),
2142bd00bcdSJeremy Linton  * or 1 if we don't.
2152bd00bcdSJeremy Linton  */
acpi_pptt_leaf_node(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * node)2162bd00bcdSJeremy Linton static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
2172bd00bcdSJeremy Linton 			       struct acpi_pptt_processor *node)
2182bd00bcdSJeremy Linton {
2192bd00bcdSJeremy Linton 	struct acpi_subtable_header *entry;
2202bd00bcdSJeremy Linton 	unsigned long table_end;
2212bd00bcdSJeremy Linton 	u32 node_entry;
2222bd00bcdSJeremy Linton 	struct acpi_pptt_processor *cpu_node;
2232bd00bcdSJeremy Linton 	u32 proc_sz;
2242bd00bcdSJeremy Linton 
2254909e6dfSJeremy Linton 	if (table_hdr->revision > 1)
2264909e6dfSJeremy Linton 		return (node->flags & ACPI_PPTT_ACPI_LEAF_NODE);
2274909e6dfSJeremy Linton 
2282bd00bcdSJeremy Linton 	table_end = (unsigned long)table_hdr + table_hdr->length;
2292bd00bcdSJeremy Linton 	node_entry = ACPI_PTR_DIFF(node, table_hdr);
2302bd00bcdSJeremy Linton 	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
2312bd00bcdSJeremy Linton 			     sizeof(struct acpi_table_pptt));
2322bd00bcdSJeremy Linton 	proc_sz = sizeof(struct acpi_pptt_processor *);
2332bd00bcdSJeremy Linton 
2342bd00bcdSJeremy Linton 	while ((unsigned long)entry + proc_sz < table_end) {
2352bd00bcdSJeremy Linton 		cpu_node = (struct acpi_pptt_processor *)entry;
2362bd00bcdSJeremy Linton 		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
2372bd00bcdSJeremy Linton 		    cpu_node->parent == node_entry)
2382bd00bcdSJeremy Linton 			return 0;
2392bd00bcdSJeremy Linton 		if (entry->length == 0)
2402bd00bcdSJeremy Linton 			return 0;
2412bd00bcdSJeremy Linton 		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
2422bd00bcdSJeremy Linton 				     entry->length);
2432bd00bcdSJeremy Linton 
2442bd00bcdSJeremy Linton 	}
2452bd00bcdSJeremy Linton 	return 1;
2462bd00bcdSJeremy Linton }
2472bd00bcdSJeremy Linton 
2482bd00bcdSJeremy Linton /**
2492bd00bcdSJeremy Linton  * acpi_find_processor_node() - Given a PPTT table find the requested processor
2502bd00bcdSJeremy Linton  * @table_hdr:  Pointer to the head of the PPTT table
251603fadf3SBjorn Helgaas  * @acpi_cpu_id: CPU we are searching for
2522bd00bcdSJeremy Linton  *
2532bd00bcdSJeremy Linton  * Find the subtable entry describing the provided processor.
2542bd00bcdSJeremy Linton  * This is done by iterating the PPTT table looking for processor nodes
2552bd00bcdSJeremy Linton  * which have an acpi_processor_id that matches the acpi_cpu_id parameter
2562bd00bcdSJeremy Linton  * passed into the function. If we find a node that matches this criteria
2572bd00bcdSJeremy Linton  * we verify that its a leaf node in the topology rather than depending
2582bd00bcdSJeremy Linton  * on the valid flag, which doesn't need to be set for leaf nodes.
2592bd00bcdSJeremy Linton  *
2602bd00bcdSJeremy Linton  * Return: NULL, or the processors acpi_pptt_processor*
2612bd00bcdSJeremy Linton  */
acpi_find_processor_node(struct acpi_table_header * table_hdr,u32 acpi_cpu_id)2622bd00bcdSJeremy Linton static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
2632bd00bcdSJeremy Linton 							    u32 acpi_cpu_id)
2642bd00bcdSJeremy Linton {
2652bd00bcdSJeremy Linton 	struct acpi_subtable_header *entry;
2662bd00bcdSJeremy Linton 	unsigned long table_end;
2672bd00bcdSJeremy Linton 	struct acpi_pptt_processor *cpu_node;
2682bd00bcdSJeremy Linton 	u32 proc_sz;
2692bd00bcdSJeremy Linton 
2702bd00bcdSJeremy Linton 	table_end = (unsigned long)table_hdr + table_hdr->length;
2712bd00bcdSJeremy Linton 	entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
2722bd00bcdSJeremy Linton 			     sizeof(struct acpi_table_pptt));
2732bd00bcdSJeremy Linton 	proc_sz = sizeof(struct acpi_pptt_processor *);
2742bd00bcdSJeremy Linton 
2752bd00bcdSJeremy Linton 	/* find the processor structure associated with this cpuid */
2762bd00bcdSJeremy Linton 	while ((unsigned long)entry + proc_sz < table_end) {
2772bd00bcdSJeremy Linton 		cpu_node = (struct acpi_pptt_processor *)entry;
2782bd00bcdSJeremy Linton 
2792bd00bcdSJeremy Linton 		if (entry->length == 0) {
2802bd00bcdSJeremy Linton 			pr_warn("Invalid zero length subtable\n");
2812bd00bcdSJeremy Linton 			break;
2822bd00bcdSJeremy Linton 		}
2832bd00bcdSJeremy Linton 		if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
2842bd00bcdSJeremy Linton 		    acpi_cpu_id == cpu_node->acpi_processor_id &&
2852bd00bcdSJeremy Linton 		     acpi_pptt_leaf_node(table_hdr, cpu_node)) {
2862bd00bcdSJeremy Linton 			return (struct acpi_pptt_processor *)entry;
2872bd00bcdSJeremy Linton 		}
2882bd00bcdSJeremy Linton 
2892bd00bcdSJeremy Linton 		entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
2902bd00bcdSJeremy Linton 				     entry->length);
2912bd00bcdSJeremy Linton 	}
2922bd00bcdSJeremy Linton 
2932bd00bcdSJeremy Linton 	return NULL;
2942bd00bcdSJeremy Linton }
2952bd00bcdSJeremy Linton 
acpi_cache_type(enum cache_type type)2962bd00bcdSJeremy Linton static u8 acpi_cache_type(enum cache_type type)
2972bd00bcdSJeremy Linton {
2982bd00bcdSJeremy Linton 	switch (type) {
2992bd00bcdSJeremy Linton 	case CACHE_TYPE_DATA:
3002bd00bcdSJeremy Linton 		pr_debug("Looking for data cache\n");
3012bd00bcdSJeremy Linton 		return ACPI_PPTT_CACHE_TYPE_DATA;
3022bd00bcdSJeremy Linton 	case CACHE_TYPE_INST:
3032bd00bcdSJeremy Linton 		pr_debug("Looking for instruction cache\n");
3042bd00bcdSJeremy Linton 		return ACPI_PPTT_CACHE_TYPE_INSTR;
3052bd00bcdSJeremy Linton 	default:
3062bd00bcdSJeremy Linton 	case CACHE_TYPE_UNIFIED:
3072bd00bcdSJeremy Linton 		pr_debug("Looking for unified cache\n");
3082bd00bcdSJeremy Linton 		/*
3092bd00bcdSJeremy Linton 		 * It is important that ACPI_PPTT_CACHE_TYPE_UNIFIED
3102bd00bcdSJeremy Linton 		 * contains the bit pattern that will match both
3112bd00bcdSJeremy Linton 		 * ACPI unified bit patterns because we use it later
3122bd00bcdSJeremy Linton 		 * to match both cases.
3132bd00bcdSJeremy Linton 		 */
3142bd00bcdSJeremy Linton 		return ACPI_PPTT_CACHE_TYPE_UNIFIED;
3152bd00bcdSJeremy Linton 	}
3162bd00bcdSJeremy Linton }
3172bd00bcdSJeremy Linton 
acpi_find_cache_node(struct acpi_table_header * table_hdr,u32 acpi_cpu_id,enum cache_type type,unsigned int level,struct acpi_pptt_processor ** node)3182bd00bcdSJeremy Linton static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr,
3192bd00bcdSJeremy Linton 						    u32 acpi_cpu_id,
3202bd00bcdSJeremy Linton 						    enum cache_type type,
3212bd00bcdSJeremy Linton 						    unsigned int level,
3222bd00bcdSJeremy Linton 						    struct acpi_pptt_processor **node)
3232bd00bcdSJeremy Linton {
324643956e6STian Tao 	unsigned int total_levels = 0;
3252bd00bcdSJeremy Linton 	struct acpi_pptt_cache *found = NULL;
3262bd00bcdSJeremy Linton 	struct acpi_pptt_processor *cpu_node;
3272bd00bcdSJeremy Linton 	u8 acpi_type = acpi_cache_type(type);
3282bd00bcdSJeremy Linton 
329643956e6STian Tao 	pr_debug("Looking for CPU %d's level %u cache type %d\n",
3302bd00bcdSJeremy Linton 		 acpi_cpu_id, level, acpi_type);
3312bd00bcdSJeremy Linton 
3322bd00bcdSJeremy Linton 	cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
3332bd00bcdSJeremy Linton 
3342bd00bcdSJeremy Linton 	while (cpu_node && !found) {
3352bd00bcdSJeremy Linton 		found = acpi_find_cache_level(table_hdr, cpu_node,
336bd500361SPierre Gondois 					      &total_levels, NULL, level, acpi_type);
3372bd00bcdSJeremy Linton 		*node = cpu_node;
3382bd00bcdSJeremy Linton 		cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
3392bd00bcdSJeremy Linton 	}
3402bd00bcdSJeremy Linton 
3412bd00bcdSJeremy Linton 	return found;
3422bd00bcdSJeremy Linton }
3432bd00bcdSJeremy Linton 
3442bd00bcdSJeremy Linton /**
3452bd00bcdSJeremy Linton  * update_cache_properties() - Update cacheinfo for the given processor
3462bd00bcdSJeremy Linton  * @this_leaf: Kernel cache info structure being updated
3472bd00bcdSJeremy Linton  * @found_cache: The PPTT node describing this cache instance
3482bd00bcdSJeremy Linton  * @cpu_node: A unique reference to describe this cache instance
3497ca1a801SJames Morse  * @revision: The revision of the PPTT table
3502bd00bcdSJeremy Linton  *
3512bd00bcdSJeremy Linton  * The ACPI spec implies that the fields in the cache structures are used to
3522bd00bcdSJeremy Linton  * extend and correct the information probed from the hardware. Lets only
3532bd00bcdSJeremy Linton  * set fields that we determine are VALID.
3542bd00bcdSJeremy Linton  *
3552bd00bcdSJeremy Linton  * Return: nothing. Side effect of updating the global cacheinfo
3562bd00bcdSJeremy Linton  */
update_cache_properties(struct cacheinfo * this_leaf,struct acpi_pptt_cache * found_cache,struct acpi_pptt_processor * cpu_node,u8 revision)3572bd00bcdSJeremy Linton static void update_cache_properties(struct cacheinfo *this_leaf,
3582bd00bcdSJeremy Linton 				    struct acpi_pptt_cache *found_cache,
3597ca1a801SJames Morse 				    struct acpi_pptt_processor *cpu_node,
3607ca1a801SJames Morse 				    u8 revision)
3612bd00bcdSJeremy Linton {
3627ca1a801SJames Morse 	struct acpi_pptt_cache_v1* found_cache_v1;
3637ca1a801SJames Morse 
3642bd00bcdSJeremy Linton 	this_leaf->fw_token = cpu_node;
36559bbff37SJeffrey Hugo 	if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID)
3662bd00bcdSJeremy Linton 		this_leaf->size = found_cache->size;
36759bbff37SJeffrey Hugo 	if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID)
3682bd00bcdSJeremy Linton 		this_leaf->coherency_line_size = found_cache->line_size;
36959bbff37SJeffrey Hugo 	if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID)
3702bd00bcdSJeremy Linton 		this_leaf->number_of_sets = found_cache->number_of_sets;
37159bbff37SJeffrey Hugo 	if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID)
3722bd00bcdSJeremy Linton 		this_leaf->ways_of_associativity = found_cache->associativity;
3732bd00bcdSJeremy Linton 	if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
3742bd00bcdSJeremy Linton 		switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
3752bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_POLICY_WT:
3762bd00bcdSJeremy Linton 			this_leaf->attributes = CACHE_WRITE_THROUGH;
3772bd00bcdSJeremy Linton 			break;
3782bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_POLICY_WB:
3792bd00bcdSJeremy Linton 			this_leaf->attributes = CACHE_WRITE_BACK;
3802bd00bcdSJeremy Linton 			break;
3812bd00bcdSJeremy Linton 		}
3822bd00bcdSJeremy Linton 	}
3832bd00bcdSJeremy Linton 	if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
3842bd00bcdSJeremy Linton 		switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
3852bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_READ_ALLOCATE:
3862bd00bcdSJeremy Linton 			this_leaf->attributes |= CACHE_READ_ALLOCATE;
3872bd00bcdSJeremy Linton 			break;
3882bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
3892bd00bcdSJeremy Linton 			this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
3902bd00bcdSJeremy Linton 			break;
3912bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_RW_ALLOCATE:
3922bd00bcdSJeremy Linton 		case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
3932bd00bcdSJeremy Linton 			this_leaf->attributes |=
3942bd00bcdSJeremy Linton 				CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
3952bd00bcdSJeremy Linton 			break;
3962bd00bcdSJeremy Linton 		}
3972bd00bcdSJeremy Linton 	}
3982bd00bcdSJeremy Linton 	/*
39959bbff37SJeffrey Hugo 	 * If cache type is NOCACHE, then the cache hasn't been specified
40059bbff37SJeffrey Hugo 	 * via other mechanisms.  Update the type if a cache type has been
40159bbff37SJeffrey Hugo 	 * provided.
40259bbff37SJeffrey Hugo 	 *
40359bbff37SJeffrey Hugo 	 * Note, we assume such caches are unified based on conventional system
40459bbff37SJeffrey Hugo 	 * design and known examples.  Significant work is required elsewhere to
40559bbff37SJeffrey Hugo 	 * fully support data/instruction only type caches which are only
40659bbff37SJeffrey Hugo 	 * specified in PPTT.
4072bd00bcdSJeremy Linton 	 */
4082bd00bcdSJeremy Linton 	if (this_leaf->type == CACHE_TYPE_NOCACHE &&
40959bbff37SJeffrey Hugo 	    found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
4102bd00bcdSJeremy Linton 		this_leaf->type = CACHE_TYPE_UNIFIED;
4117ca1a801SJames Morse 
4127ca1a801SJames Morse 	if (revision >= 3 && (found_cache->flags & ACPI_PPTT_CACHE_ID_VALID)) {
4137ca1a801SJames Morse 		found_cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
4147ca1a801SJames Morse 	                                      found_cache, sizeof(struct acpi_pptt_cache));
4157ca1a801SJames Morse 		this_leaf->id = found_cache_v1->cache_id;
4167ca1a801SJames Morse 		this_leaf->attributes |= CACHE_ID;
4177ca1a801SJames Morse 	}
4182bd00bcdSJeremy Linton }
4192bd00bcdSJeremy Linton 
cache_setup_acpi_cpu(struct acpi_table_header * table,unsigned int cpu)4202bd00bcdSJeremy Linton static void cache_setup_acpi_cpu(struct acpi_table_header *table,
4212bd00bcdSJeremy Linton 				 unsigned int cpu)
4222bd00bcdSJeremy Linton {
4232bd00bcdSJeremy Linton 	struct acpi_pptt_cache *found_cache;
4242bd00bcdSJeremy Linton 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
4252bd00bcdSJeremy Linton 	u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
4262bd00bcdSJeremy Linton 	struct cacheinfo *this_leaf;
4272bd00bcdSJeremy Linton 	unsigned int index = 0;
4282bd00bcdSJeremy Linton 	struct acpi_pptt_processor *cpu_node = NULL;
4292bd00bcdSJeremy Linton 
4302bd00bcdSJeremy Linton 	while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
4312bd00bcdSJeremy Linton 		this_leaf = this_cpu_ci->info_list + index;
4322bd00bcdSJeremy Linton 		found_cache = acpi_find_cache_node(table, acpi_cpu_id,
4332bd00bcdSJeremy Linton 						   this_leaf->type,
4342bd00bcdSJeremy Linton 						   this_leaf->level,
4352bd00bcdSJeremy Linton 						   &cpu_node);
4362bd00bcdSJeremy Linton 		pr_debug("found = %p %p\n", found_cache, cpu_node);
4372bd00bcdSJeremy Linton 		if (found_cache)
4387ca1a801SJames Morse 			update_cache_properties(this_leaf, found_cache,
4390d4c331aSSudeep Holla 						ACPI_TO_POINTER(ACPI_PTR_DIFF(cpu_node, table)),
4400d4c331aSSudeep Holla 						table->revision);
4412bd00bcdSJeremy Linton 
4422bd00bcdSJeremy Linton 		index++;
4432bd00bcdSJeremy Linton 	}
4442bd00bcdSJeremy Linton }
4452bd00bcdSJeremy Linton 
flag_identical(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu)446ed2b664fSJeremy Linton static bool flag_identical(struct acpi_table_header *table_hdr,
447ed2b664fSJeremy Linton 			   struct acpi_pptt_processor *cpu)
448ed2b664fSJeremy Linton {
449ed2b664fSJeremy Linton 	struct acpi_pptt_processor *next;
450ed2b664fSJeremy Linton 
451ed2b664fSJeremy Linton 	/* heterogeneous machines must use PPTT revision > 1 */
452ed2b664fSJeremy Linton 	if (table_hdr->revision < 2)
453ed2b664fSJeremy Linton 		return false;
454ed2b664fSJeremy Linton 
455ed2b664fSJeremy Linton 	/* Locate the last node in the tree with IDENTICAL set */
456ed2b664fSJeremy Linton 	if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) {
457ed2b664fSJeremy Linton 		next = fetch_pptt_node(table_hdr, cpu->parent);
458ed2b664fSJeremy Linton 		if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL))
459ed2b664fSJeremy Linton 			return true;
460ed2b664fSJeremy Linton 	}
461ed2b664fSJeremy Linton 
462ed2b664fSJeremy Linton 	return false;
463ed2b664fSJeremy Linton }
464ed2b664fSJeremy Linton 
4652bd00bcdSJeremy Linton /* Passing level values greater than this will result in search termination */
4662bd00bcdSJeremy Linton #define PPTT_ABORT_PACKAGE 0xFF
4672bd00bcdSJeremy Linton 
acpi_find_processor_tag(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu,int level,int flag)468ed2b664fSJeremy Linton static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr,
4692bd00bcdSJeremy Linton 							   struct acpi_pptt_processor *cpu,
4702bd00bcdSJeremy Linton 							   int level, int flag)
4712bd00bcdSJeremy Linton {
4722bd00bcdSJeremy Linton 	struct acpi_pptt_processor *prev_node;
4732bd00bcdSJeremy Linton 
4742bd00bcdSJeremy Linton 	while (cpu && level) {
475ed2b664fSJeremy Linton 		/* special case the identical flag to find last identical */
476ed2b664fSJeremy Linton 		if (flag == ACPI_PPTT_ACPI_IDENTICAL) {
477ed2b664fSJeremy Linton 			if (flag_identical(table_hdr, cpu))
478ed2b664fSJeremy Linton 				break;
479ed2b664fSJeremy Linton 		} else if (cpu->flags & flag)
4802bd00bcdSJeremy Linton 			break;
4812bd00bcdSJeremy Linton 		pr_debug("level %d\n", level);
4822bd00bcdSJeremy Linton 		prev_node = fetch_pptt_node(table_hdr, cpu->parent);
4832bd00bcdSJeremy Linton 		if (prev_node == NULL)
4842bd00bcdSJeremy Linton 			break;
4852bd00bcdSJeremy Linton 		cpu = prev_node;
4862bd00bcdSJeremy Linton 		level--;
4872bd00bcdSJeremy Linton 	}
4882bd00bcdSJeremy Linton 	return cpu;
4892bd00bcdSJeremy Linton }
4902bd00bcdSJeremy Linton 
acpi_pptt_warn_missing(void)4916cafe700SJohn Garry static void acpi_pptt_warn_missing(void)
4926cafe700SJohn Garry {
493603fadf3SBjorn Helgaas 	pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
4946cafe700SJohn Garry }
4956cafe700SJohn Garry 
4962bd00bcdSJeremy Linton /**
4972bd00bcdSJeremy Linton  * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
4982bd00bcdSJeremy Linton  * @table: Pointer to the head of the PPTT table
499603fadf3SBjorn Helgaas  * @cpu: Kernel logical CPU number
5002bd00bcdSJeremy Linton  * @level: A level that terminates the search
5012bd00bcdSJeremy Linton  * @flag: A flag which terminates the search
5022bd00bcdSJeremy Linton  *
503603fadf3SBjorn Helgaas  * Get a unique value given a CPU, and a topology level, that can be
5042bd00bcdSJeremy Linton  * matched to determine which cpus share common topological features
5052bd00bcdSJeremy Linton  * at that level.
5062bd00bcdSJeremy Linton  *
507603fadf3SBjorn Helgaas  * Return: Unique value, or -ENOENT if unable to locate CPU
5082bd00bcdSJeremy Linton  */
topology_get_acpi_cpu_tag(struct acpi_table_header * table,unsigned int cpu,int level,int flag)5092bd00bcdSJeremy Linton static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
5102bd00bcdSJeremy Linton 				     unsigned int cpu, int level, int flag)
5112bd00bcdSJeremy Linton {
5122bd00bcdSJeremy Linton 	struct acpi_pptt_processor *cpu_node;
5132bd00bcdSJeremy Linton 	u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
5142bd00bcdSJeremy Linton 
5152bd00bcdSJeremy Linton 	cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
5162bd00bcdSJeremy Linton 	if (cpu_node) {
517ed2b664fSJeremy Linton 		cpu_node = acpi_find_processor_tag(table, cpu_node,
5182bd00bcdSJeremy Linton 						   level, flag);
51930998033SSudeep Holla 		/*
52030998033SSudeep Holla 		 * As per specification if the processor structure represents
52130998033SSudeep Holla 		 * an actual processor, then ACPI processor ID must be valid.
52230998033SSudeep Holla 		 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
52330998033SSudeep Holla 		 * should be set if the UID is valid
52430998033SSudeep Holla 		 */
52530998033SSudeep Holla 		if (level == 0 ||
52630998033SSudeep Holla 		    cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
5272bd00bcdSJeremy Linton 			return cpu_node->acpi_processor_id;
5282bd00bcdSJeremy Linton 		return ACPI_PTR_DIFF(cpu_node, table);
5292bd00bcdSJeremy Linton 	}
5302bd00bcdSJeremy Linton 	pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
5312bd00bcdSJeremy Linton 		    cpu, acpi_cpu_id);
5322bd00bcdSJeremy Linton 	return -ENOENT;
5332bd00bcdSJeremy Linton }
5342bd00bcdSJeremy Linton 
5350c80f9e1SSudeep Holla 
acpi_get_pptt(void)5360c80f9e1SSudeep Holla static struct acpi_table_header *acpi_get_pptt(void)
5370c80f9e1SSudeep Holla {
5380c80f9e1SSudeep Holla 	static struct acpi_table_header *pptt;
539*91d7b60aSSudeep Holla 	static bool is_pptt_checked;
5400c80f9e1SSudeep Holla 	acpi_status status;
5410c80f9e1SSudeep Holla 
5420c80f9e1SSudeep Holla 	/*
5430c80f9e1SSudeep Holla 	 * PPTT will be used at runtime on every CPU hotplug in path, so we
5440c80f9e1SSudeep Holla 	 * don't need to call acpi_put_table() to release the table mapping.
5450c80f9e1SSudeep Holla 	 */
546*91d7b60aSSudeep Holla 	if (!pptt && !is_pptt_checked) {
5470c80f9e1SSudeep Holla 		status = acpi_get_table(ACPI_SIG_PPTT, 0, &pptt);
5480c80f9e1SSudeep Holla 		if (ACPI_FAILURE(status))
5490c80f9e1SSudeep Holla 			acpi_pptt_warn_missing();
550*91d7b60aSSudeep Holla 
551*91d7b60aSSudeep Holla 		is_pptt_checked = true;
5520c80f9e1SSudeep Holla 	}
5530c80f9e1SSudeep Holla 
5540c80f9e1SSudeep Holla 	return pptt;
5550c80f9e1SSudeep Holla }
5560c80f9e1SSudeep Holla 
find_acpi_cpu_topology_tag(unsigned int cpu,int level,int flag)5572bd00bcdSJeremy Linton static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
5582bd00bcdSJeremy Linton {
5592bd00bcdSJeremy Linton 	struct acpi_table_header *table;
5602bd00bcdSJeremy Linton 	int retval;
5612bd00bcdSJeremy Linton 
5620c80f9e1SSudeep Holla 	table = acpi_get_pptt();
5630c80f9e1SSudeep Holla 	if (!table)
5642bd00bcdSJeremy Linton 		return -ENOENT;
5650c80f9e1SSudeep Holla 
5662bd00bcdSJeremy Linton 	retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
567603fadf3SBjorn Helgaas 	pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
5682bd00bcdSJeremy Linton 		 cpu, level, retval);
5692bd00bcdSJeremy Linton 
5702bd00bcdSJeremy Linton 	return retval;
5712bd00bcdSJeremy Linton }
5722bd00bcdSJeremy Linton 
5732bd00bcdSJeremy Linton /**
574bbd1b706SJeremy Linton  * check_acpi_cpu_flag() - Determine if CPU node has a flag set
575bbd1b706SJeremy Linton  * @cpu: Kernel logical CPU number
576bbd1b706SJeremy Linton  * @rev: The minimum PPTT revision defining the flag
577bbd1b706SJeremy Linton  * @flag: The flag itself
578bbd1b706SJeremy Linton  *
579bbd1b706SJeremy Linton  * Check the node representing a CPU for a given flag.
580bbd1b706SJeremy Linton  *
581bbd1b706SJeremy Linton  * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or
582bbd1b706SJeremy Linton  *	   the table revision isn't new enough.
583bbd1b706SJeremy Linton  *	   1, any passed flag set
584bbd1b706SJeremy Linton  *	   0, flag unset
585bbd1b706SJeremy Linton  */
check_acpi_cpu_flag(unsigned int cpu,int rev,u32 flag)586bbd1b706SJeremy Linton static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag)
587bbd1b706SJeremy Linton {
588bbd1b706SJeremy Linton 	struct acpi_table_header *table;
589bbd1b706SJeremy Linton 	u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
590bbd1b706SJeremy Linton 	struct acpi_pptt_processor *cpu_node = NULL;
591bbd1b706SJeremy Linton 	int ret = -ENOENT;
592bbd1b706SJeremy Linton 
5930c80f9e1SSudeep Holla 	table = acpi_get_pptt();
5940c80f9e1SSudeep Holla 	if (!table)
5950c80f9e1SSudeep Holla 		return -ENOENT;
596bbd1b706SJeremy Linton 
597bbd1b706SJeremy Linton 	if (table->revision >= rev)
598bbd1b706SJeremy Linton 		cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
599bbd1b706SJeremy Linton 
600bbd1b706SJeremy Linton 	if (cpu_node)
601bbd1b706SJeremy Linton 		ret = (cpu_node->flags & flag) != 0;
602bbd1b706SJeremy Linton 
603bbd1b706SJeremy Linton 	return ret;
604bbd1b706SJeremy Linton }
605bbd1b706SJeremy Linton 
606bbd1b706SJeremy Linton /**
607bd500361SPierre Gondois  * acpi_get_cache_info() - Determine the number of cache levels and
608bd500361SPierre Gondois  * split cache levels (data/instruction) and for a PE.
609603fadf3SBjorn Helgaas  * @cpu: Kernel logical CPU number
610bd500361SPierre Gondois  * @levels: Number of levels if success.
611bd500361SPierre Gondois  * @split_levels:	Number of levels being split (i.e. data/instruction)
612bd500361SPierre Gondois  *			if success. Can by NULL.
6132bd00bcdSJeremy Linton  *
614603fadf3SBjorn Helgaas  * Given a logical CPU number, returns the number of levels of cache represented
6152bd00bcdSJeremy Linton  * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
6162bd00bcdSJeremy Linton  * indicating we didn't find any cache levels.
6172bd00bcdSJeremy Linton  *
618bd500361SPierre Gondois  * Return: -ENOENT if no PPTT table or no PPTT processor struct found.
619bd500361SPierre Gondois  *	   0 on success.
6202bd00bcdSJeremy Linton  */
acpi_get_cache_info(unsigned int cpu,unsigned int * levels,unsigned int * split_levels)621bd500361SPierre Gondois int acpi_get_cache_info(unsigned int cpu, unsigned int *levels,
622bd500361SPierre Gondois 			unsigned int *split_levels)
6232bd00bcdSJeremy Linton {
624fa4d566aSPierre Gondois 	struct acpi_pptt_processor *cpu_node;
6252bd00bcdSJeremy Linton 	struct acpi_table_header *table;
626fa4d566aSPierre Gondois 	u32 acpi_cpu_id;
6270c80f9e1SSudeep Holla 
628bd500361SPierre Gondois 	*levels = 0;
629bd500361SPierre Gondois 	if (split_levels)
630bd500361SPierre Gondois 		*split_levels = 0;
631bd500361SPierre Gondois 
6320c80f9e1SSudeep Holla 	table = acpi_get_pptt();
6330c80f9e1SSudeep Holla 	if (!table)
6340c80f9e1SSudeep Holla 		return -ENOENT;
6352bd00bcdSJeremy Linton 
636bd500361SPierre Gondois 	pr_debug("Cache Setup: find cache levels for CPU=%d\n", cpu);
6372bd00bcdSJeremy Linton 
6382bd00bcdSJeremy Linton 	acpi_cpu_id = get_acpi_id_for_cpu(cpu);
639fa4d566aSPierre Gondois 	cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
640bd500361SPierre Gondois 	if (!cpu_node)
641bd500361SPierre Gondois 		return -ENOENT;
642fa4d566aSPierre Gondois 
643bd500361SPierre Gondois 	acpi_count_levels(table, cpu_node, levels, split_levels);
6442bd00bcdSJeremy Linton 
645bd500361SPierre Gondois 	pr_debug("Cache Setup: last_level=%d split_levels=%d\n",
646bd500361SPierre Gondois 		 *levels, split_levels ? *split_levels : -1);
647bd500361SPierre Gondois 
648bd500361SPierre Gondois 	return 0;
6492bd00bcdSJeremy Linton }
6502bd00bcdSJeremy Linton 
6512bd00bcdSJeremy Linton /**
6522bd00bcdSJeremy Linton  * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
653603fadf3SBjorn Helgaas  * @cpu: Kernel logical CPU number
6542bd00bcdSJeremy Linton  *
6552bd00bcdSJeremy Linton  * Updates the global cache info provided by cpu_get_cacheinfo()
6562bd00bcdSJeremy Linton  * when there are valid properties in the acpi_pptt_cache nodes. A
6572bd00bcdSJeremy Linton  * successful parse may not result in any updates if none of the
658603fadf3SBjorn Helgaas  * cache levels have any valid flags set.  Further, a unique value is
6592bd00bcdSJeremy Linton  * associated with each known CPU cache entry. This unique value
660603fadf3SBjorn Helgaas  * can be used to determine whether caches are shared between CPUs.
6612bd00bcdSJeremy Linton  *
6622bd00bcdSJeremy Linton  * Return: -ENOENT on failure to find table, or 0 on success
6632bd00bcdSJeremy Linton  */
cache_setup_acpi(unsigned int cpu)6642bd00bcdSJeremy Linton int cache_setup_acpi(unsigned int cpu)
6652bd00bcdSJeremy Linton {
6662bd00bcdSJeremy Linton 	struct acpi_table_header *table;
6670c80f9e1SSudeep Holla 
6680c80f9e1SSudeep Holla 	table = acpi_get_pptt();
6690c80f9e1SSudeep Holla 	if (!table)
6700c80f9e1SSudeep Holla 		return -ENOENT;
6712bd00bcdSJeremy Linton 
672603fadf3SBjorn Helgaas 	pr_debug("Cache Setup ACPI CPU %d\n", cpu);
6732bd00bcdSJeremy Linton 
6742bd00bcdSJeremy Linton 	cache_setup_acpi_cpu(table, cpu);
6752bd00bcdSJeremy Linton 
6760c80f9e1SSudeep Holla 	return 0;
6772bd00bcdSJeremy Linton }
6782bd00bcdSJeremy Linton 
6792bd00bcdSJeremy Linton /**
680bbd1b706SJeremy Linton  * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread
681bbd1b706SJeremy Linton  * @cpu: Kernel logical CPU number
682bbd1b706SJeremy Linton  *
683bbd1b706SJeremy Linton  * Return: 1, a thread
684bbd1b706SJeremy Linton  *         0, not a thread
685bbd1b706SJeremy Linton  *         -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or
686bbd1b706SJeremy Linton  *         the table revision isn't new enough.
687bbd1b706SJeremy Linton  */
acpi_pptt_cpu_is_thread(unsigned int cpu)688bbd1b706SJeremy Linton int acpi_pptt_cpu_is_thread(unsigned int cpu)
689bbd1b706SJeremy Linton {
690bbd1b706SJeremy Linton 	return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD);
691bbd1b706SJeremy Linton }
692bbd1b706SJeremy Linton 
693bbd1b706SJeremy Linton /**
694603fadf3SBjorn Helgaas  * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
695603fadf3SBjorn Helgaas  * @cpu: Kernel logical CPU number
6962bd00bcdSJeremy Linton  * @level: The topological level for which we would like a unique ID
6972bd00bcdSJeremy Linton  *
6982bd00bcdSJeremy Linton  * Determine a topology unique ID for each thread/core/cluster/mc_grouping
6992bd00bcdSJeremy Linton  * /socket/etc. This ID can then be used to group peers, which will have
7002bd00bcdSJeremy Linton  * matching ids.
7012bd00bcdSJeremy Linton  *
7022bd00bcdSJeremy Linton  * The search terminates when either the requested level is found or
7032bd00bcdSJeremy Linton  * we reach a root node. Levels beyond the termination point will return the
7042bd00bcdSJeremy Linton  * same unique ID. The unique id for level 0 is the acpi processor id. All
7052bd00bcdSJeremy Linton  * other levels beyond this use a generated value to uniquely identify
7062bd00bcdSJeremy Linton  * a topological feature.
7072bd00bcdSJeremy Linton  *
708603fadf3SBjorn Helgaas  * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
7092bd00bcdSJeremy Linton  * Otherwise returns a value which represents a unique topological feature.
7102bd00bcdSJeremy Linton  */
find_acpi_cpu_topology(unsigned int cpu,int level)7112bd00bcdSJeremy Linton int find_acpi_cpu_topology(unsigned int cpu, int level)
7122bd00bcdSJeremy Linton {
7132bd00bcdSJeremy Linton 	return find_acpi_cpu_topology_tag(cpu, level, 0);
7142bd00bcdSJeremy Linton }
7152bd00bcdSJeremy Linton 
7162bd00bcdSJeremy Linton /**
717603fadf3SBjorn Helgaas  * find_acpi_cpu_topology_package() - Determine a unique CPU package value
718603fadf3SBjorn Helgaas  * @cpu: Kernel logical CPU number
7192bd00bcdSJeremy Linton  *
720603fadf3SBjorn Helgaas  * Determine a topology unique package ID for the given CPU.
7212bd00bcdSJeremy Linton  * This ID can then be used to group peers, which will have matching ids.
7222bd00bcdSJeremy Linton  *
7232bd00bcdSJeremy Linton  * The search terminates when either a level is found with the PHYSICAL_PACKAGE
7242bd00bcdSJeremy Linton  * flag set or we reach a root node.
7252bd00bcdSJeremy Linton  *
726603fadf3SBjorn Helgaas  * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
727603fadf3SBjorn Helgaas  * Otherwise returns a value which represents the package for this CPU.
7282bd00bcdSJeremy Linton  */
find_acpi_cpu_topology_package(unsigned int cpu)7292bd00bcdSJeremy Linton int find_acpi_cpu_topology_package(unsigned int cpu)
7302bd00bcdSJeremy Linton {
7312bd00bcdSJeremy Linton 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
7322bd00bcdSJeremy Linton 					  ACPI_PPTT_PHYSICAL_PACKAGE);
7332bd00bcdSJeremy Linton }
73456855a99SJeremy Linton 
73556855a99SJeremy Linton /**
736c5e22fefSJonathan Cameron  * find_acpi_cpu_topology_cluster() - Determine a unique CPU cluster value
737c5e22fefSJonathan Cameron  * @cpu: Kernel logical CPU number
738c5e22fefSJonathan Cameron  *
739c5e22fefSJonathan Cameron  * Determine a topology unique cluster ID for the given CPU/thread.
740c5e22fefSJonathan Cameron  * This ID can then be used to group peers, which will have matching ids.
741c5e22fefSJonathan Cameron  *
742c5e22fefSJonathan Cameron  * The cluster, if present is the level of topology above CPUs. In a
743c5e22fefSJonathan Cameron  * multi-thread CPU, it will be the level above the CPU, not the thread.
744c5e22fefSJonathan Cameron  * It may not exist in single CPU systems. In simple multi-CPU systems,
745c5e22fefSJonathan Cameron  * it may be equal to the package topology level.
746c5e22fefSJonathan Cameron  *
747c5e22fefSJonathan Cameron  * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found
748c5e22fefSJonathan Cameron  * or there is no toplogy level above the CPU..
749c5e22fefSJonathan Cameron  * Otherwise returns a value which represents the package for this CPU.
750c5e22fefSJonathan Cameron  */
751c5e22fefSJonathan Cameron 
find_acpi_cpu_topology_cluster(unsigned int cpu)752c5e22fefSJonathan Cameron int find_acpi_cpu_topology_cluster(unsigned int cpu)
753c5e22fefSJonathan Cameron {
754c5e22fefSJonathan Cameron 	struct acpi_table_header *table;
755c5e22fefSJonathan Cameron 	struct acpi_pptt_processor *cpu_node, *cluster_node;
756c5e22fefSJonathan Cameron 	u32 acpi_cpu_id;
757c5e22fefSJonathan Cameron 	int retval;
758c5e22fefSJonathan Cameron 	int is_thread;
759c5e22fefSJonathan Cameron 
7600c80f9e1SSudeep Holla 	table = acpi_get_pptt();
7610c80f9e1SSudeep Holla 	if (!table)
762c5e22fefSJonathan Cameron 		return -ENOENT;
763c5e22fefSJonathan Cameron 
764c5e22fefSJonathan Cameron 	acpi_cpu_id = get_acpi_id_for_cpu(cpu);
765c5e22fefSJonathan Cameron 	cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
7660c80f9e1SSudeep Holla 	if (!cpu_node || !cpu_node->parent)
7670c80f9e1SSudeep Holla 		return -ENOENT;
768c5e22fefSJonathan Cameron 
769c5e22fefSJonathan Cameron 	is_thread = cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD;
770c5e22fefSJonathan Cameron 	cluster_node = fetch_pptt_node(table, cpu_node->parent);
7710c80f9e1SSudeep Holla 	if (!cluster_node)
7720c80f9e1SSudeep Holla 		return -ENOENT;
7730c80f9e1SSudeep Holla 
774c5e22fefSJonathan Cameron 	if (is_thread) {
7750c80f9e1SSudeep Holla 		if (!cluster_node->parent)
7760c80f9e1SSudeep Holla 			return -ENOENT;
7770c80f9e1SSudeep Holla 
778c5e22fefSJonathan Cameron 		cluster_node = fetch_pptt_node(table, cluster_node->parent);
7790c80f9e1SSudeep Holla 		if (!cluster_node)
7800c80f9e1SSudeep Holla 			return -ENOENT;
781c5e22fefSJonathan Cameron 	}
782c5e22fefSJonathan Cameron 	if (cluster_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
783c5e22fefSJonathan Cameron 		retval = cluster_node->acpi_processor_id;
784c5e22fefSJonathan Cameron 	else
785c5e22fefSJonathan Cameron 		retval = ACPI_PTR_DIFF(cluster_node, table);
786c5e22fefSJonathan Cameron 
787c5e22fefSJonathan Cameron 	return retval;
788c5e22fefSJonathan Cameron }
789c5e22fefSJonathan Cameron 
790c5e22fefSJonathan Cameron /**
79156855a99SJeremy Linton  * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag
79256855a99SJeremy Linton  * @cpu: Kernel logical CPU number
79356855a99SJeremy Linton  *
79456855a99SJeremy Linton  * Determine a unique heterogeneous tag for the given CPU. CPUs with the same
79556855a99SJeremy Linton  * implementation should have matching tags.
79656855a99SJeremy Linton  *
79756855a99SJeremy Linton  * The returned tag can be used to group peers with identical implementation.
79856855a99SJeremy Linton  *
79956855a99SJeremy Linton  * The search terminates when a level is found with the identical implementation
80056855a99SJeremy Linton  * flag set or we reach a root node.
80156855a99SJeremy Linton  *
80256855a99SJeremy Linton  * Due to limitations in the PPTT data structure, there may be rare situations
80356855a99SJeremy Linton  * where two cores in a heterogeneous machine may be identical, but won't have
80456855a99SJeremy Linton  * the same tag.
80556855a99SJeremy Linton  *
80656855a99SJeremy Linton  * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
80756855a99SJeremy Linton  * Otherwise returns a value which represents a group of identical cores
80856855a99SJeremy Linton  * similar to this CPU.
80956855a99SJeremy Linton  */
find_acpi_cpu_topology_hetero_id(unsigned int cpu)81056855a99SJeremy Linton int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
81156855a99SJeremy Linton {
81256855a99SJeremy Linton 	return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
81356855a99SJeremy Linton 					  ACPI_PPTT_ACPI_IDENTICAL);
81456855a99SJeremy Linton }
815