intel_cacheinfo.c 27 KB

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