LCOV - code coverage report
Current view: top level - drivers/common/dpaax - compat.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 12 0.0 %
Date: 2025-03-01 20:23:48 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 78 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
       2                 :            :  *
       3                 :            :  * Copyright 2011 Freescale Semiconductor, Inc.
       4                 :            :  * All rights reserved.
       5                 :            :  * Copyright 2019-2020 NXP
       6                 :            :  *
       7                 :            :  */
       8                 :            : 
       9                 :            : #ifndef __COMPAT_H
      10                 :            : #define __COMPAT_H
      11                 :            : 
      12                 :            : #include <sched.h>
      13                 :            : #include <stdint.h>
      14                 :            : #include <stdlib.h>
      15                 :            : #include <stddef.h>
      16                 :            : #include <stdio.h>
      17                 :            : #include <errno.h>
      18                 :            : #include <string.h>
      19                 :            : #include <pthread.h>
      20                 :            : #include <linux/types.h>
      21                 :            : #include <stdbool.h>
      22                 :            : #include <ctype.h>
      23                 :            : #include <malloc.h>
      24                 :            : #include <sys/types.h>
      25                 :            : #include <sys/stat.h>
      26                 :            : #include <fcntl.h>
      27                 :            : #include <unistd.h>
      28                 :            : #include <sys/mman.h>
      29                 :            : #include <limits.h>
      30                 :            : #include <assert.h>
      31                 :            : #include <dirent.h>
      32                 :            : #include <inttypes.h>
      33                 :            : 
      34                 :            : #include <rte_byteorder.h>
      35                 :            : #include <rte_atomic.h>
      36                 :            : #include <rte_spinlock.h>
      37                 :            : #include <rte_common.h>
      38                 :            : #include <rte_debug.h>
      39                 :            : #include <rte_cycles.h>
      40                 :            : #include <rte_malloc.h>
      41                 :            : #include <rte_prefetch.h>
      42                 :            : 
      43                 :            : /* The following definitions are primarily to allow the single-source driver
      44                 :            :  * interfaces to be included by arbitrary program code. Ie. for interfaces that
      45                 :            :  * are also available in kernel-space, these definitions provide compatibility
      46                 :            :  * with certain attributes and types used in those interfaces.
      47                 :            :  */
      48                 :            : 
      49                 :            : /* Required compiler attributes */
      50                 :            : #ifndef __maybe_unused
      51                 :            : #define __maybe_unused  __rte_unused
      52                 :            : #endif
      53                 :            : #ifndef __always_unused
      54                 :            : #define __always_unused __rte_unused
      55                 :            : #endif
      56                 :            : #ifndef noinline
      57                 :            : #define noinline        __rte_noinline
      58                 :            : #endif
      59                 :            : #define L1_CACHE_BYTES 64
      60                 :            : #define ____cacheline_aligned __rte_aligned(L1_CACHE_BYTES)
      61                 :            : #define __stringify_1(x) #x
      62                 :            : #define __stringify(x)  __stringify_1(x)
      63                 :            : 
      64                 :            : #ifdef ARRAY_SIZE
      65                 :            : #undef ARRAY_SIZE
      66                 :            : #endif
      67                 :            : #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
      68                 :            : 
      69                 :            : /* Debugging */
      70                 :            : #define prflush(fmt, ...) \
      71                 :            :         do { \
      72                 :            :                 printf(fmt, ##__VA_ARGS__); \
      73                 :            :                 fflush(stdout); \
      74                 :            :         } while (0)
      75                 :            : #ifndef pr_crit
      76                 :            : #define pr_crit(fmt, ...)        prflush("CRIT:" fmt, ##__VA_ARGS__)
      77                 :            : #endif
      78                 :            : #ifndef pr_err
      79                 :            : #define pr_err(fmt, ...)         prflush("ERR:" fmt, ##__VA_ARGS__)
      80                 :            : #endif
      81                 :            : #ifndef pr_warn
      82                 :            : #define pr_warn(fmt, ...)        prflush("WARN:" fmt, ##__VA_ARGS__)
      83                 :            : #endif
      84                 :            : #ifndef pr_info
      85                 :            : #define pr_info(fmt, ...)        prflush(fmt, ##__VA_ARGS__)
      86                 :            : #endif
      87                 :            : #ifndef pr_debug
      88                 :            : #ifdef RTE_LIBRTE_DPAA_DEBUG_BUS
      89                 :            : #define pr_debug(fmt, ...)      printf(fmt, ##__VA_ARGS__)
      90                 :            : #else
      91                 :            : #define pr_debug(fmt, ...) {}
      92                 :            : #endif
      93                 :            : #endif
      94                 :            : 
      95                 :            : #define DPAA_BUG_ON(x) RTE_ASSERT(x)
      96                 :            : 
      97                 :            : /* Required types */
      98                 :            : typedef uint8_t         u8;
      99                 :            : typedef uint16_t        u16;
     100                 :            : typedef uint32_t        u32;
     101                 :            : typedef uint64_t        u64;
     102                 :            : typedef uint64_t        dma_addr_t;
     103                 :            : typedef cpu_set_t       cpumask_t;
     104                 :            : typedef uint32_t        phandle;
     105                 :            : typedef uint32_t        gfp_t;
     106                 :            : typedef uint32_t        irqreturn_t;
     107                 :            : 
     108                 :            : #define ETHER_ADDR_LEN 6
     109                 :            : 
     110                 :            : #define IRQ_HANDLED     0
     111                 :            : #define request_irq     qbman_request_irq
     112                 :            : #define free_irq        qbman_free_irq
     113                 :            : 
     114                 :            : #define __iomem
     115                 :            : #define GFP_KERNEL      0
     116                 :            : #define __raw_readb(p)  (*(const volatile unsigned char *)(p))
     117                 :            : #define __raw_readl(p)  (*(const volatile unsigned int *)(p))
     118                 :            : #define __raw_writel(v, p) {*(volatile unsigned int *)(p) = (v); }
     119                 :            : 
     120                 :            : /* to be used as an upper-limit only */
     121                 :            : #define NR_CPUS                 64
     122                 :            : 
     123                 :            : /* Waitqueue stuff */
     124                 :            : typedef struct { }              wait_queue_head_t;
     125                 :            : #define DECLARE_WAIT_QUEUE_HEAD(x) int dummy_##x __always_unused
     126                 :            : #define wake_up(x)              do { } while (0)
     127                 :            : 
     128                 :            : /* I/O operations */
     129                 :            : static inline u32 in_be32(volatile void *__p)
     130                 :            : {
     131                 :            :         volatile u32 *p = __p;
     132                 :          0 :         return rte_be_to_cpu_32(*p);
     133                 :            : }
     134                 :            : 
     135                 :            : static inline void out_be32(volatile void *__p, u32 val)
     136                 :            : {
     137                 :            :         volatile u32 *p = __p;
     138   [ #  #  #  #  :          0 :         *p = rte_cpu_to_be_32(val);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     139                 :          0 : }
     140                 :            : 
     141                 :            : #define hwsync() rte_rmb()
     142                 :            : #define lwsync() rte_wmb()
     143                 :            : 
     144                 :            : #define dcbt_ro(p) rte_prefetch0(p)
     145                 :            : #define dcbt_rw(p) rte_prefetch0_write(p)
     146                 :            : 
     147                 :            : #if defined(RTE_ARCH_ARM)
     148                 :            : #if defined(RTE_ARCH_64)
     149                 :            : #define dcbz(p) { asm volatile("dc zva, %0" : : "r" (p) : "memory"); }
     150                 :            : #define dcbz_64(p) dcbz(p)
     151                 :            : #define dcbf(p) { asm volatile("dc cvac, %0" : : "r"(p) : "memory"); }
     152                 :            : #define dcbf_64(p) dcbf(p)
     153                 :            : #define dccivac(p) { asm volatile("dc civac, %0" : : "r"(p) : "memory"); }
     154                 :            : 
     155                 :            : #define dcbit_ro(p) \
     156                 :            :         do { \
     157                 :            :                 dccivac(p);                                             \
     158                 :            :                 asm volatile("prfm pldl1keep, [%0, #64]" : : "r" (p));      \
     159                 :            :         } while (0)
     160                 :            : 
     161                 :            : #else /* RTE_ARCH_32 */
     162                 :            : #define dcbz(p) memset((p), 0, 32)
     163                 :            : #define dcbz_64(p) memset((p), 0, 64)
     164                 :            : #define dcbf(p) RTE_SET_USED(p)
     165                 :            : #define dcbf_64(p) dcbf(p)
     166                 :            : #define dccivac(p)      RTE_SET_USED(p)
     167                 :            : #define dcbit_ro(p)     RTE_SET_USED(p)
     168                 :            : #endif
     169                 :            : 
     170                 :            : #else
     171                 :            : #define dcbz(p) RTE_SET_USED(p)
     172                 :            : #define dcbz_64(p) dcbz(p)
     173                 :            : #define dcbf(p) RTE_SET_USED(p)
     174                 :            : #define dcbf_64(p) dcbf(p)
     175                 :            : #define dccivac(p)      RTE_SET_USED(p)
     176                 :            : #define dcbit_ro(p)     RTE_SET_USED(p)
     177                 :            : #endif
     178                 :            : 
     179                 :            : #define barrier() { asm volatile ("" : : : "memory"); }
     180                 :            : #define cpu_relax barrier
     181                 :            : 
     182                 :            : #if defined(RTE_ARCH_ARM64)
     183                 :            : static inline uint64_t mfatb(void)
     184                 :            : {
     185                 :            :         uint64_t ret, ret_new, timeout = 200;
     186                 :            : 
     187                 :            :         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret));
     188                 :            :         asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
     189                 :            :         while (ret != ret_new && timeout--) {
     190                 :            :                 ret = ret_new;
     191                 :            :                 asm volatile ("mrs %0, cntvct_el0" : "=r" (ret_new));
     192                 :            :         }
     193                 :            :         DPAA_BUG_ON(!timeout && (ret != ret_new));
     194                 :            :         return ret * 64;
     195                 :            : }
     196                 :            : #else
     197                 :            : 
     198                 :            : #define mfatb rte_rdtsc
     199                 :            : 
     200                 :            : #endif
     201                 :            : 
     202                 :            : /* Spin for a few cycles without bothering the bus */
     203                 :            : static inline void cpu_spin(int cycles)
     204                 :            : {
     205                 :            :         uint64_t now = mfatb();
     206                 :            : 
     207         [ #  # ]:          0 :         while (mfatb() < (now + cycles))
     208                 :            :                 ;
     209                 :            : }
     210                 :            : 
     211                 :            : /* Qman/Bman API inlines and macros; */
     212                 :            : #ifdef lower_32_bits
     213                 :            : #undef lower_32_bits
     214                 :            : #endif
     215                 :            : #define lower_32_bits(x) ((u32)(x))
     216                 :            : 
     217                 :            : #ifdef upper_32_bits
     218                 :            : #undef upper_32_bits
     219                 :            : #endif
     220                 :            : #define upper_32_bits(x) ((u32)(((x) >> 16) >> 16))
     221                 :            : 
     222                 :            : /*
     223                 :            :  * Swap bytes of a 48-bit value.
     224                 :            :  */
     225                 :            : static inline uint64_t
     226                 :            : __bswap_48(uint64_t x)
     227                 :            : {
     228                 :            :         return  ((x & 0x0000000000ffULL) << 40) |
     229                 :            :                 ((x & 0x00000000ff00ULL) << 24) |
     230                 :            :                 ((x & 0x000000ff0000ULL) <<  8) |
     231                 :            :                 ((x & 0x0000ff000000ULL) >>  8) |
     232                 :            :                 ((x & 0x00ff00000000ULL) >> 24) |
     233                 :            :                 ((x & 0xff0000000000ULL) >> 40);
     234                 :            : }
     235                 :            : 
     236                 :            : /*
     237                 :            :  * Swap bytes of a 40-bit value.
     238                 :            :  */
     239                 :            : static inline uint64_t
     240                 :            : __bswap_40(uint64_t x)
     241                 :            : {
     242                 :          0 :         return  ((x & 0x00000000ffULL) << 32) |
     243                 :          0 :                 ((x & 0x000000ff00ULL) << 16) |
     244                 :          0 :                 ((x & 0x0000ff0000ULL)) |
     245                 :          0 :                 ((x & 0x00ff000000ULL) >> 16) |
     246   [ #  #  #  #  :          0 :                 ((x & 0xff00000000ULL) >> 32);
          #  #  #  #  #  
                      # ]
     247                 :            : }
     248                 :            : 
     249                 :            : /*
     250                 :            :  * Swap bytes of a 24-bit value.
     251                 :            :  */
     252                 :            : static inline uint32_t
     253                 :            : __bswap_24(uint32_t x)
     254                 :            : {
     255                 :          0 :         return  ((x & 0x0000ffULL) << 16) |
     256                 :          0 :                 ((x & 0x00ff00ULL)) |
     257   [ #  #  #  #  :          0 :                 ((x & 0xff0000ULL) >> 16);
                   #  # ]
     258                 :            : }
     259                 :            : 
     260                 :            : #define be64_to_cpu(x) rte_be_to_cpu_64(x)
     261                 :            : #define be32_to_cpu(x) rte_be_to_cpu_32(x)
     262                 :            : #define be16_to_cpu(x) rte_be_to_cpu_16(x)
     263                 :            : 
     264                 :            : #define cpu_to_be64(x) rte_cpu_to_be_64(x)
     265                 :            : #if !defined(cpu_to_be32)
     266                 :            : #define cpu_to_be32(x) rte_cpu_to_be_32(x)
     267                 :            : #endif
     268                 :            : #define cpu_to_be16(x) rte_cpu_to_be_16(x)
     269                 :            : 
     270                 :            : #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
     271                 :            : 
     272                 :            : #define cpu_to_be48(x) __bswap_48(x)
     273                 :            : #define be48_to_cpu(x) __bswap_48(x)
     274                 :            : 
     275                 :            : #define cpu_to_be40(x) __bswap_40(x)
     276                 :            : #define be40_to_cpu(x) __bswap_40(x)
     277                 :            : 
     278                 :            : #define cpu_to_be24(x) __bswap_24(x)
     279                 :            : #define be24_to_cpu(x) __bswap_24(x)
     280                 :            : 
     281                 :            : #else /* RTE_BIG_ENDIAN */
     282                 :            : 
     283                 :            : #define cpu_to_be48(x) (x)
     284                 :            : #define be48_to_cpu(x) (x)
     285                 :            : 
     286                 :            : #define cpu_to_be40(x) (x)
     287                 :            : #define be40_to_cpu(x) (x)
     288                 :            : 
     289                 :            : #define cpu_to_be24(x) (x)
     290                 :            : #define be24_to_cpu(x) (x)
     291                 :            : 
     292                 :            : #endif /* RTE_BIG_ENDIAN */
     293                 :            : 
     294                 :            : /* When copying aligned words or shorts, try to avoid memcpy() */
     295                 :            : /* memcpy() stuff - when you know alignments in advance */
     296                 :            : #define CONFIG_TRY_BETTER_MEMCPY
     297                 :            : 
     298                 :            : #ifdef CONFIG_TRY_BETTER_MEMCPY
     299                 :            : static inline void copy_words(void *dest, const void *src, size_t sz)
     300                 :            : {
     301                 :            :         u32 *__dest = dest;
     302                 :            :         const u32 *__src = src;
     303                 :            :         size_t __sz = sz >> 2;
     304                 :            : 
     305                 :            :         DPAA_BUG_ON((unsigned long)dest & 0x3);
     306                 :            :         DPAA_BUG_ON((unsigned long)src & 0x3);
     307                 :            :         DPAA_BUG_ON(sz & 0x3);
     308                 :            :         while (__sz--)
     309                 :            :                 *(__dest++) = *(__src++);
     310                 :            : }
     311                 :            : 
     312                 :            : static inline void copy_shorts(void *dest, const void *src, size_t sz)
     313                 :            : {
     314                 :            :         u16 *__dest = dest;
     315                 :            :         const u16 *__src = src;
     316                 :            :         size_t __sz = sz >> 1;
     317                 :            : 
     318                 :            :         DPAA_BUG_ON((unsigned long)dest & 0x1);
     319                 :            :         DPAA_BUG_ON((unsigned long)src & 0x1);
     320                 :            :         DPAA_BUG_ON(sz & 0x1);
     321                 :            :         while (__sz--)
     322                 :            :                 *(__dest++) = *(__src++);
     323                 :            : }
     324                 :            : 
     325                 :            : static inline void copy_bytes(void *dest, const void *src, size_t sz)
     326                 :            : {
     327                 :            :         u8 *__dest = dest;
     328                 :            :         const u8 *__src = src;
     329                 :            : 
     330                 :            :         while (sz--)
     331                 :            :                 *(__dest++) = *(__src++);
     332                 :            : }
     333                 :            : #else
     334                 :            : #define copy_words memcpy
     335                 :            : #define copy_shorts memcpy
     336                 :            : #define copy_bytes memcpy
     337                 :            : #endif
     338                 :            : 
     339                 :            : /* Allocator stuff */
     340                 :            : #define kmalloc(sz, t)  rte_malloc(NULL, sz, 0)
     341                 :            : #define kzalloc(sz, t)  rte_zmalloc(NULL, sz, 0)
     342                 :            : #define vmalloc(sz)     rte_malloc(NULL, sz, 0)
     343                 :            : #define kfree(p)        rte_free(p)
     344                 :            : 
     345                 :            : static inline unsigned long get_zeroed_page(gfp_t __foo __rte_unused)
     346                 :            : {
     347                 :            :         void *p;
     348                 :            : 
     349                 :            :         if (posix_memalign(&p, 4096, 4096))
     350                 :            :                 return 0;
     351                 :            :         memset(p, 0, 4096);
     352                 :            :         return (unsigned long)p;
     353                 :            : }
     354                 :            : 
     355                 :            : /* Spinlock stuff */
     356                 :            : #define spinlock_t              rte_spinlock_t
     357                 :            : #define __SPIN_LOCK_UNLOCKED(x) RTE_SPINLOCK_INITIALIZER
     358                 :            : #define DEFINE_SPINLOCK(x)      spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
     359                 :            : #define spin_lock_init(x)       rte_spinlock_init(x)
     360                 :            : #define spin_lock_destroy(x)
     361                 :            : #define spin_lock(x)            rte_spinlock_lock(x)
     362                 :            : #define spin_unlock(x)          rte_spinlock_unlock(x)
     363                 :            : #define spin_lock_irq(x)        spin_lock(x)
     364                 :            : #define spin_unlock_irq(x)      spin_unlock(x)
     365                 :            : #define spin_lock_irqsave(x, f) spin_lock_irq(x)
     366                 :            : #define spin_unlock_irqrestore(x, f) spin_unlock_irq(x)
     367                 :            : 
     368                 :            : #define atomic_t                rte_atomic32_t
     369                 :            : #define atomic_read(v)          rte_atomic32_read(v)
     370                 :            : #define atomic_set(v, i)        rte_atomic32_set(v, i)
     371                 :            : 
     372                 :            : #define atomic_inc(v)           rte_atomic32_add(v, 1)
     373                 :            : #define atomic_dec(v)           rte_atomic32_sub(v, 1)
     374                 :            : 
     375                 :            : #define atomic_inc_and_test(v)  rte_atomic32_inc_and_test(v)
     376                 :            : #define atomic_dec_and_test(v)  rte_atomic32_dec_and_test(v)
     377                 :            : 
     378                 :            : #define atomic_inc_return(v)    rte_atomic32_add_return(v, 1)
     379                 :            : #define atomic_dec_return(v)    rte_atomic32_sub_return(v, 1)
     380                 :            : #define atomic_sub_and_test(i, v) (rte_atomic32_sub_return(v, i) == 0)
     381                 :            : 
     382                 :            : /* Interface name len*/
     383                 :            : #define IF_NAME_MAX_LEN 16
     384                 :            : 
     385                 :            : #endif /* __COMPAT_H */

Generated by: LCOV version 1.14