altivec.uc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <altivec.h>
  25. #ifdef __KERNEL__
  26. # include <asm/cputable.h>
  27. # include <asm/switch_to.h>
  28. /*
  29. * This is the C data type to use. We use a vector of
  30. * signed char so vec_cmpgt() will generate the right
  31. * instruction.
  32. */
  33. typedef vector signed char unative_t;
  34. #define NBYTES(x) ((vector signed char) {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x})
  35. #define NSIZE sizeof(unative_t)
  36. /*
  37. * The SHLBYTE() operation shifts each byte left by 1, *not*
  38. * rolling over into the next byte
  39. */
  40. static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
  41. {
  42. return vec_add(v,v);
  43. }
  44. /*
  45. * The MASK() operation returns 0xFF in any byte for which the high
  46. * bit is 1, 0x00 for any byte for which the high bit is 0.
  47. */
  48. static inline __attribute_const__ unative_t MASK(unative_t v)
  49. {
  50. unative_t zv = NBYTES(0);
  51. /* vec_cmpgt returns a vector bool char; thus the need for the cast */
  52. return (unative_t)vec_cmpgt(zv, v);
  53. }
  54. /* This is noinline to make damned sure that gcc doesn't move any of the
  55. Altivec code around the enable/disable code */
  56. static void noinline
  57. raid6_altivec$#_gen_syndrome_real(int disks, size_t bytes, void **ptrs)
  58. {
  59. u8 **dptr = (u8 **)ptrs;
  60. u8 *p, *q;
  61. int d, z, z0;
  62. unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
  63. unative_t x1d = NBYTES(0x1d);
  64. z0 = disks - 3; /* Highest data disk */
  65. p = dptr[z0+1]; /* XOR parity */
  66. q = dptr[z0+2]; /* RS syndrome */
  67. for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
  68. wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
  69. for ( z = z0-1 ; z >= 0 ; z-- ) {
  70. wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
  71. wp$$ = vec_xor(wp$$, wd$$);
  72. w2$$ = MASK(wq$$);
  73. w1$$ = SHLBYTE(wq$$);
  74. w2$$ = vec_and(w2$$, x1d);
  75. w1$$ = vec_xor(w1$$, w2$$);
  76. wq$$ = vec_xor(w1$$, wd$$);
  77. }
  78. *(unative_t *)&p[d+NSIZE*$$] = wp$$;
  79. *(unative_t *)&q[d+NSIZE*$$] = wq$$;
  80. }
  81. }
  82. static void raid6_altivec$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
  83. {
  84. preempt_disable();
  85. enable_kernel_altivec();
  86. raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs);
  87. disable_kernel_altivec();
  88. preempt_enable();
  89. }
  90. int raid6_have_altivec(void);
  91. #if $# == 1
  92. int raid6_have_altivec(void)
  93. {
  94. /* This assumes either all CPUs have Altivec or none does */
  95. # ifdef __KERNEL__
  96. return cpu_has_feature(CPU_FTR_ALTIVEC);
  97. # else
  98. return 1;
  99. # endif
  100. }
  101. #endif
  102. const struct raid6_calls raid6_altivec$# = {
  103. raid6_altivec$#_gen_syndrome,
  104. NULL, /* XOR not yet implemented */
  105. raid6_have_altivec,
  106. "altivecx$#",
  107. 0
  108. };
  109. #endif /* CONFIG_ALTIVEC */