Skip to contentSkip to conversation

add armv7m cache support

PATCH Superseded Vikas Manocha 2 participants 6 messages Mar 9, 2017 – Mar 10, 2017

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover add armv7m cache support

This patchset adds armv7m instruction/data caches support & enable it for stm32f7.

Vikas Manocha (2):
armv7m: add instruction & data cache support
stm32f7: enable instruction & data cache

 arch/arm/cpu/armv7m/Makefile      |   2 +-
 arch/arm/cpu/armv7m/cache.c       | 295 ++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/armv7m.h     |  23 ++-
 arch/arm/lib/Makefile             |   2 +
 arch/arm/mach-stm32/stm32f7/soc.c |   2 +
 include/configs/stm32f746-disco.h |   4 +-
 6 files changed, 322 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm/cpu/armv7m/cache.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 armv7m: add instruction & data cache support

This patch adds armv7m instruction & data cache support.

---
 arch/arm/cpu/armv7m/Makefile  |   2 +-
 arch/arm/cpu/armv7m/cache.c   | 295 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/armv7m.h |  23 +++-
 arch/arm/lib/Makefile         |   2 +
 4 files changed, 319 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/cpu/armv7m/cache.c
arch/arm/cpu/armv7m/Makefile +1 -1
index aff60e8..41efe11 100644
@@ -6,4 +6,4 @@
 #
 
 extra-y := start.o
-obj-y += cpu.o
+obj-y += cpu.o cache.o
arch/arm/cpu/armv7m/cache.c new file +295 -0

295 lines not on this page · raw message

arch/arm/include/asm/armv7m.h +21 -2
index 54d8a2b..9b3d73d 100644
@@ -16,8 +16,15 @@
 .thumb
 #endif
 
-#define V7M_SCB_BASE		0xE000ED00
-#define V7M_MPU_BASE		0xE000ED90
+/* armv7m fixed base addresses */
+#define V7M_SCS_BASE		(0xE000E000)
+#define V7M_NVIC_BASE		(V7M_SCS_BASE + 0x0100)
+#define V7M_SCB_BASE		(V7M_SCS_BASE + 0x0D00)
+#define V7M_PROC_FTR_BASE	(V7M_SCS_BASE + 0x0D78)
+#define V7M_MPU_BASE		(V7M_SCS_BASE + 0x0D90)
+#define V7M_FPU_BASE		(V7M_SCS_BASE + 0x0F30)
+#define V7M_CACHE_MAINT_BASE	(V7M_SCS_BASE + 0x0F50)
+#define V7M_ACCESS_CNTL_BASE	(V7M_SCS_BASE + 0x0F90)
 
 #define V7M_SCB_VTOR		0x08
 
@@ -27,6 +34,18 @@ struct v7m_scb {
 	uint32_t icsr;		/* Interrupt Control and State Register */
 	uint32_t vtor;		/* Vector Table Offset Register */
 	uint32_t aircr;		/* App Interrupt and Reset Control Register */
+	uint32_t scr;		/* offset 0x10 */
+	uint32_t ccr;		/* offset 0x14 */
+	uint32_t shpr1;		/* offset 0x18 */
+	uint32_t shpr2;		/* offset 0x1c */
+	uint32_t shpr3;		/* offset 0x20 */
+	uint32_t shcrs;		/* offset 0x24 */
+	uint32_t cfsr;		/* offset 0x28 */
+	uint32_t hfsr;		/* offset 0x2C */
+	uint32_t res;		/* offset 0x30 */
+	uint32_t mmar;		/* offset 0x34 */
+	uint32_t bfar;		/* offset 0x38 */
+	uint32_t afsr;		/* offset 0x3C */
 };
 #define V7M_SCB				((struct v7m_scb *)V7M_SCB_BASE)
 
arch/arm/lib/Makefile +2 -0
index 166fa9e..52b36b3 100644
@@ -55,8 +55,10 @@ endif
 
 obj-y	+= cache.o
 ifndef CONFIG_ARM64
+ifndef CONFIG_CPU_V7M
 obj-y	+= cache-cp15.o
 endif
+endif
 
 obj-y	+= psci-dt.o
 
Signature · 1 line
1.9.1
#2 lore raw

PATCH 1/2 armv7m: add instruction & data cache support

Quoted 42 lines · Vikas Manocha

This patch adds armv7m instruction & data cache support.

---
 arch/arm/cpu/armv7m/Makefile  |   2 +-
 arch/arm/cpu/armv7m/cache.c   | 295 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/armv7m.h |  23 +++-
 arch/arm/lib/Makefile         |   2 +
 4 files changed, 319 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/cpu/armv7m/cache.c
arch/arm/cpu/armv7m/Makefile +1 -1
index aff60e8..41efe11 100644
@@ -6,4 +6,4 @@
 #
 
 extra-y := start.o
-obj-y += cpu.o
+obj-y += cpu.o cache.o
arch/arm/cpu/armv7m/cache.c new file +15 -0
new file mode 100644
index 0000000..0fded33
@@ -0,0 +1,295 @@
+/*
+ * (C) Copyright 2017
+ * Vikas Manocha, ST Micoelectronics, vikas.manocha at st.com.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/armv7m.h>
+#include <asm/io.h>
+#include <errno.h>
+
+struct v7m_cache {
+	uint32_t ic_iallu;	/* offset 0x0*/
+	uint32_t res;		/* offset 0x4*/

arch/arm/cpu/armv7m/cache.c line 25

Quoted 10 lines
arch/arm/cpu/armv7m/cache.c new file +10 -0
+	uint32_t ic_imvalu;	/* offset 0x8*/
+	uint32_t dc_imvac;	/* offset 0xc*/
+	uint32_t dc_isw;	/* offset 0x10*/
+	uint32_t dc_cmvau;	/* offset 0x14*/
+	uint32_t dc_cmvac;	/* offset 0x18*/
+	uint32_t dc_csw;	/* offset 0x1c*/
+	uint32_t dc_cimvac;	/* offset 0x20*/
+	uint32_t dc_cisw;	/* offset 0x24*/
+	uint32_t bp_iall;	/* offset 0x28*/
+};

We should get rid of this struct register definitions, just use #define MACRO ...

Quoted 30 lines
arch/arm/cpu/armv7m/cache.c new file +30 -0
+struct v7m_proc_features {
+	uint32_t clidr;		/* offset 0x00 */
+	uint32_t ctr;		/* offset 0x04 */
+	uint32_t ccsidr;	/* offset 0x08 */
+	uint32_t csselr;	/* offset 0x0c */
+};
+
+struct v7m_cache *const v7m_cache_maint = (void *)V7M_CACHE_MAINT_BASE;
+struct v7m_proc_features *const v7m_processor = (void *)V7M_PROC_FTR_BASE;
+
+#define WAYS_SHIFT		30
+#define SETS_SHIFT		5
+
+enum cache_type {
+	DCACHE = 0,
+	ICACHE,
+};
+
+/* PoU : Point of Unification, Poc: Point of Coherency */
+enum cache_action {
+	INVALIDATE_POU,		/* for i-cache invalidate by address */
+	INVALIDATE_POC,		/* for d-cache invalidate by address */
+	INVALIDATE_SET_WAY,	/* for d-cache invalidate by sets/ways */
+	FLUSH_POU,
+	FLUSH_POC,
+	FLUSH_SET_WAY,
+	FLUSH_INVAL_POC,
+	FLUSH_INVAL_SET_WAY,
+};
+

arch/arm/cpu/armv7m/cache.c line 66

Quoted 10 lines
arch/arm/cpu/armv7m/cache.c new file +10 -0
+#ifndef CONFIG_SYS_DCACHE_OFF
+struct dcache_config {
+	uint32_t ways;
+	uint32_t sets;
+};
+
+static void get_cache_ways_sets(struct dcache_config *cache)
+{
+	cache->ways = (readl(&v7m_processor->ccsidr) & GENMASK(12, 3)) >> 3;
+	cache->sets = (readl(&v7m_processor->ccsidr) & GENMASK(27, 13)) >> 13;

What are these ad-hoc numbers ? Please use macros ...

Quoted 21 lines
arch/arm/cpu/armv7m/cache.c new file +21 -0
+}
+
+static uint32_t *get_action_reg_set_ways(enum cache_action action)
+{
+	uint32_t *action_reg;
+
+	switch (action) {
+	case INVALIDATE_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_isw;
+		break;
+	case FLUSH_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_csw;
+		break;
+	case FLUSH_INVAL_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_cisw;
+		break;
+	default:
+		action_reg = NULL;
+		break;
+	};
+

arch/arm/cpu/armv7m/cache.c line 97

Quoted 10 lines
arch/arm/cpu/armv7m/cache.c new file +10 -0
+	return action_reg;
+}
+
+static uint32_t *get_action_reg_range(enum cache_action action)
+{
+	uint32_t *action_reg;
+
+	switch (action) {
+	case INVALIDATE_POU:
+		action_reg = &v7m_cache_maint->ic_imvalu;

Just use return FOO here , if this function is needed at all ...

Quoted 22 lines
arch/arm/cpu/armv7m/cache.c new file +22 -0
+		break;
+	case INVALIDATE_POC:
+		action_reg = &v7m_cache_maint->dc_imvac;
+		break;
+	case FLUSH_POU:
+		action_reg = &v7m_cache_maint->dc_cmvau;
+		break;
+	case FLUSH_POC:
+		action_reg = &v7m_cache_maint->dc_cmvac;
+		break;
+	case FLUSH_INVAL_POC:
+		action_reg = &v7m_cache_maint->dc_cimvac;
+		break;
+	default:
+		action_reg = NULL;
+		break;
+	}
+
+	return action_reg;
+}
+
+static uint32_t get_cline_size(enum cache_type type)

arch/arm/cpu/armv7m/cache.c line 129

Quoted 10 lines
arch/arm/cpu/armv7m/cache.c new file +10 -0
+{
+	uint32_t size;
+
+	if (type == DCACHE)
+		clrbits_le32(v7m_processor->csselr, BIT(0));
+	else if (type == ICACHE)
+		clrbits_le32(v7m_processor->csselr, BIT(1));
+	dsb();
+
+	size = (readl(&v7m_processor->ccsidr) & GENMASK(2, 0));

Extra parenthesis around the statement not needed.

Quoted 55 lines
arch/arm/cpu/armv7m/cache.c new file +55 -0
+	size = size * 4 + 4; /* 0 means 4, 1 means 8 & so on */
+	debug("cache line size is %d\n", size);
+
+	return size;
+}
+
+int action_cache_range(enum cache_action action, uint32_t start_addr,
+		       int64_t size)
+{
+	uint32_t cline_size;
+	uint32_t *action_reg;
+	enum cache_type type;
+
+	action_reg = get_action_reg_range(action);
+	if (!action_reg)
+		return -EINVAL;
+	if (action == INVALIDATE_POU)
+		type = ICACHE;
+	else
+		type = DCACHE;
+
+	/* cache line size is minium size for the cache action */
+	cline_size = get_cline_size(type);
+	do {
+		writel(start_addr, action_reg);
+		size -= cline_size;
+		start_addr += cline_size;
+	} while (size > cline_size);
+	debug("cache action on range done\n");
+	dsb();
+	isb();
+
+	return 0;
+}
+
+static int action_dcache_all(enum cache_action action)
+{
+	struct dcache_config cache;
+	uint32_t *action_reg;
+	int i, j;
+
+	action_reg = get_action_reg_set_ways(action);
+	if (!action_reg)
+		return -EINVAL;
+
+	clrbits_le32(v7m_processor->csselr, BIT(0));	/* select data cache */
+	dsb();
+	get_cache_ways_sets(&cache);	/* Get number of ways & sets */
+	debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
+	for (i = cache.sets; i >= 0; i--) {
+		for (j = cache.ways; j >= 0; j--) {
+			writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
+			       action_reg);
+		}
+	}

arch/arm/cpu/armv7m/cache.c line 194

Quoted 10 lines
arch/arm/cpu/armv7m/cache.c new file +10 -0
+	dsb();
+	isb();
+
+	return 0;
+}
+
+void dcache_enable(void)
+{
+	if (dcache_status())	/* return if cache already enabled */
+		return;

Add newline here

arch/arm/cpu/armv7m/cache.c line 198

Quoted 4 lines
arch/arm/cpu/armv7m/cache.c new file +4 -0
+	if (action_dcache_all(INVALIDATE_SET_WAY)) {
+		printf("ERR: D-cache not enabled\n");
+		return;
+	}

And here ... just add newline between if statements etc to make the code a bit more readable.

arch/arm/cpu/armv7m/cache.c line 199

Quoted 1 line
arch/arm/cpu/armv7m/cache.c new file +1 -0
+	setbits_le32(&V7M_SCB->ccr, BIT(16));

What is this ad-hoc BIT ? Define and use macros for these bits ...

Quoted 101 lines
arch/arm/cpu/armv7m/cache.c new file +96 -0
+	dsb();
+	isb();
+}
+
+void dcache_disable(void)
+{
+	/* if dcache is enabled-> dcache disable & then flush */
+	if (dcache_status()) {
+		if (action_dcache_all(FLUSH_SET_WAY)) {
+			printf("ERR: D-cahe not flushed\n");
+			return;
+		}
+		clrbits_le32(&V7M_SCB->ccr, BIT(16));
+		dsb();
+		isb();
+	}
+}
+
+int dcache_status(void)
+{
+	return (readl(&V7M_SCB->ccr) & BIT(16)) != 0;
+}
+
+#else
+void dcache_enable(void)
+{
+	return;
+}
+
+void dcache_disable(void)
+{
+	return;
+}
+
+int dcache_status(void)
+{
+	return 0;
+}
+#endif
+
+#ifndef CONFIG_SYS_ICACHE_OFF
+
+void invalidate_icache_all(void)
+{
+	writel(0, &v7m_cache_maint->ic_iallu);
+	dsb();
+	isb();
+}
+
+void icache_enable(void)
+{
+	if (icache_status())
+		return;
+	invalidate_icache_all();
+	setbits_le32(&V7M_SCB->ccr, BIT(17));
+	dsb();
+	isb();
+}
+
+int icache_status(void)
+{
+	return (readl(&V7M_SCB->ccr) & BIT(17)) != 0;
+}
+
+void icache_disable(void)
+{
+	isb();
+	clrbits_le32(&V7M_SCB->ccr, BIT(17));
+	isb();
+}
+#else
+void icache_enable(void)
+{
+	return;
+}
+
+void icache_disable(void)
+{
+	return;
+}
+
+int icache_status(void)
+{
+	return 0;
+}
+#endif
+
+void enable_caches(void)
+{
+#ifndef CONFIG_SYS_ICACHE_OFF
+	icache_enable();
+#endif
+#ifndef CONFIG_SYS_DCACHE_OFF
+	dcache_enable();
+#endif
+}
arch/arm/include/asm/armv7m.h +0 -0
index 54d8a2b..9b3d73d 100644

arch/arm/include/asm/armv7m.h line 20

Quoted 7 lines
arch/arm/include/asm/armv7m.h +2 -2
@@ -16,8 +16,15 @@
 .thumb
 #endif
 
-#define V7M_SCB_BASE		0xE000ED00
-#define V7M_MPU_BASE		0xE000ED90
+/* armv7m fixed base addresses */
+#define V7M_SCS_BASE		(0xE000E000)

Extra parenthesis are useless.

btw is this controller always at this address ?

Quoted 45 lines
arch/arm/include/asm/armv7m.h +19 -0
+#define V7M_NVIC_BASE		(V7M_SCS_BASE + 0x0100)
+#define V7M_SCB_BASE		(V7M_SCS_BASE + 0x0D00)
+#define V7M_PROC_FTR_BASE	(V7M_SCS_BASE + 0x0D78)
+#define V7M_MPU_BASE		(V7M_SCS_BASE + 0x0D90)
+#define V7M_FPU_BASE		(V7M_SCS_BASE + 0x0F30)
+#define V7M_CACHE_MAINT_BASE	(V7M_SCS_BASE + 0x0F50)
+#define V7M_ACCESS_CNTL_BASE	(V7M_SCS_BASE + 0x0F90)
 
 #define V7M_SCB_VTOR		0x08
 
@@ -27,6 +34,18 @@ struct v7m_scb {
 	uint32_t icsr;		/* Interrupt Control and State Register */
 	uint32_t vtor;		/* Vector Table Offset Register */
 	uint32_t aircr;		/* App Interrupt and Reset Control Register */
+	uint32_t scr;		/* offset 0x10 */
+	uint32_t ccr;		/* offset 0x14 */
+	uint32_t shpr1;		/* offset 0x18 */
+	uint32_t shpr2;		/* offset 0x1c */
+	uint32_t shpr3;		/* offset 0x20 */
+	uint32_t shcrs;		/* offset 0x24 */
+	uint32_t cfsr;		/* offset 0x28 */
+	uint32_t hfsr;		/* offset 0x2C */
+	uint32_t res;		/* offset 0x30 */
+	uint32_t mmar;		/* offset 0x34 */
+	uint32_t bfar;		/* offset 0x38 */
+	uint32_t afsr;		/* offset 0x3C */
 };
 #define V7M_SCB				((struct v7m_scb *)V7M_SCB_BASE)
 
arch/arm/lib/Makefile +2 -0
index 166fa9e..52b36b3 100644
@@ -55,8 +55,10 @@ endif
 
 obj-y	+= cache.o
 ifndef CONFIG_ARM64
+ifndef CONFIG_CPU_V7M
 obj-y	+= cache-cp15.o
 endif
+endif
 
 obj-y	+= psci-dt.o
 
Signature · 2 lines
Best regards,
Marek Vasut
#3 lore raw

PATCH 1/2 armv7m: add instruction & data cache support

Hi Marek,

Quoted 56 lines

-----Original Message-----
From: Marek Vasut [mailto:marex at denx.de]
Sent: Thursday, March 09, 2017 5:33 PM
To: Vikas MANOCHA <[email protected]>; u-boot at lists.denx.de
Cc: Christophe KERELLO <[email protected]>; Albert Aribaud <[email protected]>; Alexander Graf <[email protected]>; Andre Przywara <[email protected]>; Heiko Schocher <[email protected]>; Ladislav Michl <ladis@linux- mips.org>; Simon Glass <[email protected]>; Stephen Warren <[email protected]>; Toshifumi NISHINAGA <[email protected]>
Subject: Re: [PATCH 1/2] armv7m: add instruction & data cache support

Quoted 45 lines · Vikas Manocha

This patch adds armv7m instruction & data cache support.

---
 arch/arm/cpu/armv7m/Makefile  |   2 +-
 arch/arm/cpu/armv7m/cache.c   | 295 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/armv7m.h |  23 +++-
 arch/arm/lib/Makefile         |   2 +
 4 files changed, 319 insertions(+), 3 deletions(-)  create mode

100644 arch/arm/cpu/armv7m/cache.c

(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile index aff60e8..41efe11 100644

arch/arm/cpu/armv7m/Makefile +1 -1
@@ -6,4 +6,4 @@
 #
 
 extra-y := start.o
-obj-y += cpu.o
+obj-y += cpu.o cache.o
arch/arm/cpu/armv7m/cache.c new file +19 -0
new file mode 100644 index 0000000..0fded33
@@ -0,0 +1,295 @@
+/*
+ * (C) Copyright 2017
+ * Vikas Manocha, ST Micoelectronics, vikas.manocha at st.com.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/armv7m.h>
+#include <asm/io.h>
+#include <errno.h>
+
+struct v7m_cache {
+	uint32_t ic_iallu;	/* offset 0x0*/
+	uint32_t res;		/* offset 0x4*/
+	uint32_t ic_imvalu;	/* offset 0x8*/
+	uint32_t dc_imvac;	/* offset 0xc*/
+	uint32_t dc_isw;	/* offset 0x10*/
+	uint32_t dc_cmvau;	/* offset 0x14*/
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c line 25

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+	uint32_t dc_cmvac;	/* offset 0x18*/
+	uint32_t dc_csw;	/* offset 0x1c*/
+	uint32_t dc_cimvac;	/* offset 0x20*/
+	uint32_t dc_cisw;	/* offset 0x24*/
+	uint32_t bp_iall;	/* offset 0x28*/
+};

We should get rid of this struct register definitions, just use #define MACRO ...

struct or macro: both approaches have their merits for block of registers, one better than other is arguable. I used structures here as it's very unlikely that there would be design changes in these registers to break the functionality. Had it been general peripherals like i2c, spi, it could be otherwise.

Quoted 36 lines
Quoted 36 lines
arch/arm/cpu/armv7m/cache.c new file +35 -0
+struct v7m_proc_features {
+	uint32_t clidr;		/* offset 0x00 */
+	uint32_t ctr;		/* offset 0x04 */
+	uint32_t ccsidr;	/* offset 0x08 */
+	uint32_t csselr;	/* offset 0x0c */
+};
+
+struct v7m_cache *const v7m_cache_maint = (void
+*)V7M_CACHE_MAINT_BASE; struct v7m_proc_features *const v7m_processor
+= (void *)V7M_PROC_FTR_BASE;
+
+#define WAYS_SHIFT		30
+#define SETS_SHIFT		5
+
+enum cache_type {
+	DCACHE = 0,
+	ICACHE,
+};
+
+/* PoU : Point of Unification, Poc: Point of Coherency */ enum
+cache_action {
+	INVALIDATE_POU,		/* for i-cache invalidate by address */
+	INVALIDATE_POC,		/* for d-cache invalidate by address */
+	INVALIDATE_SET_WAY,	/* for d-cache invalidate by sets/ways */
+	FLUSH_POU,
+	FLUSH_POC,
+	FLUSH_SET_WAY,
+	FLUSH_INVAL_POC,
+	FLUSH_INVAL_SET_WAY,
+};
+
+#ifndef CONFIG_SYS_DCACHE_OFF
+struct dcache_config {
+	uint32_t ways;
+	uint32_t sets;
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+};
+
+static void get_cache_ways_sets(struct dcache_config *cache) {
+	cache->ways = (readl(&v7m_processor->ccsidr) & GENMASK(12, 3)) >> 3;
+	cache->sets = (readl(&v7m_processor->ccsidr) & GENMASK(27, 13)) >>
+13;

What are these ad-hoc numbers ? Please use macros ...

Ok.

Quoted 24 lines
Quoted 24 lines
arch/arm/cpu/armv7m/cache.c new file +23 -0
+}
+
+static uint32_t *get_action_reg_set_ways(enum cache_action action) {
+	uint32_t *action_reg;
+
+	switch (action) {
+	case INVALIDATE_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_isw;
+		break;
+	case FLUSH_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_csw;
+		break;
+	case FLUSH_INVAL_SET_WAY:
+		action_reg = &v7m_cache_maint->dc_cisw;
+		break;
+	default:
+		action_reg = NULL;
+		break;
+	};
+
+	return action_reg;
+}
+
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c line 97

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+static uint32_t *get_action_reg_range(enum cache_action action) {
+	uint32_t *action_reg;
+
+	switch (action) {
+	case INVALIDATE_POU:
+		action_reg = &v7m_cache_maint->ic_imvalu;

Just use return FOO here , if this function is needed at all ...

Ok,

Quoted 26 lines
Quoted 26 lines
arch/arm/cpu/armv7m/cache.c new file +25 -0
+		break;
+	case INVALIDATE_POC:
+		action_reg = &v7m_cache_maint->dc_imvac;
+		break;
+	case FLUSH_POU:
+		action_reg = &v7m_cache_maint->dc_cmvau;
+		break;
+	case FLUSH_POC:
+		action_reg = &v7m_cache_maint->dc_cmvac;
+		break;
+	case FLUSH_INVAL_POC:
+		action_reg = &v7m_cache_maint->dc_cimvac;
+		break;
+	default:
+		action_reg = NULL;
+		break;
+	}
+
+	return action_reg;
+}
+
+static uint32_t get_cline_size(enum cache_type type) {
+	uint32_t size;
+
+	if (type == DCACHE)
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c line 129

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+		clrbits_le32(v7m_processor->csselr, BIT(0));
+	else if (type == ICACHE)
+		clrbits_le32(v7m_processor->csselr, BIT(1));
+	dsb();
+
+	size = (readl(&v7m_processor->ccsidr) & GENMASK(2, 0));

Extra parenthesis around the statement not needed.

Oops :-), thanks for pointing it, I will remove it in v2.

Quoted 59 lines
Quoted 59 lines
arch/arm/cpu/armv7m/cache.c new file +58 -0
+	size = size * 4 + 4; /* 0 means 4, 1 means 8 & so on */
+	debug("cache line size is %d\n", size);
+
+	return size;
+}
+
+int action_cache_range(enum cache_action action, uint32_t start_addr,
+		       int64_t size)
+{
+	uint32_t cline_size;
+	uint32_t *action_reg;
+	enum cache_type type;
+
+	action_reg = get_action_reg_range(action);
+	if (!action_reg)
+		return -EINVAL;
+	if (action == INVALIDATE_POU)
+		type = ICACHE;
+	else
+		type = DCACHE;
+
+	/* cache line size is minium size for the cache action */
+	cline_size = get_cline_size(type);
+	do {
+		writel(start_addr, action_reg);
+		size -= cline_size;
+		start_addr += cline_size;
+	} while (size > cline_size);
+	debug("cache action on range done\n");
+	dsb();
+	isb();
+
+	return 0;
+}
+
+static int action_dcache_all(enum cache_action action) {
+	struct dcache_config cache;
+	uint32_t *action_reg;
+	int i, j;
+
+	action_reg = get_action_reg_set_ways(action);
+	if (!action_reg)
+		return -EINVAL;
+
+	clrbits_le32(v7m_processor->csselr, BIT(0));	/* select data cache */
+	dsb();
+	get_cache_ways_sets(&cache);	/* Get number of ways & sets */
+	debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
+	for (i = cache.sets; i >= 0; i--) {
+		for (j = cache.ways; j >= 0; j--) {
+			writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
+			       action_reg);
+		}
+	}
+	dsb();
+	isb();
+
+	return 0;
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c line 194

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+}
+
+void dcache_enable(void)
+{
+	if (dcache_status())	/* return if cache already enabled */
+		return;

Add newline here

Ok

Quoted 7 lines

arch/arm/cpu/armv7m/cache.c line 198

Quoted 5 lines
arch/arm/cpu/armv7m/cache.c new file +4 -0
+	if (action_dcache_all(INVALIDATE_SET_WAY)) {
+		printf("ERR: D-cache not enabled\n");
+		return;
+	}

And here ... just add newline between if statements etc to make the code a bit more readable.

Ok

Quoted 4 lines

arch/arm/cpu/armv7m/cache.c line 199

Quoted 2 lines
arch/arm/cpu/armv7m/cache.c new file +1 -0
+	setbits_le32(&V7M_SCB->ccr, BIT(16));

What is this ad-hoc BIT ? Define and use macros for these bits ...

Ok

Quoted 103 lines
Quoted 103 lines
arch/arm/cpu/armv7m/cache.c new file +94 -0
+	dsb();
+	isb();
+}
+
+void dcache_disable(void)
+{
+	/* if dcache is enabled-> dcache disable & then flush */
+	if (dcache_status()) {
+		if (action_dcache_all(FLUSH_SET_WAY)) {
+			printf("ERR: D-cahe not flushed\n");
+			return;
+		}
+		clrbits_le32(&V7M_SCB->ccr, BIT(16));
+		dsb();
+		isb();
+	}
+}
+
+int dcache_status(void)
+{
+	return (readl(&V7M_SCB->ccr) & BIT(16)) != 0; }
+
+#else
+void dcache_enable(void)
+{
+	return;
+}
+
+void dcache_disable(void)
+{
+	return;
+}
+
+int dcache_status(void)
+{
+	return 0;
+}
+#endif
+
+#ifndef CONFIG_SYS_ICACHE_OFF
+
+void invalidate_icache_all(void)
+{
+	writel(0, &v7m_cache_maint->ic_iallu);
+	dsb();
+	isb();
+}
+
+void icache_enable(void)
+{
+	if (icache_status())
+		return;
+	invalidate_icache_all();
+	setbits_le32(&V7M_SCB->ccr, BIT(17));
+	dsb();
+	isb();
+}
+
+int icache_status(void)
+{
+	return (readl(&V7M_SCB->ccr) & BIT(17)) != 0; }
+
+void icache_disable(void)
+{
+	isb();
+	clrbits_le32(&V7M_SCB->ccr, BIT(17));
+	isb();
+}
+#else
+void icache_enable(void)
+{
+	return;
+}
+
+void icache_disable(void)
+{
+	return;
+}
+
+int icache_status(void)
+{
+	return 0;
+}
+#endif
+
+void enable_caches(void)
+{
+#ifndef CONFIG_SYS_ICACHE_OFF
+	icache_enable();
+#endif
+#ifndef CONFIG_SYS_DCACHE_OFF
+	dcache_enable();
+#endif
+}
(quoted diff) +0 -0

b/arch/arm/include/asm/armv7m.h index 54d8a2b..9b3d73d 100644

arch/arm/include/asm/armv7m.h +0 -0
@@ -16,8 +16,15 @@
 .thumb
 #endif
 
Quoted 6 lines

arch/arm/include/asm/armv7m.h line 20

Quoted 4 lines
arch/arm/include/asm/armv7m.h +2 -2
-#define V7M_SCB_BASE		0xE000ED00
-#define V7M_MPU_BASE		0xE000ED90
+/* armv7m fixed base addresses */
+#define V7M_SCS_BASE		(0xE000E000)

Extra parenthesis are useless.

Right.

Quoted 2 lines

btw is this controller always at this address ?

Yes.

Cheers,
Vikas

Quoted 51 lines
Quoted 36 lines
arch/arm/include/asm/armv7m.h +19 -0
+#define V7M_NVIC_BASE		(V7M_SCS_BASE + 0x0100)
+#define V7M_SCB_BASE		(V7M_SCS_BASE + 0x0D00)
+#define V7M_PROC_FTR_BASE	(V7M_SCS_BASE + 0x0D78)
+#define V7M_MPU_BASE		(V7M_SCS_BASE + 0x0D90)
+#define V7M_FPU_BASE		(V7M_SCS_BASE + 0x0F30)
+#define V7M_CACHE_MAINT_BASE	(V7M_SCS_BASE + 0x0F50)
+#define V7M_ACCESS_CNTL_BASE	(V7M_SCS_BASE + 0x0F90)
 
 #define V7M_SCB_VTOR		0x08
 
@@ -27,6 +34,18 @@ struct v7m_scb {
 	uint32_t icsr;		/* Interrupt Control and State Register */
 	uint32_t vtor;		/* Vector Table Offset Register */
 	uint32_t aircr;		/* App Interrupt and Reset Control Register */
+	uint32_t scr;		/* offset 0x10 */
+	uint32_t ccr;		/* offset 0x14 */
+	uint32_t shpr1;		/* offset 0x18 */
+	uint32_t shpr2;		/* offset 0x1c */
+	uint32_t shpr3;		/* offset 0x20 */
+	uint32_t shcrs;		/* offset 0x24 */
+	uint32_t cfsr;		/* offset 0x28 */
+	uint32_t hfsr;		/* offset 0x2C */
+	uint32_t res;		/* offset 0x30 */
+	uint32_t mmar;		/* offset 0x34 */
+	uint32_t bfar;		/* offset 0x38 */
+	uint32_t afsr;		/* offset 0x3C */
 };
 #define V7M_SCB				((struct v7m_scb *)V7M_SCB_BASE)
 
arch/arm/lib/Makefile index renamed from arch/arm/lib/Makefile +0 -0

166fa9e..52b36b3 100644

arch/arm/lib/Makefile endif · line 64

Quoted 10 lines
arch/arm/lib/Makefile +2 -0
@@ -55,8 +55,10 @@ endif
 
 obj-y	+= cache.o
 ifndef CONFIG_ARM64
+ifndef CONFIG_CPU_V7M
 obj-y	+= cache-cp15.o
 endif
+endif
 
 obj-y	+= psci-dt.o
 

--
Best regards,
Marek Vasut

#4 lore raw

PATCH 1/2 armv7m: add instruction & data cache support

Quoted 67 lines · Vikas MANOCHA

Hi Marek,

Quoted 56 lines

-----Original Message-----
From: Marek Vasut [mailto:marex at denx.de]
Sent: Thursday, March 09, 2017 5:33 PM
To: Vikas MANOCHA <[email protected]>; u-boot at lists.denx.de
Cc: Christophe KERELLO <[email protected]>; Albert Aribaud <[email protected]>; Alexander Graf <[email protected]>; Andre Przywara <[email protected]>; Heiko Schocher <[email protected]>; Ladislav Michl <ladis@linux- mips.org>; Simon Glass <[email protected]>; Stephen Warren <[email protected]>; Toshifumi NISHINAGA <[email protected]>
Subject: Re: [PATCH 1/2] armv7m: add instruction & data cache support

Quoted 45 lines · Vikas Manocha

This patch adds armv7m instruction & data cache support.

---
 arch/arm/cpu/armv7m/Makefile  |   2 +-
 arch/arm/cpu/armv7m/cache.c   | 295 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/armv7m.h |  23 +++-
 arch/arm/lib/Makefile         |   2 +
 4 files changed, 319 insertions(+), 3 deletions(-)  create mode

100644 arch/arm/cpu/armv7m/cache.c

(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile index aff60e8..41efe11 100644

arch/arm/cpu/armv7m/Makefile +1 -1
@@ -6,4 +6,4 @@
 #
 
 extra-y := start.o
-obj-y += cpu.o
+obj-y += cpu.o cache.o
arch/arm/cpu/armv7m/cache.c new file +19 -0
new file mode 100644 index 0000000..0fded33
@@ -0,0 +1,295 @@
+/*
+ * (C) Copyright 2017
+ * Vikas Manocha, ST Micoelectronics, vikas.manocha at st.com.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/armv7m.h>
+#include <asm/io.h>
+#include <errno.h>
+
+struct v7m_cache {
+	uint32_t ic_iallu;	/* offset 0x0*/
+	uint32_t res;		/* offset 0x4*/
+	uint32_t ic_imvalu;	/* offset 0x8*/
+	uint32_t dc_imvac;	/* offset 0xc*/
+	uint32_t dc_isw;	/* offset 0x10*/
+	uint32_t dc_cmvau;	/* offset 0x14*/
Quoted 8 lines

arch/arm/cpu/armv7m/cache.c line 25

Quoted 6 lines
arch/arm/cpu/armv7m/cache.c new file +6 -0
+	uint32_t dc_cmvac;	/* offset 0x18*/
+	uint32_t dc_csw;	/* offset 0x1c*/
+	uint32_t dc_cimvac;	/* offset 0x20*/
+	uint32_t dc_cisw;	/* offset 0x24*/
+	uint32_t bp_iall;	/* offset 0x28*/
+};

We should get rid of this struct register definitions, just use #define MACRO ...

Quoted 2 lines

struct or macro: both approaches have their merits for block of registers, one better than other is arguable. I used structures here as it's very unlikely that there would be design changes in these registers to break the functionality.

It's always very unlikely ... until it happens.

Quoted 1 line

Had it been general peripherals like i2c, spi, it could be otherwise.

So let's keep it consistent and use macros ...

[...]

Signature · 2 lines
Best regards,
Marek Vasut
#5 lore raw

PATCH 2/2 stm32f7: enable instruction & data cache

In reply to Vikas Manocha

It also enables commands for cache enable/disable/status.

---
 arch/arm/mach-stm32/stm32f7/soc.c | 2 ++
 include/configs/stm32f746-disco.h | 4 +---
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/arm/mach-stm32/stm32f7/soc.c +2 -0
index 8baee99..ca54603 100644
@@ -60,6 +60,8 @@ int arch_cpu_init(void)
 		 (V7M_MPU_RASR_XN_ENABLE
 			 | V7M_MPU_RASR_AP_RW_RW
 			 | 0x01 << V7M_MPU_RASR_TEX_SHIFT
+			 | 0x01 << V7M_MPU_RASR_B_SHIFT
+			 | 0x01 << V7M_MPU_RASR_C_SHIFT
 			 | V7M_MPU_RASR_SIZE_8MB
 			 | V7M_MPU_RASR_EN)
 			 , &V7M_MPU->rasr
include/configs/stm32f746-disco.h +1 -3
index ae3211a..9e9406a 100644
@@ -14,9 +14,6 @@
 #define CONFIG_SYS_INIT_SP_ADDR		0x20050000
 #define CONFIG_SYS_TEXT_BASE		0x08000000
 
-#define CONFIG_SYS_ICACHE_OFF
-#define CONFIG_SYS_DCACHE_OFF
-
 /*
  * Configuration of the external SDRAM memory
  */
@@ -82,4 +79,5 @@
 #define CONFIG_CMDLINE_EDITING
 
 #define CONFIG_CMD_MEM
+#define CONFIG_CMD_CACHE
 #endif /* __CONFIG_H */
Signature · 1 line
1.9.1