cyrix.c 5.8 KB

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