18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2017, Matt Brown, IBM Corp.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
58c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License
68c2ecf20Sopenharmony_ci * as published by the Free Software Foundation; either version
78c2ecf20Sopenharmony_ci * 2 of the License, or (at your option) any later version.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * vpermxor$#.c
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Based on H. Peter Anvin's paper - The mathematics of RAID-6
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * $#-way unrolled portable integer math RAID-6 instruction set
148c2ecf20Sopenharmony_ci * This file is postprocessed using unroll.awk
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * vpermxor$#.c makes use of the vpermxor instruction to optimise the RAID6 Q
178c2ecf20Sopenharmony_ci * syndrome calculations.
188c2ecf20Sopenharmony_ci * This can be run on systems which have both Altivec and vpermxor instruction.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * This instruction was introduced in POWER8 - ISA v2.07.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/raid/pq.h>
248c2ecf20Sopenharmony_ci#ifdef CONFIG_ALTIVEC
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <altivec.h>
278c2ecf20Sopenharmony_ci#ifdef __KERNEL__
288c2ecf20Sopenharmony_ci#include <asm/cputable.h>
298c2ecf20Sopenharmony_ci#include <asm/ppc-opcode.h>
308c2ecf20Sopenharmony_ci#include <asm/switch_to.h>
318c2ecf20Sopenharmony_ci#endif
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_citypedef vector unsigned char unative_t;
348c2ecf20Sopenharmony_ci#define NSIZE sizeof(unative_t)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic const vector unsigned char gf_low = {0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14,
378c2ecf20Sopenharmony_ci					    0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08,
388c2ecf20Sopenharmony_ci					    0x06, 0x04, 0x02,0x00};
398c2ecf20Sopenharmony_cistatic const vector unsigned char gf_high = {0xfd, 0xdd, 0xbd, 0x9d, 0x7d, 0x5d,
408c2ecf20Sopenharmony_ci					     0x3d, 0x1d, 0xe0, 0xc0, 0xa0, 0x80,
418c2ecf20Sopenharmony_ci					     0x60, 0x40, 0x20, 0x00};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void noinline raid6_vpermxor$#_gen_syndrome_real(int disks, size_t bytes,
448c2ecf20Sopenharmony_ci							void **ptrs)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	u8 **dptr = (u8 **)ptrs;
478c2ecf20Sopenharmony_ci	u8 *p, *q;
488c2ecf20Sopenharmony_ci	int d, z, z0;
498c2ecf20Sopenharmony_ci	unative_t wp$$, wq$$, wd$$;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	z0 = disks - 3;		/* Highest data disk */
528c2ecf20Sopenharmony_ci	p = dptr[z0+1];		/* XOR parity */
538c2ecf20Sopenharmony_ci	q = dptr[z0+2];		/* RS syndrome */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	for (d = 0; d < bytes; d += NSIZE*$#) {
568c2ecf20Sopenharmony_ci		wp$$ = wq$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		for (z = z0-1; z>=0; z--) {
598c2ecf20Sopenharmony_ci			wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
608c2ecf20Sopenharmony_ci			/* P syndrome */
618c2ecf20Sopenharmony_ci			wp$$ = vec_xor(wp$$, wd$$);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci			/* Q syndrome */
648c2ecf20Sopenharmony_ci			asm(VPERMXOR(%0,%1,%2,%3):"=v"(wq$$):"v"(gf_high), "v"(gf_low), "v"(wq$$));
658c2ecf20Sopenharmony_ci			wq$$ = vec_xor(wq$$, wd$$);
668c2ecf20Sopenharmony_ci		}
678c2ecf20Sopenharmony_ci		*(unative_t *)&p[d+NSIZE*$$] = wp$$;
688c2ecf20Sopenharmony_ci		*(unative_t *)&q[d+NSIZE*$$] = wq$$;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void raid6_vpermxor$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	preempt_disable();
758c2ecf20Sopenharmony_ci	enable_kernel_altivec();
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	raid6_vpermxor$#_gen_syndrome_real(disks, bytes, ptrs);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	disable_kernel_altivec();
808c2ecf20Sopenharmony_ci	preempt_enable();
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint raid6_have_altivec_vpermxor(void);
848c2ecf20Sopenharmony_ci#if $# == 1
858c2ecf20Sopenharmony_ciint raid6_have_altivec_vpermxor(void)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	/* Check if arch has both altivec and the vpermxor instructions */
888c2ecf20Sopenharmony_ci# ifdef __KERNEL__
898c2ecf20Sopenharmony_ci	return (cpu_has_feature(CPU_FTR_ALTIVEC_COMP) &&
908c2ecf20Sopenharmony_ci		cpu_has_feature(CPU_FTR_ARCH_207S));
918c2ecf20Sopenharmony_ci# else
928c2ecf20Sopenharmony_ci	return 1;
938c2ecf20Sopenharmony_ci#endif
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciconst struct raid6_calls raid6_vpermxor$# = {
998c2ecf20Sopenharmony_ci	raid6_vpermxor$#_gen_syndrome,
1008c2ecf20Sopenharmony_ci	NULL,
1018c2ecf20Sopenharmony_ci	raid6_have_altivec_vpermxor,
1028c2ecf20Sopenharmony_ci	"vpermxor$#",
1038c2ecf20Sopenharmony_ci	0
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci#endif
106