objtool: Allow AR to be overridden with HOSTAR
[linux/fpc-iii.git] / drivers / acpi / pptt.c
blobb72e6afaa8fb9a2efc088110e58e0ec6ad4de262
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * pptt.c - parsing of Processor Properties Topology Table (PPTT)
5 * Copyright (C) 2018, ARM
7 * This file implements parsing of the Processor Properties Topology Table
8 * which is optionally used to describe the processor and cache topology.
9 * Due to the relative pointers used throughout the table, this doesn't
10 * leverage the existing subtable parsing in the kernel.
12 * The PPTT structure is an inverted tree, with each node potentially
13 * holding one or two inverted tree data structures describing
14 * the caches available at that level. Each cache structure optionally
15 * contains properties describing the cache at a given level which can be
16 * used to override hardware probed values.
18 #define pr_fmt(fmt) "ACPI PPTT: " fmt
20 #include <linux/acpi.h>
21 #include <linux/cacheinfo.h>
22 #include <acpi/processor.h>
24 static struct acpi_subtable_header *fetch_pptt_subtable(struct acpi_table_header *table_hdr,
25 u32 pptt_ref)
27 struct acpi_subtable_header *entry;
29 /* there isn't a subtable at reference 0 */
30 if (pptt_ref < sizeof(struct acpi_subtable_header))
31 return NULL;
33 if (pptt_ref + sizeof(struct acpi_subtable_header) > table_hdr->length)
34 return NULL;
36 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, pptt_ref);
38 if (entry->length == 0)
39 return NULL;
41 if (pptt_ref + entry->length > table_hdr->length)
42 return NULL;
44 return entry;
47 static struct acpi_pptt_processor *fetch_pptt_node(struct acpi_table_header *table_hdr,
48 u32 pptt_ref)
50 return (struct acpi_pptt_processor *)fetch_pptt_subtable(table_hdr, pptt_ref);
53 static struct acpi_pptt_cache *fetch_pptt_cache(struct acpi_table_header *table_hdr,
54 u32 pptt_ref)
56 return (struct acpi_pptt_cache *)fetch_pptt_subtable(table_hdr, pptt_ref);
59 static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
60 struct acpi_pptt_processor *node,
61 int resource)
63 u32 *ref;
65 if (resource >= node->number_of_priv_resources)
66 return NULL;
68 ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
69 ref += resource;
71 return fetch_pptt_subtable(table_hdr, *ref);
74 static inline bool acpi_pptt_match_type(int table_type, int type)
76 return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
77 table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
80 /**
81 * acpi_pptt_walk_cache() - Attempt to find the requested acpi_pptt_cache
82 * @table_hdr: Pointer to the head of the PPTT table
83 * @local_level: passed res reflects this cache level
84 * @res: cache resource in the PPTT we want to walk
85 * @found: returns a pointer to the requested level if found
86 * @level: the requested cache level
87 * @type: the requested cache type
89 * Attempt to find a given cache level, while counting the max number
90 * of cache levels for the cache node.
92 * Given a pptt resource, verify that it is a cache node, then walk
93 * down each level of caches, counting how many levels are found
94 * as well as checking the cache type (icache, dcache, unified). If a
95 * level & type match, then we set found, and continue the search.
96 * Once the entire cache branch has been walked return its max
97 * depth.
99 * Return: The cache structure and the level we terminated with.
101 static int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr,
102 int local_level,
103 struct acpi_subtable_header *res,
104 struct acpi_pptt_cache **found,
105 int level, int type)
107 struct acpi_pptt_cache *cache;
109 if (res->type != ACPI_PPTT_TYPE_CACHE)
110 return 0;
112 cache = (struct acpi_pptt_cache *) res;
113 while (cache) {
114 local_level++;
116 if (local_level == level &&
117 cache->flags & ACPI_PPTT_CACHE_TYPE_VALID &&
118 acpi_pptt_match_type(cache->attributes, type)) {
119 if (*found != NULL && cache != *found)
120 pr_warn("Found duplicate cache level/type unable to determine uniqueness\n");
122 pr_debug("Found cache @ level %d\n", level);
123 *found = cache;
125 * continue looking at this node's resource list
126 * to verify that we don't find a duplicate
127 * cache node.
130 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
132 return local_level;
135 static struct acpi_pptt_cache *acpi_find_cache_level(struct acpi_table_header *table_hdr,
136 struct acpi_pptt_processor *cpu_node,
137 int *starting_level, int level,
138 int type)
140 struct acpi_subtable_header *res;
141 int number_of_levels = *starting_level;
142 int resource = 0;
143 struct acpi_pptt_cache *ret = NULL;
144 int local_level;
146 /* walk down from processor node */
147 while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
148 resource++;
150 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
151 res, &ret, level, type);
153 * we are looking for the max depth. Since its potentially
154 * possible for a given node to have resources with differing
155 * depths verify that the depth we have found is the largest.
157 if (number_of_levels < local_level)
158 number_of_levels = local_level;
160 if (number_of_levels > *starting_level)
161 *starting_level = number_of_levels;
163 return ret;
167 * acpi_count_levels() - Given a PPTT table, and a CPU node, count the caches
168 * @table_hdr: Pointer to the head of the PPTT table
169 * @cpu_node: processor node we wish to count caches for
171 * Given a processor node containing a processing unit, walk into it and count
172 * how many levels exist solely for it, and then walk up each level until we hit
173 * the root node (ignore the package level because it may be possible to have
174 * caches that exist across packages). Count the number of cache levels that
175 * exist at each level on the way up.
177 * Return: Total number of levels found.
179 static int acpi_count_levels(struct acpi_table_header *table_hdr,
180 struct acpi_pptt_processor *cpu_node)
182 int total_levels = 0;
184 do {
185 acpi_find_cache_level(table_hdr, cpu_node, &total_levels, 0, 0);
186 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
187 } while (cpu_node);
189 return total_levels;
193 * acpi_pptt_leaf_node() - Given a processor node, determine if its a leaf
194 * @table_hdr: Pointer to the head of the PPTT table
195 * @node: passed node is checked to see if its a leaf
197 * Determine if the *node parameter is a leaf node by iterating the
198 * PPTT table, looking for nodes which reference it.
200 * Return: 0 if we find a node referencing the passed node (or table error),
201 * or 1 if we don't.
203 static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
204 struct acpi_pptt_processor *node)
206 struct acpi_subtable_header *entry;
207 unsigned long table_end;
208 u32 node_entry;
209 struct acpi_pptt_processor *cpu_node;
210 u32 proc_sz;
212 if (table_hdr->revision > 1)
213 return (node->flags & ACPI_PPTT_ACPI_LEAF_NODE);
215 table_end = (unsigned long)table_hdr + table_hdr->length;
216 node_entry = ACPI_PTR_DIFF(node, table_hdr);
217 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
218 sizeof(struct acpi_table_pptt));
219 proc_sz = sizeof(struct acpi_pptt_processor *);
221 while ((unsigned long)entry + proc_sz < table_end) {
222 cpu_node = (struct acpi_pptt_processor *)entry;
223 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
224 cpu_node->parent == node_entry)
225 return 0;
226 if (entry->length == 0)
227 return 0;
228 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
229 entry->length);
232 return 1;
236 * acpi_find_processor_node() - Given a PPTT table find the requested processor
237 * @table_hdr: Pointer to the head of the PPTT table
238 * @acpi_cpu_id: CPU we are searching for
240 * Find the subtable entry describing the provided processor.
241 * This is done by iterating the PPTT table looking for processor nodes
242 * which have an acpi_processor_id that matches the acpi_cpu_id parameter
243 * passed into the function. If we find a node that matches this criteria
244 * we verify that its a leaf node in the topology rather than depending
245 * on the valid flag, which doesn't need to be set for leaf nodes.
247 * Return: NULL, or the processors acpi_pptt_processor*
249 static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
250 u32 acpi_cpu_id)
252 struct acpi_subtable_header *entry;
253 unsigned long table_end;
254 struct acpi_pptt_processor *cpu_node;
255 u32 proc_sz;
257 table_end = (unsigned long)table_hdr + table_hdr->length;
258 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
259 sizeof(struct acpi_table_pptt));
260 proc_sz = sizeof(struct acpi_pptt_processor *);
262 /* find the processor structure associated with this cpuid */
263 while ((unsigned long)entry + proc_sz < table_end) {
264 cpu_node = (struct acpi_pptt_processor *)entry;
266 if (entry->length == 0) {
267 pr_warn("Invalid zero length subtable\n");
268 break;
270 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
271 acpi_cpu_id == cpu_node->acpi_processor_id &&
272 acpi_pptt_leaf_node(table_hdr, cpu_node)) {
273 return (struct acpi_pptt_processor *)entry;
276 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
277 entry->length);
280 return NULL;
283 static int acpi_find_cache_levels(struct acpi_table_header *table_hdr,
284 u32 acpi_cpu_id)
286 int number_of_levels = 0;
287 struct acpi_pptt_processor *cpu;
289 cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id);
290 if (cpu)
291 number_of_levels = acpi_count_levels(table_hdr, cpu);
293 return number_of_levels;
296 static u8 acpi_cache_type(enum cache_type type)
298 switch (type) {
299 case CACHE_TYPE_DATA:
300 pr_debug("Looking for data cache\n");
301 return ACPI_PPTT_CACHE_TYPE_DATA;
302 case CACHE_TYPE_INST:
303 pr_debug("Looking for instruction cache\n");
304 return ACPI_PPTT_CACHE_TYPE_INSTR;
305 default:
306 case CACHE_TYPE_UNIFIED:
307 pr_debug("Looking for unified cache\n");
309 * It is important that ACPI_PPTT_CACHE_TYPE_UNIFIED
310 * contains the bit pattern that will match both
311 * ACPI unified bit patterns because we use it later
312 * to match both cases.
314 return ACPI_PPTT_CACHE_TYPE_UNIFIED;
318 static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr,
319 u32 acpi_cpu_id,
320 enum cache_type type,
321 unsigned int level,
322 struct acpi_pptt_processor **node)
324 int total_levels = 0;
325 struct acpi_pptt_cache *found = NULL;
326 struct acpi_pptt_processor *cpu_node;
327 u8 acpi_type = acpi_cache_type(type);
329 pr_debug("Looking for CPU %d's level %d cache type %d\n",
330 acpi_cpu_id, level, acpi_type);
332 cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
334 while (cpu_node && !found) {
335 found = acpi_find_cache_level(table_hdr, cpu_node,
336 &total_levels, level, acpi_type);
337 *node = cpu_node;
338 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
341 return found;
345 * update_cache_properties() - Update cacheinfo for the given processor
346 * @this_leaf: Kernel cache info structure being updated
347 * @found_cache: The PPTT node describing this cache instance
348 * @cpu_node: A unique reference to describe this cache instance
350 * The ACPI spec implies that the fields in the cache structures are used to
351 * extend and correct the information probed from the hardware. Lets only
352 * set fields that we determine are VALID.
354 * Return: nothing. Side effect of updating the global cacheinfo
356 static void update_cache_properties(struct cacheinfo *this_leaf,
357 struct acpi_pptt_cache *found_cache,
358 struct acpi_pptt_processor *cpu_node)
360 this_leaf->fw_token = cpu_node;
361 if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID)
362 this_leaf->size = found_cache->size;
363 if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID)
364 this_leaf->coherency_line_size = found_cache->line_size;
365 if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID)
366 this_leaf->number_of_sets = found_cache->number_of_sets;
367 if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID)
368 this_leaf->ways_of_associativity = found_cache->associativity;
369 if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
370 switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
371 case ACPI_PPTT_CACHE_POLICY_WT:
372 this_leaf->attributes = CACHE_WRITE_THROUGH;
373 break;
374 case ACPI_PPTT_CACHE_POLICY_WB:
375 this_leaf->attributes = CACHE_WRITE_BACK;
376 break;
379 if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
380 switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
381 case ACPI_PPTT_CACHE_READ_ALLOCATE:
382 this_leaf->attributes |= CACHE_READ_ALLOCATE;
383 break;
384 case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
385 this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
386 break;
387 case ACPI_PPTT_CACHE_RW_ALLOCATE:
388 case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
389 this_leaf->attributes |=
390 CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
391 break;
395 * If cache type is NOCACHE, then the cache hasn't been specified
396 * via other mechanisms. Update the type if a cache type has been
397 * provided.
399 * Note, we assume such caches are unified based on conventional system
400 * design and known examples. Significant work is required elsewhere to
401 * fully support data/instruction only type caches which are only
402 * specified in PPTT.
404 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
405 found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
406 this_leaf->type = CACHE_TYPE_UNIFIED;
409 static void cache_setup_acpi_cpu(struct acpi_table_header *table,
410 unsigned int cpu)
412 struct acpi_pptt_cache *found_cache;
413 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
414 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
415 struct cacheinfo *this_leaf;
416 unsigned int index = 0;
417 struct acpi_pptt_processor *cpu_node = NULL;
419 while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
420 this_leaf = this_cpu_ci->info_list + index;
421 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
422 this_leaf->type,
423 this_leaf->level,
424 &cpu_node);
425 pr_debug("found = %p %p\n", found_cache, cpu_node);
426 if (found_cache)
427 update_cache_properties(this_leaf,
428 found_cache,
429 cpu_node);
431 index++;
435 /* Passing level values greater than this will result in search termination */
436 #define PPTT_ABORT_PACKAGE 0xFF
438 static struct acpi_pptt_processor *acpi_find_processor_package_id(struct acpi_table_header *table_hdr,
439 struct acpi_pptt_processor *cpu,
440 int level, int flag)
442 struct acpi_pptt_processor *prev_node;
444 while (cpu && level) {
445 if (cpu->flags & flag)
446 break;
447 pr_debug("level %d\n", level);
448 prev_node = fetch_pptt_node(table_hdr, cpu->parent);
449 if (prev_node == NULL)
450 break;
451 cpu = prev_node;
452 level--;
454 return cpu;
457 static void acpi_pptt_warn_missing(void)
459 pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
463 * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
464 * @table: Pointer to the head of the PPTT table
465 * @cpu: Kernel logical CPU number
466 * @level: A level that terminates the search
467 * @flag: A flag which terminates the search
469 * Get a unique value given a CPU, and a topology level, that can be
470 * matched to determine which cpus share common topological features
471 * at that level.
473 * Return: Unique value, or -ENOENT if unable to locate CPU
475 static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
476 unsigned int cpu, int level, int flag)
478 struct acpi_pptt_processor *cpu_node;
479 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
481 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
482 if (cpu_node) {
483 cpu_node = acpi_find_processor_package_id(table, cpu_node,
484 level, flag);
486 * As per specification if the processor structure represents
487 * an actual processor, then ACPI processor ID must be valid.
488 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
489 * should be set if the UID is valid
491 if (level == 0 ||
492 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
493 return cpu_node->acpi_processor_id;
494 return ACPI_PTR_DIFF(cpu_node, table);
496 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
497 cpu, acpi_cpu_id);
498 return -ENOENT;
501 static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
503 struct acpi_table_header *table;
504 acpi_status status;
505 int retval;
507 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
508 if (ACPI_FAILURE(status)) {
509 acpi_pptt_warn_missing();
510 return -ENOENT;
512 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
513 pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
514 cpu, level, retval);
515 acpi_put_table(table);
517 return retval;
521 * acpi_find_last_cache_level() - Determines the number of cache levels for a PE
522 * @cpu: Kernel logical CPU number
524 * Given a logical CPU number, returns the number of levels of cache represented
525 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
526 * indicating we didn't find any cache levels.
528 * Return: Cache levels visible to this core.
530 int acpi_find_last_cache_level(unsigned int cpu)
532 u32 acpi_cpu_id;
533 struct acpi_table_header *table;
534 int number_of_levels = 0;
535 acpi_status status;
537 pr_debug("Cache Setup find last level CPU=%d\n", cpu);
539 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
540 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
541 if (ACPI_FAILURE(status)) {
542 acpi_pptt_warn_missing();
543 } else {
544 number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id);
545 acpi_put_table(table);
547 pr_debug("Cache Setup find last level level=%d\n", number_of_levels);
549 return number_of_levels;
553 * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
554 * @cpu: Kernel logical CPU number
556 * Updates the global cache info provided by cpu_get_cacheinfo()
557 * when there are valid properties in the acpi_pptt_cache nodes. A
558 * successful parse may not result in any updates if none of the
559 * cache levels have any valid flags set. Further, a unique value is
560 * associated with each known CPU cache entry. This unique value
561 * can be used to determine whether caches are shared between CPUs.
563 * Return: -ENOENT on failure to find table, or 0 on success
565 int cache_setup_acpi(unsigned int cpu)
567 struct acpi_table_header *table;
568 acpi_status status;
570 pr_debug("Cache Setup ACPI CPU %d\n", cpu);
572 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
573 if (ACPI_FAILURE(status)) {
574 acpi_pptt_warn_missing();
575 return -ENOENT;
578 cache_setup_acpi_cpu(table, cpu);
579 acpi_put_table(table);
581 return status;
585 * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
586 * @cpu: Kernel logical CPU number
587 * @level: The topological level for which we would like a unique ID
589 * Determine a topology unique ID for each thread/core/cluster/mc_grouping
590 * /socket/etc. This ID can then be used to group peers, which will have
591 * matching ids.
593 * The search terminates when either the requested level is found or
594 * we reach a root node. Levels beyond the termination point will return the
595 * same unique ID. The unique id for level 0 is the acpi processor id. All
596 * other levels beyond this use a generated value to uniquely identify
597 * a topological feature.
599 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
600 * Otherwise returns a value which represents a unique topological feature.
602 int find_acpi_cpu_topology(unsigned int cpu, int level)
604 return find_acpi_cpu_topology_tag(cpu, level, 0);
608 * find_acpi_cpu_cache_topology() - Determine a unique cache topology value
609 * @cpu: Kernel logical CPU number
610 * @level: The cache level for which we would like a unique ID
612 * Determine a unique ID for each unified cache in the system
614 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
615 * Otherwise returns a value which represents a unique topological feature.
617 int find_acpi_cpu_cache_topology(unsigned int cpu, int level)
619 struct acpi_table_header *table;
620 struct acpi_pptt_cache *found_cache;
621 acpi_status status;
622 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
623 struct acpi_pptt_processor *cpu_node = NULL;
624 int ret = -1;
626 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
627 if (ACPI_FAILURE(status)) {
628 acpi_pptt_warn_missing();
629 return -ENOENT;
632 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
633 CACHE_TYPE_UNIFIED,
634 level,
635 &cpu_node);
636 if (found_cache)
637 ret = ACPI_PTR_DIFF(cpu_node, table);
639 acpi_put_table(table);
641 return ret;
646 * find_acpi_cpu_topology_package() - Determine a unique CPU package value
647 * @cpu: Kernel logical CPU number
649 * Determine a topology unique package ID for the given CPU.
650 * This ID can then be used to group peers, which will have matching ids.
652 * The search terminates when either a level is found with the PHYSICAL_PACKAGE
653 * flag set or we reach a root node.
655 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
656 * Otherwise returns a value which represents the package for this CPU.
658 int find_acpi_cpu_topology_package(unsigned int cpu)
660 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
661 ACPI_PPTT_PHYSICAL_PACKAGE);