Blame SOURCES/kexec-tools-2.0.8-arm64-Add-enable-disable-d-cache-support-for-purgato.patch

de80c6
From 8b40614f4c1925fe94f274dfc69d1a63537fe399 Mon Sep 17 00:00:00 2001
de80c6
Message-Id: <8b40614f4c1925fe94f274dfc69d1a63537fe399.1430751022.git.panand@redhat.com>
de80c6
From: Pratyush Anand <panand@redhat.com>
de80c6
Date: Mon, 4 May 2015 17:43:25 +0530
de80c6
Subject: [PATCH 1/3] arm64: Add enable/disable d-cache support for purgatory
de80c6
de80c6
This patch adds support to enable/disable d-cache, which can be used for
de80c6
faster purgatory sha256 verification.
de80c6
de80c6
Signed-off-by: Pratyush Anand <panand@redhat.com>
de80c6
---
de80c6
 purgatory/arch/arm64/Makefile |   1 +
de80c6
 purgatory/arch/arm64/cache.S  | 222 ++++++++++++++++++++++++++++++++++++++++++
de80c6
 purgatory/arch/arm64/cache.h  |  42 ++++++++
de80c6
 3 files changed, 265 insertions(+)
de80c6
 create mode 100644 purgatory/arch/arm64/cache.S
de80c6
 create mode 100644 purgatory/arch/arm64/cache.h
de80c6
de80c6
diff --git a/purgatory/arch/arm64/Makefile b/purgatory/arch/arm64/Makefile
de80c6
index 5d35161fc5f4..04fef16476fb 100644
de80c6
--- a/purgatory/arch/arm64/Makefile
de80c6
+++ b/purgatory/arch/arm64/Makefile
de80c6
@@ -12,6 +12,7 @@ arm64_PURGATORY_EXTRA_CFLAGS = \
de80c6
 
de80c6
 arm64_PURGATORY_SRCS += \
de80c6
 	purgatory/arch/arm64/entry.S \
de80c6
+	purgatory/arch/arm64/cache.S \
de80c6
 	purgatory/arch/arm64/purgatory-arm64.c
de80c6
 
de80c6
 dist += \
de80c6
diff --git a/purgatory/arch/arm64/cache.S b/purgatory/arch/arm64/cache.S
de80c6
new file mode 100644
de80c6
index 000000000000..6bbdeacdab47
de80c6
--- /dev/null
de80c6
+++ b/purgatory/arch/arm64/cache.S
de80c6
@@ -0,0 +1,222 @@
de80c6
+/*
de80c6
+ * Cache maintenance
de80c6
+ * Some of the routine has been copied from Linux Kernel, therefore
de80c6
+ * copying the license as well.
de80c6
+ *
de80c6
+ * Copyright (C) 2001 Deep Blue Solutions Ltd.
de80c6
+ * Copyright (C) 2012 ARM Ltd.
de80c6
+ * Copyright (C) 2015 Pratyush Anand <panand@redhat.com>
de80c6
+ *
de80c6
+ * This program is free software; you can redistribute it and/or modify
de80c6
+ * it under the terms of the GNU General Public License version 2 as
de80c6
+ * published by the Free Software Foundation.
de80c6
+ *
de80c6
+ * This program is distributed in the hope that it will be useful,
de80c6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
de80c6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
de80c6
+ * GNU General Public License for more details.
de80c6
+ *
de80c6
+ * You should have received a copy of the GNU General Public License
de80c6
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
de80c6
+ */
de80c6
+
de80c6
+#include "cache.h"
de80c6
+
de80c6
+/*
de80c6
+ * dcache_line_size - get the minimum D-cache line size from the CTR register.
de80c6
+ */
de80c6
+	.macro	dcache_line_size, reg, tmp
de80c6
+	mrs	\tmp, ctr_el0			// read CTR
de80c6
+	ubfm	\tmp, \tmp, #16, #19		// cache line size encoding
de80c6
+	mov	\reg, #4			// bytes per word
de80c6
+	lsl	\reg, \reg, \tmp		// actual cache line size
de80c6
+	.endm
de80c6
+
de80c6
+/*
de80c6
+ *	__inval_cache_range(start, end)
de80c6
+ *	- start	- start address of region
de80c6
+ *	- end	- end address of region
de80c6
+ */
de80c6
+__inval_cache_range:
de80c6
+	dcache_line_size x2, x3
de80c6
+	sub	x3, x2, #1
de80c6
+	tst	x1, x3				// end cache line aligned?
de80c6
+	bic	x1, x1, x3
de80c6
+	b.eq	1f
de80c6
+	dc	civac, x1			// clean & invalidate D / U line
de80c6
+1:	tst	x0, x3				// start cache line aligned?
de80c6
+	bic	x0, x0, x3
de80c6
+	b.eq	2f
de80c6
+	dc	civac, x0			// clean & invalidate D / U line
de80c6
+	b	3f
de80c6
+2:	dc	ivac, x0			// invalidate D / U line
de80c6
+3:	add	x0, x0, x2
de80c6
+	cmp	x0, x1
de80c6
+	b.lo	2b
de80c6
+	dsb	sy
de80c6
+	ret
de80c6
+/*
de80c6
+ *	__flush_dcache_range(start, end)
de80c6
+ *	- start	- start address of region
de80c6
+ *	- end	- end address of region
de80c6
+ *
de80c6
+ */
de80c6
+__flush_dcache_range:
de80c6
+	dcache_line_size x2, x3
de80c6
+	sub	x3, x2, #1
de80c6
+	bic	x0, x0, x3
de80c6
+1:	dc	civac, x0			// clean & invalidate D line / unified line
de80c6
+	add	x0, x0, x2
de80c6
+	cmp	x0, x1
de80c6
+	b.lo	1b
de80c6
+	dsb	sy
de80c6
+	ret
de80c6
+
de80c6
+/*
de80c6
+ *	enable_dcache(start, end, page_table)
de80c6
+ *	- start	- start address of ram
de80c6
+ *	- end	- end address of ram
de80c6
+ *	- page_table - base of page table
de80c6
+ */
de80c6
+.globl enable_dcache
de80c6
+enable_dcache:
de80c6
+	stp	x6, x7, [sp,#-16]!
de80c6
+	stp	x16, x17, [sp,#-16]!
de80c6
+	stp	x18, x19, [sp,#-16]!
de80c6
+
de80c6
+	/* save args */
de80c6
+	mov x16, x0	/* first segment start */
de80c6
+	mov x17, x1	/* last segment end */
de80c6
+	mov x18, x2 	/* page table */
de80c6
+	mov x19, x30	/* save ret addr */
de80c6
+
de80c6
+	/*
de80c6
+	 * Invalidate the page tables to avoid potential
de80c6
+	 * dirty cache lines being evicted.
de80c6
+	 */
de80c6
+	mov x0, x18
de80c6
+	add x1, x0, #PAGE_TABLE_SIZE
de80c6
+	bl __inval_cache_range
de80c6
+
de80c6
+	/*
de80c6
+	 * Clear the page tables.
de80c6
+	 */
de80c6
+	mov x0, x18
de80c6
+	add x1, x0, #PAGE_TABLE_SIZE
de80c6
+1:	stp	xzr, xzr, [x0], #16
de80c6
+	stp	xzr, xzr, [x0], #16
de80c6
+	stp	xzr, xzr, [x0], #16
de80c6
+	stp	xzr, xzr, [x0], #16
de80c6
+	cmp	x0, x1
de80c6
+	b.lo	1b
de80c6
+
de80c6
+	/*
de80c6
+	 * Create the identity mapping.
de80c6
+	 */
de80c6
+	ldr	x6, =SECTION_SHIFT
de80c6
+	ldr	x7, =MM_MMUFLAGS
de80c6
+	lsr	x0, x16, x6	//first index
de80c6
+	lsr	x1, x17, x6	//last index
de80c6
+
de80c6
+next_sect:
de80c6
+	lsl	x2, x0, x6	//section
de80c6
+	orr	x2, x2, x7
de80c6
+	str	x2, [x18, x0, lsl #3]
de80c6
+	add	x0, x0, #1
de80c6
+	cmp	x0, x1
de80c6
+	b.ls	next_sect
de80c6
+
de80c6
+	/*
de80c6
+	 * Since the page tables have been populated with non-cacheable
de80c6
+	 * accesses (MMU disabled), invalidate the idmap page
de80c6
+	 * tables again to remove any speculatively loaded cache lines.
de80c6
+	 */
de80c6
+	mov x0, x18
de80c6
+	add x1, x0, #PAGE_TABLE_SIZE
de80c6
+	bl __inval_cache_range
de80c6
+
de80c6
+	mrs 	x0, CurrentEL
de80c6
+	cmp	x0, #12	//EL3
de80c6
+	b.eq	set_el3
de80c6
+	cmp	x0, #8	//EL2
de80c6
+	b.eq	set_el2
de80c6
+	cmp	x0, #4	//EL1
de80c6
+	b.eq	set_el1
de80c6
+	b	done_enable
de80c6
+
de80c6
+set_el1:
de80c6
+	msr	ttbr0_el1, x18
de80c6
+	ldr	x0, =TCR_FLAGS
de80c6
+	orr	x0, x0, #TCR_EL1_IPS_BITS
de80c6
+	msr	tcr_el1, x0
de80c6
+	ldr	x0, =MEMORY_ATTRIBUTES
de80c6
+	msr	mair_el1, x0
de80c6
+	mrs	x0, sctlr_el1
de80c6
+	orr	x0, x0, #CR_M
de80c6
+	orr	x0, x0, #CR_C
de80c6
+	msr	sctlr_el1, x0
de80c6
+	b	done_enable
de80c6
+set_el2:
de80c6
+	msr	ttbr0_el2, x18
de80c6
+	ldr	x0, =TCR_FLAGS
de80c6
+	orr	x0, x0, #TCR_EL2_IPS_BITS
de80c6
+	msr	tcr_el2, x0
de80c6
+	ldr	x0, =MEMORY_ATTRIBUTES
de80c6
+	msr	mair_el2, x0
de80c6
+	mrs	x0, sctlr_el2
de80c6
+	orr	x0, x0, #CR_M
de80c6
+	orr	x0, x0, #CR_C
de80c6
+	msr	sctlr_el2, x0
de80c6
+	b	done_enable
de80c6
+set_el3:
de80c6
+	msr	ttbr0_el3, x18
de80c6
+	ldr	x0, =TCR_FLAGS
de80c6
+	orr	x0, x0, #TCR_EL3_IPS_BITS
de80c6
+	msr	tcr_el3, x0
de80c6
+	ldr	x0, =MEMORY_ATTRIBUTES
de80c6
+	msr	mair_el3, x0
de80c6
+	mrs	x0, sctlr_el3
de80c6
+	orr	x0, x0, #CR_M
de80c6
+	orr	x0, x0, #CR_C
de80c6
+	msr	sctlr_el3, x0
de80c6
+done_enable:
de80c6
+
de80c6
+	mov	x30, x19
de80c6
+	ldp	x18, x19, [sp],#16
de80c6
+	ldp	x16, x17, [sp],#16
de80c6
+	ldp	x6, x7, [sp],#16
de80c6
+
de80c6
+	ret
de80c6
+
de80c6
+.globl disable_dcache
de80c6
+disable_dcache:
de80c6
+	stp	x5, x30, [sp,#-16]!
de80c6
+	mrs 	x5, CurrentEL
de80c6
+	cmp	x5, #12	//EL3
de80c6
+	b.eq	disable_el3
de80c6
+	cmp	x5, #8	//EL2
de80c6
+	b.eq	disable_el2
de80c6
+	cmp	x5, #4	//EL1
de80c6
+	b.eq	disable_el1
de80c6
+	b	done_disable
de80c6
+disable_el3:
de80c6
+	mrs	x5, sctlr_el3
de80c6
+	bic	x5, x2, #CR_M
de80c6
+	bic	x5, x2, #CR_C
de80c6
+	msr	sctlr_el3, x5
de80c6
+	b	done_disable
de80c6
+disable_el2:
de80c6
+	mrs	x5, sctlr_el2
de80c6
+	bic	x5, x2, #CR_M
de80c6
+	bic	x5, x2, #CR_C
de80c6
+	msr	sctlr_el2, x5
de80c6
+	b	done_disable
de80c6
+disable_el1:
de80c6
+	mrs	x5, sctlr_el1
de80c6
+	bic	x5, x2, #CR_M
de80c6
+	bic	x5, x2, #CR_C
de80c6
+	msr	sctlr_el1, x5
de80c6
+done_disable:
de80c6
+	bl __flush_dcache_range
de80c6
+	ldp	x5, x30, [sp],#16
de80c6
+	ret
de80c6
diff --git a/purgatory/arch/arm64/cache.h b/purgatory/arch/arm64/cache.h
de80c6
new file mode 100644
de80c6
index 000000000000..3ca1d7f9a5ca
de80c6
--- /dev/null
de80c6
+++ b/purgatory/arch/arm64/cache.h
de80c6
@@ -0,0 +1,42 @@
de80c6
+#ifndef	__CACHE_H__
de80c6
+#define __CACHE_H__
de80c6
+
de80c6
+#define VA_BITS			42
de80c6
+#define SECTION_SHIFT		29
de80c6
+#define PAGE_TABLE_SIZE		(1 << (VA_BITS - SECTION_SHIFT + 3))
de80c6
+
de80c6
+#define TCR_TG0_64K 		(1 << 14)
de80c6
+#define TCR_SHARED_NON		(0 << 12)
de80c6
+#define TCR_ORGN_WBWA		(1 << 10)
de80c6
+#define TCR_IRGN_WBWA		(1 << 8)
de80c6
+#define TCR_T0SZ(x)		((64 - (x)) << 0)
de80c6
+#define TCR_EL1_IPS_BITS	(3 << 32) /* 42 bits physical address */
de80c6
+#define TCR_EL2_IPS_BITS	(3 << 16) /* 42 bits physical address */
de80c6
+#define TCR_EL3_IPS_BITS	(3 << 16) /* 42 bits physical address */
de80c6
+
de80c6
+#define TCR_FLAGS (TCR_TG0_64K | TCR_SHARED_NON | TCR_ORGN_WBWA | \
de80c6
+		TCR_IRGN_WBWA | TCR_T0SZ(VA_BITS))
de80c6
+
de80c6
+#define MT_DEVICE_NGNRNE	0
de80c6
+#define MT_DEVICE_NGNRE		1
de80c6
+#define MT_DEVICE_GRE		2
de80c6
+#define MT_NORMAL_NC		3
de80c6
+#define MT_NORMAL		4
de80c6
+
de80c6
+#define MEMORY_ATTRIBUTES	((0x00 << (MT_DEVICE_NGNRNE*8)) | \
de80c6
+				(0x04 << (MT_DEVICE_NGNRE*8)) | \
de80c6
+				(0x0c << (MT_DEVICE_GRE*8)) | \
de80c6
+				(0x44 << (MT_NORMAL_NC*8)) | \
de80c6
+				(0xff << (MT_NORMAL*8)))
de80c6
+
de80c6
+#define CR_M			(1 << 0)	/* MMU enable */
de80c6
+#define CR_C			(1 << 2)	/* Dcache enable */
de80c6
+
de80c6
+
de80c6
+#define PMD_TYPE_SECT		(1 << 0)
de80c6
+#define PMD_SECT_AF		(1 << 10)
de80c6
+#define PMD_ATTRINDX(t)		((t) << 2)
de80c6
+#define PMD_FLAGS	(PMD_TYPE_SECT | PMD_SECT_AF)
de80c6
+#define MM_MMUFLAGS	PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS
de80c6
+
de80c6
+#endif
de80c6
-- 
de80c6
2.1.0
de80c6