drm: Improve linux/scatterlist.h
[dragonfly.git] / sys / dev / drm / include / linux / scatterlist.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef _LINUX_SCATTERLIST_H_
32 #define _LINUX_SCATTERLIST_H_
33
34 #include <linux/bug.h>
35
36 /*
37  * SG table design.
38  *
39  * If flags bit 0 is set, then the sg field contains a pointer to the next sg
40  * table list. Otherwise the next entry is at sg + 1, can be determined using
41  * the sg_is_chain() function.
42  *
43  * If flags bit 1 is set, then this sg entry is the last element in a list,
44  * can be determined using the sg_is_last() function.
45  *
46  * See sg_next().
47  *
48  */
49
50 struct scatterlist {
51         union {
52                 struct vm_page          *page;
53                 struct scatterlist      *sg;
54         } sl_un;
55         unsigned long   page_link;
56         unsigned long   offset;
57         uint32_t        length;
58         dma_addr_t      dma_address;
59         uint32_t        flags;
60 };
61
62 struct sg_table {
63         struct scatterlist *sgl;        /* the list */
64         unsigned int nents;             /* number of mapped entries */
65         unsigned int orig_nents;        /* original size of list */
66 };
67
68 struct sg_page_iter {
69         struct scatterlist      *sg;
70         unsigned int            sg_pgoffset;    /* page index */
71         unsigned int            maxents;
72 };
73
74
75 #define sg_is_chain(sg)         ((sg)->page_link & 0x01)
76 #define sg_chain_ptr(sg)        \
77         ((struct scatterlist *) ((sg)->page_link & ~0x03))
78
79 /*
80  * Maximum number of entries that will be allocated in one piece, if
81  * a list larger than this is required then chaining will be utilized.
82  */
83 #define SG_MAX_SINGLE_ALLOC             (PAGE_SIZE / sizeof(struct scatterlist))
84
85 #define sg_dma_address(sg)      (sg)->dma_address
86 #define sg_dma_len(sg)          (sg)->length
87 #define sg_page(sg)             (sg)->sl_un.page
88 #define sg_scatternext(sg)      (sg)->sl_un.sg
89
90 #define SG_END          0x01
91 #define SG_CHAIN        0x02
92
93 static inline void
94 sg_set_page(struct scatterlist *sg, struct vm_page *page, unsigned int len,
95     unsigned int offset)
96 {
97         sg_page(sg) = page;
98         sg_dma_len(sg) = len;
99         sg->offset = offset;
100         if (offset > PAGE_SIZE)
101                 panic("sg_set_page: Invalid offset %d\n", offset);
102 }
103
104 #if 0
105 static inline void
106 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
107 {
108         sg_set_page(sg, virt_to_page(buf), buflen,
109             ((uintptr_t)buf) & ~PAGE_MASK);
110 }
111 #endif
112
113 static inline void
114 sg_init_table(struct scatterlist *sg, unsigned int nents)
115 {
116         bzero(sg, sizeof(*sg) * nents);
117         sg[nents - 1].flags = SG_END;
118 }
119
120 static inline struct scatterlist *
121 sg_next(struct scatterlist *sg)
122 {
123         if (sg->flags & SG_END)
124                 return (NULL);
125         sg++;
126         if (sg->flags & SG_CHAIN)
127                 sg = sg_scatternext(sg);
128         return (sg);
129 }
130
131 static inline vm_paddr_t
132 sg_phys(struct scatterlist *sg)
133 {
134         return sg_page(sg)->phys_addr + sg->offset;
135 }
136
137 /**
138  * sg_chain - Chain two sglists together
139  * @prv:        First scatterlist
140  * @prv_nents:  Number of entries in prv
141  * @sgl:        Second scatterlist
142  *
143  * Description:
144  *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
145  *
146  **/
147 static inline void
148 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
149                                         struct scatterlist *sgl)
150 {
151 /*
152  * offset and length are unused for chain entry.  Clear them.
153  */
154         struct scatterlist *sg = &prv[prv_nents - 1];
155
156         sg->offset = 0;
157         sg->length = 0;
158
159         /*
160          * Indicate a link pointer, and set the link to the second list.
161          */
162         sg->flags = SG_CHAIN;
163         sg->sl_un.sg = sgl;
164 }
165
166 /**
167  * sg_mark_end - Mark the end of the scatterlist
168  * @sg:          SG entryScatterlist
169  *
170  * Description:
171  *   Marks the passed in sg entry as the termination point for the sg
172  *   table. A call to sg_next() on this entry will return NULL.
173  *
174  **/
175 static inline void sg_mark_end(struct scatterlist *sg)
176 {
177         sg->flags = SG_END;
178 }
179
180 /**
181  * __sg_free_table - Free a previously mapped sg table
182  * @table:      The sg table header to use
183  * @max_ents:   The maximum number of entries per single scatterlist
184  *
185  *  Description:
186  *    Free an sg table previously allocated and setup with
187  *    __sg_alloc_table().  The @max_ents value must be identical to
188  *    that previously used with __sg_alloc_table().
189  *
190  **/
191 static inline void
192 __sg_free_table(struct sg_table *table, unsigned int max_ents)
193 {
194         struct scatterlist *sgl, *next;
195
196         if (unlikely(!table->sgl))
197                 return;
198
199         sgl = table->sgl;
200         while (table->orig_nents) {
201                 unsigned int alloc_size = table->orig_nents;
202                 unsigned int sg_size;
203
204                 /*
205                  * If we have more than max_ents segments left,
206                  * then assign 'next' to the sg table after the current one.
207                  * sg_size is then one less than alloc size, since the last
208                  * element is the chain pointer.
209                  */
210                 if (alloc_size > max_ents) {
211                         next = sgl[max_ents - 1].sl_un.sg;
212                         alloc_size = max_ents;
213                         sg_size = alloc_size - 1;
214                 } else {
215                         sg_size = alloc_size;
216                         next = NULL;
217                 }
218
219                 table->orig_nents -= sg_size;
220                 kfree(sgl);
221                 sgl = next;
222         }
223
224         table->sgl = NULL;
225 }
226
227 /**
228  * sg_free_table - Free a previously allocated sg table
229  * @table:      The mapped sg table header
230  *
231  **/
232 static inline void
233 sg_free_table(struct sg_table *table)
234 {
235         __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
236 }
237
238 /**
239  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
240  * @table:      The sg table header to use
241  * @nents:      Number of entries in sg list
242  * @max_ents:   The maximum number of entries the allocator returns per call
243  * @gfp_mask:   GFP allocation mask
244  *
245  * Description:
246  *   This function returns a @table @nents long. The allocator is
247  *   defined to return scatterlist chunks of maximum size @max_ents.
248  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
249  *   chained in units of @max_ents.
250  *
251  * Notes:
252  *   If this function returns non-0 (eg failure), the caller must call
253  *   __sg_free_table() to cleanup any leftover allocations.
254  *
255  **/
256 static inline int
257 __sg_alloc_table(struct sg_table *table, unsigned int nents,
258                 unsigned int max_ents, gfp_t gfp_mask)
259 {
260         struct scatterlist *sg, *prv;
261         unsigned int left;
262
263         memset(table, 0, sizeof(*table));
264
265         if (nents == 0)
266                 return -EINVAL;
267         left = nents;
268         prv = NULL;
269         do {
270                 unsigned int sg_size, alloc_size = left;
271
272                 if (alloc_size > max_ents) {
273                         alloc_size = max_ents;
274                         sg_size = alloc_size - 1;
275                 } else
276                         sg_size = alloc_size;
277
278                 left -= sg_size;
279
280                 sg = kmalloc(alloc_size * sizeof(struct scatterlist), M_DRM, gfp_mask);
281                 if (unlikely(!sg)) {
282                 /*
283                  * Adjust entry count to reflect that the last
284                  * entry of the previous table won't be used for
285                  * linkage.  Without this, sg_kfree() may get
286                  * confused.
287                  */
288                         if (prv)
289                                 table->nents = ++table->orig_nents;
290
291                         return -ENOMEM;
292                 }
293
294                 sg_init_table(sg, alloc_size);
295                 table->nents = table->orig_nents += sg_size;
296
297                 /*
298                  * If this is the first mapping, assign the sg table header.
299                  * If this is not the first mapping, chain previous part.
300                  */
301                 if (prv)
302                         sg_chain(prv, max_ents, sg);
303                 else
304                         table->sgl = sg;
305
306                 /*
307                 * If no more entries after this one, mark the end
308                 */
309                 if (!left)
310                         sg_mark_end(&sg[sg_size - 1]);
311
312                 prv = sg;
313         } while (left);
314
315         return 0;
316 }
317
318 /**
319  * sg_alloc_table - Allocate and initialize an sg table
320  * @table:      The sg table header to use
321  * @nents:      Number of entries in sg list
322  * @gfp_mask:   GFP allocation mask
323  *
324  *  Description:
325  *    Allocate and initialize an sg table. If @nents@ is larger than
326  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
327  *
328  **/
329
330 static inline int
331 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
332 {
333         int ret;
334
335         ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
336                 gfp_mask);
337         if (unlikely(ret))
338                 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
339
340         return ret;
341 }
342
343 /*
344  * Iterate pages in sg list.
345  */
346 static inline void
347 _sg_iter_next(struct sg_page_iter *iter)
348 {
349         struct scatterlist *sg;
350         unsigned int pgcount;
351
352         sg = iter->sg;
353         pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
354
355         ++iter->sg_pgoffset;
356         while (iter->sg_pgoffset >= pgcount) {
357                 iter->sg_pgoffset -= pgcount;
358                 sg = sg_next(sg);
359                 --iter->maxents;
360                 if (sg == NULL || iter->maxents == 0)
361                         break;
362                 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
363         }
364         iter->sg = sg;
365 }
366
367 /*
368  * NOTE: pgoffset is really a page index, not a byte offset.
369  */
370 static inline void
371 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
372               unsigned int nents, unsigned long pgoffset)
373 {
374         if (nents) {
375                 /*
376                  * Nominal case.  Note subtract 1 from starting page index
377                  * for initial _sg_iter_next() call.
378                  */
379                 iter->sg = sgl;
380                 iter->sg_pgoffset = pgoffset - 1;
381                 iter->maxents = nents;
382                 _sg_iter_next(iter);
383         } else {
384                 /*
385                  * Degenerate case
386                  */
387                 iter->sg = NULL;
388                 iter->sg_pgoffset = 0;
389                 iter->maxents = 0;
390         }
391 }
392
393 static inline struct vm_page *
394 sg_page_iter_page(struct sg_page_iter *piter)
395 {
396         return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
397 }
398
399 static inline dma_addr_t
400 sg_page_iter_dma_address(struct sg_page_iter *spi)
401 {
402         return spi->sg->dma_address + (spi->sg_pgoffset << PAGE_SHIFT);
403 }
404
405 #define for_each_sg_page(sgl, iter, nents, pgoffset)                    \
406         for (_sg_iter_init(sgl, iter, nents, pgoffset);                 \
407              (iter)->sg; _sg_iter_next(iter))
408
409 #define for_each_sg(sglist, sg, sgmax, _itr)                            \
410         for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
411
412 #endif  /* _LINUX_SCATTERLIST_H_ */