cacheinfo.c 28 KB

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