intel_cacheinfo.c 27 KB

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