0bf040d6395fc143fbae590fda5787038fa54ed2
[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 #ifndef __INTEL_NET_BASE_OSDEP__
25 #define __INTEL_NET_BASE_OSDEP__
26
27 #define INLINE inline
28 #define STATIC static
29
30 typedef uint8_t         u8;
31 typedef int8_t          s8;
32 typedef uint16_t        u16;
33 typedef int16_t         s16;
34 typedef uint32_t        u32;
35 typedef int32_t         s32;
36 typedef uint64_t        u64;
37 typedef uint64_t        s64;
38
39 #ifndef __le16
40 #define __le16          uint16_t
41 #endif
42 #ifndef __le32
43 #define __le32          uint32_t
44 #endif
45 #ifndef __le64
46 #define __le64          uint64_t
47 #endif
48 #ifndef __be16
49 #define __be16          uint16_t
50 #endif
51 #ifndef __be32
52 #define __be32          uint32_t
53 #endif
54 #ifndef __be64
55 #define __be64          uint64_t
56 #endif
57
58 #define min(a, b) RTE_MIN(a, b)
59 #define max(a, b) RTE_MAX(a, b)
60
61 #define FIELD_SIZEOF(t, f) RTE_SIZEOF_FIELD(t, f)
62 #define ARRAY_SIZE(arr) RTE_DIM(arr)
63
64 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
65 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
66 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
67 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
68 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
69 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
70
71 #define CPU_TO_BE16(o) rte_cpu_to_be_16(o)
72 #define CPU_TO_BE32(o) rte_cpu_to_be_32(o)
73 #define CPU_TO_BE64(o) rte_cpu_to_be_64(o)
74
75 #define NTOHS(a) rte_be_to_cpu_16(a)
76 #define NTOHL(a) rte_be_to_cpu_32(a)
77 #define HTONS(a) rte_cpu_to_be_16(a)
78 #define HTONL(a) rte_cpu_to_be_32(a)
79
80 static __rte_always_inline uint32_t
81 readl(volatile void *addr)
82 {
83         return rte_le_to_cpu_32(rte_read32(addr));
84 }
85
86 static __rte_always_inline void
87 writel(uint32_t value, volatile void *addr)
88 {
89         rte_write32(rte_cpu_to_le_32(value), addr);
90 }
91
92 static __rte_always_inline void
93 writel_relaxed(uint32_t value, volatile void *addr)
94 {
95         rte_write32_relaxed(rte_cpu_to_le_32(value), addr);
96 }
97
98 static __rte_always_inline uint64_t
99 readq(volatile void *addr)
100 {
101         return rte_le_to_cpu_64(rte_read64(addr));
102 }
103
104 static __rte_always_inline void
105 writeq(uint64_t value, volatile void *addr)
106 {
107         rte_write64(rte_cpu_to_le_64(value), addr);
108 }
109
110 #define wr32(a, reg, value) writel((value), (a)->hw_addr + (reg))
111 #define rd32(a, reg)        readl((a)->hw_addr + (reg))
112 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
113 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
114
115 #endif /* __INTEL_NET_BASE_OSDEP__ */
116
117 #define iavf_memset(a, b, c, d) memset((a), (b), (c))
118 #define iavf_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
119
120 #define iavf_usec_delay(x) rte_delay_us_sleep(x)
121 #define iavf_msec_delay(x) iavf_usec_delay(1000 * (x))
122
123 #define IAVF_PCI_REG_WRITE(reg, value)         writel(value, reg)
124 #define IAVF_PCI_REG_WRITE_RELAXED(reg, value) writel_relaxed(value, reg)
125
126 #define IAVF_READ_REG(hw, reg)                 rd32(hw, reg)
127 #define IAVF_WRITE_REG(hw, reg, value)         wr32(hw, reg, value)
128
129 #define IAVF_WRITE_FLUSH(a) IAVF_READ_REG(a, IAVF_VFGEN_RSTAT)
130
131 extern int iavf_common_logger;
132
133 #define DEBUGOUT(S)          rte_log(RTE_LOG_DEBUG, iavf_common_logger, S)
134 #define DEBUGOUT2(S, A...)   rte_log(RTE_LOG_DEBUG, iavf_common_logger, S, ##A)
135 #define DEBUGFUNC(F)         DEBUGOUT(F "\n")
136
137 #define iavf_debug(h, m, s, ...)                                \
138 do {                                                            \
139         if (((m) & (h)->debug_mask))                            \
140                 rte_log(RTE_LOG_DEBUG, iavf_common_logger,      \
141                         "iavf %02x.%x " s,                      \
142                         (h)->bus.device, (h)->bus.func,         \
143                                         ##__VA_ARGS__);         \
144 } while (0)
145
146 /* memory allocation tracking */
147 struct iavf_dma_mem {
148         void *va;
149         u64 pa;
150         u32 size;
151         const void *zone;
152 } __attribute__((packed));
153
154 struct iavf_virt_mem {
155         void *va;
156         u32 size;
157 } __attribute__((packed));
158
159 /* SW spinlock */
160 struct iavf_spinlock {
161         rte_spinlock_t spinlock;
162 };
163
164 #define iavf_allocate_dma_mem(h, m, unused, s, a) \
165                         iavf_allocate_dma_mem_d(h, m, s, a)
166 #define iavf_free_dma_mem(h, m) iavf_free_dma_mem_d(h, m)
167
168 #define iavf_allocate_virt_mem(h, m, s) iavf_allocate_virt_mem_d(h, m, s)
169 #define iavf_free_virt_mem(h, m) iavf_free_virt_mem_d(h, m)
170
171 static inline void
172 iavf_init_spinlock_d(struct iavf_spinlock *sp)
173 {
174         rte_spinlock_init(&sp->spinlock);
175 }
176
177 static inline void
178 iavf_acquire_spinlock_d(struct iavf_spinlock *sp)
179 {
180         rte_spinlock_lock(&sp->spinlock);
181 }
182
183 static inline void
184 iavf_release_spinlock_d(struct iavf_spinlock *sp)
185 {
186         rte_spinlock_unlock(&sp->spinlock);
187 }
188
189 static inline void
190 iavf_destroy_spinlock_d(__rte_unused struct iavf_spinlock *sp)
191 {
192 }
193
194 #define iavf_init_spinlock(_sp) iavf_init_spinlock_d(_sp)
195 #define iavf_acquire_spinlock(_sp) iavf_acquire_spinlock_d(_sp)
196 #define iavf_release_spinlock(_sp) iavf_release_spinlock_d(_sp)
197 #define iavf_destroy_spinlock(_sp) iavf_destroy_spinlock_d(_sp)
198
199 #endif /* _IAVF_OSDEP_H_ */