aboutsummaryrefslogtreecommitdiff
path: root/drivers/firmware/arm_scpi_test.c
blob: c21b35e6f4b887ce91c2a9193c635e6beedf8e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 * Test code for System Control and Power Interface (SCPI)
 *
 * Copyright (C) 2015 Linaro Ltd.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/scpi_protocol.h>


static int stress_time;
module_param(stress_time, int, 0644);
MODULE_PARM_DESC(stress_time, "Number of seconds to run each stress test for, overides each test's default.");

static int run;
static struct kernel_param_ops run_ops;
module_param_cb(run, &run_ops, &run, 0644);
MODULE_PARM_DESC(run, "The number of the test case to run, or -1 for all, 0 to stop tests.");


static struct scpi_ops *scpi;

static struct task_struct *main_thread;

static DEFINE_MUTEX(main_thread_lock);


#define MAX_TEST_THREADS 4

static struct test_thread {
	struct task_struct *task;
	int thread_num;
} test_threads[MAX_TEST_THREADS];

static DEFINE_MUTEX(thread_lock);


#define MAX_POWER_DOMAINS 8

static u16 num_sensors;
static u8 num_pd;
static u8 num_opps[MAX_POWER_DOMAINS];
static struct mutex pd_lock[MAX_POWER_DOMAINS];
static u8 num_devices_with_power_states;


#define	FLAG_SERIAL_DVFS	(1<<0)
#define	FLAG_SERIAL_PD		(1<<1)

static int test_flags;


static int sensor_pmic = -1;


static u32 random_seed;

static u32 random(u32 range)
{
	random_seed = random_seed*69069+1;

	return ((u64)random_seed * (u64)range) >> 32;
}


static atomic_t passes;
static atomic_t failures;

static bool fail_on(bool fail)
{
	if (fail)
		atomic_inc(&failures);
	else
		atomic_inc(&passes);
	return fail;
}

static void show_results(const char *title)
{
	int fail = atomic_xchg(&failures, 0);
	int pass = atomic_xchg(&passes, 0);

	if (fail)
		pr_err("Results for '%s' is %d/%d (pass/fail)\n", title, pass, fail);
	else
		pr_info("Results for '%s' is %d/%d (pass/fail)\n", title, pass, fail);
}


static bool check_name(const char *name)
{
	char c;

	if (!isalpha(*name++))
		return false;

	while ((c = *name++))
		if (!isalnum(c) && c != '_')
			return false;

	return true;
}

static u64 get_sensor(u16 id)
{
	u64 val;
	int ret;

	ret = scpi->sensor_get_value(id, &val);
	if (fail_on(ret < 0))
		pr_err("FAILED sensor_get_value %d (%d)\n", id, ret);

	return val;
}

static void init_sensors(void)
{
	u16 id;
	int ret;

	ret = scpi->sensor_get_capability(&num_sensors);
	if (fail_on(ret))
		pr_err("FAILED sensor_get_capability (%d)\n", ret);

	pr_info("num_sensors: %d\n", num_sensors);

	for (id = 0; id < num_sensors; id++) {

		struct scpi_sensor_info info;
		char name[sizeof(info.name) + 1];

		ret = scpi->sensor_get_info(id, &info);
		if (fail_on(ret)) {
			pr_err("FAILED sensor_get_info (%d)\n", ret);
			continue;
		}

		/* Get sensor name, guarding against missing NUL terminator */
		memcpy(name, info.name, sizeof(info.name));
		name[sizeof(info.name)] = 0;

		pr_info("sensor[%d] id=%d class=%d trigger=%d name=%s\n", id,
			info.sensor_id, info.class, info.trigger_type, name);

		if (fail_on(id != info.sensor_id))
			pr_err("FAILED bad sensor id\n");
		if (fail_on(info.class > 4))
			pr_err("FAILED bad sensor class\n");
		if (fail_on(info.trigger_type > 3))
			pr_err("FAILED bad sensor trigger type\n");
		if (fail_on(strlen(name) >= sizeof(info.name) || !check_name(name)))
			pr_err("FAILED bad name\n");

		pr_info("sensor[%d] value is %llu\n", id, get_sensor(id));
		if (strstr(name, "PMIC"))
			sensor_pmic = id;
	}
}

static int get_dvfs(u8 pd)
{
	int ret = scpi->dvfs_get_idx(pd);

	if (fail_on(ret < 0))
		pr_err("FAILED get_dvfs %d (%d)\n", pd, ret);
	else if (fail_on(ret >= num_opps[pd]))
		pr_err("FAILED get_dvfs %d returned out of range index (%d)\n", pd, ret);

	return ret;
}

static int set_dvfs(u8 pd, u8 opp)
{
	int ret;

	if (test_flags & FLAG_SERIAL_DVFS)
		mutex_lock(&pd_lock[0]);
	else if (test_flags & FLAG_SERIAL_PD)
		mutex_lock(&pd_lock[pd]);

	ret = scpi->dvfs_set_idx(pd, opp);

	if (test_flags & FLAG_SERIAL_DVFS)
		mutex_unlock(&pd_lock[0]);
	else if (test_flags & FLAG_SERIAL_PD)
		mutex_unlock(&pd_lock[pd]);

	if (fail_on(ret < 0))
		pr_err("FAILED set_dvfs %d %d (%d)\n", pd, opp, ret);

	return ret;
}

static void init_dvfs(void)
{
	u8 pd;

	for (pd = 0; pd < MAX_POWER_DOMAINS; ++pd) {
		struct scpi_dvfs_info *info;
		int opp;

		info = scpi->dvfs_get_info(pd);
		if (IS_ERR(info)) {
			pr_info("dvfs_get_info %d failed with %d assume because no more power domains\n",
				pd, (int)PTR_ERR(info));
			break;
		}

		num_opps[pd] = info->count;
		mutex_init(&pd_lock[pd]);
		pr_info("pd[%d] count=%u latency=%u\n",
			pd, info->count, info->latency);

		opp = get_dvfs(pd);
		pr_info("pd[%d] current opp=%d\n", pd, opp);

		for (opp = 0; opp < info->count; ++opp) {
			pr_info("pd[%d].opp[%d] freq=%u m_volt=%u\n", pd, opp,
				info->opps[opp].freq, info->opps[opp].m_volt);
			/*
			 * Try setting each opp. Note, failure is not necessarily
			 * an error because cpufreq may be setting values too.
			 */
			set_dvfs(pd, opp);
			if (get_dvfs(pd) == opp)
				pr_info("pd[%d] set to opp %d OK\n", pd, opp);
			else
				pr_warn("pd[%d] failed to set opp to %d\n", pd, opp);
		}
	}

	if (!pd) {
		/* Assume device should have at least one DVFS power domain */
		pr_err("FAILED no power domains\n");
		fail_on(true);
	}
	num_pd = pd;
}

static int device_get_power_state(u16 dev_id)
{
	int ret;

	ret = scpi->device_get_power_state(dev_id);
	if (fail_on(ret < 0))
		pr_err("FAILED device_get_power_state %d (%d)\n", dev_id, ret);

	return ret;
}

static int device_set_power_state(u16 dev_id, u8 pstate)
{
	int ret;

	ret = scpi->device_set_power_state(dev_id, pstate);
	if (fail_on(ret < 0))
		pr_err("FAILED device_get_power_state %d (%d)\n", dev_id, ret);

	return ret;
}

static void init_device_power_states(void)
{
	u16 dev_id;

	for (dev_id = 0; dev_id < 0xffff; ++dev_id) {
		int state = scpi->device_get_power_state(dev_id);

		if (state < 0) {
			pr_info("device_get_power_state %d failed with %d assume because no more devices\n",
				dev_id, state);
			break;
		}

		pr_info("device[%d] current state=%d\n", dev_id, state);

		device_set_power_state(dev_id, state);
		if (device_get_power_state(dev_id) == state)
			pr_info("device[%d] set state to %d OK\n", dev_id, state);
		else
			pr_warn("device[%d] failed set state to %d\n", dev_id, state);
	}

	if (!dev_id) {
		/* Assume device should have at least one device power state */
		pr_err("FAILED no devices with power states\n");
		fail_on(true);
	}
	num_devices_with_power_states = dev_id;
}

static int stress_pmic(void *data)
{
	int sensor, pd, opp;

	while (!kthread_should_stop()) {
		sensor = sensor_pmic;
		pd = random(num_pd);
		opp = random(num_opps[pd]);

		switch (random(3)) {
		case 0:
			if (sensor >= 0) {
				get_sensor(sensor);
				break;
			}
			/* If no sensor, do DFVS... */
		case 1:
			set_dvfs(pd, opp);
			break;
		default:
			msleep(random(20));
			break;
		}
	}

	return 0;
}

static int stress_all(void *data)
{
	int sensor, pd, opp;

	while (!kthread_should_stop()) {
		sensor = random(num_sensors);
		pd = random(num_pd);
		opp = random(num_opps[pd]);

		switch (random(4)) {
		case 0:
			set_dvfs(pd, opp);
			break;
		case 1:
			opp = get_dvfs(pd);
			break;
		case 2:
			get_sensor(sensor);
			break;
		default:
			msleep(random(20));
			break;
		}
	}

	return 0;
}

struct test {
	const char *title;
	int (*thread_fn)(void *);
	int flags;
	int num_threads;
	int duration;
};

static void stop_test_threads(void)
{
	int t, ret;

	for (t = 0; t < MAX_TEST_THREADS; ++t) {
		struct test_thread *thread = &test_threads[t];

		mutex_lock(&thread_lock);
		if (thread->task) {
			ret = kthread_stop(thread->task);
			thread->task = NULL;
			if (ret)
				pr_warn("Test thread %d exited with status %d\n", t, ret);
		}
		mutex_unlock(&thread_lock);
	}
}

static void run_test(struct test *test)
{
	int num_threads = min(test->num_threads, MAX_TEST_THREADS);
	int duration = stress_time;
	int t;

	if (test->duration <= 0)
		duration = 0;
	else if (duration <= 0)
		duration = test->duration;

	pr_info("Running test '%s' for %d seconds\n", test->title, duration);

	test_flags = test->flags;

	for (t = 0; t < num_threads; ++t) {
		struct test_thread *thread = &test_threads[t];
		struct task_struct *task;

		mutex_lock(&thread_lock);
		thread->thread_num = t;
		task = kthread_run(test->thread_fn, thread, "scpi-test-%d", t);
		if (IS_ERR(task))
			pr_warn("Failed to create test thread %d\n", t);
		else
			thread->task = task;
		mutex_unlock(&thread_lock);
	}

	schedule_timeout_interruptible(msecs_to_jiffies(duration * 1000));

	stop_test_threads();

	show_results(test->title);
}

static struct test tests[] = {
	{"Stress All, concurrent DVFS",
		stress_all,	0,			MAX_TEST_THREADS, 60},
	{"Stress All, concurrent DVFS on different PDs",
		stress_all,	FLAG_SERIAL_PD,		MAX_TEST_THREADS, 60},
	{"Stress All, no concurrent DVFS",
		stress_all,	FLAG_SERIAL_DVFS,	MAX_TEST_THREADS, 60},
	{"Stress PMIC, concurrent DVFS",
		stress_pmic,	0,			MAX_TEST_THREADS, 60},
	{"Stress PMIC, concurrent DVFS on different PDs",
		stress_pmic,	FLAG_SERIAL_PD,		MAX_TEST_THREADS, 60},
	{"Stress PMIC, no concurrent DVFS",
		stress_pmic,	FLAG_SERIAL_DVFS,	MAX_TEST_THREADS, 60},
	{}
};

static int main_thread_fn(void *data)
{
	struct test *test = tests;
	int i = 1;

	for (; test->title && !kthread_should_stop(); ++test, ++i)
		if (run < 0 || run == i)
			run_test(test);

	run = 0;
	return 0;
}

static DEFINE_MUTEX(setup_lock);

static int setup(void)
{
	int ret = 0;

	mutex_lock(&setup_lock);

	if (!scpi) {
		int tries = 12;

		pr_info("Initial setup\n");
		while ((scpi = get_scpi_ops()) == 0 && --tries) {
			pr_info("Waiting for get_scpi_ops\n");
			msleep(5000);
		}

		if (scpi) {
			init_sensors();
			init_dvfs();
			init_device_power_states();
			show_results("Initial setup");
		} else {
			pr_err("Given up on get_scpi_ops\n");
			ret = -ENODEV;
		}
	}

	mutex_unlock(&setup_lock);

	return ret;
}

static int start_tests(void)
{
	struct task_struct *task;
	int ret;

	ret = setup();
	if (ret) {
		run = 0;
		return ret;
	}

	pr_info("Creating main thread\n");
	mutex_lock(&main_thread_lock);
	if (main_thread) {
		ret = -EBUSY;
	} else {
		task = kthread_run(main_thread_fn, 0, "scpi-test-main");
		if (IS_ERR(task))
			ret = PTR_ERR(task);
		else
			main_thread = task;
	}
	mutex_unlock(&main_thread_lock);

	if (ret) {
		pr_err("Failed to create main thread (%d)\n", ret);
		run = 0;
	}

	return ret;
}

static void stop_tests(void)
{
	pr_info("Stopping tests\n");
	mutex_lock(&main_thread_lock);
	if (main_thread)
		kthread_stop(main_thread);
	main_thread = NULL;
	mutex_unlock(&main_thread_lock);
}

static int param_set_running(const char *val, const struct kernel_param *kp)
{
	int ret;

	ret = param_set_int(val, kp);
	if (!ret) {
		if (run)
			ret = start_tests();
		else
			stop_tests();
	}

	return ret;
}

static struct kernel_param_ops run_ops = {
	.set = param_set_running,
	.get = param_get_int,
};


static int scpi_test_init(void)
{
	return 0;
}

static void scpi_test_exit(void)
{
	stop_tests();
}

module_init(scpi_test_init);
module_exit(scpi_test_exit);


MODULE_AUTHOR("Jon Medhurst (Tixy) <tixy@linaro.org>");
MODULE_DESCRIPTION("ARM SCPI driver tests");
MODULE_LICENSE("GPL v2");