1# SPDX-License-Identifier: GPL-2.0
2#
3# Author: Huacai Chen <chenhuacai@loongson.cn>
4# Copyright (C) 2020 Loongson Technology Corporation Limited
5
6archscripts: scripts_basic
7	$(Q)$(MAKE) $(build)=arch/loongarch/tools elf-entry
8	$(Q)$(MAKE) $(build)=arch/loongarch/boot/tools relocs
9
10KBUILD_DEFCONFIG := loongson3_defconfig
11KBUILD_DTBS      := dtbs
12
13#
14# Select the object file format to substitute into the linker script.
15#
1664bit-tool-archpref	= loongarch64
1732bit-bfd		= elf32-loongarch
1864bit-bfd		= elf64-loongarch
1932bit-emul		= elf32loongarch
2064bit-emul		= elf64loongarch
21
22ifdef CONFIG_64BIT
23tool-archpref		= $(64bit-tool-archpref)
24UTS_MACHINE		:= loongarch64
25endif
26
27ifneq ($(SUBARCH),$(ARCH))
28  ifeq ($(CROSS_COMPILE),)
29    CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux-  $(tool-archpref)-linux-gnu-  $(tool-archpref)-unknown-linux-gnu-)
30  endif
31endif
32
33ifdef CONFIG_DYNAMIC_FTRACE
34  KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
35  CC_FLAGS_FTRACE := -fpatchable-function-entry=2
36endif
37
38cflags-y += $(call cc-option, -mno-check-zero-division)
39
40ifdef CONFIG_64BIT
41ld-emul			= $(64bit-emul)
42cflags-y		+= -mabi=lp64s
43endif
44
45all-y			:= vmlinux
46all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlinuz
47all-$(CONFIG_EFI_STUB) 	+= vmlinuz.efi
48
49vmlinuz.efi: vmlinuz
50
51cflags-y			+= -pipe -msoft-float
52LDFLAGS_vmlinux			+= -static -n -nostdlib
53
54# When the assembler supports explicit relocation hint, we must use it.
55# GCC may have -mexplicit-relocs off by default if it was built with an old
56# assembler, so we force it via an option.
57#
58# When the assembler does not supports explicit relocation hint, we can't use
59# it.  Disable it if the compiler supports it.
60#
61# The combination of a "new" assembler and "old" GCC is not supported, given
62# the rarity of this combo and the extra complexity needed to make it work.
63# Either upgrade the compiler or downgrade the assembler; the build will error
64# out if it is the case (by probing for the model attribute; all supported
65# compilers in this case would have support).
66#
67# Also, -mdirect-extern-access is useful in case of building with explicit
68# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have
69# support though.
70ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
71cflags-y			+= $(call cc-option,-mexplicit-relocs)
72KBUILD_CFLAGS_KERNEL		+= $(call cc-option,-mdirect-extern-access)
73KBUILD_CFLAGS_KERNEL		+= $(call cc-option,-fdirect-access-external-data)
74KBUILD_AFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
75KBUILD_CFLAGS_MODULE		+= $(call cc-option,-fno-direct-access-external-data)
76else
77cflags-y			+= $(call cc-option,-mno-explicit-relocs)
78KBUILD_AFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
79KBUILD_CFLAGS_KERNEL		+= -Wa,-mla-global-with-pcrel
80KBUILD_AFLAGS_MODULE		+= -Wa,-mla-global-with-abs
81KBUILD_CFLAGS_MODULE		+= -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
82endif
83
84KBUILD_AFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
85KBUILD_CFLAGS			+= $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
86
87ifeq ($(CONFIG_RELOCATABLE),y)
88LDFLAGS_vmlinux		+= --emit-relocs
89endif
90
91cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
92
93#
94# Kernel compression
95#
96ifdef CONFIG_SYS_SUPPORTS_ZBOOT
97KBUILD_IMAGE	= vmlinuz
98else
99KBUILD_IMAGE	= vmlinux
100endif
101
102#
103# Board-dependent options and extra files
104#
105include arch/loongarch/Kbuild.platforms
106
107entry-y				= $(shell $(objtree)/arch/loongarch/tools/elf-entry vmlinux)
108drivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
109
110KBUILD_AFLAGS	+= $(cflags-y)
111KBUILD_CFLAGS	+= $(cflags-y)
112KBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y)
113
114bootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y) \
115		  VMLINUX_ENTRY_ADDRESS=$(entry-y) PLATFORM="$(platform-y)"
116
117ifdef CONFIG_64BIT
118bootvars-y	+= ADDR_BITS=64
119endif
120
121# This is required to get dwarf unwinding tables into .debug_frame
122# instead of .eh_frame so we don't discard them.
123KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
124ifdef CONFIG_ARCH_STRICT_ALIGN
125KBUILD_CFLAGS += $(call cc-option,-mstrict-align)
126else
127KBUILD_CFLAGS += $(call cc-option,-mno-strict-align)
128endif
129
130KBUILD_LDFLAGS	+= -m $(ld-emul)
131
132ifdef CONFIG_LOONGARCH
133CHECKFLAGS += $(shell $(CC) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \
134	grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \
135	sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g')
136endif
137
138head-y := arch/loongarch/kernel/head.o
139
140libs-y += arch/loongarch/lib/
141
142# See arch/loongarch/Kbuild for content of core part of the kernel
143core-y += arch/loongarch/
144
145drivers-y		+= arch/loongarch/crypto/
146
147# suspend and hibernation support
148drivers-$(CONFIG_PM)	+= arch/loongarch/power/
149
150ifeq ($(KBUILD_EXTMOD),)
151prepare: vdso_prepare
152vdso_prepare: prepare0
153	$(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h
154endif
155
156PHONY += vdso_install
157vdso_install:
158	$(Q)$(MAKE) $(build)=arch/loongarch/vdso $@
159
160# boot image targets (arch/loongarch/boot/)
161boot-y			:= vmlinux.bin
162
163# compressed boot image targets (arch/loongarch/boot/compressed/)
164bootz-y			:= vmlinuz
165bootz-y			+= vmlinuz.bin
166bootz-$(CONFIG_EFI_STUB)+= vmlinuz.efi
167
168all:	$(all-y) $(KBUILD_DTBS)
169
170# boot
171$(boot-y): vmlinux FORCE
172	$(Q)$(MAKE) $(build)=arch/loongarch/boot VMLINUX=vmlinux \
173		$(bootvars-y) arch/loongarch/boot/$@
174
175ifdef CONFIG_SYS_SUPPORTS_ZBOOT
176# boot/compressed
177$(bootz-y): vmlinux FORCE
178	$(Q)$(MAKE) $(build)=arch/loongarch/boot/compressed \
179		$(bootvars-y) 64bit-bfd=$(64bit-bfd) $@
180else
181vmlinuz: FORCE
182	@echo '   CONFIG_SYS_SUPPORTS_ZBOOT is not enabled'
183	/bin/false
184endif
185
186CLEAN_FILES += vmlinux
187
188install:
189	$(Q)install -D -m 755 vmlinux $(INSTALL_PATH)/vmlinux-$(KERNELRELEASE)
190ifdef CONFIG_SYS_SUPPORTS_ZBOOT
191	$(Q)install -D -m 755 vmlinuz $(INSTALL_PATH)/vmlinuz-$(KERNELRELEASE)
192endif
193	$(Q)install -D -m 644 .config $(INSTALL_PATH)/config-$(KERNELRELEASE)
194	$(Q)install -D -m 644 System.map $(INSTALL_PATH)/System.map-$(KERNELRELEASE)
195
196archclean:
197	$(Q)$(MAKE) $(clean)=arch/loongarch/boot
198	$(Q)$(MAKE) $(clean)=arch/loongarch/boot/compressed
199	$(Q)$(MAKE) $(clean)=arch/loongarch/boot/tools
200	$(Q)$(MAKE) $(clean)=arch/loongarch/tools
201
202define archhelp
203	echo '  install              - install kernel into $(INSTALL_PATH)'
204	echo '  vmlinux.bin          - Raw binary boot image'
205	echo '  vmlinuz              - Compressed boot(zboot) image'
206	echo '  vmlinuz.bin          - Raw binary zboot image'
207	echo
208endef
209