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