xref: /open-nvidia-gpu/kernel-open/Kbuild (revision 91676d66)
1###########################################################################
2# Kbuild file for NVIDIA Linux GPU driver kernel modules
3###########################################################################
4
5#
6# The parent makefile is expected to define:
7#
8# NV_KERNEL_SOURCES : The root of the kernel source tree.
9# NV_KERNEL_OUTPUT : The kernel's output tree.
10# NV_KERNEL_MODULES : A whitespace-separated list of modules to build.
11# ARCH : The target CPU architecture: x86_64|arm64|powerpc
12#
13# Kbuild provides the variables:
14#
15# $(src) : The directory containing this Kbuild file.
16# $(obj) : The directory where the output from this build is written.
17#
18
19NV_BUILD_TYPE ?= release
20
21#
22# Utility macro ASSIGN_PER_OBJ_CFLAGS: to control CFLAGS on a
23# per-object basis, Kbuild honors the 'CFLAGS_$(object)' variable.
24# E.g., "CFLAGS_nv.o" for CFLAGS that are specific to nv.o. Use this
25# macro to assign 'CFLAGS_$(object)' variables for multiple object
26# files.
27#
28# $(1): The object files.
29# $(2): The CFLAGS to add for those object files.
30#
31# With kernel git commit 54b8ae66ae1a3454a7645d159a482c31cd89ab33, the
32# handling of object-specific CFLAGs, CFLAGS_$(object) has changed. Prior to
33# this commit, the CFLAGS_$(object) variable was required to be defined with
34# only the the object name (<CFLAGS_somefile.o>). With the aforementioned git
35# commit, it is now required to give Kbuild relative paths along-with the
36# object name (CFLAGS_<somepath>/somefile.o>). As a result, CFLAGS_$(object)
37# is set twice, once with a relative path to the object files and once with
38# just the object files.
39#
40ASSIGN_PER_OBJ_CFLAGS = \
41 $(foreach _cflags_variable, \
42 $(notdir $(1)) $(1), \
43 $(eval $(addprefix CFLAGS_,$(_cflags_variable)) += $(2)))
44
45
46#
47# Include the specifics of the individual NVIDIA kernel modules.
48#
49# Each of these should:
50# - Append to 'obj-m', to indicate the kernel module that should be built.
51# - Define the object files that should get built to produce the kernel module.
52# - Tie into conftest (see the description below).
53#
54
55NV_UNDEF_BEHAVIOR_SANITIZER ?=
56ifeq ($(NV_UNDEF_BEHAVIOR_SANITIZER),1)
57 UBSAN_SANITIZE := y
58endif
59
60$(foreach _module, $(NV_KERNEL_MODULES), \
61 $(eval include $(src)/$(_module)/$(_module).Kbuild))
62
63
64#
65# Define CFLAGS that apply to all the NVIDIA kernel modules. EXTRA_CFLAGS
66# is deprecated since 2.6.24 in favor of ccflags-y, but we need to support
67# older kernels which do not have ccflags-y. Newer kernels append
68# $(EXTRA_CFLAGS) to ccflags-y for compatibility.
69#
70
71EXTRA_CFLAGS += -I$(src)/common/inc
72EXTRA_CFLAGS += -I$(src)
73EXTRA_CFLAGS += -Wall $(DEFINES) $(INCLUDES) -Wno-cast-qual -Wno-format-extra-args
74EXTRA_CFLAGS += -D__KERNEL__ -DMODULE -DNVRM
75EXTRA_CFLAGS += -DNV_VERSION_STRING=\"550.40.07\"
76
77ifneq ($(SYSSRCHOST1X),)
78 EXTRA_CFLAGS += -I$(SYSSRCHOST1X)
79endif
80
81# Some Android kernels prohibit driver use of filesystem functions like
82# filp_open() and kernel_read(). Disable the NV_FILESYSTEM_ACCESS_AVAILABLE
83# functionality that uses those functions when building for Android.
84
85PLATFORM_IS_ANDROID ?= 0
86
87ifeq ($(PLATFORM_IS_ANDROID),1)
88 EXTRA_CFLAGS += -DNV_FILESYSTEM_ACCESS_AVAILABLE=0
89else
90 EXTRA_CFLAGS += -DNV_FILESYSTEM_ACCESS_AVAILABLE=1
91endif
92
93EXTRA_CFLAGS += -Wno-unused-function
94
95ifneq ($(NV_BUILD_TYPE),debug)
96 EXTRA_CFLAGS += -Wuninitialized
97endif
98
99EXTRA_CFLAGS += -fno-strict-aliasing
100
101ifeq ($(ARCH),arm64)
102 EXTRA_CFLAGS += -mstrict-align
103endif
104
105ifeq ($(NV_BUILD_TYPE),debug)
106 EXTRA_CFLAGS += -g
107endif
108
109EXTRA_CFLAGS += -ffreestanding
110
111ifeq ($(ARCH),arm64)
112 EXTRA_CFLAGS += -mgeneral-regs-only -march=armv8-a
113 EXTRA_CFLAGS += $(call cc-option,-mno-outline-atomics,)
114endif
115
116ifeq ($(ARCH),x86_64)
117 EXTRA_CFLAGS += -mno-red-zone -mcmodel=kernel
118endif
119
120ifeq ($(ARCH),powerpc)
121 EXTRA_CFLAGS += -mlittle-endian -mno-strict-align -mno-altivec
122endif
123
124EXTRA_CFLAGS += -DNV_UVM_ENABLE
125EXTRA_CFLAGS += $(call cc-option,-Werror=undef,)
126EXTRA_CFLAGS += -DNV_SPECTRE_V2=$(NV_SPECTRE_V2)
127EXTRA_CFLAGS += -DNV_KERNEL_INTERFACE_LAYER
128
129#
130# Detect SGI UV systems and apply system-specific optimizations.
131#
132
133ifneq ($(wildcard /proc/sgi_uv),)
134 EXTRA_CFLAGS += -DNV_CONFIG_X86_UV
135endif
136
137ifdef VGX_FORCE_VFIO_PCI_CORE
138 EXTRA_CFLAGS += -DNV_VGPU_FORCE_VFIO_PCI_CORE
139endif
140
141WARNINGS_AS_ERRORS ?=
142ifeq ($(WARNINGS_AS_ERRORS),1)
143 ccflags-y += -Werror
144else
145 ccflags-y += -Wno-error
146endif
147
148#
149# The conftest.sh script tests various aspects of the target kernel.
150# The per-module Kbuild files included above should:
151#
152# - Append to the NV_CONFTEST_*_COMPILE_TESTS variables to indicate
153# which conftests they require.
154# - Append to the NV_OBJECTS_DEPEND_ON_CONFTEST variable any object files
155# that depend on conftest.
156#
157# The conftest machinery below will run the requested tests and
158# generate the appropriate header files.
159#
160
161CC ?= cc
162LD ?= ld
163
164NV_CONFTEST_SCRIPT := $(src)/conftest.sh
165NV_CONFTEST_HEADER := $(obj)/conftest/headers.h
166
167NV_CONFTEST_CMD := /bin/sh $(NV_CONFTEST_SCRIPT) \
168 "$(CC)" $(ARCH) $(NV_KERNEL_SOURCES) $(NV_KERNEL_OUTPUT)
169
170NV_CFLAGS_FROM_CONFTEST := $(shell $(NV_CONFTEST_CMD) build_cflags)
171
172NV_CONFTEST_CFLAGS = $(NV_CFLAGS_FROM_CONFTEST) $(EXTRA_CFLAGS) -fno-pie
173NV_CONFTEST_CFLAGS += -Wno-error
174
175NV_CONFTEST_COMPILE_TEST_HEADERS := $(obj)/conftest/macros.h
176NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/functions.h
177NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/symbols.h
178NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/types.h
179NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/generic.h
180
181NV_CONFTEST_HEADERS := $(obj)/conftest/patches.h
182NV_CONFTEST_HEADERS += $(obj)/conftest/headers.h
183NV_CONFTEST_HEADERS += $(NV_CONFTEST_COMPILE_TEST_HEADERS)
184
185
186#
187# Generate a header file for a single conftest compile test. Each compile test
188# header depends on conftest.sh, as well as the generated conftest/headers.h
189# file, which is included in the compile test preamble.
190#
191
192$(obj)/conftest/compile-tests/%.h: $(NV_CONFTEST_SCRIPT) $(NV_CONFTEST_HEADER)
193	@mkdir -p $(obj)/conftest/compile-tests
194	@echo " CONFTEST: $(notdir $*)"
195	@$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' \
196	 $(notdir $*) > $@
197
198#
199# Concatenate a conftest/*.h header from its constituent compile test headers
200#
201# $(1): The name of the concatenated header
202# $(2): The list of compile tests that make up the header
203#
204
205define NV_GENERATE_COMPILE_TEST_HEADER
206 $(obj)/conftest/$(1).h: $(addprefix $(obj)/conftest/compile-tests/,$(addsuffix .h,$(2)))
207	@mkdir -p $(obj)/conftest
208	@# concatenate /dev/null to prevent cat from hanging when $$^ is empty
209	@cat $$^ /dev/null > $$@
210endef
211
212#
213# Generate the conftest compile test headers from the lists of compile tests
214# provided by the module-specific Kbuild files.
215#
216
217NV_CONFTEST_FUNCTION_COMPILE_TESTS ?=
218NV_CONFTEST_GENERIC_COMPILE_TESTS ?=
219NV_CONFTEST_MACRO_COMPILE_TESTS ?=
220NV_CONFTEST_SYMBOL_COMPILE_TESTS ?=
221NV_CONFTEST_TYPE_COMPILE_TESTS ?=
222
223$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,functions,$(NV_CONFTEST_FUNCTION_COMPILE_TESTS)))
224$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,generic,$(NV_CONFTEST_GENERIC_COMPILE_TESTS)))
225$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,macros,$(NV_CONFTEST_MACRO_COMPILE_TESTS)))
226$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,symbols,$(NV_CONFTEST_SYMBOL_COMPILE_TESTS)))
227$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,types,$(NV_CONFTEST_TYPE_COMPILE_TESTS)))
228
229$(obj)/conftest/patches.h: $(NV_CONFTEST_SCRIPT)
230	@mkdir -p $(obj)/conftest
231	@$(NV_CONFTEST_CMD) patch_check > $@
232
233include $(src)/header-presence-tests.mk
234
235# Filename to store the define for the header in $(1); this is only consumed by
236# the rule below that concatenates all of these together.
237NV_HEADER_PRESENCE_PART = $(addprefix $(obj)/conftest/header_presence/,$(addsuffix .part,$(1)))
238
239# Define a rule to check the header $(1).
240define NV_HEADER_PRESENCE_CHECK
241 $$(call NV_HEADER_PRESENCE_PART,$(1)): $$(NV_CONFTEST_SCRIPT) $(obj)/conftest/uts_release
242	@mkdir -p $$(dir $$@)
243	@$$(NV_CONFTEST_CMD) test_kernel_header '$$(NV_CONFTEST_CFLAGS)' '$(1)' > $$@
244endef
245
246# Evaluate the rule above for each header in the list.
247$(foreach header,$(NV_HEADER_PRESENCE_TESTS),$(eval $(call NV_HEADER_PRESENCE_CHECK,$(header))))
248
249# Concatenate all of the parts into headers.h.
250$(obj)/conftest/headers.h: $(call NV_HEADER_PRESENCE_PART,$(NV_HEADER_PRESENCE_TESTS))
251	@cat $^ > $@
252
253clean-dirs := $(obj)/conftest
254
255
256# For any object files that depend on conftest, declare the dependency here.
257$(addprefix $(obj)/,$(NV_OBJECTS_DEPEND_ON_CONFTEST)): | $(NV_CONFTEST_HEADERS)
258
259# Sanity checks of the build environment and target system/kernel
260
261BUILD_SANITY_CHECKS = \
262 cc_sanity_check \
263 cc_version_check \
264 dom0_sanity_check \
265 xen_sanity_check \
266 preempt_rt_sanity_check \
267 vgpu_kvm_sanity_check \
268 module_symvers_sanity_check
269
270.PHONY: $(BUILD_SANITY_CHECKS)
271
272$(BUILD_SANITY_CHECKS):
273	@$(NV_CONFTEST_CMD) $@ full_output
274
275# Perform all sanity checks before generating the conftest headers
276
277$(NV_CONFTEST_HEADERS): | $(BUILD_SANITY_CHECKS)
278
279# Make the conftest headers depend on the kernel version string
280
281$(obj)/conftest/uts_release: NV_GENERATE_UTS_RELEASE
282	@mkdir -p $(dir $@)
283	@NV_UTS_RELEASE="// Kernel version: `$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' uts_release`"; \
284	if ! [ -f "$@" ] || [ "$$NV_UTS_RELEASE" != "`cat $@`" ]; \
285	then echo "$$NV_UTS_RELEASE" > $@; fi
286
287.PHONY: NV_GENERATE_UTS_RELEASE
288
289$(NV_CONFTEST_HEADERS): $(obj)/conftest/uts_release
290