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