cyrix.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <linux/init.h>
  2. #include <linux/mm.h>
  3. #include <asm/mtrr.h>
  4. #include <asm/msr.h>
  5. #include <asm/io.h>
  6. #include "mtrr.h"
  7. int arr3_protected;
  8. static void
  9. cyrix_get_arr(unsigned int reg, unsigned long *base,
  10. unsigned int *size, mtrr_type * type)
  11. {
  12. unsigned long flags;
  13. unsigned char arr, ccr3, rcr, shift;
  14. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  15. /* Save flags and disable interrupts */
  16. local_irq_save(flags);
  17. ccr3 = getCx86(CX86_CCR3);
  18. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10); /* enable MAPEN */
  19. ((unsigned char *) base)[3] = getCx86(arr);
  20. ((unsigned char *) base)[2] = getCx86(arr + 1);
  21. ((unsigned char *) base)[1] = getCx86(arr + 2);
  22. rcr = getCx86(CX86_RCR_BASE + reg);
  23. setCx86(CX86_CCR3, ccr3); /* disable MAPEN */
  24. /* Enable interrupts if it was enabled previously */
  25. local_irq_restore(flags);
  26. shift = ((unsigned char *) base)[1] & 0x0f;
  27. *base >>= PAGE_SHIFT;
  28. /* Power of two, at least 4K on ARR0-ARR6, 256K on ARR7
  29. * Note: shift==0xf means 4G, this is unsupported.
  30. */
  31. if (shift)
  32. *size = (reg < 7 ? 0x1UL : 0x40UL) << (shift - 1);
  33. else
  34. *size = 0;
  35. /* Bit 0 is Cache Enable on ARR7, Cache Disable on ARR0-ARR6 */
  36. if (reg < 7) {
  37. switch (rcr) {
  38. case 1:
  39. *type = MTRR_TYPE_UNCACHABLE;
  40. break;
  41. case 8:
  42. *type = MTRR_TYPE_WRBACK;
  43. break;
  44. case 9:
  45. *type = MTRR_TYPE_WRCOMB;
  46. break;
  47. case 24:
  48. default:
  49. *type = MTRR_TYPE_WRTHROUGH;
  50. break;
  51. }
  52. } else {
  53. switch (rcr) {
  54. case 0:
  55. *type = MTRR_TYPE_UNCACHABLE;
  56. break;
  57. case 8:
  58. *type = MTRR_TYPE_WRCOMB;
  59. break;
  60. case 9:
  61. *type = MTRR_TYPE_WRBACK;
  62. break;
  63. case 25:
  64. default:
  65. *type = MTRR_TYPE_WRTHROUGH;
  66. break;
  67. }
  68. }
  69. }
  70. static int
  71. cyrix_get_free_region(unsigned long base, unsigned long size)
  72. /* [SUMMARY] Get a free ARR.
  73. <base> The starting (base) address of the region.
  74. <size> The size (in bytes) of the region.
  75. [RETURNS] The index of the region on success, else -1 on error.
  76. */
  77. {
  78. int i;
  79. mtrr_type ltype;
  80. unsigned long lbase;
  81. unsigned int lsize;
  82. /* If we are to set up a region >32M then look at ARR7 immediately */
  83. if (size > 0x2000) {
  84. cyrix_get_arr(7, &lbase, &lsize, &ltype);
  85. if (lsize == 0)
  86. return 7;
  87. /* Else try ARR0-ARR6 first */
  88. } else {
  89. for (i = 0; i < 7; i++) {
  90. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  91. if ((i == 3) && arr3_protected)
  92. continue;
  93. if (lsize == 0)
  94. return i;
  95. }
  96. /* ARR0-ARR6 isn't free, try ARR7 but its size must be at least 256K */
  97. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  98. if ((lsize == 0) && (size >= 0x40))
  99. return i;
  100. }
  101. return -ENOSPC;
  102. }
  103. static u32 cr4 = 0;
  104. static u32 ccr3;
  105. static void prepare_set(void)
  106. {
  107. u32 cr0;
  108. /* Save value of CR4 and clear Page Global Enable (bit 7) */
  109. if ( cpu_has_pge ) {
  110. cr4 = read_cr4();
  111. write_cr4(cr4 & (unsigned char) ~(1 << 7));
  112. }
  113. /* Disable and flush caches. Note that wbinvd flushes the TLBs as
  114. a side-effect */
  115. cr0 = read_cr0() | 0x40000000;
  116. wbinvd();
  117. write_cr0(cr0);
  118. wbinvd();
  119. /* Cyrix ARRs - everything else were excluded at the top */
  120. ccr3 = getCx86(CX86_CCR3);
  121. /* Cyrix ARRs - everything else were excluded at the top */
  122. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10);
  123. }
  124. static void post_set(void)
  125. {
  126. /* Flush caches and TLBs */
  127. wbinvd();
  128. /* Cyrix ARRs - everything else was excluded at the top */
  129. setCx86(CX86_CCR3, ccr3);
  130. /* Enable caches */
  131. write_cr0(read_cr0() & 0xbfffffff);
  132. /* Restore value of CR4 */
  133. if ( cpu_has_pge )
  134. write_cr4(cr4);
  135. }
  136. static void cyrix_set_arr(unsigned int reg, unsigned long base,
  137. unsigned long size, mtrr_type type)
  138. {
  139. unsigned char arr, arr_type, arr_size;
  140. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  141. /* count down from 32M (ARR0-ARR6) or from 2G (ARR7) */
  142. if (reg >= 7)
  143. size >>= 6;
  144. size &= 0x7fff; /* make sure arr_size <= 14 */
  145. for (arr_size = 0; size; arr_size++, size >>= 1) ;
  146. if (reg < 7) {
  147. switch (type) {
  148. case MTRR_TYPE_UNCACHABLE:
  149. arr_type = 1;
  150. break;
  151. case MTRR_TYPE_WRCOMB:
  152. arr_type = 9;
  153. break;
  154. case MTRR_TYPE_WRTHROUGH:
  155. arr_type = 24;
  156. break;
  157. default:
  158. arr_type = 8;
  159. break;
  160. }
  161. } else {
  162. switch (type) {
  163. case MTRR_TYPE_UNCACHABLE:
  164. arr_type = 0;
  165. break;
  166. case MTRR_TYPE_WRCOMB:
  167. arr_type = 8;
  168. break;
  169. case MTRR_TYPE_WRTHROUGH:
  170. arr_type = 25;
  171. break;
  172. default:
  173. arr_type = 9;
  174. break;
  175. }
  176. }
  177. prepare_set();
  178. base <<= PAGE_SHIFT;
  179. setCx86(arr, ((unsigned char *) &base)[3]);
  180. setCx86(arr + 1, ((unsigned char *) &base)[2]);
  181. setCx86(arr + 2, (((unsigned char *) &base)[1]) | arr_size);
  182. setCx86(CX86_RCR_BASE + reg, arr_type);
  183. post_set();
  184. }
  185. typedef struct {
  186. unsigned long base;
  187. unsigned int size;
  188. mtrr_type type;
  189. } arr_state_t;
  190. static arr_state_t arr_state[8] __devinitdata = {
  191. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL},
  192. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}
  193. };
  194. static unsigned char ccr_state[7] __devinitdata = { 0, 0, 0, 0, 0, 0, 0 };
  195. static void cyrix_set_all(void)
  196. {
  197. int i;
  198. prepare_set();
  199. /* the CCRs are not contiguous */
  200. for (i = 0; i < 4; i++)
  201. setCx86(CX86_CCR0 + i, ccr_state[i]);
  202. for (; i < 7; i++)
  203. setCx86(CX86_CCR4 + i, ccr_state[i]);
  204. for (i = 0; i < 8; i++)
  205. cyrix_set_arr(i, arr_state[i].base,
  206. arr_state[i].size, arr_state[i].type);
  207. post_set();
  208. }
  209. #if 0
  210. /*
  211. * On Cyrix 6x86(MX) and M II the ARR3 is special: it has connection
  212. * with the SMM (System Management Mode) mode. So we need the following:
  213. * Check whether SMI_LOCK (CCR3 bit 0) is set
  214. * if it is set, write a warning message: ARR3 cannot be changed!
  215. * (it cannot be changed until the next processor reset)
  216. * if it is reset, then we can change it, set all the needed bits:
  217. * - disable access to SMM memory through ARR3 range (CCR1 bit 7 reset)
  218. * - disable access to SMM memory (CCR1 bit 2 reset)
  219. * - disable SMM mode (CCR1 bit 1 reset)
  220. * - disable write protection of ARR3 (CCR6 bit 1 reset)
  221. * - (maybe) disable ARR3
  222. * Just to be sure, we enable ARR usage by the processor (CCR5 bit 5 set)
  223. */
  224. static void __init
  225. cyrix_arr_init(void)
  226. {
  227. struct set_mtrr_context ctxt;
  228. unsigned char ccr[7];
  229. int ccrc[7] = { 0, 0, 0, 0, 0, 0, 0 };
  230. #ifdef CONFIG_SMP
  231. int i;
  232. #endif
  233. /* flush cache and enable MAPEN */
  234. set_mtrr_prepare_save(&ctxt);
  235. set_mtrr_cache_disable(&ctxt);
  236. /* Save all CCRs locally */
  237. ccr[0] = getCx86(CX86_CCR0);
  238. ccr[1] = getCx86(CX86_CCR1);
  239. ccr[2] = getCx86(CX86_CCR2);
  240. ccr[3] = ctxt.ccr3;
  241. ccr[4] = getCx86(CX86_CCR4);
  242. ccr[5] = getCx86(CX86_CCR5);
  243. ccr[6] = getCx86(CX86_CCR6);
  244. if (ccr[3] & 1) {
  245. ccrc[3] = 1;
  246. arr3_protected = 1;
  247. } else {
  248. /* Disable SMM mode (bit 1), access to SMM memory (bit 2) and
  249. * access to SMM memory through ARR3 (bit 7).
  250. */
  251. if (ccr[1] & 0x80) {
  252. ccr[1] &= 0x7f;
  253. ccrc[1] |= 0x80;
  254. }
  255. if (ccr[1] & 0x04) {
  256. ccr[1] &= 0xfb;
  257. ccrc[1] |= 0x04;
  258. }
  259. if (ccr[1] & 0x02) {
  260. ccr[1] &= 0xfd;
  261. ccrc[1] |= 0x02;
  262. }
  263. arr3_protected = 0;
  264. if (ccr[6] & 0x02) {
  265. ccr[6] &= 0xfd;
  266. ccrc[6] = 1; /* Disable write protection of ARR3 */
  267. setCx86(CX86_CCR6, ccr[6]);
  268. }
  269. /* Disable ARR3. This is safe now that we disabled SMM. */
  270. /* cyrix_set_arr_up (3, 0, 0, 0, FALSE); */
  271. }
  272. /* If we changed CCR1 in memory, change it in the processor, too. */
  273. if (ccrc[1])
  274. setCx86(CX86_CCR1, ccr[1]);
  275. /* Enable ARR usage by the processor */
  276. if (!(ccr[5] & 0x20)) {
  277. ccr[5] |= 0x20;
  278. ccrc[5] = 1;
  279. setCx86(CX86_CCR5, ccr[5]);
  280. }
  281. #ifdef CONFIG_SMP
  282. for (i = 0; i < 7; i++)
  283. ccr_state[i] = ccr[i];
  284. for (i = 0; i < 8; i++)
  285. cyrix_get_arr(i,
  286. &arr_state[i].base, &arr_state[i].size,
  287. &arr_state[i].type);
  288. #endif
  289. set_mtrr_done(&ctxt); /* flush cache and disable MAPEN */
  290. if (ccrc[5])
  291. printk(KERN_INFO "mtrr: ARR usage was not enabled, enabled manually\n");
  292. if (ccrc[3])
  293. printk(KERN_INFO "mtrr: ARR3 cannot be changed\n");
  294. /*
  295. if ( ccrc[1] & 0x80) printk ("mtrr: SMM memory access through ARR3 disabled\n");
  296. if ( ccrc[1] & 0x04) printk ("mtrr: SMM memory access disabled\n");
  297. if ( ccrc[1] & 0x02) printk ("mtrr: SMM mode disabled\n");
  298. */
  299. if (ccrc[6])
  300. printk(KERN_INFO "mtrr: ARR3 was write protected, unprotected\n");
  301. }
  302. #endif
  303. static struct mtrr_ops cyrix_mtrr_ops = {
  304. .vendor = X86_VENDOR_CYRIX,
  305. // .init = cyrix_arr_init,
  306. .set_all = cyrix_set_all,
  307. .set = cyrix_set_arr,
  308. .get = cyrix_get_arr,
  309. .get_free_region = cyrix_get_free_region,
  310. .validate_add_page = generic_validate_add_page,
  311. .have_wrcomb = positive_have_wrcomb,
  312. };
  313. int __init cyrix_init_mtrr(void)
  314. {
  315. set_mtrr_ops(&cyrix_mtrr_ops);
  316. return 0;
  317. }
  318. //arch_initcall(cyrix_init_mtrr);