intel_cacheinfo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 unsigned short assocs[] __cpuinitdata = {
  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 unsigned char levels[] __cpuinitdata = { 1, 1, 2, 3 };
  163. static unsigned char types[] __cpuinitdata = { 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. per_cpu(cpu_llc_id, cpu) = l2_id;
  374. #endif
  375. }
  376. if (new_l3) {
  377. l3 = new_l3;
  378. #ifdef CONFIG_X86_HT
  379. per_cpu(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 __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index) {}
  437. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index) {}
  438. #endif
  439. static void free_cache_attributes(unsigned int cpu)
  440. {
  441. int i;
  442. for (i = 0; i < num_cache_leaves; i++)
  443. cache_remove_shared_cpu_map(cpu, i);
  444. kfree(cpuid4_info[cpu]);
  445. cpuid4_info[cpu] = NULL;
  446. }
  447. static int __cpuinit detect_cache_attributes(unsigned int cpu)
  448. {
  449. struct _cpuid4_info *this_leaf;
  450. unsigned long j;
  451. int retval;
  452. cpumask_t oldmask;
  453. if (num_cache_leaves == 0)
  454. return -ENOENT;
  455. cpuid4_info[cpu] = kzalloc(
  456. sizeof(struct _cpuid4_info) * num_cache_leaves, GFP_KERNEL);
  457. if (cpuid4_info[cpu] == NULL)
  458. return -ENOMEM;
  459. oldmask = current->cpus_allowed;
  460. retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
  461. if (retval)
  462. goto out;
  463. /* Do cpuid and store the results */
  464. for (j = 0; j < num_cache_leaves; j++) {
  465. this_leaf = CPUID4_INFO_IDX(cpu, j);
  466. retval = cpuid4_cache_lookup(j, this_leaf);
  467. if (unlikely(retval < 0)) {
  468. int i;
  469. for (i = 0; i < j; i++)
  470. cache_remove_shared_cpu_map(cpu, i);
  471. break;
  472. }
  473. cache_shared_cpu_map_setup(cpu, j);
  474. }
  475. set_cpus_allowed(current, oldmask);
  476. out:
  477. if (retval) {
  478. kfree(cpuid4_info[cpu]);
  479. cpuid4_info[cpu] = NULL;
  480. }
  481. return retval;
  482. }
  483. #ifdef CONFIG_SYSFS
  484. #include <linux/kobject.h>
  485. #include <linux/sysfs.h>
  486. extern struct sysdev_class cpu_sysdev_class; /* from drivers/base/cpu.c */
  487. /* pointer to kobject for cpuX/cache */
  488. static struct kobject * cache_kobject[NR_CPUS];
  489. struct _index_kobject {
  490. struct kobject kobj;
  491. unsigned int cpu;
  492. unsigned short index;
  493. };
  494. /* pointer to array of kobjects for cpuX/cache/indexY */
  495. static struct _index_kobject *index_kobject[NR_CPUS];
  496. #define INDEX_KOBJECT_PTR(x,y) (&((index_kobject[x])[y]))
  497. #define show_one_plus(file_name, object, val) \
  498. static ssize_t show_##file_name \
  499. (struct _cpuid4_info *this_leaf, char *buf) \
  500. { \
  501. return sprintf (buf, "%lu\n", (unsigned long)this_leaf->object + val); \
  502. }
  503. show_one_plus(level, eax.split.level, 0);
  504. show_one_plus(coherency_line_size, ebx.split.coherency_line_size, 1);
  505. show_one_plus(physical_line_partition, ebx.split.physical_line_partition, 1);
  506. show_one_plus(ways_of_associativity, ebx.split.ways_of_associativity, 1);
  507. show_one_plus(number_of_sets, ecx.split.number_of_sets, 1);
  508. static ssize_t show_size(struct _cpuid4_info *this_leaf, char *buf)
  509. {
  510. return sprintf (buf, "%luK\n", this_leaf->size / 1024);
  511. }
  512. static ssize_t show_shared_cpu_map(struct _cpuid4_info *this_leaf, char *buf)
  513. {
  514. char mask_str[NR_CPUS];
  515. cpumask_scnprintf(mask_str, NR_CPUS, this_leaf->shared_cpu_map);
  516. return sprintf(buf, "%s\n", mask_str);
  517. }
  518. static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf) {
  519. switch(this_leaf->eax.split.type) {
  520. case CACHE_TYPE_DATA:
  521. return sprintf(buf, "Data\n");
  522. break;
  523. case CACHE_TYPE_INST:
  524. return sprintf(buf, "Instruction\n");
  525. break;
  526. case CACHE_TYPE_UNIFIED:
  527. return sprintf(buf, "Unified\n");
  528. break;
  529. default:
  530. return sprintf(buf, "Unknown\n");
  531. break;
  532. }
  533. }
  534. struct _cache_attr {
  535. struct attribute attr;
  536. ssize_t (*show)(struct _cpuid4_info *, char *);
  537. ssize_t (*store)(struct _cpuid4_info *, const char *, size_t count);
  538. };
  539. #define define_one_ro(_name) \
  540. static struct _cache_attr _name = \
  541. __ATTR(_name, 0444, show_##_name, NULL)
  542. define_one_ro(level);
  543. define_one_ro(type);
  544. define_one_ro(coherency_line_size);
  545. define_one_ro(physical_line_partition);
  546. define_one_ro(ways_of_associativity);
  547. define_one_ro(number_of_sets);
  548. define_one_ro(size);
  549. define_one_ro(shared_cpu_map);
  550. static struct attribute * default_attrs[] = {
  551. &type.attr,
  552. &level.attr,
  553. &coherency_line_size.attr,
  554. &physical_line_partition.attr,
  555. &ways_of_associativity.attr,
  556. &number_of_sets.attr,
  557. &size.attr,
  558. &shared_cpu_map.attr,
  559. NULL
  560. };
  561. #define to_object(k) container_of(k, struct _index_kobject, kobj)
  562. #define to_attr(a) container_of(a, struct _cache_attr, attr)
  563. static ssize_t show(struct kobject * kobj, struct attribute * attr, char * buf)
  564. {
  565. struct _cache_attr *fattr = to_attr(attr);
  566. struct _index_kobject *this_leaf = to_object(kobj);
  567. ssize_t ret;
  568. ret = fattr->show ?
  569. fattr->show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  570. buf) :
  571. 0;
  572. return ret;
  573. }
  574. static ssize_t store(struct kobject * kobj, struct attribute * attr,
  575. const char * buf, size_t count)
  576. {
  577. return 0;
  578. }
  579. static struct sysfs_ops sysfs_ops = {
  580. .show = show,
  581. .store = store,
  582. };
  583. static struct kobj_type ktype_cache = {
  584. .sysfs_ops = &sysfs_ops,
  585. .default_attrs = default_attrs,
  586. };
  587. static struct kobj_type ktype_percpu_entry = {
  588. .sysfs_ops = &sysfs_ops,
  589. };
  590. static void __cpuinit cpuid4_cache_sysfs_exit(unsigned int cpu)
  591. {
  592. kfree(cache_kobject[cpu]);
  593. kfree(index_kobject[cpu]);
  594. cache_kobject[cpu] = NULL;
  595. index_kobject[cpu] = NULL;
  596. free_cache_attributes(cpu);
  597. }
  598. static int __cpuinit cpuid4_cache_sysfs_init(unsigned int cpu)
  599. {
  600. int err;
  601. if (num_cache_leaves == 0)
  602. return -ENOENT;
  603. err = detect_cache_attributes(cpu);
  604. if (err)
  605. return err;
  606. /* Allocate all required memory */
  607. cache_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  608. if (unlikely(cache_kobject[cpu] == NULL))
  609. goto err_out;
  610. index_kobject[cpu] = kzalloc(
  611. sizeof(struct _index_kobject ) * num_cache_leaves, GFP_KERNEL);
  612. if (unlikely(index_kobject[cpu] == NULL))
  613. goto err_out;
  614. return 0;
  615. err_out:
  616. cpuid4_cache_sysfs_exit(cpu);
  617. return -ENOMEM;
  618. }
  619. static cpumask_t cache_dev_map = CPU_MASK_NONE;
  620. /* Add/Remove cache interface for CPU device */
  621. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  622. {
  623. unsigned int cpu = sys_dev->id;
  624. unsigned long i, j;
  625. struct _index_kobject *this_object;
  626. int retval;
  627. retval = cpuid4_cache_sysfs_init(cpu);
  628. if (unlikely(retval < 0))
  629. return retval;
  630. cache_kobject[cpu]->parent = &sys_dev->kobj;
  631. kobject_set_name(cache_kobject[cpu], "%s", "cache");
  632. cache_kobject[cpu]->ktype = &ktype_percpu_entry;
  633. retval = kobject_register(cache_kobject[cpu]);
  634. if (retval < 0) {
  635. cpuid4_cache_sysfs_exit(cpu);
  636. return retval;
  637. }
  638. for (i = 0; i < num_cache_leaves; i++) {
  639. this_object = INDEX_KOBJECT_PTR(cpu,i);
  640. this_object->cpu = cpu;
  641. this_object->index = i;
  642. this_object->kobj.parent = cache_kobject[cpu];
  643. kobject_set_name(&(this_object->kobj), "index%1lu", i);
  644. this_object->kobj.ktype = &ktype_cache;
  645. retval = kobject_register(&(this_object->kobj));
  646. if (unlikely(retval)) {
  647. for (j = 0; j < i; j++) {
  648. kobject_unregister(
  649. &(INDEX_KOBJECT_PTR(cpu,j)->kobj));
  650. }
  651. kobject_unregister(cache_kobject[cpu]);
  652. cpuid4_cache_sysfs_exit(cpu);
  653. break;
  654. }
  655. }
  656. if (!retval)
  657. cpu_set(cpu, cache_dev_map);
  658. return retval;
  659. }
  660. static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
  661. {
  662. unsigned int cpu = sys_dev->id;
  663. unsigned long i;
  664. if (cpuid4_info[cpu] == NULL)
  665. return;
  666. if (!cpu_isset(cpu, cache_dev_map))
  667. return;
  668. cpu_clear(cpu, cache_dev_map);
  669. for (i = 0; i < num_cache_leaves; i++)
  670. kobject_unregister(&(INDEX_KOBJECT_PTR(cpu,i)->kobj));
  671. kobject_unregister(cache_kobject[cpu]);
  672. cpuid4_cache_sysfs_exit(cpu);
  673. }
  674. static int __cpuinit cacheinfo_cpu_callback(struct notifier_block *nfb,
  675. unsigned long action, void *hcpu)
  676. {
  677. unsigned int cpu = (unsigned long)hcpu;
  678. struct sys_device *sys_dev;
  679. sys_dev = get_cpu_sysdev(cpu);
  680. switch (action) {
  681. case CPU_ONLINE:
  682. case CPU_ONLINE_FROZEN:
  683. cache_add_dev(sys_dev);
  684. break;
  685. case CPU_DEAD:
  686. case CPU_DEAD_FROZEN:
  687. cache_remove_dev(sys_dev);
  688. break;
  689. }
  690. return NOTIFY_OK;
  691. }
  692. static struct notifier_block __cpuinitdata cacheinfo_cpu_notifier =
  693. {
  694. .notifier_call = cacheinfo_cpu_callback,
  695. };
  696. static int __cpuinit cache_sysfs_init(void)
  697. {
  698. int i;
  699. if (num_cache_leaves == 0)
  700. return 0;
  701. for_each_online_cpu(i) {
  702. int err;
  703. struct sys_device *sys_dev = get_cpu_sysdev(i);
  704. err = cache_add_dev(sys_dev);
  705. if (err)
  706. return err;
  707. }
  708. register_hotcpu_notifier(&cacheinfo_cpu_notifier);
  709. return 0;
  710. }
  711. device_initcall(cache_sysfs_init);
  712. #endif