components/open-fabrics/libsif/include/psifapi/psif_endian.h
changeset 5564 e533d5840fdd
child 7120 b01185225eaa
equal deleted inserted replaced
5563:f50fba81e706 5564:e533d5840fdd
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  */
       
     4 
       
     5 /*
       
     6  * Redistribution and use in source and binary forms, with or without modification,
       
     7  * are permitted provided that the following conditions are met:
       
     8  *
       
     9  * 1. Redistributions of source code must retain the above copyright notice,
       
    10  *    this list of conditions and the following disclaimer.
       
    11  *
       
    12  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    13  *    this list of conditions and the following disclaimer in the documentation
       
    14  *    and/or other materials provided with the distribution.
       
    15  *
       
    16  * 3. Neither the name of the copyright holder nor the names of its contributors
       
    17  *    may be used to endorse or promote products derived from this software without
       
    18  *    specific prior written permission.
       
    19  *
       
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
       
    23  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
       
    24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
       
    25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
       
    28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    29  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    30  */
       
    31 
       
    32 #ifndef	_PSIF_ENDIAN_H
       
    33 #define	_PSIF_ENDIAN_H
       
    34 
       
    35 #if defined(__arm__)
       
    36 #undef HOST_BIG_ENDIAN
       
    37 #define HOST_LITTLE_ENDIAN
       
    38 #else /* __arm__ */
       
    39 #if defined(__KERNEL__)
       
    40 #  if defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)
       
    41 #    define HOST_BIG_ENDIAN
       
    42 #  elif defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN)
       
    43 #    define HOST_LITTLE_ENDIAN
       
    44 #  else
       
    45 #    error "could not determine byte order"
       
    46 #  endif
       
    47 #else /* defined(__KERNEL__) */
       
    48 #  include <assert.h>
       
    49 #  include "os_header.h"
       
    50 #  if defined(__BYTE_ORDER)
       
    51 #    if __BYTE_ORDER == __BIG_ENDIAN
       
    52 #      define HOST_BIG_ENDIAN
       
    53 #    elif __BYTE_ORDER == __LITTLE_ENDIAN
       
    54 #      define HOST_LITTLE_ENDIAN
       
    55 #    else
       
    56 #      error "could not determine byte order"
       
    57 #    endif
       
    58 #  else /* defined(__BYTE_ORDER) */
       
    59 #    error "could not determine byte order"
       
    60 #  endif
       
    61 #endif /* defined(__KERNEL__) */
       
    62 #endif
       
    63 
       
    64 #if !defined (__KERNEL__)
       
    65 
       
    66 #if !defined(__arm__)
       
    67 #include <stdlib.h>
       
    68 #endif /* !__arm__ */
       
    69 
       
    70 #if defined(HOST_BIG_ENDIAN)
       
    71 
       
    72 #define copy_convert(dest,src,n) { memcpy((void *)dest, (void const *)(src), n); wmb(); }
       
    73 #define copy_convert_to_hw(dest,src,n) copy_convert(dest,src,n)
       
    74 #define copy_convert_to_sw(dest,src,n) copy_convert(dest,src,n)
       
    75 
       
    76 #else /* HOST_LITTLE_ENDIAN */
       
    77 
       
    78 #if defined(__arm__)
       
    79 #include <assert.h>
       
    80 #include <stdint.h>
       
    81 #include "epsfw_misc.h"
       
    82 #define htobe64(x) eps_htobe64(x)
       
    83 #define u64 uint64_t
       
    84 
       
    85 static inline void __DSB(void) {
       
    86     /* __dsb() doesn't serve as sequence point in armcc so adding
       
    87      * __schedule_barrier() around it to force the compiler to see it as a
       
    88      * sequence point */
       
    89     __schedule_barrier();
       
    90     __dsb(0xf);
       
    91     __schedule_barrier();
       
    92 }
       
    93 #define wmb() __DSB();
       
    94 
       
    95 /*
       
    96  * Alternatively, use this one as barrier?
       
    97  * #define wmb() __memory_changed();
       
    98  */
       
    99 #endif /* __arm__ */
       
   100 
       
   101 static inline void _copy_convert(void *dest, void const *src, size_t n)
       
   102 {
       
   103 	int           i, words = n / 8;
       
   104 	assert((n % 8) == 0);
       
   105 	volatile u64       *dp = (volatile u64       *) dest;
       
   106 	const volatile u64 *sp = (const volatile u64 *) src;
       
   107 	for (i = 0; i < words; i++) {
       
   108 		*dp++ = htobe64(*sp);
       
   109 		sp++;
       
   110 	}
       
   111 	wmb();
       
   112 }
       
   113 
       
   114 static inline void _copy_convert_to_hw(volatile void *dest, void const *src, size_t n)
       
   115 {
       
   116 	int           i, words = n / 8;
       
   117 	assert((n % 8) == 0);
       
   118 	volatile u64 *dp = (volatile u64 *) dest;
       
   119 	const u64    *sp = (const u64    *) src;
       
   120 	for (i = 0; i < words; i++) {
       
   121 		*dp++ = htobe64(*sp);
       
   122 		sp++;
       
   123 	}
       
   124 	wmb();
       
   125 }
       
   126 
       
   127 static inline void _copy_convert_to_sw(void *dest, volatile void const *src, size_t n)
       
   128 {
       
   129 	int           i, words = n / 8;
       
   130 	assert((n % 8) == 0);
       
   131 	u64                *dp = (u64                *) dest;
       
   132 	const volatile u64 *sp = (const volatile u64 *) src;
       
   133 	for (i = 0; i < words; i++) {
       
   134 		*dp++ = htobe64(*sp);
       
   135 		sp++;
       
   136 	}
       
   137 	wmb();
       
   138 }
       
   139 
       
   140 #define copy_convert(dest,src,n) _copy_convert(dest,src,n)
       
   141 #define copy_convert_to_hw(dest,src,n) _copy_convert_to_hw(dest,src,n)
       
   142 #define copy_convert_to_sw(dest,src,n) _copy_convert_to_sw(dest,src,n)
       
   143 
       
   144 #endif /* HOST_ENDIAN */
       
   145 #endif /* !__KERNEL__ */
       
   146 #endif	/* _PSIF_ENDIAN_H */