pptt.c 22 KB

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