cacheinfo.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cacheinfo support - processor cache information via sysfs
  4. *
  5. * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
  6. * Author: Sudeep Holla <sudeep.holla@arm.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/acpi.h>
  10. #include <linux/bitops.h>
  11. #include <linux/cacheinfo.h>
  12. #include <linux/compiler.h>
  13. #include <linux/cpu.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/of.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/smp.h>
  20. #include <linux/sysfs.h>
  21. /* pointer to per cpu cacheinfo */
  22. static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
  23. #define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu))
  24. #define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves)
  25. #define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list)
  26. #define per_cpu_cacheinfo_idx(cpu, idx) \
  27. (per_cpu_cacheinfo(cpu) + (idx))
  28. /* Set if no cache information is found in DT/ACPI. */
  29. static bool use_arch_info;
  30. struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
  31. {
  32. return ci_cacheinfo(cpu);
  33. }
  34. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  35. struct cacheinfo *sib_leaf)
  36. {
  37. /*
  38. * For non DT/ACPI systems, assume unique level 1 caches,
  39. * system-wide shared caches for all other levels.
  40. */
  41. if (!(IS_ENABLED(CONFIG_OF) || IS_ENABLED(CONFIG_ACPI)) ||
  42. use_arch_info)
  43. return (this_leaf->level != 1) && (sib_leaf->level != 1);
  44. if ((sib_leaf->attributes & CACHE_ID) &&
  45. (this_leaf->attributes & CACHE_ID))
  46. return sib_leaf->id == this_leaf->id;
  47. return sib_leaf->fw_token == this_leaf->fw_token;
  48. }
  49. bool last_level_cache_is_valid(unsigned int cpu)
  50. {
  51. struct cacheinfo *llc;
  52. if (!cache_leaves(cpu) || !per_cpu_cacheinfo(cpu))
  53. return false;
  54. llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
  55. return (llc->attributes & CACHE_ID) || !!llc->fw_token;
  56. }
  57. bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y)
  58. {
  59. struct cacheinfo *llc_x, *llc_y;
  60. if (!last_level_cache_is_valid(cpu_x) ||
  61. !last_level_cache_is_valid(cpu_y))
  62. return false;
  63. llc_x = per_cpu_cacheinfo_idx(cpu_x, cache_leaves(cpu_x) - 1);
  64. llc_y = per_cpu_cacheinfo_idx(cpu_y, cache_leaves(cpu_y) - 1);
  65. return cache_leaves_are_shared(llc_x, llc_y);
  66. }
  67. #ifdef CONFIG_OF
  68. static bool of_check_cache_nodes(struct device_node *np);
  69. /* OF properties to query for a given cache type */
  70. struct cache_type_info {
  71. const char *size_prop;
  72. const char *line_size_props[2];
  73. const char *nr_sets_prop;
  74. };
  75. static const struct cache_type_info cache_type_info[] = {
  76. {
  77. .size_prop = "cache-size",
  78. .line_size_props = { "cache-line-size",
  79. "cache-block-size", },
  80. .nr_sets_prop = "cache-sets",
  81. }, {
  82. .size_prop = "i-cache-size",
  83. .line_size_props = { "i-cache-line-size",
  84. "i-cache-block-size", },
  85. .nr_sets_prop = "i-cache-sets",
  86. }, {
  87. .size_prop = "d-cache-size",
  88. .line_size_props = { "d-cache-line-size",
  89. "d-cache-block-size", },
  90. .nr_sets_prop = "d-cache-sets",
  91. },
  92. };
  93. static inline int get_cacheinfo_idx(enum cache_type type)
  94. {
  95. if (type == CACHE_TYPE_UNIFIED)
  96. return 0;
  97. return type;
  98. }
  99. static void cache_size(struct cacheinfo *this_leaf, struct device_node *np)
  100. {
  101. const char *propname;
  102. int ct_idx;
  103. ct_idx = get_cacheinfo_idx(this_leaf->type);
  104. propname = cache_type_info[ct_idx].size_prop;
  105. of_property_read_u32(np, propname, &this_leaf->size);
  106. }
  107. /* not cache_line_size() because that's a macro in include/linux/cache.h */
  108. static void cache_get_line_size(struct cacheinfo *this_leaf,
  109. struct device_node *np)
  110. {
  111. int i, lim, ct_idx;
  112. ct_idx = get_cacheinfo_idx(this_leaf->type);
  113. lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
  114. for (i = 0; i < lim; i++) {
  115. int ret;
  116. u32 line_size;
  117. const char *propname;
  118. propname = cache_type_info[ct_idx].line_size_props[i];
  119. ret = of_property_read_u32(np, propname, &line_size);
  120. if (!ret) {
  121. this_leaf->coherency_line_size = line_size;
  122. break;
  123. }
  124. }
  125. }
  126. static void cache_nr_sets(struct cacheinfo *this_leaf, struct device_node *np)
  127. {
  128. const char *propname;
  129. int ct_idx;
  130. ct_idx = get_cacheinfo_idx(this_leaf->type);
  131. propname = cache_type_info[ct_idx].nr_sets_prop;
  132. of_property_read_u32(np, propname, &this_leaf->number_of_sets);
  133. }
  134. static void cache_associativity(struct cacheinfo *this_leaf)
  135. {
  136. unsigned int line_size = this_leaf->coherency_line_size;
  137. unsigned int nr_sets = this_leaf->number_of_sets;
  138. unsigned int size = this_leaf->size;
  139. /*
  140. * If the cache is fully associative, there is no need to
  141. * check the other properties.
  142. */
  143. if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
  144. this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
  145. }
  146. static bool cache_node_is_unified(struct cacheinfo *this_leaf,
  147. struct device_node *np)
  148. {
  149. return of_property_read_bool(np, "cache-unified");
  150. }
  151. static void cache_of_set_props(struct cacheinfo *this_leaf,
  152. struct device_node *np)
  153. {
  154. /*
  155. * init_cache_level must setup the cache level correctly
  156. * overriding the architecturally specified levels, so
  157. * if type is NONE at this stage, it should be unified
  158. */
  159. if (this_leaf->type == CACHE_TYPE_NOCACHE &&
  160. cache_node_is_unified(this_leaf, np))
  161. this_leaf->type = CACHE_TYPE_UNIFIED;
  162. cache_size(this_leaf, np);
  163. cache_get_line_size(this_leaf, np);
  164. cache_nr_sets(this_leaf, np);
  165. cache_associativity(this_leaf);
  166. }
  167. static int cache_setup_of_node(unsigned int cpu)
  168. {
  169. struct cacheinfo *this_leaf;
  170. unsigned int index = 0;
  171. struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
  172. if (!np) {
  173. pr_err("Failed to find cpu%d device node\n", cpu);
  174. return -ENOENT;
  175. }
  176. if (!of_check_cache_nodes(np)) {
  177. return -ENOENT;
  178. }
  179. while (index < cache_leaves(cpu)) {
  180. this_leaf = per_cpu_cacheinfo_idx(cpu, index);
  181. if (this_leaf->level != 1) {
  182. struct device_node *prev __free(device_node) = np;
  183. np = of_find_next_cache_node(np);
  184. if (!np)
  185. break;
  186. }
  187. cache_of_set_props(this_leaf, np);
  188. this_leaf->fw_token = np;
  189. index++;
  190. }
  191. if (index != cache_leaves(cpu)) /* not all OF nodes populated */
  192. return -ENOENT;
  193. return 0;
  194. }
  195. static bool of_check_cache_nodes(struct device_node *np)
  196. {
  197. if (of_property_present(np, "cache-size") ||
  198. of_property_present(np, "i-cache-size") ||
  199. of_property_present(np, "d-cache-size") ||
  200. of_property_present(np, "cache-unified"))
  201. return true;
  202. struct device_node *next __free(device_node) = of_find_next_cache_node(np);
  203. if (next) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. static int of_count_cache_leaves(struct device_node *np)
  209. {
  210. unsigned int leaves = 0;
  211. if (of_property_read_bool(np, "cache-size"))
  212. ++leaves;
  213. if (of_property_read_bool(np, "i-cache-size"))
  214. ++leaves;
  215. if (of_property_read_bool(np, "d-cache-size"))
  216. ++leaves;
  217. if (!leaves) {
  218. /* The '[i-|d-|]cache-size' property is required, but
  219. * if absent, fallback on the 'cache-unified' property.
  220. */
  221. if (of_property_read_bool(np, "cache-unified"))
  222. return 1;
  223. else
  224. return 2;
  225. }
  226. return leaves;
  227. }
  228. int init_of_cache_level(unsigned int cpu)
  229. {
  230. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  231. struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
  232. unsigned int levels = 0, leaves, level;
  233. if (!of_check_cache_nodes(np)) {
  234. return -ENOENT;
  235. }
  236. leaves = of_count_cache_leaves(np);
  237. if (leaves > 0)
  238. levels = 1;
  239. while (1) {
  240. struct device_node *prev __free(device_node) = np;
  241. np = of_find_next_cache_node(np);
  242. if (!np)
  243. break;
  244. if (!of_device_is_compatible(np, "cache"))
  245. return -EINVAL;
  246. if (of_property_read_u32(np, "cache-level", &level))
  247. return -EINVAL;
  248. if (level <= levels)
  249. return -EINVAL;
  250. leaves += of_count_cache_leaves(np);
  251. levels = level;
  252. }
  253. this_cpu_ci->num_levels = levels;
  254. this_cpu_ci->num_leaves = leaves;
  255. return 0;
  256. }
  257. #else
  258. static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
  259. int init_of_cache_level(unsigned int cpu) { return 0; }
  260. #endif
  261. int __weak cache_setup_acpi(unsigned int cpu)
  262. {
  263. return -ENOTSUPP;
  264. }
  265. unsigned int coherency_max_size;
  266. static int cache_setup_properties(unsigned int cpu)
  267. {
  268. int ret = 0;
  269. if (of_have_populated_dt())
  270. ret = cache_setup_of_node(cpu);
  271. else if (!acpi_disabled)
  272. ret = cache_setup_acpi(cpu);
  273. // Assume there is no cache information available in DT/ACPI from now.
  274. if (ret && use_arch_cache_info())
  275. use_arch_info = true;
  276. return ret;
  277. }
  278. static int cache_shared_cpu_map_setup(unsigned int cpu)
  279. {
  280. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  281. struct cacheinfo *this_leaf, *sib_leaf;
  282. unsigned int index, sib_index;
  283. int ret = 0;
  284. if (this_cpu_ci->cpu_map_populated)
  285. return 0;
  286. /*
  287. * skip setting up cache properties if LLC is valid, just need
  288. * to update the shared cpu_map if the cache attributes were
  289. * populated early before all the cpus are brought online
  290. */
  291. if (!last_level_cache_is_valid(cpu) && !use_arch_info) {
  292. ret = cache_setup_properties(cpu);
  293. if (ret)
  294. return ret;
  295. }
  296. for (index = 0; index < cache_leaves(cpu); index++) {
  297. unsigned int i;
  298. this_leaf = per_cpu_cacheinfo_idx(cpu, index);
  299. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  300. for_each_online_cpu(i) {
  301. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  302. if (i == cpu || !sib_cpu_ci->info_list)
  303. continue;/* skip if itself or no cacheinfo */
  304. for (sib_index = 0; sib_index < cache_leaves(i); sib_index++) {
  305. sib_leaf = per_cpu_cacheinfo_idx(i, sib_index);
  306. /*
  307. * Comparing cache IDs only makes sense if the leaves
  308. * belong to the same cache level of same type. Skip
  309. * the check if level and type do not match.
  310. */
  311. if (sib_leaf->level != this_leaf->level ||
  312. sib_leaf->type != this_leaf->type)
  313. continue;
  314. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  315. cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
  316. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  317. break;
  318. }
  319. }
  320. }
  321. /* record the maximum cache line size */
  322. if (this_leaf->coherency_line_size > coherency_max_size)
  323. coherency_max_size = this_leaf->coherency_line_size;
  324. }
  325. /* shared_cpu_map is now populated for the cpu */
  326. this_cpu_ci->cpu_map_populated = true;
  327. return 0;
  328. }
  329. static void cache_shared_cpu_map_remove(unsigned int cpu)
  330. {
  331. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  332. struct cacheinfo *this_leaf, *sib_leaf;
  333. unsigned int sibling, index, sib_index;
  334. for (index = 0; index < cache_leaves(cpu); index++) {
  335. this_leaf = per_cpu_cacheinfo_idx(cpu, index);
  336. for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
  337. struct cpu_cacheinfo *sib_cpu_ci =
  338. get_cpu_cacheinfo(sibling);
  339. if (sibling == cpu || !sib_cpu_ci->info_list)
  340. continue;/* skip if itself or no cacheinfo */
  341. for (sib_index = 0; sib_index < cache_leaves(sibling); sib_index++) {
  342. sib_leaf = per_cpu_cacheinfo_idx(sibling, sib_index);
  343. /*
  344. * Comparing cache IDs only makes sense if the leaves
  345. * belong to the same cache level of same type. Skip
  346. * the check if level and type do not match.
  347. */
  348. if (sib_leaf->level != this_leaf->level ||
  349. sib_leaf->type != this_leaf->type)
  350. continue;
  351. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  352. cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
  353. cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. /* cpu is no longer populated in the shared map */
  360. this_cpu_ci->cpu_map_populated = false;
  361. }
  362. static void free_cache_attributes(unsigned int cpu)
  363. {
  364. if (!per_cpu_cacheinfo(cpu))
  365. return;
  366. cache_shared_cpu_map_remove(cpu);
  367. }
  368. int __weak early_cache_level(unsigned int cpu)
  369. {
  370. return -ENOENT;
  371. }
  372. int __weak init_cache_level(unsigned int cpu)
  373. {
  374. return -ENOENT;
  375. }
  376. int __weak populate_cache_leaves(unsigned int cpu)
  377. {
  378. return -ENOENT;
  379. }
  380. static inline int allocate_cache_info(int cpu)
  381. {
  382. per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), sizeof(struct cacheinfo), GFP_ATOMIC);
  383. if (!per_cpu_cacheinfo(cpu)) {
  384. cache_leaves(cpu) = 0;
  385. return -ENOMEM;
  386. }
  387. return 0;
  388. }
  389. int fetch_cache_info(unsigned int cpu)
  390. {
  391. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  392. unsigned int levels = 0, split_levels = 0;
  393. int ret;
  394. if (acpi_disabled) {
  395. ret = init_of_cache_level(cpu);
  396. } else {
  397. ret = acpi_get_cache_info(cpu, &levels, &split_levels);
  398. if (!ret) {
  399. this_cpu_ci->num_levels = levels;
  400. /*
  401. * This assumes that:
  402. * - there cannot be any split caches (data/instruction)
  403. * above a unified cache
  404. * - data/instruction caches come by pair
  405. */
  406. this_cpu_ci->num_leaves = levels + split_levels;
  407. }
  408. }
  409. if (ret || !cache_leaves(cpu)) {
  410. ret = early_cache_level(cpu);
  411. if (ret)
  412. return ret;
  413. if (!cache_leaves(cpu))
  414. return -ENOENT;
  415. this_cpu_ci->early_ci_levels = true;
  416. }
  417. return allocate_cache_info(cpu);
  418. }
  419. static inline int init_level_allocate_ci(unsigned int cpu)
  420. {
  421. unsigned int early_leaves = cache_leaves(cpu);
  422. /* Since early initialization/allocation of the cacheinfo is allowed
  423. * via fetch_cache_info() and this also gets called as CPU hotplug
  424. * callbacks via cacheinfo_cpu_online, the init/alloc can be skipped
  425. * as it will happen only once (the cacheinfo memory is never freed).
  426. * Just populate the cacheinfo. However, if the cacheinfo has been
  427. * allocated early through the arch-specific early_cache_level() call,
  428. * there is a chance the info is wrong (this can happen on arm64). In
  429. * that case, call init_cache_level() anyway to give the arch-specific
  430. * code a chance to make things right.
  431. */
  432. if (per_cpu_cacheinfo(cpu) && !ci_cacheinfo(cpu)->early_ci_levels)
  433. return 0;
  434. if (init_cache_level(cpu) || !cache_leaves(cpu))
  435. return -ENOENT;
  436. /*
  437. * Now that we have properly initialized the cache level info, make
  438. * sure we don't try to do that again the next time we are called
  439. * (e.g. as CPU hotplug callbacks).
  440. */
  441. ci_cacheinfo(cpu)->early_ci_levels = false;
  442. /*
  443. * Some architectures (e.g., x86) do not use early initialization.
  444. * Allocate memory now in such case.
  445. */
  446. if (cache_leaves(cpu) <= early_leaves && per_cpu_cacheinfo(cpu))
  447. return 0;
  448. kfree(per_cpu_cacheinfo(cpu));
  449. return allocate_cache_info(cpu);
  450. }
  451. int detect_cache_attributes(unsigned int cpu)
  452. {
  453. int ret;
  454. ret = init_level_allocate_ci(cpu);
  455. if (ret)
  456. return ret;
  457. /*
  458. * If LLC is valid the cache leaves were already populated so just go to
  459. * update the cpu map.
  460. */
  461. if (!last_level_cache_is_valid(cpu)) {
  462. /*
  463. * populate_cache_leaves() may completely setup the cache leaves and
  464. * shared_cpu_map or it may leave it partially setup.
  465. */
  466. ret = populate_cache_leaves(cpu);
  467. if (ret)
  468. goto free_ci;
  469. }
  470. /*
  471. * For systems using DT for cache hierarchy, fw_token
  472. * and shared_cpu_map will be set up here only if they are
  473. * not populated already
  474. */
  475. ret = cache_shared_cpu_map_setup(cpu);
  476. if (ret) {
  477. pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
  478. goto free_ci;
  479. }
  480. return 0;
  481. free_ci:
  482. free_cache_attributes(cpu);
  483. return ret;
  484. }
  485. /* pointer to cpuX/cache device */
  486. static DEFINE_PER_CPU(struct device *, ci_cache_dev);
  487. #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
  488. static cpumask_t cache_dev_map;
  489. /* pointer to array of devices for cpuX/cache/indexY */
  490. static DEFINE_PER_CPU(struct device **, ci_index_dev);
  491. #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
  492. #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
  493. #define show_one(file_name, object) \
  494. static ssize_t file_name##_show(struct device *dev, \
  495. struct device_attribute *attr, char *buf) \
  496. { \
  497. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  498. return sysfs_emit(buf, "%u\n", this_leaf->object); \
  499. }
  500. show_one(id, id);
  501. show_one(level, level);
  502. show_one(coherency_line_size, coherency_line_size);
  503. show_one(number_of_sets, number_of_sets);
  504. show_one(physical_line_partition, physical_line_partition);
  505. show_one(ways_of_associativity, ways_of_associativity);
  506. static ssize_t size_show(struct device *dev,
  507. struct device_attribute *attr, char *buf)
  508. {
  509. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  510. return sysfs_emit(buf, "%uK\n", this_leaf->size >> 10);
  511. }
  512. static ssize_t shared_cpu_map_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  516. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  517. return sysfs_emit(buf, "%*pb\n", nr_cpu_ids, mask);
  518. }
  519. static ssize_t shared_cpu_list_show(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  523. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  524. return sysfs_emit(buf, "%*pbl\n", nr_cpu_ids, mask);
  525. }
  526. static ssize_t type_show(struct device *dev,
  527. struct device_attribute *attr, char *buf)
  528. {
  529. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  530. const char *output;
  531. switch (this_leaf->type) {
  532. case CACHE_TYPE_DATA:
  533. output = "Data";
  534. break;
  535. case CACHE_TYPE_INST:
  536. output = "Instruction";
  537. break;
  538. case CACHE_TYPE_UNIFIED:
  539. output = "Unified";
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. return sysfs_emit(buf, "%s\n", output);
  545. }
  546. static ssize_t allocation_policy_show(struct device *dev,
  547. struct device_attribute *attr, char *buf)
  548. {
  549. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  550. unsigned int ci_attr = this_leaf->attributes;
  551. const char *output;
  552. if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
  553. output = "ReadWriteAllocate";
  554. else if (ci_attr & CACHE_READ_ALLOCATE)
  555. output = "ReadAllocate";
  556. else if (ci_attr & CACHE_WRITE_ALLOCATE)
  557. output = "WriteAllocate";
  558. else
  559. return 0;
  560. return sysfs_emit(buf, "%s\n", output);
  561. }
  562. static ssize_t write_policy_show(struct device *dev,
  563. struct device_attribute *attr, char *buf)
  564. {
  565. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  566. unsigned int ci_attr = this_leaf->attributes;
  567. int n = 0;
  568. if (ci_attr & CACHE_WRITE_THROUGH)
  569. n = sysfs_emit(buf, "WriteThrough\n");
  570. else if (ci_attr & CACHE_WRITE_BACK)
  571. n = sysfs_emit(buf, "WriteBack\n");
  572. return n;
  573. }
  574. static DEVICE_ATTR_RO(id);
  575. static DEVICE_ATTR_RO(level);
  576. static DEVICE_ATTR_RO(type);
  577. static DEVICE_ATTR_RO(coherency_line_size);
  578. static DEVICE_ATTR_RO(ways_of_associativity);
  579. static DEVICE_ATTR_RO(number_of_sets);
  580. static DEVICE_ATTR_RO(size);
  581. static DEVICE_ATTR_RO(allocation_policy);
  582. static DEVICE_ATTR_RO(write_policy);
  583. static DEVICE_ATTR_RO(shared_cpu_map);
  584. static DEVICE_ATTR_RO(shared_cpu_list);
  585. static DEVICE_ATTR_RO(physical_line_partition);
  586. static struct attribute *cache_default_attrs[] = {
  587. &dev_attr_id.attr,
  588. &dev_attr_type.attr,
  589. &dev_attr_level.attr,
  590. &dev_attr_shared_cpu_map.attr,
  591. &dev_attr_shared_cpu_list.attr,
  592. &dev_attr_coherency_line_size.attr,
  593. &dev_attr_ways_of_associativity.attr,
  594. &dev_attr_number_of_sets.attr,
  595. &dev_attr_size.attr,
  596. &dev_attr_allocation_policy.attr,
  597. &dev_attr_write_policy.attr,
  598. &dev_attr_physical_line_partition.attr,
  599. NULL
  600. };
  601. static umode_t
  602. cache_default_attrs_is_visible(struct kobject *kobj,
  603. struct attribute *attr, int unused)
  604. {
  605. struct device *dev = kobj_to_dev(kobj);
  606. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  607. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  608. umode_t mode = attr->mode;
  609. if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
  610. return mode;
  611. if ((attr == &dev_attr_type.attr) && this_leaf->type)
  612. return mode;
  613. if ((attr == &dev_attr_level.attr) && this_leaf->level)
  614. return mode;
  615. if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
  616. return mode;
  617. if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
  618. return mode;
  619. if ((attr == &dev_attr_coherency_line_size.attr) &&
  620. this_leaf->coherency_line_size)
  621. return mode;
  622. if ((attr == &dev_attr_ways_of_associativity.attr) &&
  623. this_leaf->size) /* allow 0 = full associativity */
  624. return mode;
  625. if ((attr == &dev_attr_number_of_sets.attr) &&
  626. this_leaf->number_of_sets)
  627. return mode;
  628. if ((attr == &dev_attr_size.attr) && this_leaf->size)
  629. return mode;
  630. if ((attr == &dev_attr_write_policy.attr) &&
  631. (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
  632. return mode;
  633. if ((attr == &dev_attr_allocation_policy.attr) &&
  634. (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
  635. return mode;
  636. if ((attr == &dev_attr_physical_line_partition.attr) &&
  637. this_leaf->physical_line_partition)
  638. return mode;
  639. return 0;
  640. }
  641. static const struct attribute_group cache_default_group = {
  642. .attrs = cache_default_attrs,
  643. .is_visible = cache_default_attrs_is_visible,
  644. };
  645. static const struct attribute_group *cache_default_groups[] = {
  646. &cache_default_group,
  647. NULL,
  648. };
  649. static const struct attribute_group *cache_private_groups[] = {
  650. &cache_default_group,
  651. NULL, /* Place holder for private group */
  652. NULL,
  653. };
  654. const struct attribute_group *
  655. __weak cache_get_priv_group(struct cacheinfo *this_leaf)
  656. {
  657. return NULL;
  658. }
  659. static const struct attribute_group **
  660. cache_get_attribute_groups(struct cacheinfo *this_leaf)
  661. {
  662. const struct attribute_group *priv_group =
  663. cache_get_priv_group(this_leaf);
  664. if (!priv_group)
  665. return cache_default_groups;
  666. if (!cache_private_groups[1])
  667. cache_private_groups[1] = priv_group;
  668. return cache_private_groups;
  669. }
  670. /* Add/Remove cache interface for CPU device */
  671. static void cpu_cache_sysfs_exit(unsigned int cpu)
  672. {
  673. int i;
  674. struct device *ci_dev;
  675. if (per_cpu_index_dev(cpu)) {
  676. for (i = 0; i < cache_leaves(cpu); i++) {
  677. ci_dev = per_cache_index_dev(cpu, i);
  678. if (!ci_dev)
  679. continue;
  680. device_unregister(ci_dev);
  681. }
  682. kfree(per_cpu_index_dev(cpu));
  683. per_cpu_index_dev(cpu) = NULL;
  684. }
  685. device_unregister(per_cpu_cache_dev(cpu));
  686. per_cpu_cache_dev(cpu) = NULL;
  687. }
  688. static int cpu_cache_sysfs_init(unsigned int cpu)
  689. {
  690. struct device *dev = get_cpu_device(cpu);
  691. if (per_cpu_cacheinfo(cpu) == NULL)
  692. return -ENOENT;
  693. per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
  694. if (IS_ERR(per_cpu_cache_dev(cpu)))
  695. return PTR_ERR(per_cpu_cache_dev(cpu));
  696. /* Allocate all required memory */
  697. per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
  698. sizeof(struct device *), GFP_KERNEL);
  699. if (unlikely(per_cpu_index_dev(cpu) == NULL))
  700. goto err_out;
  701. return 0;
  702. err_out:
  703. cpu_cache_sysfs_exit(cpu);
  704. return -ENOMEM;
  705. }
  706. static int cache_add_dev(unsigned int cpu)
  707. {
  708. unsigned int i;
  709. int rc;
  710. struct device *ci_dev, *parent;
  711. struct cacheinfo *this_leaf;
  712. const struct attribute_group **cache_groups;
  713. rc = cpu_cache_sysfs_init(cpu);
  714. if (unlikely(rc < 0))
  715. return rc;
  716. parent = per_cpu_cache_dev(cpu);
  717. for (i = 0; i < cache_leaves(cpu); i++) {
  718. this_leaf = per_cpu_cacheinfo_idx(cpu, i);
  719. if (this_leaf->disable_sysfs)
  720. continue;
  721. if (this_leaf->type == CACHE_TYPE_NOCACHE)
  722. break;
  723. cache_groups = cache_get_attribute_groups(this_leaf);
  724. ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
  725. "index%1u", i);
  726. if (IS_ERR(ci_dev)) {
  727. rc = PTR_ERR(ci_dev);
  728. goto err;
  729. }
  730. per_cache_index_dev(cpu, i) = ci_dev;
  731. }
  732. cpumask_set_cpu(cpu, &cache_dev_map);
  733. return 0;
  734. err:
  735. cpu_cache_sysfs_exit(cpu);
  736. return rc;
  737. }
  738. static unsigned int cpu_map_shared_cache(bool online, unsigned int cpu,
  739. cpumask_t **map)
  740. {
  741. struct cacheinfo *llc, *sib_llc;
  742. unsigned int sibling;
  743. if (!last_level_cache_is_valid(cpu))
  744. return 0;
  745. llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
  746. if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
  747. return 0;
  748. if (online) {
  749. *map = &llc->shared_cpu_map;
  750. return cpumask_weight(*map);
  751. }
  752. /* shared_cpu_map of offlined CPU will be cleared, so use sibling map */
  753. for_each_cpu(sibling, &llc->shared_cpu_map) {
  754. if (sibling == cpu || !last_level_cache_is_valid(sibling))
  755. continue;
  756. sib_llc = per_cpu_cacheinfo_idx(sibling, cache_leaves(sibling) - 1);
  757. *map = &sib_llc->shared_cpu_map;
  758. return cpumask_weight(*map);
  759. }
  760. return 0;
  761. }
  762. /*
  763. * Calculate the size of the per-CPU data cache slice. This can be
  764. * used to estimate the size of the data cache slice that can be used
  765. * by one CPU under ideal circumstances. UNIFIED caches are counted
  766. * in addition to DATA caches. So, please consider code cache usage
  767. * when use the result.
  768. *
  769. * Because the cache inclusive/non-inclusive information isn't
  770. * available, we just use the size of the per-CPU slice of LLC to make
  771. * the result more predictable across architectures.
  772. */
  773. static void update_per_cpu_data_slice_size_cpu(unsigned int cpu)
  774. {
  775. struct cpu_cacheinfo *ci;
  776. struct cacheinfo *llc;
  777. unsigned int nr_shared;
  778. if (!last_level_cache_is_valid(cpu))
  779. return;
  780. ci = ci_cacheinfo(cpu);
  781. llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
  782. if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
  783. return;
  784. nr_shared = cpumask_weight(&llc->shared_cpu_map);
  785. if (nr_shared)
  786. ci->per_cpu_data_slice_size = llc->size / nr_shared;
  787. }
  788. static void update_per_cpu_data_slice_size(bool cpu_online, unsigned int cpu,
  789. cpumask_t *cpu_map)
  790. {
  791. unsigned int icpu;
  792. for_each_cpu(icpu, cpu_map) {
  793. if (!cpu_online && icpu == cpu)
  794. continue;
  795. update_per_cpu_data_slice_size_cpu(icpu);
  796. setup_pcp_cacheinfo(icpu);
  797. }
  798. }
  799. static int cacheinfo_cpu_online(unsigned int cpu)
  800. {
  801. int rc = detect_cache_attributes(cpu);
  802. cpumask_t *cpu_map;
  803. if (rc)
  804. return rc;
  805. rc = cache_add_dev(cpu);
  806. if (rc)
  807. goto err;
  808. if (cpu_map_shared_cache(true, cpu, &cpu_map))
  809. update_per_cpu_data_slice_size(true, cpu, cpu_map);
  810. return 0;
  811. err:
  812. free_cache_attributes(cpu);
  813. return rc;
  814. }
  815. static int cacheinfo_cpu_pre_down(unsigned int cpu)
  816. {
  817. cpumask_t *cpu_map;
  818. unsigned int nr_shared;
  819. nr_shared = cpu_map_shared_cache(false, cpu, &cpu_map);
  820. if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
  821. cpu_cache_sysfs_exit(cpu);
  822. free_cache_attributes(cpu);
  823. if (nr_shared > 1)
  824. update_per_cpu_data_slice_size(false, cpu, cpu_map);
  825. return 0;
  826. }
  827. static int __init cacheinfo_sysfs_init(void)
  828. {
  829. return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
  830. "base/cacheinfo:online",
  831. cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
  832. }
  833. device_initcall(cacheinfo_sysfs_init);