intel_cacheinfo.c 25 KB

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