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 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 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 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 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) 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 74 static 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 */ 101 static 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 135 static struct acpi_pptt_cache * 136 acpi_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 */ 180 static 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 */ 204 static 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 */ 250 static 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 284 static 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 297 static 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 319 static 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 * @revision: The revision of the PPTT table 351 * 352 * The ACPI spec implies that the fields in the cache structures are used to 353 * extend and correct the information probed from the hardware. Lets only 354 * set fields that we determine are VALID. 355 * 356 * Return: nothing. Side effect of updating the global cacheinfo 357 */ 358 static void update_cache_properties(struct cacheinfo *this_leaf, 359 struct acpi_pptt_cache *found_cache, 360 struct acpi_pptt_processor *cpu_node, 361 u8 revision) 362 { 363 struct acpi_pptt_cache_v1* found_cache_v1; 364 365 this_leaf->fw_token = cpu_node; 366 if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID) 367 this_leaf->size = found_cache->size; 368 if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID) 369 this_leaf->coherency_line_size = found_cache->line_size; 370 if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID) 371 this_leaf->number_of_sets = found_cache->number_of_sets; 372 if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID) 373 this_leaf->ways_of_associativity = found_cache->associativity; 374 if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) { 375 switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) { 376 case ACPI_PPTT_CACHE_POLICY_WT: 377 this_leaf->attributes = CACHE_WRITE_THROUGH; 378 break; 379 case ACPI_PPTT_CACHE_POLICY_WB: 380 this_leaf->attributes = CACHE_WRITE_BACK; 381 break; 382 } 383 } 384 if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) { 385 switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) { 386 case ACPI_PPTT_CACHE_READ_ALLOCATE: 387 this_leaf->attributes |= CACHE_READ_ALLOCATE; 388 break; 389 case ACPI_PPTT_CACHE_WRITE_ALLOCATE: 390 this_leaf->attributes |= CACHE_WRITE_ALLOCATE; 391 break; 392 case ACPI_PPTT_CACHE_RW_ALLOCATE: 393 case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT: 394 this_leaf->attributes |= 395 CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE; 396 break; 397 } 398 } 399 /* 400 * If cache type is NOCACHE, then the cache hasn't been specified 401 * via other mechanisms. Update the type if a cache type has been 402 * provided. 403 * 404 * Note, we assume such caches are unified based on conventional system 405 * design and known examples. Significant work is required elsewhere to 406 * fully support data/instruction only type caches which are only 407 * specified in PPTT. 408 */ 409 if (this_leaf->type == CACHE_TYPE_NOCACHE && 410 found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID) 411 this_leaf->type = CACHE_TYPE_UNIFIED; 412 413 if (revision >= 3 && (found_cache->flags & ACPI_PPTT_CACHE_ID_VALID)) { 414 found_cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1, 415 found_cache, sizeof(struct acpi_pptt_cache)); 416 this_leaf->id = found_cache_v1->cache_id; 417 this_leaf->attributes |= CACHE_ID; 418 } 419 } 420 421 static void cache_setup_acpi_cpu(struct acpi_table_header *table, 422 unsigned int cpu) 423 { 424 struct acpi_pptt_cache *found_cache; 425 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); 426 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 427 struct cacheinfo *this_leaf; 428 unsigned int index = 0; 429 struct acpi_pptt_processor *cpu_node = NULL; 430 431 while (index < get_cpu_cacheinfo(cpu)->num_leaves) { 432 this_leaf = this_cpu_ci->info_list + index; 433 found_cache = acpi_find_cache_node(table, acpi_cpu_id, 434 this_leaf->type, 435 this_leaf->level, 436 &cpu_node); 437 pr_debug("found = %p %p\n", found_cache, cpu_node); 438 if (found_cache) 439 update_cache_properties(this_leaf, found_cache, 440 ACPI_TO_POINTER(ACPI_PTR_DIFF(cpu_node, table)), 441 table->revision); 442 443 index++; 444 } 445 } 446 447 static bool flag_identical(struct acpi_table_header *table_hdr, 448 struct acpi_pptt_processor *cpu) 449 { 450 struct acpi_pptt_processor *next; 451 452 /* heterogeneous machines must use PPTT revision > 1 */ 453 if (table_hdr->revision < 2) 454 return false; 455 456 /* Locate the last node in the tree with IDENTICAL set */ 457 if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) { 458 next = fetch_pptt_node(table_hdr, cpu->parent); 459 if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL)) 460 return true; 461 } 462 463 return false; 464 } 465 466 /* Passing level values greater than this will result in search termination */ 467 #define PPTT_ABORT_PACKAGE 0xFF 468 469 static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr, 470 struct acpi_pptt_processor *cpu, 471 int level, int flag) 472 { 473 struct acpi_pptt_processor *prev_node; 474 475 while (cpu && level) { 476 /* special case the identical flag to find last identical */ 477 if (flag == ACPI_PPTT_ACPI_IDENTICAL) { 478 if (flag_identical(table_hdr, cpu)) 479 break; 480 } else if (cpu->flags & flag) 481 break; 482 pr_debug("level %d\n", level); 483 prev_node = fetch_pptt_node(table_hdr, cpu->parent); 484 if (prev_node == NULL) 485 break; 486 cpu = prev_node; 487 level--; 488 } 489 return cpu; 490 } 491 492 static void acpi_pptt_warn_missing(void) 493 { 494 pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n"); 495 } 496 497 /** 498 * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature 499 * @table: Pointer to the head of the PPTT table 500 * @cpu: Kernel logical CPU number 501 * @level: A level that terminates the search 502 * @flag: A flag which terminates the search 503 * 504 * Get a unique value given a CPU, and a topology level, that can be 505 * matched to determine which cpus share common topological features 506 * at that level. 507 * 508 * Return: Unique value, or -ENOENT if unable to locate CPU 509 */ 510 static int topology_get_acpi_cpu_tag(struct acpi_table_header *table, 511 unsigned int cpu, int level, int flag) 512 { 513 struct acpi_pptt_processor *cpu_node; 514 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 515 516 cpu_node = acpi_find_processor_node(table, acpi_cpu_id); 517 if (cpu_node) { 518 cpu_node = acpi_find_processor_tag(table, cpu_node, 519 level, flag); 520 /* 521 * As per specification if the processor structure represents 522 * an actual processor, then ACPI processor ID must be valid. 523 * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID 524 * should be set if the UID is valid 525 */ 526 if (level == 0 || 527 cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) 528 return cpu_node->acpi_processor_id; 529 return ACPI_PTR_DIFF(cpu_node, table); 530 } 531 pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n", 532 cpu, acpi_cpu_id); 533 return -ENOENT; 534 } 535 536 static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag) 537 { 538 struct acpi_table_header *table; 539 acpi_status status; 540 int retval; 541 542 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); 543 if (ACPI_FAILURE(status)) { 544 acpi_pptt_warn_missing(); 545 return -ENOENT; 546 } 547 retval = topology_get_acpi_cpu_tag(table, cpu, level, flag); 548 pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n", 549 cpu, level, retval); 550 acpi_put_table(table); 551 552 return retval; 553 } 554 555 /** 556 * check_acpi_cpu_flag() - Determine if CPU node has a flag set 557 * @cpu: Kernel logical CPU number 558 * @rev: The minimum PPTT revision defining the flag 559 * @flag: The flag itself 560 * 561 * Check the node representing a CPU for a given flag. 562 * 563 * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or 564 * the table revision isn't new enough. 565 * 1, any passed flag set 566 * 0, flag unset 567 */ 568 static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag) 569 { 570 struct acpi_table_header *table; 571 acpi_status status; 572 u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 573 struct acpi_pptt_processor *cpu_node = NULL; 574 int ret = -ENOENT; 575 576 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); 577 if (ACPI_FAILURE(status)) { 578 acpi_pptt_warn_missing(); 579 return ret; 580 } 581 582 if (table->revision >= rev) 583 cpu_node = acpi_find_processor_node(table, acpi_cpu_id); 584 585 if (cpu_node) 586 ret = (cpu_node->flags & flag) != 0; 587 588 acpi_put_table(table); 589 590 return ret; 591 } 592 593 /** 594 * acpi_find_last_cache_level() - Determines the number of cache levels for a PE 595 * @cpu: Kernel logical CPU number 596 * 597 * Given a logical CPU number, returns the number of levels of cache represented 598 * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0 599 * indicating we didn't find any cache levels. 600 * 601 * Return: Cache levels visible to this core. 602 */ 603 int acpi_find_last_cache_level(unsigned int cpu) 604 { 605 u32 acpi_cpu_id; 606 struct acpi_table_header *table; 607 int number_of_levels = 0; 608 acpi_status status; 609 610 pr_debug("Cache Setup find last level CPU=%d\n", cpu); 611 612 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 613 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); 614 if (ACPI_FAILURE(status)) { 615 acpi_pptt_warn_missing(); 616 } else { 617 number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id); 618 acpi_put_table(table); 619 } 620 pr_debug("Cache Setup find last level level=%d\n", number_of_levels); 621 622 return number_of_levels; 623 } 624 625 /** 626 * cache_setup_acpi() - Override CPU cache topology with data from the PPTT 627 * @cpu: Kernel logical CPU number 628 * 629 * Updates the global cache info provided by cpu_get_cacheinfo() 630 * when there are valid properties in the acpi_pptt_cache nodes. A 631 * successful parse may not result in any updates if none of the 632 * cache levels have any valid flags set. Further, a unique value is 633 * associated with each known CPU cache entry. This unique value 634 * can be used to determine whether caches are shared between CPUs. 635 * 636 * Return: -ENOENT on failure to find table, or 0 on success 637 */ 638 int cache_setup_acpi(unsigned int cpu) 639 { 640 struct acpi_table_header *table; 641 acpi_status status; 642 643 pr_debug("Cache Setup ACPI CPU %d\n", cpu); 644 645 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); 646 if (ACPI_FAILURE(status)) { 647 acpi_pptt_warn_missing(); 648 return -ENOENT; 649 } 650 651 cache_setup_acpi_cpu(table, cpu); 652 acpi_put_table(table); 653 654 return status; 655 } 656 657 /** 658 * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread 659 * @cpu: Kernel logical CPU number 660 * 661 * Return: 1, a thread 662 * 0, not a thread 663 * -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or 664 * the table revision isn't new enough. 665 */ 666 int acpi_pptt_cpu_is_thread(unsigned int cpu) 667 { 668 return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD); 669 } 670 671 /** 672 * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU 673 * @cpu: Kernel logical CPU number 674 * @level: The topological level for which we would like a unique ID 675 * 676 * Determine a topology unique ID for each thread/core/cluster/mc_grouping 677 * /socket/etc. This ID can then be used to group peers, which will have 678 * matching ids. 679 * 680 * The search terminates when either the requested level is found or 681 * we reach a root node. Levels beyond the termination point will return the 682 * same unique ID. The unique id for level 0 is the acpi processor id. All 683 * other levels beyond this use a generated value to uniquely identify 684 * a topological feature. 685 * 686 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found. 687 * Otherwise returns a value which represents a unique topological feature. 688 */ 689 int find_acpi_cpu_topology(unsigned int cpu, int level) 690 { 691 return find_acpi_cpu_topology_tag(cpu, level, 0); 692 } 693 694 /** 695 * find_acpi_cpu_topology_package() - Determine a unique CPU package value 696 * @cpu: Kernel logical CPU number 697 * 698 * Determine a topology unique package ID for the given CPU. 699 * This ID can then be used to group peers, which will have matching ids. 700 * 701 * The search terminates when either a level is found with the PHYSICAL_PACKAGE 702 * flag set or we reach a root node. 703 * 704 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found. 705 * Otherwise returns a value which represents the package for this CPU. 706 */ 707 int find_acpi_cpu_topology_package(unsigned int cpu) 708 { 709 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE, 710 ACPI_PPTT_PHYSICAL_PACKAGE); 711 } 712 713 /** 714 * find_acpi_cpu_topology_cluster() - Determine a unique CPU cluster value 715 * @cpu: Kernel logical CPU number 716 * 717 * Determine a topology unique cluster ID for the given CPU/thread. 718 * This ID can then be used to group peers, which will have matching ids. 719 * 720 * The cluster, if present is the level of topology above CPUs. In a 721 * multi-thread CPU, it will be the level above the CPU, not the thread. 722 * It may not exist in single CPU systems. In simple multi-CPU systems, 723 * it may be equal to the package topology level. 724 * 725 * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found 726 * or there is no toplogy level above the CPU.. 727 * Otherwise returns a value which represents the package for this CPU. 728 */ 729 730 int find_acpi_cpu_topology_cluster(unsigned int cpu) 731 { 732 struct acpi_table_header *table; 733 acpi_status status; 734 struct acpi_pptt_processor *cpu_node, *cluster_node; 735 u32 acpi_cpu_id; 736 int retval; 737 int is_thread; 738 739 status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); 740 if (ACPI_FAILURE(status)) { 741 acpi_pptt_warn_missing(); 742 return -ENOENT; 743 } 744 745 acpi_cpu_id = get_acpi_id_for_cpu(cpu); 746 cpu_node = acpi_find_processor_node(table, acpi_cpu_id); 747 if (cpu_node == NULL || !cpu_node->parent) { 748 retval = -ENOENT; 749 goto put_table; 750 } 751 752 is_thread = cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD; 753 cluster_node = fetch_pptt_node(table, cpu_node->parent); 754 if (cluster_node == NULL) { 755 retval = -ENOENT; 756 goto put_table; 757 } 758 if (is_thread) { 759 if (!cluster_node->parent) { 760 retval = -ENOENT; 761 goto put_table; 762 } 763 cluster_node = fetch_pptt_node(table, cluster_node->parent); 764 if (cluster_node == NULL) { 765 retval = -ENOENT; 766 goto put_table; 767 } 768 } 769 if (cluster_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) 770 retval = cluster_node->acpi_processor_id; 771 else 772 retval = ACPI_PTR_DIFF(cluster_node, table); 773 774 put_table: 775 acpi_put_table(table); 776 777 return retval; 778 } 779 780 /** 781 * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag 782 * @cpu: Kernel logical CPU number 783 * 784 * Determine a unique heterogeneous tag for the given CPU. CPUs with the same 785 * implementation should have matching tags. 786 * 787 * The returned tag can be used to group peers with identical implementation. 788 * 789 * The search terminates when a level is found with the identical implementation 790 * flag set or we reach a root node. 791 * 792 * Due to limitations in the PPTT data structure, there may be rare situations 793 * where two cores in a heterogeneous machine may be identical, but won't have 794 * the same tag. 795 * 796 * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found. 797 * Otherwise returns a value which represents a group of identical cores 798 * similar to this CPU. 799 */ 800 int find_acpi_cpu_topology_hetero_id(unsigned int cpu) 801 { 802 return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE, 803 ACPI_PPTT_ACPI_IDENTICAL); 804 } 805