intel_cacheinfo.c 25 KB

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