Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  cb710/sgbuf2.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright by Michał Mirosław, 2008-2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/cb710.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) static bool sg_dwiter_next(struct sg_mapping_iter *miter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	if (sg_miter_next(miter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 		miter->consumed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static bool sg_dwiter_is_at_end(struct sg_mapping_iter *miter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	return miter->length == miter->consumed && !sg_dwiter_next(miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter *miter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	size_t len, left = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	uint32_t data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	void *addr = &data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		len = min(miter->length - miter->consumed, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		memcpy(addr, miter->addr + miter->consumed, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		miter->consumed += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		left -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		if (!left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		addr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	} while (sg_dwiter_next(miter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	memset(addr, 0, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static inline bool needs_unaligned_copy(const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return ((uintptr_t)ptr & 3) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static bool sg_dwiter_get_next_block(struct sg_mapping_iter *miter, uint32_t **ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (sg_dwiter_is_at_end(miter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	len = miter->length - miter->consumed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (likely(len >= 4 && !needs_unaligned_copy(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			miter->addr + miter->consumed))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		*ptr = miter->addr + miter->consumed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		miter->consumed += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * @miter: sg mapping iterator used for reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  *   Returns 32-bit word starting at byte pointed to by @miter@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  *   handling any alignment issues.  Bytes past the buffer's end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *   are not accessed (read) but are returned as zeroes.  @miter@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  *   is advanced by 4 bytes or to the end of buffer whichever is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  *   closer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * Context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *   Same requirements as in sg_miter_next().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *   32-bit word just read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter *miter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	uint32_t *ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (likely(sg_dwiter_get_next_block(miter, &ptr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return ptr ? *ptr : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return sg_dwiter_read_buffer(miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void sg_dwiter_write_slow(struct sg_mapping_iter *miter, uint32_t data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	size_t len, left = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	void *addr = &data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		len = min(miter->length - miter->consumed, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		memcpy(miter->addr, addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		miter->consumed += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		left -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (!left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		addr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	} while (sg_dwiter_next(miter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * @miter: sg mapping iterator used for writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * @data: data to write to sg buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  *   Writes 32-bit word starting at byte pointed to by @miter@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  *   handling any alignment issues.  Bytes which would be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  *   past the buffer's end are silently discarded. @miter@ is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  *   advanced by 4 bytes or to the end of buffer whichever is closer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * Context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  *   Same requirements as in sg_miter_next().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter *miter, uint32_t data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	uint32_t *ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (likely(sg_dwiter_get_next_block(miter, &ptr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			*ptr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		sg_dwiter_write_slow(miter, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)