xref: /linux/arch/loongarch/Makefile (revision b7191581a973ab2fca45d2ca64416065f1660ae0)
1fa96b57cSHuacai Chen# SPDX-License-Identifier: GPL-2.0
2fa96b57cSHuacai Chen#
3fa96b57cSHuacai Chen# Author: Huacai Chen <chenhuacai@loongson.cn>
4fa96b57cSHuacai Chen# Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5fa96b57cSHuacai Chen
6fa96b57cSHuacai Chenboot	:= arch/loongarch/boot
7fa96b57cSHuacai Chen
89e8536e2SHuacai ChenKBUILD_DEFCONFIG := loongson3_defconfig
95f346a6eSBinbin ZhouKBUILD_DTBS      := dtbs
109e8536e2SHuacai Chen
11c5d5cba7SArd Biesheuvelimage-name-y			:= vmlinux
12c5d5cba7SArd Biesheuvelimage-name-$(CONFIG_EFI_ZBOOT)	:= vmlinuz
13c5d5cba7SArd Biesheuvel
14ead384d9SHuacai Chenifndef CONFIG_EFI_STUB
15ead384d9SHuacai ChenKBUILD_IMAGE	:= $(boot)/vmlinux.elf
16ead384d9SHuacai Chenelse
17c5d5cba7SArd BiesheuvelKBUILD_IMAGE	:= $(boot)/$(image-name-y).efi
18ead384d9SHuacai Chenendif
19fa96b57cSHuacai Chen
20fa96b57cSHuacai Chen#
21fa96b57cSHuacai Chen# Select the object file format to substitute into the linker script.
22fa96b57cSHuacai Chen#
23fa96b57cSHuacai Chen64bit-tool-archpref	= loongarch64
24fa96b57cSHuacai Chen32bit-bfd		= elf32-loongarch
25fa96b57cSHuacai Chen64bit-bfd		= elf64-loongarch
26fa96b57cSHuacai Chen32bit-emul		= elf32loongarch
27fa96b57cSHuacai Chen64bit-emul		= elf64loongarch
28fa96b57cSHuacai Chen
29372f6623SSamuel HollandCC_FLAGS_FPU		:= -mfpu=64
30372f6623SSamuel HollandCC_FLAGS_NO_FPU		:= -msoft-float
31372f6623SSamuel Holland
32cb8a2ef0STiezhu Yangifdef CONFIG_UNWINDER_ORC
33cb8a2ef0STiezhu Yangorc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h
34cb8a2ef0STiezhu Yangorc_hash_sh := $(srctree)/scripts/orc_hash.sh
35cb8a2ef0STiezhu Yangtargets += $(orc_hash_h)
36cb8a2ef0STiezhu Yangquiet_cmd_orc_hash = GEN     $@
37cb8a2ef0STiezhu Yang      cmd_orc_hash = mkdir -p $(dir $@); \
38cb8a2ef0STiezhu Yang		     $(CONFIG_SHELL) $(orc_hash_sh) < $< > $@
39cb8a2ef0STiezhu Yang$(orc_hash_h): $(srctree)/arch/loongarch/include/asm/orc_types.h $(orc_hash_sh) FORCE
40cb8a2ef0STiezhu Yang	$(call if_changed,orc_hash)
41cb8a2ef0STiezhu Yangarchprepare: $(orc_hash_h)
42cb8a2ef0STiezhu Yangendif
43cb8a2ef0STiezhu Yang
444733f09dSQing Zhangifdef CONFIG_DYNAMIC_FTRACE
454733f09dSQing ZhangKBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
464733f09dSQing ZhangCC_FLAGS_FTRACE := -fpatchable-function-entry=2
474733f09dSQing Zhangendif
484733f09dSQing Zhang
49fa96b57cSHuacai Chenifdef CONFIG_64BIT
50fa96b57cSHuacai Chentool-archpref		= $(64bit-tool-archpref)
51fa96b57cSHuacai ChenUTS_MACHINE		:= loongarch64
52fa96b57cSHuacai Chenendif
53fa96b57cSHuacai Chen
54fa96b57cSHuacai Chenifneq ($(SUBARCH),$(ARCH))
55fa96b57cSHuacai Chen  ifeq ($(CROSS_COMPILE),)
56fa96b57cSHuacai Chen    CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux-  $(tool-archpref)-linux-gnu-  $(tool-archpref)-unknown-linux-gnu-)
57fa96b57cSHuacai Chen  endif
58fa96b57cSHuacai Chenendif
59fa96b57cSHuacai Chen
60fa96b57cSHuacai Chenifdef CONFIG_64BIT
61fa96b57cSHuacai Chenld-emul			= $(64bit-emul)
62e67e0eb6SHuacai Chencflags-y		+= -mabi=lp64s -mcmodel=normal
63fa96b57cSHuacai Chenendif
64fa96b57cSHuacai Chen
65372f6623SSamuel Hollandcflags-y			+= -pipe $(CC_FLAGS_NO_FPU)
6638b10b26SWANG XueruiLDFLAGS_vmlinux			+= -static -n -nostdlib
6711cd8a64SXi Ruoyao
6811cd8a64SXi Ruoyao# When the assembler supports explicit relocation hint, we must use it.
6911cd8a64SXi Ruoyao# GCC may have -mexplicit-relocs off by default if it was built with an old
7011cd8a64SXi Ruoyao# assembler, so we force it via an option.
7111cd8a64SXi Ruoyao#
7211cd8a64SXi Ruoyao# When the assembler does not supports explicit relocation hint, we can't use
7311cd8a64SXi Ruoyao# it.  Disable it if the compiler supports it.
7411cd8a64SXi Ruoyao#
7538b10b26SWANG Xuerui# The combination of a "new" assembler and "old" GCC is not supported, given
7638b10b26SWANG Xuerui# the rarity of this combo and the extra complexity needed to make it work.
7738b10b26SWANG Xuerui# Either upgrade the compiler or downgrade the assembler; the build will error
7838b10b26SWANG Xuerui# out if it is the case (by probing for the model attribute; all supported
7938b10b26SWANG Xuerui# compilers in this case would have support).
8038b10b26SWANG Xuerui#
8138b10b26SWANG Xuerui# Also, -mdirect-extern-access is useful in case of building with explicit
8238b10b26SWANG Xuerui# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have
8338b10b26SWANG Xuerui# support though.
8411cd8a64SXi Ruoyaoifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
8538b10b26SWANG Xueruicflags-y			+= $(call cc-option,-mexplicit-relocs)
8638b10b26SWANG XueruiKBUILD_CFLAGS_KERNEL		+= $(call cc-option,-mdirect-extern-access)
87cbfd44bdSWANG RuiKBUILD_CFLAGS_KERNEL		+= $(call cc-option,-fdirect-access-external-data)
8821eb2bfeSWANG RuiKBUILD_AFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
8921eb2bfeSWANG RuiKBUILD_CFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
9011cd8a64SXi Ruoyaoelse
9111cd8a64SXi Ruoyaocflags-y			+= $(call cc-option,-mno-explicit-relocs)
92fa96b57cSHuacai ChenKBUILD_AFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
93fa96b57cSHuacai ChenKBUILD_CFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
94fa96b57cSHuacai ChenKBUILD_AFLAGS_MODULE		+= -Wa,-mla-global-with-abs
95fa96b57cSHuacai ChenKBUILD_CFLAGS_MODULE		+= -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
9611cd8a64SXi Ruoyaoendif
97fa96b57cSHuacai Chen
98cb8a2ef0STiezhu YangKBUILD_AFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
99cb8a2ef0STiezhu YangKBUILD_CFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
100cb8a2ef0STiezhu YangKBUILD_AFLAGS			+= $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub)
101cb8a2ef0STiezhu YangKBUILD_CFLAGS			+= $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub)
102cb8a2ef0STiezhu Yang
103cb8a2ef0STiezhu Yangifdef CONFIG_OBJTOOL
104e20ab7d4STiezhu Yangifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP
105e20ab7d4STiezhu YangKBUILD_CFLAGS			+= -mannotate-tablejump
106e20ab7d4STiezhu Yangelse
107e20ab7d4STiezhu YangKBUILD_CFLAGS			+= -fno-jump-tables # keep compatibility with older compilers
108e20ab7d4STiezhu Yangendif
109cb8a2ef0STiezhu Yangendif
110cb8a2ef0STiezhu Yang
111e67e0eb6SHuacai ChenKBUILD_RUSTFLAGS		+= --target=loongarch64-unknown-none-softfloat -Ccode-model=small
1120d3a6322SWANG RuiKBUILD_RUSTFLAGS_KERNEL		+= -Zdirect-access-external-data=yes
1130d3a6322SWANG RuiKBUILD_RUSTFLAGS_MODULE		+= -Zdirect-access-external-data=no
11490868ff9SWANG Rui
115d8da19fbSYouling Tangifeq ($(CONFIG_RELOCATABLE),y)
116d8da19fbSYouling TangKBUILD_CFLAGS_KERNEL		+= -fPIE
11790868ff9SWANG RuiKBUILD_RUSTFLAGS_KERNEL		+= -Crelocation-model=pie
118eea673e9SWANG RuiLDFLAGS_vmlinux			+= -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs)
119d8da19fbSYouling Tangendif
120d8da19fbSYouling Tang
121fa96b57cSHuacai Chencflags-y += $(call cc-option, -mno-check-zero-division)
1225aa4ac64SQing Zhang
1235aa4ac64SQing Zhangifndef CONFIG_KASAN
1243f301dc2SWANG Xueruicflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
1255aa4ac64SQing Zhangendif
126fa96b57cSHuacai Chen
127fa96b57cSHuacai Chenload-y		= 0x9000000000200000
128fa96b57cSHuacai Chenbootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y)
129fa96b57cSHuacai Chen
13057fc7323SHuacai Chendrivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
13157fc7323SHuacai Chen
132fa96b57cSHuacai ChenKBUILD_AFLAGS	+= $(cflags-y)
133fa96b57cSHuacai ChenKBUILD_CFLAGS	+= $(cflags-y)
134fa96b57cSHuacai ChenKBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y)
135fa96b57cSHuacai Chen
136fa96b57cSHuacai Chen# This is required to get dwarf unwinding tables into .debug_frame
137fa96b57cSHuacai Chen# instead of .eh_frame so we don't discard them.
138fa96b57cSHuacai ChenKBUILD_CFLAGS += -fno-asynchronous-unwind-tables
139fa96b57cSHuacai Chen
14041596803SHuacai Chenifdef CONFIG_ARCH_STRICT_ALIGN
141fa96b57cSHuacai Chen# Don't emit unaligned accesses.
142fa96b57cSHuacai Chen# Not all LoongArch cores support unaligned access, and as kernel we can't
143fa96b57cSHuacai Chen# rely on others to provide emulation for these accesses.
144fa96b57cSHuacai ChenKBUILD_CFLAGS += $(call cc-option,-mstrict-align)
14541596803SHuacai Chenelse
14641596803SHuacai Chen# Optimise for performance on hardware supports unaligned access.
14741596803SHuacai ChenKBUILD_CFLAGS += $(call cc-option,-mno-strict-align)
14841596803SHuacai Chenendif
149fa96b57cSHuacai Chen
150fa96b57cSHuacai ChenKBUILD_CFLAGS += -isystem $(shell $(CC) -print-file-name=include)
151fa96b57cSHuacai Chen
152fa96b57cSHuacai ChenKBUILD_LDFLAGS	+= -m $(ld-emul)
153fa96b57cSHuacai Chen
15454c2c9dfSHuacai Chenifdef need-compiler
1555ddc7a37SWANG XueruiCHECKFLAGS += $(shell $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \
15683f638bcSTiezhu Yang	grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \
157fa96b57cSHuacai Chen	sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g')
158fa96b57cSHuacai Chenendif
159fa96b57cSHuacai Chen
160fa96b57cSHuacai Chenlibs-y += arch/loongarch/lib/
161ead384d9SHuacai Chenlibs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
162fa96b57cSHuacai Chen
1632f164822SMin Zhoudrivers-y		+= arch/loongarch/crypto/
1642f164822SMin Zhou
165366bb35aSHuacai Chen# suspend and hibernation support
166366bb35aSHuacai Chendrivers-$(CONFIG_PM)	+= arch/loongarch/power/
167366bb35aSHuacai Chen
168fa96b57cSHuacai Chenifeq ($(KBUILD_EXTMOD),)
169fa96b57cSHuacai Chenprepare: vdso_prepare
170fa96b57cSHuacai Chenvdso_prepare: prepare0
171fa96b57cSHuacai Chen	$(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h
172fa96b57cSHuacai Chenendif
173fa96b57cSHuacai Chen
17456769ba4SMasahiro Yamadavdso-install-y += arch/loongarch/vdso/vdso.so.dbg
175fa96b57cSHuacai Chen
1765f346a6eSBinbin Zhouall:	$(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS)
177fa96b57cSHuacai Chen
178d3ec75bcSMasahiro Yamadavmlinuz.efi: vmlinux.efi
179d3ec75bcSMasahiro Yamada
180c5d5cba7SArd Biesheuvelvmlinux.elf vmlinux.efi vmlinuz.efi: vmlinux
181ead384d9SHuacai Chen	$(Q)$(MAKE) $(build)=$(boot) $(bootvars-y) $(boot)/$@
182fa96b57cSHuacai Chen
183fa96b57cSHuacai Cheninstall:
18475cffd39SYouling Tang	$(call cmd,install)
185fa96b57cSHuacai Chen
186fa96b57cSHuacai Chendefine archhelp
187*980d4a42SYouling Tang	echo '  vmlinux.elf    - Uncompressed ELF kernel image (arch/loongarch/boot/vmlinux.elf)'
188*980d4a42SYouling Tang	echo '  vmlinux.efi    - Uncompressed EFI kernel image (arch/loongarch/boot/vmlinux.efi)'
189*980d4a42SYouling Tang	echo '  vmlinuz.efi    - GZIP/ZSTD-compressed EFI kernel image (arch/loongarch/boot/vmlinuz.efi)'
190*980d4a42SYouling Tang	echo '                   Default when CONFIG_EFI_ZBOOT=y'
191*980d4a42SYouling Tang	echo '  install        - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
192*980d4a42SYouling Tang	echo '                   (distribution) /sbin/$(INSTALLKERNEL) or install.sh to $$(INSTALL_PATH)'
193fa96b57cSHuacai Chen	echo
194fa96b57cSHuacai Chenendef
195