intel_cacheinfo.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * Routines to identify 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/slab.h>
  10. #include <linux/cacheinfo.h>
  11. #include <linux/cpu.h>
  12. #include <linux/sched.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/pci.h>
  15. #include <asm/cpufeature.h>
  16. #include <asm/amd_nb.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. unsigned char descriptor;
  25. char cache_type;
  26. short size;
  27. };
  28. #define MB(x) ((x) * 1024)
  29. /* All the cache descriptor types we care about (no TLB or
  30. trace cache entries) */
  31. static const struct _cache_table cache_table[] =
  32. {
  33. { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */
  34. { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */
  35. { 0x09, LVL_1_INST, 32 }, /* 4-way set assoc, 64 byte line size */
  36. { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */
  37. { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */
  38. { 0x0d, LVL_1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */
  39. { 0x0e, LVL_1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */
  40. { 0x21, LVL_2, 256 }, /* 8-way set assoc, 64 byte line size */
  41. { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  42. { 0x23, LVL_3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  43. { 0x25, LVL_3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  44. { 0x29, LVL_3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  45. { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */
  46. { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */
  47. { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  48. { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  49. { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line size */
  50. { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  51. { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  52. { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  53. { 0x3f, LVL_2, 256 }, /* 2-way set assoc, 64 byte line size */
  54. { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */
  55. { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */
  56. { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */
  57. { 0x44, LVL_2, MB(1) }, /* 4-way set assoc, 32 byte line size */
  58. { 0x45, LVL_2, MB(2) }, /* 4-way set assoc, 32 byte line size */
  59. { 0x46, LVL_3, MB(4) }, /* 4-way set assoc, 64 byte line size */
  60. { 0x47, LVL_3, MB(8) }, /* 8-way set assoc, 64 byte line size */
  61. { 0x48, LVL_2, MB(3) }, /* 12-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. { 0x80, LVL_2, 512 }, /* 8-way set assoc, 64 byte line size */
  84. { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */
  85. { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */
  86. { 0x84, LVL_2, MB(1) }, /* 8-way set assoc, 32 byte line size */
  87. { 0x85, LVL_2, MB(2) }, /* 8-way set assoc, 32 byte line size */
  88. { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */
  89. { 0x87, LVL_2, MB(1) }, /* 8-way set assoc, 64 byte line size */
  90. { 0xd0, LVL_3, 512 }, /* 4-way set assoc, 64 byte line size */
  91. { 0xd1, LVL_3, MB(1) }, /* 4-way set assoc, 64 byte line size */
  92. { 0xd2, LVL_3, MB(2) }, /* 4-way set assoc, 64 byte line size */
  93. { 0xd6, LVL_3, MB(1) }, /* 8-way set assoc, 64 byte line size */
  94. { 0xd7, LVL_3, MB(2) }, /* 8-way set assoc, 64 byte line size */
  95. { 0xd8, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  96. { 0xdc, LVL_3, MB(2) }, /* 12-way set assoc, 64 byte line size */
  97. { 0xdd, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  98. { 0xde, LVL_3, MB(8) }, /* 12-way set assoc, 64 byte line size */
  99. { 0xe2, LVL_3, MB(2) }, /* 16-way set assoc, 64 byte line size */
  100. { 0xe3, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  101. { 0xe4, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  102. { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */
  103. { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */
  104. { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */
  105. { 0x00, 0, 0}
  106. };
  107. enum _cache_type {
  108. CTYPE_NULL = 0,
  109. CTYPE_DATA = 1,
  110. CTYPE_INST = 2,
  111. CTYPE_UNIFIED = 3
  112. };
  113. union _cpuid4_leaf_eax {
  114. struct {
  115. enum _cache_type type:5;
  116. unsigned int level:3;
  117. unsigned int is_self_initializing:1;
  118. unsigned int is_fully_associative:1;
  119. unsigned int reserved:4;
  120. unsigned int num_threads_sharing:12;
  121. unsigned int num_cores_on_die:6;
  122. } split;
  123. u32 full;
  124. };
  125. union _cpuid4_leaf_ebx {
  126. struct {
  127. unsigned int coherency_line_size:12;
  128. unsigned int physical_line_partition:10;
  129. unsigned int ways_of_associativity:10;
  130. } split;
  131. u32 full;
  132. };
  133. union _cpuid4_leaf_ecx {
  134. struct {
  135. unsigned int number_of_sets:32;
  136. } split;
  137. u32 full;
  138. };
  139. struct _cpuid4_info_regs {
  140. union _cpuid4_leaf_eax eax;
  141. union _cpuid4_leaf_ebx ebx;
  142. union _cpuid4_leaf_ecx ecx;
  143. unsigned long size;
  144. struct amd_northbridge *nb;
  145. };
  146. static unsigned short num_cache_leaves;
  147. /* AMD doesn't have CPUID4. Emulate it here to report the same
  148. information to the user. This makes some assumptions about the machine:
  149. L2 not shared, no SMT etc. that is currently true on AMD CPUs.
  150. In theory the TLBs could be reported as fake type (they are in "dummy").
  151. Maybe later */
  152. union l1_cache {
  153. struct {
  154. unsigned line_size:8;
  155. unsigned lines_per_tag:8;
  156. unsigned assoc:8;
  157. unsigned size_in_kb:8;
  158. };
  159. unsigned val;
  160. };
  161. union l2_cache {
  162. struct {
  163. unsigned line_size:8;
  164. unsigned lines_per_tag:4;
  165. unsigned assoc:4;
  166. unsigned size_in_kb:16;
  167. };
  168. unsigned val;
  169. };
  170. union l3_cache {
  171. struct {
  172. unsigned line_size:8;
  173. unsigned lines_per_tag:4;
  174. unsigned assoc:4;
  175. unsigned res:2;
  176. unsigned size_encoded:14;
  177. };
  178. unsigned val;
  179. };
  180. static const unsigned short assocs[] = {
  181. [1] = 1,
  182. [2] = 2,
  183. [4] = 4,
  184. [6] = 8,
  185. [8] = 16,
  186. [0xa] = 32,
  187. [0xb] = 48,
  188. [0xc] = 64,
  189. [0xd] = 96,
  190. [0xe] = 128,
  191. [0xf] = 0xffff /* fully associative - no way to show this currently */
  192. };
  193. static const unsigned char levels[] = { 1, 1, 2, 3 };
  194. static const unsigned char types[] = { 1, 2, 3, 3 };
  195. static const enum cache_type cache_type_map[] = {
  196. [CTYPE_NULL] = CACHE_TYPE_NOCACHE,
  197. [CTYPE_DATA] = CACHE_TYPE_DATA,
  198. [CTYPE_INST] = CACHE_TYPE_INST,
  199. [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
  200. };
  201. static void
  202. amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  203. union _cpuid4_leaf_ebx *ebx,
  204. union _cpuid4_leaf_ecx *ecx)
  205. {
  206. unsigned dummy;
  207. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  208. union l1_cache l1i, l1d;
  209. union l2_cache l2;
  210. union l3_cache l3;
  211. union l1_cache *l1 = &l1d;
  212. eax->full = 0;
  213. ebx->full = 0;
  214. ecx->full = 0;
  215. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  216. cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val);
  217. switch (leaf) {
  218. case 1:
  219. l1 = &l1i;
  220. case 0:
  221. if (!l1->val)
  222. return;
  223. assoc = assocs[l1->assoc];
  224. line_size = l1->line_size;
  225. lines_per_tag = l1->lines_per_tag;
  226. size_in_kb = l1->size_in_kb;
  227. break;
  228. case 2:
  229. if (!l2.val)
  230. return;
  231. assoc = assocs[l2.assoc];
  232. line_size = l2.line_size;
  233. lines_per_tag = l2.lines_per_tag;
  234. /* cpu_data has errata corrections for K7 applied */
  235. size_in_kb = __this_cpu_read(cpu_info.x86_cache_size);
  236. break;
  237. case 3:
  238. if (!l3.val)
  239. return;
  240. assoc = assocs[l3.assoc];
  241. line_size = l3.line_size;
  242. lines_per_tag = l3.lines_per_tag;
  243. size_in_kb = l3.size_encoded * 512;
  244. if (boot_cpu_has(X86_FEATURE_AMD_DCM)) {
  245. size_in_kb = size_in_kb >> 1;
  246. assoc = assoc >> 1;
  247. }
  248. break;
  249. default:
  250. return;
  251. }
  252. eax->split.is_self_initializing = 1;
  253. eax->split.type = types[leaf];
  254. eax->split.level = levels[leaf];
  255. eax->split.num_threads_sharing = 0;
  256. eax->split.num_cores_on_die = __this_cpu_read(cpu_info.x86_max_cores) - 1;
  257. if (assoc == 0xffff)
  258. eax->split.is_fully_associative = 1;
  259. ebx->split.coherency_line_size = line_size - 1;
  260. ebx->split.ways_of_associativity = assoc - 1;
  261. ebx->split.physical_line_partition = lines_per_tag - 1;
  262. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  263. (ebx->split.ways_of_associativity + 1) - 1;
  264. }
  265. #if defined(CONFIG_AMD_NB) && defined(CONFIG_SYSFS)
  266. /*
  267. * L3 cache descriptors
  268. */
  269. static void amd_calc_l3_indices(struct amd_northbridge *nb)
  270. {
  271. struct amd_l3_cache *l3 = &nb->l3_cache;
  272. unsigned int sc0, sc1, sc2, sc3;
  273. u32 val = 0;
  274. pci_read_config_dword(nb->misc, 0x1C4, &val);
  275. /* calculate subcache sizes */
  276. l3->subcaches[0] = sc0 = !(val & BIT(0));
  277. l3->subcaches[1] = sc1 = !(val & BIT(4));
  278. if (boot_cpu_data.x86 == 0x15) {
  279. l3->subcaches[0] = sc0 += !(val & BIT(1));
  280. l3->subcaches[1] = sc1 += !(val & BIT(5));
  281. }
  282. l3->subcaches[2] = sc2 = !(val & BIT(8)) + !(val & BIT(9));
  283. l3->subcaches[3] = sc3 = !(val & BIT(12)) + !(val & BIT(13));
  284. l3->indices = (max(max3(sc0, sc1, sc2), sc3) << 10) - 1;
  285. }
  286. /*
  287. * check whether a slot used for disabling an L3 index is occupied.
  288. * @l3: L3 cache descriptor
  289. * @slot: slot number (0..1)
  290. *
  291. * @returns: the disabled index if used or negative value if slot free.
  292. */
  293. static int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned slot)
  294. {
  295. unsigned int reg = 0;
  296. pci_read_config_dword(nb->misc, 0x1BC + slot * 4, &reg);
  297. /* check whether this slot is activated already */
  298. if (reg & (3UL << 30))
  299. return reg & 0xfff;
  300. return -1;
  301. }
  302. static ssize_t show_cache_disable(struct cacheinfo *this_leaf, char *buf,
  303. unsigned int slot)
  304. {
  305. int index;
  306. struct amd_northbridge *nb = this_leaf->priv;
  307. index = amd_get_l3_disable_slot(nb, slot);
  308. if (index >= 0)
  309. return sprintf(buf, "%d\n", index);
  310. return sprintf(buf, "FREE\n");
  311. }
  312. #define SHOW_CACHE_DISABLE(slot) \
  313. static ssize_t \
  314. cache_disable_##slot##_show(struct device *dev, \
  315. struct device_attribute *attr, char *buf) \
  316. { \
  317. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  318. return show_cache_disable(this_leaf, buf, slot); \
  319. }
  320. SHOW_CACHE_DISABLE(0)
  321. SHOW_CACHE_DISABLE(1)
  322. static void amd_l3_disable_index(struct amd_northbridge *nb, int cpu,
  323. unsigned slot, unsigned long idx)
  324. {
  325. int i;
  326. idx |= BIT(30);
  327. /*
  328. * disable index in all 4 subcaches
  329. */
  330. for (i = 0; i < 4; i++) {
  331. u32 reg = idx | (i << 20);
  332. if (!nb->l3_cache.subcaches[i])
  333. continue;
  334. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  335. /*
  336. * We need to WBINVD on a core on the node containing the L3
  337. * cache which indices we disable therefore a simple wbinvd()
  338. * is not sufficient.
  339. */
  340. wbinvd_on_cpu(cpu);
  341. reg |= BIT(31);
  342. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  343. }
  344. }
  345. /*
  346. * disable a L3 cache index by using a disable-slot
  347. *
  348. * @l3: L3 cache descriptor
  349. * @cpu: A CPU on the node containing the L3 cache
  350. * @slot: slot number (0..1)
  351. * @index: index to disable
  352. *
  353. * @return: 0 on success, error status on failure
  354. */
  355. static int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu,
  356. unsigned slot, unsigned long index)
  357. {
  358. int ret = 0;
  359. /* check if @slot is already used or the index is already disabled */
  360. ret = amd_get_l3_disable_slot(nb, slot);
  361. if (ret >= 0)
  362. return -EEXIST;
  363. if (index > nb->l3_cache.indices)
  364. return -EINVAL;
  365. /* check whether the other slot has disabled the same index already */
  366. if (index == amd_get_l3_disable_slot(nb, !slot))
  367. return -EEXIST;
  368. amd_l3_disable_index(nb, cpu, slot, index);
  369. return 0;
  370. }
  371. static ssize_t store_cache_disable(struct cacheinfo *this_leaf,
  372. const char *buf, size_t count,
  373. unsigned int slot)
  374. {
  375. unsigned long val = 0;
  376. int cpu, err = 0;
  377. struct amd_northbridge *nb = this_leaf->priv;
  378. if (!capable(CAP_SYS_ADMIN))
  379. return -EPERM;
  380. cpu = cpumask_first(&this_leaf->shared_cpu_map);
  381. if (kstrtoul(buf, 10, &val) < 0)
  382. return -EINVAL;
  383. err = amd_set_l3_disable_slot(nb, cpu, slot, val);
  384. if (err) {
  385. if (err == -EEXIST)
  386. pr_warn("L3 slot %d in use/index already disabled!\n",
  387. slot);
  388. return err;
  389. }
  390. return count;
  391. }
  392. #define STORE_CACHE_DISABLE(slot) \
  393. static ssize_t \
  394. cache_disable_##slot##_store(struct device *dev, \
  395. struct device_attribute *attr, \
  396. const char *buf, size_t count) \
  397. { \
  398. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  399. return store_cache_disable(this_leaf, buf, count, slot); \
  400. }
  401. STORE_CACHE_DISABLE(0)
  402. STORE_CACHE_DISABLE(1)
  403. static ssize_t subcaches_show(struct device *dev,
  404. struct device_attribute *attr, char *buf)
  405. {
  406. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  407. int cpu = cpumask_first(&this_leaf->shared_cpu_map);
  408. return sprintf(buf, "%x\n", amd_get_subcaches(cpu));
  409. }
  410. static ssize_t subcaches_store(struct device *dev,
  411. struct device_attribute *attr,
  412. const char *buf, size_t count)
  413. {
  414. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  415. int cpu = cpumask_first(&this_leaf->shared_cpu_map);
  416. unsigned long val;
  417. if (!capable(CAP_SYS_ADMIN))
  418. return -EPERM;
  419. if (kstrtoul(buf, 16, &val) < 0)
  420. return -EINVAL;
  421. if (amd_set_subcaches(cpu, val))
  422. return -EINVAL;
  423. return count;
  424. }
  425. static DEVICE_ATTR_RW(cache_disable_0);
  426. static DEVICE_ATTR_RW(cache_disable_1);
  427. static DEVICE_ATTR_RW(subcaches);
  428. static umode_t
  429. cache_private_attrs_is_visible(struct kobject *kobj,
  430. struct attribute *attr, int unused)
  431. {
  432. struct device *dev = kobj_to_dev(kobj);
  433. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  434. umode_t mode = attr->mode;
  435. if (!this_leaf->priv)
  436. return 0;
  437. if ((attr == &dev_attr_subcaches.attr) &&
  438. amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  439. return mode;
  440. if ((attr == &dev_attr_cache_disable_0.attr ||
  441. attr == &dev_attr_cache_disable_1.attr) &&
  442. amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  443. return mode;
  444. return 0;
  445. }
  446. static struct attribute_group cache_private_group = {
  447. .is_visible = cache_private_attrs_is_visible,
  448. };
  449. static void init_amd_l3_attrs(void)
  450. {
  451. int n = 1;
  452. static struct attribute **amd_l3_attrs;
  453. if (amd_l3_attrs) /* already initialized */
  454. return;
  455. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  456. n += 2;
  457. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  458. n += 1;
  459. amd_l3_attrs = kcalloc(n, sizeof(*amd_l3_attrs), GFP_KERNEL);
  460. if (!amd_l3_attrs)
  461. return;
  462. n = 0;
  463. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) {
  464. amd_l3_attrs[n++] = &dev_attr_cache_disable_0.attr;
  465. amd_l3_attrs[n++] = &dev_attr_cache_disable_1.attr;
  466. }
  467. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  468. amd_l3_attrs[n++] = &dev_attr_subcaches.attr;
  469. cache_private_group.attrs = amd_l3_attrs;
  470. }
  471. const struct attribute_group *
  472. cache_get_priv_group(struct cacheinfo *this_leaf)
  473. {
  474. struct amd_northbridge *nb = this_leaf->priv;
  475. if (this_leaf->level < 3 || !nb)
  476. return NULL;
  477. if (nb && nb->l3_cache.indices)
  478. init_amd_l3_attrs();
  479. return &cache_private_group;
  480. }
  481. static void amd_init_l3_cache(struct _cpuid4_info_regs *this_leaf, int index)
  482. {
  483. int node;
  484. /* only for L3, and not in virtualized environments */
  485. if (index < 3)
  486. return;
  487. node = amd_get_nb_id(smp_processor_id());
  488. this_leaf->nb = node_to_amd_nb(node);
  489. if (this_leaf->nb && !this_leaf->nb->l3_cache.indices)
  490. amd_calc_l3_indices(this_leaf->nb);
  491. }
  492. #else
  493. #define amd_init_l3_cache(x, y)
  494. #endif /* CONFIG_AMD_NB && CONFIG_SYSFS */
  495. static int
  496. cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *this_leaf)
  497. {
  498. union _cpuid4_leaf_eax eax;
  499. union _cpuid4_leaf_ebx ebx;
  500. union _cpuid4_leaf_ecx ecx;
  501. unsigned edx;
  502. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  503. if (boot_cpu_has(X86_FEATURE_TOPOEXT))
  504. cpuid_count(0x8000001d, index, &eax.full,
  505. &ebx.full, &ecx.full, &edx);
  506. else
  507. amd_cpuid4(index, &eax, &ebx, &ecx);
  508. amd_init_l3_cache(this_leaf, index);
  509. } else {
  510. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  511. }
  512. if (eax.split.type == CTYPE_NULL)
  513. return -EIO; /* better error ? */
  514. this_leaf->eax = eax;
  515. this_leaf->ebx = ebx;
  516. this_leaf->ecx = ecx;
  517. this_leaf->size = (ecx.split.number_of_sets + 1) *
  518. (ebx.split.coherency_line_size + 1) *
  519. (ebx.split.physical_line_partition + 1) *
  520. (ebx.split.ways_of_associativity + 1);
  521. return 0;
  522. }
  523. static int find_num_cache_leaves(struct cpuinfo_x86 *c)
  524. {
  525. unsigned int eax, ebx, ecx, edx, op;
  526. union _cpuid4_leaf_eax cache_eax;
  527. int i = -1;
  528. if (c->x86_vendor == X86_VENDOR_AMD)
  529. op = 0x8000001d;
  530. else
  531. op = 4;
  532. do {
  533. ++i;
  534. /* Do cpuid(op) loop to find out num_cache_leaves */
  535. cpuid_count(op, i, &eax, &ebx, &ecx, &edx);
  536. cache_eax.full = eax;
  537. } while (cache_eax.split.type != CTYPE_NULL);
  538. return i;
  539. }
  540. void init_amd_cacheinfo(struct cpuinfo_x86 *c)
  541. {
  542. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  543. num_cache_leaves = find_num_cache_leaves(c);
  544. } else if (c->extended_cpuid_level >= 0x80000006) {
  545. if (cpuid_edx(0x80000006) & 0xf000)
  546. num_cache_leaves = 4;
  547. else
  548. num_cache_leaves = 3;
  549. }
  550. }
  551. unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c)
  552. {
  553. /* Cache sizes */
  554. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0;
  555. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  556. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  557. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  558. #ifdef CONFIG_SMP
  559. unsigned int cpu = c->cpu_index;
  560. #endif
  561. if (c->cpuid_level > 3) {
  562. static int is_initialized;
  563. if (is_initialized == 0) {
  564. /* Init num_cache_leaves from boot CPU */
  565. num_cache_leaves = find_num_cache_leaves(c);
  566. is_initialized++;
  567. }
  568. /*
  569. * Whenever possible use cpuid(4), deterministic cache
  570. * parameters cpuid leaf to find the cache details
  571. */
  572. for (i = 0; i < num_cache_leaves; i++) {
  573. struct _cpuid4_info_regs this_leaf = {};
  574. int retval;
  575. retval = cpuid4_cache_lookup_regs(i, &this_leaf);
  576. if (retval < 0)
  577. continue;
  578. switch (this_leaf.eax.split.level) {
  579. case 1:
  580. if (this_leaf.eax.split.type == CTYPE_DATA)
  581. new_l1d = this_leaf.size/1024;
  582. else if (this_leaf.eax.split.type == CTYPE_INST)
  583. new_l1i = this_leaf.size/1024;
  584. break;
  585. case 2:
  586. new_l2 = this_leaf.size/1024;
  587. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  588. index_msb = get_count_order(num_threads_sharing);
  589. l2_id = c->apicid & ~((1 << index_msb) - 1);
  590. break;
  591. case 3:
  592. new_l3 = this_leaf.size/1024;
  593. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  594. index_msb = get_count_order(num_threads_sharing);
  595. l3_id = c->apicid & ~((1 << index_msb) - 1);
  596. break;
  597. default:
  598. break;
  599. }
  600. }
  601. }
  602. /*
  603. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  604. * trace cache
  605. */
  606. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  607. /* supports eax=2 call */
  608. int j, n;
  609. unsigned int regs[4];
  610. unsigned char *dp = (unsigned char *)regs;
  611. int only_trace = 0;
  612. if (num_cache_leaves != 0 && c->x86 == 15)
  613. only_trace = 1;
  614. /* Number of times to iterate */
  615. n = cpuid_eax(2) & 0xFF;
  616. for (i = 0 ; i < n ; i++) {
  617. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  618. /* If bit 31 is set, this is an unknown format */
  619. for (j = 0 ; j < 3 ; j++)
  620. if (regs[j] & (1 << 31))
  621. regs[j] = 0;
  622. /* Byte 0 is level count, not a descriptor */
  623. for (j = 1 ; j < 16 ; j++) {
  624. unsigned char des = dp[j];
  625. unsigned char k = 0;
  626. /* look up this descriptor in the table */
  627. while (cache_table[k].descriptor != 0) {
  628. if (cache_table[k].descriptor == des) {
  629. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  630. break;
  631. switch (cache_table[k].cache_type) {
  632. case LVL_1_INST:
  633. l1i += cache_table[k].size;
  634. break;
  635. case LVL_1_DATA:
  636. l1d += cache_table[k].size;
  637. break;
  638. case LVL_2:
  639. l2 += cache_table[k].size;
  640. break;
  641. case LVL_3:
  642. l3 += cache_table[k].size;
  643. break;
  644. case LVL_TRACE:
  645. trace += cache_table[k].size;
  646. break;
  647. }
  648. break;
  649. }
  650. k++;
  651. }
  652. }
  653. }
  654. }
  655. if (new_l1d)
  656. l1d = new_l1d;
  657. if (new_l1i)
  658. l1i = new_l1i;
  659. if (new_l2) {
  660. l2 = new_l2;
  661. #ifdef CONFIG_SMP
  662. per_cpu(cpu_llc_id, cpu) = l2_id;
  663. #endif
  664. }
  665. if (new_l3) {
  666. l3 = new_l3;
  667. #ifdef CONFIG_SMP
  668. per_cpu(cpu_llc_id, cpu) = l3_id;
  669. #endif
  670. }
  671. #ifdef CONFIG_SMP
  672. /*
  673. * If cpu_llc_id is not yet set, this means cpuid_level < 4 which in
  674. * turns means that the only possibility is SMT (as indicated in
  675. * cpuid1). Since cpuid2 doesn't specify shared caches, and we know
  676. * that SMT shares all caches, we can unconditionally set cpu_llc_id to
  677. * c->phys_proc_id.
  678. */
  679. if (per_cpu(cpu_llc_id, cpu) == BAD_APICID)
  680. per_cpu(cpu_llc_id, cpu) = c->phys_proc_id;
  681. #endif
  682. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  683. return l2;
  684. }
  685. static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
  686. struct _cpuid4_info_regs *base)
  687. {
  688. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  689. struct cacheinfo *this_leaf;
  690. int i, sibling;
  691. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  692. unsigned int apicid, nshared, first, last;
  693. this_leaf = this_cpu_ci->info_list + index;
  694. nshared = base->eax.split.num_threads_sharing + 1;
  695. apicid = cpu_data(cpu).apicid;
  696. first = apicid - (apicid % nshared);
  697. last = first + nshared - 1;
  698. for_each_online_cpu(i) {
  699. this_cpu_ci = get_cpu_cacheinfo(i);
  700. if (!this_cpu_ci->info_list)
  701. continue;
  702. apicid = cpu_data(i).apicid;
  703. if ((apicid < first) || (apicid > last))
  704. continue;
  705. this_leaf = this_cpu_ci->info_list + index;
  706. for_each_online_cpu(sibling) {
  707. apicid = cpu_data(sibling).apicid;
  708. if ((apicid < first) || (apicid > last))
  709. continue;
  710. cpumask_set_cpu(sibling,
  711. &this_leaf->shared_cpu_map);
  712. }
  713. }
  714. } else if (index == 3) {
  715. for_each_cpu(i, cpu_llc_shared_mask(cpu)) {
  716. this_cpu_ci = get_cpu_cacheinfo(i);
  717. if (!this_cpu_ci->info_list)
  718. continue;
  719. this_leaf = this_cpu_ci->info_list + index;
  720. for_each_cpu(sibling, cpu_llc_shared_mask(cpu)) {
  721. if (!cpu_online(sibling))
  722. continue;
  723. cpumask_set_cpu(sibling,
  724. &this_leaf->shared_cpu_map);
  725. }
  726. }
  727. } else
  728. return 0;
  729. return 1;
  730. }
  731. static void __cache_cpumap_setup(unsigned int cpu, int index,
  732. struct _cpuid4_info_regs *base)
  733. {
  734. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  735. struct cacheinfo *this_leaf, *sibling_leaf;
  736. unsigned long num_threads_sharing;
  737. int index_msb, i;
  738. struct cpuinfo_x86 *c = &cpu_data(cpu);
  739. if (c->x86_vendor == X86_VENDOR_AMD) {
  740. if (__cache_amd_cpumap_setup(cpu, index, base))
  741. return;
  742. }
  743. this_leaf = this_cpu_ci->info_list + index;
  744. num_threads_sharing = 1 + base->eax.split.num_threads_sharing;
  745. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  746. if (num_threads_sharing == 1)
  747. return;
  748. index_msb = get_count_order(num_threads_sharing);
  749. for_each_online_cpu(i)
  750. if (cpu_data(i).apicid >> index_msb == c->apicid >> index_msb) {
  751. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  752. if (i == cpu || !sib_cpu_ci->info_list)
  753. continue;/* skip if itself or no cacheinfo */
  754. sibling_leaf = sib_cpu_ci->info_list + index;
  755. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  756. cpumask_set_cpu(cpu, &sibling_leaf->shared_cpu_map);
  757. }
  758. }
  759. static void ci_leaf_init(struct cacheinfo *this_leaf,
  760. struct _cpuid4_info_regs *base)
  761. {
  762. this_leaf->level = base->eax.split.level;
  763. this_leaf->type = cache_type_map[base->eax.split.type];
  764. this_leaf->coherency_line_size =
  765. base->ebx.split.coherency_line_size + 1;
  766. this_leaf->ways_of_associativity =
  767. base->ebx.split.ways_of_associativity + 1;
  768. this_leaf->size = base->size;
  769. this_leaf->number_of_sets = base->ecx.split.number_of_sets + 1;
  770. this_leaf->physical_line_partition =
  771. base->ebx.split.physical_line_partition + 1;
  772. this_leaf->priv = base->nb;
  773. }
  774. static int __init_cache_level(unsigned int cpu)
  775. {
  776. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  777. if (!num_cache_leaves)
  778. return -ENOENT;
  779. if (!this_cpu_ci)
  780. return -EINVAL;
  781. this_cpu_ci->num_levels = 3;
  782. this_cpu_ci->num_leaves = num_cache_leaves;
  783. return 0;
  784. }
  785. static int __populate_cache_leaves(unsigned int cpu)
  786. {
  787. unsigned int idx, ret;
  788. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  789. struct cacheinfo *this_leaf = this_cpu_ci->info_list;
  790. struct _cpuid4_info_regs id4_regs = {};
  791. for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
  792. ret = cpuid4_cache_lookup_regs(idx, &id4_regs);
  793. if (ret)
  794. return ret;
  795. ci_leaf_init(this_leaf++, &id4_regs);
  796. __cache_cpumap_setup(cpu, idx, &id4_regs);
  797. }
  798. this_cpu_ci->cpu_map_populated = true;
  799. return 0;
  800. }
  801. DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
  802. DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)