18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2016-2018, NXP Semiconductors
38c2ecf20Sopenharmony_ci * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#include <linux/packing.h>
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/bitops.h>
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic int get_le_offset(int offset)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	int closest_multiple_of_4;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	closest_multiple_of_4 = (offset / 4) * 4;
168c2ecf20Sopenharmony_ci	offset -= closest_multiple_of_4;
178c2ecf20Sopenharmony_ci	return closest_multiple_of_4 + (3 - offset);
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int get_reverse_lsw32_offset(int offset, size_t len)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	int closest_multiple_of_4;
238c2ecf20Sopenharmony_ci	int word_index;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	word_index = offset / 4;
268c2ecf20Sopenharmony_ci	closest_multiple_of_4 = word_index * 4;
278c2ecf20Sopenharmony_ci	offset -= closest_multiple_of_4;
288c2ecf20Sopenharmony_ci	word_index = (len / 4) - word_index - 1;
298c2ecf20Sopenharmony_ci	return word_index * 4 + offset;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic u64 bit_reverse(u64 val, unsigned int width)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	u64 new_val = 0;
358c2ecf20Sopenharmony_ci	unsigned int bit;
368c2ecf20Sopenharmony_ci	unsigned int i;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	for (i = 0; i < width; i++) {
398c2ecf20Sopenharmony_ci		bit = (val & (1 << i)) != 0;
408c2ecf20Sopenharmony_ci		new_val |= (bit << (width - i - 1));
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	return new_val;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void adjust_for_msb_right_quirk(u64 *to_write, int *box_start_bit,
468c2ecf20Sopenharmony_ci				       int *box_end_bit, u8 *box_mask)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	int box_bit_width = *box_start_bit - *box_end_bit + 1;
498c2ecf20Sopenharmony_ci	int new_box_start_bit, new_box_end_bit;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	*to_write >>= *box_end_bit;
528c2ecf20Sopenharmony_ci	*to_write = bit_reverse(*to_write, box_bit_width);
538c2ecf20Sopenharmony_ci	*to_write <<= *box_end_bit;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	new_box_end_bit   = box_bit_width - *box_start_bit - 1;
568c2ecf20Sopenharmony_ci	new_box_start_bit = box_bit_width - *box_end_bit - 1;
578c2ecf20Sopenharmony_ci	*box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit);
588c2ecf20Sopenharmony_ci	*box_start_bit = new_box_start_bit;
598c2ecf20Sopenharmony_ci	*box_end_bit   = new_box_end_bit;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/**
638c2ecf20Sopenharmony_ci * packing - Convert numbers (currently u64) between a packed and an unpacked
648c2ecf20Sopenharmony_ci *	     format. Unpacked means laid out in memory in the CPU's native
658c2ecf20Sopenharmony_ci *	     understanding of integers, while packed means anything else that
668c2ecf20Sopenharmony_ci *	     requires translation.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * @pbuf: Pointer to a buffer holding the packed value.
698c2ecf20Sopenharmony_ci * @uval: Pointer to an u64 holding the unpacked value.
708c2ecf20Sopenharmony_ci * @startbit: The index (in logical notation, compensated for quirks) where
718c2ecf20Sopenharmony_ci *	      the packed value starts within pbuf. Must be larger than, or
728c2ecf20Sopenharmony_ci *	      equal to, endbit.
738c2ecf20Sopenharmony_ci * @endbit: The index (in logical notation, compensated for quirks) where
748c2ecf20Sopenharmony_ci *	    the packed value ends within pbuf. Must be smaller than, or equal
758c2ecf20Sopenharmony_ci *	    to, startbit.
768c2ecf20Sopenharmony_ci * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
778c2ecf20Sopenharmony_ci * @op: If PACK, then uval will be treated as const pointer and copied (packed)
788c2ecf20Sopenharmony_ci *	into pbuf, between startbit and endbit.
798c2ecf20Sopenharmony_ci *	If UNPACK, then pbuf will be treated as const pointer and the logical
808c2ecf20Sopenharmony_ci *	value between startbit and endbit will be copied (unpacked) to uval.
818c2ecf20Sopenharmony_ci * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
828c2ecf20Sopenharmony_ci *	    QUIRK_MSB_ON_THE_RIGHT.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
858c2ecf20Sopenharmony_ci *	   correct usage, return code may be discarded.
868c2ecf20Sopenharmony_ci *	   If op is PACK, pbuf is modified.
878c2ecf20Sopenharmony_ci *	   If op is UNPACK, uval is modified.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ciint packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
908c2ecf20Sopenharmony_ci	    enum packing_op op, u8 quirks)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	/* Number of bits for storing "uval"
938c2ecf20Sopenharmony_ci	 * also width of the field to access in the pbuf
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	u64 value_width;
968c2ecf20Sopenharmony_ci	/* Logical byte indices corresponding to the
978c2ecf20Sopenharmony_ci	 * start and end of the field.
988c2ecf20Sopenharmony_ci	 */
998c2ecf20Sopenharmony_ci	int plogical_first_u8, plogical_last_u8, box;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* startbit is expected to be larger than endbit */
1028c2ecf20Sopenharmony_ci	if (startbit < endbit)
1038c2ecf20Sopenharmony_ci		/* Invalid function call */
1048c2ecf20Sopenharmony_ci		return -EINVAL;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	value_width = startbit - endbit + 1;
1078c2ecf20Sopenharmony_ci	if (value_width > 64)
1088c2ecf20Sopenharmony_ci		return -ERANGE;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Check if "uval" fits in "value_width" bits.
1118c2ecf20Sopenharmony_ci	 * If value_width is 64, the check will fail, but any
1128c2ecf20Sopenharmony_ci	 * 64-bit uval will surely fit.
1138c2ecf20Sopenharmony_ci	 */
1148c2ecf20Sopenharmony_ci	if (op == PACK && value_width < 64 && (*uval >= (1ull << value_width)))
1158c2ecf20Sopenharmony_ci		/* Cannot store "uval" inside "value_width" bits.
1168c2ecf20Sopenharmony_ci		 * Truncating "uval" is most certainly not desirable,
1178c2ecf20Sopenharmony_ci		 * so simply erroring out is appropriate.
1188c2ecf20Sopenharmony_ci		 */
1198c2ecf20Sopenharmony_ci		return -ERANGE;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* Initialize parameter */
1228c2ecf20Sopenharmony_ci	if (op == UNPACK)
1238c2ecf20Sopenharmony_ci		*uval = 0;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Iterate through an idealistic view of the pbuf as an u64 with
1268c2ecf20Sopenharmony_ci	 * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
1278c2ecf20Sopenharmony_ci	 * logical bit significance. "box" denotes the current logical u8.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	plogical_first_u8 = startbit / 8;
1308c2ecf20Sopenharmony_ci	plogical_last_u8  = endbit / 8;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
1338c2ecf20Sopenharmony_ci		/* Bit indices into the currently accessed 8-bit box */
1348c2ecf20Sopenharmony_ci		int box_start_bit, box_end_bit, box_addr;
1358c2ecf20Sopenharmony_ci		u8  box_mask;
1368c2ecf20Sopenharmony_ci		/* Corresponding bits from the unpacked u64 parameter */
1378c2ecf20Sopenharmony_ci		int proj_start_bit, proj_end_bit;
1388c2ecf20Sopenharmony_ci		u64 proj_mask;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		/* This u8 may need to be accessed in its entirety
1418c2ecf20Sopenharmony_ci		 * (from bit 7 to bit 0), or not, depending on the
1428c2ecf20Sopenharmony_ci		 * input arguments startbit and endbit.
1438c2ecf20Sopenharmony_ci		 */
1448c2ecf20Sopenharmony_ci		if (box == plogical_first_u8)
1458c2ecf20Sopenharmony_ci			box_start_bit = startbit % 8;
1468c2ecf20Sopenharmony_ci		else
1478c2ecf20Sopenharmony_ci			box_start_bit = 7;
1488c2ecf20Sopenharmony_ci		if (box == plogical_last_u8)
1498c2ecf20Sopenharmony_ci			box_end_bit = endbit % 8;
1508c2ecf20Sopenharmony_ci		else
1518c2ecf20Sopenharmony_ci			box_end_bit = 0;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		/* We have determined the box bit start and end.
1548c2ecf20Sopenharmony_ci		 * Now we calculate where this (masked) u8 box would fit
1558c2ecf20Sopenharmony_ci		 * in the unpacked (CPU-readable) u64 - the u8 box's
1568c2ecf20Sopenharmony_ci		 * projection onto the unpacked u64. Though the
1578c2ecf20Sopenharmony_ci		 * box is u8, the projection is u64 because it may fall
1588c2ecf20Sopenharmony_ci		 * anywhere within the unpacked u64.
1598c2ecf20Sopenharmony_ci		 */
1608c2ecf20Sopenharmony_ci		proj_start_bit = ((box * 8) + box_start_bit) - endbit;
1618c2ecf20Sopenharmony_ci		proj_end_bit   = ((box * 8) + box_end_bit) - endbit;
1628c2ecf20Sopenharmony_ci		proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
1638c2ecf20Sopenharmony_ci		box_mask  = GENMASK_ULL(box_start_bit, box_end_bit);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		/* Determine the offset of the u8 box inside the pbuf,
1668c2ecf20Sopenharmony_ci		 * adjusted for quirks. The adjusted box_addr will be used for
1678c2ecf20Sopenharmony_ci		 * effective addressing inside the pbuf (so it's not
1688c2ecf20Sopenharmony_ci		 * logical any longer).
1698c2ecf20Sopenharmony_ci		 */
1708c2ecf20Sopenharmony_ci		box_addr = pbuflen - box - 1;
1718c2ecf20Sopenharmony_ci		if (quirks & QUIRK_LITTLE_ENDIAN)
1728c2ecf20Sopenharmony_ci			box_addr = get_le_offset(box_addr);
1738c2ecf20Sopenharmony_ci		if (quirks & QUIRK_LSW32_IS_FIRST)
1748c2ecf20Sopenharmony_ci			box_addr = get_reverse_lsw32_offset(box_addr,
1758c2ecf20Sopenharmony_ci							    pbuflen);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		if (op == UNPACK) {
1788c2ecf20Sopenharmony_ci			u64 pval;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci			/* Read from pbuf, write to uval */
1818c2ecf20Sopenharmony_ci			pval = ((u8 *)pbuf)[box_addr] & box_mask;
1828c2ecf20Sopenharmony_ci			if (quirks & QUIRK_MSB_ON_THE_RIGHT)
1838c2ecf20Sopenharmony_ci				adjust_for_msb_right_quirk(&pval,
1848c2ecf20Sopenharmony_ci							   &box_start_bit,
1858c2ecf20Sopenharmony_ci							   &box_end_bit,
1868c2ecf20Sopenharmony_ci							   &box_mask);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci			pval >>= box_end_bit;
1898c2ecf20Sopenharmony_ci			pval <<= proj_end_bit;
1908c2ecf20Sopenharmony_ci			*uval &= ~proj_mask;
1918c2ecf20Sopenharmony_ci			*uval |= pval;
1928c2ecf20Sopenharmony_ci		} else {
1938c2ecf20Sopenharmony_ci			u64 pval;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci			/* Write to pbuf, read from uval */
1968c2ecf20Sopenharmony_ci			pval = (*uval) & proj_mask;
1978c2ecf20Sopenharmony_ci			pval >>= proj_end_bit;
1988c2ecf20Sopenharmony_ci			if (quirks & QUIRK_MSB_ON_THE_RIGHT)
1998c2ecf20Sopenharmony_ci				adjust_for_msb_right_quirk(&pval,
2008c2ecf20Sopenharmony_ci							   &box_start_bit,
2018c2ecf20Sopenharmony_ci							   &box_end_bit,
2028c2ecf20Sopenharmony_ci							   &box_mask);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci			pval <<= box_end_bit;
2058c2ecf20Sopenharmony_ci			((u8 *)pbuf)[box_addr] &= ~box_mask;
2068c2ecf20Sopenharmony_ci			((u8 *)pbuf)[box_addr] |= pval;
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(packing);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2148c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic bitfield packing and unpacking");
215