intel_cacheinfo.c 26 KB

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