| 1 |
/*
|
| 2 |
* Copyright (c) 2012 Yuichi Watanabe
|
| 3 |
* All rights reserved.
|
| 4 |
*
|
| 5 |
* Redistribution and use in source and binary forms, with or without
|
| 6 |
* modification, are permitted provided that the following conditions are met:
|
| 7 |
*
|
| 8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
| 9 |
* this list of conditions and the following disclaimer.
|
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 11 |
* this list of conditions and the following disclaimer in the documentation
|
| 12 |
* and/or other materials provided with the distribution.
|
| 13 |
* 3. Neither the name of the Yuichi Watanabe nor the names of its
|
| 14 |
* contributors may be used to endorse or promote products derived from
|
| 15 |
* this software without specific prior written permission.
|
| 16 |
*
|
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 18 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
| 21 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 22 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 23 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 24 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 25 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 26 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 27 |
* POSSIBILITY OF SUCH DAMAGE.
|
| 28 |
*/
|
| 29 |
|
| 30 |
#include <core/cpu.h>
|
| 31 |
#include <core/initfunc.h>
|
| 32 |
#include <core/printf.h>
|
| 33 |
#include <core/types.h>
|
| 34 |
#include "asm.h"
|
| 35 |
#include "constants.h"
|
| 36 |
#include "mtrr.h"
|
| 37 |
|
| 38 |
u8
|
| 39 |
mtrr_get_fixed_mem_type(phys_t addr)
|
| 40 |
{
|
| 41 |
u64 msr_val;
|
| 42 |
ulong msr_num;
|
| 43 |
phys_t width;
|
| 44 |
phys_t index;
|
| 45 |
u8 type;
|
| 46 |
|
| 47 |
if (addr >= 0xc0000) {
|
| 48 |
width = 0x1000;
|
| 49 |
msr_num = MSR_IA32_MTRR_FIX4K_C000 + (addr - 0xc0000)
|
| 50 |
/ (0x1000 * MSR_IA32_MTRR_FIX_TYPE_NUM);
|
| 51 |
} else if (addr >= 0x80000) {
|
| 52 |
width = 0x4000;
|
| 53 |
msr_num = MSR_IA32_MTRR_FIX16K_800 + (addr - 0x80000)
|
| 54 |
/ (0x4000 * MSR_IA32_MTRR_FIX_TYPE_NUM);
|
| 55 |
} else {
|
| 56 |
width = 0x10000;
|
| 57 |
msr_num = MSR_IA32_MTRR_FIX64K_000;
|
| 58 |
}
|
| 59 |
|
| 60 |
index = ((addr / width) % MSR_IA32_MTRR_FIX_TYPE_NUM);
|
| 61 |
asm_rdmsr64(msr_num, &msr_val);
|
| 62 |
type = (msr_val >> index * MSR_IA32_MTRR_FIX_TYPE_SHIFT)
|
| 63 |
& MSR_IA32_MTRR_FIX_TYPE_MASK;
|
| 64 |
|
| 65 |
return type;
|
| 66 |
}
|
| 67 |
|
| 68 |
u8
|
| 69 |
mtrr_get_mem_type(phys_t addr)
|
| 70 |
{
|
| 71 |
u64 msr_val;
|
| 72 |
u64 mtrrcap;
|
| 73 |
u8 type;
|
| 74 |
u8 vcnt;
|
| 75 |
phys_t base;
|
| 76 |
phys_t mask;
|
| 77 |
int i;
|
| 78 |
|
| 79 |
asm_rdmsr64(MSR_IA32_MTRRCAP, &mtrrcap);
|
| 80 |
|
| 81 |
asm_rdmsr64(MSR_IA32_MTRR_DEF_TYPE, &msr_val);
|
| 82 |
if ((msr_val & MSR_IA32_MTRR_DEF_TYPE_E_BIT) == 0) {
|
| 83 |
return MTRR_ENCODING_UC;
|
| 84 |
}
|
| 85 |
type = msr_val & MSR_IA32_MTRR_DEF_TYPE_TYPE_MASK;
|
| 86 |
|
| 87 |
if ((mtrrcap & MSR_IA32_MTRRCAP_FIX_BIT) &&
|
| 88 |
(msr_val & MSR_IA32_MTRR_DEF_TYPE_FE_BIT) &&
|
| 89 |
addr < 0x100000) {
|
| 90 |
return mtrr_get_fixed_mem_type(addr);
|
| 91 |
}
|
| 92 |
|
| 93 |
vcnt = mtrrcap & MSR_IA32_MTRRCAP_VCNT_MASK;
|
| 94 |
|
| 95 |
for (i = vcnt - 1; i >= 0; i--) {
|
| 96 |
asm_rdmsr64(MSR_IA32_MTRR_PHYSMASK0 + i * 2, &msr_val);
|
| 97 |
if ((msr_val & MSR_IA32_MTRR_PHYSMASK_V_BIT) == 0) {
|
| 98 |
continue;
|
| 99 |
}
|
| 100 |
mask = msr_val & MSR_IA32_MTRR_PHYSMASK_MASK_MASK;
|
| 101 |
|
| 102 |
asm_rdmsr64(MSR_IA32_MTRR_PHYSBASE0 + i * 2, &msr_val);
|
| 103 |
base = msr_val & MSR_IA32_MTRR_PHYSBASE_BASE_MASK;
|
| 104 |
if ((addr & mask) == (base & mask)) {
|
| 105 |
type = msr_val & MSR_IA32_MTRR_PHYSBASE_TYPE_MASK;
|
| 106 |
break;
|
| 107 |
}
|
| 108 |
|
| 109 |
}
|
| 110 |
|
| 111 |
return type;
|
| 112 |
}
|
| 113 |
|
| 114 |
static void
|
| 115 |
mtrr_init(void)
|
| 116 |
{
|
| 117 |
phys_t start = 0;
|
| 118 |
phys_t cur;
|
| 119 |
u8 type, new_type;
|
| 120 |
|
| 121 |
if (get_cpu_id() == 0) {
|
| 122 |
type = mtrr_get_mem_type(0);
|
| 123 |
for (cur = 0;; cur += PAGESIZE) {
|
| 124 |
new_type = mtrr_get_mem_type(cur);
|
| 125 |
if (new_type != type) {
|
| 126 |
if (cur > 0xffffffff &&
|
| 127 |
new_type != MTRR_ENCODING_WB) {
|
| 128 |
break;
|
| 129 |
}
|
| 130 |
printf("MTRR 0x%llx-0x%llx 0x%x\n",
|
| 131 |
start, cur - 1, type);
|
| 132 |
start = cur;
|
| 133 |
type = new_type;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
printf("MTRR 0x%llx-0x%llx 0x%x\n",
|
| 137 |
start, cur - 1, type);
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
INITFUNC("pcpu5", mtrr_init);
|