intel_cacheinfo.c 26 KB

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