intel_cacheinfo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 : 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 <asm/processor.h>
  16. #include <asm/smp.h>
  17. #define LVL_1_INST 1
  18. #define LVL_1_DATA 2
  19. #define LVL_2 3
  20. #define LVL_3 4
  21. #define LVL_TRACE 5
  22. struct _cache_table
  23. {
  24. unsigned char descriptor;
  25. char cache_type;
  26. short size;
  27. };
  28. /* all the cache descriptor types we care about (no TLB or trace cache entries) */
  29. static struct _cache_table cache_table[] __cpuinitdata =
  30. {
  31. { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */
  32. { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */
  33. { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */
  34. { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */
  35. { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  36. { 0x23, LVL_3, 1024 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  37. { 0x25, LVL_3, 2048 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  38. { 0x29, LVL_3, 4096 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  39. { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */
  40. { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */
  41. { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  42. { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  43. { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line size */
  44. { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  45. { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  46. { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  47. { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */
  48. { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */
  49. { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */
  50. { 0x44, LVL_2, 1024 }, /* 4-way set assoc, 32 byte line size */
  51. { 0x45, LVL_2, 2048 }, /* 4-way set assoc, 32 byte line size */
  52. { 0x46, LVL_3, 4096 }, /* 4-way set assoc, 64 byte line size */
  53. { 0x47, LVL_3, 8192 }, /* 8-way set assoc, 64 byte line size */
  54. { 0x49, LVL_3, 4096 }, /* 16-way set assoc, 64 byte line size */
  55. { 0x4a, LVL_3, 6144 }, /* 12-way set assoc, 64 byte line size */
  56. { 0x4b, LVL_3, 8192 }, /* 16-way set assoc, 64 byte line size */
  57. { 0x4c, LVL_3, 12288 }, /* 12-way set assoc, 64 byte line size */
  58. { 0x4d, LVL_3, 16384 }, /* 16-way set assoc, 64 byte line size */
  59. { 0x60, LVL_1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  60. { 0x66, LVL_1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  61. { 0x67, LVL_1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  62. { 0x68, LVL_1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  63. { 0x70, LVL_TRACE, 12 }, /* 8-way set assoc */
  64. { 0x71, LVL_TRACE, 16 }, /* 8-way set assoc */
  65. { 0x72, LVL_TRACE, 32 }, /* 8-way set assoc */
  66. { 0x73, LVL_TRACE, 64 }, /* 8-way set assoc */
  67. { 0x78, LVL_2, 1024 }, /* 4-way set assoc, 64 byte line size */
  68. { 0x79, LVL_2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  69. { 0x7a, LVL_2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  70. { 0x7b, LVL_2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  71. { 0x7c, LVL_2, 1024 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  72. { 0x7d, LVL_2, 2048 }, /* 8-way set assoc, 64 byte line size */
  73. { 0x7f, LVL_2, 512 }, /* 2-way set assoc, 64 byte line size */
  74. { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */
  75. { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */
  76. { 0x84, LVL_2, 1024 }, /* 8-way set assoc, 32 byte line size */
  77. { 0x85, LVL_2, 2048 }, /* 8-way set assoc, 32 byte line size */
  78. { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */
  79. { 0x87, LVL_2, 1024 }, /* 8-way set assoc, 64 byte line size */
  80. { 0x00, 0, 0}
  81. };
  82. enum _cache_type
  83. {
  84. CACHE_TYPE_NULL = 0,
  85. CACHE_TYPE_DATA = 1,
  86. CACHE_TYPE_INST = 2,
  87. CACHE_TYPE_UNIFIED = 3
  88. };
  89. union _cpuid4_leaf_eax {
  90. struct {
  91. enum _cache_type type:5;
  92. unsigned int level:3;
  93. unsigned int is_self_initializing:1;
  94. unsigned int is_fully_associative:1;
  95. unsigned int reserved:4;
  96. unsigned int num_threads_sharing:12;
  97. unsigned int num_cores_on_die:6;
  98. } split;
  99. u32 full;
  100. };
  101. union _cpuid4_leaf_ebx {
  102. struct {
  103. unsigned int coherency_line_size:12;
  104. unsigned int physical_line_partition:10;
  105. unsigned int ways_of_associativity:10;
  106. } split;
  107. u32 full;
  108. };
  109. union _cpuid4_leaf_ecx {
  110. struct {
  111. unsigned int number_of_sets:32;
  112. } split;
  113. u32 full;
  114. };
  115. struct _cpuid4_info {
  116. union _cpuid4_leaf_eax eax;
  117. union _cpuid4_leaf_ebx ebx;
  118. union _cpuid4_leaf_ecx ecx;
  119. unsigned long size;
  120. cpumask_t shared_cpu_map;
  121. };
  122. unsigned short num_cache_leaves;
  123. /* AMD doesn't have CPUID4. Emulate it here to report the same
  124. information to the user. This makes some assumptions about the machine:
  125. No L3, L2 not shared, no SMT etc. that is currently true on AMD CPUs.
  126. In theory the TLBs could be reported as fake type (they are in "dummy").
  127. Maybe later */
  128. union l1_cache {
  129. struct {
  130. unsigned line_size : 8;
  131. unsigned lines_per_tag : 8;
  132. unsigned assoc : 8;
  133. unsigned size_in_kb : 8;
  134. };
  135. unsigned val;
  136. };
  137. union l2_cache {
  138. struct {
  139. unsigned line_size : 8;
  140. unsigned lines_per_tag : 4;
  141. unsigned assoc : 4;
  142. unsigned size_in_kb : 16;
  143. };
  144. unsigned val;
  145. };
  146. static const unsigned short assocs[] = {
  147. [1] = 1, [2] = 2, [4] = 4, [6] = 8,
  148. [8] = 16,
  149. [0xf] = 0xffff // ??
  150. };
  151. static const unsigned char levels[] = { 1, 1, 2 };
  152. static const unsigned char types[] = { 1, 2, 3 };
  153. static void __cpuinit amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  154. union _cpuid4_leaf_ebx *ebx,
  155. union _cpuid4_leaf_ecx *ecx)
  156. {
  157. unsigned dummy;
  158. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  159. union l1_cache l1i, l1d;
  160. union l2_cache l2;
  161. eax->full = 0;
  162. ebx->full = 0;
  163. ecx->full = 0;
  164. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  165. cpuid(0x80000006, &dummy, &dummy, &l2.val, &dummy);
  166. if (leaf > 2 || !l1d.val || !l1i.val || !l2.val)
  167. return;
  168. eax->split.is_self_initializing = 1;
  169. eax->split.type = types[leaf];
  170. eax->split.level = levels[leaf];
  171. eax->split.num_threads_sharing = 0;
  172. eax->split.num_cores_on_die = current_cpu_data.x86_max_cores - 1;
  173. if (leaf <= 1) {
  174. union l1_cache *l1 = leaf == 0 ? &l1d : &l1i;
  175. assoc = l1->assoc;
  176. line_size = l1->line_size;
  177. lines_per_tag = l1->lines_per_tag;
  178. size_in_kb = l1->size_in_kb;
  179. } else {
  180. assoc = l2.assoc;
  181. line_size = l2.line_size;
  182. lines_per_tag = l2.lines_per_tag;
  183. /* cpu_data has errata corrections for K7 applied */
  184. size_in_kb = current_cpu_data.x86_cache_size;
  185. }
  186. if (assoc == 0xf)
  187. eax->split.is_fully_associative = 1;
  188. ebx->split.coherency_line_size = line_size - 1;
  189. ebx->split.ways_of_associativity = assocs[assoc] - 1;
  190. ebx->split.physical_line_partition = lines_per_tag - 1;
  191. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  192. (ebx->split.ways_of_associativity + 1) - 1;
  193. }
  194. static int __cpuinit cpuid4_cache_lookup(int index, struct _cpuid4_info *this_leaf)
  195. {
  196. union _cpuid4_leaf_eax eax;
  197. union _cpuid4_leaf_ebx ebx;
  198. union _cpuid4_leaf_ecx ecx;
  199. unsigned edx;
  200. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  201. amd_cpuid4(index, &eax, &ebx, &ecx);
  202. else
  203. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  204. if (eax.split.type == CACHE_TYPE_NULL)
  205. return -EIO; /* better error ? */
  206. this_leaf->eax = eax;
  207. this_leaf->ebx = ebx;
  208. this_leaf->ecx = ecx;
  209. this_leaf->size = (ecx.split.number_of_sets + 1) *
  210. (ebx.split.coherency_line_size + 1) *
  211. (ebx.split.physical_line_partition + 1) *
  212. (ebx.split.ways_of_associativity + 1);
  213. return 0;
  214. }
  215. /* will only be called once; __init is safe here */
  216. static int __init find_num_cache_leaves(void)
  217. {
  218. unsigned int eax, ebx, ecx, edx;
  219. union _cpuid4_leaf_eax cache_eax;
  220. int i = -1;
  221. do {
  222. ++i;
  223. /* Do cpuid(4) loop to find out num_cache_leaves */
  224. cpuid_count(4, i, &eax, &ebx, &ecx, &edx);
  225. cache_eax.full = eax;
  226. } while (cache_eax.split.type != CACHE_TYPE_NULL);
  227. return i;
  228. }
  229. unsigned int __cpuinit init_intel_cacheinfo(struct cpuinfo_x86 *c)
  230. {
  231. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0; /* Cache sizes */
  232. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  233. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  234. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  235. #ifdef CONFIG_X86_HT
  236. unsigned int cpu = (c == &boot_cpu_data) ? 0 : (c - cpu_data);
  237. #endif
  238. if (c->cpuid_level > 3) {
  239. static int is_initialized;
  240. if (is_initialized == 0) {
  241. /* Init num_cache_leaves from boot CPU */
  242. num_cache_leaves = find_num_cache_leaves();
  243. is_initialized++;
  244. }
  245. /*
  246. * Whenever possible use cpuid(4), deterministic cache
  247. * parameters cpuid leaf to find the cache details
  248. */
  249. for (i = 0; i < num_cache_leaves; i++) {
  250. struct _cpuid4_info this_leaf;
  251. int retval;
  252. retval = cpuid4_cache_lookup(i, &this_leaf);
  253. if (retval >= 0) {
  254. switch(this_leaf.eax.split.level) {
  255. case 1:
  256. if (this_leaf.eax.split.type ==
  257. CACHE_TYPE_DATA)
  258. new_l1d = this_leaf.size/1024;
  259. else if (this_leaf.eax.split.type ==
  260. CACHE_TYPE_INST)
  261. new_l1i = this_leaf.size/1024;
  262. break;
  263. case 2:
  264. new_l2 = this_leaf.size/1024;
  265. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  266. index_msb = get_count_order(num_threads_sharing);
  267. l2_id = c->apicid >> index_msb;
  268. break;
  269. case 3:
  270. new_l3 = this_leaf.size/1024;
  271. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  272. index_msb = get_count_order(num_threads_sharing);
  273. l3_id = c->apicid >> index_msb;
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. }
  281. /*
  282. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  283. * trace cache
  284. */
  285. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  286. /* supports eax=2 call */
  287. int i, j, n;
  288. int regs[4];
  289. unsigned char *dp = (unsigned char *)regs;
  290. int only_trace = 0;
  291. if (num_cache_leaves != 0 && c->x86 == 15)
  292. only_trace = 1;
  293. /* Number of times to iterate */
  294. n = cpuid_eax(2) & 0xFF;
  295. for ( i = 0 ; i < n ; i++ ) {
  296. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  297. /* If bit 31 is set, this is an unknown format */
  298. for ( j = 0 ; j < 3 ; j++ ) {
  299. if ( regs[j] < 0 ) regs[j] = 0;
  300. }
  301. /* Byte 0 is level count, not a descriptor */
  302. for ( j = 1 ; j < 16 ; j++ ) {
  303. unsigned char des = dp[j];
  304. unsigned char k = 0;
  305. /* look up this descriptor in the table */
  306. while (cache_table[k].descriptor != 0)
  307. {
  308. if (cache_table[k].descriptor == des) {
  309. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  310. break;
  311. switch (cache_table[k].cache_type) {
  312. case LVL_1_INST:
  313. l1i += cache_table[k].size;
  314. break;
  315. case LVL_1_DATA:
  316. l1d += cache_table[k].size;
  317. break;
  318. case LVL_2:
  319. l2 += cache_table[k].size;
  320. break;
  321. case LVL_3:
  322. l3 += cache_table[k].size;
  323. break;
  324. case LVL_TRACE:
  325. trace += cache_table[k].size;
  326. break;
  327. }
  328. break;
  329. }
  330. k++;
  331. }
  332. }
  333. }
  334. }
  335. if (new_l1d)
  336. l1d = new_l1d;
  337. if (new_l1i)
  338. l1i = new_l1i;
  339. if (new_l2) {
  340. l2 = new_l2;
  341. #ifdef CONFIG_X86_HT
  342. cpu_llc_id[cpu] = l2_id;
  343. #endif
  344. }
  345. if (new_l3) {
  346. l3 = new_l3;
  347. #ifdef CONFIG_X86_HT
  348. cpu_llc_id[cpu] = l3_id;
  349. #endif
  350. }
  351. if (trace)
  352. printk (KERN_INFO "CPU: Trace cache: %dK uops", trace);
  353. else if ( l1i )
  354. printk (KERN_INFO "CPU: L1 I cache: %dK", l1i);
  355. if (l1d)
  356. printk(", L1 D cache: %dK\n", l1d);
  357. else
  358. printk("\n");
  359. if (l2)
  360. printk(KERN_INFO "CPU: L2 cache: %dK\n", l2);
  361. if (l3)
  362. printk(KERN_INFO "CPU: L3 cache: %dK\n", l3);
  363. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  364. return l2;
  365. }
  366. /* pointer to _cpuid4_info array (for each cache leaf) */
  367. static struct _cpuid4_info *cpuid4_info[NR_CPUS];
  368. #define CPUID4_INFO_IDX(x,y) (&((cpuid4_info[x])[y]))
  369. #ifdef CONFIG_SMP
  370. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  371. {
  372. struct _cpuid4_info *this_leaf, *sibling_leaf;
  373. unsigned long num_threads_sharing;
  374. int index_msb, i;
  375. struct cpuinfo_x86 *c = cpu_data;
  376. this_leaf = CPUID4_INFO_IDX(cpu, index);
  377. num_threads_sharing = 1 + this_leaf->eax.split.num_threads_sharing;
  378. if (num_threads_sharing == 1)
  379. cpu_set(cpu, this_leaf->shared_cpu_map);
  380. else {
  381. index_msb = get_count_order(num_threads_sharing);
  382. for_each_online_cpu(i) {
  383. if (c[i].apicid >> index_msb ==
  384. c[cpu].apicid >> index_msb) {
  385. cpu_set(i, this_leaf->shared_cpu_map);
  386. if (i != cpu && cpuid4_info[i]) {
  387. sibling_leaf = CPUID4_INFO_IDX(i, index);
  388. cpu_set(cpu, sibling_leaf->shared_cpu_map);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  395. {
  396. struct _cpuid4_info *this_leaf, *sibling_leaf;
  397. int sibling;
  398. this_leaf = CPUID4_INFO_IDX(cpu, index);
  399. for_each_cpu_mask(sibling, this_leaf->shared_cpu_map) {
  400. sibling_leaf = CPUID4_INFO_IDX(sibling, index);
  401. cpu_clear(cpu, sibling_leaf->shared_cpu_map);
  402. }
  403. }
  404. #else
  405. static void __init cache_shared_cpu_map_setup(unsigned int cpu, int index) {}
  406. static void __init cache_remove_shared_cpu_map(unsigned int cpu, int index) {}
  407. #endif
  408. static void free_cache_attributes(unsigned int cpu)
  409. {
  410. kfree(cpuid4_info[cpu]);
  411. cpuid4_info[cpu] = NULL;
  412. }
  413. static int __cpuinit detect_cache_attributes(unsigned int cpu)
  414. {
  415. struct _cpuid4_info *this_leaf;
  416. unsigned long j;
  417. int retval;
  418. cpumask_t oldmask;
  419. if (num_cache_leaves == 0)
  420. return -ENOENT;
  421. cpuid4_info[cpu] = kmalloc(
  422. sizeof(struct _cpuid4_info) * num_cache_leaves, GFP_KERNEL);
  423. if (unlikely(cpuid4_info[cpu] == NULL))
  424. return -ENOMEM;
  425. memset(cpuid4_info[cpu], 0,
  426. sizeof(struct _cpuid4_info) * num_cache_leaves);
  427. oldmask = current->cpus_allowed;
  428. retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
  429. if (retval)
  430. goto out;
  431. /* Do cpuid and store the results */
  432. retval = 0;
  433. for (j = 0; j < num_cache_leaves; j++) {
  434. this_leaf = CPUID4_INFO_IDX(cpu, j);
  435. retval = cpuid4_cache_lookup(j, this_leaf);
  436. if (unlikely(retval < 0))
  437. break;
  438. cache_shared_cpu_map_setup(cpu, j);
  439. }
  440. set_cpus_allowed(current, oldmask);
  441. out:
  442. if (retval)
  443. free_cache_attributes(cpu);
  444. return retval;
  445. }
  446. #ifdef CONFIG_SYSFS
  447. #include <linux/kobject.h>
  448. #include <linux/sysfs.h>
  449. extern struct sysdev_class cpu_sysdev_class; /* from drivers/base/cpu.c */
  450. /* pointer to kobject for cpuX/cache */
  451. static struct kobject * cache_kobject[NR_CPUS];
  452. struct _index_kobject {
  453. struct kobject kobj;
  454. unsigned int cpu;
  455. unsigned short index;
  456. };
  457. /* pointer to array of kobjects for cpuX/cache/indexY */
  458. static struct _index_kobject *index_kobject[NR_CPUS];
  459. #define INDEX_KOBJECT_PTR(x,y) (&((index_kobject[x])[y]))
  460. #define show_one_plus(file_name, object, val) \
  461. static ssize_t show_##file_name \
  462. (struct _cpuid4_info *this_leaf, char *buf) \
  463. { \
  464. return sprintf (buf, "%lu\n", (unsigned long)this_leaf->object + val); \
  465. }
  466. show_one_plus(level, eax.split.level, 0);
  467. show_one_plus(coherency_line_size, ebx.split.coherency_line_size, 1);
  468. show_one_plus(physical_line_partition, ebx.split.physical_line_partition, 1);
  469. show_one_plus(ways_of_associativity, ebx.split.ways_of_associativity, 1);
  470. show_one_plus(number_of_sets, ecx.split.number_of_sets, 1);
  471. static ssize_t show_size(struct _cpuid4_info *this_leaf, char *buf)
  472. {
  473. return sprintf (buf, "%luK\n", this_leaf->size / 1024);
  474. }
  475. static ssize_t show_shared_cpu_map(struct _cpuid4_info *this_leaf, char *buf)
  476. {
  477. char mask_str[NR_CPUS];
  478. cpumask_scnprintf(mask_str, NR_CPUS, this_leaf->shared_cpu_map);
  479. return sprintf(buf, "%s\n", mask_str);
  480. }
  481. static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf) {
  482. switch(this_leaf->eax.split.type) {
  483. case CACHE_TYPE_DATA:
  484. return sprintf(buf, "Data\n");
  485. break;
  486. case CACHE_TYPE_INST:
  487. return sprintf(buf, "Instruction\n");
  488. break;
  489. case CACHE_TYPE_UNIFIED:
  490. return sprintf(buf, "Unified\n");
  491. break;
  492. default:
  493. return sprintf(buf, "Unknown\n");
  494. break;
  495. }
  496. }
  497. struct _cache_attr {
  498. struct attribute attr;
  499. ssize_t (*show)(struct _cpuid4_info *, char *);
  500. ssize_t (*store)(struct _cpuid4_info *, const char *, size_t count);
  501. };
  502. #define define_one_ro(_name) \
  503. static struct _cache_attr _name = \
  504. __ATTR(_name, 0444, show_##_name, NULL)
  505. define_one_ro(level);
  506. define_one_ro(type);
  507. define_one_ro(coherency_line_size);
  508. define_one_ro(physical_line_partition);
  509. define_one_ro(ways_of_associativity);
  510. define_one_ro(number_of_sets);
  511. define_one_ro(size);
  512. define_one_ro(shared_cpu_map);
  513. static struct attribute * default_attrs[] = {
  514. &type.attr,
  515. &level.attr,
  516. &coherency_line_size.attr,
  517. &physical_line_partition.attr,
  518. &ways_of_associativity.attr,
  519. &number_of_sets.attr,
  520. &size.attr,
  521. &shared_cpu_map.attr,
  522. NULL
  523. };
  524. #define to_object(k) container_of(k, struct _index_kobject, kobj)
  525. #define to_attr(a) container_of(a, struct _cache_attr, attr)
  526. static ssize_t show(struct kobject * kobj, struct attribute * attr, char * buf)
  527. {
  528. struct _cache_attr *fattr = to_attr(attr);
  529. struct _index_kobject *this_leaf = to_object(kobj);
  530. ssize_t ret;
  531. ret = fattr->show ?
  532. fattr->show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  533. buf) :
  534. 0;
  535. return ret;
  536. }
  537. static ssize_t store(struct kobject * kobj, struct attribute * attr,
  538. const char * buf, size_t count)
  539. {
  540. return 0;
  541. }
  542. static struct sysfs_ops sysfs_ops = {
  543. .show = show,
  544. .store = store,
  545. };
  546. static struct kobj_type ktype_cache = {
  547. .sysfs_ops = &sysfs_ops,
  548. .default_attrs = default_attrs,
  549. };
  550. static struct kobj_type ktype_percpu_entry = {
  551. .sysfs_ops = &sysfs_ops,
  552. };
  553. static void cpuid4_cache_sysfs_exit(unsigned int cpu)
  554. {
  555. kfree(cache_kobject[cpu]);
  556. kfree(index_kobject[cpu]);
  557. cache_kobject[cpu] = NULL;
  558. index_kobject[cpu] = NULL;
  559. free_cache_attributes(cpu);
  560. }
  561. static int __cpuinit cpuid4_cache_sysfs_init(unsigned int cpu)
  562. {
  563. if (num_cache_leaves == 0)
  564. return -ENOENT;
  565. detect_cache_attributes(cpu);
  566. if (cpuid4_info[cpu] == NULL)
  567. return -ENOENT;
  568. /* Allocate all required memory */
  569. cache_kobject[cpu] = kmalloc(sizeof(struct kobject), GFP_KERNEL);
  570. if (unlikely(cache_kobject[cpu] == NULL))
  571. goto err_out;
  572. memset(cache_kobject[cpu], 0, sizeof(struct kobject));
  573. index_kobject[cpu] = kmalloc(
  574. sizeof(struct _index_kobject ) * num_cache_leaves, GFP_KERNEL);
  575. if (unlikely(index_kobject[cpu] == NULL))
  576. goto err_out;
  577. memset(index_kobject[cpu], 0,
  578. sizeof(struct _index_kobject) * num_cache_leaves);
  579. return 0;
  580. err_out:
  581. cpuid4_cache_sysfs_exit(cpu);
  582. return -ENOMEM;
  583. }
  584. /* Add/Remove cache interface for CPU device */
  585. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  586. {
  587. unsigned int cpu = sys_dev->id;
  588. unsigned long i, j;
  589. struct _index_kobject *this_object;
  590. int retval = 0;
  591. retval = cpuid4_cache_sysfs_init(cpu);
  592. if (unlikely(retval < 0))
  593. return retval;
  594. cache_kobject[cpu]->parent = &sys_dev->kobj;
  595. kobject_set_name(cache_kobject[cpu], "%s", "cache");
  596. cache_kobject[cpu]->ktype = &ktype_percpu_entry;
  597. retval = kobject_register(cache_kobject[cpu]);
  598. for (i = 0; i < num_cache_leaves; i++) {
  599. this_object = INDEX_KOBJECT_PTR(cpu,i);
  600. this_object->cpu = cpu;
  601. this_object->index = i;
  602. this_object->kobj.parent = cache_kobject[cpu];
  603. kobject_set_name(&(this_object->kobj), "index%1lu", i);
  604. this_object->kobj.ktype = &ktype_cache;
  605. retval = kobject_register(&(this_object->kobj));
  606. if (unlikely(retval)) {
  607. for (j = 0; j < i; j++) {
  608. kobject_unregister(
  609. &(INDEX_KOBJECT_PTR(cpu,j)->kobj));
  610. }
  611. kobject_unregister(cache_kobject[cpu]);
  612. cpuid4_cache_sysfs_exit(cpu);
  613. break;
  614. }
  615. }
  616. return retval;
  617. }
  618. static void __cpuexit cache_remove_dev(struct sys_device * sys_dev)
  619. {
  620. unsigned int cpu = sys_dev->id;
  621. unsigned long i;
  622. for (i = 0; i < num_cache_leaves; i++) {
  623. cache_remove_shared_cpu_map(cpu, i);
  624. kobject_unregister(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
  625. }
  626. kobject_unregister(cache_kobject[cpu]);
  627. cpuid4_cache_sysfs_exit(cpu);
  628. return;
  629. }
  630. static int __cpuinit cacheinfo_cpu_callback(struct notifier_block *nfb,
  631. unsigned long action, void *hcpu)
  632. {
  633. unsigned int cpu = (unsigned long)hcpu;
  634. struct sys_device *sys_dev;
  635. sys_dev = get_cpu_sysdev(cpu);
  636. switch (action) {
  637. case CPU_ONLINE:
  638. cache_add_dev(sys_dev);
  639. break;
  640. case CPU_DEAD:
  641. cache_remove_dev(sys_dev);
  642. break;
  643. }
  644. return NOTIFY_OK;
  645. }
  646. static struct notifier_block __cpuinitdata cacheinfo_cpu_notifier =
  647. {
  648. .notifier_call = cacheinfo_cpu_callback,
  649. };
  650. static int __cpuinit cache_sysfs_init(void)
  651. {
  652. int i;
  653. if (num_cache_leaves == 0)
  654. return 0;
  655. register_hotcpu_notifier(&cacheinfo_cpu_notifier);
  656. for_each_online_cpu(i) {
  657. cacheinfo_cpu_callback(&cacheinfo_cpu_notifier, CPU_ONLINE,
  658. (void *)(long)i);
  659. }
  660. return 0;
  661. }
  662. device_initcall(cache_sysfs_init);
  663. #endif