cyrix.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <asm/processor-cyrix.h>
  7. #include <asm/processor-flags.h>
  8. #include "mtrr.h"
  9. static void
  10. cyrix_get_arr(unsigned int reg, unsigned long *base,
  11. unsigned long *size, mtrr_type * type)
  12. {
  13. unsigned long flags;
  14. unsigned char arr, ccr3, rcr, shift;
  15. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  16. /* Save flags and disable interrupts */
  17. local_irq_save(flags);
  18. ccr3 = getCx86(CX86_CCR3);
  19. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10); /* enable MAPEN */
  20. ((unsigned char *) base)[3] = getCx86(arr);
  21. ((unsigned char *) base)[2] = getCx86(arr + 1);
  22. ((unsigned char *) base)[1] = getCx86(arr + 2);
  23. rcr = getCx86(CX86_RCR_BASE + reg);
  24. setCx86(CX86_CCR3, ccr3); /* disable MAPEN */
  25. /* Enable interrupts if it was enabled previously */
  26. local_irq_restore(flags);
  27. shift = ((unsigned char *) base)[1] & 0x0f;
  28. *base >>= PAGE_SHIFT;
  29. /* Power of two, at least 4K on ARR0-ARR6, 256K on ARR7
  30. * Note: shift==0xf means 4G, this is unsupported.
  31. */
  32. if (shift)
  33. *size = (reg < 7 ? 0x1UL : 0x40UL) << (shift - 1);
  34. else
  35. *size = 0;
  36. /* Bit 0 is Cache Enable on ARR7, Cache Disable on ARR0-ARR6 */
  37. if (reg < 7) {
  38. switch (rcr) {
  39. case 1:
  40. *type = MTRR_TYPE_UNCACHABLE;
  41. break;
  42. case 8:
  43. *type = MTRR_TYPE_WRBACK;
  44. break;
  45. case 9:
  46. *type = MTRR_TYPE_WRCOMB;
  47. break;
  48. case 24:
  49. default:
  50. *type = MTRR_TYPE_WRTHROUGH;
  51. break;
  52. }
  53. } else {
  54. switch (rcr) {
  55. case 0:
  56. *type = MTRR_TYPE_UNCACHABLE;
  57. break;
  58. case 8:
  59. *type = MTRR_TYPE_WRCOMB;
  60. break;
  61. case 9:
  62. *type = MTRR_TYPE_WRBACK;
  63. break;
  64. case 25:
  65. default:
  66. *type = MTRR_TYPE_WRTHROUGH;
  67. break;
  68. }
  69. }
  70. }
  71. static int
  72. cyrix_get_free_region(unsigned long base, unsigned long size, int replace_reg)
  73. /* [SUMMARY] Get a free ARR.
  74. <base> The starting (base) address of the region.
  75. <size> The size (in bytes) of the region.
  76. [RETURNS] The index of the region on success, else -1 on error.
  77. */
  78. {
  79. int i;
  80. mtrr_type ltype;
  81. unsigned long lbase, lsize;
  82. switch (replace_reg) {
  83. case 7:
  84. if (size < 0x40)
  85. break;
  86. case 6:
  87. case 5:
  88. case 4:
  89. return replace_reg;
  90. case 3:
  91. case 2:
  92. case 1:
  93. case 0:
  94. return replace_reg;
  95. }
  96. /* If we are to set up a region >32M then look at ARR7 immediately */
  97. if (size > 0x2000) {
  98. cyrix_get_arr(7, &lbase, &lsize, &ltype);
  99. if (lsize == 0)
  100. return 7;
  101. /* Else try ARR0-ARR6 first */
  102. } else {
  103. for (i = 0; i < 7; i++) {
  104. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  105. if (lsize == 0)
  106. return i;
  107. }
  108. /* ARR0-ARR6 isn't free, try ARR7 but its size must be at least 256K */
  109. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  110. if ((lsize == 0) && (size >= 0x40))
  111. return i;
  112. }
  113. return -ENOSPC;
  114. }
  115. static u32 cr4 = 0;
  116. static u32 ccr3;
  117. static void prepare_set(void)
  118. {
  119. u32 cr0;
  120. /* Save value of CR4 and clear Page Global Enable (bit 7) */
  121. if ( cpu_has_pge ) {
  122. cr4 = read_cr4();
  123. write_cr4(cr4 & ~X86_CR4_PGE);
  124. }
  125. /* Disable and flush caches. Note that wbinvd flushes the TLBs as
  126. a side-effect */
  127. cr0 = read_cr0() | X86_CR0_CD;
  128. wbinvd();
  129. write_cr0(cr0);
  130. wbinvd();
  131. /* Cyrix ARRs - everything else was excluded at the top */
  132. ccr3 = getCx86(CX86_CCR3);
  133. /* Cyrix ARRs - everything else was excluded at the top */
  134. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10);
  135. }
  136. static void post_set(void)
  137. {
  138. /* Flush caches and TLBs */
  139. wbinvd();
  140. /* Cyrix ARRs - everything else was excluded at the top */
  141. setCx86(CX86_CCR3, ccr3);
  142. /* Enable caches */
  143. write_cr0(read_cr0() & 0xbfffffff);
  144. /* Restore value of CR4 */
  145. if ( cpu_has_pge )
  146. write_cr4(cr4);
  147. }
  148. static void cyrix_set_arr(unsigned int reg, unsigned long base,
  149. unsigned long size, mtrr_type type)
  150. {
  151. unsigned char arr, arr_type, arr_size;
  152. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  153. /* count down from 32M (ARR0-ARR6) or from 2G (ARR7) */
  154. if (reg >= 7)
  155. size >>= 6;
  156. size &= 0x7fff; /* make sure arr_size <= 14 */
  157. for (arr_size = 0; size; arr_size++, size >>= 1) ;
  158. if (reg < 7) {
  159. switch (type) {
  160. case MTRR_TYPE_UNCACHABLE:
  161. arr_type = 1;
  162. break;
  163. case MTRR_TYPE_WRCOMB:
  164. arr_type = 9;
  165. break;
  166. case MTRR_TYPE_WRTHROUGH:
  167. arr_type = 24;
  168. break;
  169. default:
  170. arr_type = 8;
  171. break;
  172. }
  173. } else {
  174. switch (type) {
  175. case MTRR_TYPE_UNCACHABLE:
  176. arr_type = 0;
  177. break;
  178. case MTRR_TYPE_WRCOMB:
  179. arr_type = 8;
  180. break;
  181. case MTRR_TYPE_WRTHROUGH:
  182. arr_type = 25;
  183. break;
  184. default:
  185. arr_type = 9;
  186. break;
  187. }
  188. }
  189. prepare_set();
  190. base <<= PAGE_SHIFT;
  191. setCx86(arr, ((unsigned char *) &base)[3]);
  192. setCx86(arr + 1, ((unsigned char *) &base)[2]);
  193. setCx86(arr + 2, (((unsigned char *) &base)[1]) | arr_size);
  194. setCx86(CX86_RCR_BASE + reg, arr_type);
  195. post_set();
  196. }
  197. typedef struct {
  198. unsigned long base;
  199. unsigned long size;
  200. mtrr_type type;
  201. } arr_state_t;
  202. static arr_state_t arr_state[8] = {
  203. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL},
  204. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}
  205. };
  206. static unsigned char ccr_state[7] = { 0, 0, 0, 0, 0, 0, 0 };
  207. static void cyrix_set_all(void)
  208. {
  209. int i;
  210. prepare_set();
  211. /* the CCRs are not contiguous */
  212. for (i = 0; i < 4; i++)
  213. setCx86(CX86_CCR0 + i, ccr_state[i]);
  214. for (; i < 7; i++)
  215. setCx86(CX86_CCR4 + i, ccr_state[i]);
  216. for (i = 0; i < 8; i++)
  217. cyrix_set_arr(i, arr_state[i].base,
  218. arr_state[i].size, arr_state[i].type);
  219. post_set();
  220. }
  221. static struct mtrr_ops cyrix_mtrr_ops = {
  222. .vendor = X86_VENDOR_CYRIX,
  223. // .init = cyrix_arr_init,
  224. .set_all = cyrix_set_all,
  225. .set = cyrix_set_arr,
  226. .get = cyrix_get_arr,
  227. .get_free_region = cyrix_get_free_region,
  228. .validate_add_page = generic_validate_add_page,
  229. .have_wrcomb = positive_have_wrcomb,
  230. };
  231. int __init cyrix_init_mtrr(void)
  232. {
  233. set_mtrr_ops(&cyrix_mtrr_ops);
  234. return 0;
  235. }
  236. //arch_initcall(cyrix_init_mtrr);