xref: /openbmc/linux/lib/raid6/sse1.c (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*dd165a65SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cc4589ebSDavid Woodhouse /* -*- linux-c -*- ------------------------------------------------------- *
3cc4589ebSDavid Woodhouse  *
4cc4589ebSDavid Woodhouse  *   Copyright 2002 H. Peter Anvin - All Rights Reserved
5cc4589ebSDavid Woodhouse  *
6cc4589ebSDavid Woodhouse  * ----------------------------------------------------------------------- */
7cc4589ebSDavid Woodhouse 
8cc4589ebSDavid Woodhouse /*
9a8e026c7SNeilBrown  * raid6/sse1.c
10cc4589ebSDavid Woodhouse  *
11cc4589ebSDavid Woodhouse  * SSE-1/MMXEXT implementation of RAID-6 syndrome functions
12cc4589ebSDavid Woodhouse  *
13cc4589ebSDavid Woodhouse  * This is really an MMX implementation, but it requires SSE-1 or
14cc4589ebSDavid Woodhouse  * AMD MMXEXT for prefetch support and a few other features.  The
15cc4589ebSDavid Woodhouse  * support for nontemporal memory accesses is enough to make this
16cc4589ebSDavid Woodhouse  * worthwhile as a separate implementation.
17cc4589ebSDavid Woodhouse  */
18cc4589ebSDavid Woodhouse 
194f8c55c5SYuanhan Liu #ifdef CONFIG_X86_32
20cc4589ebSDavid Woodhouse 
21cc4589ebSDavid Woodhouse #include <linux/raid/pq.h>
22a8e026c7SNeilBrown #include "x86.h"
23cc4589ebSDavid Woodhouse 
24a8e026c7SNeilBrown /* Defined in raid6/mmx.c */
25cc4589ebSDavid Woodhouse extern const struct raid6_mmx_constants {
26cc4589ebSDavid Woodhouse 	u64 x1d;
27cc4589ebSDavid Woodhouse } raid6_mmx_constants;
28cc4589ebSDavid Woodhouse 
raid6_have_sse1_or_mmxext(void)29cc4589ebSDavid Woodhouse static int raid6_have_sse1_or_mmxext(void)
30cc4589ebSDavid Woodhouse {
31cc4589ebSDavid Woodhouse 	/* Not really boot_cpu but "all_cpus" */
32cc4589ebSDavid Woodhouse 	return boot_cpu_has(X86_FEATURE_MMX) &&
33cc4589ebSDavid Woodhouse 		(boot_cpu_has(X86_FEATURE_XMM) ||
34cc4589ebSDavid Woodhouse 		 boot_cpu_has(X86_FEATURE_MMXEXT));
35cc4589ebSDavid Woodhouse }
36cc4589ebSDavid Woodhouse 
37cc4589ebSDavid Woodhouse /*
38cc4589ebSDavid Woodhouse  * Plain SSE1 implementation
39cc4589ebSDavid Woodhouse  */
raid6_sse11_gen_syndrome(int disks,size_t bytes,void ** ptrs)40cc4589ebSDavid Woodhouse static void raid6_sse11_gen_syndrome(int disks, size_t bytes, void **ptrs)
41cc4589ebSDavid Woodhouse {
42cc4589ebSDavid Woodhouse 	u8 **dptr = (u8 **)ptrs;
43cc4589ebSDavid Woodhouse 	u8 *p, *q;
44cc4589ebSDavid Woodhouse 	int d, z, z0;
45cc4589ebSDavid Woodhouse 
46cc4589ebSDavid Woodhouse 	z0 = disks - 3;		/* Highest data disk */
47cc4589ebSDavid Woodhouse 	p = dptr[z0+1];		/* XOR parity */
48cc4589ebSDavid Woodhouse 	q = dptr[z0+2];		/* RS syndrome */
49cc4589ebSDavid Woodhouse 
50cc4589ebSDavid Woodhouse 	kernel_fpu_begin();
51cc4589ebSDavid Woodhouse 
52cc4589ebSDavid Woodhouse 	asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
53cc4589ebSDavid Woodhouse 	asm volatile("pxor %mm5,%mm5");	/* Zero temp */
54cc4589ebSDavid Woodhouse 
55cc4589ebSDavid Woodhouse 	for ( d = 0 ; d < bytes ; d += 8 ) {
56cc4589ebSDavid Woodhouse 		asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
57cc4589ebSDavid Woodhouse 		asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
58cc4589ebSDavid Woodhouse 		asm volatile("prefetchnta %0" : : "m" (dptr[z0-1][d]));
59cc4589ebSDavid Woodhouse 		asm volatile("movq %mm2,%mm4");	/* Q[0] */
60cc4589ebSDavid Woodhouse 		asm volatile("movq %0,%%mm6" : : "m" (dptr[z0-1][d]));
61cc4589ebSDavid Woodhouse 		for ( z = z0-2 ; z >= 0 ; z-- ) {
62cc4589ebSDavid Woodhouse 			asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
63cc4589ebSDavid Woodhouse 			asm volatile("pcmpgtb %mm4,%mm5");
64cc4589ebSDavid Woodhouse 			asm volatile("paddb %mm4,%mm4");
65cc4589ebSDavid Woodhouse 			asm volatile("pand %mm0,%mm5");
66cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm4");
67cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm5");
68cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm6,%mm2");
69cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm6,%mm4");
70cc4589ebSDavid Woodhouse 			asm volatile("movq %0,%%mm6" : : "m" (dptr[z][d]));
71cc4589ebSDavid Woodhouse 		}
72cc4589ebSDavid Woodhouse 		asm volatile("pcmpgtb %mm4,%mm5");
73cc4589ebSDavid Woodhouse 		asm volatile("paddb %mm4,%mm4");
74cc4589ebSDavid Woodhouse 		asm volatile("pand %mm0,%mm5");
75cc4589ebSDavid Woodhouse 		asm volatile("pxor %mm5,%mm4");
76cc4589ebSDavid Woodhouse 		asm volatile("pxor %mm5,%mm5");
77cc4589ebSDavid Woodhouse 		asm volatile("pxor %mm6,%mm2");
78cc4589ebSDavid Woodhouse 		asm volatile("pxor %mm6,%mm4");
79cc4589ebSDavid Woodhouse 
80cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm2,%0" : "=m" (p[d]));
81cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm4,%0" : "=m" (q[d]));
82cc4589ebSDavid Woodhouse 	}
83cc4589ebSDavid Woodhouse 
84cc4589ebSDavid Woodhouse 	asm volatile("sfence" : : : "memory");
85cc4589ebSDavid Woodhouse 	kernel_fpu_end();
86cc4589ebSDavid Woodhouse }
87cc4589ebSDavid Woodhouse 
88cc4589ebSDavid Woodhouse const struct raid6_calls raid6_sse1x1 = {
89cc4589ebSDavid Woodhouse 	raid6_sse11_gen_syndrome,
90fe5cbc6eSMarkus Stockhausen 	NULL,			/* XOR not yet implemented */
91cc4589ebSDavid Woodhouse 	raid6_have_sse1_or_mmxext,
92cc4589ebSDavid Woodhouse 	"sse1x1",
93cc4589ebSDavid Woodhouse 	1			/* Has cache hints */
94cc4589ebSDavid Woodhouse };
95cc4589ebSDavid Woodhouse 
96cc4589ebSDavid Woodhouse /*
97cc4589ebSDavid Woodhouse  * Unrolled-by-2 SSE1 implementation
98cc4589ebSDavid Woodhouse  */
raid6_sse12_gen_syndrome(int disks,size_t bytes,void ** ptrs)99cc4589ebSDavid Woodhouse static void raid6_sse12_gen_syndrome(int disks, size_t bytes, void **ptrs)
100cc4589ebSDavid Woodhouse {
101cc4589ebSDavid Woodhouse 	u8 **dptr = (u8 **)ptrs;
102cc4589ebSDavid Woodhouse 	u8 *p, *q;
103cc4589ebSDavid Woodhouse 	int d, z, z0;
104cc4589ebSDavid Woodhouse 
105cc4589ebSDavid Woodhouse 	z0 = disks - 3;		/* Highest data disk */
106cc4589ebSDavid Woodhouse 	p = dptr[z0+1];		/* XOR parity */
107cc4589ebSDavid Woodhouse 	q = dptr[z0+2];		/* RS syndrome */
108cc4589ebSDavid Woodhouse 
109cc4589ebSDavid Woodhouse 	kernel_fpu_begin();
110cc4589ebSDavid Woodhouse 
111cc4589ebSDavid Woodhouse 	asm volatile("movq %0,%%mm0" : : "m" (raid6_mmx_constants.x1d));
112cc4589ebSDavid Woodhouse 	asm volatile("pxor %mm5,%mm5");	/* Zero temp */
113cc4589ebSDavid Woodhouse 	asm volatile("pxor %mm7,%mm7"); /* Zero temp */
114cc4589ebSDavid Woodhouse 
115cc4589ebSDavid Woodhouse 	/* We uniformly assume a single prefetch covers at least 16 bytes */
116cc4589ebSDavid Woodhouse 	for ( d = 0 ; d < bytes ; d += 16 ) {
117cc4589ebSDavid Woodhouse 		asm volatile("prefetchnta %0" : : "m" (dptr[z0][d]));
118cc4589ebSDavid Woodhouse 		asm volatile("movq %0,%%mm2" : : "m" (dptr[z0][d])); /* P[0] */
119cc4589ebSDavid Woodhouse 		asm volatile("movq %0,%%mm3" : : "m" (dptr[z0][d+8])); /* P[1] */
120cc4589ebSDavid Woodhouse 		asm volatile("movq %mm2,%mm4");	/* Q[0] */
121cc4589ebSDavid Woodhouse 		asm volatile("movq %mm3,%mm6"); /* Q[1] */
122cc4589ebSDavid Woodhouse 		for ( z = z0-1 ; z >= 0 ; z-- ) {
123cc4589ebSDavid Woodhouse 			asm volatile("prefetchnta %0" : : "m" (dptr[z][d]));
124cc4589ebSDavid Woodhouse 			asm volatile("pcmpgtb %mm4,%mm5");
125cc4589ebSDavid Woodhouse 			asm volatile("pcmpgtb %mm6,%mm7");
126cc4589ebSDavid Woodhouse 			asm volatile("paddb %mm4,%mm4");
127cc4589ebSDavid Woodhouse 			asm volatile("paddb %mm6,%mm6");
128cc4589ebSDavid Woodhouse 			asm volatile("pand %mm0,%mm5");
129cc4589ebSDavid Woodhouse 			asm volatile("pand %mm0,%mm7");
130cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm4");
131cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm7,%mm6");
132cc4589ebSDavid Woodhouse 			asm volatile("movq %0,%%mm5" : : "m" (dptr[z][d]));
133cc4589ebSDavid Woodhouse 			asm volatile("movq %0,%%mm7" : : "m" (dptr[z][d+8]));
134cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm2");
135cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm7,%mm3");
136cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm4");
137cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm7,%mm6");
138cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm5,%mm5");
139cc4589ebSDavid Woodhouse 			asm volatile("pxor %mm7,%mm7");
140cc4589ebSDavid Woodhouse 		}
141cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm2,%0" : "=m" (p[d]));
142cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm3,%0" : "=m" (p[d+8]));
143cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm4,%0" : "=m" (q[d]));
144cc4589ebSDavid Woodhouse 		asm volatile("movntq %%mm6,%0" : "=m" (q[d+8]));
145cc4589ebSDavid Woodhouse 	}
146cc4589ebSDavid Woodhouse 
147cc4589ebSDavid Woodhouse 	asm volatile("sfence" : :: "memory");
148cc4589ebSDavid Woodhouse 	kernel_fpu_end();
149cc4589ebSDavid Woodhouse }
150cc4589ebSDavid Woodhouse 
151cc4589ebSDavid Woodhouse const struct raid6_calls raid6_sse1x2 = {
152cc4589ebSDavid Woodhouse 	raid6_sse12_gen_syndrome,
153fe5cbc6eSMarkus Stockhausen 	NULL,			/* XOR not yet implemented */
154cc4589ebSDavid Woodhouse 	raid6_have_sse1_or_mmxext,
155cc4589ebSDavid Woodhouse 	"sse1x2",
156cc4589ebSDavid Woodhouse 	1			/* Has cache hints */
157cc4589ebSDavid Woodhouse };
158cc4589ebSDavid Woodhouse 
159cc4589ebSDavid Woodhouse #endif
160