intel_cacheinfo.c 33 KB

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