xref: /openbmc/linux/crypto/xor.c (revision 2208f39c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * xor.c : Multiple Devices driver for Linux
4  *
5  * Copyright (C) 1996, 1997, 1998, 1999, 2000,
6  * Ingo Molnar, Matti Aarnio, Jakub Jelinek, Richard Henderson.
7  *
8  * Dispatch optimized RAID-5 checksumming functions.
9  */
10 
11 #define BH_TRACE 0
12 #include <linux/module.h>
13 #include <linux/gfp.h>
14 #include <linux/raid/xor.h>
15 #include <linux/jiffies.h>
16 #include <linux/preempt.h>
17 #include <asm/xor.h>
18 
19 #ifndef XOR_SELECT_TEMPLATE
20 #define XOR_SELECT_TEMPLATE(x) (x)
21 #endif
22 
23 /* The xor routines to use.  */
24 static struct xor_block_template *active_template;
25 
26 void
27 xor_blocks(unsigned int src_count, unsigned int bytes, void *dest, void **srcs)
28 {
29 	unsigned long *p1, *p2, *p3, *p4;
30 
31 	p1 = (unsigned long *) srcs[0];
32 	if (src_count == 1) {
33 		active_template->do_2(bytes, dest, p1);
34 		return;
35 	}
36 
37 	p2 = (unsigned long *) srcs[1];
38 	if (src_count == 2) {
39 		active_template->do_3(bytes, dest, p1, p2);
40 		return;
41 	}
42 
43 	p3 = (unsigned long *) srcs[2];
44 	if (src_count == 3) {
45 		active_template->do_4(bytes, dest, p1, p2, p3);
46 		return;
47 	}
48 
49 	p4 = (unsigned long *) srcs[3];
50 	active_template->do_5(bytes, dest, p1, p2, p3, p4);
51 }
52 EXPORT_SYMBOL(xor_blocks);
53 
54 /* Set of all registered templates.  */
55 static struct xor_block_template *__initdata template_list;
56 
57 #ifndef MODULE
58 static void __init do_xor_register(struct xor_block_template *tmpl)
59 {
60 	tmpl->next = template_list;
61 	template_list = tmpl;
62 }
63 
64 static int __init register_xor_blocks(void)
65 {
66 	active_template = XOR_SELECT_TEMPLATE(NULL);
67 
68 	if (!active_template) {
69 #define xor_speed	do_xor_register
70 		// register all the templates and pick the first as the default
71 		XOR_TRY_TEMPLATES;
72 #undef xor_speed
73 		active_template = template_list;
74 	}
75 	return 0;
76 }
77 #endif
78 
79 #define BENCH_SIZE	4096
80 #define REPS		800U
81 
82 static void __init
83 do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2)
84 {
85 	int speed;
86 	int i, j;
87 	ktime_t min, start, diff;
88 
89 	tmpl->next = template_list;
90 	template_list = tmpl;
91 
92 	preempt_disable();
93 
94 	min = (ktime_t)S64_MAX;
95 	for (i = 0; i < 3; i++) {
96 		start = ktime_get();
97 		for (j = 0; j < REPS; j++) {
98 			mb(); /* prevent loop optimzation */
99 			tmpl->do_2(BENCH_SIZE, b1, b2);
100 			mb();
101 		}
102 		diff = ktime_sub(ktime_get(), start);
103 		if (diff < min)
104 			min = diff;
105 	}
106 
107 	preempt_enable();
108 
109 	// bytes/ns == GB/s, multiply by 1000 to get MB/s [not MiB/s]
110 	speed = (1000 * REPS * BENCH_SIZE) / (unsigned int)ktime_to_ns(min);
111 	tmpl->speed = speed;
112 
113 	pr_info("   %-16s: %5d MB/sec\n", tmpl->name, speed);
114 }
115 
116 static int __init
117 calibrate_xor_blocks(void)
118 {
119 	void *b1, *b2;
120 	struct xor_block_template *f, *fastest;
121 
122 	fastest = XOR_SELECT_TEMPLATE(NULL);
123 
124 	if (fastest) {
125 		printk(KERN_INFO "xor: automatically using best "
126 				 "checksumming function   %-10s\n",
127 		       fastest->name);
128 		goto out;
129 	}
130 
131 	b1 = (void *) __get_free_pages(GFP_KERNEL, 2);
132 	if (!b1) {
133 		printk(KERN_WARNING "xor: Yikes!  No memory available.\n");
134 		return -ENOMEM;
135 	}
136 	b2 = b1 + 2*PAGE_SIZE + BENCH_SIZE;
137 
138 	/*
139 	 * If this arch/cpu has a short-circuited selection, don't loop through
140 	 * all the possible functions, just test the best one
141 	 */
142 
143 #define xor_speed(templ)	do_xor_speed((templ), b1, b2)
144 
145 	printk(KERN_INFO "xor: measuring software checksum speed\n");
146 	template_list = NULL;
147 	XOR_TRY_TEMPLATES;
148 	fastest = template_list;
149 	for (f = fastest; f; f = f->next)
150 		if (f->speed > fastest->speed)
151 			fastest = f;
152 
153 	pr_info("xor: using function: %s (%d MB/sec)\n",
154 	       fastest->name, fastest->speed);
155 
156 #undef xor_speed
157 
158 	free_pages((unsigned long)b1, 2);
159 out:
160 	active_template = fastest;
161 	return 0;
162 }
163 
164 static __exit void xor_exit(void) { }
165 
166 MODULE_LICENSE("GPL");
167 
168 #ifndef MODULE
169 /* when built-in xor.o must initialize before drivers/md/md.o */
170 core_initcall(register_xor_blocks);
171 #endif
172 
173 module_init(calibrate_xor_blocks);
174 module_exit(xor_exit);
175