2013-12-14 03:47:35 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2013
|
|
|
|
* David Feng <fenghua@phytium.com.cn>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/armv8/mmu.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
#ifndef CONFIG_SYS_DCACHE_OFF
|
2015-08-18 03:22:05 +00:00
|
|
|
inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
|
|
|
|
u64 memory_type, u64 share)
|
2013-12-14 03:47:35 +00:00
|
|
|
{
|
|
|
|
u64 value;
|
|
|
|
|
2014-06-23 22:15:53 +00:00
|
|
|
value = section | PMD_TYPE_SECT | PMD_SECT_AF;
|
2013-12-14 03:47:35 +00:00
|
|
|
value |= PMD_ATTRINDX(memory_type);
|
2015-08-18 03:22:05 +00:00
|
|
|
value |= share;
|
|
|
|
page_table[index] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
|
|
|
|
{
|
|
|
|
u64 value;
|
|
|
|
|
|
|
|
value = (u64)table_addr | PMD_TYPE_TABLE;
|
2014-06-23 22:15:53 +00:00
|
|
|
page_table[index] = value;
|
2013-12-14 03:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* to activate the MMU we need to set up virtual memory */
|
2015-10-05 18:09:00 +00:00
|
|
|
__weak void mmu_setup(void)
|
2013-12-14 03:47:35 +00:00
|
|
|
{
|
|
|
|
bd_t *bd = gd->bd;
|
2015-07-22 23:10:11 +00:00
|
|
|
u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
|
|
|
|
int el;
|
2013-12-14 03:47:35 +00:00
|
|
|
|
|
|
|
/* Setup an identity-mapping for all spaces */
|
2014-06-23 22:15:53 +00:00
|
|
|
for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
|
|
|
|
set_pgtable_section(page_table, i, i << SECTION_SHIFT,
|
2015-08-18 03:22:05 +00:00
|
|
|
MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
|
2014-06-23 22:15:53 +00:00
|
|
|
}
|
2013-12-14 03:47:35 +00:00
|
|
|
|
|
|
|
/* Setup an identity-mapping for all RAM space */
|
|
|
|
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
|
|
|
|
ulong start = bd->bi_dram[i].start;
|
|
|
|
ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
|
|
|
|
for (j = start >> SECTION_SHIFT;
|
|
|
|
j < end >> SECTION_SHIFT; j++) {
|
2014-06-23 22:15:53 +00:00
|
|
|
set_pgtable_section(page_table, j, j << SECTION_SHIFT,
|
2015-08-18 03:22:05 +00:00
|
|
|
MT_NORMAL, PMD_SECT_NON_SHARE);
|
2013-12-14 03:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load TTBR0 */
|
|
|
|
el = current_el();
|
2014-02-26 21:26:02 +00:00
|
|
|
if (el == 1) {
|
2014-06-23 22:15:53 +00:00
|
|
|
set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
|
2015-08-20 09:52:14 +00:00
|
|
|
TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
|
2014-06-23 22:15:53 +00:00
|
|
|
MEMORY_ATTRIBUTES);
|
2014-02-26 21:26:02 +00:00
|
|
|
} else if (el == 2) {
|
2014-06-23 22:15:53 +00:00
|
|
|
set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
|
2015-08-20 09:52:14 +00:00
|
|
|
TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
|
2014-06-23 22:15:53 +00:00
|
|
|
MEMORY_ATTRIBUTES);
|
2014-02-26 21:26:02 +00:00
|
|
|
} else {
|
2014-06-23 22:15:53 +00:00
|
|
|
set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
|
2015-08-20 09:52:14 +00:00
|
|
|
TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
|
2014-06-23 22:15:53 +00:00
|
|
|
MEMORY_ATTRIBUTES);
|
2014-02-26 21:26:02 +00:00
|
|
|
}
|
2013-12-14 03:47:35 +00:00
|
|
|
/* enable the mmu */
|
|
|
|
set_sctlr(get_sctlr() | CR_M);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs a invalidation of the entire data cache at all levels
|
|
|
|
*/
|
|
|
|
void invalidate_dcache_all(void)
|
|
|
|
{
|
2014-02-26 21:26:04 +00:00
|
|
|
__asm_invalidate_dcache_all();
|
2013-12-14 03:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-06 21:18:42 +00:00
|
|
|
* Performs a clean & invalidation of the entire data cache at all levels.
|
|
|
|
* This function needs to be inline to avoid using stack.
|
|
|
|
* __asm_flush_l3_cache return status of timeout
|
2013-12-14 03:47:35 +00:00
|
|
|
*/
|
2015-01-06 21:18:42 +00:00
|
|
|
inline void flush_dcache_all(void)
|
2013-12-14 03:47:35 +00:00
|
|
|
{
|
2015-01-06 21:18:42 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-12-14 03:47:35 +00:00
|
|
|
__asm_flush_dcache_all();
|
2015-01-06 21:18:42 +00:00
|
|
|
ret = __asm_flush_l3_cache();
|
|
|
|
if (ret)
|
|
|
|
debug("flushing dcache returns 0x%x\n", ret);
|
|
|
|
else
|
|
|
|
debug("flushing dcache successfully.\n");
|
2013-12-14 03:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invalidates range in all levels of D-cache/unified cache
|
|
|
|
*/
|
|
|
|
void invalidate_dcache_range(unsigned long start, unsigned long stop)
|
|
|
|
{
|
|
|
|
__asm_flush_dcache_range(start, stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush range(clean & invalidate) from all levels of D-cache/unified cache
|
|
|
|
*/
|
|
|
|
void flush_dcache_range(unsigned long start, unsigned long stop)
|
|
|
|
{
|
|
|
|
__asm_flush_dcache_range(start, stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dcache_enable(void)
|
|
|
|
{
|
|
|
|
/* The data cache is not active unless the mmu is enabled */
|
|
|
|
if (!(get_sctlr() & CR_M)) {
|
|
|
|
invalidate_dcache_all();
|
|
|
|
__asm_invalidate_tlb_all();
|
|
|
|
mmu_setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
set_sctlr(get_sctlr() | CR_C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dcache_disable(void)
|
|
|
|
{
|
|
|
|
uint32_t sctlr;
|
|
|
|
|
|
|
|
sctlr = get_sctlr();
|
|
|
|
|
|
|
|
/* if cache isn't enabled no need to disable */
|
|
|
|
if (!(sctlr & CR_C))
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_sctlr(sctlr & ~(CR_C|CR_M));
|
|
|
|
|
|
|
|
flush_dcache_all();
|
|
|
|
__asm_invalidate_tlb_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
int dcache_status(void)
|
|
|
|
{
|
|
|
|
return (get_sctlr() & CR_C) != 0;
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:35:07 +00:00
|
|
|
u64 *__weak arch_get_page_table(void) {
|
|
|
|
puts("No page table offset defined\n");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
|
|
|
|
enum dcache_option option)
|
|
|
|
{
|
|
|
|
u64 *page_table = arch_get_page_table();
|
|
|
|
u64 upto, end;
|
|
|
|
|
|
|
|
if (page_table == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
|
|
|
|
MMU_SECTION_SHIFT;
|
|
|
|
start = start >> MMU_SECTION_SHIFT;
|
|
|
|
for (upto = start; upto < end; upto++) {
|
|
|
|
page_table[upto] &= ~PMD_ATTRINDX_MASK;
|
|
|
|
page_table[upto] |= PMD_ATTRINDX(option);
|
|
|
|
}
|
|
|
|
asm volatile("dsb sy");
|
|
|
|
__asm_invalidate_tlb_all();
|
|
|
|
asm volatile("dsb sy");
|
|
|
|
asm volatile("isb");
|
|
|
|
start = start << MMU_SECTION_SHIFT;
|
|
|
|
end = end << MMU_SECTION_SHIFT;
|
|
|
|
flush_dcache_range(start, end);
|
|
|
|
asm volatile("dsb sy");
|
|
|
|
}
|
2013-12-14 03:47:35 +00:00
|
|
|
#else /* CONFIG_SYS_DCACHE_OFF */
|
|
|
|
|
|
|
|
void invalidate_dcache_all(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush_dcache_all(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void dcache_enable(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void dcache_disable(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int dcache_status(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:35:07 +00:00
|
|
|
void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
|
|
|
|
enum dcache_option option)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-14 03:47:35 +00:00
|
|
|
#endif /* CONFIG_SYS_DCACHE_OFF */
|
|
|
|
|
|
|
|
#ifndef CONFIG_SYS_ICACHE_OFF
|
|
|
|
|
|
|
|
void icache_enable(void)
|
|
|
|
{
|
2014-02-26 21:26:04 +00:00
|
|
|
__asm_invalidate_icache_all();
|
2013-12-14 03:47:35 +00:00
|
|
|
set_sctlr(get_sctlr() | CR_I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void icache_disable(void)
|
|
|
|
{
|
|
|
|
set_sctlr(get_sctlr() & ~CR_I);
|
|
|
|
}
|
|
|
|
|
|
|
|
int icache_status(void)
|
|
|
|
{
|
|
|
|
return (get_sctlr() & CR_I) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate_icache_all(void)
|
|
|
|
{
|
|
|
|
__asm_invalidate_icache_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_SYS_ICACHE_OFF */
|
|
|
|
|
|
|
|
void icache_enable(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void icache_disable(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int icache_status(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate_icache_all(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SYS_ICACHE_OFF */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable dCache & iCache, whether cache is actually enabled
|
|
|
|
* depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
|
|
|
|
*/
|
2014-06-23 22:15:54 +00:00
|
|
|
void __weak enable_caches(void)
|
2013-12-14 03:47:35 +00:00
|
|
|
{
|
|
|
|
icache_enable();
|
|
|
|
dcache_enable();
|
|
|
|
}
|