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-error -Wno-format-extra-args 74EXTRA_CFLAGS += -D__KERNEL__ -DMODULE -DNVRM 75EXTRA_CFLAGS += -DNV_VERSION_STRING=\"535.54.03\" 76 77ifneq ($(SYSSRCHOST1X),) 78 EXTRA_CFLAGS += -I$(SYSSRCHOST1X) 79endif 80 81EXTRA_CFLAGS += -Wno-unused-function 82 83ifneq ($(NV_BUILD_TYPE),debug) 84 EXTRA_CFLAGS += -Wuninitialized 85endif 86 87EXTRA_CFLAGS += -fno-strict-aliasing 88 89ifeq ($(ARCH),arm64) 90 EXTRA_CFLAGS += -mstrict-align 91endif 92 93ifeq ($(NV_BUILD_TYPE),debug) 94 EXTRA_CFLAGS += -g 95 EXTRA_CFLAGS += $(call cc-option,-gsplit-dwarf,) 96endif 97 98EXTRA_CFLAGS += -ffreestanding 99 100ifeq ($(ARCH),arm64) 101 EXTRA_CFLAGS += -mgeneral-regs-only -march=armv8-a 102 EXTRA_CFLAGS += $(call cc-option,-mno-outline-atomics,) 103endif 104 105ifeq ($(ARCH),x86_64) 106 EXTRA_CFLAGS += -mno-red-zone -mcmodel=kernel 107endif 108 109ifeq ($(ARCH),powerpc) 110 EXTRA_CFLAGS += -mlittle-endian -mno-strict-align -mno-altivec 111endif 112 113EXTRA_CFLAGS += -DNV_UVM_ENABLE 114EXTRA_CFLAGS += $(call cc-option,-Werror=undef,) 115EXTRA_CFLAGS += -DNV_SPECTRE_V2=$(NV_SPECTRE_V2) 116EXTRA_CFLAGS += -DNV_KERNEL_INTERFACE_LAYER 117 118# 119# Detect SGI UV systems and apply system-specific optimizations. 120# 121 122ifneq ($(wildcard /proc/sgi_uv),) 123 EXTRA_CFLAGS += -DNV_CONFIG_X86_UV 124endif 125 126 127# 128# The conftest.sh script tests various aspects of the target kernel. 129# The per-module Kbuild files included above should: 130# 131# - Append to the NV_CONFTEST_*_COMPILE_TESTS variables to indicate 132# which conftests they require. 133# - Append to the NV_OBJECTS_DEPEND_ON_CONFTEST variable any object files 134# that depend on conftest. 135# 136# The conftest machinery below will run the requested tests and 137# generate the appropriate header files. 138# 139 140CC ?= cc 141LD ?= ld 142 143NV_CONFTEST_SCRIPT := $(src)/conftest.sh 144NV_CONFTEST_HEADER := $(obj)/conftest/headers.h 145 146NV_CONFTEST_CMD := /bin/sh $(NV_CONFTEST_SCRIPT) \ 147 "$(CC)" $(ARCH) $(NV_KERNEL_SOURCES) $(NV_KERNEL_OUTPUT) 148 149NV_CFLAGS_FROM_CONFTEST := $(shell $(NV_CONFTEST_CMD) build_cflags) 150 151NV_CONFTEST_CFLAGS = $(NV_CFLAGS_FROM_CONFTEST) $(EXTRA_CFLAGS) -fno-pie 152 153NV_CONFTEST_COMPILE_TEST_HEADERS := $(obj)/conftest/macros.h 154NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/functions.h 155NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/symbols.h 156NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/types.h 157NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/generic.h 158 159NV_CONFTEST_HEADERS := $(obj)/conftest/patches.h 160NV_CONFTEST_HEADERS += $(obj)/conftest/headers.h 161NV_CONFTEST_HEADERS += $(NV_CONFTEST_COMPILE_TEST_HEADERS) 162 163 164# 165# Generate a header file for a single conftest compile test. Each compile test 166# header depends on conftest.sh, as well as the generated conftest/headers.h 167# file, which is included in the compile test preamble. 168# 169 170$(obj)/conftest/compile-tests/%.h: $(NV_CONFTEST_SCRIPT) $(NV_CONFTEST_HEADER) 171 @mkdir -p $(obj)/conftest/compile-tests 172 @echo " CONFTEST: $(notdir $*)" 173 @$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' \ 174 $(notdir $*) > $@ 175 176# 177# Concatenate a conftest/*.h header from its constituent compile test headers 178# 179# $(1): The name of the concatenated header 180# $(2): The list of compile tests that make up the header 181# 182 183define NV_GENERATE_COMPILE_TEST_HEADER 184 $(obj)/conftest/$(1).h: $(addprefix $(obj)/conftest/compile-tests/,$(addsuffix .h,$(2))) 185 @mkdir -p $(obj)/conftest 186 @# concatenate /dev/null to prevent cat from hanging when $$^ is empty 187 @cat $$^ /dev/null > $$@ 188endef 189 190# 191# Generate the conftest compile test headers from the lists of compile tests 192# provided by the module-specific Kbuild files. 193# 194 195NV_CONFTEST_FUNCTION_COMPILE_TESTS ?= 196NV_CONFTEST_GENERIC_COMPILE_TESTS ?= 197NV_CONFTEST_MACRO_COMPILE_TESTS ?= 198NV_CONFTEST_SYMBOL_COMPILE_TESTS ?= 199NV_CONFTEST_TYPE_COMPILE_TESTS ?= 200 201$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,functions,$(NV_CONFTEST_FUNCTION_COMPILE_TESTS))) 202$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,generic,$(NV_CONFTEST_GENERIC_COMPILE_TESTS))) 203$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,macros,$(NV_CONFTEST_MACRO_COMPILE_TESTS))) 204$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,symbols,$(NV_CONFTEST_SYMBOL_COMPILE_TESTS))) 205$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,types,$(NV_CONFTEST_TYPE_COMPILE_TESTS))) 206 207$(obj)/conftest/patches.h: $(NV_CONFTEST_SCRIPT) 208 @mkdir -p $(obj)/conftest 209 @$(NV_CONFTEST_CMD) patch_check > $@ 210 211 212# Each of these headers is checked for presence with a test #include; a 213# corresponding #define will be generated in conftest/headers.h. 214NV_HEADER_PRESENCE_TESTS = \ 215 asm/system.h \ 216 drm/drmP.h \ 217 drm/drm_auth.h \ 218 drm/drm_gem.h \ 219 drm/drm_crtc.h \ 220 drm/drm_color_mgmt.h \ 221 drm/drm_atomic.h \ 222 drm/drm_atomic_helper.h \ 223 drm/drm_atomic_state_helper.h \ 224 drm/drm_encoder.h \ 225 drm/drm_atomic_uapi.h \ 226 drm/drm_drv.h \ 227 drm/drm_framebuffer.h \ 228 drm/drm_connector.h \ 229 drm/drm_probe_helper.h \ 230 drm/drm_blend.h \ 231 drm/drm_fourcc.h \ 232 drm/drm_prime.h \ 233 drm/drm_plane.h \ 234 drm/drm_vblank.h \ 235 drm/drm_file.h \ 236 drm/drm_ioctl.h \ 237 drm/drm_device.h \ 238 drm/drm_mode_config.h \ 239 drm/drm_modeset_lock.h \ 240 dt-bindings/interconnect/tegra_icc_id.h \ 241 generated/autoconf.h \ 242 generated/compile.h \ 243 generated/utsrelease.h \ 244 linux/efi.h \ 245 linux/kconfig.h \ 246 linux/platform/tegra/mc_utils.h \ 247 linux/printk.h \ 248 linux/ratelimit.h \ 249 linux/prio_tree.h \ 250 linux/log2.h \ 251 linux/of.h \ 252 linux/bug.h \ 253 linux/sched.h \ 254 linux/sched/mm.h \ 255 linux/sched/signal.h \ 256 linux/sched/task.h \ 257 linux/sched/task_stack.h \ 258 xen/ioemu.h \ 259 linux/fence.h \ 260 linux/dma-resv.h \ 261 soc/tegra/chip-id.h \ 262 soc/tegra/fuse.h \ 263 soc/tegra/tegra_bpmp.h \ 264 video/nv_internal.h \ 265 linux/platform/tegra/dce/dce-client-ipc.h \ 266 linux/nvhost.h \ 267 linux/nvhost_t194.h \ 268 linux/host1x-next.h \ 269 asm/book3s/64/hash-64k.h \ 270 asm/set_memory.h \ 271 asm/prom.h \ 272 asm/powernv.h \ 273 linux/atomic.h \ 274 asm/barrier.h \ 275 asm/opal-api.h \ 276 sound/hdaudio.h \ 277 asm/pgtable_types.h \ 278 linux/stringhash.h \ 279 linux/dma-map-ops.h \ 280 rdma/peer_mem.h \ 281 sound/hda_codec.h \ 282 linux/dma-buf.h \ 283 linux/time.h \ 284 linux/platform_device.h \ 285 linux/mutex.h \ 286 linux/reset.h \ 287 linux/of_platform.h \ 288 linux/of_device.h \ 289 linux/of_gpio.h \ 290 linux/gpio.h \ 291 linux/gpio/consumer.h \ 292 linux/interconnect.h \ 293 linux/pm_runtime.h \ 294 linux/clk.h \ 295 linux/clk-provider.h \ 296 linux/ioasid.h \ 297 linux/stdarg.h \ 298 linux/iosys-map.h \ 299 asm/coco.h \ 300 linux/vfio_pci_core.h \ 301 linux/mdev.h \ 302 soc/tegra/bpmp-abi.h \ 303 soc/tegra/bpmp.h 304 305# Filename to store the define for the header in $(1); this is only consumed by 306# the rule below that concatenates all of these together. 307NV_HEADER_PRESENCE_PART = $(addprefix $(obj)/conftest/header_presence/,$(addsuffix .part,$(1))) 308 309# Define a rule to check the header $(1). 310define NV_HEADER_PRESENCE_CHECK 311 $$(call NV_HEADER_PRESENCE_PART,$(1)): $$(NV_CONFTEST_SCRIPT) $(obj)/conftest/uts_release 312 @mkdir -p $$(dir $$@) 313 @$$(NV_CONFTEST_CMD) test_kernel_header '$$(NV_CONFTEST_CFLAGS)' '$(1)' > $$@ 314endef 315 316# Evaluate the rule above for each header in the list. 317$(foreach header,$(NV_HEADER_PRESENCE_TESTS),$(eval $(call NV_HEADER_PRESENCE_CHECK,$(header)))) 318 319# Concatenate all of the parts into headers.h. 320$(obj)/conftest/headers.h: $(call NV_HEADER_PRESENCE_PART,$(NV_HEADER_PRESENCE_TESTS)) 321 @cat $^ > $@ 322 323clean-dirs := $(obj)/conftest 324 325 326# For any object files that depend on conftest, declare the dependency here. 327$(addprefix $(obj)/,$(NV_OBJECTS_DEPEND_ON_CONFTEST)): | $(NV_CONFTEST_HEADERS) 328 329# Sanity checks of the build environment and target system/kernel 330 331BUILD_SANITY_CHECKS = \ 332 cc_sanity_check \ 333 cc_version_check \ 334 dom0_sanity_check \ 335 xen_sanity_check \ 336 preempt_rt_sanity_check \ 337 vgpu_kvm_sanity_check \ 338 module_symvers_sanity_check 339 340.PHONY: $(BUILD_SANITY_CHECKS) 341 342$(BUILD_SANITY_CHECKS): 343 @$(NV_CONFTEST_CMD) $@ full_output 344 345# Perform all sanity checks before generating the conftest headers 346 347$(NV_CONFTEST_HEADERS): | $(BUILD_SANITY_CHECKS) 348 349# Make the conftest headers depend on the kernel version string 350 351$(obj)/conftest/uts_release: NV_GENERATE_UTS_RELEASE 352 @mkdir -p $(dir $@) 353 @NV_UTS_RELEASE="// Kernel version: `$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' uts_release`"; \ 354 if ! [ -f "$@" ] || [ "$$NV_UTS_RELEASE" != "`cat $@`" ]; \ 355 then echo "$$NV_UTS_RELEASE" > $@; fi 356 357.PHONY: NV_GENERATE_UTS_RELEASE 358 359$(NV_CONFTEST_HEADERS): $(obj)/conftest/uts_release 360