intel_cacheinfo.c 22 KB

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