1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
13 #include <rte_string_fns.h>
14 #include <rte_cycles.h>
17 #include <rte_spinlock.h>
18 #include "../dlb2_main.h"
20 #include "dlb2_resource.h"
22 #include "../../dlb2_log.h"
23 #include "../../dlb2_user.h"
26 #define DLB2_PCI_REG_READ(addr) rte_read32((void *)addr)
27 #define DLB2_PCI_REG_WRITE(reg, value) rte_write32(value, (void *)reg)
29 /* Read/write register 'reg' in the CSR BAR space */
30 #define DLB2_CSR_REG_ADDR(a, reg) ((void *)((uintptr_t)(a)->csr_kva + (reg)))
31 #define DLB2_CSR_RD(hw, reg) \
32 DLB2_PCI_REG_READ(DLB2_CSR_REG_ADDR((hw), (reg)))
33 #define DLB2_CSR_WR(hw, reg, value) \
34 DLB2_PCI_REG_WRITE(DLB2_CSR_REG_ADDR((hw), (reg)), (value))
36 /* Read/write register 'reg' in the func BAR space */
37 #define DLB2_FUNC_REG_ADDR(a, reg) ((void *)((uintptr_t)(a)->func_kva + (reg)))
38 #define DLB2_FUNC_RD(hw, reg) \
39 DLB2_PCI_REG_READ(DLB2_FUNC_REG_ADDR((hw), (reg)))
40 #define DLB2_FUNC_WR(hw, reg, value) \
41 DLB2_PCI_REG_WRITE(DLB2_FUNC_REG_ADDR((hw), (reg)), (value))
43 /* Map to PMDs logging interface */
44 #define DLB2_ERR(dev, fmt, args...) \
45 DLB2_LOG_ERR(fmt, ## args)
47 #define DLB2_INFO(dev, fmt, args...) \
48 DLB2_LOG_INFO(fmt, ## args)
50 #define DLB2_DEBUG(dev, fmt, args...) \
51 DLB2_LOG_DBG(fmt, ## args)
54 * os_udelay() - busy-wait for a number of microseconds
55 * @usecs: delay duration.
57 static inline void os_udelay(int usecs)
63 * os_msleep() - sleep for a number of milliseconds
64 * @usecs: delay duration.
66 static inline void os_msleep(int msecs)
71 #define DLB2_PP_BASE(__is_ldb) \
72 ((__is_ldb) ? DLB2_LDB_PP_BASE : DLB2_DIR_PP_BASE)
75 * os_map_producer_port() - map a producer port into the caller's address space
76 * @hw: dlb2_hw handle for a particular device.
78 * @is_ldb: true for load-balanced port, false for a directed port
80 * This function maps the requested producer port memory into the caller's
84 * Returns the base address at which the PP memory was mapped, else NULL.
86 static inline void *os_map_producer_port(struct dlb2_hw *hw,
93 pp_dma_base = (uintptr_t)hw->func_kva + DLB2_PP_BASE(is_ldb);
94 addr = (pp_dma_base + (rte_mem_page_size() * port_id));
96 return (void *)(uintptr_t)addr;
100 * os_unmap_producer_port() - unmap a producer port
101 * @addr: mapped producer port address
103 * This function undoes os_map_producer_port() by unmapping the producer port
104 * memory from the caller's address space.
107 * Returns the base address at which the PP memory was mapped, else NULL.
109 static inline void os_unmap_producer_port(struct dlb2_hw *hw, void *addr)
116 * os_fence_hcw() - fence an HCW to ensure it arrives at the device
117 * @hw: dlb2_hw handle for a particular device.
118 * @pp_addr: producer port address
120 static inline void os_fence_hcw(struct dlb2_hw *hw, u64 *pp_addr)
124 /* To ensure outstanding HCWs reach the device, read the PP address. IA
125 * memory ordering prevents reads from passing older writes, and the
126 * mfence also ensures this.
130 *(volatile u64 *)pp_addr;
134 * DLB2_HW_ERR() - log an error message
135 * @dlb2: dlb2_hw handle for a particular device.
136 * @...: variable string args.
138 #define DLB2_HW_ERR(dlb2, ...) do { \
139 RTE_SET_USED(dlb2); \
140 DLB2_ERR(dlb2, __VA_ARGS__); \
144 * DLB2_HW_DBG() - log an info message
145 * @dlb2: dlb2_hw handle for a particular device.
146 * @...: variable string args.
148 #define DLB2_HW_DBG(dlb2, ...) do { \
149 RTE_SET_USED(dlb2); \
150 DLB2_DEBUG(dlb2, __VA_ARGS__); \
153 /* The callback runs until it completes all outstanding QID->CQ
154 * map and unmap requests. To prevent deadlock, this function gives other
155 * threads a chance to grab the resource mutex and configure hardware.
157 static void *dlb2_complete_queue_map_unmap(void *__args)
159 struct dlb2_dev *dlb2_dev = (struct dlb2_dev *)__args;
163 rte_spinlock_lock(&dlb2_dev->resource_mutex);
165 ret = dlb2_finish_unmap_qid_procedures(&dlb2_dev->hw);
166 ret += dlb2_finish_map_qid_procedures(&dlb2_dev->hw);
169 rte_spinlock_unlock(&dlb2_dev->resource_mutex);
170 /* Relinquish the CPU so the application can process
171 * its CQs, so this function doesn't deadlock.
179 dlb2_dev->worker_launched = false;
181 rte_spinlock_unlock(&dlb2_dev->resource_mutex);
188 * os_schedule_work() - launch a thread to process pending map and unmap work
189 * @hw: dlb2_hw handle for a particular device.
191 * This function launches a kernel thread that will run until all pending
192 * map and unmap procedures are complete.
194 static inline void os_schedule_work(struct dlb2_hw *hw)
196 struct dlb2_dev *dlb2_dev;
197 pthread_t complete_queue_map_unmap_thread;
200 dlb2_dev = container_of(hw, struct dlb2_dev, hw);
202 ret = rte_ctrl_thread_create(&complete_queue_map_unmap_thread,
203 "dlb_queue_unmap_waiter",
205 dlb2_complete_queue_map_unmap,
209 "Could not create queue complete map/unmap thread, err=%d\n",
212 dlb2_dev->worker_launched = true;
216 * os_worker_active() - query whether the map/unmap worker thread is active
217 * @hw: dlb2_hw handle for a particular device.
219 * This function returns a boolean indicating whether a thread (launched by
220 * os_schedule_work()) is active. This function is used to determine
221 * whether or not to launch a worker thread.
223 static inline bool os_worker_active(struct dlb2_hw *hw)
225 struct dlb2_dev *dlb2_dev;
227 dlb2_dev = container_of(hw, struct dlb2_dev, hw);
229 return dlb2_dev->worker_launched;
232 #endif /* __DLB2_OSDEP_H */