1) wraps the s3 parts of chipset code/memory init code with if CONFIG_HAVE_ACPI_RESUM...
[coreboot.git] / src / cpu / amd / car / post_cache_as_ram.c
1 /* 2005.6 by yhlu
2  * 2006.3 yhlu add copy data from CAR to ram
3  */
4 #include <string.h>
5 #include <arch/stages.h>
6 #include <cpu/x86/mtrr.h>
7 #include "cpu/amd/car/disable_cache_as_ram.c"
8 #include "cpu/x86/mtrr/earlymtrr.c"
9
10 static inline void print_debug_pcar(const char *strval, uint32_t val)
11 {
12         printk(BIOS_DEBUG, "%s%08x\n", strval, val);
13 }
14
15 /* from linux kernel 2.6.32 asm/string_32.h */
16
17 static void inline __attribute__((always_inline))  memcopy(void *dest, const void *src, unsigned long bytes)
18 {
19         int d0, d1, d2;
20         asm volatile("cld ; rep ; movsl\n\t"
21                         "movl %4,%%ecx\n\t"
22                         "andl $3,%%ecx\n\t"
23                         "jz 1f\n\t"
24                         "rep ; movsb\n\t"
25                         "1:"
26                         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
27                         : "0" (bytes / 4), "g" (bytes), "1" ((long)dest), "2" ((long)src)
28                         : "memory", "cc");
29 }
30
31 #if CONFIG_HAVE_ACPI_RESUME == 1
32
33 static inline void *backup_resume(void) {
34         unsigned long high_ram_base;
35         void *resume_backup_memory;
36         int suspend = acpi_is_wakeup_early();
37
38         if (!suspend)
39                 return NULL;
40
41         /* Start address of high memory tables */
42         high_ram_base = (u32) get_cbmem_toc();
43
44         print_debug_pcar("CBMEM TOC is at: ", (uint32_t)high_ram_base);
45         print_debug_pcar("CBMEM TOC 0-size: ",(uint32_t)(high_ram_base + HIGH_MEMORY_SIZE + 4096));
46
47         cbmem_reinit((u64)high_ram_base);
48
49         resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
50
51         /* copy 1MB - 64K to high tables ram_base to prevent memory corruption
52          * through stage 2. We could keep stuff like stack and heap in high tables
53          * memory completely, but that's a wonderful clean up task for another
54          * day.
55          */
56
57         if (resume_backup_memory) {
58                 print_debug_pcar("Will copy coreboot region to: ", (uint32_t) resume_backup_memory);
59                 /* copy only backup only memory used for CAR */
60                 memcopy(resume_backup_memory+HIGH_MEMORY_SAVE-CONFIG_DCACHE_RAM_SIZE,
61                         (void *)((CONFIG_RAMTOP)-CONFIG_DCACHE_RAM_SIZE),
62                          CONFIG_DCACHE_RAM_SIZE); //inline
63         }
64
65         return resume_backup_memory;
66 }
67 #endif
68
69 /* Disable Erratum 343 Workaround, see RevGuide for Fam10h, Pub#41322 Rev 3.33 */
70
71 static void vErrata343(void)
72 {
73 #ifdef BU_CFG2_MSR
74     msr_t msr;
75     unsigned int uiMask = 0xFFFFFFF7;
76
77     msr = rdmsr(BU_CFG2_MSR);
78     msr.hi &= uiMask; // set bit 35 to 0
79     wrmsr(BU_CFG2_MSR, msr);
80 #endif
81 }
82
83 static void post_cache_as_ram(void)
84 {
85 #if CONFIG_HAVE_ACPI_RESUME == 1
86         void *resume_backup_memory;
87 #endif
88 #if 1
89         {
90         /* Check value of esp to verify if we have enough rom for stack in Cache as RAM */
91         unsigned v_esp;
92         __asm__ volatile (
93                 "movl   %%esp, %0\n\t"
94                 : "=a" (v_esp)
95         );
96         print_debug_pcar("v_esp=", v_esp);
97         }
98 #endif
99
100         unsigned testx = 0x5a5a5a5a;
101         print_debug_pcar("testx = ", testx);
102
103         /* copy data from cache as ram to
104                 ram need to set CONFIG_RAMTOP to 2M and use var mtrr instead.
105          */
106 #if CONFIG_RAMTOP <= 0x100000
107         #error "You need to set CONFIG_RAMTOP greater than 1M"
108 #endif
109
110 #if CONFIG_HAVE_ACPI_RESUME == 1
111         resume_backup_memory = backup_resume();
112 #endif
113
114         print_debug("Copying data from cache to RAM -- switching to use RAM as stack... ");
115
116         /* from here don't store more data in CAR */
117         vErrata343();
118
119         memcopy((void *)((CONFIG_RAMTOP)-CONFIG_DCACHE_RAM_SIZE), (void *)CONFIG_DCACHE_RAM_BASE, CONFIG_DCACHE_RAM_SIZE); //inline
120
121         __asm__ volatile (
122                 /* set new esp */ /* before CONFIG_RAMBASE */
123                 "subl   %0, %%esp\n\t"
124                 ::"a"( (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE)- (CONFIG_RAMTOP) )
125                 /* discard all registers (eax is used for %0), so gcc redo everything
126                    after the stack is moved */
127                 : "cc", "memory", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp"
128         );
129
130         /* We can put data to stack again */
131
132         /* only global variable sysinfo in cache need to be offset */
133         print_debug("Done\n");
134         print_debug_pcar("testx = ", testx);
135
136         print_debug("Disabling cache as ram now \n");
137
138         disable_cache_as_ram_bsp();
139
140         disable_cache();
141         set_var_mtrr(0, 0x00000000, CONFIG_RAMTOP, MTRR_TYPE_WRBACK);
142         enable_cache();
143
144 #if CONFIG_HAVE_ACPI_RESUME == 1
145         /* now copy the rest of the area, using the WB method because we already
146            run normal RAM */
147         if (resume_backup_memory) {
148                 memcopy(resume_backup_memory,
149                                 (void *)(CONFIG_RAMBASE),
150                                 (CONFIG_RAMTOP) - CONFIG_RAMBASE - CONFIG_DCACHE_RAM_SIZE);
151         }
152 #endif
153
154         print_debug("Clearing initial memory region: ");
155
156 #if CONFIG_HAVE_ACPI_RESUME == 1
157         /* clear only coreboot used region of memory. Note: this may break ECC enabled boards */
158         memset((void*) CONFIG_RAMBASE, 0, (CONFIG_RAMTOP) - CONFIG_RAMBASE - CONFIG_DCACHE_RAM_SIZE);
159 #else
160         memset((void*)0, 0, ((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_SIZE));
161 #endif
162         print_debug("Done\n");
163
164         set_sysinfo_in_ram(1); // So other core0 could start to train mem
165
166 #if CONFIG_MEM_TRAIN_SEQ == 1
167 //      struct sys_info *sysinfox = ((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
168
169         // wait for ap memory to trained
170 //      wait_all_core0_mem_trained(sysinfox); // moved to lapic_init_cpus.c
171 #endif
172         /*copy and execute coreboot_ram */
173         copy_and_run(0);
174         /* We will not return */
175
176         print_debug("should not be here -\n");
177 }