]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
imx8m: Refactor the OPTEE memory removal
authorPeng Fan <peng.fan@nxp.com>
Thu, 9 Jul 2020 07:26:06 +0000 (15:26 +0800)
committerPeng Fan <peng.fan@nxp.com>
Tue, 14 Jul 2020 07:23:48 +0000 (15:23 +0800)
Current codes assume the OPTEE address is at the end of first DRAM bank.
Adjust the process to allow OPTEE in the middle of first bank.

When OPTEE memory is removed from first bank, it may split the first bank
to two banks, adjust the MMU table for the split case,
Since the default CONFIG_NR_DRAM_BANKS is 4, it is enough, just enlarge
i.MX8MP evk to default to avoid issue.

Signed-off-by: Ye Li <ye.li@nxp.com>
Signed-off-by: Silvano di Ninno <silvano.dininno@nxp.com>
Tested-by: Silvano di Ninno <silvano.dininno@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
arch/arm/mach-imx/imx8m/soc.c
board/beacon/imx8mm/imx8mm_beacon.c
board/freescale/imx8mm_evk/imx8mm_evk.c
board/freescale/imx8mn_evk/imx8mn_evk.c
board/freescale/imx8mp_evk/imx8mp_evk.c
board/freescale/imx8mq_evk/imx8mq_evk.c
board/google/imx8mq_phanbell/imx8mq_phanbell.c
board/technexion/pico-imx8mq/pico-imx8mq.c
board/toradex/verdin-imx8mm/verdin-imx8mm.c
configs/imx8mp_evk_defconfig

index bb2f112af697bf29a6d028c9769c0c7c00c2d425..b3c08271e605294d0ac765238786484bf839d610 100644 (file)
@@ -142,6 +142,9 @@ static struct mm_region imx8m_mem_map[] = {
                .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
                         PTE_BLOCK_OUTER_SHARE
 #endif
+       }, {
+               /* empty entrie to split table entry 5 if needed when TEEs are used */
+               0,
        }, {
                /* List terminator */
                0,
@@ -152,18 +155,123 @@ struct mm_region *mem_map = imx8m_mem_map;
 
 void enable_caches(void)
 {
-       /*
-        * If OPTEE runs, remove OPTEE memory from MMU table to
-        * avoid speculative prefetch. OPTEE runs at the top of
-        * the first memory bank
-        */
-       if (rom_pointer[1])
-               imx8m_mem_map[5].size -= rom_pointer[1];
+       /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
+       if (rom_pointer[1]) {
+               /*
+                * TEE are loaded, So the ddr bank structures
+                * have been modified update mmu table accordingly
+                */
+               int i = 0;
+               /*
+                * please make sure that entry initial value matches
+                * imx8m_mem_map for DRAM1
+                */
+               int entry = 5;
+               u64 attrs = imx8m_mem_map[entry].attrs;
+
+               while (i < CONFIG_NR_DRAM_BANKS && entry < 8) {
+                       if (gd->bd->bi_dram[i].start == 0)
+                               break;
+                       imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
+                       imx8m_mem_map[entry].virt = gd->bd->bi_dram[i].start;
+                       imx8m_mem_map[entry].size = gd->bd->bi_dram[i].size;
+                       imx8m_mem_map[entry].attrs = attrs;
+                       debug("Added memory mapping (%d): %llx %llx\n", entry,
+                             imx8m_mem_map[entry].phys, imx8m_mem_map[entry].size);
+                       i++; entry++;
+               }
+       }
 
        icache_enable();
        dcache_enable();
 }
 
+__weak int board_phys_sdram_size(phys_size_t *size)
+{
+       if (!size)
+               return -EINVAL;
+
+       *size = PHYS_SDRAM_SIZE;
+       return 0;
+}
+
+int dram_init(void)
+{
+       phys_size_t sdram_size;
+       int ret;
+
+       ret = board_phys_sdram_size(&sdram_size);
+       if (ret)
+               return ret;
+
+       /* rom_pointer[1] contains the size of TEE occupies */
+       if (rom_pointer[1])
+               gd->ram_size = sdram_size - rom_pointer[1];
+       else
+               gd->ram_size = sdram_size;
+
+#ifdef PHYS_SDRAM_2_SIZE
+       gd->ram_size += PHYS_SDRAM_2_SIZE;
+#endif
+
+       return 0;
+}
+
+int dram_init_banksize(void)
+{
+       int bank = 0;
+       int ret;
+       phys_size_t sdram_size;
+
+       ret = board_phys_sdram_size(&sdram_size);
+       if (ret)
+               return ret;
+
+       gd->bd->bi_dram[bank].start = PHYS_SDRAM;
+       if (rom_pointer[1]) {
+               phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
+               phys_size_t optee_size = (size_t)rom_pointer[1];
+
+               gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
+               if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
+                       if (++bank >= CONFIG_NR_DRAM_BANKS) {
+                               puts("CONFIG_NR_DRAM_BANKS is not enough\n");
+                               return -1;
+                       }
+
+                       gd->bd->bi_dram[bank].start = optee_start + optee_size;
+                       gd->bd->bi_dram[bank].size = PHYS_SDRAM +
+                               sdram_size - gd->bd->bi_dram[bank].start;
+               }
+       } else {
+               gd->bd->bi_dram[bank].size = sdram_size;
+       }
+
+#ifdef PHYS_SDRAM_2_SIZE
+       if (++bank >= CONFIG_NR_DRAM_BANKS) {
+               puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
+               return -1;
+       }
+       gd->bd->bi_dram[bank].start = PHYS_SDRAM_2;
+       gd->bd->bi_dram[bank].size = PHYS_SDRAM_2_SIZE;
+#endif
+
+       return 0;
+}
+
+phys_size_t get_effective_memsize(void)
+{
+       /* return the first bank as effective memory */
+       if (rom_pointer[1])
+               return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
+
+#ifdef PHYS_SDRAM_2_SIZE
+       return gd->ram_size - PHYS_SDRAM_2_SIZE;
+#else
+       return gd->ram_size;
+#endif
+}
+
 static u32 get_cpu_variant_type(u32 type)
 {
        struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
index e82e8b78d80e542892dd1ca59ce2b08deef39c8b..c61d25fbead702f9fc64a1b9bb9b1cc18391a112 100644 (file)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 #if IS_ENABLED(CONFIG_FEC_MXC)
 static int setup_fec(void)
 {
index c43af9bc484e3a0b9101a7344db17c89402ff095..6af71006966c3a817cc0550a001a51a15b576ed4 100644 (file)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 #if IS_ENABLED(CONFIG_FEC_MXC)
 static int setup_fec(void)
 {
index ea02bb75f411c0f5ea0b71ed2d8a90bbe654600e..e5ca54f9ae75127e62d7daabd74a072dfff00293 100644 (file)
@@ -9,13 +9,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int dram_init(void)
-{
-       gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 int board_init(void)
 {
        return 0;
index 97ba15645a2b500f6818f7e9cdfe1de9c3410c77..034a349236e828cc8937b4962926929568ce7386 100644 (file)
@@ -40,46 +40,6 @@ int board_early_init_f(void)
        return 0;
 }
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-#if CONFIG_NR_DRAM_BANKS > 1
-       gd->ram_size += PHYS_SDRAM_2_SIZE;
-#endif
-
-       return 0;
-}
-
-int dram_init_banksize(void)
-{
-       gd->bd->bi_dram[0].start = PHYS_SDRAM;
-       if (rom_pointer[1])
-
-               gd->bd->bi_dram[0].size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->bd->bi_dram[0].size = PHYS_SDRAM_SIZE;
-
-#if CONFIG_NR_DRAM_BANKS > 1
-       gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
-       gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
-#endif
-
-       return 0;
-}
-
-phys_size_t get_effective_memsize(void)
-{
-       if (rom_pointer[1])
-               return (PHYS_SDRAM_SIZE - rom_pointer[1]);
-       else
-               return PHYS_SDRAM_SIZE;
-}
-
 int board_init(void)
 {
        return 0;
index ae3be5785c9fcc6db969a78044c931c3f501706e..1ad670b8ccf0081533ec9912056d3c2f97981d4d 100644 (file)
@@ -53,17 +53,6 @@ int board_early_init_f(void)
        return 0;
 }
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 #ifdef CONFIG_FEC_MXC
 static int setup_fec(void)
 {
index c0cc3e9b71d1e1f33cb9d0bbf21b80d0b0e6b72e..746071b4150f8c15f9c77d4d8373b3d2e0656792 100644 (file)
@@ -48,17 +48,6 @@ int board_early_init_f(void)
        return 0;
 }
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 #ifdef CONFIG_FEC_MXC
 static int setup_fec(void)
 {
index 2a3e6e7e2657babf7fec145d2896c82fbd4ef41b..330de7137c3dff7524ad0302f209b1a6f1f61ae6 100644 (file)
@@ -51,24 +51,22 @@ int board_early_init_f(void)
        return 0;
 }
 
-int dram_init(void)
+int board_phys_sdram_size(phys_size_t *size)
 {
        int ddr_size = readl(M4_BOOTROM_BASE_ADDR);
 
-       if (ddr_size == 0x4)
-               gd->ram_size = 0x100000000;
-       else if (ddr_size == 0x3)
-               gd->ram_size = 0xc0000000;
-       else if (ddr_size == 0x2)
-               gd->ram_size = 0x80000000;
-       else if (ddr_size == 0x1)
-               gd->ram_size = 0x40000000;
-       else
+       if (ddr_size == 0x4) {
+               *size = 0x100000000;
+       } else if (ddr_size == 0x3) {
+               *size = 0xc0000000;
+       } else if (ddr_size == 0x2) {
+               *size = 0x80000000;
+       } else if (ddr_size == 0x1) {
+               *size = 0x40000000;
+       } else {
                printf("Unknown DDR type!!!\n");
-
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size -= rom_pointer[1];
+               return -1;
+       }
 
        return 0;
 }
index ff05c7d55237f6a5cc5a2a767c5e2848095eb4cc..fa51b776abcb3b19ba7d7d5e771239096f617d83 100644 (file)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-int dram_init(void)
-{
-       /* rom_pointer[1] contains the size of TEE occupies */
-       if (rom_pointer[1])
-               gd->ram_size = PHYS_SDRAM_SIZE - rom_pointer[1];
-       else
-               gd->ram_size = PHYS_SDRAM_SIZE;
-
-       return 0;
-}
-
 #if IS_ENABLED(CONFIG_FEC_MXC)
 static int setup_fec(void)
 {
index dcfc336634d5a73b053f814967f783a98579de30..f49116c9c5562eae0b5d163bfb3b3441bfe037fb 100644 (file)
@@ -16,7 +16,6 @@ CONFIG_TARGET_IMX8MP_EVK=y
 CONFIG_SPL_MMC_SUPPORT=y
 CONFIG_SPL_SERIAL_SUPPORT=y
 CONFIG_SPL_DRIVERS_MISC_SUPPORT=y
-CONFIG_NR_DRAM_BANKS=2
 CONFIG_SPL=y
 CONFIG_SPL_IMX_ROMAPI_LOADADDR=0x48000000
 CONFIG_FIT=y