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=\"545.29.02\" 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 137 138# 139# The conftest.sh script tests various aspects of the target kernel. 140# The per-module Kbuild files included above should: 141# 142# - Append to the NV_CONFTEST_*_COMPILE_TESTS variables to indicate 143# which conftests they require. 144# - Append to the NV_OBJECTS_DEPEND_ON_CONFTEST variable any object files 145# that depend on conftest. 146# 147# The conftest machinery below will run the requested tests and 148# generate the appropriate header files. 149# 150 151CC ?= cc 152LD ?= ld 153 154NV_CONFTEST_SCRIPT := $(src)/conftest.sh 155NV_CONFTEST_HEADER := $(obj)/conftest/headers.h 156 157NV_CONFTEST_CMD := /bin/sh $(NV_CONFTEST_SCRIPT) \ 158 "$(CC)" $(ARCH) $(NV_KERNEL_SOURCES) $(NV_KERNEL_OUTPUT) 159 160NV_CFLAGS_FROM_CONFTEST := $(shell $(NV_CONFTEST_CMD) build_cflags) 161 162NV_CONFTEST_CFLAGS = $(NV_CFLAGS_FROM_CONFTEST) $(EXTRA_CFLAGS) -fno-pie 163 164NV_CONFTEST_COMPILE_TEST_HEADERS := $(obj)/conftest/macros.h 165NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/functions.h 166NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/symbols.h 167NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/types.h 168NV_CONFTEST_COMPILE_TEST_HEADERS += $(obj)/conftest/generic.h 169 170NV_CONFTEST_HEADERS := $(obj)/conftest/patches.h 171NV_CONFTEST_HEADERS += $(obj)/conftest/headers.h 172NV_CONFTEST_HEADERS += $(NV_CONFTEST_COMPILE_TEST_HEADERS) 173 174 175# 176# Generate a header file for a single conftest compile test. Each compile test 177# header depends on conftest.sh, as well as the generated conftest/headers.h 178# file, which is included in the compile test preamble. 179# 180 181$(obj)/conftest/compile-tests/%.h: $(NV_CONFTEST_SCRIPT) $(NV_CONFTEST_HEADER) 182 @mkdir -p $(obj)/conftest/compile-tests 183 @echo " CONFTEST: $(notdir $*)" 184 @$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' \ 185 $(notdir $*) > $@ 186 187# 188# Concatenate a conftest/*.h header from its constituent compile test headers 189# 190# $(1): The name of the concatenated header 191# $(2): The list of compile tests that make up the header 192# 193 194define NV_GENERATE_COMPILE_TEST_HEADER 195 $(obj)/conftest/$(1).h: $(addprefix $(obj)/conftest/compile-tests/,$(addsuffix .h,$(2))) 196 @mkdir -p $(obj)/conftest 197 @# concatenate /dev/null to prevent cat from hanging when $$^ is empty 198 @cat $$^ /dev/null > $$@ 199endef 200 201# 202# Generate the conftest compile test headers from the lists of compile tests 203# provided by the module-specific Kbuild files. 204# 205 206NV_CONFTEST_FUNCTION_COMPILE_TESTS ?= 207NV_CONFTEST_GENERIC_COMPILE_TESTS ?= 208NV_CONFTEST_MACRO_COMPILE_TESTS ?= 209NV_CONFTEST_SYMBOL_COMPILE_TESTS ?= 210NV_CONFTEST_TYPE_COMPILE_TESTS ?= 211 212$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,functions,$(NV_CONFTEST_FUNCTION_COMPILE_TESTS))) 213$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,generic,$(NV_CONFTEST_GENERIC_COMPILE_TESTS))) 214$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,macros,$(NV_CONFTEST_MACRO_COMPILE_TESTS))) 215$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,symbols,$(NV_CONFTEST_SYMBOL_COMPILE_TESTS))) 216$(eval $(call NV_GENERATE_COMPILE_TEST_HEADER,types,$(NV_CONFTEST_TYPE_COMPILE_TESTS))) 217 218$(obj)/conftest/patches.h: $(NV_CONFTEST_SCRIPT) 219 @mkdir -p $(obj)/conftest 220 @$(NV_CONFTEST_CMD) patch_check > $@ 221 222 223# Each of these headers is checked for presence with a test #include; a 224# corresponding #define will be generated in conftest/headers.h. 225NV_HEADER_PRESENCE_TESTS = \ 226 asm/system.h \ 227 drm/drmP.h \ 228 drm/drm_aperture.h \ 229 drm/drm_auth.h \ 230 drm/drm_gem.h \ 231 drm/drm_crtc.h \ 232 drm/drm_color_mgmt.h \ 233 drm/drm_atomic.h \ 234 drm/drm_atomic_helper.h \ 235 drm/drm_atomic_state_helper.h \ 236 drm/drm_encoder.h \ 237 drm/drm_atomic_uapi.h \ 238 drm/drm_drv.h \ 239 drm/drm_fbdev_generic.h \ 240 drm/drm_framebuffer.h \ 241 drm/drm_connector.h \ 242 drm/drm_probe_helper.h \ 243 drm/drm_blend.h \ 244 drm/drm_fourcc.h \ 245 drm/drm_prime.h \ 246 drm/drm_plane.h \ 247 drm/drm_vblank.h \ 248 drm/drm_file.h \ 249 drm/drm_ioctl.h \ 250 drm/drm_device.h \ 251 drm/drm_mode_config.h \ 252 drm/drm_modeset_lock.h \ 253 dt-bindings/interconnect/tegra_icc_id.h \ 254 generated/autoconf.h \ 255 generated/compile.h \ 256 generated/utsrelease.h \ 257 linux/efi.h \ 258 linux/kconfig.h \ 259 linux/platform/tegra/mc_utils.h \ 260 linux/printk.h \ 261 linux/ratelimit.h \ 262 linux/prio_tree.h \ 263 linux/log2.h \ 264 linux/of.h \ 265 linux/bug.h \ 266 linux/sched.h \ 267 linux/sched/mm.h \ 268 linux/sched/signal.h \ 269 linux/sched/task.h \ 270 linux/sched/task_stack.h \ 271 xen/ioemu.h \ 272 linux/fence.h \ 273 linux/dma-fence.h \ 274 linux/dma-resv.h \ 275 soc/tegra/chip-id.h \ 276 soc/tegra/fuse.h \ 277 soc/tegra/tegra_bpmp.h \ 278 video/nv_internal.h \ 279 linux/platform/tegra/dce/dce-client-ipc.h \ 280 linux/nvhost.h \ 281 linux/nvhost_t194.h \ 282 linux/host1x-next.h \ 283 asm/book3s/64/hash-64k.h \ 284 asm/set_memory.h \ 285 asm/prom.h \ 286 asm/powernv.h \ 287 linux/atomic.h \ 288 asm/barrier.h \ 289 asm/opal-api.h \ 290 sound/hdaudio.h \ 291 asm/pgtable_types.h \ 292 asm/page.h \ 293 linux/stringhash.h \ 294 linux/dma-map-ops.h \ 295 rdma/peer_mem.h \ 296 sound/hda_codec.h \ 297 linux/dma-buf.h \ 298 linux/time.h \ 299 linux/platform_device.h \ 300 linux/mutex.h \ 301 linux/reset.h \ 302 linux/of_platform.h \ 303 linux/of_device.h \ 304 linux/of_gpio.h \ 305 linux/gpio.h \ 306 linux/gpio/consumer.h \ 307 linux/interconnect.h \ 308 linux/pm_runtime.h \ 309 linux/clk.h \ 310 linux/clk-provider.h \ 311 linux/ioasid.h \ 312 linux/stdarg.h \ 313 linux/iosys-map.h \ 314 asm/coco.h \ 315 linux/vfio_pci_core.h \ 316 linux/mdev.h \ 317 soc/tegra/bpmp-abi.h \ 318 soc/tegra/bpmp.h \ 319 linux/sync_file.h \ 320 linux/cc_platform.h \ 321 asm/cpufeature.h 322 323# Filename to store the define for the header in $(1); this is only consumed by 324# the rule below that concatenates all of these together. 325NV_HEADER_PRESENCE_PART = $(addprefix $(obj)/conftest/header_presence/,$(addsuffix .part,$(1))) 326 327# Define a rule to check the header $(1). 328define NV_HEADER_PRESENCE_CHECK 329 $$(call NV_HEADER_PRESENCE_PART,$(1)): $$(NV_CONFTEST_SCRIPT) $(obj)/conftest/uts_release 330 @mkdir -p $$(dir $$@) 331 @$$(NV_CONFTEST_CMD) test_kernel_header '$$(NV_CONFTEST_CFLAGS)' '$(1)' > $$@ 332endef 333 334# Evaluate the rule above for each header in the list. 335$(foreach header,$(NV_HEADER_PRESENCE_TESTS),$(eval $(call NV_HEADER_PRESENCE_CHECK,$(header)))) 336 337# Concatenate all of the parts into headers.h. 338$(obj)/conftest/headers.h: $(call NV_HEADER_PRESENCE_PART,$(NV_HEADER_PRESENCE_TESTS)) 339 @cat $^ > $@ 340 341clean-dirs := $(obj)/conftest 342 343 344# For any object files that depend on conftest, declare the dependency here. 345$(addprefix $(obj)/,$(NV_OBJECTS_DEPEND_ON_CONFTEST)): | $(NV_CONFTEST_HEADERS) 346 347# Sanity checks of the build environment and target system/kernel 348 349BUILD_SANITY_CHECKS = \ 350 cc_sanity_check \ 351 cc_version_check \ 352 dom0_sanity_check \ 353 xen_sanity_check \ 354 preempt_rt_sanity_check \ 355 vgpu_kvm_sanity_check \ 356 module_symvers_sanity_check 357 358.PHONY: $(BUILD_SANITY_CHECKS) 359 360$(BUILD_SANITY_CHECKS): 361 @$(NV_CONFTEST_CMD) $@ full_output 362 363# Perform all sanity checks before generating the conftest headers 364 365$(NV_CONFTEST_HEADERS): | $(BUILD_SANITY_CHECKS) 366 367# Make the conftest headers depend on the kernel version string 368 369$(obj)/conftest/uts_release: NV_GENERATE_UTS_RELEASE 370 @mkdir -p $(dir $@) 371 @NV_UTS_RELEASE="// Kernel version: `$(NV_CONFTEST_CMD) compile_tests '$(NV_CONFTEST_CFLAGS)' uts_release`"; \ 372 if ! [ -f "$@" ] || [ "$$NV_UTS_RELEASE" != "`cat $@`" ]; \ 373 then echo "$$NV_UTS_RELEASE" > $@; fi 374 375.PHONY: NV_GENERATE_UTS_RELEASE 376 377$(NV_CONFTEST_HEADERS): $(obj)/conftest/uts_release 378