GNU Linux-libre 4.14.319-gnu1
[releases.git] / tools / virtio / linux / scatterlist.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef SCATTERLIST_H
3 #define SCATTERLIST_H
4 #include <linux/kernel.h>
5 #include <linux/bug.h>
6
7 struct scatterlist {
8         unsigned long   page_link;
9         unsigned int    offset;
10         unsigned int    length;
11         dma_addr_t      dma_address;
12 };
13
14 /* Scatterlist helpers, stolen from linux/scatterlist.h */
15 #define sg_is_chain(sg)         ((sg)->page_link & 0x01)
16 #define sg_is_last(sg)          ((sg)->page_link & 0x02)
17 #define sg_chain_ptr(sg)        \
18         ((struct scatterlist *) ((sg)->page_link & ~0x03))
19
20 /**
21  * sg_assign_page - Assign a given page to an SG entry
22  * @sg:             SG entry
23  * @page:           The page
24  *
25  * Description:
26  *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
27  *   variant.
28  *
29  **/
30 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
31 {
32         unsigned long page_link = sg->page_link & 0x3;
33
34         /*
35          * In order for the low bit stealing approach to work, pages
36          * must be aligned at a 32-bit boundary as a minimum.
37          */
38         BUG_ON((unsigned long) page & 0x03);
39 #ifdef CONFIG_DEBUG_SG
40         BUG_ON(sg->sg_magic != SG_MAGIC);
41         BUG_ON(sg_is_chain(sg));
42 #endif
43         sg->page_link = page_link | (unsigned long) page;
44 }
45
46 /**
47  * sg_set_page - Set sg entry to point at given page
48  * @sg:          SG entry
49  * @page:        The page
50  * @len:         Length of data
51  * @offset:      Offset into page
52  *
53  * Description:
54  *   Use this function to set an sg entry pointing at a page, never assign
55  *   the page directly. We encode sg table information in the lower bits
56  *   of the page pointer. See sg_page() for looking up the page belonging
57  *   to an sg entry.
58  *
59  **/
60 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
61                                unsigned int len, unsigned int offset)
62 {
63         sg_assign_page(sg, page);
64         sg->offset = offset;
65         sg->length = len;
66 }
67
68 static inline struct page *sg_page(struct scatterlist *sg)
69 {
70 #ifdef CONFIG_DEBUG_SG
71         BUG_ON(sg->sg_magic != SG_MAGIC);
72         BUG_ON(sg_is_chain(sg));
73 #endif
74         return (struct page *)((sg)->page_link & ~0x3);
75 }
76
77 /*
78  * Loop over each sg element, following the pointer to a new list if necessary
79  */
80 #define for_each_sg(sglist, sg, nr, __i)        \
81         for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
82
83 /**
84  * sg_chain - Chain two sglists together
85  * @prv:        First scatterlist
86  * @prv_nents:  Number of entries in prv
87  * @sgl:        Second scatterlist
88  *
89  * Description:
90  *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
91  *
92  **/
93 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
94                             struct scatterlist *sgl)
95 {
96         /*
97          * offset and length are unused for chain entry.  Clear them.
98          */
99         prv[prv_nents - 1].offset = 0;
100         prv[prv_nents - 1].length = 0;
101
102         /*
103          * Set lowest bit to indicate a link pointer, and make sure to clear
104          * the termination bit if it happens to be set.
105          */
106         prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
107 }
108
109 /**
110  * sg_mark_end - Mark the end of the scatterlist
111  * @sg:          SG entryScatterlist
112  *
113  * Description:
114  *   Marks the passed in sg entry as the termination point for the sg
115  *   table. A call to sg_next() on this entry will return NULL.
116  *
117  **/
118 static inline void sg_mark_end(struct scatterlist *sg)
119 {
120 #ifdef CONFIG_DEBUG_SG
121         BUG_ON(sg->sg_magic != SG_MAGIC);
122 #endif
123         /*
124          * Set termination bit, clear potential chain bit
125          */
126         sg->page_link |= 0x02;
127         sg->page_link &= ~0x01;
128 }
129
130 /**
131  * sg_unmark_end - Undo setting the end of the scatterlist
132  * @sg:          SG entryScatterlist
133  *
134  * Description:
135  *   Removes the termination marker from the given entry of the scatterlist.
136  *
137  **/
138 static inline void sg_unmark_end(struct scatterlist *sg)
139 {
140 #ifdef CONFIG_DEBUG_SG
141         BUG_ON(sg->sg_magic != SG_MAGIC);
142 #endif
143         sg->page_link &= ~0x02;
144 }
145
146 static inline struct scatterlist *sg_next(struct scatterlist *sg)
147 {
148 #ifdef CONFIG_DEBUG_SG
149         BUG_ON(sg->sg_magic != SG_MAGIC);
150 #endif
151         if (sg_is_last(sg))
152                 return NULL;
153
154         sg++;
155         if (unlikely(sg_is_chain(sg)))
156                 sg = sg_chain_ptr(sg);
157
158         return sg;
159 }
160
161 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
162 {
163         memset(sgl, 0, sizeof(*sgl) * nents);
164 #ifdef CONFIG_DEBUG_SG
165         {
166                 unsigned int i;
167                 for (i = 0; i < nents; i++)
168                         sgl[i].sg_magic = SG_MAGIC;
169         }
170 #endif
171         sg_mark_end(&sgl[nents - 1]);
172 }
173
174 static inline dma_addr_t sg_phys(struct scatterlist *sg)
175 {
176         return page_to_phys(sg_page(sg)) + sg->offset;
177 }
178
179 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
180                               unsigned int buflen)
181 {
182         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
183 }
184
185 static inline void sg_init_one(struct scatterlist *sg,
186                                const void *buf, unsigned int buflen)
187 {
188         sg_init_table(sg, 1);
189         sg_set_buf(sg, buf, buflen);
190 }
191 #endif /* SCATTERLIST_H */