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
fetch_pptt_subtable(struct acpi_table_header * table_hdr,u32 pptt_ref)24 static 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
fetch_pptt_node(struct acpi_table_header * table_hdr,u32 pptt_ref)47 static 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
fetch_pptt_cache(struct acpi_table_header * table_hdr,u32 pptt_ref)53 static 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
upgrade_pptt_cache(struct acpi_pptt_cache * cache)59 static struct acpi_pptt_cache_v1 *upgrade_pptt_cache(struct acpi_pptt_cache *cache)
60 {
61 if (cache->header.length < sizeof(struct acpi_pptt_cache_v1))
62 return NULL;
63
64 /* No use for v1 if the only additional field is invalid */
65 if (!(cache->flags & ACPI_PPTT_CACHE_ID_VALID))
66 return NULL;
67
68 return (struct acpi_pptt_cache_v1 *)cache;
69 }
70
acpi_get_pptt_resource(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * node,int resource)71 static struct acpi_subtable_header *acpi_get_pptt_resource(struct acpi_table_header *table_hdr,
72 struct acpi_pptt_processor *node,
73 int resource)
74 {
75 u32 *ref;
76
77 if (resource >= node->number_of_priv_resources)
78 return NULL;
79
80 ref = ACPI_ADD_PTR(u32, node, sizeof(struct acpi_pptt_processor));
81 ref += resource;
82
83 return fetch_pptt_subtable(table_hdr, *ref);
84 }
85
acpi_pptt_match_type(int table_type,int type)86 static inline bool acpi_pptt_match_type(int table_type, int type)
87 {
88 return ((table_type & ACPI_PPTT_MASK_CACHE_TYPE) == type ||
89 table_type & ACPI_PPTT_CACHE_TYPE_UNIFIED & type);
90 }
91
92 /**
93 * acpi_pptt_walk_cache() - Attempt to find the requested acpi_pptt_cache
94 * @table_hdr: Pointer to the head of the PPTT table
95 * @local_level: passed res reflects this cache level
96 * @split_levels: Number of split cache levels (data/instruction).
97 * @res: cache resource in the PPTT we want to walk
98 * @found: returns a pointer to the requested level if found
99 * @level: the requested cache level
100 * @type: the requested cache type
101 *
102 * Attempt to find a given cache level, while counting the max number
103 * of cache levels for the cache node.
104 *
105 * Given a pptt resource, verify that it is a cache node, then walk
106 * down each level of caches, counting how many levels are found
107 * as well as checking the cache type (icache, dcache, unified). If a
108 * level & type match, then we set found, and continue the search.
109 * Once the entire cache branch has been walked return its max
110 * depth.
111 *
112 * Return: The cache structure and the level we terminated with.
113 */
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)114 static unsigned int acpi_pptt_walk_cache(struct acpi_table_header *table_hdr,
115 unsigned int local_level,
116 unsigned int *split_levels,
117 struct acpi_subtable_header *res,
118 struct acpi_pptt_cache **found,
119 unsigned int level, int type)
120 {
121 struct acpi_pptt_cache *cache;
122
123 if (res->type != ACPI_PPTT_TYPE_CACHE)
124 return 0;
125
126 cache = (struct acpi_pptt_cache *) res;
127 while (cache) {
128 local_level++;
129
130 if (!(cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)) {
131 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
132 continue;
133 }
134
135 if (split_levels &&
136 (acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_DATA) ||
137 acpi_pptt_match_type(cache->attributes, ACPI_PPTT_CACHE_TYPE_INSTR)))
138 *split_levels = local_level;
139
140 if (local_level == level &&
141 acpi_pptt_match_type(cache->attributes, type)) {
142 if (*found != NULL && cache != *found)
143 pr_warn("Found duplicate cache level/type unable to determine uniqueness\n");
144
145 pr_debug("Found cache @ level %u\n", level);
146 *found = cache;
147 /*
148 * continue looking at this node's resource list
149 * to verify that we don't find a duplicate
150 * cache node.
151 */
152 }
153 cache = fetch_pptt_cache(table_hdr, cache->next_level_of_cache);
154 }
155 return local_level;
156 }
157
158 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)159 acpi_find_cache_level(struct acpi_table_header *table_hdr,
160 struct acpi_pptt_processor *cpu_node,
161 unsigned int *starting_level, unsigned int *split_levels,
162 unsigned int level, int type)
163 {
164 struct acpi_subtable_header *res;
165 unsigned int number_of_levels = *starting_level;
166 int resource = 0;
167 struct acpi_pptt_cache *ret = NULL;
168 unsigned int local_level;
169
170 /* walk down from processor node */
171 while ((res = acpi_get_pptt_resource(table_hdr, cpu_node, resource))) {
172 resource++;
173
174 local_level = acpi_pptt_walk_cache(table_hdr, *starting_level,
175 split_levels, res, &ret,
176 level, type);
177 /*
178 * we are looking for the max depth. Since its potentially
179 * possible for a given node to have resources with differing
180 * depths verify that the depth we have found is the largest.
181 */
182 if (number_of_levels < local_level)
183 number_of_levels = local_level;
184 }
185 if (number_of_levels > *starting_level)
186 *starting_level = number_of_levels;
187
188 return ret;
189 }
190
191 /**
192 * acpi_count_levels() - Given a PPTT table, and a CPU node, count the
193 * total number of levels and split cache levels (data/instruction).
194 * @table_hdr: Pointer to the head of the PPTT table
195 * @cpu_node: processor node we wish to count caches for
196 * @split_levels: Number of split cache levels (data/instruction) if
197 * success. Can by NULL.
198 *
199 * Return: number of levels.
200 * Given a processor node containing a processing unit, walk into it and count
201 * how many levels exist solely for it, and then walk up each level until we hit
202 * the root node (ignore the package level because it may be possible to have
203 * caches that exist across packages). Count the number of cache levels and
204 * split cache levels (data/instruction) that exist at each level on the way
205 * up.
206 */
acpi_count_levels(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu_node,unsigned int * split_levels)207 static int acpi_count_levels(struct acpi_table_header *table_hdr,
208 struct acpi_pptt_processor *cpu_node,
209 unsigned int *split_levels)
210 {
211 int current_level = 0;
212
213 do {
214 acpi_find_cache_level(table_hdr, cpu_node, ¤t_level, split_levels, 0, 0);
215 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
216 } while (cpu_node);
217
218 return current_level;
219 }
220
221 /**
222 * acpi_pptt_leaf_node() - Given a processor node, determine if its a leaf
223 * @table_hdr: Pointer to the head of the PPTT table
224 * @node: passed node is checked to see if its a leaf
225 *
226 * Determine if the *node parameter is a leaf node by iterating the
227 * PPTT table, looking for nodes which reference it.
228 *
229 * Return: 0 if we find a node referencing the passed node (or table error),
230 * or 1 if we don't.
231 */
acpi_pptt_leaf_node(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * node)232 static int acpi_pptt_leaf_node(struct acpi_table_header *table_hdr,
233 struct acpi_pptt_processor *node)
234 {
235 struct acpi_subtable_header *entry;
236 unsigned long table_end;
237 u32 node_entry;
238 struct acpi_pptt_processor *cpu_node;
239 u32 proc_sz;
240
241 if (table_hdr->revision > 1)
242 return (node->flags & ACPI_PPTT_ACPI_LEAF_NODE);
243
244 table_end = (unsigned long)table_hdr + table_hdr->length;
245 node_entry = ACPI_PTR_DIFF(node, table_hdr);
246 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
247 sizeof(struct acpi_table_pptt));
248 proc_sz = sizeof(struct acpi_pptt_processor);
249
250 /* ignore subtable types that are smaller than a processor node */
251 while ((unsigned long)entry + proc_sz <= table_end) {
252 cpu_node = (struct acpi_pptt_processor *)entry;
253
254 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
255 cpu_node->parent == node_entry)
256 return 0;
257 if (entry->length == 0)
258 return 0;
259
260 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
261 entry->length);
262 }
263 return 1;
264 }
265
266 /**
267 * acpi_find_processor_node() - Given a PPTT table find the requested processor
268 * @table_hdr: Pointer to the head of the PPTT table
269 * @acpi_cpu_id: CPU we are searching for
270 *
271 * Find the subtable entry describing the provided processor.
272 * This is done by iterating the PPTT table looking for processor nodes
273 * which have an acpi_processor_id that matches the acpi_cpu_id parameter
274 * passed into the function. If we find a node that matches this criteria
275 * we verify that its a leaf node in the topology rather than depending
276 * on the valid flag, which doesn't need to be set for leaf nodes.
277 *
278 * Return: NULL, or the processors acpi_pptt_processor*
279 */
acpi_find_processor_node(struct acpi_table_header * table_hdr,u32 acpi_cpu_id)280 static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr,
281 u32 acpi_cpu_id)
282 {
283 struct acpi_subtable_header *entry;
284 unsigned long table_end;
285 struct acpi_pptt_processor *cpu_node;
286 u32 proc_sz;
287
288 table_end = (unsigned long)table_hdr + table_hdr->length;
289 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
290 sizeof(struct acpi_table_pptt));
291 proc_sz = sizeof(struct acpi_pptt_processor);
292
293 /* find the processor structure associated with this cpuid */
294 while ((unsigned long)entry + proc_sz <= table_end) {
295 cpu_node = (struct acpi_pptt_processor *)entry;
296
297 if (entry->length == 0) {
298 pr_warn("Invalid zero length subtable\n");
299 break;
300 }
301 /* entry->length may not equal proc_sz, revalidate the processor structure length */
302 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR &&
303 acpi_cpu_id == cpu_node->acpi_processor_id &&
304 (unsigned long)entry + entry->length <= table_end &&
305 entry->length == proc_sz + cpu_node->number_of_priv_resources * sizeof(u32) &&
306 acpi_pptt_leaf_node(table_hdr, cpu_node)) {
307 return (struct acpi_pptt_processor *)entry;
308 }
309
310 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
311 entry->length);
312 }
313
314 return NULL;
315 }
316
acpi_cache_type(enum cache_type type)317 static u8 acpi_cache_type(enum cache_type type)
318 {
319 switch (type) {
320 case CACHE_TYPE_DATA:
321 pr_debug("Looking for data cache\n");
322 return ACPI_PPTT_CACHE_TYPE_DATA;
323 case CACHE_TYPE_INST:
324 pr_debug("Looking for instruction cache\n");
325 return ACPI_PPTT_CACHE_TYPE_INSTR;
326 default:
327 case CACHE_TYPE_UNIFIED:
328 pr_debug("Looking for unified cache\n");
329 /*
330 * It is important that ACPI_PPTT_CACHE_TYPE_UNIFIED
331 * contains the bit pattern that will match both
332 * ACPI unified bit patterns because we use it later
333 * to match both cases.
334 */
335 return ACPI_PPTT_CACHE_TYPE_UNIFIED;
336 }
337 }
338
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)339 static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr,
340 u32 acpi_cpu_id,
341 enum cache_type type,
342 unsigned int level,
343 struct acpi_pptt_processor **node)
344 {
345 unsigned int total_levels = 0;
346 struct acpi_pptt_cache *found = NULL;
347 struct acpi_pptt_processor *cpu_node;
348 u8 acpi_type = acpi_cache_type(type);
349
350 pr_debug("Looking for CPU %d's level %u cache type %d\n",
351 acpi_cpu_id, level, acpi_type);
352
353 cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id);
354
355 while (cpu_node && !found) {
356 found = acpi_find_cache_level(table_hdr, cpu_node,
357 &total_levels, NULL, level, acpi_type);
358 *node = cpu_node;
359 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
360 }
361
362 return found;
363 }
364
365 /**
366 * update_cache_properties() - Update cacheinfo for the given processor
367 * @this_leaf: Kernel cache info structure being updated
368 * @found_cache: The PPTT node describing this cache instance
369 * @cpu_node: A unique reference to describe this cache instance
370 *
371 * The ACPI spec implies that the fields in the cache structures are used to
372 * extend and correct the information probed from the hardware. Lets only
373 * set fields that we determine are VALID.
374 *
375 * Return: nothing. Side effect of updating the global cacheinfo
376 */
update_cache_properties(struct cacheinfo * this_leaf,struct acpi_pptt_cache * found_cache,struct acpi_pptt_processor * cpu_node)377 static void update_cache_properties(struct cacheinfo *this_leaf,
378 struct acpi_pptt_cache *found_cache,
379 struct acpi_pptt_processor *cpu_node)
380 {
381 struct acpi_pptt_cache_v1 *found_cache_v1;
382
383 this_leaf->fw_token = cpu_node;
384 if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID)
385 this_leaf->size = found_cache->size;
386 if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID)
387 this_leaf->coherency_line_size = found_cache->line_size;
388 if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID)
389 this_leaf->number_of_sets = found_cache->number_of_sets;
390 if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID)
391 this_leaf->ways_of_associativity = found_cache->associativity;
392 if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
393 switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
394 case ACPI_PPTT_CACHE_POLICY_WT:
395 this_leaf->attributes = CACHE_WRITE_THROUGH;
396 break;
397 case ACPI_PPTT_CACHE_POLICY_WB:
398 this_leaf->attributes = CACHE_WRITE_BACK;
399 break;
400 }
401 }
402 if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
403 switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
404 case ACPI_PPTT_CACHE_READ_ALLOCATE:
405 this_leaf->attributes |= CACHE_READ_ALLOCATE;
406 break;
407 case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
408 this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
409 break;
410 case ACPI_PPTT_CACHE_RW_ALLOCATE:
411 case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
412 this_leaf->attributes |=
413 CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
414 break;
415 }
416 }
417 /*
418 * If cache type is NOCACHE, then the cache hasn't been specified
419 * via other mechanisms. Update the type if a cache type has been
420 * provided.
421 *
422 * Note, we assume such caches are unified based on conventional system
423 * design and known examples. Significant work is required elsewhere to
424 * fully support data/instruction only type caches which are only
425 * specified in PPTT.
426 */
427 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
428 found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
429 this_leaf->type = CACHE_TYPE_UNIFIED;
430
431 found_cache_v1 = upgrade_pptt_cache(found_cache);
432 if (found_cache_v1) {
433 this_leaf->id = found_cache_v1->cache_id;
434 this_leaf->attributes |= CACHE_ID;
435 }
436 }
437
cache_setup_acpi_cpu(struct acpi_table_header * table,unsigned int cpu)438 static void cache_setup_acpi_cpu(struct acpi_table_header *table,
439 unsigned int cpu)
440 {
441 struct acpi_pptt_cache *found_cache;
442 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
443 u32 acpi_cpu_id;
444 struct cacheinfo *this_leaf;
445 unsigned int index = 0;
446 struct acpi_pptt_processor *cpu_node = NULL;
447
448 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
449 return;
450
451 while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
452 this_leaf = this_cpu_ci->info_list + index;
453 found_cache = acpi_find_cache_node(table, acpi_cpu_id,
454 this_leaf->type,
455 this_leaf->level,
456 &cpu_node);
457 pr_debug("found = %p %p\n", found_cache, cpu_node);
458 if (found_cache)
459 update_cache_properties(this_leaf, found_cache,
460 ACPI_TO_POINTER(ACPI_PTR_DIFF(cpu_node, table)));
461
462 index++;
463 }
464 }
465
flag_identical(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu)466 static bool flag_identical(struct acpi_table_header *table_hdr,
467 struct acpi_pptt_processor *cpu)
468 {
469 struct acpi_pptt_processor *next;
470
471 /* heterogeneous machines must use PPTT revision > 1 */
472 if (table_hdr->revision < 2)
473 return false;
474
475 /* Locate the last node in the tree with IDENTICAL set */
476 if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) {
477 next = fetch_pptt_node(table_hdr, cpu->parent);
478 if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL))
479 return true;
480 }
481
482 return false;
483 }
484
485 /* Passing level values greater than this will result in search termination */
486 #define PPTT_ABORT_PACKAGE 0xFF
487
acpi_find_processor_tag(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * cpu,int level,int flag)488 static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr,
489 struct acpi_pptt_processor *cpu,
490 int level, int flag)
491 {
492 struct acpi_pptt_processor *prev_node;
493
494 while (cpu && level) {
495 /* special case the identical flag to find last identical */
496 if (flag == ACPI_PPTT_ACPI_IDENTICAL) {
497 if (flag_identical(table_hdr, cpu))
498 break;
499 } else if (cpu->flags & flag)
500 break;
501 pr_debug("level %d\n", level);
502 prev_node = fetch_pptt_node(table_hdr, cpu->parent);
503 if (prev_node == NULL)
504 break;
505 cpu = prev_node;
506 level--;
507 }
508 return cpu;
509 }
510
acpi_pptt_warn_missing(void)511 static void acpi_pptt_warn_missing(void)
512 {
513 pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
514 }
515
516 /**
517 * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
518 * @table: Pointer to the head of the PPTT table
519 * @cpu: Kernel logical CPU number
520 * @level: A level that terminates the search
521 * @flag: A flag which terminates the search
522 *
523 * Get a unique value given a CPU, and a topology level, that can be
524 * matched to determine which cpus share common topological features
525 * at that level.
526 *
527 * Return: Unique value, or -ENOENT if unable to locate CPU
528 */
topology_get_acpi_cpu_tag(struct acpi_table_header * table,unsigned int cpu,int level,int flag)529 static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
530 unsigned int cpu, int level, int flag)
531 {
532 struct acpi_pptt_processor *cpu_node;
533 u32 acpi_cpu_id;
534
535 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
536 return -ENOENT;
537
538 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
539 if (cpu_node) {
540 cpu_node = acpi_find_processor_tag(table, cpu_node,
541 level, flag);
542 /*
543 * As per specification if the processor structure represents
544 * an actual processor, then ACPI processor ID must be valid.
545 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
546 * should be set if the UID is valid
547 */
548 if (level == 0 ||
549 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
550 return cpu_node->acpi_processor_id;
551 return ACPI_PTR_DIFF(cpu_node, table);
552 }
553 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
554 cpu, acpi_cpu_id);
555 return -ENOENT;
556 }
557
558
acpi_get_pptt(void)559 static struct acpi_table_header *acpi_get_pptt(void)
560 {
561 static struct acpi_table_header *pptt;
562 static bool is_pptt_checked;
563 acpi_status status;
564
565 /*
566 * PPTT will be used at runtime on every CPU hotplug in path, so we
567 * don't need to call acpi_put_table() to release the table mapping.
568 */
569 if (!pptt && !is_pptt_checked) {
570 status = acpi_get_table(ACPI_SIG_PPTT, 0, &pptt);
571 if (ACPI_FAILURE(status))
572 acpi_pptt_warn_missing();
573
574 is_pptt_checked = true;
575 }
576
577 return pptt;
578 }
579
find_acpi_cpu_topology_tag(unsigned int cpu,int level,int flag)580 static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
581 {
582 struct acpi_table_header *table;
583 int retval;
584
585 table = acpi_get_pptt();
586 if (!table)
587 return -ENOENT;
588
589 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
590 pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
591 cpu, level, retval);
592
593 return retval;
594 }
595
596 /**
597 * check_acpi_cpu_flag() - Determine if CPU node has a flag set
598 * @cpu: Kernel logical CPU number
599 * @rev: The minimum PPTT revision defining the flag
600 * @flag: The flag itself
601 *
602 * Check the node representing a CPU for a given flag.
603 *
604 * Return: -ENOENT if can't get CPU's ACPI Processor UID, the PPTT doesn't
605 * exist, the CPU cannot be found or the table revision isn't new
606 * enough.
607 * 1, any passed flag set
608 * 0, flag unset
609 */
check_acpi_cpu_flag(unsigned int cpu,int rev,u32 flag)610 static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag)
611 {
612 struct acpi_table_header *table;
613 u32 acpi_cpu_id;
614 struct acpi_pptt_processor *cpu_node = NULL;
615 int ret = -ENOENT;
616
617 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
618 return -ENOENT;
619
620 table = acpi_get_pptt();
621 if (!table)
622 return -ENOENT;
623
624 if (table->revision >= rev)
625 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
626
627 if (cpu_node)
628 ret = (cpu_node->flags & flag) != 0;
629
630 return ret;
631 }
632
633 /**
634 * acpi_get_cache_info() - Determine the number of cache levels and
635 * split cache levels (data/instruction) and for a PE.
636 * @cpu: Kernel logical CPU number
637 * @levels: Number of levels if success.
638 * @split_levels: Number of levels being split (i.e. data/instruction)
639 * if success. Can by NULL.
640 *
641 * Given a logical CPU number, returns the number of levels of cache represented
642 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
643 * indicating we didn't find any cache levels.
644 *
645 * Return: -ENOENT if no PPTT table, can't get CPU's ACPI Process UID or no PPTT
646 * processor struct found.
647 * 0 on success.
648 */
acpi_get_cache_info(unsigned int cpu,unsigned int * levels,unsigned int * split_levels)649 int acpi_get_cache_info(unsigned int cpu, unsigned int *levels,
650 unsigned int *split_levels)
651 {
652 struct acpi_pptt_processor *cpu_node;
653 struct acpi_table_header *table;
654 u32 acpi_cpu_id;
655
656 *levels = 0;
657 if (split_levels)
658 *split_levels = 0;
659
660 table = acpi_get_pptt();
661 if (!table)
662 return -ENOENT;
663
664 pr_debug("Cache Setup: find cache levels for CPU=%d\n", cpu);
665
666 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id))
667 return -ENOENT;
668
669 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
670 if (!cpu_node)
671 return -ENOENT;
672
673 *levels = acpi_count_levels(table, cpu_node, split_levels);
674
675 pr_debug("Cache Setup: last_level=%d split_levels=%d\n",
676 *levels, split_levels ? *split_levels : -1);
677
678 return 0;
679 }
680
681 /**
682 * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
683 * @cpu: Kernel logical CPU number
684 *
685 * Updates the global cache info provided by cpu_get_cacheinfo()
686 * when there are valid properties in the acpi_pptt_cache nodes. A
687 * successful parse may not result in any updates if none of the
688 * cache levels have any valid flags set. Further, a unique value is
689 * associated with each known CPU cache entry. This unique value
690 * can be used to determine whether caches are shared between CPUs.
691 *
692 * Return: -ENOENT on failure to find table, or 0 on success
693 */
cache_setup_acpi(unsigned int cpu)694 int cache_setup_acpi(unsigned int cpu)
695 {
696 struct acpi_table_header *table;
697
698 table = acpi_get_pptt();
699 if (!table)
700 return -ENOENT;
701
702 pr_debug("Cache Setup ACPI CPU %d\n", cpu);
703
704 cache_setup_acpi_cpu(table, cpu);
705
706 return 0;
707 }
708
709 /**
710 * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread
711 * @cpu: Kernel logical CPU number
712 *
713 * Return: 1, a thread
714 * 0, not a thread
715 * -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or
716 * the table revision isn't new enough.
717 */
acpi_pptt_cpu_is_thread(unsigned int cpu)718 int acpi_pptt_cpu_is_thread(unsigned int cpu)
719 {
720 return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD);
721 }
722
723 /**
724 * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
725 * @cpu: Kernel logical CPU number
726 * @level: The topological level for which we would like a unique ID
727 *
728 * Determine a topology unique ID for each thread/core/cluster/mc_grouping
729 * /socket/etc. This ID can then be used to group peers, which will have
730 * matching ids.
731 *
732 * The search terminates when either the requested level is found or
733 * we reach a root node. Levels beyond the termination point will return the
734 * same unique ID. The unique id for level 0 is the acpi processor id. All
735 * other levels beyond this use a generated value to uniquely identify
736 * a topological feature.
737 *
738 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
739 * Otherwise returns a value which represents a unique topological feature.
740 */
find_acpi_cpu_topology(unsigned int cpu,int level)741 int find_acpi_cpu_topology(unsigned int cpu, int level)
742 {
743 return find_acpi_cpu_topology_tag(cpu, level, 0);
744 }
745
746 /**
747 * find_acpi_cpu_topology_package() - Determine a unique CPU package value
748 * @cpu: Kernel logical CPU number
749 *
750 * Determine a topology unique package ID for the given CPU.
751 * This ID can then be used to group peers, which will have matching ids.
752 *
753 * The search terminates when either a level is found with the PHYSICAL_PACKAGE
754 * flag set or we reach a root node.
755 *
756 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
757 * Otherwise returns a value which represents the package for this CPU.
758 */
find_acpi_cpu_topology_package(unsigned int cpu)759 int find_acpi_cpu_topology_package(unsigned int cpu)
760 {
761 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
762 ACPI_PPTT_PHYSICAL_PACKAGE);
763 }
764
765 /**
766 * find_acpi_cpu_topology_cluster() - Determine a unique CPU cluster value
767 * @cpu: Kernel logical CPU number
768 *
769 * Determine a topology unique cluster ID for the given CPU/thread.
770 * This ID can then be used to group peers, which will have matching ids.
771 *
772 * The cluster, if present is the level of topology above CPUs. In a
773 * multi-thread CPU, it will be the level above the CPU, not the thread.
774 * It may not exist in single CPU systems. In simple multi-CPU systems,
775 * it may be equal to the package topology level.
776 *
777 * Return: -ENOENT if the PPTT doesn't exist, can't get CPU's ACPI
778 * Processor UID, the CPU cannot be found or there is no toplogy level
779 * above the CPU.
780 * Otherwise returns a value which represents the package for this CPU.
781 */
782
find_acpi_cpu_topology_cluster(unsigned int cpu)783 int find_acpi_cpu_topology_cluster(unsigned int cpu)
784 {
785 struct acpi_table_header *table;
786 struct acpi_pptt_processor *cpu_node, *cluster_node;
787 u32 acpi_cpu_id;
788 int retval;
789 int is_thread;
790
791 table = acpi_get_pptt();
792 if (!table)
793 return -ENOENT;
794
795 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
796 return -ENOENT;
797
798 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
799 if (!cpu_node || !cpu_node->parent)
800 return -ENOENT;
801
802 is_thread = cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD;
803 cluster_node = fetch_pptt_node(table, cpu_node->parent);
804 if (!cluster_node)
805 return -ENOENT;
806
807 if (is_thread) {
808 if (!cluster_node->parent)
809 return -ENOENT;
810
811 cluster_node = fetch_pptt_node(table, cluster_node->parent);
812 if (!cluster_node)
813 return -ENOENT;
814 }
815 if (cluster_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
816 retval = cluster_node->acpi_processor_id;
817 else
818 retval = ACPI_PTR_DIFF(cluster_node, table);
819
820 return retval;
821 }
822
823 /**
824 * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag
825 * @cpu: Kernel logical CPU number
826 *
827 * Determine a unique heterogeneous tag for the given CPU. CPUs with the same
828 * implementation should have matching tags.
829 *
830 * The returned tag can be used to group peers with identical implementation.
831 *
832 * The search terminates when a level is found with the identical implementation
833 * flag set or we reach a root node.
834 *
835 * Due to limitations in the PPTT data structure, there may be rare situations
836 * where two cores in a heterogeneous machine may be identical, but won't have
837 * the same tag.
838 *
839 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
840 * Otherwise returns a value which represents a group of identical cores
841 * similar to this CPU.
842 */
find_acpi_cpu_topology_hetero_id(unsigned int cpu)843 int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
844 {
845 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
846 ACPI_PPTT_ACPI_IDENTICAL);
847 }
848
849 /**
850 * acpi_pptt_get_child_cpus() - Find all the CPUs below a PPTT
851 * processor hierarchy node
852 *
853 * @table_hdr: A reference to the PPTT table
854 * @parent_node: A pointer to the processor hierarchy node in the
855 * table_hdr
856 * @cpus: A cpumask to fill with the CPUs below @parent_node
857 *
858 * Walks up the PPTT from every possible CPU to find if the provided
859 * @parent_node is a parent of this CPU.
860 */
acpi_pptt_get_child_cpus(struct acpi_table_header * table_hdr,struct acpi_pptt_processor * parent_node,cpumask_t * cpus)861 static void acpi_pptt_get_child_cpus(struct acpi_table_header *table_hdr,
862 struct acpi_pptt_processor *parent_node,
863 cpumask_t *cpus)
864 {
865 struct acpi_pptt_processor *cpu_node;
866 u32 acpi_id;
867 int cpu;
868
869 cpumask_clear(cpus);
870
871 for_each_possible_cpu(cpu) {
872 if (acpi_get_cpu_uid(cpu, &acpi_id) != 0)
873 continue;
874
875 cpu_node = acpi_find_processor_node(table_hdr, acpi_id);
876
877 while (cpu_node) {
878 if (cpu_node == parent_node) {
879 cpumask_set_cpu(cpu, cpus);
880 break;
881 }
882 cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent);
883 }
884 }
885 }
886
887 /**
888 * acpi_pptt_get_cpus_from_container() - Populate a cpumask with all CPUs in a
889 * processor container
890 * @acpi_cpu_id: The UID of the processor container
891 * @cpus: The resulting CPU mask
892 *
893 * Find the specified Processor Container, and fill @cpus with all the cpus
894 * below it.
895 *
896 * Not all 'Processor Hierarchy' entries in the PPTT are either a CPU
897 * or a Processor Container, they may exist purely to describe a
898 * Private resource. CPUs have to be leaves, so a Processor Container
899 * is a non-leaf that has the 'ACPI Processor ID valid' flag set.
900 */
acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id,cpumask_t * cpus)901 void acpi_pptt_get_cpus_from_container(u32 acpi_cpu_id, cpumask_t *cpus)
902 {
903 struct acpi_table_header *table_hdr;
904 struct acpi_subtable_header *entry;
905 unsigned long table_end;
906 u32 proc_sz;
907
908 cpumask_clear(cpus);
909
910 table_hdr = acpi_get_pptt();
911 if (!table_hdr)
912 return;
913
914 table_end = (unsigned long)table_hdr + table_hdr->length;
915 entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr,
916 sizeof(struct acpi_table_pptt));
917 proc_sz = sizeof(struct acpi_pptt_processor);
918 while ((unsigned long)entry + proc_sz <= table_end) {
919 if (entry->type == ACPI_PPTT_TYPE_PROCESSOR) {
920 struct acpi_pptt_processor *cpu_node;
921
922 cpu_node = (struct acpi_pptt_processor *)entry;
923 if (cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID &&
924 !acpi_pptt_leaf_node(table_hdr, cpu_node) &&
925 cpu_node->acpi_processor_id == acpi_cpu_id) {
926 acpi_pptt_get_child_cpus(table_hdr, cpu_node, cpus);
927 break;
928 }
929 }
930 entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry,
931 entry->length);
932 }
933 }
934
935 /**
936 * find_acpi_cache_level_from_id() - Get the level of the specified cache
937 * @cache_id: The id field of the cache
938 *
939 * Determine the level relative to any CPU for the cache identified by
940 * cache_id. This allows the property to be found even if the CPUs are offline.
941 *
942 * The returned level can be used to group caches that are peers.
943 *
944 * The PPTT table must be rev 3 or later.
945 *
946 * If one CPU's L2 is shared with another CPU as L3, this function will return
947 * an unpredictable value.
948 *
949 * Return: -ENOENT if the PPTT doesn't exist, the revision isn't supported or
950 * the cache cannot be found.
951 * Otherwise returns a value which represents the level of the specified cache.
952 */
find_acpi_cache_level_from_id(u32 cache_id)953 int find_acpi_cache_level_from_id(u32 cache_id)
954 {
955 int cpu;
956 struct acpi_table_header *table;
957
958 table = acpi_get_pptt();
959 if (!table)
960 return -ENOENT;
961
962 if (table->revision < 3)
963 return -ENOENT;
964
965 for_each_possible_cpu(cpu) {
966 bool empty;
967 int level = 1;
968 u32 acpi_cpu_id;
969 struct acpi_pptt_cache *cache;
970 struct acpi_pptt_processor *cpu_node;
971
972 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
973 continue;
974
975 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
976 if (!cpu_node)
977 continue;
978
979 do {
980 int cache_type[] = {CACHE_TYPE_INST, CACHE_TYPE_DATA, CACHE_TYPE_UNIFIED};
981
982 empty = true;
983 for (int i = 0; i < ARRAY_SIZE(cache_type); i++) {
984 struct acpi_pptt_cache_v1 *cache_v1;
985
986 cache = acpi_find_cache_node(table, acpi_cpu_id, cache_type[i],
987 level, &cpu_node);
988 if (!cache)
989 continue;
990
991 empty = false;
992
993 cache_v1 = upgrade_pptt_cache(cache);
994 if (cache_v1 && cache_v1->cache_id == cache_id)
995 return level;
996 }
997 level++;
998 } while (!empty);
999 }
1000
1001 return -ENOENT;
1002 }
1003
1004 /**
1005 * acpi_pptt_get_cpumask_from_cache_id() - Get the cpus associated with the
1006 * specified cache
1007 * @cache_id: The id field of the cache
1008 * @cpus: Where to build the cpumask
1009 *
1010 * Determine which CPUs are below this cache in the PPTT. This allows the property
1011 * to be found even if the CPUs are offline.
1012 *
1013 * The PPTT table must be rev 3 or later,
1014 *
1015 * Return: -ENOENT if the PPTT doesn't exist, or the cache cannot be found.
1016 * Otherwise returns 0 and sets the cpus in the provided cpumask.
1017 */
acpi_pptt_get_cpumask_from_cache_id(u32 cache_id,cpumask_t * cpus)1018 int acpi_pptt_get_cpumask_from_cache_id(u32 cache_id, cpumask_t *cpus)
1019 {
1020 int cpu;
1021 struct acpi_table_header *table;
1022
1023 cpumask_clear(cpus);
1024
1025 table = acpi_get_pptt();
1026 if (!table)
1027 return -ENOENT;
1028
1029 if (table->revision < 3)
1030 return -ENOENT;
1031
1032 for_each_possible_cpu(cpu) {
1033 bool empty;
1034 int level = 1;
1035 u32 acpi_cpu_id;
1036 struct acpi_pptt_cache *cache;
1037 struct acpi_pptt_processor *cpu_node;
1038
1039 if (acpi_get_cpu_uid(cpu, &acpi_cpu_id) != 0)
1040 continue;
1041
1042 cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
1043 if (!cpu_node)
1044 continue;
1045
1046 do {
1047 int cache_type[] = {CACHE_TYPE_INST, CACHE_TYPE_DATA, CACHE_TYPE_UNIFIED};
1048
1049 empty = true;
1050 for (int i = 0; i < ARRAY_SIZE(cache_type); i++) {
1051 struct acpi_pptt_cache_v1 *cache_v1;
1052
1053 cache = acpi_find_cache_node(table, acpi_cpu_id, cache_type[i],
1054 level, &cpu_node);
1055
1056 if (!cache)
1057 continue;
1058
1059 empty = false;
1060
1061 cache_v1 = upgrade_pptt_cache(cache);
1062 if (cache_v1 && cache_v1->cache_id == cache_id)
1063 cpumask_set_cpu(cpu, cpus);
1064 }
1065 level++;
1066 } while (!empty);
1067 }
1068
1069 return 0;
1070 }
1071