18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#
38c2ecf20Sopenharmony_ci# Author: Huacai Chen <chenhuacai@loongson.cn>
48c2ecf20Sopenharmony_ci# Copyright (C) 2020 Loongson Technology Corporation Limited
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciarchscripts: scripts_basic
78c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/tools elf-entry
88c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/boot/tools relocs
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciKBUILD_DEFCONFIG := loongson3_defconfig
118c2ecf20Sopenharmony_ciKBUILD_DTBS      := dtbs
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#
148c2ecf20Sopenharmony_ci# Select the object file format to substitute into the linker script.
158c2ecf20Sopenharmony_ci#
168c2ecf20Sopenharmony_ci64bit-tool-archpref	= loongarch64
178c2ecf20Sopenharmony_ci32bit-bfd		= elf32-loongarch
188c2ecf20Sopenharmony_ci64bit-bfd		= elf64-loongarch
198c2ecf20Sopenharmony_ci32bit-emul		= elf32loongarch
208c2ecf20Sopenharmony_ci64bit-emul		= elf64loongarch
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciifdef CONFIG_64BIT
238c2ecf20Sopenharmony_citool-archpref		= $(64bit-tool-archpref)
248c2ecf20Sopenharmony_ciUTS_MACHINE		:= loongarch64
258c2ecf20Sopenharmony_ciendif
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciifneq ($(SUBARCH),$(ARCH))
288c2ecf20Sopenharmony_ci  ifeq ($(CROSS_COMPILE),)
298c2ecf20Sopenharmony_ci    CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux-  $(tool-archpref)-linux-gnu-  $(tool-archpref)-unknown-linux-gnu-)
308c2ecf20Sopenharmony_ci  endif
318c2ecf20Sopenharmony_ciendif
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciifdef CONFIG_DYNAMIC_FTRACE
348c2ecf20Sopenharmony_ci  KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
358c2ecf20Sopenharmony_ci  CC_FLAGS_FTRACE := -fpatchable-function-entry=2
368c2ecf20Sopenharmony_ciendif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cicflags-y += $(call cc-option, -mno-check-zero-division)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciifdef CONFIG_64BIT
418c2ecf20Sopenharmony_cild-emul			= $(64bit-emul)
428c2ecf20Sopenharmony_cicflags-y		+= -mabi=lp64s
438c2ecf20Sopenharmony_ciendif
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciall-y			:= vmlinux
468c2ecf20Sopenharmony_ciall-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlinuz
478c2ecf20Sopenharmony_ciall-$(CONFIG_EFI_STUB) 	+= vmlinuz.efi
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_civmlinuz.efi: vmlinuz
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cicflags-y			+= -pipe -msoft-float
528c2ecf20Sopenharmony_ciLDFLAGS_vmlinux			+= -static -n -nostdlib
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci# When the assembler supports explicit relocation hint, we must use it.
558c2ecf20Sopenharmony_ci# GCC may have -mexplicit-relocs off by default if it was built with an old
568c2ecf20Sopenharmony_ci# assembler, so we force it via an option.
578c2ecf20Sopenharmony_ci#
588c2ecf20Sopenharmony_ci# When the assembler does not supports explicit relocation hint, we can't use
598c2ecf20Sopenharmony_ci# it.  Disable it if the compiler supports it.
608c2ecf20Sopenharmony_ci#
618c2ecf20Sopenharmony_ci# The combination of a "new" assembler and "old" GCC is not supported, given
628c2ecf20Sopenharmony_ci# the rarity of this combo and the extra complexity needed to make it work.
638c2ecf20Sopenharmony_ci# Either upgrade the compiler or downgrade the assembler; the build will error
648c2ecf20Sopenharmony_ci# out if it is the case (by probing for the model attribute; all supported
658c2ecf20Sopenharmony_ci# compilers in this case would have support).
668c2ecf20Sopenharmony_ci#
678c2ecf20Sopenharmony_ci# Also, -mdirect-extern-access is useful in case of building with explicit
688c2ecf20Sopenharmony_ci# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have
698c2ecf20Sopenharmony_ci# support though.
708c2ecf20Sopenharmony_ciifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
718c2ecf20Sopenharmony_cicflags-y			+= $(call cc-option,-mexplicit-relocs)
728c2ecf20Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= $(call cc-option,-mdirect-extern-access)
738c2ecf20Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= $(call cc-option,-fdirect-access-external-data)
748c2ecf20Sopenharmony_ciKBUILD_AFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
758c2ecf20Sopenharmony_ciKBUILD_CFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
768c2ecf20Sopenharmony_cielse
778c2ecf20Sopenharmony_cicflags-y			+= $(call cc-option,-mno-explicit-relocs)
788c2ecf20Sopenharmony_ciKBUILD_AFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
798c2ecf20Sopenharmony_ciKBUILD_CFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
808c2ecf20Sopenharmony_ciKBUILD_AFLAGS_MODULE		+= -Wa,-mla-global-with-abs
818c2ecf20Sopenharmony_ciKBUILD_CFLAGS_MODULE		+= -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
828c2ecf20Sopenharmony_ciendif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciKBUILD_AFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
858c2ecf20Sopenharmony_ciKBUILD_CFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciifeq ($(CONFIG_RELOCATABLE),y)
888c2ecf20Sopenharmony_ciLDFLAGS_vmlinux		+= --emit-relocs
898c2ecf20Sopenharmony_ciendif
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cicflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#
948c2ecf20Sopenharmony_ci# Kernel compression
958c2ecf20Sopenharmony_ci#
968c2ecf20Sopenharmony_ciifdef CONFIG_SYS_SUPPORTS_ZBOOT
978c2ecf20Sopenharmony_ciKBUILD_IMAGE	= vmlinuz
988c2ecf20Sopenharmony_cielse
998c2ecf20Sopenharmony_ciKBUILD_IMAGE	= vmlinux
1008c2ecf20Sopenharmony_ciendif
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#
1038c2ecf20Sopenharmony_ci# Board-dependent options and extra files
1048c2ecf20Sopenharmony_ci#
1058c2ecf20Sopenharmony_ciinclude arch/loongarch/Kbuild.platforms
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cientry-y				= $(shell $(objtree)/arch/loongarch/tools/elf-entry vmlinux)
1088c2ecf20Sopenharmony_cidrivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciKBUILD_AFLAGS	+= $(cflags-y)
1118c2ecf20Sopenharmony_ciKBUILD_CFLAGS	+= $(cflags-y)
1128c2ecf20Sopenharmony_ciKBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cibootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y) \
1158c2ecf20Sopenharmony_ci		  VMLINUX_ENTRY_ADDRESS=$(entry-y) PLATFORM="$(platform-y)"
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciifdef CONFIG_64BIT
1188c2ecf20Sopenharmony_cibootvars-y	+= ADDR_BITS=64
1198c2ecf20Sopenharmony_ciendif
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci# This is required to get dwarf unwinding tables into .debug_frame
1228c2ecf20Sopenharmony_ci# instead of .eh_frame so we don't discard them.
1238c2ecf20Sopenharmony_ciKBUILD_CFLAGS += -fno-asynchronous-unwind-tables
1248c2ecf20Sopenharmony_ciifdef CONFIG_ARCH_STRICT_ALIGN
1258c2ecf20Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-mstrict-align)
1268c2ecf20Sopenharmony_cielse
1278c2ecf20Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-mno-strict-align)
1288c2ecf20Sopenharmony_ciendif
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciKBUILD_LDFLAGS	+= -m $(ld-emul)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciifdef CONFIG_LOONGARCH
1338c2ecf20Sopenharmony_ciCHECKFLAGS += $(shell $(CC) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \
1348c2ecf20Sopenharmony_ci	grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \
1358c2ecf20Sopenharmony_ci	sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g')
1368c2ecf20Sopenharmony_ciendif
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cihead-y := arch/loongarch/kernel/head.o
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cilibs-y += arch/loongarch/lib/
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci# See arch/loongarch/Kbuild for content of core part of the kernel
1438c2ecf20Sopenharmony_cicore-y += arch/loongarch/
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cidrivers-y		+= arch/loongarch/crypto/
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci# suspend and hibernation support
1488c2ecf20Sopenharmony_cidrivers-$(CONFIG_PM)	+= arch/loongarch/power/
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
1518c2ecf20Sopenharmony_ciprepare: vdso_prepare
1528c2ecf20Sopenharmony_civdso_prepare: prepare0
1538c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h
1548c2ecf20Sopenharmony_ciendif
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciPHONY += vdso_install
1578c2ecf20Sopenharmony_civdso_install:
1588c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/vdso $@
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci# boot image targets (arch/loongarch/boot/)
1618c2ecf20Sopenharmony_ciboot-y			:= vmlinux.bin
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci# compressed boot image targets (arch/loongarch/boot/compressed/)
1648c2ecf20Sopenharmony_cibootz-y			:= vmlinuz
1658c2ecf20Sopenharmony_cibootz-y			+= vmlinuz.bin
1668c2ecf20Sopenharmony_cibootz-$(CONFIG_EFI_STUB)+= vmlinuz.efi
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciall:	$(all-y) $(KBUILD_DTBS)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci# boot
1718c2ecf20Sopenharmony_ci$(boot-y): vmlinux FORCE
1728c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/boot VMLINUX=vmlinux \
1738c2ecf20Sopenharmony_ci		$(bootvars-y) arch/loongarch/boot/$@
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciifdef CONFIG_SYS_SUPPORTS_ZBOOT
1768c2ecf20Sopenharmony_ci# boot/compressed
1778c2ecf20Sopenharmony_ci$(bootz-y): vmlinux FORCE
1788c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(build)=arch/loongarch/boot/compressed \
1798c2ecf20Sopenharmony_ci		$(bootvars-y) 64bit-bfd=$(64bit-bfd) $@
1808c2ecf20Sopenharmony_cielse
1818c2ecf20Sopenharmony_civmlinuz: FORCE
1828c2ecf20Sopenharmony_ci	@echo '   CONFIG_SYS_SUPPORTS_ZBOOT is not enabled'
1838c2ecf20Sopenharmony_ci	/bin/false
1848c2ecf20Sopenharmony_ciendif
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciCLEAN_FILES += vmlinux
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciinstall:
1898c2ecf20Sopenharmony_ci	$(Q)install -D -m 755 vmlinux $(INSTALL_PATH)/vmlinux-$(KERNELRELEASE)
1908c2ecf20Sopenharmony_ciifdef CONFIG_SYS_SUPPORTS_ZBOOT
1918c2ecf20Sopenharmony_ci	$(Q)install -D -m 755 vmlinuz $(INSTALL_PATH)/vmlinuz-$(KERNELRELEASE)
1928c2ecf20Sopenharmony_ciendif
1938c2ecf20Sopenharmony_ci	$(Q)install -D -m 644 .config $(INSTALL_PATH)/config-$(KERNELRELEASE)
1948c2ecf20Sopenharmony_ci	$(Q)install -D -m 644 System.map $(INSTALL_PATH)/System.map-$(KERNELRELEASE)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciarchclean:
1978c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(clean)=arch/loongarch/boot
1988c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(clean)=arch/loongarch/boot/compressed
1998c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(clean)=arch/loongarch/boot/tools
2008c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(clean)=arch/loongarch/tools
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cidefine archhelp
2038c2ecf20Sopenharmony_ci	echo '  install              - install kernel into $(INSTALL_PATH)'
2048c2ecf20Sopenharmony_ci	echo '  vmlinux.bin          - Raw binary boot image'
2058c2ecf20Sopenharmony_ci	echo '  vmlinuz              - Compressed boot(zboot) image'
2068c2ecf20Sopenharmony_ci	echo '  vmlinuz.bin          - Raw binary zboot image'
2078c2ecf20Sopenharmony_ci	echo
2088c2ecf20Sopenharmony_ciendef
209