Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * pptt.c - parsing of Processor Properties Topology Table (PPTT)
4 *
5 * Copyright (C) 2018, ARM
6 *
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.
11 *
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.
17 */
18#define pr_fmt(fmt) "ACPI PPTT: " fmt
19
20#include <linux/acpi.h>
21#include <linux/cacheinfo.h>
22#include <acpi/processor.h>
23
24static struct acpi_subtable_header *fetch_pptt_subtable(struct acpi_table_header *table_hdr,
25 u32 pptt_ref)
26{
27 struct acpi_subtable_header *entry;
28
29 /* there isn't a subtable at reference 0 */
30 if (pptt_ref < sizeof(struct acpi_subtable_header))
31 return NULL;
32
33 if (pptt_ref + sizeof(struct acpi_subtable_header) > table_hdr->length)
34 return NULL;
35
36 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, pptt_ref);
37
38 if (entry->length == 0)
39 return NULL;
40
41 if (pptt_ref + entry->length > table_hdr->length)
42 return NULL;
43
44 return entry;
45}
46
47static struct acpi_pptt_processor *fetch_pptt_node(struct acpi_table_header *table_hdr,
48 u32 pptt_ref)
49{
50 return (struct acpi_pptt_processor *)fetch_pptt_subtable(table_hdr, pptt_ref);
51}
52
53static struct acpi_pptt_cache *fetch_pptt_cache(struct acpi_table_header *table_hdr,
54 u32 pptt_ref)
55{
56 return (struct acpi_pptt_cache *)fetch_pptt_subtable(table_hdr, pptt_ref);
57}
58
59static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
60 struct acpi_pptt_processor *node,
61 int resource)
62{
63 u32 *ref;
64
65 if (resource >= node->number_of_priv_resources)
66 return NULL;
67
68 ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
69 ref += resource;
70
71 return fetch_pptt_subtable(table_hdr, *ref);
72}
73
74static inline bool acpi_pptt_match_type(int table_type, int type)
75{
76 return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
77 table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
78}
79
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
88 *
89 * Attempt to find a given cache level, while counting the max number
90 * of cache levels for the cache node.
91 *
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.
98 *
99 * Return: The cache structure and the level we terminated with.
100 */
101static unsigned int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr,
102 unsigned int local_level,
103 struct acpi_subtable_header *res,
104 struct acpi_pptt_cache **found,
105 unsigned int level, int type)
106{
107 struct acpi_pptt_cache *cache;
108
109 if (res->type != ACPI_PPTT_TYPE_CACHE)
110 return 0;
111
112 cache = (struct acpi_pptt_cache *) res;
113 while (cache) {
114 local_level++;
115
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");
121
122 pr_debug("Found cache @ level %u\n", level);
123 *found = cache;
124 /*
125 * continue looking at this node's resource list
126 * to verify that we don't find a duplicate
127 * cache node.
128 */
129 }
130 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
131 }
132 return local_level;
133}
134
135static struct acpi_pptt_cache *
136acpi_find_cache_level(struct acpi_table_header *table_hdr,
137 struct acpi_pptt_processor *cpu_node,
138 unsigned int *starting_level, unsigned int level,
139 int type)
140{
141 struct acpi_subtable_header *res;
142 unsigned int number_of_levels = *starting_level;
143 int resource = 0;
144 struct acpi_pptt_cache *ret = NULL;
145 unsigned int local_level;
146
147 /* walk down from processor node */
148 while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
149 resource++;
150
151 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
152 res, &ret, level, type);
153 /*
154 * we are looking for the max depth. Since its potentially
155 * possible for a given node to have resources with differing
156 * depths verify that the depth we have found is the largest.
157 */
158 if (number_of_levels < local_level)
159 number_of_levels = local_level;
160 }
161 if (number_of_levels > *starting_level)
162 *starting_level = number_of_levels;
163
164 return ret;
165}
166
167/**
168 * acpi_count_levels() - Given a PPTT table, and a CPU node, count the caches
169 * @table_hdr: Pointer to the head of the PPTT table
170 * @cpu_node: processor node we wish to count caches for
171 *
172 * Given a processor node containing a processing unit, walk into it and count
173 * how many levels exist solely for it, and then walk up each level until we hit
174 * the root node (ignore the package level because it may be possible to have
175 * caches that exist across packages). Count the number of cache levels that
176 * exist at each level on the way up.
177 *
178 * Return: Total number of levels found.
179 */
180static int acpi_count_levels(struct acpi_table_header *table_hdr,
181 struct acpi_pptt_processor *cpu_node)
182{
183 int total_levels = 0;
184
185 do {
186 acpi_find_cache_level(table_hdr, cpu_node, &total_levels, 0, 0);
187 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
188 } while (cpu_node);
189
190 return total_levels;
191}
192
193/**
194 * acpi_pptt_leaf_node() - Given a processor node, determine if its a leaf
195 * @table_hdr: Pointer to the head of the PPTT table
196 * @node: passed node is checked to see if its a leaf
197 *
198 * Determine if the *node parameter is a leaf node by iterating the
199 * PPTT table, looking for nodes which reference it.
200 *
201 * Return: 0 if we find a node referencing the passed node (or table error),
202 * or 1 if we don't.
203 */
204static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
205 struct acpi_pptt_processor *node)
206{
207 struct acpi_subtable_header *entry;
208 unsigned long table_end;
209 u32 node_entry;
210 struct acpi_pptt_processor *cpu_node;
211 u32 proc_sz;
212
213 if (table_hdr->revision > 1)
214 return (node->flags & ACPI_PPTT_ACPI_LEAF_NODE);
215
216 table_end = (unsigned long)table_hdr + table_hdr->length;
217 node_entry = ACPI_PTR_DIFF(node, table_hdr);
218 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
219 sizeof(struct acpi_table_pptt));
220 proc_sz = sizeof(struct acpi_pptt_processor *);
221
222 while ((unsigned long)entry + proc_sz < table_end) {
223 cpu_node = (struct acpi_pptt_processor *)entry;
224 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
225 cpu_node->parent == node_entry)
226 return 0;
227 if (entry->length == 0)
228 return 0;
229 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
230 entry->length);
231
232 }
233 return 1;
234}
235
236/**
237 * acpi_find_processor_node() - Given a PPTT table find the requested processor
238 * @table_hdr: Pointer to the head of the PPTT table
239 * @acpi_cpu_id: CPU we are searching for
240 *
241 * Find the subtable entry describing the provided processor.
242 * This is done by iterating the PPTT table looking for processor nodes
243 * which have an acpi_processor_id that matches the acpi_cpu_id parameter
244 * passed into the function. If we find a node that matches this criteria
245 * we verify that its a leaf node in the topology rather than depending
246 * on the valid flag, which doesn't need to be set for leaf nodes.
247 *
248 * Return: NULL, or the processors acpi_pptt_processor*
249 */
250static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
251 u32 acpi_cpu_id)
252{
253 struct acpi_subtable_header *entry;
254 unsigned long table_end;
255 struct acpi_pptt_processor *cpu_node;
256 u32 proc_sz;
257
258 table_end = (unsigned long)table_hdr + table_hdr->length;
259 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
260 sizeof(struct acpi_table_pptt));
261 proc_sz = sizeof(struct acpi_pptt_processor *);
262
263 /* find the processor structure associated with this cpuid */
264 while ((unsigned long)entry + proc_sz < table_end) {
265 cpu_node = (struct acpi_pptt_processor *)entry;
266
267 if (entry->length == 0) {
268 pr_warn("Invalid zero length subtable\n");
269 break;
270 }
271 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
272 acpi_cpu_id == cpu_node->acpi_processor_id &&
273 acpi_pptt_leaf_node(table_hdr, cpu_node)) {
274 return (struct acpi_pptt_processor *)entry;
275 }
276
277 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
278 entry->length);
279 }
280
281 return NULL;
282}
283
284static int acpi_find_cache_levels(struct acpi_table_header *table_hdr,
285 u32 acpi_cpu_id)
286{
287 int number_of_levels = 0;
288 struct acpi_pptt_processor *cpu;
289
290 cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id);
291 if (cpu)
292 number_of_levels = acpi_count_levels(table_hdr, cpu);
293
294 return number_of_levels;
295}
296
297static u8 acpi_cache_type(enum cache_type type)
298{
299 switch (type) {
300 case CACHE_TYPE_DATA:
301 pr_debug("Looking for data cache\n");
302 return ACPI_PPTT_CACHE_TYPE_DATA;
303 case CACHE_TYPE_INST:
304 pr_debug("Looking for instruction cache\n");
305 return ACPI_PPTT_CACHE_TYPE_INSTR;
306 default:
307 case CACHE_TYPE_UNIFIED:
308 pr_debug("Looking for unified cache\n");
309 /*
310 * It is important that ACPI_PPTT_CACHE_TYPE_UNIFIED
311 * contains the bit pattern that will match both
312 * ACPI unified bit patterns because we use it later
313 * to match both cases.
314 */
315 return ACPI_PPTT_CACHE_TYPE_UNIFIED;
316 }
317}
318
319static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr,
320 u32 acpi_cpu_id,
321 enum cache_type type,
322 unsigned int level,
323 struct acpi_pptt_processor **node)
324{
325 unsigned int total_levels = 0;
326 struct acpi_pptt_cache *found = NULL;
327 struct acpi_pptt_processor *cpu_node;
328 u8 acpi_type = acpi_cache_type(type);
329
330 pr_debug("Looking for CPU %d's level %u cache type %d\n",
331 acpi_cpu_id, level, acpi_type);
332
333 cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
334
335 while (cpu_node && !found) {
336 found = acpi_find_cache_level(table_hdr, cpu_node,
337 &total_levels, level, acpi_type);
338 *node = cpu_node;
339 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
340 }
341
342 return found;
343}
344
345/**
346 * update_cache_properties() - Update cacheinfo for the given processor
347 * @this_leaf: Kernel cache info structure being updated
348 * @found_cache: The PPTT node describing this cache instance
349 * @cpu_node: A unique reference to describe this cache instance
350 *
351 * The ACPI spec implies that the fields in the cache structures are used to
352 * extend and correct the information probed from the hardware. Lets only
353 * set fields that we determine are VALID.
354 *
355 * Return: nothing. Side effect of updating the global cacheinfo
356 */
357static void update_cache_properties(struct cacheinfo *this_leaf,
358 struct acpi_pptt_cache *found_cache,
359 struct acpi_pptt_processor *cpu_node)
360{
361 this_leaf->fw_token = cpu_node;
362 if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID)
363 this_leaf->size = found_cache->size;
364 if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID)
365 this_leaf->coherency_line_size = found_cache->line_size;
366 if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID)
367 this_leaf->number_of_sets = found_cache->number_of_sets;
368 if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID)
369 this_leaf->ways_of_associativity = found_cache->associativity;
370 if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
371 switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
372 case ACPI_PPTT_CACHE_POLICY_WT:
373 this_leaf->attributes = CACHE_WRITE_THROUGH;
374 break;
375 case ACPI_PPTT_CACHE_POLICY_WB:
376 this_leaf->attributes = CACHE_WRITE_BACK;
377 break;
378 }
379 }
380 if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
381 switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
382 case ACPI_PPTT_CACHE_READ_ALLOCATE:
383 this_leaf->attributes |= CACHE_READ_ALLOCATE;
384 break;
385 case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
386 this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
387 break;
388 case ACPI_PPTT_CACHE_RW_ALLOCATE:
389 case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
390 this_leaf->attributes |=
391 CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
392 break;
393 }
394 }
395 /*
396 * If cache type is NOCACHE, then the cache hasn't been specified
397 * via other mechanisms. Update the type if a cache type has been
398 * provided.
399 *
400 * Note, we assume such caches are unified based on conventional system
401 * design and known examples. Significant work is required elsewhere to
402 * fully support data/instruction only type caches which are only
403 * specified in PPTT.
404 */
405 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
406 found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
407 this_leaf->type = CACHE_TYPE_UNIFIED;
408}
409
410static void cache_setup_acpi_cpu(struct acpi_table_header *table,
411 unsigned int cpu)
412{
413 struct acpi_pptt_cache *found_cache;
414 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
415 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
416 struct cacheinfo *this_leaf;
417 unsigned int index = 0;
418 struct acpi_pptt_processor *cpu_node = NULL;
419
420 while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
421 this_leaf = this_cpu_ci->info_list + index;
422 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
423 this_leaf->type,
424 this_leaf->level,
425 &cpu_node);
426 pr_debug("found = %p %p\n", found_cache, cpu_node);
427 if (found_cache)
428 update_cache_properties(this_leaf,
429 found_cache,
430 cpu_node);
431
432 index++;
433 }
434}
435
436static bool flag_identical(struct acpi_table_header *table_hdr,
437 struct acpi_pptt_processor *cpu)
438{
439 struct acpi_pptt_processor *next;
440
441 /* heterogeneous machines must use PPTT revision > 1 */
442 if (table_hdr->revision < 2)
443 return false;
444
445 /* Locate the last node in the tree with IDENTICAL set */
446 if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) {
447 next = fetch_pptt_node(table_hdr, cpu->parent);
448 if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL))
449 return true;
450 }
451
452 return false;
453}
454
455/* Passing level values greater than this will result in search termination */
456#define PPTT_ABORT_PACKAGE 0xFF
457
458static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr,
459 struct acpi_pptt_processor *cpu,
460 int level, int flag)
461{
462 struct acpi_pptt_processor *prev_node;
463
464 while (cpu && level) {
465 /* special case the identical flag to find last identical */
466 if (flag == ACPI_PPTT_ACPI_IDENTICAL) {
467 if (flag_identical(table_hdr, cpu))
468 break;
469 } else if (cpu->flags & flag)
470 break;
471 pr_debug("level %d\n", level);
472 prev_node = fetch_pptt_node(table_hdr, cpu->parent);
473 if (prev_node == NULL)
474 break;
475 cpu = prev_node;
476 level--;
477 }
478 return cpu;
479}
480
481static void acpi_pptt_warn_missing(void)
482{
483 pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
484}
485
486/**
487 * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
488 * @table: Pointer to the head of the PPTT table
489 * @cpu: Kernel logical CPU number
490 * @level: A level that terminates the search
491 * @flag: A flag which terminates the search
492 *
493 * Get a unique value given a CPU, and a topology level, that can be
494 * matched to determine which cpus share common topological features
495 * at that level.
496 *
497 * Return: Unique value, or -ENOENT if unable to locate CPU
498 */
499static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
500 unsigned int cpu, int level, int flag)
501{
502 struct acpi_pptt_processor *cpu_node;
503 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
504
505 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
506 if (cpu_node) {
507 cpu_node = acpi_find_processor_tag(table, cpu_node,
508 level, flag);
509 /*
510 * As per specification if the processor structure represents
511 * an actual processor, then ACPI processor ID must be valid.
512 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
513 * should be set if the UID is valid
514 */
515 if (level == 0 ||
516 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
517 return cpu_node->acpi_processor_id;
518 return ACPI_PTR_DIFF(cpu_node, table);
519 }
520 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
521 cpu, acpi_cpu_id);
522 return -ENOENT;
523}
524
525static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
526{
527 struct acpi_table_header *table;
528 acpi_status status;
529 int retval;
530
531 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
532 if (ACPI_FAILURE(status)) {
533 acpi_pptt_warn_missing();
534 return -ENOENT;
535 }
536 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
537 pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
538 cpu, level, retval);
539 acpi_put_table(table);
540
541 return retval;
542}
543
544/**
545 * check_acpi_cpu_flag() - Determine if CPU node has a flag set
546 * @cpu: Kernel logical CPU number
547 * @rev: The minimum PPTT revision defining the flag
548 * @flag: The flag itself
549 *
550 * Check the node representing a CPU for a given flag.
551 *
552 * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or
553 * the table revision isn't new enough.
554 * 1, any passed flag set
555 * 0, flag unset
556 */
557static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag)
558{
559 struct acpi_table_header *table;
560 acpi_status status;
561 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
562 struct acpi_pptt_processor *cpu_node = NULL;
563 int ret = -ENOENT;
564
565 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
566 if (ACPI_FAILURE(status)) {
567 acpi_pptt_warn_missing();
568 return ret;
569 }
570
571 if (table->revision >= rev)
572 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
573
574 if (cpu_node)
575 ret = (cpu_node->flags & flag) != 0;
576
577 acpi_put_table(table);
578
579 return ret;
580}
581
582/**
583 * acpi_find_last_cache_level() - Determines the number of cache levels for a PE
584 * @cpu: Kernel logical CPU number
585 *
586 * Given a logical CPU number, returns the number of levels of cache represented
587 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
588 * indicating we didn't find any cache levels.
589 *
590 * Return: Cache levels visible to this core.
591 */
592int acpi_find_last_cache_level(unsigned int cpu)
593{
594 u32 acpi_cpu_id;
595 struct acpi_table_header *table;
596 int number_of_levels = 0;
597 acpi_status status;
598
599 pr_debug("Cache Setup find last level CPU=%d\n", cpu);
600
601 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
602 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
603 if (ACPI_FAILURE(status)) {
604 acpi_pptt_warn_missing();
605 } else {
606 number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id);
607 acpi_put_table(table);
608 }
609 pr_debug("Cache Setup find last level level=%d\n", number_of_levels);
610
611 return number_of_levels;
612}
613
614/**
615 * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
616 * @cpu: Kernel logical CPU number
617 *
618 * Updates the global cache info provided by cpu_get_cacheinfo()
619 * when there are valid properties in the acpi_pptt_cache nodes. A
620 * successful parse may not result in any updates if none of the
621 * cache levels have any valid flags set. Further, a unique value is
622 * associated with each known CPU cache entry. This unique value
623 * can be used to determine whether caches are shared between CPUs.
624 *
625 * Return: -ENOENT on failure to find table, or 0 on success
626 */
627int cache_setup_acpi(unsigned int cpu)
628{
629 struct acpi_table_header *table;
630 acpi_status status;
631
632 pr_debug("Cache Setup ACPI CPU %d\n", cpu);
633
634 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
635 if (ACPI_FAILURE(status)) {
636 acpi_pptt_warn_missing();
637 return -ENOENT;
638 }
639
640 cache_setup_acpi_cpu(table, cpu);
641 acpi_put_table(table);
642
643 return status;
644}
645
646/**
647 * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread
648 * @cpu: Kernel logical CPU number
649 *
650 * Return: 1, a thread
651 * 0, not a thread
652 * -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or
653 * the table revision isn't new enough.
654 */
655int acpi_pptt_cpu_is_thread(unsigned int cpu)
656{
657 return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD);
658}
659
660/**
661 * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
662 * @cpu: Kernel logical CPU number
663 * @level: The topological level for which we would like a unique ID
664 *
665 * Determine a topology unique ID for each thread/core/cluster/mc_grouping
666 * /socket/etc. This ID can then be used to group peers, which will have
667 * matching ids.
668 *
669 * The search terminates when either the requested level is found or
670 * we reach a root node. Levels beyond the termination point will return the
671 * same unique ID. The unique id for level 0 is the acpi processor id. All
672 * other levels beyond this use a generated value to uniquely identify
673 * a topological feature.
674 *
675 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
676 * Otherwise returns a value which represents a unique topological feature.
677 */
678int find_acpi_cpu_topology(unsigned int cpu, int level)
679{
680 return find_acpi_cpu_topology_tag(cpu, level, 0);
681}
682
683/**
684 * find_acpi_cpu_cache_topology() - Determine a unique cache topology value
685 * @cpu: Kernel logical CPU number
686 * @level: The cache level for which we would like a unique ID
687 *
688 * Determine a unique ID for each unified cache in the system
689 *
690 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
691 * Otherwise returns a value which represents a unique topological feature.
692 */
693int find_acpi_cpu_cache_topology(unsigned int cpu, int level)
694{
695 struct acpi_table_header *table;
696 struct acpi_pptt_cache *found_cache;
697 acpi_status status;
698 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
699 struct acpi_pptt_processor *cpu_node = NULL;
700 int ret = -1;
701
702 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
703 if (ACPI_FAILURE(status)) {
704 acpi_pptt_warn_missing();
705 return -ENOENT;
706 }
707
708 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
709 CACHE_TYPE_UNIFIED,
710 level,
711 &cpu_node);
712 if (found_cache)
713 ret = ACPI_PTR_DIFF(cpu_node, table);
714
715 acpi_put_table(table);
716
717 return ret;
718}
719
720/**
721 * find_acpi_cpu_topology_package() - Determine a unique CPU package value
722 * @cpu: Kernel logical CPU number
723 *
724 * Determine a topology unique package ID for the given CPU.
725 * This ID can then be used to group peers, which will have matching ids.
726 *
727 * The search terminates when either a level is found with the PHYSICAL_PACKAGE
728 * flag set or we reach a root node.
729 *
730 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
731 * Otherwise returns a value which represents the package for this CPU.
732 */
733int find_acpi_cpu_topology_package(unsigned int cpu)
734{
735 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
736 ACPI_PPTT_PHYSICAL_PACKAGE);
737}
738
739/**
740 * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag
741 * @cpu: Kernel logical CPU number
742 *
743 * Determine a unique heterogeneous tag for the given CPU. CPUs with the same
744 * implementation should have matching tags.
745 *
746 * The returned tag can be used to group peers with identical implementation.
747 *
748 * The search terminates when a level is found with the identical implementation
749 * flag set or we reach a root node.
750 *
751 * Due to limitations in the PPTT data structure, there may be rare situations
752 * where two cores in a heterogeneous machine may be identical, but won't have
753 * the same tag.
754 *
755 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
756 * Otherwise returns a value which represents a group of identical cores
757 * similar to this CPU.
758 */
759int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
760{
761 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
762 ACPI_PPTT_ACPI_IDENTICAL);
763}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * pptt.c - parsing of Processor Properties Topology Table (PPTT)
4 *
5 * Copyright (C) 2018, ARM
6 *
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.
11 *
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.
17 */
18#define pr_fmt(fmt) "ACPI PPTT: " fmt
19
20#include <linux/acpi.h>
21#include <linux/cacheinfo.h>
22#include <acpi/processor.h>
23
24static struct acpi_subtable_header *fetch_pptt_subtable(struct acpi_table_header *table_hdr,
25 u32 pptt_ref)
26{
27 struct acpi_subtable_header *entry;
28
29 /* there isn't a subtable at reference 0 */
30 if (pptt_ref < sizeof(struct acpi_subtable_header))
31 return NULL;
32
33 if (pptt_ref + sizeof(struct acpi_subtable_header) > table_hdr->length)
34 return NULL;
35
36 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, pptt_ref);
37
38 if (entry->length == 0)
39 return NULL;
40
41 if (pptt_ref + entry->length > table_hdr->length)
42 return NULL;
43
44 return entry;
45}
46
47static struct acpi_pptt_processor *fetch_pptt_node(struct acpi_table_header *table_hdr,
48 u32 pptt_ref)
49{
50 return (struct acpi_pptt_processor *)fetch_pptt_subtable(table_hdr, pptt_ref);
51}
52
53static struct acpi_pptt_cache *fetch_pptt_cache(struct acpi_table_header *table_hdr,
54 u32 pptt_ref)
55{
56 return (struct acpi_pptt_cache *)fetch_pptt_subtable(table_hdr, pptt_ref);
57}
58
59static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
60 struct acpi_pptt_processor *node,
61 int resource)
62{
63 u32 *ref;
64
65 if (resource >= node->number_of_priv_resources)
66 return NULL;
67
68 ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
69 ref += resource;
70
71 return fetch_pptt_subtable(table_hdr, *ref);
72}
73
74static inline bool acpi_pptt_match_type(int table_type, int type)
75{
76 return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
77 table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
78}
79
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
88 *
89 * Attempt to find a given cache level, while counting the max number
90 * of cache levels for the cache node.
91 *
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.
98 *
99 * Return: The cache structure and the level we terminated with.
100 */
101static 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)
106{
107 struct acpi_pptt_cache *cache;
108
109 if (res->type != ACPI_PPTT_TYPE_CACHE)
110 return 0;
111
112 cache = (struct acpi_pptt_cache *) res;
113 while (cache) {
114 local_level++;
115
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");
121
122 pr_debug("Found cache @ level %d\n", level);
123 *found = cache;
124 /*
125 * continue looking at this node's resource list
126 * to verify that we don't find a duplicate
127 * cache node.
128 */
129 }
130 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
131 }
132 return local_level;
133}
134
135static 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)
139{
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;
145
146 /* walk down from processor node */
147 while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
148 resource++;
149
150 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
151 res, &ret, level, type);
152 /*
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.
156 */
157 if (number_of_levels < local_level)
158 number_of_levels = local_level;
159 }
160 if (number_of_levels > *starting_level)
161 *starting_level = number_of_levels;
162
163 return ret;
164}
165
166/**
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
170 *
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.
176 *
177 * Return: Total number of levels found.
178 */
179static int acpi_count_levels(struct acpi_table_header *table_hdr,
180 struct acpi_pptt_processor *cpu_node)
181{
182 int total_levels = 0;
183
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);
188
189 return total_levels;
190}
191
192/**
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
196 *
197 * Determine if the *node parameter is a leaf node by iterating the
198 * PPTT table, looking for nodes which reference it.
199 *
200 * Return: 0 if we find a node referencing the passed node (or table error),
201 * or 1 if we don't.
202 */
203static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
204 struct acpi_pptt_processor *node)
205{
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;
211
212 if (table_hdr->revision > 1)
213 return (node->flags & ACPI_PPTT_ACPI_LEAF_NODE);
214
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 *);
220
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);
230
231 }
232 return 1;
233}
234
235/**
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
239 *
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.
246 *
247 * Return: NULL, or the processors acpi_pptt_processor*
248 */
249static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
250 u32 acpi_cpu_id)
251{
252 struct acpi_subtable_header *entry;
253 unsigned long table_end;
254 struct acpi_pptt_processor *cpu_node;
255 u32 proc_sz;
256
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 *);
261
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;
265
266 if (entry->length == 0) {
267 pr_warn("Invalid zero length subtable\n");
268 break;
269 }
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;
274 }
275
276 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
277 entry->length);
278 }
279
280 return NULL;
281}
282
283static int acpi_find_cache_levels(struct acpi_table_header *table_hdr,
284 u32 acpi_cpu_id)
285{
286 int number_of_levels = 0;
287 struct acpi_pptt_processor *cpu;
288
289 cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id);
290 if (cpu)
291 number_of_levels = acpi_count_levels(table_hdr, cpu);
292
293 return number_of_levels;
294}
295
296static u8 acpi_cache_type(enum cache_type type)
297{
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");
308 /*
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.
313 */
314 return ACPI_PPTT_CACHE_TYPE_UNIFIED;
315 }
316}
317
318static 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)
323{
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);
328
329 pr_debug("Looking for CPU %d's level %d cache type %d\n",
330 acpi_cpu_id, level, acpi_type);
331
332 cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
333
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);
339 }
340
341 return found;
342}
343
344/**
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
349 *
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.
353 *
354 * Return: nothing. Side effect of updating the global cacheinfo
355 */
356static void update_cache_properties(struct cacheinfo *this_leaf,
357 struct acpi_pptt_cache *found_cache,
358 struct acpi_pptt_processor *cpu_node)
359{
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;
377 }
378 }
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;
392 }
393 }
394 /*
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.
398 *
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.
403 */
404 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
405 found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
406 this_leaf->type = CACHE_TYPE_UNIFIED;
407}
408
409static void cache_setup_acpi_cpu(struct acpi_table_header *table,
410 unsigned int cpu)
411{
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;
418
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);
430
431 index++;
432 }
433}
434
435static bool flag_identical(struct acpi_table_header *table_hdr,
436 struct acpi_pptt_processor *cpu)
437{
438 struct acpi_pptt_processor *next;
439
440 /* heterogeneous machines must use PPTT revision > 1 */
441 if (table_hdr->revision < 2)
442 return false;
443
444 /* Locate the last node in the tree with IDENTICAL set */
445 if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) {
446 next = fetch_pptt_node(table_hdr, cpu->parent);
447 if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL))
448 return true;
449 }
450
451 return false;
452}
453
454/* Passing level values greater than this will result in search termination */
455#define PPTT_ABORT_PACKAGE 0xFF
456
457static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr,
458 struct acpi_pptt_processor *cpu,
459 int level, int flag)
460{
461 struct acpi_pptt_processor *prev_node;
462
463 while (cpu && level) {
464 /* special case the identical flag to find last identical */
465 if (flag == ACPI_PPTT_ACPI_IDENTICAL) {
466 if (flag_identical(table_hdr, cpu))
467 break;
468 } else if (cpu->flags & flag)
469 break;
470 pr_debug("level %d\n", level);
471 prev_node = fetch_pptt_node(table_hdr, cpu->parent);
472 if (prev_node == NULL)
473 break;
474 cpu = prev_node;
475 level--;
476 }
477 return cpu;
478}
479
480static void acpi_pptt_warn_missing(void)
481{
482 pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
483}
484
485/**
486 * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
487 * @table: Pointer to the head of the PPTT table
488 * @cpu: Kernel logical CPU number
489 * @level: A level that terminates the search
490 * @flag: A flag which terminates the search
491 *
492 * Get a unique value given a CPU, and a topology level, that can be
493 * matched to determine which cpus share common topological features
494 * at that level.
495 *
496 * Return: Unique value, or -ENOENT if unable to locate CPU
497 */
498static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
499 unsigned int cpu, int level, int flag)
500{
501 struct acpi_pptt_processor *cpu_node;
502 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
503
504 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
505 if (cpu_node) {
506 cpu_node = acpi_find_processor_tag(table, cpu_node,
507 level, flag);
508 /*
509 * As per specification if the processor structure represents
510 * an actual processor, then ACPI processor ID must be valid.
511 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
512 * should be set if the UID is valid
513 */
514 if (level == 0 ||
515 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
516 return cpu_node->acpi_processor_id;
517 return ACPI_PTR_DIFF(cpu_node, table);
518 }
519 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
520 cpu, acpi_cpu_id);
521 return -ENOENT;
522}
523
524static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
525{
526 struct acpi_table_header *table;
527 acpi_status status;
528 int retval;
529
530 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
531 if (ACPI_FAILURE(status)) {
532 acpi_pptt_warn_missing();
533 return -ENOENT;
534 }
535 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
536 pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
537 cpu, level, retval);
538 acpi_put_table(table);
539
540 return retval;
541}
542
543/**
544 * check_acpi_cpu_flag() - Determine if CPU node has a flag set
545 * @cpu: Kernel logical CPU number
546 * @rev: The minimum PPTT revision defining the flag
547 * @flag: The flag itself
548 *
549 * Check the node representing a CPU for a given flag.
550 *
551 * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or
552 * the table revision isn't new enough.
553 * 1, any passed flag set
554 * 0, flag unset
555 */
556static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag)
557{
558 struct acpi_table_header *table;
559 acpi_status status;
560 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
561 struct acpi_pptt_processor *cpu_node = NULL;
562 int ret = -ENOENT;
563
564 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
565 if (ACPI_FAILURE(status)) {
566 acpi_pptt_warn_missing();
567 return ret;
568 }
569
570 if (table->revision >= rev)
571 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
572
573 if (cpu_node)
574 ret = (cpu_node->flags & flag) != 0;
575
576 acpi_put_table(table);
577
578 return ret;
579}
580
581/**
582 * acpi_find_last_cache_level() - Determines the number of cache levels for a PE
583 * @cpu: Kernel logical CPU number
584 *
585 * Given a logical CPU number, returns the number of levels of cache represented
586 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
587 * indicating we didn't find any cache levels.
588 *
589 * Return: Cache levels visible to this core.
590 */
591int acpi_find_last_cache_level(unsigned int cpu)
592{
593 u32 acpi_cpu_id;
594 struct acpi_table_header *table;
595 int number_of_levels = 0;
596 acpi_status status;
597
598 pr_debug("Cache Setup find last level CPU=%d\n", cpu);
599
600 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
601 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
602 if (ACPI_FAILURE(status)) {
603 acpi_pptt_warn_missing();
604 } else {
605 number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id);
606 acpi_put_table(table);
607 }
608 pr_debug("Cache Setup find last level level=%d\n", number_of_levels);
609
610 return number_of_levels;
611}
612
613/**
614 * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
615 * @cpu: Kernel logical CPU number
616 *
617 * Updates the global cache info provided by cpu_get_cacheinfo()
618 * when there are valid properties in the acpi_pptt_cache nodes. A
619 * successful parse may not result in any updates if none of the
620 * cache levels have any valid flags set. Further, a unique value is
621 * associated with each known CPU cache entry. This unique value
622 * can be used to determine whether caches are shared between CPUs.
623 *
624 * Return: -ENOENT on failure to find table, or 0 on success
625 */
626int cache_setup_acpi(unsigned int cpu)
627{
628 struct acpi_table_header *table;
629 acpi_status status;
630
631 pr_debug("Cache Setup ACPI CPU %d\n", cpu);
632
633 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
634 if (ACPI_FAILURE(status)) {
635 acpi_pptt_warn_missing();
636 return -ENOENT;
637 }
638
639 cache_setup_acpi_cpu(table, cpu);
640 acpi_put_table(table);
641
642 return status;
643}
644
645/**
646 * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread
647 * @cpu: Kernel logical CPU number
648 *
649 * Return: 1, a thread
650 * 0, not a thread
651 * -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or
652 * the table revision isn't new enough.
653 */
654int acpi_pptt_cpu_is_thread(unsigned int cpu)
655{
656 return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD);
657}
658
659/**
660 * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
661 * @cpu: Kernel logical CPU number
662 * @level: The topological level for which we would like a unique ID
663 *
664 * Determine a topology unique ID for each thread/core/cluster/mc_grouping
665 * /socket/etc. This ID can then be used to group peers, which will have
666 * matching ids.
667 *
668 * The search terminates when either the requested level is found or
669 * we reach a root node. Levels beyond the termination point will return the
670 * same unique ID. The unique id for level 0 is the acpi processor id. All
671 * other levels beyond this use a generated value to uniquely identify
672 * a topological feature.
673 *
674 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
675 * Otherwise returns a value which represents a unique topological feature.
676 */
677int find_acpi_cpu_topology(unsigned int cpu, int level)
678{
679 return find_acpi_cpu_topology_tag(cpu, level, 0);
680}
681
682/**
683 * find_acpi_cpu_cache_topology() - Determine a unique cache topology value
684 * @cpu: Kernel logical CPU number
685 * @level: The cache level for which we would like a unique ID
686 *
687 * Determine a unique ID for each unified cache in the system
688 *
689 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
690 * Otherwise returns a value which represents a unique topological feature.
691 */
692int find_acpi_cpu_cache_topology(unsigned int cpu, int level)
693{
694 struct acpi_table_header *table;
695 struct acpi_pptt_cache *found_cache;
696 acpi_status status;
697 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
698 struct acpi_pptt_processor *cpu_node = NULL;
699 int ret = -1;
700
701 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table);
702 if (ACPI_FAILURE(status)) {
703 acpi_pptt_warn_missing();
704 return -ENOENT;
705 }
706
707 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
708 CACHE_TYPE_UNIFIED,
709 level,
710 &cpu_node);
711 if (found_cache)
712 ret = ACPI_PTR_DIFF(cpu_node, table);
713
714 acpi_put_table(table);
715
716 return ret;
717}
718
719/**
720 * find_acpi_cpu_topology_package() - Determine a unique CPU package value
721 * @cpu: Kernel logical CPU number
722 *
723 * Determine a topology unique package ID for the given CPU.
724 * This ID can then be used to group peers, which will have matching ids.
725 *
726 * The search terminates when either a level is found with the PHYSICAL_PACKAGE
727 * flag set or we reach a root node.
728 *
729 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
730 * Otherwise returns a value which represents the package for this CPU.
731 */
732int find_acpi_cpu_topology_package(unsigned int cpu)
733{
734 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
735 ACPI_PPTT_PHYSICAL_PACKAGE);
736}
737
738/**
739 * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag
740 * @cpu: Kernel logical CPU number
741 *
742 * Determine a unique heterogeneous tag for the given CPU. CPUs with the same
743 * implementation should have matching tags.
744 *
745 * The returned tag can be used to group peers with identical implementation.
746 *
747 * The search terminates when a level is found with the identical implementation
748 * flag set or we reach a root node.
749 *
750 * Due to limitations in the PPTT data structure, there may be rare situations
751 * where two cores in a heterogeneous machine may be identical, but won't have
752 * the same tag.
753 *
754 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
755 * Otherwise returns a value which represents a group of identical cores
756 * similar to this CPU.
757 */
758int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
759{
760 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
761 ACPI_PPTT_ACPI_IDENTICAL);
762}