pptt.c 26 KB

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