File Index
Files and directories
Sign in
FILE

pptt.c

File preview with syntax highlighting.

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