af5a3f03bcef14b5b9ce1452e7c58262ad09fcc3
[coreboot.git] / src / cpu / amd / model_lx / cpubug.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2006 Indrek Kruusa <indrek.kruusa@artecdesign.ee>
5  * Copyright (C) 2006 Ronald G. Minnich <rminnich@gmail.com>
6  * Copyright (C) 2007 Advanced Micro Devices, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <console/console.h>
24 #include <arch/io.h>
25 #include <stdint.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <bitops.h>
32 #include <cpu/amd/lxdef.h>
33 #include <cpu/x86/msr.h>
34 #include <cpu/x86/cache.h>
35
36
37 #if 0
38 void
39 bug645(void){
40         msr_t msr;
41         rdmsr(CPU_ID_CONFIG);
42         msr.whatever |= ID_CONFIG_SERIAL_SET;
43         wrmsr(msr);
44 }
45
46 void
47 bug573(void){
48         msr_t msr;
49
50         msr = rdmsr(MC_GLD_MSR_PM);
51         msr.eax &= 0xfff3;
52         wrmsr(MC_GLD_MSR_PM);
53 }
54 #endif
55
56 /**************************************************************************
57  *
58  *      pcideadlock
59  *
60  *      Bugtool #465 and #609
61  *      PCI cache deadlock
62  *      There is also fix code in cache and PCI functions. This bug is very is pervasive.
63  *
64  *      Entry:
65  *      Exit:
66  *      Modified:
67  *
68  **************************************************************************/
69 static void
70 pcideadlock(void)
71 {
72         msr_t msr;
73
74         /*
75          * forces serialization of all load misses. Setting this bit prevents the 
76          * DM pipe from backing up if a read request has to be held up waiting 
77          * for PCI writes to complete.
78         */
79         msr = rdmsr(CPU_DM_CONFIG0);
80         msr.hi &= ~(7<<DM_CONFIG0_UPPER_WSREQ_SHIFT);
81         msr.hi |= (2<<DM_CONFIG0_UPPER_WSREQ_SHIFT);
82         msr.lo |= DM_CONFIG0_LOWER_MISSER_SET;
83         wrmsr(CPU_DM_CONFIG0, msr);
84
85         /* interlock instruction fetches to WS regions with data accesses.
86          * This prevents an instruction fetch from going out to PCI if the 
87          * data side is about to make a request.
88          */
89         msr = rdmsr(CPU_IM_CONFIG);
90         msr.lo |= IM_CONFIG_LOWER_QWT_SET;
91         wrmsr(CPU_IM_CONFIG, msr);
92         
93         /* write serialize memory hole to PCI. Need to unWS when something is 
94          * shadowed regardless of cachablility.
95          */
96         msr.lo = 0x021212121;
97         msr.hi = 0x021212121;
98         wrmsr( CPU_RCONF_A0_BF, msr);
99         wrmsr( CPU_RCONF_C0_DF, msr);
100         wrmsr( CPU_RCONF_E0_FF, msr);
101 }
102
103 /**************************************************************************** 
104  *
105  *      CPUbug784
106  *
107  *      Bugtool #784 + #792
108  *
109  *      Fix CPUID instructions for < 3.0 CPUs
110  *
111  *      Entry:
112  *      Exit:
113  *      Modified:
114  *
115  ****************************************************************************/
116
117 void bug784(void)
118 {
119         msr_t msr;
120         //static char *name = "Geode by NSC";
121
122         /* we'll do this the stupid way, for now, but that's the string they want. NO ONE KNOWS why you
123          * would do this -- the OS can figure this type of stuff out!
124          */
125         msr = rdmsr(0x3006);
126         msr.hi = 0x646f6547;
127         wrmsr(0x3006, msr);
128
129         msr = rdmsr(0x3007);
130         msr.hi = 0x79622065;
131         msr.lo = 0x43534e20;
132         wrmsr(0x3007, msr);
133
134         msr = rdmsr(0x3002);
135         wrmsr(0x3008, msr);
136
137         /* More CPUID to match AMD better. #792*/
138         msr = rdmsr(0x3009);
139         msr.hi = 0x0C0C0A13D;
140         msr.lo = 0x00000000;
141         wrmsr(0x3009, msr);
142 }
143
144 /* cpubug 1398: enable MC if we KNOW we have DDR*/
145 /**************************************************************************
146  *
147  *      CPUbugIAENG1398
148  *
149  *      ClearQuest #IAENG1398
150  *      The MC can not be enabled with SDR memory but can for DDR. Enable for
151  *      DDR here if the setup token is "Default"
152  *      Add this back to core by default once 2.0 CPUs are not supported.
153  *      Entry:
154  *      Exit:
155  *      Modified:
156  *
157  **************************************************************************/
158 void eng1398(void)
159 {
160         msr_t msr;
161
162         msr = rdmsr(MSR_GLCP+0x17);
163         if ((msr.lo & 0xff) <= CPU_REV_2_0) {
164                 msr = rdmsr(GLCP_SYS_RSTPLL);
165                 if (msr.lo & (1<<RSTPPL_LOWER_SDRMODE_SHIFT))
166                         return;
167         }
168
169         /* no CMOS/NVRAM to check, so enable MC Clock Gating */
170         msr = rdmsr(MC_GLD_MSR_PM);
171         msr.lo |= 3; /* enable MC clock gating.*/
172         wrmsr(MC_GLD_MSR_PM, msr);
173 }
174
175 /***************************************************************************
176  *
177  *      CPUbugIAENG2900
178  *
179  *      Clear Quest IAENG00002900, VSS 118.150
180  *
181  *      BTB issue causes blue screen in windows, but the fix is required
182  *      for all operating systems.
183  *
184  *      Entry:
185  *      Exit:
186  *      Modified:
187  *
188  **************************************************************************/
189 void
190 eng2900(void)
191 {
192         msr_t msr;
193
194         printk_debug("CPU_BUG:%s\n", __FUNCTION__);
195         /* Clear bit 43, disables the sysenter/sysexit in CPUID3 */
196         msr = rdmsr(0x3003);
197         msr.hi &= 0xFFFFF7FF;
198         wrmsr(0x3003, msr);
199
200         /* change this value to zero if you need to disable this BTB SWAPSiF. */
201         if (1) { 
202
203                 /* Disable enable_actions in DIAGCTL while setting up GLCP */
204                 msr.hi = 0;
205                 msr.lo = 0;
206                 wrmsr(MSR_GLCP + 0x005f, msr);
207
208                 /* Changing DBGCLKCTL register to GeodeLink */
209                 msr.hi = 0;
210                 msr.lo = 0;
211                 wrmsr(MSR_GLCP + 0x0016, msr);
212
213                 msr.hi = 0;
214                 msr.lo = 2;
215                 wrmsr(MSR_GLCP + 0x0016, msr);
216
217                 /* The code below sets up the CPU to stall for 4 GeodeLink 
218                  * clocks when CPU is snooped.  Because setting XSTATE to 0 
219                  * overrides any other XSTATE action, the code will always 
220                  * stall for 4 GeodeLink clocks after a snoop request goes 
221                  * away even if it occured a clock or two later than a 
222                  * different snoop; the stall signal will never 'glitch high' 
223                  * for only one or two CPU clocks with this code.
224                  */
225
226                 /* Send mb0 port 3 requests to upper GeodeLink diag bits 
227                    [63:32] */
228                 msr.hi = 0;
229                 msr.lo = 0x80338041;
230                 wrmsr(MSR_GLIU0 + 0x2005, msr);
231
232                 /* set5m watches request ready from mb0 to CPU (snoop) */
233                 msr.hi = 0x5ad68000;
234                 msr.lo = 0;
235                 wrmsr(MSR_GLCP + 0x0045, msr);
236
237                 /* SET4M will be high when state is idle (XSTATE=11) */
238                 msr.hi = 0;
239                 msr.lo = 0x0140;
240                 wrmsr(MSR_GLCP + 0x0044, msr);
241
242                 /* SET5n to watch for processor stalled state */
243                 msr.hi = 0x2000;
244                 msr.lo = 0;
245                 wrmsr(MSR_GLCP + 0x004D, msr);
246
247                 /* Writing action number 13: XSTATE=0 to occur when CPU is 
248                    snooped unless we're stalled */
249                 msr.hi = 0;
250                 msr.lo = 0x00400000;
251                 wrmsr(MSR_GLCP + 0x0075, msr);
252
253                 /* Writing action number 11: inc XSTATE every GeodeLink clock 
254                    unless we're idle */
255                 msr.hi = 0;
256                 msr.lo = 0x30000;
257                 wrmsr(MSR_GLCP + 0x0073, msr);
258
259                 /* Writing action number 5: STALL_CPU_PIPE when exitting idle 
260                    state or not in idle state */
261                 msr.hi = 0;
262                 msr.lo = 0x00430000;
263                 wrmsr(MSR_GLCP + 0x006D, msr);
264
265                 /* Writing DIAGCTL Register to enable the stall action and to 
266                    let set5m watch the upper GeodeLink diag bits. */
267                 msr.hi = 0;
268                 msr.lo = 0x80004000;
269                 wrmsr(MSR_GLCP + 0x005f, msr);
270         }
271 }
272
273 void bug118253(void)
274 {
275         /* GLPCI PIO Post Control shouldn't be enabled */
276         msr_t msr;
277
278         msr = rdmsr(GLPCI_SPARE);
279         msr.lo &= ~GLPCI_SPARE_LOWER_PPC_SET;
280         wrmsr(GLPCI_SPARE, msr);
281 }
282
283 void bug118339(void)
284 {
285         /* per AMD, do this always */
286         msr_t msr = {0,0};
287         int msrnum;
288
289         /* Disable enable_actions in DIAGCTL while setting up GLCP */
290         wrmsr(MSR_GLCP + 0x005f, msr);
291
292         /*  SET2M fires if VG pri is odd (3, not 2) and Ystate=0 */
293         msrnum =  MSR_GLCP + 0x042;
294         /*      msr.hi =  2d6b8000h */;
295         msr.hi =  0x596b8000;
296         msr.lo =  0x00000a00;
297         wrmsr(msrnum, msr);
298
299         /*  SET3M fires if MBUS changed and VG pri is odd */
300         msrnum =  MSR_GLCP + 0x043;
301         msr.hi =  0x596b8040;
302         msr.lo = 0;
303         wrmsr(msrnum, msr);
304
305         /*  Put VG request data on lower diag bus */
306         msrnum =  MSR_GLIU0 + 0x2005;
307         msr.hi = 0;
308         msr.lo =  0x80338041;
309         wrmsr(msrnum, msr);
310
311         /*  Increment Y state if SET3M if true */
312         msrnum =  MSR_GLCP + 0x074;
313         msr.hi = 0;
314         msr.lo =  0x0000c000;
315         wrmsr(msrnum, msr);
316
317         /*  Set up MBUS action to PRI=3 read of MBIU */
318         msrnum =  MSR_GLCP + 0x020;
319         msr.hi =  0x0000d863;
320         msr.lo =  0x20002000;
321         wrmsr(msrnum, msr);
322
323         /*  Trigger MBUS action if VG=pri3 and Y=0, this blocks most PCI */
324         msrnum =  MSR_GLCP + 0x071;
325         msr.hi = 0;
326         msr.lo =  0x00000c00;
327         wrmsr(msrnum, msr);
328
329         /* Writing DIAGCTL */
330         msrnum =  MSR_GLCP + 0x005f;
331         msr.hi = 0;
332         msr.lo =  0x80004000;
333         wrmsr(msrnum, msr);
334
335         /*  Code to enable FS2 even when BTB and VGTEAR SWAPSiFs are enabled */
336         /*  As per Todd Roberts in PBz1094 and PBz1095 */
337         /*  Moved from CPUREG to CPUBUG per Tom Sylla */
338         msrnum =  0x04C000042;          /*  GLCP SETMCTL Register */;
339         msr = rdmsr(msrnum);
340         msr.hi |= 8;                    /*  Bit 35 = MCP_IN */
341         wrmsr(msrnum, msr);
342 }
343
344
345
346 /****************************************************************************/
347 /***/
348 /**     DisableMemoryReorder*/
349 /***/
350 /**     PBZ 3659:*/
351 /**      The MC reordered transactions incorrectly and breaks coherency.*/
352 /**      Disable reording and take a potential performance hit.*/
353 /**      This is safe to do here and not in MC init since there is nothing*/
354 /**      to maintain coherency with and the cache is not enabled yet.*/
355 /***/
356 /***/
357 /**     Entry:*/
358 /**     Exit:*/
359 /**     Modified:*/
360 /***/
361 /****************************************************************************/
362 void disablememoryreadorder(void)
363 {       
364         msr_t msr;
365         msr = rdmsr(MC_CF8F_DATA);
366
367         msr.hi |=  CF8F_UPPER_REORDER_DIS_SET;
368         wrmsr(MC_CF8F_DATA, msr);
369 }
370
371 void
372 cpubug(void)
373 {
374 #if 0 //GX3: any CPU bugs to fix here? :)
375         msr_t msr;
376         int rev;
377
378         msr = rdmsr(GLCP_CHIP_REVID);
379
380         rev = msr.lo & 0xff;
381         if (rev < 0x20) {
382                 printk_err("%s: rev < 0x20! bailing!\n");
383                 return;
384         }
385         printk_debug("Doing cpubug fixes for rev 0x%x\n", rev);
386         switch(rev)
387         {
388                 case 0x20:
389                         pcideadlock();
390                         eng1398();
391                         /* cs 5530 bug; ignore 
392                         bug752();
393                         */
394                         break;
395                 case 0x21:
396                         pcideadlock();
397                         eng1398();
398                         eng2900();
399                         bug118339();
400                         break;
401                 case 0x22:
402                 case 0x30: 
403                         break;
404                 default:
405                         printk_err("unknown rev %x, bailing\n", rev);
406                         return;
407         }
408         bug784();
409         bug118253();
410         disablememoryreadorder();
411         printk_debug("Done cpubug fixes \n");
412 #endif
413
414 }