intel_cacheinfo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 <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. 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. union l3_cache {
  147. struct {
  148. unsigned line_size : 8;
  149. unsigned lines_per_tag : 4;
  150. unsigned assoc : 4;
  151. unsigned res : 2;
  152. unsigned size_encoded : 14;
  153. };
  154. unsigned val;
  155. };
  156. static const unsigned short assocs[] = {
  157. [1] = 1, [2] = 2, [4] = 4, [6] = 8,
  158. [8] = 16, [0xa] = 32, [0xb] = 48,
  159. [0xc] = 64,
  160. [0xf] = 0xffff // ??
  161. };
  162. static const unsigned char levels[] = { 1, 1, 2, 3 };
  163. static const unsigned char types[] = { 1, 2, 3, 3 };
  164. static void __cpuinit amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  165. union _cpuid4_leaf_ebx *ebx,
  166. union _cpuid4_leaf_ecx *ecx)
  167. {
  168. unsigned dummy;
  169. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  170. union l1_cache l1i, l1d;
  171. union l2_cache l2;
  172. union l3_cache l3;
  173. union l1_cache *l1 = &l1d;
  174. eax->full = 0;
  175. ebx->full = 0;
  176. ecx->full = 0;
  177. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  178. cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val);
  179. switch (leaf) {
  180. case 1:
  181. l1 = &l1i;
  182. case 0:
  183. if (!l1->val)
  184. return;
  185. assoc = l1->assoc;
  186. line_size = l1->line_size;
  187. lines_per_tag = l1->lines_per_tag;
  188. size_in_kb = l1->size_in_kb;
  189. break;
  190. case 2:
  191. if (!l2.val)
  192. return;
  193. assoc = l2.assoc;
  194. line_size = l2.line_size;
  195. lines_per_tag = l2.lines_per_tag;
  196. /* cpu_data has errata corrections for K7 applied */
  197. size_in_kb = current_cpu_data.x86_cache_size;
  198. break;
  199. case 3:
  200. if (!l3.val)
  201. return;
  202. assoc = l3.assoc;
  203. line_size = l3.line_size;
  204. lines_per_tag = l3.lines_per_tag;
  205. size_in_kb = l3.size_encoded * 512;
  206. break;
  207. default:
  208. return;
  209. }
  210. eax->split.is_self_initializing = 1;
  211. eax->split.type = types[leaf];
  212. eax->split.level = levels[leaf];
  213. if (leaf == 3)
  214. eax->split.num_threads_sharing = current_cpu_data.x86_max_cores - 1;
  215. else
  216. eax->split.num_threads_sharing = 0;
  217. eax->split.num_cores_on_die = current_cpu_data.x86_max_cores - 1;
  218. if (assoc == 0xf)
  219. eax->split.is_fully_associative = 1;
  220. ebx->split.coherency_line_size = line_size - 1;
  221. ebx->split.ways_of_associativity = assocs[assoc] - 1;
  222. ebx->split.physical_line_partition = lines_per_tag - 1;
  223. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  224. (ebx->split.ways_of_associativity + 1) - 1;
  225. }
  226. static int __cpuinit cpuid4_cache_lookup(int index, struct _cpuid4_info *this_leaf)
  227. {
  228. union _cpuid4_leaf_eax eax;
  229. union _cpuid4_leaf_ebx ebx;
  230. union _cpuid4_leaf_ecx ecx;
  231. unsigned edx;
  232. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  233. amd_cpuid4(index, &eax, &ebx, &ecx);
  234. else
  235. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  236. if (eax.split.type == CACHE_TYPE_NULL)
  237. return -EIO; /* better error ? */
  238. this_leaf->eax = eax;
  239. this_leaf->ebx = ebx;
  240. this_leaf->ecx = ecx;
  241. this_leaf->size = (ecx.split.number_of_sets + 1) *
  242. (ebx.split.coherency_line_size + 1) *
  243. (ebx.split.physical_line_partition + 1) *
  244. (ebx.split.ways_of_associativity + 1);
  245. return 0;
  246. }
  247. static int __cpuinit find_num_cache_leaves(void)
  248. {
  249. unsigned int eax, ebx, ecx, edx;
  250. union _cpuid4_leaf_eax cache_eax;
  251. int i = -1;
  252. do {
  253. ++i;
  254. /* Do cpuid(4) loop to find out num_cache_leaves */
  255. cpuid_count(4, i, &eax, &ebx, &ecx, &edx);
  256. cache_eax.full = eax;
  257. } while (cache_eax.split.type != CACHE_TYPE_NULL);
  258. return i;
  259. }
  260. unsigned int __cpuinit init_intel_cacheinfo(struct cpuinfo_x86 *c)
  261. {
  262. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0; /* Cache sizes */
  263. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  264. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  265. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  266. #ifdef CONFIG_X86_HT
  267. unsigned int cpu = (c == &boot_cpu_data) ? 0 : (c - cpu_data);
  268. #endif
  269. if (c->cpuid_level > 3) {
  270. static int is_initialized;
  271. if (is_initialized == 0) {
  272. /* Init num_cache_leaves from boot CPU */
  273. num_cache_leaves = find_num_cache_leaves();
  274. is_initialized++;
  275. }
  276. /*
  277. * Whenever possible use cpuid(4), deterministic cache
  278. * parameters cpuid leaf to find the cache details
  279. */
  280. for (i = 0; i < num_cache_leaves; i++) {
  281. struct _cpuid4_info this_leaf;
  282. int retval;
  283. retval = cpuid4_cache_lookup(i, &this_leaf);
  284. if (retval >= 0) {
  285. switch(this_leaf.eax.split.level) {
  286. case 1:
  287. if (this_leaf.eax.split.type ==
  288. CACHE_TYPE_DATA)
  289. new_l1d = this_leaf.size/1024;
  290. else if (this_leaf.eax.split.type ==
  291. CACHE_TYPE_INST)
  292. new_l1i = this_leaf.size/1024;
  293. break;
  294. case 2:
  295. new_l2 = this_leaf.size/1024;
  296. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  297. index_msb = get_count_order(num_threads_sharing);
  298. l2_id = c->apicid >> index_msb;
  299. break;
  300. case 3:
  301. new_l3 = this_leaf.size/1024;
  302. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  303. index_msb = get_count_order(num_threads_sharing);
  304. l3_id = c->apicid >> index_msb;
  305. break;
  306. default:
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. /*
  313. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  314. * trace cache
  315. */
  316. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  317. /* supports eax=2 call */
  318. int i, j, n;
  319. int regs[4];
  320. unsigned char *dp = (unsigned char *)regs;
  321. int only_trace = 0;
  322. if (num_cache_leaves != 0 && c->x86 == 15)
  323. only_trace = 1;
  324. /* Number of times to iterate */
  325. n = cpuid_eax(2) & 0xFF;
  326. for ( i = 0 ; i < n ; i++ ) {
  327. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  328. /* If bit 31 is set, this is an unknown format */
  329. for ( j = 0 ; j < 3 ; j++ ) {
  330. if ( regs[j] < 0 ) regs[j] = 0;
  331. }
  332. /* Byte 0 is level count, not a descriptor */
  333. for ( j = 1 ; j < 16 ; j++ ) {
  334. unsigned char des = dp[j];
  335. unsigned char k = 0;
  336. /* look up this descriptor in the table */
  337. while (cache_table[k].descriptor != 0)
  338. {
  339. if (cache_table[k].descriptor == des) {
  340. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  341. break;
  342. switch (cache_table[k].cache_type) {
  343. case LVL_1_INST:
  344. l1i += cache_table[k].size;
  345. break;
  346. case LVL_1_DATA:
  347. l1d += cache_table[k].size;
  348. break;
  349. case LVL_2:
  350. l2 += cache_table[k].size;
  351. break;
  352. case LVL_3:
  353. l3 += cache_table[k].size;
  354. break;
  355. case LVL_TRACE:
  356. trace += cache_table[k].size;
  357. break;
  358. }
  359. break;
  360. }
  361. k++;
  362. }
  363. }
  364. }
  365. }
  366. if (new_l1d)
  367. l1d = new_l1d;
  368. if (new_l1i)
  369. l1i = new_l1i;
  370. if (new_l2) {
  371. l2 = new_l2;
  372. #ifdef CONFIG_X86_HT
  373. cpu_llc_id[cpu] = l2_id;
  374. #endif
  375. }
  376. if (new_l3) {
  377. l3 = new_l3;
  378. #ifdef CONFIG_X86_HT
  379. cpu_llc_id[cpu] = l3_id;
  380. #endif
  381. }
  382. if (trace)
  383. printk (KERN_INFO "CPU: Trace cache: %dK uops", trace);
  384. else if ( l1i )
  385. printk (KERN_INFO "CPU: L1 I cache: %dK", l1i);
  386. if (l1d)
  387. printk(", L1 D cache: %dK\n", l1d);
  388. else
  389. printk("\n");
  390. if (l2)
  391. printk(KERN_INFO "CPU: L2 cache: %dK\n", l2);
  392. if (l3)
  393. printk(KERN_INFO "CPU: L3 cache: %dK\n", l3);
  394. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  395. return l2;
  396. }
  397. /* pointer to _cpuid4_info array (for each cache leaf) */
  398. static struct _cpuid4_info *cpuid4_info[NR_CPUS];
  399. #define CPUID4_INFO_IDX(x,y) (&((cpuid4_info[x])[y]))
  400. #ifdef CONFIG_SMP
  401. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  402. {
  403. struct _cpuid4_info *this_leaf, *sibling_leaf;
  404. unsigned long num_threads_sharing;
  405. int index_msb, i;
  406. struct cpuinfo_x86 *c = cpu_data;
  407. this_leaf = CPUID4_INFO_IDX(cpu, index);
  408. num_threads_sharing = 1 + this_leaf->eax.split.num_threads_sharing;
  409. if (num_threads_sharing == 1)
  410. cpu_set(cpu, this_leaf->shared_cpu_map);
  411. else {
  412. index_msb = get_count_order(num_threads_sharing);
  413. for_each_online_cpu(i) {
  414. if (c[i].apicid >> index_msb ==
  415. c[cpu].apicid >> index_msb) {
  416. cpu_set(i, this_leaf->shared_cpu_map);
  417. if (i != cpu && cpuid4_info[i]) {
  418. sibling_leaf = CPUID4_INFO_IDX(i, index);
  419. cpu_set(cpu, sibling_leaf->shared_cpu_map);
  420. }
  421. }
  422. }
  423. }
  424. }
  425. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  426. {
  427. struct _cpuid4_info *this_leaf, *sibling_leaf;
  428. int sibling;
  429. this_leaf = CPUID4_INFO_IDX(cpu, index);
  430. for_each_cpu_mask(sibling, this_leaf->shared_cpu_map) {
  431. sibling_leaf = CPUID4_INFO_IDX(sibling, index);
  432. cpu_clear(cpu, sibling_leaf->shared_cpu_map);
  433. }
  434. }
  435. #else
  436. static void __init cache_shared_cpu_map_setup(unsigned int cpu, int index) {}
  437. static void __init cache_remove_shared_cpu_map(unsigned int cpu, int index) {}
  438. #endif
  439. static void free_cache_attributes(unsigned int cpu)
  440. {
  441. kfree(cpuid4_info[cpu]);
  442. cpuid4_info[cpu] = NULL;
  443. }
  444. static int __cpuinit detect_cache_attributes(unsigned int cpu)
  445. {
  446. struct _cpuid4_info *this_leaf;
  447. unsigned long j;
  448. int retval;
  449. cpumask_t oldmask;
  450. if (num_cache_leaves == 0)
  451. return -ENOENT;
  452. cpuid4_info[cpu] = kzalloc(
  453. sizeof(struct _cpuid4_info) * num_cache_leaves, GFP_KERNEL);
  454. if (cpuid4_info[cpu] == NULL)
  455. return -ENOMEM;
  456. oldmask = current->cpus_allowed;
  457. retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
  458. if (retval)
  459. goto out;
  460. /* Do cpuid and store the results */
  461. retval = 0;
  462. for (j = 0; j < num_cache_leaves; j++) {
  463. this_leaf = CPUID4_INFO_IDX(cpu, j);
  464. retval = cpuid4_cache_lookup(j, this_leaf);
  465. if (unlikely(retval < 0))
  466. break;
  467. cache_shared_cpu_map_setup(cpu, j);
  468. }
  469. set_cpus_allowed(current, oldmask);
  470. out:
  471. if (retval)
  472. free_cache_attributes(cpu);
  473. return retval;
  474. }
  475. #ifdef CONFIG_SYSFS
  476. #include <linux/kobject.h>
  477. #include <linux/sysfs.h>
  478. extern struct sysdev_class cpu_sysdev_class; /* from drivers/base/cpu.c */
  479. /* pointer to kobject for cpuX/cache */
  480. static struct kobject * cache_kobject[NR_CPUS];
  481. struct _index_kobject {
  482. struct kobject kobj;
  483. unsigned int cpu;
  484. unsigned short index;
  485. };
  486. /* pointer to array of kobjects for cpuX/cache/indexY */
  487. static struct _index_kobject *index_kobject[NR_CPUS];
  488. #define INDEX_KOBJECT_PTR(x,y) (&((index_kobject[x])[y]))
  489. #define show_one_plus(file_name, object, val) \
  490. static ssize_t show_##file_name \
  491. (struct _cpuid4_info *this_leaf, char *buf) \
  492. { \
  493. return sprintf (buf, "%lu\n", (unsigned long)this_leaf->object + val); \
  494. }
  495. show_one_plus(level, eax.split.level, 0);
  496. show_one_plus(coherency_line_size, ebx.split.coherency_line_size, 1);
  497. show_one_plus(physical_line_partition, ebx.split.physical_line_partition, 1);
  498. show_one_plus(ways_of_associativity, ebx.split.ways_of_associativity, 1);
  499. show_one_plus(number_of_sets, ecx.split.number_of_sets, 1);
  500. static ssize_t show_size(struct _cpuid4_info *this_leaf, char *buf)
  501. {
  502. return sprintf (buf, "%luK\n", this_leaf->size / 1024);
  503. }
  504. static ssize_t show_shared_cpu_map(struct _cpuid4_info *this_leaf, char *buf)
  505. {
  506. char mask_str[NR_CPUS];
  507. cpumask_scnprintf(mask_str, NR_CPUS, this_leaf->shared_cpu_map);
  508. return sprintf(buf, "%s\n", mask_str);
  509. }
  510. static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf) {
  511. switch(this_leaf->eax.split.type) {
  512. case CACHE_TYPE_DATA:
  513. return sprintf(buf, "Data\n");
  514. break;
  515. case CACHE_TYPE_INST:
  516. return sprintf(buf, "Instruction\n");
  517. break;
  518. case CACHE_TYPE_UNIFIED:
  519. return sprintf(buf, "Unified\n");
  520. break;
  521. default:
  522. return sprintf(buf, "Unknown\n");
  523. break;
  524. }
  525. }
  526. struct _cache_attr {
  527. struct attribute attr;
  528. ssize_t (*show)(struct _cpuid4_info *, char *);
  529. ssize_t (*store)(struct _cpuid4_info *, const char *, size_t count);
  530. };
  531. #define define_one_ro(_name) \
  532. static struct _cache_attr _name = \
  533. __ATTR(_name, 0444, show_##_name, NULL)
  534. define_one_ro(level);
  535. define_one_ro(type);
  536. define_one_ro(coherency_line_size);
  537. define_one_ro(physical_line_partition);
  538. define_one_ro(ways_of_associativity);
  539. define_one_ro(number_of_sets);
  540. define_one_ro(size);
  541. define_one_ro(shared_cpu_map);
  542. static struct attribute * default_attrs[] = {
  543. &type.attr,
  544. &level.attr,
  545. &coherency_line_size.attr,
  546. &physical_line_partition.attr,
  547. &ways_of_associativity.attr,
  548. &number_of_sets.attr,
  549. &size.attr,
  550. &shared_cpu_map.attr,
  551. NULL
  552. };
  553. #define to_object(k) container_of(k, struct _index_kobject, kobj)
  554. #define to_attr(a) container_of(a, struct _cache_attr, attr)
  555. static ssize_t show(struct kobject * kobj, struct attribute * attr, char * buf)
  556. {
  557. struct _cache_attr *fattr = to_attr(attr);
  558. struct _index_kobject *this_leaf = to_object(kobj);
  559. ssize_t ret;
  560. ret = fattr->show ?
  561. fattr->show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  562. buf) :
  563. 0;
  564. return ret;
  565. }
  566. static ssize_t store(struct kobject * kobj, struct attribute * attr,
  567. const char * buf, size_t count)
  568. {
  569. return 0;
  570. }
  571. static struct sysfs_ops sysfs_ops = {
  572. .show = show,
  573. .store = store,
  574. };
  575. static struct kobj_type ktype_cache = {
  576. .sysfs_ops = &sysfs_ops,
  577. .default_attrs = default_attrs,
  578. };
  579. static struct kobj_type ktype_percpu_entry = {
  580. .sysfs_ops = &sysfs_ops,
  581. };
  582. static void cpuid4_cache_sysfs_exit(unsigned int cpu)
  583. {
  584. kfree(cache_kobject[cpu]);
  585. kfree(index_kobject[cpu]);
  586. cache_kobject[cpu] = NULL;
  587. index_kobject[cpu] = NULL;
  588. free_cache_attributes(cpu);
  589. }
  590. static int __cpuinit cpuid4_cache_sysfs_init(unsigned int cpu)
  591. {
  592. if (num_cache_leaves == 0)
  593. return -ENOENT;
  594. detect_cache_attributes(cpu);
  595. if (cpuid4_info[cpu] == NULL)
  596. return -ENOENT;
  597. /* Allocate all required memory */
  598. cache_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  599. if (unlikely(cache_kobject[cpu] == NULL))
  600. goto err_out;
  601. index_kobject[cpu] = kzalloc(
  602. sizeof(struct _index_kobject ) * num_cache_leaves, GFP_KERNEL);
  603. if (unlikely(index_kobject[cpu] == NULL))
  604. goto err_out;
  605. return 0;
  606. err_out:
  607. cpuid4_cache_sysfs_exit(cpu);
  608. return -ENOMEM;
  609. }
  610. /* Add/Remove cache interface for CPU device */
  611. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  612. {
  613. unsigned int cpu = sys_dev->id;
  614. unsigned long i, j;
  615. struct _index_kobject *this_object;
  616. int retval = 0;
  617. retval = cpuid4_cache_sysfs_init(cpu);
  618. if (unlikely(retval < 0))
  619. return retval;
  620. cache_kobject[cpu]->parent = &sys_dev->kobj;
  621. kobject_set_name(cache_kobject[cpu], "%s", "cache");
  622. cache_kobject[cpu]->ktype = &ktype_percpu_entry;
  623. retval = kobject_register(cache_kobject[cpu]);
  624. for (i = 0; i < num_cache_leaves; i++) {
  625. this_object = INDEX_KOBJECT_PTR(cpu,i);
  626. this_object->cpu = cpu;
  627. this_object->index = i;
  628. this_object->kobj.parent = cache_kobject[cpu];
  629. kobject_set_name(&(this_object->kobj), "index%1lu", i);
  630. this_object->kobj.ktype = &ktype_cache;
  631. retval = kobject_register(&(this_object->kobj));
  632. if (unlikely(retval)) {
  633. for (j = 0; j < i; j++) {
  634. kobject_unregister(
  635. &(INDEX_KOBJECT_PTR(cpu,j)->kobj));
  636. }
  637. kobject_unregister(cache_kobject[cpu]);
  638. cpuid4_cache_sysfs_exit(cpu);
  639. break;
  640. }
  641. }
  642. return retval;
  643. }
  644. static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
  645. {
  646. unsigned int cpu = sys_dev->id;
  647. unsigned long i;
  648. if (cpuid4_info[cpu] == NULL)
  649. return;
  650. for (i = 0; i < num_cache_leaves; i++) {
  651. cache_remove_shared_cpu_map(cpu, i);
  652. kobject_unregister(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
  653. }
  654. kobject_unregister(cache_kobject[cpu]);
  655. cpuid4_cache_sysfs_exit(cpu);
  656. return;
  657. }
  658. static int __cpuinit cacheinfo_cpu_callback(struct notifier_block *nfb,
  659. unsigned long action, void *hcpu)
  660. {
  661. unsigned int cpu = (unsigned long)hcpu;
  662. struct sys_device *sys_dev;
  663. sys_dev = get_cpu_sysdev(cpu);
  664. switch (action) {
  665. case CPU_ONLINE:
  666. case CPU_ONLINE_FROZEN:
  667. cache_add_dev(sys_dev);
  668. break;
  669. case CPU_DEAD:
  670. case CPU_DEAD_FROZEN:
  671. cache_remove_dev(sys_dev);
  672. break;
  673. }
  674. return NOTIFY_OK;
  675. }
  676. static struct notifier_block __cpuinitdata cacheinfo_cpu_notifier =
  677. {
  678. .notifier_call = cacheinfo_cpu_callback,
  679. };
  680. static int __cpuinit cache_sysfs_init(void)
  681. {
  682. int i;
  683. if (num_cache_leaves == 0)
  684. return 0;
  685. register_hotcpu_notifier(&cacheinfo_cpu_notifier);
  686. for_each_online_cpu(i) {
  687. cacheinfo_cpu_callback(&cacheinfo_cpu_notifier, CPU_ONLINE,
  688. (void *)(long)i);
  689. }
  690. return 0;
  691. }
  692. device_initcall(cache_sysfs_init);
  693. #endif