Skip to contentSkip to conversation

linux/compat.h: port lower_32_bits and upper_32_bits from Linux

PATCH Superseded Lijun Pan 2 participants 2 messages Jun 13, 2014

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lijun Pan

author
#0 lore raw

PATCH v3 1/1 linux/compat.h: port lower_32_bits and upper_32_bits from Linux

[backport from linux commit 204b885e and 218e180e7]
64 bit processors are becomming more and more popular. lower_32_bits and upper_32_bits save our labor doing shifts/manipulations like (u32)(n) and (u32)((n) >> 32). They are good helpers in both little and big endian cases. Port these two functions here from Linux:include/linux/kernel.h, cater the comment message to little/big endian cases, also remove the definition in drivers/usb/host/xhci.h. Later on, developers could include linux/compat.h if they want to use these two functions.

---
v3: change the comment message for lower_32_bits() and upper_32_bits()
change the commit message to argue the necessity of this patch.
v2: add git SHA of linux kernel tree.

 drivers/usb/host/xhci.h |  4 +---
 include/linux/compat.h  | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
drivers/usb/host/xhci.h +1 -3
index ceb1573..6381596 100644
@@ -20,9 +20,7 @@
 #include <asm/cache.h>
 #include <asm/io.h>
 #include <linux/list.h>
-
-#define upper_32_bits(n) (u32)((n) >> 32)
-#define lower_32_bits(n) (u32)(n)
+#include <linux/compat.h>
 
 #define MAX_EP_CTX_NUM		31
 #define XHCI_ALIGNMENT		64
include/linux/compat.h +19 -0
index 3fdfb39..35e216e 100644
@@ -57,4 +57,23 @@
 				  , __FILE__, __LINE__); }
 
 #define PAGE_SIZE	4096
+
+/**
+ * upper_32_bits - return MSB bits 32-63 of a number if little endian, or
+ * return MSB bits 0-31 of a number if big endian.
+ * @n: the number we're accessing
+ *
+ * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
+ * the "right shift count >= width of type" warning when that quantity is
+ * 32-bits.
+ */
+#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
+
+/**
+ * lower_32_bits - return LSB bits 0-31 of a number if little endian, or
+ * return LSB bits 32-63 of a number if big endian.
+ * @n: the number we're accessing
+ */
+#define lower_32_bits(n) ((u32)(n))
+
 #endif
Signature · 1 line
1.9.3
#1 lore raw

PATCH v3 1/1 linux/compat.h: port lower_32_bits and upper_32_bits from Linux

Dear Lijun Pan,

Quoted 11 lines · In message <[email protected]> you wrote:

[backport from linux commit 204b885e and 218e180e7]
64 bit processors are becomming more and more popular. lower_32_bits and upper_32_bits save our labor doing shifts/manipulations like (u32)(n) and (u32)((n) >> 32). They are good helpers in both little and big endian cases. Port these two functions here from Linux:include/linux/kernel.h, cater the comment message to little/big endian cases, also remove the definition in drivers/usb/host/xhci.h. Later on, developers could include linux/compat.h if they want to use these two functions.

Quoted 5 lines

---
v3: change the comment message for lower_32_bits() and upper_32_bits()
change the commit message to argue the necessity of this patch.
v2: add git SHA of linux kernel tree.

I think it would be better to split this commit into two separate patches.

One, which in my opinion should be part of a patch series that adds more users of these functions, would add the new functions. The other would then convert the only existing user (drivers/usb/host/xhci.h) to use these.

But before we convert drivers/usb/host/xhci.h, we should fix the FIXME there - this looks very much like a bug to me:

1119 static inline void xhci_writeq(__le64 volatile *regs, const u64 val) 1120 {

1121         __u32 *ptr = (__u32 *)regs;
1122         u32 val_lo = lower_32_bits(val);
1123         /* FIXME */
1124         u32 val_hi = 0;

I think this should rather be

	u32 val_hi = upper_32_bits(val);

and I cannot think of any good reason why it is not...

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Der Irrtum wiederholt sich immerfort in der Tat.  Deshalb mu? man das
Wahre unerm?dlich in Worten wiederholen.                     - Goethe