Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: xhci: standardize single bit-field macros

Convert single bit-field macros to simple masks. The change makes the
masks more universal. Multi bit-field macros are changed in the next
commit. After both changes, all masks in xhci-caps.h will follow the
same format. I plan to introduce this change to all xhci macros.

Bit shift operations on a 32-bit signed can be problematic on some
architectures. Instead use BIT() macro, which returns a 64-bit unsigned
value. This ensures that the shift operation is performed on an unsigned
type, which is safer and more portable across different architectures.
Using unsigned integers for bit shifts avoids issues related to sign bits
and ensures consistent behavior.

Switch from 32-bit to 64-bit?
As far as I am aware, this does not cause any issues.
Performing bitwise operations between 32 and 64 bit values, the smaller
operand is promoted to match the size of the larger one, resulting in a
64-bit operation. This promotion extends the 32-bit value to 64 bits,
by zero-padding (for unsigned).

Will the change to 64-bit slow down the xhci driver?
On a 64-bit architecture - No. On a 32-bit architecture, yes? but in my
opinion the performance decrease does not outweigh the readability and
other benefits of using BIT() macro.

Why not use FIELD_GET() and FIELD_PREP()?
While they can be used for single bit macros, I prefer to use simple
bitwise operation directly. Because, it takes less space, is less overhead
and is as clear as if using FIELD_GET() and FIELD_PREP().

Why not use test_bit() macro?
Same reason as with FIELD_GET() and FIELD_PREP().

Suggested-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Link: https://patch.msgid.link/20251119142417.2820519-23-mathias.nyman@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Niklas Neronin and committed by
Greg Kroah-Hartman
757508d6 2282ab38

+38 -37
+25 -23
drivers/usb/host/xhci-caps.h
··· 4 4 * xHCI Specification Section 5.3, Revision 1.2. 5 5 */ 6 6 7 + #include <linux/bits.h> 8 + 7 9 /* hc_capbase - bitmasks */ 8 10 /* bits 7:0 - Capability Registers Length */ 9 11 #define HC_LENGTH(p) ((p) & 0xff) ··· 34 32 * xHCI specification section 5.3.4. 35 33 */ 36 34 #define HCS_IST_VALUE(p) ((p) & 0x7) 37 - #define HCS_IST_UNIT(p) ((p) & (1 << 3)) 35 + #define HCS_IST_UNIT BIT(3) 38 36 /* bits 7:4 - Event Ring Segment Table Max, 2^(n) */ 39 37 #define HCS_ERST_MAX(p) (((p) >> 4) & 0xf) 40 38 /* bits 20:8 - Rsvd */ ··· 54 52 55 53 /* HCCPARAMS1 - hcc_params - bitmasks */ 56 54 /* bit 0 - 64-bit Addressing Capability */ 57 - #define HCC_64BIT_ADDR(p) ((p) & (1 << 0)) 55 + #define HCC_64BIT_ADDR BIT(0) 58 56 /* bit 1 - BW Negotiation Capability */ 59 - #define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1)) 57 + #define HCC_BANDWIDTH_NEG BIT(1) 60 58 /* bit 2 - Context Size */ 61 - #define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2)) 62 - #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32) 59 + #define HCC_64BYTE_CONTEXT BIT(2) 60 + #define CTX_SIZE(_hcc) (_hcc & HCC_64BYTE_CONTEXT ? 64 : 32) 63 61 /* bit 3 - Port Power Control */ 64 - #define HCC_PPC(p) ((p) & (1 << 3)) 62 + #define HCC_PPC BIT(3) 65 63 /* bit 4 - Port Indicators */ 66 - #define HCS_INDICATOR(p) ((p) & (1 << 4)) 64 + #define HCS_INDICATOR BIT(4) 67 65 /* bit 5 - Light HC Reset Capability */ 68 - #define HCC_LIGHT_RESET(p) ((p) & (1 << 5)) 66 + #define HCC_LIGHT_RESET BIT(5) 69 67 /* bit 6 - Latency Tolerance Messaging Capability */ 70 - #define HCC_LTC(p) ((p) & (1 << 6)) 68 + #define HCC_LTC BIT(6) 71 69 /* bit 7 - No Secondary Stream ID Support */ 72 - #define HCC_NSS(p) ((p) & (1 << 7)) 70 + #define HCC_NSS BIT(7) 73 71 /* bit 8 - Parse All Event Data */ 74 72 /* bit 9 - Short Packet Capability */ 75 - #define HCC_SPC(p) ((p) & (1 << 9)) 73 + #define HCC_SPC BIT(9) 76 74 /* bit 10 - Stopped EDTLA Capability */ 77 75 /* bit 11 - Contiguous Frame ID Capability */ 78 - #define HCC_CFC(p) ((p) & (1 << 11)) 76 + #define HCC_CFC BIT(11) 79 77 /* bits 15:12 - Max size for Primary Stream Arrays, 2^(n+1) */ 80 78 #define HCC_MAX_PSA(p) (1 << ((((p) >> 12) & 0xf) + 1)) 81 79 /* bits 31:16 - xHCI Extended Capabilities Pointer, from PCI base: 2^(n) */ ··· 93 91 94 92 /* HCCPARAMS2 - hcc_params2 - bitmasks */ 95 93 /* bit 0 - U3 Entry Capability */ 96 - #define HCC2_U3C(p) ((p) & (1 << 0)) 94 + #define HCC2_U3C BIT(0) 97 95 /* bit 1 - Configure Endpoint Command Max Exit Latency Too Large Capability */ 98 - #define HCC2_CMC(p) ((p) & (1 << 1)) 96 + #define HCC2_CMC BIT(1) 99 97 /* bit 2 - Force Save Context Capabilitu */ 100 - #define HCC2_FSC(p) ((p) & (1 << 2)) 98 + #define HCC2_FSC BIT(2) 101 99 /* bit 3 - Compliance Transition Capability, false: compliance is enabled by default */ 102 - #define HCC2_CTC(p) ((p) & (1 << 3)) 100 + #define HCC2_CTC BIT(3) 103 101 /* bit 4 - Large ESIT Payload Capability, true: HC support ESIT payload > 48k */ 104 - #define HCC2_LEC(p) ((p) & (1 << 4)) 102 + #define HCC2_LEC BIT(4) 105 103 /* bit 5 - Configuration Information Capability */ 106 - #define HCC2_CIC(p) ((p) & (1 << 5)) 104 + #define HCC2_CIC BIT(5) 107 105 /* bit 6 - Extended TBC Capability, true: Isoc burst count > 65535 */ 108 - #define HCC2_ETC(p) ((p) & (1 << 6)) 106 + #define HCC2_ETC BIT(6) 109 107 /* bit 7 - Extended TBC TRB Status Capability */ 110 - #define HCC2_ETC_TSC(p) ((p) & (1 << 7)) 108 + #define HCC2_ETC_TSC BIT(7) 111 109 /* bit 8 - Get/Set Extended Property Capability */ 112 - #define HCC2_GSC(p) ((p) & (1 << 8)) 110 + #define HCC2_GSC BIT(8) 113 111 /* bit 9 - Virtualization Based Trusted I/O Capability */ 114 - #define HCC2_VTC(p) ((p) & (1 << 9)) 112 + #define HCC2_VTC BIT(9) 115 113 /* bit 10 - Rsvd */ 116 114 /* bit 11 - HC support Double BW on a eUSB2 HS ISOC EP */ 117 - #define HCC2_EUSB2_DIC(p) ((p) & (1 << 11)) 115 + #define HCC2_EUSB2_DIC BIT(11) 118 116 /* bits 31:12 - Rsvd */
+1 -1
drivers/usb/host/xhci-debugfs.c
··· 355 355 356 356 if (!strncmp(buf, "compliance", 10)) { 357 357 /* If CTC is clear, compliance is enabled by default */ 358 - if (!HCC2_CTC(xhci->hcc_params2)) 358 + if (!(xhci->hcc_params2 & HCC2_CTC)) 359 359 return count; 360 360 spin_lock_irqsave(&xhci->lock, flags); 361 361 /* compliance mode can only be enabled on ports in RxDetect */
+3 -3
drivers/usb/host/xhci-hub.c
··· 110 110 ss_cap->bU2DevExitLat = 0; /* set later */ 111 111 112 112 reg = readl(&xhci->cap_regs->hcc_params); 113 - if (HCC_LTC(reg)) 113 + if (reg & HCC_LTC) 114 114 ss_cap->bmAttributes |= USB_LTM_SUPPORT; 115 115 116 116 if ((xhci->quirks & XHCI_LPM_SUPPORT)) { ··· 263 263 desc->bNbrPorts = ports; 264 264 temp = 0; 265 265 /* Bits 1:0 - support per-port power switching, or power always on */ 266 - if (HCC_PPC(xhci->hcc_params)) 266 + if (xhci->hcc_params & HCC_PPC) 267 267 temp |= HUB_CHAR_INDV_PORT_LPSM; 268 268 else 269 269 temp |= HUB_CHAR_NO_LPSM; ··· 1400 1400 * automatically entered as on 1.0 and prior. 1401 1401 */ 1402 1402 if (link_state == USB_SS_PORT_LS_COMP_MOD) { 1403 - if (!HCC2_CTC(xhci->hcc_params2)) { 1403 + if (!(xhci->hcc_params2 & HCC2_CTC)) { 1404 1404 xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n"); 1405 1405 break; 1406 1406 }
+3 -4
drivers/usb/host/xhci-mem.c
··· 463 463 return NULL; 464 464 465 465 ctx->type = type; 466 - ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024; 466 + ctx->size = xhci->hcc_params & HCC_64BYTE_CONTEXT ? 2048 : 1024; 467 467 if (type == XHCI_CTX_TYPE_INPUT) 468 468 ctx->size += CTX_SIZE(xhci->hcc_params); 469 469 ··· 1344 1344 bool lec; 1345 1345 1346 1346 /* xHCI 1.1 with LEC set does not use mult field, except intel eUSB2 */ 1347 - lec = xhci->hci_version > 0x100 && HCC2_LEC(xhci->hcc_params2); 1347 + lec = xhci->hci_version > 0x100 && (xhci->hcc_params2 & HCC2_LEC); 1348 1348 1349 1349 /* eUSB2 double isoc bw devices are the only USB2 devices using mult */ 1350 1350 if (usb_endpoint_is_hs_isoc_double(udev, ep) && ··· 1433 1433 ring_type = usb_endpoint_type(&ep->desc); 1434 1434 1435 1435 /* Ensure host supports double isoc bandwidth for eUSB2 devices */ 1436 - if (usb_endpoint_is_hs_isoc_double(udev, ep) && 1437 - !HCC2_EUSB2_DIC(xhci->hcc_params2)) { 1436 + if (usb_endpoint_is_hs_isoc_double(udev, ep) && !(xhci->hcc_params2 & HCC2_EUSB2_DIC)) { 1438 1437 dev_dbg(&udev->dev, "Double Isoc Bandwidth not supported by xhci\n"); 1439 1438 return -EINVAL; 1440 1439 }
+4 -4
drivers/usb/host/xhci-ring.c
··· 3966 3966 { 3967 3967 int ist = HCS_IST_VALUE(xhci->hcs_params2); 3968 3968 3969 - if (HCS_IST_UNIT(xhci->hcs_params2)) 3969 + if (xhci->hcs_params2 & HCS_IST_UNIT) 3970 3970 ist *= 8; 3971 3971 return ist; 3972 3972 } ··· 4135 4135 /* use SIA as default, if frame id is used overwrite it */ 4136 4136 sia_frame_id = TRB_SIA; 4137 4137 if (!(urb->transfer_flags & URB_ISO_ASAP) && 4138 - HCC_CFC(xhci->hcc_params)) { 4138 + (xhci->hcc_params & HCC_CFC)) { 4139 4139 frame_id = xhci_get_isoc_frame_id(xhci, urb, i); 4140 4140 if (frame_id >= 0) 4141 4141 sia_frame_id = TRB_FRAME_ID(frame_id); ··· 4219 4219 } 4220 4220 4221 4221 /* store the next frame id */ 4222 - if (HCC_CFC(xhci->hcc_params)) 4222 + if (xhci->hcc_params & HCC_CFC) 4223 4223 xep->next_frame_id = urb->start_frame + num_tds * urb->interval; 4224 4224 4225 4225 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { ··· 4298 4298 check_interval(urb, ep_ctx); 4299 4299 4300 4300 /* Calculate the start frame and put it in urb->start_frame. */ 4301 - if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { 4301 + if ((xhci->hcc_params & HCC_CFC) && !list_empty(&ep_ring->td_list)) { 4302 4302 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) { 4303 4303 urb->start_frame = xep->next_frame_id; 4304 4304 goto skip_start_over;
+1 -1
drivers/usb/host/xhci-trace.h
··· 81 81 ), 82 82 TP_fast_assign( 83 83 84 - __entry->ctx_64 = HCC_64BYTE_CONTEXT(xhci->hcc_params); 84 + __entry->ctx_64 = xhci->hcc_params & HCC_64BYTE_CONTEXT; 85 85 __entry->ctx_type = ctx->type; 86 86 __entry->ctx_dma = ctx->dma; 87 87 __entry->ctx_va = ctx->bytes;
+1 -1
drivers/usb/host/xhci.c
··· 5495 5495 5496 5496 /* Set dma_mask and coherent_dma_mask to 64-bits, 5497 5497 * if xHC supports 64-bit addressing */ 5498 - if (HCC_64BIT_ADDR(xhci->hcc_params) && 5498 + if ((xhci->hcc_params & HCC_64BIT_ADDR) && 5499 5499 !dma_set_mask(dev, DMA_BIT_MASK(64))) { 5500 5500 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); 5501 5501 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));