1# SPDX-License-Identifier: GPL-2.0 2# 3# Author: Huacai Chen <chenhuacai@loongson.cn> 4# Copyright (C) 2020-2022 Loongson Technology Corporation Limited 5 6boot := arch/loongarch/boot 7 8ifeq ($(shell uname -m),loongarch32) 9KBUILD_DEFCONFIG := loongson32_defconfig 10else 11KBUILD_DEFCONFIG := loongson64_defconfig 12endif 13 14KBUILD_DTBS := dtbs 15 16image-name-y := vmlinux 17image-name-$(CONFIG_EFI_ZBOOT) := vmlinuz 18 19ifndef CONFIG_EFI_STUB 20KBUILD_IMAGE := $(boot)/vmlinux.elf 21else 22KBUILD_IMAGE := $(boot)/$(image-name-y).efi 23endif 24 25# 26# Select the object file format to substitute into the linker script. 27# 2832bit-tool-archpref = loongarch32 2964bit-tool-archpref = loongarch64 3032bit-bfd = elf32-loongarch 3164bit-bfd = elf64-loongarch 3232bit-emul = elf32loongarch 3364bit-emul = elf64loongarch 34 35CC_FLAGS_FPU := -mfpu=64 36CC_FLAGS_NO_FPU := -msoft-float 37 38ifdef CONFIG_UNWINDER_ORC 39orc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h 40orc_hash_sh := $(srctree)/scripts/orc_hash.sh 41targets += $(orc_hash_h) 42quiet_cmd_orc_hash = GEN $@ 43 cmd_orc_hash = mkdir -p $(dir $@); \ 44 $(CONFIG_SHELL) $(orc_hash_sh) < $< > $@ 45$(orc_hash_h): $(srctree)/arch/loongarch/include/asm/orc_types.h $(orc_hash_sh) FORCE 46 $(call if_changed,orc_hash) 47archprepare: $(orc_hash_h) 48endif 49 50ifdef CONFIG_DYNAMIC_FTRACE 51KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY 52CC_FLAGS_FTRACE := -fpatchable-function-entry=2 53endif 54 55ifdef CONFIG_32BIT 56tool-archpref = $(32bit-tool-archpref) 57UTS_MACHINE := loongarch32 58else 59tool-archpref = $(64bit-tool-archpref) 60UTS_MACHINE := loongarch64 61endif 62 63ifneq ($(SUBARCH),$(ARCH)) 64 ifeq ($(CROSS_COMPILE),) 65 CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux- $(tool-archpref)-linux-gnu- $(tool-archpref)-unknown-linux-gnu-) 66 endif 67endif 68 69ifdef CONFIG_32BIT 70ifdef CONFIG_32BIT_STANDARD 71ld-emul = $(32bit-emul) 72cflags-y += -march=la32v1.0 -mabi=ilp32s -mcmodel=normal 73else # CONFIG_32BIT_REDUCED 74ld-emul = $(32bit-emul) 75cflags-y += -march=la32rv1.0 -mabi=ilp32s -mcmodel=normal 76endif 77endif 78 79ifdef CONFIG_64BIT 80ld-emul = $(64bit-emul) 81cflags-y += -march=loongarch64 -mabi=lp64s -mcmodel=normal 82endif 83 84cflags-y += -pipe $(CC_FLAGS_NO_FPU) 85LDFLAGS_vmlinux += -static -n -nostdlib 86 87# When the assembler supports explicit relocation hint, we must use it. 88# GCC may have -mexplicit-relocs off by default if it was built with an old 89# assembler, so we force it via an option. 90# 91# When the assembler does not supports explicit relocation hint, we can't use 92# it. Disable it if the compiler supports it. 93# 94# The combination of a "new" assembler and "old" GCC is not supported, given 95# the rarity of this combo and the extra complexity needed to make it work. 96# Either upgrade the compiler or downgrade the assembler; the build will error 97# out if it is the case (by probing for the model attribute; all supported 98# compilers in this case would have support). 99# 100# Also, -mdirect-extern-access is useful in case of building with explicit 101# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have 102# support though. 103ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS 104cflags-y += $(call cc-option,-mexplicit-relocs) 105KBUILD_CFLAGS_KERNEL += $(call cc-option,-mdirect-extern-access) 106KBUILD_CFLAGS_KERNEL += $(call cc-option,-fdirect-access-external-data) 107KBUILD_AFLAGS_MODULE += $(call cc-option,-fno-direct-access-external-data) 108KBUILD_CFLAGS_MODULE += $(call cc-option,-fno-direct-access-external-data) 109else 110cflags-y += $(call cc-option,-mno-explicit-relocs) 111KBUILD_AFLAGS_KERNEL += -Wa,-mla-global-with-pcrel 112KBUILD_CFLAGS_KERNEL += -Wa,-mla-global-with-pcrel 113KBUILD_AFLAGS_MODULE += -Wa,-mla-global-with-abs 114KBUILD_CFLAGS_MODULE += -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs 115endif 116 117KBUILD_AFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax) 118KBUILD_CFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax) 119KBUILD_AFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub) 120KBUILD_CFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub) 121 122ifdef CONFIG_OBJTOOL 123ifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP 124KBUILD_CFLAGS += -mannotate-tablejump 125else 126KBUILD_CFLAGS += -fno-jump-tables # keep compatibility with older compilers 127endif 128ifdef CONFIG_RUSTC_HAS_ANNOTATE_TABLEJUMP 129KBUILD_RUSTFLAGS += -Cllvm-args=--loongarch-annotate-tablejump 130else 131KBUILD_RUSTFLAGS += $(if $(call rustc-min-version,109300),-Cjump-tables=n,-Zno-jump-tables) # keep compatibility with older compilers 132endif 133ifdef CONFIG_LTO_CLANG 134# The annotate-tablejump option can not be passed to LLVM backend when LTO is enabled. 135# Ensure it is aware of linker with LTO, '--loongarch-annotate-tablejump' also needs to 136# be passed via '-mllvm' to ld.lld. 137KBUILD_LDFLAGS += $(call ld-option,-mllvm --loongarch-annotate-tablejump) 138endif 139endif 140 141KBUILD_RUSTFLAGS += --target=loongarch64-unknown-none-softfloat -Ccode-model=small 142KBUILD_RUSTFLAGS_KERNEL += -Zdirect-access-external-data=yes 143KBUILD_RUSTFLAGS_MODULE += -Zdirect-access-external-data=no 144 145ifeq ($(CONFIG_RELOCATABLE),y) 146KBUILD_CFLAGS_KERNEL += -fPIE 147KBUILD_RUSTFLAGS_KERNEL += -Crelocation-model=pie 148LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs) 149endif 150 151cflags-y += $(call cc-option, -mno-check-zero-division -fno-isolate-erroneous-paths-dereference) 152 153ifndef CONFIG_KASAN 154cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset 155endif 156 157ifdef CONFIG_32BIT 158load-y = 0xa0200000 159else 160load-y = 0x9000000000200000 161endif 162 163bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y) 164 165drivers-$(CONFIG_PCI) += arch/loongarch/pci/ 166 167KBUILD_AFLAGS += $(cflags-y) 168KBUILD_CFLAGS += $(cflags-y) 169KBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y) 170 171# This is required to get dwarf unwinding tables into .debug_frame 172# instead of .eh_frame so we don't discard them. 173KBUILD_CFLAGS += -fno-asynchronous-unwind-tables 174 175ifdef CONFIG_ARCH_STRICT_ALIGN 176# Don't emit unaligned accesses. 177# Not all LoongArch cores support unaligned access, and as kernel we can't 178# rely on others to provide emulation for these accesses. 179KBUILD_CFLAGS += $(call cc-option,-mstrict-align) 180else 181# Optimise for performance on hardware supports unaligned access. 182KBUILD_CFLAGS += $(call cc-option,-mno-strict-align) 183endif 184 185KBUILD_CFLAGS += -isystem $(shell $(CC) -print-file-name=include) 186 187KBUILD_LDFLAGS += -m $(ld-emul) 188 189ifdef need-compiler 190CHECKFLAGS += $(shell $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \ 191 grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \ 192 sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g') 193endif 194 195libs-y += arch/loongarch/lib/ 196libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a 197 198drivers-y += arch/loongarch/crypto/ 199 200# suspend and hibernation support 201drivers-$(CONFIG_PM) += arch/loongarch/power/ 202 203ifeq ($(KBUILD_EXTMOD),) 204prepare: vdso_prepare 205vdso_prepare: prepare0 206 $(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h 207endif 208 209vdso-install-y += arch/loongarch/vdso/vdso.so.dbg 210 211all: $(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS) 212 213vmlinuz.efi: vmlinux.efi 214 215vmlinux.elf vmlinux.efi vmlinuz.efi: vmlinux 216 $(Q)$(MAKE) $(build)=$(boot) $(bootvars-y) $(boot)/$@ 217 218install: 219 $(call cmd,install) 220 221define archhelp 222 echo ' vmlinux.elf - Uncompressed ELF kernel image (arch/loongarch/boot/vmlinux.elf)' 223 echo ' vmlinux.efi - Uncompressed EFI kernel image (arch/loongarch/boot/vmlinux.efi)' 224 echo ' vmlinuz.efi - GZIP/ZSTD-compressed EFI kernel image (arch/loongarch/boot/vmlinuz.efi)' 225 echo ' Default when CONFIG_EFI_ZBOOT=y' 226 echo ' install - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or' 227 echo ' (distribution) /sbin/$(INSTALLKERNEL) or install.sh to $$(INSTALL_PATH)' 228 echo 229endef 230