1554aae35SVladimir Oltean // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2*3c9cfb52SVladimir Oltean /* Copyright 2016-2018 NXP 3554aae35SVladimir Oltean * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> 4554aae35SVladimir Oltean */ 5554aae35SVladimir Oltean #include <linux/packing.h> 6554aae35SVladimir Oltean #include <linux/module.h> 7554aae35SVladimir Oltean #include <linux/bitops.h> 8554aae35SVladimir Oltean #include <linux/errno.h> 9554aae35SVladimir Oltean #include <linux/types.h> 10554aae35SVladimir Oltean 11554aae35SVladimir Oltean static int get_le_offset(int offset) 12554aae35SVladimir Oltean { 13554aae35SVladimir Oltean int closest_multiple_of_4; 14554aae35SVladimir Oltean 15554aae35SVladimir Oltean closest_multiple_of_4 = (offset / 4) * 4; 16554aae35SVladimir Oltean offset -= closest_multiple_of_4; 17554aae35SVladimir Oltean return closest_multiple_of_4 + (3 - offset); 18554aae35SVladimir Oltean } 19554aae35SVladimir Oltean 20554aae35SVladimir Oltean static int get_reverse_lsw32_offset(int offset, size_t len) 21554aae35SVladimir Oltean { 22554aae35SVladimir Oltean int closest_multiple_of_4; 23554aae35SVladimir Oltean int word_index; 24554aae35SVladimir Oltean 25554aae35SVladimir Oltean word_index = offset / 4; 26554aae35SVladimir Oltean closest_multiple_of_4 = word_index * 4; 27554aae35SVladimir Oltean offset -= closest_multiple_of_4; 28554aae35SVladimir Oltean word_index = (len / 4) - word_index - 1; 29554aae35SVladimir Oltean return word_index * 4 + offset; 30554aae35SVladimir Oltean } 31554aae35SVladimir Oltean 32554aae35SVladimir Oltean static u64 bit_reverse(u64 val, unsigned int width) 33554aae35SVladimir Oltean { 34554aae35SVladimir Oltean u64 new_val = 0; 35554aae35SVladimir Oltean unsigned int bit; 36554aae35SVladimir Oltean unsigned int i; 37554aae35SVladimir Oltean 38554aae35SVladimir Oltean for (i = 0; i < width; i++) { 39554aae35SVladimir Oltean bit = (val & (1 << i)) != 0; 40554aae35SVladimir Oltean new_val |= (bit << (width - i - 1)); 41554aae35SVladimir Oltean } 42554aae35SVladimir Oltean return new_val; 43554aae35SVladimir Oltean } 44554aae35SVladimir Oltean 45554aae35SVladimir Oltean static void adjust_for_msb_right_quirk(u64 *to_write, int *box_start_bit, 46554aae35SVladimir Oltean int *box_end_bit, u8 *box_mask) 47554aae35SVladimir Oltean { 48554aae35SVladimir Oltean int box_bit_width = *box_start_bit - *box_end_bit + 1; 49554aae35SVladimir Oltean int new_box_start_bit, new_box_end_bit; 50554aae35SVladimir Oltean 51554aae35SVladimir Oltean *to_write >>= *box_end_bit; 52554aae35SVladimir Oltean *to_write = bit_reverse(*to_write, box_bit_width); 53554aae35SVladimir Oltean *to_write <<= *box_end_bit; 54554aae35SVladimir Oltean 55554aae35SVladimir Oltean new_box_end_bit = box_bit_width - *box_start_bit - 1; 56554aae35SVladimir Oltean new_box_start_bit = box_bit_width - *box_end_bit - 1; 57554aae35SVladimir Oltean *box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit); 58554aae35SVladimir Oltean *box_start_bit = new_box_start_bit; 59554aae35SVladimir Oltean *box_end_bit = new_box_end_bit; 60554aae35SVladimir Oltean } 61554aae35SVladimir Oltean 62554aae35SVladimir Oltean /** 63554aae35SVladimir Oltean * packing - Convert numbers (currently u64) between a packed and an unpacked 64554aae35SVladimir Oltean * format. Unpacked means laid out in memory in the CPU's native 65554aae35SVladimir Oltean * understanding of integers, while packed means anything else that 66554aae35SVladimir Oltean * requires translation. 67554aae35SVladimir Oltean * 68554aae35SVladimir Oltean * @pbuf: Pointer to a buffer holding the packed value. 69554aae35SVladimir Oltean * @uval: Pointer to an u64 holding the unpacked value. 70554aae35SVladimir Oltean * @startbit: The index (in logical notation, compensated for quirks) where 71554aae35SVladimir Oltean * the packed value starts within pbuf. Must be larger than, or 72554aae35SVladimir Oltean * equal to, endbit. 73554aae35SVladimir Oltean * @endbit: The index (in logical notation, compensated for quirks) where 74554aae35SVladimir Oltean * the packed value ends within pbuf. Must be smaller than, or equal 75554aae35SVladimir Oltean * to, startbit. 767dea927fSVladimir Oltean * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf. 77554aae35SVladimir Oltean * @op: If PACK, then uval will be treated as const pointer and copied (packed) 78554aae35SVladimir Oltean * into pbuf, between startbit and endbit. 79554aae35SVladimir Oltean * If UNPACK, then pbuf will be treated as const pointer and the logical 80554aae35SVladimir Oltean * value between startbit and endbit will be copied (unpacked) to uval. 81554aae35SVladimir Oltean * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and 82554aae35SVladimir Oltean * QUIRK_MSB_ON_THE_RIGHT. 83554aae35SVladimir Oltean * 84554aae35SVladimir Oltean * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming 85554aae35SVladimir Oltean * correct usage, return code may be discarded. 86554aae35SVladimir Oltean * If op is PACK, pbuf is modified. 87554aae35SVladimir Oltean * If op is UNPACK, uval is modified. 88554aae35SVladimir Oltean */ 89554aae35SVladimir Oltean int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen, 90554aae35SVladimir Oltean enum packing_op op, u8 quirks) 91554aae35SVladimir Oltean { 92554aae35SVladimir Oltean /* Number of bits for storing "uval" 93554aae35SVladimir Oltean * also width of the field to access in the pbuf 94554aae35SVladimir Oltean */ 95554aae35SVladimir Oltean u64 value_width; 96554aae35SVladimir Oltean /* Logical byte indices corresponding to the 97554aae35SVladimir Oltean * start and end of the field. 98554aae35SVladimir Oltean */ 99554aae35SVladimir Oltean int plogical_first_u8, plogical_last_u8, box; 100554aae35SVladimir Oltean 101554aae35SVladimir Oltean /* startbit is expected to be larger than endbit */ 102554aae35SVladimir Oltean if (startbit < endbit) 103554aae35SVladimir Oltean /* Invalid function call */ 104554aae35SVladimir Oltean return -EINVAL; 105554aae35SVladimir Oltean 106554aae35SVladimir Oltean value_width = startbit - endbit + 1; 107554aae35SVladimir Oltean if (value_width > 64) 108554aae35SVladimir Oltean return -ERANGE; 109554aae35SVladimir Oltean 110554aae35SVladimir Oltean /* Check if "uval" fits in "value_width" bits. 111554aae35SVladimir Oltean * If value_width is 64, the check will fail, but any 112554aae35SVladimir Oltean * 64-bit uval will surely fit. 113554aae35SVladimir Oltean */ 114554aae35SVladimir Oltean if (op == PACK && value_width < 64 && (*uval >= (1ull << value_width))) 115554aae35SVladimir Oltean /* Cannot store "uval" inside "value_width" bits. 116554aae35SVladimir Oltean * Truncating "uval" is most certainly not desirable, 117554aae35SVladimir Oltean * so simply erroring out is appropriate. 118554aae35SVladimir Oltean */ 119554aae35SVladimir Oltean return -ERANGE; 120554aae35SVladimir Oltean 121554aae35SVladimir Oltean /* Initialize parameter */ 122554aae35SVladimir Oltean if (op == UNPACK) 123554aae35SVladimir Oltean *uval = 0; 124554aae35SVladimir Oltean 125554aae35SVladimir Oltean /* Iterate through an idealistic view of the pbuf as an u64 with 126554aae35SVladimir Oltean * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low 127554aae35SVladimir Oltean * logical bit significance. "box" denotes the current logical u8. 128554aae35SVladimir Oltean */ 129554aae35SVladimir Oltean plogical_first_u8 = startbit / 8; 130554aae35SVladimir Oltean plogical_last_u8 = endbit / 8; 131554aae35SVladimir Oltean 132554aae35SVladimir Oltean for (box = plogical_first_u8; box >= plogical_last_u8; box--) { 133554aae35SVladimir Oltean /* Bit indices into the currently accessed 8-bit box */ 134554aae35SVladimir Oltean int box_start_bit, box_end_bit, box_addr; 135554aae35SVladimir Oltean u8 box_mask; 136554aae35SVladimir Oltean /* Corresponding bits from the unpacked u64 parameter */ 137554aae35SVladimir Oltean int proj_start_bit, proj_end_bit; 138554aae35SVladimir Oltean u64 proj_mask; 139554aae35SVladimir Oltean 140554aae35SVladimir Oltean /* This u8 may need to be accessed in its entirety 141554aae35SVladimir Oltean * (from bit 7 to bit 0), or not, depending on the 142554aae35SVladimir Oltean * input arguments startbit and endbit. 143554aae35SVladimir Oltean */ 144554aae35SVladimir Oltean if (box == plogical_first_u8) 145554aae35SVladimir Oltean box_start_bit = startbit % 8; 146554aae35SVladimir Oltean else 147554aae35SVladimir Oltean box_start_bit = 7; 148554aae35SVladimir Oltean if (box == plogical_last_u8) 149554aae35SVladimir Oltean box_end_bit = endbit % 8; 150554aae35SVladimir Oltean else 151554aae35SVladimir Oltean box_end_bit = 0; 152554aae35SVladimir Oltean 153554aae35SVladimir Oltean /* We have determined the box bit start and end. 154554aae35SVladimir Oltean * Now we calculate where this (masked) u8 box would fit 155554aae35SVladimir Oltean * in the unpacked (CPU-readable) u64 - the u8 box's 156554aae35SVladimir Oltean * projection onto the unpacked u64. Though the 157554aae35SVladimir Oltean * box is u8, the projection is u64 because it may fall 158554aae35SVladimir Oltean * anywhere within the unpacked u64. 159554aae35SVladimir Oltean */ 160554aae35SVladimir Oltean proj_start_bit = ((box * 8) + box_start_bit) - endbit; 161554aae35SVladimir Oltean proj_end_bit = ((box * 8) + box_end_bit) - endbit; 162554aae35SVladimir Oltean proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit); 163554aae35SVladimir Oltean box_mask = GENMASK_ULL(box_start_bit, box_end_bit); 164554aae35SVladimir Oltean 165554aae35SVladimir Oltean /* Determine the offset of the u8 box inside the pbuf, 166554aae35SVladimir Oltean * adjusted for quirks. The adjusted box_addr will be used for 167554aae35SVladimir Oltean * effective addressing inside the pbuf (so it's not 168554aae35SVladimir Oltean * logical any longer). 169554aae35SVladimir Oltean */ 170554aae35SVladimir Oltean box_addr = pbuflen - box - 1; 171554aae35SVladimir Oltean if (quirks & QUIRK_LITTLE_ENDIAN) 172554aae35SVladimir Oltean box_addr = get_le_offset(box_addr); 173554aae35SVladimir Oltean if (quirks & QUIRK_LSW32_IS_FIRST) 174554aae35SVladimir Oltean box_addr = get_reverse_lsw32_offset(box_addr, 175554aae35SVladimir Oltean pbuflen); 176554aae35SVladimir Oltean 177554aae35SVladimir Oltean if (op == UNPACK) { 178554aae35SVladimir Oltean u64 pval; 179554aae35SVladimir Oltean 180554aae35SVladimir Oltean /* Read from pbuf, write to uval */ 181554aae35SVladimir Oltean pval = ((u8 *)pbuf)[box_addr] & box_mask; 182554aae35SVladimir Oltean if (quirks & QUIRK_MSB_ON_THE_RIGHT) 183554aae35SVladimir Oltean adjust_for_msb_right_quirk(&pval, 184554aae35SVladimir Oltean &box_start_bit, 185554aae35SVladimir Oltean &box_end_bit, 186554aae35SVladimir Oltean &box_mask); 187554aae35SVladimir Oltean 188554aae35SVladimir Oltean pval >>= box_end_bit; 189554aae35SVladimir Oltean pval <<= proj_end_bit; 190554aae35SVladimir Oltean *uval &= ~proj_mask; 191554aae35SVladimir Oltean *uval |= pval; 192554aae35SVladimir Oltean } else { 193554aae35SVladimir Oltean u64 pval; 194554aae35SVladimir Oltean 195554aae35SVladimir Oltean /* Write to pbuf, read from uval */ 196554aae35SVladimir Oltean pval = (*uval) & proj_mask; 197554aae35SVladimir Oltean pval >>= proj_end_bit; 198554aae35SVladimir Oltean if (quirks & QUIRK_MSB_ON_THE_RIGHT) 199554aae35SVladimir Oltean adjust_for_msb_right_quirk(&pval, 200554aae35SVladimir Oltean &box_start_bit, 201554aae35SVladimir Oltean &box_end_bit, 202554aae35SVladimir Oltean &box_mask); 203554aae35SVladimir Oltean 204554aae35SVladimir Oltean pval <<= box_end_bit; 205554aae35SVladimir Oltean ((u8 *)pbuf)[box_addr] &= ~box_mask; 206554aae35SVladimir Oltean ((u8 *)pbuf)[box_addr] |= pval; 207554aae35SVladimir Oltean } 208554aae35SVladimir Oltean } 209554aae35SVladimir Oltean return 0; 210554aae35SVladimir Oltean } 211554aae35SVladimir Oltean EXPORT_SYMBOL(packing); 212554aae35SVladimir Oltean 213554aae35SVladimir Oltean MODULE_LICENSE("GPL v2"); 214554aae35SVladimir Oltean MODULE_DESCRIPTION("Generic bitfield packing and unpacking"); 215