|
7 | 7 | #define __DMA_BUF_MAP_H__ |
8 | 8 |
|
9 | 9 | #include <linux/io.h> |
| 10 | +#include <linux/string.h> |
10 | 11 |
|
11 | 12 | /** |
12 | 13 | * DOC: overview |
|
32 | 33 | * accessing the buffer. Use the returned instance and the helper functions |
33 | 34 | * to access the buffer's memory in the correct way. |
34 | 35 | * |
| 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. |
| 43 | + * |
35 | 44 | * Open-coding access to :c:type:`struct dma_buf_map <dma_buf_map>` is |
36 | 45 | * considered bad style. Rather then accessing its fields directly, use one |
37 | 46 | * of the provided helper functions, or implement your own. For example, |
|
51 | 60 | * |
52 | 61 | * dma_buf_map_set_vaddr_iomem(&map. 0xdeadbeaf); |
53 | 62 | * |
| 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. |
| 66 | + * |
| 67 | + * .. code-block:: c |
| 68 | + * |
| 69 | + * dma_buf_map_clear(&map); |
| 70 | + * |
54 | 71 | * Test if a mapping is valid with either dma_buf_map_is_set() or |
55 | 72 | * dma_buf_map_is_null(). |
56 | 73 | * |
|
73 | 90 | * if (dma_buf_map_is_equal(&sys_map, &io_map)) |
74 | 91 | * // always false |
75 | 92 | * |
76 | | - * Instances of struct dma_buf_map do not have to be cleaned up, but |
77 | | - * can be cleared to NULL with dma_buf_map_clear(). Cleared mappings |
78 | | - * always refer to system memory. |
| 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 |
| 97 | + * dma_buf_map_incr(). |
79 | 98 | * |
80 | | - * The type :c:type:`struct dma_buf_map <dma_buf_map>` and its helpers are |
81 | | - * actually independent from the dma-buf infrastructure. When sharing buffers |
82 | | - * among devices, drivers have to know the location of the memory to access |
83 | | - * the buffers in a safe way. :c:type:`struct dma_buf_map <dma_buf_map>` |
84 | | - * solves this problem for dma-buf and its users. If other drivers or |
85 | | - * sub-systems require similar functionality, the type could be generalized |
86 | | - * and moved to a more prominent header file. |
| 99 | + * .. code-block:: c |
| 100 | + * |
| 101 | + * const void *src = ...; // source buffer |
| 102 | + * size_t len = ...; // length of src |
| 103 | + * |
| 104 | + * dma_buf_map_memcpy_to(&map, src, len); |
| 105 | + * dma_buf_map_incr(&map, len); // go to first byte after the memcpy |
87 | 106 | */ |
88 | 107 |
|
89 | 108 | /** |
@@ -210,4 +229,38 @@ static inline void dma_buf_map_clear(struct dma_buf_map *map) |
210 | 229 | } |
211 | 230 | } |
212 | 231 |
|
| 232 | +/** |
| 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 |
| 237 | + * |
| 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. |
| 241 | + */ |
| 242 | +static inline void dma_buf_map_memcpy_to(struct dma_buf_map *dst, const void *src, size_t len) |
| 243 | +{ |
| 244 | + if (dst->is_iomem) |
| 245 | + memcpy_toio(dst->vaddr_iomem, src, len); |
| 246 | + else |
| 247 | + memcpy(dst->vaddr, src, len); |
| 248 | +} |
| 249 | + |
| 250 | +/** |
| 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 |
| 254 | + * |
| 255 | + * Increments the address stored in a dma-buf mapping. Depending on the |
| 256 | + * buffer's location, the correct value will be updated. |
| 257 | + */ |
| 258 | +static inline void dma_buf_map_incr(struct dma_buf_map *map, size_t incr) |
| 259 | +{ |
| 260 | + if (map->is_iomem) |
| 261 | + map->vaddr_iomem += incr; |
| 262 | + else |
| 263 | + map->vaddr += incr; |
| 264 | +} |
| 265 | + |
213 | 266 | #endif /* __DMA_BUF_MAP_H__ */ |
0 commit comments