intel_cacheinfo.c 29 KB

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