intel_cacheinfo.c 27 KB

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