Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) static int ss_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	ktime_t start, finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	int loops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	int cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	DEFINE_RAW_SPINLOCK(ss_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	loops = 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	cont = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	while (cont) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		raw_spin_lock(&ss_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		loops--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		if (loops == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			cont = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		raw_spin_unlock(&ss_spin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	finish = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	*val = ktime_us_delta(finish, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) DEFINE_SIMPLE_ATTRIBUTE(fops_ss, ss_get, NULL, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct spin_multi_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	raw_spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	atomic_t start_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	atomic_t enter_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	atomic_t exit_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int loops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct spin_multi_per_thread {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct spin_multi_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ktime_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int multi_other(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int loops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct spin_multi_per_thread *pt = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct spin_multi_state *s = pt->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	loops = s->loops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	cont = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	atomic_dec(&s->enter_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	while (atomic_read(&s->enter_wait))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		; /* spin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	pt->start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	atomic_dec(&s->start_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	while (atomic_read(&s->start_wait))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		; /* spin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	while (cont) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		raw_spin_lock(&s->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		loops--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (loops == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			cont = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		raw_spin_unlock(&s->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	atomic_dec(&s->exit_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	while (atomic_read(&s->exit_wait))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		; /* spin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int multi_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ktime_t finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct spin_multi_state ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct spin_multi_per_thread t1, t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	ms.lock = __RAW_SPIN_LOCK_UNLOCKED("multi_get");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	ms.loops = 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	atomic_set(&ms.start_wait, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	atomic_set(&ms.enter_wait, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	atomic_set(&ms.exit_wait, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	t1.state = &ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	t2.state = &ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	kthread_run(multi_other, &t2, "multi_get");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	multi_other(&t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	finish = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	*val = ktime_us_delta(finish, t1.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) DEFINE_SIMPLE_ATTRIBUTE(fops_multi, multi_get, NULL, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int __init spinlock_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	debugfs_create_file("spin_single", S_IRUGO, mips_debugfs_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			    &fops_ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	debugfs_create_file("spin_multi", S_IRUGO, mips_debugfs_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			    &fops_multi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) device_initcall(spinlock_test);