cacheinfo.c 33 KB

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