162306a36Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
262306a36Sopenharmony_ci/* Copyright 2016-2018 NXP
362306a36Sopenharmony_ci * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#include <linux/packing.h>
662306a36Sopenharmony_ci#include <linux/module.h>
762306a36Sopenharmony_ci#include <linux/bitops.h>
862306a36Sopenharmony_ci#include <linux/errno.h>
962306a36Sopenharmony_ci#include <linux/types.h>
1062306a36Sopenharmony_ci#include <linux/bitrev.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistatic int get_le_offset(int offset)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	int closest_multiple_of_4;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	closest_multiple_of_4 = (offset / 4) * 4;
1762306a36Sopenharmony_ci	offset -= closest_multiple_of_4;
1862306a36Sopenharmony_ci	return closest_multiple_of_4 + (3 - offset);
1962306a36Sopenharmony_ci}
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistatic int get_reverse_lsw32_offset(int offset, size_t len)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	int closest_multiple_of_4;
2462306a36Sopenharmony_ci	int word_index;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	word_index = offset / 4;
2762306a36Sopenharmony_ci	closest_multiple_of_4 = word_index * 4;
2862306a36Sopenharmony_ci	offset -= closest_multiple_of_4;
2962306a36Sopenharmony_ci	word_index = (len / 4) - word_index - 1;
3062306a36Sopenharmony_ci	return word_index * 4 + offset;
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic void adjust_for_msb_right_quirk(u64 *to_write, int *box_start_bit,
3462306a36Sopenharmony_ci				       int *box_end_bit, u8 *box_mask)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	int box_bit_width = *box_start_bit - *box_end_bit + 1;
3762306a36Sopenharmony_ci	int new_box_start_bit, new_box_end_bit;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	*to_write >>= *box_end_bit;
4062306a36Sopenharmony_ci	*to_write = bitrev8(*to_write) >> (8 - box_bit_width);
4162306a36Sopenharmony_ci	*to_write <<= *box_end_bit;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	new_box_end_bit   = box_bit_width - *box_start_bit - 1;
4462306a36Sopenharmony_ci	new_box_start_bit = box_bit_width - *box_end_bit - 1;
4562306a36Sopenharmony_ci	*box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit);
4662306a36Sopenharmony_ci	*box_start_bit = new_box_start_bit;
4762306a36Sopenharmony_ci	*box_end_bit   = new_box_end_bit;
4862306a36Sopenharmony_ci}
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/**
5162306a36Sopenharmony_ci * packing - Convert numbers (currently u64) between a packed and an unpacked
5262306a36Sopenharmony_ci *	     format. Unpacked means laid out in memory in the CPU's native
5362306a36Sopenharmony_ci *	     understanding of integers, while packed means anything else that
5462306a36Sopenharmony_ci *	     requires translation.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * @pbuf: Pointer to a buffer holding the packed value.
5762306a36Sopenharmony_ci * @uval: Pointer to an u64 holding the unpacked value.
5862306a36Sopenharmony_ci * @startbit: The index (in logical notation, compensated for quirks) where
5962306a36Sopenharmony_ci *	      the packed value starts within pbuf. Must be larger than, or
6062306a36Sopenharmony_ci *	      equal to, endbit.
6162306a36Sopenharmony_ci * @endbit: The index (in logical notation, compensated for quirks) where
6262306a36Sopenharmony_ci *	    the packed value ends within pbuf. Must be smaller than, or equal
6362306a36Sopenharmony_ci *	    to, startbit.
6462306a36Sopenharmony_ci * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
6562306a36Sopenharmony_ci * @op: If PACK, then uval will be treated as const pointer and copied (packed)
6662306a36Sopenharmony_ci *	into pbuf, between startbit and endbit.
6762306a36Sopenharmony_ci *	If UNPACK, then pbuf will be treated as const pointer and the logical
6862306a36Sopenharmony_ci *	value between startbit and endbit will be copied (unpacked) to uval.
6962306a36Sopenharmony_ci * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
7062306a36Sopenharmony_ci *	    QUIRK_MSB_ON_THE_RIGHT.
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
7362306a36Sopenharmony_ci *	   correct usage, return code may be discarded.
7462306a36Sopenharmony_ci *	   If op is PACK, pbuf is modified.
7562306a36Sopenharmony_ci *	   If op is UNPACK, uval is modified.
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_ciint packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
7862306a36Sopenharmony_ci	    enum packing_op op, u8 quirks)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	/* Number of bits for storing "uval"
8162306a36Sopenharmony_ci	 * also width of the field to access in the pbuf
8262306a36Sopenharmony_ci	 */
8362306a36Sopenharmony_ci	u64 value_width;
8462306a36Sopenharmony_ci	/* Logical byte indices corresponding to the
8562306a36Sopenharmony_ci	 * start and end of the field.
8662306a36Sopenharmony_ci	 */
8762306a36Sopenharmony_ci	int plogical_first_u8, plogical_last_u8, box;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	/* startbit is expected to be larger than endbit */
9062306a36Sopenharmony_ci	if (startbit < endbit)
9162306a36Sopenharmony_ci		/* Invalid function call */
9262306a36Sopenharmony_ci		return -EINVAL;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	value_width = startbit - endbit + 1;
9562306a36Sopenharmony_ci	if (value_width > 64)
9662306a36Sopenharmony_ci		return -ERANGE;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* Check if "uval" fits in "value_width" bits.
9962306a36Sopenharmony_ci	 * If value_width is 64, the check will fail, but any
10062306a36Sopenharmony_ci	 * 64-bit uval will surely fit.
10162306a36Sopenharmony_ci	 */
10262306a36Sopenharmony_ci	if (op == PACK && value_width < 64 && (*uval >= (1ull << value_width)))
10362306a36Sopenharmony_ci		/* Cannot store "uval" inside "value_width" bits.
10462306a36Sopenharmony_ci		 * Truncating "uval" is most certainly not desirable,
10562306a36Sopenharmony_ci		 * so simply erroring out is appropriate.
10662306a36Sopenharmony_ci		 */
10762306a36Sopenharmony_ci		return -ERANGE;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* Initialize parameter */
11062306a36Sopenharmony_ci	if (op == UNPACK)
11162306a36Sopenharmony_ci		*uval = 0;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/* Iterate through an idealistic view of the pbuf as an u64 with
11462306a36Sopenharmony_ci	 * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
11562306a36Sopenharmony_ci	 * logical bit significance. "box" denotes the current logical u8.
11662306a36Sopenharmony_ci	 */
11762306a36Sopenharmony_ci	plogical_first_u8 = startbit / 8;
11862306a36Sopenharmony_ci	plogical_last_u8  = endbit / 8;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
12162306a36Sopenharmony_ci		/* Bit indices into the currently accessed 8-bit box */
12262306a36Sopenharmony_ci		int box_start_bit, box_end_bit, box_addr;
12362306a36Sopenharmony_ci		u8  box_mask;
12462306a36Sopenharmony_ci		/* Corresponding bits from the unpacked u64 parameter */
12562306a36Sopenharmony_ci		int proj_start_bit, proj_end_bit;
12662306a36Sopenharmony_ci		u64 proj_mask;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci		/* This u8 may need to be accessed in its entirety
12962306a36Sopenharmony_ci		 * (from bit 7 to bit 0), or not, depending on the
13062306a36Sopenharmony_ci		 * input arguments startbit and endbit.
13162306a36Sopenharmony_ci		 */
13262306a36Sopenharmony_ci		if (box == plogical_first_u8)
13362306a36Sopenharmony_ci			box_start_bit = startbit % 8;
13462306a36Sopenharmony_ci		else
13562306a36Sopenharmony_ci			box_start_bit = 7;
13662306a36Sopenharmony_ci		if (box == plogical_last_u8)
13762306a36Sopenharmony_ci			box_end_bit = endbit % 8;
13862306a36Sopenharmony_ci		else
13962306a36Sopenharmony_ci			box_end_bit = 0;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci		/* We have determined the box bit start and end.
14262306a36Sopenharmony_ci		 * Now we calculate where this (masked) u8 box would fit
14362306a36Sopenharmony_ci		 * in the unpacked (CPU-readable) u64 - the u8 box's
14462306a36Sopenharmony_ci		 * projection onto the unpacked u64. Though the
14562306a36Sopenharmony_ci		 * box is u8, the projection is u64 because it may fall
14662306a36Sopenharmony_ci		 * anywhere within the unpacked u64.
14762306a36Sopenharmony_ci		 */
14862306a36Sopenharmony_ci		proj_start_bit = ((box * 8) + box_start_bit) - endbit;
14962306a36Sopenharmony_ci		proj_end_bit   = ((box * 8) + box_end_bit) - endbit;
15062306a36Sopenharmony_ci		proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
15162306a36Sopenharmony_ci		box_mask  = GENMASK_ULL(box_start_bit, box_end_bit);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci		/* Determine the offset of the u8 box inside the pbuf,
15462306a36Sopenharmony_ci		 * adjusted for quirks. The adjusted box_addr will be used for
15562306a36Sopenharmony_ci		 * effective addressing inside the pbuf (so it's not
15662306a36Sopenharmony_ci		 * logical any longer).
15762306a36Sopenharmony_ci		 */
15862306a36Sopenharmony_ci		box_addr = pbuflen - box - 1;
15962306a36Sopenharmony_ci		if (quirks & QUIRK_LITTLE_ENDIAN)
16062306a36Sopenharmony_ci			box_addr = get_le_offset(box_addr);
16162306a36Sopenharmony_ci		if (quirks & QUIRK_LSW32_IS_FIRST)
16262306a36Sopenharmony_ci			box_addr = get_reverse_lsw32_offset(box_addr,
16362306a36Sopenharmony_ci							    pbuflen);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci		if (op == UNPACK) {
16662306a36Sopenharmony_ci			u64 pval;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci			/* Read from pbuf, write to uval */
16962306a36Sopenharmony_ci			pval = ((u8 *)pbuf)[box_addr] & box_mask;
17062306a36Sopenharmony_ci			if (quirks & QUIRK_MSB_ON_THE_RIGHT)
17162306a36Sopenharmony_ci				adjust_for_msb_right_quirk(&pval,
17262306a36Sopenharmony_ci							   &box_start_bit,
17362306a36Sopenharmony_ci							   &box_end_bit,
17462306a36Sopenharmony_ci							   &box_mask);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci			pval >>= box_end_bit;
17762306a36Sopenharmony_ci			pval <<= proj_end_bit;
17862306a36Sopenharmony_ci			*uval &= ~proj_mask;
17962306a36Sopenharmony_ci			*uval |= pval;
18062306a36Sopenharmony_ci		} else {
18162306a36Sopenharmony_ci			u64 pval;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci			/* Write to pbuf, read from uval */
18462306a36Sopenharmony_ci			pval = (*uval) & proj_mask;
18562306a36Sopenharmony_ci			pval >>= proj_end_bit;
18662306a36Sopenharmony_ci			if (quirks & QUIRK_MSB_ON_THE_RIGHT)
18762306a36Sopenharmony_ci				adjust_for_msb_right_quirk(&pval,
18862306a36Sopenharmony_ci							   &box_start_bit,
18962306a36Sopenharmony_ci							   &box_end_bit,
19062306a36Sopenharmony_ci							   &box_mask);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci			pval <<= box_end_bit;
19362306a36Sopenharmony_ci			((u8 *)pbuf)[box_addr] &= ~box_mask;
19462306a36Sopenharmony_ci			((u8 *)pbuf)[box_addr] |= pval;
19562306a36Sopenharmony_ci		}
19662306a36Sopenharmony_ci	}
19762306a36Sopenharmony_ci	return 0;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ciEXPORT_SYMBOL(packing);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ciMODULE_DESCRIPTION("Generic bitfield packing and unpacking");
202