16751faf3SViresh Kumar#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
36751faf3SViresh Kumar#
46751faf3SViresh Kumar# Modules specific tests cases
56751faf3SViresh Kumar
66751faf3SViresh Kumar# protect against multiple inclusion
76751faf3SViresh Kumarif [ $FILE_MODULE ]; then
86751faf3SViresh Kumar	return 0
96751faf3SViresh Kumarelse
106751faf3SViresh Kumar	FILE_MODULE=DONE
116751faf3SViresh Kumarfi
126751faf3SViresh Kumar
136751faf3SViresh Kumarsource cpu.sh
146751faf3SViresh Kumarsource cpufreq.sh
156751faf3SViresh Kumarsource governor.sh
166751faf3SViresh Kumar
176751faf3SViresh Kumar# Check basic insmod/rmmod
186751faf3SViresh Kumar# $1: module
196751faf3SViresh Kumartest_basic_insmod_rmmod()
206751faf3SViresh Kumar{
216751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
226751faf3SViresh Kumar
236751faf3SViresh Kumar	printf "Inserting $1 module\n"
246751faf3SViresh Kumar	# insert module
256751faf3SViresh Kumar	insmod $1
266751faf3SViresh Kumar	if [ $? != 0 ]; then
276751faf3SViresh Kumar		printf "Insmod $1 failed\n"
286751faf3SViresh Kumar		exit;
296751faf3SViresh Kumar	fi
306751faf3SViresh Kumar
316751faf3SViresh Kumar	printf "Removing $1 module\n"
326751faf3SViresh Kumar	# remove module
336751faf3SViresh Kumar	rmmod $1
346751faf3SViresh Kumar	if [ $? != 0 ]; then
356751faf3SViresh Kumar		printf "rmmod $1 failed\n"
366751faf3SViresh Kumar		exit;
376751faf3SViresh Kumar	fi
386751faf3SViresh Kumar
396751faf3SViresh Kumar	printf "\n"
406751faf3SViresh Kumar}
416751faf3SViresh Kumar
426751faf3SViresh Kumar# Insert cpufreq driver module and perform basic tests
436751faf3SViresh Kumar# $1: cpufreq-driver module to insert
446751faf3SViresh Kumar# $2: If we want to play with CPUs (1) or not (0)
456751faf3SViresh Kumarmodule_driver_test_single()
466751faf3SViresh Kumar{
476751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} for driver $1 and cpus_hotplug=$2 **\n\n"
486751faf3SViresh Kumar
496751faf3SViresh Kumar	if [ $2 -eq 1 ]; then
506751faf3SViresh Kumar		# offline all non-boot CPUs
516751faf3SViresh Kumar		for_each_non_boot_cpu offline_cpu
526751faf3SViresh Kumar		printf "\n"
536751faf3SViresh Kumar	fi
546751faf3SViresh Kumar
556751faf3SViresh Kumar	# insert module
566751faf3SViresh Kumar	printf "Inserting $1 module\n\n"
576751faf3SViresh Kumar	insmod $1
586751faf3SViresh Kumar	if [ $? != 0 ]; then
596751faf3SViresh Kumar		printf "Insmod $1 failed\n"
606751faf3SViresh Kumar		return;
616751faf3SViresh Kumar	fi
626751faf3SViresh Kumar
636751faf3SViresh Kumar	if [ $2 -eq 1 ]; then
646751faf3SViresh Kumar		# online all non-boot CPUs
656751faf3SViresh Kumar		for_each_non_boot_cpu online_cpu
666751faf3SViresh Kumar		printf "\n"
676751faf3SViresh Kumar	fi
686751faf3SViresh Kumar
696751faf3SViresh Kumar	# run basic tests
706751faf3SViresh Kumar	cpufreq_basic_tests
716751faf3SViresh Kumar
726751faf3SViresh Kumar	# remove module
736751faf3SViresh Kumar	printf "Removing $1 module\n\n"
746751faf3SViresh Kumar	rmmod $1
756751faf3SViresh Kumar	if [ $? != 0 ]; then
766751faf3SViresh Kumar		printf "rmmod $1 failed\n"
776751faf3SViresh Kumar		return;
786751faf3SViresh Kumar	fi
796751faf3SViresh Kumar
806751faf3SViresh Kumar	# There shouldn't be any cpufreq directories now.
816751faf3SViresh Kumar	for_each_cpu cpu_should_not_have_cpufreq_directory
826751faf3SViresh Kumar	printf "\n"
836751faf3SViresh Kumar}
846751faf3SViresh Kumar
856751faf3SViresh Kumar# $1: cpufreq-driver module to insert
866751faf3SViresh Kumarmodule_driver_test()
876751faf3SViresh Kumar{
886751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
896751faf3SViresh Kumar
906751faf3SViresh Kumar	# check if module is present or not
916751faf3SViresh Kumar	ls $1 > /dev/null
926751faf3SViresh Kumar	if [ $? != 0 ]; then
936751faf3SViresh Kumar		printf "$1: not present in `pwd` folder\n"
946751faf3SViresh Kumar		return;
956751faf3SViresh Kumar	fi
966751faf3SViresh Kumar
976751faf3SViresh Kumar	# test basic module tests
986751faf3SViresh Kumar	test_basic_insmod_rmmod $1
996751faf3SViresh Kumar
1006751faf3SViresh Kumar	# Do simple module test
1016751faf3SViresh Kumar	module_driver_test_single $1 0
1026751faf3SViresh Kumar
1036751faf3SViresh Kumar	# Remove CPUs before inserting module and then bring them back
1046751faf3SViresh Kumar	module_driver_test_single $1 1
1056751faf3SViresh Kumar	printf "\n"
1066751faf3SViresh Kumar}
1076751faf3SViresh Kumar
1086751faf3SViresh Kumar# find governor name based on governor module name
1096751faf3SViresh Kumar# $1: governor module name
1106751faf3SViresh Kumarfind_gov_name()
1116751faf3SViresh Kumar{
1126751faf3SViresh Kumar	if [ $1 = "cpufreq_ondemand.ko" ]; then
1136751faf3SViresh Kumar		printf "ondemand"
1146751faf3SViresh Kumar	elif [ $1 = "cpufreq_conservative.ko" ]; then
1156751faf3SViresh Kumar		printf "conservative"
1166751faf3SViresh Kumar	elif [ $1 = "cpufreq_userspace.ko" ]; then
1176751faf3SViresh Kumar		printf "userspace"
1186751faf3SViresh Kumar	elif [ $1 = "cpufreq_performance.ko" ]; then
1196751faf3SViresh Kumar		printf "performance"
1206751faf3SViresh Kumar	elif [ $1 = "cpufreq_powersave.ko" ]; then
1216751faf3SViresh Kumar		printf "powersave"
1226751faf3SViresh Kumar	elif [ $1 = "cpufreq_schedutil.ko" ]; then
1236751faf3SViresh Kumar		printf "schedutil"
1246751faf3SViresh Kumar	fi
1256751faf3SViresh Kumar}
1266751faf3SViresh Kumar
1276751faf3SViresh Kumar# $1: governor string, $2: governor module, $3: policy
1286751faf3SViresh Kumar# example: module_governor_test_single "ondemand" "cpufreq_ondemand.ko" 2
1296751faf3SViresh Kumarmodule_governor_test_single()
1306751faf3SViresh Kumar{
1316751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} for $3 **\n\n"
1326751faf3SViresh Kumar
1336751faf3SViresh Kumar	backup_governor $3
1346751faf3SViresh Kumar
1356751faf3SViresh Kumar	# switch to new governor
1366751faf3SViresh Kumar	printf "Switch from $CUR_GOV to $1\n"
1376751faf3SViresh Kumar	switch_show_governor $3 $1
1386751faf3SViresh Kumar
1396751faf3SViresh Kumar	# try removing module, it should fail as governor is used
1406751faf3SViresh Kumar	printf "Removing $2 module\n\n"
1416751faf3SViresh Kumar	rmmod $2
1426751faf3SViresh Kumar	if [ $? = 0 ]; then
1436751faf3SViresh Kumar		printf "WARN: rmmod $2 succeeded even if governor is used\n"
1446751faf3SViresh Kumar		insmod $2
1456751faf3SViresh Kumar	else
1466751faf3SViresh Kumar		printf "Pass: unable to remove $2 while it is being used\n\n"
1476751faf3SViresh Kumar	fi
1486751faf3SViresh Kumar
1496751faf3SViresh Kumar	# switch back to old governor
1506751faf3SViresh Kumar	printf "Switchback to $CUR_GOV from $1\n"
1516751faf3SViresh Kumar	restore_governor $3
1526751faf3SViresh Kumar	printf "\n"
1536751faf3SViresh Kumar}
1546751faf3SViresh Kumar
1556751faf3SViresh Kumar# Insert cpufreq governor module and perform basic tests
1566751faf3SViresh Kumar# $1: cpufreq-governor module to insert
1576751faf3SViresh Kumarmodule_governor_test()
1586751faf3SViresh Kumar{
1596751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
1606751faf3SViresh Kumar
1616751faf3SViresh Kumar	# check if module is present or not
1626751faf3SViresh Kumar	ls $1 > /dev/null
1636751faf3SViresh Kumar	if [ $? != 0 ]; then
1646751faf3SViresh Kumar		printf "$1: not present in `pwd` folder\n"
1656751faf3SViresh Kumar		return;
1666751faf3SViresh Kumar	fi
1676751faf3SViresh Kumar
1686751faf3SViresh Kumar	# test basic module tests
1696751faf3SViresh Kumar	test_basic_insmod_rmmod $1
1706751faf3SViresh Kumar
1716751faf3SViresh Kumar	# insert module
1726751faf3SViresh Kumar	printf "Inserting $1 module\n\n"
1736751faf3SViresh Kumar	insmod $1
1746751faf3SViresh Kumar	if [ $? != 0 ]; then
1756751faf3SViresh Kumar		printf "Insmod $1 failed\n"
1766751faf3SViresh Kumar		return;
1776751faf3SViresh Kumar	fi
1786751faf3SViresh Kumar
1796751faf3SViresh Kumar	# switch to new governor for each cpu
1806751faf3SViresh Kumar	for_each_policy module_governor_test_single $(find_gov_name $1) $1
1816751faf3SViresh Kumar
1826751faf3SViresh Kumar	# remove module
1836751faf3SViresh Kumar	printf "Removing $1 module\n\n"
1846751faf3SViresh Kumar	rmmod $1
1856751faf3SViresh Kumar	if [ $? != 0 ]; then
1866751faf3SViresh Kumar		printf "rmmod $1 failed\n"
1876751faf3SViresh Kumar		return;
1886751faf3SViresh Kumar	fi
1896751faf3SViresh Kumar	printf "\n"
1906751faf3SViresh Kumar}
1916751faf3SViresh Kumar
1926751faf3SViresh Kumar# test modules: driver and governor
1936751faf3SViresh Kumar# $1: driver module, $2: governor module
1946751faf3SViresh Kumarmodule_test()
1956751faf3SViresh Kumar{
1966751faf3SViresh Kumar	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
1976751faf3SViresh Kumar
1986751faf3SViresh Kumar	# check if modules are present or not
1996751faf3SViresh Kumar	ls $1 $2 > /dev/null
2006751faf3SViresh Kumar	if [ $? != 0 ]; then
2016751faf3SViresh Kumar		printf "$1 or $2: is not present in `pwd` folder\n"
2026751faf3SViresh Kumar		return;
2036751faf3SViresh Kumar	fi
2046751faf3SViresh Kumar
2056751faf3SViresh Kumar	# TEST1: Insert gov after driver
2066751faf3SViresh Kumar	# insert driver module
2076751faf3SViresh Kumar	printf "Inserting $1 module\n\n"
2086751faf3SViresh Kumar	insmod $1
2096751faf3SViresh Kumar	if [ $? != 0 ]; then
2106751faf3SViresh Kumar		printf "Insmod $1 failed\n"
2116751faf3SViresh Kumar		return;
2126751faf3SViresh Kumar	fi
2136751faf3SViresh Kumar
2146751faf3SViresh Kumar	# run governor tests
2156751faf3SViresh Kumar	module_governor_test $2
2166751faf3SViresh Kumar
2176751faf3SViresh Kumar	# remove driver module
2186751faf3SViresh Kumar	printf "Removing $1 module\n\n"
2196751faf3SViresh Kumar	rmmod $1
2206751faf3SViresh Kumar	if [ $? != 0 ]; then
2216751faf3SViresh Kumar		printf "rmmod $1 failed\n"
2226751faf3SViresh Kumar		return;
2236751faf3SViresh Kumar	fi
2246751faf3SViresh Kumar
2256751faf3SViresh Kumar	# TEST2: Insert driver after governor
2266751faf3SViresh Kumar	# insert governor module
2276751faf3SViresh Kumar	printf "Inserting $2 module\n\n"
2286751faf3SViresh Kumar	insmod $2
2296751faf3SViresh Kumar	if [ $? != 0 ]; then
2306751faf3SViresh Kumar		printf "Insmod $2 failed\n"
2316751faf3SViresh Kumar		return;
2326751faf3SViresh Kumar	fi
2336751faf3SViresh Kumar
2346751faf3SViresh Kumar	# run governor tests
2356751faf3SViresh Kumar	module_driver_test $1
2366751faf3SViresh Kumar
2376751faf3SViresh Kumar	# remove driver module
2386751faf3SViresh Kumar	printf "Removing $2 module\n\n"
2396751faf3SViresh Kumar	rmmod $2
2406751faf3SViresh Kumar	if [ $? != 0 ]; then
2416751faf3SViresh Kumar		printf "rmmod $2 failed\n"
2426751faf3SViresh Kumar		return;
2436751faf3SViresh Kumar	fi
2446751faf3SViresh Kumar}
245