intel_cacheinfo.c 27 KB

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