pptt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. * @revision: The revision of the PPTT table
  303. *
  304. * The ACPI spec implies that the fields in the cache structures are used to
  305. * extend and correct the information probed from the hardware. Lets only
  306. * set fields that we determine are VALID.
  307. *
  308. * Return: nothing. Side effect of updating the global cacheinfo
  309. */
  310. static void update_cache_properties(struct cacheinfo *this_leaf,
  311. struct acpi_pptt_cache *found_cache,
  312. struct acpi_pptt_processor *cpu_node,
  313. u8 revision)
  314. {
  315. struct acpi_pptt_cache_v1* found_cache_v1;
  316. this_leaf->fw_token = cpu_node;
  317. if (found_cache->flags & ACPI_PPTT_SIZE_PROPERTY_VALID)
  318. this_leaf->size = found_cache->size;
  319. if (found_cache->flags & ACPI_PPTT_LINE_SIZE_VALID)
  320. this_leaf->coherency_line_size = found_cache->line_size;
  321. if (found_cache->flags & ACPI_PPTT_NUMBER_OF_SETS_VALID)
  322. this_leaf->number_of_sets = found_cache->number_of_sets;
  323. if (found_cache->flags & ACPI_PPTT_ASSOCIATIVITY_VALID)
  324. this_leaf->ways_of_associativity = found_cache->associativity;
  325. if (found_cache->flags & ACPI_PPTT_WRITE_POLICY_VALID) {
  326. switch (found_cache->attributes & ACPI_PPTT_MASK_WRITE_POLICY) {
  327. case ACPI_PPTT_CACHE_POLICY_WT:
  328. this_leaf->attributes = CACHE_WRITE_THROUGH;
  329. break;
  330. case ACPI_PPTT_CACHE_POLICY_WB:
  331. this_leaf->attributes = CACHE_WRITE_BACK;
  332. break;
  333. }
  334. }
  335. if (found_cache->flags & ACPI_PPTT_ALLOCATION_TYPE_VALID) {
  336. switch (found_cache->attributes & ACPI_PPTT_MASK_ALLOCATION_TYPE) {
  337. case ACPI_PPTT_CACHE_READ_ALLOCATE:
  338. this_leaf->attributes |= CACHE_READ_ALLOCATE;
  339. break;
  340. case ACPI_PPTT_CACHE_WRITE_ALLOCATE:
  341. this_leaf->attributes |= CACHE_WRITE_ALLOCATE;
  342. break;
  343. case ACPI_PPTT_CACHE_RW_ALLOCATE:
  344. case ACPI_PPTT_CACHE_RW_ALLOCATE_ALT:
  345. this_leaf->attributes |=
  346. CACHE_READ_ALLOCATE | CACHE_WRITE_ALLOCATE;
  347. break;
  348. }
  349. }
  350. /*
  351. * If cache type is NOCACHE, then the cache hasn't been specified
  352. * via other mechanisms. Update the type if a cache type has been
  353. * provided.
  354. *
  355. * Note, we assume such caches are unified based on conventional system
  356. * design and known examples. Significant work is required elsewhere to
  357. * fully support data/instruction only type caches which are only
  358. * specified in PPTT.
  359. */
  360. if (this_leaf->type == CACHE_TYPE_NOCACHE &&
  361. found_cache->flags & ACPI_PPTT_CACHE_TYPE_VALID)
  362. this_leaf->type = CACHE_TYPE_UNIFIED;
  363. if (revision >= 3 && (found_cache->flags & ACPI_PPTT_CACHE_ID_VALID)) {
  364. found_cache_v1 = ACPI_ADD_PTR(struct acpi_pptt_cache_v1,
  365. found_cache, sizeof(struct acpi_pptt_cache));
  366. this_leaf->id = found_cache_v1->cache_id;
  367. this_leaf->attributes |= CACHE_ID;
  368. }
  369. }
  370. static void cache_setup_acpi_cpu(struct acpi_table_header *table,
  371. unsigned int cpu)
  372. {
  373. struct acpi_pptt_cache *found_cache;
  374. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  375. u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
  376. struct cacheinfo *this_leaf;
  377. unsigned int index = 0;
  378. struct acpi_pptt_processor *cpu_node = NULL;
  379. while (index < get_cpu_cacheinfo(cpu)->num_leaves) {
  380. this_leaf = this_cpu_ci->info_list + index;
  381. found_cache = acpi_find_cache_node(table, acpi_cpu_id,
  382. this_leaf->type,
  383. this_leaf->level,
  384. &cpu_node);
  385. pr_debug("found = %p %p\n", found_cache, cpu_node);
  386. if (found_cache)
  387. update_cache_properties(this_leaf, found_cache,
  388. ACPI_TO_POINTER(ACPI_PTR_DIFF(cpu_node, table)),
  389. table->revision);
  390. index++;
  391. }
  392. }
  393. static bool flag_identical(struct acpi_table_header *table_hdr,
  394. struct acpi_pptt_processor *cpu)
  395. {
  396. struct acpi_pptt_processor *next;
  397. /* heterogeneous machines must use PPTT revision > 1 */
  398. if (table_hdr->revision < 2)
  399. return false;
  400. /* Locate the last node in the tree with IDENTICAL set */
  401. if (cpu->flags & ACPI_PPTT_ACPI_IDENTICAL) {
  402. next = fetch_pptt_node(table_hdr, cpu->parent);
  403. if (!(next && next->flags & ACPI_PPTT_ACPI_IDENTICAL))
  404. return true;
  405. }
  406. return false;
  407. }
  408. /* Passing level values greater than this will result in search termination */
  409. #define PPTT_ABORT_PACKAGE 0xFF
  410. static struct acpi_pptt_processor *acpi_find_processor_tag(struct acpi_table_header *table_hdr,
  411. struct acpi_pptt_processor *cpu,
  412. int level, int flag)
  413. {
  414. struct acpi_pptt_processor *prev_node;
  415. while (cpu && level) {
  416. /* special case the identical flag to find last identical */
  417. if (flag == ACPI_PPTT_ACPI_IDENTICAL) {
  418. if (flag_identical(table_hdr, cpu))
  419. break;
  420. } else if (cpu->flags & flag)
  421. break;
  422. pr_debug("level %d\n", level);
  423. prev_node = fetch_pptt_node(table_hdr, cpu->parent);
  424. if (prev_node == NULL)
  425. break;
  426. cpu = prev_node;
  427. level--;
  428. }
  429. return cpu;
  430. }
  431. static void acpi_pptt_warn_missing(void)
  432. {
  433. pr_warn_once("No PPTT table found, CPU and cache topology may be inaccurate\n");
  434. }
  435. /**
  436. * topology_get_acpi_cpu_tag() - Find a unique topology value for a feature
  437. * @table: Pointer to the head of the PPTT table
  438. * @cpu: Kernel logical CPU number
  439. * @level: A level that terminates the search
  440. * @flag: A flag which terminates the search
  441. *
  442. * Get a unique value given a CPU, and a topology level, that can be
  443. * matched to determine which cpus share common topological features
  444. * at that level.
  445. *
  446. * Return: Unique value, or -ENOENT if unable to locate CPU
  447. */
  448. static int topology_get_acpi_cpu_tag(struct acpi_table_header *table,
  449. unsigned int cpu, int level, int flag)
  450. {
  451. struct acpi_pptt_processor *cpu_node;
  452. u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
  453. cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
  454. if (cpu_node) {
  455. cpu_node = acpi_find_processor_tag(table, cpu_node,
  456. level, flag);
  457. /*
  458. * As per specification if the processor structure represents
  459. * an actual processor, then ACPI processor ID must be valid.
  460. * For processor containers ACPI_PPTT_ACPI_PROCESSOR_ID_VALID
  461. * should be set if the UID is valid
  462. */
  463. if (level == 0 ||
  464. cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
  465. return cpu_node->acpi_processor_id;
  466. return ACPI_PTR_DIFF(cpu_node, table);
  467. }
  468. pr_warn_once("PPTT table found, but unable to locate core %d (%d)\n",
  469. cpu, acpi_cpu_id);
  470. return -ENOENT;
  471. }
  472. static struct acpi_table_header *acpi_get_pptt(void)
  473. {
  474. static struct acpi_table_header *pptt;
  475. static bool is_pptt_checked;
  476. acpi_status status;
  477. /*
  478. * PPTT will be used at runtime on every CPU hotplug in path, so we
  479. * don't need to call acpi_put_table() to release the table mapping.
  480. */
  481. if (!pptt && !is_pptt_checked) {
  482. status = acpi_get_table(ACPI_SIG_PPTT, 0, &pptt);
  483. if (ACPI_FAILURE(status))
  484. acpi_pptt_warn_missing();
  485. is_pptt_checked = true;
  486. }
  487. return pptt;
  488. }
  489. static int find_acpi_cpu_topology_tag(unsigned int cpu, int level, int flag)
  490. {
  491. struct acpi_table_header *table;
  492. int retval;
  493. table = acpi_get_pptt();
  494. if (!table)
  495. return -ENOENT;
  496. retval = topology_get_acpi_cpu_tag(table, cpu, level, flag);
  497. pr_debug("Topology Setup ACPI CPU %d, level %d ret = %d\n",
  498. cpu, level, retval);
  499. return retval;
  500. }
  501. /**
  502. * check_acpi_cpu_flag() - Determine if CPU node has a flag set
  503. * @cpu: Kernel logical CPU number
  504. * @rev: The minimum PPTT revision defining the flag
  505. * @flag: The flag itself
  506. *
  507. * Check the node representing a CPU for a given flag.
  508. *
  509. * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found or
  510. * the table revision isn't new enough.
  511. * 1, any passed flag set
  512. * 0, flag unset
  513. */
  514. static int check_acpi_cpu_flag(unsigned int cpu, int rev, u32 flag)
  515. {
  516. struct acpi_table_header *table;
  517. u32 acpi_cpu_id = get_acpi_id_for_cpu(cpu);
  518. struct acpi_pptt_processor *cpu_node = NULL;
  519. int ret = -ENOENT;
  520. table = acpi_get_pptt();
  521. if (!table)
  522. return -ENOENT;
  523. if (table->revision >= rev)
  524. cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
  525. if (cpu_node)
  526. ret = (cpu_node->flags & flag) != 0;
  527. return ret;
  528. }
  529. /**
  530. * acpi_find_last_cache_level() - Determines the number of cache levels for a PE
  531. * @cpu: Kernel logical CPU number
  532. *
  533. * Given a logical CPU number, returns the number of levels of cache represented
  534. * in the PPTT. Errors caused by lack of a PPTT table, or otherwise, return 0
  535. * indicating we didn't find any cache levels.
  536. *
  537. * Return: Cache levels visible to this core.
  538. */
  539. int acpi_find_last_cache_level(unsigned int cpu)
  540. {
  541. u32 acpi_cpu_id;
  542. struct acpi_table_header *table;
  543. int number_of_levels = 0;
  544. table = acpi_get_pptt();
  545. if (!table)
  546. return -ENOENT;
  547. pr_debug("Cache Setup find last level CPU=%d\n", cpu);
  548. acpi_cpu_id = get_acpi_id_for_cpu(cpu);
  549. number_of_levels = acpi_find_cache_levels(table, acpi_cpu_id);
  550. pr_debug("Cache Setup find last level level=%d\n", number_of_levels);
  551. return number_of_levels;
  552. }
  553. /**
  554. * cache_setup_acpi() - Override CPU cache topology with data from the PPTT
  555. * @cpu: Kernel logical CPU number
  556. *
  557. * Updates the global cache info provided by cpu_get_cacheinfo()
  558. * when there are valid properties in the acpi_pptt_cache nodes. A
  559. * successful parse may not result in any updates if none of the
  560. * cache levels have any valid flags set. Further, a unique value is
  561. * associated with each known CPU cache entry. This unique value
  562. * can be used to determine whether caches are shared between CPUs.
  563. *
  564. * Return: -ENOENT on failure to find table, or 0 on success
  565. */
  566. int cache_setup_acpi(unsigned int cpu)
  567. {
  568. struct acpi_table_header *table;
  569. table = acpi_get_pptt();
  570. if (!table)
  571. return -ENOENT;
  572. pr_debug("Cache Setup ACPI CPU %d\n", cpu);
  573. cache_setup_acpi_cpu(table, cpu);
  574. return 0;
  575. }
  576. /**
  577. * acpi_pptt_cpu_is_thread() - Determine if CPU is a thread
  578. * @cpu: Kernel logical CPU number
  579. *
  580. * Return: 1, a thread
  581. * 0, not a thread
  582. * -ENOENT ,if the PPTT doesn't exist, the CPU cannot be found or
  583. * the table revision isn't new enough.
  584. */
  585. int acpi_pptt_cpu_is_thread(unsigned int cpu)
  586. {
  587. return check_acpi_cpu_flag(cpu, 2, ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD);
  588. }
  589. /**
  590. * find_acpi_cpu_topology() - Determine a unique topology value for a given CPU
  591. * @cpu: Kernel logical CPU number
  592. * @level: The topological level for which we would like a unique ID
  593. *
  594. * Determine a topology unique ID for each thread/core/cluster/mc_grouping
  595. * /socket/etc. This ID can then be used to group peers, which will have
  596. * matching ids.
  597. *
  598. * The search terminates when either the requested level is found or
  599. * we reach a root node. Levels beyond the termination point will return the
  600. * same unique ID. The unique id for level 0 is the acpi processor id. All
  601. * other levels beyond this use a generated value to uniquely identify
  602. * a topological feature.
  603. *
  604. * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
  605. * Otherwise returns a value which represents a unique topological feature.
  606. */
  607. int find_acpi_cpu_topology(unsigned int cpu, int level)
  608. {
  609. return find_acpi_cpu_topology_tag(cpu, level, 0);
  610. }
  611. /**
  612. * find_acpi_cpu_topology_package() - Determine a unique CPU package value
  613. * @cpu: Kernel logical CPU number
  614. *
  615. * Determine a topology unique package ID for the given CPU.
  616. * This ID can then be used to group peers, which will have matching ids.
  617. *
  618. * The search terminates when either a level is found with the PHYSICAL_PACKAGE
  619. * flag set or we reach a root node.
  620. *
  621. * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
  622. * Otherwise returns a value which represents the package for this CPU.
  623. */
  624. int find_acpi_cpu_topology_package(unsigned int cpu)
  625. {
  626. return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
  627. ACPI_PPTT_PHYSICAL_PACKAGE);
  628. }
  629. /**
  630. * find_acpi_cpu_topology_cluster() - Determine a unique CPU cluster value
  631. * @cpu: Kernel logical CPU number
  632. *
  633. * Determine a topology unique cluster ID for the given CPU/thread.
  634. * This ID can then be used to group peers, which will have matching ids.
  635. *
  636. * The cluster, if present is the level of topology above CPUs. In a
  637. * multi-thread CPU, it will be the level above the CPU, not the thread.
  638. * It may not exist in single CPU systems. In simple multi-CPU systems,
  639. * it may be equal to the package topology level.
  640. *
  641. * Return: -ENOENT if the PPTT doesn't exist, the CPU cannot be found
  642. * or there is no toplogy level above the CPU..
  643. * Otherwise returns a value which represents the package for this CPU.
  644. */
  645. int find_acpi_cpu_topology_cluster(unsigned int cpu)
  646. {
  647. struct acpi_table_header *table;
  648. struct acpi_pptt_processor *cpu_node, *cluster_node;
  649. u32 acpi_cpu_id;
  650. int retval;
  651. int is_thread;
  652. table = acpi_get_pptt();
  653. if (!table)
  654. return -ENOENT;
  655. acpi_cpu_id = get_acpi_id_for_cpu(cpu);
  656. cpu_node = acpi_find_processor_node(table, acpi_cpu_id);
  657. if (!cpu_node || !cpu_node->parent)
  658. return -ENOENT;
  659. is_thread = cpu_node->flags & ACPI_PPTT_ACPI_PROCESSOR_IS_THREAD;
  660. cluster_node = fetch_pptt_node(table, cpu_node->parent);
  661. if (!cluster_node)
  662. return -ENOENT;
  663. if (is_thread) {
  664. if (!cluster_node->parent)
  665. return -ENOENT;
  666. cluster_node = fetch_pptt_node(table, cluster_node->parent);
  667. if (!cluster_node)
  668. return -ENOENT;
  669. }
  670. if (cluster_node->flags & ACPI_PPTT_ACPI_PROCESSOR_ID_VALID)
  671. retval = cluster_node->acpi_processor_id;
  672. else
  673. retval = ACPI_PTR_DIFF(cluster_node, table);
  674. return retval;
  675. }
  676. /**
  677. * find_acpi_cpu_topology_hetero_id() - Get a core architecture tag
  678. * @cpu: Kernel logical CPU number
  679. *
  680. * Determine a unique heterogeneous tag for the given CPU. CPUs with the same
  681. * implementation should have matching tags.
  682. *
  683. * The returned tag can be used to group peers with identical implementation.
  684. *
  685. * The search terminates when a level is found with the identical implementation
  686. * flag set or we reach a root node.
  687. *
  688. * Due to limitations in the PPTT data structure, there may be rare situations
  689. * where two cores in a heterogeneous machine may be identical, but won't have
  690. * the same tag.
  691. *
  692. * Return: -ENOENT if the PPTT doesn't exist, or the CPU cannot be found.
  693. * Otherwise returns a value which represents a group of identical cores
  694. * similar to this CPU.
  695. */
  696. int find_acpi_cpu_topology_hetero_id(unsigned int cpu)
  697. {
  698. return find_acpi_cpu_topology_tag(cpu, PPTT_ABORT_PACKAGE,
  699. ACPI_PPTT_ACPI_IDENTICAL);
  700. }