1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source cpu.sh
5source cpufreq.sh
6source governor.sh
7source module.sh
8source special-tests.sh
9
10FUNC=basic	# do basic tests by default
11OUTFILE=cpufreq_selftest
12SYSFS=
13CPUROOT=
14CPUFREQROOT=
15
16# Kselftest framework requirement - SKIP code is 4.
17ksft_skip=4
18
19helpme()
20{
21	printf "Usage: $0 [-h] [-todg args]
22	[-h <help>]
23	[-o <output-file-for-dump>]
24	[-t <basic: Basic cpufreq testing
25	     suspend: suspend/resume,
26	     hibernate: hibernate/resume,
27	     modtest: test driver or governor modules. Only to be used with -d or -g options,
28	     sptest1: Simple governor switch to produce lockdep.
29	     sptest2: Concurrent governor switch to produce lockdep.
30	     sptest3: Governor races, shuffle between governors quickly.
31	     sptest4: CPU hotplugs with updates to cpufreq files.>]
32	[-d <driver's module name: only with \"-t modtest>\"]
33	[-g <governor's module name: only with \"-t modtest>\"]
34	\n"
35	exit 2
36}
37
38prerequisite()
39{
40	msg="skip all tests:"
41
42	if [ $UID != 0 ]; then
43		echo $msg must be run as root >&2
44		exit $ksft_skip
45	fi
46
47	taskset -p 01 $$
48
49	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
50
51	if [ ! -d "$SYSFS" ]; then
52		echo $msg sysfs is not mounted >&2
53		exit 2
54	fi
55
56	CPUROOT=$SYSFS/devices/system/cpu
57	CPUFREQROOT="$CPUROOT/cpufreq"
58
59	if ! ls $CPUROOT/cpu* > /dev/null 2>&1; then
60		echo $msg cpus not available in sysfs >&2
61		exit 2
62	fi
63
64	if ! ls $CPUROOT/cpufreq > /dev/null 2>&1; then
65		echo $msg cpufreq directory not available in sysfs >&2
66		exit 2
67	fi
68}
69
70parse_arguments()
71{
72	while getopts ht:o:d:g: arg
73	do
74		case $arg in
75			h) # --help
76				helpme
77				;;
78
79			t) # --func_type (Function to perform: basic, suspend, hibernate, modtest, sptest1/2/3/4 (default: basic))
80				FUNC=$OPTARG
81				;;
82
83			o) # --output-file (Output file to store dumps)
84				OUTFILE=$OPTARG
85				;;
86
87			d) # --driver-mod-name (Name of the driver module)
88				DRIVER_MOD=$OPTARG
89				;;
90
91			g) # --governor-mod-name (Name of the governor module)
92				GOVERNOR_MOD=$OPTARG
93				;;
94
95			\?)
96				helpme
97				;;
98		esac
99	done
100}
101
102do_test()
103{
104	# Check if CPUs are managed by cpufreq or not
105	count=$(count_cpufreq_managed_cpus)
106
107	if [ $count = 0 -a $FUNC != "modtest" ]; then
108		echo "No cpu is managed by cpufreq core, exiting"
109		exit 2;
110	fi
111
112	case "$FUNC" in
113		"basic")
114		cpufreq_basic_tests
115		;;
116
117		"suspend")
118		do_suspend "suspend" 1
119		;;
120
121		"hibernate")
122		do_suspend "hibernate" 1
123		;;
124
125		"modtest")
126		# Do we have modules in place?
127		if [ -z $DRIVER_MOD ] && [ -z $GOVERNOR_MOD ]; then
128			echo "No driver or governor module passed with -d or -g"
129			exit 2;
130		fi
131
132		if [ $DRIVER_MOD ]; then
133			if [ $GOVERNOR_MOD ]; then
134				module_test $DRIVER_MOD $GOVERNOR_MOD
135			else
136				module_driver_test $DRIVER_MOD
137			fi
138		else
139			if [ $count = 0 ]; then
140				echo "No cpu is managed by cpufreq core, exiting"
141				exit 2;
142			fi
143
144			module_governor_test $GOVERNOR_MOD
145		fi
146		;;
147
148		"sptest1")
149		simple_lockdep
150		;;
151
152		"sptest2")
153		concurrent_lockdep
154		;;
155
156		"sptest3")
157		governor_race
158		;;
159
160		"sptest4")
161		hotplug_with_updates
162		;;
163
164		*)
165		echo "Invalid [-f] function type"
166		helpme
167		;;
168	esac
169}
170
171# clear dumps
172# $1: file name
173clear_dumps()
174{
175	echo "" > $1.txt
176	echo "" > $1.dmesg_cpufreq.txt
177	echo "" > $1.dmesg_full.txt
178}
179
180# $1: output file name
181dmesg_dumps()
182{
183	dmesg | grep cpufreq >> $1.dmesg_cpufreq.txt
184
185	# We may need the full logs as well
186	dmesg >> $1.dmesg_full.txt
187}
188
189# Parse arguments
190parse_arguments $@
191
192# Make sure all requirements are met
193prerequisite
194
195# Run requested functions
196clear_dumps $OUTFILE
197do_test >> $OUTFILE.txt
198dmesg_dumps $OUTFILE
199