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"
19 #include "dlb2_resource.h"
20 #include "../../dlb2_log.h"
21 #include "../../dlb2_user.h"
24 #define DLB2_PCI_REG_READ(addr) rte_read32((void *)addr)
25 #define DLB2_PCI_REG_WRITE(reg, value) rte_write32(value, (void *)reg)
27 /* Read/write register 'reg' in the CSR BAR space */
28 #define DLB2_CSR_REG_ADDR(a, reg) ((void *)((uintptr_t)(a)->csr_kva + (reg)))
29 #define DLB2_CSR_RD(hw, reg) \
30 DLB2_PCI_REG_READ(DLB2_CSR_REG_ADDR((hw), (reg)))
31 #define DLB2_CSR_WR(hw, reg, value) \
32 DLB2_PCI_REG_WRITE(DLB2_CSR_REG_ADDR((hw), (reg)), (value))
34 /* Read/write register 'reg' in the func BAR space */
35 #define DLB2_FUNC_REG_ADDR(a, reg) ((void *)((uintptr_t)(a)->func_kva + (reg)))
36 #define DLB2_FUNC_RD(hw, reg) \
37 DLB2_PCI_REG_READ(DLB2_FUNC_REG_ADDR((hw), (reg)))
38 #define DLB2_FUNC_WR(hw, reg, value) \
39 DLB2_PCI_REG_WRITE(DLB2_FUNC_REG_ADDR((hw), (reg)), (value))
41 /* Map to PMDs logging interface */
42 #define DLB2_ERR(dev, fmt, args...) \
43 DLB2_LOG_ERR(fmt, ## args)
45 #define DLB2_INFO(dev, fmt, args...) \
46 DLB2_LOG_INFO(fmt, ## args)
48 #define DLB2_DEBUG(dev, fmt, args...) \
49 DLB2_LOG_DBG(fmt, ## args)
52 * os_udelay() - busy-wait for a number of microseconds
53 * @usecs: delay duration.
55 static inline void os_udelay(int usecs)
61 * os_msleep() - sleep for a number of milliseconds
62 * @usecs: delay duration.
64 static inline void os_msleep(int msecs)
69 #define DLB2_PP_BASE(__is_ldb) \
70 ((__is_ldb) ? DLB2_LDB_PP_BASE : DLB2_DIR_PP_BASE)
73 * os_map_producer_port() - map a producer port into the caller's address space
74 * @hw: dlb2_hw handle for a particular device.
76 * @is_ldb: true for load-balanced port, false for a directed port
78 * This function maps the requested producer port memory into the caller's
82 * Returns the base address at which the PP memory was mapped, else NULL.
84 static inline void *os_map_producer_port(struct dlb2_hw *hw,
91 pp_dma_base = (uintptr_t)hw->func_kva + DLB2_PP_BASE(is_ldb);
92 addr = (pp_dma_base + (PAGE_SIZE * port_id));
94 return (void *)(uintptr_t)addr;
98 * os_unmap_producer_port() - unmap a producer port
99 * @addr: mapped producer port address
101 * This function undoes os_map_producer_port() by unmapping the producer port
102 * memory from the caller's address space.
105 * Returns the base address at which the PP memory was mapped, else NULL.
107 static inline void os_unmap_producer_port(struct dlb2_hw *hw, void *addr)
114 * os_fence_hcw() - fence an HCW to ensure it arrives at the device
115 * @hw: dlb2_hw handle for a particular device.
116 * @pp_addr: producer port address
118 static inline void os_fence_hcw(struct dlb2_hw *hw, u64 *pp_addr)
122 /* To ensure outstanding HCWs reach the device, read the PP address. IA
123 * memory ordering prevents reads from passing older writes, and the
124 * mfence also ensures this.
128 *(volatile u64 *)pp_addr;
132 * DLB2_HW_ERR() - log an error message
133 * @dlb2: dlb2_hw handle for a particular device.
134 * @...: variable string args.
136 #define DLB2_HW_ERR(dlb2, ...) do { \
137 RTE_SET_USED(dlb2); \
138 DLB2_ERR(dlb2, __VA_ARGS__); \
142 * DLB2_HW_DBG() - log an info message
143 * @dlb2: dlb2_hw handle for a particular device.
144 * @...: variable string args.
146 #define DLB2_HW_DBG(dlb2, ...) do { \
147 RTE_SET_USED(dlb2); \
148 DLB2_DEBUG(dlb2, __VA_ARGS__); \
151 /* The callback runs until it completes all outstanding QID->CQ
152 * map and unmap requests. To prevent deadlock, this function gives other
153 * threads a chance to grab the resource mutex and configure hardware.
155 static void *dlb2_complete_queue_map_unmap(void *__args)
157 struct dlb2_dev *dlb2_dev = (struct dlb2_dev *)__args;
161 rte_spinlock_lock(&dlb2_dev->resource_mutex);
163 ret = dlb2_finish_unmap_qid_procedures(&dlb2_dev->hw);
164 ret += dlb2_finish_map_qid_procedures(&dlb2_dev->hw);
167 rte_spinlock_unlock(&dlb2_dev->resource_mutex);
168 /* Relinquish the CPU so the application can process
169 * its CQs, so this function doesn't deadlock.
177 dlb2_dev->worker_launched = false;
179 rte_spinlock_unlock(&dlb2_dev->resource_mutex);
186 * os_schedule_work() - launch a thread to process pending map and unmap work
187 * @hw: dlb2_hw handle for a particular device.
189 * This function launches a kernel thread that will run until all pending
190 * map and unmap procedures are complete.
192 static inline void os_schedule_work(struct dlb2_hw *hw)
194 struct dlb2_dev *dlb2_dev;
195 pthread_t complete_queue_map_unmap_thread;
198 dlb2_dev = container_of(hw, struct dlb2_dev, hw);
200 ret = rte_ctrl_thread_create(&complete_queue_map_unmap_thread,
201 "dlb_queue_unmap_waiter",
203 dlb2_complete_queue_map_unmap,
207 "Could not create queue complete map/unmap thread, err=%d\n",
210 dlb2_dev->worker_launched = true;
214 * os_worker_active() - query whether the map/unmap worker thread is active
215 * @hw: dlb2_hw handle for a particular device.
217 * This function returns a boolean indicating whether a thread (launched by
218 * os_schedule_work()) is active. This function is used to determine
219 * whether or not to launch a worker thread.
221 static inline bool os_worker_active(struct dlb2_hw *hw)
223 struct dlb2_dev *dlb2_dev;
225 dlb2_dev = container_of(hw, struct dlb2_dev, hw);
227 return dlb2_dev->worker_launched;
230 #endif /* __DLB2_OSDEP_H */