XenevaOS
Loading...
Searching...
No Matches
dma-mapping.h
Go to the documentation of this file.
1#ifndef __LINUX_DMA_MAPPING_H
2#define __LINUX_DMA_MAPPING_H
3
4/*
5 * DCL <linux/dma-mapping.h> -- the DMA-buffer fence markers, and nothing else.
6 *
7 * mainline's header is 27 KB: the dma_alloc_coherent family, the dmam_alloc
8 * family, dma_map_sg, dma_sync_single_*, the attrs and direction enums, the
9 * DMA_MASK checks. None of it has a caller in this tree, and all of it would
10 * need a DMA layer behind it -- which Xeneva does not have. What a ported
11 * source reaches for today is a pair of macros that fence a buffer inside a
12 * struct, so that is what this is. If a driver needs real allocation, that is
13 * the moment to decide whether the linear map is the answer and to write the
14 * rest of the header then.
15 *
16 * The two macros are mainline's, including the quirk that matters:
17 *
18 * #define __dma_from_device_group_begin(GROUP) \
19 * __cacheline_group_begin(GROUP) ____dma_from_device_aligned
20 *
21 * NO trailing semicolon (the caller's `;` terminates the member), and GROUP
22 * may be empty -- virtio-rng.c:34 and :38 write `__dma_from_device_group_begin();`
23 * with nothing inside the parens. See the note in <linux/cache.h>.
24 *
25 * ARCH_HAS_DMA_MINALIGN is not defined for this target, so
26 * ____dma_from_device_aligned is the empty spelling mainline falls back to
27 * too -- no extra alignment. That is not an oversight: virtio-rng.c guards
28 * its buffer with `#if SMP_CACHE_BYTES < 32` and sizes it itself, so the
29 * cacheline (64) already governs the layout. ARCH_DMA_MINALIGN itself is
30 * defined now, in <asm/cache.h>, for drm_managed.c's struct drmres -- but it
31 * is ARCH_HAS_DMA_MINALIGN that the #ifdef below tests, and that one stays
32 * undefined, so the layout here does not move.
33 */
34
35#include <linux/cache.h> /* __cacheline_group_begin/end, SMP_CACHE_BYTES */
36#include <linux/kernel.h> /* gfp_t, dma_addr_t, size_t */
37/* mainline's first include: enum dma_data_direction, which this header's
38 * dma_sync_* prototypes and everything downstream (dma-buf.h) name. */
39#include <linux/dma-direction.h>
40#include <linux/scatterlist.h> /* struct scatterlist, struct sg_table -- dma_map_sg* */
41
42struct device;
43
44/*
45 * DMA_BIT_MASK(n) -- mainline defines it here, and vkms_drv.c:188
46 * (vkms_create) is this tree's only use: dma_coerce_mask_and_coherent(dev,
47 * DMA_BIT_MASK(64)). Without the macro the argument compiles as an implicit
48 * call to a function named DMA_BIT_MASK returning int -- no compile error,
49 * because -Wno-implicit-function-declaration is on -- and the mask would be
50 * a 32-bit value from a function that does not exist. The #ifndef is
51 * belt-and-braces for a second definition arriving with some other port; the
52 * spelling is mainline's.
53 */
54#ifndef DMA_BIT_MASK
55#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL << (n)) - 1))
56#endif
57
58/*
59 * dma_alloc_coherent() / dma_free_coherent() -- the one pair the fence macros
60 * above were standing in front of.
61 *
62 * mainline's header declares a whole family (the dmam_ allocators, dma_map_sg,
63 * the direction enum, the mask checks) and this comment used to say none of it
64 * had a caller. virtio_console.c:439 is a caller:
65 *
66 * buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma, GFP_KERNEL);
67 *
68 * -- the rproc-serial arm of alloc_buf(), the arm DCL folds away (see
69 * CONFIG_REMOTEPROC in <linux/autoconf.h>). It has to *compile*, and it has to
70 * link if the compiler ever declines to fold it, so the two declarations are
71 * here and the bodies are in DCL/linux_mm_shim.c.
72 *
73 * What they do: dma_alloc_coherent() is a plain kmalloc with the address
74 * handed back as the dma_addr_t as well. That is sound rather than convenient
75 * -- Xeneva's virtio path takes a *virtual* address and translates it at kick
76 * time (see <linux/scatterlist.h> on why buf is a linear-map pointer), so the
77 * only thing this tree ever does with buf->dma is hand it straight back to
78 * dma_free_coherent() at :373, and a token that round-trips is a token that
79 * works. There is no streaming mapping here to need a bus address, and no
80 * IOMMU to need an address the CPU cannot reach.
81 *
82 * The parameter types are mainline's, so a caller written against mainline
83 * compiles without a cast: `struct device *` (incomplete is fine -- nothing
84 * here dereferences it), size_t, `dma_addr_t *`, gfp_t.
85 */
86void* dma_alloc_coherent(struct device* dev, size_t size,
87 dma_addr_t* dma_handle, gfp_t gfp);
88void dma_free_coherent(struct device* dev, size_t size, void* vaddr,
89 dma_addr_t handle);
90
91#ifdef ARCH_HAS_DMA_MINALIGN
92#define ____dma_from_device_aligned __aligned(ARCH_DMA_MINALIGN)
93#else
94#define ____dma_from_device_aligned
95#endif
96
97/* Mark the start of a DMA buffer. */
98#define __dma_from_device_group_begin(GROUP) \
99 __cacheline_group_begin(GROUP) ____dma_from_device_aligned
100
101/* Mark the end of a DMA buffer. */
102#define __dma_from_device_group_end(GROUP) \
103 __cacheline_group_end(GROUP) ____dma_from_device_aligned
104
105/*
106 * DMA_ATTR_* -- the streaming-mapping attribute bits, of which
107 * DMA_ATTR_SKIP_CPU_SYNC is the only one this tree uses (drm_prime.c:684 and
108 * :710, in the prime export path). Defined as mainline's values rather than
109 * renumbered, so a later port that carries two of them does not have to
110 * reconcile two schemes; the rest are carried for the same reason, and none
111 * of them is read anywhere.
112 *
113 * mainline: include/linux/dma-mapping.h.
114 */
115#define DMA_ATTR_WRITE_BARRIER (1UL << 0)
116#define DMA_ATTR_WEAK_ORDERING (1UL << 1)
117#define DMA_ATTR_WRITE_COMBINE (1UL << 2)
118#define DMA_ATTR_NON_CONSISTENT (1UL << 3)
119#define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4)
120#define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5)
121#define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6)
122#define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7)
123#define DMA_ATTR_NO_WARN (1UL << 8)
124#define DMA_ATTR_MERGEABLE (1UL << 9)
125#define DMA_ATTR_CPU_MAPPABLE (1UL << 10)
126#define DMA_ATTR_FORCE_DMA32 (1UL << 11)
127#define DMA_ATTR_ALLOC_PAGES (1UL << 12)
128
129/*
130 * dma_map_sg() -- map `nents` sg entries for the device and report how many
131 * DMA segments they became.
132 *
133 * The answer is `nents` unchanged, which is the identity mapping: with no
134 * IOMMU and no bounce buffers, an entry is already contiguous and already
135 * addressable, so nothing coalesces. That is the same reasoning
136 * dma_alloc_coherent() above uses for handing the virtual address back as the
137 * dma_addr_t -- and drm_need_swiotlb() in drm_cache.c answering false is the
138 * other half of it. The @attrs word is mainline's and, at this mapping
139 * granularity, has nothing to act on: DMA_ATTR_SKIP_CPU_SYNC in particular
140 * only defers a sync that never happens here.
141 *
142 * Declared here, defined in DCL/linux_mm_shim.c beside the two allocators.
143 */
144int dma_map_sg(struct device* dev, struct scatterlist* sg, int nents,
145 enum dma_data_direction dir);
146void dma_unmap_sg(struct device* dev, struct scatterlist* sg, int nents,
147 enum dma_data_direction dir);
148
149/*
150 * dma_map_sgtable() -- mainline's wrapper, and mainline's body verbatim: map
151 * the list, and on success rewrite nents to the count that survived. The
152 * -EINVAL arm is mainline's for the "mapped to nothing" case, which cannot
153 * happen against the dma_map_sg() above but is kept so a real mapper could
154 * be dropped in without the contract changing.
155 *
156 * Inline rather than a shim because the whole of it is the wrapper -- there is
157 * no state of its own, and mainline keeps it in the header too.
158 */
159static inline int dma_map_sgtable(struct device* dev, struct sg_table* sgt,
160 enum dma_data_direction dir,
161 unsigned long attrs)
162{
163 int nents;
164
165 (void)attrs; /* see the note above: nothing here reads it */
166
167 nents = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
168 if (nents <= 0)
169 return nents < 0 ? nents : -EINVAL;
170
171 sgt->nents = (unsigned int)nents;
172 return 0;
173}
174
175/*
176 * dma_unmap_sgtable() -- mainline's wrapper, and mainline's body verbatim
177 * (include/linux/dma-mapping.h): unmap the table's *original* entries.
178 * drm_prime.c:710 (drm_gem_unmap_dma_buf) is the only caller. Inline for the
179 * same reason dma_map_sgtable() above is -- the whole of it is the call --
180 * and `attrs` has nothing left to act on because dma_unmap_sg() below
181 * performs no unmapping (see the note over dma_map_sg).
182 *
183 * orig_nents rather than nents is mainline's choice and matters to the
184 * contract even here: dma_map_sgtable() rewrites nents to the post-map
185 * count, while unmapping must cover what was handed in.
186 */
187static inline void dma_unmap_sgtable(struct device* dev, struct sg_table* sgt,
188 enum dma_data_direction dir,
189 unsigned long attrs)
190{
191 (void)attrs;
192 dma_unmap_sg(dev, sgt->sgl, (int)sgt->orig_nents, dir);
193}
194
195/*
196 * dma_coerce_mask_and_coherent() / dma_max_mapping_size() -- the two
197 * remaining DMA operations a vendored caller names as symbols. Both are
198 * mainline declarations in this header; the callers reach it directly
199 * (vkms_drv.c:187, drm_prime.c:862), and both bodies are in
200 * DCL/linux_mm_shim.c beside dma_map_sg() above. The reasons they cannot be
201 * inlines are there: one would dereference dev->dma_mask fields that fs.h's
202 * pinned struct device does not name, the other has a value to answer with.
203 */
206
207/*
208 * dma_set_max_seg_size() -- mainline's cap on one DMA segment, read by
209 * virtio_gpu_probe() (Vendored/drivers/gpu/drm/virtio/virtgpu_drv.c:102:
210 * `dma_set_max_seg_size(dev->dev, dma_max_mapping_size(dev->dev) ?: UINT_MAX)`).
211 * There is no DMA layer here for a segment limit to bind -- dma_map_sg()
212 * above is the identity and the virtio queues carry linear-map buffers --
213 * and the caller does not read the result, so the whole of the contract that
214 * matters is "accept the call". 0 is success, mainline's own answer for a
215 * limit it stored. Inline because there is no state to keep.
216 */
217static inline int dma_set_max_seg_size(struct device* dev, unsigned int size)
218{
219 (void)dev;
220 (void)size;
221 return 0;
222}
223
224/*
225 * dma_map_resource()/dma_unmap_resource()/dma_mapping_error() -- the
226 * resource mapping trio, added with the virtio-gpu port. One caller:
227 * virtgpu_vram.c maps a host-visible blob's physical range before hanging
228 * it on an sg element and checks the result with dma_mapping_error(). That
229 * path is unreachable on a device with no host-visible region, and the
230 * bodies are the identity this port's DMA story is throughout (dma_map_sg
231 * above; the queues carry linear-map buffers): the address maps to itself,
232 * the unmap has nothing to tear down, and 0 answers "not an error".
233 *
234 * u64 for mainline's phys_addr_t: the name is not spelled anywhere in this
235 * tree and u64 is what it aliases on arm64.
236 */
237static inline dma_addr_t dma_map_resource(struct device* dev, u64 phys,
238 size_t size,
239 enum dma_data_direction dir,
240 unsigned long attrs)
241{
242 (void)dev;
243 (void)size;
244 (void)dir;
245 (void)attrs;
246 return (dma_addr_t)phys;
247}
248
249static inline void dma_unmap_resource(struct device* dev, dma_addr_t addr,
250 size_t size,
251 enum dma_data_direction dir,
252 unsigned long attrs)
253{
254 (void)dev;
255 (void)addr;
256 (void)size;
257 (void)dir;
258 (void)attrs;
259}
260
261static inline bool dma_mapping_error(struct device* dev, dma_addr_t addr)
262{
263 (void)dev;
264 (void)addr;
265 return false;
266}
267
268/*
269 * dma_sync_sgtable_for_device() -- mainline's sync-a-whole-table entry
270 * point, added with the virtio-gpu port. DCL has no caches to sync (the
271 * queues carry linear-map buffers and the device side is QEMU's
272 * emulation), and every caller reaches it behind the driver's use_dma_api
273 * test, which is false without VIRTIO_F_ACCESS_PLATFORM -- the only kind
274 * of virtio-gpu device this tree scans. A no-op rather than a forward to a
275 * dma_sync_sg_for_device() that does not exist here.
276 */
277static inline void dma_sync_sgtable_for_device(struct device* dev,
278 struct sg_table* sgt,
279 enum dma_data_direction dir)
280{
281 (void)dev;
282 (void)sgt;
283 (void)dir;
284}
285
286#endif /* __LINUX_DMA_MAPPING_H */
#define EINVAL
Definition acclib.h:198
dma_data_direction
Definition dma-direction.h:21
int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
void * dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir)
void dma_free_coherent(struct device *dev, size_t size, void *vaddr, dma_addr_t handle)
size_t dma_max_mapping_size(struct device *dev)
void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir)
size_t size
Definition drm_bridge.h:1299
const struct device * dev
Definition drm_print.h:519
uint64_t u64
Definition kernel.h:17
uintptr_t dma_addr_t
Definition kernel.h:42
unsigned int gfp_t
Definition kernel.h:56
char phys[]
Definition fs.h:255
Definition scatterlist.h:73
Definition scatterlist.h:135
struct scatterlist * sgl
Definition scatterlist.h:136
unsigned int orig_nents
Definition scatterlist.h:138
unsigned int nents
Definition scatterlist.h:137