pptt.c 23 KB

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