4 * Copyright by Michał Mirosław, 2008-2009
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/cb710.h>
14 static bool sg_dwiter_next(struct sg_mapping_iter
*miter
)
16 if (sg_miter_next(miter
)) {
23 static bool sg_dwiter_is_at_end(struct sg_mapping_iter
*miter
)
25 return miter
->length
== miter
->consumed
&& !sg_dwiter_next(miter
);
28 static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter
*miter
)
35 len
= min(miter
->length
- miter
->consumed
, left
);
36 memcpy(addr
, miter
->addr
+ miter
->consumed
, len
);
37 miter
->consumed
+= len
;
42 } while (sg_dwiter_next(miter
));
44 memset(addr
, 0, left
);
48 static inline bool needs_unaligned_copy(const void *ptr
)
50 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
53 return ((ptr
- NULL
) & 3) != 0;
57 static bool sg_dwiter_get_next_block(struct sg_mapping_iter
*miter
, uint32_t **ptr
)
61 if (sg_dwiter_is_at_end(miter
))
64 len
= miter
->length
- miter
->consumed
;
66 if (likely(len
>= 4 && !needs_unaligned_copy(
67 miter
->addr
+ miter
->consumed
))) {
68 *ptr
= miter
->addr
+ miter
->consumed
;
77 * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
78 * @miter: sg mapping iterator used for reading
81 * Returns 32-bit word starting at byte pointed to by @miter@
82 * handling any alignment issues. Bytes past the buffer's end
83 * are not accessed (read) but are returned as zeroes. @miter@
84 * is advanced by 4 bytes or to the end of buffer whichever is
88 * Same requirements as in sg_miter_next().
91 * 32-bit word just read.
93 uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter
*miter
)
97 if (likely(sg_dwiter_get_next_block(miter
, &ptr
)))
98 return ptr
? *ptr
: 0;
100 return sg_dwiter_read_buffer(miter
);
102 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block
);
104 static void sg_dwiter_write_slow(struct sg_mapping_iter
*miter
, uint32_t data
)
106 size_t len
, left
= 4;
110 len
= min(miter
->length
- miter
->consumed
, left
);
111 memcpy(miter
->addr
, addr
, len
);
112 miter
->consumed
+= len
;
117 } while (sg_dwiter_next(miter
));
121 * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
122 * @miter: sg mapping iterator used for writing
125 * Writes 32-bit word starting at byte pointed to by @miter@
126 * handling any alignment issues. Bytes which would be written
127 * past the buffer's end are silently discarded. @miter@ is
128 * advanced by 4 bytes or to the end of buffer whichever is closer.
131 * Same requirements as in sg_miter_next().
133 void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter
*miter
, uint32_t data
)
135 uint32_t *ptr
= NULL
;
137 if (likely(sg_dwiter_get_next_block(miter
, &ptr
))) {
143 sg_dwiter_write_slow(miter
, data
);
145 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block
);