intel_cacheinfo.c 32 KB

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