xref: /linux/tools/testing/selftests/cpufreq/main.sh (revision 0b631ed3ce922b34dad4938215f84e5321f7e524)
1e66d5b67SViresh Kumar#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3e66d5b67SViresh Kumar
4e66d5b67SViresh Kumarsource cpu.sh
5e66d5b67SViresh Kumarsource cpufreq.sh
6e66d5b67SViresh Kumarsource governor.sh
76751faf3SViresh Kumarsource module.sh
81e4c2830SViresh Kumarsource special-tests.sh
9e66d5b67SViresh Kumar
10c7e84706SMuhammad Usama AnjumDIR="$(dirname $(readlink -f "$0"))"
11c7e84706SMuhammad Usama Anjumsource "${DIR}"/../kselftest/ktap_helpers.sh
12c7e84706SMuhammad Usama Anjum
13e66d5b67SViresh KumarFUNC=basic	# do basic tests by default
14e66d5b67SViresh KumarOUTFILE=cpufreq_selftest
15e66d5b67SViresh KumarSYSFS=
16e66d5b67SViresh KumarCPUROOT=
17e66d5b67SViresh KumarCPUFREQROOT=
18e66d5b67SViresh Kumar
19e66d5b67SViresh Kumarhelpme()
20e66d5b67SViresh Kumar{
216751faf3SViresh Kumar	printf "Usage: $0 [-h] [-todg args]
22e66d5b67SViresh Kumar	[-h <help>]
23e66d5b67SViresh Kumar	[-o <output-file-for-dump>]
24b03eaf8dSViresh Kumar	[-t <basic: Basic cpufreq testing
25b03eaf8dSViresh Kumar	     suspend: suspend/resume,
266751faf3SViresh Kumar	     hibernate: hibernate/resume,
27*0b631ed3SShreeya Patel	     suspend_rtc: suspend/resume back using the RTC wakeup alarm,
28*0b631ed3SShreeya Patel	     hibernate_rtc: hibernate/resume back using the RTC wakeup alarm,
291e4c2830SViresh Kumar	     modtest: test driver or governor modules. Only to be used with -d or -g options,
301e4c2830SViresh Kumar	     sptest1: Simple governor switch to produce lockdep.
311e4c2830SViresh Kumar	     sptest2: Concurrent governor switch to produce lockdep.
321e4c2830SViresh Kumar	     sptest3: Governor races, shuffle between governors quickly.
331e4c2830SViresh Kumar	     sptest4: CPU hotplugs with updates to cpufreq files.>]
346751faf3SViresh Kumar	[-d <driver's module name: only with \"-t modtest>\"]
356751faf3SViresh Kumar	[-g <governor's module name: only with \"-t modtest>\"]
36e66d5b67SViresh Kumar	\n"
37c7e84706SMuhammad Usama Anjum	exit "${KSFT_FAIL}"
38e66d5b67SViresh Kumar}
39e66d5b67SViresh Kumar
40e66d5b67SViresh Kumarprerequisite()
41e66d5b67SViresh Kumar{
42e66d5b67SViresh Kumar	msg="skip all tests:"
43e66d5b67SViresh Kumar
44e66d5b67SViresh Kumar	if [ $UID != 0 ]; then
45c7e84706SMuhammad Usama Anjum		ktap_skip_all "$msg must be run as root"
46c7e84706SMuhammad Usama Anjum		exit "${KSFT_SKIP}"
47e66d5b67SViresh Kumar	fi
48e66d5b67SViresh Kumar
49e66d5b67SViresh Kumar	taskset -p 01 $$
50e66d5b67SViresh Kumar
51e66d5b67SViresh Kumar	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
52e66d5b67SViresh Kumar
53e66d5b67SViresh Kumar	if [ ! -d "$SYSFS" ]; then
54c7e84706SMuhammad Usama Anjum		ktap_skip_all "$msg sysfs is not mounted"
55c7e84706SMuhammad Usama Anjum		exit "${KSFT_SKIP}"
56e66d5b67SViresh Kumar	fi
57e66d5b67SViresh Kumar
58e66d5b67SViresh Kumar	CPUROOT=$SYSFS/devices/system/cpu
59e66d5b67SViresh Kumar	CPUFREQROOT="$CPUROOT/cpufreq"
60e66d5b67SViresh Kumar
61e66d5b67SViresh Kumar	if ! ls $CPUROOT/cpu* > /dev/null 2>&1; then
62c7e84706SMuhammad Usama Anjum		ktap_skip_all "$msg cpus not available in sysfs"
63c7e84706SMuhammad Usama Anjum		exit "${KSFT_SKIP}"
64e66d5b67SViresh Kumar	fi
65e66d5b67SViresh Kumar
66e66d5b67SViresh Kumar	if ! ls $CPUROOT/cpufreq > /dev/null 2>&1; then
67c7e84706SMuhammad Usama Anjum		ktap_skip_all "$msg cpufreq directory not available in sysfs"
68c7e84706SMuhammad Usama Anjum		exit "${KSFT_SKIP}"
69e66d5b67SViresh Kumar	fi
70e66d5b67SViresh Kumar}
71e66d5b67SViresh Kumar
72e66d5b67SViresh Kumarparse_arguments()
73e66d5b67SViresh Kumar{
746751faf3SViresh Kumar	while getopts ht:o:d:g: arg
75e66d5b67SViresh Kumar	do
76e66d5b67SViresh Kumar		case $arg in
77e66d5b67SViresh Kumar			h) # --help
78e66d5b67SViresh Kumar				helpme
79e66d5b67SViresh Kumar				;;
80e66d5b67SViresh Kumar
81*0b631ed3SShreeya Patel			t) # --func_type (Function to perform: basic, suspend, hibernate,
82*0b631ed3SShreeya Patel			   # suspend_rtc, hibernate_rtc, modtest, sptest1/2/3/4 (default: basic))
83e66d5b67SViresh Kumar				FUNC=$OPTARG
84e66d5b67SViresh Kumar				;;
85e66d5b67SViresh Kumar
86e66d5b67SViresh Kumar			o) # --output-file (Output file to store dumps)
87e66d5b67SViresh Kumar				OUTFILE=$OPTARG
88e66d5b67SViresh Kumar				;;
89e66d5b67SViresh Kumar
906751faf3SViresh Kumar			d) # --driver-mod-name (Name of the driver module)
916751faf3SViresh Kumar				DRIVER_MOD=$OPTARG
926751faf3SViresh Kumar				;;
936751faf3SViresh Kumar
946751faf3SViresh Kumar			g) # --governor-mod-name (Name of the governor module)
956751faf3SViresh Kumar				GOVERNOR_MOD=$OPTARG
966751faf3SViresh Kumar				;;
976751faf3SViresh Kumar
98e66d5b67SViresh Kumar			\?)
99e66d5b67SViresh Kumar				helpme
100e66d5b67SViresh Kumar				;;
101e66d5b67SViresh Kumar		esac
102e66d5b67SViresh Kumar	done
103e66d5b67SViresh Kumar}
104e66d5b67SViresh Kumar
105e66d5b67SViresh Kumardo_test()
106e66d5b67SViresh Kumar{
107e66d5b67SViresh Kumar	# Check if CPUs are managed by cpufreq or not
108e66d5b67SViresh Kumar	count=$(count_cpufreq_managed_cpus)
109e66d5b67SViresh Kumar
1106751faf3SViresh Kumar	if [ $count = 0 -a $FUNC != "modtest" ]; then
111c7e84706SMuhammad Usama Anjum		ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting"
112e66d5b67SViresh Kumar	fi
113e66d5b67SViresh Kumar
114e66d5b67SViresh Kumar	case "$FUNC" in
115e66d5b67SViresh Kumar		"basic")
116e66d5b67SViresh Kumar		cpufreq_basic_tests
117e66d5b67SViresh Kumar		;;
118e66d5b67SViresh Kumar
119b03eaf8dSViresh Kumar		"suspend")
120b03eaf8dSViresh Kumar		do_suspend "suspend" 1
121b03eaf8dSViresh Kumar		;;
122b03eaf8dSViresh Kumar
123b03eaf8dSViresh Kumar		"hibernate")
124b03eaf8dSViresh Kumar		do_suspend "hibernate" 1
125b03eaf8dSViresh Kumar		;;
126b03eaf8dSViresh Kumar
127*0b631ed3SShreeya Patel		"suspend_rtc")
128*0b631ed3SShreeya Patel                do_suspend "suspend" 1 rtc
129*0b631ed3SShreeya Patel                ;;
130*0b631ed3SShreeya Patel
131*0b631ed3SShreeya Patel                "hibernate_rtc")
132*0b631ed3SShreeya Patel                do_suspend "hibernate" 1 rtc
133*0b631ed3SShreeya Patel                ;;
134*0b631ed3SShreeya Patel
1356751faf3SViresh Kumar		"modtest")
1366751faf3SViresh Kumar		# Do we have modules in place?
1376751faf3SViresh Kumar		if [ -z $DRIVER_MOD ] && [ -z $GOVERNOR_MOD ]; then
138c7e84706SMuhammad Usama Anjum			ktap_exit_fail_msg "No driver or governor module passed with -d or -g"
1396751faf3SViresh Kumar		fi
1406751faf3SViresh Kumar
1416751faf3SViresh Kumar		if [ $DRIVER_MOD ]; then
1426751faf3SViresh Kumar			if [ $GOVERNOR_MOD ]; then
1436751faf3SViresh Kumar				module_test $DRIVER_MOD $GOVERNOR_MOD
1446751faf3SViresh Kumar			else
1456751faf3SViresh Kumar				module_driver_test $DRIVER_MOD
1466751faf3SViresh Kumar			fi
1476751faf3SViresh Kumar		else
1486751faf3SViresh Kumar			if [ $count = 0 ]; then
149c7e84706SMuhammad Usama Anjum				ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting"
1506751faf3SViresh Kumar			fi
1516751faf3SViresh Kumar
1526751faf3SViresh Kumar			module_governor_test $GOVERNOR_MOD
1536751faf3SViresh Kumar		fi
1546751faf3SViresh Kumar		;;
1556751faf3SViresh Kumar
1561e4c2830SViresh Kumar		"sptest1")
1571e4c2830SViresh Kumar		simple_lockdep
1581e4c2830SViresh Kumar		;;
1591e4c2830SViresh Kumar
1601e4c2830SViresh Kumar		"sptest2")
1611e4c2830SViresh Kumar		concurrent_lockdep
1621e4c2830SViresh Kumar		;;
1631e4c2830SViresh Kumar
1641e4c2830SViresh Kumar		"sptest3")
1651e4c2830SViresh Kumar		governor_race
1661e4c2830SViresh Kumar		;;
1671e4c2830SViresh Kumar
1681e4c2830SViresh Kumar		"sptest4")
1691e4c2830SViresh Kumar		hotplug_with_updates
1701e4c2830SViresh Kumar		;;
1711e4c2830SViresh Kumar
172e66d5b67SViresh Kumar		*)
173c7e84706SMuhammad Usama Anjum		ktap_print_msg "Invalid [-f] function type"
174e66d5b67SViresh Kumar		helpme
175e66d5b67SViresh Kumar		;;
176e66d5b67SViresh Kumar	esac
177e66d5b67SViresh Kumar}
178e66d5b67SViresh Kumar
179e66d5b67SViresh Kumar# clear dumps
180e66d5b67SViresh Kumar# $1: file name
181e66d5b67SViresh Kumarclear_dumps()
182e66d5b67SViresh Kumar{
183e66d5b67SViresh Kumar	echo "" > $1.txt
184e66d5b67SViresh Kumar	echo "" > $1.dmesg_cpufreq.txt
185e66d5b67SViresh Kumar	echo "" > $1.dmesg_full.txt
186e66d5b67SViresh Kumar}
187e66d5b67SViresh Kumar
188e66d5b67SViresh Kumar# $1: output file name
189e66d5b67SViresh Kumardmesg_dumps()
190e66d5b67SViresh Kumar{
191e66d5b67SViresh Kumar	dmesg | grep cpufreq >> $1.dmesg_cpufreq.txt
192e66d5b67SViresh Kumar
193e66d5b67SViresh Kumar	# We may need the full logs as well
194e66d5b67SViresh Kumar	dmesg >> $1.dmesg_full.txt
195e66d5b67SViresh Kumar}
196e66d5b67SViresh Kumar
197c7e84706SMuhammad Usama Anjumktap_print_header
198c7e84706SMuhammad Usama Anjum
199e66d5b67SViresh Kumar# Parse arguments
200e66d5b67SViresh Kumarparse_arguments $@
201e66d5b67SViresh Kumar
202c7e84706SMuhammad Usama Anjumktap_set_plan 1
203c7e84706SMuhammad Usama Anjum
204e66d5b67SViresh Kumar# Make sure all requirements are met
205e66d5b67SViresh Kumarprerequisite
206e66d5b67SViresh Kumar
207e66d5b67SViresh Kumar# Run requested functions
208e66d5b67SViresh Kumarclear_dumps $OUTFILE
20940d70d4dSNícolas F. R. A. Pradodo_test | tee -a $OUTFILE.txt
210c7e84706SMuhammad Usama Anjumif [ "${PIPESTATUS[0]}" -ne 0 ]; then
211c7e84706SMuhammad Usama Anjum    exit ${PIPESTATUS[0]};
212c7e84706SMuhammad Usama Anjumfi
213e66d5b67SViresh Kumardmesg_dumps $OUTFILE
214c7e84706SMuhammad Usama Anjum
215c7e84706SMuhammad Usama Anjumktap_test_pass "Completed successfully"
216c7e84706SMuhammad Usama Anjum
217c7e84706SMuhammad Usama Anjumktap_print_totals
218c7e84706SMuhammad Usama Anjumexit "${KSFT_PASS}"
219