cacheinfo.c 26 KB

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