#include <sys/param.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <machine/pmap.h>
#if defined(PERFCTRS)
struct arm_pmc_funcs *arm_pmc;
#endif
int arm_picache_size;
int arm_picache_line_size;
int arm_picache_ways;
int arm_pdcache_size;
int arm_pdcache_line_size;
int arm_pdcache_ways;
int arm_pcache_type;
int arm_pcache_unified;
int arm_dcache_align;
int arm_dcache_align_mask;
int cpu_do_powersave;
struct cpu_functions armv7_cpufuncs = {
cpufunc_id,
cpufunc_nullop,
cpufunc_control,
cpufunc_auxcontrol,
cpufunc_domains,
armv7_setttb,
cpufunc_dfsr,
cpufunc_dfar,
cpufunc_ifsr,
cpufunc_ifar,
armv7_tlb_flushID,
armv7_tlb_flushID_SE,
armv7_tlb_flushID,
armv7_tlb_flushID_SE,
armv7_tlb_flushD,
armv7_tlb_flushD_SE,
armv7_icache_sync_all,
armv7_icache_sync_range,
armv7_dcache_wbinv_all,
armv7_dcache_wbinv_range,
armv7_dcache_inv_range,
armv7_dcache_wb_range,
armv7_idcache_wbinv_all,
armv7_idcache_wbinv_range,
cpufunc_nullop,
(void *)cpufunc_nullop,
(void *)cpufunc_nullop,
(void *)cpufunc_nullop,
(void *)cpufunc_nullop,
cpufunc_nullop,
armv7_drain_writebuf,
armv7_cpu_sleep,
armv7_context_switch,
armv7_setup
};
struct cpu_functions cpufuncs;
u_int cputype;
int arm_icache_min_line_size = 32;
int arm_dcache_min_line_size = 32;
int arm_idcache_min_line_size = 32;
void arm_get_cachetype_cp15v7 (void);
int arm_dcache_l2_nsets;
int arm_dcache_l2_assoc;
int arm_dcache_l2_linesize;
static int
log2(unsigned int i)
{
int ret = 0;
while (i >>= 1)
ret++;
return (ret);
}
void
arm_get_cachetype_cp15v7(void)
{
uint32_t ctype;
uint32_t cachereg;
uint32_t cache_level_id;
uint32_t sets;
uint32_t sel, level;
__asm volatile("mrc p15, 0, %0, c0, c0, 1"
: "=r" (ctype));
arm_dcache_min_line_size = 1 << (CPU_CT_DMINLINE(ctype) + 2);
arm_icache_min_line_size = 1 << (CPU_CT_IMINLINE(ctype) + 2);
arm_idcache_min_line_size =
min(arm_icache_min_line_size, arm_dcache_min_line_size);
__asm volatile("mrc p15, 1, %0, c0, c0, 1"
: "=r" (cache_level_id) :);
cpu_drain_writebuf();
level = 0;
if (cache_level_id & (0x7 << level)) {
if (cache_level_id & (0x4 << level))
arm_pcache_unified = 1;
if (cache_level_id & (0x4 << level) ||
cache_level_id & (0x2 << level)) {
sel = level << 1 | 0 << 0;
__asm volatile("mcr p15, 2, %0, c0, c0, 0"
:: "r" (sel));
cpu_drain_writebuf();
__asm volatile("mrc p15, 1, %0, c0, c0, 0"
: "=r" (cachereg) :);
cpu_drain_writebuf();
sets = ((cachereg >> 13) & 0x7fff) + 1;
arm_pdcache_line_size = 1 << ((cachereg & 0x7) + 4);
arm_pdcache_ways = ((cachereg >> 3) & 0x3ff) + 1;
arm_pdcache_size = arm_pdcache_line_size * arm_pdcache_ways * sets;
switch (cachereg & 0xc0000000) {
case 0x00000000:
arm_pcache_type = 0;
break;
case 0x40000000:
case 0xc0000000:
arm_pcache_type = CPU_CT_CTYPE_WB1;
break;
case 0x80000000:
arm_pcache_type = CPU_CT_CTYPE_WT;
break;
}
}
if (cache_level_id & (0x1 << level)) {
sel = level << 1 | 1 << 0;
__asm volatile("mcr p15, 2, %0, c0, c0, 0"
:: "r" (sel));
cpu_drain_writebuf();
__asm volatile("mrc p15, 1, %0, c0, c0, 0"
: "=r" (cachereg) :);
cpu_drain_writebuf();
sets = ((cachereg >> 13) & 0x7fff) + 1;
arm_picache_line_size = 1 << ((cachereg & 0x7) + 4);
arm_picache_ways = ((cachereg >> 3) & 0x3ff) + 1;
arm_picache_size = arm_picache_line_size * arm_picache_ways * sets;
}
}
arm_dcache_align = arm_pdcache_line_size;
arm_dcache_align_mask = arm_dcache_align - 1;
arm_dcache_l2_nsets = arm_pdcache_size/arm_pdcache_ways/arm_pdcache_line_size;
arm_dcache_l2_assoc = log2(arm_pdcache_ways);
arm_dcache_l2_linesize = log2(arm_pdcache_line_size);
}
void
armv7_idcache_wbinv_all(void)
{
uint32_t arg;
arg = 0;
__asm volatile("mcr p15, 0, r0, c7, c5, 0" :: "r" (arg));
armv7_dcache_wbinv_all();
}
void
armv7_dcache_wbinv_all(void)
{
int sets, ways, lvl;
int nsets, nways;
uint32_t wayincr, setincr;
uint32_t wayval, setval;
uint32_t word;
nsets = arm_dcache_l2_nsets;
nways = arm_pdcache_ways;
setincr = armv7_dcache_sets_inc;
wayincr = armv7_dcache_index_inc;
#if 0
printf("l1 nsets %d nways %d wayincr %x setincr %x\n",
nsets, nways, wayincr, setincr);
#endif
lvl = 0;
setval = 0;
for (sets = 0; sets < nsets; sets++) {
wayval = 0;
for (ways = 0; ways < nways; ways++) {
word = wayval | setval | lvl;
__asm volatile("mcr p15, 0, %0, c7, c10, 2"
: : "r" (word));
wayval += wayincr;
}
setval += setincr;
}
cpu_drain_writebuf();
}
int
set_cpufuncs(void)
{
cputype = cpufunc_id();
cputype &= CPU_ID_CPU_MASK;
if ((cputype & CPU_ID_ARCH_MASK) == CPU_ID_ARCH_CPUID) {
uint32_t mmfr0;
__asm volatile("mrc p15, 0, %0, c0, c1, 4"
: "=r" (mmfr0));
switch (mmfr0 & ID_MMFR0_VMSA_MASK) {
case VMSA_V7:
case VMSA_V7_PXN:
case VMSA_V7_LDT:
cpufuncs = armv7_cpufuncs;
arm_get_cachetype_cp15v7();
armv7_dcache_sets_inc = 1U << arm_dcache_l2_linesize;
armv7_dcache_sets_max = (1U << (arm_dcache_l2_linesize +
arm_dcache_l2_nsets)) - armv7_dcache_sets_inc;
armv7_dcache_index_inc = 1U << (32 -
arm_dcache_l2_assoc);
armv7_dcache_index_max = 0U - armv7_dcache_index_inc;
pmap_pte_init_armv7();
cpu_do_powersave = 1;
return 0;
}
}
panic("No support for this CPU type (%08x) in kernel", cputype);
return(ARCHITECTURE_NOT_PRESENT);
}
void
armv7_setup(void)
{
uint32_t auxctrl, auxctrlmask;
uint32_t cpuctrl, cpuctrlmask;
uint32_t id_pfr1;
auxctrl = auxctrlmask = 0;
switch (cputype & CPU_ID_CORTEX_MASK) {
case CPU_ID_CORTEX_A5:
case CPU_ID_CORTEX_A9:
#ifdef notyet
auxctrlmask |= CORTEXA9_AUXCTL_FW;
auxctrl |= CORTEXA9_AUXCTL_FW;
#endif
case CPU_ID_CORTEX_A7:
case CPU_ID_CORTEX_A12:
case CPU_ID_CORTEX_A15:
case CPU_ID_CORTEX_A17:
auxctrlmask |= CORTEXA9_AUXCTL_SMP;
auxctrl |= CORTEXA9_AUXCTL_SMP;
break;
}
cpuctrlmask = CPU_CONTROL_MMU_ENABLE
| CPU_CONTROL_AFLT_ENABLE
| CPU_CONTROL_DC_ENABLE
| CPU_CONTROL_BPRD_ENABLE
| CPU_CONTROL_IC_ENABLE
| CPU_CONTROL_VECRELOC
| CPU_CONTROL_TRE
| CPU_CONTROL_AFE;
cpuctrl = CPU_CONTROL_MMU_ENABLE
| CPU_CONTROL_DC_ENABLE
| CPU_CONTROL_BPRD_ENABLE
| CPU_CONTROL_IC_ENABLE
| CPU_CONTROL_AFE;
if (vector_page == ARM_VECTORS_HIGH)
cpuctrl |= CPU_CONTROL_VECRELOC;
__asm volatile("mrc p15, 0, %0, c0, c1, 1" : "=r"(id_pfr1));
if ((id_pfr1 & 0x0000f000) == 0x00001000) {
cpuctrlmask |= CPU_CONTROL_UWXN;
cpuctrl |= CPU_CONTROL_UWXN;
}
cpu_idcache_wbinv_all();
cpu_auxcontrol(auxctrlmask, auxctrl);
cpu_control(cpuctrlmask, cpuctrl);
cpu_idcache_wbinv_all();
}