altivec.uc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* -*- 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,
  8. * Boston MA 02111-1307, USA; either version 2 of the License, or
  9. * (at your option) any later version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * raid6altivec$#.c
  14. *
  15. * $#-way unrolled portable integer math RAID-6 instruction set
  16. *
  17. * This file is postprocessed using unroll.awk
  18. *
  19. * <benh> hpa: in process,
  20. * you can just "steal" the vec unit with enable_kernel_altivec() (but
  21. * bracked this with preempt_disable/enable or in a lock)
  22. */
  23. #include <linux/raid/pq.h>
  24. #ifdef CONFIG_ALTIVEC
  25. #include <altivec.h>
  26. #ifdef __KERNEL__
  27. # include <asm/cputable.h>
  28. # include <asm/switch_to.h>
  29. #endif /* __KERNEL__ */
  30. /*
  31. * This is the C data type to use. We use a vector of
  32. * signed char so vec_cmpgt() will generate the right
  33. * instruction.
  34. */
  35. typedef vector signed char unative_t;
  36. #define NBYTES(x) ((vector signed char) {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x})
  37. #define NSIZE sizeof(unative_t)
  38. /*
  39. * The SHLBYTE() operation shifts each byte left by 1, *not*
  40. * rolling over into the next byte
  41. */
  42. static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
  43. {
  44. return vec_add(v,v);
  45. }
  46. /*
  47. * The MASK() operation returns 0xFF in any byte for which the high
  48. * bit is 1, 0x00 for any byte for which the high bit is 0.
  49. */
  50. static inline __attribute_const__ unative_t MASK(unative_t v)
  51. {
  52. unative_t zv = NBYTES(0);
  53. /* vec_cmpgt returns a vector bool char; thus the need for the cast */
  54. return (unative_t)vec_cmpgt(zv, v);
  55. }
  56. /* This is noinline to make damned sure that gcc doesn't move any of the
  57. Altivec code around the enable/disable code */
  58. static void noinline
  59. raid6_altivec$#_gen_syndrome_real(int disks, size_t bytes, void **ptrs)
  60. {
  61. u8 **dptr = (u8 **)ptrs;
  62. u8 *p, *q;
  63. int d, z, z0;
  64. unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
  65. unative_t x1d = NBYTES(0x1d);
  66. z0 = disks - 3; /* Highest data disk */
  67. p = dptr[z0+1]; /* XOR parity */
  68. q = dptr[z0+2]; /* RS syndrome */
  69. for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
  70. wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
  71. for ( z = z0-1 ; z >= 0 ; z-- ) {
  72. wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
  73. wp$$ = vec_xor(wp$$, wd$$);
  74. w2$$ = MASK(wq$$);
  75. w1$$ = SHLBYTE(wq$$);
  76. w2$$ = vec_and(w2$$, x1d);
  77. w1$$ = vec_xor(w1$$, w2$$);
  78. wq$$ = vec_xor(w1$$, wd$$);
  79. }
  80. *(unative_t *)&p[d+NSIZE*$$] = wp$$;
  81. *(unative_t *)&q[d+NSIZE*$$] = wq$$;
  82. }
  83. }
  84. static void raid6_altivec$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
  85. {
  86. preempt_disable();
  87. enable_kernel_altivec();
  88. raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs);
  89. disable_kernel_altivec();
  90. preempt_enable();
  91. }
  92. int raid6_have_altivec(void);
  93. #if $# == 1
  94. int raid6_have_altivec(void)
  95. {
  96. /* This assumes either all CPUs have Altivec or none does */
  97. # ifdef __KERNEL__
  98. return cpu_has_feature(CPU_FTR_ALTIVEC);
  99. # else
  100. return 1;
  101. # endif
  102. }
  103. #endif
  104. const struct raid6_calls raid6_altivec$# = {
  105. raid6_altivec$#_gen_syndrome,
  106. NULL, /* XOR not yet implemented */
  107. raid6_have_altivec,
  108. "altivecx$#",
  109. 0
  110. };
  111. #endif /* CONFIG_ALTIVEC */