| 1 |
|
| 2 |
|
| 3 |
#include <amdblocks/cpu.h> |
| 4 |
#include <amdblocks/iomap.h> |
| 5 |
#include <cpu/amd/mtrr.h> |
| 6 |
#include <cpu/x86/cache.h> |
| 7 |
#include <cpu/x86/msr.h> |
| 8 |
#include <cpu/x86/mtrr.h> |
| 9 |
#include <soc/iomap.h> |
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
|
| 13 |
#define MAX_VAR_MTRR_USE 10 |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
void early_cache_setup(void) |
| 21 |
{ |
| 22 |
msr_t sys_cfg; |
| 23 |
msr_t mtrr_def_type; |
| 24 |
msr_t fixed_mtrr_ram; |
| 25 |
msr_t fixed_mtrr_mmio; |
| 26 |
union mtrr_ctx { |
| 27 |
struct var_mtrr_context ctx; |
| 28 |
char buffer[sizeof(struct var_mtrr_context) |
| 29 |
+ 2 * MAX_VAR_MTRR_USE * sizeof(msr_t)]; |
| 30 |
|
| 31 |
} mtrr_ctx; |
| 32 |
|
| 33 |
var_mtrr_context_init(&mtrr_ctx.ctx); |
| 34 |
mtrr_ctx.ctx.max_var_mtrrs = MIN(MAX_VAR_MTRR_USE, mtrr_ctx.ctx.max_var_mtrrs); |
| 35 |
|
| 36 |
sys_cfg = rdmsr(SYSCFG_MSR); |
| 37 |
sys_cfg.lo |= SYSCFG_MSR_MtrrFixDramModEn; |
| 38 |
|
| 39 |
|
| 40 |
fixed_mtrr_ram.lo = fixed_mtrr_ram.hi = |
| 41 |
((MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM) << 0) | |
| 42 |
((MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM) << 8) | |
| 43 |
((MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM) << 16) | |
| 44 |
((MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM) << 24); |
| 45 |
fixed_mtrr_mmio.lo = fixed_mtrr_mmio.hi = |
| 46 |
((MTRR_TYPE_UNCACHEABLE) << 0) | |
| 47 |
((MTRR_TYPE_UNCACHEABLE) << 8) | |
| 48 |
((MTRR_TYPE_UNCACHEABLE) << 16) | |
| 49 |
((MTRR_TYPE_UNCACHEABLE) << 24); |
| 50 |
|
| 51 |
|
| 52 |
mtrr_def_type = rdmsr(MTRR_DEF_TYPE_MSR); |
| 53 |
mtrr_def_type.lo &= ~MTRR_DEF_TYPE_MASK; |
| 54 |
mtrr_def_type.lo |= MTRR_TYPE_UNCACHEABLE; |
| 55 |
mtrr_def_type.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN; |
| 56 |
|
| 57 |
disable_cache(); |
| 58 |
|
| 59 |
wrmsr(SYSCFG_MSR, sys_cfg); |
| 60 |
|
| 61 |
var_mtrr_set(&mtrr_ctx.ctx, 0, ALIGN_DOWN(get_top_of_mem_below_4gb(), 8 * MiB), |
| 62 |
MTRR_TYPE_WRBACK); |
| 63 |
|
| 64 |
var_mtrr_set(&mtrr_ctx.ctx, FLASH_BELOW_4GB_MAPPING_REGION_BASE, |
| 65 |
FLASH_BELOW_4GB_MAPPING_REGION_SIZE, MTRR_TYPE_WRPROT); |
| 66 |
|
| 67 |
commit_mtrr_setup(&mtrr_ctx.ctx); |
| 68 |
|
| 69 |
|
| 70 |
wrmsr(MTRR_FIX_64K_00000, fixed_mtrr_ram); |
| 71 |
wrmsr(MTRR_FIX_16K_80000, fixed_mtrr_ram); |
| 72 |
wrmsr(MTRR_FIX_16K_A0000, fixed_mtrr_mmio); |
| 73 |
wrmsr(MTRR_FIX_4K_C0000, fixed_mtrr_ram); |
| 74 |
wrmsr(MTRR_FIX_4K_C8000, fixed_mtrr_ram); |
| 75 |
wrmsr(MTRR_FIX_4K_D0000, fixed_mtrr_ram); |
| 76 |
wrmsr(MTRR_FIX_4K_D8000, fixed_mtrr_ram); |
| 77 |
wrmsr(MTRR_FIX_4K_E0000, fixed_mtrr_ram); |
| 78 |
wrmsr(MTRR_FIX_4K_E8000, fixed_mtrr_ram); |
| 79 |
wrmsr(MTRR_FIX_4K_F0000, fixed_mtrr_ram); |
| 80 |
wrmsr(MTRR_FIX_4K_F8000, fixed_mtrr_ram); |
| 81 |
|
| 82 |
wrmsr(MTRR_DEF_TYPE_MSR, mtrr_def_type); |
| 83 |
|
| 84 |
|
| 85 |
sys_cfg.lo |= SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn; |
| 86 |
sys_cfg.lo |= SYSCFG_MSR_TOM2En | SYSCFG_MSR_TOM2WB; |
| 87 |
|
| 88 |
|
| 89 |
wrmsr(SYSCFG_MSR, sys_cfg); |
| 90 |
|
| 91 |
enable_cache(); |
| 92 |
} |