Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/compat/linuxkpi/common/include/linux/dma-mapping.h
103096 views
1
/*-
2
* Copyright (c) 2010 Isilon Systems, Inc.
3
* Copyright (c) 2010 iX Systems, Inc.
4
* Copyright (c) 2010 Panasas, Inc.
5
* Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6
* All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
10
* are met:
11
* 1. Redistributions of source code must retain the above copyright
12
* notice unmodified, this list of conditions, and the following
13
* disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
#ifndef _LINUXKPI_LINUX_DMA_MAPPING_H_
30
#define _LINUXKPI_LINUX_DMA_MAPPING_H_
31
32
#include <linux/types.h>
33
#include <linux/device.h>
34
#include <linux/err.h>
35
#include <linux/dma-attrs.h>
36
#include <linux/scatterlist.h>
37
#include <linux/mm.h>
38
#include <linux/page.h>
39
#include <linux/sizes.h>
40
41
#include <sys/systm.h>
42
#include <sys/malloc.h>
43
44
#include <vm/vm.h>
45
#include <vm/vm_page.h>
46
#include <vm/uma_align_mask.h>
47
#include <vm/pmap.h>
48
49
#include <machine/bus.h>
50
51
enum dma_data_direction {
52
DMA_BIDIRECTIONAL = 0,
53
DMA_TO_DEVICE = 1,
54
DMA_FROM_DEVICE = 2,
55
DMA_NONE = 3,
56
};
57
58
struct dma_map_ops {
59
void* (*alloc_coherent)(struct device *dev, size_t size,
60
dma_addr_t *dma_handle, gfp_t gfp);
61
void (*free_coherent)(struct device *dev, size_t size,
62
void *vaddr, dma_addr_t dma_handle);
63
dma_addr_t (*map_page)(struct device *dev, struct page *page,
64
unsigned long offset, size_t size, enum dma_data_direction dir,
65
unsigned long attrs);
66
void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
67
size_t size, enum dma_data_direction dir, unsigned long attrs);
68
int (*map_sg)(struct device *dev, struct scatterlist *sg,
69
int nents, enum dma_data_direction dir, unsigned long attrs);
70
void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
71
enum dma_data_direction dir, unsigned long attrs);
72
void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
73
size_t size, enum dma_data_direction dir);
74
void (*sync_single_for_device)(struct device *dev,
75
dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
76
void (*sync_single_range_for_cpu)(struct device *dev,
77
dma_addr_t dma_handle, unsigned long offset, size_t size,
78
enum dma_data_direction dir);
79
void (*sync_single_range_for_device)(struct device *dev,
80
dma_addr_t dma_handle, unsigned long offset, size_t size,
81
enum dma_data_direction dir);
82
void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
83
int nents, enum dma_data_direction dir);
84
void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
85
int nents, enum dma_data_direction dir);
86
int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
87
int (*dma_supported)(struct device *dev, u64 mask);
88
int is_phys;
89
};
90
91
#define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)
92
93
int linux_dma_tag_init(struct device *, u64);
94
int linux_dma_tag_init_coherent(struct device *, u64);
95
void *linux_dma_alloc_coherent(struct device *dev, size_t size,
96
dma_addr_t *dma_handle, gfp_t flag);
97
void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size,
98
dma_addr_t *dma_handle, gfp_t flag);
99
void linuxkpi_dmam_free_coherent(struct device *dev, size_t size,
100
void *addr, dma_addr_t dma_handle);
101
dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len); /* backward compat */
102
dma_addr_t lkpi_dma_map_phys(struct device *, vm_paddr_t, size_t,
103
enum dma_data_direction, unsigned long);
104
void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size); /* backward compat */
105
void lkpi_dma_unmap(struct device *, dma_addr_t, size_t,
106
enum dma_data_direction, unsigned long);
107
int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
108
int nents, enum dma_data_direction direction,
109
unsigned long attrs __unused);
110
void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
111
int nents __unused, enum dma_data_direction direction,
112
unsigned long attrs __unused);
113
void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);
114
115
static inline int
116
dma_supported(struct device *dev, u64 dma_mask)
117
{
118
119
/* XXX busdma takes care of this elsewhere. */
120
return (1);
121
}
122
123
static inline int
124
dma_set_mask(struct device *dev, u64 dma_mask)
125
{
126
127
if (!dev->dma_priv || !dma_supported(dev, dma_mask))
128
return -EIO;
129
130
return (linux_dma_tag_init(dev, dma_mask));
131
}
132
133
static inline int
134
dma_set_coherent_mask(struct device *dev, u64 dma_mask)
135
{
136
137
if (!dev->dma_priv || !dma_supported(dev, dma_mask))
138
return -EIO;
139
140
return (linux_dma_tag_init_coherent(dev, dma_mask));
141
}
142
143
static inline int
144
dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
145
{
146
int r;
147
148
r = dma_set_mask(dev, dma_mask);
149
if (r == 0)
150
dma_set_coherent_mask(dev, dma_mask);
151
return (r);
152
}
153
154
static inline void *
155
dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
156
gfp_t flag)
157
{
158
return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
159
}
160
161
static inline void *
162
dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
163
gfp_t flag)
164
{
165
166
return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
167
}
168
169
static inline void *
170
dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
171
gfp_t flag)
172
{
173
174
return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag));
175
}
176
177
static inline void
178
dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
179
dma_addr_t dma_addr)
180
{
181
182
lkpi_dma_unmap(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0);
183
kmem_free(cpu_addr, size);
184
}
185
186
static inline void
187
dmam_free_coherent(struct device *dev, size_t size, void *addr,
188
dma_addr_t dma_handle)
189
{
190
linuxkpi_dmam_free_coherent(dev, size, addr, dma_handle);
191
}
192
193
static inline dma_addr_t
194
dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
195
size_t size, enum dma_data_direction direction, unsigned long attrs)
196
{
197
198
return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,
199
direction, attrs));
200
}
201
202
/* linux_dma_(un)map_sg_attrs does not support attrs yet */
203
#define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
204
linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)
205
206
#define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \
207
linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)
208
209
static inline dma_addr_t
210
dma_map_page(struct device *dev, struct page *page,
211
unsigned long offset, size_t size, enum dma_data_direction direction)
212
{
213
214
return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,
215
direction, 0));
216
}
217
218
static inline void
219
dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
220
enum dma_data_direction direction)
221
{
222
223
lkpi_dma_unmap(dev, dma_address, size, direction, 0);
224
}
225
226
static inline dma_addr_t
227
dma_map_resource(struct device *dev, phys_addr_t paddr, size_t size,
228
enum dma_data_direction direction, unsigned long attrs)
229
{
230
return (lkpi_dma_map_phys(dev, paddr, size, direction, attrs));
231
}
232
233
static inline void
234
dma_unmap_resource(struct device *dev, dma_addr_t dma, size_t size,
235
enum dma_data_direction direction, unsigned long attrs)
236
{
237
lkpi_dma_unmap(dev, dma, size, direction, attrs);
238
}
239
240
static inline void
241
dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,
242
enum dma_data_direction direction)
243
{
244
bus_dmasync_op_t op;
245
246
switch (direction) {
247
case DMA_BIDIRECTIONAL:
248
op = BUS_DMASYNC_POSTREAD;
249
linuxkpi_dma_sync(dev, dma, size, op);
250
op = BUS_DMASYNC_PREREAD;
251
break;
252
case DMA_TO_DEVICE:
253
op = BUS_DMASYNC_POSTWRITE;
254
break;
255
case DMA_FROM_DEVICE:
256
op = BUS_DMASYNC_POSTREAD;
257
break;
258
default:
259
return;
260
}
261
262
linuxkpi_dma_sync(dev, dma, size, op);
263
}
264
265
static inline void
266
dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
267
enum dma_data_direction dir)
268
{
269
dma_sync_single_for_cpu(dev, addr, size, dir);
270
}
271
272
static inline void
273
dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
274
size_t size, enum dma_data_direction direction)
275
{
276
bus_dmasync_op_t op;
277
278
switch (direction) {
279
case DMA_BIDIRECTIONAL:
280
op = BUS_DMASYNC_PREWRITE;
281
break;
282
case DMA_TO_DEVICE:
283
op = BUS_DMASYNC_PREREAD;
284
break;
285
case DMA_FROM_DEVICE:
286
op = BUS_DMASYNC_PREWRITE;
287
break;
288
default:
289
return;
290
}
291
292
linuxkpi_dma_sync(dev, dma, size, op);
293
}
294
295
/* (20250329) These four seem to be unused code. */
296
static inline void
297
dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
298
enum dma_data_direction direction)
299
{
300
pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
301
}
302
303
static inline void
304
dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
305
enum dma_data_direction direction)
306
{
307
pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
308
}
309
310
static inline void
311
dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
312
unsigned long offset, size_t size, enum dma_data_direction direction)
313
{
314
pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
315
}
316
317
static inline void
318
dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
319
unsigned long offset, size_t size, enum dma_data_direction direction)
320
{
321
pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
322
}
323
324
#define DMA_MAPPING_ERROR (~(dma_addr_t)0)
325
326
static inline int
327
dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
328
{
329
330
if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)
331
return (-ENOMEM);
332
return (0);
333
}
334
335
static inline unsigned int dma_set_max_seg_size(struct device *dev,
336
unsigned int size)
337
{
338
return (0);
339
}
340
341
static inline dma_addr_t
342
_dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
343
enum dma_data_direction direction, unsigned long attrs)
344
{
345
return (lkpi_dma_map_phys(dev, vtophys(ptr), size,
346
direction, attrs));
347
}
348
349
static inline void
350
_dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
351
enum dma_data_direction direction, unsigned long attrs)
352
{
353
lkpi_dma_unmap(dev, dma, size, direction, attrs);
354
}
355
356
static inline size_t
357
dma_max_mapping_size(struct device *dev)
358
{
359
360
return (SCATTERLIST_MAX_SEGMENT);
361
}
362
363
#define dma_map_single_attrs(dev, ptr, size, dir, attrs) \
364
_dma_map_single_attrs(dev, ptr, size, dir, 0)
365
366
#define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
367
_dma_unmap_single_attrs(dev, dma_addr, size, dir, 0)
368
369
#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
370
#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
371
#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
372
#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
373
374
#define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name
375
#define DEFINE_DMA_UNMAP_LEN(name) __u32 name
376
#define dma_unmap_addr(p, name) ((p)->name)
377
#define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))
378
#define dma_unmap_len(p, name) ((p)->name)
379
#define dma_unmap_len_set(p, name, v) (((p)->name) = (v))
380
381
#define dma_get_cache_alignment() (uma_get_cache_align_mask() + 1)
382
383
384
static inline int
385
dma_map_sgtable(struct device *dev, struct sg_table *sgt,
386
enum dma_data_direction dir,
387
unsigned long attrs)
388
{
389
int nents;
390
391
nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
392
if (nents < 0)
393
return (nents);
394
sgt->nents = nents;
395
return (0);
396
}
397
398
static inline void
399
dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
400
enum dma_data_direction dir,
401
unsigned long attrs)
402
{
403
404
dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
405
}
406
407
408
#endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
409
410