1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25f5bac82SMichał Mirosław /*
35f5bac82SMichał Mirosław * cb710/sgbuf2.c
45f5bac82SMichał Mirosław *
55f5bac82SMichał Mirosław * Copyright by Michał Mirosław, 2008-2009
65f5bac82SMichał Mirosław */
75f5bac82SMichał Mirosław #include <linux/kernel.h>
85f5bac82SMichał Mirosław #include <linux/module.h>
95f5bac82SMichał Mirosław #include <linux/cb710.h>
105f5bac82SMichał Mirosław
sg_dwiter_next(struct sg_mapping_iter * miter)115f5bac82SMichał Mirosław static bool sg_dwiter_next(struct sg_mapping_iter *miter)
125f5bac82SMichał Mirosław {
135f5bac82SMichał Mirosław if (sg_miter_next(miter)) {
145f5bac82SMichał Mirosław miter->consumed = 0;
155f5bac82SMichał Mirosław return true;
165f5bac82SMichał Mirosław } else
175f5bac82SMichał Mirosław return false;
185f5bac82SMichał Mirosław }
195f5bac82SMichał Mirosław
sg_dwiter_is_at_end(struct sg_mapping_iter * miter)205f5bac82SMichał Mirosław static bool sg_dwiter_is_at_end(struct sg_mapping_iter *miter)
215f5bac82SMichał Mirosław {
225f5bac82SMichał Mirosław return miter->length == miter->consumed && !sg_dwiter_next(miter);
235f5bac82SMichał Mirosław }
245f5bac82SMichał Mirosław
sg_dwiter_read_buffer(struct sg_mapping_iter * miter)255f5bac82SMichał Mirosław static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter *miter)
265f5bac82SMichał Mirosław {
275f5bac82SMichał Mirosław size_t len, left = 4;
285f5bac82SMichał Mirosław uint32_t data;
295f5bac82SMichał Mirosław void *addr = &data;
305f5bac82SMichał Mirosław
315f5bac82SMichał Mirosław do {
325f5bac82SMichał Mirosław len = min(miter->length - miter->consumed, left);
335f5bac82SMichał Mirosław memcpy(addr, miter->addr + miter->consumed, len);
345f5bac82SMichał Mirosław miter->consumed += len;
355f5bac82SMichał Mirosław left -= len;
365f5bac82SMichał Mirosław if (!left)
375f5bac82SMichał Mirosław return data;
385f5bac82SMichał Mirosław addr += len;
395f5bac82SMichał Mirosław } while (sg_dwiter_next(miter));
405f5bac82SMichał Mirosław
415f5bac82SMichał Mirosław memset(addr, 0, left);
425f5bac82SMichał Mirosław return data;
435f5bac82SMichał Mirosław }
445f5bac82SMichał Mirosław
needs_unaligned_copy(const void * ptr)455f5bac82SMichał Mirosław static inline bool needs_unaligned_copy(const void *ptr)
465f5bac82SMichał Mirosław {
47e9e8bcb8SJames Hogan #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
485f5bac82SMichał Mirosław return false;
495f5bac82SMichał Mirosław #else
50*42641042SArnd Bergmann return ((uintptr_t)ptr & 3) != 0;
515f5bac82SMichał Mirosław #endif
525f5bac82SMichał Mirosław }
535f5bac82SMichał Mirosław
sg_dwiter_get_next_block(struct sg_mapping_iter * miter,uint32_t ** ptr)545f5bac82SMichał Mirosław static bool sg_dwiter_get_next_block(struct sg_mapping_iter *miter, uint32_t **ptr)
555f5bac82SMichał Mirosław {
565f5bac82SMichał Mirosław size_t len;
575f5bac82SMichał Mirosław
585f5bac82SMichał Mirosław if (sg_dwiter_is_at_end(miter))
595f5bac82SMichał Mirosław return true;
605f5bac82SMichał Mirosław
615f5bac82SMichał Mirosław len = miter->length - miter->consumed;
625f5bac82SMichał Mirosław
635f5bac82SMichał Mirosław if (likely(len >= 4 && !needs_unaligned_copy(
645f5bac82SMichał Mirosław miter->addr + miter->consumed))) {
655f5bac82SMichał Mirosław *ptr = miter->addr + miter->consumed;
665f5bac82SMichał Mirosław miter->consumed += 4;
675f5bac82SMichał Mirosław return true;
685f5bac82SMichał Mirosław }
695f5bac82SMichał Mirosław
705f5bac82SMichał Mirosław return false;
715f5bac82SMichał Mirosław }
725f5bac82SMichał Mirosław
735f5bac82SMichał Mirosław /**
745f5bac82SMichał Mirosław * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
755f5bac82SMichał Mirosław * @miter: sg mapping iterator used for reading
765f5bac82SMichał Mirosław *
775f5bac82SMichał Mirosław * Description:
785f5bac82SMichał Mirosław * Returns 32-bit word starting at byte pointed to by @miter@
795f5bac82SMichał Mirosław * handling any alignment issues. Bytes past the buffer's end
805f5bac82SMichał Mirosław * are not accessed (read) but are returned as zeroes. @miter@
815f5bac82SMichał Mirosław * is advanced by 4 bytes or to the end of buffer whichever is
825f5bac82SMichał Mirosław * closer.
835f5bac82SMichał Mirosław *
845f5bac82SMichał Mirosław * Context:
855f5bac82SMichał Mirosław * Same requirements as in sg_miter_next().
865f5bac82SMichał Mirosław *
875f5bac82SMichał Mirosław * Returns:
885f5bac82SMichał Mirosław * 32-bit word just read.
895f5bac82SMichał Mirosław */
cb710_sg_dwiter_read_next_block(struct sg_mapping_iter * miter)905f5bac82SMichał Mirosław uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter *miter)
915f5bac82SMichał Mirosław {
925f5bac82SMichał Mirosław uint32_t *ptr = NULL;
935f5bac82SMichał Mirosław
945f5bac82SMichał Mirosław if (likely(sg_dwiter_get_next_block(miter, &ptr)))
955f5bac82SMichał Mirosław return ptr ? *ptr : 0;
965f5bac82SMichał Mirosław
975f5bac82SMichał Mirosław return sg_dwiter_read_buffer(miter);
985f5bac82SMichał Mirosław }
995f5bac82SMichał Mirosław EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block);
1005f5bac82SMichał Mirosław
sg_dwiter_write_slow(struct sg_mapping_iter * miter,uint32_t data)1015f5bac82SMichał Mirosław static void sg_dwiter_write_slow(struct sg_mapping_iter *miter, uint32_t data)
1025f5bac82SMichał Mirosław {
1035f5bac82SMichał Mirosław size_t len, left = 4;
1045f5bac82SMichał Mirosław void *addr = &data;
1055f5bac82SMichał Mirosław
1065f5bac82SMichał Mirosław do {
1075f5bac82SMichał Mirosław len = min(miter->length - miter->consumed, left);
1085f5bac82SMichał Mirosław memcpy(miter->addr, addr, len);
1095f5bac82SMichał Mirosław miter->consumed += len;
1105f5bac82SMichał Mirosław left -= len;
1115f5bac82SMichał Mirosław if (!left)
1125f5bac82SMichał Mirosław return;
1135f5bac82SMichał Mirosław addr += len;
1145f5bac82SMichał Mirosław } while (sg_dwiter_next(miter));
1155f5bac82SMichał Mirosław }
1165f5bac82SMichał Mirosław
1175f5bac82SMichał Mirosław /**
1185f5bac82SMichał Mirosław * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
1195f5bac82SMichał Mirosław * @miter: sg mapping iterator used for writing
120dfe40cccSLee Jones * @data: data to write to sg buffer
1215f5bac82SMichał Mirosław *
1225f5bac82SMichał Mirosław * Description:
1235f5bac82SMichał Mirosław * Writes 32-bit word starting at byte pointed to by @miter@
1245f5bac82SMichał Mirosław * handling any alignment issues. Bytes which would be written
1255f5bac82SMichał Mirosław * past the buffer's end are silently discarded. @miter@ is
1265f5bac82SMichał Mirosław * advanced by 4 bytes or to the end of buffer whichever is closer.
1275f5bac82SMichał Mirosław *
1285f5bac82SMichał Mirosław * Context:
1295f5bac82SMichał Mirosław * Same requirements as in sg_miter_next().
1305f5bac82SMichał Mirosław */
cb710_sg_dwiter_write_next_block(struct sg_mapping_iter * miter,uint32_t data)1315f5bac82SMichał Mirosław void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter *miter, uint32_t data)
1325f5bac82SMichał Mirosław {
1335f5bac82SMichał Mirosław uint32_t *ptr = NULL;
1345f5bac82SMichał Mirosław
1355f5bac82SMichał Mirosław if (likely(sg_dwiter_get_next_block(miter, &ptr))) {
1365f5bac82SMichał Mirosław if (ptr)
1375f5bac82SMichał Mirosław *ptr = data;
1385f5bac82SMichał Mirosław else
1395f5bac82SMichał Mirosław return;
1405f5bac82SMichał Mirosław } else
1415f5bac82SMichał Mirosław sg_dwiter_write_slow(miter, data);
1425f5bac82SMichał Mirosław }
1435f5bac82SMichał Mirosław EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block);
1445f5bac82SMichał Mirosław
145