blob: 69f3a9ff870f090f5a235bf8092e02d7410f99eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
Atsushi SAKAI93ed05e2009-03-31 14:57:37 +11008 * Boston MA 02111-1307, USA; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * (at your option) any later version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
14 * raid6altivec$#.c
15 *
16 * $#-way unrolled portable integer math RAID-6 instruction set
17 *
Vladimir Dronnikovdce3a7a2009-10-16 16:25:19 +110018 * This file is postprocessed using unroll.awk
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * <benh> hpa: in process,
21 * you can just "steal" the vec unit with enable_kernel_altivec() (but
22 * bracked this with preempt_disable/enable or in a lock)
23 */
24
Dan Williamsf701d582009-03-31 15:09:39 +110025#include <linux/raid/pq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#ifdef CONFIG_ALTIVEC
28
29#include <altivec.h>
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070030#ifdef __KERNEL__
31# include <asm/system.h>
32# include <asm/cputable.h>
David Howellsae3a1972012-03-28 18:30:02 +010033# include <asm/switch_to.h>
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070037 * This is the C data type to use. We use a vector of
38 * signed char so vec_cmpgt() will generate the right
39 * instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070042typedef vector signed char unative_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
H. Peter Anvind7e70ba2005-09-16 19:27:29 -070044#define NBYTES(x) ((vector signed char) {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x})
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define NSIZE sizeof(unative_t)
46
47/*
48 * The SHLBYTE() operation shifts each byte left by 1, *not*
49 * rolling over into the next byte
50 */
51static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
52{
53 return vec_add(v,v);
54}
55
56/*
57 * The MASK() operation returns 0xFF in any byte for which the high
58 * bit is 1, 0x00 for any byte for which the high bit is 0.
59 */
60static inline __attribute_const__ unative_t MASK(unative_t v)
61{
62 unative_t zv = NBYTES(0);
63
64 /* vec_cmpgt returns a vector bool char; thus the need for the cast */
65 return (unative_t)vec_cmpgt(zv, v);
66}
67
68
69/* This is noinline to make damned sure that gcc doesn't move any of the
70 Altivec code around the enable/disable code */
71static void noinline
72raid6_altivec$#_gen_syndrome_real(int disks, size_t bytes, void **ptrs)
73{
74 u8 **dptr = (u8 **)ptrs;
75 u8 *p, *q;
76 int d, z, z0;
77
78 unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
79 unative_t x1d = NBYTES(0x1d);
80
81 z0 = disks - 3; /* Highest data disk */
82 p = dptr[z0+1]; /* XOR parity */
83 q = dptr[z0+2]; /* RS syndrome */
84
85 for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
86 wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
87 for ( z = z0-1 ; z >= 0 ; z-- ) {
88 wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
89 wp$$ = vec_xor(wp$$, wd$$);
90 w2$$ = MASK(wq$$);
91 w1$$ = SHLBYTE(wq$$);
92 w2$$ = vec_and(w2$$, x1d);
93 w1$$ = vec_xor(w1$$, w2$$);
94 wq$$ = vec_xor(w1$$, wd$$);
95 }
96 *(unative_t *)&p[d+NSIZE*$$] = wp$$;
97 *(unative_t *)&q[d+NSIZE*$$] = wq$$;
98 }
99}
100
101static void raid6_altivec$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
102{
103 preempt_disable();
104 enable_kernel_altivec();
105
106 raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs);
107
108 preempt_enable();
109}
110
111int raid6_have_altivec(void);
112#if $# == 1
113int raid6_have_altivec(void)
114{
115 /* This assumes either all CPUs have Altivec or none does */
H. Peter Anvind7e70ba2005-09-16 19:27:29 -0700116# ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return cpu_has_feature(CPU_FTR_ALTIVEC);
H. Peter Anvind7e70ba2005-09-16 19:27:29 -0700118# else
119 return 1;
120# endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122#endif
123
124const struct raid6_calls raid6_altivec$# = {
125 raid6_altivec$#_gen_syndrome,
126 raid6_have_altivec,
127 "altivecx$#",
128 0
129};
130
131#endif /* CONFIG_ALTIVEC */