net/iavf/base: move to drivers common directory
[dpdk.git] / drivers / common / iavf / iavf_osdep.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef _IAVF_OSDEP_H_
6 #define _IAVF_OSDEP_H_
7
8 #include <string.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include <rte_common.h>
15 #include <rte_memcpy.h>
16 #include <rte_memzone.h>
17 #include <rte_malloc.h>
18 #include <rte_byteorder.h>
19 #include <rte_cycles.h>
20 #include <rte_spinlock.h>
21 #include <rte_log.h>
22 #include <rte_io.h>
23
24 #define INLINE inline
25 #define STATIC static
26
27 typedef uint8_t         u8;
28 typedef int8_t          s8;
29 typedef uint16_t        u16;
30 typedef uint32_t        u32;
31 typedef int32_t         s32;
32 typedef uint64_t        u64;
33
34 #define __iomem
35 #define hw_dbg(hw, S, A...) do {} while (0)
36 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
37 #define lower_32_bits(n) ((u32)(n))
38
39 #ifndef ETH_ADDR_LEN
40 #define ETH_ADDR_LEN                  6
41 #endif
42
43 #ifndef __le16
44 #define __le16          uint16_t
45 #endif
46 #ifndef __le32
47 #define __le32          uint32_t
48 #endif
49 #ifndef __le64
50 #define __le64          uint64_t
51 #endif
52 #ifndef __be16
53 #define __be16          uint16_t
54 #endif
55 #ifndef __be32
56 #define __be32          uint32_t
57 #endif
58 #ifndef __be64
59 #define __be64          uint64_t
60 #endif
61
62 #define FALSE           0
63 #define TRUE            1
64 #define false           0
65 #define true            1
66
67 #define min(a,b) RTE_MIN(a,b)
68 #define max(a,b) RTE_MAX(a,b)
69
70 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
71 #define ASSERT(x) if(!(x)) rte_panic("IAVF: x")
72
73 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
74 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
75 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
76 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
77 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
78 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
79
80 #define cpu_to_le16(o) rte_cpu_to_le_16(o)
81 #define cpu_to_le32(s) rte_cpu_to_le_32(s)
82 #define cpu_to_le64(h) rte_cpu_to_le_64(h)
83 #define le16_to_cpu(a) rte_le_to_cpu_16(a)
84 #define le32_to_cpu(c) rte_le_to_cpu_32(c)
85 #define le64_to_cpu(k) rte_le_to_cpu_64(k)
86
87 #define iavf_memset(a, b, c, d) memset((a), (b), (c))
88 #define iavf_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
89
90 #define iavf_usec_delay(x) rte_delay_us_sleep(x)
91 #define iavf_msec_delay(x) iavf_usec_delay(1000 * (x))
92
93 #define IAVF_PCI_REG(reg)               rte_read32(reg)
94 #define IAVF_PCI_REG_ADDR(a, reg) \
95         ((volatile uint32_t *)((char *)(a)->hw_addr + (reg)))
96
97 #define IAVF_PCI_REG_WRITE(reg, value)          \
98         rte_write32((rte_cpu_to_le_32(value)), reg)
99 #define IAVF_PCI_REG_WRITE_RELAXED(reg, value)  \
100         rte_write32_relaxed((rte_cpu_to_le_32(value)), reg)
101 static inline
102 uint32_t iavf_read_addr(volatile void *addr)
103 {
104         return rte_le_to_cpu_32(IAVF_PCI_REG(addr));
105 }
106
107 #define IAVF_READ_REG(hw, reg) \
108         iavf_read_addr(IAVF_PCI_REG_ADDR((hw), (reg)))
109 #define IAVF_WRITE_REG(hw, reg, value) \
110         IAVF_PCI_REG_WRITE(IAVF_PCI_REG_ADDR((hw), (reg)), (value))
111 #define IAVF_WRITE_FLUSH(a) \
112         IAVF_READ_REG(a, IAVF_VFGEN_RSTAT)
113
114 #define rd32(a, reg) iavf_read_addr(IAVF_PCI_REG_ADDR((a), (reg)))
115 #define wr32(a, reg, value) \
116         IAVF_PCI_REG_WRITE(IAVF_PCI_REG_ADDR((a), (reg)), (value))
117
118 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
119
120 extern int iavf_common_logger;
121
122 #define DEBUGOUT(S)          rte_log(RTE_LOG_DEBUG, iavf_common_logger, S)
123 #define DEBUGOUT2(S, A...)   rte_log(RTE_LOG_DEBUG, iavf_common_logger, S, ##A)
124 #define DEBUGFUNC(F)         DEBUGOUT(F "\n")
125
126 #define iavf_debug(h, m, s, ...)                                \
127 do {                                                            \
128         if (((m) & (h)->debug_mask))                            \
129                 rte_log(RTE_LOG_DEBUG, iavf_common_logger,      \
130                         "iavf %02x.%x " s,                      \
131                         (h)->bus.device, (h)->bus.func,         \
132                                         ##__VA_ARGS__);         \
133 } while (0)
134
135 /* memory allocation tracking */
136 struct iavf_dma_mem {
137         void *va;
138         u64 pa;
139         u32 size;
140         const void *zone;
141 } __attribute__((packed));
142
143 struct iavf_virt_mem {
144         void *va;
145         u32 size;
146 } __attribute__((packed));
147
148 /* SW spinlock */
149 struct iavf_spinlock {
150         rte_spinlock_t spinlock;
151 };
152
153 #define iavf_allocate_dma_mem(h, m, unused, s, a) \
154                         iavf_allocate_dma_mem_d(h, m, s, a)
155 #define iavf_free_dma_mem(h, m) iavf_free_dma_mem_d(h, m)
156
157 #define iavf_allocate_virt_mem(h, m, s) iavf_allocate_virt_mem_d(h, m, s)
158 #define iavf_free_virt_mem(h, m) iavf_free_virt_mem_d(h, m)
159
160 static inline void
161 iavf_init_spinlock_d(struct iavf_spinlock *sp)
162 {
163         rte_spinlock_init(&sp->spinlock);
164 }
165
166 static inline void
167 iavf_acquire_spinlock_d(struct iavf_spinlock *sp)
168 {
169         rte_spinlock_lock(&sp->spinlock);
170 }
171
172 static inline void
173 iavf_release_spinlock_d(struct iavf_spinlock *sp)
174 {
175         rte_spinlock_unlock(&sp->spinlock);
176 }
177
178 static inline void
179 iavf_destroy_spinlock_d(__rte_unused struct iavf_spinlock *sp)
180 {
181 }
182
183 #define iavf_init_spinlock(_sp) iavf_init_spinlock_d(_sp)
184 #define iavf_acquire_spinlock(_sp) iavf_acquire_spinlock_d(_sp)
185 #define iavf_release_spinlock(_sp) iavf_release_spinlock_d(_sp)
186 #define iavf_destroy_spinlock(_sp) iavf_destroy_spinlock_d(_sp)
187
188 #endif /* _IAVF_OSDEP_H_ */