xref: /openbmc/linux/arch/riscv/Makefile (revision 2a9eb57e)
1# This file is included by the global makefile so that you can add your own
2# architecture-specific flags and dependencies.
3#
4# This file is subject to the terms and conditions of the GNU General Public
5# License.  See the file "COPYING" in the main directory of this archive
6# for more details.
7#
8
9OBJCOPYFLAGS    := -O binary
10LDFLAGS_vmlinux :=
11ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
12	LDFLAGS_vmlinux := --no-relax
13	KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
14	CC_FLAGS_FTRACE := -fpatchable-function-entry=8
15endif
16
17ifeq ($(CONFIG_CMODEL_MEDLOW),y)
18KBUILD_CFLAGS_MODULE += -mcmodel=medany
19endif
20
21export BITS
22ifeq ($(CONFIG_ARCH_RV64I),y)
23	BITS := 64
24	UTS_MACHINE := riscv64
25
26	KBUILD_CFLAGS += -mabi=lp64
27	KBUILD_AFLAGS += -mabi=lp64
28
29	KBUILD_LDFLAGS += -melf64lriscv
30else
31	BITS := 32
32	UTS_MACHINE := riscv32
33
34	KBUILD_CFLAGS += -mabi=ilp32
35	KBUILD_AFLAGS += -mabi=ilp32
36	KBUILD_LDFLAGS += -melf32lriscv
37endif
38
39ifeq ($(CONFIG_LD_IS_LLD),y)
40	KBUILD_CFLAGS += -mno-relax
41	KBUILD_AFLAGS += -mno-relax
42ifndef CONFIG_AS_IS_LLVM
43	KBUILD_CFLAGS += -Wa,-mno-relax
44	KBUILD_AFLAGS += -Wa,-mno-relax
45endif
46endif
47
48# ISA string setting
49riscv-march-$(CONFIG_ARCH_RV32I)	:= rv32ima
50riscv-march-$(CONFIG_ARCH_RV64I)	:= rv64ima
51riscv-march-$(CONFIG_FPU)		:= $(riscv-march-y)fd
52riscv-march-$(CONFIG_RISCV_ISA_C)	:= $(riscv-march-y)c
53
54# Newer binutils versions default to ISA spec version 20191213 which moves some
55# instructions from the I extension to the Zicsr and Zifencei extensions.
56toolchain-need-zicsr-zifencei := $(call cc-option-yn, -march=$(riscv-march-y)_zicsr_zifencei)
57riscv-march-$(toolchain-need-zicsr-zifencei) := $(riscv-march-y)_zicsr_zifencei
58
59# Check if the toolchain supports Zicbom extension
60toolchain-supports-zicbom := $(call cc-option-yn, -march=$(riscv-march-y)_zicbom)
61riscv-march-$(toolchain-supports-zicbom) := $(riscv-march-y)_zicbom
62
63# Check if the toolchain supports Zihintpause extension
64toolchain-supports-zihintpause := $(call cc-option-yn, -march=$(riscv-march-y)_zihintpause)
65riscv-march-$(toolchain-supports-zihintpause) := $(riscv-march-y)_zihintpause
66
67KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y))
68KBUILD_AFLAGS += -march=$(riscv-march-y)
69
70KBUILD_CFLAGS += -mno-save-restore
71KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
72
73ifeq ($(CONFIG_CMODEL_MEDLOW),y)
74	KBUILD_CFLAGS += -mcmodel=medlow
75endif
76ifeq ($(CONFIG_CMODEL_MEDANY),y)
77	KBUILD_CFLAGS += -mcmodel=medany
78endif
79ifeq ($(CONFIG_PERF_EVENTS),y)
80        KBUILD_CFLAGS += -fno-omit-frame-pointer
81endif
82
83KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax)
84KBUILD_AFLAGS_MODULE += $(call as-option,-Wa$(comma)-mno-relax)
85
86# GCC versions that support the "-mstrict-align" option default to allowing
87# unaligned accesses.  While unaligned accesses are explicitly allowed in the
88# RISC-V ISA, they're emulated by machine mode traps on all extant
89# architectures.  It's faster to have GCC emit only aligned accesses.
90KBUILD_CFLAGS += $(call cc-option,-mstrict-align)
91
92ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
93prepare: stack_protector_prepare
94stack_protector_prepare: prepare0
95	$(eval KBUILD_CFLAGS += -mstack-protector-guard=tls		  \
96				-mstack-protector-guard-reg=tp		  \
97				-mstack-protector-guard-offset=$(shell	  \
98			awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \
99					include/generated/asm-offsets.h))
100endif
101
102# arch specific predefines for sparse
103CHECKFLAGS += -D__riscv -D__riscv_xlen=$(BITS)
104
105# Default target when executing plain make
106boot		:= arch/riscv/boot
107ifeq ($(CONFIG_XIP_KERNEL),y)
108KBUILD_IMAGE := $(boot)/xipImage
109else
110KBUILD_IMAGE	:= $(boot)/Image.gz
111endif
112
113head-y := arch/riscv/kernel/head.o
114
115libs-y += arch/riscv/lib/
116libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
117
118PHONY += vdso_install
119vdso_install:
120	$(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso $@
121	$(if $(CONFIG_COMPAT),$(Q)$(MAKE) \
122		$(build)=arch/riscv/kernel/compat_vdso compat_$@)
123
124ifeq ($(KBUILD_EXTMOD),)
125ifeq ($(CONFIG_MMU),y)
126prepare: vdso_prepare
127vdso_prepare: prepare0
128	$(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso include/generated/vdso-offsets.h
129	$(if $(CONFIG_COMPAT),$(Q)$(MAKE) \
130		$(build)=arch/riscv/kernel/compat_vdso include/generated/compat_vdso-offsets.h)
131
132endif
133endif
134
135ifneq ($(CONFIG_XIP_KERNEL),y)
136ifeq ($(CONFIG_RISCV_M_MODE)$(CONFIG_SOC_CANAAN),yy)
137KBUILD_IMAGE := $(boot)/loader.bin
138else
139KBUILD_IMAGE := $(boot)/Image.gz
140endif
141endif
142BOOT_TARGETS := Image Image.gz loader loader.bin xipImage
143
144all:	$(notdir $(KBUILD_IMAGE))
145
146$(BOOT_TARGETS): vmlinux
147	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
148	@$(kecho) '  Kernel: $(boot)/$@ is ready'
149
150Image.%: Image
151	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
152
153install: KBUILD_IMAGE := $(boot)/Image
154zinstall: KBUILD_IMAGE := $(boot)/Image.gz
155install zinstall:
156	$(call cmd,install)
157
158PHONY += rv32_randconfig
159rv32_randconfig:
160	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/32-bit.config \
161		-f $(srctree)/Makefile randconfig
162
163PHONY += rv64_randconfig
164rv64_randconfig:
165	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/64-bit.config \
166		-f $(srctree)/Makefile randconfig
167
168PHONY += rv32_defconfig
169rv32_defconfig:
170	$(Q)$(MAKE) -f $(srctree)/Makefile defconfig 32-bit.config
171