cache_v8.c 8.0 KB
Newer Older
D
David Feng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * (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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#ifdef CONFIG_SYS_FULL_VA
static void set_ptl1_entry(u64 index, u64 ptl2_entry)
{
	u64 *pgd = (u64 *)gd->arch.tlb_addr;
	u64 value;

	value = ptl2_entry | PTL1_TYPE_TABLE;
	pgd[index] = value;
}

static void set_ptl2_block(u64 ptl1, u64 bfn, u64 address, u64 memory_attrs)
{
	u64 *pmd = (u64 *)ptl1;
	u64 value;

	value = address | PTL2_TYPE_BLOCK | PTL2_BLOCK_AF;
	value |= memory_attrs;
	pmd[bfn] = value;
}

static struct mm_region mem_map[] = CONFIG_SYS_MEM_MAP;

#define PTL1_ENTRIES CONFIG_SYS_PTL1_ENTRIES
#define PTL2_ENTRIES CONFIG_SYS_PTL2_ENTRIES

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static u64 get_tcr(int el, u64 *pips, u64 *pva_bits)
{
	u64 max_addr = 0;
	u64 ips, va_bits;
	u64 tcr;
	int i;

	/* Find the largest address we need to support */
	for (i = 0; i < ARRAY_SIZE(mem_map); i++)
		max_addr = max(max_addr, mem_map[i].base + mem_map[i].size);

	/* Calculate the maximum physical (and thus virtual) address */
	if (max_addr > (1ULL << 44)) {
		ips = 5;
		va_bits = 48;
	} else  if (max_addr > (1ULL << 42)) {
		ips = 4;
		va_bits = 44;
	} else  if (max_addr > (1ULL << 40)) {
		ips = 3;
		va_bits = 42;
	} else  if (max_addr > (1ULL << 36)) {
		ips = 2;
		va_bits = 40;
	} else  if (max_addr > (1ULL << 32)) {
		ips = 1;
		va_bits = 36;
	} else {
		ips = 0;
		va_bits = 32;
	}

	if (el == 1) {
		tcr = TCR_EL1_RSVD | (ips << 32);
	} else if (el == 2) {
		tcr = TCR_EL2_RSVD | (ips << 16);
	} else {
		tcr = TCR_EL3_RSVD | (ips << 16);
	}

	/* PTWs cacheable, inner/outer WBWA and inner shareable */
	tcr |= TCR_TG0_64K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA;
	tcr |= TCR_T0SZ(VA_BITS);

	if (pips)
		*pips = ips;
	if (pva_bits)
		*pva_bits = va_bits;

	return tcr;
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static void setup_pgtables(void)
{
	int l1_e, l2_e;
	unsigned long pmd = 0;
	unsigned long address;

	/* Setup the PMD pointers */
	for (l1_e = 0; l1_e < CONFIG_SYS_MEM_MAP_SIZE; l1_e++) {
		gd->arch.pmd_addr[l1_e] = gd->arch.tlb_addr +
						PTL1_ENTRIES * sizeof(u64);
		gd->arch.pmd_addr[l1_e] += PTL2_ENTRIES * sizeof(u64) * l1_e;
		gd->arch.pmd_addr[l1_e] = ALIGN(gd->arch.pmd_addr[l1_e],
						0x10000UL);
	}

	/* Setup the page tables */
	for (l1_e = 0; l1_e < PTL1_ENTRIES; l1_e++) {
		if (mem_map[pmd].base ==
			(uintptr_t)l1_e << PTL2_BITS) {
			set_ptl1_entry(l1_e, gd->arch.pmd_addr[pmd]);

			for (l2_e = 0; l2_e < PTL2_ENTRIES; l2_e++) {
				address = mem_map[pmd].base
					+ (uintptr_t)l2_e * BLOCK_SIZE;
				set_ptl2_block(gd->arch.pmd_addr[pmd], l2_e,
					       address, mem_map[pmd].attrs);
			}

			pmd++;
		} else {
			set_ptl1_entry(l1_e, 0);
		}
	}
}

#else

130
inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
131
			 u64 memory_type, u64 attribute)
D
David Feng 已提交
132 133 134
{
	u64 value;

Y
York Sun 已提交
135
	value = section | PMD_TYPE_SECT | PMD_SECT_AF;
D
David Feng 已提交
136
	value |= PMD_ATTRINDX(memory_type);
137
	value |= attribute;
138 139 140 141 142 143 144 145
	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;
Y
York Sun 已提交
146
	page_table[index] = value;
D
David Feng 已提交
147
}
148
#endif
D
David Feng 已提交
149 150

/* to activate the MMU we need to set up virtual memory */
151
__weak void mmu_setup(void)
D
David Feng 已提交
152
{
153
#ifndef CONFIG_SYS_FULL_VA
D
David Feng 已提交
154
	bd_t *bd = gd->bd;
155
	u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
156
#endif
157
	int el;
D
David Feng 已提交
158

159 160 161 162 163 164
#ifdef CONFIG_SYS_FULL_VA
	unsigned long coreid = read_mpidr() & CONFIG_COREID_MASK;

	/* Set up page tables only on BSP */
	if (coreid == BSP_COREID)
		setup_pgtables();
165 166 167 168

	el = current_el();
	set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
			  MEMORY_ATTRIBUTES);
169
#else
D
David Feng 已提交
170
	/* Setup an identity-mapping for all spaces */
Y
York Sun 已提交
171 172
	for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
		set_pgtable_section(page_table, i, i << SECTION_SHIFT,
173
				    MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
Y
York Sun 已提交
174
	}
D
David Feng 已提交
175 176 177 178 179 180 181

	/* 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++) {
Y
York Sun 已提交
182
			set_pgtable_section(page_table, j, j << SECTION_SHIFT,
183
					    MT_NORMAL, PMD_SECT_NON_SHARE);
D
David Feng 已提交
184 185 186 187 188
		}
	}

	/* load TTBR0 */
	el = current_el();
189
	if (el == 1) {
Y
York Sun 已提交
190
		set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
191
				  TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
Y
York Sun 已提交
192
				  MEMORY_ATTRIBUTES);
193
	} else if (el == 2) {
Y
York Sun 已提交
194
		set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
195
				  TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
Y
York Sun 已提交
196
				  MEMORY_ATTRIBUTES);
197
	} else {
Y
York Sun 已提交
198
		set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
199
				  TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
Y
York Sun 已提交
200
				  MEMORY_ATTRIBUTES);
201
	}
202 203
#endif

D
David Feng 已提交
204 205 206 207 208 209 210 211 212
	/* 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)
{
213
	__asm_invalidate_dcache_all();
D
David Feng 已提交
214 215 216
}

/*
217 218 219
 * 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
D
David Feng 已提交
220
 */
221
inline void flush_dcache_all(void)
D
David Feng 已提交
222
{
223 224
	int ret;

D
David Feng 已提交
225
	__asm_flush_dcache_all();
226 227 228 229 230
	ret = __asm_flush_l3_cache();
	if (ret)
		debug("flushing dcache returns 0x%x\n", ret);
	else
		debug("flushing dcache successfully.\n");
D
David Feng 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

/*
 * 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;
}

282 283 284 285 286 287
u64 *__weak arch_get_page_table(void) {
	puts("No page table offset defined\n");

	return NULL;
}

288
#ifndef CONFIG_SYS_FULL_VA
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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");
}
314 315
#endif

D
David Feng 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
#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;
}

339 340 341 342 343
void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
				     enum dcache_option option)
{
}

D
David Feng 已提交
344 345 346 347 348 349
#endif	/* CONFIG_SYS_DCACHE_OFF */

#ifndef CONFIG_SYS_ICACHE_OFF

void icache_enable(void)
{
350
	__asm_invalidate_icache_all();
D
David Feng 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	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
 */
Y
York Sun 已提交
394
void __weak enable_caches(void)
D
David Feng 已提交
395 396 397 398
{
	icache_enable();
	dcache_enable();
}