intel_cacheinfo.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Routines to indentify 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/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/device.h>
  12. #include <linux/compiler.h>
  13. #include <linux/cpu.h>
  14. #include <linux/sched.h>
  15. #include <linux/pci.h>
  16. #include <asm/processor.h>
  17. #include <linux/smp.h>
  18. #include <asm/amd_nb.h>
  19. #include <asm/smp.h>
  20. #define LVL_1_INST 1
  21. #define LVL_1_DATA 2
  22. #define LVL_2 3
  23. #define LVL_3 4
  24. #define LVL_TRACE 5
  25. struct _cache_table {
  26. unsigned char descriptor;
  27. char cache_type;
  28. short size;
  29. };
  30. #define MB(x) ((x) * 1024)
  31. /* All the cache descriptor types we care about (no TLB or
  32. trace cache entries) */
  33. static const struct _cache_table __cpuinitconst cache_table[] =
  34. {
  35. { 0x06, LVL_1_INST, 8 }, /* 4-way set assoc, 32 byte line size */
  36. { 0x08, LVL_1_INST, 16 }, /* 4-way set assoc, 32 byte line size */
  37. { 0x09, LVL_1_INST, 32 }, /* 4-way set assoc, 64 byte line size */
  38. { 0x0a, LVL_1_DATA, 8 }, /* 2 way set assoc, 32 byte line size */
  39. { 0x0c, LVL_1_DATA, 16 }, /* 4-way set assoc, 32 byte line size */
  40. { 0x0d, LVL_1_DATA, 16 }, /* 4-way set assoc, 64 byte line size */
  41. { 0x0e, LVL_1_DATA, 24 }, /* 6-way set assoc, 64 byte line size */
  42. { 0x21, LVL_2, 256 }, /* 8-way set assoc, 64 byte line size */
  43. { 0x22, LVL_3, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  44. { 0x23, LVL_3, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  45. { 0x25, LVL_3, MB(2) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  46. { 0x29, LVL_3, MB(4) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  47. { 0x2c, LVL_1_DATA, 32 }, /* 8-way set assoc, 64 byte line size */
  48. { 0x30, LVL_1_INST, 32 }, /* 8-way set assoc, 64 byte line size */
  49. { 0x39, LVL_2, 128 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  50. { 0x3a, LVL_2, 192 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  51. { 0x3b, LVL_2, 128 }, /* 2-way set assoc, sectored cache, 64 byte line size */
  52. { 0x3c, LVL_2, 256 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  53. { 0x3d, LVL_2, 384 }, /* 6-way set assoc, sectored cache, 64 byte line size */
  54. { 0x3e, LVL_2, 512 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  55. { 0x3f, LVL_2, 256 }, /* 2-way set assoc, 64 byte line size */
  56. { 0x41, LVL_2, 128 }, /* 4-way set assoc, 32 byte line size */
  57. { 0x42, LVL_2, 256 }, /* 4-way set assoc, 32 byte line size */
  58. { 0x43, LVL_2, 512 }, /* 4-way set assoc, 32 byte line size */
  59. { 0x44, LVL_2, MB(1) }, /* 4-way set assoc, 32 byte line size */
  60. { 0x45, LVL_2, MB(2) }, /* 4-way set assoc, 32 byte line size */
  61. { 0x46, LVL_3, MB(4) }, /* 4-way set assoc, 64 byte line size */
  62. { 0x47, LVL_3, MB(8) }, /* 8-way set assoc, 64 byte line size */
  63. { 0x48, LVL_2, MB(3) }, /* 12-way set assoc, 64 byte line size */
  64. { 0x49, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  65. { 0x4a, LVL_3, MB(6) }, /* 12-way set assoc, 64 byte line size */
  66. { 0x4b, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  67. { 0x4c, LVL_3, MB(12) }, /* 12-way set assoc, 64 byte line size */
  68. { 0x4d, LVL_3, MB(16) }, /* 16-way set assoc, 64 byte line size */
  69. { 0x4e, LVL_2, MB(6) }, /* 24-way set assoc, 64 byte line size */
  70. { 0x60, LVL_1_DATA, 16 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  71. { 0x66, LVL_1_DATA, 8 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  72. { 0x67, LVL_1_DATA, 16 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  73. { 0x68, LVL_1_DATA, 32 }, /* 4-way set assoc, sectored cache, 64 byte line size */
  74. { 0x70, LVL_TRACE, 12 }, /* 8-way set assoc */
  75. { 0x71, LVL_TRACE, 16 }, /* 8-way set assoc */
  76. { 0x72, LVL_TRACE, 32 }, /* 8-way set assoc */
  77. { 0x73, LVL_TRACE, 64 }, /* 8-way set assoc */
  78. { 0x78, LVL_2, MB(1) }, /* 4-way set assoc, 64 byte line size */
  79. { 0x79, LVL_2, 128 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  80. { 0x7a, LVL_2, 256 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  81. { 0x7b, LVL_2, 512 }, /* 8-way set assoc, sectored cache, 64 byte line size */
  82. { 0x7c, LVL_2, MB(1) }, /* 8-way set assoc, sectored cache, 64 byte line size */
  83. { 0x7d, LVL_2, MB(2) }, /* 8-way set assoc, 64 byte line size */
  84. { 0x7f, LVL_2, 512 }, /* 2-way set assoc, 64 byte line size */
  85. { 0x80, LVL_2, 512 }, /* 8-way set assoc, 64 byte line size */
  86. { 0x82, LVL_2, 256 }, /* 8-way set assoc, 32 byte line size */
  87. { 0x83, LVL_2, 512 }, /* 8-way set assoc, 32 byte line size */
  88. { 0x84, LVL_2, MB(1) }, /* 8-way set assoc, 32 byte line size */
  89. { 0x85, LVL_2, MB(2) }, /* 8-way set assoc, 32 byte line size */
  90. { 0x86, LVL_2, 512 }, /* 4-way set assoc, 64 byte line size */
  91. { 0x87, LVL_2, MB(1) }, /* 8-way set assoc, 64 byte line size */
  92. { 0xd0, LVL_3, 512 }, /* 4-way set assoc, 64 byte line size */
  93. { 0xd1, LVL_3, MB(1) }, /* 4-way set assoc, 64 byte line size */
  94. { 0xd2, LVL_3, MB(2) }, /* 4-way set assoc, 64 byte line size */
  95. { 0xd6, LVL_3, MB(1) }, /* 8-way set assoc, 64 byte line size */
  96. { 0xd7, LVL_3, MB(2) }, /* 8-way set assoc, 64 byte line size */
  97. { 0xd8, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  98. { 0xdc, LVL_3, MB(2) }, /* 12-way set assoc, 64 byte line size */
  99. { 0xdd, LVL_3, MB(4) }, /* 12-way set assoc, 64 byte line size */
  100. { 0xde, LVL_3, MB(8) }, /* 12-way set assoc, 64 byte line size */
  101. { 0xe2, LVL_3, MB(2) }, /* 16-way set assoc, 64 byte line size */
  102. { 0xe3, LVL_3, MB(4) }, /* 16-way set assoc, 64 byte line size */
  103. { 0xe4, LVL_3, MB(8) }, /* 16-way set assoc, 64 byte line size */
  104. { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */
  105. { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */
  106. { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */
  107. { 0x00, 0, 0}
  108. };
  109. enum _cache_type {
  110. CACHE_TYPE_NULL = 0,
  111. CACHE_TYPE_DATA = 1,
  112. CACHE_TYPE_INST = 2,
  113. CACHE_TYPE_UNIFIED = 3
  114. };
  115. union _cpuid4_leaf_eax {
  116. struct {
  117. enum _cache_type type:5;
  118. unsigned int level:3;
  119. unsigned int is_self_initializing:1;
  120. unsigned int is_fully_associative:1;
  121. unsigned int reserved:4;
  122. unsigned int num_threads_sharing:12;
  123. unsigned int num_cores_on_die:6;
  124. } split;
  125. u32 full;
  126. };
  127. union _cpuid4_leaf_ebx {
  128. struct {
  129. unsigned int coherency_line_size:12;
  130. unsigned int physical_line_partition:10;
  131. unsigned int ways_of_associativity:10;
  132. } split;
  133. u32 full;
  134. };
  135. union _cpuid4_leaf_ecx {
  136. struct {
  137. unsigned int number_of_sets:32;
  138. } split;
  139. u32 full;
  140. };
  141. struct _cpuid4_info_regs {
  142. union _cpuid4_leaf_eax eax;
  143. union _cpuid4_leaf_ebx ebx;
  144. union _cpuid4_leaf_ecx ecx;
  145. unsigned long size;
  146. struct amd_northbridge *nb;
  147. };
  148. struct _cpuid4_info {
  149. struct _cpuid4_info_regs base;
  150. DECLARE_BITMAP(shared_cpu_map, NR_CPUS);
  151. };
  152. unsigned short num_cache_leaves;
  153. /* AMD doesn't have CPUID4. Emulate it here to report the same
  154. information to the user. This makes some assumptions about the machine:
  155. L2 not shared, no SMT etc. that is currently true on AMD CPUs.
  156. In theory the TLBs could be reported as fake type (they are in "dummy").
  157. Maybe later */
  158. union l1_cache {
  159. struct {
  160. unsigned line_size:8;
  161. unsigned lines_per_tag:8;
  162. unsigned assoc:8;
  163. unsigned size_in_kb:8;
  164. };
  165. unsigned val;
  166. };
  167. union l2_cache {
  168. struct {
  169. unsigned line_size:8;
  170. unsigned lines_per_tag:4;
  171. unsigned assoc:4;
  172. unsigned size_in_kb:16;
  173. };
  174. unsigned val;
  175. };
  176. union l3_cache {
  177. struct {
  178. unsigned line_size:8;
  179. unsigned lines_per_tag:4;
  180. unsigned assoc:4;
  181. unsigned res:2;
  182. unsigned size_encoded:14;
  183. };
  184. unsigned val;
  185. };
  186. static const unsigned short __cpuinitconst assocs[] = {
  187. [1] = 1,
  188. [2] = 2,
  189. [4] = 4,
  190. [6] = 8,
  191. [8] = 16,
  192. [0xa] = 32,
  193. [0xb] = 48,
  194. [0xc] = 64,
  195. [0xd] = 96,
  196. [0xe] = 128,
  197. [0xf] = 0xffff /* fully associative - no way to show this currently */
  198. };
  199. static const unsigned char __cpuinitconst levels[] = { 1, 1, 2, 3 };
  200. static const unsigned char __cpuinitconst types[] = { 1, 2, 3, 3 };
  201. static void __cpuinit
  202. amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,
  203. union _cpuid4_leaf_ebx *ebx,
  204. union _cpuid4_leaf_ecx *ecx)
  205. {
  206. unsigned dummy;
  207. unsigned line_size, lines_per_tag, assoc, size_in_kb;
  208. union l1_cache l1i, l1d;
  209. union l2_cache l2;
  210. union l3_cache l3;
  211. union l1_cache *l1 = &l1d;
  212. eax->full = 0;
  213. ebx->full = 0;
  214. ecx->full = 0;
  215. cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
  216. cpuid(0x80000006, &dummy, &dummy, &l2.val, &l3.val);
  217. switch (leaf) {
  218. case 1:
  219. l1 = &l1i;
  220. case 0:
  221. if (!l1->val)
  222. return;
  223. assoc = assocs[l1->assoc];
  224. line_size = l1->line_size;
  225. lines_per_tag = l1->lines_per_tag;
  226. size_in_kb = l1->size_in_kb;
  227. break;
  228. case 2:
  229. if (!l2.val)
  230. return;
  231. assoc = assocs[l2.assoc];
  232. line_size = l2.line_size;
  233. lines_per_tag = l2.lines_per_tag;
  234. /* cpu_data has errata corrections for K7 applied */
  235. size_in_kb = __this_cpu_read(cpu_info.x86_cache_size);
  236. break;
  237. case 3:
  238. if (!l3.val)
  239. return;
  240. assoc = assocs[l3.assoc];
  241. line_size = l3.line_size;
  242. lines_per_tag = l3.lines_per_tag;
  243. size_in_kb = l3.size_encoded * 512;
  244. if (boot_cpu_has(X86_FEATURE_AMD_DCM)) {
  245. size_in_kb = size_in_kb >> 1;
  246. assoc = assoc >> 1;
  247. }
  248. break;
  249. default:
  250. return;
  251. }
  252. eax->split.is_self_initializing = 1;
  253. eax->split.type = types[leaf];
  254. eax->split.level = levels[leaf];
  255. eax->split.num_threads_sharing = 0;
  256. eax->split.num_cores_on_die = __this_cpu_read(cpu_info.x86_max_cores) - 1;
  257. if (assoc == 0xffff)
  258. eax->split.is_fully_associative = 1;
  259. ebx->split.coherency_line_size = line_size - 1;
  260. ebx->split.ways_of_associativity = assoc - 1;
  261. ebx->split.physical_line_partition = lines_per_tag - 1;
  262. ecx->split.number_of_sets = (size_in_kb * 1024) / line_size /
  263. (ebx->split.ways_of_associativity + 1) - 1;
  264. }
  265. struct _cache_attr {
  266. struct attribute attr;
  267. ssize_t (*show)(struct _cpuid4_info *, char *, unsigned int);
  268. ssize_t (*store)(struct _cpuid4_info *, const char *, size_t count,
  269. unsigned int);
  270. };
  271. #ifdef CONFIG_AMD_NB
  272. /*
  273. * L3 cache descriptors
  274. */
  275. static void __cpuinit amd_calc_l3_indices(struct amd_northbridge *nb)
  276. {
  277. struct amd_l3_cache *l3 = &nb->l3_cache;
  278. unsigned int sc0, sc1, sc2, sc3;
  279. u32 val = 0;
  280. pci_read_config_dword(nb->misc, 0x1C4, &val);
  281. /* calculate subcache sizes */
  282. l3->subcaches[0] = sc0 = !(val & BIT(0));
  283. l3->subcaches[1] = sc1 = !(val & BIT(4));
  284. if (boot_cpu_data.x86 == 0x15) {
  285. l3->subcaches[0] = sc0 += !(val & BIT(1));
  286. l3->subcaches[1] = sc1 += !(val & BIT(5));
  287. }
  288. l3->subcaches[2] = sc2 = !(val & BIT(8)) + !(val & BIT(9));
  289. l3->subcaches[3] = sc3 = !(val & BIT(12)) + !(val & BIT(13));
  290. l3->indices = (max(max3(sc0, sc1, sc2), sc3) << 10) - 1;
  291. }
  292. static void __cpuinit amd_init_l3_cache(struct _cpuid4_info_regs *this_leaf,
  293. int index)
  294. {
  295. int node;
  296. /* only for L3, and not in virtualized environments */
  297. if (index < 3)
  298. return;
  299. node = amd_get_nb_id(smp_processor_id());
  300. this_leaf->nb = node_to_amd_nb(node);
  301. if (this_leaf->nb && !this_leaf->nb->l3_cache.indices)
  302. amd_calc_l3_indices(this_leaf->nb);
  303. }
  304. /*
  305. * check whether a slot used for disabling an L3 index is occupied.
  306. * @l3: L3 cache descriptor
  307. * @slot: slot number (0..1)
  308. *
  309. * @returns: the disabled index if used or negative value if slot free.
  310. */
  311. int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned slot)
  312. {
  313. unsigned int reg = 0;
  314. pci_read_config_dword(nb->misc, 0x1BC + slot * 4, &reg);
  315. /* check whether this slot is activated already */
  316. if (reg & (3UL << 30))
  317. return reg & 0xfff;
  318. return -1;
  319. }
  320. static ssize_t show_cache_disable(struct _cpuid4_info *this_leaf, char *buf,
  321. unsigned int slot)
  322. {
  323. int index;
  324. if (!this_leaf->base.nb || !amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  325. return -EINVAL;
  326. index = amd_get_l3_disable_slot(this_leaf->base.nb, slot);
  327. if (index >= 0)
  328. return sprintf(buf, "%d\n", index);
  329. return sprintf(buf, "FREE\n");
  330. }
  331. #define SHOW_CACHE_DISABLE(slot) \
  332. static ssize_t \
  333. show_cache_disable_##slot(struct _cpuid4_info *this_leaf, char *buf, \
  334. unsigned int cpu) \
  335. { \
  336. return show_cache_disable(this_leaf, buf, slot); \
  337. }
  338. SHOW_CACHE_DISABLE(0)
  339. SHOW_CACHE_DISABLE(1)
  340. static void amd_l3_disable_index(struct amd_northbridge *nb, int cpu,
  341. unsigned slot, unsigned long idx)
  342. {
  343. int i;
  344. idx |= BIT(30);
  345. /*
  346. * disable index in all 4 subcaches
  347. */
  348. for (i = 0; i < 4; i++) {
  349. u32 reg = idx | (i << 20);
  350. if (!nb->l3_cache.subcaches[i])
  351. continue;
  352. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  353. /*
  354. * We need to WBINVD on a core on the node containing the L3
  355. * cache which indices we disable therefore a simple wbinvd()
  356. * is not sufficient.
  357. */
  358. wbinvd_on_cpu(cpu);
  359. reg |= BIT(31);
  360. pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
  361. }
  362. }
  363. /*
  364. * disable a L3 cache index by using a disable-slot
  365. *
  366. * @l3: L3 cache descriptor
  367. * @cpu: A CPU on the node containing the L3 cache
  368. * @slot: slot number (0..1)
  369. * @index: index to disable
  370. *
  371. * @return: 0 on success, error status on failure
  372. */
  373. int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu, unsigned slot,
  374. unsigned long index)
  375. {
  376. int ret = 0;
  377. /* check if @slot is already used or the index is already disabled */
  378. ret = amd_get_l3_disable_slot(nb, slot);
  379. if (ret >= 0)
  380. return -EINVAL;
  381. if (index > nb->l3_cache.indices)
  382. return -EINVAL;
  383. /* check whether the other slot has disabled the same index already */
  384. if (index == amd_get_l3_disable_slot(nb, !slot))
  385. return -EINVAL;
  386. amd_l3_disable_index(nb, cpu, slot, index);
  387. return 0;
  388. }
  389. static ssize_t store_cache_disable(struct _cpuid4_info *this_leaf,
  390. const char *buf, size_t count,
  391. unsigned int slot)
  392. {
  393. unsigned long val = 0;
  394. int cpu, err = 0;
  395. if (!capable(CAP_SYS_ADMIN))
  396. return -EPERM;
  397. if (!this_leaf->base.nb || !amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  398. return -EINVAL;
  399. cpu = cpumask_first(to_cpumask(this_leaf->shared_cpu_map));
  400. if (strict_strtoul(buf, 10, &val) < 0)
  401. return -EINVAL;
  402. err = amd_set_l3_disable_slot(this_leaf->base.nb, cpu, slot, val);
  403. if (err) {
  404. if (err == -EEXIST)
  405. printk(KERN_WARNING "L3 disable slot %d in use!\n",
  406. slot);
  407. return err;
  408. }
  409. return count;
  410. }
  411. #define STORE_CACHE_DISABLE(slot) \
  412. static ssize_t \
  413. store_cache_disable_##slot(struct _cpuid4_info *this_leaf, \
  414. const char *buf, size_t count, \
  415. unsigned int cpu) \
  416. { \
  417. return store_cache_disable(this_leaf, buf, count, slot); \
  418. }
  419. STORE_CACHE_DISABLE(0)
  420. STORE_CACHE_DISABLE(1)
  421. static struct _cache_attr cache_disable_0 = __ATTR(cache_disable_0, 0644,
  422. show_cache_disable_0, store_cache_disable_0);
  423. static struct _cache_attr cache_disable_1 = __ATTR(cache_disable_1, 0644,
  424. show_cache_disable_1, store_cache_disable_1);
  425. static ssize_t
  426. show_subcaches(struct _cpuid4_info *this_leaf, char *buf, unsigned int cpu)
  427. {
  428. if (!this_leaf->base.nb || !amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  429. return -EINVAL;
  430. return sprintf(buf, "%x\n", amd_get_subcaches(cpu));
  431. }
  432. static ssize_t
  433. store_subcaches(struct _cpuid4_info *this_leaf, const char *buf, size_t count,
  434. unsigned int cpu)
  435. {
  436. unsigned long val;
  437. if (!capable(CAP_SYS_ADMIN))
  438. return -EPERM;
  439. if (!this_leaf->base.nb || !amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  440. return -EINVAL;
  441. if (strict_strtoul(buf, 16, &val) < 0)
  442. return -EINVAL;
  443. if (amd_set_subcaches(cpu, val))
  444. return -EINVAL;
  445. return count;
  446. }
  447. static struct _cache_attr subcaches =
  448. __ATTR(subcaches, 0644, show_subcaches, store_subcaches);
  449. #else /* CONFIG_AMD_NB */
  450. #define amd_init_l3_cache(x, y)
  451. #endif /* CONFIG_AMD_NB */
  452. static int
  453. __cpuinit cpuid4_cache_lookup_regs(int index,
  454. struct _cpuid4_info_regs *this_leaf)
  455. {
  456. union _cpuid4_leaf_eax eax;
  457. union _cpuid4_leaf_ebx ebx;
  458. union _cpuid4_leaf_ecx ecx;
  459. unsigned edx;
  460. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
  461. amd_cpuid4(index, &eax, &ebx, &ecx);
  462. amd_init_l3_cache(this_leaf, index);
  463. } else {
  464. cpuid_count(4, index, &eax.full, &ebx.full, &ecx.full, &edx);
  465. }
  466. if (eax.split.type == CACHE_TYPE_NULL)
  467. return -EIO; /* better error ? */
  468. this_leaf->eax = eax;
  469. this_leaf->ebx = ebx;
  470. this_leaf->ecx = ecx;
  471. this_leaf->size = (ecx.split.number_of_sets + 1) *
  472. (ebx.split.coherency_line_size + 1) *
  473. (ebx.split.physical_line_partition + 1) *
  474. (ebx.split.ways_of_associativity + 1);
  475. return 0;
  476. }
  477. static int __cpuinit find_num_cache_leaves(void)
  478. {
  479. unsigned int eax, ebx, ecx, edx;
  480. union _cpuid4_leaf_eax cache_eax;
  481. int i = -1;
  482. do {
  483. ++i;
  484. /* Do cpuid(4) loop to find out num_cache_leaves */
  485. cpuid_count(4, i, &eax, &ebx, &ecx, &edx);
  486. cache_eax.full = eax;
  487. } while (cache_eax.split.type != CACHE_TYPE_NULL);
  488. return i;
  489. }
  490. unsigned int __cpuinit init_intel_cacheinfo(struct cpuinfo_x86 *c)
  491. {
  492. /* Cache sizes */
  493. unsigned int trace = 0, l1i = 0, l1d = 0, l2 = 0, l3 = 0;
  494. unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
  495. unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
  496. unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
  497. #ifdef CONFIG_X86_HT
  498. unsigned int cpu = c->cpu_index;
  499. #endif
  500. if (c->cpuid_level > 3) {
  501. static int is_initialized;
  502. if (is_initialized == 0) {
  503. /* Init num_cache_leaves from boot CPU */
  504. num_cache_leaves = find_num_cache_leaves();
  505. is_initialized++;
  506. }
  507. /*
  508. * Whenever possible use cpuid(4), deterministic cache
  509. * parameters cpuid leaf to find the cache details
  510. */
  511. for (i = 0; i < num_cache_leaves; i++) {
  512. struct _cpuid4_info_regs this_leaf;
  513. int retval;
  514. retval = cpuid4_cache_lookup_regs(i, &this_leaf);
  515. if (retval >= 0) {
  516. switch (this_leaf.eax.split.level) {
  517. case 1:
  518. if (this_leaf.eax.split.type ==
  519. CACHE_TYPE_DATA)
  520. new_l1d = this_leaf.size/1024;
  521. else if (this_leaf.eax.split.type ==
  522. CACHE_TYPE_INST)
  523. new_l1i = this_leaf.size/1024;
  524. break;
  525. case 2:
  526. new_l2 = this_leaf.size/1024;
  527. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  528. index_msb = get_count_order(num_threads_sharing);
  529. l2_id = c->apicid >> index_msb;
  530. break;
  531. case 3:
  532. new_l3 = this_leaf.size/1024;
  533. num_threads_sharing = 1 + this_leaf.eax.split.num_threads_sharing;
  534. index_msb = get_count_order(
  535. num_threads_sharing);
  536. l3_id = c->apicid >> index_msb;
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. }
  543. }
  544. /*
  545. * Don't use cpuid2 if cpuid4 is supported. For P4, we use cpuid2 for
  546. * trace cache
  547. */
  548. if ((num_cache_leaves == 0 || c->x86 == 15) && c->cpuid_level > 1) {
  549. /* supports eax=2 call */
  550. int j, n;
  551. unsigned int regs[4];
  552. unsigned char *dp = (unsigned char *)regs;
  553. int only_trace = 0;
  554. if (num_cache_leaves != 0 && c->x86 == 15)
  555. only_trace = 1;
  556. /* Number of times to iterate */
  557. n = cpuid_eax(2) & 0xFF;
  558. for (i = 0 ; i < n ; i++) {
  559. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  560. /* If bit 31 is set, this is an unknown format */
  561. for (j = 0 ; j < 3 ; j++)
  562. if (regs[j] & (1 << 31))
  563. regs[j] = 0;
  564. /* Byte 0 is level count, not a descriptor */
  565. for (j = 1 ; j < 16 ; j++) {
  566. unsigned char des = dp[j];
  567. unsigned char k = 0;
  568. /* look up this descriptor in the table */
  569. while (cache_table[k].descriptor != 0) {
  570. if (cache_table[k].descriptor == des) {
  571. if (only_trace && cache_table[k].cache_type != LVL_TRACE)
  572. break;
  573. switch (cache_table[k].cache_type) {
  574. case LVL_1_INST:
  575. l1i += cache_table[k].size;
  576. break;
  577. case LVL_1_DATA:
  578. l1d += cache_table[k].size;
  579. break;
  580. case LVL_2:
  581. l2 += cache_table[k].size;
  582. break;
  583. case LVL_3:
  584. l3 += cache_table[k].size;
  585. break;
  586. case LVL_TRACE:
  587. trace += cache_table[k].size;
  588. break;
  589. }
  590. break;
  591. }
  592. k++;
  593. }
  594. }
  595. }
  596. }
  597. if (new_l1d)
  598. l1d = new_l1d;
  599. if (new_l1i)
  600. l1i = new_l1i;
  601. if (new_l2) {
  602. l2 = new_l2;
  603. #ifdef CONFIG_X86_HT
  604. per_cpu(cpu_llc_id, cpu) = l2_id;
  605. #endif
  606. }
  607. if (new_l3) {
  608. l3 = new_l3;
  609. #ifdef CONFIG_X86_HT
  610. per_cpu(cpu_llc_id, cpu) = l3_id;
  611. #endif
  612. }
  613. c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
  614. return l2;
  615. }
  616. #ifdef CONFIG_SYSFS
  617. /* pointer to _cpuid4_info array (for each cache leaf) */
  618. static DEFINE_PER_CPU(struct _cpuid4_info *, ici_cpuid4_info);
  619. #define CPUID4_INFO_IDX(x, y) (&((per_cpu(ici_cpuid4_info, x))[y]))
  620. #ifdef CONFIG_SMP
  621. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  622. {
  623. struct _cpuid4_info *this_leaf, *sibling_leaf;
  624. unsigned long num_threads_sharing;
  625. int index_msb, i, sibling;
  626. struct cpuinfo_x86 *c = &cpu_data(cpu);
  627. if ((index == 3) && (c->x86_vendor == X86_VENDOR_AMD)) {
  628. for_each_cpu(i, cpu_llc_shared_mask(cpu)) {
  629. if (!per_cpu(ici_cpuid4_info, i))
  630. continue;
  631. this_leaf = CPUID4_INFO_IDX(i, index);
  632. for_each_cpu(sibling, cpu_llc_shared_mask(cpu)) {
  633. if (!cpu_online(sibling))
  634. continue;
  635. set_bit(sibling, this_leaf->shared_cpu_map);
  636. }
  637. }
  638. return;
  639. }
  640. this_leaf = CPUID4_INFO_IDX(cpu, index);
  641. num_threads_sharing = 1 + this_leaf->base.eax.split.num_threads_sharing;
  642. if (num_threads_sharing == 1)
  643. cpumask_set_cpu(cpu, to_cpumask(this_leaf->shared_cpu_map));
  644. else {
  645. index_msb = get_count_order(num_threads_sharing);
  646. for_each_online_cpu(i) {
  647. if (cpu_data(i).apicid >> index_msb ==
  648. c->apicid >> index_msb) {
  649. cpumask_set_cpu(i,
  650. to_cpumask(this_leaf->shared_cpu_map));
  651. if (i != cpu && per_cpu(ici_cpuid4_info, i)) {
  652. sibling_leaf =
  653. CPUID4_INFO_IDX(i, index);
  654. cpumask_set_cpu(cpu, to_cpumask(
  655. sibling_leaf->shared_cpu_map));
  656. }
  657. }
  658. }
  659. }
  660. }
  661. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  662. {
  663. struct _cpuid4_info *this_leaf, *sibling_leaf;
  664. int sibling;
  665. this_leaf = CPUID4_INFO_IDX(cpu, index);
  666. for_each_cpu(sibling, to_cpumask(this_leaf->shared_cpu_map)) {
  667. sibling_leaf = CPUID4_INFO_IDX(sibling, index);
  668. cpumask_clear_cpu(cpu,
  669. to_cpumask(sibling_leaf->shared_cpu_map));
  670. }
  671. }
  672. #else
  673. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
  674. {
  675. }
  676. static void __cpuinit cache_remove_shared_cpu_map(unsigned int cpu, int index)
  677. {
  678. }
  679. #endif
  680. static void __cpuinit free_cache_attributes(unsigned int cpu)
  681. {
  682. int i;
  683. for (i = 0; i < num_cache_leaves; i++)
  684. cache_remove_shared_cpu_map(cpu, i);
  685. kfree(per_cpu(ici_cpuid4_info, cpu));
  686. per_cpu(ici_cpuid4_info, cpu) = NULL;
  687. }
  688. static void __cpuinit get_cpu_leaves(void *_retval)
  689. {
  690. int j, *retval = _retval, cpu = smp_processor_id();
  691. /* Do cpuid and store the results */
  692. for (j = 0; j < num_cache_leaves; j++) {
  693. struct _cpuid4_info *this_leaf = CPUID4_INFO_IDX(cpu, j);
  694. *retval = cpuid4_cache_lookup_regs(j, &this_leaf->base);
  695. if (unlikely(*retval < 0)) {
  696. int i;
  697. for (i = 0; i < j; i++)
  698. cache_remove_shared_cpu_map(cpu, i);
  699. break;
  700. }
  701. cache_shared_cpu_map_setup(cpu, j);
  702. }
  703. }
  704. static int __cpuinit detect_cache_attributes(unsigned int cpu)
  705. {
  706. int retval;
  707. if (num_cache_leaves == 0)
  708. return -ENOENT;
  709. per_cpu(ici_cpuid4_info, cpu) = kzalloc(
  710. sizeof(struct _cpuid4_info) * num_cache_leaves, GFP_KERNEL);
  711. if (per_cpu(ici_cpuid4_info, cpu) == NULL)
  712. return -ENOMEM;
  713. smp_call_function_single(cpu, get_cpu_leaves, &retval, true);
  714. if (retval) {
  715. kfree(per_cpu(ici_cpuid4_info, cpu));
  716. per_cpu(ici_cpuid4_info, cpu) = NULL;
  717. }
  718. return retval;
  719. }
  720. #include <linux/kobject.h>
  721. #include <linux/sysfs.h>
  722. extern struct sysdev_class cpu_sysdev_class; /* from drivers/base/cpu.c */
  723. /* pointer to kobject for cpuX/cache */
  724. static DEFINE_PER_CPU(struct kobject *, ici_cache_kobject);
  725. struct _index_kobject {
  726. struct kobject kobj;
  727. unsigned int cpu;
  728. unsigned short index;
  729. };
  730. /* pointer to array of kobjects for cpuX/cache/indexY */
  731. static DEFINE_PER_CPU(struct _index_kobject *, ici_index_kobject);
  732. #define INDEX_KOBJECT_PTR(x, y) (&((per_cpu(ici_index_kobject, x))[y]))
  733. #define show_one_plus(file_name, object, val) \
  734. static ssize_t show_##file_name(struct _cpuid4_info *this_leaf, char *buf, \
  735. unsigned int cpu) \
  736. { \
  737. return sprintf(buf, "%lu\n", (unsigned long)this_leaf->object + val); \
  738. }
  739. show_one_plus(level, base.eax.split.level, 0);
  740. show_one_plus(coherency_line_size, base.ebx.split.coherency_line_size, 1);
  741. show_one_plus(physical_line_partition, base.ebx.split.physical_line_partition, 1);
  742. show_one_plus(ways_of_associativity, base.ebx.split.ways_of_associativity, 1);
  743. show_one_plus(number_of_sets, base.ecx.split.number_of_sets, 1);
  744. static ssize_t show_size(struct _cpuid4_info *this_leaf, char *buf,
  745. unsigned int cpu)
  746. {
  747. return sprintf(buf, "%luK\n", this_leaf->base.size / 1024);
  748. }
  749. static ssize_t show_shared_cpu_map_func(struct _cpuid4_info *this_leaf,
  750. int type, char *buf)
  751. {
  752. ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
  753. int n = 0;
  754. if (len > 1) {
  755. const struct cpumask *mask;
  756. mask = to_cpumask(this_leaf->shared_cpu_map);
  757. n = type ?
  758. cpulist_scnprintf(buf, len-2, mask) :
  759. cpumask_scnprintf(buf, len-2, mask);
  760. buf[n++] = '\n';
  761. buf[n] = '\0';
  762. }
  763. return n;
  764. }
  765. static inline ssize_t show_shared_cpu_map(struct _cpuid4_info *leaf, char *buf,
  766. unsigned int cpu)
  767. {
  768. return show_shared_cpu_map_func(leaf, 0, buf);
  769. }
  770. static inline ssize_t show_shared_cpu_list(struct _cpuid4_info *leaf, char *buf,
  771. unsigned int cpu)
  772. {
  773. return show_shared_cpu_map_func(leaf, 1, buf);
  774. }
  775. static ssize_t show_type(struct _cpuid4_info *this_leaf, char *buf,
  776. unsigned int cpu)
  777. {
  778. switch (this_leaf->base.eax.split.type) {
  779. case CACHE_TYPE_DATA:
  780. return sprintf(buf, "Data\n");
  781. case CACHE_TYPE_INST:
  782. return sprintf(buf, "Instruction\n");
  783. case CACHE_TYPE_UNIFIED:
  784. return sprintf(buf, "Unified\n");
  785. default:
  786. return sprintf(buf, "Unknown\n");
  787. }
  788. }
  789. #define to_object(k) container_of(k, struct _index_kobject, kobj)
  790. #define to_attr(a) container_of(a, struct _cache_attr, attr)
  791. #define define_one_ro(_name) \
  792. static struct _cache_attr _name = \
  793. __ATTR(_name, 0444, show_##_name, NULL)
  794. define_one_ro(level);
  795. define_one_ro(type);
  796. define_one_ro(coherency_line_size);
  797. define_one_ro(physical_line_partition);
  798. define_one_ro(ways_of_associativity);
  799. define_one_ro(number_of_sets);
  800. define_one_ro(size);
  801. define_one_ro(shared_cpu_map);
  802. define_one_ro(shared_cpu_list);
  803. static struct attribute *default_attrs[] = {
  804. &type.attr,
  805. &level.attr,
  806. &coherency_line_size.attr,
  807. &physical_line_partition.attr,
  808. &ways_of_associativity.attr,
  809. &number_of_sets.attr,
  810. &size.attr,
  811. &shared_cpu_map.attr,
  812. &shared_cpu_list.attr,
  813. NULL
  814. };
  815. #ifdef CONFIG_AMD_NB
  816. static struct attribute ** __cpuinit amd_l3_attrs(void)
  817. {
  818. static struct attribute **attrs;
  819. int n;
  820. if (attrs)
  821. return attrs;
  822. n = sizeof (default_attrs) / sizeof (struct attribute *);
  823. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE))
  824. n += 2;
  825. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  826. n += 1;
  827. attrs = kzalloc(n * sizeof (struct attribute *), GFP_KERNEL);
  828. if (attrs == NULL)
  829. return attrs = default_attrs;
  830. for (n = 0; default_attrs[n]; n++)
  831. attrs[n] = default_attrs[n];
  832. if (amd_nb_has_feature(AMD_NB_L3_INDEX_DISABLE)) {
  833. attrs[n++] = &cache_disable_0.attr;
  834. attrs[n++] = &cache_disable_1.attr;
  835. }
  836. if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  837. attrs[n++] = &subcaches.attr;
  838. return attrs;
  839. }
  840. #endif
  841. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  842. {
  843. struct _cache_attr *fattr = to_attr(attr);
  844. struct _index_kobject *this_leaf = to_object(kobj);
  845. ssize_t ret;
  846. ret = fattr->show ?
  847. fattr->show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  848. buf, this_leaf->cpu) :
  849. 0;
  850. return ret;
  851. }
  852. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  853. const char *buf, size_t count)
  854. {
  855. struct _cache_attr *fattr = to_attr(attr);
  856. struct _index_kobject *this_leaf = to_object(kobj);
  857. ssize_t ret;
  858. ret = fattr->store ?
  859. fattr->store(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
  860. buf, count, this_leaf->cpu) :
  861. 0;
  862. return ret;
  863. }
  864. static const struct sysfs_ops sysfs_ops = {
  865. .show = show,
  866. .store = store,
  867. };
  868. static struct kobj_type ktype_cache = {
  869. .sysfs_ops = &sysfs_ops,
  870. .default_attrs = default_attrs,
  871. };
  872. static struct kobj_type ktype_percpu_entry = {
  873. .sysfs_ops = &sysfs_ops,
  874. };
  875. static void __cpuinit cpuid4_cache_sysfs_exit(unsigned int cpu)
  876. {
  877. kfree(per_cpu(ici_cache_kobject, cpu));
  878. kfree(per_cpu(ici_index_kobject, cpu));
  879. per_cpu(ici_cache_kobject, cpu) = NULL;
  880. per_cpu(ici_index_kobject, cpu) = NULL;
  881. free_cache_attributes(cpu);
  882. }
  883. static int __cpuinit cpuid4_cache_sysfs_init(unsigned int cpu)
  884. {
  885. int err;
  886. if (num_cache_leaves == 0)
  887. return -ENOENT;
  888. err = detect_cache_attributes(cpu);
  889. if (err)
  890. return err;
  891. /* Allocate all required memory */
  892. per_cpu(ici_cache_kobject, cpu) =
  893. kzalloc(sizeof(struct kobject), GFP_KERNEL);
  894. if (unlikely(per_cpu(ici_cache_kobject, cpu) == NULL))
  895. goto err_out;
  896. per_cpu(ici_index_kobject, cpu) = kzalloc(
  897. sizeof(struct _index_kobject) * num_cache_leaves, GFP_KERNEL);
  898. if (unlikely(per_cpu(ici_index_kobject, cpu) == NULL))
  899. goto err_out;
  900. return 0;
  901. err_out:
  902. cpuid4_cache_sysfs_exit(cpu);
  903. return -ENOMEM;
  904. }
  905. static DECLARE_BITMAP(cache_dev_map, NR_CPUS);
  906. /* Add/Remove cache interface for CPU device */
  907. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  908. {
  909. unsigned int cpu = sys_dev->id;
  910. unsigned long i, j;
  911. struct _index_kobject *this_object;
  912. struct _cpuid4_info *this_leaf;
  913. int retval;
  914. retval = cpuid4_cache_sysfs_init(cpu);
  915. if (unlikely(retval < 0))
  916. return retval;
  917. retval = kobject_init_and_add(per_cpu(ici_cache_kobject, cpu),
  918. &ktype_percpu_entry,
  919. &sys_dev->kobj, "%s", "cache");
  920. if (retval < 0) {
  921. cpuid4_cache_sysfs_exit(cpu);
  922. return retval;
  923. }
  924. for (i = 0; i < num_cache_leaves; i++) {
  925. this_object = INDEX_KOBJECT_PTR(cpu, i);
  926. this_object->cpu = cpu;
  927. this_object->index = i;
  928. this_leaf = CPUID4_INFO_IDX(cpu, i);
  929. ktype_cache.default_attrs = default_attrs;
  930. #ifdef CONFIG_AMD_NB
  931. if (this_leaf->base.nb)
  932. ktype_cache.default_attrs = amd_l3_attrs();
  933. #endif
  934. retval = kobject_init_and_add(&(this_object->kobj),
  935. &ktype_cache,
  936. per_cpu(ici_cache_kobject, cpu),
  937. "index%1lu", i);
  938. if (unlikely(retval)) {
  939. for (j = 0; j < i; j++)
  940. kobject_put(&(INDEX_KOBJECT_PTR(cpu, j)->kobj));
  941. kobject_put(per_cpu(ici_cache_kobject, cpu));
  942. cpuid4_cache_sysfs_exit(cpu);
  943. return retval;
  944. }
  945. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  946. }
  947. cpumask_set_cpu(cpu, to_cpumask(cache_dev_map));
  948. kobject_uevent(per_cpu(ici_cache_kobject, cpu), KOBJ_ADD);
  949. return 0;
  950. }
  951. static void __cpuinit cache_remove_dev(struct sys_device * sys_dev)
  952. {
  953. unsigned int cpu = sys_dev->id;
  954. unsigned long i;
  955. if (per_cpu(ici_cpuid4_info, cpu) == NULL)
  956. return;
  957. if (!cpumask_test_cpu(cpu, to_cpumask(cache_dev_map)))
  958. return;
  959. cpumask_clear_cpu(cpu, to_cpumask(cache_dev_map));
  960. for (i = 0; i < num_cache_leaves; i++)
  961. kobject_put(&(INDEX_KOBJECT_PTR(cpu, i)->kobj));
  962. kobject_put(per_cpu(ici_cache_kobject, cpu));
  963. cpuid4_cache_sysfs_exit(cpu);
  964. }
  965. static int __cpuinit cacheinfo_cpu_callback(struct notifier_block *nfb,
  966. unsigned long action, void *hcpu)
  967. {
  968. unsigned int cpu = (unsigned long)hcpu;
  969. struct sys_device *sys_dev;
  970. sys_dev = get_cpu_sysdev(cpu);
  971. switch (action) {
  972. case CPU_ONLINE:
  973. case CPU_ONLINE_FROZEN:
  974. cache_add_dev(sys_dev);
  975. break;
  976. case CPU_DEAD:
  977. case CPU_DEAD_FROZEN:
  978. cache_remove_dev(sys_dev);
  979. break;
  980. }
  981. return NOTIFY_OK;
  982. }
  983. static struct notifier_block __cpuinitdata cacheinfo_cpu_notifier = {
  984. .notifier_call = cacheinfo_cpu_callback,
  985. };
  986. static int __cpuinit cache_sysfs_init(void)
  987. {
  988. int i;
  989. if (num_cache_leaves == 0)
  990. return 0;
  991. for_each_online_cpu(i) {
  992. int err;
  993. struct sys_device *sys_dev = get_cpu_sysdev(i);
  994. err = cache_add_dev(sys_dev);
  995. if (err)
  996. return err;
  997. }
  998. register_hotcpu_notifier(&cacheinfo_cpu_notifier);
  999. return 0;
  1000. }
  1001. device_initcall(cache_sysfs_init);
  1002. #endif