XenevaOS
Loading...
Searching...
No Matches
scatterlist.h
Go to the documentation of this file.
1#ifndef __LINUX_SCATTERLIST_H__
2#define __LINUX_SCATTERLIST_H__
3
4/*
5 * DCL <linux/scatterlist.h> -- struct scatterlist and sg_init_one().
6 *
7 * ### Deliberately not mainline's layout
8 *
9 * mainline's struct scatterlist is
10 *
11 * unsigned long page_link; // page pointer + chain/terminate bits
12 * unsigned int offset;
13 * unsigned int length;
14 * dma_addr_t dma_address;
15 *
16 * -- a *page-based* list, whose whole job is to walk a compound page in
17 * physical segments and to carry a DMA address that the IOMMU/streaming
18 * mapper filled in. Xeneva has neither: a buffer handed to a virtio queue is
19 * a single linear-map virtual address (AuVirtioPCISetupQueue and buf_phys()
20 * in virtio_shim.c turn it into a ring address directly), and there is no
21 * DMA API to populate dma_address through. So the struct here carries exactly
22 * what the one consumer reads:
23 *
24 * struct scatterlist { void* buf; unsigned int len; };
25 *
26 * That is the shape DCL/linux_kmod_shim.c's sg_init_one() fills in, and the
27 * shape virtio_shim.c's virtqueue_add_inbuf/sgs walk -- both of which are
28 * written against this definition, not against upstream's. Keeping upstream's
29 * fields here would leave four members that every write has to remember to
30 * leave zero, for a mapping layer that does not exist.
31 *
32 * The consequence worth writing down: a driver that reaches into the sg for a
33 * page (`sg_virt()` on a compound page, DMA mapping helpers) will not build.
34 * Add those when something needs them, with the linear-map address doing the
35 * job of `page_link`.
36 *
37 * This struct used to live in <linux/virtio.h>, which included it in every
38 * file that talked to a virtqueue. It is here because that is mainline's home
39 * for it, so a ported source includes the header mainline tells it to; virtio.h
40 * now includes this file instead of redeclaring the type. Exactly one
41 * definition either way -- two would be two different `struct scatterlist`s
42 * with no way to pass one to the other.
43 */
44
45#include <linux/kernel.h> /* size_t */
46
47/*
48 * One contiguous region, as the shim sees it. buf is a *virtual* address in
49 * the linear map; the shim converts to a bus address at kick time.
50 *
51 * `page` is the one member that is not read by the queue machinery -- it is
52 * the bookkeeping a *releasing* driver walks. virtio_console.c:350-357 does
53 *
54 * for (i = 0; i < buf->sgpages; i++) {
55 * struct page *page = sg_page(&buf->sg[i]);
56 * if (!page)
57 * break;
58 * put_page(page);
59 * }
60 *
61 * and sg_set_page() at :872/:891 is what would have put the pointer there.
62 * Without a slot for it sg_page() has nothing to return but NULL, the `break`
63 * fires on the first entry, and the pages behind it are never released: a
64 * leak that no test in this tree would report, because the only path that
65 * fills sgpages > 0 is the splice one (see <linux/splice.h> on why that is
66 * not reachable here). Carrying the pointer costs 8 bytes an entry and makes
67 * the release loop do what it reads as doing.
68 *
69 * buf stays the first member: every existing walk (virtio_shim.c,
70 * linux_kmod_shim.c's sg_init_one) reads ->buf and ->len by name, and putting
71 * a pointer in front of them would move those offsets for no gain.
72 */
74 void* buf;
75 /*
76 * One length, two spellings. DCL's writers (sg_init_one,
77 * sg_set_page, the shmem tables) name it `len`; mainline's readers
78 * name it `length` -- virtgpu_object.c:195 copies sg->length into a
79 * virtio_gpu_mem_entry -- and vendored sources are not edited to a
80 * local spelling. A union keeps the two names on one storage, so a
81 * write through either is visible through the other.
82 */
83 union {
84 unsigned int len;
85 unsigned int length;
86 };
87 struct page* page;
88 /*
89 * mainline's two DMA fields, carried in full even though nothing
90 * fills them: there is no DMA mapping layer in this kernel, so
91 * dma_address is never written and always reads 0. drm_prime.c:889
92 * still walks it (`sg_dma_address(sgt->sgl)`) and drm_prime.c:893
93 * walks sg_dma_len(), so the *reads* have to be well-formed even
94 * where the writes never happen -- which is the same situation as
95 * iomem_resource in <linux/ioport.h>: an empty but correctly shaped
96 * answer, rather than a missing member.
97 *
98 * mainline makes these conditional on CONFIG_NEED_SG_DMA_LENGTH; here
99 * they are unconditional, so sg_dma_len() below has one arm instead
100 * of two.
101 */
103 unsigned int dma_length;
104};
105
106/*
107 * struct sg_table -- the *list* of scatterlists, and the reason this header
108 * had to grow it is a C scoping rule rather than a missing field.
109 *
110 * drm_cache.h:41 declares `void drm_clflush_sg(struct sg_table *st);` and
111 * drm_cache.c:127 defines the same function. Both spell the parameter
112 * identically, and clang still reports "conflicting types for
113 * 'drm_clflush_sg'" -- because nothing named `struct sg_table` at file scope
114 * before those lines. C scopes a struct tag that is *first* mentioned inside a
115 * function prototype's parameter list to that declaration alone, so the two
116 * lines name two unrelated incomplete types and the signatures genuinely do
117 * differ. clang warns about the rule first ("declaration of 'struct sg_table'
118 * will not be visible outside of this function", -Wvisibility) and errors
119 * after, and reading only the error sends you hunting for a difference
120 * between two spellings that are byte-identical.
121 *
122 * mainline does not hit this because mainline's <linux/scatterlist.h>
123 * *defines* struct sg_table, so the tag is at file scope before any drm
124 * header is parsed. A forward declaration already exists here in drm_drv.h:67,
125 * but drm_cache.h does not include it -- and moving the decl there would fix
126 * only this consumer, leaving the next header that names the type to
127 * rediscover the same rule.
128 *
129 * All three of mainline's fields, whole, though nothing in this tree fills one
130 * yet: `sgl` is the array, `nents` the entries that were mapped, and
131 * `orig_nents` the entries that were handed in. A two-field version would be
132 * worse than carrying all three -- see the note on struct vm_area_struct in
133 * <linux/mm.h> for why.
134 */
135struct sg_table {
137 unsigned int nents;
138 unsigned int orig_nents;
139};
140
141/*
142 * sg_init_one() -- point a single sg entry at a whole buffer.
143 *
144 * Declared here, defined in DCL/linux_kmod_shim.c (also exported to loaded
145 * modules through KernelAA64/kernel_exports.c, which is how the old prebuilt
146 * virtio_rng.ko reached it). It now also clears ->page: an sg entry built by
147 * sg_init_one() names no page, and sg_page() on it answers NULL, which is
148 * what a caller walking a mixed list to release it needs to see for the
149 * entries that came from the buffer path rather than the page path.
150 */
151extern void sg_init_one(struct scatterlist* sg, const void* buf,
152 unsigned int len);
153
154/*
155 * sg_page() / sg_set_page() / sg_init_table() -- declarations only, with the
156 * bodies in DCL/linux_kmod_shim.c beside sg_init_one().
157 *
158 * They cannot be static inlines here without dragging <linux/pagemap.h> in:
159 * sg_set_page() has to turn a struct page* into the address buf should point
160 * at, and page_address() is defined there. mainline gets this for free
161 * because its scatterlist.h and pagemap.h are the same subsystem; in DCL
162 * pagemap.h sits on top of mm.h and this header is included by
163 * <linux/virtio.h>, which nearly every driver pulls in -- so the dependency
164 * goes the other way and the three become calls.
165 *
166 * The signatures are mainline's, including sg_set_page()'s `offset` (bytes
167 * into the page) which lands in buf as page_address(page) + offset, and
168 * sg_init_table()'s count, which clears buf/len/page across `n` entries so a
169 * partially filled list never hands the queue a stale pointer.
170 */
171extern struct page* sg_page(const struct scatterlist* sg);
172extern void sg_set_page(struct scatterlist* sg, struct page* page,
173 unsigned int len, unsigned int offset);
174extern void sg_init_table(struct scatterlist* sgl, unsigned int n);
175
176/*
177 * sg_virt() -- the linear-map virtual address of an sg entry.
178 *
179 * mainline computes it from the page; here buf *is* the virtual address, so
180 * the helper is a field read. Provided because it is the accessor ported code
181 * reaches for, and returning nothing but `buf` is correct rather than merely
182 * convenient.
183 */
184static inline void* sg_virt(const struct scatterlist* sg)
185{
186 return (void*)sg->buf;
187}
188
189static inline unsigned int sg_len(const struct scatterlist* sg)
190{
191 return sg->len;
192}
193
194/*
195 * struct sg_page_iter and the for_each_sg*_page family -- the page-granular
196 * walk over an sg list, needed by drm_prime.c:1059 (a local
197 * `struct sg_page_iter page_iter`, so the type has to be complete, not merely
198 * forward-declared) and by its for_each_sgtable_page() loop at :1061.
199 *
200 * mainline's five fields are all here. The sixth, __sg_page_nents, is not
201 * mainline's and exists because of a difference this header documents above:
202 * mainline's scatterlist carries chain and terminate *bits* in page_link, so
203 * __sg_page_iter_next() finds the end of the list with sg_is_last(). DCL's
204 * scatterlist is {buf, len, page} with no bits to spare and no terminator,
205 * so the bound has to be carried alongside the walk instead. It is added
206 * rather than substituted: every mainline field keeps its name and meaning,
207 * and one more records what sg_is_last() would have.
208 *
209 * __sg_page_index stays 0 for every entry, because an sg entry here maps one
210 * buffer and nth_page() would have nothing to index into. It is kept so that
211 * sg_page_iter_page() can be spelled mainline's way.
212 */
215 unsigned int __sg_page_accum;
216 unsigned int __sg_page_index;
217 unsigned int entry;
218 unsigned int offset;
219 unsigned int __sg_page_nents;
220};
221
222static inline struct page* sg_page_iter_page(struct sg_page_iter* pgi)
223{
224 return sg_page(pgi->sg);
225}
226
227/*
228 * Both static inline rather than shim functions: neither touches shared
229 * state, and the header already answers the other accessors that way
230 * (sg_virt(), sg_len() above). mainline declares them extern because
231 * mainline's bodies walk compound pages through nth_page(); there is no
232 * compound page here to walk.
233 */
234static inline void __sg_page_iter_start(struct sg_page_iter* pgi,
235 struct scatterlist* sglist,
236 unsigned int nents, unsigned int pgoff)
237{
238 pgi->sg = sglist;
239 pgi->entry = 0;
240 pgi->offset = pgoff;
241 pgi->__sg_page_accum = 0;
242 pgi->__sg_page_index = 0;
243 pgi->__sg_page_nents = nents;
244}
245
246static inline int __sg_page_iter_next(struct sg_page_iter* pgi)
247{
248 if (pgi->entry >= pgi->__sg_page_nents)
249 return 0;
250 pgi->sg += 1;
251 pgi->entry += 1;
252 return 1;
253}
254
255#define for_each_sg_page(sglist, pgi, nents, pgoff) \
256 for (__sg_page_iter_start(pgi, sglist, nents, pgoff); \
257 __sg_page_iter_next(pgi);)
258
259#define for_each_sgtable_page(sgt, pgi, pgoff) \
260 for_each_sg_page((sgt)->sgl, pgi, (sgt)->orig_nents, pgoff)
261
262/*
263 * The DMA-address walk, and the accessors behind it -- drm_prime.c:889 and
264 * :893 (sg_dma_address, sg_dma_len) and :894 (for_each_sgtable_dma_sg).
265 *
266 * Both accessors are field reads off the two members added to struct
267 * scatterlist above. sg_dma_len() has one arm because dma_length is
268 * unconditional here; mainline's second arm reads ->length when
269 * CONFIG_NEED_SG_DMA_LENGTH is off, and there is no config to be off.
270 */
271#define sg_dma_address(sg) ((sg)->dma_address)
272#define sg_dma_len(sg) ((sg)->dma_length)
273
274/*
275 * The table walk keeps mainline's spelling -- for_each_sgtable_dma_sg(sgt,
276 * sg, i) -- and threads the caller's variable through to the entry walk
277 * instead of naming a fixed `sg` inside it: virtgpu_prime.c:174 walks with
278 * `sl`, and a macro that hardcodes the name cannot serve that caller. The
279 * bound is explicit because DCL's entries carry no chain/terminator bit (see
280 * struct scatterlist).
281 */
282#define for_each_sg_dma(sglist, sg, nents, i) \
283 for ((i) = 0; (i) < (nents) && ((sg) = &(sglist)[(i)]); (i)++)
284
285#define for_each_sgtable_dma_sg(sgt, sg, i) \
286 for_each_sg_dma((sgt)->sgl, sg, (sgt)->nents, i)
287
288/*
289 * for_each_sg() -- mainline's plain entry walk, added with the virtio-gpu
290 * port. The caller is virtgpu_object.c:193, filling a resource's attach
291 * mem-entry list: with VIRTIO_F_ACCESS_PLATFORM never offered, the addresses
292 * come from sg_phys() and every entry counts (orig_nents, not the
293 * dma-degraded nents).
294 *
295 * The spellings are mainline's, same trade as for_each_sg_dma() above: the
296 * loop variable is the hardcoded `sg`, and the bound is explicit because
297 * DCL's entries carry no chain/terminator bit (see struct scatterlist).
298 */
299#define for_each_sg(sglist, sg, nents, i) \
300 for ((i) = 0; (i) < (nents) && ((sg) = &(sglist)[(i)]); (i)++)
301
302#define for_each_sgtable_sg(sgt, sg, i) \
303 for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
304
305/*
306 * struct sg_dma_page_iter and its walk -- the address-granular counterpart,
307 * asked for by drm_prime.c:1085 (a local `struct sg_dma_page_iter dma_iter`,
308 * so the type has to be complete) and its for_each_sgtable_dma_page() loop
309 * and sg_page_iter_dma_address() call at :1088.
310 *
311 * Two spellings there are worth recording, because both look wrong and are
312 * not. First, sg_page_iter_dma_address() is named for the *page* iter but is
313 * called with a `struct sg_dma_page_iter *`; it takes that type, which is
314 * the only spelling under which mainline's own call site compiles. Second,
315 * for_each_sgtable_dma_page() hands `&(pgi)->base` to the page walk rather
316 * than driving the outer struct -- the dma iteration *is* the page iteration
317 * with a dma address read alongside it, so there is one advance step and not
318 * two to keep in sync.
319 *
320 * mainline: include/linux/scatterlist.h.
321 *
322 * sg_page_iter_dma_len() is the one shape that differs: mainline caps it at
323 * PAGE_SIZE, because mainline's iterator yields one page per step and the
324 * remainder of a larger entry belongs to the next step. Here
325 * __sg_page_iter_next() above advances one whole sg entry at a time, so
326 * there is no next step to hand the remainder to and the cap would truncate
327 * the answer to a value no caller could use to cover the entry. The rest --
328 * sg_dma_len minus the offset -- is the length that actually stands.
329 */
335
336static inline dma_addr_t
337sg_page_iter_dma_address(struct sg_dma_page_iter* pgi)
338{
339 return sg_dma_address(pgi->base.sg) + pgi->base.offset;
340}
341
342static inline unsigned int
343sg_page_iter_dma_len(struct sg_dma_page_iter* pgi)
344{
345 return sg_dma_len(pgi->base.sg) - pgi->base.offset;
346}
347
348#define for_each_sgtable_dma_page(sgt, pgi, pgoff) \
349 for_each_sg_page((sgt)->sgl, &(pgi)->base, (sgt)->nents, pgoff)
350
351/*
352 * sg_alloc_table_from_pages_segment() -- build an sg_table over an array of
353 * pages, mainline's lib/scatterlist.c helper. The one caller is
354 * drm_prime_pages_to_sg() (drm_prime.c:865), the generic
355 * drm_gem_object_funcs.get_sg_table implementation: it has already allocated
356 * the struct sg_table and hands over the page array plus the byte range the
357 * buffer covers. The body is in DCL/linux_mm_shim.c and allocates one entry
358 * per page, which is the granularity DCL's iterators above walk
359 * (__sg_page_iter_next() advances by entry) rather than mainline's squashing
360 * of physically contiguous runs.
361 *
362 * `offset` is the byte offset into the first page, `size` the valid byte
363 * count after it, `max_segment` mainline's cap on a squashed run; that cap
364 * has nothing to apply to here and is accepted and ignored (as the call
365 * site's UINT_MAX-derived value already implies). `gfp_mask` likewise: the
366 * array comes from the native allocator, which takes no flags.
367 */
369 struct page** pages, unsigned int n_pages,
370 unsigned int offset, unsigned long size,
371 unsigned int max_segment, gfp_t gfp_mask);
372
373/*
374 * sg_free_table() -- release the scatterlist array behind a table.
375 *
376 * This was a static-inline no-op as long as every table in the tree was
377 * filled in place: sg_init_table() above allocates nothing. The allocator
378 * above is the first producer whose array is on the heap, so its release has
379 * to be real; the two callers are both in drm_prime.c (:686 on the map
380 * failure path, :711 on unmap), and both tables got there through a
381 * get_sg_table() helper, i.e. through that allocator. A caller holding its
382 * own array still has sg_init_table() and no release to call.
383 *
384 * Declared here, defined in DCL/linux_mm_shim.c: the body is kfree(), and
385 * this header deliberately does not include <linux/slab.h> to name it.
386 * mainline declares the same function in this header.
387 */
388/*
389 * sg_alloc_table() -- allocate a table of `nents` zeroed entries, the
390 * entry point sg_init_table() (whose callers fill in place) is the static
391 * form of. Added with the virtio-gpu port: the command path sizes its
392 * per-page table from it and the vram path its one-entry mapping. Body in
393 * DCL/linux_mm_shim.c beside sg_free_table().
394 */
395int sg_alloc_table(struct sg_table* sgt, unsigned int nents, gfp_t gfp);
396
397void sg_free_table(struct sg_table* sgt);
398
399/*
400 * sg_phys() -- the physical address an entry covers. mainline spells it
401 * page_to_phys(sg_page(sg)) + sg->offset; a DCL entry names its buffer
402 * directly (struct scatterlist above). The entries that reach this are the
403 * shmem tables' linear-map aliases, so the answer is the direct map's own
404 * inverse: V2P() of ->buf, page offset included. Body in
405 * DCL/linux_kmod_shim.c beside sg_init_one().
406 *
407 * The caller is the non-DMA-API arm of virtgpu_object.c:194's mem-entry
408 * fill; mainline declares this in the same header.
409 */
411
412#endif /* __LINUX_SCATTERLIST_H__ */
size_t pages
Definition audrv.c:83
size_t size
Definition drm_bridge.h:1299
size_t size_t offset
Definition drm_bridge.h:1299
size_t len
Definition initial.c:23
void sg_init_one(void)
uintptr_t dma_addr_t
Definition kernel.h:42
unsigned int gfp_t
Definition kernel.h:56
int sg_alloc_table(struct sg_table *sgt, unsigned int nents, gfp_t gfp)
void sg_init_table(struct scatterlist *sgl, unsigned int n)
#define sg_dma_address(sg)
Definition scatterlist.h:271
struct page * sg_page(const struct scatterlist *sg)
dma_addr_t sg_phys(const struct scatterlist *sg)
#define sg_dma_len(sg)
Definition scatterlist.h:272
int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages, unsigned int n_pages, unsigned int offset, unsigned long size, unsigned int max_segment, gfp_t gfp_mask)
void sg_free_table(struct sg_table *sgt)
void sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len, unsigned int offset)
Definition pagemap.h:38
Definition scatterlist.h:73
dma_addr_t dma_address
Definition scatterlist.h:102
struct page * page
Definition scatterlist.h:87
unsigned int dma_length
Definition scatterlist.h:103
void * buf
Definition scatterlist.h:74
unsigned int len
Definition scatterlist.h:84
unsigned int length
Definition scatterlist.h:85
Definition scatterlist.h:330
struct sg_page_iter base
Definition scatterlist.h:331
unsigned int __sg_dma_page_accum
Definition scatterlist.h:333
dma_addr_t dma_address
Definition scatterlist.h:332
Definition scatterlist.h:213
unsigned int offset
Definition scatterlist.h:218
unsigned int __sg_page_nents
Definition scatterlist.h:219
unsigned int __sg_page_accum
Definition scatterlist.h:215
struct scatterlist * sg
Definition scatterlist.h:214
unsigned int entry
Definition scatterlist.h:217
unsigned int __sg_page_index
Definition scatterlist.h:216
Definition scatterlist.h:135
struct scatterlist * sgl
Definition scatterlist.h:136
unsigned int orig_nents
Definition scatterlist.h:138
unsigned int nents
Definition scatterlist.h:137