1 /* Public domain. */
2
3 #ifndef _LINUX_DMA_BUF_MAP_H
4 #define _LINUX_DMA_BUF_MAP_H
5
6 #include <linux/io.h>
7 #include <linux/string.h>
8
9 struct dma_buf_map {
10 union {
11 void *vaddr_iomem;
12 void *vaddr;
13 };
14 bool is_iomem;
15 };
16
17 static inline void
dma_buf_map_incr(struct dma_buf_map * dbm,size_t n)18 dma_buf_map_incr(struct dma_buf_map *dbm, size_t n)
19 {
20 if (dbm->is_iomem)
21 dbm->vaddr_iomem += n;
22 else
23 dbm->vaddr += n;
24 }
25
26 static inline void
dma_buf_map_memcpy_to(struct dma_buf_map * dbm,const void * src,size_t len)27 dma_buf_map_memcpy_to(struct dma_buf_map *dbm, const void *src, size_t len)
28 {
29 if (dbm->is_iomem)
30 memcpy_toio(dbm->vaddr_iomem, src, len);
31 else
32 memcpy(dbm->vaddr, src, len);
33 }
34
35 static inline bool
dma_buf_map_is_null(const struct dma_buf_map * dbm)36 dma_buf_map_is_null(const struct dma_buf_map *dbm)
37 {
38 if (dbm->is_iomem)
39 return (dbm->vaddr_iomem == NULL);
40 else
41 return (dbm->vaddr == NULL);
42 }
43
44 static inline bool
dma_buf_map_is_set(const struct dma_buf_map * dbm)45 dma_buf_map_is_set(const struct dma_buf_map *dbm)
46 {
47 if (dbm->is_iomem)
48 return (dbm->vaddr_iomem != NULL);
49 else
50 return (dbm->vaddr != NULL);
51 }
52
53 static inline bool
dma_buf_map_is_equal(const struct dma_buf_map * dbm_a,const struct dma_buf_map * dbm_b)54 dma_buf_map_is_equal(
55 const struct dma_buf_map *dbm_a, const struct dma_buf_map *dbm_b)
56 {
57 if (dbm_a->is_iomem != dbm_b->is_iomem)
58 return (false);
59
60 if (dbm_a->is_iomem)
61 return (dbm_a->vaddr_iomem == dbm_b->vaddr_iomem);
62 else
63 return (dbm_a->vaddr == dbm_b->vaddr);
64 }
65
66 static inline void
dma_buf_map_clear(struct dma_buf_map * dbm)67 dma_buf_map_clear(struct dma_buf_map *dbm)
68 {
69 if (dbm->is_iomem) {
70 dbm->vaddr_iomem = NULL;
71 dbm->is_iomem = false;
72 } else {
73 dbm->vaddr = NULL;
74 }
75 }
76
77 static inline void
dma_buf_map_set_vaddr_iomem(struct dma_buf_map * dbm,void * addr)78 dma_buf_map_set_vaddr_iomem(struct dma_buf_map *dbm, void *addr)
79 {
80 dbm->vaddr_iomem = addr;
81 dbm->is_iomem = true;
82 }
83
84 static inline void
dma_buf_map_set_vaddr(struct dma_buf_map * dbm,void * addr)85 dma_buf_map_set_vaddr(struct dma_buf_map *dbm, void *addr)
86 {
87 dbm->vaddr = addr;
88 dbm->is_iomem = false;
89 }
90
91 #endif
92