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