1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright by Michał Mirosław, 2008-2009
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/cb710.h>
11 static bool sg_dwiter_next(struct sg_mapping_iter
*miter
)
13 if (sg_miter_next(miter
)) {
20 static bool sg_dwiter_is_at_end(struct sg_mapping_iter
*miter
)
22 return miter
->length
== miter
->consumed
&& !sg_dwiter_next(miter
);
25 static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter
*miter
)
32 len
= min(miter
->length
- miter
->consumed
, left
);
33 memcpy(addr
, miter
->addr
+ miter
->consumed
, len
);
34 miter
->consumed
+= len
;
39 } while (sg_dwiter_next(miter
));
41 memset(addr
, 0, left
);
45 static inline bool needs_unaligned_copy(const void *ptr
)
47 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
50 return ((ptr
- NULL
) & 3) != 0;
54 static bool sg_dwiter_get_next_block(struct sg_mapping_iter
*miter
, uint32_t **ptr
)
58 if (sg_dwiter_is_at_end(miter
))
61 len
= miter
->length
- miter
->consumed
;
63 if (likely(len
>= 4 && !needs_unaligned_copy(
64 miter
->addr
+ miter
->consumed
))) {
65 *ptr
= miter
->addr
+ miter
->consumed
;
74 * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
75 * @miter: sg mapping iterator used for reading
78 * Returns 32-bit word starting at byte pointed to by @miter@
79 * handling any alignment issues. Bytes past the buffer's end
80 * are not accessed (read) but are returned as zeroes. @miter@
81 * is advanced by 4 bytes or to the end of buffer whichever is
85 * Same requirements as in sg_miter_next().
88 * 32-bit word just read.
90 uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter
*miter
)
94 if (likely(sg_dwiter_get_next_block(miter
, &ptr
)))
95 return ptr
? *ptr
: 0;
97 return sg_dwiter_read_buffer(miter
);
99 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block
);
101 static void sg_dwiter_write_slow(struct sg_mapping_iter
*miter
, uint32_t data
)
103 size_t len
, left
= 4;
107 len
= min(miter
->length
- miter
->consumed
, left
);
108 memcpy(miter
->addr
, addr
, len
);
109 miter
->consumed
+= len
;
114 } while (sg_dwiter_next(miter
));
118 * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
119 * @miter: sg mapping iterator used for writing
120 * @data: data to write to sg buffer
123 * Writes 32-bit word starting at byte pointed to by @miter@
124 * handling any alignment issues. Bytes which would be written
125 * past the buffer's end are silently discarded. @miter@ is
126 * advanced by 4 bytes or to the end of buffer whichever is closer.
129 * Same requirements as in sg_miter_next().
131 void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter
*miter
, uint32_t data
)
133 uint32_t *ptr
= NULL
;
135 if (likely(sg_dwiter_get_next_block(miter
, &ptr
))) {
141 sg_dwiter_write_slow(miter
, data
);
143 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block
);