########################################################################### ###
#@Title         Root kernel makefile
#@Copyright     Copyright (c) Imagination Technologies Ltd. All Rights Reserved
#@License       Dual MIT/GPLv2
#
# The contents of this file are subject to the MIT license as set out below.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# Alternatively, the contents of this file may be used under the terms of
# the GNU General Public License Version 2 ("GPL") in which case the provisions
# of GPL are applicable instead of those above.
# 
# If you wish to allow use of your version of this file only under the terms of
# GPL, and not to allow others to use your version of this file under the terms
# of the MIT license, indicate your decision by deleting the provisions above
# and replace them with the notice and other provisions required by GPL as set
# out in the file called "GPL-COPYING" included in this distribution. If you do
# not delete the provisions above, a recipient may use your version of this file
# under the terms of either the MIT license or GPL.
#
# This License is also included in this distribution in the file called
# "MIT-COPYING".
#
# EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
# PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
### ###########################################################################

# This top-level kbuild makefile builds all the Linux kernel modules in the
# DDK. To run kbuild, this makefile is copied to $(TARGET_PRIMARY_OUT)/kbuild/Makefile
# and make is invoked in $(TARGET_PRIMARY_OUT)/kbuild.

# This makefile doesn't define any kbuild special variables apart from
# ccflags-y and obj-m. The variables for objects are picked up by including
# the kbuild makefile fragments named in $(INTERNAL_KBUILD_MAKEFILES). The
# list of objects that these fragments make is collected in
# $(INTERNAL_KBUILD_OBJECTS) and $(INTERNAL_EXTRA_KBUILD_OBJECTS). These
# variables are set according to the build's $(KERNEL_COMPONENTS) and
# $(EXTRA_PVRSRVKM_COMPONENTS). To add a new kernel module to the build, edit
# these variables in the per-build Makefile.
TOP := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))

PVRSRVKM_NAME := kunlun-pvrsrvkm
bridge_base := $(TOP)/generated/$(PVR_ARCH)/

ifeq ($(CONFIG_ANDROID), y)
ccflags-y += -DANDROID
endif

ifeq ($(CONFIG_BUFFER_SYNC), y)
ccflags-y += -DSUPPORT_BUFFER_SYNC
endif

ifeq ($(cc-name), clang)
ccflags-y += -Wno-error=typedef-redefinition -Wno-error=enum-conversion
endif

ifeq ($(CONFIG_PM_DEVFREQ),y)
#ccflags-y += -DSUPPORT_LINUX_DVFS
endif

ccflags-y += -D__linux__ -include $(TOP)/config_kernel.h \
  -I$(TOP)/include \
  -I$(TOP)/include/km \
  -I$(TOP)/include/rxv_clk \
  -I$(TOP)/include/rxvdrive \
  -I$(TOP)/include/vpucoda988

ccflags-y += -DKO_VERSION=\"$(shell date +%Y-%m-%d-%H:%M:%S)\" \
  -DUSE_RTOS_I2C \
  -DUSE_SDRV_MSI=${USE_SDRV_MSI}0

ccflags-y += \
  -DWITH_KERNEL_VM=1 \
  -DENABLE_SD_CLKGEN

$(PVRSRVKM_NAME)-y += \
  rgxdebug.o  \
  vmm_pvz_client.o  \
  rgxfwdbg.o  \
  pvr_debugfs.o  \
  server_rgxtq_bridge.o  \
  physmem_osmem_linux.o  \
  server_rgxcmp_bridge.o  \
  physmem_lma.o  \
  info_page_km.o  \
  pvr_buffer_sync.o  \
  rgxfwimageutils.o  \
  htbuffer.o  \
  lt9611_bridge.o  \
  server_pvrtl_bridge.o  \
  devicemem_server.o  \
  pvr_counting_timeline.o  \
  server_rgxregconfig_bridge.o  \
  lt9611.o  \
  rgxutils.o  \
  client_mm_direct_bridge.o  \
  pvr_fence.o  \
  rgxpower.o  \
  server_rgxbreakpoint_bridge.o  \
  pvr_sw_fence.o  \
  rgxfwtrace_strings.o  \
  rgxkicksync.o  \
  server_devicememhistory_bridge.o  \
  connection_server.o  \
  pvrsrv_bridge_init.o  \
  hash.o  \
  tlserver.o  \
  pvrsrv_pool.o  \
  physmem_dmabuf.o  \
  rgxmulticore.o  \
  pvr_pci_drv.o  \
  devicemem_heapcfg.o  \
  allocmem.o  \
  module_common.o  \
  sdrv-mipi-dsi.o  \
  kunlun_drm_parallel.o  \
  server_cache_bridge.o  \
  kunlun_drm_drv.o  \
  pvrsrv_error.o  \
  pvrsrv.o  \
  rgxtimecorr.o  \
  client_devicememhistory_direct_bridge.o  \
  rgxshader.o  \
  rgxtdmtransfer.o  \
  ra.o  \
  pvr_gputrace.o  \
  client_htbuffer_direct_bridge.o  \
  osfunc_arm64.o  \
  server_rgxta3d_bridge.o  \
  rgxstartstop.o  \
  rgxtimerquery.o  \
  pcie-vpu-dev.o  \
  pcie-audio-dev.o  \
  di_impl_brg.o  \
  server_rgxkicksync_bridge.o  \
  server_rgxtq2_bridge.o  \
  vz_vmm_vm.o  \
  sync.o  \
  lists.o  \
  osmmap_stub.o  \
  client_sync_direct_bridge.o  \
  pmr_os.o  \
  dc_r0p1.o  \
  debug_common.o  \
  pvr_drm.o  \
  tlclient.o  \
  server_rgxhwperf_bridge.o  \
  vmm_type_stub.o  \
  tlintern.o  \
  pvr_bridge_k.o  \
  devicemem.o  \
  kunlun_drm_lvds.o  \
  rgxinit.o  \
  sdrv_dpc.o  \
  rgxccb.o  \
  devicemem_utils.o  \
  cache_km.o  \
  physmem_hostmem.o  \
  rgxsyncutils.o  \
  tlstream.o  \
  rgxsrvinit.o  \
  rgxfwutils.o  \
  kunlun_drm_memblock.o  \
  server_cmm_bridge.o  \
  htbserver.o  \
  rgxbvnc.o  \
  rgxlayer_impl.o  \
  devicemem_history_server.o  \
  osconnection_server.o  \
  uniq_key_splay_tree.o  \
  rgxta3d.o  \
  server_dmabuf_bridge.o  \
  rgxbreakpoint.o  \
  physmem_test.o  \
  interrupt_support.o  \
  dw_i2c.o  \
  rgxhwperf.o  \
  handle.o  \
  srvcore.o  \
  kunlun_drm_panel.o  \
  server_srvcore_bridge.o  \
  rgxmem.o  \
  process_stats.o  \
  power.o  \
  mem_utils.o  \
  dwc_mipi_dsi_host.o  \
  server_rgxfwdbg_bridge.o  \
  rgxmipsmmuinit.o  \
  client_synctracking_direct_bridge.o  \
  server_mm_bridge.o  \
  client_pvrtl_direct_bridge.o  \
  event.o  \
  client_cache_direct_bridge.o  \
  pcie-sd-dev.o  \
  km_apphint.o  \
  vmm_pvz_server.o  \
  handle_idr.o  \
  osfunc.o  \
  pvr_sync_file.o  \
  mmu_common.o  \
  gpipe_dc.o  \
  spipe.o  \
  rgxhwperf_common.o  \
  rgx_bridge_init.o  \
  pvr_sync_ioctl_dev.o  \
  pvr_sync_ioctl_common.o  \
  physheap.o  \
  dma_support.o  \
  server_rgxtimerquery_bridge.o  \
  physmem.o  \
  server_htbuffer_bridge.o  \
  sync_server.o  \
  vz_vmm_pvz.o  \
  rgxregconfig.o  \
  sync_checkpoint.o  \
  server_sync_bridge.o  \
  dp_dummy.o  \
  server_di_bridge.o  \
  sdrv_plane.o  \
  server_synctracking_bridge.o  \
  sysconfig.o  \
  kunlun_drm_gem.o  \
  rgxmmuinit.o  \
  rgxcompute.o  \
  htb_debug.o  \
  pvr_notifier.o  \
  rgx_hwperf_table.o  \
  fwload.o  \
  rgxtransfer.o  \
  pmr.o  \
  pvr_debug.o  \
  di_server.o

$(PVRSRVKM_NAME)-y += \
  clkgen_hal.o \
  pll_hal.o \
  res.o \
  module_helper_hal.o \
  res_clk.o \
  per_disp.o \
  pll.o \
  clkgen_drv.o \
  vm.o \
  sdrv_clk_export.o

$(PVRSRVKM_NAME)-y += rxvdrive/rxview_hdmi.o
$(PVRSRVKM_NAME)-y += vpucoda988/vpucoda988.o

$(PVRSRVKM_NAME)-y += $(foreach _m,$(INTERNAL_EXTRA_KBUILD_OBJECTS:.o=),$($(_m)-y))

obj-m += $(PVRSRVKM_NAME).o


PWD:=$(shell pwd)
KDIR?=/lib/modules/`uname -r`/build
CROSS_ARCH:=ARCH=arm64

all: rxview
	$(info please make rxview !)
	@sudo cp kunlun-pvrsrvkm.ko /usr/lib/modules/5.4.18-53-generic/kernel/updates/
	@sudo su

rxview:
	$(info rxview KDIR: $(KDIR))
	unset LDFLAGS
	$(MAKE) $(CROSS_ARCH) -C $(KDIR) M=$(PWD) modules
	@mkdir -p ./package/$@
	@cp -v $(obj-m:.o=.ko) ./package/$@


.PHONY: modules clean

modules: clean
	$(MAKE) $(CROSS_ARCH) -C $(KDIR) M=$(PWD) modules

clean:
	@sudo rm -rf package
	$(MAKE) $(CROSS_ARCH) -C $(KDIR) M=$(PWD) clean

install:
	@depmod -a; sync; update-initramfs -u; sync
