#
# Copyright (C) 2010-2011 ARM Limited. All rights reserved.
# 
# This program is free software and is provided to you under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
# 
# A copy of the licence is included with the program, and can also be obtained from Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

# This file is called by the Linux build system.

OSKOS=linux
FILES_PREFIX=

# set up defaults if not defined by the user
USING_UMP ?= 0
USING_OS_MEMORY ?= 0
USING_MALI_PMU ?= 0
USING_PMM ?= 1
USING_GPU_UTILIZATION ?= 0
USING_MALI_RUN_TIME_PM ?= 1
USING_MALI_PMM_TESTSUITE ?= 0
OS_MEMORY_KERNEL_BUFFER_SIZE_IN_MB ?= 6
USING_PROFILING ?= 0
DISABLE_PP0 ?= 0
DISABLE_PP1 ?= 0
DISABLE_PP2 ?= 0
DISABLE_PP3 ?= 0
TIMESTAMP ?= default
BUILD ?= debug
TARGET_PLATFORM ?= default
KERNEL_RUNTIME_PM_ENABLED ?= 0
CONFIG ?= pb-virtex5-m200

# Get path to driver source from Linux build system
DRIVER_DIR=$(srctree)/$(src)

MALI_RELEASE_NAME=$(shell cat $(DRIVER_DIR)/.version 2> /dev/null)
include $(DRIVER_DIR)/Makefile.common

# Set up our defines, which will be passed to gcc
DEFINES += -DUSING_OS_MEMORY=$(USING_OS_MEMORY)
DEFINES += -DUSING_MMU=1
DEFINES += -DUSING_UMP=$(USING_UMP)
DEFINES += -D_MALI_OSK_SPECIFIC_INDIRECT_MMAP
DEFINES += -DMALI_TIMELINE_PROFILING_ENABLED=$(USING_PROFILING)
DEFINES += -DDISABLE_PP0=$(DISABLE_PP0)
DEFINES += -DDISABLE_PP1=$(DISABLE_PP1)
DEFINES += -DDISABLE_PP2=$(DISABLE_PP2)
DEFINES += -DDISABLE_PP3=$(DISABLE_PP3)
DEFINES += -DMALI_POWER_MGMT_TEST_SUITE=$(USING_MALI_PMM_TESTSUITE)
ifeq ($(shell test $(SUBLEVEL) -gt 32 -a $(PATCHLEVEL) = 6 -a $(VERSION) = 2 -o $(VERSION) -gt 2 && echo "OK"),OK)
# MALI_STATE_TRACKING is only supported on Linux kernels from version 2.6.32.
DEFINES += -DMALI_STATE_TRACKING=1
else
DEFINES += -DMALI_STATE_TRACKING=0
endif
DEFINES += -DMALI_OS_MEMORY_KERNEL_BUFFER_SIZE_IN_MB=$(OS_MEMORY_KERNEL_BUFFER_SIZE_IN_MB)

MALI_PLATFORM_FILE = platform/$(TARGET_PLATFORM)/mali_platform.c

DEFINES += -DUSING_MALI_PMM=$(USING_PMM)
DEFINES += -DMALI_GPU_UTILIZATION=$(USING_GPU_UTILIZATION)

ifneq ($(call submodule_enabled, $(DRIVER_DIR), PMU),0)
	USING_MALI_PMU = 1
endif

ifeq ($(USING_MALI_RUN_TIME_PM),1)
ifdef CONFIG_PM
ifdef CONFIG_PM_RUNTIME
	KERNEL_RUNTIME_PM_ENABLED = 1
endif
endif
endif

DEFINES += -DUSING_MALI_PMU=$(USING_MALI_PMU)
DEFINES += -DMALI_PMM_RUNTIME_JOB_CONTROL_ON=$(KERNEL_RUNTIME_PM_ENABLED)

ifeq ($(BUILD), debug)
DEFINES += -DDEBUG
endif
DEFINES += -DSVN_REV=$(SVN_REV)
DEFINES += -DSVN_REV_STRING=\"$(SVN_REV)\"

# Linux has its own mmap cleanup handlers (see mali_kernel_mem_mmu.c)
DEFINES += -DMALI_UKK_HAS_IMPLICIT_MMAP_CLEANUP

ifeq ($(USING_UMP),1)
	DEFINES += -DMALI_USE_UNIFIED_MEMORY_PROVIDER=1
	ccflags-y += -I$(DRIVER_DIR)/../../ump/include/ump
	ccflags-y += -I$(DRIVER_DIR)/../include/ump -I$(DRIVER_DIR)/../../include/ump
else
	DEFINES += -DMALI_USE_UNIFIED_MEMORY_PROVIDER=0
endif

# Use our defines when compiling
ccflags-y += $(DEFINES) -I$(DRIVER_DIR) -I$(DRIVER_DIR)/common -I$(DRIVER_DIR)/linux -I$(DRIVER_DIR)/platform

# For customer releases the Linux Device Drivers will be provided as ARM proprietary and GPL releases:
# The ARM proprietary product will only include the license/proprietary directory
# The GPL product will only include the license/gpl directory

ifeq ($(wildcard $(DRIVER_DIR)/linux/license/gpl/*),)
ccflags-y += -I$(DRIVER_DIR)/linux/license/proprietary
else
ccflags-y += -I$(DRIVER_DIR)/linux/license/gpl
endif
ccflags-y += -I$(DRIVER_DIR)/common/pmm

# Source files which always are included in a build
SRC = \
	common/mali_kernel_core.c \
	linux/mali_kernel_linux.c \
	$(OSKOS)/mali_osk_indir_mmap.c \
	common/mali_kernel_rendercore.c \
	common/mali_kernel_descriptor_mapping.c \
	common/mali_kernel_vsync.c \
	linux/mali_ukk_vsync.c \
	linux/mali_kernel_sysfs.c \
	common/mali_kernel_mem_mmu.c \
	common/mali_kernel_memory_engine.c \
	common/mali_block_allocator.c \
	common/mali_kernel_mem_os.c \
	$(MALI_PLATFORM_FILE) \
	$(OSKFILES) \
	$(UKKFILES) \
	__malidrv_build_info.c

ifeq ($(USING_PROFILING),1)
SRC += \
	common/mali_kernel_profiling.c \
	timestamp-$(TIMESTAMP)/mali_timestamp.c
ccflags-y += -I$(DRIVER_DIR)/timestamp-$(TIMESTAMP)
endif

# Selecting files to compile by parsing the config file

ifeq ($(USING_PMM),1)
ifeq ($(USING_MALI_PMU),1)
SRC +=	\
	common/pmm/mali_pmm_pmu.c
endif
SRC += \
	common/pmm/mali_pmm.c \
	common/pmm/mali_pmm_policy.c \
	common/pmm/mali_pmm_policy_alwayson.c \
	common/pmm/mali_pmm_policy_jobcontrol.c \
	common/pmm/mali_pmm_state.c \
	linux/mali_kernel_pm.c \
	linux/mali_osk_pm.c \
	linux/mali_device_pause_resume.c
endif

ifeq ($(USING_MALI_PMM_TESTSUITE),1)
ccflags-y += -I$(DRIVER_DIR)/platform/mali_pmu_testing
ifeq ($(USING_MALI_PMU),0)
SRC += \
	platform/mali_pmu_testing/mali_platform_pmu_internal_testing.c
endif
endif

ifeq ($(USING_GPU_UTILIZATION),1)
SRC += \
	common/mali_kernel_utilization.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI400PP),0)
	# Mali-400 PP in use
	ccflags-y += -DUSING_MALI400
	SRC += common/mali_kernel_MALI200.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI400GP),0)
	# Mali-400 GP in use
	SRC += common/mali_kernel_GP2.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI300PP),0)
	# Mali-400 PP in use
	ccflags-y += -DUSING_MALI400
	SRC += common/mali_kernel_MALI200.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI300GP),0)
	# Mali-400 GP in use
	SRC += common/mali_kernel_GP2.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI200),0)
	# Mali200 in use
	ccflags-y += -DUSING_MALI200
	SRC += common/mali_kernel_MALI200.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALIGP2),0)
	# MaliGP2 in use
	SRC += common/mali_kernel_GP2.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI400L2),0)
	# Mali Level2 cache in use
	ccflags-y += -DUSING_MALI400_L2_CACHE
	SRC += common/mali_kernel_l2_cache.c
endif

ifneq ($(call submodule_enabled, $(DRIVER_DIR), MALI300L2),0)
	# Mali Level2 cache in use
	ccflags-y += -DUSING_MALI400_L2_CACHE
	SRC += common/mali_kernel_l2_cache.c
endif

# Tell the Linux build system from which .o file to create the kernel module
obj-m := mali.o
# Tell the Linux build system to enable building of our .c files
mali-y := $(SRC:.c=.o)

# Extend common version-string
VERSION_STRINGS += BUILD=$(shell echo $(BUILD) | tr a-z A-Z)
VERSION_STRINGS += CPU=$(CPU)
VERSION_STRINGS += USING_UMP=$(USING_UMP)
VERSION_STRINGS += USING_PMM=$(USING_PMM)
VERSION_STRINGS += USING_MALI200=$(call submodule_enabled, $(DRIVER_DIR), MALI200)
VERSION_STRINGS += USING_MALI400=$(call submodule_enabled, $(DRIVER_DIR), MALI400)
VERSION_STRINGS += USING_MALI400_L2_CACHE=$(call submodule_enabled, $(DRIVER_DIR), MALI400L2)
VERSION_STRINGS += USING_GP2=$(call submodule_enabled, $(DRIVER_DIR), MALIGP2)
VERSION_STRINGS += KDIR=$(KDIR)
VERSION_STRINGS += MALI_PLATFORM_FILE=$(MALI_PLATFORM_FILE)
VERSION_STRINGS += OS_MEMORY_KERNEL_BUFFER_SIZE_IN_MB=$(OS_MEMORY_KERNEL_BUFFER_SIZE_IN_MB)
VERSION_STRINGS += USING_PROFILING=$(USING_PROFILING)
VERSION_STRINGS += USING_GPU_UTILIZATION=$(USING_GPU_UTILIZATION)
VERSION_STRINGS += USING_MALI_RUN_TIME_PM=$(USING_MALI_RUN_TIME_PM)

# Create file with Mali driver configuration
$(DRIVER_DIR)/__malidrv_build_info.c:
	@echo 'const char *__malidrv_build_info(void) { return "malidrv: $(VERSION_STRINGS)";}' > $(DRIVER_DIR)/__malidrv_build_info.c
