intel_cacheinfo.c 23 KB

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