24ea8da5e6ff26f4196ea2ae80f924a910005a2b
[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 <arch/stages.h>
5 #include "cpu/amd/car/disable_cache_as_ram.c"
6
7 static inline void print_debug_pcar(const char *strval, uint32_t val)
8 {
9         printk(BIOS_DEBUG, "%s%08x\n", strval, val);
10 }
11
12 /* from linux kernel 2.6.32 asm/string_32.h */
13
14 static void inline __attribute__((always_inline))  memcopy(void *dest, const void *src, unsigned long bytes)
15 {
16         int d0, d1, d2;
17         asm volatile("cld ; rep ; movsl\n\t"
18                         "movl %4,%%ecx\n\t"
19                         "andl $3,%%ecx\n\t"
20                         "jz 1f\n\t"
21                         "rep ; movsb\n\t"
22                         "1:"
23                         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
24                         : "0" (bytes / 4), "g" (bytes), "1" ((long)dest), "2" ((long)src)
25                         : "memory", "cc");
26 }
27 /* Disable Erratum 343 Workaround, see RevGuide for Fam10h, Pub#41322 Rev 3.33 */
28
29 static void vErrata343(void)
30 {
31 #ifdef BU_CFG2_MSR
32     msr_t msr;
33     unsigned int uiMask = 0xFFFFFFF7;
34
35     msr = rdmsr(BU_CFG2_MSR);
36     msr.hi &= uiMask; // set bit 35 to 0
37     wrmsr(BU_CFG2_MSR, msr);
38 #endif
39 }
40
41 static void post_cache_as_ram(void)
42 {
43
44 #if 1
45         {
46         /* Check value of esp to verify if we have enough rom for stack in Cache as RAM */
47         unsigned v_esp;
48         __asm__ volatile (
49                 "movl   %%esp, %0\n\t"
50                 : "=a" (v_esp)
51         );
52         print_debug_pcar("v_esp=", v_esp);
53         }
54 #endif
55
56         unsigned testx = 0x5a5a5a5a;
57         print_debug_pcar("testx = ", testx);
58
59         /* copy data from cache as ram to
60                 ram need to set CONFIG_RAMTOP to 2M and use var mtrr instead.
61          */
62 #if CONFIG_RAMTOP <= 0x100000
63         #error "You need to set CONFIG_RAMTOP greater than 1M"
64 #endif
65
66         /* So we can access RAM from [1M, CONFIG_RAMTOP) */
67         set_var_mtrr(0, 0x00000000, CONFIG_RAMTOP, MTRR_TYPE_WRBACK);
68
69 //      dump_mem(CONFIG_DCACHE_RAM_BASE+CONFIG_DCACHE_RAM_SIZE-0x8000, CONFIG_DCACHE_RAM_BASE+CONFIG_DCACHE_RAM_SIZE-0x7c00);
70         print_debug("Copying data from cache to RAM -- switching to use RAM as stack... ");
71
72         /* from here don't store more data in CAR */
73         vErrata343();
74
75         memcopy((void *)((CONFIG_RAMTOP)-CONFIG_DCACHE_RAM_SIZE), (void *)CONFIG_DCACHE_RAM_BASE, CONFIG_DCACHE_RAM_SIZE); //inline
76 //      dump_mem((CONFIG_RAMTOP) - 0x8000, (CONFIG_RAMTOP) - 0x7c00);
77
78         __asm__ volatile (
79                 /* set new esp */ /* before CONFIG_RAMBASE */
80                 "subl   %0, %%esp\n\t"
81                 ::"a"( (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE)- (CONFIG_RAMTOP) )
82                 /* discard all registers (eax is used for %0), so gcc redo everything
83                    after the stack is moved */
84                 : "cc", "memory", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp"
85         );
86
87         /* We can put data to stack again */
88
89         /* only global variable sysinfo in cache need to be offset */
90         print_debug("Done\n");
91         print_debug_pcar("testx = ", testx);
92
93         print_debug("Disabling cache as ram now \n");
94         disable_cache_as_ram_bsp();
95
96         print_debug("Clearing initial memory region: ");
97 #if CONFIG_HAVE_ACPI_RESUME == 1
98         /* clear only coreboot used region of memory. Note: this may break ECC enabled boards */
99         memset((void*) CONFIG_RAMBASE, 0, (CONFIG_RAMTOP) - CONFIG_RAMBASE - CONFIG_DCACHE_RAM_SIZE);
100 #else
101         memset((void*)0, 0, ((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_SIZE));
102 #endif
103         print_debug("Done\n");
104
105 //      dump_mem((CONFIG_RAMTOP) - 0x8000, (CONFIG_RAMTOP) - 0x7c00);
106
107         set_sysinfo_in_ram(1); // So other core0 could start to train mem
108
109 #if CONFIG_MEM_TRAIN_SEQ == 1
110 //      struct sys_info *sysinfox = ((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
111
112         // wait for ap memory to trained
113 //      wait_all_core0_mem_trained(sysinfox); // moved to lapic_init_cpus.c
114 #endif
115         /*copy and execute coreboot_ram */
116         copy_and_run(0);
117         /* We will not return */
118
119         print_debug("should not be here -\n");
120 }