Skip to contentSkip to conversation

usb: xhci: page size improvements

PATCH Superseded Niklas Neronin 3 participants 5 messages Feb 4, 2025 – Mar 5, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover usb: xhci: page size improvements

Correct off-by-one page size debug message.
Set page size to the xHCI-supported size, instead of 4KB.

Clarified the interpretation of the xHCI spec 1.9 regarding page size. The correct interpretation is that only one bit is set, indicating the only supported page size. This is supported by the following sources:

Section 6.6.1, PSZ:
The PSZ calculation uses the page size bit and would not work with multiple bits set.

Section 7.7, Implementation Notes:
"This version of the xHCI spec only allows an implementation to support
a single page size, as reported by the PAGESIZE register."

Version 3 changes:
* Rebase agains Linux 6.14-rc1.
* Repalce variable int 'i' with unsigned int 'val'.
* Rename temp variable 'page_shift' to 'page_size'.
Version 2 changes:
* Added handling for invalid page size register values.

Niklas Neronin (2):
usb: xhci: correct debug message page size calculation
usb: xhci: set page size to the xHCI-supported size

 drivers/usb/host/xhci-mem.c | 34 ++++++++++++++++++----------------
 drivers/usb/host/xhci.h     |  8 ++++----
 2 files changed, 22 insertions(+), 20 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 1/2 usb: xhci: correct debug message page size calculation

The ffs() function returns the index of the first set bit, starting from 1. If no bits are set, it returns zero. This behavior causes an off-by-one page size in the debug message, as the page size calculation [1] is zero-based, while ffs() is one-based.

Fix this by subtracting one from the result of ffs(). Note that since variable 'val' is unsigned, subtracting one from zero will result in the maximum unsigned integer value. Consequently, the condition 'if (val < 16)' will still function correctly.

[1], Page size: (2^(n+12)), where 'n' is the set page size bit.

Fixes 81720ec5320c ("usb: host: xhci: use ffs() in xhci_mem_init()")

---
V3:
* Repalce variable int 'i' with unsigned int 'val'.

 drivers/usb/host/xhci-mem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-mem.c +3 -3
index 92703efda1f7..dc5bcd8db4c0 100644
@@ -2391,10 +2391,10 @@ int (struct xhci_hcd *xhci, gfp_t flags)
 	page_size = readl(&xhci->op_regs->page_size);
 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
 			"Supported page size register = 0x%x", page_size);
-	i = ffs(page_size);
-	if (i < 16)
+	val = ffs(page_size) - 1;
+	if (val < 16)
 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
-			"Supported page size of %iK", (1 << (i+12)) / 1024);
+			"Supported page size of %iK", (1 << (val + 12)) / 1024);
 	else
 		xhci_warn(xhci, "WARN: no supported page size\n");
 	/* Use 4K pages, since that's common and the minimum the HC supports */
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH v3 1/2] usb: xhci: correct debug message page size calculation

Quoted 10 lines · Niklas Neronin

The ffs() function returns the index of the first set bit, starting from 1. If no bits are set, it returns zero. This behavior causes an off-by-one page size in the debug message, as the page size calculation [1] is zero-based, while ffs() is one-based.

Fix this by subtracting one from the result of ffs(). Note that since variable 'val' is unsigned, subtracting one from zero will result in the maximum unsigned integer value. Consequently, the condition 'if (val < 16)' will still function correctly.

Quoted 4 lines

[1], Page size: (2^(n+12)), where 'n' is the set page size bit.

Fixes 81720ec5320c ("usb: host: xhci: use ffs() in xhci_mem_init()")

Actually, is it useful to fix this code if the next patch in the series removes it completely?

I thought that you perhaps meant this simple fix to go to stable, but it isn't marked as such.

Regards,
Michal

#3 lore raw

PATCH v3 2/2 usb: xhci: set page size to the xHCI-supported size

In reply to Niklas Neronin

The current xHCI driver does not validate whether a page size of 4096 bytes is supported. Address the issue by setting the page size to the value supported by the xHCI controller, as read from the Page Size register. In the event of an unexpected value; default to a 4K page size.

Additionally, this commit removes unnecessary debug messages and instead prints the supported and used page size once.

The xHCI controller supports page sizes of (2^{(n+12)}) bytes, where 'n' is the Page Size Bit. Only one page size is supported, with a maximum page size of 128 KB.

---
V3:
* Rename local variable 'page_shift' to 'page_size'.

 drivers/usb/host/xhci-mem.c | 34 ++++++++++++++++++----------------
 drivers/usb/host/xhci.h     |  8 ++++----
 2 files changed, 22 insertions(+), 20 deletions(-)
drivers/usb/host/xhci-mem.c +18 -16
index dc5bcd8db4c0..c2ee0d32f5cd 100644
@@ -1953,7 +1953,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 	xhci->interrupters = NULL;
 
 	xhci->page_size = 0;
-	xhci->page_shift = 0;
 	xhci->usb2_rhub.bus_state.bus_suspended = 0;
 	xhci->usb3_rhub.bus_state.bus_suspended = 0;
 }
@@ -2372,6 +2371,22 @@ xhci_create_secondary_interrupter(struct usb_hcd *hcd, unsigned int segs,
 }
 EXPORT_SYMBOL_GPL(xhci_create_secondary_interrupter);
 
+static void xhci_hcd_page_size(struct xhci_hcd *xhci)
+{
+	u32 page_size;
+
+	page_size = readl(&xhci->op_regs->page_size) & XHCI_PAGE_SIZE_MASK;
+	if (!page_size || page_size & (page_size - 1)) {
+		xhci_warn(xhci, "Invalid page size register = 0x%x\n", page_size);
+		/* Fallback to 4K page size, since that's common */
+		page_size = 1;
+	}
+
+	xhci->page_size = page_size << 12;
+	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "HCD page size set to %iK",
+		       xhci->page_size >> 10);
+}
+
 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 {
 	struct xhci_interrupter *ir;
@@ -2379,7 +2394,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 	dma_addr_t	dma;
 	unsigned int	val, val2;
 	u64		val_64;
-	u32		page_size, temp;
+	u32		temp;
 	int		i;
 
 	INIT_LIST_HEAD(&xhci->cmd_list);
@@ -2388,20 +2403,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 	INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
 	init_completion(&xhci->cmd_ring_stop_completion);
 
-	page_size = readl(&xhci->op_regs->page_size);
-	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
-			"Supported page size register = 0x%x", page_size);
-	val = ffs(page_size) - 1;
-	if (val < 16)
-		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
-			"Supported page size of %iK", (1 << (val + 12)) / 1024);
-	else
-		xhci_warn(xhci, "WARN: no supported page size\n");
-	/* Use 4K pages, since that's common and the minimum the HC supports */
-	xhci->page_shift = 12;
-	xhci->page_size = 1 << xhci->page_shift;
-	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
-			"HCD page size set to %iK", xhci->page_size / 1024);
+	xhci_hcd_page_size(xhci);
 
 	/*
 	 * Program the Number of Device Slots Enabled field in the CONFIG
drivers/usb/host/xhci.h +4 -4
index 8c164340a2c3..5b8751b86008 100644
@@ -211,6 +211,9 @@ struct xhci_op_regs {
 #define CONFIG_CIE		(1 << 9)
 /* bits 10:31 - reserved and should be preserved */
 
+/* bits 15:0 - HCD page shift bit */
+#define XHCI_PAGE_SIZE_MASK     0xffff
+
 /**
  * struct xhci_intr_reg - Interrupt Register Set
  * @irq_pending:	IMAN - Interrupt Management Register.  Used to enable
@@ -1514,10 +1517,7 @@ struct xhci_hcd {
 	u16		max_interrupters;
 	/* imod_interval in ns (I * 250ns) */
 	u32		imod_interval;
-	/* 4KB min, 128MB max */
-	int		page_size;
-	/* Valid values are 12 to 20, inclusive */
-	int		page_shift;
+	u32		page_size;
 	/* MSI-X/MSI vectors */
 	int		nvecs;
 	/* optional clocks */
Signature · 1 line
2.47.2
#4 lore raw

Re: [PATCH v3 2/2] usb: xhci: set page size to the xHCI-supported size

Quoted 34 lines · Niklas Neronin

The current xHCI driver does not validate whether a page size of 4096 bytes is supported. Address the issue by setting the page size to the value supported by the xHCI controller, as read from the Page Size register. In the event of an unexpected value; default to a 4K page size.

Additionally, this commit removes unnecessary debug messages and instead prints the supported and used page size once.

The xHCI controller supports page sizes of (2^{(n+12)}) bytes, where 'n' is the Page Size Bit. Only one page size is supported, with a maximum page size of 128 KB.

---
V3:
* Rename local variable 'page_shift' to 'page_size'.

  drivers/usb/host/xhci-mem.c | 34 ++++++++++++++++++----------------
  drivers/usb/host/xhci.h     |  8 ++++----
  2 files changed, 22 insertions(+), 20 deletions(-)
drivers/usb/host/xhci-mem.c +0 -1
index dc5bcd8db4c0..c2ee0d32f5cd 100644
@@ -1953,7 +1953,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
  	xhci->interrupters = NULL;
 
  	xhci->page_size = 0;
-	xhci->page_shift = 0;
  	xhci->usb2_rhub.bus_state.bus_suspended = 0;
  	xhci->usb3_rhub.bus_state.bus_suspended = 0;
  }

drivers/usb/host/xhci-mem.c xhci_create_secondary_interrupter(struct usb_hcd *hcd, unsigned int segs, · line 2379

Quoted 9 lines
drivers/usb/host/xhci-mem.c +6 -0
@@ -2372,6 +2371,22 @@ xhci_create_secondary_interrupter(struct usb_hcd *hcd, unsigned int segs,
  }
  EXPORT_SYMBOL_GPL(xhci_create_secondary_interrupter);
 
+static void xhci_hcd_page_size(struct xhci_hcd *xhci)
+{
+	u32 page_size;
+
+	page_size = readl(&xhci->op_regs->page_size) & XHCI_PAGE_SIZE_MASK;
+	if (!page_size || page_size & (page_size - 1)) {

is_power_of_2(page_size) does the same, and is easier to understand

-Mathias