xref: /linux/arch/loongarch/Makefile (revision 1c656b1efde6ce86a6c810d27a5f925e938d568d)
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
105*5dfea664STiezhu Yang# The annotate-tablejump option can not be passed to LLVM backend when LTO is enabled.
106*5dfea664STiezhu Yang# Ensure it is aware of linker with LTO, '--loongarch-annotate-tablejump' also needs to
107*5dfea664STiezhu Yang# be passed via '-mllvm' to ld.lld.
108e20ab7d4STiezhu YangKBUILD_CFLAGS			+= -mannotate-tablejump
109*5dfea664STiezhu Yangifdef CONFIG_LTO_CLANG
110*5dfea664STiezhu YangKBUILD_LDFLAGS			+= -mllvm --loongarch-annotate-tablejump
111*5dfea664STiezhu Yangendif
112e20ab7d4STiezhu Yangelse
113e20ab7d4STiezhu YangKBUILD_CFLAGS			+= -fno-jump-tables # keep compatibility with older compilers
114e20ab7d4STiezhu Yangendif
115cb8a2ef0STiezhu Yangendif
116cb8a2ef0STiezhu Yang
117e67e0eb6SHuacai ChenKBUILD_RUSTFLAGS		+= --target=loongarch64-unknown-none-softfloat -Ccode-model=small
1180d3a6322SWANG RuiKBUILD_RUSTFLAGS_KERNEL		+= -Zdirect-access-external-data=yes
1190d3a6322SWANG RuiKBUILD_RUSTFLAGS_MODULE		+= -Zdirect-access-external-data=no
12090868ff9SWANG Rui
121d8da19fbSYouling Tangifeq ($(CONFIG_RELOCATABLE),y)
122d8da19fbSYouling TangKBUILD_CFLAGS_KERNEL		+= -fPIE
12390868ff9SWANG RuiKBUILD_RUSTFLAGS_KERNEL		+= -Crelocation-model=pie
124eea673e9SWANG RuiLDFLAGS_vmlinux			+= -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs)
125d8da19fbSYouling Tangendif
126d8da19fbSYouling Tang
127fa96b57cSHuacai Chencflags-y += $(call cc-option, -mno-check-zero-division)
1285aa4ac64SQing Zhang
1295aa4ac64SQing Zhangifndef CONFIG_KASAN
1303f301dc2SWANG Xueruicflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
1315aa4ac64SQing Zhangendif
132fa96b57cSHuacai Chen
133fa96b57cSHuacai Chenload-y		= 0x9000000000200000
134fa96b57cSHuacai Chenbootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y)
135fa96b57cSHuacai Chen
13657fc7323SHuacai Chendrivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
13757fc7323SHuacai Chen
138fa96b57cSHuacai ChenKBUILD_AFLAGS	+= $(cflags-y)
139fa96b57cSHuacai ChenKBUILD_CFLAGS	+= $(cflags-y)
140fa96b57cSHuacai ChenKBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y)
141fa96b57cSHuacai Chen
142fa96b57cSHuacai Chen# This is required to get dwarf unwinding tables into .debug_frame
143fa96b57cSHuacai Chen# instead of .eh_frame so we don't discard them.
144fa96b57cSHuacai ChenKBUILD_CFLAGS += -fno-asynchronous-unwind-tables
145fa96b57cSHuacai Chen
14641596803SHuacai Chenifdef CONFIG_ARCH_STRICT_ALIGN
147fa96b57cSHuacai Chen# Don't emit unaligned accesses.
148fa96b57cSHuacai Chen# Not all LoongArch cores support unaligned access, and as kernel we can't
149fa96b57cSHuacai Chen# rely on others to provide emulation for these accesses.
150fa96b57cSHuacai ChenKBUILD_CFLAGS += $(call cc-option,-mstrict-align)
15141596803SHuacai Chenelse
15241596803SHuacai Chen# Optimise for performance on hardware supports unaligned access.
15341596803SHuacai ChenKBUILD_CFLAGS += $(call cc-option,-mno-strict-align)
15441596803SHuacai Chenendif
155fa96b57cSHuacai Chen
156fa96b57cSHuacai ChenKBUILD_CFLAGS += -isystem $(shell $(CC) -print-file-name=include)
157fa96b57cSHuacai Chen
158fa96b57cSHuacai ChenKBUILD_LDFLAGS	+= -m $(ld-emul)
159fa96b57cSHuacai Chen
16054c2c9dfSHuacai Chenifdef need-compiler
1615ddc7a37SWANG XueruiCHECKFLAGS += $(shell $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \
16283f638bcSTiezhu Yang	grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \
163fa96b57cSHuacai Chen	sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g')
164fa96b57cSHuacai Chenendif
165fa96b57cSHuacai Chen
166fa96b57cSHuacai Chenlibs-y += arch/loongarch/lib/
167ead384d9SHuacai Chenlibs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
168fa96b57cSHuacai Chen
1692f164822SMin Zhoudrivers-y		+= arch/loongarch/crypto/
1702f164822SMin Zhou
171366bb35aSHuacai Chen# suspend and hibernation support
172366bb35aSHuacai Chendrivers-$(CONFIG_PM)	+= arch/loongarch/power/
173366bb35aSHuacai Chen
174fa96b57cSHuacai Chenifeq ($(KBUILD_EXTMOD),)
175fa96b57cSHuacai Chenprepare: vdso_prepare
176fa96b57cSHuacai Chenvdso_prepare: prepare0
177fa96b57cSHuacai Chen	$(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h
178fa96b57cSHuacai Chenendif
179fa96b57cSHuacai Chen
18056769ba4SMasahiro Yamadavdso-install-y += arch/loongarch/vdso/vdso.so.dbg
181fa96b57cSHuacai Chen
1825f346a6eSBinbin Zhouall:	$(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS)
183fa96b57cSHuacai Chen
184d3ec75bcSMasahiro Yamadavmlinuz.efi: vmlinux.efi
185d3ec75bcSMasahiro Yamada
186c5d5cba7SArd Biesheuvelvmlinux.elf vmlinux.efi vmlinuz.efi: vmlinux
187ead384d9SHuacai Chen	$(Q)$(MAKE) $(build)=$(boot) $(bootvars-y) $(boot)/$@
188fa96b57cSHuacai Chen
189fa96b57cSHuacai Cheninstall:
19075cffd39SYouling Tang	$(call cmd,install)
191fa96b57cSHuacai Chen
192fa96b57cSHuacai Chendefine archhelp
193980d4a42SYouling Tang	echo '  vmlinux.elf    - Uncompressed ELF kernel image (arch/loongarch/boot/vmlinux.elf)'
194980d4a42SYouling Tang	echo '  vmlinux.efi    - Uncompressed EFI kernel image (arch/loongarch/boot/vmlinux.efi)'
195980d4a42SYouling Tang	echo '  vmlinuz.efi    - GZIP/ZSTD-compressed EFI kernel image (arch/loongarch/boot/vmlinuz.efi)'
196980d4a42SYouling Tang	echo '                   Default when CONFIG_EFI_ZBOOT=y'
197980d4a42SYouling Tang	echo '  install        - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
198980d4a42SYouling Tang	echo '                   (distribution) /sbin/$(INSTALLKERNEL) or install.sh to $$(INSTALL_PATH)'
199fa96b57cSHuacai Chen	echo
200fa96b57cSHuacai Chenendef
201