1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Pointer to dma-buf-mapped memory, plus helpers.
6 #ifndef __DMA_BUF_MAP_H__
7 #define __DMA_BUF_MAP_H__
10 #include <linux/string.h>
15 * Calling dma-buf's vmap operation returns a pointer to the buffer's memory.
16 * Depending on the location of the buffer, users may have to access it with
17 * I/O operations or memory load/store operations. For example, copying to
18 * system memory could be done with memcpy(), copying to I/O memory would be
19 * done with memcpy_toio().
23 * void *vaddr = ...; // pointer to system memory
24 * memcpy(vaddr, src, len);
26 * void *vaddr_iomem = ...; // pointer to I/O memory
27 * memcpy_toio(vaddr, _iomem, src, len);
29 * When using dma-buf's vmap operation, the returned pointer is encoded as
30 * :c:type:`struct dma_buf_map <dma_buf_map>`.
31 * :c:type:`struct dma_buf_map <dma_buf_map>` stores the buffer's address in
32 * system or I/O memory and a flag that signals the required method of
33 * accessing the buffer. Use the returned instance and the helper functions
34 * to access the buffer's memory in the correct way.
36 * The type :c:type:`struct dma_buf_map <dma_buf_map>` and its helpers are
37 * actually independent from the dma-buf infrastructure. When sharing buffers
38 * among devices, drivers have to know the location of the memory to access
39 * the buffers in a safe way. :c:type:`struct dma_buf_map <dma_buf_map>`
40 * solves this problem for dma-buf and its users. If other drivers or
41 * sub-systems require similar functionality, the type could be generalized
42 * and moved to a more prominent header file.
44 * Open-coding access to :c:type:`struct dma_buf_map <dma_buf_map>` is
45 * considered bad style. Rather then accessing its fields directly, use one
46 * of the provided helper functions, or implement your own. For example,
47 * instances of :c:type:`struct dma_buf_map <dma_buf_map>` can be initialized
48 * statically with DMA_BUF_MAP_INIT_VADDR(), or at runtime with
49 * dma_buf_map_set_vaddr(). These helpers will set an address in system memory.
53 * struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(0xdeadbeaf);
55 * dma_buf_map_set_vaddr(&map. 0xdeadbeaf);
57 * To set an address in I/O memory, use dma_buf_map_set_vaddr_iomem().
61 * dma_buf_map_set_vaddr_iomem(&map. 0xdeadbeaf);
63 * Instances of struct dma_buf_map do not have to be cleaned up, but
64 * can be cleared to NULL with dma_buf_map_clear(). Cleared mappings
65 * always refer to system memory.
69 * dma_buf_map_clear(&map);
71 * Test if a mapping is valid with either dma_buf_map_is_set() or
72 * dma_buf_map_is_null().
76 * if (dma_buf_map_is_set(&map) != dma_buf_map_is_null(&map))
79 * Instances of :c:type:`struct dma_buf_map <dma_buf_map>` can be compared
80 * for equality with dma_buf_map_is_equal(). Mappings the point to different
81 * memory spaces, system or I/O, are never equal. That's even true if both
82 * spaces are located in the same address space, both mappings contain the
83 * same address value, or both mappings refer to NULL.
87 * struct dma_buf_map sys_map; // refers to system memory
88 * struct dma_buf_map io_map; // refers to I/O memory
90 * if (dma_buf_map_is_equal(&sys_map, &io_map))
93 * A set up instance of struct dma_buf_map can be used to access or manipulate
94 * the buffer memory. Depending on the location of the memory, the provided
95 * helpers will pick the correct operations. Data can be copied into the memory
96 * with dma_buf_map_memcpy_to(). The address can be manipulated with
101 * const void *src = ...; // source buffer
102 * size_t len = ...; // length of src
104 * dma_buf_map_memcpy_to(&map, src, len);
105 * dma_buf_map_incr(&map, len); // go to first byte after the memcpy
109 * struct dma_buf_map - Pointer to vmap'ed dma-buf memory.
110 * @vaddr_iomem: The buffer's address if in I/O memory
111 * @vaddr: The buffer's address if in system memory
112 * @is_iomem: True if the dma-buf memory is located in I/O
113 * memory, or false otherwise.
117 void __iomem *vaddr_iomem;
124 * DMA_BUF_MAP_INIT_VADDR - Initializes struct dma_buf_map to an address in system memory
125 * @vaddr_: A system-memory address
127 #define DMA_BUF_MAP_INIT_VADDR(vaddr_) \
134 * dma_buf_map_set_vaddr - Sets a dma-buf mapping structure to an address in system memory
135 * @map: The dma-buf mapping structure
136 * @vaddr: A system-memory address
138 * Sets the address and clears the I/O-memory flag.
140 static inline void dma_buf_map_set_vaddr(struct dma_buf_map *map, void *vaddr)
143 map->is_iomem = false;
147 * dma_buf_map_set_vaddr_iomem - Sets a dma-buf mapping structure to an address in I/O memory
148 * @map: The dma-buf mapping structure
149 * @vaddr_iomem: An I/O-memory address
151 * Sets the address and the I/O-memory flag.
153 static inline void dma_buf_map_set_vaddr_iomem(struct dma_buf_map *map,
154 void __iomem *vaddr_iomem)
156 map->vaddr_iomem = vaddr_iomem;
157 map->is_iomem = true;
161 * dma_buf_map_is_equal - Compares two dma-buf mapping structures for equality
162 * @lhs: The dma-buf mapping structure
163 * @rhs: A dma-buf mapping structure to compare with
165 * Two dma-buf mapping structures are equal if they both refer to the same type of memory
166 * and to the same address within that memory.
169 * True is both structures are equal, or false otherwise.
171 static inline bool dma_buf_map_is_equal(const struct dma_buf_map *lhs,
172 const struct dma_buf_map *rhs)
174 if (lhs->is_iomem != rhs->is_iomem)
176 else if (lhs->is_iomem)
177 return lhs->vaddr_iomem == rhs->vaddr_iomem;
179 return lhs->vaddr == rhs->vaddr;
183 * dma_buf_map_is_null - Tests for a dma-buf mapping to be NULL
184 * @map: The dma-buf mapping structure
186 * Depending on the state of struct dma_buf_map.is_iomem, tests if the
190 * True if the mapping is NULL, or false otherwise.
192 static inline bool dma_buf_map_is_null(const struct dma_buf_map *map)
195 return !map->vaddr_iomem;
200 * dma_buf_map_is_set - Tests is the dma-buf mapping has been set
201 * @map: The dma-buf mapping structure
203 * Depending on the state of struct dma_buf_map.is_iomem, tests if the
204 * mapping has been set.
207 * True if the mapping is been set, or false otherwise.
209 static inline bool dma_buf_map_is_set(const struct dma_buf_map *map)
211 return !dma_buf_map_is_null(map);
215 * dma_buf_map_clear - Clears a dma-buf mapping structure
216 * @map: The dma-buf mapping structure
218 * Clears all fields to zero; including struct dma_buf_map.is_iomem. So
219 * mapping structures that were set to point to I/O memory are reset for
220 * system memory. Pointers are cleared to NULL. This is the default.
222 static inline void dma_buf_map_clear(struct dma_buf_map *map)
225 map->vaddr_iomem = NULL;
226 map->is_iomem = false;
233 * dma_buf_map_memcpy_to - Memcpy into dma-buf mapping
234 * @dst: The dma-buf mapping structure
235 * @src: The source buffer
236 * @len: The number of byte in src
238 * Copies data into a dma-buf mapping. The source buffer is in system
239 * memory. Depending on the buffer's location, the helper picks the correct
240 * method of accessing the memory.
242 static inline void dma_buf_map_memcpy_to(struct dma_buf_map *dst, const void *src, size_t len)
245 memcpy_toio(dst->vaddr_iomem, src, len);
247 memcpy(dst->vaddr, src, len);
251 * dma_buf_map_incr - Increments the address stored in a dma-buf mapping
252 * @map: The dma-buf mapping structure
253 * @incr: The number of bytes to increment
255 * Increments the address stored in a dma-buf mapping. Depending on the
256 * buffer's location, the correct value will be updated.
258 static inline void dma_buf_map_incr(struct dma_buf_map *map, size_t incr)
261 map->vaddr_iomem += incr;
266 #endif /* __DMA_BUF_MAP_H__ */