vpermxor.uc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2017, Matt Brown, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * vpermxor$#.c
  10. *
  11. * Based on H. Peter Anvin's paper - The mathematics of RAID-6
  12. *
  13. * $#-way unrolled portable integer math RAID-6 instruction set
  14. * This file is postprocessed using unroll.awk
  15. *
  16. * vpermxor$#.c makes use of the vpermxor instruction to optimise the RAID6 Q
  17. * syndrome calculations.
  18. * This can be run on systems which have both Altivec and vpermxor instruction.
  19. *
  20. * This instruction was introduced in POWER8 - ISA v2.07.
  21. */
  22. #include <linux/raid/pq.h>
  23. #ifdef CONFIG_ALTIVEC
  24. #include <altivec.h>
  25. #ifdef __KERNEL__
  26. #include <asm/cputable.h>
  27. #include <asm/ppc-opcode.h>
  28. #include <asm/switch_to.h>
  29. #endif
  30. typedef vector unsigned char unative_t;
  31. #define NSIZE sizeof(unative_t)
  32. static const vector unsigned char gf_low = {0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14,
  33. 0x12, 0x10, 0x0e, 0x0c, 0x0a, 0x08,
  34. 0x06, 0x04, 0x02,0x00};
  35. static const vector unsigned char gf_high = {0xfd, 0xdd, 0xbd, 0x9d, 0x7d, 0x5d,
  36. 0x3d, 0x1d, 0xe0, 0xc0, 0xa0, 0x80,
  37. 0x60, 0x40, 0x20, 0x00};
  38. static void noinline raid6_vpermxor$#_gen_syndrome_real(int disks, size_t bytes,
  39. void **ptrs)
  40. {
  41. u8 **dptr = (u8 **)ptrs;
  42. u8 *p, *q;
  43. int d, z, z0;
  44. unative_t wp$$, wq$$, wd$$;
  45. z0 = disks - 3; /* Highest data disk */
  46. p = dptr[z0+1]; /* XOR parity */
  47. q = dptr[z0+2]; /* RS syndrome */
  48. for (d = 0; d < bytes; d += NSIZE*$#) {
  49. wp$$ = wq$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
  50. for (z = z0-1; z>=0; z--) {
  51. wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
  52. /* P syndrome */
  53. wp$$ = vec_xor(wp$$, wd$$);
  54. /* Q syndrome */
  55. asm(VPERMXOR(%0,%1,%2,%3):"=v"(wq$$):"v"(gf_high), "v"(gf_low), "v"(wq$$));
  56. wq$$ = vec_xor(wq$$, wd$$);
  57. }
  58. *(unative_t *)&p[d+NSIZE*$$] = wp$$;
  59. *(unative_t *)&q[d+NSIZE*$$] = wq$$;
  60. }
  61. }
  62. static void raid6_vpermxor$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
  63. {
  64. preempt_disable();
  65. enable_kernel_altivec();
  66. raid6_vpermxor$#_gen_syndrome_real(disks, bytes, ptrs);
  67. disable_kernel_altivec();
  68. preempt_enable();
  69. }
  70. int raid6_have_altivec_vpermxor(void);
  71. #if $# == 1
  72. int raid6_have_altivec_vpermxor(void)
  73. {
  74. /* Check if arch has both altivec and the vpermxor instructions */
  75. # ifdef __KERNEL__
  76. return (cpu_has_feature(CPU_FTR_ALTIVEC_COMP) &&
  77. cpu_has_feature(CPU_FTR_ARCH_207S));
  78. # else
  79. return 1;
  80. #endif
  81. }
  82. #endif
  83. const struct raid6_calls raid6_vpermxor$# = {
  84. raid6_vpermxor$#_gen_syndrome,
  85. NULL,
  86. raid6_have_altivec_vpermxor,
  87. "vpermxor$#",
  88. 0
  89. };
  90. #endif