| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 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 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 |  | 
|  | 135 | static struct acpi_pptt_cache *acpi_find_cache_level(struct acpi_table_header *table_hdr, | 
|  | 136 | struct acpi_pptt_processor *cpu_node, | 
|  | 137 | int *starting_level, int level, | 
|  | 138 | int type) | 
|  | 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 | */ | 
|  | 179 | static 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 | */ | 
|  | 203 | static 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 | table_end = (unsigned long)table_hdr + table_hdr->length; | 
|  | 213 | node_entry = ACPI_PTR_DIFF(node, table_hdr); | 
|  | 214 | entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, | 
|  | 215 | sizeof(struct acpi_table_pptt)); | 
|  | 216 | proc_sz = sizeof(struct acpi_pptt_processor *); | 
|  | 217 |  | 
|  | 218 | while ((unsigned long)entry + proc_sz < table_end) { | 
|  | 219 | cpu_node = (struct acpi_pptt_processor *)entry; | 
|  | 220 | if (entry->type == ACPI_PPTT_TYPE_PROCESSOR && | 
|  | 221 | cpu_node->parent == node_entry) | 
|  | 222 | return 0; | 
|  | 223 | if (entry->length == 0) | 
|  | 224 | return 0; | 
|  | 225 | entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry, | 
|  | 226 | entry->length); | 
|  | 227 |  | 
|  | 228 | } | 
|  | 229 | return 1; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | /** | 
|  | 233 | * acpi_find_processor_node() - Given a PPTT table find the requested processor | 
|  | 234 | * @table_hdr:  Pointer to the head of the PPTT table | 
|  | 235 | * @acpi_cpu_id: cpu we are searching for | 
|  | 236 | * | 
|  | 237 | * Find the subtable entry describing the provided processor. | 
|  | 238 | * This is done by iterating the PPTT table looking for processor nodes | 
|  | 239 | * which have an acpi_processor_id that matches the acpi_cpu_id parameter | 
|  | 240 | * passed into the function. If we find a node that matches this criteria | 
|  | 241 | * we verify that its a leaf node in the topology rather than depending | 
|  | 242 | * on the valid flag, which doesn't need to be set for leaf nodes. | 
|  | 243 | * | 
|  | 244 | * Return: NULL, or the processors acpi_pptt_processor* | 
|  | 245 | */ | 
|  | 246 | static struct acpi_pptt_processor *acpi_find_processor_node(struct acpi_table_header *table_hdr, | 
|  | 247 | u32 acpi_cpu_id) | 
|  | 248 | { | 
|  | 249 | struct acpi_subtable_header *entry; | 
|  | 250 | unsigned long table_end; | 
|  | 251 | struct acpi_pptt_processor *cpu_node; | 
|  | 252 | u32 proc_sz; | 
|  | 253 |  | 
|  | 254 | table_end = (unsigned long)table_hdr + table_hdr->length; | 
|  | 255 | entry = ACPI_ADD_PTR(struct acpi_subtable_header, table_hdr, | 
|  | 256 | sizeof(struct acpi_table_pptt)); | 
|  | 257 | proc_sz = sizeof(struct acpi_pptt_processor *); | 
|  | 258 |  | 
|  | 259 | /* find the processor structure associated with this cpuid */ | 
|  | 260 | while ((unsigned long)entry + proc_sz < table_end) { | 
|  | 261 | cpu_node = (struct acpi_pptt_processor *)entry; | 
|  | 262 |  | 
|  | 263 | if (entry->length == 0) { | 
|  | 264 | pr_warn("Invalid zero length subtable\n"); | 
|  | 265 | break; | 
|  | 266 | } | 
|  | 267 | if (entry->type == ACPI_PPTT_TYPE_PROCESSOR && | 
|  | 268 | acpi_cpu_id == cpu_node->acpi_processor_id && | 
|  | 269 | acpi_pptt_leaf_node(table_hdr, cpu_node)) { | 
|  | 270 | return (struct acpi_pptt_processor *)entry; | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | entry = ACPI_ADD_PTR(struct acpi_subtable_header, entry, | 
|  | 274 | entry->length); | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | return NULL; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | static int acpi_find_cache_levels(struct acpi_table_header *table_hdr, | 
|  | 281 | u32 acpi_cpu_id) | 
|  | 282 | { | 
|  | 283 | int number_of_levels = 0; | 
|  | 284 | struct acpi_pptt_processor *cpu; | 
|  | 285 |  | 
|  | 286 | cpu = acpi_find_processor_node(table_hdr, acpi_cpu_id); | 
|  | 287 | if (cpu) | 
|  | 288 | number_of_levels = acpi_count_levels(table_hdr, cpu); | 
|  | 289 |  | 
|  | 290 | return number_of_levels; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | static u8 acpi_cache_type(enum cache_type type) | 
|  | 294 | { | 
|  | 295 | switch (type) { | 
|  | 296 | case CACHE_TYPE_DATA: | 
|  | 297 | pr_debug("Looking for data cache\n"); | 
|  | 298 | return ACPI_PPTT_CACHE_TYPE_DATA; | 
|  | 299 | case CACHE_TYPE_INST: | 
|  | 300 | pr_debug("Looking for instruction cache\n"); | 
|  | 301 | return ACPI_PPTT_CACHE_TYPE_INSTR; | 
|  | 302 | default: | 
|  | 303 | case CACHE_TYPE_UNIFIED: | 
|  | 304 | pr_debug("Looking for unified cache\n"); | 
|  | 305 | /* | 
|  | 306 | * It is important that ACPI_PPTT_CACHE_TYPE_UNIFIED | 
|  | 307 | * contains the bit pattern that will match both | 
|  | 308 | * ACPI unified bit patterns because we use it later | 
|  | 309 | * to match both cases. | 
|  | 310 | */ | 
|  | 311 | return ACPI_PPTT_CACHE_TYPE_UNIFIED; | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | static struct acpi_pptt_cache *acpi_find_cache_node(struct acpi_table_header *table_hdr, | 
|  | 316 | u32 acpi_cpu_id, | 
|  | 317 | enum cache_type type, | 
|  | 318 | unsigned int level, | 
|  | 319 | struct acpi_pptt_processor **node) | 
|  | 320 | { | 
|  | 321 | int total_levels = 0; | 
|  | 322 | struct acpi_pptt_cache *found = NULL; | 
|  | 323 | struct acpi_pptt_processor *cpu_node; | 
|  | 324 | u8 acpi_type = acpi_cache_type(type); | 
|  | 325 |  | 
|  | 326 | pr_debug("Looking for CPU %d's level %d cache type %d\n", | 
|  | 327 | acpi_cpu_id, level, acpi_type); | 
|  | 328 |  | 
|  | 329 | cpu_node = acpi_find_processor_node(table_hdr, acpi_cpu_id); | 
|  | 330 |  | 
|  | 331 | while (cpu_node && !found) { | 
|  | 332 | found = acpi_find_cache_level(table_hdr, cpu_node, | 
|  | 333 | &total_levels, level, acpi_type); | 
|  | 334 | *node = cpu_node; | 
|  | 335 | cpu_node = fetch_pptt_node(table_hdr, cpu_node->parent); | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | return found; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | /** | 
|  | 342 | * update_cache_properties() - Update cacheinfo for the given processor | 
|  | 343 | * @this_leaf: Kernel cache info structure being updated | 
|  | 344 | * @found_cache: The PPTT node describing this cache instance | 
|  | 345 | * @cpu_node: A unique reference to describe this cache instance | 
|  | 346 | * | 
|  | 347 | * The ACPI spec implies that the fields in the cache structures are used to | 
|  | 348 | * extend and correct the information probed from the hardware. Lets only | 
|  | 349 | * set fields that we determine are VALID. | 
|  | 350 | * | 
|  | 351 | * Return: nothing. Side effect of updating the global cacheinfo | 
|  | 352 | */ | 
|  | 353 | static void update_cache_properties(struct cacheinfo *this_leaf, | 
|  | 354 | struct acpi_pptt_cache *found_cache, | 
|  | 355 | struct acpi_pptt_processor *cpu_node) | 
|  | 356 | { | 
|  | 357 | this_leaf->fw_token = cpu_node; | 
|  | 358 | if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID) | 
|  | 359 | this_leaf->size = found_cache->size; | 
|  | 360 | if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID) | 
|  | 361 | this_leaf->coherency_line_size = found_cache->line_size; | 
|  | 362 | if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID) | 
|  | 363 | this_leaf->number_of_sets = found_cache->number_of_sets; | 
|  | 364 | if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID) | 
|  | 365 | this_leaf->ways_of_associativity = found_cache->associativity; | 
|  | 366 | if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) { | 
|  | 367 | switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) { | 
|  | 368 | case ACPI_PPTT_CACHE_POLICY_WT: | 
|  | 369 | this_leaf->attributes = CACHE_WRITE_THROUGH; | 
|  | 370 | break; | 
|  | 371 | case ACPI_PPTT_CACHE_POLICY_WB: | 
|  | 372 | this_leaf->attributes = CACHE_WRITE_BACK; | 
|  | 373 | break; | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 | if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) { | 
|  | 377 | switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) { | 
|  | 378 | case ACPI_PPTT_CACHE_READ_ALLOCATE: | 
|  | 379 | this_leaf->attributes |= CACHE_READ_ALLOCATE; | 
|  | 380 | break; | 
|  | 381 | case ACPI_PPTT_CACHE_WRITE_ALLOCATE: | 
|  | 382 | this_leaf->attributes |= CACHE_WRITE_ALLOCATE; | 
|  | 383 | break; | 
|  | 384 | case ACPI_PPTT_CACHE_RW_ALLOCATE: | 
|  | 385 | case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT: | 
|  | 386 | this_leaf->attributes |= | 
|  | 387 | CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE; | 
|  | 388 | break; | 
|  | 389 | } | 
|  | 390 | } | 
|  | 391 | /* | 
|  | 392 | * If cache type is NOCACHE, then the cache hasn't been specified | 
|  | 393 | * via other mechanisms.  Update the type if a cache type has been | 
|  | 394 | * provided. | 
|  | 395 | * | 
|  | 396 | * Note, we assume such caches are unified based on conventional system | 
|  | 397 | * design and known examples.  Significant work is required elsewhere to | 
|  | 398 | * fully support data/instruction only type caches which are only | 
|  | 399 | * specified in PPTT. | 
|  | 400 | */ | 
|  | 401 | if (this_leaf->type == CACHE_TYPE_NOCACHE && | 
|  | 402 | found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID) | 
|  | 403 | this_leaf->type = CACHE_TYPE_UNIFIED; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | static void cache_setup_acpi_cpu(struct acpi_table_header *table, | 
|  | 407 | unsigned int cpu) | 
|  | 408 | { | 
|  | 409 | struct acpi_pptt_cache *found_cache; | 
|  | 410 | struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); | 
|  | 411 | u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); | 
|  | 412 | struct cacheinfo *this_leaf; | 
|  | 413 | unsigned int index = 0; | 
|  | 414 | struct acpi_pptt_processor *cpu_node = NULL; | 
|  | 415 |  | 
|  | 416 | while (index < get_cpu_cacheinfo(cpu)->num_leaves) { | 
|  | 417 | this_leaf = this_cpu_ci->info_list + index; | 
|  | 418 | found_cache = acpi_find_cache_node(table, acpi_cpu_id, | 
|  | 419 | this_leaf->type, | 
|  | 420 | this_leaf->level, | 
|  | 421 | &cpu_node); | 
|  | 422 | pr_debug("found = %p %p\n", found_cache, cpu_node); | 
|  | 423 | if (found_cache) | 
|  | 424 | update_cache_properties(this_leaf, | 
|  | 425 | found_cache, | 
|  | 426 | cpu_node); | 
|  | 427 |  | 
|  | 428 | index++; | 
|  | 429 | } | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | /* Passing level values greater than this will result in search termination */ | 
|  | 433 | #define PPTT_ABORT_PACKAGE 0xFF | 
|  | 434 |  | 
|  | 435 | static struct acpi_pptt_processor *acpi_find_processor_package_id(struct acpi_table_header *table_hdr, | 
|  | 436 | struct acpi_pptt_processor *cpu, | 
|  | 437 | int level, int flag) | 
|  | 438 | { | 
|  | 439 | struct acpi_pptt_processor *prev_node; | 
|  | 440 |  | 
|  | 441 | while (cpu && level) { | 
|  | 442 | if (cpu->flags & flag) | 
|  | 443 | break; | 
|  | 444 | pr_debug("level %d\n", level); | 
|  | 445 | prev_node = fetch_pptt_node(table_hdr, cpu->parent); | 
|  | 446 | if (prev_node == NULL) | 
|  | 447 | break; | 
|  | 448 | cpu = prev_node; | 
|  | 449 | level--; | 
|  | 450 | } | 
|  | 451 | return cpu; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | /** | 
|  | 455 | * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature | 
|  | 456 | * @table: Pointer to the head of the PPTT table | 
|  | 457 | * @cpu: Kernel logical cpu number | 
|  | 458 | * @level: A level that terminates the search | 
|  | 459 | * @flag: A flag which terminates the search | 
|  | 460 | * | 
|  | 461 | * Get a unique value given a cpu, and a topology level, that can be | 
|  | 462 | * matched to determine which cpus share common topological features | 
|  | 463 | * at that level. | 
|  | 464 | * | 
|  | 465 | * Return: Unique value, or -ENOENT if unable to locate cpu | 
|  | 466 | */ | 
|  | 467 | static int topology_get_acpi_cpu_tag(struct acpi_table_header *table, | 
|  | 468 | unsigned int cpu, int level, int flag) | 
|  | 469 | { | 
|  | 470 | struct acpi_pptt_processor *cpu_node; | 
|  | 471 | u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); | 
|  | 472 |  | 
|  | 473 | cpu_node = acpi_find_processor_node(table, acpi_cpu_id); | 
|  | 474 | if (cpu_node) { | 
|  | 475 | cpu_node = acpi_find_processor_package_id(table, cpu_node, | 
|  | 476 | level, flag); | 
|  | 477 | /* | 
|  | 478 | * As per specification if the processor structure represents | 
|  | 479 | * an actual processor, then ACPI processor ID must be valid. | 
|  | 480 | * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID | 
|  | 481 | * should be set if the UID is valid | 
|  | 482 | */ | 
|  | 483 | if (level == 0 || | 
|  | 484 | cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID) | 
|  | 485 | return cpu_node->acpi_processor_id; | 
|  | 486 | return ACPI_PTR_DIFF(cpu_node, table); | 
|  | 487 | } | 
|  | 488 | pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n", | 
|  | 489 | cpu, acpi_cpu_id); | 
|  | 490 | return -ENOENT; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag) | 
|  | 494 | { | 
|  | 495 | struct acpi_table_header *table; | 
|  | 496 | acpi_status status; | 
|  | 497 | int retval; | 
|  | 498 |  | 
|  | 499 | status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); | 
|  | 500 | if (ACPI_FAILURE(status)) { | 
|  | 501 | pr_warn_once("No PPTT table found, cpu topology may be inaccurate\n"); | 
|  | 502 | return -ENOENT; | 
|  | 503 | } | 
|  | 504 | retval = topology_get_acpi_cpu_tag(table, cpu, level, flag); | 
|  | 505 | pr_debug("Topology Setup ACPI cpu %d, level %d ret = %d\n", | 
|  | 506 | cpu, level, retval); | 
|  | 507 | acpi_put_table(table); | 
|  | 508 |  | 
|  | 509 | return retval; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | /** | 
|  | 513 | * check_acpi_cpu_flag() - Determine if CPU node has a flag set | 
|  | 514 | * @cpu: Kernel logical CPU number | 
|  | 515 | * @rev: The minimum PPTT revision defining the flag | 
|  | 516 | * @flag: The flag itself | 
|  | 517 | * | 
|  | 518 | * Check the node representing a CPU for a given flag. | 
|  | 519 | * | 
|  | 520 | * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or | 
|  | 521 | *	   the table revision isn't new enough. | 
|  | 522 | *	   1, any passed flag set | 
|  | 523 | *	   0, flag unset | 
|  | 524 | */ | 
|  | 525 | static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag) | 
|  | 526 | { | 
|  | 527 | struct acpi_table_header *table; | 
|  | 528 | acpi_status status; | 
|  | 529 | u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); | 
|  | 530 | struct acpi_pptt_processor *cpu_node = NULL; | 
|  | 531 | int ret = -ENOENT; | 
|  | 532 |  | 
|  | 533 | status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); | 
|  | 534 | if (ACPI_FAILURE(status)) { | 
|  | 535 | pr_warn_once("No PPTT table found, cpu topology may be inaccurate\n"); | 
|  | 536 | return ret; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | if (table->revision >= rev) | 
|  | 540 | cpu_node = acpi_find_processor_node(table, acpi_cpu_id); | 
|  | 541 |  | 
|  | 542 | if (cpu_node) | 
|  | 543 | ret = (cpu_node->flags & flag) != 0; | 
|  | 544 |  | 
|  | 545 | acpi_put_table(table); | 
|  | 546 |  | 
|  | 547 | return ret; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | /** | 
|  | 551 | * acpi_find_last_cache_level() - Determines the number of cache levels for a PE | 
|  | 552 | * @cpu: Kernel logical cpu number | 
|  | 553 | * | 
|  | 554 | * Given a logical cpu number, returns the number of levels of cache represented | 
|  | 555 | * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0 | 
|  | 556 | * indicating we didn't find any cache levels. | 
|  | 557 | * | 
|  | 558 | * Return: Cache levels visible to this core. | 
|  | 559 | */ | 
|  | 560 | int acpi_find_last_cache_level(unsigned int cpu) | 
|  | 561 | { | 
|  | 562 | u32 acpi_cpu_id; | 
|  | 563 | struct acpi_table_header *table; | 
|  | 564 | int number_of_levels = 0; | 
|  | 565 | acpi_status status; | 
|  | 566 |  | 
|  | 567 | pr_debug("Cache Setup find last level cpu=%d\n", cpu); | 
|  | 568 |  | 
|  | 569 | acpi_cpu_id = get_acpi_id_for_cpu(cpu); | 
|  | 570 | status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); | 
|  | 571 | if (ACPI_FAILURE(status)) { | 
|  | 572 | pr_warn_once("No PPTT table found, cache topology may be inaccurate\n"); | 
|  | 573 | } else { | 
|  | 574 | number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id); | 
|  | 575 | acpi_put_table(table); | 
|  | 576 | } | 
|  | 577 | pr_debug("Cache Setup find last level level=%d\n", number_of_levels); | 
|  | 578 |  | 
|  | 579 | return number_of_levels; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | /** | 
|  | 583 | * cache_setup_acpi() - Override CPU cache topology with data from the PPTT | 
|  | 584 | * @cpu: Kernel logical cpu number | 
|  | 585 | * | 
|  | 586 | * Updates the global cache info provided by cpu_get_cacheinfo() | 
|  | 587 | * when there are valid properties in the acpi_pptt_cache nodes. A | 
|  | 588 | * successful parse may not result in any updates if none of the | 
|  | 589 | * cache levels have any valid flags set.  Futher, a unique value is | 
|  | 590 | * associated with each known CPU cache entry. This unique value | 
|  | 591 | * can be used to determine whether caches are shared between cpus. | 
|  | 592 | * | 
|  | 593 | * Return: -ENOENT on failure to find table, or 0 on success | 
|  | 594 | */ | 
|  | 595 | int cache_setup_acpi(unsigned int cpu) | 
|  | 596 | { | 
|  | 597 | struct acpi_table_header *table; | 
|  | 598 | acpi_status status; | 
|  | 599 |  | 
|  | 600 | pr_debug("Cache Setup ACPI cpu %d\n", cpu); | 
|  | 601 |  | 
|  | 602 | status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); | 
|  | 603 | if (ACPI_FAILURE(status)) { | 
|  | 604 | pr_warn_once("No PPTT table found, cache topology may be inaccurate\n"); | 
|  | 605 | return -ENOENT; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | cache_setup_acpi_cpu(table, cpu); | 
|  | 609 | acpi_put_table(table); | 
|  | 610 |  | 
|  | 611 | return status; | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | /** | 
|  | 615 | * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread | 
|  | 616 | * @cpu: Kernel logical CPU number | 
|  | 617 | * | 
|  | 618 | * Return: 1, a thread | 
|  | 619 | *         0, not a thread | 
|  | 620 | *         -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or | 
|  | 621 | *         the table revision isn't new enough. | 
|  | 622 | */ | 
|  | 623 | int acpi_pptt_cpu_is_thread(unsigned int cpu) | 
|  | 624 | { | 
|  | 625 | return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD); | 
|  | 626 | } | 
|  | 627 |  | 
|  | 628 | /** | 
|  | 629 | * find_acpi_cpu_topology() - Determine a unique topology value for a given cpu | 
|  | 630 | * @cpu: Kernel logical cpu number | 
|  | 631 | * @level: The topological level for which we would like a unique ID | 
|  | 632 | * | 
|  | 633 | * Determine a topology unique ID for each thread/core/cluster/mc_grouping | 
|  | 634 | * /socket/etc. This ID can then be used to group peers, which will have | 
|  | 635 | * matching ids. | 
|  | 636 | * | 
|  | 637 | * The search terminates when either the requested level is found or | 
|  | 638 | * we reach a root node. Levels beyond the termination point will return the | 
|  | 639 | * same unique ID. The unique id for level 0 is the acpi processor id. All | 
|  | 640 | * other levels beyond this use a generated value to uniquely identify | 
|  | 641 | * a topological feature. | 
|  | 642 | * | 
|  | 643 | * Return: -ENOENT if the PPTT doesn't exist, or the cpu cannot be found. | 
|  | 644 | * Otherwise returns a value which represents a unique topological feature. | 
|  | 645 | */ | 
|  | 646 | int find_acpi_cpu_topology(unsigned int cpu, int level) | 
|  | 647 | { | 
|  | 648 | return find_acpi_cpu_topology_tag(cpu, level, 0); | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | /** | 
|  | 652 | * find_acpi_cpu_cache_topology() - Determine a unique cache topology value | 
|  | 653 | * @cpu: Kernel logical cpu number | 
|  | 654 | * @level: The cache level for which we would like a unique ID | 
|  | 655 | * | 
|  | 656 | * Determine a unique ID for each unified cache in the system | 
|  | 657 | * | 
|  | 658 | * Return: -ENOENT if the PPTT doesn't exist, or the cpu cannot be found. | 
|  | 659 | * Otherwise returns a value which represents a unique topological feature. | 
|  | 660 | */ | 
|  | 661 | int find_acpi_cpu_cache_topology(unsigned int cpu, int level) | 
|  | 662 | { | 
|  | 663 | struct acpi_table_header *table; | 
|  | 664 | struct acpi_pptt_cache *found_cache; | 
|  | 665 | acpi_status status; | 
|  | 666 | u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu); | 
|  | 667 | struct acpi_pptt_processor *cpu_node = NULL; | 
|  | 668 | int ret = -1; | 
|  | 669 |  | 
|  | 670 | status = acpi_get_table(ACPI_SIG_PPTT, 0, &table); | 
|  | 671 | if (ACPI_FAILURE(status)) { | 
|  | 672 | pr_warn_once("No PPTT table found, topology may be inaccurate\n"); | 
|  | 673 | return -ENOENT; | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | found_cache = acpi_find_cache_node(table, acpi_cpu_id, | 
|  | 677 | CACHE_TYPE_UNIFIED, | 
|  | 678 | level, | 
|  | 679 | &cpu_node); | 
|  | 680 | if (found_cache) | 
|  | 681 | ret = ACPI_PTR_DIFF(cpu_node, table); | 
|  | 682 |  | 
|  | 683 | acpi_put_table(table); | 
|  | 684 |  | 
|  | 685 | return ret; | 
|  | 686 | } | 
|  | 687 |  | 
|  | 688 |  | 
|  | 689 | /** | 
|  | 690 | * find_acpi_cpu_topology_package() - Determine a unique cpu package value | 
|  | 691 | * @cpu: Kernel logical cpu number | 
|  | 692 | * | 
|  | 693 | * Determine a topology unique package ID for the given cpu. | 
|  | 694 | * This ID can then be used to group peers, which will have matching ids. | 
|  | 695 | * | 
|  | 696 | * The search terminates when either a level is found with the PHYSICAL_PACKAGE | 
|  | 697 | * flag set or we reach a root node. | 
|  | 698 | * | 
|  | 699 | * Return: -ENOENT if the PPTT doesn't exist, or the cpu cannot be found. | 
|  | 700 | * Otherwise returns a value which represents the package for this cpu. | 
|  | 701 | */ | 
|  | 702 | int find_acpi_cpu_topology_package(unsigned int cpu) | 
|  | 703 | { | 
|  | 704 | return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE, | 
|  | 705 | ACPI_PPTT_PHYSICAL_PACKAGE); | 
|  | 706 | } |