2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2016-2019 Ruslan Bukin <br@bsdpad.com>
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifndef _DEV_XDMA_XDMA_H_
35 #define _DEV_XDMA_XDMA_H_
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/openfirm.h>
55 enum xdma_operation_type {
62 enum xdma_request_type {
75 struct xdma_transfer_status {
80 typedef struct xdma_transfer_status xdma_transfer_status_t;
82 struct xdma_controller {
83 device_t dev; /* DMA consumer device_t. */
84 device_t dma_dev; /* A real DMA device_t. */
85 void *data; /* OFW MD part. */
86 vmem_t *vmem; /* Bounce memory. */
88 /* List of virtual channels allocated. */
89 TAILQ_HEAD(xdma_channel_list, xdma_channel) channels;
92 typedef struct xdma_controller xdma_controller_t;
103 struct xdma_request {
106 enum xdma_operation_type operation;
107 enum xdma_request_type req_type;
108 enum xdma_direction direction;
113 bus_size_t block_num;
114 bus_size_t block_len;
115 xdma_transfer_status_t status;
117 TAILQ_ENTRY(xdma_request) xr_next;
118 struct xchan_buf buf;
127 enum xdma_direction direction;
134 vmem_t *vmem; /* VA space */
135 device_t dev; /* IOMMU device */
138 struct xdma_channel {
139 xdma_controller_t *xdma;
143 #define XCHAN_BUFS_ALLOCATED (1 << 0)
144 #define XCHAN_SGLIST_ALLOCATED (1 << 1)
145 #define XCHAN_CONFIGURED (1 << 2)
146 #define XCHAN_TYPE_CYCLIC (1 << 3)
147 #define XCHAN_TYPE_MEMCPY (1 << 4)
148 #define XCHAN_TYPE_FIFO (1 << 5)
149 #define XCHAN_TYPE_SG (1 << 6)
152 #define XCHAN_CAP_BUSDMA (1 << 0)
153 #define XCHAN_CAP_NOSEG (1 << 1)
154 #define XCHAN_CAP_BOUNCE (1 << 2)
155 #define XCHAN_CAP_IOMMU (1 << 3)
157 /* A real hardware driver channel. */
160 /* Interrupt handlers. */
161 TAILQ_HEAD(, xdma_intr_handler) ie_handlers;
162 TAILQ_ENTRY(xdma_channel) xchan_next;
165 struct mtx mtx_qin_lock;
166 struct mtx mtx_qout_lock;
167 struct mtx mtx_bank_lock;
168 struct mtx mtx_proc_lock;
171 bus_dma_tag_t dma_tag_bufs;
172 struct xdma_request *xr_mem;
175 /* Bus dma tag options. */
176 bus_size_t maxsegsize;
178 bus_size_t alignment;
183 struct xdma_sglist *sg;
185 TAILQ_HEAD(, xdma_request) bank;
186 TAILQ_HEAD(, xdma_request) queue_in;
187 TAILQ_HEAD(, xdma_request) queue_out;
188 TAILQ_HEAD(, xdma_request) processing;
191 struct xdma_iommu xio;
194 typedef struct xdma_channel xdma_channel_t;
196 struct xdma_intr_handler {
197 int (*cb)(void *cb_user, xdma_transfer_status_t *status);
199 #define XDMA_INTR_NET (1 << 0)
201 TAILQ_ENTRY(xdma_intr_handler) ih_next;
204 static MALLOC_DEFINE(M_XDMA, "xdma", "xDMA framework");
206 #define XCHAN_LOCK(xchan) mtx_lock(&(xchan)->mtx_lock)
207 #define XCHAN_UNLOCK(xchan) mtx_unlock(&(xchan)->mtx_lock)
208 #define XCHAN_ASSERT_LOCKED(xchan) \
209 mtx_assert(&(xchan)->mtx_lock, MA_OWNED)
211 #define QUEUE_IN_LOCK(xchan) mtx_lock(&(xchan)->mtx_qin_lock)
212 #define QUEUE_IN_UNLOCK(xchan) mtx_unlock(&(xchan)->mtx_qin_lock)
213 #define QUEUE_IN_ASSERT_LOCKED(xchan) \
214 mtx_assert(&(xchan)->mtx_qin_lock, MA_OWNED)
216 #define QUEUE_OUT_LOCK(xchan) mtx_lock(&(xchan)->mtx_qout_lock)
217 #define QUEUE_OUT_UNLOCK(xchan) mtx_unlock(&(xchan)->mtx_qout_lock)
218 #define QUEUE_OUT_ASSERT_LOCKED(xchan) \
219 mtx_assert(&(xchan)->mtx_qout_lock, MA_OWNED)
221 #define QUEUE_BANK_LOCK(xchan) mtx_lock(&(xchan)->mtx_bank_lock)
222 #define QUEUE_BANK_UNLOCK(xchan) mtx_unlock(&(xchan)->mtx_bank_lock)
223 #define QUEUE_BANK_ASSERT_LOCKED(xchan) \
224 mtx_assert(&(xchan)->mtx_bank_lock, MA_OWNED)
226 #define QUEUE_PROC_LOCK(xchan) mtx_lock(&(xchan)->mtx_proc_lock)
227 #define QUEUE_PROC_UNLOCK(xchan) mtx_unlock(&(xchan)->mtx_proc_lock)
228 #define QUEUE_PROC_ASSERT_LOCKED(xchan) \
229 mtx_assert(&(xchan)->mtx_proc_lock, MA_OWNED)
231 #define XDMA_SGLIST_MAXLEN 2048
232 #define XDMA_MAX_SEG 128
234 /* xDMA controller ops */
235 xdma_controller_t *xdma_ofw_get(device_t dev, const char *prop);
236 xdma_controller_t *xdma_get(device_t dev, device_t dma_dev);
237 int xdma_put(xdma_controller_t *xdma);
238 vmem_t * xdma_get_memory(device_t dev);
239 void xdma_put_memory(vmem_t *vmem);
241 int xdma_handle_mem_node(vmem_t *vmem, phandle_t memory);
244 /* xDMA channel ops */
245 xdma_channel_t * xdma_channel_alloc(xdma_controller_t *, uint32_t caps);
246 int xdma_channel_free(xdma_channel_t *);
247 int xdma_request(xdma_channel_t *xchan, struct xdma_request *r);
248 void xchan_set_memory(xdma_channel_t *xchan, vmem_t *vmem);
251 int xdma_prep_sg(xdma_channel_t *, uint32_t,
252 bus_size_t, bus_size_t, bus_size_t, bus_addr_t, bus_addr_t, bus_addr_t);
253 void xdma_channel_free_sg(xdma_channel_t *xchan);
254 int xdma_queue_submit_sg(xdma_channel_t *xchan);
255 void xchan_seg_done(xdma_channel_t *xchan, xdma_transfer_status_t *);
257 /* Queue operations */
258 int xdma_dequeue_mbuf(xdma_channel_t *xchan, struct mbuf **m,
259 xdma_transfer_status_t *);
260 int xdma_enqueue_mbuf(xdma_channel_t *xchan, struct mbuf **m, uintptr_t addr,
261 uint8_t, uint8_t, enum xdma_direction dir);
262 int xdma_dequeue_bio(xdma_channel_t *xchan, struct bio **bp,
263 xdma_transfer_status_t *status);
264 int xdma_enqueue_bio(xdma_channel_t *xchan, struct bio **bp, bus_addr_t addr,
265 uint8_t, uint8_t, enum xdma_direction dir);
266 int xdma_dequeue(xdma_channel_t *xchan, void **user,
267 xdma_transfer_status_t *status);
268 int xdma_enqueue(xdma_channel_t *xchan, uintptr_t src, uintptr_t dst,
269 uint8_t, uint8_t, bus_size_t, enum xdma_direction dir, void *);
270 int xdma_queue_submit(xdma_channel_t *xchan);
272 /* Mbuf operations */
273 uint32_t xdma_mbuf_defrag(xdma_channel_t *xchan, struct xdma_request *xr);
274 uint32_t xdma_mbuf_chain_count(struct mbuf *m0);
276 /* Channel Control */
277 int xdma_control(xdma_channel_t *xchan, enum xdma_command cmd);
279 /* Interrupt callback */
280 int xdma_setup_intr(xdma_channel_t *xchan, int flags, int (*cb)(void *,
281 xdma_transfer_status_t *), void *arg, void **);
282 int xdma_teardown_intr(xdma_channel_t *xchan, struct xdma_intr_handler *ih);
283 int xdma_teardown_all_intr(xdma_channel_t *xchan);
284 void xdma_callback(struct xdma_channel *xchan, xdma_transfer_status_t *status);
287 int xchan_sglist_alloc(xdma_channel_t *xchan);
288 void xchan_sglist_free(xdma_channel_t *xchan);
289 int xdma_sglist_add(struct xdma_sglist *sg, struct bus_dma_segment *seg,
290 uint32_t nsegs, struct xdma_request *xr);
293 void xchan_bank_init(xdma_channel_t *xchan);
294 int xchan_bank_free(xdma_channel_t *xchan);
295 struct xdma_request * xchan_bank_get(xdma_channel_t *xchan);
296 int xchan_bank_put(xdma_channel_t *xchan, struct xdma_request *xr);
299 void xdma_iommu_add_entry(xdma_channel_t *xchan, vm_offset_t *va,
300 vm_paddr_t pa, vm_size_t size, vm_prot_t prot);
301 void xdma_iommu_remove_entry(xdma_channel_t *xchan, vm_offset_t va);
302 int xdma_iommu_init(struct xdma_iommu *xio);
303 int xdma_iommu_release(struct xdma_iommu *xio);
305 #endif /* !_DEV_XDMA_XDMA_H_ */