intel_cacheinfo.c 26 KB

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