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