intel_cacheinfo.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Routines to indentify caches on Intel CPU.
  3. *
  4. * Changes:
  5. * Venkatesh Pallipadi : Adding cache identification through cpuid(4)
  6. * Ashok Raj <ashok.raj@intel.com>: Work with CPU hotplug infrastructure.
  7. * Andi Kleen / Andreas Herrmann : CPUID4 emulation on AMD.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/device.h>
  12. #include <linux/compiler.h>
  13. #include <linux/cpu.h>
  14. #include <linux/sched.h>
  15. #include <linux/pci.h>
  16. #include <asm/processor.h>
  17. #include <linux/smp.h>
  18. #include <asm/k8.h>
  19. #define LVL_1_INST 1
  20. #define LVL_1_DATA 2
  21. #define LVL_2 3
  22. #define LVL_3 4
  23. #define LVL_TRACE 5
  24. struct _cache_table {
  25. unsigned char descriptor;
  26. char cache_type;
  27. short size;
  28. };
  29. #define MB(x) ((x) * 1024)
  30. /* All the cache descriptor types we care about (no TLB or
  31. trace cache entries) */
  32. static const struct _cache_table __cpuinitconst cache_table[] =
  33. {
  34. { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */
  35. { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */
  36. { 0x09, LVL_1_INST, 32 }, /* 4-way set assoc, 64 byte line size */
  37. { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */
  38. { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */
  39. { 0x0d, LVL_1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */
  40. { 0x21, LVL_2, 256 }, /* 8-way set assoc, 64 byte line size */
  41. { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  42. { 0x23, LVL_3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  43. { 0x25, LVL_3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  44. { 0x29, LVL_3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  45. { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */
  46. { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */
  47. { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  48. { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  49. { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line size */
  50. { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  51. { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  52. { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  53. { 0x3f, LVL_2, 256 }, /* 2-way set assoc, 64 byte line size */
  54. { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */
  55. { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */
  56. { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */
  57. { 0x44, LVL_2, MB(1) }, /* 4-way set assoc, 32 byte line size */
  58. { 0x45, LVL_2, MB(2) }, /* 4-way set assoc, 32 byte line size */
  59. { 0x46, LVL_3, MB(4) }, /* 4-way set assoc, 64 byte line size */
  60. { 0x47, LVL_3, MB(8) }, /* 8-way set assoc, 64 byte line size */
  61. { 0x49, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  62. { 0x4a, LVL_3, MB(6) }, /* 12-way set assoc, 64 byte line size */
  63. { 0x4b, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  64. { 0x4c, LVL_3, MB(12) }, /* 12-way set assoc, 64 byte line size */
  65. { 0x4d, LVL_3, MB(16) }, /* 16-way set assoc, 64 byte line size */
  66. { 0x4e, LVL_2, MB(6) }, /* 24-way set assoc, 64 byte line size */
  67. { 0x60, LVL_1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  68. { 0x66, LVL_1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  69. { 0x67, LVL_1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  70. { 0x68, LVL_1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  71. { 0x70, LVL_TRACE, 12 }, /* 8-way set assoc */
  72. { 0x71, LVL_TRACE, 16 }, /* 8-way set assoc */
  73. { 0x72, LVL_TRACE, 32 }, /* 8-way set assoc */
  74. { 0x73, LVL_TRACE, 64 }, /* 8-way set assoc */
  75. { 0x78, LVL_2, MB(1) }, /* 4-way set assoc, 64 byte line size */
  76. { 0x79, LVL_2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  77. { 0x7a, LVL_2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  78. { 0x7b, LVL_2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  79. { 0x7c, LVL_2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  80. { 0x7d, LVL_2, MB(2) }, /* 8-way set assoc, 64 byte line size */
  81. { 0x7f, LVL_2, 512 }, /* 2-way set assoc, 64 byte line size */
  82. { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */
  83. { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */
  84. { 0x84, LVL_2, MB(1) }, /* 8-way set assoc, 32 byte line size */
  85. { 0x85, LVL_2, MB(2) }, /* 8-way set assoc, 32 byte line size */
  86. { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */
  87. { 0x87, LVL_2, MB(1) }, /* 8-way set assoc, 64 byte line size */
  88. { 0xd0, LVL_3, 512 }, /* 4-way set assoc, 64 byte line size */
  89. { 0xd1, LVL_3, MB(1) }, /* 4-way set assoc, 64 byte line size */
  90. { 0xd2, LVL_3, MB(2) }, /* 4-way set assoc, 64 byte line size */
  91. { 0xd6, LVL_3, MB(1) }, /* 8-way set assoc, 64 byte line size */
  92. { 0xd7, LVL_3, MB(2) }, /* 8-way set assoc, 64 byte line size */
  93. { 0xd8, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  94. { 0xdc, LVL_3, MB(2) }, /* 12-way set assoc, 64 byte line size */
  95. { 0xdd, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  96. { 0xde, LVL_3, MB(8) }, /* 12-way set assoc, 64 byte line size */
  97. { 0xe2, LVL_3, MB(2) }, /* 16-way set assoc, 64 byte line size */
  98. { 0xe3, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  99. { 0xe4, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  100. { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */
  101. { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */
  102. { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */
  103. { 0x00, 0, 0}
  104. };
  105. enum _cache_type {
  106. CACHE_TYPE_NULL = 0,
  107. CACHE_TYPE_DATA = 1,
  108. CACHE_TYPE_INST = 2,
  109. CACHE_TYPE_UNIFIED = 3
  110. };
  111. union _cpuid4_leaf_eax {
  112. struct {
  113. enum _cache_type type:5;
  114. unsigned int level:3;
  115. unsigned int is_self_initializing:1;
  116. unsigned int is_fully_associative:1;
  117. unsigned int reserved:4;
  118. unsigned int num_threads_sharing:12;
  119. unsigned int num_cores_on_die:6;
  120. } split;
  121. u32 full;
  122. };
  123. union _cpuid4_leaf_ebx {
  124. struct {
  125. unsigned int coherency_line_size:12;
  126. unsigned int physical_line_partition:10;
  127. unsigned int ways_of_associativity:10;
  128. } split;
  129. u32 full;
  130. };
  131. union _cpuid4_leaf_ecx {
  132. struct {
  133. unsigned int number_of_sets:32;
  134. } split;
  135. u32 full;
  136. };
  137. struct _cpuid4_info {
  138. union _cpuid4_leaf_eax eax;
  139. union _cpuid4_leaf_ebx ebx;
  140. union _cpuid4_leaf_ecx ecx;
  141. unsigned long size;
  142. unsigned long can_disable;
  143. DECLARE_BITMAP(shared_cpu_map, NR_CPUS);
  144. };
  145. /* subset of above _cpuid4_info w/o shared_cpu_map */
  146. struct _cpuid4_info_regs {
  147. union _cpuid4_leaf_eax eax;
  148. union _cpuid4_leaf_ebx ebx;
  149. union _cpuid4_leaf_ecx ecx;
  150. unsigned long size;
  151. unsigned long can_disable;
  152. };
  153. unsigned short num_cache_leaves;
  154. /* AMD doesn't have CPUID4. Emulate it here to report the same
  155. information to the user. This makes some assumptions about the machine:
  156. L2 not shared, no SMT etc. that is currently true on AMD CPUs.
  157. In theory the TLBs could be reported as fake type (they are in "dummy").
  158. Maybe later */
  159. union l1_cache {
  160. struct {
  161. unsigned line_size:8;
  162. unsigned lines_per_tag:8;
  163. unsigned assoc:8;
  164. unsigned size_in_kb:8;
  165. };
  166. unsigned val;
  167. };
  168. union l2_cache {
  169. struct {
  170. unsigned line_size:8;
  171. unsigned lines_per_tag:4;
  172. unsigned assoc:4;
  173. unsigned size_in_kb:16;
  174. };
  175. unsigned val;
  176. };
  177. union l3_cache {
  178. struct {
  179. unsigned line_size:8;
  180. unsigned lines_per_tag:4;
  181. unsigned assoc:4;
  182. unsigned res:2;
  183. unsigned size_encoded:14;
  184. };
  185. unsigned val;
  186. };
  187. static const unsigned short __cpuinitconst assocs[] = {
  188. [1] = 1,
  189. [2] = 2,
  190. [4] = 4,
  191. [6] = 8,
  192. [8] = 16,
  193. [0xa] = 32,
  194. [0xb] = 48,
  195. [0xc] = 64,
  196. [0xd] = 96,
  197. [0xe] = 128,
  198. [0xf] = 0xffff /* fully associative - no way to show this currently */
  199. };
  200. static const unsigned char __cpuinitconst levels[] = { 1, 1, 2, 3 };
  201. static const unsigned char __cpuinitconst types[] = { 1, 2, 3, 3 };
  202. static void __cpuinit
  203. amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  204. union _cpuid4_leaf_ebx *ebx,
  205. union _cpuid4_leaf_ecx *ecx)
  206. {
  207. unsigned dummy;
  208. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  209. union l1_cache l1i, l1d;
  210. union l2_cache l2;
  211. union l3_cache l3;
  212. union l1_cache *l1 = &l1d;
  213. eax->full = 0;
  214. ebx->full = 0;
  215. ecx->full = 0;
  216. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  217. cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val);
  218. switch (leaf) {
  219. case 1:
  220. l1 = &l1i;
  221. case 0:
  222. if (!l1->val)
  223. return;
  224. assoc = assocs[l1->assoc];
  225. line_size = l1->line_size;
  226. lines_per_tag = l1->lines_per_tag;
  227. size_in_kb = l1->size_in_kb;
  228. break;
  229. case 2:
  230. if (!l2.val)
  231. return;
  232. assoc = assocs[l2.assoc];
  233. line_size = l2.line_size;
  234. lines_per_tag = l2.lines_per_tag;
  235. /* cpu_data has errata corrections for K7 applied */
  236. size_in_kb = current_cpu_data.x86_cache_size;
  237. break;
  238. case 3:
  239. if (!l3.val)
  240. return;
  241. assoc = assocs[l3.assoc];
  242. line_size = l3.line_size;
  243. lines_per_tag = l3.lines_per_tag;
  244. size_in_kb = l3.size_encoded * 512;
  245. if (boot_cpu_has(X86_FEATURE_AMD_DCM)) {
  246. size_in_kb = size_in_kb >> 1;
  247. assoc = assoc >> 1;
  248. }
  249. break;
  250. default:
  251. return;
  252. }
  253. eax->split.is_self_initializing = 1;
  254. eax->split.type = types[leaf];
  255. eax->split.level = levels[leaf];
  256. eax->split.num_threads_sharing = 0;
  257. eax->split.num_cores_on_die = current_cpu_data.x86_max_cores - 1;
  258. if (assoc == 0xffff)
  259. eax->split.is_fully_associative = 1;
  260. ebx->split.coherency_line_size = line_size - 1;
  261. ebx->split.ways_of_associativity = assoc - 1;
  262. ebx->split.physical_line_partition = lines_per_tag - 1;
  263. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  264. (ebx->split.ways_of_associativity + 1) - 1;
  265. }
  266. static void __cpuinit
  267. amd_check_l3_disable(int index, struct _cpuid4_info_regs *this_leaf)
  268. {
  269. if (index < 3)
  270. return;
  271. if (boot_cpu_data.x86 == 0x11)
  272. return;
  273. /* see erratum #382 */
  274. if ((boot_cpu_data.x86 == 0x10) && (boot_cpu_data.x86_model < 0x8))
  275. return;
  276. this_leaf->can_disable = 1;
  277. }
  278. static int
  279. __cpuinit cpuid4_cache_lookup_regs(int index,
  280. struct _cpuid4_info_regs *this_leaf)
  281. {
  282. union _cpuid4_leaf_eax eax;
  283. union _cpuid4_leaf_ebx ebx;
  284. union _cpuid4_leaf_ecx ecx;
  285. unsigned edx;
  286. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  287. amd_cpuid4(index, &eax, &ebx, &ecx);
  288. if (boot_cpu_data.x86 >= 0x10)
  289. amd_check_l3_disable(index, this_leaf);
  290. } else {
  291. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  292. }
  293. if (eax.split.type == CACHE_TYPE_NULL)
  294. return -EIO; /* better error ? */
  295. this_leaf->eax = eax;
  296. this_leaf->ebx = ebx;
  297. this_leaf->ecx = ecx;
  298. this_leaf->size = (ecx.split.number_of_sets + 1) *
  299. (ebx.split.coherency_line_size + 1) *
  300. (ebx.split.physical_line_partition + 1) *
  301. (ebx.split.ways_of_associativity + 1);
  302. return 0;
  303. }
  304. static int __cpuinit find_num_cache_leaves(void)
  305. {
  306. unsigned int eax, ebx, ecx, edx;
  307. union _cpuid4_leaf_eax cache_eax;
  308. int i = -1;
  309. do {
  310. ++i;
  311. /* Do cpuid(4) loop to find out num_cache_leaves */
  312. cpuid_count(4, i, &eax, &ebx, &ecx, &edx);
  313. cache_eax.full = eax;
  314. } while (cache_eax.split.type != CACHE_TYPE_NULL);
  315. return i;
  316. }
  317. unsigned int __cpuinit init_intel_cacheinfo(struct cpuinfo_x86 *c)
  318. {
  319. /* Cache sizes */
  320. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0;
  321. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  322. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  323. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  324. #ifdef CONFIG_X86_HT
  325. unsigned int cpu = c->cpu_index;
  326. #endif
  327. if (c->cpuid_level > 3) {
  328. static int is_initialized;
  329. if (is_initialized == 0) {
  330. /* Init num_cache_leaves from boot CPU */
  331. num_cache_leaves = find_num_cache_leaves();
  332. is_initialized++;
  333. }
  334. /*
  335. * Whenever possible use cpuid(4), deterministic cache
  336. * parameters cpuid leaf to find the cache details
  337. */
  338. for (i = 0; i < num_cache_leaves; i++) {
  339. struct _cpuid4_info_regs this_leaf;
  340. int retval;
  341. retval = cpuid4_cache_lookup_regs(i, &this_leaf);
  342. if (retval >= 0) {
  343. switch (this_leaf.eax.split.level) {
  344. case 1:
  345. if (this_leaf.eax.split.type ==
  346. CACHE_TYPE_DATA)
  347. new_l1d = this_leaf.size/1024;
  348. else if (this_leaf.eax.split.type ==
  349. CACHE_TYPE_INST)
  350. new_l1i = this_leaf.size/1024;
  351. break;
  352. case 2:
  353. new_l2 = this_leaf.size/1024;
  354. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  355. index_msb = get_count_order(num_threads_sharing);
  356. l2_id = c->apicid >> index_msb;
  357. break;
  358. case 3:
  359. new_l3 = this_leaf.size/1024;
  360. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  361. index_msb = get_count_order(
  362. num_threads_sharing);
  363. l3_id = c->apicid >> index_msb;
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. /*
  372. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  373. * trace cache
  374. */
  375. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  376. /* supports eax=2 call */
  377. int j, n;
  378. unsigned int regs[4];
  379. unsigned char *dp = (unsigned char *)regs;
  380. int only_trace = 0;
  381. if (num_cache_leaves != 0 && c->x86 == 15)
  382. only_trace = 1;
  383. /* Number of times to iterate */
  384. n = cpuid_eax(2) & 0xFF;
  385. for (i = 0 ; i < n ; i++) {
  386. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  387. /* If bit 31 is set, this is an unknown format */
  388. for (j = 0 ; j < 3 ; j++)
  389. if (regs[j] & (1 << 31))
  390. regs[j] = 0;
  391. /* Byte 0 is level count, not a descriptor */
  392. for (j = 1 ; j < 16 ; j++) {
  393. unsigned char des = dp[j];
  394. unsigned char k = 0;
  395. /* look up this descriptor in the table */
  396. while (cache_table[k].descriptor != 0) {
  397. if (cache_table[k].descriptor == des) {
  398. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  399. break;
  400. switch (cache_table[k].cache_type) {
  401. case LVL_1_INST:
  402. l1i += cache_table[k].size;
  403. break;
  404. case LVL_1_DATA:
  405. l1d += cache_table[k].size;
  406. break;
  407. case LVL_2:
  408. l2 += cache_table[k].size;
  409. break;
  410. case LVL_3:
  411. l3 += cache_table[k].size;
  412. break;
  413. case LVL_TRACE:
  414. trace += cache_table[k].size;
  415. break;
  416. }
  417. break;
  418. }
  419. k++;
  420. }
  421. }
  422. }
  423. }
  424. if (new_l1d)
  425. l1d = new_l1d;
  426. if (new_l1i)
  427. l1i = new_l1i;
  428. if (new_l2) {
  429. l2 = new_l2;
  430. #ifdef CONFIG_X86_HT
  431. per_cpu(cpu_llc_id, cpu) = l2_id;
  432. #endif
  433. }
  434. if (new_l3) {
  435. l3 = new_l3;
  436. #ifdef CONFIG_X86_HT
  437. per_cpu(cpu_llc_id, cpu) = l3_id;
  438. #endif
  439. }
  440. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  441. return l2;
  442. }
  443. #ifdef CONFIG_SYSFS
  444. /* pointer to _cpuid4_info array (for each cache leaf) */
  445. static DEFINE_PER_CPU(struct _cpuid4_info *, ici_cpuid4_info);
  446. #define CPUID4_INFO_IDX(x, y) (&((per_cpu(ici_cpuid4_info, x))[y]))
  447. #ifdef CONFIG_SMP
  448. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  449. {
  450. struct _cpuid4_info *this_leaf, *sibling_leaf;
  451. unsigned long num_threads_sharing;
  452. int index_msb, i, sibling;
  453. struct cpuinfo_x86 *c = &cpu_data(cpu);
  454. if ((index == 3) && (c->x86_vendor == X86_VENDOR_AMD)) {
  455. for_each_cpu(i, c->llc_shared_map) {
  456. if (!per_cpu(ici_cpuid4_info, i))
  457. continue;
  458. this_leaf = CPUID4_INFO_IDX(i, index);
  459. for_each_cpu(sibling, c->llc_shared_map) {
  460. if (!cpu_online(sibling))
  461. continue;
  462. set_bit(sibling, this_leaf->shared_cpu_map);
  463. }
  464. }
  465. return;
  466. }
  467. this_leaf = CPUID4_INFO_IDX(cpu, index);
  468. num_threads_sharing = 1 + this_leaf->eax.split.num_threads_sharing;
  469. if (num_threads_sharing == 1)
  470. cpumask_set_cpu(cpu, to_cpumask(this_leaf->shared_cpu_map));
  471. else {
  472. index_msb = get_count_order(num_threads_sharing);
  473. for_each_online_cpu(i) {
  474. if (cpu_data(i).apicid >> index_msb ==
  475. c->apicid >> index_msb) {
  476. cpumask_set_cpu(i,
  477. to_cpumask(this_leaf->shared_cpu_map));
  478. if (i != cpu && per_cpu(ici_cpuid4_info, i)) {
  479. sibling_leaf =
  480. CPUID4_INFO_IDX(i, index);
  481. cpumask_set_cpu(cpu, to_cpumask(
  482. sibling_leaf->shared_cpu_map));
  483. }
  484. }
  485. }
  486. }
  487. }
  488. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  489. {
  490. struct _cpuid4_info *this_leaf, *sibling_leaf;
  491. int sibling;
  492. this_leaf = CPUID4_INFO_IDX(cpu, index);
  493. for_each_cpu(sibling, to_cpumask(this_leaf->shared_cpu_map)) {
  494. sibling_leaf = CPUID4_INFO_IDX(sibling, index);
  495. cpumask_clear_cpu(cpu,
  496. to_cpumask(sibling_leaf->shared_cpu_map));
  497. }
  498. }
  499. #else
  500. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  501. {
  502. }
  503. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  504. {
  505. }
  506. #endif
  507. static void __cpuinit free_cache_attributes(unsigned int cpu)
  508. {
  509. int i;
  510. for (i = 0; i < num_cache_leaves; i++)
  511. cache_remove_shared_cpu_map(cpu, i);
  512. kfree(per_cpu(ici_cpuid4_info, cpu));
  513. per_cpu(ici_cpuid4_info, cpu) = NULL;
  514. }
  515. static int
  516. __cpuinit cpuid4_cache_lookup(int index, struct _cpuid4_info *this_leaf)
  517. {
  518. struct _cpuid4_info_regs *leaf_regs =
  519. (struct _cpuid4_info_regs *)this_leaf;
  520. return cpuid4_cache_lookup_regs(index, leaf_regs);
  521. }
  522. static void __cpuinit get_cpu_leaves(void *_retval)
  523. {
  524. int j, *retval = _retval, cpu = smp_processor_id();
  525. /* Do cpuid and store the results */
  526. for (j = 0; j < num_cache_leaves; j++) {
  527. struct _cpuid4_info *this_leaf;
  528. this_leaf = CPUID4_INFO_IDX(cpu, j);
  529. *retval = cpuid4_cache_lookup(j, this_leaf);
  530. if (unlikely(*retval < 0)) {
  531. int i;
  532. for (i = 0; i < j; i++)
  533. cache_remove_shared_cpu_map(cpu, i);
  534. break;
  535. }
  536. cache_shared_cpu_map_setup(cpu, j);
  537. }
  538. }
  539. static int __cpuinit detect_cache_attributes(unsigned int cpu)
  540. {
  541. int retval;
  542. if (num_cache_leaves == 0)
  543. return -ENOENT;
  544. per_cpu(ici_cpuid4_info, cpu) = kzalloc(
  545. sizeof(struct _cpuid4_info) * num_cache_leaves, GFP_KERNEL);
  546. if (per_cpu(ici_cpuid4_info, cpu) == NULL)
  547. return -ENOMEM;
  548. smp_call_function_single(cpu, get_cpu_leaves, &retval, true);
  549. if (retval) {
  550. kfree(per_cpu(ici_cpuid4_info, cpu));
  551. per_cpu(ici_cpuid4_info, cpu) = NULL;
  552. }
  553. return retval;
  554. }
  555. #include <linux/kobject.h>
  556. #include <linux/sysfs.h>
  557. extern struct sysdev_class cpu_sysdev_class; /* from drivers/base/cpu.c */
  558. /* pointer to kobject for cpuX/cache */
  559. static DEFINE_PER_CPU(struct kobject *, ici_cache_kobject);
  560. struct _index_kobject {
  561. struct kobject kobj;
  562. unsigned int cpu;
  563. unsigned short index;
  564. };
  565. /* pointer to array of kobjects for cpuX/cache/indexY */
  566. static DEFINE_PER_CPU(struct _index_kobject *, ici_index_kobject);
  567. #define INDEX_KOBJECT_PTR(x, y) (&((per_cpu(ici_index_kobject, x))[y]))
  568. #define show_one_plus(file_name, object, val) \
  569. static ssize_t show_##file_name \
  570. (struct _cpuid4_info *this_leaf, char *buf) \
  571. { \
  572. return sprintf(buf, "%lu\n", (unsigned long)this_leaf->object + val); \
  573. }
  574. show_one_plus(level, eax.split.level, 0);
  575. show_one_plus(coherency_line_size, ebx.split.coherency_line_size, 1);
  576. show_one_plus(physical_line_partition, ebx.split.physical_line_partition, 1);
  577. show_one_plus(ways_of_associativity, ebx.split.ways_of_associativity, 1);
  578. show_one_plus(number_of_sets, ecx.split.number_of_sets, 1);
  579. static ssize_t show_size(struct _cpuid4_info *this_leaf, char *buf)
  580. {
  581. return sprintf(buf, "%luK\n", this_leaf->size / 1024);
  582. }
  583. static ssize_t show_shared_cpu_map_func(struct _cpuid4_info *this_leaf,
  584. int type, char *buf)
  585. {
  586. ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
  587. int n = 0;
  588. if (len > 1) {
  589. const struct cpumask *mask;
  590. mask = to_cpumask(this_leaf->shared_cpu_map);
  591. n = type ?
  592. cpulist_scnprintf(buf, len-2, mask) :
  593. cpumask_scnprintf(buf, len-2, mask);
  594. buf[n++] = '\n';
  595. buf[n] = '\0';
  596. }
  597. return n;
  598. }
  599. static inline ssize_t show_shared_cpu_map(struct _cpuid4_info *leaf, char *buf)
  600. {
  601. return show_shared_cpu_map_func(leaf, 0, buf);
  602. }
  603. static inline ssize_t show_shared_cpu_list(struct _cpuid4_info *leaf, char *buf)
  604. {
  605. return show_shared_cpu_map_func(leaf, 1, buf);
  606. }
  607. static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf)
  608. {
  609. switch (this_leaf->eax.split.type) {
  610. case CACHE_TYPE_DATA:
  611. return sprintf(buf, "Data\n");
  612. case CACHE_TYPE_INST:
  613. return sprintf(buf, "Instruction\n");
  614. case CACHE_TYPE_UNIFIED:
  615. return sprintf(buf, "Unified\n");
  616. default:
  617. return sprintf(buf, "Unknown\n");
  618. }
  619. }
  620. #define to_object(k) container_of(k, struct _index_kobject, kobj)
  621. #define to_attr(a) container_of(a, struct _cache_attr, attr)
  622. static ssize_t show_cache_disable(struct _cpuid4_info *this_leaf, char *buf,
  623. unsigned int index)
  624. {
  625. int cpu = cpumask_first(to_cpumask(this_leaf->shared_cpu_map));
  626. int node = cpu_to_node(cpu);
  627. struct pci_dev *dev = node_to_k8_nb_misc(node);
  628. unsigned int reg = 0;
  629. if (!this_leaf->can_disable)
  630. return -EINVAL;
  631. if (!dev)
  632. return -EINVAL;
  633. pci_read_config_dword(dev, 0x1BC + index * 4, &reg);
  634. return sprintf(buf, "%x\n", reg);
  635. }
  636. #define SHOW_CACHE_DISABLE(index) \
  637. static ssize_t \
  638. show_cache_disable_##index(struct _cpuid4_info *this_leaf, char *buf) \
  639. { \
  640. return show_cache_disable(this_leaf, buf, index); \
  641. }
  642. SHOW_CACHE_DISABLE(0)
  643. SHOW_CACHE_DISABLE(1)
  644. static ssize_t store_cache_disable(struct _cpuid4_info *this_leaf,
  645. const char *buf, size_t count, unsigned int index)
  646. {
  647. int cpu = cpumask_first(to_cpumask(this_leaf->shared_cpu_map));
  648. int node = cpu_to_node(cpu);
  649. struct pci_dev *dev = node_to_k8_nb_misc(node);
  650. unsigned long val = 0;
  651. unsigned int scrubber = 0;
  652. if (!this_leaf->can_disable)
  653. return -EINVAL;
  654. if (!capable(CAP_SYS_ADMIN))
  655. return -EPERM;
  656. if (!dev)
  657. return -EINVAL;
  658. if (strict_strtoul(buf, 10, &val) < 0)
  659. return -EINVAL;
  660. val |= 0xc0000000;
  661. pci_read_config_dword(dev, 0x58, &scrubber);
  662. scrubber &= ~0x1f000000;
  663. pci_write_config_dword(dev, 0x58, scrubber);
  664. pci_write_config_dword(dev, 0x1BC + index * 4, val & ~0x40000000);
  665. wbinvd();
  666. pci_write_config_dword(dev, 0x1BC + index * 4, val);
  667. return count;
  668. }
  669. #define STORE_CACHE_DISABLE(index) \
  670. static ssize_t \
  671. store_cache_disable_##index(struct _cpuid4_info *this_leaf, \
  672. const char *buf, size_t count) \
  673. { \
  674. return store_cache_disable(this_leaf, buf, count, index); \
  675. }
  676. STORE_CACHE_DISABLE(0)
  677. STORE_CACHE_DISABLE(1)
  678. struct _cache_attr {
  679. struct attribute attr;
  680. ssize_t (*show)(struct _cpuid4_info *, char *);
  681. ssize_t (*store)(struct _cpuid4_info *, const char *, size_t count);
  682. };
  683. #define define_one_ro(_name) \
  684. static struct _cache_attr _name = \
  685. __ATTR(_name, 0444, show_##_name, NULL)
  686. define_one_ro(level);
  687. define_one_ro(type);
  688. define_one_ro(coherency_line_size);
  689. define_one_ro(physical_line_partition);
  690. define_one_ro(ways_of_associativity);
  691. define_one_ro(number_of_sets);
  692. define_one_ro(size);
  693. define_one_ro(shared_cpu_map);
  694. define_one_ro(shared_cpu_list);
  695. static struct _cache_attr cache_disable_0 = __ATTR(cache_disable_0, 0644,
  696. show_cache_disable_0, store_cache_disable_0);
  697. static struct _cache_attr cache_disable_1 = __ATTR(cache_disable_1, 0644,
  698. show_cache_disable_1, store_cache_disable_1);
  699. static struct attribute *default_attrs[] = {
  700. &type.attr,
  701. &level.attr,
  702. &coherency_line_size.attr,
  703. &physical_line_partition.attr,
  704. &ways_of_associativity.attr,
  705. &number_of_sets.attr,
  706. &size.attr,
  707. &shared_cpu_map.attr,
  708. &shared_cpu_list.attr,
  709. &cache_disable_0.attr,
  710. &cache_disable_1.attr,
  711. NULL
  712. };
  713. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  714. {
  715. struct _cache_attr *fattr = to_attr(attr);
  716. struct _index_kobject *this_leaf = to_object(kobj);
  717. ssize_t ret;
  718. ret = fattr->show ?
  719. fattr->show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  720. buf) :
  721. 0;
  722. return ret;
  723. }
  724. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  725. const char *buf, size_t count)
  726. {
  727. struct _cache_attr *fattr = to_attr(attr);
  728. struct _index_kobject *this_leaf = to_object(kobj);
  729. ssize_t ret;
  730. ret = fattr->store ?
  731. fattr->store(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  732. buf, count) :
  733. 0;
  734. return ret;
  735. }
  736. static struct sysfs_ops sysfs_ops = {
  737. .show = show,
  738. .store = store,
  739. };
  740. static struct kobj_type ktype_cache = {
  741. .sysfs_ops = &sysfs_ops,
  742. .default_attrs = default_attrs,
  743. };
  744. static struct kobj_type ktype_percpu_entry = {
  745. .sysfs_ops = &sysfs_ops,
  746. };
  747. static void __cpuinit cpuid4_cache_sysfs_exit(unsigned int cpu)
  748. {
  749. kfree(per_cpu(ici_cache_kobject, cpu));
  750. kfree(per_cpu(ici_index_kobject, cpu));
  751. per_cpu(ici_cache_kobject, cpu) = NULL;
  752. per_cpu(ici_index_kobject, cpu) = NULL;
  753. free_cache_attributes(cpu);
  754. }
  755. static int __cpuinit cpuid4_cache_sysfs_init(unsigned int cpu)
  756. {
  757. int err;
  758. if (num_cache_leaves == 0)
  759. return -ENOENT;
  760. err = detect_cache_attributes(cpu);
  761. if (err)
  762. return err;
  763. /* Allocate all required memory */
  764. per_cpu(ici_cache_kobject, cpu) =
  765. kzalloc(sizeof(struct kobject), GFP_KERNEL);
  766. if (unlikely(per_cpu(ici_cache_kobject, cpu) == NULL))
  767. goto err_out;
  768. per_cpu(ici_index_kobject, cpu) = kzalloc(
  769. sizeof(struct _index_kobject) * num_cache_leaves, GFP_KERNEL);
  770. if (unlikely(per_cpu(ici_index_kobject, cpu) == NULL))
  771. goto err_out;
  772. return 0;
  773. err_out:
  774. cpuid4_cache_sysfs_exit(cpu);
  775. return -ENOMEM;
  776. }
  777. static DECLARE_BITMAP(cache_dev_map, NR_CPUS);
  778. /* Add/Remove cache interface for CPU device */
  779. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  780. {
  781. unsigned int cpu = sys_dev->id;
  782. unsigned long i, j;
  783. struct _index_kobject *this_object;
  784. int retval;
  785. retval = cpuid4_cache_sysfs_init(cpu);
  786. if (unlikely(retval < 0))
  787. return retval;
  788. retval = kobject_init_and_add(per_cpu(ici_cache_kobject, cpu),
  789. &ktype_percpu_entry,
  790. &sys_dev->kobj, "%s", "cache");
  791. if (retval < 0) {
  792. cpuid4_cache_sysfs_exit(cpu);
  793. return retval;
  794. }
  795. for (i = 0; i < num_cache_leaves; i++) {
  796. this_object = INDEX_KOBJECT_PTR(cpu, i);
  797. this_object->cpu = cpu;
  798. this_object->index = i;
  799. retval = kobject_init_and_add(&(this_object->kobj),
  800. &ktype_cache,
  801. per_cpu(ici_cache_kobject, cpu),
  802. "index%1lu", i);
  803. if (unlikely(retval)) {
  804. for (j = 0; j < i; j++)
  805. kobject_put(&(INDEX_KOBJECT_PTR(cpu, j)->kobj));
  806. kobject_put(per_cpu(ici_cache_kobject, cpu));
  807. cpuid4_cache_sysfs_exit(cpu);
  808. return retval;
  809. }
  810. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  811. }
  812. cpumask_set_cpu(cpu, to_cpumask(cache_dev_map));
  813. kobject_uevent(per_cpu(ici_cache_kobject, cpu), KOBJ_ADD);
  814. return 0;
  815. }
  816. static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
  817. {
  818. unsigned int cpu = sys_dev->id;
  819. unsigned long i;
  820. if (per_cpu(ici_cpuid4_info, cpu) == NULL)
  821. return;
  822. if (!cpumask_test_cpu(cpu, to_cpumask(cache_dev_map)))
  823. return;
  824. cpumask_clear_cpu(cpu, to_cpumask(cache_dev_map));
  825. for (i = 0; i < num_cache_leaves; i++)
  826. kobject_put(&(INDEX_KOBJECT_PTR(cpu, i)->kobj));
  827. kobject_put(per_cpu(ici_cache_kobject, cpu));
  828. cpuid4_cache_sysfs_exit(cpu);
  829. }
  830. static int __cpuinit cacheinfo_cpu_callback(struct notifier_block *nfb,
  831. unsigned long action, void *hcpu)
  832. {
  833. unsigned int cpu = (unsigned long)hcpu;
  834. struct sys_device *sys_dev;
  835. sys_dev = get_cpu_sysdev(cpu);
  836. switch (action) {
  837. case CPU_ONLINE:
  838. case CPU_ONLINE_FROZEN:
  839. cache_add_dev(sys_dev);
  840. break;
  841. case CPU_DEAD:
  842. case CPU_DEAD_FROZEN:
  843. cache_remove_dev(sys_dev);
  844. break;
  845. }
  846. return NOTIFY_OK;
  847. }
  848. static struct notifier_block __cpuinitdata cacheinfo_cpu_notifier = {
  849. .notifier_call = cacheinfo_cpu_callback,
  850. };
  851. static int __cpuinit cache_sysfs_init(void)
  852. {
  853. int i;
  854. if (num_cache_leaves == 0)
  855. return 0;
  856. for_each_online_cpu(i) {
  857. int err;
  858. struct sys_device *sys_dev = get_cpu_sysdev(i);
  859. err = cache_add_dev(sys_dev);
  860. if (err)
  861. return err;
  862. }
  863. register_hotcpu_notifier(&cacheinfo_cpu_notifier);
  864. return 0;
  865. }
  866. device_initcall(cache_sysfs_init);
  867. #endif