1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
6 #include <asm/processor-cyrix.h>
7 #include <asm/processor-flags.h>
14 cyrix_get_arr(unsigned int reg
, unsigned long *base
,
15 unsigned long *size
, mtrr_type
* type
)
17 unsigned char arr
, ccr3
, rcr
, shift
;
20 arr
= CX86_ARR_BASE
+ (reg
<< 1) + reg
; /* avoid multiplication by 3 */
22 local_irq_save(flags
);
24 ccr3
= getCx86(CX86_CCR3
);
25 setCx86(CX86_CCR3
, (ccr3
& 0x0f) | 0x10); /* enable MAPEN */
26 ((unsigned char *)base
)[3] = getCx86(arr
);
27 ((unsigned char *)base
)[2] = getCx86(arr
+ 1);
28 ((unsigned char *)base
)[1] = getCx86(arr
+ 2);
29 rcr
= getCx86(CX86_RCR_BASE
+ reg
);
30 setCx86(CX86_CCR3
, ccr3
); /* disable MAPEN */
32 local_irq_restore(flags
);
34 shift
= ((unsigned char *) base
)[1] & 0x0f;
38 * Power of two, at least 4K on ARR0-ARR6, 256K on ARR7
39 * Note: shift==0xf means 4G, this is unsupported.
42 *size
= (reg
< 7 ? 0x1UL
: 0x40UL
) << (shift
- 1);
46 /* Bit 0 is Cache Enable on ARR7, Cache Disable on ARR0-ARR6 */
50 *type
= MTRR_TYPE_UNCACHABLE
;
53 *type
= MTRR_TYPE_WRBACK
;
56 *type
= MTRR_TYPE_WRCOMB
;
60 *type
= MTRR_TYPE_WRTHROUGH
;
66 *type
= MTRR_TYPE_UNCACHABLE
;
69 *type
= MTRR_TYPE_WRCOMB
;
72 *type
= MTRR_TYPE_WRBACK
;
76 *type
= MTRR_TYPE_WRTHROUGH
;
83 * cyrix_get_free_region - get a free ARR.
85 * @base: the starting (base) address of the region.
86 * @size: the size (in bytes) of the region.
88 * Returns: the index of the region on success, else -1 on error.
91 cyrix_get_free_region(unsigned long base
, unsigned long size
, int replace_reg
)
93 unsigned long lbase
, lsize
;
97 switch (replace_reg
) {
111 /* If we are to set up a region >32M then look at ARR7 immediately */
113 cyrix_get_arr(7, &lbase
, &lsize
, <ype
);
116 /* Else try ARR0-ARR6 first */
118 for (i
= 0; i
< 7; i
++) {
119 cyrix_get_arr(i
, &lbase
, &lsize
, <ype
);
124 * ARR0-ARR6 isn't free
125 * try ARR7 but its size must be at least 256K
127 cyrix_get_arr(i
, &lbase
, &lsize
, <ype
);
128 if ((lsize
== 0) && (size
>= 0x40))
134 static u32 cr4
, ccr3
;
136 static void prepare_set(void)
140 /* Save value of CR4 and clear Page Global Enable (bit 7) */
141 if (boot_cpu_has(X86_FEATURE_PGE
)) {
143 __write_cr4(cr4
& ~X86_CR4_PGE
);
147 * Disable and flush caches.
148 * Note that wbinvd flushes the TLBs as a side-effect
150 cr0
= read_cr0() | X86_CR0_CD
;
155 /* Cyrix ARRs - everything else was excluded at the top */
156 ccr3
= getCx86(CX86_CCR3
);
158 /* Cyrix ARRs - everything else was excluded at the top */
159 setCx86(CX86_CCR3
, (ccr3
& 0x0f) | 0x10);
162 static void post_set(void)
164 /* Flush caches and TLBs */
167 /* Cyrix ARRs - everything else was excluded at the top */
168 setCx86(CX86_CCR3
, ccr3
);
171 write_cr0(read_cr0() & ~X86_CR0_CD
);
173 /* Restore value of CR4 */
174 if (boot_cpu_has(X86_FEATURE_PGE
))
178 static void cyrix_set_arr(unsigned int reg
, unsigned long base
,
179 unsigned long size
, mtrr_type type
)
181 unsigned char arr
, arr_type
, arr_size
;
183 arr
= CX86_ARR_BASE
+ (reg
<< 1) + reg
; /* avoid multiplication by 3 */
185 /* count down from 32M (ARR0-ARR6) or from 2G (ARR7) */
189 size
&= 0x7fff; /* make sure arr_size <= 14 */
190 for (arr_size
= 0; size
; arr_size
++, size
>>= 1)
195 case MTRR_TYPE_UNCACHABLE
:
198 case MTRR_TYPE_WRCOMB
:
201 case MTRR_TYPE_WRTHROUGH
:
210 case MTRR_TYPE_UNCACHABLE
:
213 case MTRR_TYPE_WRCOMB
:
216 case MTRR_TYPE_WRTHROUGH
:
228 setCx86(arr
+ 0, ((unsigned char *)&base
)[3]);
229 setCx86(arr
+ 1, ((unsigned char *)&base
)[2]);
230 setCx86(arr
+ 2, (((unsigned char *)&base
)[1]) | arr_size
);
231 setCx86(CX86_RCR_BASE
+ reg
, arr_type
);
242 static arr_state_t arr_state
[8] = {
243 {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL},
244 {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}, {0UL, 0UL, 0UL}
247 static unsigned char ccr_state
[7] = { 0, 0, 0, 0, 0, 0, 0 };
249 static void cyrix_set_all(void)
255 /* the CCRs are not contiguous */
256 for (i
= 0; i
< 4; i
++)
257 setCx86(CX86_CCR0
+ i
, ccr_state
[i
]);
259 setCx86(CX86_CCR4
+ i
, ccr_state
[i
]);
261 for (i
= 0; i
< 8; i
++) {
262 cyrix_set_arr(i
, arr_state
[i
].base
,
263 arr_state
[i
].size
, arr_state
[i
].type
);
269 static const struct mtrr_ops cyrix_mtrr_ops
= {
270 .vendor
= X86_VENDOR_CYRIX
,
271 .set_all
= cyrix_set_all
,
272 .set
= cyrix_set_arr
,
273 .get
= cyrix_get_arr
,
274 .get_free_region
= cyrix_get_free_region
,
275 .validate_add_page
= generic_validate_add_page
,
276 .have_wrcomb
= positive_have_wrcomb
,
279 int __init
cyrix_init_mtrr(void)
281 set_mtrr_ops(&cyrix_mtrr_ops
);