replace outb -> port 0x80 with post_code() in some places.
[coreboot.git] / src / cpu / amd / model_lx / cache_as_ram.inc
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
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 version 2 as
8  * published by the Free Software Foundation.
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 #define LX_STACK_BASE           CONFIG_DCACHE_RAM_BASE          /* this is where the DCache will be mapped and be used as stack, It would be cool if it was the same base as coreboot normal stack */
21 #define LX_STACK_END            LX_STACK_BASE+(CONFIG_DCACHE_RAM_SIZE-1)
22
23 #define LX_NUM_CACHELINES       0x080   /* there are 128lines per way */
24 #define LX_CACHELINE_SIZE       0x020   /* there are 32bytes per line */
25 #define LX_CACHEWAY_SIZE        (LX_NUM_CACHELINES * LX_CACHELINE_SIZE)
26 #define CR0_CD                          0x40000000      /* bit 30 = Cache Disable */
27 #define CR0_NW                          0x20000000      /* bit 29 = Not Write Through */
28 #include <cpu/amd/lxdef.h>
29 /***************************************************************************
30 /**
31 /**     DCacheSetup
32 /**
33 /**     Setup data cache for  use as RAM for a stack.
34 /**
35 /***************************************************************************/
36 DCacheSetup:
37         /* Save the BIST result */
38         movl    %eax, %ebx
39
40         invd
41         /* set cache properties */
42         movl    $CPU_RCONF_DEFAULT, %ecx
43         rdmsr
44         movl    $0x010010000, %eax              /*1MB system memory in write back 1|00100|00 */
45         wrmsr
46
47         /* in LX DCDIS is set after POR which disables the cache..., clear this bit */
48         movl    CPU_DM_CONFIG0,%ecx
49         rdmsr
50         andl    $(~(DM_CONFIG0_LOWER_DCDIS_SET)), %eax  /* TODO: make consistent with i$ init,  either whole reg = 0,  or just this bit... */
51         wrmsr
52
53         /* get cache timing params from BIOS config data locations and apply */
54         /* fix delay controls for DM and IM arrays */
55         /* fix delay controls for DM and IM arrays */
56         movl    $CPU_BC_MSS_ARRAY_CTL0, %ecx
57         xorl    %edx, %edx
58         movl    $0x2814D352, %eax
59         wrmsr
60
61         movl    $CPU_BC_MSS_ARRAY_CTL1, %ecx
62         xorl    %edx, %edx
63         movl    $0x1068334D, %eax
64         wrmsr
65
66         movl    $CPU_BC_MSS_ARRAY_CTL2, %ecx
67         movl    $0x00000106, %edx
68         movl    $0x83104104, %eax
69         wrmsr
70
71         movl    $GLCP_FIFOCTL, %ecx
72         rdmsr
73         movl    $0x00000005, %edx
74         wrmsr
75
76         /* Enable setting */
77         movl    $CPU_BC_MSS_ARRAY_CTL_ENA, %ecx
78         xorl    %edx, %edx
79         movl    $0x01, %eax
80         wrmsr
81
82         /* Get cleaned up. */
83         xorl    %edi, %edi
84         xorl    %esi, %esi
85         xorl    %ebp, %ebp
86
87         /* DCache Ways0 through Ways7 will be tagged for LX_STACK_BASE + CONFIG_DCACHE_RAM_SIZE for holding stack */
88         /* remember,  there is NO stack yet... */
89
90         /* Tell cache we want to fill WAY 0 starting at the top */
91         xorl    %edx, %edx
92         xorl    %eax, %eax
93         movl    $CPU_DC_INDEX, %ecx
94         wrmsr
95
96         /* startaddress for tag of Way0: ebp will hold the incrementing address. dont destroy! */
97         movl    $LX_STACK_BASE, %ebp    /* init to start address */
98         orl             $1, %ebp                                /* set valid bit and tag for this Way (B[31:12] : Cache tag value for line/way curr. selected by CPU_DC_INDEX */
99
100         /* start tag Ways 0 with 128 lines with 32bytes each: edi will hold the line counter. dont destroy! */
101         movl    $LX_NUM_CACHELINES, %edi
102 DCacheSetupFillWay:
103
104         /* fill with dummy data: zero it so we can tell it from PCI memory space (returns FFs). */
105         /* We will now store a line (32 bytes = 4 x 8bytes = 4 quadWords) */
106         movw    $0x04, %si
107         xorl    %edx, %edx
108         xorl    %eax, %eax
109         movl    $CPU_DC_DATA, %ecx
110 DCacheSetup_quadWordLoop:
111         wrmsr
112         decw    %si
113         jnz     DCacheSetup_quadWordLoop
114
115         /* Set the tag for this line,  need to do this for every new cache line to validate it! */
116         /* accessing CPU_DC_TAG_I makes the LINE field in CPU_DC_INDEX increment and thus cont. in the next cache line... */
117         xorl    %edx, %edx
118         movl    %ebp, %eax
119         movl    $CPU_DC_TAG, %ecx
120         wrmsr
121
122         /* switch to next line */
123         /* lines are in Bits10:4 */
124         /* when index is crossing 0x7F -> 0x80  writing a RSVD bit as 0x80 is not a valid CL anymore! */
125         movl    $CPU_DC_INDEX, %ecx
126         rdmsr
127         addl    $0x010, %eax /* TODO: prob. would be more elegant to calc. this from counter var edi... */
128         wrmsr
129
130         decl    %edi
131         jnz     DCacheSetupFillWay
132
133         /* 1 Way has been filled,  forward start address for next Way,  terminate if we have reached end of desired address range */
134         addl    $LX_CACHEWAY_SIZE, %ebp
135         cmpl    $LX_STACK_END, %ebp
136         jge     leave_DCacheSetup
137         movl    $LX_NUM_CACHELINES, %edi
138
139         /* switch to next way */
140         movl    $CPU_DC_INDEX, %ecx
141         rdmsr
142         addl    $0x01, %eax
143         andl    $0xFFFFF80F, %eax /* lets be sure: reset line index Bits10:4 */
144         wrmsr
145
146         jmp     DCacheSetupFillWay
147
148 leave_DCacheSetup:
149         xorl    %edi, %edi
150         xorl    %esi, %esi
151         xorl    %ebp, %ebp
152
153         /* Disable the cache,  but ... DO NOT INVALIDATE the tags. */
154         /* Memory reads and writes will all hit in the cache. */
155         /* Cache updates and memory write-backs will not occur ! */
156         movl    %cr0, %eax
157         orl             $(CR0_CD + CR0_NW), %eax        /* set the CD and NW bits */
158         movl    %eax, %cr0
159
160         /* Now point sp to the cached stack. */
161         /* The stack will be fully functional at this location. No system memory is required at all ! */
162         /* set up the stack pointer */
163         movl    $LX_STACK_END, %eax
164         movl    %eax, %esp
165
166         /* test the stack*/
167         movl    $0x0F0F05A5A, %edx
168         pushl   %edx
169         popl    %ecx
170         cmpl    %ecx, %edx
171         je      DCacheSetupGood
172
173         post_code(0xc5)
174 DCacheSetupBad:
175         hlt             /* issues */
176         jmp DCacheSetupBad
177 DCacheSetupGood:
178         /* Go do early init and memory setup */
179
180         /* Restore the BIST result */
181         movl    %ebx, %eax
182         movl    %esp, %ebp
183         pushl   %eax
184
185         post_code(0x23)
186
187         /* Call romstage.c main function */
188         call    main
189 done_cache_as_ram_main:
190
191         /* We now run over the stack-in-cache, copying it back to itself to invalidate the cache */
192
193         push   %edi
194         mov    $(CONFIG_DCACHE_RAM_SIZE/4),%ecx
195         push   %esi
196         mov    $(CONFIG_DCACHE_RAM_BASE),%edi
197         mov    %edi,%esi
198         cld
199         rep movsl %ds:(%esi),%es:(%edi)
200         pop    %esi
201         pop    %edi
202
203         /* Clear the cache out to ram */
204         wbinvd
205         /* re-enable the cache */
206         movl    %cr0, %eax
207         xorl             $(CR0_CD + CR0_NW), %eax        /* clear  the CD and NW bits */
208         movl    %eax, %cr0
209
210         /* clear boot_complete flag */
211         xorl    %ebp, %ebp
212 __main:
213         post_code(0x11)         /* post 11 */
214
215         /* TODO For suspend/resume the cache will have to live between
216          * CONFIG_RAMBASE and CONFIG_RAMTOP
217          */
218
219         cld                             /* clear direction flag */
220
221         /* copy coreboot from it's initial load location to
222          * the location it is compiled to run at.
223          * Normally this is copying from FLASH ROM to RAM.
224          */
225         movl    %ebp, %esi
226         pushl   %esi
227         call copy_and_run
228
229 .Lhlt:
230         post_code(0xee) /* post fail ee */
231         hlt
232         jmp     .Lhlt
233