Skip to content

Commit b4e7090

Browse files
committed
dma-buf-map: Add memcpy and pointer-increment interfaces
To do framebuffer updates, one needs memcpy from system memory and a pointer-increment function. Add both interfaces with documentation. v5: * include <linux/string.h> to build on sparc64 (Sam) Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Reviewed-by: Sam Ravnborg <sam@ravnborg.org> Tested-by: Sam Ravnborg <sam@ravnborg.org> Link: https://patchwork.freedesktop.org/patch/msgid/20201103093015.1063-10-tzimmermann@suse.de
1 parent a859555 commit b4e7090

1 file changed

Lines changed: 63 additions & 10 deletions

File tree

‎include/linux/dma-buf-map.h‎

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define __DMA_BUF_MAP_H__
88

99
#include <linux/io.h>
10+
#include <linux/string.h>
1011

1112
/**
1213
* DOC: overview
@@ -32,6 +33,14 @@
3233
* accessing the buffer. Use the returned instance and the helper functions
3334
* to access the buffer's memory in the correct way.
3435
*
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+
*
3544
* Open-coding access to :c:type:`struct dma_buf_map <dma_buf_map>` is
3645
* considered bad style. Rather then accessing its fields directly, use one
3746
* of the provided helper functions, or implement your own. For example,
@@ -51,6 +60,14 @@
5160
*
5261
* dma_buf_map_set_vaddr_iomem(&map. 0xdeadbeaf);
5362
*
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+
*
5471
* Test if a mapping is valid with either dma_buf_map_is_set() or
5572
* dma_buf_map_is_null().
5673
*
@@ -73,17 +90,19 @@
7390
* if (dma_buf_map_is_equal(&sys_map, &io_map))
7491
* // always false
7592
*
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().
7998
*
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
87106
*/
88107

89108
/**
@@ -210,4 +229,38 @@ static inline void dma_buf_map_clear(struct dma_buf_map *map)
210229
}
211230
}
212231

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+
213266
#endif /* __DMA_BUF_MAP_H__ */

0 commit comments

Comments
 (0)