cacheinfo.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Routines to identify caches on Intel CPU.
  4. *
  5. * Changes:
  6. * Venkatesh Pallipadi : Adding cache identification through cpuid(4)
  7. * Ashok Raj <[email protected]>: Work with CPU hotplug infrastructure.
  8. * Andi Kleen / Andreas Herrmann : CPUID4 emulation on AMD.
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/cacheinfo.h>
  12. #include <linux/cpu.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/pci.h>
  17. #include <asm/cpufeature.h>
  18. #include <asm/cacheinfo.h>
  19. #include <asm/amd_nb.h>
  20. #include <asm/smp.h>
  21. #include "cpu.h"
  22. #define LVL_1_INST 1
  23. #define LVL_1_DATA 2
  24. #define LVL_2 3
  25. #define LVL_3 4
  26. #define LVL_TRACE 5
  27. /* Shared last level cache maps */
  28. DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
  29. /* Shared L2 cache maps */
  30. DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_l2c_shared_map);
  31. struct _cache_table {
  32. unsigned char descriptor;
  33. char cache_type;
  34. short size;
  35. };
  36. #define MB(x) ((x) * 1024)
  37. /* All the cache descriptor types we care about (no TLB or
  38. trace cache entries) */
  39. static const struct _cache_table cache_table[] =
  40. {
  41. { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */
  42. { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */
  43. { 0x09, LVL_1_INST, 32 }, /* 4-way set assoc, 64 byte line size */
  44. { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */
  45. { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */
  46. { 0x0d, LVL_1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */
  47. { 0x0e, LVL_1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */
  48. { 0x21, LVL_2, 256 }, /* 8-way set assoc, 64 byte line size */
  49. { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  50. { 0x23, LVL_3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  51. { 0x25, LVL_3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  52. { 0x29, LVL_3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  53. { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */
  54. { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */
  55. { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  56. { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  57. { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line size */
  58. { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  59. { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  60. { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  61. { 0x3f, LVL_2, 256 }, /* 2-way set assoc, 64 byte line size */
  62. { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */
  63. { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */
  64. { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */
  65. { 0x44, LVL_2, MB(1) }, /* 4-way set assoc, 32 byte line size */
  66. { 0x45, LVL_2, MB(2) }, /* 4-way set assoc, 32 byte line size */
  67. { 0x46, LVL_3, MB(4) }, /* 4-way set assoc, 64 byte line size */
  68. { 0x47, LVL_3, MB(8) }, /* 8-way set assoc, 64 byte line size */
  69. { 0x48, LVL_2, MB(3) }, /* 12-way set assoc, 64 byte line size */
  70. { 0x49, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  71. { 0x4a, LVL_3, MB(6) }, /* 12-way set assoc, 64 byte line size */
  72. { 0x4b, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  73. { 0x4c, LVL_3, MB(12) }, /* 12-way set assoc, 64 byte line size */
  74. { 0x4d, LVL_3, MB(16) }, /* 16-way set assoc, 64 byte line size */
  75. { 0x4e, LVL_2, MB(6) }, /* 24-way set assoc, 64 byte line size */
  76. { 0x60, LVL_1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  77. { 0x66, LVL_1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  78. { 0x67, LVL_1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  79. { 0x68, LVL_1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  80. { 0x70, LVL_TRACE, 12 }, /* 8-way set assoc */
  81. { 0x71, LVL_TRACE, 16 }, /* 8-way set assoc */
  82. { 0x72, LVL_TRACE, 32 }, /* 8-way set assoc */
  83. { 0x73, LVL_TRACE, 64 }, /* 8-way set assoc */
  84. { 0x78, LVL_2, MB(1) }, /* 4-way set assoc, 64 byte line size */
  85. { 0x79, LVL_2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  86. { 0x7a, LVL_2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  87. { 0x7b, LVL_2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  88. { 0x7c, LVL_2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  89. { 0x7d, LVL_2, MB(2) }, /* 8-way set assoc, 64 byte line size */
  90. { 0x7f, LVL_2, 512 }, /* 2-way set assoc, 64 byte line size */
  91. { 0x80, LVL_2, 512 }, /* 8-way set assoc, 64 byte line size */
  92. { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */
  93. { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */
  94. { 0x84, LVL_2, MB(1) }, /* 8-way set assoc, 32 byte line size */
  95. { 0x85, LVL_2, MB(2) }, /* 8-way set assoc, 32 byte line size */
  96. { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */
  97. { 0x87, LVL_2, MB(1) }, /* 8-way set assoc, 64 byte line size */
  98. { 0xd0, LVL_3, 512 }, /* 4-way set assoc, 64 byte line size */
  99. { 0xd1, LVL_3, MB(1) }, /* 4-way set assoc, 64 byte line size */
  100. { 0xd2, LVL_3, MB(2) }, /* 4-way set assoc, 64 byte line size */
  101. { 0xd6, LVL_3, MB(1) }, /* 8-way set assoc, 64 byte line size */
  102. { 0xd7, LVL_3, MB(2) }, /* 8-way set assoc, 64 byte line size */
  103. { 0xd8, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  104. { 0xdc, LVL_3, MB(2) }, /* 12-way set assoc, 64 byte line size */
  105. { 0xdd, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  106. { 0xde, LVL_3, MB(8) }, /* 12-way set assoc, 64 byte line size */
  107. { 0xe2, LVL_3, MB(2) }, /* 16-way set assoc, 64 byte line size */
  108. { 0xe3, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  109. { 0xe4, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  110. { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */
  111. { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */
  112. { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */
  113. { 0x00, 0, 0}
  114. };
  115. enum _cache_type {
  116. CTYPE_NULL = 0,
  117. CTYPE_DATA = 1,
  118. CTYPE_INST = 2,
  119. CTYPE_UNIFIED = 3
  120. };
  121. union _cpuid4_leaf_eax {
  122. struct {
  123. enum _cache_type type:5;
  124. unsigned int level:3;
  125. unsigned int is_self_initializing:1;
  126. unsigned int is_fully_associative:1;
  127. unsigned int reserved:4;
  128. unsigned int num_threads_sharing:12;
  129. unsigned int num_cores_on_die:6;
  130. } split;
  131. u32 full;
  132. };
  133. union _cpuid4_leaf_ebx {
  134. struct {
  135. unsigned int coherency_line_size:12;
  136. unsigned int physical_line_partition:10;
  137. unsigned int ways_of_associativity:10;
  138. } split;
  139. u32 full;
  140. };
  141. union _cpuid4_leaf_ecx {
  142. struct {
  143. unsigned int number_of_sets:32;
  144. } split;
  145. u32 full;
  146. };
  147. struct _cpuid4_info_regs {
  148. union _cpuid4_leaf_eax eax;
  149. union _cpuid4_leaf_ebx ebx;
  150. union _cpuid4_leaf_ecx ecx;
  151. unsigned int id;
  152. unsigned long size;
  153. struct amd_northbridge *nb;
  154. };
  155. static unsigned short num_cache_leaves;
  156. /* AMD doesn't have CPUID4. Emulate it here to report the same
  157. information to the user. This makes some assumptions about the machine:
  158. L2 not shared, no SMT etc. that is currently true on AMD CPUs.
  159. In theory the TLBs could be reported as fake type (they are in "dummy").
  160. Maybe later */
  161. union l1_cache {
  162. struct {
  163. unsigned line_size:8;
  164. unsigned lines_per_tag:8;
  165. unsigned assoc:8;
  166. unsigned size_in_kb:8;
  167. };
  168. unsigned val;
  169. };
  170. union l2_cache {
  171. struct {
  172. unsigned line_size:8;
  173. unsigned lines_per_tag:4;
  174. unsigned assoc:4;
  175. unsigned size_in_kb:16;
  176. };
  177. unsigned val;
  178. };
  179. union l3_cache {
  180. struct {
  181. unsigned line_size:8;
  182. unsigned lines_per_tag:4;
  183. unsigned assoc:4;
  184. unsigned res:2;
  185. unsigned size_encoded:14;
  186. };
  187. unsigned val;
  188. };
  189. static const unsigned short assocs[] = {
  190. [1] = 1,
  191. [2] = 2,
  192. [4] = 4,
  193. [6] = 8,
  194. [8] = 16,
  195. [0xa] = 32,
  196. [0xb] = 48,
  197. [0xc] = 64,
  198. [0xd] = 96,
  199. [0xe] = 128,
  200. [0xf] = 0xffff /* fully associative - no way to show this currently */
  201. };
  202. static const unsigned char levels[] = { 1, 1, 2, 3 };
  203. static const unsigned char types[] = { 1, 2, 3, 3 };
  204. static const enum cache_type cache_type_map[] = {
  205. [CTYPE_NULL] = CACHE_TYPE_NOCACHE,
  206. [CTYPE_DATA] = CACHE_TYPE_DATA,
  207. [CTYPE_INST] = CACHE_TYPE_INST,
  208. [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
  209. };
  210. static void
  211. amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  212. union _cpuid4_leaf_ebx *ebx,
  213. union _cpuid4_leaf_ecx *ecx)
  214. {
  215. unsigned dummy;
  216. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  217. union l1_cache l1i, l1d;
  218. union l2_cache l2;
  219. union l3_cache l3;
  220. union l1_cache *l1 = &l1d;
  221. eax->full = 0;
  222. ebx->full = 0;
  223. ecx->full = 0;
  224. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  225. cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val);
  226. switch (leaf) {
  227. case 1:
  228. l1 = &l1i;
  229. fallthrough;
  230. case 0:
  231. if (!l1->val)
  232. return;
  233. assoc = assocs[l1->assoc];
  234. line_size = l1->line_size;
  235. lines_per_tag = l1->lines_per_tag;
  236. size_in_kb = l1->size_in_kb;
  237. break;
  238. case 2:
  239. if (!l2.val)
  240. return;
  241. assoc = assocs[l2.assoc];
  242. line_size = l2.line_size;
  243. lines_per_tag = l2.lines_per_tag;
  244. /* cpu_data has errata corrections for K7 applied */
  245. size_in_kb = __this_cpu_read(cpu_info.x86_cache_size);
  246. break;
  247. case 3:
  248. if (!l3.val)
  249. return;
  250. assoc = assocs[l3.assoc];
  251. line_size = l3.line_size;
  252. lines_per_tag = l3.lines_per_tag;
  253. size_in_kb = l3.size_encoded * 512;
  254. if (boot_cpu_has(X86_FEATURE_AMD_DCM)) {
  255. size_in_kb = size_in_kb >> 1;
  256. assoc = assoc >> 1;
  257. }
  258. break;
  259. default:
  260. return;
  261. }
  262. eax->split.is_self_initializing = 1;
  263. eax->split.type = types[leaf];
  264. eax->split.level = levels[leaf];
  265. eax->split.num_threads_sharing = 0;
  266. eax->split.num_cores_on_die = __this_cpu_read(cpu_info.x86_max_cores) - 1;
  267. if (assoc == 0xffff)
  268. eax->split.is_fully_associative = 1;
  269. ebx->split.coherency_line_size = line_size - 1;
  270. ebx->split.ways_of_associativity = assoc - 1;
  271. ebx->split.physical_line_partition = lines_per_tag - 1;
  272. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  273. (ebx->split.ways_of_associativity + 1) - 1;
  274. }
  275. #if defined(CONFIG_AMD_NB) && defined(CONFIG_SYSFS)
  276. /*
  277. * L3 cache descriptors
  278. */
  279. static void amd_calc_l3_indices(struct amd_northbridge *nb)
  280. {
  281. struct amd_l3_cache *l3 = &nb->l3_cache;
  282. unsigned int sc0, sc1, sc2, sc3;
  283. u32 val = 0;
  284. pci_read_config_dword(nb->misc, 0x1C4, &val);
  285. /* calculate subcache sizes */
  286. l3->subcaches[0] = sc0 = !(val & BIT(0));
  287. l3->subcaches[1] = sc1 = !(val & BIT(4));
  288. if (boot_cpu_data.x86 == 0x15) {
  289. l3->subcaches[0] = sc0 += !(val & BIT(1));
  290. l3->subcaches[1] = sc1 += !(val & BIT(5));
  291. }
  292. l3->subcaches[2] = sc2 = !(val & BIT(8)) + !(val & BIT(9));
  293. l3->subcaches[3] = sc3 = !(val & BIT(12)) + !(val & BIT(13));
  294. l3->indices = (max(max3(sc0, sc1, sc2), sc3) << 10) - 1;
  295. }
  296. /*
  297. * check whether a slot used for disabling an L3 index is occupied.
  298. * @l3: L3 cache descriptor
  299. * @slot: slot number (0..1)
  300. *
  301. * @returns: the disabled index if used or negative value if slot free.
  302. */
  303. static int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned slot)
  304. {
  305. unsigned int reg = 0;
  306. pci_read_config_dword(nb->misc, 0x1BC + slot * 4, &reg);
  307. /* check whether this slot is activated already */
  308. if (reg & (3UL << 30))
  309. return reg & 0xfff;
  310. return -1;
  311. }
  312. static ssize_t show_cache_disable(struct cacheinfo *this_leaf, char *buf,
  313. unsigned int slot)
  314. {
  315. int index;
  316. struct amd_northbridge *nb = this_leaf->priv;
  317. index = amd_get_l3_disable_slot(nb, slot);
  318. if (index >= 0)
  319. return sprintf(buf, "%d\n", index);
  320. return sprintf(buf, "FREE\n");
  321. }
  322. #define SHOW_CACHE_DISABLE(slot) \
  323. static ssize_t \
  324. cache_disable_##slot##_show(struct device *dev, \
  325. struct device_attribute *attr, char *buf) \
  326. { \
  327. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  328. return show_cache_disable(this_leaf, buf, slot); \
  329. }
  330. SHOW_CACHE_DISABLE(0)
  331. SHOW_CACHE_DISABLE(1)
  332. static void amd_l3_disable_index(struct amd_northbridge *nb, int cpu,
  333. unsigned slot, unsigned long idx)
  334. {
  335. int i;
  336. idx |= BIT(30);
  337. /*
  338. * disable index in all 4 subcaches
  339. */
  340. for (i = 0; i < 4; i++) {
  341. u32 reg = idx | (i << 20);
  342. if (!nb->l3_cache.subcaches[i])
  343. continue;
  344. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  345. /*
  346. * We need to WBINVD on a core on the node containing the L3
  347. * cache which indices we disable therefore a simple wbinvd()
  348. * is not sufficient.
  349. */
  350. wbinvd_on_cpu(cpu);
  351. reg |= BIT(31);
  352. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  353. }
  354. }
  355. /*
  356. * disable a L3 cache index by using a disable-slot
  357. *
  358. * @l3: L3 cache descriptor
  359. * @cpu: A CPU on the node containing the L3 cache
  360. * @slot: slot number (0..1)
  361. * @index: index to disable
  362. *
  363. * @return: 0 on success, error status on failure
  364. */
  365. static int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu,
  366. unsigned slot, unsigned long index)
  367. {
  368. int ret = 0;
  369. /* check if @slot is already used or the index is already disabled */
  370. ret = amd_get_l3_disable_slot(nb, slot);
  371. if (ret >= 0)
  372. return -EEXIST;
  373. if (index > nb->l3_cache.indices)
  374. return -EINVAL;
  375. /* check whether the other slot has disabled the same index already */
  376. if (index == amd_get_l3_disable_slot(nb, !slot))
  377. return -EEXIST;
  378. amd_l3_disable_index(nb, cpu, slot, index);
  379. return 0;
  380. }
  381. static ssize_t store_cache_disable(struct cacheinfo *this_leaf,
  382. const char *buf, size_t count,
  383. unsigned int slot)
  384. {
  385. unsigned long val = 0;
  386. int cpu, err = 0;
  387. struct amd_northbridge *nb = this_leaf->priv;
  388. if (!capable(CAP_SYS_ADMIN))
  389. return -EPERM;
  390. cpu = cpumask_first(&this_leaf->shared_cpu_map);
  391. if (kstrtoul(buf, 10, &val) < 0)
  392. return -EINVAL;
  393. err = amd_set_l3_disable_slot(nb, cpu, slot, val);
  394. if (err) {
  395. if (err == -EEXIST)
  396. pr_warn("L3 slot %d in use/index already disabled!\n",
  397. slot);
  398. return err;
  399. }
  400. return count;
  401. }
  402. #define STORE_CACHE_DISABLE(slot) \
  403. static ssize_t \
  404. cache_disable_##slot##_store(struct device *dev, \
  405. struct device_attribute *attr, \
  406. const char *buf, size_t count) \
  407. { \
  408. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  409. return store_cache_disable(this_leaf, buf, count, slot); \
  410. }
  411. STORE_CACHE_DISABLE(0)
  412. STORE_CACHE_DISABLE(1)
  413. static ssize_t subcaches_show(struct device *dev,
  414. struct device_attribute *attr, char *buf)
  415. {
  416. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  417. int cpu = cpumask_first(&this_leaf->shared_cpu_map);
  418. return sprintf(buf, "%x\n", amd_get_subcaches(cpu));
  419. }
  420. static ssize_t subcaches_store(struct device *dev,
  421. struct device_attribute *attr,
  422. const char *buf, size_t count)
  423. {
  424. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  425. int cpu = cpumask_first(&this_leaf->shared_cpu_map);
  426. unsigned long val;
  427. if (!capable(CAP_SYS_ADMIN))
  428. return -EPERM;
  429. if (kstrtoul(buf, 16, &val) < 0)
  430. return -EINVAL;
  431. if (amd_set_subcaches(cpu, val))
  432. return -EINVAL;
  433. return count;
  434. }
  435. static DEVICE_ATTR_RW(cache_disable_0);
  436. static DEVICE_ATTR_RW(cache_disable_1);
  437. static DEVICE_ATTR_RW(subcaches);
  438. static umode_t
  439. cache_private_attrs_is_visible(struct kobject *kobj,
  440. struct attribute *attr, int unused)
  441. {
  442. struct device *dev = kobj_to_dev(kobj);
  443. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  444. umode_t mode = attr->mode;
  445. if (!this_leaf->priv)
  446. return 0;
  447. if ((attr == &dev_attr_subcaches.attr) &&
  448. amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  449. return mode;
  450. if ((attr == &dev_attr_cache_disable_0.attr ||
  451. attr == &dev_attr_cache_disable_1.attr) &&
  452. amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  453. return mode;
  454. return 0;
  455. }
  456. static struct attribute_group cache_private_group = {
  457. .is_visible = cache_private_attrs_is_visible,
  458. };
  459. static void init_amd_l3_attrs(void)
  460. {
  461. int n = 1;
  462. static struct attribute **amd_l3_attrs;
  463. if (amd_l3_attrs) /* already initialized */
  464. return;
  465. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  466. n += 2;
  467. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  468. n += 1;
  469. amd_l3_attrs = kcalloc(n, sizeof(*amd_l3_attrs), GFP_KERNEL);
  470. if (!amd_l3_attrs)
  471. return;
  472. n = 0;
  473. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) {
  474. amd_l3_attrs[n++] = &dev_attr_cache_disable_0.attr;
  475. amd_l3_attrs[n++] = &dev_attr_cache_disable_1.attr;
  476. }
  477. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  478. amd_l3_attrs[n++] = &dev_attr_subcaches.attr;
  479. cache_private_group.attrs = amd_l3_attrs;
  480. }
  481. const struct attribute_group *
  482. cache_get_priv_group(struct cacheinfo *this_leaf)
  483. {
  484. struct amd_northbridge *nb = this_leaf->priv;
  485. if (this_leaf->level < 3 || !nb)
  486. return NULL;
  487. if (nb && nb->l3_cache.indices)
  488. init_amd_l3_attrs();
  489. return &cache_private_group;
  490. }
  491. static void amd_init_l3_cache(struct _cpuid4_info_regs *this_leaf, int index)
  492. {
  493. int node;
  494. /* only for L3, and not in virtualized environments */
  495. if (index < 3)
  496. return;
  497. node = topology_die_id(smp_processor_id());
  498. this_leaf->nb = node_to_amd_nb(node);
  499. if (this_leaf->nb && !this_leaf->nb->l3_cache.indices)
  500. amd_calc_l3_indices(this_leaf->nb);
  501. }
  502. #else
  503. #define amd_init_l3_cache(x, y)
  504. #endif /* CONFIG_AMD_NB && CONFIG_SYSFS */
  505. static int
  506. cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *this_leaf)
  507. {
  508. union _cpuid4_leaf_eax eax;
  509. union _cpuid4_leaf_ebx ebx;
  510. union _cpuid4_leaf_ecx ecx;
  511. unsigned edx;
  512. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  513. if (boot_cpu_has(X86_FEATURE_TOPOEXT))
  514. cpuid_count(0x8000001d, index, &eax.full,
  515. &ebx.full, &ecx.full, &edx);
  516. else
  517. amd_cpuid4(index, &eax, &ebx, &ecx);
  518. amd_init_l3_cache(this_leaf, index);
  519. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) {
  520. cpuid_count(0x8000001d, index, &eax.full,
  521. &ebx.full, &ecx.full, &edx);
  522. amd_init_l3_cache(this_leaf, index);
  523. } else {
  524. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  525. }
  526. if (eax.split.type == CTYPE_NULL)
  527. return -EIO; /* better error ? */
  528. this_leaf->eax = eax;
  529. this_leaf->ebx = ebx;
  530. this_leaf->ecx = ecx;
  531. this_leaf->size = (ecx.split.number_of_sets + 1) *
  532. (ebx.split.coherency_line_size + 1) *
  533. (ebx.split.physical_line_partition + 1) *
  534. (ebx.split.ways_of_associativity + 1);
  535. return 0;
  536. }
  537. static int find_num_cache_leaves(struct cpuinfo_x86 *c)
  538. {
  539. unsigned int eax, ebx, ecx, edx, op;
  540. union _cpuid4_leaf_eax cache_eax;
  541. int i = -1;
  542. if (c->x86_vendor == X86_VENDOR_AMD ||
  543. c->x86_vendor == X86_VENDOR_HYGON)
  544. op = 0x8000001d;
  545. else
  546. op = 4;
  547. do {
  548. ++i;
  549. /* Do cpuid(op) loop to find out num_cache_leaves */
  550. cpuid_count(op, i, &eax, &ebx, &ecx, &edx);
  551. cache_eax.full = eax;
  552. } while (cache_eax.split.type != CTYPE_NULL);
  553. return i;
  554. }
  555. void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu)
  556. {
  557. /*
  558. * We may have multiple LLCs if L3 caches exist, so check if we
  559. * have an L3 cache by looking at the L3 cache CPUID leaf.
  560. */
  561. if (!cpuid_edx(0x80000006))
  562. return;
  563. if (c->x86 < 0x17) {
  564. /* LLC is at the node level. */
  565. per_cpu(cpu_llc_id, cpu) = c->cpu_die_id;
  566. } else if (c->x86 == 0x17 && c->x86_model <= 0x1F) {
  567. /*
  568. * LLC is at the core complex level.
  569. * Core complex ID is ApicId[3] for these processors.
  570. */
  571. per_cpu(cpu_llc_id, cpu) = c->apicid >> 3;
  572. } else {
  573. /*
  574. * LLC ID is calculated from the number of threads sharing the
  575. * cache.
  576. * */
  577. u32 eax, ebx, ecx, edx, num_sharing_cache = 0;
  578. u32 llc_index = find_num_cache_leaves(c) - 1;
  579. cpuid_count(0x8000001d, llc_index, &eax, &ebx, &ecx, &edx);
  580. if (eax)
  581. num_sharing_cache = ((eax >> 14) & 0xfff) + 1;
  582. if (num_sharing_cache) {
  583. int bits = get_count_order(num_sharing_cache);
  584. per_cpu(cpu_llc_id, cpu) = c->apicid >> bits;
  585. }
  586. }
  587. }
  588. void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu)
  589. {
  590. /*
  591. * We may have multiple LLCs if L3 caches exist, so check if we
  592. * have an L3 cache by looking at the L3 cache CPUID leaf.
  593. */
  594. if (!cpuid_edx(0x80000006))
  595. return;
  596. /*
  597. * LLC is at the core complex level.
  598. * Core complex ID is ApicId[3] for these processors.
  599. */
  600. per_cpu(cpu_llc_id, cpu) = c->apicid >> 3;
  601. }
  602. void init_amd_cacheinfo(struct cpuinfo_x86 *c)
  603. {
  604. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  605. num_cache_leaves = find_num_cache_leaves(c);
  606. } else if (c->extended_cpuid_level >= 0x80000006) {
  607. if (cpuid_edx(0x80000006) & 0xf000)
  608. num_cache_leaves = 4;
  609. else
  610. num_cache_leaves = 3;
  611. }
  612. }
  613. void init_hygon_cacheinfo(struct cpuinfo_x86 *c)
  614. {
  615. num_cache_leaves = find_num_cache_leaves(c);
  616. }
  617. void init_intel_cacheinfo(struct cpuinfo_x86 *c)
  618. {
  619. /* Cache sizes */
  620. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0;
  621. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  622. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  623. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  624. #ifdef CONFIG_SMP
  625. unsigned int cpu = c->cpu_index;
  626. #endif
  627. if (c->cpuid_level > 3) {
  628. static int is_initialized;
  629. if (is_initialized == 0) {
  630. /* Init num_cache_leaves from boot CPU */
  631. num_cache_leaves = find_num_cache_leaves(c);
  632. is_initialized++;
  633. }
  634. /*
  635. * Whenever possible use cpuid(4), deterministic cache
  636. * parameters cpuid leaf to find the cache details
  637. */
  638. for (i = 0; i < num_cache_leaves; i++) {
  639. struct _cpuid4_info_regs this_leaf = {};
  640. int retval;
  641. retval = cpuid4_cache_lookup_regs(i, &this_leaf);
  642. if (retval < 0)
  643. continue;
  644. switch (this_leaf.eax.split.level) {
  645. case 1:
  646. if (this_leaf.eax.split.type == CTYPE_DATA)
  647. new_l1d = this_leaf.size/1024;
  648. else if (this_leaf.eax.split.type == CTYPE_INST)
  649. new_l1i = this_leaf.size/1024;
  650. break;
  651. case 2:
  652. new_l2 = this_leaf.size/1024;
  653. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  654. index_msb = get_count_order(num_threads_sharing);
  655. l2_id = c->apicid & ~((1 << index_msb) - 1);
  656. break;
  657. case 3:
  658. new_l3 = this_leaf.size/1024;
  659. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  660. index_msb = get_count_order(num_threads_sharing);
  661. l3_id = c->apicid & ~((1 << index_msb) - 1);
  662. break;
  663. default:
  664. break;
  665. }
  666. }
  667. }
  668. /*
  669. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  670. * trace cache
  671. */
  672. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  673. /* supports eax=2 call */
  674. int j, n;
  675. unsigned int regs[4];
  676. unsigned char *dp = (unsigned char *)regs;
  677. int only_trace = 0;
  678. if (num_cache_leaves != 0 && c->x86 == 15)
  679. only_trace = 1;
  680. /* Number of times to iterate */
  681. n = cpuid_eax(2) & 0xFF;
  682. for (i = 0 ; i < n ; i++) {
  683. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  684. /* If bit 31 is set, this is an unknown format */
  685. for (j = 0 ; j < 3 ; j++)
  686. if (regs[j] & (1 << 31))
  687. regs[j] = 0;
  688. /* Byte 0 is level count, not a descriptor */
  689. for (j = 1 ; j < 16 ; j++) {
  690. unsigned char des = dp[j];
  691. unsigned char k = 0;
  692. /* look up this descriptor in the table */
  693. while (cache_table[k].descriptor != 0) {
  694. if (cache_table[k].descriptor == des) {
  695. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  696. break;
  697. switch (cache_table[k].cache_type) {
  698. case LVL_1_INST:
  699. l1i += cache_table[k].size;
  700. break;
  701. case LVL_1_DATA:
  702. l1d += cache_table[k].size;
  703. break;
  704. case LVL_2:
  705. l2 += cache_table[k].size;
  706. break;
  707. case LVL_3:
  708. l3 += cache_table[k].size;
  709. break;
  710. case LVL_TRACE:
  711. trace += cache_table[k].size;
  712. break;
  713. }
  714. break;
  715. }
  716. k++;
  717. }
  718. }
  719. }
  720. }
  721. if (new_l1d)
  722. l1d = new_l1d;
  723. if (new_l1i)
  724. l1i = new_l1i;
  725. if (new_l2) {
  726. l2 = new_l2;
  727. #ifdef CONFIG_SMP
  728. per_cpu(cpu_llc_id, cpu) = l2_id;
  729. per_cpu(cpu_l2c_id, cpu) = l2_id;
  730. #endif
  731. }
  732. if (new_l3) {
  733. l3 = new_l3;
  734. #ifdef CONFIG_SMP
  735. per_cpu(cpu_llc_id, cpu) = l3_id;
  736. #endif
  737. }
  738. #ifdef CONFIG_SMP
  739. /*
  740. * If cpu_llc_id is not yet set, this means cpuid_level < 4 which in
  741. * turns means that the only possibility is SMT (as indicated in
  742. * cpuid1). Since cpuid2 doesn't specify shared caches, and we know
  743. * that SMT shares all caches, we can unconditionally set cpu_llc_id to
  744. * c->phys_proc_id.
  745. */
  746. if (per_cpu(cpu_llc_id, cpu) == BAD_APICID)
  747. per_cpu(cpu_llc_id, cpu) = c->phys_proc_id;
  748. #endif
  749. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  750. if (!l2)
  751. cpu_detect_cache_sizes(c);
  752. }
  753. static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
  754. struct _cpuid4_info_regs *base)
  755. {
  756. struct cpu_cacheinfo *this_cpu_ci;
  757. struct cacheinfo *this_leaf;
  758. int i, sibling;
  759. /*
  760. * For L3, always use the pre-calculated cpu_llc_shared_mask
  761. * to derive shared_cpu_map.
  762. */
  763. if (index == 3) {
  764. for_each_cpu(i, cpu_llc_shared_mask(cpu)) {
  765. this_cpu_ci = get_cpu_cacheinfo(i);
  766. if (!this_cpu_ci->info_list)
  767. continue;
  768. this_leaf = this_cpu_ci->info_list + index;
  769. for_each_cpu(sibling, cpu_llc_shared_mask(cpu)) {
  770. if (!cpu_online(sibling))
  771. continue;
  772. cpumask_set_cpu(sibling,
  773. &this_leaf->shared_cpu_map);
  774. }
  775. }
  776. } else if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  777. unsigned int apicid, nshared, first, last;
  778. nshared = base->eax.split.num_threads_sharing + 1;
  779. apicid = cpu_data(cpu).apicid;
  780. first = apicid - (apicid % nshared);
  781. last = first + nshared - 1;
  782. for_each_online_cpu(i) {
  783. this_cpu_ci = get_cpu_cacheinfo(i);
  784. if (!this_cpu_ci->info_list)
  785. continue;
  786. apicid = cpu_data(i).apicid;
  787. if ((apicid < first) || (apicid > last))
  788. continue;
  789. this_leaf = this_cpu_ci->info_list + index;
  790. for_each_online_cpu(sibling) {
  791. apicid = cpu_data(sibling).apicid;
  792. if ((apicid < first) || (apicid > last))
  793. continue;
  794. cpumask_set_cpu(sibling,
  795. &this_leaf->shared_cpu_map);
  796. }
  797. }
  798. } else
  799. return 0;
  800. return 1;
  801. }
  802. static void __cache_cpumap_setup(unsigned int cpu, int index,
  803. struct _cpuid4_info_regs *base)
  804. {
  805. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  806. struct cacheinfo *this_leaf, *sibling_leaf;
  807. unsigned long num_threads_sharing;
  808. int index_msb, i;
  809. struct cpuinfo_x86 *c = &cpu_data(cpu);
  810. if (c->x86_vendor == X86_VENDOR_AMD ||
  811. c->x86_vendor == X86_VENDOR_HYGON) {
  812. if (__cache_amd_cpumap_setup(cpu, index, base))
  813. return;
  814. }
  815. this_leaf = this_cpu_ci->info_list + index;
  816. num_threads_sharing = 1 + base->eax.split.num_threads_sharing;
  817. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  818. if (num_threads_sharing == 1)
  819. return;
  820. index_msb = get_count_order(num_threads_sharing);
  821. for_each_online_cpu(i)
  822. if (cpu_data(i).apicid >> index_msb == c->apicid >> index_msb) {
  823. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  824. if (i == cpu || !sib_cpu_ci->info_list)
  825. continue;/* skip if itself or no cacheinfo */
  826. sibling_leaf = sib_cpu_ci->info_list + index;
  827. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  828. cpumask_set_cpu(cpu, &sibling_leaf->shared_cpu_map);
  829. }
  830. }
  831. static void ci_leaf_init(struct cacheinfo *this_leaf,
  832. struct _cpuid4_info_regs *base)
  833. {
  834. this_leaf->id = base->id;
  835. this_leaf->attributes = CACHE_ID;
  836. this_leaf->level = base->eax.split.level;
  837. this_leaf->type = cache_type_map[base->eax.split.type];
  838. this_leaf->coherency_line_size =
  839. base->ebx.split.coherency_line_size + 1;
  840. this_leaf->ways_of_associativity =
  841. base->ebx.split.ways_of_associativity + 1;
  842. this_leaf->size = base->size;
  843. this_leaf->number_of_sets = base->ecx.split.number_of_sets + 1;
  844. this_leaf->physical_line_partition =
  845. base->ebx.split.physical_line_partition + 1;
  846. this_leaf->priv = base->nb;
  847. }
  848. int init_cache_level(unsigned int cpu)
  849. {
  850. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  851. if (!num_cache_leaves)
  852. return -ENOENT;
  853. if (!this_cpu_ci)
  854. return -EINVAL;
  855. this_cpu_ci->num_levels = 3;
  856. this_cpu_ci->num_leaves = num_cache_leaves;
  857. return 0;
  858. }
  859. /*
  860. * The max shared threads number comes from CPUID.4:EAX[25-14] with input
  861. * ECX as cache index. Then right shift apicid by the number's order to get
  862. * cache id for this cache node.
  863. */
  864. static void get_cache_id(int cpu, struct _cpuid4_info_regs *id4_regs)
  865. {
  866. struct cpuinfo_x86 *c = &cpu_data(cpu);
  867. unsigned long num_threads_sharing;
  868. int index_msb;
  869. num_threads_sharing = 1 + id4_regs->eax.split.num_threads_sharing;
  870. index_msb = get_count_order(num_threads_sharing);
  871. id4_regs->id = c->apicid >> index_msb;
  872. }
  873. int populate_cache_leaves(unsigned int cpu)
  874. {
  875. unsigned int idx, ret;
  876. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  877. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  878. struct _cpuid4_info_regs id4_regs = {};
  879. for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
  880. ret = cpuid4_cache_lookup_regs(idx, &id4_regs);
  881. if (ret)
  882. return ret;
  883. get_cache_id(cpu, &id4_regs);
  884. ci_leaf_init(this_leaf++, &id4_regs);
  885. __cache_cpumap_setup(cpu, idx, &id4_regs);
  886. }
  887. this_cpu_ci->cpu_map_populated = true;
  888. return 0;
  889. }