Skip to contentSkip to conversation

LoongArch initial support

PATCH In review Yao Zi 2 participants 24 messages Nov 12 – Nov 13

Subsystems (9)
lore
Share & tools
lore

Yao Zi

author
#0 lore raw

v7 cover LoongArch initial support

Hi all,

So far this series has implemented general support for initializing CPU, exceptions, kernel booting, CPU and timer drivers, QEMU LoongArch virt machine support and UEFI standard compliant EFI booting support.

LoongArch had defined 3 ISA variants, LA64, LA32 and LA32R (Reduced, intended for MCUs and education purpose). This is a little-endian only architecture. Only LA64 is implemented in this series since the hardware is more widely available, and I'll work on 32bit support later.

This series had passed checkpatch with exceptions on some false alarms and headers imported elsewhere. I've tested virtio devices, direct kernel booting, efistub kernel booting, grub, and hello/selftests EFI app.

LoongArch CI depends on some already-merged dockerfile[1] and buildman[2] changes to run. With the docker image replaced with one built by myself, this series passes the Azure CI[3].

Toolchain can be found at [4] or using upstream one, to build

make qemu-loongarch64_defconfig
make

To run in QEMU:

qemu-system-loongarch64 -nographic -machine virt -bios u-boot.bin

TODOs on the architecture & board:
- loongson,ls7a-rtc driver
- The only device on QEMU board haven't been supported yet
- LoongArch sandbox host support
- TLB enablement
- This is required for real CPU to utilize caches
- Possibly EFI-APP support for real machine

Thanks for your time and review!

Link to v5: https://lore.kernel.org/u-boot/[email protected]/
Link to v4: https://lore.kernel.org/u-boot/[email protected]/
Link to v3: https://lore.kernel.org/u-boot/[email protected]/
Link to v2: https://lore.kernel.org/u-boot/[email protected]/
Link to v1: https://lore.kernel.org/u-boot/[email protected]/

[1]: https://lore.kernel.org/u-boot/[email protected]/
[2]: https://lore.kernel.org/u-boot/[email protected]/
[3]: https://github.com/u-boot/u-boot/pull/1073
[4]: https://mirrors.edge.kernel.org/pub/tools/crosstool/files/bin/x86_64/14.2.0/x86_64-gcc-14.2.0-nolibc-loongarch64-linux.tar.gz

Jiaxun Yang (16):
lib: fdtdec: Handle multiple memory nodes linux/io.h: Use map_physmem to implement ioremap
image: Take entry point as an output of booti_setup
elf: Define LoongArch bits
image: Define IH_ARCH_LOONGARCH
LoongArch: skeleton and headers
LoongArch: lib: General routines
LoongArch: CPU assembly routines
LoongArch: Exception handling
LoongArch: Boot Image bits
LoongArch: Generic platform type
cpu: Add loongarch_cpu driver
timer: Add loongarch_timer driver
board: emulation: Add qemu-loongarch
efi: LoongArch: Define LoongArch bits everywhere
efi: LoongArch: Implement everything

Yao Zi (4):
board_f: Add LoongArch path of mon_len calculation
doc: arch: Add documentation for LoongArch
MAINTAINERS: Add entry for LoongArch
CI: Wire up qemu-loongarch64

 .azure-pipelines.yml                          |   11 +-
 .gitlab-ci.yml                                |    6 +
 MAINTAINERS                                   |   11 +
 Makefile                                      |    2 +
 arch/Kconfig                                  |   17 +
 arch/arm/lib/image.c                          |    3 +-
 arch/loongarch/Kconfig                        |   64 +
 arch/loongarch/Makefile                       |   21 +
 arch/loongarch/config.mk                      |   27 +
 arch/loongarch/cpu/Makefile                   |    9 +
 arch/loongarch/cpu/cpu.c                      |   17 +
 arch/loongarch/cpu/generic/Kconfig            |   13 +
 arch/loongarch/cpu/generic/Makefile           |    7 +
 arch/loongarch/cpu/generic/cpu.c              |   23 +
 arch/loongarch/cpu/generic/dram.c             |   16 +
 arch/loongarch/cpu/genex.S                    |   21 +
 arch/loongarch/cpu/smp_secondary.S            |   55 +
 arch/loongarch/cpu/start.S                    |  168 ++
 arch/loongarch/cpu/u-boot.lds                 |   85 +
 arch/loongarch/dts/Makefile                   |    6 +
 arch/loongarch/include/asm/acpi_table.h       |    8 +
 arch/loongarch/include/asm/addrspace.h        |   86 +
 .../include/asm/arch-generic/entry-init.h     |   15 +
 arch/loongarch/include/asm/asm.h              |  186 +++
 arch/loongarch/include/asm/atomic.h           |   12 +
 arch/loongarch/include/asm/barrier.h          |  138 ++
 arch/loongarch/include/asm/bitops.h           |  156 ++
 arch/loongarch/include/asm/byteorder.h        |   22 +
 arch/loongarch/include/asm/cache.h            |   35 +
 arch/loongarch/include/asm/config.h           |   11 +
 arch/loongarch/include/asm/cpu.h              |  123 ++
 arch/loongarch/include/asm/dma-mapping.h      |   27 +
 arch/loongarch/include/asm/global_data.h      |   50 +
 arch/loongarch/include/asm/gpio.h             |   11 +
 arch/loongarch/include/asm/io.h               |  399 +++++
 arch/loongarch/include/asm/linkage.h          |   11 +
 arch/loongarch/include/asm/loongarch.h        | 1467 +++++++++++++++++
 arch/loongarch/include/asm/posix_types.h      |   87 +
 arch/loongarch/include/asm/processor.h        |   11 +
 arch/loongarch/include/asm/ptrace.h           |   33 +
 arch/loongarch/include/asm/regdef.h           |   42 +
 arch/loongarch/include/asm/sections.h         |    8 +
 arch/loongarch/include/asm/setjmp.h           |   20 +
 arch/loongarch/include/asm/spl.h              |   11 +
 arch/loongarch/include/asm/stackframe.h       |  175 ++
 arch/loongarch/include/asm/string.h           |   11 +
 arch/loongarch/include/asm/system.h           |   72 +
 arch/loongarch/include/asm/types.h            |   37 +
 arch/loongarch/include/asm/u-boot-loongarch.h |   23 +
 arch/loongarch/include/asm/u-boot.h           |   30 +
 arch/loongarch/include/asm/unaligned.h        |   11 +
 arch/loongarch/lib/Makefile                   |   22 +
 arch/loongarch/lib/asm-offsets.c              |   66 +
 arch/loongarch/lib/boot.c                     |   14 +
 arch/loongarch/lib/bootm.c                    |  129 ++
 arch/loongarch/lib/cache.c                    |  235 +++
 arch/loongarch/lib/crt0_loongarch_efi.S       |  182 ++
 arch/loongarch/lib/elf_loongarch_efi.lds      |   76 +
 arch/loongarch/lib/image.c                    |   77 +
 arch/loongarch/lib/interrupts.c               |  184 +++
 arch/loongarch/lib/reloc_loongarch_efi.c      |  101 ++
 arch/loongarch/lib/reset.c                    |   14 +
 arch/loongarch/lib/setjmp.S                   |   52 +
 arch/riscv/lib/image.c                        |    4 +-
 arch/sandbox/lib/bootm.c                      |    2 +-
 board/emulation/qemu-loongarch/Kconfig        |   68 +
 board/emulation/qemu-loongarch/MAINTAINERS    |    9 +
 board/emulation/qemu-loongarch/Makefile       |    6 +
 .../emulation/qemu-loongarch/qemu-loongarch.c |   67 +
 .../qemu-loongarch/qemu-loongarch.env         |    5 +
 boot/Kconfig                                  |    1 +
 boot/bootm.c                                  |    5 +-
 boot/image.c                                  |    1 +
 cmd/Kconfig                                   |    2 +-
 cmd/booti.c                                   |    5 +-
 common/board_f.c                              |    2 +-
 common/spl/spl.c                              |    9 +-
 configs/qemu-loongarch64_defconfig            |   35 +
 doc/arch/index.rst                            |    1 +
 doc/arch/loongarch.rst                        |   31 +
 doc/board/emulation/index.rst                 |    1 +
 doc/board/emulation/qemu-loongarch.rst        |   39 +
 drivers/cpu/Kconfig                           |    6 +
 drivers/cpu/Makefile                          |    1 +
 drivers/cpu/loongarch_cpu.c                   |  145 ++
 drivers/timer/Kconfig                         |    8 +
 drivers/timer/Makefile                        |    1 +
 drivers/timer/loongarch_timer.c               |  132 ++
 include/asm-generic/global_data.h             |    2 +-
 include/asm-generic/pe.h                      |    2 +
 include/config_distro_bootcmd.h               |    5 +
 include/configs/qemu-loongarch.h              |   17 +
 include/elf.h                                 |    9 +-
 include/host_arch.h                           |    2 +
 include/image.h                               |    8 +-
 include/linux/io.h                            |    3 +-
 include/pe.h                                  |    2 +
 lib/efi_loader/Kconfig                        |    2 +-
 lib/efi_loader/efi_helper.c                   |    7 +
 lib/efi_loader/efi_image_loader.c             |   40 +
 lib/efi_loader/efi_runtime.c                  |    4 +
 .../efi_selftest_miniapp_exception.c          |    3 +
 lib/fdtdec.c                                  |  127 +-
 103 files changed, 5803 insertions(+), 78 deletions(-)
 create mode 100644 arch/loongarch/Kconfig
 create mode 100644 arch/loongarch/Makefile
 create mode 100644 arch/loongarch/config.mk
 create mode 100644 arch/loongarch/cpu/Makefile
 create mode 100644 arch/loongarch/cpu/cpu.c
 create mode 100644 arch/loongarch/cpu/generic/Kconfig
 create mode 100644 arch/loongarch/cpu/generic/Makefile
 create mode 100644 arch/loongarch/cpu/generic/cpu.c
 create mode 100644 arch/loongarch/cpu/generic/dram.c
 create mode 100644 arch/loongarch/cpu/genex.S
 create mode 100644 arch/loongarch/cpu/smp_secondary.S
 create mode 100644 arch/loongarch/cpu/start.S
 create mode 100644 arch/loongarch/cpu/u-boot.lds
 create mode 100644 arch/loongarch/dts/Makefile
 create mode 100644 arch/loongarch/include/asm/acpi_table.h
 create mode 100644 arch/loongarch/include/asm/addrspace.h
 create mode 100644 arch/loongarch/include/asm/arch-generic/entry-init.h
 create mode 100644 arch/loongarch/include/asm/asm.h
 create mode 100644 arch/loongarch/include/asm/atomic.h
 create mode 100644 arch/loongarch/include/asm/barrier.h
 create mode 100644 arch/loongarch/include/asm/bitops.h
 create mode 100644 arch/loongarch/include/asm/byteorder.h
 create mode 100644 arch/loongarch/include/asm/cache.h
 create mode 100644 arch/loongarch/include/asm/config.h
 create mode 100644 arch/loongarch/include/asm/cpu.h
 create mode 100644 arch/loongarch/include/asm/dma-mapping.h
 create mode 100644 arch/loongarch/include/asm/global_data.h
 create mode 100644 arch/loongarch/include/asm/gpio.h
 create mode 100644 arch/loongarch/include/asm/io.h
 create mode 100644 arch/loongarch/include/asm/linkage.h
 create mode 100644 arch/loongarch/include/asm/loongarch.h
 create mode 100644 arch/loongarch/include/asm/posix_types.h
 create mode 100644 arch/loongarch/include/asm/processor.h
 create mode 100644 arch/loongarch/include/asm/ptrace.h
 create mode 100644 arch/loongarch/include/asm/regdef.h
 create mode 100644 arch/loongarch/include/asm/sections.h
 create mode 100644 arch/loongarch/include/asm/setjmp.h
 create mode 100644 arch/loongarch/include/asm/spl.h
 create mode 100644 arch/loongarch/include/asm/stackframe.h
 create mode 100644 arch/loongarch/include/asm/string.h
 create mode 100644 arch/loongarch/include/asm/system.h
 create mode 100644 arch/loongarch/include/asm/types.h
 create mode 100644 arch/loongarch/include/asm/u-boot-loongarch.h
 create mode 100644 arch/loongarch/include/asm/u-boot.h
 create mode 100644 arch/loongarch/include/asm/unaligned.h
 create mode 100644 arch/loongarch/lib/Makefile
 create mode 100644 arch/loongarch/lib/asm-offsets.c
 create mode 100644 arch/loongarch/lib/boot.c
 create mode 100644 arch/loongarch/lib/bootm.c
 create mode 100644 arch/loongarch/lib/cache.c
 create mode 100644 arch/loongarch/lib/crt0_loongarch_efi.S
 create mode 100644 arch/loongarch/lib/elf_loongarch_efi.lds
 create mode 100644 arch/loongarch/lib/image.c
 create mode 100644 arch/loongarch/lib/interrupts.c
 create mode 100644 arch/loongarch/lib/reloc_loongarch_efi.c
 create mode 100644 arch/loongarch/lib/reset.c
 create mode 100644 arch/loongarch/lib/setjmp.S
 create mode 100644 board/emulation/qemu-loongarch/Kconfig
 create mode 100644 board/emulation/qemu-loongarch/MAINTAINERS
 create mode 100644 board/emulation/qemu-loongarch/Makefile
 create mode 100644 board/emulation/qemu-loongarch/qemu-loongarch.c
 create mode 100644 board/emulation/qemu-loongarch/qemu-loongarch.env
 create mode 100644 configs/qemu-loongarch64_defconfig
 create mode 100644 doc/arch/loongarch.rst
 create mode 100644 doc/board/emulation/qemu-loongarch.rst
 create mode 100644 drivers/cpu/loongarch_cpu.c
 create mode 100644 drivers/timer/loongarch_timer.c
 create mode 100644 include/configs/qemu-loongarch.h
Signature · 1 line
2.56.0
author
#1 lore raw

PATCH v7 1/20 lib: fdtdec: Handle multiple memory nodes

From: Jiaxun Yang <[email protected]>

Current code only tries to fetch the first memory node found in fdt tree and determine memory banks from multiple reg properties.

Specification allows multiple memory nodes in devicetree, rework fdtdec_setup_mem_size_base_lowest and fdtdec_setup_memory_banksize to iterate over all memory nodes.

---

No changes since v6

Changed from v5
- Drop the redundant 0x prefix when printing debug message

Changed from v4
- Do not fail fdtdec_setup_memory_banksize() with -EINVAL when there are more memory banks than CONFIG_DR_DRAM_BANKS to avoid breaking existing boards

No changes since v3

Changed from v2
- Return directly from the inner loop of fdtdec_setup_memory_banksize(), instead of using a goto
- Use %pap instead of %llx when appropriate
- Use phys_addr_t for base in fdtdec_get_next_memory_node()

Changed from v1
- Reword commit message to reflect the fact that dtspec allows multiple memory nodes
- Replace sort_memory_banks() with qsort()
- Do not use CONFIG_VAL when referencing NR_DRAM_BANKS to avoid breaking SPL build
- print gd->ram_size with %pap instead of %lx, drop unnecessary cast for gd->ram_base in fdtdec_setup_size_base_lowest()
- Style improvements

---
 include/asm-generic/global_data.h |   2 +-
 lib/fdtdec.c                      | 127 +++++++++++++++++-------------
 2 files changed, 74 insertions(+), 55 deletions(-)
include/asm-generic/global_data.h +1 -1
index 886c12900ddb..75c80c8a0db4 100644
@@ -462,7 +462,7 @@ struct global_data {
 	/**
 	 * @dram: array describing DRAM banks (start address and size for each bank)
 	 */
-	struct {			/* RAM configuration */
+	struct dram_bank {		/* RAM configuration */
 		phys_addr_t start;
 		phys_size_t size;
 	} dram[CONFIG_NR_DRAM_BANKS];
lib/fdtdec.c +73 -54

169 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#2 lore raw

PATCH v7 2/20 linux/io.h: Use map_physmem to implement ioremap

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

ioremap API is here to be compatible with Linux drivers, to maintain compatibility we need to ensure we are using the same way to determine virtual address for IO devices from physical address.

map_physmem is U-Boot's standard way for doing the trick, we should follow that. For architectures VA == PA it has no impact, for MIPS it has it's own ioremap implementation in asm/io.h anyway.

---

No changes since v3

Changed from v2
- Collect tags

No changes since v1

---
 include/linux/io.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/io.h +2 -1
index 79847886be95..ada6525a1c6a 100644
@@ -59,11 +59,12 @@ static inline void iowrite64(u64 value, volatile void __iomem *addr)
 static inline void __iomem *ioremap(resource_size_t offset,
 				    resource_size_t size)
 {
-	return (void __iomem *)(unsigned long)offset;
+	return (void __iomem *)map_physmem(offset, size, MAP_NOCACHE);
 }
 
 static inline void iounmap(void __iomem *addr)
 {
+	return unmap_physmem(addr, MAP_NOCACHE);
 }
 #endif
 
Signature · 1 line
2.56.0
author
#3 lore raw

PATCH v7 3/20 image: Take entry point as an output of booti_setup

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

For LoongArch, the start of the image is not the entry point. Refactor the code base to allow entry point to be supplied by booti_setup(). Also update the kerneldoc comment for booti_setup() to reflect the latest argument lists.

---

No changes since v6

Changed from v5
- Re-title, change "setup_boot" to "booti_setup"

No changes since v4

Changed from v3
- Collect Tom's review tags

Changed from v2
- Improve commit message, attach a link to documentation of LoongArch kernel image format
- Update kerneldoc comment for booti_setup()

Changed from v1
- Correct type of "entry" argument for RISC-V and sandbox

---
 arch/arm/lib/image.c     | 3 ++-
 arch/riscv/lib/image.c   | 4 +++-
 arch/sandbox/lib/bootm.c | 2 +-
 boot/bootm.c             | 5 +++--
 cmd/booti.c              | 5 +++--
 common/spl/spl.c         | 9 +++++----
 include/image.h          | 7 ++++---
 7 files changed, 21 insertions(+), 14 deletions(-)
arch/arm/lib/image.c +2 -1
index 2268661de93a..62c28b8310a5 100644
@@ -29,7 +29,7 @@ struct Image_header {
 };
 
 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
-		bool force_reloc)
+		ulong *entry, bool force_reloc)
 {
 	struct Image_header *ih;
 	uint64_t dst;
@@ -72,6 +72,7 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
 		dst = gd->dram[0].start;
 
 	*relocated_addr = ALIGN(dst, SZ_2M) + text_offset;
+	*entry = *relocated_addr;
 
 	unmap_sysmem(ih);
 
arch/riscv/lib/image.c +3 -1
index a82f48e9a505..300be365ddfd 100644
@@ -33,7 +33,7 @@ struct linux_image_h {
 };
 
 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
-		bool force_reloc)
+		ulong *entry, bool force_reloc)
 {
 	struct linux_image_h *lhdr;
 
@@ -56,6 +56,8 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
 		*relocated_addr = image;
 	}
 
+	*entry = *relocated_addr;
+
 	unmap_sysmem(lhdr);
 
 	return 0;
arch/sandbox/lib/bootm.c +1 -1
index 7a5f6f7d36ed..1a41c7c2735b 100644
@@ -84,7 +84,7 @@ int do_bootm_linux(int flag, struct bootm_info *bmi)
 
 /* used for testing 'booti' command */
 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
-		bool force_reloc)
+		ulong *entry, bool force_reloc)
 {
 	log_err("Booting is not supported on the sandbox.\n");
 
boot/bootm.c +3 -2
index 4b03a367a217..46a19a37f19f 100644
@@ -888,9 +888,10 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 	    (images->os.arch == IH_ARCH_ARM64 || images->os.arch == IH_ARCH_RISCV)) {
 		ulong relocated_addr;
 		ulong image_size;
+		ulong entry;
 		int ret;
 
-		ret = booti_setup(load, &relocated_addr, &image_size, false);
+		ret = booti_setup(load, &relocated_addr, &image_size, &entry, false);
 		if (ret) {
 			printf("Failed to prep kernel image (err=%d)\n", ret);
 			return BOOTM_ERR_RESET;
@@ -904,7 +905,7 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress)
 			memmove((void *)relocated_addr, load_buf, image_size);
 		}
 
-		images->ep = relocated_addr;
+		images->ep = entry;
 		images->os.start = relocated_addr;
 		images->os.end = relocated_addr + image_size;
 	}
cmd/booti.c +3 -2
index e6f67d6e1368..24c55ff92a27 100644
@@ -27,6 +27,7 @@ static int booti_start(struct bootm_info *bmi)
 	ulong ld;
 	ulong relocated_addr;
 	ulong image_size;
+	ulong entry;
 	uint8_t *temp;
 	ulong dest;
 	ulong dest_end;
@@ -74,7 +75,7 @@ static int booti_start(struct bootm_info *bmi)
 	}
 	unmap_sysmem((void *)ld);
 
-	ret = booti_setup(ld, &relocated_addr, &image_size, false);
+	ret = booti_setup(ld, &relocated_addr, &image_size, &entry, false);
 	if (ret)
 		return 1;
 
@@ -85,7 +86,7 @@ static int booti_start(struct bootm_info *bmi)
 		memmove((void *)relocated_addr, (void *)ld, image_size);
 	}
 
-	images->ep = relocated_addr;
+	images->ep = entry;
 	images->os.start = relocated_addr;
 	images->os.end = relocated_addr + image_size;
 
common/spl/spl.c +5 -4
index 6c9a1a1cbcc3..3856aafbca9a 100644
@@ -115,7 +115,8 @@ int __weak bootz_setup(ulong image, ulong *start, ulong *end)
 	 return 1;
 }
 
-int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
+int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size,
+		       ulong *entry, bool force_reloc)
 {
 	 return 1;
 }
@@ -342,13 +343,13 @@ int spl_parse_image_header(struct spl_image_info *spl_image,
 	}
 
 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_BOOTI)) {
-		ulong start, size;
+		ulong start, size, entry;
 
-		if (!booti_setup((ulong)header, &start, &size, 0)) {
+		if (!booti_setup((ulong)header, &start, &size, &entry, 0)) {
 			spl_image->name = "Linux";
 			spl_image->os = IH_OS_LINUX;
 			spl_image->load_addr = start;
-			spl_image->entry_point = start;
+			spl_image->entry_point = entry;
 			spl_image->size = size;
 			debug(PHASE_PROMPT
 			      "payload Image, load addr: 0x%lx size: %d\n",
include/image.h +4 -3
index f059fc5811c0..eb19f6d34e39 100644
@@ -1129,16 +1129,17 @@ int image_setup_linux(struct bootm_headers *images);
 int bootz_setup(ulong image, ulong *start, ulong *end);
 
 /**
- * Return the correct start address and size of a Linux aarch64 Image.
+ * Return the correct start address and size of a Linux Image.
  *
  * @image: Address of image
- * @start: Returns start address of image
+ * @relocated_addr: Returns start address of image
  * @size : Returns size image
+ * @entry: Returns entry point of image
  * @force_reloc: Ignore image->ep field, always place image to RAM start
  * Return: 0 if OK, 1 if the image was not recognised
  */
 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
-		bool force_reloc);
+		ulong *entry, bool force_reloc);
 
 /*******************************************************************/
 /* New uImage format specific code (prefixed with fit_) */
Signature · 1 line
2.56.0
author
#4 lore raw

PATCH v7 4/20 elf: Define LoongArch bits

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

They all come from glibc's elf.h

---

No changes since v5

Changed since v4
- Add back the accidentally-removed EM_NUM

No changes since v3

Changed from v2
- Collect tags
- Drop a redundant blank line between /* Version */ comment and EM_LOONGARCH definition

Changed from v1
- Re-title to "elf: Define LoongArch bits" (with the semicolon)
- No function changes involved

---
 include/elf.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
include/elf.h +8 -1
index b88e6cf4032b..23157f5d6b4c 100644
@@ -225,7 +225,8 @@ typedef struct {
 #define EM_MN10300	89		/* Matsushita MN10200 */
 #define EM_MN10200	90		/* Matsushita MN10200 */
 #define EM_PJ		91		/* picoJava */
-#define EM_NUM		92		/* number of machine types */
+#define EM_LOONGARCH	258		/* LoongArch */
+#define EM_NUM		259
 
 /* Version */
 #define EV_NONE		0		/* Invalid */
@@ -705,6 +706,12 @@ unsigned long elf_hash(const unsigned char *name);
 #define R_RISCV_64		2
 #define R_RISCV_RELATIVE	3
 
+/* LoongArch Relocations */
+#define R_LARCH_NONE				0
+#define R_LARCH_32				1
+#define R_LARCH_64				2
+#define R_LARCH_RELATIVE			3
+
 #ifndef __ASSEMBLY__
 unsigned long bootelf_exec(ulong (*entry)(int, char * const[]),
 			   int argc, char *const argv[]);
Signature · 1 line
2.56.0
author
#5 lore raw

PATCH v7 5/20 image: Define IH_ARCH_LOONGARCH

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Allocate the next value to IH_ARCH_LOONGARCH.

---

No functional changes since v1

---
 boot/image.c    | 1 +
 include/image.h | 1 +
 2 files changed, 2 insertions(+)
boot/image.c +1 -0
index 289e80f23a7f..cb12f9db01c1 100644
@@ -86,6 +86,7 @@ static const table_entry_t uimage_arch[] = {
 	{	IH_ARCH_X86_64,		"x86_64",	"AMD x86_64",	},
 	{	IH_ARCH_XTENSA,		"xtensa",	"Xtensa",	},
 	{	IH_ARCH_RISCV,		"riscv",	"RISC-V",	},
+	{	IH_ARCH_LOONGARCH,	"loongarch",	"LoongArch",	},
 	{	-1,			"",		"",		},
 };
 
include/image.h +1 -0
index eb19f6d34e39..41310155beed 100644
@@ -139,6 +139,7 @@ enum {
 	IH_ARCH_X86_64,			/* AMD x86_64, Intel and Via */
 	IH_ARCH_XTENSA,			/* Xtensa	*/
 	IH_ARCH_RISCV,			/* RISC-V */
+	IH_ARCH_LOONGARCH,		/* Loongson LoongArch */
 
 	IH_ARCH_COUNT,
 };
Signature · 1 line
2.56.0
author
#6 lore raw

PATCH v7 6/20 LoongArch: skeleton and headers

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Commit for directories, Kconfig, Makefile and headers, aiming to build for 64-bit LoongArch hardware with FPU.

Some of them are copied from linux, some of them are derived from other architectures, the rest are written on my own.

---

No changes since v5

Changed from v4
- Select SYS_BOOT_GET_CMDLINE to populate images->cmdline_start
- Update the comment for header guards in asm/setjmp.h
- Modify LONG_IOCSRRD/WR to ensure their width matches the machine word size on loongarch64

No changes since v3

Changed from v2
- Expand the commit message to mention the targetting architecture
- Fix definition of SZREG in arch/loongarch/include/asm/asm.h
- Rewrite irq save/restore helpers
- Define BITS_PER_LONG as constant 64 for now
- Drop unnecessary declaration of "struct event" in asm/system.h
- Gate ABI/march-related flags in arch/loongarch/Makefile on CONFIG_ARCH_LA64
- Clean up setjmp.h and select HAVE_SETJMP
- Add FRAMEPOINTER-related Kconfig stuff
- Select lp64d instead of lp64s ABI, so toolchain shipping only lp64d libgcc doesn't complain. Support for platforms without FPU will be re-considered later.

Changed from v1
- Remove the redundant dtbs target from arch/loongarch/dts/Makefile
- Add loongarch64 definitions for HOST_ARCH
- Whitespace clean-up

---
 Makefile                                      |    2 +
 arch/Kconfig                                  |   17 +
 arch/loongarch/Kconfig                        |   40 +
 arch/loongarch/Makefile                       |   21 +
 arch/loongarch/config.mk                      |   23 +
 arch/loongarch/cpu/Makefile                   |    5 +
 arch/loongarch/cpu/u-boot.lds                 |   85 +
 arch/loongarch/dts/Makefile                   |    6 +
 arch/loongarch/include/asm/acpi_table.h       |    8 +
 arch/loongarch/include/asm/addrspace.h        |   86 +
 .../include/asm/arch-generic/entry-init.h     |   15 +
 arch/loongarch/include/asm/asm.h              |  186 +++
 arch/loongarch/include/asm/atomic.h           |   12 +
 arch/loongarch/include/asm/barrier.h          |  138 ++
 arch/loongarch/include/asm/bitops.h           |  156 ++
 arch/loongarch/include/asm/byteorder.h        |   22 +
 arch/loongarch/include/asm/cache.h            |   24 +
 arch/loongarch/include/asm/config.h           |   11 +
 arch/loongarch/include/asm/cpu.h              |  123 ++
 arch/loongarch/include/asm/dma-mapping.h      |   27 +
 arch/loongarch/include/asm/global_data.h      |   43 +
 arch/loongarch/include/asm/gpio.h             |   11 +
 arch/loongarch/include/asm/io.h               |  399 +++++
 arch/loongarch/include/asm/linkage.h          |   11 +
 arch/loongarch/include/asm/loongarch.h        | 1467 +++++++++++++++++
 arch/loongarch/include/asm/posix_types.h      |   87 +
 arch/loongarch/include/asm/processor.h        |   11 +
 arch/loongarch/include/asm/ptrace.h           |   33 +
 arch/loongarch/include/asm/regdef.h           |   42 +
 arch/loongarch/include/asm/sections.h         |    8 +
 arch/loongarch/include/asm/setjmp.h           |   20 +
 arch/loongarch/include/asm/spl.h              |   11 +
 arch/loongarch/include/asm/string.h           |   11 +
 arch/loongarch/include/asm/system.h           |   72 +
 arch/loongarch/include/asm/types.h            |   37 +
 arch/loongarch/include/asm/u-boot-loongarch.h |   23 +
 arch/loongarch/include/asm/u-boot.h           |   30 +
 arch/loongarch/include/asm/unaligned.h        |   11 +
 arch/loongarch/lib/Makefile                   |    5 +
 include/host_arch.h                           |    2 +
 40 files changed, 3341 insertions(+)
 create mode 100644 arch/loongarch/Kconfig
 create mode 100644 arch/loongarch/Makefile
 create mode 100644 arch/loongarch/config.mk
 create mode 100644 arch/loongarch/cpu/Makefile
 create mode 100644 arch/loongarch/cpu/u-boot.lds
 create mode 100644 arch/loongarch/dts/Makefile
 create mode 100644 arch/loongarch/include/asm/acpi_table.h
 create mode 100644 arch/loongarch/include/asm/addrspace.h
 create mode 100644 arch/loongarch/include/asm/arch-generic/entry-init.h
 create mode 100644 arch/loongarch/include/asm/asm.h
 create mode 100644 arch/loongarch/include/asm/atomic.h
 create mode 100644 arch/loongarch/include/asm/barrier.h
 create mode 100644 arch/loongarch/include/asm/bitops.h
 create mode 100644 arch/loongarch/include/asm/byteorder.h
 create mode 100644 arch/loongarch/include/asm/cache.h
 create mode 100644 arch/loongarch/include/asm/config.h
 create mode 100644 arch/loongarch/include/asm/cpu.h
 create mode 100644 arch/loongarch/include/asm/dma-mapping.h
 create mode 100644 arch/loongarch/include/asm/global_data.h
 create mode 100644 arch/loongarch/include/asm/gpio.h
 create mode 100644 arch/loongarch/include/asm/io.h
 create mode 100644 arch/loongarch/include/asm/linkage.h
 create mode 100644 arch/loongarch/include/asm/loongarch.h
 create mode 100644 arch/loongarch/include/asm/posix_types.h
 create mode 100644 arch/loongarch/include/asm/processor.h
 create mode 100644 arch/loongarch/include/asm/ptrace.h
 create mode 100644 arch/loongarch/include/asm/regdef.h
 create mode 100644 arch/loongarch/include/asm/sections.h
 create mode 100644 arch/loongarch/include/asm/setjmp.h
 create mode 100644 arch/loongarch/include/asm/spl.h
 create mode 100644 arch/loongarch/include/asm/string.h
 create mode 100644 arch/loongarch/include/asm/system.h
 create mode 100644 arch/loongarch/include/asm/types.h
 create mode 100644 arch/loongarch/include/asm/u-boot-loongarch.h
 create mode 100644 arch/loongarch/include/asm/u-boot.h
 create mode 100644 arch/loongarch/include/asm/unaligned.h
 create mode 100644 arch/loongarch/lib/Makefile
Makefile +2 -0
index 292d1e073567..fd4b9a2a86e7 100644
@@ -233,6 +233,8 @@ else ifeq ("riscv32", $(MK_ARCH))
   export HOST_ARCH=$(HOST_ARCH_RISCV32)
 else ifeq ("riscv64", $(MK_ARCH))
   export HOST_ARCH=$(HOST_ARCH_RISCV64)
+else ifeq ("loongarch64", $(MK_ARCH))
+  export HOST_ARCH=$(HOST_ARCH_LOONGARCH64)
 endif
 undefine MK_ARCH
 
arch/Kconfig +17 -0
index 8d63afeb138e..0ae7ea8c740b 100644
@@ -110,6 +110,22 @@ config ARM
 	select SUPPORT_LITTLE_ENDIAN
 	select SUPPORT_OF_CONTROL
 
+config LOONGARCH
+	bool "LoongArch architecture"
+	select HAVE_SETJMP
+	select CREATE_ARCH_SYMLINK
+	select SUPPORT_OF_CONTROL
+	select OF_CONTROL
+	select DM
+	select DM_EVENT
+	select SYS_BOOT_GET_CMDLINE
+	imply DM_SERIAL
+	imply BLK
+	imply CLK
+	imply MTD
+	imply TIMER
+	imply CMD_DM
+
 config M68K
 	bool "M68000 architecture"
 	select HAVE_PRIVATE_LIBGCC
@@ -569,6 +585,7 @@ config SYS_NONCACHED_MEMORY
 
 source "arch/arc/Kconfig"
 source "arch/arm/Kconfig"
+source "arch/loongarch/Kconfig"
 source "arch/m68k/Kconfig"
 source "arch/microblaze/Kconfig"
 source "arch/mips/Kconfig"
arch/loongarch/Kconfig new file +40 -0
new file mode 100644
index 000000000000..4e8e9d4ee88b
@@ -0,0 +1,40 @@
+menu "LoongArch architecture"
+	depends on LOONGARCH
+
+config SYS_ARCH
+	default "loongarch"
+
+choice
+	prompt "Target select"
+
+endchoice
+
+# board-specific options below
+
+# platform-specific options below
+
+# architecture-specific options below
+choice
+	prompt "Base ISA"
+
+config ARCH_LA64
+	bool "LoongArch64"
+	select 64BIT
+	select PHYS_64BIT
+	help
+	  Choose this option to target the LoongArch64 base ISA.
+
+endchoice
+
+config DMA_ADDR_T_64BIT
+	bool
+	default y if 64BIT
+
+config STACK_SIZE_SHIFT
+	int
+	default 14
+
+config OF_BOARD_FIXUP
+	default y if OF_SEPARATE
+
+endmenu
arch/loongarch/Makefile new file +21 -0
new file mode 100644
index 000000000000..b507d94613e0
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
+ifeq ($(CONFIG_ARCH_LA64),y)
+	ARCH_FLAGS = -march=loongarch64 -mabi=lp64d
+endif
+
+ifeq ($(CONFIG_$(SPL_)FRAMEPOINTER),y)
+	ARCH_FLAGS += -fno-omit-frame-pointer
+endif
+
+PLATFORM_CPPFLAGS += $(ARCH_FLAGS)
+
+head-y := arch/loongarch/cpu/start.o
+
+libs-y += arch/loongarch/cpu/
+libs-y += arch/loongarch/cpu/$(CPU)/
+libs-y += arch/loongarch/lib/
+
arch/loongarch/config.mk new file +23 -0
new file mode 100644
index 000000000000..7c247400e361
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
+32bit-bfd		= elf32-loongarch
+64bit-bfd		= elf64-loongarch
+32bit-emul		= elf32loongarch
+64bit-emul		= elf64loongarch
+
+ifdef CONFIG_32BIT
+KBUILD_LDFLAGS		+= -m $(32bit-emul)
+PLATFORM_ELFFLAGS	+= -B loongarch -O $(32bit-bfd)
+endif
+
+ifdef CONFIG_64BIT
+KBUILD_LDFLAGS		+= -m $(64bit-emul)
+PLATFORM_ELFFLAGS	+= -B loongarch -O $(64bit-bfd)
+endif
+
+PLATFORM_CPPFLAGS	+= -fpic
+PLATFORM_RELFLAGS	+= -fno-common -ffunction-sections -fdata-sections
+LDFLAGS_u-boot		+= --gc-sections -static -pie
arch/loongarch/cpu/Makefile new file +5 -0
new file mode 100644
index 000000000000..3dbed94cc624
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
arch/loongarch/cpu/u-boot.lds new file +85 -0

85 lines not on this page · raw message

arch/loongarch/dts/Makefile new file +6 -0
new file mode 100644
index 000000000000..5cc395b6ea5d
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+include $(srctree)/scripts/Makefile.dts
+
+# Add any required device tree compiler flags here
+DTC_FLAGS += -R 4 -p 0x1000
arch/loongarch/include/asm/acpi_table.h new file +8 -0
new file mode 100644
index 000000000000..db2f644f07af
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __ASM_ACPI_TABLE_H__
+#define __ASM_ACPI_TABLE_H__
+
+/* Dummy header */
+
+#endif /* __ASM_ACPI_TABLE_H__ */
arch/loongarch/include/asm/addrspace.h new file +86 -0

86 lines not on this page · raw message

arch/loongarch/include/asm/arch-generic/entry-init.h new file +15 -0
new file mode 100644
index 000000000000..a618f66d0d7a
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __ASM_ENTRY_INIT_H
+#define __ASM_ENTRY_INIT_H
+
+	.macro	entry_setup
+	.endm
+
+	.macro	smp_secondary_setup
+	.endm
+
+#endif
arch/loongarch/include/asm/asm.h new file +186 -0

186 lines not on this page · raw message

arch/loongarch/include/asm/atomic.h new file +12 -0
new file mode 100644
index 000000000000..abd0b6f5f342
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __LOONGARCH_ATOMIC_H
+#define __LOONGARCH_ATOMIC_H
+
+#include <asm/system.h>
+#include <asm-generic/atomic.h>
+
+#endif
arch/loongarch/include/asm/barrier.h new file +138 -0

138 lines not on this page · raw message

arch/loongarch/include/asm/bitops.h new file +156 -0

156 lines not on this page · raw message

arch/loongarch/include/asm/byteorder.h new file +22 -0
new file mode 100644
index 000000000000..ba25f25729ac
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __ASM_LOONGARCH_BYTEORDER_H
+#define __ASM_LOONGARCH_BYTEORDER_H
+
+#include <asm/types.h>
+
+#if !defined(__STRICT_ANSI__) || defined(__KERNEL__)
+#  define __BYTEORDER_HAS_U64__
+#  define __SWAB_64_THRU_32__
+#endif
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#include <linux/byteorder/little_endian.h>
+#else
+#include <linux/byteorder/big_endian.h>
+#endif
+
+#endif
arch/loongarch/include/asm/cache.h new file +24 -0
new file mode 100644
index 000000000000..854dd0c0a02e
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef _ASM_LOONGARCH_CACHE_H
+#define _ASM_LOONGARCH_CACHE_H
+
+/* cache */
+void cache_flush(void);
+
+#define cache_op(op, addr)						\
+	__asm__ __volatile__(						\
+	"	cacop	%0, %1					\n"	\
+	:								\
+	: "i" (op), "ZC" (*(unsigned char *)(addr)))
+
+#ifdef CONFIG_SYS_CACHELINE_SIZE
+#define ARCH_DMA_MINALIGN	CONFIG_SYS_CACHELINE_SIZE
+#else
+#define ARCH_DMA_MINALIGN	32
+#endif
+
+#endif /* _ASM_LOONGARCH_CACHE_H */
arch/loongarch/include/asm/config.h new file +11 -0
new file mode 100644
index 000000000000..23eb49847e7b
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef _ASM_CONFIG_H_
+#define _ASM_CONFIG_H_
+
+/* Dummy header */
+
+#endif
arch/loongarch/include/asm/cpu.h new file +123 -0

123 lines not on this page · raw message

arch/loongarch/include/asm/dma-mapping.h new file +27 -0
new file mode 100644
index 000000000000..87088815b955
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __ASM_LOONGARCH_DMA_MAPPING_H
+#define __ASM_LOONGARCH_DMA_MAPPING_H
+
+#include <linux/types.h>
+#include <asm/cache.h>
+#include <cpu_func.h>
+#include <linux/dma-direction.h>
+#include <malloc.h>
+
+static inline void *dma_alloc_coherent(size_t len, unsigned long *handle)
+{
+	/* TODO:For non-coherent system allocate from DMW1 */
+	*handle = (unsigned long)memalign(ARCH_DMA_MINALIGN, len);
+	return (void *)*handle;
+}
+
+static inline void dma_free_coherent(void *addr)
+{
+	free(addr);
+}
+
+#endif
arch/loongarch/include/asm/global_data.h new file +43 -0

43 lines not on this page · raw message

arch/loongarch/include/asm/gpio.h new file +11 -0
new file mode 100644
index 000000000000..b2508fc2e9f8
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __ASM_GPIO_H
+#define __ASM_GPIO_H
+
+#include <asm-generic/gpio.h>
+
+#endif
arch/loongarch/include/asm/io.h new file +399 -0

399 lines not on this page · raw message

arch/loongarch/include/asm/linkage.h new file +11 -0
new file mode 100644
index 000000000000..f004bdd8efe3
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __ASM_LINKAGE_H
+#define __ASM_LINKAGE_H
+
+/* Dummy header */
+
+#endif
arch/loongarch/include/asm/loongarch.h new file +1467 -0

1,467 lines not on this page · raw message

arch/loongarch/include/asm/posix_types.h new file +87 -0

44 lines not on this page · raw message

arch/loongarch/include/asm/processor.h new file +11 -0
new file mode 100644
index 000000000000..7242a3e6ca7e

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/ptrace.h new file +33 -0
new file mode 100644
index 000000000000..1b3c2b5b8f16

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/regdef.h new file +42 -0

42 lines not on this page · raw message

arch/loongarch/include/asm/sections.h new file +8 -0
new file mode 100644
index 000000000000..9f5009a1f235

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/setjmp.h new file +20 -0
new file mode 100644
index 000000000000..b0321155ca0c

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/spl.h new file +11 -0
new file mode 100644
index 000000000000..b8b19f65cb36

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/string.h new file +11 -0
new file mode 100644
index 000000000000..09877956de9a

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/system.h new file +72 -0

72 lines not on this page · raw message

arch/loongarch/include/asm/types.h new file +37 -0
new file mode 100644
index 000000000000..eb5749457922

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/u-boot-loongarch.h new file +23 -0
new file mode 100644
index 000000000000..7f21d2a3e963

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/u-boot.h new file +30 -0
new file mode 100644
index 000000000000..123a7f09a01c

Lines of this file are not shown. View the raw message.

arch/loongarch/include/asm/unaligned.h new file +11 -0
new file mode 100644
index 000000000000..65cd4340ce93

Lines of this file are not shown. View the raw message.

arch/loongarch/lib/Makefile new file +5 -0
new file mode 100644
index 000000000000..3dbed94cc624

Lines of this file are not shown. View the raw message.

include/host_arch.h +2 -0
index 261194bd7c6e..ceeb61d6ad88 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.56.0

Part of the diff is not shown here. View the raw message.

author
#7 lore raw

PATCH v7 7/20 LoongArch: lib: General routines

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Add cache probing through CPUCFG, cache operations, setjmp()/longjump(), and etc. for the architecture.

---

Changes since v6
- Comment on purpose of CACHE_MAX_{LEVEL,INDEX}
- Clean up whitespace in setjmp.S

No changes since v5

Changed from v4
- Drop cache_flush()/cache_invalidate() helpers

Changed from v3
- Align the start address down to cacheline size when flushing dcache by address
- Fix off-by-one error when reading CPUCFG to probe L2/L3 unified cache

Changed from v2
- Improve commit message to list introduced routines, and how cache-probing is implemented
- Pass through addr in do_flush() macros
- Use CACHE_MAX_LEVEL instead of hard-coded 3 in probe_caches()
- Comment on cache behavior and cache probing logic in cache.c

Changed from v1
- Correctly implement dcache operations

---
 arch/loongarch/include/asm/cache.h       |  13 +-
 arch/loongarch/include/asm/global_data.h |   7 +
 arch/loongarch/lib/Makefile              |   7 +
 arch/loongarch/lib/asm-offsets.c         |  66 +++++++
 arch/loongarch/lib/boot.c                |  14 ++
 arch/loongarch/lib/cache.c               | 235 +++++++++++++++++++++++
 arch/loongarch/lib/reset.c               |  14 ++
 arch/loongarch/lib/setjmp.S              |  52 +++++
 8 files changed, 407 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/lib/asm-offsets.c
 create mode 100644 arch/loongarch/lib/boot.c
 create mode 100644 arch/loongarch/lib/cache.c
 create mode 100644 arch/loongarch/lib/reset.c
 create mode 100644 arch/loongarch/lib/setjmp.S
arch/loongarch/include/asm/cache.h +12 -1
index 854dd0c0a02e..857ed645be49 100644
@@ -6,8 +6,10 @@
 #ifndef _ASM_LOONGARCH_CACHE_H
 #define _ASM_LOONGARCH_CACHE_H
 
+#include <linux/bitops.h>
+
 /* cache */
-void cache_flush(void);
+void probe_caches(void);
 
 #define cache_op(op, addr)						\
 	__asm__ __volatile__(						\
@@ -15,6 +17,15 @@ void cache_flush(void);
 	:								\
 	: "i" (op), "ZC" (*(unsigned char *)(addr)))
 
+/* Max levels of D-caches */
+#define CACHE_MAX_LEVEL		3
+/* Max cache ID plus one which is accepted by cacop */
+#define CACHE_MAX_INDEX		6
+#define CACHE_OP		GENMASK(4, 3)
+#define  CACHE_INDEX_INVWB	0x1
+#define  CACHE_HIT_INVWB	0x2
+#define CACHE_INDEX		GENMASK(2, 0)
+
 #ifdef CONFIG_SYS_CACHELINE_SIZE
 #define ARCH_DMA_MINALIGN	CONFIG_SYS_CACHELINE_SIZE
 #else
arch/loongarch/include/asm/global_data.h +7 -0
index 2a9a525e2f37..7aab802ca04d 100644
@@ -10,6 +10,7 @@
 #define __ASM_GBL_DATA_H
 
 #include <linux/types.h>
+#include <asm/cache.h>
 #include <asm/u-boot.h>
 #include <compiler.h>
 
@@ -24,6 +25,12 @@ struct arch_global_data {
 #ifdef CONFIG_SMBIOS
 	ulong smbios_start;		/* Start address of SMBIOS table */
 #endif
+	ushort dcache_ways[CACHE_MAX_INDEX];
+	ushort dcache_sets[CACHE_MAX_INDEX];
+	ushort dcache_linesizes[CACHE_MAX_INDEX];
+	ushort dcache_index[CACHE_MAX_INDEX];
+	ushort dcache_levels;
+	bool dcache_inclusive;
 };
 
 #include <asm-generic/global_data.h>
arch/loongarch/lib/Makefile +7 -0
index 3dbed94cc624..3c17b9cd85af 100644
@@ -3,3 +3,10 @@
 # Copyright (C) 2024 Jiaxun yang <[email protected]>
 #
 
+obj-$(CONFIG_CMD_GO) += boot.o
+obj-y	+= cache.o
+obj-y	+= interrupts.o
+ifeq ($(CONFIG_$(SPL_)SYSRESET),)
+obj-y	+= reset.o
+endif
+obj-y	+= setjmp.o
arch/loongarch/lib/asm-offsets.c new file +66 -0

66 lines not on this page · raw message

arch/loongarch/lib/boot.c new file +14 -0
new file mode 100644
index 000000000000..327be16bb59f
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <asm/u-boot.h>
+
+unsigned long do_go_exec(ulong (*entry)(int, char * const []),
+			 int argc, char *const argv[])
+{
+	cleanup_before_linux();
+
+	return entry(argc, argv);
+}
arch/loongarch/lib/cache.c new file +235 -0

235 lines not on this page · raw message

arch/loongarch/lib/reset.c new file +14 -0
new file mode 100644
index 000000000000..ddf29ee41d95
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <command.h>
+#include <cpu_func.h>
+
+int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	reset_cpu();
+
+	return 0;
+}
arch/loongarch/lib/setjmp.S new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#8 lore raw

PATCH v7 8/20 LoongArch: CPU assembly routines

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

start.S for initialisation, smp_secondary routine for a spin-table like interface for secondary cpus.

---

No changes since v3

Changed from v2
- Add copyright to cpu/cpu.c
- Drop unnecessary includes from cpu/cpu.c
- Drop "resetting..." message from reset_cpu()
- Fix misleading comment about t1 in start.S
- Remove "move sp, s0" after board_init_f_alloc_reserve from start.S
- Clean up whitespaces in start.S

Changed from v1
- Replace t6, which is volatile across C function boundaries, with s5 to hold relocation offset in start.S
- Fix typo "CLear" in smp_secondary.S

---
 arch/loongarch/cpu/Makefile        |   4 +
 arch/loongarch/cpu/cpu.c           |  17 +++
 arch/loongarch/cpu/smp_secondary.S |  55 ++++++++++
 arch/loongarch/cpu/start.S         | 168 +++++++++++++++++++++++++++++
 4 files changed, 244 insertions(+)
 create mode 100644 arch/loongarch/cpu/cpu.c
 create mode 100644 arch/loongarch/cpu/smp_secondary.S
 create mode 100644 arch/loongarch/cpu/start.S
arch/loongarch/cpu/Makefile +4 -0
index 3dbed94cc624..d3c38a16d057 100644
@@ -3,3 +3,7 @@
 # Copyright (C) 2024 Jiaxun yang <[email protected]>
 #
 
+extra-y = start.o
+
+obj-y += cpu.o
+obj-y += smp_secondary.o
arch/loongarch/cpu/cpu.c new file +17 -0
new file mode 100644
index 000000000000..38005d576458
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ * Copyright (C) 2026 Yao Zi <[email protected]>
+ */
+
+#include <hang.h>
+#include <stdio.h>
+
+#if !CONFIG_IS_ENABLED(SYSRESET)
+void reset_cpu(void)
+{
+	printf("reset not supported yet\n");
+	hang();
+}
+#endif
arch/loongarch/cpu/smp_secondary.S new file +55 -0

55 lines not on this page · raw message

arch/loongarch/cpu/start.S new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#9 lore raw

PATCH v7 9/20 LoongArch: Exception handling

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Add exception entry assembly code, import stackframe.h from Linux, provide debug prints when exception happens.

---

Changed from v6
- Guard call to show_regs() with a plain branch instead of #if

No changes since v4

Changed from v3
- Drop unused and unimplemented set_resume()

Changed from v2
- Guard show_regs at the callsite
- Provide a macro for $fp offset in pt_regs.regs
- Add help text for SHOW_REGS, make it enabled by default

No functional changes since v1

---
 arch/loongarch/Kconfig                  |  19 +++
 arch/loongarch/cpu/Makefile             |   2 +-
 arch/loongarch/cpu/genex.S              |  21 +++
 arch/loongarch/include/asm/stackframe.h | 175 ++++++++++++++++++++++
 arch/loongarch/lib/interrupts.c         | 184 ++++++++++++++++++++++++
 5 files changed, 400 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/cpu/genex.S
 create mode 100644 arch/loongarch/include/asm/stackframe.h
 create mode 100644 arch/loongarch/lib/interrupts.c
arch/loongarch/Kconfig +19 -0
index 4e8e9d4ee88b..ac4be2c837c0 100644
@@ -30,6 +30,25 @@ config DMA_ADDR_T_64BIT
 	bool
 	default y if 64BIT
 
+config SHOW_REGS
+	bool "Show registers on unhandled exception"
+	default y
+	help
+	  By default only the program counter and the return address register
+	  are shown in crash dumps. Enable this symbol to show all registers
+	  in main U-Boot.
+
+config FRAMEPOINTER
+	bool "Build with frame pointer for stack unwinding"
+	default y
+	help
+	  Choose this option to use the framepointer so the stack can be
+	  unwound if needed. This is useful for tracing where faults came
+	  from as the source may be several functions back.
+
+	  If you say Y here, the code size will increase due to having to
+	  store the fp.
+
 config STACK_SIZE_SHIFT
 	int
 	default 14
arch/loongarch/cpu/Makefile +1 -1
index d3c38a16d057..6b3dd7ad7d69 100644
@@ -6,4 +6,4 @@
 extra-y = start.o
 
 obj-y += cpu.o
-obj-y += smp_secondary.o
+obj-y += smp_secondary.o genex.o
arch/loongarch/cpu/genex.S new file +21 -0
new file mode 100644
index 000000000000..18d183a352b9
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Exception entry for LoongArch CPU
+ *
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+#include <asm/loongarch.h>
+#include <asm/stackframe.h>
+
+.align 12
+ENTRY(exception_entry)
+	BACKUP_T0T1
+	SAVE_ALL
+	move		a0, sp
+	la.pcrel	t0, do_exceptions
+	jirl		ra, t0, 0
+	RESTORE_ALL_AND_RET
+END(exception_entry)
arch/loongarch/include/asm/stackframe.h new file +175 -0

175 lines not on this page · raw message

arch/loongarch/lib/interrupts.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#10 lore raw

PATCH v7 10/20 LoongArch: Boot Image bits

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Implement loading and booting functions for LoongArch standard kernel image as per spec.

LoongArch kernel do expect us to fake a efi systemtable for passing fdt to kernel, we don't need to implement any EFI functions for kernel because it won't look into anything beside devicetree from that table if we tell kernel we are not efi compatible by setting a0 boot argument to zero.

---

Changes since v6
- Replace uint32_t/uint64_t with u32/u64 in definition of linux_image_h

No changes since v4

Changed from v3
- Do not include bootm.o twice for compilation in arch/loongarch/lib/Makefile
- Declare LoongArch-specific, LIB_BOOTM's dependency on CRC32 in boot/Kconfig instead of arch/loongarch/Kconfig
- Store the fake EFI system table in gd->bd->bi_boot_params instead of images->kbd->bi_boot_params, since the latter isn't automatically initialized

Changed from v2
- Use LIB_BOOT[MI] instead of CMD_BOOT[MI] in arch/loongarch/lib/Makefile
- Drop now redundant definition of board_quiesce_devices() stub
- Replace announce_and_cleanup() with generic bootm_final() in bootm.c
- Remove usage of USB_DEVICE
- Clean up header includes in lib/bootm.c
- Use u-boot/crc.h for CRC32 in lib/bootm.c, select CRC32 for LIB_BOOTM on LoongArch
- Don't align the generated systab to 64K
- Correctly pass cmdline_start to Linux kernel
- Hang if either OF_LIBFDT is disabled or images->ft_len is zero in boot_jump_linux()
- Tidy up comments for linux_image_h
- Explain the kernel bug fixed by clearing the highest 4bits of kernel entry address.
- Clear only the highest 4bits of kernel entry address, instead of the highest 16bits with TO_PHYS(), to ensure compatibility with future kernels supporting wider address ranges.

Changed from v1
- Remove now unused arch_lmb_reserve()

---
 arch/loongarch/lib/Makefile |   2 +
 arch/loongarch/lib/bootm.c  | 129 ++++++++++++++++++++++++++++++++++++
 arch/loongarch/lib/image.c  |  77 +++++++++++++++++++++
 boot/Kconfig                |   1 +
 cmd/Kconfig                 |   2 +-
 5 files changed, 210 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/lib/bootm.c
 create mode 100644 arch/loongarch/lib/image.c
arch/loongarch/lib/Makefile +2 -0
index 3c17b9cd85af..9eb9662066a5 100644
@@ -3,6 +3,8 @@
 # Copyright (C) 2024 Jiaxun yang <[email protected]>
 #
 
+obj-$(CONFIG_$(PHASE_)LIB_BOOTM) += bootm.o
+obj-$(CONFIG_$(PHASE_)LIB_BOOTI) += image.o
 obj-$(CONFIG_CMD_GO) += boot.o
 obj-y	+= cache.o
 obj-y	+= interrupts.o
arch/loongarch/lib/bootm.c new file +129 -0

129 lines not on this page · raw message

arch/loongarch/lib/image.c new file +77 -0

77 lines not on this page · raw message

boot/Kconfig +1 -0
index 8fcdc36c9318..7dcda1d17ea5 100644
@@ -38,6 +38,7 @@ config LIB_BOOTI
 
 config LIB_BOOTM
 	bool
+	select CRC32 if LOONGARCH
 
 config LIB_BOOTZ
 	bool
cmd/Kconfig +1 -1
index ace0b2e9c80e..3b4cc5a5cb9e 100644
@@ -405,7 +405,7 @@ config CMD_BOOTZ
 
 config CMD_BOOTI
 	bool "booti"
-	depends on ARM64 || RISCV || SANDBOX
+	depends on ARM64 || LOONGARCH || RISCV || SANDBOX
 	depends on LMB
 	default y
 	select LIB_BOOTI
Signature · 1 line
2.56.0
author
#11 lore raw

PATCH v7 11/20 LoongArch: Generic platform type

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Provide a generic platform with some common routines expected to be implemented at CPU level.

---

No changes since v5

Changed from v4
- Avoid using LoongArch-specific cache_flush() helpers in cleanup_before_linux()

No changes since v3

Changed from v2
- Select CPU if DISPLAY_INFO is enabled
- Reword the commit message, re-titled as "LoongArch: Generic platform type"
- Fix the comment for cleanup_before_linux() to note that cache isn't disabled, captilize Linux
- Drop unused DECLARE_GLOBAL_DATA_PTR from cpu/generic/dram.c

No changes since v1

---
 arch/loongarch/Kconfig              |  1 +
 arch/loongarch/cpu/generic/Kconfig  | 13 +++++++++++++
 arch/loongarch/cpu/generic/Makefile |  7 +++++++
 arch/loongarch/cpu/generic/cpu.c    | 23 +++++++++++++++++++++++
 arch/loongarch/cpu/generic/dram.c   | 16 ++++++++++++++++
 5 files changed, 60 insertions(+)
 create mode 100644 arch/loongarch/cpu/generic/Kconfig
 create mode 100644 arch/loongarch/cpu/generic/Makefile
 create mode 100644 arch/loongarch/cpu/generic/cpu.c
 create mode 100644 arch/loongarch/cpu/generic/dram.c
arch/loongarch/Kconfig +1 -0
index ac4be2c837c0..1f376ca291c1 100644
@@ -12,6 +12,7 @@ endchoice
 # board-specific options below
 
 # platform-specific options below
+source "arch/loongarch/cpu/generic/Kconfig"
 
 # architecture-specific options below
 choice
arch/loongarch/cpu/generic/Kconfig new file +13 -0
new file mode 100644
index 000000000000..5fcc2156906d
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
+config GENERIC_LOONGARCH
+	bool
+	select SYS_CACHE_SHIFT_6
+	imply CPU
+	imply CPU_LOONGARCH
+	imply LOONGARCH_TIMER
+	imply CMD_CPU
+	select CPU if DISPLAY_CPUINFO
arch/loongarch/cpu/generic/Makefile new file +7 -0
new file mode 100644
index 000000000000..c98d63b4f831
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
+obj-y += dram.o
+obj-y += cpu.o
arch/loongarch/cpu/generic/cpu.c new file +23 -0
new file mode 100644
index 000000000000..eb11f1349856
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <cpu_func.h>
+#include <irq_func.h>
+
+/*
+ * cleanup_before_linux() is called just before we call Linux, it prepares the
+ * processor for linux
+ *
+ * We disable interrupts and flush caches.
+ */
+int cleanup_before_linux(void)
+{
+	disable_interrupts();
+
+	flush_dcache_all();
+	invalidate_icache_all();
+
+	return 0;
+}
arch/loongarch/cpu/generic/dram.c new file +16 -0
new file mode 100644
index 000000000000..26cabc4b3ccf
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <fdtdec.h>
+
+int dram_init(void)
+{
+	return fdtdec_setup_mem_size_base_lowest();
+}
+
+int dram_init_banksize(void)
+{
+	return fdtdec_setup_memory_banksize();
+}
Signature · 1 line
2.56.0
author
#12 lore raw

PATCH v7 12/20 board_f: Add LoongArch path of mon_len calculation

In reply to Yao Zi

gd->mon_len is calculated in the same way as SH and RISC-V.

---

New patch since v4, to ensure U-Boot correctly reserves its own space for relocation, even without SYS_MONITOR_BASE enabled through MTD_NOR_FLASH.

---
 common/board_f.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/board_f.c +1 -1
index 2f26b4644cae..22650d9d003c 100644
@@ -292,7 +292,7 @@ static int setup_mon_len(void)
 	gd->mon_len = (ulong)_end - (ulong)_init;
 #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
 	gd->mon_len = CONFIG_SYS_MONITOR_LEN;
-#elif defined(CONFIG_SH) || defined(CONFIG_RISCV)
+#elif defined(CONFIG_SH) || defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
 	gd->mon_len = (ulong)(__bss_end) - (ulong)(_start);
 #elif defined(CONFIG_SYS_MONITOR_BASE)
 	/* TODO: use (ulong)__bss_end - (ulong)__text_start; ? */
Signature · 1 line
2.56.0
Acked-by on v7 12/20
#13 lore raw

Re: [PATCH v7 12/20] board_f: Add LoongArch path of mon_len calculation

Quoted 11 lines · Yao Zi

gd->mon_len is calculated in the same way as SH and RISC-V.

---

New patch since v4, to ensure U-Boot correctly reserves its own space for relocation, even without SYS_MONITOR_BASE enabled through MTD_NOR_FLASH.
---

Quoted 19 lines
 common/board_f.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/board_f.c +1 -2
index 2f26b4644cae..22650d9d003c 100644
@@ -292,7 +292,7 @@ static int setup_mon_len(void)
        gd->mon_len = (ulong)_end - (ulong)_init;
 #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
        gd->mon_len = CONFIG_SYS_MONITOR_LEN;
-#elif defined(CONFIG_SH) || defined(CONFIG_RISCV)
+#elif defined(CONFIG_SH) || defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
        gd->mon_len = (ulong)(__bss_end) - (ulong)(_start);
 #elif defined(CONFIG_SYS_MONITOR_BASE)
        /* TODO: use (ulong)__bss_end - (ulong)__text_start; ? */
--

2.56.0

author
#14 lore raw

PATCH v7 13/20 cpu: Add loongarch_cpu driver

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Implement a driver for LoongArch CPU solely for gathering some information bits from FDT and CPU level config registers.

---

No changes since v5

Changed from v4
- Explicitly clear info->cpu_freq before passing its address to dev_read_u32()
- Temporarily store return value of clk_get_rate() in an ulong() to avoid truncation

No changes since v3

Changed from v2
- Fall through to devicetree path in loongarch_cpu_get_desc() when probing with CPUCFG gets no meaningful results, instead of handling such cases with goto
- Replace open-coded copying loop with memcpy() in loongarch_cpu_get_desc()
- Correctly return -ENOSPC when snprintf() truncates the string in loongarch_cpu_get_desc()

Changed from v1
- Drop unnecessary check against -ENOTSUPP clk_enable()'s return value in loongarch_cpu_probe()

---
 drivers/cpu/Kconfig         |   6 ++
 drivers/cpu/Makefile        |   1 +
 drivers/cpu/loongarch_cpu.c | 145 ++++++++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+)
 create mode 100644 drivers/cpu/loongarch_cpu.c
drivers/cpu/Kconfig +6 -0
index c805c0bbfa19..9fd43ff88fde 100644
@@ -13,6 +13,12 @@ config CPU_IMX
 	help
 	  Support CPU cores for SoCs of the i.MX series.
 
+config CPU_LOONGARCH
+	bool "Enable LoongArch CPU driver"
+	depends on CPU && LOONGARCH
+	help
+	  Support CPU cores for LoongArch processors.
+
 config CPU_MPC83XX
 	bool "Enable MPC83xx CPU driver"
 	depends on CPU && MPC83xx
drivers/cpu/Makefile +1 -0
index eaf494706e2c..d9c0db74dd5a 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_ARCH_AT91) += at91_cpu.o
 obj-$(CONFIG_ARCH_MEDIATEK) += mtk_cpu.o
 obj-$(CONFIG_CPU_ARMV8) += armv8_cpu.o
 obj-$(CONFIG_CPU_IMX) += imx8_cpu.o
+obj-$(CONFIG_CPU_LOONGARCH) += loongarch_cpu.o
 obj-$(CONFIG_CPU_MPC83XX) += mpc83xx_cpu.o
 obj-$(CONFIG_CPU_RISCV) += riscv_cpu.o
 obj-$(CONFIG_CPU_MICROBLAZE) += microblaze_cpu.o
drivers/cpu/loongarch_cpu.c new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#15 lore raw

PATCH v7 14/20 timer: Add loongarch_timer driver

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Implement a timer driver for LoongArch architecture timer. It's synced in hardware for every core in a system, and frequency information can be gathered from CPUCFG instruction.

It is not described in fdt, thus a DRVINFO is declared for it. During probing, stability of the timer frequency is checked through CPUCFG, and the probing would fail if it isn't constant.

---

No changes since v5

Changed from v4:
- Convert base_freq to unsigned long before calculating timer frequency to avoid possible overflow

No changes since v3

Changed from v2
- include div64.h for definition of lldiv()
- Rename loongarch_timer_bind() to loongarch_timer_probe(), to correctly reflect that it's used in the probe() callback
- Bail out with -ENODEV when missing LLFTP support, which means there isn't a timer with stable frequency

No changes from v1

---
 drivers/timer/Kconfig           |   8 ++
 drivers/timer/Makefile          |   1 +
 drivers/timer/loongarch_timer.c | 132 ++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
 create mode 100644 drivers/timer/loongarch_timer.c
drivers/timer/Kconfig +8 -0
index 2b9123020533..12d93274f0f6 100644
@@ -353,4 +353,12 @@ config GOLDFISH_TIMER
 	  It uses the Goldfish RTC hardware to provide a nanosecond-resolution
 	  timer, commonly found in QEMU virt machines.
 
+config LOONGARCH_TIMER
+	bool "LoongArch CPU timer support"
+	depends on TIMER
+	depends on LOONGARCH
+	help
+	  Select this to enable support for the timer found on
+	  LoongArch CPUs.
+
 endmenu
drivers/timer/Makefile +1 -0
index d8b3f2b65d4c..0b70b915bfeb 100644
@@ -37,3 +37,4 @@ obj-$(CONFIG_IMX_GPT_TIMER)	+= imx-gpt-timer.o
 obj-$(CONFIG_XILINX_TIMER)	+= xilinx-timer.o
 obj-$(CONFIG_STARFIVE_TIMER)	+= starfive-timer.o
 obj-$(CONFIG_GOLDFISH_TIMER)	+= goldfish_timer.o
+obj-$(CONFIG_LOONGARCH_TIMER)	+= loongarch_timer.o
drivers/timer/loongarch_timer.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.56.0
author
#16 lore raw

PATCH v7 15/20 board: emulation: Add qemu-loongarch

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Yet another generic QEMU VIRT machine.

LoongArch QEMU splits the main RAM into two parts: the first 512M is placed at 0x0, and the rest is placed at 0x8000000. FDT locates at 0x100000. BIOS is mapped to the first flash mapped to 0x1c000000, where U-Boot starts execution.

---

No changes since v4

Changed from v3
- Remove the dummy empty devicetree
- Fix the typo when limiting top usable RAM address top (0x1000_0000 -> 0x1_0000_0000)

Changed from v2
- Add board-level documentation for qemu-loongarch
- Mention the memory layout and booting process in commit message
- Move TARGET_QEMU_LOONGARCH_VIRT to board/emulation/qemu-loongarch/Kconfig
- Drop duplicated selection of BOARD_LATE_INIT in TARGET_QEMU_LOONGARCH_VIRT
- Explain why RAM is limited to the lowest 4GiB
- Drop unused board_init()
- Rework comment in board_fdt_blob_setup()
- Define FDT address, 0x100000, as a meaningful constant
- Clarify how CFG_SYS_INIT_RAM_[ADDR,SIZE] are chosen
- Add myself to MAINTAINERS

Changed from v1
- Remove dynamic setup for environment variables

---
 arch/loongarch/Kconfig                        |  4 ++
 board/emulation/qemu-loongarch/Kconfig        | 68 +++++++++++++++++++
 board/emulation/qemu-loongarch/MAINTAINERS    |  9 +++
 board/emulation/qemu-loongarch/Makefile       |  6 ++
 .../emulation/qemu-loongarch/qemu-loongarch.c | 67 ++++++++++++++++++
 .../qemu-loongarch/qemu-loongarch.env         |  5 ++
 configs/qemu-loongarch64_defconfig            | 35 ++++++++++
 doc/board/emulation/index.rst                 |  1 +
 doc/board/emulation/qemu-loongarch.rst        | 39 +++++++++++
 include/configs/qemu-loongarch.h              | 17 +++++
 10 files changed, 251 insertions(+)
 create mode 100644 board/emulation/qemu-loongarch/Kconfig
 create mode 100644 board/emulation/qemu-loongarch/MAINTAINERS
 create mode 100644 board/emulation/qemu-loongarch/Makefile
 create mode 100644 board/emulation/qemu-loongarch/qemu-loongarch.c
 create mode 100644 board/emulation/qemu-loongarch/qemu-loongarch.env
 create mode 100644 configs/qemu-loongarch64_defconfig
 create mode 100644 doc/board/emulation/qemu-loongarch.rst
 create mode 100644 include/configs/qemu-loongarch.h
arch/loongarch/Kconfig +4 -0
index 1f376ca291c1..a53d26a59497 100644
@@ -7,9 +7,13 @@ config SYS_ARCH
 choice
 	prompt "Target select"
 
+config TARGET_QEMU_LOONGARCH_VIRT
+	bool "Support QEMU Virt Board"
+
 endchoice
 
 # board-specific options below
+source "board/emulation/qemu-loongarch/Kconfig"
 
 # platform-specific options below
 source "arch/loongarch/cpu/generic/Kconfig"
board/emulation/qemu-loongarch/Kconfig new file +68 -0

68 lines not on this page · raw message

board/emulation/qemu-loongarch/MAINTAINERS new file +9 -0
new file mode 100644
index 000000000000..cd9636d6dc9b
@@ -0,0 +1,9 @@
+QEMU LOONGARCH 'VIRT' BOARD
+M:	Jiaxun Yang <[email protected]>
+M:	Yao Zi <[email protected]>
+S:	Maintained
+F:	board/emulation/qemu-loongarch/
+F:	board/emulation/common/
+F:	include/configs/qemu-loongarch.h
+F:	configs/qemu-loongarch64_defconfig
+F:	doc/board/emulation/qemu-loongarch.rst
board/emulation/qemu-loongarch/Makefile new file +6 -0
new file mode 100644
index 000000000000..a928b90780e9
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2024 Jiaxun yang <[email protected]>
+#
+
+obj-y	+= qemu-loongarch.o
board/emulation/qemu-loongarch/qemu-loongarch.c new file +67 -0

67 lines not on this page · raw message

board/emulation/qemu-loongarch/qemu-loongarch.env new file +5 -0
new file mode 100644
index 000000000000..7655f26d8d6b
@@ -0,0 +1,5 @@
+stdin=serial,usbkbd
+stdout=serial,vidconsole
+stderr=serial,vidconsole
+
+boot_targets=qfw usb scsi virtio nvme dhcp
configs/qemu-loongarch64_defconfig new file +35 -0
new file mode 100644
index 000000000000..5d5ae44f8e56
@@ -0,0 +1,35 @@
+CONFIG_LOONGARCH=y
+CONFIG_SYS_MALLOC_LEN=0x800000
+CONFIG_SYS_MALLOC_F_LEN=0x4000
+CONFIG_NR_DRAM_BANKS=8
+CONFIG_ENV_SIZE=0x20000
+CONFIG_DM_RESET=y
+CONFIG_SYS_LOAD_ADDR=0x02000000
+CONFIG_SHOW_REGS=y
+# CONFIG_OF_BOARD_FIXUP is not set
+CONFIG_SYS_PCI_64BIT=y
+CONFIG_FIT=y
+CONFIG_BOOTSTD_FULL=y
+CONFIG_SYS_BOOTM_LEN=0x4000000
+CONFIG_USE_BOOTARGS=y
+CONFIG_BOOTARGS_SUBST=y
+CONFIG_DISPLAY_CPUINFO=y
+CONFIG_DISPLAY_BOARDINFO=y
+# CONFIG_CMD_MII is not set
+CONFIG_CMD_2048=y
+CONFIG_CMD_RTC=y
+CONFIG_CMD_TIME=y
+CONFIG_CMD_SYSBOOT=y
+CONFIG_CMD_QFW=y
+CONFIG_PARTITION_TYPE_GUID=y
+CONFIG_SYS_RELOC_GD_ENV_ADDR=y
+CONFIG_BLKMAP=y
+CONFIG_EFI_MEDIA=y
+CONFIG_DM_MMC=y
+CONFIG_DM_MTD=y
+CONFIG_FLASH_SHOW_PROGRESS=0
+CONFIG_SYS_MAX_FLASH_BANKS=2
+CONFIG_PCI_REGION_MULTI_ENTRY=y
+CONFIG_DM_REBOOT_MODE=y
+CONFIG_RESET_SYSCON=y
+CONFIG_HEXDUMP=y
doc/board/emulation/index.rst +1 -0
index 5d0dd83d8aca..a142cd3360ac 100644
@@ -11,6 +11,7 @@ Emulation
    network
    video
    qemu-arm
+   qemu-loongarch
    qemu-mips
    qemu-ppce500
    qemu-riscv
doc/board/emulation/qemu-loongarch.rst new file +39 -0
new file mode 100644
index 000000000000..0d97dc7df797
@@ -0,0 +1,39 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+.. Copyright (C) 2026 Yao Zi <[email protected]>
+
+QEMU LoongArch
+==============
+
+QEMU for LoongArch supports a special 'virt' machine designed for emulation
+and virtualization purpose. This document describes how to run U-Boot under it.
+Only 64-bit LoongArch is supported at this time.
+
+The 'virt' platform provides the following as the basic functionality:
+
+    - A freely configurable amount of CPU cores
+    - U-Boot loaded and executing in the emulated flash, mapped to 0x1c000000.
+    - A generated device tree blob placed at 0x100000.
+    - A freely configurable amount of RAM, described by the DTB, starting at
+      0x0. RAM above 512MiB is placed at 0x80000000.
+    - A NS16550A serial port, discoverable via the DTB.
+    - An in-core constant frequency timer.
+    - syscon-reboot and syscon-poweroff for powering off and rebooting the
+      system.
+    - A generic ECAM-based PCI host controller, discoverable via the DTB.
+
+Additionally, a number of optional peripherals can be added to the PCI bus.
+
+See :doc:`../../develop/devicetree/dt_qemu` for information on how to see the
+devicetree actually generated by QEMU.
+
+Building and Running
+--------------------
+
+Set the CROSS_COMPILE environment variable as usual, and run::
+
+    make qemu-loongarch64_defconfig
+    make
+
+The minimal QEMU command line to get U-Boot up and running is::
+
+    qemu-system-loongarch64 -M virt -nographic -bios u-boot.bin
include/configs/qemu-loongarch.h new file +17 -0
new file mode 100644
index 000000000000..7103dc1b02ef
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#ifndef __CONFIG_H
+#define __CONFIG_H
+
+/*
+ * LoongArch QEMU always places a part of the RAM at 0x0. These options are only
+ * intended to specify the initial stack pointer address, and can be replaced
+ * with CUSTOM_SYS_INIT_SP_ADDR when it's supported in the future.
+ */
+#define CFG_SYS_INIT_RAM_ADDR	0x0
+#define CFG_SYS_INIT_RAM_SIZE	0x80000
+
+#endif
Signature · 1 line
2.56.0
author
#17 lore raw

PATCH v7 16/20 efi: LoongArch: Define LoongArch bits everywhere

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Add PE machine type, removable boot image name, vendor class identifier, client architecture identifier, relative ELF relocation, and undefined instruction for LoongArch. They are all coming from UEFI specification, PE-COFF specification, or IANA websites.

---

No changes since v3

Changed from v2
- Describe what has been added in commit message
- Drop extra paretheses in elif condition in config_distro_bootcmd.h
- Define IMAGE_REL_BASED_LOONGARCH32_MARK_LA as well in include/pe.h

Changed from v1
- Reword commit message, replacing "PE spec" with "PE-COFF specification"
- Replace obsolete __loongarch64 macro with __loongarch__ and __loongarch_grlen
- Use a true undefined instruction in efi_selftest_miniapp_exception.c
- Define HOST_BOOTEFI_NAME and HOST_PXE_ARCH
- Introduce definition for IMAGE_REL_BASEd_LOONGARCH64_MARK_LA

---
 include/asm-generic/pe.h                          | 2 ++
 include/config_distro_bootcmd.h                   | 5 +++++
 include/pe.h                                      | 2 ++
 lib/efi_loader/efi_helper.c                       | 4 ++++
 lib/efi_loader/efi_image_loader.c                 | 7 +++++++
 lib/efi_loader/efi_runtime.c                      | 4 ++++
 lib/efi_selftest/efi_selftest_miniapp_exception.c | 3 +++
 7 files changed, 27 insertions(+)
include/asm-generic/pe.h +2 -0
index cd5b6ad62bf0..fb93f2621715 100644
@@ -38,6 +38,8 @@
 #define IMAGE_FILE_MACHINE_ARM64		0xaa64
 #define IMAGE_FILE_MACHINE_RISCV32		0x5032
 #define IMAGE_FILE_MACHINE_RISCV64		0x5064
+#define IMAGE_FILE_MACHINE_LOONGARCH32	0x6232
+#define IMAGE_FILE_MACHINE_LOONGARCH64	0x6264
 
 /* Header magic constants */
 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC		0x010b
include/config_distro_bootcmd.h +5 -0
index 8d6f80a0ce5c..0eb6bd742b9f 100644
@@ -118,6 +118,8 @@
 #define BOOTEFI_NAME "bootriscv32.efi"
 #elif defined(CONFIG_ARCH_RV64I)
 #define BOOTEFI_NAME "bootriscv64.efi"
+#elif defined(CONFIG_ARCH_LA64)
+#define BOOTEFI_NAME "bootloongarch64.efi"
 #endif
 #endif
 
@@ -371,6 +373,9 @@
 #elif defined(CONFIG_ARCH_RV64I) || ((defined(__riscv) && __riscv_xlen == 64))
 #define BOOTENV_EFI_PXE_ARCH "0x1b"
 #define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00027:UNDI:003000"
+#elif defined(CONFIG_ARCH_LA64) || (defined(__loongarch__) && __loongarch_grlen == 64)
+#define BOOTENV_EFI_PXE_ARCH "0x27"
+#define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00039:UNDI:003000"
 #elif defined(CONFIG_SANDBOX)
 # error "sandbox EFI support is only supported on ARM and x86"
 #else
include/pe.h +2 -0
index 086f2b860e99..eac9eb25ce47 100644
@@ -194,6 +194,8 @@ typedef struct _IMAGE_RELOCATION
 #define IMAGE_REL_BASED_THUMB_MOV32             7 /* yes, 7 too */
 #define IMAGE_REL_BASED_RISCV_LOW12I		7 /* yes, 7 too */
 #define IMAGE_REL_BASED_RISCV_LOW12S		8
+#define IMAGE_REL_BASED_LOONGARCH32_MARK_LA	8 /* yes, 8 too */
+#define IMAGE_REL_BASED_LOONGARCH64_MARK_LA	8 /* yes, 8 too */
 #define IMAGE_REL_BASED_MIPS_JMPADDR16          9
 #define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
 #define IMAGE_REL_BASED_DIR64                   10
lib/efi_loader/efi_helper.c +4 -0
index 44b806aadc4f..bc48e265b901 100644
@@ -62,6 +62,8 @@
 #define BOOTEFI_NAME "BOOTRISCV32.EFI"
 #elif defined(CONFIG_ARCH_RV64I)
 #define BOOTEFI_NAME "BOOTRISCV64.EFI"
+#elif defined(CONFIG_ARCH_LA64)
+#define BOOTEFI_NAME "BOOTLOONGARCH64.EFI"
 #else
 #error Unsupported UEFI architecture
 #endif
@@ -94,6 +96,8 @@ int efi_get_pxe_arch(void)
 		return 0x19;
 	else if (IS_ENABLED(CONFIG_ARCH_RV64I))
 		return 0x1b;
+	else if (IS_ENABLED(CONFIG_ARCH_LA64))
+		return 0x27;
 
 	return -EINVAL;
 }
lib/efi_loader/efi_image_loader.c +7 -0
index f9a2d2df405d..bed5f1817266 100644
@@ -51,6 +51,13 @@ static int machines[] = {
 #if defined(__riscv) && (__riscv_xlen == 64)
 	IMAGE_FILE_MACHINE_RISCV64,
 #endif
+
+#if defined(__loongarch__) && __loongarch_grlen == 64
+	IMAGE_FILE_MACHINE_LOONGARCH64,
+#endif
+#if defined(__loongarch__) && __loongarch_grlen == 32
+	IMAGE_FILE_MACHINE_LOONGARCH32,
+#endif
 	0 };
 
 /**
lib/efi_loader/efi_runtime.c +4 -0
index 73d4097464c0..de50d72d634a 100644
@@ -78,6 +78,10 @@ struct dyn_sym {
 #else
 #error unknown riscv target
 #endif
+#elif defined(__loongarch__)
+#define R_RELATIVE	R_LARCH_RELATIVE
+#define R_MASK		0xffffffffULL
+#define IS_RELA		1
 #else
 #error Need to add relocation awareness
 #endif
lib/efi_selftest/efi_selftest_miniapp_exception.c +3 -0
index f668cdac4ab2..648206c17098 100644
@@ -35,6 +35,9 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 	asm volatile (".word 0xffffffff\n");
 #elif defined(CONFIG_X86)
 	asm volatile (".word 0xffff\n");
+#elif defined(CONFIG_LOONGARCH)
+	/* ud 31 */
+	asm volatile (".word 0x386007ff\n");
 #elif defined(CONFIG_SANDBOX)
 #if (HOST_ARCH == HOST_ARCH_ARM || HOST_ARCH == HOST_ARCH_AARCH64)
 	asm volatile (".word 0xe7f7defb\n");
Signature · 1 line
2.56.0
Acked-by on v7 16/20
#18 lore raw

Re: [PATCH v7 16/20] efi: LoongArch: Define LoongArch bits everywhere

Quoted 8 lines · Yao Zi

From: Jiaxun Yang <[email protected]>

Add PE machine type, removable boot image name, vendor class identifier, client architecture identifier, relative ELF relocation, and undefined instruction for LoongArch. They are all coming from UEFI specification, PE-COFF specification, or IANA websites.

Quoted 4 lines

---

Quoted 146 lines

No changes since v3

Changed from v2
- Describe what has been added in commit message
- Drop extra paretheses in elif condition in config_distro_bootcmd.h
- Define IMAGE_REL_BASED_LOONGARCH32_MARK_LA as well in include/pe.h

Changed from v1
- Reword commit message, replacing "PE spec" with "PE-COFF specification"
- Replace obsolete __loongarch64 macro with __loongarch__ and __loongarch_grlen
- Use a true undefined instruction in efi_selftest_miniapp_exception.c
- Define HOST_BOOTEFI_NAME and HOST_PXE_ARCH
- Introduce definition for IMAGE_REL_BASEd_LOONGARCH64_MARK_LA

---
 include/asm-generic/pe.h                          | 2 ++
 include/config_distro_bootcmd.h                   | 5 +++++
 include/pe.h                                      | 2 ++
 lib/efi_loader/efi_helper.c                       | 4 ++++
 lib/efi_loader/efi_image_loader.c                 | 7 +++++++
 lib/efi_loader/efi_runtime.c                      | 4 ++++
 lib/efi_selftest/efi_selftest_miniapp_exception.c | 3 +++
 7 files changed, 27 insertions(+)
include/asm-generic/pe.h +2 -0
index cd5b6ad62bf0..fb93f2621715 100644
@@ -38,6 +38,8 @@
 #define IMAGE_FILE_MACHINE_ARM64               0xaa64
 #define IMAGE_FILE_MACHINE_RISCV32             0x5032
 #define IMAGE_FILE_MACHINE_RISCV64             0x5064
+#define IMAGE_FILE_MACHINE_LOONGARCH32 0x6232
+#define IMAGE_FILE_MACHINE_LOONGARCH64 0x6264
 
 /* Header magic constants */
 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC          0x010b
include/config_distro_bootcmd.h +5 -0
index 8d6f80a0ce5c..0eb6bd742b9f 100644
@@ -118,6 +118,8 @@
 #define BOOTEFI_NAME "bootriscv32.efi"
 #elif defined(CONFIG_ARCH_RV64I)
 #define BOOTEFI_NAME "bootriscv64.efi"
+#elif defined(CONFIG_ARCH_LA64)
+#define BOOTEFI_NAME "bootloongarch64.efi"
 #endif
 #endif
 
@@ -371,6 +373,9 @@
 #elif defined(CONFIG_ARCH_RV64I) || ((defined(__riscv) && __riscv_xlen == 64))
 #define BOOTENV_EFI_PXE_ARCH "0x1b"
 #define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00027:UNDI:003000"
+#elif defined(CONFIG_ARCH_LA64) || (defined(__loongarch__) && __loongarch_grlen == 64)
+#define BOOTENV_EFI_PXE_ARCH "0x27"
+#define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00039:UNDI:003000"
 #elif defined(CONFIG_SANDBOX)
 # error "sandbox EFI support is only supported on ARM and x86"
 #else
include/pe.h +2 -0
index 086f2b860e99..eac9eb25ce47 100644
@@ -194,6 +194,8 @@ typedef struct _IMAGE_RELOCATION
 #define IMAGE_REL_BASED_THUMB_MOV32             7 /* yes, 7 too */
 #define IMAGE_REL_BASED_RISCV_LOW12I           7 /* yes, 7 too */
 #define IMAGE_REL_BASED_RISCV_LOW12S           8
+#define IMAGE_REL_BASED_LOONGARCH32_MARK_LA    8 /* yes, 8 too */
+#define IMAGE_REL_BASED_LOONGARCH64_MARK_LA    8 /* yes, 8 too */
 #define IMAGE_REL_BASED_MIPS_JMPADDR16          9
 #define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
 #define IMAGE_REL_BASED_DIR64                   10
lib/efi_loader/efi_helper.c +4 -0
index 44b806aadc4f..bc48e265b901 100644
@@ -62,6 +62,8 @@
 #define BOOTEFI_NAME "BOOTRISCV32.EFI"
 #elif defined(CONFIG_ARCH_RV64I)
 #define BOOTEFI_NAME "BOOTRISCV64.EFI"
+#elif defined(CONFIG_ARCH_LA64)
+#define BOOTEFI_NAME "BOOTLOONGARCH64.EFI"
 #else
 #error Unsupported UEFI architecture
 #endif
@@ -94,6 +96,8 @@ int efi_get_pxe_arch(void)
                return 0x19;
        else if (IS_ENABLED(CONFIG_ARCH_RV64I))
                return 0x1b;
+       else if (IS_ENABLED(CONFIG_ARCH_LA64))
+               return 0x27;
 
        return -EINVAL;
 }
lib/efi_loader/efi_image_loader.c +7 -0
index f9a2d2df405d..bed5f1817266 100644
@@ -51,6 +51,13 @@ static int machines[] = {
 #if defined(__riscv) && (__riscv_xlen == 64)
        IMAGE_FILE_MACHINE_RISCV64,
 #endif
+
+#if defined(__loongarch__) && __loongarch_grlen == 64
+       IMAGE_FILE_MACHINE_LOONGARCH64,
+#endif
+#if defined(__loongarch__) && __loongarch_grlen == 32
+       IMAGE_FILE_MACHINE_LOONGARCH32,
+#endif
        0 };
 
 /**
lib/efi_loader/efi_runtime.c +4 -0
index 73d4097464c0..de50d72d634a 100644
@@ -78,6 +78,10 @@ struct dyn_sym {
 #else
 #error unknown riscv target
 #endif
+#elif defined(__loongarch__)
+#define R_RELATIVE     R_LARCH_RELATIVE
+#define R_MASK         0xffffffffULL
+#define IS_RELA                1
 #else
 #error Need to add relocation awareness
 #endif
lib/efi_selftest/efi_selftest_miniapp_exception.c +3 -1
index f668cdac4ab2..648206c17098 100644
@@ -35,6 +35,9 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
        asm volatile (".word 0xffffffff\n");
 #elif defined(CONFIG_X86)
        asm volatile (".word 0xffff\n");
+#elif defined(CONFIG_LOONGARCH)
+       /* ud 31 */
+       asm volatile (".word 0x386007ff\n");
 #elif defined(CONFIG_SANDBOX)
 #if (HOST_ARCH == HOST_ARCH_ARM || HOST_ARCH == HOST_ARCH_AARCH64)
        asm volatile (".word 0xe7f7defb\n");
--

2.56.0

author
#19 lore raw

PATCH v7 17/20 efi: LoongArch: Implement everything

In reply to Yao Zi

From: Jiaxun Yang <[email protected]>

Implement crt, relocator, linker scripts, and handling of PE relocation IMAGE_REL_BASED_LOONGARCH64_MARK_LA. Wire things up in Makefiles and Kconfig. The relocator originates from gnu-efi project.

---

No changes since v6

Changed from v5:
- Remove unnecessary settings of extra-$(CONFIG_*) in arch/loongarch/lib/Makefile for EFI applications
- Whitespace/empty line clean up
- Localize the temporary variable used only by loongarch in efi_loader_relocate()
- Fix grammar nits in the commit message

No changes since v4

Changed from v3
- Read addend from Rela entry when processing relative relocations in reloc_loongarch_efi.c
- This has been submitted to gnu-efi upstream[1]

Changed from v2
- Mention IMAGE_REL_BASED_LOONGARCH64_MARK_LA handling in the commit message, explain the instruction sequence in comments
- Mention the origin of reloc_loongarch_efi.c
- Fix typo in copyright of crt0_loongarch_efi.S
- Clean up whitespaces in crt0_loongarch_efi.S
- Fix SPDX identifier for reloc_loongarch_efi.c
- Drop the meaningless branch for DT_PLTGOT in reloc_loongarch_efi.c

Changed from v1
- Replace obsolete macro __loongarch64 with __loongarch__ and __loongarch_grlen
- Support relocation of IMAGE_REL_BASED_LOONGARCH64_MARK_LA

[1]: https://github.com/ncroxon/gnu-efi/pull/109

---
 arch/loongarch/config.mk                 |   4 +
 arch/loongarch/lib/Makefile              |   8 +
 arch/loongarch/lib/crt0_loongarch_efi.S  | 182 +++++++++++++++++++++++
 arch/loongarch/lib/elf_loongarch_efi.lds |  76 ++++++++++
 arch/loongarch/lib/reloc_loongarch_efi.c | 101 +++++++++++++
 lib/efi_loader/Kconfig                   |   2 +-
 lib/efi_loader/efi_helper.c              |   3 +
 lib/efi_loader/efi_image_loader.c        |  33 ++++
 8 files changed, 408 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/lib/crt0_loongarch_efi.S
 create mode 100644 arch/loongarch/lib/elf_loongarch_efi.lds
 create mode 100644 arch/loongarch/lib/reloc_loongarch_efi.c
arch/loongarch/config.mk +4 -0
index 7c247400e361..bae4566e9b62 100644
@@ -21,3 +21,7 @@ endif
 PLATFORM_CPPFLAGS	+= -fpic
 PLATFORM_RELFLAGS	+= -fno-common -ffunction-sections -fdata-sections
 LDFLAGS_u-boot		+= --gc-sections -static -pie
+
+EFI_LDS				:= elf_loongarch_efi.lds
+EFI_CRT0			:= crt0_loongarch_efi.o
+EFI_RELOC			:= reloc_loongarch_efi.o
arch/loongarch/lib/Makefile +8 -0
index 9eb9662066a5..d67cfa9ed18c 100644
@@ -12,3 +12,11 @@ ifeq ($(CONFIG_$(SPL_)SYSRESET),)
 obj-y	+= reset.o
 endif
 obj-y	+= setjmp.o
+
+# For building EFI apps
+CFLAGS_NON_EFI := -fstack-protector-strong
+CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_CRT0) := $(CFLAGS_NON_EFI)
+
+CFLAGS_$(EFI_RELOC) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_RELOC) := $(CFLAGS_NON_EFI)
arch/loongarch/lib/crt0_loongarch_efi.S new file +182 -0

182 lines not on this page · raw message

arch/loongarch/lib/elf_loongarch_efi.lds new file +76 -0

76 lines not on this page · raw message

arch/loongarch/lib/reloc_loongarch_efi.c new file +101 -0

101 lines not on this page · raw message

lib/efi_loader/Kconfig +1 -1
index c4b0c2137b87..6a417e75bf46 100644
@@ -7,7 +7,7 @@ config EFI_LOADER
 			SYS_CPU = arm1176 || \
 			SYS_CPU = armv7   || \
 			SYS_CPU = armv8)  || \
-		X86 || RISCV || SANDBOX)
+		X86 || RISCV || LOONGARCH || SANDBOX)
 	# We have not fully removed the requirement for some block device
 	depends on BLK
 	# We need EFI_STUB_64BIT to be set on x86_64 with EFI_STUB
lib/efi_loader/efi_helper.c +3 -0
index bc48e265b901..641b001a9a16 100644
@@ -44,6 +44,9 @@
 #elif HOST_ARCH == HOST_ARCH_RISCV64
 #define HOST_BOOTEFI_NAME "BOOTRISCV64.EFI"
 #define HOST_PXE_ARCH 0x1b
+#elif HOST_ARCH == HOST_ARCH_LOONGARCH64
+#define HOST_BOOTEFI_NAME "BOOTLOONGARCH64.EFI"
+#define HOST_PXE_ARCH 0x27
 #else
 #error Unsupported Host architecture
 #endif
lib/efi_loader/efi_image_loader.c +33 -0
index bed5f1817266..612e0cfec2ce 100644
@@ -19,6 +19,7 @@
 #include <crypto/mscode.h>
 #include <crypto/pkcs7_parser.h>
 #include <linux/err.h>
+#include <linux/bitfield.h>
 
 const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
 const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
@@ -231,6 +232,38 @@ static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
 					return EFI_LOAD_ERROR;
 				}
 				break;
+#elif defined(__loongarch__) && __loongarch_grlen == 64
+			case IMAGE_REL_BASED_LOONGARCH64_MARK_LA: {
+				uint64_t tmp;
+
+				if (virt_size - offset < 16) {
+					log_debug("relocation address out of bounds\n");
+					return EFI_LOAD_ERROR;
+				}
+
+				/*
+				 * Apply an offset to a 64-bit immediate loaded
+				 * by four instructions, lu12i.w, ori, lu32i.d,
+				 * and lu52i.d.
+				 */
+				tmp = FIELD_GET(GENMASK(24, 5), x32[0]) << 12;
+				tmp |= FIELD_GET(GENMASK(21, 10), x32[1]);
+				tmp |= FIELD_GET(GENMASK(24, 5), x32[2]) << 32;
+				tmp |= FIELD_GET(GENMASK(21, 10), x32[3]) << 52;
+				tmp += (uint64_t)delta;
+
+				x32[0] &= ~GENMASK(24, 5);
+				x32[0] |= FIELD_PREP(GENMASK(24, 5), tmp >> 12);
+				x32[1] &= ~GENMASK(21, 10);
+				x32[1] |= FIELD_PREP(GENMASK(21, 10), tmp);
+				x32[2] &= ~GENMASK(24, 5);
+				x32[2] |= FIELD_PREP(GENMASK(24, 5), tmp >> 32);
+				x32[3] &= ~GENMASK(21, 10);
+				x32[3] |= FIELD_PREP(GENMASK(21, 10),
+						     tmp >> 52);
+
+				break;
+			}
 #endif
 			default:
 				log_err("Unknown Relocation off %lx type %x\n",
Signature · 1 line
2.56.0
Acked-by on v7 17/20
#20 lore raw

Re: [PATCH v7 17/20] efi: LoongArch: Implement everything

Quoted 11 lines · Yao Zi

From: Jiaxun Yang <[email protected]>

Implement crt, relocator, linker scripts, and handling of PE relocation IMAGE_REL_BASED_LOONGARCH64_MARK_LA. Wire things up in Makefiles and Kconfig. The relocator originates from gnu-efi project.

---

loader bits

Quoted 533 lines

No changes since v6

Changed from v5:
- Remove unnecessary settings of extra-$(CONFIG_*) in arch/loongarch/lib/Makefile for EFI applications
- Whitespace/empty line clean up
- Localize the temporary variable used only by loongarch in efi_loader_relocate()
- Fix grammar nits in the commit message

No changes since v4

Changed from v3
- Read addend from Rela entry when processing relative relocations in reloc_loongarch_efi.c
- This has been submitted to gnu-efi upstream[1]

Changed from v2
- Mention IMAGE_REL_BASED_LOONGARCH64_MARK_LA handling in the commit message, explain the instruction sequence in comments
- Mention the origin of reloc_loongarch_efi.c
- Fix typo in copyright of crt0_loongarch_efi.S
- Clean up whitespaces in crt0_loongarch_efi.S
- Fix SPDX identifier for reloc_loongarch_efi.c
- Drop the meaningless branch for DT_PLTGOT in reloc_loongarch_efi.c

Changed from v1
- Replace obsolete macro __loongarch64 with __loongarch__ and __loongarch_grlen
- Support relocation of IMAGE_REL_BASED_LOONGARCH64_MARK_LA

[1]: https://github.com/ncroxon/gnu-efi/pull/109

---
 arch/loongarch/config.mk                 |   4 +
 arch/loongarch/lib/Makefile              |   8 +
 arch/loongarch/lib/crt0_loongarch_efi.S  | 182 +++++++++++++++++++++++
 arch/loongarch/lib/elf_loongarch_efi.lds |  76 ++++++++++
 arch/loongarch/lib/reloc_loongarch_efi.c | 101 +++++++++++++
 lib/efi_loader/Kconfig                   |   2 +-
 lib/efi_loader/efi_helper.c              |   3 +
 lib/efi_loader/efi_image_loader.c        |  33 ++++
 8 files changed, 408 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/lib/crt0_loongarch_efi.S
 create mode 100644 arch/loongarch/lib/elf_loongarch_efi.lds
 create mode 100644 arch/loongarch/lib/reloc_loongarch_efi.c
arch/loongarch/config.mk +4 -0
index 7c247400e361..bae4566e9b62 100644
@@ -21,3 +21,7 @@ endif
 PLATFORM_CPPFLAGS      += -fpic
 PLATFORM_RELFLAGS      += -fno-common -ffunction-sections -fdata-sections
 LDFLAGS_u-boot         += --gc-sections -static -pie
+
+EFI_LDS                                := elf_loongarch_efi.lds
+EFI_CRT0                       := crt0_loongarch_efi.o
+EFI_RELOC                      := reloc_loongarch_efi.o
arch/loongarch/lib/Makefile +8 -0
index 9eb9662066a5..d67cfa9ed18c 100644
@@ -12,3 +12,11 @@ ifeq ($(CONFIG_$(SPL_)SYSRESET),)
 obj-y  += reset.o
 endif
 obj-y  += setjmp.o
+
+# For building EFI apps
+CFLAGS_NON_EFI := -fstack-protector-strong
+CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_CRT0) := $(CFLAGS_NON_EFI)
+
+CFLAGS_$(EFI_RELOC) := $(CFLAGS_EFI)
+CFLAGS_REMOVE_$(EFI_RELOC) := $(CFLAGS_NON_EFI)
arch/loongarch/lib/crt0_loongarch_efi.S new file +182 -0
new file mode 100644
index 000000000000..ba3d9b4ad71d
@@ -0,0 +1,182 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * crt0-efi-loongarch.S - PE/COFF header for LoongArch EFI applications
+ *
+ * Copyright (C) 2024 Jiaxun Yang <[email protected]>
+ */
+
+#include <asm-generic/pe.h>
+#include <asm/asm.h>
+
+#if __loongarch_grlen == 64
+#define PE_MACHINE     IMAGE_FILE_MACHINE_LOONGARCH64
+#define PE_MAGIC    IMAGE_NT_OPTIONAL_HDR64_MAGIC
+#define IMG_CHARACTERISTICS \
+       (IMAGE_FILE_EXECUTABLE_IMAGE | \
+        IMAGE_FILE_LINE_NUMS_STRIPPED | \
+        IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+        IMAGE_FILE_LARGE_ADDRESS_AWARE | \
+        IMAGE_FILE_DEBUG_STRIPPED)
+#else
+#define PE_MACHINE     IMAGE_FILE_MACHINE_LOONGARCH32
+#define PE_MAGIC    IMAGE_NT_OPTIONAL_HDR32_MAGIC
+#define IMG_CHARACTERISTICS \
+       (IMAGE_FILE_EXECUTABLE_IMAGE | \
+        IMAGE_FILE_LINE_NUMS_STRIPPED | \
+        IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
+        IMAGE_FILE_DEBUG_STRIPPED)
+#endif
+
+       .section        .text.head
+
+       /*
+        * Magic "MZ" signature for PE/COFF
+        */
+       .globl  ImageBase
+ImageBase:
+       .short  IMAGE_DOS_SIGNATURE             /* 'MZ' */
+       .skip   58
+       .long   pe_header - ImageBase           /* Offset to the PE header */
+pe_header:
+       .long   IMAGE_NT_SIGNATURE              /* 'PE' */
+coff_header:
+       .short  PE_MACHINE                      /* LoongArch 64/32-bit */
+       .short  3                               /* nr_sections */
+       .long   0                               /* TimeDateStamp */
+       .long   0                               /* PointerToSymbolTable */
+       .long   0                               /* NumberOfSymbols */
+       .short  section_table - optional_header /* SizeOfOptionalHeader */
+       .short  IMG_CHARACTERISTICS             /* Characteristics */
+optional_header:
+       .short  PE_MAGIC                        /* PE32(+) format */
+       .byte   0x02                            /* MajorLinkerVersion */
+       .byte   0x14                            /* MinorLinkerVersion */
+       .long   _edata - _start                 /* SizeOfCode */
+       .long   0                               /* SizeOfInitializedData */
+       .long   0                               /* SizeOfUninitializedData */
+       .long   _start - ImageBase              /* AddressOfEntryPoint */
+       .long   _start - ImageBase              /* BaseOfCode */
+#if __loongarch_grlen != 64
+       .long   0                               /* BaseOfData */
+#endif
+
+extra_header_fields:
+       LONG    0
+       .long   0x200                           /* SectionAlignment */
+       .long   0x200                           /* FileAlignment */
+       .short  0                               /* MajorOperatingSystemVersion */
+       .short  0                               /* MinorOperatingSystemVersion */
+       .short  1                               /* MajorImageVersion */
+       .short  0                               /* MinorImageVersion */
+       .short  0                               /* MajorSubsystemVersion */
+       .short  0                               /* MinorSubsystemVersion */
+       .long   0                               /* Win32VersionValue */
+
+       .long   _edata - ImageBase              /* SizeOfImage */
+
+       /*
+        * Everything before the kernel image is considered part of the header
+        */
+       .long   _start - ImageBase              /* SizeOfHeaders */
+       .long   0                               /* CheckSum */
+       .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
+#if CONFIG_VENDOR_EFI
+       .short  0                               /* DllCharacteristics */
+#else
+       .short  IMAGE_DLLCHARACTERISTICS_NX_COMPAT
+#endif
+       LONG    0                               /* SizeOfStackReserve */
+       LONG    0                               /* SizeOfStackCommit */
+       LONG    0                               /* SizeOfHeapReserve */
+       LONG    0                               /* SizeOfHeapCommit */
+
+       .long   0                               /* LoaderFlags */
+       .long   0x6                             /* NumberOfRvaAndSizes */
+
+       .quad   0                               /* ExportTable */
+       .quad   0                               /* ImportTable */
+       .quad   0                               /* ResourceTable */
+       .quad   0                               /* ExceptionTable */
+       .quad   0                               /* CertificationTable */
+       .quad   0                               /* BaseRelocationTable */
+
+       /* Section table */
+section_table:
+
+       /*
+        * The EFI application loader requires a relocation section
+        * because EFI applications must be relocatable.  This is a
+        * dummy section as far as we are concerned.
+        */
+       .ascii  ".reloc"
+       .byte   0
+       .byte   0                       /* end of 0 padding of section name */
+       .long   0
+       .long   0
+       .long   0                       /* SizeOfRawData */
+       .long   0                       /* PointerToRawData */
+       .long   0                       /* PointerToRelocations */
+       .long   0                       /* PointerToLineNumbers */
+       .short  0                       /* NumberOfRelocations */
+       .short  0                       /* NumberOfLineNumbers */
+       .long   0x42100040              /* Characteristics (section flags) */
+
+
+       .ascii  ".text"
+       .byte   0
+       .byte   0
+       .byte   0                       /* end of 0 padding of section name */
+       .long   _etext - _start         /* VirtualSize */
+       .long   _start - ImageBase      /* VirtualAddress */
+       .long   _etext - _start         /* SizeOfRawData */
+       .long   _start - ImageBase      /* PointerToRawData */
+       .long   0                       /* PointerToRelocations (0 for executables) */
+       .long   0                       /* PointerToLineNumbers (0 for executables) */
+       .short  0                       /* NumberOfRelocations  (0 for executables) */
+       .short  0                       /* NumberOfLineNumbers  (0 for executables) */
+       /* Characteristics (section flags) */
+       .long   (IMAGE_SCN_MEM_READ | \
+                IMAGE_SCN_MEM_EXECUTE | \
+                IMAGE_SCN_CNT_CODE)
+
+       .ascii  ".data"
+       .byte   0
+       .byte   0
+       .byte   0                       /* end of 0 padding of section name */
+       .long   _edata - _data          /* VirtualSize */
+       .long   _data - ImageBase       /* VirtualAddress */
+       .long   _edata - _data          /* SizeOfRawData */
+       .long   _data - ImageBase       /* PointerToRawData */
+       .long   0                       /* PointerToRelocations */
+       .long   0                       /* PointerToLineNumbers */
+       .short  0                       /* NumberOfRelocations */
+       .short  0                       /* NumberOfLineNumbers */
+       /* Characteristics (section flags) */
+       .long   (IMAGE_SCN_MEM_WRITE | \
+                IMAGE_SCN_MEM_READ | \
+                IMAGE_SCN_CNT_INITIALIZED_DATA)
+
+       .align          12
+       .globl  _start
+       .type   _start, @function
+_start:
+       PTR_ADDI        sp, sp, -(3 * LONGSIZE)
+       LONG_S          ra, sp, 0
+       LONG_S          a0, sp, (1 * LONGSIZE)
+       LONG_S          a1, sp, (2 * LONGSIZE)
+
+       move            a2, a0          /* a2: ImageHandle */
+       move            a3, a1          /* a3: SystemTable */
+       la.local        a0, ImageBase   /* a0: ImageBase */
+       la.local        a1, _DYNAMIC    /* a1: DynamicSection */
+       bl              _relocate
+       bnez            a0, 0f
+
+       LONG_L          a0, sp, (1 * LONGSIZE)
+       LONG_L          a1, sp, (2 * LONGSIZE)
+       bl              efi_main
+
+0:     LONG_L          ra, sp, 0
+       PTR_ADDI        sp, sp, (3 * LONGSIZE)
+       jr              ra
+       .end    _start
arch/loongarch/lib/elf_loongarch_efi.lds new file +76 -0
new file mode 100644
index 000000000000..050e5a52a0b3
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * U-Boot LoongArch
+ *
+ * Modified from arch/riscv/lib/elf_riscv64_efi.lds
+ */
+
+OUTPUT_ARCH(loongarch)
+
+PHDRS
+{
+       data PT_LOAD FLAGS(3); /* SHF_WRITE | SHF_ALLOC */
+}
+
+ENTRY(_start)
+SECTIONS
+{
+       .text 0x0 : {
+               _text = .;
+               *(.text.head)
+               *(.text)
+               *(.text.*)
+               *(.gnu.linkonce.t.*)
+               *(.srodata)
+               *(.rodata*)
+               . = ALIGN(16);
+               *(.dynamic);
+               . = ALIGN(512);
+       }
+       .rela.dyn : { *(.rela.dyn) }
+       .rela.plt : { *(.rela.plt) }
+       .rela.got : { *(.rela.got) }
+       .rela.data : { *(.rela.data) *(.rela.data*) }
+       _etext = .;
+       _text_size = . - _text;
+       . = ALIGN(4096);
+       .data : {
+               _data = .;
+               *(.sdata)
+               *(.data)
+               *(.data1)
+               *(.data.*)
+               *(.got.plt)
+               *(.got)
+
+               /*
+                * The EFI loader doesn't seem to like a .bss section, so we
+                * stick it all into .data:
+                */
+               . = ALIGN(16);
+               _bss = .;
+               *(.sbss)
+               *(.scommon)
+               *(.dynbss)
+               *(.bss)
+               *(.bss.*)
+               *(COMMON)
+               . = ALIGN(512);
+               _bss_end = .;
+               _edata = .;
+       } :data
+       _data_size = _edata - _data;
+
+       . = ALIGN(4096);
+       .dynsym   : { *(.dynsym) }
+       . = ALIGN(4096);
+       .dynstr   : { *(.dynstr) }
+       . = ALIGN(4096);
+       .note.gnu.build-id : { *(.note.gnu.build-id) }
+       /DISCARD/ : {
+               *(.rel.reloc)
+               *(.eh_frame)
+               *(.note.GNU-stack)
+       }
+       .comment 0 : { *(.comment) }
+}
arch/loongarch/lib/reloc_loongarch_efi.c new file +101 -0
new file mode 100644
index 000000000000..72acfe525c3f
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/* reloc_loongarch.c - position independent ELF shared object relocator
+   Copyright (C) 2018 Alexander Graf <[email protected]>
+   Copyright (C) 2014 Linaro Ltd. <[email protected]>
+   Copyright (C) 1999 Hewlett-Packard Co.
+       Contributed by David Mosberger <[email protected]>.
+
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions
+    are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials
+      provided with the distribution.
+    * Neither the name of Hewlett-Packard Co. nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+    BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+    SUCH DAMAGE.
+*/
+
+#include <efi.h>
+
+#include <elf.h>
+
+#if __loongarch_grlen == 64
+#define Elf_Dyn                Elf64_Dyn
+#define Elf_Rela       Elf64_Rela
+#define ELF_R_TYPE     ELF64_R_TYPE
+#else
+#define Elf_Dyn                Elf32_Dyn
+#define Elf_Rela       Elf32_Rela
+#define ELF_R_TYPE     ELF32_R_TYPE
+#endif
+
+efi_status_t EFIAPI _relocate(long ldbase, Elf_Dyn *dyn)
+{
+       long relsz = 0, relent = 0;
+       Elf_Rela *rel = 0;
+       unsigned long *addr;
+       int i;
+
+       for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
+               switch (dyn[i].d_tag) {
+               case DT_RELA:
+                       rel = (Elf_Rela *)
+                               ((unsigned long)dyn[i].d_un.d_ptr
+                                       + ldbase);
+                       break;
+               case DT_RELASZ:
+                       relsz = dyn[i].d_un.d_val;
+                       break;
+               case DT_RELAENT:
+                       relent = dyn[i].d_un.d_val;
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       if (!rel && relent == 0)
+               return EFI_SUCCESS;
+
+       if (!rel || relent == 0)
+               return EFI_LOAD_ERROR;
+
+       while (relsz > 0) {
+               /* apply the relocs */
+               switch (ELF_R_TYPE(rel->r_info)) {
+               case R_LARCH_NONE:
+                       break;
+               case R_LARCH_RELATIVE:
+                       addr = (unsigned long *)
+                               (ldbase + rel->r_offset);
+                       *addr = ldbase + rel->r_addend;
+                       break;
+               default:
+                       break;
+               }
+               rel = (Elf_Rela *)((char *)rel + relent);
+               relsz -= relent;
+       }
+       return EFI_SUCCESS;
+}
lib/efi_loader/Kconfig +1 -1
index c4b0c2137b87..6a417e75bf46 100644
@@ -7,7 +7,7 @@ config EFI_LOADER
                        SYS_CPU = arm1176 || \
                        SYS_CPU = armv7   || \
                        SYS_CPU = armv8)  || \
-               X86 || RISCV || SANDBOX)
+               X86 || RISCV || LOONGARCH || SANDBOX)
        # We have not fully removed the requirement for some block device
        depends on BLK
        # We need EFI_STUB_64BIT to be set on x86_64 with EFI_STUB
lib/efi_loader/efi_helper.c +3 -0
index bc48e265b901..641b001a9a16 100644
@@ -44,6 +44,9 @@
 #elif HOST_ARCH == HOST_ARCH_RISCV64
 #define HOST_BOOTEFI_NAME "BOOTRISCV64.EFI"
 #define HOST_PXE_ARCH 0x1b
+#elif HOST_ARCH == HOST_ARCH_LOONGARCH64
+#define HOST_BOOTEFI_NAME "BOOTLOONGARCH64.EFI"
+#define HOST_PXE_ARCH 0x27
 #else
 #error Unsupported Host architecture
 #endif
lib/efi_loader/efi_image_loader.c +33 -1
index bed5f1817266..612e0cfec2ce 100644
@@ -19,6 +19,7 @@
 #include <crypto/mscode.h>
 #include <crypto/pkcs7_parser.h>
 #include <linux/err.h>
+#include <linux/bitfield.h>
 
 const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
 const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
@@ -231,6 +232,38 @@ static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
                                        return EFI_LOAD_ERROR;
                                }
                                break;
+#elif defined(__loongarch__) && __loongarch_grlen == 64
+                       case IMAGE_REL_BASED_LOONGARCH64_MARK_LA: {
+                               uint64_t tmp;
+
+                               if (virt_size - offset < 16) {
+                                       log_debug("relocation address out of bounds\n");
+                                       return EFI_LOAD_ERROR;
+                               }
+
+                               /*
+                                * Apply an offset to a 64-bit immediate loaded
+                                * by four instructions, lu12i.w, ori, lu32i.d,
+                                * and lu52i.d.
+                                */
+                               tmp = FIELD_GET(GENMASK(24, 5), x32[0]) << 12;
+                               tmp |= FIELD_GET(GENMASK(21, 10), x32[1]);
+                               tmp |= FIELD_GET(GENMASK(24, 5), x32[2]) << 32;
+                               tmp |= FIELD_GET(GENMASK(21, 10), x32[3]) << 52;
+                               tmp += (uint64_t)delta;
+
+                               x32[0] &= ~GENMASK(24, 5);
+                               x32[0] |= FIELD_PREP(GENMASK(24, 5), tmp >> 12);
+                               x32[1] &= ~GENMASK(21, 10);
+                               x32[1] |= FIELD_PREP(GENMASK(21, 10), tmp);
+                               x32[2] &= ~GENMASK(24, 5);
+                               x32[2] |= FIELD_PREP(GENMASK(24, 5), tmp >> 32);
+                               x32[3] &= ~GENMASK(21, 10);
+                               x32[3] |= FIELD_PREP(GENMASK(21, 10),
+                                                    tmp >> 52);
+
+                               break;
+                       }
 #endif
                        default:
                                log_err("Unknown Relocation off %lx type %x\n",
--

2.56.0

author
#21 lore raw

PATCH v7 18/20 doc: arch: Add documentation for LoongArch

In reply to Yao Zi

Add a brief introduction to LoongArch architecture, some notes on the implementation, and the toolchain supporting status.

---

Changed from v5
- Improve the documentation

New patch, no changes since v3

---
 doc/arch/index.rst     |  1 +
 doc/arch/loongarch.rst | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 doc/arch/loongarch.rst
doc/arch/index.rst +1 -0
index 60c93b3b6640..10903b806d2c 100644
@@ -9,6 +9,7 @@ Architecture-specific doc
    arc
    arm64
    arm64.ffa
+   loongarch
    m68k
    mips
    nios2
doc/arch/loongarch.rst new file +31 -0
new file mode 100644
index 000000000000..93eed74fe666
@@ -0,0 +1,31 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+.. Copyright (C) 2026 Yao Zi <[email protected]>
+
+LoongArch
+=========
+
+LoongArch is a RISC ISA developed by Loongson Technology. It currently defines
+three variants: a reduced 32-bit version (LA32R), a standard 32-bit version
+(LA32S) for embedded devices, and a 64-bit version (LA64).
+
+Notes
+-----
+
+1. Currently U-Boot only runs on LA64 variant with the highest privilege level
+   (PLV0).
+
+2. CONFIG_ARCH_LA64=y selects support for 64-bit LoongArch variant.
+
+3. U-Boot requires hardware-maintained coherency of different levels of
+   instruction and data caches. It determines the cache topology by reading
+   CPUCFG registers; L2 and L3 caches, if present, must be unified or
+   instruction-only. This is true for all commercial LA64 hardware. Refer to
+   arch/loongarch/lib/cache.c for further description.
+
+Toolchain
+---------
+
+LoongArch is supported in upstream binutils, gcc, and LLVM. GCC 12.1 started to
+support LA64, and GCC 16 started to support LA32.
+`kernel.org <https://mirrors.edge.kernel.org/pub/tools/crosstool/>`_ provides
+pre-built toolchain for both 32-bit and 64-bit LoongArch.
Signature · 1 line
2.56.0
author
#22 lore raw

PATCH v7 19/20 MAINTAINERS: Add entry for LoongArch

In reply to Yao Zi

Add an entry for LoongArch-specific code and documentation. Jiaxun and I will maintain the LoongArch port.

---

New patch, no changes since v3

---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index b6ce499d0c20..c712c69507e3 100644
@@ -1178,6 +1178,17 @@ F:	lib/getopt.c
 F:	test/log/
 F:	test/py/tests/test_log.py
 
+LOONGARCH
+M:	Jiaxun Yang <[email protected]>
+M:	Yao Zi <[email protected]>
+S:	Maintained
+T:	git https://git.u-boot-project.org/u-boot/custodians/u-boot-loongarch.git
+F:	arch/loongarch
+F:	doc/arch/loongarch.rst
+F:	drivers/cpu/loongarch_cpu.c
+F:	drivers/timer/loongarch_timer.c
+N:	loongarch
+
 M680X0 ARCHITECTURE
 M:	Kuan-Wei Chiu <[email protected]>
 S:	Maintained
Signature · 1 line
2.56.0
author
#23 lore raw

PATCH v7 20/20 CI: Wire up qemu-loongarch64

In reply to Yao Zi

Enable qemu-loongarch64 in CI so it could be automatically tested.

---

No changes since v4

Changed from v3
- Collect Tom's review tag

New patch since v3

---
 .azure-pipelines.yml | 11 +++++++----
 .gitlab-ci.yml       |  6 ++++++
 2 files changed, 13 insertions(+), 4 deletions(-)
.azure-pipelines.yml +7 -4
index fcf7aafade6e..a8f57f5910e2 100644
@@ -19,7 +19,7 @@ variables:
   k3_64b: "k3 -x armv7,phytec,toradex"
   kirkwood_mvebu: "kirkwood mvebu"
   layerscape_vf610: "ls1 ls2 lx2 vf610 -x phytec,toradex"
-  m68k_remaining_mx_xtensa: "m68k imxrt mx xtensa -x mx6,aarch64"
+  loongarch_m68k_remaining_mx_xtensa: "loongarch m68k imxrt mx xtensa -x mx6,aarch64"
   mips_x86: "mips x86 -x mediatek"
   mx6: "mx6 -x engicam,phytec,toradex"
   imx8: "imx8 -x engicam,phytec,toradex"
@@ -213,7 +213,7 @@ stages:
           built="$built $(tools/buildman/buildman ${BMANARGS} $(k3_64b) | grep '^   ')"
           built="$built $(tools/buildman/buildman ${BMANARGS} $(kirkwood_mvebu) | grep '^   ')"
           built="$built $(tools/buildman/buildman ${BMANARGS} $(layerscape_vf610) | grep '^   ')"
-          built="$built $(tools/buildman/buildman ${BMANARGS} $(m68k_remaining_mx_xtensa) | grep '^   ')"
+          built="$built $(tools/buildman/buildman ${BMANARGS} $(loongarch_m68k_remaining_mx_xtensa) | grep '^   ')"
           built="$built $(tools/buildman/buildman ${BMANARGS} $(mips_x86) | grep '^   ')"
           built="$built $(tools/buildman/buildman ${BMANARGS} $(mx6) | grep '^   ')"
           built="$built $(tools/buildman/buildman ${BMANARGS} $(imx8) | grep '^   ')"
@@ -491,6 +491,9 @@ stages:
         qemu_arm_sbsa_ref:
           TEST_PY_BD: "qemu-arm-sbsa"
           TEST_PY_TEST_SPEC: "not sleep"
+        qemu_loongarch64:
+          TEST_PY_BD: "qemu-loongarch64"
+          TEST_PY_TEST_SPEC: "not sleep"
         qemu_m68k:
           TEST_PY_BD: "M5208EVBE"
           TEST_PY_ID: "--id qemu"
@@ -658,8 +661,8 @@ stages:
           BUILDMAN: $(kirkwood_mvebu)
         layerscape_vf610:
           BUILDMAN: $(layerscape_vf610)
-        m68k_remaining_mx_xtensa:
-          BUILDMAN: $(m68k_remaining_mx_xtensa)
+        loongarch_m68k_remaining_mx_xtensa:
+          BUILDMAN: $(loongarch_m68k_remaining_mx_xtensa)
         mips_x86:
           BUILDMAN: $(mips_x86)
         mx6:
.gitlab-ci.yml +6 -0
index 0338f20b8a2e..d3a83ad0a619 100644
@@ -418,6 +418,12 @@ qemu_arm_sbsa test.py:
     TEST_PY_TEST_SPEC: "not sleep"
   <<: *buildman_and_testpy_dfn
 
+qemu_loongarch64 test.py:
+  variables:
+    TEST_PY_BD: "qemu-loongarch64"
+    TEST_PY_TEST_SPEC: "not sleep"
+  <<: *buildman_and_testpy_dfn
+
 qemu_m68k test.py:
   variables:
     TEST_PY_BD: "M5208EVBE"
Signature · 1 line
2.56.0