Skip to contentSkip to conversation

mpc86xx: set the DDR BATs after calculating true DDR size

PATCH New Kumar Gala 2 participants 5 messages Mar 30, 2010

lore
Share & tools
lore

Kumar Gala

author
#0 lore raw

PATCH 1/1 mpc86xx: set the DDR BATs after calculating true DDR size

From: Timur Tabi <[email protected]>

After determining how much DDR is actually in the system, set DBAT0 and IBAT0 accordingly. This ensures that the CPU won't attempt to access (via speculation) addresses outside of actual memory.

On 86xx systems, DBAT0 and IBAT0 (the BATs for DDR) are initialized to 2GB and kept that way. If the system has less than 2GB of memory (typical for an MPC8610 HPCD), the CPU may attempt to access this memory during speculation. The zlib code is notorious for generating such memory reads, and indeed on the MPC8610, uncompressing the Linux kernel causes a machine check (without this patch).

---
* This is my attempt at reworking the patch Timur posted based on comments from Becky. I've tried to mimic the behavior or setup_ddr_tlb that we have on 85xx

 board/freescale/mpc8610hpcd/mpc8610hpcd.c |    2 +
 board/freescale/mpc8641hpcn/mpc8641hpcn.c |    2 +
 cpu/mpc86xx/cpu.c                         |   32 +++++++++++++++++++++++++++++
 cpu/mpc86xx/cpu_init.c                    |    4 +++
 include/asm-ppc/mmu.h                     |    4 ++-
 include/configs/MPC8610HPCD.h             |    6 +---
 include/configs/MPC8641HPCN.h             |    4 +--
 include/mpc86xx.h                         |    2 +
 8 files changed, 48 insertions(+), 8 deletions(-)
board/freescale/mpc8610hpcd/mpc8610hpcd.c +2 -0
index 784a2ed..ab5f800 100644
@@ -127,6 +127,8 @@ initdram(int board_type)
 	dram_size = fixed_sdram();
 #endif
 
+	setup_ddr_bat(dram_size);
+
 	puts(" DDR: ");
 	return dram_size;
 }
board/freescale/mpc8641hpcn/mpc8641hpcn.c +2 -0
index c521527..443c9fd 100644
@@ -74,6 +74,8 @@ initdram(int board_type)
 	dram_size = fixed_sdram();
 #endif
 
+	setup_ddr_bat(dram_size);
+
 	puts("    DDR: ");
 	return dram_size;
 }
cpu/mpc86xx/cpu.c +32 -0
index f7e012d..c03a2b6 100644
@@ -197,3 +197,35 @@ void mpc86xx_reginfo(void)
 	printf("\tBR7\t0x%08X\tOR7\t0x%08X \n", in_be32(&lbc->br7), in_be32(&lbc->or7));
 
 }
+
+/*
+ * Set the DDR BATs to reflect the actual size of DDR.
+ *
+ * dram_size is the actual size of DDR, in bytes
+ *
+ * Note: we assume that CONFIG_MAX_MEM_MAPPED is 2G or smaller as we only
+ * are using a single BAT to cover DDR.  
+ *
+ * If this is not true, (e.g. CONFIG_MAX_MEM_MAPPED is 2GB but HID0_XBSEN
+ * is not defined) then we might have a situation where U-Boot will attempt
+ * to relocated itself outside of the region mapped by DBAT0.
+ * This will cause a machine check.
+ *
+ * We also assume that the XBL bits are ignored by the processor (even if set)
+ * if extended BAT addressing is disabled.
+ */
+void setup_ddr_bat(phys_addr_t dram_size)
+{
+	unsigned long batu, bl;
+
+	bl = TO_BATU_BL(min(dram_size, CONFIG_MAX_MEM_MAPPED));
+
+	if (BATU_SIZE(bl) != dram_size) {
+		u64 sz = (u64)dram_size - BATU_SIZE(bl);
+		print_size(sz, " left unmapped\n");
+	}
+
+	batu = BATU_BL_2G | BATU_VS | BATU_VP;
+	write_bat(DBAT0, batu, CONFIG_SYS_DBAT0L);
+	write_bat(IBAT0, batu, CONFIG_SYS_IBAT0L);
+}
cpu/mpc86xx/cpu_init.c +4 -0
index 5a78a9c..b4f047d 100644
@@ -138,8 +138,12 @@ int cpu_init_r(void)
 /* Set up BAT registers */
 void setup_bats(void)
 {
+#if defined(CONFIG_SYS_DBAT0U) && defined(CONFIG_SYS_DBAT0L)
 	write_bat(DBAT0, CONFIG_SYS_DBAT0U, CONFIG_SYS_DBAT0L);
+#endif
+#if defined(CONFIG_SYS_IBAT0U) && defined(CONFIG_SYS_IBAT0L)
 	write_bat(IBAT0, CONFIG_SYS_IBAT0U, CONFIG_SYS_IBAT0L);
+#endif
 	write_bat(DBAT1, CONFIG_SYS_DBAT1U, CONFIG_SYS_DBAT1L);
 	write_bat(IBAT1, CONFIG_SYS_IBAT1U, CONFIG_SYS_IBAT1L);
 	write_bat(DBAT2, CONFIG_SYS_DBAT2U, CONFIG_SYS_DBAT2L);
include/asm-ppc/mmu.h +3 -1
index fd10249..210b171 100644
@@ -213,7 +213,9 @@ extern void print_bats(void);
 #define BATL_PADDR(x) ((phys_addr_t)((x & 0xfffe0000)		\
 				     | ((x & 0x0e00ULL) << 24)	\
 				     | ((x & 0x04ULL) << 30)))
-#define BATU_SIZE(x) (1UL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+#define BATU_SIZE(x) (1ULL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+
+#define TO_BATU_BL(x) ((((x)/(128*1024)) - 1) * 4) /* bytes into BATU_BL */
 
 /* Used to set up SDR1 register */
 #define HASH_TABLE_SIZE_64K	0x00010000
include/configs/MPC8610HPCD.h +2 -4
index 1d2d659..fed441e 100644
@@ -341,10 +341,8 @@
  * BAT0		2G	Cacheable, non-guarded
  * 0x0000_0000	2G	DDR
  */
-#define CONFIG_SYS_DBAT0L	(BATL_PP_RW | BATL_MEMCOHERENCE)
-#define CONFIG_SYS_DBAT0U	(BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L	(BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U	CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_DBAT0L	(BATL_PP_RW)
+#define CONFIG_SYS_IBAT0L	(BATL_PP_RW)
 
 /*
  * BAT1		1G	Cache-inhibited, guarded
include/configs/MPC8641HPCN.h +1 -3
index 12a8f60..94e4d24 100644
@@ -482,9 +482,7 @@ extern unsigned long get_board_sys_clk(unsigned long dummy);
  * BAT0		DDR
  */
 #define CONFIG_SYS_DBAT0L	(BATL_PP_RW | BATL_MEMCOHERENCE)
-#define CONFIG_SYS_DBAT0U	(BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L	(BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U	CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_IBAT0L	(BATL_PP_RW | BATL_MEMCOHERENCE)
 
 /*
  * BAT1		LBC (PIXIS/CF)
include/mpc86xx.h +2 -0
index c6f30f9..eb85d60 100644
@@ -83,5 +83,7 @@ static __inline__ unsigned long get_l2cr (void)
    return l2cr_val;
 }
 
+void setup_ddr_bat(phys_addr_t dram_size);
+
 #endif  /* _ASMLANGUAGE */
 #endif	/* __MPC86xx_H__ */
Signature · 1 line
1.6.0.6
#1 lore raw

PATCH 1/1 mpc86xx: set the DDR BATs after calculating true DDR size

Quoted 2 lines · Kumar Gala

+ * We also assume that the XBL bits are ignored by the processor (even if set)
+ * if extended BAT addressing is disabled.

Since we don't read/modify/write that BATs, this comment is probably not true any more.

include/asm-ppc/mmu.h extern void print_bats(void); · line 218

Quoted 4 lines
include/asm-ppc/mmu.h +3 -1
-#define BATU_SIZE(x) (1UL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+#define BATU_SIZE(x) (1ULL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+
+#define TO_BATU_BL(x) ((((x)/(128*1024)) - 1) * 4) /* bytes into BATU_BL */

I don't think this works if (x) is not a power of two. E.g., TO_BATU_BL(1.5GB) = 0xbffc. It should truncate the number to 1GB and give us 7ffc. This is one of the issues that Becky raised. I don't know if there's some simple way to truncate a number to its lowest power of two.

Quoted 23 lines
include/configs/MPC8610HPCD.h +0 -0
@@ -341,10 +341,8 @@

?* BAT0 ? ? ? ? ? ? ? ?2G ? ? ?Cacheable, non-guarded ?* 0x0000_0000 2G ? ? ?DDR
?*/

include/configs/MPC8610HPCD.h +2 -4
-#define CONFIG_SYS_DBAT0L ? ? ?(BATL_PP_RW | BATL_MEMCOHERENCE)
-#define CONFIG_SYS_DBAT0U ? ? ?(BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L ? ? ?(BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U ? ? ?CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_DBAT0L ? ? ?(BATL_PP_RW)
+#define CONFIG_SYS_IBAT0L ? ? ?(BATL_PP_RW)

?/*
?* BAT1 ? ? ? ? ? ? ? ?1G ? ? ?Cache-inhibited, guarded

include/configs/MPC8641HPCN.h +0 -0
index 12a8f60..94e4d24 100644
@@ -482,9 +482,7 @@ extern unsigned long get_board_sys_clk(unsigned long dummy);

?* BAT0 ? ? ? ? ? ? ? ?DDR
?*/
?#define CONFIG_SYS_DBAT0L ? ? ?(BATL_PP_RW | BATL_MEMCOHERENCE)

include/configs/MPC8641HPCN.h

Quoted 4 lines
include/configs/MPC8641HPCN.h +1 -3
-#define CONFIG_SYS_DBAT0U ? ? ?(BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L ? ? ?(BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U ? ? ?CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_IBAT0L ? ? ?(BATL_PP_RW | BATL_MEMCOHERENCE)

Why do we need BATL_MEMCOHERENCE on 8641 but not on 8610?

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#2 lore raw

PATCH 1/1 mpc86xx: set the DDR BATs after calculating true DDR size

Quoted 8 lines · Timur Tabi
Quoted 2 lines · Kumar Gala

+ * We also assume that the XBL bits are ignored by the processor (even if set)
+ * if extended BAT addressing is disabled.

Since we don't read/modify/write that BATs, this comment is probably not true any more.

Quoted 10 lines

include/asm-ppc/mmu.h extern void print_bats(void); · line 218

Quoted 4 lines
include/asm-ppc/mmu.h +3 -1
-#define BATU_SIZE(x) (1UL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+#define BATU_SIZE(x) (1ULL << (fls((x & BATU_BL_MAX) >> 2) + 17))
+
+#define TO_BATU_BL(x) ((((x)/(128*1024)) - 1) * 4) /* bytes into BATU_BL */

I don't think this works if (x) is not a power of two. E.g., TO_BATU_BL(1.5GB) = 0xbffc. It should truncate the number to 1GB and give us 7ffc. This is one of the issues that Becky raised. I don't know if there's some simple way to truncate a number to its lowest power of two.

Good point, we can do something like to cover to power two first:

	(1 << __ilog2_u64(x))
Quoted 13 lines
Quoted 13 lines
include/configs/MPC8610HPCD.h +2 -4
@@ -341,10 +341,8 @@
 * BAT0                2G      Cacheable, non-guarded
 * 0x0000_0000 2G      DDR
 */
-#define CONFIG_SYS_DBAT0L      (BATL_PP_RW | BATL_MEMCOHERENCE)
-#define CONFIG_SYS_DBAT0U      (BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L      (BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U      CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_DBAT0L      (BATL_PP_RW)
+#define CONFIG_SYS_IBAT0L      (BATL_PP_RW)
 
Quoted 16 lines
Quoted 7 lines
include/configs/MPC8610HPCD.h +0 -0
 /*
 * BAT1                1G      Cache-inhibited, guarded
include/configs/MPC8641HPCN.h +0 -0
index 12a8f60..94e4d24 100644

include/configs/MPC8641HPCN.h extern unsigned long get_board_sys_clk(unsigned long dummy); · line 485

Quoted 7 lines
include/configs/MPC8641HPCN.h +1 -3
@@ -482,9 +482,7 @@ extern unsigned long get_board_sys_clk(unsigned long dummy);
 * BAT0                DDR
 */
 #define CONFIG_SYS_DBAT0L      (BATL_PP_RW | BATL_MEMCOHERENCE)
-#define CONFIG_SYS_DBAT0U      (BATU_BL_2G | BATU_VS | BATU_VP)
-#define CONFIG_SYS_IBAT0L      (BATL_PP_RW | BATL_MEMCOHERENCE )
-#define CONFIG_SYS_IBAT0U      CONFIG_SYS_DBAT0U
+#define CONFIG_SYS_IBAT0L      (BATL_PP_RW | BATL_MEMCOHERENCE)

Why do we need BATL_MEMCOHERENCE on 8641 but not on 8610?

dual core on 8641, single core on 8610.

- k

#3 lore raw

PATCH 1/1 mpc86xx: set the DDR BATs after calculating true DDR size

Quoted 3 lines · Kumar Gala
Quoted 1 line

Why do we need BATL_MEMCOHERENCE on 8641 but not on 8610?

dual core on 8641, single core on 8610.

The 8610 does lots of DMA for audio and video. Don't we need coherence for that? I read the e600 manual, but I couldn't glean whether we need the M bit set to make DMA work.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
author
#4 lore raw

PATCH 1/1 mpc86xx: set the DDR BATs after calculating true DDR size

Quoted 8 lines · Timur Tabi
Quoted 3 lines · Kumar Gala
Quoted 1 line

Why do we need BATL_MEMCOHERENCE on 8641 but not on 8610?

dual core on 8641, single core on 8610.

The 8610 does lots of DMA for audio and video. Don't we need coherence for that? I read the e600 manual, but I couldn't glean whether we need the M bit set to make DMA work.

No, "M" in the core is only for other cores, has nothing to do w/DMAs.

- k