centaur.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/bitops.h>
  4. #include <asm/processor.h>
  5. #include <asm/msr.h>
  6. #include <asm/e820.h>
  7. #include "cpu.h"
  8. #ifdef CONFIG_X86_OOSTORE
  9. static u32 __init power2(u32 x)
  10. {
  11. u32 s=1;
  12. while(s<=x)
  13. s<<=1;
  14. return s>>=1;
  15. }
  16. /*
  17. * Set up an actual MCR
  18. */
  19. static void __init centaur_mcr_insert(int reg, u32 base, u32 size, int key)
  20. {
  21. u32 lo, hi;
  22. hi = base & ~0xFFF;
  23. lo = ~(size-1); /* Size is a power of 2 so this makes a mask */
  24. lo &= ~0xFFF; /* Remove the ctrl value bits */
  25. lo |= key; /* Attribute we wish to set */
  26. wrmsr(reg+MSR_IDT_MCR0, lo, hi);
  27. mtrr_centaur_report_mcr(reg, lo, hi); /* Tell the mtrr driver */
  28. }
  29. /*
  30. * Figure what we can cover with MCR's
  31. *
  32. * Shortcut: We know you can't put 4Gig of RAM on a winchip
  33. */
  34. static u32 __init ramtop(void) /* 16388 */
  35. {
  36. int i;
  37. u32 top = 0;
  38. u32 clip = 0xFFFFFFFFUL;
  39. for (i = 0; i < e820.nr_map; i++) {
  40. unsigned long start, end;
  41. if (e820.map[i].addr > 0xFFFFFFFFUL)
  42. continue;
  43. /*
  44. * Don't MCR over reserved space. Ignore the ISA hole
  45. * we frob around that catastrophy already
  46. */
  47. if (e820.map[i].type == E820_RESERVED)
  48. {
  49. if(e820.map[i].addr >= 0x100000UL && e820.map[i].addr < clip)
  50. clip = e820.map[i].addr;
  51. continue;
  52. }
  53. start = e820.map[i].addr;
  54. end = e820.map[i].addr + e820.map[i].size;
  55. if (start >= end)
  56. continue;
  57. if (end > top)
  58. top = end;
  59. }
  60. /* Everything below 'top' should be RAM except for the ISA hole.
  61. Because of the limited MCR's we want to map NV/ACPI into our
  62. MCR range for gunk in RAM
  63. Clip might cause us to MCR insufficient RAM but that is an
  64. acceptable failure mode and should only bite obscure boxes with
  65. a VESA hole at 15Mb
  66. The second case Clip sometimes kicks in is when the EBDA is marked
  67. as reserved. Again we fail safe with reasonable results
  68. */
  69. if(top>clip)
  70. top=clip;
  71. return top;
  72. }
  73. /*
  74. * Compute a set of MCR's to give maximum coverage
  75. */
  76. static int __init centaur_mcr_compute(int nr, int key)
  77. {
  78. u32 mem = ramtop();
  79. u32 root = power2(mem);
  80. u32 base = root;
  81. u32 top = root;
  82. u32 floor = 0;
  83. int ct = 0;
  84. while(ct<nr)
  85. {
  86. u32 fspace = 0;
  87. /*
  88. * Find the largest block we will fill going upwards
  89. */
  90. u32 high = power2(mem-top);
  91. /*
  92. * Find the largest block we will fill going downwards
  93. */
  94. u32 low = base/2;
  95. /*
  96. * Don't fill below 1Mb going downwards as there
  97. * is an ISA hole in the way.
  98. */
  99. if(base <= 1024*1024)
  100. low = 0;
  101. /*
  102. * See how much space we could cover by filling below
  103. * the ISA hole
  104. */
  105. if(floor == 0)
  106. fspace = 512*1024;
  107. else if(floor ==512*1024)
  108. fspace = 128*1024;
  109. /* And forget ROM space */
  110. /*
  111. * Now install the largest coverage we get
  112. */
  113. if(fspace > high && fspace > low)
  114. {
  115. centaur_mcr_insert(ct, floor, fspace, key);
  116. floor += fspace;
  117. }
  118. else if(high > low)
  119. {
  120. centaur_mcr_insert(ct, top, high, key);
  121. top += high;
  122. }
  123. else if(low > 0)
  124. {
  125. base -= low;
  126. centaur_mcr_insert(ct, base, low, key);
  127. }
  128. else break;
  129. ct++;
  130. }
  131. /*
  132. * We loaded ct values. We now need to set the mask. The caller
  133. * must do this bit.
  134. */
  135. return ct;
  136. }
  137. static void __init centaur_create_optimal_mcr(void)
  138. {
  139. int i;
  140. /*
  141. * Allocate up to 6 mcrs to mark as much of ram as possible
  142. * as write combining and weak write ordered.
  143. *
  144. * To experiment with: Linux never uses stack operations for
  145. * mmio spaces so we could globally enable stack operation wc
  146. *
  147. * Load the registers with type 31 - full write combining, all
  148. * writes weakly ordered.
  149. */
  150. int used = centaur_mcr_compute(6, 31);
  151. /*
  152. * Wipe unused MCRs
  153. */
  154. for(i=used;i<8;i++)
  155. wrmsr(MSR_IDT_MCR0+i, 0, 0);
  156. }
  157. static void __init winchip2_create_optimal_mcr(void)
  158. {
  159. u32 lo, hi;
  160. int i;
  161. /*
  162. * Allocate up to 6 mcrs to mark as much of ram as possible
  163. * as write combining, weak store ordered.
  164. *
  165. * Load the registers with type 25
  166. * 8 - weak write ordering
  167. * 16 - weak read ordering
  168. * 1 - write combining
  169. */
  170. int used = centaur_mcr_compute(6, 25);
  171. /*
  172. * Mark the registers we are using.
  173. */
  174. rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
  175. for(i=0;i<used;i++)
  176. lo|=1<<(9+i);
  177. wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
  178. /*
  179. * Wipe unused MCRs
  180. */
  181. for(i=used;i<8;i++)
  182. wrmsr(MSR_IDT_MCR0+i, 0, 0);
  183. }
  184. /*
  185. * Handle the MCR key on the Winchip 2.
  186. */
  187. static void __init winchip2_unprotect_mcr(void)
  188. {
  189. u32 lo, hi;
  190. u32 key;
  191. rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
  192. lo&=~0x1C0; /* blank bits 8-6 */
  193. key = (lo>>17) & 7;
  194. lo |= key<<6; /* replace with unlock key */
  195. wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
  196. }
  197. static void __init winchip2_protect_mcr(void)
  198. {
  199. u32 lo, hi;
  200. rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
  201. lo&=~0x1C0; /* blank bits 8-6 */
  202. wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
  203. }
  204. #endif /* CONFIG_X86_OOSTORE */
  205. #define ACE_PRESENT (1 << 6)
  206. #define ACE_ENABLED (1 << 7)
  207. #define ACE_FCR (1 << 28) /* MSR_VIA_FCR */
  208. #define RNG_PRESENT (1 << 2)
  209. #define RNG_ENABLED (1 << 3)
  210. #define RNG_ENABLE (1 << 6) /* MSR_VIA_RNG */
  211. static void __init init_c3(struct cpuinfo_x86 *c)
  212. {
  213. u32 lo, hi;
  214. /* Test for Centaur Extended Feature Flags presence */
  215. if (cpuid_eax(0xC0000000) >= 0xC0000001) {
  216. u32 tmp = cpuid_edx(0xC0000001);
  217. /* enable ACE unit, if present and disabled */
  218. if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
  219. rdmsr (MSR_VIA_FCR, lo, hi);
  220. lo |= ACE_FCR; /* enable ACE unit */
  221. wrmsr (MSR_VIA_FCR, lo, hi);
  222. printk(KERN_INFO "CPU: Enabled ACE h/w crypto\n");
  223. }
  224. /* enable RNG unit, if present and disabled */
  225. if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
  226. rdmsr (MSR_VIA_RNG, lo, hi);
  227. lo |= RNG_ENABLE; /* enable RNG unit */
  228. wrmsr (MSR_VIA_RNG, lo, hi);
  229. printk(KERN_INFO "CPU: Enabled h/w RNG\n");
  230. }
  231. /* store Centaur Extended Feature Flags as
  232. * word 5 of the CPU capability bit array
  233. */
  234. c->x86_capability[5] = cpuid_edx(0xC0000001);
  235. }
  236. /* Cyrix III family needs CX8 & PGE explicity enabled. */
  237. if (c->x86_model >=6 && c->x86_model <= 9) {
  238. rdmsr (MSR_VIA_FCR, lo, hi);
  239. lo |= (1<<1 | 1<<7);
  240. wrmsr (MSR_VIA_FCR, lo, hi);
  241. set_bit(X86_FEATURE_CX8, c->x86_capability);
  242. }
  243. /* Before Nehemiah, the C3's had 3dNOW! */
  244. if (c->x86_model >=6 && c->x86_model <9)
  245. set_bit(X86_FEATURE_3DNOW, c->x86_capability);
  246. get_model_name(c);
  247. display_cacheinfo(c);
  248. }
  249. static void __init init_centaur(struct cpuinfo_x86 *c)
  250. {
  251. enum {
  252. ECX8=1<<1,
  253. EIERRINT=1<<2,
  254. DPM=1<<3,
  255. DMCE=1<<4,
  256. DSTPCLK=1<<5,
  257. ELINEAR=1<<6,
  258. DSMC=1<<7,
  259. DTLOCK=1<<8,
  260. EDCTLB=1<<8,
  261. EMMX=1<<9,
  262. DPDC=1<<11,
  263. EBRPRED=1<<12,
  264. DIC=1<<13,
  265. DDC=1<<14,
  266. DNA=1<<15,
  267. ERETSTK=1<<16,
  268. E2MMX=1<<19,
  269. EAMD3D=1<<20,
  270. };
  271. char *name;
  272. u32 fcr_set=0;
  273. u32 fcr_clr=0;
  274. u32 lo,hi,newlo;
  275. u32 aa,bb,cc,dd;
  276. /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  277. 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
  278. clear_bit(0*32+31, c->x86_capability);
  279. switch (c->x86) {
  280. case 5:
  281. switch(c->x86_model) {
  282. case 4:
  283. name="C6";
  284. fcr_set=ECX8|DSMC|EDCTLB|EMMX|ERETSTK;
  285. fcr_clr=DPDC;
  286. printk(KERN_NOTICE "Disabling bugged TSC.\n");
  287. clear_bit(X86_FEATURE_TSC, c->x86_capability);
  288. #ifdef CONFIG_X86_OOSTORE
  289. centaur_create_optimal_mcr();
  290. /* Enable
  291. write combining on non-stack, non-string
  292. write combining on string, all types
  293. weak write ordering
  294. The C6 original lacks weak read order
  295. Note 0x120 is write only on Winchip 1 */
  296. wrmsr(MSR_IDT_MCR_CTRL, 0x01F0001F, 0);
  297. #endif
  298. break;
  299. case 8:
  300. switch(c->x86_mask) {
  301. default:
  302. name="2";
  303. break;
  304. case 7 ... 9:
  305. name="2A";
  306. break;
  307. case 10 ... 15:
  308. name="2B";
  309. break;
  310. }
  311. fcr_set=ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|E2MMX|EAMD3D;
  312. fcr_clr=DPDC;
  313. #ifdef CONFIG_X86_OOSTORE
  314. winchip2_unprotect_mcr();
  315. winchip2_create_optimal_mcr();
  316. rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
  317. /* Enable
  318. write combining on non-stack, non-string
  319. write combining on string, all types
  320. weak write ordering
  321. */
  322. lo|=31;
  323. wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
  324. winchip2_protect_mcr();
  325. #endif
  326. break;
  327. case 9:
  328. name="3";
  329. fcr_set=ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|E2MMX|EAMD3D;
  330. fcr_clr=DPDC;
  331. #ifdef CONFIG_X86_OOSTORE
  332. winchip2_unprotect_mcr();
  333. winchip2_create_optimal_mcr();
  334. rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
  335. /* Enable
  336. write combining on non-stack, non-string
  337. write combining on string, all types
  338. weak write ordering
  339. */
  340. lo|=31;
  341. wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
  342. winchip2_protect_mcr();
  343. #endif
  344. break;
  345. case 10:
  346. name="4";
  347. /* no info on the WC4 yet */
  348. break;
  349. default:
  350. name="??";
  351. }
  352. rdmsr(MSR_IDT_FCR1, lo, hi);
  353. newlo=(lo|fcr_set) & (~fcr_clr);
  354. if (newlo!=lo) {
  355. printk(KERN_INFO "Centaur FCR was 0x%X now 0x%X\n", lo, newlo );
  356. wrmsr(MSR_IDT_FCR1, newlo, hi );
  357. } else {
  358. printk(KERN_INFO "Centaur FCR is 0x%X\n",lo);
  359. }
  360. /* Emulate MTRRs using Centaur's MCR. */
  361. set_bit(X86_FEATURE_CENTAUR_MCR, c->x86_capability);
  362. /* Report CX8 */
  363. set_bit(X86_FEATURE_CX8, c->x86_capability);
  364. /* Set 3DNow! on Winchip 2 and above. */
  365. if (c->x86_model >=8)
  366. set_bit(X86_FEATURE_3DNOW, c->x86_capability);
  367. /* See if we can find out some more. */
  368. if ( cpuid_eax(0x80000000) >= 0x80000005 ) {
  369. /* Yes, we can. */
  370. cpuid(0x80000005,&aa,&bb,&cc,&dd);
  371. /* Add L1 data and code cache sizes. */
  372. c->x86_cache_size = (cc>>24)+(dd>>24);
  373. }
  374. sprintf( c->x86_model_id, "WinChip %s", name );
  375. break;
  376. case 6:
  377. init_c3(c);
  378. break;
  379. }
  380. }
  381. static unsigned int centaur_size_cache(struct cpuinfo_x86 * c, unsigned int size)
  382. {
  383. /* VIA C3 CPUs (670-68F) need further shifting. */
  384. if ((c->x86 == 6) && ((c->x86_model == 7) || (c->x86_model == 8)))
  385. size >>= 8;
  386. /* VIA also screwed up Nehemiah stepping 1, and made
  387. it return '65KB' instead of '64KB'
  388. - Note, it seems this may only be in engineering samples. */
  389. if ((c->x86==6) && (c->x86_model==9) && (c->x86_mask==1) && (size==65))
  390. size -=1;
  391. return size;
  392. }
  393. static struct cpu_dev centaur_cpu_dev __initdata = {
  394. .c_vendor = "Centaur",
  395. .c_ident = { "CentaurHauls" },
  396. .c_init = init_centaur,
  397. .c_size_cache = centaur_size_cache,
  398. };
  399. int __init centaur_init_cpu(void)
  400. {
  401. cpu_devs[X86_VENDOR_CENTAUR] = &centaur_cpu_dev;
  402. return 0;
  403. }
  404. //early_arch_initcall(centaur_init_cpu);