162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#
362306a36Sopenharmony_ci# Author: Huacai Chen <chenhuacai@loongson.cn>
462306a36Sopenharmony_ci# Copyright (C) 2020-2022 Loongson Technology Corporation Limited
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciboot	:= arch/loongarch/boot
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciKBUILD_DEFCONFIG := loongson3_defconfig
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciimage-name-y			:= vmlinux
1162306a36Sopenharmony_ciimage-name-$(CONFIG_EFI_ZBOOT)	:= vmlinuz
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciifndef CONFIG_EFI_STUB
1462306a36Sopenharmony_ciKBUILD_IMAGE	:= $(boot)/vmlinux.elf
1562306a36Sopenharmony_cielse
1662306a36Sopenharmony_ciKBUILD_IMAGE	:= $(boot)/$(image-name-y).efi
1762306a36Sopenharmony_ciendif
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#
2062306a36Sopenharmony_ci# Select the object file format to substitute into the linker script.
2162306a36Sopenharmony_ci#
2262306a36Sopenharmony_ci64bit-tool-archpref	= loongarch64
2362306a36Sopenharmony_ci32bit-bfd		= elf32-loongarch
2462306a36Sopenharmony_ci64bit-bfd		= elf64-loongarch
2562306a36Sopenharmony_ci32bit-emul		= elf32loongarch
2662306a36Sopenharmony_ci64bit-emul		= elf64loongarch
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciifdef CONFIG_DYNAMIC_FTRACE
2962306a36Sopenharmony_ciKBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
3062306a36Sopenharmony_ciCC_FLAGS_FTRACE := -fpatchable-function-entry=2
3162306a36Sopenharmony_ciendif
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciifdef CONFIG_64BIT
3462306a36Sopenharmony_citool-archpref		= $(64bit-tool-archpref)
3562306a36Sopenharmony_ciUTS_MACHINE		:= loongarch64
3662306a36Sopenharmony_ciendif
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciifneq ($(SUBARCH),$(ARCH))
3962306a36Sopenharmony_ci  ifeq ($(CROSS_COMPILE),)
4062306a36Sopenharmony_ci    CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux-  $(tool-archpref)-linux-gnu-  $(tool-archpref)-unknown-linux-gnu-)
4162306a36Sopenharmony_ci  endif
4262306a36Sopenharmony_ciendif
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciifdef CONFIG_64BIT
4562306a36Sopenharmony_cild-emul			= $(64bit-emul)
4662306a36Sopenharmony_cicflags-y		+= -mabi=lp64s
4762306a36Sopenharmony_ciendif
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cicflags-y			+= -pipe -msoft-float
5062306a36Sopenharmony_ciLDFLAGS_vmlinux			+= -static -n -nostdlib
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci# When the assembler supports explicit relocation hint, we must use it.
5362306a36Sopenharmony_ci# GCC may have -mexplicit-relocs off by default if it was built with an old
5462306a36Sopenharmony_ci# assembler, so we force it via an option.
5562306a36Sopenharmony_ci#
5662306a36Sopenharmony_ci# When the assembler does not supports explicit relocation hint, we can't use
5762306a36Sopenharmony_ci# it.  Disable it if the compiler supports it.
5862306a36Sopenharmony_ci#
5962306a36Sopenharmony_ci# The combination of a "new" assembler and "old" GCC is not supported, given
6062306a36Sopenharmony_ci# the rarity of this combo and the extra complexity needed to make it work.
6162306a36Sopenharmony_ci# Either upgrade the compiler or downgrade the assembler; the build will error
6262306a36Sopenharmony_ci# out if it is the case (by probing for the model attribute; all supported
6362306a36Sopenharmony_ci# compilers in this case would have support).
6462306a36Sopenharmony_ci#
6562306a36Sopenharmony_ci# Also, -mdirect-extern-access is useful in case of building with explicit
6662306a36Sopenharmony_ci# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have
6762306a36Sopenharmony_ci# support though.
6862306a36Sopenharmony_ciifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
6962306a36Sopenharmony_cicflags-y			+= $(call cc-option,-mexplicit-relocs)
7062306a36Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= $(call cc-option,-mdirect-extern-access)
7162306a36Sopenharmony_ciKBUILD_AFLAGS_MODULE		+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
7262306a36Sopenharmony_ciKBUILD_CFLAGS_MODULE		+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
7362306a36Sopenharmony_cielse
7462306a36Sopenharmony_cicflags-y			+= $(call cc-option,-mno-explicit-relocs)
7562306a36Sopenharmony_ciKBUILD_AFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
7662306a36Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
7762306a36Sopenharmony_ciKBUILD_AFLAGS_MODULE		+= -Wa,-mla-global-with-abs
7862306a36Sopenharmony_ciKBUILD_CFLAGS_MODULE		+= -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
7962306a36Sopenharmony_ciendif
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciifeq ($(CONFIG_RELOCATABLE),y)
8262306a36Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= -fPIE
8362306a36Sopenharmony_ciLDFLAGS_vmlinux			+= -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs)
8462306a36Sopenharmony_ciendif
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cicflags-y += $(call cc-option, -mno-check-zero-division)
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciifndef CONFIG_KASAN
8962306a36Sopenharmony_cicflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
9062306a36Sopenharmony_ciendif
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciload-y		= 0x9000000000200000
9362306a36Sopenharmony_cibootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y)
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cidrivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciKBUILD_AFLAGS	+= $(cflags-y)
9862306a36Sopenharmony_ciKBUILD_CFLAGS	+= $(cflags-y)
9962306a36Sopenharmony_ciKBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci# This is required to get dwarf unwinding tables into .debug_frame
10262306a36Sopenharmony_ci# instead of .eh_frame so we don't discard them.
10362306a36Sopenharmony_ciKBUILD_CFLAGS += -fno-asynchronous-unwind-tables
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciifdef CONFIG_ARCH_STRICT_ALIGN
10662306a36Sopenharmony_ci# Don't emit unaligned accesses.
10762306a36Sopenharmony_ci# Not all LoongArch cores support unaligned access, and as kernel we can't
10862306a36Sopenharmony_ci# rely on others to provide emulation for these accesses.
10962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-mstrict-align)
11062306a36Sopenharmony_cielse
11162306a36Sopenharmony_ci# Optimise for performance on hardware supports unaligned access.
11262306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-mno-strict-align)
11362306a36Sopenharmony_ciendif
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciKBUILD_CFLAGS += -isystem $(shell $(CC) -print-file-name=include)
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciKBUILD_LDFLAGS	+= -m $(ld-emul)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciifdef need-compiler
12062306a36Sopenharmony_ciCHECKFLAGS += $(shell $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \
12162306a36Sopenharmony_ci	grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \
12262306a36Sopenharmony_ci	sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g')
12362306a36Sopenharmony_ciendif
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cilibs-y += arch/loongarch/lib/
12662306a36Sopenharmony_cilibs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cidrivers-y		+= arch/loongarch/crypto/
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci# suspend and hibernation support
13162306a36Sopenharmony_cidrivers-$(CONFIG_PM)	+= arch/loongarch/power/
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
13462306a36Sopenharmony_ciprepare: vdso_prepare
13562306a36Sopenharmony_civdso_prepare: prepare0
13662306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h
13762306a36Sopenharmony_ciendif
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciPHONY += vdso_install
14062306a36Sopenharmony_civdso_install:
14162306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/vdso $@
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciall:	$(notdir $(KBUILD_IMAGE))
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_civmlinuz.efi: vmlinux.efi
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_civmlinux.elf vmlinux.efi vmlinuz.efi: vmlinux
14862306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=$(boot) $(bootvars-y) $(boot)/$@
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciinstall:
15162306a36Sopenharmony_ci	$(Q)install -D -m 755 $(KBUILD_IMAGE) $(INSTALL_PATH)/$(image-name-y)-$(KERNELRELEASE)
15262306a36Sopenharmony_ci	$(Q)install -D -m 644 .config $(INSTALL_PATH)/config-$(KERNELRELEASE)
15362306a36Sopenharmony_ci	$(Q)install -D -m 644 System.map $(INSTALL_PATH)/System.map-$(KERNELRELEASE)
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cidefine archhelp
15662306a36Sopenharmony_ci	echo '  install              - install kernel into $(INSTALL_PATH)'
15762306a36Sopenharmony_ci	echo
15862306a36Sopenharmony_ciendef
159