Skip to contentSkip to conversation

PCI: Add PCI_RID_MASK macro

PATCH In review Aksh Garg 4 participants 6 messages Feb 16 – Feb 17

Subsystems (3)
lore
Share & tools
lore

Aksh Garg

author
#0 lore raw

PATCH 1/1 PCI: Add PCI_RID_MASK macro

Routing ID (RID) is a 16-bit value composed of bus number and devfn. Add a PCI_RID_MASK macro to eliminate the use of magic number 0xffff when masking for RIDs.

This provides a standard way for drivers to extract RIDs without resorting to magic numbers.

---
 drivers/pci/controller/pcie-apple.c | 2 +-
 include/linux/pci.h                 | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/pci/controller/pcie-apple.c +1 -1
index 2d92fc79f6dd..6bf9fe102e2f 100644
@@ -802,7 +802,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci
 		u32 val;
 
 		val = readl_relaxed(port_rid2sid_addr(port, idx));
-		if ((val & 0xffff) == rid) {
+		if ((val & PCI_RID_MASK) == rid) {
 			apple_pcie_rid2sid_write(port, idx, 0);
 			bitmap_release_region(port->sid_map, idx, 0);
 			dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
include/linux/pci.h +1 -0
index 864775651c6f..e3d9730121c8 100644
@@ -71,6 +71,7 @@
 #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
 /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
 #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff
 
 /* pci_slot represents a physical slot */
 struct pci_slot {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Aksh Garg

Routing ID (RID) is a 16-bit value composed of bus number and devfn.

nitpick: "Requester ID" might be a more appropriate description of BDF.

Quoted 7 lines

Add a PCI_RID_MASK macro to eliminate the use of magic number 0xffff when masking for RIDs.

This provides a standard way for drivers to extract RIDs without resorting to magic numbers.

Quoted 23 lines
---
 drivers/pci/controller/pcie-apple.c | 2 +-
 include/linux/pci.h                 | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/pci/controller/pcie-apple.c +1 -1
index 2d92fc79f6dd..6bf9fe102e2f 100644
@@ -802,7 +802,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci
 		u32 val;
 
 		val = readl_relaxed(port_rid2sid_addr(port, idx));
-		if ((val & 0xffff) == rid) {
+		if ((val & PCI_RID_MASK) == rid) {
 			apple_pcie_rid2sid_write(port, idx, 0);
 			bitmap_release_region(port->sid_map, idx, 0);
 			dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
include/linux/pci.h +0 -0
index 864775651c6f..e3d9730121c8 100644

include/linux/pci.h line 77

Quoted 7 lines
include/linux/pci.h +1 -0
@@ -71,6 +71,7 @@
 #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
 /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
 #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff
 
 /* pci_slot represents a physical slot */
 struct pci_slot {

Regards,
Siddharth.

#2 lore raw
In reply to Aksh Garg

include/linux/pci.h line 74

Quoted 6 lines · Aksh Garg

+++ b/include/linux/pci.h

include/linux/pci.h +1 -0
@@ -71,6 +71,7 @@
 #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
 /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
 #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff

Why not instead introduce

#define PCI_RID(x) ((x) & 0xffff)

to go alongside PCI_SLOT() & friends?

Thanks,

Lukas

maintainer
#3 lore raw
In reply to Aksh Garg
Quoted 2 lines · Aksh Garg

Routing ID (RID) is a 16-bit value composed of bus number and devfn.

No. RID here means "Requester ID". Please lookup the PCI spec.

Quoted 2 lines

Add a PCI_RID_MASK macro to eliminate the use of magic number 0xffff when masking for RIDs.

I'm not convinced this is universally true. Some RIDs (or RID-like identifiers, such as Stream IDs and Device IDs) are larger than 16 bit, depending on the integration.

The fact that this particular integration uses a 16bit RID without any extra qualifier in the upper bits doesn't make it universal.

Quoted 27 lines

This provides a standard way for drivers to extract RIDs without resorting to magic numbers.

---
 drivers/pci/controller/pcie-apple.c | 2 +-
 include/linux/pci.h                 | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/pci/controller/pcie-apple.c +1 -1
index 2d92fc79f6dd..6bf9fe102e2f 100644
@@ -802,7 +802,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci
 		u32 val;
 
 		val = readl_relaxed(port_rid2sid_addr(port, idx));
-		if ((val & 0xffff) == rid) {
+		if ((val & PCI_RID_MASK) == rid) {
 			apple_pcie_rid2sid_write(port, idx, 0);
 			bitmap_release_region(port->sid_map, idx, 0);
 			dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
include/linux/pci.h +0 -0
index 864775651c6f..e3d9730121c8 100644

include/linux/pci.h line 74

Quoted 4 lines
include/linux/pci.h +1 -0
@@ -71,6 +71,7 @@
 #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
 /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
 #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff

Even the naming of this macro clashes with the current nomenclature, where we use PCI_DEVID()/pci_dev_id() to manipulate this information.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#4 lore raw

Marc, Lukas,

Quoted 43 lines · Marc Zyngier
Quoted 2 lines · Aksh Garg

Routing ID (RID) is a 16-bit value composed of bus number and devfn.

No. RID here means "Requester ID". Please lookup the PCI spec.

Quoted 2 lines

Add a PCI_RID_MASK macro to eliminate the use of magic number 0xffff when masking for RIDs.

I'm not convinced this is universally true. Some RIDs (or RID-like identifiers, such as Stream IDs and Device IDs) are larger than 16 bit, depending on the integration.

The fact that this particular integration uses a 16bit RID without any extra qualifier in the upper bits doesn't make it universal.

Quoted 26 lines

This provides a standard way for drivers to extract RIDs without resorting to magic numbers.

---
  drivers/pci/controller/pcie-apple.c | 2 +-
  include/linux/pci.h                 | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
drivers/pci/controller/pcie-apple.c +1 -1
index 2d92fc79f6dd..6bf9fe102e2f 100644
@@ -802,7 +802,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci
  		u32 val;
 
  		val = readl_relaxed(port_rid2sid_addr(port, idx));
-		if ((val & 0xffff) == rid) {
+		if ((val & PCI_RID_MASK) == rid) {
  			apple_pcie_rid2sid_write(port, idx, 0);
  			bitmap_release_region(port->sid_map, idx, 0);
  			dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
include/linux/pci.h +0 -0
index 864775651c6f..e3d9730121c8 100644
Quoted 9 lines

include/linux/pci.h line 74

Quoted 5 lines
include/linux/pci.h +1 -0
@@ -71,6 +71,7 @@
  #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
  /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
  #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff

Even the naming of this macro clashes with the current nomenclature, where we use PCI_DEVID()/pci_dev_id() to manipulate this information.

Is the below proposed macro acceptable at the same place:
#define PCI_GET_DEVID(x) ((x) & 0xffff)

Regards,
Aksh Garg

Quoted 4 lines

Thanks,

	M.
maintainer
#5 lore raw
Quoted 56 lines · Aksh Garg

Marc, Lukas,

Quoted 42 lines · Marc Zyngier
Quoted 2 lines · Aksh Garg

Routing ID (RID) is a 16-bit value composed of bus number and devfn.

No. RID here means "Requester ID". Please lookup the PCI spec.

Quoted 2 lines

Add a PCI_RID_MASK macro to eliminate the use of magic number 0xffff when masking for RIDs.

I'm not convinced this is universally true. Some RIDs (or RID-like identifiers, such as Stream IDs and Device IDs) are larger than 16 bit, depending on the integration.

The fact that this particular integration uses a 16bit RID without any extra qualifier in the upper bits doesn't make it universal.

Quoted 25 lines

This provides a standard way for drivers to extract RIDs without resorting to magic numbers.

---
  drivers/pci/controller/pcie-apple.c | 2 +-
  include/linux/pci.h                 | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
drivers/pci/controller/pcie-apple.c +1 -1
index 2d92fc79f6dd..6bf9fe102e2f 100644
@@ -802,7 +802,7 @@ static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci
  		u32 val;
    		val = readl_relaxed(port_rid2sid_addr(port, idx));
-		if ((val & 0xffff) == rid) {
+		if ((val & PCI_RID_MASK) == rid) {
  			apple_pcie_rid2sid_write(port, idx, 0);
  			bitmap_release_region(port->sid_map, idx, 0);
  			dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
include/linux/pci.h +0 -0
index 864775651c6f..e3d9730121c8 100644
Quoted 9 lines

include/linux/pci.h line 74

Quoted 5 lines
include/linux/pci.h +1 -0
@@ -71,6 +71,7 @@
  #define PCI_DEVID(bus, devfn)	((((u16)(bus)) << 8) | (devfn))
  /* return bus from PCI devid = ((u16)bus_number) << 8) | devfn */
  #define PCI_BUS_NUM(x) (((x) >> 8) & 0xff)
+#define PCI_RID_MASK	0xffff

Even the naming of this macro clashes with the current nomenclature, where we use PCI_DEVID()/pci_dev_id() to manipulate this information.

Quoted 2 lines

Is the below proposed macro acceptable at the same place:
#define PCI_GET_DEVID(x) ((x) & 0xffff)

But what meaning does this have? What containing type are you extracting the value from?

The code you're hacking extracts the value from a register. What if the register had a completely different layout? Would you move the mask to match the layout of that particular register?

If you really want to get rid of what you consider a magic number, add a bunch of fields to the apple driver to describe the rid2sid registers, and use that with FIELD_GET()/FIELD_PREP() to manipulate the registers.

But what you are proposing here is not generic. It has no defined semantics, and is actively misleading.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.