* [PATCH v3 0/5] i3c: introduce and use generic parity helper
@ 2025-01-06 9:27 Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 1/5] bitops: add generic parity calculation for u8 Wolfram Sang
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang, Alexandre Belloni,
Guenter Roeck, Jean Delvare, linux-hwmon, Przemysław Gaj,
Rasmus Villemoes, Yury Norov
Changes since v2:
* fixed commit messages from 'get_parity8' to 'parity8'
* added tag from Jarkko (thanks!)
* rebased to 6.13-rc6
All acks from bitmap.h and HWMON maintainers are already included.
Old coverletter follows:
I am currently working on upstreaming another I3C controller driver. As
many others, it needs to ensure odd parity for a dynamically assigned
address. The BSP version of the driver implemented a custom parity
algorithm. Wondering why we don't have a generic helper for this in the
kernel, I found that many I3C controller drivers all implement their
version of handling parity.
So, I sent out an RFC[1] moving the efficient implementation of the
SPD5118 driver to a generic location. The series was well received, but
the path for upstream was not clear. Because I need the implementation
for my I3C controller driver and I3C is a prominent user of this new
function, I got the idea of converting the existing I3C drivers and
resend the series, suggesting this all goes upstream via I3C.
A build-tested branch is here:
git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git renesas/i3c/get_parity
Looking forward to comments...
[1] https://lore.kernel.org/all/20241214085833.8695-1-wsa+renesas@sang-engineering.com/
Wolfram Sang (5):
bitops: add generic parity calculation for u8
hwmon: (spd5118) Use generic parity calculation
i3c: dw: use parity8 helper instead of open coding it
i3c: mipi-i3c-hci: use parity8 helper instead of open coding it
i3c: cdns: use parity8 helper instead of open coding it
drivers/hwmon/spd5118.c | 8 +-----
drivers/i3c/master/dw-i3c-master.c | 14 +++--------
drivers/i3c/master/i3c-master-cdns.c | 3 +--
drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 11 +--------
include/linux/bitops.h | 31 ++++++++++++++++++++++++
5 files changed, 37 insertions(+), 30 deletions(-)
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 1/5] bitops: add generic parity calculation for u8
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
@ 2025-01-06 9:27 ` Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 2/5] hwmon: (spd5118) Use generic parity calculation Wolfram Sang
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang, Guenter Roeck,
Geert Uytterhoeven, Yury Norov, Kuan-Wei Chiu, Rasmus Villemoes
There are multiple open coded implementations for getting the parity of
a byte in the kernel, even using different approaches. Take the pretty
efficient version from SPD5118 driver and make it generally available by
putting it into the bitops header. As long as there is just one parity
calculation helper, the creation of a distinct 'parity.h' header was
discarded. Also, the usage of hweight8() for architectures having a
popcnt instruction is postponed until a use case within hot paths is
desired. The motivation for this patch is the frequent use of odd parity
in the I3C specification and to simplify drivers there.
Changes compared to the original SPD5118 version are the addition of
kernel documentation, switching the return type from bool to int, and
renaming the argument of the function.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Acked-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Tested-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
include/linux/bitops.h | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index ba35bbf07798..c1cb53cf2f0f 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -229,6 +229,37 @@ static inline int get_count_order_long(unsigned long l)
return (int)fls_long(--l);
}
+/**
+ * parity8 - get the parity of an u8 value
+ * @value: the value to be examined
+ *
+ * Determine the parity of the u8 argument.
+ *
+ * Returns:
+ * 0 for even parity, 1 for odd parity
+ *
+ * Note: This function informs you about the current parity. Example to bail
+ * out when parity is odd:
+ *
+ * if (parity8(val) == 1)
+ * return -EBADMSG;
+ *
+ * If you need to calculate a parity bit, you need to draw the conclusion from
+ * this result yourself. Example to enforce odd parity, parity bit is bit 7:
+ *
+ * if (parity8(val) == 0)
+ * val ^= BIT(7);
+ */
+static inline int parity8(u8 val)
+{
+ /*
+ * One explanation of this algorithm:
+ * https://funloop.org/codex/problem/parity/README.html
+ */
+ val ^= val >> 4;
+ return (0x6996 >> (val & 0xf)) & 1;
+}
+
/**
* __ffs64 - find first set bit in a 64 bit word
* @word: The 64 bit word
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 2/5] hwmon: (spd5118) Use generic parity calculation
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 1/5] bitops: add generic parity calculation for u8 Wolfram Sang
@ 2025-01-06 9:27 ` Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 3/5] i3c: dw: use parity8 helper instead of open coding it Wolfram Sang
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang, Guenter Roeck,
Geert Uytterhoeven, Kuan-Wei Chiu, Jean Delvare, linux-hwmon
Make use of the new generic helper for calculating the parity.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
drivers/hwmon/spd5118.c | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/drivers/hwmon/spd5118.c b/drivers/hwmon/spd5118.c
index 6cee48a3e5c3..358152868d96 100644
--- a/drivers/hwmon/spd5118.c
+++ b/drivers/hwmon/spd5118.c
@@ -291,12 +291,6 @@ static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types typ
}
}
-static inline bool spd5118_parity8(u8 w)
-{
- w ^= w >> 4;
- return (0x6996 >> (w & 0xf)) & 1;
-}
-
/*
* Bank and vendor id are 8-bit fields with seven data bits and odd parity.
* Vendor IDs 0 and 0x7f are invalid.
@@ -304,7 +298,7 @@ static inline bool spd5118_parity8(u8 w)
*/
static bool spd5118_vendor_valid(u8 bank, u8 id)
{
- if (!spd5118_parity8(bank) || !spd5118_parity8(id))
+ if (parity8(bank) == 0 || parity8(id) == 0)
return false;
id &= 0x7f;
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 3/5] i3c: dw: use parity8 helper instead of open coding it
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 1/5] bitops: add generic parity calculation for u8 Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 2/5] hwmon: (spd5118) Use generic parity calculation Wolfram Sang
@ 2025-01-06 9:27 ` Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 4/5] i3c: mipi-i3c-hci: " Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 5/5] i3c: cdns: " Wolfram Sang
4 siblings, 0 replies; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang, Alexandre Belloni
The kernel has now a generic helper for getting parity with easier to
understand semantics. Make use of it.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
drivers/i3c/master/dw-i3c-master.c | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c
index d4b80eb8cecd..0d4d44458c11 100644
--- a/drivers/i3c/master/dw-i3c-master.c
+++ b/drivers/i3c/master/dw-i3c-master.c
@@ -251,14 +251,6 @@ struct dw_i3c_i2c_dev_data {
struct i3c_generic_ibi_pool *ibi_pool;
};
-static u8 even_parity(u8 p)
-{
- p ^= p >> 4;
- p &= 0xf;
-
- return (0x9669 >> p) & 1;
-}
-
static bool dw_i3c_master_supports_ccc_cmd(struct i3c_master_controller *m,
const struct i3c_ccc_cmd *cmd)
{
@@ -848,7 +840,7 @@ static int dw_i3c_master_daa(struct i3c_master_controller *m)
struct dw_i3c_xfer *xfer;
struct dw_i3c_cmd *cmd;
u32 olddevs, newdevs;
- u8 p, last_addr = 0;
+ u8 last_addr = 0;
int ret, pos;
ret = pm_runtime_resume_and_get(master->dev);
@@ -873,9 +865,9 @@ static int dw_i3c_master_daa(struct i3c_master_controller *m)
}
master->devs[pos].addr = ret;
- p = even_parity(ret);
last_addr = ret;
- ret |= (p << 7);
+
+ ret |= parity8(ret) ? 0 : BIT(7);
writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret),
master->regs +
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 4/5] i3c: mipi-i3c-hci: use parity8 helper instead of open coding it
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
` (2 preceding siblings ...)
2025-01-06 9:27 ` [PATCH v3 3/5] i3c: dw: use parity8 helper instead of open coding it Wolfram Sang
@ 2025-01-06 9:27 ` Wolfram Sang
2025-01-06 13:06 ` David Laight
2025-01-06 9:27 ` [PATCH v3 5/5] i3c: cdns: " Wolfram Sang
4 siblings, 1 reply; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang, Jarkko Nikula,
Alexandre Belloni
The kernel has now a generic helper for getting parity with easier to
understand semantics. Make use of it.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
---
drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 11 +----------
1 file changed, 1 insertion(+), 10 deletions(-)
diff --git a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
index 47b9b4d4ed3f..85c4916972e4 100644
--- a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
+++ b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
@@ -40,15 +40,6 @@
#define dat_w0_write(i, v) writel(v, hci->DAT_regs + (i) * 8)
#define dat_w1_write(i, v) writel(v, hci->DAT_regs + (i) * 8 + 4)
-static inline bool dynaddr_parity(unsigned int addr)
-{
- addr |= 1 << 7;
- addr += addr >> 4;
- addr += addr >> 2;
- addr += addr >> 1;
- return (addr & 1);
-}
-
static int hci_dat_v1_init(struct i3c_hci *hci)
{
unsigned int dat_idx;
@@ -123,7 +114,7 @@ static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,
dat_w0 = dat_w0_read(dat_idx);
dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY);
dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) |
- (dynaddr_parity(address) ? DAT_0_DYNADDR_PARITY : 0);
+ (parity8(address) ? 0 : DAT_0_DYNADDR_PARITY);
dat_w0_write(dat_idx, dat_w0);
}
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 5/5] i3c: cdns: use parity8 helper instead of open coding it
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
` (3 preceding siblings ...)
2025-01-06 9:27 ` [PATCH v3 4/5] i3c: mipi-i3c-hci: " Wolfram Sang
@ 2025-01-06 9:27 ` Wolfram Sang
4 siblings, 0 replies; 7+ messages in thread
From: Wolfram Sang @ 2025-01-06 9:27 UTC (permalink / raw)
To: linux-i3c
Cc: linux-kernel, linux-renesas-soc, Wolfram Sang,
Przemysław Gaj, Alexandre Belloni
The kernel has now a generic helper for getting parity with easier to
understand semantics. Make use of it.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
drivers/i3c/master/i3c-master-cdns.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/i3c/master/i3c-master-cdns.c b/drivers/i3c/master/i3c-master-cdns.c
index 06c0592487d3..fedbe6624a1c 100644
--- a/drivers/i3c/master/i3c-master-cdns.c
+++ b/drivers/i3c/master/i3c-master-cdns.c
@@ -889,8 +889,7 @@ static u32 prepare_rr0_dev_address(u32 addr)
ret |= (addr & GENMASK(9, 7)) << 6;
/* RR0[0] = ~XOR(addr[6:0]) */
- if (!(hweight8(addr & 0x7f) & 1))
- ret |= 1;
+ ret |= parity8(addr & 0x7f) ? 0 : BIT(0);
return ret;
}
--
2.45.2
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v3 4/5] i3c: mipi-i3c-hci: use parity8 helper instead of open coding it
2025-01-06 9:27 ` [PATCH v3 4/5] i3c: mipi-i3c-hci: " Wolfram Sang
@ 2025-01-06 13:06 ` David Laight
0 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2025-01-06 13:06 UTC (permalink / raw)
To: Wolfram Sang
Cc: linux-i3c, linux-kernel, linux-renesas-soc, Jarkko Nikula,
Alexandre Belloni
On Mon, 6 Jan 2025 10:27:22 +0100
Wolfram Sang <wsa+renesas@sang-engineering.com> wrote:
> The kernel has now a generic helper for getting parity with easier to
> understand semantics. Make use of it.
This need to explain that although the algorithm has changed the existing
code is wrong.
David
>
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> ---
> drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 11 +----------
> 1 file changed, 1 insertion(+), 10 deletions(-)
>
> diff --git a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
> index 47b9b4d4ed3f..85c4916972e4 100644
> --- a/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
> +++ b/drivers/i3c/master/mipi-i3c-hci/dat_v1.c
> @@ -40,15 +40,6 @@
> #define dat_w0_write(i, v) writel(v, hci->DAT_regs + (i) * 8)
> #define dat_w1_write(i, v) writel(v, hci->DAT_regs + (i) * 8 + 4)
>
> -static inline bool dynaddr_parity(unsigned int addr)
> -{
> - addr |= 1 << 7;
> - addr += addr >> 4;
> - addr += addr >> 2;
> - addr += addr >> 1;
> - return (addr & 1);
> -}
> -
> static int hci_dat_v1_init(struct i3c_hci *hci)
> {
> unsigned int dat_idx;
> @@ -123,7 +114,7 @@ static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci,
> dat_w0 = dat_w0_read(dat_idx);
> dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY);
> dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) |
> - (dynaddr_parity(address) ? DAT_0_DYNADDR_PARITY : 0);
> + (parity8(address) ? 0 : DAT_0_DYNADDR_PARITY);
> dat_w0_write(dat_idx, dat_w0);
> }
>
--
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-01-10 11:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-06 9:27 [PATCH v3 0/5] i3c: introduce and use generic parity helper Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 1/5] bitops: add generic parity calculation for u8 Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 2/5] hwmon: (spd5118) Use generic parity calculation Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 3/5] i3c: dw: use parity8 helper instead of open coding it Wolfram Sang
2025-01-06 9:27 ` [PATCH v3 4/5] i3c: mipi-i3c-hci: " Wolfram Sang
2025-01-06 13:06 ` David Laight
2025-01-06 9:27 ` [PATCH v3 5/5] i3c: cdns: " Wolfram Sang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).