Sorry for this for second time. Now compile tested for both cases ;)
[coreboot.git] / src / include / cpu / x86 / cache.h
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Eric W. Biederman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #ifndef CPU_X86_CACHE
21 #define CPU_X86_CACHE
22
23 /* the memory clobber prevents the GCC from reordering the read/write order
24    of CR0 */
25
26 #if defined(__GNUC__)
27
28 /*
29 Need this because ROMCC fails here with:
30
31 cache.h:29.71: cache.h:60.24: earlymtrr.c:117.23: romstage.c:144.33: 
32 0x1559920 asm        Internal compiler error: lhs 1 regcm == 0
33 */
34
35 static inline unsigned long read_cr0(void)
36 {
37         unsigned long cr0;
38         asm volatile ("movl %%cr0, %0" : "=r" (cr0) :: "memory");
39         return cr0;
40 }
41
42 static inline void write_cr0(unsigned long cr0)
43 {
44         asm volatile ("movl %0, %%cr0" : : "r" (cr0) : "memory");
45 }
46
47 static inline void wbinvd(void)
48 {
49         asm volatile ("wbinvd" ::: "memory");
50 }
51
52 #else
53
54 static inline unsigned long read_cr0(void)
55 {
56         unsigned long cr0;
57         asm volatile ("movl %%cr0, %0" : "=r" (cr0));
58         return cr0;
59 }
60
61 static inline void write_cr0(unsigned long cr0)
62 {
63         asm volatile ("movl %0, %%cr0" : : "r" (cr0));
64 }
65
66 static inline void wbinvd(void)
67 {
68         asm volatile ("wbinvd");
69 }
70
71 #endif
72
73 static inline void invd(void)
74 {
75         asm volatile("invd" ::: "memory");
76 }
77
78 static inline void enable_cache(void)
79 {
80         unsigned long cr0;
81         cr0 = read_cr0();
82         cr0 &= 0x9fffffff;
83         write_cr0(cr0);
84 }
85
86 static inline void disable_cache(void)
87 {
88         /* Disable and write back the cache */
89         unsigned long cr0;
90         cr0 = read_cr0();
91         cr0 |= 0x40000000;
92         wbinvd();
93         write_cr0(cr0);
94         wbinvd();
95 }
96
97 #if !defined(__PRE_RAM__)
98 void x86_enable_cache(void);
99 #endif
100
101 #endif /* CPU_X86_CACHE */