cyrix.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/io.h>
  4. #include <linux/mm.h>
  5. #include <asm/processor-cyrix.h>
  6. #include <asm/processor-flags.h>
  7. #include <asm/mtrr.h>
  8. #include <asm/msr.h>
  9. #include "mtrr.h"
  10. static void
  11. cyrix_get_arr(unsigned int reg, unsigned long *base,
  12. unsigned long *size, mtrr_type * type)
  13. {
  14. unsigned char arr, ccr3, rcr, shift;
  15. unsigned long flags;
  16. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  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. local_irq_restore(flags);
  26. shift = ((unsigned char *) base)[1] & 0x0f;
  27. *base >>= PAGE_SHIFT;
  28. /*
  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. /*
  72. * cyrix_get_free_region - get a free ARR.
  73. *
  74. * @base: the starting (base) address of the region.
  75. * @size: the size (in bytes) of the region.
  76. *
  77. * Returns: the index of the region on success, else -1 on error.
  78. */
  79. static int
  80. cyrix_get_free_region(unsigned long base, unsigned long size, int replace_reg)
  81. {
  82. unsigned long lbase, lsize;
  83. mtrr_type ltype;
  84. int i;
  85. switch (replace_reg) {
  86. case 7:
  87. if (size < 0x40)
  88. break;
  89. case 6:
  90. case 5:
  91. case 4:
  92. return replace_reg;
  93. case 3:
  94. case 2:
  95. case 1:
  96. case 0:
  97. return replace_reg;
  98. }
  99. /* If we are to set up a region >32M then look at ARR7 immediately */
  100. if (size > 0x2000) {
  101. cyrix_get_arr(7, &lbase, &lsize, &ltype);
  102. if (lsize == 0)
  103. return 7;
  104. /* Else try ARR0-ARR6 first */
  105. } else {
  106. for (i = 0; i < 7; i++) {
  107. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  108. if (lsize == 0)
  109. return i;
  110. }
  111. /*
  112. * ARR0-ARR6 isn't free
  113. * try ARR7 but its size must be at least 256K
  114. */
  115. cyrix_get_arr(i, &lbase, &lsize, &ltype);
  116. if ((lsize == 0) && (size >= 0x40))
  117. return i;
  118. }
  119. return -ENOSPC;
  120. }
  121. static u32 cr4, ccr3;
  122. static void prepare_set(void)
  123. {
  124. u32 cr0;
  125. /* Save value of CR4 and clear Page Global Enable (bit 7) */
  126. if (boot_cpu_has(X86_FEATURE_PGE)) {
  127. cr4 = __read_cr4();
  128. __write_cr4(cr4 & ~X86_CR4_PGE);
  129. }
  130. /*
  131. * Disable and flush caches.
  132. * Note that wbinvd flushes the TLBs as a side-effect
  133. */
  134. cr0 = read_cr0() | X86_CR0_CD;
  135. wbinvd();
  136. write_cr0(cr0);
  137. wbinvd();
  138. /* Cyrix ARRs - everything else was excluded at the top */
  139. ccr3 = getCx86(CX86_CCR3);
  140. /* Cyrix ARRs - everything else was excluded at the top */
  141. setCx86(CX86_CCR3, (ccr3 & 0x0f) | 0x10);
  142. }
  143. static void post_set(void)
  144. {
  145. /* Flush caches and TLBs */
  146. wbinvd();
  147. /* Cyrix ARRs - everything else was excluded at the top */
  148. setCx86(CX86_CCR3, ccr3);
  149. /* Enable caches */
  150. write_cr0(read_cr0() & ~X86_CR0_CD);
  151. /* Restore value of CR4 */
  152. if (boot_cpu_has(X86_FEATURE_PGE))
  153. __write_cr4(cr4);
  154. }
  155. static void cyrix_set_arr(unsigned int reg, unsigned long base,
  156. unsigned long size, mtrr_type type)
  157. {
  158. unsigned char arr, arr_type, arr_size;
  159. arr = CX86_ARR_BASE + (reg << 1) + reg; /* avoid multiplication by 3 */
  160. /* count down from 32M (ARR0-ARR6) or from 2G (ARR7) */
  161. if (reg >= 7)
  162. size >>= 6;
  163. size &= 0x7fff; /* make sure arr_size <= 14 */
  164. for (arr_size = 0; size; arr_size++, size >>= 1)
  165. ;
  166. if (reg < 7) {
  167. switch (type) {
  168. case MTRR_TYPE_UNCACHABLE:
  169. arr_type = 1;
  170. break;
  171. case MTRR_TYPE_WRCOMB:
  172. arr_type = 9;
  173. break;
  174. case MTRR_TYPE_WRTHROUGH:
  175. arr_type = 24;
  176. break;
  177. default:
  178. arr_type = 8;
  179. break;
  180. }
  181. } else {
  182. switch (type) {
  183. case MTRR_TYPE_UNCACHABLE:
  184. arr_type = 0;
  185. break;
  186. case MTRR_TYPE_WRCOMB:
  187. arr_type = 8;
  188. break;
  189. case MTRR_TYPE_WRTHROUGH:
  190. arr_type = 25;
  191. break;
  192. default:
  193. arr_type = 9;
  194. break;
  195. }
  196. }
  197. prepare_set();
  198. base <<= PAGE_SHIFT;
  199. setCx86(arr + 0, ((unsigned char *)&base)[3]);
  200. setCx86(arr + 1, ((unsigned char *)&base)[2]);
  201. setCx86(arr + 2, (((unsigned char *)&base)[1]) | arr_size);
  202. setCx86(CX86_RCR_BASE + reg, arr_type);
  203. post_set();
  204. }
  205. typedef struct {
  206. unsigned long base;
  207. unsigned long size;
  208. mtrr_type type;
  209. } arr_state_t;
  210. static arr_state_t arr_state[8] = {
  211. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL},
  212. {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}
  213. };
  214. static unsigned char ccr_state[7] = { 0, 0, 0, 0, 0, 0, 0 };
  215. static void cyrix_set_all(void)
  216. {
  217. int i;
  218. prepare_set();
  219. /* the CCRs are not contiguous */
  220. for (i = 0; i < 4; i++)
  221. setCx86(CX86_CCR0 + i, ccr_state[i]);
  222. for (; i < 7; i++)
  223. setCx86(CX86_CCR4 + i, ccr_state[i]);
  224. for (i = 0; i < 8; i++) {
  225. cyrix_set_arr(i, arr_state[i].base,
  226. arr_state[i].size, arr_state[i].type);
  227. }
  228. post_set();
  229. }
  230. static const struct mtrr_ops cyrix_mtrr_ops = {
  231. .vendor = X86_VENDOR_CYRIX,
  232. .set_all = cyrix_set_all,
  233. .set = cyrix_set_arr,
  234. .get = cyrix_get_arr,
  235. .get_free_region = cyrix_get_free_region,
  236. .validate_add_page = generic_validate_add_page,
  237. .have_wrcomb = positive_have_wrcomb,
  238. };
  239. int __init cyrix_init_mtrr(void)
  240. {
  241. set_mtrr_ops(&cyrix_mtrr_ops);
  242. return 0;
  243. }