intel_cacheinfo.c 27 KB

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