summaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2019-05-10 18:15:50 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-05-10 18:15:50 +0200
commit65cfdbebb19f0b1c8d322a3eb9ec464d669a4d3f (patch)
treec279915875be7850298cfe1ccd3b96c149373590 /arch
parente7dad8c88a19e7130b4241bfe1f251620850ce4b (diff)
parent113c2bc244649430200bf51df7ffd122fd260c24 (diff)
downloadbarebox-65cfdbebb19f0b1c8d322a3eb9ec464d669a4d3f.tar.gz
barebox-65cfdbebb19f0b1c8d322a3eb9ec464d669a4d3f.tar.xz
Merge branch 'for-next/mips'
Diffstat (limited to 'arch')
-rw-r--r--arch/mips/Kconfig19
-rw-r--r--arch/mips/Makefile9
-rw-r--r--arch/mips/boards/8devices-lima/lowlevel.S3
-rw-r--r--arch/mips/boards/black-swift/lowlevel.S3
-rw-r--r--arch/mips/boards/dlink-dir-320/lowlevel.S3
-rw-r--r--arch/mips/boards/dptechnics-dpt-module/lowlevel.S3
-rw-r--r--arch/mips/boards/img-ci20/lowlevel.S3
-rw-r--r--arch/mips/boards/loongson-ls1b/lowlevel.S3
-rw-r--r--arch/mips/boards/qemu-malta/lowlevel.S3
-rw-r--r--arch/mips/boards/ritmix-rzx50/lowlevel.S3
-rw-r--r--arch/mips/boards/tplink-mr3020/lowlevel.S3
-rw-r--r--arch/mips/boards/tplink-wdr4300/lowlevel.S3
-rw-r--r--arch/mips/boot/dtb.c4
-rw-r--r--arch/mips/boot/main_entry-pbl.c13
-rw-r--r--arch/mips/boot/main_entry.c17
-rw-r--r--arch/mips/boot/start.S4
-rw-r--r--arch/mips/include/asm/asm.h3
-rw-r--r--arch/mips/include/asm/pbl_macros.h4
-rw-r--r--arch/mips/include/asm/relocs.h23
-rw-r--r--arch/mips/include/asm/sections.h14
-rw-r--r--arch/mips/lib/Makefile2
-rw-r--r--arch/mips/lib/barebox.lds.S27
-rw-r--r--arch/mips/lib/cpu-probe.c14
-rw-r--r--arch/mips/lib/pbl.lds.S13
-rw-r--r--arch/mips/lib/reloc.c182
-rw-r--r--arch/mips/lib/sections.c9
26 files changed, 351 insertions, 36 deletions
diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index a4070cfe32..5901930a73 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -7,7 +7,6 @@ config MIPS
select GENERIC_LIB_ASHRDI3
select GENERIC_LIB_LSHRDI3
select HAS_KALLSYMS
- select HAVE_CONFIGURABLE_MEMORY_LAYOUT
select HAVE_CONFIGURABLE_TEXT_BASE
select HAVE_PBL_MULTI_IMAGES
select HAS_DMA
@@ -32,6 +31,24 @@ config PHYS_ADDR_T_64BIT
menu "Machine selection"
+config MIPS_RELOCATION_TABLE_SIZE
+ hex "Relocation table size"
+ range 0x100 0x10000
+ default "0x8000"
+ ---help---
+ A table of relocation data will be appended to the Barebox binary
+ and parsed in relocate_code() to fix up all offsets in the relocated
+ Barebox.
+
+ This option allows the amount of space reserved for the table to be
+ adjusted in a range from 256 up to 64k. The default is 32k and should
+ be ok in most cases. Reduce this value to shrink the size of U-Boot
+ binary.
+
+ The build will fail and a valid size suggested if this is too small.
+
+ If unsure, leave at the default value.
+
config BUILTIN_DTB
bool "link a DTB into the barebox image"
depends on OFTREE
diff --git a/arch/mips/Makefile b/arch/mips/Makefile
index 5fbd51ceee..6f1a1ce617 100644
--- a/arch/mips/Makefile
+++ b/arch/mips/Makefile
@@ -59,7 +59,7 @@ CPPFLAGS += -DTEXT_BASE=$(CONFIG_TEXT_BASE)
ifndef CONFIG_MODULES
# Add cleanup flags
CPPFLAGS += -fdata-sections -ffunction-sections
-LDFLAGS_barebox += -static --gc-sections
+LDFLAGS_barebox += -static --gc-sections --emit-relocs
endif
ifdef CONFIG_IMAGE_COMPRESSION
@@ -116,6 +116,13 @@ CFLAGS += $(cflags-y)
lds-$(CONFIG_GENERIC_LINKER_SCRIPT) := arch/mips/lib/barebox.lds
+cmd_barebox__ ?= $(LD) $(LDFLAGS) $(LDFLAGS_barebox) -o $@ \
+ -T $(barebox-lds) \
+ --start-group $(barebox-common) --end-group \
+ $(filter-out $(barebox-lds) $(barebox-common) FORCE ,$^); \
+ $(objtree)/scripts/mips-relocs $@
+
+
CLEAN_FILES += arch/mips/lib/barebox.lds barebox.map barebox.S
pbl := arch/mips/pbl
diff --git a/arch/mips/boards/8devices-lima/lowlevel.S b/arch/mips/boards/8devices-lima/lowlevel.S
index b53b23b42a..dd1ab6247d 100644
--- a/arch/mips/boards/8devices-lima/lowlevel.S
+++ b/arch/mips/boards/8devices-lima/lowlevel.S
@@ -11,6 +11,7 @@
#include <mach/pbl_macros.h>
#include <mach/pbl_ll_init_qca4531.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -38,4 +39,4 @@ skip_flash_test:
dcache_enable
skip_pll_ram_config:
-ENTRY_FUNCTION_END(BOARD_PBL_START, qca4531_8devices_lima)
+ENTRY_FUNCTION_END(BOARD_PBL_START, qca4531_8devices_lima, SZ_64M)
diff --git a/arch/mips/boards/black-swift/lowlevel.S b/arch/mips/boards/black-swift/lowlevel.S
index 0ba77435f8..5c5afcdf09 100644
--- a/arch/mips/boards/black-swift/lowlevel.S
+++ b/arch/mips/boards/black-swift/lowlevel.S
@@ -11,9 +11,10 @@
#include <asm/asm.h>
#include <mach/pbl_macros.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
ar9331_pbl_generic_start
-ENTRY_FUNCTION_END(BOARD_PBL_START, black_swift)
+ENTRY_FUNCTION_END(BOARD_PBL_START, black_swift, SZ_64M)
diff --git a/arch/mips/boards/dlink-dir-320/lowlevel.S b/arch/mips/boards/dlink-dir-320/lowlevel.S
index 9f3bd5dead..da969bc74e 100644
--- a/arch/mips/boards/dlink-dir-320/lowlevel.S
+++ b/arch/mips/boards/dlink-dir-320/lowlevel.S
@@ -9,6 +9,7 @@
#include <asm/pbl_macros.h>
#include <mach/debug_ll.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -19,4 +20,4 @@ ENTRY_FUNCTION(BOARD_PBL_START)
/* CPU/SoC specific setup ... */
/* ... absent */
-ENTRY_FUNCTION_END(BOARD_PBL_START, dlink_dir_320)
+ENTRY_FUNCTION_END(BOARD_PBL_START, dlink_dir_320, SZ_32M)
diff --git a/arch/mips/boards/dptechnics-dpt-module/lowlevel.S b/arch/mips/boards/dptechnics-dpt-module/lowlevel.S
index 28b8f4fac7..b5621963c3 100644
--- a/arch/mips/boards/dptechnics-dpt-module/lowlevel.S
+++ b/arch/mips/boards/dptechnics-dpt-module/lowlevel.S
@@ -12,9 +12,10 @@
#include <asm/pbl_macros.h>
#include <mach/pbl_macros.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
ar9331_pbl_generic_start
-ENTRY_FUNCTION_END(BOARD_PBL_START, ar9331_dptechnics_dpt_module)
+ENTRY_FUNCTION_END(BOARD_PBL_START, ar9331_dptechnics_dpt_module, SZ_64M)
diff --git a/arch/mips/boards/img-ci20/lowlevel.S b/arch/mips/boards/img-ci20/lowlevel.S
index 056df17bf8..0295e44d1a 100644
--- a/arch/mips/boards/img-ci20/lowlevel.S
+++ b/arch/mips/boards/img-ci20/lowlevel.S
@@ -10,6 +10,7 @@
#include <asm/pbl_macros.h>
#include <mach/debug_ll.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -23,4 +24,4 @@ ENTRY_FUNCTION(BOARD_PBL_START)
debug_ll_outc '.'
debug_ll_ns16550_outnl
-ENTRY_FUNCTION_END(BOARD_PBL_START, img_ci20)
+ENTRY_FUNCTION_END(BOARD_PBL_START, img_ci20, SZ_1G)
diff --git a/arch/mips/boards/loongson-ls1b/lowlevel.S b/arch/mips/boards/loongson-ls1b/lowlevel.S
index 37744e9fac..c533df3ce5 100644
--- a/arch/mips/boards/loongson-ls1b/lowlevel.S
+++ b/arch/mips/boards/loongson-ls1b/lowlevel.S
@@ -9,6 +9,7 @@
#include <mach/loongson1.h>
#include <mach/debug_ll.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -21,4 +22,4 @@ ENTRY_FUNCTION(BOARD_PBL_START)
debug_ll_outc '.'
debug_ll_ns16550_outnl
-ENTRY_FUNCTION_END(BOARD_PBL_START, loongson_ls1b)
+ENTRY_FUNCTION_END(BOARD_PBL_START, loongson_ls1b, SZ_64M)
diff --git a/arch/mips/boards/qemu-malta/lowlevel.S b/arch/mips/boards/qemu-malta/lowlevel.S
index 3861ae9e46..e4ecde45cf 100644
--- a/arch/mips/boards/qemu-malta/lowlevel.S
+++ b/arch/mips/boards/qemu-malta/lowlevel.S
@@ -10,6 +10,7 @@
#include <asm/asm.h>
#include <asm/pbl_macros.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
#include <asm/addrspace.h>
#include <asm/gt64120.h>
@@ -82,4 +83,4 @@ __start:
li t0, GT_LD(0x1bdfffff)
sw t0, GT_PCI0M1HD_OFS(t1)
-ENTRY_FUNCTION_END(BOARD_PBL_START, qemu_malta)
+ENTRY_FUNCTION_END(BOARD_PBL_START, qemu_malta, SZ_256M)
diff --git a/arch/mips/boards/ritmix-rzx50/lowlevel.S b/arch/mips/boards/ritmix-rzx50/lowlevel.S
index 7a9743835e..33810f67f5 100644
--- a/arch/mips/boards/ritmix-rzx50/lowlevel.S
+++ b/arch/mips/boards/ritmix-rzx50/lowlevel.S
@@ -10,6 +10,7 @@
#include <asm/pbl_macros.h>
#include <mach/debug_ll.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -23,4 +24,4 @@ ENTRY_FUNCTION(BOARD_PBL_START)
debug_ll_outc '.'
debug_ll_ns16550_outnl
-ENTRY_FUNCTION_END(BOARD_PBL_START, rzx50)
+ENTRY_FUNCTION_END(BOARD_PBL_START, rzx50, SZ_64M)
diff --git a/arch/mips/boards/tplink-mr3020/lowlevel.S b/arch/mips/boards/tplink-mr3020/lowlevel.S
index 0a1c193c1e..b96292ecc4 100644
--- a/arch/mips/boards/tplink-mr3020/lowlevel.S
+++ b/arch/mips/boards/tplink-mr3020/lowlevel.S
@@ -11,9 +11,10 @@
#include <asm/asm.h>
#include <mach/pbl_macros.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
ar9331_pbl_generic_start
-ENTRY_FUNCTION_END(BOARD_PBL_START, tplink_mr3020)
+ENTRY_FUNCTION_END(BOARD_PBL_START, tplink_mr3020, SZ_32M)
diff --git a/arch/mips/boards/tplink-wdr4300/lowlevel.S b/arch/mips/boards/tplink-wdr4300/lowlevel.S
index 461b257f41..8aed5ddcf1 100644
--- a/arch/mips/boards/tplink-wdr4300/lowlevel.S
+++ b/arch/mips/boards/tplink-wdr4300/lowlevel.S
@@ -11,6 +11,7 @@
#include <mach/pbl_macros.h>
#include <mach/pbl_ll_init_ar9344_1.1.h>
#include <asm/pbl_nmon.h>
+#include <linux/sizes.h>
ENTRY_FUNCTION(BOARD_PBL_START)
@@ -35,4 +36,4 @@ skip_flash_test:
skip_pll_ram_config:
-ENTRY_FUNCTION_END(BOARD_PBL_START, ar9344_tl_wdr4300_v1.7)
+ENTRY_FUNCTION_END(BOARD_PBL_START, ar9344_tl_wdr4300_v1.7, SZ_128M)
diff --git a/arch/mips/boot/dtb.c b/arch/mips/boot/dtb.c
index ea30e16f21..5e316270f6 100644
--- a/arch/mips/boot/dtb.c
+++ b/arch/mips/boot/dtb.c
@@ -29,10 +29,6 @@ void of_add_memory_bank(struct device_node *node, bool dump, int r,
if (dump)
pr_info("%s: %s: 0x%llx@0x%llx\n", node->name, str, size, base);
-
- if (glob_fdt && glob_fdt_size)
- request_sdram_region("fdt", (resource_size_t)glob_fdt,
- glob_fdt_size);
}
extern char __dtb_start[];
diff --git a/arch/mips/boot/main_entry-pbl.c b/arch/mips/boot/main_entry-pbl.c
index b40887b064..02ddd5ec24 100644
--- a/arch/mips/boot/main_entry-pbl.c
+++ b/arch/mips/boot/main_entry-pbl.c
@@ -18,14 +18,14 @@ extern void *input_data_end;
unsigned long free_mem_ptr;
unsigned long free_mem_end_ptr;
-void pbl_main_entry(void *fdt, void *fdt_end);
+void pbl_main_entry(void *fdt, void *fdt_end, u32 ram_size);
static unsigned long *ttb;
static void barebox_uncompress(void *compressed_start, unsigned int len)
{
/* set 128 KiB at the end of the MALLOC_BASE for early malloc */
- free_mem_ptr = MALLOC_BASE + MALLOC_SIZE - SZ_128K;
+ free_mem_ptr = TEXT_BASE - SZ_128K;
free_mem_end_ptr = free_mem_ptr + SZ_128K;
ttb = (void *)((free_mem_ptr - 0x4000) & ~0x3fff);
@@ -33,11 +33,12 @@ static void barebox_uncompress(void *compressed_start, unsigned int len)
pbl_barebox_uncompress((void*)TEXT_BASE, compressed_start, len);
}
-void __section(.text_entry) pbl_main_entry(void *fdt, void *fdt_end)
+void __section(.text_entry) pbl_main_entry(void *fdt, void *fdt_end,
+ u32 ram_size)
{
u32 pg_start, pg_end, pg_len, fdt_len;
void *fdt_new;
- void (*barebox)(void *fdt, u32 fdt_len);
+ void (*barebox)(void *fdt, u32 fdt_len, u32 ram_size);
puts_ll("pbl_main_entry()\n");
@@ -51,9 +52,9 @@ void __section(.text_entry) pbl_main_entry(void *fdt, void *fdt_end)
barebox_uncompress(&input_data, pg_len);
fdt_len = (u32)fdt_end - (u32)fdt;
- fdt_new = (void *)PAGE_ALIGN_DOWN(STACK_BASE - fdt_len);
+ fdt_new = (void *)PAGE_ALIGN_DOWN(TEXT_BASE - MALLOC_SIZE - STACK_SIZE - fdt_len);
memcpy(fdt_new, fdt, fdt_len);
barebox = (void *)TEXT_BASE;
- barebox(fdt_new, fdt_len);
+ barebox(fdt_new, fdt_len, ram_size);
}
diff --git a/arch/mips/boot/main_entry.c b/arch/mips/boot/main_entry.c
index 84325da93a..5b88730b07 100644
--- a/arch/mips/boot/main_entry.c
+++ b/arch/mips/boot/main_entry.c
@@ -12,6 +12,7 @@
#include <asm/cpu-features.h>
#include <asm/mipsregs.h>
#include <asm/addrspace.h>
+#include <linux/sizes.h>
extern void handle_reserved(void);
@@ -61,6 +62,7 @@ static void trap_init(void)
extern void *glob_fdt;
extern u32 glob_fdt_size;
+extern unsigned long mips_stack_top;
/**
* Called plainly from assembler code
@@ -69,6 +71,7 @@ extern u32 glob_fdt_size;
*/
void __bare_init main_entry(void *fdt, u32 fdt_size)
{
+ unsigned long malloc_start, malloc_end;
/* clear the BSS first */
memset(__bss_start, 0x00, __bss_stop - __bss_start);
@@ -82,8 +85,18 @@ void __bare_init main_entry(void *fdt, u32 fdt_size)
trap_init();
- mem_malloc_init((void *)MALLOC_BASE,
- (void *)(MALLOC_BASE + MALLOC_SIZE - 1));
+ malloc_end = _stext;
+
+ if (MALLOC_SIZE > 0)
+ malloc_start = malloc_end - MALLOC_SIZE;
+ else
+ malloc_start = malloc_end - SZ_8M;
+
+ pr_debug("initializing malloc pool at 0x%08lx (size 0x%08lx)\n",
+ malloc_start, malloc_end - malloc_start);
+
+ mem_malloc_init((void *)malloc_start, (void *)_stext - 1);
+ mips_stack_top = malloc_start;
glob_fdt = fdt;
glob_fdt_size = fdt_size;
diff --git a/arch/mips/boot/start.S b/arch/mips/boot/start.S
index 6efe03e98d..c1cd2d9dd5 100644
--- a/arch/mips/boot/start.S
+++ b/arch/mips/boot/start.S
@@ -18,6 +18,7 @@ EXPORT(_start)
/* save dtb pointer */
move s0, a0
move s1, a1
+ move s2, a2
/* disable watchpoints */
mtc0 zero, CP0_WATCHLO
@@ -32,7 +33,8 @@ EXPORT(_start)
/* restore dtb pointer */
move a0, s0
move a1, s1
- la v0, main_entry
+ move a2, s2
+ la v0, relocate_code
jal v0
nop
diff --git a/arch/mips/include/asm/asm.h b/arch/mips/include/asm/asm.h
index 7d2b673bd9..a85467ceed 100644
--- a/arch/mips/include/asm/asm.h
+++ b/arch/mips/include/asm/asm.h
@@ -89,13 +89,14 @@ EXPORT(symbol)
/*
* ENTRY_FUNCTION_END - mark end of entry function
*/
-#define ENTRY_FUNCTION_END(symbol, dtb) \
+#define ENTRY_FUNCTION_END(symbol, dtb, ram_size) \
mips_nmon; \
copy_to_link_location symbol; \
stack_setup; \
\
la a0, __dtb_ ## dtb##_start; \
la a1, __dtb_ ## dtb##_end; \
+ li a2, ram_size; \
la v0, pbl_main_entry; \
jal v0; \
nop; \
diff --git a/arch/mips/include/asm/pbl_macros.h b/arch/mips/include/asm/pbl_macros.h
index e78d1afe6a..c62910ff60 100644
--- a/arch/mips/include/asm/pbl_macros.h
+++ b/arch/mips/include/asm/pbl_macros.h
@@ -187,7 +187,7 @@ copy_loop_exit:
*
*/
-#if (STACK_BASE + STACK_SIZE) % 16 != 0
+#if (TEXT_BASE - MALLOC_SIZE) % 16 != 0
#error stack pointer must be 16-byte-aligned
#endif
@@ -196,7 +196,7 @@ copy_loop_exit:
.set noreorder
/* set stack pointer; reserve four 32-bit argument slots */
- la sp, STACK_BASE + STACK_SIZE - 16
+ la sp, (TEXT_BASE - MALLOC_SIZE - 16)
.set pop
.endm
diff --git a/arch/mips/include/asm/relocs.h b/arch/mips/include/asm/relocs.h
new file mode 100644
index 0000000000..0987c4bb13
--- /dev/null
+++ b/arch/mips/include/asm/relocs.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * MIPS Relocations
+ *
+ * Copyright (c) 2017 Imagination Technologies Ltd.
+ */
+
+#ifndef __ASM_MIPS_RELOCS_H__
+#define __ASM_MIPS_RELOCS_H__
+
+#define R_MIPS_NONE 0
+#define R_MIPS_32 2
+#define R_MIPS_26 4
+#define R_MIPS_HI16 5
+#define R_MIPS_LO16 6
+#define R_MIPS_PC16 10
+#define R_MIPS_64 18
+#define R_MIPS_HIGHER 28
+#define R_MIPS_HIGHEST 29
+#define R_MIPS_PC21_S2 60
+#define R_MIPS_PC26_S2 61
+
+#endif /* __ASM_MIPS_RELOCS_H__ */
diff --git a/arch/mips/include/asm/sections.h b/arch/mips/include/asm/sections.h
index 2b8c516038..8e004eaf34 100644
--- a/arch/mips/include/asm/sections.h
+++ b/arch/mips/include/asm/sections.h
@@ -1 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#ifndef __ASM_MIPS_SECTIONS_H
+#define __ASM_MIPS_SECTIONS_H
+
#include <asm-generic/sections.h>
+
+/**
+ * __rel_start: Relocation data generated by the mips-relocs tool
+ *
+ * See arch/mips/lib/reloc.c for details on the format & use of this data.
+ */
+extern uint8_t __rel_start[];
+
+#endif
diff --git a/arch/mips/lib/Makefile b/arch/mips/lib/Makefile
index 7372e8dc27..4ba42e526e 100644
--- a/arch/mips/lib/Makefile
+++ b/arch/mips/lib/Makefile
@@ -4,6 +4,8 @@ obj-y += cpu-probe.o
lwl-y += end.o
obj-y += traps.o
obj-y += genex.o
+obj-y += reloc.o
+obj-y += sections.o
obj-y += shutdown.o
obj-y += dma-default.o
diff --git a/arch/mips/lib/barebox.lds.S b/arch/mips/lib/barebox.lds.S
index 8ddf954517..58b0c5919d 100644
--- a/arch/mips/lib/barebox.lds.S
+++ b/arch/mips/lib/barebox.lds.S
@@ -13,6 +13,8 @@ SECTIONS
. = TEXT_BASE;
. = ALIGN(4);
+
+ .image_start : { *(.__image_start) }
.text :
{
_stext = .;
@@ -79,9 +81,28 @@ SECTIONS
.dtb : { BAREBOX_DTB() }
_edata = .;
+ .image_end : { *(.__image_end) }
+
. = ALIGN(4);
- __bss_start = .;
- .bss : { *(.bss*) }
- __bss_stop = .;
+ /*
+ * .rel must come last so that the mips-relocs tool can shrink
+ * the section size & the PT_LOAD program header filesz.
+ */
+ .data.reloc : {
+ __rel_start = .;
+ BYTE(0x0)
+ . += (32 * 1024) - 1;
+ }
+
_end = .;
+
+ .bss __rel_start (OVERLAY) : {
+ __bss_start = .;
+ *(.sbss.*)
+ *(.bss.*)
+ *(COMMON)
+ . = ALIGN(4);
+ __bss_stop = .;
+ }
+
}
diff --git a/arch/mips/lib/cpu-probe.c b/arch/mips/lib/cpu-probe.c
index cf63849743..2556a8b240 100644
--- a/arch/mips/lib/cpu-probe.c
+++ b/arch/mips/lib/cpu-probe.c
@@ -11,6 +11,9 @@
#include <asm/mipsregs.h>
#include <asm/cpu-info.h>
#include <asm/cpu.h>
+#include <memory.h>
+#include <asm-generic/memory_layout.h>
+#include <init.h>
const char *__cpu_name;
struct cpuinfo_mips cpu_data[1];
@@ -161,3 +164,14 @@ void cpu_probe(void)
break;
}
}
+
+unsigned long mips_stack_top;
+
+static int mips_request_stack(void)
+{
+ if (!request_sdram_region("stack", mips_stack_top - STACK_SIZE, STACK_SIZE))
+ pr_err("Error: Cannot request SDRAM region for stack\n");
+
+ return 0;
+}
+coredevice_initcall(mips_request_stack);
diff --git a/arch/mips/lib/pbl.lds.S b/arch/mips/lib/pbl.lds.S
index f1752ec720..75069b0c50 100644
--- a/arch/mips/lib/pbl.lds.S
+++ b/arch/mips/lib/pbl.lds.S
@@ -6,11 +6,14 @@
#include <asm-generic/barebox.lds.h>
#include <asm-generic/memory_layout.h>
+#include <linux/sizes.h>
+
+#define BASE (TEXT_BASE - SZ_2M)
OUTPUT_ARCH("mips")
SECTIONS
{
- . = HEAD_TEXT_BASE;
+ . = BASE;
PRE_IMAGE
@@ -38,21 +41,21 @@ SECTIONS
. = ALIGN(4);
.data : { *(.data*) }
- pbl_code_size = . - HEAD_TEXT_BASE;
+ pbl_code_size = . - BASE;
. = ALIGN(4);
__piggydata_start = .;
.piggydata : {
*(.piggydata)
}
- __piggydata_end = .;
+ __piggydata_end = . - BASE;
- pbl_image_size = . - HEAD_TEXT_BASE;
+ pbl_image_size = .;
. = ALIGN(4);
__bss_start = .;
.bss : { *(.bss*) }
__bss_stop = .;
- pbl_memory_size = . - HEAD_TEXT_BASE;
+ pbl_memory_size = . - BASE;
_end = .;
}
diff --git a/arch/mips/lib/reloc.c b/arch/mips/lib/reloc.c
new file mode 100644
index 0000000000..9756d61666
--- /dev/null
+++ b/arch/mips/lib/reloc.c
@@ -0,0 +1,182 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * MIPS Relocation
+ *
+ * Copyright (c) 2017 Imagination Technologies Ltd.
+ *
+ * Relocation data, found in the .rel section, is generated by the mips-relocs
+ * tool & contains a record of all locations in the Barebox binary that need to
+ * be fixed up during relocation.
+ *
+ * The data is a sequence of unsigned integers, which are of somewhat arbitrary
+ * size. This is achieved by encoding integers as a sequence of bytes, each of
+ * which contains 7 bits of data with the most significant bit indicating
+ * whether any further bytes need to be read. The least significant bits of the
+ * integer are found in the first byte - ie. it somewhat resembles little
+ * endian.
+ *
+ * Each pair of two integers represents a relocation that must be applied. The
+ * first integer represents the type of relocation as a standard ELF relocation
+ * type (ie. R_MIPS_*). The second integer represents the offset at which to
+ * apply the relocation, relative to the previous relocation or for the first
+ * relocation the start of the relocated .text section.
+ *
+ * The end of the relocation data is indicated when type R_MIPS_NONE (0) is
+ * read, at which point no further integers should be read. That is, the
+ * terminating R_MIPS_NONE reloc includes no offset.
+ */
+
+#include <common.h>
+
+#include <asm/bitops.h>
+#include <asm/cache.h>
+#include <asm/cacheops.h>
+#include <asm/cpu.h>
+#include <asm/cpu-info.h>
+#include <asm/io.h>
+#include <asm/mipsregs.h>
+#include <asm/relocs.h>
+#include <asm/sections.h>
+#include <linux/sizes.h>
+#include <asm-generic/memory_layout.h>
+
+#define MAX_BSS_SIZE SZ_1M
+
+void main_entry(void *fdt, u32 fdt_size);
+void relocate_code(void *fdt, u32 fdt_size, u32 relocaddr);
+
+/**
+ * read_uint() - Read an unsigned integer from the buffer
+ * @buf: pointer to a pointer to the reloc buffer
+ *
+ * Read one whole unsigned integer from the relocation data pointed to by @buf,
+ * advancing @buf past the bytes encoding the integer.
+ *
+ * Returns: the integer read from @buf
+ */
+static unsigned long read_uint(uint8_t **buf)
+{
+ unsigned long val = 0;
+ unsigned int shift = 0;
+ uint8_t new;
+
+ do {
+ new = *(*buf)++;
+ val |= (new & 0x7f) << shift;
+ shift += 7;
+ } while (new & 0x80);
+
+ return val;
+}
+
+/**
+ * apply_reloc() - Apply a single relocation
+ * @type: the type of reloc (R_MIPS_*)
+ * @addr: the address that the reloc should be applied to
+ * @off: the relocation offset, ie. number of bytes we're moving Barebox by
+ *
+ * Apply a single relocation of type @type at @addr. This function is
+ * intentionally simple, and does the bare minimum needed to fixup the
+ * relocated Barebox - in particular, it does not check for overflows.
+ */
+static void apply_reloc(unsigned int type, void *addr, long off)
+{
+ uint32_t u32;
+
+ switch (type) {
+ case R_MIPS_26:
+ u32 = *(uint32_t *)addr;
+ u32 = (u32 & GENMASK(31, 26)) |
+ ((u32 + (off >> 2)) & GENMASK(25, 0));
+ *(uint32_t *)addr = u32;
+ break;
+
+ case R_MIPS_32:
+ *(uint32_t *)addr += off;
+ break;
+
+ case R_MIPS_64:
+ *(uint64_t *)addr += off;
+ break;
+
+ case R_MIPS_HI16:
+ *(uint32_t *)addr += off >> 16;
+ break;
+
+ default:
+ panic("Unhandled reloc type %u\n", type);
+ }
+}
+
+/**
+ * relocate_code() - Relocate Barebox, generally from flash to DDR
+ * @start_addr_sp: new stack pointer
+ * @new_gd: pointer to relocated global data
+ * @relocaddr: the address to relocate to
+ *
+ * Relocate Barebox from its current location (generally in flash) to a new one
+ * (generally in DDR). This function will copy the Barebox binary & apply
+ * relocations as necessary, then jump to board_init_r in the new build of
+ * Barebox. As such, this function does not return.
+ */
+void relocate_code(void *fdt, u32 fdt_size, u32 ram_size)
+{
+ unsigned long addr, length, bss_len;
+ u32 relocaddr, new_stack;
+ uint8_t *buf, *bss_start;
+ unsigned int type;
+ long off;
+
+ length = barebox_image_size + MAX_BSS_SIZE;
+ relocaddr = ALIGN_DOWN(ram_size - barebox_image_size, SZ_64K);
+ relocaddr = KSEG0ADDR(relocaddr);
+ new_stack = relocaddr - MALLOC_SIZE - 16;
+
+ /*
+ * Ensure that we're relocating by an offset which is a multiple of
+ * 64KiB, ie. doesn't change the least significant 16 bits of any
+ * addresses. This allows us to discard R_MIPS_LO16 relocs, saving
+ * space in the Barebox binary & complexity in handling them.
+ */
+ off = relocaddr - (unsigned long)__image_start;
+ if (off & 0xffff)
+ panic("Mis-aligned relocation\n");
+
+ /* Copy Barebox to RAM */
+ memcpy((void *)relocaddr, __image_start, length);
+
+ /* Now apply relocations to the copy in RAM */
+ buf = __rel_start;
+ addr = relocaddr;
+ while (true) {
+ type = read_uint(&buf);
+ if (type == R_MIPS_NONE)
+ break;
+
+ addr += read_uint(&buf) << 2;
+ apply_reloc(type, (void *)addr, off);
+ }
+
+ /* Ensure the icache is coherent */
+ flush_cache_all();
+
+ /* Clear the .bss section */
+ bss_start = (uint8_t *)((unsigned long)__bss_start + off);
+ bss_len = (unsigned long)&__bss_stop - (unsigned long)__bss_start;
+ memset(bss_start, 0, bss_len);
+
+ __asm__ __volatile__ (
+ "move $a0, %0\n"
+ " move $a1, %1\n"
+ " move $31, $0\n"
+ " move $sp, %2\n"
+ " jr %3\n"
+ : /* no outputs */
+ : "r"(fdt),
+ "r"(fdt_size),
+ "r"(new_stack),
+ "r"((unsigned long)main_entry + off));
+
+ /* Since we jumped to the new Barebox above, we won't get here */
+ unreachable();
+}
diff --git a/arch/mips/lib/sections.c b/arch/mips/lib/sections.c
new file mode 100644
index 0000000000..66d559f94a
--- /dev/null
+++ b/arch/mips/lib/sections.c
@@ -0,0 +1,9 @@
+#include <common.h>
+#include <asm/sections.h>
+#include <linux/types.h>
+
+char _text[0] __attribute__((section("._text")));
+char __bss_start[0] __attribute__((section(".__bss_start")));
+char __bss_stop[0] __attribute__((section(".__bss_stop")));
+char __image_start[0] __attribute__((section(".__image_start")));
+char __image_end[0] __attribute__((section(".__image_end")));